aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/plugin_interface/clz.h
blob: f205c02f56bd17126e51200030108d16d136a2c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*

count leading zeroes function and those that can be derived from it

*/


#ifndef _CLZ_
#define _CLZ_

#include "SC_Types.h"

#ifdef __MWERKS__

#define __PPC__ 1
#define __X86__ 0

// powerpc native count leading zeroes instruction:
#define CLZ(x)  ((int)__cntlzw((unsigned int)x))

#elif defined(SC_WIN32) && !defined(__GCC__)

static int32 CLZ( int32 arg )
{
    __asm{
        bsr    eax, arg
        jnz    non_zero
        mov    arg, 32
        jmp    end
non_zero:
        xor    eax, 31
        mov    arg, eax
end:
    }
    return arg;
}

#elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__)

static __inline__ int32 CLZ(int32 arg) {
	__asm__ volatile("cntlzw %0, %1" : "=r" (arg) : "r" (arg));
	return arg;
}

#elif defined(__i386__) || defined(__x86_64__)
static __inline__ int32 CLZ(int32 arg) {
	if (arg) {
		__asm__ volatile("bsrl %0, %0\nxorl $31, %0\n"
						 : "=r" (arg) : "0" (arg));
	} else {
		arg = 32;
	}
	return arg;
}

#else
# error "clz.h: Unsupported architecture"
#endif

// count trailing zeroes
inline int32 CTZ(int32 x) 
{
	return 32 - CLZ(~x & (x-1));
}

// count leading ones
inline int32 CLO(int32 x) 
{
	return CLZ(~x);
}

// count trailing ones
inline int32 CTO(int32 x) 
{
	return 32 - CLZ(x & (~x-1));
}

// number of bits required to represent x. 
inline int32 NUMBITS(int32 x) 
{
	return 32 - CLZ(x);
}

// log2 of the next power of two greater than or equal to x. 
inline int32 LOG2CEIL(int32 x) 
{
	return 32 - CLZ(x - 1);
}

// next power of two greater than or equal to x
inline int32 NEXTPOWEROFTWO(int32 x) 
{
	return 1L << LOG2CEIL(x);
}

// is x a power of two
inline bool ISPOWEROFTWO(int32 x) 
{
	return (x & (x-1)) == 0;
}

// input a series of counting integers, outputs a series of gray codes .
inline int32 GRAYCODE(int32 x)
{
	return x & (x>>1);
}

// find least significant bit
inline int32 LSBit(int32 x)
{
	return x & -x;
}

// find least significant bit position
inline int32 LSBitPos(int32 x)
{
	return CTZ(x & -x);
}

// find most significant bit position
inline int32 MSBitPos(int32 x)
{
	return 31 - CLZ(x);
}

// find most significant bit
inline int32 MSBit(int32 x)
{
	return 1L << MSBitPos(x);
}

// count number of one bits
inline uint32 ONES(uint32 x) 
{
	uint32 t; 
	x = x - ((x >> 1) & 0x55555555); 
	t = ((x >> 2) & 0x33333333); 
	x = (x & 0x33333333) + t; 
	x = (x + (x >> 4)) & 0x0F0F0F0F; 
	x = x + (x << 8); 
	x = x + (x << 16); 
	return x >> 24; 
}

// count number of zero bits
inline uint32 ZEROES(uint32 x)
{
	return ONES(~x);
}


// reverse bits in a word
inline uint32 BitReverse(uint32 x)
{
  x = ((x & 0xAAAAAAAA) >>  1) | ((x & 0x55555555) <<  1);
  x = ((x & 0xCCCCCCCC) >>  2) | ((x & 0x33333333) <<  2);
  x = ((x & 0xF0F0F0F0) >>  4) | ((x & 0x0F0F0F0F) <<  4);
  x = ((x & 0xFF00FF00) >>  8) | ((x & 0x00FF00FF) <<  8);
  return (x >> 16) | (x << 16);
}

// barrel shifts
inline uint32 RotateRight (uint32 x, uint32 s)
{
	s = s & 31;
	return (x << (32-s)) | (x >> s);
}
   
inline uint32 RotateLeft (uint32 x, uint32 s)
{
	s = s & 31;
	return (x >> (32-s)) | (x << s);
}

#endif