Skip to content

Commit 361ddfe

Browse files
authored
Rollup merge of #49466 - glandium:master, r=rkruppe
Use f{32,64}::to_bits for is_zero test in vec::SpecFromElem vec::SpecFromElem provides an optimization to use calloc to fill a Vec when the element given to fill the Vec is represented by 0. For floats, the test for that currently used is `x == 0. && x.is_sign_positive()`. When compiled in a standalone function, rustc generates the following assembly: ``` xorps xmm1, xmm1 ucomisd xmm0, xmm1 setnp al sete cl and cl, al movq rax, xmm0 test rax, rax setns al and al, cl ret ``` A simpler test telling us whether the value is represented by 0, is `x.to_bits() == 0`, which rustc compiles to: ``` movq rax, xmm0 test rax, rax sete al ret ``` Not that the test is hot in any way, but it also makes it clearer what the intent in the rust code is.
2 parents 9ab97a8 + 262be13 commit 361ddfe

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/liballoc/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,8 +1599,8 @@ impl_spec_from_elem!(u64, |x| x == 0);
15991599
impl_spec_from_elem!(u128, |x| x == 0);
16001600
impl_spec_from_elem!(usize, |x| x == 0);
16011601

1602-
impl_spec_from_elem!(f32, |x: f32| x == 0. && x.is_sign_positive());
1603-
impl_spec_from_elem!(f64, |x: f64| x == 0. && x.is_sign_positive());
1602+
impl_spec_from_elem!(f32, |x: f32| x.to_bits() == 0);
1603+
impl_spec_from_elem!(f64, |x: f64| x.to_bits() == 0);
16041604

16051605
////////////////////////////////////////////////////////////////////////////////
16061606
// Common trait implementations for Vec

0 commit comments

Comments
 (0)