1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
6 from nmigen
.cli
import main
10 def __init__(self
, width
, m_width
=None):
14 m_width
= width
+ 3 # extra accuracy bits
15 self
.v
= Signal(width
) # Latched copy of value
16 self
.m
= Signal(m_width
) # Mantissa
17 self
.e
= Signal((10, True)) # Exponent: 10 bits, signed
18 self
.s
= Signal() # Sign bit
22 return [self
.m
.eq(Cat(0, 0, 0, v
[0:23])), # mantissa
23 self
.e
.eq(Cat(v
[23:31]) - 127), # exponent (take off bias)
24 self
.s
.eq(Cat(v
[31])), # sign
27 def create(self
, s
, e
, m
):
29 self
.v
[31].eq(s
), # sign
30 self
.v
[23:31].eq(e
+ 127), # exp
31 self
.v
[0:23].eq(m
) # mantissa
35 return self
.create(self
.s
,
37 Cat(self
.m
[0] | self
.m
[1], self
.m
[1:self
.mw
-1], 0))
40 return self
.create(s
, 0xff, 1<<22)
43 return self
.create(s
, 0xff, 0)
46 return (self
.e
== 128) & (self
.m
!= 0)
49 return (self
.e
== 128) & (self
.m
== 0)
52 return (self
.e
== -127) & (self
.m
== 0)
56 def __init__(self
, width
):
59 self
.in_a
= Signal(width
)
60 self
.in_a_stb
= Signal()
61 self
.in_a_ack
= Signal()
63 self
.in_b
= Signal(width
)
64 self
.in_b_stb
= Signal()
65 self
.in_b_ack
= Signal()
67 self
.out_z
= Signal(width
)
68 self
.out_z_stb
= Signal()
69 self
.out_z_ack
= Signal()
71 def get_fragment(self
, platform
):
77 z
= FPNum(self
.width
, 24)
95 with m
.State("get_a"):
96 with m
.If((self
.in_a_ack
) & (self
.in_a_stb
)):
103 m
.d
.sync
+= self
.in_a_ack
.eq(1)
108 with m
.State("get_b"):
109 with m
.If((self
.in_b_ack
) & (self
.in_b_stb
)):
116 m
.d
.sync
+= self
.in_b_ack
.eq(1)
119 # unpacks operands into sign, mantissa and exponent
121 with m
.State("unpack"):
122 m
.next
= "special_cases"
123 m
.d
.sync
+= a
.decode()
124 m
.d
.sync
+= b
.decode()
127 # special cases: NaNs, infs, zeros, denormalised
129 with m
.State("special_cases"):
131 # if a is NaN or b is NaN return NaN
132 with m
.If(a
.is_nan() | b
.is_nan()):
136 # if a is inf return inf (or NaN)
137 with m
.Elif(a
.is_inf()):
139 m
.d
.sync
+= z
.inf(a
.s
)
140 # if a is inf and signs don't match return NaN
141 with m
.If((b
.e
== 128) & (a
.s
!= b
.s
)):
142 m
.d
.sync
+= z
.nan(b
.s
)
144 # if b is inf return inf
145 with m
.Elif(b
.is_inf()):
147 m
.d
.sync
+= z
.inf(b
.s
)
149 # if a is zero and b zero return signed-a/b
150 with m
.Elif(a
.is_zero() & b
.is_zero()):
152 m
.d
.sync
+= z
.create(a
.s
& b
.s
, b
.e
[0:8], b
.m
[3:26])
154 # if a is zero return b
155 with m
.Elif(a
.is_zero()):
157 m
.d
.sync
+= z
.create(b
.s
, b
.e
[0:8], b
.m
[3:26])
159 # if b is zero return a
160 with m
.Elif(b
.is_zero()):
162 m
.d
.sync
+= z
.create(a
.s
, a
.e
[0:8], a
.m
[3:26])
164 # Denormalised Number checks
167 # denormalise a check
168 with m
.If(a
.e
== -127):
169 m
.d
.sync
+= a
.e
.eq(-126) # limit a exponent
171 m
.d
.sync
+= a
.m
[26].eq(1) # set highest mantissa bit
172 # denormalise b check
173 with m
.If(b
.e
== -127):
174 m
.d
.sync
+= b
.e
.eq(-126) # limit b exponent
176 m
.d
.sync
+= b
.m
[26].eq(1) # set highest mantissa bit
179 # align. NOTE: this does *not* do single-cycle multi-shifting,
180 # it *STAYS* in the align state until the exponents match
182 with m
.State("align"):
183 # exponent of a greater than b: increment b exp, shift b mant
184 with m
.If(a
.e
> b
.e
):
185 m
.d
.sync
+= b
.shift_down()
186 # exponent of b greater than a: increment a exp, shift a mant
187 with m
.Elif(a
.e
< b
.e
):
188 m
.d
.sync
+= a
.shift_down()
189 # exponents equal: move to next stage.
194 # First stage of add. covers same-sign (add) and subtract
195 # special-casing when mantissas are greater or equal, to
196 # give greatest accuracy.
198 with m
.State("add_0"):
200 m
.d
.sync
+= z
.e
.eq(a
.e
)
201 # same-sign (both negative or both positive) add mantissas
202 with m
.If(a
.s
== b
.s
):
207 # a mantissa greater than b, use a
208 with m
.Elif(a
.m
>= b
.m
):
213 # b mantissa greater than a, use b
221 # Second stage of add: preparation for normalisation.
222 # detects when tot sum is too big (tot[27] is kinda a carry bit)
224 with m
.State("add_1"):
225 m
.next
= "normalise_1"
226 # tot[27] gets set when the sum overflows. shift result down
231 round_bit
.eq(tot
[2]),
232 sticky
.eq(tot
[1] | tot
[0]),
240 round_bit
.eq(tot
[1]),
245 # First stage of normalisation.
246 # NOTE: just like "align", this one keeps going round every clock
247 # until the result's exponent is within acceptable "range"
248 # NOTE: the weirdness of reassigning guard and round is due to
249 # the extra mantissa bits coming from tot[0..2]
251 with m
.State("normalise_1"):
252 with m
.If((z
.m
[23] == 0) & (z
.e
> -126)):
254 z
.e
.eq(z
.e
- 1), # DECREASE exponent
255 z
.m
.eq(z
.m
<< 1), # shift mantissa UP
256 z
.m
[0].eq(guard
), # steal guard bit (was tot[2])
257 guard
.eq(round_bit
), # steal round_bit (was tot[1])
260 m
.next
= "normalize_2"
263 # Second stage of normalisation.
264 # NOTE: just like "align", this one keeps going round every clock
265 # until the result's exponent is within acceptable "range"
266 # NOTE: the weirdness of reassigning guard and round is due to
267 # the extra mantissa bits coming from tot[0..2]
269 with m
.State("normalise_2"):
270 with m
.If(z
.e
< -126):
272 z
.e
.eq(z
.e
+ 1), # INCREASE exponent
273 z
.m
.eq(z
.m
>> 1), # shift mantissa DOWN
276 sticky
.eq(sticky | round_bit
)
284 with m
.State("round"):
286 with m
.If(guard
& (round_bit | sticky | z
.m
[0])):
287 m
.d
.sync
+= z
.m
.eq(z
.m
+ 1) # mantissa rounds up
288 with m
.If(z
.m
== 0xffffff): # all 1s
289 m
.d
.sync
+= z
.e
.eq(z
.e
+ 1) # exponent rounds up
294 always @(posedge clk)
302 if (s_in_a_ack && in_a_stb) begin
312 if (s_in_b_ack && in_b_stb) begin
321 a_m <= {a[22 : 0], 3'd0};
322 b_m <= {b[22 : 0], 3'd0};
323 a_e <= a[30 : 23] - 127;
324 b_e <= b[30 : 23] - 127;
327 state <= special_cases;
332 //if a is NaN or b is NaN return NaN
333 if ((a_e == 128 && a_m != 0) || (b_e == 128 && b_m != 0)) begin
339 //if a is inf return inf
340 end else if (a_e == 128) begin
344 //if a is inf and signs don't match return nan
345 if ((b_e == 128) && (a_s != b_s)) begin
352 //if b is inf return inf
353 end else if (b_e == 128) begin
358 //if a is zero return b
359 end else if ((($signed(a_e) == -127) && (a_m == 0)) && (($signed(b_e) == -127) && (b_m == 0))) begin
361 z[30:23] <= b_e[7:0] + 127;
362 z[22:0] <= b_m[26:3];
364 //if a is zero return b
365 end else if (($signed(a_e) == -127) && (a_m == 0)) begin
367 z[30:23] <= b_e[7:0] + 127;
368 z[22:0] <= b_m[26:3];
370 //if b is zero return a
371 end else if (($signed(b_e) == -127) && (b_m == 0)) begin
373 z[30:23] <= a_e[7:0] + 127;
374 z[22:0] <= a_m[26:3];
377 //Denormalised Number
378 if ($signed(a_e) == -127) begin
383 //Denormalised Number
384 if ($signed(b_e) == -127) begin
395 if ($signed(a_e) > $signed(b_e)) begin
398 b_m[0] <= b_m[0] | b_m[1];
399 end else if ($signed(a_e) < $signed(b_e)) begin
402 a_m[0] <= a_m[0] | a_m[1];
411 if (a_s == b_s) begin
415 if (a_m >= b_m) begin
432 sticky <= tot[1] | tot[0];
440 state <= normalise_1;
445 if (z_m[23] == 0 && $signed(z_e) > -126) begin
452 state <= normalise_2;
458 if ($signed(z_e) < -126) begin
463 sticky <= sticky | round_bit;
471 if (guard && (round_bit | sticky | z_m[0])) begin
473 if (z_m == 24'hffffff) begin
482 z[22 : 0] <= z_m[22:0];
483 z[30 : 23] <= z_e[7:0] + 127;
485 if ($signed(z_e) == -126 && z_m[23] == 0) begin
488 if ($signed(z_e) == -126 && z_m[23:0] == 24'h0) begin
489 z[31] <= 1'b0; // FIX SIGN BUG: -a + a = +0.
491 //if overflow occurs, return inf
492 if ($signed(z_e) > 127) begin
504 if (s_out_z_stb && out_z_ack) begin
520 assign in_a_ack = s_in_a_ack;
521 assign in_b_ack = s_in_b_ack;
522 assign out_z_stb = s_out_z_stb;
523 assign out_z = s_out_z;
528 if __name__
== "__main__":
529 alu
= FPADD(width
=32)
531 alu
.in_a
, alu
.in_a_stb
, alu
.in_a_ack
,
532 alu
.in_b
, alu
.in_b_stb
, alu
.in_b_ack
,
533 alu
.out_z
, alu
.out_z_stb
, alu
.out_z_ack
,