Line 1... |
Line 1... |
|
`timescale 1ns / 1ps
|
// ============================================================================
|
// ============================================================================
|
// __
|
// __
|
// \\__/ o\ (C) 2006-2016 Robert Finch, Stratford
|
// \\__/ o\ (C) 2006-2016 Robert Finch, Waterloo
|
// \ __ / All rights reserved.
|
// \ __ / All rights reserved.
|
// \/_// robfinch<remove>@finitron.ca
|
// \/_// robfinch<remove>@finitron.ca
|
// ||
|
// ||
|
//
|
//
|
|
// fpAddsub.v
|
|
// - floating point adder/subtracter
|
|
// - two cycle latency
|
|
// - can issue every clock cycle
|
|
// - parameterized width
|
|
// - IEEE 754 representation
|
|
//
|
|
//
|
// This source file is free software: you can redistribute it and/or modify
|
// This source file is free software: you can redistribute it and/or modify
|
// it under the terms of the GNU Lesser General Public License as published
|
// it under the terms of the GNU Lesser General Public License as published
|
// by the Free Software Foundation, either version 3 of the License, or
|
// by the Free Software Foundation, either version 3 of the License, or
|
// (at your option) any later version.
|
// (at your option) any later version.
|
//
|
//
|
Line 16... |
Line 25... |
// GNU General Public License for more details.
|
// GNU General Public License for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
//
|
//
|
// fpAddsub.v
|
|
// - floating point adder/subtracter
|
|
// - two cycle latency
|
|
// - can issue every clock cycle
|
|
// - parameterized width
|
|
// - IEEE 754 representation
|
|
//
|
|
// This adder/subtractor handles denormalized numbers.
|
|
// It has a two cycle latency.
|
|
// The output format is of an internal expanded representation
|
|
// in preparation to be fed into a normalization unit, then
|
|
// rounding. Basically, it's the same as the regular format
|
|
// except the mantissa is doubled in size, the leading two
|
|
// bits of which are assumed to be whole bits.
|
|
// ============================================================================
|
// ============================================================================
|
//
|
|
module fpAddsub(clk, ce, rm, op, a, b, o);
|
module fpAddsub(clk, ce, rm, op, a, b, o);
|
parameter WID = 32;
|
parameter WID = 128;
|
localparam MSB = WID-1;
|
localparam MSB = WID-1;
|
localparam EMSB = WID==80 ? 14 :
|
localparam EMSB = WID==128 ? 14 :
|
|
WID==96 ? 14 :
|
|
WID==80 ? 14 :
|
WID==64 ? 10 :
|
WID==64 ? 10 :
|
WID==52 ? 10 :
|
WID==52 ? 10 :
|
WID==48 ? 10 :
|
WID==48 ? 10 :
|
WID==44 ? 10 :
|
WID==44 ? 10 :
|
WID==42 ? 10 :
|
WID==42 ? 10 :
|
WID==40 ? 9 :
|
WID==40 ? 9 :
|
WID==32 ? 7 :
|
WID==32 ? 7 :
|
WID==24 ? 6 : 4;
|
WID==24 ? 6 : 4;
|
localparam FMSB = WID==80 ? 63 :
|
localparam FMSB = WID==128 ? 111 :
|
|
WID==96 ? 79 :
|
|
WID==80 ? 63 :
|
WID==64 ? 51 :
|
WID==64 ? 51 :
|
WID==52 ? 39 :
|
WID==52 ? 39 :
|
WID==48 ? 35 :
|
WID==48 ? 35 :
|
WID==44 ? 31 :
|
WID==44 ? 31 :
|
WID==42 ? 29 :
|
WID==42 ? 29 :
|
WID==40 ? 28 :
|
WID==40 ? 28 :
|
WID==32 ? 22 :
|
WID==32 ? 22 :
|
WID==24 ? 15 : 9;
|
WID==24 ? 15 : 9;
|
|
|
localparam WX = 3;
|
localparam FX = (FMSB+2)*2-1; // the MSB of the expanded fraction
|
localparam FX = (FMSB+1)*2-1; // the MSB of the expanded fraction
|
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
|
localparam EX = FX + WX + EMSB + 1;
|
|
|
|
input clk; // system clock
|
input clk; // system clock
|
input ce; // core clock enable
|
input ce; // core clock enable
|
input [2:0] rm; // rounding mode
|
input [2:0] rm; // rounding mode
|
input op; // operation 0 = add, 1 = subtract
|
input op; // operation 0 = add, 1 = subtract
|
input [WID-1:0] a; // operand a
|
input [WID-1:0] a; // operand a
|
input [WID-1:0] b; // operand b
|
input [WID-1:0] b; // operand b
|
output [EX+1:0] o; // output
|
output [EX:0] o; // output
|
|
|
|
|
// variables
|
// variables
|
wire so; // sign output
|
wire so; // sign output
|
wire [EMSB:0] xo; // de normalized exponent output
|
wire [EMSB:0] xo; // de normalized exponent output
|
reg [EMSB:0] xo1; // de normalized exponent output
|
reg [EMSB:0] xo1; // de normalized exponent output
|
wire [FX+WX:0] mo; // mantissa output
|
wire [FX:0] mo; // mantissa output
|
reg [FX+WX:0] mo1; // mantissa output
|
reg [FX:0] mo1; // mantissa output
|
|
|
// There's an extra bit output in the mantissa to allow for three whole
|
|
// digits which the normalizer uses.
|
|
assign o = {so,xo,mo};
|
assign o = {so,xo,mo};
|
|
|
// operands sign,exponent,mantissa
|
// operands sign,exponent,mantissa
|
wire sa, sb;
|
wire sa, sb;
|
wire [EMSB:0] xa, xb;
|
wire [EMSB:0] xa, xb;
|
Line 100... |
Line 96... |
wire aNan, bNan, aNan1, bNan1;
|
wire aNan, bNan, aNan1, bNan1;
|
|
|
wire [EMSB:0] xad = xa|adn; // operand a exponent, compensated for denormalized numbers
|
wire [EMSB:0] xad = xa|adn; // operand a exponent, compensated for denormalized numbers
|
wire [EMSB:0] xbd = xb|bdn; // operand b exponent, compensated for denormalized numbers
|
wire [EMSB:0] xbd = xb|bdn; // operand b exponent, compensated for denormalized numbers
|
|
|
fpDecompose #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .man(ma), .fract(fracta), .xz(adn), .vz(az), .xinf(xaInf), .inf(aInf), .nan(aNan) );
|
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .man(ma), .fract(fracta), .xz(adn), .vz(az), .xinf(xaInf), .inf(aInf), .nan(aNan) );
|
fpDecompose #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .man(mb), .fract(fractb), .xz(bdn), .vz(bz), .xinf(xbInf), .inf(bInf), .nan(bNan) );
|
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .man(mb), .fract(fractb), .xz(bdn), .vz(bz), .xinf(xbInf), .inf(bInf), .nan(bNan) );
|
|
|
// Figure out which operation is really needed an add or
|
// Figure out which operation is really needed an add or
|
// subtract ?
|
// subtract ?
|
// If the signs are the same, use the orignal op,
|
// If the signs are the same, use the orignal op,
|
// otherwise flip the operation
|
// otherwise flip the operation
|
Line 169... |
Line 165... |
wire [FMSB+1:0] mfs = xa_gt_xb ? fractb : fracta;
|
wire [FMSB+1:0] mfs = xa_gt_xb ? fractb : fracta;
|
wire [FMSB+1:0] mfs1;
|
wire [FMSB+1:0] mfs1;
|
|
|
// Determine the sticky bit
|
// Determine the sticky bit
|
wire sticky, sticky1;
|
wire sticky, sticky1;
|
|
generate
|
|
begin
|
|
if (WID==128)
|
|
redor128 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
|
|
else if (WID==96)
|
|
redor96 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
|
|
else if (WID==80)
|
|
redor80 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
|
|
else if (WID==64)
|
redor64 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
|
redor64 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
|
|
else if (WID==32)
|
|
redor32 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
|
|
end
|
|
endgenerate
|
|
|
// register inputs to shifter and shift
|
// register inputs to shifter and shift
|
delay1 #(1) d16(.clk(clk), .ce(ce), .i(sticky), .o(sticky1) );
|
delay1 #(1) d16(.clk(clk), .ce(ce), .i(sticky), .o(sticky1) );
|
delay1 #(7) d15(.clk(clk), .ce(ce), .i(xdif), .o(xdif1) );
|
delay1 #(7) d15(.clk(clk), .ce(ce), .i(xdif), .o(xdif1) );
|
delay1 #(FMSB+2) d14(.clk(clk), .ce(ce), .i(mfs), .o(mfs1) );
|
delay1 #(FMSB+2) d14(.clk(clk), .ce(ce), .i(mfs), .o(mfs1) );
|
Line 203... |
Line 212... |
always @*
|
always @*
|
casex({aInf1&bInf1,aNan1,bNan1})
|
casex({aInf1&bInf1,aNan1,bNan1})
|
3'b1xx: mo1 = {1'b0,op1,{FMSB-1{1'b0}},op1,{FMSB{1'b0}}}; // inf +/- inf - generate QNaN on subtract, inf on add
|
3'b1xx: mo1 = {1'b0,op1,{FMSB-1{1'b0}},op1,{FMSB{1'b0}}}; // inf +/- inf - generate QNaN on subtract, inf on add
|
3'bx1x: mo1 = {1'b0,fracta1[FMSB+1:0],{FMSB{1'b0}}};
|
3'bx1x: mo1 = {1'b0,fracta1[FMSB+1:0],{FMSB{1'b0}}};
|
3'bxx1: mo1 = {1'b0,fractb1[FMSB+1:0],{FMSB{1'b0}}};
|
3'bxx1: mo1 = {1'b0,fractb1[FMSB+1:0],{FMSB{1'b0}}};
|
default: mo1 = {mab,{FMSB-1{1'b0}}}; // mab has an extra lead bit
|
default: mo1 = {mab,{FMSB-1{1'b0}}}; // mab has an extra lead bit and two trailing bits
|
endcase
|
endcase
|
|
|
delay1 #(FX+WX+1) d3(.clk(clk), .ce(ce), .i(mo1), .o(mo) );
|
delay1 #(FX+1) d3(.clk(clk), .ce(ce), .i(mo1), .o(mo) );
|
|
|
endmodule
|
|
|
|
module fpAddsub_tb();
|
|
reg clk;
|
|
wire ce = 1'b1;
|
|
wire [2:0] rm = 3'b0;
|
|
wire [57:0] o1,o2,o3,o4,o5,o6;
|
|
wire [35:0] o11,o12,o13;
|
|
wire [31:0] o21,o22,o23;
|
|
|
|
initial begin
|
|
clk = 1'b0;
|
|
end
|
|
always #10 clk = ~clk;
|
|
|
|
fpAddsub u1 (clk, ce, rm, 1'b0, 32'h0, 32'h0, o1); // zero plus zero
|
|
fpAddsub u2 (clk, ce, rm, 1'b1, 32'h0, 32'h0, o2); // zero minus zero
|
|
fpAddsub u3 (clk, ce, rm, 1'b0, 32'h3F000000, 32'h3F000000, o3); // .5 + .5
|
|
fpAddsub u4 (clk, ce, rm, 1'b0, 32'h43520000, 32'h41700000, o4); // 210+15
|
|
fpAddsub u5 (clk, ce, rm, 1'b1, 32'hC3520000, 32'hC1700000, o5); // -210- -15
|
|
|
|
fpNormalize u11 (clk, ce, 1'b0, o3, o11);
|
|
fpNormalize u12 (clk, ce, 1'b0, o4, o12);
|
|
fpNormalize u13 (clk, ce, 1'b0, o5, o13);
|
|
|
|
fpRound u21 (3'd1, o11, o21); // zero for zero
|
|
fpRound u22 (3'd1, o12, o22); //
|
|
fpRound u23 (3'd1, o13, o23); //
|
|
|
|
endmodule
|
endmodule
|
|
|
|
|
No newline at end of file
|
No newline at end of file
|