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
21namespace LIBC_NAMESPACE_DECL {
22
23namespace math {
24
25LIBC_INLINE float cospif(float x) {
26 using namespace sincosf_utils_internal;
27 using FPBits = typename fputil::FPBits<float>;
28
29 FPBits xbits(x);
30 xbits.set_sign(Sign::POS);
31
32 uint32_t x_abs = xbits.uintval();
33 double xd = static_cast<double>(xbits.get_val());
34
35 // Range reduction:
36 // For |x| > 1/32, we perform range reduction as follows:
37 // Find k and y such that:
38 // x = (k + y) * 1/32
39 // k is an integer
40 // |y| < 0.5
41 //
42 // This is done by performing:
43 // k = round(x * 32)
44 // y = x * 32 - k
45 //
46 // Once k and y are computed, we then deduce the answer by the cosine of sum
47 // formula:
48 // cospi(x) = cos((k + y)*pi/32)
49 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
50 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed
51 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
52 // computed using degree-7 and degree-6 minimax polynomials generated by
53 // Sollya respectively.
54
55 // The exhautive test passes for smaller values
56 if (LIBC_UNLIKELY(x_abs < 0x38A2'F984U)) {
57
58#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)
59 return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f);
60#else
61 return static_cast<float>(fputil::multiply_add(x: xd, y: -0x1.0p-25, z: 1.0));
62#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
63 }
64
65 // Numbers greater or equal to 2^23 are always integers or NaN
66 if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
67
68 if (LIBC_UNLIKELY(x_abs < 0x4B80'0000)) {
69 return (x_abs & 0x1) ? -1.0f : 1.0f;
70 }
71
72 // x is inf or nan.
73 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
74 if (xbits.is_signaling_nan()) {
75 fputil::raise_except_if_required(FE_INVALID);
76 return FPBits::quiet_nan().get_val();
77 }
78
79 if (x_abs == 0x7f80'0000U) {
80 fputil::set_errno_if_required(EDOM);
81 fputil::raise_except_if_required(FE_INVALID);
82 }
83 return x + FPBits::quiet_nan().get_val();
84 }
85
86 return 1.0f;
87 }
88
89 // Combine the results with the sine of sum formula:
90 // cos(pi * x) = cos((k + y)*pi/32)
91 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
92 // = (cosm1_y + 1) * cos_k - sin_y * sin_k
93 // = (cosm1_y * cos_k + cos_k) - sin_y * sin_k
94 double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
95
96 sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
97
98 if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
99 return 0.0f;
100 }
101
102 return static_cast<float>(fputil::multiply_add(
103 x: sin_y, y: -sin_k, z: fputil::multiply_add(x: cosm1_y, y: cos_k, z: cos_k)));
104}
105
106} // namespace math
107
108} // namespace LIBC_NAMESPACE_DECL
109
110#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF_H
111