Skip to main content

leanfran_runtime/
float.rs

1//! `Float` (`f64`) and `Float32` (`f32`). The transcendental functions keep the bare C names of
2//! `math.h` and are computed by the `libm` crate; `sqrt` uses the exact `f64::sqrt`.
3
4use crate::int::lean_int_to_int;
5use crate::int::lean_scalar_to_int;
6use crate::nat::lean_mpz_value;
7use crate::object::*;
8use crate::string::lean_mk_string_unchecked;
9use num_traits::Signed;
10
11/// `std::to_string` of a finite or infinite `double`: `%f` with `inf`/`-inf`.
12fn c_fixed(a: f64) -> String {
13    if a.is_infinite() {
14        if a > 0.0 {
15            "inf".to_string()
16        } else {
17            "-inf".to_string()
18        }
19    } else {
20        format!("{a:.6}")
21    }
22}
23
24pub unsafe fn lean_float_to_string(a: f64) -> Obj {
25    let s = if a.is_nan() {
26        "NaN".to_string()
27    } else {
28        c_fixed(a)
29    };
30    lean_mk_string_unchecked(&s, s.len(), s.len())
31}
32
33pub unsafe fn lean_float32_to_string(a: f32) -> Obj {
34    let s = if a.is_nan() {
35        "NaN".to_string()
36    } else {
37        c_fixed(a as f64)
38    };
39    lean_mk_string_unchecked(&s, s.len(), s.len())
40}
41
42macro_rules! float_common {
43    ($t:ty, $bits_t:ty, $qnan:expr,
44     $add:ident, $sub:ident, $mul:ident, $div:ident, $negate:ident,
45     $beq:ident, $declt:ident, $decle:ident, $isnan:ident, $isfinite:ident, $isinf:ident,
46     $of_bits:ident, $to_bits:ident, $scaleb:ident, $frexp:ident, $boxf:ident,
47     $to_u8:ident, $to_u16:ident, $to_u32:ident, $to_u64:ident, $to_usize:ident,
48     $to_i8:ident, $to_i16:ident, $to_i32:ident, $to_i64:ident, $to_isize:ident) => {
49        #[inline]
50        pub unsafe fn $add(a: $t, b: $t) -> $t {
51            a + b
52        }
53        #[inline]
54        pub unsafe fn $sub(a: $t, b: $t) -> $t {
55            a - b
56        }
57        #[inline]
58        pub unsafe fn $mul(a: $t, b: $t) -> $t {
59            a * b
60        }
61        #[inline]
62        pub unsafe fn $div(a: $t, b: $t) -> $t {
63            a / b
64        }
65        #[inline]
66        pub unsafe fn $negate(a: $t) -> $t {
67            -a
68        }
69        #[inline]
70        pub unsafe fn $beq(a: $t, b: $t) -> u8 {
71            (a == b) as u8
72        }
73        #[inline]
74        pub unsafe fn $declt(a: $t, b: $t) -> u8 {
75            (a < b) as u8
76        }
77        #[inline]
78        pub unsafe fn $decle(a: $t, b: $t) -> u8 {
79            (a <= b) as u8
80        }
81        #[inline]
82        pub unsafe fn $isnan(a: $t) -> u8 {
83            a.is_nan() as u8
84        }
85        #[inline]
86        pub unsafe fn $isfinite(a: $t) -> u8 {
87            a.is_finite() as u8
88        }
89        #[inline]
90        pub unsafe fn $isinf(a: $t) -> u8 {
91            a.is_infinite() as u8
92        }
93        /// Bit pattern to float; NaNs become the quiet NaN.
94        #[inline]
95        pub unsafe fn $of_bits(u: $bits_t) -> $t {
96            let r = <$t>::from_bits(u);
97            if r.is_nan() {
98                <$t>::NAN
99            } else {
100                r
101            }
102        }
103        /// Float to bit pattern; NaNs give the quiet NaN pattern.
104        #[inline]
105        pub unsafe fn $to_bits(a: $t) -> $bits_t {
106            if a.is_nan() {
107                $qnan
108            } else {
109                a.to_bits()
110            }
111        }
112        /// `a * 2^b` for an `Int` `b` (borrowed).
113        pub unsafe fn $scaleb(a: $t, b: Obj) -> $t {
114            if lean_is_scalar(b) {
115                scalbn(a as f64, lean_scalar_to_int(b)) as $t
116            } else if a == 0.0 || lean_mpz_value(b).is_negative() {
117                0.0
118            } else {
119                a * <$t>::INFINITY
120            }
121        }
122        /// `(mantissa, exponent)` with the exponent an `Int` (0 for non-finite values).
123        pub unsafe fn $frexp(a: $t) -> Obj {
124            let r = lean_alloc_ctor(0, 2, 0);
125            let (m, e) = frexp(a as f64);
126            lean_ctor_set(r, 0, $boxf(m as $t));
127            lean_ctor_set(
128                r,
129                1,
130                if a.is_finite() {
131                    lean_int_to_int(e)
132                } else {
133                    lean_box(0)
134                },
135            );
136            r
137        }
138        #[inline]
139        pub unsafe fn $to_u8(a: $t) -> u8 {
140            a as u8
141        }
142        #[inline]
143        pub unsafe fn $to_u16(a: $t) -> u16 {
144            a as u16
145        }
146        #[inline]
147        pub unsafe fn $to_u32(a: $t) -> u32 {
148            a as u32
149        }
150        #[inline]
151        pub unsafe fn $to_u64(a: $t) -> u64 {
152            a as u64
153        }
154        #[inline]
155        pub unsafe fn $to_usize(a: $t) -> usize {
156            a as usize
157        }
158        #[inline]
159        pub unsafe fn $to_i8(a: $t) -> u8 {
160            a as i8 as u8
161        }
162        #[inline]
163        pub unsafe fn $to_i16(a: $t) -> u16 {
164            a as i16 as u16
165        }
166        #[inline]
167        pub unsafe fn $to_i32(a: $t) -> u32 {
168            a as i32 as u32
169        }
170        #[inline]
171        pub unsafe fn $to_i64(a: $t) -> u64 {
172            a as i64 as u64
173        }
174        #[inline]
175        pub unsafe fn $to_isize(a: $t) -> usize {
176            a as isize as usize
177        }
178    };
179}
180
181float_common!(
182    f64,
183    u64,
184    0x7ff8000000000000u64,
185    lean_float_add,
186    lean_float_sub,
187    lean_float_mul,
188    lean_float_div,
189    lean_float_negate,
190    lean_float_beq,
191    lean_float_decLt,
192    lean_float_decLe,
193    lean_float_isnan,
194    lean_float_isfinite,
195    lean_float_isinf,
196    lean_float_of_bits,
197    lean_float_to_bits,
198    lean_float_scaleb,
199    lean_float_frexp,
200    lean_box_float,
201    lean_float_to_uint8,
202    lean_float_to_uint16,
203    lean_float_to_uint32,
204    lean_float_to_uint64,
205    lean_float_to_usize,
206    lean_float_to_int8,
207    lean_float_to_int16,
208    lean_float_to_int32,
209    lean_float_to_int64,
210    lean_float_to_isize
211);
212float_common!(
213    f32,
214    u32,
215    0x7fc00000u32,
216    lean_float32_add,
217    lean_float32_sub,
218    lean_float32_mul,
219    lean_float32_div,
220    lean_float32_negate,
221    lean_float32_beq,
222    lean_float32_decLt,
223    lean_float32_decLe,
224    lean_float32_isnan,
225    lean_float32_isfinite,
226    lean_float32_isinf,
227    lean_float32_of_bits,
228    lean_float32_to_bits,
229    lean_float32_scaleb,
230    lean_float32_frexp,
231    lean_box_float32,
232    lean_float32_to_uint8,
233    lean_float32_to_uint16,
234    lean_float32_to_uint32,
235    lean_float32_to_uint64,
236    lean_float32_to_usize,
237    lean_float32_to_int8,
238    lean_float32_to_int16,
239    lean_float32_to_int32,
240    lean_float32_to_int64,
241    lean_float32_to_isize
242);
243
244#[inline]
245pub unsafe fn lean_float_to_float32(a: f64) -> f32 {
246    a as f32
247}
248
249#[inline]
250pub unsafe fn lean_float32_to_float(a: f32) -> f64 {
251    a as f64
252}
253
254fn scalbn(a: f64, n: i32) -> f64 {
255    libm::scalbn(a, n)
256}
257
258fn frexp(a: f64) -> (f64, i32) {
259    libm::frexp(a)
260}
261
262/* `math.h` */
263
264macro_rules! libm1 {
265    ($($name:ident: $t:ty),* $(,)?) => {
266        $(
267            #[inline]
268            pub unsafe fn $name(a: $t) -> $t {
269                libm::$name(a)
270            }
271        )*
272    };
273}
274
275macro_rules! libm2 {
276    ($($name:ident: $t:ty),* $(,)?) => {
277        $(
278            #[inline]
279            pub unsafe fn $name(a: $t, b: $t) -> $t {
280                libm::$name(a, b)
281            }
282        )*
283    };
284}
285
286libm1!(
287    sin: f64, cos: f64, tan: f64, asin: f64, acos: f64, atan: f64, sinh: f64, cosh: f64, tanh: f64,
288    asinh: f64, acosh: f64, atanh: f64, exp: f64, exp2: f64, log: f64, log2: f64, log10: f64,
289    cbrt: f64, ceil: f64, floor: f64, round: f64, fabs: f64,
290    sinf: f32, cosf: f32, tanf: f32, asinf: f32, acosf: f32, atanf: f32, sinhf: f32, coshf: f32,
291    tanhf: f32, asinhf: f32, acoshf: f32, atanhf: f32, expf: f32, exp2f: f32, logf: f32, log2f: f32,
292    log10f: f32, cbrtf: f32, ceilf: f32, floorf: f32, roundf: f32, fabsf: f32,
293);
294
295libm2!(atan2: f64, pow: f64, atan2f: f32, powf: f32);
296
297#[inline]
298pub unsafe fn sqrt(a: f64) -> f64 {
299    a.sqrt()
300}
301
302#[inline]
303pub unsafe fn sqrtf(a: f32) -> f32 {
304    a.sqrt()
305}
306
307#[inline]
308pub unsafe fn fma(a: f64, b: f64, c: f64) -> f64 {
309    a.mul_add(b, c)
310}
311
312#[inline]
313pub unsafe fn fmaf(a: f32, b: f32, c: f32) -> f32 {
314    a.mul_add(b, c)
315}