41 #ifndef _CABitOperations_h_
42 #define _CABitOperations_h_
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
46 #include <CoreFoundation/CFBase.h>
51 #include <TargetConditionals.h>
54 inline UInt32 IsPowerOfTwo(UInt32 x)
56 return (x & (x-1)) == 0;
63 #define CountLeadingZeroes(x) ((int)__cntlzw((unsigned int)x))
67 static int CountLeadingZeroes(
int arg )
80 static __inline__
int CountLeadingZeroes(
int arg) {
81 #if TARGET_CPU_PPC || TARGET_CPU_PPC64
82 __asm__
volatile(
"cntlzw %0, %1" :
"=r" (arg) :
"r" (arg));
84 #elif TARGET_CPU_X86 || TARGET_CPU_X86_64
95 if (arg == 0)
return 32;
96 return __builtin_clz(arg);
103 inline UInt32 CountTrailingZeroes(UInt32 x)
105 return 32 - CountLeadingZeroes(~x & (x-1));
109 inline UInt32 CountLeadingOnes(UInt32 x)
111 return CountLeadingZeroes(~x);
115 inline UInt32 CountTrailingOnes(UInt32 x)
117 return 32 - CountLeadingZeroes(x & (~x-1));
121 inline UInt32 NumBits(UInt32 x)
123 return 32 - CountLeadingZeroes(x);
127 inline UInt32 Log2Ceil(UInt32 x)
129 return 32 - CountLeadingZeroes(x - 1);
133 inline UInt32 NextPowerOfTwo(UInt32 x)
135 return 1L << Log2Ceil(x);
139 inline UInt32 CountOnes(UInt32 x)
143 x = x - ((x >> 1) & 0x55555555);
144 t = ((x >> 2) & 0x33333333);
145 x = (x & 0x33333333) + t;
146 x = (x + (x >> 4)) & 0x0F0F0F0F;
153 inline UInt32 CountZeroes(UInt32 x)
155 return CountOnes(~x);
159 inline UInt32 LSBitPos(UInt32 x)
161 return CountTrailingZeroes(x & -(SInt32)x);
165 inline UInt32 LSBit(UInt32 x)
167 return x & -(SInt32)x;
171 inline UInt32 MSBitPos(UInt32 x)
173 return 31 - CountLeadingZeroes(x);
177 inline UInt32 MSBit(UInt32 x)
179 return 1UL << MSBitPos(x);
183 inline UInt32 DivInt(UInt32 numerator, UInt32 denominator)
185 if(IsPowerOfTwo(denominator))
186 return numerator >> (31 - CountLeadingZeroes(denominator));
188 return numerator/denominator;