1#[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 if e >= F64_EXP_BIAS + 52 {
22 return x;
23 }
24
25 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#[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 if e >= F32_EXP_BIAS + 23 {
49 return x;
50 }
51
52 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
67const LRINT_TWO63: f64 = 9223372036854775808.0;
72
73#[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#[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(target_arch = "riscv64")]
137#[no_mangle]
138pub extern "C" fn lrintl(x: f128) -> c_long {
139 lrint(f128_to_f64(x))
140}
141
142#[cfg(target_arch = "riscv64")]
143#[no_mangle]
144pub extern "C" fn llrintl(x: f128) -> c_longlong {
145 llrint(f128_to_f64(x))
146}