24e87428e559177cdb1727d8fa7a5ed44d013daf
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 """ Floating-point Number Class, variable-width TODO (currently 32-bit)
12 Contains signals for an incoming copy of the value, decoded into
13 sign / exponent / mantissa.
14 Also contains encoding functions, creation and recognition of
15 zero, NaN and inf (all signed)
17 Four extra bits are included in the mantissa: the top bit
18 (m[-1]) is effectively a carry-overflow. The other three are
19 guard (m[2]), round (m[1]), and sticky (m[0])
21 def __init__(self
, width
, m_width
=None):
24 m_width
= width
- 5 # mantissa extra bits (top,guard,round)
25 self
.v
= Signal(width
) # Latched copy of value
26 self
.m
= Signal(m_width
) # Mantissa
27 self
.e
= Signal((10, True)) # Exponent: 10 bits, signed
28 self
.s
= Signal() # Sign bit
31 """ decodes a latched value into sign / exponent / mantissa
33 bias is subtracted here, from the exponent.
36 return [self
.m
.eq(Cat(0, 0, 0, v
[0:23])), # mantissa
37 self
.e
.eq(Cat(v
[23:31]) - 127), # exponent (take off bias)
38 self
.s
.eq(Cat(v
[31])), # sign
41 def create(self
, s
, e
, m
):
42 """ creates a value from sign / exponent / mantissa
44 bias is added here, to the exponent
47 self
.v
[31].eq(s
), # sign
48 self
.v
[23:31].eq(e
+ 127), # exp (add on bias)
49 self
.v
[0:23].eq(m
) # mantissa
53 """ shifts a mantissa down by one. exponent is increased to compensate
55 accuracy is lost as a result in the mantissa however there are 3
56 guard bits (the latter of which is the "sticky" bit)
58 return self
.create(self
.s
,
60 Cat(self
.m
[0] | self
.m
[1], self
.m
[1:-5], 0))
63 return self
.create(s
, 0x80, 1<<22)
66 return self
.create(s
, 0x80, 0)
69 return self
.create(s
, -127, 0)
72 return (self
.e
== 128) & (self
.m
!= 0)
75 return (self
.e
== 128) & (self
.m
== 0)
78 return (self
.e
== -127) & (self
.m
== 0)
80 def is_overflowed(self
):
85 def __init__(self
, width
):
88 self
.in_a
= Signal(width
)
89 self
.in_a_stb
= Signal()
90 self
.in_a_ack
= Signal()
92 self
.in_b
= Signal(width
)
93 self
.in_b_stb
= Signal()
94 self
.in_b_ack
= Signal()
96 self
.out_z
= Signal(width
)
97 self
.out_z_stb
= Signal()
98 self
.out_z_ack
= Signal()
100 def get_fragment(self
, platform
):
104 a
= FPNum(self
.width
)
105 b
= FPNum(self
.width
)
106 z
= FPNum(self
.width
, 24)
108 tot
= Signal(28) # sticky/round/guard bits, 23 result, 1 overflow
110 guard
= Signal() # tot[2]
111 round_bit
= Signal() # tot[1]
112 sticky
= Signal() # tot[0]
119 with m
.State("get_a"):
120 with m
.If((self
.in_a_ack
) & (self
.in_a_stb
)):
127 m
.d
.sync
+= self
.in_a_ack
.eq(1)
132 with m
.State("get_b"):
133 with m
.If((self
.in_b_ack
) & (self
.in_b_stb
)):
140 m
.d
.sync
+= self
.in_b_ack
.eq(1)
143 # unpacks operands into sign, mantissa and exponent
145 with m
.State("unpack"):
146 m
.next
= "special_cases"
147 m
.d
.sync
+= a
.decode()
148 m
.d
.sync
+= b
.decode()
151 # special cases: NaNs, infs, zeros, denormalised
153 with m
.State("special_cases"):
155 # if a is NaN or b is NaN return NaN
156 with m
.If(a
.is_nan() | b
.is_nan()):
160 # if a is inf return inf (or NaN)
161 with m
.Elif(a
.is_inf()):
163 m
.d
.sync
+= z
.inf(a
.s
)
164 # if a is inf and signs don't match return NaN
165 with m
.If((b
.e
== 128) & (a
.s
!= b
.s
)):
166 m
.d
.sync
+= z
.nan(b
.s
)
168 # if b is inf return inf
169 with m
.Elif(b
.is_inf()):
171 m
.d
.sync
+= z
.inf(b
.s
)
173 # if a is zero and b zero return signed-a/b
174 with m
.Elif(a
.is_zero() & b
.is_zero()):
176 m
.d
.sync
+= z
.create(a
.s
& b
.s
, b
.e
[0:8], b
.m
[3:26])
178 # if a is zero return b
179 with m
.Elif(a
.is_zero()):
181 m
.d
.sync
+= z
.create(b
.s
, b
.e
[0:8], b
.m
[3:26])
183 # if b is zero return a
184 with m
.Elif(b
.is_zero()):
186 m
.d
.sync
+= z
.create(a
.s
, a
.e
[0:8], a
.m
[3:26])
188 # Denormalised Number checks
191 # denormalise a check
192 with m
.If(a
.e
== -127):
193 m
.d
.sync
+= a
.e
.eq(-126) # limit a exponent
195 m
.d
.sync
+= a
.m
[26].eq(1) # set highest mantissa bit
196 # denormalise b check
197 with m
.If(b
.e
== -127):
198 m
.d
.sync
+= b
.e
.eq(-126) # limit b exponent
200 m
.d
.sync
+= b
.m
[26].eq(1) # set highest mantissa bit
203 # align. NOTE: this does *not* do single-cycle multi-shifting,
204 # it *STAYS* in the align state until the exponents match
206 with m
.State("align"):
207 # exponent of a greater than b: increment b exp, shift b mant
208 with m
.If(a
.e
> b
.e
):
209 m
.d
.sync
+= b
.shift_down()
210 # exponent of b greater than a: increment a exp, shift a mant
211 with m
.Elif(a
.e
< b
.e
):
212 m
.d
.sync
+= a
.shift_down()
213 # exponents equal: move to next stage.
218 # First stage of add. covers same-sign (add) and subtract
219 # special-casing when mantissas are greater or equal, to
220 # give greatest accuracy.
222 with m
.State("add_0"):
224 m
.d
.sync
+= z
.e
.eq(a
.e
)
225 # same-sign (both negative or both positive) add mantissas
226 with m
.If(a
.s
== b
.s
):
231 # a mantissa greater than b, use a
232 with m
.Elif(a
.m
>= b
.m
):
237 # b mantissa greater than a, use b
245 # Second stage of add: preparation for normalisation.
246 # detects when tot sum is too big (tot[27] is kinda a carry bit)
248 with m
.State("add_1"):
249 m
.next
= "normalise_1"
250 # tot[27] gets set when the sum overflows. shift result down
255 round_bit
.eq(tot
[2]),
256 sticky
.eq(tot
[1] | tot
[0]),
264 round_bit
.eq(tot
[1]),
269 # First stage of normalisation.
270 # NOTE: just like "align", this one keeps going round every clock
271 # until the result's exponent is within acceptable "range"
272 # NOTE: the weirdness of reassigning guard and round is due to
273 # the extra mantissa bits coming from tot[0..2]
275 with m
.State("normalise_1"):
276 with m
.If((z
.m
[23] == 0) & (z
.e
> -126)):
278 z
.e
.eq(z
.e
- 1), # DECREASE exponent
279 z
.m
.eq(z
.m
<< 1), # shift mantissa UP
280 z
.m
[0].eq(guard
), # steal guard bit (was tot[2])
281 guard
.eq(round_bit
), # steal round_bit (was tot[1])
284 m
.next
= "normalize_2"
287 # Second stage of normalisation.
288 # NOTE: just like "align", this one keeps going round every clock
289 # until the result's exponent is within acceptable "range"
290 # NOTE: the weirdness of reassigning guard and round is due to
291 # the extra mantissa bits coming from tot[0..2]
293 with m
.State("normalise_2"):
294 with m
.If(z
.e
< -126):
296 z
.e
.eq(z
.e
+ 1), # INCREASE exponent
297 z
.m
.eq(z
.m
>> 1), # shift mantissa DOWN
300 sticky
.eq(sticky | round_bit
)
308 with m
.State("round"):
310 with m
.If(guard
& (round_bit | sticky | z
.m
[0])):
311 m
.d
.sync
+= z
.m
.eq(z
.m
+ 1) # mantissa rounds up
312 with m
.If(z
.m
== 0xffffff): # all 1s
313 m
.d
.sync
+= z
.e
.eq(z
.e
+ 1) # exponent rounds up
318 with m
.State("pack"):
321 z
.v
[0:22].eq(z
.m
[0:22]),
322 z
.v
[22:31].eq(z
.e
[0:7]),
325 with m
.If((z
.e
== -126) & (z
.m
[23] == 0)):
326 m
.d
.sync
+= z
.v
[23:31].eq(0)
327 with m
.If(z
.is_overflowed()):
338 if (s_out_z_stb && out_z_ack) begin
348 always @(posedge clk)
356 if (s_in_a_ack && in_a_stb) begin
366 if (s_in_b_ack && in_b_stb) begin
375 a_m <= {a[22 : 0], 3'd0};
376 b_m <= {b[22 : 0], 3'd0};
377 a_e <= a[30 : 23] - 127;
378 b_e <= b[30 : 23] - 127;
381 state <= special_cases;
386 //if a is NaN or b is NaN return NaN
387 if ((a_e == 128 && a_m != 0) || (b_e == 128 && b_m != 0)) begin
393 //if a is inf return inf
394 end else if (a_e == 128) begin
398 //if a is inf and signs don't match return nan
399 if ((b_e == 128) && (a_s != b_s)) begin
406 //if b is inf return inf
407 end else if (b_e == 128) begin
412 //if a is zero return b
413 end else if ((($signed(a_e) == -127) && (a_m == 0)) && (($signed(b_e) == -127) && (b_m == 0))) begin
415 z[30:23] <= b_e[7:0] + 127;
416 z[22:0] <= b_m[26:3];
418 //if a is zero return b
419 end else if (($signed(a_e) == -127) && (a_m == 0)) begin
421 z[30:23] <= b_e[7:0] + 127;
422 z[22:0] <= b_m[26:3];
424 //if b is zero return a
425 end else if (($signed(b_e) == -127) && (b_m == 0)) begin
427 z[30:23] <= a_e[7:0] + 127;
428 z[22:0] <= a_m[26:3];
431 //Denormalised Number
432 if ($signed(a_e) == -127) begin
437 //Denormalised Number
438 if ($signed(b_e) == -127) begin
449 if ($signed(a_e) > $signed(b_e)) begin
452 b_m[0] <= b_m[0] | b_m[1];
453 end else if ($signed(a_e) < $signed(b_e)) begin
456 a_m[0] <= a_m[0] | a_m[1];
465 if (a_s == b_s) begin
469 if (a_m >= b_m) begin
486 sticky <= tot[1] | tot[0];
494 state <= normalise_1;
499 if (z_m[23] == 0 && $signed(z_e) > -126) begin
506 state <= normalise_2;
512 if ($signed(z_e) < -126) begin
517 sticky <= sticky | round_bit;
525 if (guard && (round_bit | sticky | z_m[0])) begin
527 if (z_m == 24'hffffff) begin
536 z[22 : 0] <= z_m[22:0];
537 z[30 : 23] <= z_e[7:0] + 127;
539 if ($signed(z_e) == -126 && z_m[23] == 0) begin
542 if ($signed(z_e) == -126 && z_m[23:0] == 24'h0) begin
543 z[31] <= 1'b0; // FIX SIGN BUG: -a + a = +0.
545 //if overflow occurs, return inf
546 if ($signed(z_e) > 127) begin
558 if (s_out_z_stb && out_z_ack) begin
574 assign in_a_ack = s_in_a_ack;
575 assign in_b_ack = s_in_b_ack;
576 assign out_z_stb = s_out_z_stb;
577 assign out_z = s_out_z;
582 if __name__
== "__main__":
583 alu
= FPADD(width
=32)
585 alu
.in_a
, alu
.in_a_stb
, alu
.in_a_ack
,
586 alu
.in_b
, alu
.in_b_stb
, alu
.in_b_ack
,
587 alu
.out_z
, alu
.out_z_stb
, alu
.out_z_ack
,
592 print(verilog.convert(alu, ports=[in_a, in_a_stb, in_a_ack, #doesnt work for some reason
593 in_b, in_b_stb, in_b_ack,
594 out_z, out_z_stb, out_z_ack]))