Skip to content

Commit 6e6772b

Browse files
committed
Use reverse_bits intrinsic based on feature
1 parent 2fe98aa commit 6e6772b

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

.travis.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
language: rust
2-
rust: stable
2+
3+
matrix:
4+
include:
5+
- rust: nightly
6+
env: FEATURES=reverse_bits
7+
- rust: nightly
8+
env: FEATURES=''
9+
- rust: stable
10+
- rust: beta
11+
12+
script:
13+
- cargo build --features "$FEATURES"
14+
- cargo bench --features "$FEATURES"

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ categories = ["data-structures"]
1313
[dependencies]
1414
num-traits = "0.2.1"
1515
serde = { version="1.0", features=["derive"], optional=true }
16+
17+
[dev-dependencies]
18+
rand = "0.5.0"
19+
20+
[features]
21+
reverse_bits = []

benches/vob.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(test)]
2+
3+
extern crate vob;
4+
extern crate test;
5+
extern crate rand;
6+
7+
use rand::Rng;
8+
use test::Bencher;
9+
use vob::*;
10+
11+
12+
#[bench]
13+
fn from_bytes(b: &mut Bencher) {
14+
let mut rng = rand::thread_rng();
15+
let mut source = [0u8; 1024];
16+
rng.fill(&mut source);
17+
18+
b.iter(|| {
19+
Vob::from_bytes(&source)
20+
});
21+
}

src/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(feature = "reverse_bits", feature(reverse_bits))]
12
// Copyright (c) 2018 King's College London created by the Software Development Team
23
// <http://soft-dev.org/>
34
//
@@ -160,6 +161,9 @@ impl Vob<usize> {
160161
/// Create a Vob from a `u8` slice. The most significant bit of each byte comes first in the
161162
/// resulting Vob.
162163
///
164+
/// If the optional feature `reverse_bits` is enabled (and you're running nightly)
165+
/// it will use the new `reverse_bits` method.
166+
///
163167
/// # Examples
164168
///
165169
/// ```
@@ -186,12 +190,21 @@ impl Vob<usize> {
186190
continue;
187191
}
188192
let b = slice[off];
189-
if b != 0 {
190-
let mut rb: u8 = 0; // the byte b with its bits in reverse order
191-
for k in 0..8 {
192-
rb |= ((b >> k) & 1) << (8 - k - 1);
193+
#[cfg(not(feature = "reverse_bits"))]
194+
{
195+
if b != 0 {
196+
{
197+
let mut rb: u8 = 0; // the byte b with its bits in reverse order
198+
for k in 0..8 {
199+
rb |= ((b >> k) & 1) << (8 - k - 1);
200+
}
201+
w |= (rb as usize) << (j * 8);
202+
}
193203
}
194-
w |= (rb as usize) << (j * 8);
204+
}
205+
#[cfg(feature = "reverse_bits")]
206+
{
207+
w |= (b.reverse_bits() as usize) << (j * 8);
195208
}
196209
}
197210
v.vec.push(w);

0 commit comments

Comments
 (0)