Skip to content

Commit 1b42175

Browse files
committed
More bit twiddling
1 parent f0a3afe commit 1b42175

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

deploy/src/shift_driver.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,23 @@ end
124124

125125
# --- Flush ---
126126

127-
function _bitreverse(x::UInt32, nbits::Integer)
128-
r = UInt32(0)
129-
for i in 0:nbits-1
130-
r |= ((x >> i) & UInt32(1)) << (nbits - 1 - i)
131-
end
132-
r
127+
function _reverse_byte(b::UInt8)
128+
b = ((b & 0xF0) >> 4) | ((b & 0x0F) << 4)
129+
b = ((b & 0xCC) >> 2) | ((b & 0x33) << 2)
130+
b = ((b & 0xAA) >> 1) | ((b & 0x55) << 1)
131+
b
133132
end
134133

135134
function flush!(chain::ShiftRegisterChain)
136-
# OSR shifts left (MSB first). Bit-reverse so shadow bit 0 → Q0,
137-
# then left-align into the top of the 32-bit word.
138-
reversed = _bitreverse(chain.state & 0x00FFFFFF, NBITS)
139-
shift_out!(chain.sm, reversed << (32 - NBITS))
135+
# OSR shifts left (MSB first). Bytes go out in the right order,
136+
# but bits within each byte land reversed on Q0-Q7.
137+
# Reverse bits within each byte so shadow bit 0 → Q0.
138+
v = chain.state & 0x00FFFFFF
139+
b0 = _reverse_byte(UInt8( v & 0xFF))
140+
b1 = _reverse_byte(UInt8((v >> 8) & 0xFF))
141+
b2 = _reverse_byte(UInt8((v >> 16) & 0xFF))
142+
swapped = (UInt32(b2) << 16) | (UInt32(b1) << 8) | UInt32(b0)
143+
shift_out!(chain.sm, swapped << (32 - NBITS))
140144
end
141145

142146
# --- Bulk writes (update shadow + flush) ---

0 commit comments

Comments
 (0)