1 | //===-- Single-precision cospi function -----------------------------------===// |
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 | #include "src/math/cospif.h" |
10 | #include "sincosf_utils.h" |
11 | #include "src/__support/FPUtil/FEnvImpl.h" |
12 | #include "src/__support/FPUtil/FPBits.h" |
13 | #include "src/__support/FPUtil/multiply_add.h" |
14 | #include "src/__support/common.h" |
15 | #include "src/__support/macros/config.h" |
16 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
17 | #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
18 | |
19 | namespace LIBC_NAMESPACE_DECL { |
20 | |
21 | LLVM_LIBC_FUNCTION(float, cospif, (float x)) { |
22 | using FPBits = typename fputil::FPBits<float>; |
23 | |
24 | FPBits xbits(x); |
25 | xbits.set_sign(Sign::POS); |
26 | |
27 | uint32_t x_abs = xbits.uintval(); |
28 | double xd = static_cast<double>(xbits.get_val()); |
29 | |
30 | // Range reduction: |
31 | // For |x| > 1/32, we perform range reduction as follows: |
32 | // Find k and y such that: |
33 | // x = (k + y) * 1/32 |
34 | // k is an integer |
35 | // |y| < 0.5 |
36 | // |
37 | // This is done by performing: |
38 | // k = round(x * 32) |
39 | // y = x * 32 - k |
40 | // |
41 | // Once k and y are computed, we then deduce the answer by the cosine of sum |
42 | // formula: |
43 | // cospi(x) = cos((k + y)*pi/32) |
44 | // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32) |
45 | // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed |
46 | // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
47 | // computed using degree-7 and degree-6 minimax polynomials generated by |
48 | // Sollya respectively. |
49 | |
50 | // The exhautive test passes for smaller values |
51 | if (LIBC_UNLIKELY(x_abs < 0x38A2'F984U)) { |
52 | |
53 | #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) |
54 | return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f); |
55 | #else |
56 | return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, 1.0)); |
57 | #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT |
58 | } |
59 | |
60 | // Numbers greater or equal to 2^23 are always integers or NaN |
61 | if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) { |
62 | |
63 | if (LIBC_UNLIKELY(x_abs < 0x4B80'0000)) { |
64 | return (x_abs & 0x1) ? -1.0f : 1.0f; |
65 | } |
66 | |
67 | // x is inf or nan. |
68 | if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) { |
69 | if (xbits.is_signaling_nan()) { |
70 | fputil::raise_except_if_required(FE_INVALID); |
71 | return FPBits::quiet_nan().get_val(); |
72 | } |
73 | |
74 | if (x_abs == 0x7f80'0000U) { |
75 | fputil::set_errno_if_required(EDOM); |
76 | fputil::raise_except_if_required(FE_INVALID); |
77 | } |
78 | return x + FPBits::quiet_nan().get_val(); |
79 | } |
80 | |
81 | return 1.0f; |
82 | } |
83 | |
84 | // Combine the results with the sine of sum formula: |
85 | // cos(pi * x) = cos((k + y)*pi/32) |
86 | // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32) |
87 | // = (cosm1_y + 1) * cos_k - sin_y * sin_k |
88 | // = (cosm1_y * cos_k + cos_k) - sin_y * sin_k |
89 | double sin_k, cos_k, sin_y, cosm1_y; |
90 | |
91 | sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y); |
92 | |
93 | if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) { |
94 | return 0.0f; |
95 | } |
96 | |
97 | return static_cast<float>(fputil::multiply_add( |
98 | sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k))); |
99 | } |
100 | |
101 | } // namespace LIBC_NAMESPACE_DECL |
102 | |