[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 051/115] target/i386: implement special cases for fxtract
From: |
Paolo Bonzini |
Subject: |
[PULL 051/115] target/i386: implement special cases for fxtract |
Date: |
Thu, 11 Jun 2020 15:43:45 -0400 |
From: Joseph Myers <joseph@codesourcery.com>
The implementation of the fxtract instruction treats all nonzero
operands as normal numbers, so yielding incorrect results for invalid
formats, infinities, NaNs and subnormal and pseudo-denormal operands.
Implement appropriate handling of all those cases.
Signed-off-by: Joseph Myers <joseph@codesourcery.com>
Acked-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <alpine.DEB.2.21.2005070042360.18350@digraph.polyomino.org.uk>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
target/i386/fpu_helper.c | 25 +++++-
tests/tcg/i386/test-i386-fxtract.c | 120 +++++++++++++++++++++++++++++
2 files changed, 144 insertions(+), 1 deletion(-)
create mode 100644 tests/tcg/i386/test-i386-fxtract.c
diff --git a/target/i386/fpu_helper.c b/target/i386/fpu_helper.c
index b34fa784eb..568bd3b359 100644
--- a/target/i386/fpu_helper.c
+++ b/target/i386/fpu_helper.c
@@ -767,10 +767,33 @@ void helper_fxtract(CPUX86State *env)
&env->fp_status);
fpush(env);
ST0 = temp.d;
+ } else if (floatx80_invalid_encoding(ST0)) {
+ float_raise(float_flag_invalid, &env->fp_status);
+ ST0 = floatx80_default_nan(&env->fp_status);
+ fpush(env);
+ ST0 = ST1;
+ } else if (floatx80_is_any_nan(ST0)) {
+ if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
+ float_raise(float_flag_invalid, &env->fp_status);
+ ST0 = floatx80_silence_nan(ST0, &env->fp_status);
+ }
+ fpush(env);
+ ST0 = ST1;
+ } else if (floatx80_is_infinity(ST0)) {
+ fpush(env);
+ ST0 = ST1;
+ ST1 = floatx80_infinity;
} else {
int expdif;
- expdif = EXPD(temp) - EXPBIAS;
+ if (EXPD(temp) == 0) {
+ int shift = clz64(temp.l.lower);
+ temp.l.lower <<= shift;
+ expdif = 1 - EXPBIAS - shift;
+ float_raise(float_flag_input_denormal, &env->fp_status);
+ } else {
+ expdif = EXPD(temp) - EXPBIAS;
+ }
/* DP exponent bias */
ST0 = int32_to_floatx80(expdif, &env->fp_status);
fpush(env);
diff --git a/tests/tcg/i386/test-i386-fxtract.c
b/tests/tcg/i386/test-i386-fxtract.c
new file mode 100644
index 0000000000..64fd93d333
--- /dev/null
+++ b/tests/tcg/i386/test-i386-fxtract.c
@@ -0,0 +1,120 @@
+/* Test fxtract instruction. */
+
+#include <stdint.h>
+#include <stdio.h>
+
+union u {
+ struct { uint64_t sig; uint16_t sign_exp; } s;
+ long double ld;
+};
+
+volatile union u ld_pseudo_m16382 = { .s = { UINT64_C(1) << 63, 0 } };
+volatile union u ld_invalid_1 = { .s = { 1, 1234 } };
+volatile union u ld_invalid_2 = { .s = { 0, 1234 } };
+volatile union u ld_invalid_3 = { .s = { 0, 0x7fff } };
+volatile union u ld_invalid_4 = { .s = { (UINT64_C(1) << 63) - 1, 0x7fff } };
+
+volatile long double ld_sig, ld_exp;
+
+int isnan_ld(long double x)
+{
+ union u tmp = { .ld = x };
+ return ((tmp.s.sign_exp & 0x7fff) == 0x7fff &&
+ (tmp.s.sig >> 63) != 0 &&
+ (tmp.s.sig << 1) != 0);
+}
+
+int issignaling_ld(long double x)
+{
+ union u tmp = { .ld = x };
+ return isnan_ld(x) && (tmp.s.sig & UINT64_C(0x4000000000000000)) == 0;
+}
+
+int main(void)
+{
+ int ret = 0;
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) : "0" (2.5L));
+ if (ld_sig != 1.25L || ld_exp != 1.0L) {
+ printf("FAIL: fxtract 2.5\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) : "0" (0.0L));
+ if (ld_sig != 0.0L || __builtin_copysignl(1.0L, ld_sig) != 1.0L ||
+ ld_exp != -__builtin_infl()) {
+ printf("FAIL: fxtract 0.0\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) : "0" (-0.0L));
+ if (ld_sig != -0.0L || __builtin_copysignl(1.0L, ld_sig) != -1.0L ||
+ ld_exp != -__builtin_infl()) {
+ printf("FAIL: fxtract -0.0\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (__builtin_infl()));
+ if (ld_sig != __builtin_infl() || ld_exp != __builtin_infl()) {
+ printf("FAIL: fxtract inf\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (-__builtin_infl()));
+ if (ld_sig != -__builtin_infl() || ld_exp != __builtin_infl()) {
+ printf("FAIL: fxtract -inf\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (__builtin_nanl("")));
+ if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
+ !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
+ printf("FAIL: fxtract qnan\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (__builtin_nansl("")));
+ if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
+ !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
+ printf("FAIL: fxtract snan\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (0x1p-16445L));
+ if (ld_sig != 1.0L || ld_exp != -16445.0L) {
+ printf("FAIL: fxtract subnormal\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (ld_pseudo_m16382.ld));
+ if (ld_sig != 1.0L || ld_exp != -16382.0L) {
+ printf("FAIL: fxtract pseudo\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (ld_invalid_1.ld));
+ if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
+ !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
+ printf("FAIL: fxtract invalid 1\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (ld_invalid_2.ld));
+ if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
+ !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
+ printf("FAIL: fxtract invalid 2\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (ld_invalid_3.ld));
+ if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
+ !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
+ printf("FAIL: fxtract invalid 3\n");
+ ret = 1;
+ }
+ __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
+ "0" (ld_invalid_4.ld));
+ if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
+ !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
+ printf("FAIL: fxtract invalid 4\n");
+ ret = 1;
+ }
+ return ret;
+}
--
2.26.2
- [PULL 012/115] qom/object: Fix object_child_foreach_recursive() return value, (continued)
- [PULL 012/115] qom/object: Fix object_child_foreach_recursive() return value, Paolo Bonzini, 2020/06/11
- [PULL 027/115] hw/i386/vmport: Propagate IOPort read to vCPU EAX register, Paolo Bonzini, 2020/06/11
- [PULL 005/115] run-coverity-scan: add --no-update-tools option, Paolo Bonzini, 2020/06/11
- [PULL 033/115] hw/i386/vmport: Add support for CMD_GETBIOSUUID, Paolo Bonzini, 2020/06/11
- [PULL 009/115] vl.c: run preconfig loop before creating default RAM backend, Paolo Bonzini, 2020/06/11
- [PULL 032/115] hw/i386/vmport: Define enum for all commands, Paolo Bonzini, 2020/06/11
- [PULL 013/115] target/i386: Fix OUTL debug output, Paolo Bonzini, 2020/06/11
- [PULL 036/115] i386/cpu: Store LAPIC bus frequency in CPU structure, Paolo Bonzini, 2020/06/11
- [PULL 039/115] accel: Move Xen accelerator code under accel/xen/, Paolo Bonzini, 2020/06/11
- [PULL 026/115] hw/i386/vmport: Add device properties, Paolo Bonzini, 2020/06/11
- [PULL 051/115] target/i386: implement special cases for fxtract,
Paolo Bonzini <=
- [PULL 056/115] exec: Let address_space_read/write_cached() propagate MemTxResult, Paolo Bonzini, 2020/06/11
- [PULL 038/115] hw/i386/vmport: Assert vmport initialized before registering commands, Paolo Bonzini, 2020/06/11
- [PULL 042/115] qom/object: simplify type_initialize_interface(), Paolo Bonzini, 2020/06/11
- [PULL 046/115] hax: Dynamic allocate vcpu state structure, Paolo Bonzini, 2020/06/11
- [PULL 053/115] target/i386: fix fscale handling of invalid exponent encodings, Paolo Bonzini, 2020/06/11
- [PULL 052/115] target/i386: fix fscale handling of signaling NaN, Paolo Bonzini, 2020/06/11
- [PULL 043/115] qom/object: pass (Object *) to object_initialize_with_type(), Paolo Bonzini, 2020/06/11
- [PULL 034/115] hw/i386/vmport: Add support for CMD_GET_VCPU_INFO, Paolo Bonzini, 2020/06/11
- [PULL 049/115] megasas: avoid NULL pointer dereference, Paolo Bonzini, 2020/06/11
- [PULL 061/115] target/i386: fix fxam handling of invalid encodings, Paolo Bonzini, 2020/06/11