2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
22 #include "host-utils.h"
25 #include "helper_regs.h"
28 //#define DEBUG_EXCEPTIONS
29 //#define DEBUG_SOFTWARE_TLB
31 #ifdef DEBUG_SOFTWARE_TLB
32 # define LOG_SWTLB(...) qemu_log(__VA_ARGS__)
34 # define LOG_SWTLB(...) do { } while (0)
38 /*****************************************************************************/
39 /* Exceptions processing helpers */
41 void helper_raise_exception_err (uint32_t exception
, uint32_t error_code
)
44 printf("Raise exception %3x code : %d\n", exception
, error_code
);
46 env
->exception_index
= exception
;
47 env
->error_code
= error_code
;
51 void helper_raise_exception (uint32_t exception
)
53 helper_raise_exception_err(exception
, 0);
56 /*****************************************************************************/
58 void helper_load_dump_spr (uint32_t sprn
)
60 qemu_log("Read SPR %d %03x => " ADDRX
"\n",
61 sprn
, sprn
, env
->spr
[sprn
]);
64 void helper_store_dump_spr (uint32_t sprn
)
66 qemu_log("Write SPR %d %03x <= " ADDRX
"\n",
67 sprn
, sprn
, env
->spr
[sprn
]);
70 target_ulong
helper_load_tbl (void)
72 return cpu_ppc_load_tbl(env
);
75 target_ulong
helper_load_tbu (void)
77 return cpu_ppc_load_tbu(env
);
80 target_ulong
helper_load_atbl (void)
82 return cpu_ppc_load_atbl(env
);
85 target_ulong
helper_load_atbu (void)
87 return cpu_ppc_load_atbu(env
);
90 target_ulong
helper_load_601_rtcl (void)
92 return cpu_ppc601_load_rtcl(env
);
95 target_ulong
helper_load_601_rtcu (void)
97 return cpu_ppc601_load_rtcu(env
);
100 #if !defined(CONFIG_USER_ONLY)
101 #if defined (TARGET_PPC64)
102 void helper_store_asr (target_ulong val
)
104 ppc_store_asr(env
, val
);
108 void helper_store_sdr1 (target_ulong val
)
110 ppc_store_sdr1(env
, val
);
113 void helper_store_tbl (target_ulong val
)
115 cpu_ppc_store_tbl(env
, val
);
118 void helper_store_tbu (target_ulong val
)
120 cpu_ppc_store_tbu(env
, val
);
123 void helper_store_atbl (target_ulong val
)
125 cpu_ppc_store_atbl(env
, val
);
128 void helper_store_atbu (target_ulong val
)
130 cpu_ppc_store_atbu(env
, val
);
133 void helper_store_601_rtcl (target_ulong val
)
135 cpu_ppc601_store_rtcl(env
, val
);
138 void helper_store_601_rtcu (target_ulong val
)
140 cpu_ppc601_store_rtcu(env
, val
);
143 target_ulong
helper_load_decr (void)
145 return cpu_ppc_load_decr(env
);
148 void helper_store_decr (target_ulong val
)
150 cpu_ppc_store_decr(env
, val
);
153 void helper_store_hid0_601 (target_ulong val
)
157 hid0
= env
->spr
[SPR_HID0
];
158 if ((val
^ hid0
) & 0x00000008) {
159 /* Change current endianness */
160 env
->hflags
&= ~(1 << MSR_LE
);
161 env
->hflags_nmsr
&= ~(1 << MSR_LE
);
162 env
->hflags_nmsr
|= (1 << MSR_LE
) & (((val
>> 3) & 1) << MSR_LE
);
163 env
->hflags
|= env
->hflags_nmsr
;
164 qemu_log("%s: set endianness to %c => " ADDRX
"\n",
165 __func__
, val
& 0x8 ? 'l' : 'b', env
->hflags
);
167 env
->spr
[SPR_HID0
] = (uint32_t)val
;
170 void helper_store_403_pbr (uint32_t num
, target_ulong value
)
172 if (likely(env
->pb
[num
] != value
)) {
173 env
->pb
[num
] = value
;
174 /* Should be optimized */
179 target_ulong
helper_load_40x_pit (void)
181 return load_40x_pit(env
);
184 void helper_store_40x_pit (target_ulong val
)
186 store_40x_pit(env
, val
);
189 void helper_store_40x_dbcr0 (target_ulong val
)
191 store_40x_dbcr0(env
, val
);
194 void helper_store_40x_sler (target_ulong val
)
196 store_40x_sler(env
, val
);
199 void helper_store_booke_tcr (target_ulong val
)
201 store_booke_tcr(env
, val
);
204 void helper_store_booke_tsr (target_ulong val
)
206 store_booke_tsr(env
, val
);
209 void helper_store_ibatu (uint32_t nr
, target_ulong val
)
211 ppc_store_ibatu(env
, nr
, val
);
214 void helper_store_ibatl (uint32_t nr
, target_ulong val
)
216 ppc_store_ibatl(env
, nr
, val
);
219 void helper_store_dbatu (uint32_t nr
, target_ulong val
)
221 ppc_store_dbatu(env
, nr
, val
);
224 void helper_store_dbatl (uint32_t nr
, target_ulong val
)
226 ppc_store_dbatl(env
, nr
, val
);
229 void helper_store_601_batl (uint32_t nr
, target_ulong val
)
231 ppc_store_ibatl_601(env
, nr
, val
);
234 void helper_store_601_batu (uint32_t nr
, target_ulong val
)
236 ppc_store_ibatu_601(env
, nr
, val
);
240 /*****************************************************************************/
241 /* Memory load and stores */
243 static always_inline target_ulong
addr_add(target_ulong addr
, target_long arg
)
245 #if defined(TARGET_PPC64)
247 return (uint32_t)(addr
+ arg
);
253 void helper_lmw (target_ulong addr
, uint32_t reg
)
255 for (; reg
< 32; reg
++) {
257 env
->gpr
[reg
] = bswap32(ldl(addr
));
259 env
->gpr
[reg
] = ldl(addr
);
260 addr
= addr_add(addr
, 4);
264 void helper_stmw (target_ulong addr
, uint32_t reg
)
266 for (; reg
< 32; reg
++) {
268 stl(addr
, bswap32((uint32_t)env
->gpr
[reg
]));
270 stl(addr
, (uint32_t)env
->gpr
[reg
]);
271 addr
= addr_add(addr
, 4);
275 void helper_lsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
278 for (; nb
> 3; nb
-= 4) {
279 env
->gpr
[reg
] = ldl(addr
);
280 reg
= (reg
+ 1) % 32;
281 addr
= addr_add(addr
, 4);
283 if (unlikely(nb
> 0)) {
285 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
286 env
->gpr
[reg
] |= ldub(addr
) << sh
;
287 addr
= addr_add(addr
, 1);
291 /* PPC32 specification says we must generate an exception if
292 * rA is in the range of registers to be loaded.
293 * In an other hand, IBM says this is valid, but rA won't be loaded.
294 * For now, I'll follow the spec...
296 void helper_lswx(target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
298 if (likely(xer_bc
!= 0)) {
299 if (unlikely((ra
!= 0 && reg
< ra
&& (reg
+ xer_bc
) > ra
) ||
300 (reg
< rb
&& (reg
+ xer_bc
) > rb
))) {
301 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
303 POWERPC_EXCP_INVAL_LSWX
);
305 helper_lsw(addr
, xer_bc
, reg
);
310 void helper_stsw(target_ulong addr
, uint32_t nb
, uint32_t reg
)
313 for (; nb
> 3; nb
-= 4) {
314 stl(addr
, env
->gpr
[reg
]);
315 reg
= (reg
+ 1) % 32;
316 addr
= addr_add(addr
, 4);
318 if (unlikely(nb
> 0)) {
319 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
320 stb(addr
, (env
->gpr
[reg
] >> sh
) & 0xFF);
321 addr
= addr_add(addr
, 1);
326 static void do_dcbz(target_ulong addr
, int dcache_line_size
)
328 addr
&= ~(dcache_line_size
- 1);
330 for (i
= 0 ; i
< dcache_line_size
; i
+= 4) {
333 if (env
->reserve
== addr
)
334 env
->reserve
= (target_ulong
)-1ULL;
337 void helper_dcbz(target_ulong addr
)
339 do_dcbz(addr
, env
->dcache_line_size
);
342 void helper_dcbz_970(target_ulong addr
)
344 if (((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1)
347 do_dcbz(addr
, env
->dcache_line_size
);
350 void helper_icbi(target_ulong addr
)
354 addr
&= ~(env
->dcache_line_size
- 1);
355 /* Invalidate one cache line :
356 * PowerPC specification says this is to be treated like a load
357 * (not a fetch) by the MMU. To be sure it will be so,
358 * do the load "by hand".
361 tb_invalidate_page_range(addr
, addr
+ env
->icache_line_size
);
365 target_ulong
helper_lscbx (target_ulong addr
, uint32_t reg
, uint32_t ra
, uint32_t rb
)
369 for (i
= 0; i
< xer_bc
; i
++) {
371 addr
= addr_add(addr
, 1);
372 /* ra (if not 0) and rb are never modified */
373 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
374 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
376 if (unlikely(c
== xer_cmp
))
378 if (likely(d
!= 0)) {
389 /*****************************************************************************/
390 /* Fixed point operations helpers */
391 #if defined(TARGET_PPC64)
393 /* multiply high word */
394 uint64_t helper_mulhd (uint64_t arg1
, uint64_t arg2
)
398 muls64(&tl
, &th
, arg1
, arg2
);
402 /* multiply high word unsigned */
403 uint64_t helper_mulhdu (uint64_t arg1
, uint64_t arg2
)
407 mulu64(&tl
, &th
, arg1
, arg2
);
411 uint64_t helper_mulldo (uint64_t arg1
, uint64_t arg2
)
416 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
417 /* If th != 0 && th != -1, then we had an overflow */
418 if (likely((uint64_t)(th
+ 1) <= 1)) {
419 env
->xer
&= ~(1 << XER_OV
);
421 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
427 target_ulong
helper_cntlzw (target_ulong t
)
432 #if defined(TARGET_PPC64)
433 target_ulong
helper_cntlzd (target_ulong t
)
439 /* shift right arithmetic helper */
440 target_ulong
helper_sraw (target_ulong value
, target_ulong shift
)
444 if (likely(!(shift
& 0x20))) {
445 if (likely((uint32_t)shift
!= 0)) {
447 ret
= (int32_t)value
>> shift
;
448 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
449 env
->xer
&= ~(1 << XER_CA
);
451 env
->xer
|= (1 << XER_CA
);
454 ret
= (int32_t)value
;
455 env
->xer
&= ~(1 << XER_CA
);
458 ret
= (int32_t)value
>> 31;
460 env
->xer
|= (1 << XER_CA
);
462 env
->xer
&= ~(1 << XER_CA
);
465 return (target_long
)ret
;
468 #if defined(TARGET_PPC64)
469 target_ulong
helper_srad (target_ulong value
, target_ulong shift
)
473 if (likely(!(shift
& 0x40))) {
474 if (likely((uint64_t)shift
!= 0)) {
476 ret
= (int64_t)value
>> shift
;
477 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
478 env
->xer
&= ~(1 << XER_CA
);
480 env
->xer
|= (1 << XER_CA
);
483 ret
= (int64_t)value
;
484 env
->xer
&= ~(1 << XER_CA
);
487 ret
= (int64_t)value
>> 63;
489 env
->xer
|= (1 << XER_CA
);
491 env
->xer
&= ~(1 << XER_CA
);
498 target_ulong
helper_popcntb (target_ulong val
)
500 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
501 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
502 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
506 #if defined(TARGET_PPC64)
507 target_ulong
helper_popcntb_64 (target_ulong val
)
509 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
510 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
511 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
516 /*****************************************************************************/
517 /* Floating point operations helpers */
518 uint64_t helper_float32_to_float64(uint32_t arg
)
523 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
527 uint32_t helper_float64_to_float32(uint64_t arg
)
532 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
536 static always_inline
int isden (float64 d
)
542 return ((u
.ll
>> 52) & 0x7FF) == 0;
545 uint32_t helper_compute_fprf (uint64_t arg
, uint32_t set_fprf
)
551 isneg
= float64_is_neg(farg
.d
);
552 if (unlikely(float64_is_nan(farg
.d
))) {
553 if (float64_is_signaling_nan(farg
.d
)) {
554 /* Signaling NaN: flags are undefined */
560 } else if (unlikely(float64_is_infinity(farg
.d
))) {
567 if (float64_is_zero(farg
.d
)) {
575 /* Denormalized numbers */
578 /* Normalized numbers */
589 /* We update FPSCR_FPRF */
590 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
591 env
->fpscr
|= ret
<< FPSCR_FPRF
;
593 /* We just need fpcc to update Rc1 */
597 /* Floating-point invalid operations exception */
598 static always_inline
uint64_t fload_invalid_op_excp (int op
)
605 case POWERPC_EXCP_FP_VXSNAN
:
606 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
608 case POWERPC_EXCP_FP_VXSOFT
:
609 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
611 case POWERPC_EXCP_FP_VXISI
:
612 /* Magnitude subtraction of infinities */
613 env
->fpscr
|= 1 << FPSCR_VXISI
;
615 case POWERPC_EXCP_FP_VXIDI
:
616 /* Division of infinity by infinity */
617 env
->fpscr
|= 1 << FPSCR_VXIDI
;
619 case POWERPC_EXCP_FP_VXZDZ
:
620 /* Division of zero by zero */
621 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
623 case POWERPC_EXCP_FP_VXIMZ
:
624 /* Multiplication of zero by infinity */
625 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
627 case POWERPC_EXCP_FP_VXVC
:
628 /* Ordered comparison of NaN */
629 env
->fpscr
|= 1 << FPSCR_VXVC
;
630 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
631 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
632 /* We must update the target FPR before raising the exception */
634 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
635 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
636 /* Update the floating-point enabled exception summary */
637 env
->fpscr
|= 1 << FPSCR_FEX
;
638 /* Exception is differed */
642 case POWERPC_EXCP_FP_VXSQRT
:
643 /* Square root of a negative number */
644 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
646 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
648 /* Set the result to quiet NaN */
649 ret
= 0xFFF8000000000000ULL
;
650 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
651 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
654 case POWERPC_EXCP_FP_VXCVI
:
655 /* Invalid conversion */
656 env
->fpscr
|= 1 << FPSCR_VXCVI
;
657 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
659 /* Set the result to quiet NaN */
660 ret
= 0xFFF8000000000000ULL
;
661 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
662 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
666 /* Update the floating-point invalid operation summary */
667 env
->fpscr
|= 1 << FPSCR_VX
;
668 /* Update the floating-point exception summary */
669 env
->fpscr
|= 1 << FPSCR_FX
;
671 /* Update the floating-point enabled exception summary */
672 env
->fpscr
|= 1 << FPSCR_FEX
;
673 if (msr_fe0
!= 0 || msr_fe1
!= 0)
674 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_FP
| op
);
679 static always_inline
void float_zero_divide_excp (void)
681 env
->fpscr
|= 1 << FPSCR_ZX
;
682 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
683 /* Update the floating-point exception summary */
684 env
->fpscr
|= 1 << FPSCR_FX
;
686 /* Update the floating-point enabled exception summary */
687 env
->fpscr
|= 1 << FPSCR_FEX
;
688 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
689 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
690 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
695 static always_inline
void float_overflow_excp (void)
697 env
->fpscr
|= 1 << FPSCR_OX
;
698 /* Update the floating-point exception summary */
699 env
->fpscr
|= 1 << FPSCR_FX
;
701 /* XXX: should adjust the result */
702 /* Update the floating-point enabled exception summary */
703 env
->fpscr
|= 1 << FPSCR_FEX
;
704 /* We must update the target FPR before raising the exception */
705 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
706 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
708 env
->fpscr
|= 1 << FPSCR_XX
;
709 env
->fpscr
|= 1 << FPSCR_FI
;
713 static always_inline
void float_underflow_excp (void)
715 env
->fpscr
|= 1 << FPSCR_UX
;
716 /* Update the floating-point exception summary */
717 env
->fpscr
|= 1 << FPSCR_FX
;
719 /* XXX: should adjust the result */
720 /* Update the floating-point enabled exception summary */
721 env
->fpscr
|= 1 << FPSCR_FEX
;
722 /* We must update the target FPR before raising the exception */
723 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
724 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
728 static always_inline
void float_inexact_excp (void)
730 env
->fpscr
|= 1 << FPSCR_XX
;
731 /* Update the floating-point exception summary */
732 env
->fpscr
|= 1 << FPSCR_FX
;
734 /* Update the floating-point enabled exception summary */
735 env
->fpscr
|= 1 << FPSCR_FEX
;
736 /* We must update the target FPR before raising the exception */
737 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
738 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
742 static always_inline
void fpscr_set_rounding_mode (void)
746 /* Set rounding mode */
749 /* Best approximation (round to nearest) */
750 rnd_type
= float_round_nearest_even
;
753 /* Smaller magnitude (round toward zero) */
754 rnd_type
= float_round_to_zero
;
757 /* Round toward +infinite */
758 rnd_type
= float_round_up
;
762 /* Round toward -infinite */
763 rnd_type
= float_round_down
;
766 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
769 void helper_fpscr_clrbit (uint32_t bit
)
773 prev
= (env
->fpscr
>> bit
) & 1;
774 env
->fpscr
&= ~(1 << bit
);
779 fpscr_set_rounding_mode();
787 void helper_fpscr_setbit (uint32_t bit
)
791 prev
= (env
->fpscr
>> bit
) & 1;
792 env
->fpscr
|= 1 << bit
;
796 env
->fpscr
|= 1 << FPSCR_FX
;
800 env
->fpscr
|= 1 << FPSCR_FX
;
805 env
->fpscr
|= 1 << FPSCR_FX
;
810 env
->fpscr
|= 1 << FPSCR_FX
;
815 env
->fpscr
|= 1 << FPSCR_FX
;
828 env
->fpscr
|= 1 << FPSCR_VX
;
829 env
->fpscr
|= 1 << FPSCR_FX
;
836 env
->error_code
= POWERPC_EXCP_FP
;
838 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
840 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
842 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
844 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
846 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
848 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
850 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
852 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
854 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
861 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
868 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
875 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
882 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
888 fpscr_set_rounding_mode();
893 /* Update the floating-point enabled exception summary */
894 env
->fpscr
|= 1 << FPSCR_FEX
;
895 /* We have to update Rc1 before raising the exception */
896 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
902 void helper_store_fpscr (uint64_t arg
, uint32_t mask
)
905 * We use only the 32 LSB of the incoming fpr
913 new |= prev
& 0x60000000;
914 for (i
= 0; i
< 8; i
++) {
915 if (mask
& (1 << i
)) {
916 env
->fpscr
&= ~(0xF << (4 * i
));
917 env
->fpscr
|= new & (0xF << (4 * i
));
920 /* Update VX and FEX */
922 env
->fpscr
|= 1 << FPSCR_VX
;
924 env
->fpscr
&= ~(1 << FPSCR_VX
);
925 if ((fpscr_ex
& fpscr_eex
) != 0) {
926 env
->fpscr
|= 1 << FPSCR_FEX
;
927 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
928 /* XXX: we should compute it properly */
929 env
->error_code
= POWERPC_EXCP_FP
;
932 env
->fpscr
&= ~(1 << FPSCR_FEX
);
933 fpscr_set_rounding_mode();
936 void helper_float_check_status (void)
938 #ifdef CONFIG_SOFTFLOAT
939 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
940 (env
->error_code
& POWERPC_EXCP_FP
)) {
941 /* Differred floating-point exception after target FPR update */
942 if (msr_fe0
!= 0 || msr_fe1
!= 0)
943 helper_raise_exception_err(env
->exception_index
, env
->error_code
);
945 int status
= get_float_exception_flags(&env
->fp_status
);
946 if (status
& float_flag_divbyzero
) {
947 float_zero_divide_excp();
948 } else if (status
& float_flag_overflow
) {
949 float_overflow_excp();
950 } else if (status
& float_flag_underflow
) {
951 float_underflow_excp();
952 } else if (status
& float_flag_inexact
) {
953 float_inexact_excp();
957 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
958 (env
->error_code
& POWERPC_EXCP_FP
)) {
959 /* Differred floating-point exception after target FPR update */
960 if (msr_fe0
!= 0 || msr_fe1
!= 0)
961 helper_raise_exception_err(env
->exception_index
, env
->error_code
);
966 #ifdef CONFIG_SOFTFLOAT
967 void helper_reset_fpstatus (void)
969 set_float_exception_flags(0, &env
->fp_status
);
974 uint64_t helper_fadd (uint64_t arg1
, uint64_t arg2
)
976 CPU_DoubleU farg1
, farg2
;
980 #if USE_PRECISE_EMULATION
981 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
982 float64_is_signaling_nan(farg2
.d
))) {
984 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
985 } else if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
986 float64_is_neg(farg1
.d
) != float64_is_neg(farg2
.d
))) {
987 /* Magnitude subtraction of infinities */
988 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
990 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
993 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
999 uint64_t helper_fsub (uint64_t arg1
, uint64_t arg2
)
1001 CPU_DoubleU farg1
, farg2
;
1005 #if USE_PRECISE_EMULATION
1007 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1008 float64_is_signaling_nan(farg2
.d
))) {
1009 /* sNaN subtraction */
1010 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1011 } else if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
1012 float64_is_neg(farg1
.d
) == float64_is_neg(farg2
.d
))) {
1013 /* Magnitude subtraction of infinities */
1014 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1016 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
1020 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
1026 uint64_t helper_fmul (uint64_t arg1
, uint64_t arg2
)
1028 CPU_DoubleU farg1
, farg2
;
1032 #if USE_PRECISE_EMULATION
1033 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1034 float64_is_signaling_nan(farg2
.d
))) {
1035 /* sNaN multiplication */
1036 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1037 } else if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1038 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1039 /* Multiplication of zero by infinity */
1040 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1042 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1045 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1051 uint64_t helper_fdiv (uint64_t arg1
, uint64_t arg2
)
1053 CPU_DoubleU farg1
, farg2
;
1057 #if USE_PRECISE_EMULATION
1058 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1059 float64_is_signaling_nan(farg2
.d
))) {
1061 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1062 } else if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
))) {
1063 /* Division of infinity by infinity */
1064 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI
);
1065 } else if (unlikely(float64_is_zero(farg1
.d
) && float64_is_zero(farg2
.d
))) {
1066 /* Division of zero by zero */
1067 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ
);
1069 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
1072 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
1078 uint64_t helper_fabs (uint64_t arg
)
1083 farg
.d
= float64_abs(farg
.d
);
1088 uint64_t helper_fnabs (uint64_t arg
)
1093 farg
.d
= float64_abs(farg
.d
);
1094 farg
.d
= float64_chs(farg
.d
);
1099 uint64_t helper_fneg (uint64_t arg
)
1104 farg
.d
= float64_chs(farg
.d
);
1108 /* fctiw - fctiw. */
1109 uint64_t helper_fctiw (uint64_t arg
)
1114 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1115 /* sNaN conversion */
1116 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1117 } else if (unlikely(float64_is_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1118 /* qNan / infinity conversion */
1119 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1121 farg
.ll
= float64_to_int32(farg
.d
, &env
->fp_status
);
1122 #if USE_PRECISE_EMULATION
1123 /* XXX: higher bits are not supposed to be significant.
1124 * to make tests easier, return the same as a real PowerPC 750
1126 farg
.ll
|= 0xFFF80000ULL
<< 32;
1132 /* fctiwz - fctiwz. */
1133 uint64_t helper_fctiwz (uint64_t arg
)
1138 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1139 /* sNaN conversion */
1140 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1141 } else if (unlikely(float64_is_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1142 /* qNan / infinity conversion */
1143 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1145 farg
.ll
= float64_to_int32_round_to_zero(farg
.d
, &env
->fp_status
);
1146 #if USE_PRECISE_EMULATION
1147 /* XXX: higher bits are not supposed to be significant.
1148 * to make tests easier, return the same as a real PowerPC 750
1150 farg
.ll
|= 0xFFF80000ULL
<< 32;
1156 #if defined(TARGET_PPC64)
1157 /* fcfid - fcfid. */
1158 uint64_t helper_fcfid (uint64_t arg
)
1161 farg
.d
= int64_to_float64(arg
, &env
->fp_status
);
1165 /* fctid - fctid. */
1166 uint64_t helper_fctid (uint64_t arg
)
1171 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1172 /* sNaN conversion */
1173 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1174 } else if (unlikely(float64_is_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1175 /* qNan / infinity conversion */
1176 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1178 farg
.ll
= float64_to_int64(farg
.d
, &env
->fp_status
);
1183 /* fctidz - fctidz. */
1184 uint64_t helper_fctidz (uint64_t arg
)
1189 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1190 /* sNaN conversion */
1191 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1192 } else if (unlikely(float64_is_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1193 /* qNan / infinity conversion */
1194 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1196 farg
.ll
= float64_to_int64_round_to_zero(farg
.d
, &env
->fp_status
);
1203 static always_inline
uint64_t do_fri (uint64_t arg
, int rounding_mode
)
1208 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1210 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
| POWERPC_EXCP_FP_VXCVI
);
1211 } else if (unlikely(float64_is_nan(farg
.d
) || float64_is_infinity(farg
.d
))) {
1212 /* qNan / infinity round */
1213 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI
);
1215 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
1216 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
1217 /* Restore rounding mode from FPSCR */
1218 fpscr_set_rounding_mode();
1223 uint64_t helper_frin (uint64_t arg
)
1225 return do_fri(arg
, float_round_nearest_even
);
1228 uint64_t helper_friz (uint64_t arg
)
1230 return do_fri(arg
, float_round_to_zero
);
1233 uint64_t helper_frip (uint64_t arg
)
1235 return do_fri(arg
, float_round_up
);
1238 uint64_t helper_frim (uint64_t arg
)
1240 return do_fri(arg
, float_round_down
);
1243 /* fmadd - fmadd. */
1244 uint64_t helper_fmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1246 CPU_DoubleU farg1
, farg2
, farg3
;
1251 #if USE_PRECISE_EMULATION
1252 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1253 float64_is_signaling_nan(farg2
.d
) ||
1254 float64_is_signaling_nan(farg3
.d
))) {
1255 /* sNaN operation */
1256 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1257 } else if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1258 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1259 /* Multiplication of zero by infinity */
1260 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1263 /* This is the way the PowerPC specification defines it */
1264 float128 ft0_128
, ft1_128
;
1266 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1267 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1268 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1269 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1270 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
1271 /* Magnitude subtraction of infinities */
1272 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1274 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1275 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1276 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1279 /* This is OK on x86 hosts */
1280 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1284 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1285 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1290 /* fmsub - fmsub. */
1291 uint64_t helper_fmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1293 CPU_DoubleU farg1
, farg2
, farg3
;
1298 #if USE_PRECISE_EMULATION
1299 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1300 float64_is_signaling_nan(farg2
.d
) ||
1301 float64_is_signaling_nan(farg3
.d
))) {
1302 /* sNaN operation */
1303 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1304 } else if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1305 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1306 /* Multiplication of zero by infinity */
1307 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1310 /* This is the way the PowerPC specification defines it */
1311 float128 ft0_128
, ft1_128
;
1313 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1314 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1315 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1316 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1317 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
1318 /* Magnitude subtraction of infinities */
1319 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1321 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1322 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1323 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1326 /* This is OK on x86 hosts */
1327 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1331 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1332 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1337 /* fnmadd - fnmadd. */
1338 uint64_t helper_fnmadd (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1340 CPU_DoubleU farg1
, farg2
, farg3
;
1346 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1347 float64_is_signaling_nan(farg2
.d
) ||
1348 float64_is_signaling_nan(farg3
.d
))) {
1349 /* sNaN operation */
1350 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1351 } else if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1352 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1353 /* Multiplication of zero by infinity */
1354 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1356 #if USE_PRECISE_EMULATION
1358 /* This is the way the PowerPC specification defines it */
1359 float128 ft0_128
, ft1_128
;
1361 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1362 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1363 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1364 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1365 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
1366 /* Magnitude subtraction of infinities */
1367 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1369 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1370 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
1371 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1374 /* This is OK on x86 hosts */
1375 farg1
.d
= (farg1
.d
* farg2
.d
) + farg3
.d
;
1378 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1379 farg1
.d
= float64_add(farg1
.d
, farg3
.d
, &env
->fp_status
);
1381 if (likely(!float64_is_nan(farg1
.d
)))
1382 farg1
.d
= float64_chs(farg1
.d
);
1387 /* fnmsub - fnmsub. */
1388 uint64_t helper_fnmsub (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1390 CPU_DoubleU farg1
, farg2
, farg3
;
1396 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
1397 float64_is_signaling_nan(farg2
.d
) ||
1398 float64_is_signaling_nan(farg3
.d
))) {
1399 /* sNaN operation */
1400 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1401 } else if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
1402 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
1403 /* Multiplication of zero by infinity */
1404 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ
);
1406 #if USE_PRECISE_EMULATION
1408 /* This is the way the PowerPC specification defines it */
1409 float128 ft0_128
, ft1_128
;
1411 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
1412 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
1413 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
1414 if (unlikely(float128_is_infinity(ft0_128
) && float64_is_infinity(farg3
.d
) &&
1415 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
1416 /* Magnitude subtraction of infinities */
1417 farg1
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI
);
1419 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
1420 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
1421 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
1424 /* This is OK on x86 hosts */
1425 farg1
.d
= (farg1
.d
* farg2
.d
) - farg3
.d
;
1428 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
1429 farg1
.d
= float64_sub(farg1
.d
, farg3
.d
, &env
->fp_status
);
1431 if (likely(!float64_is_nan(farg1
.d
)))
1432 farg1
.d
= float64_chs(farg1
.d
);
1438 uint64_t helper_frsp (uint64_t arg
)
1444 #if USE_PRECISE_EMULATION
1445 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1446 /* sNaN square root */
1447 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1449 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1450 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1453 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1454 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1459 /* fsqrt - fsqrt. */
1460 uint64_t helper_fsqrt (uint64_t arg
)
1465 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1466 /* sNaN square root */
1467 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1468 } else if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
1469 /* Square root of a negative nonzero number */
1470 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1472 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1478 uint64_t helper_fre (uint64_t arg
)
1483 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1484 /* sNaN reciprocal */
1485 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1487 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1493 uint64_t helper_fres (uint64_t arg
)
1499 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1500 /* sNaN reciprocal */
1501 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1503 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1504 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1505 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1510 /* frsqrte - frsqrte. */
1511 uint64_t helper_frsqrte (uint64_t arg
)
1517 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
1518 /* sNaN reciprocal square root */
1519 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1520 } else if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
1521 /* Reciprocal square root of a negative nonzero number */
1522 farg
.ll
= fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT
);
1524 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1525 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1526 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
1527 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
1533 uint64_t helper_fsel (uint64_t arg1
, uint64_t arg2
, uint64_t arg3
)
1539 if ((!float64_is_neg(farg1
.d
) || float64_is_zero(farg1
.d
)) && !float64_is_nan(farg1
.d
))
1545 void helper_fcmpu (uint64_t arg1
, uint64_t arg2
, uint32_t crfD
)
1547 CPU_DoubleU farg1
, farg2
;
1552 if (unlikely(float64_is_nan(farg1
.d
) ||
1553 float64_is_nan(farg2
.d
))) {
1555 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1557 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1563 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1564 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1565 env
->crf
[crfD
] = ret
;
1566 if (unlikely(ret
== 0x01UL
1567 && (float64_is_signaling_nan(farg1
.d
) ||
1568 float64_is_signaling_nan(farg2
.d
)))) {
1569 /* sNaN comparison */
1570 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
);
1574 void helper_fcmpo (uint64_t arg1
, uint64_t arg2
, uint32_t crfD
)
1576 CPU_DoubleU farg1
, farg2
;
1581 if (unlikely(float64_is_nan(farg1
.d
) ||
1582 float64_is_nan(farg2
.d
))) {
1584 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1586 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1592 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1593 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1594 env
->crf
[crfD
] = ret
;
1595 if (unlikely (ret
== 0x01UL
)) {
1596 if (float64_is_signaling_nan(farg1
.d
) ||
1597 float64_is_signaling_nan(farg2
.d
)) {
1598 /* sNaN comparison */
1599 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN
|
1600 POWERPC_EXCP_FP_VXVC
);
1602 /* qNaN comparison */
1603 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC
);
1608 #if !defined (CONFIG_USER_ONLY)
1609 void helper_store_msr (target_ulong val
)
1611 val
= hreg_store_msr(env
, val
, 0);
1613 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1614 helper_raise_exception(val
);
1618 static always_inline
void do_rfi (target_ulong nip
, target_ulong msr
,
1619 target_ulong msrm
, int keep_msrh
)
1621 #if defined(TARGET_PPC64)
1622 if (msr
& (1ULL << MSR_SF
)) {
1623 nip
= (uint64_t)nip
;
1624 msr
&= (uint64_t)msrm
;
1626 nip
= (uint32_t)nip
;
1627 msr
= (uint32_t)(msr
& msrm
);
1629 msr
|= env
->msr
& ~((uint64_t)0xFFFFFFFF);
1632 nip
= (uint32_t)nip
;
1633 msr
&= (uint32_t)msrm
;
1635 /* XXX: beware: this is false if VLE is supported */
1636 env
->nip
= nip
& ~((target_ulong
)0x00000003);
1637 hreg_store_msr(env
, msr
, 1);
1638 #if defined (DEBUG_OP)
1639 cpu_dump_rfi(env
->nip
, env
->msr
);
1641 /* No need to raise an exception here,
1642 * as rfi is always the last insn of a TB
1644 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1647 void helper_rfi (void)
1649 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1650 ~((target_ulong
)0x0), 1);
1653 #if defined(TARGET_PPC64)
1654 void helper_rfid (void)
1656 do_rfi(env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
],
1657 ~((target_ulong
)0x0), 0);
1660 void helper_hrfid (void)
1662 do_rfi(env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
],
1663 ~((target_ulong
)0x0), 0);
1668 void helper_tw (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1670 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
1671 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
1672 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
1673 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
1674 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
1675 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1679 #if defined(TARGET_PPC64)
1680 void helper_td (target_ulong arg1
, target_ulong arg2
, uint32_t flags
)
1682 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
1683 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
1684 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
1685 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
1686 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01)))))
1687 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
, POWERPC_EXCP_TRAP
);
1691 /*****************************************************************************/
1692 /* PowerPC 601 specific instructions (POWER bridge) */
1694 target_ulong
helper_clcs (uint32_t arg
)
1698 /* Instruction cache line size */
1699 return env
->icache_line_size
;
1702 /* Data cache line size */
1703 return env
->dcache_line_size
;
1706 /* Minimum cache line size */
1707 return (env
->icache_line_size
< env
->dcache_line_size
) ?
1708 env
->icache_line_size
: env
->dcache_line_size
;
1711 /* Maximum cache line size */
1712 return (env
->icache_line_size
> env
->dcache_line_size
) ?
1713 env
->icache_line_size
: env
->dcache_line_size
;
1722 target_ulong
helper_div (target_ulong arg1
, target_ulong arg2
)
1724 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1726 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1727 (int32_t)arg2
== 0) {
1728 env
->spr
[SPR_MQ
] = 0;
1731 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1732 return tmp
/ (int32_t)arg2
;
1736 target_ulong
helper_divo (target_ulong arg1
, target_ulong arg2
)
1738 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
1740 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1741 (int32_t)arg2
== 0) {
1742 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1743 env
->spr
[SPR_MQ
] = 0;
1746 env
->spr
[SPR_MQ
] = tmp
% arg2
;
1747 tmp
/= (int32_t)arg2
;
1748 if ((int32_t)tmp
!= tmp
) {
1749 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1751 env
->xer
&= ~(1 << XER_OV
);
1757 target_ulong
helper_divs (target_ulong arg1
, target_ulong arg2
)
1759 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1760 (int32_t)arg2
== 0) {
1761 env
->spr
[SPR_MQ
] = 0;
1764 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1765 return (int32_t)arg1
/ (int32_t)arg2
;
1769 target_ulong
helper_divso (target_ulong arg1
, target_ulong arg2
)
1771 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
1772 (int32_t)arg2
== 0) {
1773 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
1774 env
->spr
[SPR_MQ
] = 0;
1777 env
->xer
&= ~(1 << XER_OV
);
1778 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
1779 return (int32_t)arg1
/ (int32_t)arg2
;
1783 #if !defined (CONFIG_USER_ONLY)
1784 target_ulong
helper_rac (target_ulong addr
)
1788 target_ulong ret
= 0;
1790 /* We don't have to generate many instances of this instruction,
1791 * as rac is supervisor only.
1793 /* XXX: FIX THIS: Pretend we have no BAT */
1794 nb_BATs
= env
->nb_BATs
;
1796 if (get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) == 0)
1798 env
->nb_BATs
= nb_BATs
;
1802 void helper_rfsvc (void)
1804 do_rfi(env
->lr
, env
->ctr
, 0x0000FFFF, 0);
1808 /*****************************************************************************/
1809 /* 602 specific instructions */
1810 /* mfrom is the most crazy instruction ever seen, imho ! */
1811 /* Real implementation uses a ROM table. Do the same */
1812 /* Extremly decomposed:
1814 * return 256 * log10(10 + 1.0) + 0.5
1816 #if !defined (CONFIG_USER_ONLY)
1817 target_ulong
helper_602_mfrom (target_ulong arg
)
1819 if (likely(arg
< 602)) {
1820 #include "mfrom_table.c"
1821 return mfrom_ROM_table
[arg
];
1828 /*****************************************************************************/
1829 /* Embedded PowerPC specific helpers */
1831 /* XXX: to be improved to check access rights when in user-mode */
1832 target_ulong
helper_load_dcr (target_ulong dcrn
)
1834 target_ulong val
= 0;
1836 if (unlikely(env
->dcr_env
== NULL
)) {
1837 qemu_log("No DCR environment\n");
1838 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1839 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1840 } else if (unlikely(ppc_dcr_read(env
->dcr_env
, dcrn
, &val
) != 0)) {
1841 qemu_log("DCR read error %d %03x\n", (int)dcrn
, (int)dcrn
);
1842 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1843 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1848 void helper_store_dcr (target_ulong dcrn
, target_ulong val
)
1850 if (unlikely(env
->dcr_env
== NULL
)) {
1851 qemu_log("No DCR environment\n");
1852 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1853 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
1854 } else if (unlikely(ppc_dcr_write(env
->dcr_env
, dcrn
, val
) != 0)) {
1855 qemu_log("DCR write error %d %03x\n", (int)dcrn
, (int)dcrn
);
1856 helper_raise_exception_err(POWERPC_EXCP_PROGRAM
,
1857 POWERPC_EXCP_INVAL
| POWERPC_EXCP_PRIV_REG
);
1861 #if !defined(CONFIG_USER_ONLY)
1862 void helper_40x_rfci (void)
1864 do_rfi(env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
],
1865 ~((target_ulong
)0xFFFF0000), 0);
1868 void helper_rfci (void)
1870 do_rfi(env
->spr
[SPR_BOOKE_CSRR0
], SPR_BOOKE_CSRR1
,
1871 ~((target_ulong
)0x3FFF0000), 0);
1874 void helper_rfdi (void)
1876 do_rfi(env
->spr
[SPR_BOOKE_DSRR0
], SPR_BOOKE_DSRR1
,
1877 ~((target_ulong
)0x3FFF0000), 0);
1880 void helper_rfmci (void)
1882 do_rfi(env
->spr
[SPR_BOOKE_MCSRR0
], SPR_BOOKE_MCSRR1
,
1883 ~((target_ulong
)0x3FFF0000), 0);
1888 target_ulong
helper_dlmzb (target_ulong high
, target_ulong low
, uint32_t update_Rc
)
1894 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1895 if ((high
& mask
) == 0) {
1903 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1904 if ((low
& mask
) == 0) {
1916 env
->xer
= (env
->xer
& ~0x7F) | i
;
1918 env
->crf
[0] |= xer_so
;
1923 /*****************************************************************************/
1924 /* Altivec extension helpers */
1925 #if defined(WORDS_BIGENDIAN)
1933 #if defined(WORDS_BIGENDIAN)
1934 #define VECTOR_FOR_INORDER_I(index, element) \
1935 for (index = 0; index < ARRAY_SIZE(r->element); index++)
1937 #define VECTOR_FOR_INORDER_I(index, element) \
1938 for (index = ARRAY_SIZE(r->element)-1; index >= 0; index--)
1941 /* If X is a NaN, store the corresponding QNaN into RESULT. Otherwise,
1942 * execute the following block. */
1943 #define DO_HANDLE_NAN(result, x) \
1944 if (float32_is_nan(x) || float32_is_signaling_nan(x)) { \
1947 __f.l = __f.l | (1 << 22); /* Set QNaN bit. */ \
1951 #define HANDLE_NAN1(result, x) \
1952 DO_HANDLE_NAN(result, x)
1953 #define HANDLE_NAN2(result, x, y) \
1954 DO_HANDLE_NAN(result, x) DO_HANDLE_NAN(result, y)
1955 #define HANDLE_NAN3(result, x, y, z) \
1956 DO_HANDLE_NAN(result, x) DO_HANDLE_NAN(result, y) DO_HANDLE_NAN(result, z)
1958 /* Saturating arithmetic helpers. */
1959 #define SATCVT(from, to, from_type, to_type, min, max, use_min, use_max) \
1960 static always_inline to_type cvt##from##to (from_type x, int *sat) \
1963 if (use_min && x < min) { \
1966 } else if (use_max && x > max) { \
1974 SATCVT(sh
, sb
, int16_t, int8_t, INT8_MIN
, INT8_MAX
, 1, 1)
1975 SATCVT(sw
, sh
, int32_t, int16_t, INT16_MIN
, INT16_MAX
, 1, 1)
1976 SATCVT(sd
, sw
, int64_t, int32_t, INT32_MIN
, INT32_MAX
, 1, 1)
1978 /* Work around gcc problems with the macro version */
1979 static always_inline
uint8_t cvtuhub(uint16_t x
, int *sat
)
1983 if (x
> UINT8_MAX
) {
1991 //SATCVT(uh, ub, uint16_t, uint8_t, 0, UINT8_MAX, 0, 1)
1992 SATCVT(uw
, uh
, uint32_t, uint16_t, 0, UINT16_MAX
, 0, 1)
1993 SATCVT(ud
, uw
, uint64_t, uint32_t, 0, UINT32_MAX
, 0, 1)
1994 SATCVT(sh
, ub
, int16_t, uint8_t, 0, UINT8_MAX
, 1, 1)
1995 SATCVT(sw
, uh
, int32_t, uint16_t, 0, UINT16_MAX
, 1, 1)
1996 SATCVT(sd
, uw
, int64_t, uint32_t, 0, UINT32_MAX
, 1, 1)
1999 #define LVE(name, access, swap, element) \
2000 void helper_##name (ppc_avr_t *r, target_ulong addr) \
2002 size_t n_elems = ARRAY_SIZE(r->element); \
2003 int adjust = HI_IDX*(n_elems-1); \
2004 int sh = sizeof(r->element[0]) >> 1; \
2005 int index = (addr & 0xf) >> sh; \
2007 r->element[LO_IDX ? index : (adjust - index)] = swap(access(addr)); \
2009 r->element[LO_IDX ? index : (adjust - index)] = access(addr); \
2013 LVE(lvebx
, ldub
, I
, u8
)
2014 LVE(lvehx
, lduw
, bswap16
, u16
)
2015 LVE(lvewx
, ldl
, bswap32
, u32
)
2019 void helper_lvsl (ppc_avr_t
*r
, target_ulong sh
)
2021 int i
, j
= (sh
& 0xf);
2023 VECTOR_FOR_INORDER_I (i
, u8
) {
2028 void helper_lvsr (ppc_avr_t
*r
, target_ulong sh
)
2030 int i
, j
= 0x10 - (sh
& 0xf);
2032 VECTOR_FOR_INORDER_I (i
, u8
) {
2037 #define STVE(name, access, swap, element) \
2038 void helper_##name (ppc_avr_t *r, target_ulong addr) \
2040 size_t n_elems = ARRAY_SIZE(r->element); \
2041 int adjust = HI_IDX*(n_elems-1); \
2042 int sh = sizeof(r->element[0]) >> 1; \
2043 int index = (addr & 0xf) >> sh; \
2045 access(addr, swap(r->element[LO_IDX ? index : (adjust - index)])); \
2047 access(addr, r->element[LO_IDX ? index : (adjust - index)]); \
2051 STVE(stvebx
, stb
, I
, u8
)
2052 STVE(stvehx
, stw
, bswap16
, u16
)
2053 STVE(stvewx
, stl
, bswap32
, u32
)
2057 void helper_mtvscr (ppc_avr_t
*r
)
2059 #if defined(WORDS_BIGENDIAN)
2060 env
->vscr
= r
->u32
[3];
2062 env
->vscr
= r
->u32
[0];
2064 set_flush_to_zero(vscr_nj
, &env
->vec_status
);
2067 void helper_vaddcuw (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2070 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2071 r
->u32
[i
] = ~a
->u32
[i
] < b
->u32
[i
];
2075 #define VARITH_DO(name, op, element) \
2076 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2079 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2080 r->element[i] = a->element[i] op b->element[i]; \
2083 #define VARITH(suffix, element) \
2084 VARITH_DO(add##suffix, +, element) \
2085 VARITH_DO(sub##suffix, -, element)
2092 #define VARITHFP(suffix, func) \
2093 void helper_v##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2096 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2097 HANDLE_NAN2(r->f[i], a->f[i], b->f[i]) { \
2098 r->f[i] = func(a->f[i], b->f[i], &env->vec_status); \
2102 VARITHFP(addfp
, float32_add
)
2103 VARITHFP(subfp
, float32_sub
)
2106 #define VARITHSAT_CASE(type, op, cvt, element) \
2108 type result = (type)a->element[i] op (type)b->element[i]; \
2109 r->element[i] = cvt(result, &sat); \
2112 #define VARITHSAT_DO(name, op, optype, cvt, element) \
2113 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2117 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2118 switch (sizeof(r->element[0])) { \
2119 case 1: VARITHSAT_CASE(optype, op, cvt, element); break; \
2120 case 2: VARITHSAT_CASE(optype, op, cvt, element); break; \
2121 case 4: VARITHSAT_CASE(optype, op, cvt, element); break; \
2125 env->vscr |= (1 << VSCR_SAT); \
2128 #define VARITHSAT_SIGNED(suffix, element, optype, cvt) \
2129 VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element) \
2130 VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
2131 #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt) \
2132 VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element) \
2133 VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
2134 VARITHSAT_SIGNED(b
, s8
, int16_t, cvtshsb
)
2135 VARITHSAT_SIGNED(h
, s16
, int32_t, cvtswsh
)
2136 VARITHSAT_SIGNED(w
, s32
, int64_t, cvtsdsw
)
2137 VARITHSAT_UNSIGNED(b
, u8
, uint16_t, cvtshub
)
2138 VARITHSAT_UNSIGNED(h
, u16
, uint32_t, cvtswuh
)
2139 VARITHSAT_UNSIGNED(w
, u32
, uint64_t, cvtsduw
)
2140 #undef VARITHSAT_CASE
2142 #undef VARITHSAT_SIGNED
2143 #undef VARITHSAT_UNSIGNED
2145 #define VAVG_DO(name, element, etype) \
2146 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2149 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2150 etype x = (etype)a->element[i] + (etype)b->element[i] + 1; \
2151 r->element[i] = x >> 1; \
2155 #define VAVG(type, signed_element, signed_type, unsigned_element, unsigned_type) \
2156 VAVG_DO(avgs##type, signed_element, signed_type) \
2157 VAVG_DO(avgu##type, unsigned_element, unsigned_type)
2158 VAVG(b
, s8
, int16_t, u8
, uint16_t)
2159 VAVG(h
, s16
, int32_t, u16
, uint32_t)
2160 VAVG(w
, s32
, int64_t, u32
, uint64_t)
2164 #define VCF(suffix, cvt, element) \
2165 void helper_vcf##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t uim) \
2168 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2169 float32 t = cvt(b->element[i], &env->vec_status); \
2170 r->f[i] = float32_scalbn (t, -uim, &env->vec_status); \
2173 VCF(ux
, uint32_to_float32
, u32
)
2174 VCF(sx
, int32_to_float32
, s32
)
2177 #define VCMP_DO(suffix, compare, element, record) \
2178 void helper_vcmp##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2180 uint32_t ones = (uint32_t)-1; \
2181 uint32_t all = ones; \
2182 uint32_t none = 0; \
2184 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2185 uint32_t result = (a->element[i] compare b->element[i] ? ones : 0x0); \
2186 switch (sizeof (a->element[0])) { \
2187 case 4: r->u32[i] = result; break; \
2188 case 2: r->u16[i] = result; break; \
2189 case 1: r->u8[i] = result; break; \
2195 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
2198 #define VCMP(suffix, compare, element) \
2199 VCMP_DO(suffix, compare, element, 0) \
2200 VCMP_DO(suffix##_dot, compare, element, 1)
2213 #define VCMPFP_DO(suffix, compare, order, record) \
2214 void helper_vcmp##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2216 uint32_t ones = (uint32_t)-1; \
2217 uint32_t all = ones; \
2218 uint32_t none = 0; \
2220 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2222 int rel = float32_compare_quiet(a->f[i], b->f[i], &env->vec_status); \
2223 if (rel == float_relation_unordered) { \
2225 } else if (rel compare order) { \
2230 r->u32[i] = result; \
2235 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
2238 #define VCMPFP(suffix, compare, order) \
2239 VCMPFP_DO(suffix, compare, order, 0) \
2240 VCMPFP_DO(suffix##_dot, compare, order, 1)
2241 VCMPFP(eqfp
, ==, float_relation_equal
)
2242 VCMPFP(gefp
, !=, float_relation_less
)
2243 VCMPFP(gtfp
, ==, float_relation_greater
)
2247 static always_inline
void vcmpbfp_internal (ppc_avr_t
*r
, ppc_avr_t
*a
,
2248 ppc_avr_t
*b
, int record
)
2252 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2253 int le_rel
= float32_compare_quiet(a
->f
[i
], b
->f
[i
], &env
->vec_status
);
2254 if (le_rel
== float_relation_unordered
) {
2255 r
->u32
[i
] = 0xc0000000;
2256 /* ALL_IN does not need to be updated here. */
2258 float32 bneg
= float32_chs(b
->f
[i
]);
2259 int ge_rel
= float32_compare_quiet(a
->f
[i
], bneg
, &env
->vec_status
);
2260 int le
= le_rel
!= float_relation_greater
;
2261 int ge
= ge_rel
!= float_relation_less
;
2262 r
->u32
[i
] = ((!le
) << 31) | ((!ge
) << 30);
2263 all_in
|= (!le
| !ge
);
2267 env
->crf
[6] = (all_in
== 0) << 1;
2271 void helper_vcmpbfp (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2273 vcmpbfp_internal(r
, a
, b
, 0);
2276 void helper_vcmpbfp_dot (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2278 vcmpbfp_internal(r
, a
, b
, 1);
2281 #define VCT(suffix, satcvt, element) \
2282 void helper_vct##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t uim) \
2286 float_status s = env->vec_status; \
2287 set_float_rounding_mode(float_round_to_zero, &s); \
2288 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2289 if (float32_is_nan(b->f[i]) || \
2290 float32_is_signaling_nan(b->f[i])) { \
2291 r->element[i] = 0; \
2293 float64 t = float32_to_float64(b->f[i], &s); \
2295 t = float64_scalbn(t, uim, &s); \
2296 j = float64_to_int64(t, &s); \
2297 r->element[i] = satcvt(j, &sat); \
2301 env->vscr |= (1 << VSCR_SAT); \
2304 VCT(uxs
, cvtsduw
, u32
)
2305 VCT(sxs
, cvtsdsw
, s32
)
2308 void helper_vmaddfp (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2311 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2312 HANDLE_NAN3(r
->f
[i
], a
->f
[i
], b
->f
[i
], c
->f
[i
]) {
2313 /* Need to do the computation in higher precision and round
2314 * once at the end. */
2315 float64 af
, bf
, cf
, t
;
2316 af
= float32_to_float64(a
->f
[i
], &env
->vec_status
);
2317 bf
= float32_to_float64(b
->f
[i
], &env
->vec_status
);
2318 cf
= float32_to_float64(c
->f
[i
], &env
->vec_status
);
2319 t
= float64_mul(af
, cf
, &env
->vec_status
);
2320 t
= float64_add(t
, bf
, &env
->vec_status
);
2321 r
->f
[i
] = float64_to_float32(t
, &env
->vec_status
);
2326 void helper_vmhaddshs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2331 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2332 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
2333 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
2334 r
->s16
[i
] = cvtswsh (t
, &sat
);
2338 env
->vscr
|= (1 << VSCR_SAT
);
2342 void helper_vmhraddshs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2347 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2348 int32_t prod
= a
->s16
[i
] * b
->s16
[i
] + 0x00004000;
2349 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
2350 r
->s16
[i
] = cvtswsh (t
, &sat
);
2354 env
->vscr
|= (1 << VSCR_SAT
);
2358 #define VMINMAX_DO(name, compare, element) \
2359 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2362 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2363 if (a->element[i] compare b->element[i]) { \
2364 r->element[i] = b->element[i]; \
2366 r->element[i] = a->element[i]; \
2370 #define VMINMAX(suffix, element) \
2371 VMINMAX_DO(min##suffix, >, element) \
2372 VMINMAX_DO(max##suffix, <, element)
2382 #define VMINMAXFP(suffix, rT, rF) \
2383 void helper_v##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2386 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2387 HANDLE_NAN2(r->f[i], a->f[i], b->f[i]) { \
2388 if (float32_lt_quiet(a->f[i], b->f[i], &env->vec_status)) { \
2389 r->f[i] = rT->f[i]; \
2391 r->f[i] = rF->f[i]; \
2396 VMINMAXFP(minfp
, a
, b
)
2397 VMINMAXFP(maxfp
, b
, a
)
2400 void helper_vmladduhm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2403 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2404 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
2405 r
->s16
[i
] = (int16_t) (prod
+ c
->s16
[i
]);
2409 #define VMRG_DO(name, element, highp) \
2410 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2414 size_t n_elems = ARRAY_SIZE(r->element); \
2415 for (i = 0; i < n_elems/2; i++) { \
2417 result.element[i*2+HI_IDX] = a->element[i]; \
2418 result.element[i*2+LO_IDX] = b->element[i]; \
2420 result.element[n_elems - i*2 - (1+HI_IDX)] = b->element[n_elems - i - 1]; \
2421 result.element[n_elems - i*2 - (1+LO_IDX)] = a->element[n_elems - i - 1]; \
2426 #if defined(WORDS_BIGENDIAN)
2433 #define VMRG(suffix, element) \
2434 VMRG_DO(mrgl##suffix, element, MRGHI) \
2435 VMRG_DO(mrgh##suffix, element, MRGLO)
2444 void helper_vmsummbm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2449 for (i
= 0; i
< ARRAY_SIZE(r
->s8
); i
++) {
2450 prod
[i
] = (int32_t)a
->s8
[i
] * b
->u8
[i
];
2453 VECTOR_FOR_INORDER_I(i
, s32
) {
2454 r
->s32
[i
] = c
->s32
[i
] + prod
[4*i
] + prod
[4*i
+1] + prod
[4*i
+2] + prod
[4*i
+3];
2458 void helper_vmsumshm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2463 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2464 prod
[i
] = a
->s16
[i
] * b
->s16
[i
];
2467 VECTOR_FOR_INORDER_I(i
, s32
) {
2468 r
->s32
[i
] = c
->s32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2472 void helper_vmsumshs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2478 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
2479 prod
[i
] = (int32_t)a
->s16
[i
] * b
->s16
[i
];
2482 VECTOR_FOR_INORDER_I (i
, s32
) {
2483 int64_t t
= (int64_t)c
->s32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2484 r
->u32
[i
] = cvtsdsw(t
, &sat
);
2488 env
->vscr
|= (1 << VSCR_SAT
);
2492 void helper_vmsumubm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2497 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2498 prod
[i
] = a
->u8
[i
] * b
->u8
[i
];
2501 VECTOR_FOR_INORDER_I(i
, u32
) {
2502 r
->u32
[i
] = c
->u32
[i
] + prod
[4*i
] + prod
[4*i
+1] + prod
[4*i
+2] + prod
[4*i
+3];
2506 void helper_vmsumuhm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2511 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
2512 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
2515 VECTOR_FOR_INORDER_I(i
, u32
) {
2516 r
->u32
[i
] = c
->u32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2520 void helper_vmsumuhs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2526 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
2527 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
2530 VECTOR_FOR_INORDER_I (i
, s32
) {
2531 uint64_t t
= (uint64_t)c
->u32
[i
] + prod
[2*i
] + prod
[2*i
+1];
2532 r
->u32
[i
] = cvtuduw(t
, &sat
);
2536 env
->vscr
|= (1 << VSCR_SAT
);
2540 #define VMUL_DO(name, mul_element, prod_element, evenp) \
2541 void helper_v##name (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2544 VECTOR_FOR_INORDER_I(i, prod_element) { \
2546 r->prod_element[i] = a->mul_element[i*2+HI_IDX] * b->mul_element[i*2+HI_IDX]; \
2548 r->prod_element[i] = a->mul_element[i*2+LO_IDX] * b->mul_element[i*2+LO_IDX]; \
2552 #define VMUL(suffix, mul_element, prod_element) \
2553 VMUL_DO(mule##suffix, mul_element, prod_element, 1) \
2554 VMUL_DO(mulo##suffix, mul_element, prod_element, 0)
2562 void helper_vnmsubfp (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2565 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2566 HANDLE_NAN3(r
->f
[i
], a
->f
[i
], b
->f
[i
], c
->f
[i
]) {
2567 /* Need to do the computation is higher precision and round
2568 * once at the end. */
2569 float64 af
, bf
, cf
, t
;
2570 af
= float32_to_float64(a
->f
[i
], &env
->vec_status
);
2571 bf
= float32_to_float64(b
->f
[i
], &env
->vec_status
);
2572 cf
= float32_to_float64(c
->f
[i
], &env
->vec_status
);
2573 t
= float64_mul(af
, cf
, &env
->vec_status
);
2574 t
= float64_sub(t
, bf
, &env
->vec_status
);
2576 r
->f
[i
] = float64_to_float32(t
, &env
->vec_status
);
2581 void helper_vperm (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2585 VECTOR_FOR_INORDER_I (i
, u8
) {
2586 int s
= c
->u8
[i
] & 0x1f;
2587 #if defined(WORDS_BIGENDIAN)
2588 int index
= s
& 0xf;
2590 int index
= 15 - (s
& 0xf);
2593 result
.u8
[i
] = b
->u8
[index
];
2595 result
.u8
[i
] = a
->u8
[index
];
2601 #if defined(WORDS_BIGENDIAN)
2606 void helper_vpkpx (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2610 #if defined(WORDS_BIGENDIAN)
2611 const ppc_avr_t
*x
[2] = { a
, b
};
2613 const ppc_avr_t
*x
[2] = { b
, a
};
2616 VECTOR_FOR_INORDER_I (i
, u64
) {
2617 VECTOR_FOR_INORDER_I (j
, u32
){
2618 uint32_t e
= x
[i
]->u32
[j
];
2619 result
.u16
[4*i
+j
] = (((e
>> 9) & 0xfc00) |
2620 ((e
>> 6) & 0x3e0) |
2627 #define VPK(suffix, from, to, cvt, dosat) \
2628 void helper_vpk##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2633 ppc_avr_t *a0 = PKBIG ? a : b; \
2634 ppc_avr_t *a1 = PKBIG ? b : a; \
2635 VECTOR_FOR_INORDER_I (i, from) { \
2636 result.to[i] = cvt(a0->from[i], &sat); \
2637 result.to[i+ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat); \
2640 if (dosat && sat) { \
2641 env->vscr |= (1 << VSCR_SAT); \
2645 VPK(shss
, s16
, s8
, cvtshsb
, 1)
2646 VPK(shus
, s16
, u8
, cvtshub
, 1)
2647 VPK(swss
, s32
, s16
, cvtswsh
, 1)
2648 VPK(swus
, s32
, u16
, cvtswuh
, 1)
2649 VPK(uhus
, u16
, u8
, cvtuhub
, 1)
2650 VPK(uwus
, u32
, u16
, cvtuwuh
, 1)
2651 VPK(uhum
, u16
, u8
, I
, 0)
2652 VPK(uwum
, u32
, u16
, I
, 0)
2657 void helper_vrefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2660 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2661 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2662 r
->f
[i
] = float32_div(float32_one
, b
->f
[i
], &env
->vec_status
);
2667 #define VRFI(suffix, rounding) \
2668 void helper_vrfi##suffix (ppc_avr_t *r, ppc_avr_t *b) \
2671 float_status s = env->vec_status; \
2672 set_float_rounding_mode(rounding, &s); \
2673 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
2674 HANDLE_NAN1(r->f[i], b->f[i]) { \
2675 r->f[i] = float32_round_to_int (b->f[i], &s); \
2679 VRFI(n
, float_round_nearest_even
)
2680 VRFI(m
, float_round_down
)
2681 VRFI(p
, float_round_up
)
2682 VRFI(z
, float_round_to_zero
)
2685 #define VROTATE(suffix, element) \
2686 void helper_vrl##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2689 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2690 unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2691 unsigned int shift = b->element[i] & mask; \
2692 r->element[i] = (a->element[i] << shift) | (a->element[i] >> (sizeof(a->element[0]) * 8 - shift)); \
2700 void helper_vrsqrtefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2703 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2704 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2705 float32 t
= float32_sqrt(b
->f
[i
], &env
->vec_status
);
2706 r
->f
[i
] = float32_div(float32_one
, t
, &env
->vec_status
);
2711 void helper_vsel (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2713 r
->u64
[0] = (a
->u64
[0] & ~c
->u64
[0]) | (b
->u64
[0] & c
->u64
[0]);
2714 r
->u64
[1] = (a
->u64
[1] & ~c
->u64
[1]) | (b
->u64
[1] & c
->u64
[1]);
2717 void helper_vlogefp (ppc_avr_t
*r
, ppc_avr_t
*b
)
2720 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
2721 HANDLE_NAN1(r
->f
[i
], b
->f
[i
]) {
2722 r
->f
[i
] = float32_log2(b
->f
[i
], &env
->vec_status
);
2727 #if defined(WORDS_BIGENDIAN)
2734 /* The specification says that the results are undefined if all of the
2735 * shift counts are not identical. We check to make sure that they are
2736 * to conform to what real hardware appears to do. */
2737 #define VSHIFT(suffix, leftp) \
2738 void helper_vs##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2740 int shift = b->u8[LO_IDX*15] & 0x7; \
2743 for (i = 0; i < ARRAY_SIZE(r->u8); i++) { \
2744 doit = doit && ((b->u8[i] & 0x7) == shift); \
2749 } else if (leftp) { \
2750 uint64_t carry = a->u64[LO_IDX] >> (64 - shift); \
2751 r->u64[HI_IDX] = (a->u64[HI_IDX] << shift) | carry; \
2752 r->u64[LO_IDX] = a->u64[LO_IDX] << shift; \
2754 uint64_t carry = a->u64[HI_IDX] << (64 - shift); \
2755 r->u64[LO_IDX] = (a->u64[LO_IDX] >> shift) | carry; \
2756 r->u64[HI_IDX] = a->u64[HI_IDX] >> shift; \
2766 #define VSL(suffix, element) \
2767 void helper_vsl##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2770 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2771 unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2772 unsigned int shift = b->element[i] & mask; \
2773 r->element[i] = a->element[i] << shift; \
2781 void helper_vsldoi (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t shift
)
2783 int sh
= shift
& 0xf;
2787 #if defined(WORDS_BIGENDIAN)
2788 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2791 result
.u8
[i
] = b
->u8
[index
-0x10];
2793 result
.u8
[i
] = a
->u8
[index
];
2797 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2798 int index
= (16 - sh
) + i
;
2800 result
.u8
[i
] = a
->u8
[index
-0x10];
2802 result
.u8
[i
] = b
->u8
[index
];
2809 void helper_vslo (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2811 int sh
= (b
->u8
[LO_IDX
*0xf] >> 3) & 0xf;
2813 #if defined (WORDS_BIGENDIAN)
2814 memmove (&r
->u8
[0], &a
->u8
[sh
], 16-sh
);
2815 memset (&r
->u8
[16-sh
], 0, sh
);
2817 memmove (&r
->u8
[sh
], &a
->u8
[0], 16-sh
);
2818 memset (&r
->u8
[0], 0, sh
);
2822 /* Experimental testing shows that hardware masks the immediate. */
2823 #define _SPLAT_MASKED(element) (splat & (ARRAY_SIZE(r->element) - 1))
2824 #if defined(WORDS_BIGENDIAN)
2825 #define SPLAT_ELEMENT(element) _SPLAT_MASKED(element)
2827 #define SPLAT_ELEMENT(element) (ARRAY_SIZE(r->element)-1 - _SPLAT_MASKED(element))
2829 #define VSPLT(suffix, element) \
2830 void helper_vsplt##suffix (ppc_avr_t *r, ppc_avr_t *b, uint32_t splat) \
2832 uint32_t s = b->element[SPLAT_ELEMENT(element)]; \
2834 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2835 r->element[i] = s; \
2842 #undef SPLAT_ELEMENT
2843 #undef _SPLAT_MASKED
2845 #define VSPLTI(suffix, element, splat_type) \
2846 void helper_vspltis##suffix (ppc_avr_t *r, uint32_t splat) \
2848 splat_type x = (int8_t)(splat << 3) >> 3; \
2850 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2851 r->element[i] = x; \
2854 VSPLTI(b
, s8
, int8_t)
2855 VSPLTI(h
, s16
, int16_t)
2856 VSPLTI(w
, s32
, int32_t)
2859 #define VSR(suffix, element) \
2860 void helper_vsr##suffix (ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
2863 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
2864 unsigned int mask = ((1 << (3 + (sizeof (a->element[0]) >> 1))) - 1); \
2865 unsigned int shift = b->element[i] & mask; \
2866 r->element[i] = a->element[i] >> shift; \
2877 void helper_vsro (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2879 int sh
= (b
->u8
[LO_IDX
*0xf] >> 3) & 0xf;
2881 #if defined (WORDS_BIGENDIAN)
2882 memmove (&r
->u8
[sh
], &a
->u8
[0], 16-sh
);
2883 memset (&r
->u8
[0], 0, sh
);
2885 memmove (&r
->u8
[0], &a
->u8
[sh
], 16-sh
);
2886 memset (&r
->u8
[16-sh
], 0, sh
);
2890 void helper_vsubcuw (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2893 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2894 r
->u32
[i
] = a
->u32
[i
] >= b
->u32
[i
];
2898 void helper_vsumsws (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2905 #if defined(WORDS_BIGENDIAN)
2906 upper
= ARRAY_SIZE(r
->s32
)-1;
2910 t
= (int64_t)b
->s32
[upper
];
2911 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
2915 result
.s32
[upper
] = cvtsdsw(t
, &sat
);
2919 env
->vscr
|= (1 << VSCR_SAT
);
2923 void helper_vsum2sws (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2929 #if defined(WORDS_BIGENDIAN)
2934 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
2935 int64_t t
= (int64_t)b
->s32
[upper
+i
*2];
2937 for (j
= 0; j
< ARRAY_SIZE(r
->u64
); j
++) {
2940 result
.s32
[upper
+i
*2] = cvtsdsw(t
, &sat
);
2945 env
->vscr
|= (1 << VSCR_SAT
);
2949 void helper_vsum4sbs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2954 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
2955 int64_t t
= (int64_t)b
->s32
[i
];
2956 for (j
= 0; j
< ARRAY_SIZE(r
->s32
); j
++) {
2959 r
->s32
[i
] = cvtsdsw(t
, &sat
);
2963 env
->vscr
|= (1 << VSCR_SAT
);
2967 void helper_vsum4shs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2972 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
2973 int64_t t
= (int64_t)b
->s32
[i
];
2974 t
+= a
->s16
[2*i
] + a
->s16
[2*i
+1];
2975 r
->s32
[i
] = cvtsdsw(t
, &sat
);
2979 env
->vscr
|= (1 << VSCR_SAT
);
2983 void helper_vsum4ubs (ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2988 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2989 uint64_t t
= (uint64_t)b
->u32
[i
];
2990 for (j
= 0; j
< ARRAY_SIZE(r
->u32
); j
++) {
2993 r
->u32
[i
] = cvtuduw(t
, &sat
);
2997 env
->vscr
|= (1 << VSCR_SAT
);
3001 #if defined(WORDS_BIGENDIAN)
3008 #define VUPKPX(suffix, hi) \
3009 void helper_vupk##suffix (ppc_avr_t *r, ppc_avr_t *b) \
3013 for (i = 0; i < ARRAY_SIZE(r->u32); i++) { \
3014 uint16_t e = b->u16[hi ? i : i+4]; \
3015 uint8_t a = (e >> 15) ? 0xff : 0; \
3016 uint8_t r = (e >> 10) & 0x1f; \
3017 uint8_t g = (e >> 5) & 0x1f; \
3018 uint8_t b = e & 0x1f; \
3019 result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b; \
3027 #define VUPK(suffix, unpacked, packee, hi) \
3028 void helper_vupk##suffix (ppc_avr_t *r, ppc_avr_t *b) \
3033 for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) { \
3034 result.unpacked[i] = b->packee[i]; \
3037 for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); i++) { \
3038 result.unpacked[i-ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
3043 VUPK(hsb
, s16
, s8
, UPKHI
)
3044 VUPK(hsh
, s32
, s16
, UPKHI
)
3045 VUPK(lsb
, s16
, s8
, UPKLO
)
3046 VUPK(lsh
, s32
, s16
, UPKLO
)
3051 #undef DO_HANDLE_NAN
3055 #undef VECTOR_FOR_INORDER_I
3059 /*****************************************************************************/
3060 /* SPE extension helpers */
3061 /* Use a table to make this quicker */
3062 static uint8_t hbrev
[16] = {
3063 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
3064 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
3067 static always_inline
uint8_t byte_reverse (uint8_t val
)
3069 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
3072 static always_inline
uint32_t word_reverse (uint32_t val
)
3074 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
3075 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
3078 #define MASKBITS 16 // Random value - to be fixed (implementation dependant)
3079 target_ulong
helper_brinc (target_ulong arg1
, target_ulong arg2
)
3081 uint32_t a
, b
, d
, mask
;
3083 mask
= UINT32_MAX
>> (32 - MASKBITS
);
3086 d
= word_reverse(1 + word_reverse(a
| ~b
));
3087 return (arg1
& ~mask
) | (d
& b
);
3090 uint32_t helper_cntlsw32 (uint32_t val
)
3092 if (val
& 0x80000000)
3098 uint32_t helper_cntlzw32 (uint32_t val
)
3103 /* Single-precision floating-point conversions */
3104 static always_inline
uint32_t efscfsi (uint32_t val
)
3108 u
.f
= int32_to_float32(val
, &env
->vec_status
);
3113 static always_inline
uint32_t efscfui (uint32_t val
)
3117 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
3122 static always_inline
int32_t efsctsi (uint32_t val
)
3127 /* NaN are not treated the same way IEEE 754 does */
3128 if (unlikely(float32_is_nan(u
.f
)))
3131 return float32_to_int32(u
.f
, &env
->vec_status
);
3134 static always_inline
uint32_t efsctui (uint32_t val
)
3139 /* NaN are not treated the same way IEEE 754 does */
3140 if (unlikely(float32_is_nan(u
.f
)))
3143 return float32_to_uint32(u
.f
, &env
->vec_status
);
3146 static always_inline
uint32_t efsctsiz (uint32_t val
)
3151 /* NaN are not treated the same way IEEE 754 does */
3152 if (unlikely(float32_is_nan(u
.f
)))
3155 return float32_to_int32_round_to_zero(u
.f
, &env
->vec_status
);
3158 static always_inline
uint32_t efsctuiz (uint32_t val
)
3163 /* NaN are not treated the same way IEEE 754 does */
3164 if (unlikely(float32_is_nan(u
.f
)))
3167 return float32_to_uint32_round_to_zero(u
.f
, &env
->vec_status
);
3170 static always_inline
uint32_t efscfsf (uint32_t val
)
3175 u
.f
= int32_to_float32(val
, &env
->vec_status
);
3176 tmp
= int64_to_float32(1ULL << 32, &env
->vec_status
);
3177 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
3182 static always_inline
uint32_t efscfuf (uint32_t val
)
3187 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
3188 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
3189 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
3194 static always_inline
uint32_t efsctsf (uint32_t val
)
3200 /* NaN are not treated the same way IEEE 754 does */
3201 if (unlikely(float32_is_nan(u
.f
)))
3203 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
3204 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
3206 return float32_to_int32(u
.f
, &env
->vec_status
);
3209 static always_inline
uint32_t efsctuf (uint32_t val
)
3215 /* NaN are not treated the same way IEEE 754 does */
3216 if (unlikely(float32_is_nan(u
.f
)))
3218 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
3219 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
3221 return float32_to_uint32(u
.f
, &env
->vec_status
);
3224 #define HELPER_SPE_SINGLE_CONV(name) \
3225 uint32_t helper_e##name (uint32_t val) \
3227 return e##name(val); \
3230 HELPER_SPE_SINGLE_CONV(fscfsi
);
3232 HELPER_SPE_SINGLE_CONV(fscfui
);
3234 HELPER_SPE_SINGLE_CONV(fscfuf
);
3236 HELPER_SPE_SINGLE_CONV(fscfsf
);
3238 HELPER_SPE_SINGLE_CONV(fsctsi
);
3240 HELPER_SPE_SINGLE_CONV(fsctui
);
3242 HELPER_SPE_SINGLE_CONV(fsctsiz
);
3244 HELPER_SPE_SINGLE_CONV(fsctuiz
);
3246 HELPER_SPE_SINGLE_CONV(fsctsf
);
3248 HELPER_SPE_SINGLE_CONV(fsctuf
);
3250 #define HELPER_SPE_VECTOR_CONV(name) \
3251 uint64_t helper_ev##name (uint64_t val) \
3253 return ((uint64_t)e##name(val >> 32) << 32) | \
3254 (uint64_t)e##name(val); \
3257 HELPER_SPE_VECTOR_CONV(fscfsi
);
3259 HELPER_SPE_VECTOR_CONV(fscfui
);
3261 HELPER_SPE_VECTOR_CONV(fscfuf
);
3263 HELPER_SPE_VECTOR_CONV(fscfsf
);
3265 HELPER_SPE_VECTOR_CONV(fsctsi
);
3267 HELPER_SPE_VECTOR_CONV(fsctui
);
3269 HELPER_SPE_VECTOR_CONV(fsctsiz
);
3271 HELPER_SPE_VECTOR_CONV(fsctuiz
);
3273 HELPER_SPE_VECTOR_CONV(fsctsf
);
3275 HELPER_SPE_VECTOR_CONV(fsctuf
);
3277 /* Single-precision floating-point arithmetic */
3278 static always_inline
uint32_t efsadd (uint32_t op1
, uint32_t op2
)
3283 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->vec_status
);
3287 static always_inline
uint32_t efssub (uint32_t op1
, uint32_t op2
)
3292 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->vec_status
);
3296 static always_inline
uint32_t efsmul (uint32_t op1
, uint32_t op2
)
3301 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->vec_status
);
3305 static always_inline
uint32_t efsdiv (uint32_t op1
, uint32_t op2
)
3310 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->vec_status
);
3314 #define HELPER_SPE_SINGLE_ARITH(name) \
3315 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
3317 return e##name(op1, op2); \
3320 HELPER_SPE_SINGLE_ARITH(fsadd
);
3322 HELPER_SPE_SINGLE_ARITH(fssub
);
3324 HELPER_SPE_SINGLE_ARITH(fsmul
);
3326 HELPER_SPE_SINGLE_ARITH(fsdiv
);
3328 #define HELPER_SPE_VECTOR_ARITH(name) \
3329 uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
3331 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
3332 (uint64_t)e##name(op1, op2); \
3335 HELPER_SPE_VECTOR_ARITH(fsadd
);
3337 HELPER_SPE_VECTOR_ARITH(fssub
);
3339 HELPER_SPE_VECTOR_ARITH(fsmul
);
3341 HELPER_SPE_VECTOR_ARITH(fsdiv
);
3343 /* Single-precision floating-point comparisons */
3344 static always_inline
uint32_t efststlt (uint32_t op1
, uint32_t op2
)
3349 return float32_lt(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
3352 static always_inline
uint32_t efststgt (uint32_t op1
, uint32_t op2
)
3357 return float32_le(u1
.f
, u2
.f
, &env
->vec_status
) ? 0 : 4;
3360 static always_inline
uint32_t efststeq (uint32_t op1
, uint32_t op2
)
3365 return float32_eq(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
3368 static always_inline
uint32_t efscmplt (uint32_t op1
, uint32_t op2
)
3370 /* XXX: TODO: test special values (NaN, infinites, ...) */
3371 return efststlt(op1
, op2
);
3374 static always_inline
uint32_t efscmpgt (uint32_t op1
, uint32_t op2
)
3376 /* XXX: TODO: test special values (NaN, infinites, ...) */
3377 return efststgt(op1
, op2
);
3380 static always_inline
uint32_t efscmpeq (uint32_t op1
, uint32_t op2
)
3382 /* XXX: TODO: test special values (NaN, infinites, ...) */
3383 return efststeq(op1
, op2
);
3386 #define HELPER_SINGLE_SPE_CMP(name) \
3387 uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
3389 return e##name(op1, op2) << 2; \
3392 HELPER_SINGLE_SPE_CMP(fststlt
);
3394 HELPER_SINGLE_SPE_CMP(fststgt
);
3396 HELPER_SINGLE_SPE_CMP(fststeq
);
3398 HELPER_SINGLE_SPE_CMP(fscmplt
);
3400 HELPER_SINGLE_SPE_CMP(fscmpgt
);
3402 HELPER_SINGLE_SPE_CMP(fscmpeq
);
3404 static always_inline
uint32_t evcmp_merge (int t0
, int t1
)
3406 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
3409 #define HELPER_VECTOR_SPE_CMP(name) \
3410 uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
3412 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
3415 HELPER_VECTOR_SPE_CMP(fststlt
);
3417 HELPER_VECTOR_SPE_CMP(fststgt
);
3419 HELPER_VECTOR_SPE_CMP(fststeq
);
3421 HELPER_VECTOR_SPE_CMP(fscmplt
);
3423 HELPER_VECTOR_SPE_CMP(fscmpgt
);
3425 HELPER_VECTOR_SPE_CMP(fscmpeq
);
3427 /* Double-precision floating-point conversion */
3428 uint64_t helper_efdcfsi (uint32_t val
)
3432 u
.d
= int32_to_float64(val
, &env
->vec_status
);
3437 uint64_t helper_efdcfsid (uint64_t val
)
3441 u
.d
= int64_to_float64(val
, &env
->vec_status
);
3446 uint64_t helper_efdcfui (uint32_t val
)
3450 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
3455 uint64_t helper_efdcfuid (uint64_t val
)
3459 u
.d
= uint64_to_float64(val
, &env
->vec_status
);
3464 uint32_t helper_efdctsi (uint64_t val
)
3469 /* NaN are not treated the same way IEEE 754 does */
3470 if (unlikely(float64_is_nan(u
.d
)))
3473 return float64_to_int32(u
.d
, &env
->vec_status
);
3476 uint32_t helper_efdctui (uint64_t val
)
3481 /* NaN are not treated the same way IEEE 754 does */
3482 if (unlikely(float64_is_nan(u
.d
)))
3485 return float64_to_uint32(u
.d
, &env
->vec_status
);
3488 uint32_t helper_efdctsiz (uint64_t val
)
3493 /* NaN are not treated the same way IEEE 754 does */
3494 if (unlikely(float64_is_nan(u
.d
)))
3497 return float64_to_int32_round_to_zero(u
.d
, &env
->vec_status
);
3500 uint64_t helper_efdctsidz (uint64_t val
)
3505 /* NaN are not treated the same way IEEE 754 does */
3506 if (unlikely(float64_is_nan(u
.d
)))
3509 return float64_to_int64_round_to_zero(u
.d
, &env
->vec_status
);
3512 uint32_t helper_efdctuiz (uint64_t val
)
3517 /* NaN are not treated the same way IEEE 754 does */
3518 if (unlikely(float64_is_nan(u
.d
)))
3521 return float64_to_uint32_round_to_zero(u
.d
, &env
->vec_status
);
3524 uint64_t helper_efdctuidz (uint64_t val
)
3529 /* NaN are not treated the same way IEEE 754 does */
3530 if (unlikely(float64_is_nan(u
.d
)))
3533 return float64_to_uint64_round_to_zero(u
.d
, &env
->vec_status
);
3536 uint64_t helper_efdcfsf (uint32_t val
)
3541 u
.d
= int32_to_float64(val
, &env
->vec_status
);
3542 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
3543 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
3548 uint64_t helper_efdcfuf (uint32_t val
)
3553 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
3554 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
3555 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
3560 uint32_t helper_efdctsf (uint64_t val
)
3566 /* NaN are not treated the same way IEEE 754 does */
3567 if (unlikely(float64_is_nan(u
.d
)))
3569 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
3570 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
3572 return float64_to_int32(u
.d
, &env
->vec_status
);
3575 uint32_t helper_efdctuf (uint64_t val
)
3581 /* NaN are not treated the same way IEEE 754 does */
3582 if (unlikely(float64_is_nan(u
.d
)))
3584 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
3585 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
3587 return float64_to_uint32(u
.d
, &env
->vec_status
);
3590 uint32_t helper_efscfd (uint64_t val
)
3596 u2
.f
= float64_to_float32(u1
.d
, &env
->vec_status
);
3601 uint64_t helper_efdcfs (uint32_t val
)
3607 u2
.d
= float32_to_float64(u1
.f
, &env
->vec_status
);
3612 /* Double precision fixed-point arithmetic */
3613 uint64_t helper_efdadd (uint64_t op1
, uint64_t op2
)
3618 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->vec_status
);
3622 uint64_t helper_efdsub (uint64_t op1
, uint64_t op2
)
3627 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->vec_status
);
3631 uint64_t helper_efdmul (uint64_t op1
, uint64_t op2
)
3636 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->vec_status
);
3640 uint64_t helper_efddiv (uint64_t op1
, uint64_t op2
)
3645 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->vec_status
);
3649 /* Double precision floating point helpers */
3650 uint32_t helper_efdtstlt (uint64_t op1
, uint64_t op2
)
3655 return float64_lt(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
3658 uint32_t helper_efdtstgt (uint64_t op1
, uint64_t op2
)
3663 return float64_le(u1
.d
, u2
.d
, &env
->vec_status
) ? 0 : 4;
3666 uint32_t helper_efdtsteq (uint64_t op1
, uint64_t op2
)
3671 return float64_eq(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
3674 uint32_t helper_efdcmplt (uint64_t op1
, uint64_t op2
)
3676 /* XXX: TODO: test special values (NaN, infinites, ...) */
3677 return helper_efdtstlt(op1
, op2
);
3680 uint32_t helper_efdcmpgt (uint64_t op1
, uint64_t op2
)
3682 /* XXX: TODO: test special values (NaN, infinites, ...) */
3683 return helper_efdtstgt(op1
, op2
);
3686 uint32_t helper_efdcmpeq (uint64_t op1
, uint64_t op2
)
3688 /* XXX: TODO: test special values (NaN, infinites, ...) */
3689 return helper_efdtsteq(op1
, op2
);
3692 /*****************************************************************************/
3693 /* Softmmu support */
3694 #if !defined (CONFIG_USER_ONLY)
3696 #define MMUSUFFIX _mmu
3699 #include "softmmu_template.h"
3702 #include "softmmu_template.h"
3705 #include "softmmu_template.h"
3708 #include "softmmu_template.h"
3710 /* try to fill the TLB and return an exception if error. If retaddr is
3711 NULL, it means that the function was called in C code (i.e. not
3712 from generated code or from helper.c) */
3713 /* XXX: fix it to restore all registers */
3714 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
3716 TranslationBlock
*tb
;
3717 CPUState
*saved_env
;
3721 /* XXX: hack to restore env in all cases, even if not called from
3724 env
= cpu_single_env
;
3725 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
3726 if (unlikely(ret
!= 0)) {
3727 if (likely(retaddr
)) {
3728 /* now we have a real cpu fault */
3729 pc
= (unsigned long)retaddr
;
3730 tb
= tb_find_pc(pc
);
3732 /* the PC is inside the translated code. It means that we have
3733 a virtual CPU fault */
3734 cpu_restore_state(tb
, env
, pc
, NULL
);
3737 helper_raise_exception_err(env
->exception_index
, env
->error_code
);
3742 /* Segment registers load and store */
3743 target_ulong
helper_load_sr (target_ulong sr_num
)
3745 #if defined(TARGET_PPC64)
3746 if (env
->mmu_model
& POWERPC_MMU_64
)
3747 return ppc_load_sr(env
, sr_num
);
3749 return env
->sr
[sr_num
];
3752 void helper_store_sr (target_ulong sr_num
, target_ulong val
)
3754 ppc_store_sr(env
, sr_num
, val
);
3757 /* SLB management */
3758 #if defined(TARGET_PPC64)
3759 target_ulong
helper_load_slb (target_ulong slb_nr
)
3761 return ppc_load_slb(env
, slb_nr
);
3764 void helper_store_slb (target_ulong rb
, target_ulong rs
)
3766 ppc_store_slb(env
, rb
, rs
);
3769 void helper_slbia (void)
3771 ppc_slb_invalidate_all(env
);
3774 void helper_slbie (target_ulong addr
)
3776 ppc_slb_invalidate_one(env
, addr
);
3779 #endif /* defined(TARGET_PPC64) */
3781 /* TLB management */
3782 void helper_tlbia (void)
3784 ppc_tlb_invalidate_all(env
);
3787 void helper_tlbie (target_ulong addr
)
3789 ppc_tlb_invalidate_one(env
, addr
);
3792 /* Software driven TLBs management */
3793 /* PowerPC 602/603 software TLB load instructions helpers */
3794 static void do_6xx_tlb (target_ulong new_EPN
, int is_code
)
3796 target_ulong RPN
, CMP
, EPN
;
3799 RPN
= env
->spr
[SPR_RPA
];
3801 CMP
= env
->spr
[SPR_ICMP
];
3802 EPN
= env
->spr
[SPR_IMISS
];
3804 CMP
= env
->spr
[SPR_DCMP
];
3805 EPN
= env
->spr
[SPR_DMISS
];
3807 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
3808 LOG_SWTLB("%s: EPN " ADDRX
" " ADDRX
" PTE0 " ADDRX
3809 " PTE1 " ADDRX
" way %d\n",
3810 __func__
, new_EPN
, EPN
, CMP
, RPN
, way
);
3811 /* Store this TLB */
3812 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
3813 way
, is_code
, CMP
, RPN
);
3816 void helper_6xx_tlbd (target_ulong EPN
)
3821 void helper_6xx_tlbi (target_ulong EPN
)
3826 /* PowerPC 74xx software TLB load instructions helpers */
3827 static void do_74xx_tlb (target_ulong new_EPN
, int is_code
)
3829 target_ulong RPN
, CMP
, EPN
;
3832 RPN
= env
->spr
[SPR_PTELO
];
3833 CMP
= env
->spr
[SPR_PTEHI
];
3834 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
3835 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
3836 LOG_SWTLB("%s: EPN " ADDRX
" " ADDRX
" PTE0 " ADDRX
3837 " PTE1 " ADDRX
" way %d\n",
3838 __func__
, new_EPN
, EPN
, CMP
, RPN
, way
);
3839 /* Store this TLB */
3840 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
3841 way
, is_code
, CMP
, RPN
);
3844 void helper_74xx_tlbd (target_ulong EPN
)
3846 do_74xx_tlb(EPN
, 0);
3849 void helper_74xx_tlbi (target_ulong EPN
)
3851 do_74xx_tlb(EPN
, 1);
3854 static always_inline target_ulong
booke_tlb_to_page_size (int size
)
3856 return 1024 << (2 * size
);
3859 static always_inline
int booke_page_size_to_tlb (target_ulong page_size
)
3863 switch (page_size
) {
3897 #if defined (TARGET_PPC64)
3898 case 0x000100000000ULL
:
3901 case 0x000400000000ULL
:
3904 case 0x001000000000ULL
:
3907 case 0x004000000000ULL
:
3910 case 0x010000000000ULL
:
3922 /* Helpers for 4xx TLB management */
3923 target_ulong
helper_4xx_tlbre_lo (target_ulong entry
)
3930 tlb
= &env
->tlb
[entry
].tlbe
;
3932 if (tlb
->prot
& PAGE_VALID
)
3934 size
= booke_page_size_to_tlb(tlb
->size
);
3935 if (size
< 0 || size
> 0x7)
3938 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
3942 target_ulong
helper_4xx_tlbre_hi (target_ulong entry
)
3948 tlb
= &env
->tlb
[entry
].tlbe
;
3950 if (tlb
->prot
& PAGE_EXEC
)
3952 if (tlb
->prot
& PAGE_WRITE
)
3957 void helper_4xx_tlbwe_hi (target_ulong entry
, target_ulong val
)
3960 target_ulong page
, end
;
3962 LOG_SWTLB("%s entry %d val " ADDRX
"\n", __func__
, (int)entry
, val
);
3964 tlb
= &env
->tlb
[entry
].tlbe
;
3965 /* Invalidate previous TLB (if it's valid) */
3966 if (tlb
->prot
& PAGE_VALID
) {
3967 end
= tlb
->EPN
+ tlb
->size
;
3968 LOG_SWTLB("%s: invalidate old TLB %d start " ADDRX
3969 " end " ADDRX
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
3970 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
3971 tlb_flush_page(env
, page
);
3973 tlb
->size
= booke_tlb_to_page_size((val
>> 7) & 0x7);
3974 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
3975 * If this ever occurs, one should use the ppcemb target instead
3976 * of the ppc or ppc64 one
3978 if ((val
& 0x40) && tlb
->size
< TARGET_PAGE_SIZE
) {
3979 cpu_abort(env
, "TLB size " TARGET_FMT_lu
" < %u "
3980 "are not supported (%d)\n",
3981 tlb
->size
, TARGET_PAGE_SIZE
, (int)((val
>> 7) & 0x7));
3983 tlb
->EPN
= val
& ~(tlb
->size
- 1);
3985 tlb
->prot
|= PAGE_VALID
;
3987 tlb
->prot
&= ~PAGE_VALID
;
3989 /* XXX: TO BE FIXED */
3990 cpu_abort(env
, "Little-endian TLB entries are not supported by now\n");
3992 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
3993 tlb
->attr
= val
& 0xFF;
3994 LOG_SWTLB("%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
3995 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
3996 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
3997 tlb
->prot
& PAGE_READ
? 'r' : '-',
3998 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
3999 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
4000 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
4001 /* Invalidate new TLB (if valid) */
4002 if (tlb
->prot
& PAGE_VALID
) {
4003 end
= tlb
->EPN
+ tlb
->size
;
4004 LOG_SWTLB("%s: invalidate TLB %d start " ADDRX
4005 " end " ADDRX
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
4006 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
4007 tlb_flush_page(env
, page
);
4011 void helper_4xx_tlbwe_lo (target_ulong entry
, target_ulong val
)
4015 LOG_SWTLB("%s entry %i val " ADDRX
"\n", __func__
, (int)entry
, val
);
4017 tlb
= &env
->tlb
[entry
].tlbe
;
4018 tlb
->RPN
= val
& 0xFFFFFC00;
4019 tlb
->prot
= PAGE_READ
;
4021 tlb
->prot
|= PAGE_EXEC
;
4023 tlb
->prot
|= PAGE_WRITE
;
4024 LOG_SWTLB("%s: set up TLB %d RPN " PADDRX
" EPN " ADDRX
4025 " size " ADDRX
" prot %c%c%c%c PID %d\n", __func__
,
4026 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
4027 tlb
->prot
& PAGE_READ
? 'r' : '-',
4028 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
4029 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
4030 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
4033 target_ulong
helper_4xx_tlbsx (target_ulong address
)
4035 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_40x_PID
]);
4038 /* PowerPC 440 TLB management */
4039 void helper_440_tlbwe (uint32_t word
, target_ulong entry
, target_ulong value
)
4042 target_ulong EPN
, RPN
, size
;
4045 LOG_SWTLB("%s word %d entry %d value " ADDRX
"\n",
4046 __func__
, word
, (int)entry
, value
);
4049 tlb
= &env
->tlb
[entry
].tlbe
;
4052 /* Just here to please gcc */
4054 EPN
= value
& 0xFFFFFC00;
4055 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
)
4058 size
= booke_tlb_to_page_size((value
>> 4) & 0xF);
4059 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
)
4063 tlb
->attr
|= (value
>> 8) & 1;
4064 if (value
& 0x200) {
4065 tlb
->prot
|= PAGE_VALID
;
4067 if (tlb
->prot
& PAGE_VALID
) {
4068 tlb
->prot
&= ~PAGE_VALID
;
4072 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
4077 RPN
= value
& 0xFFFFFC0F;
4078 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
)
4083 tlb
->attr
= (tlb
->attr
& 0x1) | (value
& 0x0000FF00);
4084 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
4086 tlb
->prot
|= PAGE_READ
<< 4;
4088 tlb
->prot
|= PAGE_WRITE
<< 4;
4090 tlb
->prot
|= PAGE_EXEC
<< 4;
4092 tlb
->prot
|= PAGE_READ
;
4094 tlb
->prot
|= PAGE_WRITE
;
4096 tlb
->prot
|= PAGE_EXEC
;
4101 target_ulong
helper_440_tlbre (uint32_t word
, target_ulong entry
)
4108 tlb
= &env
->tlb
[entry
].tlbe
;
4111 /* Just here to please gcc */
4114 size
= booke_page_size_to_tlb(tlb
->size
);
4115 if (size
< 0 || size
> 0xF)
4118 if (tlb
->attr
& 0x1)
4120 if (tlb
->prot
& PAGE_VALID
)
4122 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
4123 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
4129 ret
= tlb
->attr
& ~0x1;
4130 if (tlb
->prot
& (PAGE_READ
<< 4))
4132 if (tlb
->prot
& (PAGE_WRITE
<< 4))
4134 if (tlb
->prot
& (PAGE_EXEC
<< 4))
4136 if (tlb
->prot
& PAGE_READ
)
4138 if (tlb
->prot
& PAGE_WRITE
)
4140 if (tlb
->prot
& PAGE_EXEC
)
4147 target_ulong
helper_440_tlbsx (target_ulong address
)
4149 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_440_MMUCR
] & 0xFF);
4152 #endif /* !CONFIG_USER_ONLY */