From 9671dc573cbca478c6570771f16ba6741e815fa1 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 29 Dec 2020 06:37:37 -0800 Subject: Add log2 helper function --- riscv/arith.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/riscv/arith.h b/riscv/arith.h index 72bb515..b900e27 100644 --- a/riscv/arith.h +++ b/riscv/arith.h @@ -166,4 +166,12 @@ static inline int clz(uint64_t val) return res; } +static inline int log2(uint64_t val) +{ + if (!val) + return 0; + + return 63 - clz(val); +} + #endif -- cgit v1.1