Skip to main content

c/
math_helpers.rs

1
2
3#[inline]
4const fn asuint(f: f32) -> u32 {
5    f.to_bits()
6}
7
8#[inline]
9const fn asfloat(i: u32) -> f32 {
10    f32::from_bits(i)
11}
12
13#[inline]
14const fn asuint64(f: f64) -> u64 {
15    f.to_bits()
16}
17
18#[inline]
19const fn asdouble(i: u64) -> f64 {
20    f64::from_bits(i)
21}
22
23#[inline]
24unsafe fn extract_words(hi: *mut u32, lo: *mut u32, d: f64) {
25    let u = asuint64(d);
26    *hi = (u >> 32) as u32;
27    *lo = u as u32;
28}
29
30#[inline]
31unsafe fn get_high_word(d: f64) -> u32 {
32    (asuint64(d) >> 32) as u32
33}
34
35#[inline]
36unsafe fn get_low_word(d: f64) -> u32 {
37    asuint64(d) as u32
38}
39
40#[inline]
41unsafe fn insert_words(d: *mut f64, hi: u32, lo: u32) {
42    *d = asdouble(((hi as u64) << 32) | (lo as u64));
43}
44
45#[inline]
46unsafe fn set_high_word(d: *mut f64, hi: u32) {
47    let lo = asuint64(*d) as u32;
48    *d = asdouble(((hi as u64) << 32) | (lo as u64));
49}
50
51#[inline]
52unsafe fn set_low_word(d: *mut f64, lo: u32) {
53    let hi = asuint64(*d) >> 32;
54    *d = asdouble((hi << 32) | (lo as u64));
55}
56
57#[inline]
58unsafe fn get_float_word(f: f32) -> u32 {
59    asuint(f)
60}
61
62#[inline]
63unsafe fn set_float_word(f: *mut f32, w: u32) {
64    *f = asfloat(w);
65}
66
67#[inline]
68fn eval_as_float(x: f32) -> f32 {
69    x
70}
71
72#[inline]
73fn eval_as_double(x: f64) -> f64 {
74    x
75}
76
77#[inline]
78fn fp_barrierf(x: f32) -> f32 {
79    unsafe { core::ptr::read_volatile(&x) }
80}
81
82#[inline]
83fn fp_barrier(x: f64) -> f64 {
84    unsafe { core::ptr::read_volatile(&x) }
85}
86
87#[inline]
88fn fp_force_evalf(x: f32) {
89    unsafe {
90        core::ptr::read_volatile(&x);
91    }
92}
93
94#[inline]
95fn fp_force_eval(x: f64) {
96    unsafe {
97        core::ptr::read_volatile(&x);
98    }
99}
100
101#[inline]
102fn fp_force_evall(x: f64) {
103    unsafe {
104        core::ptr::read_volatile(&x);
105    }
106}
107
108#[inline]
109fn force_eval<T: Copy>(x: T) {
110    unsafe {
111        core::ptr::read_volatile(&x);
112    }
113}
114
115#[inline]
116fn predict_true(x: bool) -> bool {
117    x
118}
119
120#[inline]
121fn predict_false(x: bool) -> bool {
122    x
123}
124
125
126#[inline]
127pub fn __math_invalid(x: f64) -> f64 {
128    (x - x) / (x - x)
129}
130
131#[inline]
132pub fn __math_invalidf(x: f32) -> f32 {
133    (x - x) / (x - x)
134}
135
136#[inline]
137pub fn __math_divzero(sign: u32) -> f64 {
138    let x = if sign != 0 { -1.0f64 } else { 1.0f64 };
139    x / 0.0
140}
141
142#[inline]
143pub fn __math_divzerof(sign: u32) -> f32 {
144    let x = if sign != 0 { -1.0f32 } else { 1.0f32 };
145    x / 0.0
146}
147
148#[inline]
149pub fn __math_xflow(sign: u32, y: f64) -> f64 {
150    // musl: (sign ? -y : y) * y  (preserves sign of infinity/zero)
151    if sign != 0 {
152        -y * y
153    } else {
154        y * y
155    }
156}
157
158#[inline]
159pub fn __math_xflowf(sign: u32, y: f32) -> f32 {
160    if sign != 0 {
161        -y * y
162    } else {
163        y * y
164    }
165}
166
167#[inline]
168pub fn __math_oflow(sign: u32) -> f64 {
169    __math_xflow(sign, 1e231f64)
170}
171
172#[inline]
173pub fn __math_oflowf(sign: u32) -> f32 {
174    __math_xflowf(sign, 1.7e38f32)
175}
176
177#[inline]
178pub fn __math_uflow(sign: u32) -> f64 {
179    __math_xflow(sign, 1e-231f64)
180}
181
182#[inline]
183pub fn __math_uflowf(sign: u32) -> f32 {
184    __math_xflowf(sign, 1.2e-38f32)
185}
186
187#[no_mangle]
188pub static mut signgam: c_int = 0;
189
190#[no_mangle]
191pub static mut __signgam: c_int = 0;
192
193pub const FP_NAN: c_int = 0;
194pub const FP_INFINITE: c_int = 1;
195pub const FP_ZERO: c_int = 2;
196pub const FP_SUBNORMAL: c_int = 3;
197pub const FP_NORMAL: c_int = 4;
198
199pub const FP_ILOGB0: c_int = -2147483648i32;
200pub const FP_ILOGBNAN: c_int = 2147483647i32;
201
202#[no_mangle]
203pub unsafe extern "C" fn __fpclassify(x: f64) -> c_int {
204    let u = asuint64(x);
205    let e = (u >> 52) & 0x7ff;
206    if e == 0 {
207        if (u << 1) != 0 { FP_SUBNORMAL } else { FP_ZERO }
208    } else if e == 0x7ff {
209        if (u << 12) != 0 { FP_NAN } else { FP_INFINITE }
210    } else {
211        FP_NORMAL
212    }
213}
214
215#[no_mangle]
216pub unsafe extern "C" fn __fpclassifyf(x: f32) -> c_int {
217    let u = asuint(x);
218    let e = (u >> 23) & 0xff;
219    if e == 0 {
220        if (u << 1) != 0 { FP_SUBNORMAL } else { FP_ZERO }
221    } else if e == 0xff {
222        if (u << 9) != 0 { FP_NAN } else { FP_INFINITE }
223    } else {
224        FP_NORMAL
225    }
226}
227
228#[inline]
229pub fn __signbit(x: f64) -> c_int {
230    ((asuint64(x) >> 63) & 1) as c_int
231}
232
233#[inline]
234pub fn __signbitf(x: f32) -> c_int {
235    ((asuint(x) >> 31) & 1) as c_int
236}
237
238#[inline]
239pub fn is_nan(x: f64) -> bool {
240    let u = asuint64(x);
241    ((u >> 52) & 0x7ff) == 0x7ff && (u << 12) != 0
242}
243
244#[inline]
245pub fn is_nanf(x: f32) -> bool {
246    let u = asuint(x);
247    ((u >> 23) & 0xff) == 0xff && (u << 9) != 0
248}
249
250#[inline]
251pub fn is_inf(x: f64) -> bool {
252    let u = asuint64(x);
253    ((u >> 52) & 0x7ff) == 0x7ff && (u << 12) == 0
254}
255
256#[inline]
257pub fn is_inff(x: f32) -> bool {
258    let u = asuint(x);
259    ((u >> 23) & 0xff) == 0xff && (u << 9) == 0
260}
261
262#[inline]
263pub fn is_finite(x: f64) -> bool {
264    let u = asuint64(x);
265    ((u >> 52) & 0x7ff) != 0x7ff
266}
267
268#[inline]
269pub fn is_finitef(x: f32) -> bool {
270    let u = asuint(x);
271    ((u >> 23) & 0xff) != 0xff
272}
273
274#[inline]
275pub fn toint_value() -> f64 {
276    4503599627370496.0
277}
278
279#[inline]
280pub fn tointf_value() -> f32 {
281    8388608.0f32
282}