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