Skip to main content

c/
math_helpers.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// Shared bit, classification, fenv and gamma-sign helpers used by the local
5// math ports and the libm-backed wrappers.
6
7// IEEE-754 layout constants shared by the math wrappers.
8const F64_EXP_MASK: u64 = 0x7ff;
9const F32_EXP_MASK: u64 = 0xff;
10const F64_EXP_BIAS: i32 = 0x3ff;
11const F32_EXP_BIAS: i32 = 0x7f;
12
13#[inline]
14const fn asuint(f: f32) -> u32 {
15    f.to_bits()
16}
17
18#[inline]
19const fn asfloat(i: u32) -> f32 {
20    f32::from_bits(i)
21}
22
23#[inline]
24const fn asuint64(f: f64) -> u64 {
25    f.to_bits()
26}
27
28#[inline]
29const fn asdouble(i: u64) -> f64 {
30    f64::from_bits(i)
31}
32
33#[inline]
34unsafe fn extract_words(hi: *mut u32, lo: *mut u32, d: f64) {
35    let u = asuint64(d);
36    *hi = (u >> 32) as u32;
37    *lo = u as u32;
38}
39
40#[inline]
41fn get_high_word(d: f64) -> u32 {
42    (asuint64(d) >> 32) as u32
43}
44
45#[inline]
46fn get_low_word(d: f64) -> u32 {
47    asuint64(d) as u32
48}
49
50#[inline]
51unsafe fn set_low_word(d: *mut f64, lo: u32) {
52    let hi = asuint64(*d) >> 32;
53    *d = asdouble((hi << 32) | (lo as u64));
54}
55
56#[inline]
57unsafe fn get_float_word(f: f32) -> u32 {
58    asuint(f)
59}
60
61#[inline]
62fn eval_as_float(x: f32) -> f32 {
63    x
64}
65
66#[inline]
67fn eval_as_double(x: f64) -> f64 {
68    x
69}
70
71#[inline]
72fn fp_barrierf(x: f32) -> f32 {
73    unsafe { core::ptr::read_volatile(&x) }
74}
75
76#[inline]
77fn fp_barrier(x: f64) -> f64 {
78    unsafe { core::ptr::read_volatile(&x) }
79}
80
81#[inline]
82fn fp_force_evalf(x: f32) {
83    unsafe {
84        core::ptr::read_volatile(&x);
85    }
86}
87
88#[inline]
89fn fp_force_eval(x: f64) {
90    unsafe {
91        core::ptr::read_volatile(&x);
92    }
93}
94
95#[inline]
96fn force_eval<T: Copy>(x: T) {
97    unsafe {
98        core::ptr::read_volatile(&x);
99    }
100}
101
102#[inline]
103fn predict_false(x: bool) -> bool {
104    x
105}
106
107
108const SIN_PI_PI: f64 = asdouble(0x400921FB54442D18);
109
110/// sin(pi*x) for x > 2^-100.  If the result is zero its sign is unspecified,
111/// matching musl's lgamma/tgamma helper.
112#[inline]
113fn sin_pi(x: f64) -> f64 {
114    // Reduce x modulo 2; a spurious inexact is raised for odd integers.
115    let x = 2.0 * (x * 0.5 - floor(x * 0.5));
116    let mut n = (x * 4.0) as i32;
117    n = (n + 1) / 2;
118    let x = x - n as f64 * 0.5;
119    let x = x * SIN_PI_PI;
120    match n {
121        1 => __cos(x, 0.0),
122        2 => __sin(-x, 0.0, 0),
123        3 => -__cos(x, 0.0),
124        _ => __sin(x, 0.0, 0),
125    }
126}
127
128#[inline]
129pub fn __math_invalid(x: f64) -> f64 {
130    if x.is_nan() { return x; }
131    unsafe { feraiseexcept(FE_INVALID); }
132    (x - x) / (x - x)
133}
134
135#[inline]
136pub fn __math_invalidf(x: f32) -> f32 {
137    if x.is_nan() { return x; }
138    unsafe { feraiseexcept(FE_INVALID); }
139    (x - x) / (x - x)
140}
141
142#[inline]
143pub fn __math_divzero(sign: u32) -> f64 {
144    unsafe { feraiseexcept(FE_DIVBYZERO); }
145    let x = if sign != 0 { -1.0f64 } else { 1.0f64 };
146    x / 0.0
147}
148
149#[inline]
150pub fn __math_divzerof(sign: u32) -> f32 {
151    unsafe { feraiseexcept(FE_DIVBYZERO); }
152    let x = if sign != 0 { -1.0f32 } else { 1.0f32 };
153    x / 0.0
154}
155
156#[inline]
157pub fn __math_xflow(sign: u32, y: f64) -> f64 {
158    // musl: (sign ? -y : y) * y  (preserves sign of infinity/zero)
159    if sign != 0 {
160        -y * y
161    } else {
162        y * y
163    }
164}
165
166#[inline]
167pub fn __math_xflowf(sign: u32, y: f32) -> f32 {
168    if sign != 0 {
169        -y * y
170    } else {
171        y * y
172    }
173}
174
175#[inline]
176pub fn __math_oflow(sign: u32) -> f64 {
177    unsafe { feraiseexcept(FE_OVERFLOW | FE_INEXACT); }
178    __math_xflow(sign, 1e231f64)
179}
180
181#[inline]
182pub fn __math_oflowf(sign: u32) -> f32 {
183    unsafe { feraiseexcept(FE_OVERFLOW | FE_INEXACT); }
184    __math_xflowf(sign, 1.7e38f32)
185}
186
187#[inline]
188pub fn __math_uflow(sign: u32) -> f64 {
189    unsafe { feraiseexcept(FE_UNDERFLOW | FE_INEXACT); }
190    __math_xflow(sign, 1e-231f64)
191}
192
193#[inline]
194pub fn __math_uflowf(sign: u32) -> f32 {
195    unsafe { feraiseexcept(FE_UNDERFLOW | FE_INEXACT); }
196    __math_xflowf(sign, 1.2e-38f32)
197}
198
199// musl exports signgam as an alias of __signgam. rustc's cdylib export list
200// localizes linker-level aliases, so keep two real statics and update both
201// together wherever the gamma sign is produced.
202#[no_mangle]
203pub static mut signgam: c_int = 0;
204
205#[no_mangle]
206pub static mut __signgam: c_int = 0;
207
208#[inline]
209pub(crate) unsafe fn set_signgam(v: c_int) {
210    signgam = v;
211    __signgam = v;
212}
213
214pub const FP_NAN: c_int = 0;
215pub const FP_INFINITE: c_int = 1;
216pub const FP_ZERO: c_int = 2;
217pub const FP_SUBNORMAL: c_int = 3;
218pub const FP_NORMAL: c_int = 4;
219
220pub const FP_ILOGB0: c_int = c_int::MIN;
221pub const FP_ILOGBNAN: c_int = c_int::MAX;
222
223#[no_mangle]
224pub unsafe extern "C" fn __fpclassify(x: f64) -> c_int {
225    let u = asuint64(x);
226    let e = (u >> 52) & F64_EXP_MASK;
227    if e == 0 {
228        if (u << 1) != 0 { FP_SUBNORMAL } else { FP_ZERO }
229    } else if e == F64_EXP_MASK {
230        if (u << 12) != 0 { FP_NAN } else { FP_INFINITE }
231    } else {
232        FP_NORMAL
233    }
234}
235
236#[no_mangle]
237pub unsafe extern "C" fn __fpclassifyf(x: f32) -> c_int {
238    let u = asuint(x);
239    let e = (u >> 23) & F32_EXP_MASK as u32;
240    if e == 0 {
241        if (u << 1) != 0 { FP_SUBNORMAL } else { FP_ZERO }
242    } else if e == F32_EXP_MASK as u32 {
243        if (u << 9) != 0 { FP_NAN } else { FP_INFINITE }
244    } else {
245        FP_NORMAL
246    }
247}
248
249#[no_mangle]
250pub extern "C" fn __signbit(x: f64) -> c_int {
251    ((asuint64(x) >> 63) & 1) as c_int
252}
253
254#[no_mangle]
255pub extern "C" fn __signbitf(x: f32) -> c_int {
256    ((asuint(x) >> 31) & 1) as c_int
257}
258
259#[inline]
260pub fn is_nan(x: f64) -> bool {
261    let u = asuint64(x);
262    ((u >> 52) & F64_EXP_MASK) == F64_EXP_MASK && (u << 12) != 0
263}
264
265#[inline]
266pub fn is_nanf(x: f32) -> bool {
267    let u = asuint(x);
268    ((u >> 23) & F32_EXP_MASK as u32) == F32_EXP_MASK as u32 && (u << 9) != 0
269}
270
271#[inline]
272pub fn is_finite(x: f64) -> bool {
273    let u = asuint64(x);
274    ((u >> 52) & F64_EXP_MASK) != F64_EXP_MASK
275}