Skip to content

Commit 7c1913b

Browse files
committed
Use reverse_bits intrinsic based on feature
1 parent ecacc11 commit 7c1913b

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

.travis.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
language: rust
22

3+
rust:
4+
- stable
5+
- beta
6+
- nightly
7+
env:
8+
- FEATURES=unsafe_internals
9+
- FEATURES=reverse_bits
10+
- FEATURES=reverse_bits unsafe_internals
11+
312
matrix:
13+
# Exclude combinations with unstable features for stable and beta
14+
exclude:
15+
- rust: stable
16+
- rust: beta
417
include:
5-
- rust: nightly
6-
env: FEATURES=unsafe_internals
718
- rust: nightly
819
env: FEATURES=''
920
- rust: stable
21+
env: FEATURES=''
1022
- rust: beta
23+
env: FEATURES=''
1124

1225
script:
1326
- cargo build --features "$FEATURES"

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ rand = "0.5"
1919

2020
[features]
2121
unsafe_internals = []
22+
reverse_bits = []

benches/vob.rs

+13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#[macro_use]
44
extern crate vob;
55
extern crate test;
6+
extern crate rand;
67

78
use test::Bencher;
89
use vob::*;
10+
use rand::Rng;
911

1012
const N: usize = 100000;
1113

@@ -49,3 +51,14 @@ fn extend_vob_not_aligned(b: &mut Bencher) {
4951
vector.extend_from_vob(&source)
5052
});
5153
}
54+
55+
#[bench]
56+
fn from_bytes(b: &mut Bencher) {
57+
let mut rng = rand::thread_rng();
58+
let mut source = [0u8; 1024];
59+
rng.fill(&mut source);
60+
61+
b.iter(|| {
62+
Vob::from_bytes(&source)
63+
});
64+
}

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
//
@@ -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 the optional feature `reverse_bits` is enabled (and you're running nightly)
167+
/// it will use the new `reverse_bits` method.
168+
///
165169
/// # Examples
166170
///
167171
/// ```
@@ -188,12 +192,21 @@ impl Vob<usize> {
188192
continue;
189193
}
190194
let b = slice[off];
191-
if b != 0 {
192-
let mut rb: u8 = 0; // the byte b with its bits in reverse order
193-
for k in 0..8 {
194-
rb |= ((b >> k) & 1) << (8 - k - 1);
195+
#[cfg(not(feature = "reverse_bits"))]
196+
{
197+
if b != 0 {
198+
{
199+
let mut rb: u8 = 0; // the byte b with its bits in reverse order
200+
for k in 0..8 {
201+
rb |= ((b >> k) & 1) << (8 - k - 1);
202+
}
203+
w |= (rb as usize) << (j * 8);
204+
}
195205
}
196-
w |= (rb as usize) << (j * 8);
206+
}
207+
#[cfg(feature = "reverse_bits")]
208+
{
209+
w |= (b.reverse_bits() as usize) << (j * 8);
197210
}
198211
}
199212
v.vec.push(w);

0 commit comments

Comments
 (0)