Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company: Instituto Superior Técnico
  3. -- Prof: Paulo Alexandre Crisóstomo Lopes
  4. -- paulo.lopes@inesc-id.pt
  5. --
  6. -- Create Date: 14:01:57 12/27/2011
  7. -- Design Name:
  8. -- Module Name: multiplier - Behavioral
  9. -- Project Name:
  10. -- Target Devices:
  11. -- Tool versions:
  12. -- Description: VHDL implementation of a 32 bit floating multiplier.
  13. -- Float format: sign | 8 bits exponent + 127 | 23 bits normalized mantissa.
  14. -- Uses IEEE 754-1985, with the following exceptions.
  15. -- NaN is not implemented. Operations that would result in NaN
  16. -- have a non definied result.
  17. -- An exponent of all zeros will always mean zero, and an
  18. -- exponent of all ones will always mean infinity.
  19. -- Rounding is round nearest ties away from zero.
  20. -- Non normalized numbers are not implemented.
  21. --
  22. -- Dependencies:
  23. --
  24. -- Revision:
  25. -- Revision 1.02
  26. -- - Removal of comparators to test undeflow and overflow.
  27. -- - Adding of test for 0*x and inf*x.
  28. -- - Added rounding.
  29. --
  30. -- Additional Comments:
  31. --
  32. ----------------------------------------------------------------------------------
  33. library IEEE;
  34. use IEEE.STD_LOGIC_1164.ALL;
  35. use ieee.std_logic_unsigned.all;
  36.  
  37. entity multiplier is
  38. Port ( x : in STD_LOGIC_VECTOR (31 downto 0);
  39. y : in STD_LOGIC_VECTOR (31 downto 0);
  40. z : out STD_LOGIC_VECTOR (31 downto 0));
  41. end multiplier;
  42.  
  43. architecture Behavioral of multiplier is
  44.  
  45. begin
  46. process(x,y)
  47. variable x_mantissa : STD_LOGIC_VECTOR (22 downto 0);
  48. variable x_exponent : STD_LOGIC_VECTOR (7 downto 0);
  49. variable x_sign : STD_LOGIC;
  50. variable y_mantissa : STD_LOGIC_VECTOR (22 downto 0);
  51. variable y_exponent : STD_LOGIC_VECTOR (7 downto 0);
  52. variable y_sign : STD_LOGIC;
  53. variable z_mantissa : STD_LOGIC_VECTOR (22 downto 0);
  54. variable z_exponent : STD_LOGIC_VECTOR (7 downto 0);
  55. variable z_sign : STD_LOGIC;
  56. variable aux : STD_LOGIC;
  57. variable aux2 : STD_LOGIC_VECTOR (47 downto 0);
  58. variable exponent_sum : STD_LOGIC_VECTOR (8 downto 0);
  59. begin
  60. x_mantissa := x(22 downto 0);
  61. x_exponent := x(30 downto 23);
  62. x_sign := x(31);
  63. y_mantissa := y(22 downto 0);
  64. y_exponent := y(30 downto 23);
  65. y_sign := y(31);
  66.  
  67. -- inf*0 is not tested (result would be NaN)
  68. if (x_exponent=255 or y_exponent=255) then
  69. -- inf*x or x*inf
  70. z_exponent := "11111111";
  71. z_mantissa := (others => '0');
  72. z_sign := x_sign xor y_sign;
  73.  
  74. elsif (x_exponent=0 or y_exponent=0) then
  75. -- 0*x or x*0
  76. z_exponent := (others => '0');
  77. z_mantissa := (others => '0');
  78. z_sign := '0';
  79. else
  80.  
  81. aux2 := ('1' & x_mantissa) * ('1' & y_mantissa);
  82. -- args in Q23 result in Q46
  83. if (aux2(47)='1') then
  84. -- >=2, shift left and add one to exponent
  85. z_mantissa := aux2(46 downto 24) + aux2(23); -- with rounding
  86. aux := '1';
  87. else
  88. z_mantissa := aux2(45 downto 23) + aux2(22); -- with rounding
  89. aux := '0';
  90. end if;
  91.  
  92. -- calculate exponent
  93. exponent_sum := ('0' & x_exponent) + ('0' & y_exponent) + aux - 127;
  94.  
  95. if (exponent_sum(8)='1') then
  96. if (exponent_sum(7)='0') then -- overflow
  97. z_exponent := "11111111";
  98. z_mantissa := (others => '0');
  99. z_sign := x_sign xor y_sign;
  100. else -- underflow
  101. z_exponent := (others => '0');
  102. z_mantissa := (others => '0');
  103. z_sign := '0';
  104. end if;
  105. else -- Ok
  106. z_exponent := exponent_sum(7 downto 0);
  107. z_sign := x_sign xor y_sign;
  108. end if;
  109. end if;
  110.  
  111.  
  112. z(22 downto 0) <= z_mantissa;
  113. z(30 downto 23) <= z_exponent;
  114. z(31) <= z_sign;
  115.  
  116. end process;
  117. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement