From c53e4245b9c1fc8a31fcdfa0a3dc9c3bd056ee20 Mon Sep 17 00:00:00 2001 From: Flavien Solt Date: Mon, 22 Jun 2026 10:38:26 +0800 Subject: [PATCH] Add regression test for negative zero sign preservation Check that the vvp code generator emits a -0.0 real constant with its sign bit set, so the compiled value matches the runtime real value. The sign used to be detected with (value < 0), which is false for IEEE 754 -0.0, and a -0.0 constant was turned into +0.0. Co-Authored-By: Claude Opus 4.8 (1M context) --- ivtest/ivltests/real_negative_zero.v | 35 ++++++++++++++++++++++++ ivtest/regress-vvp.list | 1 + ivtest/vvp_tests/real_negative_zero.json | 4 +++ 3 files changed, 40 insertions(+) create mode 100644 ivtest/ivltests/real_negative_zero.v create mode 100644 ivtest/vvp_tests/real_negative_zero.json diff --git a/ivtest/ivltests/real_negative_zero.v b/ivtest/ivltests/real_negative_zero.v new file mode 100644 index 000000000..03199005c --- /dev/null +++ b/ivtest/ivltests/real_negative_zero.v @@ -0,0 +1,35 @@ +// Check that the vvp code generator preserves the sign of a negative zero +// real constant. The sign used to be detected with (value < 0), which is +// false for IEEE 754 -0.0, so a -0.0 constant was emitted as +0.0 and the +// compiled value no longer matched the runtime real value. + +module test; + + real nz; + real pz; + + initial begin + nz = -0.0; + pz = 0.0; + + // The sign bit is the only thing that tells -0.0 from +0.0. + if ($realtobits(nz) !== 64'h8000_0000_0000_0000) begin + $display("FAILED: -0.0 stored as %h", $realtobits(nz)); + $finish; + end + + if ($realtobits(pz) !== 64'h0000_0000_0000_0000) begin + $display("FAILED: 0.0 stored as %h", $realtobits(pz)); + $finish; + end + + // The reciprocal makes the sign observable: 1.0/-0.0 is -inf. + if (1.0 / nz >= 0.0) begin + $display("FAILED: 1.0/-0.0 = %g", 1.0 / nz); + $finish; + end + + $display("PASSED"); + end + +endmodule diff --git a/ivtest/regress-vvp.list b/ivtest/regress-vvp.list index e17f69eee..97eaddbd0 100644 --- a/ivtest/regress-vvp.list +++ b/ivtest/regress-vvp.list @@ -207,6 +207,7 @@ pv_wr_fn_vec2 vvp_tests/pv_wr_fn_vec2.json pv_wr_fn_vec4 vvp_tests/pv_wr_fn_vec4.json queue_fail vvp_tests/queue_fail.json readmem-invalid vvp_tests/readmem-invalid.json +real_negative_zero vvp_tests/real_negative_zero.json scaled_real vvp_tests/scaled_real.json scan-invalid vvp_tests/scan-invalid.json sdf_interconnect1 vvp_tests/sdf_interconnect1.json diff --git a/ivtest/vvp_tests/real_negative_zero.json b/ivtest/vvp_tests/real_negative_zero.json new file mode 100644 index 000000000..1165f27f5 --- /dev/null +++ b/ivtest/vvp_tests/real_negative_zero.json @@ -0,0 +1,4 @@ +{ + "type" : "normal", + "source" : "real_negative_zero.v" +}