From 27075a53492a6c6f81a4587bc16072ce69506a7d Mon Sep 17 00:00:00 2001 From: eastWillow Date: Fri, 10 Oct 2025 05:12:06 +0800 Subject: [PATCH] Fix signed zero handling according to IEEE 754 Ensure correct handling of signed zero in addition cases: (+0) + (-0) should yield +0, and (-0) + (-0) should yield -0, as required by IEEE 754-2019 Section 6.3 under the default rounding mode. Original behavior: 1. (+0) + (-0) returns -0, not following the spec 2. (-0) + (-0) returns -0, following the spec Patched behavior: 1. (+0) + (-0) returns +0, following the spec 2. (-0) + (-0) returns -0, following the spec --- q1-bfloat16.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/q1-bfloat16.c b/q1-bfloat16.c index 65e044e..488092c 100644 --- a/q1-bfloat16.c +++ b/q1-bfloat16.c @@ -67,8 +67,15 @@ static inline bf16_t bf16_add(bf16_t a, bf16_t b) } if (exp_b == 0xFF) return b; - if (!exp_a && !mant_a) - return b; + if (!exp_a && !mant_a) { + if (!exp_b && !mant_b) { + if (sign_a != sign_b) + return BF16_ZERO(); + else + return a; + } else + return b; + } if (!exp_b && !mant_b) return a; if (exp_a)