1 -- Floating-point unit for Microwatt
4 use ieee.std_logic_1164.all;
5 use ieee.numeric_std.all;
8 use work.insn_helpers.all;
9 use work.decode_types.all;
10 use work.crhelpers.all;
19 e_in : in Execute1toFPUType;
20 e_out : out FPUToExecute1Type;
22 w_out : out FPUToWritebackType
26 architecture behaviour of fpu is
27 type fp_number_class is (ZERO, FINITE, INFINITY, NAN);
29 constant EXP_BITS : natural := 13;
31 type fpu_reg_type is record
32 class : fp_number_class;
33 negative : std_ulogic;
34 exponent : signed(EXP_BITS-1 downto 0); -- unbiased
35 mantissa : std_ulogic_vector(63 downto 0); -- 10.54 format
38 type state_t is (IDLE,
39 DO_MCRFS, DO_MTFSB, DO_MTFSFI, DO_MFFS, DO_MTFSF,
45 ADD_SHIFT, ADD_2, ADD_3,
46 INT_SHIFT, INT_ROUND, INT_ISHIFT,
47 INT_FINAL, INT_CHECK, INT_OFLOW,
49 ROUND_UFLOW, ROUND_OFLOW,
50 ROUNDING, ROUNDING_2, ROUNDING_3,
53 type reg_type is record
56 instr_done : std_ulogic;
59 insn : std_ulogic_vector(31 downto 0);
60 dest_fpr : gspr_index_t;
64 single_prec : std_ulogic;
65 fpscr : std_ulogic_vector(31 downto 0);
68 r : std_ulogic_vector(63 downto 0); -- 10.54 format
70 result_sign : std_ulogic;
71 result_class : fp_number_class;
72 result_exp : signed(EXP_BITS-1 downto 0);
73 shift : signed(EXP_BITS-1 downto 0);
74 writing_back : std_ulogic;
75 int_result : std_ulogic;
76 cr_result : std_ulogic_vector(3 downto 0);
77 cr_mask : std_ulogic_vector(7 downto 0);
78 old_exc : std_ulogic_vector(4 downto 0);
79 update_fprf : std_ulogic;
80 quieten_nan : std_ulogic;
83 round_mode : std_ulogic_vector(2 downto 0);
84 is_subtract : std_ulogic;
86 add_bsmall : std_ulogic;
89 signal r, rin : reg_type;
91 signal fp_result : std_ulogic_vector(63 downto 0);
92 signal opsel_a : std_ulogic_vector(1 downto 0);
93 signal opsel_b : std_ulogic_vector(1 downto 0);
94 signal opsel_r : std_ulogic_vector(1 downto 0);
95 signal opsel_ainv : std_ulogic;
96 signal opsel_amask : std_ulogic;
97 signal opsel_binv : std_ulogic;
98 signal in_a : std_ulogic_vector(63 downto 0);
99 signal in_b : std_ulogic_vector(63 downto 0);
100 signal result : std_ulogic_vector(63 downto 0);
101 signal carry_in : std_ulogic;
102 signal lost_bits : std_ulogic;
103 signal r_hi_nz : std_ulogic;
104 signal r_lo_nz : std_ulogic;
105 signal misc_sel : std_ulogic_vector(3 downto 0);
108 constant AIN_R : std_ulogic_vector(1 downto 0) := "00";
109 constant AIN_A : std_ulogic_vector(1 downto 0) := "01";
110 constant AIN_B : std_ulogic_vector(1 downto 0) := "10";
112 constant BIN_ZERO : std_ulogic_vector(1 downto 0) := "00";
113 constant BIN_R : std_ulogic_vector(1 downto 0) := "01";
114 constant BIN_MASK : std_ulogic_vector(1 downto 0) := "10";
116 constant RES_SUM : std_ulogic_vector(1 downto 0) := "00";
117 constant RES_SHIFT : std_ulogic_vector(1 downto 0) := "01";
118 constant RES_MISC : std_ulogic_vector(1 downto 0) := "11";
120 -- Left and right shifter with 120 bit input and 64 bit output.
121 -- Shifts inp left by shift bits and returns the upper 64 bits of
122 -- the result. The shift parameter is interpreted as a signed
123 -- number in the range -64..63, with negative values indicating
125 function shifter_64(inp: std_ulogic_vector(119 downto 0);
126 shift: std_ulogic_vector(6 downto 0))
127 return std_ulogic_vector is
128 variable s1 : std_ulogic_vector(94 downto 0);
129 variable s2 : std_ulogic_vector(70 downto 0);
130 variable result : std_ulogic_vector(63 downto 0);
132 case shift(6 downto 5) is
134 s1 := inp(119 downto 25);
136 s1 := inp(87 downto 0) & "0000000";
138 s1 := x"0000000000000000" & inp(119 downto 89);
140 s1 := x"00000000" & inp(119 downto 57);
142 case shift(4 downto 3) is
144 s2 := s1(94 downto 24);
146 s2 := s1(86 downto 16);
148 s2 := s1(78 downto 8);
150 s2 := s1(70 downto 0);
152 case shift(2 downto 0) is
154 result := s2(70 downto 7);
156 result := s2(69 downto 6);
158 result := s2(68 downto 5);
160 result := s2(67 downto 4);
162 result := s2(66 downto 3);
164 result := s2(65 downto 2);
166 result := s2(64 downto 1);
168 result := s2(63 downto 0);
173 -- Generate a mask with 0-bits on the left and 1-bits on the right which
174 -- selects the bits will be lost in doing a right shift. The shift
175 -- parameter is the bottom 6 bits of a negative shift count,
176 -- indicating a right shift.
177 function right_mask(shift: unsigned(5 downto 0)) return std_ulogic_vector is
178 variable result: std_ulogic_vector(63 downto 0);
180 result := (others => '0');
181 for i in 0 to 63 loop
183 result(63 - i) := '1';
189 -- Split a DP floating-point number into components and work out its class.
190 -- If is_int = 1, the input is considered an integer
191 function decode_dp(fpr: std_ulogic_vector(63 downto 0); is_int: std_ulogic) return fpu_reg_type is
192 variable r : fpu_reg_type;
193 variable exp_nz : std_ulogic;
194 variable exp_ao : std_ulogic;
195 variable frac_nz : std_ulogic;
196 variable cls : std_ulogic_vector(2 downto 0);
198 r.negative := fpr(63);
199 exp_nz := or (fpr(62 downto 52));
200 exp_ao := and (fpr(62 downto 52));
201 frac_nz := or (fpr(51 downto 0));
203 r.exponent := signed(resize(unsigned(fpr(62 downto 52)), EXP_BITS)) - to_signed(1023, EXP_BITS);
205 r.exponent := to_signed(-1022, EXP_BITS);
207 r.mantissa := "000000000" & exp_nz & fpr(51 downto 0) & "00";
208 cls := exp_ao & exp_nz & frac_nz;
210 when "000" => r.class := ZERO;
211 when "001" => r.class := FINITE; -- denormalized
212 when "010" => r.class := FINITE;
213 when "011" => r.class := FINITE;
214 when "110" => r.class := INFINITY;
215 when others => r.class := NAN;
219 r.exponent := (others => '0');
220 if (fpr(63) or exp_nz or frac_nz) = '1' then
229 -- Construct a DP floating-point result from components
230 function pack_dp(sign: std_ulogic; class: fp_number_class; exp: signed(EXP_BITS-1 downto 0);
231 mantissa: std_ulogic_vector; single_prec: std_ulogic; quieten_nan: std_ulogic)
232 return std_ulogic_vector is
233 variable result : std_ulogic_vector(63 downto 0);
235 result := (others => '0');
240 if mantissa(54) = '1' then
242 result(62 downto 52) := std_ulogic_vector(resize(exp, 11) + 1023);
244 result(51 downto 29) := mantissa(53 downto 31);
245 if single_prec = '0' then
246 result(28 downto 0) := mantissa(30 downto 2);
249 result(62 downto 52) := "11111111111";
251 result(62 downto 52) := "11111111111";
252 result(51) := quieten_nan or mantissa(53);
253 result(50 downto 29) := mantissa(52 downto 31);
254 if single_prec = '0' then
255 result(28 downto 0) := mantissa(30 downto 2);
261 -- Determine whether to increment when rounding
262 -- Returns rounding_inc & inexact
263 -- Assumes x includes the bottom 29 bits of the mantissa already
264 -- if single_prec = 1 (usually arranged by setting set_x = 1 earlier).
265 function fp_rounding(mantissa: std_ulogic_vector(63 downto 0); x: std_ulogic;
266 single_prec: std_ulogic; rn: std_ulogic_vector(2 downto 0);
268 return std_ulogic_vector is
269 variable grx : std_ulogic_vector(2 downto 0);
270 variable ret : std_ulogic_vector(1 downto 0);
271 variable lsb : std_ulogic;
273 if single_prec = '0' then
274 grx := mantissa(1 downto 0) & x;
277 grx := mantissa(30 downto 29) & x;
282 case rn(1 downto 0) is
283 when "00" => -- round to nearest
284 if grx = "100" and rn(2) = '0' then
285 ret(1) := lsb; -- tie, round to even
289 when "01" => -- round towards zero
290 when others => -- round towards +/- inf
292 -- round towards greater magnitude
299 -- Determine result flags to write into the FPSCR
300 function result_flags(sign: std_ulogic; class: fp_number_class; unitbit: std_ulogic)
301 return std_ulogic_vector is
305 return sign & "0010";
307 return (not unitbit) & sign & (not sign) & "00";
309 return '0' & sign & (not sign) & "01";
318 if rising_edge(clk) then
324 r.fpscr <= (others => '0');
325 r.writing_back <= '0';
327 assert not (r.state /= IDLE and e_in.valid = '1') severity failure;
333 e_out.busy <= r.busy;
334 e_out.exception <= r.fpscr(FPSCR_FEX);
335 e_out.interrupt <= r.do_intr;
337 w_out.valid <= r.instr_done and not r.do_intr;
338 w_out.write_enable <= r.writing_back;
339 w_out.write_reg <= r.dest_fpr;
340 w_out.write_data <= fp_result;
341 w_out.write_cr_enable <= r.instr_done and (r.rc or r.is_cmp);
342 w_out.write_cr_mask <= r.cr_mask;
343 w_out.write_cr_data <= r.cr_result & r.cr_result & r.cr_result & r.cr_result &
344 r.cr_result & r.cr_result & r.cr_result & r.cr_result;
347 variable v : reg_type;
348 variable adec : fpu_reg_type;
349 variable bdec : fpu_reg_type;
350 variable fpscr_mask : std_ulogic_vector(31 downto 0);
351 variable illegal : std_ulogic;
352 variable j, k : integer;
353 variable flm : std_ulogic_vector(7 downto 0);
354 variable int_input : std_ulogic;
355 variable mask : std_ulogic_vector(63 downto 0);
356 variable in_a0 : std_ulogic_vector(63 downto 0);
357 variable in_b0 : std_ulogic_vector(63 downto 0);
358 variable misc : std_ulogic_vector(63 downto 0);
359 variable shift_res : std_ulogic_vector(63 downto 0);
360 variable round : std_ulogic_vector(1 downto 0);
361 variable update_fx : std_ulogic;
362 variable arith_done : std_ulogic;
363 variable invalid : std_ulogic;
364 variable mant_nz : std_ulogic;
365 variable min_exp : signed(EXP_BITS-1 downto 0);
366 variable max_exp : signed(EXP_BITS-1 downto 0);
367 variable bias_exp : signed(EXP_BITS-1 downto 0);
368 variable new_exp : signed(EXP_BITS-1 downto 0);
369 variable exp_tiny : std_ulogic;
370 variable exp_huge : std_ulogic;
371 variable renormalize : std_ulogic;
372 variable clz : std_ulogic_vector(5 downto 0);
373 variable set_x : std_ulogic;
374 variable mshift : signed(EXP_BITS-1 downto 0);
375 variable need_check : std_ulogic;
376 variable msb : std_ulogic;
377 variable is_add : std_ulogic;
378 variable qnan_result : std_ulogic;
379 variable longmask : std_ulogic;
386 -- capture incoming instruction
387 if e_in.valid = '1' then
390 v.fe_mode := or (e_in.fe_mode);
391 v.dest_fpr := e_in.frt;
392 v.single_prec := e_in.single;
395 v.is_cmp := e_in.out_cr;
396 if e_in.out_cr = '0' then
397 v.cr_mask := num_to_fxm(1);
399 v.cr_mask := num_to_fxm(to_integer(unsigned(insn_bf(e_in.insn))));
402 if e_in.op = OP_FPOP_I then
405 v.quieten_nan := '1';
408 v.round_mode := '0' & r.fpscr(FPSCR_RN+1 downto FPSCR_RN);
409 v.is_subtract := '0';
411 adec := decode_dp(e_in.fra, int_input);
412 bdec := decode_dp(e_in.frb, int_input);
416 if adec.exponent > bdec.exponent then
421 r_hi_nz <= or (r.r(55 downto 31));
422 r_lo_nz <= or (r.r(30 downto 2));
424 if r.single_prec = '0' then
425 max_exp := to_signed(1023, EXP_BITS);
426 min_exp := to_signed(-1022, EXP_BITS);
427 bias_exp := to_signed(1536, EXP_BITS);
429 max_exp := to_signed(127, EXP_BITS);
430 min_exp := to_signed(-126, EXP_BITS);
431 bias_exp := to_signed(192, EXP_BITS);
433 new_exp := r.result_exp - r.shift;
436 if new_exp < min_exp then
439 if new_exp > max_exp then
443 v.writing_back := '0';
445 v.update_fprf := '0';
446 v.shift := to_signed(0, EXP_BITS);
455 fpscr_mask := (others => '1');
462 longmask := r.single_prec;
466 if e_in.valid = '1' then
467 case e_in.insn(5 downto 1) is
471 if e_in.insn(10) = '0' then
472 if e_in.insn(8) = '0' then
475 v.state := DO_MTFSFI;
481 if e_in.insn(8) = '0' then
487 if e_in.insn(9 downto 8) /= "11" then
495 if int_input = '1' then
502 v.round_mode := "001";
504 when "10100" | "10101" =>
511 v.old_exc := r.fpscr(FPSCR_VX downto FPSCR_XX);
514 j := to_integer(unsigned(insn_bfa(r.insn)));
518 v.cr_result := r.fpscr(k + 3 downto k);
519 fpscr_mask(k + 3 downto k) := "0000";
522 v.fpscr := r.fpscr and (fpscr_mask or x"6007F8FF");
528 j := to_integer(unsigned(insn_bt(r.insn)));
529 for i in 0 to 31 loop
531 v.fpscr(31 - i) := r.insn(6);
539 j := to_integer(unsigned(insn_bf(r.insn)));
540 if r.insn(16) = '0' then
544 v.fpscr(k + 3 downto k) := insn_u(r.insn);
554 misc_sel <= "01" & r.insn(8) & '0';
556 v.writing_back := '1';
562 v.writing_back := '1';
564 case r.insn(20 downto 16) is
569 v.fpscr(FPSCR_VE downto FPSCR_XE) := "00000";
570 when "10100" | "10101" =>
571 -- mffscdrn[i] (but we don't implement DRN)
572 fpscr_mask := x"000000FF";
575 fpscr_mask := x"000000FF";
576 v.fpscr(FPSCR_RN+1 downto FPSCR_RN) :=
577 r.b.mantissa(FPSCR_RN+1 downto FPSCR_RN);
580 fpscr_mask := x"000000FF";
581 v.fpscr(FPSCR_RN+1 downto FPSCR_RN) := r.insn(12 downto 11);
584 fpscr_mask := x"0007F0FF";
592 if r.insn(25) = '1' then
594 elsif r.insn(16) = '1' then
597 flm := r.insn(24 downto 17);
602 v.fpscr(k + 3 downto k) := r.b.mantissa(k + 3 downto k);
610 v.result_class := r.b.class;
611 v.result_exp := r.b.exponent;
612 v.quieten_nan := '0';
613 if r.insn(9) = '1' then
614 v.result_sign := '0'; -- fabs
615 elsif r.insn(8) = '1' then
616 v.result_sign := '1'; -- fnabs
617 elsif r.insn(7) = '1' then
618 v.result_sign := r.b.negative; -- fmr
619 elsif r.insn(6) = '1' then
620 v.result_sign := not r.b.negative; -- fneg
622 v.result_sign := r.a.negative; -- fcpsgn
624 v.writing_back := '1';
628 when DO_FRI => -- fri[nzpm]
630 v.result_class := r.b.class;
631 v.result_sign := r.b.negative;
632 v.result_exp := r.b.exponent;
633 v.fpscr(FPSCR_FR) := '0';
634 v.fpscr(FPSCR_FI) := '0';
635 if r.b.class = NAN and r.b.mantissa(53) = '0' then
637 v.fpscr(FPSCR_VXSNAN) := '1';
640 if r.b.class = FINITE then
641 if r.b.exponent >= to_signed(52, EXP_BITS) then
642 -- integer already, no rounding required
645 v.shift := r.b.exponent - to_signed(52, EXP_BITS);
647 v.round_mode := '1' & r.insn(7 downto 6);
655 v.result_class := r.b.class;
656 v.result_sign := r.b.negative;
657 v.result_exp := r.b.exponent;
658 v.fpscr(FPSCR_FR) := '0';
659 v.fpscr(FPSCR_FI) := '0';
660 if r.b.class = NAN and r.b.mantissa(53) = '0' then
662 v.fpscr(FPSCR_VXSNAN) := '1';
666 if r.b.class = FINITE then
667 if r.b.exponent < to_signed(-126, EXP_BITS) then
668 v.shift := r.b.exponent - to_signed(-126, EXP_BITS);
669 v.state := ROUND_UFLOW;
670 elsif r.b.exponent > to_signed(127, EXP_BITS) then
671 v.state := ROUND_OFLOW;
673 v.shift := to_signed(-2, EXP_BITS);
681 -- instr bit 9: 1=dword 0=word
682 -- instr bit 8: 1=unsigned 0=signed
683 -- instr bit 1: 1=round to zero 0=use fpscr[RN]
685 v.result_class := r.b.class;
686 v.result_sign := r.b.negative;
687 v.result_exp := r.b.exponent;
688 v.fpscr(FPSCR_FR) := '0';
689 v.fpscr(FPSCR_FI) := '0';
690 if r.b.class = NAN and r.b.mantissa(53) = '0' then
692 v.fpscr(FPSCR_VXSNAN) := '1';
701 if r.b.exponent >= to_signed(64, EXP_BITS) or
702 (r.insn(9) = '0' and r.b.exponent >= to_signed(32, EXP_BITS)) then
703 v.state := INT_OFLOW;
704 elsif r.b.exponent >= to_signed(52, EXP_BITS) then
705 -- integer already, no rounding required,
706 -- shift into final position
707 v.shift := r.b.exponent - to_signed(54, EXP_BITS);
708 if r.insn(8) = '1' and r.b.negative = '1' then
709 v.state := INT_OFLOW;
711 v.state := INT_ISHIFT;
714 v.shift := r.b.exponent - to_signed(52, EXP_BITS);
715 v.state := INT_SHIFT;
717 when INFINITY | NAN =>
718 v.state := INT_OFLOW;
722 v.result_sign := '0';
724 if r.insn(8) = '0' and r.b.negative = '1' then
725 -- fcfid[s] with negative operand, set R = -B
728 v.result_sign := '1';
730 v.result_class := r.b.class;
731 v.result_exp := to_signed(54, EXP_BITS);
732 v.fpscr(FPSCR_FR) := '0';
733 v.fpscr(FPSCR_FI) := '0';
734 if r.b.class = ZERO then
741 -- fadd[s] and fsub[s]
743 v.result_sign := r.a.negative;
744 v.result_class := r.a.class;
745 v.result_exp := r.a.exponent;
746 v.fpscr(FPSCR_FR) := '0';
747 v.fpscr(FPSCR_FI) := '0';
748 is_add := r.a.negative xor r.b.negative xor r.insn(1);
749 if r.a.class = FINITE and r.b.class = FINITE then
750 v.is_subtract := not is_add;
751 v.add_bsmall := r.exp_cmp;
752 if r.exp_cmp = '0' then
753 v.shift := r.a.exponent - r.b.exponent;
754 v.result_sign := r.b.negative xnor r.insn(1);
755 if r.a.exponent = r.b.exponent then
758 v.state := ADD_SHIFT;
762 v.shift := r.b.exponent - r.a.exponent;
763 v.result_exp := r.b.exponent;
764 v.state := ADD_SHIFT;
767 if (r.a.class = NAN and r.a.mantissa(53) = '0') or
768 (r.b.class = NAN and r.b.mantissa(53) = '0') then
770 v.fpscr(FPSCR_VXSNAN) := '1';
773 if r.a.class = NAN then
774 -- nothing to do, result is A
775 elsif r.b.class = NAN then
776 v.result_class := NAN;
777 v.result_sign := r.b.negative;
779 elsif r.a.class = INFINITY and r.b.class = INFINITY and is_add = '0' then
780 -- invalid operation, construct QNaN
781 v.fpscr(FPSCR_VXISI) := '1';
783 elsif r.a.class = ZERO and r.b.class = ZERO and is_add = '0' then
784 -- return -0 for rounding to -infinity
785 v.result_sign := r.round_mode(1) and r.round_mode(0);
786 elsif r.a.class = INFINITY or r.b.class = ZERO then
787 -- nothing to do, result is A
790 v.result_sign := r.b.negative xnor r.insn(1);
791 v.result_class := r.b.class;
792 v.result_exp := r.b.exponent;
799 opsel_r <= RES_SHIFT;
805 if r.add_bsmall = '1' then
811 opsel_binv <= r.is_subtract;
812 carry_in <= r.is_subtract and not r.x;
813 v.shift := to_signed(-1, EXP_BITS);
817 -- check for overflow or negative result (can't get both)
818 if r.r(63) = '1' then
819 -- result is opposite sign to expected
820 v.result_sign := not r.result_sign;
824 elsif r.r(55) = '1' then
825 -- sum overflowed, shift right
826 opsel_r <= RES_SHIFT;
828 v.shift := to_signed(-2, EXP_BITS);
829 if exp_huge = '1' then
830 v.state := ROUND_OFLOW;
834 elsif r.r(54) = '1' then
836 v.shift := to_signed(-2, EXP_BITS);
838 elsif (r_hi_nz or r_lo_nz or r.r(1) or r.r(0)) = '0' then
839 -- r.x must be zero at this point
840 v.result_class := ZERO;
841 if r.is_subtract = '1' then
842 -- set result sign depending on rounding mode
843 v.result_sign := r.round_mode(1) and r.round_mode(0);
848 v.state := NORMALIZE;
852 opsel_r <= RES_SHIFT;
854 v.state := INT_ROUND;
855 v.shift := to_signed(-2, EXP_BITS);
858 opsel_r <= RES_SHIFT;
859 round := fp_rounding(r.r, r.x, '0', r.round_mode, r.result_sign);
860 v.fpscr(FPSCR_FR downto FPSCR_FI) := round;
861 -- Check for negative values that don't round to 0 for fcti*u*
862 if r.insn(8) = '1' and r.result_sign = '1' and
863 (r_hi_nz or r_lo_nz or v.fpscr(FPSCR_FR)) = '1' then
864 v.state := INT_OFLOW;
866 v.state := INT_FINAL;
870 opsel_r <= RES_SHIFT;
871 v.state := INT_FINAL;
874 -- Negate if necessary, and increment for rounding if needed
875 opsel_ainv <= r.result_sign;
876 carry_in <= r.fpscr(FPSCR_FR) xor r.result_sign;
877 -- Check for possible overflows
878 case r.insn(9 downto 8) is
879 when "00" => -- fctiw[z]
880 need_check := r.r(31) or (r.r(30) and not r.result_sign);
881 when "01" => -- fctiwu[z]
882 need_check := r.r(31);
883 when "10" => -- fctid[z]
884 need_check := r.r(63) or (r.r(62) and not r.result_sign);
885 when others => -- fctidu[z]
886 need_check := r.r(63);
888 if need_check = '1' then
889 v.state := INT_CHECK;
891 if r.fpscr(FPSCR_FI) = '1' then
892 v.fpscr(FPSCR_XX) := '1';
898 if r.insn(9) = '0' then
903 misc_sel <= '1' & r.insn(9 downto 8) & r.result_sign;
904 if (r.insn(8) = '0' and msb /= r.result_sign) or
905 (r.insn(8) = '1' and msb /= '1') then
907 v.fpscr(FPSCR_VXCVI) := '1';
910 if r.fpscr(FPSCR_FI) = '1' then
911 v.fpscr(FPSCR_XX) := '1';
918 misc_sel <= '1' & r.insn(9 downto 8) & r.result_sign;
919 if r.b.class = NAN then
922 v.fpscr(FPSCR_VXCVI) := '1';
927 opsel_r <= RES_SHIFT;
929 v.shift := to_signed(-2, EXP_BITS);
933 if r.r(63 downto 54) /= "0000000001" then
935 v.state := NORMALIZE;
938 if exp_tiny = '1' then
939 v.shift := new_exp - min_exp;
940 v.state := ROUND_UFLOW;
941 elsif exp_huge = '1' then
942 v.state := ROUND_OFLOW;
944 v.shift := to_signed(-2, EXP_BITS);
950 -- Shift so we have 9 leading zeroes (we know R is non-zero)
951 opsel_r <= RES_SHIFT;
953 if exp_tiny = '1' then
954 v.shift := new_exp - min_exp;
955 v.state := ROUND_UFLOW;
956 elsif exp_huge = '1' then
957 v.state := ROUND_OFLOW;
959 v.shift := to_signed(-2, EXP_BITS);
965 if r.fpscr(FPSCR_UE) = '0' then
966 -- disabled underflow exception case
967 -- have to denormalize before rounding
968 opsel_r <= RES_SHIFT;
970 v.shift := to_signed(-2, EXP_BITS);
973 -- enabled underflow exception case
974 -- if denormalized, have to normalize before rounding
975 v.fpscr(FPSCR_UX) := '1';
976 v.result_exp := r.result_exp + bias_exp;
977 if r.r(54) = '0' then
979 v.state := NORMALIZE;
981 v.shift := to_signed(-2, EXP_BITS);
987 v.fpscr(FPSCR_OX) := '1';
988 if r.fpscr(FPSCR_OE) = '0' then
989 -- disabled overflow exception
990 -- result depends on rounding mode
991 v.fpscr(FPSCR_XX) := '1';
992 v.fpscr(FPSCR_FI) := '1';
993 if r.round_mode(1 downto 0) = "00" or
994 (r.round_mode(1) = '1' and r.round_mode(0) = r.result_sign) then
995 v.result_class := INFINITY;
996 v.fpscr(FPSCR_FR) := '1';
998 v.fpscr(FPSCR_FR) := '0';
1000 -- construct largest representable number
1001 v.result_exp := max_exp;
1002 opsel_r <= RES_MISC;
1003 misc_sel <= "001" & r.single_prec;
1006 -- enabled overflow exception
1007 v.result_exp := r.result_exp - bias_exp;
1008 v.shift := to_signed(-2, EXP_BITS);
1009 v.state := ROUNDING;
1014 round := fp_rounding(r.r, r.x, r.single_prec, r.round_mode, r.result_sign);
1015 v.fpscr(FPSCR_FR downto FPSCR_FI) := round;
1016 if round(1) = '1' then
1017 -- set mask to increment the LSB for the precision
1018 opsel_b <= BIN_MASK;
1020 v.shift := to_signed(-1, EXP_BITS);
1021 v.state := ROUNDING_2;
1023 if r.r(54) = '0' then
1024 -- result after masking could be zero, or could be a
1025 -- denormalized result that needs to be renormalized
1027 v.state := ROUNDING_3;
1032 if round(0) = '1' then
1033 v.fpscr(FPSCR_XX) := '1';
1034 if r.tiny = '1' then
1035 v.fpscr(FPSCR_UX) := '1';
1040 -- Check for overflow during rounding
1042 if r.r(55) = '1' then
1043 opsel_r <= RES_SHIFT;
1044 if exp_huge = '1' then
1045 v.state := ROUND_OFLOW;
1049 elsif r.r(54) = '0' then
1050 -- Do CLZ so we can renormalize the result
1052 v.state := ROUNDING_3;
1058 mant_nz := r_hi_nz or (r_lo_nz and not r.single_prec);
1059 if mant_nz = '0' then
1060 v.result_class := ZERO;
1061 if r.is_subtract = '1' then
1062 -- set result sign depending on rounding mode
1063 v.result_sign := r.round_mode(1) and r.round_mode(0);
1067 -- Renormalize result after rounding
1068 opsel_r <= RES_SHIFT;
1069 v.denorm := exp_tiny;
1070 v.shift := new_exp - to_signed(-1022, EXP_BITS);
1071 if new_exp < to_signed(-1022, EXP_BITS) then
1079 opsel_r <= RES_SHIFT;
1084 if qnan_result = '1' then
1086 v.result_class := NAN;
1087 v.result_sign := '0';
1089 opsel_r <= RES_MISC;
1091 if arith_done = '1' then
1092 -- Enabled invalid exception doesn't write result or FPRF
1093 if (invalid and r.fpscr(FPSCR_VE)) = '0' then
1094 v.writing_back := '1';
1095 v.update_fprf := '1';
1097 v.instr_done := '1';
1103 -- This has A and B input multiplexers, an adder, a shifter,
1104 -- count-leading-zeroes logic, and a result mux.
1105 if longmask = '1' then
1106 mshift := r.shift + to_signed(-29, EXP_BITS);
1110 if mshift < to_signed(-64, EXP_BITS) then
1111 mask := (others => '1');
1112 elsif mshift >= to_signed(0, EXP_BITS) then
1113 mask := (others => '0');
1115 mask := right_mask(unsigned(mshift(5 downto 0)));
1121 in_a0 := r.a.mantissa;
1123 in_a0 := r.b.mantissa;
1125 if (or (mask and in_a0)) = '1' and set_x = '1' then
1128 if opsel_ainv = '1' then
1131 if opsel_amask = '1' then
1132 in_a0 := in_a0 and not mask;
1137 in_b0 := (others => '0');
1143 in_b0 := (others => '0');
1145 if opsel_binv = '1' then
1149 if r.shift >= to_signed(-64, EXP_BITS) and r.shift <= to_signed(63, EXP_BITS) then
1150 shift_res := shifter_64(r.r & x"00000000000000",
1151 std_ulogic_vector(r.shift(6 downto 0)));
1153 shift_res := (others => '0');
1157 result <= std_ulogic_vector(unsigned(in_a) + unsigned(in_b) + carry_in);
1159 result <= shift_res;
1163 misc := x"00000000" & (r.fpscr and fpscr_mask);
1165 -- generated QNaN mantissa
1166 misc := x"0020000000000000";
1168 -- mantissa of max representable DP number
1169 misc := x"007ffffffffffffc";
1171 -- mantissa of max representable SP number
1172 misc := x"007fffff80000000";
1175 misc := r.a.mantissa(31 downto 0) & r.b.mantissa(31 downto 0);
1178 misc := r.a.mantissa(63 downto 32) & r.b.mantissa(63 downto 32);
1180 -- max positive result for fctiw[z]
1181 misc := x"000000007fffffff";
1183 -- max negative result for fctiw[z]
1184 misc := x"ffffffff80000000";
1186 -- max positive result for fctiwu[z]
1187 misc := x"00000000ffffffff";
1189 -- max negative result for fctiwu[z]
1190 misc := x"0000000000000000";
1192 -- max positive result for fctid[z]
1193 misc := x"7fffffffffffffff";
1195 -- max negative result for fctid[z]
1196 misc := x"8000000000000000";
1198 -- max positive result for fctidu[z]
1199 misc := x"ffffffffffffffff";
1201 -- max negative result for fctidu[z]
1202 misc := x"0000000000000000";
1204 misc := x"0000000000000000";
1210 if opsel_r = RES_SHIFT then
1211 v.result_exp := new_exp;
1214 if renormalize = '1' then
1215 clz := count_left_zeroes(r.r);
1216 v.shift := resize(signed('0' & clz) - 9, EXP_BITS);
1219 if r.int_result = '1' then
1222 fp_result <= pack_dp(r.result_sign, r.result_class, r.result_exp, r.r,
1223 r.single_prec, r.quieten_nan);
1225 if r.update_fprf = '1' then
1226 v.fpscr(FPSCR_C downto FPSCR_FU) := result_flags(r.result_sign, r.result_class,
1227 r.r(54) and not r.denorm);
1230 v.fpscr(FPSCR_VX) := (or (v.fpscr(FPSCR_VXSNAN downto FPSCR_VXVC))) or
1231 (or (v.fpscr(FPSCR_VXSOFT downto FPSCR_VXCVI)));
1232 v.fpscr(FPSCR_FEX) := or (v.fpscr(FPSCR_VX downto FPSCR_XX) and
1233 v.fpscr(FPSCR_VE downto FPSCR_XE));
1234 if update_fx = '1' and
1235 (v.fpscr(FPSCR_VX downto FPSCR_XX) and not r.old_exc) /= "00000" then
1236 v.fpscr(FPSCR_FX) := '1';
1239 v.cr_result := v.fpscr(FPSCR_FX downto FPSCR_OX);
1242 if illegal = '1' then
1243 v.instr_done := '0';
1245 v.writing_back := '0';
1249 v.do_intr := v.instr_done and v.fpscr(FPSCR_FEX) and r.fe_mode;
1250 if v.state /= IDLE or v.do_intr = '1' then
1256 e_out.illegal <= illegal;
1259 end architecture behaviour;