Subversion Repositories Kolibri OS

Rev

Rev 6934 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6934 Rev 6936
Line 14... Line 14...
14
 
14
 
15
#include 
15
#include 
Line 16... Line 16...
16
#include 
16
#include 
-
 
17
 
-
 
18
/*
-
 
19
 * deal with unrepresentable constant logarithms
-
 
20
 */
-
 
21
extern __attribute__((const, noreturn))
-
 
22
int ____ilog2_NaN(void);
17
 
23
 
18
/*
24
/*
19
 * non-constant log of base 2 calculators
25
 * non-constant log of base 2 calculators
20
 * - the arch may override these in asm/bitops.h if they can be implemented
26
 * - the arch may override these in asm/bitops.h if they can be implemented
21
 *   more efficiently than using fls() and fls64()
27
 *   more efficiently than using fls() and fls64()
Line 77... Line 83...
77
 * selects the appropriately-sized optimised version depending on sizeof(n)
83
 * selects the appropriately-sized optimised version depending on sizeof(n)
78
 */
84
 */
79
#define ilog2(n)				\
85
#define ilog2(n)				\
80
(						\
86
(						\
81
	__builtin_constant_p(n) ? (		\
87
	__builtin_constant_p(n) ? (		\
82
		(n) < 2 ? 0 :			\
88
		(n) < 1 ? ____ilog2_NaN() :	\
83
		(n) & (1ULL << 63) ? 63 :	\
89
		(n) & (1ULL << 63) ? 63 :	\
84
		(n) & (1ULL << 62) ? 62 :	\
90
		(n) & (1ULL << 62) ? 62 :	\
85
		(n) & (1ULL << 61) ? 61 :	\
91
		(n) & (1ULL << 61) ? 61 :	\
86
		(n) & (1ULL << 60) ? 60 :	\
92
		(n) & (1ULL << 60) ? 60 :	\
87
		(n) & (1ULL << 59) ? 59 :	\
93
		(n) & (1ULL << 59) ? 59 :	\
Line 140... Line 146...
140
		(n) & (1ULL <<  6) ?  6 :	\
146
		(n) & (1ULL <<  6) ?  6 :	\
141
		(n) & (1ULL <<  5) ?  5 :	\
147
		(n) & (1ULL <<  5) ?  5 :	\
142
		(n) & (1ULL <<  4) ?  4 :	\
148
		(n) & (1ULL <<  4) ?  4 :	\
143
		(n) & (1ULL <<  3) ?  3 :	\
149
		(n) & (1ULL <<  3) ?  3 :	\
144
		(n) & (1ULL <<  2) ?  2 :	\
150
		(n) & (1ULL <<  2) ?  2 :	\
-
 
151
		(n) & (1ULL <<  1) ?  1 :	\
-
 
152
		(n) & (1ULL <<  0) ?  0 :	\
-
 
153
		____ilog2_NaN()			\
145
		1 ) :				\
154
				   ) :		\
146
	(sizeof(n) <= 4) ?			\
155
	(sizeof(n) <= 4) ?			\
147
	__ilog2_u32(n) :			\
156
	__ilog2_u32(n) :			\
148
	__ilog2_u64(n)				\
157
	__ilog2_u64(n)				\
149
 )
158
 )
Line 192... Line 201...
192
 *  ob2(4) = 2
201
 *  ob2(4) = 2
193
 *  ob2(5) = 3
202
 *  ob2(5) = 3
194
 *  ... and so on.
203
 *  ... and so on.
195
 */
204
 */
Line 196... Line -...
196
 
-
 
197
static inline __attribute_const__
205
 
198
int __order_base_2(unsigned long n)
-
 
199
{
-
 
200
	return n > 1 ? ilog2(n - 1) + 1 : 0;
-
 
Line 201... Line -...
201
}
-
 
202
 
-
 
203
#define order_base_2(n)				\
-
 
204
(						\
-
 
205
	__builtin_constant_p(n) ? (		\
-
 
206
		((n) == 0 || (n) == 1) ? 0 :	\
-
 
207
		ilog2((n) - 1) + 1) :		\
-
 
208
	__order_base_2(n)			\
206
#define order_base_2(n) ilog2(roundup_pow_of_two(n))