Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/target/i386/tcg/fpu_helper.c
Line
Count
Source
1
/*
2
 *  x86 FPU, MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI helpers
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "qemu/osdep.h"
21
#include <math.h>
22
#include "cpu.h"
23
#include "tcg-cpu.h"
24
#include "exec/cputlb.h"
25
#include "accel/tcg/cpu-ldst.h"
26
#include "exec/helper-proto.h"
27
#include "fpu/softfloat.h"
28
#include "fpu/softfloat-macros.h"
29
#include "helper-tcg.h"
30
#include "access.h"
31
32
/* float macros */
33
0
#define FT0    (env->ft0)
34
0
#define ST0    (env->fpregs[env->fpstt].d)
35
0
#define ST(n)  (env->fpregs[(env->fpstt + (n)) & 7].d)
36
0
#define ST1    ST(1)
37
38
0
#define FPU_RC_SHIFT        10
39
0
#define FPU_RC_MASK         (3 << FPU_RC_SHIFT)
40
#define FPU_RC_NEAR         0x000
41
0
#define FPU_RC_DOWN         0x400
42
0
#define FPU_RC_UP           0x800
43
0
#define FPU_RC_CHOP         0xc00
44
45
0
#define MAXTAN 9223372036854775808.0
46
47
/* the following deal with x86 long double-precision numbers */
48
0
#define MAXEXPD 0x7fff
49
0
#define EXPBIAS 16383
50
0
#define EXPD(fp)        (fp.l.upper & 0x7fff)
51
0
#define SIGND(fp)       ((fp.l.upper) & 0x8000)
52
0
#define MANTD(fp)       (fp.l.lower)
53
0
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
54
55
0
#define FPUS_IE (1 << 0)
56
0
#define FPUS_DE (1 << 1)
57
0
#define FPUS_ZE (1 << 2)
58
0
#define FPUS_OE (1 << 3)
59
0
#define FPUS_UE (1 << 4)
60
0
#define FPUS_PE (1 << 5)
61
#define FPUS_SF (1 << 6)
62
0
#define FPUS_SE (1 << 7)
63
0
#define FPUS_B  (1 << 15)
64
65
0
#define FPUC_EM 0x3f
66
67
0
#define floatx80_lg2 make_floatx80(0x3ffd, 0x9a209a84fbcff799LL)
68
0
#define floatx80_lg2_d make_floatx80(0x3ffd, 0x9a209a84fbcff798LL)
69
0
#define floatx80_l2e make_floatx80(0x3fff, 0xb8aa3b295c17f0bcLL)
70
0
#define floatx80_l2e_d make_floatx80(0x3fff, 0xb8aa3b295c17f0bbLL)
71
0
#define floatx80_l2t make_floatx80(0x4000, 0xd49a784bcd1b8afeLL)
72
0
#define floatx80_l2t_u make_floatx80(0x4000, 0xd49a784bcd1b8affLL)
73
0
#define floatx80_ln2_d make_floatx80(0x3ffe, 0xb17217f7d1cf79abLL)
74
0
#define floatx80_pi_d make_floatx80(0x4000, 0xc90fdaa22168c234LL)
75
76
static inline void fpush(CPUX86State *env)
77
0
{
78
0
    env->fpstt = (env->fpstt - 1) & 7;
79
0
    env->fptags[env->fpstt] = 0; /* validate stack entry */
80
0
}
81
82
static inline void fpop(CPUX86State *env)
83
0
{
84
0
    env->fptags[env->fpstt] = 1; /* invalidate stack entry */
85
0
    env->fpstt = (env->fpstt + 1) & 7;
86
0
}
87
88
static floatx80 do_fldt(X86Access *ac, target_ulong ptr)
89
0
{
90
0
    CPU_LDoubleU temp;
91
92
0
    temp.l.lower = access_ldq(ac, ptr);
93
0
    temp.l.upper = access_ldw(ac, ptr + 8);
94
0
    return temp.d;
95
0
}
96
97
static void do_fstt(X86Access *ac, target_ulong ptr, floatx80 f)
98
0
{
99
0
    CPU_LDoubleU temp;
100
101
0
    temp.d = f;
102
0
    access_stq(ac, ptr, temp.l.lower);
103
0
    access_stw(ac, ptr + 8, temp.l.upper);
104
0
}
105
106
/* x87 FPU helpers */
107
108
static inline double floatx80_to_double(CPUX86State *env, floatx80 a)
109
0
{
110
0
    union {
111
0
        float64 f64;
112
0
        double d;
113
0
    } u;
114
115
0
    u.f64 = floatx80_to_float64(a, &env->fp_status);
116
0
    return u.d;
117
0
}
118
119
static inline floatx80 double_to_floatx80(CPUX86State *env, double a)
120
0
{
121
0
    union {
122
0
        float64 f64;
123
0
        double d;
124
0
    } u;
125
126
0
    u.d = a;
127
0
    return float64_to_floatx80(u.f64, &env->fp_status);
128
0
}
129
130
static void fpu_set_exception(CPUX86State *env, int mask)
131
0
{
132
0
    env->fpus |= mask;
133
0
    if (env->fpus & (~env->fpuc & FPUC_EM)) {
134
0
        env->fpus |= FPUS_SE | FPUS_B;
135
0
    }
136
0
}
137
138
void cpu_init_fp_statuses(CPUX86State *env)
139
0
{
140
    /*
141
     * Initialise the non-runtime-varying fields of the various
142
     * float_status words to x86 behaviour. This must be called at
143
     * CPU reset because the float_status words are in the
144
     * "zeroed on reset" portion of the CPU state struct.
145
     * Fields in float_status that vary under guest control are set
146
     * via the codepath for setting that register, eg cpu_set_fpuc().
147
     */
148
    /*
149
     * Use x87 NaN propagation rules:
150
     * SNaN + QNaN => return the QNaN
151
     * two SNaNs => return the one with the larger significand, silenced
152
     * two QNaNs => return the one with the larger significand
153
     * SNaN and a non-NaN => return the SNaN, silenced
154
     * QNaN and a non-NaN => return the QNaN
155
     *
156
     * If we get down to comparing significands and they are the same,
157
     * return the NaN with the positive sign bit (if any).
158
     */
159
0
    set_float_2nan_prop_rule(float_2nan_prop_x87, &env->fp_status);
160
    /*
161
     * TODO: These are incorrect: the x86 Software Developer's Manual vol 1
162
     * section 4.8.3.5 "Operating on SNaNs and QNaNs" says that the
163
     * "larger significand" behaviour is only used for x87 FPU operations.
164
     * For SSE the required behaviour is to always return the first NaN,
165
     * which is float_2nan_prop_ab.
166
     *
167
     * mmx_status is used only for the AMD 3DNow! instructions, which
168
     * are documented in the "3DNow! Technology Manual" as not supporting
169
     * NaNs or infinities as inputs. The result of passing two NaNs is
170
     * documented as "undefined", so we can do what we choose.
171
     * (Strictly there is some behaviour we don't implement correctly
172
     * for these "unsupported" NaN and Inf values, like "NaN * 0 == 0".)
173
     */
174
0
    set_float_2nan_prop_rule(float_2nan_prop_x87, &env->mmx_status);
175
0
    set_float_2nan_prop_rule(float_2nan_prop_x87, &env->sse_status);
176
    /*
177
     * Only SSE has multiply-add instructions. In the SDM Section 14.5.2
178
     * "Fused-Multiply-ADD (FMA) Numeric Behavior" the NaN handling is
179
     * specified -- for 0 * inf + NaN the input NaN is selected, and if
180
     * there are multiple input NaNs they are selected in the order a, b, c.
181
     * We also do not raise Invalid for the 0 * inf + (Q)NaN case.
182
     */
183
0
    set_float_infzeronan_rule(float_infzeronan_dnan_never |
184
0
                              float_infzeronan_suppress_invalid,
185
0
                              &env->sse_status);
186
0
    set_float_3nan_prop_rule(float_3nan_prop_abc, &env->sse_status);
187
    /* Default NaN: sign bit set, most significant frac bit set */
188
0
    set_float_default_nan_pattern(0b11000000, &env->fp_status);
189
0
    set_float_default_nan_pattern(0b11000000, &env->mmx_status);
190
0
    set_float_default_nan_pattern(0b11000000, &env->sse_status);
191
    /*
192
     * x86 does flush-to-zero detection after rounding (the SDM
193
     * section 10.2.3.3 on the FTZ bit of MXCSR says that we flush
194
     * when we detect underflow, which x86 does after rounding).
195
     */
196
0
    set_float_ftz_detection(float_ftz_after_rounding, &env->fp_status);
197
0
    set_float_ftz_detection(float_ftz_after_rounding, &env->mmx_status);
198
0
    set_float_ftz_detection(float_ftz_after_rounding, &env->sse_status);
199
0
}
200
201
static inline int save_exception_flags(CPUX86State *env)
202
0
{
203
0
    int old_flags = get_float_exception_flags(&env->fp_status);
204
0
    set_float_exception_flags(0, &env->fp_status);
205
0
    return old_flags;
206
0
}
207
208
static void merge_exception_flags(CPUX86State *env, int old_flags)
209
0
{
210
0
    int new_flags = get_float_exception_flags(&env->fp_status);
211
0
    float_raise(old_flags, &env->fp_status);
212
0
    fpu_set_exception(env,
213
0
                      ((new_flags & float_flag_invalid ? FPUS_IE : 0) |
214
0
                       (new_flags & float_flag_divbyzero ? FPUS_ZE : 0) |
215
0
                       (new_flags & float_flag_overflow ? FPUS_OE : 0) |
216
0
                       (new_flags & float_flag_underflow ? FPUS_UE : 0) |
217
0
                       (new_flags & float_flag_inexact ? FPUS_PE : 0) |
218
0
                       (new_flags & float_flag_input_denormal_used ? FPUS_DE : 0)));
219
0
}
220
221
static inline floatx80 helper_fdiv(CPUX86State *env, floatx80 a, floatx80 b)
222
0
{
223
0
    int old_flags = save_exception_flags(env);
224
0
    floatx80 ret = floatx80_div(a, b, &env->fp_status);
225
0
    merge_exception_flags(env, old_flags);
226
0
    return ret;
227
0
}
228
229
static void fpu_raise_exception(CPUX86State *env, uintptr_t retaddr)
230
0
{
231
0
    if (env->cr[0] & CR0_NE_MASK) {
232
0
        raise_exception_ra(env, EXCP10_COPR, retaddr);
233
0
    }
234
0
#if !defined(CONFIG_USER_ONLY)
235
0
    else {
236
0
        fpu_check_raise_ferr_irq(env);
237
0
    }
238
0
#endif
239
0
}
240
241
void HELPER(flds_FT0)(CPUX86State *env, uint32_t val)
242
0
{
243
0
    int old_flags = save_exception_flags(env);
244
0
    union {
245
0
        float32 f;
246
0
        uint32_t i;
247
0
    } u;
248
249
0
    u.i = val;
250
0
    FT0 = float32_to_floatx80(u.f, &env->fp_status);
251
0
    merge_exception_flags(env, old_flags);
252
0
}
253
254
void HELPER(fldl_FT0)(CPUX86State *env, uint64_t val)
255
0
{
256
0
    int old_flags = save_exception_flags(env);
257
0
    union {
258
0
        float64 f;
259
0
        uint64_t i;
260
0
    } u;
261
262
0
    u.i = val;
263
0
    FT0 = float64_to_floatx80(u.f, &env->fp_status);
264
0
    merge_exception_flags(env, old_flags);
265
0
}
266
267
void HELPER(fildl_FT0)(CPUX86State *env, int32_t val)
268
0
{
269
0
    FT0 = int32_to_floatx80(val, &env->fp_status);
270
0
}
271
272
void HELPER(flds_ST0)(CPUX86State *env, uint32_t val)
273
0
{
274
0
    int old_flags = save_exception_flags(env);
275
0
    int new_fpstt;
276
0
    union {
277
0
        float32 f;
278
0
        uint32_t i;
279
0
    } u;
280
281
0
    new_fpstt = (env->fpstt - 1) & 7;
282
0
    u.i = val;
283
0
    env->fpregs[new_fpstt].d = float32_to_floatx80(u.f, &env->fp_status);
284
0
    env->fpstt = new_fpstt;
285
0
    env->fptags[new_fpstt] = 0; /* validate stack entry */
286
0
    merge_exception_flags(env, old_flags);
287
0
}
288
289
void HELPER(fldl_ST0)(CPUX86State *env, uint64_t val)
290
0
{
291
0
    int old_flags = save_exception_flags(env);
292
0
    int new_fpstt;
293
0
    union {
294
0
        float64 f;
295
0
        uint64_t i;
296
0
    } u;
297
298
0
    new_fpstt = (env->fpstt - 1) & 7;
299
0
    u.i = val;
300
0
    env->fpregs[new_fpstt].d = float64_to_floatx80(u.f, &env->fp_status);
301
0
    env->fpstt = new_fpstt;
302
0
    env->fptags[new_fpstt] = 0; /* validate stack entry */
303
0
    merge_exception_flags(env, old_flags);
304
0
}
305
306
static FloatX80RoundPrec tmp_maximise_precision(float_status *st)
307
0
{
308
0
    FloatX80RoundPrec old = get_floatx80_rounding_precision(st);
309
0
    set_floatx80_rounding_precision(floatx80_precision_x, st);
310
0
    return old;
311
0
}
312
313
void HELPER(fildl_ST0)(CPUX86State *env, int32_t val)
314
0
{
315
0
    int new_fpstt;
316
0
    FloatX80RoundPrec old = tmp_maximise_precision(&env->fp_status);
317
318
0
    new_fpstt = (env->fpstt - 1) & 7;
319
0
    env->fpregs[new_fpstt].d = int32_to_floatx80(val, &env->fp_status);
320
0
    env->fpstt = new_fpstt;
321
0
    env->fptags[new_fpstt] = 0; /* validate stack entry */
322
323
0
    set_floatx80_rounding_precision(old, &env->fp_status);
324
0
}
325
326
void HELPER(fildll_ST0)(CPUX86State *env, int64_t val)
327
0
{
328
0
    int new_fpstt;
329
0
    FloatX80RoundPrec old = tmp_maximise_precision(&env->fp_status);
330
331
0
    new_fpstt = (env->fpstt - 1) & 7;
332
0
    env->fpregs[new_fpstt].d = int64_to_floatx80(val, &env->fp_status);
333
0
    env->fpstt = new_fpstt;
334
0
    env->fptags[new_fpstt] = 0; /* validate stack entry */
335
336
0
    set_floatx80_rounding_precision(old, &env->fp_status);
337
0
}
338
339
uint32_t HELPER(fsts_ST0)(CPUX86State *env)
340
0
{
341
0
    int old_flags = save_exception_flags(env);
342
0
    union {
343
0
        float32 f;
344
0
        uint32_t i;
345
0
    } u;
346
347
0
    u.f = floatx80_to_float32(ST0, &env->fp_status);
348
0
    merge_exception_flags(env, old_flags);
349
0
    return u.i;
350
0
}
351
352
uint64_t HELPER(fstl_ST0)(CPUX86State *env)
353
0
{
354
0
    int old_flags = save_exception_flags(env);
355
0
    union {
356
0
        float64 f;
357
0
        uint64_t i;
358
0
    } u;
359
360
0
    u.f = floatx80_to_float64(ST0, &env->fp_status);
361
0
    merge_exception_flags(env, old_flags);
362
0
    return u.i;
363
0
}
364
365
int32_t HELPER(fist_ST0)(CPUX86State *env)
366
0
{
367
0
    int old_flags = save_exception_flags(env);
368
0
    int32_t val;
369
370
0
    val = floatx80_to_int32(ST0, &env->fp_status);
371
0
    if (val != (int16_t)val) {
372
0
        set_float_exception_flags(float_flag_invalid, &env->fp_status);
373
0
        val = -32768;
374
0
    }
375
0
    merge_exception_flags(env, old_flags);
376
0
    return val;
377
0
}
378
379
int32_t HELPER(fistl_ST0)(CPUX86State *env)
380
0
{
381
0
    int old_flags = save_exception_flags(env);
382
0
    int32_t val;
383
384
0
    val = floatx80_to_int32(ST0, &env->fp_status);
385
0
    if (get_float_exception_flags(&env->fp_status) & float_flag_invalid) {
386
0
        val = 0x80000000;
387
0
    }
388
0
    merge_exception_flags(env, old_flags);
389
0
    return val;
390
0
}
391
392
int64_t HELPER(fistll_ST0)(CPUX86State *env)
393
0
{
394
0
    int old_flags = save_exception_flags(env);
395
0
    int64_t val;
396
397
0
    val = floatx80_to_int64(ST0, &env->fp_status);
398
0
    if (get_float_exception_flags(&env->fp_status) & float_flag_invalid) {
399
0
        val = 0x8000000000000000ULL;
400
0
    }
401
0
    merge_exception_flags(env, old_flags);
402
0
    return val;
403
0
}
404
405
int32_t HELPER(fistt_ST0)(CPUX86State *env)
406
0
{
407
0
    int old_flags = save_exception_flags(env);
408
0
    int32_t val;
409
410
0
    val = floatx80_to_int32_round_to_zero(ST0, &env->fp_status);
411
0
    if (val != (int16_t)val) {
412
0
        set_float_exception_flags(float_flag_invalid, &env->fp_status);
413
0
        val = -32768;
414
0
    }
415
0
    merge_exception_flags(env, old_flags);
416
0
    return val;
417
0
}
418
419
int32_t HELPER(fisttl_ST0)(CPUX86State *env)
420
0
{
421
0
    int old_flags = save_exception_flags(env);
422
0
    int32_t val;
423
424
0
    val = floatx80_to_int32_round_to_zero(ST0, &env->fp_status);
425
0
    if (get_float_exception_flags(&env->fp_status) & float_flag_invalid) {
426
0
        val = 0x80000000;
427
0
    }
428
0
    merge_exception_flags(env, old_flags);
429
0
    return val;
430
0
}
431
432
int64_t HELPER(fisttll_ST0)(CPUX86State *env)
433
0
{
434
0
    int old_flags = save_exception_flags(env);
435
0
    int64_t val;
436
437
0
    val = floatx80_to_int64_round_to_zero(ST0, &env->fp_status);
438
0
    if (get_float_exception_flags(&env->fp_status) & float_flag_invalid) {
439
0
        val = 0x8000000000000000ULL;
440
0
    }
441
0
    merge_exception_flags(env, old_flags);
442
0
    return val;
443
0
}
444
445
void HELPER(fldt_ST0)(CPUX86State *env, target_ulong ptr)
446
0
{
447
0
    int new_fpstt;
448
0
    X86Access ac;
449
450
0
    access_prepare(&ac, env, ptr, 10, MMU_DATA_LOAD, GETPC());
451
452
0
    new_fpstt = (env->fpstt - 1) & 7;
453
0
    env->fpregs[new_fpstt].d = do_fldt(&ac, ptr);
454
0
    env->fpstt = new_fpstt;
455
0
    env->fptags[new_fpstt] = 0; /* validate stack entry */
456
0
}
457
458
void HELPER(fstt_ST0)(CPUX86State *env, target_ulong ptr)
459
0
{
460
0
    X86Access ac;
461
462
0
    access_prepare(&ac, env, ptr, 10, MMU_DATA_STORE, GETPC());
463
0
    do_fstt(&ac, ptr, ST0);
464
0
}
465
466
void HELPER(fpush)(CPUX86State *env)
467
0
{
468
0
    fpush(env);
469
0
}
470
471
void HELPER(fpop)(CPUX86State *env)
472
0
{
473
0
    fpop(env);
474
0
}
475
476
void HELPER(fdecstp)(CPUX86State *env)
477
0
{
478
0
    env->fpstt = (env->fpstt - 1) & 7;
479
0
    env->fpus &= ~0x4700;
480
0
}
481
482
void HELPER(fincstp)(CPUX86State *env)
483
0
{
484
0
    env->fpstt = (env->fpstt + 1) & 7;
485
0
    env->fpus &= ~0x4700;
486
0
}
487
488
/* FPU move */
489
490
void HELPER(ffree_STN)(CPUX86State *env, int st_index)
491
0
{
492
0
    env->fptags[(env->fpstt + st_index) & 7] = 1;
493
0
}
494
495
void HELPER(fmov_ST0_FT0)(CPUX86State *env)
496
0
{
497
0
    ST0 = FT0;
498
0
}
499
500
void HELPER(fmov_FT0_STN)(CPUX86State *env, int st_index)
501
0
{
502
0
    FT0 = ST(st_index);
503
0
}
504
505
void HELPER(fmov_ST0_STN)(CPUX86State *env, int st_index)
506
0
{
507
0
    ST0 = ST(st_index);
508
0
}
509
510
void HELPER(fmov_STN_ST0)(CPUX86State *env, int st_index)
511
0
{
512
0
    ST(st_index) = ST0;
513
0
    env->fptags[(env->fpstt + st_index) & 7] = 0;
514
0
}
515
516
void HELPER(fxchg_ST0_STN)(CPUX86State *env, int st_index)
517
0
{
518
0
    floatx80 tmp;
519
520
0
    tmp = ST(st_index);
521
0
    ST(st_index) = ST0;
522
0
    ST0 = tmp;
523
524
0
    env->fptags[env->fpstt] = 0;
525
0
    env->fptags[(env->fpstt + st_index) & 7] = 0;
526
527
    /* C1 is unconditionally cleared to 0 */
528
0
    env->fpus &= ~0x0200;
529
0
}
530
531
/* FPU operations */
532
533
static const int fcom_ccval[4] = {0x0100, 0x4000, 0x0000, 0x4500};
534
535
void HELPER(fcom_ST0_FT0)(CPUX86State *env)
536
0
{
537
0
    int old_flags = save_exception_flags(env);
538
0
    FloatRelation ret;
539
540
0
    ret = floatx80_compare(ST0, FT0, &env->fp_status);
541
    /* C1 is unconditionally cleared to 0 */
542
0
    env->fpus = (env->fpus & ~0x4700) | fcom_ccval[ret + 1];
543
0
    merge_exception_flags(env, old_flags);
544
0
}
545
546
void HELPER(fucom_ST0_FT0)(CPUX86State *env)
547
0
{
548
0
    int old_flags = save_exception_flags(env);
549
0
    FloatRelation ret;
550
551
0
    ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
552
    /* C1 is unconditionally cleared to 0 */
553
0
    env->fpus = (env->fpus & ~0x4700) | fcom_ccval[ret + 1];
554
0
    merge_exception_flags(env, old_flags);
555
0
}
556
557
static const int fcomi_ccval[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};
558
559
void HELPER(fcomi_ST0_FT0)(CPUX86State *env)
560
0
{
561
0
    int old_flags = save_exception_flags(env);
562
0
    FloatRelation ret;
563
564
0
    ret = floatx80_compare(ST0, FT0, &env->fp_status);
565
    /* OF, SF, and AF are unconditionally cleared to 0 */
566
0
    CC_SRC = fcomi_ccval[ret + 1];
567
0
    CC_OP = CC_OP_EFLAGS;
568
0
    merge_exception_flags(env, old_flags);
569
0
}
570
571
void HELPER(fucomi_ST0_FT0)(CPUX86State *env)
572
0
{
573
0
    int old_flags = save_exception_flags(env);
574
0
    FloatRelation ret;
575
576
0
    ret = floatx80_compare_quiet(ST0, FT0, &env->fp_status);
577
    /* OF, SF, and AF are unconditionally cleared to 0 */
578
0
    CC_SRC = fcomi_ccval[ret + 1];
579
0
    CC_OP = CC_OP_EFLAGS;
580
0
    merge_exception_flags(env, old_flags);
581
0
}
582
583
void HELPER(fadd_ST0_FT0)(CPUX86State *env)
584
0
{
585
0
    int old_flags = save_exception_flags(env);
586
0
    ST0 = floatx80_add(ST0, FT0, &env->fp_status);
587
0
    merge_exception_flags(env, old_flags);
588
0
}
589
590
void HELPER(fmul_ST0_FT0)(CPUX86State *env)
591
0
{
592
0
    int old_flags = save_exception_flags(env);
593
0
    ST0 = floatx80_mul(ST0, FT0, &env->fp_status);
594
0
    merge_exception_flags(env, old_flags);
595
0
}
596
597
void HELPER(fsub_ST0_FT0)(CPUX86State *env)
598
0
{
599
0
    int old_flags = save_exception_flags(env);
600
0
    ST0 = floatx80_sub(ST0, FT0, &env->fp_status);
601
0
    merge_exception_flags(env, old_flags);
602
0
}
603
604
void HELPER(fsubr_ST0_FT0)(CPUX86State *env)
605
0
{
606
0
    int old_flags = save_exception_flags(env);
607
0
    ST0 = floatx80_sub(FT0, ST0, &env->fp_status);
608
0
    merge_exception_flags(env, old_flags);
609
0
}
610
611
void HELPER(fdiv_ST0_FT0)(CPUX86State *env)
612
0
{
613
0
    ST0 = helper_fdiv(env, ST0, FT0);
614
0
}
615
616
void HELPER(fdivr_ST0_FT0)(CPUX86State *env)
617
0
{
618
0
    ST0 = helper_fdiv(env, FT0, ST0);
619
0
}
620
621
/* fp operations between STN and ST0 */
622
623
void HELPER(fadd_STN_ST0)(CPUX86State *env, int st_index)
624
0
{
625
0
    int old_flags = save_exception_flags(env);
626
0
    ST(st_index) = floatx80_add(ST(st_index), ST0, &env->fp_status);
627
0
    merge_exception_flags(env, old_flags);
628
0
}
629
630
void HELPER(fmul_STN_ST0)(CPUX86State *env, int st_index)
631
0
{
632
0
    int old_flags = save_exception_flags(env);
633
0
    ST(st_index) = floatx80_mul(ST(st_index), ST0, &env->fp_status);
634
0
    merge_exception_flags(env, old_flags);
635
0
}
636
637
void HELPER(fsub_STN_ST0)(CPUX86State *env, int st_index)
638
0
{
639
0
    int old_flags = save_exception_flags(env);
640
0
    ST(st_index) = floatx80_sub(ST(st_index), ST0, &env->fp_status);
641
0
    merge_exception_flags(env, old_flags);
642
0
}
643
644
void HELPER(fsubr_STN_ST0)(CPUX86State *env, int st_index)
645
0
{
646
0
    int old_flags = save_exception_flags(env);
647
0
    ST(st_index) = floatx80_sub(ST0, ST(st_index), &env->fp_status);
648
0
    merge_exception_flags(env, old_flags);
649
0
}
650
651
void HELPER(fdiv_STN_ST0)(CPUX86State *env, int st_index)
652
0
{
653
0
    floatx80 *p;
654
655
0
    p = &ST(st_index);
656
0
    *p = helper_fdiv(env, *p, ST0);
657
0
}
658
659
void HELPER(fdivr_STN_ST0)(CPUX86State *env, int st_index)
660
0
{
661
0
    floatx80 *p;
662
663
0
    p = &ST(st_index);
664
0
    *p = helper_fdiv(env, ST0, *p);
665
0
}
666
667
/* misc FPU operations */
668
void HELPER(fchs_ST0)(CPUX86State *env)
669
0
{
670
0
    ST0 = floatx80_chs(ST0);
671
0
}
672
673
void HELPER(fabs_ST0)(CPUX86State *env)
674
0
{
675
0
    ST0 = floatx80_abs(ST0);
676
0
}
677
678
void HELPER(fld1_ST0)(CPUX86State *env)
679
0
{
680
0
    ST0 = floatx80_one;
681
0
}
682
683
void HELPER(fldl2t_ST0)(CPUX86State *env)
684
0
{
685
0
    switch (env->fpuc & FPU_RC_MASK) {
686
0
    case FPU_RC_UP:
687
0
        ST0 = floatx80_l2t_u;
688
0
        break;
689
0
    default:
690
0
        ST0 = floatx80_l2t;
691
0
        break;
692
0
    }
693
0
}
694
695
void HELPER(fldl2e_ST0)(CPUX86State *env)
696
0
{
697
0
    switch (env->fpuc & FPU_RC_MASK) {
698
0
    case FPU_RC_DOWN:
699
0
    case FPU_RC_CHOP:
700
0
        ST0 = floatx80_l2e_d;
701
0
        break;
702
0
    default:
703
0
        ST0 = floatx80_l2e;
704
0
        break;
705
0
    }
706
0
}
707
708
void HELPER(fldpi_ST0)(CPUX86State *env)
709
0
{
710
0
    switch (env->fpuc & FPU_RC_MASK) {
711
0
    case FPU_RC_DOWN:
712
0
    case FPU_RC_CHOP:
713
0
        ST0 = floatx80_pi_d;
714
0
        break;
715
0
    default:
716
0
        ST0 = floatx80_pi;
717
0
        break;
718
0
    }
719
0
}
720
721
void HELPER(fldlg2_ST0)(CPUX86State *env)
722
0
{
723
0
    switch (env->fpuc & FPU_RC_MASK) {
724
0
    case FPU_RC_DOWN:
725
0
    case FPU_RC_CHOP:
726
0
        ST0 = floatx80_lg2_d;
727
0
        break;
728
0
    default:
729
0
        ST0 = floatx80_lg2;
730
0
        break;
731
0
    }
732
0
}
733
734
void HELPER(fldln2_ST0)(CPUX86State *env)
735
0
{
736
0
    switch (env->fpuc & FPU_RC_MASK) {
737
0
    case FPU_RC_DOWN:
738
0
    case FPU_RC_CHOP:
739
0
        ST0 = floatx80_ln2_d;
740
0
        break;
741
0
    default:
742
0
        ST0 = floatx80_ln2;
743
0
        break;
744
0
    }
745
0
}
746
747
void HELPER(fldz_ST0)(CPUX86State *env)
748
0
{
749
0
    ST0 = floatx80_zero;
750
0
}
751
752
void HELPER(fldz_FT0)(CPUX86State *env)
753
0
{
754
0
    FT0 = floatx80_zero;
755
0
}
756
757
uint32_t HELPER(fnstsw)(CPUX86State *env)
758
0
{
759
0
    return (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
760
0
}
761
762
uint32_t HELPER(fnstcw)(CPUX86State *env)
763
0
{
764
0
    return env->fpuc;
765
0
}
766
767
static void set_x86_rounding_mode(unsigned mode, float_status *status)
768
0
{
769
0
    static FloatRoundMode x86_round_mode[4] = {
770
0
        float_round_nearest_even,
771
0
        float_round_down,
772
0
        float_round_up,
773
0
        float_round_to_zero
774
0
    };
775
0
    assert(mode < ARRAY_SIZE(x86_round_mode));
776
0
    set_float_rounding_mode(x86_round_mode[mode], status);
777
0
}
778
779
void update_fp_status(CPUX86State *env)
780
0
{
781
0
    int rnd_mode;
782
0
    FloatX80RoundPrec rnd_prec;
783
784
    /* set rounding mode */
785
0
    rnd_mode = (env->fpuc & FPU_RC_MASK) >> FPU_RC_SHIFT;
786
0
    set_x86_rounding_mode(rnd_mode, &env->fp_status);
787
788
0
    switch ((env->fpuc >> 8) & 3) {
789
0
    case 0:
790
0
        rnd_prec = floatx80_precision_s;
791
0
        break;
792
0
    case 2:
793
0
        rnd_prec = floatx80_precision_d;
794
0
        break;
795
0
    case 3:
796
0
    default:
797
0
        rnd_prec = floatx80_precision_x;
798
0
        break;
799
0
    }
800
0
    set_floatx80_rounding_precision(rnd_prec, &env->fp_status);
801
0
}
802
803
void HELPER(fldcw)(CPUX86State *env, uint32_t val)
804
0
{
805
0
    cpu_set_fpuc(env, val);
806
0
}
807
808
void HELPER(fclex)(CPUX86State *env)
809
0
{
810
0
    env->fpus &= 0x7f00;
811
0
}
812
813
void HELPER(fwait)(CPUX86State *env)
814
0
{
815
0
    if (env->fpus & FPUS_SE) {
816
0
        fpu_raise_exception(env, GETPC());
817
0
    }
818
0
}
819
820
static void do_fninit(CPUX86State *env)
821
0
{
822
0
    env->fpus = 0;
823
0
    env->fpstt = 0;
824
0
    env->fpcs = 0;
825
0
    env->fpds = 0;
826
0
    env->fpip = 0;
827
0
    env->fpdp = 0;
828
0
    cpu_set_fpuc(env, 0x37f);
829
0
    env->fptags[0] = 1;
830
0
    env->fptags[1] = 1;
831
0
    env->fptags[2] = 1;
832
0
    env->fptags[3] = 1;
833
0
    env->fptags[4] = 1;
834
0
    env->fptags[5] = 1;
835
0
    env->fptags[6] = 1;
836
0
    env->fptags[7] = 1;
837
0
}
838
839
void HELPER(fninit)(CPUX86State *env)
840
0
{
841
0
    do_fninit(env);
842
0
}
843
844
/* BCD ops */
845
846
void HELPER(fbld_ST0)(CPUX86State *env, target_ulong ptr)
847
0
{
848
0
    X86Access ac;
849
0
    floatx80 tmp;
850
0
    uint64_t val;
851
0
    unsigned int v;
852
0
    int i;
853
854
0
    access_prepare(&ac, env, ptr, 10, MMU_DATA_LOAD, GETPC());
855
856
0
    val = 0;
857
0
    for (i = 8; i >= 0; i--) {
858
0
        v = access_ldb(&ac, ptr + i);
859
0
        val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
860
0
    }
861
0
    tmp = int64_to_floatx80(val, &env->fp_status);
862
0
    if (access_ldb(&ac, ptr + 9) & 0x80) {
863
0
        tmp = floatx80_chs(tmp);
864
0
    }
865
0
    fpush(env);
866
0
    ST0 = tmp;
867
0
}
868
869
void HELPER(fbst_ST0)(CPUX86State *env, target_ulong ptr)
870
0
{
871
0
    int old_flags = save_exception_flags(env);
872
0
    int v;
873
0
    target_ulong mem_ref, mem_end;
874
0
    int64_t val;
875
0
    CPU_LDoubleU temp;
876
0
    X86Access ac;
877
878
0
    access_prepare(&ac, env, ptr, 10, MMU_DATA_STORE, GETPC());
879
0
    temp.d = ST0;
880
881
0
    val = floatx80_to_int64(ST0, &env->fp_status);
882
0
    mem_ref = ptr;
883
0
    if (val >= 1000000000000000000LL || val <= -1000000000000000000LL) {
884
0
        set_float_exception_flags(float_flag_invalid, &env->fp_status);
885
0
        while (mem_ref < ptr + 7) {
886
0
            access_stb(&ac, mem_ref++, 0);
887
0
        }
888
0
        access_stb(&ac, mem_ref++, 0xc0);
889
0
        access_stb(&ac, mem_ref++, 0xff);
890
0
        access_stb(&ac, mem_ref++, 0xff);
891
0
        merge_exception_flags(env, old_flags);
892
0
        return;
893
0
    }
894
0
    mem_end = mem_ref + 9;
895
0
    if (SIGND(temp)) {
896
0
        access_stb(&ac, mem_end, 0x80);
897
0
        val = -val;
898
0
    } else {
899
0
        access_stb(&ac, mem_end, 0x00);
900
0
    }
901
0
    while (mem_ref < mem_end) {
902
0
        if (val == 0) {
903
0
            break;
904
0
        }
905
0
        v = val % 100;
906
0
        val = val / 100;
907
0
        v = ((v / 10) << 4) | (v % 10);
908
0
        access_stb(&ac, mem_ref++, v);
909
0
    }
910
0
    while (mem_ref < mem_end) {
911
0
        access_stb(&ac, mem_ref++, 0);
912
0
    }
913
0
    merge_exception_flags(env, old_flags);
914
0
}
915
916
/* 128-bit significand of log(2).  */
917
0
#define ln2_sig_high 0xb17217f7d1cf79abULL
918
0
#define ln2_sig_low 0xc9e3b39803f2f6afULL
919
920
/*
921
 * Polynomial coefficients for an approximation to (2^x - 1) / x, on
922
 * the interval [-1/64, 1/64].
923
 */
924
0
#define f2xm1_coeff_0 make_floatx80(0x3ffe, 0xb17217f7d1cf79acULL)
925
0
#define f2xm1_coeff_0_low make_floatx80(0xbfbc, 0xd87edabf495b3762ULL)
926
0
#define f2xm1_coeff_1 make_floatx80(0x3ffc, 0xf5fdeffc162c7543ULL)
927
0
#define f2xm1_coeff_2 make_floatx80(0x3ffa, 0xe35846b82505fcc7ULL)
928
0
#define f2xm1_coeff_3 make_floatx80(0x3ff8, 0x9d955b7dd273b899ULL)
929
0
#define f2xm1_coeff_4 make_floatx80(0x3ff5, 0xaec3ff3c4ef4ac0cULL)
930
0
#define f2xm1_coeff_5 make_floatx80(0x3ff2, 0xa184897c3a7f0de9ULL)
931
0
#define f2xm1_coeff_6 make_floatx80(0x3fee, 0xffe634d0ec30d504ULL)
932
0
#define f2xm1_coeff_7 make_floatx80(0x3feb, 0xb160111d2db515e4ULL)
933
934
struct f2xm1_data {
935
    /*
936
     * A value very close to a multiple of 1/32, such that 2^t and 2^t - 1
937
     * are very close to exact floatx80 values.
938
     */
939
    floatx80 t;
940
    /* The value of 2^t.  */
941
    floatx80 exp2;
942
    /* The value of 2^t - 1.  */
943
    floatx80 exp2m1;
944
};
945
946
static const struct f2xm1_data f2xm1_table[65] = {
947
    { make_floatx80_init(0xbfff, 0x8000000000000000ULL),
948
      make_floatx80_init(0x3ffe, 0x8000000000000000ULL),
949
      make_floatx80_init(0xbffe, 0x8000000000000000ULL) },
950
    { make_floatx80_init(0xbffe, 0xf800000000002e7eULL),
951
      make_floatx80_init(0x3ffe, 0x82cd8698ac2b9160ULL),
952
      make_floatx80_init(0xbffd, 0xfa64f2cea7a8dd40ULL) },
953
    { make_floatx80_init(0xbffe, 0xefffffffffffe960ULL),
954
      make_floatx80_init(0x3ffe, 0x85aac367cc488345ULL),
955
      make_floatx80_init(0xbffd, 0xf4aa7930676ef976ULL) },
956
    { make_floatx80_init(0xbffe, 0xe800000000006f10ULL),
957
      make_floatx80_init(0x3ffe, 0x88980e8092da5c14ULL),
958
      make_floatx80_init(0xbffd, 0xeecfe2feda4b47d8ULL) },
959
    { make_floatx80_init(0xbffe, 0xe000000000008a45ULL),
960
      make_floatx80_init(0x3ffe, 0x8b95c1e3ea8ba2a5ULL),
961
      make_floatx80_init(0xbffd, 0xe8d47c382ae8bab6ULL) },
962
    { make_floatx80_init(0xbffe, 0xd7ffffffffff8a9eULL),
963
      make_floatx80_init(0x3ffe, 0x8ea4398b45cd8116ULL),
964
      make_floatx80_init(0xbffd, 0xe2b78ce97464fdd4ULL) },
965
    { make_floatx80_init(0xbffe, 0xd0000000000019a0ULL),
966
      make_floatx80_init(0x3ffe, 0x91c3d373ab11b919ULL),
967
      make_floatx80_init(0xbffd, 0xdc785918a9dc8dceULL) },
968
    { make_floatx80_init(0xbffe, 0xc7ffffffffff14dfULL),
969
      make_floatx80_init(0x3ffe, 0x94f4efa8fef76836ULL),
970
      make_floatx80_init(0xbffd, 0xd61620ae02112f94ULL) },
971
    { make_floatx80_init(0xbffe, 0xc000000000006530ULL),
972
      make_floatx80_init(0x3ffe, 0x9837f0518db87fbbULL),
973
      make_floatx80_init(0xbffd, 0xcf901f5ce48f008aULL) },
974
    { make_floatx80_init(0xbffe, 0xb7ffffffffff1723ULL),
975
      make_floatx80_init(0x3ffe, 0x9b8d39b9d54eb74cULL),
976
      make_floatx80_init(0xbffd, 0xc8e58c8c55629168ULL) },
977
    { make_floatx80_init(0xbffe, 0xb00000000000b5e1ULL),
978
      make_floatx80_init(0x3ffe, 0x9ef5326091a0c366ULL),
979
      make_floatx80_init(0xbffd, 0xc2159b3edcbe7934ULL) },
980
    { make_floatx80_init(0xbffe, 0xa800000000006f8aULL),
981
      make_floatx80_init(0x3ffe, 0xa27043030c49370aULL),
982
      make_floatx80_init(0xbffd, 0xbb1f79f9e76d91ecULL) },
983
    { make_floatx80_init(0xbffe, 0x9fffffffffff816aULL),
984
      make_floatx80_init(0x3ffe, 0xa5fed6a9b15171cfULL),
985
      make_floatx80_init(0xbffd, 0xb40252ac9d5d1c62ULL) },
986
    { make_floatx80_init(0xbffe, 0x97ffffffffffb621ULL),
987
      make_floatx80_init(0x3ffe, 0xa9a15ab4ea7c30e6ULL),
988
      make_floatx80_init(0xbffd, 0xacbd4a962b079e34ULL) },
989
    { make_floatx80_init(0xbffe, 0x8fffffffffff162bULL),
990
      make_floatx80_init(0x3ffe, 0xad583eea42a1b886ULL),
991
      make_floatx80_init(0xbffd, 0xa54f822b7abc8ef4ULL) },
992
    { make_floatx80_init(0xbffe, 0x87ffffffffff4d34ULL),
993
      make_floatx80_init(0x3ffe, 0xb123f581d2ac7b51ULL),
994
      make_floatx80_init(0xbffd, 0x9db814fc5aa7095eULL) },
995
    { make_floatx80_init(0xbffe, 0x800000000000227dULL),
996
      make_floatx80_init(0x3ffe, 0xb504f333f9de539dULL),
997
      make_floatx80_init(0xbffd, 0x95f619980c4358c6ULL) },
998
    { make_floatx80_init(0xbffd, 0xefffffffffff3978ULL),
999
      make_floatx80_init(0x3ffe, 0xb8fbaf4762fbd0a1ULL),
1000
      make_floatx80_init(0xbffd, 0x8e08a1713a085ebeULL) },
1001
    { make_floatx80_init(0xbffd, 0xe00000000000df81ULL),
1002
      make_floatx80_init(0x3ffe, 0xbd08a39f580bfd8cULL),
1003
      make_floatx80_init(0xbffd, 0x85eeb8c14fe804e8ULL) },
1004
    { make_floatx80_init(0xbffd, 0xd00000000000bccfULL),
1005
      make_floatx80_init(0x3ffe, 0xc12c4cca667062f6ULL),
1006
      make_floatx80_init(0xbffc, 0xfb4eccd6663e7428ULL) },
1007
    { make_floatx80_init(0xbffd, 0xc00000000000eff0ULL),
1008
      make_floatx80_init(0x3ffe, 0xc5672a1155069abeULL),
1009
      make_floatx80_init(0xbffc, 0xea6357baabe59508ULL) },
1010
    { make_floatx80_init(0xbffd, 0xb000000000000fe6ULL),
1011
      make_floatx80_init(0x3ffe, 0xc9b9bd866e2f234bULL),
1012
      make_floatx80_init(0xbffc, 0xd91909e6474372d4ULL) },
1013
    { make_floatx80_init(0xbffd, 0x9fffffffffff2172ULL),
1014
      make_floatx80_init(0x3ffe, 0xce248c151f84bf00ULL),
1015
      make_floatx80_init(0xbffc, 0xc76dcfab81ed0400ULL) },
1016
    { make_floatx80_init(0xbffd, 0x8fffffffffffafffULL),
1017
      make_floatx80_init(0x3ffe, 0xd2a81d91f12afb2bULL),
1018
      make_floatx80_init(0xbffc, 0xb55f89b83b541354ULL) },
1019
    { make_floatx80_init(0xbffc, 0xffffffffffff81a3ULL),
1020
      make_floatx80_init(0x3ffe, 0xd744fccad69d7d5eULL),
1021
      make_floatx80_init(0xbffc, 0xa2ec0cd4a58a0a88ULL) },
1022
    { make_floatx80_init(0xbffc, 0xdfffffffffff1568ULL),
1023
      make_floatx80_init(0x3ffe, 0xdbfbb797daf25a44ULL),
1024
      make_floatx80_init(0xbffc, 0x901121a0943696f0ULL) },
1025
    { make_floatx80_init(0xbffc, 0xbfffffffffff68daULL),
1026
      make_floatx80_init(0x3ffe, 0xe0ccdeec2a94f811ULL),
1027
      make_floatx80_init(0xbffb, 0xf999089eab583f78ULL) },
1028
    { make_floatx80_init(0xbffc, 0x9fffffffffff4690ULL),
1029
      make_floatx80_init(0x3ffe, 0xe5b906e77c83657eULL),
1030
      make_floatx80_init(0xbffb, 0xd237c8c41be4d410ULL) },
1031
    { make_floatx80_init(0xbffb, 0xffffffffffff8aeeULL),
1032
      make_floatx80_init(0x3ffe, 0xeac0c6e7dd24427cULL),
1033
      make_floatx80_init(0xbffb, 0xa9f9c8c116ddec20ULL) },
1034
    { make_floatx80_init(0xbffb, 0xbfffffffffff2d18ULL),
1035
      make_floatx80_init(0x3ffe, 0xefe4b99bdcdb06ebULL),
1036
      make_floatx80_init(0xbffb, 0x80da33211927c8a8ULL) },
1037
    { make_floatx80_init(0xbffa, 0xffffffffffff8ccbULL),
1038
      make_floatx80_init(0x3ffe, 0xf5257d152486d0f4ULL),
1039
      make_floatx80_init(0xbffa, 0xada82eadb792f0c0ULL) },
1040
    { make_floatx80_init(0xbff9, 0xffffffffffff11feULL),
1041
      make_floatx80_init(0x3ffe, 0xfa83b2db722a0846ULL),
1042
      make_floatx80_init(0xbff9, 0xaf89a491babef740ULL) },
1043
    { floatx80_zero_init,
1044
      make_floatx80_init(0x3fff, 0x8000000000000000ULL),
1045
      floatx80_zero_init },
1046
    { make_floatx80_init(0x3ff9, 0xffffffffffff2680ULL),
1047
      make_floatx80_init(0x3fff, 0x82cd8698ac2b9f6fULL),
1048
      make_floatx80_init(0x3ff9, 0xb361a62b0ae7dbc0ULL) },
1049
    { make_floatx80_init(0x3ffb, 0x800000000000b500ULL),
1050
      make_floatx80_init(0x3fff, 0x85aac367cc488345ULL),
1051
      make_floatx80_init(0x3ffa, 0xb5586cf9891068a0ULL) },
1052
    { make_floatx80_init(0x3ffb, 0xbfffffffffff4b67ULL),
1053
      make_floatx80_init(0x3fff, 0x88980e8092da7cceULL),
1054
      make_floatx80_init(0x3ffb, 0x8980e8092da7cce0ULL) },
1055
    { make_floatx80_init(0x3ffb, 0xffffffffffffff57ULL),
1056
      make_floatx80_init(0x3fff, 0x8b95c1e3ea8bd6dfULL),
1057
      make_floatx80_init(0x3ffb, 0xb95c1e3ea8bd6df0ULL) },
1058
    { make_floatx80_init(0x3ffc, 0x9fffffffffff811fULL),
1059
      make_floatx80_init(0x3fff, 0x8ea4398b45cd4780ULL),
1060
      make_floatx80_init(0x3ffb, 0xea4398b45cd47800ULL) },
1061
    { make_floatx80_init(0x3ffc, 0xbfffffffffff9980ULL),
1062
      make_floatx80_init(0x3fff, 0x91c3d373ab11b919ULL),
1063
      make_floatx80_init(0x3ffc, 0x8e1e9b9d588dc8c8ULL) },
1064
    { make_floatx80_init(0x3ffc, 0xdffffffffffff631ULL),
1065
      make_floatx80_init(0x3fff, 0x94f4efa8fef70864ULL),
1066
      make_floatx80_init(0x3ffc, 0xa7a77d47f7b84320ULL) },
1067
    { make_floatx80_init(0x3ffc, 0xffffffffffff2499ULL),
1068
      make_floatx80_init(0x3fff, 0x9837f0518db892d4ULL),
1069
      make_floatx80_init(0x3ffc, 0xc1bf828c6dc496a0ULL) },
1070
    { make_floatx80_init(0x3ffd, 0x8fffffffffff80fbULL),
1071
      make_floatx80_init(0x3fff, 0x9b8d39b9d54e3a79ULL),
1072
      make_floatx80_init(0x3ffc, 0xdc69cdceaa71d3c8ULL) },
1073
    { make_floatx80_init(0x3ffd, 0x9fffffffffffbc23ULL),
1074
      make_floatx80_init(0x3fff, 0x9ef5326091a10313ULL),
1075
      make_floatx80_init(0x3ffc, 0xf7a993048d081898ULL) },
1076
    { make_floatx80_init(0x3ffd, 0xafffffffffff20ecULL),
1077
      make_floatx80_init(0x3fff, 0xa27043030c49370aULL),
1078
      make_floatx80_init(0x3ffd, 0x89c10c0c3124dc28ULL) },
1079
    { make_floatx80_init(0x3ffd, 0xc00000000000fd2cULL),
1080
      make_floatx80_init(0x3fff, 0xa5fed6a9b15171cfULL),
1081
      make_floatx80_init(0x3ffd, 0x97fb5aa6c545c73cULL) },
1082
    { make_floatx80_init(0x3ffd, 0xd0000000000093beULL),
1083
      make_floatx80_init(0x3fff, 0xa9a15ab4ea7c30e6ULL),
1084
      make_floatx80_init(0x3ffd, 0xa6856ad3a9f0c398ULL) },
1085
    { make_floatx80_init(0x3ffd, 0xe00000000000c2aeULL),
1086
      make_floatx80_init(0x3fff, 0xad583eea42a17876ULL),
1087
      make_floatx80_init(0x3ffd, 0xb560fba90a85e1d8ULL) },
1088
    { make_floatx80_init(0x3ffd, 0xefffffffffff1e3fULL),
1089
      make_floatx80_init(0x3fff, 0xb123f581d2abef6cULL),
1090
      make_floatx80_init(0x3ffd, 0xc48fd6074aafbdb0ULL) },
1091
    { make_floatx80_init(0x3ffd, 0xffffffffffff1c23ULL),
1092
      make_floatx80_init(0x3fff, 0xb504f333f9de2cadULL),
1093
      make_floatx80_init(0x3ffd, 0xd413cccfe778b2b4ULL) },
1094
    { make_floatx80_init(0x3ffe, 0x8800000000006344ULL),
1095
      make_floatx80_init(0x3fff, 0xb8fbaf4762fbd0a1ULL),
1096
      make_floatx80_init(0x3ffd, 0xe3eebd1d8bef4284ULL) },
1097
    { make_floatx80_init(0x3ffe, 0x9000000000005d67ULL),
1098
      make_floatx80_init(0x3fff, 0xbd08a39f580c668dULL),
1099
      make_floatx80_init(0x3ffd, 0xf4228e7d60319a34ULL) },
1100
    { make_floatx80_init(0x3ffe, 0x9800000000009127ULL),
1101
      make_floatx80_init(0x3fff, 0xc12c4cca6670e042ULL),
1102
      make_floatx80_init(0x3ffe, 0x82589994cce1c084ULL) },
1103
    { make_floatx80_init(0x3ffe, 0x9fffffffffff06f9ULL),
1104
      make_floatx80_init(0x3fff, 0xc5672a11550655c3ULL),
1105
      make_floatx80_init(0x3ffe, 0x8ace5422aa0cab86ULL) },
1106
    { make_floatx80_init(0x3ffe, 0xa7fffffffffff80dULL),
1107
      make_floatx80_init(0x3fff, 0xc9b9bd866e2f234bULL),
1108
      make_floatx80_init(0x3ffe, 0x93737b0cdc5e4696ULL) },
1109
    { make_floatx80_init(0x3ffe, 0xafffffffffff1470ULL),
1110
      make_floatx80_init(0x3fff, 0xce248c151f83fd69ULL),
1111
      make_floatx80_init(0x3ffe, 0x9c49182a3f07fad2ULL) },
1112
    { make_floatx80_init(0x3ffe, 0xb800000000000e0aULL),
1113
      make_floatx80_init(0x3fff, 0xd2a81d91f12aec5cULL),
1114
      make_floatx80_init(0x3ffe, 0xa5503b23e255d8b8ULL) },
1115
    { make_floatx80_init(0x3ffe, 0xc00000000000b7faULL),
1116
      make_floatx80_init(0x3fff, 0xd744fccad69dd630ULL),
1117
      make_floatx80_init(0x3ffe, 0xae89f995ad3bac60ULL) },
1118
    { make_floatx80_init(0x3ffe, 0xc800000000003aa6ULL),
1119
      make_floatx80_init(0x3fff, 0xdbfbb797daf25a44ULL),
1120
      make_floatx80_init(0x3ffe, 0xb7f76f2fb5e4b488ULL) },
1121
    { make_floatx80_init(0x3ffe, 0xd00000000000a6aeULL),
1122
      make_floatx80_init(0x3fff, 0xe0ccdeec2a954685ULL),
1123
      make_floatx80_init(0x3ffe, 0xc199bdd8552a8d0aULL) },
1124
    { make_floatx80_init(0x3ffe, 0xd800000000004165ULL),
1125
      make_floatx80_init(0x3fff, 0xe5b906e77c837155ULL),
1126
      make_floatx80_init(0x3ffe, 0xcb720dcef906e2aaULL) },
1127
    { make_floatx80_init(0x3ffe, 0xe00000000000582cULL),
1128
      make_floatx80_init(0x3fff, 0xeac0c6e7dd24713aULL),
1129
      make_floatx80_init(0x3ffe, 0xd5818dcfba48e274ULL) },
1130
    { make_floatx80_init(0x3ffe, 0xe800000000001a5dULL),
1131
      make_floatx80_init(0x3fff, 0xefe4b99bdcdb06ebULL),
1132
      make_floatx80_init(0x3ffe, 0xdfc97337b9b60dd6ULL) },
1133
    { make_floatx80_init(0x3ffe, 0xefffffffffffc1efULL),
1134
      make_floatx80_init(0x3fff, 0xf5257d152486a2faULL),
1135
      make_floatx80_init(0x3ffe, 0xea4afa2a490d45f4ULL) },
1136
    { make_floatx80_init(0x3ffe, 0xf800000000001069ULL),
1137
      make_floatx80_init(0x3fff, 0xfa83b2db722a0e5cULL),
1138
      make_floatx80_init(0x3ffe, 0xf50765b6e4541cb8ULL) },
1139
    { make_floatx80_init(0x3fff, 0x8000000000000000ULL),
1140
      make_floatx80_init(0x4000, 0x8000000000000000ULL),
1141
      make_floatx80_init(0x3fff, 0x8000000000000000ULL) },
1142
};
1143
1144
void HELPER(f2xm1)(CPUX86State *env)
1145
0
{
1146
0
    int old_flags = save_exception_flags(env);
1147
0
    uint64_t sig = extractFloatx80Frac(ST0);
1148
0
    int32_t exp = extractFloatx80Exp(ST0);
1149
0
    bool sign = extractFloatx80Sign(ST0);
1150
1151
0
    if (floatx80_invalid_encoding(ST0, &env->fp_status)) {
1152
0
        float_raise(float_flag_invalid, &env->fp_status);
1153
0
        ST0 = floatx80_default_nan(&env->fp_status);
1154
0
    } else if (floatx80_is_any_nan(ST0)) {
1155
0
        if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
1156
0
            float_raise(float_flag_invalid, &env->fp_status);
1157
0
            ST0 = floatx80_silence_nan(ST0, &env->fp_status);
1158
0
        }
1159
0
    } else if (exp > 0x3fff ||
1160
0
               (exp == 0x3fff && sig != (0x8000000000000000ULL))) {
1161
        /* Out of range for the instruction, treat as invalid.  */
1162
0
        float_raise(float_flag_invalid, &env->fp_status);
1163
0
        ST0 = floatx80_default_nan(&env->fp_status);
1164
0
    } else if (exp == 0x3fff) {
1165
        /* Argument 1 or -1, exact result 1 or -0.5.  */
1166
0
        if (sign) {
1167
0
            ST0 = make_floatx80(0xbffe, 0x8000000000000000ULL);
1168
0
        }
1169
0
    } else if (exp < 0x3fb0) {
1170
0
        if (!floatx80_is_zero(ST0)) {
1171
            /*
1172
             * Multiplying the argument by an extra-precision version
1173
             * of log(2) is sufficiently precise.  Zero arguments are
1174
             * returned unchanged.
1175
             */
1176
0
            uint64_t sig0, sig1, sig2;
1177
0
            if (exp == 0) {
1178
0
                normalizeFloatx80Subnormal(sig, &exp, &sig);
1179
0
            }
1180
0
            mul128By64To192(ln2_sig_high, ln2_sig_low, sig, &sig0, &sig1,
1181
0
                            &sig2);
1182
            /* This result is inexact.  */
1183
0
            sig1 |= 1;
1184
0
            ST0 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
1185
0
                                                sign, exp, sig0, sig1,
1186
0
                                                &env->fp_status);
1187
0
        }
1188
0
    } else {
1189
0
        floatx80 tmp, y, accum;
1190
0
        bool asign, bsign;
1191
0
        int32_t n, aexp, bexp;
1192
0
        uint64_t asig0, asig1, asig2, bsig0, bsig1;
1193
0
        FloatRoundMode save_mode = get_float_rounding_mode(&env->fp_status);
1194
0
        FloatX80RoundPrec save_prec =
1195
0
            get_floatx80_rounding_precision(&env->fp_status);
1196
0
        set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
1197
0
        set_floatx80_rounding_precision(floatx80_precision_x, &env->fp_status);
1198
1199
        /* Find the nearest multiple of 1/32 to the argument.  */
1200
0
        tmp = floatx80_scalbn(ST0, 5, &env->fp_status);
1201
0
        n = 32 + floatx80_to_int32(tmp, &env->fp_status);
1202
0
        y = floatx80_sub(ST0, f2xm1_table[n].t, &env->fp_status);
1203
1204
0
        if (floatx80_is_zero(y)) {
1205
            /*
1206
             * Use the value of 2^t - 1 from the table, to avoid
1207
             * needing to special-case zero as a result of
1208
             * multiplication below.
1209
             */
1210
0
            ST0 = f2xm1_table[n].t;
1211
0
            set_float_exception_flags(float_flag_inexact, &env->fp_status);
1212
0
            set_float_rounding_mode(save_mode, &env->fp_status);
1213
0
        } else {
1214
            /*
1215
             * Compute the lower parts of a polynomial expansion for
1216
             * (2^y - 1) / y.
1217
             */
1218
0
            accum = floatx80_mul(f2xm1_coeff_7, y, &env->fp_status);
1219
0
            accum = floatx80_add(f2xm1_coeff_6, accum, &env->fp_status);
1220
0
            accum = floatx80_mul(accum, y, &env->fp_status);
1221
0
            accum = floatx80_add(f2xm1_coeff_5, accum, &env->fp_status);
1222
0
            accum = floatx80_mul(accum, y, &env->fp_status);
1223
0
            accum = floatx80_add(f2xm1_coeff_4, accum, &env->fp_status);
1224
0
            accum = floatx80_mul(accum, y, &env->fp_status);
1225
0
            accum = floatx80_add(f2xm1_coeff_3, accum, &env->fp_status);
1226
0
            accum = floatx80_mul(accum, y, &env->fp_status);
1227
0
            accum = floatx80_add(f2xm1_coeff_2, accum, &env->fp_status);
1228
0
            accum = floatx80_mul(accum, y, &env->fp_status);
1229
0
            accum = floatx80_add(f2xm1_coeff_1, accum, &env->fp_status);
1230
0
            accum = floatx80_mul(accum, y, &env->fp_status);
1231
0
            accum = floatx80_add(f2xm1_coeff_0_low, accum, &env->fp_status);
1232
1233
            /*
1234
             * The full polynomial expansion is f2xm1_coeff_0 + accum
1235
             * (where accum has much lower magnitude, and so, in
1236
             * particular, carry out of the addition is not possible).
1237
             * (This expansion is only accurate to about 70 bits, not
1238
             * 128 bits.)
1239
             */
1240
0
            aexp = extractFloatx80Exp(f2xm1_coeff_0);
1241
0
            asign = extractFloatx80Sign(f2xm1_coeff_0);
1242
0
            shift128RightJamming(extractFloatx80Frac(accum), 0,
1243
0
                                 aexp - extractFloatx80Exp(accum),
1244
0
                                 &asig0, &asig1);
1245
0
            bsig0 = extractFloatx80Frac(f2xm1_coeff_0);
1246
0
            bsig1 = 0;
1247
0
            if (asign == extractFloatx80Sign(accum)) {
1248
0
                add128(bsig0, bsig1, asig0, asig1, &asig0, &asig1);
1249
0
            } else {
1250
0
                sub128(bsig0, bsig1, asig0, asig1, &asig0, &asig1);
1251
0
            }
1252
            /* And thus compute an approximation to 2^y - 1.  */
1253
0
            mul128By64To192(asig0, asig1, extractFloatx80Frac(y),
1254
0
                            &asig0, &asig1, &asig2);
1255
0
            aexp += extractFloatx80Exp(y) - 0x3ffe;
1256
0
            asign ^= extractFloatx80Sign(y);
1257
0
            if (n != 32) {
1258
                /*
1259
                 * Multiply this by the precomputed value of 2^t and
1260
                 * add that of 2^t - 1.
1261
                 */
1262
0
                mul128By64To192(asig0, asig1,
1263
0
                                extractFloatx80Frac(f2xm1_table[n].exp2),
1264
0
                                &asig0, &asig1, &asig2);
1265
0
                aexp += extractFloatx80Exp(f2xm1_table[n].exp2) - 0x3ffe;
1266
0
                bexp = extractFloatx80Exp(f2xm1_table[n].exp2m1);
1267
0
                bsig0 = extractFloatx80Frac(f2xm1_table[n].exp2m1);
1268
0
                bsig1 = 0;
1269
0
                if (bexp < aexp) {
1270
0
                    shift128RightJamming(bsig0, bsig1, aexp - bexp,
1271
0
                                         &bsig0, &bsig1);
1272
0
                } else if (aexp < bexp) {
1273
0
                    shift128RightJamming(asig0, asig1, bexp - aexp,
1274
0
                                         &asig0, &asig1);
1275
0
                    aexp = bexp;
1276
0
                }
1277
                /* The sign of 2^t - 1 is always that of the result.  */
1278
0
                bsign = extractFloatx80Sign(f2xm1_table[n].exp2m1);
1279
0
                if (asign == bsign) {
1280
                    /* Avoid possible carry out of the addition.  */
1281
0
                    shift128RightJamming(asig0, asig1, 1,
1282
0
                                         &asig0, &asig1);
1283
0
                    shift128RightJamming(bsig0, bsig1, 1,
1284
0
                                         &bsig0, &bsig1);
1285
0
                    ++aexp;
1286
0
                    add128(asig0, asig1, bsig0, bsig1, &asig0, &asig1);
1287
0
                } else {
1288
0
                    sub128(bsig0, bsig1, asig0, asig1, &asig0, &asig1);
1289
0
                    asign = bsign;
1290
0
                }
1291
0
            }
1292
0
            set_float_rounding_mode(save_mode, &env->fp_status);
1293
            /* This result is inexact.  */
1294
0
            asig1 |= 1;
1295
0
            ST0 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
1296
0
                                                asign, aexp, asig0, asig1,
1297
0
                                                &env->fp_status);
1298
0
        }
1299
1300
0
        set_floatx80_rounding_precision(save_prec, &env->fp_status);
1301
0
    }
1302
0
    merge_exception_flags(env, old_flags);
1303
0
}
1304
1305
void HELPER(fptan)(CPUX86State *env)
1306
0
{
1307
0
    double fptemp = floatx80_to_double(env, ST0);
1308
1309
0
    if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
1310
0
        env->fpus |= 0x400;
1311
0
    } else {
1312
0
        fptemp = tan(fptemp);
1313
0
        ST0 = double_to_floatx80(env, fptemp);
1314
0
        fpush(env);
1315
0
        ST0 = floatx80_one;
1316
0
        env->fpus &= ~0x400; /* C2 <-- 0 */
1317
        /* the above code is for |arg| < 2**52 only */
1318
0
    }
1319
0
}
1320
1321
/* Values of pi/4, pi/2, 3pi/4 and pi, with 128-bit precision.  */
1322
0
#define pi_4_exp 0x3ffe
1323
0
#define pi_4_sig_high 0xc90fdaa22168c234ULL
1324
0
#define pi_4_sig_low 0xc4c6628b80dc1cd1ULL
1325
0
#define pi_2_exp 0x3fff
1326
0
#define pi_2_sig_high 0xc90fdaa22168c234ULL
1327
0
#define pi_2_sig_low 0xc4c6628b80dc1cd1ULL
1328
0
#define pi_34_exp 0x4000
1329
0
#define pi_34_sig_high 0x96cbe3f9990e91a7ULL
1330
0
#define pi_34_sig_low 0x9394c9e8a0a5159dULL
1331
0
#define pi_exp 0x4000
1332
0
#define pi_sig_high 0xc90fdaa22168c234ULL
1333
0
#define pi_sig_low 0xc4c6628b80dc1cd1ULL
1334
1335
/*
1336
 * Polynomial coefficients for an approximation to atan(x), with only
1337
 * odd powers of x used, for x in the interval [-1/16, 1/16].  (Unlike
1338
 * for some other approximations, no low part is needed for the first
1339
 * coefficient here to achieve a sufficiently accurate result, because
1340
 * the coefficient in this minimax approximation is very close to
1341
 * exactly 1.)
1342
 */
1343
0
#define fpatan_coeff_0 make_floatx80(0x3fff, 0x8000000000000000ULL)
1344
0
#define fpatan_coeff_1 make_floatx80(0xbffd, 0xaaaaaaaaaaaaaa43ULL)
1345
0
#define fpatan_coeff_2 make_floatx80(0x3ffc, 0xccccccccccbfe4f8ULL)
1346
0
#define fpatan_coeff_3 make_floatx80(0xbffc, 0x92492491fbab2e66ULL)
1347
0
#define fpatan_coeff_4 make_floatx80(0x3ffb, 0xe38e372881ea1e0bULL)
1348
0
#define fpatan_coeff_5 make_floatx80(0xbffb, 0xba2c0104bbdd0615ULL)
1349
0
#define fpatan_coeff_6 make_floatx80(0x3ffb, 0x9baf7ebf898b42efULL)
1350
1351
struct fpatan_data {
1352
    /* High and low parts of atan(x).  */
1353
    floatx80 atan_high, atan_low;
1354
};
1355
1356
static const struct fpatan_data fpatan_table[9] = {
1357
    { floatx80_zero_init,
1358
      floatx80_zero_init },
1359
    { make_floatx80_init(0x3ffb, 0xfeadd4d5617b6e33ULL),
1360
      make_floatx80_init(0xbfb9, 0xdda19d8305ddc420ULL) },
1361
    { make_floatx80_init(0x3ffc, 0xfadbafc96406eb15ULL),
1362
      make_floatx80_init(0x3fbb, 0xdb8f3debef442fccULL) },
1363
    { make_floatx80_init(0x3ffd, 0xb7b0ca0f26f78474ULL),
1364
      make_floatx80_init(0xbfbc, 0xeab9bdba460376faULL) },
1365
    { make_floatx80_init(0x3ffd, 0xed63382b0dda7b45ULL),
1366
      make_floatx80_init(0x3fbc, 0xdfc88bd978751a06ULL) },
1367
    { make_floatx80_init(0x3ffe, 0x8f005d5ef7f59f9bULL),
1368
      make_floatx80_init(0x3fbd, 0xb906bc2ccb886e90ULL) },
1369
    { make_floatx80_init(0x3ffe, 0xa4bc7d1934f70924ULL),
1370
      make_floatx80_init(0x3fbb, 0xcd43f9522bed64f8ULL) },
1371
    { make_floatx80_init(0x3ffe, 0xb8053e2bc2319e74ULL),
1372
      make_floatx80_init(0xbfbc, 0xd3496ab7bd6eef0cULL) },
1373
    { make_floatx80_init(0x3ffe, 0xc90fdaa22168c235ULL),
1374
      make_floatx80_init(0xbfbc, 0xece675d1fc8f8cbcULL) },
1375
};
1376
1377
void HELPER(fpatan)(CPUX86State *env)
1378
0
{
1379
0
    int old_flags = save_exception_flags(env);
1380
0
    uint64_t arg0_sig = extractFloatx80Frac(ST0);
1381
0
    int32_t arg0_exp = extractFloatx80Exp(ST0);
1382
0
    bool arg0_sign = extractFloatx80Sign(ST0);
1383
0
    uint64_t arg1_sig = extractFloatx80Frac(ST1);
1384
0
    int32_t arg1_exp = extractFloatx80Exp(ST1);
1385
0
    bool arg1_sign = extractFloatx80Sign(ST1);
1386
1387
0
    if (floatx80_invalid_encoding(ST0, &env->fp_status) ||
1388
0
        floatx80_invalid_encoding(ST1, &env->fp_status)) {
1389
0
        float_raise(float_flag_invalid, &env->fp_status);
1390
0
        ST1 = floatx80_default_nan(&env->fp_status);
1391
0
    } else if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
1392
0
        float_raise(float_flag_invalid, &env->fp_status);
1393
0
        ST1 = floatx80_silence_nan(ST0, &env->fp_status);
1394
0
    } else if (floatx80_is_signaling_nan(ST1, &env->fp_status)) {
1395
0
        float_raise(float_flag_invalid, &env->fp_status);
1396
0
        ST1 = floatx80_silence_nan(ST1, &env->fp_status);
1397
0
    } else if (floatx80_is_any_nan(ST0)) {
1398
0
        ST1 = ST0;
1399
0
    } else if (floatx80_is_any_nan(ST1)) {
1400
        /* Pass this NaN through.  */
1401
0
    } else if (floatx80_is_zero(ST1) && !arg0_sign) {
1402
        /* Pass this zero through.  */
1403
0
    } else if (((floatx80_is_infinity(ST0, &env->fp_status) &&
1404
0
                 !floatx80_is_infinity(ST1, &env->fp_status)) ||
1405
0
                 arg0_exp - arg1_exp >= 80) &&
1406
0
               !arg0_sign) {
1407
        /*
1408
         * Dividing ST1 by ST0 gives the correct result up to
1409
         * rounding, and avoids spurious underflow exceptions that
1410
         * might result from passing some small values through the
1411
         * polynomial approximation, but if a finite nonzero result of
1412
         * division is exact, the result of fpatan is still inexact
1413
         * (and underflowing where appropriate).
1414
         */
1415
0
        FloatX80RoundPrec save_prec =
1416
0
            get_floatx80_rounding_precision(&env->fp_status);
1417
0
        set_floatx80_rounding_precision(floatx80_precision_x, &env->fp_status);
1418
0
        ST1 = floatx80_div(ST1, ST0, &env->fp_status);
1419
0
        set_floatx80_rounding_precision(save_prec, &env->fp_status);
1420
0
        if (!floatx80_is_zero(ST1) &&
1421
0
            !(get_float_exception_flags(&env->fp_status) &
1422
0
              float_flag_inexact)) {
1423
            /*
1424
             * The mathematical result is very slightly closer to zero
1425
             * than this exact result.  Round a value with the
1426
             * significand adjusted accordingly to get the correct
1427
             * exceptions, and possibly an adjusted result depending
1428
             * on the rounding mode.
1429
             */
1430
0
            uint64_t sig = extractFloatx80Frac(ST1);
1431
0
            int32_t exp = extractFloatx80Exp(ST1);
1432
0
            bool sign = extractFloatx80Sign(ST1);
1433
0
            if (exp == 0) {
1434
0
                normalizeFloatx80Subnormal(sig, &exp, &sig);
1435
0
            }
1436
0
            ST1 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
1437
0
                                                sign, exp, sig - 1,
1438
0
                                                -1, &env->fp_status);
1439
0
        }
1440
0
    } else {
1441
        /* The result is inexact.  */
1442
0
        bool rsign = arg1_sign;
1443
0
        int32_t rexp;
1444
0
        uint64_t rsig0, rsig1;
1445
0
        if (floatx80_is_zero(ST1)) {
1446
            /*
1447
             * ST0 is negative.  The result is pi with the sign of
1448
             * ST1.
1449
             */
1450
0
            rexp = pi_exp;
1451
0
            rsig0 = pi_sig_high;
1452
0
            rsig1 = pi_sig_low;
1453
0
        } else if (floatx80_is_infinity(ST1, &env->fp_status)) {
1454
0
            if (floatx80_is_infinity(ST0, &env->fp_status)) {
1455
0
                if (arg0_sign) {
1456
0
                    rexp = pi_34_exp;
1457
0
                    rsig0 = pi_34_sig_high;
1458
0
                    rsig1 = pi_34_sig_low;
1459
0
                } else {
1460
0
                    rexp = pi_4_exp;
1461
0
                    rsig0 = pi_4_sig_high;
1462
0
                    rsig1 = pi_4_sig_low;
1463
0
                }
1464
0
            } else {
1465
0
                rexp = pi_2_exp;
1466
0
                rsig0 = pi_2_sig_high;
1467
0
                rsig1 = pi_2_sig_low;
1468
0
            }
1469
0
        } else if (floatx80_is_zero(ST0) || arg1_exp - arg0_exp >= 80) {
1470
0
            rexp = pi_2_exp;
1471
0
            rsig0 = pi_2_sig_high;
1472
0
            rsig1 = pi_2_sig_low;
1473
0
        } else if (floatx80_is_infinity(ST0, &env->fp_status) ||
1474
0
                   arg0_exp - arg1_exp >= 80) {
1475
            /* ST0 is negative.  */
1476
0
            rexp = pi_exp;
1477
0
            rsig0 = pi_sig_high;
1478
0
            rsig1 = pi_sig_low;
1479
0
        } else {
1480
            /*
1481
             * ST0 and ST1 are finite, nonzero and with exponents not
1482
             * too far apart.
1483
             */
1484
0
            int32_t adj_exp, num_exp, den_exp, xexp, yexp, n, texp, zexp, aexp;
1485
0
            int32_t azexp, axexp;
1486
0
            bool adj_sub, ysign, zsign;
1487
0
            uint64_t adj_sig0, adj_sig1, num_sig, den_sig, xsig0, xsig1;
1488
0
            uint64_t msig0, msig1, msig2, remsig0, remsig1, remsig2;
1489
0
            uint64_t ysig0, ysig1, tsig, zsig0, zsig1, asig0, asig1;
1490
0
            uint64_t azsig0, azsig1;
1491
0
            uint64_t azsig2, azsig3, axsig0, axsig1;
1492
0
            floatx80 x8;
1493
0
            FloatRoundMode save_mode = get_float_rounding_mode(&env->fp_status);
1494
0
            FloatX80RoundPrec save_prec =
1495
0
                get_floatx80_rounding_precision(&env->fp_status);
1496
0
            set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
1497
0
            set_floatx80_rounding_precision(floatx80_precision_x,
1498
0
                                            &env->fp_status);
1499
1500
0
            if (arg0_exp == 0) {
1501
0
                normalizeFloatx80Subnormal(arg0_sig, &arg0_exp, &arg0_sig);
1502
0
            }
1503
0
            if (arg1_exp == 0) {
1504
0
                normalizeFloatx80Subnormal(arg1_sig, &arg1_exp, &arg1_sig);
1505
0
            }
1506
0
            if (arg0_exp > arg1_exp ||
1507
0
                (arg0_exp == arg1_exp && arg0_sig >= arg1_sig)) {
1508
                /* Work with abs(ST1) / abs(ST0).  */
1509
0
                num_exp = arg1_exp;
1510
0
                num_sig = arg1_sig;
1511
0
                den_exp = arg0_exp;
1512
0
                den_sig = arg0_sig;
1513
0
                if (arg0_sign) {
1514
                    /* The result is subtracted from pi.  */
1515
0
                    adj_exp = pi_exp;
1516
0
                    adj_sig0 = pi_sig_high;
1517
0
                    adj_sig1 = pi_sig_low;
1518
0
                    adj_sub = true;
1519
0
                } else {
1520
                    /* The result is used as-is.  */
1521
0
                    adj_exp = 0;
1522
0
                    adj_sig0 = 0;
1523
0
                    adj_sig1 = 0;
1524
0
                    adj_sub = false;
1525
0
                }
1526
0
            } else {
1527
                /* Work with abs(ST0) / abs(ST1).  */
1528
0
                num_exp = arg0_exp;
1529
0
                num_sig = arg0_sig;
1530
0
                den_exp = arg1_exp;
1531
0
                den_sig = arg1_sig;
1532
                /* The result is added to or subtracted from pi/2.  */
1533
0
                adj_exp = pi_2_exp;
1534
0
                adj_sig0 = pi_2_sig_high;
1535
0
                adj_sig1 = pi_2_sig_low;
1536
0
                adj_sub = !arg0_sign;
1537
0
            }
1538
1539
            /*
1540
             * Compute x = num/den, where 0 < x <= 1 and x is not too
1541
             * small.
1542
             */
1543
0
            xexp = num_exp - den_exp + 0x3ffe;
1544
0
            remsig0 = num_sig;
1545
0
            remsig1 = 0;
1546
0
            if (den_sig <= remsig0) {
1547
0
                shift128Right(remsig0, remsig1, 1, &remsig0, &remsig1);
1548
0
                ++xexp;
1549
0
            }
1550
0
            xsig0 = estimateDiv128To64(remsig0, remsig1, den_sig);
1551
0
            mul64To128(den_sig, xsig0, &msig0, &msig1);
1552
0
            sub128(remsig0, remsig1, msig0, msig1, &remsig0, &remsig1);
1553
0
            while ((int64_t) remsig0 < 0) {
1554
0
                --xsig0;
1555
0
                add128(remsig0, remsig1, 0, den_sig, &remsig0, &remsig1);
1556
0
            }
1557
0
            xsig1 = estimateDiv128To64(remsig1, 0, den_sig);
1558
            /*
1559
             * No need to correct any estimation error in xsig1; even
1560
             * with such error, it is accurate enough.
1561
             */
1562
1563
            /*
1564
             * Split x as x = t + y, where t = n/8 is the nearest
1565
             * multiple of 1/8 to x.
1566
             */
1567
0
            x8 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
1568
0
                                               false, xexp + 3, xsig0,
1569
0
                                               xsig1, &env->fp_status);
1570
0
            n = floatx80_to_int32(x8, &env->fp_status);
1571
0
            if (n == 0) {
1572
0
                ysign = false;
1573
0
                yexp = xexp;
1574
0
                ysig0 = xsig0;
1575
0
                ysig1 = xsig1;
1576
0
                texp = 0;
1577
0
                tsig = 0;
1578
0
            } else {
1579
0
                int shift = clz32(n) + 32;
1580
0
                texp = 0x403b - shift;
1581
0
                tsig = n;
1582
0
                tsig <<= shift;
1583
0
                if (texp == xexp) {
1584
0
                    sub128(xsig0, xsig1, tsig, 0, &ysig0, &ysig1);
1585
0
                    if ((int64_t) ysig0 >= 0) {
1586
0
                        ysign = false;
1587
0
                        if (ysig0 == 0) {
1588
0
                            if (ysig1 == 0) {
1589
0
                                yexp = 0;
1590
0
                            } else {
1591
0
                                shift = clz64(ysig1) + 64;
1592
0
                                yexp = xexp - shift;
1593
0
                                shift128Left(ysig0, ysig1, shift,
1594
0
                                             &ysig0, &ysig1);
1595
0
                            }
1596
0
                        } else {
1597
0
                            shift = clz64(ysig0);
1598
0
                            yexp = xexp - shift;
1599
0
                            shift128Left(ysig0, ysig1, shift, &ysig0, &ysig1);
1600
0
                        }
1601
0
                    } else {
1602
0
                        ysign = true;
1603
0
                        sub128(0, 0, ysig0, ysig1, &ysig0, &ysig1);
1604
0
                        if (ysig0 == 0) {
1605
0
                            shift = clz64(ysig1) + 64;
1606
0
                        } else {
1607
0
                            shift = clz64(ysig0);
1608
0
                        }
1609
0
                        yexp = xexp - shift;
1610
0
                        shift128Left(ysig0, ysig1, shift, &ysig0, &ysig1);
1611
0
                    }
1612
0
                } else {
1613
                    /*
1614
                     * t's exponent must be greater than x's because t
1615
                     * is positive and the nearest multiple of 1/8 to
1616
                     * x, and if x has a greater exponent, the power
1617
                     * of 2 with that exponent is also a multiple of
1618
                     * 1/8.
1619
                     */
1620
0
                    uint64_t usig0, usig1;
1621
0
                    shift128RightJamming(xsig0, xsig1, texp - xexp,
1622
0
                                         &usig0, &usig1);
1623
0
                    ysign = true;
1624
0
                    sub128(tsig, 0, usig0, usig1, &ysig0, &ysig1);
1625
0
                    if (ysig0 == 0) {
1626
0
                        shift = clz64(ysig1) + 64;
1627
0
                    } else {
1628
0
                        shift = clz64(ysig0);
1629
0
                    }
1630
0
                    yexp = texp - shift;
1631
0
                    shift128Left(ysig0, ysig1, shift, &ysig0, &ysig1);
1632
0
                }
1633
0
            }
1634
1635
            /*
1636
             * Compute z = y/(1+tx), so arctan(x) = arctan(t) +
1637
             * arctan(z).
1638
             */
1639
0
            zsign = ysign;
1640
0
            if (texp == 0 || yexp == 0) {
1641
0
                zexp = yexp;
1642
0
                zsig0 = ysig0;
1643
0
                zsig1 = ysig1;
1644
0
            } else {
1645
                /*
1646
                 * t <= 1, x <= 1 and if both are 1 then y is 0, so tx < 1.
1647
                 */
1648
0
                int32_t dexp = texp + xexp - 0x3ffe;
1649
0
                uint64_t dsig0, dsig1, dsig2;
1650
0
                mul128By64To192(xsig0, xsig1, tsig, &dsig0, &dsig1, &dsig2);
1651
                /*
1652
                 * dexp <= 0x3fff (and if equal, dsig0 has a leading 0
1653
                 * bit).  Add 1 to produce the denominator 1+tx.
1654
                 */
1655
0
                shift128RightJamming(dsig0, dsig1, 0x3fff - dexp,
1656
0
                                     &dsig0, &dsig1);
1657
0
                dsig0 |= 0x8000000000000000ULL;
1658
0
                zexp = yexp - 1;
1659
0
                remsig0 = ysig0;
1660
0
                remsig1 = ysig1;
1661
0
                remsig2 = 0;
1662
0
                if (dsig0 <= remsig0) {
1663
0
                    shift128Right(remsig0, remsig1, 1, &remsig0, &remsig1);
1664
0
                    ++zexp;
1665
0
                }
1666
0
                zsig0 = estimateDiv128To64(remsig0, remsig1, dsig0);
1667
0
                mul128By64To192(dsig0, dsig1, zsig0, &msig0, &msig1, &msig2);
1668
0
                sub192(remsig0, remsig1, remsig2, msig0, msig1, msig2,
1669
0
                       &remsig0, &remsig1, &remsig2);
1670
0
                while ((int64_t) remsig0 < 0) {
1671
0
                    --zsig0;
1672
0
                    add192(remsig0, remsig1, remsig2, 0, dsig0, dsig1,
1673
0
                           &remsig0, &remsig1, &remsig2);
1674
0
                }
1675
0
                zsig1 = estimateDiv128To64(remsig1, remsig2, dsig0);
1676
                /* No need to correct any estimation error in zsig1.  */
1677
0
            }
1678
1679
0
            if (zexp == 0) {
1680
0
                azexp = 0;
1681
0
                azsig0 = 0;
1682
0
                azsig1 = 0;
1683
0
            } else {
1684
0
                floatx80 z2, accum;
1685
0
                uint64_t z2sig0, z2sig1, z2sig2, z2sig3;
1686
                /* Compute z^2.  */
1687
0
                mul128To256(zsig0, zsig1, zsig0, zsig1,
1688
0
                            &z2sig0, &z2sig1, &z2sig2, &z2sig3);
1689
0
                z2 = normalizeRoundAndPackFloatx80(floatx80_precision_x, false,
1690
0
                                                   zexp + zexp - 0x3ffe,
1691
0
                                                   z2sig0, z2sig1,
1692
0
                                                   &env->fp_status);
1693
1694
                /* Compute the lower parts of the polynomial expansion.  */
1695
0
                accum = floatx80_mul(fpatan_coeff_6, z2, &env->fp_status);
1696
0
                accum = floatx80_add(fpatan_coeff_5, accum, &env->fp_status);
1697
0
                accum = floatx80_mul(accum, z2, &env->fp_status);
1698
0
                accum = floatx80_add(fpatan_coeff_4, accum, &env->fp_status);
1699
0
                accum = floatx80_mul(accum, z2, &env->fp_status);
1700
0
                accum = floatx80_add(fpatan_coeff_3, accum, &env->fp_status);
1701
0
                accum = floatx80_mul(accum, z2, &env->fp_status);
1702
0
                accum = floatx80_add(fpatan_coeff_2, accum, &env->fp_status);
1703
0
                accum = floatx80_mul(accum, z2, &env->fp_status);
1704
0
                accum = floatx80_add(fpatan_coeff_1, accum, &env->fp_status);
1705
0
                accum = floatx80_mul(accum, z2, &env->fp_status);
1706
1707
                /*
1708
                 * The full polynomial expansion is z*(fpatan_coeff_0 + accum).
1709
                 * fpatan_coeff_0 is 1, and accum is negative and much smaller.
1710
                 */
1711
0
                aexp = extractFloatx80Exp(fpatan_coeff_0);
1712
0
                shift128RightJamming(extractFloatx80Frac(accum), 0,
1713
0
                                     aexp - extractFloatx80Exp(accum),
1714
0
                                     &asig0, &asig1);
1715
0
                sub128(extractFloatx80Frac(fpatan_coeff_0), 0, asig0, asig1,
1716
0
                       &asig0, &asig1);
1717
                /* Multiply by z to compute arctan(z).  */
1718
0
                azexp = aexp + zexp - 0x3ffe;
1719
0
                mul128To256(asig0, asig1, zsig0, zsig1, &azsig0, &azsig1,
1720
0
                            &azsig2, &azsig3);
1721
0
            }
1722
1723
            /* Add arctan(t) (positive or zero) and arctan(z) (sign zsign).  */
1724
0
            if (texp == 0) {
1725
                /* z is positive.  */
1726
0
                axexp = azexp;
1727
0
                axsig0 = azsig0;
1728
0
                axsig1 = azsig1;
1729
0
            } else {
1730
0
                bool low_sign = extractFloatx80Sign(fpatan_table[n].atan_low);
1731
0
                int32_t low_exp = extractFloatx80Exp(fpatan_table[n].atan_low);
1732
0
                uint64_t low_sig0 =
1733
0
                    extractFloatx80Frac(fpatan_table[n].atan_low);
1734
0
                uint64_t low_sig1 = 0;
1735
0
                axexp = extractFloatx80Exp(fpatan_table[n].atan_high);
1736
0
                axsig0 = extractFloatx80Frac(fpatan_table[n].atan_high);
1737
0
                axsig1 = 0;
1738
0
                shift128RightJamming(low_sig0, low_sig1, axexp - low_exp,
1739
0
                                     &low_sig0, &low_sig1);
1740
0
                if (low_sign) {
1741
0
                    sub128(axsig0, axsig1, low_sig0, low_sig1,
1742
0
                           &axsig0, &axsig1);
1743
0
                } else {
1744
0
                    add128(axsig0, axsig1, low_sig0, low_sig1,
1745
0
                           &axsig0, &axsig1);
1746
0
                }
1747
0
                if (azexp >= axexp) {
1748
0
                    shift128RightJamming(axsig0, axsig1, azexp - axexp + 1,
1749
0
                                         &axsig0, &axsig1);
1750
0
                    axexp = azexp + 1;
1751
0
                    shift128RightJamming(azsig0, azsig1, 1,
1752
0
                                         &azsig0, &azsig1);
1753
0
                } else {
1754
0
                    shift128RightJamming(axsig0, axsig1, 1,
1755
0
                                         &axsig0, &axsig1);
1756
0
                    shift128RightJamming(azsig0, azsig1, axexp - azexp + 1,
1757
0
                                         &azsig0, &azsig1);
1758
0
                    ++axexp;
1759
0
                }
1760
0
                if (zsign) {
1761
0
                    sub128(axsig0, axsig1, azsig0, azsig1,
1762
0
                           &axsig0, &axsig1);
1763
0
                } else {
1764
0
                    add128(axsig0, axsig1, azsig0, azsig1,
1765
0
                           &axsig0, &axsig1);
1766
0
                }
1767
0
            }
1768
1769
0
            if (adj_exp == 0) {
1770
0
                rexp = axexp;
1771
0
                rsig0 = axsig0;
1772
0
                rsig1 = axsig1;
1773
0
            } else {
1774
                /*
1775
                 * Add or subtract arctan(x) (exponent axexp,
1776
                 * significand axsig0 and axsig1, positive, not
1777
                 * necessarily normalized) to the number given by
1778
                 * adj_exp, adj_sig0 and adj_sig1, according to
1779
                 * adj_sub.
1780
                 */
1781
0
                if (adj_exp >= axexp) {
1782
0
                    shift128RightJamming(axsig0, axsig1, adj_exp - axexp + 1,
1783
0
                                         &axsig0, &axsig1);
1784
0
                    rexp = adj_exp + 1;
1785
0
                    shift128RightJamming(adj_sig0, adj_sig1, 1,
1786
0
                                         &adj_sig0, &adj_sig1);
1787
0
                } else {
1788
0
                    shift128RightJamming(axsig0, axsig1, 1,
1789
0
                                         &axsig0, &axsig1);
1790
0
                    shift128RightJamming(adj_sig0, adj_sig1,
1791
0
                                         axexp - adj_exp + 1,
1792
0
                                         &adj_sig0, &adj_sig1);
1793
0
                    rexp = axexp + 1;
1794
0
                }
1795
0
                if (adj_sub) {
1796
0
                    sub128(adj_sig0, adj_sig1, axsig0, axsig1,
1797
0
                           &rsig0, &rsig1);
1798
0
                } else {
1799
0
                    add128(adj_sig0, adj_sig1, axsig0, axsig1,
1800
0
                           &rsig0, &rsig1);
1801
0
                }
1802
0
            }
1803
1804
0
            set_float_rounding_mode(save_mode, &env->fp_status);
1805
0
            set_floatx80_rounding_precision(save_prec, &env->fp_status);
1806
0
        }
1807
        /* This result is inexact.  */
1808
0
        rsig1 |= 1;
1809
0
        ST1 = normalizeRoundAndPackFloatx80(floatx80_precision_x, rsign, rexp,
1810
0
                                            rsig0, rsig1, &env->fp_status);
1811
0
    }
1812
1813
0
    fpop(env);
1814
0
    merge_exception_flags(env, old_flags);
1815
0
}
1816
1817
/* fpush() only validates the new top. FXTRACT also needs ST(1) validated. */
1818
static inline void fpush_fxtract(CPUX86State *env)
1819
0
{
1820
0
    fpush(env);
1821
0
    env->fptags[(env->fpstt + 1) & 7] = 0;
1822
0
}
1823
1824
void HELPER(fxtract)(CPUX86State *env)
1825
0
{
1826
0
    int old_flags = save_exception_flags(env);
1827
0
    CPU_LDoubleU temp;
1828
1829
0
    temp.d = ST0;
1830
1831
0
    if (floatx80_is_zero(ST0)) {
1832
        /* Easy way to generate -inf and raising division by 0 exception */
1833
0
        ST0 = floatx80_div(floatx80_chs(floatx80_one), floatx80_zero,
1834
0
                           &env->fp_status);
1835
0
        fpush_fxtract(env);
1836
0
        ST0 = temp.d;
1837
0
    } else if (floatx80_invalid_encoding(ST0, &env->fp_status)) {
1838
0
        float_raise(float_flag_invalid, &env->fp_status);
1839
0
        ST0 = floatx80_default_nan(&env->fp_status);
1840
0
        fpush_fxtract(env);
1841
0
        ST0 = ST1;
1842
0
    } else if (floatx80_is_any_nan(ST0)) {
1843
0
        if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
1844
0
            float_raise(float_flag_invalid, &env->fp_status);
1845
0
            ST0 = floatx80_silence_nan(ST0, &env->fp_status);
1846
0
        }
1847
0
        fpush_fxtract(env);
1848
0
        ST0 = ST1;
1849
0
    } else if (floatx80_is_infinity(ST0, &env->fp_status)) {
1850
0
        fpush_fxtract(env);
1851
0
        ST0 = ST1;
1852
0
        ST1 = floatx80_default_inf(0, &env->fp_status);
1853
0
    } else {
1854
0
        int expdif;
1855
1856
0
        if (EXPD(temp) == 0) {
1857
0
            int shift = clz64(temp.l.lower);
1858
0
            temp.l.lower <<= shift;
1859
0
            expdif = 1 - EXPBIAS - shift;
1860
0
            float_raise(float_flag_input_denormal_flushed, &env->fp_status);
1861
0
        } else {
1862
0
            expdif = EXPD(temp) - EXPBIAS;
1863
0
        }
1864
        /* DP exponent bias */
1865
0
        ST0 = int32_to_floatx80(expdif, &env->fp_status);
1866
0
        fpush_fxtract(env);
1867
0
        BIASEXPONENT(temp);
1868
0
        ST0 = temp.d;
1869
0
    }
1870
0
    merge_exception_flags(env, old_flags);
1871
0
}
1872
1873
static void helper_fprem_common(CPUX86State *env, bool mod)
1874
0
{
1875
0
    int old_flags = save_exception_flags(env);
1876
0
    uint64_t quotient;
1877
0
    CPU_LDoubleU temp0, temp1;
1878
0
    int exp0, exp1, expdiff;
1879
1880
0
    temp0.d = ST0;
1881
0
    temp1.d = ST1;
1882
0
    exp0 = EXPD(temp0);
1883
0
    exp1 = EXPD(temp1);
1884
1885
0
    env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
1886
0
    if (floatx80_is_zero(ST0) || floatx80_is_zero(ST1) ||
1887
0
        exp0 == 0x7fff || exp1 == 0x7fff ||
1888
0
        floatx80_invalid_encoding(ST0, &env->fp_status) ||
1889
0
        floatx80_invalid_encoding(ST1, &env->fp_status)) {
1890
0
        ST0 = floatx80_modrem(ST0, ST1, mod, &quotient, &env->fp_status);
1891
0
    } else {
1892
0
        if (exp0 == 0) {
1893
0
            exp0 = 1 - clz64(temp0.l.lower);
1894
0
        }
1895
0
        if (exp1 == 0) {
1896
0
            exp1 = 1 - clz64(temp1.l.lower);
1897
0
        }
1898
0
        expdiff = exp0 - exp1;
1899
0
        if (expdiff < 64) {
1900
0
            ST0 = floatx80_modrem(ST0, ST1, mod, &quotient, &env->fp_status);
1901
0
            env->fpus |= (quotient & 0x4) << (8 - 2);  /* (C0) <-- q2 */
1902
0
            env->fpus |= (quotient & 0x2) << (14 - 1); /* (C3) <-- q1 */
1903
0
            env->fpus |= (quotient & 0x1) << (9 - 0);  /* (C1) <-- q0 */
1904
0
        } else {
1905
            /*
1906
             * Partial remainder.  This choice of how many bits to
1907
             * process at once is specified in AMD instruction set
1908
             * manuals, and empirically is followed by Intel
1909
             * processors as well; it ensures that the final remainder
1910
             * operation in a loop does produce the correct low three
1911
             * bits of the quotient.  AMD manuals specify that the
1912
             * flags other than C2 are cleared, and empirically Intel
1913
             * processors clear them as well.
1914
             */
1915
0
            int n = 32 + (expdiff % 32);
1916
0
            temp1.d = floatx80_scalbn(temp1.d, expdiff - n, &env->fp_status);
1917
0
            ST0 = floatx80_mod(ST0, temp1.d, &env->fp_status);
1918
0
            env->fpus |= 0x400;  /* C2 <-- 1 */
1919
0
        }
1920
0
    }
1921
0
    merge_exception_flags(env, old_flags);
1922
0
}
1923
1924
void HELPER(fprem1)(CPUX86State *env)
1925
0
{
1926
0
    helper_fprem_common(env, false);
1927
0
}
1928
1929
void HELPER(fprem)(CPUX86State *env)
1930
0
{
1931
0
    helper_fprem_common(env, true);
1932
0
}
1933
1934
/* 128-bit significand of log2(e).  */
1935
0
#define log2_e_sig_high 0xb8aa3b295c17f0bbULL
1936
0
#define log2_e_sig_low 0xbe87fed0691d3e89ULL
1937
1938
/*
1939
 * Polynomial coefficients for an approximation to log2((1+x)/(1-x)),
1940
 * with only odd powers of x used, for x in the interval [2*sqrt(2)-3,
1941
 * 3-2*sqrt(2)], which corresponds to logarithms of numbers in the
1942
 * interval [sqrt(2)/2, sqrt(2)].
1943
 */
1944
0
#define fyl2x_coeff_0 make_floatx80(0x4000, 0xb8aa3b295c17f0bcULL)
1945
0
#define fyl2x_coeff_0_low make_floatx80(0xbfbf, 0x834972fe2d7bab1bULL)
1946
0
#define fyl2x_coeff_1 make_floatx80(0x3ffe, 0xf6384ee1d01febb8ULL)
1947
0
#define fyl2x_coeff_2 make_floatx80(0x3ffe, 0x93bb62877cdfa2e3ULL)
1948
0
#define fyl2x_coeff_3 make_floatx80(0x3ffd, 0xd30bb153d808f269ULL)
1949
0
#define fyl2x_coeff_4 make_floatx80(0x3ffd, 0xa42589eaf451499eULL)
1950
0
#define fyl2x_coeff_5 make_floatx80(0x3ffd, 0x864d42c0f8f17517ULL)
1951
0
#define fyl2x_coeff_6 make_floatx80(0x3ffc, 0xe3476578adf26272ULL)
1952
0
#define fyl2x_coeff_7 make_floatx80(0x3ffc, 0xc506c5f874e6d80fULL)
1953
0
#define fyl2x_coeff_8 make_floatx80(0x3ffc, 0xac5cf50cc57d6372ULL)
1954
0
#define fyl2x_coeff_9 make_floatx80(0x3ffc, 0xb1ed0066d971a103ULL)
1955
1956
/*
1957
 * Compute an approximation of log2(1+arg), where 1+arg is in the
1958
 * interval [sqrt(2)/2, sqrt(2)].  It is assumed that when this
1959
 * function is called, rounding precision is set to 80 and the
1960
 * round-to-nearest mode is in effect.  arg must not be exactly zero,
1961
 * and must not be so close to zero that underflow might occur.
1962
 */
1963
static void helper_fyl2x_common(CPUX86State *env, floatx80 arg, int32_t *exp,
1964
                                uint64_t *sig0, uint64_t *sig1)
1965
0
{
1966
0
    uint64_t arg0_sig = extractFloatx80Frac(arg);
1967
0
    int32_t arg0_exp = extractFloatx80Exp(arg);
1968
0
    bool arg0_sign = extractFloatx80Sign(arg);
1969
0
    bool asign;
1970
0
    int32_t dexp, texp, aexp;
1971
0
    uint64_t dsig0, dsig1, tsig0, tsig1, rsig0, rsig1, rsig2;
1972
0
    uint64_t msig0, msig1, msig2, t2sig0, t2sig1, t2sig2, t2sig3;
1973
0
    uint64_t asig0, asig1, asig2, asig3, bsig0, bsig1;
1974
0
    floatx80 t2, accum;
1975
1976
    /*
1977
     * Compute an approximation of arg/(2+arg), with extra precision,
1978
     * as the argument to a polynomial approximation.  The extra
1979
     * precision is only needed for the first term of the
1980
     * approximation, with subsequent terms being significantly
1981
     * smaller; the approximation only uses odd exponents, and the
1982
     * square of arg/(2+arg) is at most 17-12*sqrt(2) = 0.029....
1983
     */
1984
0
    if (arg0_sign) {
1985
0
        dexp = 0x3fff;
1986
0
        shift128RightJamming(arg0_sig, 0, dexp - arg0_exp, &dsig0, &dsig1);
1987
0
        sub128(0, 0, dsig0, dsig1, &dsig0, &dsig1);
1988
0
    } else {
1989
0
        dexp = 0x4000;
1990
0
        shift128RightJamming(arg0_sig, 0, dexp - arg0_exp, &dsig0, &dsig1);
1991
0
        dsig0 |= 0x8000000000000000ULL;
1992
0
    }
1993
0
    texp = arg0_exp - dexp + 0x3ffe;
1994
0
    rsig0 = arg0_sig;
1995
0
    rsig1 = 0;
1996
0
    rsig2 = 0;
1997
0
    if (dsig0 <= rsig0) {
1998
0
        shift128Right(rsig0, rsig1, 1, &rsig0, &rsig1);
1999
0
        ++texp;
2000
0
    }
2001
0
    tsig0 = estimateDiv128To64(rsig0, rsig1, dsig0);
2002
0
    mul128By64To192(dsig0, dsig1, tsig0, &msig0, &msig1, &msig2);
2003
0
    sub192(rsig0, rsig1, rsig2, msig0, msig1, msig2,
2004
0
           &rsig0, &rsig1, &rsig2);
2005
0
    while ((int64_t) rsig0 < 0) {
2006
0
        --tsig0;
2007
0
        add192(rsig0, rsig1, rsig2, 0, dsig0, dsig1,
2008
0
               &rsig0, &rsig1, &rsig2);
2009
0
    }
2010
0
    tsig1 = estimateDiv128To64(rsig1, rsig2, dsig0);
2011
    /*
2012
     * No need to correct any estimation error in tsig1; even with
2013
     * such error, it is accurate enough.  Now compute the square of
2014
     * that approximation.
2015
     */
2016
0
    mul128To256(tsig0, tsig1, tsig0, tsig1,
2017
0
                &t2sig0, &t2sig1, &t2sig2, &t2sig3);
2018
0
    t2 = normalizeRoundAndPackFloatx80(floatx80_precision_x, false,
2019
0
                                       texp + texp - 0x3ffe,
2020
0
                                       t2sig0, t2sig1, &env->fp_status);
2021
2022
    /* Compute the lower parts of the polynomial expansion.  */
2023
0
    accum = floatx80_mul(fyl2x_coeff_9, t2, &env->fp_status);
2024
0
    accum = floatx80_add(fyl2x_coeff_8, accum, &env->fp_status);
2025
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2026
0
    accum = floatx80_add(fyl2x_coeff_7, accum, &env->fp_status);
2027
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2028
0
    accum = floatx80_add(fyl2x_coeff_6, accum, &env->fp_status);
2029
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2030
0
    accum = floatx80_add(fyl2x_coeff_5, accum, &env->fp_status);
2031
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2032
0
    accum = floatx80_add(fyl2x_coeff_4, accum, &env->fp_status);
2033
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2034
0
    accum = floatx80_add(fyl2x_coeff_3, accum, &env->fp_status);
2035
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2036
0
    accum = floatx80_add(fyl2x_coeff_2, accum, &env->fp_status);
2037
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2038
0
    accum = floatx80_add(fyl2x_coeff_1, accum, &env->fp_status);
2039
0
    accum = floatx80_mul(accum, t2, &env->fp_status);
2040
0
    accum = floatx80_add(fyl2x_coeff_0_low, accum, &env->fp_status);
2041
2042
    /*
2043
     * The full polynomial expansion is fyl2x_coeff_0 + accum (where
2044
     * accum has much lower magnitude, and so, in particular, carry
2045
     * out of the addition is not possible), multiplied by t.  (This
2046
     * expansion is only accurate to about 70 bits, not 128 bits.)
2047
     */
2048
0
    aexp = extractFloatx80Exp(fyl2x_coeff_0);
2049
0
    asign = extractFloatx80Sign(fyl2x_coeff_0);
2050
0
    shift128RightJamming(extractFloatx80Frac(accum), 0,
2051
0
                         aexp - extractFloatx80Exp(accum),
2052
0
                         &asig0, &asig1);
2053
0
    bsig0 = extractFloatx80Frac(fyl2x_coeff_0);
2054
0
    bsig1 = 0;
2055
0
    if (asign == extractFloatx80Sign(accum)) {
2056
0
        add128(bsig0, bsig1, asig0, asig1, &asig0, &asig1);
2057
0
    } else {
2058
0
        sub128(bsig0, bsig1, asig0, asig1, &asig0, &asig1);
2059
0
    }
2060
    /* Multiply by t to compute the required result.  */
2061
0
    mul128To256(asig0, asig1, tsig0, tsig1,
2062
0
                &asig0, &asig1, &asig2, &asig3);
2063
0
    aexp += texp - 0x3ffe;
2064
0
    *exp = aexp;
2065
0
    *sig0 = asig0;
2066
0
    *sig1 = asig1;
2067
0
}
2068
2069
void HELPER(fyl2xp1)(CPUX86State *env)
2070
0
{
2071
0
    int old_flags = save_exception_flags(env);
2072
0
    uint64_t arg0_sig = extractFloatx80Frac(ST0);
2073
0
    int32_t arg0_exp = extractFloatx80Exp(ST0);
2074
0
    bool arg0_sign = extractFloatx80Sign(ST0);
2075
0
    uint64_t arg1_sig = extractFloatx80Frac(ST1);
2076
0
    int32_t arg1_exp = extractFloatx80Exp(ST1);
2077
0
    bool arg1_sign = extractFloatx80Sign(ST1);
2078
2079
0
    if (floatx80_invalid_encoding(ST0, &env->fp_status) ||
2080
0
        floatx80_invalid_encoding(ST1, &env->fp_status)) {
2081
0
        float_raise(float_flag_invalid, &env->fp_status);
2082
0
        ST1 = floatx80_default_nan(&env->fp_status);
2083
0
    } else if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
2084
0
        float_raise(float_flag_invalid, &env->fp_status);
2085
0
        ST1 = floatx80_silence_nan(ST0, &env->fp_status);
2086
0
    } else if (floatx80_is_signaling_nan(ST1, &env->fp_status)) {
2087
0
        float_raise(float_flag_invalid, &env->fp_status);
2088
0
        ST1 = floatx80_silence_nan(ST1, &env->fp_status);
2089
0
    } else if (floatx80_is_any_nan(ST0)) {
2090
0
        ST1 = ST0;
2091
0
    } else if (floatx80_is_any_nan(ST1)) {
2092
        /* Pass this NaN through.  */
2093
0
    } else if (arg0_exp > 0x3ffd ||
2094
0
               (arg0_exp == 0x3ffd && arg0_sig > (arg0_sign ?
2095
0
                                                  0x95f619980c4336f7ULL :
2096
0
                                                  0xd413cccfe7799211ULL))) {
2097
        /*
2098
         * Out of range for the instruction (ST0 must have absolute
2099
         * value less than 1 - sqrt(2)/2 = 0.292..., according to
2100
         * Intel manuals; AMD manuals allow a range from sqrt(2)/2 - 1
2101
         * to sqrt(2) - 1, which we allow here), treat as invalid.
2102
         */
2103
0
        float_raise(float_flag_invalid, &env->fp_status);
2104
0
        ST1 = floatx80_default_nan(&env->fp_status);
2105
0
    } else if (floatx80_is_zero(ST0) || floatx80_is_zero(ST1) ||
2106
0
               arg1_exp == 0x7fff) {
2107
        /*
2108
         * One argument is zero, or multiplying by infinity; correct
2109
         * result is exact and can be obtained by multiplying the
2110
         * arguments.
2111
         */
2112
0
        ST1 = floatx80_mul(ST0, ST1, &env->fp_status);
2113
0
    } else if (arg0_exp < 0x3fb0) {
2114
        /*
2115
         * Multiplying both arguments and an extra-precision version
2116
         * of log2(e) is sufficiently precise.
2117
         */
2118
0
        uint64_t sig0, sig1, sig2;
2119
0
        int32_t exp;
2120
0
        if (arg0_exp == 0) {
2121
0
            normalizeFloatx80Subnormal(arg0_sig, &arg0_exp, &arg0_sig);
2122
0
        }
2123
0
        if (arg1_exp == 0) {
2124
0
            normalizeFloatx80Subnormal(arg1_sig, &arg1_exp, &arg1_sig);
2125
0
        }
2126
0
        mul128By64To192(log2_e_sig_high, log2_e_sig_low, arg0_sig,
2127
0
                        &sig0, &sig1, &sig2);
2128
0
        exp = arg0_exp + 1;
2129
0
        mul128By64To192(sig0, sig1, arg1_sig, &sig0, &sig1, &sig2);
2130
0
        exp += arg1_exp - 0x3ffe;
2131
        /* This result is inexact.  */
2132
0
        sig1 |= 1;
2133
0
        ST1 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
2134
0
                                            arg0_sign ^ arg1_sign, exp,
2135
0
                                            sig0, sig1, &env->fp_status);
2136
0
    } else {
2137
0
        int32_t aexp;
2138
0
        uint64_t asig0, asig1, asig2;
2139
0
        FloatRoundMode save_mode = get_float_rounding_mode(&env->fp_status);
2140
0
        FloatX80RoundPrec save_prec =
2141
0
            get_floatx80_rounding_precision(&env->fp_status);
2142
0
        set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
2143
0
        set_floatx80_rounding_precision(floatx80_precision_x, &env->fp_status);
2144
2145
0
        helper_fyl2x_common(env, ST0, &aexp, &asig0, &asig1);
2146
        /*
2147
         * Multiply by the second argument to compute the required
2148
         * result.
2149
         */
2150
0
        if (arg1_exp == 0) {
2151
0
            normalizeFloatx80Subnormal(arg1_sig, &arg1_exp, &arg1_sig);
2152
0
        }
2153
0
        mul128By64To192(asig0, asig1, arg1_sig, &asig0, &asig1, &asig2);
2154
0
        aexp += arg1_exp - 0x3ffe;
2155
        /* This result is inexact.  */
2156
0
        asig1 |= 1;
2157
0
        set_float_rounding_mode(save_mode, &env->fp_status);
2158
0
        ST1 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
2159
0
                                            arg0_sign ^ arg1_sign, aexp,
2160
0
                                            asig0, asig1, &env->fp_status);
2161
0
        set_floatx80_rounding_precision(save_prec, &env->fp_status);
2162
0
    }
2163
0
    fpop(env);
2164
0
    merge_exception_flags(env, old_flags);
2165
0
}
2166
2167
void HELPER(fyl2x)(CPUX86State *env)
2168
0
{
2169
0
    int old_flags = save_exception_flags(env);
2170
0
    uint64_t arg0_sig = extractFloatx80Frac(ST0);
2171
0
    int32_t arg0_exp = extractFloatx80Exp(ST0);
2172
0
    bool arg0_sign = extractFloatx80Sign(ST0);
2173
0
    uint64_t arg1_sig = extractFloatx80Frac(ST1);
2174
0
    int32_t arg1_exp = extractFloatx80Exp(ST1);
2175
0
    bool arg1_sign = extractFloatx80Sign(ST1);
2176
2177
0
    if (floatx80_invalid_encoding(ST0, &env->fp_status) ||
2178
0
        floatx80_invalid_encoding(ST1, &env->fp_status)) {
2179
0
        float_raise(float_flag_invalid, &env->fp_status);
2180
0
        ST1 = floatx80_default_nan(&env->fp_status);
2181
0
    } else if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
2182
0
        float_raise(float_flag_invalid, &env->fp_status);
2183
0
        ST1 = floatx80_silence_nan(ST0, &env->fp_status);
2184
0
    } else if (floatx80_is_signaling_nan(ST1, &env->fp_status)) {
2185
0
        float_raise(float_flag_invalid, &env->fp_status);
2186
0
        ST1 = floatx80_silence_nan(ST1, &env->fp_status);
2187
0
    } else if (floatx80_is_any_nan(ST0)) {
2188
0
        ST1 = ST0;
2189
0
    } else if (floatx80_is_any_nan(ST1)) {
2190
        /* Pass this NaN through.  */
2191
0
    } else if (arg0_sign && !floatx80_is_zero(ST0)) {
2192
0
        float_raise(float_flag_invalid, &env->fp_status);
2193
0
        ST1 = floatx80_default_nan(&env->fp_status);
2194
0
    } else if (floatx80_is_infinity(ST1, &env->fp_status)) {
2195
0
        FloatRelation cmp = floatx80_compare(ST0, floatx80_one,
2196
0
                                             &env->fp_status);
2197
0
        switch (cmp) {
2198
0
        case float_relation_less:
2199
0
            ST1 = floatx80_chs(ST1);
2200
0
            break;
2201
0
        case float_relation_greater:
2202
            /* Result is infinity of the same sign as ST1.  */
2203
0
            break;
2204
0
        default:
2205
0
            float_raise(float_flag_invalid, &env->fp_status);
2206
0
            ST1 = floatx80_default_nan(&env->fp_status);
2207
0
            break;
2208
0
        }
2209
0
    } else if (floatx80_is_infinity(ST0, &env->fp_status)) {
2210
0
        if (floatx80_is_zero(ST1)) {
2211
0
            float_raise(float_flag_invalid, &env->fp_status);
2212
0
            ST1 = floatx80_default_nan(&env->fp_status);
2213
0
        } else if (arg1_sign) {
2214
0
            ST1 = floatx80_chs(ST0);
2215
0
        } else {
2216
0
            ST1 = ST0;
2217
0
        }
2218
0
    } else if (floatx80_is_zero(ST0)) {
2219
0
        if (floatx80_is_zero(ST1)) {
2220
0
            float_raise(float_flag_invalid, &env->fp_status);
2221
0
            ST1 = floatx80_default_nan(&env->fp_status);
2222
0
        } else {
2223
            /* Result is infinity with opposite sign to ST1.  */
2224
0
            float_raise(float_flag_divbyzero, &env->fp_status);
2225
0
            ST1 = make_floatx80(arg1_sign ? 0x7fff : 0xffff,
2226
0
                                0x8000000000000000ULL);
2227
0
        }
2228
0
    } else if (floatx80_is_zero(ST1)) {
2229
0
        if (floatx80_lt(ST0, floatx80_one, &env->fp_status)) {
2230
0
            ST1 = floatx80_chs(ST1);
2231
0
        }
2232
        /* Otherwise, ST1 is already the correct result.  */
2233
0
    } else if (floatx80_eq(ST0, floatx80_one, &env->fp_status)) {
2234
0
        if (arg1_sign) {
2235
0
            ST1 = floatx80_chs(floatx80_zero);
2236
0
        } else {
2237
0
            ST1 = floatx80_zero;
2238
0
        }
2239
0
    } else {
2240
0
        int32_t int_exp;
2241
0
        floatx80 arg0_m1;
2242
0
        FloatRoundMode save_mode = get_float_rounding_mode(&env->fp_status);
2243
0
        FloatX80RoundPrec save_prec =
2244
0
            get_floatx80_rounding_precision(&env->fp_status);
2245
0
        set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
2246
0
        set_floatx80_rounding_precision(floatx80_precision_x, &env->fp_status);
2247
2248
0
        if (arg0_exp == 0) {
2249
0
            normalizeFloatx80Subnormal(arg0_sig, &arg0_exp, &arg0_sig);
2250
0
        }
2251
0
        if (arg1_exp == 0) {
2252
0
            normalizeFloatx80Subnormal(arg1_sig, &arg1_exp, &arg1_sig);
2253
0
        }
2254
0
        int_exp = arg0_exp - 0x3fff;
2255
0
        if (arg0_sig > 0xb504f333f9de6484ULL) {
2256
0
            ++int_exp;
2257
0
        }
2258
0
        arg0_m1 = floatx80_sub(floatx80_scalbn(ST0, -int_exp,
2259
0
                                               &env->fp_status),
2260
0
                               floatx80_one, &env->fp_status);
2261
0
        if (floatx80_is_zero(arg0_m1)) {
2262
            /* Exact power of 2; multiply by ST1.  */
2263
0
            set_float_rounding_mode(save_mode, &env->fp_status);
2264
0
            ST1 = floatx80_mul(int32_to_floatx80(int_exp, &env->fp_status),
2265
0
                               ST1, &env->fp_status);
2266
0
        } else {
2267
0
            bool asign = extractFloatx80Sign(arg0_m1);
2268
0
            int32_t aexp;
2269
0
            uint64_t asig0, asig1, asig2;
2270
0
            helper_fyl2x_common(env, arg0_m1, &aexp, &asig0, &asig1);
2271
0
            if (int_exp != 0) {
2272
0
                bool isign = (int_exp < 0);
2273
0
                int32_t iexp;
2274
0
                uint64_t isig;
2275
0
                int shift;
2276
0
                int_exp = isign ? -int_exp : int_exp;
2277
0
                shift = clz32(int_exp) + 32;
2278
0
                isig = int_exp;
2279
0
                isig <<= shift;
2280
0
                iexp = 0x403e - shift;
2281
0
                shift128RightJamming(asig0, asig1, iexp - aexp,
2282
0
                                     &asig0, &asig1);
2283
0
                if (asign == isign) {
2284
0
                    add128(isig, 0, asig0, asig1, &asig0, &asig1);
2285
0
                } else {
2286
0
                    sub128(isig, 0, asig0, asig1, &asig0, &asig1);
2287
0
                }
2288
0
                aexp = iexp;
2289
0
                asign = isign;
2290
0
            }
2291
            /*
2292
             * Multiply by the second argument to compute the required
2293
             * result.
2294
             */
2295
0
            if (arg1_exp == 0) {
2296
0
                normalizeFloatx80Subnormal(arg1_sig, &arg1_exp, &arg1_sig);
2297
0
            }
2298
0
            mul128By64To192(asig0, asig1, arg1_sig, &asig0, &asig1, &asig2);
2299
0
            aexp += arg1_exp - 0x3ffe;
2300
            /* This result is inexact.  */
2301
0
            asig1 |= 1;
2302
0
            set_float_rounding_mode(save_mode, &env->fp_status);
2303
0
            ST1 = normalizeRoundAndPackFloatx80(floatx80_precision_x,
2304
0
                                                asign ^ arg1_sign, aexp,
2305
0
                                                asig0, asig1, &env->fp_status);
2306
0
        }
2307
2308
0
        set_floatx80_rounding_precision(save_prec, &env->fp_status);
2309
0
    }
2310
0
    fpop(env);
2311
0
    merge_exception_flags(env, old_flags);
2312
0
}
2313
2314
void HELPER(fsqrt)(CPUX86State *env)
2315
0
{
2316
0
    int old_flags = save_exception_flags(env);
2317
0
    if (floatx80_is_neg(ST0)) {
2318
0
        env->fpus &= ~0x4700;  /* (C3,C2,C1,C0) <-- 0000 */
2319
0
        env->fpus |= 0x400;
2320
0
    }
2321
0
    ST0 = floatx80_sqrt(ST0, &env->fp_status);
2322
0
    merge_exception_flags(env, old_flags);
2323
0
}
2324
2325
void HELPER(fsincos)(CPUX86State *env)
2326
0
{
2327
0
    double fptemp = floatx80_to_double(env, ST0);
2328
2329
0
    if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
2330
0
        env->fpus |= 0x400;
2331
0
    } else {
2332
0
        ST0 = double_to_floatx80(env, sin(fptemp));
2333
0
        fpush(env);
2334
0
        ST0 = double_to_floatx80(env, cos(fptemp));
2335
0
        env->fpus &= ~0x400;  /* C2 <-- 0 */
2336
        /* the above code is for |arg| < 2**63 only */
2337
0
    }
2338
0
}
2339
2340
void HELPER(frndint)(CPUX86State *env)
2341
0
{
2342
0
    int old_flags = save_exception_flags(env);
2343
0
    ST0 = floatx80_round_to_int(ST0, &env->fp_status);
2344
0
    merge_exception_flags(env, old_flags);
2345
0
}
2346
2347
void HELPER(fscale)(CPUX86State *env)
2348
0
{
2349
0
    int old_flags = save_exception_flags(env);
2350
0
    if (floatx80_invalid_encoding(ST1, &env->fp_status) ||
2351
0
        floatx80_invalid_encoding(ST0, &env->fp_status)) {
2352
0
        float_raise(float_flag_invalid, &env->fp_status);
2353
0
        ST0 = floatx80_default_nan(&env->fp_status);
2354
0
    } else if (floatx80_is_any_nan(ST1)) {
2355
0
        if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
2356
0
            float_raise(float_flag_invalid, &env->fp_status);
2357
0
        }
2358
0
        ST0 = ST1;
2359
0
        if (floatx80_is_signaling_nan(ST0, &env->fp_status)) {
2360
0
            float_raise(float_flag_invalid, &env->fp_status);
2361
0
            ST0 = floatx80_silence_nan(ST0, &env->fp_status);
2362
0
        }
2363
0
    } else if (floatx80_is_infinity(ST1, &env->fp_status) &&
2364
0
               !floatx80_invalid_encoding(ST0, &env->fp_status) &&
2365
0
               !floatx80_is_any_nan(ST0)) {
2366
0
        if (floatx80_is_neg(ST1)) {
2367
0
            if (floatx80_is_infinity(ST0, &env->fp_status)) {
2368
0
                float_raise(float_flag_invalid, &env->fp_status);
2369
0
                ST0 = floatx80_default_nan(&env->fp_status);
2370
0
            } else {
2371
0
                ST0 = (floatx80_is_neg(ST0) ?
2372
0
                       floatx80_chs(floatx80_zero) :
2373
0
                       floatx80_zero);
2374
0
            }
2375
0
        } else {
2376
0
            if (floatx80_is_zero(ST0)) {
2377
0
                float_raise(float_flag_invalid, &env->fp_status);
2378
0
                ST0 = floatx80_default_nan(&env->fp_status);
2379
0
            } else {
2380
0
                ST0 = floatx80_default_inf(floatx80_is_neg(ST0),
2381
0
                                           &env->fp_status);
2382
0
            }
2383
0
        }
2384
0
    } else {
2385
0
        int n;
2386
0
        FloatX80RoundPrec save = get_floatx80_rounding_precision(&env->fp_status);
2387
0
        int save_flags = get_float_exception_flags(&env->fp_status);
2388
0
        set_float_exception_flags(0, &env->fp_status);
2389
0
        n = floatx80_to_int32_round_to_zero(ST1, &env->fp_status);
2390
0
        set_float_exception_flags(save_flags, &env->fp_status);
2391
0
        set_floatx80_rounding_precision(floatx80_precision_x, &env->fp_status);
2392
0
        ST0 = floatx80_scalbn(ST0, n, &env->fp_status);
2393
0
        set_floatx80_rounding_precision(save, &env->fp_status);
2394
0
    }
2395
0
    merge_exception_flags(env, old_flags);
2396
0
}
2397
2398
void HELPER(fsin)(CPUX86State *env)
2399
0
{
2400
0
    double fptemp = floatx80_to_double(env, ST0);
2401
2402
0
    if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
2403
0
        env->fpus |= 0x400;
2404
0
    } else {
2405
0
        ST0 = double_to_floatx80(env, sin(fptemp));
2406
0
        env->fpus &= ~0x400;  /* C2 <-- 0 */
2407
        /* the above code is for |arg| < 2**53 only */
2408
0
    }
2409
0
}
2410
2411
void HELPER(fcos)(CPUX86State *env)
2412
0
{
2413
0
    double fptemp = floatx80_to_double(env, ST0);
2414
2415
0
    if ((fptemp > MAXTAN) || (fptemp < -MAXTAN)) {
2416
0
        env->fpus |= 0x400;
2417
0
    } else {
2418
0
        ST0 = double_to_floatx80(env, cos(fptemp));
2419
0
        env->fpus &= ~0x400;  /* C2 <-- 0 */
2420
        /* the above code is for |arg| < 2**63 only */
2421
0
    }
2422
0
}
2423
2424
void HELPER(fxam_ST0)(CPUX86State *env)
2425
0
{
2426
0
    CPU_LDoubleU temp;
2427
0
    int expdif;
2428
2429
0
    temp.d = ST0;
2430
2431
0
    env->fpus &= ~0x4700; /* (C3,C2,C1,C0) <-- 0000 */
2432
0
    if (SIGND(temp)) {
2433
0
        env->fpus |= 0x200; /* C1 <-- 1 */
2434
0
    }
2435
2436
0
    if (env->fptags[env->fpstt]) {
2437
0
        env->fpus |= 0x4100; /* Empty */
2438
0
        return;
2439
0
    }
2440
2441
0
    expdif = EXPD(temp);
2442
0
    if (expdif == MAXEXPD) {
2443
0
        if (MANTD(temp) == 0x8000000000000000ULL) {
2444
0
            env->fpus |= 0x500; /* Infinity */
2445
0
        } else if (MANTD(temp) & 0x8000000000000000ULL) {
2446
0
            env->fpus |= 0x100; /* NaN */
2447
0
        }
2448
0
    } else if (expdif == 0) {
2449
0
        if (MANTD(temp) == 0) {
2450
0
            env->fpus |=  0x4000; /* Zero */
2451
0
        } else {
2452
0
            env->fpus |= 0x4400; /* Denormal */
2453
0
        }
2454
0
    } else if (MANTD(temp) & 0x8000000000000000ULL) {
2455
0
        env->fpus |= 0x400;
2456
0
    }
2457
0
}
2458
2459
static void do_fstenv(X86Access *ac, target_ulong ptr, int data32)
2460
0
{
2461
0
    CPUX86State *env = ac->env;
2462
0
    int fpus, fptag, exp, i;
2463
0
    uint64_t mant;
2464
0
    CPU_LDoubleU tmp;
2465
2466
0
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2467
0
    fptag = 0;
2468
0
    for (i = 7; i >= 0; i--) {
2469
0
        fptag <<= 2;
2470
0
        if (env->fptags[i]) {
2471
0
            fptag |= 3;
2472
0
        } else {
2473
0
            tmp.d = env->fpregs[i].d;
2474
0
            exp = EXPD(tmp);
2475
0
            mant = MANTD(tmp);
2476
0
            if (exp == 0 && mant == 0) {
2477
                /* zero */
2478
0
                fptag |= 1;
2479
0
            } else if (exp == 0 || exp == MAXEXPD
2480
0
                       || (mant & (1LL << 63)) == 0) {
2481
                /* NaNs, infinity, denormal */
2482
0
                fptag |= 2;
2483
0
            }
2484
0
        }
2485
0
    }
2486
0
    if (data32) {
2487
        /* 32 bit */
2488
0
        access_stl(ac, ptr, env->fpuc);
2489
0
        access_stl(ac, ptr + 4, fpus);
2490
0
        access_stl(ac, ptr + 8, fptag);
2491
0
        access_stl(ac, ptr + 12, env->fpip); /* fpip */
2492
0
        access_stl(ac, ptr + 16, env->fpcs); /* fpcs */
2493
0
        access_stl(ac, ptr + 20, env->fpdp); /* fpoo */
2494
0
        access_stl(ac, ptr + 24, env->fpds); /* fpos */
2495
0
    } else {
2496
        /* 16 bit */
2497
0
        access_stw(ac, ptr, env->fpuc);
2498
0
        access_stw(ac, ptr + 2, fpus);
2499
0
        access_stw(ac, ptr + 4, fptag);
2500
0
        access_stw(ac, ptr + 6, env->fpip);
2501
0
        access_stw(ac, ptr + 8, env->fpcs);
2502
0
        access_stw(ac, ptr + 10, env->fpdp);
2503
0
        access_stw(ac, ptr + 12, env->fpds);
2504
0
    }
2505
0
}
2506
2507
void HELPER(fstenv)(CPUX86State *env, target_ulong ptr, int data32)
2508
0
{
2509
0
    X86Access ac;
2510
2511
0
    access_prepare(&ac, env, ptr, 14 << data32, MMU_DATA_STORE, GETPC());
2512
0
    do_fstenv(&ac, ptr, data32);
2513
0
}
2514
2515
static void cpu_set_fpus(CPUX86State *env, uint16_t fpus)
2516
0
{
2517
0
    env->fpstt = (fpus >> 11) & 7;
2518
0
    env->fpus = fpus & ~0x3800 & ~FPUS_B;
2519
0
    env->fpus |= env->fpus & FPUS_SE ? FPUS_B : 0;
2520
0
#if !defined(CONFIG_USER_ONLY)
2521
0
    if (!(env->fpus & FPUS_SE)) {
2522
        /*
2523
         * Here the processor deasserts FERR#; in response, the chipset deasserts
2524
         * IGNNE#.
2525
         */
2526
0
        cpu_clear_ignne();
2527
0
    }
2528
0
#endif
2529
0
}
2530
2531
static void do_fldenv(X86Access *ac, target_ulong ptr, int data32)
2532
0
{
2533
0
    int i, fpus, fptag;
2534
0
    CPUX86State *env = ac->env;
2535
2536
0
    cpu_set_fpuc(env, access_ldw(ac, ptr));
2537
0
    fpus = access_ldw(ac, ptr + (2 << data32));
2538
0
    fptag = access_ldw(ac, ptr + (4 << data32));
2539
2540
0
    cpu_set_fpus(env, fpus);
2541
0
    for (i = 0; i < 8; i++) {
2542
0
        env->fptags[i] = ((fptag & 3) == 3);
2543
0
        fptag >>= 2;
2544
0
    }
2545
0
}
2546
2547
void HELPER(fldenv)(CPUX86State *env, target_ulong ptr, int data32)
2548
0
{
2549
0
    X86Access ac;
2550
2551
0
    access_prepare(&ac, env, ptr, 14 << data32, MMU_DATA_STORE, GETPC());
2552
0
    do_fldenv(&ac, ptr, data32);
2553
0
}
2554
2555
/*
2556
 * Store the environment and the register stack, as FSAVE does, but
2557
 * without the FNINIT that FSAVE performs afterward.
2558
 */
2559
static void do_fsave(X86Access *ac, target_ulong ptr, int data32)
2560
0
{
2561
0
    CPUX86State *env = ac->env;
2562
2563
0
    do_fstenv(ac, ptr, data32);
2564
0
    ptr += 14 << data32;
2565
2566
0
    for (int i = 0; i < 8; i++) {
2567
0
        floatx80 tmp = ST(i);
2568
0
        do_fstt(ac, ptr, tmp);
2569
0
        ptr += 10;
2570
0
    }
2571
0
}
2572
2573
void HELPER(fsave)(CPUX86State *env, target_ulong ptr, int data32)
2574
0
{
2575
0
    int size = (14 << data32) + 80;
2576
0
    X86Access ac;
2577
2578
0
    access_prepare(&ac, env, ptr, size, MMU_DATA_STORE, GETPC());
2579
0
    do_fsave(&ac, ptr, data32);
2580
0
    do_fninit(env);
2581
0
}
2582
2583
static void do_frstor(X86Access *ac, target_ulong ptr, int data32)
2584
0
{
2585
0
    CPUX86State *env = ac->env;
2586
2587
0
    do_fldenv(ac, ptr, data32);
2588
0
    ptr += 14 << data32;
2589
2590
0
    for (int i = 0; i < 8; i++) {
2591
0
        floatx80 tmp = do_fldt(ac, ptr);
2592
0
        ST(i) = tmp;
2593
0
        ptr += 10;
2594
0
    }
2595
0
}
2596
2597
void HELPER(frstor)(CPUX86State *env, target_ulong ptr, int data32)
2598
0
{
2599
0
    int size = (14 << data32) + 80;
2600
0
    X86Access ac;
2601
2602
0
    access_prepare(&ac, env, ptr, size, MMU_DATA_LOAD, GETPC());
2603
0
    do_frstor(&ac, ptr, data32);
2604
0
}
2605
2606
0
#define XO(X)  offsetof(X86XSaveArea, X)
2607
2608
static void do_xsave_fpu(X86Access *ac, target_ulong ptr)
2609
0
{
2610
0
    CPUX86State *env = ac->env;
2611
0
    int fpus, fptag, i;
2612
0
    target_ulong addr;
2613
2614
0
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2615
0
    fptag = 0;
2616
0
    for (i = 0; i < 8; i++) {
2617
0
        fptag |= (env->fptags[i] << i);
2618
0
    }
2619
2620
0
    access_stw(ac, ptr + XO(legacy.fcw), env->fpuc);
2621
0
    access_stw(ac, ptr + XO(legacy.fsw), fpus);
2622
0
    access_stw(ac, ptr + XO(legacy.ftw), fptag ^ 0xff);
2623
0
    access_stw(ac, ptr + XO(legacy.fpop), 0);
2624
2625
    /* In 32-bit mode this is eip, sel, dp, sel.
2626
       In 64-bit mode this is rip, rdp.
2627
       But in either case we don't write actual data, just zeros.  */
2628
0
    access_stq(ac, ptr + XO(legacy.fpip), 0); /* eip+sel; rip */
2629
0
    access_stq(ac, ptr + XO(legacy.fpdp), 0); /* edp+sel; rdp */
2630
2631
0
    addr = ptr + XO(legacy.fpregs);
2632
2633
0
    for (i = 0; i < 8; i++) {
2634
0
        floatx80 tmp = ST(i);
2635
0
        do_fstt(ac, addr, tmp);
2636
0
        access_stw(ac, addr + 10, 0);
2637
0
        access_stl(ac, addr + 12, 0);
2638
0
        addr += 16;
2639
0
    }
2640
0
}
2641
2642
static void do_xsave_mxcsr(X86Access *ac, target_ulong ptr)
2643
0
{
2644
0
    CPUX86State *env = ac->env;
2645
2646
0
    update_mxcsr_from_sse_status(env);
2647
0
    access_stl(ac, ptr + XO(legacy.mxcsr), env->mxcsr);
2648
0
    access_stl(ac, ptr + XO(legacy.mxcsr_mask), 0x0000ffff);
2649
0
}
2650
2651
static void do_xsave_sse(X86Access *ac, target_ulong ptr)
2652
0
{
2653
0
    CPUX86State *env = ac->env;
2654
0
    int i, nb_xmm_regs;
2655
0
    target_ulong addr;
2656
2657
0
    if (env->hflags & HF_CS64_MASK) {
2658
0
        nb_xmm_regs = 16;
2659
0
    } else {
2660
0
        nb_xmm_regs = 8;
2661
0
    }
2662
2663
0
    addr = ptr + XO(legacy.xmm_regs);
2664
0
    for (i = 0; i < nb_xmm_regs; i++) {
2665
0
        access_stq(ac, addr, env->xmm_regs[i].ZMM_Q(0));
2666
0
        access_stq(ac, addr + 8, env->xmm_regs[i].ZMM_Q(1));
2667
0
        addr += 16;
2668
0
    }
2669
0
}
2670
2671
static void do_xsave_ymmh(X86Access *ac, target_ulong ptr)
2672
0
{
2673
0
    CPUX86State *env = ac->env;
2674
0
    int i, nb_xmm_regs;
2675
2676
0
    if (env->hflags & HF_CS64_MASK) {
2677
0
        nb_xmm_regs = 16;
2678
0
    } else {
2679
0
        nb_xmm_regs = 8;
2680
0
    }
2681
2682
0
    for (i = 0; i < nb_xmm_regs; i++, ptr += 16) {
2683
0
        access_stq(ac, ptr, env->xmm_regs[i].ZMM_Q(2));
2684
0
        access_stq(ac, ptr + 8, env->xmm_regs[i].ZMM_Q(3));
2685
0
    }
2686
0
}
2687
2688
static void do_xsave_bndregs(X86Access *ac, target_ulong ptr)
2689
0
{
2690
0
    CPUX86State *env = ac->env;
2691
0
    target_ulong addr = ptr + offsetof(XSaveBNDREG, bnd_regs);
2692
0
    int i;
2693
2694
0
    for (i = 0; i < 4; i++, addr += 16) {
2695
0
        access_stq(ac, addr, env->bnd_regs[i].lb);
2696
0
        access_stq(ac, addr + 8, env->bnd_regs[i].ub);
2697
0
    }
2698
0
}
2699
2700
static void do_xsave_bndcsr(X86Access *ac, target_ulong ptr)
2701
0
{
2702
0
    CPUX86State *env = ac->env;
2703
2704
0
    access_stq(ac, ptr + offsetof(XSaveBNDCSR, bndcsr.cfgu),
2705
0
               env->bndcs_regs.cfgu);
2706
0
    access_stq(ac, ptr + offsetof(XSaveBNDCSR, bndcsr.sts),
2707
0
               env->bndcs_regs.sts);
2708
0
}
2709
2710
static void do_xsave_pkru(X86Access *ac, target_ulong ptr)
2711
0
{
2712
0
    access_stq(ac, ptr, ac->env->pkru);
2713
0
}
2714
2715
#ifdef TARGET_X86_64
2716
static void do_xsave_apx(X86Access *ac, target_ulong ptr)
2717
{
2718
    CPUX86State *env = ac->env;
2719
    int i;
2720
2721
    for (i = 16; i < 32; i++) {
2722
        access_stq(ac, ptr, env->regs[i]);
2723
        ptr += 8;
2724
    }
2725
}
2726
#endif
2727
2728
static void do_fxsave(X86Access *ac, target_ulong ptr)
2729
0
{
2730
0
    CPUX86State *env = ac->env;
2731
2732
0
    do_xsave_fpu(ac, ptr);
2733
0
    if (env->cr[4] & CR4_OSFXSR_MASK) {
2734
0
        do_xsave_mxcsr(ac, ptr);
2735
        /* Fast FXSAVE leaves out the XMM registers */
2736
0
        if (!(env->efer & MSR_EFER_FFXSR)
2737
0
            || (env->hflags & HF_CPL_MASK)
2738
0
            || !(env->hflags & HF_LMA_MASK)) {
2739
0
            do_xsave_sse(ac, ptr);
2740
0
        }
2741
0
    }
2742
0
}
2743
2744
void HELPER(fxsave)(CPUX86State *env, target_ulong ptr)
2745
0
{
2746
0
    uintptr_t ra = GETPC();
2747
0
    X86Access ac;
2748
2749
    /* The operand must be 16 byte aligned */
2750
0
    if (ptr & 0xf) {
2751
0
        raise_exception_ra(env, EXCP0D_GPF, ra);
2752
0
    }
2753
2754
0
    access_prepare(&ac, env, ptr, sizeof(X86LegacyXSaveArea),
2755
0
                   MMU_DATA_STORE, ra);
2756
0
    do_fxsave(&ac, ptr);
2757
0
}
2758
2759
static uint64_t get_xinuse(CPUX86State *env)
2760
0
{
2761
0
    uint64_t inuse = -1;
2762
2763
    /* For the most part, we don't track XINUSE.  We could calculate it
2764
       here for all components, but it's probably less work to simply
2765
       indicate in use.  That said, the state of BNDREGS is important
2766
       enough to track in HFLAGS, so we might as well use that here.  */
2767
0
    if ((env->hflags & HF_MPX_IU_MASK) == 0) {
2768
0
       inuse &= ~XSTATE_BNDREGS_MASK;
2769
0
    }
2770
0
    return inuse;
2771
0
}
2772
2773
static void do_xsave_access(X86Access *ac, target_ulong ptr, uint64_t rfbm,
2774
                            uint64_t inuse, uint64_t opt)
2775
0
{
2776
0
    uint64_t old_bv, new_bv;
2777
2778
0
    if (opt & XSTATE_FP_MASK) {
2779
0
        do_xsave_fpu(ac, ptr);
2780
0
    }
2781
0
    if (rfbm & XSTATE_SSE_MASK) {
2782
        /* Note that saving MXCSR is not suppressed by XSAVEOPT.  */
2783
0
        do_xsave_mxcsr(ac, ptr);
2784
0
    }
2785
0
    if (opt & XSTATE_SSE_MASK) {
2786
0
        do_xsave_sse(ac, ptr);
2787
0
    }
2788
0
    if (opt & XSTATE_YMM_MASK) {
2789
0
        do_xsave_ymmh(ac, ptr + XO(avx_state));
2790
0
    }
2791
0
    if (opt & XSTATE_BNDREGS_MASK) {
2792
0
        do_xsave_bndregs(ac, ptr + XO(mpx_state.bndreg));
2793
0
    }
2794
0
    if (opt & XSTATE_BNDCSR_MASK) {
2795
0
        do_xsave_bndcsr(ac, ptr + XO(mpx_state.bndcsr));
2796
0
    }
2797
0
    if (opt & XSTATE_PKRU_MASK) {
2798
0
        do_xsave_pkru(ac, ptr + XO(pkru_state));
2799
0
    }
2800
#ifdef TARGET_X86_64
2801
    if (opt & XSTATE_APX_MASK) {
2802
        do_xsave_apx(ac, ptr + XO(apx_state));
2803
    }
2804
#endif
2805
2806
    /* Update the XSTATE_BV field.  */
2807
0
    old_bv = access_ldq(ac, ptr + XO(header.xstate_bv));
2808
0
    new_bv = (old_bv & ~rfbm) | (inuse & rfbm);
2809
0
    access_stq(ac, ptr + XO(header.xstate_bv), new_bv);
2810
0
}
2811
2812
static void do_xsave_chk(CPUX86State *env, target_ulong ptr, uintptr_t ra)
2813
0
{
2814
    /* The OS must have enabled XSAVE.  */
2815
0
    if (!(env->cr[4] & CR4_OSXSAVE_MASK)) {
2816
0
        raise_exception_ra(env, EXCP06_ILLOP, ra);
2817
0
    }
2818
2819
    /* The operand must be 64 byte aligned.  */
2820
0
    if (ptr & 63) {
2821
0
        raise_exception_ra(env, EXCP0D_GPF, ra);
2822
0
    }
2823
0
}
2824
2825
static void do_xsave(CPUX86State *env, target_ulong ptr, uint64_t rfbm,
2826
                     uint64_t inuse, uint64_t opt, uintptr_t ra)
2827
0
{
2828
0
    X86Access ac;
2829
0
    unsigned size;
2830
2831
0
    do_xsave_chk(env, ptr, ra);
2832
2833
    /* Never save anything not enabled by XCR0.  */
2834
0
    rfbm &= env->xcr0;
2835
0
    opt &= rfbm;
2836
0
    size = xsave_area_size(opt, false);
2837
2838
0
    access_prepare(&ac, env, ptr, size, MMU_DATA_STORE, ra);
2839
0
    do_xsave_access(&ac, ptr, rfbm, inuse, opt);
2840
0
}
2841
2842
void HELPER(xsave)(CPUX86State *env, target_ulong ptr, uint64_t rfbm)
2843
0
{
2844
0
    do_xsave(env, ptr, rfbm, get_xinuse(env), rfbm, GETPC());
2845
0
}
2846
2847
void HELPER(xsaveopt)(CPUX86State *env, target_ulong ptr, uint64_t rfbm)
2848
0
{
2849
0
    uint64_t inuse = get_xinuse(env);
2850
0
    do_xsave(env, ptr, rfbm, inuse, inuse, GETPC());
2851
0
}
2852
2853
static void do_xrstor_fpu(X86Access *ac, target_ulong ptr)
2854
0
{
2855
0
    CPUX86State *env = ac->env;
2856
0
    int i, fpuc, fpus, fptag;
2857
0
    target_ulong addr;
2858
2859
0
    fpuc = access_ldw(ac, ptr + XO(legacy.fcw));
2860
0
    fpus = access_ldw(ac, ptr + XO(legacy.fsw));
2861
0
    fptag = access_ldw(ac, ptr + XO(legacy.ftw));
2862
0
    cpu_set_fpuc(env, fpuc);
2863
0
    cpu_set_fpus(env, fpus);
2864
2865
0
    fptag ^= 0xff;
2866
0
    for (i = 0; i < 8; i++) {
2867
0
        env->fptags[i] = ((fptag >> i) & 1);
2868
0
    }
2869
2870
0
    addr = ptr + XO(legacy.fpregs);
2871
2872
0
    for (i = 0; i < 8; i++) {
2873
0
        floatx80 tmp = do_fldt(ac, addr);
2874
0
        ST(i) = tmp;
2875
0
        addr += 16;
2876
0
    }
2877
0
}
2878
2879
static void do_xrstor_mxcsr(X86Access *ac, target_ulong ptr)
2880
0
{
2881
0
    CPUX86State *env = ac->env;
2882
0
    cpu_set_mxcsr(env, access_ldl(ac, ptr + XO(legacy.mxcsr)));
2883
0
}
2884
2885
static void do_xrstor_sse(X86Access *ac, target_ulong ptr)
2886
0
{
2887
0
    CPUX86State *env = ac->env;
2888
0
    int i, nb_xmm_regs;
2889
0
    target_ulong addr;
2890
2891
0
    if (env->hflags & HF_CS64_MASK) {
2892
0
        nb_xmm_regs = 16;
2893
0
    } else {
2894
0
        nb_xmm_regs = 8;
2895
0
    }
2896
2897
0
    addr = ptr + XO(legacy.xmm_regs);
2898
0
    for (i = 0; i < nb_xmm_regs; i++) {
2899
0
        env->xmm_regs[i].ZMM_Q(0) = access_ldq(ac, addr);
2900
0
        env->xmm_regs[i].ZMM_Q(1) = access_ldq(ac, addr + 8);
2901
0
        addr += 16;
2902
0
    }
2903
0
}
2904
2905
static void do_clear_sse(CPUX86State *env)
2906
0
{
2907
0
    int i, nb_xmm_regs;
2908
2909
0
    if (env->hflags & HF_CS64_MASK) {
2910
0
        nb_xmm_regs = 16;
2911
0
    } else {
2912
0
        nb_xmm_regs = 8;
2913
0
    }
2914
2915
0
    for (i = 0; i < nb_xmm_regs; i++) {
2916
0
        env->xmm_regs[i].ZMM_Q(0) = 0;
2917
0
        env->xmm_regs[i].ZMM_Q(1) = 0;
2918
0
    }
2919
0
}
2920
2921
static void do_xrstor_ymmh(X86Access *ac, target_ulong ptr)
2922
0
{
2923
0
    CPUX86State *env = ac->env;
2924
0
    int i, nb_xmm_regs;
2925
2926
0
    if (env->hflags & HF_CS64_MASK) {
2927
0
        nb_xmm_regs = 16;
2928
0
    } else {
2929
0
        nb_xmm_regs = 8;
2930
0
    }
2931
2932
0
    for (i = 0; i < nb_xmm_regs; i++, ptr += 16) {
2933
0
        env->xmm_regs[i].ZMM_Q(2) = access_ldq(ac, ptr);
2934
0
        env->xmm_regs[i].ZMM_Q(3) = access_ldq(ac, ptr + 8);
2935
0
    }
2936
0
}
2937
2938
static void do_clear_ymmh(CPUX86State *env)
2939
0
{
2940
0
    int i, nb_xmm_regs;
2941
2942
0
    if (env->hflags & HF_CS64_MASK) {
2943
0
        nb_xmm_regs = 16;
2944
0
    } else {
2945
0
        nb_xmm_regs = 8;
2946
0
    }
2947
2948
0
    for (i = 0; i < nb_xmm_regs; i++) {
2949
0
        env->xmm_regs[i].ZMM_Q(2) = 0;
2950
0
        env->xmm_regs[i].ZMM_Q(3) = 0;
2951
0
    }
2952
0
}
2953
2954
static void do_xrstor_bndregs(X86Access *ac, target_ulong ptr)
2955
0
{
2956
0
    CPUX86State *env = ac->env;
2957
0
    target_ulong addr = ptr + offsetof(XSaveBNDREG, bnd_regs);
2958
0
    int i;
2959
2960
0
    for (i = 0; i < 4; i++, addr += 16) {
2961
0
        env->bnd_regs[i].lb = access_ldq(ac, addr);
2962
0
        env->bnd_regs[i].ub = access_ldq(ac, addr + 8);
2963
0
    }
2964
0
}
2965
2966
static void do_xrstor_bndcsr(X86Access *ac, target_ulong ptr)
2967
0
{
2968
0
    CPUX86State *env = ac->env;
2969
2970
    /* FIXME: Extend highest implemented bit of linear address.  */
2971
0
    env->bndcs_regs.cfgu
2972
0
        = access_ldq(ac, ptr + offsetof(XSaveBNDCSR, bndcsr.cfgu));
2973
0
    env->bndcs_regs.sts
2974
0
        = access_ldq(ac, ptr + offsetof(XSaveBNDCSR, bndcsr.sts));
2975
0
}
2976
2977
static void do_xrstor_pkru(X86Access *ac, target_ulong ptr)
2978
0
{
2979
0
    ac->env->pkru = access_ldq(ac, ptr);
2980
0
}
2981
2982
static void do_fxrstor(X86Access *ac, target_ulong ptr)
2983
0
{
2984
0
    CPUX86State *env = ac->env;
2985
2986
0
    do_xrstor_fpu(ac, ptr);
2987
0
    if (env->cr[4] & CR4_OSFXSR_MASK) {
2988
0
        do_xrstor_mxcsr(ac, ptr);
2989
        /* Fast FXRSTOR leaves out the XMM registers */
2990
0
        if (!(env->efer & MSR_EFER_FFXSR)
2991
0
            || (env->hflags & HF_CPL_MASK)
2992
0
            || !(env->hflags & HF_LMA_MASK)) {
2993
0
            do_xrstor_sse(ac, ptr);
2994
0
        }
2995
0
    }
2996
0
}
2997
2998
#ifdef TARGET_X86_64
2999
static void do_xrstor_apx(X86Access *ac, target_ulong ptr)
3000
{
3001
    CPUX86State *env = ac->env;
3002
    int i;
3003
3004
    for (i = 16; i < 32; i++) {
3005
        env->regs[i] = access_ldq(ac, ptr);
3006
        ptr += 8;
3007
    }
3008
}
3009
3010
static void do_clear_apx(X86Access *ac)
3011
{
3012
    CPUX86State *env = ac->env;
3013
    int i;
3014
3015
    for (i = 16; i < 32; i++) {
3016
        env->regs[i] = 0;
3017
    }
3018
}
3019
#endif
3020
3021
void HELPER(fxrstor)(CPUX86State *env, target_ulong ptr)
3022
0
{
3023
0
    uintptr_t ra = GETPC();
3024
0
    X86Access ac;
3025
3026
    /* The operand must be 16 byte aligned */
3027
0
    if (ptr & 0xf) {
3028
0
        raise_exception_ra(env, EXCP0D_GPF, ra);
3029
0
    }
3030
3031
0
    access_prepare(&ac, env, ptr, sizeof(X86LegacyXSaveArea),
3032
0
                   MMU_DATA_LOAD, ra);
3033
0
    do_fxrstor(&ac, ptr);
3034
0
}
3035
3036
static bool valid_xrstor_header(X86Access *ac, uint64_t *pxsbv,
3037
                                target_ulong ptr)
3038
0
{
3039
0
    uint64_t xstate_bv, xcomp_bv, reserve0;
3040
3041
0
    xstate_bv = access_ldq(ac, ptr + XO(header.xstate_bv));
3042
0
    xcomp_bv = access_ldq(ac, ptr + XO(header.xcomp_bv));
3043
0
    reserve0 = access_ldq(ac, ptr + XO(header.reserve0));
3044
0
    *pxsbv = xstate_bv;
3045
3046
    /*
3047
     * XCOMP_BV bit 63 indicates compact form, which we do not support,
3048
     * and thus must raise #GP.  That leaves us in standard form.
3049
     * In standard form, bytes 23:8 must be zero -- which is both
3050
     * XCOMP_BV and the following 64-bit field.
3051
     */
3052
0
    if (xcomp_bv || reserve0) {
3053
0
        return false;
3054
0
    }
3055
3056
    /* The XSTATE_BV field must not set bits not present in XCR0.  */
3057
0
    return (xstate_bv & ~ac->env->xcr0) == 0;
3058
0
}
3059
3060
static void do_xrstor(X86Access *ac, target_ulong ptr,
3061
                      uint64_t rfbm, uint64_t xstate_bv)
3062
0
{
3063
0
    CPUX86State *env = ac->env;
3064
3065
0
    if (rfbm & XSTATE_FP_MASK) {
3066
0
        if (xstate_bv & XSTATE_FP_MASK) {
3067
0
            do_xrstor_fpu(ac, ptr);
3068
0
        } else {
3069
0
            do_fninit(env);
3070
0
            memset(env->fpregs, 0, sizeof(env->fpregs));
3071
0
        }
3072
0
    }
3073
0
    if (rfbm & XSTATE_SSE_MASK) {
3074
        /* Note that the standard form of XRSTOR loads MXCSR from memory
3075
           whether or not the XSTATE_BV bit is set.  */
3076
0
        do_xrstor_mxcsr(ac, ptr);
3077
0
        if (xstate_bv & XSTATE_SSE_MASK) {
3078
0
            do_xrstor_sse(ac, ptr);
3079
0
        } else {
3080
0
            do_clear_sse(env);
3081
0
        }
3082
0
    }
3083
0
    if (rfbm & XSTATE_YMM_MASK) {
3084
0
        if (xstate_bv & XSTATE_YMM_MASK) {
3085
0
            do_xrstor_ymmh(ac, ptr + XO(avx_state));
3086
0
        } else {
3087
0
            do_clear_ymmh(env);
3088
0
        }
3089
0
    }
3090
0
    if (rfbm & XSTATE_BNDREGS_MASK) {
3091
0
        if (xstate_bv & XSTATE_BNDREGS_MASK) {
3092
0
            do_xrstor_bndregs(ac, ptr + XO(mpx_state.bndreg));
3093
0
            env->hflags |= HF_MPX_IU_MASK;
3094
0
        } else {
3095
0
            memset(env->bnd_regs, 0, sizeof(env->bnd_regs));
3096
0
            env->hflags &= ~HF_MPX_IU_MASK;
3097
0
        }
3098
0
    }
3099
0
    if (rfbm & XSTATE_BNDCSR_MASK) {
3100
0
        if (xstate_bv & XSTATE_BNDCSR_MASK) {
3101
0
            do_xrstor_bndcsr(ac, ptr + XO(mpx_state.bndcsr));
3102
0
        } else {
3103
0
            memset(&env->bndcs_regs, 0, sizeof(env->bndcs_regs));
3104
0
        }
3105
0
        cpu_sync_bndcs_hflags(env);
3106
0
    }
3107
0
    if (rfbm & XSTATE_PKRU_MASK) {
3108
0
        uint64_t old_pkru = env->pkru;
3109
0
        if (xstate_bv & XSTATE_PKRU_MASK) {
3110
0
            do_xrstor_pkru(ac, ptr + XO(pkru_state));
3111
0
        } else {
3112
0
            env->pkru = 0;
3113
0
        }
3114
0
        if (env->pkru != old_pkru) {
3115
0
            CPUState *cs = env_cpu(env);
3116
0
            tlb_flush(cs);
3117
0
        }
3118
0
    }
3119
#ifdef TARGET_X86_64
3120
    if (rfbm & XSTATE_APX_MASK) {
3121
        if (xstate_bv & XSTATE_APX_MASK) {
3122
            do_xrstor_apx(ac, ptr + XO(apx_state));
3123
        } else {
3124
            do_clear_apx(ac);
3125
        }
3126
    }
3127
#endif
3128
0
}
3129
3130
#undef XO
3131
3132
void HELPER(xrstor)(CPUX86State *env, target_ulong ptr, uint64_t rfbm)
3133
0
{
3134
0
    uintptr_t ra = GETPC();
3135
0
    X86Access ac;
3136
0
    uint64_t xstate_bv;
3137
0
    unsigned size, size_ext;
3138
3139
0
    do_xsave_chk(env, ptr, ra);
3140
3141
    /* Begin with just the minimum size to validate the header. */
3142
0
    size = sizeof(X86LegacyXSaveArea) + sizeof(X86XSaveHeader);
3143
0
    access_prepare(&ac, env, ptr, size, MMU_DATA_LOAD, ra);
3144
0
    if (!valid_xrstor_header(&ac, &xstate_bv, ptr)) {
3145
0
        raise_exception_ra(env, EXCP0D_GPF, ra);
3146
0
    }
3147
3148
0
    rfbm &= env->xcr0;
3149
0
    size_ext = xsave_area_size(rfbm & xstate_bv, false);
3150
0
    if (size < size_ext) {
3151
        /* TODO: See if existing page probe has covered extra size. */
3152
0
        access_prepare(&ac, env, ptr, size_ext, MMU_DATA_LOAD, ra);
3153
0
    }
3154
3155
0
    do_xrstor(&ac, ptr, rfbm, xstate_bv);
3156
0
}
3157
3158
#if defined(CONFIG_USER_ONLY)
3159
void cpu_x86_fsave(CPUX86State *env, void *host, size_t len)
3160
{
3161
    cpu_x86_fsave_noinit(env, host, len);
3162
    do_fninit(env);
3163
}
3164
3165
void cpu_x86_fsave_noinit(CPUX86State *env, void *host, size_t len)
3166
{
3167
    X86Access ac = {
3168
        .haddr1 = host,
3169
        .size = 4 * 7 + 8 * 10,
3170
        .env = env,
3171
    };
3172
3173
    assert(ac.size <= len);
3174
    do_fsave(&ac, 0, true);
3175
}
3176
3177
void cpu_x86_frstor(CPUX86State *env, void *host, size_t len)
3178
{
3179
    X86Access ac = {
3180
        .haddr1 = host,
3181
        .size = 4 * 7 + 8 * 10,
3182
        .env = env,
3183
    };
3184
3185
    assert(ac.size <= len);
3186
    do_frstor(&ac, 0, true);
3187
}
3188
3189
void cpu_x86_fxsave(CPUX86State *env, void *host, size_t len)
3190
{
3191
    X86Access ac = {
3192
        .haddr1 = host,
3193
        .size = sizeof(X86LegacyXSaveArea),
3194
        .env = env,
3195
    };
3196
3197
    assert(ac.size <= len);
3198
    do_fxsave(&ac, 0);
3199
}
3200
3201
void cpu_x86_fxrstor(CPUX86State *env, void *host, size_t len)
3202
{
3203
    X86Access ac = {
3204
        .haddr1 = host,
3205
        .size = sizeof(X86LegacyXSaveArea),
3206
        .env = env,
3207
    };
3208
3209
    assert(ac.size <= len);
3210
    do_fxrstor(&ac, 0);
3211
}
3212
3213
void cpu_x86_xsave(CPUX86State *env, void *host, size_t len, uint64_t rfbm)
3214
{
3215
    X86Access ac = {
3216
        .haddr1 = host,
3217
        .env = env,
3218
    };
3219
3220
    /*
3221
     * Since this is only called from user-level signal handling,
3222
     * we should have done the job correctly there.
3223
     */
3224
    assert((rfbm & ~env->xcr0) == 0);
3225
    ac.size = xsave_area_size(rfbm, false);
3226
    assert(ac.size <= len);
3227
    do_xsave_access(&ac, 0, rfbm, get_xinuse(env), rfbm);
3228
}
3229
3230
bool cpu_x86_xrstor(CPUX86State *env, void *host, size_t len, uint64_t rfbm)
3231
{
3232
    X86Access ac = {
3233
        .haddr1 = host,
3234
        .env = env,
3235
    };
3236
    uint64_t xstate_bv;
3237
3238
    /*
3239
     * Since this is only called from user-level signal handling,
3240
     * we should have done the job correctly there.
3241
     */
3242
    assert((rfbm & ~env->xcr0) == 0);
3243
    ac.size = xsave_area_size(rfbm, false);
3244
    assert(ac.size <= len);
3245
3246
    if (!valid_xrstor_header(&ac, &xstate_bv, 0)) {
3247
        return false;
3248
    }
3249
    do_xrstor(&ac, 0, rfbm, xstate_bv);
3250
    return true;
3251
}
3252
#endif
3253
3254
uint64_t HELPER(xgetbv)(CPUX86State *env, uint32_t ecx)
3255
0
{
3256
    /* The OS must have enabled XSAVE.  */
3257
0
    if (!(env->cr[4] & CR4_OSXSAVE_MASK)) {
3258
0
        raise_exception_ra(env, EXCP06_ILLOP, GETPC());
3259
0
    }
3260
3261
0
    switch (ecx) {
3262
0
    case 0:
3263
0
        return env->xcr0;
3264
0
    case 1:
3265
0
        if (env->features[FEAT_XSAVE] & CPUID_XSAVE_XGETBV1) {
3266
0
            return env->xcr0 & get_xinuse(env);
3267
0
        }
3268
0
        break;
3269
0
    }
3270
0
    raise_exception_ra(env, EXCP0D_GPF, GETPC());
3271
0
}
3272
3273
void HELPER(xsetbv)(CPUX86State *env, uint32_t ecx, uint64_t mask)
3274
0
{
3275
0
    uint32_t dummy, ena_lo, ena_hi;
3276
0
    uint64_t ena;
3277
3278
    /* The OS must have enabled XSAVE.  */
3279
0
    if (!(env->cr[4] & CR4_OSXSAVE_MASK)) {
3280
0
        raise_exception_ra(env, EXCP06_ILLOP, GETPC());
3281
0
    }
3282
3283
    /* Only XCR0 is defined at present; the FPU may not be disabled.  */
3284
0
    if (ecx != 0 || (mask & XSTATE_FP_MASK) == 0) {
3285
0
        goto do_gpf;
3286
0
    }
3287
3288
    /* SSE can be disabled, but only if AVX is disabled too.  */
3289
0
    if ((mask & (XSTATE_SSE_MASK | XSTATE_YMM_MASK)) == XSTATE_YMM_MASK) {
3290
0
        goto do_gpf;
3291
0
    }
3292
3293
    /* Disallow enabling unimplemented features.  */
3294
0
    cpu_x86_cpuid(env, 0x0d, 0, &ena_lo, &dummy, &dummy, &ena_hi);
3295
0
    ena = ((uint64_t)ena_hi << 32) | ena_lo;
3296
0
    if (mask & ~ena) {
3297
0
        goto do_gpf;
3298
0
    }
3299
3300
    /* Disallow enabling only half of MPX.  */
3301
0
    if ((mask ^ (mask * (XSTATE_BNDCSR_MASK / XSTATE_BNDREGS_MASK)))
3302
0
        & XSTATE_BNDCSR_MASK) {
3303
0
        goto do_gpf;
3304
0
    }
3305
3306
0
    env->xcr0 = mask;
3307
0
    cpu_sync_bndcs_hflags(env);
3308
0
    cpu_sync_avx_hflag(env);
3309
0
    cpu_sync_apx_hflag(env);
3310
0
    return;
3311
3312
0
 do_gpf:
3313
0
    raise_exception_ra(env, EXCP0D_GPF, GETPC());
3314
0
}
3315
3316
/* MMX/SSE */
3317
/* XXX: optimize by storing fptt and fptags in the static cpu state */
3318
3319
0
#define SSE_DAZ             0x0040
3320
0
#define SSE_RC_SHIFT        13
3321
0
#define SSE_RC_MASK         (3 << SSE_RC_SHIFT)
3322
0
#define SSE_FZ              0x8000
3323
3324
void update_mxcsr_status(CPUX86State *env)
3325
0
{
3326
0
    uint32_t mxcsr = env->mxcsr;
3327
0
    int rnd_type;
3328
3329
    /* set rounding mode */
3330
0
    rnd_type = (mxcsr & SSE_RC_MASK) >> SSE_RC_SHIFT;
3331
0
    set_x86_rounding_mode(rnd_type, &env->sse_status);
3332
3333
    /* Set exception flags.  */
3334
0
    set_float_exception_flags((mxcsr & FPUS_IE ? float_flag_invalid : 0) |
3335
0
                              (mxcsr & FPUS_DE ? float_flag_input_denormal_used : 0) |
3336
0
                              (mxcsr & FPUS_ZE ? float_flag_divbyzero : 0) |
3337
0
                              (mxcsr & FPUS_OE ? float_flag_overflow : 0) |
3338
0
                              (mxcsr & FPUS_UE ? float_flag_underflow : 0) |
3339
0
                              (mxcsr & FPUS_PE ? float_flag_inexact : 0),
3340
0
                              &env->sse_status);
3341
3342
    /* set denormals are zero */
3343
0
    set_flush_inputs_to_zero((mxcsr & SSE_DAZ) ? 1 : 0, &env->sse_status);
3344
3345
    /* set flush to zero */
3346
0
    set_flush_to_zero((mxcsr & SSE_FZ) ? 1 : 0, &env->sse_status);
3347
0
}
3348
3349
void update_mxcsr_from_sse_status(CPUX86State *env)
3350
0
{
3351
0
    int flags = get_float_exception_flags(&env->sse_status);
3352
0
    env->mxcsr |= ((flags & float_flag_invalid ? FPUS_IE : 0) |
3353
0
                   (flags & float_flag_input_denormal_used ? FPUS_DE : 0) |
3354
0
                   (flags & float_flag_divbyzero ? FPUS_ZE : 0) |
3355
0
                   (flags & float_flag_overflow ? FPUS_OE : 0) |
3356
0
                   (flags & float_flag_underflow ? FPUS_UE : 0) |
3357
0
                   (flags & float_flag_inexact ? FPUS_PE : 0) |
3358
0
                   (flags & float_flag_output_denormal_flushed ? FPUS_UE | FPUS_PE :
3359
0
                    0));
3360
0
}
3361
3362
void HELPER(update_mxcsr)(CPUX86State *env)
3363
0
{
3364
0
    update_mxcsr_from_sse_status(env);
3365
0
}
3366
3367
void HELPER(ldmxcsr)(CPUX86State *env, uint32_t val)
3368
0
{
3369
0
    cpu_set_mxcsr(env, val);
3370
0
}
3371
3372
void HELPER(enter_mmx)(CPUX86State *env)
3373
0
{
3374
0
    env->fpstt = 0;
3375
0
    *(uint32_t *)(env->fptags) = 0;
3376
0
    *(uint32_t *)(env->fptags + 4) = 0;
3377
0
}
3378
3379
void HELPER(emms)(CPUX86State *env)
3380
0
{
3381
    /* set to empty state */
3382
0
    *(uint32_t *)(env->fptags) = 0x01010101;
3383
0
    *(uint32_t *)(env->fptags + 4) = 0x01010101;
3384
0
}
3385
3386
0
#define SHIFT 0
3387
#include "ops_sse.h"
3388
3389
0
#define SHIFT 1
3390
#include "ops_sse.h"
3391
3392
0
#define SHIFT 2
3393
#include "ops_sse.h"