Skip to main content

c/
math_lrint.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// Port of musl lrint/llrint/lrintf/llrintf/lrintl/llrintl
5//
6// On x86_64: c_long = i64 (LONG_MAX >= 2^53), c_longlong = i64, long double = f64.
7// All six functions reduce to: round-to-integer-using-current-FP-mode, then cast.
8//
9// The rint/rintf helpers use musl's `x + toint - toint` trick which rounds via
10// the current FP rounding mode (MXCSR on x86_64), unlike Rust's `as` which
11// truncates toward zero.
12
13// musl rint: rounds f64 to integer using current FP rounding mode
14#[no_mangle]
15pub extern "C" fn rint(x: f64) -> f64 {
16    let u = asuint64(x);
17    let e = ((u >> 52) & F64_EXP_MASK) as i32;
18    let s = u >> 63;
19
20    // Already an integer (exponent >= 52 means no fractional bits)
21    if e >= F64_EXP_BIAS + 52 {
22        return x;
23    }
24
25    // toint = 1/DBL_EPSILON = 2^52 = 4503599627370496.0
26    let toint = 4503599627370496.0f64;
27    let y = if s != 0 {
28        x - toint + toint
29    } else {
30        x + toint - toint
31    };
32
33    if y == 0.0 {
34        if s != 0 { -0.0 } else { 0.0 }
35    } else {
36        y
37    }
38}
39
40// musl rintf: rounds f32 to integer using current FP rounding mode
41#[no_mangle]
42pub extern "C" fn rintf(x: f32) -> f32 {
43    let u = asuint(x);
44    let e = ((u >> 23) & F32_EXP_MASK as u32) as i32;
45    let s = u >> 31;
46
47    // Already an integer (exponent >= 23 means no fractional bits)
48    if e >= F32_EXP_BIAS + 23 {
49        return x;
50    }
51
52    // toint = 1/FLT_EPSILON = 2^23 = 8388608.0
53    let toint = 8388608.0f32;
54    let y = if s != 0 {
55        x - toint + toint
56    } else {
57        x + toint - toint
58    };
59
60    if y == 0.0 {
61        if s != 0 { -0.0f32 } else { 0.0f32 }
62    } else {
63        y
64    }
65}
66
67// rint returns an exact integer, so the only extra work is C's FE_INVALID
68// contract: values outside [-2^63, 2^63) must raise invalid instead of being
69// silently saturated by Rust's `as` cast.
70
71const LRINT_TWO63: f64 = 9223372036854775808.0;
72
73/// Round with the current FP mode and convert to i64.  Rust's saturating cast
74/// does not raise FE_INVALID, so check the representable range explicitly.
75#[inline]
76unsafe fn lrint_i64(x: f64) -> i64 {
77    let r = rint(x);
78    if r.is_nan() || r >= LRINT_TWO63 || r < -LRINT_TWO63 {
79        feraiseexcept(FE_INVALID);
80        return i64::MIN;
81    }
82    r as i64
83}
84
85#[inline]
86unsafe fn lrintf_i64(x: f32) -> i64 {
87    lrint_i64(rintf(x) as f64)
88}
89
90#[no_mangle]
91pub extern "C" fn lrint(x: f64) -> c_long {
92    unsafe { lrint_i64(x) }
93}
94
95#[no_mangle]
96pub extern "C" fn llrint(x: f64) -> c_longlong {
97    unsafe { lrint_i64(x) }
98}
99
100#[no_mangle]
101pub extern "C" fn lrintf(x: f32) -> c_long {
102    unsafe { lrintf_i64(x) }
103}
104
105#[no_mangle]
106pub extern "C" fn llrintf(x: f32) -> c_longlong {
107    unsafe { lrintf_i64(x) }
108}
109
110// On x86_64 with -mlong-double-64: long double = f64, delegate to lrint/llrint.
111// On aarch64: long double = f128 (IEEE quad), cast to f64 then round.
112#[cfg(target_arch = "x86_64")]
113#[no_mangle]
114pub extern "C" fn lrintl(x: f64) -> c_long {
115    lrint(x)
116}
117
118#[cfg(target_arch = "x86_64")]
119#[no_mangle]
120pub extern "C" fn llrintl(x: f64) -> c_longlong {
121    llrint(x)
122}
123
124#[cfg(target_arch = "aarch64")]
125#[no_mangle]
126pub extern "C" fn lrintl(x: f128) -> c_long {
127    lrint(f128_to_f64(x))
128}
129
130#[cfg(target_arch = "aarch64")]
131#[no_mangle]
132pub extern "C" fn llrintl(x: f128) -> c_longlong {
133    llrint(f128_to_f64(x))
134}
135
136#[cfg(any(target_arch = "riscv64", target_arch = "powerpc64", target_arch = "loongarch64"))]
137#[no_mangle]
138pub extern "C" fn lrintl(x: f128) -> c_long {
139    lrint(f128_to_f64(x))
140}
141
142#[cfg(any(target_arch = "riscv64", target_arch = "powerpc64", target_arch = "loongarch64"))]
143#[no_mangle]
144pub extern "C" fn llrintl(x: f128) -> c_longlong {
145    llrint(f128_to_f64(x))
146}