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);
746 #define FPU_MADDSUB_UPDATE(NAME, TP) \
747 static void NAME(CPUPPCState *env, TP arg1, TP arg2, TP arg3, \
748 unsigned int madd_flags) \
750 if (TP##_is_signaling_nan(arg1, &env->fp_status) || \
751 TP##_is_signaling_nan(arg2, &env->fp_status) || \
752 TP##_is_signaling_nan(arg3, &env->fp_status)) { \
753 /* sNaN operation */ \
754 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1); \
756 if ((TP##_is_infinity(arg1) && TP##_is_zero(arg2)) || \
757 (TP##_is_zero(arg1) && TP##_is_infinity(arg2))) { \
758 /* Multiplication of zero by infinity */ \
759 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIMZ, 1); \
761 if ((TP##_is_infinity(arg1) || TP##_is_infinity(arg2)) && \
762 TP##_is_infinity(arg3)) { \
763 uint8_t aSign, bSign, cSign; \
765 aSign = TP##_is_neg(arg1); \
766 bSign = TP##_is_neg(arg2); \
767 cSign = TP##_is_neg(arg3); \
768 if (madd_flags & float_muladd_negate_c) { \
771 if (aSign ^ bSign ^ cSign) { \
772 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, 1); \
776 FPU_MADDSUB_UPDATE(float32_maddsub_update_excp, float32)
777 FPU_MADDSUB_UPDATE(float64_maddsub_update_excp, float64)
779 #define FPU_FMADD(op, madd_flags) \
780 uint64_t helper_##op(CPUPPCState *env, uint64_t arg1, \
781 uint64_t arg2, uint64_t arg3) \
784 float64 ret = float64_muladd(arg1, arg2, arg3, madd_flags, \
786 flags = get_float_exception_flags(&env->fp_status); \
788 if (flags & float_flag_invalid) { \
789 float64_maddsub_update_excp(env, arg1, arg2, arg3, \
792 float_check_status(env); \
798 #define MSUB_FLGS float_muladd_negate_c
799 #define NMADD_FLGS float_muladd_negate_result
800 #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
802 FPU_FMADD(fmadd, MADD_FLGS)
803 FPU_FMADD(fnmadd, NMADD_FLGS)
804 FPU_FMADD(fmsub, MSUB_FLGS)
805 FPU_FMADD(fnmsub, NMSUB_FLGS)
808 uint64_t helper_frsp(CPUPPCState *env, uint64_t arg)
815 if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
816 /* sNaN square root */
817 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
819 f32 = float64_to_float32(farg.d, &env->fp_status);
820 farg.d = float32_to_float64(f32, &env->fp_status);
826 uint64_t helper_fsqrt(CPUPPCState *env, uint64_t arg)
832 if (unlikely(float64_is_any_nan(farg.d))) {
833 if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
834 /* sNaN reciprocal square root */
835 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
836 farg.ll = float64_snan_to_qnan(farg.ll);
838 } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
839 /* Square root of a negative nonzero number */
840 farg.ll = float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, 1);
842 farg.d = float64_sqrt(farg.d, &env->fp_status);
848 uint64_t helper_fre(CPUPPCState *env, uint64_t arg)
854 if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
855 /* sNaN reciprocal */
856 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
858 farg.d = float64_div(float64_one, farg.d, &env->fp_status);
863 uint64_t helper_fres(CPUPPCState *env, uint64_t arg)
870 if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
871 /* sNaN reciprocal */
872 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
874 farg.d = float64_div(float64_one, farg.d, &env->fp_status);
875 f32 = float64_to_float32(farg.d, &env->fp_status);
876 farg.d = float32_to_float64(f32, &env->fp_status);
881 /* frsqrte - frsqrte. */
882 uint64_t helper_frsqrte(CPUPPCState *env, uint64_t arg)
888 if (unlikely(float64_is_any_nan(farg.d))) {
889 if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
890 /* sNaN reciprocal square root */
891 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
892 farg.ll = float64_snan_to_qnan(farg.ll);
894 } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
895 /* Reciprocal square root of a negative nonzero number */
896 farg.ll = float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, 1);
898 farg.d = float64_sqrt(farg.d, &env->fp_status);
899 farg.d = float64_div(float64_one, farg.d, &env->fp_status);
906 uint64_t helper_fsel(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
913 if ((!float64_is_neg(farg1.d) || float64_is_zero(farg1.d)) &&
914 !float64_is_any_nan(farg1.d)) {
921 uint32_t helper_ftdiv(uint64_t fra, uint64_t frb)
926 if (unlikely(float64_is_infinity(fra) ||
927 float64_is_infinity(frb) ||
928 float64_is_zero(frb))) {
932 int e_a = ppc_float64_get_unbiased_exp(fra);
933 int e_b = ppc_float64_get_unbiased_exp(frb);
935 if (unlikely(float64_is_any_nan(fra) ||
936 float64_is_any_nan(frb))) {
938 } else if ((e_b <= -1022) || (e_b >= 1021)) {
940 } else if (!float64_is_zero(fra) &&
941 (((e_a - e_b) >= 1023) ||
942 ((e_a - e_b) <= -1021) ||
947 if (unlikely(float64_is_zero_or_denormal(frb))) {
948 /* XB is not zero because of the above check and */
949 /* so must be denormalized. */
954 return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
957 uint32_t helper_ftsqrt(uint64_t frb)
962 if (unlikely(float64_is_infinity(frb) || float64_is_zero(frb))) {
966 int e_b = ppc_float64_get_unbiased_exp(frb);
968 if (unlikely(float64_is_any_nan(frb))) {
970 } else if (unlikely(float64_is_zero(frb))) {
972 } else if (unlikely(float64_is_neg(frb))) {
974 } else if (!float64_is_zero(frb) && (e_b <= (-1022+52))) {
978 if (unlikely(float64_is_zero_or_denormal(frb))) {
979 /* XB is not zero because of the above check and */
980 /* therefore must be denormalized. */
985 return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
988 void helper_fcmpu(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
991 CPU_DoubleU farg1, farg2;
997 if (unlikely(float64_is_any_nan(farg1.d) ||
998 float64_is_any_nan(farg2.d))) {
1000 } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1002 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1008 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1009 env->fpscr |= ret << FPSCR_FPRF;
1010 env->crf[crfD] = ret;
1011 if (unlikely(ret == 0x01UL
1012 && (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
1013 float64_is_signaling_nan(farg2.d, &env->fp_status)))) {
1014 /* sNaN comparison */
1015 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
1019 void helper_fcmpo(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
1022 CPU_DoubleU farg1, farg2;
1028 if (unlikely(float64_is_any_nan(farg1.d) ||
1029 float64_is_any_nan(farg2.d))) {
1031 } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1033 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1039 env->fpscr &= ~(0x0F << FPSCR_FPRF);
1040 env->fpscr |= ret << FPSCR_FPRF;
1041 env->crf[crfD] = ret;
1042 if (unlikely(ret == 0x01UL)) {
1043 if (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
1044 float64_is_signaling_nan(farg2.d, &env->fp_status)) {
1045 /* sNaN comparison */
1046 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN |
1047 POWERPC_EXCP_FP_VXVC, 1);
1049 /* qNaN comparison */
1050 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 1);
1055 /* Single-precision floating-point conversions */
1056 static inline uint32_t efscfsi(CPUPPCState *env, uint32_t val)
1060 u.f = int32_to_float32(val, &env->vec_status);
1065 static inline uint32_t efscfui(CPUPPCState *env, uint32_t val)
1069 u.f = uint32_to_float32(val, &env->vec_status);
1074 static inline int32_t efsctsi(CPUPPCState *env, uint32_t val)
1079 /* NaN are not treated the same way IEEE 754 does */
1080 if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1084 return float32_to_int32(u.f, &env->vec_status);
1087 static inline uint32_t efsctui(CPUPPCState *env, uint32_t val)
1092 /* NaN are not treated the same way IEEE 754 does */
1093 if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1097 return float32_to_uint32(u.f, &env->vec_status);
1100 static inline uint32_t efsctsiz(CPUPPCState *env, uint32_t val)
1105 /* NaN are not treated the same way IEEE 754 does */
1106 if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1110 return float32_to_int32_round_to_zero(u.f, &env->vec_status);
1113 static inline uint32_t efsctuiz(CPUPPCState *env, uint32_t val)
1118 /* NaN are not treated the same way IEEE 754 does */
1119 if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1123 return float32_to_uint32_round_to_zero(u.f, &env->vec_status);
1126 static inline uint32_t efscfsf(CPUPPCState *env, uint32_t val)
1131 u.f = int32_to_float32(val, &env->vec_status);
1132 tmp = int64_to_float32(1ULL << 32, &env->vec_status);
1133 u.f = float32_div(u.f, tmp, &env->vec_status);
1138 static inline uint32_t efscfuf(CPUPPCState *env, uint32_t val)
1143 u.f = uint32_to_float32(val, &env->vec_status);
1144 tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1145 u.f = float32_div(u.f, tmp, &env->vec_status);
1150 static inline uint32_t efsctsf(CPUPPCState *env, uint32_t val)
1156 /* NaN are not treated the same way IEEE 754 does */
1157 if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1160 tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1161 u.f = float32_mul(u.f, tmp, &env->vec_status);
1163 return float32_to_int32(u.f, &env->vec_status);
1166 static inline uint32_t efsctuf(CPUPPCState *env, uint32_t val)
1172 /* NaN are not treated the same way IEEE 754 does */
1173 if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1176 tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1177 u.f = float32_mul(u.f, tmp, &env->vec_status);
1179 return float32_to_uint32(u.f, &env->vec_status);
1182 #define HELPER_SPE_SINGLE_CONV(name) \
1183 uint32_t helper_e##name(CPUPPCState *env, uint32_t val) \
1185 return e##name(env, val); \
1188 HELPER_SPE_SINGLE_CONV(fscfsi);
1190 HELPER_SPE_SINGLE_CONV(fscfui);
1192 HELPER_SPE_SINGLE_CONV(fscfuf);
1194 HELPER_SPE_SINGLE_CONV(fscfsf);
1196 HELPER_SPE_SINGLE_CONV(fsctsi);
1198 HELPER_SPE_SINGLE_CONV(fsctui);
1200 HELPER_SPE_SINGLE_CONV(fsctsiz);
1202 HELPER_SPE_SINGLE_CONV(fsctuiz);
1204 HELPER_SPE_SINGLE_CONV(fsctsf);
1206 HELPER_SPE_SINGLE_CONV(fsctuf);
1208 #define HELPER_SPE_VECTOR_CONV(name) \
1209 uint64_t helper_ev##name(CPUPPCState *env, uint64_t val) \
1211 return ((uint64_t)e##name(env, val >> 32) << 32) | \
1212 (uint64_t)e##name(env, val); \
1215 HELPER_SPE_VECTOR_CONV(fscfsi);
1217 HELPER_SPE_VECTOR_CONV(fscfui);
1219 HELPER_SPE_VECTOR_CONV(fscfuf);
1221 HELPER_SPE_VECTOR_CONV(fscfsf);
1223 HELPER_SPE_VECTOR_CONV(fsctsi);
1225 HELPER_SPE_VECTOR_CONV(fsctui);
1227 HELPER_SPE_VECTOR_CONV(fsctsiz);
1229 HELPER_SPE_VECTOR_CONV(fsctuiz);
1231 HELPER_SPE_VECTOR_CONV(fsctsf);
1233 HELPER_SPE_VECTOR_CONV(fsctuf);
1235 /* Single-precision floating-point arithmetic */
1236 static inline uint32_t efsadd(CPUPPCState *env, uint32_t op1, uint32_t op2)
1242 u1.f = float32_add(u1.f, u2.f, &env->vec_status);
1246 static inline uint32_t efssub(CPUPPCState *env, uint32_t op1, uint32_t op2)
1252 u1.f = float32_sub(u1.f, u2.f, &env->vec_status);
1256 static inline uint32_t efsmul(CPUPPCState *env, uint32_t op1, uint32_t op2)
1262 u1.f = float32_mul(u1.f, u2.f, &env->vec_status);
1266 static inline uint32_t efsdiv(CPUPPCState *env, uint32_t op1, uint32_t op2)
1272 u1.f = float32_div(u1.f, u2.f, &env->vec_status);
1276 #define HELPER_SPE_SINGLE_ARITH(name) \
1277 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1279 return e##name(env, op1, op2); \
1282 HELPER_SPE_SINGLE_ARITH(fsadd);
1284 HELPER_SPE_SINGLE_ARITH(fssub);
1286 HELPER_SPE_SINGLE_ARITH(fsmul);
1288 HELPER_SPE_SINGLE_ARITH(fsdiv);
1290 #define HELPER_SPE_VECTOR_ARITH(name) \
1291 uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1293 return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) | \
1294 (uint64_t)e##name(env, op1, op2); \
1297 HELPER_SPE_VECTOR_ARITH(fsadd);
1299 HELPER_SPE_VECTOR_ARITH(fssub);
1301 HELPER_SPE_VECTOR_ARITH(fsmul);
1303 HELPER_SPE_VECTOR_ARITH(fsdiv);
1305 /* Single-precision floating-point comparisons */
1306 static inline uint32_t efscmplt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1312 return float32_lt(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1315 static inline uint32_t efscmpgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1321 return float32_le(u1.f, u2.f, &env->vec_status) ? 0 : 4;
1324 static inline uint32_t efscmpeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1330 return float32_eq(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1333 static inline uint32_t efststlt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1335 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1336 return efscmplt(env, op1, op2);
1339 static inline uint32_t efststgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1341 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1342 return efscmpgt(env, op1, op2);
1345 static inline uint32_t efststeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1347 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1348 return efscmpeq(env, op1, op2);
1351 #define HELPER_SINGLE_SPE_CMP(name) \
1352 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1354 return e##name(env, op1, op2); \
1357 HELPER_SINGLE_SPE_CMP(fststlt);
1359 HELPER_SINGLE_SPE_CMP(fststgt);
1361 HELPER_SINGLE_SPE_CMP(fststeq);
1363 HELPER_SINGLE_SPE_CMP(fscmplt);
1365 HELPER_SINGLE_SPE_CMP(fscmpgt);
1367 HELPER_SINGLE_SPE_CMP(fscmpeq);
1369 static inline uint32_t evcmp_merge(int t0, int t1)
1371 return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
1374 #define HELPER_VECTOR_SPE_CMP(name) \
1375 uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1377 return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32), \
1378 e##name(env, op1, op2)); \
1381 HELPER_VECTOR_SPE_CMP(fststlt);
1383 HELPER_VECTOR_SPE_CMP(fststgt);
1385 HELPER_VECTOR_SPE_CMP(fststeq);
1387 HELPER_VECTOR_SPE_CMP(fscmplt);
1389 HELPER_VECTOR_SPE_CMP(fscmpgt);
1391 HELPER_VECTOR_SPE_CMP(fscmpeq);
1393 /* Double-precision floating-point conversion */
1394 uint64_t helper_efdcfsi(CPUPPCState *env, uint32_t val)
1398 u.d = int32_to_float64(val, &env->vec_status);
1403 uint64_t helper_efdcfsid(CPUPPCState *env, uint64_t val)
1407 u.d = int64_to_float64(val, &env->vec_status);
1412 uint64_t helper_efdcfui(CPUPPCState *env, uint32_t val)
1416 u.d = uint32_to_float64(val, &env->vec_status);
1421 uint64_t helper_efdcfuid(CPUPPCState *env, uint64_t val)
1425 u.d = uint64_to_float64(val, &env->vec_status);
1430 uint32_t helper_efdctsi(CPUPPCState *env, uint64_t val)
1435 /* NaN are not treated the same way IEEE 754 does */
1436 if (unlikely(float64_is_any_nan(u.d))) {
1440 return float64_to_int32(u.d, &env->vec_status);
1443 uint32_t helper_efdctui(CPUPPCState *env, uint64_t val)
1448 /* NaN are not treated the same way IEEE 754 does */
1449 if (unlikely(float64_is_any_nan(u.d))) {
1453 return float64_to_uint32(u.d, &env->vec_status);
1456 uint32_t helper_efdctsiz(CPUPPCState *env, uint64_t val)
1461 /* NaN are not treated the same way IEEE 754 does */
1462 if (unlikely(float64_is_any_nan(u.d))) {
1466 return float64_to_int32_round_to_zero(u.d, &env->vec_status);
1469 uint64_t helper_efdctsidz(CPUPPCState *env, uint64_t val)
1474 /* NaN are not treated the same way IEEE 754 does */
1475 if (unlikely(float64_is_any_nan(u.d))) {
1479 return float64_to_int64_round_to_zero(u.d, &env->vec_status);
1482 uint32_t helper_efdctuiz(CPUPPCState *env, uint64_t val)
1487 /* NaN are not treated the same way IEEE 754 does */
1488 if (unlikely(float64_is_any_nan(u.d))) {
1492 return float64_to_uint32_round_to_zero(u.d, &env->vec_status);
1495 uint64_t helper_efdctuidz(CPUPPCState *env, uint64_t val)
1500 /* NaN are not treated the same way IEEE 754 does */
1501 if (unlikely(float64_is_any_nan(u.d))) {
1505 return float64_to_uint64_round_to_zero(u.d, &env->vec_status);
1508 uint64_t helper_efdcfsf(CPUPPCState *env, uint32_t val)
1513 u.d = int32_to_float64(val, &env->vec_status);
1514 tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1515 u.d = float64_div(u.d, tmp, &env->vec_status);
1520 uint64_t helper_efdcfuf(CPUPPCState *env, uint32_t val)
1525 u.d = uint32_to_float64(val, &env->vec_status);
1526 tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1527 u.d = float64_div(u.d, tmp, &env->vec_status);
1532 uint32_t helper_efdctsf(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))) {
1542 tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1543 u.d = float64_mul(u.d, tmp, &env->vec_status);
1545 return float64_to_int32(u.d, &env->vec_status);
1548 uint32_t helper_efdctuf(CPUPPCState *env, uint64_t val)
1554 /* NaN are not treated the same way IEEE 754 does */
1555 if (unlikely(float64_is_any_nan(u.d))) {
1558 tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1559 u.d = float64_mul(u.d, tmp, &env->vec_status);
1561 return float64_to_uint32(u.d, &env->vec_status);
1564 uint32_t helper_efscfd(CPUPPCState *env, uint64_t val)
1570 u2.f = float64_to_float32(u1.d, &env->vec_status);
1575 uint64_t helper_efdcfs(CPUPPCState *env, uint32_t val)
1581 u2.d = float32_to_float64(u1.f, &env->vec_status);
1586 /* Double precision fixed-point arithmetic */
1587 uint64_t helper_efdadd(CPUPPCState *env, uint64_t op1, uint64_t op2)
1593 u1.d = float64_add(u1.d, u2.d, &env->vec_status);
1597 uint64_t helper_efdsub(CPUPPCState *env, uint64_t op1, uint64_t op2)
1603 u1.d = float64_sub(u1.d, u2.d, &env->vec_status);
1607 uint64_t helper_efdmul(CPUPPCState *env, uint64_t op1, uint64_t op2)
1613 u1.d = float64_mul(u1.d, u2.d, &env->vec_status);
1617 uint64_t helper_efddiv(CPUPPCState *env, uint64_t op1, uint64_t op2)
1623 u1.d = float64_div(u1.d, u2.d, &env->vec_status);
1627 /* Double precision floating point helpers */
1628 uint32_t helper_efdtstlt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1634 return float64_lt(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1637 uint32_t helper_efdtstgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1643 return float64_le(u1.d, u2.d, &env->vec_status) ? 0 : 4;
1646 uint32_t helper_efdtsteq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1652 return float64_eq_quiet(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1655 uint32_t helper_efdcmplt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1657 /* XXX: TODO: test special values (NaN, infinites, ...) */
1658 return helper_efdtstlt(env, op1, op2);
1661 uint32_t helper_efdcmpgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1663 /* XXX: TODO: test special values (NaN, infinites, ...) */
1664 return helper_efdtstgt(env, op1, op2);
1667 uint32_t helper_efdcmpeq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1669 /* XXX: TODO: test special values (NaN, infinites, ...) */
1670 return helper_efdtsteq(env, op1, op2);
1673 #define float64_to_float64(x, env) x
1676 /* VSX_ADD_SUB - VSX floating point add/subract
1677 * name - instruction mnemonic
1678 * op - operation (add or sub)
1679 * nels - number of elements (1, 2 or 4)
1680 * tp - type (float32 or float64)
1681 * fld - vsr_t field (VsrD(*) or VsrW(*))
1684 #define VSX_ADD_SUB(name, op, nels, tp, fld, sfprf, r2sp) \
1685 void helper_##name(CPUPPCState *env, uint32_t opcode) \
1687 ppc_vsr_t xt, xa, xb; \
1690 getVSR(xA(opcode), &xa, env); \
1691 getVSR(xB(opcode), &xb, env); \
1692 getVSR(xT(opcode), &xt, env); \
1693 helper_reset_fpstatus(env); \
1695 for (i = 0; i < nels; i++) { \
1696 float_status tstat = env->fp_status; \
1697 set_float_exception_flags(0, &tstat); \
1698 xt.fld = tp##_##op(xa.fld, xb.fld, &tstat); \
1699 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1701 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1702 if (tp##_is_infinity(xa.fld) && tp##_is_infinity(xb.fld)) { \
1703 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
1704 } else if (tp##_is_signaling_nan(xa.fld, &tstat) || \
1705 tp##_is_signaling_nan(xb.fld, &tstat)) { \
1706 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1711 xt.fld = helper_frsp(env, xt.fld); \
1715 helper_compute_fprf_float64(env, xt.fld); \
1718 putVSR(xT(opcode), &xt, env); \
1719 float_check_status(env); \
1722 VSX_ADD_SUB(xsadddp, add, 1, float64, VsrD(0), 1, 0)
1723 VSX_ADD_SUB(xsaddsp, add, 1, float64, VsrD(0), 1, 1)
1724 VSX_ADD_SUB(xvadddp, add, 2, float64, VsrD(i), 0, 0)
1725 VSX_ADD_SUB(xvaddsp, add, 4, float32, VsrW(i), 0, 0)
1726 VSX_ADD_SUB(xssubdp, sub, 1, float64, VsrD(0), 1, 0)
1727 VSX_ADD_SUB(xssubsp, sub, 1, float64, VsrD(0), 1, 1)
1728 VSX_ADD_SUB(xvsubdp, sub, 2, float64, VsrD(i), 0, 0)
1729 VSX_ADD_SUB(xvsubsp, sub, 4, float32, VsrW(i), 0, 0)
1731 void helper_xsaddqp(CPUPPCState *env, uint32_t opcode)
1733 ppc_vsr_t xt, xa, xb;
1736 getVSR(rA(opcode) + 32, &xa, env);
1737 getVSR(rB(opcode) + 32, &xb, env);
1738 getVSR(rD(opcode) + 32, &xt, env);
1739 helper_reset_fpstatus(env);
1741 tstat = env->fp_status;
1742 if (unlikely(Rc(opcode) != 0)) {
1743 tstat.float_rounding_mode = float_round_to_odd;
1746 set_float_exception_flags(0, &tstat);
1747 xt.f128 = float128_add(xa.f128, xb.f128, &tstat);
1748 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1750 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
1751 if (float128_is_infinity(xa.f128) && float128_is_infinity(xb.f128)) {
1752 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, 1);
1753 } else if (float128_is_signaling_nan(xa.f128, &tstat) ||
1754 float128_is_signaling_nan(xb.f128, &tstat)) {
1755 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
1759 helper_compute_fprf_float128(env, xt.f128);
1761 putVSR(rD(opcode) + 32, &xt, env);
1762 float_check_status(env);
1765 /* VSX_MUL - VSX floating point multiply
1766 * op - instruction mnemonic
1767 * nels - number of elements (1, 2 or 4)
1768 * tp - type (float32 or float64)
1769 * fld - vsr_t field (VsrD(*) or VsrW(*))
1772 #define VSX_MUL(op, nels, tp, fld, sfprf, r2sp) \
1773 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1775 ppc_vsr_t xt, xa, xb; \
1778 getVSR(xA(opcode), &xa, env); \
1779 getVSR(xB(opcode), &xb, env); \
1780 getVSR(xT(opcode), &xt, env); \
1781 helper_reset_fpstatus(env); \
1783 for (i = 0; i < nels; i++) { \
1784 float_status tstat = env->fp_status; \
1785 set_float_exception_flags(0, &tstat); \
1786 xt.fld = tp##_mul(xa.fld, xb.fld, &tstat); \
1787 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1789 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1790 if ((tp##_is_infinity(xa.fld) && tp##_is_zero(xb.fld)) || \
1791 (tp##_is_infinity(xb.fld) && tp##_is_zero(xa.fld))) { \
1792 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIMZ, sfprf); \
1793 } else if (tp##_is_signaling_nan(xa.fld, &tstat) || \
1794 tp##_is_signaling_nan(xb.fld, &tstat)) { \
1795 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1800 xt.fld = helper_frsp(env, xt.fld); \
1804 helper_compute_fprf_float64(env, xt.fld); \
1808 putVSR(xT(opcode), &xt, env); \
1809 float_check_status(env); \
1812 VSX_MUL(xsmuldp, 1, float64, VsrD(0), 1, 0)
1813 VSX_MUL(xsmulsp, 1, float64, VsrD(0), 1, 1)
1814 VSX_MUL(xvmuldp, 2, float64, VsrD(i), 0, 0)
1815 VSX_MUL(xvmulsp, 4, float32, VsrW(i), 0, 0)
1817 void helper_xsmulqp(CPUPPCState *env, uint32_t opcode)
1819 ppc_vsr_t xt, xa, xb;
1822 getVSR(rA(opcode) + 32, &xa, env);
1823 getVSR(rB(opcode) + 32, &xb, env);
1824 getVSR(rD(opcode) + 32, &xt, env);
1826 helper_reset_fpstatus(env);
1827 tstat = env->fp_status;
1828 if (unlikely(Rc(opcode) != 0)) {
1829 tstat.float_rounding_mode = float_round_to_odd;
1832 set_float_exception_flags(0, &tstat);
1833 xt.f128 = float128_mul(xa.f128, xb.f128, &tstat);
1834 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1836 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
1837 if ((float128_is_infinity(xa.f128) && float128_is_zero(xb.f128)) ||
1838 (float128_is_infinity(xb.f128) && float128_is_zero(xa.f128))) {
1839 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIMZ, 1);
1840 } else if (float128_is_signaling_nan(xa.f128, &tstat) ||
1841 float128_is_signaling_nan(xb.f128, &tstat)) {
1842 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
1845 helper_compute_fprf_float128(env, xt.f128);
1847 putVSR(rD(opcode) + 32, &xt, env);
1848 float_check_status(env);
1851 /* VSX_DIV - VSX floating point divide
1852 * op - instruction mnemonic
1853 * nels - number of elements (1, 2 or 4)
1854 * tp - type (float32 or float64)
1855 * fld - vsr_t field (VsrD(*) or VsrW(*))
1858 #define VSX_DIV(op, nels, tp, fld, sfprf, r2sp) \
1859 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1861 ppc_vsr_t xt, xa, xb; \
1864 getVSR(xA(opcode), &xa, env); \
1865 getVSR(xB(opcode), &xb, env); \
1866 getVSR(xT(opcode), &xt, env); \
1867 helper_reset_fpstatus(env); \
1869 for (i = 0; i < nels; i++) { \
1870 float_status tstat = env->fp_status; \
1871 set_float_exception_flags(0, &tstat); \
1872 xt.fld = tp##_div(xa.fld, xb.fld, &tstat); \
1873 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1875 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1876 if (tp##_is_infinity(xa.fld) && tp##_is_infinity(xb.fld)) { \
1877 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIDI, sfprf); \
1878 } else if (tp##_is_zero(xa.fld) && \
1879 tp##_is_zero(xb.fld)) { \
1880 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXZDZ, sfprf); \
1881 } else if (tp##_is_signaling_nan(xa.fld, &tstat) || \
1882 tp##_is_signaling_nan(xb.fld, &tstat)) { \
1883 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1888 xt.fld = helper_frsp(env, xt.fld); \
1892 helper_compute_fprf_float64(env, xt.fld); \
1896 putVSR(xT(opcode), &xt, env); \
1897 float_check_status(env); \
1900 VSX_DIV(xsdivdp, 1, float64, VsrD(0), 1, 0)
1901 VSX_DIV(xsdivsp, 1, float64, VsrD(0), 1, 1)
1902 VSX_DIV(xvdivdp, 2, float64, VsrD(i), 0, 0)
1903 VSX_DIV(xvdivsp, 4, float32, VsrW(i), 0, 0)
1905 void helper_xsdivqp(CPUPPCState *env, uint32_t opcode)
1907 ppc_vsr_t xt, xa, xb;
1910 getVSR(rA(opcode) + 32, &xa, env);
1911 getVSR(rB(opcode) + 32, &xb, env);
1912 getVSR(rD(opcode) + 32, &xt, env);
1914 helper_reset_fpstatus(env);
1915 tstat = env->fp_status;
1916 if (unlikely(Rc(opcode) != 0)) {
1917 tstat.float_rounding_mode = float_round_to_odd;
1920 set_float_exception_flags(0, &tstat);
1921 xt.f128 = float128_div(xa.f128, xb.f128, &tstat);
1922 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1924 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
1925 if (float128_is_infinity(xa.f128) && float128_is_infinity(xb.f128)) {
1926 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXIDI, 1);
1927 } else if (float128_is_zero(xa.f128) &&
1928 float128_is_zero(xb.f128)) {
1929 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXZDZ, 1);
1930 } else if (float128_is_signaling_nan(xa.f128, &tstat) ||
1931 float128_is_signaling_nan(xb.f128, &tstat)) {
1932 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
1936 helper_compute_fprf_float128(env, xt.f128);
1937 putVSR(rD(opcode) + 32, &xt, env);
1938 float_check_status(env);
1941 /* VSX_RE - VSX floating point reciprocal estimate
1942 * op - instruction mnemonic
1943 * nels - number of elements (1, 2 or 4)
1944 * tp - type (float32 or float64)
1945 * fld - vsr_t field (VsrD(*) or VsrW(*))
1948 #define VSX_RE(op, nels, tp, fld, sfprf, r2sp) \
1949 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1954 getVSR(xB(opcode), &xb, env); \
1955 getVSR(xT(opcode), &xt, env); \
1956 helper_reset_fpstatus(env); \
1958 for (i = 0; i < nels; i++) { \
1959 if (unlikely(tp##_is_signaling_nan(xb.fld, &env->fp_status))) { \
1960 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1962 xt.fld = tp##_div(tp##_one, xb.fld, &env->fp_status); \
1965 xt.fld = helper_frsp(env, xt.fld); \
1969 helper_compute_fprf_float64(env, xt.fld); \
1973 putVSR(xT(opcode), &xt, env); \
1974 float_check_status(env); \
1977 VSX_RE(xsredp, 1, float64, VsrD(0), 1, 0)
1978 VSX_RE(xsresp, 1, float64, VsrD(0), 1, 1)
1979 VSX_RE(xvredp, 2, float64, VsrD(i), 0, 0)
1980 VSX_RE(xvresp, 4, float32, VsrW(i), 0, 0)
1982 /* VSX_SQRT - VSX floating point square root
1983 * op - instruction mnemonic
1984 * nels - number of elements (1, 2 or 4)
1985 * tp - type (float32 or float64)
1986 * fld - vsr_t field (VsrD(*) or VsrW(*))
1989 #define VSX_SQRT(op, nels, tp, fld, sfprf, r2sp) \
1990 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1995 getVSR(xB(opcode), &xb, env); \
1996 getVSR(xT(opcode), &xt, env); \
1997 helper_reset_fpstatus(env); \
1999 for (i = 0; i < nels; i++) { \
2000 float_status tstat = env->fp_status; \
2001 set_float_exception_flags(0, &tstat); \
2002 xt.fld = tp##_sqrt(xb.fld, &tstat); \
2003 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2005 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2006 if (tp##_is_neg(xb.fld) && !tp##_is_zero(xb.fld)) { \
2007 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2008 } else if (tp##_is_signaling_nan(xb.fld, &tstat)) { \
2009 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2014 xt.fld = helper_frsp(env, xt.fld); \
2018 helper_compute_fprf_float64(env, xt.fld); \
2022 putVSR(xT(opcode), &xt, env); \
2023 float_check_status(env); \
2026 VSX_SQRT(xssqrtdp, 1, float64, VsrD(0), 1, 0)
2027 VSX_SQRT(xssqrtsp, 1, float64, VsrD(0), 1, 1)
2028 VSX_SQRT(xvsqrtdp, 2, float64, VsrD(i), 0, 0)
2029 VSX_SQRT(xvsqrtsp, 4, float32, VsrW(i), 0, 0)
2031 /* VSX_RSQRTE - VSX floating point reciprocal square root estimate
2032 * op - instruction mnemonic
2033 * nels - number of elements (1, 2 or 4)
2034 * tp - type (float32 or float64)
2035 * fld - vsr_t field (VsrD(*) or VsrW(*))
2038 #define VSX_RSQRTE(op, nels, tp, fld, sfprf, r2sp) \
2039 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2044 getVSR(xB(opcode), &xb, env); \
2045 getVSR(xT(opcode), &xt, env); \
2046 helper_reset_fpstatus(env); \
2048 for (i = 0; i < nels; i++) { \
2049 float_status tstat = env->fp_status; \
2050 set_float_exception_flags(0, &tstat); \
2051 xt.fld = tp##_sqrt(xb.fld, &tstat); \
2052 xt.fld = tp##_div(tp##_one, xt.fld, &tstat); \
2053 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2055 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2056 if (tp##_is_neg(xb.fld) && !tp##_is_zero(xb.fld)) { \
2057 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2058 } else if (tp##_is_signaling_nan(xb.fld, &tstat)) { \
2059 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2064 xt.fld = helper_frsp(env, xt.fld); \
2068 helper_compute_fprf_float64(env, xt.fld); \
2072 putVSR(xT(opcode), &xt, env); \
2073 float_check_status(env); \
2076 VSX_RSQRTE(xsrsqrtedp, 1, float64, VsrD(0), 1, 0)
2077 VSX_RSQRTE(xsrsqrtesp, 1, float64, VsrD(0), 1, 1)
2078 VSX_RSQRTE(xvrsqrtedp, 2, float64, VsrD(i), 0, 0)
2079 VSX_RSQRTE(xvrsqrtesp, 4, float32, VsrW(i), 0, 0)
2081 /* VSX_TDIV - VSX floating point test for divide
2082 * op - instruction mnemonic
2083 * nels - number of elements (1, 2 or 4)
2084 * tp - type (float32 or float64)
2085 * fld - vsr_t field (VsrD(*) or VsrW(*))
2086 * emin - minimum unbiased exponent
2087 * emax - maximum unbiased exponent
2088 * nbits - number of fraction bits
2090 #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits) \
2091 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2098 getVSR(xA(opcode), &xa, env); \
2099 getVSR(xB(opcode), &xb, env); \
2101 for (i = 0; i < nels; i++) { \
2102 if (unlikely(tp##_is_infinity(xa.fld) || \
2103 tp##_is_infinity(xb.fld) || \
2104 tp##_is_zero(xb.fld))) { \
2108 int e_a = ppc_##tp##_get_unbiased_exp(xa.fld); \
2109 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld); \
2111 if (unlikely(tp##_is_any_nan(xa.fld) || \
2112 tp##_is_any_nan(xb.fld))) { \
2114 } else if ((e_b <= emin) || (e_b >= (emax-2))) { \
2116 } else if (!tp##_is_zero(xa.fld) && \
2117 (((e_a - e_b) >= emax) || \
2118 ((e_a - e_b) <= (emin+1)) || \
2119 (e_a <= (emin+nbits)))) { \
2123 if (unlikely(tp##_is_zero_or_denormal(xb.fld))) { \
2124 /* XB is not zero because of the above check and */ \
2125 /* so must be denormalized. */ \
2131 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2134 VSX_TDIV(xstdivdp, 1, float64, VsrD(0), -1022, 1023, 52)
2135 VSX_TDIV(xvtdivdp, 2, float64, VsrD(i), -1022, 1023, 52)
2136 VSX_TDIV(xvtdivsp, 4, float32, VsrW(i), -126, 127, 23)
2138 /* VSX_TSQRT - VSX floating point test for square root
2139 * op - instruction mnemonic
2140 * nels - number of elements (1, 2 or 4)
2141 * tp - type (float32 or float64)
2142 * fld - vsr_t field (VsrD(*) or VsrW(*))
2143 * emin - minimum unbiased exponent
2144 * emax - maximum unbiased exponent
2145 * nbits - number of fraction bits
2147 #define VSX_TSQRT(op, nels, tp, fld, emin, nbits) \
2148 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2155 getVSR(xA(opcode), &xa, env); \
2156 getVSR(xB(opcode), &xb, env); \
2158 for (i = 0; i < nels; i++) { \
2159 if (unlikely(tp##_is_infinity(xb.fld) || \
2160 tp##_is_zero(xb.fld))) { \
2164 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld); \
2166 if (unlikely(tp##_is_any_nan(xb.fld))) { \
2168 } else if (unlikely(tp##_is_zero(xb.fld))) { \
2170 } else if (unlikely(tp##_is_neg(xb.fld))) { \
2172 } else if (!tp##_is_zero(xb.fld) && \
2173 (e_b <= (emin+nbits))) { \
2177 if (unlikely(tp##_is_zero_or_denormal(xb.fld))) { \
2178 /* XB is not zero because of the above check and */ \
2179 /* therefore must be denormalized. */ \
2185 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2188 VSX_TSQRT(xstsqrtdp, 1, float64, VsrD(0), -1022, 52)
2189 VSX_TSQRT(xvtsqrtdp, 2, float64, VsrD(i), -1022, 52)
2190 VSX_TSQRT(xvtsqrtsp, 4, float32, VsrW(i), -126, 23)
2192 /* VSX_MADD - VSX floating point muliply/add variations
2193 * op - instruction mnemonic
2194 * nels - number of elements (1, 2 or 4)
2195 * tp - type (float32 or float64)
2196 * fld - vsr_t field (VsrD(*) or VsrW(*))
2197 * maddflgs - flags for the float*muladd routine that control the
2198 * various forms (madd, msub, nmadd, nmsub)
2199 * afrm - A form (1=A, 0=M)
2202 #define VSX_MADD(op, nels, tp, fld, maddflgs, afrm, sfprf, r2sp) \
2203 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2205 ppc_vsr_t xt_in, xa, xb, xt_out; \
2209 if (afrm) { /* AxB + T */ \
2212 } else { /* AxT + B */ \
2217 getVSR(xA(opcode), &xa, env); \
2218 getVSR(xB(opcode), &xb, env); \
2219 getVSR(xT(opcode), &xt_in, env); \
2223 helper_reset_fpstatus(env); \
2225 for (i = 0; i < nels; i++) { \
2226 float_status tstat = env->fp_status; \
2227 set_float_exception_flags(0, &tstat); \
2228 if (r2sp && (tstat.float_rounding_mode == float_round_nearest_even)) {\
2229 /* Avoid double rounding errors by rounding the intermediate */ \
2230 /* result to odd. */ \
2231 set_float_rounding_mode(float_round_to_zero, &tstat); \
2232 xt_out.fld = tp##_muladd(xa.fld, b->fld, c->fld, \
2233 maddflgs, &tstat); \
2234 xt_out.fld |= (get_float_exception_flags(&tstat) & \
2235 float_flag_inexact) != 0; \
2237 xt_out.fld = tp##_muladd(xa.fld, b->fld, c->fld, \
2238 maddflgs, &tstat); \
2240 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2242 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2243 tp##_maddsub_update_excp(env, xa.fld, b->fld, c->fld, maddflgs); \
2247 xt_out.fld = helper_frsp(env, xt_out.fld); \
2251 helper_compute_fprf_float64(env, xt_out.fld); \
2254 putVSR(xT(opcode), &xt_out, env); \
2255 float_check_status(env); \
2258 VSX_MADD(xsmaddadp, 1, float64, VsrD(0), MADD_FLGS, 1, 1, 0)
2259 VSX_MADD(xsmaddmdp, 1, float64, VsrD(0), MADD_FLGS, 0, 1, 0)
2260 VSX_MADD(xsmsubadp, 1, float64, VsrD(0), MSUB_FLGS, 1, 1, 0)
2261 VSX_MADD(xsmsubmdp, 1, float64, VsrD(0), MSUB_FLGS, 0, 1, 0)
2262 VSX_MADD(xsnmaddadp, 1, float64, VsrD(0), NMADD_FLGS, 1, 1, 0)
2263 VSX_MADD(xsnmaddmdp, 1, float64, VsrD(0), NMADD_FLGS, 0, 1, 0)
2264 VSX_MADD(xsnmsubadp, 1, float64, VsrD(0), NMSUB_FLGS, 1, 1, 0)
2265 VSX_MADD(xsnmsubmdp, 1, float64, VsrD(0), NMSUB_FLGS, 0, 1, 0)
2267 VSX_MADD(xsmaddasp, 1, float64, VsrD(0), MADD_FLGS, 1, 1, 1)
2268 VSX_MADD(xsmaddmsp, 1, float64, VsrD(0), MADD_FLGS, 0, 1, 1)
2269 VSX_MADD(xsmsubasp, 1, float64, VsrD(0), MSUB_FLGS, 1, 1, 1)
2270 VSX_MADD(xsmsubmsp, 1, float64, VsrD(0), MSUB_FLGS, 0, 1, 1)
2271 VSX_MADD(xsnmaddasp, 1, float64, VsrD(0), NMADD_FLGS, 1, 1, 1)
2272 VSX_MADD(xsnmaddmsp, 1, float64, VsrD(0), NMADD_FLGS, 0, 1, 1)
2273 VSX_MADD(xsnmsubasp, 1, float64, VsrD(0), NMSUB_FLGS, 1, 1, 1)
2274 VSX_MADD(xsnmsubmsp, 1, float64, VsrD(0), NMSUB_FLGS, 0, 1, 1)
2276 VSX_MADD(xvmaddadp, 2, float64, VsrD(i), MADD_FLGS, 1, 0, 0)
2277 VSX_MADD(xvmaddmdp, 2, float64, VsrD(i), MADD_FLGS, 0, 0, 0)
2278 VSX_MADD(xvmsubadp, 2, float64, VsrD(i), MSUB_FLGS, 1, 0, 0)
2279 VSX_MADD(xvmsubmdp, 2, float64, VsrD(i), MSUB_FLGS, 0, 0, 0)
2280 VSX_MADD(xvnmaddadp, 2, float64, VsrD(i), NMADD_FLGS, 1, 0, 0)
2281 VSX_MADD(xvnmaddmdp, 2, float64, VsrD(i), NMADD_FLGS, 0, 0, 0)
2282 VSX_MADD(xvnmsubadp, 2, float64, VsrD(i), NMSUB_FLGS, 1, 0, 0)
2283 VSX_MADD(xvnmsubmdp, 2, float64, VsrD(i), NMSUB_FLGS, 0, 0, 0)
2285 VSX_MADD(xvmaddasp, 4, float32, VsrW(i), MADD_FLGS, 1, 0, 0)
2286 VSX_MADD(xvmaddmsp, 4, float32, VsrW(i), MADD_FLGS, 0, 0, 0)
2287 VSX_MADD(xvmsubasp, 4, float32, VsrW(i), MSUB_FLGS, 1, 0, 0)
2288 VSX_MADD(xvmsubmsp, 4, float32, VsrW(i), MSUB_FLGS, 0, 0, 0)
2289 VSX_MADD(xvnmaddasp, 4, float32, VsrW(i), NMADD_FLGS, 1, 0, 0)
2290 VSX_MADD(xvnmaddmsp, 4, float32, VsrW(i), NMADD_FLGS, 0, 0, 0)
2291 VSX_MADD(xvnmsubasp, 4, float32, VsrW(i), NMSUB_FLGS, 1, 0, 0)
2292 VSX_MADD(xvnmsubmsp, 4, float32, VsrW(i), NMSUB_FLGS, 0, 0, 0)
2294 /* VSX_SCALAR_CMP_DP - VSX scalar floating point compare double precision
2295 * op - instruction mnemonic
2296 * cmp - comparison operation
2297 * exp - expected result of comparison
2298 * svxvc - set VXVC bit
2300 #define VSX_SCALAR_CMP_DP(op, cmp, exp, svxvc) \
2301 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2303 ppc_vsr_t xt, xa, xb; \
2304 bool vxsnan_flag = false, vxvc_flag = false, vex_flag = false; \
2306 getVSR(xA(opcode), &xa, env); \
2307 getVSR(xB(opcode), &xb, env); \
2308 getVSR(xT(opcode), &xt, env); \
2310 if (float64_is_signaling_nan(xa.VsrD(0), &env->fp_status) || \
2311 float64_is_signaling_nan(xb.VsrD(0), &env->fp_status)) { \
2312 vxsnan_flag = true; \
2313 if (fpscr_ve == 0 && svxvc) { \
2316 } else if (svxvc) { \
2317 vxvc_flag = float64_is_quiet_nan(xa.VsrD(0), &env->fp_status) || \
2318 float64_is_quiet_nan(xb.VsrD(0), &env->fp_status); \
2320 if (vxsnan_flag) { \
2321 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2324 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2326 vex_flag = fpscr_ve && (vxvc_flag || vxsnan_flag); \
2329 if (float64_##cmp(xb.VsrD(0), xa.VsrD(0), &env->fp_status) == exp) { \
2337 putVSR(xT(opcode), &xt, env); \
2338 helper_float_check_status(env); \
2341 VSX_SCALAR_CMP_DP(xscmpeqdp, eq, 1, 0)
2342 VSX_SCALAR_CMP_DP(xscmpgedp, le, 1, 1)
2343 VSX_SCALAR_CMP_DP(xscmpgtdp, lt, 1, 1)
2344 VSX_SCALAR_CMP_DP(xscmpnedp, eq, 0, 0)
2346 void helper_xscmpexpdp(CPUPPCState *env, uint32_t opcode)
2349 int64_t exp_a, exp_b;
2352 getVSR(xA(opcode), &xa, env);
2353 getVSR(xB(opcode), &xb, env);
2355 exp_a = extract64(xa.VsrD(0), 52, 11);
2356 exp_b = extract64(xb.VsrD(0), 52, 11);
2358 if (unlikely(float64_is_any_nan(xa.VsrD(0)) ||
2359 float64_is_any_nan(xb.VsrD(0)))) {
2362 if (exp_a < exp_b) {
2364 } else if (exp_a > exp_b) {
2371 env->fpscr &= ~(0x0F << FPSCR_FPRF);
2372 env->fpscr |= cc << FPSCR_FPRF;
2373 env->crf[BF(opcode)] = cc;
2375 helper_float_check_status(env);
2378 void helper_xscmpexpqp(CPUPPCState *env, uint32_t opcode)
2381 int64_t exp_a, exp_b;
2384 getVSR(rA(opcode) + 32, &xa, env);
2385 getVSR(rB(opcode) + 32, &xb, env);
2387 exp_a = extract64(xa.VsrD(0), 48, 15);
2388 exp_b = extract64(xb.VsrD(0), 48, 15);
2390 if (unlikely(float128_is_any_nan(xa.f128) ||
2391 float128_is_any_nan(xb.f128))) {
2394 if (exp_a < exp_b) {
2396 } else if (exp_a > exp_b) {
2403 env->fpscr &= ~(0x0F << FPSCR_FPRF);
2404 env->fpscr |= cc << FPSCR_FPRF;
2405 env->crf[BF(opcode)] = cc;
2407 helper_float_check_status(env);
2410 #define VSX_SCALAR_CMP(op, ordered) \
2411 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2415 bool vxsnan_flag = false, vxvc_flag = false; \
2417 helper_reset_fpstatus(env); \
2418 getVSR(xA(opcode), &xa, env); \
2419 getVSR(xB(opcode), &xb, env); \
2421 if (float64_is_signaling_nan(xa.VsrD(0), &env->fp_status) || \
2422 float64_is_signaling_nan(xb.VsrD(0), &env->fp_status)) { \
2423 vxsnan_flag = true; \
2425 if (fpscr_ve == 0 && ordered) { \
2428 } else if (float64_is_quiet_nan(xa.VsrD(0), &env->fp_status) || \
2429 float64_is_quiet_nan(xb.VsrD(0), &env->fp_status)) { \
2435 if (vxsnan_flag) { \
2436 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2439 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2442 if (float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) { \
2444 } else if (!float64_le(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) { \
2450 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
2451 env->fpscr |= cc << FPSCR_FPRF; \
2452 env->crf[BF(opcode)] = cc; \
2454 float_check_status(env); \
2457 VSX_SCALAR_CMP(xscmpodp, 1)
2458 VSX_SCALAR_CMP(xscmpudp, 0)
2460 #define VSX_SCALAR_CMPQ(op, ordered) \
2461 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2465 bool vxsnan_flag = false, vxvc_flag = false; \
2467 helper_reset_fpstatus(env); \
2468 getVSR(rA(opcode) + 32, &xa, env); \
2469 getVSR(rB(opcode) + 32, &xb, env); \
2471 if (float128_is_signaling_nan(xa.f128, &env->fp_status) || \
2472 float128_is_signaling_nan(xb.f128, &env->fp_status)) { \
2473 vxsnan_flag = true; \
2475 if (fpscr_ve == 0 && ordered) { \
2478 } else if (float128_is_quiet_nan(xa.f128, &env->fp_status) || \
2479 float128_is_quiet_nan(xb.f128, &env->fp_status)) { \
2485 if (vxsnan_flag) { \
2486 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2489 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2492 if (float128_lt(xa.f128, xb.f128, &env->fp_status)) { \
2494 } else if (!float128_le(xa.f128, xb.f128, &env->fp_status)) { \
2500 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
2501 env->fpscr |= cc << FPSCR_FPRF; \
2502 env->crf[BF(opcode)] = cc; \
2504 float_check_status(env); \
2507 VSX_SCALAR_CMPQ(xscmpoqp, 1)
2508 VSX_SCALAR_CMPQ(xscmpuqp, 0)
2510 /* VSX_MAX_MIN - VSX floating point maximum/minimum
2511 * name - instruction mnemonic
2512 * op - operation (max or min)
2513 * nels - number of elements (1, 2 or 4)
2514 * tp - type (float32 or float64)
2515 * fld - vsr_t field (VsrD(*) or VsrW(*))
2517 #define VSX_MAX_MIN(name, op, nels, tp, fld) \
2518 void helper_##name(CPUPPCState *env, uint32_t opcode) \
2520 ppc_vsr_t xt, xa, xb; \
2523 getVSR(xA(opcode), &xa, env); \
2524 getVSR(xB(opcode), &xb, env); \
2525 getVSR(xT(opcode), &xt, env); \
2527 for (i = 0; i < nels; i++) { \
2528 xt.fld = tp##_##op(xa.fld, xb.fld, &env->fp_status); \
2529 if (unlikely(tp##_is_signaling_nan(xa.fld, &env->fp_status) || \
2530 tp##_is_signaling_nan(xb.fld, &env->fp_status))) { \
2531 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2535 putVSR(xT(opcode), &xt, env); \
2536 float_check_status(env); \
2539 VSX_MAX_MIN(xsmaxdp, maxnum, 1, float64, VsrD(0))
2540 VSX_MAX_MIN(xvmaxdp, maxnum, 2, float64, VsrD(i))
2541 VSX_MAX_MIN(xvmaxsp, maxnum, 4, float32, VsrW(i))
2542 VSX_MAX_MIN(xsmindp, minnum, 1, float64, VsrD(0))
2543 VSX_MAX_MIN(xvmindp, minnum, 2, float64, VsrD(i))
2544 VSX_MAX_MIN(xvminsp, minnum, 4, float32, VsrW(i))
2546 #define VSX_MAX_MINC(name, max) \
2547 void helper_##name(CPUPPCState *env, uint32_t opcode) \
2549 ppc_vsr_t xt, xa, xb; \
2550 bool vxsnan_flag = false, vex_flag = false; \
2552 getVSR(rA(opcode) + 32, &xa, env); \
2553 getVSR(rB(opcode) + 32, &xb, env); \
2554 getVSR(rD(opcode) + 32, &xt, env); \
2556 if (unlikely(float64_is_any_nan(xa.VsrD(0)) || \
2557 float64_is_any_nan(xb.VsrD(0)))) { \
2558 if (float64_is_signaling_nan(xa.VsrD(0), &env->fp_status) || \
2559 float64_is_signaling_nan(xb.VsrD(0), &env->fp_status)) { \
2560 vxsnan_flag = true; \
2562 xt.VsrD(0) = xb.VsrD(0); \
2563 } else if ((max && \
2564 !float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) || \
2566 float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status))) { \
2567 xt.VsrD(0) = xa.VsrD(0); \
2569 xt.VsrD(0) = xb.VsrD(0); \
2572 vex_flag = fpscr_ve & vxsnan_flag; \
2573 if (vxsnan_flag) { \
2574 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2577 putVSR(rD(opcode) + 32, &xt, env); \
2581 VSX_MAX_MINC(xsmaxcdp, 1);
2582 VSX_MAX_MINC(xsmincdp, 0);
2584 #define VSX_MAX_MINJ(name, max) \
2585 void helper_##name(CPUPPCState *env, uint32_t opcode) \
2587 ppc_vsr_t xt, xa, xb; \
2588 bool vxsnan_flag = false, vex_flag = false; \
2590 getVSR(rA(opcode) + 32, &xa, env); \
2591 getVSR(rB(opcode) + 32, &xb, env); \
2592 getVSR(rD(opcode) + 32, &xt, env); \
2594 if (unlikely(float64_is_any_nan(xa.VsrD(0)))) { \
2595 if (float64_is_signaling_nan(xa.VsrD(0), &env->fp_status)) { \
2596 vxsnan_flag = true; \
2598 xt.VsrD(0) = xa.VsrD(0); \
2599 } else if (unlikely(float64_is_any_nan(xb.VsrD(0)))) { \
2600 if (float64_is_signaling_nan(xb.VsrD(0), &env->fp_status)) { \
2601 vxsnan_flag = true; \
2603 xt.VsrD(0) = xb.VsrD(0); \
2604 } else if (float64_is_zero(xa.VsrD(0)) && float64_is_zero(xb.VsrD(0))) { \
2606 if (!float64_is_neg(xa.VsrD(0)) || !float64_is_neg(xb.VsrD(0))) { \
2607 xt.VsrD(0) = 0ULL; \
2609 xt.VsrD(0) = 0x8000000000000000ULL; \
2612 if (float64_is_neg(xa.VsrD(0)) || float64_is_neg(xb.VsrD(0))) { \
2613 xt.VsrD(0) = 0x8000000000000000ULL; \
2615 xt.VsrD(0) = 0ULL; \
2618 } else if ((max && \
2619 !float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status)) || \
2621 float64_lt(xa.VsrD(0), xb.VsrD(0), &env->fp_status))) { \
2622 xt.VsrD(0) = xa.VsrD(0); \
2624 xt.VsrD(0) = xb.VsrD(0); \
2627 vex_flag = fpscr_ve & vxsnan_flag; \
2628 if (vxsnan_flag) { \
2629 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2632 putVSR(rD(opcode) + 32, &xt, env); \
2636 VSX_MAX_MINJ(xsmaxjdp, 1);
2637 VSX_MAX_MINJ(xsminjdp, 0);
2639 /* VSX_CMP - VSX floating point compare
2640 * op - instruction mnemonic
2641 * nels - number of elements (1, 2 or 4)
2642 * tp - type (float32 or float64)
2643 * fld - vsr_t field (VsrD(*) or VsrW(*))
2644 * cmp - comparison operation
2645 * svxvc - set VXVC bit
2646 * exp - expected result of comparison
2648 #define VSX_CMP(op, nels, tp, fld, cmp, svxvc, exp) \
2649 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2651 ppc_vsr_t xt, xa, xb; \
2654 int all_false = 1; \
2656 getVSR(xA(opcode), &xa, env); \
2657 getVSR(xB(opcode), &xb, env); \
2658 getVSR(xT(opcode), &xt, env); \
2660 for (i = 0; i < nels; i++) { \
2661 if (unlikely(tp##_is_any_nan(xa.fld) || \
2662 tp##_is_any_nan(xb.fld))) { \
2663 if (tp##_is_signaling_nan(xa.fld, &env->fp_status) || \
2664 tp##_is_signaling_nan(xb.fld, &env->fp_status)) { \
2665 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2668 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2673 if (tp##_##cmp(xb.fld, xa.fld, &env->fp_status) == exp) { \
2683 putVSR(xT(opcode), &xt, env); \
2684 if ((opcode >> (31-21)) & 1) { \
2685 env->crf[6] = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0); \
2687 float_check_status(env); \
2690 VSX_CMP(xvcmpeqdp, 2, float64, VsrD(i), eq, 0, 1)
2691 VSX_CMP(xvcmpgedp, 2, float64, VsrD(i), le, 1, 1)
2692 VSX_CMP(xvcmpgtdp, 2, float64, VsrD(i), lt, 1, 1)
2693 VSX_CMP(xvcmpnedp, 2, float64, VsrD(i), eq, 0, 0)
2694 VSX_CMP(xvcmpeqsp, 4, float32, VsrW(i), eq, 0, 1)
2695 VSX_CMP(xvcmpgesp, 4, float32, VsrW(i), le, 1, 1)
2696 VSX_CMP(xvcmpgtsp, 4, float32, VsrW(i), lt, 1, 1)
2697 VSX_CMP(xvcmpnesp, 4, float32, VsrW(i), eq, 0, 0)
2699 /* VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2700 * op - instruction mnemonic
2701 * nels - number of elements (1, 2 or 4)
2702 * stp - source type (float32 or float64)
2703 * ttp - target type (float32 or float64)
2704 * sfld - source vsr_t field
2705 * tfld - target vsr_t field (f32 or f64)
2708 #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf) \
2709 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2714 getVSR(xB(opcode), &xb, env); \
2715 getVSR(xT(opcode), &xt, env); \
2717 for (i = 0; i < nels; i++) { \
2718 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2719 if (unlikely(stp##_is_signaling_nan(xb.sfld, \
2720 &env->fp_status))) { \
2721 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2722 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2725 helper_compute_fprf_##ttp(env, xt.tfld); \
2729 putVSR(xT(opcode), &xt, env); \
2730 float_check_status(env); \
2733 VSX_CVT_FP_TO_FP(xscvdpsp, 1, float64, float32, VsrD(0), VsrW(0), 1)
2734 VSX_CVT_FP_TO_FP(xscvspdp, 1, float32, float64, VsrW(0), VsrD(0), 1)
2735 VSX_CVT_FP_TO_FP(xvcvdpsp, 2, float64, float32, VsrD(i), VsrW(2*i), 0)
2736 VSX_CVT_FP_TO_FP(xvcvspdp, 2, float32, float64, VsrW(2*i), VsrD(i), 0)
2738 /* VSX_CVT_FP_TO_FP_VECTOR - VSX floating point/floating point conversion
2739 * op - instruction mnemonic
2740 * nels - number of elements (1, 2 or 4)
2741 * stp - source type (float32 or float64)
2742 * ttp - target type (float32 or float64)
2743 * sfld - source vsr_t field
2744 * tfld - target vsr_t field (f32 or f64)
2747 #define VSX_CVT_FP_TO_FP_VECTOR(op, nels, stp, ttp, sfld, tfld, sfprf) \
2748 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2753 getVSR(rB(opcode) + 32, &xb, env); \
2754 getVSR(rD(opcode) + 32, &xt, env); \
2756 for (i = 0; i < nels; i++) { \
2757 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2758 if (unlikely(stp##_is_signaling_nan(xb.sfld, \
2759 &env->fp_status))) { \
2760 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2761 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2764 helper_compute_fprf_##ttp(env, xt.tfld); \
2768 putVSR(rD(opcode) + 32, &xt, env); \
2769 float_check_status(env); \
2772 VSX_CVT_FP_TO_FP_VECTOR(xscvdpqp, 1, float64, float128, VsrD(0), f128, 1)
2774 /* VSX_CVT_FP_TO_FP_HP - VSX floating point/floating point conversion
2775 * involving one half precision value
2776 * op - instruction mnemonic
2777 * nels - number of elements (1, 2 or 4)
2780 * sfld - source vsr_t field
2781 * tfld - target vsr_t field
2784 #define VSX_CVT_FP_TO_FP_HP(op, nels, stp, ttp, sfld, tfld, sfprf) \
2785 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2790 getVSR(xB(opcode), &xb, env); \
2791 memset(&xt, 0, sizeof(xt)); \
2793 for (i = 0; i < nels; i++) { \
2794 xt.tfld = stp##_to_##ttp(xb.sfld, 1, &env->fp_status); \
2795 if (unlikely(stp##_is_signaling_nan(xb.sfld, \
2796 &env->fp_status))) { \
2797 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2798 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2801 helper_compute_fprf_##ttp(env, xt.tfld); \
2805 putVSR(xT(opcode), &xt, env); \
2806 float_check_status(env); \
2809 VSX_CVT_FP_TO_FP_HP(xscvdphp, 1, float64, float16, VsrD(0), VsrH(3), 1)
2810 VSX_CVT_FP_TO_FP_HP(xscvhpdp, 1, float16, float64, VsrH(3), VsrD(0), 1)
2811 VSX_CVT_FP_TO_FP_HP(xvcvsphp, 4, float32, float16, VsrW(i), VsrH(2 * i + 1), 0)
2812 VSX_CVT_FP_TO_FP_HP(xvcvhpsp, 4, float16, float32, VsrH(2 * i + 1), VsrW(i), 0)
2815 * xscvqpdp isn't using VSX_CVT_FP_TO_FP() because xscvqpdpo will be
2816 * added to this later.
2818 void helper_xscvqpdp(CPUPPCState *env, uint32_t opcode)
2823 getVSR(rB(opcode) + 32, &xb, env);
2824 memset(&xt, 0, sizeof(xt));
2826 tstat = env->fp_status;
2827 if (unlikely(Rc(opcode) != 0)) {
2828 tstat.float_rounding_mode = float_round_to_odd;
2831 xt.VsrD(0) = float128_to_float64(xb.f128, &tstat);
2832 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
2833 if (unlikely(float128_is_signaling_nan(xb.f128,
2835 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0);
2836 xt.VsrD(0) = float64_snan_to_qnan(xt.VsrD(0));
2838 helper_compute_fprf_float64(env, xt.VsrD(0));
2840 putVSR(rD(opcode) + 32, &xt, env);
2841 float_check_status(env);
2844 uint64_t helper_xscvdpspn(CPUPPCState *env, uint64_t xb)
2846 float_status tstat = env->fp_status;
2847 set_float_exception_flags(0, &tstat);
2849 return (uint64_t)float64_to_float32(xb, &tstat) << 32;
2852 uint64_t helper_xscvspdpn(CPUPPCState *env, uint64_t xb)
2854 float_status tstat = env->fp_status;
2855 set_float_exception_flags(0, &tstat);
2857 return float32_to_float64(xb >> 32, &tstat);
2860 /* VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2861 * op - instruction mnemonic
2862 * nels - number of elements (1, 2 or 4)
2863 * stp - source type (float32 or float64)
2864 * ttp - target type (int32, uint32, int64 or uint64)
2865 * sfld - source vsr_t field
2866 * tfld - target vsr_t field
2867 * rnan - resulting NaN
2869 #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, rnan) \
2870 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2875 getVSR(xB(opcode), &xb, env); \
2876 getVSR(xT(opcode), &xt, env); \
2878 for (i = 0; i < nels; i++) { \
2879 if (unlikely(stp##_is_any_nan(xb.sfld))) { \
2880 if (stp##_is_signaling_nan(xb.sfld, &env->fp_status)) { \
2881 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2883 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2886 xt.tfld = stp##_to_##ttp##_round_to_zero(xb.sfld, \
2888 if (env->fp_status.float_exception_flags & float_flag_invalid) { \
2889 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2894 putVSR(xT(opcode), &xt, env); \
2895 float_check_status(env); \
2898 VSX_CVT_FP_TO_INT(xscvdpsxds, 1, float64, int64, VsrD(0), VsrD(0), \
2899 0x8000000000000000ULL)
2900 VSX_CVT_FP_TO_INT(xscvdpsxws, 1, float64, int32, VsrD(0), VsrW(1), \
2902 VSX_CVT_FP_TO_INT(xscvdpuxds, 1, float64, uint64, VsrD(0), VsrD(0), 0ULL)
2903 VSX_CVT_FP_TO_INT(xscvdpuxws, 1, float64, uint32, VsrD(0), VsrW(1), 0U)
2904 VSX_CVT_FP_TO_INT(xvcvdpsxds, 2, float64, int64, VsrD(i), VsrD(i), \
2905 0x8000000000000000ULL)
2906 VSX_CVT_FP_TO_INT(xvcvdpsxws, 2, float64, int32, VsrD(i), VsrW(2*i), \
2908 VSX_CVT_FP_TO_INT(xvcvdpuxds, 2, float64, uint64, VsrD(i), VsrD(i), 0ULL)
2909 VSX_CVT_FP_TO_INT(xvcvdpuxws, 2, float64, uint32, VsrD(i), VsrW(2*i), 0U)
2910 VSX_CVT_FP_TO_INT(xvcvspsxds, 2, float32, int64, VsrW(2*i), VsrD(i), \
2911 0x8000000000000000ULL)
2912 VSX_CVT_FP_TO_INT(xvcvspsxws, 4, float32, int32, VsrW(i), VsrW(i), 0x80000000U)
2913 VSX_CVT_FP_TO_INT(xvcvspuxds, 2, float32, uint64, VsrW(2*i), VsrD(i), 0ULL)
2914 VSX_CVT_FP_TO_INT(xvcvspuxws, 4, float32, uint32, VsrW(i), VsrW(i), 0U)
2916 /* VSX_CVT_FP_TO_INT_VECTOR - VSX floating point to integer conversion
2917 * op - instruction mnemonic
2918 * stp - source type (float32 or float64)
2919 * ttp - target type (int32, uint32, int64 or uint64)
2920 * sfld - source vsr_t field
2921 * tfld - target vsr_t field
2922 * rnan - resulting NaN
2924 #define VSX_CVT_FP_TO_INT_VECTOR(op, stp, ttp, sfld, tfld, rnan) \
2925 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2929 getVSR(rB(opcode) + 32, &xb, env); \
2930 memset(&xt, 0, sizeof(xt)); \
2932 if (unlikely(stp##_is_any_nan(xb.sfld))) { \
2933 if (stp##_is_signaling_nan(xb.sfld, &env->fp_status)) { \
2934 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2936 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2939 xt.tfld = stp##_to_##ttp##_round_to_zero(xb.sfld, \
2941 if (env->fp_status.float_exception_flags & float_flag_invalid) { \
2942 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2946 putVSR(rD(opcode) + 32, &xt, env); \
2947 float_check_status(env); \
2950 VSX_CVT_FP_TO_INT_VECTOR(xscvqpsdz, float128, int64, f128, VsrD(0), \
2951 0x8000000000000000ULL)
2953 VSX_CVT_FP_TO_INT_VECTOR(xscvqpswz, float128, int32, f128, VsrD(0), \
2954 0xffffffff80000000ULL)
2955 VSX_CVT_FP_TO_INT_VECTOR(xscvqpudz, float128, uint64, f128, VsrD(0), 0x0ULL)
2956 VSX_CVT_FP_TO_INT_VECTOR(xscvqpuwz, float128, uint32, f128, VsrD(0), 0x0ULL)
2958 /* VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
2959 * op - instruction mnemonic
2960 * nels - number of elements (1, 2 or 4)
2961 * stp - source type (int32, uint32, int64 or uint64)
2962 * ttp - target type (float32 or float64)
2963 * sfld - source vsr_t field
2964 * tfld - target vsr_t field
2965 * jdef - definition of the j index (i or 2*i)
2968 #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf, r2sp) \
2969 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2974 getVSR(xB(opcode), &xb, env); \
2975 getVSR(xT(opcode), &xt, env); \
2977 for (i = 0; i < nels; i++) { \
2978 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2980 xt.tfld = helper_frsp(env, xt.tfld); \
2983 helper_compute_fprf_float64(env, xt.tfld); \
2987 putVSR(xT(opcode), &xt, env); \
2988 float_check_status(env); \
2991 VSX_CVT_INT_TO_FP(xscvsxddp, 1, int64, float64, VsrD(0), VsrD(0), 1, 0)
2992 VSX_CVT_INT_TO_FP(xscvuxddp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 0)
2993 VSX_CVT_INT_TO_FP(xscvsxdsp, 1, int64, float64, VsrD(0), VsrD(0), 1, 1)
2994 VSX_CVT_INT_TO_FP(xscvuxdsp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 1)
2995 VSX_CVT_INT_TO_FP(xvcvsxddp, 2, int64, float64, VsrD(i), VsrD(i), 0, 0)
2996 VSX_CVT_INT_TO_FP(xvcvuxddp, 2, uint64, float64, VsrD(i), VsrD(i), 0, 0)
2997 VSX_CVT_INT_TO_FP(xvcvsxwdp, 2, int32, float64, VsrW(2*i), VsrD(i), 0, 0)
2998 VSX_CVT_INT_TO_FP(xvcvuxwdp, 2, uint64, float64, VsrW(2*i), VsrD(i), 0, 0)
2999 VSX_CVT_INT_TO_FP(xvcvsxdsp, 2, int64, float32, VsrD(i), VsrW(2*i), 0, 0)
3000 VSX_CVT_INT_TO_FP(xvcvuxdsp, 2, uint64, float32, VsrD(i), VsrW(2*i), 0, 0)
3001 VSX_CVT_INT_TO_FP(xvcvsxwsp, 4, int32, float32, VsrW(i), VsrW(i), 0, 0)
3002 VSX_CVT_INT_TO_FP(xvcvuxwsp, 4, uint32, float32, VsrW(i), VsrW(i), 0, 0)
3004 /* VSX_CVT_INT_TO_FP_VECTOR - VSX integer to floating point conversion
3005 * op - instruction mnemonic
3006 * stp - source type (int32, uint32, int64 or uint64)
3007 * ttp - target type (float32 or float64)
3008 * sfld - source vsr_t field
3009 * tfld - target vsr_t field
3011 #define VSX_CVT_INT_TO_FP_VECTOR(op, stp, ttp, sfld, tfld) \
3012 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3016 getVSR(rB(opcode) + 32, &xb, env); \
3017 getVSR(rD(opcode) + 32, &xt, env); \
3019 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
3020 helper_compute_fprf_##ttp(env, xt.tfld); \
3022 putVSR(xT(opcode) + 32, &xt, env); \
3023 float_check_status(env); \
3026 VSX_CVT_INT_TO_FP_VECTOR(xscvsdqp, int64, float128, VsrD(0), f128)
3027 VSX_CVT_INT_TO_FP_VECTOR(xscvudqp, uint64, float128, VsrD(0), f128)
3029 /* For "use current rounding mode", define a value that will not be one of
3030 * the existing rounding model enums.
3032 #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
3033 float_round_up + float_round_to_zero)
3035 /* VSX_ROUND - VSX floating point round
3036 * op - instruction mnemonic
3037 * nels - number of elements (1, 2 or 4)
3038 * tp - type (float32 or float64)
3039 * fld - vsr_t field (VsrD(*) or VsrW(*))
3040 * rmode - rounding mode
3043 #define VSX_ROUND(op, nels, tp, fld, rmode, sfprf) \
3044 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3048 getVSR(xB(opcode), &xb, env); \
3049 getVSR(xT(opcode), &xt, env); \
3051 if (rmode != FLOAT_ROUND_CURRENT) { \
3052 set_float_rounding_mode(rmode, &env->fp_status); \
3055 for (i = 0; i < nels; i++) { \
3056 if (unlikely(tp##_is_signaling_nan(xb.fld, \
3057 &env->fp_status))) { \
3058 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
3059 xt.fld = tp##_snan_to_qnan(xb.fld); \
3061 xt.fld = tp##_round_to_int(xb.fld, &env->fp_status); \
3064 helper_compute_fprf_float64(env, xt.fld); \
3068 /* If this is not a "use current rounding mode" instruction, \
3069 * then inhibit setting of the XX bit and restore rounding \
3070 * mode from FPSCR */ \
3071 if (rmode != FLOAT_ROUND_CURRENT) { \
3072 fpscr_set_rounding_mode(env); \
3073 env->fp_status.float_exception_flags &= ~float_flag_inexact; \
3076 putVSR(xT(opcode), &xt, env); \
3077 float_check_status(env); \
3080 VSX_ROUND(xsrdpi, 1, float64, VsrD(0), float_round_ties_away, 1)
3081 VSX_ROUND(xsrdpic, 1, float64, VsrD(0), FLOAT_ROUND_CURRENT, 1)
3082 VSX_ROUND(xsrdpim, 1, float64, VsrD(0), float_round_down, 1)
3083 VSX_ROUND(xsrdpip, 1, float64, VsrD(0), float_round_up, 1)
3084 VSX_ROUND(xsrdpiz, 1, float64, VsrD(0), float_round_to_zero, 1)
3086 VSX_ROUND(xvrdpi, 2, float64, VsrD(i), float_round_ties_away, 0)
3087 VSX_ROUND(xvrdpic, 2, float64, VsrD(i), FLOAT_ROUND_CURRENT, 0)
3088 VSX_ROUND(xvrdpim, 2, float64, VsrD(i), float_round_down, 0)
3089 VSX_ROUND(xvrdpip, 2, float64, VsrD(i), float_round_up, 0)
3090 VSX_ROUND(xvrdpiz, 2, float64, VsrD(i), float_round_to_zero, 0)
3092 VSX_ROUND(xvrspi, 4, float32, VsrW(i), float_round_ties_away, 0)
3093 VSX_ROUND(xvrspic, 4, float32, VsrW(i), FLOAT_ROUND_CURRENT, 0)
3094 VSX_ROUND(xvrspim, 4, float32, VsrW(i), float_round_down, 0)
3095 VSX_ROUND(xvrspip, 4, float32, VsrW(i), float_round_up, 0)
3096 VSX_ROUND(xvrspiz, 4, float32, VsrW(i), float_round_to_zero, 0)
3098 uint64_t helper_xsrsp(CPUPPCState *env, uint64_t xb)
3100 helper_reset_fpstatus(env);
3102 uint64_t xt = helper_frsp(env, xb);
3104 helper_compute_fprf_float64(env, xt);
3105 float_check_status(env);
3109 #define VSX_XXPERM(op, indexed) \
3110 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3112 ppc_vsr_t xt, xa, pcv, xto; \
3115 getVSR(xA(opcode), &xa, env); \
3116 getVSR(xT(opcode), &xt, env); \
3117 getVSR(xB(opcode), &pcv, env); \
3119 for (i = 0; i < 16; i++) { \
3120 idx = pcv.VsrB(i) & 0x1F; \
3124 xto.VsrB(i) = (idx <= 15) ? xa.VsrB(idx) : xt.VsrB(idx - 16); \
3126 putVSR(xT(opcode), &xto, env); \
3129 VSX_XXPERM(xxperm, 0)
3130 VSX_XXPERM(xxpermr, 1)
3132 void helper_xvxsigsp(CPUPPCState *env, uint32_t opcode)
3135 uint32_t exp, i, fraction;
3137 getVSR(xB(opcode), &xb, env);
3138 memset(&xt, 0, sizeof(xt));
3140 for (i = 0; i < 4; i++) {
3141 exp = (xb.VsrW(i) >> 23) & 0xFF;
3142 fraction = xb.VsrW(i) & 0x7FFFFF;
3143 if (exp != 0 && exp != 255) {
3144 xt.VsrW(i) = fraction | 0x00800000;
3146 xt.VsrW(i) = fraction;
3149 putVSR(xT(opcode), &xt, env);
3152 /* VSX_TEST_DC - VSX floating point test data class
3153 * op - instruction mnemonic
3154 * nels - number of elements (1, 2 or 4)
3155 * xbn - VSR register number
3156 * tp - type (float32 or float64)
3157 * fld - vsr_t field (VsrD(*) or VsrW(*))
3158 * tfld - target vsr_t field (VsrD(*) or VsrW(*))
3159 * fld_max - target field max
3160 * scrf - set result in CR and FPCC
3162 #define VSX_TEST_DC(op, nels, xbn, tp, fld, tfld, fld_max, scrf) \
3163 void helper_##op(CPUPPCState *env, uint32_t opcode) \
3166 uint32_t i, sign, dcmx; \
3167 uint32_t cc, match = 0; \
3169 getVSR(xbn, &xb, env); \
3171 memset(&xt, 0, sizeof(xt)); \
3172 dcmx = DCMX_XV(opcode); \
3174 dcmx = DCMX(opcode); \
3177 for (i = 0; i < nels; i++) { \
3178 sign = tp##_is_neg(xb.fld); \
3179 if (tp##_is_any_nan(xb.fld)) { \
3180 match = extract32(dcmx, 6, 1); \
3181 } else if (tp##_is_infinity(xb.fld)) { \
3182 match = extract32(dcmx, 4 + !sign, 1); \
3183 } else if (tp##_is_zero(xb.fld)) { \
3184 match = extract32(dcmx, 2 + !sign, 1); \
3185 } else if (tp##_is_zero_or_denormal(xb.fld)) { \
3186 match = extract32(dcmx, 0 + !sign, 1); \
3190 cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT; \
3191 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
3192 env->fpscr |= cc << FPSCR_FPRF; \
3193 env->crf[BF(opcode)] = cc; \
3195 xt.tfld = match ? fld_max : 0; \
3200 putVSR(xT(opcode), &xt, env); \
3204 VSX_TEST_DC(xvtstdcdp, 2, xB(opcode), float64, VsrD(i), VsrD(i), UINT64_MAX, 0)
3205 VSX_TEST_DC(xvtstdcsp, 4, xB(opcode), float32, VsrW(i), VsrW(i), UINT32_MAX, 0)
3206 VSX_TEST_DC(xststdcdp, 1, xB(opcode), float64, VsrD(0), VsrD(0), 0, 1)
3207 VSX_TEST_DC(xststdcqp, 1, (rB(opcode) + 32), float128, f128, VsrD(0), 0, 1)
3209 void helper_xststdcsp(CPUPPCState *env, uint32_t opcode)
3212 uint32_t dcmx, sign, exp;
3213 uint32_t cc, match = 0, not_sp = 0;
3215 getVSR(xB(opcode), &xb, env);
3216 dcmx = DCMX(opcode);
3217 exp = (xb.VsrD(0) >> 52) & 0x7FF;
3219 sign = float64_is_neg(xb.VsrD(0));
3220 if (float64_is_any_nan(xb.VsrD(0))) {
3221 match = extract32(dcmx, 6, 1);
3222 } else if (float64_is_infinity(xb.VsrD(0))) {
3223 match = extract32(dcmx, 4 + !sign, 1);
3224 } else if (float64_is_zero(xb.VsrD(0))) {
3225 match = extract32(dcmx, 2 + !sign, 1);
3226 } else if (float64_is_zero_or_denormal(xb.VsrD(0)) ||
3227 (exp > 0 && exp < 0x381)) {
3228 match = extract32(dcmx, 0 + !sign, 1);
3231 not_sp = !float64_eq(xb.VsrD(0),
3233 float64_to_float32(xb.VsrD(0), &env->fp_status),
3234 &env->fp_status), &env->fp_status);
3236 cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT | not_sp << CRF_SO_BIT;
3237 env->fpscr &= ~(0x0F << FPSCR_FPRF);
3238 env->fpscr |= cc << FPSCR_FPRF;
3239 env->crf[BF(opcode)] = cc;
3242 void helper_xsrqpi(CPUPPCState *env, uint32_t opcode)
3246 uint8_t r = Rrm(opcode);
3247 uint8_t ex = Rc(opcode);
3248 uint8_t rmc = RMC(opcode);
3252 getVSR(rB(opcode) + 32, &xb, env);
3253 memset(&xt, 0, sizeof(xt));
3254 helper_reset_fpstatus(env);
3256 if (r == 0 && rmc == 0) {
3257 rmode = float_round_ties_away;
3258 } else if (r == 0 && rmc == 0x3) {
3260 } else if (r == 1) {
3263 rmode = float_round_nearest_even;
3266 rmode = float_round_to_zero;
3269 rmode = float_round_up;
3272 rmode = float_round_down;
3279 tstat = env->fp_status;
3280 set_float_exception_flags(0, &tstat);
3281 set_float_rounding_mode(rmode, &tstat);
3282 xt.f128 = float128_round_to_int(xb.f128, &tstat);
3283 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3285 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3286 if (float128_is_signaling_nan(xb.f128, &tstat)) {
3287 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0);
3288 xt.f128 = float128_snan_to_qnan(xt.f128);
3292 if (ex == 0 && (tstat.float_exception_flags & float_flag_inexact)) {
3293 env->fp_status.float_exception_flags &= ~float_flag_inexact;
3296 helper_compute_fprf_float128(env, xt.f128);
3297 float_check_status(env);
3298 putVSR(rD(opcode) + 32, &xt, env);
3301 void helper_xsrqpxp(CPUPPCState *env, uint32_t opcode)
3305 uint8_t r = Rrm(opcode);
3306 uint8_t rmc = RMC(opcode);
3311 getVSR(rB(opcode) + 32, &xb, env);
3312 memset(&xt, 0, sizeof(xt));
3313 helper_reset_fpstatus(env);
3315 if (r == 0 && rmc == 0) {
3316 rmode = float_round_ties_away;
3317 } else if (r == 0 && rmc == 0x3) {
3319 } else if (r == 1) {
3322 rmode = float_round_nearest_even;
3325 rmode = float_round_to_zero;
3328 rmode = float_round_up;
3331 rmode = float_round_down;
3338 tstat = env->fp_status;
3339 set_float_exception_flags(0, &tstat);
3340 set_float_rounding_mode(rmode, &tstat);
3341 round_res = float128_to_floatx80(xb.f128, &tstat);
3342 xt.f128 = floatx80_to_float128(round_res, &tstat);
3343 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3345 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3346 if (float128_is_signaling_nan(xb.f128, &tstat)) {
3347 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0);
3348 xt.f128 = float128_snan_to_qnan(xt.f128);
3352 helper_compute_fprf_float128(env, xt.f128);
3353 putVSR(rD(opcode) + 32, &xt, env);
3354 float_check_status(env);
3357 void helper_xssqrtqp(CPUPPCState *env, uint32_t opcode)
3363 getVSR(rB(opcode) + 32, &xb, env);
3364 memset(&xt, 0, sizeof(xt));
3365 helper_reset_fpstatus(env);
3367 tstat = env->fp_status;
3368 if (unlikely(Rc(opcode) != 0)) {
3369 tstat.float_rounding_mode = float_round_to_odd;
3372 set_float_exception_flags(0, &tstat);
3373 xt.f128 = float128_sqrt(xb.f128, &tstat);
3374 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3376 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3377 if (float128_is_signaling_nan(xb.f128, &tstat)) {
3378 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
3379 xt.f128 = float128_snan_to_qnan(xb.f128);
3380 } else if (float128_is_quiet_nan(xb.f128, &tstat)) {
3382 } else if (float128_is_neg(xb.f128) && !float128_is_zero(xb.f128)) {
3383 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, 1);
3384 set_snan_bit_is_one(0, &env->fp_status);
3385 xt.f128 = float128_default_nan(&env->fp_status);
3389 helper_compute_fprf_float128(env, xt.f128);
3390 putVSR(rD(opcode) + 32, &xt, env);
3391 float_check_status(env);
3394 void helper_xssubqp(CPUPPCState *env, uint32_t opcode)
3396 ppc_vsr_t xt, xa, xb;
3399 getVSR(rA(opcode) + 32, &xa, env);
3400 getVSR(rB(opcode) + 32, &xb, env);
3401 getVSR(rD(opcode) + 32, &xt, env);
3402 helper_reset_fpstatus(env);
3404 tstat = env->fp_status;
3405 if (unlikely(Rc(opcode) != 0)) {
3406 tstat.float_rounding_mode = float_round_to_odd;
3409 set_float_exception_flags(0, &tstat);
3410 xt.f128 = float128_sub(xa.f128, xb.f128, &tstat);
3411 env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3413 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3414 if (float128_is_infinity(xa.f128) && float128_is_infinity(xb.f128)) {
3415 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, 1);
3416 } else if (float128_is_signaling_nan(xa.f128, &tstat) ||
3417 float128_is_signaling_nan(xb.f128, &tstat)) {
3418 float_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1);
3422 helper_compute_fprf_float128(env, xt.f128);
3423 putVSR(rD(opcode) + 32, &xt, env);
3424 float_check_status(env);