about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-03-30 01:31:16 +0200
committerGitHub <noreply@github.com>2018-03-30 01:31:16 +0200
commit361ddfeb1324179811280002c1e40443cc9c74a1 (patch)
tree1271a47d7590ed291d61220132971d886d2516c7 /src/liballoc
parent9ab97a8747d968943662c3be533bf3337eb9876d (diff)
parent262be13643cf5e3ff4f4e880b2dee601d4740fd8 (diff)
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.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/vec.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index df08e46fe25..2eedb964f88 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -1599,8 +1599,8 @@ impl_spec_from_elem!(u64, |x| x == 0);
 impl_spec_from_elem!(u128, |x| x == 0);
 impl_spec_from_elem!(usize, |x| x == 0);
 
-impl_spec_from_elem!(f32, |x: f32| x == 0. && x.is_sign_positive());
-impl_spec_from_elem!(f64, |x: f64| x == 0. && x.is_sign_positive());
+impl_spec_from_elem!(f32, |x: f32| x.to_bits() == 0);
+impl_spec_from_elem!(f64, |x: f64| x.to_bits() == 0);
 
 ////////////////////////////////////////////////////////////////////////////////
 // Common trait implementations for Vec