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(uint32_t 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(__GNUC__) && (defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__))
59 __asm__(
"clz %[res], %[bits]"
63#elif defined(__ppc__) || defined(__powerpc__)
66 __asm__ (
"cntlzw %[res],%[bits];\n"
86 if (bits & 0xFFFF0000)
91 if (bits & 0xFF00FF00)
96 if (bits & 0xF0F0F0F0)
101 if (bits & 0xCCCCCCCC)
106 if (bits & 0xAAAAAAAA)
118 if (bits & 0xFFFF0000)
123 if (bits & 0xFF00FF00)
128 if (bits & 0xF0F0F0F0)
133 if (bits & 0xCCCCCCCC)
138 if (bits & 0xAAAAAAAA)
151static __inline__
int bottom_bit(uint32_t bits)
155#if defined(SPANDSP_USE_86_ASM)
156 __asm__ (
" xorl %[res],%[res];\n"
158 " bsfl %[bits],%[res]\n"
160 : [bits]
"rm" (bits));
166 if (bits & 0x0000FFFF)
171 if (bits & 0x00FF00FF)
176 if (bits & 0x0F0F0F0F)
181 if (bits & 0x33333333)
186 if (bits & 0x55555555)
199static __inline__ uint8_t bit_reverse8(uint8_t x)
201#if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__powerpc__)
203 return ((x*0x0802U & 0x22110U) | (x*0x8020U & 0x88440U))*0x10101U >> 16;
206 x = (x >> 4) | (x << 4);
207 x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
208 return ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
228#if defined(__x86_64__)
232SPAN_DECLARE(uint64_t) bit_reverse_8bytes(uint64_t data);
239SPAN_DECLARE(
void)
bit_reverse(uint8_t to[],
const uint8_t from[],
int len);
260static __inline__ uint32_t least_significant_one32(uint32_t x)
262 return (x & (-(int32_t) x));
270static __inline__ uint32_t most_significant_one32(uint32_t x)
272#if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__powerpc__)
273 return 1 << top_bit(x);
276 return (x ^ (x >> 1));
284static __inline__
int parity8(uint8_t x)
286 x = (x ^ (x >> 4)) & 0x0F;
287 return (0x6996 >> x) & 1;
294static __inline__
int parity16(uint16_t x)
297 x = (x ^ (x >> 4)) & 0x0F;
298 return (0x6996 >> x) & 1;
305static __inline__
int parity32(uint32_t x)
309 x = (x ^ (x >> 4)) & 0x0F;
310 return (0x6996 >> x) & 1;
314#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:163
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:174
int one_bits32(uint32_t x)
Find the number of set bits in a 32 bit word.
Definition bit_operations.c:143
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