1ddc3f2ddSwldfngrs //===-- Half-precision sinpif function ------------------------------------===// 2ddc3f2ddSwldfngrs // 3ddc3f2ddSwldfngrs // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4ddc3f2ddSwldfngrs // See https://llvm.org/LICENSE.txt for license information. 5ddc3f2ddSwldfngrs // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ddc3f2ddSwldfngrs // 7ddc3f2ddSwldfngrs //===----------------------------------------------------------------------===// 8ddc3f2ddSwldfngrs 9ddc3f2ddSwldfngrs #include "src/math/sinpif16.h" 10*7395ef54Swldfngrs #include "hdr/errno_macros.h" 11*7395ef54Swldfngrs #include "hdr/fenv_macros.h" 12*7395ef54Swldfngrs #include "sincosf16_utils.h" 13ddc3f2ddSwldfngrs #include "src/__support/FPUtil/FEnvImpl.h" 14ddc3f2ddSwldfngrs #include "src/__support/FPUtil/FPBits.h" 15ddc3f2ddSwldfngrs #include "src/__support/FPUtil/cast.h" 16ddc3f2ddSwldfngrs #include "src/__support/FPUtil/multiply_add.h" 17ddc3f2ddSwldfngrs 18ddc3f2ddSwldfngrs namespace LIBC_NAMESPACE_DECL { 19ddc3f2ddSwldfngrs 20ddc3f2ddSwldfngrs LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) { 21ddc3f2ddSwldfngrs using FPBits = typename fputil::FPBits<float16>; 22ddc3f2ddSwldfngrs FPBits xbits(x); 23ddc3f2ddSwldfngrs 24ddc3f2ddSwldfngrs uint16_t x_u = xbits.uintval(); 25ddc3f2ddSwldfngrs uint16_t x_abs = x_u & 0x7fff; 26*7395ef54Swldfngrs float xf = x; 27ddc3f2ddSwldfngrs 28ddc3f2ddSwldfngrs // Range reduction: 29ddc3f2ddSwldfngrs // For |x| > 1/32, we perform range reduction as follows: 30ddc3f2ddSwldfngrs // Find k and y such that: 31ddc3f2ddSwldfngrs // x = (k + y) * 1/32 32ddc3f2ddSwldfngrs // k is an integer 33ddc3f2ddSwldfngrs // |y| < 0.5 34ddc3f2ddSwldfngrs // 35ddc3f2ddSwldfngrs // This is done by performing: 36ddc3f2ddSwldfngrs // k = round(x * 32) 37ddc3f2ddSwldfngrs // y = x * 32 - k 38ddc3f2ddSwldfngrs // 39ddc3f2ddSwldfngrs // Once k and y are computed, we then deduce the answer by the sine of sum 40ddc3f2ddSwldfngrs // formula: 41ddc3f2ddSwldfngrs // sin(x * pi) = sin((k + y) * pi/32) 42*7395ef54Swldfngrs // = sin(k * pi/32) * cos(y * pi/32) + 43*7395ef54Swldfngrs // sin(y * pi/32) * cos(k * pi/32) 44ddc3f2ddSwldfngrs 45ddc3f2ddSwldfngrs // For signed zeros 46ddc3f2ddSwldfngrs if (LIBC_UNLIKELY(x_abs == 0U)) 47ddc3f2ddSwldfngrs return x; 48ddc3f2ddSwldfngrs 49ddc3f2ddSwldfngrs // Numbers greater or equal to 2^10 are integers, or infinity, or NaN 50ddc3f2ddSwldfngrs if (LIBC_UNLIKELY(x_abs >= 0x6400)) { 51ddc3f2ddSwldfngrs // Check for NaN or infinity values 52ddc3f2ddSwldfngrs if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { 53ddc3f2ddSwldfngrs // If value is equal to infinity 54ddc3f2ddSwldfngrs if (x_abs == 0x7c00) { 55ddc3f2ddSwldfngrs fputil::set_errno_if_required(EDOM); 56ddc3f2ddSwldfngrs fputil::raise_except_if_required(FE_INVALID); 57ddc3f2ddSwldfngrs } 58ddc3f2ddSwldfngrs 59ddc3f2ddSwldfngrs return x + FPBits::quiet_nan().get_val(); 60ddc3f2ddSwldfngrs } 61ddc3f2ddSwldfngrs return FPBits::zero(xbits.sign()).get_val(); 62ddc3f2ddSwldfngrs } 63ddc3f2ddSwldfngrs 64*7395ef54Swldfngrs float sin_k, cos_k, sin_y, cosm1_y; 65*7395ef54Swldfngrs sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); 66ddc3f2ddSwldfngrs 67ddc3f2ddSwldfngrs if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) 68ddc3f2ddSwldfngrs return FPBits::zero(xbits.sign()).get_val(); 69ddc3f2ddSwldfngrs 70ddc3f2ddSwldfngrs // Since, cosm1_y = cos_y - 1, therefore: 71ddc3f2ddSwldfngrs // sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) 72ddc3f2ddSwldfngrs return fputil::cast<float16>(fputil::multiply_add( 73ddc3f2ddSwldfngrs sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); 74ddc3f2ddSwldfngrs } 75*7395ef54Swldfngrs 76ddc3f2ddSwldfngrs } // namespace LIBC_NAMESPACE_DECL 77