1//===-- Half-precision cospif 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/cospif16.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/multiply_add.h"
17#include "src/__support/macros/optimization.h"
18
19namespace LIBC_NAMESPACE_DECL {
20
21LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
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/32
33 // k is an integer
34 // |y| < 0.5
35 //
36 // This is done by performing:
37 // k = round(x * 32)
38 // y = x * 32 - k
39 //
40 // Once k and y are computed, we then deduce the answer by the cosine of sum
41 // formula:
42 // cos(x * pi) = cos((k + y) * pi/32)
43 // = cos(k * pi/32) * cos(y * pi/32) +
44 // sin(y * pi/32) * sin(k * pi/32)
45
46 // For signed zeros
47 if (LIBC_UNLIKELY(x_abs == 0U))
48 return fputil::cast<float16>(1.0f);
49
50 // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
51 if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
52 if (LIBC_UNLIKELY(x_abs <= 0x67FF))
53 return fputil::cast<float16>((x_abs & 0x1) ? -1.0f : 1.0f);
54
55 // Check for NaN or infintiy values
56 if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
57 if (xbits.is_signaling_nan()) {
58 fputil::raise_except_if_required(FE_INVALID);
59 return FPBits::quiet_nan().get_val();
60 }
61 // If value is equal to infinity
62 if (x_abs == 0x7c00) {
63 fputil::set_errno_if_required(EDOM);
64 fputil::raise_except_if_required(FE_INVALID);
65 }
66
67 return x + FPBits::quiet_nan().get_val();
68 }
69
70 return fputil::cast<float16>(1.0f);
71 }
72
73 float sin_k, cos_k, sin_y, cosm1_y;
74 sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
75
76 if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))
77 return fputil::cast<float16>(0.0f);
78
79 // Since, cosm1_y = cos_y - 1, therefore:
80 // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
81 return fputil::cast<float16>(fputil::multiply_add(
82 cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
83}
84
85} // namespace LIBC_NAMESPACE_DECL
86

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