82 lines · cpp
1//===-- Half-precision sinpif 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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/math/sinpif16.h"10#include "hdr/errno_macros.h"11#include "hdr/fenv_macros.h"12#include "src/__support/FPUtil/FEnvImpl.h"13#include "src/__support/FPUtil/FPBits.h"14#include "src/__support/FPUtil/cast.h"15#include "src/__support/FPUtil/multiply_add.h"16#include "src/__support/math/sincosf16_utils.h"17 18namespace LIBC_NAMESPACE_DECL {19 20LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) {21 using namespace sincosf16_internal;22 using FPBits = typename fputil::FPBits<float16>;23 FPBits xbits(x);24 25 uint16_t x_u = xbits.uintval();26 uint16_t x_abs = x_u & 0x7fff;27 float xf = x;28 29 // Range reduction:30 // For |x| > 1/32, we perform range reduction as follows:31 // Find k and y such that:32 // x = (k + y) * 1/3233 // k is an integer34 // |y| < 0.535 //36 // This is done by performing:37 // k = round(x * 32)38 // y = x * 32 - k39 //40 // Once k and y are computed, we then deduce the answer by the sine of sum41 // formula:42 // sin(x * pi) = sin((k + y) * pi/32)43 // = sin(k * pi/32) * cos(y * pi/32) +44 // sin(y * pi/32) * cos(k * pi/32)45 46 // For signed zeros47 if (LIBC_UNLIKELY(x_abs == 0U))48 return x;49 50 // Numbers greater or equal to 2^10 are integers, or infinity, or NaN51 if (LIBC_UNLIKELY(x_abs >= 0x6400)) {52 // Check for NaN or infinity values53 if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {54 if (xbits.is_signaling_nan()) {55 fputil::raise_except_if_required(FE_INVALID);56 return FPBits::quiet_nan().get_val();57 }58 // If value is equal to infinity59 if (x_abs == 0x7c00) {60 fputil::set_errno_if_required(EDOM);61 fputil::raise_except_if_required(FE_INVALID);62 }63 64 return x + FPBits::quiet_nan().get_val();65 }66 return FPBits::zero(xbits.sign()).get_val();67 }68 69 float sin_k, cos_k, sin_y, cosm1_y;70 sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);71 72 if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))73 return FPBits::zero(xbits.sign()).get_val();74 75 // Since, cosm1_y = cos_y - 1, therefore:76 // sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)77 return fputil::cast<float16>(fputil::multiply_add(78 sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));79}80 81} // namespace LIBC_NAMESPACE_DECL82