Skip to content

Commit 467fa51

Browse files
committed
Switch canonicalize to a trait method
1 parent c2db65a commit 467fa51

File tree

7 files changed

+15
-12
lines changed

7 files changed

+15
-12
lines changed

libm/src/math/generic/fmax.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ use crate::support::Float;
1919
#[inline]
2020
pub fn fmax<F: Float>(x: F, y: F) -> F {
2121
let res = if x.is_nan() || x < y { y } else { x };
22-
// Canonicalize
23-
res * F::ONE
22+
res.canonicalize()
2423
}

libm/src/math/generic/fmaximum.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ pub fn fmaximum<F: Float>(x: F, y: F) -> F {
2323
y
2424
};
2525

26-
// Canonicalize
27-
res * F::ONE
26+
res.canonicalize()
2827
}

libm/src/math/generic/fmaximum_num.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ pub fn fmaximum_num<F: Float>(x: F, y: F) -> F {
2525
y
2626
};
2727

28-
// Canonicalize
29-
res * F::ONE
28+
res.canonicalize()
3029
}

libm/src/math/generic/fmin.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ use crate::support::Float;
1919
#[inline]
2020
pub fn fmin<F: Float>(x: F, y: F) -> F {
2121
let res = if y.is_nan() || x < y { x } else { y };
22-
// Canonicalize
23-
res * F::ONE
22+
res.canonicalize()
2423
}

libm/src/math/generic/fminimum.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ pub fn fminimum<F: Float>(x: F, y: F) -> F {
2323
y
2424
};
2525

26-
// Canonicalize
27-
res * F::ONE
26+
res.canonicalize()
2827
}

libm/src/math/generic/fminimum_num.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ pub fn fminimum_num<F: Float>(x: F, y: F) -> F {
2525
x
2626
};
2727

28-
// Canonicalize
29-
res * F::ONE
28+
res.canonicalize()
3029
}

libm/src/math/support/float_traits.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ pub trait Float:
190190
Self::ONE.copysign(self)
191191
}
192192
}
193+
194+
/// Make a best-effort attempt to canonicalize the number. Note that this is often allowed
195+
/// to be a nop and does not actually quiet sNaNs.
196+
fn canonicalize(self) -> Self {
197+
// FIXME: LLVM often removes this. We should determine whether we can remove the operation,
198+
// or switch to something based on `llvm.canonicalize` (which has crashes,
199+
// <https://github.com/llvm/llvm-project/issues/32650>).
200+
self * Self::ONE
201+
}
193202
}
194203

195204
/// Access the associated `Int` type from a float (helper to avoid ambiguous associated types).

0 commit comments

Comments
 (0)