ReactOS 0.4.15-dev-8039-g69ebfd6
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 */
21int ____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
30static inline __attribute__((const))
31int __ilog2_u32(u32 n)
32{
33 return fls(n) - 1;
34}
35#endif
36
37#ifndef CONFIG_ARCH_HAS_ILOG2_U64
38static inline __attribute__((const))
39int __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
50static inline __attribute__((const))
51bool 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 */
59static inline __attribute__((const))
60unsigned 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 */
68static inline __attribute__((const))
69unsigned 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 */
static int fls(int x)
Definition: bitops.h:157
static int fls64(__u64 x)
Definition: bitops.h:186
static unsigned fls_long(unsigned long l)
Definition: bitops.h:239
ULONG32 u32
Definition: btrfs.h:14
ULONG64 u64
Definition: btrfs.h:15
#define __attribute__(x)
Definition: wpp_private.h:207
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define is_power_of_2(x)
Definition: memory.c:2306
GLdouble n
Definition: glext.h:7729
int ____ilog2_NaN(void)
static unsigned(__cdecl *hash_bstr)(bstr_t s)
#define bool
Definition: nsiface.idl:72