| 1 | //===-- Single-precision sinpif 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/sinpif.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/PolyEval.h" |
| 14 | #include "src/__support/FPUtil/multiply_add.h" |
| 15 | #include "src/__support/common.h" |
| 16 | #include "src/__support/macros/config.h" |
| 17 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 18 | |
| 19 | namespace LIBC_NAMESPACE_DECL { |
| 20 | |
| 21 | LLVM_LIBC_FUNCTION(float, sinpif, (float x)) { |
| 22 | using FPBits = typename fputil::FPBits<float>; |
| 23 | FPBits xbits(x); |
| 24 | |
| 25 | uint32_t x_u = xbits.uintval(); |
| 26 | uint32_t x_abs = x_u & 0x7fff'ffffU; |
| 27 | double xd = static_cast<double>(x); |
| 28 | |
| 29 | // Range reduction: |
| 30 | // For |x| > 1/32, we perform range reduction as follows: |
| 31 | // Find k and y such that: |
| 32 | // x = (k + y) * 1/32 |
| 33 | // k is an integer |
| 34 | // |y| < 0.5 |
| 35 | // |
| 36 | // This is done by performing: |
| 37 | // k = round(x * 32) |
| 38 | // y = x * 32 - k |
| 39 | // |
| 40 | // Once k and y are computed, we then deduce the answer by the sine of sum |
| 41 | // formula: |
| 42 | // sin(x * pi) = sin((k + y)*pi/32) |
| 43 | // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 44 | // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed |
| 45 | // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
| 46 | // computed using degree-7 and degree-6 minimax polynomials generated by |
| 47 | // Sollya respectively. |
| 48 | |
| 49 | // |x| <= 1/16 |
| 50 | if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) { |
| 51 | |
| 52 | if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) { |
| 53 | if (LIBC_UNLIKELY(x_abs == 0U)) { |
| 54 | // For signed zeros. |
| 55 | return x; |
| 56 | } |
| 57 | |
| 58 | // For very small values we can approximate sinpi(x) with x * pi |
| 59 | // An exhaustive test shows that this is accurate for |x| < 9.546391 × |
| 60 | // 10-8 |
| 61 | double xdpi = xd * 0x1.921fb54442d18p1; |
| 62 | return static_cast<float>(xdpi); |
| 63 | } |
| 64 | |
| 65 | // |x| < 1/16. |
| 66 | double xsq = xd * xd; |
| 67 | |
| 68 | // Degree-9 polynomial approximation: |
| 69 | // sinpi(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9 |
| 70 | // = x (1 + a_3 x^2 + ... + a_9 x^8) |
| 71 | // = x * P(x^2) |
| 72 | // generated by Sollya with the following commands: |
| 73 | // > display = hexadecimal; |
| 74 | // > Q = fpminimax(sin(pi * x)/x, [|0, 2, 4, 6, 8|], [|D...|], [0, 1/16]); |
| 75 | double result = fputil::polyeval( |
| 76 | xsq, 0x1.921fb54442d18p1, -0x1.4abbce625bbf2p2, 0x1.466bc675e116ap1, |
| 77 | -0x1.32d2c0b62d41cp-1, 0x1.501ec4497cb7dp-4); |
| 78 | return static_cast<float>(xd * result); |
| 79 | } |
| 80 | |
| 81 | // Numbers greater or equal to 2^23 are always integers or NaN |
| 82 | if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) { |
| 83 | |
| 84 | // check for NaN values |
| 85 | if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) { |
| 86 | if (xbits.is_signaling_nan()) { |
| 87 | fputil::raise_except_if_required(FE_INVALID); |
| 88 | return FPBits::quiet_nan().get_val(); |
| 89 | } |
| 90 | |
| 91 | if (x_abs == 0x7f80'0000U) { |
| 92 | fputil::set_errno_if_required(EDOM); |
| 93 | fputil::raise_except_if_required(FE_INVALID); |
| 94 | } |
| 95 | |
| 96 | return x + FPBits::quiet_nan().get_val(); |
| 97 | } |
| 98 | |
| 99 | return FPBits::zero(xbits.sign()).get_val(); |
| 100 | } |
| 101 | |
| 102 | // Combine the results with the sine of sum formula: |
| 103 | // sin(x * pi) = sin((k + y)*pi/32) |
| 104 | // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 105 | // = sin_y * cos_k + (1 + cosm1_y) * sin_k |
| 106 | // = sin_y * cos_k + (cosm1_y * sin_k + sin_k) |
| 107 | double sin_k, cos_k, sin_y, cosm1_y; |
| 108 | sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y); |
| 109 | |
| 110 | if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) |
| 111 | return FPBits::zero(xbits.sign()).get_val(); |
| 112 | |
| 113 | return static_cast<float>(fputil::multiply_add( |
| 114 | sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); |
| 115 | } |
| 116 | |
| 117 | } // namespace LIBC_NAMESPACE_DECL |
| 118 | |