From nobody Mon Mar 18 13:04:09 2024 X-Original-To: bugs@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Tyw6L19VRz5DhgD for ; Mon, 18 Mar 2024 13:04:10 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Tyw6K6kmhz4HWW for ; Mon, 18 Mar 2024 13:04:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1710767049; a=rsa-sha256; cv=none; b=YNqA3U00oXzjDwzDjPRtCQjWv8uvReZM9HJ5aIk/lPpW21b+PMvnZ2OtElUe4IPm4SQFcc QF1qk0BzhQ3+Wpa0RJKrhUoieW0xjoseVYxJpQyBF22cKr0LzrJWzMwLxTTomQtxrVmDiW au+GGgX12Q97Zz2m6FAW6qTAG8tXafwtwAUbTQPKQmf8o38rR9Xz3cn3A8sruI3HKLd9Hc qbuE/nAMIIrE50GSqPKl5aTC5YnF/gRDMYZYfLLBjfbsLiZX33X1it83H/igTjy/tqqY5I Yfa0Ki2YJysEddkHgdDgZarDorajvZ124vypw5aagcJcYcviJ3rkjGbg+3F8gg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1710767049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ca4caISQB4GeoYoDKUYLt/K5XOjuVlTC3Eacl+grcfQ=; b=i5qTQPxJwyvRr9e8acvkfl4C3znsAN9ramF0isa5CxPYIhgOoErurzdn5VHClg4j7qTEfM nFY6QKVWtBaT38ROMU0TvIWAcM2/4no37r7PUW/Bik+1I1oqaJtwzR3lRtynmlCR5MjAgv v3zgBT8zksCQMi3YCW8u4Sfeplt/W0FxKstb1zTeu0OSs4BFhr3Q6iMIhfUFddrpPK+wHW +z70XPlrdKAtn/E90ViAvDbu+RiCdSCElkzbf7FPjEfQtskNCObiVZ4fnhH/VkNm8J0IMG rfj0+kFVwpqhrocdhjhQNh+HVHWtzdMudbKxY2Z43wF5Tjrbx2H6brQSjGFKWg== Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Tyw6K6MLmzNsQ for ; Mon, 18 Mar 2024 13:04:09 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 42ID49rI080708 for ; Mon, 18 Mar 2024 13:04:09 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 42ID49A8080705 for bugs@FreeBSD.org; Mon, 18 Mar 2024 13:04:09 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 277783] libc fma() doesn't not return the correct zero sign Date: Mon, 18 Mar 2024 13:04:09 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: 14.0-RELEASE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Only Me X-Bugzilla-Who: victor.stinner@gmail.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter attachments.mimetype attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Bug reports List-Archive: https://lists.freebsd.org/archives/freebsd-bugs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-bugs@freebsd.org MIME-Version: 1.0 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D277783 Bug ID: 277783 Summary: libc fma() doesn't not return the correct zero sign Product: Base System Version: 14.0-RELEASE Hardware: Any OS: Any Status: New Severity: Affects Only Me Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: victor.stinner@gmail.com Attachment #249266 text/plain mime type: Created attachment 249266 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=3D249266&action= =3Dedit Example reproducing the fma() zero sign issue Hi, I exposed the libc fma() function in Python: * https://github.com/python/cpython/commit/8e3c953b3acd7c656f66696806c9ae917e= 816492 * https://github.com/python/cpython/pull/116667 * https://github.com/python/cpython/issues/73468 The problem is that the Python test suite fails on FreeBSD x86-64 on tests = on the zero sign: test_fma_zero_result() https://github.com/python/cpython/blob/cd2ed917801b93fb46d1dcf19dd480e51469= 32d8/Lib/test/test_math.py#L2694-L2748 Example: --- #include #include #include void set_double(double *x, const char *bytes) { memcpy(x, bytes, sizeof(*x)); } int main(int argc, char **argv) { double x =3D 0; //float.fromhex('0x1p-500') double y =3D 0; //float.fromhex('0x1p-550') double z =3D 0; //float.fromhex('0x1p-1000') #if 1 // condition always true, but trick the compiler, otherwise it will not // call fma() at runtime, but just compute the result at build time. if (argc) { set_double(&x, "\x00\x00\x00\x00\x00\x00\xb0\x20"); } else { x =3D 123; } #else set_double(&x, "\x00\x00\x00\x00\x00\x00\xb0\x20"); #endif set_double(&y, "\x00\x00\x00\x00\x00\x00\x90\x1d"); set_double(&z, "\x00\x00\x00\x00\x00\x00p\x01"); printf("x %g\n", x); printf("y %g\n", y); printf("z %g\n", z); double a, b, c, r; a =3D x-y; b =3D x+y; c =3D -z; r =3D fma(a, b, c); printf("fma(x-y, x+y, -z):\n"); printf("fma(%+g, %+g, %+g) =3D %+g\n", a, b, c, r); return 0; } --- Output on x86-64: --- $ clang x.c -Og -o x -lm && ./x x 3.05494e-151 y 2.71333e-166 z 9.33264e-302 fma(x-y, x+y, -z): fma(+3.05494e-151, +3.05494e-151, -9.33264e-302) =3D +0 --- fma() result is "+0" which is wrong. For example, if you replace "#if 1" wi= th "#if 0", you get: --- (...) fma(+3.05494e-151, +3.05494e-151, -9.33264e-302) =3D -0 --- fma() result is now "-0" which is correct. This time, the result was comput= ed at build time. Extract of the Python tests which fail on FreeBSD: --- # Corner case where rounding the multiplication would # give the wrong result. x =3D float.fromhex('0x1p-500') y =3D float.fromhex('0x1p-550') z =3D float.fromhex('0x1p-1000') self.assertIsNegativeZero(math.fma(x-y, x+y, -z)) self.assertIsPositiveZero(math.fma(y-x, x+y, z)) self.assertIsNegativeZero(math.fma(y-x, -(x+y), -z)) self.assertIsPositiveZero(math.fma(x-y, -(x+y), z)) --- For example, on Linux x86-64, the example works as expected with GCC and cl= ang: --- vstinner@mona$ gcc x.c -o x -O2 -lm && ./x x 3.05494e-151 y 2.71333e-166 z 9.33264e-302 fma(x-y, x+y, -z): fma(+3.05494e-151, +3.05494e-151, -9.33264e-302) =3D -0 vstinner@mona$ clang x.c -o x -O2 -lm && ./x x 3.05494e-151 y 2.71333e-166 z 9.33264e-302 fma(x-y, x+y, -z): fma(+3.05494e-151, +3.05494e-151, -9.33264e-302) =3D -0 --- fma() returns -0 which is correct. On the bug, I selected "kern" component. I expect that the bug is more in t= he libc, but I cannot see "libc" choice. --=20 You are receiving this mail because: You are the assignee for the bug.=