| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 10 | /// Implementation of the sinbf16 function. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H |
| 15 | #define LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H |
| 16 | |
| 17 | #include "hdr/errno_macros.h" |
| 18 | #include "hdr/fenv_macros.h" |
| 19 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 20 | #include "src/__support/FPUtil/FPBits.h" |
| 21 | #include "src/__support/FPUtil/bfloat16.h" |
| 22 | #include "src/__support/FPUtil/cast.h" |
| 23 | #include "src/__support/FPUtil/multiply_add.h" |
| 24 | #include "src/__support/macros/attributes.h" |
| 25 | #include "src/__support/macros/config.h" |
| 26 | #include "src/__support/macros/optimization.h" |
| 27 | #include "src/__support/math/sincosf_utils.h" |
| 28 | |
| 29 | namespace LIBC_NAMESPACE_DECL { |
| 30 | |
| 31 | namespace math { |
| 32 | |
| 33 | LIBC_INLINE bfloat16 sinbf16(bfloat16 x) { |
| 34 | using namespace sincosf_utils_internal; |
| 35 | |
| 36 | using FPBits = fputil::FPBits<bfloat16>; |
| 37 | FPBits xbits(x); |
| 38 | |
| 39 | uint16_t x_u = xbits.uintval(); |
| 40 | uint16_t x_abs = x_u & 0x7fff; |
| 41 | float xf = x; |
| 42 | |
| 43 | // |x| < inf (0x7f80) |
| 44 | if (x_abs < 0x7f80) { |
| 45 | // |x| <= 0x1.24p-3f (0x3e12) |
| 46 | if (LIBC_UNLIKELY(x_abs <= 0x3e12)) { |
| 47 | // sin(+/-0) = +/-0 |
| 48 | if (LIBC_UNLIKELY(x_abs == 0)) |
| 49 | return x; |
| 50 | |
| 51 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 52 | int rounding = fputil::quick_get_round(); |
| 53 | // When x > 0, and rounding upward, sin(x) == x. |
| 54 | // When x < 0, and rounding downward, sin(x) == x. |
| 55 | if ((rounding == FE_UPWARD && xbits.is_pos()) || |
| 56 | (rounding == FE_DOWNWARD && xbits.is_neg())) |
| 57 | return x; |
| 58 | |
| 59 | // When x < 0, and rounding upward, sin(x) == (x - 1ULP) |
| 60 | if (rounding == FE_UPWARD && xbits.is_neg()) { |
| 61 | x_u--; |
| 62 | return FPBits(x_u).get_val(); |
| 63 | } |
| 64 | #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 65 | } |
| 66 | double xd = static_cast<double>(xf); |
| 67 | uint32_t x_abs_f = fputil::FPBits<float>(xf).uintval() & 0x7fffffff; |
| 68 | double sin_k, cos_k, sin_y, cosm1_y; |
| 69 | |
| 70 | sincosf_eval(xd, x_abs: x_abs_f, sin_k, cos_k, sin_y, cosm1_y); |
| 71 | // using sin(a + b) = sin(a)*cos(b) + cos(a)*sin(b) |
| 72 | // sin(x) = sin_k*cos_y + cos_k*sin_y |
| 73 | // but cosm1_y = cos_y - 1 --> cos_y = cosm1_y + 1 |
| 74 | // sin(x) = sin_k*cosm1_y + sin_k + cos_k * sin_y |
| 75 | |
| 76 | if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) |
| 77 | return FPBits::zero(sign: xbits.sign()).get_val(); |
| 78 | return fputil::cast<bfloat16>(x: fputil::multiply_add( |
| 79 | x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k))); |
| 80 | } |
| 81 | |
| 82 | // NaN |
| 83 | if (xbits.is_nan()) { |
| 84 | if (xbits.is_signaling_nan()) { |
| 85 | fputil::raise_except_if_required(FE_INVALID); |
| 86 | return FPBits::quiet_nan().get_val(); |
| 87 | } |
| 88 | return x; |
| 89 | } |
| 90 | |
| 91 | // +/- inf |
| 92 | fputil::set_errno_if_required(EDOM); |
| 93 | fputil::raise_except_if_required(FE_INVALID); |
| 94 | |
| 95 | return x + FPBits::quiet_nan().get_val(); |
| 96 | } |
| 97 | |
| 98 | } // namespace math |
| 99 | |
| 100 | } // namespace LIBC_NAMESPACE_DECL |
| 101 | |
| 102 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINBF16_H |
| 103 | |