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