| 1 | //===-- Half-precision sin(x) 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/sinf16.h" |
| 10 | #include "hdr/errno_macros.h" |
| 11 | #include "hdr/fenv_macros.h" |
| 12 | #include "sincosf16_utils.h" |
| 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | #include "src/__support/FPUtil/FPBits.h" |
| 15 | #include "src/__support/FPUtil/cast.h" |
| 16 | #include "src/__support/FPUtil/except_value_utils.h" |
| 17 | #include "src/__support/FPUtil/multiply_add.h" |
| 18 | #include "src/__support/macros/optimization.h" |
| 19 | |
| 20 | namespace LIBC_NAMESPACE_DECL { |
| 21 | |
| 22 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 23 | constexpr size_t N_EXCEPTS = 4; |
| 24 | |
| 25 | constexpr fputil::ExceptValues<float16, N_EXCEPTS> SINF16_EXCEPTS{{ |
| 26 | // (input, RZ output, RU offset, RD offset, RN offset) |
| 27 | {0x2b45, 0x2b43, 1, 0, 1}, |
| 28 | {0x585c, 0x3ba3, 1, 0, 1}, |
| 29 | {0x5cb0, 0xbbff, 0, 1, 0}, |
| 30 | {0x51f5, 0xb80f, 0, 1, 0}, |
| 31 | }}; |
| 32 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 33 | |
| 34 | LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) { |
| 35 | using FPBits = fputil::FPBits<float16>; |
| 36 | FPBits xbits(x); |
| 37 | |
| 38 | uint16_t x_u = xbits.uintval(); |
| 39 | uint16_t x_abs = x_u & 0x7fff; |
| 40 | float xf = x; |
| 41 | |
| 42 | // Range reduction: |
| 43 | // For |x| > pi/32, we perform range reduction as follows: |
| 44 | // Find k and y such that: |
| 45 | // x = (k + y) * pi/32 |
| 46 | // k is an integer, |y| < 0.5 |
| 47 | // |
| 48 | // This is done by performing: |
| 49 | // k = round(x * 32/pi) |
| 50 | // y = x * 32/pi - k |
| 51 | // |
| 52 | // Once k and y are computed, we then deduce the answer by the sine of sum |
| 53 | // formula: |
| 54 | // sin(x) = sin((k + y) * pi/32) |
| 55 | // = sin(k * pi/32) * cos(y * pi/32) + |
| 56 | // sin(y * pi/32) * cos(k * pi/32) |
| 57 | |
| 58 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 59 | // Handle exceptional values |
| 60 | bool x_sign = x_u >> 15; |
| 61 | |
| 62 | if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign); |
| 63 | LIBC_UNLIKELY(r.has_value())) |
| 64 | return r.value(); |
| 65 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 66 | |
| 67 | int rounding = fputil::quick_get_round(); |
| 68 | |
| 69 | // Exhaustive tests show that for |x| <= 0x1.f4p-11, 1ULP rounding errors |
| 70 | // occur. To fix this, the following apply: |
| 71 | if (LIBC_UNLIKELY(x_abs <= 0x13d0)) { |
| 72 | // sin(+/-0) = +/-0 |
| 73 | if (LIBC_UNLIKELY(x_abs == 0U)) |
| 74 | return x; |
| 75 | |
| 76 | // When x > 0, and rounding upward, sin(x) == x. |
| 77 | // When x < 0, and rounding downward, sin(x) == x. |
| 78 | if ((rounding == FE_UPWARD && xbits.is_pos()) || |
| 79 | (rounding == FE_DOWNWARD && xbits.is_neg())) |
| 80 | return x; |
| 81 | |
| 82 | // When x < 0, and rounding upward, sin(x) == (x - 1ULP) |
| 83 | if (rounding == FE_UPWARD && xbits.is_neg()) { |
| 84 | x_u--; |
| 85 | return FPBits(x_u).get_val(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (xbits.is_inf_or_nan()) { |
| 90 | if (xbits.is_signaling_nan()) { |
| 91 | fputil::raise_except_if_required(FE_INVALID); |
| 92 | return FPBits::quiet_nan().get_val(); |
| 93 | } |
| 94 | |
| 95 | if (xbits.is_inf()) { |
| 96 | fputil::set_errno_if_required(EDOM); |
| 97 | fputil::raise_except_if_required(FE_INVALID); |
| 98 | } |
| 99 | |
| 100 | return x + FPBits::quiet_nan().get_val(); |
| 101 | } |
| 102 | |
| 103 | float sin_k, cos_k, sin_y, cosm1_y; |
| 104 | sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); |
| 105 | |
| 106 | if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) |
| 107 | return FPBits::zero(xbits.sign()).get_val(); |
| 108 | |
| 109 | // Since, cosm1_y = cos_y - 1, therefore: |
| 110 | // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) |
| 111 | return fputil::cast<float16>(fputil::multiply_add( |
| 112 | sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); |
| 113 | } |
| 114 | |
| 115 | } // namespace LIBC_NAMESPACE_DECL |
| 116 | |