From e9342ce0ae76789eaba3aee7cb94b1352c5921ca Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Sun, 2 Feb 2020 13:37:10 -0500 Subject: [PATCH] Add test case for NaN and -0 handling to fpmax --- src/ieee754/fpmax/test/test_fpmax_pipe.py | 28 +++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ieee754/fpmax/test/test_fpmax_pipe.py b/src/ieee754/fpmax/test/test_fpmax_pipe.py index b8168af7..a2f2174e 100644 --- a/src/ieee754/fpmax/test/test_fpmax_pipe.py +++ b/src/ieee754/fpmax/test/test_fpmax_pipe.py @@ -3,6 +3,8 @@ from ieee754.fpmax.pipeline import (FPMAXMuxInOut) from ieee754.fpcommon.test.fpmux import runfp +from ieee754.fpcommon.test.case_gen import run_pipe_fp +from ieee754.fpcommon.test import unit_test_single from sfpy import Float16, Float32, Float64 import math @@ -19,7 +21,9 @@ def fpmax_f32_max(a, b): return Float32(float('nan')) else: return b if math.isnan(a) else a - if a > b: + if a.bits & (1<<31) != b.bits & (1<<31): + return b if a.bits & (1<<31) else a + elif a > b: return a else: return b @@ -31,7 +35,9 @@ def fpmax_f32_min(a, b): return Float32(float('nan')) else: return b if math.isnan(a) else a - if a < b: + if a.bits & (1<<31) != b.bits & (1<<31): + return a if a.bits & (1<<31) else b + elif a < b: return a else: return b @@ -48,8 +54,26 @@ def test_fpmax_f32_min(): runfp(dut, 32, "test_fpmax_f32_min", Float32, fpmax_f32_min, n_vals=100, opcode=0x1) +def nan_testcases(): + nan = Float32(float('nan')).bits + yield nan, Float32(1.0).bits + yield Float32(1.0).bits, nan + yield nan, nan + yield Float32(0.0).bits, Float32(-0.0).bits + yield Float32(-0.0).bits, Float32(0.0).bits + +def test_fpmax_f32_nans(): + dut = FPMAXMuxInOut(32, 4) + run_pipe_fp(dut, 32, "test_fpmax_f32_max_nans", unit_test_single, + Float32, nan_testcases, fpmax_f32_max, 5, + opcode=0b0) + run_pipe_fp(dut, 32, "test_fpmax_f32_min_nans", unit_test_single, + Float32, nan_testcases, fpmax_f32_min, 5, + opcode=0b1) + if __name__ == '__main__': + test_fpmax_f32_nans() for i in range(50): test_fpmax_f32_max() test_fpmax_f32_min() -- 2.30.2