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 = "aarch64")]
112#[no_mangle]
113pub extern "C" fn lrintl(x: f128) -> c_long {
114 lrint(f128_to_f64(x))
115}
116
117#[cfg(target_arch = "aarch64")]
118#[no_mangle]
119pub extern "C" fn llrintl(x: f128) -> c_longlong {
120 llrint(f128_to_f64(x))
121}
122
123#[cfg(any(target_arch = "riscv64", target_arch = "powerpc64", target_arch = "loongarch64"))]
124#[no_mangle]
125pub extern "C" fn lrintl(x: f128) -> c_long {
126 lrint(f128_to_f64(x))
127}
128
129#[cfg(any(target_arch = "riscv64", target_arch = "powerpc64", target_arch = "loongarch64"))]
130#[no_mangle]
131pub extern "C" fn llrintl(x: f128) -> c_longlong {
132 llrint(f128_to_f64(x))
133}