[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 060/115] target/i386: fix floating-point load-constant rounding
From: |
Paolo Bonzini |
Subject: |
[PULL 060/115] target/i386: fix floating-point load-constant rounding |
Date: |
Thu, 11 Jun 2020 15:43:54 -0400 |
From: Joseph Myers <joseph@codesourcery.com>
The implementations of the fldl2t, fldl2e, fldpi, fldlg2 and fldln2
instructions load fixed constants independent of the rounding mode.
Fix them to load a value correctly rounded for the current rounding
mode (but always rounded to 64-bit precision independent of the
precision control, and without setting "inexact") as specified.
Signed-off-by: Joseph Myers <joseph@codesourcery.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <alpine.DEB.2.21.2005132348310.11687@digraph.polyomino.org.uk>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
target/i386/fpu_helper.c | 54 +++++++-
tests/tcg/i386/test-i386-fldcst.c | 199 ++++++++++++++++++++++++++++++
2 files changed, 248 insertions(+), 5 deletions(-)
create mode 100644 tests/tcg/i386/test-i386-fldcst.c
diff --git a/target/i386/fpu_helper.c b/target/i386/fpu_helper.c
index 4d14c1ca24..f0b9cb5de8 100644
--- a/target/i386/fpu_helper.c
+++ b/target/i386/fpu_helper.c
@@ -59,8 +59,13 @@
#define FPUC_EM 0x3f
#define floatx80_lg2 make_floatx80(0x3ffd, 0x9a209a84fbcff799LL)
+#define floatx80_lg2_d make_floatx80(0x3ffd, 0x9a209a84fbcff798LL)
#define floatx80_l2e make_floatx80(0x3fff, 0xb8aa3b295c17f0bcLL)
+#define floatx80_l2e_d make_floatx80(0x3fff, 0xb8aa3b295c17f0bbLL)
#define floatx80_l2t make_floatx80(0x4000, 0xd49a784bcd1b8afeLL)
+#define floatx80_l2t_u make_floatx80(0x4000, 0xd49a784bcd1b8affLL)
+#define floatx80_ln2_d make_floatx80(0x3ffe, 0xb17217f7d1cf79abLL)
+#define floatx80_pi_d make_floatx80(0x4000, 0xc90fdaa22168c234LL)
#if !defined(CONFIG_USER_ONLY)
static qemu_irq ferr_irq;
@@ -544,27 +549,66 @@ void helper_fld1_ST0(CPUX86State *env)
void helper_fldl2t_ST0(CPUX86State *env)
{
- ST0 = floatx80_l2t;
+ switch (env->fpuc & FPU_RC_MASK) {
+ case FPU_RC_UP:
+ ST0 = floatx80_l2t_u;
+ break;
+ default:
+ ST0 = floatx80_l2t;
+ break;
+ }
}
void helper_fldl2e_ST0(CPUX86State *env)
{
- ST0 = floatx80_l2e;
+ switch (env->fpuc & FPU_RC_MASK) {
+ case FPU_RC_DOWN:
+ case FPU_RC_CHOP:
+ ST0 = floatx80_l2e_d;
+ break;
+ default:
+ ST0 = floatx80_l2e;
+ break;
+ }
}
void helper_fldpi_ST0(CPUX86State *env)
{
- ST0 = floatx80_pi;
+ switch (env->fpuc & FPU_RC_MASK) {
+ case FPU_RC_DOWN:
+ case FPU_RC_CHOP:
+ ST0 = floatx80_pi_d;
+ break;
+ default:
+ ST0 = floatx80_pi;
+ break;
+ }
}
void helper_fldlg2_ST0(CPUX86State *env)
{
- ST0 = floatx80_lg2;
+ switch (env->fpuc & FPU_RC_MASK) {
+ case FPU_RC_DOWN:
+ case FPU_RC_CHOP:
+ ST0 = floatx80_lg2_d;
+ break;
+ default:
+ ST0 = floatx80_lg2;
+ break;
+ }
}
void helper_fldln2_ST0(CPUX86State *env)
{
- ST0 = floatx80_ln2;
+ switch (env->fpuc & FPU_RC_MASK) {
+ case FPU_RC_DOWN:
+ case FPU_RC_CHOP:
+ ST0 = floatx80_ln2_d;
+ break;
+ default:
+ ST0 = floatx80_ln2;
+ break;
+ }
}
void helper_fldz_ST0(CPUX86State *env)
diff --git a/tests/tcg/i386/test-i386-fldcst.c
b/tests/tcg/i386/test-i386-fldcst.c
new file mode 100644
index 0000000000..e635432ccf
--- /dev/null
+++ b/tests/tcg/i386/test-i386-fldcst.c
@@ -0,0 +1,199 @@
+/* Test instructions loading floating-point constants. */
+
+#include <stdint.h>
+#include <stdio.h>
+
+volatile long double ld_res;
+
+int main(void)
+{
+ short cw;
+ int ret = 0;
+
+ /* Round to nearest. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x000;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2t" : "=t" (ld_res));
+ if (ld_res != 0x3.5269e12f346e2bf8p+0L) {
+ printf("FAIL: fldl2t N\n");
+ ret = 1;
+ }
+ /* Round downward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x400;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2t" : "=t" (ld_res));
+ if (ld_res != 0x3.5269e12f346e2bf8p+0L) {
+ printf("FAIL: fldl2t D\n");
+ ret = 1;
+ }
+ /* Round toward zero. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0xc00;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2t" : "=t" (ld_res));
+ if (ld_res != 0x3.5269e12f346e2bf8p+0L) {
+ printf("FAIL: fldl2t Z\n");
+ ret = 1;
+ }
+ /* Round upward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x800;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2t" : "=t" (ld_res));
+ if (ld_res != 0x3.5269e12f346e2bfcp+0L) {
+ printf("FAIL: fldl2t U\n");
+ ret = 1;
+ }
+
+ /* Round to nearest. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x000;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2e" : "=t" (ld_res));
+ if (ld_res != 0x1.71547652b82fe178p+0L) {
+ printf("FAIL: fldl2e N\n");
+ ret = 1;
+ }
+ /* Round downward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x400;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2e" : "=t" (ld_res));
+ if (ld_res != 0x1.71547652b82fe176p+0L) {
+ printf("FAIL: fldl2e D\n");
+ ret = 1;
+ }
+ /* Round toward zero. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0xc00;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2e" : "=t" (ld_res));
+ if (ld_res != 0x1.71547652b82fe176p+0L) {
+ printf("FAIL: fldl2e Z\n");
+ ret = 1;
+ }
+ /* Round upward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x800;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldl2e" : "=t" (ld_res));
+ if (ld_res != 0x1.71547652b82fe178p+0L) {
+ printf("FAIL: fldl2e U\n");
+ ret = 1;
+ }
+
+ /* Round to nearest. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x000;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldpi" : "=t" (ld_res));
+ if (ld_res != 0x3.243f6a8885a308d4p+0L) {
+ printf("FAIL: fldpi N\n");
+ ret = 1;
+ }
+ /* Round downward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x400;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldpi" : "=t" (ld_res));
+ if (ld_res != 0x3.243f6a8885a308dp+0L) {
+ printf("FAIL: fldpi D\n");
+ ret = 1;
+ }
+ /* Round toward zero. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0xc00;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldpi" : "=t" (ld_res));
+ if (ld_res != 0x3.243f6a8885a308dp+0L) {
+ printf("FAIL: fldpi Z\n");
+ ret = 1;
+ }
+ /* Round upward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x800;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldpi" : "=t" (ld_res));
+ if (ld_res != 0x3.243f6a8885a308d4p+0L) {
+ printf("FAIL: fldpi U\n");
+ ret = 1;
+ }
+
+ /* Round to nearest. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x000;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldlg2" : "=t" (ld_res));
+ if (ld_res != 0x4.d104d427de7fbcc8p-4L) {
+ printf("FAIL: fldlg2 N\n");
+ ret = 1;
+ }
+ /* Round downward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x400;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldlg2" : "=t" (ld_res));
+ if (ld_res != 0x4.d104d427de7fbccp-4L) {
+ printf("FAIL: fldlg2 D\n");
+ ret = 1;
+ }
+ /* Round toward zero. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0xc00;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldlg2" : "=t" (ld_res));
+ if (ld_res != 0x4.d104d427de7fbccp-4L) {
+ printf("FAIL: fldlg2 Z\n");
+ ret = 1;
+ }
+ /* Round upward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x800;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldlg2" : "=t" (ld_res));
+ if (ld_res != 0x4.d104d427de7fbcc8p-4L) {
+ printf("FAIL: fldlg2 U\n");
+ ret = 1;
+ }
+
+ /* Round to nearest. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x000;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldln2" : "=t" (ld_res));
+ if (ld_res != 0xb.17217f7d1cf79acp-4L) {
+ printf("FAIL: fldln2 N\n");
+ ret = 1;
+ }
+ /* Round downward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x400;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldln2" : "=t" (ld_res));
+ if (ld_res != 0xb.17217f7d1cf79abp-4L) {
+ printf("FAIL: fldln2 D\n");
+ ret = 1;
+ }
+ /* Round toward zero. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0xc00;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldln2" : "=t" (ld_res));
+ if (ld_res != 0xb.17217f7d1cf79abp-4L) {
+ printf("FAIL: fldln2 Z\n");
+ ret = 1;
+ }
+ /* Round upward. */
+ __asm__ volatile ("fnstcw %0" : "=m" (cw));
+ cw = (cw & ~0xc00) | 0x800;
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
+ __asm__ volatile ("fldln2" : "=t" (ld_res));
+ if (ld_res != 0xb.17217f7d1cf79acp-4L) {
+ printf("FAIL: fldln2 U\n");
+ ret = 1;
+ }
+
+ return ret;
+}
--
2.26.2
- [PULL 025/115] hw/i386/vmport: Add reference to VMware open-vm-tools, (continued)
- [PULL 025/115] hw/i386/vmport: Add reference to VMware open-vm-tools, Paolo Bonzini, 2020/06/11
- [PULL 017/115] hyperv: expose API to determine if synic is enabled, Paolo Bonzini, 2020/06/11
- [PULL 014/115] qom/object: Move Object typedef to 'qemu/typedefs.h', Paolo Bonzini, 2020/06/11
- [PULL 066/115] x86/cpu: Enable AVX512_VP2INTERSECT cpu feature, Paolo Bonzini, 2020/06/11
- [PULL 067/115] vfio/pci: Use kvm_irqchip_add_irqfd_notifier_gsi() for irqfds, Paolo Bonzini, 2020/06/11
- [PULL 035/115] hw/i386/vmport: Allow x2apic without IR, Paolo Bonzini, 2020/06/11
- [PULL 030/115] hw/i386/vmport: Report vmware-vmx-type in CMD_GETVERSION, Paolo Bonzini, 2020/06/11
- [PULL 064/115] target/i386: fix fisttpl, fisttpll handling of out-of-range values, Paolo Bonzini, 2020/06/11
- [PULL 024/115] target/i386: fix phadd* with identical destination and source register, Paolo Bonzini, 2020/06/11
- [PULL 048/115] megasas: use unsigned type for reply_queue_head and check index, Paolo Bonzini, 2020/06/11
- [PULL 060/115] target/i386: fix floating-point load-constant rounding,
Paolo Bonzini <=
- [PULL 019/115] vmbus: vmbus implementation, Paolo Bonzini, 2020/06/11
- [PULL 022/115] vmbus: add infrastructure to save/load vmbus requests, Paolo Bonzini, 2020/06/11
- [PULL 057/115] exec: Propagate cpu_memory_rw_debug() error, Paolo Bonzini, 2020/06/11
- [PULL 031/115] hw/i386/vmport: Introduce vmport.h, Paolo Bonzini, 2020/06/11
- [PULL 047/115] i386/kvm: fix a use-after-free when vcpu plug/unplug, Paolo Bonzini, 2020/06/11
- [PULL 040/115] qom: remove index from object_resolve_abs_path(), Paolo Bonzini, 2020/06/11
- [PULL 070/115] chardev/char-socket: Properly make qio connections non blocking, Paolo Bonzini, 2020/06/11
- [PULL 072/115] hw/i386/amd_iommu: Fix the reserved bits definition of IOMMU commands, Paolo Bonzini, 2020/06/11
- [PULL 068/115] KVM: Pass EventNotifier into kvm_irqchip_assign_irqfd, Paolo Bonzini, 2020/06/11
- [PULL 076/115] util/oslib: Returns the real thread identifier on FreeBSD and NetBSD, Paolo Bonzini, 2020/06/11