target-ppc: fadd/fsub: correctly propagate NaN
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "exec.h"
21 #include "host-utils.h"
22 #include "helper.h"
23
24 #include "helper_regs.h"
25
26 //#define DEBUG_OP
27 //#define DEBUG_EXCEPTIONS
28 //#define DEBUG_SOFTWARE_TLB
29
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
32
33 void helper_raise_exception_err (uint32_t exception, uint32_t error_code)
34 {
35 #if 0
36     printf("Raise exception %3x code : %d\n", exception, error_code);
37 #endif
38     env->exception_index = exception;
39     env->error_code = error_code;
40     cpu_loop_exit();
41 }
42
43 void helper_raise_exception (uint32_t exception)
44 {
45     helper_raise_exception_err(exception, 0);
46 }
47
48 /*****************************************************************************/
49 /* Registers load and stores */
50 target_ulong helper_load_cr (void)
51 {
52     return (env->crf[0] << 28) |
53            (env->crf[1] << 24) |
54            (env->crf[2] << 20) |
55            (env->crf[3] << 16) |
56            (env->crf[4] << 12) |
57            (env->crf[5] << 8) |
58            (env->crf[6] << 4) |
59            (env->crf[7] << 0);
60 }
61
62 void helper_store_cr (target_ulong val, uint32_t mask)
63 {
64     int i, sh;
65
66     for (i = 0, sh = 7; i < 8; i++, sh--) {
67         if (mask & (1 << sh))
68             env->crf[i] = (val >> (sh * 4)) & 0xFUL;
69     }
70 }
71
72 /*****************************************************************************/
73 /* SPR accesses */
74 void helper_load_dump_spr (uint32_t sprn)
75 {
76     if (loglevel != 0) {
77         fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
78                 sprn, sprn, env->spr[sprn]);
79     }
80 }
81
82 void helper_store_dump_spr (uint32_t sprn)
83 {
84     if (loglevel != 0) {
85         fprintf(logfile, "Write SPR %d %03x <= " ADDRX "\n",
86                 sprn, sprn, env->spr[sprn]);
87     }
88 }
89
90 target_ulong helper_load_tbl (void)
91 {
92     return cpu_ppc_load_tbl(env);
93 }
94
95 target_ulong helper_load_tbu (void)
96 {
97     return cpu_ppc_load_tbu(env);
98 }
99
100 target_ulong helper_load_atbl (void)
101 {
102     return cpu_ppc_load_atbl(env);
103 }
104
105 target_ulong helper_load_atbu (void)
106 {
107     return cpu_ppc_load_atbu(env);
108 }
109
110 target_ulong helper_load_601_rtcl (void)
111 {
112     return cpu_ppc601_load_rtcl(env);
113 }
114
115 target_ulong helper_load_601_rtcu (void)
116 {
117     return cpu_ppc601_load_rtcu(env);
118 }
119
120 #if !defined(CONFIG_USER_ONLY)
121 #if defined (TARGET_PPC64)
122 void helper_store_asr (target_ulong val)
123 {
124     ppc_store_asr(env, val);
125 }
126 #endif
127
128 void helper_store_sdr1 (target_ulong val)
129 {
130     ppc_store_sdr1(env, val);
131 }
132
133 void helper_store_tbl (target_ulong val)
134 {
135     cpu_ppc_store_tbl(env, val);
136 }
137
138 void helper_store_tbu (target_ulong val)
139 {
140     cpu_ppc_store_tbu(env, val);
141 }
142
143 void helper_store_atbl (target_ulong val)
144 {
145     cpu_ppc_store_atbl(env, val);
146 }
147
148 void helper_store_atbu (target_ulong val)
149 {
150     cpu_ppc_store_atbu(env, val);
151 }
152
153 void helper_store_601_rtcl (target_ulong val)
154 {
155     cpu_ppc601_store_rtcl(env, val);
156 }
157
158 void helper_store_601_rtcu (target_ulong val)
159 {
160     cpu_ppc601_store_rtcu(env, val);
161 }
162
163 target_ulong helper_load_decr (void)
164 {
165     return cpu_ppc_load_decr(env);
166 }
167
168 void helper_store_decr (target_ulong val)
169 {
170     cpu_ppc_store_decr(env, val);
171 }
172
173 void helper_store_hid0_601 (target_ulong val)
174 {
175     target_ulong hid0;
176
177     hid0 = env->spr[SPR_HID0];
178     if ((val ^ hid0) & 0x00000008) {
179         /* Change current endianness */
180         env->hflags &= ~(1 << MSR_LE);
181         env->hflags_nmsr &= ~(1 << MSR_LE);
182         env->hflags_nmsr |= (1 << MSR_LE) & (((val >> 3) & 1) << MSR_LE);
183         env->hflags |= env->hflags_nmsr;
184         if (loglevel != 0) {
185             fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
186                     __func__, val & 0x8 ? 'l' : 'b', env->hflags);
187         }
188     }
189     env->spr[SPR_HID0] = (uint32_t)val;
190 }
191
192 void helper_store_403_pbr (uint32_t num, target_ulong value)
193 {
194     if (likely(env->pb[num] != value)) {
195         env->pb[num] = value;
196         /* Should be optimized */
197         tlb_flush(env, 1);
198     }
199 }
200
201 target_ulong helper_load_40x_pit (void)
202 {
203     return load_40x_pit(env);
204 }
205
206 void helper_store_40x_pit (target_ulong val)
207 {
208     store_40x_pit(env, val);
209 }
210
211 void helper_store_40x_dbcr0 (target_ulong val)
212 {
213     store_40x_dbcr0(env, val);
214 }
215
216 void helper_store_40x_sler (target_ulong val)
217 {
218     store_40x_sler(env, val);
219 }
220
221 void helper_store_booke_tcr (target_ulong val)
222 {
223     store_booke_tcr(env, val);
224 }
225
226 void helper_store_booke_tsr (target_ulong val)
227 {
228     store_booke_tsr(env, val);
229 }
230
231 void helper_store_ibatu (uint32_t nr, target_ulong val)
232 {
233     ppc_store_ibatu(env, nr, val);
234 }
235
236 void helper_store_ibatl (uint32_t nr, target_ulong val)
237 {
238     ppc_store_ibatl(env, nr, val);
239 }
240
241 void helper_store_dbatu (uint32_t nr, target_ulong val)
242 {
243     ppc_store_dbatu(env, nr, val);
244 }
245
246 void helper_store_dbatl (uint32_t nr, target_ulong val)
247 {
248     ppc_store_dbatl(env, nr, val);
249 }
250
251 void helper_store_601_batl (uint32_t nr, target_ulong val)
252 {
253     ppc_store_ibatl_601(env, nr, val);
254 }
255
256 void helper_store_601_batu (uint32_t nr, target_ulong val)
257 {
258     ppc_store_ibatu_601(env, nr, val);
259 }
260 #endif
261
262 /*****************************************************************************/
263 /* Memory load and stores */
264
265 static always_inline target_ulong addr_add(target_ulong addr, target_long arg)
266 {
267 #if defined(TARGET_PPC64)
268         if (!msr_sf)
269             return (uint32_t)(addr + arg);
270         else
271 #endif
272             return addr + arg;
273 }
274
275 void helper_lmw (target_ulong addr, uint32_t reg)
276 {
277     for (; reg < 32; reg++) {
278         if (msr_le)
279             env->gpr[reg] = bswap32(ldl(addr));
280         else
281             env->gpr[reg] = ldl(addr);
282         addr = addr_add(addr, 4);
283     }
284 }
285
286 void helper_stmw (target_ulong addr, uint32_t reg)
287 {
288     for (; reg < 32; reg++) {
289         if (msr_le)
290             stl(addr, bswap32((uint32_t)env->gpr[reg]));
291         else
292             stl(addr, (uint32_t)env->gpr[reg]);
293         addr = addr_add(addr, 4);
294     }
295 }
296
297 void helper_lsw(target_ulong addr, uint32_t nb, uint32_t reg)
298 {
299     int sh;
300     for (; nb > 3; nb -= 4) {
301         env->gpr[reg] = ldl(addr);
302         reg = (reg + 1) % 32;
303         addr = addr_add(addr, 4);
304     }
305     if (unlikely(nb > 0)) {
306         env->gpr[reg] = 0;
307         for (sh = 24; nb > 0; nb--, sh -= 8) {
308             env->gpr[reg] |= ldub(addr) << sh;
309             addr = addr_add(addr, 1);
310         }
311     }
312 }
313 /* PPC32 specification says we must generate an exception if
314  * rA is in the range of registers to be loaded.
315  * In an other hand, IBM says this is valid, but rA won't be loaded.
316  * For now, I'll follow the spec...
317  */
318 void helper_lswx(target_ulong addr, uint32_t reg, uint32_t ra, uint32_t rb)
319 {
320     if (likely(xer_bc != 0)) {
321         if (unlikely((ra != 0 && reg < ra && (reg + xer_bc) > ra) ||
322                      (reg < rb && (reg + xer_bc) > rb))) {
323             helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
324                                        POWERPC_EXCP_INVAL |
325                                        POWERPC_EXCP_INVAL_LSWX);
326         } else {
327             helper_lsw(addr, xer_bc, reg);
328         }
329     }
330 }
331
332 void helper_stsw(target_ulong addr, uint32_t nb, uint32_t reg)
333 {
334     int sh;
335     for (; nb > 3; nb -= 4) {
336         stl(addr, env->gpr[reg]);
337         reg = (reg + 1) % 32;
338         addr = addr_add(addr, 4);
339     }
340     if (unlikely(nb > 0)) {
341         for (sh = 24; nb > 0; nb--, sh -= 8)
342             stb(addr, (env->gpr[reg] >> sh) & 0xFF);
343             addr = addr_add(addr, 1);
344     }
345 }
346
347 static void do_dcbz(target_ulong addr, int dcache_line_size)
348 {
349     addr &= ~(dcache_line_size - 1);
350     int i;
351     for (i = 0 ; i < dcache_line_size ; i += 4) {
352         stl(addr + i , 0);
353     }
354     if (env->reserve == addr)
355         env->reserve = (target_ulong)-1ULL;
356 }
357
358 void helper_dcbz(target_ulong addr)
359 {
360     do_dcbz(addr, env->dcache_line_size);
361 }
362
363 void helper_dcbz_970(target_ulong addr)
364 {
365     if (((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1)
366         do_dcbz(addr, 32);
367     else
368         do_dcbz(addr, env->dcache_line_size);
369 }
370
371 void helper_icbi(target_ulong addr)
372 {
373     uint32_t tmp;
374
375     addr &= ~(env->dcache_line_size - 1);
376     /* Invalidate one cache line :
377      * PowerPC specification says this is to be treated like a load
378      * (not a fetch) by the MMU. To be sure it will be so,
379      * do the load "by hand".
380      */
381     tmp = ldl(addr);
382     tb_invalidate_page_range(addr, addr + env->icache_line_size);
383 }
384
385 // XXX: to be tested
386 target_ulong helper_lscbx (target_ulong addr, uint32_t reg, uint32_t ra, uint32_t rb)
387 {
388     int i, c, d;
389     d = 24;
390     for (i = 0; i < xer_bc; i++) {
391         c = ldub(addr);
392         addr = addr_add(addr, 1);
393         /* ra (if not 0) and rb are never modified */
394         if (likely(reg != rb && (ra == 0 || reg != ra))) {
395             env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
396         }
397         if (unlikely(c == xer_cmp))
398             break;
399         if (likely(d != 0)) {
400             d -= 8;
401         } else {
402             d = 24;
403             reg++;
404             reg = reg & 0x1F;
405         }
406     }
407     return i;
408 }
409
410 /*****************************************************************************/
411 /* Fixed point operations helpers */
412 #if defined(TARGET_PPC64)
413
414 /* multiply high word */
415 uint64_t helper_mulhd (uint64_t arg1, uint64_t arg2)
416 {
417     uint64_t tl, th;
418
419     muls64(&tl, &th, arg1, arg2);
420     return th;
421 }
422
423 /* multiply high word unsigned */
424 uint64_t helper_mulhdu (uint64_t arg1, uint64_t arg2)
425 {
426     uint64_t tl, th;
427
428     mulu64(&tl, &th, arg1, arg2);
429     return th;
430 }
431
432 uint64_t helper_mulldo (uint64_t arg1, uint64_t arg2)
433 {
434     int64_t th;
435     uint64_t tl;
436
437     muls64(&tl, (uint64_t *)&th, arg1, arg2);
438     /* If th != 0 && th != -1, then we had an overflow */
439     if (likely((uint64_t)(th + 1) <= 1)) {
440         env->xer &= ~(1 << XER_OV);
441     } else {
442         env->xer |= (1 << XER_OV) | (1 << XER_SO);
443     }
444     return (int64_t)tl;
445 }
446 #endif
447
448 target_ulong helper_cntlzw (target_ulong t)
449 {
450     return clz32(t);
451 }
452
453 #if defined(TARGET_PPC64)
454 target_ulong helper_cntlzd (target_ulong t)
455 {
456     return clz64(t);
457 }
458 #endif
459
460 /* shift right arithmetic helper */
461 target_ulong helper_sraw (target_ulong value, target_ulong shift)
462 {
463     int32_t ret;
464
465     if (likely(!(shift & 0x20))) {
466         if (likely((uint32_t)shift != 0)) {
467             shift &= 0x1f;
468             ret = (int32_t)value >> shift;
469             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
470                 env->xer &= ~(1 << XER_CA);
471             } else {
472                 env->xer |= (1 << XER_CA);
473             }
474         } else {
475             ret = (int32_t)value;
476             env->xer &= ~(1 << XER_CA);
477         }
478     } else {
479         ret = (int32_t)value >> 31;
480         if (ret) {
481             env->xer |= (1 << XER_CA);
482         } else {
483             env->xer &= ~(1 << XER_CA);
484         }
485     }
486     return (target_long)ret;
487 }
488
489 #if defined(TARGET_PPC64)
490 target_ulong helper_srad (target_ulong value, target_ulong shift)
491 {
492     int64_t ret;
493
494     if (likely(!(shift & 0x40))) {
495         if (likely((uint64_t)shift != 0)) {
496             shift &= 0x3f;
497             ret = (int64_t)value >> shift;
498             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
499                 env->xer &= ~(1 << XER_CA);
500             } else {
501                 env->xer |= (1 << XER_CA);
502             }
503         } else {
504             ret = (int64_t)value;
505             env->xer &= ~(1 << XER_CA);
506         }
507     } else {
508         ret = (int64_t)value >> 63;
509         if (ret) {
510             env->xer |= (1 << XER_CA);
511         } else {
512             env->xer &= ~(1 << XER_CA);
513         }
514     }
515     return ret;
516 }
517 #endif
518
519 target_ulong helper_popcntb (target_ulong val)
520 {
521     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
522     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
523     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
524     return val;
525 }
526
527 #if defined(TARGET_PPC64)
528 target_ulong helper_popcntb_64 (target_ulong val)
529 {
530     val = (val & 0x5555555555555555ULL) + ((val >>  1) & 0x5555555555555555ULL);
531     val = (val & 0x3333333333333333ULL) + ((val >>  2) & 0x3333333333333333ULL);
532     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) & 0x0f0f0f0f0f0f0f0fULL);
533     return val;
534 }
535 #endif
536
537 /*****************************************************************************/
538 /* Floating point operations helpers */
539 uint64_t helper_float32_to_float64(uint32_t arg)
540 {
541     CPU_FloatU f;
542     CPU_DoubleU d;
543     f.l = arg;
544     d.d = float32_to_float64(f.f, &env->fp_status);
545     return d.ll;
546 }
547
548 uint32_t helper_float64_to_float32(uint64_t arg)
549 {
550     CPU_FloatU f;
551     CPU_DoubleU d;
552     d.ll = arg;
553     f.f = float64_to_float32(d.d, &env->fp_status);
554     return f.l;
555 }
556
557 static always_inline int isden (float64 d)
558 {
559     CPU_DoubleU u;
560
561     u.d = d;
562
563     return ((u.ll >> 52) & 0x7FF) == 0;
564 }
565
566 #ifdef CONFIG_SOFTFLOAT
567 static always_inline int isnormal (float64 d)
568 {
569     CPU_DoubleU u;
570
571     u.d = d;
572
573     uint32_t exp = (u.ll >> 52) & 0x7FF;
574     return ((0 < exp) && (exp < 0x7FF));
575 }
576 #endif
577
578 uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
579 {
580     CPU_DoubleU farg;
581     int isneg;
582     int ret;
583     farg.ll = arg;
584     isneg = float64_is_neg(farg.d);
585     if (unlikely(float64_is_nan(farg.d))) {
586         if (float64_is_signaling_nan(farg.d)) {
587             /* Signaling NaN: flags are undefined */
588             ret = 0x00;
589         } else {
590             /* Quiet NaN */
591             ret = 0x11;
592         }
593     } else if (unlikely(float64_is_infinity(farg.d))) {
594         /* +/- infinity */
595         if (isneg)
596             ret = 0x09;
597         else
598             ret = 0x05;
599     } else {
600         if (float64_is_zero(farg.d)) {
601             /* +/- zero */
602             if (isneg)
603                 ret = 0x12;
604             else
605                 ret = 0x02;
606         } else {
607             if (isden(farg.d)) {
608                 /* Denormalized numbers */
609                 ret = 0x10;
610             } else {
611                 /* Normalized numbers */
612                 ret = 0x00;
613             }
614             if (isneg) {
615                 ret |= 0x08;
616             } else {
617                 ret |= 0x04;
618             }
619         }
620     }
621     if (set_fprf) {
622         /* We update FPSCR_FPRF */
623         env->fpscr &= ~(0x1F << FPSCR_FPRF);
624         env->fpscr |= ret << FPSCR_FPRF;
625     }
626     /* We just need fpcc to update Rc1 */
627     return ret & 0xF;
628 }
629
630 /* Floating-point invalid operations exception */
631 static always_inline uint64_t fload_invalid_op_excp (int op)
632 {
633     uint64_t ret = 0;
634     int ve;
635
636     ve = fpscr_ve;
637     switch (op) {
638     case POWERPC_EXCP_FP_VXSNAN:
639         env->fpscr |= 1 << FPSCR_VXSNAN;
640         break;
641     case POWERPC_EXCP_FP_VXSOFT:
642         env->fpscr |= 1 << FPSCR_VXSOFT;
643         break;
644     case POWERPC_EXCP_FP_VXISI:
645         /* Magnitude subtraction of infinities */
646         env->fpscr |= 1 << FPSCR_VXISI;
647         goto update_arith;
648     case POWERPC_EXCP_FP_VXIDI:
649         /* Division of infinity by infinity */
650         env->fpscr |= 1 << FPSCR_VXIDI;
651         goto update_arith;
652     case POWERPC_EXCP_FP_VXZDZ:
653         /* Division of zero by zero */
654         env->fpscr |= 1 << FPSCR_VXZDZ;
655         goto update_arith;
656     case POWERPC_EXCP_FP_VXIMZ:
657         /* Multiplication of zero by infinity */
658         env->fpscr |= 1 << FPSCR_VXIMZ;
659         goto update_arith;
660     case POWERPC_EXCP_FP_VXVC:
661         /* Ordered comparison of NaN */
662         env->fpscr |= 1 << FPSCR_VXVC;
663         env->fpscr &= ~(0xF << FPSCR_FPCC);
664         env->fpscr |= 0x11 << FPSCR_FPCC;
665         /* We must update the target FPR before raising the exception */
666         if (ve != 0) {
667             env->exception_index = POWERPC_EXCP_PROGRAM;
668             env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
669             /* Update the floating-point enabled exception summary */
670             env->fpscr |= 1 << FPSCR_FEX;
671             /* Exception is differed */
672             ve = 0;
673         }
674         break;
675     case POWERPC_EXCP_FP_VXSQRT:
676         /* Square root of a negative number */
677         env->fpscr |= 1 << FPSCR_VXSQRT;
678     update_arith:
679         env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
680         if (ve == 0) {
681             /* Set the result to quiet NaN */
682             ret = 0xFFF8000000000000ULL;
683             env->fpscr &= ~(0xF << FPSCR_FPCC);
684             env->fpscr |= 0x11 << FPSCR_FPCC;
685         }
686         break;
687     case POWERPC_EXCP_FP_VXCVI:
688         /* Invalid conversion */
689         env->fpscr |= 1 << FPSCR_VXCVI;
690         env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
691         if (ve == 0) {
692             /* Set the result to quiet NaN */
693             ret = 0xFFF8000000000000ULL;
694             env->fpscr &= ~(0xF << FPSCR_FPCC);
695             env->fpscr |= 0x11 << FPSCR_FPCC;
696         }
697         break;
698     }
699     /* Update the floating-point invalid operation summary */
700     env->fpscr |= 1 << FPSCR_VX;
701     /* Update the floating-point exception summary */
702     env->fpscr |= 1 << FPSCR_FX;
703     if (ve != 0) {
704         /* Update the floating-point enabled exception summary */
705         env->fpscr |= 1 << FPSCR_FEX;
706         if (msr_fe0 != 0 || msr_fe1 != 0)
707             helper_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
708     }
709     return ret;
710 }
711
712 static always_inline uint64_t float_zero_divide_excp (uint64_t arg1, uint64_t arg2)
713 {
714     env->fpscr |= 1 << FPSCR_ZX;
715     env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
716     /* Update the floating-point exception summary */
717     env->fpscr |= 1 << FPSCR_FX;
718     if (fpscr_ze != 0) {
719         /* Update the floating-point enabled exception summary */
720         env->fpscr |= 1 << FPSCR_FEX;
721         if (msr_fe0 != 0 || msr_fe1 != 0) {
722             helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
723                                        POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
724         }
725     } else {
726         /* Set the result to infinity */
727         arg1 = ((arg1 ^ arg2) & 0x8000000000000000ULL);
728         arg1 |= 0x7FFULL << 52;
729     }
730     return arg1;
731 }
732
733 static always_inline void float_overflow_excp (void)
734 {
735     env->fpscr |= 1 << FPSCR_OX;
736     /* Update the floating-point exception summary */
737     env->fpscr |= 1 << FPSCR_FX;
738     if (fpscr_oe != 0) {
739         /* XXX: should adjust the result */
740         /* Update the floating-point enabled exception summary */
741         env->fpscr |= 1 << FPSCR_FEX;
742         /* We must update the target FPR before raising the exception */
743         env->exception_index = POWERPC_EXCP_PROGRAM;
744         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
745     } else {
746         env->fpscr |= 1 << FPSCR_XX;
747         env->fpscr |= 1 << FPSCR_FI;
748     }
749 }
750
751 static always_inline void float_underflow_excp (void)
752 {
753     env->fpscr |= 1 << FPSCR_UX;
754     /* Update the floating-point exception summary */
755     env->fpscr |= 1 << FPSCR_FX;
756     if (fpscr_ue != 0) {
757         /* XXX: should adjust the result */
758         /* Update the floating-point enabled exception summary */
759         env->fpscr |= 1 << FPSCR_FEX;
760         /* We must update the target FPR before raising the exception */
761         env->exception_index = POWERPC_EXCP_PROGRAM;
762         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
763     }
764 }
765
766 static always_inline void float_inexact_excp (void)
767 {
768     env->fpscr |= 1 << FPSCR_XX;
769     /* Update the floating-point exception summary */
770     env->fpscr |= 1 << FPSCR_FX;
771     if (fpscr_xe != 0) {
772         /* Update the floating-point enabled exception summary */
773         env->fpscr |= 1 << FPSCR_FEX;
774         /* We must update the target FPR before raising the exception */
775         env->exception_index = POWERPC_EXCP_PROGRAM;
776         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
777     }
778 }
779
780 static always_inline void fpscr_set_rounding_mode (void)
781 {
782     int rnd_type;
783
784     /* Set rounding mode */
785     switch (fpscr_rn) {
786     case 0:
787         /* Best approximation (round to nearest) */
788         rnd_type = float_round_nearest_even;
789         break;
790     case 1:
791         /* Smaller magnitude (round toward zero) */
792         rnd_type = float_round_to_zero;
793         break;
794     case 2:
795         /* Round toward +infinite */
796         rnd_type = float_round_up;
797         break;
798     default:
799     case 3:
800         /* Round toward -infinite */
801         rnd_type = float_round_down;
802         break;
803     }
804     set_float_rounding_mode(rnd_type, &env->fp_status);
805 }
806
807 void helper_fpscr_clrbit (uint32_t bit)
808 {
809     int prev;
810
811     prev = (env->fpscr >> bit) & 1;
812     env->fpscr &= ~(1 << bit);
813     if (prev == 1) {
814         switch (bit) {
815         case FPSCR_RN1:
816         case FPSCR_RN:
817             fpscr_set_rounding_mode();
818             break;
819         default:
820             break;
821         }
822     }
823 }
824
825 void helper_fpscr_setbit (uint32_t bit)
826 {
827     int prev;
828
829     prev = (env->fpscr >> bit) & 1;
830     env->fpscr |= 1 << bit;
831     if (prev == 0) {
832         switch (bit) {
833         case FPSCR_VX:
834             env->fpscr |= 1 << FPSCR_FX;
835             if (fpscr_ve)
836                 goto raise_ve;
837         case FPSCR_OX:
838             env->fpscr |= 1 << FPSCR_FX;
839             if (fpscr_oe)
840                 goto raise_oe;
841             break;
842         case FPSCR_UX:
843             env->fpscr |= 1 << FPSCR_FX;
844             if (fpscr_ue)
845                 goto raise_ue;
846             break;
847         case FPSCR_ZX:
848             env->fpscr |= 1 << FPSCR_FX;
849             if (fpscr_ze)
850                 goto raise_ze;
851             break;
852         case FPSCR_XX:
853             env->fpscr |= 1 << FPSCR_FX;
854             if (fpscr_xe)
855                 goto raise_xe;
856             break;
857         case FPSCR_VXSNAN:
858         case FPSCR_VXISI:
859         case FPSCR_VXIDI:
860         case FPSCR_VXZDZ:
861         case FPSCR_VXIMZ:
862         case FPSCR_VXVC:
863         case FPSCR_VXSOFT:
864         case FPSCR_VXSQRT:
865         case FPSCR_VXCVI:
866             env->fpscr |= 1 << FPSCR_VX;
867             env->fpscr |= 1 << FPSCR_FX;
868             if (fpscr_ve != 0)
869                 goto raise_ve;
870             break;
871         case FPSCR_VE:
872             if (fpscr_vx != 0) {
873             raise_ve:
874                 env->error_code = POWERPC_EXCP_FP;
875                 if (fpscr_vxsnan)
876                     env->error_code |= POWERPC_EXCP_FP_VXSNAN;
877                 if (fpscr_vxisi)
878                     env->error_code |= POWERPC_EXCP_FP_VXISI;
879                 if (fpscr_vxidi)
880                     env->error_code |= POWERPC_EXCP_FP_VXIDI;
881                 if (fpscr_vxzdz)
882                     env->error_code |= POWERPC_EXCP_FP_VXZDZ;
883                 if (fpscr_vximz)
884                     env->error_code |= POWERPC_EXCP_FP_VXIMZ;
885                 if (fpscr_vxvc)
886                     env->error_code |= POWERPC_EXCP_FP_VXVC;
887                 if (fpscr_vxsoft)
888                     env->error_code |= POWERPC_EXCP_FP_VXSOFT;
889                 if (fpscr_vxsqrt)
890                     env->error_code |= POWERPC_EXCP_FP_VXSQRT;
891                 if (fpscr_vxcvi)
892                     env->error_code |= POWERPC_EXCP_FP_VXCVI;
893                 goto raise_excp;
894             }
895             break;
896         case FPSCR_OE:
897             if (fpscr_ox != 0) {
898             raise_oe:
899                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
900                 goto raise_excp;
901             }
902             break;
903         case FPSCR_UE:
904             if (fpscr_ux != 0) {
905             raise_ue:
906                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
907                 goto raise_excp;
908             }
909             break;
910         case FPSCR_ZE:
911             if (fpscr_zx != 0) {
912             raise_ze:
913                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
914                 goto raise_excp;
915             }
916             break;
917         case FPSCR_XE:
918             if (fpscr_xx != 0) {
919             raise_xe:
920                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
921                 goto raise_excp;
922             }
923             break;
924         case FPSCR_RN1:
925         case FPSCR_RN:
926             fpscr_set_rounding_mode();
927             break;
928         default:
929             break;
930         raise_excp:
931             /* Update the floating-point enabled exception summary */
932             env->fpscr |= 1 << FPSCR_FEX;
933                 /* We have to update Rc1 before raising the exception */
934             env->exception_index = POWERPC_EXCP_PROGRAM;
935             break;
936         }
937     }
938 }
939
940 void helper_store_fpscr (uint64_t arg, uint32_t mask)
941 {
942     /*
943      * We use only the 32 LSB of the incoming fpr
944      */
945     uint32_t prev, new;
946     int i;
947
948     prev = env->fpscr;
949     new = (uint32_t)arg;
950     new &= ~0x60000000;
951     new |= prev & 0x60000000;
952     for (i = 0; i < 8; i++) {
953         if (mask & (1 << i)) {
954             env->fpscr &= ~(0xF << (4 * i));
955             env->fpscr |= new & (0xF << (4 * i));
956         }
957     }
958     /* Update VX and FEX */
959     if (fpscr_ix != 0)
960         env->fpscr |= 1 << FPSCR_VX;
961     else
962         env->fpscr &= ~(1 << FPSCR_VX);
963     if ((fpscr_ex & fpscr_eex) != 0) {
964         env->fpscr |= 1 << FPSCR_FEX;
965         env->exception_index = POWERPC_EXCP_PROGRAM;
966         /* XXX: we should compute it properly */
967         env->error_code = POWERPC_EXCP_FP;
968     }
969     else
970         env->fpscr &= ~(1 << FPSCR_FEX);
971     fpscr_set_rounding_mode();
972 }
973
974 void helper_float_check_status (void)
975 {
976 #ifdef CONFIG_SOFTFLOAT
977     if (env->exception_index == POWERPC_EXCP_PROGRAM &&
978         (env->error_code & POWERPC_EXCP_FP)) {
979         /* Differred floating-point exception after target FPR update */
980         if (msr_fe0 != 0 || msr_fe1 != 0)
981             helper_raise_exception_err(env->exception_index, env->error_code);
982     } else {
983         int status = get_float_exception_flags(&env->fp_status);
984         if (status & float_flag_overflow) {
985             float_overflow_excp();
986         } else if (status & float_flag_underflow) {
987             float_underflow_excp();
988         } else if (status & float_flag_inexact) {
989             float_inexact_excp();
990         }
991     }
992 #else
993     if (env->exception_index == POWERPC_EXCP_PROGRAM &&
994         (env->error_code & POWERPC_EXCP_FP)) {
995         /* Differred floating-point exception after target FPR update */
996         if (msr_fe0 != 0 || msr_fe1 != 0)
997             helper_raise_exception_err(env->exception_index, env->error_code);
998     }
999 #endif
1000 }
1001
1002 #ifdef CONFIG_SOFTFLOAT
1003 void helper_reset_fpstatus (void)
1004 {
1005     set_float_exception_flags(0, &env->fp_status);
1006 }
1007 #endif
1008
1009 /* fadd - fadd. */
1010 uint64_t helper_fadd (uint64_t arg1, uint64_t arg2)
1011 {
1012     CPU_DoubleU farg1, farg2;
1013
1014     farg1.ll = arg1;
1015     farg2.ll = arg2;
1016 #if USE_PRECISE_EMULATION
1017     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1018                  float64_is_signaling_nan(farg2.d))) {
1019         /* sNaN addition */
1020         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1021     } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d) &&
1022                       float64_is_neg(farg1.d) != float64_is_neg(farg2.d))) {
1023         /* Magnitude subtraction of infinities */
1024         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1025     } else {
1026         farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
1027     }
1028 #else
1029     farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
1030 #endif
1031     return farg1.ll;
1032 }
1033
1034 /* fsub - fsub. */
1035 uint64_t helper_fsub (uint64_t arg1, uint64_t arg2)
1036 {
1037     CPU_DoubleU farg1, farg2;
1038
1039     farg1.ll = arg1;
1040     farg2.ll = arg2;
1041 #if USE_PRECISE_EMULATION
1042 {
1043     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1044                  float64_is_signaling_nan(farg2.d))) {
1045         /* sNaN subtraction */
1046         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1047     } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d) &&
1048                       float64_is_neg(farg1.d) == float64_is_neg(farg2.d))) {
1049         /* Magnitude subtraction of infinities */
1050         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
1051     } else {
1052         farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
1053     }
1054 }
1055 #else
1056     farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
1057 #endif
1058     return farg1.ll;
1059 }
1060
1061 /* fmul - fmul. */
1062 uint64_t helper_fmul (uint64_t arg1, uint64_t arg2)
1063 {
1064     CPU_DoubleU farg1, farg2;
1065
1066     farg1.ll = arg1;
1067     farg2.ll = arg2;
1068 #if USE_PRECISE_EMULATION
1069     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1070                  float64_is_signaling_nan(farg2.d))) {
1071         /* sNaN multiplication */
1072         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1073     } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
1074                         (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
1075         /* Multiplication of zero by infinity */
1076         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
1077     } else {
1078         farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1079     }
1080 #else
1081     farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1082 #endif
1083     return farg1.ll;
1084 }
1085
1086 /* fdiv - fdiv. */
1087 uint64_t helper_fdiv (uint64_t arg1, uint64_t arg2)
1088 {
1089     CPU_DoubleU farg1, farg2;
1090
1091     farg1.ll = arg1;
1092     farg2.ll = arg2;
1093 #if USE_PRECISE_EMULATION
1094     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1095                  float64_is_signaling_nan(farg2.d))) {
1096         /* sNaN division */
1097         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1098     } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d))) {
1099         /* Division of infinity by infinity */
1100         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
1101     } else if (unlikely(!float64_is_nan(farg1.d) && float64_is_zero(farg2.d))) {
1102         if (float64_is_zero(farg1.d)) {
1103             /* Division of zero by zero */
1104             farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
1105         } else {
1106             /* Division by zero */
1107             farg1.ll = float_zero_divide_excp(farg1.d, farg2.d);
1108         }
1109     } else {
1110         farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
1111     }
1112 #else
1113     farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
1114 #endif
1115     return farg1.ll;
1116 }
1117
1118 /* fabs */
1119 uint64_t helper_fabs (uint64_t arg)
1120 {
1121     CPU_DoubleU farg;
1122
1123     farg.ll = arg;
1124     farg.d = float64_abs(farg.d);
1125     return farg.ll;
1126 }
1127
1128 /* fnabs */
1129 uint64_t helper_fnabs (uint64_t arg)
1130 {
1131     CPU_DoubleU farg;
1132
1133     farg.ll = arg;
1134     farg.d = float64_abs(farg.d);
1135     farg.d = float64_chs(farg.d);
1136     return farg.ll;
1137 }
1138
1139 /* fneg */
1140 uint64_t helper_fneg (uint64_t arg)
1141 {
1142     CPU_DoubleU farg;
1143
1144     farg.ll = arg;
1145     farg.d = float64_chs(farg.d);
1146     return farg.ll;
1147 }
1148
1149 /* fctiw - fctiw. */
1150 uint64_t helper_fctiw (uint64_t arg)
1151 {
1152     CPU_DoubleU farg;
1153     farg.ll = arg;
1154
1155     if (unlikely(float64_is_signaling_nan(farg.d))) {
1156         /* sNaN conversion */
1157         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1158     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1159         /* qNan / infinity conversion */
1160         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1161     } else {
1162         farg.ll = float64_to_int32(farg.d, &env->fp_status);
1163 #if USE_PRECISE_EMULATION
1164         /* XXX: higher bits are not supposed to be significant.
1165          *     to make tests easier, return the same as a real PowerPC 750
1166          */
1167         farg.ll |= 0xFFF80000ULL << 32;
1168 #endif
1169     }
1170     return farg.ll;
1171 }
1172
1173 /* fctiwz - fctiwz. */
1174 uint64_t helper_fctiwz (uint64_t arg)
1175 {
1176     CPU_DoubleU farg;
1177     farg.ll = arg;
1178
1179     if (unlikely(float64_is_signaling_nan(farg.d))) {
1180         /* sNaN conversion */
1181         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1182     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1183         /* qNan / infinity conversion */
1184         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1185     } else {
1186         farg.ll = float64_to_int32_round_to_zero(farg.d, &env->fp_status);
1187 #if USE_PRECISE_EMULATION
1188         /* XXX: higher bits are not supposed to be significant.
1189          *     to make tests easier, return the same as a real PowerPC 750
1190          */
1191         farg.ll |= 0xFFF80000ULL << 32;
1192 #endif
1193     }
1194     return farg.ll;
1195 }
1196
1197 #if defined(TARGET_PPC64)
1198 /* fcfid - fcfid. */
1199 uint64_t helper_fcfid (uint64_t arg)
1200 {
1201     CPU_DoubleU farg;
1202     farg.d = int64_to_float64(arg, &env->fp_status);
1203     return farg.ll;
1204 }
1205
1206 /* fctid - fctid. */
1207 uint64_t helper_fctid (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(farg.d, &env->fp_status);
1220     }
1221     return farg.ll;
1222 }
1223
1224 /* fctidz - fctidz. */
1225 uint64_t helper_fctidz (uint64_t arg)
1226 {
1227     CPU_DoubleU farg;
1228     farg.ll = arg;
1229
1230     if (unlikely(float64_is_signaling_nan(farg.d))) {
1231         /* sNaN conversion */
1232         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1233     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1234         /* qNan / infinity conversion */
1235         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1236     } else {
1237         farg.ll = float64_to_int64_round_to_zero(farg.d, &env->fp_status);
1238     }
1239     return farg.ll;
1240 }
1241
1242 #endif
1243
1244 static always_inline uint64_t do_fri (uint64_t arg, int rounding_mode)
1245 {
1246     CPU_DoubleU farg;
1247     farg.ll = arg;
1248
1249     if (unlikely(float64_is_signaling_nan(farg.d))) {
1250         /* sNaN round */
1251         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1252     } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
1253         /* qNan / infinity round */
1254         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
1255     } else {
1256         set_float_rounding_mode(rounding_mode, &env->fp_status);
1257         farg.ll = float64_round_to_int(farg.d, &env->fp_status);
1258         /* Restore rounding mode from FPSCR */
1259         fpscr_set_rounding_mode();
1260     }
1261     return farg.ll;
1262 }
1263
1264 uint64_t helper_frin (uint64_t arg)
1265 {
1266     return do_fri(arg, float_round_nearest_even);
1267 }
1268
1269 uint64_t helper_friz (uint64_t arg)
1270 {
1271     return do_fri(arg, float_round_to_zero);
1272 }
1273
1274 uint64_t helper_frip (uint64_t arg)
1275 {
1276     return do_fri(arg, float_round_up);
1277 }
1278
1279 uint64_t helper_frim (uint64_t arg)
1280 {
1281     return do_fri(arg, float_round_down);
1282 }
1283
1284 /* fmadd - fmadd. */
1285 uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1286 {
1287     CPU_DoubleU farg1, farg2, farg3;
1288
1289     farg1.ll = arg1;
1290     farg2.ll = arg2;
1291     farg3.ll = arg3;
1292 #if USE_PRECISE_EMULATION
1293     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1294                  float64_is_signaling_nan(farg2.d) ||
1295                  float64_is_signaling_nan(farg3.d))) {
1296         /* sNaN operation */
1297         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1298     } else {
1299 #ifdef FLOAT128
1300         /* This is the way the PowerPC specification defines it */
1301         float128 ft0_128, ft1_128;
1302
1303         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1304         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1305         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1306         ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1307         ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1308         farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1309 #else
1310         /* This is OK on x86 hosts */
1311         farg1.d = (farg1.d * farg2.d) + farg3.d;
1312 #endif
1313     }
1314 #else
1315     farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1316     farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1317 #endif
1318     return farg1.ll;
1319 }
1320
1321 /* fmsub - fmsub. */
1322 uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1323 {
1324     CPU_DoubleU farg1, farg2, farg3;
1325
1326     farg1.ll = arg1;
1327     farg2.ll = arg2;
1328     farg3.ll = arg3;
1329 #if USE_PRECISE_EMULATION
1330     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1331                  float64_is_signaling_nan(farg2.d) ||
1332                  float64_is_signaling_nan(farg3.d))) {
1333         /* sNaN operation */
1334         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1335     } else {
1336 #ifdef FLOAT128
1337         /* This is the way the PowerPC specification defines it */
1338         float128 ft0_128, ft1_128;
1339
1340         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1341         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1342         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1343         ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1344         ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1345         farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1346 #else
1347         /* This is OK on x86 hosts */
1348         farg1.d = (farg1.d * farg2.d) - farg3.d;
1349 #endif
1350     }
1351 #else
1352     farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1353     farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1354 #endif
1355     return farg1.ll;
1356 }
1357
1358 /* fnmadd - fnmadd. */
1359 uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1360 {
1361     CPU_DoubleU farg1, farg2, farg3;
1362
1363     farg1.ll = arg1;
1364     farg2.ll = arg2;
1365     farg3.ll = arg3;
1366
1367     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1368                  float64_is_signaling_nan(farg2.d) ||
1369                  float64_is_signaling_nan(farg3.d))) {
1370         /* sNaN operation */
1371         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1372     } else {
1373 #if USE_PRECISE_EMULATION
1374 #ifdef FLOAT128
1375         /* This is the way the PowerPC specification defines it */
1376         float128 ft0_128, ft1_128;
1377
1378         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1379         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1380         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1381         ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1382         ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
1383         farg1.d= float128_to_float64(ft0_128, &env->fp_status);
1384 #else
1385         /* This is OK on x86 hosts */
1386         farg1.d = (farg1.d * farg2.d) + farg3.d;
1387 #endif
1388 #else
1389         farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1390         farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1391 #endif
1392         if (likely(!float64_is_nan(farg1.d)))
1393             farg1.d = float64_chs(farg1.d);
1394     }
1395     return farg1.ll;
1396 }
1397
1398 /* fnmsub - fnmsub. */
1399 uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1400 {
1401     CPU_DoubleU farg1, farg2, farg3;
1402
1403     farg1.ll = arg1;
1404     farg2.ll = arg2;
1405     farg3.ll = arg3;
1406
1407     if (unlikely(float64_is_signaling_nan(farg1.d) ||
1408                  float64_is_signaling_nan(farg2.d) ||
1409                  float64_is_signaling_nan(farg3.d))) {
1410         /* sNaN operation */
1411         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1412     } else {
1413 #if USE_PRECISE_EMULATION
1414 #ifdef FLOAT128
1415         /* This is the way the PowerPC specification defines it */
1416         float128 ft0_128, ft1_128;
1417
1418         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1419         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
1420         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
1421         ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
1422         ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
1423         farg1.d = float128_to_float64(ft0_128, &env->fp_status);
1424 #else
1425         /* This is OK on x86 hosts */
1426         farg1.d = (farg1.d * farg2.d) - farg3.d;
1427 #endif
1428 #else
1429         farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1430         farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1431 #endif
1432         if (likely(!float64_is_nan(farg1.d)))
1433             farg1.d = float64_chs(farg1.d);
1434     }
1435     return farg1.ll;
1436 }
1437
1438 /* frsp - frsp. */
1439 uint64_t helper_frsp (uint64_t arg)
1440 {
1441     CPU_DoubleU farg;
1442     float32 f32;
1443     farg.ll = arg;
1444
1445 #if USE_PRECISE_EMULATION
1446     if (unlikely(float64_is_signaling_nan(farg.d))) {
1447         /* sNaN square root */
1448        farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1449     } else {
1450        f32 = float64_to_float32(farg.d, &env->fp_status);
1451        farg.d = float32_to_float64(f32, &env->fp_status);
1452     }
1453 #else
1454     f32 = float64_to_float32(farg.d, &env->fp_status);
1455     farg.d = float32_to_float64(f32, &env->fp_status);
1456 #endif
1457     return farg.ll;
1458 }
1459
1460 /* fsqrt - fsqrt. */
1461 uint64_t helper_fsqrt (uint64_t arg)
1462 {
1463     CPU_DoubleU farg;
1464     farg.ll = arg;
1465
1466     if (unlikely(float64_is_signaling_nan(farg.d))) {
1467         /* sNaN square root */
1468         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1469     } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
1470         /* Square root of a negative nonzero number */
1471         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1472     } else {
1473         farg.d = float64_sqrt(farg.d, &env->fp_status);
1474     }
1475     return farg.ll;
1476 }
1477
1478 /* fre - fre. */
1479 uint64_t helper_fre (uint64_t arg)
1480 {
1481     CPU_DoubleU fone, farg;
1482     fone.ll = 0x3FF0000000000000ULL;
1483     farg.ll = arg;
1484
1485     if (unlikely(float64_is_signaling_nan(farg.d))) {
1486         /* sNaN reciprocal */
1487         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1488     } else if (unlikely(float64_is_zero(farg.d))) {
1489         /* Zero reciprocal */
1490         farg.ll = float_zero_divide_excp(fone.d, farg.d);
1491     } else if (likely(isnormal(farg.d))) {
1492         farg.d = float64_div(fone.d, farg.d, &env->fp_status);
1493     } else {
1494         if (farg.ll == 0x8000000000000000ULL) {
1495             farg.ll = 0xFFF0000000000000ULL;
1496         } else if (farg.ll == 0x0000000000000000ULL) {
1497             farg.ll = 0x7FF0000000000000ULL;
1498         } else if (float64_is_nan(farg.d)) {
1499             farg.ll = 0x7FF8000000000000ULL;
1500         } else if (float64_is_neg(farg.d)) {
1501             farg.ll = 0x8000000000000000ULL;
1502         } else {
1503             farg.ll = 0x0000000000000000ULL;
1504         }
1505     }
1506     return farg.d;
1507 }
1508
1509 /* fres - fres. */
1510 uint64_t helper_fres (uint64_t arg)
1511 {
1512     CPU_DoubleU fone, farg;
1513     fone.ll = 0x3FF0000000000000ULL;
1514     farg.ll = arg;
1515
1516     if (unlikely(float64_is_signaling_nan(farg.d))) {
1517         /* sNaN reciprocal */
1518         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1519     } else if (unlikely(float64_is_zero(farg.d))) {
1520         /* Zero reciprocal */
1521         farg.ll = float_zero_divide_excp(fone.d, farg.d);
1522     } else if (likely(isnormal(farg.d))) {
1523 #if USE_PRECISE_EMULATION
1524         farg.d = float64_div(fone.d, farg.d, &env->fp_status);
1525         farg.d = float64_to_float32(farg.d, &env->fp_status);
1526 #else
1527         farg.d = float32_div(fone.d, farg.d, &env->fp_status);
1528 #endif
1529     } else {
1530         if (farg.ll == 0x8000000000000000ULL) {
1531             farg.ll = 0xFFF0000000000000ULL;
1532         } else if (farg.ll == 0x0000000000000000ULL) {
1533             farg.ll = 0x7FF0000000000000ULL;
1534         } else if (float64_is_nan(farg.d)) {
1535             farg.ll = 0x7FF8000000000000ULL;
1536         } else if (float64_is_neg(farg.d)) {
1537             farg.ll = 0x8000000000000000ULL;
1538         } else {
1539             farg.ll = 0x0000000000000000ULL;
1540         }
1541     }
1542     return farg.ll;
1543 }
1544
1545 /* frsqrte  - frsqrte. */
1546 uint64_t helper_frsqrte (uint64_t arg)
1547 {
1548     CPU_DoubleU fone, farg;
1549     fone.ll = 0x3FF0000000000000ULL;
1550     farg.ll = arg;
1551
1552     if (unlikely(float64_is_signaling_nan(farg.d))) {
1553         /* sNaN reciprocal square root */
1554         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1555     } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
1556         /* Reciprocal square root of a negative nonzero number */
1557         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1558     } else if (likely(isnormal(farg.d))) {
1559         farg.d = float64_sqrt(farg.d, &env->fp_status);
1560         farg.d = float32_div(fone.d, farg.d, &env->fp_status);
1561     } else {
1562         if (farg.ll == 0x8000000000000000ULL) {
1563             farg.ll = 0xFFF0000000000000ULL;
1564         } else if (farg.ll == 0x0000000000000000ULL) {
1565             farg.ll = 0x7FF0000000000000ULL;
1566         } else if (float64_is_nan(farg.d)) {
1567             farg.ll |= 0x000FFFFFFFFFFFFFULL;
1568         } else if (float64_is_neg(farg.d)) {
1569             farg.ll = 0x7FF8000000000000ULL;
1570         } else {
1571             farg.ll = 0x0000000000000000ULL;
1572         }
1573     }
1574     return farg.ll;
1575 }
1576
1577 /* fsel - fsel. */
1578 uint64_t helper_fsel (uint64_t arg1, uint64_t arg2, uint64_t arg3)
1579 {
1580     CPU_DoubleU farg1;
1581
1582     farg1.ll = arg1;
1583
1584     if (!float64_is_neg(farg1.d) || float64_is_zero(farg1.d))
1585         return arg2;
1586     else
1587         return arg3;
1588 }
1589
1590 void helper_fcmpu (uint64_t arg1, uint64_t arg2, uint32_t crfD)
1591 {
1592     CPU_DoubleU farg1, farg2;
1593     uint32_t ret = 0;
1594     farg1.ll = arg1;
1595     farg2.ll = arg2;
1596
1597     if (unlikely(float64_is_nan(farg1.d) ||
1598                  float64_is_nan(farg2.d))) {
1599         ret = 0x01UL;
1600     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1601         ret = 0x08UL;
1602     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1603         ret = 0x04UL;
1604     } else {
1605         ret = 0x02UL;
1606     }
1607
1608     env->fpscr &= ~(0x0F << FPSCR_FPRF);
1609     env->fpscr |= ret << FPSCR_FPRF;
1610     env->crf[crfD] = ret;
1611     if (unlikely(ret == 0x01UL
1612                  && (float64_is_signaling_nan(farg1.d) ||
1613                      float64_is_signaling_nan(farg2.d)))) {
1614         /* sNaN comparison */
1615         fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1616     }
1617 }
1618
1619 void helper_fcmpo (uint64_t arg1, uint64_t arg2, uint32_t crfD)
1620 {
1621     CPU_DoubleU farg1, farg2;
1622     uint32_t ret = 0;
1623     farg1.ll = arg1;
1624     farg2.ll = arg2;
1625
1626     if (unlikely(float64_is_nan(farg1.d) ||
1627                  float64_is_nan(farg2.d))) {
1628         ret = 0x01UL;
1629     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1630         ret = 0x08UL;
1631     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1632         ret = 0x04UL;
1633     } else {
1634         ret = 0x02UL;
1635     }
1636
1637     env->fpscr &= ~(0x0F << FPSCR_FPRF);
1638     env->fpscr |= ret << FPSCR_FPRF;
1639     env->crf[crfD] = ret;
1640     if (unlikely (ret == 0x01UL)) {
1641         if (float64_is_signaling_nan(farg1.d) ||
1642             float64_is_signaling_nan(farg2.d)) {
1643             /* sNaN comparison */
1644             fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1645                                   POWERPC_EXCP_FP_VXVC);
1646         } else {
1647             /* qNaN comparison */
1648             fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1649         }
1650     }
1651 }
1652
1653 #if !defined (CONFIG_USER_ONLY)
1654 void helper_store_msr (target_ulong val)
1655 {
1656     val = hreg_store_msr(env, val, 0);
1657     if (val != 0) {
1658         env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1659         helper_raise_exception(val);
1660     }
1661 }
1662
1663 static always_inline void do_rfi (target_ulong nip, target_ulong msr,
1664                                     target_ulong msrm, int keep_msrh)
1665 {
1666 #if defined(TARGET_PPC64)
1667     if (msr & (1ULL << MSR_SF)) {
1668         nip = (uint64_t)nip;
1669         msr &= (uint64_t)msrm;
1670     } else {
1671         nip = (uint32_t)nip;
1672         msr = (uint32_t)(msr & msrm);
1673         if (keep_msrh)
1674             msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
1675     }
1676 #else
1677     nip = (uint32_t)nip;
1678     msr &= (uint32_t)msrm;
1679 #endif
1680     /* XXX: beware: this is false if VLE is supported */
1681     env->nip = nip & ~((target_ulong)0x00000003);
1682     hreg_store_msr(env, msr, 1);
1683 #if defined (DEBUG_OP)
1684     cpu_dump_rfi(env->nip, env->msr);
1685 #endif
1686     /* No need to raise an exception here,
1687      * as rfi is always the last insn of a TB
1688      */
1689     env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1690 }
1691
1692 void helper_rfi (void)
1693 {
1694     do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1695            ~((target_ulong)0xFFFF0000), 1);
1696 }
1697
1698 #if defined(TARGET_PPC64)
1699 void helper_rfid (void)
1700 {
1701     do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1702            ~((target_ulong)0xFFFF0000), 0);
1703 }
1704
1705 void helper_hrfid (void)
1706 {
1707     do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1708            ~((target_ulong)0xFFFF0000), 0);
1709 }
1710 #endif
1711 #endif
1712
1713 void helper_tw (target_ulong arg1, target_ulong arg2, uint32_t flags)
1714 {
1715     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
1716                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
1717                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
1718                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
1719                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
1720         helper_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1721     }
1722 }
1723
1724 #if defined(TARGET_PPC64)
1725 void helper_td (target_ulong arg1, target_ulong arg2, uint32_t flags)
1726 {
1727     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
1728                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
1729                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
1730                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
1731                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01)))))
1732         helper_raise_exception_err(POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
1733 }
1734 #endif
1735
1736 /*****************************************************************************/
1737 /* PowerPC 601 specific instructions (POWER bridge) */
1738
1739 target_ulong helper_clcs (uint32_t arg)
1740 {
1741     switch (arg) {
1742     case 0x0CUL:
1743         /* Instruction cache line size */
1744         return env->icache_line_size;
1745         break;
1746     case 0x0DUL:
1747         /* Data cache line size */
1748         return env->dcache_line_size;
1749         break;
1750     case 0x0EUL:
1751         /* Minimum cache line size */
1752         return (env->icache_line_size < env->dcache_line_size) ?
1753                 env->icache_line_size : env->dcache_line_size;
1754         break;
1755     case 0x0FUL:
1756         /* Maximum cache line size */
1757         return (env->icache_line_size > env->dcache_line_size) ?
1758                 env->icache_line_size : env->dcache_line_size;
1759         break;
1760     default:
1761         /* Undefined */
1762         return 0;
1763         break;
1764     }
1765 }
1766
1767 target_ulong helper_div (target_ulong arg1, target_ulong arg2)
1768 {
1769     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
1770
1771     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1772         (int32_t)arg2 == 0) {
1773         env->spr[SPR_MQ] = 0;
1774         return INT32_MIN;
1775     } else {
1776         env->spr[SPR_MQ] = tmp % arg2;
1777         return  tmp / (int32_t)arg2;
1778     }
1779 }
1780
1781 target_ulong helper_divo (target_ulong arg1, target_ulong arg2)
1782 {
1783     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
1784
1785     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1786         (int32_t)arg2 == 0) {
1787         env->xer |= (1 << XER_OV) | (1 << XER_SO);
1788         env->spr[SPR_MQ] = 0;
1789         return INT32_MIN;
1790     } else {
1791         env->spr[SPR_MQ] = tmp % arg2;
1792         tmp /= (int32_t)arg2;
1793         if ((int32_t)tmp != tmp) {
1794             env->xer |= (1 << XER_OV) | (1 << XER_SO);
1795         } else {
1796             env->xer &= ~(1 << XER_OV);
1797         }
1798         return tmp;
1799     }
1800 }
1801
1802 target_ulong helper_divs (target_ulong arg1, target_ulong arg2)
1803 {
1804     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1805         (int32_t)arg2 == 0) {
1806         env->spr[SPR_MQ] = 0;
1807         return INT32_MIN;
1808     } else {
1809         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
1810         return (int32_t)arg1 / (int32_t)arg2;
1811     }
1812 }
1813
1814 target_ulong helper_divso (target_ulong arg1, target_ulong arg2)
1815 {
1816     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
1817         (int32_t)arg2 == 0) {
1818         env->xer |= (1 << XER_OV) | (1 << XER_SO);
1819         env->spr[SPR_MQ] = 0;
1820         return INT32_MIN;
1821     } else {
1822         env->xer &= ~(1 << XER_OV);
1823         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
1824         return (int32_t)arg1 / (int32_t)arg2;
1825     }
1826 }
1827
1828 #if !defined (CONFIG_USER_ONLY)
1829 target_ulong helper_rac (target_ulong addr)
1830 {
1831     mmu_ctx_t ctx;
1832     int nb_BATs;
1833     target_ulong ret = 0;
1834
1835     /* We don't have to generate many instances of this instruction,
1836      * as rac is supervisor only.
1837      */
1838     /* XXX: FIX THIS: Pretend we have no BAT */
1839     nb_BATs = env->nb_BATs;
1840     env->nb_BATs = 0;
1841     if (get_physical_address(env, &ctx, addr, 0, ACCESS_INT) == 0)
1842         ret = ctx.raddr;
1843     env->nb_BATs = nb_BATs;
1844     return ret;
1845 }
1846
1847 void helper_rfsvc (void)
1848 {
1849     do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
1850 }
1851 #endif
1852
1853 /*****************************************************************************/
1854 /* 602 specific instructions */
1855 /* mfrom is the most crazy instruction ever seen, imho ! */
1856 /* Real implementation uses a ROM table. Do the same */
1857 /* Extremly decomposed:
1858  *                      -arg / 256
1859  * return 256 * log10(10           + 1.0) + 0.5
1860  */
1861 #if !defined (CONFIG_USER_ONLY)
1862 target_ulong helper_602_mfrom (target_ulong arg)
1863 {
1864     if (likely(arg < 602)) {
1865 #include "mfrom_table.c"
1866         return mfrom_ROM_table[arg];
1867     } else {
1868         return 0;
1869     }
1870 }
1871 #endif
1872
1873 /*****************************************************************************/
1874 /* Embedded PowerPC specific helpers */
1875
1876 /* XXX: to be improved to check access rights when in user-mode */
1877 target_ulong helper_load_dcr (target_ulong dcrn)
1878 {
1879     target_ulong val = 0;
1880
1881     if (unlikely(env->dcr_env == NULL)) {
1882         if (loglevel != 0) {
1883             fprintf(logfile, "No DCR environment\n");
1884         }
1885         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1886                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1887     } else if (unlikely(ppc_dcr_read(env->dcr_env, dcrn, &val) != 0)) {
1888         if (loglevel != 0) {
1889             fprintf(logfile, "DCR read error %d %03x\n", (int)dcrn, (int)dcrn);
1890         }
1891         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1892                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1893     }
1894     return val;
1895 }
1896
1897 void helper_store_dcr (target_ulong dcrn, target_ulong val)
1898 {
1899     if (unlikely(env->dcr_env == NULL)) {
1900         if (loglevel != 0) {
1901             fprintf(logfile, "No DCR environment\n");
1902         }
1903         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1904                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
1905     } else if (unlikely(ppc_dcr_write(env->dcr_env, dcrn, val) != 0)) {
1906         if (loglevel != 0) {
1907             fprintf(logfile, "DCR write error %d %03x\n", (int)dcrn, (int)dcrn);
1908         }
1909         helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
1910                                    POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
1911     }
1912 }
1913
1914 #if !defined(CONFIG_USER_ONLY)
1915 void helper_40x_rfci (void)
1916 {
1917     do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1918            ~((target_ulong)0xFFFF0000), 0);
1919 }
1920
1921 void helper_rfci (void)
1922 {
1923     do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1924            ~((target_ulong)0x3FFF0000), 0);
1925 }
1926
1927 void helper_rfdi (void)
1928 {
1929     do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1930            ~((target_ulong)0x3FFF0000), 0);
1931 }
1932
1933 void helper_rfmci (void)
1934 {
1935     do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1936            ~((target_ulong)0x3FFF0000), 0);
1937 }
1938 #endif
1939
1940 /* 440 specific */
1941 target_ulong helper_dlmzb (target_ulong high, target_ulong low, uint32_t update_Rc)
1942 {
1943     target_ulong mask;
1944     int i;
1945
1946     i = 1;
1947     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1948         if ((high & mask) == 0) {
1949             if (update_Rc) {
1950                 env->crf[0] = 0x4;
1951             }
1952             goto done;
1953         }
1954         i++;
1955     }
1956     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1957         if ((low & mask) == 0) {
1958             if (update_Rc) {
1959                 env->crf[0] = 0x8;
1960             }
1961             goto done;
1962         }
1963         i++;
1964     }
1965     if (update_Rc) {
1966         env->crf[0] = 0x2;
1967     }
1968  done:
1969     env->xer = (env->xer & ~0x7F) | i;
1970     if (update_Rc) {
1971         env->crf[0] |= xer_so;
1972     }
1973     return i;
1974 }
1975
1976 /*****************************************************************************/
1977 /* SPE extension helpers */
1978 /* Use a table to make this quicker */
1979 static uint8_t hbrev[16] = {
1980     0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1981     0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1982 };
1983
1984 static always_inline uint8_t byte_reverse (uint8_t val)
1985 {
1986     return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
1987 }
1988
1989 static always_inline uint32_t word_reverse (uint32_t val)
1990 {
1991     return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
1992         (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
1993 }
1994
1995 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
1996 target_ulong helper_brinc (target_ulong arg1, target_ulong arg2)
1997 {
1998     uint32_t a, b, d, mask;
1999
2000     mask = UINT32_MAX >> (32 - MASKBITS);
2001     a = arg1 & mask;
2002     b = arg2 & mask;
2003     d = word_reverse(1 + word_reverse(a | ~b));
2004     return (arg1 & ~mask) | (d & b);
2005 }
2006
2007 uint32_t helper_cntlsw32 (uint32_t val)
2008 {
2009     if (val & 0x80000000)
2010         return clz32(~val);
2011     else
2012         return clz32(val);
2013 }
2014
2015 uint32_t helper_cntlzw32 (uint32_t val)
2016 {
2017     return clz32(val);
2018 }
2019
2020 /* Single-precision floating-point conversions */
2021 static always_inline uint32_t efscfsi (uint32_t val)
2022 {
2023     CPU_FloatU u;
2024
2025     u.f = int32_to_float32(val, &env->spe_status);
2026
2027     return u.l;
2028 }
2029
2030 static always_inline uint32_t efscfui (uint32_t val)
2031 {
2032     CPU_FloatU u;
2033
2034     u.f = uint32_to_float32(val, &env->spe_status);
2035
2036     return u.l;
2037 }
2038
2039 static always_inline int32_t efsctsi (uint32_t val)
2040 {
2041     CPU_FloatU u;
2042
2043     u.l = val;
2044     /* NaN are not treated the same way IEEE 754 does */
2045     if (unlikely(float32_is_nan(u.f)))
2046         return 0;
2047
2048     return float32_to_int32(u.f, &env->spe_status);
2049 }
2050
2051 static always_inline uint32_t efsctui (uint32_t val)
2052 {
2053     CPU_FloatU u;
2054
2055     u.l = val;
2056     /* NaN are not treated the same way IEEE 754 does */
2057     if (unlikely(float32_is_nan(u.f)))
2058         return 0;
2059
2060     return float32_to_uint32(u.f, &env->spe_status);
2061 }
2062
2063 static always_inline uint32_t efsctsiz (uint32_t val)
2064 {
2065     CPU_FloatU u;
2066
2067     u.l = val;
2068     /* NaN are not treated the same way IEEE 754 does */
2069     if (unlikely(float32_is_nan(u.f)))
2070         return 0;
2071
2072     return float32_to_int32_round_to_zero(u.f, &env->spe_status);
2073 }
2074
2075 static always_inline uint32_t efsctuiz (uint32_t val)
2076 {
2077     CPU_FloatU u;
2078
2079     u.l = val;
2080     /* NaN are not treated the same way IEEE 754 does */
2081     if (unlikely(float32_is_nan(u.f)))
2082         return 0;
2083
2084     return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
2085 }
2086
2087 static always_inline uint32_t efscfsf (uint32_t val)
2088 {
2089     CPU_FloatU u;
2090     float32 tmp;
2091
2092     u.f = int32_to_float32(val, &env->spe_status);
2093     tmp = int64_to_float32(1ULL << 32, &env->spe_status);
2094     u.f = float32_div(u.f, tmp, &env->spe_status);
2095
2096     return u.l;
2097 }
2098
2099 static always_inline uint32_t efscfuf (uint32_t val)
2100 {
2101     CPU_FloatU u;
2102     float32 tmp;
2103
2104     u.f = uint32_to_float32(val, &env->spe_status);
2105     tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2106     u.f = float32_div(u.f, tmp, &env->spe_status);
2107
2108     return u.l;
2109 }
2110
2111 static always_inline uint32_t efsctsf (uint32_t val)
2112 {
2113     CPU_FloatU u;
2114     float32 tmp;
2115
2116     u.l = val;
2117     /* NaN are not treated the same way IEEE 754 does */
2118     if (unlikely(float32_is_nan(u.f)))
2119         return 0;
2120     tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2121     u.f = float32_mul(u.f, tmp, &env->spe_status);
2122
2123     return float32_to_int32(u.f, &env->spe_status);
2124 }
2125
2126 static always_inline uint32_t efsctuf (uint32_t val)
2127 {
2128     CPU_FloatU u;
2129     float32 tmp;
2130
2131     u.l = val;
2132     /* NaN are not treated the same way IEEE 754 does */
2133     if (unlikely(float32_is_nan(u.f)))
2134         return 0;
2135     tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2136     u.f = float32_mul(u.f, tmp, &env->spe_status);
2137
2138     return float32_to_uint32(u.f, &env->spe_status);
2139 }
2140
2141 #define HELPER_SPE_SINGLE_CONV(name)                                          \
2142 uint32_t helper_e##name (uint32_t val)                                        \
2143 {                                                                             \
2144     return e##name(val);                                                      \
2145 }
2146 /* efscfsi */
2147 HELPER_SPE_SINGLE_CONV(fscfsi);
2148 /* efscfui */
2149 HELPER_SPE_SINGLE_CONV(fscfui);
2150 /* efscfuf */
2151 HELPER_SPE_SINGLE_CONV(fscfuf);
2152 /* efscfsf */
2153 HELPER_SPE_SINGLE_CONV(fscfsf);
2154 /* efsctsi */
2155 HELPER_SPE_SINGLE_CONV(fsctsi);
2156 /* efsctui */
2157 HELPER_SPE_SINGLE_CONV(fsctui);
2158 /* efsctsiz */
2159 HELPER_SPE_SINGLE_CONV(fsctsiz);
2160 /* efsctuiz */
2161 HELPER_SPE_SINGLE_CONV(fsctuiz);
2162 /* efsctsf */
2163 HELPER_SPE_SINGLE_CONV(fsctsf);
2164 /* efsctuf */
2165 HELPER_SPE_SINGLE_CONV(fsctuf);
2166
2167 #define HELPER_SPE_VECTOR_CONV(name)                                          \
2168 uint64_t helper_ev##name (uint64_t val)                                       \
2169 {                                                                             \
2170     return ((uint64_t)e##name(val >> 32) << 32) |                             \
2171             (uint64_t)e##name(val);                                           \
2172 }
2173 /* evfscfsi */
2174 HELPER_SPE_VECTOR_CONV(fscfsi);
2175 /* evfscfui */
2176 HELPER_SPE_VECTOR_CONV(fscfui);
2177 /* evfscfuf */
2178 HELPER_SPE_VECTOR_CONV(fscfuf);
2179 /* evfscfsf */
2180 HELPER_SPE_VECTOR_CONV(fscfsf);
2181 /* evfsctsi */
2182 HELPER_SPE_VECTOR_CONV(fsctsi);
2183 /* evfsctui */
2184 HELPER_SPE_VECTOR_CONV(fsctui);
2185 /* evfsctsiz */
2186 HELPER_SPE_VECTOR_CONV(fsctsiz);
2187 /* evfsctuiz */
2188 HELPER_SPE_VECTOR_CONV(fsctuiz);
2189 /* evfsctsf */
2190 HELPER_SPE_VECTOR_CONV(fsctsf);
2191 /* evfsctuf */
2192 HELPER_SPE_VECTOR_CONV(fsctuf);
2193
2194 /* Single-precision floating-point arithmetic */
2195 static always_inline uint32_t efsadd (uint32_t op1, uint32_t op2)
2196 {
2197     CPU_FloatU u1, u2;
2198     u1.l = op1;
2199     u2.l = op2;
2200     u1.f = float32_add(u1.f, u2.f, &env->spe_status);
2201     return u1.l;
2202 }
2203
2204 static always_inline uint32_t efssub (uint32_t op1, uint32_t op2)
2205 {
2206     CPU_FloatU u1, u2;
2207     u1.l = op1;
2208     u2.l = op2;
2209     u1.f = float32_sub(u1.f, u2.f, &env->spe_status);
2210     return u1.l;
2211 }
2212
2213 static always_inline uint32_t efsmul (uint32_t op1, uint32_t op2)
2214 {
2215     CPU_FloatU u1, u2;
2216     u1.l = op1;
2217     u2.l = op2;
2218     u1.f = float32_mul(u1.f, u2.f, &env->spe_status);
2219     return u1.l;
2220 }
2221
2222 static always_inline uint32_t efsdiv (uint32_t op1, uint32_t op2)
2223 {
2224     CPU_FloatU u1, u2;
2225     u1.l = op1;
2226     u2.l = op2;
2227     u1.f = float32_div(u1.f, u2.f, &env->spe_status);
2228     return u1.l;
2229 }
2230
2231 #define HELPER_SPE_SINGLE_ARITH(name)                                         \
2232 uint32_t helper_e##name (uint32_t op1, uint32_t op2)                          \
2233 {                                                                             \
2234     return e##name(op1, op2);                                                 \
2235 }
2236 /* efsadd */
2237 HELPER_SPE_SINGLE_ARITH(fsadd);
2238 /* efssub */
2239 HELPER_SPE_SINGLE_ARITH(fssub);
2240 /* efsmul */
2241 HELPER_SPE_SINGLE_ARITH(fsmul);
2242 /* efsdiv */
2243 HELPER_SPE_SINGLE_ARITH(fsdiv);
2244
2245 #define HELPER_SPE_VECTOR_ARITH(name)                                         \
2246 uint64_t helper_ev##name (uint64_t op1, uint64_t op2)                         \
2247 {                                                                             \
2248     return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) |                  \
2249             (uint64_t)e##name(op1, op2);                                      \
2250 }
2251 /* evfsadd */
2252 HELPER_SPE_VECTOR_ARITH(fsadd);
2253 /* evfssub */
2254 HELPER_SPE_VECTOR_ARITH(fssub);
2255 /* evfsmul */
2256 HELPER_SPE_VECTOR_ARITH(fsmul);
2257 /* evfsdiv */
2258 HELPER_SPE_VECTOR_ARITH(fsdiv);
2259
2260 /* Single-precision floating-point comparisons */
2261 static always_inline uint32_t efststlt (uint32_t op1, uint32_t op2)
2262 {
2263     CPU_FloatU u1, u2;
2264     u1.l = op1;
2265     u2.l = op2;
2266     return float32_lt(u1.f, u2.f, &env->spe_status) ? 4 : 0;
2267 }
2268
2269 static always_inline uint32_t efststgt (uint32_t op1, uint32_t op2)
2270 {
2271     CPU_FloatU u1, u2;
2272     u1.l = op1;
2273     u2.l = op2;
2274     return float32_le(u1.f, u2.f, &env->spe_status) ? 0 : 4;
2275 }
2276
2277 static always_inline uint32_t efststeq (uint32_t op1, uint32_t op2)
2278 {
2279     CPU_FloatU u1, u2;
2280     u1.l = op1;
2281     u2.l = op2;
2282     return float32_eq(u1.f, u2.f, &env->spe_status) ? 4 : 0;
2283 }
2284
2285 static always_inline uint32_t efscmplt (uint32_t op1, uint32_t op2)
2286 {
2287     /* XXX: TODO: test special values (NaN, infinites, ...) */
2288     return efststlt(op1, op2);
2289 }
2290
2291 static always_inline uint32_t efscmpgt (uint32_t op1, uint32_t op2)
2292 {
2293     /* XXX: TODO: test special values (NaN, infinites, ...) */
2294     return efststgt(op1, op2);
2295 }
2296
2297 static always_inline uint32_t efscmpeq (uint32_t op1, uint32_t op2)
2298 {
2299     /* XXX: TODO: test special values (NaN, infinites, ...) */
2300     return efststeq(op1, op2);
2301 }
2302
2303 #define HELPER_SINGLE_SPE_CMP(name)                                           \
2304 uint32_t helper_e##name (uint32_t op1, uint32_t op2)                          \
2305 {                                                                             \
2306     return e##name(op1, op2) << 2;                                            \
2307 }
2308 /* efststlt */
2309 HELPER_SINGLE_SPE_CMP(fststlt);
2310 /* efststgt */
2311 HELPER_SINGLE_SPE_CMP(fststgt);
2312 /* efststeq */
2313 HELPER_SINGLE_SPE_CMP(fststeq);
2314 /* efscmplt */
2315 HELPER_SINGLE_SPE_CMP(fscmplt);
2316 /* efscmpgt */
2317 HELPER_SINGLE_SPE_CMP(fscmpgt);
2318 /* efscmpeq */
2319 HELPER_SINGLE_SPE_CMP(fscmpeq);
2320
2321 static always_inline uint32_t evcmp_merge (int t0, int t1)
2322 {
2323     return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
2324 }
2325
2326 #define HELPER_VECTOR_SPE_CMP(name)                                           \
2327 uint32_t helper_ev##name (uint64_t op1, uint64_t op2)                         \
2328 {                                                                             \
2329     return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2));     \
2330 }
2331 /* evfststlt */
2332 HELPER_VECTOR_SPE_CMP(fststlt);
2333 /* evfststgt */
2334 HELPER_VECTOR_SPE_CMP(fststgt);
2335 /* evfststeq */
2336 HELPER_VECTOR_SPE_CMP(fststeq);
2337 /* evfscmplt */
2338 HELPER_VECTOR_SPE_CMP(fscmplt);
2339 /* evfscmpgt */
2340 HELPER_VECTOR_SPE_CMP(fscmpgt);
2341 /* evfscmpeq */
2342 HELPER_VECTOR_SPE_CMP(fscmpeq);
2343
2344 /* Double-precision floating-point conversion */
2345 uint64_t helper_efdcfsi (uint32_t val)
2346 {
2347     CPU_DoubleU u;
2348
2349     u.d = int32_to_float64(val, &env->spe_status);
2350
2351     return u.ll;
2352 }
2353
2354 uint64_t helper_efdcfsid (uint64_t val)
2355 {
2356     CPU_DoubleU u;
2357
2358     u.d = int64_to_float64(val, &env->spe_status);
2359
2360     return u.ll;
2361 }
2362
2363 uint64_t helper_efdcfui (uint32_t val)
2364 {
2365     CPU_DoubleU u;
2366
2367     u.d = uint32_to_float64(val, &env->spe_status);
2368
2369     return u.ll;
2370 }
2371
2372 uint64_t helper_efdcfuid (uint64_t val)
2373 {
2374     CPU_DoubleU u;
2375
2376     u.d = uint64_to_float64(val, &env->spe_status);
2377
2378     return u.ll;
2379 }
2380
2381 uint32_t helper_efdctsi (uint64_t val)
2382 {
2383     CPU_DoubleU u;
2384
2385     u.ll = val;
2386     /* NaN are not treated the same way IEEE 754 does */
2387     if (unlikely(float64_is_nan(u.d)))
2388         return 0;
2389
2390     return float64_to_int32(u.d, &env->spe_status);
2391 }
2392
2393 uint32_t helper_efdctui (uint64_t val)
2394 {
2395     CPU_DoubleU u;
2396
2397     u.ll = val;
2398     /* NaN are not treated the same way IEEE 754 does */
2399     if (unlikely(float64_is_nan(u.d)))
2400         return 0;
2401
2402     return float64_to_uint32(u.d, &env->spe_status);
2403 }
2404
2405 uint32_t helper_efdctsiz (uint64_t val)
2406 {
2407     CPU_DoubleU u;
2408
2409     u.ll = val;
2410     /* NaN are not treated the same way IEEE 754 does */
2411     if (unlikely(float64_is_nan(u.d)))
2412         return 0;
2413
2414     return float64_to_int32_round_to_zero(u.d, &env->spe_status);
2415 }
2416
2417 uint64_t helper_efdctsidz (uint64_t val)
2418 {
2419     CPU_DoubleU u;
2420
2421     u.ll = val;
2422     /* NaN are not treated the same way IEEE 754 does */
2423     if (unlikely(float64_is_nan(u.d)))
2424         return 0;
2425
2426     return float64_to_int64_round_to_zero(u.d, &env->spe_status);
2427 }
2428
2429 uint32_t helper_efdctuiz (uint64_t val)
2430 {
2431     CPU_DoubleU u;
2432
2433     u.ll = val;
2434     /* NaN are not treated the same way IEEE 754 does */
2435     if (unlikely(float64_is_nan(u.d)))
2436         return 0;
2437
2438     return float64_to_uint32_round_to_zero(u.d, &env->spe_status);
2439 }
2440
2441 uint64_t helper_efdctuidz (uint64_t val)
2442 {
2443     CPU_DoubleU u;
2444
2445     u.ll = val;
2446     /* NaN are not treated the same way IEEE 754 does */
2447     if (unlikely(float64_is_nan(u.d)))
2448         return 0;
2449
2450     return float64_to_uint64_round_to_zero(u.d, &env->spe_status);
2451 }
2452
2453 uint64_t helper_efdcfsf (uint32_t val)
2454 {
2455     CPU_DoubleU u;
2456     float64 tmp;
2457
2458     u.d = int32_to_float64(val, &env->spe_status);
2459     tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2460     u.d = float64_div(u.d, tmp, &env->spe_status);
2461
2462     return u.ll;
2463 }
2464
2465 uint64_t helper_efdcfuf (uint32_t val)
2466 {
2467     CPU_DoubleU u;
2468     float64 tmp;
2469
2470     u.d = uint32_to_float64(val, &env->spe_status);
2471     tmp = int64_to_float64(1ULL << 32, &env->spe_status);
2472     u.d = float64_div(u.d, tmp, &env->spe_status);
2473
2474     return u.ll;
2475 }
2476
2477 uint32_t helper_efdctsf (uint64_t val)
2478 {
2479     CPU_DoubleU u;
2480     float64 tmp;
2481
2482     u.ll = val;
2483     /* NaN are not treated the same way IEEE 754 does */
2484     if (unlikely(float64_is_nan(u.d)))
2485         return 0;
2486     tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2487     u.d = float64_mul(u.d, tmp, &env->spe_status);
2488
2489     return float64_to_int32(u.d, &env->spe_status);
2490 }
2491
2492 uint32_t helper_efdctuf (uint64_t val)
2493 {
2494     CPU_DoubleU u;
2495     float64 tmp;
2496
2497     u.ll = val;
2498     /* NaN are not treated the same way IEEE 754 does */
2499     if (unlikely(float64_is_nan(u.d)))
2500         return 0;
2501     tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
2502     u.d = float64_mul(u.d, tmp, &env->spe_status);
2503
2504     return float64_to_uint32(u.d, &env->spe_status);
2505 }
2506
2507 uint32_t helper_efscfd (uint64_t val)
2508 {
2509     CPU_DoubleU u1;
2510     CPU_FloatU u2;
2511
2512     u1.ll = val;
2513     u2.f = float64_to_float32(u1.d, &env->spe_status);
2514
2515     return u2.l;
2516 }
2517
2518 uint64_t helper_efdcfs (uint32_t val)
2519 {
2520     CPU_DoubleU u2;
2521     CPU_FloatU u1;
2522
2523     u1.l = val;
2524     u2.d = float32_to_float64(u1.f, &env->spe_status);
2525
2526     return u2.ll;
2527 }
2528
2529 /* Double precision fixed-point arithmetic */
2530 uint64_t helper_efdadd (uint64_t op1, uint64_t op2)
2531 {
2532     CPU_DoubleU u1, u2;
2533     u1.ll = op1;
2534     u2.ll = op2;
2535     u1.d = float64_add(u1.d, u2.d, &env->spe_status);
2536     return u1.ll;
2537 }
2538
2539 uint64_t helper_efdsub (uint64_t op1, uint64_t op2)
2540 {
2541     CPU_DoubleU u1, u2;
2542     u1.ll = op1;
2543     u2.ll = op2;
2544     u1.d = float64_sub(u1.d, u2.d, &env->spe_status);
2545     return u1.ll;
2546 }
2547
2548 uint64_t helper_efdmul (uint64_t op1, uint64_t op2)
2549 {
2550     CPU_DoubleU u1, u2;
2551     u1.ll = op1;
2552     u2.ll = op2;
2553     u1.d = float64_mul(u1.d, u2.d, &env->spe_status);
2554     return u1.ll;
2555 }
2556
2557 uint64_t helper_efddiv (uint64_t op1, uint64_t op2)
2558 {
2559     CPU_DoubleU u1, u2;
2560     u1.ll = op1;
2561     u2.ll = op2;
2562     u1.d = float64_div(u1.d, u2.d, &env->spe_status);
2563     return u1.ll;
2564 }
2565
2566 /* Double precision floating point helpers */
2567 uint32_t helper_efdtstlt (uint64_t op1, uint64_t op2)
2568 {
2569     CPU_DoubleU u1, u2;
2570     u1.ll = op1;
2571     u2.ll = op2;
2572     return float64_lt(u1.d, u2.d, &env->spe_status) ? 4 : 0;
2573 }
2574
2575 uint32_t helper_efdtstgt (uint64_t op1, uint64_t op2)
2576 {
2577     CPU_DoubleU u1, u2;
2578     u1.ll = op1;
2579     u2.ll = op2;
2580     return float64_le(u1.d, u2.d, &env->spe_status) ? 0 : 4;
2581 }
2582
2583 uint32_t helper_efdtsteq (uint64_t op1, uint64_t op2)
2584 {
2585     CPU_DoubleU u1, u2;
2586     u1.ll = op1;
2587     u2.ll = op2;
2588     return float64_eq(u1.d, u2.d, &env->spe_status) ? 4 : 0;
2589 }
2590
2591 uint32_t helper_efdcmplt (uint64_t op1, uint64_t op2)
2592 {
2593     /* XXX: TODO: test special values (NaN, infinites, ...) */
2594     return helper_efdtstlt(op1, op2);
2595 }
2596
2597 uint32_t helper_efdcmpgt (uint64_t op1, uint64_t op2)
2598 {
2599     /* XXX: TODO: test special values (NaN, infinites, ...) */
2600     return helper_efdtstgt(op1, op2);
2601 }
2602
2603 uint32_t helper_efdcmpeq (uint64_t op1, uint64_t op2)
2604 {
2605     /* XXX: TODO: test special values (NaN, infinites, ...) */
2606     return helper_efdtsteq(op1, op2);
2607 }
2608
2609 /*****************************************************************************/
2610 /* Softmmu support */
2611 #if !defined (CONFIG_USER_ONLY)
2612
2613 #define MMUSUFFIX _mmu
2614
2615 #define SHIFT 0
2616 #include "softmmu_template.h"
2617
2618 #define SHIFT 1
2619 #include "softmmu_template.h"
2620
2621 #define SHIFT 2
2622 #include "softmmu_template.h"
2623
2624 #define SHIFT 3
2625 #include "softmmu_template.h"
2626
2627 /* try to fill the TLB and return an exception if error. If retaddr is
2628    NULL, it means that the function was called in C code (i.e. not
2629    from generated code or from helper.c) */
2630 /* XXX: fix it to restore all registers */
2631 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
2632 {
2633     TranslationBlock *tb;
2634     CPUState *saved_env;
2635     unsigned long pc;
2636     int ret;
2637
2638     /* XXX: hack to restore env in all cases, even if not called from
2639        generated code */
2640     saved_env = env;
2641     env = cpu_single_env;
2642     ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
2643     if (unlikely(ret != 0)) {
2644         if (likely(retaddr)) {
2645             /* now we have a real cpu fault */
2646             pc = (unsigned long)retaddr;
2647             tb = tb_find_pc(pc);
2648             if (likely(tb)) {
2649                 /* the PC is inside the translated code. It means that we have
2650                    a virtual CPU fault */
2651                 cpu_restore_state(tb, env, pc, NULL);
2652             }
2653         }
2654         helper_raise_exception_err(env->exception_index, env->error_code);
2655     }
2656     env = saved_env;
2657 }
2658
2659 /* Segment registers load and store */
2660 target_ulong helper_load_sr (target_ulong sr_num)
2661 {
2662     return env->sr[sr_num];
2663 }
2664
2665 void helper_store_sr (target_ulong sr_num, target_ulong val)
2666 {
2667     ppc_store_sr(env, sr_num, val);
2668 }
2669
2670 /* SLB management */
2671 #if defined(TARGET_PPC64)
2672 target_ulong helper_load_slb (target_ulong slb_nr)
2673 {
2674     return ppc_load_slb(env, slb_nr);
2675 }
2676
2677 void helper_store_slb (target_ulong slb_nr, target_ulong rs)
2678 {
2679     ppc_store_slb(env, slb_nr, rs);
2680 }
2681
2682 void helper_slbia (void)
2683 {
2684     ppc_slb_invalidate_all(env);
2685 }
2686
2687 void helper_slbie (target_ulong addr)
2688 {
2689     ppc_slb_invalidate_one(env, addr);
2690 }
2691
2692 #endif /* defined(TARGET_PPC64) */
2693
2694 /* TLB management */
2695 void helper_tlbia (void)
2696 {
2697     ppc_tlb_invalidate_all(env);
2698 }
2699
2700 void helper_tlbie (target_ulong addr)
2701 {
2702     ppc_tlb_invalidate_one(env, addr);
2703 }
2704
2705 /* Software driven TLBs management */
2706 /* PowerPC 602/603 software TLB load instructions helpers */
2707 static void do_6xx_tlb (target_ulong new_EPN, int is_code)
2708 {
2709     target_ulong RPN, CMP, EPN;
2710     int way;
2711
2712     RPN = env->spr[SPR_RPA];
2713     if (is_code) {
2714         CMP = env->spr[SPR_ICMP];
2715         EPN = env->spr[SPR_IMISS];
2716     } else {
2717         CMP = env->spr[SPR_DCMP];
2718         EPN = env->spr[SPR_DMISS];
2719     }
2720     way = (env->spr[SPR_SRR1] >> 17) & 1;
2721 #if defined (DEBUG_SOFTWARE_TLB)
2722     if (loglevel != 0) {
2723         fprintf(logfile, "%s: EPN " ADDRX " " ADDRX " PTE0 " ADDRX
2724                 " PTE1 " ADDRX " way %d\n",
2725                 __func__, new_EPN, EPN, CMP, RPN, way);
2726     }
2727 #endif
2728     /* Store this TLB */
2729     ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
2730                      way, is_code, CMP, RPN);
2731 }
2732
2733 void helper_6xx_tlbd (target_ulong EPN)
2734 {
2735     do_6xx_tlb(EPN, 0);
2736 }
2737
2738 void helper_6xx_tlbi (target_ulong EPN)
2739 {
2740     do_6xx_tlb(EPN, 1);
2741 }
2742
2743 /* PowerPC 74xx software TLB load instructions helpers */
2744 static void do_74xx_tlb (target_ulong new_EPN, int is_code)
2745 {
2746     target_ulong RPN, CMP, EPN;
2747     int way;
2748
2749     RPN = env->spr[SPR_PTELO];
2750     CMP = env->spr[SPR_PTEHI];
2751     EPN = env->spr[SPR_TLBMISS] & ~0x3;
2752     way = env->spr[SPR_TLBMISS] & 0x3;
2753 #if defined (DEBUG_SOFTWARE_TLB)
2754     if (loglevel != 0) {
2755         fprintf(logfile, "%s: EPN " ADDRX " " ADDRX " PTE0 " ADDRX
2756                 " PTE1 " ADDRX " way %d\n",
2757                 __func__, new_EPN, EPN, CMP, RPN, way);
2758     }
2759 #endif
2760     /* Store this TLB */
2761     ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
2762                      way, is_code, CMP, RPN);
2763 }
2764
2765 void helper_74xx_tlbd (target_ulong EPN)
2766 {
2767     do_74xx_tlb(EPN, 0);
2768 }
2769
2770 void helper_74xx_tlbi (target_ulong EPN)
2771 {
2772     do_74xx_tlb(EPN, 1);
2773 }
2774
2775 static always_inline target_ulong booke_tlb_to_page_size (int size)
2776 {
2777     return 1024 << (2 * size);
2778 }
2779
2780 static always_inline int booke_page_size_to_tlb (target_ulong page_size)
2781 {
2782     int size;
2783
2784     switch (page_size) {
2785     case 0x00000400UL:
2786         size = 0x0;
2787         break;
2788     case 0x00001000UL:
2789         size = 0x1;
2790         break;
2791     case 0x00004000UL:
2792         size = 0x2;
2793         break;
2794     case 0x00010000UL:
2795         size = 0x3;
2796         break;
2797     case 0x00040000UL:
2798         size = 0x4;
2799         break;
2800     case 0x00100000UL:
2801         size = 0x5;
2802         break;
2803     case 0x00400000UL:
2804         size = 0x6;
2805         break;
2806     case 0x01000000UL:
2807         size = 0x7;
2808         break;
2809     case 0x04000000UL:
2810         size = 0x8;
2811         break;
2812     case 0x10000000UL:
2813         size = 0x9;
2814         break;
2815     case 0x40000000UL:
2816         size = 0xA;
2817         break;
2818 #if defined (TARGET_PPC64)
2819     case 0x000100000000ULL:
2820         size = 0xB;
2821         break;
2822     case 0x000400000000ULL:
2823         size = 0xC;
2824         break;
2825     case 0x001000000000ULL:
2826         size = 0xD;
2827         break;
2828     case 0x004000000000ULL:
2829         size = 0xE;
2830         break;
2831     case 0x010000000000ULL:
2832         size = 0xF;
2833         break;
2834 #endif
2835     default:
2836         size = -1;
2837         break;
2838     }
2839
2840     return size;
2841 }
2842
2843 /* Helpers for 4xx TLB management */
2844 target_ulong helper_4xx_tlbre_lo (target_ulong entry)
2845 {
2846     ppcemb_tlb_t *tlb;
2847     target_ulong ret;
2848     int size;
2849
2850     entry &= 0x3F;
2851     tlb = &env->tlb[entry].tlbe;
2852     ret = tlb->EPN;
2853     if (tlb->prot & PAGE_VALID)
2854         ret |= 0x400;
2855     size = booke_page_size_to_tlb(tlb->size);
2856     if (size < 0 || size > 0x7)
2857         size = 1;
2858     ret |= size << 7;
2859     env->spr[SPR_40x_PID] = tlb->PID;
2860     return ret;
2861 }
2862
2863 target_ulong helper_4xx_tlbre_hi (target_ulong entry)
2864 {
2865     ppcemb_tlb_t *tlb;
2866     target_ulong ret;
2867
2868     entry &= 0x3F;
2869     tlb = &env->tlb[entry].tlbe;
2870     ret = tlb->RPN;
2871     if (tlb->prot & PAGE_EXEC)
2872         ret |= 0x200;
2873     if (tlb->prot & PAGE_WRITE)
2874         ret |= 0x100;
2875     return ret;
2876 }
2877
2878 void helper_4xx_tlbwe_hi (target_ulong entry, target_ulong val)
2879 {
2880     ppcemb_tlb_t *tlb;
2881     target_ulong page, end;
2882
2883 #if defined (DEBUG_SOFTWARE_TLB)
2884     if (loglevel != 0) {
2885         fprintf(logfile, "%s entry %d val " ADDRX "\n", __func__, (int)entry, val);
2886     }
2887 #endif
2888     entry &= 0x3F;
2889     tlb = &env->tlb[entry].tlbe;
2890     /* Invalidate previous TLB (if it's valid) */
2891     if (tlb->prot & PAGE_VALID) {
2892         end = tlb->EPN + tlb->size;
2893 #if defined (DEBUG_SOFTWARE_TLB)
2894         if (loglevel != 0) {
2895             fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2896                     " end " ADDRX "\n", __func__, (int)entry, tlb->EPN, end);
2897         }
2898 #endif
2899         for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2900             tlb_flush_page(env, page);
2901     }
2902     tlb->size = booke_tlb_to_page_size((val >> 7) & 0x7);
2903     /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2904      * If this ever occurs, one should use the ppcemb target instead
2905      * of the ppc or ppc64 one
2906      */
2907     if ((val & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
2908         cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2909                   "are not supported (%d)\n",
2910                   tlb->size, TARGET_PAGE_SIZE, (int)((val >> 7) & 0x7));
2911     }
2912     tlb->EPN = val & ~(tlb->size - 1);
2913     if (val & 0x40)
2914         tlb->prot |= PAGE_VALID;
2915     else
2916         tlb->prot &= ~PAGE_VALID;
2917     if (val & 0x20) {
2918         /* XXX: TO BE FIXED */
2919         cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2920     }
2921     tlb->PID = env->spr[SPR_40x_PID]; /* PID */
2922     tlb->attr = val & 0xFF;
2923 #if defined (DEBUG_SOFTWARE_TLB)
2924     if (loglevel != 0) {
2925         fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2926                 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2927                 (int)entry, tlb->RPN, tlb->EPN, tlb->size,
2928                 tlb->prot & PAGE_READ ? 'r' : '-',
2929                 tlb->prot & PAGE_WRITE ? 'w' : '-',
2930                 tlb->prot & PAGE_EXEC ? 'x' : '-',
2931                 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2932     }
2933 #endif
2934     /* Invalidate new TLB (if valid) */
2935     if (tlb->prot & PAGE_VALID) {
2936         end = tlb->EPN + tlb->size;
2937 #if defined (DEBUG_SOFTWARE_TLB)
2938         if (loglevel != 0) {
2939             fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
2940                     " end " ADDRX "\n", __func__, (int)entry, tlb->EPN, end);
2941         }
2942 #endif
2943         for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2944             tlb_flush_page(env, page);
2945     }
2946 }
2947
2948 void helper_4xx_tlbwe_lo (target_ulong entry, target_ulong val)
2949 {
2950     ppcemb_tlb_t *tlb;
2951
2952 #if defined (DEBUG_SOFTWARE_TLB)
2953     if (loglevel != 0) {
2954         fprintf(logfile, "%s entry %i val " ADDRX "\n", __func__, (int)entry, val);
2955     }
2956 #endif
2957     entry &= 0x3F;
2958     tlb = &env->tlb[entry].tlbe;
2959     tlb->RPN = val & 0xFFFFFC00;
2960     tlb->prot = PAGE_READ;
2961     if (val & 0x200)
2962         tlb->prot |= PAGE_EXEC;
2963     if (val & 0x100)
2964         tlb->prot |= PAGE_WRITE;
2965 #if defined (DEBUG_SOFTWARE_TLB)
2966     if (loglevel != 0) {
2967         fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
2968                 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
2969                 (int)entry, tlb->RPN, tlb->EPN, tlb->size,
2970                 tlb->prot & PAGE_READ ? 'r' : '-',
2971                 tlb->prot & PAGE_WRITE ? 'w' : '-',
2972                 tlb->prot & PAGE_EXEC ? 'x' : '-',
2973                 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2974     }
2975 #endif
2976 }
2977
2978 target_ulong helper_4xx_tlbsx (target_ulong address)
2979 {
2980     return ppcemb_tlb_search(env, address, env->spr[SPR_40x_PID]);
2981 }
2982
2983 /* PowerPC 440 TLB management */
2984 void helper_440_tlbwe (uint32_t word, target_ulong entry, target_ulong value)
2985 {
2986     ppcemb_tlb_t *tlb;
2987     target_ulong EPN, RPN, size;
2988     int do_flush_tlbs;
2989
2990 #if defined (DEBUG_SOFTWARE_TLB)
2991     if (loglevel != 0) {
2992         fprintf(logfile, "%s word %d entry %d value " ADDRX "\n",
2993                 __func__, word, (int)entry, value);
2994     }
2995 #endif
2996     do_flush_tlbs = 0;
2997     entry &= 0x3F;
2998     tlb = &env->tlb[entry].tlbe;
2999     switch (word) {
3000     default:
3001         /* Just here to please gcc */
3002     case 0:
3003         EPN = value & 0xFFFFFC00;
3004         if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
3005             do_flush_tlbs = 1;
3006         tlb->EPN = EPN;
3007         size = booke_tlb_to_page_size((value >> 4) & 0xF);
3008         if ((tlb->prot & PAGE_VALID) && tlb->size < size)
3009             do_flush_tlbs = 1;
3010         tlb->size = size;
3011         tlb->attr &= ~0x1;
3012         tlb->attr |= (value >> 8) & 1;
3013         if (value & 0x200) {
3014             tlb->prot |= PAGE_VALID;
3015         } else {
3016             if (tlb->prot & PAGE_VALID) {
3017                 tlb->prot &= ~PAGE_VALID;
3018                 do_flush_tlbs = 1;
3019             }
3020         }
3021         tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
3022         if (do_flush_tlbs)
3023             tlb_flush(env, 1);
3024         break;
3025     case 1:
3026         RPN = value & 0xFFFFFC0F;
3027         if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
3028             tlb_flush(env, 1);
3029         tlb->RPN = RPN;
3030         break;
3031     case 2:
3032         tlb->attr = (tlb->attr & 0x1) | (value & 0x0000FF00);
3033         tlb->prot = tlb->prot & PAGE_VALID;
3034         if (value & 0x1)
3035             tlb->prot |= PAGE_READ << 4;
3036         if (value & 0x2)
3037             tlb->prot |= PAGE_WRITE << 4;
3038         if (value & 0x4)
3039             tlb->prot |= PAGE_EXEC << 4;
3040         if (value & 0x8)
3041             tlb->prot |= PAGE_READ;
3042         if (value & 0x10)
3043             tlb->prot |= PAGE_WRITE;
3044         if (value & 0x20)
3045             tlb->prot |= PAGE_EXEC;
3046         break;
3047     }
3048 }
3049
3050 target_ulong helper_440_tlbre (uint32_t word, target_ulong entry)
3051 {
3052     ppcemb_tlb_t *tlb;
3053     target_ulong ret;
3054     int size;
3055
3056     entry &= 0x3F;
3057     tlb = &env->tlb[entry].tlbe;
3058     switch (word) {
3059     default:
3060         /* Just here to please gcc */
3061     case 0:
3062         ret = tlb->EPN;
3063         size = booke_page_size_to_tlb(tlb->size);
3064         if (size < 0 || size > 0xF)
3065             size = 1;
3066         ret |= size << 4;
3067         if (tlb->attr & 0x1)
3068             ret |= 0x100;
3069         if (tlb->prot & PAGE_VALID)
3070             ret |= 0x200;
3071         env->spr[SPR_440_MMUCR] &= ~0x000000FF;
3072         env->spr[SPR_440_MMUCR] |= tlb->PID;
3073         break;
3074     case 1:
3075         ret = tlb->RPN;
3076         break;
3077     case 2:
3078         ret = tlb->attr & ~0x1;
3079         if (tlb->prot & (PAGE_READ << 4))
3080             ret |= 0x1;
3081         if (tlb->prot & (PAGE_WRITE << 4))
3082             ret |= 0x2;
3083         if (tlb->prot & (PAGE_EXEC << 4))
3084             ret |= 0x4;
3085         if (tlb->prot & PAGE_READ)
3086             ret |= 0x8;
3087         if (tlb->prot & PAGE_WRITE)
3088             ret |= 0x10;
3089         if (tlb->prot & PAGE_EXEC)
3090             ret |= 0x20;
3091         break;
3092     }
3093     return ret;
3094 }
3095
3096 target_ulong helper_440_tlbsx (target_ulong address)
3097 {
3098     return ppcemb_tlb_search(env, address, env->spr[SPR_440_MMUCR] & 0xFF);
3099 }
3100
3101 #endif /* !CONFIG_USER_ONLY */