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/>.
29 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
32 * Inspired by <ieee754.h>'s union ieee854_long_double, but with single
33 * long long mantissa fields and assuming little-endianness for simplicity.
38 /* This is the IEEE 854 double-extended-precision format. */
40 unsigned long long mantissa
:63;
42 unsigned int exponent
:15;
43 unsigned int negative
:1;
44 unsigned int empty
:16;
45 } __attribute__((packed
)) ieee
;
47 /* This is for NaNs in the IEEE 854 double-extended-precision format. */
49 unsigned long long mantissa
:62;
50 unsigned int quiet_nan
:1;
52 unsigned int exponent
:15;
53 unsigned int negative
:1;
54 unsigned int empty
:16;
55 } __attribute__((packed
)) ieee_nan
;
58 #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
60 static const union float80u q_nan
= {
61 .ieee_nan
.negative
= 0, /* X */
62 .ieee_nan
.exponent
= 0x7fff,
64 .ieee_nan
.quiet_nan
= 1,
65 .ieee_nan
.mantissa
= 0,
68 static const union float80u s_nan
= {
69 .ieee_nan
.negative
= 0, /* X */
70 .ieee_nan
.exponent
= 0x7fff,
72 .ieee_nan
.quiet_nan
= 0,
73 .ieee_nan
.mantissa
= 1, /* nonzero */
76 static const union float80u pos_inf
= {
78 .ieee
.exponent
= 0x7fff,
83 static const union float80u pseudo_pos_inf
= { /* "unsupported" */
85 .ieee
.exponent
= 0x7fff,
90 static const union float80u pos_denorm
= {
97 static const union float80u smallest_positive_norm
= {
106 asm volatile ("fninit\n");
109 static long double fprem(long double a
, long double b
, uint16_t *sw
)
112 asm volatile ("fprem\n"
114 : "=t" (result
), "=m" (*sw
)
120 static long double fprem1(long double a
, long double b
, uint16_t *sw
)
123 asm volatile ("fprem1\n"
125 : "=t" (result
), "=m" (*sw
)
131 #define FPUS_IE (1 << 0)
132 #define FPUS_DE (1 << 1)
133 #define FPUS_ZE (1 << 2)
134 #define FPUS_OE (1 << 3)
135 #define FPUS_UE (1 << 4)
136 #define FPUS_PE (1 << 5)
137 #define FPUS_SF (1 << 6)
138 #define FPUS_SE (1 << 7)
139 #define FPUS_C0 (1 << 8)
140 #define FPUS_C1 (1 << 9)
141 #define FPUS_C2 (1 << 10)
142 #define FPUS_TOP 0x3800
143 #define FPUS_C3 (1 << 14)
144 #define FPUS_B (1 << 15)
146 #define FPUS_EMASK 0x007f
150 static void psw(uint16_t sw
)
152 printf("SW: C3 TopC2C1C0\n");
153 printf("SW: %c %d %3d %d %d %d %c %c %c %c %c %c %c %c\n",
154 sw
& FPUS_B
? 'B' : 'b',
156 (sw
& FPUS_TOP
) >> 11,
160 (sw
& FPUS_SE
) ? 'S' : 's',
161 (sw
& FPUS_SF
) ? 'F' : 'f',
162 (sw
& FPUS_PE
) ? 'P' : 'p',
163 (sw
& FPUS_UE
) ? 'U' : 'u',
164 (sw
& FPUS_OE
) ? 'O' : 'o',
165 (sw
& FPUS_ZE
) ? 'Z' : 'z',
166 (sw
& FPUS_DE
) ? 'D' : 'd',
167 (sw
& FPUS_IE
) ? 'I' : 'i');
170 static void do_fprem(long double a
, long double b
)
172 const union float80u au
= {.d
= a
};
173 const union float80u bu
= {.d
= b
};
177 printf("A: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
178 au
.ieee
.negative
, au
.ieee
.exponent
, au
.ieee
.one
,
179 au
.ieee_nan
.quiet_nan
, (unsigned long long)au
.ieee
.mantissa
,
181 printf("B: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
182 bu
.ieee
.negative
, bu
.ieee
.exponent
, bu
.ieee
.one
,
183 bu
.ieee_nan
.quiet_nan
, (unsigned long long)bu
.ieee
.mantissa
,
188 ru
.d
= fprem(a
, b
, &sw
);
191 printf("R : S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
192 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
193 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
197 ru
.d
= fprem1(a
, b
, &sw
);
200 printf("R1: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
201 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
202 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
208 static void do_fprem_stack_underflow(void)
210 const long double a
= 1.0;
215 asm volatile ("fprem\n"
217 : "=t" (ru
.d
), "=m" (sw
)
222 printf("R: S=%d Exp=%04x Int=%d (QNaN=%d) Sig=%016llx (%.06Le)\n",
223 ru
.ieee
.negative
, ru
.ieee
.exponent
, ru
.ieee
.one
,
224 ru
.ieee_nan
.quiet_nan
, (unsigned long long)ru
.ieee
.mantissa
,
229 static void test_fprem_cases(void)
231 printf("= stack underflow =\n");
232 do_fprem_stack_underflow();
234 printf("= invalid operation =\n");
235 do_fprem(q_nan
.d
, 1.0);
236 do_fprem(s_nan
.d
, 1.0);
238 do_fprem(pos_inf
.d
, 1.0);
239 do_fprem(pseudo_pos_inf
.d
, 1.0);
241 printf("= denormal =\n");
242 do_fprem(pos_denorm
.d
, 1.0);
243 do_fprem(1.0, pos_denorm
.d
);
245 do_fprem(smallest_positive_norm
.d
, smallest_positive_norm
.d
);
247 /* printf("= underflow =\n"); */
248 /* TODO: Is there a case where FPREM raises underflow? */
251 static void test_fprem_pairs(void)
253 unsigned long long count
;
255 unsigned int negative_index_a
= 0;
256 unsigned int negative_index_b
= 0;
257 static const unsigned int negative_values
[] = {
262 unsigned int exponent_index_a
= 0;
263 unsigned int exponent_index_b
= 0;
264 static const unsigned int exponent_values
[] = {
268 IEEE854_LONG_DOUBLE_BIAS
- 1,
269 IEEE854_LONG_DOUBLE_BIAS
,
270 IEEE854_LONG_DOUBLE_BIAS
+ 1,
276 unsigned int one_index_a
= 0;
277 unsigned int one_index_b
= 0;
278 static const unsigned int one_values
[] = {
283 unsigned int quiet_nan_index_a
= 0;
284 unsigned int quiet_nan_index_b
= 0;
285 static const unsigned int quiet_nan_values
[] = {
290 unsigned int mantissa_index_a
= 0;
291 unsigned int mantissa_index_b
= 0;
292 static const unsigned long long mantissa_values
[] = {
296 0x3ffffffffffffffdULL
,
297 0x3ffffffffffffffeULL
,
298 0x3fffffffffffffffULL
,
301 for (count
= 0; ; ++count
) {
302 #define INIT_FIELD(var, field) \
303 .ieee_nan.field = field##_values[field##_index_##var]
304 const union float80u a
= {
305 INIT_FIELD(a
, negative
),
306 INIT_FIELD(a
, exponent
),
308 INIT_FIELD(a
, quiet_nan
),
309 INIT_FIELD(a
, mantissa
),
311 const union float80u b
= {
312 INIT_FIELD(b
, negative
),
313 INIT_FIELD(b
, exponent
),
315 INIT_FIELD(b
, quiet_nan
),
316 INIT_FIELD(b
, mantissa
),
323 #define CARRY_INTO(var, field) do { \
325 if (++field##_index_##var == ARRAY_SIZE(field##_values)) { \
326 field##_index_##var = 0; \
332 CARRY_INTO(b
, mantissa
);
333 CARRY_INTO(b
, quiet_nan
);
335 CARRY_INTO(b
, exponent
);
336 CARRY_INTO(b
, negative
);
337 CARRY_INTO(a
, mantissa
);
338 CARRY_INTO(a
, quiet_nan
);
340 CARRY_INTO(a
, exponent
);
341 CARRY_INTO(a
, negative
);
349 fprintf(stderr
, "test-i386-fprem: tested %llu cases\n", count
);
352 int main(int argc
, char **argv
)