Bug 253847

Summary: Printf does not work correctly for denormal doubles on non-x86
Product: Base System Reporter: Alex Richardson <arichardson>
Component: standardsAssignee: freebsd-standards (Nobody) <standards>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   

Description Alex Richardson freebsd_committer freebsd_triage 2021-02-25 14:18:07 UTC
Test code:
```
cat test2.cpp
#include <stdio.h>
#include <limits>

int main() {
	// volatile double x = 0x1p-1537;
	volatile double x = std::numeric_limits<double>::denorm_min();
	printf("x=%g/%a, as long double=%Lg/%La\n", x, x, (long double)x, (long double)x);
}
```
Output on aarch64
x=0/0x1p-1537, as long double=4.94066e-324/0x1p-1074
Output on amd64:
x=4.94066e-324/0x1p-1074, as long double=4.94066e-324/0x1p-1074

This causes issues with e.g. the msun ldexp test on AArch64.

My guess would be that gdtoa depends on the x86 floating point arithmetic behaviour.
I also tried updating gdtoa locally, but other than lots of merge conflicts that did not change anything.
Comment 1 Alex Richardson freebsd_committer freebsd_triage 2021-02-26 13:14:00 UTC
https://reviews.freebsd.org/D28938
Comment 2 commit-hook freebsd_committer freebsd_triage 2021-03-01 14:28:23 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0e4ff0acbe80c547988bede738af2e227c7eb47c

commit 0e4ff0acbe80c547988bede738af2e227c7eb47c
Author:     Alex Richardson <arichardson@FreeBSD.org>
AuthorDate: 2021-03-01 14:27:30 +0000
Commit:     Alex Richardson <arichardson@FreeBSD.org>
CommitDate: 2021-03-01 14:27:30 +0000

    AArch64: Don't set flush-subnormals-to-zero flag on startup

    This flag has been set on startup since 65618fdda0f272a823e6701966421bdca0efa301.
    However, This causes some of the math-related tests to fail as they report
    zero instead of a tiny number. This fixes at least
    /usr/tests/lib/msun/ldexp_test and possibly others.
    Additionally, setting this flag prevents printf() from printing subnormal
    numbers in decimal form.
    See also https://www.openwall.com/lists/musl/2021/02/26/1

    PR:             253847
    Reviewed By:    mmel
    Differential Revision: https://reviews.freebsd.org/D28938

 lib/libc/tests/stdio/printfloat_test.c | 35 +++++++++++++++++++++++++++++++++-
 sys/arm64/arm64/vm_machdep.c           |  2 +-
 2 files changed, 35 insertions(+), 2 deletions(-)
Comment 3 commit-hook freebsd_committer freebsd_triage 2021-03-17 10:29:49 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=d37fb0e37814db8f76462b3b9f1fb0e6dfca6324

commit d37fb0e37814db8f76462b3b9f1fb0e6dfca6324
Author:     Alex Richardson <arichardson@FreeBSD.org>
AuthorDate: 2021-03-01 14:27:30 +0000
Commit:     Alex Richardson <arichardson@FreeBSD.org>
CommitDate: 2021-03-17 09:45:56 +0000

    AArch64: Don't set flush-subnormals-to-zero flag on startup

    This flag has been set on startup since 65618fdda0f272a823e6701966421bdca0efa301.
    However, This causes some of the math-related tests to fail as they report
    zero instead of a tiny number. This fixes at least
    /usr/tests/lib/msun/ldexp_test and possibly others.
    Additionally, setting this flag prevents printf() from printing subnormal
    numbers in decimal form.
    See also https://www.openwall.com/lists/musl/2021/02/26/1

    PR:             253847
    Reviewed By:    mmel
    Differential Revision: https://reviews.freebsd.org/D28938

    (cherry picked from commit 0e4ff0acbe80c547988bede738af2e227c7eb47c)

 lib/libc/tests/stdio/printfloat_test.c | 35 +++++++++++++++++++++++++++++++++-
 sys/arm64/arm64/vm_machdep.c           |  2 +-
 2 files changed, 35 insertions(+), 2 deletions(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2021-03-24 00:08:47 UTC
A commit in branch releng/13.0 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f979a90f4209418a26d858ca97b1497985bda0a7

commit f979a90f4209418a26d858ca97b1497985bda0a7
Author:     Alex Richardson <arichardson@FreeBSD.org>
AuthorDate: 2021-03-01 14:27:30 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-03-24 00:07:34 +0000

    AArch64: Don't set flush-subnormals-to-zero flag on startup

    This flag has been set on startup since 65618fdda0f272a823e6701966421bdca0efa301.
    However, This causes some of the math-related tests to fail as they report
    zero instead of a tiny number. This fixes at least
    /usr/tests/lib/msun/ldexp_test and possibly others.
    Additionally, setting this flag prevents printf() from printing subnormal
    numbers in decimal form.
    See also https://www.openwall.com/lists/musl/2021/02/26/1

    PR:             253847
    Reviewed By:    mmel
    Approved by:    re (gjb)
    Differential Revision: https://reviews.freebsd.org/D28938

    (cherry picked from commit 0e4ff0acbe80c547988bede738af2e227c7eb47c)
    (cherry picked from commit d37fb0e37814db8f76462b3b9f1fb0e6dfca6324)

 lib/libc/tests/stdio/printfloat_test.c | 35 +++++++++++++++++++++++++++++++++-
 sys/arm64/arm64/vm_machdep.c           |  2 +-
 2 files changed, 35 insertions(+), 2 deletions(-)