2 * x86 FPREM test - executes the FPREM and FPREM1 instructions with corner case
3 * operands and prints the operands, result and FPU status word.
5 * Run this on real hardware, then under QEMU, and diff the outputs, to compare
6 * QEMU's implementation to your hardware. The 'run-test-i386-fprem' make
9 * Copyright (c) 2003 Fabrice Bellard
10 * Copyright (c) 2012 Catalin Patulea
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "qemu/compiler.h"
26 #include "qemu/osdep.h"
31 * Inspired by <ieee754.h>'s union ieee854_long_double, but with single
32 * long long mantissa fields and assuming little-endianness for simplicity.
37 /* This is the IEEE 854 double-extended-precision format. */
39 unsigned long long mantissa
:63;
41 unsigned int exponent
:15;
42 unsigned int negative
:1;
43 unsigned int empty
:16;
46 /* This is for NaNs in the IEEE 854 double-extended-precision format. */
48 unsigned long long mantissa
:62;
49 unsigned int quiet_nan
:1;
51 unsigned int exponent
:15;
52 unsigned int negative
:1;
53 unsigned int empty
:16;
54 } QEMU_PACKED ieee_nan
;
57 #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
59 static const union float80u q_nan
= {
60 .ieee_nan
.negative
= 0, /* X */
61 .ieee_nan
.exponent
= 0x7fff,
63 .ieee_nan
.quiet_nan
= 1,
64 .ieee_nan
.mantissa
= 0,
67 static const union float80u s_nan
= {
68 .ieee_nan
.negative
= 0, /* X */
69 .ieee_nan
.exponent
= 0x7fff,
71 .ieee_nan
.quiet_nan
= 0,
72 .ieee_nan
.mantissa
= 1, /* nonzero */
75 static const union float80u pos_inf
= {
77 .ieee
.exponent
= 0x7fff,
82 static const union float80u pseudo_pos_inf
= { /* "unsupported" */
84 .ieee
.exponent
= 0x7fff,
89 static const union float80u pos_denorm
= {
96 static const union float80u smallest_positive_norm
= {
105 asm volatile ("fninit\n");
108 static long double fprem(long double a
, long double b
, uint16_t *sw
)
111 asm volatile ("fprem\n"
113 : "=t" (result
), "=m" (*sw
)
119 static long double fprem1(long double a
, long double b
, uint16_t *sw
)
122 asm volatile ("fprem1\n"
124 : "=t" (result
), "=m" (*sw
)
130 #define FPUS_IE (1 << 0)
131 #define FPUS_DE (1 << 1)
132 #define FPUS_ZE (1 << 2)
133 #define FPUS_OE (1 << 3)
134 #define FPUS_UE (1 << 4)
135 #define FPUS_PE (1 << 5)
136 #define FPUS_SF (1 << 6)
137 #define FPUS_SE (1 << 7)
138 #define FPUS_C0 (1 << 8)
139 #define FPUS_C1 (1 << 9)
140 #define FPUS_C2 (1 << 10)
141 #define FPUS_TOP 0x3800
142 #define FPUS_C3 (1 << 14)
143 #define FPUS_B (1 << 15)
145 #define FPUS_EMASK 0x007f
149 static void psw(uint16_t sw
)
151 printf("SW: C3 TopC2C1C0\n");
152 printf("SW: %c %d %3d %d %d %d %c %c %c %c %c %c %c %c\n",
153 sw
& FPUS_B
? 'B' : 'b',
155 (sw
& FPUS_TOP
) >> 11,
159 (sw
& FPUS_SE
) ? 'S' : 's',
160 (sw
& FPUS_SF
) ? 'F' : 'f',
161 (sw
& FPUS_PE
) ? 'P' : 'p',
162 (sw
& FPUS_UE
) ? 'U' : 'u',
163 (sw
& FPUS_OE
) ? 'O' : 'o',
164 (sw
& FPUS_ZE
) ? 'Z' : 'z',
165 (sw
& FPUS_DE
) ? 'D' : 'd',
166 (sw
& FPUS_IE
) ? 'I' : 'i');
169 static void do_fprem(long double a
, long double b
)
171 const union float80u au
= {.d
= a
};
172 const union float80u bu
= {.d
= b
};
176 printf("A: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
177 au
.ieee
.negative
, au
.ieee
.exponent
, au
.ieee
.one
,
178 au
.ieee_nan
.quiet_nan
, (unsigned long long)au
.ieee
.mantissa
,
180 printf("B: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
181 bu
.ieee
.negative
, bu
.ieee
.exponent
, bu
.ieee
.one
,
182 bu
.ieee_nan
.quiet_nan
, (unsigned long long)bu
.ieee
.mantissa
,
187 ru
.d
= fprem(a
, b
, &sw
);
190 printf("R : S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
191 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
192 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
196 ru
.d
= fprem1(a
, b
, &sw
);
199 printf("R1: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
200 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
201 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
207 static void do_fprem_stack_underflow(void)
209 const long double a
= 1.0;
214 asm volatile ("fprem\n"
216 : "=t" (ru
.d
), "=m" (sw
)
221 printf("R: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
222 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
223 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
228 static void test_fprem_cases(void)
230 printf("= stack underflow =\n");
231 do_fprem_stack_underflow();
233 printf("= invalid operation =\n");
234 do_fprem(s_nan
.d
, 1.0);
236 do_fprem(pos_inf
.d
, 1.0);
237 do_fprem(pseudo_pos_inf
.d
, 1.0);
239 printf("= denormal =\n");
240 do_fprem(pos_denorm
.d
, 1.0);
241 do_fprem(1.0, pos_denorm
.d
);
243 /* printf("= underflow =\n"); */
244 /* TODO: Is there a case where FPREM raises underflow? */
247 static void test_fprem_pairs(void)
249 unsigned long long count
;
251 unsigned int negative_index_a
= 0;
252 unsigned int negative_index_b
= 0;
253 static const unsigned int negative_values
[] = {
258 unsigned int exponent_index_a
= 0;
259 unsigned int exponent_index_b
= 0;
260 static const unsigned int exponent_values
[] = {
264 IEEE854_LONG_DOUBLE_BIAS
- 1,
265 IEEE854_LONG_DOUBLE_BIAS
,
266 IEEE854_LONG_DOUBLE_BIAS
+ 1,
272 unsigned int one_index_a
= 0;
273 unsigned int one_index_b
= 0;
274 static const unsigned int one_values
[] = {
279 unsigned int quiet_nan_index_a
= 0;
280 unsigned int quiet_nan_index_b
= 0;
281 static const unsigned int quiet_nan_values
[] = {
286 unsigned int mantissa_index_a
= 0;
287 unsigned int mantissa_index_b
= 0;
288 static const unsigned long long mantissa_values
[] = {
292 0x3ffffffffffffffdULL
,
293 0x3ffffffffffffffeULL
,
294 0x3fffffffffffffffULL
,
297 for (count
= 0; ; ++count
) {
298 #define INIT_FIELD(var, field) \
299 .ieee_nan.field = field##_values[field##_index_##var]
300 const union float80u a
= {
301 INIT_FIELD(a
, negative
),
302 INIT_FIELD(a
, exponent
),
304 INIT_FIELD(a
, quiet_nan
),
305 INIT_FIELD(a
, mantissa
),
307 const union float80u b
= {
308 INIT_FIELD(b
, negative
),
309 INIT_FIELD(b
, exponent
),
311 INIT_FIELD(b
, quiet_nan
),
312 INIT_FIELD(b
, mantissa
),
319 #define CARRY_INTO(var, field) do { \
321 if (++field##_index_##var == ARRAY_SIZE(field##_values)) { \
322 field##_index_##var = 0; \
328 CARRY_INTO(b
, mantissa
);
329 CARRY_INTO(b
, quiet_nan
);
331 CARRY_INTO(b
, exponent
);
332 CARRY_INTO(b
, negative
);
333 CARRY_INTO(a
, mantissa
);
334 CARRY_INTO(a
, quiet_nan
);
336 CARRY_INTO(a
, exponent
);
337 CARRY_INTO(a
, negative
);
345 fprintf(stderr
, "test-i386-fprem: tested %llu cases\n", count
);
348 int main(int argc
, char **argv
)