Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit d22cc6a

Browse files
committed
Cleanup
1 parent c680ff9 commit d22cc6a

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

crates/libm-test/src/gen/domain.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ impl<F> FloatExt for F where F: Float {}
162162
pub trait FloatExt: Float {
163163
const TINY_BITS: Self::Int = Self::Int::ONE;
164164

165+
/// Increment by one ULP, saturating at infinity.
165166
fn next_up(self) -> Self {
166167
let bits = self.to_bits();
167168
if self.is_nan() || bits == Self::INFINITY.to_bits() {
@@ -182,6 +183,7 @@ pub trait FloatExt: Float {
182183
Self::from_bits(next_bits)
183184
}
184185

186+
/// A faster version of `next_up` when skipping a specified number of bits.
185187
fn n_up(self, n: Self::Int) -> Self {
186188
let bits = self.to_bits();
187189
if self.is_nan() || bits == Self::INFINITY.to_bits() || n == Self::Int::ZERO {
@@ -194,16 +196,13 @@ pub trait FloatExt: Float {
194196
let inf_bits = Self::INFINITY.to_bits();
195197
let ninf_bits = Self::NEG_INFINITY.to_bits();
196198

197-
dbg!("up", self, n, crosses_zero, is_positive, inf_bits);
198-
199199
let next_bits = if abs == Self::Int::ZERO {
200200
min(n, inf_bits)
201201
} else if crosses_zero {
202-
let off = n - abs;
203-
if off > inf_bits { inf_bits } else { off }
202+
min(n - abs, inf_bits)
204203
} else if is_positive {
205204
// Positive, counting up is more positive but this may overflow
206-
match dbg!(bits.checked_add(n)) {
205+
match bits.checked_add(n) {
207206
Some(v) if v >= inf_bits => inf_bits,
208207
Some(v) => v,
209208
None => inf_bits,
@@ -215,6 +214,7 @@ pub trait FloatExt: Float {
215214
Self::from_bits(next_bits)
216215
}
217216

217+
/// Decrement by one ULP, saturating at negative infinity.
218218
fn next_down(self) -> Self {
219219
let bits = self.to_bits();
220220
if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() {
@@ -235,6 +235,7 @@ pub trait FloatExt: Float {
235235
Self::from_bits(next_bits)
236236
}
237237

238+
/// A faster version of `n_down` when skipping a specified number of bits.
238239
fn n_down(self, n: Self::Int) -> Self {
239240
let bits = self.to_bits();
240241
if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() || n == Self::Int::ZERO {
@@ -247,21 +248,16 @@ pub trait FloatExt: Float {
247248
let inf_bits = Self::INFINITY.to_bits();
248249
let ninf_bits = Self::NEG_INFINITY.to_bits();
249250

250-
dbg!("down", self, n, crosses_zero);
251-
252251
let next_bits = if abs == Self::Int::ZERO {
253-
dbg!(min(n, inf_bits) | Self::SIGN_MASK)
252+
min(n, inf_bits) | Self::SIGN_MASK
254253
} else if crosses_zero {
255-
// dbg!(n - abs | Self::SIGN_MASK)
256-
let off = n - abs;
257-
let off = if off > inf_bits { inf_bits } else { off };
258-
off | Self::SIGN_MASK
254+
min(n - abs, inf_bits) | Self::SIGN_MASK
259255
} else if is_positive {
260256
// Positive, counting down is more negative
261257
bits - n
262258
} else {
263259
// Negative, counting up is more negative but this may overflow
264-
match dbg!(bits.checked_add(n)) {
260+
match bits.checked_add(n) {
265261
Some(v) if v > ninf_bits => ninf_bits,
266262
Some(v) => v,
267263
None => ninf_bits,
@@ -273,7 +269,6 @@ pub trait FloatExt: Float {
273269

274270
#[cfg(test)]
275271
mod tests {
276-
277272
use super::*;
278273
use crate::f8;
279274

@@ -335,13 +330,11 @@ mod tests {
335330
let v = f8::ALL[i];
336331

337332
for n in 0..(f8::ALL_LEN - 110) {
338-
eprintln!("start");
339333
let down = v.n_down(n as u8).to_bits();
340334
let up = v.n_up(n as u8).to_bits();
341335

342336
if let Some(down_exp_idx) = i.checked_sub(n) {
343337
// No overflow
344-
345338
let mut expected = f8::ALL[down_exp_idx].to_bits();
346339
if n >= 1 {
347340
let crossed = &f8::ALL[down_exp_idx..=i];
@@ -350,16 +343,13 @@ mod tests {
350343
if crossed.iter().any(|f| f8::eq_repr(*f, f8::ZERO))
351344
&& crossed.iter().any(|f| f8::eq_repr(*f, f8::NEG_ZERO))
352345
{
346+
// Saturate to -inf if we are out of values
353347
match down_exp_idx.checked_sub(1) {
354348
Some(v) => expected = f8::ALL[v].to_bits(),
355349
None => expected = f8::NEG_INFINITY.to_bits(),
356350
}
357-
358-
// down_exp_idx -= 1;
359351
}
360352
}
361-
// dbg!(f8::ALL[down_exp_idx], down_exp_idx);
362-
363353
assert_eq!(down, expected, "{i} {n} n_down({v:#010b})");
364354
} else {
365355
// Overflow to -inf
@@ -368,7 +358,6 @@ mod tests {
368358

369359
let mut up_exp_idx = i + n;
370360
if up_exp_idx < f8::ALL_LEN {
371-
dbg!(up_exp_idx);
372361
// No overflow
373362
if n >= 1 && up_exp_idx < f8::ALL_LEN {
374363
let crossed = &f8::ALL[i..=up_exp_idx];
@@ -381,7 +370,6 @@ mod tests {
381370
}
382371
}
383372

384-
dbg!(up_exp_idx);
385373
let expected = if up_exp_idx >= f8::ALL_LEN {
386374
f8::INFINITY.to_bits()
387375
} else {
@@ -396,9 +384,4 @@ mod tests {
396384
}
397385
}
398386
}
399-
400-
// #[test]
401-
// fn logspace_basic() {
402-
// let v: Vec<_> = logspace::<f8, domain::Unbounded>(f8::ZERO, f8::ONE, 10).collect();
403-
// }
404387
}

0 commit comments

Comments
 (0)