Skip to content

Commit

Permalink
Use reverse_bits intrinsic based on feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thomwiggers committed May 29, 2018
1 parent 2fe98aa commit 6e6772b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
language: rust
rust: stable

matrix:
include:
- rust: nightly
env: FEATURES=reverse_bits
- rust: nightly
env: FEATURES=''
- rust: stable
- rust: beta

script:
- cargo build --features "$FEATURES"
- cargo bench --features "$FEATURES"
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ categories = ["data-structures"]
[dependencies]
num-traits = "0.2.1"
serde = { version="1.0", features=["derive"], optional=true }

[dev-dependencies]
rand = "0.5.0"

[features]
reverse_bits = []
21 changes: 21 additions & 0 deletions benches/vob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(test)]

extern crate vob;
extern crate test;
extern crate rand;

use rand::Rng;
use test::Bencher;
use vob::*;


#[bench]
fn from_bytes(b: &mut Bencher) {
let mut rng = rand::thread_rng();
let mut source = [0u8; 1024];
rng.fill(&mut source);

b.iter(|| {
Vob::from_bytes(&source)
});
}
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(feature = "reverse_bits", feature(reverse_bits))]
// Copyright (c) 2018 King's College London created by the Software Development Team
// <http://soft-dev.org/>
//
Expand Down Expand Up @@ -160,6 +161,9 @@ impl Vob<usize> {
/// Create a Vob from a `u8` slice. The most significant bit of each byte comes first in the
/// resulting Vob.
///
/// If the optional feature `reverse_bits` is enabled (and you're running nightly)
/// it will use the new `reverse_bits` method.
///
/// # Examples
///
/// ```
Expand All @@ -186,12 +190,21 @@ impl Vob<usize> {
continue;
}
let b = slice[off];
if b != 0 {
let mut rb: u8 = 0; // the byte b with its bits in reverse order
for k in 0..8 {
rb |= ((b >> k) & 1) << (8 - k - 1);
#[cfg(not(feature = "reverse_bits"))]
{
if b != 0 {
{
let mut rb: u8 = 0; // the byte b with its bits in reverse order
for k in 0..8 {
rb |= ((b >> k) & 1) << (8 - k - 1);
}
w |= (rb as usize) << (j * 8);
}
}
w |= (rb as usize) << (j * 8);
}
#[cfg(feature = "reverse_bits")]
{
w |= (b.reverse_bits() as usize) << (j * 8);
}
}
v.vec.push(w);
Expand Down

0 comments on commit 6e6772b

Please sign in to comment.