1//===-- Implementation header for sinpif16 -------------------------* 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_SINPIF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "sincosf16_utils.h"
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/is_integer.h"
21#include "src/__support/FPUtil/multiply_add.h"
22#include "src/__support/common.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
25
26namespace LIBC_NAMESPACE_DECL {
27namespace math {
28
29LIBC_INLINE float16 sinpif16(float16 x) {
30 using namespace sincosf16_internal;
31 using FPBits = typename fputil::FPBits<float16>;
32 FPBits xbits(x);
33
34 uint16_t x_u = xbits.uintval();
35 uint16_t x_abs = x_u & 0x7fff;
36 float xf = x;
37
38 // Range reduction:
39 // For |x| > 1/32, we perform range reduction as follows:
40 // Find k and y such that:
41 // x = (k + y) * 1/32
42 // k is an integer
43 // |y| < 0.5
44 //
45 // This is done by performing:
46 // k = round(x * 32)
47 // y = x * 32 - k
48 //
49 // Once k and y are computed, we then deduce the answer by the sine of sum
50 // formula:
51 // sin(x * pi) = sin((k + y) * pi/32)
52 // = sin(k * pi/32) * cos(y * pi/32) +
53 // sin(y * pi/32) * cos(k * pi/32)
54
55 // sinpi(x) underflows for 0 < |x| <= 0x1.45p-16.
56 if (LIBC_UNLIKELY(x_abs <= 0x0145U)) {
57 if (x_abs == 0U)
58 return x;
59 fputil::set_errno_if_required(ERANGE);
60 fputil::raise_underflow_except_if_required<float>();
61 }
62
63 // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
64 if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
65 // Check for NaN or infinity values
66 if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
67 if (xbits.is_signaling_nan()) {
68 fputil::raise_except_if_required(FE_INVALID);
69 return FPBits::quiet_nan().get_val();
70 }
71 // If value is equal to infinity
72 if (x_abs == 0x7c00) {
73 fputil::set_errno_if_required(EDOM);
74 fputil::raise_except_if_required(FE_INVALID);
75 }
76
77 return x + FPBits::quiet_nan().get_val();
78 }
79 return FPBits::zero(sign: xbits.sign()).get_val();
80 }
81
82 // Half-integers have exact results of +1 or -1.
83 if (LIBC_UNLIKELY(fputil::is_odd_integer(2.0f * xf)))
84 return fputil::is_odd_integer(x: xf - 0.5f) ? -1.0f16 : 1.0f16;
85
86 float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
87 sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
88
89 if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
90 return FPBits::zero(sign: xbits.sign()).get_val();
91
92 // Since, cosm1_y = cos_y - 1, therefore:
93 // sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
94 return fputil::cast<float16>(x: fputil::multiply_add(
95 x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k)));
96}
97
98} // namespace math
99} // namespace LIBC_NAMESPACE_DECL
100
101#endif // LIBC_TYPES_HAS_FLOAT16
102
103#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
104