| 1 |
#![feature(test)] |
| 2 |
|
| 3 |
extern crate bit_field; |
| 4 |
|
| 5 |
use bit_field::*; |
| 6 |
|
| 7 |
pub trait BitOper { |
| 8 |
const BIT_LEN: usize; |
| 9 |
fn get_b(&self, idx: usize) -> bool; |
| 10 |
fn set_b(&mut self, idx: usize, val: bool); |
| 11 |
fn toggle(&mut self, idx: usize); |
| 12 |
} |
| 13 |
|
| 14 |
pub trait BitArrayOper<T: BitOper> { |
| 15 |
fn get_blen(&self) -> usize; |
| 16 |
fn get_b(&self, idx: usize) -> bool; |
| 17 |
fn set_b(&mut self, idx: usize, val: bool); |
| 18 |
fn toggle(&mut self, idx: usize); |
| 19 |
} |
| 20 |
|
| 21 |
impl BitOper for u8 { |
| 22 |
const BIT_LEN: usize = std::mem::size_of::<Self>() as usize * 8; |
| 23 |
|
| 24 |
fn set_b(&mut self, idx: usize, val: bool) { |
| 25 |
assert!(idx < Self::BIT_LEN); |
| 26 |
if val { |
| 27 |
*self |= 1 << idx; |
| 28 |
} else { |
| 29 |
*self &= !(1 << idx); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
fn get_b(&self, idx: usize) -> bool { |
| 34 |
assert!(idx < Self::BIT_LEN); |
| 35 |
(self & 1 << idx) != 0 |
| 36 |
} |
| 37 |
|
| 38 |
fn toggle(&mut self, idx: usize) { |
| 39 |
assert!(idx < Self::BIT_LEN); |
| 40 |
*self ^= 1 << idx; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl BitOper for u32 { |
| 45 |
const BIT_LEN: usize = std::mem::size_of::<Self>() as usize * 8; |
| 46 |
fn set_b(&mut self, idx: usize, val: bool) { |
| 47 |
assert!(idx < Self::BIT_LEN); |
| 48 |
if val { |
| 49 |
*self |= 1 << idx; |
| 50 |
} else { |
| 51 |
*self &= !(1 << idx); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
fn get_b(&self, idx: usize) -> bool { |
| 56 |
assert!(idx < Self::BIT_LEN); |
| 57 |
(self & 1 << idx) != 0 |
| 58 |
} |
| 59 |
|
| 60 |
fn toggle(&mut self, idx: usize) { |
| 61 |
assert!(idx < Self::BIT_LEN); |
| 62 |
*self ^= 1 << idx; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl BitOper for u64 { |
| 67 |
const BIT_LEN: usize = std::mem::size_of::<Self>() as usize * 8; |
| 68 |
fn set_b(&mut self, idx: usize, val: bool) { |
| 69 |
assert!(idx < Self::BIT_LEN); |
| 70 |
if val { |
| 71 |
*self |= 1 << idx; |
| 72 |
} else { |
| 73 |
*self &= !(1 << idx); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
fn get_b(&self, idx: usize) -> bool { |
| 78 |
assert!(idx < Self::BIT_LEN); |
| 79 |
(self & 1 << idx) != 0 |
| 80 |
} |
| 81 |
|
| 82 |
fn toggle(&mut self, idx: usize) { |
| 83 |
assert!(idx < Self::BIT_LEN); |
| 84 |
*self ^= 1 << idx; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl<T: BitOper> BitArrayOper<T> for [T] { |
| 89 |
fn get_blen(&self) -> usize { |
| 90 |
self.len() * T::BIT_LEN |
| 91 |
} |
| 92 |
|
| 93 |
fn get_b(&self, idx: usize) -> bool { |
| 94 |
self[idx / T::BIT_LEN].get_b(idx % T::BIT_LEN) |
| 95 |
} |
| 96 |
|
| 97 |
fn set_b(&mut self, idx: usize, val: bool) { |
| 98 |
self[idx / T::BIT_LEN].set_b(idx % T::BIT_LEN, val); |
| 99 |
} |
| 100 |
|
| 101 |
fn toggle(&mut self, idx: usize) { |
| 102 |
self[idx / T::BIT_LEN].toggle(idx % T::BIT_LEN); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
extern crate test; |
| 107 |
|
| 108 |
use test::Bencher; |
| 109 |
|
| 110 |
const LEN: usize = 256; |
| 111 |
|
| 112 |
fn set_bitfield<T: BitField>(v: &mut Vec<T>) { |
| 113 |
for i in 0..v.len() * T::BIT_LENGTH { |
| 114 |
v.as_mut_slice().set_bit(i, true);; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fn get_bitfield<T: BitField>(v: &Vec<T>) { |
| 119 |
for i in 0..v.len() * T::BIT_LENGTH { |
| 120 |
let _b = v.as_slice().get_bit(i); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
fn set_trivial<T: BitOper>(v: &mut Vec<T>) { |
| 125 |
for i in 0..v.len() * T::BIT_LEN { |
| 126 |
v.set_b(i, true); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fn get_trivial<T: BitOper>(v: &Vec<T>) { |
| 131 |
for i in 0..v.len() * T::BIT_LEN { |
| 132 |
let _b = v.get_b(i); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[bench] |
| 137 |
fn u8_set_bitfield(b: &mut Bencher) { |
| 138 |
let mut v = vec![0u8; LEN]; |
| 139 |
b.iter(|| { |
| 140 |
set_bitfield(&mut v); |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
#[bench] |
| 145 |
fn u8_set_trivial(b: &mut Bencher) { |
| 146 |
let mut v = vec![0u8; LEN]; |
| 147 |
|
| 148 |
b.iter(|| { |
| 149 |
set_trivial(&mut v); |
| 150 |
}); |
| 151 |
} |
| 152 |
|
| 153 |
#[bench] |
| 154 |
fn u8_get_bitfield(b: &mut Bencher) { |
| 155 |
let v = vec![1u8; LEN]; |
| 156 |
b.iter(|| { |
| 157 |
get_bitfield(&v); |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
#[bench] |
| 162 |
fn u8_get_trivial(b: &mut Bencher) { |
| 163 |
let v = vec![1u8; LEN]; |
| 164 |
b.iter(|| { |
| 165 |
get_trivial(&v); |
| 166 |
}); |
| 167 |
} |
| 168 |
|
| 169 |
#[bench] |
| 170 |
fn u32_set_bitfield(b: &mut Bencher) { |
| 171 |
let mut v = vec![0u32; LEN]; |
| 172 |
b.iter(|| { |
| 173 |
set_bitfield(&mut v); |
| 174 |
}); |
| 175 |
} |
| 176 |
|
| 177 |
#[bench] |
| 178 |
fn u32_set_trivial(b: &mut Bencher) { |
| 179 |
let mut v = vec![0u32; LEN]; |
| 180 |
|
| 181 |
b.iter(|| { |
| 182 |
set_trivial(&mut v); |
| 183 |
}); |
| 184 |
} |
| 185 |
|
| 186 |
#[bench] |
| 187 |
fn u32_get_bitfield(b: &mut Bencher) { |
| 188 |
let v = vec![1u32; LEN]; |
| 189 |
b.iter(|| { |
| 190 |
get_bitfield(&v); |
| 191 |
}); |
| 192 |
} |
| 193 |
|
| 194 |
#[bench] |
| 195 |
fn u32_get_trivial(b: &mut Bencher) { |
| 196 |
let v = vec![1u32; LEN]; |
| 197 |
b.iter(|| { |
| 198 |
get_trivial(&v); |
| 199 |
}); |
| 200 |
} |
| 201 |
|
| 202 |
#[bench] |
| 203 |
fn u64_set_bitfield(b: &mut Bencher) { |
| 204 |
let mut v = vec![0u64; LEN]; |
| 205 |
b.iter(|| { |
| 206 |
set_bitfield(&mut v); |
| 207 |
}); |
| 208 |
} |
| 209 |
|
| 210 |
#[bench] |
| 211 |
fn u64_set_trivial(b: &mut Bencher) { |
| 212 |
let mut v = vec![0u64; LEN]; |
| 213 |
|
| 214 |
b.iter(|| { |
| 215 |
set_trivial(&mut v); |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
#[bench] |
| 220 |
fn u64_get_bitfield(b: &mut Bencher) { |
| 221 |
let v = vec![1u64; LEN]; |
| 222 |
b.iter(|| { |
| 223 |
get_bitfield(&v); |
| 224 |
}); |
| 225 |
} |
| 226 |
|
| 227 |
#[bench] |
| 228 |
fn u64_get_trivial(b: &mut Bencher) { |
| 229 |
let v = vec![1u64; LEN]; |
| 230 |
b.iter(|| { |
| 231 |
get_trivial(&v); |
| 232 |
}); |
| 233 |
} |