From 8f63810fd6fe99d1fe16b671700f1222875e7933 Mon Sep 17 00:00:00 2001 From: Brendan Gregg Date: Mon, 21 Sep 2015 15:39:46 -0700 Subject: [PATCH] add bpf_log2 functions --- src/cc/export/helpers.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/cc/export/helpers.h b/src/cc/export/helpers.h index 4571166..f0aaf9f 100644 --- a/src/cc/export/helpers.h +++ b/src/cc/export/helpers.h @@ -185,6 +185,28 @@ static inline void bpf_store_dword(void *skb, u64 off, u64 val) { #define MASK(_n) ((_n) < 64 ? (1ull << (_n)) - 1 : ((u64)-1LL)) #define MASK128(_n) ((_n) < 128 ? ((unsigned __int128)1 << (_n)) - 1 : ((unsigned __int128)-1)) +static unsigned int bpf_log2(unsigned int v) +{ + unsigned int r; + unsigned int shift; + + r = (v > 0xFFFF) << 4; v >>= r; + shift = (v > 0xFF) << 3; v >>= shift; r |= shift; + shift = (v > 0xF) << 2; v >>= shift; r |= shift; + shift = (v > 0x3) << 1; v >>= shift; r |= shift; + r |= (v >> 1); + return r; +} + +static unsigned int bpf_log2l(unsigned long v) +{ + unsigned int hi = v >> 32; + if (hi) + return bpf_log2(hi) + 32 + 1; + else + return bpf_log2(v) + 1; +} + struct bpf_context; static inline __attribute__((always_inline)) -- 2.7.4