2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
8 function fls_32 (val: std_ulogic_vector(31 downto 0)) return integer;
9 function ffs_32 (val: std_ulogic_vector(31 downto 0)) return integer;
11 function fls_64 (val: std_ulogic_vector(63 downto 0)) return integer;
12 function ffs_64 (val: std_ulogic_vector(63 downto 0)) return integer;
14 function popcnt8(val: std_ulogic_vector(7 downto 0)) return std_ulogic_vector;
15 function popcnt32(val: std_ulogic_vector(31 downto 0)) return std_ulogic_vector;
16 function popcnt64(val: std_ulogic_vector(63 downto 0)) return std_ulogic_vector;
18 function cmp_one_byte(a, b: std_ulogic_vector(7 downto 0)) return std_ulogic_vector;
20 function ppc_signed_compare(a, b: signed(63 downto 0); so: std_ulogic) return std_ulogic_vector;
21 function ppc_unsigned_compare(a, b: unsigned(63 downto 0); so: std_ulogic) return std_ulogic_vector;
23 function ra_or_zero(ra: std_ulogic_vector(63 downto 0); reg: std_ulogic_vector(4 downto 0)) return std_ulogic_vector;
25 function byte_reverse(val: std_ulogic_vector(63 downto 0); size: integer) return std_ulogic_vector;
27 function sign_extend(val: std_ulogic_vector(63 downto 0); size: natural) return std_ulogic_vector;
29 function bit_reverse(a: std_ulogic_vector) return std_ulogic_vector;
30 function bit_number(a: std_ulogic_vector(63 downto 0)) return std_ulogic_vector;
31 function count_left_zeroes(val: std_ulogic_vector) return std_ulogic_vector;
34 package body helpers is
35 function fls_32 (val: std_ulogic_vector(31 downto 0)) return integer is
36 variable ret: integer;
39 for i in val'range loop
49 function ffs_32 (val: std_ulogic_vector(31 downto 0)) return integer is
50 variable ret: integer;
53 for i in val'reverse_range loop
63 function fls_64 (val: std_ulogic_vector(63 downto 0)) return integer is
64 variable ret: integer;
67 for i in val'range loop
77 function ffs_64 (val: std_ulogic_vector(63 downto 0)) return integer is
78 variable ret: integer;
81 for i in val'reverse_range loop
91 function popcnt8(val: std_ulogic_vector(7 downto 0)) return std_ulogic_vector is
92 variable ret: unsigned(3 downto 0) := (others => '0');
94 for i in val'range loop
95 ret := ret + ("000" & val(i));
98 return std_ulogic_vector(resize(ret, val'length));
101 function popcnt32(val: std_ulogic_vector(31 downto 0)) return std_ulogic_vector is
102 variable ret: unsigned(5 downto 0) := (others => '0');
104 for i in val'range loop
105 ret := ret + ("00000" & val(i));
108 return std_ulogic_vector(resize(ret, val'length));
111 function popcnt64(val: std_ulogic_vector(63 downto 0)) return std_ulogic_vector is
112 variable ret: unsigned(6 downto 0) := (others => '0');
114 for i in val'range loop
115 ret := ret + ("000000" & val(i));
118 return std_ulogic_vector(resize(ret, val'length));
121 function cmp_one_byte(a, b: std_ulogic_vector(7 downto 0)) return std_ulogic_vector is
122 variable ret: std_ulogic_vector(7 downto 0);
133 function ppc_signed_compare(a, b: signed(63 downto 0); so: std_ulogic) return std_ulogic_vector is
134 variable ret: std_ulogic_vector(2 downto 0);
147 function ppc_unsigned_compare(a, b: unsigned(63 downto 0); so: std_ulogic) return std_ulogic_vector is
148 variable ret: std_ulogic_vector(2 downto 0);
161 function ra_or_zero(ra: std_ulogic_vector(63 downto 0); reg: std_ulogic_vector(4 downto 0)) return std_ulogic_vector is
163 if to_integer(unsigned(reg)) = 0 then
164 return x"0000000000000000";
170 function byte_reverse(val: std_ulogic_vector(63 downto 0); size: integer) return std_ulogic_vector is
171 variable ret : std_ulogic_vector(63 downto 0) := (others => '0');
173 -- Vivado doesn't support non constant vector slices, so we have to code
177 for_2 : for k in 0 to 1 loop
178 ret(((8*k)+7) downto (8*k)) := val((8*(1-k)+7) downto (8*(1-k)));
181 for_4 : for k in 0 to 3 loop
182 ret(((8*k)+7) downto (8*k)) := val((8*(3-k)+7) downto (8*(3-k)));
185 for_8 : for k in 0 to 7 loop
186 ret(((8*k)+7) downto (8*k)) := val((8*(7-k)+7) downto (8*(7-k)));
189 report "bad byte reverse length " & integer'image(size) severity failure;
195 function sign_extend(val: std_ulogic_vector(63 downto 0); size: natural) return std_ulogic_vector is
196 variable ret : signed(63 downto 0) := (others => '0');
197 variable upper : integer := 0;
201 ret := resize(signed(val(15 downto 0)), 64);
203 ret := resize(signed(val(31 downto 0)), 64);
205 ret := resize(signed(val(63 downto 0)), 64);
207 report "bad byte reverse length " & integer'image(size) severity failure;
210 return std_ulogic_vector(ret);
214 -- Reverse the order of bits in a word
215 function bit_reverse(a: std_ulogic_vector) return std_ulogic_vector is
216 variable ret: std_ulogic_vector(a'left downto a'right);
218 for i in a'right to a'left loop
219 ret(a'left + a'right - i) := a(i);
224 -- If there is only one bit set in a doubleword, return its bit number
225 -- (counting from the right). Each bit of the result is obtained by
226 -- ORing together 32 bits of the input:
227 -- bit 0 = a[1] or a[3] or a[5] or ...
228 -- bit 1 = a[2] or a[3] or a[6] or a[7] or ...
229 -- bit 2 = a[4..7] or a[12..15] or ...
230 -- bit 5 = a[32..63] ORed together
231 function bit_number(a: std_ulogic_vector(63 downto 0)) return std_ulogic_vector is
232 variable ret: std_ulogic_vector(5 downto 0);
233 variable stride: natural;
234 variable bit: std_ulogic;
240 for j in 0 to (64 / stride) - 1 loop
242 bit := bit or (or a(k + stride - 1 downto k + (stride / 2)));
245 stride := stride * 2;
250 -- Count leading zeroes operation
251 -- Assumes the value passed in is not zero (if it is, zero is returned)
252 function count_left_zeroes(val: std_ulogic_vector) return std_ulogic_vector is
253 variable rev: std_ulogic_vector(val'left downto val'right);
254 variable sum: std_ulogic_vector(val'left downto val'right);
255 variable onehot: std_ulogic_vector(val'left downto val'right);
257 rev := bit_reverse(val);
258 sum := std_ulogic_vector(- signed(rev));
259 onehot := sum and rev;
260 return bit_number(std_ulogic_vector(resize(unsigned(onehot), 64)));
262 end package body helpers;