2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 count leading zeroes function and those that can be derived from it
37 // powerpc native count leading zeroes instruction:
38 #define CLZ(x) ((int)__cntlzw((unsigned int)x))
40 #elif defined(__GNUC__)
42 /* use gcc's builtins */
43 static __inline__ int32
CLZ(int32 arg
)
46 return __builtin_clz(arg
);
51 #elif defined(_MSC_VER)
54 #pragma intrinsic(_BitScanReverse)
56 __forceinline
static int32
CLZ( int32 arg
)
59 if (_BitScanReverse(&idx
, (unsigned long)arg
))
61 return (int32
)(31-idx
);
66 #elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__)
68 static __inline__ int32
CLZ(int32 arg
) {
69 __asm__
volatile("cntlzw %0, %1" : "=r" (arg
) : "r" (arg
));
73 #elif defined(__i386__) || defined(__x86_64__)
74 static __inline__ int32
CLZ(int32 arg
) {
76 __asm__
volatile("bsrl %0, %0\nxorl $31, %0\n"
77 : "=r" (arg
) : "0" (arg
));
84 #elif defined(SC_IPHONE)
85 static __inline__ int32
CLZ(int32 arg
)
87 return __builtin_clz(arg
);
91 # error "clz.h: Unsupported architecture"
94 // count trailing zeroes
95 inline int32
CTZ(int32 x
)
97 return 32 - CLZ(~x
& (x
-1));
100 // count leading ones
101 inline int32
CLO(int32 x
)
106 // count trailing ones
107 inline int32
CTO(int32 x
)
109 return 32 - CLZ(x
& (~x
-1));
112 // number of bits required to represent x.
113 inline int32
NUMBITS(int32 x
)
118 // log2 of the next power of two greater than or equal to x.
119 inline int32
LOG2CEIL(int32 x
)
121 return 32 - CLZ(x
- 1);
124 // is x a power of two
125 inline bool ISPOWEROFTWO(int32 x
)
127 return (x
& (x
-1)) == 0;
130 // next power of two greater than or equal to x
131 inline int32
NEXTPOWEROFTWO(int32 x
)
133 return 1L << LOG2CEIL(x
);
136 // previous power of two less than or equal to x
137 inline int32
PREVIOUSPOWEROFTWO(int32 x
)
141 return 1L << (LOG2CEIL(x
) - 1);
144 // input a series of counting integers, outputs a series of gray codes .
145 inline int32
GRAYCODE(int32 x
)
150 // find least significant bit
151 inline int32
LSBit(int32 x
)
156 // find least significant bit position
157 inline int32
LSBitPos(int32 x
)
162 // find most significant bit position
163 inline int32
MSBitPos(int32 x
)
168 // find most significant bit
169 inline int32
MSBit(int32 x
)
171 return 1L << MSBitPos(x
);
174 // count number of one bits
175 inline uint32
ONES(uint32 x
)
178 x
= x
- ((x
>> 1) & 0x55555555);
179 t
= ((x
>> 2) & 0x33333333);
180 x
= (x
& 0x33333333) + t
;
181 x
= (x
+ (x
>> 4)) & 0x0F0F0F0F;
187 // count number of zero bits
188 inline uint32
ZEROES(uint32 x
)
194 // reverse bits in a word
195 inline uint32
BitReverse(uint32 x
)
197 x
= ((x
& 0xAAAAAAAA) >> 1) | ((x
& 0x55555555) << 1);
198 x
= ((x
& 0xCCCCCCCC) >> 2) | ((x
& 0x33333333) << 2);
199 x
= ((x
& 0xF0F0F0F0) >> 4) | ((x
& 0x0F0F0F0F) << 4);
200 x
= ((x
& 0xFF00FF00) >> 8) | ((x
& 0x00FF00FF) << 8);
201 return (x
>> 16) | (x
<< 16);
205 inline uint32
RotateRight (uint32 x
, uint32 s
)
208 return (x
<< (32-s
)) | (x
>> s
);
211 inline uint32
RotateLeft (uint32 x
, uint32 s
)
214 return (x
>> (32-s
)) | (x
<< s
);