Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F115964793
D45536.id139669.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
10 KB
Referenced Files
None
Subscribers
None
D45536.id139669.diff
View Options
Index: sys/compat/linuxkpi/common/include/linux/log2.h
===================================================================
--- sys/compat/linuxkpi/common/include/linux/log2.h
+++ sys/compat/linuxkpi/common/include/linux/log2.h
@@ -33,24 +33,6 @@
#include <sys/libkern.h>
-static inline unsigned long
-roundup_pow_of_two(unsigned long x)
-{
- return (1UL << flsl(x - 1));
-}
-
-static inline int
-is_power_of_2(unsigned long n)
-{
- return (n == roundup_pow_of_two(n));
-}
-
-static inline unsigned long
-rounddown_pow_of_two(unsigned long x)
-{
- return (1UL << (flsl(x) - 1));
-}
-
-#define order_base_2(x) ilog2(roundup_pow_of_two(x))
+#define is_power_of_2(n) ((((n) - 1) & (n)) == 0)
#endif /* _LINUXKPI_LINUX_LOG2_H_ */
Index: sys/dev/drm2/drm_os_freebsd.h
===================================================================
--- sys/dev/drm2/drm_os_freebsd.h
+++ sys/dev/drm2/drm_os_freebsd.h
@@ -234,13 +234,6 @@
#define div_u64(n, d) ((n) / (d))
#define hweight32(i) bitcount32(i)
-static inline unsigned long
-roundup_pow_of_two(unsigned long x)
-{
-
- return (1UL << flsl(x - 1));
-}
-
/**
* ror32 - rotate a 32-bit value right
* @word: value to rotate
Index: sys/dev/mana/gdma_util.h
===================================================================
--- sys/dev/mana/gdma_util.h
+++ sys/dev/mana/gdma_util.h
@@ -170,18 +170,6 @@
return (max);
}
-static inline unsigned long
-roundup_pow_of_two(unsigned long x)
-{
- return (1UL << flsl(x - 1));
-}
-
-static inline int
-is_power_of_2(unsigned long n)
-{
- return (n == roundup_pow_of_two(n));
-}
-
struct completion {
unsigned int done;
struct mtx lock;
Index: sys/dev/qlnx/qlnxe/bcm_osal.h
===================================================================
--- sys/dev/qlnx/qlnxe/bcm_osal.h
+++ sys/dev/qlnx/qlnxe/bcm_osal.h
@@ -101,24 +101,7 @@
#define s32 uint32_t
#ifndef QLNX_RDMA
-
-static __inline unsigned long
-roundup_pow_of_two(unsigned long x)
-{
- return (1UL << flsl(x - 1));
-}
-
-static __inline int
-is_power_of_2(unsigned long n)
-{
- return (n == roundup_pow_of_two(n));
-}
-
-static __inline unsigned long
-rounddown_pow_of_two(unsigned long x)
-{
- return (1UL << (flsl(x) - 1));
-}
+#include <sys/log2.h>
#define max_t(type, val1, val2) \
((type)(val1) > (type)(val2) ? (type)(val1) : (val2))
Index: sys/sys/libkern.h
===================================================================
--- sys/sys/libkern.h
+++ sys/sys/libkern.h
@@ -36,6 +36,7 @@
#ifdef _KERNEL
#include <sys/systm.h>
#endif
+#include <sys/log2.h>
#ifndef LIBKERN_INLINE
#define LIBKERN_INLINE static __inline
@@ -186,112 +187,6 @@
8 * sizeof(mask) - __builtin_clzll((unsigned long long)mask));
}
-static __inline __pure2 int
-ilog2_int(int n)
-{
-
- KASSERT(n != 0, ("ilog argument must be nonzero"));
- return (8 * sizeof(n) - 1 - __builtin_clz((u_int)n));
-}
-
-static __inline __pure2 int
-ilog2_long(long n)
-{
-
- KASSERT(n != 0, ("ilog argument must be nonzero"));
- return (8 * sizeof(n) - 1 - __builtin_clzl((u_long)n));
-}
-
-static __inline __pure2 int
-ilog2_long_long(long long n)
-{
-
- KASSERT(n != 0, ("ilog argument must be nonzero"));
- return (8 * sizeof(n) - 1 -
- __builtin_clzll((unsigned long long)n));
-}
-
-#define ilog2_var(n) \
- _Generic((n), \
- default: ilog2_int, \
- long: ilog2_long, \
- unsigned long: ilog2_long, \
- long long: ilog2_long_long, \
- unsigned long long: ilog2_long_long \
- )(n)
-
-#define ilog2(n) \
-( \
- __builtin_constant_p(n) ? ( \
- (n) < 1 ? -1 : \
- (n) & (1ULL << 63) ? 63 : \
- (n) & (1ULL << 62) ? 62 : \
- (n) & (1ULL << 61) ? 61 : \
- (n) & (1ULL << 60) ? 60 : \
- (n) & (1ULL << 59) ? 59 : \
- (n) & (1ULL << 58) ? 58 : \
- (n) & (1ULL << 57) ? 57 : \
- (n) & (1ULL << 56) ? 56 : \
- (n) & (1ULL << 55) ? 55 : \
- (n) & (1ULL << 54) ? 54 : \
- (n) & (1ULL << 53) ? 53 : \
- (n) & (1ULL << 52) ? 52 : \
- (n) & (1ULL << 51) ? 51 : \
- (n) & (1ULL << 50) ? 50 : \
- (n) & (1ULL << 49) ? 49 : \
- (n) & (1ULL << 48) ? 48 : \
- (n) & (1ULL << 47) ? 47 : \
- (n) & (1ULL << 46) ? 46 : \
- (n) & (1ULL << 45) ? 45 : \
- (n) & (1ULL << 44) ? 44 : \
- (n) & (1ULL << 43) ? 43 : \
- (n) & (1ULL << 42) ? 42 : \
- (n) & (1ULL << 41) ? 41 : \
- (n) & (1ULL << 40) ? 40 : \
- (n) & (1ULL << 39) ? 39 : \
- (n) & (1ULL << 38) ? 38 : \
- (n) & (1ULL << 37) ? 37 : \
- (n) & (1ULL << 36) ? 36 : \
- (n) & (1ULL << 35) ? 35 : \
- (n) & (1ULL << 34) ? 34 : \
- (n) & (1ULL << 33) ? 33 : \
- (n) & (1ULL << 32) ? 32 : \
- (n) & (1ULL << 31) ? 31 : \
- (n) & (1ULL << 30) ? 30 : \
- (n) & (1ULL << 29) ? 29 : \
- (n) & (1ULL << 28) ? 28 : \
- (n) & (1ULL << 27) ? 27 : \
- (n) & (1ULL << 26) ? 26 : \
- (n) & (1ULL << 25) ? 25 : \
- (n) & (1ULL << 24) ? 24 : \
- (n) & (1ULL << 23) ? 23 : \
- (n) & (1ULL << 22) ? 22 : \
- (n) & (1ULL << 21) ? 21 : \
- (n) & (1ULL << 20) ? 20 : \
- (n) & (1ULL << 19) ? 19 : \
- (n) & (1ULL << 18) ? 18 : \
- (n) & (1ULL << 17) ? 17 : \
- (n) & (1ULL << 16) ? 16 : \
- (n) & (1ULL << 15) ? 15 : \
- (n) & (1ULL << 14) ? 14 : \
- (n) & (1ULL << 13) ? 13 : \
- (n) & (1ULL << 12) ? 12 : \
- (n) & (1ULL << 11) ? 11 : \
- (n) & (1ULL << 10) ? 10 : \
- (n) & (1ULL << 9) ? 9 : \
- (n) & (1ULL << 8) ? 8 : \
- (n) & (1ULL << 7) ? 7 : \
- (n) & (1ULL << 6) ? 6 : \
- (n) & (1ULL << 5) ? 5 : \
- (n) & (1ULL << 4) ? 4 : \
- (n) & (1ULL << 3) ? 3 : \
- (n) & (1ULL << 2) ? 2 : \
- (n) & (1ULL << 1) ? 1 : \
- (n) & (1ULL << 0) ? 0 : \
- -1) : \
- ilog2_var(n) \
-)
-
#define bitcount64(x) __bitcount64((uint64_t)(x))
#define bitcount32(x) __bitcount32((uint32_t)(x))
#define bitcount16(x) __bitcount16((uint16_t)(x))
Index: sys/sys/log2.h
===================================================================
--- /dev/null
+++ sys/sys/log2.h
@@ -0,0 +1,155 @@
+/*-
+ * Copyright (c) 2010 Isilon Systems, Inc.
+ * Copyright (c) 2010 iX Systems, Inc.
+ * Copyright (c) 2010 Panasas, Inc.
+ * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice unmodified, this list of conditions, and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef _LOG2_H_
+#define _LOG2_H_
+
+#ifndef __pure2
+#define __pure2 __attribute((__const__))
+#endif
+
+#ifndef KASSERT
+#define KASSERT(ex, line)
+#endif
+
+static __inline __pure2 int
+ilog2_int(int n)
+{
+
+ KASSERT(n != 0, ("ilog argument must be nonzero"));
+ return (8 * sizeof(n) - 1 - __builtin_clz((unsigned int)n));
+}
+
+static __inline __pure2 int
+ilog2_long(long n)
+{
+
+ KASSERT(n != 0, ("ilog argument must be nonzero"));
+ return (8 * sizeof(n) - 1 - __builtin_clzl((unsigned long)n));
+}
+
+static __inline __pure2 int
+ilog2_long_long(long long n)
+{
+
+ KASSERT(n != 0, ("ilog argument must be nonzero"));
+ return (8 * sizeof(n) - 1 -
+ __builtin_clzll((unsigned long long)n));
+}
+
+#define ilog2_var(n) \
+ _Generic((n), \
+ default: ilog2_int, \
+ long: ilog2_long, \
+ unsigned long: ilog2_long, \
+ long long: ilog2_long_long, \
+ unsigned long long: ilog2_long_long \
+ )(n)
+
+/*
+ * deal with unrepresentable constant logarithms
+ */
+extern __attribute__((const, noreturn))
+int ____ilog2_NaN(void);
+
+#define ilog2_const(n) \
+( \
+ (n) < 1 ? ____ilog2_NaN() : \
+ (n) & (1ULL << 63) ? 63 : \
+ (n) & (1ULL << 62) ? 62 : \
+ (n) & (1ULL << 61) ? 61 : \
+ (n) & (1ULL << 60) ? 60 : \
+ (n) & (1ULL << 59) ? 59 : \
+ (n) & (1ULL << 58) ? 58 : \
+ (n) & (1ULL << 57) ? 57 : \
+ (n) & (1ULL << 56) ? 56 : \
+ (n) & (1ULL << 55) ? 55 : \
+ (n) & (1ULL << 54) ? 54 : \
+ (n) & (1ULL << 53) ? 53 : \
+ (n) & (1ULL << 52) ? 52 : \
+ (n) & (1ULL << 51) ? 51 : \
+ (n) & (1ULL << 50) ? 50 : \
+ (n) & (1ULL << 49) ? 49 : \
+ (n) & (1ULL << 48) ? 48 : \
+ (n) & (1ULL << 47) ? 47 : \
+ (n) & (1ULL << 46) ? 46 : \
+ (n) & (1ULL << 45) ? 45 : \
+ (n) & (1ULL << 44) ? 44 : \
+ (n) & (1ULL << 43) ? 43 : \
+ (n) & (1ULL << 42) ? 42 : \
+ (n) & (1ULL << 41) ? 41 : \
+ (n) & (1ULL << 40) ? 40 : \
+ (n) & (1ULL << 39) ? 39 : \
+ (n) & (1ULL << 38) ? 38 : \
+ (n) & (1ULL << 37) ? 37 : \
+ (n) & (1ULL << 36) ? 36 : \
+ (n) & (1ULL << 35) ? 35 : \
+ (n) & (1ULL << 34) ? 34 : \
+ (n) & (1ULL << 33) ? 33 : \
+ (n) & (1ULL << 32) ? 32 : \
+ (n) & (1ULL << 31) ? 31 : \
+ (n) & (1ULL << 30) ? 30 : \
+ (n) & (1ULL << 29) ? 29 : \
+ (n) & (1ULL << 28) ? 28 : \
+ (n) & (1ULL << 27) ? 27 : \
+ (n) & (1ULL << 26) ? 26 : \
+ (n) & (1ULL << 25) ? 25 : \
+ (n) & (1ULL << 24) ? 24 : \
+ (n) & (1ULL << 23) ? 23 : \
+ (n) & (1ULL << 22) ? 22 : \
+ (n) & (1ULL << 21) ? 21 : \
+ (n) & (1ULL << 20) ? 20 : \
+ (n) & (1ULL << 19) ? 19 : \
+ (n) & (1ULL << 18) ? 18 : \
+ (n) & (1ULL << 17) ? 17 : \
+ (n) & (1ULL << 16) ? 16 : \
+ (n) & (1ULL << 15) ? 15 : \
+ (n) & (1ULL << 14) ? 14 : \
+ (n) & (1ULL << 13) ? 13 : \
+ (n) & (1ULL << 12) ? 12 : \
+ (n) & (1ULL << 11) ? 11 : \
+ (n) & (1ULL << 10) ? 10 : \
+ (n) & (1ULL << 9) ? 9 : \
+ (n) & (1ULL << 8) ? 8 : \
+ (n) & (1ULL << 7) ? 7 : \
+ (n) & (1ULL << 6) ? 6 : \
+ (n) & (1ULL << 5) ? 5 : \
+ (n) & (1ULL << 4) ? 4 : \
+ (n) & (1ULL << 3) ? 3 : \
+ (n) & (1ULL << 2) ? 2 : \
+ (n) & (1ULL << 1) ? 1 : \
+ (n) & (1ULL << 0) ? 0 : \
+ ____ilog2_NaN() \
+)
+
+#define ilog2(n) (__builtin_constant_p(n)? ilog2_const(n) : ilog2_var(n))
+#define rounddown_pow_of_two(n) ((typeof(n))1 << ilog2(n))
+#define order_base_2(n) ((n) == 1 ? 0 : 1 + ilog2((n) - 1))
+#define roundup_pow_of_two(n) ((typeof(n))1 << order_base_2(n))
+
+#endif /* _LOG2_H_ */
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, May 1, 11:36 PM (4 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
17892899
Default Alt Text
D45536.id139669.diff (10 KB)
Attached To
Mode
D45536: log2: move log2 related functions from kpi to libkern
Attached
Detach File
Event Timeline
Log In to Comment