File: ../../Sources/A4_quantization_gradients.vhd
0: ----------------------------------------------------------------------------------
1: -- Engineer: Vitor Mendes Camilo
2: --
3: -- Module Name: quantization_gradients - Behavioral
4: --
5: -- Description: Code segment A.4
6: -- Quantization of the gradients
7: --
8: ----------------------------------------------------------------------------------
9:
10: library ieee;
11: use ieee.std_logic_1164.all;
12: use ieee.numeric_std.all;
13: use work.openjls_pkg.all;
14:
15: entity a4_quantization_gradients is
16: generic (
17: BITNESS : natural range 8 to 16 := CO_BITNESS_STD;
18: MAX_VAL : natural := CO_MAX_VAL_STD
19: );
20: port (
21: iD1 : in signed (BITNESS downto 0);
22: iD2 : in signed (BITNESS downto 0);
23: iD3 : in signed (BITNESS downto 0);
24: oQ1 : out signed (3 downto 0);
25: oQ2 : out signed (3 downto 0);
26: oQ3 : out signed (3 downto 0)
27: );
28: end entity a4_quantization_gradients;
29:
30: architecture behavioral of a4_quantization_gradients is
31:
32: -- T.87 compliant clamping function
33:
34: function clamp (
35: i : integer;
36: j : integer;
37: maxvalue : integer
38: ) return integer is
39: begin
40:
41: if (i > maxvalue or i < j) then
42: return j;
43: else
44: return i;
45: end if;
46:
47: end function clamp;
48:
49: function quantizate (
50: signal di : signed (BITNESS downto 0);
51: constant t1 : natural;
52: constant t2 : natural;
53: constant t3 : natural
54: ) return signed is
55:
56: variable qi : signed (3 downto 0);
57: variable vAbsQi : natural;
58: variable vSign : std_logic;
59:
60: begin
61:
62: vAbsQi := abs(to_integer(di));
63: vSign := di(di'high);
64:
65: if (vAbsQi = 0) then
66: qi := to_signed(0, qi'length);
67: elsif (vAbsQi < t1) then
68: qi := to_signed(1, qi'length);
69: elsif (vAbsQi < t2) then
70: qi := to_signed(2, qi'length);
71: elsif (vAbsQi < t3) then
72: qi := to_signed(3, qi'length);
73: else
74: qi := to_signed(4, qi'length);
75: end if;
76:
77: if (vSign = '1') then
78: qi := - qi;
79: end if;
80:
81: return qi;
82:
83: end function quantizate;
84:
85: constant BASIC_T1 : natural := 3;
86: constant BASIC_T2 : natural := 7;
87: constant BASIC_T3 : natural := 21;
88: constant FACTOR : natural := (math_min(MAX_VAL, 4095) + 128) / 256;
89: constant T1 : natural := clamp(FACTOR * (BASIC_T1 - 2) + 2, 1, MAX_VAL);
90: constant T2 : natural := clamp(FACTOR * (BASIC_T2 - 3) + 3, T1, MAX_VAL);
91: constant T3 : natural := clamp(FACTOR * (BASIC_T3 - 4) + 4, T2, MAX_VAL);
92:
93: begin
94:
95: oQ1 <= quantizate(iD1, T1, T2, T3);
96: oQ2 <= quantizate(iD2, T1, T2, T3);
97: oQ3 <= quantizate(iD3, T1, T2, T3);
98:
99: end architecture behavioral;