|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF_H |
| 11 | + |
| 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_UNLIKELY |
| 19 | +#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
| 20 | + |
| 21 | +namespace LIBC_NAMESPACE_DECL { |
| 22 | + |
| 23 | +namespace math { |
| 24 | + |
| 25 | +LIBC_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/32 |
| 38 | + // k is an integer |
| 39 | + // |y| < 0.5 |
| 40 | + // |
| 41 | + // This is done by performing: |
| 42 | + // k = round(x * 32) |
| 43 | + // y = x * 32 - k |
| 44 | + // |
| 45 | + // Once k and y are computed, we then deduce the answer by the cosine of sum |
| 46 | + // 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 precomputed |
| 50 | + // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
| 51 | + // computed using degree-7 and degree-6 minimax polynomials generated by |
| 52 | + // Sollya respectively. |
| 53 | + |
| 54 | + // The exhautive test passes for smaller values |
| 55 | + 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 | +#else |
| 60 | + return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, 1.0)); |
| 61 | +#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 62 | + } |
| 63 | + |
| 64 | + // Numbers greater or equal to 2^23 are always integers or NaN |
| 65 | + 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_k |
| 92 | + // = (cosm1_y * cos_k + cos_k) - sin_y * sin_k |
| 93 | + 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 math |
| 106 | + |
| 107 | +} // namespace LIBC_NAMESPACE_DECL |
| 108 | + |
| 109 | +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H |
0 commit comments