ReactOS  0.4.15-dev-341-g17c5fb8
log2.h
Go to the documentation of this file.
1 /* Integer base 2 logarithm calculation
2  *
3  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #ifndef _LINUX_LOG2_H
13 #define _LINUX_LOG2_H
14 
15 #include <linux/types.h>
16 #include <linux/bitops.h>
17 
18 /*
19  * deal with unrepresentable constant logarithms
20  */
21 int ____ilog2_NaN(void);
22 
23 /*
24  * non-constant log of base 2 calculators
25  * - the arch may override these in asm/bitops.h if they can be implemented
26  * more efficiently than using fls() and fls64()
27  * - the arch is not required to handle n==0 if implementing the fallback
28  */
29 #ifndef CONFIG_ARCH_HAS_ILOG2_U32
30 static inline __attribute__((const))
31 int __ilog2_u32(u32 n)
32 {
33  return fls(n) - 1;
34 }
35 #endif
36 
37 #ifndef CONFIG_ARCH_HAS_ILOG2_U64
38 static inline __attribute__((const))
39 int __ilog2_u64(u64 n)
40 {
41  return fls64(n) - 1;
42 }
43 #endif
44 
45 /*
46  * Determine whether some value is a power of two, where zero is
47  * *not* considered a power of two.
48  */
49 
50 static inline __attribute__((const))
51 bool is_power_of_2(unsigned long n)
52 {
53  return (n != 0 && ((n & (n - 1)) == 0));
54 }
55 
56 /*
57  * round up to nearest power of two
58  */
59 static inline __attribute__((const))
60 unsigned long __roundup_pow_of_two(unsigned long n)
61 {
62  return 1UL << fls_long(n - 1);
63 }
64 
65 /*
66  * round down to nearest power of two
67  */
68 static inline __attribute__((const))
69 unsigned long __rounddown_pow_of_two(unsigned long n)
70 {
71  return 1UL << (fls_long(n) - 1);
72 }
73 
84 #define ilog2(n) \
85 ( \
86  (sizeof(n) <= 4) ? \
87  __ilog2_u32(n) : \
88  __ilog2_u64(n) \
89  )
90 
99 #define roundup_pow_of_two(n) \
100 ( \
101  __builtin_constant_p(n) ? ( \
102  (n == 1) ? 1 : \
103  (1UL << (ilog2((n) - 1) + 1)) \
104  ) : \
105  __roundup_pow_of_two(n) \
106  )
107 
116 #define rounddown_pow_of_two(n) \
117 ( \
118  __builtin_constant_p(n) ? ( \
119  (n == 1) ? 0 : \
120  (1UL << ilog2(n))) : \
121  __rounddown_pow_of_two(n) \
122  )
123 
138 #define order_base_2(n) ilog2(roundup_pow_of_two(n))
139 
140 #endif /* _LINUX_LOG2_H */
#define is_power_of_2(x)
Definition: memory.c:2294
static int fls64(__u64 x)
Definition: bitops.h:186
GLdouble n
Definition: glext.h:7729
ULONG32 u32
Definition: btrfs.h:14
int ____ilog2_NaN(void)
static int fls(int x)
Definition: bitops.h:157
typedef bool(CARDLIBPROC *pCanDragProc)(CardRegion &stackobj
ULONG64 u64
Definition: btrfs.h:15
static __attribute__((const)) int __ilog2_u32(u32 n)
Definition: log2.h:30
static unsigned fls_long(unsigned long l)
Definition: bitops.h:239
#define UL
Definition: tui.h:82
static unsigned(__cdecl *hash_bstr)(bstr_t s)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31