From 41e480c83718dc4aefb9b847a0f231a8df37e517 Mon Sep 17 00:00:00 2001 From: Hadaq in Frankfurt Date: Fri, 15 Mar 2013 19:26:08 +0100 Subject: [PATCH] forgot to add this file before --- vhdl/code/mathhelpers.vhd | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 vhdl/code/mathhelpers.vhd diff --git a/vhdl/code/mathhelpers.vhd b/vhdl/code/mathhelpers.vhd new file mode 100644 index 0000000..bbc99b2 --- /dev/null +++ b/vhdl/code/mathhelpers.vhd @@ -0,0 +1,40 @@ +library work; + +package mathhelpers is +--- find minimum number of bits required to +--- represent N as an unsigned binary number +--- +function log2_ceil(N: natural) return integer; +function plus_one_log2_ceil(N: natural) return positive; +end; + +package body mathhelpers is +--- Calculate the rounded up logarithm dualis (binary logarithm) CEIL(log2(N)) +--- represent N as an unsigned binary number +--- +function log2_ceil(N: natural) return integer is +begin +if N = 1 then + return 0; +elsif N < 3 then +return 1; +else +return 1 + log2_ceil((N-1)/2+1); +end if; +end; + + +--- from http://www.velocityreviews.com/forums/t22799-log2-n.html: +--- find minimum number of bits required to +--- represent N as an unsigned binary number +--- +function plus_one_log2_ceil(N: natural) return positive is +begin +if N < 2 then +return 1; +else +return 1 + plus_one_log2_ceil(N/2); +end if; +end; + +end; -- 2.43.0