1//===-- Implementation header for cospif16 ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "sincosf16_utils.h"
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/is_integer.h"
21#include "src/__support/FPUtil/multiply_add.h"
22#include "src/__support/macros/optimization.h"
23
24namespace LIBC_NAMESPACE_DECL {
25
26namespace math {
27
28LIBC_INLINE constexpr float16 cospif16(float16 x) {
29
30 using namespace sincosf16_internal;
31 using FPBits = typename fputil::FPBits<float16>;
32 FPBits xbits(x);
33
34 uint16_t x_u = xbits.uintval();
35 uint16_t x_abs = x_u & 0x7fff;
36 float xf = x;
37
38 // Range reduction:
39 // For |x| > 1/32, we perform range reduction as follows:
40 // Find k and y such that:
41 // x = (k + y) * 1/32
42 // k is an integer
43 // |y| < 0.5
44 //
45 // This is done by performing:
46 // k = round(x * 32)
47 // y = x * 32 - k
48 //
49 // Once k and y are computed, we then deduce the answer by the cosine of sum
50 // formula:
51 // cos(x * pi) = cos((k + y) * pi/32)
52 // = cos(k * pi/32) * cos(y * pi/32) +
53 // sin(y * pi/32) * sin(k * pi/32)
54
55 // TODO: Revisit float16 literals after resolving C++17 compatibility:
56 // https://github.com/llvm/llvm-project/issues/207380
57 // For signed zeros
58 if (LIBC_UNLIKELY(x_abs == 0U))
59 return FPBits::one().get_val();
60
61 // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
62 if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
63 if (LIBC_UNLIKELY(x_abs <= 0x67FF))
64 return FPBits::one(sign: (x_abs & 0x1) ? Sign::NEG : Sign::POS).get_val();
65
66 // Check for NaN or infintiy values
67 if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
68 if (xbits.is_signaling_nan()) {
69 fputil::raise_except_if_required(FE_INVALID);
70 return FPBits::quiet_nan().get_val();
71 }
72 // If value is equal to infinity
73 if (x_abs == 0x7c00) {
74 fputil::set_errno_if_required(EDOM);
75 fputil::raise_except_if_required(FE_INVALID);
76 }
77
78 return x + FPBits::quiet_nan().get_val();
79 }
80
81 return FPBits::one().get_val();
82 }
83
84 // Integers have exact results of +1 or -1.
85 if (LIBC_UNLIKELY(fputil::is_integer(x)))
86 return FPBits::one(sign: fputil::is_odd_integer(x) ? Sign::NEG : Sign::POS)
87 .get_val();
88
89 float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
90 sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
91
92 if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))
93 return FPBits::zero().get_val();
94
95 // Since, cosm1_y = cos_y - 1, therefore:
96 // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
97 return fputil::cast<float16>(x: fputil::multiply_add(
98 x: cos_k, y: cosm1_y, z: fputil::sub_multiply(x: cos_k, y: sin_k, z: sin_y)));
99}
100
101} // namespace math
102
103} // namespace LIBC_NAMESPACE_DECL
104
105#endif // LIBC_TYPES_HAS_FLOAT16
106
107#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF16_H
108