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/>.
22 /*****************************************************************************/
23 /* Floating point operations helpers */
24 uint64_t helper_float32_to_float64(CPUPPCState
*env
, uint32_t arg
)
30 d
.d
= float32_to_float64(f
.f
, &env
->fp_status
);
34 uint32_t helper_float64_to_float32(CPUPPCState
*env
, uint64_t arg
)
40 f
.f
= float64_to_float32(d
.d
, &env
->fp_status
);
44 static inline int isden(float64 d
)
50 return ((u
.ll
>> 52) & 0x7FF) == 0;
53 static inline int ppc_float32_get_unbiased_exp(float32 f
)
55 return ((f
>> 23) & 0xFF) - 127;
58 static inline int ppc_float64_get_unbiased_exp(float64 f
)
60 return ((f
>> 52) & 0x7FF) - 1023;
63 uint32_t helper_compute_fprf(CPUPPCState
*env
, uint64_t arg
, uint32_t set_fprf
)
70 isneg
= float64_is_neg(farg
.d
);
71 if (unlikely(float64_is_any_nan(farg
.d
))) {
72 if (float64_is_signaling_nan(farg
.d
)) {
73 /* Signaling NaN: flags are undefined */
79 } else if (unlikely(float64_is_infinity(farg
.d
))) {
87 if (float64_is_zero(farg
.d
)) {
96 /* Denormalized numbers */
99 /* Normalized numbers */
110 /* We update FPSCR_FPRF */
111 env
->fpscr
&= ~(0x1F << FPSCR_FPRF
);
112 env
->fpscr
|= ret
<< FPSCR_FPRF
;
114 /* We just need fpcc to update Rc1 */
118 /* Floating-point invalid operations exception */
119 static inline uint64_t fload_invalid_op_excp(CPUPPCState
*env
, int op
,
127 case POWERPC_EXCP_FP_VXSNAN
:
128 env
->fpscr
|= 1 << FPSCR_VXSNAN
;
130 case POWERPC_EXCP_FP_VXSOFT
:
131 env
->fpscr
|= 1 << FPSCR_VXSOFT
;
133 case POWERPC_EXCP_FP_VXISI
:
134 /* Magnitude subtraction of infinities */
135 env
->fpscr
|= 1 << FPSCR_VXISI
;
137 case POWERPC_EXCP_FP_VXIDI
:
138 /* Division of infinity by infinity */
139 env
->fpscr
|= 1 << FPSCR_VXIDI
;
141 case POWERPC_EXCP_FP_VXZDZ
:
142 /* Division of zero by zero */
143 env
->fpscr
|= 1 << FPSCR_VXZDZ
;
145 case POWERPC_EXCP_FP_VXIMZ
:
146 /* Multiplication of zero by infinity */
147 env
->fpscr
|= 1 << FPSCR_VXIMZ
;
149 case POWERPC_EXCP_FP_VXVC
:
150 /* Ordered comparison of NaN */
151 env
->fpscr
|= 1 << FPSCR_VXVC
;
153 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
154 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
156 /* We must update the target FPR before raising the exception */
158 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
159 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_VXVC
;
160 /* Update the floating-point enabled exception summary */
161 env
->fpscr
|= 1 << FPSCR_FEX
;
162 /* Exception is differed */
166 case POWERPC_EXCP_FP_VXSQRT
:
167 /* Square root of a negative number */
168 env
->fpscr
|= 1 << FPSCR_VXSQRT
;
170 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
172 /* Set the result to quiet NaN */
173 ret
= 0x7FF8000000000000ULL
;
175 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
176 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
180 case POWERPC_EXCP_FP_VXCVI
:
181 /* Invalid conversion */
182 env
->fpscr
|= 1 << FPSCR_VXCVI
;
183 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
185 /* Set the result to quiet NaN */
186 ret
= 0x7FF8000000000000ULL
;
188 env
->fpscr
&= ~(0xF << FPSCR_FPCC
);
189 env
->fpscr
|= 0x11 << FPSCR_FPCC
;
194 /* Update the floating-point invalid operation summary */
195 env
->fpscr
|= 1 << FPSCR_VX
;
196 /* Update the floating-point exception summary */
197 env
->fpscr
|= 1 << FPSCR_FX
;
199 /* Update the floating-point enabled exception summary */
200 env
->fpscr
|= 1 << FPSCR_FEX
;
201 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
202 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
203 POWERPC_EXCP_FP
| op
);
209 static inline void float_zero_divide_excp(CPUPPCState
*env
)
211 env
->fpscr
|= 1 << FPSCR_ZX
;
212 env
->fpscr
&= ~((1 << FPSCR_FR
) | (1 << FPSCR_FI
));
213 /* Update the floating-point exception summary */
214 env
->fpscr
|= 1 << FPSCR_FX
;
216 /* Update the floating-point enabled exception summary */
217 env
->fpscr
|= 1 << FPSCR_FEX
;
218 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
219 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
220 POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
);
225 static inline void float_overflow_excp(CPUPPCState
*env
)
227 env
->fpscr
|= 1 << FPSCR_OX
;
228 /* Update the floating-point exception summary */
229 env
->fpscr
|= 1 << FPSCR_FX
;
231 /* XXX: should adjust the result */
232 /* Update the floating-point enabled exception summary */
233 env
->fpscr
|= 1 << FPSCR_FEX
;
234 /* We must update the target FPR before raising the exception */
235 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
236 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
238 env
->fpscr
|= 1 << FPSCR_XX
;
239 env
->fpscr
|= 1 << FPSCR_FI
;
243 static inline void float_underflow_excp(CPUPPCState
*env
)
245 env
->fpscr
|= 1 << FPSCR_UX
;
246 /* Update the floating-point exception summary */
247 env
->fpscr
|= 1 << FPSCR_FX
;
249 /* XXX: should adjust the result */
250 /* Update the floating-point enabled exception summary */
251 env
->fpscr
|= 1 << FPSCR_FEX
;
252 /* We must update the target FPR before raising the exception */
253 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
254 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
258 static inline void float_inexact_excp(CPUPPCState
*env
)
260 env
->fpscr
|= 1 << FPSCR_XX
;
261 /* Update the floating-point exception summary */
262 env
->fpscr
|= 1 << FPSCR_FX
;
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 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
268 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
272 static inline void fpscr_set_rounding_mode(CPUPPCState
*env
)
276 /* Set rounding mode */
279 /* Best approximation (round to nearest) */
280 rnd_type
= float_round_nearest_even
;
283 /* Smaller magnitude (round toward zero) */
284 rnd_type
= float_round_to_zero
;
287 /* Round toward +infinite */
288 rnd_type
= float_round_up
;
292 /* Round toward -infinite */
293 rnd_type
= float_round_down
;
296 set_float_rounding_mode(rnd_type
, &env
->fp_status
);
299 void helper_fpscr_clrbit(CPUPPCState
*env
, uint32_t bit
)
303 prev
= (env
->fpscr
>> bit
) & 1;
304 env
->fpscr
&= ~(1 << bit
);
309 fpscr_set_rounding_mode(env
);
317 void helper_fpscr_setbit(CPUPPCState
*env
, uint32_t bit
)
321 prev
= (env
->fpscr
>> bit
) & 1;
322 env
->fpscr
|= 1 << bit
;
326 env
->fpscr
|= 1 << FPSCR_FX
;
332 env
->fpscr
|= 1 << FPSCR_FX
;
338 env
->fpscr
|= 1 << FPSCR_FX
;
344 env
->fpscr
|= 1 << FPSCR_FX
;
350 env
->fpscr
|= 1 << FPSCR_FX
;
364 env
->fpscr
|= 1 << FPSCR_VX
;
365 env
->fpscr
|= 1 << FPSCR_FX
;
373 env
->error_code
= POWERPC_EXCP_FP
;
375 env
->error_code
|= POWERPC_EXCP_FP_VXSNAN
;
378 env
->error_code
|= POWERPC_EXCP_FP_VXISI
;
381 env
->error_code
|= POWERPC_EXCP_FP_VXIDI
;
384 env
->error_code
|= POWERPC_EXCP_FP_VXZDZ
;
387 env
->error_code
|= POWERPC_EXCP_FP_VXIMZ
;
390 env
->error_code
|= POWERPC_EXCP_FP_VXVC
;
393 env
->error_code
|= POWERPC_EXCP_FP_VXSOFT
;
396 env
->error_code
|= POWERPC_EXCP_FP_VXSQRT
;
399 env
->error_code
|= POWERPC_EXCP_FP_VXCVI
;
407 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_OX
;
414 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_UX
;
421 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_ZX
;
428 env
->error_code
= POWERPC_EXCP_FP
| POWERPC_EXCP_FP_XX
;
434 fpscr_set_rounding_mode(env
);
439 /* Update the floating-point enabled exception summary */
440 env
->fpscr
|= 1 << FPSCR_FEX
;
441 /* We have to update Rc1 before raising the exception */
442 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
448 void helper_store_fpscr(CPUPPCState
*env
, uint64_t arg
, uint32_t mask
)
450 target_ulong prev
, new;
454 new = (target_ulong
)arg
;
455 new &= ~0x60000000LL
;
456 new |= prev
& 0x60000000LL
;
457 for (i
= 0; i
< sizeof(target_ulong
) * 2; i
++) {
458 if (mask
& (1 << i
)) {
459 env
->fpscr
&= ~(0xFLL
<< (4 * i
));
460 env
->fpscr
|= new & (0xFLL
<< (4 * i
));
463 /* Update VX and FEX */
465 env
->fpscr
|= 1 << FPSCR_VX
;
467 env
->fpscr
&= ~(1 << FPSCR_VX
);
469 if ((fpscr_ex
& fpscr_eex
) != 0) {
470 env
->fpscr
|= 1 << FPSCR_FEX
;
471 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
472 /* XXX: we should compute it properly */
473 env
->error_code
= POWERPC_EXCP_FP
;
475 env
->fpscr
&= ~(1 << FPSCR_FEX
);
477 fpscr_set_rounding_mode(env
);
480 void store_fpscr(CPUPPCState
*env
, uint64_t arg
, uint32_t mask
)
482 helper_store_fpscr(env
, arg
, mask
);
485 void helper_float_check_status(CPUPPCState
*env
)
487 int status
= get_float_exception_flags(&env
->fp_status
);
489 if (status
& float_flag_divbyzero
) {
490 float_zero_divide_excp(env
);
491 } else if (status
& float_flag_overflow
) {
492 float_overflow_excp(env
);
493 } else if (status
& float_flag_underflow
) {
494 float_underflow_excp(env
);
495 } else if (status
& float_flag_inexact
) {
496 float_inexact_excp(env
);
499 if (env
->exception_index
== POWERPC_EXCP_PROGRAM
&&
500 (env
->error_code
& POWERPC_EXCP_FP
)) {
501 /* Differred floating-point exception after target FPR update */
502 if (msr_fe0
!= 0 || msr_fe1
!= 0) {
503 helper_raise_exception_err(env
, env
->exception_index
,
509 void helper_reset_fpstatus(CPUPPCState
*env
)
511 set_float_exception_flags(0, &env
->fp_status
);
515 uint64_t helper_fadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
517 CPU_DoubleU farg1
, farg2
;
522 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
523 float64_is_neg(farg1
.d
) != float64_is_neg(farg2
.d
))) {
524 /* Magnitude subtraction of infinities */
525 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
527 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
528 float64_is_signaling_nan(farg2
.d
))) {
530 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
532 farg1
.d
= float64_add(farg1
.d
, farg2
.d
, &env
->fp_status
);
539 uint64_t helper_fsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
541 CPU_DoubleU farg1
, farg2
;
546 if (unlikely(float64_is_infinity(farg1
.d
) && float64_is_infinity(farg2
.d
) &&
547 float64_is_neg(farg1
.d
) == float64_is_neg(farg2
.d
))) {
548 /* Magnitude subtraction of infinities */
549 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
551 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
552 float64_is_signaling_nan(farg2
.d
))) {
553 /* sNaN subtraction */
554 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
556 farg1
.d
= float64_sub(farg1
.d
, farg2
.d
, &env
->fp_status
);
563 uint64_t helper_fmul(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
565 CPU_DoubleU farg1
, farg2
;
570 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
571 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
572 /* Multiplication of zero by infinity */
573 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
575 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
576 float64_is_signaling_nan(farg2
.d
))) {
577 /* sNaN multiplication */
578 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
580 farg1
.d
= float64_mul(farg1
.d
, farg2
.d
, &env
->fp_status
);
587 uint64_t helper_fdiv(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
589 CPU_DoubleU farg1
, farg2
;
594 if (unlikely(float64_is_infinity(farg1
.d
) &&
595 float64_is_infinity(farg2
.d
))) {
596 /* Division of infinity by infinity */
597 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIDI
, 1);
598 } else if (unlikely(float64_is_zero(farg1
.d
) && float64_is_zero(farg2
.d
))) {
599 /* Division of zero by zero */
600 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXZDZ
, 1);
602 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
603 float64_is_signaling_nan(farg2
.d
))) {
605 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
607 farg1
.d
= float64_div(farg1
.d
, farg2
.d
, &env
->fp_status
);
614 #define FPU_FCTI(op, cvt, nanval) \
615 uint64_t helper_##op(CPUPPCState *env, uint64_t arg) \
620 farg.ll = float64_to_##cvt(farg.d, &env->fp_status); \
622 if (unlikely(env->fp_status.float_exception_flags)) { \
623 if (float64_is_any_nan(arg)) { \
624 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 1); \
625 if (float64_is_signaling_nan(arg)) { \
626 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 1); \
629 } else if (env->fp_status.float_exception_flags & \
630 float_flag_invalid) { \
631 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 1); \
633 helper_float_check_status(env); \
638 FPU_FCTI(fctiw
, int32
, 0x80000000U
)
639 FPU_FCTI(fctiwz
, int32_round_to_zero
, 0x80000000U
)
640 FPU_FCTI(fctiwu
, uint32
, 0x00000000U
)
641 FPU_FCTI(fctiwuz
, uint32_round_to_zero
, 0x00000000U
)
642 #if defined(TARGET_PPC64)
643 FPU_FCTI(fctid
, int64
, 0x8000000000000000ULL
)
644 FPU_FCTI(fctidz
, int64_round_to_zero
, 0x8000000000000000ULL
)
645 FPU_FCTI(fctidu
, uint64
, 0x0000000000000000ULL
)
646 FPU_FCTI(fctiduz
, uint64_round_to_zero
, 0x0000000000000000ULL
)
649 #if defined(TARGET_PPC64)
651 #define FPU_FCFI(op, cvtr, is_single) \
652 uint64_t helper_##op(CPUPPCState *env, uint64_t arg) \
657 float32 tmp = cvtr(arg, &env->fp_status); \
658 farg.d = float32_to_float64(tmp, &env->fp_status); \
660 farg.d = cvtr(arg, &env->fp_status); \
662 helper_float_check_status(env); \
666 FPU_FCFI(fcfid
, int64_to_float64
, 0)
667 FPU_FCFI(fcfids
, int64_to_float32
, 1)
668 FPU_FCFI(fcfidu
, uint64_to_float64
, 0)
669 FPU_FCFI(fcfidus
, uint64_to_float32
, 1)
673 static inline uint64_t do_fri(CPUPPCState
*env
, uint64_t arg
,
680 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
682 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
683 farg
.ll
= arg
| 0x0008000000000000ULL
;
685 int inexact
= get_float_exception_flags(&env
->fp_status
) &
687 set_float_rounding_mode(rounding_mode
, &env
->fp_status
);
688 farg
.ll
= float64_round_to_int(farg
.d
, &env
->fp_status
);
689 /* Restore rounding mode from FPSCR */
690 fpscr_set_rounding_mode(env
);
692 /* fri* does not set FPSCR[XX] */
694 env
->fp_status
.float_exception_flags
&= ~float_flag_inexact
;
697 helper_float_check_status(env
);
701 uint64_t helper_frin(CPUPPCState
*env
, uint64_t arg
)
703 return do_fri(env
, arg
, float_round_ties_away
);
706 uint64_t helper_friz(CPUPPCState
*env
, uint64_t arg
)
708 return do_fri(env
, arg
, float_round_to_zero
);
711 uint64_t helper_frip(CPUPPCState
*env
, uint64_t arg
)
713 return do_fri(env
, arg
, float_round_up
);
716 uint64_t helper_frim(CPUPPCState
*env
, uint64_t arg
)
718 return do_fri(env
, arg
, float_round_down
);
722 uint64_t helper_fmadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
725 CPU_DoubleU farg1
, farg2
, farg3
;
731 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
732 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
733 /* Multiplication of zero by infinity */
734 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
736 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
737 float64_is_signaling_nan(farg2
.d
) ||
738 float64_is_signaling_nan(farg3
.d
))) {
740 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
742 /* This is the way the PowerPC specification defines it */
743 float128 ft0_128
, ft1_128
;
745 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
746 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
747 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
748 if (unlikely(float128_is_infinity(ft0_128
) &&
749 float64_is_infinity(farg3
.d
) &&
750 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
751 /* Magnitude subtraction of infinities */
752 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
754 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
755 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
756 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
764 uint64_t helper_fmsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
767 CPU_DoubleU farg1
, farg2
, farg3
;
773 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
774 (float64_is_zero(farg1
.d
) &&
775 float64_is_infinity(farg2
.d
)))) {
776 /* Multiplication of zero by infinity */
777 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
779 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
780 float64_is_signaling_nan(farg2
.d
) ||
781 float64_is_signaling_nan(farg3
.d
))) {
783 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
785 /* This is the way the PowerPC specification defines it */
786 float128 ft0_128
, ft1_128
;
788 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
789 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
790 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
791 if (unlikely(float128_is_infinity(ft0_128
) &&
792 float64_is_infinity(farg3
.d
) &&
793 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
794 /* Magnitude subtraction of infinities */
795 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
797 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
798 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
799 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
805 /* fnmadd - fnmadd. */
806 uint64_t helper_fnmadd(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
809 CPU_DoubleU farg1
, farg2
, farg3
;
815 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
816 (float64_is_zero(farg1
.d
) && float64_is_infinity(farg2
.d
)))) {
817 /* Multiplication of zero by infinity */
818 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
820 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
821 float64_is_signaling_nan(farg2
.d
) ||
822 float64_is_signaling_nan(farg3
.d
))) {
824 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
826 /* This is the way the PowerPC specification defines it */
827 float128 ft0_128
, ft1_128
;
829 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
830 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
831 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
832 if (unlikely(float128_is_infinity(ft0_128
) &&
833 float64_is_infinity(farg3
.d
) &&
834 float128_is_neg(ft0_128
) != float64_is_neg(farg3
.d
))) {
835 /* Magnitude subtraction of infinities */
836 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
838 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
839 ft0_128
= float128_add(ft0_128
, ft1_128
, &env
->fp_status
);
840 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
842 if (likely(!float64_is_any_nan(farg1
.d
))) {
843 farg1
.d
= float64_chs(farg1
.d
);
849 /* fnmsub - fnmsub. */
850 uint64_t helper_fnmsub(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
853 CPU_DoubleU farg1
, farg2
, farg3
;
859 if (unlikely((float64_is_infinity(farg1
.d
) && float64_is_zero(farg2
.d
)) ||
860 (float64_is_zero(farg1
.d
) &&
861 float64_is_infinity(farg2
.d
)))) {
862 /* Multiplication of zero by infinity */
863 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXIMZ
, 1);
865 if (unlikely(float64_is_signaling_nan(farg1
.d
) ||
866 float64_is_signaling_nan(farg2
.d
) ||
867 float64_is_signaling_nan(farg3
.d
))) {
869 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
871 /* This is the way the PowerPC specification defines it */
872 float128 ft0_128
, ft1_128
;
874 ft0_128
= float64_to_float128(farg1
.d
, &env
->fp_status
);
875 ft1_128
= float64_to_float128(farg2
.d
, &env
->fp_status
);
876 ft0_128
= float128_mul(ft0_128
, ft1_128
, &env
->fp_status
);
877 if (unlikely(float128_is_infinity(ft0_128
) &&
878 float64_is_infinity(farg3
.d
) &&
879 float128_is_neg(ft0_128
) == float64_is_neg(farg3
.d
))) {
880 /* Magnitude subtraction of infinities */
881 farg1
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXISI
, 1);
883 ft1_128
= float64_to_float128(farg3
.d
, &env
->fp_status
);
884 ft0_128
= float128_sub(ft0_128
, ft1_128
, &env
->fp_status
);
885 farg1
.d
= float128_to_float64(ft0_128
, &env
->fp_status
);
887 if (likely(!float64_is_any_nan(farg1
.d
))) {
888 farg1
.d
= float64_chs(farg1
.d
);
895 uint64_t helper_frsp(CPUPPCState
*env
, uint64_t arg
)
902 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
903 /* sNaN square root */
904 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
906 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
907 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
913 uint64_t helper_fsqrt(CPUPPCState
*env
, uint64_t arg
)
919 if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
920 /* Square root of a negative nonzero number */
921 farg
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSQRT
, 1);
923 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
924 /* sNaN square root */
925 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
927 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
933 uint64_t helper_fre(CPUPPCState
*env
, uint64_t arg
)
939 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
940 /* sNaN reciprocal */
941 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
943 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
948 uint64_t helper_fres(CPUPPCState
*env
, uint64_t arg
)
955 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
956 /* sNaN reciprocal */
957 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
959 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
960 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
961 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
966 /* frsqrte - frsqrte. */
967 uint64_t helper_frsqrte(CPUPPCState
*env
, uint64_t arg
)
974 if (unlikely(float64_is_neg(farg
.d
) && !float64_is_zero(farg
.d
))) {
975 /* Reciprocal square root of a negative nonzero number */
976 farg
.ll
= fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSQRT
, 1);
978 if (unlikely(float64_is_signaling_nan(farg
.d
))) {
979 /* sNaN reciprocal square root */
980 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
982 farg
.d
= float64_sqrt(farg
.d
, &env
->fp_status
);
983 farg
.d
= float64_div(float64_one
, farg
.d
, &env
->fp_status
);
984 f32
= float64_to_float32(farg
.d
, &env
->fp_status
);
985 farg
.d
= float32_to_float64(f32
, &env
->fp_status
);
991 uint64_t helper_fsel(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
998 if ((!float64_is_neg(farg1
.d
) || float64_is_zero(farg1
.d
)) &&
999 !float64_is_any_nan(farg1
.d
)) {
1006 uint32_t helper_ftdiv(uint64_t fra
, uint64_t frb
)
1011 if (unlikely(float64_is_infinity(fra
) ||
1012 float64_is_infinity(frb
) ||
1013 float64_is_zero(frb
))) {
1017 int e_a
= ppc_float64_get_unbiased_exp(fra
);
1018 int e_b
= ppc_float64_get_unbiased_exp(frb
);
1020 if (unlikely(float64_is_any_nan(fra
) ||
1021 float64_is_any_nan(frb
))) {
1023 } else if ((e_b
<= -1022) || (e_b
>= 1021)) {
1025 } else if (!float64_is_zero(fra
) &&
1026 (((e_a
- e_b
) >= 1023) ||
1027 ((e_a
- e_b
) <= -1021) ||
1032 if (unlikely(float64_is_zero_or_denormal(frb
))) {
1033 /* XB is not zero because of the above check and */
1034 /* so must be denormalized. */
1039 return 0x8 | (fg_flag
? 4 : 0) | (fe_flag
? 2 : 0);
1042 uint32_t helper_ftsqrt(uint64_t frb
)
1047 if (unlikely(float64_is_infinity(frb
) || float64_is_zero(frb
))) {
1051 int e_b
= ppc_float64_get_unbiased_exp(frb
);
1053 if (unlikely(float64_is_any_nan(frb
))) {
1055 } else if (unlikely(float64_is_zero(frb
))) {
1057 } else if (unlikely(float64_is_neg(frb
))) {
1059 } else if (!float64_is_zero(frb
) && (e_b
<= (-1022+52))) {
1063 if (unlikely(float64_is_zero_or_denormal(frb
))) {
1064 /* XB is not zero because of the above check and */
1065 /* therefore must be denormalized. */
1070 return 0x8 | (fg_flag
? 4 : 0) | (fe_flag
? 2 : 0);
1073 void helper_fcmpu(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1076 CPU_DoubleU farg1
, farg2
;
1082 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1083 float64_is_any_nan(farg2
.d
))) {
1085 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1087 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1093 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1094 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1095 env
->crf
[crfD
] = ret
;
1096 if (unlikely(ret
== 0x01UL
1097 && (float64_is_signaling_nan(farg1
.d
) ||
1098 float64_is_signaling_nan(farg2
.d
)))) {
1099 /* sNaN comparison */
1100 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
, 1);
1104 void helper_fcmpo(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
,
1107 CPU_DoubleU farg1
, farg2
;
1113 if (unlikely(float64_is_any_nan(farg1
.d
) ||
1114 float64_is_any_nan(farg2
.d
))) {
1116 } else if (float64_lt(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1118 } else if (!float64_le(farg1
.d
, farg2
.d
, &env
->fp_status
)) {
1124 env
->fpscr
&= ~(0x0F << FPSCR_FPRF
);
1125 env
->fpscr
|= ret
<< FPSCR_FPRF
;
1126 env
->crf
[crfD
] = ret
;
1127 if (unlikely(ret
== 0x01UL
)) {
1128 if (float64_is_signaling_nan(farg1
.d
) ||
1129 float64_is_signaling_nan(farg2
.d
)) {
1130 /* sNaN comparison */
1131 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXSNAN
|
1132 POWERPC_EXCP_FP_VXVC
, 1);
1134 /* qNaN comparison */
1135 fload_invalid_op_excp(env
, POWERPC_EXCP_FP_VXVC
, 1);
1140 /* Single-precision floating-point conversions */
1141 static inline uint32_t efscfsi(CPUPPCState
*env
, uint32_t val
)
1145 u
.f
= int32_to_float32(val
, &env
->vec_status
);
1150 static inline uint32_t efscfui(CPUPPCState
*env
, uint32_t val
)
1154 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
1159 static inline int32_t efsctsi(CPUPPCState
*env
, uint32_t val
)
1164 /* NaN are not treated the same way IEEE 754 does */
1165 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1169 return float32_to_int32(u
.f
, &env
->vec_status
);
1172 static inline uint32_t efsctui(CPUPPCState
*env
, uint32_t val
)
1177 /* NaN are not treated the same way IEEE 754 does */
1178 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1182 return float32_to_uint32(u
.f
, &env
->vec_status
);
1185 static inline uint32_t efsctsiz(CPUPPCState
*env
, uint32_t val
)
1190 /* NaN are not treated the same way IEEE 754 does */
1191 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1195 return float32_to_int32_round_to_zero(u
.f
, &env
->vec_status
);
1198 static inline uint32_t efsctuiz(CPUPPCState
*env
, uint32_t val
)
1203 /* NaN are not treated the same way IEEE 754 does */
1204 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1208 return float32_to_uint32_round_to_zero(u
.f
, &env
->vec_status
);
1211 static inline uint32_t efscfsf(CPUPPCState
*env
, uint32_t val
)
1216 u
.f
= int32_to_float32(val
, &env
->vec_status
);
1217 tmp
= int64_to_float32(1ULL << 32, &env
->vec_status
);
1218 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
1223 static inline uint32_t efscfuf(CPUPPCState
*env
, uint32_t val
)
1228 u
.f
= uint32_to_float32(val
, &env
->vec_status
);
1229 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1230 u
.f
= float32_div(u
.f
, tmp
, &env
->vec_status
);
1235 static inline uint32_t efsctsf(CPUPPCState
*env
, uint32_t val
)
1241 /* NaN are not treated the same way IEEE 754 does */
1242 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1245 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1246 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
1248 return float32_to_int32(u
.f
, &env
->vec_status
);
1251 static inline uint32_t efsctuf(CPUPPCState
*env
, uint32_t val
)
1257 /* NaN are not treated the same way IEEE 754 does */
1258 if (unlikely(float32_is_quiet_nan(u
.f
))) {
1261 tmp
= uint64_to_float32(1ULL << 32, &env
->vec_status
);
1262 u
.f
= float32_mul(u
.f
, tmp
, &env
->vec_status
);
1264 return float32_to_uint32(u
.f
, &env
->vec_status
);
1267 #define HELPER_SPE_SINGLE_CONV(name) \
1268 uint32_t helper_e##name(CPUPPCState *env, uint32_t val) \
1270 return e##name(env, val); \
1273 HELPER_SPE_SINGLE_CONV(fscfsi
);
1275 HELPER_SPE_SINGLE_CONV(fscfui
);
1277 HELPER_SPE_SINGLE_CONV(fscfuf
);
1279 HELPER_SPE_SINGLE_CONV(fscfsf
);
1281 HELPER_SPE_SINGLE_CONV(fsctsi
);
1283 HELPER_SPE_SINGLE_CONV(fsctui
);
1285 HELPER_SPE_SINGLE_CONV(fsctsiz
);
1287 HELPER_SPE_SINGLE_CONV(fsctuiz
);
1289 HELPER_SPE_SINGLE_CONV(fsctsf
);
1291 HELPER_SPE_SINGLE_CONV(fsctuf
);
1293 #define HELPER_SPE_VECTOR_CONV(name) \
1294 uint64_t helper_ev##name(CPUPPCState *env, uint64_t val) \
1296 return ((uint64_t)e##name(env, val >> 32) << 32) | \
1297 (uint64_t)e##name(env, val); \
1300 HELPER_SPE_VECTOR_CONV(fscfsi
);
1302 HELPER_SPE_VECTOR_CONV(fscfui
);
1304 HELPER_SPE_VECTOR_CONV(fscfuf
);
1306 HELPER_SPE_VECTOR_CONV(fscfsf
);
1308 HELPER_SPE_VECTOR_CONV(fsctsi
);
1310 HELPER_SPE_VECTOR_CONV(fsctui
);
1312 HELPER_SPE_VECTOR_CONV(fsctsiz
);
1314 HELPER_SPE_VECTOR_CONV(fsctuiz
);
1316 HELPER_SPE_VECTOR_CONV(fsctsf
);
1318 HELPER_SPE_VECTOR_CONV(fsctuf
);
1320 /* Single-precision floating-point arithmetic */
1321 static inline uint32_t efsadd(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1327 u1
.f
= float32_add(u1
.f
, u2
.f
, &env
->vec_status
);
1331 static inline uint32_t efssub(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1337 u1
.f
= float32_sub(u1
.f
, u2
.f
, &env
->vec_status
);
1341 static inline uint32_t efsmul(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1347 u1
.f
= float32_mul(u1
.f
, u2
.f
, &env
->vec_status
);
1351 static inline uint32_t efsdiv(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1357 u1
.f
= float32_div(u1
.f
, u2
.f
, &env
->vec_status
);
1361 #define HELPER_SPE_SINGLE_ARITH(name) \
1362 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1364 return e##name(env, op1, op2); \
1367 HELPER_SPE_SINGLE_ARITH(fsadd
);
1369 HELPER_SPE_SINGLE_ARITH(fssub
);
1371 HELPER_SPE_SINGLE_ARITH(fsmul
);
1373 HELPER_SPE_SINGLE_ARITH(fsdiv
);
1375 #define HELPER_SPE_VECTOR_ARITH(name) \
1376 uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1378 return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) | \
1379 (uint64_t)e##name(env, op1, op2); \
1382 HELPER_SPE_VECTOR_ARITH(fsadd
);
1384 HELPER_SPE_VECTOR_ARITH(fssub
);
1386 HELPER_SPE_VECTOR_ARITH(fsmul
);
1388 HELPER_SPE_VECTOR_ARITH(fsdiv
);
1390 /* Single-precision floating-point comparisons */
1391 static inline uint32_t efscmplt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1397 return float32_lt(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
1400 static inline uint32_t efscmpgt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1406 return float32_le(u1
.f
, u2
.f
, &env
->vec_status
) ? 0 : 4;
1409 static inline uint32_t efscmpeq(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1415 return float32_eq(u1
.f
, u2
.f
, &env
->vec_status
) ? 4 : 0;
1418 static inline uint32_t efststlt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1420 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1421 return efscmplt(env
, op1
, op2
);
1424 static inline uint32_t efststgt(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1426 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1427 return efscmpgt(env
, op1
, op2
);
1430 static inline uint32_t efststeq(CPUPPCState
*env
, uint32_t op1
, uint32_t op2
)
1432 /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1433 return efscmpeq(env
, op1
, op2
);
1436 #define HELPER_SINGLE_SPE_CMP(name) \
1437 uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1439 return e##name(env, op1, op2) << 2; \
1442 HELPER_SINGLE_SPE_CMP(fststlt
);
1444 HELPER_SINGLE_SPE_CMP(fststgt
);
1446 HELPER_SINGLE_SPE_CMP(fststeq
);
1448 HELPER_SINGLE_SPE_CMP(fscmplt
);
1450 HELPER_SINGLE_SPE_CMP(fscmpgt
);
1452 HELPER_SINGLE_SPE_CMP(fscmpeq
);
1454 static inline uint32_t evcmp_merge(int t0
, int t1
)
1456 return (t0
<< 3) | (t1
<< 2) | ((t0
| t1
) << 1) | (t0
& t1
);
1459 #define HELPER_VECTOR_SPE_CMP(name) \
1460 uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1462 return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32), \
1463 e##name(env, op1, op2)); \
1466 HELPER_VECTOR_SPE_CMP(fststlt
);
1468 HELPER_VECTOR_SPE_CMP(fststgt
);
1470 HELPER_VECTOR_SPE_CMP(fststeq
);
1472 HELPER_VECTOR_SPE_CMP(fscmplt
);
1474 HELPER_VECTOR_SPE_CMP(fscmpgt
);
1476 HELPER_VECTOR_SPE_CMP(fscmpeq
);
1478 /* Double-precision floating-point conversion */
1479 uint64_t helper_efdcfsi(CPUPPCState
*env
, uint32_t val
)
1483 u
.d
= int32_to_float64(val
, &env
->vec_status
);
1488 uint64_t helper_efdcfsid(CPUPPCState
*env
, uint64_t val
)
1492 u
.d
= int64_to_float64(val
, &env
->vec_status
);
1497 uint64_t helper_efdcfui(CPUPPCState
*env
, uint32_t val
)
1501 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
1506 uint64_t helper_efdcfuid(CPUPPCState
*env
, uint64_t val
)
1510 u
.d
= uint64_to_float64(val
, &env
->vec_status
);
1515 uint32_t helper_efdctsi(CPUPPCState
*env
, uint64_t val
)
1520 /* NaN are not treated the same way IEEE 754 does */
1521 if (unlikely(float64_is_any_nan(u
.d
))) {
1525 return float64_to_int32(u
.d
, &env
->vec_status
);
1528 uint32_t helper_efdctui(CPUPPCState
*env
, uint64_t val
)
1533 /* NaN are not treated the same way IEEE 754 does */
1534 if (unlikely(float64_is_any_nan(u
.d
))) {
1538 return float64_to_uint32(u
.d
, &env
->vec_status
);
1541 uint32_t helper_efdctsiz(CPUPPCState
*env
, uint64_t val
)
1546 /* NaN are not treated the same way IEEE 754 does */
1547 if (unlikely(float64_is_any_nan(u
.d
))) {
1551 return float64_to_int32_round_to_zero(u
.d
, &env
->vec_status
);
1554 uint64_t helper_efdctsidz(CPUPPCState
*env
, uint64_t val
)
1559 /* NaN are not treated the same way IEEE 754 does */
1560 if (unlikely(float64_is_any_nan(u
.d
))) {
1564 return float64_to_int64_round_to_zero(u
.d
, &env
->vec_status
);
1567 uint32_t helper_efdctuiz(CPUPPCState
*env
, uint64_t val
)
1572 /* NaN are not treated the same way IEEE 754 does */
1573 if (unlikely(float64_is_any_nan(u
.d
))) {
1577 return float64_to_uint32_round_to_zero(u
.d
, &env
->vec_status
);
1580 uint64_t helper_efdctuidz(CPUPPCState
*env
, uint64_t val
)
1585 /* NaN are not treated the same way IEEE 754 does */
1586 if (unlikely(float64_is_any_nan(u
.d
))) {
1590 return float64_to_uint64_round_to_zero(u
.d
, &env
->vec_status
);
1593 uint64_t helper_efdcfsf(CPUPPCState
*env
, uint32_t val
)
1598 u
.d
= int32_to_float64(val
, &env
->vec_status
);
1599 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
1600 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
1605 uint64_t helper_efdcfuf(CPUPPCState
*env
, uint32_t val
)
1610 u
.d
= uint32_to_float64(val
, &env
->vec_status
);
1611 tmp
= int64_to_float64(1ULL << 32, &env
->vec_status
);
1612 u
.d
= float64_div(u
.d
, tmp
, &env
->vec_status
);
1617 uint32_t helper_efdctsf(CPUPPCState
*env
, uint64_t val
)
1623 /* NaN are not treated the same way IEEE 754 does */
1624 if (unlikely(float64_is_any_nan(u
.d
))) {
1627 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
1628 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
1630 return float64_to_int32(u
.d
, &env
->vec_status
);
1633 uint32_t helper_efdctuf(CPUPPCState
*env
, uint64_t val
)
1639 /* NaN are not treated the same way IEEE 754 does */
1640 if (unlikely(float64_is_any_nan(u
.d
))) {
1643 tmp
= uint64_to_float64(1ULL << 32, &env
->vec_status
);
1644 u
.d
= float64_mul(u
.d
, tmp
, &env
->vec_status
);
1646 return float64_to_uint32(u
.d
, &env
->vec_status
);
1649 uint32_t helper_efscfd(CPUPPCState
*env
, uint64_t val
)
1655 u2
.f
= float64_to_float32(u1
.d
, &env
->vec_status
);
1660 uint64_t helper_efdcfs(CPUPPCState
*env
, uint32_t val
)
1666 u2
.d
= float32_to_float64(u1
.f
, &env
->vec_status
);
1671 /* Double precision fixed-point arithmetic */
1672 uint64_t helper_efdadd(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1678 u1
.d
= float64_add(u1
.d
, u2
.d
, &env
->vec_status
);
1682 uint64_t helper_efdsub(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1688 u1
.d
= float64_sub(u1
.d
, u2
.d
, &env
->vec_status
);
1692 uint64_t helper_efdmul(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1698 u1
.d
= float64_mul(u1
.d
, u2
.d
, &env
->vec_status
);
1702 uint64_t helper_efddiv(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1708 u1
.d
= float64_div(u1
.d
, u2
.d
, &env
->vec_status
);
1712 /* Double precision floating point helpers */
1713 uint32_t helper_efdtstlt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1719 return float64_lt(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
1722 uint32_t helper_efdtstgt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1728 return float64_le(u1
.d
, u2
.d
, &env
->vec_status
) ? 0 : 4;
1731 uint32_t helper_efdtsteq(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1737 return float64_eq_quiet(u1
.d
, u2
.d
, &env
->vec_status
) ? 4 : 0;
1740 uint32_t helper_efdcmplt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1742 /* XXX: TODO: test special values (NaN, infinites, ...) */
1743 return helper_efdtstlt(env
, op1
, op2
);
1746 uint32_t helper_efdcmpgt(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1748 /* XXX: TODO: test special values (NaN, infinites, ...) */
1749 return helper_efdtstgt(env
, op1
, op2
);
1752 uint32_t helper_efdcmpeq(CPUPPCState
*env
, uint64_t op1
, uint64_t op2
)
1754 /* XXX: TODO: test special values (NaN, infinites, ...) */
1755 return helper_efdtsteq(env
, op1
, op2
);
1758 #define DECODE_SPLIT(opcode, shift1, nb1, shift2, nb2) \
1759 (((((opcode) >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) | \
1760 (((opcode) >> (shift2)) & ((1 << (nb2)) - 1)))
1762 #define xT(opcode) DECODE_SPLIT(opcode, 0, 1, 21, 5)
1763 #define xA(opcode) DECODE_SPLIT(opcode, 2, 1, 16, 5)
1764 #define xB(opcode) DECODE_SPLIT(opcode, 1, 1, 11, 5)
1765 #define xC(opcode) DECODE_SPLIT(opcode, 3, 1, 6, 5)
1766 #define BF(opcode) (((opcode) >> (31-8)) & 7)
1768 typedef union _ppc_vsr_t
{
1775 static void getVSR(int n
, ppc_vsr_t
*vsr
, CPUPPCState
*env
)
1778 vsr
->f64
[0] = env
->fpr
[n
];
1779 vsr
->u64
[1] = env
->vsr
[n
];
1781 vsr
->u64
[0] = env
->avr
[n
-32].u64
[0];
1782 vsr
->u64
[1] = env
->avr
[n
-32].u64
[1];
1786 static void putVSR(int n
, ppc_vsr_t
*vsr
, CPUPPCState
*env
)
1789 env
->fpr
[n
] = vsr
->f64
[0];
1790 env
->vsr
[n
] = vsr
->u64
[1];
1792 env
->avr
[n
-32].u64
[0] = vsr
->u64
[0];
1793 env
->avr
[n
-32].u64
[1] = vsr
->u64
[1];
1797 #define float64_to_float64(x, env) x
1800 /* VSX_ADD_SUB - VSX floating point add/subract
1801 * name - instruction mnemonic
1802 * op - operation (add or sub)
1803 * nels - number of elements (1, 2 or 4)
1804 * tp - type (float32 or float64)
1805 * fld - vsr_t field (f32 or f64)
1808 #define VSX_ADD_SUB(name, op, nels, tp, fld, sfprf, r2sp) \
1809 void helper_##name(CPUPPCState *env, uint32_t opcode) \
1811 ppc_vsr_t xt, xa, xb; \
1814 getVSR(xA(opcode), &xa, env); \
1815 getVSR(xB(opcode), &xb, env); \
1816 getVSR(xT(opcode), &xt, env); \
1817 helper_reset_fpstatus(env); \
1819 for (i = 0; i < nels; i++) { \
1820 float_status tstat = env->fp_status; \
1821 set_float_exception_flags(0, &tstat); \
1822 xt.fld[i] = tp##_##op(xa.fld[i], xb.fld[i], &tstat); \
1823 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1825 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1826 if (tp##_is_infinity(xa.fld[i]) && tp##_is_infinity(xb.fld[i])) {\
1827 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
1828 } else if (tp##_is_signaling_nan(xa.fld[i]) || \
1829 tp##_is_signaling_nan(xb.fld[i])) { \
1830 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1835 xt.fld[i] = helper_frsp(env, xt.fld[i]); \
1839 helper_compute_fprf(env, xt.fld[i], sfprf); \
1842 putVSR(xT(opcode), &xt, env); \
1843 helper_float_check_status(env); \
1846 VSX_ADD_SUB(xsadddp
, add
, 1, float64
, f64
, 1, 0)
1847 VSX_ADD_SUB(xsaddsp
, add
, 1, float64
, f64
, 1, 1)
1848 VSX_ADD_SUB(xvadddp
, add
, 2, float64
, f64
, 0, 0)
1849 VSX_ADD_SUB(xvaddsp
, add
, 4, float32
, f32
, 0, 0)
1850 VSX_ADD_SUB(xssubdp
, sub
, 1, float64
, f64
, 1, 0)
1851 VSX_ADD_SUB(xssubsp
, sub
, 1, float64
, f64
, 1, 1)
1852 VSX_ADD_SUB(xvsubdp
, sub
, 2, float64
, f64
, 0, 0)
1853 VSX_ADD_SUB(xvsubsp
, sub
, 4, float32
, f32
, 0, 0)
1855 /* VSX_MUL - VSX floating point multiply
1856 * op - instruction mnemonic
1857 * nels - number of elements (1, 2 or 4)
1858 * tp - type (float32 or float64)
1859 * fld - vsr_t field (f32 or f64)
1862 #define VSX_MUL(op, nels, tp, fld, sfprf, r2sp) \
1863 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1865 ppc_vsr_t xt, xa, xb; \
1868 getVSR(xA(opcode), &xa, env); \
1869 getVSR(xB(opcode), &xb, env); \
1870 getVSR(xT(opcode), &xt, env); \
1871 helper_reset_fpstatus(env); \
1873 for (i = 0; i < nels; i++) { \
1874 float_status tstat = env->fp_status; \
1875 set_float_exception_flags(0, &tstat); \
1876 xt.fld[i] = tp##_mul(xa.fld[i], xb.fld[i], &tstat); \
1877 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1879 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1880 if ((tp##_is_infinity(xa.fld[i]) && tp##_is_zero(xb.fld[i])) || \
1881 (tp##_is_infinity(xb.fld[i]) && tp##_is_zero(xa.fld[i]))) { \
1882 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXIMZ, sfprf); \
1883 } else if (tp##_is_signaling_nan(xa.fld[i]) || \
1884 tp##_is_signaling_nan(xb.fld[i])) { \
1885 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1890 xt.fld[i] = helper_frsp(env, xt.fld[i]); \
1894 helper_compute_fprf(env, xt.fld[i], sfprf); \
1898 putVSR(xT(opcode), &xt, env); \
1899 helper_float_check_status(env); \
1902 VSX_MUL(xsmuldp
, 1, float64
, f64
, 1, 0)
1903 VSX_MUL(xsmulsp
, 1, float64
, f64
, 1, 1)
1904 VSX_MUL(xvmuldp
, 2, float64
, f64
, 0, 0)
1905 VSX_MUL(xvmulsp
, 4, float32
, f32
, 0, 0)
1907 /* VSX_DIV - VSX floating point divide
1908 * op - instruction mnemonic
1909 * nels - number of elements (1, 2 or 4)
1910 * tp - type (float32 or float64)
1911 * fld - vsr_t field (f32 or f64)
1914 #define VSX_DIV(op, nels, tp, fld, sfprf, r2sp) \
1915 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1917 ppc_vsr_t xt, xa, xb; \
1920 getVSR(xA(opcode), &xa, env); \
1921 getVSR(xB(opcode), &xb, env); \
1922 getVSR(xT(opcode), &xt, env); \
1923 helper_reset_fpstatus(env); \
1925 for (i = 0; i < nels; i++) { \
1926 float_status tstat = env->fp_status; \
1927 set_float_exception_flags(0, &tstat); \
1928 xt.fld[i] = tp##_div(xa.fld[i], xb.fld[i], &tstat); \
1929 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1931 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
1932 if (tp##_is_infinity(xa.fld[i]) && tp##_is_infinity(xb.fld[i])) { \
1933 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXIDI, sfprf); \
1934 } else if (tp##_is_zero(xa.fld[i]) && \
1935 tp##_is_zero(xb.fld[i])) { \
1936 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXZDZ, sfprf); \
1937 } else if (tp##_is_signaling_nan(xa.fld[i]) || \
1938 tp##_is_signaling_nan(xb.fld[i])) { \
1939 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1944 xt.fld[i] = helper_frsp(env, xt.fld[i]); \
1948 helper_compute_fprf(env, xt.fld[i], sfprf); \
1952 putVSR(xT(opcode), &xt, env); \
1953 helper_float_check_status(env); \
1956 VSX_DIV(xsdivdp
, 1, float64
, f64
, 1, 0)
1957 VSX_DIV(xsdivsp
, 1, float64
, f64
, 1, 1)
1958 VSX_DIV(xvdivdp
, 2, float64
, f64
, 0, 0)
1959 VSX_DIV(xvdivsp
, 4, float32
, f32
, 0, 0)
1961 /* VSX_RE - VSX floating point reciprocal estimate
1962 * op - instruction mnemonic
1963 * nels - number of elements (1, 2 or 4)
1964 * tp - type (float32 or float64)
1965 * fld - vsr_t field (f32 or f64)
1968 #define VSX_RE(op, nels, tp, fld, sfprf, r2sp) \
1969 void helper_##op(CPUPPCState *env, uint32_t opcode) \
1974 getVSR(xB(opcode), &xb, env); \
1975 getVSR(xT(opcode), &xt, env); \
1976 helper_reset_fpstatus(env); \
1978 for (i = 0; i < nels; i++) { \
1979 if (unlikely(tp##_is_signaling_nan(xb.fld[i]))) { \
1980 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
1982 xt.fld[i] = tp##_div(tp##_one, xb.fld[i], &env->fp_status); \
1985 xt.fld[i] = helper_frsp(env, xt.fld[i]); \
1989 helper_compute_fprf(env, xt.fld[0], sfprf); \
1993 putVSR(xT(opcode), &xt, env); \
1994 helper_float_check_status(env); \
1997 VSX_RE(xsredp
, 1, float64
, f64
, 1, 0)
1998 VSX_RE(xsresp
, 1, float64
, f64
, 1, 1)
1999 VSX_RE(xvredp
, 2, float64
, f64
, 0, 0)
2000 VSX_RE(xvresp
, 4, float32
, f32
, 0, 0)
2002 /* VSX_SQRT - VSX floating point square root
2003 * op - instruction mnemonic
2004 * nels - number of elements (1, 2 or 4)
2005 * tp - type (float32 or float64)
2006 * fld - vsr_t field (f32 or f64)
2009 #define VSX_SQRT(op, nels, tp, fld, sfprf, r2sp) \
2010 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2015 getVSR(xB(opcode), &xb, env); \
2016 getVSR(xT(opcode), &xt, env); \
2017 helper_reset_fpstatus(env); \
2019 for (i = 0; i < nels; i++) { \
2020 float_status tstat = env->fp_status; \
2021 set_float_exception_flags(0, &tstat); \
2022 xt.fld[i] = tp##_sqrt(xb.fld[i], &tstat); \
2023 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2025 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2026 if (tp##_is_neg(xb.fld[i]) && !tp##_is_zero(xb.fld[i])) { \
2027 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2028 } else if (tp##_is_signaling_nan(xb.fld[i])) { \
2029 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2034 xt.fld[i] = helper_frsp(env, xt.fld[i]); \
2038 helper_compute_fprf(env, xt.fld[i], sfprf); \
2042 putVSR(xT(opcode), &xt, env); \
2043 helper_float_check_status(env); \
2046 VSX_SQRT(xssqrtdp
, 1, float64
, f64
, 1, 0)
2047 VSX_SQRT(xssqrtsp
, 1, float64
, f64
, 1, 1)
2048 VSX_SQRT(xvsqrtdp
, 2, float64
, f64
, 0, 0)
2049 VSX_SQRT(xvsqrtsp
, 4, float32
, f32
, 0, 0)
2051 /* VSX_RSQRTE - VSX floating point reciprocal square root estimate
2052 * op - instruction mnemonic
2053 * nels - number of elements (1, 2 or 4)
2054 * tp - type (float32 or float64)
2055 * fld - vsr_t field (f32 or f64)
2058 #define VSX_RSQRTE(op, nels, tp, fld, sfprf, r2sp) \
2059 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2064 getVSR(xB(opcode), &xb, env); \
2065 getVSR(xT(opcode), &xt, env); \
2066 helper_reset_fpstatus(env); \
2068 for (i = 0; i < nels; i++) { \
2069 float_status tstat = env->fp_status; \
2070 set_float_exception_flags(0, &tstat); \
2071 xt.fld[i] = tp##_sqrt(xb.fld[i], &tstat); \
2072 xt.fld[i] = tp##_div(tp##_one, xt.fld[i], &tstat); \
2073 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2075 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2076 if (tp##_is_neg(xb.fld[i]) && !tp##_is_zero(xb.fld[i])) { \
2077 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSQRT, sfprf); \
2078 } else if (tp##_is_signaling_nan(xb.fld[i])) { \
2079 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2084 xt.fld[i] = helper_frsp(env, xt.fld[i]); \
2088 helper_compute_fprf(env, xt.fld[i], sfprf); \
2092 putVSR(xT(opcode), &xt, env); \
2093 helper_float_check_status(env); \
2096 VSX_RSQRTE(xsrsqrtedp
, 1, float64
, f64
, 1, 0)
2097 VSX_RSQRTE(xsrsqrtesp
, 1, float64
, f64
, 1, 1)
2098 VSX_RSQRTE(xvrsqrtedp
, 2, float64
, f64
, 0, 0)
2099 VSX_RSQRTE(xvrsqrtesp
, 4, float32
, f32
, 0, 0)
2101 /* VSX_TDIV - VSX floating point test for divide
2102 * op - instruction mnemonic
2103 * nels - number of elements (1, 2 or 4)
2104 * tp - type (float32 or float64)
2105 * fld - vsr_t field (f32 or f64)
2106 * emin - minimum unbiased exponent
2107 * emax - maximum unbiased exponent
2108 * nbits - number of fraction bits
2110 #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits) \
2111 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2118 getVSR(xA(opcode), &xa, env); \
2119 getVSR(xB(opcode), &xb, env); \
2121 for (i = 0; i < nels; i++) { \
2122 if (unlikely(tp##_is_infinity(xa.fld[i]) || \
2123 tp##_is_infinity(xb.fld[i]) || \
2124 tp##_is_zero(xb.fld[i]))) { \
2128 int e_a = ppc_##tp##_get_unbiased_exp(xa.fld[i]); \
2129 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld[i]); \
2131 if (unlikely(tp##_is_any_nan(xa.fld[i]) || \
2132 tp##_is_any_nan(xb.fld[i]))) { \
2134 } else if ((e_b <= emin) || (e_b >= (emax-2))) { \
2136 } else if (!tp##_is_zero(xa.fld[i]) && \
2137 (((e_a - e_b) >= emax) || \
2138 ((e_a - e_b) <= (emin+1)) || \
2139 (e_a <= (emin+nbits)))) { \
2143 if (unlikely(tp##_is_zero_or_denormal(xb.fld[i]))) { \
2144 /* XB is not zero because of the above check and */ \
2145 /* so must be denormalized. */ \
2151 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2154 VSX_TDIV(xstdivdp
, 1, float64
, f64
, -1022, 1023, 52)
2155 VSX_TDIV(xvtdivdp
, 2, float64
, f64
, -1022, 1023, 52)
2156 VSX_TDIV(xvtdivsp
, 4, float32
, f32
, -126, 127, 23)
2158 /* VSX_TSQRT - VSX floating point test for square root
2159 * op - instruction mnemonic
2160 * nels - number of elements (1, 2 or 4)
2161 * tp - type (float32 or float64)
2162 * fld - vsr_t field (f32 or f64)
2163 * emin - minimum unbiased exponent
2164 * emax - maximum unbiased exponent
2165 * nbits - number of fraction bits
2167 #define VSX_TSQRT(op, nels, tp, fld, emin, nbits) \
2168 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2175 getVSR(xA(opcode), &xa, env); \
2176 getVSR(xB(opcode), &xb, env); \
2178 for (i = 0; i < nels; i++) { \
2179 if (unlikely(tp##_is_infinity(xb.fld[i]) || \
2180 tp##_is_zero(xb.fld[i]))) { \
2184 int e_b = ppc_##tp##_get_unbiased_exp(xb.fld[i]); \
2186 if (unlikely(tp##_is_any_nan(xb.fld[i]))) { \
2188 } else if (unlikely(tp##_is_zero(xb.fld[i]))) { \
2190 } else if (unlikely(tp##_is_neg(xb.fld[i]))) { \
2192 } else if (!tp##_is_zero(xb.fld[i]) && \
2193 (e_b <= (emin+nbits))) { \
2197 if (unlikely(tp##_is_zero_or_denormal(xb.fld[i]))) { \
2198 /* XB is not zero because of the above check and */ \
2199 /* therefore must be denormalized. */ \
2205 env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2208 VSX_TSQRT(xstsqrtdp
, 1, float64
, f64
, -1022, 52)
2209 VSX_TSQRT(xvtsqrtdp
, 2, float64
, f64
, -1022, 52)
2210 VSX_TSQRT(xvtsqrtsp
, 4, float32
, f32
, -126, 23)
2212 /* VSX_MADD - VSX floating point muliply/add variations
2213 * op - instruction mnemonic
2214 * nels - number of elements (1, 2 or 4)
2215 * tp - type (float32 or float64)
2216 * fld - vsr_t field (f32 or f64)
2217 * maddflgs - flags for the float*muladd routine that control the
2218 * various forms (madd, msub, nmadd, nmsub)
2219 * afrm - A form (1=A, 0=M)
2222 #define VSX_MADD(op, nels, tp, fld, maddflgs, afrm, sfprf, r2sp) \
2223 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2225 ppc_vsr_t xt_in, xa, xb, xt_out; \
2229 if (afrm) { /* AxB + T */ \
2232 } else { /* AxT + B */ \
2237 getVSR(xA(opcode), &xa, env); \
2238 getVSR(xB(opcode), &xb, env); \
2239 getVSR(xT(opcode), &xt_in, env); \
2243 helper_reset_fpstatus(env); \
2245 for (i = 0; i < nels; i++) { \
2246 float_status tstat = env->fp_status; \
2247 set_float_exception_flags(0, &tstat); \
2248 if (r2sp && (tstat.float_rounding_mode == float_round_nearest_even)) {\
2249 /* Avoid double rounding errors by rounding the intermediate */ \
2250 /* result to odd. */ \
2251 set_float_rounding_mode(float_round_to_zero, &tstat); \
2252 xt_out.fld[i] = tp##_muladd(xa.fld[i], b->fld[i], c->fld[i], \
2253 maddflgs, &tstat); \
2254 xt_out.fld[i] |= (get_float_exception_flags(&tstat) & \
2255 float_flag_inexact) != 0; \
2257 xt_out.fld[i] = tp##_muladd(xa.fld[i], b->fld[i], c->fld[i], \
2258 maddflgs, &tstat); \
2260 env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2262 if (unlikely(tstat.float_exception_flags & float_flag_invalid)) { \
2263 if (tp##_is_signaling_nan(xa.fld[i]) || \
2264 tp##_is_signaling_nan(b->fld[i]) || \
2265 tp##_is_signaling_nan(c->fld[i])) { \
2266 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, sfprf); \
2267 tstat.float_exception_flags &= ~float_flag_invalid; \
2269 if ((tp##_is_infinity(xa.fld[i]) && tp##_is_zero(b->fld[i])) || \
2270 (tp##_is_zero(xa.fld[i]) && tp##_is_infinity(b->fld[i]))) { \
2271 xt_out.fld[i] = float64_to_##tp(fload_invalid_op_excp(env, \
2272 POWERPC_EXCP_FP_VXIMZ, sfprf), &env->fp_status); \
2273 tstat.float_exception_flags &= ~float_flag_invalid; \
2275 if ((tstat.float_exception_flags & float_flag_invalid) && \
2276 ((tp##_is_infinity(xa.fld[i]) || \
2277 tp##_is_infinity(b->fld[i])) && \
2278 tp##_is_infinity(c->fld[i]))) { \
2279 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXISI, sfprf); \
2284 xt_out.fld[i] = helper_frsp(env, xt_out.fld[i]); \
2288 helper_compute_fprf(env, xt_out.fld[i], sfprf); \
2291 putVSR(xT(opcode), &xt_out, env); \
2292 helper_float_check_status(env); \
2296 #define MSUB_FLGS float_muladd_negate_c
2297 #define NMADD_FLGS float_muladd_negate_result
2298 #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
2300 VSX_MADD(xsmaddadp
, 1, float64
, f64
, MADD_FLGS
, 1, 1, 0)
2301 VSX_MADD(xsmaddmdp
, 1, float64
, f64
, MADD_FLGS
, 0, 1, 0)
2302 VSX_MADD(xsmsubadp
, 1, float64
, f64
, MSUB_FLGS
, 1, 1, 0)
2303 VSX_MADD(xsmsubmdp
, 1, float64
, f64
, MSUB_FLGS
, 0, 1, 0)
2304 VSX_MADD(xsnmaddadp
, 1, float64
, f64
, NMADD_FLGS
, 1, 1, 0)
2305 VSX_MADD(xsnmaddmdp
, 1, float64
, f64
, NMADD_FLGS
, 0, 1, 0)
2306 VSX_MADD(xsnmsubadp
, 1, float64
, f64
, NMSUB_FLGS
, 1, 1, 0)
2307 VSX_MADD(xsnmsubmdp
, 1, float64
, f64
, NMSUB_FLGS
, 0, 1, 0)
2309 VSX_MADD(xsmaddasp
, 1, float64
, f64
, MADD_FLGS
, 1, 1, 1)
2310 VSX_MADD(xsmaddmsp
, 1, float64
, f64
, MADD_FLGS
, 0, 1, 1)
2311 VSX_MADD(xsmsubasp
, 1, float64
, f64
, MSUB_FLGS
, 1, 1, 1)
2312 VSX_MADD(xsmsubmsp
, 1, float64
, f64
, MSUB_FLGS
, 0, 1, 1)
2313 VSX_MADD(xsnmaddasp
, 1, float64
, f64
, NMADD_FLGS
, 1, 1, 1)
2314 VSX_MADD(xsnmaddmsp
, 1, float64
, f64
, NMADD_FLGS
, 0, 1, 1)
2315 VSX_MADD(xsnmsubasp
, 1, float64
, f64
, NMSUB_FLGS
, 1, 1, 1)
2316 VSX_MADD(xsnmsubmsp
, 1, float64
, f64
, NMSUB_FLGS
, 0, 1, 1)
2318 VSX_MADD(xvmaddadp
, 2, float64
, f64
, MADD_FLGS
, 1, 0, 0)
2319 VSX_MADD(xvmaddmdp
, 2, float64
, f64
, MADD_FLGS
, 0, 0, 0)
2320 VSX_MADD(xvmsubadp
, 2, float64
, f64
, MSUB_FLGS
, 1, 0, 0)
2321 VSX_MADD(xvmsubmdp
, 2, float64
, f64
, MSUB_FLGS
, 0, 0, 0)
2322 VSX_MADD(xvnmaddadp
, 2, float64
, f64
, NMADD_FLGS
, 1, 0, 0)
2323 VSX_MADD(xvnmaddmdp
, 2, float64
, f64
, NMADD_FLGS
, 0, 0, 0)
2324 VSX_MADD(xvnmsubadp
, 2, float64
, f64
, NMSUB_FLGS
, 1, 0, 0)
2325 VSX_MADD(xvnmsubmdp
, 2, float64
, f64
, NMSUB_FLGS
, 0, 0, 0)
2327 VSX_MADD(xvmaddasp
, 4, float32
, f32
, MADD_FLGS
, 1, 0, 0)
2328 VSX_MADD(xvmaddmsp
, 4, float32
, f32
, MADD_FLGS
, 0, 0, 0)
2329 VSX_MADD(xvmsubasp
, 4, float32
, f32
, MSUB_FLGS
, 1, 0, 0)
2330 VSX_MADD(xvmsubmsp
, 4, float32
, f32
, MSUB_FLGS
, 0, 0, 0)
2331 VSX_MADD(xvnmaddasp
, 4, float32
, f32
, NMADD_FLGS
, 1, 0, 0)
2332 VSX_MADD(xvnmaddmsp
, 4, float32
, f32
, NMADD_FLGS
, 0, 0, 0)
2333 VSX_MADD(xvnmsubasp
, 4, float32
, f32
, NMSUB_FLGS
, 1, 0, 0)
2334 VSX_MADD(xvnmsubmsp
, 4, float32
, f32
, NMSUB_FLGS
, 0, 0, 0)
2336 #define VSX_SCALAR_CMP(op, ordered) \
2337 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2342 getVSR(xA(opcode), &xa, env); \
2343 getVSR(xB(opcode), &xb, env); \
2345 if (unlikely(float64_is_any_nan(xa.f64[0]) || \
2346 float64_is_any_nan(xb.f64[0]))) { \
2347 if (float64_is_signaling_nan(xa.f64[0]) || \
2348 float64_is_signaling_nan(xb.f64[0])) { \
2349 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2352 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2356 if (float64_lt(xa.f64[0], xb.f64[0], &env->fp_status)) { \
2358 } else if (!float64_le(xa.f64[0], xb.f64[0], &env->fp_status)) { \
2365 env->fpscr &= ~(0x0F << FPSCR_FPRF); \
2366 env->fpscr |= cc << FPSCR_FPRF; \
2367 env->crf[BF(opcode)] = cc; \
2369 helper_float_check_status(env); \
2372 VSX_SCALAR_CMP(xscmpodp
, 1)
2373 VSX_SCALAR_CMP(xscmpudp
, 0)
2375 #define float64_snan_to_qnan(x) ((x) | 0x0008000000000000ULL)
2376 #define float32_snan_to_qnan(x) ((x) | 0x00400000)
2378 /* VSX_MAX_MIN - VSX floating point maximum/minimum
2379 * name - instruction mnemonic
2380 * op - operation (max or min)
2381 * nels - number of elements (1, 2 or 4)
2382 * tp - type (float32 or float64)
2383 * fld - vsr_t field (f32 or f64)
2385 #define VSX_MAX_MIN(name, op, nels, tp, fld) \
2386 void helper_##name(CPUPPCState *env, uint32_t opcode) \
2388 ppc_vsr_t xt, xa, xb; \
2391 getVSR(xA(opcode), &xa, env); \
2392 getVSR(xB(opcode), &xb, env); \
2393 getVSR(xT(opcode), &xt, env); \
2395 for (i = 0; i < nels; i++) { \
2396 xt.fld[i] = tp##_##op(xa.fld[i], xb.fld[i], &env->fp_status); \
2397 if (unlikely(tp##_is_signaling_nan(xa.fld[i]) || \
2398 tp##_is_signaling_nan(xb.fld[i]))) { \
2399 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2403 putVSR(xT(opcode), &xt, env); \
2404 helper_float_check_status(env); \
2407 VSX_MAX_MIN(xsmaxdp
, maxnum
, 1, float64
, f64
)
2408 VSX_MAX_MIN(xvmaxdp
, maxnum
, 2, float64
, f64
)
2409 VSX_MAX_MIN(xvmaxsp
, maxnum
, 4, float32
, f32
)
2410 VSX_MAX_MIN(xsmindp
, minnum
, 1, float64
, f64
)
2411 VSX_MAX_MIN(xvmindp
, minnum
, 2, float64
, f64
)
2412 VSX_MAX_MIN(xvminsp
, minnum
, 4, float32
, f32
)
2414 /* VSX_CMP - VSX floating point compare
2415 * op - instruction mnemonic
2416 * nels - number of elements (1, 2 or 4)
2417 * tp - type (float32 or float64)
2418 * fld - vsr_t field (f32 or f64)
2419 * cmp - comparison operation
2420 * svxvc - set VXVC bit
2422 #define VSX_CMP(op, nels, tp, fld, cmp, svxvc) \
2423 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2425 ppc_vsr_t xt, xa, xb; \
2428 int all_false = 1; \
2430 getVSR(xA(opcode), &xa, env); \
2431 getVSR(xB(opcode), &xb, env); \
2432 getVSR(xT(opcode), &xt, env); \
2434 for (i = 0; i < nels; i++) { \
2435 if (unlikely(tp##_is_any_nan(xa.fld[i]) || \
2436 tp##_is_any_nan(xb.fld[i]))) { \
2437 if (tp##_is_signaling_nan(xa.fld[i]) || \
2438 tp##_is_signaling_nan(xb.fld[i])) { \
2439 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2442 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXVC, 0); \
2447 if (tp##_##cmp(xb.fld[i], xa.fld[i], &env->fp_status) == 1) { \
2457 putVSR(xT(opcode), &xt, env); \
2458 if ((opcode >> (31-21)) & 1) { \
2459 env->crf[6] = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0); \
2461 helper_float_check_status(env); \
2464 VSX_CMP(xvcmpeqdp
, 2, float64
, f64
, eq
, 0)
2465 VSX_CMP(xvcmpgedp
, 2, float64
, f64
, le
, 1)
2466 VSX_CMP(xvcmpgtdp
, 2, float64
, f64
, lt
, 1)
2467 VSX_CMP(xvcmpeqsp
, 4, float32
, f32
, eq
, 0)
2468 VSX_CMP(xvcmpgesp
, 4, float32
, f32
, le
, 1)
2469 VSX_CMP(xvcmpgtsp
, 4, float32
, f32
, lt
, 1)
2471 #if defined(HOST_WORDS_BIGENDIAN)
2477 /* VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2478 * op - instruction mnemonic
2479 * nels - number of elements (1, 2 or 4)
2480 * stp - source type (float32 or float64)
2481 * ttp - target type (float32 or float64)
2482 * sfld - source vsr_t field
2483 * tfld - target vsr_t field (f32 or f64)
2486 #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf) \
2487 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2492 getVSR(xB(opcode), &xb, env); \
2493 getVSR(xT(opcode), &xt, env); \
2495 for (i = 0; i < nels; i++) { \
2496 int j = 2*i + JOFFSET; \
2497 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2498 if (unlikely(stp##_is_signaling_nan(xb.sfld))) { \
2499 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2500 xt.tfld = ttp##_snan_to_qnan(xt.tfld); \
2503 helper_compute_fprf(env, ttp##_to_float64(xt.tfld, \
2504 &env->fp_status), sfprf); \
2508 putVSR(xT(opcode), &xt, env); \
2509 helper_float_check_status(env); \
2512 VSX_CVT_FP_TO_FP(xscvdpsp
, 1, float64
, float32
, f64
[i
], f32
[j
], 1)
2513 VSX_CVT_FP_TO_FP(xscvspdp
, 1, float32
, float64
, f32
[j
], f64
[i
], 1)
2514 VSX_CVT_FP_TO_FP(xvcvdpsp
, 2, float64
, float32
, f64
[i
], f32
[j
], 0)
2515 VSX_CVT_FP_TO_FP(xvcvspdp
, 2, float32
, float64
, f32
[j
], f64
[i
], 0)
2517 uint64_t helper_xscvdpspn(CPUPPCState
*env
, uint64_t xb
)
2519 float_status tstat
= env
->fp_status
;
2520 set_float_exception_flags(0, &tstat
);
2522 return (uint64_t)float64_to_float32(xb
, &tstat
) << 32;
2525 uint64_t helper_xscvspdpn(CPUPPCState
*env
, uint64_t xb
)
2527 float_status tstat
= env
->fp_status
;
2528 set_float_exception_flags(0, &tstat
);
2530 return float32_to_float64(xb
>> 32, &tstat
);
2533 /* VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2534 * op - instruction mnemonic
2535 * nels - number of elements (1, 2 or 4)
2536 * stp - source type (float32 or float64)
2537 * ttp - target type (int32, uint32, int64 or uint64)
2538 * sfld - source vsr_t field
2539 * tfld - target vsr_t field
2540 * jdef - definition of the j index (i or 2*i)
2541 * rnan - resulting NaN
2543 #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, jdef, rnan) \
2544 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2549 getVSR(xB(opcode), &xb, env); \
2550 getVSR(xT(opcode), &xt, env); \
2552 for (i = 0; i < nels; i++) { \
2554 if (unlikely(stp##_is_any_nan(xb.sfld))) { \
2555 if (stp##_is_signaling_nan(xb.sfld)) { \
2556 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2558 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2561 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2562 if (env->fp_status.float_exception_flags & float_flag_invalid) { \
2563 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, 0); \
2568 putVSR(xT(opcode), &xt, env); \
2569 helper_float_check_status(env); \
2572 VSX_CVT_FP_TO_INT(xscvdpsxds
, 1, float64
, int64
, f64
[j
], u64
[i
], i
, \
2573 0x8000000000000000ULL
)
2574 VSX_CVT_FP_TO_INT(xscvdpsxws
, 1, float64
, int32
, f64
[i
], u32
[j
], \
2575 2*i
+ JOFFSET
, 0x80000000U
)
2576 VSX_CVT_FP_TO_INT(xscvdpuxds
, 1, float64
, uint64
, f64
[j
], u64
[i
], i
, 0ULL)
2577 VSX_CVT_FP_TO_INT(xscvdpuxws
, 1, float64
, uint32
, f64
[i
], u32
[j
], \
2579 VSX_CVT_FP_TO_INT(xvcvdpsxds
, 2, float64
, int64
, f64
[j
], u64
[i
], i
, \
2580 0x8000000000000000ULL
)
2581 VSX_CVT_FP_TO_INT(xvcvdpsxws
, 2, float64
, int32
, f64
[i
], u32
[j
], \
2582 2*i
+ JOFFSET
, 0x80000000U
)
2583 VSX_CVT_FP_TO_INT(xvcvdpuxds
, 2, float64
, uint64
, f64
[j
], u64
[i
], i
, 0ULL)
2584 VSX_CVT_FP_TO_INT(xvcvdpuxws
, 2, float64
, uint32
, f64
[i
], u32
[j
], \
2586 VSX_CVT_FP_TO_INT(xvcvspsxds
, 2, float32
, int64
, f32
[j
], u64
[i
], \
2587 2*i
+ JOFFSET
, 0x8000000000000000ULL
)
2588 VSX_CVT_FP_TO_INT(xvcvspsxws
, 4, float32
, int32
, f32
[j
], u32
[j
], i
, \
2590 VSX_CVT_FP_TO_INT(xvcvspuxds
, 2, float32
, uint64
, f32
[j
], u64
[i
], \
2591 2*i
+ JOFFSET
, 0ULL)
2592 VSX_CVT_FP_TO_INT(xvcvspuxws
, 4, float32
, uint32
, f32
[j
], u32
[i
], i
, 0U)
2594 /* VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
2595 * op - instruction mnemonic
2596 * nels - number of elements (1, 2 or 4)
2597 * stp - source type (int32, uint32, int64 or uint64)
2598 * ttp - target type (float32 or float64)
2599 * sfld - source vsr_t field
2600 * tfld - target vsr_t field
2601 * jdef - definition of the j index (i or 2*i)
2604 #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, jdef, sfprf, r2sp) \
2605 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2610 getVSR(xB(opcode), &xb, env); \
2611 getVSR(xT(opcode), &xt, env); \
2613 for (i = 0; i < nels; i++) { \
2615 xt.tfld = stp##_to_##ttp(xb.sfld, &env->fp_status); \
2617 xt.tfld = helper_frsp(env, xt.tfld); \
2620 helper_compute_fprf(env, xt.tfld, sfprf); \
2624 putVSR(xT(opcode), &xt, env); \
2625 helper_float_check_status(env); \
2628 VSX_CVT_INT_TO_FP(xscvsxddp
, 1, int64
, float64
, u64
[j
], f64
[i
], i
, 1, 0)
2629 VSX_CVT_INT_TO_FP(xscvuxddp
, 1, uint64
, float64
, u64
[j
], f64
[i
], i
, 1, 0)
2630 VSX_CVT_INT_TO_FP(xscvsxdsp
, 1, int64
, float64
, u64
[j
], f64
[i
], i
, 1, 1)
2631 VSX_CVT_INT_TO_FP(xscvuxdsp
, 1, uint64
, float64
, u64
[j
], f64
[i
], i
, 1, 1)
2632 VSX_CVT_INT_TO_FP(xvcvsxddp
, 2, int64
, float64
, u64
[j
], f64
[i
], i
, 0, 0)
2633 VSX_CVT_INT_TO_FP(xvcvuxddp
, 2, uint64
, float64
, u64
[j
], f64
[i
], i
, 0, 0)
2634 VSX_CVT_INT_TO_FP(xvcvsxwdp
, 2, int32
, float64
, u32
[j
], f64
[i
], \
2635 2*i
+ JOFFSET
, 0, 0)
2636 VSX_CVT_INT_TO_FP(xvcvuxwdp
, 2, uint64
, float64
, u32
[j
], f64
[i
], \
2637 2*i
+ JOFFSET
, 0, 0)
2638 VSX_CVT_INT_TO_FP(xvcvsxdsp
, 2, int64
, float32
, u64
[i
], f32
[j
], \
2639 2*i
+ JOFFSET
, 0, 0)
2640 VSX_CVT_INT_TO_FP(xvcvuxdsp
, 2, uint64
, float32
, u64
[i
], f32
[j
], \
2641 2*i
+ JOFFSET
, 0, 0)
2642 VSX_CVT_INT_TO_FP(xvcvsxwsp
, 4, int32
, float32
, u32
[j
], f32
[i
], i
, 0, 0)
2643 VSX_CVT_INT_TO_FP(xvcvuxwsp
, 4, uint32
, float32
, u32
[j
], f32
[i
], i
, 0, 0)
2645 /* For "use current rounding mode", define a value that will not be one of
2646 * the existing rounding model enums.
2648 #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
2649 float_round_up + float_round_to_zero)
2651 /* VSX_ROUND - VSX floating point round
2652 * op - instruction mnemonic
2653 * nels - number of elements (1, 2 or 4)
2654 * tp - type (float32 or float64)
2655 * fld - vsr_t field (f32 or f64)
2656 * rmode - rounding mode
2659 #define VSX_ROUND(op, nels, tp, fld, rmode, sfprf) \
2660 void helper_##op(CPUPPCState *env, uint32_t opcode) \
2664 getVSR(xB(opcode), &xb, env); \
2665 getVSR(xT(opcode), &xt, env); \
2667 if (rmode != FLOAT_ROUND_CURRENT) { \
2668 set_float_rounding_mode(rmode, &env->fp_status); \
2671 for (i = 0; i < nels; i++) { \
2672 if (unlikely(tp##_is_signaling_nan(xb.fld[i]))) { \
2673 fload_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, 0); \
2674 xt.fld[i] = tp##_snan_to_qnan(xb.fld[i]); \
2676 xt.fld[i] = tp##_round_to_int(xb.fld[i], &env->fp_status); \
2679 helper_compute_fprf(env, xt.fld[i], sfprf); \
2683 /* If this is not a "use current rounding mode" instruction, \
2684 * then inhibit setting of the XX bit and restore rounding \
2685 * mode from FPSCR */ \
2686 if (rmode != FLOAT_ROUND_CURRENT) { \
2687 fpscr_set_rounding_mode(env); \
2688 env->fp_status.float_exception_flags &= ~float_flag_inexact; \
2691 putVSR(xT(opcode), &xt, env); \
2692 helper_float_check_status(env); \
2695 VSX_ROUND(xsrdpi
, 1, float64
, f64
, float_round_nearest_even
, 1)
2696 VSX_ROUND(xsrdpic
, 1, float64
, f64
, FLOAT_ROUND_CURRENT
, 1)
2697 VSX_ROUND(xsrdpim
, 1, float64
, f64
, float_round_down
, 1)
2698 VSX_ROUND(xsrdpip
, 1, float64
, f64
, float_round_up
, 1)
2699 VSX_ROUND(xsrdpiz
, 1, float64
, f64
, float_round_to_zero
, 1)
2701 VSX_ROUND(xvrdpi
, 2, float64
, f64
, float_round_nearest_even
, 0)
2702 VSX_ROUND(xvrdpic
, 2, float64
, f64
, FLOAT_ROUND_CURRENT
, 0)
2703 VSX_ROUND(xvrdpim
, 2, float64
, f64
, float_round_down
, 0)
2704 VSX_ROUND(xvrdpip
, 2, float64
, f64
, float_round_up
, 0)
2705 VSX_ROUND(xvrdpiz
, 2, float64
, f64
, float_round_to_zero
, 0)
2707 VSX_ROUND(xvrspi
, 4, float32
, f32
, float_round_nearest_even
, 0)
2708 VSX_ROUND(xvrspic
, 4, float32
, f32
, FLOAT_ROUND_CURRENT
, 0)
2709 VSX_ROUND(xvrspim
, 4, float32
, f32
, float_round_down
, 0)
2710 VSX_ROUND(xvrspip
, 4, float32
, f32
, float_round_up
, 0)
2711 VSX_ROUND(xvrspiz
, 4, float32
, f32
, float_round_to_zero
, 0)
2713 uint64_t helper_xsrsp(CPUPPCState
*env
, uint64_t xb
)
2715 helper_reset_fpstatus(env
);
2717 uint64_t xt
= helper_frsp(env
, xb
);
2719 helper_compute_fprf(env
, xt
, 1);
2720 helper_float_check_status(env
);