revert: add reflect function since its used in crc.c

This commit is contained in:
iceman1001 2018-01-28 10:49:57 +01:00
parent cf44d04be1
commit 57df6a1a7e
2 changed files with 18 additions and 0 deletions

View file

@ -13,6 +13,23 @@ size_t nbytes(size_t nbits) {
return (nbits >> 3)+((nbits % 8) > 0); return (nbits >> 3)+((nbits % 8) > 0);
} }
/*
ref http://www.csm.ornl.gov/~dunigan/crc.html
Returns the value v with the bottom b [0,32] bits reflected.
Example: reflect(0x3e23L,3) == 0x3e26
*/
uint32_t reflect(uint32_t v, int b) {
uint32_t t = v;
for ( int i = 0; i < b; ++i) {
if (t & 1)
v |= BITMASK((b-1)-i);
else
v &= ~BITMASK((b-1)-i);
t>>=1;
}
return v;
}
uint8_t reflect8(uint8_t b) { uint8_t reflect8(uint8_t b) {
return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
} }

View file

@ -41,6 +41,7 @@
size_t nbytes(size_t nbits); size_t nbytes(size_t nbits);
extern uint32_t reflect(uint32_t v, int b); // used in crc.c ...
extern uint8_t reflect8(uint8_t b); // dedicated 8bit reversal extern uint8_t reflect8(uint8_t b); // dedicated 8bit reversal
extern uint16_t reflect16(uint16_t b); // dedicated 16bit reversal extern uint16_t reflect16(uint16_t b); // dedicated 16bit reversal