Statistics
| Branch: | Revision:

root / target-ppc / op_helper.c @ a11b8151

History | View | Annotate | Download (76.2 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

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

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

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

    
46
/*****************************************************************************/
47
/* Exceptions processing helpers */
48

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
384
/* shift right arithmetic helper */
385
void do_sraw (void)
386
{
387
    int32_t ret;
388

    
389
    if (likely(!(T1 & 0x20UL))) {
390
        if (likely((uint32_t)T1 != 0)) {
391
            ret = (int32_t)T0 >> (T1 & 0x1fUL);
392
            if (likely(ret >= 0 || ((int32_t)T0 & ((1 << T1) - 1)) == 0)) {
393
                xer_ca = 0;
394
            } else {
395
                xer_ca = 1;
396
            }
397
        } else {
398
            ret = T0;
399
            xer_ca = 0;
400
        }
401
    } else {
402
        ret = (-1) * ((uint32_t)T0 >> 31);
403
        if (likely(ret >= 0 || ((uint32_t)T0 & ~0x80000000UL) == 0)) {
404
            xer_ca = 0;
405
        } else {
406
            xer_ca = 1;
407
        }
408
    }
409
    T0 = ret;
410
}
411

    
412
#if defined(TARGET_PPC64)
413
void do_srad (void)
414
{
415
    int64_t ret;
416

    
417
    if (likely(!(T1 & 0x40UL))) {
418
        if (likely((uint64_t)T1 != 0)) {
419
            ret = (int64_t)T0 >> (T1 & 0x3FUL);
420
            if (likely(ret >= 0 || ((int64_t)T0 & ((1 << T1) - 1)) == 0)) {
421
                xer_ca = 0;
422
            } else {
423
                xer_ca = 1;
424
            }
425
        } else {
426
            ret = T0;
427
            xer_ca = 0;
428
        }
429
    } else {
430
        ret = (-1) * ((uint64_t)T0 >> 63);
431
        if (likely(ret >= 0 || ((uint64_t)T0 & ~0x8000000000000000ULL) == 0)) {
432
            xer_ca = 0;
433
        } else {
434
            xer_ca = 1;
435
        }
436
    }
437
    T0 = ret;
438
}
439
#endif
440

    
441
static always_inline int popcnt (uint32_t val)
442
{
443
    int i;
444

    
445
    for (i = 0; val != 0;)
446
        val = val ^ (val - 1);
447

    
448
    return i;
449
}
450

    
451
void do_popcntb (void)
452
{
453
    uint32_t ret;
454
    int i;
455

    
456
    ret = 0;
457
    for (i = 0; i < 32; i += 8)
458
        ret |= popcnt((T0 >> i) & 0xFF) << i;
459
    T0 = ret;
460
}
461

    
462
#if defined(TARGET_PPC64)
463
void do_popcntb_64 (void)
464
{
465
    uint64_t ret;
466
    int i;
467

    
468
    ret = 0;
469
    for (i = 0; i < 64; i += 8)
470
        ret |= popcnt((T0 >> i) & 0xFF) << i;
471
    T0 = ret;
472
}
473
#endif
474

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

    
484
    u.f = f;
485

    
486
    return u.u >> 63 != 0;
487
}
488

    
489
static always_inline int isden (float f)
490
{
491
    union {
492
        float64 f;
493
        uint64_t u;
494
    } u;
495

    
496
    u.f = f;
497

    
498
    return ((u.u >> 52) & 0x7FF) == 0;
499
}
500

    
501
static always_inline int iszero (float64 f)
502
{
503
    union {
504
        float64 f;
505
        uint64_t u;
506
    } u;
507

    
508
    u.f = f;
509

    
510
    return (u.u & ~0x8000000000000000ULL) == 0;
511
}
512

    
513
static always_inline int isinfinity (float64 f)
514
{
515
    union {
516
        float64 f;
517
        uint64_t u;
518
    } u;
519

    
520
    u.f = f;
521

    
522
    return ((u.u >> 52) & 0x3FF) == 0x3FF &&
523
        (u.u & 0x000FFFFFFFFFFFFFULL) == 0;
524
}
525

    
526
void do_compute_fprf (int set_fprf)
527
{
528
    int isneg;
529

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

    
576
/* Floating-point invalid operations exception */
577
static always_inline void fload_invalid_op_excp (int op)
578
{
579
    int ve;
580

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

    
658
static always_inline void float_zero_divide_excp (void)
659
{
660
    union {
661
        float64 f;
662
        uint64_t u;
663
    } u0, u1;
664

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

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

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

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

    
733
static always_inline void fpscr_set_rounding_mode (void)
734
{
735
    int rnd_type;
736

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

    
760
void do_fpscr_setbit (int bit)
761
{
762
    int prev;
763

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

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

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

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

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

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

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

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

    
1008
void do_fctiw (void)
1009
{
1010
    union {
1011
        double d;
1012
        uint64_t i;
1013
    } p;
1014

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

    
1033
void do_fctiwz (void)
1034
{
1035
    union {
1036
        double d;
1037
        uint64_t i;
1038
    } p;
1039

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

    
1058
#if defined(TARGET_PPC64)
1059
void do_fcfid (void)
1060
{
1061
    union {
1062
        double d;
1063
        uint64_t i;
1064
    } p;
1065

    
1066
    p.d = FT0;
1067
    FT0 = int64_to_float64(p.i, &env->fp_status);
1068
}
1069

    
1070
void do_fctid (void)
1071
{
1072
    union {
1073
        double d;
1074
        uint64_t i;
1075
    } p;
1076

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

    
1089
void do_fctidz (void)
1090
{
1091
    union {
1092
        double d;
1093
        uint64_t i;
1094
    } p;
1095

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

    
1108
#endif
1109

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

    
1126
void do_frin (void)
1127
{
1128
    do_fri(float_round_nearest_even);
1129
}
1130

    
1131
void do_friz (void)
1132
{
1133
    do_fri(float_round_to_zero);
1134
}
1135

    
1136
void do_frip (void)
1137
{
1138
    do_fri(float_round_up);
1139
}
1140

    
1141
void do_frim (void)
1142
{
1143
    do_fri(float_round_down);
1144
}
1145

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

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

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

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

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

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

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

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

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

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

    
1287
void do_fre (void)
1288
{
1289
    union {
1290
        double d;
1291
        uint64_t i;
1292
    } p;
1293

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

    
1319
void do_fres (void)
1320
{
1321
    union {
1322
        double d;
1323
        uint64_t i;
1324
    } p;
1325

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

    
1356
void do_frsqrte (void)
1357
{
1358
    union {
1359
        double d;
1360
        uint64_t i;
1361
    } p;
1362

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

    
1389
void do_fsel (void)
1390
{
1391
    if (!fpisneg(FT0) || iszero(FT0))
1392
        FT0 = FT1;
1393
    else
1394
        FT0 = FT2;
1395
}
1396

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

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

    
1442
#if !defined (CONFIG_USER_ONLY)
1443
void cpu_dump_rfi (target_ulong RA, target_ulong msr);
1444

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

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

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

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

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

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

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

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

    
1569
void do_POWER_div (void)
1570
{
1571
    uint64_t tmp;
1572

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

    
1583
void do_POWER_divo (void)
1584
{
1585
    int64_t tmp;
1586

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

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

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

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

    
1649
void do_POWER_maskg (void)
1650
{
1651
    uint32_t ret;
1652

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

    
1664
void do_POWER_mulo (void)
1665
{
1666
    uint64_t tmp;
1667

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

    
1679
#if !defined (CONFIG_USER_ONLY)
1680
void do_POWER_rac (void)
1681
{
1682
#if 0
1683
    mmu_ctx_t ctx;
1684

1685
    /* We don't have to generate many instances of this instruction,
1686
     * as rac is supervisor only.
1687
     */
1688
    if (get_physical_address(env, &ctx, T0, 0, ACCESS_INT, 1) == 0)
1689
        T0 = ctx.raddr;
1690
#endif
1691
}
1692

    
1693
void do_POWER_rfsvc (void)
1694
{
1695
    __do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1696
}
1697

    
1698
/* PowerPC 601 BAT management helper */
1699
void do_store_601_batu (int nr)
1700
{
1701
    do_store_ibatu(env, nr, (uint32_t)T0);
1702
    env->DBAT[0][nr] = env->IBAT[0][nr];
1703
    env->DBAT[1][nr] = env->IBAT[1][nr];
1704
}
1705
#endif
1706

    
1707
/*****************************************************************************/
1708
/* 602 specific instructions */
1709
/* mfrom is the most crazy instruction ever seen, imho ! */
1710
/* Real implementation uses a ROM table. Do the same */
1711
#define USE_MFROM_ROM_TABLE
1712
void do_op_602_mfrom (void)
1713
{
1714
    if (likely(T0 < 602)) {
1715
#if defined(USE_MFROM_ROM_TABLE)
1716
#include "mfrom_table.c"
1717
        T0 = mfrom_ROM_table[T0];
1718
#else
1719
        double d;
1720
        /* Extremly decomposed:
1721
         *                    -T0 / 256
1722
         * T0 = 256 * log10(10          + 1.0) + 0.5
1723
         */
1724
        d = T0;
1725
        d = float64_div(d, 256, &env->fp_status);
1726
        d = float64_chs(d);
1727
        d = exp10(d); // XXX: use float emulation function
1728
        d = float64_add(d, 1.0, &env->fp_status);
1729
        d = log10(d); // XXX: use float emulation function
1730
        d = float64_mul(d, 256, &env->fp_status);
1731
        d = float64_add(d, 0.5, &env->fp_status);
1732
        T0 = float64_round_to_int(d, &env->fp_status);
1733
#endif
1734
    } else {
1735
        T0 = 0;
1736
    }
1737
}
1738

    
1739
/*****************************************************************************/
1740
/* Embedded PowerPC specific helpers */
1741
void do_405_check_ov (void)
1742
{
1743
    if (likely((((uint32_t)T1 ^ (uint32_t)T2) >> 31) ||
1744
               !(((uint32_t)T0 ^ (uint32_t)T2) >> 31))) {
1745
        xer_ov = 0;
1746
    } else {
1747
        xer_ov = 1;
1748
        xer_so = 1;
1749
    }
1750
}
1751

    
1752
void do_405_check_sat (void)
1753
{
1754
    if (!likely((((uint32_t)T1 ^ (uint32_t)T2) >> 31) ||
1755
                !(((uint32_t)T0 ^ (uint32_t)T2) >> 31))) {
1756
        /* Saturate result */
1757
        if (T2 >> 31) {
1758
            T0 = INT32_MIN;
1759
        } else {
1760
            T0 = INT32_MAX;
1761
        }
1762
    }
1763
}
1764

    
1765
/* XXX: to be improved to check access rights when in user-mode */
1766
void do_load_dcr (void)
1767
{
1768
    target_ulong val;
1769

    
1770
    if (unlikely(env->dcr_env == NULL)) {
1771
        if (loglevel != 0) {
1772
            fprintf(logfile, "No DCR environment\n");
1773
        }
1774
        do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1775
                               POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1776
    } else if (unlikely(ppc_dcr_read(env->dcr_env, T0, &val) != 0)) {
1777
        if (loglevel != 0) {
1778
            fprintf(logfile, "DCR read error %d %03x\n", (int)T0, (int)T0);
1779
        }
1780
        do_raise_exception_err(POWERPC_EXCP_PROGRAM,
1781
                               POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1782
    } else {
1783
        T0 = val;
1784
    }
1785
}
1786

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

    
1804
#if !defined(CONFIG_USER_ONLY)
1805
void do_40x_rfci (void)
1806
{
1807
    __do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1808
             ~((target_ulong)0xFFFF0000), 0);
1809
}
1810

    
1811
void do_rfci (void)
1812
{
1813
    __do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1814
             ~((target_ulong)0x3FFF0000), 0);
1815
}
1816

    
1817
void do_rfdi (void)
1818
{
1819
    __do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1820
             ~((target_ulong)0x3FFF0000), 0);
1821
}
1822

    
1823
void do_rfmci (void)
1824
{
1825
    __do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1826
             ~((target_ulong)0x3FFF0000), 0);
1827
}
1828

    
1829
void do_load_403_pb (int num)
1830
{
1831
    T0 = env->pb[num];
1832
}
1833

    
1834
void do_store_403_pb (int num)
1835
{
1836
    if (likely(env->pb[num] != T0)) {
1837
        env->pb[num] = T0;
1838
        /* Should be optimized */
1839
        tlb_flush(env, 1);
1840
    }
1841
}
1842
#endif
1843

    
1844
/* 440 specific */
1845
void do_440_dlmzb (void)
1846
{
1847
    target_ulong mask;
1848
    int i;
1849

    
1850
    i = 1;
1851
    for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1852
        if ((T0 & mask) == 0)
1853
            goto done;
1854
        i++;
1855
    }
1856
    for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1857
        if ((T1 & mask) == 0)
1858
            break;
1859
        i++;
1860
    }
1861
 done:
1862
    T0 = i;
1863
}
1864

    
1865
#if defined(TARGET_PPCEMB)
1866
/* SPE extension helpers */
1867
/* Use a table to make this quicker */
1868
static uint8_t hbrev[16] = {
1869
    0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1870
    0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1871
};
1872

    
1873
static always_inline uint8_t byte_reverse (uint8_t val)
1874
{
1875
    return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
1876
}
1877

    
1878
static always_inline uint32_t word_reverse (uint32_t val)
1879
{
1880
    return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
1881
        (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
1882
}
1883

    
1884
#define MASKBITS 16 // Random value - to be fixed
1885
void do_brinc (void)
1886
{
1887
    uint32_t a, b, d, mask;
1888

    
1889
    mask = (uint32_t)(-1UL) >> MASKBITS;
1890
    b = T1_64 & mask;
1891
    a = T0_64 & mask;
1892
    d = word_reverse(1 + word_reverse(a | ~mask));
1893
    T0_64 = (T0_64 & ~mask) | (d & mask);
1894
}
1895

    
1896
#define DO_SPE_OP2(name)                                                      \
1897
void do_ev##name (void)                                                       \
1898
{                                                                             \
1899
    T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32, T1_64 >> 32) << 32) |         \
1900
        (uint64_t)_do_e##name(T0_64, T1_64);                                  \
1901
}
1902

    
1903
#define DO_SPE_OP1(name)                                                      \
1904
void do_ev##name (void)                                                       \
1905
{                                                                             \
1906
    T0_64 = ((uint64_t)_do_e##name(T0_64 >> 32) << 32) |                      \
1907
        (uint64_t)_do_e##name(T0_64);                                         \
1908
}
1909

    
1910
/* Fixed-point vector arithmetic */
1911
static always_inline uint32_t _do_eabs (uint32_t val)
1912
{
1913
    if (val != 0x80000000)
1914
        val &= ~0x80000000;
1915

    
1916
    return val;
1917
}
1918

    
1919
static always_inline uint32_t _do_eaddw (uint32_t op1, uint32_t op2)
1920
{
1921
    return op1 + op2;
1922
}
1923

    
1924
static always_inline int _do_ecntlsw (uint32_t val)
1925
{
1926
    if (val & 0x80000000)
1927
        return _do_cntlzw(~val);
1928
    else
1929
        return _do_cntlzw(val);
1930
}
1931

    
1932
static always_inline int _do_ecntlzw (uint32_t val)
1933
{
1934
    return _do_cntlzw(val);
1935
}
1936

    
1937
static always_inline uint32_t _do_eneg (uint32_t val)
1938
{
1939
    if (val != 0x80000000)
1940
        val ^= 0x80000000;
1941

    
1942
    return val;
1943
}
1944

    
1945
static always_inline uint32_t _do_erlw (uint32_t op1, uint32_t op2)
1946
{
1947
    return rotl32(op1, op2);
1948
}
1949

    
1950
static always_inline uint32_t _do_erndw (uint32_t val)
1951
{
1952
    return (val + 0x000080000000) & 0xFFFF0000;
1953
}
1954

    
1955
static always_inline uint32_t _do_eslw (uint32_t op1, uint32_t op2)
1956
{
1957
    /* No error here: 6 bits are used */
1958
    return op1 << (op2 & 0x3F);
1959
}
1960

    
1961
static always_inline int32_t _do_esrws (int32_t op1, uint32_t op2)
1962
{
1963
    /* No error here: 6 bits are used */
1964
    return op1 >> (op2 & 0x3F);
1965
}
1966

    
1967
static always_inline uint32_t _do_esrwu (uint32_t op1, uint32_t op2)
1968
{
1969
    /* No error here: 6 bits are used */
1970
    return op1 >> (op2 & 0x3F);
1971
}
1972

    
1973
static always_inline uint32_t _do_esubfw (uint32_t op1, uint32_t op2)
1974
{
1975
    return op2 - op1;
1976
}
1977

    
1978
/* evabs */
1979
DO_SPE_OP1(abs);
1980
/* evaddw */
1981
DO_SPE_OP2(addw);
1982
/* evcntlsw */
1983
DO_SPE_OP1(cntlsw);
1984
/* evcntlzw */
1985
DO_SPE_OP1(cntlzw);
1986
/* evneg */
1987
DO_SPE_OP1(neg);
1988
/* evrlw */
1989
DO_SPE_OP2(rlw);
1990
/* evrnd */
1991
DO_SPE_OP1(rndw);
1992
/* evslw */
1993
DO_SPE_OP2(slw);
1994
/* evsrws */
1995
DO_SPE_OP2(srws);
1996
/* evsrwu */
1997
DO_SPE_OP2(srwu);
1998
/* evsubfw */
1999
DO_SPE_OP2(subfw);
2000

    
2001
/* evsel is a little bit more complicated... */
2002
static always_inline uint32_t _do_esel (uint32_t op1, uint32_t op2, int n)
2003
{
2004
    if (n)
2005
        return op1;
2006
    else
2007
        return op2;
2008
}
2009

    
2010
void do_evsel (void)
2011
{
2012
    T0_64 = ((uint64_t)_do_esel(T0_64 >> 32, T1_64 >> 32, T0 >> 3) << 32) |
2013
        (uint64_t)_do_esel(T0_64, T1_64, (T0 >> 2) & 1);
2014
}
2015

    
2016
/* Fixed-point vector comparisons */
2017
#define DO_SPE_CMP(name)                                                      \
2018
void do_ev##name (void)                                                       \
2019
{                                                                             \
2020
    T0 = _do_evcmp_merge((uint64_t)_do_e##name(T0_64 >> 32,                   \
2021
                                               T1_64 >> 32) << 32,            \
2022
                         _do_e##name(T0_64, T1_64));                          \
2023
}
2024

    
2025
static always_inline uint32_t _do_evcmp_merge (int t0, int t1)
2026
{
2027
    return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
2028
}
2029
static always_inline int _do_ecmpeq (uint32_t op1, uint32_t op2)
2030
{
2031
    return op1 == op2 ? 1 : 0;
2032
}
2033

    
2034
static always_inline int _do_ecmpgts (int32_t op1, int32_t op2)
2035
{
2036
    return op1 > op2 ? 1 : 0;
2037
}
2038

    
2039
static always_inline int _do_ecmpgtu (uint32_t op1, uint32_t op2)
2040
{
2041
    return op1 > op2 ? 1 : 0;
2042
}
2043

    
2044
static always_inline int _do_ecmplts (int32_t op1, int32_t op2)
2045
{
2046
    return op1 < op2 ? 1 : 0;
2047
}
2048

    
2049
static always_inline int _do_ecmpltu (uint32_t op1, uint32_t op2)
2050
{
2051
    return op1 < op2 ? 1 : 0;
2052
}
2053

    
2054
/* evcmpeq */
2055
DO_SPE_CMP(cmpeq);
2056
/* evcmpgts */
2057
DO_SPE_CMP(cmpgts);
2058
/* evcmpgtu */
2059
DO_SPE_CMP(cmpgtu);
2060
/* evcmplts */
2061
DO_SPE_CMP(cmplts);
2062
/* evcmpltu */
2063
DO_SPE_CMP(cmpltu);
2064

    
2065
/* Single precision floating-point conversions from/to integer */
2066
static always_inline uint32_t _do_efscfsi (int32_t val)
2067
{
2068
    union {
2069
        uint32_t u;
2070
        float32 f;
2071
    } u;
2072

    
2073
    u.f = int32_to_float32(val, &env->spe_status);
2074

    
2075
    return u.u;
2076
}
2077

    
2078
static always_inline uint32_t _do_efscfui (uint32_t val)
2079
{
2080
    union {
2081
        uint32_t u;
2082
        float32 f;
2083
    } u;
2084

    
2085
    u.f = uint32_to_float32(val, &env->spe_status);
2086

    
2087
    return u.u;
2088
}
2089

    
2090
static always_inline int32_t _do_efsctsi (uint32_t val)
2091
{
2092
    union {
2093
        int32_t u;
2094
        float32 f;
2095
    } u;
2096

    
2097
    u.u = val;
2098
    /* NaN are not treated the same way IEEE 754 does */
2099
    if (unlikely(isnan(u.f)))
2100
        return 0;
2101

    
2102
    return float32_to_int32(u.f, &env->spe_status);
2103
}
2104

    
2105
static always_inline uint32_t _do_efsctui (uint32_t val)
2106
{
2107
    union {
2108
        int32_t u;
2109
        float32 f;
2110
    } u;
2111

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

    
2117
    return float32_to_uint32(u.f, &env->spe_status);
2118
}
2119

    
2120
static always_inline int32_t _do_efsctsiz (uint32_t val)
2121
{
2122
    union {
2123
        int32_t u;
2124
        float32 f;
2125
    } u;
2126

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

    
2132
    return float32_to_int32_round_to_zero(u.f, &env->spe_status);
2133
}
2134

    
2135
static always_inline uint32_t _do_efsctuiz (uint32_t val)
2136
{
2137
    union {
2138
        int32_t u;
2139
        float32 f;
2140
    } u;
2141

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

    
2147
    return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
2148
}
2149

    
2150
void do_efscfsi (void)
2151
{
2152
    T0_64 = _do_efscfsi(T0_64);
2153
}
2154

    
2155
void do_efscfui (void)
2156
{
2157
    T0_64 = _do_efscfui(T0_64);
2158
}
2159

    
2160
void do_efsctsi (void)
2161
{
2162
    T0_64 = _do_efsctsi(T0_64);
2163
}
2164

    
2165
void do_efsctui (void)
2166
{
2167
    T0_64 = _do_efsctui(T0_64);
2168
}
2169

    
2170
void do_efsctsiz (void)
2171
{
2172
    T0_64 = _do_efsctsiz(T0_64);
2173
}
2174

    
2175
void do_efsctuiz (void)
2176
{
2177
    T0_64 = _do_efsctuiz(T0_64);
2178
}
2179

    
2180
/* Single precision floating-point conversion to/from fractional */
2181
static always_inline uint32_t _do_efscfsf (uint32_t val)
2182
{
2183
    union {
2184
        uint32_t u;
2185
        float32 f;
2186
    } u;
2187
    float32 tmp;
2188

    
2189
    u.f = int32_to_float32(val, &env->spe_status);
2190
    tmp = int64_to_float32(1ULL << 32, &env->spe_status);
2191
    u.f = float32_div(u.f, tmp, &env->spe_status);
2192

    
2193
    return u.u;
2194
}
2195

    
2196
static always_inline uint32_t _do_efscfuf (uint32_t val)
2197
{
2198
    union {
2199
        uint32_t u;
2200
        float32 f;
2201
    } u;
2202
    float32 tmp;
2203

    
2204
    u.f = uint32_to_float32(val, &env->spe_status);
2205
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2206
    u.f = float32_div(u.f, tmp, &env->spe_status);
2207

    
2208
    return u.u;
2209
}
2210

    
2211
static always_inline int32_t _do_efsctsf (uint32_t val)
2212
{
2213
    union {
2214
        int32_t u;
2215
        float32 f;
2216
    } u;
2217
    float32 tmp;
2218

    
2219
    u.u = val;
2220
    /* NaN are not treated the same way IEEE 754 does */
2221
    if (unlikely(isnan(u.f)))
2222
        return 0;
2223
    tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2224
    u.f = float32_mul(u.f, tmp, &env->spe_status);
2225

    
2226
    return float32_to_int32(u.f, &env->spe_status);
2227
}
2228

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

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

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

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

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

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

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

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

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

    
2283
void do_efscfsf (void)
2284
{
2285
    T0_64 = _do_efscfsf(T0_64);
2286
}
2287

    
2288
void do_efscfuf (void)
2289
{
2290
    T0_64 = _do_efscfuf(T0_64);
2291
}
2292

    
2293
void do_efsctsf (void)
2294
{
2295
    T0_64 = _do_efsctsf(T0_64);
2296
}
2297

    
2298
void do_efsctuf (void)
2299
{
2300
    T0_64 = _do_efsctuf(T0_64);
2301
}
2302

    
2303
void do_efsctsfz (void)
2304
{
2305
    T0_64 = _do_efsctsfz(T0_64);
2306
}
2307

    
2308
void do_efsctufz (void)
2309
{
2310
    T0_64 = _do_efsctufz(T0_64);
2311
}
2312

    
2313
/* Double precision floating point helpers */
2314
static always_inline int _do_efdcmplt (uint64_t op1, uint64_t op2)
2315
{
2316
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2317
    return _do_efdtstlt(op1, op2);
2318
}
2319

    
2320
static always_inline int _do_efdcmpgt (uint64_t op1, uint64_t op2)
2321
{
2322
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2323
    return _do_efdtstgt(op1, op2);
2324
}
2325

    
2326
static always_inline int _do_efdcmpeq (uint64_t op1, uint64_t op2)
2327
{
2328
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2329
    return _do_efdtsteq(op1, op2);
2330
}
2331

    
2332
void do_efdcmplt (void)
2333
{
2334
    T0 = _do_efdcmplt(T0_64, T1_64);
2335
}
2336

    
2337
void do_efdcmpgt (void)
2338
{
2339
    T0 = _do_efdcmpgt(T0_64, T1_64);
2340
}
2341

    
2342
void do_efdcmpeq (void)
2343
{
2344
    T0 = _do_efdcmpeq(T0_64, T1_64);
2345
}
2346

    
2347
/* Double precision floating-point conversion to/from integer */
2348
static always_inline uint64_t _do_efdcfsi (int64_t val)
2349
{
2350
    union {
2351
        uint64_t u;
2352
        float64 f;
2353
    } u;
2354

    
2355
    u.f = int64_to_float64(val, &env->spe_status);
2356

    
2357
    return u.u;
2358
}
2359

    
2360
static always_inline uint64_t _do_efdcfui (uint64_t val)
2361
{
2362
    union {
2363
        uint64_t u;
2364
        float64 f;
2365
    } u;
2366

    
2367
    u.f = uint64_to_float64(val, &env->spe_status);
2368

    
2369
    return u.u;
2370
}
2371

    
2372
static always_inline int64_t _do_efdctsi (uint64_t val)
2373
{
2374
    union {
2375
        int64_t u;
2376
        float64 f;
2377
    } u;
2378

    
2379
    u.u = val;
2380
    /* NaN are not treated the same way IEEE 754 does */
2381
    if (unlikely(isnan(u.f)))
2382
        return 0;
2383

    
2384
    return float64_to_int64(u.f, &env->spe_status);
2385
}
2386

    
2387
static always_inline uint64_t _do_efdctui (uint64_t val)
2388
{
2389
    union {
2390
        int64_t u;
2391
        float64 f;
2392
    } u;
2393

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

    
2399
    return float64_to_uint64(u.f, &env->spe_status);
2400
}
2401

    
2402
static always_inline int64_t _do_efdctsiz (uint64_t val)
2403
{
2404
    union {
2405
        int64_t u;
2406
        float64 f;
2407
    } u;
2408

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

    
2414
    return float64_to_int64_round_to_zero(u.f, &env->spe_status);
2415
}
2416

    
2417
static always_inline uint64_t _do_efdctuiz (uint64_t val)
2418
{
2419
    union {
2420
        int64_t u;
2421
        float64 f;
2422
    } u;
2423

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

    
2429
    return float64_to_uint64_round_to_zero(u.f, &env->spe_status);
2430
}
2431

    
2432
void do_efdcfsi (void)
2433
{
2434
    T0_64 = _do_efdcfsi(T0_64);
2435
}
2436

    
2437
void do_efdcfui (void)
2438
{
2439
    T0_64 = _do_efdcfui(T0_64);
2440
}
2441

    
2442
void do_efdctsi (void)
2443
{
2444
    T0_64 = _do_efdctsi(T0_64);
2445
}
2446

    
2447
void do_efdctui (void)
2448
{
2449
    T0_64 = _do_efdctui(T0_64);
2450
}
2451

    
2452
void do_efdctsiz (void)
2453
{
2454
    T0_64 = _do_efdctsiz(T0_64);
2455
}
2456

    
2457
void do_efdctuiz (void)
2458
{
2459
    T0_64 = _do_efdctuiz(T0_64);
2460
}
2461

    
2462
/* Double precision floating-point conversion to/from fractional */
2463
static always_inline uint64_t _do_efdcfsf (int64_t val)
2464
{
2465
    union {
2466
        uint64_t u;
2467
        float64 f;
2468
    } u;
2469
    float64 tmp;
2470

    
2471
    u.f = int32_to_float64(val, &env->spe_status);
2472
    tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2473
    u.f = float64_div(u.f, tmp, &env->spe_status);
2474

    
2475
    return u.u;
2476
}
2477

    
2478
static always_inline uint64_t _do_efdcfuf (uint64_t val)
2479
{
2480
    union {
2481
        uint64_t u;
2482
        float64 f;
2483
    } u;
2484
    float64 tmp;
2485

    
2486
    u.f = uint32_to_float64(val, &env->spe_status);
2487
    tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2488
    u.f = float64_div(u.f, tmp, &env->spe_status);
2489

    
2490
    return u.u;
2491
}
2492

    
2493
static always_inline int64_t _do_efdctsf (uint64_t val)
2494
{
2495
    union {
2496
        int64_t u;
2497
        float64 f;
2498
    } u;
2499
    float64 tmp;
2500

    
2501
    u.u = val;
2502
    /* NaN are not treated the same way IEEE 754 does */
2503
    if (unlikely(isnan(u.f)))
2504
        return 0;
2505
    tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2506
    u.f = float64_mul(u.f, tmp, &env->spe_status);
2507

    
2508
    return float64_to_int32(u.f, &env->spe_status);
2509
}
2510

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

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

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

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

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

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

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

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

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

    
2565
void do_efdcfsf (void)
2566
{
2567
    T0_64 = _do_efdcfsf(T0_64);
2568
}
2569

    
2570
void do_efdcfuf (void)
2571
{
2572
    T0_64 = _do_efdcfuf(T0_64);
2573
}
2574

    
2575
void do_efdctsf (void)
2576
{
2577
    T0_64 = _do_efdctsf(T0_64);
2578
}
2579

    
2580
void do_efdctuf (void)
2581
{
2582
    T0_64 = _do_efdctuf(T0_64);
2583
}
2584

    
2585
void do_efdctsfz (void)
2586
{
2587
    T0_64 = _do_efdctsfz(T0_64);
2588
}
2589

    
2590
void do_efdctufz (void)
2591
{
2592
    T0_64 = _do_efdctufz(T0_64);
2593
}
2594

    
2595
/* Floating point conversion between single and double precision */
2596
static always_inline uint32_t _do_efscfd (uint64_t val)
2597
{
2598
    union {
2599
        uint64_t u;
2600
        float64 f;
2601
    } u1;
2602
    union {
2603
        uint32_t u;
2604
        float32 f;
2605
    } u2;
2606

    
2607
    u1.u = val;
2608
    u2.f = float64_to_float32(u1.f, &env->spe_status);
2609

    
2610
    return u2.u;
2611
}
2612

    
2613
static always_inline uint64_t _do_efdcfs (uint32_t val)
2614
{
2615
    union {
2616
        uint64_t u;
2617
        float64 f;
2618
    } u2;
2619
    union {
2620
        uint32_t u;
2621
        float32 f;
2622
    } u1;
2623

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

    
2627
    return u2.u;
2628
}
2629

    
2630
void do_efscfd (void)
2631
{
2632
    T0_64 = _do_efscfd(T0_64);
2633
}
2634

    
2635
void do_efdcfs (void)
2636
{
2637
    T0_64 = _do_efdcfs(T0_64);
2638
}
2639

    
2640
/* Single precision fixed-point vector arithmetic */
2641
/* evfsabs */
2642
DO_SPE_OP1(fsabs);
2643
/* evfsnabs */
2644
DO_SPE_OP1(fsnabs);
2645
/* evfsneg */
2646
DO_SPE_OP1(fsneg);
2647
/* evfsadd */
2648
DO_SPE_OP2(fsadd);
2649
/* evfssub */
2650
DO_SPE_OP2(fssub);
2651
/* evfsmul */
2652
DO_SPE_OP2(fsmul);
2653
/* evfsdiv */
2654
DO_SPE_OP2(fsdiv);
2655

    
2656
/* Single-precision floating-point comparisons */
2657
static always_inline int _do_efscmplt (uint32_t op1, uint32_t op2)
2658
{
2659
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2660
    return _do_efststlt(op1, op2);
2661
}
2662

    
2663
static always_inline int _do_efscmpgt (uint32_t op1, uint32_t op2)
2664
{
2665
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2666
    return _do_efststgt(op1, op2);
2667
}
2668

    
2669
static always_inline int _do_efscmpeq (uint32_t op1, uint32_t op2)
2670
{
2671
    /* XXX: TODO: test special values (NaN, infinites, ...) */
2672
    return _do_efststeq(op1, op2);
2673
}
2674

    
2675
void do_efscmplt (void)
2676
{
2677
    T0 = _do_efscmplt(T0_64, T1_64);
2678
}
2679

    
2680
void do_efscmpgt (void)
2681
{
2682
    T0 = _do_efscmpgt(T0_64, T1_64);
2683
}
2684

    
2685
void do_efscmpeq (void)
2686
{
2687
    T0 = _do_efscmpeq(T0_64, T1_64);
2688
}
2689

    
2690
/* Single-precision floating-point vector comparisons */
2691
/* evfscmplt */
2692
DO_SPE_CMP(fscmplt);
2693
/* evfscmpgt */
2694
DO_SPE_CMP(fscmpgt);
2695
/* evfscmpeq */
2696
DO_SPE_CMP(fscmpeq);
2697
/* evfststlt */
2698
DO_SPE_CMP(fststlt);
2699
/* evfststgt */
2700
DO_SPE_CMP(fststgt);
2701
/* evfststeq */
2702
DO_SPE_CMP(fststeq);
2703

    
2704
/* Single-precision floating-point vector conversions */
2705
/* evfscfsi */
2706
DO_SPE_OP1(fscfsi);
2707
/* evfscfui */
2708
DO_SPE_OP1(fscfui);
2709
/* evfscfuf */
2710
DO_SPE_OP1(fscfuf);
2711
/* evfscfsf */
2712
DO_SPE_OP1(fscfsf);
2713
/* evfsctsi */
2714
DO_SPE_OP1(fsctsi);
2715
/* evfsctui */
2716
DO_SPE_OP1(fsctui);
2717
/* evfsctsiz */
2718
DO_SPE_OP1(fsctsiz);
2719
/* evfsctuiz */
2720
DO_SPE_OP1(fsctuiz);
2721
/* evfsctsf */
2722
DO_SPE_OP1(fsctsf);
2723
/* evfsctuf */
2724
DO_SPE_OP1(fsctuf);
2725
#endif /* defined(TARGET_PPCEMB) */
2726

    
2727
/*****************************************************************************/
2728
/* Softmmu support */
2729
#if !defined (CONFIG_USER_ONLY)
2730

    
2731
#define MMUSUFFIX _mmu
2732
#define GETPC() (__builtin_return_address(0))
2733

    
2734
#define SHIFT 0
2735
#include "softmmu_template.h"
2736

    
2737
#define SHIFT 1
2738
#include "softmmu_template.h"
2739

    
2740
#define SHIFT 2
2741
#include "softmmu_template.h"
2742

    
2743
#define SHIFT 3
2744
#include "softmmu_template.h"
2745

    
2746
/* try to fill the TLB and return an exception if error. If retaddr is
2747
   NULL, it means that the function was called in C code (i.e. not
2748
   from generated code or from helper.c) */
2749
/* XXX: fix it to restore all registers */
2750
void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2751
{
2752
    TranslationBlock *tb;
2753
    CPUState *saved_env;
2754
    target_phys_addr_t pc;
2755
    int ret;
2756

    
2757
    /* XXX: hack to restore env in all cases, even if not called from
2758
       generated code */
2759
    saved_env = env;
2760
    env = cpu_single_env;
2761
    ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2762
    if (unlikely(ret != 0)) {
2763
        if (likely(retaddr)) {
2764
            /* now we have a real cpu fault */
2765
            pc = (target_phys_addr_t)(unsigned long)retaddr;
2766
            tb = tb_find_pc(pc);
2767
            if (likely(tb)) {
2768
                /* the PC is inside the translated code. It means that we have
2769
                   a virtual CPU fault */
2770
                cpu_restore_state(tb, env, pc, NULL);
2771
            }
2772
        }
2773
        do_raise_exception_err(env->exception_index, env->error_code);
2774
    }
2775
    env = saved_env;
2776
}
2777

    
2778
/* Software driven TLBs management */
2779
/* PowerPC 602/603 software TLB load instructions helpers */
2780
void do_load_6xx_tlb (int is_code)
2781
{
2782
    target_ulong RPN, CMP, EPN;
2783
    int way;
2784

    
2785
    RPN = env->spr[SPR_RPA];
2786
    if (is_code) {
2787
        CMP = env->spr[SPR_ICMP];
2788
        EPN = env->spr[SPR_IMISS];
2789
    } else {
2790
        CMP = env->spr[SPR_DCMP];
2791
        EPN = env->spr[SPR_DMISS];
2792
    }
2793
    way = (env->spr[SPR_SRR1] >> 17) & 1;
2794
#if defined (DEBUG_SOFTWARE_TLB)
2795
    if (loglevel != 0) {
2796
        fprintf(logfile, "%s: EPN %08lx %08lx PTE0 %08lx PTE1 %08lx way %d\n",
2797
                __func__, (unsigned long)T0, (unsigned long)EPN,
2798
                (unsigned long)CMP, (unsigned long)RPN, way);
2799
    }
2800
#endif
2801
    /* Store this TLB */
2802
    ppc6xx_tlb_store(env, (uint32_t)(T0 & TARGET_PAGE_MASK),
2803
                     way, is_code, CMP, RPN);
2804
}
2805

    
2806
void do_load_74xx_tlb (int is_code)
2807
{
2808
    target_ulong RPN, CMP, EPN;
2809
    int way;
2810

    
2811
    RPN = env->spr[SPR_PTELO];
2812
    CMP = env->spr[SPR_PTEHI];
2813
    EPN = env->spr[SPR_TLBMISS] & ~0x3;
2814
    way = env->spr[SPR_TLBMISS] & 0x3;
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
static always_inline target_ulong booke_tlb_to_page_size (int size)
2828
{
2829
    return 1024 << (2 * size);
2830
}
2831

    
2832
static always_inline int booke_page_size_to_tlb (target_ulong page_size)
2833
{
2834
    int size;
2835

    
2836
    switch (page_size) {
2837
    case 0x00000400UL:
2838
        size = 0x0;
2839
        break;
2840
    case 0x00001000UL:
2841
        size = 0x1;
2842
        break;
2843
    case 0x00004000UL:
2844
        size = 0x2;
2845
        break;
2846
    case 0x00010000UL:
2847
        size = 0x3;
2848
        break;
2849
    case 0x00040000UL:
2850
        size = 0x4;
2851
        break;
2852
    case 0x00100000UL:
2853
        size = 0x5;
2854
        break;
2855
    case 0x00400000UL:
2856
        size = 0x6;
2857
        break;
2858
    case 0x01000000UL:
2859
        size = 0x7;
2860
        break;
2861
    case 0x04000000UL:
2862
        size = 0x8;
2863
        break;
2864
    case 0x10000000UL:
2865
        size = 0x9;
2866
        break;
2867
    case 0x40000000UL:
2868
        size = 0xA;
2869
        break;
2870
#if defined (TARGET_PPC64)
2871
    case 0x000100000000ULL:
2872
        size = 0xB;
2873
        break;
2874
    case 0x000400000000ULL:
2875
        size = 0xC;
2876
        break;
2877
    case 0x001000000000ULL:
2878
        size = 0xD;
2879
        break;
2880
    case 0x004000000000ULL:
2881
        size = 0xE;
2882
        break;
2883
    case 0x010000000000ULL:
2884
        size = 0xF;
2885
        break;
2886
#endif
2887
    default:
2888
        size = -1;
2889
        break;
2890
    }
2891

    
2892
    return size;
2893
}
2894

    
2895
/* Helpers for 4xx TLB management */
2896
void do_4xx_tlbre_lo (void)
2897
{
2898
    ppcemb_tlb_t *tlb;
2899
    int size;
2900

    
2901
    T0 &= 0x3F;
2902
    tlb = &env->tlb[T0].tlbe;
2903
    T0 = tlb->EPN;
2904
    if (tlb->prot & PAGE_VALID)
2905
        T0 |= 0x400;
2906
    size = booke_page_size_to_tlb(tlb->size);
2907
    if (size < 0 || size > 0x7)
2908
        size = 1;
2909
    T0 |= size << 7;
2910
    env->spr[SPR_40x_PID] = tlb->PID;
2911
}
2912

    
2913
void do_4xx_tlbre_hi (void)
2914
{
2915
    ppcemb_tlb_t *tlb;
2916

    
2917
    T0 &= 0x3F;
2918
    tlb = &env->tlb[T0].tlbe;
2919
    T0 = tlb->RPN;
2920
    if (tlb->prot & PAGE_EXEC)
2921
        T0 |= 0x200;
2922
    if (tlb->prot & PAGE_WRITE)
2923
        T0 |= 0x100;
2924
}
2925

    
2926
void do_4xx_tlbwe_hi (void)
2927
{
2928
    ppcemb_tlb_t *tlb;
2929
    target_ulong page, end;
2930

    
2931
#if defined (DEBUG_SOFTWARE_TLB)
2932
    if (loglevel != 0) {
2933
        fprintf(logfile, "%s T0 " REGX " T1 " REGX "\n", __func__, T0, T1);
2934
    }
2935
#endif
2936
    T0 &= 0x3F;
2937
    tlb = &env->tlb[T0].tlbe;
2938
    /* Invalidate previous TLB (if it's valid) */
2939
    if (tlb->prot & PAGE_VALID) {
2940
        end = tlb->EPN + tlb->size;
2941
#if defined (DEBUG_SOFTWARE_TLB)
2942
        if (loglevel != 0) {
2943
            fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2944
                    " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2945
        }
2946
#endif
2947
        for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2948
            tlb_flush_page(env, page);
2949
    }
2950
    tlb->size = booke_tlb_to_page_size((T1 >> 7) & 0x7);
2951
    /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2952
     * If this ever occurs, one should use the ppcemb target instead
2953
     * of the ppc or ppc64 one
2954
     */
2955
    if ((T1 & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
2956
        cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2957
                  "are not supported (%d)\n",
2958
                  tlb->size, TARGET_PAGE_SIZE, (int)((T1 >> 7) & 0x7));
2959
    }
2960
    tlb->EPN = T1 & ~(tlb->size - 1);
2961
    if (T1 & 0x40)
2962
        tlb->prot |= PAGE_VALID;
2963
    else
2964
        tlb->prot &= ~PAGE_VALID;
2965
    if (T1 & 0x20) {
2966
        /* XXX: TO BE FIXED */
2967
        cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2968
    }
2969
    tlb->PID = env->spr[SPR_40x_PID]; /* PID */
2970
    tlb->attr = T1 & 0xFF;
2971
#if defined (DEBUG_SOFTWARE_TLB)
2972
    if (loglevel != 0) {
2973
        fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2974
                " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2975
                (int)T0, tlb->RPN, tlb->EPN, tlb->size,
2976
                tlb->prot & PAGE_READ ? 'r' : '-',
2977
                tlb->prot & PAGE_WRITE ? 'w' : '-',
2978
                tlb->prot & PAGE_EXEC ? 'x' : '-',
2979
                tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2980
    }
2981
#endif
2982
    /* Invalidate new TLB (if valid) */
2983
    if (tlb->prot & PAGE_VALID) {
2984
        end = tlb->EPN + tlb->size;
2985
#if defined (DEBUG_SOFTWARE_TLB)
2986
        if (loglevel != 0) {
2987
            fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
2988
                    " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2989
        }
2990
#endif
2991
        for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2992
            tlb_flush_page(env, page);
2993
    }
2994
}
2995

    
2996
void do_4xx_tlbwe_lo (void)
2997
{
2998
    ppcemb_tlb_t *tlb;
2999

    
3000
#if defined (DEBUG_SOFTWARE_TLB)
3001
    if (loglevel != 0) {
3002
        fprintf(logfile, "%s T0 " REGX " T1 " REGX "\n", __func__, T0, T1);
3003
    }
3004
#endif
3005
    T0 &= 0x3F;
3006
    tlb = &env->tlb[T0].tlbe;
3007
    tlb->RPN = T1 & 0xFFFFFC00;
3008
    tlb->prot = PAGE_READ;
3009
    if (T1 & 0x200)
3010
        tlb->prot |= PAGE_EXEC;
3011
    if (T1 & 0x100)
3012
        tlb->prot |= PAGE_WRITE;
3013
#if defined (DEBUG_SOFTWARE_TLB)
3014
    if (loglevel != 0) {
3015
        fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
3016
                " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
3017
                (int)T0, tlb->RPN, tlb->EPN, tlb->size,
3018
                tlb->prot & PAGE_READ ? 'r' : '-',
3019
                tlb->prot & PAGE_WRITE ? 'w' : '-',
3020
                tlb->prot & PAGE_EXEC ? 'x' : '-',
3021
                tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
3022
    }
3023
#endif
3024
}
3025

    
3026
/* PowerPC 440 TLB management */
3027
void do_440_tlbwe (int word)
3028
{
3029
    ppcemb_tlb_t *tlb;
3030
    target_ulong EPN, RPN, size;
3031
    int do_flush_tlbs;
3032

    
3033
#if defined (DEBUG_SOFTWARE_TLB)
3034
    if (loglevel != 0) {
3035
        fprintf(logfile, "%s word %d T0 " REGX " T1 " REGX "\n",
3036
                __func__, word, T0, T1);
3037
    }
3038
#endif
3039
    do_flush_tlbs = 0;
3040
    T0 &= 0x3F;
3041
    tlb = &env->tlb[T0].tlbe;
3042
    switch (word) {
3043
    default:
3044
        /* Just here to please gcc */
3045
    case 0:
3046
        EPN = T1 & 0xFFFFFC00;
3047
        if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
3048
            do_flush_tlbs = 1;
3049
        tlb->EPN = EPN;
3050
        size = booke_tlb_to_page_size((T1 >> 4) & 0xF);
3051
        if ((tlb->prot & PAGE_VALID) && tlb->size < size)
3052
            do_flush_tlbs = 1;
3053
        tlb->size = size;
3054
        tlb->attr &= ~0x1;
3055
        tlb->attr |= (T1 >> 8) & 1;
3056
        if (T1 & 0x200) {
3057
            tlb->prot |= PAGE_VALID;
3058
        } else {
3059
            if (tlb->prot & PAGE_VALID) {
3060
                tlb->prot &= ~PAGE_VALID;
3061
                do_flush_tlbs = 1;
3062
            }
3063
        }
3064
        tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
3065
        if (do_flush_tlbs)
3066
            tlb_flush(env, 1);
3067
        break;
3068
    case 1:
3069
        RPN = T1 & 0xFFFFFC0F;
3070
        if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
3071
            tlb_flush(env, 1);
3072
        tlb->RPN = RPN;
3073
        break;
3074
    case 2:
3075
        tlb->attr = (tlb->attr & 0x1) | (T1 & 0x0000FF00);
3076
        tlb->prot = tlb->prot & PAGE_VALID;
3077
        if (T1 & 0x1)
3078
            tlb->prot |= PAGE_READ << 4;
3079
        if (T1 & 0x2)
3080
            tlb->prot |= PAGE_WRITE << 4;
3081
        if (T1 & 0x4)
3082
            tlb->prot |= PAGE_EXEC << 4;
3083
        if (T1 & 0x8)
3084
            tlb->prot |= PAGE_READ;
3085
        if (T1 & 0x10)
3086
            tlb->prot |= PAGE_WRITE;
3087
        if (T1 & 0x20)
3088
            tlb->prot |= PAGE_EXEC;
3089
        break;
3090
    }
3091
}
3092

    
3093
void do_440_tlbre (int word)
3094
{
3095
    ppcemb_tlb_t *tlb;
3096
    int size;
3097

    
3098
    T0 &= 0x3F;
3099
    tlb = &env->tlb[T0].tlbe;
3100
    switch (word) {
3101
    default:
3102
        /* Just here to please gcc */
3103
    case 0:
3104
        T0 = tlb->EPN;
3105
        size = booke_page_size_to_tlb(tlb->size);
3106
        if (size < 0 || size > 0xF)
3107
            size = 1;
3108
        T0 |= size << 4;
3109
        if (tlb->attr & 0x1)
3110
            T0 |= 0x100;
3111
        if (tlb->prot & PAGE_VALID)
3112
            T0 |= 0x200;
3113
        env->spr[SPR_440_MMUCR] &= ~0x000000FF;
3114
        env->spr[SPR_440_MMUCR] |= tlb->PID;
3115
        break;
3116
    case 1:
3117
        T0 = tlb->RPN;
3118
        break;
3119
    case 2:
3120
        T0 = tlb->attr & ~0x1;
3121
        if (tlb->prot & (PAGE_READ << 4))
3122
            T0 |= 0x1;
3123
        if (tlb->prot & (PAGE_WRITE << 4))
3124
            T0 |= 0x2;
3125
        if (tlb->prot & (PAGE_EXEC << 4))
3126
            T0 |= 0x4;
3127
        if (tlb->prot & PAGE_READ)
3128
            T0 |= 0x8;
3129
        if (tlb->prot & PAGE_WRITE)
3130
            T0 |= 0x10;
3131
        if (tlb->prot & PAGE_EXEC)
3132
            T0 |= 0x20;
3133
        break;
3134
    }
3135
}
3136
#endif /* !CONFIG_USER_ONLY */