xref: /dflybsd-src/sys/dev/drm/include/linux/log2.h (revision b42e8066bb51657fe02c08196d8e571f7248f249)
11392385eSFrançois Tigeot /*-
21392385eSFrançois Tigeot  * Copyright (c) 2010 Isilon Systems, Inc.
31392385eSFrançois Tigeot  * Copyright (c) 2010 iX Systems, Inc.
41392385eSFrançois Tigeot  * Copyright (c) 2010 Panasas, Inc.
51392385eSFrançois Tigeot  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
61392385eSFrançois Tigeot  * All rights reserved.
71392385eSFrançois Tigeot  *
81392385eSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
91392385eSFrançois Tigeot  * modification, are permitted provided that the following conditions
101392385eSFrançois Tigeot  * are met:
111392385eSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
121392385eSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
131392385eSFrançois Tigeot  *    disclaimer.
141392385eSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
151392385eSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
161392385eSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
171392385eSFrançois Tigeot  *
181392385eSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191392385eSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201392385eSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211392385eSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221392385eSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231392385eSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241392385eSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251392385eSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261392385eSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271392385eSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281392385eSFrançois Tigeot  */
291392385eSFrançois Tigeot 
301392385eSFrançois Tigeot #ifndef	_LINUX_LOG2_H_
311392385eSFrançois Tigeot #define	_LINUX_LOG2_H_
321392385eSFrançois Tigeot 
331392385eSFrançois Tigeot #include <linux/types.h>
341392385eSFrançois Tigeot 
351392385eSFrançois Tigeot #include <sys/libkern.h>
361392385eSFrançois Tigeot 
371392385eSFrançois Tigeot static inline unsigned long
roundup_pow_of_two(unsigned long x)381392385eSFrançois Tigeot roundup_pow_of_two(unsigned long x)
391392385eSFrançois Tigeot {
401392385eSFrançois Tigeot 	return (1UL << flsl(x - 1));
411392385eSFrançois Tigeot }
421392385eSFrançois Tigeot 
431392385eSFrançois Tigeot static inline int
is_power_of_2(unsigned long n)441392385eSFrançois Tigeot is_power_of_2(unsigned long n)
451392385eSFrançois Tigeot {
461392385eSFrançois Tigeot 	return (n == roundup_pow_of_two(n));
471392385eSFrançois Tigeot }
481392385eSFrançois Tigeot 
491392385eSFrançois Tigeot static inline unsigned long
rounddown_pow_of_two(unsigned long x)501392385eSFrançois Tigeot rounddown_pow_of_two(unsigned long x)
511392385eSFrançois Tigeot {
521392385eSFrançois Tigeot 	return (1UL << (flsl(x) - 1));
531392385eSFrançois Tigeot }
541392385eSFrançois Tigeot 
551392385eSFrançois Tigeot /**
561392385eSFrançois Tigeot  * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
571392385eSFrançois Tigeot  * @n - parameter
581392385eSFrançois Tigeot  *
591392385eSFrançois Tigeot  * constant-capable log of base 2 calculation
601392385eSFrançois Tigeot  * - this can be used to initialise global variables from constant data, hence
611392385eSFrançois Tigeot  *   the massive ternary operator construction
621392385eSFrançois Tigeot  *
631392385eSFrançois Tigeot  * selects the appropriately-sized optimised version depending on sizeof(n)
641392385eSFrançois Tigeot  */
651392385eSFrançois Tigeot #define	ilog2(n)				\
661392385eSFrançois Tigeot (						\
671392385eSFrançois Tigeot 	__builtin_constant_p(n) ? (		\
68*b42e8066Szrj 		(n) < 2 ? 0 :			\
691392385eSFrançois Tigeot 		(n) & (1ULL << 63) ? 63 :	\
701392385eSFrançois Tigeot 		(n) & (1ULL << 62) ? 62 :	\
711392385eSFrançois Tigeot 		(n) & (1ULL << 61) ? 61 :	\
721392385eSFrançois Tigeot 		(n) & (1ULL << 60) ? 60 :	\
731392385eSFrançois Tigeot 		(n) & (1ULL << 59) ? 59 :	\
741392385eSFrançois Tigeot 		(n) & (1ULL << 58) ? 58 :	\
751392385eSFrançois Tigeot 		(n) & (1ULL << 57) ? 57 :	\
761392385eSFrançois Tigeot 		(n) & (1ULL << 56) ? 56 :	\
771392385eSFrançois Tigeot 		(n) & (1ULL << 55) ? 55 :	\
781392385eSFrançois Tigeot 		(n) & (1ULL << 54) ? 54 :	\
791392385eSFrançois Tigeot 		(n) & (1ULL << 53) ? 53 :	\
801392385eSFrançois Tigeot 		(n) & (1ULL << 52) ? 52 :	\
811392385eSFrançois Tigeot 		(n) & (1ULL << 51) ? 51 :	\
821392385eSFrançois Tigeot 		(n) & (1ULL << 50) ? 50 :	\
831392385eSFrançois Tigeot 		(n) & (1ULL << 49) ? 49 :	\
841392385eSFrançois Tigeot 		(n) & (1ULL << 48) ? 48 :	\
851392385eSFrançois Tigeot 		(n) & (1ULL << 47) ? 47 :	\
861392385eSFrançois Tigeot 		(n) & (1ULL << 46) ? 46 :	\
871392385eSFrançois Tigeot 		(n) & (1ULL << 45) ? 45 :	\
881392385eSFrançois Tigeot 		(n) & (1ULL << 44) ? 44 :	\
891392385eSFrançois Tigeot 		(n) & (1ULL << 43) ? 43 :	\
901392385eSFrançois Tigeot 		(n) & (1ULL << 42) ? 42 :	\
911392385eSFrançois Tigeot 		(n) & (1ULL << 41) ? 41 :	\
921392385eSFrançois Tigeot 		(n) & (1ULL << 40) ? 40 :	\
931392385eSFrançois Tigeot 		(n) & (1ULL << 39) ? 39 :	\
941392385eSFrançois Tigeot 		(n) & (1ULL << 38) ? 38 :	\
951392385eSFrançois Tigeot 		(n) & (1ULL << 37) ? 37 :	\
961392385eSFrançois Tigeot 		(n) & (1ULL << 36) ? 36 :	\
971392385eSFrançois Tigeot 		(n) & (1ULL << 35) ? 35 :	\
981392385eSFrançois Tigeot 		(n) & (1ULL << 34) ? 34 :	\
991392385eSFrançois Tigeot 		(n) & (1ULL << 33) ? 33 :	\
1001392385eSFrançois Tigeot 		(n) & (1ULL << 32) ? 32 :	\
1011392385eSFrançois Tigeot 		(n) & (1ULL << 31) ? 31 :	\
1021392385eSFrançois Tigeot 		(n) & (1ULL << 30) ? 30 :	\
1031392385eSFrançois Tigeot 		(n) & (1ULL << 29) ? 29 :	\
1041392385eSFrançois Tigeot 		(n) & (1ULL << 28) ? 28 :	\
1051392385eSFrançois Tigeot 		(n) & (1ULL << 27) ? 27 :	\
1061392385eSFrançois Tigeot 		(n) & (1ULL << 26) ? 26 :	\
1071392385eSFrançois Tigeot 		(n) & (1ULL << 25) ? 25 :	\
1081392385eSFrançois Tigeot 		(n) & (1ULL << 24) ? 24 :	\
1091392385eSFrançois Tigeot 		(n) & (1ULL << 23) ? 23 :	\
1101392385eSFrançois Tigeot 		(n) & (1ULL << 22) ? 22 :	\
1111392385eSFrançois Tigeot 		(n) & (1ULL << 21) ? 21 :	\
1121392385eSFrançois Tigeot 		(n) & (1ULL << 20) ? 20 :	\
1131392385eSFrançois Tigeot 		(n) & (1ULL << 19) ? 19 :	\
1141392385eSFrançois Tigeot 		(n) & (1ULL << 18) ? 18 :	\
1151392385eSFrançois Tigeot 		(n) & (1ULL << 17) ? 17 :	\
1161392385eSFrançois Tigeot 		(n) & (1ULL << 16) ? 16 :	\
1171392385eSFrançois Tigeot 		(n) & (1ULL << 15) ? 15 :	\
1181392385eSFrançois Tigeot 		(n) & (1ULL << 14) ? 14 :	\
1191392385eSFrançois Tigeot 		(n) & (1ULL << 13) ? 13 :	\
1201392385eSFrançois Tigeot 		(n) & (1ULL << 12) ? 12 :	\
1211392385eSFrançois Tigeot 		(n) & (1ULL << 11) ? 11 :	\
1221392385eSFrançois Tigeot 		(n) & (1ULL << 10) ? 10 :	\
1231392385eSFrançois Tigeot 		(n) & (1ULL <<  9) ?  9 :	\
1241392385eSFrançois Tigeot 		(n) & (1ULL <<  8) ?  8 :	\
1251392385eSFrançois Tigeot 		(n) & (1ULL <<  7) ?  7 :	\
1261392385eSFrançois Tigeot 		(n) & (1ULL <<  6) ?  6 :	\
1271392385eSFrançois Tigeot 		(n) & (1ULL <<  5) ?  5 :	\
1281392385eSFrançois Tigeot 		(n) & (1ULL <<  4) ?  4 :	\
1291392385eSFrançois Tigeot 		(n) & (1ULL <<  3) ?  3 :	\
1301392385eSFrançois Tigeot 		(n) & (1ULL <<  2) ?  2 :	\
131*b42e8066Szrj 		1 ) :				\
1321392385eSFrançois Tigeot 	(sizeof(n) <= 4) ?			\
133*b42e8066Szrj 	fls((u32)(n)) - 1 : flsll((u64)(n)) - 1	\
1341392385eSFrançois Tigeot )
1351392385eSFrançois Tigeot 
136bf05ec5bSzrj #define	order_base_2(n) ilog2(roundup_pow_of_two(n))
137bf05ec5bSzrj 
1381392385eSFrançois Tigeot #endif	/* _LINUX_LOG2_H_ */
139