| 1 | //===-- Implementation header for sinpif16 -------------------------* C++*-===// |
| 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_SINPIF16_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H |
| 11 | |
| 12 | #include "include/llvm-libc-macros/float16-macros.h" |
| 13 | |
| 14 | #ifdef LIBC_TYPES_HAS_FLOAT16 |
| 15 | |
| 16 | #include "sincosf16_utils.h" |
| 17 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 18 | #include "src/__support/FPUtil/FPBits.h" |
| 19 | #include "src/__support/FPUtil/cast.h" |
| 20 | #include "src/__support/FPUtil/multiply_add.h" |
| 21 | #include "src/__support/common.h" |
| 22 | #include "src/__support/macros/config.h" |
| 23 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 24 | |
| 25 | namespace LIBC_NAMESPACE_DECL { |
| 26 | namespace math { |
| 27 | |
| 28 | LIBC_INLINE float16 sinpif16(float16 x) { |
| 29 | using namespace sincosf16_internal; |
| 30 | using FPBits = typename fputil::FPBits<float16>; |
| 31 | FPBits xbits(x); |
| 32 | |
| 33 | uint16_t x_u = xbits.uintval(); |
| 34 | uint16_t x_abs = x_u & 0x7fff; |
| 35 | float xf = x; |
| 36 | |
| 37 | // Range reduction: |
| 38 | // For |x| > 1/32, we perform range reduction as follows: |
| 39 | // Find k and y such that: |
| 40 | // x = (k + y) * 1/32 |
| 41 | // k is an integer |
| 42 | // |y| < 0.5 |
| 43 | // |
| 44 | // This is done by performing: |
| 45 | // k = round(x * 32) |
| 46 | // y = x * 32 - k |
| 47 | // |
| 48 | // Once k and y are computed, we then deduce the answer by the sine of sum |
| 49 | // formula: |
| 50 | // sin(x * pi) = sin((k + y) * pi/32) |
| 51 | // = sin(k * pi/32) * cos(y * pi/32) + |
| 52 | // sin(y * pi/32) * cos(k * pi/32) |
| 53 | |
| 54 | // For signed zeros |
| 55 | if (LIBC_UNLIKELY(x_abs == 0U)) |
| 56 | return x; |
| 57 | |
| 58 | // Numbers greater or equal to 2^10 are integers, or infinity, or NaN |
| 59 | if (LIBC_UNLIKELY(x_abs >= 0x6400)) { |
| 60 | // Check for NaN or infinity values |
| 61 | if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { |
| 62 | if (xbits.is_signaling_nan()) { |
| 63 | fputil::raise_except_if_required(FE_INVALID); |
| 64 | return FPBits::quiet_nan().get_val(); |
| 65 | } |
| 66 | // If value is equal to infinity |
| 67 | if (x_abs == 0x7c00) { |
| 68 | fputil::set_errno_if_required(EDOM); |
| 69 | fputil::raise_except_if_required(FE_INVALID); |
| 70 | } |
| 71 | |
| 72 | return x + FPBits::quiet_nan().get_val(); |
| 73 | } |
| 74 | return FPBits::zero(sign: xbits.sign()).get_val(); |
| 75 | } |
| 76 | |
| 77 | float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0; |
| 78 | sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); |
| 79 | |
| 80 | if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) |
| 81 | return FPBits::zero(sign: xbits.sign()).get_val(); |
| 82 | |
| 83 | // Since, cosm1_y = cos_y - 1, therefore: |
| 84 | // sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) |
| 85 | return fputil::cast<float16>(x: fputil::multiply_add( |
| 86 | x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k))); |
| 87 | } |
| 88 | |
| 89 | } // namespace math |
| 90 | } // namespace LIBC_NAMESPACE_DECL |
| 91 | |
| 92 | #endif // LIBC_TYPES_HAS_FLOAT16 |
| 93 | |
| 94 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H |
| 95 | |