2 * PowerPC floating point and SPE 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, see <http://www.gnu.org/licenses/>.
20 #include "exec/helper-proto.h"
22 #define float64_snan_to_qnan(x) ((x) | 0x0008000000000000ULL)
23 #define float32_snan_to_qnan(x) ((x) | 0x00400000)
25 /*****************************************************************************/
26 /* Floating point operations helpers */
27 uint64_t helper_float32_to_float64(CPUPPCState
*env
, uint32_t arg
)
33 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
37 uint32_t helper_float64_to_float32(CPUPPCState
*env
, uint64_t arg
)
43 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
47 static inline int isden(float64 d
)
53 return ((u
.ll
>> 52) & 0x7FF) == 0;
56 static inline int ppc_float32_get_unbiased_exp(float32 f
)
58 return ((f
>> 23) & 0xFF) - 127;
61 static inline int ppc_float64_get_unbiased_exp(float64 f
)
63 return ((f
>> 52) & 0x7FF) - 1023;
66 void helper_compute_fprf(CPUPPCState
*env
, uint64_t arg
)
73 isneg
= float64_is_neg(farg
.d
);
74 if (unlikely(float64_is_any_nan(farg
.d
))) {
75 if (float64_is_signaling_nan(farg
.d
)) {
76 /* Signaling NaN: flags are undefined */
82 } else if (unlikely(float64_is_infinity(farg
.d
))) {
90 if (float64_is_zero(farg
.d
)) {
99 /* Denormalized numbers */
102 /* Normalized numbers */
112 /* We update FPSCR_FPRF */
113 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
114 env
->fpscr
|= fprf
<< FPSCR_FPRF
;
117 /* Floating-point invalid operations exception */
118 static inline uint64_t fload_invalid_op_excp(CPUPPCState
*env
, int op
,
121 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
127 case POWERPC_EXCP_FP_VXSNAN
:
128 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
130 case POWERPC_EXCP_FP_VXSOFT
:
131 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
133 case POWERPC_EXCP_FP_VXISI
:
134 /* Magnitude subtraction of infinities */
135 env
->fpscr
|= 1 << FPSCR_VXISI
;
137 case POWERPC_EXCP_FP_VXIDI
:
138 /* Division of infinity by infinity */
139 env
->fpscr
|= 1 << FPSCR_VXIDI
;
141 case POWERPC_EXCP_FP_VXZDZ
:
142 /* Division of zero by zero */
143 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
145 case POWERPC_EXCP_FP_VXIMZ
:
146 /* Multiplication of zero by infinity */
147 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
149 case POWERPC_EXCP_FP_VXVC
:
150 /* Ordered comparison of NaN */
151 env
->fpscr
|= 1 << FPSCR_VXVC
;
153 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
154 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
156 /* We must update the target FPR before raising the exception */
158 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
159 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
160 /* Update the floating-point enabled exception summary */
161 env
->fpscr
|= 1 << FPSCR_FEX
;
162 /* Exception is differed */
166 case POWERPC_EXCP_FP_VXSQRT
:
167 /* Square root of a negative number */
168 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
170 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
172 /* Set the result to quiet NaN */
173 ret
= 0x7FF8000000000000ULL
;
175 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
176 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
180 case POWERPC_EXCP_FP_VXCVI
:
181 /* Invalid conversion */
182 env
->fpscr
|= 1 << FPSCR_VXCVI
;
183 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
185 /* Set the result to quiet NaN */
186 ret
= 0x7FF8000000000000ULL
;
188 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
189 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
194 /* Update the floating-point invalid operation summary */
195 env
->fpscr
|= 1 << FPSCR_VX
;
196 /* Update the floating-point exception summary */
199 /* Update the floating-point enabled exception summary */
200 env
->fpscr
|= 1 << FPSCR_FEX
;
201 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
202 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
203 POWERPC_EXCP_FP
| op
);
209 static inline void float_zero_divide_excp(CPUPPCState
*env
)
211 env
->fpscr
|= 1 << FPSCR_ZX
;
212 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
213 /* Update the floating-point exception summary */
216 /* Update the floating-point enabled exception summary */
217 env
->fpscr
|= 1 << FPSCR_FEX
;
218 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
219 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
220 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
225 static inline void float_overflow_excp(CPUPPCState
*env
)
227 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
229 env
->fpscr
|= 1 << FPSCR_OX
;
230 /* Update the floating-point exception summary */
233 /* XXX: should adjust the result */
234 /* Update the floating-point enabled exception summary */
235 env
->fpscr
|= 1 << FPSCR_FEX
;
236 /* We must update the target FPR before raising the exception */
237 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
238 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
240 env
->fpscr
|= 1 << FPSCR_XX
;
241 env
->fpscr
|= 1 << FPSCR_FI
;
245 static inline void float_underflow_excp(CPUPPCState
*env
)
247 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
249 env
->fpscr
|= 1 << FPSCR_UX
;
250 /* Update the floating-point exception summary */
253 /* XXX: should adjust the result */
254 /* Update the floating-point enabled exception summary */
255 env
->fpscr
|= 1 << FPSCR_FEX
;
256 /* We must update the target FPR before raising the exception */
257 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
258 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
262 static inline void float_inexact_excp(CPUPPCState
*env
)
264 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
266 env
->fpscr
|= 1 << FPSCR_XX
;
267 /* Update the floating-point exception summary */
270 /* Update the floating-point enabled exception summary */
271 env
->fpscr
|= 1 << FPSCR_FEX
;
272 /* We must update the target FPR before raising the exception */
273 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
274 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
278 static inline void fpscr_set_rounding_mode(CPUPPCState
*env
)
282 /* Set rounding mode */
285 /* Best approximation (round to nearest) */
286 rnd_type
= float_round_nearest_even
;
289 /* Smaller magnitude (round toward zero) */
290 rnd_type
= float_round_to_zero
;
293 /* Round toward +infinite */
294 rnd_type
= float_round_up
;
298 /* Round toward -infinite */
299 rnd_type
= float_round_down
;
302 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
305 void helper_fpscr_clrbit(CPUPPCState
*env
, uint32_t bit
)
309 prev
= (env
->fpscr
>> bit
) & 1;
310 env
->fpscr
&= ~(1 << bit
);
315 fpscr_set_rounding_mode(env
);
323 void helper_fpscr_setbit(CPUPPCState
*env
, uint32_t bit
)
325 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
328 prev
= (env
->fpscr
>> bit
) & 1;
329 env
->fpscr
|= 1 << bit
;
371 env
->fpscr
|= 1 << FPSCR_VX
;
380 env
->error_code
= POWERPC_EXCP_FP
;
382 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
385 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
388 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
391 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
394 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
397 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
400 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
403 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
406 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
414 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
421 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
428 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
435 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
441 fpscr_set_rounding_mode(env
);
446 /* Update the floating-point enabled exception summary */
447 env
->fpscr
|= 1 << FPSCR_FEX
;
448 /* We have to update Rc1 before raising the exception */
449 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
455 void helper_store_fpscr(CPUPPCState
*env
, uint64_t arg
, uint32_t mask
)
457 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
458 target_ulong prev
, new;
462 new = (target_ulong
)arg
;
463 new &= ~0x60000000LL
;
464 new |= prev
& 0x60000000LL
;
465 for (i
= 0; i
< sizeof(target_ulong
) * 2; i
++) {
466 if (mask
& (1 << i
)) {
467 env
->fpscr
&= ~(0xFLL
<< (4 * i
));
468 env
->fpscr
|= new & (0xFLL
<< (4 * i
));
471 /* Update VX and FEX */
473 env
->fpscr
|= 1 << FPSCR_VX
;
475 env
->fpscr
&= ~(1 << FPSCR_VX
);
477 if ((fpscr_ex
& fpscr_eex
) != 0) {
478 env
->fpscr
|= 1 << FPSCR_FEX
;
479 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
480 /* XXX: we should compute it properly */
481 env
->error_code
= POWERPC_EXCP_FP
;
483 env
->fpscr
&= ~(1 << FPSCR_FEX
);
485 fpscr_set_rounding_mode(env
);
488 void store_fpscr(CPUPPCState
*env
, uint64_t arg
, uint32_t mask
)
490 helper_store_fpscr(env
, arg
, mask
);
493 void helper_float_check_status(CPUPPCState
*env
)
495 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
496 int status
= get_float_exception_flags(&env
->fp_status
);
498 if (status
& float_flag_divbyzero
) {
499 float_zero_divide_excp(env
);
500 } else if (status
& float_flag_overflow
) {
501 float_overflow_excp(env
);
502 } else if (status
& float_flag_underflow
) {
503 float_underflow_excp(env
);
504 } else if (status
& float_flag_inexact
) {
505 float_inexact_excp(env
);
508 if (cs
->exception_index
== POWERPC_EXCP_PROGRAM
&&
509 (env
->error_code
& POWERPC_EXCP_FP
)) {
510 /* Differred floating-point exception after target FPR update */
511 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
512 helper_raise_exception_err(env
, cs
->exception_index
,
518 void helper_reset_fpstatus(CPUPPCState
*env
)
520 set_float_exception_flags(0, &env
->fp_status
);
524 uint64_t helper_fadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
526 CPU_DoubleU farg1
, farg2
;
531 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
532 float64_is_neg(farg1
.d
) != float64_is_neg(farg2
.d
))) {
533 /* Magnitude subtraction of infinities */
534 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
536 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
537 float64_is_signaling_nan(farg2
.d
))) {
539 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
541 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
548 uint64_t helper_fsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
550 CPU_DoubleU farg1
, farg2
;
555 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
556 float64_is_neg(farg1
.d
) == float64_is_neg(farg2
.d
))) {
557 /* Magnitude subtraction of infinities */
558 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
560 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
561 float64_is_signaling_nan(farg2
.d
))) {
562 /* sNaN subtraction */
563 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
565 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
572 uint64_t helper_fmul(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
574 CPU_DoubleU farg1
, farg2
;
579 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
580 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
581 /* Multiplication of zero by infinity */
582 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
584 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
585 float64_is_signaling_nan(farg2
.d
))) {
586 /* sNaN multiplication */
587 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
589 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
596 uint64_t helper_fdiv(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
598 CPU_DoubleU farg1
, farg2
;
603 if (unlikely(float64_is_infinity(farg1
.d
) &&
604 float64_is_infinity(farg2
.d
))) {
605 /* Division of infinity by infinity */
606 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIDI
, 1);
607 } else if (unlikely(float64_is_zero(farg1
.d
) && float64_is_zero(farg2
.d
))) {
608 /* Division of zero by zero */
609 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXZDZ
, 1);
611 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
612 float64_is_signaling_nan(farg2
.d
))) {
614 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
616 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
623 #define FPU_FCTI(op, cvt, nanval) \
624 uint64_t helper_##op(CPUPPCState *env, uint64_t arg) \
629 farg.ll = float64_to_##cvt(farg.d, &env->fp_status); \
631 if (unlikely(env->fp_status.float_exception_flags)) { \
632 if (float64_is_any_nan(arg)) { \
633 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 1); \
634 if (float64_is_signaling_nan(arg)) { \
635 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1); \
638 } else if (env->fp_status.float_exception_flags & \
639 float_flag_invalid) { \
640 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 1); \
642 helper_float_check_status(env); \
647 FPU_FCTI(fctiw
, int32
, 0x80000000U
)
648 FPU_FCTI(fctiwz
, int32_round_to_zero
, 0x80000000U
)
649 FPU_FCTI(fctiwu
, uint32
, 0x00000000U
)
650 FPU_FCTI(fctiwuz
, uint32_round_to_zero
, 0x00000000U
)
651 FPU_FCTI(fctid
, int64
, 0x8000000000000000ULL
)
652 FPU_FCTI(fctidz
, int64_round_to_zero
, 0x8000000000000000ULL
)
653 FPU_FCTI(fctidu
, uint64
, 0x0000000000000000ULL
)
654 FPU_FCTI(fctiduz
, uint64_round_to_zero
, 0x0000000000000000ULL
)
656 #define FPU_FCFI(op, cvtr, is_single) \
657 uint64_t helper_##op(CPUPPCState *env, uint64_t arg) \
662 float32 tmp = cvtr(arg, &env->fp_status); \
663 farg.d = float32_to_float64(tmp, &env->fp_status); \
665 farg.d = cvtr(arg, &env->fp_status); \
667 helper_float_check_status(env); \
671 FPU_FCFI(fcfid
, int64_to_float64
, 0)
672 FPU_FCFI(fcfids
, int64_to_float32
, 1)
673 FPU_FCFI(fcfidu
, uint64_to_float64
, 0)
674 FPU_FCFI(fcfidus
, uint64_to_float32
, 1)
676 static inline uint64_t do_fri(CPUPPCState
*env
, uint64_t arg
,
683 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
685 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
686 farg
.ll
= arg
| 0x0008000000000000ULL
;
688 int inexact
= get_float_exception_flags(&env
->fp_status
) &
690 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
691 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
692 /* Restore rounding mode from FPSCR */
693 fpscr_set_rounding_mode(env
);
695 /* fri* does not set FPSCR[XX] */
697 env
->fp_status
.float_exception_flags
&= ~float_flag_inexact
;
700 helper_float_check_status(env
);
704 uint64_t helper_frin(CPUPPCState
*env
, uint64_t arg
)
706 return do_fri(env
, arg
, float_round_ties_away
);
709 uint64_t helper_friz(CPUPPCState
*env
, uint64_t arg
)
711 return do_fri(env
, arg
, float_round_to_zero
);
714 uint64_t helper_frip(CPUPPCState
*env
, uint64_t arg
)
716 return do_fri(env
, arg
, float_round_up
);
719 uint64_t helper_frim(CPUPPCState
*env
, uint64_t arg
)
721 return do_fri(env
, arg
, float_round_down
);
725 uint64_t helper_fmadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
728 CPU_DoubleU farg1
, farg2
, farg3
;
734 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
735 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
736 /* Multiplication of zero by infinity */
737 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
739 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
740 float64_is_signaling_nan(farg2
.d
) ||
741 float64_is_signaling_nan(farg3
.d
))) {
743 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
745 /* This is the way the PowerPC specification defines it */
746 float128 ft0_128
, ft1_128
;
748 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
749 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
750 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
751 if (unlikely(float128_is_infinity(ft0_128
) &&
752 float64_is_infinity(farg3
.d
) &&
753 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
754 /* Magnitude subtraction of infinities */
755 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
757 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
758 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
759 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
767 uint64_t helper_fmsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
770 CPU_DoubleU farg1
, farg2
, farg3
;
776 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
777 (float64_is_zero(farg1
.d
) &&
778 float64_is_infinity(farg2
.d
)))) {
779 /* Multiplication of zero by infinity */
780 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
782 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
783 float64_is_signaling_nan(farg2
.d
) ||
784 float64_is_signaling_nan(farg3
.d
))) {
786 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
788 /* This is the way the PowerPC specification defines it */
789 float128 ft0_128
, ft1_128
;
791 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
792 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
793 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
794 if (unlikely(float128_is_infinity(ft0_128
) &&
795 float64_is_infinity(farg3
.d
) &&
796 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
797 /* Magnitude subtraction of infinities */
798 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
800 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
801 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
802 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
808 /* fnmadd - fnmadd. */
809 uint64_t helper_fnmadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
812 CPU_DoubleU farg1
, farg2
, farg3
;
818 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
819 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
820 /* Multiplication of zero by infinity */
821 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
823 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
824 float64_is_signaling_nan(farg2
.d
) ||
825 float64_is_signaling_nan(farg3
.d
))) {
827 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
829 /* This is the way the PowerPC specification defines it */
830 float128 ft0_128
, ft1_128
;
832 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
833 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
834 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
835 if (unlikely(float128_is_infinity(ft0_128
) &&
836 float64_is_infinity(farg3
.d
) &&
837 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
838 /* Magnitude subtraction of infinities */
839 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
841 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
842 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
843 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
845 if (likely(!float64_is_any_nan(farg1
.d
))) {
846 farg1
.d
= float64_chs(farg1
.d
);
852 /* fnmsub - fnmsub. */
853 uint64_t helper_fnmsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
856 CPU_DoubleU farg1
, farg2
, farg3
;
862 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
863 (float64_is_zero(farg1
.d
) &&
864 float64_is_infinity(farg2
.d
)))) {
865 /* Multiplication of zero by infinity */
866 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
868 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
869 float64_is_signaling_nan(farg2
.d
) ||
870 float64_is_signaling_nan(farg3
.d
))) {
872 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
874 /* This is the way the PowerPC specification defines it */
875 float128 ft0_128
, ft1_128
;
877 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
878 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
879 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
880 if (unlikely(float128_is_infinity(ft0_128
) &&
881 float64_is_infinity(farg3
.d
) &&
882 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
883 /* Magnitude subtraction of infinities */
884 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
886 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
887 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
888 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
890 if (likely(!float64_is_any_nan(farg1
.d
))) {
891 farg1
.d
= float64_chs(farg1
.d
);
898 uint64_t helper_frsp(CPUPPCState
*env
, uint64_t arg
)
905 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
906 /* sNaN square root */
907 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
909 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
910 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
916 uint64_t helper_fsqrt(CPUPPCState
*env
, uint64_t arg
)
922 if (unlikely(float64_is_any_nan(farg
.d
))) {
923 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
924 /* sNaN reciprocal square root */
925 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
926 farg
.ll
= float64_snan_to_qnan(farg
.ll
);
928 } else if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
929 /* Square root of a negative nonzero number */
930 farg
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSQRT
, 1);
932 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
938 uint64_t helper_fre(CPUPPCState
*env
, uint64_t arg
)
944 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
945 /* sNaN reciprocal */
946 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
948 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
953 uint64_t helper_fres(CPUPPCState
*env
, uint64_t arg
)
960 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
961 /* sNaN reciprocal */
962 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
964 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
965 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
966 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
971 /* frsqrte - frsqrte. */
972 uint64_t helper_frsqrte(CPUPPCState
*env
, uint64_t arg
)
978 if (unlikely(float64_is_any_nan(farg
.d
))) {
979 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
980 /* sNaN reciprocal square root */
981 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
982 farg
.ll
= float64_snan_to_qnan(farg
.ll
);
984 } else if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
985 /* Reciprocal square root of a negative nonzero number */
986 farg
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSQRT
, 1);
988 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
989 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
996 uint64_t helper_fsel(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1003 if ((!float64_is_neg(farg1
.d
) || float64_is_zero(farg1
.d
)) &&
1004 !float64_is_any_nan(farg1
.d
)) {
1011 uint32_t helper_ftdiv(uint64_t fra
, uint64_t frb
)
1016 if (unlikely(float64_is_infinity(fra
) ||
1017 float64_is_infinity(frb
) ||
1018 float64_is_zero(frb
))) {
1022 int e_a
= ppc_float64_get_unbiased_exp(fra
);
1023 int e_b
= ppc_float64_get_unbiased_exp(frb
);
1025 if (unlikely(float64_is_any_nan(fra
) ||
1026 float64_is_any_nan(frb
))) {
1028 } else if ((e_b
<= -1022) || (e_b
>= 1021)) {
1030 } else if (!float64_is_zero(fra
) &&
1031 (((e_a
- e_b
) >= 1023) ||
1032 ((e_a
- e_b
) <= -1021) ||
1037 if (unlikely(float64_is_zero_or_denormal(frb
))) {
1038 /* XB is not zero because of the above check and */
1039 /* so must be denormalized. */
1044 return 0x8 | (fg_flag
? 4 : 0) | (fe_flag
? 2 : 0);
1047 uint32_t helper_ftsqrt(uint64_t frb
)
1052 if (unlikely(float64_is_infinity(frb
) || float64_is_zero(frb
))) {
1056 int e_b
= ppc_float64_get_unbiased_exp(frb
);
1058 if (unlikely(float64_is_any_nan(frb
))) {
1060 } else if (unlikely(float64_is_zero(frb
))) {
1062 } else if (unlikely(float64_is_neg(frb
))) {
1064 } else if (!float64_is_zero(frb
) && (e_b
<= (-1022+52))) {
1068 if (unlikely(float64_is_zero_or_denormal(frb
))) {
1069 /* XB is not zero because of the above check and */
1070 /* therefore must be denormalized. */
1075 return 0x8 | (fg_flag
? 4 : 0) | (fe_flag
? 2 : 0);
1078 void helper_fcmpu(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1081 CPU_DoubleU farg1
, farg2
;
1087 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1088 float64_is_any_nan(farg2
.d
))) {
1090 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1092 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1098 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1099 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1100 env
->crf
[crfD
] = ret
;
1101 if (unlikely(ret
== 0x01UL
1102 && (float64_is_signaling_nan(farg1
.d
) ||
1103 float64_is_signaling_nan(farg2
.d
)))) {
1104 /* sNaN comparison */
1105 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
1109 void helper_fcmpo(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1112 CPU_DoubleU farg1
, farg2
;
1118 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1119 float64_is_any_nan(farg2
.d
))) {
1121 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1123 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1129 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1130 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1131 env
->crf
[crfD
] = ret
;
1132 if (unlikely(ret
== 0x01UL
)) {
1133 if (float64_is_signaling_nan(farg1
.d
) ||
1134 float64_is_signaling_nan(farg2
.d
)) {
1135 /* sNaN comparison */
1136 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
|
1137 POWERPC_EXCP_FP_VXVC
, 1);
1139 /* qNaN comparison */
1140 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXVC
, 1);
1145 /* Single-precision floating-point conversions */
1146 static inline uint32_t efscfsi(CPUPPCState
*env
, uint32_t val
)
1150 u
.f
= int32_to_float32(val
, &env
->vec_status
);
1155 static inline uint32_t efscfui(CPUPPCState
*env
, uint32_t val
)
1159 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
1164 static inline int32_t efsctsi(CPUPPCState
*env
, uint32_t val
)
1169 /* NaN are not treated the same way IEEE 754 does */
1170 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1174 return float32_to_int32(u
.f
, &env
->vec_status
);
1177 static inline uint32_t efsctui(CPUPPCState
*env
, uint32_t val
)
1182 /* NaN are not treated the same way IEEE 754 does */
1183 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1187 return float32_to_uint32(u
.f
, &env
->vec_status
);
1190 static inline uint32_t efsctsiz(CPUPPCState
*env
, uint32_t val
)
1195 /* NaN are not treated the same way IEEE 754 does */
1196 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1200 return float32_to_int32_round_to_zero(u
.f
, &env
->vec_status
);
1203 static inline uint32_t efsctuiz(CPUPPCState
*env
, uint32_t val
)
1208 /* NaN are not treated the same way IEEE 754 does */
1209 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1213 return float32_to_uint32_round_to_zero(u
.f
, &env
->vec_status
);
1216 static inline uint32_t efscfsf(CPUPPCState
*env
, uint32_t val
)
1221 u
.f
= int32_to_float32(val
, &env
->vec_status
);
1222 tmp
= int64_to_float32(1ULL << 32, &env
->vec_status
);
1223 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
1228 static inline uint32_t efscfuf(CPUPPCState
*env
, uint32_t val
)
1233 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
1234 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1235 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
1240 static inline uint32_t efsctsf(CPUPPCState
*env
, uint32_t val
)
1246 /* NaN are not treated the same way IEEE 754 does */
1247 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1250 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1251 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
1253 return float32_to_int32(u
.f
, &env
->vec_status
);
1256 static inline uint32_t efsctuf(CPUPPCState
*env
, uint32_t val
)
1262 /* NaN are not treated the same way IEEE 754 does */
1263 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1266 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1267 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
1269 return float32_to_uint32(u
.f
, &env
->vec_status
);
1272 #define HELPER_SPE_SINGLE_CONV(name) \
1273 uint32_t helper_e##name(CPUPPCState *env, uint32_t val) \
1275 return e##name(env, val); \
1278 HELPER_SPE_SINGLE_CONV(fscfsi
);
1280 HELPER_SPE_SINGLE_CONV(fscfui
);
1282 HELPER_SPE_SINGLE_CONV(fscfuf
);
1284 HELPER_SPE_SINGLE_CONV(fscfsf
);
1286 HELPER_SPE_SINGLE_CONV(fsctsi
);
1288 HELPER_SPE_SINGLE_CONV(fsctui
);
1290 HELPER_SPE_SINGLE_CONV(fsctsiz
);
1292 HELPER_SPE_SINGLE_CONV(fsctuiz
);
1294 HELPER_SPE_SINGLE_CONV(fsctsf
);
1296 HELPER_SPE_SINGLE_CONV(fsctuf
);
1298 #define HELPER_SPE_VECTOR_CONV(name) \
1299 uint64_t helper_ev##name(CPUPPCState *env, uint64_t val) \
1301 return ((uint64_t)e##name(env, val >> 32) << 32) | \
1302 (uint64_t)e##name(env, val); \
1305 HELPER_SPE_VECTOR_CONV(fscfsi
);
1307 HELPER_SPE_VECTOR_CONV(fscfui
);
1309 HELPER_SPE_VECTOR_CONV(fscfuf
);
1311 HELPER_SPE_VECTOR_CONV(fscfsf
);
1313 HELPER_SPE_VECTOR_CONV(fsctsi
);
1315 HELPER_SPE_VECTOR_CONV(fsctui
);
1317 HELPER_SPE_VECTOR_CONV(fsctsiz
);
1319 HELPER_SPE_VECTOR_CONV(fsctuiz
);
1321 HELPER_SPE_VECTOR_CONV(fsctsf
);
1323 HELPER_SPE_VECTOR_CONV(fsctuf
);
1325 /* Single-precision floating-point arithmetic */
1326 static inline uint32_t efsadd(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1332 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->vec_status
);
1336 static inline uint32_t efssub(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1342 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->vec_status
);
1346 static inline uint32_t efsmul(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1352 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->vec_status
);
1356 static inline uint32_t efsdiv(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1362 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->vec_status
);
1366 #define HELPER_SPE_SINGLE_ARITH(name) \
1367 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1369 return e##name(env, op1, op2); \
1372 HELPER_SPE_SINGLE_ARITH(fsadd
);
1374 HELPER_SPE_SINGLE_ARITH(fssub
);
1376 HELPER_SPE_SINGLE_ARITH(fsmul
);
1378 HELPER_SPE_SINGLE_ARITH(fsdiv
);
1380 #define HELPER_SPE_VECTOR_ARITH(name) \
1381 uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1383 return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) | \
1384 (uint64_t)e##name(env, op1, op2); \
1387 HELPER_SPE_VECTOR_ARITH(fsadd
);
1389 HELPER_SPE_VECTOR_ARITH(fssub
);
1391 HELPER_SPE_VECTOR_ARITH(fsmul
);
1393 HELPER_SPE_VECTOR_ARITH(fsdiv
);
1395 /* Single-precision floating-point comparisons */
1396 static inline uint32_t efscmplt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1402 return float32_lt(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
1405 static inline uint32_t efscmpgt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1411 return float32_le(u1
.f
, u2
.f
, &env
->vec_status
) ? 0 : 4;
1414 static inline uint32_t efscmpeq(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1420 return float32_eq(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
1423 static inline uint32_t efststlt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1425 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1426 return efscmplt(env
, op1
, op2
);
1429 static inline uint32_t efststgt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1431 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1432 return efscmpgt(env
, op1
, op2
);
1435 static inline uint32_t efststeq(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1437 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1438 return efscmpeq(env
, op1
, op2
);
1441 #define HELPER_SINGLE_SPE_CMP(name) \
1442 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1444 return e##name(env, op1, op2) << 2; \
1447 HELPER_SINGLE_SPE_CMP(fststlt
);
1449 HELPER_SINGLE_SPE_CMP(fststgt
);
1451 HELPER_SINGLE_SPE_CMP(fststeq
);
1453 HELPER_SINGLE_SPE_CMP(fscmplt
);
1455 HELPER_SINGLE_SPE_CMP(fscmpgt
);
1457 HELPER_SINGLE_SPE_CMP(fscmpeq
);
1459 static inline uint32_t evcmp_merge(int t0
, int t1
)
1461 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1464 #define HELPER_VECTOR_SPE_CMP(name) \
1465 uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1467 return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32), \
1468 e##name(env, op1, op2)); \
1471 HELPER_VECTOR_SPE_CMP(fststlt
);
1473 HELPER_VECTOR_SPE_CMP(fststgt
);
1475 HELPER_VECTOR_SPE_CMP(fststeq
);
1477 HELPER_VECTOR_SPE_CMP(fscmplt
);
1479 HELPER_VECTOR_SPE_CMP(fscmpgt
);
1481 HELPER_VECTOR_SPE_CMP(fscmpeq
);
1483 /* Double-precision floating-point conversion */
1484 uint64_t helper_efdcfsi(CPUPPCState
*env
, uint32_t val
)
1488 u
.d
= int32_to_float64(val
, &env
->vec_status
);
1493 uint64_t helper_efdcfsid(CPUPPCState
*env
, uint64_t val
)
1497 u
.d
= int64_to_float64(val
, &env
->vec_status
);
1502 uint64_t helper_efdcfui(CPUPPCState
*env
, uint32_t val
)
1506 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
1511 uint64_t helper_efdcfuid(CPUPPCState
*env
, uint64_t val
)
1515 u
.d
= uint64_to_float64(val
, &env
->vec_status
);
1520 uint32_t helper_efdctsi(CPUPPCState
*env
, uint64_t val
)
1525 /* NaN are not treated the same way IEEE 754 does */
1526 if (unlikely(float64_is_any_nan(u
.d
))) {
1530 return float64_to_int32(u
.d
, &env
->vec_status
);
1533 uint32_t helper_efdctui(CPUPPCState
*env
, uint64_t val
)
1538 /* NaN are not treated the same way IEEE 754 does */
1539 if (unlikely(float64_is_any_nan(u
.d
))) {
1543 return float64_to_uint32(u
.d
, &env
->vec_status
);
1546 uint32_t helper_efdctsiz(CPUPPCState
*env
, uint64_t val
)
1551 /* NaN are not treated the same way IEEE 754 does */
1552 if (unlikely(float64_is_any_nan(u
.d
))) {
1556 return float64_to_int32_round_to_zero(u
.d
, &env
->vec_status
);
1559 uint64_t helper_efdctsidz(CPUPPCState
*env
, uint64_t val
)
1564 /* NaN are not treated the same way IEEE 754 does */
1565 if (unlikely(float64_is_any_nan(u
.d
))) {
1569 return float64_to_int64_round_to_zero(u
.d
, &env
->vec_status
);
1572 uint32_t helper_efdctuiz(CPUPPCState
*env
, uint64_t val
)
1577 /* NaN are not treated the same way IEEE 754 does */
1578 if (unlikely(float64_is_any_nan(u
.d
))) {
1582 return float64_to_uint32_round_to_zero(u
.d
, &env
->vec_status
);
1585 uint64_t helper_efdctuidz(CPUPPCState
*env
, uint64_t val
)
1590 /* NaN are not treated the same way IEEE 754 does */
1591 if (unlikely(float64_is_any_nan(u
.d
))) {
1595 return float64_to_uint64_round_to_zero(u
.d
, &env
->vec_status
);
1598 uint64_t helper_efdcfsf(CPUPPCState
*env
, uint32_t val
)
1603 u
.d
= int32_to_float64(val
, &env
->vec_status
);
1604 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
1605 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
1610 uint64_t helper_efdcfuf(CPUPPCState
*env
, uint32_t val
)
1615 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
1616 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
1617 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
1622 uint32_t helper_efdctsf(CPUPPCState
*env
, uint64_t val
)
1628 /* NaN are not treated the same way IEEE 754 does */
1629 if (unlikely(float64_is_any_nan(u
.d
))) {
1632 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
1633 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
1635 return float64_to_int32(u
.d
, &env
->vec_status
);
1638 uint32_t helper_efdctuf(CPUPPCState
*env
, uint64_t val
)
1644 /* NaN are not treated the same way IEEE 754 does */
1645 if (unlikely(float64_is_any_nan(u
.d
))) {
1648 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
1649 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
1651 return float64_to_uint32(u
.d
, &env
->vec_status
);
1654 uint32_t helper_efscfd(CPUPPCState
*env
, uint64_t val
)
1660 u2
.f
= float64_to_float32(u1
.d
, &env
->vec_status
);
1665 uint64_t helper_efdcfs(CPUPPCState
*env
, uint32_t val
)
1671 u2
.d
= float32_to_float64(u1
.f
, &env
->vec_status
);
1676 /* Double precision fixed-point arithmetic */
1677 uint64_t helper_efdadd(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1683 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->vec_status
);
1687 uint64_t helper_efdsub(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1693 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->vec_status
);
1697 uint64_t helper_efdmul(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1703 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->vec_status
);
1707 uint64_t helper_efddiv(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1713 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->vec_status
);
1717 /* Double precision floating point helpers */
1718 uint32_t helper_efdtstlt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1724 return float64_lt(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
1727 uint32_t helper_efdtstgt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1733 return float64_le(u1
.d
, u2
.d
, &env
->vec_status
) ? 0 : 4;
1736 uint32_t helper_efdtsteq(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1742 return float64_eq_quiet(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
1745 uint32_t helper_efdcmplt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1747 /* XXX: TODO: test special values (NaN, infinites, ...) */
1748 return helper_efdtstlt(env
, op1
, op2
);
1751 uint32_t helper_efdcmpgt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1753 /* XXX: TODO: test special values (NaN, infinites, ...) */
1754 return helper_efdtstgt(env
, op1
, op2
);
1757 uint32_t helper_efdcmpeq(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1759 /* XXX: TODO: test special values (NaN, infinites, ...) */
1760 return helper_efdtsteq(env
, op1
, op2
);
1763 #define DECODE_SPLIT(opcode, shift1, nb1, shift2, nb2) \
1764 (((((opcode) >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) | \
1765 (((opcode) >> (shift2)) & ((1 << (nb2)) - 1)))
1767 #define xT(opcode) DECODE_SPLIT(opcode, 0, 1, 21, 5)
1768 #define xA(opcode) DECODE_SPLIT(opcode, 2, 1, 16, 5)
1769 #define xB(opcode) DECODE_SPLIT(opcode, 1, 1, 11, 5)
1770 #define xC(opcode) DECODE_SPLIT(opcode, 3, 1, 6, 5)
1771 #define BF(opcode) (((opcode) >> (31-8)) & 7)
1773 typedef union _ppc_vsr_t
{
1780 #if defined(HOST_WORDS_BIGENDIAN)
1781 #define VsrW(i) u32[i]
1782 #define VsrD(i) u64[i]
1784 #define VsrW(i) u32[3-(i)]
1785 #define VsrD(i) u64[1-(i)]
1788 static void getVSR(int n
, ppc_vsr_t
*vsr
, CPUPPCState
*env
)
1791 vsr
->VsrD(0) = env
->fpr
[n
];
1792 vsr
->VsrD(1) = env
->vsr
[n
];
1794 vsr
->u64
[0] = env
->avr
[n
-32].u64
[0];
1795 vsr
->u64
[1] = env
->avr
[n
-32].u64
[1];
1799 static void putVSR(int n
, ppc_vsr_t
*vsr
, CPUPPCState
*env
)
1802 env
->fpr
[n
] = vsr
->VsrD(0);
1803 env
->vsr
[n
] = vsr
->VsrD(1);
1805 env
->avr
[n
-32].u64
[0] = vsr
->u64
[0];
1806 env
->avr
[n
-32].u64
[1] = vsr
->u64
[1];
1810 #define float64_to_float64(x, env) x
1813 /* VSX_ADD_SUB - VSX floating point add/subract
1814 * name - instruction mnemonic
1815 * op - operation (add or sub)
1816 * nels - number of elements (1, 2 or 4)
1817 * tp - type (float32 or float64)
1818 * fld - vsr_t field (VsrD(*) or VsrW(*))
1821 #define VSX_ADD_SUB(name, op, nels, tp, fld, sfprf, r2sp) \
1822 void helper_##name(CPUPPCState *env, uint32_t opcode) \
1824 ppc_vsr_t xt, xa, xb; \
1827 getVSR(xA(opcode), &xa, env); \
1828 getVSR(xB(opcode), &xb, env); \
1829 getVSR(xT(opcode), &xt, env); \
1830 helper_reset_fpstatus(env); \
1832 for (i = 0; i < nels; i++) { \
1833 float_status tstat = env->fp_status; \
1834 set_float_exception_flags(0, &tstat); \
1835 xt.fld = tp##_##op(xa.fld, xb.fld, &tstat); \
1836 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1838 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1839 if (tp##_is_infinity(xa.fld) && tp##_is_infinity(xb.fld)) { \
1840 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
1841 } else if (tp##_is_signaling_nan(xa.fld) || \
1842 tp##_is_signaling_nan(xb.fld)) { \
1843 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1848 xt.fld = helper_frsp(env, xt.fld); \
1852 helper_compute_fprf(env, xt.fld); \
1855 putVSR(xT(opcode), &xt, env); \
1856 helper_float_check_status(env); \
1859 VSX_ADD_SUB(xsadddp
, add
, 1, float64
, VsrD(0), 1, 0)
1860 VSX_ADD_SUB(xsaddsp
, add
, 1, float64
, VsrD(0), 1, 1)
1861 VSX_ADD_SUB(xvadddp
, add
, 2, float64
, VsrD(i
), 0, 0)
1862 VSX_ADD_SUB(xvaddsp
, add
, 4, float32
, VsrW(i
), 0, 0)
1863 VSX_ADD_SUB(xssubdp
, sub
, 1, float64
, VsrD(0), 1, 0)
1864 VSX_ADD_SUB(xssubsp
, sub
, 1, float64
, VsrD(0), 1, 1)
1865 VSX_ADD_SUB(xvsubdp
, sub
, 2, float64
, VsrD(i
), 0, 0)
1866 VSX_ADD_SUB(xvsubsp
, sub
, 4, float32
, VsrW(i
), 0, 0)
1868 /* VSX_MUL - VSX floating point multiply
1869 * op - instruction mnemonic
1870 * nels - number of elements (1, 2 or 4)
1871 * tp - type (float32 or float64)
1872 * fld - vsr_t field (VsrD(*) or VsrW(*))
1875 #define VSX_MUL(op, nels, tp, fld, sfprf, r2sp) \
1876 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1878 ppc_vsr_t xt, xa, xb; \
1881 getVSR(xA(opcode), &xa, env); \
1882 getVSR(xB(opcode), &xb, env); \
1883 getVSR(xT(opcode), &xt, env); \
1884 helper_reset_fpstatus(env); \
1886 for (i = 0; i < nels; i++) { \
1887 float_status tstat = env->fp_status; \
1888 set_float_exception_flags(0, &tstat); \
1889 xt.fld = tp##_mul(xa.fld, xb.fld, &tstat); \
1890 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1892 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1893 if ((tp##_is_infinity(xa.fld) && tp##_is_zero(xb.fld)) || \
1894 (tp##_is_infinity(xb.fld) && tp##_is_zero(xa.fld))) { \
1895 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXIMZ, sfprf); \
1896 } else if (tp##_is_signaling_nan(xa.fld) || \
1897 tp##_is_signaling_nan(xb.fld)) { \
1898 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1903 xt.fld = helper_frsp(env, xt.fld); \
1907 helper_compute_fprf(env, xt.fld); \
1911 putVSR(xT(opcode), &xt, env); \
1912 helper_float_check_status(env); \
1915 VSX_MUL(xsmuldp
, 1, float64
, VsrD(0), 1, 0)
1916 VSX_MUL(xsmulsp
, 1, float64
, VsrD(0), 1, 1)
1917 VSX_MUL(xvmuldp
, 2, float64
, VsrD(i
), 0, 0)
1918 VSX_MUL(xvmulsp
, 4, float32
, VsrW(i
), 0, 0)
1920 /* VSX_DIV - VSX floating point divide
1921 * op - instruction mnemonic
1922 * nels - number of elements (1, 2 or 4)
1923 * tp - type (float32 or float64)
1924 * fld - vsr_t field (VsrD(*) or VsrW(*))
1927 #define VSX_DIV(op, nels, tp, fld, sfprf, r2sp) \
1928 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1930 ppc_vsr_t xt, xa, xb; \
1933 getVSR(xA(opcode), &xa, env); \
1934 getVSR(xB(opcode), &xb, env); \
1935 getVSR(xT(opcode), &xt, env); \
1936 helper_reset_fpstatus(env); \
1938 for (i = 0; i < nels; i++) { \
1939 float_status tstat = env->fp_status; \
1940 set_float_exception_flags(0, &tstat); \
1941 xt.fld = tp##_div(xa.fld, xb.fld, &tstat); \
1942 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1944 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1945 if (tp##_is_infinity(xa.fld) && tp##_is_infinity(xb.fld)) { \
1946 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXIDI, sfprf); \
1947 } else if (tp##_is_zero(xa.fld) && \
1948 tp##_is_zero(xb.fld)) { \
1949 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXZDZ, sfprf); \
1950 } else if (tp##_is_signaling_nan(xa.fld) || \
1951 tp##_is_signaling_nan(xb.fld)) { \
1952 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1957 xt.fld = helper_frsp(env, xt.fld); \
1961 helper_compute_fprf(env, xt.fld); \
1965 putVSR(xT(opcode), &xt, env); \
1966 helper_float_check_status(env); \
1969 VSX_DIV(xsdivdp
, 1, float64
, VsrD(0), 1, 0)
1970 VSX_DIV(xsdivsp
, 1, float64
, VsrD(0), 1, 1)
1971 VSX_DIV(xvdivdp
, 2, float64
, VsrD(i
), 0, 0)
1972 VSX_DIV(xvdivsp
, 4, float32
, VsrW(i
), 0, 0)
1974 /* VSX_RE - VSX floating point reciprocal estimate
1975 * op - instruction mnemonic
1976 * nels - number of elements (1, 2 or 4)
1977 * tp - type (float32 or float64)
1978 * fld - vsr_t field (VsrD(*) or VsrW(*))
1981 #define VSX_RE(op, nels, tp, fld, sfprf, r2sp) \
1982 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1987 getVSR(xB(opcode), &xb, env); \
1988 getVSR(xT(opcode), &xt, env); \
1989 helper_reset_fpstatus(env); \
1991 for (i = 0; i < nels; i++) { \
1992 if (unlikely(tp##_is_signaling_nan(xb.fld))) { \
1993 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1995 xt.fld = tp##_div(tp##_one, xb.fld, &env->fp_status); \
1998 xt.fld = helper_frsp(env, xt.fld); \
2002 helper_compute_fprf(env, xt.fld); \
2006 putVSR(xT(opcode), &xt, env); \
2007 helper_float_check_status(env); \
2010 VSX_RE(xsredp
, 1, float64
, VsrD(0), 1, 0)
2011 VSX_RE(xsresp
, 1, float64
, VsrD(0), 1, 1)
2012 VSX_RE(xvredp
, 2, float64
, VsrD(i
), 0, 0)
2013 VSX_RE(xvresp
, 4, float32
, VsrW(i
), 0, 0)
2015 /* VSX_SQRT - VSX floating point square root
2016 * op - instruction mnemonic
2017 * nels - number of elements (1, 2 or 4)
2018 * tp - type (float32 or float64)
2019 * fld - vsr_t field (VsrD(*) or VsrW(*))
2022 #define VSX_SQRT(op, nels, tp, fld, sfprf, r2sp) \
2023 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2028 getVSR(xB(opcode), &xb, env); \
2029 getVSR(xT(opcode), &xt, env); \
2030 helper_reset_fpstatus(env); \
2032 for (i = 0; i < nels; i++) { \
2033 float_status tstat = env->fp_status; \
2034 set_float_exception_flags(0, &tstat); \
2035 xt.fld = tp##_sqrt(xb.fld, &tstat); \
2036 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2038 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2039 if (tp##_is_neg(xb.fld) && !tp##_is_zero(xb.fld)) { \
2040 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2041 } else if (tp##_is_signaling_nan(xb.fld)) { \
2042 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2047 xt.fld = helper_frsp(env, xt.fld); \
2051 helper_compute_fprf(env, xt.fld); \
2055 putVSR(xT(opcode), &xt, env); \
2056 helper_float_check_status(env); \
2059 VSX_SQRT(xssqrtdp
, 1, float64
, VsrD(0), 1, 0)
2060 VSX_SQRT(xssqrtsp
, 1, float64
, VsrD(0), 1, 1)
2061 VSX_SQRT(xvsqrtdp
, 2, float64
, VsrD(i
), 0, 0)
2062 VSX_SQRT(xvsqrtsp
, 4, float32
, VsrW(i
), 0, 0)
2064 /* VSX_RSQRTE - VSX floating point reciprocal square root estimate
2065 * op - instruction mnemonic
2066 * nels - number of elements (1, 2 or 4)
2067 * tp - type (float32 or float64)
2068 * fld - vsr_t field (VsrD(*) or VsrW(*))
2071 #define VSX_RSQRTE(op, nels, tp, fld, sfprf, r2sp) \
2072 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2077 getVSR(xB(opcode), &xb, env); \
2078 getVSR(xT(opcode), &xt, env); \
2079 helper_reset_fpstatus(env); \
2081 for (i = 0; i < nels; i++) { \
2082 float_status tstat = env->fp_status; \
2083 set_float_exception_flags(0, &tstat); \
2084 xt.fld = tp##_sqrt(xb.fld, &tstat); \
2085 xt.fld = tp##_div(tp##_one, xt.fld, &tstat); \
2086 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2088 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2089 if (tp##_is_neg(xb.fld) && !tp##_is_zero(xb.fld)) { \
2090 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2091 } else if (tp##_is_signaling_nan(xb.fld)) { \
2092 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2097 xt.fld = helper_frsp(env, xt.fld); \
2101 helper_compute_fprf(env, xt.fld); \
2105 putVSR(xT(opcode), &xt, env); \
2106 helper_float_check_status(env); \
2109 VSX_RSQRTE(xsrsqrtedp
, 1, float64
, VsrD(0), 1, 0)
2110 VSX_RSQRTE(xsrsqrtesp
, 1, float64
, VsrD(0), 1, 1)
2111 VSX_RSQRTE(xvrsqrtedp
, 2, float64
, VsrD(i
), 0, 0)
2112 VSX_RSQRTE(xvrsqrtesp
, 4, float32
, VsrW(i
), 0, 0)
2114 /* VSX_TDIV - VSX floating point test for divide
2115 * op - instruction mnemonic
2116 * nels - number of elements (1, 2 or 4)
2117 * tp - type (float32 or float64)
2118 * fld - vsr_t field (VsrD(*) or VsrW(*))
2119 * emin - minimum unbiased exponent
2120 * emax - maximum unbiased exponent
2121 * nbits - number of fraction bits
2123 #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits) \
2124 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2131 getVSR(xA(opcode), &xa, env); \
2132 getVSR(xB(opcode), &xb, env); \
2134 for (i = 0; i < nels; i++) { \
2135 if (unlikely(tp##_is_infinity(xa.fld) || \
2136 tp##_is_infinity(xb.fld) || \
2137 tp##_is_zero(xb.fld))) { \
2141 int e_a = ppc_##tp##_get_unbiased_exp(xa.fld); \
2142 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld); \
2144 if (unlikely(tp##_is_any_nan(xa.fld) || \
2145 tp##_is_any_nan(xb.fld))) { \
2147 } else if ((e_b <= emin) || (e_b >= (emax-2))) { \
2149 } else if (!tp##_is_zero(xa.fld) && \
2150 (((e_a - e_b) >= emax) || \
2151 ((e_a - e_b) <= (emin+1)) || \
2152 (e_a <= (emin+nbits)))) { \
2156 if (unlikely(tp##_is_zero_or_denormal(xb.fld))) { \
2157 /* XB is not zero because of the above check and */ \
2158 /* so must be denormalized. */ \
2164 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2167 VSX_TDIV(xstdivdp
, 1, float64
, VsrD(0), -1022, 1023, 52)
2168 VSX_TDIV(xvtdivdp
, 2, float64
, VsrD(i
), -1022, 1023, 52)
2169 VSX_TDIV(xvtdivsp
, 4, float32
, VsrW(i
), -126, 127, 23)
2171 /* VSX_TSQRT - VSX floating point test for square root
2172 * op - instruction mnemonic
2173 * nels - number of elements (1, 2 or 4)
2174 * tp - type (float32 or float64)
2175 * fld - vsr_t field (VsrD(*) or VsrW(*))
2176 * emin - minimum unbiased exponent
2177 * emax - maximum unbiased exponent
2178 * nbits - number of fraction bits
2180 #define VSX_TSQRT(op, nels, tp, fld, emin, nbits) \
2181 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2188 getVSR(xA(opcode), &xa, env); \
2189 getVSR(xB(opcode), &xb, env); \
2191 for (i = 0; i < nels; i++) { \
2192 if (unlikely(tp##_is_infinity(xb.fld) || \
2193 tp##_is_zero(xb.fld))) { \
2197 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld); \
2199 if (unlikely(tp##_is_any_nan(xb.fld))) { \
2201 } else if (unlikely(tp##_is_zero(xb.fld))) { \
2203 } else if (unlikely(tp##_is_neg(xb.fld))) { \
2205 } else if (!tp##_is_zero(xb.fld) && \
2206 (e_b <= (emin+nbits))) { \
2210 if (unlikely(tp##_is_zero_or_denormal(xb.fld))) { \
2211 /* XB is not zero because of the above check and */ \
2212 /* therefore must be denormalized. */ \
2218 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2221 VSX_TSQRT(xstsqrtdp
, 1, float64
, VsrD(0), -1022, 52)
2222 VSX_TSQRT(xvtsqrtdp
, 2, float64
, VsrD(i
), -1022, 52)
2223 VSX_TSQRT(xvtsqrtsp
, 4, float32
, VsrW(i
), -126, 23)
2225 /* VSX_MADD - VSX floating point muliply/add variations
2226 * op - instruction mnemonic
2227 * nels - number of elements (1, 2 or 4)
2228 * tp - type (float32 or float64)
2229 * fld - vsr_t field (VsrD(*) or VsrW(*))
2230 * maddflgs - flags for the float*muladd routine that control the
2231 * various forms (madd, msub, nmadd, nmsub)
2232 * afrm - A form (1=A, 0=M)
2235 #define VSX_MADD(op, nels, tp, fld, maddflgs, afrm, sfprf, r2sp) \
2236 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2238 ppc_vsr_t xt_in, xa, xb, xt_out; \
2242 if (afrm) { /* AxB + T */ \
2245 } else { /* AxT + B */ \
2250 getVSR(xA(opcode), &xa, env); \
2251 getVSR(xB(opcode), &xb, env); \
2252 getVSR(xT(opcode), &xt_in, env); \
2256 helper_reset_fpstatus(env); \
2258 for (i = 0; i < nels; i++) { \
2259 float_status tstat = env->fp_status; \
2260 set_float_exception_flags(0, &tstat); \
2261 if (r2sp && (tstat.float_rounding_mode == float_round_nearest_even)) {\
2262 /* Avoid double rounding errors by rounding the intermediate */ \
2263 /* result to odd. */ \
2264 set_float_rounding_mode(float_round_to_zero, &tstat); \
2265 xt_out.fld = tp##_muladd(xa.fld, b->fld, c->fld, \
2266 maddflgs, &tstat); \
2267 xt_out.fld |= (get_float_exception_flags(&tstat) & \
2268 float_flag_inexact) != 0; \
2270 xt_out.fld = tp##_muladd(xa.fld, b->fld, c->fld, \
2271 maddflgs, &tstat); \
2273 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2275 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2276 if (tp##_is_signaling_nan(xa.fld) || \
2277 tp##_is_signaling_nan(b->fld) || \
2278 tp##_is_signaling_nan(c->fld)) { \
2279 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2280 tstat.float_exception_flags &= ~float_flag_invalid; \
2282 if ((tp##_is_infinity(xa.fld) && tp##_is_zero(b->fld)) || \
2283 (tp##_is_zero(xa.fld) && tp##_is_infinity(b->fld))) { \
2284 xt_out.fld = float64_to_##tp(fload_invalid_op_excp(env, \
2285 POWERPC_EXCP_FP_VXIMZ, sfprf), &env->fp_status); \
2286 tstat.float_exception_flags &= ~float_flag_invalid; \
2288 if ((tstat.float_exception_flags & float_flag_invalid) && \
2289 ((tp##_is_infinity(xa.fld) || \
2290 tp##_is_infinity(b->fld)) && \
2291 tp##_is_infinity(c->fld))) { \
2292 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
2297 xt_out.fld = helper_frsp(env, xt_out.fld); \
2301 helper_compute_fprf(env, xt_out.fld); \
2304 putVSR(xT(opcode), &xt_out, env); \
2305 helper_float_check_status(env); \
2309 #define MSUB_FLGS float_muladd_negate_c
2310 #define NMADD_FLGS float_muladd_negate_result
2311 #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
2313 VSX_MADD(xsmaddadp
, 1, float64
, VsrD(0), MADD_FLGS
, 1, 1, 0)
2314 VSX_MADD(xsmaddmdp
, 1, float64
, VsrD(0), MADD_FLGS
, 0, 1, 0)
2315 VSX_MADD(xsmsubadp
, 1, float64
, VsrD(0), MSUB_FLGS
, 1, 1, 0)
2316 VSX_MADD(xsmsubmdp
, 1, float64
, VsrD(0), MSUB_FLGS
, 0, 1, 0)
2317 VSX_MADD(xsnmaddadp
, 1, float64
, VsrD(0), NMADD_FLGS
, 1, 1, 0)
2318 VSX_MADD(xsnmaddmdp
, 1, float64
, VsrD(0), NMADD_FLGS
, 0, 1, 0)
2319 VSX_MADD(xsnmsubadp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 1, 1, 0)
2320 VSX_MADD(xsnmsubmdp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 0, 1, 0)
2322 VSX_MADD(xsmaddasp
, 1, float64
, VsrD(0), MADD_FLGS
, 1, 1, 1)
2323 VSX_MADD(xsmaddmsp
, 1, float64
, VsrD(0), MADD_FLGS
, 0, 1, 1)
2324 VSX_MADD(xsmsubasp
, 1, float64
, VsrD(0), MSUB_FLGS
, 1, 1, 1)
2325 VSX_MADD(xsmsubmsp
, 1, float64
, VsrD(0), MSUB_FLGS
, 0, 1, 1)
2326 VSX_MADD(xsnmaddasp
, 1, float64
, VsrD(0), NMADD_FLGS
, 1, 1, 1)
2327 VSX_MADD(xsnmaddmsp
, 1, float64
, VsrD(0), NMADD_FLGS
, 0, 1, 1)
2328 VSX_MADD(xsnmsubasp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 1, 1, 1)
2329 VSX_MADD(xsnmsubmsp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 0, 1, 1)
2331 VSX_MADD(xvmaddadp
, 2, float64
, VsrD(i
), MADD_FLGS
, 1, 0, 0)
2332 VSX_MADD(xvmaddmdp
, 2, float64
, VsrD(i
), MADD_FLGS
, 0, 0, 0)
2333 VSX_MADD(xvmsubadp
, 2, float64
, VsrD(i
), MSUB_FLGS
, 1, 0, 0)
2334 VSX_MADD(xvmsubmdp
, 2, float64
, VsrD(i
), MSUB_FLGS
, 0, 0, 0)
2335 VSX_MADD(xvnmaddadp
, 2, float64
, VsrD(i
), NMADD_FLGS
, 1, 0, 0)
2336 VSX_MADD(xvnmaddmdp
, 2, float64
, VsrD(i
), NMADD_FLGS
, 0, 0, 0)
2337 VSX_MADD(xvnmsubadp
, 2, float64
, VsrD(i
), NMSUB_FLGS
, 1, 0, 0)
2338 VSX_MADD(xvnmsubmdp
, 2, float64
, VsrD(i
), NMSUB_FLGS
, 0, 0, 0)
2340 VSX_MADD(xvmaddasp
, 4, float32
, VsrW(i
), MADD_FLGS
, 1, 0, 0)
2341 VSX_MADD(xvmaddmsp
, 4, float32
, VsrW(i
), MADD_FLGS
, 0, 0, 0)
2342 VSX_MADD(xvmsubasp
, 4, float32
, VsrW(i
), MSUB_FLGS
, 1, 0, 0)
2343 VSX_MADD(xvmsubmsp
, 4, float32
, VsrW(i
), MSUB_FLGS
, 0, 0, 0)
2344 VSX_MADD(xvnmaddasp
, 4, float32
, VsrW(i
), NMADD_FLGS
, 1, 0, 0)
2345 VSX_MADD(xvnmaddmsp
, 4, float32
, VsrW(i
), NMADD_FLGS
, 0, 0, 0)
2346 VSX_MADD(xvnmsubasp
, 4, float32
, VsrW(i
), NMSUB_FLGS
, 1, 0, 0)
2347 VSX_MADD(xvnmsubmsp
, 4, float32
, VsrW(i
), NMSUB_FLGS
, 0, 0, 0)
2349 #define VSX_SCALAR_CMP(op, ordered) \
2350 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2355 getVSR(xA(opcode), &xa, env); \
2356 getVSR(xB(opcode), &xb, env); \
2358 if (unlikely(float64_is_any_nan(xa.VsrD(0)) || \
2359 float64_is_any_nan(xb.VsrD(0)))) { \
2360 if (float64_is_signaling_nan(xa.VsrD(0)) || \
2361 float64_is_signaling_nan(xb.VsrD(0))) { \
2362 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2365 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2369 if (float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) { \
2371 } else if (!float64_le(xa.VsrD(0), xb.VsrD(0), \
2372 &env->fp_status)) { \
2379 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
2380 env->fpscr |= cc << FPSCR_FPRF; \
2381 env->crf[BF(opcode)] = cc; \
2383 helper_float_check_status(env); \
2386 VSX_SCALAR_CMP(xscmpodp
, 1)
2387 VSX_SCALAR_CMP(xscmpudp
, 0)
2389 /* VSX_MAX_MIN - VSX floating point maximum/minimum
2390 * name - instruction mnemonic
2391 * op - operation (max or min)
2392 * nels - number of elements (1, 2 or 4)
2393 * tp - type (float32 or float64)
2394 * fld - vsr_t field (VsrD(*) or VsrW(*))
2396 #define VSX_MAX_MIN(name, op, nels, tp, fld) \
2397 void helper_##name(CPUPPCState *env, uint32_t opcode) \
2399 ppc_vsr_t xt, xa, xb; \
2402 getVSR(xA(opcode), &xa, env); \
2403 getVSR(xB(opcode), &xb, env); \
2404 getVSR(xT(opcode), &xt, env); \
2406 for (i = 0; i < nels; i++) { \
2407 xt.fld = tp##_##op(xa.fld, xb.fld, &env->fp_status); \
2408 if (unlikely(tp##_is_signaling_nan(xa.fld) || \
2409 tp##_is_signaling_nan(xb.fld))) { \
2410 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2414 putVSR(xT(opcode), &xt, env); \
2415 helper_float_check_status(env); \
2418 VSX_MAX_MIN(xsmaxdp
, maxnum
, 1, float64
, VsrD(0))
2419 VSX_MAX_MIN(xvmaxdp
, maxnum
, 2, float64
, VsrD(i
))
2420 VSX_MAX_MIN(xvmaxsp
, maxnum
, 4, float32
, VsrW(i
))
2421 VSX_MAX_MIN(xsmindp
, minnum
, 1, float64
, VsrD(0))
2422 VSX_MAX_MIN(xvmindp
, minnum
, 2, float64
, VsrD(i
))
2423 VSX_MAX_MIN(xvminsp
, minnum
, 4, float32
, VsrW(i
))
2425 /* VSX_CMP - VSX floating point compare
2426 * op - instruction mnemonic
2427 * nels - number of elements (1, 2 or 4)
2428 * tp - type (float32 or float64)
2429 * fld - vsr_t field (VsrD(*) or VsrW(*))
2430 * cmp - comparison operation
2431 * svxvc - set VXVC bit
2433 #define VSX_CMP(op, nels, tp, fld, cmp, svxvc) \
2434 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2436 ppc_vsr_t xt, xa, xb; \
2439 int all_false = 1; \
2441 getVSR(xA(opcode), &xa, env); \
2442 getVSR(xB(opcode), &xb, env); \
2443 getVSR(xT(opcode), &xt, env); \
2445 for (i = 0; i < nels; i++) { \
2446 if (unlikely(tp##_is_any_nan(xa.fld) || \
2447 tp##_is_any_nan(xb.fld))) { \
2448 if (tp##_is_signaling_nan(xa.fld) || \
2449 tp##_is_signaling_nan(xb.fld)) { \
2450 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2453 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2458 if (tp##_##cmp(xb.fld, xa.fld, &env->fp_status) == 1) { \
2468 putVSR(xT(opcode), &xt, env); \
2469 if ((opcode >> (31-21)) & 1) { \
2470 env->crf[6] = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0); \
2472 helper_float_check_status(env); \
2475 VSX_CMP(xvcmpeqdp
, 2, float64
, VsrD(i
), eq
, 0)
2476 VSX_CMP(xvcmpgedp
, 2, float64
, VsrD(i
), le
, 1)
2477 VSX_CMP(xvcmpgtdp
, 2, float64
, VsrD(i
), lt
, 1)
2478 VSX_CMP(xvcmpeqsp
, 4, float32
, VsrW(i
), eq
, 0)
2479 VSX_CMP(xvcmpgesp
, 4, float32
, VsrW(i
), le
, 1)
2480 VSX_CMP(xvcmpgtsp
, 4, float32
, VsrW(i
), lt
, 1)
2482 /* VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2483 * op - instruction mnemonic
2484 * nels - number of elements (1, 2 or 4)
2485 * stp - source type (float32 or float64)
2486 * ttp - target type (float32 or float64)
2487 * sfld - source vsr_t field
2488 * tfld - target vsr_t field (f32 or f64)
2491 #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf) \
2492 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2497 getVSR(xB(opcode), &xb, env); \
2498 getVSR(xT(opcode), &xt, env); \
2500 for (i = 0; i < nels; i++) { \
2501 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2502 if (unlikely(stp##_is_signaling_nan(xb.sfld))) { \
2503 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2504 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2507 helper_compute_fprf(env, ttp##_to_float64(xt.tfld, \
2508 &env->fp_status)); \
2512 putVSR(xT(opcode), &xt, env); \
2513 helper_float_check_status(env); \
2516 VSX_CVT_FP_TO_FP(xscvdpsp
, 1, float64
, float32
, VsrD(0), VsrW(0), 1)
2517 VSX_CVT_FP_TO_FP(xscvspdp
, 1, float32
, float64
, VsrW(0), VsrD(0), 1)
2518 VSX_CVT_FP_TO_FP(xvcvdpsp
, 2, float64
, float32
, VsrD(i
), VsrW(2*i
), 0)
2519 VSX_CVT_FP_TO_FP(xvcvspdp
, 2, float32
, float64
, VsrW(2*i
), VsrD(i
), 0)
2521 uint64_t helper_xscvdpspn(CPUPPCState
*env
, uint64_t xb
)
2523 float_status tstat
= env
->fp_status
;
2524 set_float_exception_flags(0, &tstat
);
2526 return (uint64_t)float64_to_float32(xb
, &tstat
) << 32;
2529 uint64_t helper_xscvspdpn(CPUPPCState
*env
, uint64_t xb
)
2531 float_status tstat
= env
->fp_status
;
2532 set_float_exception_flags(0, &tstat
);
2534 return float32_to_float64(xb
>> 32, &tstat
);
2537 /* VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2538 * op - instruction mnemonic
2539 * nels - number of elements (1, 2 or 4)
2540 * stp - source type (float32 or float64)
2541 * ttp - target type (int32, uint32, int64 or uint64)
2542 * sfld - source vsr_t field
2543 * tfld - target vsr_t field
2544 * rnan - resulting NaN
2546 #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, rnan) \
2547 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2552 getVSR(xB(opcode), &xb, env); \
2553 getVSR(xT(opcode), &xt, env); \
2555 for (i = 0; i < nels; i++) { \
2556 if (unlikely(stp##_is_any_nan(xb.sfld))) { \
2557 if (stp##_is_signaling_nan(xb.sfld)) { \
2558 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2560 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2563 xt.tfld = stp##_to_##ttp##_round_to_zero(xb.sfld, \
2565 if (env->fp_status.float_exception_flags & float_flag_invalid) { \
2566 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2571 putVSR(xT(opcode), &xt, env); \
2572 helper_float_check_status(env); \
2575 VSX_CVT_FP_TO_INT(xscvdpsxds
, 1, float64
, int64
, VsrD(0), VsrD(0), \
2576 0x8000000000000000ULL
)
2577 VSX_CVT_FP_TO_INT(xscvdpsxws
, 1, float64
, int32
, VsrD(0), VsrW(1), \
2579 VSX_CVT_FP_TO_INT(xscvdpuxds
, 1, float64
, uint64
, VsrD(0), VsrD(0), 0ULL)
2580 VSX_CVT_FP_TO_INT(xscvdpuxws
, 1, float64
, uint32
, VsrD(0), VsrW(1), 0U)
2581 VSX_CVT_FP_TO_INT(xvcvdpsxds
, 2, float64
, int64
, VsrD(i
), VsrD(i
), \
2582 0x8000000000000000ULL
)
2583 VSX_CVT_FP_TO_INT(xvcvdpsxws
, 2, float64
, int32
, VsrD(i
), VsrW(2*i
), \
2585 VSX_CVT_FP_TO_INT(xvcvdpuxds
, 2, float64
, uint64
, VsrD(i
), VsrD(i
), 0ULL)
2586 VSX_CVT_FP_TO_INT(xvcvdpuxws
, 2, float64
, uint32
, VsrD(i
), VsrW(2*i
), 0U)
2587 VSX_CVT_FP_TO_INT(xvcvspsxds
, 2, float32
, int64
, VsrW(2*i
), VsrD(i
), \
2588 0x8000000000000000ULL
)
2589 VSX_CVT_FP_TO_INT(xvcvspsxws
, 4, float32
, int32
, VsrW(i
), VsrW(i
), 0x80000000U
)
2590 VSX_CVT_FP_TO_INT(xvcvspuxds
, 2, float32
, uint64
, VsrW(2*i
), VsrD(i
), 0ULL)
2591 VSX_CVT_FP_TO_INT(xvcvspuxws
, 4, float32
, uint32
, VsrW(i
), VsrW(i
), 0U)
2593 /* VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
2594 * op - instruction mnemonic
2595 * nels - number of elements (1, 2 or 4)
2596 * stp - source type (int32, uint32, int64 or uint64)
2597 * ttp - target type (float32 or float64)
2598 * sfld - source vsr_t field
2599 * tfld - target vsr_t field
2600 * jdef - definition of the j index (i or 2*i)
2603 #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf, r2sp) \
2604 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2609 getVSR(xB(opcode), &xb, env); \
2610 getVSR(xT(opcode), &xt, env); \
2612 for (i = 0; i < nels; i++) { \
2613 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2615 xt.tfld = helper_frsp(env, xt.tfld); \
2618 helper_compute_fprf(env, xt.tfld); \
2622 putVSR(xT(opcode), &xt, env); \
2623 helper_float_check_status(env); \
2626 VSX_CVT_INT_TO_FP(xscvsxddp
, 1, int64
, float64
, VsrD(0), VsrD(0), 1, 0)
2627 VSX_CVT_INT_TO_FP(xscvuxddp
, 1, uint64
, float64
, VsrD(0), VsrD(0), 1, 0)
2628 VSX_CVT_INT_TO_FP(xscvsxdsp
, 1, int64
, float64
, VsrD(0), VsrD(0), 1, 1)
2629 VSX_CVT_INT_TO_FP(xscvuxdsp
, 1, uint64
, float64
, VsrD(0), VsrD(0), 1, 1)
2630 VSX_CVT_INT_TO_FP(xvcvsxddp
, 2, int64
, float64
, VsrD(i
), VsrD(i
), 0, 0)
2631 VSX_CVT_INT_TO_FP(xvcvuxddp
, 2, uint64
, float64
, VsrD(i
), VsrD(i
), 0, 0)
2632 VSX_CVT_INT_TO_FP(xvcvsxwdp
, 2, int32
, float64
, VsrW(2*i
), VsrD(i
), 0, 0)
2633 VSX_CVT_INT_TO_FP(xvcvuxwdp
, 2, uint64
, float64
, VsrW(2*i
), VsrD(i
), 0, 0)
2634 VSX_CVT_INT_TO_FP(xvcvsxdsp
, 2, int64
, float32
, VsrD(i
), VsrW(2*i
), 0, 0)
2635 VSX_CVT_INT_TO_FP(xvcvuxdsp
, 2, uint64
, float32
, VsrD(i
), VsrW(2*i
), 0, 0)
2636 VSX_CVT_INT_TO_FP(xvcvsxwsp
, 4, int32
, float32
, VsrW(i
), VsrW(i
), 0, 0)
2637 VSX_CVT_INT_TO_FP(xvcvuxwsp
, 4, uint32
, float32
, VsrW(i
), VsrW(i
), 0, 0)
2639 /* For "use current rounding mode", define a value that will not be one of
2640 * the existing rounding model enums.
2642 #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
2643 float_round_up + float_round_to_zero)
2645 /* VSX_ROUND - VSX floating point round
2646 * op - instruction mnemonic
2647 * nels - number of elements (1, 2 or 4)
2648 * tp - type (float32 or float64)
2649 * fld - vsr_t field (VsrD(*) or VsrW(*))
2650 * rmode - rounding mode
2653 #define VSX_ROUND(op, nels, tp, fld, rmode, sfprf) \
2654 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2658 getVSR(xB(opcode), &xb, env); \
2659 getVSR(xT(opcode), &xt, env); \
2661 if (rmode != FLOAT_ROUND_CURRENT) { \
2662 set_float_rounding_mode(rmode, &env->fp_status); \
2665 for (i = 0; i < nels; i++) { \
2666 if (unlikely(tp##_is_signaling_nan(xb.fld))) { \
2667 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2668 xt.fld = tp##_snan_to_qnan(xb.fld); \
2670 xt.fld = tp##_round_to_int(xb.fld, &env->fp_status); \
2673 helper_compute_fprf(env, xt.fld); \
2677 /* If this is not a "use current rounding mode" instruction, \
2678 * then inhibit setting of the XX bit and restore rounding \
2679 * mode from FPSCR */ \
2680 if (rmode != FLOAT_ROUND_CURRENT) { \
2681 fpscr_set_rounding_mode(env); \
2682 env->fp_status.float_exception_flags &= ~float_flag_inexact; \
2685 putVSR(xT(opcode), &xt, env); \
2686 helper_float_check_status(env); \
2689 VSX_ROUND(xsrdpi
, 1, float64
, VsrD(0), float_round_nearest_even
, 1)
2690 VSX_ROUND(xsrdpic
, 1, float64
, VsrD(0), FLOAT_ROUND_CURRENT
, 1)
2691 VSX_ROUND(xsrdpim
, 1, float64
, VsrD(0), float_round_down
, 1)
2692 VSX_ROUND(xsrdpip
, 1, float64
, VsrD(0), float_round_up
, 1)
2693 VSX_ROUND(xsrdpiz
, 1, float64
, VsrD(0), float_round_to_zero
, 1)
2695 VSX_ROUND(xvrdpi
, 2, float64
, VsrD(i
), float_round_nearest_even
, 0)
2696 VSX_ROUND(xvrdpic
, 2, float64
, VsrD(i
), FLOAT_ROUND_CURRENT
, 0)
2697 VSX_ROUND(xvrdpim
, 2, float64
, VsrD(i
), float_round_down
, 0)
2698 VSX_ROUND(xvrdpip
, 2, float64
, VsrD(i
), float_round_up
, 0)
2699 VSX_ROUND(xvrdpiz
, 2, float64
, VsrD(i
), float_round_to_zero
, 0)
2701 VSX_ROUND(xvrspi
, 4, float32
, VsrW(i
), float_round_nearest_even
, 0)
2702 VSX_ROUND(xvrspic
, 4, float32
, VsrW(i
), FLOAT_ROUND_CURRENT
, 0)
2703 VSX_ROUND(xvrspim
, 4, float32
, VsrW(i
), float_round_down
, 0)
2704 VSX_ROUND(xvrspip
, 4, float32
, VsrW(i
), float_round_up
, 0)
2705 VSX_ROUND(xvrspiz
, 4, float32
, VsrW(i
), float_round_to_zero
, 0)
2707 uint64_t helper_xsrsp(CPUPPCState
*env
, uint64_t xb
)
2709 helper_reset_fpstatus(env
);
2711 uint64_t xt
= helper_frsp(env
, xb
);
2713 helper_compute_fprf(env
, xt
);
2714 helper_float_check_status(env
);