target/i386: miscellaneous x87 fixes
cover target/i386: miscellaneous x87 fixes
Following my previous patch series
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg00781.html>
and
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg01465.html>
for problems found in the x87 floating-point emulation, this patch series fixes further miscellaneous bugs in that emulation.
There are further problems with x87 emulation that I am aware of and intend to address in future patch series. Those other problems, not addressed by the first three patch series, generally relate to exceptions, flag setting and those instructions for which the emulation currently converts to host double (so losing range and precision) and then works on host double for the rest of the emulation process before converting back to floatx80 at the end. Thus, the same comments as for the previous patch series apply about this patch series not fixing missing propagation of exceptions even when it adds code to set exceptions in the softfloat state.
Joseph Myers (4):
target/i386: fix floating-point load-constant rounding target/i386: fix fxam handling of invalid encodings target/i386: fix fbstp handling of negative zero target/i386: fix fbstp handling of out-of-range values
target/i386/fpu_helper.c | 73 +++++++++-- tests/tcg/i386/test-i386-fbstp.c | 140 +++++++++++++++++++++ tests/tcg/i386/test-i386-fldcst.c | 199 ++++++++++++++++++++++++++++++ tests/tcg/i386/test-i386-fxam.c | 143 +++++++++++++++++++++ 4 files changed, 547 insertions(+), 8 deletions(-) create mode 100644 tests/tcg/i386/test-i386-fbstp.c create mode 100644 tests/tcg/i386/test-i386-fldcst.c create mode 100644 tests/tcg/i386/test-i386-fxam.c
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH 1/4 target/i386: fix floating-point load-constant rounding
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.
--- 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
target/i386/fpu_helper.c +49 -5
@@ -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)
tests/tcg/i386/test-i386-fldcst.c new file +199 -0
199 lines not on this page · raw message
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
Re: [PATCH 1/4] target/i386: fix floating-point load-constant rounding
Quoted 6 lines · Joseph Myers
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.
Quoted 6 lines
Signed-off-by Joseph Myers <[email protected]>--- 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
r~
PATCH 2/4 target/i386: fix fxam handling of invalid encodings
In reply to Joseph MyersThe fxam implementation does not check for invalid encodings, instead treating them like NaN or normal numbers depending on the exponent. Fix it to check that the high bit of the significand is set before treating an encoding as NaN or normal, thus resulting in correct handling (all of C0, C2 and C3 cleared) for invalid encodings.
--- target/i386/fpu_helper.c | 4 +- tests/tcg/i386/test-i386-fxam.c | 143 ++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/i386/test-i386-fxam.c
target/i386/fpu_helper.c +2 -2
@@ -1099,7 +1099,7 @@ void helper_fxam_ST0(CPUX86State *env)if (expdif ==MAXEXPD ) {if (MANTD (temp) ==0x8000000000000000ULL ) { env->fpus |=0x500 ;/* Infinity */ - }else { + }else if (MANTD (temp) &0x8000000000000000ULL ) { env->fpus |=0x100 ;/* NaN */ } }else if (expdif ==0 ) {
@@ -1108,7 +1108,7 @@ void helper_fxam_ST0(CPUX86State *env) }else { env->fpus |=0x4400 ;/* Denormal */ } - }else { + }else if (MANTD (temp) &0x8000000000000000ULL ) { env->fpus |=0x400 ; } }
tests/tcg/i386/test-i386-fxam.c new file +143 -0
143 lines not on this page · raw message
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH 3/4 target/i386: fix fbstp handling of negative zero
In reply to Joseph MyersThe fbstp implementation stores +0 when the rounded result should be -0 because it compares an integer value with 0 to determine the sign. Fix this by checking the sign bit of the operand instead.
--- target/i386/fpu_helper.c | 5 ++++- tests/tcg/i386/test-i386-fbstp.c | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/i386/test-i386-fbstp.c
target/i386/fpu_helper.c +4 -1
@@ -726,11 +726,14 @@ void helper_fbst_ST0(CPUX86State *env, target_ulong ptr)int v;target_ulong mem_ref, mem_end;int64_t val; +CPU_LDoubleU temp; + + temp.d =ST0 ; val =floatx80_to_int64 (ST0 , &env->fp_status); mem_ref = ptr; mem_end = mem_ref +9 ; -if (val <0 ) { +if (SIGND (temp)) {cpu_stb_data_ra (env, mem_end,0x80 ,GETPC ()); val = -val; }else {
tests/tcg/i386/test-i386-fbstp.c new file +25 -0
@@ -0,0 +1,25 @@ +/* Test fbstp instruction. */ + +#include <stdio.h> +#include <string.h> + +int main (void ) +{ +int ret =0 ; +unsigned char out[10 ]; +memset (out,0xfe ,sizeof out); + __asm__volatile ("fbstp %0" :"=m" (out) :"t" (-0.0L ) :"st" ); + out[9 ] &=0x80 ; +if (memcmp (out,"\0\0\0\0\0\0\0\0\0\x80" ,sizeof out) !=0 ) { +printf ("FAIL: fbstp -0\n" ); + ret =1 ; + } +memset (out,0x12 ,sizeof out); + __asm__volatile ("fbstp %0" :"=m" (out) :"t" (-0.1L ) :"st" ); + out[9 ] &=0x80 ; +if (memcmp (out,"\0\0\0\0\0\0\0\0\0\x80" ,sizeof out) !=0 ) { +printf ("FAIL: fbstp -0.1\n" ); + ret =1 ; + } +return ret; +}
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
PATCH 4/4 target/i386: fix fbstp handling of out-of-range values
In reply to Joseph MyersThe fbstp implementation fails to check for out-of-range and invalid values, instead just taking the result of conversion to int64_t and storing its sign and low 18 decimal digits. Fix this by checking for an out-of-range result (invalid conversions always result in INT64_MAX or INT64_MIN from the softfloat code, which are large enough to be considered as out-of-range by this code) and storing the packed BCD indefinite encoding in that case.
--- target/i386/fpu_helper.c | 10 +++ tests/tcg/i386/test-i386-fbstp.c | 115 +++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+)
target/i386/fpu_helper.c +10 -0
@@ -732,6 +732,16 @@ void helper_fbst_ST0(CPUX86State *env, target_ulong ptr) val =floatx80_to_int64 (ST0 , &env->fp_status); mem_ref = ptr; +if (val >=1000000000000000000LL || val <=-1000000000000000000LL ) { +float_raise (float_flag_invalid, &env->fp_status); +while (mem_ref < ptr +7 ) { +cpu_stb_data_ra (env, mem_ref++,0 ,GETPC ()); + } +cpu_stb_data_ra (env, mem_ref++,0xc0 ,GETPC ()); +cpu_stb_data_ra (env, mem_ref++,0xff ,GETPC ()); +cpu_stb_data_ra (env, mem_ref++,0xff ,GETPC ()); +return ; + } mem_end = mem_ref +9 ;if (SIGND (temp)) {cpu_stb_data_ra (env, mem_end,0x80 ,GETPC ());
tests/tcg/i386/test-i386-fbstp.c +115 -0
128 lines not on this page · raw message
Signature · 6 lines
2.17.1 -- Joseph S. Myers [email protected]
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Message-id: [email protected]
Subject: [PATCH 0/4] target/i386: miscellaneous x87 fixes
Type: series
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected] - [tag update] patchew/[email protected] -> patchew/[email protected] * [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
9abdf27 target/i386: fix fbstp handling of out-of-range values 53fdfa9 target/i386: fix fbstp handling of negative zero 350fd81 target/i386: fix fxam handling of invalid encodings 0cd240c target/i386: fix floating-point load-constant rounding
=== OUTPUT BEGIN ===
1/4 Checking commit 0cd240c60f14 (target/i386: fix floating-point load-constant rounding)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #107:
new file mode 100644
ERROR: Use of volatile is usually wrong, please add a comment #117: FILE: tests/tcg/i386/test-i386-fldcst.c:6:
+volatile long double ld_res;
ERROR: spaces required around that '+' (ctx:VxV)
#129: FILE: tests/tcg/i386/test-i386-fldcst.c:18:
+ if (ld_res != 0x3.5269e12f346e2bf8p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#138: FILE: tests/tcg/i386/test-i386-fldcst.c:27:
+ if (ld_res != 0x3.5269e12f346e2bf8p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#147: FILE: tests/tcg/i386/test-i386-fldcst.c:36:
+ if (ld_res != 0x3.5269e12f346e2bf8p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#156: FILE: tests/tcg/i386/test-i386-fldcst.c:45:
+ if (ld_res != 0x3.5269e12f346e2bfcp+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#166: FILE: tests/tcg/i386/test-i386-fldcst.c:55:
+ if (ld_res != 0x1.71547652b82fe178p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#175: FILE: tests/tcg/i386/test-i386-fldcst.c:64:
+ if (ld_res != 0x1.71547652b82fe176p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#184: FILE: tests/tcg/i386/test-i386-fldcst.c:73:
+ if (ld_res != 0x1.71547652b82fe176p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#193: FILE: tests/tcg/i386/test-i386-fldcst.c:82:
+ if (ld_res != 0x1.71547652b82fe178p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV)
#203: FILE: tests/tcg/i386/test-i386-fldcst.c:92:
+ if (ld_res != 0x3.243f6a8885a308d4p+0L) {
^ERROR: spaces required around that '+' (ctx:VxV) #212: FILE: tests/tcg/i386/test-i386-fldcst.c:101:
+ if (ld_res != 0x3.243f6a8885a308dp+0L) {
^ERROR: spaces required around that '+' (ctx:VxV) #221: FILE: tests/tcg/i386/test-i386-fldcst.c:110:
+ if (ld_res != 0x3.243f6a8885a308dp+0L) {
^ERROR: spaces required around that '+' (ctx:VxV) #230: FILE: tests/tcg/i386/test-i386-fldcst.c:119:
+ if (ld_res != 0x3.243f6a8885a308d4p+0L) {
^ERROR: spaces required around that '-' (ctx:VxV) #240: FILE: tests/tcg/i386/test-i386-fldcst.c:129:
+ if (ld_res != 0x4.d104d427de7fbcc8p-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #249: FILE: tests/tcg/i386/test-i386-fldcst.c:138:
+ if (ld_res != 0x4.d104d427de7fbccp-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #258: FILE: tests/tcg/i386/test-i386-fldcst.c:147:
+ if (ld_res != 0x4.d104d427de7fbccp-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #267: FILE: tests/tcg/i386/test-i386-fldcst.c:156:
+ if (ld_res != 0x4.d104d427de7fbcc8p-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #277: FILE: tests/tcg/i386/test-i386-fldcst.c:166:
+ if (ld_res != 0xb.17217f7d1cf79acp-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #286: FILE: tests/tcg/i386/test-i386-fldcst.c:175:
+ if (ld_res != 0xb.17217f7d1cf79abp-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #295: FILE: tests/tcg/i386/test-i386-fldcst.c:184:
+ if (ld_res != 0xb.17217f7d1cf79abp-4L) {
^ERROR: spaces required around that '-' (ctx:VxV) #304: FILE: tests/tcg/i386/test-i386-fldcst.c:193:
+ if (ld_res != 0xb.17217f7d1cf79acp-4L) {
^total: 21 errors, 1 warnings, 283 lines checked
Patch 1/4 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
2/4 Checking commit 350fd815b461 (target/i386: fix fxam handling of invalid encodings)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #39:
new file mode 100644
ERROR: Use of volatile is usually wrong, please add a comment #54: FILE: tests/tcg/i386/test-i386-fxam.c:11:
+volatile union u ld_pseudo_m16382 = { .s = { UINT64_C(1) << 63, 0 } };
ERROR: Use of volatile is usually wrong, please add a comment #55: FILE: tests/tcg/i386/test-i386-fxam.c:12:
+volatile union u ld_pseudo_nm16382 = { .s = { UINT64_C(1) << 63, 0x8000 } };
ERROR: Use of volatile is usually wrong, please add a comment #56: FILE: tests/tcg/i386/test-i386-fxam.c:13:
+volatile union u ld_invalid_1 = { .s = { 1, 1234 } };
ERROR: Use of volatile is usually wrong, please add a comment #57: FILE: tests/tcg/i386/test-i386-fxam.c:14:
+volatile union u ld_invalid_2 = { .s = { 0, 1234 } };
ERROR: Use of volatile is usually wrong, please add a comment #58: FILE: tests/tcg/i386/test-i386-fxam.c:15:
+volatile union u ld_invalid_3 = { .s = { 0, 0x7fff } };
ERROR: Use of volatile is usually wrong, please add a comment #59: FILE: tests/tcg/i386/test-i386-fxam.c:16:
+volatile union u ld_invalid_4 = { .s = { (UINT64_C(1) << 63) - 1, 0x7fff } };
ERROR: Use of volatile is usually wrong, please add a comment #60: FILE: tests/tcg/i386/test-i386-fxam.c:17:
+volatile union u ld_invalid_n1 = { .s = { 1, 0x8123 } };
ERROR: Use of volatile is usually wrong, please add a comment #61: FILE: tests/tcg/i386/test-i386-fxam.c:18:
+volatile union u ld_invalid_n2 = { .s = { 0, 0x8123 } };
ERROR: Use of volatile is usually wrong, please add a comment #62: FILE: tests/tcg/i386/test-i386-fxam.c:19:
+volatile union u ld_invalid_n3 = { .s = { 0, 0xffff } };
ERROR: Use of volatile is usually wrong, please add a comment
#63: FILE: tests/tcg/i386/test-i386-fxam.c:20:
+volatile union u ld_invalid_n4 = { .s = { (UINT64_C(1) << 63) - 1, 0xffff } };
ERROR: spaces required around that '-' (ctx:VxV)
#125: FILE: tests/tcg/i386/test-i386-fxam.c:82:
+ __asm__ volatile ("fxam\nfnstsw" : "=a" (sw) : "t" (0x1p-16445L));
^ERROR: spaces required around that '-' (ctx:VxV)
#130: FILE: tests/tcg/i386/test-i386-fxam.c:87:
+ __asm__ volatile ("fxam\nfnstsw" : "=a" (sw) : "t" (-0x1p-16445L));
^total: 12 errors, 1 warnings, 159 lines checked
Patch 2/4 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
3/4 Checking commit 53fdfa9ee04d (target/i386: fix fbstp handling of negative zero)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #35:
new file mode 100644
total: 0 errors, 1 warnings, 40 lines checked
Patch 3/4 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
4/4 Checking commit 9abdf27f3157 (target/i386: fix fbstp handling of out-of-range values)
ERROR: Use of volatile is usually wrong, please add a comment
#55: FILE: tests/tcg/i386/test-i386-fbstp.c:12:
+volatile union u ld_invalid_1 = { .s = { 1, 1234 } };
ERROR: Use of volatile is usually wrong, please add a comment #56: FILE: tests/tcg/i386/test-i386-fbstp.c:13:
+volatile union u ld_invalid_2 = { .s = { 0, 1234 } };
ERROR: Use of volatile is usually wrong, please add a comment #57: FILE: tests/tcg/i386/test-i386-fbstp.c:14:
+volatile union u ld_invalid_3 = { .s = { 0, 0x7fff } };
ERROR: Use of volatile is usually wrong, please add a comment #58: FILE: tests/tcg/i386/test-i386-fbstp.c:15:
+volatile union u ld_invalid_4 = { .s = { (UINT64_C(1) << 63) - 1, 0x7fff } };
total: 4 errors, 0 warnings, 144 lines checked
Patch 4/4 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Quoted 2 lines · On Thu, 14 May 2020, [email protected] wrote:
This series seems to have some coding style problems. See output below for more information:
These are all false positives for the same reasons as for the previous patch series.
Signature · 2 lines
Joseph S. Myers [email protected]
Ping for this patch series
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg03543.html>, and the subsequent series
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg04462.html> and individual patch
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg06521.html>.
Signature · 2 lines
Joseph S. Myers [email protected]
Quoted 6 lines · Joseph Myers
Ping for this patch series
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg03543.html>, and the subsequent series
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg04462.html> and individual patch
<https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg06521.html>.
Hi Joseph,
these are the patches that I have queued:
Joseph Myers (12):
target/i386: implement special cases for fxtract target/i386: fix fscale handling of signaling NaN target/i386: fix fscale handling of invalid exponent encodings target/i386: fix fscale handling of infinite exponents target/i386: fix fscale handling of rounding precision target/i386: fix floating-point load-constant rounding target/i386: fix fxam handling of invalid encodings target/i386: fix fbstp handling of negative zero target/i386: fix fbstp handling of out-of-range values target/i386: fix fisttpl, fisttpll handling of out-of-range values target/i386: fix IEEE x87 floating-point exception raising target/i386: correct fix for pcmpxstrx substring search
I was busy because Linus opened the merge window this week and because of some public holidays, but I hope to send the pull request tomorrow as it's grown quite big.
Paolo