1//===-- Half-precision cos(x) 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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/math/cosf16.h"
10#include "hdr/errno_macros.h"
11#include "hdr/fenv_macros.h"
12#include "sincosf16_utils.h"
13#include "src/__support/FPUtil/FEnvImpl.h"
14#include "src/__support/FPUtil/FPBits.h"
15#include "src/__support/FPUtil/cast.h"
16#include "src/__support/FPUtil/except_value_utils.h"
17#include "src/__support/FPUtil/multiply_add.h"
18#include "src/__support/macros/optimization.h"
19
20namespace LIBC_NAMESPACE_DECL {
21
22#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
23constexpr size_t N_EXCEPTS = 4;
24
25constexpr fputil::ExceptValues<float16, N_EXCEPTS> COSF16_EXCEPTS{{
26 // (input, RZ output, RU offset, RD offset, RN offset)
27 {0x2b7c, 0x3bfc, 1, 0, 1},
28 {0x4ac1, 0x38b5, 1, 0, 0},
29 {0x5c49, 0xb8c6, 0, 1, 0},
30 {0x7acc, 0xa474, 0, 1, 0},
31}};
32#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
33
34LLVM_LIBC_FUNCTION(float16, cosf16, (float16 x)) {
35 using FPBits = fputil::FPBits<float16>;
36 FPBits xbits(x);
37
38 uint16_t x_u = xbits.uintval();
39 uint16_t x_abs = x_u & 0x7fff;
40 float xf = x;
41
42 // Range reduction:
43 // For |x| > pi/32, we perform range reduction as follows:
44 // Find k and y such that:
45 // x = (k + y) * pi/32
46 // k is an integer, |y| < 0.5
47 //
48 // This is done by performing:
49 // k = round(x * 32/pi)
50 // y = x * 32/pi - k
51 //
52 // Once k and y are computed, we then deduce the answer by the cosine of sum
53 // formula:
54 // cos(x) = cos((k + y) * pi/32)
55 // = cos(k * pi/32) * cos(y * pi/32) -
56 // sin(k * pi/32) * sin(y * pi/32)
57
58#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
59 // Handle exceptional values
60 if (auto r = COSF16_EXCEPTS.lookup(x_abs); LIBC_UNLIKELY(r.has_value()))
61 return r.value();
62#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
63
64 // cos(+/-0) = 1
65 if (LIBC_UNLIKELY(x_abs == 0U))
66 return fputil::cast<float16>(1.0f);
67
68 // cos(+/-inf) = NaN, and cos(NaN) = NaN
69 if (xbits.is_inf_or_nan()) {
70 if (xbits.is_signaling_nan()) {
71 fputil::raise_except_if_required(FE_INVALID);
72 return FPBits::quiet_nan().get_val();
73 }
74
75 if (xbits.is_inf()) {
76 fputil::set_errno_if_required(EDOM);
77 fputil::raise_except_if_required(FE_INVALID);
78 }
79
80 return x + FPBits::quiet_nan().get_val();
81 }
82
83 float sin_k, cos_k, sin_y, cosm1_y;
84 sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
85 // Since, cosm1_y = cos_y - 1, therefore:
86 // cos(x) = cos_k * cos_y - sin_k * sin_y
87 // = cos_k * (cos_y - 1 + 1) - sin_k * sin_y
88 // = cos_k * cosm1_y - sin_k * sin_y + cos_k
89 return fputil::cast<float16>(fputil::multiply_add(
90 cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
91}
92
93} // namespace LIBC_NAMESPACE_DECL
94

source code of libc/src/math/generic/cosf16.cpp