summaryrefslogtreecommitdiff
path: root/wsutil/bits_ctz.h
diff options
context:
space:
mode:
authorKevin Bracey <kevin.bracey@arm.com>2016-01-12 13:20:46 +0200
committerMichael Mann <mmann78@netscape.net>2016-01-24 00:03:18 +0000
commit9eda2fa063569c49ce238d6bbee023c91b46d879 (patch)
tree0b1da4fcc19e0e328fa9c87b9839f9a8f3c130cc /wsutil/bits_ctz.h
parent88cd6552ea861a56df9355e681dcaec50e845bd6 (diff)
downloadwireshark-9eda2fa063569c49ce238d6bbee023c91b46d879.tar.gz
Set width of hex output according to bitmask
Output from BASE_HEX et al has always been zero-padded to according to the field type - 8 digits for FT_UINT32, etc. When the field has a bitmask, this is definitely not appropriate - the field type is used to indicate the size of the bitfield container, and tells us nothing about the size of this field. Instead, determine the actual size of the field by inspecting the bitmask, and output the corresponding number of hex digits. Change-Id: I10ec4e93e1e40e8b1354d5368cc8945cf671a617 Reviewed-on: https://code.wireshark.org/review/13225 Reviewed-by: João Valverde <j@v6e.pt> Petri-Dish: João Valverde <j@v6e.pt> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Michael Mann <mmann78@netscape.net>
Diffstat (limited to 'wsutil/bits_ctz.h')
-rw-r--r--wsutil/bits_ctz.h47
1 files changed, 46 insertions, 1 deletions
diff --git a/wsutil/bits_ctz.h b/wsutil/bits_ctz.h
index dfdf24bf93..08125c04ab 100644
--- a/wsutil/bits_ctz.h
+++ b/wsutil/bits_ctz.h
@@ -25,18 +25,32 @@
#include <glib.h>
+/* ws_ctz == trailing zeros == position of lowest set bit [0..63] */
+/* ws_ilog2 == position of highest set bit == 63 - leading zeros [0..63] */
+
+/* The return value of both ws_ctz and ws_ilog2 is undefined for x == 0 */
+
#if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
+
static inline int
ws_ctz(guint64 x)
{
return __builtin_ctzll(x);
}
+
+static inline int
+ws_ilog2(guint64 x)
+{
+ return 63 - __builtin_clzll(x);
+}
+
#else
+
static inline int
__ws_ctz32(guint32 x)
{
/* From http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
- static const int table[32] = {
+ static const guint8 table[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
@@ -55,6 +69,37 @@ ws_ctz(guint64 x)
else
return __ws_ctz32(lo);
}
+
+static inline int
+__ws_ilog2_32(guint32 x)
+{
+ /* From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn */
+ static const guint8 table[32] = {
+ 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
+ 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
+ };
+
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ x |= x >> 16;
+
+ return table[((guint32)(x * 0x07C4ACDDU)) >> 27];
+}
+
+static inline int
+ws_ilog2(guint64 x)
+{
+ guint32 hi = x >> 32;
+ guint32 lo = (guint32) x;
+
+ if (hi == 0)
+ return __ws_ilog2_32(lo);
+ else
+ return 32 + __ws_ilog2_32(hi);
+}
+
#endif
#endif /* __WSUTIL_BITS_CTZ_H__ */