Skip to contentSkip to conversation

float: tests and bug fix for #5203

PATCH In review Alex Bennée 4 participants 16 messages Dec 1 – Dec 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover float: tests and bug fix for #5203

We had a bug reported that fma fails on some platforms. I can't test the fix but I took the opportunity to extend the TCG test cases to cover a few more of the fma corner cases along with what is hopefully the fix.

It occurred to me that the linux-user tests would be hard to run on windows and also only covered fma() so I added fp-test-hard to do general A/B test comparing softfloat and hardfloat results. Whether this is worth the cost of promoting force_soft_fma to force_soft_float remains to be seen. My initial run with fp-bench shows a drop

Before:

760.21 MFlops

After:

720.04 MFlops

So it might we worth scaling the test back just to fma() and any other places where the compiler/libc can get in the way.

v2
- added tags from pbo
- moved the ref updates to the right commit
- added fp-test-hard

Alex.

Alex Bennée (6):
meson: expose MESON_SOURCE_ROOT to scripts
scripts/tests: add regen-reference.sh
tests/tcg: extend the float_madds test case
fpu/softfloat: gate hardfloat on working fmaf
fpu: promote force_soft_fma to force_soft_float
tests/fp: add fp-test-hard

 meson.build                           |   1 +
 include/fpu/softfloat.h               |   6 +
 fpu/softfloat.c                       |  26 +-
 tests/fp/fp-test-hard.c               | 702 ++++++++++++++++++++++++++
 tests/tcg/multiarch/float_madds.c     |  13 +
 scripts/tests/regen-reference.sh      |  87 ++++
 tests/fp/meson.build                  |  30 +-
 tests/tcg/aarch64/float_madds.ref     |  64 +++
 tests/tcg/alpha/float_madds.ref       |  64 +++
 tests/tcg/arm/float_madds.ref         |  64 +++
 tests/tcg/hexagon/float_madds.ref     |  64 +++
 tests/tcg/hppa/float_madds.ref        |  64 +++
 tests/tcg/i386/float_madds.ref        |  64 +++
 tests/tcg/loongarch64/float_madds.ref |  64 +++
 tests/tcg/m68k/float_madds.ref        |  64 +++
 tests/tcg/mips/float_madds.ref        |  64 +++
 tests/tcg/mips64/float_madds.ref      |  64 +++
 tests/tcg/mips64el/float_madds.ref    |  64 +++
 tests/tcg/mipsel/float_madds.ref      |  64 +++
 tests/tcg/or1k/float_madds.ref        |  16 +
 tests/tcg/ppc64/float_madds.ref       |  64 +++
 tests/tcg/ppc64le/float_madds.ref     |  64 +++
 tests/tcg/riscv64/float_madds.ref     |  64 +++
 tests/tcg/s390x/float_madds.ref       |  64 +++
 tests/tcg/sh4/float_madds.ref         |  32 ++
 tests/tcg/x86_64/float_madds.ref      |  64 +++
 26 files changed, 1979 insertions(+), 22 deletions(-)
 create mode 100644 tests/fp/fp-test-hard.c
 create mode 100755 scripts/tests/regen-reference.sh
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/6 meson: expose MESON_SOURCE_ROOT to scripts

If we are mandating using the ./run wrapper when calling scripts we should also expose the source root to save scripts having to jump hoops to find it.

---
 meson.build | 1 +
 1 file changed, 1 insertion(+)
meson.build +1 -0
index f8dd3926dd8..a74e82222c9 100644
@@ -3501,6 +3501,7 @@ config_host_h = configure_file(output: 'config-host.h', configuration: config_ho
 genh += config_host_h
 
 devenv = environment()
+devenv.set('MESON_SOURCE_ROOT', meson.project_source_root())
 devenv.set('MESON_BUILD_ROOT', meson.project_build_root())
 devenv.set('VIRTUAL_ENV', meson.project_build_root() / 'pyvenv')
 devenv.prepend('PATH', meson.project_build_root() / 'pyvenv'/ 'bin')
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/6 scripts/tests: add regen-reference.sh

In reply to Alex Bennée

While we should verify references with real hardware we don't have hardware for all cases. Since the meson conversion we've made reference files mandatory so lets include a helper script to make things a bit easier to update.

---
v2
- moved the refs to the next patch

---
 scripts/tests/regen-reference.sh | 87 ++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100755 scripts/tests/regen-reference.sh
scripts/tests/regen-reference.sh new file +87 -0
new file mode 100755
index 00000000000..0600b56bef8
@@ -0,0 +1,87 @@
+#!/bin/sh
+#
+# Regenerate TCG test reference files.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+set -eu
+
+usage() {
+    cat <<EOF
+Usage: ./run $(basename "$0") [-n] <test-name-or-stub>
+
+Regenerate TCG reference (.ref) files for tests matching <test-name-or-stub>.
+This script must be executed from inside a QEMU build directory using the './run' wrapper.
+
+Options:
+  -n         Dry run (print commands without executing or overwriting files)
+  -h         Show this help message
+EOF
+    exit "${1:-1}"
+}
+
+DRY_RUN=0
+
+while getopts "nh" opt; do
+    case "$opt" in
+        n) DRY_RUN=1 ;;
+        h) usage 0 ;;
+        *) usage 1 ;;
+    esac
+done
+shift $((OPTIND - 1))
+
+if [ $# -lt 1 ]; then
+    usage 1
+fi
+
+TEST_PATTERN="$1"
+
+# Must be run using the ./run wrapper which provides MESON_SOURCE_ROOT and MESON_BUILD_ROOT
+if [ -z "${MESON_SOURCE_ROOT:-}" ] || [ -z "${MESON_BUILD_ROOT:-}" ]; then
+    echo "Error: MESON_SOURCE_ROOT or MESON_BUILD_ROOT is not set." >&2
+    echo "This script must be executed from inside a build directory using the './run' wrapper, e.g.:" >&2
+    echo "  cd <build-dir> && ./run \$PWD/../scripts/tests/regen-reference.sh ${TEST_PATTERN}" >&2
+    exit 1
+fi
+
+SRC_ROOT="$MESON_SOURCE_ROOT"
+BUILD_DIR="$MESON_BUILD_ROOT"
+
+# Ensure we are inside the build directory
+if [ ! -f "build.ninja" ] || [ "$(pwd -P)" != "$(cd "$BUILD_DIR" && pwd -P)" ]; then
+    echo "Error: This script must be executed from inside the build directory." >&2
+    echo "Please cd into your build directory and run via './run'." >&2
+    exit 1
+fi
+
+echo "!!! Warning: about to regenerate with TCG !!!"
+echo "You should verify the real hardware generates the same reference files where you can"
+
+# Query meson test list
+TEST_LIST=$(meson test --suite tcg --list "*${TEST_PATTERN}*" | grep -v "plugin")
+
+while IFS= read -r line; do
+    case "$line" in
+        *" / "*)
+            test_name="${line##* / }"
+            case "$test_name" in
+                *-linux-user-*)
+                    arch="${test_name%%-linux-user-*}"
+                    test_base="${test_name#*-linux-user-}"
+
+                    qemu_bin="./qemu-${arch}"
+                    test_bin="tests/tcg/${test_name}.test"
+                    ref_file="${SRC_ROOT}/tests/tcg/${arch}/${test_base}.ref"
+
+                    echo "${qemu_bin} ${test_bin} > ${ref_file}"
+                    if [ "$DRY_RUN" -eq 0 ]; then
+                        "${qemu_bin}" "${test_bin}" > "${ref_file}"
+                    fi
+                    ;;
+            esac
+            ;;
+    esac
+done <<EOF
+$TEST_LIST
+EOF
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH v2 2/6] scripts/tests: add regen-reference.sh

Quoted 98 lines · Alex Bennée

While we should verify references with real hardware we don't have hardware for all cases. Since the meson conversion we've made reference files mandatory so lets include a helper script to make things a bit easier to update.

---
v2
- moved the refs to the next patch

---
 scripts/tests/regen-reference.sh | 87 ++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100755 scripts/tests/regen-reference.sh
scripts/tests/regen-reference.sh new file +77 -0
new file mode 100755
index 00000000000..0600b56bef8
@@ -0,0 +1,87 @@
+#!/bin/sh
+#
+# Regenerate TCG test reference files.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+set -eu
+
+usage() {
+    cat <<EOF
+Usage: ./run $(basename "$0") [-n] <test-name-or-stub>
+
+Regenerate TCG reference (.ref) files for tests matching <test-name-or-stub>.
+This script must be executed from inside a QEMU build directory using the './run' wrapper.
+
+Options:
+  -n         Dry run (print commands without executing or overwriting files)
+  -h         Show this help message
+EOF
+    exit "${1:-1}"
+}
+
+DRY_RUN=0
+
+while getopts "nh" opt; do
+    case "$opt" in
+        n) DRY_RUN=1 ;;
+        h) usage 0 ;;
+        *) usage 1 ;;
+    esac
+done
+shift $((OPTIND - 1))
+
+if [ $# -lt 1 ]; then
+    usage 1
+fi
+
+TEST_PATTERN="$1"
+
+# Must be run using the ./run wrapper which provides MESON_SOURCE_ROOT and MESON_BUILD_ROOT
+if [ -z "${MESON_SOURCE_ROOT:-}" ] || [ -z "${MESON_BUILD_ROOT:-}" ]; then
+    echo "Error: MESON_SOURCE_ROOT or MESON_BUILD_ROOT is not set." >&2
+    echo "This script must be executed from inside a build directory using the './run' wrapper, e.g.:" >&2
+    echo "  cd <build-dir> && ./run \$PWD/../scripts/tests/regen-reference.sh ${TEST_PATTERN}" >&2
+    exit 1
+fi
+
+SRC_ROOT="$MESON_SOURCE_ROOT"
+BUILD_DIR="$MESON_BUILD_ROOT"
+
+# Ensure we are inside the build directory
+if [ ! -f "build.ninja" ] || [ "$(pwd -P)" != "$(cd "$BUILD_DIR" && pwd -P)" ]; then
+    echo "Error: This script must be executed from inside the build directory." >&2
+    echo "Please cd into your build directory and run via './run'." >&2
+    exit 1
+fi
+
+echo "!!! Warning: about to regenerate with TCG !!!"
+echo "You should verify the real hardware generates the same reference files where you can"
+
+# Query meson test list
+TEST_LIST=$(meson test --suite tcg --list "*${TEST_PATTERN}*" | grep -v "plugin")
+
+while IFS= read -r line; do
+    case "$line" in
+        *" / "*)
+            test_name="${line##* / }"
+            case "$test_name" in
+                *-linux-user-*)
+                    arch="${test_name%%-linux-user-*}"
+                    test_base="${test_name#*-linux-user-}"
+
+                    qemu_bin="./qemu-${arch}"
+                    test_bin="tests/tcg/${test_name}.test"
+                    ref_file="${SRC_ROOT}/tests/tcg/${arch}/${test_base}.ref"
+
+                    echo "${qemu_bin} ${test_bin} > ${ref_file}"

scripts/tests/regen-reference.sh line 87

Quoted 10 lines
scripts/tests/regen-reference.sh new file +10 -0
+                    if [ "$DRY_RUN" -eq 0 ]; then
+                        "${qemu_bin}" "${test_bin}" > "${ref_file}"
+                    fi
+                    ;;
+            esac
+            ;;
+    esac
+done <<EOF
+$TEST_LIST
+EOF

See original review here:
https://lore.kernel.org/qemu-devel/[email protected]/

#4 lore raw

Re: [PATCH v2 2/6] scripts/tests: add regen-reference.sh

In reply to Alex Bennée
Quoted 7 lines · Alex Bennée

While we should verify references with real hardware we don't have hardware for all cases. Since the meson conversion we've made reference files mandatory so lets include a helper script to make things a bit easier to update.

Quoted 7 lines

---
v2
- moved the refs to the next patch

---
  scripts/tests/regen-reference.sh | 87 ++++++++++++++++++++++++++++++++
  1 file changed, 87 insertions(+)

create mode 100755 scripts/tests/regen-reference.sh

I'm not keen on this.

As you say, ideally we'd *always* use hardware to generate the reference. For some targets that's impractical, but for some it isn't, and using this script is just encouraging incorrect behaviour.

I think we should never use this for hardware present in cfarm.net:

x86, aarch64, powerpc, s390x, riscv64, loongarch64

Most of those aarch64 hosts support arm32 el0, so we can include arm in there as well.

There are Sparc machines, but the non-flaky ones are Solaris, so I'm inclined to skip those.

There is are two mips64, but those are both flaky, so I'm skipping those.

r~

#5 lore raw

Re: [PATCH v2 2/6] scripts/tests: add regen-reference.sh

Quoted 26 lines · Richard Henderson
Quoted 7 lines · Alex Bennée

While we should verify references with real hardware we don't have hardware for all cases. Since the meson conversion we've made reference files mandatory so lets include a helper script to make things a bit easier to update.

Quoted 7 lines

---
v2
   - moved the refs to the next patch
---
  scripts/tests/regen-reference.sh | 87 ++++++++++++++++++++++++++++++++

  1 file changed, 87 insertions(+)

  create mode 100755 scripts/tests/regen-reference.sh

I'm not keen on this.

As you say, ideally we'd *always* use hardware to generate the reference.  For some targets that's impractical, but for some it isn't, and using this script is just encouraging incorrect behaviour.

I think we should never use this for hardware present in cfarm.net:

  x86, aarch64, powerpc, s390x, riscv64, loongarch64

Quoted 8 lines

Most of those aarch64 hosts support arm32 el0, so we can include arm in there as well.

There are Sparc machines, but the non-flaky ones are Solaris, so I'm inclined to skip those.

There is are two mips64, but those are both flaky, so I'm skipping those.

That sounds like a reasonable compromise.
A verbose output to the script to mention cfarm for people unfamiliar with it could help show the right direction also.

Quoted 3 lines

r~

Regards,
Pierrick

#6 lore raw

Re: [PATCH v2 2/6] scripts/tests: add regen-reference.sh

In reply to Richard Henderson
Quoted 22 lines · Richard Henderson
Quoted 7 lines · Alex Bennée

While we should verify references with real hardware we don't have hardware for all cases. Since the meson conversion we've made reference files mandatory so lets include a helper script to make things a bit easier to update.

Quoted 7 lines

---
v2
   - moved the refs to the next patch
---
  scripts/tests/regen-reference.sh | 87 ++++++++++++++++++++++++++++++++

  1 file changed, 87 insertions(+)

  create mode 100755 scripts/tests/regen-reference.sh

I'm not keen on this.

As you say, ideally we'd *always* use hardware to generate the reference.  For some targets that's impractical, but for some it isn't, and using this script is just encouraging incorrect behaviour.

I think we should never use this for hardware present in cfarm.net:

Quoted 7 lines

  x86, aarch64, powerpc, s390x, riscv64, loongarch64

Most of those aarch64 hosts support arm32 el0, so we can include arm in there as well.

There are Sparc machines, but the non-flaky ones are Solaris, so I'm inclined to skip those.

There is are two mips64, but those are both flaky, so I'm skipping those.

I kind of concur. Alternative is to have 2 sets of references, one from real hw machines and one from emulated ones.

I'm worried about the reference format changing and having to re-generate. Foreseeing 2-3 years from now the SPARC and HPPA machines will likely be dead; I'd however like to keep their reference files in QEMU. Can we think about that? Either have iterated refs, versioned, split in smaller tests, ...

#7 lore raw

Re: [PATCH v2 2/6] scripts/tests: add regen-reference.sh

Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Richard Henderson
Quoted 7 lines · Alex Bennée

While we should verify references with real hardware we don't have hardware for all cases. Since the meson conversion we've made reference files mandatory so lets include a helper script to make things a bit easier to update.

Quoted 7 lines

---
v2
   - moved the refs to the next patch
---
  scripts/tests/regen-reference.sh | 87 ++++++++++++++++++++++++++++++++

  1 file changed, 87 insertions(+)

  create mode 100755 scripts/tests/regen-reference.sh

I'm not keen on this.

As you say, ideally we'd *always* use hardware to generate the reference.  For some targets that's impractical, but for some it isn't, and using this script is just encouraging incorrect behaviour.

I think we should never use this for hardware present in cfarm.net:

   x86, aarch64, powerpc, s390x, riscv64, loongarch64

Quoted 7 lines

Most of those aarch64 hosts support arm32 el0, so we can include arm in there as well.

There are Sparc machines, but the non-flaky ones are Solaris, so I'm inclined to skip those.

There is are two mips64, but those are both flaky, so I'm skipping those.

Quoted 9 lines

I kind of concur. Alternative is to have 2 sets of references, one from real hw machines and one from emulated ones.

I'm worried about the reference format changing and having to re-generate. Foreseeing 2-3 years from now the SPARC and HPPA machines will likely be dead; I'd however like to keep their reference files in QEMU. Can we think about that? Either have iterated refs, versioned, split in smaller tests, ...

Split it with more tests (optionally enabled for some targets if can't be enabled for all) seems like the right way to go.

Regards,
Pierrick

#8 lore raw

PATCH v2 3/6 tests/tcg: extend the float_madds test case

In reply to Alex Bennée

We have a bug report about a failing case when using a broken mingw-w64 and in the upstream bug there where some additional test cases added. Import them into our *-user test so we can verify the fix later.

I've regenerated the references with the new TCG helper script but I have verified they are correct for arm, aarch64, ppc64 and s390x.

---
v2
- fixed the refs

---
 tests/tcg/multiarch/float_madds.c     | 13 ++++++
 tests/tcg/aarch64/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/alpha/float_madds.ref       | 64 +++++++++++++++++++++++++++
 tests/tcg/arm/float_madds.ref         | 64 +++++++++++++++++++++++++++
 tests/tcg/hexagon/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/hppa/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/i386/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/loongarch64/float_madds.ref | 64 +++++++++++++++++++++++++++
 tests/tcg/m68k/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/mips/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/mips64/float_madds.ref      | 64 +++++++++++++++++++++++++++
 tests/tcg/mips64el/float_madds.ref    | 64 +++++++++++++++++++++++++++
 tests/tcg/mipsel/float_madds.ref      | 64 +++++++++++++++++++++++++++
 tests/tcg/or1k/float_madds.ref        | 16 +++++++
 tests/tcg/ppc64/float_madds.ref       | 64 +++++++++++++++++++++++++++
 tests/tcg/ppc64le/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/riscv64/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/s390x/float_madds.ref       | 64 +++++++++++++++++++++++++++
 tests/tcg/sh4/float_madds.ref         | 32 ++++++++++++++
 tests/tcg/x86_64/float_madds.ref      | 64 +++++++++++++++++++++++++++
 20 files changed, 1149 insertions(+)
tests/tcg/multiarch/float_madds.c +13 -0
index a692e052d5b..47d9847b573 100644
@@ -99,6 +99,19 @@ int main(int argc, char *argv[argc])
         printf("# LP184149\n");
         do_madds(0x1.ffffffffffffcp-1022, 0x1.0000000000001p-1, 0x0.0000000000001p-1022, j, 0);
         do_madds(0x8p-152, 0x8p-152, 0x8p-152, j+1, 0);
+
+        /* From https://gitlab.com/qemu-project/qemu/-/issues/5023 */
+        printf("# Issue 5023\n");
+        do_madds(FLT_MAX, 2.0f, -FLT_MAX, j+2, 0);
+
+        /* From https://gist.github.com/minoki/ff4412f2a385eab3c7e689acd589ea3e */
+        printf("# Gist cases\n");
+        do_madds(0x1p100f, 0x1p100f, -INFINITY, j+3, 0);
+        do_madds(0x1.fffffep23f, 0x1.000004p28f, 0x1.fcp5f, j+4, 0);
+        do_madds(0x1.84ae3p125f, 0x1.6p-141f, 0x1p-149f, j+5, 0);
+        do_madds(0x1.00001p50f, 0x1.1p50f, 0x1p-149f, j+6, 0);
+        do_madds(0x1.000002p50f, 0x1.8p50f, -0x1p-149f, j+7, 0);
+        do_madds(0x1.83bd78p4f, -0x1.cp118f, -0x1.344108p-2f, j+8, 0);
     }
 
     return 0;
tests/tcg/aarch64/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/alpha/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/arm/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/hexagon/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/hppa/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/i386/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/loongarch64/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/m68k/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/mips/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/mips64/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/mips64el/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/mipsel/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/or1k/float_madds.ref +16 -0
index 2c7c35c8b47..b94d65c19e4 100644
@@ -190,3 +190,19 @@ op : f32(0x0.00000000000000000000p+0:0000000000) * f32(0x1.00000000000000000000p
 res: f32(0x0.00000000000000000000p+0:0000000000) flags=OK (31/0)
 op : f32(0x1.00000000000000000000p-149:0x00000001) * f32(0x1.00000000000000000000p-149:0x00000001) + f32(0x1.00000000000000000000p-149:0x00000001)
 res: f32(0x1.00000000000000000000p-149:0x00000001) flags=OK (32/0)
+# Issue 5023
+op : f32(0x1.fffffe00000000000000p+127:0x7f7fffff) * f32(0x1.00000000000000000000p+1:0x40000000) + f32(-0x1.fffffe00000000000000p+127:0xff7fffff)
+res: f32(0x1.fffffe00000000000000p+127:0x7f7fffff) flags=OK (33/0)
+# Gist cases
+op : f32(0x1.00000000000000000000p+100:0x71800000) * f32(0x1.00000000000000000000p+100:0x71800000) + f32(-inf:0xff800000)
+res: f32(-inf:0xff800000) flags=OK (34/0)
+op : f32(0x1.fffffe00000000000000p+23:0x4b7fffff) * f32(0x1.00000400000000000000p+28:0x4d800002) + f32(0x1.fc000000000000000000p+5:0x427e0000)
+res: f32(0x1.00000200000000000000p+52:0x59800001) flags=OK (35/0)
+op : f32(0x1.84ae3000000000000000p+125:0x7e425718) * f32(0x1.60000000000000000000p-141:0x00000160) + f32(0x1.00000000000000000000p-149:0x00000001)
+res: f32(0x1.0b37c200000000000000p-15:0x38059be1) flags=OK (36/0)
+op : f32(0x1.00001000000000000000p+50:0x58800008) * f32(0x1.10000000000000000000p+50:0x58880000) + f32(0x1.00000000000000000000p-149:0x00000001)
+res: f32(0x1.10001200000000000000p+100:0x71880009) flags=OK (37/0)
+op : f32(0x1.00000200000000000000p+50:0x58800001) * f32(0x1.80000000000000000000p+50:0x58c00000) + f32(-0x1.00000000000000000000p-149:0x80000001)
+res: f32(0x1.80000200000000000000p+100:0x71c00001) flags=OK (38/0)
+op : f32(0x1.83bd7800000000000000p+4:0x41c1debc) * f32(-0x1.c0000000000000000000p+118:0xfae00000) + f32(-0x1.34410800000000000000p-2:0xbe9a2084)
+res: f32(-0x1.5345ca00000000000000p+123:0xfd29a2e5) flags=OK (39/0)
tests/tcg/ppc64/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/ppc64le/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/riscv64/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/s390x/float_madds.ref +64 -0

85 lines not on this page · raw message

tests/tcg/sh4/float_madds.ref +32 -0
index 6d96a2a5929..ceed6008ff8 100644
@@ -190,6 +190,22 @@ op : f32(0x0.00000000000000000000p+0:0000000000) * f32(0x1.00000000000000000000p
 res: f32(0x0.00000000000000000000p+0:0000000000) flags=OK (31/0)
 op : f32(0x1.00000000000000000000p-149:0x00000001) * f32(0x1.00000000000000000000p-149:0x00000001) + f32(0x1.00000000000000000000p-149:0x00000001)
 res: f32(0x1.00000000000000000000p-149:0x00000001) flags=UNDERFLOW INEXACT  (32/0)
+# Issue 5023
+op : f32(0x1.fffffe00000000000000p+127:0x7f7fffff) * f32(0x1.00000000000000000000p+1:0x40000000) + f32(-0x1.fffffe00000000000000p+127:0xff7fffff)
+res: f32(0x1.fffffe00000000000000p+127:0x7f7fffff) flags=OK (33/0)
+# Gist cases
+op : f32(0x1.00000000000000000000p+100:0x71800000) * f32(0x1.00000000000000000000p+100:0x71800000) + f32(-inf:0xff800000)
+res: f32(-inf:0xff800000) flags=OK (34/0)
+op : f32(0x1.fffffe00000000000000p+23:0x4b7fffff) * f32(0x1.00000400000000000000p+28:0x4d800002) + f32(0x1.fc000000000000000000p+5:0x427e0000)
+res: f32(0x1.00000200000000000000p+52:0x59800001) flags=INEXACT  (35/0)
+op : f32(0x1.84ae3000000000000000p+125:0x7e425718) * f32(0x1.60000000000000000000p-141:0x00000160) + f32(0x1.00000000000000000000p-149:0x00000001)
+res: f32(0x1.0b37c200000000000000p-15:0x38059be1) flags=INEXACT  (36/0)
+op : f32(0x1.00001000000000000000p+50:0x58800008) * f32(0x1.10000000000000000000p+50:0x58880000) + f32(0x1.00000000000000000000p-149:0x00000001)
+res: f32(0x1.10001200000000000000p+100:0x71880009) flags=INEXACT  (37/0)
+op : f32(0x1.00000200000000000000p+50:0x58800001) * f32(0x1.80000000000000000000p+50:0x58c00000) + f32(-0x1.00000000000000000000p-149:0x80000001)
+res: f32(0x1.80000200000000000000p+100:0x71c00001) flags=INEXACT  (38/0)
+op : f32(0x1.83bd7800000000000000p+4:0x41c1debc) * f32(-0x1.c0000000000000000000p+118:0xfae00000) + f32(-0x1.34410800000000000000p-2:0xbe9a2084)
+res: f32(-0x1.5345ca00000000000000p+123:0xfd29a2e5) flags=INEXACT  (39/0)
 ### Rounding to zero
 op : f32(nan:0xffa00000) * f32(nan:0xffc00000) + f32(-inf:0xff800000)
 res: f32(nan:0x7fbfffff) flags=INVALID (0/0)
@@ -382,3 +398,19 @@ op : f32(0x0.00000000000000000000p+0:0000000000) * f32(0x1.00000000000000000000p
 res: f32(0x0.00000000000000000000p+0:0000000000) flags=OK (31/0)
 op : f32(0x1.00000000000000000000p-149:0x00000001) * f32(0x1.00000000000000000000p-149:0x00000001) + f32(0x1.00000000000000000000p-149:0x00000001)
 res: f32(0x1.00000000000000000000p-149:0x00000001) flags=UNDERFLOW INEXACT  (32/0)
+# Issue 5023
+op : f32(0x1.fffffe00000000000000p+127:0x7f7fffff) * f32(0x1.00000000000000000000p+1:0x40000000) + f32(-0x1.fffffe00000000000000p+127:0xff7fffff)
+res: f32(0x1.fffffe00000000000000p+127:0x7f7fffff) flags=OK (33/0)
+# Gist cases
+op : f32(0x1.00000000000000000000p+100:0x71800000) * f32(0x1.00000000000000000000p+100:0x71800000) + f32(-inf:0xff800000)
+res: f32(-inf:0xff800000) flags=OK (34/0)
+op : f32(0x1.fffffe00000000000000p+23:0x4b7fffff) * f32(0x1.00000400000000000000p+28:0x4d800002) + f32(0x1.fc000000000000000000p+5:0x427e0000)
+res: f32(0x1.00000200000000000000p+52:0x59800001) flags=INEXACT  (35/0)
+op : f32(0x1.84ae3000000000000000p+125:0x7e425718) * f32(0x1.60000000000000000000p-141:0x00000160) + f32(0x1.00000000000000000000p-149:0x00000001)
+res: f32(0x1.0b37c000000000000000p-15:0x38059be0) flags=INEXACT  (36/0)
+op : f32(0x1.00001000000000000000p+50:0x58800008) * f32(0x1.10000000000000000000p+50:0x58880000) + f32(0x1.00000000000000000000p-149:0x00000001)
+res: f32(0x1.10001000000000000000p+100:0x71880008) flags=INEXACT  (37/0)
+op : f32(0x1.00000200000000000000p+50:0x58800001) * f32(0x1.80000000000000000000p+50:0x58c00000) + f32(-0x1.00000000000000000000p-149:0x80000001)
+res: f32(0x1.80000200000000000000p+100:0x71c00001) flags=INEXACT  (38/0)
+op : f32(0x1.83bd7800000000000000p+4:0x41c1debc) * f32(-0x1.c0000000000000000000p+118:0xfae00000) + f32(-0x1.34410800000000000000p-2:0xbe9a2084)
+res: f32(-0x1.5345c800000000000000p+123:0xfd29a2e4) flags=INEXACT  (39/0)
tests/tcg/x86_64/float_madds.ref +64 -0

85 lines not on this page · raw message

Signature · 1 line
2.47.3
Reviewed-by on v2 3/6
#9 lore raw

Re: [PATCH v2 3/6] tests/tcg: extend the float_madds test case

Quoted 28 lines · Alex Bennée

We have a bug report about a failing case when using a broken mingw-w64 and in the upstream bug there where some additional test cases added. Import them into our *-user test so we can verify the fix later.

I've regenerated the references with the new TCG helper script but I have verified they are correct for arm, aarch64, ppc64 and s390x.

---
v2
- fixed the refs

---
 tests/tcg/multiarch/float_madds.c     | 13 ++++++
 tests/tcg/aarch64/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/alpha/float_madds.ref       | 64 +++++++++++++++++++++++++++
 tests/tcg/arm/float_madds.ref         | 64 +++++++++++++++++++++++++++
 tests/tcg/hexagon/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/hppa/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/i386/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/loongarch64/float_madds.ref | 64 +++++++++++++++++++++++++++
 tests/tcg/m68k/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/mips/float_madds.ref        | 64 +++++++++++++++++++++++++++
 tests/tcg/mips64/float_madds.ref      | 64 +++++++++++++++++++++++++++
 tests/tcg/mips64el/float_madds.ref    | 64 +++++++++++++++++++++++++++
 tests/tcg/mipsel/float_madds.ref      | 64 +++++++++++++++++++++++++++
Quoted 9 lines
 tests/tcg/or1k/float_madds.ref        | 16 +++++++
 tests/tcg/ppc64/float_madds.ref       | 64 +++++++++++++++++++++++++++
 tests/tcg/ppc64le/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/riscv64/float_madds.ref     | 64 +++++++++++++++++++++++++++
 tests/tcg/s390x/float_madds.ref       | 64 +++++++++++++++++++++++++++
 tests/tcg/sh4/float_madds.ref         | 32 ++++++++++++++
 tests/tcg/x86_64/float_madds.ref      | 64 +++++++++++++++++++++++++++
 20 files changed, 1149 insertions(+)
#10 lore raw

Re: [PATCH v2 3/6] tests/tcg: extend the float_madds test case

In reply to Alex Bennée
Quoted 48 lines · Alex Bennée

We have a bug report about a failing case when using a broken mingw-w64 and in the upstream bug there where some additional test cases added. Import them into our *-user test so we can verify the fix later.

I've regenerated the references with the new TCG helper script but I have verified they are correct for arm, aarch64, ppc64 and s390x.

---
v2
- fixed the refs

---
  tests/tcg/multiarch/float_madds.c     | 13 ++++++
  tests/tcg/aarch64/float_madds.ref     | 64 +++++++++++++++++++++++++++
  tests/tcg/alpha/float_madds.ref       | 64 +++++++++++++++++++++++++++
  tests/tcg/arm/float_madds.ref         | 64 +++++++++++++++++++++++++++
  tests/tcg/hexagon/float_madds.ref     | 64 +++++++++++++++++++++++++++
  tests/tcg/hppa/float_madds.ref        | 64 +++++++++++++++++++++++++++
  tests/tcg/i386/float_madds.ref        | 64 +++++++++++++++++++++++++++
  tests/tcg/loongarch64/float_madds.ref | 64 +++++++++++++++++++++++++++
  tests/tcg/m68k/float_madds.ref        | 64 +++++++++++++++++++++++++++
  tests/tcg/mips/float_madds.ref        | 64 +++++++++++++++++++++++++++
  tests/tcg/mips64/float_madds.ref      | 64 +++++++++++++++++++++++++++
  tests/tcg/mips64el/float_madds.ref    | 64 +++++++++++++++++++++++++++
  tests/tcg/mipsel/float_madds.ref      | 64 +++++++++++++++++++++++++++
  tests/tcg/or1k/float_madds.ref        | 16 +++++++
  tests/tcg/ppc64/float_madds.ref       | 64 +++++++++++++++++++++++++++
  tests/tcg/ppc64le/float_madds.ref     | 64 +++++++++++++++++++++++++++
  tests/tcg/riscv64/float_madds.ref     | 64 +++++++++++++++++++++++++++
  tests/tcg/s390x/float_madds.ref       | 64 +++++++++++++++++++++++++++
  tests/tcg/sh4/float_madds.ref         | 32 ++++++++++++++
  tests/tcg/x86_64/float_madds.ref      | 64 +++++++++++++++++++++++++++
  20 files changed, 1149 insertions(+)
tests/tcg/multiarch/float_madds.c +3 -0
index a692e052d5b..47d9847b573 100644
@@ -99,6 +99,19 @@ int main(int argc, char *argv[argc])
          printf("# LP184149\n");
          do_madds(0x1.ffffffffffffcp-1022, 0x1.0000000000001p-1, 0x0.0000000000001p-1022, j, 0);
          do_madds(0x8p-152, 0x8p-152, 0x8p-152, j+1, 0);
+
+        /* From https://gitlab.com/qemu-project/qemu/-/issues/5023 */
+        printf("# Issue 5023\n");

tests/tcg/multiarch/float_madds.c int main(int argc, char *argv[argc]) · line 114

Quoted 10 lines
tests/tcg/multiarch/float_madds.c +10 -0
+        do_madds(FLT_MAX, 2.0f, -FLT_MAX, j+2, 0);
+
+        /* From https://gist.github.com/minoki/ff4412f2a385eab3c7e689acd589ea3e */
+        printf("# Gist cases\n");
+        do_madds(0x1p100f, 0x1p100f, -INFINITY, j+3, 0);
+        do_madds(0x1.fffffep23f, 0x1.000004p28f, 0x1.fcp5f, j+4, 0);
+        do_madds(0x1.84ae3p125f, 0x1.6p-141f, 0x1p-149f, j+5, 0);
+        do_madds(0x1.00001p50f, 0x1.1p50f, 0x1p-149f, j+6, 0);
+        do_madds(0x1.000002p50f, 0x1.8p50f, -0x1p-149f, j+7, 0);
+        do_madds(0x1.83bd78p4f, -0x1.cp118f, -0x1.344108p-2f, j+8, 0);

I think it would be better to reduce our reliance on reference data rather than increase it.

The test case under issue has an exact result, no need for any reference. Why not put this in its own 2 line test case?

r~

#11 lore raw

PATCH v2 4/6 fpu/softfloat: gate hardfloat on working fmaf

In reply to Alex Bennée

We already had a test against fma to catch broken glibc's but apparently there are mingw64 cases that are broken for fmaf on Windows. Add a test to softfloat_init so we can disable if we detect the brokenness.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/5023

---
 fpu/softfloat.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fpu/softfloat.c +10 -0
index bc244a44ffb..4531de5e6cf 100644
@@ -5161,4 +5161,14 @@ static void __attribute__((constructor)) softfloat_init(void)
     if (ur.s != 0x0020000000000001ULL) {
         force_soft_fma = true;
     }
+
+    /*
+     * Test that the host's fmaf is not obviously broken. For example,
+     * mingw-w64 can perform an incorrect fmaf on certain hosts; see
+     *   https://gitlab.com/qemu-project/qemu/-/issues/5023
+     *   https://sourceforge.net/p/mingw-w64/bugs/848/
+     */
+    if (fmaf(FLT_MAX, 2.0f, -FLT_MAX) != FLT_MAX) {
+        force_soft_fma = true;
+    }
 }
Signature · 1 line
2.47.3
Reviewed-by on v2 4/6
#12 lore raw

Re: [PATCH v2 4/6] fpu/softfloat: gate hardfloat on working fmaf

Quoted 21 lines · Alex Bennée

We already had a test against fma to catch broken glibc's but apparently there are mingw64 cases that are broken for fmaf on Windows. Add a test to softfloat_init so we can disable if we detect the brokenness.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/5023

---
  fpu/softfloat.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
fpu/softfloat.c +1 -0
index bc244a44ffb..4531de5e6cf 100644
@@ -5161,4 +5161,14 @@ static void __attribute__((constructor)) softfloat_init(void)
      if (ur.s != 0x0020000000000001ULL) {
          force_soft_fma = true;
      }
+

fpu/softfloat.c static void __attribute__((constructor)) softfloat_init(void) · line 5174

Quoted 10 lines
fpu/softfloat.c +9 -0
+    /*
+     * Test that the host's fmaf is not obviously broken. For example,
+     * mingw-w64 can perform an incorrect fmaf on certain hosts; see
+     *   https://gitlab.com/qemu-project/qemu/-/issues/5023
+     *   https://sourceforge.net/p/mingw-w64/bugs/848/
+     */
+    if (fmaf(FLT_MAX, 2.0f, -FLT_MAX) != FLT_MAX) {
+        force_soft_fma = true;
+    }
  }

r~

#13 lore raw

PATCH v2 5/6 fpu: promote force_soft_fma to force_soft_float

In reply to Alex Bennée

Rather than only tweaking fma allow any hardfloat test failure in init to force back the softfloat path. This is going to be used for testing.

---
 include/fpu/softfloat.h |  6 ++++++
 fpu/softfloat.c         | 18 +++++-------------
 2 files changed, 11 insertions(+), 13 deletions(-)
include/fpu/softfloat.h +6 -0
index d48fc6b0437..f702ae88b2e 100644
@@ -1401,4 +1401,10 @@ MINMAX_2(float128)
 #undef MINMAX_1
 #undef MINMAX_2
 
+/*
+ * When set to true, disable hardfloat optimizations and force all FP operations
+ * to use the software emulation routines.
+ */
+extern bool force_soft_float;
+
 #endif /* SOFTFLOAT_H */
fpu/softfloat.c +5 -13
index 4531de5e6cf..febc16bd904 100644
@@ -228,9 +228,11 @@ GEN_INPUT_FLUSH3(float64_input_flush3, float64)
 # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN __attribute__((noinline))
 #endif
 
+bool force_soft_float;
+
 static inline bool can_use_fpu(const float_status *s)
 {
-    if (QEMU_NO_HARDFLOAT) {
+    if (QEMU_NO_HARDFLOAT || unlikely(force_soft_float)) {
         return false;
     }
     return likely(s->float_exception_flags & float_flag_inexact &&
@@ -1984,8 +1986,6 @@ float64_muladd_scalbn(float64 a, float64 b, float64 c,
     return pack_raw64(&pr, &float64_params);
 }
 
-static bool force_soft_fma;
-
 float32 QEMU_FLATTEN
 float32_muladd(float32 xa, float32 xb, float32 xc, int flags, float_status *s)
 {
@@ -2007,10 +2007,6 @@ float32_muladd(float32 xa, float32 xb, float32 xc, int flags, float_status *s)
         goto soft;
     }
 
-    if (unlikely(force_soft_fma)) {
-        goto soft;
-    }
-
     /*
      * When (a || b) == 0, there's no need to check for under/over flow,
      * since we know the addend is (normal || 0) and the product is 0.
@@ -2075,10 +2071,6 @@ float64_muladd(float64 xa, float64 xb, float64 xc, int flags, float_status *s)
         goto soft;
     }
 
-    if (unlikely(force_soft_fma)) {
-        goto soft;
-    }
-
     /*
      * When (a || b) == 0, there's no need to check for under/over flow,
      * since we know the addend is (normal || 0) and the product is 0.
@@ -5159,7 +5151,7 @@ static void __attribute__((constructor)) softfloat_init(void)
     uc.s = 0x0020000000000000ULL;
     ur.h = fma(ua.h, ub.h, uc.h);
     if (ur.s != 0x0020000000000001ULL) {
-        force_soft_fma = true;
+        force_soft_float = true;
     }
 
     /*
@@ -5169,6 +5161,6 @@ static void __attribute__((constructor)) softfloat_init(void)
      *   https://sourceforge.net/p/mingw-w64/bugs/848/
      */
     if (fmaf(FLT_MAX, 2.0f, -FLT_MAX) != FLT_MAX) {
-        force_soft_fma = true;
+        force_soft_float = true;
     }
 }
Signature · 1 line
2.47.3
#14 lore raw

Re: [PATCH v2 5/6] fpu: promote force_soft_fma to force_soft_float

Quoted 9 lines · Alex Bennée

Rather than only tweaking fma allow any hardfloat test failure in init to force back the softfloat path. This is going to be used for testing.

---
  include/fpu/softfloat.h |  6 ++++++
  fpu/softfloat.c         | 18 +++++-------------
  2 files changed, 11 insertions(+), 13 deletions(-)

Why should a libc's broken fma disable the use of hardware addition?

Nack to that.

I'm open to a function, void force_soft_float_for_testing(void), which sets both force_soft_fma and a new force_soft_float variable to true.

r~

#15 lore raw

PATCH v2 6/6 tests/fp: add fp-test-hard

In reply to Alex Bennée

We have had reports of hardfloat failures with various broken toolchains. To avoid finding this out while running a translated guest add a new test case to exercise the hardfloat code.

It iterates through a bunch of operations with a mixture of random and edge case floats and runs the same operation while toggling force_soft_float. It also ensures the status flags are set so hardfloat can run. Any difference dump the output and exits with a failing error code.

This is probably overkill at the moment as the toolchain errors tend to be with places with libc gets involved with fallback emulation (e.g. fma) rather than the raw ops themselves. However testing all the potential hardfloat ops at least gives us a reference point if enabling hardfloat ops on new host architectures.

AI-used-for: initial prototype, based off fp-test/fp-bench

---
 tests/fp/fp-test-hard.c | 702 ++++++++++++++++++++++++++++++++++++++++
 tests/fp/meson.build    |  30 +-
 2 files changed, 722 insertions(+), 10 deletions(-)
 create mode 100644 tests/fp/fp-test-hard.c
tests/fp/fp-test-hard.c new file +702 -0
new file mode 100644
index 00000000000..f863f00554b
@@ -0,0 +1,702 @@
+/*
+ * fp-test-hard.c - test QEMU's hardfloat against softfloat implementation
+ *
+ * Copyright (C) 2026, Linaro, Ltd.
+ *
+ * License: GNU GPL, version 2 or later.
+ *   See the COPYING file in the top-level directory.
+ */
+#ifndef HW_POISON_H
+#error Must define HW_POISON_H to work around TARGET_* poisoning
+#endif
+
+#include "qemu/osdep.h"
+#include <float.h>
+#include "fpu/softfloat.h"
+
+#define NUM_OP_ITERATIONS     50000
+#define NUM_CONV_ITERATIONS   20000
+
+static unsigned int total_tests;
+
+typedef union {
+    float32 s;
+    float f;
+    uint32_t u;
+} ufloat32;
+
+typedef union {
+    float64 s;
+    double d;
+    uint64_t u;
+} ufloat64;
+
+static uint64_t prng_state = 0x123456789abcdef0ULL;
+
+static inline uint64_t prng_next(void)
+{
+    /* 64-bit xorshift */
+    uint64_t x = prng_state;
+    x ^= x << 13;
+    x ^= x >> 7;
+    x ^= x << 17;
+    prng_state = x;
+    return x;
+}
+
+static inline float32 prng_normal_f32(void)
+{
+    ufloat32 u;
+    do {
+        u.u = (uint32_t)prng_next();
+    } while (!float32_is_zero_or_normal(u.s));
+    return u.s;
+}
+
+static inline float64 prng_normal_f64(void)
+{
+    ufloat64 u;
+    do {
+        u.u = prng_next();
+    } while (!float64_is_zero_or_normal(u.s));
+    return u.s;
+}
+
+/* Static edge test inputs */
+static const float32 f32_edge_inputs[] = {
+    0x00000000, /* +0.0 */
+    0x80000000, /* -0.0 */
+    0x3f800000, /*  1.0 */
+    0xbf800000, /* -1.0 */
+    0x40000000, /*  2.0 */
+    0xc0000000, /* -2.0 */
+    0x3f000000, /*  0.5 */
+    0xbf000000, /* -0.5 */
+    0x40400000, /*  3.0 */
+    0x41200000, /* 10.0 */
+    0x42c80000, /* 100.0 */
+    0x00800000, /* FLT_MIN */
+    0x01000000, /* 2 * FLT_MIN */
+    0x7f7fffff, /* FLT_MAX */
+    0xff7fffff, /* -FLT_MAX */
+    0x7f7ffffe, /* nextafterf(FLT_MAX, 0) */
+    0x7effffff, /* FLT_MAX / 2 */
+    0x3eaaaaab, /* 1.0 / 3.0 */
+};
+
+static const float64 f64_edge_inputs[] = {
+    0x0000000000000000ULL, /* +0.0 */
+    0x8000000000000000ULL, /* -0.0 */
+    0x3ff0000000000000ULL, /*  1.0 */
+    0xbff0000000000000ULL, /* -1.0 */
+    0x4000000000000000ULL, /*  2.0 */
+    0xc000000000000000ULL, /* -2.0 */
+    0x3fe0000000000000ULL, /*  0.5 */
+    0xbfe0000000000000ULL, /* -0.5 */
+    0x4008000000000000ULL, /*  3.0 */
+    0x4024000000000000ULL, /* 10.0 */
+    0x4059000000000000ULL, /* 100.0 */
+    0x0010000000000000ULL, /* DBL_MIN */
+    0x0020000000000000ULL, /* 2 * DBL_MIN */
+    0x7fefffffffffffffULL, /* DBL_MAX */
+    0xffefffffffffffffULL, /* -DBL_MAX */
+    0x7feffffffffffffeULL, /* nextafter(DBL_MAX, 0) */
+    0x7fdfffffffffffffULL, /* DBL_MAX / 2 */
+    0x3fd5555555555555ULL, /* 1.0 / 3.0 */
+};
+
+/* Integer test inputs */
+static const int64_t int_edge_inputs[] = {
+    0, 1, -1, 2, -2, 3, -3, 10, -10, 100, -100,
+    INT16_MIN, INT16_MAX,
+    INT32_MIN, INT32_MAX,
+    INT64_MIN, INT64_MAX,
+    (1LL << 24) - 1, (1LL << 24), (1LL << 24) + 1,
+    (1LL << 53) - 1, (1LL << 53), (1LL << 53) + 1,
+};
+
+static const uint64_t uint_edge_inputs[] = {
+    0, 1, 2, 3, 10, 100,
+    UINT16_MAX,
+    UINT32_MAX,
+    UINT64_MAX,
+    (1ULL << 24) - 1, (1ULL << 24), (1ULL << 24) + 1,
+    (1ULL << 53) - 1, (1ULL << 53), (1ULL << 53) + 1,
+};
+
+/* Mix edge inputs with pseudo-random normal inputs */
+static inline float32 get_mixed_f32(void)
+{
+    /* 25% chance of returning an edge input */
+    if ((prng_next() & 3) == 0) {
+        size_t idx = prng_next() % ARRAY_SIZE(f32_edge_inputs);
+        return f32_edge_inputs[idx];
+    }
+    return prng_normal_f32();
+}
+
+static inline float64 get_mixed_f64(void)
+{
+    if ((prng_next() & 3) == 0) {
+        size_t idx = prng_next() % ARRAY_SIZE(f64_edge_inputs);
+        return f64_edge_inputs[idx];
+    }
+    return prng_normal_f64();
+}
+
+static inline int64_t get_mixed_int(void)
+{
+    if ((prng_next() & 3) == 0) {
+        size_t idx = prng_next() % ARRAY_SIZE(int_edge_inputs);
+        return int_edge_inputs[idx];
+    }
+    return (int64_t)prng_next();
+}
+
+static inline uint64_t get_mixed_uint(void)
+{
+    if ((prng_next() & 3) == 0) {
+        size_t idx = prng_next() % ARRAY_SIZE(uint_edge_inputs);
+        return uint_edge_inputs[idx];
+    }
+    return prng_next();
+}
+
+static inline void init_status(float_status *s)
+{
+    memset(s, 0, sizeof(*s));
+    /*
+     * can_use_fpu requires inexact flag to be already set and rounding mode
+     * to be float_round_nearest_even.
+     */
+    s->float_exception_flags = float_flag_inexact;
+    set_float_rounding_mode(float_round_nearest_even, s);
+    set_float_2nan_prop_rule(float_2nan_prop_s_ab, s);
+    set_float_3nan_prop_rule(float_3nan_prop_s_cab, s);
+    set_float_default_nan_pattern(0b01000000, s);
+    set_float_infzeronan_rule(float_infzeronan_dnan_if_qnan, s);
+}
+
+static G_NORETURN void report_fail_f32(const char *op_name,
+                                      float32 soft_res, uint8_t soft_flags,
+                                      float32 hard_res, uint8_t hard_flags,
+                                      const char *inputs_str)
+{
+    fprintf(stderr, "FAIL: %s\n", op_name);
+    fprintf(stderr, "  inputs: %s\n", inputs_str);
+    fprintf(stderr, "  soft: res=0x%08" PRIx32 " (flags=0x%02x)\n",
+            soft_res, soft_flags);
+    fprintf(stderr, "  hard: res=0x%08" PRIx32 " (flags=0x%02x)\n",
+            hard_res, hard_flags);
+    exit(EXIT_FAILURE);
+}
+
+static G_NORETURN void report_fail_f64(const char *op_name,
+                                      float64 soft_res, uint8_t soft_flags,
+                                      float64 hard_res, uint8_t hard_flags,
+                                      const char *inputs_str)
+{
+    fprintf(stderr, "FAIL: %s\n", op_name);
+    fprintf(stderr, "  inputs: %s\n", inputs_str);
+    fprintf(stderr, "  soft: res=0x%016" PRIx64 " (flags=0x%02x)\n",
+            soft_res, soft_flags);
+    fprintf(stderr, "  hard: res=0x%016" PRIx64 " (flags=0x%02x)\n",
+            hard_res, hard_flags);
+    exit(EXIT_FAILURE);
+}
+
+static inline void check_f32(const char *op_name,
+                             float32 soft_res, uint8_t soft_flags,
+                             float32 hard_res, uint8_t hard_flags,
+                             const char *inputs_str)
+{
+    total_tests++;
+    if (unlikely(soft_res != hard_res || soft_flags != hard_flags)) {
+        report_fail_f32(op_name, soft_res, soft_flags,
+                        hard_res, hard_flags, inputs_str);
+    }
+}
+
+static inline void check_f64(const char *op_name,
+                             float64 soft_res, uint8_t soft_flags,
+                             float64 hard_res, uint8_t hard_flags,
+                             const char *inputs_str)
+{
+    total_tests++;
+    if (unlikely(soft_res != hard_res || soft_flags != hard_flags)) {
+        report_fail_f64(op_name, soft_res, soft_flags,
+                        hard_res, hard_flags, inputs_str);
+    }
+}
+
+static void test_f32_op2(const char *op_name,
+                         float32 (*op)(float32, float32, float_status *),
+                         float32 a, float32 b)
+{
+    float_status status_soft, status_hard;
+    float32 res_soft, res_hard;
+    char inp[128];
+
+    init_status(&status_soft);
+    init_status(&status_hard);
+
+    force_soft_float = true;
+    res_soft = op(a, b, &status_soft);
+
+    force_soft_float = false;
+    res_hard = op(a, b, &status_hard);
+
+    snprintf(inp, sizeof(inp), "a=0x%08" PRIx32 ", b=0x%08" PRIx32, a, b);
+    check_f32(op_name, res_soft, status_soft.float_exception_flags,
+              res_hard, status_hard.float_exception_flags, inp);
+}
+
+static void test_f64_op2(const char *op_name,
+                         float64 (*op)(float64, float64, float_status *),
+                         float64 a, float64 b)
+{
+    float_status status_soft, status_hard;
+    float64 res_soft, res_hard;
+    char inp[128];
+
+    init_status(&status_soft);
+    init_status(&status_hard);
+
+    force_soft_float = true;
+    res_soft = op(a, b, &status_soft);
+
+    force_soft_float = false;
+    res_hard = op(a, b, &status_hard);
+
+    snprintf(inp, sizeof(inp), "a=0x%016" PRIx64 ", b=0x%016" PRIx64, a, b);
+    check_f64(op_name, res_soft, status_soft.float_exception_flags,
+              res_hard, status_hard.float_exception_flags, inp);
+}
+
+static void test_f32_muladd_case(float32 a, float32 b, float32 c, int flags)
+{
+    float_status status_soft, status_hard;
+    float32 res_soft, res_hard;
+    char inp[160];
+
+    init_status(&status_soft);
+    init_status(&status_hard);
+
+    force_soft_float = true;
+    res_soft = float32_muladd(a, b, c, flags, &status_soft);
+
+    force_soft_float = false;
+    res_hard = float32_muladd(a, b, c, flags, &status_hard);
+
+    snprintf(inp, sizeof(inp),
+             "a=0x%08" PRIx32 ", b=0x%08" PRIx32 ", c=0x%08" PRIx32
+             ", flags=0x%x", a, b, c, flags);
+    check_f32("f32_muladd", res_soft, status_soft.float_exception_flags,
+              res_hard, status_hard.float_exception_flags, inp);
+}
+
+static void test_f64_muladd_case(float64 a, float64 b, float64 c, int flags)
+{
+    float_status status_soft, status_hard;
+    float64 res_soft, res_hard;
+    char inp[180];
+
+    init_status(&status_soft);
+    init_status(&status_hard);
+
+    force_soft_float = true;
+    res_soft = float64_muladd(a, b, c, flags, &status_soft);
+
+    force_soft_float = false;
+    res_hard = float64_muladd(a, b, c, flags, &status_hard);
+
+    snprintf(inp, sizeof(inp),
+             "a=0x%016" PRIx64 ", b=0x%016" PRIx64 ", c=0x%016" PRIx64
+             ", flags=0x%x", a, b, c, flags);
+    check_f64("f64_muladd", res_soft, status_soft.float_exception_flags,
+              res_hard, status_hard.float_exception_flags, inp);
+}
+
+static void test_f32_sqrt_case(float32 a)
+{
+    float_status status_soft, status_hard;
+    float32 res_soft, res_hard;
+    char inp[64];
+
+    init_status(&status_soft);
+    init_status(&status_hard);
+
+    force_soft_float = true;
+    res_soft = float32_sqrt(a, &status_soft);
+
+    force_soft_float = false;
+    res_hard = float32_sqrt(a, &status_hard);
+
+    snprintf(inp, sizeof(inp), "a=0x%08" PRIx32, a);
+    check_f32("f32_sqrt", res_soft, status_soft.float_exception_flags,
+              res_hard, status_hard.float_exception_flags, inp);
+}
+
+static void test_f64_sqrt_case(float64 a)
+{
+    float_status status_soft, status_hard;
+    float64 res_soft, res_hard;
+    char inp[64];
+
+    init_status(&status_soft);
+    init_status(&status_hard);
+
+    force_soft_float = true;
+    res_soft = float64_sqrt(a, &status_soft);
+
+    force_soft_float = false;
+    res_hard = float64_sqrt(a, &status_hard);
+
+    snprintf(inp, sizeof(inp), "a=0x%016" PRIx64, a);
+    check_f64("f64_sqrt", res_soft, status_soft.float_exception_flags,
+              res_hard, status_hard.float_exception_flags, inp);
+}
+
+/* Binary FP operations runners */
+static void run_f32_binary(const char *name,
+                           float32 (*op)(float32, float32, float_status *))
+{
+    size_t n_edge = ARRAY_SIZE(f32_edge_inputs);
+    size_t i, j;
+
+    for (i = 0; i < n_edge; i++) {
+        for (j = 0; j < n_edge; j++) {
+            test_f32_op2(name, op, f32_edge_inputs[i],
+                         f32_edge_inputs[j]);
+        }
+    }
+
+    for (i = 0; i < NUM_OP_ITERATIONS; i++) {
+        float32 a = get_mixed_f32();
+        float32 b = get_mixed_f32();
+        test_f32_op2(name, op, a, b);
+    }
+}
+
+static void run_f64_binary(const char *name,
+                           float64 (*op)(float64, float64, float_status *))
+{
+    size_t n_edge = ARRAY_SIZE(f64_edge_inputs);
+    size_t i, j;
+
+    for (i = 0; i < n_edge; i++) {
+        for (j = 0; j < n_edge; j++) {
+            test_f64_op2(name, op, f64_edge_inputs[i],
+                         f64_edge_inputs[j]);
+        }
+    }
+
+    for (i = 0; i < NUM_OP_ITERATIONS; i++) {
+        float64 a = get_mixed_f64();
+        float64 b = get_mixed_f64();
+        test_f64_op2(name, op, a, b);
+    }
+}
+
+static void test_f32_add(void)
+{
+    run_f32_binary("f32_add", float32_add);
+}
+
+static void test_f32_sub(void)
+{
+    run_f32_binary("f32_sub", float32_sub);
+}
+
+static void test_f32_mul(void)
+{
+    run_f32_binary("f32_mul", float32_mul);
+}
+
+static void test_f32_div(void)
+{
+    run_f32_binary("f32_div", float32_div);
+}
+
+static void test_f64_add(void)
+{
+    run_f64_binary("f64_add", float64_add);
+}
+
+static void test_f64_sub(void)
+{
+    run_f64_binary("f64_sub", float64_sub);
+}
+
+static void test_f64_mul(void)
+{
+    run_f64_binary("f64_mul", float64_mul);
+}
+
+static void test_f64_div(void)
+{
+    run_f64_binary("f64_div", float64_div);
+}
+
+static void test_f32_muladd(void)
+{
+    static const int muladd_flags[] = {
+        0,
+        float_muladd_negate_c,
+        float_muladd_negate_product,
+        float_muladd_negate_result,
+        float_muladd_negate_c | float_muladd_negate_product,
+    };
+    size_t nf = ARRAY_SIZE(muladd_flags);
+    size_t k;
+
+    /* Test bug 5023 vector: FLT_MAX * 2.0f - FLT_MAX */
+    float32 flt_max = 0x7f7fffff;
+    float32 two = 0x40000000;
+    float32 neg_flt_max = 0xff7fffff;
+
+    for (k = 0; k < nf; k++) {
+        int flags = muladd_flags[k];
+        test_f32_muladd_case(flt_max, two, neg_flt_max, flags);
+        test_f32_muladd_case(neg_flt_max, two, flt_max, flags);
+        test_f32_muladd_case(flt_max, 0x00000000, flt_max, flags);
+        test_f32_muladd_case(0x00000000, flt_max, flt_max, flags);
+    }
+
+    for (unsigned int i = 0; i < NUM_OP_ITERATIONS; i++) {
+        float32 a = get_mixed_f32();
+        float32 b = get_mixed_f32();
+        float32 c = get_mixed_f32();
+        int flags = muladd_flags[i % nf];
+        test_f32_muladd_case(a, b, c, flags);
+    }
+}
+
+static void test_f64_muladd(void)
+{
+    static const int muladd_flags[] = {
+        0,
+        float_muladd_negate_c,
+        float_muladd_negate_product,
+        float_muladd_negate_result,
+        float_muladd_negate_c | float_muladd_negate_product,
+    };
+    size_t nf = ARRAY_SIZE(muladd_flags);
+    size_t k;
+
+    /* DBL_MAX * 2.0 - DBL_MAX */
+    float64 dbl_max = 0x7fefffffffffffffULL;
+    float64 two = 0x4000000000000000ULL;
+    float64 neg_dbl_max = 0xffefffffffffffffULL;
+
+    for (k = 0; k < nf; k++) {
+        int flags = muladd_flags[k];
+        test_f64_muladd_case(dbl_max, two, neg_dbl_max, flags);
+        test_f64_muladd_case(neg_dbl_max, two, dbl_max, flags);
+        test_f64_muladd_case(dbl_max, 0ULL, dbl_max, flags);
+        test_f64_muladd_case(0ULL, dbl_max, dbl_max, flags);
+    }
+
+    for (unsigned int i = 0; i < NUM_OP_ITERATIONS; i++) {
+        float64 a = get_mixed_f64();
+        float64 b = get_mixed_f64();
+        float64 c = get_mixed_f64();
+        int flags = muladd_flags[i % nf];
+        test_f64_muladd_case(a, b, c, flags);
+    }
+}
+
+static void test_f32_sqrt(void)
+{
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE(f32_edge_inputs); i++) {
+        float32 val = f32_edge_inputs[i];
+        if (!float32_is_neg(val)) {
+            test_f32_sqrt_case(val);
+        }
+    }
+
+    for (i = 0; i < NUM_OP_ITERATIONS; i++) {
+        float32 a = get_mixed_f32() & 0x7fffffff; /* positive only */
+        test_f32_sqrt_case(a);
+    }
+}
+
+static void test_f64_sqrt(void)
+{
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE(f64_edge_inputs); i++) {
+        float64 val = f64_edge_inputs[i];
+        if (!float64_is_neg(val)) {
+            test_f64_sqrt_case(val);
+        }
+    }
+
+    for (i = 0; i < NUM_OP_ITERATIONS; i++) {
+        float64 a = get_mixed_f64() & 0x7fffffffffffffffULL;
+        test_f64_sqrt_case(a);
+    }
+}
+
+#define GEN_INT_TO_FLOAT(name, in_t, out_t, func, check_fn, is_uint, fmt)     \
+static void test_##name(void)                                                 \
+{                                                                             \
+    float_status status_soft, status_hard;                                    \
+    out_t res_soft, res_hard;                                                 \
+    char inp[64];                                                             \
+    size_t i;                                                                 \
+                                                                              \
+    if (is_uint) {                                                            \
+        for (i = 0; i < ARRAY_SIZE(uint_edge_inputs); i++) {                   \
+            in_t a = (in_t)uint_edge_inputs[i];                               \
+            init_status(&status_soft);                                        \
+            init_status(&status_hard);                                        \
+            force_soft_float = true;                                          \
+            res_soft = func(a, &status_soft);                                 \
+            force_soft_float = false;                                         \
+            res_hard = func(a, &status_hard);                                 \
+            snprintf(inp, sizeof(inp), "a=" fmt, a);                          \
+            check_fn(#name, res_soft, status_soft.float_exception_flags,      \
+                     res_hard, status_hard.float_exception_flags, inp);       \
+        }                                                                     \
+    } else {                                                                  \
+        for (i = 0; i < ARRAY_SIZE(int_edge_inputs); i++) {                    \
+            in_t a = (in_t)int_edge_inputs[i];                                \
+            init_status(&status_soft);                                        \
+            init_status(&status_hard);                                        \
+            force_soft_float = true;                                          \
+            res_soft = func(a, &status_soft);                                 \
+            force_soft_float = false;                                         \
+            res_hard = func(a, &status_hard);                                 \
+            snprintf(inp, sizeof(inp), "a=" fmt, a);                          \
+            check_fn(#name, res_soft, status_soft.float_exception_flags,      \
+                     res_hard, status_hard.float_exception_flags, inp);       \
+        }                                                                     \
+    }                                                                         \
+                                                                              \
+    for (i = 0; i < NUM_CONV_ITERATIONS; i++) {                               \
+        in_t a = is_uint ? (in_t)get_mixed_uint() : (in_t)get_mixed_int();   \
+        init_status(&status_soft);                                            \
+        init_status(&status_hard);                                            \
+        force_soft_float = true;                                              \
+        res_soft = func(a, &status_soft);                                     \
+        force_soft_float = false;                                             \
+        res_hard = func(a, &status_hard);                                     \
+        snprintf(inp, sizeof(inp), "a=" fmt, a);                              \
+        check_fn(#name, res_soft, status_soft.float_exception_flags,          \
+                 res_hard, status_hard.float_exception_flags, inp);           \
+    }                                                                         \
+}
+
+GEN_INT_TO_FLOAT(i32_to_f32, int32_t, float32, int32_to_float32, \
+                 check_f32, false, "%" PRId32)
+GEN_INT_TO_FLOAT(i64_to_f32, int64_t, float32, int64_to_float32, \
+                 check_f32, false, "%" PRId64)
+GEN_INT_TO_FLOAT(ui32_to_f32, uint32_t, float32, uint32_to_float32, \
+                 check_f32, true, "%" PRIu32)
+GEN_INT_TO_FLOAT(ui64_to_f32, uint64_t, float32, uint64_to_float32, \
+                 check_f32, true, "%" PRIu64)
+GEN_INT_TO_FLOAT(i16_to_f32, int16_t, float32, int16_to_float32, \
+                 check_f32, false, "%" PRId16)
+GEN_INT_TO_FLOAT(ui16_to_f32, uint16_t, float32, uint16_to_float32, \
+                 check_f32, true, "%" PRIu16)
+
+GEN_INT_TO_FLOAT(i32_to_f64, int32_t, float64, int32_to_float64, \
+                 check_f64, false, "%" PRId32)
+GEN_INT_TO_FLOAT(i64_to_f64, int64_t, float64, int64_to_float64, \
+                 check_f64, false, "%" PRId64)
+GEN_INT_TO_FLOAT(ui32_to_f64, uint32_t, float64, uint32_to_float64, \
+                 check_f64, true, "%" PRIu32)
+GEN_INT_TO_FLOAT(ui64_to_f64, uint64_t, float64, uint64_to_float64, \
+                 check_f64, true, "%" PRIu64)
+GEN_INT_TO_FLOAT(i16_to_f64, int16_t, float64, int16_to_float64, \
+                 check_f64, false, "%" PRId16)
+GEN_INT_TO_FLOAT(ui16_to_f64, uint16_t, float64, uint16_to_float64, \
+                 check_f64, true, "%" PRIu16)
+
+#undef GEN_INT_TO_FLOAT
+
+typedef struct {
+    const char *name;
+    void (*func)(void);
+} OpTest;
+
+static const OpTest op_tests[] = {
+    { "f32_add", test_f32_add },
+    { "f32_sub", test_f32_sub },
+    { "f32_mul", test_f32_mul },
+    { "f32_div", test_f32_div },
+    { "f64_add", test_f64_add },
+    { "f64_sub", test_f64_sub },
+    { "f64_mul", test_f64_mul },
+    { "f64_div", test_f64_div },
+    { "f32_muladd", test_f32_muladd },
+    { "f64_muladd", test_f64_muladd },
+    { "f32_sqrt", test_f32_sqrt },
+    { "f64_sqrt", test_f64_sqrt },
+    { "i32_to_f32", test_i32_to_f32 },
+    { "i64_to_f32", test_i64_to_f32 },
+    { "ui32_to_f32", test_ui32_to_f32 },
+    { "ui64_to_f32", test_ui64_to_f32 },
+    { "i16_to_f32", test_i16_to_f32 },
+    { "ui16_to_f32", test_ui16_to_f32 },
+    { "i32_to_f64", test_i32_to_f64 },
+    { "i64_to_f64", test_i64_to_f64 },
+    { "ui32_to_f64", test_ui32_to_f64 },
+    { "ui64_to_f64", test_ui64_to_f64 },
+    { "i16_to_f64", test_i16_to_f64 },
+    { "ui16_to_f64", test_ui16_to_f64 },
+};
+
+static void usage(const char *prog)
+{
+    fprintf(stderr, "Usage: %s [operation ...]\n"
+            "Supported operations:\n", prog);
+    for (size_t i = 0; i < ARRAY_SIZE(op_tests); i++) {
+        fprintf(stderr, "  %s\n", op_tests[i].name);
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    bool initial_force_soft = force_soft_float;
+
+    if (argc > 1 && strcmp(argv[1], "-h") == 0) {
+        usage(argv[0]);
+        return EXIT_SUCCESS;
+    }
+
+    if (initial_force_soft) {
+        printf("NOTE: softfloat_init() detected broken host FPU;\n"
+               "hardfloat was globally disabled by default.\n");
+    }
+
+    /* If specific ops were requested on CLI */
+    int n_requested = argc - 1;
+
+    for (size_t i = 0; i < ARRAY_SIZE(op_tests); i++) {
+        if (n_requested > 0) {
+            bool found = false;
+            for (int a = 1; a < argc; a++) {
+                if (strcmp(argv[a], op_tests[i].name) == 0) {
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) {
+                continue;
+            }
+        }
+
+        printf(">> Testing %s...\n", op_tests[i].name);
+        op_tests[i].func();
+    }
+
+    /* Restore initial state */
+    force_soft_float = initial_force_soft;
+
+    printf("In %u tests, no errors found.\n", total_tests);
+    return EXIT_SUCCESS;
+}
tests/fp/meson.build +20 -10
index 9059a247521..4e444154f78 100644
@@ -1,6 +1,26 @@
 if 'CONFIG_TCG' not in config_all_accel
   subdir_done()
 endif
+
+fpcflags = [
+  # work around TARGET_* poisoning
+  '-DHW_POISON_H',
+  # define a target to match testfloat's implementation-defined choices, such as
+  # whether to raise the invalid flag when dealing with NaNs in muladd.
+  '-DTARGET_ARM',
+  # FIXME: uiZ may be used uninitialized in this function
+  '-Wno-uninitialized',
+]
+
+fptesthard = executable(
+  'fp-test-hard',
+  ['fp-test-hard.c', '../../fpu/softfloat.c'],
+  dependencies: [qemuutil],
+  c_args: fpcflags,
+)
+test('fp-test-hard', fptesthard,
+     suite: ['softfloat', 'softfloat-hard'])
+
 # There are namespace pollution issues on Windows, due to osdep.h
 # bringing in Windows headers that define a FLOAT128 type.
 if host_os == 'windows'
@@ -44,16 +64,6 @@ libtestfloat_proj = subproject('berkeley-testfloat-3', required: true,
 libtestfloat = libtestfloat_proj.get_variable('libtestfloat_dep')
 libslowfloat = libtestfloat_proj.get_variable('libslowfloat_dep')
 
-fpcflags = [
-  # work around TARGET_* poisoning
-  '-DHW_POISON_H',
-  # define a target to match testfloat's implementation-defined choices, such as
-  # whether to raise the invalid flag when dealing with NaNs in muladd.
-  '-DTARGET_ARM',
-  # FIXME: uiZ may be used uninitialized in this function
-  '-Wno-uninitialized',
-]
-
 fptest = executable(
   'fp-test',
   ['fp-test.c', '../../fpu/softfloat.c'],
Signature · 1 line
2.47.3