Add vspltis{b,h,w} instructions
[qemu] / target-ppc / op_helper.c
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., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19  */
20 #include <string.h>
21 #include "exec.h"
22 #include "host-utils.h"
23 #include "helper.h"
24
25 #include "helper_regs.h"
26
27 //#define DEBUG_OP
28 //#define DEBUG_EXCEPTIONS
29 //#define DEBUG_SOFTWARE_TLB
30
31 /*****************************************************************************/
32 /* Exceptions processing helpers */
33
34 void helper_raise_exception_err (uint32_t exception, uint32_t error_code)
35 {
36 #if 0
37     printf("Raise exception %3x code : %d\n", exception, error_code);
38 #endif
39     env->exception_index = exception;
40     env->error_code = error_code;
41     cpu_loop_exit();
42 }
43
44 void helper_raise_exception (uint32_t exception)
45 {
46     helper_raise_exception_err(exception, 0);
47 }
48
49 /*****************************************************************************/
50 /* Registers load and stores */
51 target_ulong helper_load_cr (void)
52 {
53     return (env->crf[0] << 28) |
54            (env->crf[1] << 24) |
55            (env->crf[2] << 20) |
56            (env->crf[3] << 16) |
57            (env->crf[4] << 12) |
58            (env->crf[5] << 8) |
59            (env->crf[6] << 4) |
60            (env->crf[7] << 0);
61 }
62
63 void helper_store_cr (target_ulong val, uint32_t mask)
64 {
65     int i, sh;
66
67     for (i = 0, sh = 7; i < 8; i++, sh--) {
68         if (mask & (1 << sh))
69             env->crf[i] = (val >> (sh * 4)) & 0xFUL;
70     }
71 }
72
73 /*****************************************************************************/
74 /* SPR accesses */
75 void helper_load_dump_spr (uint32_t sprn)
76 {
77     if (loglevel != 0) {
78         fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
79                 sprn, sprn, env->spr[sprn]);
80     }
81 }
82
83 void helper_store_dump_spr (uint32_t sprn)
84 {
85     if (loglevel != 0) {
86         fprintf(logfile, "Write SPR %d %03x <= " ADDRX "\n",
87                 sprn, sprn, env->spr[sprn]);
88     }
89 }
90
91 target_ulong helper_load_tbl (void)
92 {
93     return cpu_ppc_load_tbl(env);
94 }
95
96 target_ulong helper_load_tbu (void)
97 {
98     return cpu_ppc_load_tbu(env);
99 }
100
101 target_ulong helper_load_atbl (void)
102 {
103     return cpu_ppc_load_atbl(env);
104 }
105
106 target_ulong helper_load_atbu (void)
107 {
108     return cpu_ppc_load_atbu(env);
109 }
110
111 target_ulong helper_load_601_rtcl (void)
112 {
113     return cpu_ppc601_load_rtcl(env);
114 }
115
116 target_ulong helper_load_601_rtcu (void)
117 {
118     return cpu_ppc601_load_rtcu(env);
119 }
120
121 #if !defined(CONFIG_USER_ONLY)
122 #if defined (TARGET_PPC64)
123 void helper_store_asr (target_ulong val)
124 {
125     ppc_store_asr(env, val);
126 }
127 #endif
128
129 void helper_store_sdr1 (target_ulong val)
130 {
131     ppc_store_sdr1(env, val);
132 }
133
134 void helper_store_tbl (target_ulong val)
135 {
136     cpu_ppc_store_tbl(env, val);
137 }
138
139 void helper_store_tbu (target_ulong val)
140 {
141     cpu_ppc_store_tbu(env, val);
142 }
143
144 void helper_store_atbl (target_ulong val)
145 {
146     cpu_ppc_store_atbl(env, val);
147 }
148
149 void helper_store_atbu (target_ulong val)
150 {
151     cpu_ppc_store_atbu(env, val);
152 }
153
154 void helper_store_601_rtcl (target_ulong val)
155 {
156     cpu_ppc601_store_rtcl(env, val);
157 }
158
159 void helper_store_601_rtcu (target_ulong val)
160 {
161     cpu_ppc601_store_rtcu(env, val);
162 }
163
164 target_ulong helper_load_decr (void)
165 {
166     return cpu_ppc_load_decr(env);
167 }
168
169 void helper_store_decr (target_ulong val)
170 {
171     cpu_ppc_store_decr(env, val);
172 }
173
174 void helper_store_hid0_601 (target_ulong val)
175 {
176     target_ulong hid0;
177
178     hid0 = env->spr[SPR_HID0];
179     if ((val ^ hid0) & 0x00000008) {
180         /* Change current endianness */
181         env->hflags &= ~(1 << MSR_LE);
182         env->hflags_nmsr &= ~(1 << MSR_LE);
183         env->hflags_nmsr |= (1 << MSR_LE) & (((val >> 3) & 1) << MSR_LE);
184         env->hflags |= env->hflags_nmsr;
185         if (loglevel != 0) {
186             fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
187                     __func__, val & 0x8 ? 'l' : 'b', env->hflags);
188         }
189     }
190     env->spr[SPR_HID0] = (uint32_t)val;
191 }
192
193 void helper_store_403_pbr (uint32_t num, target_ulong value)
194 {
195     if (likely(env->pb[num] != value)) {
196         env->pb[num] = value;
197         /* Should be optimized */
198         tlb_flush(env, 1);
199     }
200 }
201
202 target_ulong helper_load_40x_pit (void)
203 {
204     return load_40x_pit(env);
205 }
206
207 void helper_store_40x_pit (target_ulong val)
208 {
209     store_40x_pit(env, val);
210 }
211
212 void helper_store_40x_dbcr0 (target_ulong val)
213 {
214     store_40x_dbcr0(env, val);
215 }
216
217 void helper_store_40x_sler (target_ulong val)
218 {
219     store_40x_sler(env, val);
220 }
221
222 void helper_store_booke_tcr (target_ulong val)
223 {
224     store_booke_tcr(env, val);
225 }
226
227 void helper_store_booke_tsr (target_ulong val)
228 {
229     store_booke_tsr(env, val);
230 }
231
232 void helper_store_ibatu (uint32_t nr, target_ulong val)
233 {
234     ppc_store_ibatu(env, nr, val);
235 }
236
237 void helper_store_ibatl (uint32_t nr, target_ulong val)
238 {
239     ppc_store_ibatl(env, nr, val);
240 }
241
242 void helper_store_dbatu (uint32_t nr, target_ulong val)
243 {
244     ppc_store_dbatu(env, nr, val);
245 }
246
247 void helper_store_dbatl (uint32_t nr, target_ulong val)
248 {
249     ppc_store_dbatl(env, nr, val);
250 }
251
252 void helper_store_601_batl (uint32_t nr, target_ulong val)
253 {
254     ppc_store_ibatl_601(env, nr, val);
255 }
256
257 void helper_store_601_batu (uint32_t nr, target_ulong val)
258 {
259     ppc_store_ibatu_601(env, nr, val);
260 }
261 #endif
262
263 /*****************************************************************************/
264 /* Memory load and stores */
265
266 static always_inline target_ulong addr_add(target_ulong addr, target_long arg)
267 {
268 #if defined(TARGET_PPC64)
269         if (!msr_sf)
270             return (uint32_t)(addr + arg);
271         else
272 #endif
273             return addr + arg;
274 }
275
276 void helper_lmw (target_ulong addr, uint32_t reg)
277 {
278     for (; reg < 32; reg++) {
279         if (msr_le)
280             env->gpr[reg] = bswap32(ldl(addr));
281         else
282             env->gpr[reg] = ldl(addr);
283         addr = addr_add(addr, 4);
284     }
285 }
286
287 void helper_stmw (target_ulong addr, uint32_t reg)
288 {
289     for (; reg < 32; reg++) {
290         if (msr_le)
291             stl(addr, bswap32((uint32_t)env->gpr[reg]));
292         else
293             stl(addr, (uint32_t)env->gpr[reg]);
294         addr = addr_add(addr, 4);
295     }
296 }
297
298 void helper_lsw(target_ulong addr, uint32_t nb, uint32_t reg)
299 {
300     int sh;
301     for (; nb > 3; nb -= 4) {
302         env->gpr[reg] = ldl(addr);
303         reg = (reg + 1) % 32;
304         addr = addr_add(addr, 4);
305     }
306     if (unlikely(nb > 0)) {
307         env->gpr[reg] = 0;
308         for (sh = 24; nb > 0; nb--, sh -= 8) {
309             env->gpr[reg] |= ldub(addr) << sh;
310             addr = addr_add(addr, 1);
311         }
312     }
313 }
314 /* PPC32 specification says we must generate an exception if
315  * rA is in the range of registers to be loaded.
316  * In an other hand, IBM says this is valid, but rA won't be loaded.
317  * For now, I'll follow the spec...
318  */
319 void helper_lswx(target_ulong addr, uint32_t reg, uint32_t ra, uint32_t rb)
320 {
321     if (likely(xer_bc != 0)) {
322         if (unlikely((ra != 0 && reg < ra && (reg + xer_bc) > ra) ||
323                      (reg < rb && (reg + xer_bc) > rb))) {
324             helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
325                                        POWERPC_EXCP_INVAL |
326                                        POWERPC_EXCP_INVAL_LSWX);
327         } else {
328             helper_lsw(addr, xer_bc, reg);
329         }
330     }
331 }
332
333 void helper_stsw(target_ulong addr, uint32_t nb, uint32_t reg)
334 {
335     int sh;
336     for (; nb > 3; nb -= 4) {
337         stl(addr, env->gpr[reg]);
338         reg = (reg + 1) % 32;
339         addr = addr_add(addr, 4);
340     }
341     if (unlikely(nb > 0)) {
342         for (sh = 24; nb > 0; nb--, sh -= 8) {
343             stb(addr, (env->gpr[reg] >> sh) & 0xFF);
344             addr = addr_add(addr, 1);
345         }
346     }
347 }
348
349 static void do_dcbz(target_ulong addr, int dcache_line_size)
350 {
351     addr &= ~(dcache_line_size - 1);
352     int i;
353     for (i = 0 ; i < dcache_line_size ; i += 4) {
354         stl(addr + i , 0);
355     }
356     if (env->reserve == addr)
357         env->reserve = (target_ulong)-1ULL;
358 }
359
360 void helper_dcbz(target_ulong addr)
361 {
362     do_dcbz(addr, env->dcache_line_size);
363 }
364
365 void helper_dcbz_970(target_ulong addr)
366 {
367     if (((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1)
368         do_dcbz(addr, 32);
369     else
370         do_dcbz(addr, env->dcache_line_size);
371 }
372
373 void helper_icbi(target_ulong addr)
374 {
375     uint32_t tmp;
376
377     addr &= ~(env->dcache_line_size - 1);
378     /* Invalidate one cache line :
379      * PowerPC specification says this is to be treated like a load
380      * (not a fetch) by the MMU. To be sure it will be so,
381      * do the load "by hand".
382      */
383     tmp = ldl(addr);
384     tb_invalidate_page_range(addr, addr + env->icache_line_size);
385 }
386
387 // XXX: to be tested
388 target_ulong helper_lscbx (target_ulong addr, uint32_t reg, uint32_t ra, uint32_t rb)
389 {
390     int i, c, d;
391     d = 24;
392     for (i = 0; i < xer_bc; i++) {
393         c = ldub(addr);
394         addr = addr_add(addr, 1);
395         /* ra (if not 0) and rb are never modified */
396         if (likely(reg != rb && (ra == 0 || reg != ra))) {
397             env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
398         }
399         if (unlikely(c == xer_cmp))
400             break;
401         if (likely(d != 0)) {
402             d -= 8;
403         } else {
404             d = 24;
405             reg++;
406             reg = reg & 0x1F;
407         }
408     }
409     return i;
410 }
411
412 /*****************************************************************************/
413 /* Fixed point operations helpers */
414 #if defined(TARGET_PPC64)
415
416 /* multiply high word */
417 uint64_t helper_mulhd (uint64_t arg1, uint64_t arg2)
418 {
419     uint64_t tl, th;
420
421     muls64(&tl, &th, arg1, arg2);
422     return th;
423 }
424
425 /* multiply high word unsigned */
426 uint64_t helper_mulhdu (uint64_t arg1, uint64_t arg2)
427 {
428     uint64_t tl, th;
429
430     mulu64(&tl, &th, arg1, arg2);
431     return th;
432 }
433
434 uint64_t helper_mulldo (uint64_t arg1, uint64_t arg2)
435 {
436     int64_t th;
437     uint64_t tl;
438
439     muls64(&tl, (uint64_t *)&th, arg1, arg2);
440     /* If th != 0 && th != -1, then we had an overflow */
441     if (likely((uint64_t)(th + 1) <= 1)) {
442         env->xer &= ~(1 << XER_OV);
443     } else {
444         env->xer |= (1 << XER_OV) | (1 << XER_SO);
445     }
446     return (int64_t)tl;
447 }
448 #endif
449
450 target_ulong helper_cntlzw (target_ulong t)
451 {
452     return clz32(t);
453 }
454
455 #if defined(TARGET_PPC64)
456 target_ulong helper_cntlzd (target_ulong t)
457 {
458     return clz64(t);
459 }
460 #endif
461
462 /* shift right arithmetic helper */
463 target_ulong helper_sraw (target_ulong value, target_ulong shift)
464 {
465     int32_t ret;
466
467     if (likely(!(shift & 0x20))) {
468         if (likely((uint32_t)shift != 0)) {
469             shift &= 0x1f;
470             ret = (int32_t)value >> shift;
471             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
472                 env->xer &= ~(1 << XER_CA);
473             } else {
474                 env->xer |= (1 << XER_CA);
475             }
476         } else {
477             ret = (int32_t)value;
478             env->xer &= ~(1 << XER_CA);
479         }
480     } else {
481         ret = (int32_t)value >> 31;
482         if (ret) {
483             env->xer |= (1 << XER_CA);
484         } else {
485             env->xer &= ~(1 << XER_CA);
486         }
487     }
488     return (target_long)ret;
489 }
490
491 #if defined(TARGET_PPC64)
492 target_ulong helper_srad (target_ulong value, target_ulong shift)
493 {
494     int64_t ret;
495
496     if (likely(!(shift & 0x40))) {
497         if (likely((uint64_t)shift != 0)) {
498             shift &= 0x3f;
499             ret = (int64_t)value >> shift;
500             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
501                 env->xer &= ~(1 << XER_CA);
502             } else {
503                 env->xer |= (1 << XER_CA);
504             }
505         } else {
506             ret = (int64_t)value;
507             env->xer &= ~(1 << XER_CA);
508         }
509     } else {
510         ret = (int64_t)value >> 63;
511         if (ret) {
512             env->xer |= (1 << XER_CA);
513         } else {
514             env->xer &= ~(1 << XER_CA);
515         }
516     }
517     return ret;
518 }
519 #endif
520
521 target_ulong helper_popcntb (target_ulong val)
522 {
523     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
524     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
525     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
526     return val;
527 }
528
529 #if defined(TARGET_PPC64)
530 target_ulong helper_popcntb_64 (target_ulong val)
531 {
532     val = (val & 0x5555555555555555ULL) + ((val >>  1) & 0x5555555555555555ULL);
533     val = (val & 0x3333333333333333ULL) + ((val >>  2) & 0x3333333333333333ULL);
534     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) & 0x0f0f0f0f0f0f0f0fULL);
535     return val;
536 }
537 #endif
538
539 /*****************************************************************************/
540 /* Floating point operations helpers */
541 uint64_t helper_float32_to_float64(uint32_t arg)
542 {
543     CPU_FloatU f;
544     CPU_DoubleU d;
545     f.l = arg;
546     d.d = float32_to_float64(f.f, &env->fp_status);
547     return d.ll;
548 }
549
550 uint32_t helper_float64_to_float32(uint64_t arg)
551 {
552     CPU_FloatU f;
553     CPU_DoubleU d;
554     d.ll = arg;
555     f.f = float64_to_float32(d.d, &env->fp_status);
556     return f.l;
557 }
558
559 static always_inline int isden (float64 d)
560 {
561     CPU_DoubleU u;
562
563     u.d = d;
564
565     return ((u.ll >> 52) & 0x7FF) == 0;
566 }
567
568 uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
569 {
570     CPU_DoubleU farg;
571     int isneg;
572     int ret;
573     farg.ll = arg;
574     isneg = float64_is_neg(farg.d);
575     if (unlikely(float64_is_nan(farg.d))) {
576         if (float64_is_signaling_nan(farg.d)) {
577             /* Signaling NaN: flags are undefined */
578             ret = 0x00;
579         } else {
580             /* Quiet NaN */
581             ret = 0x11;
582         }
583     } else if (unlikely(float64_is_infinity(farg.d))) {
584         /* +/- infinity */
585         if (isneg)
586             ret = 0x09;
587         else
588             ret = 0x05;
589     } else {
590         if (float64_is_zero(farg.d)) {
591             /* +/- zero */
592             if (isneg)
593                 ret = 0x12;
594             else
595                 ret = 0x02;
596         } else {
597             if (isden(farg.d)) {
598                 /* Denormalized numbers */
599                 ret = 0x10;
600             } else {
601                 /* Normalized numbers */
602                 ret = 0x00;
603             }
604             if (isneg) {
605                 ret |= 0x08;
606             } else {
607                 ret |= 0x04;
608             }
609         }
610     }
611     if (set_fprf) {
612         /* We update FPSCR_FPRF */
613         env->fpscr &= ~(0x1F << FPSCR_FPRF);
614         env->fpscr |= ret << FPSCR_FPRF;
615     }
616     /* We just need fpcc to update Rc1 */
617     return ret & 0xF;
618 }
619
620 /* Floating-point invalid operations exception */
621 static always_inline uint64_t fload_invalid_op_excp (int op)
622 {
623     uint64_t ret = 0;
624     int ve;
625
626     ve = fpscr_ve;
627     switch (op) {
628     case POWERPC_EXCP_FP_VXSNAN:
629         env->fpscr |= 1 << FPSCR_VXSNAN;
630         break;
631     case POWERPC_EXCP_FP_VXSOFT:
632         env->fpscr |= 1 << FPSCR_VXSOFT;
633         break;
634     case POWERPC_EXCP_FP_VXISI:
635         /* Magnitude subtraction of infinities */
636         env->fpscr |= 1 << FPSCR_VXISI;
637         goto update_arith;
638     case POWERPC_EXCP_FP_VXIDI:
639         /* Division of infinity by infinity */
640         env->fpscr |= 1 << FPSCR_VXIDI;
641         goto update_arith;
642     case POWERPC_EXCP_FP_VXZDZ:
643         /* Division of zero by zero */
644         env->fpscr |= 1 << FPSCR_VXZDZ;
645         goto update_arith;
646     case POWERPC_EXCP_FP_VXIMZ:
647         /* Multiplication of zero by infinity */
648         env->fpscr |= 1 << FPSCR_VXIMZ;
649         goto update_arith;
650     case POWERPC_EXCP_FP_VXVC:
651         /* Ordered comparison of NaN */
652         env->fpscr |= 1 << FPSCR_VXVC;
653         env->fpscr &= ~(0xF << FPSCR_FPCC);
654         env->fpscr |= 0x11 << FPSCR_FPCC;
655         /* We must update the target FPR before raising the exception */
656         if (ve != 0) {
657             env->exception_index = POWERPC_EXCP_PROGRAM;
658             env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
659             /* Update the floating-point enabled exception summary */
660             env->fpscr |= 1 << FPSCR_FEX;
661             /* Exception is differed */
662             ve = 0;
663         }
664         break;
665     case POWERPC_EXCP_FP_VXSQRT:
666         /* Square root of a negative number */
667         env->fpscr |= 1 << FPSCR_VXSQRT;
668     update_arith:
669         env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
670         if (ve == 0) {
671             /* Set the result to quiet NaN */
672             ret = 0xFFF8000000000000ULL;
673             env->fpscr &= ~(0xF << FPSCR_FPCC);
674             env->fpscr |= 0x11 << FPSCR_FPCC;
675         }
676         break;
677     case POWERPC_EXCP_FP_VXCVI:
678         /* Invalid conversion */
679         env->fpscr |= 1 << FPSCR_VXCVI;
680         env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
681         if (ve == 0) {
682             /* Set the result to quiet NaN */
683             ret = 0xFFF8000000000000ULL;
684             env->fpscr &= ~(0xF << FPSCR_FPCC);
685             env->fpscr |= 0x11 << FPSCR_FPCC;
686         }
687         break;
688     }
689     /* Update the floating-point invalid operation summary */
690     env->fpscr |= 1 << FPSCR_VX;
691     /* Update the floating-point exception summary */
692     env->fpscr |= 1 << FPSCR_FX;
693     if (ve != 0) {
694         /* Update the floating-point enabled exception summary */
695         env->fpscr |= 1 << FPSCR_FEX;
696         if (msr_fe0 != 0 || msr_fe1 != 0)
697             helper_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
698     }
699     return ret;
700 }
701
702 static always_inline void float_zero_divide_excp (void)
703 {
704     env->fpscr |= 1 << FPSCR_ZX;
705     env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
706     /* Update the floating-point exception summary */
707     env->fpscr |= 1 << FPSCR_FX;
708     if (fpscr_ze != 0) {
709         /* Update the floating-point enabled exception summary */
710         env->fpscr |= 1 << FPSCR_FEX;
711         if (msr_fe0 != 0 || msr_fe1 != 0) {
712             helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
713                                        POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
714         }
715     }
716 }
717
718 static always_inline void float_overflow_excp (void)
719 {
720     env->fpscr |= 1 << FPSCR_OX;
721     /* Update the floating-point exception summary */
722     env->fpscr |= 1 << FPSCR_FX;
723     if (fpscr_oe != 0) {
724         /* XXX: should adjust the result */
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_OX;
730     } else {
731         env->fpscr |= 1 << FPSCR_XX;
732         env->fpscr |= 1 << FPSCR_FI;
733     }
734 }
735
736 static always_inline void float_underflow_excp (void)
737 {
738     env->fpscr |= 1 << FPSCR_UX;
739     /* Update the floating-point exception summary */
740     env->fpscr |= 1 << FPSCR_FX;
741     if (fpscr_ue != 0) {
742         /* XXX: should adjust the result */
743         /* Update the floating-point enabled exception summary */
744         env->fpscr |= 1 << FPSCR_FEX;
745         /* We must update the target FPR before raising the exception */
746         env->exception_index = POWERPC_EXCP_PROGRAM;
747         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
748     }
749 }
750
751 static always_inline void float_inexact_excp (void)
752 {
753     env->fpscr |= 1 << FPSCR_XX;
754     /* Update the floating-point exception summary */
755     env->fpscr |= 1 << FPSCR_FX;
756     if (fpscr_xe != 0) {
757         /* Update the floating-point enabled exception summary */
758         env->fpscr |= 1 << FPSCR_FEX;
759         /* We must update the target FPR before raising the exception */
760         env->exception_index = POWERPC_EXCP_PROGRAM;
761         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
762     }
763 }
764
765 static always_inline void fpscr_set_rounding_mode (void)
766 {
767     int rnd_type;
768
769     /* Set rounding mode */
770     switch (fpscr_rn) {
771     case 0:
772         /* Best approximation (round to nearest) */
773         rnd_type = float_round_nearest_even;
774         break;
775     case 1:
776         /* Smaller magnitude (round toward zero) */
777         rnd_type = float_round_to_zero;
778         break;
779     case 2:
780         /* Round toward +infinite */
781         rnd_type = float_round_up;
782         break;
783     default:
784     case 3:
785         /* Round toward -infinite */
786         rnd_type = float_round_down;
787         break;
788     }
789     set_float_rounding_mode(rnd_type, &env->fp_status);
790 }
791
792 void helper_fpscr_clrbit (uint32_t bit)
793 {
794     int prev;
795
796     prev = (env->fpscr >> bit) & 1;
797     env->fpscr &= ~(1 << bit);
798     if (prev == 1) {
799         switch (bit) {
800         case FPSCR_RN1:
801         case FPSCR_RN:
802             fpscr_set_rounding_mode();
803             break;
804         default:
805             break;
806         }
807     }
808 }
809
810 void helper_fpscr_setbit (uint32_t bit)
811 {
812     int prev;
813
814     prev = (env->fpscr >> bit) & 1;
815     env->fpscr |= 1 << bit;
816     if (prev == 0) {
817         switch (bit) {
818         case FPSCR_VX:
819             env->fpscr |= 1 << FPSCR_FX;
820             if (fpscr_ve)
821                 goto raise_ve;
822         case FPSCR_OX:
823             env->fpscr |= 1 << FPSCR_FX;
824             if (fpscr_oe)
825                 goto raise_oe;
826             break;
827         case FPSCR_UX:
828             env->fpscr |= 1 << FPSCR_FX;
829             if (fpscr_ue)
830                 goto raise_ue;
831             break;
832         case FPSCR_ZX:
833             env->fpscr |= 1 << FPSCR_FX;
834             if (fpscr_ze)
835                 goto raise_ze;
836             break;
837         case FPSCR_XX:
838             env->fpscr |= 1 << FPSCR_FX;
839             if (fpscr_xe)
840                 goto raise_xe;
841             break;
842         case FPSCR_VXSNAN:
843         case FPSCR_VXISI:
844         case FPSCR_VXIDI:
845         case FPSCR_VXZDZ:
846         case FPSCR_VXIMZ:
847         case FPSCR_VXVC:
848         case FPSCR_VXSOFT:
849         case FPSCR_VXSQRT:
850         case FPSCR_VXCVI:
851             env->fpscr |= 1 << FPSCR_VX;
852             env->fpscr |= 1 << FPSCR_FX;
853             if (fpscr_ve != 0)
854                 goto raise_ve;
855             break;
856         case FPSCR_VE:
857             if (fpscr_vx != 0) {
858             raise_ve:
859                 env->error_code = POWERPC_EXCP_FP;
860                 if (fpscr_vxsnan)
861                     env->error_code |= POWERPC_EXCP_FP_VXSNAN;
862                 if (fpscr_vxisi)
863                     env->error_code |= POWERPC_EXCP_FP_VXISI;
864                 if (fpscr_vxidi)
865                     env->error_code |= POWERPC_EXCP_FP_VXIDI;
866                 if (fpscr_vxzdz)
867                     env->error_code |= POWERPC_EXCP_FP_VXZDZ;
868                 if (fpscr_vximz)
869                     env->error_code |= POWERPC_EXCP_FP_VXIMZ;
870                 if (fpscr_vxvc)
871                     env->error_code |= POWERPC_EXCP_FP_VXVC;
872                 if (fpscr_vxsoft)
873                     env->error_code |= POWERPC_EXCP_FP_VXSOFT;
874                 if (fpscr_vxsqrt)
875                     env->error_code |= POWERPC_EXCP_FP_VXSQRT;
876                 if (fpscr_vxcvi)
877                     env->error_code |= POWERPC_EXCP_FP_VXCVI;
878                 goto raise_excp;
879             }
880             break;
881         case FPSCR_OE:
882             if (fpscr_ox != 0) {
883             raise_oe:
884                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
885                 goto raise_excp;
886             }
887             break;
888         case FPSCR_UE:
889             if (fpscr_ux != 0) {
890             raise_ue:
891                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
892                 goto raise_excp;
893             }
894             break;
895         case FPSCR_ZE:
896             if (fpscr_zx != 0) {
897             raise_ze:
898                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
899                 goto raise_excp;
900             }
901             break;
902         case FPSCR_XE:
903             if (fpscr_xx != 0) {
904             raise_xe:
905                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
906                 goto raise_excp;
907             }
908             break;
909         case FPSCR_RN1:
910         case FPSCR_RN:
911             fpscr_set_rounding_mode();
912             break;
913         default:
914             break;
915         raise_excp:
916             /* Update the floating-point enabled exception summary */
917             env->fpscr |= 1 << FPSCR_FEX;
918                 /* We have to update Rc1 before raising the exception */
919             env->exception_index = POWERPC_EXCP_PROGRAM;
920             break;
921         }
922     }
923 }
924
925 void helper_store_fpscr (uint64_t arg, uint32_t mask)
926 {
927     /*
928      * We use only the 32 LSB of the incoming fpr
929      */
930     uint32_t prev, new;
931     int i;
932
933     prev = env->fpscr;
934     new = (uint32_t)arg;
935     new &= ~0x60000000;
936     new |= prev & 0x60000000;
937     for (i = 0; i < 8; i++) {
938         if (mask & (1 << i)) {
939             env->fpscr &= ~(0xF << (4 * i));
940             env->fpscr |= new & (0xF << (4 * i));
941         }
942     }
943     /* Update VX and FEX */
944     if (fpscr_ix != 0)
945         env->fpscr |= 1 << FPSCR_VX;
946     else
947         env->fpscr &= ~(1 << FPSCR_VX);
948     if ((fpscr_ex & fpscr_eex) != 0) {
949         env->fpscr |= 1 << FPSCR_FEX;
950         env->exception_index = POWERPC_EXCP_PROGRAM;
951         /* XXX: we should compute it properly */
952         env->error_code = POWERPC_EXCP_FP;
953     }
954     else
955         env->fpscr &= ~(1 << FPSCR_FEX);
956     fpscr_set_rounding_mode();
957 }
958
959 void helper_float_check_status (void)
960 {
961 #ifdef CONFIG_SOFTFLOAT
962     if (env->exception_index == POWERPC_EXCP_PROGRAM &&
963         (env->error_code & POWERPC_EXCP_FP)) {
964         /* Differred floating-point exception after target FPR update */
965         if (msr_fe0 != 0 || msr_fe1 != 0)
966             helper_raise_exception_err(env->exception_index, env->error_code);
967     } else {
968         int status = get_float_exception_flags(&env->fp_status);
969         if (status & float_flag_divbyzero) {
970             float_zero_divide_excp();
971         } else if (status & float_flag_overflow) {
972             float_overflow_excp();
973         } else if (status & float_flag_underflow) {
974             float_underflow_excp();
975         } else if (status & float_flag_inexact) {
976             float_inexact_excp();
977         }
978     }
979 #else
980     if (env->exception_index == POWERPC_EXCP_PROGRAM &&
981         (env->error_code & POWERPC_EXCP_FP)) {
982         /* Differred floating-point exception after target FPR update */
983         if (msr_fe0 != 0 || msr_fe1 != 0)
984             helper_raise_exception_err(env->exception_index, env->error_code);
985     }
986 #endif
987 }
988
989 #ifdef CONFIG_SOFTFLOAT
990 void helper_reset_fpstatus (void)
991 {
992     set_float_exception_flags(0, &env->fp_status);
993 }
994 #endif
995
996 /* fadd - fadd. */
997 uint64_t helper_fadd (uint64_t arg1, uint64_t arg2)
998 {
999     CPU_DoubleU farg1, farg2;
1000
1001     farg1.ll = arg1;
1002     farg2.ll = arg2;
1003 #if USE_PRECISE_EMULATION
1004     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1005                  float64_is_signaling_nan(farg2.d))) {
1006         /* sNaN addition */
1007         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1008     } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d) &&
1009                       float64_is_neg(farg1.d) != float64_is_neg(farg2.d))) {
1010         /* Magnitude subtraction of infinities */
1011         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1012     } else {
1013         farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
1014     }
1015 #else
1016     farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
1017 #endif
1018     return farg1.ll;
1019 }
1020
1021 /* fsub - fsub. */
1022 uint64_t helper_fsub (uint64_t arg1, uint64_t arg2)
1023 {
1024     CPU_DoubleU farg1, farg2;
1025
1026     farg1.ll = arg1;
1027     farg2.ll = arg2;
1028 #if USE_PRECISE_EMULATION
1029 {
1030     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1031                  float64_is_signaling_nan(farg2.d))) {
1032         /* sNaN subtraction */
1033         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1034     } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d) &&
1035                       float64_is_neg(farg1.d) == float64_is_neg(farg2.d))) {
1036         /* Magnitude subtraction of infinities */
1037         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1038     } else {
1039         farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
1040     }
1041 }
1042 #else
1043     farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
1044 #endif
1045     return farg1.ll;
1046 }
1047
1048 /* fmul - fmul. */
1049 uint64_t helper_fmul (uint64_t arg1, uint64_t arg2)
1050 {
1051     CPU_DoubleU farg1, farg2;
1052
1053     farg1.ll = arg1;
1054     farg2.ll = arg2;
1055 #if USE_PRECISE_EMULATION
1056     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1057                  float64_is_signaling_nan(farg2.d))) {
1058         /* sNaN multiplication */
1059         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1060     } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
1061                         (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
1062         /* Multiplication of zero by infinity */
1063         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1064     } else {
1065         farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1066     }
1067 #else
1068     farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1069 #endif
1070     return farg1.ll;
1071 }
1072
1073 /* fdiv - fdiv. */
1074 uint64_t helper_fdiv (uint64_t arg1, uint64_t arg2)
1075 {
1076     CPU_DoubleU farg1, farg2;
1077
1078     farg1.ll = arg1;
1079     farg2.ll = arg2;
1080 #if USE_PRECISE_EMULATION
1081     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1082                  float64_is_signaling_nan(farg2.d))) {
1083         /* sNaN division */
1084         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1085     } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d))) {
1086         /* Division of infinity by infinity */
1087         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
1088     } else if (unlikely(float64_is_zero(farg1.d) && float64_is_zero(farg2.d))) {
1089         /* Division of zero by zero */
1090         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
1091     } else {
1092         farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
1093     }
1094 #else
1095     farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
1096 #endif
1097     return farg1.ll;
1098 }
1099
1100 /* fabs */
1101 uint64_t helper_fabs (uint64_t arg)
1102 {
1103     CPU_DoubleU farg;
1104
1105     farg.ll = arg;
1106     farg.d = float64_abs(farg.d);
1107     return farg.ll;
1108 }
1109
1110 /* fnabs */
1111 uint64_t helper_fnabs (uint64_t arg)
1112 {
1113     CPU_DoubleU farg;
1114
1115     farg.ll = arg;
1116     farg.d = float64_abs(farg.d);
1117     farg.d = float64_chs(farg.d);
1118     return farg.ll;
1119 }
1120
1121 /* fneg */
1122 uint64_t helper_fneg (uint64_t arg)
1123 {
1124     CPU_DoubleU farg;
1125
1126     farg.ll = arg;
1127     farg.d = float64_chs(farg.d);
1128     return farg.ll;
1129 }
1130
1131 /* fctiw - fctiw. */
1132 uint64_t helper_fctiw (uint64_t arg)
1133 {
1134     CPU_DoubleU farg;
1135     farg.ll = arg;
1136
1137     if (unlikely(float64_is_signaling_nan(farg.d))) {
1138         /* sNaN conversion */
1139         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1140     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1141         /* qNan / infinity conversion */
1142         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1143     } else {
1144         farg.ll = float64_to_int32(farg.d, &env->fp_status);
1145 #if USE_PRECISE_EMULATION
1146         /* XXX: higher bits are not supposed to be significant.
1147          *     to make tests easier, return the same as a real PowerPC 750
1148          */
1149         farg.ll |= 0xFFF80000ULL << 32;
1150 #endif
1151     }
1152     return farg.ll;
1153 }
1154
1155 /* fctiwz - fctiwz. */
1156 uint64_t helper_fctiwz (uint64_t arg)
1157 {
1158     CPU_DoubleU farg;
1159     farg.ll = arg;
1160
1161     if (unlikely(float64_is_signaling_nan(farg.d))) {
1162         /* sNaN conversion */
1163         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1164     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1165         /* qNan / infinity conversion */
1166         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1167     } else {
1168         farg.ll = float64_to_int32_round_to_zero(farg.d, &env->fp_status);
1169 #if USE_PRECISE_EMULATION
1170         /* XXX: higher bits are not supposed to be significant.
1171          *     to make tests easier, return the same as a real PowerPC 750
1172          */
1173         farg.ll |= 0xFFF80000ULL << 32;
1174 #endif
1175     }
1176     return farg.ll;
1177 }
1178
1179 #if defined(TARGET_PPC64)
1180 /* fcfid - fcfid. */
1181 uint64_t helper_fcfid (uint64_t arg)
1182 {
1183     CPU_DoubleU farg;
1184     farg.d = int64_to_float64(arg, &env->fp_status);
1185     return farg.ll;
1186 }
1187
1188 /* fctid - fctid. */
1189 uint64_t helper_fctid (uint64_t arg)
1190 {
1191     CPU_DoubleU farg;
1192     farg.ll = arg;
1193
1194     if (unlikely(float64_is_signaling_nan(farg.d))) {
1195         /* sNaN conversion */
1196         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1197     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1198         /* qNan / infinity conversion */
1199         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1200     } else {
1201         farg.ll = float64_to_int64(farg.d, &env->fp_status);
1202     }
1203     return farg.ll;
1204 }
1205
1206 /* fctidz - fctidz. */
1207 uint64_t helper_fctidz (uint64_t arg)
1208 {
1209     CPU_DoubleU farg;
1210     farg.ll = arg;
1211
1212     if (unlikely(float64_is_signaling_nan(farg.d))) {
1213         /* sNaN conversion */
1214         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1215     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1216         /* qNan / infinity conversion */
1217         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1218     } else {
1219         farg.ll = float64_to_int64_round_to_zero(farg.d, &env->fp_status);
1220     }
1221     return farg.ll;
1222 }
1223
1224 #endif
1225
1226 static always_inline uint64_t do_fri (uint64_t arg, int rounding_mode)
1227 {
1228     CPU_DoubleU farg;
1229     farg.ll = arg;
1230
1231     if (unlikely(float64_is_signaling_nan(farg.d))) {
1232         /* sNaN round */
1233         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1234     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1235         /* qNan / infinity round */
1236         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1237     } else {
1238         set_float_rounding_mode(rounding_mode, &env->fp_status);
1239         farg.ll = float64_round_to_int(farg.d, &env->fp_status);
1240         /* Restore rounding mode from FPSCR */
1241         fpscr_set_rounding_mode();
1242     }
1243     return farg.ll;
1244 }
1245
1246 uint64_t helper_frin (uint64_t arg)
1247 {
1248     return do_fri(arg, float_round_nearest_even);
1249 }
1250
1251 uint64_t helper_friz (uint64_t arg)
1252 {
1253     return do_fri(arg, float_round_to_zero);
1254 }
1255
1256 uint64_t helper_frip (uint64_t arg)
1257 {
1258     return do_fri(arg, float_round_up);
1259 }
1260
1261 uint64_t helper_frim (uint64_t arg)
1262 {
1263     return do_fri(arg, float_round_down);
1264 }
1265
1266 /* fmadd - fmadd. */
1267 uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1268 {
1269     CPU_DoubleU farg1, farg2, farg3;
1270
1271     farg1.ll = arg1;
1272     farg2.ll = arg2;
1273     farg3.ll = arg3;
1274 #if USE_PRECISE_EMULATION
1275     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1276                  float64_is_signaling_nan(farg2.d) ||
1277                  float64_is_signaling_nan(farg3.d))) {
1278         /* sNaN operation */
1279         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1280     } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
1281                         (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
1282         /* Multiplication of zero by infinity */
1283         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1284     } else {
1285 #ifdef FLOAT128
1286         /* This is the way the PowerPC specification defines it */
1287         float128 ft0_128, ft1_128;
1288
1289         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1290         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1291         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1292         if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
1293                      float128_is_neg(ft0_128) != float64_is_neg(farg3.d))) {
1294             /* Magnitude subtraction of infinities */
1295             farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1296         } else {
1297             ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1298             ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1299             farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1300         }
1301 #else
1302         /* This is OK on x86 hosts */
1303         farg1.d = (farg1.d * farg2.d) + farg3.d;
1304 #endif
1305     }
1306 #else
1307     farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1308     farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1309 #endif
1310     return farg1.ll;
1311 }
1312
1313 /* fmsub - fmsub. */
1314 uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1315 {
1316     CPU_DoubleU farg1, farg2, farg3;
1317
1318     farg1.ll = arg1;
1319     farg2.ll = arg2;
1320     farg3.ll = arg3;
1321 #if USE_PRECISE_EMULATION
1322     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1323                  float64_is_signaling_nan(farg2.d) ||
1324                  float64_is_signaling_nan(farg3.d))) {
1325         /* sNaN operation */
1326         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1327     } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
1328                         (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
1329         /* Multiplication of zero by infinity */
1330         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1331     } else {
1332 #ifdef FLOAT128
1333         /* This is the way the PowerPC specification defines it */
1334         float128 ft0_128, ft1_128;
1335
1336         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1337         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1338         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1339         if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
1340                      float128_is_neg(ft0_128) == float64_is_neg(farg3.d))) {
1341             /* Magnitude subtraction of infinities */
1342             farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1343         } else {
1344             ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1345             ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1346             farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1347         }
1348 #else
1349         /* This is OK on x86 hosts */
1350         farg1.d = (farg1.d * farg2.d) - farg3.d;
1351 #endif
1352     }
1353 #else
1354     farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1355     farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1356 #endif
1357     return farg1.ll;
1358 }
1359
1360 /* fnmadd - fnmadd. */
1361 uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1362 {
1363     CPU_DoubleU farg1, farg2, farg3;
1364
1365     farg1.ll = arg1;
1366     farg2.ll = arg2;
1367     farg3.ll = arg3;
1368
1369     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1370                  float64_is_signaling_nan(farg2.d) ||
1371                  float64_is_signaling_nan(farg3.d))) {
1372         /* sNaN operation */
1373         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1374     } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
1375                         (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
1376         /* Multiplication of zero by infinity */
1377         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1378     } else {
1379 #if USE_PRECISE_EMULATION
1380 #ifdef FLOAT128
1381         /* This is the way the PowerPC specification defines it */
1382         float128 ft0_128, ft1_128;
1383
1384         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1385         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1386         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1387         if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
1388                      float128_is_neg(ft0_128) != float64_is_neg(farg3.d))) {
1389             /* Magnitude subtraction of infinities */
1390             farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1391         } else {
1392             ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1393             ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1394             farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1395         }
1396 #else
1397         /* This is OK on x86 hosts */
1398         farg1.d = (farg1.d * farg2.d) + farg3.d;
1399 #endif
1400 #else
1401         farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1402         farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1403 #endif
1404         if (likely(!float64_is_nan(farg1.d)))
1405             farg1.d = float64_chs(farg1.d);
1406     }
1407     return farg1.ll;
1408 }
1409
1410 /* fnmsub - fnmsub. */
1411 uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1412 {
1413     CPU_DoubleU farg1, farg2, farg3;
1414
1415     farg1.ll = arg1;
1416     farg2.ll = arg2;
1417     farg3.ll = arg3;
1418
1419     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1420                  float64_is_signaling_nan(farg2.d) ||
1421                  float64_is_signaling_nan(farg3.d))) {
1422         /* sNaN operation */
1423         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1424     } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
1425                         (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
1426         /* Multiplication of zero by infinity */
1427         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1428     } else {
1429 #if USE_PRECISE_EMULATION
1430 #ifdef FLOAT128
1431         /* This is the way the PowerPC specification defines it */
1432         float128 ft0_128, ft1_128;
1433
1434         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1435         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1436         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1437         if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
1438                      float128_is_neg(ft0_128) == float64_is_neg(farg3.d))) {
1439             /* Magnitude subtraction of infinities */
1440             farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1441         } else {
1442             ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1443             ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1444             farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1445         }
1446 #else
1447         /* This is OK on x86 hosts */
1448         farg1.d = (farg1.d * farg2.d) - farg3.d;
1449 #endif
1450 #else
1451         farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1452         farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1453 #endif
1454         if (likely(!float64_is_nan(farg1.d)))
1455             farg1.d = float64_chs(farg1.d);
1456     }
1457     return farg1.ll;
1458 }
1459
1460 /* frsp - frsp. */
1461 uint64_t helper_frsp (uint64_t arg)
1462 {
1463     CPU_DoubleU farg;
1464     float32 f32;
1465     farg.ll = arg;
1466
1467 #if USE_PRECISE_EMULATION
1468     if (unlikely(float64_is_signaling_nan(farg.d))) {
1469         /* sNaN square root */
1470        farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1471     } else {
1472        f32 = float64_to_float32(farg.d, &env->fp_status);
1473        farg.d = float32_to_float64(f32, &env->fp_status);
1474     }
1475 #else
1476     f32 = float64_to_float32(farg.d, &env->fp_status);
1477     farg.d = float32_to_float64(f32, &env->fp_status);
1478 #endif
1479     return farg.ll;
1480 }
1481
1482 /* fsqrt - fsqrt. */
1483 uint64_t helper_fsqrt (uint64_t arg)
1484 {
1485     CPU_DoubleU farg;
1486     farg.ll = arg;
1487
1488     if (unlikely(float64_is_signaling_nan(farg.d))) {
1489         /* sNaN square root */
1490         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1491     } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
1492         /* Square root of a negative nonzero number */
1493         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1494     } else {
1495         farg.d = float64_sqrt(farg.d, &env->fp_status);
1496     }
1497     return farg.ll;
1498 }
1499
1500 /* fre - fre. */
1501 uint64_t helper_fre (uint64_t arg)
1502 {
1503     CPU_DoubleU fone, farg;
1504     fone.ll = 0x3FF0000000000000ULL; /* 1.0 */
1505     farg.ll = arg;
1506
1507     if (unlikely(float64_is_signaling_nan(farg.d))) {
1508         /* sNaN reciprocal */
1509         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1510     } else {
1511         farg.d = float64_div(fone.d, farg.d, &env->fp_status);
1512     }
1513     return farg.d;
1514 }
1515
1516 /* fres - fres. */
1517 uint64_t helper_fres (uint64_t arg)
1518 {
1519     CPU_DoubleU fone, farg;
1520     float32 f32;
1521     fone.ll = 0x3FF0000000000000ULL; /* 1.0 */
1522     farg.ll = arg;
1523
1524     if (unlikely(float64_is_signaling_nan(farg.d))) {
1525         /* sNaN reciprocal */
1526         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1527     } else {
1528         farg.d = float64_div(fone.d, farg.d, &env->fp_status);
1529         f32 = float64_to_float32(farg.d, &env->fp_status);
1530         farg.d = float32_to_float64(f32, &env->fp_status);
1531     }
1532     return farg.ll;
1533 }
1534
1535 /* frsqrte  - frsqrte. */
1536 uint64_t helper_frsqrte (uint64_t arg)
1537 {
1538     CPU_DoubleU fone, farg;
1539     float32 f32;
1540     fone.ll = 0x3FF0000000000000ULL; /* 1.0 */
1541     farg.ll = arg;
1542
1543     if (unlikely(float64_is_signaling_nan(farg.d))) {
1544         /* sNaN reciprocal square root */
1545         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1546     } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
1547         /* Reciprocal square root of a negative nonzero number */
1548         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1549     } else {
1550         farg.d = float64_sqrt(farg.d, &env->fp_status);
1551         farg.d = float64_div(fone.d, farg.d, &env->fp_status);
1552         f32 = float64_to_float32(farg.d, &env->fp_status);
1553         farg.d = float32_to_float64(f32, &env->fp_status);
1554     }
1555     return farg.ll;
1556 }
1557
1558 /* fsel - fsel. */
1559 uint64_t helper_fsel (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1560 {
1561     CPU_DoubleU farg1;
1562
1563     farg1.ll = arg1;
1564
1565     if ((!float64_is_neg(farg1.d) || float64_is_zero(farg1.d)) && !float64_is_nan(farg1.d))
1566         return arg2;
1567     else
1568         return arg3;
1569 }
1570
1571 void helper_fcmpu (uint64_t arg1, uint64_t arg2, uint32_t crfD)
1572 {
1573     CPU_DoubleU farg1, farg2;
1574     uint32_t ret = 0;
1575     farg1.ll = arg1;
1576     farg2.ll = arg2;
1577
1578     if (unlikely(float64_is_nan(farg1.d) ||
1579                  float64_is_nan(farg2.d))) {
1580         ret = 0x01UL;
1581     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1582         ret = 0x08UL;
1583     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1584         ret = 0x04UL;
1585     } else {
1586         ret = 0x02UL;
1587     }
1588
1589     env->fpscr &= ~(0x0F << FPSCR_FPRF);
1590     env->fpscr |= ret << FPSCR_FPRF;
1591     env->crf[crfD] = ret;
1592     if (unlikely(ret == 0x01UL
1593                  && (float64_is_signaling_nan(farg1.d) ||
1594                      float64_is_signaling_nan(farg2.d)))) {
1595         /* sNaN comparison */
1596         fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1597     }
1598 }
1599
1600 void helper_fcmpo (uint64_t arg1, uint64_t arg2, uint32_t crfD)
1601 {
1602     CPU_DoubleU farg1, farg2;
1603     uint32_t ret = 0;
1604     farg1.ll = arg1;
1605     farg2.ll = arg2;
1606
1607     if (unlikely(float64_is_nan(farg1.d) ||
1608                  float64_is_nan(farg2.d))) {
1609         ret = 0x01UL;
1610     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1611         ret = 0x08UL;
1612     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1613         ret = 0x04UL;
1614     } else {
1615         ret = 0x02UL;
1616     }
1617
1618     env->fpscr &= ~(0x0F << FPSCR_FPRF);
1619     env->fpscr |= ret << FPSCR_FPRF;
1620     env->crf[crfD] = ret;
1621     if (unlikely (ret == 0x01UL)) {
1622         if (float64_is_signaling_nan(farg1.d) ||
1623             float64_is_signaling_nan(farg2.d)) {
1624             /* sNaN comparison */
1625             fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1626                                   POWERPC_EXCP_FP_VXVC);
1627         } else {
1628             /* qNaN comparison */
1629             fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1630         }
1631     }
1632 }
1633
1634 #if !defined (CONFIG_USER_ONLY)
1635 void helper_store_msr (target_ulong val)
1636 {
1637     val = hreg_store_msr(env, val, 0);
1638     if (val != 0) {
1639         env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1640         helper_raise_exception(val);
1641     }
1642 }
1643
1644 static always_inline void do_rfi (target_ulong nip, target_ulong msr,
1645                                     target_ulong msrm, int keep_msrh)
1646 {
1647 #if defined(TARGET_PPC64)
1648     if (msr & (1ULL << MSR_SF)) {
1649         nip = (uint64_t)nip;
1650         msr &= (uint64_t)msrm;
1651     } else {
1652         nip = (uint32_t)nip;
1653         msr = (uint32_t)(msr & msrm);
1654         if (keep_msrh)
1655             msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
1656     }
1657 #else
1658     nip = (uint32_t)nip;
1659     msr &= (uint32_t)msrm;
1660 #endif
1661     /* XXX: beware: this is false if VLE is supported */
1662     env->nip = nip & ~((target_ulong)0x00000003);
1663     hreg_store_msr(env, msr, 1);
1664 #if defined (DEBUG_OP)
1665     cpu_dump_rfi(env->nip, env->msr);
1666 #endif
1667     /* No need to raise an exception here,
1668      * as rfi is always the last insn of a TB
1669      */
1670     env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1671 }
1672
1673 void helper_rfi (void)
1674 {
1675     do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1676            ~((target_ulong)0xFFFF0000), 1);
1677 }
1678
1679 #if defined(TARGET_PPC64)
1680 void helper_rfid (void)
1681 {
1682     do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1683            ~((target_ulong)0xFFFF0000), 0);
1684 }
1685
1686 void helper_hrfid (void)
1687 {
1688     do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1689            ~((target_ulong)0xFFFF0000), 0);
1690 }
1691 #endif
1692 #endif
1693
1694 void helper_tw (target_ulong arg1, target_ulong arg2, uint32_t flags)
1695 {
1696     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
1697                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
1698                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
1699                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
1700                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
1701         helper_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1702     }
1703 }
1704
1705 #if defined(TARGET_PPC64)
1706 void helper_td (target_ulong arg1, target_ulong arg2, uint32_t flags)
1707 {
1708     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
1709                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
1710                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
1711                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
1712                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01)))))
1713         helper_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1714 }
1715 #endif
1716
1717 /*****************************************************************************/
1718 /* PowerPC 601 specific instructions (POWER bridge) */
1719
1720 target_ulong helper_clcs (uint32_t arg)
1721 {
1722     switch (arg) {
1723     case 0x0CUL:
1724         /* Instruction cache line size */
1725         return env->icache_line_size;
1726         break;
1727     case 0x0DUL:
1728         /* Data cache line size */
1729         return env->dcache_line_size;
1730         break;
1731     case 0x0EUL:
1732         /* Minimum cache line size */
1733         return (env->icache_line_size < env->dcache_line_size) ?
1734                 env->icache_line_size : env->dcache_line_size;
1735         break;
1736     case 0x0FUL:
1737         /* Maximum cache line size */
1738         return (env->icache_line_size > env->dcache_line_size) ?
1739                 env->icache_line_size : env->dcache_line_size;
1740         break;
1741     default:
1742         /* Undefined */
1743         return 0;
1744         break;
1745     }
1746 }
1747
1748 target_ulong helper_div (target_ulong arg1, target_ulong arg2)
1749 {
1750     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
1751
1752     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1753         (int32_t)arg2 == 0) {
1754         env->spr[SPR_MQ] = 0;
1755         return INT32_MIN;
1756     } else {
1757         env->spr[SPR_MQ] = tmp % arg2;
1758         return  tmp / (int32_t)arg2;
1759     }
1760 }
1761
1762 target_ulong helper_divo (target_ulong arg1, target_ulong arg2)
1763 {
1764     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
1765
1766     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1767         (int32_t)arg2 == 0) {
1768         env->xer |= (1 << XER_OV) | (1 << XER_SO);
1769         env->spr[SPR_MQ] = 0;
1770         return INT32_MIN;
1771     } else {
1772         env->spr[SPR_MQ] = tmp % arg2;
1773         tmp /= (int32_t)arg2;
1774         if ((int32_t)tmp != tmp) {
1775             env->xer |= (1 << XER_OV) | (1 << XER_SO);
1776         } else {
1777             env->xer &= ~(1 << XER_OV);
1778         }
1779         return tmp;
1780     }
1781 }
1782
1783 target_ulong helper_divs (target_ulong arg1, target_ulong arg2)
1784 {
1785     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1786         (int32_t)arg2 == 0) {
1787         env->spr[SPR_MQ] = 0;
1788         return INT32_MIN;
1789     } else {
1790         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
1791         return (int32_t)arg1 / (int32_t)arg2;
1792     }
1793 }
1794
1795 target_ulong helper_divso (target_ulong arg1, target_ulong arg2)
1796 {
1797     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1798         (int32_t)arg2 == 0) {
1799         env->xer |= (1 << XER_OV) | (1 << XER_SO);
1800         env->spr[SPR_MQ] = 0;
1801         return INT32_MIN;
1802     } else {
1803         env->xer &= ~(1 << XER_OV);
1804         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
1805         return (int32_t)arg1 / (int32_t)arg2;
1806     }
1807 }
1808
1809 #if !defined (CONFIG_USER_ONLY)
1810 target_ulong helper_rac (target_ulong addr)
1811 {
1812     mmu_ctx_t ctx;
1813     int nb_BATs;
1814     target_ulong ret = 0;
1815
1816     /* We don't have to generate many instances of this instruction,
1817      * as rac is supervisor only.
1818      */
1819     /* XXX: FIX THIS: Pretend we have no BAT */
1820     nb_BATs = env->nb_BATs;
1821     env->nb_BATs = 0;
1822     if (get_physical_address(env, &ctx, addr, 0, ACCESS_INT) == 0)
1823         ret = ctx.raddr;
1824     env->nb_BATs = nb_BATs;
1825     return ret;
1826 }
1827
1828 void helper_rfsvc (void)
1829 {
1830     do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1831 }
1832 #endif
1833
1834 /*****************************************************************************/
1835 /* 602 specific instructions */
1836 /* mfrom is the most crazy instruction ever seen, imho ! */
1837 /* Real implementation uses a ROM table. Do the same */
1838 /* Extremly decomposed:
1839  *                      -arg / 256
1840  * return 256 * log10(10           + 1.0) + 0.5
1841  */
1842 #if !defined (CONFIG_USER_ONLY)
1843 target_ulong helper_602_mfrom (target_ulong arg)
1844 {
1845     if (likely(arg < 602)) {
1846 #include "mfrom_table.c"
1847         return mfrom_ROM_table[arg];
1848     } else {
1849         return 0;
1850     }
1851 }
1852 #endif
1853
1854 /*****************************************************************************/
1855 /* Embedded PowerPC specific helpers */
1856
1857 /* XXX: to be improved to check access rights when in user-mode */
1858 target_ulong helper_load_dcr (target_ulong dcrn)
1859 {
1860     target_ulong val = 0;
1861
1862     if (unlikely(env->dcr_env == NULL)) {
1863         if (loglevel != 0) {
1864             fprintf(logfile, "No DCR environment\n");
1865         }
1866         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1867                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1868     } else if (unlikely(ppc_dcr_read(env->dcr_env, dcrn, &val) != 0)) {
1869         if (loglevel != 0) {
1870             fprintf(logfile, "DCR read error %d %03x\n", (int)dcrn, (int)dcrn);
1871         }
1872         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1873                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1874     }
1875     return val;
1876 }
1877
1878 void helper_store_dcr (target_ulong dcrn, target_ulong val)
1879 {
1880     if (unlikely(env->dcr_env == NULL)) {
1881         if (loglevel != 0) {
1882             fprintf(logfile, "No DCR environment\n");
1883         }
1884         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1885                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1886     } else if (unlikely(ppc_dcr_write(env->dcr_env, dcrn, val) != 0)) {
1887         if (loglevel != 0) {
1888             fprintf(logfile, "DCR write error %d %03x\n", (int)dcrn, (int)dcrn);
1889         }
1890         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1891                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1892     }
1893 }
1894
1895 #if !defined(CONFIG_USER_ONLY)
1896 void helper_40x_rfci (void)
1897 {
1898     do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1899            ~((target_ulong)0xFFFF0000), 0);
1900 }
1901
1902 void helper_rfci (void)
1903 {
1904     do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1905            ~((target_ulong)0x3FFF0000), 0);
1906 }
1907
1908 void helper_rfdi (void)
1909 {
1910     do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1911            ~((target_ulong)0x3FFF0000), 0);
1912 }
1913
1914 void helper_rfmci (void)
1915 {
1916     do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1917            ~((target_ulong)0x3FFF0000), 0);
1918 }
1919 #endif
1920
1921 /* 440 specific */
1922 target_ulong helper_dlmzb (target_ulong high, target_ulong low, uint32_t update_Rc)
1923 {
1924     target_ulong mask;
1925     int i;
1926
1927     i = 1;
1928     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1929         if ((high & mask) == 0) {
1930             if (update_Rc) {
1931                 env->crf[0] = 0x4;
1932             }
1933             goto done;
1934         }
1935         i++;
1936     }
1937     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1938         if ((low & mask) == 0) {
1939             if (update_Rc) {
1940                 env->crf[0] = 0x8;
1941             }
1942             goto done;
1943         }
1944         i++;
1945     }
1946     if (update_Rc) {
1947         env->crf[0] = 0x2;
1948     }
1949  done:
1950     env->xer = (env->xer & ~0x7F) | i;
1951     if (update_Rc) {
1952         env->crf[0] |= xer_so;
1953     }
1954     return i;
1955 }
1956
1957 /*****************************************************************************/
1958 /* Altivec extension helpers */
1959 #if defined(WORDS_BIGENDIAN)
1960 #define HI_IDX 0
1961 #define LO_IDX 1
1962 #else
1963 #define HI_IDX 1
1964 #define LO_IDX 0
1965 #endif
1966
1967 #if defined(WORDS_BIGENDIAN)
1968 #define VECTOR_FOR_INORDER_I(index, element)            \
1969     for (index = 0; index < ARRAY_SIZE(r->element); index++)
1970 #else
1971 #define VECTOR_FOR_INORDER_I(index, element)            \
1972   for (index = ARRAY_SIZE(r->element)-1; index >= 0; index--)
1973 #endif
1974
1975 /* Saturating arithmetic helpers.  */
1976 #define SATCVT(from, to, from_type, to_type, min, max, use_min, use_max) \
1977     static always_inline to_type cvt##from##to (from_type x, int *sat)  \
1978     {                                                                   \
1979         to_type r;                                                      \
1980         if (use_min && x < min) {                                       \
1981             r = min;                                                    \
1982             *sat = 1;                                                   \
1983         } else if (use_max && x > max) {                                \
1984             r = max;                                                    \
1985             *sat = 1;                                                   \
1986         } else {                                                        \
1987             r = x;                                                      \
1988         }                                                               \
1989         return r;                                                       \
1990     }
1991 SATCVT(sh, sb, int16_t, int8_t, INT8_MIN, INT8_MAX, 1, 1)
1992 SATCVT(sw, sh, int32_t, int16_t, INT16_MIN, INT16_MAX, 1, 1)
1993 SATCVT(sd, sw, int64_t, int32_t, INT32_MIN, INT32_MAX, 1, 1)
1994 SATCVT(uh, ub, uint16_t, uint8_t, 0, UINT8_MAX, 0, 1)
1995 SATCVT(uw, uh, uint32_t, uint16_t, 0, UINT16_MAX, 0, 1)
1996 SATCVT(ud, uw, uint64_t, uint32_t, 0, UINT32_MAX, 0, 1)
1997 SATCVT(sh, ub, int16_t, uint8_t, 0, UINT8_MAX, 1, 1)
1998 SATCVT(sw, uh, int32_t, uint16_t, 0, UINT16_MAX, 1, 1)
1999 SATCVT(sd, uw, int64_t, uint32_t, 0, UINT32_MAX, 1, 1)
2000 #undef SATCVT
2001
2002 #define LVE(name, access, swap, element)                        \
2003     void helper_##name (ppc_avr_t *r, target_ulong addr)        \
2004     {                                                           \
2005         size_t n_elems = ARRAY_SIZE(r->element);                \
2006         int adjust = HI_IDX*(n_elems-1);                        \
2007         int sh = sizeof(r->element[0]) >> 1;                    \
2008         int index = (addr & 0xf) >> sh;                         \
2009         if(msr_le) {                                            \
2010             r->element[LO_IDX ? index : (adjust - index)] = swap(access(addr)); \
2011         } else {                                                        \
2012             r->element[LO_IDX ? index : (adjust - index)] = access(addr); \
2013         }                                                               \
2014     }
2015 #define I(x) (x)
2016 LVE(lvebx, ldub, I, u8)
2017 LVE(lvehx, lduw, bswap16, u16)
2018 LVE(lvewx, ldl, bswap32, u32)
2019 #undef I
2020 #undef LVE
2021
2022 void helper_lvsl (ppc_avr_t *r, target_ulong sh)
2023 {
2024     int i, j = (sh & 0xf);
2025
2026     VECTOR_FOR_INORDER_I (i, u8) {
2027         r->u8[i] = j++;
2028     }
2029 }
2030
2031 void helper_lvsr (ppc_avr_t *r, target_ulong sh)
2032 {
2033     int i, j = 0x10 - (sh & 0xf);
2034
2035     VECTOR_FOR_INORDER_I (i, u8) {
2036         r->u8[i] = j++;
2037     }
2038 }
2039
2040 #define STVE(name, access, swap, element)                       \
2041     void helper_##name (ppc_avr_t *r, target_ulong addr)        \
2042     {                                                           \
2043         size_t n_elems = ARRAY_SIZE(r->element);                \
2044         int adjust = HI_IDX*(n_elems-1);                        \
2045         int sh = sizeof(r->element[0]) >> 1;                    \
2046         int index = (addr & 0xf) >> sh;                         \
2047         if(msr_le) {                                            \
2048             access(addr, swap(r->element[LO_IDX ? index : (adjust - index)])); \
2049         } else {                                                        \
2050             access(addr, r->element[LO_IDX ? index : (adjust - index)]); \
2051         }                                                               \
2052     }
2053 #define I(x) (x)
2054 STVE(stvebx, stb, I, u8)
2055 STVE(stvehx, stw, bswap16, u16)
2056 STVE(stvewx, stl, bswap32, u32)
2057 #undef I
2058 #undef LVE
2059
2060 void helper_vaddcuw (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2061 {
2062     int i;
2063     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2064         r->u32[i] = ~a->u32[i] < b->u32[i];
2065     }
2066 }
2067
2068 #define VARITH_DO(name, op, element)        \
2069 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)          \
2070 {                                                                       \
2071     int i;                                                              \
2072     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
2073         r->element[i] = a->element[i] op b->element[i];                 \
2074     }                                                                   \
2075 }
2076 #define VARITH(suffix, element)                  \
2077   VARITH_DO(add##suffix, +, element)             \
2078   VARITH_DO(sub##suffix, -, element)
2079 VARITH(ubm, u8)
2080 VARITH(uhm, u16)
2081 VARITH(uwm, u32)
2082 #undef VARITH_DO
2083 #undef VARITH
2084
2085 #define VAVG_DO(name, element, etype)                                   \
2086     void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)      \
2087     {                                                                   \
2088         int i;                                                          \
2089         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2090             etype x = (etype)a->element[i] + (etype)b->element[i] + 1;  \
2091             r->element[i] = x >> 1;                                     \
2092         }                                                               \
2093     }
2094
2095 #define VAVG(type, signed_element, signed_type, unsigned_element, unsigned_type) \
2096     VAVG_DO(avgs##type, signed_element, signed_type)                    \
2097     VAVG_DO(avgu##type, unsigned_element, unsigned_type)
2098 VAVG(b, s8, int16_t, u8, uint16_t)
2099 VAVG(h, s16, int32_t, u16, uint32_t)
2100 VAVG(w, s32, int64_t, u32, uint64_t)
2101 #undef VAVG_DO
2102 #undef VAVG
2103
2104 #define VCMP_DO(suffix, compare, element, record)                       \
2105     void helper_vcmp##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2106     {                                                                   \
2107         uint32_t ones = (uint32_t)-1;                                   \
2108         uint32_t all = ones;                                            \
2109         uint32_t none = 0;                                              \
2110         int i;                                                          \
2111         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2112             uint32_t result = (a->element[i] compare b->element[i] ? ones : 0x0); \
2113             switch (sizeof (a->element[0])) {                           \
2114             case 4: r->u32[i] = result; break;                          \
2115             case 2: r->u16[i] = result; break;                          \
2116             case 1: r->u8[i] = result; break;                           \
2117             }                                                           \
2118             all &= result;                                              \
2119             none |= result;                                             \
2120         }                                                               \
2121         if (record) {                                                   \
2122             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
2123         }                                                               \
2124     }
2125 #define VCMP(suffix, compare, element)          \
2126     VCMP_DO(suffix, compare, element, 0)        \
2127     VCMP_DO(suffix##_dot, compare, element, 1)
2128 VCMP(equb, ==, u8)
2129 VCMP(equh, ==, u16)
2130 VCMP(equw, ==, u32)
2131 VCMP(gtub, >, u8)
2132 VCMP(gtuh, >, u16)
2133 VCMP(gtuw, >, u32)
2134 VCMP(gtsb, >, s8)
2135 VCMP(gtsh, >, s16)
2136 VCMP(gtsw, >, s32)
2137 #undef VCMP_DO
2138 #undef VCMP
2139
2140 void helper_vmhaddshs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2141 {
2142     int sat = 0;
2143     int i;
2144
2145     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
2146         int32_t prod = a->s16[i] * b->s16[i];
2147         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
2148         r->s16[i] = cvtswsh (t, &sat);
2149     }
2150
2151     if (sat) {
2152         env->vscr |= (1 << VSCR_SAT);
2153     }
2154 }
2155
2156 void helper_vmhraddshs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2157 {
2158     int sat = 0;
2159     int i;
2160
2161     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
2162         int32_t prod = a->s16[i] * b->s16[i] + 0x00004000;
2163         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
2164         r->s16[i] = cvtswsh (t, &sat);
2165     }
2166
2167     if (sat) {
2168         env->vscr |= (1 << VSCR_SAT);
2169     }
2170 }
2171
2172 #define VMINMAX_DO(name, compare, element)                              \
2173     void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)      \
2174     {                                                                   \
2175         int i;                                                          \
2176         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2177             if (a->element[i] compare b->element[i]) {                  \
2178                 r->element[i] = b->element[i];                          \
2179             } else {                                                    \
2180                 r->element[i] = a->element[i];                          \
2181             }                                                           \
2182         }                                                               \
2183     }
2184 #define VMINMAX(suffix, element)                \
2185   VMINMAX_DO(min##suffix, >, element)           \
2186   VMINMAX_DO(max##suffix, <, element)
2187 VMINMAX(sb, s8)
2188 VMINMAX(sh, s16)
2189 VMINMAX(sw, s32)
2190 VMINMAX(ub, u8)
2191 VMINMAX(uh, u16)
2192 VMINMAX(uw, u32)
2193 #undef VMINMAX_DO
2194 #undef VMINMAX
2195
2196 void helper_vmladduhm (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2197 {
2198     int i;
2199     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
2200         int32_t prod = a->s16[i] * b->s16[i];
2201         r->s16[i] = (int16_t) (prod + c->s16[i]);
2202     }
2203 }
2204
2205 #define VMRG_DO(name, element, highp)                                   \
2206     void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)      \
2207     {                                                                   \
2208         ppc_avr_t result;                                               \
2209         int i;                                                          \
2210         size_t n_elems = ARRAY_SIZE(r->element);                        \
2211         for (i = 0; i < n_elems/2; i++) {                               \
2212             if (highp) {                                                \
2213                 result.element[i*2+HI_IDX] = a->element[i];             \
2214                 result.element[i*2+LO_IDX] = b->element[i];             \
2215             } else {                                                    \
2216                 result.element[n_elems - i*2 - (1+HI_IDX)] = b->element[n_elems - i - 1]; \
2217                 result.element[n_elems - i*2 - (1+LO_IDX)] = a->element[n_elems - i - 1]; \
2218             }                                                           \
2219         }                                                               \
2220         *r = result;                                                    \
2221     }
2222 #if defined(WORDS_BIGENDIAN)
2223 #define MRGHI 0
2224 #define MRGLO 1
2225 #else
2226 #define MRGHI 1
2227 #define MRGLO 0
2228 #endif
2229 #define VMRG(suffix, element)                   \
2230   VMRG_DO(mrgl##suffix, element, MRGHI)         \
2231   VMRG_DO(mrgh##suffix, element, MRGLO)
2232 VMRG(b, u8)
2233 VMRG(h, u16)
2234 VMRG(w, u32)
2235 #undef VMRG_DO
2236 #undef VMRG
2237 #undef MRGHI
2238 #undef MRGLO
2239
2240 void helper_vmsummbm (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2241 {
2242     int32_t prod[16];
2243     int i;
2244
2245     for (i = 0; i < ARRAY_SIZE(r->s8); i++) {
2246         prod[i] = (int32_t)a->s8[i] * b->u8[i];
2247     }
2248
2249     VECTOR_FOR_INORDER_I(i, s32) {
2250         r->s32[i] = c->s32[i] + prod[4*i] + prod[4*i+1] + prod[4*i+2] + prod[4*i+3];
2251     }
2252 }
2253
2254 void helper_vmsumshm (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2255 {
2256     int32_t prod[8];
2257     int i;
2258
2259     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
2260         prod[i] = a->s16[i] * b->s16[i];
2261     }
2262
2263     VECTOR_FOR_INORDER_I(i, s32) {
2264         r->s32[i] = c->s32[i] + prod[2*i] + prod[2*i+1];
2265     }
2266 }
2267
2268 void helper_vmsumshs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2269 {
2270     int32_t prod[8];
2271     int i;
2272     int sat = 0;
2273
2274     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
2275         prod[i] = (int32_t)a->s16[i] * b->s16[i];
2276     }
2277
2278     VECTOR_FOR_INORDER_I (i, s32) {
2279         int64_t t = (int64_t)c->s32[i] + prod[2*i] + prod[2*i+1];
2280         r->u32[i] = cvtsdsw(t, &sat);
2281     }
2282
2283     if (sat) {
2284         env->vscr |= (1 << VSCR_SAT);
2285     }
2286 }
2287
2288 void helper_vmsumubm (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2289 {
2290     uint16_t prod[16];
2291     int i;
2292
2293     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
2294         prod[i] = a->u8[i] * b->u8[i];
2295     }
2296
2297     VECTOR_FOR_INORDER_I(i, u32) {
2298         r->u32[i] = c->u32[i] + prod[4*i] + prod[4*i+1] + prod[4*i+2] + prod[4*i+3];
2299     }
2300 }
2301
2302 void helper_vmsumuhm (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2303 {
2304     uint32_t prod[8];
2305     int i;
2306
2307     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
2308         prod[i] = a->u16[i] * b->u16[i];
2309     }
2310
2311     VECTOR_FOR_INORDER_I(i, u32) {
2312         r->u32[i] = c->u32[i] + prod[2*i] + prod[2*i+1];
2313     }
2314 }
2315
2316 void helper_vmsumuhs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2317 {
2318     uint32_t prod[8];
2319     int i;
2320     int sat = 0;
2321
2322     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
2323         prod[i] = a->u16[i] * b->u16[i];
2324     }
2325
2326     VECTOR_FOR_INORDER_I (i, s32) {
2327         uint64_t t = (uint64_t)c->u32[i] + prod[2*i] + prod[2*i+1];
2328         r->u32[i] = cvtuduw(t, &sat);
2329     }
2330
2331     if (sat) {
2332         env->vscr |= (1 << VSCR_SAT);
2333     }
2334 }
2335
2336 #define VMUL_DO(name, mul_element, prod_element, evenp)                 \
2337     void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)      \
2338     {                                                                   \
2339         int i;                                                          \
2340         VECTOR_FOR_INORDER_I(i, prod_element) {                         \
2341             if (evenp) {                                                \
2342                 r->prod_element[i] = a->mul_element[i*2+HI_IDX] * b->mul_element[i*2+HI_IDX]; \
2343             } else {                                                    \
2344                 r->prod_element[i] = a->mul_element[i*2+LO_IDX] * b->mul_element[i*2+LO_IDX]; \
2345             }                                                           \
2346         }                                                               \
2347     }
2348 #define VMUL(suffix, mul_element, prod_element) \
2349   VMUL_DO(mule##suffix, mul_element, prod_element, 1) \
2350   VMUL_DO(mulo##suffix, mul_element, prod_element, 0)
2351 VMUL(sb, s8, s16)
2352 VMUL(sh, s16, s32)
2353 VMUL(ub, u8, u16)
2354 VMUL(uh, u16, u32)
2355 #undef VMUL_DO
2356 #undef VMUL
2357
2358 void helper_vperm (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2359 {
2360     ppc_avr_t result;
2361     int i;
2362     VECTOR_FOR_INORDER_I (i, u8) {
2363         int s = c->u8[i] & 0x1f;
2364 #if defined(WORDS_BIGENDIAN)
2365         int index = s & 0xf;
2366 #else
2367         int index = 15 - (s & 0xf);
2368 #endif
2369         if (s & 0x10) {
2370             result.u8[i] = b->u8[index];
2371         } else {
2372             result.u8[i] = a->u8[index];
2373         }
2374     }
2375     *r = result;
2376 }
2377
2378 #if defined(WORDS_BIGENDIAN)
2379 #define PKBIG 1
2380 #else
2381 #define PKBIG 0
2382 #endif
2383 void helper_vpkpx (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2384 {
2385     int i, j;
2386     ppc_avr_t result;
2387 #if defined(WORDS_BIGENDIAN)
2388     const ppc_avr_t *x[2] = { a, b };
2389 #else
2390     const ppc_avr_t *x[2] = { b, a };
2391 #endif
2392
2393     VECTOR_FOR_INORDER_I (i, u64) {
2394         VECTOR_FOR_INORDER_I (j, u32){
2395             uint32_t e = x[i]->u32[j];
2396             result.u16[4*i+j] = (((e >> 9) & 0xfc00) |
2397                                  ((e >> 6) & 0x3e0) |
2398                                  ((e >> 3) & 0x1f));
2399         }
2400     }
2401     *r = result;
2402 }
2403
2404 #define VPK(suffix, from, to, cvt, dosat)       \
2405     void helper_vpk##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
2406     {                                                                   \
2407         int i;                                                          \
2408         int sat = 0;                                                    \
2409         ppc_avr_t result;                                               \
2410         ppc_avr_t *a0 = PKBIG ? a : b;                                  \
2411         ppc_avr_t *a1 = PKBIG ? b : a;                                  \
2412         VECTOR_FOR_INORDER_I (i, from) {                                \
2413             result.to[i] = cvt(a0->from[i], &sat);                      \
2414             result.to[i+ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);  \
2415         }                                                               \
2416         *r = result;                                                    \
2417         if (dosat && sat) {                                             \
2418             env->vscr |= (1 << VSCR_SAT);                               \
2419         }                                                               \
2420     }
2421 #define I(x, y) (x)
2422 VPK(shss, s16, s8, cvtshsb, 1)
2423 VPK(shus, s16, u8, cvtshub, 1)
2424 VPK(swss, s32, s16, cvtswsh, 1)
2425 VPK(swus, s32, u16, cvtswuh, 1)
2426 VPK(uhus, u16, u8, cvtuhub, 1)
2427 VPK(uwus, u32, u16, cvtuwuh, 1)
2428 VPK(uhum, u16, u8, I, 0)
2429 VPK(uwum, u32, u16, I, 0)
2430 #undef I
2431 #undef VPK
2432 #undef PKBIG
2433
2434 #define VROTATE(suffix, element)                                        \
2435     void helper_vrl##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
2436     {                                                                   \
2437         int i;                                                          \
2438         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2439             unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2440             unsigned int shift = b->element[i] & mask;                  \
2441             r->element[i] = (a->element[i] << shift) | (a->element[i] >> (sizeof(a->element[0]) * 8 - shift)); \
2442         }                                                               \
2443     }
2444 VROTATE(b, u8)
2445 VROTATE(h, u16)
2446 VROTATE(w, u32)
2447 #undef VROTATE
2448
2449 void helper_vsel (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2450 {
2451     r->u64[0] = (a->u64[0] & ~c->u64[0]) | (b->u64[0] & c->u64[0]);
2452     r->u64[1] = (a->u64[1] & ~c->u64[1]) | (b->u64[1] & c->u64[1]);
2453 }
2454
2455 #if defined(WORDS_BIGENDIAN)
2456 #define LEFT 0
2457 #define RIGHT 1
2458 #else
2459 #define LEFT 1
2460 #define RIGHT 0
2461 #endif
2462 /* The specification says that the results are undefined if all of the
2463  * shift counts are not identical.  We check to make sure that they are
2464  * to conform to what real hardware appears to do.  */
2465 #define VSHIFT(suffix, leftp)                                           \
2466     void helper_vs##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)   \
2467     {                                                                   \
2468         int shift = b->u8[LO_IDX*0x15] & 0x7;                           \
2469         int doit = 1;                                                   \
2470         int i;                                                          \
2471         for (i = 0; i < ARRAY_SIZE(r->u8); i++) {                       \
2472             doit = doit && ((b->u8[i] & 0x7) == shift);                 \
2473         }                                                               \
2474         if (doit) {                                                     \
2475             if (shift == 0) {                                           \
2476                 *r = *a;                                                \
2477             } else if (leftp) {                                         \
2478                 uint64_t carry = a->u64[LO_IDX] >> (64 - shift);        \
2479                 r->u64[HI_IDX] = (a->u64[HI_IDX] << shift) | carry;     \
2480                 r->u64[LO_IDX] = a->u64[LO_IDX] << shift;               \
2481             } else {                                                    \
2482                 uint64_t carry = a->u64[HI_IDX] << (64 - shift);        \
2483                 r->u64[LO_IDX] = (a->u64[LO_IDX] >> shift) | carry;     \
2484                 r->u64[HI_IDX] = a->u64[HI_IDX] >> shift;               \
2485             }                                                           \
2486         }                                                               \
2487     }
2488 VSHIFT(l, LEFT)
2489 VSHIFT(r, RIGHT)
2490 #undef VSHIFT
2491 #undef LEFT
2492 #undef RIGHT
2493
2494 #define VSL(suffix, element)                                            \
2495     void helper_vsl##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
2496     {                                                                   \
2497         int i;                                                          \
2498         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2499             unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2500             unsigned int shift = b->element[i] & mask;                  \
2501             r->element[i] = a->element[i] << shift;                     \
2502         }                                                               \
2503     }
2504 VSL(b, u8)
2505 VSL(h, u16)
2506 VSL(w, u32)
2507 #undef VSL
2508
2509 void helper_vsldoi (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t shift)
2510 {
2511     int sh = shift & 0xf;
2512     int i;
2513     ppc_avr_t result;
2514
2515 #if defined(WORDS_BIGENDIAN)
2516     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
2517         int index = sh + i;
2518         if (index > 0xf) {
2519             result.u8[i] = b->u8[index-0x10];
2520         } else {
2521             result.u8[i] = a->u8[index];
2522         }
2523     }
2524 #else
2525     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
2526         int index = (16 - sh) + i;
2527         if (index > 0xf) {
2528             result.u8[i] = a->u8[index-0x10];
2529         } else {
2530             result.u8[i] = b->u8[index];
2531         }
2532     }
2533 #endif
2534     *r = result;
2535 }
2536
2537 void helper_vslo (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2538 {
2539   int sh = (b->u8[LO_IDX*0xf] >> 3) & 0xf;
2540
2541 #if defined (WORDS_BIGENDIAN)
2542   memmove (&r->u8[0], &a->u8[sh], 16-sh);
2543   memset (&r->u8[16-sh], 0, sh);
2544 #else
2545   memmove (&r->u8[sh], &a->u8[0], 16-sh);
2546   memset (&r->u8[0], 0, sh);
2547 #endif
2548 }
2549
2550 /* Experimental testing shows that hardware masks the immediate.  */
2551 #define _SPLAT_MASKED(element) (splat & (ARRAY_SIZE(r->element) - 1))
2552 #if defined(WORDS_BIGENDIAN)
2553 #define SPLAT_ELEMENT(element) _SPLAT_MASKED(element)
2554 #else
2555 #define SPLAT_ELEMENT(element) (ARRAY_SIZE(r->element)-1 - _SPLAT_MASKED(element))
2556 #endif
2557 #define VSPLT(suffix, element)                                          \
2558     void helper_vsplt##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t splat) \
2559     {                                                                   \
2560         uint32_t s = b->element[SPLAT_ELEMENT(element)];                \
2561         int i;                                                          \
2562         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2563             r->element[i] = s;                                          \
2564         }                                                               \
2565     }
2566 VSPLT(b, u8)
2567 VSPLT(h, u16)
2568 VSPLT(w, u32)
2569 #undef VSPLT
2570 #undef SPLAT_ELEMENT
2571 #undef _SPLAT_MASKED
2572
2573 #define VSPLTI(suffix, element, splat_type)                     \
2574     void helper_vspltis##suffix (ppc_avr_t *r, uint32_t splat)  \
2575     {                                                           \
2576         splat_type x = (int8_t)(splat << 3) >> 3;               \
2577         int i;                                                  \
2578         for (i = 0; i < ARRAY_SIZE(r->element); i++) {          \
2579             r->element[i] = x;                                  \
2580         }                                                       \
2581     }
2582 VSPLTI(b, s8, int8_t)
2583 VSPLTI(h, s16, int16_t)
2584 VSPLTI(w, s32, int32_t)
2585 #undef VSPLTI
2586
2587 #define VSR(suffix, element)                                            \
2588     void helper_vsr##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
2589     {                                                                   \
2590         int i;                                                          \
2591         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2592             unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2593             unsigned int shift = b->element[i] & mask;                  \
2594             r->element[i] = a->element[i] >> shift;                     \
2595         }                                                               \
2596     }
2597 VSR(ab, s8)
2598 VSR(ah, s16)
2599 VSR(aw, s32)
2600 VSR(b, u8)
2601 VSR(h, u16)
2602 VSR(w, u32)
2603 #undef VSR
2604
2605 void helper_vsro (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2606 {
2607   int sh = (b->u8[LO_IDX*0xf] >> 3) & 0xf;
2608
2609 #if defined (WORDS_BIGENDIAN)
2610   memmove (&r->u8[sh], &a->u8[0], 16-sh);
2611   memset (&r->u8[0], 0, sh);
2612 #else
2613   memmove (&r->u8[0], &a->u8[sh], 16-sh);
2614   memset (&r->u8[16-sh], 0, sh);
2615 #endif
2616 }
2617
2618 void helper_vsubcuw (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2619 {
2620     int i;
2621     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2622         r->u32[i] = a->u32[i] >= b->u32[i];
2623     }
2624 }
2625
2626 void helper_vsumsws (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2627 {
2628     int64_t t;
2629     int i, upper;
2630     ppc_avr_t result;
2631     int sat = 0;
2632
2633 #if defined(WORDS_BIGENDIAN)
2634     upper = ARRAY_SIZE(r->s32)-1;
2635 #else
2636     upper = 0;
2637 #endif
2638     t = (int64_t)b->s32[upper];
2639     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2640         t += a->s32[i];
2641         result.s32[i] = 0;
2642     }
2643     result.s32[upper] = cvtsdsw(t, &sat);
2644     *r = result;
2645
2646     if (sat) {
2647         env->vscr |= (1 << VSCR_SAT);
2648     }
2649 }
2650
2651 void helper_vsum2sws (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2652 {
2653     int i, j, upper;
2654     ppc_avr_t result;
2655     int sat = 0;
2656
2657 #if defined(WORDS_BIGENDIAN)
2658     upper = 1;
2659 #else
2660     upper = 0;
2661 #endif
2662     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
2663         int64_t t = (int64_t)b->s32[upper+i*2];
2664         result.u64[i] = 0;
2665         for (j = 0; j < ARRAY_SIZE(r->u64); j++) {
2666             t += a->s32[2*i+j];
2667         }
2668         result.s32[upper+i*2] = cvtsdsw(t, &sat);
2669     }
2670
2671     *r = result;
2672     if (sat) {
2673         env->vscr |= (1 << VSCR_SAT);
2674     }
2675 }
2676
2677 void helper_vsum4sbs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2678 {
2679     int i, j;
2680     int sat = 0;
2681
2682     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2683         int64_t t = (int64_t)b->s32[i];
2684         for (j = 0; j < ARRAY_SIZE(r->s32); j++) {
2685             t += a->s8[4*i+j];
2686         }
2687         r->s32[i] = cvtsdsw(t, &sat);
2688     }
2689
2690     if (sat) {
2691         env->vscr |= (1 << VSCR_SAT);
2692     }
2693 }
2694
2695 void helper_vsum4shs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2696 {
2697     int sat = 0;
2698     int i;
2699
2700     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2701         int64_t t = (int64_t)b->s32[i];
2702         t += a->s16[2*i] + a->s16[2*i+1];
2703         r->s32[i] = cvtsdsw(t, &sat);
2704     }
2705
2706     if (sat) {
2707         env->vscr |= (1 << VSCR_SAT);
2708     }
2709 }
2710
2711 void helper_vsum4ubs (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2712 {
2713     int i, j;
2714     int sat = 0;
2715
2716     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2717         uint64_t t = (uint64_t)b->u32[i];
2718         for (j = 0; j < ARRAY_SIZE(r->u32); j++) {
2719             t += a->u8[4*i+j];
2720         }
2721         r->u32[i] = cvtuduw(t, &sat);
2722     }
2723
2724     if (sat) {
2725         env->vscr |= (1 << VSCR_SAT);
2726     }
2727 }
2728
2729 #if defined(WORDS_BIGENDIAN)
2730 #define UPKHI 1
2731 #define UPKLO 0
2732 #else
2733 #define UPKHI 0
2734 #define UPKLO 1
2735 #endif
2736 #define VUPKPX(suffix, hi)                                      \
2737     void helper_vupk##suffix (ppc_avr_t *r, ppc_avr_t *b)       \
2738     {                                                           \
2739         int i;                                                  \
2740         ppc_avr_t result;                                       \
2741         for (i = 0; i < ARRAY_SIZE(r->u32); i++) {              \
2742             uint16_t e = b->u16[hi ? i : i+4];                  \
2743             uint8_t a = (e >> 15) ? 0xff : 0;                   \
2744             uint8_t r = (e >> 10) & 0x1f;                       \
2745             uint8_t g = (e >> 5) & 0x1f;                        \
2746             uint8_t b = e & 0x1f;                               \
2747             result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b;       \
2748         }                                                               \
2749         *r = result;                                                    \
2750     }
2751 VUPKPX(lpx, UPKLO)
2752 VUPKPX(hpx, UPKHI)
2753 #undef VUPKPX
2754
2755 #define VUPK(suffix, unpacked, packee, hi)                              \
2756     void helper_vupk##suffix (ppc_avr_t *r, ppc_avr_t *b)               \
2757     {                                                                   \
2758         int i;                                                          \
2759         ppc_avr_t result;                                               \
2760         if (hi) {                                                       \
2761             for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) {             \
2762                 result.unpacked[i] = b->packee[i];                      \
2763             }                                                           \
2764         } else {                                                        \
2765             for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); i++) { \
2766                 result.unpacked[i-ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
2767             }                                                           \
2768         }                                                               \
2769         *r = result;                                                    \
2770     }
2771 VUPK(hsb, s16, s8, UPKHI)
2772 VUPK(hsh, s32, s16, UPKHI)
2773 VUPK(lsb, s16, s8, UPKLO)
2774 VUPK(lsh, s32, s16, UPKLO)
2775 #undef VUPK
2776 #undef UPKHI
2777 #undef UPKLO
2778
2779 #undef VECTOR_FOR_INORDER_I
2780 #undef HI_IDX
2781 #undef LO_IDX
2782
2783 /*****************************************************************************/
2784 /* SPE extension helpers */
2785 /* Use a table to make this quicker */
2786 static uint8_t hbrev[16] = {
2787     0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
2788     0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
2789 };
2790
2791 static always_inline uint8_t byte_reverse (uint8_t val)
2792 {
2793     return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
2794 }
2795
2796 static always_inline uint32_t word_reverse (uint32_t val)
2797 {
2798     return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
2799         (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
2800 }
2801
2802 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
2803 target_ulong helper_brinc (target_ulong arg1, target_ulong arg2)
2804 {
2805     uint32_t a, b, d, mask;
2806
2807     mask = UINT32_MAX >> (32 - MASKBITS);
2808     a = arg1 & mask;
2809     b = arg2 & mask;
2810     d = word_reverse(1 + word_reverse(a | ~b));
2811     return (arg1 & ~mask) | (d & b);
2812 }
2813
2814 uint32_t helper_cntlsw32 (uint32_t val)
2815 {
2816     if (val & 0x80000000)
2817         return clz32(~val);
2818     else
2819         return clz32(val);
2820 }
2821
2822 uint32_t helper_cntlzw32 (uint32_t val)
2823 {
2824     return clz32(val);
2825 }
2826
2827 /* Single-precision floating-point conversions */
2828 static always_inline uint32_t efscfsi (uint32_t val)
2829 {
2830     CPU_FloatU u;
2831
2832     u.f = int32_to_float32(val, &env->spe_status);
2833
2834     return u.l;
2835 }
2836
2837 static always_inline uint32_t efscfui (uint32_t val)
2838 {
2839     CPU_FloatU u;
2840
2841     u.f = uint32_to_float32(val, &env->spe_status);
2842
2843     return u.l;
2844 }
2845
2846 static always_inline int32_t efsctsi (uint32_t val)
2847 {
2848     CPU_FloatU u;
2849
2850     u.l = val;
2851     /* NaN are not treated the same way IEEE 754 does */
2852     if (unlikely(float32_is_nan(u.f)))
2853         return 0;
2854
2855     return float32_to_int32(u.f, &env->spe_status);
2856 }
2857
2858 static always_inline uint32_t efsctui (uint32_t val)
2859 {
2860     CPU_FloatU u;
2861
2862     u.l = val;
2863     /* NaN are not treated the same way IEEE 754 does */
2864     if (unlikely(float32_is_nan(u.f)))
2865         return 0;
2866
2867     return float32_to_uint32(u.f, &env->spe_status);
2868 }
2869
2870 static always_inline uint32_t efsctsiz (uint32_t val)
2871 {
2872     CPU_FloatU u;
2873
2874     u.l = val;
2875     /* NaN are not treated the same way IEEE 754 does */
2876     if (unlikely(float32_is_nan(u.f)))
2877         return 0;
2878
2879     return float32_to_int32_round_to_zero(u.f, &env->spe_status);
2880 }
2881
2882 static always_inline uint32_t efsctuiz (uint32_t val)
2883 {
2884     CPU_FloatU u;
2885
2886     u.l = val;
2887     /* NaN are not treated the same way IEEE 754 does */
2888     if (unlikely(float32_is_nan(u.f)))
2889         return 0;
2890
2891     return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
2892 }
2893
2894 static always_inline uint32_t efscfsf (uint32_t val)
2895 {
2896     CPU_FloatU u;
2897     float32 tmp;
2898
2899     u.f = int32_to_float32(val, &env->spe_status);
2900     tmp = int64_to_float32(1ULL << 32, &env->spe_status);
2901     u.f = float32_div(u.f, tmp, &env->spe_status);
2902
2903     return u.l;
2904 }
2905
2906 static always_inline uint32_t efscfuf (uint32_t val)
2907 {
2908     CPU_FloatU u;
2909     float32 tmp;
2910
2911     u.f = uint32_to_float32(val, &env->spe_status);
2912     tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2913     u.f = float32_div(u.f, tmp, &env->spe_status);
2914
2915     return u.l;
2916 }
2917
2918 static always_inline uint32_t efsctsf (uint32_t val)
2919 {
2920     CPU_FloatU u;
2921     float32 tmp;
2922
2923     u.l = val;
2924     /* NaN are not treated the same way IEEE 754 does */
2925     if (unlikely(float32_is_nan(u.f)))
2926         return 0;
2927     tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2928     u.f = float32_mul(u.f, tmp, &env->spe_status);
2929
2930     return float32_to_int32(u.f, &env->spe_status);
2931 }
2932
2933 static always_inline uint32_t efsctuf (uint32_t val)
2934 {
2935     CPU_FloatU u;
2936     float32 tmp;
2937
2938     u.l = val;
2939     /* NaN are not treated the same way IEEE 754 does */
2940     if (unlikely(float32_is_nan(u.f)))
2941         return 0;
2942     tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2943     u.f = float32_mul(u.f, tmp, &env->spe_status);
2944
2945     return float32_to_uint32(u.f, &env->spe_status);
2946 }
2947
2948 #define HELPER_SPE_SINGLE_CONV(name)                                          \
2949 uint32_t helper_e##name (uint32_t val)                                        \
2950 {                                                                             \
2951     return e##name(val);                                                      \
2952 }
2953 /* efscfsi */
2954 HELPER_SPE_SINGLE_CONV(fscfsi);
2955 /* efscfui */
2956 HELPER_SPE_SINGLE_CONV(fscfui);
2957 /* efscfuf */
2958 HELPER_SPE_SINGLE_CONV(fscfuf);
2959 /* efscfsf */
2960 HELPER_SPE_SINGLE_CONV(fscfsf);
2961 /* efsctsi */
2962 HELPER_SPE_SINGLE_CONV(fsctsi);
2963 /* efsctui */
2964 HELPER_SPE_SINGLE_CONV(fsctui);
2965 /* efsctsiz */
2966 HELPER_SPE_SINGLE_CONV(fsctsiz);
2967 /* efsctuiz */
2968 HELPER_SPE_SINGLE_CONV(fsctuiz);
2969 /* efsctsf */
2970 HELPER_SPE_SINGLE_CONV(fsctsf);
2971 /* efsctuf */
2972 HELPER_SPE_SINGLE_CONV(fsctuf);
2973
2974 #define HELPER_SPE_VECTOR_CONV(name)                                          \
2975 uint64_t helper_ev##name (uint64_t val)                                       \
2976 {                                                                             \
2977     return ((uint64_t)e##name(val >> 32) << 32) |                             \
2978             (uint64_t)e##name(val);                                           \
2979 }
2980 /* evfscfsi */
2981 HELPER_SPE_VECTOR_CONV(fscfsi);
2982 /* evfscfui */
2983 HELPER_SPE_VECTOR_CONV(fscfui);
2984 /* evfscfuf */
2985 HELPER_SPE_VECTOR_CONV(fscfuf);
2986 /* evfscfsf */
2987 HELPER_SPE_VECTOR_CONV(fscfsf);
2988 /* evfsctsi */
2989 HELPER_SPE_VECTOR_CONV(fsctsi);
2990 /* evfsctui */
2991 HELPER_SPE_VECTOR_CONV(fsctui);
2992 /* evfsctsiz */
2993 HELPER_SPE_VECTOR_CONV(fsctsiz);
2994 /* evfsctuiz */
2995 HELPER_SPE_VECTOR_CONV(fsctuiz);
2996 /* evfsctsf */
2997 HELPER_SPE_VECTOR_CONV(fsctsf);
2998 /* evfsctuf */
2999 HELPER_SPE_VECTOR_CONV(fsctuf);
3000
3001 /* Single-precision floating-point arithmetic */
3002 static always_inline uint32_t efsadd (uint32_t op1, uint32_t op2)
3003 {
3004     CPU_FloatU u1, u2;
3005     u1.l = op1;
3006     u2.l = op2;
3007     u1.f = float32_add(u1.f, u2.f, &env->spe_status);
3008     return u1.l;
3009 }
3010
3011 static always_inline uint32_t efssub (uint32_t op1, uint32_t op2)
3012 {
3013     CPU_FloatU u1, u2;
3014     u1.l = op1;
3015     u2.l = op2;
3016     u1.f = float32_sub(u1.f, u2.f, &env->spe_status);
3017     return u1.l;
3018 }
3019
3020 static always_inline uint32_t efsmul (uint32_t op1, uint32_t op2)
3021 {
3022     CPU_FloatU u1, u2;
3023     u1.l = op1;
3024     u2.l = op2;
3025     u1.f = float32_mul(u1.f, u2.f, &env->spe_status);
3026     return u1.l;
3027 }
3028
3029 static always_inline uint32_t efsdiv (uint32_t op1, uint32_t op2)
3030 {
3031     CPU_FloatU u1, u2;
3032     u1.l = op1;
3033     u2.l = op2;
3034     u1.f = float32_div(u1.f, u2.f, &env->spe_status);
3035     return u1.l;
3036 }
3037
3038 #define HELPER_SPE_SINGLE_ARITH(name)                                         \
3039 uint32_t helper_e##name (uint32_t op1, uint32_t op2)                          \
3040 {                                                                             \
3041     return e##name(op1, op2);                                                 \
3042 }
3043 /* efsadd */
3044 HELPER_SPE_SINGLE_ARITH(fsadd);
3045 /* efssub */
3046 HELPER_SPE_SINGLE_ARITH(fssub);
3047 /* efsmul */
3048 HELPER_SPE_SINGLE_ARITH(fsmul);
3049 /* efsdiv */
3050 HELPER_SPE_SINGLE_ARITH(fsdiv);
3051
3052 #define HELPER_SPE_VECTOR_ARITH(name)                                         \
3053 uint64_t helper_ev##name (uint64_t op1, uint64_t op2)                         \
3054 {                                                                             \
3055     return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) |                  \
3056             (uint64_t)e##name(op1, op2);                                      \
3057 }
3058 /* evfsadd */
3059 HELPER_SPE_VECTOR_ARITH(fsadd);
3060 /* evfssub */
3061 HELPER_SPE_VECTOR_ARITH(fssub);
3062 /* evfsmul */
3063 HELPER_SPE_VECTOR_ARITH(fsmul);
3064 /* evfsdiv */
3065 HELPER_SPE_VECTOR_ARITH(fsdiv);
3066
3067 /* Single-precision floating-point comparisons */
3068 static always_inline uint32_t efststlt (uint32_t op1, uint32_t op2)
3069 {
3070     CPU_FloatU u1, u2;
3071     u1.l = op1;
3072     u2.l = op2;
3073     return float32_lt(u1.f, u2.f, &env->spe_status) ? 4 : 0;
3074 }
3075
3076 static always_inline uint32_t efststgt (uint32_t op1, uint32_t op2)
3077 {
3078     CPU_FloatU u1, u2;
3079     u1.l = op1;
3080     u2.l = op2;
3081     return float32_le(u1.f, u2.f, &env->spe_status) ? 0 : 4;
3082 }
3083
3084 static always_inline uint32_t efststeq (uint32_t op1, uint32_t op2)
3085 {
3086     CPU_FloatU u1, u2;
3087     u1.l = op1;
3088     u2.l = op2;
3089     return float32_eq(u1.f, u2.f, &env->spe_status) ? 4 : 0;
3090 }
3091
3092 static always_inline uint32_t efscmplt (uint32_t op1, uint32_t op2)
3093 {
3094     /* XXX: TODO: test special values (NaN, infinites, ...) */
3095     return efststlt(op1, op2);
3096 }
3097
3098 static always_inline uint32_t efscmpgt (uint32_t op1, uint32_t op2)
3099 {
3100     /* XXX: TODO: test special values (NaN, infinites, ...) */
3101     return efststgt(op1, op2);
3102 }
3103
3104 static always_inline uint32_t efscmpeq (uint32_t op1, uint32_t op2)
3105 {
3106     /* XXX: TODO: test special values (NaN, infinites, ...) */
3107     return efststeq(op1, op2);
3108 }
3109
3110 #define HELPER_SINGLE_SPE_CMP(name)                                           \
3111 uint32_t helper_e##name (uint32_t op1, uint32_t op2)                          \
3112 {                                                                             \
3113     return e##name(op1, op2) << 2;                                            \
3114 }
3115 /* efststlt */
3116 HELPER_SINGLE_SPE_CMP(fststlt);
3117 /* efststgt */
3118 HELPER_SINGLE_SPE_CMP(fststgt);
3119 /* efststeq */
3120 HELPER_SINGLE_SPE_CMP(fststeq);
3121 /* efscmplt */
3122 HELPER_SINGLE_SPE_CMP(fscmplt);
3123 /* efscmpgt */
3124 HELPER_SINGLE_SPE_CMP(fscmpgt);
3125 /* efscmpeq */
3126 HELPER_SINGLE_SPE_CMP(fscmpeq);
3127
3128 static always_inline uint32_t evcmp_merge (int t0, int t1)
3129 {
3130     return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
3131 }
3132
3133 #define HELPER_VECTOR_SPE_CMP(name)                                           \
3134 uint32_t helper_ev##name (uint64_t op1, uint64_t op2)                         \
3135 {                                                                             \
3136     return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2));     \
3137 }
3138 /* evfststlt */
3139 HELPER_VECTOR_SPE_CMP(fststlt);
3140 /* evfststgt */
3141 HELPER_VECTOR_SPE_CMP(fststgt);
3142 /* evfststeq */
3143 HELPER_VECTOR_SPE_CMP(fststeq);
3144 /* evfscmplt */
3145 HELPER_VECTOR_SPE_CMP(fscmplt);
3146 /* evfscmpgt */
3147 HELPER_VECTOR_SPE_CMP(fscmpgt);
3148 /* evfscmpeq */
3149 HELPER_VECTOR_SPE_CMP(fscmpeq);
3150
3151 /* Double-precision floating-point conversion */
3152 uint64_t helper_efdcfsi (uint32_t val)
3153 {
3154     CPU_DoubleU u;
3155
3156     u.d = int32_to_float64(val, &env->spe_status);
3157
3158     return u.ll;
3159 }
3160
3161 uint64_t helper_efdcfsid (uint64_t val)
3162 {
3163     CPU_DoubleU u;
3164
3165     u.d = int64_to_float64(val, &env->spe_status);
3166
3167     return u.ll;
3168 }
3169
3170 uint64_t helper_efdcfui (uint32_t val)
3171 {
3172     CPU_DoubleU u;
3173
3174     u.d = uint32_to_float64(val, &env->spe_status);
3175
3176     return u.ll;
3177 }
3178
3179 uint64_t helper_efdcfuid (uint64_t val)
3180 {
3181     CPU_DoubleU u;
3182
3183     u.d = uint64_to_float64(val, &env->spe_status);
3184
3185     return u.ll;
3186 }
3187
3188 uint32_t helper_efdctsi (uint64_t val)
3189 {
3190     CPU_DoubleU u;
3191
3192     u.ll = val;
3193     /* NaN are not treated the same way IEEE 754 does */
3194     if (unlikely(float64_is_nan(u.d)))
3195         return 0;
3196
3197     return float64_to_int32(u.d, &env->spe_status);
3198 }
3199
3200 uint32_t helper_efdctui (uint64_t val)
3201 {
3202     CPU_DoubleU u;
3203
3204     u.ll = val;
3205     /* NaN are not treated the same way IEEE 754 does */
3206     if (unlikely(float64_is_nan(u.d)))
3207         return 0;
3208
3209     return float64_to_uint32(u.d, &env->spe_status);
3210 }
3211
3212 uint32_t helper_efdctsiz (uint64_t val)
3213 {
3214     CPU_DoubleU u;
3215
3216     u.ll = val;
3217     /* NaN are not treated the same way IEEE 754 does */
3218     if (unlikely(float64_is_nan(u.d)))
3219         return 0;
3220
3221     return float64_to_int32_round_to_zero(u.d, &env->spe_status);
3222 }
3223
3224 uint64_t helper_efdctsidz (uint64_t val)
3225 {
3226     CPU_DoubleU u;
3227
3228     u.ll = val;
3229     /* NaN are not treated the same way IEEE 754 does */
3230     if (unlikely(float64_is_nan(u.d)))
3231         return 0;
3232
3233     return float64_to_int64_round_to_zero(u.d, &env->spe_status);
3234 }
3235
3236 uint32_t helper_efdctuiz (uint64_t val)
3237 {
3238     CPU_DoubleU u;
3239
3240     u.ll = val;
3241     /* NaN are not treated the same way IEEE 754 does */
3242     if (unlikely(float64_is_nan(u.d)))
3243         return 0;
3244
3245     return float64_to_uint32_round_to_zero(u.d, &env->spe_status);
3246 }
3247
3248 uint64_t helper_efdctuidz (uint64_t val)
3249 {
3250     CPU_DoubleU u;
3251
3252     u.ll = val;
3253     /* NaN are not treated the same way IEEE 754 does */
3254     if (unlikely(float64_is_nan(u.d)))
3255         return 0;
3256
3257     return float64_to_uint64_round_to_zero(u.d, &env->spe_status);
3258 }
3259
3260 uint64_t helper_efdcfsf (uint32_t val)
3261 {
3262     CPU_DoubleU u;
3263     float64 tmp;
3264
3265     u.d = int32_to_float64(val, &env->spe_status);
3266     tmp = int64_to_float64(1ULL << 32, &env->spe_status);
3267     u.d = float64_div(u.d, tmp, &env->spe_status);
3268
3269     return u.ll;
3270 }
3271
3272 uint64_t helper_efdcfuf (uint32_t val)
3273 {
3274     CPU_DoubleU u;
3275     float64 tmp;
3276
3277     u.d = uint32_to_float64(val, &env->spe_status);
3278     tmp = int64_to_float64(1ULL << 32, &env->spe_status);
3279     u.d = float64_div(u.d, tmp, &env->spe_status);
3280
3281     return u.ll;
3282 }
3283
3284 uint32_t helper_efdctsf (uint64_t val)
3285 {
3286     CPU_DoubleU u;
3287     float64 tmp;
3288
3289     u.ll = val;
3290     /* NaN are not treated the same way IEEE 754 does */
3291     if (unlikely(float64_is_nan(u.d)))
3292         return 0;
3293     tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
3294     u.d = float64_mul(u.d, tmp, &env->spe_status);
3295
3296     return float64_to_int32(u.d, &env->spe_status);
3297 }
3298
3299 uint32_t helper_efdctuf (uint64_t val)
3300 {
3301     CPU_DoubleU u;
3302     float64 tmp;
3303
3304     u.ll = val;
3305     /* NaN are not treated the same way IEEE 754 does */
3306     if (unlikely(float64_is_nan(u.d)))
3307         return 0;
3308     tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
3309     u.d = float64_mul(u.d, tmp, &env->spe_status);
3310
3311     return float64_to_uint32(u.d, &env->spe_status);
3312 }
3313
3314 uint32_t helper_efscfd (uint64_t val)
3315 {
3316     CPU_DoubleU u1;
3317     CPU_FloatU u2;
3318
3319     u1.ll = val;
3320     u2.f = float64_to_float32(u1.d, &env->spe_status);
3321
3322     return u2.l;
3323 }
3324
3325 uint64_t helper_efdcfs (uint32_t val)
3326 {
3327     CPU_DoubleU u2;
3328     CPU_FloatU u1;
3329
3330     u1.l = val;
3331     u2.d = float32_to_float64(u1.f, &env->spe_status);
3332
3333     return u2.ll;
3334 }
3335
3336 /* Double precision fixed-point arithmetic */
3337 uint64_t helper_efdadd (uint64_t op1, uint64_t op2)
3338 {
3339     CPU_DoubleU u1, u2;
3340     u1.ll = op1;
3341     u2.ll = op2;
3342     u1.d = float64_add(u1.d, u2.d, &env->spe_status);
3343     return u1.ll;
3344 }
3345
3346 uint64_t helper_efdsub (uint64_t op1, uint64_t op2)
3347 {
3348     CPU_DoubleU u1, u2;
3349     u1.ll = op1;
3350     u2.ll = op2;
3351     u1.d = float64_sub(u1.d, u2.d, &env->spe_status);
3352     return u1.ll;
3353 }
3354
3355 uint64_t helper_efdmul (uint64_t op1, uint64_t op2)
3356 {
3357     CPU_DoubleU u1, u2;
3358     u1.ll = op1;
3359     u2.ll = op2;
3360     u1.d = float64_mul(u1.d, u2.d, &env->spe_status);
3361     return u1.ll;
3362 }
3363
3364 uint64_t helper_efddiv (uint64_t op1, uint64_t op2)
3365 {
3366     CPU_DoubleU u1, u2;
3367     u1.ll = op1;
3368     u2.ll = op2;
3369     u1.d = float64_div(u1.d, u2.d, &env->spe_status);
3370     return u1.ll;
3371 }
3372
3373 /* Double precision floating point helpers */
3374 uint32_t helper_efdtstlt (uint64_t op1, uint64_t op2)
3375 {
3376     CPU_DoubleU u1, u2;
3377     u1.ll = op1;
3378     u2.ll = op2;
3379     return float64_lt(u1.d, u2.d, &env->spe_status) ? 4 : 0;
3380 }
3381
3382 uint32_t helper_efdtstgt (uint64_t op1, uint64_t op2)
3383 {
3384     CPU_DoubleU u1, u2;
3385     u1.ll = op1;
3386     u2.ll = op2;
3387     return float64_le(u1.d, u2.d, &env->spe_status) ? 0 : 4;
3388 }
3389
3390 uint32_t helper_efdtsteq (uint64_t op1, uint64_t op2)
3391 {
3392     CPU_DoubleU u1, u2;
3393     u1.ll = op1;
3394     u2.ll = op2;
3395     return float64_eq(u1.d, u2.d, &env->spe_status) ? 4 : 0;
3396 }
3397
3398 uint32_t helper_efdcmplt (uint64_t op1, uint64_t op2)
3399 {
3400     /* XXX: TODO: test special values (NaN, infinites, ...) */
3401     return helper_efdtstlt(op1, op2);
3402 }
3403
3404 uint32_t helper_efdcmpgt (uint64_t op1, uint64_t op2)
3405 {
3406     /* XXX: TODO: test special values (NaN, infinites, ...) */
3407     return helper_efdtstgt(op1, op2);
3408 }
3409
3410 uint32_t helper_efdcmpeq (uint64_t op1, uint64_t op2)
3411 {
3412     /* XXX: TODO: test special values (NaN, infinites, ...) */
3413     return helper_efdtsteq(op1, op2);
3414 }
3415
3416 /*****************************************************************************/
3417 /* Softmmu support */
3418 #if !defined (CONFIG_USER_ONLY)
3419
3420 #define MMUSUFFIX _mmu
3421
3422 #define SHIFT 0
3423 #include "softmmu_template.h"
3424
3425 #define SHIFT 1
3426 #include "softmmu_template.h"
3427
3428 #define SHIFT 2
3429 #include "softmmu_template.h"
3430
3431 #define SHIFT 3
3432 #include "softmmu_template.h"
3433
3434 /* try to fill the TLB and return an exception if error. If retaddr is
3435    NULL, it means that the function was called in C code (i.e. not
3436    from generated code or from helper.c) */
3437 /* XXX: fix it to restore all registers */
3438 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
3439 {
3440     TranslationBlock *tb;
3441     CPUState *saved_env;
3442     unsigned long pc;
3443     int ret;
3444
3445     /* XXX: hack to restore env in all cases, even if not called from
3446        generated code */
3447     saved_env = env;
3448     env = cpu_single_env;
3449     ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
3450     if (unlikely(ret != 0)) {
3451         if (likely(retaddr)) {
3452             /* now we have a real cpu fault */
3453             pc = (unsigned long)retaddr;
3454             tb = tb_find_pc(pc);
3455             if (likely(tb)) {
3456                 /* the PC is inside the translated code. It means that we have
3457                    a virtual CPU fault */
3458                 cpu_restore_state(tb, env, pc, NULL);
3459             }
3460         }
3461         helper_raise_exception_err(env->exception_index, env->error_code);
3462     }
3463     env = saved_env;
3464 }
3465
3466 /* Segment registers load and store */
3467 target_ulong helper_load_sr (target_ulong sr_num)
3468 {
3469     return env->sr[sr_num];
3470 }
3471
3472 void helper_store_sr (target_ulong sr_num, target_ulong val)
3473 {
3474     ppc_store_sr(env, sr_num, val);
3475 }
3476
3477 /* SLB management */
3478 #if defined(TARGET_PPC64)
3479 target_ulong helper_load_slb (target_ulong slb_nr)
3480 {
3481     return ppc_load_slb(env, slb_nr);
3482 }
3483
3484 void helper_store_slb (target_ulong slb_nr, target_ulong rs)
3485 {
3486     ppc_store_slb(env, slb_nr, rs);
3487 }
3488
3489 void helper_slbia (void)
3490 {
3491     ppc_slb_invalidate_all(env);
3492 }
3493
3494 void helper_slbie (target_ulong addr)
3495 {
3496     ppc_slb_invalidate_one(env, addr);
3497 }
3498
3499 #endif /* defined(TARGET_PPC64) */
3500
3501 /* TLB management */
3502 void helper_tlbia (void)
3503 {
3504     ppc_tlb_invalidate_all(env);
3505 }
3506
3507 void helper_tlbie (target_ulong addr)
3508 {
3509     ppc_tlb_invalidate_one(env, addr);
3510 }
3511
3512 /* Software driven TLBs management */
3513 /* PowerPC 602/603 software TLB load instructions helpers */
3514 static void do_6xx_tlb (target_ulong new_EPN, int is_code)
3515 {
3516     target_ulong RPN, CMP, EPN;
3517     int way;
3518
3519     RPN = env->spr[SPR_RPA];
3520     if (is_code) {
3521         CMP = env->spr[SPR_ICMP];
3522         EPN = env->spr[SPR_IMISS];
3523     } else {
3524         CMP = env->spr[SPR_DCMP];
3525         EPN = env->spr[SPR_DMISS];
3526     }
3527     way = (env->spr[SPR_SRR1] >> 17) & 1;
3528 #if defined (DEBUG_SOFTWARE_TLB)
3529     if (loglevel != 0) {
3530         fprintf(logfile, "%s: EPN " ADDRX " " ADDRX " PTE0 " ADDRX
3531                 " PTE1 " ADDRX " way %d\n",
3532                 __func__, new_EPN, EPN, CMP, RPN, way);
3533     }
3534 #endif
3535     /* Store this TLB */
3536     ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
3537                      way, is_code, CMP, RPN);
3538 }
3539
3540 void helper_6xx_tlbd (target_ulong EPN)
3541 {
3542     do_6xx_tlb(EPN, 0);
3543 }
3544
3545 void helper_6xx_tlbi (target_ulong EPN)
3546 {
3547     do_6xx_tlb(EPN, 1);
3548 }
3549
3550 /* PowerPC 74xx software TLB load instructions helpers */
3551 static void do_74xx_tlb (target_ulong new_EPN, int is_code)
3552 {
3553     target_ulong RPN, CMP, EPN;
3554     int way;
3555
3556     RPN = env->spr[SPR_PTELO];
3557     CMP = env->spr[SPR_PTEHI];
3558     EPN = env->spr[SPR_TLBMISS] & ~0x3;
3559     way = env->spr[SPR_TLBMISS] & 0x3;
3560 #if defined (DEBUG_SOFTWARE_TLB)
3561     if (loglevel != 0) {
3562         fprintf(logfile, "%s: EPN " ADDRX " " ADDRX " PTE0 " ADDRX
3563                 " PTE1 " ADDRX " way %d\n",
3564                 __func__, new_EPN, EPN, CMP, RPN, way);
3565     }
3566 #endif
3567     /* Store this TLB */
3568     ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
3569                      way, is_code, CMP, RPN);
3570 }
3571
3572 void helper_74xx_tlbd (target_ulong EPN)
3573 {
3574     do_74xx_tlb(EPN, 0);
3575 }
3576
3577 void helper_74xx_tlbi (target_ulong EPN)
3578 {
3579     do_74xx_tlb(EPN, 1);
3580 }
3581
3582 static always_inline target_ulong booke_tlb_to_page_size (int size)
3583 {
3584     return 1024 << (2 * size);
3585 }
3586
3587 static always_inline int booke_page_size_to_tlb (target_ulong page_size)
3588 {
3589     int size;
3590
3591     switch (page_size) {
3592     case 0x00000400UL:
3593         size = 0x0;
3594         break;
3595     case 0x00001000UL:
3596         size = 0x1;
3597         break;
3598     case 0x00004000UL:
3599         size = 0x2;
3600         break;
3601     case 0x00010000UL:
3602         size = 0x3;
3603         break;
3604     case 0x00040000UL:
3605         size = 0x4;
3606         break;
3607     case 0x00100000UL:
3608         size = 0x5;
3609         break;
3610     case 0x00400000UL:
3611         size = 0x6;
3612         break;
3613     case 0x01000000UL:
3614         size = 0x7;
3615         break;
3616     case 0x04000000UL:
3617         size = 0x8;
3618         break;
3619     case 0x10000000UL:
3620         size = 0x9;
3621         break;
3622     case 0x40000000UL:
3623         size = 0xA;
3624         break;
3625 #if defined (TARGET_PPC64)
3626     case 0x000100000000ULL:
3627         size = 0xB;
3628         break;
3629     case 0x000400000000ULL:
3630         size = 0xC;
3631         break;
3632     case 0x001000000000ULL:
3633         size = 0xD;
3634         break;
3635     case 0x004000000000ULL:
3636         size = 0xE;
3637         break;
3638     case 0x010000000000ULL:
3639         size = 0xF;
3640         break;
3641 #endif
3642     default:
3643         size = -1;
3644         break;
3645     }
3646
3647     return size;
3648 }
3649
3650 /* Helpers for 4xx TLB management */
3651 target_ulong helper_4xx_tlbre_lo (target_ulong entry)
3652 {
3653     ppcemb_tlb_t *tlb;
3654     target_ulong ret;
3655     int size;
3656
3657     entry &= 0x3F;
3658     tlb = &env->tlb[entry].tlbe;
3659     ret = tlb->EPN;
3660     if (tlb->prot & PAGE_VALID)
3661         ret |= 0x400;
3662     size = booke_page_size_to_tlb(tlb->size);
3663     if (size < 0 || size > 0x7)
3664         size = 1;
3665     ret |= size << 7;
3666     env->spr[SPR_40x_PID] = tlb->PID;
3667     return ret;
3668 }
3669
3670 target_ulong helper_4xx_tlbre_hi (target_ulong entry)
3671 {
3672     ppcemb_tlb_t *tlb;
3673     target_ulong ret;
3674
3675     entry &= 0x3F;
3676     tlb = &env->tlb[entry].tlbe;
3677     ret = tlb->RPN;
3678     if (tlb->prot & PAGE_EXEC)
3679         ret |= 0x200;
3680     if (tlb->prot & PAGE_WRITE)
3681         ret |= 0x100;
3682     return ret;
3683 }
3684
3685 void helper_4xx_tlbwe_hi (target_ulong entry, target_ulong val)
3686 {
3687     ppcemb_tlb_t *tlb;
3688     target_ulong page, end;
3689
3690 #if defined (DEBUG_SOFTWARE_TLB)
3691     if (loglevel != 0) {
3692         fprintf(logfile, "%s entry %d val " ADDRX "\n", __func__, (int)entry, val);
3693     }
3694 #endif
3695     entry &= 0x3F;
3696     tlb = &env->tlb[entry].tlbe;
3697     /* Invalidate previous TLB (if it's valid) */
3698     if (tlb->prot & PAGE_VALID) {
3699         end = tlb->EPN + tlb->size;
3700 #if defined (DEBUG_SOFTWARE_TLB)
3701         if (loglevel != 0) {
3702             fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
3703                     " end " ADDRX "\n", __func__, (int)entry, tlb->EPN, end);
3704         }
3705 #endif
3706         for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
3707             tlb_flush_page(env, page);
3708     }
3709     tlb->size = booke_tlb_to_page_size((val >> 7) & 0x7);
3710     /* We cannot handle TLB size < TARGET_PAGE_SIZE.
3711      * If this ever occurs, one should use the ppcemb target instead
3712      * of the ppc or ppc64 one
3713      */
3714     if ((val & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
3715         cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
3716                   "are not supported (%d)\n",
3717                   tlb->size, TARGET_PAGE_SIZE, (int)((val >> 7) & 0x7));
3718     }
3719     tlb->EPN = val & ~(tlb->size - 1);
3720     if (val & 0x40)
3721         tlb->prot |= PAGE_VALID;
3722     else
3723         tlb->prot &= ~PAGE_VALID;
3724     if (val & 0x20) {
3725         /* XXX: TO BE FIXED */
3726         cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
3727     }
3728     tlb->PID = env->spr[SPR_40x_PID]; /* PID */
3729     tlb->attr = val & 0xFF;
3730 #if defined (DEBUG_SOFTWARE_TLB)
3731     if (loglevel != 0) {
3732         fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
3733                 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
3734                 (int)entry, tlb->RPN, tlb->EPN, tlb->size,
3735                 tlb->prot & PAGE_READ ? 'r' : '-',
3736                 tlb->prot & PAGE_WRITE ? 'w' : '-',
3737                 tlb->prot & PAGE_EXEC ? 'x' : '-',
3738                 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
3739     }
3740 #endif
3741     /* Invalidate new TLB (if valid) */
3742     if (tlb->prot & PAGE_VALID) {
3743         end = tlb->EPN + tlb->size;
3744 #if defined (DEBUG_SOFTWARE_TLB)
3745         if (loglevel != 0) {
3746             fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
3747                     " end " ADDRX "\n", __func__, (int)entry, tlb->EPN, end);
3748         }
3749 #endif
3750         for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
3751             tlb_flush_page(env, page);
3752     }
3753 }
3754
3755 void helper_4xx_tlbwe_lo (target_ulong entry, target_ulong val)
3756 {
3757     ppcemb_tlb_t *tlb;
3758
3759 #if defined (DEBUG_SOFTWARE_TLB)
3760     if (loglevel != 0) {
3761         fprintf(logfile, "%s entry %i val " ADDRX "\n", __func__, (int)entry, val);
3762     }
3763 #endif
3764     entry &= 0x3F;
3765     tlb = &env->tlb[entry].tlbe;
3766     tlb->RPN = val & 0xFFFFFC00;
3767     tlb->prot = PAGE_READ;
3768     if (val & 0x200)
3769         tlb->prot |= PAGE_EXEC;
3770     if (val & 0x100)
3771         tlb->prot |= PAGE_WRITE;
3772 #if defined (DEBUG_SOFTWARE_TLB)
3773     if (loglevel != 0) {
3774         fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
3775                 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
3776                 (int)entry, tlb->RPN, tlb->EPN, tlb->size,
3777                 tlb->prot & PAGE_READ ? 'r' : '-',
3778                 tlb->prot & PAGE_WRITE ? 'w' : '-',
3779                 tlb->prot & PAGE_EXEC ? 'x' : '-',
3780                 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
3781     }
3782 #endif
3783 }
3784
3785 target_ulong helper_4xx_tlbsx (target_ulong address)
3786 {
3787     return ppcemb_tlb_search(env, address, env->spr[SPR_40x_PID]);
3788 }
3789
3790 /* PowerPC 440 TLB management */
3791 void helper_440_tlbwe (uint32_t word, target_ulong entry, target_ulong value)
3792 {
3793     ppcemb_tlb_t *tlb;
3794     target_ulong EPN, RPN, size;
3795     int do_flush_tlbs;
3796
3797 #if defined (DEBUG_SOFTWARE_TLB)
3798     if (loglevel != 0) {
3799         fprintf(logfile, "%s word %d entry %d value " ADDRX "\n",
3800                 __func__, word, (int)entry, value);
3801     }
3802 #endif
3803     do_flush_tlbs = 0;
3804     entry &= 0x3F;
3805     tlb = &env->tlb[entry].tlbe;
3806     switch (word) {
3807     default:
3808         /* Just here to please gcc */
3809     case 0:
3810         EPN = value & 0xFFFFFC00;
3811         if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
3812             do_flush_tlbs = 1;
3813         tlb->EPN = EPN;
3814         size = booke_tlb_to_page_size((value >> 4) & 0xF);
3815         if ((tlb->prot & PAGE_VALID) && tlb->size < size)
3816             do_flush_tlbs = 1;
3817         tlb->size = size;
3818         tlb->attr &= ~0x1;
3819         tlb->attr |= (value >> 8) & 1;
3820         if (value & 0x200) {
3821             tlb->prot |= PAGE_VALID;
3822         } else {
3823             if (tlb->prot & PAGE_VALID) {
3824                 tlb->prot &= ~PAGE_VALID;
3825                 do_flush_tlbs = 1;
3826             }
3827         }
3828         tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
3829         if (do_flush_tlbs)
3830             tlb_flush(env, 1);
3831         break;
3832     case 1:
3833         RPN = value & 0xFFFFFC0F;
3834         if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
3835             tlb_flush(env, 1);
3836         tlb->RPN = RPN;
3837         break;
3838     case 2:
3839         tlb->attr = (tlb->attr & 0x1) | (value & 0x0000FF00);
3840         tlb->prot = tlb->prot & PAGE_VALID;
3841         if (value & 0x1)
3842             tlb->prot |= PAGE_READ << 4;
3843         if (value & 0x2)
3844             tlb->prot |= PAGE_WRITE << 4;
3845         if (value & 0x4)
3846             tlb->prot |= PAGE_EXEC << 4;
3847         if (value & 0x8)
3848             tlb->prot |= PAGE_READ;
3849         if (value & 0x10)
3850             tlb->prot |= PAGE_WRITE;
3851         if (value & 0x20)
3852             tlb->prot |= PAGE_EXEC;
3853         break;
3854     }
3855 }
3856
3857 target_ulong helper_440_tlbre (uint32_t word, target_ulong entry)
3858 {
3859     ppcemb_tlb_t *tlb;
3860     target_ulong ret;
3861     int size;
3862
3863     entry &= 0x3F;
3864     tlb = &env->tlb[entry].tlbe;
3865     switch (word) {
3866     default:
3867         /* Just here to please gcc */
3868     case 0:
3869         ret = tlb->EPN;
3870         size = booke_page_size_to_tlb(tlb->size);
3871         if (size < 0 || size > 0xF)
3872             size = 1;
3873         ret |= size << 4;
3874         if (tlb->attr & 0x1)
3875             ret |= 0x100;
3876         if (tlb->prot & PAGE_VALID)
3877             ret |= 0x200;
3878         env->spr[SPR_440_MMUCR] &= ~0x000000FF;
3879         env->spr[SPR_440_MMUCR] |= tlb->PID;
3880         break;
3881     case 1:
3882         ret = tlb->RPN;
3883         break;
3884     case 2:
3885         ret = tlb->attr & ~0x1;
3886         if (tlb->prot & (PAGE_READ << 4))
3887             ret |= 0x1;
3888         if (tlb->prot & (PAGE_WRITE << 4))
3889             ret |= 0x2;
3890         if (tlb->prot & (PAGE_EXEC << 4))
3891             ret |= 0x4;
3892         if (tlb->prot & PAGE_READ)
3893             ret |= 0x8;
3894         if (tlb->prot & PAGE_WRITE)
3895             ret |= 0x10;
3896         if (tlb->prot & PAGE_EXEC)
3897             ret |= 0x20;
3898         break;
3899     }
3900     return ret;
3901 }
3902
3903 target_ulong helper_440_tlbsx (target_ulong address)
3904 {
3905     return ppcemb_tlb_search(env, address, env->spr[SPR_440_MMUCR] & 0xFF);
3906 }
3907
3908 #endif /* !CONFIG_USER_ONLY */