Add fpmin handling to fpmax module
authorMichael Nolan <mtnolan2640@gmail.com>
Tue, 28 Jan 2020 16:42:58 +0000 (11:42 -0500)
committerMichael Nolan <mtnolan2640@gmail.com>
Tue, 28 Jan 2020 19:53:47 +0000 (14:53 -0500)
src/ieee754/fpmax/fpmax.py
src/ieee754/fpmax/test/test_fpmax_pipe.py

index 1987d8ee9baf935b3e8d3e1cef0bf923b6e5aff8..5c91976d65a526177eab4a7e9cbca5959be732f8 100644 (file)
@@ -48,18 +48,24 @@ class FPMAXPipeMod(PipeModBase):
 
         has_nan = Signal()
         comb += has_nan.eq(a1.is_nan | b1.is_nan)
+        both_nan = Signal()
+        comb += both_nan.eq(a1.is_nan & b1.is_nan)
         with m.If(has_nan):
-            comb += z1.eq(Mux(a1.is_nan, self.i.b, self.i.a))
+            with m.If(both_nan):
+                comb += z1.eq(a1.fp.nan2(0))
+            with m.Else():
+                comb += z1.eq(Mux(a1.is_nan, self.i.b, self.i.a))
         with m.Else():
             with m.If(a1.s != b1.s):
                 
-                comb += z1.eq(Mux(a1.s, self.i.b, self.i.a))
+                comb += z1.eq(Mux(a1.s ^ opcode[0], self.i.b, self.i.a))
             with m.Else():
                 gt = Signal()
                 sign = Signal()
                 comb += sign.eq(a1.s)
                 comb += gt.eq(a1.v > b1.v)
-                comb += z1.eq(Mux(gt ^ sign, self.i.a, self.i.b))
+                comb += z1.eq(Mux(gt ^ sign ^ opcode[0],
+                                  self.i.a, self.i.b))
                               
 
         # copy the context (muxid, operator)
index 65823f3f5daf4ed8c3c3936af4f8ba1b565c667e..e95d3430d31e9762082ca49a142a1b46db2881ca 100644 (file)
@@ -7,26 +7,44 @@ from ieee754.fpcommon.test.fpmux import runfp
 from sfpy import Float16, Float32, Float64
 import math
 
+
 def fpmax_f32_max(a, b):
-    
     if math.isnan(a) or math.isnan(b):
         if math.isnan(a) and math.isnan(b):
             return Float32(float('nan'))
         else:
             return b if math.isnan(a) else a
-                    
     if a > b:
         return a
     else:
         return b
 
 
+def fpmax_f32_min(a, b):
+    if math.isnan(a) or math.isnan(b):
+        if math.isnan(a) and math.isnan(b):
+            return Float32(float('nan'))
+        else:
+            return b if math.isnan(a) else a
+    if a < b:
+        return a
+    else:
+        return b
+
+
 def test_fpmax_f32_max():
     dut = FPMAXMuxInOut(32, 4)
     runfp(dut, 32, "test_fpmax_f32_max", Float32, fpmax_f32_max,
           n_vals=100, opcode=0x0)
 
 
+def test_fpmax_f32_min():
+    dut = FPMAXMuxInOut(32, 4)
+    runfp(dut, 32, "test_fpmax_f32_min", Float32, fpmax_f32_min,
+          n_vals=100, opcode=0x1)
+
+
 if __name__ == '__main__':
     for i in range(50):
+        test_fpmax_f32_min()
         test_fpmax_f32_max()