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