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/>.
19 #include "qemu/osdep.h"
21 #include "exec/helper-proto.h"
22 #include "exec/exec-all.h"
25 static inline float128
float128_snan_to_qnan(float128 x
)
29 r
.high
= x
.high
| 0x0000800000000000;
34 #define float64_snan_to_qnan(x) ((x) | 0x0008000000000000ULL)
35 #define float32_snan_to_qnan(x) ((x) | 0x00400000)
36 #define float16_snan_to_qnan(x) ((x) | 0x0200)
38 /*****************************************************************************/
39 /* Floating point operations helpers */
40 uint64_t helper_float32_to_float64(CPUPPCState
*env
, uint32_t arg
)
46 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
50 uint32_t helper_float64_to_float32(CPUPPCState
*env
, uint64_t arg
)
56 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
60 static inline int ppc_float32_get_unbiased_exp(float32 f
)
62 return ((f
>> 23) & 0xFF) - 127;
65 static inline int ppc_float64_get_unbiased_exp(float64 f
)
67 return ((f
>> 52) & 0x7FF) - 1023;
70 #define COMPUTE_FPRF(tp) \
71 void helper_compute_fprf_##tp(CPUPPCState *env, tp arg) \
76 isneg = tp##_is_neg(arg); \
77 if (unlikely(tp##_is_any_nan(arg))) { \
78 if (tp##_is_signaling_nan(arg, &env->fp_status)) { \
79 /* Signaling NaN: flags are undefined */ \
85 } else if (unlikely(tp##_is_infinity(arg))) { \
93 if (tp##_is_zero(arg)) { \
101 if (tp##_is_zero_or_denormal(arg)) { \
102 /* Denormalized numbers */ \
105 /* Normalized numbers */ \
115 /* We update FPSCR_FPRF */ \
116 env->fpscr &= ~(0x1F << FPSCR_FPRF); \
117 env->fpscr |= fprf << FPSCR_FPRF; \
120 COMPUTE_FPRF(float16
)
121 COMPUTE_FPRF(float32
)
122 COMPUTE_FPRF(float64
)
123 COMPUTE_FPRF(float128
)
125 /* Floating-point invalid operations exception */
126 static inline __attribute__((__always_inline__
))
127 uint64_t float_invalid_op_excp(CPUPPCState
*env
, int op
, int set_fpcc
)
129 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
135 case POWERPC_EXCP_FP_VXSNAN
:
136 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
138 case POWERPC_EXCP_FP_VXSOFT
:
139 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
141 case POWERPC_EXCP_FP_VXISI
:
142 /* Magnitude subtraction of infinities */
143 env
->fpscr
|= 1 << FPSCR_VXISI
;
145 case POWERPC_EXCP_FP_VXIDI
:
146 /* Division of infinity by infinity */
147 env
->fpscr
|= 1 << FPSCR_VXIDI
;
149 case POWERPC_EXCP_FP_VXZDZ
:
150 /* Division of zero by zero */
151 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
153 case POWERPC_EXCP_FP_VXIMZ
:
154 /* Multiplication of zero by infinity */
155 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
157 case POWERPC_EXCP_FP_VXVC
:
158 /* Ordered comparison of NaN */
159 env
->fpscr
|= 1 << FPSCR_VXVC
;
161 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
162 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
164 /* We must update the target FPR before raising the exception */
166 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
167 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
168 /* Update the floating-point enabled exception summary */
169 env
->fpscr
|= 1 << FPSCR_FEX
;
170 /* Exception is differed */
174 case POWERPC_EXCP_FP_VXSQRT
:
175 /* Square root of a negative number */
176 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
178 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
180 /* Set the result to quiet NaN */
181 ret
= 0x7FF8000000000000ULL
;
183 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
184 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
188 case POWERPC_EXCP_FP_VXCVI
:
189 /* Invalid conversion */
190 env
->fpscr
|= 1 << FPSCR_VXCVI
;
191 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
193 /* Set the result to quiet NaN */
194 ret
= 0x7FF8000000000000ULL
;
196 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
197 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
202 /* Update the floating-point invalid operation summary */
203 env
->fpscr
|= 1 << FPSCR_VX
;
204 /* Update the floating-point exception summary */
207 /* Update the floating-point enabled exception summary */
208 env
->fpscr
|= 1 << FPSCR_FEX
;
209 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
210 /* GETPC() works here because this is inline */
211 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
212 POWERPC_EXCP_FP
| op
, GETPC());
218 static inline void float_zero_divide_excp(CPUPPCState
*env
, uintptr_t raddr
)
220 env
->fpscr
|= 1 << FPSCR_ZX
;
221 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
222 /* Update the floating-point exception summary */
225 /* Update the floating-point enabled exception summary */
226 env
->fpscr
|= 1 << FPSCR_FEX
;
227 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
228 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
229 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
,
235 static inline void float_overflow_excp(CPUPPCState
*env
)
237 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
239 env
->fpscr
|= 1 << FPSCR_OX
;
240 /* Update the floating-point exception summary */
243 /* XXX: should adjust the result */
244 /* Update the floating-point enabled exception summary */
245 env
->fpscr
|= 1 << FPSCR_FEX
;
246 /* We must update the target FPR before raising the exception */
247 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
248 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
250 env
->fpscr
|= 1 << FPSCR_XX
;
251 env
->fpscr
|= 1 << FPSCR_FI
;
255 static inline void float_underflow_excp(CPUPPCState
*env
)
257 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
259 env
->fpscr
|= 1 << FPSCR_UX
;
260 /* Update the floating-point exception summary */
263 /* XXX: should adjust the result */
264 /* Update the floating-point enabled exception summary */
265 env
->fpscr
|= 1 << FPSCR_FEX
;
266 /* We must update the target FPR before raising the exception */
267 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
268 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
272 static inline void float_inexact_excp(CPUPPCState
*env
)
274 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
276 env
->fpscr
|= 1 << FPSCR_XX
;
277 /* Update the floating-point exception summary */
280 /* Update the floating-point enabled exception summary */
281 env
->fpscr
|= 1 << FPSCR_FEX
;
282 /* We must update the target FPR before raising the exception */
283 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
284 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
288 static inline void fpscr_set_rounding_mode(CPUPPCState
*env
)
292 /* Set rounding mode */
295 /* Best approximation (round to nearest) */
296 rnd_type
= float_round_nearest_even
;
299 /* Smaller magnitude (round toward zero) */
300 rnd_type
= float_round_to_zero
;
303 /* Round toward +infinite */
304 rnd_type
= float_round_up
;
308 /* Round toward -infinite */
309 rnd_type
= float_round_down
;
312 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
315 void helper_fpscr_clrbit(CPUPPCState
*env
, uint32_t bit
)
319 prev
= (env
->fpscr
>> bit
) & 1;
320 env
->fpscr
&= ~(1 << bit
);
325 fpscr_set_rounding_mode(env
);
333 void helper_fpscr_setbit(CPUPPCState
*env
, uint32_t bit
)
335 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
338 prev
= (env
->fpscr
>> bit
) & 1;
339 env
->fpscr
|= 1 << bit
;
381 env
->fpscr
|= 1 << FPSCR_VX
;
390 env
->error_code
= POWERPC_EXCP_FP
;
392 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
395 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
398 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
401 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
404 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
407 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
410 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
413 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
416 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
424 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
431 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
438 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
445 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
451 fpscr_set_rounding_mode(env
);
456 /* Update the floating-point enabled exception summary */
457 env
->fpscr
|= 1 << FPSCR_FEX
;
458 /* We have to update Rc1 before raising the exception */
459 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
465 void helper_store_fpscr(CPUPPCState
*env
, uint64_t arg
, uint32_t mask
)
467 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
468 target_ulong prev
, new;
472 new = (target_ulong
)arg
;
473 new &= ~0x60000000LL
;
474 new |= prev
& 0x60000000LL
;
475 for (i
= 0; i
< sizeof(target_ulong
) * 2; i
++) {
476 if (mask
& (1 << i
)) {
477 env
->fpscr
&= ~(0xFLL
<< (4 * i
));
478 env
->fpscr
|= new & (0xFLL
<< (4 * i
));
481 /* Update VX and FEX */
483 env
->fpscr
|= 1 << FPSCR_VX
;
485 env
->fpscr
&= ~(1 << FPSCR_VX
);
487 if ((fpscr_ex
& fpscr_eex
) != 0) {
488 env
->fpscr
|= 1 << FPSCR_FEX
;
489 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
490 /* XXX: we should compute it properly */
491 env
->error_code
= POWERPC_EXCP_FP
;
493 env
->fpscr
&= ~(1 << FPSCR_FEX
);
495 fpscr_set_rounding_mode(env
);
498 void store_fpscr(CPUPPCState
*env
, uint64_t arg
, uint32_t mask
)
500 helper_store_fpscr(env
, arg
, mask
);
503 static void do_float_check_status(CPUPPCState
*env
, uintptr_t raddr
)
505 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
506 int status
= get_float_exception_flags(&env
->fp_status
);
508 if (status
& float_flag_divbyzero
) {
509 float_zero_divide_excp(env
, raddr
);
510 } else if (status
& float_flag_overflow
) {
511 float_overflow_excp(env
);
512 } else if (status
& float_flag_underflow
) {
513 float_underflow_excp(env
);
514 } else if (status
& float_flag_inexact
) {
515 float_inexact_excp(env
);
518 if (cs
->exception_index
== POWERPC_EXCP_PROGRAM
&&
519 (env
->error_code
& POWERPC_EXCP_FP
)) {
520 /* Differred floating-point exception after target FPR update */
521 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
522 raise_exception_err_ra(env
, cs
->exception_index
,
523 env
->error_code
, raddr
);
528 static inline __attribute__((__always_inline__
))
529 void float_check_status(CPUPPCState
*env
)
531 /* GETPC() works here because this is inline */
532 do_float_check_status(env
, GETPC());
535 void helper_float_check_status(CPUPPCState
*env
)
537 do_float_check_status(env
, GETPC());
540 void helper_reset_fpstatus(CPUPPCState
*env
)
542 set_float_exception_flags(0, &env
->fp_status
);
546 uint64_t helper_fadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
548 CPU_DoubleU farg1
, farg2
;
553 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
554 float64_is_neg(farg1
.d
) != float64_is_neg(farg2
.d
))) {
555 /* Magnitude subtraction of infinities */
556 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
558 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
559 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
))) {
561 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
563 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
570 uint64_t helper_fsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
572 CPU_DoubleU farg1
, farg2
;
577 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
578 float64_is_neg(farg1
.d
) == float64_is_neg(farg2
.d
))) {
579 /* Magnitude subtraction of infinities */
580 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
582 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
583 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
))) {
584 /* sNaN subtraction */
585 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
587 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
594 uint64_t helper_fmul(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
596 CPU_DoubleU farg1
, farg2
;
601 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
602 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
603 /* Multiplication of zero by infinity */
604 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
606 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
607 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
))) {
608 /* sNaN multiplication */
609 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
611 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
618 uint64_t helper_fdiv(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
620 CPU_DoubleU farg1
, farg2
;
625 if (unlikely(float64_is_infinity(farg1
.d
) &&
626 float64_is_infinity(farg2
.d
))) {
627 /* Division of infinity by infinity */
628 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIDI
, 1);
629 } else if (unlikely(float64_is_zero(farg1
.d
) && float64_is_zero(farg2
.d
))) {
630 /* Division of zero by zero */
631 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXZDZ
, 1);
633 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
634 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
))) {
636 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
638 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
645 #define FPU_FCTI(op, cvt, nanval) \
646 uint64_t helper_##op(CPUPPCState *env, uint64_t arg) \
651 farg.ll = float64_to_##cvt(farg.d, &env->fp_status); \
653 if (unlikely(env->fp_status.float_exception_flags)) { \
654 if (float64_is_any_nan(arg)) { \
655 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 1); \
656 if (float64_is_signaling_nan(arg, &env->fp_status)) { \
657 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1); \
660 } else if (env->fp_status.float_exception_flags & \
661 float_flag_invalid) { \
662 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 1); \
664 float_check_status(env); \
669 FPU_FCTI(fctiw
, int32
, 0x80000000U
)
670 FPU_FCTI(fctiwz
, int32_round_to_zero
, 0x80000000U
)
671 FPU_FCTI(fctiwu
, uint32
, 0x00000000U
)
672 FPU_FCTI(fctiwuz
, uint32_round_to_zero
, 0x00000000U
)
673 FPU_FCTI(fctid
, int64
, 0x8000000000000000ULL
)
674 FPU_FCTI(fctidz
, int64_round_to_zero
, 0x8000000000000000ULL
)
675 FPU_FCTI(fctidu
, uint64
, 0x0000000000000000ULL
)
676 FPU_FCTI(fctiduz
, uint64_round_to_zero
, 0x0000000000000000ULL
)
678 #define FPU_FCFI(op, cvtr, is_single) \
679 uint64_t helper_##op(CPUPPCState *env, uint64_t arg) \
684 float32 tmp = cvtr(arg, &env->fp_status); \
685 farg.d = float32_to_float64(tmp, &env->fp_status); \
687 farg.d = cvtr(arg, &env->fp_status); \
689 float_check_status(env); \
693 FPU_FCFI(fcfid
, int64_to_float64
, 0)
694 FPU_FCFI(fcfids
, int64_to_float32
, 1)
695 FPU_FCFI(fcfidu
, uint64_to_float64
, 0)
696 FPU_FCFI(fcfidus
, uint64_to_float32
, 1)
698 static inline uint64_t do_fri(CPUPPCState
*env
, uint64_t arg
,
705 if (unlikely(float64_is_signaling_nan(farg
.d
, &env
->fp_status
))) {
707 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
708 farg
.ll
= arg
| 0x0008000000000000ULL
;
710 int inexact
= get_float_exception_flags(&env
->fp_status
) &
712 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
713 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
714 /* Restore rounding mode from FPSCR */
715 fpscr_set_rounding_mode(env
);
717 /* fri* does not set FPSCR[XX] */
719 env
->fp_status
.float_exception_flags
&= ~float_flag_inexact
;
722 float_check_status(env
);
726 uint64_t helper_frin(CPUPPCState
*env
, uint64_t arg
)
728 return do_fri(env
, arg
, float_round_ties_away
);
731 uint64_t helper_friz(CPUPPCState
*env
, uint64_t arg
)
733 return do_fri(env
, arg
, float_round_to_zero
);
736 uint64_t helper_frip(CPUPPCState
*env
, uint64_t arg
)
738 return do_fri(env
, arg
, float_round_up
);
741 uint64_t helper_frim(CPUPPCState
*env
, uint64_t arg
)
743 return do_fri(env
, arg
, float_round_down
);
747 uint64_t helper_fmadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
750 CPU_DoubleU farg1
, farg2
, farg3
;
756 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
757 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
758 /* Multiplication of zero by infinity */
759 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
761 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
762 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
) ||
763 float64_is_signaling_nan(farg3
.d
, &env
->fp_status
))) {
765 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
767 /* This is the way the PowerPC specification defines it */
768 float128 ft0_128
, ft1_128
;
770 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
771 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
772 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
773 if (unlikely(float128_is_infinity(ft0_128
) &&
774 float64_is_infinity(farg3
.d
) &&
775 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
776 /* Magnitude subtraction of infinities */
777 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
779 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
780 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
781 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
789 uint64_t helper_fmsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
792 CPU_DoubleU farg1
, farg2
, farg3
;
798 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
799 (float64_is_zero(farg1
.d
) &&
800 float64_is_infinity(farg2
.d
)))) {
801 /* Multiplication of zero by infinity */
802 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
804 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
805 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
) ||
806 float64_is_signaling_nan(farg3
.d
, &env
->fp_status
))) {
808 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
810 /* This is the way the PowerPC specification defines it */
811 float128 ft0_128
, ft1_128
;
813 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
814 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
815 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
816 if (unlikely(float128_is_infinity(ft0_128
) &&
817 float64_is_infinity(farg3
.d
) &&
818 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
819 /* Magnitude subtraction of infinities */
820 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
822 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
823 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
824 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
830 /* fnmadd - fnmadd. */
831 uint64_t helper_fnmadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
834 CPU_DoubleU farg1
, farg2
, farg3
;
840 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
841 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
842 /* Multiplication of zero by infinity */
843 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
845 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
846 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
) ||
847 float64_is_signaling_nan(farg3
.d
, &env
->fp_status
))) {
849 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
851 /* This is the way the PowerPC specification defines it */
852 float128 ft0_128
, ft1_128
;
854 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
855 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
856 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
857 if (unlikely(float128_is_infinity(ft0_128
) &&
858 float64_is_infinity(farg3
.d
) &&
859 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
860 /* Magnitude subtraction of infinities */
861 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
863 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
864 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
865 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
867 if (likely(!float64_is_any_nan(farg1
.d
))) {
868 farg1
.d
= float64_chs(farg1
.d
);
874 /* fnmsub - fnmsub. */
875 uint64_t helper_fnmsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
878 CPU_DoubleU farg1
, farg2
, farg3
;
884 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
885 (float64_is_zero(farg1
.d
) &&
886 float64_is_infinity(farg2
.d
)))) {
887 /* Multiplication of zero by infinity */
888 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
890 if (unlikely(float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
891 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
) ||
892 float64_is_signaling_nan(farg3
.d
, &env
->fp_status
))) {
894 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
896 /* This is the way the PowerPC specification defines it */
897 float128 ft0_128
, ft1_128
;
899 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
900 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
901 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
902 if (unlikely(float128_is_infinity(ft0_128
) &&
903 float64_is_infinity(farg3
.d
) &&
904 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
905 /* Magnitude subtraction of infinities */
906 farg1
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
908 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
909 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
910 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
912 if (likely(!float64_is_any_nan(farg1
.d
))) {
913 farg1
.d
= float64_chs(farg1
.d
);
920 uint64_t helper_frsp(CPUPPCState
*env
, uint64_t arg
)
927 if (unlikely(float64_is_signaling_nan(farg
.d
, &env
->fp_status
))) {
928 /* sNaN square root */
929 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
931 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
932 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
938 uint64_t helper_fsqrt(CPUPPCState
*env
, uint64_t arg
)
944 if (unlikely(float64_is_any_nan(farg
.d
))) {
945 if (unlikely(float64_is_signaling_nan(farg
.d
, &env
->fp_status
))) {
946 /* sNaN reciprocal square root */
947 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
948 farg
.ll
= float64_snan_to_qnan(farg
.ll
);
950 } else if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
951 /* Square root of a negative nonzero number */
952 farg
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSQRT
, 1);
954 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
960 uint64_t helper_fre(CPUPPCState
*env
, uint64_t arg
)
966 if (unlikely(float64_is_signaling_nan(farg
.d
, &env
->fp_status
))) {
967 /* sNaN reciprocal */
968 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
970 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
975 uint64_t helper_fres(CPUPPCState
*env
, uint64_t arg
)
982 if (unlikely(float64_is_signaling_nan(farg
.d
, &env
->fp_status
))) {
983 /* sNaN reciprocal */
984 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
986 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
987 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
988 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
993 /* frsqrte - frsqrte. */
994 uint64_t helper_frsqrte(CPUPPCState
*env
, uint64_t arg
)
1000 if (unlikely(float64_is_any_nan(farg
.d
))) {
1001 if (unlikely(float64_is_signaling_nan(farg
.d
, &env
->fp_status
))) {
1002 /* sNaN reciprocal square root */
1003 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
1004 farg
.ll
= float64_snan_to_qnan(farg
.ll
);
1006 } else if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
1007 /* Reciprocal square root of a negative nonzero number */
1008 farg
.ll
= float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSQRT
, 1);
1010 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
1011 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
1018 uint64_t helper_fsel(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1025 if ((!float64_is_neg(farg1
.d
) || float64_is_zero(farg1
.d
)) &&
1026 !float64_is_any_nan(farg1
.d
)) {
1033 uint32_t helper_ftdiv(uint64_t fra
, uint64_t frb
)
1038 if (unlikely(float64_is_infinity(fra
) ||
1039 float64_is_infinity(frb
) ||
1040 float64_is_zero(frb
))) {
1044 int e_a
= ppc_float64_get_unbiased_exp(fra
);
1045 int e_b
= ppc_float64_get_unbiased_exp(frb
);
1047 if (unlikely(float64_is_any_nan(fra
) ||
1048 float64_is_any_nan(frb
))) {
1050 } else if ((e_b
<= -1022) || (e_b
>= 1021)) {
1052 } else if (!float64_is_zero(fra
) &&
1053 (((e_a
- e_b
) >= 1023) ||
1054 ((e_a
- e_b
) <= -1021) ||
1059 if (unlikely(float64_is_zero_or_denormal(frb
))) {
1060 /* XB is not zero because of the above check and */
1061 /* so must be denormalized. */
1066 return 0x8 | (fg_flag
? 4 : 0) | (fe_flag
? 2 : 0);
1069 uint32_t helper_ftsqrt(uint64_t frb
)
1074 if (unlikely(float64_is_infinity(frb
) || float64_is_zero(frb
))) {
1078 int e_b
= ppc_float64_get_unbiased_exp(frb
);
1080 if (unlikely(float64_is_any_nan(frb
))) {
1082 } else if (unlikely(float64_is_zero(frb
))) {
1084 } else if (unlikely(float64_is_neg(frb
))) {
1086 } else if (!float64_is_zero(frb
) && (e_b
<= (-1022+52))) {
1090 if (unlikely(float64_is_zero_or_denormal(frb
))) {
1091 /* XB is not zero because of the above check and */
1092 /* therefore must be denormalized. */
1097 return 0x8 | (fg_flag
? 4 : 0) | (fe_flag
? 2 : 0);
1100 void helper_fcmpu(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1103 CPU_DoubleU farg1
, farg2
;
1109 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1110 float64_is_any_nan(farg2
.d
))) {
1112 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1114 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1120 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1121 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1122 env
->crf
[crfD
] = ret
;
1123 if (unlikely(ret
== 0x01UL
1124 && (float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
1125 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
)))) {
1126 /* sNaN comparison */
1127 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
1131 void helper_fcmpo(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1134 CPU_DoubleU farg1
, farg2
;
1140 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1141 float64_is_any_nan(farg2
.d
))) {
1143 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1145 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1151 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1152 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1153 env
->crf
[crfD
] = ret
;
1154 if (unlikely(ret
== 0x01UL
)) {
1155 if (float64_is_signaling_nan(farg1
.d
, &env
->fp_status
) ||
1156 float64_is_signaling_nan(farg2
.d
, &env
->fp_status
)) {
1157 /* sNaN comparison */
1158 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
|
1159 POWERPC_EXCP_FP_VXVC
, 1);
1161 /* qNaN comparison */
1162 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXVC
, 1);
1167 /* Single-precision floating-point conversions */
1168 static inline uint32_t efscfsi(CPUPPCState
*env
, uint32_t val
)
1172 u
.f
= int32_to_float32(val
, &env
->vec_status
);
1177 static inline uint32_t efscfui(CPUPPCState
*env
, uint32_t val
)
1181 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
1186 static inline int32_t efsctsi(CPUPPCState
*env
, uint32_t val
)
1191 /* NaN are not treated the same way IEEE 754 does */
1192 if (unlikely(float32_is_quiet_nan(u
.f
, &env
->vec_status
))) {
1196 return float32_to_int32(u
.f
, &env
->vec_status
);
1199 static inline uint32_t efsctui(CPUPPCState
*env
, uint32_t val
)
1204 /* NaN are not treated the same way IEEE 754 does */
1205 if (unlikely(float32_is_quiet_nan(u
.f
, &env
->vec_status
))) {
1209 return float32_to_uint32(u
.f
, &env
->vec_status
);
1212 static inline uint32_t efsctsiz(CPUPPCState
*env
, uint32_t val
)
1217 /* NaN are not treated the same way IEEE 754 does */
1218 if (unlikely(float32_is_quiet_nan(u
.f
, &env
->vec_status
))) {
1222 return float32_to_int32_round_to_zero(u
.f
, &env
->vec_status
);
1225 static inline uint32_t efsctuiz(CPUPPCState
*env
, uint32_t val
)
1230 /* NaN are not treated the same way IEEE 754 does */
1231 if (unlikely(float32_is_quiet_nan(u
.f
, &env
->vec_status
))) {
1235 return float32_to_uint32_round_to_zero(u
.f
, &env
->vec_status
);
1238 static inline uint32_t efscfsf(CPUPPCState
*env
, uint32_t val
)
1243 u
.f
= int32_to_float32(val
, &env
->vec_status
);
1244 tmp
= int64_to_float32(1ULL << 32, &env
->vec_status
);
1245 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
1250 static inline uint32_t efscfuf(CPUPPCState
*env
, uint32_t val
)
1255 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
1256 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1257 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
1262 static inline uint32_t efsctsf(CPUPPCState
*env
, uint32_t val
)
1268 /* NaN are not treated the same way IEEE 754 does */
1269 if (unlikely(float32_is_quiet_nan(u
.f
, &env
->vec_status
))) {
1272 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1273 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
1275 return float32_to_int32(u
.f
, &env
->vec_status
);
1278 static inline uint32_t efsctuf(CPUPPCState
*env
, uint32_t val
)
1284 /* NaN are not treated the same way IEEE 754 does */
1285 if (unlikely(float32_is_quiet_nan(u
.f
, &env
->vec_status
))) {
1288 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1289 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
1291 return float32_to_uint32(u
.f
, &env
->vec_status
);
1294 #define HELPER_SPE_SINGLE_CONV(name) \
1295 uint32_t helper_e##name(CPUPPCState *env, uint32_t val) \
1297 return e##name(env, val); \
1300 HELPER_SPE_SINGLE_CONV(fscfsi
);
1302 HELPER_SPE_SINGLE_CONV(fscfui
);
1304 HELPER_SPE_SINGLE_CONV(fscfuf
);
1306 HELPER_SPE_SINGLE_CONV(fscfsf
);
1308 HELPER_SPE_SINGLE_CONV(fsctsi
);
1310 HELPER_SPE_SINGLE_CONV(fsctui
);
1312 HELPER_SPE_SINGLE_CONV(fsctsiz
);
1314 HELPER_SPE_SINGLE_CONV(fsctuiz
);
1316 HELPER_SPE_SINGLE_CONV(fsctsf
);
1318 HELPER_SPE_SINGLE_CONV(fsctuf
);
1320 #define HELPER_SPE_VECTOR_CONV(name) \
1321 uint64_t helper_ev##name(CPUPPCState *env, uint64_t val) \
1323 return ((uint64_t)e##name(env, val >> 32) << 32) | \
1324 (uint64_t)e##name(env, val); \
1327 HELPER_SPE_VECTOR_CONV(fscfsi
);
1329 HELPER_SPE_VECTOR_CONV(fscfui
);
1331 HELPER_SPE_VECTOR_CONV(fscfuf
);
1333 HELPER_SPE_VECTOR_CONV(fscfsf
);
1335 HELPER_SPE_VECTOR_CONV(fsctsi
);
1337 HELPER_SPE_VECTOR_CONV(fsctui
);
1339 HELPER_SPE_VECTOR_CONV(fsctsiz
);
1341 HELPER_SPE_VECTOR_CONV(fsctuiz
);
1343 HELPER_SPE_VECTOR_CONV(fsctsf
);
1345 HELPER_SPE_VECTOR_CONV(fsctuf
);
1347 /* Single-precision floating-point arithmetic */
1348 static inline uint32_t efsadd(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1354 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->vec_status
);
1358 static inline uint32_t efssub(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1364 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->vec_status
);
1368 static inline uint32_t efsmul(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1374 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->vec_status
);
1378 static inline uint32_t efsdiv(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1384 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->vec_status
);
1388 #define HELPER_SPE_SINGLE_ARITH(name) \
1389 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1391 return e##name(env, op1, op2); \
1394 HELPER_SPE_SINGLE_ARITH(fsadd
);
1396 HELPER_SPE_SINGLE_ARITH(fssub
);
1398 HELPER_SPE_SINGLE_ARITH(fsmul
);
1400 HELPER_SPE_SINGLE_ARITH(fsdiv
);
1402 #define HELPER_SPE_VECTOR_ARITH(name) \
1403 uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1405 return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) | \
1406 (uint64_t)e##name(env, op1, op2); \
1409 HELPER_SPE_VECTOR_ARITH(fsadd
);
1411 HELPER_SPE_VECTOR_ARITH(fssub
);
1413 HELPER_SPE_VECTOR_ARITH(fsmul
);
1415 HELPER_SPE_VECTOR_ARITH(fsdiv
);
1417 /* Single-precision floating-point comparisons */
1418 static inline uint32_t efscmplt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1424 return float32_lt(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
1427 static inline uint32_t efscmpgt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1433 return float32_le(u1
.f
, u2
.f
, &env
->vec_status
) ? 0 : 4;
1436 static inline uint32_t efscmpeq(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1442 return float32_eq(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
1445 static inline uint32_t efststlt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1447 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1448 return efscmplt(env
, op1
, op2
);
1451 static inline uint32_t efststgt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1453 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1454 return efscmpgt(env
, op1
, op2
);
1457 static inline uint32_t efststeq(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1459 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1460 return efscmpeq(env
, op1
, op2
);
1463 #define HELPER_SINGLE_SPE_CMP(name) \
1464 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1466 return e##name(env, op1, op2); \
1469 HELPER_SINGLE_SPE_CMP(fststlt
);
1471 HELPER_SINGLE_SPE_CMP(fststgt
);
1473 HELPER_SINGLE_SPE_CMP(fststeq
);
1475 HELPER_SINGLE_SPE_CMP(fscmplt
);
1477 HELPER_SINGLE_SPE_CMP(fscmpgt
);
1479 HELPER_SINGLE_SPE_CMP(fscmpeq
);
1481 static inline uint32_t evcmp_merge(int t0
, int t1
)
1483 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1486 #define HELPER_VECTOR_SPE_CMP(name) \
1487 uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1489 return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32), \
1490 e##name(env, op1, op2)); \
1493 HELPER_VECTOR_SPE_CMP(fststlt
);
1495 HELPER_VECTOR_SPE_CMP(fststgt
);
1497 HELPER_VECTOR_SPE_CMP(fststeq
);
1499 HELPER_VECTOR_SPE_CMP(fscmplt
);
1501 HELPER_VECTOR_SPE_CMP(fscmpgt
);
1503 HELPER_VECTOR_SPE_CMP(fscmpeq
);
1505 /* Double-precision floating-point conversion */
1506 uint64_t helper_efdcfsi(CPUPPCState
*env
, uint32_t val
)
1510 u
.d
= int32_to_float64(val
, &env
->vec_status
);
1515 uint64_t helper_efdcfsid(CPUPPCState
*env
, uint64_t val
)
1519 u
.d
= int64_to_float64(val
, &env
->vec_status
);
1524 uint64_t helper_efdcfui(CPUPPCState
*env
, uint32_t val
)
1528 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
1533 uint64_t helper_efdcfuid(CPUPPCState
*env
, uint64_t val
)
1537 u
.d
= uint64_to_float64(val
, &env
->vec_status
);
1542 uint32_t helper_efdctsi(CPUPPCState
*env
, uint64_t val
)
1547 /* NaN are not treated the same way IEEE 754 does */
1548 if (unlikely(float64_is_any_nan(u
.d
))) {
1552 return float64_to_int32(u
.d
, &env
->vec_status
);
1555 uint32_t helper_efdctui(CPUPPCState
*env
, uint64_t val
)
1560 /* NaN are not treated the same way IEEE 754 does */
1561 if (unlikely(float64_is_any_nan(u
.d
))) {
1565 return float64_to_uint32(u
.d
, &env
->vec_status
);
1568 uint32_t helper_efdctsiz(CPUPPCState
*env
, uint64_t val
)
1573 /* NaN are not treated the same way IEEE 754 does */
1574 if (unlikely(float64_is_any_nan(u
.d
))) {
1578 return float64_to_int32_round_to_zero(u
.d
, &env
->vec_status
);
1581 uint64_t helper_efdctsidz(CPUPPCState
*env
, uint64_t val
)
1586 /* NaN are not treated the same way IEEE 754 does */
1587 if (unlikely(float64_is_any_nan(u
.d
))) {
1591 return float64_to_int64_round_to_zero(u
.d
, &env
->vec_status
);
1594 uint32_t helper_efdctuiz(CPUPPCState
*env
, uint64_t val
)
1599 /* NaN are not treated the same way IEEE 754 does */
1600 if (unlikely(float64_is_any_nan(u
.d
))) {
1604 return float64_to_uint32_round_to_zero(u
.d
, &env
->vec_status
);
1607 uint64_t helper_efdctuidz(CPUPPCState
*env
, uint64_t val
)
1612 /* NaN are not treated the same way IEEE 754 does */
1613 if (unlikely(float64_is_any_nan(u
.d
))) {
1617 return float64_to_uint64_round_to_zero(u
.d
, &env
->vec_status
);
1620 uint64_t helper_efdcfsf(CPUPPCState
*env
, uint32_t val
)
1625 u
.d
= int32_to_float64(val
, &env
->vec_status
);
1626 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
1627 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
1632 uint64_t helper_efdcfuf(CPUPPCState
*env
, uint32_t val
)
1637 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
1638 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
1639 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
1644 uint32_t helper_efdctsf(CPUPPCState
*env
, uint64_t val
)
1650 /* NaN are not treated the same way IEEE 754 does */
1651 if (unlikely(float64_is_any_nan(u
.d
))) {
1654 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
1655 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
1657 return float64_to_int32(u
.d
, &env
->vec_status
);
1660 uint32_t helper_efdctuf(CPUPPCState
*env
, uint64_t val
)
1666 /* NaN are not treated the same way IEEE 754 does */
1667 if (unlikely(float64_is_any_nan(u
.d
))) {
1670 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
1671 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
1673 return float64_to_uint32(u
.d
, &env
->vec_status
);
1676 uint32_t helper_efscfd(CPUPPCState
*env
, uint64_t val
)
1682 u2
.f
= float64_to_float32(u1
.d
, &env
->vec_status
);
1687 uint64_t helper_efdcfs(CPUPPCState
*env
, uint32_t val
)
1693 u2
.d
= float32_to_float64(u1
.f
, &env
->vec_status
);
1698 /* Double precision fixed-point arithmetic */
1699 uint64_t helper_efdadd(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1705 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->vec_status
);
1709 uint64_t helper_efdsub(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1715 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->vec_status
);
1719 uint64_t helper_efdmul(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1725 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->vec_status
);
1729 uint64_t helper_efddiv(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1735 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->vec_status
);
1739 /* Double precision floating point helpers */
1740 uint32_t helper_efdtstlt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1746 return float64_lt(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
1749 uint32_t helper_efdtstgt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1755 return float64_le(u1
.d
, u2
.d
, &env
->vec_status
) ? 0 : 4;
1758 uint32_t helper_efdtsteq(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1764 return float64_eq_quiet(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
1767 uint32_t helper_efdcmplt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1769 /* XXX: TODO: test special values (NaN, infinites, ...) */
1770 return helper_efdtstlt(env
, op1
, op2
);
1773 uint32_t helper_efdcmpgt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1775 /* XXX: TODO: test special values (NaN, infinites, ...) */
1776 return helper_efdtstgt(env
, op1
, op2
);
1779 uint32_t helper_efdcmpeq(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1781 /* XXX: TODO: test special values (NaN, infinites, ...) */
1782 return helper_efdtsteq(env
, op1
, op2
);
1785 #define float64_to_float64(x, env) x
1788 /* VSX_ADD_SUB - VSX floating point add/subract
1789 * name - instruction mnemonic
1790 * op - operation (add or sub)
1791 * nels - number of elements (1, 2 or 4)
1792 * tp - type (float32 or float64)
1793 * fld - vsr_t field (VsrD(*) or VsrW(*))
1796 #define VSX_ADD_SUB(name, op, nels, tp, fld, sfprf, r2sp) \
1797 void helper_##name(CPUPPCState *env, uint32_t opcode) \
1799 ppc_vsr_t xt, xa, xb; \
1802 getVSR(xA(opcode), &xa, env); \
1803 getVSR(xB(opcode), &xb, env); \
1804 getVSR(xT(opcode), &xt, env); \
1805 helper_reset_fpstatus(env); \
1807 for (i = 0; i < nels; i++) { \
1808 float_status tstat = env->fp_status; \
1809 set_float_exception_flags(0, &tstat); \
1810 xt.fld = tp##_##op(xa.fld, xb.fld, &tstat); \
1811 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1813 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1814 if (tp##_is_infinity(xa.fld) && tp##_is_infinity(xb.fld)) { \
1815 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
1816 } else if (tp##_is_signaling_nan(xa.fld, &tstat) || \
1817 tp##_is_signaling_nan(xb.fld, &tstat)) { \
1818 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1823 xt.fld = helper_frsp(env, xt.fld); \
1827 helper_compute_fprf_float64(env, xt.fld); \
1830 putVSR(xT(opcode), &xt, env); \
1831 float_check_status(env); \
1834 VSX_ADD_SUB(xsadddp
, add
, 1, float64
, VsrD(0), 1, 0)
1835 VSX_ADD_SUB(xsaddsp
, add
, 1, float64
, VsrD(0), 1, 1)
1836 VSX_ADD_SUB(xvadddp
, add
, 2, float64
, VsrD(i
), 0, 0)
1837 VSX_ADD_SUB(xvaddsp
, add
, 4, float32
, VsrW(i
), 0, 0)
1838 VSX_ADD_SUB(xssubdp
, sub
, 1, float64
, VsrD(0), 1, 0)
1839 VSX_ADD_SUB(xssubsp
, sub
, 1, float64
, VsrD(0), 1, 1)
1840 VSX_ADD_SUB(xvsubdp
, sub
, 2, float64
, VsrD(i
), 0, 0)
1841 VSX_ADD_SUB(xvsubsp
, sub
, 4, float32
, VsrW(i
), 0, 0)
1843 void helper_xsaddqp(CPUPPCState
*env
, uint32_t opcode
)
1845 ppc_vsr_t xt
, xa
, xb
;
1848 getVSR(rA(opcode
) + 32, &xa
, env
);
1849 getVSR(rB(opcode
) + 32, &xb
, env
);
1850 getVSR(rD(opcode
) + 32, &xt
, env
);
1851 helper_reset_fpstatus(env
);
1853 if (unlikely(Rc(opcode
) != 0)) {
1854 /* TODO: Support xsadddpo after round-to-odd is implemented */
1858 tstat
= env
->fp_status
;
1859 set_float_exception_flags(0, &tstat
);
1860 xt
.f128
= float128_add(xa
.f128
, xb
.f128
, &tstat
);
1861 env
->fp_status
.float_exception_flags
|= tstat
.float_exception_flags
;
1863 if (unlikely(tstat
.float_exception_flags
& float_flag_invalid
)) {
1864 if (float128_is_infinity(xa
.f128
) && float128_is_infinity(xb
.f128
)) {
1865 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
1866 } else if (float128_is_signaling_nan(xa
.f128
, &tstat
) ||
1867 float128_is_signaling_nan(xb
.f128
, &tstat
)) {
1868 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
1872 helper_compute_fprf_float128(env
, xt
.f128
);
1874 putVSR(rD(opcode
) + 32, &xt
, env
);
1875 float_check_status(env
);
1878 /* VSX_MUL - VSX floating point multiply
1879 * op - instruction mnemonic
1880 * nels - number of elements (1, 2 or 4)
1881 * tp - type (float32 or float64)
1882 * fld - vsr_t field (VsrD(*) or VsrW(*))
1885 #define VSX_MUL(op, nels, tp, fld, sfprf, r2sp) \
1886 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1888 ppc_vsr_t xt, xa, xb; \
1891 getVSR(xA(opcode), &xa, env); \
1892 getVSR(xB(opcode), &xb, env); \
1893 getVSR(xT(opcode), &xt, env); \
1894 helper_reset_fpstatus(env); \
1896 for (i = 0; i < nels; i++) { \
1897 float_status tstat = env->fp_status; \
1898 set_float_exception_flags(0, &tstat); \
1899 xt.fld = tp##_mul(xa.fld, xb.fld, &tstat); \
1900 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1902 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1903 if ((tp##_is_infinity(xa.fld) && tp##_is_zero(xb.fld)) || \
1904 (tp##_is_infinity(xb.fld) && tp##_is_zero(xa.fld))) { \
1905 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIMZ, sfprf); \
1906 } else if (tp##_is_signaling_nan(xa.fld, &tstat) || \
1907 tp##_is_signaling_nan(xb.fld, &tstat)) { \
1908 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1913 xt.fld = helper_frsp(env, xt.fld); \
1917 helper_compute_fprf_float64(env, xt.fld); \
1921 putVSR(xT(opcode), &xt, env); \
1922 float_check_status(env); \
1925 VSX_MUL(xsmuldp
, 1, float64
, VsrD(0), 1, 0)
1926 VSX_MUL(xsmulsp
, 1, float64
, VsrD(0), 1, 1)
1927 VSX_MUL(xvmuldp
, 2, float64
, VsrD(i
), 0, 0)
1928 VSX_MUL(xvmulsp
, 4, float32
, VsrW(i
), 0, 0)
1930 void helper_xsmulqp(CPUPPCState
*env
, uint32_t opcode
)
1932 ppc_vsr_t xt
, xa
, xb
;
1934 getVSR(rA(opcode
) + 32, &xa
, env
);
1935 getVSR(rB(opcode
) + 32, &xb
, env
);
1936 getVSR(rD(opcode
) + 32, &xt
, env
);
1938 if (unlikely(Rc(opcode
) != 0)) {
1939 /* TODO: Support xsmulpo after round-to-odd is implemented */
1943 helper_reset_fpstatus(env
);
1945 float_status tstat
= env
->fp_status
;
1946 set_float_exception_flags(0, &tstat
);
1947 xt
.f128
= float128_mul(xa
.f128
, xb
.f128
, &tstat
);
1948 env
->fp_status
.float_exception_flags
|= tstat
.float_exception_flags
;
1950 if (unlikely(tstat
.float_exception_flags
& float_flag_invalid
)) {
1951 if ((float128_is_infinity(xa
.f128
) && float128_is_zero(xb
.f128
)) ||
1952 (float128_is_infinity(xb
.f128
) && float128_is_zero(xa
.f128
))) {
1953 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
1954 } else if (float128_is_signaling_nan(xa
.f128
, &tstat
) ||
1955 float128_is_signaling_nan(xb
.f128
, &tstat
)) {
1956 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
1959 helper_compute_fprf_float128(env
, xt
.f128
);
1961 putVSR(rD(opcode
) + 32, &xt
, env
);
1962 float_check_status(env
);
1965 /* VSX_DIV - VSX floating point divide
1966 * op - instruction mnemonic
1967 * nels - number of elements (1, 2 or 4)
1968 * tp - type (float32 or float64)
1969 * fld - vsr_t field (VsrD(*) or VsrW(*))
1972 #define VSX_DIV(op, nels, tp, fld, sfprf, r2sp) \
1973 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1975 ppc_vsr_t xt, xa, xb; \
1978 getVSR(xA(opcode), &xa, env); \
1979 getVSR(xB(opcode), &xb, env); \
1980 getVSR(xT(opcode), &xt, env); \
1981 helper_reset_fpstatus(env); \
1983 for (i = 0; i < nels; i++) { \
1984 float_status tstat = env->fp_status; \
1985 set_float_exception_flags(0, &tstat); \
1986 xt.fld = tp##_div(xa.fld, xb.fld, &tstat); \
1987 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1989 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1990 if (tp##_is_infinity(xa.fld) && tp##_is_infinity(xb.fld)) { \
1991 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIDI, sfprf); \
1992 } else if (tp##_is_zero(xa.fld) && \
1993 tp##_is_zero(xb.fld)) { \
1994 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXZDZ, sfprf); \
1995 } else if (tp##_is_signaling_nan(xa.fld, &tstat) || \
1996 tp##_is_signaling_nan(xb.fld, &tstat)) { \
1997 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2002 xt.fld = helper_frsp(env, xt.fld); \
2006 helper_compute_fprf_float64(env, xt.fld); \
2010 putVSR(xT(opcode), &xt, env); \
2011 float_check_status(env); \
2014 VSX_DIV(xsdivdp
, 1, float64
, VsrD(0), 1, 0)
2015 VSX_DIV(xsdivsp
, 1, float64
, VsrD(0), 1, 1)
2016 VSX_DIV(xvdivdp
, 2, float64
, VsrD(i
), 0, 0)
2017 VSX_DIV(xvdivsp
, 4, float32
, VsrW(i
), 0, 0)
2019 void helper_xsdivqp(CPUPPCState
*env
, uint32_t opcode
)
2021 ppc_vsr_t xt
, xa
, xb
;
2023 getVSR(rA(opcode
) + 32, &xa
, env
);
2024 getVSR(rB(opcode
) + 32, &xb
, env
);
2025 getVSR(rD(opcode
) + 32, &xt
, env
);
2027 if (unlikely(Rc(opcode
) != 0)) {
2028 /* TODO: Support xsdivqpo after round-to-odd is implemented */
2032 helper_reset_fpstatus(env
);
2033 float_status tstat
= env
->fp_status
;
2034 set_float_exception_flags(0, &tstat
);
2035 xt
.f128
= float128_div(xa
.f128
, xb
.f128
, &tstat
);
2036 env
->fp_status
.float_exception_flags
|= tstat
.float_exception_flags
;
2038 if (unlikely(tstat
.float_exception_flags
& float_flag_invalid
)) {
2039 if (float128_is_infinity(xa
.f128
) && float128_is_infinity(xb
.f128
)) {
2040 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIDI
, 1);
2041 } else if (float128_is_zero(xa
.f128
) &&
2042 float128_is_zero(xb
.f128
)) {
2043 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXZDZ
, 1);
2044 } else if (float128_is_signaling_nan(xa
.f128
, &tstat
) ||
2045 float128_is_signaling_nan(xb
.f128
, &tstat
)) {
2046 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
2050 helper_compute_fprf_float128(env
, xt
.f128
);
2051 putVSR(rD(opcode
) + 32, &xt
, env
);
2052 float_check_status(env
);
2055 /* VSX_RE - VSX floating point reciprocal estimate
2056 * op - instruction mnemonic
2057 * nels - number of elements (1, 2 or 4)
2058 * tp - type (float32 or float64)
2059 * fld - vsr_t field (VsrD(*) or VsrW(*))
2062 #define VSX_RE(op, nels, tp, fld, sfprf, r2sp) \
2063 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2068 getVSR(xB(opcode), &xb, env); \
2069 getVSR(xT(opcode), &xt, env); \
2070 helper_reset_fpstatus(env); \
2072 for (i = 0; i < nels; i++) { \
2073 if (unlikely(tp##_is_signaling_nan(xb.fld, &env->fp_status))) { \
2074 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2076 xt.fld = tp##_div(tp##_one, xb.fld, &env->fp_status); \
2079 xt.fld = helper_frsp(env, xt.fld); \
2083 helper_compute_fprf_float64(env, xt.fld); \
2087 putVSR(xT(opcode), &xt, env); \
2088 float_check_status(env); \
2091 VSX_RE(xsredp
, 1, float64
, VsrD(0), 1, 0)
2092 VSX_RE(xsresp
, 1, float64
, VsrD(0), 1, 1)
2093 VSX_RE(xvredp
, 2, float64
, VsrD(i
), 0, 0)
2094 VSX_RE(xvresp
, 4, float32
, VsrW(i
), 0, 0)
2096 /* VSX_SQRT - VSX floating point square root
2097 * op - instruction mnemonic
2098 * nels - number of elements (1, 2 or 4)
2099 * tp - type (float32 or float64)
2100 * fld - vsr_t field (VsrD(*) or VsrW(*))
2103 #define VSX_SQRT(op, nels, tp, fld, sfprf, r2sp) \
2104 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2109 getVSR(xB(opcode), &xb, env); \
2110 getVSR(xT(opcode), &xt, env); \
2111 helper_reset_fpstatus(env); \
2113 for (i = 0; i < nels; i++) { \
2114 float_status tstat = env->fp_status; \
2115 set_float_exception_flags(0, &tstat); \
2116 xt.fld = tp##_sqrt(xb.fld, &tstat); \
2117 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2119 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2120 if (tp##_is_neg(xb.fld) && !tp##_is_zero(xb.fld)) { \
2121 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2122 } else if (tp##_is_signaling_nan(xb.fld, &tstat)) { \
2123 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2128 xt.fld = helper_frsp(env, xt.fld); \
2132 helper_compute_fprf_float64(env, xt.fld); \
2136 putVSR(xT(opcode), &xt, env); \
2137 float_check_status(env); \
2140 VSX_SQRT(xssqrtdp
, 1, float64
, VsrD(0), 1, 0)
2141 VSX_SQRT(xssqrtsp
, 1, float64
, VsrD(0), 1, 1)
2142 VSX_SQRT(xvsqrtdp
, 2, float64
, VsrD(i
), 0, 0)
2143 VSX_SQRT(xvsqrtsp
, 4, float32
, VsrW(i
), 0, 0)
2145 /* VSX_RSQRTE - VSX floating point reciprocal square root estimate
2146 * op - instruction mnemonic
2147 * nels - number of elements (1, 2 or 4)
2148 * tp - type (float32 or float64)
2149 * fld - vsr_t field (VsrD(*) or VsrW(*))
2152 #define VSX_RSQRTE(op, nels, tp, fld, sfprf, r2sp) \
2153 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2158 getVSR(xB(opcode), &xb, env); \
2159 getVSR(xT(opcode), &xt, env); \
2160 helper_reset_fpstatus(env); \
2162 for (i = 0; i < nels; i++) { \
2163 float_status tstat = env->fp_status; \
2164 set_float_exception_flags(0, &tstat); \
2165 xt.fld = tp##_sqrt(xb.fld, &tstat); \
2166 xt.fld = tp##_div(tp##_one, xt.fld, &tstat); \
2167 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2169 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2170 if (tp##_is_neg(xb.fld) && !tp##_is_zero(xb.fld)) { \
2171 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2172 } else if (tp##_is_signaling_nan(xb.fld, &tstat)) { \
2173 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2178 xt.fld = helper_frsp(env, xt.fld); \
2182 helper_compute_fprf_float64(env, xt.fld); \
2186 putVSR(xT(opcode), &xt, env); \
2187 float_check_status(env); \
2190 VSX_RSQRTE(xsrsqrtedp
, 1, float64
, VsrD(0), 1, 0)
2191 VSX_RSQRTE(xsrsqrtesp
, 1, float64
, VsrD(0), 1, 1)
2192 VSX_RSQRTE(xvrsqrtedp
, 2, float64
, VsrD(i
), 0, 0)
2193 VSX_RSQRTE(xvrsqrtesp
, 4, float32
, VsrW(i
), 0, 0)
2195 /* VSX_TDIV - VSX floating point test for divide
2196 * op - instruction mnemonic
2197 * nels - number of elements (1, 2 or 4)
2198 * tp - type (float32 or float64)
2199 * fld - vsr_t field (VsrD(*) or VsrW(*))
2200 * emin - minimum unbiased exponent
2201 * emax - maximum unbiased exponent
2202 * nbits - number of fraction bits
2204 #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits) \
2205 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2212 getVSR(xA(opcode), &xa, env); \
2213 getVSR(xB(opcode), &xb, env); \
2215 for (i = 0; i < nels; i++) { \
2216 if (unlikely(tp##_is_infinity(xa.fld) || \
2217 tp##_is_infinity(xb.fld) || \
2218 tp##_is_zero(xb.fld))) { \
2222 int e_a = ppc_##tp##_get_unbiased_exp(xa.fld); \
2223 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld); \
2225 if (unlikely(tp##_is_any_nan(xa.fld) || \
2226 tp##_is_any_nan(xb.fld))) { \
2228 } else if ((e_b <= emin) || (e_b >= (emax-2))) { \
2230 } else if (!tp##_is_zero(xa.fld) && \
2231 (((e_a - e_b) >= emax) || \
2232 ((e_a - e_b) <= (emin+1)) || \
2233 (e_a <= (emin+nbits)))) { \
2237 if (unlikely(tp##_is_zero_or_denormal(xb.fld))) { \
2238 /* XB is not zero because of the above check and */ \
2239 /* so must be denormalized. */ \
2245 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2248 VSX_TDIV(xstdivdp
, 1, float64
, VsrD(0), -1022, 1023, 52)
2249 VSX_TDIV(xvtdivdp
, 2, float64
, VsrD(i
), -1022, 1023, 52)
2250 VSX_TDIV(xvtdivsp
, 4, float32
, VsrW(i
), -126, 127, 23)
2252 /* VSX_TSQRT - VSX floating point test for square root
2253 * op - instruction mnemonic
2254 * nels - number of elements (1, 2 or 4)
2255 * tp - type (float32 or float64)
2256 * fld - vsr_t field (VsrD(*) or VsrW(*))
2257 * emin - minimum unbiased exponent
2258 * emax - maximum unbiased exponent
2259 * nbits - number of fraction bits
2261 #define VSX_TSQRT(op, nels, tp, fld, emin, nbits) \
2262 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2269 getVSR(xA(opcode), &xa, env); \
2270 getVSR(xB(opcode), &xb, env); \
2272 for (i = 0; i < nels; i++) { \
2273 if (unlikely(tp##_is_infinity(xb.fld) || \
2274 tp##_is_zero(xb.fld))) { \
2278 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld); \
2280 if (unlikely(tp##_is_any_nan(xb.fld))) { \
2282 } else if (unlikely(tp##_is_zero(xb.fld))) { \
2284 } else if (unlikely(tp##_is_neg(xb.fld))) { \
2286 } else if (!tp##_is_zero(xb.fld) && \
2287 (e_b <= (emin+nbits))) { \
2291 if (unlikely(tp##_is_zero_or_denormal(xb.fld))) { \
2292 /* XB is not zero because of the above check and */ \
2293 /* therefore must be denormalized. */ \
2299 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2302 VSX_TSQRT(xstsqrtdp
, 1, float64
, VsrD(0), -1022, 52)
2303 VSX_TSQRT(xvtsqrtdp
, 2, float64
, VsrD(i
), -1022, 52)
2304 VSX_TSQRT(xvtsqrtsp
, 4, float32
, VsrW(i
), -126, 23)
2306 /* VSX_MADD - VSX floating point muliply/add variations
2307 * op - instruction mnemonic
2308 * nels - number of elements (1, 2 or 4)
2309 * tp - type (float32 or float64)
2310 * fld - vsr_t field (VsrD(*) or VsrW(*))
2311 * maddflgs - flags for the float*muladd routine that control the
2312 * various forms (madd, msub, nmadd, nmsub)
2313 * afrm - A form (1=A, 0=M)
2316 #define VSX_MADD(op, nels, tp, fld, maddflgs, afrm, sfprf, r2sp) \
2317 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2319 ppc_vsr_t xt_in, xa, xb, xt_out; \
2323 if (afrm) { /* AxB + T */ \
2326 } else { /* AxT + B */ \
2331 getVSR(xA(opcode), &xa, env); \
2332 getVSR(xB(opcode), &xb, env); \
2333 getVSR(xT(opcode), &xt_in, env); \
2337 helper_reset_fpstatus(env); \
2339 for (i = 0; i < nels; i++) { \
2340 float_status tstat = env->fp_status; \
2341 set_float_exception_flags(0, &tstat); \
2342 if (r2sp && (tstat.float_rounding_mode == float_round_nearest_even)) {\
2343 /* Avoid double rounding errors by rounding the intermediate */ \
2344 /* result to odd. */ \
2345 set_float_rounding_mode(float_round_to_zero, &tstat); \
2346 xt_out.fld = tp##_muladd(xa.fld, b->fld, c->fld, \
2347 maddflgs, &tstat); \
2348 xt_out.fld |= (get_float_exception_flags(&tstat) & \
2349 float_flag_inexact) != 0; \
2351 xt_out.fld = tp##_muladd(xa.fld, b->fld, c->fld, \
2352 maddflgs, &tstat); \
2354 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2356 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2357 if (tp##_is_signaling_nan(xa.fld, &tstat) || \
2358 tp##_is_signaling_nan(b->fld, &tstat) || \
2359 tp##_is_signaling_nan(c->fld, &tstat)) { \
2360 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2361 tstat.float_exception_flags &= ~float_flag_invalid; \
2363 if ((tp##_is_infinity(xa.fld) && tp##_is_zero(b->fld)) || \
2364 (tp##_is_zero(xa.fld) && tp##_is_infinity(b->fld))) { \
2365 xt_out.fld = float64_to_##tp(float_invalid_op_excp(env, \
2366 POWERPC_EXCP_FP_VXIMZ, sfprf), &env->fp_status); \
2367 tstat.float_exception_flags &= ~float_flag_invalid; \
2369 if ((tstat.float_exception_flags & float_flag_invalid) && \
2370 ((tp##_is_infinity(xa.fld) || \
2371 tp##_is_infinity(b->fld)) && \
2372 tp##_is_infinity(c->fld))) { \
2373 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
2378 xt_out.fld = helper_frsp(env, xt_out.fld); \
2382 helper_compute_fprf_float64(env, xt_out.fld); \
2385 putVSR(xT(opcode), &xt_out, env); \
2386 float_check_status(env); \
2390 #define MSUB_FLGS float_muladd_negate_c
2391 #define NMADD_FLGS float_muladd_negate_result
2392 #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
2394 VSX_MADD(xsmaddadp
, 1, float64
, VsrD(0), MADD_FLGS
, 1, 1, 0)
2395 VSX_MADD(xsmaddmdp
, 1, float64
, VsrD(0), MADD_FLGS
, 0, 1, 0)
2396 VSX_MADD(xsmsubadp
, 1, float64
, VsrD(0), MSUB_FLGS
, 1, 1, 0)
2397 VSX_MADD(xsmsubmdp
, 1, float64
, VsrD(0), MSUB_FLGS
, 0, 1, 0)
2398 VSX_MADD(xsnmaddadp
, 1, float64
, VsrD(0), NMADD_FLGS
, 1, 1, 0)
2399 VSX_MADD(xsnmaddmdp
, 1, float64
, VsrD(0), NMADD_FLGS
, 0, 1, 0)
2400 VSX_MADD(xsnmsubadp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 1, 1, 0)
2401 VSX_MADD(xsnmsubmdp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 0, 1, 0)
2403 VSX_MADD(xsmaddasp
, 1, float64
, VsrD(0), MADD_FLGS
, 1, 1, 1)
2404 VSX_MADD(xsmaddmsp
, 1, float64
, VsrD(0), MADD_FLGS
, 0, 1, 1)
2405 VSX_MADD(xsmsubasp
, 1, float64
, VsrD(0), MSUB_FLGS
, 1, 1, 1)
2406 VSX_MADD(xsmsubmsp
, 1, float64
, VsrD(0), MSUB_FLGS
, 0, 1, 1)
2407 VSX_MADD(xsnmaddasp
, 1, float64
, VsrD(0), NMADD_FLGS
, 1, 1, 1)
2408 VSX_MADD(xsnmaddmsp
, 1, float64
, VsrD(0), NMADD_FLGS
, 0, 1, 1)
2409 VSX_MADD(xsnmsubasp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 1, 1, 1)
2410 VSX_MADD(xsnmsubmsp
, 1, float64
, VsrD(0), NMSUB_FLGS
, 0, 1, 1)
2412 VSX_MADD(xvmaddadp
, 2, float64
, VsrD(i
), MADD_FLGS
, 1, 0, 0)
2413 VSX_MADD(xvmaddmdp
, 2, float64
, VsrD(i
), MADD_FLGS
, 0, 0, 0)
2414 VSX_MADD(xvmsubadp
, 2, float64
, VsrD(i
), MSUB_FLGS
, 1, 0, 0)
2415 VSX_MADD(xvmsubmdp
, 2, float64
, VsrD(i
), MSUB_FLGS
, 0, 0, 0)
2416 VSX_MADD(xvnmaddadp
, 2, float64
, VsrD(i
), NMADD_FLGS
, 1, 0, 0)
2417 VSX_MADD(xvnmaddmdp
, 2, float64
, VsrD(i
), NMADD_FLGS
, 0, 0, 0)
2418 VSX_MADD(xvnmsubadp
, 2, float64
, VsrD(i
), NMSUB_FLGS
, 1, 0, 0)
2419 VSX_MADD(xvnmsubmdp
, 2, float64
, VsrD(i
), NMSUB_FLGS
, 0, 0, 0)
2421 VSX_MADD(xvmaddasp
, 4, float32
, VsrW(i
), MADD_FLGS
, 1, 0, 0)
2422 VSX_MADD(xvmaddmsp
, 4, float32
, VsrW(i
), MADD_FLGS
, 0, 0, 0)
2423 VSX_MADD(xvmsubasp
, 4, float32
, VsrW(i
), MSUB_FLGS
, 1, 0, 0)
2424 VSX_MADD(xvmsubmsp
, 4, float32
, VsrW(i
), MSUB_FLGS
, 0, 0, 0)
2425 VSX_MADD(xvnmaddasp
, 4, float32
, VsrW(i
), NMADD_FLGS
, 1, 0, 0)
2426 VSX_MADD(xvnmaddmsp
, 4, float32
, VsrW(i
), NMADD_FLGS
, 0, 0, 0)
2427 VSX_MADD(xvnmsubasp
, 4, float32
, VsrW(i
), NMSUB_FLGS
, 1, 0, 0)
2428 VSX_MADD(xvnmsubmsp
, 4, float32
, VsrW(i
), NMSUB_FLGS
, 0, 0, 0)
2430 /* VSX_SCALAR_CMP_DP - VSX scalar floating point compare double precision
2431 * op - instruction mnemonic
2432 * cmp - comparison operation
2433 * exp - expected result of comparison
2434 * svxvc - set VXVC bit
2436 #define VSX_SCALAR_CMP_DP(op, cmp, exp, svxvc) \
2437 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2439 ppc_vsr_t xt, xa, xb; \
2440 bool vxsnan_flag = false, vxvc_flag = false, vex_flag = false; \
2442 getVSR(xA(opcode), &xa, env); \
2443 getVSR(xB(opcode), &xb, env); \
2444 getVSR(xT(opcode), &xt, env); \
2446 if (float64_is_signaling_nan(xa.VsrD(0), &env->fp_status) || \
2447 float64_is_signaling_nan(xb.VsrD(0), &env->fp_status)) { \
2448 vxsnan_flag = true; \
2449 if (fpscr_ve == 0 && svxvc) { \
2452 } else if (svxvc) { \
2453 vxvc_flag = float64_is_quiet_nan(xa.VsrD(0), &env->fp_status) || \
2454 float64_is_quiet_nan(xb.VsrD(0), &env->fp_status); \
2456 if (vxsnan_flag) { \
2457 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2460 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2462 vex_flag = fpscr_ve && (vxvc_flag || vxsnan_flag); \
2465 if (float64_##cmp(xb.VsrD(0), xa.VsrD(0), &env->fp_status) == exp) { \
2473 putVSR(xT(opcode), &xt, env); \
2474 helper_float_check_status(env); \
2477 VSX_SCALAR_CMP_DP(xscmpeqdp
, eq
, 1, 0)
2478 VSX_SCALAR_CMP_DP(xscmpgedp
, le
, 1, 1)
2479 VSX_SCALAR_CMP_DP(xscmpgtdp
, lt
, 1, 1)
2480 VSX_SCALAR_CMP_DP(xscmpnedp
, eq
, 0, 0)
2482 void helper_xscmpexpdp(CPUPPCState
*env
, uint32_t opcode
)
2485 int64_t exp_a
, exp_b
;
2488 getVSR(xA(opcode
), &xa
, env
);
2489 getVSR(xB(opcode
), &xb
, env
);
2491 exp_a
= extract64(xa
.VsrD(0), 52, 11);
2492 exp_b
= extract64(xb
.VsrD(0), 52, 11);
2494 if (unlikely(float64_is_any_nan(xa
.VsrD(0)) ||
2495 float64_is_any_nan(xb
.VsrD(0)))) {
2498 if (exp_a
< exp_b
) {
2500 } else if (exp_a
> exp_b
) {
2507 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
2508 env
->fpscr
|= cc
<< FPSCR_FPRF
;
2509 env
->crf
[BF(opcode
)] = cc
;
2511 helper_float_check_status(env
);
2514 void helper_xscmpexpqp(CPUPPCState
*env
, uint32_t opcode
)
2517 int64_t exp_a
, exp_b
;
2520 getVSR(rA(opcode
) + 32, &xa
, env
);
2521 getVSR(rB(opcode
) + 32, &xb
, env
);
2523 exp_a
= extract64(xa
.VsrD(0), 48, 15);
2524 exp_b
= extract64(xb
.VsrD(0), 48, 15);
2526 if (unlikely(float128_is_any_nan(xa
.f128
) ||
2527 float128_is_any_nan(xb
.f128
))) {
2530 if (exp_a
< exp_b
) {
2532 } else if (exp_a
> exp_b
) {
2539 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
2540 env
->fpscr
|= cc
<< FPSCR_FPRF
;
2541 env
->crf
[BF(opcode
)] = cc
;
2543 helper_float_check_status(env
);
2546 #define VSX_SCALAR_CMP(op, ordered) \
2547 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2551 bool vxsnan_flag = false, vxvc_flag = false; \
2553 helper_reset_fpstatus(env); \
2554 getVSR(xA(opcode), &xa, env); \
2555 getVSR(xB(opcode), &xb, env); \
2557 if (float64_is_signaling_nan(xa.VsrD(0), &env->fp_status) || \
2558 float64_is_signaling_nan(xb.VsrD(0), &env->fp_status)) { \
2559 vxsnan_flag = true; \
2561 if (fpscr_ve == 0 && ordered) { \
2564 } else if (float64_is_quiet_nan(xa.VsrD(0), &env->fp_status) || \
2565 float64_is_quiet_nan(xb.VsrD(0), &env->fp_status)) { \
2571 if (vxsnan_flag) { \
2572 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2575 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2578 if (float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) { \
2580 } else if (!float64_le(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) { \
2586 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
2587 env->fpscr |= cc << FPSCR_FPRF; \
2588 env->crf[BF(opcode)] = cc; \
2590 float_check_status(env); \
2593 VSX_SCALAR_CMP(xscmpodp
, 1)
2594 VSX_SCALAR_CMP(xscmpudp
, 0)
2596 #define VSX_SCALAR_CMPQ(op, ordered) \
2597 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2601 bool vxsnan_flag = false, vxvc_flag = false; \
2603 helper_reset_fpstatus(env); \
2604 getVSR(rA(opcode) + 32, &xa, env); \
2605 getVSR(rB(opcode) + 32, &xb, env); \
2607 if (float128_is_signaling_nan(xa.f128, &env->fp_status) || \
2608 float128_is_signaling_nan(xb.f128, &env->fp_status)) { \
2609 vxsnan_flag = true; \
2611 if (fpscr_ve == 0 && ordered) { \
2614 } else if (float128_is_quiet_nan(xa.f128, &env->fp_status) || \
2615 float128_is_quiet_nan(xb.f128, &env->fp_status)) { \
2621 if (vxsnan_flag) { \
2622 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2625 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2628 if (float128_lt(xa.f128, xb.f128, &env->fp_status)) { \
2630 } else if (!float128_le(xa.f128, xb.f128, &env->fp_status)) { \
2636 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
2637 env->fpscr |= cc << FPSCR_FPRF; \
2638 env->crf[BF(opcode)] = cc; \
2640 float_check_status(env); \
2643 VSX_SCALAR_CMPQ(xscmpoqp
, 1)
2644 VSX_SCALAR_CMPQ(xscmpuqp
, 0)
2646 /* VSX_MAX_MIN - VSX floating point maximum/minimum
2647 * name - instruction mnemonic
2648 * op - operation (max or min)
2649 * nels - number of elements (1, 2 or 4)
2650 * tp - type (float32 or float64)
2651 * fld - vsr_t field (VsrD(*) or VsrW(*))
2653 #define VSX_MAX_MIN(name, op, nels, tp, fld) \
2654 void helper_##name(CPUPPCState *env, uint32_t opcode) \
2656 ppc_vsr_t xt, xa, xb; \
2659 getVSR(xA(opcode), &xa, env); \
2660 getVSR(xB(opcode), &xb, env); \
2661 getVSR(xT(opcode), &xt, env); \
2663 for (i = 0; i < nels; i++) { \
2664 xt.fld = tp##_##op(xa.fld, xb.fld, &env->fp_status); \
2665 if (unlikely(tp##_is_signaling_nan(xa.fld, &env->fp_status) || \
2666 tp##_is_signaling_nan(xb.fld, &env->fp_status))) { \
2667 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2671 putVSR(xT(opcode), &xt, env); \
2672 float_check_status(env); \
2675 VSX_MAX_MIN(xsmaxdp
, maxnum
, 1, float64
, VsrD(0))
2676 VSX_MAX_MIN(xvmaxdp
, maxnum
, 2, float64
, VsrD(i
))
2677 VSX_MAX_MIN(xvmaxsp
, maxnum
, 4, float32
, VsrW(i
))
2678 VSX_MAX_MIN(xsmindp
, minnum
, 1, float64
, VsrD(0))
2679 VSX_MAX_MIN(xvmindp
, minnum
, 2, float64
, VsrD(i
))
2680 VSX_MAX_MIN(xvminsp
, minnum
, 4, float32
, VsrW(i
))
2682 /* VSX_CMP - VSX floating point compare
2683 * op - instruction mnemonic
2684 * nels - number of elements (1, 2 or 4)
2685 * tp - type (float32 or float64)
2686 * fld - vsr_t field (VsrD(*) or VsrW(*))
2687 * cmp - comparison operation
2688 * svxvc - set VXVC bit
2689 * exp - expected result of comparison
2691 #define VSX_CMP(op, nels, tp, fld, cmp, svxvc, exp) \
2692 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2694 ppc_vsr_t xt, xa, xb; \
2697 int all_false = 1; \
2699 getVSR(xA(opcode), &xa, env); \
2700 getVSR(xB(opcode), &xb, env); \
2701 getVSR(xT(opcode), &xt, env); \
2703 for (i = 0; i < nels; i++) { \
2704 if (unlikely(tp##_is_any_nan(xa.fld) || \
2705 tp##_is_any_nan(xb.fld))) { \
2706 if (tp##_is_signaling_nan(xa.fld, &env->fp_status) || \
2707 tp##_is_signaling_nan(xb.fld, &env->fp_status)) { \
2708 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2711 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2716 if (tp##_##cmp(xb.fld, xa.fld, &env->fp_status) == exp) { \
2726 putVSR(xT(opcode), &xt, env); \
2727 if ((opcode >> (31-21)) & 1) { \
2728 env->crf[6] = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0); \
2730 float_check_status(env); \
2733 VSX_CMP(xvcmpeqdp
, 2, float64
, VsrD(i
), eq
, 0, 1)
2734 VSX_CMP(xvcmpgedp
, 2, float64
, VsrD(i
), le
, 1, 1)
2735 VSX_CMP(xvcmpgtdp
, 2, float64
, VsrD(i
), lt
, 1, 1)
2736 VSX_CMP(xvcmpnedp
, 2, float64
, VsrD(i
), eq
, 0, 0)
2737 VSX_CMP(xvcmpeqsp
, 4, float32
, VsrW(i
), eq
, 0, 1)
2738 VSX_CMP(xvcmpgesp
, 4, float32
, VsrW(i
), le
, 1, 1)
2739 VSX_CMP(xvcmpgtsp
, 4, float32
, VsrW(i
), lt
, 1, 1)
2740 VSX_CMP(xvcmpnesp
, 4, float32
, VsrW(i
), eq
, 0, 0)
2742 /* VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2743 * op - instruction mnemonic
2744 * nels - number of elements (1, 2 or 4)
2745 * stp - source type (float32 or float64)
2746 * ttp - target type (float32 or float64)
2747 * sfld - source vsr_t field
2748 * tfld - target vsr_t field (f32 or f64)
2751 #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf) \
2752 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2757 getVSR(xB(opcode), &xb, env); \
2758 getVSR(xT(opcode), &xt, env); \
2760 for (i = 0; i < nels; i++) { \
2761 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2762 if (unlikely(stp##_is_signaling_nan(xb.sfld, \
2763 &env->fp_status))) { \
2764 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2765 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2768 helper_compute_fprf_##ttp(env, xt.tfld); \
2772 putVSR(xT(opcode), &xt, env); \
2773 float_check_status(env); \
2776 VSX_CVT_FP_TO_FP(xscvdpsp
, 1, float64
, float32
, VsrD(0), VsrW(0), 1)
2777 VSX_CVT_FP_TO_FP(xscvspdp
, 1, float32
, float64
, VsrW(0), VsrD(0), 1)
2778 VSX_CVT_FP_TO_FP(xvcvdpsp
, 2, float64
, float32
, VsrD(i
), VsrW(2*i
), 0)
2779 VSX_CVT_FP_TO_FP(xvcvspdp
, 2, float32
, float64
, VsrW(2*i
), VsrD(i
), 0)
2781 /* VSX_CVT_FP_TO_FP_VECTOR - VSX floating point/floating point conversion
2782 * op - instruction mnemonic
2783 * nels - number of elements (1, 2 or 4)
2784 * stp - source type (float32 or float64)
2785 * ttp - target type (float32 or float64)
2786 * sfld - source vsr_t field
2787 * tfld - target vsr_t field (f32 or f64)
2790 #define VSX_CVT_FP_TO_FP_VECTOR(op, nels, stp, ttp, sfld, tfld, sfprf) \
2791 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2796 getVSR(rB(opcode) + 32, &xb, env); \
2797 getVSR(rD(opcode) + 32, &xt, env); \
2799 for (i = 0; i < nels; i++) { \
2800 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2801 if (unlikely(stp##_is_signaling_nan(xb.sfld, \
2802 &env->fp_status))) { \
2803 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2804 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2807 helper_compute_fprf_##ttp(env, xt.tfld); \
2811 putVSR(rD(opcode) + 32, &xt, env); \
2812 float_check_status(env); \
2815 VSX_CVT_FP_TO_FP_VECTOR(xscvdpqp
, 1, float64
, float128
, VsrD(0), f128
, 1)
2817 /* VSX_CVT_FP_TO_FP_HP - VSX floating point/floating point conversion
2818 * involving one half precision value
2819 * op - instruction mnemonic
2820 * nels - number of elements (1, 2 or 4)
2823 * sfld - source vsr_t field
2824 * tfld - target vsr_t field
2827 #define VSX_CVT_FP_TO_FP_HP(op, nels, stp, ttp, sfld, tfld, sfprf) \
2828 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2833 getVSR(xB(opcode), &xb, env); \
2834 memset(&xt, 0, sizeof(xt)); \
2836 for (i = 0; i < nels; i++) { \
2837 xt.tfld = stp##_to_##ttp(xb.sfld, 1, &env->fp_status); \
2838 if (unlikely(stp##_is_signaling_nan(xb.sfld, \
2839 &env->fp_status))) { \
2840 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2841 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2844 helper_compute_fprf_##ttp(env, xt.tfld); \
2848 putVSR(xT(opcode), &xt, env); \
2849 float_check_status(env); \
2852 VSX_CVT_FP_TO_FP_HP(xscvdphp
, 1, float64
, float16
, VsrD(0), VsrH(3), 1)
2853 VSX_CVT_FP_TO_FP_HP(xscvhpdp
, 1, float16
, float64
, VsrH(3), VsrD(0), 1)
2854 VSX_CVT_FP_TO_FP_HP(xvcvsphp
, 4, float32
, float16
, VsrW(i
), VsrH(2 * i
+ 1), 0)
2855 VSX_CVT_FP_TO_FP_HP(xvcvhpsp
, 4, float16
, float32
, VsrH(2 * i
+ 1), VsrW(i
), 0)
2858 * xscvqpdp isn't using VSX_CVT_FP_TO_FP() because xscvqpdpo will be
2859 * added to this later.
2861 void helper_xscvqpdp(CPUPPCState
*env
, uint32_t opcode
)
2865 getVSR(rB(opcode
) + 32, &xb
, env
);
2866 memset(&xt
, 0, sizeof(xt
));
2868 if (unlikely(Rc(opcode
) != 0)) {
2869 /* TODO: Support xscvqpdpo after round-to-odd is implemented */
2873 xt
.VsrD(0) = float128_to_float64(xb
.f128
, &env
->fp_status
);
2874 if (unlikely(float128_is_signaling_nan(xb
.f128
,
2875 &env
->fp_status
))) {
2876 float_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 0);
2877 xt
.VsrD(0) = float64_snan_to_qnan(xt
.VsrD(0));
2879 helper_compute_fprf_float64(env
, xt
.VsrD(0));
2881 putVSR(rD(opcode
) + 32, &xt
, env
);
2882 float_check_status(env
);
2885 uint64_t helper_xscvdpspn(CPUPPCState
*env
, uint64_t xb
)
2887 float_status tstat
= env
->fp_status
;
2888 set_float_exception_flags(0, &tstat
);
2890 return (uint64_t)float64_to_float32(xb
, &tstat
) << 32;
2893 uint64_t helper_xscvspdpn(CPUPPCState
*env
, uint64_t xb
)
2895 float_status tstat
= env
->fp_status
;
2896 set_float_exception_flags(0, &tstat
);
2898 return float32_to_float64(xb
>> 32, &tstat
);
2901 /* VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2902 * op - instruction mnemonic
2903 * nels - number of elements (1, 2 or 4)
2904 * stp - source type (float32 or float64)
2905 * ttp - target type (int32, uint32, int64 or uint64)
2906 * sfld - source vsr_t field
2907 * tfld - target vsr_t field
2908 * rnan - resulting NaN
2910 #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, rnan) \
2911 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2916 getVSR(xB(opcode), &xb, env); \
2917 getVSR(xT(opcode), &xt, env); \
2919 for (i = 0; i < nels; i++) { \
2920 if (unlikely(stp##_is_any_nan(xb.sfld))) { \
2921 if (stp##_is_signaling_nan(xb.sfld, &env->fp_status)) { \
2922 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2924 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2927 xt.tfld = stp##_to_##ttp##_round_to_zero(xb.sfld, \
2929 if (env->fp_status.float_exception_flags & float_flag_invalid) { \
2930 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2935 putVSR(xT(opcode), &xt, env); \
2936 float_check_status(env); \
2939 VSX_CVT_FP_TO_INT(xscvdpsxds
, 1, float64
, int64
, VsrD(0), VsrD(0), \
2940 0x8000000000000000ULL
)
2941 VSX_CVT_FP_TO_INT(xscvdpsxws
, 1, float64
, int32
, VsrD(0), VsrW(1), \
2943 VSX_CVT_FP_TO_INT(xscvdpuxds
, 1, float64
, uint64
, VsrD(0), VsrD(0), 0ULL)
2944 VSX_CVT_FP_TO_INT(xscvdpuxws
, 1, float64
, uint32
, VsrD(0), VsrW(1), 0U)
2945 VSX_CVT_FP_TO_INT(xvcvdpsxds
, 2, float64
, int64
, VsrD(i
), VsrD(i
), \
2946 0x8000000000000000ULL
)
2947 VSX_CVT_FP_TO_INT(xvcvdpsxws
, 2, float64
, int32
, VsrD(i
), VsrW(2*i
), \
2949 VSX_CVT_FP_TO_INT(xvcvdpuxds
, 2, float64
, uint64
, VsrD(i
), VsrD(i
), 0ULL)
2950 VSX_CVT_FP_TO_INT(xvcvdpuxws
, 2, float64
, uint32
, VsrD(i
), VsrW(2*i
), 0U)
2951 VSX_CVT_FP_TO_INT(xvcvspsxds
, 2, float32
, int64
, VsrW(2*i
), VsrD(i
), \
2952 0x8000000000000000ULL
)
2953 VSX_CVT_FP_TO_INT(xvcvspsxws
, 4, float32
, int32
, VsrW(i
), VsrW(i
), 0x80000000U
)
2954 VSX_CVT_FP_TO_INT(xvcvspuxds
, 2, float32
, uint64
, VsrW(2*i
), VsrD(i
), 0ULL)
2955 VSX_CVT_FP_TO_INT(xvcvspuxws
, 4, float32
, uint32
, VsrW(i
), VsrW(i
), 0U)
2957 /* VSX_CVT_FP_TO_INT_VECTOR - VSX floating point to integer conversion
2958 * op - instruction mnemonic
2959 * stp - source type (float32 or float64)
2960 * ttp - target type (int32, uint32, int64 or uint64)
2961 * sfld - source vsr_t field
2962 * tfld - target vsr_t field
2963 * rnan - resulting NaN
2965 #define VSX_CVT_FP_TO_INT_VECTOR(op, stp, ttp, sfld, tfld, rnan) \
2966 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2970 getVSR(rB(opcode) + 32, &xb, env); \
2971 memset(&xt, 0, sizeof(xt)); \
2973 if (unlikely(stp##_is_any_nan(xb.sfld))) { \
2974 if (stp##_is_signaling_nan(xb.sfld, &env->fp_status)) { \
2975 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2977 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2980 xt.tfld = stp##_to_##ttp##_round_to_zero(xb.sfld, \
2982 if (env->fp_status.float_exception_flags & float_flag_invalid) { \
2983 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2987 putVSR(rD(opcode) + 32, &xt, env); \
2988 float_check_status(env); \
2991 VSX_CVT_FP_TO_INT_VECTOR(xscvqpsdz
, float128
, int64
, f128
, VsrD(0), \
2992 0x8000000000000000ULL
)
2994 VSX_CVT_FP_TO_INT_VECTOR(xscvqpswz
, float128
, int32
, f128
, VsrD(0), \
2995 0xffffffff80000000ULL
)
2997 /* VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
2998 * op - instruction mnemonic
2999 * nels - number of elements (1, 2 or 4)
3000 * stp - source type (int32, uint32, int64 or uint64)
3001 * ttp - target type (float32 or float64)
3002 * sfld - source vsr_t field
3003 * tfld - target vsr_t field
3004 * jdef - definition of the j index (i or 2*i)
3007 #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf, r2sp) \
3008 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3013 getVSR(xB(opcode), &xb, env); \
3014 getVSR(xT(opcode), &xt, env); \
3016 for (i = 0; i < nels; i++) { \
3017 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
3019 xt.tfld = helper_frsp(env, xt.tfld); \
3022 helper_compute_fprf_float64(env, xt.tfld); \
3026 putVSR(xT(opcode), &xt, env); \
3027 float_check_status(env); \
3030 VSX_CVT_INT_TO_FP(xscvsxddp
, 1, int64
, float64
, VsrD(0), VsrD(0), 1, 0)
3031 VSX_CVT_INT_TO_FP(xscvuxddp
, 1, uint64
, float64
, VsrD(0), VsrD(0), 1, 0)
3032 VSX_CVT_INT_TO_FP(xscvsxdsp
, 1, int64
, float64
, VsrD(0), VsrD(0), 1, 1)
3033 VSX_CVT_INT_TO_FP(xscvuxdsp
, 1, uint64
, float64
, VsrD(0), VsrD(0), 1, 1)
3034 VSX_CVT_INT_TO_FP(xvcvsxddp
, 2, int64
, float64
, VsrD(i
), VsrD(i
), 0, 0)
3035 VSX_CVT_INT_TO_FP(xvcvuxddp
, 2, uint64
, float64
, VsrD(i
), VsrD(i
), 0, 0)
3036 VSX_CVT_INT_TO_FP(xvcvsxwdp
, 2, int32
, float64
, VsrW(2*i
), VsrD(i
), 0, 0)
3037 VSX_CVT_INT_TO_FP(xvcvuxwdp
, 2, uint64
, float64
, VsrW(2*i
), VsrD(i
), 0, 0)
3038 VSX_CVT_INT_TO_FP(xvcvsxdsp
, 2, int64
, float32
, VsrD(i
), VsrW(2*i
), 0, 0)
3039 VSX_CVT_INT_TO_FP(xvcvuxdsp
, 2, uint64
, float32
, VsrD(i
), VsrW(2*i
), 0, 0)
3040 VSX_CVT_INT_TO_FP(xvcvsxwsp
, 4, int32
, float32
, VsrW(i
), VsrW(i
), 0, 0)
3041 VSX_CVT_INT_TO_FP(xvcvuxwsp
, 4, uint32
, float32
, VsrW(i
), VsrW(i
), 0, 0)
3043 /* VSX_CVT_INT_TO_FP_VECTOR - VSX integer to floating point conversion
3044 * op - instruction mnemonic
3045 * stp - source type (int32, uint32, int64 or uint64)
3046 * ttp - target type (float32 or float64)
3047 * sfld - source vsr_t field
3048 * tfld - target vsr_t field
3050 #define VSX_CVT_INT_TO_FP_VECTOR(op, stp, ttp, sfld, tfld) \
3051 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3055 getVSR(rB(opcode) + 32, &xb, env); \
3056 getVSR(rD(opcode) + 32, &xt, env); \
3058 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
3059 helper_compute_fprf_##ttp(env, xt.tfld); \
3061 putVSR(xT(opcode) + 32, &xt, env); \
3062 float_check_status(env); \
3065 VSX_CVT_INT_TO_FP_VECTOR(xscvsdqp
, int64
, float128
, VsrD(0), f128
)
3066 VSX_CVT_INT_TO_FP_VECTOR(xscvudqp
, uint64
, float128
, VsrD(0), f128
)
3068 /* For "use current rounding mode", define a value that will not be one of
3069 * the existing rounding model enums.
3071 #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
3072 float_round_up + float_round_to_zero)
3074 /* VSX_ROUND - VSX floating point round
3075 * op - instruction mnemonic
3076 * nels - number of elements (1, 2 or 4)
3077 * tp - type (float32 or float64)
3078 * fld - vsr_t field (VsrD(*) or VsrW(*))
3079 * rmode - rounding mode
3082 #define VSX_ROUND(op, nels, tp, fld, rmode, sfprf) \
3083 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3087 getVSR(xB(opcode), &xb, env); \
3088 getVSR(xT(opcode), &xt, env); \
3090 if (rmode != FLOAT_ROUND_CURRENT) { \
3091 set_float_rounding_mode(rmode, &env->fp_status); \
3094 for (i = 0; i < nels; i++) { \
3095 if (unlikely(tp##_is_signaling_nan(xb.fld, \
3096 &env->fp_status))) { \
3097 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
3098 xt.fld = tp##_snan_to_qnan(xb.fld); \
3100 xt.fld = tp##_round_to_int(xb.fld, &env->fp_status); \
3103 helper_compute_fprf_float64(env, xt.fld); \
3107 /* If this is not a "use current rounding mode" instruction, \
3108 * then inhibit setting of the XX bit and restore rounding \
3109 * mode from FPSCR */ \
3110 if (rmode != FLOAT_ROUND_CURRENT) { \
3111 fpscr_set_rounding_mode(env); \
3112 env->fp_status.float_exception_flags &= ~float_flag_inexact; \
3115 putVSR(xT(opcode), &xt, env); \
3116 float_check_status(env); \
3119 VSX_ROUND(xsrdpi
, 1, float64
, VsrD(0), float_round_ties_away
, 1)
3120 VSX_ROUND(xsrdpic
, 1, float64
, VsrD(0), FLOAT_ROUND_CURRENT
, 1)
3121 VSX_ROUND(xsrdpim
, 1, float64
, VsrD(0), float_round_down
, 1)
3122 VSX_ROUND(xsrdpip
, 1, float64
, VsrD(0), float_round_up
, 1)
3123 VSX_ROUND(xsrdpiz
, 1, float64
, VsrD(0), float_round_to_zero
, 1)
3125 VSX_ROUND(xvrdpi
, 2, float64
, VsrD(i
), float_round_ties_away
, 0)
3126 VSX_ROUND(xvrdpic
, 2, float64
, VsrD(i
), FLOAT_ROUND_CURRENT
, 0)
3127 VSX_ROUND(xvrdpim
, 2, float64
, VsrD(i
), float_round_down
, 0)
3128 VSX_ROUND(xvrdpip
, 2, float64
, VsrD(i
), float_round_up
, 0)
3129 VSX_ROUND(xvrdpiz
, 2, float64
, VsrD(i
), float_round_to_zero
, 0)
3131 VSX_ROUND(xvrspi
, 4, float32
, VsrW(i
), float_round_ties_away
, 0)
3132 VSX_ROUND(xvrspic
, 4, float32
, VsrW(i
), FLOAT_ROUND_CURRENT
, 0)
3133 VSX_ROUND(xvrspim
, 4, float32
, VsrW(i
), float_round_down
, 0)
3134 VSX_ROUND(xvrspip
, 4, float32
, VsrW(i
), float_round_up
, 0)
3135 VSX_ROUND(xvrspiz
, 4, float32
, VsrW(i
), float_round_to_zero
, 0)
3137 uint64_t helper_xsrsp(CPUPPCState
*env
, uint64_t xb
)
3139 helper_reset_fpstatus(env
);
3141 uint64_t xt
= helper_frsp(env
, xb
);
3143 helper_compute_fprf_float64(env
, xt
);
3144 float_check_status(env
);
3148 #define VSX_XXPERM(op, indexed) \
3149 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3151 ppc_vsr_t xt, xa, pcv, xto; \
3154 getVSR(xA(opcode), &xa, env); \
3155 getVSR(xT(opcode), &xt, env); \
3156 getVSR(xB(opcode), &pcv, env); \
3158 for (i = 0; i < 16; i++) { \
3159 idx = pcv.VsrB(i) & 0x1F; \
3163 xto.VsrB(i) = (idx <= 15) ? xa.VsrB(idx) : xt.VsrB(idx - 16); \
3165 putVSR(xT(opcode), &xto, env); \
3168 VSX_XXPERM(xxperm
, 0)
3169 VSX_XXPERM(xxpermr
, 1)
3171 void helper_xvxsigsp(CPUPPCState
*env
, uint32_t opcode
)
3174 uint32_t exp
, i
, fraction
;
3176 getVSR(xB(opcode
), &xb
, env
);
3177 memset(&xt
, 0, sizeof(xt
));
3179 for (i
= 0; i
< 4; i
++) {
3180 exp
= (xb
.VsrW(i
) >> 23) & 0xFF;
3181 fraction
= xb
.VsrW(i
) & 0x7FFFFF;
3182 if (exp
!= 0 && exp
!= 255) {
3183 xt
.VsrW(i
) = fraction
| 0x00800000;
3185 xt
.VsrW(i
) = fraction
;
3188 putVSR(xT(opcode
), &xt
, env
);
3191 /* VSX_TEST_DC - VSX floating point test data class
3192 * op - instruction mnemonic
3193 * nels - number of elements (1, 2 or 4)
3194 * xbn - VSR register number
3195 * tp - type (float32 or float64)
3196 * fld - vsr_t field (VsrD(*) or VsrW(*))
3197 * tfld - target vsr_t field (VsrD(*) or VsrW(*))
3198 * fld_max - target field max
3199 * scrf - set result in CR and FPCC
3201 #define VSX_TEST_DC(op, nels, xbn, tp, fld, tfld, fld_max, scrf) \
3202 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3205 uint32_t i, sign, dcmx; \
3206 uint32_t cc, match = 0; \
3208 getVSR(xbn, &xb, env); \
3210 memset(&xt, 0, sizeof(xt)); \
3211 dcmx = DCMX_XV(opcode); \
3213 dcmx = DCMX(opcode); \
3216 for (i = 0; i < nels; i++) { \
3217 sign = tp##_is_neg(xb.fld); \
3218 if (tp##_is_any_nan(xb.fld)) { \
3219 match = extract32(dcmx, 6, 1); \
3220 } else if (tp##_is_infinity(xb.fld)) { \
3221 match = extract32(dcmx, 4 + !sign, 1); \
3222 } else if (tp##_is_zero(xb.fld)) { \
3223 match = extract32(dcmx, 2 + !sign, 1); \
3224 } else if (tp##_is_zero_or_denormal(xb.fld)) { \
3225 match = extract32(dcmx, 0 + !sign, 1); \
3229 cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT; \
3230 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
3231 env->fpscr |= cc << FPSCR_FPRF; \
3232 env->crf[BF(opcode)] = cc; \
3234 xt.tfld = match ? fld_max : 0; \
3239 putVSR(xT(opcode), &xt, env); \
3243 VSX_TEST_DC(xvtstdcdp
, 2, xB(opcode
), float64
, VsrD(i
), VsrD(i
), UINT64_MAX
, 0)
3244 VSX_TEST_DC(xvtstdcsp
, 4, xB(opcode
), float32
, VsrW(i
), VsrW(i
), UINT32_MAX
, 0)
3245 VSX_TEST_DC(xststdcdp
, 1, xB(opcode
), float64
, VsrD(0), VsrD(0), 0, 1)
3246 VSX_TEST_DC(xststdcqp
, 1, (rB(opcode
) + 32), float128
, f128
, VsrD(0), 0, 1)
3248 void helper_xststdcsp(CPUPPCState
*env
, uint32_t opcode
)
3251 uint32_t dcmx
, sign
, exp
;
3252 uint32_t cc
, match
= 0, not_sp
= 0;
3254 getVSR(xB(opcode
), &xb
, env
);
3255 dcmx
= DCMX(opcode
);
3256 exp
= (xb
.VsrD(0) >> 52) & 0x7FF;
3258 sign
= float64_is_neg(xb
.VsrD(0));
3259 if (float64_is_any_nan(xb
.VsrD(0))) {
3260 match
= extract32(dcmx
, 6, 1);
3261 } else if (float64_is_infinity(xb
.VsrD(0))) {
3262 match
= extract32(dcmx
, 4 + !sign
, 1);
3263 } else if (float64_is_zero(xb
.VsrD(0))) {
3264 match
= extract32(dcmx
, 2 + !sign
, 1);
3265 } else if (float64_is_zero_or_denormal(xb
.VsrD(0)) ||
3266 (exp
> 0 && exp
< 0x381)) {
3267 match
= extract32(dcmx
, 0 + !sign
, 1);
3270 not_sp
= !float64_eq(xb
.VsrD(0),
3272 float64_to_float32(xb
.VsrD(0), &env
->fp_status
),
3273 &env
->fp_status
), &env
->fp_status
);
3275 cc
= sign
<< CRF_LT_BIT
| match
<< CRF_EQ_BIT
| not_sp
<< CRF_SO_BIT
;
3276 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
3277 env
->fpscr
|= cc
<< FPSCR_FPRF
;
3278 env
->crf
[BF(opcode
)] = cc
;