From 4b4c2f03f79dbd7a9b9138371440117a514644ce Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Wed, 12 Jun 2019 08:00:58 +0200 Subject: Add mutils. --- src/mutils.adb | 27 +++++++++++++++++++++++++++ src/mutils.ads | 7 +++++++ 2 files changed, 34 insertions(+) create mode 100644 src/mutils.adb create mode 100644 src/mutils.ads diff --git a/src/mutils.adb b/src/mutils.adb new file mode 100644 index 000000000..1b40bff79 --- /dev/null +++ b/src/mutils.adb @@ -0,0 +1,27 @@ +package body Mutils is + function Clog2 (V : Uns64) return Integer + is + Low : Natural; + begin + if V >= 2**16 then + if V >= 2**32 then + Low := 32; + else + Low := 16; + end if; + else + if V >= 2**8 then + Low := 8; + else + Low := 0; + end if; + end if; + for I in Low .. 63 loop + if 2**I >= V then + return I; + end if; + end loop; + return 64; + end Clog2; + +end Mutils; diff --git a/src/mutils.ads b/src/mutils.ads new file mode 100644 index 000000000..4275fe6c0 --- /dev/null +++ b/src/mutils.ads @@ -0,0 +1,7 @@ +with Types; use Types; + +package Mutils is + -- Return the ceiling log2 of V. + -- Returns 0 for 0. + function Clog2 (V : Uns64) return Integer; +end Mutils; -- cgit v1.2.3