1//===-- Implementation header for lgammaf16 ---------------------*- 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_LGAMMAF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMAF16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "hdr/errno_macros.h"
17#include "hdr/fenv_macros.h"
18#include "hdr/stdint_proxy.h"
19#include "src/__support/FPUtil/FEnvImpl.h"
20#include "src/__support/FPUtil/FPBits.h"
21#include "src/__support/FPUtil/NearestIntegerOperations.h"
22#include "src/__support/FPUtil/cast.h"
23#include "src/__support/FPUtil/except_value_utils.h"
24#include "src/__support/FPUtil/is_integer.h"
25#include "src/__support/FPUtil/multiply_add.h"
26#include "src/__support/FPUtil/polyeval.h"
27#include "src/__support/macros/attributes.h"
28#include "src/__support/macros/config.h"
29#include "src/__support/macros/optimization.h"
30#include "src/__support/macros/properties/types.h"
31#include "src/__support/math/gamma_util.h"
32
33namespace LIBC_NAMESPACE_DECL {
34
35namespace math {
36
37LIBC_INLINE float16 lgammaf16(float16 x) {
38 using namespace gamma_internal;
39 using FPBits = fputil::FPBits<float16>;
40
41 FPBits xbits(x);
42 uint16_t x_abs = xbits.abs().uintval();
43
44 // Handle NaN and Inf
45 if (LIBC_UNLIKELY(x_abs >= 0x7c00u)) {
46 if (x_abs == 0x7c00u)
47 return FPBits::inf().get_val();
48 if (xbits.is_signaling_nan()) {
49 fputil::raise_except_if_required(FE_INVALID);
50 return FPBits::quiet_nan().get_val();
51 }
52 return x;
53 }
54
55 // +-0 pole error
56 if (LIBC_UNLIKELY(x_abs == 0)) {
57 fputil::raise_except_if_required(FE_DIVBYZERO);
58 fputil::set_errno_if_required(ERANGE);
59 return FPBits::inf().get_val();
60 }
61
62 if (LIBC_UNLIKELY(fputil::is_integer(x))) {
63 if (xbits.is_neg()) {
64 // pole -> +Inf
65 fputil::raise_except_if_required(FE_DIVBYZERO);
66 fputil::set_errno_if_required(ERANGE);
67 return FPBits::inf().get_val();
68 }
69 // lgamma(1) = lgamma(2) = 0
70 if (x_abs == 0x3c00u || x_abs == 0x4000u)
71 return FPBits::zero().get_val();
72 }
73
74 double xd = fputil::cast<double>(x);
75 double abs_xd = xd < 0.0 ? -xd : xd;
76 double lgamma_val;
77
78 if (LIBC_UNLIKELY(x_abs < 0x3948u)) {
79 // Small path: |x| < 0.66015625
80 // For t = |x|: lgamma(t) = t*P(u) - log(t), where P(u) approximates
81 // h(t) = (lgamma(t) + log(t)) / t, u = (t - MID_S) / HW_S in [-1, 1]
82 // For x < 0: lgamma(-t) = log(pi) - log(sin(pi*t)) - lgamma(t) - log(t)
83 // = log(pi) - lg_ln(lg_sinpi(t)) - t*P(u)
84 // Degree-12 Chebyshev approximation for h in u; max lgamma error ~1.5e-13
85 // Coefficients generated by DCT at 13 Chebyshev nodes on
86 // [1e-5, 0.66015625].
87 // Evaluated via Clenshaw: sum_{k=0}^{12} CHEB_H[k] * T_k(u)
88 constexpr double MID_S = 0x1.52014f8b588e3p-2;
89 constexpr double HW_S = 0x1.51feb074a771dp-2;
90 constexpr double CHEB_H[13] = {
91 -0x1.6ab0351d4e02fp-2, 0x1.ac55f136e49cep-3, -0x1.9fc5280cd41bfp-7,
92 0x1.1825d37c81c1ep-10, -0x1.ae2f107c31c9ep-14, 0x1.6174bdf95e9abp-17,
93 -0x1.2e6abd22bc4ecp-20, 0x1.09b66d4f14b06p-23, -0x1.dbb938f189d8ap-27,
94 0x1.afcb748e20e4ep-30, -0x1.8c221a9d89d8ap-33, 0x1.6e708e49b3aeep-36,
95 -0x1.50accec4ec4ecp-39};
96 // Clenshaw's algorithm: p = sum_k CHEB_H[k] * T_k(u_s)
97 double u_s = (abs_xd - MID_S) / HW_S, u_s2 = 2.0 * u_s;
98 double bh1 = CHEB_H[12], bh2 = 0.0;
99 for (int k = 11; k >= 1; k--) {
100 double bh0 = fputil::multiply_add(x: u_s2, y: bh1, z: CHEB_H[k] - bh2);
101 bh2 = bh1;
102 bh1 = bh0;
103 }
104 double poly_h = fputil::multiply_add(x: u_s, y: bh1, z: CHEB_H[0] - bh2);
105 // abs_xd * poly_h = lgamma(abs_xd) + log(abs_xd)
106 double poly_val = abs_xd * poly_h;
107 if (xbits.is_neg())
108 lgamma_val = 0x1.250d048e7a1bdp+0 - lg_ln(x: lg_sinpi(x: abs_xd)) - poly_val;
109 else
110 lgamma_val = poly_val - lg_ln(x: abs_xd);
111 } else if (x_abs > 0x42BEu) { // 3.3710938
112 // For x >= 8184.0f16, lgamma(x) >= 65549.73 > 65520, overflowing float16
113 // in all rounding modes.
114 if (LIBC_UNLIKELY(!xbits.is_neg() && x_abs >= 0x6ffeu))
115 return fputil::set_overflow<float16>();
116
117 double log_abs_xd = lg_ln(x: abs_xd);
118 // lgamma(x) = (x-0.5)*log(x) - x + log(2*pi)/2 + Bernoulli corrections
119 // Stirling base: (x-0.5)*(log(x)-1) + (log(2*pi)-1)/2
120 lgamma_val = (abs_xd - 0.5) * (log_abs_xd - 1.0) + 0x1.acfe390c97d69p-2;
121
122 double inv_x = 1.0 / abs_xd, inv_x2 = inv_x * inv_x;
123
124 if (x_abs > 0x64AEu) {
125 // |x| > 1198 1-term Stirling-Bernoulli: 1/(12*x)
126 lgamma_val += inv_x * 0x1.5555555555555p-4;
127 } else if (x_abs > 0x549Eu) {
128 // |x| in (73.875, 1198] 2-term correction
129 // Minimax polynomial in 1/x^2 for R(x) = lgamma(x) - stirling_base(x),
130 // where stirling_base(x) = (x-0.5)*log(x) - x + log(2*pi)/2
131 // Exact Bernoulli: B_2/(1*2) = 1/12, B_4/(3*4) = -1/360
132 // Generated by Sollya with:
133 // > stir = proc(x) { return lgamma(x)-((x-0.5)*log(x)-x+log(2*pi)/2);
134 // }; > P = fpminimax(stir(x), [|1,3|], [|D...|], [73.875, 1198]);
135 constexpr double BERN2[2] = {0x1.555555547fbadp-4, -0x1.6c0fd270c465p-9};
136 lgamma_val += inv_x * fputil::polyeval(x: inv_x2, a0: BERN2[0], a: BERN2[1]);
137 } else if (x_abs > 0x4955u) {
138 // |x| in (10.664, 73.875] 4-term correction
139 // Minimax polynomial in 1/x^2 for R(x); exact Bernoulli terms are
140 // 1/12, -1/360, 1/1260, -1/1680
141 // Generated by Sollya with:
142 // > display = hexadecimal;
143 // > stir = proc(x) { return lgamma(x)-((x-0.5)*log(x)-x+log(2*pi)/2);
144 // }; > P = fpminimax(stir(x), [|1,3,5,7|], [|D...|], [10.664, 73.875]);
145 constexpr double BERN4[4] = {0x1.555555554de0bp-4, -0x1.6c16bdc45944fp-9,
146 0x1.a0077f300ecb3p-11,
147 -0x1.2e9cfff3b29c2p-11};
148 double inv_x4 = inv_x2 * inv_x2;
149 lgamma_val +=
150 inv_x * fputil::multiply_add(
151 x: inv_x4, y: fputil::multiply_add(x: inv_x2, y: BERN4[3], z: BERN4[2]),
152 z: fputil::multiply_add(x: inv_x2, y: BERN4[1], z: BERN4[0]));
153 } else {
154 // |x| in (3.373, 10.664]: 8-term correction.
155 // Degree-7 minimax polynomial in 1/x^2 for R(x), generated by Sollya
156 // with:
157 // > stir = proc(x) { return lgamma(x)-((x-0.5)*log(x)-x+log(2*pi)/2);
158 // }; > P = fpminimax(stir(x), [|1,3,5,7,9,11,13,15|], [|D...|],
159 // [3.373, 10.664]);
160 constexpr double BERN8[8] = {
161 0x1.5555555551286p-4, -0x1.6c16c0e7c4cf4p-9, 0x1.a0193267fe6f2p-11,
162 -0x1.37e87ec19cb45p-11, 0x1.b40011dfff081p-11, -0x1.c16c8946b19b6p-10,
163 0x1.e9f47ace150d8p-9, -0x1.4f5843a71a338p-8};
164 double inv_x4 = inv_x2 * inv_x2, inv_x8 = inv_x4 * inv_x4;
165 double p01 = fputil::multiply_add(x: inv_x2, y: BERN8[1], z: BERN8[0]);
166 double p23 = fputil::multiply_add(x: inv_x2, y: BERN8[3], z: BERN8[2]);
167 double p45 = fputil::multiply_add(x: inv_x2, y: BERN8[5], z: BERN8[4]);
168 double p67 = fputil::multiply_add(x: inv_x2, y: BERN8[7], z: BERN8[6]);
169 double p03 = fputil::multiply_add(x: inv_x4, y: p23, z: p01);
170 double p47 = fputil::multiply_add(x: inv_x4, y: p67, z: p45);
171 lgamma_val += inv_x * fputil::multiply_add(x: inv_x8, y: p47, z: p03);
172 }
173
174 if (xbits.is_neg()) {
175 // Reflection formula
176 // lgamma(x) = log(pi) - log|x| - log|sin(pi*frac_x)| - lgamma|x|
177 double frac_x = xd - fputil::floor(x: xd);
178 lgamma_val = 0x1.250d048e7a1bdp+0 - lgamma_val - log_abs_xd;
179 lgamma_val -= lg_ln(x: lg_sinpi(x: frac_x));
180 }
181 } else {
182 // Medium path: |x| in [0.66015625, 3.373046875]
183 // lgamma(|x|) = (|x|-1)*(|x|-2) * P(u), where P(u) approximates
184 // g(t) = lgamma(t) / ((t-1)*(t-2)), u = (t - MID_M) / HW_M in [-1, 1]
185 // Degree-20 Chebyshev approximation for g in u; max lgamma error ~1.4e-10
186 // Coefficients generated by DCT at 21 Chebyshev nodes on
187 // [0.66015625, 3.373046875]. Evaluated via Clenshaw: sum_{k=0}^{20}
188 // CHEB_G[k] * T_k(u)
189 constexpr double MID_M = 0x1.0220000000000p+1;
190 constexpr double HW_M = 0x1.5b40000000000p+0;
191 constexpr double CHEB_G[21] = {
192 0x1.d690d4de8b355p-2, -0x1.53439726f7d8bp-3, 0x1.568fc6653c026p-5,
193 -0x1.8d074f73f36efp-7, 0x1.eefebf4ca8e86p-9, -0x1.4275913d85ff5p-10,
194 0x1.b075f0d96e492p-12, -0x1.27ed35f357339p-13, 0x1.9b12cbe71a800p-15,
195 -0x1.20c949c2829b5p-16, 0x1.996eb95241555p-18, -0x1.2460d6c60f800p-19,
196 0x1.a42297aae1862p-21, -0x1.2f6c7d28230a5p-22, 0x1.b8394aac30c31p-24,
197 -0x1.409726ce1110ep-25, 0x1.d484ac42db6dbp-27, -0x1.574dae55861abp-28,
198 0x1.f782605555555p-30, -0x1.6cefb8f65aa2ep-31, 0x1.d9f268f3cf3cfp-33};
199 // Clenshaw's algorithm: poly_g = sum_k CHEB_G[k] * T_k(u_m)
200 double u_m = (abs_xd - MID_M) / HW_M, u_m2 = 2.0 * u_m;
201 double bg1 = CHEB_G[20], bg2 = 0.0;
202 for (int k = 19; k >= 1; k--) {
203 double bg0 = fputil::multiply_add(x: u_m2, y: bg1, z: CHEB_G[k] - bg2);
204 bg2 = bg1;
205 bg1 = bg0;
206 }
207 double poly_g = fputil::multiply_add(x: u_m, y: bg1, z: CHEB_G[0] - bg2);
208 lgamma_val = (abs_xd - 1.0) * (abs_xd - 2.0) * poly_g;
209
210 if (xbits.is_neg()) {
211 // Reflection
212 // (x) = log(pi) - lgamma|x| - log(|x|*|sin(pi*frac_x)|)
213 double frac_x = xd - fputil::floor(x: xd);
214 lgamma_val = 0x1.250d048e7a1bdp+0 - lgamma_val;
215 lgamma_val -= lg_ln(x: lg_sinpi(x: frac_x) * abs_xd);
216 }
217 }
218
219 float16 result = fputil::cast<float16>(x: lgamma_val);
220 if (LIBC_UNLIKELY(FPBits(result).is_inf())) {
221 fputil::raise_overflow_except_if_required<float16>();
222 fputil::set_errno_if_required(ERANGE);
223 }
224 return result;
225}
226
227} // namespace math
228
229} // namespace LIBC_NAMESPACE_DECL
230
231#endif // LIBC_TYPES_HAS_FLOAT16
232
233#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMAF16_H
234