Skip to content

Commit

Permalink
Merge pull request #16 from thomwiggers/fast_reverse
Browse files Browse the repository at this point in the history
Use reverse_bits intrinsic when supported
  • Loading branch information
ltratt authored Jun 29, 2018
2 parents 022bf04 + ef0d020 commit bbef6a0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
14 changes: 6 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
language: rust

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

before_script:
- rustup component add rustfmt-preview
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ readme = "README.md"
license = "Apache-2.0 OR MIT"
categories = ["data-structures"]

[build-dependencies]
rustc_version = "0.2"

[dependencies]
num-traits = "0.2.1"
serde = { version="1.0", features=["derive"], optional=true }
Expand Down
9 changes: 9 additions & 0 deletions benches/vob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,12 @@ fn and(bench: &mut Bencher) {
a.and(&b);
});
}

#[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));
}
12 changes: 12 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extern crate rustc_version;

use rustc_version::{version_meta, Channel};

fn main() {
// Set features depending on channel
if let Channel::Nightly = version_meta().unwrap().channel {
println!("cargo:rustc-cfg=nightly");
// Nightly supports https://github.com/rust-lang/rust/issues/48763
println!("cargo:rustc-cfg=reverse_bits");
}
}
22 changes: 17 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(nightly, feature(reverse_bits))]
// Copyright (c) 2018 King's College London created by the Software Development Team
// <http://soft-dev.org/>
//
Expand Down Expand Up @@ -162,6 +163,8 @@ impl Vob<usize> {
/// Create a Vob from a `u8` slice. The most significant bit of each byte comes first in the
/// resulting Vob.
///
/// If you are running nightly, this method will use the new `reverse_bits` intrinsic.
///
/// # Examples
///
/// ```
Expand All @@ -187,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(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(reverse_bits)]
{
w |= (b.reverse_bits() as usize) << (j * 8);
}
}
v.vec.push(w);
Expand Down

0 comments on commit bbef6a0

Please sign in to comment.