forked from rust-lang/compiler-builtins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloor.rs
More file actions
146 lines (127 loc) · 4 KB
/
floor.rs
File metadata and controls
146 lines (127 loc) · 4 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
/* SPDX-License-Identifier: MIT
* origin: musl src/math/floor.c */
//! Generic `floor` algorithm.
//!
//! Note that this uses the algorithm from musl's `floorf` rather than `floor` or `floorl` because
//! performance seems to be better (based on icount) and it does not seem to experience rounding
//! errors on i386.
use crate::support::{Float, FpResult, Int, IntTy, MinInt, Status};
#[inline]
pub fn floor<F: Float>(x: F) -> F {
floor_status(x).val
}
#[inline]
pub fn floor_status<F: Float>(x: F) -> FpResult<F> {
let zero = IntTy::<F>::ZERO;
let mut ix = x.to_bits();
let e = x.exp_unbiased();
// If the represented value has no fractional part, no truncation is needed.
if e >= F::SIG_BITS as i32 {
return FpResult::ok(x);
}
let res = if e >= 0 {
// |x| >= 1.0
let m = F::SIG_MASK >> e.unsigned();
if ix & m == zero {
// Portion to be masked is already zero; no adjustment needed.
return FpResult::ok(x);
}
if x.is_sign_negative() {
ix += m;
}
ix &= !m;
F::from_bits(ix)
} else {
// |x| < 1.0, zero or inexact with truncation
if (ix & !F::SIGN_MASK) == F::Int::ZERO {
return FpResult::ok(x);
}
if x.is_sign_positive() {
// 0.0 <= x < 1.0; rounding down goes toward +0.0.
F::ZERO
} else {
// -1.0 < x < 0.0; rounding down goes toward -1.0.
F::NEG_ONE
}
};
FpResult::new(res, Status::INEXACT)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::support::Hexf;
macro_rules! cases {
($f:ty) => {
[
// roundtrip
(0.0, 0.0, Status::OK),
(-0.0, -0.0, Status::OK),
(1.0, 1.0, Status::OK),
(-1.0, -1.0, Status::OK),
(<$f>::INFINITY, <$f>::INFINITY, Status::OK),
(<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
// with rounding
(0.1, 0.0, Status::INEXACT),
(-0.1, -1.0, Status::INEXACT),
(0.5, 0.0, Status::INEXACT),
(-0.5, -1.0, Status::INEXACT),
(0.9, 0.0, Status::INEXACT),
(-0.9, -1.0, Status::INEXACT),
(1.1, 1.0, Status::INEXACT),
(-1.1, -2.0, Status::INEXACT),
(1.5, 1.0, Status::INEXACT),
(-1.5, -2.0, Status::INEXACT),
(1.9, 1.0, Status::INEXACT),
(-1.9, -2.0, Status::INEXACT),
]
};
}
#[track_caller]
fn check<F: Float>(cases: &[(F, F, Status)]) {
for &(x, exp_res, exp_stat) in cases {
let FpResult { val, status } = floor_status(x);
assert_biteq!(val, exp_res, "{x:?} {}", Hexf(x));
assert_eq!(
status,
exp_stat,
"{x:?} {} -> {exp_res:?} {}",
Hexf(x),
Hexf(exp_res)
);
}
}
#[test]
#[cfg(f16_enabled)]
fn check_f16() {
check::<f16>(&cases!(f16));
check::<f16>(&[
(hf16!("0x1p10"), hf16!("0x1p10"), Status::OK),
(hf16!("-0x1p10"), hf16!("-0x1p10"), Status::OK),
]);
}
#[test]
fn check_f32() {
check::<f32>(&cases!(f32));
check::<f32>(&[
(hf32!("0x1p23"), hf32!("0x1p23"), Status::OK),
(hf32!("-0x1p23"), hf32!("-0x1p23"), Status::OK),
]);
}
#[test]
fn check_f64() {
check::<f64>(&cases!(f64));
check::<f64>(&[
(hf64!("0x1p52"), hf64!("0x1p52"), Status::OK),
(hf64!("-0x1p52"), hf64!("-0x1p52"), Status::OK),
]);
}
#[test]
#[cfg(f128_enabled)]
fn spec_tests_f128() {
check::<f128>(&cases!(f128));
check::<f128>(&[
(hf128!("0x1p112"), hf128!("0x1p112"), Status::OK),
(hf128!("-0x1p112"), hf128!("-0x1p112"), Status::OK),
]);
}
}