Skip to content

Commit 2a34269

Browse files
Better algorithm to repeat bits 3 times (#110)
1 parent eac90df commit 2a34269

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

‎src/encode/bc7.rs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,13 +2741,12 @@ fn bits_repeat2_u16(x: u16) -> u32 {
27412741
///
27422742
/// E.g. `0b1010` becomes `0b111000111000`.
27432743
fn bits_repeat3_u16(x: u16) -> u64 {
2744-
// TODO: find a faster algorithm
2745-
let mut result: u64 = 0;
2746-
for i in 0..16 {
2747-
let bit = ((x >> i) & 1) * 0b111;
2748-
result |= (bit as u64) << (i * 3);
2749-
}
2750-
result
2744+
let mut x = x as u64;
2745+
x = (x | (x << 16)) & 0b000000000000000011111111_000000000000000011111111;
2746+
x = (x | (x << 8)) & 0b_000000001111000000001111_000000001111000000001111;
2747+
x = (x | (x << 4)) & 0b_000011000011000011000011_000011000011000011000011;
2748+
x = (x | (x << 2)) & 0b_001001001001001001001001_001001001001001001001001;
2749+
x | (x << 1) | (x << 2)
27512750
}
27522751

27532752
#[cfg(test)]

0 commit comments

Comments
 (0)