Skip to content

Commit fe3000d

Browse files
authored
Fix float math implementations on Rust 1.87+ (#10534)
This commit implements a workaround for rust-lang/rust#139487 which is necessary to have these implementations continue to work on more recent compilers due to the underlying implementation changing.
1 parent 9e4b24e commit fe3000d

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

crates/math/src/lib.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ pub trait WasmFloat {
3737
impl WasmFloat for f32 {
3838
#[inline]
3939
fn wasm_trunc(self) -> f32 {
40+
if self.is_nan() {
41+
return f32::NAN;
42+
}
4043
#[cfg(feature = "std")]
4144
if !cfg!(windows) && !cfg!(target_arch = "riscv64") {
4245
return self.trunc();
4346
}
44-
if self.is_nan() {
45-
return f32::NAN;
46-
}
4747
libm::truncf(self)
4848
}
4949
#[inline]
@@ -56,24 +56,24 @@ impl WasmFloat for f32 {
5656
}
5757
#[inline]
5858
fn wasm_floor(self) -> f32 {
59+
if self.is_nan() {
60+
return f32::NAN;
61+
}
5962
#[cfg(feature = "std")]
6063
if !cfg!(target_arch = "riscv64") {
6164
return self.floor();
6265
}
63-
if self.is_nan() {
64-
return f32::NAN;
65-
}
6666
libm::floorf(self)
6767
}
6868
#[inline]
6969
fn wasm_ceil(self) -> f32 {
70+
if self.is_nan() {
71+
return f32::NAN;
72+
}
7073
#[cfg(feature = "std")]
7174
if !cfg!(target_arch = "riscv64") {
7275
return self.ceil();
7376
}
74-
if self.is_nan() {
75-
return f32::NAN;
76-
}
7777
libm::ceilf(self)
7878
}
7979
#[inline]
@@ -94,13 +94,13 @@ impl WasmFloat for f32 {
9494
}
9595
#[inline]
9696
fn wasm_nearest(self) -> f32 {
97+
if self.is_nan() {
98+
return f32::NAN;
99+
}
97100
#[cfg(feature = "std")]
98101
if !cfg!(windows) && !cfg!(target_arch = "riscv64") {
99102
return self.round_ties_even();
100103
}
101-
if self.is_nan() {
102-
return f32::NAN;
103-
}
104104
let round = libm::roundf(self);
105105
if libm::fabsf(self - round) != 0.5 {
106106
return round;
@@ -162,13 +162,13 @@ impl WasmFloat for f32 {
162162
impl WasmFloat for f64 {
163163
#[inline]
164164
fn wasm_trunc(self) -> f64 {
165+
if self.is_nan() {
166+
return f64::NAN;
167+
}
165168
#[cfg(feature = "std")]
166169
if !cfg!(windows) && !cfg!(target_arch = "riscv64") {
167170
return self.trunc();
168171
}
169-
if self.is_nan() {
170-
return f64::NAN;
171-
}
172172
libm::trunc(self)
173173
}
174174
#[inline]
@@ -181,24 +181,24 @@ impl WasmFloat for f64 {
181181
}
182182
#[inline]
183183
fn wasm_floor(self) -> f64 {
184+
if self.is_nan() {
185+
return f64::NAN;
186+
}
184187
#[cfg(feature = "std")]
185188
if !cfg!(target_arch = "riscv64") {
186189
return self.floor();
187190
}
188-
if self.is_nan() {
189-
return f64::NAN;
190-
}
191191
libm::floor(self)
192192
}
193193
#[inline]
194194
fn wasm_ceil(self) -> f64 {
195+
if self.is_nan() {
196+
return f64::NAN;
197+
}
195198
#[cfg(feature = "std")]
196199
if !cfg!(target_arch = "riscv64") {
197200
return self.ceil();
198201
}
199-
if self.is_nan() {
200-
return f64::NAN;
201-
}
202202
libm::ceil(self)
203203
}
204204
#[inline]
@@ -219,13 +219,13 @@ impl WasmFloat for f64 {
219219
}
220220
#[inline]
221221
fn wasm_nearest(self) -> f64 {
222+
if self.is_nan() {
223+
return f64::NAN;
224+
}
222225
#[cfg(feature = "std")]
223226
if !cfg!(windows) && !cfg!(target_arch = "riscv64") {
224227
return self.round_ties_even();
225228
}
226-
if self.is_nan() {
227-
return f64::NAN;
228-
}
229229
let round = libm::round(self);
230230
if libm::fabs(self - round) != 0.5 {
231231
return round;

0 commit comments

Comments
 (0)