3 Email : g.vinod1993@gmail.com
4 Last Update : 27th November 2017
5 See LICENSE for more details
11 import defined_types ::*;
12 import UniqueWrappers::*;
13 `include "defined_parameters.bsv"
14 interface Ifc_fpu_int_to_sp;
15 method ActionValue#(Floating_output#(32)) _start(Bit#(64) inp_int, Bit#(1) unsigned_bit, Bit#(1) long, Bit#(3) rounding_mode);
19 function Bit#(37) roundFunc(Bit#(n) unrounded, Bit#(8) expo, Bit#(3) rounding_mode)
23 let nInd = valueOf(n);
24 bit guard = unrounded[nInd-25];
25 bit round = unrounded[nInd-26];
27 bit sign = unrounded[nInd-1];
28 Bit#(8) local_expo = expo;
29 Bit#(TSub#(n,26)) sticky_check = unrounded[nInd-27:0];
30 if(sticky_check != '0)
32 bit inexact = (guard | round | sticky);
34 Bit#(25) lv_man = {2'b0,unrounded[nInd-2:nInd-24]};
35 if(rounding_mode == 'b000)
36 lv_roundup = guard & (unrounded[nInd-24] | round | sticky);
37 else if (rounding_mode == 'b100)
38 lv_roundup = guard; //& (round | sticky | ~sign);
39 else if (rounding_mode == 'b011)
40 lv_roundup = (guard | round | sticky) & (~sign);
41 else if (rounding_mode == 'b010)
42 lv_roundup = (guard | round | sticky) & (sign);
45 if(lv_man[23] == 1) begin
46 local_expo = local_expo + 1;
48 let fflags = {1'b0,1'b0,1'b0,1'b0,inexact};
49 return {fflags,sign,local_expo,lv_man[22:0]};
52 function Bit#(37) fcvt_s_w_l (Bit#(n) inp, Bit#(1) unsigned_bit, Bit#(3) rounding_mode)
57 Add#(c__, logN, TLog#(TAdd#(1, n)))
59 let nInd = valueOf(n);
60 Bool ubit = (unsigned_bit == 1);
61 Bit#(1) lv_sign = ubit? 0 : inp[nInd-1];
62 Bool sbit = (lv_sign == 1);
64 Bit#(8) expo = zeroExtend(bias) + fromInteger(nInd-1);
67 Bit#(logN) lv_zeros = truncate(pack(countZerosMSB(inp)));
68 inp = inp << lv_zeros;
69 expo = expo - zeroExtend(pack(lv_zeros));
70 Bit#(TSub#(n,1)) inpS = inp[nInd-2:0];
71 Bit#(n) inp_temp = {lv_sign,inpS};
72 Bit#(37) res = roundFunc(inp_temp, expo, rounding_mode);
77 `ifdef fpu_hierarchical
80 module mkfpu_int_to_sp(Ifc_fpu_int_to_sp);
83 Wrapper3#(Bit#(32), Bit#(1), Bit#(3),Bit#(37)) fcvt_s_wwu <- mkUniqueWrapper3(fcvt_s_w_l);
84 Wrapper3#(Bit#(64), Bit#(1), Bit#(3),Bit#(37)) fcvt_s_llu <- mkUniqueWrapper3(fcvt_s_w_l);
86 method ActionValue#(Floating_output#(32)) _start(Bit#(64) inp_int, Bit#(1) unsigned_bit, Bit#(1) long, Bit#(3) rounding_mode);
87 `ifdef verbose $display($time,"\tGiving inputs: %h unsigned %b long %b rounding %b", inp_int, unsigned_bit, long, rounding_mode); `endif
88 Floating_output#(32) wr_final_out=?;
89 if((inp_int == 0 && long==1) || (inp_int[31:0] == 0 && long == 0))
90 wr_final_out = Floating_output{ final_result : 32'b0,
93 else if(long == 0) begin
94 Bit#(32) inp32 = truncate(inp_int);
95 `ifdef verbose $display("inp_int : %b",inp32); `endif
96 Bit#(1) lv_sign = inp32[31];
97 if(unsigned_bit == 0) begin
98 if((inp32 & 'h7fffffff) == 0) begin
99 Bit#(32) res = lv_sign==1? {1'b1,8'h9e,'0} : '0;
100 wr_final_out = Floating_output{
106 Bit#(37) ressw <- fcvt_s_wwu.func(inp32,unsigned_bit,rounding_mode);
107 wr_final_out = Floating_output{
108 final_result : (ressw[31:0]),
109 fflags : ressw[36:32]
114 Bit#(37) res <- fcvt_s_wwu.func(inp32,unsigned_bit,rounding_mode);
115 wr_final_out = Floating_output{
116 final_result : (res[31:0]),
122 Bit#(37) res <- fcvt_s_llu.func(inp_int,unsigned_bit,rounding_mode);
123 wr_final_out = Floating_output {
124 final_result : res[31:0],
134 Reg#(Bit#(64)) rg_operand1<-mkReg(64'h039e781bab642be4);
135 //Reg#(Bit#(64)) rg_operand1<-mkReg(~(64'hfffffffffffff812)+1);
136 Reg#(Bit#(32)) rg_clock<-mkReg(0);
137 Ifc_fpu_int_to_sp itof <- mkfpu_int_to_sp();
138 Reg#(Bit#(32)) rg_arbit <-mkReg(0);
141 rg_clock<=rg_clock+1;
144 rule rl_start_1(rg_clock=='d0);
145 `ifdef verbose $display("Giving inputs rg_operand 1 : %h through testbench",rg_operand1,$time); `endif
146 let abc<-itof._start(zeroExtend(rg_operand1),1'b1,1'b0,3'b000);
147 `ifdef verbose $display("Final result= %h fflags= %h", abc.final_result, abc.fflags, $time); `endif