Statistics
| Branch: | Revision:

root / target-ppc / op_helper.c @ 056401ea

History | View | Annotate | Download (76.8 kB)

1
/*
2
 *  PowerPC emulation helpers for qemu.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
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 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, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
#include "exec.h"
21
#include "host-utils.h"
22

    
23
#include "helper_regs.h"
24
#include "op_helper.h"
25

    
26
#define MEMSUFFIX _raw
27
#include "op_helper.h"
28
#include "op_helper_mem.h"
29
#if !defined(CONFIG_USER_ONLY)
30
#define MEMSUFFIX _user
31
#include "op_helper.h"
32
#include "op_helper_mem.h"
33
#define MEMSUFFIX _kernel
34
#include "op_helper.h"
35
#include "op_helper_mem.h"
36
#if defined(TARGET_PPC64H)
37
#define MEMSUFFIX _hypv
38
#include "op_helper.h"
39
#include "op_helper_mem.h"
40
#endif
41
#endif
42

    
43
//#define DEBUG_OP
44
//#define DEBUG_EXCEPTIONS
45
//#define DEBUG_SOFTWARE_TLB
46

    
47
/*****************************************************************************/
48
/* Exceptions processing helpers */
49

    
50
void do_raise_exception_err (uint32_t exception, int error_code)
51
{
52
#if 0
53
    printf("Raise exception %3x code : %d\n", exception, error_code);
54
#endif
55
    env->exception_index = exception;
56
    env->error_code = error_code;
57
    cpu_loop_exit();
58
}
59

    
60
void do_raise_exception (uint32_t exception)
61
{
62
    do_raise_exception_err(exception, 0);
63
}
64

    
65
void cpu_dump_EA (target_ulong EA);
66
void do_print_mem_EA (target_ulong EA)
67
{
68
    cpu_dump_EA(EA);
69
}
70

    
71
/*****************************************************************************/
72
/* Registers load and stores */
73
void do_load_cr (void)
74
{
75
    T0 = (env->crf[0] << 28) |
76
        (env->crf[1] << 24) |
77
        (env->crf[2] << 20) |
78
        (env->crf[3] << 16) |
79
        (env->crf[4] << 12) |
80
        (env->crf[5] << 8) |
81
        (env->crf[6] << 4) |
82
        (env->crf[7] << 0);
83
}
84

    
85
void do_store_cr (uint32_t mask)
86
{
87
    int i, sh;
88

    
89
    for (i = 0, sh = 7; i < 8; i++, sh--) {
90
        if (mask & (1 << sh))
91
            env->crf[i] = (T0 >> (sh * 4)) & 0xFUL;
92
    }
93
}
94

    
95
#if defined(TARGET_PPC64)
96
void do_store_pri (int prio)
97
{
98
    env->spr[SPR_PPR] &= ~0x001C000000000000ULL;
99
    env->spr[SPR_PPR] |= ((uint64_t)prio & 0x7) << 50;
100
}
101
#endif
102

    
103
target_ulong ppc_load_dump_spr (int sprn)
104
{
105
    if (loglevel != 0) {
106
        fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
107
                sprn, sprn, env->spr[sprn]);
108
    }
109

    
110
    return env->spr[sprn];
111
}
112

    
113
void ppc_store_dump_spr (int sprn, target_ulong val)
114
{
115
    if (loglevel != 0) {
116
        fprintf(logfile, "Write SPR %d %03x => " ADDRX " <= " ADDRX "\n",
117
                sprn, sprn, env->spr[sprn], val);
118
    }
119
    env->spr[sprn] = val;
120
}
121

    
122
/*****************************************************************************/
123
/* Fixed point operations helpers */
124
void do_adde (void)
125
{
126
    T2 = T0;
127
    T0 += T1 + xer_ca;
128
    if (likely(!((uint32_t)T0 < (uint32_t)T2 ||
129
                 (xer_ca == 1 && (uint32_t)T0 == (uint32_t)T2)))) {
130
        xer_ca = 0;
131
    } else {
132
        xer_ca = 1;
133
    }
134
}
135

    
136
#if defined(TARGET_PPC64)
137
void do_adde_64 (void)
138
{
139
    T2 = T0;
140
    T0 += T1 + xer_ca;
141
    if (likely(!((uint64_t)T0 < (uint64_t)T2 ||
142
                 (xer_ca == 1 && (uint64_t)T0 == (uint64_t)T2)))) {
143
        xer_ca = 0;
144
    } else {
145
        xer_ca = 1;
146
    }
147
}
148
#endif
149

    
150
void do_addmeo (void)
151
{
152
    T1 = T0;
153
    T0 += xer_ca + (-1);
154
    if (likely(!((uint32_t)T1 &
155
                 ((uint32_t)T1 ^ (uint32_t)T0) & (1UL << 31)))) {
156
        xer_ov = 0;
157
    } else {
158
        xer_ov = 1;
159
        xer_so = 1;
160
    }
161
    if (likely(T1 != 0))
162
        xer_ca = 1;
163
}
164

    
165
#if defined(TARGET_PPC64)
166
void do_addmeo_64 (void)
167
{
168
    T1 = T0;
169
    T0 += xer_ca + (-1);
170
    if (likely(!((uint64_t)T1 &
171
                 ((uint64_t)T1 ^ (uint64_t)T0) & (1ULL << 63)))) {
172
        xer_ov = 0;
173
    } else {
174
        xer_ov = 1;
175
        xer_so = 1;
176
    }
177
    if (likely(T1 != 0))
178
        xer_ca = 1;
179
}
180
#endif
181

    
182
void do_divwo (void)
183
{
184
    if (likely(!(((int32_t)T0 == INT32_MIN && (int32_t)T1 == -1) ||
185
                 (int32_t)T1 == 0))) {
186
        xer_ov = 0;
187
        T0 = (int32_t)T0 / (int32_t)T1;
188
    } else {
189
        xer_ov = 1;
190
        xer_so = 1;
191
        T0 = (-1) * ((uint32_t)T0 >> 31);
192
    }
193
}
194

    
195
#if defined(TARGET_PPC64)
196
void do_divdo (void)
197
{
198
    if (likely(!(((int64_t)T0 == INT64_MIN && (int64_t)T1 == -1ULL) ||
199
                 (int64_t)T1 == 0))) {
200
        xer_ov = 0;
201
        T0 = (int64_t)T0 / (int64_t)T1;
202
    } else {
203
        xer_ov = 1;
204
        xer_so = 1;
205
        T0 = (-1ULL) * ((uint64_t)T0 >> 63);
206
    }
207
}
208
#endif
209

    
210
void do_divwuo (void)
211
{
212
    if (likely((uint32_t)T1 != 0)) {
213
        xer_ov = 0;
214
        T0 = (uint32_t)T0 / (uint32_t)T1;
215
    } else {
216
        xer_ov = 1;
217
        xer_so = 1;
218
        T0 = 0;
219
    }
220
}
221

    
222
#if defined(TARGET_PPC64)
223
void do_divduo (void)
224
{
225
    if (likely((uint64_t)T1 != 0)) {
226
        xer_ov = 0;
227
        T0 = (uint64_t)T0 / (uint64_t)T1;
228
    } else {
229
        xer_ov = 1;
230
        xer_so = 1;
231
        T0 = 0;
232
    }
233
}
234
#endif
235

    
236
void do_mullwo (void)
237
{
238
    int64_t res = (int64_t)T0 * (int64_t)T1;
239

    
240
    if (likely((int32_t)res == res)) {
241
        xer_ov = 0;
242
    } else {
243
        xer_ov = 1;
244
        xer_so = 1;
245
    }
246
    T0 = (int32_t)res;
247
}
248

    
249
#if defined(TARGET_PPC64)
250
void do_mulldo (void)
251
{
252
    int64_t th;
253
    uint64_t tl;
254

    
255
    muls64(&tl, &th, T0, T1);
256
    /* If th != 0 && th != -1, then we had an overflow */
257
    if (likely((th + 1) <= 1)) {
258
        xer_ov = 0;
259
    } else {
260
        xer_ov = 1;
261
        xer_so = 1;
262
    }
263
    T0 = (int64_t)tl;
264
}
265
#endif
266

    
267
void do_nego (void)
268
{
269
    if (likely((int32_t)T0 != INT32_MIN)) {
270
        xer_ov = 0;
271
        T0 = -(int32_t)T0;
272
    } else {
273
        xer_ov = 1;
274
        xer_so = 1;
275
    }
276
}
277

    
278
#if defined(TARGET_PPC64)
279
void do_nego_64 (void)
280
{
281
    if (likely((int64_t)T0 != INT64_MIN)) {
282
        xer_ov = 0;
283
        T0 = -(int64_t)T0;
284
    } else {
285
        xer_ov = 1;
286
        xer_so = 1;
287
    }
288
}
289
#endif
290

    
291
void do_subfe (void)
292
{
293
    T0 = T1 + ~T0 + xer_ca;
294
    if (likely((uint32_t)T0 >= (uint32_t)T1 &&
295
               (xer_ca == 0 || (uint32_t)T0 != (uint32_t)T1))) {
296
        xer_ca = 0;
297
    } else {
298
        xer_ca = 1;
299
    }
300
}
301

    
302
#if defined(TARGET_PPC64)
303
void do_subfe_64 (void)
304
{
305
    T0 = T1 + ~T0 + xer_ca;
306
    if (likely((uint64_t)T0 >= (uint64_t)T1 &&
307
               (xer_ca == 0 || (uint64_t)T0 != (uint64_t)T1))) {
308
        xer_ca = 0;
309
    } else {
310
        xer_ca = 1;
311
    }
312
}
313
#endif
314

    
315
void do_subfmeo (void)
316
{
317
    T1 = T0;
318
    T0 = ~T0 + xer_ca - 1;
319
    if (likely(!((uint32_t)~T1 & ((uint32_t)~T1 ^ (uint32_t)T0) &
320
                 (1UL << 31)))) {
321
        xer_ov = 0;
322
    } else {
323
        xer_ov = 1;
324
        xer_so = 1;
325
    }
326
    if (likely((uint32_t)T1 != UINT32_MAX))
327
        xer_ca = 1;
328
}
329

    
330
#if defined(TARGET_PPC64)
331
void do_subfmeo_64 (void)
332
{
333
    T1 = T0;
334
    T0 = ~T0 + xer_ca - 1;
335
    if (likely(!((uint64_t)~T1 & ((uint64_t)~T1 ^ (uint64_t)T0) &
336
                 (1ULL << 63)))) {
337
        xer_ov = 0;
338
    } else {
339
        xer_ov = 1;
340
        xer_so = 1;
341
    }
342
    if (likely((uint64_t)T1 != UINT64_MAX))
343
        xer_ca = 1;
344
}
345
#endif
346

    
347
void do_subfzeo (void)
348
{
349
    T1 = T0;
350
    T0 = ~T0 + xer_ca;
351
    if (likely(!(((uint32_t)~T1 ^ UINT32_MAX) &
352
                 ((uint32_t)(~T1) ^ (uint32_t)T0) & (1UL << 31)))) {
353
        xer_ov = 0;
354
    } else {
355
        xer_ov = 1;
356
        xer_so = 1;
357
    }
358
    if (likely((uint32_t)T0 >= (uint32_t)~T1)) {
359
        xer_ca = 0;
360
    } else {
361
        xer_ca = 1;
362
    }
363
}
364

    
365
#if defined(TARGET_PPC64)
366
void do_subfzeo_64 (void)
367
{
368
    T1 = T0;
369
    T0 = ~T0 + xer_ca;
370
    if (likely(!(((uint64_t)~T1 ^ UINT64_MAX) &
371
                 ((uint64_t)(~T1) ^ (uint64_t)T0) & (1ULL << 63)))) {
372
        xer_ov = 0;
373
    } else {
374
        xer_ov = 1;
375
        xer_so = 1;
376
    }
377
    if (likely((uint64_t)T0 >= (uint64_t)~T1)) {
378
        xer_ca = 0;
379
    } else {
380
        xer_ca = 1;
381
    }
382
}
383
#endif
384

    
385
void do_cntlzw (void)
386
{
387
    T0 = clz32(T0);
388
}
389

    
390
#if defined(TARGET_PPC64)
391
void do_cntlzd (void)
392
{
393
    T0 = clz64(T0);
394
}
395
#endif
396

    
397
/* shift right arithmetic helper */
398
void do_sraw (void)
399
{
400
    int32_t ret;
401

    
402
    if (likely(!(T1 & 0x20UL))) {
403
        if (likely((uint32_t)T1 != 0)) {
404
            ret = (int32_t)T0 >> (T1 & 0x1fUL);
405
            if (likely(ret >= 0 || ((int32_t)T0 & ((1 << T1) - 1)) == 0)) {
406
                xer_ca = 0;
407
            } else {
408
                xer_ca = 1;
409
            }
410
        } else {
411
            ret = T0;
412
            xer_ca = 0;
413
        }
414
    } else {
415
        ret = (-1) * ((uint32_t)T0 >> 31);
416
        if (likely(ret >= 0 || ((uint32_t)T0 & ~0x80000000UL) == 0)) {
417
            xer_ca = 0;
418
        } else {
419
            xer_ca = 1;
420
        }
421
    }
422
    T0 = ret;
423
}
424

    
425
#if defined(TARGET_PPC64)
426
void do_srad (void)
427
{
428
    int64_t ret;
429

    
430
    if (likely(!(T1 & 0x40UL))) {
431
        if (likely((uint64_t)T1 != 0)) {
432
            ret = (int64_t)T0 >> (T1 & 0x3FUL);
433
            if (likely(ret >= 0 || ((int64_t)T0 & ((1 << T1) - 1)) == 0)) {
434
                xer_ca = 0;
435
            } else {
436
                xer_ca = 1;
437
            }
438
        } else {
439
            ret = T0;
440
            xer_ca = 0;
441
        }
442
    } else {
443
        ret = (-1) * ((uint64_t)T0 >> 63);
444
        if (likely(ret >= 0 || ((uint64_t)T0 & ~0x8000000000000000ULL) == 0)) {
445
            xer_ca = 0;
446
        } else {
447
            xer_ca = 1;
448
        }
449
    }
450
    T0 = ret;
451
}
452
#endif
453

    
454
void do_popcntb (void)
455
{
456
    uint32_t ret;
457
    int i;
458

    
459
    ret = 0;
460
    for (i = 0; i < 32; i += 8)
461
        ret |= ctpop8((T0 >> i) & 0xFF) << i;
462
    T0 = ret;
463
}
464

    
465
#if defined(TARGET_PPC64)
466
void do_popcntb_64 (void)
467
{
468
    uint64_t ret;
469
    int i;
470

    
471
    ret = 0;
472
    for (i = 0; i < 64; i += 8)
473
        ret |= ctpop8((T0 >> i) & 0xFF) << i;
474
    T0 = ret;
475
}
476
#endif
477

    
478
/*****************************************************************************/
479
/* Floating point operations helpers */
480
static always_inline int fpisneg (float64 f)
481
{
482
    union {
483
        float64 f;
484
        uint64_t u;
485
    } u;
486

    
487
    u.f = f;
488

    
489
    return u.u >> 63 != 0;
490
}
491

    
492
static always_inline int isden (float f)
493
{
494
    union {
495
        float64 f;
496
        uint64_t u;
497
    } u;
498

    
499
    u.f = f;
500

    
501
    return ((u.u >> 52) & 0x7FF) == 0;
502
}
503

    
504
static always_inline int iszero (float64 f)
505
{
506
    union {
507
        float64 f;
508
        uint64_t u;
509
    } u;
510

    
511
    u.f = f;
512

    
513
    return (u.u & ~0x8000000000000000ULL) == 0;
514
}
515

    
516
static always_inline int isinfinity (float64 f)
517
{
518
    union {
519
        float64 f;
520
        uint64_t u;
521
    } u;
522

    
523
    u.f = f;
524

    
525
    return ((u.u >> 52) & 0x7FF) == 0x7FF &&
526
        (u.u & 0x000FFFFFFFFFFFFFULL) == 0;
527
}
528

    
529
void do_compute_fprf (int set_fprf)
530
{
531
    int isneg;
532

    
533
    isneg = fpisneg(FT0);
534
    if (unlikely(float64_is_nan(FT0))) {
535
        if (float64_is_signaling_nan(FT0)) {
536
            /* Signaling NaN: flags are undefined */
537
            T0 = 0x00;
538
        } else {
539
            /* Quiet NaN */
540
            T0 = 0x11;
541
        }
542
    } else if (unlikely(isinfinity(FT0))) {
543
        /* +/- infinity */
544
        if (isneg)
545
            T0 = 0x09;
546
        else
547
            T0 = 0x05;
548
    } else {
549
        if (iszero(FT0)) {
550
            /* +/- zero */
551
            if (isneg)
552
                T0 = 0x12;
553
            else
554
                T0 = 0x02;
555
        } else {
556
            if (isden(FT0)) {
557
                /* Denormalized numbers */
558
                T0 = 0x10;
559
            } else {
560
                /* Normalized numbers */
561
                T0 = 0x00;
562
            }
563
            if (isneg) {
564
                T0 |= 0x08;
565
            } else {
566
                T0 |= 0x04;
567
            }
568
        }
569
    }
570
    if (set_fprf) {
571
        /* We update FPSCR_FPRF */
572
        env->fpscr &= ~(0x1F << FPSCR_FPRF);
573
        env->fpscr |= T0 << FPSCR_FPRF;
574
    }
575
    /* We just need fpcc to update Rc1 */
576
    T0 &= 0xF;
577
}
578

    
579
/* Floating-point invalid operations exception */
580
static always_inline void fload_invalid_op_excp (int op)
581
{
582
    int ve;
583

    
584
    ve = fpscr_ve;
585
    if (op & POWERPC_EXCP_FP_VXSNAN) {
586
        /* Operation on signaling NaN */
587
        env->fpscr |= 1 << FPSCR_VXSNAN;
588
    }
589
    if (op & POWERPC_EXCP_FP_VXSOFT) {
590
        /* Software-defined condition */
591
        env->fpscr |= 1 << FPSCR_VXSOFT;
592
    }
593
    switch (op & ~(POWERPC_EXCP_FP_VXSOFT | POWERPC_EXCP_FP_VXSNAN)) {
594
    case POWERPC_EXCP_FP_VXISI:
595
        /* Magnitude subtraction of infinities */
596
        env->fpscr |= 1 << FPSCR_VXISI;
597
        goto update_arith;
598
    case POWERPC_EXCP_FP_VXIDI:
599
        /* Division of infinity by infinity */
600
        env->fpscr |= 1 << FPSCR_VXIDI;
601
        goto update_arith;
602
    case POWERPC_EXCP_FP_VXZDZ:
603
        /* Division of zero by zero */
604
        env->fpscr |= 1 << FPSCR_VXZDZ;
605
        goto update_arith;
606
    case POWERPC_EXCP_FP_VXIMZ:
607
        /* Multiplication of zero by infinity */
608
        env->fpscr |= 1 << FPSCR_VXIMZ;
609
        goto update_arith;
610
    case POWERPC_EXCP_FP_VXVC:
611
        /* Ordered comparison of NaN */
612
        env->fpscr |= 1 << FPSCR_VXVC;
613
        env->fpscr &= ~(0xF << FPSCR_FPCC);
614
        env->fpscr |= 0x11 << FPSCR_FPCC;
615
        /* We must update the target FPR before raising the exception */
616
        if (ve != 0) {
617
            env->exception_index = POWERPC_EXCP_PROGRAM;
618
            env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
619
            /* Update the floating-point enabled exception summary */
620
            env->fpscr |= 1 << FPSCR_FEX;
621
            /* Exception is differed */
622
            ve = 0;
623
        }
624
        break;
625
    case POWERPC_EXCP_FP_VXSQRT:
626
        /* Square root of a negative number */
627
        env->fpscr |= 1 << FPSCR_VXSQRT;
628
    update_arith:
629
        env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
630
        if (ve == 0) {
631
            /* Set the result to quiet NaN */
632
            FT0 = (uint64_t)-1;
633
            env->fpscr &= ~(0xF << FPSCR_FPCC);
634
            env->fpscr |= 0x11 << FPSCR_FPCC;
635
        }
636
        break;
637
    case POWERPC_EXCP_FP_VXCVI:
638
        /* Invalid conversion */
639
        env->fpscr |= 1 << FPSCR_VXCVI;
640
        env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
641
        if (ve == 0) {
642
            /* Set the result to quiet NaN */
643
            FT0 = (uint64_t)-1;
644
            env->fpscr &= ~(0xF << FPSCR_FPCC);
645
            env->fpscr |= 0x11 << FPSCR_FPCC;
646
        }
647
        break;
648
    }
649
    /* Update the floating-point invalid operation summary */
650
    env->fpscr |= 1 << FPSCR_VX;
651
    /* Update the floating-point exception summary */
652
    env->fpscr |= 1 << FPSCR_FX;
653
    if (ve != 0) {
654
        /* Update the floating-point enabled exception summary */
655
        env->fpscr |= 1 << FPSCR_FEX;
656
        if (msr_fe0 != 0 || msr_fe1 != 0)
657
            do_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
658
    }
659
}
660

    
661
static always_inline void float_zero_divide_excp (void)
662
{
663
    union {
664
        float64 f;
665
        uint64_t u;
666
    } u0, u1;
667

    
668
    env->fpscr |= 1 << FPSCR_ZX;
669
    env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
670
    /* Update the floating-point exception summary */
671
    env->fpscr |= 1 << FPSCR_FX;
672
    if (fpscr_ze != 0) {
673
        /* Update the floating-point enabled exception summary */
674
        env->fpscr |= 1 << FPSCR_FEX;
675
        if (msr_fe0 != 0 || msr_fe1 != 0) {
676
            do_raise_exception_err(POWERPC_EXCP_PROGRAM,
677
                                   POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
678
        }
679
    } else {
680
        /* Set the result to infinity */
681
        u0.f = FT0;
682
        u1.f = FT1;
683
        u0.u = ((u0.u ^ u1.u) & 0x8000000000000000ULL);
684
        u0.u |= 0x7FFULL << 52;
685
        FT0 = u0.f;
686
    }
687
}
688

    
689
static always_inline void float_overflow_excp (void)
690
{
691
    env->fpscr |= 1 << FPSCR_OX;
692
    /* Update the floating-point exception summary */
693
    env->fpscr |= 1 << FPSCR_FX;
694
    if (fpscr_oe != 0) {
695
        /* XXX: should adjust the result */
696
        /* Update the floating-point enabled exception summary */
697
        env->fpscr |= 1 << FPSCR_FEX;
698
        /* We must update the target FPR before raising the exception */
699
        env->exception_index = POWERPC_EXCP_PROGRAM;
700
        env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
701
    } else {
702
        env->fpscr |= 1 << FPSCR_XX;
703
        env->fpscr |= 1 << FPSCR_FI;
704
    }
705
}
706

    
707
static always_inline void float_underflow_excp (void)
708
{
709
    env->fpscr |= 1 << FPSCR_UX;
710
    /* Update the floating-point exception summary */
711
    env->fpscr |= 1 << FPSCR_FX;
712
    if (fpscr_ue != 0) {
713
        /* XXX: should adjust the result */
714
        /* Update the floating-point enabled exception summary */
715
        env->fpscr |= 1 << FPSCR_FEX;
716
        /* We must update the target FPR before raising the exception */
717
        env->exception_index = POWERPC_EXCP_PROGRAM;
718
        env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
719
    }
720
}
721

    
722
static always_inline void float_inexact_excp (void)
723
{
724
    env->fpscr |= 1 << FPSCR_XX;
725
    /* Update the floating-point exception summary */
726
    env->fpscr |= 1 << FPSCR_FX;
727
    if (fpscr_xe != 0) {
728
        /* Update the floating-point enabled exception summary */
729
        env->fpscr |= 1 << FPSCR_FEX;
730
        /* We must update the target FPR before raising the exception */
731
        env->exception_index = POWERPC_EXCP_PROGRAM;
732
        env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
733
    }
734
}
735

    
736
static always_inline void fpscr_set_rounding_mode (void)
737
{
738
    int rnd_type;
739

    
740
    /* Set rounding mode */
741
    switch (fpscr_rn) {
742
    case 0:
743
        /* Best approximation (round to nearest) */
744
        rnd_type = float_round_nearest_even;
745
        break;
746
    case 1:
747
        /* Smaller magnitude (round toward zero) */
748
        rnd_type = float_round_to_zero;
749
        break;
750
    case 2:
751
        /* Round toward +infinite */
752
        rnd_type = float_round_up;
753
        break;
754
    default:
755
    case 3:
756
        /* Round toward -infinite */
757
        rnd_type = float_round_down;
758
        break;
759
    }
760
    set_float_rounding_mode(rnd_type, &env->fp_status);
761
}
762

    
763
void do_fpscr_setbit (int bit)
764
{
765
    int prev;
766

    
767
    prev = (env->fpscr >> bit) & 1;
768
    env->fpscr |= 1 << bit;
769
    if (prev == 0) {
770
        switch (bit) {
771
        case FPSCR_VX:
772
            env->fpscr |= 1 << FPSCR_FX;
773
            if (fpscr_ve)
774
                goto raise_ve;
775
        case FPSCR_OX:
776
            env->fpscr |= 1 << FPSCR_FX;
777
            if (fpscr_oe)
778
                goto raise_oe;
779
            break;
780
        case FPSCR_UX:
781
            env->fpscr |= 1 << FPSCR_FX;
782
            if (fpscr_ue)
783
                goto raise_ue;
784
            break;
785
        case FPSCR_ZX:
786
            env->fpscr |= 1 << FPSCR_FX;
787
            if (fpscr_ze)
788
                goto raise_ze;
789
            break;
790
        case FPSCR_XX:
791
            env->fpscr |= 1 << FPSCR_FX;
792
            if (fpscr_xe)
793
                goto raise_xe;
794
            break;
795
        case FPSCR_VXSNAN:
796
        case FPSCR_VXISI:
797
        case FPSCR_VXIDI:
798
        case FPSCR_VXZDZ:
799
        case FPSCR_VXIMZ:
800
        case FPSCR_VXVC:
801
        case FPSCR_VXSOFT:
802
        case FPSCR_VXSQRT:
803
        case FPSCR_VXCVI:
804
            env->fpscr |= 1 << FPSCR_VX;
805
            env->fpscr |= 1 << FPSCR_FX;
806
            if (fpscr_ve != 0)
807
                goto raise_ve;
808
            break;
809
        case FPSCR_VE:
810
            if (fpscr_vx != 0) {
811
            raise_ve:
812
                env->error_code = POWERPC_EXCP_FP;
813
                if (fpscr_vxsnan)
814
                    env->error_code |= POWERPC_EXCP_FP_VXSNAN;
815
                if (fpscr_vxisi)
816
                    env->error_code |= POWERPC_EXCP_FP_VXISI;
817
                if (fpscr_vxidi)
818
                    env->error_code |= POWERPC_EXCP_FP_VXIDI;
819
                if (fpscr_vxzdz)
820
                    env->error_code |= POWERPC_EXCP_FP_VXZDZ;
821
                if (fpscr_vximz)
822
                    env->error_code |= POWERPC_EXCP_FP_VXIMZ;
823
                if (fpscr_vxvc)
824
                    env->error_code |= POWERPC_EXCP_FP_VXVC;
825
                if (fpscr_vxsoft)
826
                    env->error_code |= POWERPC_EXCP_FP_VXSOFT;
827
                if (fpscr_vxsqrt)
828
                    env->error_code |= POWERPC_EXCP_FP_VXSQRT;
829
                if (fpscr_vxcvi)
830
                    env->error_code |= POWERPC_EXCP_FP_VXCVI;
831
                goto raise_excp;
832
            }
833
            break;
834
        case FPSCR_OE:
835
            if (fpscr_ox != 0) {
836
            raise_oe:
837
                env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
838
                goto raise_excp;
839
            }
840
            break;
841
        case FPSCR_UE:
842
            if (fpscr_ux != 0) {
843
            raise_ue:
844
                env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
845
                goto raise_excp;
846
            }
847
            break;
848
        case FPSCR_ZE:
849
            if (fpscr_zx != 0) {
850
            raise_ze:
851
                env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
852
                goto raise_excp;
853
            }
854
            break;
855
        case FPSCR_XE:
856
            if (fpscr_xx != 0) {
857
            raise_xe:
858
                env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
859
                goto raise_excp;
860
            }
861
            break;
862
        case FPSCR_RN1:
863
        case FPSCR_RN:
864
            fpscr_set_rounding_mode();
865
            break;
866
        default:
867
            break;
868
        raise_excp:
869
            /* Update the floating-point enabled exception summary */
870
            env->fpscr |= 1 << FPSCR_FEX;
871
                /* We have to update Rc1 before raising the exception */
872
            env->exception_index = POWERPC_EXCP_PROGRAM;
873
            break;
874
        }
875
    }
876
}
877

    
878
#if defined(WORDS_BIGENDIAN)
879
#define WORD0 0
880
#define WORD1 1
881
#else
882
#define WORD0 1
883
#define WORD1 0
884
#endif
885
void do_store_fpscr (uint32_t mask)
886
{
887
    /*
888
     * We use only the 32 LSB of the incoming fpr
889
     */
890
    union {
891
        double d;
892
        struct {
893
            uint32_t u[2];
894
        } s;
895
    } u;
896
    uint32_t prev, new;
897
    int i;
898

    
899
    u.d = FT0;
900
    prev = env->fpscr;
901
    new = u.s.u[WORD1];
902
    new &= ~0x90000000;
903
    new |= prev & 0x90000000;
904
    for (i = 0; i < 7; i++) {
905
        if (mask & (1 << i)) {
906
            env->fpscr &= ~(0xF << (4 * i));
907
            env->fpscr |= new & (0xF << (4 * i));
908
        }
909
    }
910
    /* Update VX and FEX */
911
    if (fpscr_ix != 0)
912
        env->fpscr |= 1 << FPSCR_VX;
913
    if ((fpscr_ex & fpscr_eex) != 0) {
914
        env->fpscr |= 1 << FPSCR_FEX;
915
        env->exception_index = POWERPC_EXCP_PROGRAM;
916
        /* XXX: we should compute it properly */
917
        env->error_code = POWERPC_EXCP_FP;
918
    }
919
    fpscr_set_rounding_mode();
920
}
921
#undef WORD0
922
#undef WORD1
923

    
924
#ifdef CONFIG_SOFTFLOAT
925
void do_float_check_status (void)
926
{
927
    if (env->exception_index == POWERPC_EXCP_PROGRAM &&
928
        (env->error_code & POWERPC_EXCP_FP)) {
929
        /* Differred floating-point exception after target FPR update */
930
        if (msr_fe0 != 0 || msr_fe1 != 0)
931
            do_raise_exception_err(env->exception_index, env->error_code);
932
    } else if (env->fp_status.float_exception_flags & float_flag_overflow) {
933
        float_overflow_excp();
934
    } else if (env->fp_status.float_exception_flags & float_flag_underflow) {
935
        float_underflow_excp();
936
    } else if (env->fp_status.float_exception_flags & float_flag_inexact) {
937
        float_inexact_excp();
938
    }
939
}
940
#endif
941

    
942
#if USE_PRECISE_EMULATION
943
void do_fadd (void)
944
{
945
    if (unlikely(float64_is_signaling_nan(FT0) ||
946
                 float64_is_signaling_nan(FT1))) {
947
        /* sNaN addition */
948
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
949
    } else if (likely(isfinite(FT0) || isfinite(FT1) ||
950
                      fpisneg(FT0) == fpisneg(FT1))) {
951
        FT0 = float64_add(FT0, FT1, &env->fp_status);
952
    } else {
953
        /* Magnitude subtraction of infinities */
954
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
955
    }
956
}
957

    
958
void do_fsub (void)
959
{
960
    if (unlikely(float64_is_signaling_nan(FT0) ||
961
                 float64_is_signaling_nan(FT1))) {
962
        /* sNaN subtraction */
963
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
964
    } else if (likely(isfinite(FT0) || isfinite(FT1) ||
965
                      fpisneg(FT0) != fpisneg(FT1))) {
966
        FT0 = float64_sub(FT0, FT1, &env->fp_status);
967
    } else {
968
        /* Magnitude subtraction of infinities */
969
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
970
    }
971
}
972

    
973
void do_fmul (void)
974
{
975
    if (unlikely(float64_is_signaling_nan(FT0) ||
976
                 float64_is_signaling_nan(FT1))) {
977
        /* sNaN multiplication */
978
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
979
    } else if (unlikely((isinfinity(FT0) && iszero(FT1)) ||
980
                        (iszero(FT0) && isinfinity(FT1)))) {
981
        /* Multiplication of zero by infinity */
982
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
983
    } else {
984
        FT0 = float64_mul(FT0, FT1, &env->fp_status);
985
    }
986
}
987

    
988
void do_fdiv (void)
989
{
990
    if (unlikely(float64_is_signaling_nan(FT0) ||
991
                 float64_is_signaling_nan(FT1))) {
992
        /* sNaN division */
993
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
994
    } else if (unlikely(isinfinity(FT0) && isinfinity(FT1))) {
995
        /* Division of infinity by infinity */
996
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
997
    } else if (unlikely(iszero(FT1))) {
998
        if (iszero(FT0)) {
999
            /* Division of zero by zero */
1000
            fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
1001
        } else {
1002
            /* Division by zero */
1003
            float_zero_divide_excp();
1004
        }
1005
    } else {
1006
        FT0 = float64_div(FT0, FT1, &env->fp_status);
1007
    }
1008
}
1009
#endif /* USE_PRECISE_EMULATION */
1010

    
1011
void do_fctiw (void)
1012
{
1013
    union {
1014
        double d;
1015
        uint64_t i;
1016
    } p;
1017

    
1018
    if (unlikely(float64_is_signaling_nan(FT0))) {
1019
        /* sNaN conversion */
1020
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1021
    } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
1022
        /* qNan / infinity conversion */
1023
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1024
    } else {
1025
        p.i = float64_to_int32(FT0, &env->fp_status);
1026
#if USE_PRECISE_EMULATION
1027
        /* XXX: higher bits are not supposed to be significant.
1028
         *     to make tests easier, return the same as a real PowerPC 750
1029
         */
1030
        p.i |= 0xFFF80000ULL << 32;
1031
#endif
1032
        FT0 = p.d;
1033
    }
1034
}
1035

    
1036
void do_fctiwz (void)
1037
{
1038
    union {
1039
        double d;
1040
        uint64_t i;
1041
    } p;
1042

    
1043
    if (unlikely(float64_is_signaling_nan(FT0))) {
1044
        /* sNaN conversion */
1045
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1046
    } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
1047
        /* qNan / infinity conversion */
1048
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1049
    } else {
1050
        p.i = float64_to_int32_round_to_zero(FT0, &env->fp_status);
1051
#if USE_PRECISE_EMULATION
1052
        /* XXX: higher bits are not supposed to be significant.
1053
         *     to make tests easier, return the same as a real PowerPC 750
1054
         */
1055
        p.i |= 0xFFF80000ULL << 32;
1056
#endif
1057
        FT0 = p.d;
1058
    }
1059
}
1060

    
1061
#if defined(TARGET_PPC64)
1062
void do_fcfid (void)
1063
{
1064
    union {
1065
        double d;
1066
        uint64_t i;
1067
    } p;
1068

    
1069
    p.d = FT0;
1070
    FT0 = int64_to_float64(p.i, &env->fp_status);
1071
}
1072

    
1073
void do_fctid (void)
1074
{
1075
    union {
1076
        double d;
1077
        uint64_t i;
1078
    } p;
1079

    
1080
    if (unlikely(float64_is_signaling_nan(FT0))) {
1081
        /* sNaN conversion */
1082
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1083
    } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
1084
        /* qNan / infinity conversion */
1085
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1086
    } else {
1087
        p.i = float64_to_int64(FT0, &env->fp_status);
1088
        FT0 = p.d;
1089
    }
1090
}
1091

    
1092
void do_fctidz (void)
1093
{
1094
    union {
1095
        double d;
1096
        uint64_t i;
1097
    } p;
1098

    
1099
    if (unlikely(float64_is_signaling_nan(FT0))) {
1100
        /* sNaN conversion */
1101
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1102
    } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
1103
        /* qNan / infinity conversion */
1104
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1105
    } else {
1106
        p.i = float64_to_int64_round_to_zero(FT0, &env->fp_status);
1107
        FT0 = p.d;
1108
    }
1109
}
1110

    
1111
#endif
1112

    
1113
static always_inline void do_fri (int rounding_mode)
1114
{
1115
    if (unlikely(float64_is_signaling_nan(FT0))) {
1116
        /* sNaN round */
1117
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1118
    } else if (unlikely(float64_is_nan(FT0) || isinfinity(FT0))) {
1119
        /* qNan / infinity round */
1120
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1121
    } else {
1122
        set_float_rounding_mode(rounding_mode, &env->fp_status);
1123
        FT0 = float64_round_to_int(FT0, &env->fp_status);
1124
        /* Restore rounding mode from FPSCR */
1125
        fpscr_set_rounding_mode();
1126
    }
1127
}
1128

    
1129
void do_frin (void)
1130
{
1131
    do_fri(float_round_nearest_even);
1132
}
1133

    
1134
void do_friz (void)
1135
{
1136
    do_fri(float_round_to_zero);
1137
}
1138

    
1139
void do_frip (void)
1140
{
1141
    do_fri(float_round_up);
1142
}
1143

    
1144
void do_frim (void)
1145
{
1146
    do_fri(float_round_down);
1147
}
1148

    
1149
#if USE_PRECISE_EMULATION
1150
void do_fmadd (void)
1151
{
1152
    if (unlikely(float64_is_signaling_nan(FT0) ||
1153
                 float64_is_signaling_nan(FT1) ||
1154
                 float64_is_signaling_nan(FT2))) {
1155
        /* sNaN operation */
1156
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1157
    } else {
1158
#ifdef FLOAT128
1159
        /* This is the way the PowerPC specification defines it */
1160
        float128 ft0_128, ft1_128;
1161

    
1162
        ft0_128 = float64_to_float128(FT0, &env->fp_status);
1163
        ft1_128 = float64_to_float128(FT1, &env->fp_status);
1164
        ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1165
        ft1_128 = float64_to_float128(FT2, &env->fp_status);
1166
        ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1167
        FT0 = float128_to_float64(ft0_128, &env->fp_status);
1168
#else
1169
        /* This is OK on x86 hosts */
1170
        FT0 = (FT0 * FT1) + FT2;
1171
#endif
1172
    }
1173
}
1174

    
1175
void do_fmsub (void)
1176
{
1177
    if (unlikely(float64_is_signaling_nan(FT0) ||
1178
                 float64_is_signaling_nan(FT1) ||
1179
                 float64_is_signaling_nan(FT2))) {
1180
        /* sNaN operation */
1181
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1182
    } else {
1183
#ifdef FLOAT128
1184
        /* This is the way the PowerPC specification defines it */
1185
        float128 ft0_128, ft1_128;
1186

    
1187
        ft0_128 = float64_to_float128(FT0, &env->fp_status);
1188
        ft1_128 = float64_to_float128(FT1, &env->fp_status);
1189
        ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1190
        ft1_128 = float64_to_float128(FT2, &env->fp_status);
1191
        ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1192
        FT0 = float128_to_float64(ft0_128, &env->fp_status);
1193
#else
1194
        /* This is OK on x86 hosts */
1195
        FT0 = (FT0 * FT1) - FT2;
1196
#endif
1197
    }
1198
}
1199
#endif /* USE_PRECISE_EMULATION */
1200

    
1201
void do_fnmadd (void)
1202
{
1203
    if (unlikely(float64_is_signaling_nan(FT0) ||
1204
                 float64_is_signaling_nan(FT1) ||
1205
                 float64_is_signaling_nan(FT2))) {
1206
        /* sNaN operation */
1207
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1208
    } else {
1209
#if USE_PRECISE_EMULATION
1210
#ifdef FLOAT128
1211
        /* This is the way the PowerPC specification defines it */
1212
        float128 ft0_128, ft1_128;
1213

    
1214
        ft0_128 = float64_to_float128(FT0, &env->fp_status);
1215
        ft1_128 = float64_to_float128(FT1, &env->fp_status);
1216
        ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1217
        ft1_128 = float64_to_float128(FT2, &env->fp_status);
1218
        ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1219
        FT0 = float128_to_float64(ft0_128, &env->fp_status);
1220
#else
1221
        /* This is OK on x86 hosts */
1222
        FT0 = (FT0 * FT1) + FT2;
1223
#endif
1224
#else
1225
        FT0 = float64_mul(FT0, FT1, &env->fp_status);
1226
        FT0 = float64_add(FT0, FT2, &env->fp_status);
1227
#endif
1228
        if (likely(!isnan(FT0)))
1229
            FT0 = float64_chs(FT0);
1230
    }
1231
}
1232

    
1233
void do_fnmsub (void)
1234
{
1235
    if (unlikely(float64_is_signaling_nan(FT0) ||
1236
                 float64_is_signaling_nan(FT1) ||
1237
                 float64_is_signaling_nan(FT2))) {
1238
        /* sNaN operation */
1239
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1240
    } else {
1241
#if USE_PRECISE_EMULATION
1242
#ifdef FLOAT128
1243
        /* This is the way the PowerPC specification defines it */
1244
        float128 ft0_128, ft1_128;
1245

    
1246
        ft0_128 = float64_to_float128(FT0, &env->fp_status);
1247
        ft1_128 = float64_to_float128(FT1, &env->fp_status);
1248
        ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1249
        ft1_128 = float64_to_float128(FT2, &env->fp_status);
1250
        ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1251
        FT0 = float128_to_float64(ft0_128, &env->fp_status);
1252
#else
1253
        /* This is OK on x86 hosts */
1254
        FT0 = (FT0 * FT1) - FT2;
1255
#endif
1256
#else
1257
        FT0 = float64_mul(FT0, FT1, &env->fp_status);
1258
        FT0 = float64_sub(FT0, FT2, &env->fp_status);
1259
#endif
1260
        if (likely(!isnan(FT0)))
1261
            FT0 = float64_chs(FT0);
1262
    }
1263
}
1264

    
1265
#if USE_PRECISE_EMULATION
1266
void do_frsp (void)
1267
{
1268
    if (unlikely(float64_is_signaling_nan(FT0))) {
1269
        /* sNaN square root */
1270
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1271
    } else {
1272
        FT0 = float64_to_float32(FT0, &env->fp_status);
1273
    }
1274
}
1275
#endif /* USE_PRECISE_EMULATION */
1276

    
1277
void do_fsqrt (void)
1278
{
1279
    if (unlikely(float64_is_signaling_nan(FT0))) {
1280
        /* sNaN square root */
1281
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1282
    } else if (unlikely(fpisneg(FT0) && !iszero(FT0))) {
1283
        /* Square root of a negative nonzero number */
1284
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1285
    } else {
1286
        FT0 = float64_sqrt(FT0, &env->fp_status);
1287
    }
1288
}
1289

    
1290
void do_fre (void)
1291
{
1292
    union {
1293
        double d;
1294
        uint64_t i;
1295
    } p;
1296

    
1297
    if (unlikely(float64_is_signaling_nan(FT0))) {
1298
        /* sNaN reciprocal */
1299
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1300
    } else if (unlikely(iszero(FT0))) {
1301
        /* Zero reciprocal */
1302
        float_zero_divide_excp();
1303
    } else if (likely(isnormal(FT0))) {
1304
        FT0 = float64_div(1.0, FT0, &env->fp_status);
1305
    } else {
1306
        p.d = FT0;
1307
        if (p.i == 0x8000000000000000ULL) {
1308
            p.i = 0xFFF0000000000000ULL;
1309
        } else if (p.i == 0x0000000000000000ULL) {
1310
            p.i = 0x7FF0000000000000ULL;
1311
        } else if (isnan(FT0)) {
1312
            p.i = 0x7FF8000000000000ULL;
1313
        } else if (fpisneg(FT0)) {
1314
            p.i = 0x8000000000000000ULL;
1315
        } else {
1316
            p.i = 0x0000000000000000ULL;
1317
        }
1318
        FT0 = p.d;
1319
    }
1320
}
1321

    
1322
void do_fres (void)
1323
{
1324
    union {
1325
        double d;
1326
        uint64_t i;
1327
    } p;
1328

    
1329
    if (unlikely(float64_is_signaling_nan(FT0))) {
1330
        /* sNaN reciprocal */
1331
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1332
    } else if (unlikely(iszero(FT0))) {
1333
        /* Zero reciprocal */
1334
        float_zero_divide_excp();
1335
    } else if (likely(isnormal(FT0))) {
1336
#if USE_PRECISE_EMULATION
1337
        FT0 = float64_div(1.0, FT0, &env->fp_status);
1338
        FT0 = float64_to_float32(FT0, &env->fp_status);
1339
#else
1340
        FT0 = float32_div(1.0, FT0, &env->fp_status);
1341
#endif
1342
    } else {
1343
        p.d = FT0;
1344
        if (p.i == 0x8000000000000000ULL) {
1345
            p.i = 0xFFF0000000000000ULL;
1346
        } else if (p.i == 0x0000000000000000ULL) {
1347
            p.i = 0x7FF0000000000000ULL;
1348
        } else if (isnan(FT0)) {
1349
            p.i = 0x7FF8000000000000ULL;
1350
        } else if (fpisneg(FT0)) {
1351
            p.i = 0x8000000000000000ULL;
1352
        } else {
1353
            p.i = 0x0000000000000000ULL;
1354
        }
1355
        FT0 = p.d;
1356
    }
1357
}
1358

    
1359
void do_frsqrte (void)
1360
{
1361
    union {
1362
        double d;
1363
        uint64_t i;
1364
    } p;
1365

    
1366
    if (unlikely(float64_is_signaling_nan(FT0))) {
1367
        /* sNaN reciprocal square root */
1368
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1369
    } else if (unlikely(fpisneg(FT0) && !iszero(FT0))) {
1370
        /* Reciprocal square root of a negative nonzero number */
1371
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1372
    } else if (likely(isnormal(FT0))) {
1373
        FT0 = float64_sqrt(FT0, &env->fp_status);
1374
        FT0 = float32_div(1.0, FT0, &env->fp_status);
1375
    } else {
1376
        p.d = FT0;
1377
        if (p.i == 0x8000000000000000ULL) {
1378
            p.i = 0xFFF0000000000000ULL;
1379
        } else if (p.i == 0x0000000000000000ULL) {
1380
            p.i = 0x7FF0000000000000ULL;
1381
        } else if (isnan(FT0)) {
1382
            p.i |= 0x000FFFFFFFFFFFFFULL;
1383
        } else if (fpisneg(FT0)) {
1384
            p.i = 0x7FF8000000000000ULL;
1385
        } else {
1386
            p.i = 0x0000000000000000ULL;
1387
        }
1388
        FT0 = p.d;
1389
    }
1390
}
1391

    
1392
void do_fsel (void)
1393
{
1394
    if (!fpisneg(FT0) || iszero(FT0))
1395
        FT0 = FT1;
1396
    else
1397
        FT0 = FT2;
1398
}
1399

    
1400
void do_fcmpu (void)
1401
{
1402
    if (unlikely(float64_is_signaling_nan(FT0) ||
1403
                 float64_is_signaling_nan(FT1))) {
1404
        /* sNaN comparison */
1405
        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1406
    } else {
1407
        if (float64_lt(FT0, FT1, &env->fp_status)) {
1408
            T0 = 0x08UL;
1409
        } else if (!float64_le(FT0, FT1, &env->fp_status)) {
1410
            T0 = 0x04UL;
1411
        } else {
1412
            T0 = 0x02UL;
1413
        }
1414
    }
1415
    env->fpscr &= ~(0x0F << FPSCR_FPRF);
1416
    env->fpscr |= T0 << FPSCR_FPRF;
1417
}
1418

    
1419
void do_fcmpo (void)
1420
{
1421
    if (unlikely(float64_is_nan(FT0) ||
1422
                 float64_is_nan(FT1))) {
1423
        if (float64_is_signaling_nan(FT0) ||
1424
            float64_is_signaling_nan(FT1)) {
1425
            /* sNaN comparison */
1426
            fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1427
                                  POWERPC_EXCP_FP_VXVC);
1428
        } else {
1429
            /* qNaN comparison */
1430
            fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1431
        }
1432
    } else {
1433
        if (float64_lt(FT0, FT1, &env->fp_status)) {
1434
            T0 = 0x08UL;
1435
        } else if (!float64_le(FT0, FT1, &env->fp_status)) {
1436
            T0 = 0x04UL;
1437
        } else {
1438
            T0 = 0x02UL;
1439
        }
1440
    }
1441
    env->fpscr &= ~(0x0F << FPSCR_FPRF);
1442
    env->fpscr |= T0 << FPSCR_FPRF;
1443
}
1444

    
1445
#if !defined (CONFIG_USER_ONLY)
1446
void cpu_dump_rfi (target_ulong RA, target_ulong msr);
1447

    
1448
void do_store_msr (void)
1449
{
1450
    T0 = hreg_store_msr(env, T0);
1451
    if (T0 != 0) {
1452
        env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1453
        do_raise_exception(T0);
1454
    }
1455
}
1456

    
1457
static always_inline void __do_rfi (target_ulong nip, target_ulong msr,
1458
                                    target_ulong msrm, int keep_msrh)
1459
{
1460
#if defined(TARGET_PPC64)
1461
    if (msr & (1ULL << MSR_SF)) {
1462
        nip = (uint64_t)nip;
1463
        msr &= (uint64_t)msrm;
1464
    } else {
1465
        nip = (uint32_t)nip;
1466
        msr = (uint32_t)(msr & msrm);
1467
        if (keep_msrh)
1468
            msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
1469
    }
1470
#else
1471
    nip = (uint32_t)nip;
1472
    msr &= (uint32_t)msrm;
1473
#endif
1474
    /* XXX: beware: this is false if VLE is supported */
1475
    env->nip = nip & ~((target_ulong)0x00000003);
1476
    hreg_store_msr(env, msr);
1477
#if defined (DEBUG_OP)
1478
    cpu_dump_rfi(env->nip, env->msr);
1479
#endif
1480
    /* No need to raise an exception here,
1481
     * as rfi is always the last insn of a TB
1482
     */
1483
    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1484
}
1485

    
1486
void do_rfi (void)
1487
{
1488
    __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1489
             ~((target_ulong)0xFFFF0000), 1);
1490
}
1491

    
1492
#if defined(TARGET_PPC64)
1493
void do_rfid (void)
1494
{
1495
    __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1496
             ~((target_ulong)0xFFFF0000), 0);
1497
}
1498
#endif
1499
#if defined(TARGET_PPC64H)
1500
void do_hrfid (void)
1501
{
1502
    __do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1503
             ~((target_ulong)0xFFFF0000), 0);
1504
}
1505
#endif
1506
#endif
1507

    
1508
void do_tw (int flags)
1509
{
1510
    if (!likely(!(((int32_t)T0 < (int32_t)T1 && (flags & 0x10)) ||
1511
                  ((int32_t)T0 > (int32_t)T1 && (flags & 0x08)) ||
1512
                  ((int32_t)T0 == (int32_t)T1 && (flags & 0x04)) ||
1513
                  ((uint32_t)T0 < (uint32_t)T1 && (flags & 0x02)) ||
1514
                  ((uint32_t)T0 > (uint32_t)T1 && (flags & 0x01))))) {
1515
        do_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1516
    }
1517
}
1518

    
1519
#if defined(TARGET_PPC64)
1520
void do_td (int flags)
1521
{
1522
    if (!likely(!(((int64_t)T0 < (int64_t)T1 && (flags & 0x10)) ||
1523
                  ((int64_t)T0 > (int64_t)T1 && (flags & 0x08)) ||
1524
                  ((int64_t)T0 == (int64_t)T1 && (flags & 0x04)) ||
1525
                  ((uint64_t)T0 < (uint64_t)T1 && (flags & 0x02)) ||
1526
                  ((uint64_t)T0 > (uint64_t)T1 && (flags & 0x01)))))
1527
        do_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1528
}
1529
#endif
1530

    
1531
/*****************************************************************************/
1532
/* PowerPC 601 specific instructions (POWER bridge) */
1533
void do_POWER_abso (void)
1534
{
1535
    if ((uint32_t)T0 == INT32_MIN) {
1536
        T0 = INT32_MAX;
1537
        xer_ov = 1;
1538
        xer_so = 1;
1539
    } else {
1540
        T0 = -T0;
1541
        xer_ov = 0;
1542
    }
1543
}
1544

    
1545
void do_POWER_clcs (void)
1546
{
1547
    switch (T0) {
1548
    case 0x0CUL:
1549
        /* Instruction cache line size */
1550
        T0 = env->icache_line_size;
1551
        break;
1552
    case 0x0DUL:
1553
        /* Data cache line size */
1554
        T0 = env->dcache_line_size;
1555
        break;
1556
    case 0x0EUL:
1557
        /* Minimum cache line size */
1558
        T0 = env->icache_line_size < env->dcache_line_size ?
1559
            env->icache_line_size : env->dcache_line_size;
1560
        break;
1561
    case 0x0FUL:
1562
        /* Maximum cache line size */
1563
        T0 = env->icache_line_size > env->dcache_line_size ?
1564
            env->icache_line_size : env->dcache_line_size;
1565
        break;
1566
    default:
1567
        /* Undefined */
1568
        break;
1569
    }
1570
}
1571

    
1572
void do_POWER_div (void)
1573
{
1574
    uint64_t tmp;
1575

    
1576
    if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == -1) || (int32_t)T1 == 0) {
1577
        T0 = (long)((-1) * (T0 >> 31));
1578
        env->spr[SPR_MQ] = 0;
1579
    } else {
1580
        tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1581
        env->spr[SPR_MQ] = tmp % T1;
1582
        T0 = tmp / (int32_t)T1;
1583
    }
1584
}
1585

    
1586
void do_POWER_divo (void)
1587
{
1588
    int64_t tmp;
1589

    
1590
    if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == -1) || (int32_t)T1 == 0) {
1591
        T0 = (long)((-1) * (T0 >> 31));
1592
        env->spr[SPR_MQ] = 0;
1593
        xer_ov = 1;
1594
        xer_so = 1;
1595
    } else {
1596
        tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1597
        env->spr[SPR_MQ] = tmp % T1;
1598
        tmp /= (int32_t)T1;
1599
        if (tmp > (int64_t)INT32_MAX || tmp < (int64_t)INT32_MIN) {
1600
            xer_ov = 1;
1601
            xer_so = 1;
1602
        } else {
1603
            xer_ov = 0;
1604
        }
1605
        T0 = tmp;
1606
    }
1607
}
1608

    
1609
void do_POWER_divs (void)
1610
{
1611
    if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == -1) || (int32_t)T1 == 0) {
1612
        T0 = (long)((-1) * (T0 >> 31));
1613
        env->spr[SPR_MQ] = 0;
1614
    } else {
1615
        env->spr[SPR_MQ] = T0 % T1;
1616
        T0 = (int32_t)T0 / (int32_t)T1;
1617
    }
1618
}
1619

    
1620
void do_POWER_divso (void)
1621
{
1622
    if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == -1) || (int32_t)T1 == 0) {
1623
        T0 = (long)((-1) * (T0 >> 31));
1624
        env->spr[SPR_MQ] = 0;
1625
        xer_ov = 1;
1626
        xer_so = 1;
1627
    } else {
1628
        T0 = (int32_t)T0 / (int32_t)T1;
1629
        env->spr[SPR_MQ] = (int32_t)T0 % (int32_t)T1;
1630
        xer_ov = 0;
1631
    }
1632
}
1633

    
1634
void do_POWER_dozo (void)
1635
{
1636
    if ((int32_t)T1 > (int32_t)T0) {
1637
        T2 = T0;
1638
        T0 = T1 - T0;
1639
        if (((uint32_t)(~T2) ^ (uint32_t)T1 ^ UINT32_MAX) &
1640
            ((uint32_t)(~T2) ^ (uint32_t)T0) & (1UL << 31)) {
1641
            xer_ov = 1;
1642
            xer_so = 1;
1643
        } else {
1644
            xer_ov = 0;
1645
        }
1646
    } else {
1647
        T0 = 0;
1648
        xer_ov = 0;
1649
    }
1650
}
1651

    
1652
void do_POWER_maskg (void)
1653
{
1654
    uint32_t ret;
1655

    
1656
    if ((uint32_t)T0 == (uint32_t)(T1 + 1)) {
1657
        ret = -1;
1658
    } else {
1659
        ret = (((uint32_t)(-1)) >> ((uint32_t)T0)) ^
1660
            (((uint32_t)(-1) >> ((uint32_t)T1)) >> 1);
1661
        if ((uint32_t)T0 > (uint32_t)T1)
1662
            ret = ~ret;
1663
    }
1664
    T0 = ret;
1665
}
1666

    
1667
void do_POWER_mulo (void)
1668
{
1669
    uint64_t tmp;
1670

    
1671
    tmp = (uint64_t)T0 * (uint64_t)T1;
1672
    env->spr[SPR_MQ] = tmp >> 32;
1673
    T0 = tmp;
1674
    if (tmp >> 32 != ((uint64_t)T0 >> 16) * ((uint64_t)T1 >> 16)) {
1675
        xer_ov = 1;
1676
        xer_so = 1;
1677
    } else {
1678
        xer_ov = 0;
1679
    }
1680
}
1681

    
1682
#if !defined (CONFIG_USER_ONLY)
1683
void do_POWER_rac (void)
1684
{
1685
    mmu_ctx_t ctx;
1686
    int nb_BATs;
1687

    
1688
    /* We don't have to generate many instances of this instruction,
1689
     * as rac is supervisor only.
1690
     */
1691
    /* XXX: FIX THIS: Pretend we have no BAT */
1692
    nb_BATs = env->nb_BATs;
1693
    env->nb_BATs = 0;
1694
    if (get_physical_address(env, &ctx, T0, 0, ACCESS_INT) == 0)
1695
        T0 = ctx.raddr;
1696
    env->nb_BATs = nb_BATs;
1697
}
1698

    
1699
void do_POWER_rfsvc (void)
1700
{
1701
    __do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1702
}
1703

    
1704
void do_store_hid0_601 (void)
1705
{
1706
    uint32_t hid0;
1707

    
1708
    hid0 = env->spr[SPR_HID0];
1709
    if ((T0 ^ hid0) & 0x00000008) {
1710
        /* Change current endianness */
1711
        env->hflags &= ~(1 << MSR_LE);
1712
        env->hflags_nmsr &= ~(1 << MSR_LE);
1713
        env->hflags_nmsr |= (1 << MSR_LE) & (((T0 >> 3) & 1) << MSR_LE);
1714
        env->hflags |= env->hflags_nmsr;
1715
        if (loglevel != 0) {
1716
            fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
1717
                    __func__, T0 & 0x8 ? 'l' : 'b', env->hflags);
1718
        }
1719
    }
1720
    env->spr[SPR_HID0] = T0;
1721
}
1722
#endif
1723

    
1724
/*****************************************************************************/
1725
/* 602 specific instructions */
1726
/* mfrom is the most crazy instruction ever seen, imho ! */
1727
/* Real implementation uses a ROM table. Do the same */
1728
#define USE_MFROM_ROM_TABLE
1729
void do_op_602_mfrom (void)
1730
{
1731
    if (likely(T0 < 602)) {
1732
#if defined(USE_MFROM_ROM_TABLE)
1733
#include "mfrom_table.c"
1734
        T0 = mfrom_ROM_table[T0];
1735
#else
1736
        double d;
1737
        /* Extremly decomposed:
1738
         *                    -T0 / 256
1739
         * T0 = 256 * log10(10          + 1.0) + 0.5
1740
         */
1741
        d = T0;
1742
        d = float64_div(d, 256, &env->fp_status);
1743
        d = float64_chs(d);
1744
        d = exp10(d); // XXX: use float emulation function
1745
        d = float64_add(d, 1.0, &env->fp_status);
1746
        d = log10(d); // XXX: use float emulation function
1747
        d = float64_mul(d, 256, &env->fp_status);
1748
        d = float64_add(d, 0.5, &env->fp_status);
1749
        T0 = float64_round_to_int(d, &env->fp_status);
1750
#endif
1751
    } else {
1752
        T0 = 0;
1753
    }
1754
}
1755

    
1756
/*****************************************************************************/
1757
/* Embedded PowerPC specific helpers */
1758
void do_405_check_ov (void)
1759
{
1760
    if (likely((((uint32_t)T1 ^ (uint32_t)T2) >> 31) ||
1761
               !(((uint32_t)T0 ^ (uint32_t)T2) >> 31))) {
1762
        xer_ov = 0;
1763
    } else {
1764
        xer_ov = 1;
1765
        xer_so = 1;
1766
    }
1767
}
1768

    
1769
void do_405_check_sat (void)
1770
{
1771
    if (!likely((((uint32_t)T1 ^ (uint32_t)T2) >> 31) ||
1772
                !(((uint32_t)T0 ^ (uint32_t)T2) >> 31))) {
1773
        /* Saturate result */
1774
        if (T2 >> 31) {
1775
            T0 = INT32_MIN;
1776
        } else {
1777
            T0 = INT32_MAX;
1778
        }
1779
    }
1780
}
1781

    
1782
/* XXX: to be improved to check access rights when in user-mode */
1783
void do_load_dcr (void)
1784
{
1785
    target_ulong val;
1786

    
1787
    if (unlikely(env->dcr_env == NULL)) {
1788
        if (loglevel != 0) {
1789
            fprintf(logfile, "No DCR environment\n");
1790
        }
1791
        do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1792
                               POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1793
    } else if (unlikely(ppc_dcr_read(env->dcr_env, T0, &val) != 0)) {
1794
        if (loglevel != 0) {
1795
            fprintf(logfile, "DCR read error %d %03x\n", (int)T0, (int)T0);
1796
        }
1797
        do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1798
                               POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1799
    } else {
1800
        T0 = val;
1801
    }
1802
}
1803

    
1804
void do_store_dcr (void)
1805
{
1806
    if (unlikely(env->dcr_env == NULL)) {
1807
        if (loglevel != 0) {
1808
            fprintf(logfile, "No DCR environment\n");
1809
        }
1810
        do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1811
                               POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1812
    } else if (unlikely(ppc_dcr_write(env->dcr_env, T0, T1) != 0)) {
1813
        if (loglevel != 0) {
1814
            fprintf(logfile, "DCR write error %d %03x\n", (int)T0, (int)T0);
1815
        }
1816
        do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1817
                               POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1818
    }
1819
}
1820

    
1821
#if !defined(CONFIG_USER_ONLY)
1822
void do_40x_rfci (void)
1823
{
1824
    __do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1825
             ~((target_ulong)0xFFFF0000), 0);
1826
}
1827

    
1828
void do_rfci (void)
1829
{
1830
    __do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1831
             ~((target_ulong)0x3FFF0000), 0);
1832
}
1833

    
1834
void do_rfdi (void)
1835
{
1836
    __do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1837
             ~((target_ulong)0x3FFF0000), 0);
1838
}
1839

    
1840
void do_rfmci (void)
1841
{
1842
    __do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1843
             ~((target_ulong)0x3FFF0000), 0);
1844
}
1845

    
1846
void do_load_403_pb (int num)
1847
{
1848
    T0 = env->pb[num];
1849
}
1850

    
1851
void do_store_403_pb (int num)
1852
{
1853
    if (likely(env->pb[num] != T0)) {
1854
        env->pb[num] = T0;
1855
        /* Should be optimized */
1856
        tlb_flush(env, 1);
1857
    }
1858
}
1859
#endif
1860

    
1861
/* 440 specific */
1862
void do_440_dlmzb (void)
1863
{
1864
    target_ulong mask;
1865
    int i;
1866

    
1867
    i = 1;
1868
    for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1869
        if ((T0 & mask) == 0)
1870
            goto done;
1871
        i++;
1872
    }
1873
    for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1874
        if ((T1 & mask) == 0)
1875
            break;
1876
        i++;
1877
    }
1878
 done:
1879
    T0 = i;
1880
}
1881

    
1882
#if defined(TARGET_PPCEMB)
1883
/* SPE extension helpers */
1884
/* Use a table to make this quicker */
1885
static uint8_t hbrev[16] = {
1886
    0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1887
    0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1888
};
1889

    
1890
static always_inline uint8_t byte_reverse (uint8_t val)
1891
{
1892
    return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
1893
}
1894

    
1895
static always_inline uint32_t word_reverse (uint32_t val)
1896
{
1897
    return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
1898
        (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
1899
}
1900

    
1901
#define MASKBITS 16 // Random value - to be fixed
1902
void do_brinc (void)
1903
{
1904
    uint32_t a, b, d, mask;
1905

    
1906
    mask = (uint32_t)(-1UL) >> MASKBITS;
1907
    b = T1_64 & mask;
1908
    a = T0_64 & mask;
1909
    d = word_reverse(1 + word_reverse(a | ~mask));
1910
    T0_64 = (T0_64 & ~mask) | (d & mask);
1911
}
1912

    
1913
#define DO_SPE_OP2(name)                                                      \
1914
void do_ev##name (void)                                                       \
1915
{                                                                             \
1916
    T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) |         \
1917
        (uint64_t)_do_e##name(T0_64, T1_64);                                  \
1918
}
1919

    
1920
#define DO_SPE_OP1(name)                                                      \
1921
void do_ev##name (void)                                                       \
1922
{                                                                             \
1923
    T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) |                      \
1924
        (uint64_t)_do_e##name(T0_64);                                         \
1925
}
1926

    
1927
/* Fixed-point vector arithmetic */
1928
static always_inline uint32_t _do_eabs (uint32_t val)
1929
{
1930
    if (val != 0x80000000)
1931
        val &= ~0x80000000;
1932

    
1933
    return val;
1934
}
1935

    
1936
static always_inline uint32_t _do_eaddw (uint32_t op1, uint32_t op2)
1937
{
1938
    return op1 + op2;
1939
}
1940

    
1941
static always_inline int _do_ecntlsw (uint32_t val)
1942
{
1943
    if (val & 0x80000000)
1944
        return clz32(~val);
1945
    else
1946
        return clz32(val);
1947
}
1948

    
1949
static always_inline int _do_ecntlzw (uint32_t val)
1950
{
1951
    return clz32(val);
1952
}
1953

    
1954
static always_inline uint32_t _do_eneg (uint32_t val)
1955
{
1956
    if (val != 0x80000000)
1957
        val ^= 0x80000000;
1958

    
1959
    return val;
1960
}
1961

    
1962
static always_inline uint32_t _do_erlw (uint32_t op1, uint32_t op2)
1963
{
1964
    return rotl32(op1, op2);
1965
}
1966

    
1967
static always_inline uint32_t _do_erndw (uint32_t val)
1968
{
1969
    return (val + 0x000080000000) & 0xFFFF0000;
1970
}
1971

    
1972
static always_inline uint32_t _do_eslw (uint32_t op1, uint32_t op2)
1973
{
1974
    /* No error here: 6 bits are used */
1975
    return op1 << (op2 & 0x3F);
1976
}
1977

    
1978
static always_inline int32_t _do_esrws (int32_t op1, uint32_t op2)
1979
{
1980
    /* No error here: 6 bits are used */
1981
    return op1 >> (op2 & 0x3F);
1982
}
1983

    
1984
static always_inline uint32_t _do_esrwu (uint32_t op1, uint32_t op2)
1985
{
1986
    /* No error here: 6 bits are used */
1987
    return op1 >> (op2 & 0x3F);
1988
}
1989

    
1990
static always_inline uint32_t _do_esubfw (uint32_t op1, uint32_t op2)
1991
{
1992
    return op2 - op1;
1993
}
1994

    
1995
/* evabs */
1996
DO_SPE_OP1(abs);
1997
/* evaddw */
1998
DO_SPE_OP2(addw);
1999
/* evcntlsw */
2000
DO_SPE_OP1(cntlsw);
2001
/* evcntlzw */
2002
DO_SPE_OP1(cntlzw);
2003
/* evneg */
2004
DO_SPE_OP1(neg);
2005
/* evrlw */
2006
DO_SPE_OP2(rlw);
2007
/* evrnd */
2008
DO_SPE_OP1(rndw);
2009
/* evslw */
2010
DO_SPE_OP2(slw);
2011
/* evsrws */
2012
DO_SPE_OP2(srws);
2013
/* evsrwu */
2014
DO_SPE_OP2(srwu);
2015
/* evsubfw */
2016
DO_SPE_OP2(subfw);
2017

    
2018
/* evsel is a little bit more complicated... */
2019
static always_inline uint32_t _do_esel (uint32_t op1, uint32_t op2, int n)
2020
{
2021
    if (n)
2022
        return op1;
2023
    else
2024
        return op2;
2025
}
2026

    
2027
void do_evsel (void)
2028
{
2029
    T0_64 = ((uint64_t)_do_esel(T0_64 >> 32, T1_64 >> 32, T0 >> 3) << 32) |
2030
        (uint64_t)_do_esel(T0_64, T1_64, (T0 >> 2) & 1);
2031
}
2032

    
2033
/* Fixed-point vector comparisons */
2034
#define DO_SPE_CMP(name)                                                      \
2035
void do_ev##name (void)                                                       \
2036
{                                                                             \
2037
    T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32,                   \
2038
                                               T1_64 >> 32) << 32,            \
2039
                         _do_e##name(T0_64, T1_64));                          \
2040
}
2041

    
2042
static always_inline uint32_t _do_evcmp_merge (int t0, int t1)
2043
{
2044
    return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
2045
}
2046
static always_inline int _do_ecmpeq (uint32_t op1, uint32_t op2)
2047
{
2048
    return op1 == op2 ? 1 : 0;
2049
}
2050

    
2051
static always_inline int _do_ecmpgts (int32_t op1, int32_t op2)
2052
{
2053
    return op1 > op2 ? 1 : 0;
2054
}
2055

    
2056
static always_inline int _do_ecmpgtu (uint32_t op1, uint32_t op2)
2057
{
2058
    return op1 > op2 ? 1 : 0;
2059
}
2060

    
2061
static always_inline int _do_ecmplts (int32_t op1, int32_t op2)
2062
{
2063
    return op1 < op2 ? 1 : 0;
2064
}
2065

    
2066
static always_inline int _do_ecmpltu (uint32_t op1, uint32_t op2)
2067
{
2068
    return op1 < op2 ? 1 : 0;
2069
}
2070

    
2071
/* evcmpeq */
2072
DO_SPE_CMP(cmpeq);
2073
/* evcmpgts */
2074
DO_SPE_CMP(cmpgts);
2075
/* evcmpgtu */
2076
DO_SPE_CMP(cmpgtu);
2077
/* evcmplts */
2078
DO_SPE_CMP(cmplts);
2079
/* evcmpltu */
2080
DO_SPE_CMP(cmpltu);
2081

    
2082
/* Single precision floating-point conversions from/to integer */
2083
static always_inline uint32_t _do_efscfsi (int32_t val)
2084
{
2085
    union {
2086
        uint32_t u;
2087
        float32 f;
2088
    } u;
2089

    
2090
    u.f = int32_to_float32(val, &env->spe_status);
2091

    
2092
    return u.u;
2093
}
2094

    
2095
static always_inline uint32_t _do_efscfui (uint32_t val)
2096
{
2097
    union {
2098
        uint32_t u;
2099
        float32 f;
2100
    } u;
2101

    
2102
    u.f = uint32_to_float32(val, &env->spe_status);
2103

    
2104
    return u.u;
2105
}
2106

    
2107
static always_inline int32_t _do_efsctsi (uint32_t val)
2108
{
2109
    union {
2110
        int32_t u;
2111
        float32 f;
2112
    } u;
2113

    
2114
    u.u = val;
2115
    /* NaN are not treated the same way IEEE 754 does */
2116
    if (unlikely(isnan(u.f)))
2117
        return 0;
2118

    
2119
    return float32_to_int32(u.f, &env->spe_status);
2120
}
2121

    
2122
static always_inline uint32_t _do_efsctui (uint32_t val)
2123
{
2124
    union {
2125
        int32_t u;
2126
        float32 f;
2127
    } u;
2128

    
2129
    u.u = val;
2130
    /* NaN are not treated the same way IEEE 754 does */
2131
    if (unlikely(isnan(u.f)))
2132
        return 0;
2133

    
2134
    return float32_to_uint32(u.f, &env->spe_status);
2135
}
2136

    
2137
static always_inline int32_t _do_efsctsiz (uint32_t val)
2138
{
2139
    union {
2140
        int32_t u;
2141
        float32 f;
2142
    } u;
2143

    
2144
    u.u = val;
2145
    /* NaN are not treated the same way IEEE 754 does */
2146
    if (unlikely(isnan(u.f)))
2147
        return 0;
2148

    
2149
    return float32_to_int32_round_to_zero(u.f, &env->spe_status);
2150
}
2151

    
2152
static always_inline uint32_t _do_efsctuiz (uint32_t val)
2153
{
2154
    union {
2155
        int32_t u;
2156
        float32 f;
2157
    } u;
2158

    
2159
    u.u = val;
2160
    /* NaN are not treated the same way IEEE 754 does */
2161
    if (unlikely(isnan(u.f)))
2162
        return 0;
2163

    
2164
    return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
2165
}
2166

    
2167
void do_efscfsi (void)
2168
{
2169
    T0_64 = _do_efscfsi(T0_64);
2170
}
2171

    
2172
void do_efscfui (void)
2173
{
2174
    T0_64 = _do_efscfui(T0_64);
2175
}
2176

    
2177
void do_efsctsi (void)
2178
{
2179
    T0_64 = _do_efsctsi(T0_64);
2180
}
2181

    
2182
void do_efsctui (void)
2183
{
2184
    T0_64 = _do_efsctui(T0_64);
2185
}
2186

    
2187
void do_efsctsiz (void)
2188
{
2189
    T0_64 = _do_efsctsiz(T0_64);
2190
}
2191

    
2192
void do_efsctuiz (void)
2193
{
2194
    T0_64 = _do_efsctuiz(T0_64);
2195
}
2196

    
2197
/* Single precision floating-point conversion to/from fractional */
2198
static always_inline uint32_t _do_efscfsf (uint32_t val)
2199
{
2200
    union {
2201
        uint32_t u;
2202
        float32 f;
2203
    } u;
2204
    float32 tmp;
2205

    
2206
    u.f = int32_to_float32(val, &env->spe_status);
2207
    tmp = int64_to_float32(1ULL << 32, &env->spe_status);
2208
    u.f = float32_div(u.f, tmp, &env->spe_status);
2209

    
2210
    return u.u;
2211
}
2212

    
2213
static always_inline uint32_t _do_efscfuf (uint32_t val)
2214
{
2215
    union {
2216
        uint32_t u;
2217
        float32 f;
2218
    } u;
2219
    float32 tmp;
2220

    
2221
    u.f = uint32_to_float32(val, &env->spe_status);
2222
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2223
    u.f = float32_div(u.f, tmp, &env->spe_status);
2224

    
2225
    return u.u;
2226
}
2227

    
2228
static always_inline int32_t _do_efsctsf (uint32_t val)
2229
{
2230
    union {
2231
        int32_t u;
2232
        float32 f;
2233
    } u;
2234
    float32 tmp;
2235

    
2236
    u.u = val;
2237
    /* NaN are not treated the same way IEEE 754 does */
2238
    if (unlikely(isnan(u.f)))
2239
        return 0;
2240
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2241
    u.f = float32_mul(u.f, tmp, &env->spe_status);
2242

    
2243
    return float32_to_int32(u.f, &env->spe_status);
2244
}
2245

    
2246
static always_inline uint32_t _do_efsctuf (uint32_t val)
2247
{
2248
    union {
2249
        int32_t u;
2250
        float32 f;
2251
    } u;
2252
    float32 tmp;
2253

    
2254
    u.u = val;
2255
    /* NaN are not treated the same way IEEE 754 does */
2256
    if (unlikely(isnan(u.f)))
2257
        return 0;
2258
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2259
    u.f = float32_mul(u.f, tmp, &env->spe_status);
2260

    
2261
    return float32_to_uint32(u.f, &env->spe_status);
2262
}
2263

    
2264
static always_inline int32_t _do_efsctsfz (uint32_t val)
2265
{
2266
    union {
2267
        int32_t u;
2268
        float32 f;
2269
    } u;
2270
    float32 tmp;
2271

    
2272
    u.u = val;
2273
    /* NaN are not treated the same way IEEE 754 does */
2274
    if (unlikely(isnan(u.f)))
2275
        return 0;
2276
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2277
    u.f = float32_mul(u.f, tmp, &env->spe_status);
2278

    
2279
    return float32_to_int32_round_to_zero(u.f, &env->spe_status);
2280
}
2281

    
2282
static always_inline uint32_t _do_efsctufz (uint32_t val)
2283
{
2284
    union {
2285
        int32_t u;
2286
        float32 f;
2287
    } u;
2288
    float32 tmp;
2289

    
2290
    u.u = val;
2291
    /* NaN are not treated the same way IEEE 754 does */
2292
    if (unlikely(isnan(u.f)))
2293
        return 0;
2294
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2295
    u.f = float32_mul(u.f, tmp, &env->spe_status);
2296

    
2297
    return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
2298
}
2299

    
2300
void do_efscfsf (void)
2301
{
2302
    T0_64 = _do_efscfsf(T0_64);
2303
}
2304

    
2305
void do_efscfuf (void)
2306
{
2307
    T0_64 = _do_efscfuf(T0_64);
2308
}
2309

    
2310
void do_efsctsf (void)
2311
{
2312
    T0_64 = _do_efsctsf(T0_64);
2313
}
2314

    
2315
void do_efsctuf (void)
2316
{
2317
    T0_64 = _do_efsctuf(T0_64);
2318
}
2319

    
2320
void do_efsctsfz (void)
2321
{
2322
    T0_64 = _do_efsctsfz(T0_64);
2323
}
2324

    
2325
void do_efsctufz (void)
2326
{
2327
    T0_64 = _do_efsctufz(T0_64);
2328
}
2329

    
2330
/* Double precision floating point helpers */
2331
static always_inline int _do_efdcmplt (uint64_t op1, uint64_t op2)
2332
{
2333
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2334
    return _do_efdtstlt(op1, op2);
2335
}
2336

    
2337
static always_inline int _do_efdcmpgt (uint64_t op1, uint64_t op2)
2338
{
2339
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2340
    return _do_efdtstgt(op1, op2);
2341
}
2342

    
2343
static always_inline int _do_efdcmpeq (uint64_t op1, uint64_t op2)
2344
{
2345
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2346
    return _do_efdtsteq(op1, op2);
2347
}
2348

    
2349
void do_efdcmplt (void)
2350
{
2351
    T0 = _do_efdcmplt(T0_64, T1_64);
2352
}
2353

    
2354
void do_efdcmpgt (void)
2355
{
2356
    T0 = _do_efdcmpgt(T0_64, T1_64);
2357
}
2358

    
2359
void do_efdcmpeq (void)
2360
{
2361
    T0 = _do_efdcmpeq(T0_64, T1_64);
2362
}
2363

    
2364
/* Double precision floating-point conversion to/from integer */
2365
static always_inline uint64_t _do_efdcfsi (int64_t val)
2366
{
2367
    union {
2368
        uint64_t u;
2369
        float64 f;
2370
    } u;
2371

    
2372
    u.f = int64_to_float64(val, &env->spe_status);
2373

    
2374
    return u.u;
2375
}
2376

    
2377
static always_inline uint64_t _do_efdcfui (uint64_t val)
2378
{
2379
    union {
2380
        uint64_t u;
2381
        float64 f;
2382
    } u;
2383

    
2384
    u.f = uint64_to_float64(val, &env->spe_status);
2385

    
2386
    return u.u;
2387
}
2388

    
2389
static always_inline int64_t _do_efdctsi (uint64_t val)
2390
{
2391
    union {
2392
        int64_t u;
2393
        float64 f;
2394
    } u;
2395

    
2396
    u.u = val;
2397
    /* NaN are not treated the same way IEEE 754 does */
2398
    if (unlikely(isnan(u.f)))
2399
        return 0;
2400

    
2401
    return float64_to_int64(u.f, &env->spe_status);
2402
}
2403

    
2404
static always_inline uint64_t _do_efdctui (uint64_t val)
2405
{
2406
    union {
2407
        int64_t u;
2408
        float64 f;
2409
    } u;
2410

    
2411
    u.u = val;
2412
    /* NaN are not treated the same way IEEE 754 does */
2413
    if (unlikely(isnan(u.f)))
2414
        return 0;
2415

    
2416
    return float64_to_uint64(u.f, &env->spe_status);
2417
}
2418

    
2419
static always_inline int64_t _do_efdctsiz (uint64_t val)
2420
{
2421
    union {
2422
        int64_t u;
2423
        float64 f;
2424
    } u;
2425

    
2426
    u.u = val;
2427
    /* NaN are not treated the same way IEEE 754 does */
2428
    if (unlikely(isnan(u.f)))
2429
        return 0;
2430

    
2431
    return float64_to_int64_round_to_zero(u.f, &env->spe_status);
2432
}
2433

    
2434
static always_inline uint64_t _do_efdctuiz (uint64_t val)
2435
{
2436
    union {
2437
        int64_t u;
2438
        float64 f;
2439
    } u;
2440

    
2441
    u.u = val;
2442
    /* NaN are not treated the same way IEEE 754 does */
2443
    if (unlikely(isnan(u.f)))
2444
        return 0;
2445

    
2446
    return float64_to_uint64_round_to_zero(u.f, &env->spe_status);
2447
}
2448

    
2449
void do_efdcfsi (void)
2450
{
2451
    T0_64 = _do_efdcfsi(T0_64);
2452
}
2453

    
2454
void do_efdcfui (void)
2455
{
2456
    T0_64 = _do_efdcfui(T0_64);
2457
}
2458

    
2459
void do_efdctsi (void)
2460
{
2461
    T0_64 = _do_efdctsi(T0_64);
2462
}
2463

    
2464
void do_efdctui (void)
2465
{
2466
    T0_64 = _do_efdctui(T0_64);
2467
}
2468

    
2469
void do_efdctsiz (void)
2470
{
2471
    T0_64 = _do_efdctsiz(T0_64);
2472
}
2473

    
2474
void do_efdctuiz (void)
2475
{
2476
    T0_64 = _do_efdctuiz(T0_64);
2477
}
2478

    
2479
/* Double precision floating-point conversion to/from fractional */
2480
static always_inline uint64_t _do_efdcfsf (int64_t val)
2481
{
2482
    union {
2483
        uint64_t u;
2484
        float64 f;
2485
    } u;
2486
    float64 tmp;
2487

    
2488
    u.f = int32_to_float64(val, &env->spe_status);
2489
    tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2490
    u.f = float64_div(u.f, tmp, &env->spe_status);
2491

    
2492
    return u.u;
2493
}
2494

    
2495
static always_inline uint64_t _do_efdcfuf (uint64_t val)
2496
{
2497
    union {
2498
        uint64_t u;
2499
        float64 f;
2500
    } u;
2501
    float64 tmp;
2502

    
2503
    u.f = uint32_to_float64(val, &env->spe_status);
2504
    tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2505
    u.f = float64_div(u.f, tmp, &env->spe_status);
2506

    
2507
    return u.u;
2508
}
2509

    
2510
static always_inline int64_t _do_efdctsf (uint64_t val)
2511
{
2512
    union {
2513
        int64_t u;
2514
        float64 f;
2515
    } u;
2516
    float64 tmp;
2517

    
2518
    u.u = val;
2519
    /* NaN are not treated the same way IEEE 754 does */
2520
    if (unlikely(isnan(u.f)))
2521
        return 0;
2522
    tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2523
    u.f = float64_mul(u.f, tmp, &env->spe_status);
2524

    
2525
    return float64_to_int32(u.f, &env->spe_status);
2526
}
2527

    
2528
static always_inline uint64_t _do_efdctuf (uint64_t val)
2529
{
2530
    union {
2531
        int64_t u;
2532
        float64 f;
2533
    } u;
2534
    float64 tmp;
2535

    
2536
    u.u = val;
2537
    /* NaN are not treated the same way IEEE 754 does */
2538
    if (unlikely(isnan(u.f)))
2539
        return 0;
2540
    tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2541
    u.f = float64_mul(u.f, tmp, &env->spe_status);
2542

    
2543
    return float64_to_uint32(u.f, &env->spe_status);
2544
}
2545

    
2546
static always_inline int64_t _do_efdctsfz (uint64_t val)
2547
{
2548
    union {
2549
        int64_t u;
2550
        float64 f;
2551
    } u;
2552
    float64 tmp;
2553

    
2554
    u.u = val;
2555
    /* NaN are not treated the same way IEEE 754 does */
2556
    if (unlikely(isnan(u.f)))
2557
        return 0;
2558
    tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2559
    u.f = float64_mul(u.f, tmp, &env->spe_status);
2560

    
2561
    return float64_to_int32_round_to_zero(u.f, &env->spe_status);
2562
}
2563

    
2564
static always_inline uint64_t _do_efdctufz (uint64_t val)
2565
{
2566
    union {
2567
        int64_t u;
2568
        float64 f;
2569
    } u;
2570
    float64 tmp;
2571

    
2572
    u.u = val;
2573
    /* NaN are not treated the same way IEEE 754 does */
2574
    if (unlikely(isnan(u.f)))
2575
        return 0;
2576
    tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2577
    u.f = float64_mul(u.f, tmp, &env->spe_status);
2578

    
2579
    return float64_to_uint32_round_to_zero(u.f, &env->spe_status);
2580
}
2581

    
2582
void do_efdcfsf (void)
2583
{
2584
    T0_64 = _do_efdcfsf(T0_64);
2585
}
2586

    
2587
void do_efdcfuf (void)
2588
{
2589
    T0_64 = _do_efdcfuf(T0_64);
2590
}
2591

    
2592
void do_efdctsf (void)
2593
{
2594
    T0_64 = _do_efdctsf(T0_64);
2595
}
2596

    
2597
void do_efdctuf (void)
2598
{
2599
    T0_64 = _do_efdctuf(T0_64);
2600
}
2601

    
2602
void do_efdctsfz (void)
2603
{
2604
    T0_64 = _do_efdctsfz(T0_64);
2605
}
2606

    
2607
void do_efdctufz (void)
2608
{
2609
    T0_64 = _do_efdctufz(T0_64);
2610
}
2611

    
2612
/* Floating point conversion between single and double precision */
2613
static always_inline uint32_t _do_efscfd (uint64_t val)
2614
{
2615
    union {
2616
        uint64_t u;
2617
        float64 f;
2618
    } u1;
2619
    union {
2620
        uint32_t u;
2621
        float32 f;
2622
    } u2;
2623

    
2624
    u1.u = val;
2625
    u2.f = float64_to_float32(u1.f, &env->spe_status);
2626

    
2627
    return u2.u;
2628
}
2629

    
2630
static always_inline uint64_t _do_efdcfs (uint32_t val)
2631
{
2632
    union {
2633
        uint64_t u;
2634
        float64 f;
2635
    } u2;
2636
    union {
2637
        uint32_t u;
2638
        float32 f;
2639
    } u1;
2640

    
2641
    u1.u = val;
2642
    u2.f = float32_to_float64(u1.f, &env->spe_status);
2643

    
2644
    return u2.u;
2645
}
2646

    
2647
void do_efscfd (void)
2648
{
2649
    T0_64 = _do_efscfd(T0_64);
2650
}
2651

    
2652
void do_efdcfs (void)
2653
{
2654
    T0_64 = _do_efdcfs(T0_64);
2655
}
2656

    
2657
/* Single precision fixed-point vector arithmetic */
2658
/* evfsabs */
2659
DO_SPE_OP1(fsabs);
2660
/* evfsnabs */
2661
DO_SPE_OP1(fsnabs);
2662
/* evfsneg */
2663
DO_SPE_OP1(fsneg);
2664
/* evfsadd */
2665
DO_SPE_OP2(fsadd);
2666
/* evfssub */
2667
DO_SPE_OP2(fssub);
2668
/* evfsmul */
2669
DO_SPE_OP2(fsmul);
2670
/* evfsdiv */
2671
DO_SPE_OP2(fsdiv);
2672

    
2673
/* Single-precision floating-point comparisons */
2674
static always_inline int _do_efscmplt (uint32_t op1, uint32_t op2)
2675
{
2676
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2677
    return _do_efststlt(op1, op2);
2678
}
2679

    
2680
static always_inline int _do_efscmpgt (uint32_t op1, uint32_t op2)
2681
{
2682
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2683
    return _do_efststgt(op1, op2);
2684
}
2685

    
2686
static always_inline int _do_efscmpeq (uint32_t op1, uint32_t op2)
2687
{
2688
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2689
    return _do_efststeq(op1, op2);
2690
}
2691

    
2692
void do_efscmplt (void)
2693
{
2694
    T0 = _do_efscmplt(T0_64, T1_64);
2695
}
2696

    
2697
void do_efscmpgt (void)
2698
{
2699
    T0 = _do_efscmpgt(T0_64, T1_64);
2700
}
2701

    
2702
void do_efscmpeq (void)
2703
{
2704
    T0 = _do_efscmpeq(T0_64, T1_64);
2705
}
2706

    
2707
/* Single-precision floating-point vector comparisons */
2708
/* evfscmplt */
2709
DO_SPE_CMP(fscmplt);
2710
/* evfscmpgt */
2711
DO_SPE_CMP(fscmpgt);
2712
/* evfscmpeq */
2713
DO_SPE_CMP(fscmpeq);
2714
/* evfststlt */
2715
DO_SPE_CMP(fststlt);
2716
/* evfststgt */
2717
DO_SPE_CMP(fststgt);
2718
/* evfststeq */
2719
DO_SPE_CMP(fststeq);
2720

    
2721
/* Single-precision floating-point vector conversions */
2722
/* evfscfsi */
2723
DO_SPE_OP1(fscfsi);
2724
/* evfscfui */
2725
DO_SPE_OP1(fscfui);
2726
/* evfscfuf */
2727
DO_SPE_OP1(fscfuf);
2728
/* evfscfsf */
2729
DO_SPE_OP1(fscfsf);
2730
/* evfsctsi */
2731
DO_SPE_OP1(fsctsi);
2732
/* evfsctui */
2733
DO_SPE_OP1(fsctui);
2734
/* evfsctsiz */
2735
DO_SPE_OP1(fsctsiz);
2736
/* evfsctuiz */
2737
DO_SPE_OP1(fsctuiz);
2738
/* evfsctsf */
2739
DO_SPE_OP1(fsctsf);
2740
/* evfsctuf */
2741
DO_SPE_OP1(fsctuf);
2742
#endif /* defined(TARGET_PPCEMB) */
2743

    
2744
/*****************************************************************************/
2745
/* Softmmu support */
2746
#if !defined (CONFIG_USER_ONLY)
2747

    
2748
#define MMUSUFFIX _mmu
2749
#ifdef __s390__
2750
# define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
2751
#else
2752
# define GETPC() (__builtin_return_address(0))
2753
#endif
2754

    
2755
#define SHIFT 0
2756
#include "softmmu_template.h"
2757

    
2758
#define SHIFT 1
2759
#include "softmmu_template.h"
2760

    
2761
#define SHIFT 2
2762
#include "softmmu_template.h"
2763

    
2764
#define SHIFT 3
2765
#include "softmmu_template.h"
2766

    
2767
/* try to fill the TLB and return an exception if error. If retaddr is
2768
   NULL, it means that the function was called in C code (i.e. not
2769
   from generated code or from helper.c) */
2770
/* XXX: fix it to restore all registers */
2771
void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2772
{
2773
    TranslationBlock *tb;
2774
    CPUState *saved_env;
2775
    target_phys_addr_t pc;
2776
    int ret;
2777

    
2778
    /* XXX: hack to restore env in all cases, even if not called from
2779
       generated code */
2780
    saved_env = env;
2781
    env = cpu_single_env;
2782
    ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2783
    if (unlikely(ret != 0)) {
2784
        if (likely(retaddr)) {
2785
            /* now we have a real cpu fault */
2786
            pc = (target_phys_addr_t)(unsigned long)retaddr;
2787
            tb = tb_find_pc(pc);
2788
            if (likely(tb)) {
2789
                /* the PC is inside the translated code. It means that we have
2790
                   a virtual CPU fault */
2791
                cpu_restore_state(tb, env, pc, NULL);
2792
            }
2793
        }
2794
        do_raise_exception_err(env->exception_index, env->error_code);
2795
    }
2796
    env = saved_env;
2797
}
2798

    
2799
/* Software driven TLBs management */
2800
/* PowerPC 602/603 software TLB load instructions helpers */
2801
void do_load_6xx_tlb (int is_code)
2802
{
2803
    target_ulong RPN, CMP, EPN;
2804
    int way;
2805

    
2806
    RPN = env->spr[SPR_RPA];
2807
    if (is_code) {
2808
        CMP = env->spr[SPR_ICMP];
2809
        EPN = env->spr[SPR_IMISS];
2810
    } else {
2811
        CMP = env->spr[SPR_DCMP];
2812
        EPN = env->spr[SPR_DMISS];
2813
    }
2814
    way = (env->spr[SPR_SRR1] >> 17) & 1;
2815
#if defined (DEBUG_SOFTWARE_TLB)
2816
    if (loglevel != 0) {
2817
        fprintf(logfile, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2818
                __func__, (unsigned long)T0, (unsigned long)EPN,
2819
                (unsigned long)CMP, (unsigned long)RPN, way);
2820
    }
2821
#endif
2822
    /* Store this TLB */
2823
    ppc6xx_tlb_store(env, (uint32_t)(T0 & TARGET_PAGE_MASK),
2824
                     way, is_code, CMP, RPN);
2825
}
2826

    
2827
void do_load_74xx_tlb (int is_code)
2828
{
2829
    target_ulong RPN, CMP, EPN;
2830
    int way;
2831

    
2832
    RPN = env->spr[SPR_PTELO];
2833
    CMP = env->spr[SPR_PTEHI];
2834
    EPN = env->spr[SPR_TLBMISS] & ~0x3;
2835
    way = env->spr[SPR_TLBMISS] & 0x3;
2836
#if defined (DEBUG_SOFTWARE_TLB)
2837
    if (loglevel != 0) {
2838
        fprintf(logfile, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2839
                __func__, (unsigned long)T0, (unsigned long)EPN,
2840
                (unsigned long)CMP, (unsigned long)RPN, way);
2841
    }
2842
#endif
2843
    /* Store this TLB */
2844
    ppc6xx_tlb_store(env, (uint32_t)(T0 & TARGET_PAGE_MASK),
2845
                     way, is_code, CMP, RPN);
2846
}
2847

    
2848
static always_inline target_ulong booke_tlb_to_page_size (int size)
2849
{
2850
    return 1024 << (2 * size);
2851
}
2852

    
2853
static always_inline int booke_page_size_to_tlb (target_ulong page_size)
2854
{
2855
    int size;
2856

    
2857
    switch (page_size) {
2858
    case 0x00000400UL:
2859
        size = 0x0;
2860
        break;
2861
    case 0x00001000UL:
2862
        size = 0x1;
2863
        break;
2864
    case 0x00004000UL:
2865
        size = 0x2;
2866
        break;
2867
    case 0x00010000UL:
2868
        size = 0x3;
2869
        break;
2870
    case 0x00040000UL:
2871
        size = 0x4;
2872
        break;
2873
    case 0x00100000UL:
2874
        size = 0x5;
2875
        break;
2876
    case 0x00400000UL:
2877
        size = 0x6;
2878
        break;
2879
    case 0x01000000UL:
2880
        size = 0x7;
2881
        break;
2882
    case 0x04000000UL:
2883
        size = 0x8;
2884
        break;
2885
    case 0x10000000UL:
2886
        size = 0x9;
2887
        break;
2888
    case 0x40000000UL:
2889
        size = 0xA;
2890
        break;
2891
#if defined (TARGET_PPC64)
2892
    case 0x000100000000ULL:
2893
        size = 0xB;
2894
        break;
2895
    case 0x000400000000ULL:
2896
        size = 0xC;
2897
        break;
2898
    case 0x001000000000ULL:
2899
        size = 0xD;
2900
        break;
2901
    case 0x004000000000ULL:
2902
        size = 0xE;
2903
        break;
2904
    case 0x010000000000ULL:
2905
        size = 0xF;
2906
        break;
2907
#endif
2908
    default:
2909
        size = -1;
2910
        break;
2911
    }
2912

    
2913
    return size;
2914
}
2915

    
2916
/* Helpers for 4xx TLB management */
2917
void do_4xx_tlbre_lo (void)
2918
{
2919
    ppcemb_tlb_t *tlb;
2920
    int size;
2921

    
2922
    T0 &= 0x3F;
2923
    tlb = &env->tlb[T0].tlbe;
2924
    T0 = tlb->EPN;
2925
    if (tlb->prot & PAGE_VALID)
2926
        T0 |= 0x400;
2927
    size = booke_page_size_to_tlb(tlb->size);
2928
    if (size < 0 || size > 0x7)
2929
        size = 1;
2930
    T0 |= size << 7;
2931
    env->spr[SPR_40x_PID] = tlb->PID;
2932
}
2933

    
2934
void do_4xx_tlbre_hi (void)
2935
{
2936
    ppcemb_tlb_t *tlb;
2937

    
2938
    T0 &= 0x3F;
2939
    tlb = &env->tlb[T0].tlbe;
2940
    T0 = tlb->RPN;
2941
    if (tlb->prot & PAGE_EXEC)
2942
        T0 |= 0x200;
2943
    if (tlb->prot & PAGE_WRITE)
2944
        T0 |= 0x100;
2945
}
2946

    
2947
void do_4xx_tlbwe_hi (void)
2948
{
2949
    ppcemb_tlb_t *tlb;
2950
    target_ulong page, end;
2951

    
2952
#if defined (DEBUG_SOFTWARE_TLB)
2953
    if (loglevel != 0) {
2954
        fprintf(logfile, "%s T0 " REGX " T1 " REGX "\n", __func__, T0, T1);
2955
    }
2956
#endif
2957
    T0 &= 0x3F;
2958
    tlb = &env->tlb[T0].tlbe;
2959
    /* Invalidate previous TLB (if it's valid) */
2960
    if (tlb->prot & PAGE_VALID) {
2961
        end = tlb->EPN + tlb->size;
2962
#if defined (DEBUG_SOFTWARE_TLB)
2963
        if (loglevel != 0) {
2964
            fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2965
                    " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2966
        }
2967
#endif
2968
        for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2969
            tlb_flush_page(env, page);
2970
    }
2971
    tlb->size = booke_tlb_to_page_size((T1 >> 7) & 0x7);
2972
    /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2973
     * If this ever occurs, one should use the ppcemb target instead
2974
     * of the ppc or ppc64 one
2975
     */
2976
    if ((T1 & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
2977
        cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2978
                  "are not supported (%d)\n",
2979
                  tlb->size, TARGET_PAGE_SIZE, (int)((T1 >> 7) & 0x7));
2980
    }
2981
    tlb->EPN = T1 & ~(tlb->size - 1);
2982
    if (T1 & 0x40)
2983
        tlb->prot |= PAGE_VALID;
2984
    else
2985
        tlb->prot &= ~PAGE_VALID;
2986
    if (T1 & 0x20) {
2987
        /* XXX: TO BE FIXED */
2988
        cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2989
    }
2990
    tlb->PID = env->spr[SPR_40x_PID]; /* PID */
2991
    tlb->attr = T1 & 0xFF;
2992
#if defined (DEBUG_SOFTWARE_TLB)
2993
    if (loglevel != 0) {
2994
        fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2995
                " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2996
                (int)T0, tlb->RPN, tlb->EPN, tlb->size,
2997
                tlb->prot & PAGE_READ ? 'r' : '-',
2998
                tlb->prot & PAGE_WRITE ? 'w' : '-',
2999
                tlb->prot & PAGE_EXEC ? 'x' : '-',
3000
                tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
3001
    }
3002
#endif
3003
    /* Invalidate new TLB (if valid) */
3004
    if (tlb->prot & PAGE_VALID) {
3005
        end = tlb->EPN + tlb->size;
3006
#if defined (DEBUG_SOFTWARE_TLB)
3007
        if (loglevel != 0) {
3008
            fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
3009
                    " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
3010
        }
3011
#endif
3012
        for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
3013
            tlb_flush_page(env, page);
3014
    }
3015
}
3016

    
3017
void do_4xx_tlbwe_lo (void)
3018
{
3019
    ppcemb_tlb_t *tlb;
3020

    
3021
#if defined (DEBUG_SOFTWARE_TLB)
3022
    if (loglevel != 0) {
3023
        fprintf(logfile, "%s T0 " REGX " T1 " REGX "\n", __func__, T0, T1);
3024
    }
3025
#endif
3026
    T0 &= 0x3F;
3027
    tlb = &env->tlb[T0].tlbe;
3028
    tlb->RPN = T1 & 0xFFFFFC00;
3029
    tlb->prot = PAGE_READ;
3030
    if (T1 & 0x200)
3031
        tlb->prot |= PAGE_EXEC;
3032
    if (T1 & 0x100)
3033
        tlb->prot |= PAGE_WRITE;
3034
#if defined (DEBUG_SOFTWARE_TLB)
3035
    if (loglevel != 0) {
3036
        fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
3037
                " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
3038
                (int)T0, tlb->RPN, tlb->EPN, tlb->size,
3039
                tlb->prot & PAGE_READ ? 'r' : '-',
3040
                tlb->prot & PAGE_WRITE ? 'w' : '-',
3041
                tlb->prot & PAGE_EXEC ? 'x' : '-',
3042
                tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
3043
    }
3044
#endif
3045
}
3046

    
3047
/* PowerPC 440 TLB management */
3048
void do_440_tlbwe (int word)
3049
{
3050
    ppcemb_tlb_t *tlb;
3051
    target_ulong EPN, RPN, size;
3052
    int do_flush_tlbs;
3053

    
3054
#if defined (DEBUG_SOFTWARE_TLB)
3055
    if (loglevel != 0) {
3056
        fprintf(logfile, "%s word %d T0 " REGX " T1 " REGX "\n",
3057
                __func__, word, T0, T1);
3058
    }
3059
#endif
3060
    do_flush_tlbs = 0;
3061
    T0 &= 0x3F;
3062
    tlb = &env->tlb[T0].tlbe;
3063
    switch (word) {
3064
    default:
3065
        /* Just here to please gcc */
3066
    case 0:
3067
        EPN = T1 & 0xFFFFFC00;
3068
        if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
3069
            do_flush_tlbs = 1;
3070
        tlb->EPN = EPN;
3071
        size = booke_tlb_to_page_size((T1 >> 4) & 0xF);
3072
        if ((tlb->prot & PAGE_VALID) && tlb->size < size)
3073
            do_flush_tlbs = 1;
3074
        tlb->size = size;
3075
        tlb->attr &= ~0x1;
3076
        tlb->attr |= (T1 >> 8) & 1;
3077
        if (T1 & 0x200) {
3078
            tlb->prot |= PAGE_VALID;
3079
        } else {
3080
            if (tlb->prot & PAGE_VALID) {
3081
                tlb->prot &= ~PAGE_VALID;
3082
                do_flush_tlbs = 1;
3083
            }
3084
        }
3085
        tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
3086
        if (do_flush_tlbs)
3087
            tlb_flush(env, 1);
3088
        break;
3089
    case 1:
3090
        RPN = T1 & 0xFFFFFC0F;
3091
        if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
3092
            tlb_flush(env, 1);
3093
        tlb->RPN = RPN;
3094
        break;
3095
    case 2:
3096
        tlb->attr = (tlb->attr & 0x1) | (T1 & 0x0000FF00);
3097
        tlb->prot = tlb->prot & PAGE_VALID;
3098
        if (T1 & 0x1)
3099
            tlb->prot |= PAGE_READ << 4;
3100
        if (T1 & 0x2)
3101
            tlb->prot |= PAGE_WRITE << 4;
3102
        if (T1 & 0x4)
3103
            tlb->prot |= PAGE_EXEC << 4;
3104
        if (T1 & 0x8)
3105
            tlb->prot |= PAGE_READ;
3106
        if (T1 & 0x10)
3107
            tlb->prot |= PAGE_WRITE;
3108
        if (T1 & 0x20)
3109
            tlb->prot |= PAGE_EXEC;
3110
        break;
3111
    }
3112
}
3113

    
3114
void do_440_tlbre (int word)
3115
{
3116
    ppcemb_tlb_t *tlb;
3117
    int size;
3118

    
3119
    T0 &= 0x3F;
3120
    tlb = &env->tlb[T0].tlbe;
3121
    switch (word) {
3122
    default:
3123
        /* Just here to please gcc */
3124
    case 0:
3125
        T0 = tlb->EPN;
3126
        size = booke_page_size_to_tlb(tlb->size);
3127
        if (size < 0 || size > 0xF)
3128
            size = 1;
3129
        T0 |= size << 4;
3130
        if (tlb->attr & 0x1)
3131
            T0 |= 0x100;
3132
        if (tlb->prot & PAGE_VALID)
3133
            T0 |= 0x200;
3134
        env->spr[SPR_440_MMUCR] &= ~0x000000FF;
3135
        env->spr[SPR_440_MMUCR] |= tlb->PID;
3136
        break;
3137
    case 1:
3138
        T0 = tlb->RPN;
3139
        break;
3140
    case 2:
3141
        T0 = tlb->attr & ~0x1;
3142
        if (tlb->prot & (PAGE_READ << 4))
3143
            T0 |= 0x1;
3144
        if (tlb->prot & (PAGE_WRITE << 4))
3145
            T0 |= 0x2;
3146
        if (tlb->prot & (PAGE_EXEC << 4))
3147
            T0 |= 0x4;
3148
        if (tlb->prot & PAGE_READ)
3149
            T0 |= 0x8;
3150
        if (tlb->prot & PAGE_WRITE)
3151
            T0 |= 0x10;
3152
        if (tlb->prot & PAGE_EXEC)
3153
            T0 |= 0x20;
3154
        break;
3155
    }
3156
}
3157
#endif /* !CONFIG_USER_ONLY */