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/>.
26 #include "qemu/osdep.h"
29 * Inspired by <ieee754.h>'s union ieee854_long_double, but with single
30 * long long mantissa fields and assuming little-endianness for simplicity.
35 /* This is the IEEE 854 double-extended-precision format. */
37 unsigned long long mantissa
:63;
39 unsigned int exponent
:15;
40 unsigned int negative
:1;
41 unsigned int empty
:16;
44 /* This is for NaNs in the IEEE 854 double-extended-precision format. */
46 unsigned long long mantissa
:62;
47 unsigned int quiet_nan
:1;
49 unsigned int exponent
:15;
50 unsigned int negative
:1;
51 unsigned int empty
:16;
52 } QEMU_PACKED ieee_nan
;
55 #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
57 static const union float80u q_nan
= {
58 .ieee_nan
.negative
= 0, /* X */
59 .ieee_nan
.exponent
= 0x7fff,
61 .ieee_nan
.quiet_nan
= 1,
62 .ieee_nan
.mantissa
= 0,
65 static const union float80u s_nan
= {
66 .ieee_nan
.negative
= 0, /* X */
67 .ieee_nan
.exponent
= 0x7fff,
69 .ieee_nan
.quiet_nan
= 0,
70 .ieee_nan
.mantissa
= 1, /* nonzero */
73 static const union float80u pos_inf
= {
75 .ieee
.exponent
= 0x7fff,
80 static const union float80u pseudo_pos_inf
= { /* "unsupported" */
82 .ieee
.exponent
= 0x7fff,
87 static const union float80u pos_denorm
= {
94 static const union float80u smallest_positive_norm
= {
103 asm volatile ("fninit\n");
106 static long double fprem(long double a
, long double b
, uint16_t *sw
)
109 asm volatile ("fprem\n"
111 : "=t" (result
), "=m" (*sw
)
117 static long double fprem1(long double a
, long double b
, uint16_t *sw
)
120 asm volatile ("fprem1\n"
122 : "=t" (result
), "=m" (*sw
)
128 #define FPUS_IE (1 << 0)
129 #define FPUS_DE (1 << 1)
130 #define FPUS_ZE (1 << 2)
131 #define FPUS_OE (1 << 3)
132 #define FPUS_UE (1 << 4)
133 #define FPUS_PE (1 << 5)
134 #define FPUS_SF (1 << 6)
135 #define FPUS_SE (1 << 7)
136 #define FPUS_C0 (1 << 8)
137 #define FPUS_C1 (1 << 9)
138 #define FPUS_C2 (1 << 10)
139 #define FPUS_TOP 0x3800
140 #define FPUS_C3 (1 << 14)
141 #define FPUS_B (1 << 15)
143 #define FPUS_EMASK 0x007f
147 static void psw(uint16_t sw
)
149 printf("SW: C3 TopC2C1C0\n");
150 printf("SW: %c %d %3d %d %d %d %c %c %c %c %c %c %c %c\n",
151 sw
& FPUS_B
? 'B' : 'b',
153 (sw
& FPUS_TOP
) >> 11,
157 (sw
& FPUS_SE
) ? 'S' : 's',
158 (sw
& FPUS_SF
) ? 'F' : 'f',
159 (sw
& FPUS_PE
) ? 'P' : 'p',
160 (sw
& FPUS_UE
) ? 'U' : 'u',
161 (sw
& FPUS_OE
) ? 'O' : 'o',
162 (sw
& FPUS_ZE
) ? 'Z' : 'z',
163 (sw
& FPUS_DE
) ? 'D' : 'd',
164 (sw
& FPUS_IE
) ? 'I' : 'i');
167 static void do_fprem(long double a
, long double b
)
169 const union float80u au
= {.d
= a
};
170 const union float80u bu
= {.d
= b
};
174 printf("A: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
175 au
.ieee
.negative
, au
.ieee
.exponent
, au
.ieee
.one
,
176 au
.ieee_nan
.quiet_nan
, (unsigned long long)au
.ieee
.mantissa
,
178 printf("B: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
179 bu
.ieee
.negative
, bu
.ieee
.exponent
, bu
.ieee
.one
,
180 bu
.ieee_nan
.quiet_nan
, (unsigned long long)bu
.ieee
.mantissa
,
185 ru
.d
= fprem(a
, b
, &sw
);
188 printf("R : S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
189 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
190 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
194 ru
.d
= fprem1(a
, b
, &sw
);
197 printf("R1: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
198 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
199 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
205 static void do_fprem_stack_underflow(void)
207 const long double a
= 1.0;
212 asm volatile ("fprem\n"
214 : "=t" (ru
.d
), "=m" (sw
)
219 printf("R: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
220 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
221 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
226 static void test_fprem_cases(void)
228 printf("= stack underflow =\n");
229 do_fprem_stack_underflow();
231 printf("= invalid operation =\n");
232 do_fprem(s_nan
.d
, 1.0);
234 do_fprem(pos_inf
.d
, 1.0);
235 do_fprem(pseudo_pos_inf
.d
, 1.0);
237 printf("= denormal =\n");
238 do_fprem(pos_denorm
.d
, 1.0);
239 do_fprem(1.0, pos_denorm
.d
);
241 /* printf("= underflow =\n"); */
242 /* TODO: Is there a case where FPREM raises underflow? */
245 static void test_fprem_pairs(void)
247 unsigned long long count
;
249 unsigned int negative_index_a
= 0;
250 unsigned int negative_index_b
= 0;
251 static const unsigned int negative_values
[] = {
256 unsigned int exponent_index_a
= 0;
257 unsigned int exponent_index_b
= 0;
258 static const unsigned int exponent_values
[] = {
262 IEEE854_LONG_DOUBLE_BIAS
- 1,
263 IEEE854_LONG_DOUBLE_BIAS
,
264 IEEE854_LONG_DOUBLE_BIAS
+ 1,
270 unsigned int one_index_a
= 0;
271 unsigned int one_index_b
= 0;
272 static const unsigned int one_values
[] = {
277 unsigned int quiet_nan_index_a
= 0;
278 unsigned int quiet_nan_index_b
= 0;
279 static const unsigned int quiet_nan_values
[] = {
284 unsigned int mantissa_index_a
= 0;
285 unsigned int mantissa_index_b
= 0;
286 static const unsigned long long mantissa_values
[] = {
290 0x3ffffffffffffffdULL
,
291 0x3ffffffffffffffeULL
,
292 0x3fffffffffffffffULL
,
295 for (count
= 0; ; ++count
) {
296 #define INIT_FIELD(var, field) \
297 .ieee_nan.field = field##_values[field##_index_##var]
298 const union float80u a
= {
299 INIT_FIELD(a
, negative
),
300 INIT_FIELD(a
, exponent
),
302 INIT_FIELD(a
, quiet_nan
),
303 INIT_FIELD(a
, mantissa
),
305 const union float80u b
= {
306 INIT_FIELD(b
, negative
),
307 INIT_FIELD(b
, exponent
),
309 INIT_FIELD(b
, quiet_nan
),
310 INIT_FIELD(b
, mantissa
),
317 #define CARRY_INTO(var, field) do { \
319 if (++field##_index_##var == ARRAY_SIZE(field##_values)) { \
320 field##_index_##var = 0; \
326 CARRY_INTO(b
, mantissa
);
327 CARRY_INTO(b
, quiet_nan
);
329 CARRY_INTO(b
, exponent
);
330 CARRY_INTO(b
, negative
);
331 CARRY_INTO(a
, mantissa
);
332 CARRY_INTO(a
, quiet_nan
);
334 CARRY_INTO(a
, exponent
);
335 CARRY_INTO(a
, negative
);
343 fprintf(stderr
, "test-i386-fprem: tested %llu cases\n", count
);
346 int main(int argc
, char **argv
)