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