Skip to content

Commit 7844496

Browse files
committed
Use reverse_bits intrinsic when running on nightly
Detect nightly and auto-enable reverse_bits
1 parent 022bf04 commit 7844496

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

.travis.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
language: rust
22

3-
matrix:
4-
include:
5-
- rust: nightly
6-
env: FEATURES=unsafe_internals
7-
- rust: nightly
8-
env: FEATURES=''
9-
- rust: stable
10-
- rust: beta
3+
rust:
4+
- stable
5+
- nightly
6+
env:
7+
- FEATURES=''
8+
- FEATURES='unsafe_internals'
119

1210
before_script:
1311
- rustup component add rustfmt-preview

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ readme = "README.md"
1010
license = "Apache-2.0 OR MIT"
1111
categories = ["data-structures"]
1212

13+
[build-dependencies]
14+
rustc_version = "0.2"
15+
1316
[dependencies]
1417
num-traits = "0.2.1"
1518
serde = { version="1.0", features=["derive"], optional=true }

benches/vob.rs

+9
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,12 @@ fn and(bench: &mut Bencher) {
101101
a.and(&b);
102102
});
103103
}
104+
105+
#[bench]
106+
fn from_bytes(b: &mut Bencher) {
107+
let mut rng = rand::thread_rng();
108+
let mut source = [0u8; 1024];
109+
rng.fill(&mut source);
110+
111+
b.iter(|| Vob::from_bytes(&source));
112+
}

build.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
extern crate rustc_version;
2+
3+
use rustc_version::{version_meta, Channel};
4+
use std::env;
5+
6+
fn main() {
7+
// Set features depending on channel
8+
if let Channel::Nightly = version_meta().unwrap().channel {
9+
println!("cargo:rustc-cfg=nightly");
10+
// Nightly supports https://github.com/rust-lang/rust/issues/48763
11+
println!("cargo:rustc-cfg=reverse_bits");
12+
}
13+
}

src/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(nightly, feature(reverse_bits))]
12
// Copyright (c) 2018 King's College London created by the Software Development Team
23
// <http://soft-dev.org/>
34
//
@@ -162,6 +163,9 @@ impl Vob<usize> {
162163
/// Create a Vob from a `u8` slice. The most significant bit of each byte comes first in the
163164
/// resulting Vob.
164165
///
166+
/// If you are running nightly, this method will use the new `reverse_bits` intrinsic.
167+
/// Set the `no_reverse_bits` feature to manually disable it.
168+
///
165169
/// # Examples
166170
///
167171
/// ```
@@ -187,12 +191,21 @@ impl Vob<usize> {
187191
continue;
188192
}
189193
let b = slice[off];
190-
if b != 0 {
191-
let mut rb: u8 = 0; // the byte b with its bits in reverse order
192-
for k in 0..8 {
193-
rb |= ((b >> k) & 1) << (8 - k - 1);
194+
#[cfg(not(reverse_bits))]
195+
{
196+
if b != 0 {
197+
{
198+
let mut rb: u8 = 0; // the byte b with its bits in reverse order
199+
for k in 0..8 {
200+
rb |= ((b >> k) & 1) << (8 - k - 1);
201+
}
202+
w |= (rb as usize) << (j * 8);
203+
}
194204
}
195-
w |= (rb as usize) << (j * 8);
205+
}
206+
#[cfg(reverse_bits)]
207+
{
208+
w |= (b.reverse_bits() as usize) << (j * 8);
196209
}
197210
}
198211
v.vec.push(w);

0 commit comments

Comments
 (0)