log2.h revision 255932
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5219820Sjeff * All rights reserved.
6219820Sjeff *
7219820Sjeff * Redistribution and use in source and binary forms, with or without
8219820Sjeff * modification, are permitted provided that the following conditions
9219820Sjeff * are met:
10219820Sjeff * 1. Redistributions of source code must retain the above copyright
11219820Sjeff *    notice unmodified, this list of conditions, and the following
12219820Sjeff *    disclaimer.
13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
14219820Sjeff *    notice, this list of conditions and the following disclaimer in the
15219820Sjeff *    documentation and/or other materials provided with the distribution.
16219820Sjeff *
17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27219820Sjeff */
28219820Sjeff
29219820Sjeff#ifndef	_LINUX_LOG2_H_
30219820Sjeff#define	_LINUX_LOG2_H_
31219820Sjeff
32219820Sjeff#include <linux/types.h>
33219820Sjeff
34219820Sjeff#include <sys/libkern.h>
35219820Sjeff
36219820Sjeffstatic inline unsigned long
37219820Sjeffroundup_pow_of_two(unsigned long x)
38219820Sjeff{
39219820Sjeff	return (1UL << flsl(x - 1));
40219820Sjeff}
41219820Sjeff
42219820Sjeffstatic inline int
43219820Sjeffis_power_of_2(unsigned long n)
44219820Sjeff{
45219820Sjeff	return (n == roundup_pow_of_two(n));
46219820Sjeff}
47219820Sjeff
48219820Sjeffstatic inline unsigned long
49219820Sjeffrounddown_pow_of_two(unsigned long x)
50219820Sjeff{
51219820Sjeff        return (1UL << (flsl(x) - 1));
52219820Sjeff}
53219820Sjeff
54255932Salfred
55255932Salfred/*
56255932Salfred * deal with unrepresentable constant logarithms
57255932Salfred */
58255932Salfredextern __attribute__((const, noreturn))
59255932Salfredint ____ilog2_NaN(void);
60255932Salfred
61255932Salfred/*
62255932Salfred * non-constant log of base 2 calculators
63255932Salfred * - the arch may override these in asm/bitops.h if they can be implemented
64255932Salfred *   more efficiently than using fls() and fls64()
65255932Salfred * - the arch is not required to handle n==0 if implementing the fallback
66255932Salfred */
67255932Salfred#ifndef CONFIG_ARCH_HAS_ILOG2_U32
68255932Salfredstatic inline __attribute__((const))
69255932Salfredint __ilog2_u32(u32 n)
70219820Sjeff{
71255932Salfred	return flsl(n) - 1;
72219820Sjeff}
73255932Salfred#endif
74219820Sjeff
75255932Salfred#ifndef CONFIG_ARCH_HAS_ILOG2_U64
76255932Salfredstatic inline __attribute__((const))
77255932Salfredint __ilog2_u64(u64 n)
78255932Salfred{
79255932Salfred	return flsl(n) - 1;
80255932Salfred}
81255932Salfred#endif
82255932Salfred
83255932Salfred
84255932Salfred/**
85255932Salfred * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
86255932Salfred * @n - parameter
87255932Salfred *
88255932Salfred * constant-capable log of base 2 calculation
89255932Salfred * - this can be used to initialise global variables from constant data, hence
90255932Salfred *   the massive ternary operator construction
91255932Salfred *
92255932Salfred * selects the appropriately-sized optimised version depending on sizeof(n)
93255932Salfred */
94255932Salfred#define ilog2(n)				\
95255932Salfred(						\
96255932Salfred	__builtin_constant_p(n) ? (		\
97255932Salfred		(n) < 1 ? ____ilog2_NaN() :	\
98255932Salfred		(n) & (1ULL << 63) ? 63 :	\
99255932Salfred		(n) & (1ULL << 62) ? 62 :	\
100255932Salfred		(n) & (1ULL << 61) ? 61 :	\
101255932Salfred		(n) & (1ULL << 60) ? 60 :	\
102255932Salfred		(n) & (1ULL << 59) ? 59 :	\
103255932Salfred		(n) & (1ULL << 58) ? 58 :	\
104255932Salfred		(n) & (1ULL << 57) ? 57 :	\
105255932Salfred		(n) & (1ULL << 56) ? 56 :	\
106255932Salfred		(n) & (1ULL << 55) ? 55 :	\
107255932Salfred		(n) & (1ULL << 54) ? 54 :	\
108255932Salfred		(n) & (1ULL << 53) ? 53 :	\
109255932Salfred		(n) & (1ULL << 52) ? 52 :	\
110255932Salfred		(n) & (1ULL << 51) ? 51 :	\
111255932Salfred		(n) & (1ULL << 50) ? 50 :	\
112255932Salfred		(n) & (1ULL << 49) ? 49 :	\
113255932Salfred		(n) & (1ULL << 48) ? 48 :	\
114255932Salfred		(n) & (1ULL << 47) ? 47 :	\
115255932Salfred		(n) & (1ULL << 46) ? 46 :	\
116255932Salfred		(n) & (1ULL << 45) ? 45 :	\
117255932Salfred		(n) & (1ULL << 44) ? 44 :	\
118255932Salfred		(n) & (1ULL << 43) ? 43 :	\
119255932Salfred		(n) & (1ULL << 42) ? 42 :	\
120255932Salfred		(n) & (1ULL << 41) ? 41 :	\
121255932Salfred		(n) & (1ULL << 40) ? 40 :	\
122255932Salfred		(n) & (1ULL << 39) ? 39 :	\
123255932Salfred		(n) & (1ULL << 38) ? 38 :	\
124255932Salfred		(n) & (1ULL << 37) ? 37 :	\
125255932Salfred		(n) & (1ULL << 36) ? 36 :	\
126255932Salfred		(n) & (1ULL << 35) ? 35 :	\
127255932Salfred		(n) & (1ULL << 34) ? 34 :	\
128255932Salfred		(n) & (1ULL << 33) ? 33 :	\
129255932Salfred		(n) & (1ULL << 32) ? 32 :	\
130255932Salfred		(n) & (1ULL << 31) ? 31 :	\
131255932Salfred		(n) & (1ULL << 30) ? 30 :	\
132255932Salfred		(n) & (1ULL << 29) ? 29 :	\
133255932Salfred		(n) & (1ULL << 28) ? 28 :	\
134255932Salfred		(n) & (1ULL << 27) ? 27 :	\
135255932Salfred		(n) & (1ULL << 26) ? 26 :	\
136255932Salfred		(n) & (1ULL << 25) ? 25 :	\
137255932Salfred		(n) & (1ULL << 24) ? 24 :	\
138255932Salfred		(n) & (1ULL << 23) ? 23 :	\
139255932Salfred		(n) & (1ULL << 22) ? 22 :	\
140255932Salfred		(n) & (1ULL << 21) ? 21 :	\
141255932Salfred		(n) & (1ULL << 20) ? 20 :	\
142255932Salfred		(n) & (1ULL << 19) ? 19 :	\
143255932Salfred		(n) & (1ULL << 18) ? 18 :	\
144255932Salfred		(n) & (1ULL << 17) ? 17 :	\
145255932Salfred		(n) & (1ULL << 16) ? 16 :	\
146255932Salfred		(n) & (1ULL << 15) ? 15 :	\
147255932Salfred		(n) & (1ULL << 14) ? 14 :	\
148255932Salfred		(n) & (1ULL << 13) ? 13 :	\
149255932Salfred		(n) & (1ULL << 12) ? 12 :	\
150255932Salfred		(n) & (1ULL << 11) ? 11 :	\
151255932Salfred		(n) & (1ULL << 10) ? 10 :	\
152255932Salfred		(n) & (1ULL <<  9) ?  9 :	\
153255932Salfred		(n) & (1ULL <<  8) ?  8 :	\
154255932Salfred		(n) & (1ULL <<  7) ?  7 :	\
155255932Salfred		(n) & (1ULL <<  6) ?  6 :	\
156255932Salfred		(n) & (1ULL <<  5) ?  5 :	\
157255932Salfred		(n) & (1ULL <<  4) ?  4 :	\
158255932Salfred		(n) & (1ULL <<  3) ?  3 :	\
159255932Salfred		(n) & (1ULL <<  2) ?  2 :	\
160255932Salfred		(n) & (1ULL <<  1) ?  1 :	\
161255932Salfred		(n) & (1ULL <<  0) ?  0 :	\
162255932Salfred		____ilog2_NaN()			\
163255932Salfred				   ) :		\
164255932Salfred	(sizeof(n) <= 4) ?			\
165255932Salfred	__ilog2_u32(n) :			\
166255932Salfred	__ilog2_u64(n)				\
167255932Salfred )
168255932Salfred
169219820Sjeff#endif	/* _LINUX_LOG2_H_ */
170