110 lines · c
1//===-- Implementation header for cospif ------------------------*- 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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF_H11 12#include "sincosf_utils.h"13#include "src/__support/FPUtil/FEnvImpl.h"14#include "src/__support/FPUtil/FPBits.h"15#include "src/__support/FPUtil/multiply_add.h"16#include "src/__support/common.h"17#include "src/__support/macros/config.h"18#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY19#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA20 21namespace LIBC_NAMESPACE_DECL {22 23namespace math {24 25LIBC_INLINE static constexpr float cospif(float x) {26 using FPBits = typename fputil::FPBits<float>;27 28 FPBits xbits(x);29 xbits.set_sign(Sign::POS);30 31 uint32_t x_abs = xbits.uintval();32 double xd = static_cast<double>(xbits.get_val());33 34 // Range reduction:35 // For |x| > 1/32, we perform range reduction as follows:36 // Find k and y such that:37 // x = (k + y) * 1/3238 // k is an integer39 // |y| < 0.540 //41 // This is done by performing:42 // k = round(x * 32)43 // y = x * 32 - k44 //45 // Once k and y are computed, we then deduce the answer by the cosine of sum46 // formula:47 // cospi(x) = cos((k + y)*pi/32)48 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)49 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed50 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are51 // computed using degree-7 and degree-6 minimax polynomials generated by52 // Sollya respectively.53 54 // The exhautive test passes for smaller values55 if (LIBC_UNLIKELY(x_abs < 0x38A2'F984U)) {56 57#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)58 return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f);59#else60 return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, 1.0));61#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT62 }63 64 // Numbers greater or equal to 2^23 are always integers or NaN65 if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {66 67 if (LIBC_UNLIKELY(x_abs < 0x4B80'0000)) {68 return (x_abs & 0x1) ? -1.0f : 1.0f;69 }70 71 // x is inf or nan.72 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {73 if (xbits.is_signaling_nan()) {74 fputil::raise_except_if_required(FE_INVALID);75 return FPBits::quiet_nan().get_val();76 }77 78 if (x_abs == 0x7f80'0000U) {79 fputil::set_errno_if_required(EDOM);80 fputil::raise_except_if_required(FE_INVALID);81 }82 return x + FPBits::quiet_nan().get_val();83 }84 85 return 1.0f;86 }87 88 // Combine the results with the sine of sum formula:89 // cos(pi * x) = cos((k + y)*pi/32)90 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)91 // = (cosm1_y + 1) * cos_k - sin_y * sin_k92 // = (cosm1_y * cos_k + cos_k) - sin_y * sin_k93 double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;94 95 sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);96 97 if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {98 return 0.0f;99 }100 101 return static_cast<float>(fputil::multiply_add(102 sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k)));103}104 105} // namespace math106 107} // namespace LIBC_NAMESPACE_DECL108 109#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H110