Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/sys Add some bitmap operation macros similar to fd_set. ...
details: https://anonhg.NetBSD.org/src/rev/54ca079bc6da
branches: trunk
changeset: 783009:54ca079bc6da
user: christos <christos%NetBSD.org@localhost>
date: Sat Dec 01 15:03:47 2012 +0000
description:
Add some bitmap operation macros similar to fd_set. Unfortunately we cannot
re-use bitstring, because it is userland only.
diffstat:
sys/sys/bitops.h | 41 ++++++++++++++++++++++++++++++++++++-----
1 files changed, 36 insertions(+), 5 deletions(-)
diffs (67 lines):
diff -r f0db7bb48216 -r 54ca079bc6da sys/sys/bitops.h
--- a/sys/sys/bitops.h Sat Dec 01 13:28:18 2012 +0000
+++ b/sys/sys/bitops.h Sat Dec 01 15:03:47 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bitops.h,v 1.9 2011/07/30 16:35:58 christos Exp $ */
+/* $NetBSD: bitops.h,v 1.10 2012/12/01 15:03:47 christos Exp $ */
/*-
* Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
@@ -188,9 +188,7 @@
* version written by David Howells.
*/
#define _ilog2_helper(_n, _x) ((_n) & (1ULL << (_x))) ? _x :
-#define ilog2(_n) \
-( \
- __builtin_constant_p(_n) ? ( \
+#define _ilog2_const(_n) ( \
_ilog2_helper(_n, 63) \
_ilog2_helper(_n, 62) \
_ilog2_helper(_n, 61) \
@@ -255,7 +253,12 @@
_ilog2_helper(_n, 2) \
_ilog2_helper(_n, 1) \
_ilog2_helper(_n, 0) \
- -1) : ((sizeof(_n) > 4 ? fls64(_n) : fls32(_n)) - 1) \
+ -1)
+
+#define ilog2(_n) \
+( \
+ __builtin_constant_p(_n) ? _ilog2_const(_n) : \
+ ((sizeof(_n) > 4 ? fls64(_n) : fls32(_n)) - 1) \
)
static __inline void
@@ -291,4 +294,32 @@
return _v - _div * fast_divide32(_v, _div, _m, _s1, _s2);
}
+#define __BITMAP_BITS(__t) (sizeof(__t) * NBBY)
+#define __BITMAP_SHIFT(__t) (ilog2(__BITMAP_BITS(__t)))
+#define __BITMAP_MASK(__t) (__BITMAP_BITS(__t) - 1)
+#define __BITMAP_SIZE(__t, __n) \
+ (((__n) + (__BITMAP_BITS(__t) - 1)) / __BITMAP_BITS(__t))
+#define __BITMAP_BIT(__n, __v) \
+ (1 << ((__n) & __BITMAP_MASK(*__v)))
+#define __BITMAP_WORD(__n, __v) \
+ ((__n) >> __BITMAP_SHIFT(*__v))
+
+#define __BITMAP_SET(__n, __v) \
+ (__v[__BITMAP_WORD(__n, __v)] |= __BITMAP_BIT(__n, __v))
+#define __BITMAP_CLR(__n, __v) \
+ (__v[__BITMAP_WORD(__n, __v)] &= ~__BITMAP_BIT(__n, __v))
+#define __BITMAP_ISSET(__n, __v) \
+ (__v[__BITMAP_WORD(__n, __v)] & __BITMAP_BIT(__n, __v))
+
+#if __GNUC_PREREQ__(2, 95)
+#define __BITMAP_ZERO(__v) \
+ (void)__builtin_memset((__v), 0, sizeof(__v))
+#else
+#define __BITMAP_ZERO(__v) do { \
+ size_t __i; \
+ for (__i = 0; __i < __arraycount(__v); __i++) \
+ (__v)[__i] = 0; \
+ } while (/* CONSTCOND */ 0)
+#endif /* GCC 2.95 */
+
#endif /* _SYS_BITOPS_H_ */
Home |
Main Index |
Thread Index |
Old Index