xref: /qemu/target/ppc/fpu_helper.c (revision e6f1bfb2)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  PowerPC floating point and SPE emulation helpers for QEMU.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2007 Jocelyn Mayer
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
9fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth #include "qemu/osdep.h"
20fcf5ef2aSThomas Huth #include "cpu.h"
21fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
22fcf5ef2aSThomas Huth #include "exec/exec-all.h"
23985e3023SBharata B Rao #include "internal.h"
2424f91e81SAlex Bennée #include "fpu/softfloat.h"
25fcf5ef2aSThomas Huth 
26e5487803SBharata B Rao static inline float128 float128_snan_to_qnan(float128 x)
27e5487803SBharata B Rao {
28e5487803SBharata B Rao     float128 r;
29e5487803SBharata B Rao 
30e5487803SBharata B Rao     r.high = x.high | 0x0000800000000000;
31e5487803SBharata B Rao     r.low = x.low;
32e5487803SBharata B Rao     return r;
33e5487803SBharata B Rao }
34e5487803SBharata B Rao 
35fcf5ef2aSThomas Huth #define float64_snan_to_qnan(x) ((x) | 0x0008000000000000ULL)
36fcf5ef2aSThomas Huth #define float32_snan_to_qnan(x) ((x) | 0x00400000)
37f566c047SBharata B Rao #define float16_snan_to_qnan(x) ((x) | 0x0200)
38fcf5ef2aSThomas Huth 
39e82c42b7SRichard Henderson static inline bool fp_exceptions_enabled(CPUPPCState *env)
40e82c42b7SRichard Henderson {
41e82c42b7SRichard Henderson #ifdef CONFIG_USER_ONLY
42e82c42b7SRichard Henderson     return true;
43e82c42b7SRichard Henderson #else
44e82c42b7SRichard Henderson     return (env->msr & ((1U << MSR_FE0) | (1U << MSR_FE1))) != 0;
45e82c42b7SRichard Henderson #endif
46e82c42b7SRichard Henderson }
47e82c42b7SRichard Henderson 
48fcf5ef2aSThomas Huth /*****************************************************************************/
49fcf5ef2aSThomas Huth /* Floating point operations helpers */
50fcf5ef2aSThomas Huth 
5186c0cab1SRichard Henderson /*
5286c0cab1SRichard Henderson  * This is the non-arithmatic conversion that happens e.g. on loads.
5386c0cab1SRichard Henderson  * In the Power ISA pseudocode, this is called DOUBLE.
5486c0cab1SRichard Henderson  */
5586c0cab1SRichard Henderson uint64_t helper_todouble(uint32_t arg)
5686c0cab1SRichard Henderson {
5786c0cab1SRichard Henderson     uint32_t abs_arg = arg & 0x7fffffff;
5886c0cab1SRichard Henderson     uint64_t ret;
5986c0cab1SRichard Henderson 
6086c0cab1SRichard Henderson     if (likely(abs_arg >= 0x00800000)) {
6186c0cab1SRichard Henderson         /* Normalized operand, or Inf, or NaN.  */
6286c0cab1SRichard Henderson         ret  = (uint64_t)extract32(arg, 30, 2) << 62;
6386c0cab1SRichard Henderson         ret |= ((extract32(arg, 30, 1) ^ 1) * (uint64_t)7) << 59;
6486c0cab1SRichard Henderson         ret |= (uint64_t)extract32(arg, 0, 30) << 29;
6586c0cab1SRichard Henderson     } else {
6686c0cab1SRichard Henderson         /* Zero or Denormalized operand.  */
6786c0cab1SRichard Henderson         ret = (uint64_t)extract32(arg, 31, 1) << 63;
6886c0cab1SRichard Henderson         if (unlikely(abs_arg != 0)) {
6986c0cab1SRichard Henderson             /* Denormalized operand.  */
7086c0cab1SRichard Henderson             int shift = clz32(abs_arg) - 9;
7186c0cab1SRichard Henderson             int exp = -126 - shift + 1023;
7286c0cab1SRichard Henderson             ret |= (uint64_t)exp << 52;
7386c0cab1SRichard Henderson             ret |= abs_arg << (shift + 29);
7486c0cab1SRichard Henderson         }
7586c0cab1SRichard Henderson     }
7686c0cab1SRichard Henderson     return ret;
77fcf5ef2aSThomas Huth }
78fcf5ef2aSThomas Huth 
7986c0cab1SRichard Henderson /*
8086c0cab1SRichard Henderson  * This is the non-arithmatic conversion that happens e.g. on stores.
8186c0cab1SRichard Henderson  * In the Power ISA pseudocode, this is called SINGLE.
8286c0cab1SRichard Henderson  */
8386c0cab1SRichard Henderson uint32_t helper_tosingle(uint64_t arg)
84fcf5ef2aSThomas Huth {
8586c0cab1SRichard Henderson     int exp = extract64(arg, 52, 11);
8686c0cab1SRichard Henderson     uint32_t ret;
87fcf5ef2aSThomas Huth 
8886c0cab1SRichard Henderson     if (likely(exp > 896)) {
8986c0cab1SRichard Henderson         /* No denormalization required (includes Inf, NaN).  */
9086c0cab1SRichard Henderson         ret  = extract64(arg, 62, 2) << 30;
9186c0cab1SRichard Henderson         ret |= extract64(arg, 29, 30);
9286c0cab1SRichard Henderson     } else {
93fa9ebf8cSDavid Gibson         /*
94fa9ebf8cSDavid Gibson          * Zero or Denormal result.  If the exponent is in bounds for
95fa9ebf8cSDavid Gibson          * a single-precision denormal result, extract the proper
96fa9ebf8cSDavid Gibson          * bits.  If the input is not zero, and the exponent is out of
97fa9ebf8cSDavid Gibson          * bounds, then the result is undefined; this underflows to
98fa9ebf8cSDavid Gibson          * zero.
9986c0cab1SRichard Henderson          */
10086c0cab1SRichard Henderson         ret = extract64(arg, 63, 1) << 31;
10186c0cab1SRichard Henderson         if (unlikely(exp >= 874)) {
10286c0cab1SRichard Henderson             /* Denormal result.  */
10386c0cab1SRichard Henderson             ret |= ((1ULL << 52) | extract64(arg, 0, 52)) >> (896 + 30 - exp);
10486c0cab1SRichard Henderson         }
10586c0cab1SRichard Henderson     }
10686c0cab1SRichard Henderson     return ret;
107fcf5ef2aSThomas Huth }
108fcf5ef2aSThomas Huth 
109fcf5ef2aSThomas Huth static inline int ppc_float32_get_unbiased_exp(float32 f)
110fcf5ef2aSThomas Huth {
111fcf5ef2aSThomas Huth     return ((f >> 23) & 0xFF) - 127;
112fcf5ef2aSThomas Huth }
113fcf5ef2aSThomas Huth 
114fcf5ef2aSThomas Huth static inline int ppc_float64_get_unbiased_exp(float64 f)
115fcf5ef2aSThomas Huth {
116fcf5ef2aSThomas Huth     return ((f >> 52) & 0x7FF) - 1023;
117fcf5ef2aSThomas Huth }
118fcf5ef2aSThomas Huth 
1190394d7a6SRichard Henderson /* Classify a floating-point number.  */
1200394d7a6SRichard Henderson enum {
1210394d7a6SRichard Henderson     is_normal   = 1,
1220394d7a6SRichard Henderson     is_zero     = 2,
1230394d7a6SRichard Henderson     is_denormal = 4,
1240394d7a6SRichard Henderson     is_inf      = 8,
1250394d7a6SRichard Henderson     is_qnan     = 16,
1260394d7a6SRichard Henderson     is_snan     = 32,
1270394d7a6SRichard Henderson     is_neg      = 64,
1280394d7a6SRichard Henderson };
1290394d7a6SRichard Henderson 
1300394d7a6SRichard Henderson #define COMPUTE_CLASS(tp)                                      \
1310394d7a6SRichard Henderson static int tp##_classify(tp arg)                               \
1320394d7a6SRichard Henderson {                                                              \
1330394d7a6SRichard Henderson     int ret = tp##_is_neg(arg) * is_neg;                       \
1340394d7a6SRichard Henderson     if (unlikely(tp##_is_any_nan(arg))) {                      \
1350394d7a6SRichard Henderson         float_status dummy = { };  /* snan_bit_is_one = 0 */   \
1360394d7a6SRichard Henderson         ret |= (tp##_is_signaling_nan(arg, &dummy)             \
1370394d7a6SRichard Henderson                 ? is_snan : is_qnan);                          \
1380394d7a6SRichard Henderson     } else if (unlikely(tp##_is_infinity(arg))) {              \
1390394d7a6SRichard Henderson         ret |= is_inf;                                         \
1400394d7a6SRichard Henderson     } else if (tp##_is_zero(arg)) {                            \
1410394d7a6SRichard Henderson         ret |= is_zero;                                        \
1420394d7a6SRichard Henderson     } else if (tp##_is_zero_or_denormal(arg)) {                \
1430394d7a6SRichard Henderson         ret |= is_denormal;                                    \
1440394d7a6SRichard Henderson     } else {                                                   \
1450394d7a6SRichard Henderson         ret |= is_normal;                                      \
1460394d7a6SRichard Henderson     }                                                          \
1470394d7a6SRichard Henderson     return ret;                                                \
1480394d7a6SRichard Henderson }
1490394d7a6SRichard Henderson 
1500394d7a6SRichard Henderson COMPUTE_CLASS(float16)
1510394d7a6SRichard Henderson COMPUTE_CLASS(float32)
1520394d7a6SRichard Henderson COMPUTE_CLASS(float64)
1530394d7a6SRichard Henderson COMPUTE_CLASS(float128)
1540394d7a6SRichard Henderson 
1550394d7a6SRichard Henderson static void set_fprf_from_class(CPUPPCState *env, int class)
1560394d7a6SRichard Henderson {
1570394d7a6SRichard Henderson     static const uint8_t fprf[6][2] = {
1580394d7a6SRichard Henderson         { 0x04, 0x08 },  /* normalized */
1590394d7a6SRichard Henderson         { 0x02, 0x12 },  /* zero */
1600394d7a6SRichard Henderson         { 0x14, 0x18 },  /* denormalized */
1610394d7a6SRichard Henderson         { 0x05, 0x09 },  /* infinity */
1620394d7a6SRichard Henderson         { 0x11, 0x11 },  /* qnan */
1630394d7a6SRichard Henderson         { 0x00, 0x00 },  /* snan -- flags are undefined */
1640394d7a6SRichard Henderson     };
1650394d7a6SRichard Henderson     bool isneg = class & is_neg;
1660394d7a6SRichard Henderson 
1670394d7a6SRichard Henderson     env->fpscr &= ~(0x1F << FPSCR_FPRF);
1680394d7a6SRichard Henderson     env->fpscr |= fprf[ctz32(class)][isneg] << FPSCR_FPRF;
1690394d7a6SRichard Henderson }
1700394d7a6SRichard Henderson 
171ffc67420SBharata B Rao #define COMPUTE_FPRF(tp)                                \
172ffc67420SBharata B Rao void helper_compute_fprf_##tp(CPUPPCState *env, tp arg) \
173ffc67420SBharata B Rao {                                                       \
1740394d7a6SRichard Henderson     set_fprf_from_class(env, tp##_classify(arg));       \
175ffc67420SBharata B Rao }
176fcf5ef2aSThomas Huth 
177f566c047SBharata B Rao COMPUTE_FPRF(float16)
1789aeae8e1SBharata B Rao COMPUTE_FPRF(float32)
179ffc67420SBharata B Rao COMPUTE_FPRF(float64)
18007bdd247SBharata B Rao COMPUTE_FPRF(float128)
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth /* Floating-point invalid operations exception */
18313c9115fSRichard Henderson static void finish_invalid_op_excp(CPUPPCState *env, int op, uintptr_t retaddr)
184fcf5ef2aSThomas Huth {
18513c9115fSRichard Henderson     /* Update the floating-point invalid operation summary */
18613c9115fSRichard Henderson     env->fpscr |= 1 << FPSCR_VX;
18713c9115fSRichard Henderson     /* Update the floating-point exception summary */
18813c9115fSRichard Henderson     env->fpscr |= FP_FX;
18913c9115fSRichard Henderson     if (fpscr_ve != 0) {
19013c9115fSRichard Henderson         /* Update the floating-point enabled exception summary */
19113c9115fSRichard Henderson         env->fpscr |= 1 << FPSCR_FEX;
19213c9115fSRichard Henderson         if (fp_exceptions_enabled(env)) {
19313c9115fSRichard Henderson             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
19413c9115fSRichard Henderson                                    POWERPC_EXCP_FP | op, retaddr);
19513c9115fSRichard Henderson         }
19613c9115fSRichard Henderson     }
19713c9115fSRichard Henderson }
198fcf5ef2aSThomas Huth 
19913c9115fSRichard Henderson static void finish_invalid_op_arith(CPUPPCState *env, int op,
20013c9115fSRichard Henderson                                     bool set_fpcc, uintptr_t retaddr)
20113c9115fSRichard Henderson {
20213c9115fSRichard Henderson     env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
20313c9115fSRichard Henderson     if (fpscr_ve == 0) {
20413c9115fSRichard Henderson         if (set_fpcc) {
20513c9115fSRichard Henderson             env->fpscr &= ~(0xF << FPSCR_FPCC);
20613c9115fSRichard Henderson             env->fpscr |= 0x11 << FPSCR_FPCC;
20713c9115fSRichard Henderson         }
20813c9115fSRichard Henderson     }
20913c9115fSRichard Henderson     finish_invalid_op_excp(env, op, retaddr);
21013c9115fSRichard Henderson }
21113c9115fSRichard Henderson 
21213c9115fSRichard Henderson /* Signalling NaN */
21313c9115fSRichard Henderson static void float_invalid_op_vxsnan(CPUPPCState *env, uintptr_t retaddr)
21413c9115fSRichard Henderson {
215fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXSNAN;
21613c9115fSRichard Henderson     finish_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, retaddr);
21713c9115fSRichard Henderson }
21813c9115fSRichard Henderson 
219fcf5ef2aSThomas Huth /* Magnitude subtraction of infinities */
22013c9115fSRichard Henderson static void float_invalid_op_vxisi(CPUPPCState *env, bool set_fpcc,
22113c9115fSRichard Henderson                                    uintptr_t retaddr)
22213c9115fSRichard Henderson {
223fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXISI;
22413c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXISI, set_fpcc, retaddr);
22513c9115fSRichard Henderson }
22613c9115fSRichard Henderson 
227fcf5ef2aSThomas Huth /* Division of infinity by infinity */
22813c9115fSRichard Henderson static void float_invalid_op_vxidi(CPUPPCState *env, bool set_fpcc,
22913c9115fSRichard Henderson                                    uintptr_t retaddr)
23013c9115fSRichard Henderson {
231fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXIDI;
23213c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXIDI, set_fpcc, retaddr);
23313c9115fSRichard Henderson }
23413c9115fSRichard Henderson 
235fcf5ef2aSThomas Huth /* Division of zero by zero */
23613c9115fSRichard Henderson static void float_invalid_op_vxzdz(CPUPPCState *env, bool set_fpcc,
23713c9115fSRichard Henderson                                    uintptr_t retaddr)
23813c9115fSRichard Henderson {
239fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXZDZ;
24013c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXZDZ, set_fpcc, retaddr);
24113c9115fSRichard Henderson }
24213c9115fSRichard Henderson 
243fcf5ef2aSThomas Huth /* Multiplication of zero by infinity */
24413c9115fSRichard Henderson static void float_invalid_op_vximz(CPUPPCState *env, bool set_fpcc,
24513c9115fSRichard Henderson                                    uintptr_t retaddr)
24613c9115fSRichard Henderson {
247fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXIMZ;
24813c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXIMZ, set_fpcc, retaddr);
24913c9115fSRichard Henderson }
25013c9115fSRichard Henderson 
25113c9115fSRichard Henderson /* Square root of a negative number */
25213c9115fSRichard Henderson static void float_invalid_op_vxsqrt(CPUPPCState *env, bool set_fpcc,
25313c9115fSRichard Henderson                                     uintptr_t retaddr)
25413c9115fSRichard Henderson {
25513c9115fSRichard Henderson     env->fpscr |= 1 << FPSCR_VXSQRT;
25613c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXSQRT, set_fpcc, retaddr);
25713c9115fSRichard Henderson }
25813c9115fSRichard Henderson 
259fcf5ef2aSThomas Huth /* Ordered comparison of NaN */
26013c9115fSRichard Henderson static void float_invalid_op_vxvc(CPUPPCState *env, bool set_fpcc,
26113c9115fSRichard Henderson                                   uintptr_t retaddr)
26213c9115fSRichard Henderson {
263fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXVC;
264fcf5ef2aSThomas Huth     if (set_fpcc) {
265fcf5ef2aSThomas Huth         env->fpscr &= ~(0xF << FPSCR_FPCC);
266fcf5ef2aSThomas Huth         env->fpscr |= 0x11 << FPSCR_FPCC;
267fcf5ef2aSThomas Huth     }
26813c9115fSRichard Henderson     /* Update the floating-point invalid operation summary */
26913c9115fSRichard Henderson     env->fpscr |= 1 << FPSCR_VX;
27013c9115fSRichard Henderson     /* Update the floating-point exception summary */
27113c9115fSRichard Henderson     env->fpscr |= FP_FX;
272fcf5ef2aSThomas Huth     /* We must update the target FPR before raising the exception */
27313c9115fSRichard Henderson     if (fpscr_ve != 0) {
274db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
27513c9115fSRichard Henderson 
276fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
277fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
278fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
279fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FEX;
280fcf5ef2aSThomas Huth         /* Exception is differed */
281fcf5ef2aSThomas Huth     }
282fcf5ef2aSThomas Huth }
28313c9115fSRichard Henderson 
284fcf5ef2aSThomas Huth /* Invalid conversion */
28513c9115fSRichard Henderson static void float_invalid_op_vxcvi(CPUPPCState *env, bool set_fpcc,
28613c9115fSRichard Henderson                                    uintptr_t retaddr)
28713c9115fSRichard Henderson {
288fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_VXCVI;
289fcf5ef2aSThomas Huth     env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
29013c9115fSRichard Henderson     if (fpscr_ve == 0) {
291fcf5ef2aSThomas Huth         if (set_fpcc) {
292fcf5ef2aSThomas Huth             env->fpscr &= ~(0xF << FPSCR_FPCC);
293fcf5ef2aSThomas Huth             env->fpscr |= 0x11 << FPSCR_FPCC;
294fcf5ef2aSThomas Huth         }
295fcf5ef2aSThomas Huth     }
29613c9115fSRichard Henderson     finish_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, retaddr);
297fcf5ef2aSThomas Huth }
298fcf5ef2aSThomas Huth 
299fcf5ef2aSThomas Huth static inline void float_zero_divide_excp(CPUPPCState *env, uintptr_t raddr)
300fcf5ef2aSThomas Huth {
301fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_ZX;
302fcf5ef2aSThomas Huth     env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
303fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
304fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
305fcf5ef2aSThomas Huth     if (fpscr_ze != 0) {
306fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
307fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FEX;
308e82c42b7SRichard Henderson         if (fp_exceptions_enabled(env)) {
309fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
310fcf5ef2aSThomas Huth                                    POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX,
311fcf5ef2aSThomas Huth                                    raddr);
312fcf5ef2aSThomas Huth         }
313fcf5ef2aSThomas Huth     }
314fcf5ef2aSThomas Huth }
315fcf5ef2aSThomas Huth 
316fcf5ef2aSThomas Huth static inline void float_overflow_excp(CPUPPCState *env)
317fcf5ef2aSThomas Huth {
318db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_OX;
321fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
322fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
323fcf5ef2aSThomas Huth     if (fpscr_oe != 0) {
324fcf5ef2aSThomas Huth         /* XXX: should adjust the result */
325fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
326fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FEX;
327fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
328fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
329fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
330fcf5ef2aSThomas Huth     } else {
331fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_XX;
332fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FI;
333fcf5ef2aSThomas Huth     }
334fcf5ef2aSThomas Huth }
335fcf5ef2aSThomas Huth 
336fcf5ef2aSThomas Huth static inline void float_underflow_excp(CPUPPCState *env)
337fcf5ef2aSThomas Huth {
338db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_UX;
341fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
342fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
343fcf5ef2aSThomas Huth     if (fpscr_ue != 0) {
344fcf5ef2aSThomas Huth         /* XXX: should adjust the result */
345fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
346fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FEX;
347fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
348fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
349fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
350fcf5ef2aSThomas Huth     }
351fcf5ef2aSThomas Huth }
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth static inline void float_inexact_excp(CPUPPCState *env)
354fcf5ef2aSThomas Huth {
355db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
356fcf5ef2aSThomas Huth 
3579e430ca3SJohn Arbuckle     env->fpscr |= 1 << FPSCR_FI;
358fcf5ef2aSThomas Huth     env->fpscr |= 1 << FPSCR_XX;
359fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
360fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
361fcf5ef2aSThomas Huth     if (fpscr_xe != 0) {
362fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
363fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FEX;
364fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
365fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
366fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
367fcf5ef2aSThomas Huth     }
368fcf5ef2aSThomas Huth }
369fcf5ef2aSThomas Huth 
370fcf5ef2aSThomas Huth static inline void fpscr_set_rounding_mode(CPUPPCState *env)
371fcf5ef2aSThomas Huth {
372fcf5ef2aSThomas Huth     int rnd_type;
373fcf5ef2aSThomas Huth 
374fcf5ef2aSThomas Huth     /* Set rounding mode */
375fcf5ef2aSThomas Huth     switch (fpscr_rn) {
376fcf5ef2aSThomas Huth     case 0:
377fcf5ef2aSThomas Huth         /* Best approximation (round to nearest) */
378fcf5ef2aSThomas Huth         rnd_type = float_round_nearest_even;
379fcf5ef2aSThomas Huth         break;
380fcf5ef2aSThomas Huth     case 1:
381fcf5ef2aSThomas Huth         /* Smaller magnitude (round toward zero) */
382fcf5ef2aSThomas Huth         rnd_type = float_round_to_zero;
383fcf5ef2aSThomas Huth         break;
384fcf5ef2aSThomas Huth     case 2:
385fcf5ef2aSThomas Huth         /* Round toward +infinite */
386fcf5ef2aSThomas Huth         rnd_type = float_round_up;
387fcf5ef2aSThomas Huth         break;
388fcf5ef2aSThomas Huth     default:
389fcf5ef2aSThomas Huth     case 3:
390fcf5ef2aSThomas Huth         /* Round toward -infinite */
391fcf5ef2aSThomas Huth         rnd_type = float_round_down;
392fcf5ef2aSThomas Huth         break;
393fcf5ef2aSThomas Huth     }
394fcf5ef2aSThomas Huth     set_float_rounding_mode(rnd_type, &env->fp_status);
395fcf5ef2aSThomas Huth }
396fcf5ef2aSThomas Huth 
397fcf5ef2aSThomas Huth void helper_fpscr_clrbit(CPUPPCState *env, uint32_t bit)
398fcf5ef2aSThomas Huth {
399fcf5ef2aSThomas Huth     int prev;
400fcf5ef2aSThomas Huth 
401fcf5ef2aSThomas Huth     prev = (env->fpscr >> bit) & 1;
402fcf5ef2aSThomas Huth     env->fpscr &= ~(1 << bit);
403fcf5ef2aSThomas Huth     if (prev == 1) {
404fcf5ef2aSThomas Huth         switch (bit) {
405fcf5ef2aSThomas Huth         case FPSCR_RN1:
40631eb7dddSPaul A. Clarke         case FPSCR_RN0:
407fcf5ef2aSThomas Huth             fpscr_set_rounding_mode(env);
408fcf5ef2aSThomas Huth             break;
40988d8d555SJohn Arbuckle         case FPSCR_VXSNAN:
41088d8d555SJohn Arbuckle         case FPSCR_VXISI:
41188d8d555SJohn Arbuckle         case FPSCR_VXIDI:
41288d8d555SJohn Arbuckle         case FPSCR_VXZDZ:
41388d8d555SJohn Arbuckle         case FPSCR_VXIMZ:
41488d8d555SJohn Arbuckle         case FPSCR_VXVC:
41588d8d555SJohn Arbuckle         case FPSCR_VXSOFT:
41688d8d555SJohn Arbuckle         case FPSCR_VXSQRT:
41788d8d555SJohn Arbuckle         case FPSCR_VXCVI:
41888d8d555SJohn Arbuckle             if (!fpscr_ix) {
41988d8d555SJohn Arbuckle                 /* Set VX bit to zero */
42088d8d555SJohn Arbuckle                 env->fpscr &= ~(1 << FPSCR_VX);
42188d8d555SJohn Arbuckle             }
42288d8d555SJohn Arbuckle             break;
42388d8d555SJohn Arbuckle         case FPSCR_OX:
42488d8d555SJohn Arbuckle         case FPSCR_UX:
42588d8d555SJohn Arbuckle         case FPSCR_ZX:
42688d8d555SJohn Arbuckle         case FPSCR_XX:
42788d8d555SJohn Arbuckle         case FPSCR_VE:
42888d8d555SJohn Arbuckle         case FPSCR_OE:
42988d8d555SJohn Arbuckle         case FPSCR_UE:
43088d8d555SJohn Arbuckle         case FPSCR_ZE:
43188d8d555SJohn Arbuckle         case FPSCR_XE:
43288d8d555SJohn Arbuckle             if (!fpscr_eex) {
43388d8d555SJohn Arbuckle                 /* Set the FEX bit */
43488d8d555SJohn Arbuckle                 env->fpscr &= ~(1 << FPSCR_FEX);
43588d8d555SJohn Arbuckle             }
43688d8d555SJohn Arbuckle             break;
437fcf5ef2aSThomas Huth         default:
438fcf5ef2aSThomas Huth             break;
439fcf5ef2aSThomas Huth         }
440fcf5ef2aSThomas Huth     }
441fcf5ef2aSThomas Huth }
442fcf5ef2aSThomas Huth 
443fcf5ef2aSThomas Huth void helper_fpscr_setbit(CPUPPCState *env, uint32_t bit)
444fcf5ef2aSThomas Huth {
445db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
446fcf5ef2aSThomas Huth     int prev;
447fcf5ef2aSThomas Huth 
448fcf5ef2aSThomas Huth     prev = (env->fpscr >> bit) & 1;
449fcf5ef2aSThomas Huth     env->fpscr |= 1 << bit;
450fcf5ef2aSThomas Huth     if (prev == 0) {
451fcf5ef2aSThomas Huth         switch (bit) {
452fcf5ef2aSThomas Huth         case FPSCR_VX:
453fcf5ef2aSThomas Huth             env->fpscr |= FP_FX;
454fcf5ef2aSThomas Huth             if (fpscr_ve) {
455fcf5ef2aSThomas Huth                 goto raise_ve;
456fcf5ef2aSThomas Huth             }
457fcf5ef2aSThomas Huth             break;
458fcf5ef2aSThomas Huth         case FPSCR_OX:
459fcf5ef2aSThomas Huth             env->fpscr |= FP_FX;
460fcf5ef2aSThomas Huth             if (fpscr_oe) {
461fcf5ef2aSThomas Huth                 goto raise_oe;
462fcf5ef2aSThomas Huth             }
463fcf5ef2aSThomas Huth             break;
464fcf5ef2aSThomas Huth         case FPSCR_UX:
465fcf5ef2aSThomas Huth             env->fpscr |= FP_FX;
466fcf5ef2aSThomas Huth             if (fpscr_ue) {
467fcf5ef2aSThomas Huth                 goto raise_ue;
468fcf5ef2aSThomas Huth             }
469fcf5ef2aSThomas Huth             break;
470fcf5ef2aSThomas Huth         case FPSCR_ZX:
471fcf5ef2aSThomas Huth             env->fpscr |= FP_FX;
472fcf5ef2aSThomas Huth             if (fpscr_ze) {
473fcf5ef2aSThomas Huth                 goto raise_ze;
474fcf5ef2aSThomas Huth             }
475fcf5ef2aSThomas Huth             break;
476fcf5ef2aSThomas Huth         case FPSCR_XX:
477fcf5ef2aSThomas Huth             env->fpscr |= FP_FX;
478fcf5ef2aSThomas Huth             if (fpscr_xe) {
479fcf5ef2aSThomas Huth                 goto raise_xe;
480fcf5ef2aSThomas Huth             }
481fcf5ef2aSThomas Huth             break;
482fcf5ef2aSThomas Huth         case FPSCR_VXSNAN:
483fcf5ef2aSThomas Huth         case FPSCR_VXISI:
484fcf5ef2aSThomas Huth         case FPSCR_VXIDI:
485fcf5ef2aSThomas Huth         case FPSCR_VXZDZ:
486fcf5ef2aSThomas Huth         case FPSCR_VXIMZ:
487fcf5ef2aSThomas Huth         case FPSCR_VXVC:
488fcf5ef2aSThomas Huth         case FPSCR_VXSOFT:
489fcf5ef2aSThomas Huth         case FPSCR_VXSQRT:
490fcf5ef2aSThomas Huth         case FPSCR_VXCVI:
491fcf5ef2aSThomas Huth             env->fpscr |= 1 << FPSCR_VX;
492fcf5ef2aSThomas Huth             env->fpscr |= FP_FX;
493fcf5ef2aSThomas Huth             if (fpscr_ve != 0) {
494fcf5ef2aSThomas Huth                 goto raise_ve;
495fcf5ef2aSThomas Huth             }
496fcf5ef2aSThomas Huth             break;
497fcf5ef2aSThomas Huth         case FPSCR_VE:
498fcf5ef2aSThomas Huth             if (fpscr_vx != 0) {
499fcf5ef2aSThomas Huth             raise_ve:
500fcf5ef2aSThomas Huth                 env->error_code = POWERPC_EXCP_FP;
501fcf5ef2aSThomas Huth                 if (fpscr_vxsnan) {
502fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXSNAN;
503fcf5ef2aSThomas Huth                 }
504fcf5ef2aSThomas Huth                 if (fpscr_vxisi) {
505fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXISI;
506fcf5ef2aSThomas Huth                 }
507fcf5ef2aSThomas Huth                 if (fpscr_vxidi) {
508fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXIDI;
509fcf5ef2aSThomas Huth                 }
510fcf5ef2aSThomas Huth                 if (fpscr_vxzdz) {
511fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXZDZ;
512fcf5ef2aSThomas Huth                 }
513fcf5ef2aSThomas Huth                 if (fpscr_vximz) {
514fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXIMZ;
515fcf5ef2aSThomas Huth                 }
516fcf5ef2aSThomas Huth                 if (fpscr_vxvc) {
517fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXVC;
518fcf5ef2aSThomas Huth                 }
519fcf5ef2aSThomas Huth                 if (fpscr_vxsoft) {
520fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXSOFT;
521fcf5ef2aSThomas Huth                 }
522fcf5ef2aSThomas Huth                 if (fpscr_vxsqrt) {
523fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXSQRT;
524fcf5ef2aSThomas Huth                 }
525fcf5ef2aSThomas Huth                 if (fpscr_vxcvi) {
526fcf5ef2aSThomas Huth                     env->error_code |= POWERPC_EXCP_FP_VXCVI;
527fcf5ef2aSThomas Huth                 }
528fcf5ef2aSThomas Huth                 goto raise_excp;
529fcf5ef2aSThomas Huth             }
530fcf5ef2aSThomas Huth             break;
531fcf5ef2aSThomas Huth         case FPSCR_OE:
532fcf5ef2aSThomas Huth             if (fpscr_ox != 0) {
533fcf5ef2aSThomas Huth             raise_oe:
534fcf5ef2aSThomas Huth                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
535fcf5ef2aSThomas Huth                 goto raise_excp;
536fcf5ef2aSThomas Huth             }
537fcf5ef2aSThomas Huth             break;
538fcf5ef2aSThomas Huth         case FPSCR_UE:
539fcf5ef2aSThomas Huth             if (fpscr_ux != 0) {
540fcf5ef2aSThomas Huth             raise_ue:
541fcf5ef2aSThomas Huth                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
542fcf5ef2aSThomas Huth                 goto raise_excp;
543fcf5ef2aSThomas Huth             }
544fcf5ef2aSThomas Huth             break;
545fcf5ef2aSThomas Huth         case FPSCR_ZE:
546fcf5ef2aSThomas Huth             if (fpscr_zx != 0) {
547fcf5ef2aSThomas Huth             raise_ze:
548fcf5ef2aSThomas Huth                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
549fcf5ef2aSThomas Huth                 goto raise_excp;
550fcf5ef2aSThomas Huth             }
551fcf5ef2aSThomas Huth             break;
552fcf5ef2aSThomas Huth         case FPSCR_XE:
553fcf5ef2aSThomas Huth             if (fpscr_xx != 0) {
554fcf5ef2aSThomas Huth             raise_xe:
555fcf5ef2aSThomas Huth                 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
556fcf5ef2aSThomas Huth                 goto raise_excp;
557fcf5ef2aSThomas Huth             }
558fcf5ef2aSThomas Huth             break;
559fcf5ef2aSThomas Huth         case FPSCR_RN1:
56031eb7dddSPaul A. Clarke         case FPSCR_RN0:
561fcf5ef2aSThomas Huth             fpscr_set_rounding_mode(env);
562fcf5ef2aSThomas Huth             break;
563fcf5ef2aSThomas Huth         default:
564fcf5ef2aSThomas Huth             break;
565fcf5ef2aSThomas Huth         raise_excp:
566fcf5ef2aSThomas Huth             /* Update the floating-point enabled exception summary */
567fcf5ef2aSThomas Huth             env->fpscr |= 1 << FPSCR_FEX;
568fcf5ef2aSThomas Huth             /* We have to update Rc1 before raising the exception */
569fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_PROGRAM;
570fcf5ef2aSThomas Huth             break;
571fcf5ef2aSThomas Huth         }
572fcf5ef2aSThomas Huth     }
573fcf5ef2aSThomas Huth }
574fcf5ef2aSThomas Huth 
575fcf5ef2aSThomas Huth void helper_store_fpscr(CPUPPCState *env, uint64_t arg, uint32_t mask)
576fcf5ef2aSThomas Huth {
577db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
578fcf5ef2aSThomas Huth     target_ulong prev, new;
579fcf5ef2aSThomas Huth     int i;
580fcf5ef2aSThomas Huth 
581fcf5ef2aSThomas Huth     prev = env->fpscr;
582fcf5ef2aSThomas Huth     new = (target_ulong)arg;
583fcf5ef2aSThomas Huth     new &= ~0x60000000LL;
584fcf5ef2aSThomas Huth     new |= prev & 0x60000000LL;
585fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong) * 2; i++) {
586fcf5ef2aSThomas Huth         if (mask & (1 << i)) {
587fcf5ef2aSThomas Huth             env->fpscr &= ~(0xFLL << (4 * i));
588fcf5ef2aSThomas Huth             env->fpscr |= new & (0xFLL << (4 * i));
589fcf5ef2aSThomas Huth         }
590fcf5ef2aSThomas Huth     }
591fcf5ef2aSThomas Huth     /* Update VX and FEX */
592fcf5ef2aSThomas Huth     if (fpscr_ix != 0) {
593fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_VX;
594fcf5ef2aSThomas Huth     } else {
595fcf5ef2aSThomas Huth         env->fpscr &= ~(1 << FPSCR_VX);
596fcf5ef2aSThomas Huth     }
597fcf5ef2aSThomas Huth     if ((fpscr_ex & fpscr_eex) != 0) {
598fcf5ef2aSThomas Huth         env->fpscr |= 1 << FPSCR_FEX;
599fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
600fcf5ef2aSThomas Huth         /* XXX: we should compute it properly */
601fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP;
602fcf5ef2aSThomas Huth     } else {
603fcf5ef2aSThomas Huth         env->fpscr &= ~(1 << FPSCR_FEX);
604fcf5ef2aSThomas Huth     }
605fcf5ef2aSThomas Huth     fpscr_set_rounding_mode(env);
606fcf5ef2aSThomas Huth }
607fcf5ef2aSThomas Huth 
608fcf5ef2aSThomas Huth void store_fpscr(CPUPPCState *env, uint64_t arg, uint32_t mask)
609fcf5ef2aSThomas Huth {
610fcf5ef2aSThomas Huth     helper_store_fpscr(env, arg, mask);
611fcf5ef2aSThomas Huth }
612fcf5ef2aSThomas Huth 
613fcf5ef2aSThomas Huth static void do_float_check_status(CPUPPCState *env, uintptr_t raddr)
614fcf5ef2aSThomas Huth {
615db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
616fcf5ef2aSThomas Huth     int status = get_float_exception_flags(&env->fp_status);
6179e430ca3SJohn Arbuckle     bool inexact_happened = false;
618fcf5ef2aSThomas Huth 
619ae13018dSRichard Henderson     if (status & float_flag_overflow) {
620fcf5ef2aSThomas Huth         float_overflow_excp(env);
621fcf5ef2aSThomas Huth     } else if (status & float_flag_underflow) {
622fcf5ef2aSThomas Huth         float_underflow_excp(env);
623fcf5ef2aSThomas Huth     } else if (status & float_flag_inexact) {
624fcf5ef2aSThomas Huth         float_inexact_excp(env);
6259e430ca3SJohn Arbuckle         inexact_happened = true;
6269e430ca3SJohn Arbuckle     }
6279e430ca3SJohn Arbuckle 
6289e430ca3SJohn Arbuckle     /* if the inexact flag was not set */
6299e430ca3SJohn Arbuckle     if (inexact_happened == false) {
6309e430ca3SJohn Arbuckle         env->fpscr &= ~(1 << FPSCR_FI); /* clear the FPSCR[FI] bit */
631fcf5ef2aSThomas Huth     }
632fcf5ef2aSThomas Huth 
633fcf5ef2aSThomas Huth     if (cs->exception_index == POWERPC_EXCP_PROGRAM &&
634fcf5ef2aSThomas Huth         (env->error_code & POWERPC_EXCP_FP)) {
635fcf5ef2aSThomas Huth         /* Differred floating-point exception after target FPR update */
636e82c42b7SRichard Henderson         if (fp_exceptions_enabled(env)) {
637fcf5ef2aSThomas Huth             raise_exception_err_ra(env, cs->exception_index,
638fcf5ef2aSThomas Huth                                    env->error_code, raddr);
639fcf5ef2aSThomas Huth         }
640fcf5ef2aSThomas Huth     }
641fcf5ef2aSThomas Huth }
642fcf5ef2aSThomas Huth 
643fcf5ef2aSThomas Huth void helper_float_check_status(CPUPPCState *env)
644fcf5ef2aSThomas Huth {
645fcf5ef2aSThomas Huth     do_float_check_status(env, GETPC());
646fcf5ef2aSThomas Huth }
647fcf5ef2aSThomas Huth 
648fcf5ef2aSThomas Huth void helper_reset_fpstatus(CPUPPCState *env)
649fcf5ef2aSThomas Huth {
650fcf5ef2aSThomas Huth     set_float_exception_flags(0, &env->fp_status);
651fcf5ef2aSThomas Huth }
652fcf5ef2aSThomas Huth 
65357483867SRichard Henderson static void float_invalid_op_addsub(CPUPPCState *env, bool set_fpcc,
65457483867SRichard Henderson                                     uintptr_t retaddr, int classes)
65557483867SRichard Henderson {
65657483867SRichard Henderson     if ((classes & ~is_neg) == is_inf) {
65757483867SRichard Henderson         /* Magnitude subtraction of infinities */
65857483867SRichard Henderson         float_invalid_op_vxisi(env, set_fpcc, retaddr);
65957483867SRichard Henderson     } else if (classes & is_snan) {
66057483867SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
66157483867SRichard Henderson     }
66257483867SRichard Henderson }
66357483867SRichard Henderson 
664fcf5ef2aSThomas Huth /* fadd - fadd. */
665ac43cec3SRichard Henderson float64 helper_fadd(CPUPPCState *env, float64 arg1, float64 arg2)
666fcf5ef2aSThomas Huth {
667ac43cec3SRichard Henderson     float64 ret = float64_add(arg1, arg2, &env->fp_status);
668ac43cec3SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
669fcf5ef2aSThomas Huth 
670ac43cec3SRichard Henderson     if (unlikely(status & float_flag_invalid)) {
67157483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
67257483867SRichard Henderson                                 float64_classify(arg1) |
67357483867SRichard Henderson                                 float64_classify(arg2));
674fcf5ef2aSThomas Huth     }
675fcf5ef2aSThomas Huth 
676ac43cec3SRichard Henderson     return ret;
677fcf5ef2aSThomas Huth }
678fcf5ef2aSThomas Huth 
679fcf5ef2aSThomas Huth /* fsub - fsub. */
680ac43cec3SRichard Henderson float64 helper_fsub(CPUPPCState *env, float64 arg1, float64 arg2)
681fcf5ef2aSThomas Huth {
682ac43cec3SRichard Henderson     float64 ret = float64_sub(arg1, arg2, &env->fp_status);
683ac43cec3SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
684fcf5ef2aSThomas Huth 
685ac43cec3SRichard Henderson     if (unlikely(status & float_flag_invalid)) {
68657483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
68757483867SRichard Henderson                                 float64_classify(arg1) |
68857483867SRichard Henderson                                 float64_classify(arg2));
689fcf5ef2aSThomas Huth     }
690fcf5ef2aSThomas Huth 
691ac43cec3SRichard Henderson     return ret;
692fcf5ef2aSThomas Huth }
693fcf5ef2aSThomas Huth 
6944f0da706SRichard Henderson static void float_invalid_op_mul(CPUPPCState *env, bool set_fprc,
6954f0da706SRichard Henderson                                  uintptr_t retaddr, int classes)
6964f0da706SRichard Henderson {
6974f0da706SRichard Henderson     if ((classes & (is_zero | is_inf)) == (is_zero | is_inf)) {
6984f0da706SRichard Henderson         /* Multiplication of zero by infinity */
6994f0da706SRichard Henderson         float_invalid_op_vximz(env, set_fprc, retaddr);
7004f0da706SRichard Henderson     } else if (classes & is_snan) {
7014f0da706SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
7024f0da706SRichard Henderson     }
7034f0da706SRichard Henderson }
7044f0da706SRichard Henderson 
705fcf5ef2aSThomas Huth /* fmul - fmul. */
70679f91633SRichard Henderson float64 helper_fmul(CPUPPCState *env, float64 arg1, float64 arg2)
707fcf5ef2aSThomas Huth {
70879f91633SRichard Henderson     float64 ret = float64_mul(arg1, arg2, &env->fp_status);
70979f91633SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
710fcf5ef2aSThomas Huth 
71179f91633SRichard Henderson     if (unlikely(status & float_flag_invalid)) {
7124f0da706SRichard Henderson         float_invalid_op_mul(env, 1, GETPC(),
7134f0da706SRichard Henderson                              float64_classify(arg1) |
7144f0da706SRichard Henderson                              float64_classify(arg2));
715fcf5ef2aSThomas Huth     }
716fcf5ef2aSThomas Huth 
71779f91633SRichard Henderson     return ret;
718fcf5ef2aSThomas Huth }
719fcf5ef2aSThomas Huth 
720fec59ef3SRichard Henderson static void float_invalid_op_div(CPUPPCState *env, bool set_fprc,
721fec59ef3SRichard Henderson                                  uintptr_t retaddr, int classes)
722fec59ef3SRichard Henderson {
723fec59ef3SRichard Henderson     classes &= ~is_neg;
724fec59ef3SRichard Henderson     if (classes == is_inf) {
725fec59ef3SRichard Henderson         /* Division of infinity by infinity */
726fec59ef3SRichard Henderson         float_invalid_op_vxidi(env, set_fprc, retaddr);
727fec59ef3SRichard Henderson     } else if (classes == is_zero) {
728fec59ef3SRichard Henderson         /* Division of zero by zero */
729fec59ef3SRichard Henderson         float_invalid_op_vxzdz(env, set_fprc, retaddr);
730fec59ef3SRichard Henderson     } else if (classes & is_snan) {
731fec59ef3SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
732fec59ef3SRichard Henderson     }
733fec59ef3SRichard Henderson }
734fec59ef3SRichard Henderson 
735fcf5ef2aSThomas Huth /* fdiv - fdiv. */
736ae13018dSRichard Henderson float64 helper_fdiv(CPUPPCState *env, float64 arg1, float64 arg2)
737fcf5ef2aSThomas Huth {
738ae13018dSRichard Henderson     float64 ret = float64_div(arg1, arg2, &env->fp_status);
739ae13018dSRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
740fcf5ef2aSThomas Huth 
741ae13018dSRichard Henderson     if (unlikely(status)) {
742ae13018dSRichard Henderson         if (status & float_flag_invalid) {
743fec59ef3SRichard Henderson             float_invalid_op_div(env, 1, GETPC(),
744fec59ef3SRichard Henderson                                  float64_classify(arg1) |
745fec59ef3SRichard Henderson                                  float64_classify(arg2));
746ae13018dSRichard Henderson         }
747ae13018dSRichard Henderson         if (status & float_flag_divbyzero) {
748ae13018dSRichard Henderson             float_zero_divide_excp(env, GETPC());
749ae13018dSRichard Henderson         }
750fcf5ef2aSThomas Huth     }
751fcf5ef2aSThomas Huth 
752ae13018dSRichard Henderson     return ret;
753fcf5ef2aSThomas Huth }
754fcf5ef2aSThomas Huth 
755a3dec427SRichard Henderson static void float_invalid_cvt(CPUPPCState *env, bool set_fprc,
756a3dec427SRichard Henderson                               uintptr_t retaddr, int class1)
757a3dec427SRichard Henderson {
758a3dec427SRichard Henderson     float_invalid_op_vxcvi(env, set_fprc, retaddr);
759a3dec427SRichard Henderson     if (class1 & is_snan) {
760a3dec427SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
761a3dec427SRichard Henderson     }
762a3dec427SRichard Henderson }
763fcf5ef2aSThomas Huth 
764fcf5ef2aSThomas Huth #define FPU_FCTI(op, cvt, nanval)                                      \
765a3dec427SRichard Henderson uint64_t helper_##op(CPUPPCState *env, float64 arg)                    \
766fcf5ef2aSThomas Huth {                                                                      \
767a3dec427SRichard Henderson     uint64_t ret = float64_to_##cvt(arg, &env->fp_status);             \
768a3dec427SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);           \
769fcf5ef2aSThomas Huth                                                                        \
770a3dec427SRichard Henderson     if (unlikely(status)) {                                            \
771a3dec427SRichard Henderson         if (status & float_flag_invalid) {                             \
772a3dec427SRichard Henderson             float_invalid_cvt(env, 1, GETPC(), float64_classify(arg)); \
773a3dec427SRichard Henderson             ret = nanval;                                              \
774fcf5ef2aSThomas Huth         }                                                              \
7756525aadcSRichard Henderson         do_float_check_status(env, GETPC());                           \
776fcf5ef2aSThomas Huth     }                                                                  \
777a3dec427SRichard Henderson     return ret;                                                        \
778fcf5ef2aSThomas Huth }
779fcf5ef2aSThomas Huth 
780fcf5ef2aSThomas Huth FPU_FCTI(fctiw, int32, 0x80000000U)
781fcf5ef2aSThomas Huth FPU_FCTI(fctiwz, int32_round_to_zero, 0x80000000U)
782fcf5ef2aSThomas Huth FPU_FCTI(fctiwu, uint32, 0x00000000U)
783fcf5ef2aSThomas Huth FPU_FCTI(fctiwuz, uint32_round_to_zero, 0x00000000U)
784fcf5ef2aSThomas Huth FPU_FCTI(fctid, int64, 0x8000000000000000ULL)
785fcf5ef2aSThomas Huth FPU_FCTI(fctidz, int64_round_to_zero, 0x8000000000000000ULL)
786fcf5ef2aSThomas Huth FPU_FCTI(fctidu, uint64, 0x0000000000000000ULL)
787fcf5ef2aSThomas Huth FPU_FCTI(fctiduz, uint64_round_to_zero, 0x0000000000000000ULL)
788fcf5ef2aSThomas Huth 
789fcf5ef2aSThomas Huth #define FPU_FCFI(op, cvtr, is_single)                      \
790fcf5ef2aSThomas Huth uint64_t helper_##op(CPUPPCState *env, uint64_t arg)       \
791fcf5ef2aSThomas Huth {                                                          \
792fcf5ef2aSThomas Huth     CPU_DoubleU farg;                                      \
793fcf5ef2aSThomas Huth                                                            \
794fcf5ef2aSThomas Huth     if (is_single) {                                       \
795fcf5ef2aSThomas Huth         float32 tmp = cvtr(arg, &env->fp_status);          \
796fcf5ef2aSThomas Huth         farg.d = float32_to_float64(tmp, &env->fp_status); \
797fcf5ef2aSThomas Huth     } else {                                               \
798fcf5ef2aSThomas Huth         farg.d = cvtr(arg, &env->fp_status);               \
799fcf5ef2aSThomas Huth     }                                                      \
8006525aadcSRichard Henderson     do_float_check_status(env, GETPC());                   \
801fcf5ef2aSThomas Huth     return farg.ll;                                        \
802fcf5ef2aSThomas Huth }
803fcf5ef2aSThomas Huth 
804fcf5ef2aSThomas Huth FPU_FCFI(fcfid, int64_to_float64, 0)
805fcf5ef2aSThomas Huth FPU_FCFI(fcfids, int64_to_float32, 1)
806fcf5ef2aSThomas Huth FPU_FCFI(fcfidu, uint64_to_float64, 0)
807fcf5ef2aSThomas Huth FPU_FCFI(fcfidus, uint64_to_float32, 1)
808fcf5ef2aSThomas Huth 
809fcf5ef2aSThomas Huth static inline uint64_t do_fri(CPUPPCState *env, uint64_t arg,
810fcf5ef2aSThomas Huth                               int rounding_mode)
811fcf5ef2aSThomas Huth {
812fcf5ef2aSThomas Huth     CPU_DoubleU farg;
813fcf5ef2aSThomas Huth 
814fcf5ef2aSThomas Huth     farg.ll = arg;
815fcf5ef2aSThomas Huth 
816fcf5ef2aSThomas Huth     if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
817fcf5ef2aSThomas Huth         /* sNaN round */
81813c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
819fcf5ef2aSThomas Huth         farg.ll = arg | 0x0008000000000000ULL;
820fcf5ef2aSThomas Huth     } else {
821fcf5ef2aSThomas Huth         int inexact = get_float_exception_flags(&env->fp_status) &
822fcf5ef2aSThomas Huth                       float_flag_inexact;
823fcf5ef2aSThomas Huth         set_float_rounding_mode(rounding_mode, &env->fp_status);
824fcf5ef2aSThomas Huth         farg.ll = float64_round_to_int(farg.d, &env->fp_status);
825fcf5ef2aSThomas Huth         /* Restore rounding mode from FPSCR */
826fcf5ef2aSThomas Huth         fpscr_set_rounding_mode(env);
827fcf5ef2aSThomas Huth 
828fcf5ef2aSThomas Huth         /* fri* does not set FPSCR[XX] */
829fcf5ef2aSThomas Huth         if (!inexact) {
830fcf5ef2aSThomas Huth             env->fp_status.float_exception_flags &= ~float_flag_inexact;
831fcf5ef2aSThomas Huth         }
832fcf5ef2aSThomas Huth     }
8336525aadcSRichard Henderson     do_float_check_status(env, GETPC());
834fcf5ef2aSThomas Huth     return farg.ll;
835fcf5ef2aSThomas Huth }
836fcf5ef2aSThomas Huth 
837fcf5ef2aSThomas Huth uint64_t helper_frin(CPUPPCState *env, uint64_t arg)
838fcf5ef2aSThomas Huth {
839fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_ties_away);
840fcf5ef2aSThomas Huth }
841fcf5ef2aSThomas Huth 
842fcf5ef2aSThomas Huth uint64_t helper_friz(CPUPPCState *env, uint64_t arg)
843fcf5ef2aSThomas Huth {
844fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_to_zero);
845fcf5ef2aSThomas Huth }
846fcf5ef2aSThomas Huth 
847fcf5ef2aSThomas Huth uint64_t helper_frip(CPUPPCState *env, uint64_t arg)
848fcf5ef2aSThomas Huth {
849fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_up);
850fcf5ef2aSThomas Huth }
851fcf5ef2aSThomas Huth 
852fcf5ef2aSThomas Huth uint64_t helper_frim(CPUPPCState *env, uint64_t arg)
853fcf5ef2aSThomas Huth {
854fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_down);
855fcf5ef2aSThomas Huth }
856fcf5ef2aSThomas Huth 
8573e5b26cfSNikunj A Dadhania #define FPU_MADDSUB_UPDATE(NAME, TP)                                    \
8583e5b26cfSNikunj A Dadhania static void NAME(CPUPPCState *env, TP arg1, TP arg2, TP arg3,           \
85913c9115fSRichard Henderson                  unsigned int madd_flags, uintptr_t retaddr)            \
8603e5b26cfSNikunj A Dadhania {                                                                       \
8613e5b26cfSNikunj A Dadhania     if (TP##_is_signaling_nan(arg1, &env->fp_status) ||                 \
8623e5b26cfSNikunj A Dadhania         TP##_is_signaling_nan(arg2, &env->fp_status) ||                 \
8633e5b26cfSNikunj A Dadhania         TP##_is_signaling_nan(arg3, &env->fp_status)) {                 \
8643e5b26cfSNikunj A Dadhania         /* sNaN operation */                                            \
86513c9115fSRichard Henderson         float_invalid_op_vxsnan(env, retaddr);                          \
8663e5b26cfSNikunj A Dadhania     }                                                                   \
8673e5b26cfSNikunj A Dadhania     if ((TP##_is_infinity(arg1) && TP##_is_zero(arg2)) ||               \
8683e5b26cfSNikunj A Dadhania         (TP##_is_zero(arg1) && TP##_is_infinity(arg2))) {               \
8693e5b26cfSNikunj A Dadhania         /* Multiplication of zero by infinity */                        \
87013c9115fSRichard Henderson         float_invalid_op_vximz(env, 1, retaddr);                        \
8713e5b26cfSNikunj A Dadhania     }                                                                   \
8723e5b26cfSNikunj A Dadhania     if ((TP##_is_infinity(arg1) || TP##_is_infinity(arg2)) &&           \
8733e5b26cfSNikunj A Dadhania         TP##_is_infinity(arg3)) {                                       \
8743e5b26cfSNikunj A Dadhania         uint8_t aSign, bSign, cSign;                                    \
8753e5b26cfSNikunj A Dadhania                                                                         \
8763e5b26cfSNikunj A Dadhania         aSign = TP##_is_neg(arg1);                                      \
8773e5b26cfSNikunj A Dadhania         bSign = TP##_is_neg(arg2);                                      \
8783e5b26cfSNikunj A Dadhania         cSign = TP##_is_neg(arg3);                                      \
8793e5b26cfSNikunj A Dadhania         if (madd_flags & float_muladd_negate_c) {                       \
8803e5b26cfSNikunj A Dadhania             cSign ^= 1;                                                 \
8813e5b26cfSNikunj A Dadhania         }                                                               \
8823e5b26cfSNikunj A Dadhania         if (aSign ^ bSign ^ cSign) {                                    \
88313c9115fSRichard Henderson             float_invalid_op_vxisi(env, 1, retaddr);                    \
8843e5b26cfSNikunj A Dadhania         }                                                               \
8853e5b26cfSNikunj A Dadhania     }                                                                   \
886806c9d71SNikunj A Dadhania }
887182fe2cfSNikunj A Dadhania FPU_MADDSUB_UPDATE(float32_maddsub_update_excp, float32)
8883e5b26cfSNikunj A Dadhania FPU_MADDSUB_UPDATE(float64_maddsub_update_excp, float64)
889fcf5ef2aSThomas Huth 
890992d7e97SNikunj A Dadhania #define FPU_FMADD(op, madd_flags)                                       \
891992d7e97SNikunj A Dadhania uint64_t helper_##op(CPUPPCState *env, uint64_t arg1,                   \
892992d7e97SNikunj A Dadhania                      uint64_t arg2, uint64_t arg3)                      \
893992d7e97SNikunj A Dadhania {                                                                       \
894992d7e97SNikunj A Dadhania     uint32_t flags;                                                     \
895992d7e97SNikunj A Dadhania     float64 ret = float64_muladd(arg1, arg2, arg3, madd_flags,          \
896992d7e97SNikunj A Dadhania                                  &env->fp_status);                      \
897992d7e97SNikunj A Dadhania     flags = get_float_exception_flags(&env->fp_status);                 \
898992d7e97SNikunj A Dadhania     if (flags) {                                                        \
899992d7e97SNikunj A Dadhania         if (flags & float_flag_invalid) {                               \
900992d7e97SNikunj A Dadhania             float64_maddsub_update_excp(env, arg1, arg2, arg3,          \
90113c9115fSRichard Henderson                                         madd_flags, GETPC());           \
902992d7e97SNikunj A Dadhania         }                                                               \
9036525aadcSRichard Henderson         do_float_check_status(env, GETPC());                            \
904992d7e97SNikunj A Dadhania     }                                                                   \
905992d7e97SNikunj A Dadhania     return ret;                                                         \
906fcf5ef2aSThomas Huth }
907fcf5ef2aSThomas Huth 
908992d7e97SNikunj A Dadhania #define MADD_FLGS 0
909992d7e97SNikunj A Dadhania #define MSUB_FLGS float_muladd_negate_c
910992d7e97SNikunj A Dadhania #define NMADD_FLGS float_muladd_negate_result
911992d7e97SNikunj A Dadhania #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
912fcf5ef2aSThomas Huth 
913992d7e97SNikunj A Dadhania FPU_FMADD(fmadd, MADD_FLGS)
914992d7e97SNikunj A Dadhania FPU_FMADD(fnmadd, NMADD_FLGS)
915992d7e97SNikunj A Dadhania FPU_FMADD(fmsub, MSUB_FLGS)
916992d7e97SNikunj A Dadhania FPU_FMADD(fnmsub, NMSUB_FLGS)
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth /* frsp - frsp. */
919fcf5ef2aSThomas Huth uint64_t helper_frsp(CPUPPCState *env, uint64_t arg)
920fcf5ef2aSThomas Huth {
921fcf5ef2aSThomas Huth     CPU_DoubleU farg;
922fcf5ef2aSThomas Huth     float32 f32;
923fcf5ef2aSThomas Huth 
924fcf5ef2aSThomas Huth     farg.ll = arg;
925fcf5ef2aSThomas Huth 
926fcf5ef2aSThomas Huth     if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
92713c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
928fcf5ef2aSThomas Huth     }
929fcf5ef2aSThomas Huth     f32 = float64_to_float32(farg.d, &env->fp_status);
930fcf5ef2aSThomas Huth     farg.d = float32_to_float64(f32, &env->fp_status);
931fcf5ef2aSThomas Huth 
932fcf5ef2aSThomas Huth     return farg.ll;
933fcf5ef2aSThomas Huth }
934fcf5ef2aSThomas Huth 
935fcf5ef2aSThomas Huth /* fsqrt - fsqrt. */
93649ab52efSRichard Henderson float64 helper_fsqrt(CPUPPCState *env, float64 arg)
937fcf5ef2aSThomas Huth {
93849ab52efSRichard Henderson     float64 ret = float64_sqrt(arg, &env->fp_status);
93949ab52efSRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
940fcf5ef2aSThomas Huth 
94149ab52efSRichard Henderson     if (unlikely(status & float_flag_invalid)) {
94249ab52efSRichard Henderson         if (unlikely(float64_is_any_nan(arg))) {
94349ab52efSRichard Henderson             if (unlikely(float64_is_signaling_nan(arg, &env->fp_status))) {
94449ab52efSRichard Henderson                 /* sNaN square root */
94513c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());
946fcf5ef2aSThomas Huth             }
947fcf5ef2aSThomas Huth         } else {
94849ab52efSRichard Henderson             /* Square root of a negative nonzero number */
94913c9115fSRichard Henderson             float_invalid_op_vxsqrt(env, 1, GETPC());
950fcf5ef2aSThomas Huth         }
95149ab52efSRichard Henderson     }
95249ab52efSRichard Henderson 
95349ab52efSRichard Henderson     return ret;
954fcf5ef2aSThomas Huth }
955fcf5ef2aSThomas Huth 
956fcf5ef2aSThomas Huth /* fre - fre. */
95738434717SRichard Henderson float64 helper_fre(CPUPPCState *env, float64 arg)
958fcf5ef2aSThomas Huth {
95938434717SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
96038434717SRichard Henderson     float64 ret = float64_div(float64_one, arg, &env->fp_status);
96138434717SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
962fcf5ef2aSThomas Huth 
96338434717SRichard Henderson     if (unlikely(status)) {
96438434717SRichard Henderson         if (status & float_flag_invalid) {
96538434717SRichard Henderson             if (float64_is_signaling_nan(arg, &env->fp_status)) {
966fcf5ef2aSThomas Huth                 /* sNaN reciprocal */
96713c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());
968fcf5ef2aSThomas Huth             }
96938434717SRichard Henderson         }
97038434717SRichard Henderson         if (status & float_flag_divbyzero) {
97138434717SRichard Henderson             float_zero_divide_excp(env, GETPC());
97238434717SRichard Henderson             /* For FPSCR.ZE == 0, the result is 1/2.  */
97338434717SRichard Henderson             ret = float64_set_sign(float64_half, float64_is_neg(arg));
97438434717SRichard Henderson         }
97538434717SRichard Henderson     }
97638434717SRichard Henderson 
97738434717SRichard Henderson     return ret;
978fcf5ef2aSThomas Huth }
979fcf5ef2aSThomas Huth 
980fcf5ef2aSThomas Huth /* fres - fres. */
981fcf5ef2aSThomas Huth uint64_t helper_fres(CPUPPCState *env, uint64_t arg)
982fcf5ef2aSThomas Huth {
983fcf5ef2aSThomas Huth     CPU_DoubleU farg;
984fcf5ef2aSThomas Huth     float32 f32;
985fcf5ef2aSThomas Huth 
986fcf5ef2aSThomas Huth     farg.ll = arg;
987fcf5ef2aSThomas Huth 
988fcf5ef2aSThomas Huth     if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
989fcf5ef2aSThomas Huth         /* sNaN reciprocal */
99013c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
991fcf5ef2aSThomas Huth     }
992fcf5ef2aSThomas Huth     farg.d = float64_div(float64_one, farg.d, &env->fp_status);
993fcf5ef2aSThomas Huth     f32 = float64_to_float32(farg.d, &env->fp_status);
994fcf5ef2aSThomas Huth     farg.d = float32_to_float64(f32, &env->fp_status);
995fcf5ef2aSThomas Huth 
996fcf5ef2aSThomas Huth     return farg.ll;
997fcf5ef2aSThomas Huth }
998fcf5ef2aSThomas Huth 
999fcf5ef2aSThomas Huth /* frsqrte  - frsqrte. */
100038434717SRichard Henderson float64 helper_frsqrte(CPUPPCState *env, float64 arg)
1001fcf5ef2aSThomas Huth {
100238434717SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
100338434717SRichard Henderson     float64 rets = float64_sqrt(arg, &env->fp_status);
100438434717SRichard Henderson     float64 retd = float64_div(float64_one, rets, &env->fp_status);
100538434717SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
1006fcf5ef2aSThomas Huth 
100738434717SRichard Henderson     if (unlikely(status)) {
100838434717SRichard Henderson         if (status & float_flag_invalid) {
100938434717SRichard Henderson             if (float64_is_signaling_nan(arg, &env->fp_status)) {
101038434717SRichard Henderson                 /* sNaN reciprocal */
101113c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());
1012fcf5ef2aSThomas Huth             } else {
101338434717SRichard Henderson                 /* Square root of a negative nonzero number */
101413c9115fSRichard Henderson                 float_invalid_op_vxsqrt(env, 1, GETPC());
101538434717SRichard Henderson             }
101638434717SRichard Henderson         }
101738434717SRichard Henderson         if (status & float_flag_divbyzero) {
101838434717SRichard Henderson             /* Reciprocal of (square root of) zero.  */
101938434717SRichard Henderson             float_zero_divide_excp(env, GETPC());
102038434717SRichard Henderson         }
1021fcf5ef2aSThomas Huth     }
1022fcf5ef2aSThomas Huth 
102338434717SRichard Henderson     return retd;
1024fcf5ef2aSThomas Huth }
1025fcf5ef2aSThomas Huth 
1026fcf5ef2aSThomas Huth /* fsel - fsel. */
1027fcf5ef2aSThomas Huth uint64_t helper_fsel(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
1028fcf5ef2aSThomas Huth                      uint64_t arg3)
1029fcf5ef2aSThomas Huth {
1030fcf5ef2aSThomas Huth     CPU_DoubleU farg1;
1031fcf5ef2aSThomas Huth 
1032fcf5ef2aSThomas Huth     farg1.ll = arg1;
1033fcf5ef2aSThomas Huth 
1034fcf5ef2aSThomas Huth     if ((!float64_is_neg(farg1.d) || float64_is_zero(farg1.d)) &&
1035fcf5ef2aSThomas Huth         !float64_is_any_nan(farg1.d)) {
1036fcf5ef2aSThomas Huth         return arg2;
1037fcf5ef2aSThomas Huth     } else {
1038fcf5ef2aSThomas Huth         return arg3;
1039fcf5ef2aSThomas Huth     }
1040fcf5ef2aSThomas Huth }
1041fcf5ef2aSThomas Huth 
1042fcf5ef2aSThomas Huth uint32_t helper_ftdiv(uint64_t fra, uint64_t frb)
1043fcf5ef2aSThomas Huth {
1044fcf5ef2aSThomas Huth     int fe_flag = 0;
1045fcf5ef2aSThomas Huth     int fg_flag = 0;
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth     if (unlikely(float64_is_infinity(fra) ||
1048fcf5ef2aSThomas Huth                  float64_is_infinity(frb) ||
1049fcf5ef2aSThomas Huth                  float64_is_zero(frb))) {
1050fcf5ef2aSThomas Huth         fe_flag = 1;
1051fcf5ef2aSThomas Huth         fg_flag = 1;
1052fcf5ef2aSThomas Huth     } else {
1053fcf5ef2aSThomas Huth         int e_a = ppc_float64_get_unbiased_exp(fra);
1054fcf5ef2aSThomas Huth         int e_b = ppc_float64_get_unbiased_exp(frb);
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth         if (unlikely(float64_is_any_nan(fra) ||
1057fcf5ef2aSThomas Huth                      float64_is_any_nan(frb))) {
1058fcf5ef2aSThomas Huth             fe_flag = 1;
1059fcf5ef2aSThomas Huth         } else if ((e_b <= -1022) || (e_b >= 1021)) {
1060fcf5ef2aSThomas Huth             fe_flag = 1;
1061fcf5ef2aSThomas Huth         } else if (!float64_is_zero(fra) &&
1062fcf5ef2aSThomas Huth                    (((e_a - e_b) >= 1023) ||
1063fcf5ef2aSThomas Huth                     ((e_a - e_b) <= -1021) ||
1064fcf5ef2aSThomas Huth                     (e_a <= -970))) {
1065fcf5ef2aSThomas Huth             fe_flag = 1;
1066fcf5ef2aSThomas Huth         }
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth         if (unlikely(float64_is_zero_or_denormal(frb))) {
1069fcf5ef2aSThomas Huth             /* XB is not zero because of the above check and */
1070fcf5ef2aSThomas Huth             /* so must be denormalized.                      */
1071fcf5ef2aSThomas Huth             fg_flag = 1;
1072fcf5ef2aSThomas Huth         }
1073fcf5ef2aSThomas Huth     }
1074fcf5ef2aSThomas Huth 
1075fcf5ef2aSThomas Huth     return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
1076fcf5ef2aSThomas Huth }
1077fcf5ef2aSThomas Huth 
1078fcf5ef2aSThomas Huth uint32_t helper_ftsqrt(uint64_t frb)
1079fcf5ef2aSThomas Huth {
1080fcf5ef2aSThomas Huth     int fe_flag = 0;
1081fcf5ef2aSThomas Huth     int fg_flag = 0;
1082fcf5ef2aSThomas Huth 
1083fcf5ef2aSThomas Huth     if (unlikely(float64_is_infinity(frb) || float64_is_zero(frb))) {
1084fcf5ef2aSThomas Huth         fe_flag = 1;
1085fcf5ef2aSThomas Huth         fg_flag = 1;
1086fcf5ef2aSThomas Huth     } else {
1087fcf5ef2aSThomas Huth         int e_b = ppc_float64_get_unbiased_exp(frb);
1088fcf5ef2aSThomas Huth 
1089fcf5ef2aSThomas Huth         if (unlikely(float64_is_any_nan(frb))) {
1090fcf5ef2aSThomas Huth             fe_flag = 1;
1091fcf5ef2aSThomas Huth         } else if (unlikely(float64_is_zero(frb))) {
1092fcf5ef2aSThomas Huth             fe_flag = 1;
1093fcf5ef2aSThomas Huth         } else if (unlikely(float64_is_neg(frb))) {
1094fcf5ef2aSThomas Huth             fe_flag = 1;
1095fcf5ef2aSThomas Huth         } else if (!float64_is_zero(frb) && (e_b <= (-1022 + 52))) {
1096fcf5ef2aSThomas Huth             fe_flag = 1;
1097fcf5ef2aSThomas Huth         }
1098fcf5ef2aSThomas Huth 
1099fcf5ef2aSThomas Huth         if (unlikely(float64_is_zero_or_denormal(frb))) {
1100fcf5ef2aSThomas Huth             /* XB is not zero because of the above check and */
1101fcf5ef2aSThomas Huth             /* therefore must be denormalized.               */
1102fcf5ef2aSThomas Huth             fg_flag = 1;
1103fcf5ef2aSThomas Huth         }
1104fcf5ef2aSThomas Huth     }
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth     return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
1107fcf5ef2aSThomas Huth }
1108fcf5ef2aSThomas Huth 
1109fcf5ef2aSThomas Huth void helper_fcmpu(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
1110fcf5ef2aSThomas Huth                   uint32_t crfD)
1111fcf5ef2aSThomas Huth {
1112fcf5ef2aSThomas Huth     CPU_DoubleU farg1, farg2;
1113fcf5ef2aSThomas Huth     uint32_t ret = 0;
1114fcf5ef2aSThomas Huth 
1115fcf5ef2aSThomas Huth     farg1.ll = arg1;
1116fcf5ef2aSThomas Huth     farg2.ll = arg2;
1117fcf5ef2aSThomas Huth 
1118fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(farg1.d) ||
1119fcf5ef2aSThomas Huth                  float64_is_any_nan(farg2.d))) {
1120fcf5ef2aSThomas Huth         ret = 0x01UL;
1121fcf5ef2aSThomas Huth     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1122fcf5ef2aSThomas Huth         ret = 0x08UL;
1123fcf5ef2aSThomas Huth     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1124fcf5ef2aSThomas Huth         ret = 0x04UL;
1125fcf5ef2aSThomas Huth     } else {
1126fcf5ef2aSThomas Huth         ret = 0x02UL;
1127fcf5ef2aSThomas Huth     }
1128fcf5ef2aSThomas Huth 
1129fcf5ef2aSThomas Huth     env->fpscr &= ~(0x0F << FPSCR_FPRF);
1130fcf5ef2aSThomas Huth     env->fpscr |= ret << FPSCR_FPRF;
1131fcf5ef2aSThomas Huth     env->crf[crfD] = ret;
1132fcf5ef2aSThomas Huth     if (unlikely(ret == 0x01UL
1133fcf5ef2aSThomas Huth                  && (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
1134fcf5ef2aSThomas Huth                      float64_is_signaling_nan(farg2.d, &env->fp_status)))) {
1135fcf5ef2aSThomas Huth         /* sNaN comparison */
113613c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
1137fcf5ef2aSThomas Huth     }
1138fcf5ef2aSThomas Huth }
1139fcf5ef2aSThomas Huth 
1140fcf5ef2aSThomas Huth void helper_fcmpo(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
1141fcf5ef2aSThomas Huth                   uint32_t crfD)
1142fcf5ef2aSThomas Huth {
1143fcf5ef2aSThomas Huth     CPU_DoubleU farg1, farg2;
1144fcf5ef2aSThomas Huth     uint32_t ret = 0;
1145fcf5ef2aSThomas Huth 
1146fcf5ef2aSThomas Huth     farg1.ll = arg1;
1147fcf5ef2aSThomas Huth     farg2.ll = arg2;
1148fcf5ef2aSThomas Huth 
1149fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(farg1.d) ||
1150fcf5ef2aSThomas Huth                  float64_is_any_nan(farg2.d))) {
1151fcf5ef2aSThomas Huth         ret = 0x01UL;
1152fcf5ef2aSThomas Huth     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1153fcf5ef2aSThomas Huth         ret = 0x08UL;
1154fcf5ef2aSThomas Huth     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1155fcf5ef2aSThomas Huth         ret = 0x04UL;
1156fcf5ef2aSThomas Huth     } else {
1157fcf5ef2aSThomas Huth         ret = 0x02UL;
1158fcf5ef2aSThomas Huth     }
1159fcf5ef2aSThomas Huth 
1160fcf5ef2aSThomas Huth     env->fpscr &= ~(0x0F << FPSCR_FPRF);
1161fcf5ef2aSThomas Huth     env->fpscr |= ret << FPSCR_FPRF;
1162fcf5ef2aSThomas Huth     env->crf[crfD] = ret;
1163fcf5ef2aSThomas Huth     if (unlikely(ret == 0x01UL)) {
116413c9115fSRichard Henderson         float_invalid_op_vxvc(env, 1, GETPC());
1165fcf5ef2aSThomas Huth         if (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
1166fcf5ef2aSThomas Huth             float64_is_signaling_nan(farg2.d, &env->fp_status)) {
1167fcf5ef2aSThomas Huth             /* sNaN comparison */
116813c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
1169fcf5ef2aSThomas Huth         }
1170fcf5ef2aSThomas Huth     }
1171fcf5ef2aSThomas Huth }
1172fcf5ef2aSThomas Huth 
1173fcf5ef2aSThomas Huth /* Single-precision floating-point conversions */
1174fcf5ef2aSThomas Huth static inline uint32_t efscfsi(CPUPPCState *env, uint32_t val)
1175fcf5ef2aSThomas Huth {
1176fcf5ef2aSThomas Huth     CPU_FloatU u;
1177fcf5ef2aSThomas Huth 
1178fcf5ef2aSThomas Huth     u.f = int32_to_float32(val, &env->vec_status);
1179fcf5ef2aSThomas Huth 
1180fcf5ef2aSThomas Huth     return u.l;
1181fcf5ef2aSThomas Huth }
1182fcf5ef2aSThomas Huth 
1183fcf5ef2aSThomas Huth static inline uint32_t efscfui(CPUPPCState *env, uint32_t val)
1184fcf5ef2aSThomas Huth {
1185fcf5ef2aSThomas Huth     CPU_FloatU u;
1186fcf5ef2aSThomas Huth 
1187fcf5ef2aSThomas Huth     u.f = uint32_to_float32(val, &env->vec_status);
1188fcf5ef2aSThomas Huth 
1189fcf5ef2aSThomas Huth     return u.l;
1190fcf5ef2aSThomas Huth }
1191fcf5ef2aSThomas Huth 
1192fcf5ef2aSThomas Huth static inline int32_t efsctsi(CPUPPCState *env, uint32_t val)
1193fcf5ef2aSThomas Huth {
1194fcf5ef2aSThomas Huth     CPU_FloatU u;
1195fcf5ef2aSThomas Huth 
1196fcf5ef2aSThomas Huth     u.l = val;
1197fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1198fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1199fcf5ef2aSThomas Huth         return 0;
1200fcf5ef2aSThomas Huth     }
1201fcf5ef2aSThomas Huth 
1202fcf5ef2aSThomas Huth     return float32_to_int32(u.f, &env->vec_status);
1203fcf5ef2aSThomas Huth }
1204fcf5ef2aSThomas Huth 
1205fcf5ef2aSThomas Huth static inline uint32_t efsctui(CPUPPCState *env, uint32_t val)
1206fcf5ef2aSThomas Huth {
1207fcf5ef2aSThomas Huth     CPU_FloatU u;
1208fcf5ef2aSThomas Huth 
1209fcf5ef2aSThomas Huth     u.l = val;
1210fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1211fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1212fcf5ef2aSThomas Huth         return 0;
1213fcf5ef2aSThomas Huth     }
1214fcf5ef2aSThomas Huth 
1215fcf5ef2aSThomas Huth     return float32_to_uint32(u.f, &env->vec_status);
1216fcf5ef2aSThomas Huth }
1217fcf5ef2aSThomas Huth 
1218fcf5ef2aSThomas Huth static inline uint32_t efsctsiz(CPUPPCState *env, uint32_t val)
1219fcf5ef2aSThomas Huth {
1220fcf5ef2aSThomas Huth     CPU_FloatU u;
1221fcf5ef2aSThomas Huth 
1222fcf5ef2aSThomas Huth     u.l = val;
1223fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1224fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1225fcf5ef2aSThomas Huth         return 0;
1226fcf5ef2aSThomas Huth     }
1227fcf5ef2aSThomas Huth 
1228fcf5ef2aSThomas Huth     return float32_to_int32_round_to_zero(u.f, &env->vec_status);
1229fcf5ef2aSThomas Huth }
1230fcf5ef2aSThomas Huth 
1231fcf5ef2aSThomas Huth static inline uint32_t efsctuiz(CPUPPCState *env, uint32_t val)
1232fcf5ef2aSThomas Huth {
1233fcf5ef2aSThomas Huth     CPU_FloatU u;
1234fcf5ef2aSThomas Huth 
1235fcf5ef2aSThomas Huth     u.l = val;
1236fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1237fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1238fcf5ef2aSThomas Huth         return 0;
1239fcf5ef2aSThomas Huth     }
1240fcf5ef2aSThomas Huth 
1241fcf5ef2aSThomas Huth     return float32_to_uint32_round_to_zero(u.f, &env->vec_status);
1242fcf5ef2aSThomas Huth }
1243fcf5ef2aSThomas Huth 
1244fcf5ef2aSThomas Huth static inline uint32_t efscfsf(CPUPPCState *env, uint32_t val)
1245fcf5ef2aSThomas Huth {
1246fcf5ef2aSThomas Huth     CPU_FloatU u;
1247fcf5ef2aSThomas Huth     float32 tmp;
1248fcf5ef2aSThomas Huth 
1249fcf5ef2aSThomas Huth     u.f = int32_to_float32(val, &env->vec_status);
1250fcf5ef2aSThomas Huth     tmp = int64_to_float32(1ULL << 32, &env->vec_status);
1251fcf5ef2aSThomas Huth     u.f = float32_div(u.f, tmp, &env->vec_status);
1252fcf5ef2aSThomas Huth 
1253fcf5ef2aSThomas Huth     return u.l;
1254fcf5ef2aSThomas Huth }
1255fcf5ef2aSThomas Huth 
1256fcf5ef2aSThomas Huth static inline uint32_t efscfuf(CPUPPCState *env, uint32_t val)
1257fcf5ef2aSThomas Huth {
1258fcf5ef2aSThomas Huth     CPU_FloatU u;
1259fcf5ef2aSThomas Huth     float32 tmp;
1260fcf5ef2aSThomas Huth 
1261fcf5ef2aSThomas Huth     u.f = uint32_to_float32(val, &env->vec_status);
1262fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1263fcf5ef2aSThomas Huth     u.f = float32_div(u.f, tmp, &env->vec_status);
1264fcf5ef2aSThomas Huth 
1265fcf5ef2aSThomas Huth     return u.l;
1266fcf5ef2aSThomas Huth }
1267fcf5ef2aSThomas Huth 
1268fcf5ef2aSThomas Huth static inline uint32_t efsctsf(CPUPPCState *env, uint32_t val)
1269fcf5ef2aSThomas Huth {
1270fcf5ef2aSThomas Huth     CPU_FloatU u;
1271fcf5ef2aSThomas Huth     float32 tmp;
1272fcf5ef2aSThomas Huth 
1273fcf5ef2aSThomas Huth     u.l = val;
1274fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1275fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1276fcf5ef2aSThomas Huth         return 0;
1277fcf5ef2aSThomas Huth     }
1278fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1279fcf5ef2aSThomas Huth     u.f = float32_mul(u.f, tmp, &env->vec_status);
1280fcf5ef2aSThomas Huth 
1281fcf5ef2aSThomas Huth     return float32_to_int32(u.f, &env->vec_status);
1282fcf5ef2aSThomas Huth }
1283fcf5ef2aSThomas Huth 
1284fcf5ef2aSThomas Huth static inline uint32_t efsctuf(CPUPPCState *env, uint32_t val)
1285fcf5ef2aSThomas Huth {
1286fcf5ef2aSThomas Huth     CPU_FloatU u;
1287fcf5ef2aSThomas Huth     float32 tmp;
1288fcf5ef2aSThomas Huth 
1289fcf5ef2aSThomas Huth     u.l = val;
1290fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1291fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1292fcf5ef2aSThomas Huth         return 0;
1293fcf5ef2aSThomas Huth     }
1294fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1295fcf5ef2aSThomas Huth     u.f = float32_mul(u.f, tmp, &env->vec_status);
1296fcf5ef2aSThomas Huth 
1297fcf5ef2aSThomas Huth     return float32_to_uint32(u.f, &env->vec_status);
1298fcf5ef2aSThomas Huth }
1299fcf5ef2aSThomas Huth 
1300fcf5ef2aSThomas Huth #define HELPER_SPE_SINGLE_CONV(name)                              \
1301fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t val)       \
1302fcf5ef2aSThomas Huth     {                                                             \
1303fcf5ef2aSThomas Huth         return e##name(env, val);                                 \
1304fcf5ef2aSThomas Huth     }
1305fcf5ef2aSThomas Huth /* efscfsi */
1306fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfsi);
1307fcf5ef2aSThomas Huth /* efscfui */
1308fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfui);
1309fcf5ef2aSThomas Huth /* efscfuf */
1310fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfuf);
1311fcf5ef2aSThomas Huth /* efscfsf */
1312fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfsf);
1313fcf5ef2aSThomas Huth /* efsctsi */
1314fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsi);
1315fcf5ef2aSThomas Huth /* efsctui */
1316fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctui);
1317fcf5ef2aSThomas Huth /* efsctsiz */
1318fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsiz);
1319fcf5ef2aSThomas Huth /* efsctuiz */
1320fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctuiz);
1321fcf5ef2aSThomas Huth /* efsctsf */
1322fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsf);
1323fcf5ef2aSThomas Huth /* efsctuf */
1324fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctuf);
1325fcf5ef2aSThomas Huth 
1326fcf5ef2aSThomas Huth #define HELPER_SPE_VECTOR_CONV(name)                            \
1327fcf5ef2aSThomas Huth     uint64_t helper_ev##name(CPUPPCState *env, uint64_t val)    \
1328fcf5ef2aSThomas Huth     {                                                           \
1329fcf5ef2aSThomas Huth         return ((uint64_t)e##name(env, val >> 32) << 32) |      \
1330fcf5ef2aSThomas Huth             (uint64_t)e##name(env, val);                        \
1331fcf5ef2aSThomas Huth     }
1332fcf5ef2aSThomas Huth /* evfscfsi */
1333fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfsi);
1334fcf5ef2aSThomas Huth /* evfscfui */
1335fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfui);
1336fcf5ef2aSThomas Huth /* evfscfuf */
1337fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfuf);
1338fcf5ef2aSThomas Huth /* evfscfsf */
1339fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfsf);
1340fcf5ef2aSThomas Huth /* evfsctsi */
1341fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsi);
1342fcf5ef2aSThomas Huth /* evfsctui */
1343fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctui);
1344fcf5ef2aSThomas Huth /* evfsctsiz */
1345fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsiz);
1346fcf5ef2aSThomas Huth /* evfsctuiz */
1347fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctuiz);
1348fcf5ef2aSThomas Huth /* evfsctsf */
1349fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsf);
1350fcf5ef2aSThomas Huth /* evfsctuf */
1351fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctuf);
1352fcf5ef2aSThomas Huth 
1353fcf5ef2aSThomas Huth /* Single-precision floating-point arithmetic */
1354fcf5ef2aSThomas Huth static inline uint32_t efsadd(CPUPPCState *env, uint32_t op1, uint32_t op2)
1355fcf5ef2aSThomas Huth {
1356fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth     u1.l = op1;
1359fcf5ef2aSThomas Huth     u2.l = op2;
1360fcf5ef2aSThomas Huth     u1.f = float32_add(u1.f, u2.f, &env->vec_status);
1361fcf5ef2aSThomas Huth     return u1.l;
1362fcf5ef2aSThomas Huth }
1363fcf5ef2aSThomas Huth 
1364fcf5ef2aSThomas Huth static inline uint32_t efssub(CPUPPCState *env, uint32_t op1, uint32_t op2)
1365fcf5ef2aSThomas Huth {
1366fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1367fcf5ef2aSThomas Huth 
1368fcf5ef2aSThomas Huth     u1.l = op1;
1369fcf5ef2aSThomas Huth     u2.l = op2;
1370fcf5ef2aSThomas Huth     u1.f = float32_sub(u1.f, u2.f, &env->vec_status);
1371fcf5ef2aSThomas Huth     return u1.l;
1372fcf5ef2aSThomas Huth }
1373fcf5ef2aSThomas Huth 
1374fcf5ef2aSThomas Huth static inline uint32_t efsmul(CPUPPCState *env, uint32_t op1, uint32_t op2)
1375fcf5ef2aSThomas Huth {
1376fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1377fcf5ef2aSThomas Huth 
1378fcf5ef2aSThomas Huth     u1.l = op1;
1379fcf5ef2aSThomas Huth     u2.l = op2;
1380fcf5ef2aSThomas Huth     u1.f = float32_mul(u1.f, u2.f, &env->vec_status);
1381fcf5ef2aSThomas Huth     return u1.l;
1382fcf5ef2aSThomas Huth }
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth static inline uint32_t efsdiv(CPUPPCState *env, uint32_t op1, uint32_t op2)
1385fcf5ef2aSThomas Huth {
1386fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1387fcf5ef2aSThomas Huth 
1388fcf5ef2aSThomas Huth     u1.l = op1;
1389fcf5ef2aSThomas Huth     u2.l = op2;
1390fcf5ef2aSThomas Huth     u1.f = float32_div(u1.f, u2.f, &env->vec_status);
1391fcf5ef2aSThomas Huth     return u1.l;
1392fcf5ef2aSThomas Huth }
1393fcf5ef2aSThomas Huth 
1394fcf5ef2aSThomas Huth #define HELPER_SPE_SINGLE_ARITH(name)                                   \
1395fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1396fcf5ef2aSThomas Huth     {                                                                   \
1397fcf5ef2aSThomas Huth         return e##name(env, op1, op2);                                  \
1398fcf5ef2aSThomas Huth     }
1399fcf5ef2aSThomas Huth /* efsadd */
1400fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsadd);
1401fcf5ef2aSThomas Huth /* efssub */
1402fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fssub);
1403fcf5ef2aSThomas Huth /* efsmul */
1404fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsmul);
1405fcf5ef2aSThomas Huth /* efsdiv */
1406fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsdiv);
1407fcf5ef2aSThomas Huth 
1408fcf5ef2aSThomas Huth #define HELPER_SPE_VECTOR_ARITH(name)                                   \
1409fcf5ef2aSThomas Huth     uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1410fcf5ef2aSThomas Huth     {                                                                   \
1411fcf5ef2aSThomas Huth         return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) |   \
1412fcf5ef2aSThomas Huth             (uint64_t)e##name(env, op1, op2);                           \
1413fcf5ef2aSThomas Huth     }
1414fcf5ef2aSThomas Huth /* evfsadd */
1415fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsadd);
1416fcf5ef2aSThomas Huth /* evfssub */
1417fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fssub);
1418fcf5ef2aSThomas Huth /* evfsmul */
1419fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsmul);
1420fcf5ef2aSThomas Huth /* evfsdiv */
1421fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsdiv);
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth /* Single-precision floating-point comparisons */
1424fcf5ef2aSThomas Huth static inline uint32_t efscmplt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1425fcf5ef2aSThomas Huth {
1426fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1427fcf5ef2aSThomas Huth 
1428fcf5ef2aSThomas Huth     u1.l = op1;
1429fcf5ef2aSThomas Huth     u2.l = op2;
1430fcf5ef2aSThomas Huth     return float32_lt(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1431fcf5ef2aSThomas Huth }
1432fcf5ef2aSThomas Huth 
1433fcf5ef2aSThomas Huth static inline uint32_t efscmpgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1434fcf5ef2aSThomas Huth {
1435fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1436fcf5ef2aSThomas Huth 
1437fcf5ef2aSThomas Huth     u1.l = op1;
1438fcf5ef2aSThomas Huth     u2.l = op2;
1439fcf5ef2aSThomas Huth     return float32_le(u1.f, u2.f, &env->vec_status) ? 0 : 4;
1440fcf5ef2aSThomas Huth }
1441fcf5ef2aSThomas Huth 
1442fcf5ef2aSThomas Huth static inline uint32_t efscmpeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1443fcf5ef2aSThomas Huth {
1444fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1445fcf5ef2aSThomas Huth 
1446fcf5ef2aSThomas Huth     u1.l = op1;
1447fcf5ef2aSThomas Huth     u2.l = op2;
1448fcf5ef2aSThomas Huth     return float32_eq(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1449fcf5ef2aSThomas Huth }
1450fcf5ef2aSThomas Huth 
1451fcf5ef2aSThomas Huth static inline uint32_t efststlt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1452fcf5ef2aSThomas Huth {
1453fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1454fcf5ef2aSThomas Huth     return efscmplt(env, op1, op2);
1455fcf5ef2aSThomas Huth }
1456fcf5ef2aSThomas Huth 
1457fcf5ef2aSThomas Huth static inline uint32_t efststgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1458fcf5ef2aSThomas Huth {
1459fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1460fcf5ef2aSThomas Huth     return efscmpgt(env, op1, op2);
1461fcf5ef2aSThomas Huth }
1462fcf5ef2aSThomas Huth 
1463fcf5ef2aSThomas Huth static inline uint32_t efststeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1464fcf5ef2aSThomas Huth {
1465fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1466fcf5ef2aSThomas Huth     return efscmpeq(env, op1, op2);
1467fcf5ef2aSThomas Huth }
1468fcf5ef2aSThomas Huth 
1469fcf5ef2aSThomas Huth #define HELPER_SINGLE_SPE_CMP(name)                                     \
1470fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1471fcf5ef2aSThomas Huth     {                                                                   \
1472fcf5ef2aSThomas Huth         return e##name(env, op1, op2);                                  \
1473fcf5ef2aSThomas Huth     }
1474fcf5ef2aSThomas Huth /* efststlt */
1475fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststlt);
1476fcf5ef2aSThomas Huth /* efststgt */
1477fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststgt);
1478fcf5ef2aSThomas Huth /* efststeq */
1479fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststeq);
1480fcf5ef2aSThomas Huth /* efscmplt */
1481fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmplt);
1482fcf5ef2aSThomas Huth /* efscmpgt */
1483fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmpgt);
1484fcf5ef2aSThomas Huth /* efscmpeq */
1485fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmpeq);
1486fcf5ef2aSThomas Huth 
1487fcf5ef2aSThomas Huth static inline uint32_t evcmp_merge(int t0, int t1)
1488fcf5ef2aSThomas Huth {
1489fcf5ef2aSThomas Huth     return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
1490fcf5ef2aSThomas Huth }
1491fcf5ef2aSThomas Huth 
1492fcf5ef2aSThomas Huth #define HELPER_VECTOR_SPE_CMP(name)                                     \
1493fcf5ef2aSThomas Huth     uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1494fcf5ef2aSThomas Huth     {                                                                   \
1495fcf5ef2aSThomas Huth         return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32),          \
1496fcf5ef2aSThomas Huth                            e##name(env, op1, op2));                     \
1497fcf5ef2aSThomas Huth     }
1498fcf5ef2aSThomas Huth /* evfststlt */
1499fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststlt);
1500fcf5ef2aSThomas Huth /* evfststgt */
1501fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststgt);
1502fcf5ef2aSThomas Huth /* evfststeq */
1503fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststeq);
1504fcf5ef2aSThomas Huth /* evfscmplt */
1505fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmplt);
1506fcf5ef2aSThomas Huth /* evfscmpgt */
1507fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmpgt);
1508fcf5ef2aSThomas Huth /* evfscmpeq */
1509fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmpeq);
1510fcf5ef2aSThomas Huth 
1511fcf5ef2aSThomas Huth /* Double-precision floating-point conversion */
1512fcf5ef2aSThomas Huth uint64_t helper_efdcfsi(CPUPPCState *env, uint32_t val)
1513fcf5ef2aSThomas Huth {
1514fcf5ef2aSThomas Huth     CPU_DoubleU u;
1515fcf5ef2aSThomas Huth 
1516fcf5ef2aSThomas Huth     u.d = int32_to_float64(val, &env->vec_status);
1517fcf5ef2aSThomas Huth 
1518fcf5ef2aSThomas Huth     return u.ll;
1519fcf5ef2aSThomas Huth }
1520fcf5ef2aSThomas Huth 
1521fcf5ef2aSThomas Huth uint64_t helper_efdcfsid(CPUPPCState *env, uint64_t val)
1522fcf5ef2aSThomas Huth {
1523fcf5ef2aSThomas Huth     CPU_DoubleU u;
1524fcf5ef2aSThomas Huth 
1525fcf5ef2aSThomas Huth     u.d = int64_to_float64(val, &env->vec_status);
1526fcf5ef2aSThomas Huth 
1527fcf5ef2aSThomas Huth     return u.ll;
1528fcf5ef2aSThomas Huth }
1529fcf5ef2aSThomas Huth 
1530fcf5ef2aSThomas Huth uint64_t helper_efdcfui(CPUPPCState *env, uint32_t val)
1531fcf5ef2aSThomas Huth {
1532fcf5ef2aSThomas Huth     CPU_DoubleU u;
1533fcf5ef2aSThomas Huth 
1534fcf5ef2aSThomas Huth     u.d = uint32_to_float64(val, &env->vec_status);
1535fcf5ef2aSThomas Huth 
1536fcf5ef2aSThomas Huth     return u.ll;
1537fcf5ef2aSThomas Huth }
1538fcf5ef2aSThomas Huth 
1539fcf5ef2aSThomas Huth uint64_t helper_efdcfuid(CPUPPCState *env, uint64_t val)
1540fcf5ef2aSThomas Huth {
1541fcf5ef2aSThomas Huth     CPU_DoubleU u;
1542fcf5ef2aSThomas Huth 
1543fcf5ef2aSThomas Huth     u.d = uint64_to_float64(val, &env->vec_status);
1544fcf5ef2aSThomas Huth 
1545fcf5ef2aSThomas Huth     return u.ll;
1546fcf5ef2aSThomas Huth }
1547fcf5ef2aSThomas Huth 
1548fcf5ef2aSThomas Huth uint32_t helper_efdctsi(CPUPPCState *env, uint64_t val)
1549fcf5ef2aSThomas Huth {
1550fcf5ef2aSThomas Huth     CPU_DoubleU u;
1551fcf5ef2aSThomas Huth 
1552fcf5ef2aSThomas Huth     u.ll = val;
1553fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1554fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1555fcf5ef2aSThomas Huth         return 0;
1556fcf5ef2aSThomas Huth     }
1557fcf5ef2aSThomas Huth 
1558fcf5ef2aSThomas Huth     return float64_to_int32(u.d, &env->vec_status);
1559fcf5ef2aSThomas Huth }
1560fcf5ef2aSThomas Huth 
1561fcf5ef2aSThomas Huth uint32_t helper_efdctui(CPUPPCState *env, uint64_t val)
1562fcf5ef2aSThomas Huth {
1563fcf5ef2aSThomas Huth     CPU_DoubleU u;
1564fcf5ef2aSThomas Huth 
1565fcf5ef2aSThomas Huth     u.ll = val;
1566fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1567fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1568fcf5ef2aSThomas Huth         return 0;
1569fcf5ef2aSThomas Huth     }
1570fcf5ef2aSThomas Huth 
1571fcf5ef2aSThomas Huth     return float64_to_uint32(u.d, &env->vec_status);
1572fcf5ef2aSThomas Huth }
1573fcf5ef2aSThomas Huth 
1574fcf5ef2aSThomas Huth uint32_t helper_efdctsiz(CPUPPCState *env, uint64_t val)
1575fcf5ef2aSThomas Huth {
1576fcf5ef2aSThomas Huth     CPU_DoubleU u;
1577fcf5ef2aSThomas Huth 
1578fcf5ef2aSThomas Huth     u.ll = val;
1579fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1580fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1581fcf5ef2aSThomas Huth         return 0;
1582fcf5ef2aSThomas Huth     }
1583fcf5ef2aSThomas Huth 
1584fcf5ef2aSThomas Huth     return float64_to_int32_round_to_zero(u.d, &env->vec_status);
1585fcf5ef2aSThomas Huth }
1586fcf5ef2aSThomas Huth 
1587fcf5ef2aSThomas Huth uint64_t helper_efdctsidz(CPUPPCState *env, uint64_t val)
1588fcf5ef2aSThomas Huth {
1589fcf5ef2aSThomas Huth     CPU_DoubleU u;
1590fcf5ef2aSThomas Huth 
1591fcf5ef2aSThomas Huth     u.ll = val;
1592fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1593fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1594fcf5ef2aSThomas Huth         return 0;
1595fcf5ef2aSThomas Huth     }
1596fcf5ef2aSThomas Huth 
1597fcf5ef2aSThomas Huth     return float64_to_int64_round_to_zero(u.d, &env->vec_status);
1598fcf5ef2aSThomas Huth }
1599fcf5ef2aSThomas Huth 
1600fcf5ef2aSThomas Huth uint32_t helper_efdctuiz(CPUPPCState *env, uint64_t val)
1601fcf5ef2aSThomas Huth {
1602fcf5ef2aSThomas Huth     CPU_DoubleU u;
1603fcf5ef2aSThomas Huth 
1604fcf5ef2aSThomas Huth     u.ll = val;
1605fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1606fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1607fcf5ef2aSThomas Huth         return 0;
1608fcf5ef2aSThomas Huth     }
1609fcf5ef2aSThomas Huth 
1610fcf5ef2aSThomas Huth     return float64_to_uint32_round_to_zero(u.d, &env->vec_status);
1611fcf5ef2aSThomas Huth }
1612fcf5ef2aSThomas Huth 
1613fcf5ef2aSThomas Huth uint64_t helper_efdctuidz(CPUPPCState *env, uint64_t val)
1614fcf5ef2aSThomas Huth {
1615fcf5ef2aSThomas Huth     CPU_DoubleU u;
1616fcf5ef2aSThomas Huth 
1617fcf5ef2aSThomas Huth     u.ll = val;
1618fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1619fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1620fcf5ef2aSThomas Huth         return 0;
1621fcf5ef2aSThomas Huth     }
1622fcf5ef2aSThomas Huth 
1623fcf5ef2aSThomas Huth     return float64_to_uint64_round_to_zero(u.d, &env->vec_status);
1624fcf5ef2aSThomas Huth }
1625fcf5ef2aSThomas Huth 
1626fcf5ef2aSThomas Huth uint64_t helper_efdcfsf(CPUPPCState *env, uint32_t val)
1627fcf5ef2aSThomas Huth {
1628fcf5ef2aSThomas Huth     CPU_DoubleU u;
1629fcf5ef2aSThomas Huth     float64 tmp;
1630fcf5ef2aSThomas Huth 
1631fcf5ef2aSThomas Huth     u.d = int32_to_float64(val, &env->vec_status);
1632fcf5ef2aSThomas Huth     tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1633fcf5ef2aSThomas Huth     u.d = float64_div(u.d, tmp, &env->vec_status);
1634fcf5ef2aSThomas Huth 
1635fcf5ef2aSThomas Huth     return u.ll;
1636fcf5ef2aSThomas Huth }
1637fcf5ef2aSThomas Huth 
1638fcf5ef2aSThomas Huth uint64_t helper_efdcfuf(CPUPPCState *env, uint32_t val)
1639fcf5ef2aSThomas Huth {
1640fcf5ef2aSThomas Huth     CPU_DoubleU u;
1641fcf5ef2aSThomas Huth     float64 tmp;
1642fcf5ef2aSThomas Huth 
1643fcf5ef2aSThomas Huth     u.d = uint32_to_float64(val, &env->vec_status);
1644fcf5ef2aSThomas Huth     tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1645fcf5ef2aSThomas Huth     u.d = float64_div(u.d, tmp, &env->vec_status);
1646fcf5ef2aSThomas Huth 
1647fcf5ef2aSThomas Huth     return u.ll;
1648fcf5ef2aSThomas Huth }
1649fcf5ef2aSThomas Huth 
1650fcf5ef2aSThomas Huth uint32_t helper_efdctsf(CPUPPCState *env, uint64_t val)
1651fcf5ef2aSThomas Huth {
1652fcf5ef2aSThomas Huth     CPU_DoubleU u;
1653fcf5ef2aSThomas Huth     float64 tmp;
1654fcf5ef2aSThomas Huth 
1655fcf5ef2aSThomas Huth     u.ll = val;
1656fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1657fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1658fcf5ef2aSThomas Huth         return 0;
1659fcf5ef2aSThomas Huth     }
1660fcf5ef2aSThomas Huth     tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1661fcf5ef2aSThomas Huth     u.d = float64_mul(u.d, tmp, &env->vec_status);
1662fcf5ef2aSThomas Huth 
1663fcf5ef2aSThomas Huth     return float64_to_int32(u.d, &env->vec_status);
1664fcf5ef2aSThomas Huth }
1665fcf5ef2aSThomas Huth 
1666fcf5ef2aSThomas Huth uint32_t helper_efdctuf(CPUPPCState *env, uint64_t val)
1667fcf5ef2aSThomas Huth {
1668fcf5ef2aSThomas Huth     CPU_DoubleU u;
1669fcf5ef2aSThomas Huth     float64 tmp;
1670fcf5ef2aSThomas Huth 
1671fcf5ef2aSThomas Huth     u.ll = val;
1672fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1673fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1674fcf5ef2aSThomas Huth         return 0;
1675fcf5ef2aSThomas Huth     }
1676fcf5ef2aSThomas Huth     tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1677fcf5ef2aSThomas Huth     u.d = float64_mul(u.d, tmp, &env->vec_status);
1678fcf5ef2aSThomas Huth 
1679fcf5ef2aSThomas Huth     return float64_to_uint32(u.d, &env->vec_status);
1680fcf5ef2aSThomas Huth }
1681fcf5ef2aSThomas Huth 
1682fcf5ef2aSThomas Huth uint32_t helper_efscfd(CPUPPCState *env, uint64_t val)
1683fcf5ef2aSThomas Huth {
1684fcf5ef2aSThomas Huth     CPU_DoubleU u1;
1685fcf5ef2aSThomas Huth     CPU_FloatU u2;
1686fcf5ef2aSThomas Huth 
1687fcf5ef2aSThomas Huth     u1.ll = val;
1688fcf5ef2aSThomas Huth     u2.f = float64_to_float32(u1.d, &env->vec_status);
1689fcf5ef2aSThomas Huth 
1690fcf5ef2aSThomas Huth     return u2.l;
1691fcf5ef2aSThomas Huth }
1692fcf5ef2aSThomas Huth 
1693fcf5ef2aSThomas Huth uint64_t helper_efdcfs(CPUPPCState *env, uint32_t val)
1694fcf5ef2aSThomas Huth {
1695fcf5ef2aSThomas Huth     CPU_DoubleU u2;
1696fcf5ef2aSThomas Huth     CPU_FloatU u1;
1697fcf5ef2aSThomas Huth 
1698fcf5ef2aSThomas Huth     u1.l = val;
1699fcf5ef2aSThomas Huth     u2.d = float32_to_float64(u1.f, &env->vec_status);
1700fcf5ef2aSThomas Huth 
1701fcf5ef2aSThomas Huth     return u2.ll;
1702fcf5ef2aSThomas Huth }
1703fcf5ef2aSThomas Huth 
1704fcf5ef2aSThomas Huth /* Double precision fixed-point arithmetic */
1705fcf5ef2aSThomas Huth uint64_t helper_efdadd(CPUPPCState *env, uint64_t op1, uint64_t op2)
1706fcf5ef2aSThomas Huth {
1707fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1708fcf5ef2aSThomas Huth 
1709fcf5ef2aSThomas Huth     u1.ll = op1;
1710fcf5ef2aSThomas Huth     u2.ll = op2;
1711fcf5ef2aSThomas Huth     u1.d = float64_add(u1.d, u2.d, &env->vec_status);
1712fcf5ef2aSThomas Huth     return u1.ll;
1713fcf5ef2aSThomas Huth }
1714fcf5ef2aSThomas Huth 
1715fcf5ef2aSThomas Huth uint64_t helper_efdsub(CPUPPCState *env, uint64_t op1, uint64_t op2)
1716fcf5ef2aSThomas Huth {
1717fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1718fcf5ef2aSThomas Huth 
1719fcf5ef2aSThomas Huth     u1.ll = op1;
1720fcf5ef2aSThomas Huth     u2.ll = op2;
1721fcf5ef2aSThomas Huth     u1.d = float64_sub(u1.d, u2.d, &env->vec_status);
1722fcf5ef2aSThomas Huth     return u1.ll;
1723fcf5ef2aSThomas Huth }
1724fcf5ef2aSThomas Huth 
1725fcf5ef2aSThomas Huth uint64_t helper_efdmul(CPUPPCState *env, uint64_t op1, uint64_t op2)
1726fcf5ef2aSThomas Huth {
1727fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1728fcf5ef2aSThomas Huth 
1729fcf5ef2aSThomas Huth     u1.ll = op1;
1730fcf5ef2aSThomas Huth     u2.ll = op2;
1731fcf5ef2aSThomas Huth     u1.d = float64_mul(u1.d, u2.d, &env->vec_status);
1732fcf5ef2aSThomas Huth     return u1.ll;
1733fcf5ef2aSThomas Huth }
1734fcf5ef2aSThomas Huth 
1735fcf5ef2aSThomas Huth uint64_t helper_efddiv(CPUPPCState *env, uint64_t op1, uint64_t op2)
1736fcf5ef2aSThomas Huth {
1737fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1738fcf5ef2aSThomas Huth 
1739fcf5ef2aSThomas Huth     u1.ll = op1;
1740fcf5ef2aSThomas Huth     u2.ll = op2;
1741fcf5ef2aSThomas Huth     u1.d = float64_div(u1.d, u2.d, &env->vec_status);
1742fcf5ef2aSThomas Huth     return u1.ll;
1743fcf5ef2aSThomas Huth }
1744fcf5ef2aSThomas Huth 
1745fcf5ef2aSThomas Huth /* Double precision floating point helpers */
1746fcf5ef2aSThomas Huth uint32_t helper_efdtstlt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1747fcf5ef2aSThomas Huth {
1748fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1749fcf5ef2aSThomas Huth 
1750fcf5ef2aSThomas Huth     u1.ll = op1;
1751fcf5ef2aSThomas Huth     u2.ll = op2;
1752fcf5ef2aSThomas Huth     return float64_lt(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1753fcf5ef2aSThomas Huth }
1754fcf5ef2aSThomas Huth 
1755fcf5ef2aSThomas Huth uint32_t helper_efdtstgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1756fcf5ef2aSThomas Huth {
1757fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1758fcf5ef2aSThomas Huth 
1759fcf5ef2aSThomas Huth     u1.ll = op1;
1760fcf5ef2aSThomas Huth     u2.ll = op2;
1761fcf5ef2aSThomas Huth     return float64_le(u1.d, u2.d, &env->vec_status) ? 0 : 4;
1762fcf5ef2aSThomas Huth }
1763fcf5ef2aSThomas Huth 
1764fcf5ef2aSThomas Huth uint32_t helper_efdtsteq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1765fcf5ef2aSThomas Huth {
1766fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1767fcf5ef2aSThomas Huth 
1768fcf5ef2aSThomas Huth     u1.ll = op1;
1769fcf5ef2aSThomas Huth     u2.ll = op2;
1770fcf5ef2aSThomas Huth     return float64_eq_quiet(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1771fcf5ef2aSThomas Huth }
1772fcf5ef2aSThomas Huth 
1773fcf5ef2aSThomas Huth uint32_t helper_efdcmplt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1774fcf5ef2aSThomas Huth {
1775fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1776fcf5ef2aSThomas Huth     return helper_efdtstlt(env, op1, op2);
1777fcf5ef2aSThomas Huth }
1778fcf5ef2aSThomas Huth 
1779fcf5ef2aSThomas Huth uint32_t helper_efdcmpgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1780fcf5ef2aSThomas Huth {
1781fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1782fcf5ef2aSThomas Huth     return helper_efdtstgt(env, op1, op2);
1783fcf5ef2aSThomas Huth }
1784fcf5ef2aSThomas Huth 
1785fcf5ef2aSThomas Huth uint32_t helper_efdcmpeq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1786fcf5ef2aSThomas Huth {
1787fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1788fcf5ef2aSThomas Huth     return helper_efdtsteq(env, op1, op2);
1789fcf5ef2aSThomas Huth }
1790fcf5ef2aSThomas Huth 
1791fcf5ef2aSThomas Huth #define float64_to_float64(x, env) x
1792fcf5ef2aSThomas Huth 
1793fcf5ef2aSThomas Huth 
1794fa9ebf8cSDavid Gibson /*
1795fa9ebf8cSDavid Gibson  * VSX_ADD_SUB - VSX floating point add/subract
1796fcf5ef2aSThomas Huth  *   name  - instruction mnemonic
1797fcf5ef2aSThomas Huth  *   op    - operation (add or sub)
1798fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1799fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1800fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1801fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1802fcf5ef2aSThomas Huth  */
1803fcf5ef2aSThomas Huth #define VSX_ADD_SUB(name, op, nels, tp, fld, sfprf, r2sp)                    \
180499125c74SMark Cave-Ayland void helper_##name(CPUPPCState *env, ppc_vsr_t *xt,                          \
180599125c74SMark Cave-Ayland                    ppc_vsr_t *xa, ppc_vsr_t *xb)                             \
1806fcf5ef2aSThomas Huth {                                                                            \
1807cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
1808fcf5ef2aSThomas Huth     int i;                                                                   \
1809fcf5ef2aSThomas Huth                                                                              \
1810fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1811fcf5ef2aSThomas Huth                                                                              \
1812fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1813fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1814fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1815cf3b0334SMark Cave-Ayland         t.fld = tp##_##op(xa->fld, xb->fld, &tstat);                         \
1816fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1817fcf5ef2aSThomas Huth                                                                              \
1818fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
181957483867SRichard Henderson             float_invalid_op_addsub(env, sfprf, GETPC(),                     \
1820cf3b0334SMark Cave-Ayland                                     tp##_classify(xa->fld) |                 \
1821cf3b0334SMark Cave-Ayland                                     tp##_classify(xb->fld));                 \
1822fcf5ef2aSThomas Huth         }                                                                    \
1823fcf5ef2aSThomas Huth                                                                              \
1824fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
1825cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
1826fcf5ef2aSThomas Huth         }                                                                    \
1827fcf5ef2aSThomas Huth                                                                              \
1828fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
1829cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1830fcf5ef2aSThomas Huth         }                                                                    \
1831fcf5ef2aSThomas Huth     }                                                                        \
1832cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
18336525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
1834fcf5ef2aSThomas Huth }
1835fcf5ef2aSThomas Huth 
1836fcf5ef2aSThomas Huth VSX_ADD_SUB(xsadddp, add, 1, float64, VsrD(0), 1, 0)
1837fcf5ef2aSThomas Huth VSX_ADD_SUB(xsaddsp, add, 1, float64, VsrD(0), 1, 1)
1838fcf5ef2aSThomas Huth VSX_ADD_SUB(xvadddp, add, 2, float64, VsrD(i), 0, 0)
1839fcf5ef2aSThomas Huth VSX_ADD_SUB(xvaddsp, add, 4, float32, VsrW(i), 0, 0)
1840fcf5ef2aSThomas Huth VSX_ADD_SUB(xssubdp, sub, 1, float64, VsrD(0), 1, 0)
1841fcf5ef2aSThomas Huth VSX_ADD_SUB(xssubsp, sub, 1, float64, VsrD(0), 1, 1)
1842fcf5ef2aSThomas Huth VSX_ADD_SUB(xvsubdp, sub, 2, float64, VsrD(i), 0, 0)
1843fcf5ef2aSThomas Huth VSX_ADD_SUB(xvsubsp, sub, 4, float32, VsrW(i), 0, 0)
1844fcf5ef2aSThomas Huth 
184523d0766bSMark Cave-Ayland void helper_xsaddqp(CPUPPCState *env, uint32_t opcode,
184623d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
184707bdd247SBharata B Rao {
1848cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
184907bdd247SBharata B Rao     float_status tstat;
185007bdd247SBharata B Rao 
185107bdd247SBharata B Rao     helper_reset_fpstatus(env);
185207bdd247SBharata B Rao 
1853a8d411abSBharata B Rao     tstat = env->fp_status;
185407bdd247SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1855a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
185607bdd247SBharata B Rao     }
185707bdd247SBharata B Rao 
185807bdd247SBharata B Rao     set_float_exception_flags(0, &tstat);
1859cf3b0334SMark Cave-Ayland     t.f128 = float128_add(xa->f128, xb->f128, &tstat);
186007bdd247SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
186107bdd247SBharata B Rao 
186207bdd247SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
186357483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
1864cf3b0334SMark Cave-Ayland                                 float128_classify(xa->f128) |
1865cf3b0334SMark Cave-Ayland                                 float128_classify(xb->f128));
186607bdd247SBharata B Rao     }
186707bdd247SBharata B Rao 
1868cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
186907bdd247SBharata B Rao 
1870cf3b0334SMark Cave-Ayland     *xt = t;
18716525aadcSRichard Henderson     do_float_check_status(env, GETPC());
187207bdd247SBharata B Rao }
187307bdd247SBharata B Rao 
1874fa9ebf8cSDavid Gibson /*
1875fa9ebf8cSDavid Gibson  * VSX_MUL - VSX floating point multiply
1876fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1877fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1878fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1879fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1880fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1881fcf5ef2aSThomas Huth  */
1882fcf5ef2aSThomas Huth #define VSX_MUL(op, nels, tp, fld, sfprf, r2sp)                              \
188399125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                            \
188499125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                               \
1885fcf5ef2aSThomas Huth {                                                                            \
1886cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
1887fcf5ef2aSThomas Huth     int i;                                                                   \
1888fcf5ef2aSThomas Huth                                                                              \
1889fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1890fcf5ef2aSThomas Huth                                                                              \
1891fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1892fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1893fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1894cf3b0334SMark Cave-Ayland         t.fld = tp##_mul(xa->fld, xb->fld, &tstat);                          \
1895fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1896fcf5ef2aSThomas Huth                                                                              \
1897fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
18984f0da706SRichard Henderson             float_invalid_op_mul(env, sfprf, GETPC(),                        \
1899cf3b0334SMark Cave-Ayland                                  tp##_classify(xa->fld) |                    \
1900cf3b0334SMark Cave-Ayland                                  tp##_classify(xb->fld));                    \
1901fcf5ef2aSThomas Huth         }                                                                    \
1902fcf5ef2aSThomas Huth                                                                              \
1903fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
1904cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
1905fcf5ef2aSThomas Huth         }                                                                    \
1906fcf5ef2aSThomas Huth                                                                              \
1907fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
1908cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1909fcf5ef2aSThomas Huth         }                                                                    \
1910fcf5ef2aSThomas Huth     }                                                                        \
1911fcf5ef2aSThomas Huth                                                                              \
1912cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
19136525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
1914fcf5ef2aSThomas Huth }
1915fcf5ef2aSThomas Huth 
1916fcf5ef2aSThomas Huth VSX_MUL(xsmuldp, 1, float64, VsrD(0), 1, 0)
1917fcf5ef2aSThomas Huth VSX_MUL(xsmulsp, 1, float64, VsrD(0), 1, 1)
1918fcf5ef2aSThomas Huth VSX_MUL(xvmuldp, 2, float64, VsrD(i), 0, 0)
1919fcf5ef2aSThomas Huth VSX_MUL(xvmulsp, 4, float32, VsrW(i), 0, 0)
1920fcf5ef2aSThomas Huth 
192123d0766bSMark Cave-Ayland void helper_xsmulqp(CPUPPCState *env, uint32_t opcode,
192223d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
1923a811ec04SBharata B Rao {
1924cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
1925a8d411abSBharata B Rao     float_status tstat;
1926a811ec04SBharata B Rao 
1927a8d411abSBharata B Rao     helper_reset_fpstatus(env);
1928a8d411abSBharata B Rao     tstat = env->fp_status;
1929a811ec04SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1930a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
1931a811ec04SBharata B Rao     }
1932a811ec04SBharata B Rao 
1933a811ec04SBharata B Rao     set_float_exception_flags(0, &tstat);
1934cf3b0334SMark Cave-Ayland     t.f128 = float128_mul(xa->f128, xb->f128, &tstat);
1935a811ec04SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1936a811ec04SBharata B Rao 
1937a811ec04SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
19384f0da706SRichard Henderson         float_invalid_op_mul(env, 1, GETPC(),
1939cf3b0334SMark Cave-Ayland                              float128_classify(xa->f128) |
1940cf3b0334SMark Cave-Ayland                              float128_classify(xb->f128));
1941a811ec04SBharata B Rao     }
1942cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
1943a811ec04SBharata B Rao 
1944cf3b0334SMark Cave-Ayland     *xt = t;
19456525aadcSRichard Henderson     do_float_check_status(env, GETPC());
1946a811ec04SBharata B Rao }
1947a811ec04SBharata B Rao 
1948fa9ebf8cSDavid Gibson /*
1949fa9ebf8cSDavid Gibson  * VSX_DIV - VSX floating point divide
1950fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1951fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1952fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1953fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1954fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1955fcf5ef2aSThomas Huth  */
1956fcf5ef2aSThomas Huth #define VSX_DIV(op, nels, tp, fld, sfprf, r2sp)                               \
195799125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
195899125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                                \
1959fcf5ef2aSThomas Huth {                                                                             \
1960cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
1961fcf5ef2aSThomas Huth     int i;                                                                    \
1962fcf5ef2aSThomas Huth                                                                               \
1963fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
1964fcf5ef2aSThomas Huth                                                                               \
1965fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
1966fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                  \
1967fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                 \
1968cf3b0334SMark Cave-Ayland         t.fld = tp##_div(xa->fld, xb->fld, &tstat);                           \
1969fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags;  \
1970fcf5ef2aSThomas Huth                                                                               \
1971fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {     \
1972fec59ef3SRichard Henderson             float_invalid_op_div(env, sfprf, GETPC(),                         \
1973cf3b0334SMark Cave-Ayland                                  tp##_classify(xa->fld) |                     \
1974cf3b0334SMark Cave-Ayland                                  tp##_classify(xb->fld));                     \
1975fcf5ef2aSThomas Huth         }                                                                     \
1976ae13018dSRichard Henderson         if (unlikely(tstat.float_exception_flags & float_flag_divbyzero)) {   \
1977ae13018dSRichard Henderson             float_zero_divide_excp(env, GETPC());                             \
1978ae13018dSRichard Henderson         }                                                                     \
1979fcf5ef2aSThomas Huth                                                                               \
1980fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
1981cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                  \
1982fcf5ef2aSThomas Huth         }                                                                     \
1983fcf5ef2aSThomas Huth                                                                               \
1984fcf5ef2aSThomas Huth         if (sfprf) {                                                          \
1985cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
1986fcf5ef2aSThomas Huth         }                                                                     \
1987fcf5ef2aSThomas Huth     }                                                                         \
1988fcf5ef2aSThomas Huth                                                                               \
1989cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
19906525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
1991fcf5ef2aSThomas Huth }
1992fcf5ef2aSThomas Huth 
1993fcf5ef2aSThomas Huth VSX_DIV(xsdivdp, 1, float64, VsrD(0), 1, 0)
1994fcf5ef2aSThomas Huth VSX_DIV(xsdivsp, 1, float64, VsrD(0), 1, 1)
1995fcf5ef2aSThomas Huth VSX_DIV(xvdivdp, 2, float64, VsrD(i), 0, 0)
1996fcf5ef2aSThomas Huth VSX_DIV(xvdivsp, 4, float32, VsrW(i), 0, 0)
1997fcf5ef2aSThomas Huth 
199823d0766bSMark Cave-Ayland void helper_xsdivqp(CPUPPCState *env, uint32_t opcode,
199923d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
2000314c1163SBharata B Rao {
2001cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
2002a8d411abSBharata B Rao     float_status tstat;
2003314c1163SBharata B Rao 
2004a8d411abSBharata B Rao     helper_reset_fpstatus(env);
2005a8d411abSBharata B Rao     tstat = env->fp_status;
2006314c1163SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
2007a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
2008314c1163SBharata B Rao     }
2009314c1163SBharata B Rao 
2010314c1163SBharata B Rao     set_float_exception_flags(0, &tstat);
2011cf3b0334SMark Cave-Ayland     t.f128 = float128_div(xa->f128, xb->f128, &tstat);
2012314c1163SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
2013314c1163SBharata B Rao 
2014314c1163SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
2015fec59ef3SRichard Henderson         float_invalid_op_div(env, 1, GETPC(),
2016cf3b0334SMark Cave-Ayland                              float128_classify(xa->f128) |
2017cf3b0334SMark Cave-Ayland                              float128_classify(xb->f128));
2018314c1163SBharata B Rao     }
2019ae13018dSRichard Henderson     if (unlikely(tstat.float_exception_flags & float_flag_divbyzero)) {
2020ae13018dSRichard Henderson         float_zero_divide_excp(env, GETPC());
2021ae13018dSRichard Henderson     }
2022314c1163SBharata B Rao 
2023cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
2024cf3b0334SMark Cave-Ayland     *xt = t;
20256525aadcSRichard Henderson     do_float_check_status(env, GETPC());
2026314c1163SBharata B Rao }
2027314c1163SBharata B Rao 
2028fa9ebf8cSDavid Gibson /*
2029fa9ebf8cSDavid Gibson  * VSX_RE  - VSX floating point reciprocal estimate
2030fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2031fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2032fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2033fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2034fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2035fcf5ef2aSThomas Huth  */
2036fcf5ef2aSThomas Huth #define VSX_RE(op, nels, tp, fld, sfprf, r2sp)                                \
203775cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)              \
2038fcf5ef2aSThomas Huth {                                                                             \
2039cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2040fcf5ef2aSThomas Huth     int i;                                                                    \
2041fcf5ef2aSThomas Huth                                                                               \
2042fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
2043fcf5ef2aSThomas Huth                                                                               \
2044fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2045cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xb->fld, &env->fp_status))) {      \
204613c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
2047fcf5ef2aSThomas Huth         }                                                                     \
2048cf3b0334SMark Cave-Ayland         t.fld = tp##_div(tp##_one, xb->fld, &env->fp_status);                 \
2049fcf5ef2aSThomas Huth                                                                               \
2050fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
2051cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                  \
2052fcf5ef2aSThomas Huth         }                                                                     \
2053fcf5ef2aSThomas Huth                                                                               \
2054fcf5ef2aSThomas Huth         if (sfprf) {                                                          \
2055cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
2056fcf5ef2aSThomas Huth         }                                                                     \
2057fcf5ef2aSThomas Huth     }                                                                         \
2058fcf5ef2aSThomas Huth                                                                               \
2059cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
20606525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2061fcf5ef2aSThomas Huth }
2062fcf5ef2aSThomas Huth 
2063fcf5ef2aSThomas Huth VSX_RE(xsredp, 1, float64, VsrD(0), 1, 0)
2064fcf5ef2aSThomas Huth VSX_RE(xsresp, 1, float64, VsrD(0), 1, 1)
2065fcf5ef2aSThomas Huth VSX_RE(xvredp, 2, float64, VsrD(i), 0, 0)
2066fcf5ef2aSThomas Huth VSX_RE(xvresp, 4, float32, VsrW(i), 0, 0)
2067fcf5ef2aSThomas Huth 
2068fa9ebf8cSDavid Gibson /*
2069fa9ebf8cSDavid Gibson  * VSX_SQRT - VSX floating point square root
2070fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2071fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2072fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2073fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2074fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2075fcf5ef2aSThomas Huth  */
2076fcf5ef2aSThomas Huth #define VSX_SQRT(op, nels, tp, fld, sfprf, r2sp)                             \
207775cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2078fcf5ef2aSThomas Huth {                                                                            \
2079cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
2080fcf5ef2aSThomas Huth     int i;                                                                   \
2081fcf5ef2aSThomas Huth                                                                              \
2082fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
2083fcf5ef2aSThomas Huth                                                                              \
2084fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
2085fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
2086fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
2087cf3b0334SMark Cave-Ayland         t.fld = tp##_sqrt(xb->fld, &tstat);                                  \
2088fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2089fcf5ef2aSThomas Huth                                                                              \
2090fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
2091cf3b0334SMark Cave-Ayland             if (tp##_is_neg(xb->fld) && !tp##_is_zero(xb->fld)) {            \
209213c9115fSRichard Henderson                 float_invalid_op_vxsqrt(env, sfprf, GETPC());                \
2093cf3b0334SMark Cave-Ayland             } else if (tp##_is_signaling_nan(xb->fld, &tstat)) {             \
209413c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                       \
2095fcf5ef2aSThomas Huth             }                                                                \
2096fcf5ef2aSThomas Huth         }                                                                    \
2097fcf5ef2aSThomas Huth                                                                              \
2098fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
2099cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
2100fcf5ef2aSThomas Huth         }                                                                    \
2101fcf5ef2aSThomas Huth                                                                              \
2102fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
2103cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
2104fcf5ef2aSThomas Huth         }                                                                    \
2105fcf5ef2aSThomas Huth     }                                                                        \
2106fcf5ef2aSThomas Huth                                                                              \
2107cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
21086525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
2109fcf5ef2aSThomas Huth }
2110fcf5ef2aSThomas Huth 
2111fcf5ef2aSThomas Huth VSX_SQRT(xssqrtdp, 1, float64, VsrD(0), 1, 0)
2112fcf5ef2aSThomas Huth VSX_SQRT(xssqrtsp, 1, float64, VsrD(0), 1, 1)
2113fcf5ef2aSThomas Huth VSX_SQRT(xvsqrtdp, 2, float64, VsrD(i), 0, 0)
2114fcf5ef2aSThomas Huth VSX_SQRT(xvsqrtsp, 4, float32, VsrW(i), 0, 0)
2115fcf5ef2aSThomas Huth 
2116fa9ebf8cSDavid Gibson /*
2117fa9ebf8cSDavid Gibson  *VSX_RSQRTE - VSX floating point reciprocal square root estimate
2118fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2119fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2120fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2121fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2122fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2123fcf5ef2aSThomas Huth  */
2124fcf5ef2aSThomas Huth #define VSX_RSQRTE(op, nels, tp, fld, sfprf, r2sp)                           \
212575cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2126fcf5ef2aSThomas Huth {                                                                            \
2127cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
2128fcf5ef2aSThomas Huth     int i;                                                                   \
2129fcf5ef2aSThomas Huth                                                                              \
2130fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
2131fcf5ef2aSThomas Huth                                                                              \
2132fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
2133fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
2134fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
2135cf3b0334SMark Cave-Ayland         t.fld = tp##_sqrt(xb->fld, &tstat);                                  \
2136cf3b0334SMark Cave-Ayland         t.fld = tp##_div(tp##_one, t.fld, &tstat);                           \
2137fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2138fcf5ef2aSThomas Huth                                                                              \
2139fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
2140cf3b0334SMark Cave-Ayland             if (tp##_is_neg(xb->fld) && !tp##_is_zero(xb->fld)) {            \
214113c9115fSRichard Henderson                 float_invalid_op_vxsqrt(env, sfprf, GETPC());                \
2142cf3b0334SMark Cave-Ayland             } else if (tp##_is_signaling_nan(xb->fld, &tstat)) {             \
214313c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                       \
2144fcf5ef2aSThomas Huth             }                                                                \
2145fcf5ef2aSThomas Huth         }                                                                    \
2146fcf5ef2aSThomas Huth                                                                              \
2147fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
2148cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
2149fcf5ef2aSThomas Huth         }                                                                    \
2150fcf5ef2aSThomas Huth                                                                              \
2151fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
2152cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
2153fcf5ef2aSThomas Huth         }                                                                    \
2154fcf5ef2aSThomas Huth     }                                                                        \
2155fcf5ef2aSThomas Huth                                                                              \
2156cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
21576525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
2158fcf5ef2aSThomas Huth }
2159fcf5ef2aSThomas Huth 
2160fcf5ef2aSThomas Huth VSX_RSQRTE(xsrsqrtedp, 1, float64, VsrD(0), 1, 0)
2161fcf5ef2aSThomas Huth VSX_RSQRTE(xsrsqrtesp, 1, float64, VsrD(0), 1, 1)
2162fcf5ef2aSThomas Huth VSX_RSQRTE(xvrsqrtedp, 2, float64, VsrD(i), 0, 0)
2163fcf5ef2aSThomas Huth VSX_RSQRTE(xvrsqrtesp, 4, float32, VsrW(i), 0, 0)
2164fcf5ef2aSThomas Huth 
2165fa9ebf8cSDavid Gibson /*
2166fa9ebf8cSDavid Gibson  * VSX_TDIV - VSX floating point test for divide
2167fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2168fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2169fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2170fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2171fcf5ef2aSThomas Huth  *   emin  - minimum unbiased exponent
2172fcf5ef2aSThomas Huth  *   emax  - maximum unbiased exponent
2173fcf5ef2aSThomas Huth  *   nbits - number of fraction bits
2174fcf5ef2aSThomas Huth  */
2175fcf5ef2aSThomas Huth #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits)                  \
2176033e1fcdSMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
2177033e1fcdSMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                          \
2178fcf5ef2aSThomas Huth {                                                                       \
2179fcf5ef2aSThomas Huth     int i;                                                              \
2180fcf5ef2aSThomas Huth     int fe_flag = 0;                                                    \
2181fcf5ef2aSThomas Huth     int fg_flag = 0;                                                    \
2182fcf5ef2aSThomas Huth                                                                         \
2183fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2184cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_infinity(xa->fld) ||                       \
2185cf3b0334SMark Cave-Ayland                      tp##_is_infinity(xb->fld) ||                       \
2186cf3b0334SMark Cave-Ayland                      tp##_is_zero(xb->fld))) {                          \
2187fcf5ef2aSThomas Huth             fe_flag = 1;                                                \
2188fcf5ef2aSThomas Huth             fg_flag = 1;                                                \
2189fcf5ef2aSThomas Huth         } else {                                                        \
2190cf3b0334SMark Cave-Ayland             int e_a = ppc_##tp##_get_unbiased_exp(xa->fld);             \
2191cf3b0334SMark Cave-Ayland             int e_b = ppc_##tp##_get_unbiased_exp(xb->fld);             \
2192fcf5ef2aSThomas Huth                                                                         \
2193cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_any_nan(xa->fld) ||                    \
2194cf3b0334SMark Cave-Ayland                          tp##_is_any_nan(xb->fld))) {                   \
2195fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2196fcf5ef2aSThomas Huth             } else if ((e_b <= emin) || (e_b >= (emax - 2))) {          \
2197fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2198cf3b0334SMark Cave-Ayland             } else if (!tp##_is_zero(xa->fld) &&                        \
2199fcf5ef2aSThomas Huth                        (((e_a - e_b) >= emax) ||                        \
2200fcf5ef2aSThomas Huth                         ((e_a - e_b) <= (emin + 1)) ||                  \
2201fcf5ef2aSThomas Huth                         (e_a <= (emin + nbits)))) {                     \
2202fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2203fcf5ef2aSThomas Huth             }                                                           \
2204fcf5ef2aSThomas Huth                                                                         \
2205cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_zero_or_denormal(xb->fld))) {          \
2206fa9ebf8cSDavid Gibson                 /*                                                      \
2207fa9ebf8cSDavid Gibson                  * XB is not zero because of the above check and so     \
2208fa9ebf8cSDavid Gibson                  * must be denormalized.                                \
2209fa9ebf8cSDavid Gibson                  */                                                     \
2210fcf5ef2aSThomas Huth                 fg_flag = 1;                                            \
2211fcf5ef2aSThomas Huth             }                                                           \
2212fcf5ef2aSThomas Huth         }                                                               \
2213fcf5ef2aSThomas Huth     }                                                                   \
2214fcf5ef2aSThomas Huth                                                                         \
2215fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2216fcf5ef2aSThomas Huth }
2217fcf5ef2aSThomas Huth 
2218fcf5ef2aSThomas Huth VSX_TDIV(xstdivdp, 1, float64, VsrD(0), -1022, 1023, 52)
2219fcf5ef2aSThomas Huth VSX_TDIV(xvtdivdp, 2, float64, VsrD(i), -1022, 1023, 52)
2220fcf5ef2aSThomas Huth VSX_TDIV(xvtdivsp, 4, float32, VsrW(i), -126, 127, 23)
2221fcf5ef2aSThomas Huth 
2222fa9ebf8cSDavid Gibson /*
2223fa9ebf8cSDavid Gibson  * VSX_TSQRT - VSX floating point test for square root
2224fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2225fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2226fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2227fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2228fcf5ef2aSThomas Huth  *   emin  - minimum unbiased exponent
2229fcf5ef2aSThomas Huth  *   emax  - maximum unbiased exponent
2230fcf5ef2aSThomas Huth  *   nbits - number of fraction bits
2231fcf5ef2aSThomas Huth  */
2232fcf5ef2aSThomas Huth #define VSX_TSQRT(op, nels, tp, fld, emin, nbits)                       \
22338d830485SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xb)      \
2234fcf5ef2aSThomas Huth {                                                                       \
2235fcf5ef2aSThomas Huth     int i;                                                              \
2236fcf5ef2aSThomas Huth     int fe_flag = 0;                                                    \
2237fcf5ef2aSThomas Huth     int fg_flag = 0;                                                    \
2238fcf5ef2aSThomas Huth                                                                         \
2239fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2240cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_infinity(xb->fld) ||                       \
2241cf3b0334SMark Cave-Ayland                      tp##_is_zero(xb->fld))) {                          \
2242fcf5ef2aSThomas Huth             fe_flag = 1;                                                \
2243fcf5ef2aSThomas Huth             fg_flag = 1;                                                \
2244fcf5ef2aSThomas Huth         } else {                                                        \
2245cf3b0334SMark Cave-Ayland             int e_b = ppc_##tp##_get_unbiased_exp(xb->fld);             \
2246fcf5ef2aSThomas Huth                                                                         \
2247cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_any_nan(xb->fld))) {                   \
2248fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2249cf3b0334SMark Cave-Ayland             } else if (unlikely(tp##_is_zero(xb->fld))) {               \
2250fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2251cf3b0334SMark Cave-Ayland             } else if (unlikely(tp##_is_neg(xb->fld))) {                \
2252fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2253cf3b0334SMark Cave-Ayland             } else if (!tp##_is_zero(xb->fld) &&                        \
2254fcf5ef2aSThomas Huth                        (e_b <= (emin + nbits))) {                       \
2255fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2256fcf5ef2aSThomas Huth             }                                                           \
2257fcf5ef2aSThomas Huth                                                                         \
2258cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_zero_or_denormal(xb->fld))) {          \
2259fa9ebf8cSDavid Gibson                 /*                                                      \
2260fa9ebf8cSDavid Gibson                  * XB is not zero because of the above check and        \
2261fa9ebf8cSDavid Gibson                  * therefore must be denormalized.                      \
2262fa9ebf8cSDavid Gibson                  */                                                     \
2263fcf5ef2aSThomas Huth                 fg_flag = 1;                                            \
2264fcf5ef2aSThomas Huth             }                                                           \
2265fcf5ef2aSThomas Huth         }                                                               \
2266fcf5ef2aSThomas Huth     }                                                                   \
2267fcf5ef2aSThomas Huth                                                                         \
2268fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2269fcf5ef2aSThomas Huth }
2270fcf5ef2aSThomas Huth 
2271fcf5ef2aSThomas Huth VSX_TSQRT(xstsqrtdp, 1, float64, VsrD(0), -1022, 52)
2272fcf5ef2aSThomas Huth VSX_TSQRT(xvtsqrtdp, 2, float64, VsrD(i), -1022, 52)
2273fcf5ef2aSThomas Huth VSX_TSQRT(xvtsqrtsp, 4, float32, VsrW(i), -126, 23)
2274fcf5ef2aSThomas Huth 
2275fa9ebf8cSDavid Gibson /*
2276fa9ebf8cSDavid Gibson  * VSX_MADD - VSX floating point muliply/add variations
2277fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2278fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2279fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2280fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2281fcf5ef2aSThomas Huth  *   maddflgs - flags for the float*muladd routine that control the
2282fcf5ef2aSThomas Huth  *           various forms (madd, msub, nmadd, nmsub)
2283fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2284fcf5ef2aSThomas Huth  */
2285c9f4e4d8SMark Cave-Ayland #define VSX_MADD(op, nels, tp, fld, maddflgs, sfprf, r2sp)                    \
228699125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
2287c9f4e4d8SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *b, ppc_vsr_t *c)                   \
2288fcf5ef2aSThomas Huth {                                                                             \
2289c9f4e4d8SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2290fcf5ef2aSThomas Huth     int i;                                                                    \
2291fcf5ef2aSThomas Huth                                                                               \
2292fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
2293fcf5ef2aSThomas Huth                                                                               \
2294fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2295fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                  \
2296fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                 \
2297fcf5ef2aSThomas Huth         if (r2sp && (tstat.float_rounding_mode == float_round_nearest_even)) {\
2298fa9ebf8cSDavid Gibson             /*                                                                \
2299fa9ebf8cSDavid Gibson              * Avoid double rounding errors by rounding the intermediate      \
2300fa9ebf8cSDavid Gibson              * result to odd.                                                 \
2301fa9ebf8cSDavid Gibson              */                                                               \
2302fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &tstat);             \
2303cf3b0334SMark Cave-Ayland             t.fld = tp##_muladd(xa->fld, b->fld, c->fld,                      \
2304fcf5ef2aSThomas Huth                                 maddflgs, &tstat);                            \
2305cf3b0334SMark Cave-Ayland             t.fld |= (get_float_exception_flags(&tstat) &                     \
2306fcf5ef2aSThomas Huth                       float_flag_inexact) != 0;                               \
2307fcf5ef2aSThomas Huth         } else {                                                              \
2308cf3b0334SMark Cave-Ayland             t.fld = tp##_muladd(xa->fld, b->fld, c->fld,                      \
2309fcf5ef2aSThomas Huth                                 maddflgs, &tstat);                            \
2310fcf5ef2aSThomas Huth         }                                                                     \
2311fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags;  \
2312fcf5ef2aSThomas Huth                                                                               \
2313fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {     \
2314cf3b0334SMark Cave-Ayland             tp##_maddsub_update_excp(env, xa->fld, b->fld,                    \
231513c9115fSRichard Henderson                                      c->fld, maddflgs, GETPC());              \
2316fcf5ef2aSThomas Huth         }                                                                     \
2317fcf5ef2aSThomas Huth                                                                               \
2318fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
2319cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                  \
2320fcf5ef2aSThomas Huth         }                                                                     \
2321fcf5ef2aSThomas Huth                                                                               \
2322fcf5ef2aSThomas Huth         if (sfprf) {                                                          \
2323cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
2324fcf5ef2aSThomas Huth         }                                                                     \
2325fcf5ef2aSThomas Huth     }                                                                         \
2326cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
23276525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2328fcf5ef2aSThomas Huth }
2329fcf5ef2aSThomas Huth 
2330c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmadddp, 1, float64, VsrD(0), MADD_FLGS, 1, 0)
2331c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmsubdp, 1, float64, VsrD(0), MSUB_FLGS, 1, 0)
2332c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmadddp, 1, float64, VsrD(0), NMADD_FLGS, 1, 0)
2333c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmsubdp, 1, float64, VsrD(0), NMSUB_FLGS, 1, 0)
2334c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmaddsp, 1, float64, VsrD(0), MADD_FLGS, 1, 1)
2335c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmsubsp, 1, float64, VsrD(0), MSUB_FLGS, 1, 1)
2336c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmaddsp, 1, float64, VsrD(0), NMADD_FLGS, 1, 1)
2337c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmsubsp, 1, float64, VsrD(0), NMSUB_FLGS, 1, 1)
2338fcf5ef2aSThomas Huth 
2339c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmadddp, 2, float64, VsrD(i), MADD_FLGS, 0, 0)
2340c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmsubdp, 2, float64, VsrD(i), MSUB_FLGS, 0, 0)
2341c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmadddp, 2, float64, VsrD(i), NMADD_FLGS, 0, 0)
2342c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmsubdp, 2, float64, VsrD(i), NMSUB_FLGS, 0, 0)
2343fcf5ef2aSThomas Huth 
2344c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmaddsp, 4, float32, VsrW(i), MADD_FLGS, 0, 0)
2345c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmsubsp, 4, float32, VsrW(i), MSUB_FLGS, 0, 0)
2346c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmaddsp, 4, float32, VsrW(i), NMADD_FLGS, 0, 0)
2347c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmsubsp, 4, float32, VsrW(i), NMSUB_FLGS, 0, 0)
2348fcf5ef2aSThomas Huth 
2349fa9ebf8cSDavid Gibson /*
2350fa9ebf8cSDavid Gibson  * VSX_SCALAR_CMP_DP - VSX scalar floating point compare double precision
2351fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2352fcf5ef2aSThomas Huth  *   cmp   - comparison operation
2353fcf5ef2aSThomas Huth  *   exp   - expected result of comparison
2354fcf5ef2aSThomas Huth  *   svxvc - set VXVC bit
2355fcf5ef2aSThomas Huth  */
2356fcf5ef2aSThomas Huth #define VSX_SCALAR_CMP_DP(op, cmp, exp, svxvc)                                \
235799125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
235899125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                                \
2359fcf5ef2aSThomas Huth {                                                                             \
2360cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2361fcf5ef2aSThomas Huth     bool vxsnan_flag = false, vxvc_flag = false, vex_flag = false;            \
2362fcf5ef2aSThomas Huth                                                                               \
2363cf3b0334SMark Cave-Ayland     if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||             \
2364cf3b0334SMark Cave-Ayland         float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {             \
2365fcf5ef2aSThomas Huth         vxsnan_flag = true;                                                   \
2366fcf5ef2aSThomas Huth         if (fpscr_ve == 0 && svxvc) {                                         \
2367fcf5ef2aSThomas Huth             vxvc_flag = true;                                                 \
2368fcf5ef2aSThomas Huth         }                                                                     \
2369fcf5ef2aSThomas Huth     } else if (svxvc) {                                                       \
2370cf3b0334SMark Cave-Ayland         vxvc_flag = float64_is_quiet_nan(xa->VsrD(0), &env->fp_status) ||     \
2371cf3b0334SMark Cave-Ayland             float64_is_quiet_nan(xb->VsrD(0), &env->fp_status);               \
2372fcf5ef2aSThomas Huth     }                                                                         \
2373fcf5ef2aSThomas Huth     if (vxsnan_flag) {                                                        \
237413c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
2375fcf5ef2aSThomas Huth     }                                                                         \
2376fcf5ef2aSThomas Huth     if (vxvc_flag) {                                                          \
237713c9115fSRichard Henderson         float_invalid_op_vxvc(env, 0, GETPC());                               \
2378fcf5ef2aSThomas Huth     }                                                                         \
2379fcf5ef2aSThomas Huth     vex_flag = fpscr_ve && (vxvc_flag || vxsnan_flag);                        \
2380fcf5ef2aSThomas Huth                                                                               \
2381fcf5ef2aSThomas Huth     if (!vex_flag) {                                                          \
2382cf3b0334SMark Cave-Ayland         if (float64_##cmp(xb->VsrD(0), xa->VsrD(0),                           \
2383cf3b0334SMark Cave-Ayland                           &env->fp_status) == exp) {                          \
2384cf3b0334SMark Cave-Ayland             t.VsrD(0) = -1;                                                   \
2385cf3b0334SMark Cave-Ayland             t.VsrD(1) = 0;                                                    \
2386fcf5ef2aSThomas Huth         } else {                                                              \
2387cf3b0334SMark Cave-Ayland             t.VsrD(0) = 0;                                                    \
2388cf3b0334SMark Cave-Ayland             t.VsrD(1) = 0;                                                    \
2389fcf5ef2aSThomas Huth         }                                                                     \
2390fcf5ef2aSThomas Huth     }                                                                         \
2391cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
23926525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2393fcf5ef2aSThomas Huth }
2394fcf5ef2aSThomas Huth 
2395fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpeqdp, eq, 1, 0)
2396fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpgedp, le, 1, 1)
2397fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpgtdp, lt, 1, 1)
2398fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpnedp, eq, 0, 0)
2399fcf5ef2aSThomas Huth 
2400033e1fcdSMark Cave-Ayland void helper_xscmpexpdp(CPUPPCState *env, uint32_t opcode,
2401033e1fcdSMark Cave-Ayland                        ppc_vsr_t *xa, ppc_vsr_t *xb)
24023a20d11dSBharata B Rao {
24033a20d11dSBharata B Rao     int64_t exp_a, exp_b;
24043a20d11dSBharata B Rao     uint32_t cc;
24053a20d11dSBharata B Rao 
2406cf3b0334SMark Cave-Ayland     exp_a = extract64(xa->VsrD(0), 52, 11);
2407cf3b0334SMark Cave-Ayland     exp_b = extract64(xb->VsrD(0), 52, 11);
24083a20d11dSBharata B Rao 
2409cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)) ||
2410cf3b0334SMark Cave-Ayland                  float64_is_any_nan(xb->VsrD(0)))) {
24113a20d11dSBharata B Rao         cc = CRF_SO;
24123a20d11dSBharata B Rao     } else {
24133a20d11dSBharata B Rao         if (exp_a < exp_b) {
24143a20d11dSBharata B Rao             cc = CRF_LT;
24153a20d11dSBharata B Rao         } else if (exp_a > exp_b) {
24163a20d11dSBharata B Rao             cc = CRF_GT;
24173a20d11dSBharata B Rao         } else {
24183a20d11dSBharata B Rao             cc = CRF_EQ;
24193a20d11dSBharata B Rao         }
24203a20d11dSBharata B Rao     }
24213a20d11dSBharata B Rao 
24223a20d11dSBharata B Rao     env->fpscr &= ~(0x0F << FPSCR_FPRF);
24233a20d11dSBharata B Rao     env->fpscr |= cc << FPSCR_FPRF;
24243a20d11dSBharata B Rao     env->crf[BF(opcode)] = cc;
24253a20d11dSBharata B Rao 
24266525aadcSRichard Henderson     do_float_check_status(env, GETPC());
24273a20d11dSBharata B Rao }
24283a20d11dSBharata B Rao 
24296ae4a57aSMark Cave-Ayland void helper_xscmpexpqp(CPUPPCState *env, uint32_t opcode,
24306ae4a57aSMark Cave-Ayland                        ppc_vsr_t *xa, ppc_vsr_t *xb)
24313a20d11dSBharata B Rao {
24323a20d11dSBharata B Rao     int64_t exp_a, exp_b;
24333a20d11dSBharata B Rao     uint32_t cc;
24343a20d11dSBharata B Rao 
2435cf3b0334SMark Cave-Ayland     exp_a = extract64(xa->VsrD(0), 48, 15);
2436cf3b0334SMark Cave-Ayland     exp_b = extract64(xb->VsrD(0), 48, 15);
24373a20d11dSBharata B Rao 
2438cf3b0334SMark Cave-Ayland     if (unlikely(float128_is_any_nan(xa->f128) ||
2439cf3b0334SMark Cave-Ayland                  float128_is_any_nan(xb->f128))) {
24403a20d11dSBharata B Rao         cc = CRF_SO;
24413a20d11dSBharata B Rao     } else {
24423a20d11dSBharata B Rao         if (exp_a < exp_b) {
24433a20d11dSBharata B Rao             cc = CRF_LT;
24443a20d11dSBharata B Rao         } else if (exp_a > exp_b) {
24453a20d11dSBharata B Rao             cc = CRF_GT;
24463a20d11dSBharata B Rao         } else {
24473a20d11dSBharata B Rao             cc = CRF_EQ;
24483a20d11dSBharata B Rao         }
24493a20d11dSBharata B Rao     }
24503a20d11dSBharata B Rao 
24513a20d11dSBharata B Rao     env->fpscr &= ~(0x0F << FPSCR_FPRF);
24523a20d11dSBharata B Rao     env->fpscr |= cc << FPSCR_FPRF;
24533a20d11dSBharata B Rao     env->crf[BF(opcode)] = cc;
24543a20d11dSBharata B Rao 
24556525aadcSRichard Henderson     do_float_check_status(env, GETPC());
24563a20d11dSBharata B Rao }
24573a20d11dSBharata B Rao 
2458fcf5ef2aSThomas Huth #define VSX_SCALAR_CMP(op, ordered)                                      \
2459033e1fcdSMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                      \
2460033e1fcdSMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                           \
2461fcf5ef2aSThomas Huth {                                                                        \
2462fcf5ef2aSThomas Huth     uint32_t cc = 0;                                                     \
2463855f7a65SBharata B Rao     bool vxsnan_flag = false, vxvc_flag = false;                         \
2464fcf5ef2aSThomas Huth                                                                          \
2465855f7a65SBharata B Rao     helper_reset_fpstatus(env);                                          \
2466fcf5ef2aSThomas Huth                                                                          \
2467cf3b0334SMark Cave-Ayland     if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||        \
2468cf3b0334SMark Cave-Ayland         float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {        \
2469855f7a65SBharata B Rao         vxsnan_flag = true;                                              \
2470855f7a65SBharata B Rao         cc = CRF_SO;                                                     \
2471855f7a65SBharata B Rao         if (fpscr_ve == 0 && ordered) {                                  \
2472855f7a65SBharata B Rao             vxvc_flag = true;                                            \
2473855f7a65SBharata B Rao         }                                                                \
2474cf3b0334SMark Cave-Ayland     } else if (float64_is_quiet_nan(xa->VsrD(0), &env->fp_status) ||     \
2475cf3b0334SMark Cave-Ayland                float64_is_quiet_nan(xb->VsrD(0), &env->fp_status)) {     \
2476855f7a65SBharata B Rao         cc = CRF_SO;                                                     \
2477855f7a65SBharata B Rao         if (ordered) {                                                   \
2478855f7a65SBharata B Rao             vxvc_flag = true;                                            \
2479855f7a65SBharata B Rao         }                                                                \
2480855f7a65SBharata B Rao     }                                                                    \
2481855f7a65SBharata B Rao     if (vxsnan_flag) {                                                   \
248213c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                           \
2483fcf5ef2aSThomas Huth     }                                                                    \
2484855f7a65SBharata B Rao     if (vxvc_flag) {                                                     \
248513c9115fSRichard Henderson         float_invalid_op_vxvc(env, 0, GETPC());                          \
2486fcf5ef2aSThomas Huth     }                                                                    \
2487855f7a65SBharata B Rao                                                                          \
2488cf3b0334SMark Cave-Ayland     if (float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) {         \
2489855f7a65SBharata B Rao         cc |= CRF_LT;                                                    \
2490cf3b0334SMark Cave-Ayland     } else if (!float64_le(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) { \
2491855f7a65SBharata B Rao         cc |= CRF_GT;                                                    \
2492fcf5ef2aSThomas Huth     } else {                                                             \
2493855f7a65SBharata B Rao         cc |= CRF_EQ;                                                    \
2494fcf5ef2aSThomas Huth     }                                                                    \
2495fcf5ef2aSThomas Huth                                                                          \
2496fcf5ef2aSThomas Huth     env->fpscr &= ~(0x0F << FPSCR_FPRF);                                 \
2497fcf5ef2aSThomas Huth     env->fpscr |= cc << FPSCR_FPRF;                                      \
2498fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = cc;                                           \
2499fcf5ef2aSThomas Huth                                                                          \
25006525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                 \
2501fcf5ef2aSThomas Huth }
2502fcf5ef2aSThomas Huth 
2503fcf5ef2aSThomas Huth VSX_SCALAR_CMP(xscmpodp, 1)
2504fcf5ef2aSThomas Huth VSX_SCALAR_CMP(xscmpudp, 0)
2505fcf5ef2aSThomas Huth 
2506be0a4fafSBharata B Rao #define VSX_SCALAR_CMPQ(op, ordered)                                    \
25076ae4a57aSMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
25086ae4a57aSMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                          \
2509be0a4fafSBharata B Rao {                                                                       \
2510be0a4fafSBharata B Rao     uint32_t cc = 0;                                                    \
2511be0a4fafSBharata B Rao     bool vxsnan_flag = false, vxvc_flag = false;                        \
2512be0a4fafSBharata B Rao                                                                         \
2513be0a4fafSBharata B Rao     helper_reset_fpstatus(env);                                         \
2514be0a4fafSBharata B Rao                                                                         \
2515cf3b0334SMark Cave-Ayland     if (float128_is_signaling_nan(xa->f128, &env->fp_status) ||         \
2516cf3b0334SMark Cave-Ayland         float128_is_signaling_nan(xb->f128, &env->fp_status)) {         \
2517be0a4fafSBharata B Rao         vxsnan_flag = true;                                             \
2518be0a4fafSBharata B Rao         cc = CRF_SO;                                                    \
2519be0a4fafSBharata B Rao         if (fpscr_ve == 0 && ordered) {                                 \
2520be0a4fafSBharata B Rao             vxvc_flag = true;                                           \
2521be0a4fafSBharata B Rao         }                                                               \
2522cf3b0334SMark Cave-Ayland     } else if (float128_is_quiet_nan(xa->f128, &env->fp_status) ||      \
2523cf3b0334SMark Cave-Ayland                float128_is_quiet_nan(xb->f128, &env->fp_status)) {      \
2524be0a4fafSBharata B Rao         cc = CRF_SO;                                                    \
2525be0a4fafSBharata B Rao         if (ordered) {                                                  \
2526be0a4fafSBharata B Rao             vxvc_flag = true;                                           \
2527be0a4fafSBharata B Rao         }                                                               \
2528be0a4fafSBharata B Rao     }                                                                   \
2529be0a4fafSBharata B Rao     if (vxsnan_flag) {                                                  \
253013c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                          \
2531be0a4fafSBharata B Rao     }                                                                   \
2532be0a4fafSBharata B Rao     if (vxvc_flag) {                                                    \
253313c9115fSRichard Henderson         float_invalid_op_vxvc(env, 0, GETPC());                         \
2534be0a4fafSBharata B Rao     }                                                                   \
2535be0a4fafSBharata B Rao                                                                         \
2536cf3b0334SMark Cave-Ayland     if (float128_lt(xa->f128, xb->f128, &env->fp_status)) {             \
2537be0a4fafSBharata B Rao         cc |= CRF_LT;                                                   \
2538cf3b0334SMark Cave-Ayland     } else if (!float128_le(xa->f128, xb->f128, &env->fp_status)) {     \
2539be0a4fafSBharata B Rao         cc |= CRF_GT;                                                   \
2540be0a4fafSBharata B Rao     } else {                                                            \
2541be0a4fafSBharata B Rao         cc |= CRF_EQ;                                                   \
2542be0a4fafSBharata B Rao     }                                                                   \
2543be0a4fafSBharata B Rao                                                                         \
2544be0a4fafSBharata B Rao     env->fpscr &= ~(0x0F << FPSCR_FPRF);                                \
2545be0a4fafSBharata B Rao     env->fpscr |= cc << FPSCR_FPRF;                                     \
2546be0a4fafSBharata B Rao     env->crf[BF(opcode)] = cc;                                          \
2547be0a4fafSBharata B Rao                                                                         \
25486525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
2549be0a4fafSBharata B Rao }
2550be0a4fafSBharata B Rao 
2551be0a4fafSBharata B Rao VSX_SCALAR_CMPQ(xscmpoqp, 1)
2552be0a4fafSBharata B Rao VSX_SCALAR_CMPQ(xscmpuqp, 0)
2553be0a4fafSBharata B Rao 
2554fa9ebf8cSDavid Gibson /*
2555fa9ebf8cSDavid Gibson  * VSX_MAX_MIN - VSX floating point maximum/minimum
2556fcf5ef2aSThomas Huth  *   name  - instruction mnemonic
2557fcf5ef2aSThomas Huth  *   op    - operation (max or min)
2558fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2559fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2560fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2561fcf5ef2aSThomas Huth  */
2562fcf5ef2aSThomas Huth #define VSX_MAX_MIN(name, op, nels, tp, fld)                                  \
256399125c74SMark Cave-Ayland void helper_##name(CPUPPCState *env, ppc_vsr_t *xt,                           \
256499125c74SMark Cave-Ayland                    ppc_vsr_t *xa, ppc_vsr_t *xb)                              \
2565fcf5ef2aSThomas Huth {                                                                             \
2566cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2567fcf5ef2aSThomas Huth     int i;                                                                    \
2568fcf5ef2aSThomas Huth                                                                               \
2569fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2570cf3b0334SMark Cave-Ayland         t.fld = tp##_##op(xa->fld, xb->fld, &env->fp_status);                 \
2571cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xa->fld, &env->fp_status) ||       \
2572cf3b0334SMark Cave-Ayland                      tp##_is_signaling_nan(xb->fld, &env->fp_status))) {      \
257313c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
2574fcf5ef2aSThomas Huth         }                                                                     \
2575fcf5ef2aSThomas Huth     }                                                                         \
2576fcf5ef2aSThomas Huth                                                                               \
2577cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
25786525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2579fcf5ef2aSThomas Huth }
2580fcf5ef2aSThomas Huth 
2581fcf5ef2aSThomas Huth VSX_MAX_MIN(xsmaxdp, maxnum, 1, float64, VsrD(0))
2582fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmaxdp, maxnum, 2, float64, VsrD(i))
2583fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmaxsp, maxnum, 4, float32, VsrW(i))
2584fcf5ef2aSThomas Huth VSX_MAX_MIN(xsmindp, minnum, 1, float64, VsrD(0))
2585fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmindp, minnum, 2, float64, VsrD(i))
2586fcf5ef2aSThomas Huth VSX_MAX_MIN(xvminsp, minnum, 4, float32, VsrW(i))
2587fcf5ef2aSThomas Huth 
25882770deedSBharata B Rao #define VSX_MAX_MINC(name, max)                                               \
258923d0766bSMark Cave-Ayland void helper_##name(CPUPPCState *env, uint32_t opcode,                         \
259023d0766bSMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)               \
25912770deedSBharata B Rao {                                                                             \
2592cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
25932770deedSBharata B Rao     bool vxsnan_flag = false, vex_flag = false;                               \
25942770deedSBharata B Rao                                                                               \
2595cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)) ||                           \
2596cf3b0334SMark Cave-Ayland                  float64_is_any_nan(xb->VsrD(0)))) {                          \
2597cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||         \
2598cf3b0334SMark Cave-Ayland             float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {         \
25992770deedSBharata B Rao             vxsnan_flag = true;                                               \
26002770deedSBharata B Rao         }                                                                     \
2601cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
26022770deedSBharata B Rao     } else if ((max &&                                                        \
2603cf3b0334SMark Cave-Ayland                !float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) ||     \
26042770deedSBharata B Rao                (!max &&                                                       \
2605cf3b0334SMark Cave-Ayland                float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status))) {      \
2606cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
26072770deedSBharata B Rao     } else {                                                                  \
2608cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
26092770deedSBharata B Rao     }                                                                         \
26102770deedSBharata B Rao                                                                               \
26112770deedSBharata B Rao     vex_flag = fpscr_ve & vxsnan_flag;                                        \
26122770deedSBharata B Rao     if (vxsnan_flag) {                                                        \
261313c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
26142770deedSBharata B Rao     }                                                                         \
26152770deedSBharata B Rao     if (!vex_flag) {                                                          \
2616cf3b0334SMark Cave-Ayland         *xt = t;                                                              \
26172770deedSBharata B Rao     }                                                                         \
26182770deedSBharata B Rao }                                                                             \
26192770deedSBharata B Rao 
26202770deedSBharata B Rao VSX_MAX_MINC(xsmaxcdp, 1);
26212770deedSBharata B Rao VSX_MAX_MINC(xsmincdp, 0);
26222770deedSBharata B Rao 
2623d4ccd87eSBharata B Rao #define VSX_MAX_MINJ(name, max)                                               \
262423d0766bSMark Cave-Ayland void helper_##name(CPUPPCState *env, uint32_t opcode,                         \
262523d0766bSMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)               \
2626d4ccd87eSBharata B Rao {                                                                             \
2627cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2628d4ccd87eSBharata B Rao     bool vxsnan_flag = false, vex_flag = false;                               \
2629d4ccd87eSBharata B Rao                                                                               \
2630cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)))) {                          \
2631cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status)) {         \
2632d4ccd87eSBharata B Rao             vxsnan_flag = true;                                               \
2633d4ccd87eSBharata B Rao         }                                                                     \
2634cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
2635cf3b0334SMark Cave-Ayland     } else if (unlikely(float64_is_any_nan(xb->VsrD(0)))) {                   \
2636cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {         \
2637d4ccd87eSBharata B Rao             vxsnan_flag = true;                                               \
2638d4ccd87eSBharata B Rao         }                                                                     \
2639cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
2640cf3b0334SMark Cave-Ayland     } else if (float64_is_zero(xa->VsrD(0)) &&                                \
2641cf3b0334SMark Cave-Ayland                float64_is_zero(xb->VsrD(0))) {                                \
2642d4ccd87eSBharata B Rao         if (max) {                                                            \
2643cf3b0334SMark Cave-Ayland             if (!float64_is_neg(xa->VsrD(0)) ||                               \
2644cf3b0334SMark Cave-Ayland                 !float64_is_neg(xb->VsrD(0))) {                               \
2645cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0ULL;                                             \
2646d4ccd87eSBharata B Rao             } else {                                                          \
2647cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0x8000000000000000ULL;                            \
2648d4ccd87eSBharata B Rao             }                                                                 \
2649d4ccd87eSBharata B Rao         } else {                                                              \
2650cf3b0334SMark Cave-Ayland             if (float64_is_neg(xa->VsrD(0)) ||                                \
2651cf3b0334SMark Cave-Ayland                 float64_is_neg(xb->VsrD(0))) {                                \
2652cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0x8000000000000000ULL;                            \
2653d4ccd87eSBharata B Rao             } else {                                                          \
2654cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0ULL;                                             \
2655d4ccd87eSBharata B Rao             }                                                                 \
2656d4ccd87eSBharata B Rao         }                                                                     \
2657d4ccd87eSBharata B Rao     } else if ((max &&                                                        \
2658cf3b0334SMark Cave-Ayland                !float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) ||     \
2659d4ccd87eSBharata B Rao                (!max &&                                                       \
2660cf3b0334SMark Cave-Ayland                float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status))) {      \
2661cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
2662d4ccd87eSBharata B Rao     } else {                                                                  \
2663cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
2664d4ccd87eSBharata B Rao     }                                                                         \
2665d4ccd87eSBharata B Rao                                                                               \
2666d4ccd87eSBharata B Rao     vex_flag = fpscr_ve & vxsnan_flag;                                        \
2667d4ccd87eSBharata B Rao     if (vxsnan_flag) {                                                        \
266813c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
2669d4ccd87eSBharata B Rao     }                                                                         \
2670d4ccd87eSBharata B Rao     if (!vex_flag) {                                                          \
2671cf3b0334SMark Cave-Ayland         *xt = t;                                                              \
2672d4ccd87eSBharata B Rao     }                                                                         \
2673d4ccd87eSBharata B Rao }                                                                             \
2674d4ccd87eSBharata B Rao 
2675d4ccd87eSBharata B Rao VSX_MAX_MINJ(xsmaxjdp, 1);
2676d4ccd87eSBharata B Rao VSX_MAX_MINJ(xsminjdp, 0);
2677d4ccd87eSBharata B Rao 
2678fa9ebf8cSDavid Gibson /*
2679fa9ebf8cSDavid Gibson  * VSX_CMP - VSX floating point compare
2680fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2681fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2682fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2683fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2684fcf5ef2aSThomas Huth  *   cmp   - comparison operation
2685fcf5ef2aSThomas Huth  *   svxvc - set VXVC bit
2686fcf5ef2aSThomas Huth  *   exp   - expected result of comparison
2687fcf5ef2aSThomas Huth  */
2688fcf5ef2aSThomas Huth #define VSX_CMP(op, nels, tp, fld, cmp, svxvc, exp)                       \
268900084a25SMark Cave-Ayland uint32_t helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                     \
269000084a25SMark Cave-Ayland                      ppc_vsr_t *xa, ppc_vsr_t *xb)                        \
2691fcf5ef2aSThomas Huth {                                                                         \
2692cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                    \
269300084a25SMark Cave-Ayland     uint32_t crf6 = 0;                                                    \
2694fcf5ef2aSThomas Huth     int i;                                                                \
2695fcf5ef2aSThomas Huth     int all_true = 1;                                                     \
2696fcf5ef2aSThomas Huth     int all_false = 1;                                                    \
2697fcf5ef2aSThomas Huth                                                                           \
2698fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                          \
2699cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_any_nan(xa->fld) ||                          \
2700cf3b0334SMark Cave-Ayland                      tp##_is_any_nan(xb->fld))) {                         \
2701cf3b0334SMark Cave-Ayland             if (tp##_is_signaling_nan(xa->fld, &env->fp_status) ||        \
2702cf3b0334SMark Cave-Ayland                 tp##_is_signaling_nan(xb->fld, &env->fp_status)) {        \
270313c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                    \
2704fcf5ef2aSThomas Huth             }                                                             \
2705fcf5ef2aSThomas Huth             if (svxvc) {                                                  \
270613c9115fSRichard Henderson                 float_invalid_op_vxvc(env, 0, GETPC());                   \
2707fcf5ef2aSThomas Huth             }                                                             \
2708cf3b0334SMark Cave-Ayland             t.fld = 0;                                                    \
2709fcf5ef2aSThomas Huth             all_true = 0;                                                 \
2710fcf5ef2aSThomas Huth         } else {                                                          \
2711cf3b0334SMark Cave-Ayland             if (tp##_##cmp(xb->fld, xa->fld, &env->fp_status) == exp) {   \
2712cf3b0334SMark Cave-Ayland                 t.fld = -1;                                               \
2713fcf5ef2aSThomas Huth                 all_false = 0;                                            \
2714fcf5ef2aSThomas Huth             } else {                                                      \
2715cf3b0334SMark Cave-Ayland                 t.fld = 0;                                                \
2716fcf5ef2aSThomas Huth                 all_true = 0;                                             \
2717fcf5ef2aSThomas Huth             }                                                             \
2718fcf5ef2aSThomas Huth         }                                                                 \
2719fcf5ef2aSThomas Huth     }                                                                     \
2720fcf5ef2aSThomas Huth                                                                           \
2721cf3b0334SMark Cave-Ayland     *xt = t;                                                              \
272200084a25SMark Cave-Ayland     crf6 = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0);                  \
272300084a25SMark Cave-Ayland     return crf6;                                                          \
2724fcf5ef2aSThomas Huth }
2725fcf5ef2aSThomas Huth 
2726fcf5ef2aSThomas Huth VSX_CMP(xvcmpeqdp, 2, float64, VsrD(i), eq, 0, 1)
2727fcf5ef2aSThomas Huth VSX_CMP(xvcmpgedp, 2, float64, VsrD(i), le, 1, 1)
2728fcf5ef2aSThomas Huth VSX_CMP(xvcmpgtdp, 2, float64, VsrD(i), lt, 1, 1)
2729fcf5ef2aSThomas Huth VSX_CMP(xvcmpnedp, 2, float64, VsrD(i), eq, 0, 0)
2730fcf5ef2aSThomas Huth VSX_CMP(xvcmpeqsp, 4, float32, VsrW(i), eq, 0, 1)
2731fcf5ef2aSThomas Huth VSX_CMP(xvcmpgesp, 4, float32, VsrW(i), le, 1, 1)
2732fcf5ef2aSThomas Huth VSX_CMP(xvcmpgtsp, 4, float32, VsrW(i), lt, 1, 1)
2733fcf5ef2aSThomas Huth VSX_CMP(xvcmpnesp, 4, float32, VsrW(i), eq, 0, 0)
2734fcf5ef2aSThomas Huth 
2735fa9ebf8cSDavid Gibson /*
2736fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2737fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2738fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2739fcf5ef2aSThomas Huth  *   stp   - source type (float32 or float64)
2740fcf5ef2aSThomas Huth  *   ttp   - target type (float32 or float64)
2741fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2742fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field (f32 or f64)
2743fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2744fcf5ef2aSThomas Huth  */
2745fcf5ef2aSThomas Huth #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf)    \
274675cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)   \
2747fcf5ef2aSThomas Huth {                                                                  \
2748cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                             \
2749fcf5ef2aSThomas Huth     int i;                                                         \
2750fcf5ef2aSThomas Huth                                                                    \
2751fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                   \
2752cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);        \
2753cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,              \
2754fcf5ef2aSThomas Huth                                             &env->fp_status))) {   \
275513c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                 \
2756cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                   \
2757fcf5ef2aSThomas Huth         }                                                          \
2758fcf5ef2aSThomas Huth         if (sfprf) {                                               \
2759cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                \
2760fcf5ef2aSThomas Huth         }                                                          \
2761fcf5ef2aSThomas Huth     }                                                              \
2762fcf5ef2aSThomas Huth                                                                    \
2763cf3b0334SMark Cave-Ayland     *xt = t;                                                       \
27646525aadcSRichard Henderson     do_float_check_status(env, GETPC());                           \
2765fcf5ef2aSThomas Huth }
2766fcf5ef2aSThomas Huth 
2767fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xscvdpsp, 1, float64, float32, VsrD(0), VsrW(0), 1)
2768fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xscvspdp, 1, float32, float64, VsrW(0), VsrD(0), 1)
2769fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xvcvdpsp, 2, float64, float32, VsrD(i), VsrW(2 * i), 0)
2770fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xvcvspdp, 2, float32, float64, VsrW(2 * i), VsrD(i), 0)
2771fcf5ef2aSThomas Huth 
2772fa9ebf8cSDavid Gibson /*
2773fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP_VECTOR - VSX floating point/floating point conversion
2774e5487803SBharata B Rao  *   op    - instruction mnemonic
2775e5487803SBharata B Rao  *   nels  - number of elements (1, 2 or 4)
2776e5487803SBharata B Rao  *   stp   - source type (float32 or float64)
2777e5487803SBharata B Rao  *   ttp   - target type (float32 or float64)
2778e5487803SBharata B Rao  *   sfld  - source vsr_t field
2779e5487803SBharata B Rao  *   tfld  - target vsr_t field (f32 or f64)
2780e5487803SBharata B Rao  *   sfprf - set FPRF
2781e5487803SBharata B Rao  */
2782e5487803SBharata B Rao #define VSX_CVT_FP_TO_FP_VECTOR(op, nels, stp, ttp, sfld, tfld, sfprf)    \
278399229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                       \
278499229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                            \
2785e5487803SBharata B Rao {                                                                       \
2786cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
2787e5487803SBharata B Rao     int i;                                                              \
2788e5487803SBharata B Rao                                                                         \
2789e5487803SBharata B Rao     for (i = 0; i < nels; i++) {                                        \
2790cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);             \
2791cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,                   \
2792e5487803SBharata B Rao                                             &env->fp_status))) {        \
279313c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                      \
2794cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                        \
2795e5487803SBharata B Rao         }                                                               \
2796e5487803SBharata B Rao         if (sfprf) {                                                    \
2797cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                     \
2798e5487803SBharata B Rao         }                                                               \
2799e5487803SBharata B Rao     }                                                                   \
2800e5487803SBharata B Rao                                                                         \
2801cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
28026525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
2803e5487803SBharata B Rao }
2804e5487803SBharata B Rao 
2805e5487803SBharata B Rao VSX_CVT_FP_TO_FP_VECTOR(xscvdpqp, 1, float64, float128, VsrD(0), f128, 1)
2806e5487803SBharata B Rao 
2807fa9ebf8cSDavid Gibson /*
2808fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP_HP - VSX floating point/floating point conversion
2809f566c047SBharata B Rao  *                       involving one half precision value
2810f566c047SBharata B Rao  *   op    - instruction mnemonic
28118b920d8aSNikunj A Dadhania  *   nels  - number of elements (1, 2 or 4)
2812f566c047SBharata B Rao  *   stp   - source type
2813f566c047SBharata B Rao  *   ttp   - target type
2814f566c047SBharata B Rao  *   sfld  - source vsr_t field
2815f566c047SBharata B Rao  *   tfld  - target vsr_t field
28168b920d8aSNikunj A Dadhania  *   sfprf - set FPRF
2817f566c047SBharata B Rao  */
28188b920d8aSNikunj A Dadhania #define VSX_CVT_FP_TO_FP_HP(op, nels, stp, ttp, sfld, tfld, sfprf) \
281975cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)   \
2820f566c047SBharata B Rao {                                                                  \
2821cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                             \
28228b920d8aSNikunj A Dadhania     int i;                                                         \
2823f566c047SBharata B Rao                                                                    \
28248b920d8aSNikunj A Dadhania     for (i = 0; i < nels; i++) {                                   \
2825cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, 1, &env->fp_status);     \
2826cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,              \
2827f566c047SBharata B Rao                                             &env->fp_status))) {   \
282813c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                 \
2829cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                   \
2830f566c047SBharata B Rao         }                                                          \
28318b920d8aSNikunj A Dadhania         if (sfprf) {                                               \
2832cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                \
28338b920d8aSNikunj A Dadhania         }                                                          \
28348b920d8aSNikunj A Dadhania     }                                                              \
2835f566c047SBharata B Rao                                                                    \
2836cf3b0334SMark Cave-Ayland     *xt = t;                                                       \
28376525aadcSRichard Henderson     do_float_check_status(env, GETPC());                           \
2838f566c047SBharata B Rao }
2839f566c047SBharata B Rao 
28408b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xscvdphp, 1, float64, float16, VsrD(0), VsrH(3), 1)
28418b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xscvhpdp, 1, float16, float64, VsrH(3), VsrD(0), 1)
28428b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xvcvsphp, 4, float32, float16, VsrW(i), VsrH(2 * i  + 1), 0)
28438b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xvcvhpsp, 4, float16, float32, VsrH(2 * i + 1), VsrW(i), 0)
2844f566c047SBharata B Rao 
28452a084dadSBharata B Rao /*
28462a084dadSBharata B Rao  * xscvqpdp isn't using VSX_CVT_FP_TO_FP() because xscvqpdpo will be
28472a084dadSBharata B Rao  * added to this later.
28482a084dadSBharata B Rao  */
2849e0d6a362SMark Cave-Ayland void helper_xscvqpdp(CPUPPCState *env, uint32_t opcode,
2850e0d6a362SMark Cave-Ayland                      ppc_vsr_t *xt, ppc_vsr_t *xb)
28512a084dadSBharata B Rao {
2852cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
2853a8d411abSBharata B Rao     float_status tstat;
28542a084dadSBharata B Rao 
2855a8d411abSBharata B Rao     tstat = env->fp_status;
28562a084dadSBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
2857a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
28582a084dadSBharata B Rao     }
28592a084dadSBharata B Rao 
2860cf3b0334SMark Cave-Ayland     t.VsrD(0) = float128_to_float64(xb->f128, &tstat);
2861a8d411abSBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
2862cf3b0334SMark Cave-Ayland     if (unlikely(float128_is_signaling_nan(xb->f128, &tstat))) {
286313c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
2864cf3b0334SMark Cave-Ayland         t.VsrD(0) = float64_snan_to_qnan(t.VsrD(0));
28652a084dadSBharata B Rao     }
2866cf3b0334SMark Cave-Ayland     helper_compute_fprf_float64(env, t.VsrD(0));
28672a084dadSBharata B Rao 
2868cf3b0334SMark Cave-Ayland     *xt = t;
28696525aadcSRichard Henderson     do_float_check_status(env, GETPC());
28702a084dadSBharata B Rao }
28712a084dadSBharata B Rao 
2872fcf5ef2aSThomas Huth uint64_t helper_xscvdpspn(CPUPPCState *env, uint64_t xb)
2873fcf5ef2aSThomas Huth {
2874*e6f1bfb2SPaul A. Clarke     uint64_t result;
2875*e6f1bfb2SPaul A. Clarke 
2876fcf5ef2aSThomas Huth     float_status tstat = env->fp_status;
2877fcf5ef2aSThomas Huth     set_float_exception_flags(0, &tstat);
2878fcf5ef2aSThomas Huth 
2879*e6f1bfb2SPaul A. Clarke     result = (uint64_t)float64_to_float32(xb, &tstat);
2880*e6f1bfb2SPaul A. Clarke     /* hardware replicates result to both words of the doubleword result.  */
2881*e6f1bfb2SPaul A. Clarke     return (result << 32) | result;
2882fcf5ef2aSThomas Huth }
2883fcf5ef2aSThomas Huth 
2884fcf5ef2aSThomas Huth uint64_t helper_xscvspdpn(CPUPPCState *env, uint64_t xb)
2885fcf5ef2aSThomas Huth {
2886fcf5ef2aSThomas Huth     float_status tstat = env->fp_status;
2887fcf5ef2aSThomas Huth     set_float_exception_flags(0, &tstat);
2888fcf5ef2aSThomas Huth 
2889fcf5ef2aSThomas Huth     return float32_to_float64(xb >> 32, &tstat);
2890fcf5ef2aSThomas Huth }
2891fcf5ef2aSThomas Huth 
2892fa9ebf8cSDavid Gibson /*
2893fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2894fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2895fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2896fcf5ef2aSThomas Huth  *   stp   - source type (float32 or float64)
2897fcf5ef2aSThomas Huth  *   ttp   - target type (int32, uint32, int64 or uint64)
2898fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2899fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field
2900fcf5ef2aSThomas Huth  *   rnan  - resulting NaN
2901fcf5ef2aSThomas Huth  */
2902fcf5ef2aSThomas Huth #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, rnan)              \
290375cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2904fcf5ef2aSThomas Huth {                                                                            \
2905a3dec427SRichard Henderson     int all_flags = env->fp_status.float_exception_flags, flags;             \
2906cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
2907fcf5ef2aSThomas Huth     int i;                                                                   \
2908fcf5ef2aSThomas Huth                                                                              \
2909fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
2910a3dec427SRichard Henderson         env->fp_status.float_exception_flags = 0;                            \
2911cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp##_round_to_zero(xb->sfld, &env->fp_status);  \
2912a3dec427SRichard Henderson         flags = env->fp_status.float_exception_flags;                        \
2913a3dec427SRichard Henderson         if (unlikely(flags & float_flag_invalid)) {                          \
2914cf3b0334SMark Cave-Ayland             float_invalid_cvt(env, 0, GETPC(), stp##_classify(xb->sfld));    \
2915cf3b0334SMark Cave-Ayland             t.tfld = rnan;                                                   \
2916fcf5ef2aSThomas Huth         }                                                                    \
2917a3dec427SRichard Henderson         all_flags |= flags;                                                  \
2918fcf5ef2aSThomas Huth     }                                                                        \
2919fcf5ef2aSThomas Huth                                                                              \
2920cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
2921a3dec427SRichard Henderson     env->fp_status.float_exception_flags = all_flags;                        \
29226525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
2923fcf5ef2aSThomas Huth }
2924fcf5ef2aSThomas Huth 
2925fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpsxds, 1, float64, int64, VsrD(0), VsrD(0), \
2926fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
2927fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpsxws, 1, float64, int32, VsrD(0), VsrW(1), \
2928fcf5ef2aSThomas Huth                   0x80000000U)
2929fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpuxds, 1, float64, uint64, VsrD(0), VsrD(0), 0ULL)
2930fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpuxws, 1, float64, uint32, VsrD(0), VsrW(1), 0U)
2931fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpsxds, 2, float64, int64, VsrD(i), VsrD(i), \
2932fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
2933fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpsxws, 2, float64, int32, VsrD(i), VsrW(2 * i), \
2934fcf5ef2aSThomas Huth                   0x80000000U)
2935fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpuxds, 2, float64, uint64, VsrD(i), VsrD(i), 0ULL)
2936fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpuxws, 2, float64, uint32, VsrD(i), VsrW(2 * i), 0U)
2937fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspsxds, 2, float32, int64, VsrW(2 * i), VsrD(i), \
2938fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
2939fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspsxws, 4, float32, int32, VsrW(i), VsrW(i), 0x80000000U)
2940fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspuxds, 2, float32, uint64, VsrW(2 * i), VsrD(i), 0ULL)
2941fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspuxws, 4, float32, uint32, VsrW(i), VsrW(i), 0U)
2942fcf5ef2aSThomas Huth 
2943fa9ebf8cSDavid Gibson /*
2944fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_INT_VECTOR - VSX floating point to integer conversion
294505590b92SBharata B Rao  *   op    - instruction mnemonic
294605590b92SBharata B Rao  *   stp   - source type (float32 or float64)
294705590b92SBharata B Rao  *   ttp   - target type (int32, uint32, int64 or uint64)
294805590b92SBharata B Rao  *   sfld  - source vsr_t field
294905590b92SBharata B Rao  *   tfld  - target vsr_t field
295005590b92SBharata B Rao  *   rnan  - resulting NaN
295105590b92SBharata B Rao  */
295205590b92SBharata B Rao #define VSX_CVT_FP_TO_INT_VECTOR(op, stp, ttp, sfld, tfld, rnan)             \
295399229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                          \
295499229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                               \
295505590b92SBharata B Rao {                                                                            \
2956cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                                       \
295705590b92SBharata B Rao                                                                              \
2958cf3b0334SMark Cave-Ayland     t.tfld = stp##_to_##ttp##_round_to_zero(xb->sfld, &env->fp_status);      \
295905590b92SBharata B Rao     if (env->fp_status.float_exception_flags & float_flag_invalid) {         \
2960cf3b0334SMark Cave-Ayland         float_invalid_cvt(env, 0, GETPC(), stp##_classify(xb->sfld));        \
2961cf3b0334SMark Cave-Ayland         t.tfld = rnan;                                                       \
296205590b92SBharata B Rao     }                                                                        \
296305590b92SBharata B Rao                                                                              \
2964cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
29656525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
296605590b92SBharata B Rao }
296705590b92SBharata B Rao 
296805590b92SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpsdz, float128, int64, f128, VsrD(0),          \
296905590b92SBharata B Rao                   0x8000000000000000ULL)
297005590b92SBharata B Rao 
297105590b92SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpswz, float128, int32, f128, VsrD(0),          \
297205590b92SBharata B Rao                   0xffffffff80000000ULL)
2973e0aee726SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpudz, float128, uint64, f128, VsrD(0), 0x0ULL)
2974e0aee726SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpuwz, float128, uint32, f128, VsrD(0), 0x0ULL)
297505590b92SBharata B Rao 
2976fa9ebf8cSDavid Gibson /*
2977fa9ebf8cSDavid Gibson  * VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
2978fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2979fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2980fcf5ef2aSThomas Huth  *   stp   - source type (int32, uint32, int64 or uint64)
2981fcf5ef2aSThomas Huth  *   ttp   - target type (float32 or float64)
2982fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2983fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field
2984fcf5ef2aSThomas Huth  *   jdef  - definition of the j index (i or 2*i)
2985fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2986fcf5ef2aSThomas Huth  */
2987fcf5ef2aSThomas Huth #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf, r2sp)  \
298875cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)        \
2989fcf5ef2aSThomas Huth {                                                                       \
2990cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
2991fcf5ef2aSThomas Huth     int i;                                                              \
2992fcf5ef2aSThomas Huth                                                                         \
2993fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2994cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);             \
2995fcf5ef2aSThomas Huth         if (r2sp) {                                                     \
2996cf3b0334SMark Cave-Ayland             t.tfld = helper_frsp(env, t.tfld);                          \
2997fcf5ef2aSThomas Huth         }                                                               \
2998fcf5ef2aSThomas Huth         if (sfprf) {                                                    \
2999cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.tfld);                   \
3000fcf5ef2aSThomas Huth         }                                                               \
3001fcf5ef2aSThomas Huth     }                                                                   \
3002fcf5ef2aSThomas Huth                                                                         \
3003cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
30046525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
3005fcf5ef2aSThomas Huth }
3006fcf5ef2aSThomas Huth 
3007fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvsxddp, 1, int64, float64, VsrD(0), VsrD(0), 1, 0)
3008fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvuxddp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 0)
3009fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvsxdsp, 1, int64, float64, VsrD(0), VsrD(0), 1, 1)
3010fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvuxdsp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 1)
3011fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxddp, 2, int64, float64, VsrD(i), VsrD(i), 0, 0)
3012fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxddp, 2, uint64, float64, VsrD(i), VsrD(i), 0, 0)
3013fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxwdp, 2, int32, float64, VsrW(2 * i), VsrD(i), 0, 0)
3014fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxwdp, 2, uint64, float64, VsrW(2 * i), VsrD(i), 0, 0)
3015fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxdsp, 2, int64, float32, VsrD(i), VsrW(2 * i), 0, 0)
3016fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxdsp, 2, uint64, float32, VsrD(i), VsrW(2 * i), 0, 0)
3017fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxwsp, 4, int32, float32, VsrW(i), VsrW(i), 0, 0)
3018fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxwsp, 4, uint32, float32, VsrW(i), VsrW(i), 0, 0)
3019fcf5ef2aSThomas Huth 
3020fa9ebf8cSDavid Gibson /*
3021fa9ebf8cSDavid Gibson  * VSX_CVT_INT_TO_FP_VECTOR - VSX integer to floating point conversion
302248ef23cbSBharata B Rao  *   op    - instruction mnemonic
302348ef23cbSBharata B Rao  *   stp   - source type (int32, uint32, int64 or uint64)
302448ef23cbSBharata B Rao  *   ttp   - target type (float32 or float64)
302548ef23cbSBharata B Rao  *   sfld  - source vsr_t field
302648ef23cbSBharata B Rao  *   tfld  - target vsr_t field
302748ef23cbSBharata B Rao  */
302848ef23cbSBharata B Rao #define VSX_CVT_INT_TO_FP_VECTOR(op, stp, ttp, sfld, tfld)              \
302999229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
303099229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                          \
303148ef23cbSBharata B Rao {                                                                       \
3032cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
303348ef23cbSBharata B Rao                                                                         \
3034cf3b0334SMark Cave-Ayland     t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);                 \
3035cf3b0334SMark Cave-Ayland     helper_compute_fprf_##ttp(env, t.tfld);                             \
303648ef23cbSBharata B Rao                                                                         \
3037cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
30386525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
303948ef23cbSBharata B Rao }
304048ef23cbSBharata B Rao 
304148ef23cbSBharata B Rao VSX_CVT_INT_TO_FP_VECTOR(xscvsdqp, int64, float128, VsrD(0), f128)
304248ef23cbSBharata B Rao VSX_CVT_INT_TO_FP_VECTOR(xscvudqp, uint64, float128, VsrD(0), f128)
304348ef23cbSBharata B Rao 
3044fa9ebf8cSDavid Gibson /*
3045fa9ebf8cSDavid Gibson  * For "use current rounding mode", define a value that will not be
3046fa9ebf8cSDavid Gibson  * one of the existing rounding model enums.
3047fcf5ef2aSThomas Huth  */
3048fcf5ef2aSThomas Huth #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
3049fcf5ef2aSThomas Huth   float_round_up + float_round_to_zero)
3050fcf5ef2aSThomas Huth 
3051fa9ebf8cSDavid Gibson /*
3052fa9ebf8cSDavid Gibson  * VSX_ROUND - VSX floating point round
3053fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
3054fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
3055fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
3056fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
3057fcf5ef2aSThomas Huth  *   rmode - rounding mode
3058fcf5ef2aSThomas Huth  *   sfprf - set FPRF
3059fcf5ef2aSThomas Huth  */
3060fcf5ef2aSThomas Huth #define VSX_ROUND(op, nels, tp, fld, rmode, sfprf)                     \
306175cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)       \
3062fcf5ef2aSThomas Huth {                                                                      \
3063cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                 \
3064fcf5ef2aSThomas Huth     int i;                                                             \
3065fcf5ef2aSThomas Huth                                                                        \
3066fcf5ef2aSThomas Huth     if (rmode != FLOAT_ROUND_CURRENT) {                                \
3067fcf5ef2aSThomas Huth         set_float_rounding_mode(rmode, &env->fp_status);               \
3068fcf5ef2aSThomas Huth     }                                                                  \
3069fcf5ef2aSThomas Huth                                                                        \
3070fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                       \
3071cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xb->fld,                    \
3072fcf5ef2aSThomas Huth                                            &env->fp_status))) {        \
307313c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                     \
3074cf3b0334SMark Cave-Ayland             t.fld = tp##_snan_to_qnan(xb->fld);                        \
3075fcf5ef2aSThomas Huth         } else {                                                       \
3076cf3b0334SMark Cave-Ayland             t.fld = tp##_round_to_int(xb->fld, &env->fp_status);       \
3077fcf5ef2aSThomas Huth         }                                                              \
3078fcf5ef2aSThomas Huth         if (sfprf) {                                                   \
3079cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                   \
3080fcf5ef2aSThomas Huth         }                                                              \
3081fcf5ef2aSThomas Huth     }                                                                  \
3082fcf5ef2aSThomas Huth                                                                        \
3083fa9ebf8cSDavid Gibson     /*                                                                 \
3084fa9ebf8cSDavid Gibson      * If this is not a "use current rounding mode" instruction,       \
3085fcf5ef2aSThomas Huth      * then inhibit setting of the XX bit and restore rounding         \
3086fa9ebf8cSDavid Gibson      * mode from FPSCR                                                 \
3087fa9ebf8cSDavid Gibson      */                                                                \
3088fcf5ef2aSThomas Huth     if (rmode != FLOAT_ROUND_CURRENT) {                                \
3089fcf5ef2aSThomas Huth         fpscr_set_rounding_mode(env);                                  \
3090fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags &= ~float_flag_inexact;   \
3091fcf5ef2aSThomas Huth     }                                                                  \
3092fcf5ef2aSThomas Huth                                                                        \
3093cf3b0334SMark Cave-Ayland     *xt = t;                                                           \
30946525aadcSRichard Henderson     do_float_check_status(env, GETPC());                               \
3095fcf5ef2aSThomas Huth }
3096fcf5ef2aSThomas Huth 
3097fcf5ef2aSThomas Huth VSX_ROUND(xsrdpi, 1, float64, VsrD(0), float_round_ties_away, 1)
3098fcf5ef2aSThomas Huth VSX_ROUND(xsrdpic, 1, float64, VsrD(0), FLOAT_ROUND_CURRENT, 1)
3099fcf5ef2aSThomas Huth VSX_ROUND(xsrdpim, 1, float64, VsrD(0), float_round_down, 1)
3100fcf5ef2aSThomas Huth VSX_ROUND(xsrdpip, 1, float64, VsrD(0), float_round_up, 1)
3101fcf5ef2aSThomas Huth VSX_ROUND(xsrdpiz, 1, float64, VsrD(0), float_round_to_zero, 1)
3102fcf5ef2aSThomas Huth 
3103fcf5ef2aSThomas Huth VSX_ROUND(xvrdpi, 2, float64, VsrD(i), float_round_ties_away, 0)
3104fcf5ef2aSThomas Huth VSX_ROUND(xvrdpic, 2, float64, VsrD(i), FLOAT_ROUND_CURRENT, 0)
3105fcf5ef2aSThomas Huth VSX_ROUND(xvrdpim, 2, float64, VsrD(i), float_round_down, 0)
3106fcf5ef2aSThomas Huth VSX_ROUND(xvrdpip, 2, float64, VsrD(i), float_round_up, 0)
3107fcf5ef2aSThomas Huth VSX_ROUND(xvrdpiz, 2, float64, VsrD(i), float_round_to_zero, 0)
3108fcf5ef2aSThomas Huth 
3109fcf5ef2aSThomas Huth VSX_ROUND(xvrspi, 4, float32, VsrW(i), float_round_ties_away, 0)
3110fcf5ef2aSThomas Huth VSX_ROUND(xvrspic, 4, float32, VsrW(i), FLOAT_ROUND_CURRENT, 0)
3111fcf5ef2aSThomas Huth VSX_ROUND(xvrspim, 4, float32, VsrW(i), float_round_down, 0)
3112fcf5ef2aSThomas Huth VSX_ROUND(xvrspip, 4, float32, VsrW(i), float_round_up, 0)
3113fcf5ef2aSThomas Huth VSX_ROUND(xvrspiz, 4, float32, VsrW(i), float_round_to_zero, 0)
3114fcf5ef2aSThomas Huth 
3115fcf5ef2aSThomas Huth uint64_t helper_xsrsp(CPUPPCState *env, uint64_t xb)
3116fcf5ef2aSThomas Huth {
3117fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);
3118fcf5ef2aSThomas Huth 
3119fcf5ef2aSThomas Huth     uint64_t xt = helper_frsp(env, xb);
3120fcf5ef2aSThomas Huth 
3121ffc67420SBharata B Rao     helper_compute_fprf_float64(env, xt);
31226525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3123fcf5ef2aSThomas Huth     return xt;
3124fcf5ef2aSThomas Huth }
3125234068abSBharata B Rao 
3126234068abSBharata B Rao #define VSX_XXPERM(op, indexed)                                       \
312799125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                     \
312899125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *pcv)                       \
3129234068abSBharata B Rao {                                                                     \
3130cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                \
3131234068abSBharata B Rao     int i, idx;                                                       \
3132234068abSBharata B Rao                                                                       \
3133234068abSBharata B Rao     for (i = 0; i < 16; i++) {                                        \
3134cf3b0334SMark Cave-Ayland         idx = pcv->VsrB(i) & 0x1F;                                    \
3135234068abSBharata B Rao         if (indexed) {                                                \
3136234068abSBharata B Rao             idx = 31 - idx;                                           \
3137234068abSBharata B Rao         }                                                             \
3138cf3b0334SMark Cave-Ayland         t.VsrB(i) = (idx <= 15) ? xa->VsrB(idx)                       \
3139cf3b0334SMark Cave-Ayland                                 : xt->VsrB(idx - 16);                 \
3140234068abSBharata B Rao     }                                                                 \
3141cf3b0334SMark Cave-Ayland     *xt = t;                                                          \
3142234068abSBharata B Rao }
3143234068abSBharata B Rao 
3144234068abSBharata B Rao VSX_XXPERM(xxperm, 0)
3145234068abSBharata B Rao VSX_XXPERM(xxpermr, 1)
3146c5969d2eSNikunj A Dadhania 
314775cf84cbSMark Cave-Ayland void helper_xvxsigsp(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)
3148c5969d2eSNikunj A Dadhania {
3149cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3150c5969d2eSNikunj A Dadhania     uint32_t exp, i, fraction;
3151c5969d2eSNikunj A Dadhania 
3152c5969d2eSNikunj A Dadhania     for (i = 0; i < 4; i++) {
3153cf3b0334SMark Cave-Ayland         exp = (xb->VsrW(i) >> 23) & 0xFF;
3154cf3b0334SMark Cave-Ayland         fraction = xb->VsrW(i) & 0x7FFFFF;
3155c5969d2eSNikunj A Dadhania         if (exp != 0 && exp != 255) {
3156cf3b0334SMark Cave-Ayland             t.VsrW(i) = fraction | 0x00800000;
3157c5969d2eSNikunj A Dadhania         } else {
3158cf3b0334SMark Cave-Ayland             t.VsrW(i) = fraction;
3159c5969d2eSNikunj A Dadhania         }
3160c5969d2eSNikunj A Dadhania     }
3161cf3b0334SMark Cave-Ayland     *xt = t;
3162c5969d2eSNikunj A Dadhania }
3163403a884aSNikunj A Dadhania 
3164fa9ebf8cSDavid Gibson /*
3165fa9ebf8cSDavid Gibson  * VSX_TEST_DC - VSX floating point test data class
3166403a884aSNikunj A Dadhania  *   op    - instruction mnemonic
3167403a884aSNikunj A Dadhania  *   nels  - number of elements (1, 2 or 4)
3168403a884aSNikunj A Dadhania  *   xbn   - VSR register number
3169403a884aSNikunj A Dadhania  *   tp    - type (float32 or float64)
3170403a884aSNikunj A Dadhania  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
3171403a884aSNikunj A Dadhania  *   tfld   - target vsr_t field (VsrD(*) or VsrW(*))
3172403a884aSNikunj A Dadhania  *   fld_max - target field max
317378241762SNikunj A Dadhania  *   scrf - set result in CR and FPCC
3174403a884aSNikunj A Dadhania  */
317578241762SNikunj A Dadhania #define VSX_TEST_DC(op, nels, xbn, tp, fld, tfld, fld_max, scrf)  \
3176403a884aSNikunj A Dadhania void helper_##op(CPUPPCState *env, uint32_t opcode)         \
3177403a884aSNikunj A Dadhania {                                                           \
3178cf3b0334SMark Cave-Ayland     ppc_vsr_t *xt = &env->vsr[xT(opcode)];                  \
3179cf3b0334SMark Cave-Ayland     ppc_vsr_t *xb = &env->vsr[xbn];                         \
3180cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                      \
3181403a884aSNikunj A Dadhania     uint32_t i, sign, dcmx;                                 \
318278241762SNikunj A Dadhania     uint32_t cc, match = 0;                                 \
3183403a884aSNikunj A Dadhania                                                             \
318478241762SNikunj A Dadhania     if (!scrf) {                                            \
3185403a884aSNikunj A Dadhania         dcmx = DCMX_XV(opcode);                             \
318678241762SNikunj A Dadhania     } else {                                                \
3187cf3b0334SMark Cave-Ayland         t = *xt;                                            \
318878241762SNikunj A Dadhania         dcmx = DCMX(opcode);                                \
318978241762SNikunj A Dadhania     }                                                       \
3190403a884aSNikunj A Dadhania                                                             \
3191403a884aSNikunj A Dadhania     for (i = 0; i < nels; i++) {                            \
3192cf3b0334SMark Cave-Ayland         sign = tp##_is_neg(xb->fld);                        \
3193cf3b0334SMark Cave-Ayland         if (tp##_is_any_nan(xb->fld)) {                     \
3194403a884aSNikunj A Dadhania             match = extract32(dcmx, 6, 1);                  \
3195cf3b0334SMark Cave-Ayland         } else if (tp##_is_infinity(xb->fld)) {             \
3196403a884aSNikunj A Dadhania             match = extract32(dcmx, 4 + !sign, 1);          \
3197cf3b0334SMark Cave-Ayland         } else if (tp##_is_zero(xb->fld)) {                 \
3198403a884aSNikunj A Dadhania             match = extract32(dcmx, 2 + !sign, 1);          \
3199cf3b0334SMark Cave-Ayland         } else if (tp##_is_zero_or_denormal(xb->fld)) {     \
3200403a884aSNikunj A Dadhania             match = extract32(dcmx, 0 + !sign, 1);          \
3201403a884aSNikunj A Dadhania         }                                                   \
320278241762SNikunj A Dadhania                                                             \
320378241762SNikunj A Dadhania         if (scrf) {                                         \
320478241762SNikunj A Dadhania             cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT;  \
320578241762SNikunj A Dadhania             env->fpscr &= ~(0x0F << FPSCR_FPRF);            \
320678241762SNikunj A Dadhania             env->fpscr |= cc << FPSCR_FPRF;                 \
320778241762SNikunj A Dadhania             env->crf[BF(opcode)] = cc;                      \
320878241762SNikunj A Dadhania         } else {                                            \
3209cf3b0334SMark Cave-Ayland             t.tfld = match ? fld_max : 0;                   \
321078241762SNikunj A Dadhania         }                                                   \
3211403a884aSNikunj A Dadhania         match = 0;                                          \
3212403a884aSNikunj A Dadhania     }                                                       \
321378241762SNikunj A Dadhania     if (!scrf) {                                            \
3214cf3b0334SMark Cave-Ayland         *xt = t;                                            \
321578241762SNikunj A Dadhania     }                                                       \
3216403a884aSNikunj A Dadhania }
3217403a884aSNikunj A Dadhania 
321878241762SNikunj A Dadhania VSX_TEST_DC(xvtstdcdp, 2, xB(opcode), float64, VsrD(i), VsrD(i), UINT64_MAX, 0)
321978241762SNikunj A Dadhania VSX_TEST_DC(xvtstdcsp, 4, xB(opcode), float32, VsrW(i), VsrW(i), UINT32_MAX, 0)
322078241762SNikunj A Dadhania VSX_TEST_DC(xststdcdp, 1, xB(opcode), float64, VsrD(0), VsrD(0), 0, 1)
322178241762SNikunj A Dadhania VSX_TEST_DC(xststdcqp, 1, (rB(opcode) + 32), float128, f128, VsrD(0), 0, 1)
322278241762SNikunj A Dadhania 
32238d830485SMark Cave-Ayland void helper_xststdcsp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xb)
322478241762SNikunj A Dadhania {
322578241762SNikunj A Dadhania     uint32_t dcmx, sign, exp;
322678241762SNikunj A Dadhania     uint32_t cc, match = 0, not_sp = 0;
322778241762SNikunj A Dadhania 
322878241762SNikunj A Dadhania     dcmx = DCMX(opcode);
3229cf3b0334SMark Cave-Ayland     exp = (xb->VsrD(0) >> 52) & 0x7FF;
323078241762SNikunj A Dadhania 
3231cf3b0334SMark Cave-Ayland     sign = float64_is_neg(xb->VsrD(0));
3232cf3b0334SMark Cave-Ayland     if (float64_is_any_nan(xb->VsrD(0))) {
323378241762SNikunj A Dadhania         match = extract32(dcmx, 6, 1);
3234cf3b0334SMark Cave-Ayland     } else if (float64_is_infinity(xb->VsrD(0))) {
323578241762SNikunj A Dadhania         match = extract32(dcmx, 4 + !sign, 1);
3236cf3b0334SMark Cave-Ayland     } else if (float64_is_zero(xb->VsrD(0))) {
323778241762SNikunj A Dadhania         match = extract32(dcmx, 2 + !sign, 1);
3238cf3b0334SMark Cave-Ayland     } else if (float64_is_zero_or_denormal(xb->VsrD(0)) ||
323978241762SNikunj A Dadhania                (exp > 0 && exp < 0x381)) {
324078241762SNikunj A Dadhania         match = extract32(dcmx, 0 + !sign, 1);
324178241762SNikunj A Dadhania     }
324278241762SNikunj A Dadhania 
3243cf3b0334SMark Cave-Ayland     not_sp = !float64_eq(xb->VsrD(0),
324478241762SNikunj A Dadhania                          float32_to_float64(
3245cf3b0334SMark Cave-Ayland                              float64_to_float32(xb->VsrD(0), &env->fp_status),
324678241762SNikunj A Dadhania                              &env->fp_status), &env->fp_status);
324778241762SNikunj A Dadhania 
324878241762SNikunj A Dadhania     cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT | not_sp << CRF_SO_BIT;
324978241762SNikunj A Dadhania     env->fpscr &= ~(0x0F << FPSCR_FPRF);
325078241762SNikunj A Dadhania     env->fpscr |= cc << FPSCR_FPRF;
325178241762SNikunj A Dadhania     env->crf[BF(opcode)] = cc;
325278241762SNikunj A Dadhania }
3253be07ad58SJose Ricardo Ziviani 
325499229620SMark Cave-Ayland void helper_xsrqpi(CPUPPCState *env, uint32_t opcode,
325599229620SMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xb)
3256be07ad58SJose Ricardo Ziviani {
3257cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3258be07ad58SJose Ricardo Ziviani     uint8_t r = Rrm(opcode);
3259be07ad58SJose Ricardo Ziviani     uint8_t ex = Rc(opcode);
3260be07ad58SJose Ricardo Ziviani     uint8_t rmc = RMC(opcode);
3261be07ad58SJose Ricardo Ziviani     uint8_t rmode = 0;
3262be07ad58SJose Ricardo Ziviani     float_status tstat;
3263be07ad58SJose Ricardo Ziviani 
3264be07ad58SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3265be07ad58SJose Ricardo Ziviani 
3266be07ad58SJose Ricardo Ziviani     if (r == 0 && rmc == 0) {
3267be07ad58SJose Ricardo Ziviani         rmode = float_round_ties_away;
3268be07ad58SJose Ricardo Ziviani     } else if (r == 0 && rmc == 0x3) {
3269be07ad58SJose Ricardo Ziviani         rmode = fpscr_rn;
3270be07ad58SJose Ricardo Ziviani     } else if (r == 1) {
3271be07ad58SJose Ricardo Ziviani         switch (rmc) {
3272be07ad58SJose Ricardo Ziviani         case 0:
3273be07ad58SJose Ricardo Ziviani             rmode = float_round_nearest_even;
3274be07ad58SJose Ricardo Ziviani             break;
3275be07ad58SJose Ricardo Ziviani         case 1:
3276be07ad58SJose Ricardo Ziviani             rmode = float_round_to_zero;
3277be07ad58SJose Ricardo Ziviani             break;
3278be07ad58SJose Ricardo Ziviani         case 2:
3279be07ad58SJose Ricardo Ziviani             rmode = float_round_up;
3280be07ad58SJose Ricardo Ziviani             break;
3281be07ad58SJose Ricardo Ziviani         case 3:
3282be07ad58SJose Ricardo Ziviani             rmode = float_round_down;
3283be07ad58SJose Ricardo Ziviani             break;
3284be07ad58SJose Ricardo Ziviani         default:
3285be07ad58SJose Ricardo Ziviani             abort();
3286be07ad58SJose Ricardo Ziviani         }
3287be07ad58SJose Ricardo Ziviani     }
3288be07ad58SJose Ricardo Ziviani 
3289be07ad58SJose Ricardo Ziviani     tstat = env->fp_status;
3290be07ad58SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3291be07ad58SJose Ricardo Ziviani     set_float_rounding_mode(rmode, &tstat);
3292cf3b0334SMark Cave-Ayland     t.f128 = float128_round_to_int(xb->f128, &tstat);
3293be07ad58SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3294be07ad58SJose Ricardo Ziviani 
3295be07ad58SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3296cf3b0334SMark Cave-Ayland         if (float128_is_signaling_nan(xb->f128, &tstat)) {
329713c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
3298cf3b0334SMark Cave-Ayland             t.f128 = float128_snan_to_qnan(t.f128);
3299be07ad58SJose Ricardo Ziviani         }
3300be07ad58SJose Ricardo Ziviani     }
3301be07ad58SJose Ricardo Ziviani 
3302be07ad58SJose Ricardo Ziviani     if (ex == 0 && (tstat.float_exception_flags & float_flag_inexact)) {
3303be07ad58SJose Ricardo Ziviani         env->fp_status.float_exception_flags &= ~float_flag_inexact;
3304be07ad58SJose Ricardo Ziviani     }
3305be07ad58SJose Ricardo Ziviani 
3306cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
33076525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3308cf3b0334SMark Cave-Ayland     *xt = t;
3309be07ad58SJose Ricardo Ziviani }
3310917950d7SJose Ricardo Ziviani 
331199229620SMark Cave-Ayland void helper_xsrqpxp(CPUPPCState *env, uint32_t opcode,
331299229620SMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xb)
3313917950d7SJose Ricardo Ziviani {
3314cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3315917950d7SJose Ricardo Ziviani     uint8_t r = Rrm(opcode);
3316917950d7SJose Ricardo Ziviani     uint8_t rmc = RMC(opcode);
3317917950d7SJose Ricardo Ziviani     uint8_t rmode = 0;
3318917950d7SJose Ricardo Ziviani     floatx80 round_res;
3319917950d7SJose Ricardo Ziviani     float_status tstat;
3320917950d7SJose Ricardo Ziviani 
3321917950d7SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3322917950d7SJose Ricardo Ziviani 
3323917950d7SJose Ricardo Ziviani     if (r == 0 && rmc == 0) {
3324917950d7SJose Ricardo Ziviani         rmode = float_round_ties_away;
3325917950d7SJose Ricardo Ziviani     } else if (r == 0 && rmc == 0x3) {
3326917950d7SJose Ricardo Ziviani         rmode = fpscr_rn;
3327917950d7SJose Ricardo Ziviani     } else if (r == 1) {
3328917950d7SJose Ricardo Ziviani         switch (rmc) {
3329917950d7SJose Ricardo Ziviani         case 0:
3330917950d7SJose Ricardo Ziviani             rmode = float_round_nearest_even;
3331917950d7SJose Ricardo Ziviani             break;
3332917950d7SJose Ricardo Ziviani         case 1:
3333917950d7SJose Ricardo Ziviani             rmode = float_round_to_zero;
3334917950d7SJose Ricardo Ziviani             break;
3335917950d7SJose Ricardo Ziviani         case 2:
3336917950d7SJose Ricardo Ziviani             rmode = float_round_up;
3337917950d7SJose Ricardo Ziviani             break;
3338917950d7SJose Ricardo Ziviani         case 3:
3339917950d7SJose Ricardo Ziviani             rmode = float_round_down;
3340917950d7SJose Ricardo Ziviani             break;
3341917950d7SJose Ricardo Ziviani         default:
3342917950d7SJose Ricardo Ziviani             abort();
3343917950d7SJose Ricardo Ziviani         }
3344917950d7SJose Ricardo Ziviani     }
3345917950d7SJose Ricardo Ziviani 
3346917950d7SJose Ricardo Ziviani     tstat = env->fp_status;
3347917950d7SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3348917950d7SJose Ricardo Ziviani     set_float_rounding_mode(rmode, &tstat);
3349cf3b0334SMark Cave-Ayland     round_res = float128_to_floatx80(xb->f128, &tstat);
3350cf3b0334SMark Cave-Ayland     t.f128 = floatx80_to_float128(round_res, &tstat);
3351917950d7SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3352917950d7SJose Ricardo Ziviani 
3353917950d7SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3354cf3b0334SMark Cave-Ayland         if (float128_is_signaling_nan(xb->f128, &tstat)) {
335513c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
3356cf3b0334SMark Cave-Ayland             t.f128 = float128_snan_to_qnan(t.f128);
3357917950d7SJose Ricardo Ziviani         }
3358917950d7SJose Ricardo Ziviani     }
3359917950d7SJose Ricardo Ziviani 
3360cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3361cf3b0334SMark Cave-Ayland     *xt = t;
33626525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3363917950d7SJose Ricardo Ziviani }
3364a4a68476SJose Ricardo Ziviani 
336599229620SMark Cave-Ayland void helper_xssqrtqp(CPUPPCState *env, uint32_t opcode,
336699229620SMark Cave-Ayland                      ppc_vsr_t *xt, ppc_vsr_t *xb)
3367a4a68476SJose Ricardo Ziviani {
3368cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3369a4a68476SJose Ricardo Ziviani     float_status tstat;
3370a4a68476SJose Ricardo Ziviani 
3371a4a68476SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3372a4a68476SJose Ricardo Ziviani 
3373a8d411abSBharata B Rao     tstat = env->fp_status;
3374a4a68476SJose Ricardo Ziviani     if (unlikely(Rc(opcode) != 0)) {
3375a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
3376a4a68476SJose Ricardo Ziviani     }
3377a4a68476SJose Ricardo Ziviani 
3378a4a68476SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3379cf3b0334SMark Cave-Ayland     t.f128 = float128_sqrt(xb->f128, &tstat);
3380a4a68476SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3381a4a68476SJose Ricardo Ziviani 
3382a4a68476SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3383cf3b0334SMark Cave-Ayland         if (float128_is_signaling_nan(xb->f128, &tstat)) {
338413c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
3385cf3b0334SMark Cave-Ayland             t.f128 = float128_snan_to_qnan(xb->f128);
3386cf3b0334SMark Cave-Ayland         } else if (float128_is_quiet_nan(xb->f128, &tstat)) {
3387cf3b0334SMark Cave-Ayland             t.f128 = xb->f128;
3388cf3b0334SMark Cave-Ayland         } else if (float128_is_neg(xb->f128) && !float128_is_zero(xb->f128)) {
338913c9115fSRichard Henderson             float_invalid_op_vxsqrt(env, 1, GETPC());
3390cf3b0334SMark Cave-Ayland             t.f128 = float128_default_nan(&env->fp_status);
3391a4a68476SJose Ricardo Ziviani         }
3392a4a68476SJose Ricardo Ziviani     }
3393a4a68476SJose Ricardo Ziviani 
3394cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3395cf3b0334SMark Cave-Ayland     *xt = t;
33966525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3397a4a68476SJose Ricardo Ziviani }
3398f6b99afdSJose Ricardo Ziviani 
339923d0766bSMark Cave-Ayland void helper_xssubqp(CPUPPCState *env, uint32_t opcode,
340023d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
3401f6b99afdSJose Ricardo Ziviani {
3402cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
3403f6b99afdSJose Ricardo Ziviani     float_status tstat;
3404f6b99afdSJose Ricardo Ziviani 
3405f6b99afdSJose Ricardo Ziviani     helper_reset_fpstatus(env);
3406f6b99afdSJose Ricardo Ziviani 
3407a8d411abSBharata B Rao     tstat = env->fp_status;
3408f6b99afdSJose Ricardo Ziviani     if (unlikely(Rc(opcode) != 0)) {
3409a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
3410f6b99afdSJose Ricardo Ziviani     }
3411f6b99afdSJose Ricardo Ziviani 
3412f6b99afdSJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3413cf3b0334SMark Cave-Ayland     t.f128 = float128_sub(xa->f128, xb->f128, &tstat);
3414f6b99afdSJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3415f6b99afdSJose Ricardo Ziviani 
3416f6b99afdSJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
341757483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
3418cf3b0334SMark Cave-Ayland                                 float128_classify(xa->f128) |
3419cf3b0334SMark Cave-Ayland                                 float128_classify(xb->f128));
3420f6b99afdSJose Ricardo Ziviani     }
3421f6b99afdSJose Ricardo Ziviani 
3422cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3423cf3b0334SMark Cave-Ayland     *xt = t;
34246525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3425f6b99afdSJose Ricardo Ziviani }
3426