]> Witch of Git - nan-gate/blob - techlib.sv
Do NaN synthesis pattern matching via attributes
[nan-gate] / techlib.sv
1 `define POS0 3'b000
2 `define NEG0 3'b100
3 `define POS1 3'b001
4 `define NEG1 3'b101
5 `define PINF 3'b010
6 `define NINF 3'b110
7 `define PNAN 3'b011
8 `define NNAN 3'b111
9 `define ANAN 3'b?11
10 `define ANYF 3'b???
11
12 // A NaN gate! Computes `Inf - max(x+y, -Inf)`.
13 // See http://tom7.org/nand/
14 module nan_fp3(
15 input [2:0] A,
16 input [2:0] B,
17 output reg [2:0] Y
18 );
19
20 always begin
21 casez ({A, B})
22 {`ANYF, `ANAN}, {`ANAN, `ANYF}: Y = `PINF;
23 {`PINF, `NINF}, {`NINF, `PINF}: Y = `PINF;
24 {`ANYF, `PINF}, {`PINF, `ANYF}: Y = `PNAN;
25 default: Y = `PINF;
26 endcase
27 end
28 endmodule
29
30 // Convert a bit into an fp3
31 module bit_to_fp3(input A, output [2:0] Y);
32 assign Y = A ? `PNAN : `PINF;
33 endmodule
34
35 // Convert an fp3 into a bit
36 module fp3_to_bit(input [2:0] A, output reg Y);
37 always begin
38 case (A)
39 `PNAN, `NNAN: Y = 1'b1;
40 default: Y = 1'b0;
41 endcase
42 end
43 endmodule
44