28#if !defined(_SPANDSP_BIT_OPERATIONS_H_)
29#define _SPANDSP_BIT_OPERATIONS_H_
31#if defined(__i386__) || defined(__x86_64__)
32#if !defined(__SUNPRO_C) || (__SUNPRO_C >= 0x0590)
33#define SPANDSP_USE_86_ASM
37#if defined(__cplusplus)
45static __inline__
int top_bit(
unsigned int bits)
47#if defined(SPANDSP_USE_86_ASM)
50 __asm__ (
" xorl %[res],%[res];\n"
52 " bsrl %[bits],%[res]\n"
54 : [bits]
"rm" (bits));
56#elif defined(__ppc__) || defined(__powerpc__)
59 __asm__ (
"cntlzw %[res],%[bits];\n"
79 if (bits & 0xFFFF0000)
84 if (bits & 0xFF00FF00)
89 if (bits & 0xF0F0F0F0)
94 if (bits & 0xCCCCCCCC)
99 if (bits & 0xAAAAAAAA)
111 if (bits & 0xFFFF0000)
116 if (bits & 0xFF00FF00)
121 if (bits & 0xF0F0F0F0)
126 if (bits & 0xCCCCCCCC)
131 if (bits & 0xAAAAAAAA)
144static __inline__
int bottom_bit(
unsigned int bits)
148#if defined(SPANDSP_USE_86_ASM)
149 __asm__ (
" xorl %[res],%[res];\n"
151 " bsfl %[bits],%[res]\n"
153 : [bits]
"rm" (bits));
159 if (bits & 0x0000FFFF)
164 if (bits & 0x00FF00FF)
169 if (bits & 0x0F0F0F0F)
174 if (bits & 0x33333333)
179 if (bits & 0x55555555)
192static __inline__ uint8_t bit_reverse8(uint8_t x)
194#if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__powerpc__)
196 return ((x*0x0802U & 0x22110U) | (x*0x8020U & 0x88440U))*0x10101U >> 16;
199 x = (x >> 4) | (x << 4);
200 x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
201 return ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
221#if defined(__x86_64__)
225SPAN_DECLARE(uint64_t) bit_reverse_8bytes(uint64_t data);
232SPAN_DECLARE(
void)
bit_reverse(uint8_t to[],
const uint8_t from[],
int len);
253static __inline__ uint32_t least_significant_one32(uint32_t x)
255 return (x & (-(int32_t) x));
263static __inline__ uint32_t most_significant_one32(uint32_t x)
265#if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__powerpc__)
266 return 1 << top_bit(x);
269 return (x ^ (x >> 1));
277static __inline__
int parity8(uint8_t x)
279 x = (x ^ (x >> 4)) & 0x0F;
280 return (0x6996 >> x) & 1;
287static __inline__
int parity16(uint16_t x)
290 x = (x ^ (x >> 4)) & 0x0F;
291 return (0x6996 >> x) & 1;
298static __inline__
int parity32(uint32_t x)
302 x = (x ^ (x >> 4)) & 0x0F;
303 return (0x6996 >> x) & 1;
307#if defined(__cplusplus)
uint32_t make_mask32(uint32_t x)
Create a mask as wide as the number in a 32 bit word.
Definition bit_operations.c:159
void bit_reverse(uint8_t to[], const uint8_t from[], int len)
Bit reverse each byte in a buffer.
Definition bit_operations.c:79
uint16_t bit_reverse16(uint16_t data)
Bit reverse a 16 bit word.
Definition bit_operations.c:42
uint16_t make_mask16(uint16_t x)
Create a mask as wide as the number in a 16 bit word.
Definition bit_operations.c:170
int one_bits32(uint32_t x)
Find the number of set bits in a 32 bit word.
Definition bit_operations.c:139
uint32_t bit_reverse32(uint32_t data)
Bit reverse a 32 bit word.
Definition bit_operations.c:51
uint32_t bit_reverse_4bytes(uint32_t data)
Bit reverse each of the four bytes in a 32 bit word.
Definition bit_operations.c:61