xref: /qemu/target/ppc/fpu_helper.c (revision fe43ba97)
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
96bd039cdSChetan Pant  * version 2.1 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)) {
61a7b7b983SPaul A. Clarke         if (unlikely(extract32(arg, 23, 8) == 0xff)) {
62a7b7b983SPaul A. Clarke             /* Inf or NAN.  */
63a7b7b983SPaul A. Clarke             ret  = (uint64_t)extract32(arg, 31, 1) << 63;
64a7b7b983SPaul A. Clarke             ret |= (uint64_t)0x7ff << 52;
65a7b7b983SPaul A. Clarke             ret |= (uint64_t)extract32(arg, 0, 23) << 29;
66a7b7b983SPaul A. Clarke         } else {
67a7b7b983SPaul A. Clarke             /* Normalized operand.  */
6886c0cab1SRichard Henderson             ret  = (uint64_t)extract32(arg, 30, 2) << 62;
6986c0cab1SRichard Henderson             ret |= ((extract32(arg, 30, 1) ^ 1) * (uint64_t)7) << 59;
7086c0cab1SRichard Henderson             ret |= (uint64_t)extract32(arg, 0, 30) << 29;
71a7b7b983SPaul A. Clarke         }
7286c0cab1SRichard Henderson     } else {
7386c0cab1SRichard Henderson         /* Zero or Denormalized operand.  */
7486c0cab1SRichard Henderson         ret = (uint64_t)extract32(arg, 31, 1) << 63;
7586c0cab1SRichard Henderson         if (unlikely(abs_arg != 0)) {
76c0e6616bSPaul A. Clarke             /*
77c0e6616bSPaul A. Clarke              * Denormalized operand.
78c0e6616bSPaul A. Clarke              * Shift fraction so that the msb is in the implicit bit position.
79c0e6616bSPaul A. Clarke              * Thus, shift is in the range [1:23].
80c0e6616bSPaul A. Clarke              */
81c0e6616bSPaul A. Clarke             int shift = clz32(abs_arg) - 8;
82c0e6616bSPaul A. Clarke             /*
83c0e6616bSPaul A. Clarke              * The first 3 terms compute the float64 exponent.  We then bias
84c0e6616bSPaul A. Clarke              * this result by -1 so that we can swallow the implicit bit below.
85c0e6616bSPaul A. Clarke              */
86c0e6616bSPaul A. Clarke             int exp = -126 - shift + 1023 - 1;
87c0e6616bSPaul A. Clarke 
8886c0cab1SRichard Henderson             ret |= (uint64_t)exp << 52;
89c0e6616bSPaul A. Clarke             ret += (uint64_t)abs_arg << (52 - 23 + shift);
9086c0cab1SRichard Henderson         }
9186c0cab1SRichard Henderson     }
9286c0cab1SRichard Henderson     return ret;
93fcf5ef2aSThomas Huth }
94fcf5ef2aSThomas Huth 
9586c0cab1SRichard Henderson /*
9686c0cab1SRichard Henderson  * This is the non-arithmatic conversion that happens e.g. on stores.
9786c0cab1SRichard Henderson  * In the Power ISA pseudocode, this is called SINGLE.
9886c0cab1SRichard Henderson  */
9986c0cab1SRichard Henderson uint32_t helper_tosingle(uint64_t arg)
100fcf5ef2aSThomas Huth {
10186c0cab1SRichard Henderson     int exp = extract64(arg, 52, 11);
10286c0cab1SRichard Henderson     uint32_t ret;
103fcf5ef2aSThomas Huth 
10486c0cab1SRichard Henderson     if (likely(exp > 896)) {
10586c0cab1SRichard Henderson         /* No denormalization required (includes Inf, NaN).  */
10686c0cab1SRichard Henderson         ret  = extract64(arg, 62, 2) << 30;
10786c0cab1SRichard Henderson         ret |= extract64(arg, 29, 30);
10886c0cab1SRichard Henderson     } else {
109fa9ebf8cSDavid Gibson         /*
110fa9ebf8cSDavid Gibson          * Zero or Denormal result.  If the exponent is in bounds for
111fa9ebf8cSDavid Gibson          * a single-precision denormal result, extract the proper
112fa9ebf8cSDavid Gibson          * bits.  If the input is not zero, and the exponent is out of
113fa9ebf8cSDavid Gibson          * bounds, then the result is undefined; this underflows to
114fa9ebf8cSDavid Gibson          * zero.
11586c0cab1SRichard Henderson          */
11686c0cab1SRichard Henderson         ret = extract64(arg, 63, 1) << 31;
11786c0cab1SRichard Henderson         if (unlikely(exp >= 874)) {
11886c0cab1SRichard Henderson             /* Denormal result.  */
11986c0cab1SRichard Henderson             ret |= ((1ULL << 52) | extract64(arg, 0, 52)) >> (896 + 30 - exp);
12086c0cab1SRichard Henderson         }
12186c0cab1SRichard Henderson     }
12286c0cab1SRichard Henderson     return ret;
123fcf5ef2aSThomas Huth }
124fcf5ef2aSThomas Huth 
125fcf5ef2aSThomas Huth static inline int ppc_float32_get_unbiased_exp(float32 f)
126fcf5ef2aSThomas Huth {
127fcf5ef2aSThomas Huth     return ((f >> 23) & 0xFF) - 127;
128fcf5ef2aSThomas Huth }
129fcf5ef2aSThomas Huth 
130fcf5ef2aSThomas Huth static inline int ppc_float64_get_unbiased_exp(float64 f)
131fcf5ef2aSThomas Huth {
132fcf5ef2aSThomas Huth     return ((f >> 52) & 0x7FF) - 1023;
133fcf5ef2aSThomas Huth }
134fcf5ef2aSThomas Huth 
1350394d7a6SRichard Henderson /* Classify a floating-point number.  */
1360394d7a6SRichard Henderson enum {
1370394d7a6SRichard Henderson     is_normal   = 1,
1380394d7a6SRichard Henderson     is_zero     = 2,
1390394d7a6SRichard Henderson     is_denormal = 4,
1400394d7a6SRichard Henderson     is_inf      = 8,
1410394d7a6SRichard Henderson     is_qnan     = 16,
1420394d7a6SRichard Henderson     is_snan     = 32,
1430394d7a6SRichard Henderson     is_neg      = 64,
1440394d7a6SRichard Henderson };
1450394d7a6SRichard Henderson 
1460394d7a6SRichard Henderson #define COMPUTE_CLASS(tp)                                      \
1470394d7a6SRichard Henderson static int tp##_classify(tp arg)                               \
1480394d7a6SRichard Henderson {                                                              \
1490394d7a6SRichard Henderson     int ret = tp##_is_neg(arg) * is_neg;                       \
1500394d7a6SRichard Henderson     if (unlikely(tp##_is_any_nan(arg))) {                      \
1510394d7a6SRichard Henderson         float_status dummy = { };  /* snan_bit_is_one = 0 */   \
1520394d7a6SRichard Henderson         ret |= (tp##_is_signaling_nan(arg, &dummy)             \
1530394d7a6SRichard Henderson                 ? is_snan : is_qnan);                          \
1540394d7a6SRichard Henderson     } else if (unlikely(tp##_is_infinity(arg))) {              \
1550394d7a6SRichard Henderson         ret |= is_inf;                                         \
1560394d7a6SRichard Henderson     } else if (tp##_is_zero(arg)) {                            \
1570394d7a6SRichard Henderson         ret |= is_zero;                                        \
1580394d7a6SRichard Henderson     } else if (tp##_is_zero_or_denormal(arg)) {                \
1590394d7a6SRichard Henderson         ret |= is_denormal;                                    \
1600394d7a6SRichard Henderson     } else {                                                   \
1610394d7a6SRichard Henderson         ret |= is_normal;                                      \
1620394d7a6SRichard Henderson     }                                                          \
1630394d7a6SRichard Henderson     return ret;                                                \
1640394d7a6SRichard Henderson }
1650394d7a6SRichard Henderson 
1660394d7a6SRichard Henderson COMPUTE_CLASS(float16)
1670394d7a6SRichard Henderson COMPUTE_CLASS(float32)
1680394d7a6SRichard Henderson COMPUTE_CLASS(float64)
1690394d7a6SRichard Henderson COMPUTE_CLASS(float128)
1700394d7a6SRichard Henderson 
1710394d7a6SRichard Henderson static void set_fprf_from_class(CPUPPCState *env, int class)
1720394d7a6SRichard Henderson {
1730394d7a6SRichard Henderson     static const uint8_t fprf[6][2] = {
1740394d7a6SRichard Henderson         { 0x04, 0x08 },  /* normalized */
1750394d7a6SRichard Henderson         { 0x02, 0x12 },  /* zero */
1760394d7a6SRichard Henderson         { 0x14, 0x18 },  /* denormalized */
1770394d7a6SRichard Henderson         { 0x05, 0x09 },  /* infinity */
1780394d7a6SRichard Henderson         { 0x11, 0x11 },  /* qnan */
1790394d7a6SRichard Henderson         { 0x00, 0x00 },  /* snan -- flags are undefined */
1800394d7a6SRichard Henderson     };
1810394d7a6SRichard Henderson     bool isneg = class & is_neg;
1820394d7a6SRichard Henderson 
1835c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPRF;
1840394d7a6SRichard Henderson     env->fpscr |= fprf[ctz32(class)][isneg] << FPSCR_FPRF;
1850394d7a6SRichard Henderson }
1860394d7a6SRichard Henderson 
187ffc67420SBharata B Rao #define COMPUTE_FPRF(tp)                                \
188ffc67420SBharata B Rao void helper_compute_fprf_##tp(CPUPPCState *env, tp arg) \
189ffc67420SBharata B Rao {                                                       \
1900394d7a6SRichard Henderson     set_fprf_from_class(env, tp##_classify(arg));       \
191ffc67420SBharata B Rao }
192fcf5ef2aSThomas Huth 
193f566c047SBharata B Rao COMPUTE_FPRF(float16)
1949aeae8e1SBharata B Rao COMPUTE_FPRF(float32)
195ffc67420SBharata B Rao COMPUTE_FPRF(float64)
19607bdd247SBharata B Rao COMPUTE_FPRF(float128)
197fcf5ef2aSThomas Huth 
198fcf5ef2aSThomas Huth /* Floating-point invalid operations exception */
19913c9115fSRichard Henderson static void finish_invalid_op_excp(CPUPPCState *env, int op, uintptr_t retaddr)
200fcf5ef2aSThomas Huth {
20113c9115fSRichard Henderson     /* Update the floating-point invalid operation summary */
2025c94dd38SPaul A. Clarke     env->fpscr |= FP_VX;
20313c9115fSRichard Henderson     /* Update the floating-point exception summary */
20413c9115fSRichard Henderson     env->fpscr |= FP_FX;
20513c9115fSRichard Henderson     if (fpscr_ve != 0) {
20613c9115fSRichard Henderson         /* Update the floating-point enabled exception summary */
2075c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
20813c9115fSRichard Henderson         if (fp_exceptions_enabled(env)) {
20913c9115fSRichard Henderson             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
21013c9115fSRichard Henderson                                    POWERPC_EXCP_FP | op, retaddr);
21113c9115fSRichard Henderson         }
21213c9115fSRichard Henderson     }
21313c9115fSRichard Henderson }
214fcf5ef2aSThomas Huth 
21513c9115fSRichard Henderson static void finish_invalid_op_arith(CPUPPCState *env, int op,
21613c9115fSRichard Henderson                                     bool set_fpcc, uintptr_t retaddr)
21713c9115fSRichard Henderson {
2185c94dd38SPaul A. Clarke     env->fpscr &= ~(FP_FR | FP_FI);
21913c9115fSRichard Henderson     if (fpscr_ve == 0) {
22013c9115fSRichard Henderson         if (set_fpcc) {
2215c94dd38SPaul A. Clarke             env->fpscr &= ~FP_FPCC;
2225c94dd38SPaul A. Clarke             env->fpscr |= (FP_C | FP_FU);
22313c9115fSRichard Henderson         }
22413c9115fSRichard Henderson     }
22513c9115fSRichard Henderson     finish_invalid_op_excp(env, op, retaddr);
22613c9115fSRichard Henderson }
22713c9115fSRichard Henderson 
22813c9115fSRichard Henderson /* Signalling NaN */
22913c9115fSRichard Henderson static void float_invalid_op_vxsnan(CPUPPCState *env, uintptr_t retaddr)
23013c9115fSRichard Henderson {
2315c94dd38SPaul A. Clarke     env->fpscr |= FP_VXSNAN;
23213c9115fSRichard Henderson     finish_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, retaddr);
23313c9115fSRichard Henderson }
23413c9115fSRichard Henderson 
235fcf5ef2aSThomas Huth /* Magnitude subtraction of infinities */
23613c9115fSRichard Henderson static void float_invalid_op_vxisi(CPUPPCState *env, bool set_fpcc,
23713c9115fSRichard Henderson                                    uintptr_t retaddr)
23813c9115fSRichard Henderson {
2395c94dd38SPaul A. Clarke     env->fpscr |= FP_VXISI;
24013c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXISI, set_fpcc, retaddr);
24113c9115fSRichard Henderson }
24213c9115fSRichard Henderson 
243fcf5ef2aSThomas Huth /* Division of infinity by infinity */
24413c9115fSRichard Henderson static void float_invalid_op_vxidi(CPUPPCState *env, bool set_fpcc,
24513c9115fSRichard Henderson                                    uintptr_t retaddr)
24613c9115fSRichard Henderson {
2475c94dd38SPaul A. Clarke     env->fpscr |= FP_VXIDI;
24813c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXIDI, set_fpcc, retaddr);
24913c9115fSRichard Henderson }
25013c9115fSRichard Henderson 
251fcf5ef2aSThomas Huth /* Division of zero by zero */
25213c9115fSRichard Henderson static void float_invalid_op_vxzdz(CPUPPCState *env, bool set_fpcc,
25313c9115fSRichard Henderson                                    uintptr_t retaddr)
25413c9115fSRichard Henderson {
2555c94dd38SPaul A. Clarke     env->fpscr |= FP_VXZDZ;
25613c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXZDZ, set_fpcc, retaddr);
25713c9115fSRichard Henderson }
25813c9115fSRichard Henderson 
259fcf5ef2aSThomas Huth /* Multiplication of zero by infinity */
26013c9115fSRichard Henderson static void float_invalid_op_vximz(CPUPPCState *env, bool set_fpcc,
26113c9115fSRichard Henderson                                    uintptr_t retaddr)
26213c9115fSRichard Henderson {
2635c94dd38SPaul A. Clarke     env->fpscr |= FP_VXIMZ;
26413c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXIMZ, set_fpcc, retaddr);
26513c9115fSRichard Henderson }
26613c9115fSRichard Henderson 
26713c9115fSRichard Henderson /* Square root of a negative number */
26813c9115fSRichard Henderson static void float_invalid_op_vxsqrt(CPUPPCState *env, bool set_fpcc,
26913c9115fSRichard Henderson                                     uintptr_t retaddr)
27013c9115fSRichard Henderson {
2715c94dd38SPaul A. Clarke     env->fpscr |= FP_VXSQRT;
27213c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXSQRT, set_fpcc, retaddr);
27313c9115fSRichard Henderson }
27413c9115fSRichard Henderson 
275fcf5ef2aSThomas Huth /* Ordered comparison of NaN */
27613c9115fSRichard Henderson static void float_invalid_op_vxvc(CPUPPCState *env, bool set_fpcc,
27713c9115fSRichard Henderson                                   uintptr_t retaddr)
27813c9115fSRichard Henderson {
2795c94dd38SPaul A. Clarke     env->fpscr |= FP_VXVC;
280fcf5ef2aSThomas Huth     if (set_fpcc) {
2815c94dd38SPaul A. Clarke         env->fpscr &= ~FP_FPCC;
2825c94dd38SPaul A. Clarke         env->fpscr |= (FP_C | FP_FU);
283fcf5ef2aSThomas Huth     }
28413c9115fSRichard Henderson     /* Update the floating-point invalid operation summary */
2855c94dd38SPaul A. Clarke     env->fpscr |= FP_VX;
28613c9115fSRichard Henderson     /* Update the floating-point exception summary */
28713c9115fSRichard Henderson     env->fpscr |= FP_FX;
288fcf5ef2aSThomas Huth     /* We must update the target FPR before raising the exception */
28913c9115fSRichard Henderson     if (fpscr_ve != 0) {
290db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
29113c9115fSRichard Henderson 
292fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
293fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
294fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
2955c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
29692eeb004SBALATON Zoltan         /* Exception is deferred */
297fcf5ef2aSThomas Huth     }
298fcf5ef2aSThomas Huth }
29913c9115fSRichard Henderson 
300fcf5ef2aSThomas Huth /* Invalid conversion */
30113c9115fSRichard Henderson static void float_invalid_op_vxcvi(CPUPPCState *env, bool set_fpcc,
30213c9115fSRichard Henderson                                    uintptr_t retaddr)
30313c9115fSRichard Henderson {
3045c94dd38SPaul A. Clarke     env->fpscr |= FP_VXCVI;
3055c94dd38SPaul A. Clarke     env->fpscr &= ~(FP_FR | FP_FI);
30613c9115fSRichard Henderson     if (fpscr_ve == 0) {
307fcf5ef2aSThomas Huth         if (set_fpcc) {
3085c94dd38SPaul A. Clarke             env->fpscr &= ~FP_FPCC;
3095c94dd38SPaul A. Clarke             env->fpscr |= (FP_C | FP_FU);
310fcf5ef2aSThomas Huth         }
311fcf5ef2aSThomas Huth     }
31213c9115fSRichard Henderson     finish_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, retaddr);
313fcf5ef2aSThomas Huth }
314fcf5ef2aSThomas Huth 
315fcf5ef2aSThomas Huth static inline void float_zero_divide_excp(CPUPPCState *env, uintptr_t raddr)
316fcf5ef2aSThomas Huth {
3175c94dd38SPaul A. Clarke     env->fpscr |= FP_ZX;
3185c94dd38SPaul A. Clarke     env->fpscr &= ~(FP_FR | FP_FI);
319fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
320fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
321fcf5ef2aSThomas Huth     if (fpscr_ze != 0) {
322fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3235c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
324e82c42b7SRichard Henderson         if (fp_exceptions_enabled(env)) {
325fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
326fcf5ef2aSThomas Huth                                    POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX,
327fcf5ef2aSThomas Huth                                    raddr);
328fcf5ef2aSThomas Huth         }
329fcf5ef2aSThomas Huth     }
330fcf5ef2aSThomas Huth }
331fcf5ef2aSThomas Huth 
332fcf5ef2aSThomas Huth static inline void float_overflow_excp(CPUPPCState *env)
333fcf5ef2aSThomas Huth {
334db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
335fcf5ef2aSThomas Huth 
3365c94dd38SPaul A. Clarke     env->fpscr |= FP_OX;
337fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
338fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
339fcf5ef2aSThomas Huth     if (fpscr_oe != 0) {
340fcf5ef2aSThomas Huth         /* XXX: should adjust the result */
341fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3425c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
343fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
344fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
345fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
346fcf5ef2aSThomas Huth     } else {
3475c94dd38SPaul A. Clarke         env->fpscr |= FP_XX;
3485c94dd38SPaul A. Clarke         env->fpscr |= FP_FI;
349fcf5ef2aSThomas Huth     }
350fcf5ef2aSThomas Huth }
351fcf5ef2aSThomas Huth 
352fcf5ef2aSThomas Huth static inline void float_underflow_excp(CPUPPCState *env)
353fcf5ef2aSThomas Huth {
354db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
355fcf5ef2aSThomas Huth 
3565c94dd38SPaul A. Clarke     env->fpscr |= FP_UX;
357fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
358fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
359fcf5ef2aSThomas Huth     if (fpscr_ue != 0) {
360fcf5ef2aSThomas Huth         /* XXX: should adjust the result */
361fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3625c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
363fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
364fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
365fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
366fcf5ef2aSThomas Huth     }
367fcf5ef2aSThomas Huth }
368fcf5ef2aSThomas Huth 
369fcf5ef2aSThomas Huth static inline void float_inexact_excp(CPUPPCState *env)
370fcf5ef2aSThomas Huth {
371db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
372fcf5ef2aSThomas Huth 
3735c94dd38SPaul A. Clarke     env->fpscr |= FP_FI;
3745c94dd38SPaul A. Clarke     env->fpscr |= FP_XX;
375fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
376fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
377fcf5ef2aSThomas Huth     if (fpscr_xe != 0) {
378fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3795c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
380fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
381fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
382fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
383fcf5ef2aSThomas Huth     }
384fcf5ef2aSThomas Huth }
385fcf5ef2aSThomas Huth 
386fcf5ef2aSThomas Huth void helper_fpscr_clrbit(CPUPPCState *env, uint32_t bit)
387fcf5ef2aSThomas Huth {
388*fe43ba97SBruno Larsen (billionai)     uint32_t mask = 1u << bit;
389*fe43ba97SBruno Larsen (billionai)     if (env->fpscr & mask) {
390*fe43ba97SBruno Larsen (billionai)         ppc_store_fpscr(env, env->fpscr & ~(target_ulong)mask);
391fcf5ef2aSThomas Huth     }
392fcf5ef2aSThomas Huth }
393fcf5ef2aSThomas Huth 
394fcf5ef2aSThomas Huth void helper_fpscr_setbit(CPUPPCState *env, uint32_t bit)
395fcf5ef2aSThomas Huth {
396*fe43ba97SBruno Larsen (billionai)     uint32_t mask = 1u << bit;
397*fe43ba97SBruno Larsen (billionai)     if (!(env->fpscr & mask)) {
398*fe43ba97SBruno Larsen (billionai)         ppc_store_fpscr(env, env->fpscr | mask);
399fcf5ef2aSThomas Huth     }
400fcf5ef2aSThomas Huth }
401fcf5ef2aSThomas Huth 
402*fe43ba97SBruno Larsen (billionai) void helper_store_fpscr(CPUPPCState *env, uint64_t val, uint32_t nibbles)
403fcf5ef2aSThomas Huth {
404*fe43ba97SBruno Larsen (billionai)     target_ulong mask = 0;
405fcf5ef2aSThomas Huth     int i;
406fcf5ef2aSThomas Huth 
407*fe43ba97SBruno Larsen (billionai)     /* TODO: push this extension back to translation time */
408fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong) * 2; i++) {
409*fe43ba97SBruno Larsen (billionai)         if (nibbles & (1 << i)) {
410*fe43ba97SBruno Larsen (billionai)             mask |= (target_ulong) 0xf << (4 * i);
411fcf5ef2aSThomas Huth         }
412fcf5ef2aSThomas Huth     }
413*fe43ba97SBruno Larsen (billionai)     val = (val & mask) | (env->fpscr & ~mask);
414*fe43ba97SBruno Larsen (billionai)     ppc_store_fpscr(env, val);
415fcf5ef2aSThomas Huth }
416fcf5ef2aSThomas Huth 
417fcf5ef2aSThomas Huth static void do_float_check_status(CPUPPCState *env, uintptr_t raddr)
418fcf5ef2aSThomas Huth {
419db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
420fcf5ef2aSThomas Huth     int status = get_float_exception_flags(&env->fp_status);
421fcf5ef2aSThomas Huth 
422ae13018dSRichard Henderson     if (status & float_flag_overflow) {
423fcf5ef2aSThomas Huth         float_overflow_excp(env);
424fcf5ef2aSThomas Huth     } else if (status & float_flag_underflow) {
425fcf5ef2aSThomas Huth         float_underflow_excp(env);
4269e430ca3SJohn Arbuckle     }
42716ce2fffSRichard Henderson     if (status & float_flag_inexact) {
42816ce2fffSRichard Henderson         float_inexact_excp(env);
42916ce2fffSRichard Henderson     } else {
4305c94dd38SPaul A. Clarke         env->fpscr &= ~FP_FI; /* clear the FPSCR[FI] bit */
431fcf5ef2aSThomas Huth     }
432fcf5ef2aSThomas Huth 
433fcf5ef2aSThomas Huth     if (cs->exception_index == POWERPC_EXCP_PROGRAM &&
434fcf5ef2aSThomas Huth         (env->error_code & POWERPC_EXCP_FP)) {
43592eeb004SBALATON Zoltan         /* Deferred floating-point exception after target FPR update */
436e82c42b7SRichard Henderson         if (fp_exceptions_enabled(env)) {
437fcf5ef2aSThomas Huth             raise_exception_err_ra(env, cs->exception_index,
438fcf5ef2aSThomas Huth                                    env->error_code, raddr);
439fcf5ef2aSThomas Huth         }
440fcf5ef2aSThomas Huth     }
441fcf5ef2aSThomas Huth }
442fcf5ef2aSThomas Huth 
443fcf5ef2aSThomas Huth void helper_float_check_status(CPUPPCState *env)
444fcf5ef2aSThomas Huth {
445fcf5ef2aSThomas Huth     do_float_check_status(env, GETPC());
446fcf5ef2aSThomas Huth }
447fcf5ef2aSThomas Huth 
448fcf5ef2aSThomas Huth void helper_reset_fpstatus(CPUPPCState *env)
449fcf5ef2aSThomas Huth {
450fcf5ef2aSThomas Huth     set_float_exception_flags(0, &env->fp_status);
451fcf5ef2aSThomas Huth }
452fcf5ef2aSThomas Huth 
45357483867SRichard Henderson static void float_invalid_op_addsub(CPUPPCState *env, bool set_fpcc,
45457483867SRichard Henderson                                     uintptr_t retaddr, int classes)
45557483867SRichard Henderson {
45657483867SRichard Henderson     if ((classes & ~is_neg) == is_inf) {
45757483867SRichard Henderson         /* Magnitude subtraction of infinities */
45857483867SRichard Henderson         float_invalid_op_vxisi(env, set_fpcc, retaddr);
45957483867SRichard Henderson     } else if (classes & is_snan) {
46057483867SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
46157483867SRichard Henderson     }
46257483867SRichard Henderson }
46357483867SRichard Henderson 
464fcf5ef2aSThomas Huth /* fadd - fadd. */
465ac43cec3SRichard Henderson float64 helper_fadd(CPUPPCState *env, float64 arg1, float64 arg2)
466fcf5ef2aSThomas Huth {
467ac43cec3SRichard Henderson     float64 ret = float64_add(arg1, arg2, &env->fp_status);
468ac43cec3SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
469fcf5ef2aSThomas Huth 
470ac43cec3SRichard Henderson     if (unlikely(status & float_flag_invalid)) {
47157483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
47257483867SRichard Henderson                                 float64_classify(arg1) |
47357483867SRichard Henderson                                 float64_classify(arg2));
474fcf5ef2aSThomas Huth     }
475fcf5ef2aSThomas Huth 
476ac43cec3SRichard Henderson     return ret;
477fcf5ef2aSThomas Huth }
478fcf5ef2aSThomas Huth 
479fcf5ef2aSThomas Huth /* fsub - fsub. */
480ac43cec3SRichard Henderson float64 helper_fsub(CPUPPCState *env, float64 arg1, float64 arg2)
481fcf5ef2aSThomas Huth {
482ac43cec3SRichard Henderson     float64 ret = float64_sub(arg1, arg2, &env->fp_status);
483ac43cec3SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
484fcf5ef2aSThomas Huth 
485ac43cec3SRichard Henderson     if (unlikely(status & float_flag_invalid)) {
48657483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
48757483867SRichard Henderson                                 float64_classify(arg1) |
48857483867SRichard Henderson                                 float64_classify(arg2));
489fcf5ef2aSThomas Huth     }
490fcf5ef2aSThomas Huth 
491ac43cec3SRichard Henderson     return ret;
492fcf5ef2aSThomas Huth }
493fcf5ef2aSThomas Huth 
4944f0da706SRichard Henderson static void float_invalid_op_mul(CPUPPCState *env, bool set_fprc,
4954f0da706SRichard Henderson                                  uintptr_t retaddr, int classes)
4964f0da706SRichard Henderson {
4974f0da706SRichard Henderson     if ((classes & (is_zero | is_inf)) == (is_zero | is_inf)) {
4984f0da706SRichard Henderson         /* Multiplication of zero by infinity */
4994f0da706SRichard Henderson         float_invalid_op_vximz(env, set_fprc, retaddr);
5004f0da706SRichard Henderson     } else if (classes & is_snan) {
5014f0da706SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
5024f0da706SRichard Henderson     }
5034f0da706SRichard Henderson }
5044f0da706SRichard Henderson 
505fcf5ef2aSThomas Huth /* fmul - fmul. */
50679f91633SRichard Henderson float64 helper_fmul(CPUPPCState *env, float64 arg1, float64 arg2)
507fcf5ef2aSThomas Huth {
50879f91633SRichard Henderson     float64 ret = float64_mul(arg1, arg2, &env->fp_status);
50979f91633SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
510fcf5ef2aSThomas Huth 
51179f91633SRichard Henderson     if (unlikely(status & float_flag_invalid)) {
5124f0da706SRichard Henderson         float_invalid_op_mul(env, 1, GETPC(),
5134f0da706SRichard Henderson                              float64_classify(arg1) |
5144f0da706SRichard Henderson                              float64_classify(arg2));
515fcf5ef2aSThomas Huth     }
516fcf5ef2aSThomas Huth 
51779f91633SRichard Henderson     return ret;
518fcf5ef2aSThomas Huth }
519fcf5ef2aSThomas Huth 
520fec59ef3SRichard Henderson static void float_invalid_op_div(CPUPPCState *env, bool set_fprc,
521fec59ef3SRichard Henderson                                  uintptr_t retaddr, int classes)
522fec59ef3SRichard Henderson {
523fec59ef3SRichard Henderson     classes &= ~is_neg;
524fec59ef3SRichard Henderson     if (classes == is_inf) {
525fec59ef3SRichard Henderson         /* Division of infinity by infinity */
526fec59ef3SRichard Henderson         float_invalid_op_vxidi(env, set_fprc, retaddr);
527fec59ef3SRichard Henderson     } else if (classes == is_zero) {
528fec59ef3SRichard Henderson         /* Division of zero by zero */
529fec59ef3SRichard Henderson         float_invalid_op_vxzdz(env, set_fprc, retaddr);
530fec59ef3SRichard Henderson     } else if (classes & is_snan) {
531fec59ef3SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
532fec59ef3SRichard Henderson     }
533fec59ef3SRichard Henderson }
534fec59ef3SRichard Henderson 
535fcf5ef2aSThomas Huth /* fdiv - fdiv. */
536ae13018dSRichard Henderson float64 helper_fdiv(CPUPPCState *env, float64 arg1, float64 arg2)
537fcf5ef2aSThomas Huth {
538ae13018dSRichard Henderson     float64 ret = float64_div(arg1, arg2, &env->fp_status);
539ae13018dSRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
540fcf5ef2aSThomas Huth 
541ae13018dSRichard Henderson     if (unlikely(status)) {
542ae13018dSRichard Henderson         if (status & float_flag_invalid) {
543fec59ef3SRichard Henderson             float_invalid_op_div(env, 1, GETPC(),
544fec59ef3SRichard Henderson                                  float64_classify(arg1) |
545fec59ef3SRichard Henderson                                  float64_classify(arg2));
546ae13018dSRichard Henderson         }
547ae13018dSRichard Henderson         if (status & float_flag_divbyzero) {
548ae13018dSRichard Henderson             float_zero_divide_excp(env, GETPC());
549ae13018dSRichard Henderson         }
550fcf5ef2aSThomas Huth     }
551fcf5ef2aSThomas Huth 
552ae13018dSRichard Henderson     return ret;
553fcf5ef2aSThomas Huth }
554fcf5ef2aSThomas Huth 
555a3dec427SRichard Henderson static void float_invalid_cvt(CPUPPCState *env, bool set_fprc,
556a3dec427SRichard Henderson                               uintptr_t retaddr, int class1)
557a3dec427SRichard Henderson {
558a3dec427SRichard Henderson     float_invalid_op_vxcvi(env, set_fprc, retaddr);
559a3dec427SRichard Henderson     if (class1 & is_snan) {
560a3dec427SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
561a3dec427SRichard Henderson     }
562a3dec427SRichard Henderson }
563fcf5ef2aSThomas Huth 
564fcf5ef2aSThomas Huth #define FPU_FCTI(op, cvt, nanval)                                      \
565a3dec427SRichard Henderson uint64_t helper_##op(CPUPPCState *env, float64 arg)                    \
566fcf5ef2aSThomas Huth {                                                                      \
567a3dec427SRichard Henderson     uint64_t ret = float64_to_##cvt(arg, &env->fp_status);             \
568a3dec427SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);           \
569fcf5ef2aSThomas Huth                                                                        \
570a3dec427SRichard Henderson     if (unlikely(status)) {                                            \
571a3dec427SRichard Henderson         if (status & float_flag_invalid) {                             \
572a3dec427SRichard Henderson             float_invalid_cvt(env, 1, GETPC(), float64_classify(arg)); \
573a3dec427SRichard Henderson             ret = nanval;                                              \
574fcf5ef2aSThomas Huth         }                                                              \
5756525aadcSRichard Henderson         do_float_check_status(env, GETPC());                           \
576fcf5ef2aSThomas Huth     }                                                                  \
577a3dec427SRichard Henderson     return ret;                                                        \
578fcf5ef2aSThomas Huth }
579fcf5ef2aSThomas Huth 
580fcf5ef2aSThomas Huth FPU_FCTI(fctiw, int32, 0x80000000U)
581fcf5ef2aSThomas Huth FPU_FCTI(fctiwz, int32_round_to_zero, 0x80000000U)
582fcf5ef2aSThomas Huth FPU_FCTI(fctiwu, uint32, 0x00000000U)
583fcf5ef2aSThomas Huth FPU_FCTI(fctiwuz, uint32_round_to_zero, 0x00000000U)
584fcf5ef2aSThomas Huth FPU_FCTI(fctid, int64, 0x8000000000000000ULL)
585fcf5ef2aSThomas Huth FPU_FCTI(fctidz, int64_round_to_zero, 0x8000000000000000ULL)
586fcf5ef2aSThomas Huth FPU_FCTI(fctidu, uint64, 0x0000000000000000ULL)
587fcf5ef2aSThomas Huth FPU_FCTI(fctiduz, uint64_round_to_zero, 0x0000000000000000ULL)
588fcf5ef2aSThomas Huth 
589fcf5ef2aSThomas Huth #define FPU_FCFI(op, cvtr, is_single)                      \
590fcf5ef2aSThomas Huth uint64_t helper_##op(CPUPPCState *env, uint64_t arg)       \
591fcf5ef2aSThomas Huth {                                                          \
592fcf5ef2aSThomas Huth     CPU_DoubleU farg;                                      \
593fcf5ef2aSThomas Huth                                                            \
594fcf5ef2aSThomas Huth     if (is_single) {                                       \
595fcf5ef2aSThomas Huth         float32 tmp = cvtr(arg, &env->fp_status);          \
596fcf5ef2aSThomas Huth         farg.d = float32_to_float64(tmp, &env->fp_status); \
597fcf5ef2aSThomas Huth     } else {                                               \
598fcf5ef2aSThomas Huth         farg.d = cvtr(arg, &env->fp_status);               \
599fcf5ef2aSThomas Huth     }                                                      \
6006525aadcSRichard Henderson     do_float_check_status(env, GETPC());                   \
601fcf5ef2aSThomas Huth     return farg.ll;                                        \
602fcf5ef2aSThomas Huth }
603fcf5ef2aSThomas Huth 
604fcf5ef2aSThomas Huth FPU_FCFI(fcfid, int64_to_float64, 0)
605fcf5ef2aSThomas Huth FPU_FCFI(fcfids, int64_to_float32, 1)
606fcf5ef2aSThomas Huth FPU_FCFI(fcfidu, uint64_to_float64, 0)
607fcf5ef2aSThomas Huth FPU_FCFI(fcfidus, uint64_to_float32, 1)
608fcf5ef2aSThomas Huth 
609fcf5ef2aSThomas Huth static inline uint64_t do_fri(CPUPPCState *env, uint64_t arg,
610fcf5ef2aSThomas Huth                               int rounding_mode)
611fcf5ef2aSThomas Huth {
612fcf5ef2aSThomas Huth     CPU_DoubleU farg;
61363d06e90SBruno Larsen (billionai)     FloatRoundMode old_rounding_mode = get_float_rounding_mode(&env->fp_status);
614fcf5ef2aSThomas Huth 
615fcf5ef2aSThomas Huth     farg.ll = arg;
616fcf5ef2aSThomas Huth 
617fcf5ef2aSThomas Huth     if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
618fcf5ef2aSThomas Huth         /* sNaN round */
61913c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
620fcf5ef2aSThomas Huth         farg.ll = arg | 0x0008000000000000ULL;
621fcf5ef2aSThomas Huth     } else {
622fcf5ef2aSThomas Huth         int inexact = get_float_exception_flags(&env->fp_status) &
623fcf5ef2aSThomas Huth                       float_flag_inexact;
624fcf5ef2aSThomas Huth         set_float_rounding_mode(rounding_mode, &env->fp_status);
625fcf5ef2aSThomas Huth         farg.ll = float64_round_to_int(farg.d, &env->fp_status);
62663d06e90SBruno Larsen (billionai)         set_float_rounding_mode(old_rounding_mode, &env->fp_status);
627fcf5ef2aSThomas Huth 
628fcf5ef2aSThomas Huth         /* fri* does not set FPSCR[XX] */
629fcf5ef2aSThomas Huth         if (!inexact) {
630fcf5ef2aSThomas Huth             env->fp_status.float_exception_flags &= ~float_flag_inexact;
631fcf5ef2aSThomas Huth         }
632fcf5ef2aSThomas Huth     }
6336525aadcSRichard Henderson     do_float_check_status(env, GETPC());
634fcf5ef2aSThomas Huth     return farg.ll;
635fcf5ef2aSThomas Huth }
636fcf5ef2aSThomas Huth 
637fcf5ef2aSThomas Huth uint64_t helper_frin(CPUPPCState *env, uint64_t arg)
638fcf5ef2aSThomas Huth {
639fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_ties_away);
640fcf5ef2aSThomas Huth }
641fcf5ef2aSThomas Huth 
642fcf5ef2aSThomas Huth uint64_t helper_friz(CPUPPCState *env, uint64_t arg)
643fcf5ef2aSThomas Huth {
644fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_to_zero);
645fcf5ef2aSThomas Huth }
646fcf5ef2aSThomas Huth 
647fcf5ef2aSThomas Huth uint64_t helper_frip(CPUPPCState *env, uint64_t arg)
648fcf5ef2aSThomas Huth {
649fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_up);
650fcf5ef2aSThomas Huth }
651fcf5ef2aSThomas Huth 
652fcf5ef2aSThomas Huth uint64_t helper_frim(CPUPPCState *env, uint64_t arg)
653fcf5ef2aSThomas Huth {
654fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_down);
655fcf5ef2aSThomas Huth }
656fcf5ef2aSThomas Huth 
6573e5b26cfSNikunj A Dadhania #define FPU_MADDSUB_UPDATE(NAME, TP)                                    \
6583e5b26cfSNikunj A Dadhania static void NAME(CPUPPCState *env, TP arg1, TP arg2, TP arg3,           \
65913c9115fSRichard Henderson                  unsigned int madd_flags, uintptr_t retaddr)            \
6603e5b26cfSNikunj A Dadhania {                                                                       \
6613e5b26cfSNikunj A Dadhania     if (TP##_is_signaling_nan(arg1, &env->fp_status) ||                 \
6623e5b26cfSNikunj A Dadhania         TP##_is_signaling_nan(arg2, &env->fp_status) ||                 \
6633e5b26cfSNikunj A Dadhania         TP##_is_signaling_nan(arg3, &env->fp_status)) {                 \
6643e5b26cfSNikunj A Dadhania         /* sNaN operation */                                            \
66513c9115fSRichard Henderson         float_invalid_op_vxsnan(env, retaddr);                          \
6663e5b26cfSNikunj A Dadhania     }                                                                   \
6673e5b26cfSNikunj A Dadhania     if ((TP##_is_infinity(arg1) && TP##_is_zero(arg2)) ||               \
6683e5b26cfSNikunj A Dadhania         (TP##_is_zero(arg1) && TP##_is_infinity(arg2))) {               \
6693e5b26cfSNikunj A Dadhania         /* Multiplication of zero by infinity */                        \
67013c9115fSRichard Henderson         float_invalid_op_vximz(env, 1, retaddr);                        \
6713e5b26cfSNikunj A Dadhania     }                                                                   \
6723e5b26cfSNikunj A Dadhania     if ((TP##_is_infinity(arg1) || TP##_is_infinity(arg2)) &&           \
6733e5b26cfSNikunj A Dadhania         TP##_is_infinity(arg3)) {                                       \
6743e5b26cfSNikunj A Dadhania         uint8_t aSign, bSign, cSign;                                    \
6753e5b26cfSNikunj A Dadhania                                                                         \
6763e5b26cfSNikunj A Dadhania         aSign = TP##_is_neg(arg1);                                      \
6773e5b26cfSNikunj A Dadhania         bSign = TP##_is_neg(arg2);                                      \
6783e5b26cfSNikunj A Dadhania         cSign = TP##_is_neg(arg3);                                      \
6793e5b26cfSNikunj A Dadhania         if (madd_flags & float_muladd_negate_c) {                       \
6803e5b26cfSNikunj A Dadhania             cSign ^= 1;                                                 \
6813e5b26cfSNikunj A Dadhania         }                                                               \
6823e5b26cfSNikunj A Dadhania         if (aSign ^ bSign ^ cSign) {                                    \
68313c9115fSRichard Henderson             float_invalid_op_vxisi(env, 1, retaddr);                    \
6843e5b26cfSNikunj A Dadhania         }                                                               \
6853e5b26cfSNikunj A Dadhania     }                                                                   \
686806c9d71SNikunj A Dadhania }
687182fe2cfSNikunj A Dadhania FPU_MADDSUB_UPDATE(float32_maddsub_update_excp, float32)
6883e5b26cfSNikunj A Dadhania FPU_MADDSUB_UPDATE(float64_maddsub_update_excp, float64)
689fcf5ef2aSThomas Huth 
690992d7e97SNikunj A Dadhania #define FPU_FMADD(op, madd_flags)                                       \
691992d7e97SNikunj A Dadhania uint64_t helper_##op(CPUPPCState *env, uint64_t arg1,                   \
692992d7e97SNikunj A Dadhania                      uint64_t arg2, uint64_t arg3)                      \
693992d7e97SNikunj A Dadhania {                                                                       \
694992d7e97SNikunj A Dadhania     uint32_t flags;                                                     \
695992d7e97SNikunj A Dadhania     float64 ret = float64_muladd(arg1, arg2, arg3, madd_flags,          \
696992d7e97SNikunj A Dadhania                                  &env->fp_status);                      \
697992d7e97SNikunj A Dadhania     flags = get_float_exception_flags(&env->fp_status);                 \
698992d7e97SNikunj A Dadhania     if (flags) {                                                        \
699992d7e97SNikunj A Dadhania         if (flags & float_flag_invalid) {                               \
700992d7e97SNikunj A Dadhania             float64_maddsub_update_excp(env, arg1, arg2, arg3,          \
70113c9115fSRichard Henderson                                         madd_flags, GETPC());           \
702992d7e97SNikunj A Dadhania         }                                                               \
7036525aadcSRichard Henderson         do_float_check_status(env, GETPC());                            \
704992d7e97SNikunj A Dadhania     }                                                                   \
705992d7e97SNikunj A Dadhania     return ret;                                                         \
706fcf5ef2aSThomas Huth }
707fcf5ef2aSThomas Huth 
708992d7e97SNikunj A Dadhania #define MADD_FLGS 0
709992d7e97SNikunj A Dadhania #define MSUB_FLGS float_muladd_negate_c
710992d7e97SNikunj A Dadhania #define NMADD_FLGS float_muladd_negate_result
711992d7e97SNikunj A Dadhania #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
712fcf5ef2aSThomas Huth 
713992d7e97SNikunj A Dadhania FPU_FMADD(fmadd, MADD_FLGS)
714992d7e97SNikunj A Dadhania FPU_FMADD(fnmadd, NMADD_FLGS)
715992d7e97SNikunj A Dadhania FPU_FMADD(fmsub, MSUB_FLGS)
716992d7e97SNikunj A Dadhania FPU_FMADD(fnmsub, NMSUB_FLGS)
717fcf5ef2aSThomas Huth 
718fcf5ef2aSThomas Huth /* frsp - frsp. */
719fcf5ef2aSThomas Huth uint64_t helper_frsp(CPUPPCState *env, uint64_t arg)
720fcf5ef2aSThomas Huth {
721fcf5ef2aSThomas Huth     CPU_DoubleU farg;
722fcf5ef2aSThomas Huth     float32 f32;
723fcf5ef2aSThomas Huth 
724fcf5ef2aSThomas Huth     farg.ll = arg;
725fcf5ef2aSThomas Huth 
726fcf5ef2aSThomas Huth     if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
72713c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
728fcf5ef2aSThomas Huth     }
729fcf5ef2aSThomas Huth     f32 = float64_to_float32(farg.d, &env->fp_status);
730fcf5ef2aSThomas Huth     farg.d = float32_to_float64(f32, &env->fp_status);
731fcf5ef2aSThomas Huth 
732fcf5ef2aSThomas Huth     return farg.ll;
733fcf5ef2aSThomas Huth }
734fcf5ef2aSThomas Huth 
735fcf5ef2aSThomas Huth /* fsqrt - fsqrt. */
73649ab52efSRichard Henderson float64 helper_fsqrt(CPUPPCState *env, float64 arg)
737fcf5ef2aSThomas Huth {
73849ab52efSRichard Henderson     float64 ret = float64_sqrt(arg, &env->fp_status);
73949ab52efSRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
740fcf5ef2aSThomas Huth 
74149ab52efSRichard Henderson     if (unlikely(status & float_flag_invalid)) {
74249ab52efSRichard Henderson         if (unlikely(float64_is_any_nan(arg))) {
74349ab52efSRichard Henderson             if (unlikely(float64_is_signaling_nan(arg, &env->fp_status))) {
74449ab52efSRichard Henderson                 /* sNaN square root */
74513c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());
746fcf5ef2aSThomas Huth             }
747fcf5ef2aSThomas Huth         } else {
74849ab52efSRichard Henderson             /* Square root of a negative nonzero number */
74913c9115fSRichard Henderson             float_invalid_op_vxsqrt(env, 1, GETPC());
750fcf5ef2aSThomas Huth         }
75149ab52efSRichard Henderson     }
75249ab52efSRichard Henderson 
75349ab52efSRichard Henderson     return ret;
754fcf5ef2aSThomas Huth }
755fcf5ef2aSThomas Huth 
756fcf5ef2aSThomas Huth /* fre - fre. */
75738434717SRichard Henderson float64 helper_fre(CPUPPCState *env, float64 arg)
758fcf5ef2aSThomas Huth {
75938434717SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
76038434717SRichard Henderson     float64 ret = float64_div(float64_one, arg, &env->fp_status);
76138434717SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
762fcf5ef2aSThomas Huth 
76338434717SRichard Henderson     if (unlikely(status)) {
76438434717SRichard Henderson         if (status & float_flag_invalid) {
76538434717SRichard Henderson             if (float64_is_signaling_nan(arg, &env->fp_status)) {
766fcf5ef2aSThomas Huth                 /* sNaN reciprocal */
76713c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());
768fcf5ef2aSThomas Huth             }
76938434717SRichard Henderson         }
77038434717SRichard Henderson         if (status & float_flag_divbyzero) {
77138434717SRichard Henderson             float_zero_divide_excp(env, GETPC());
77238434717SRichard Henderson             /* For FPSCR.ZE == 0, the result is 1/2.  */
77338434717SRichard Henderson             ret = float64_set_sign(float64_half, float64_is_neg(arg));
77438434717SRichard Henderson         }
77538434717SRichard Henderson     }
77638434717SRichard Henderson 
77738434717SRichard Henderson     return ret;
778fcf5ef2aSThomas Huth }
779fcf5ef2aSThomas Huth 
780fcf5ef2aSThomas Huth /* fres - fres. */
781fcf5ef2aSThomas Huth uint64_t helper_fres(CPUPPCState *env, uint64_t arg)
782fcf5ef2aSThomas Huth {
783fcf5ef2aSThomas Huth     CPU_DoubleU farg;
784fcf5ef2aSThomas Huth     float32 f32;
785fcf5ef2aSThomas Huth 
786fcf5ef2aSThomas Huth     farg.ll = arg;
787fcf5ef2aSThomas Huth 
788fcf5ef2aSThomas Huth     if (unlikely(float64_is_signaling_nan(farg.d, &env->fp_status))) {
789fcf5ef2aSThomas Huth         /* sNaN reciprocal */
79013c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
791fcf5ef2aSThomas Huth     }
792fcf5ef2aSThomas Huth     farg.d = float64_div(float64_one, farg.d, &env->fp_status);
793fcf5ef2aSThomas Huth     f32 = float64_to_float32(farg.d, &env->fp_status);
794fcf5ef2aSThomas Huth     farg.d = float32_to_float64(f32, &env->fp_status);
795fcf5ef2aSThomas Huth 
796fcf5ef2aSThomas Huth     return farg.ll;
797fcf5ef2aSThomas Huth }
798fcf5ef2aSThomas Huth 
799fcf5ef2aSThomas Huth /* frsqrte  - frsqrte. */
80038434717SRichard Henderson float64 helper_frsqrte(CPUPPCState *env, float64 arg)
801fcf5ef2aSThomas Huth {
80238434717SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
80338434717SRichard Henderson     float64 rets = float64_sqrt(arg, &env->fp_status);
80438434717SRichard Henderson     float64 retd = float64_div(float64_one, rets, &env->fp_status);
80538434717SRichard Henderson     int status = get_float_exception_flags(&env->fp_status);
806fcf5ef2aSThomas Huth 
80738434717SRichard Henderson     if (unlikely(status)) {
80838434717SRichard Henderson         if (status & float_flag_invalid) {
80938434717SRichard Henderson             if (float64_is_signaling_nan(arg, &env->fp_status)) {
81038434717SRichard Henderson                 /* sNaN reciprocal */
81113c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());
812fcf5ef2aSThomas Huth             } else {
81338434717SRichard Henderson                 /* Square root of a negative nonzero number */
81413c9115fSRichard Henderson                 float_invalid_op_vxsqrt(env, 1, GETPC());
81538434717SRichard Henderson             }
81638434717SRichard Henderson         }
81738434717SRichard Henderson         if (status & float_flag_divbyzero) {
81838434717SRichard Henderson             /* Reciprocal of (square root of) zero.  */
81938434717SRichard Henderson             float_zero_divide_excp(env, GETPC());
82038434717SRichard Henderson         }
821fcf5ef2aSThomas Huth     }
822fcf5ef2aSThomas Huth 
82338434717SRichard Henderson     return retd;
824fcf5ef2aSThomas Huth }
825fcf5ef2aSThomas Huth 
826fcf5ef2aSThomas Huth /* fsel - fsel. */
827fcf5ef2aSThomas Huth uint64_t helper_fsel(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
828fcf5ef2aSThomas Huth                      uint64_t arg3)
829fcf5ef2aSThomas Huth {
830fcf5ef2aSThomas Huth     CPU_DoubleU farg1;
831fcf5ef2aSThomas Huth 
832fcf5ef2aSThomas Huth     farg1.ll = arg1;
833fcf5ef2aSThomas Huth 
834fcf5ef2aSThomas Huth     if ((!float64_is_neg(farg1.d) || float64_is_zero(farg1.d)) &&
835fcf5ef2aSThomas Huth         !float64_is_any_nan(farg1.d)) {
836fcf5ef2aSThomas Huth         return arg2;
837fcf5ef2aSThomas Huth     } else {
838fcf5ef2aSThomas Huth         return arg3;
839fcf5ef2aSThomas Huth     }
840fcf5ef2aSThomas Huth }
841fcf5ef2aSThomas Huth 
842fcf5ef2aSThomas Huth uint32_t helper_ftdiv(uint64_t fra, uint64_t frb)
843fcf5ef2aSThomas Huth {
844fcf5ef2aSThomas Huth     int fe_flag = 0;
845fcf5ef2aSThomas Huth     int fg_flag = 0;
846fcf5ef2aSThomas Huth 
847fcf5ef2aSThomas Huth     if (unlikely(float64_is_infinity(fra) ||
848fcf5ef2aSThomas Huth                  float64_is_infinity(frb) ||
849fcf5ef2aSThomas Huth                  float64_is_zero(frb))) {
850fcf5ef2aSThomas Huth         fe_flag = 1;
851fcf5ef2aSThomas Huth         fg_flag = 1;
852fcf5ef2aSThomas Huth     } else {
853fcf5ef2aSThomas Huth         int e_a = ppc_float64_get_unbiased_exp(fra);
854fcf5ef2aSThomas Huth         int e_b = ppc_float64_get_unbiased_exp(frb);
855fcf5ef2aSThomas Huth 
856fcf5ef2aSThomas Huth         if (unlikely(float64_is_any_nan(fra) ||
857fcf5ef2aSThomas Huth                      float64_is_any_nan(frb))) {
858fcf5ef2aSThomas Huth             fe_flag = 1;
859fcf5ef2aSThomas Huth         } else if ((e_b <= -1022) || (e_b >= 1021)) {
860fcf5ef2aSThomas Huth             fe_flag = 1;
861fcf5ef2aSThomas Huth         } else if (!float64_is_zero(fra) &&
862fcf5ef2aSThomas Huth                    (((e_a - e_b) >= 1023) ||
863fcf5ef2aSThomas Huth                     ((e_a - e_b) <= -1021) ||
864fcf5ef2aSThomas Huth                     (e_a <= -970))) {
865fcf5ef2aSThomas Huth             fe_flag = 1;
866fcf5ef2aSThomas Huth         }
867fcf5ef2aSThomas Huth 
868fcf5ef2aSThomas Huth         if (unlikely(float64_is_zero_or_denormal(frb))) {
869fcf5ef2aSThomas Huth             /* XB is not zero because of the above check and */
870fcf5ef2aSThomas Huth             /* so must be denormalized.                      */
871fcf5ef2aSThomas Huth             fg_flag = 1;
872fcf5ef2aSThomas Huth         }
873fcf5ef2aSThomas Huth     }
874fcf5ef2aSThomas Huth 
875fcf5ef2aSThomas Huth     return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
876fcf5ef2aSThomas Huth }
877fcf5ef2aSThomas Huth 
878fcf5ef2aSThomas Huth uint32_t helper_ftsqrt(uint64_t frb)
879fcf5ef2aSThomas Huth {
880fcf5ef2aSThomas Huth     int fe_flag = 0;
881fcf5ef2aSThomas Huth     int fg_flag = 0;
882fcf5ef2aSThomas Huth 
883fcf5ef2aSThomas Huth     if (unlikely(float64_is_infinity(frb) || float64_is_zero(frb))) {
884fcf5ef2aSThomas Huth         fe_flag = 1;
885fcf5ef2aSThomas Huth         fg_flag = 1;
886fcf5ef2aSThomas Huth     } else {
887fcf5ef2aSThomas Huth         int e_b = ppc_float64_get_unbiased_exp(frb);
888fcf5ef2aSThomas Huth 
889fcf5ef2aSThomas Huth         if (unlikely(float64_is_any_nan(frb))) {
890fcf5ef2aSThomas Huth             fe_flag = 1;
891fcf5ef2aSThomas Huth         } else if (unlikely(float64_is_zero(frb))) {
892fcf5ef2aSThomas Huth             fe_flag = 1;
893fcf5ef2aSThomas Huth         } else if (unlikely(float64_is_neg(frb))) {
894fcf5ef2aSThomas Huth             fe_flag = 1;
895fcf5ef2aSThomas Huth         } else if (!float64_is_zero(frb) && (e_b <= (-1022 + 52))) {
896fcf5ef2aSThomas Huth             fe_flag = 1;
897fcf5ef2aSThomas Huth         }
898fcf5ef2aSThomas Huth 
899fcf5ef2aSThomas Huth         if (unlikely(float64_is_zero_or_denormal(frb))) {
900fcf5ef2aSThomas Huth             /* XB is not zero because of the above check and */
901fcf5ef2aSThomas Huth             /* therefore must be denormalized.               */
902fcf5ef2aSThomas Huth             fg_flag = 1;
903fcf5ef2aSThomas Huth         }
904fcf5ef2aSThomas Huth     }
905fcf5ef2aSThomas Huth 
906fcf5ef2aSThomas Huth     return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
907fcf5ef2aSThomas Huth }
908fcf5ef2aSThomas Huth 
909fcf5ef2aSThomas Huth void helper_fcmpu(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
910fcf5ef2aSThomas Huth                   uint32_t crfD)
911fcf5ef2aSThomas Huth {
912fcf5ef2aSThomas Huth     CPU_DoubleU farg1, farg2;
913fcf5ef2aSThomas Huth     uint32_t ret = 0;
914fcf5ef2aSThomas Huth 
915fcf5ef2aSThomas Huth     farg1.ll = arg1;
916fcf5ef2aSThomas Huth     farg2.ll = arg2;
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(farg1.d) ||
919fcf5ef2aSThomas Huth                  float64_is_any_nan(farg2.d))) {
920fcf5ef2aSThomas Huth         ret = 0x01UL;
921fcf5ef2aSThomas Huth     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
922fcf5ef2aSThomas Huth         ret = 0x08UL;
923fcf5ef2aSThomas Huth     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
924fcf5ef2aSThomas Huth         ret = 0x04UL;
925fcf5ef2aSThomas Huth     } else {
926fcf5ef2aSThomas Huth         ret = 0x02UL;
927fcf5ef2aSThomas Huth     }
928fcf5ef2aSThomas Huth 
9295c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
9305c94dd38SPaul A. Clarke     env->fpscr |= ret << FPSCR_FPCC;
931fcf5ef2aSThomas Huth     env->crf[crfD] = ret;
932fcf5ef2aSThomas Huth     if (unlikely(ret == 0x01UL
933fcf5ef2aSThomas Huth                  && (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
934fcf5ef2aSThomas Huth                      float64_is_signaling_nan(farg2.d, &env->fp_status)))) {
935fcf5ef2aSThomas Huth         /* sNaN comparison */
93613c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
937fcf5ef2aSThomas Huth     }
938fcf5ef2aSThomas Huth }
939fcf5ef2aSThomas Huth 
940fcf5ef2aSThomas Huth void helper_fcmpo(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
941fcf5ef2aSThomas Huth                   uint32_t crfD)
942fcf5ef2aSThomas Huth {
943fcf5ef2aSThomas Huth     CPU_DoubleU farg1, farg2;
944fcf5ef2aSThomas Huth     uint32_t ret = 0;
945fcf5ef2aSThomas Huth 
946fcf5ef2aSThomas Huth     farg1.ll = arg1;
947fcf5ef2aSThomas Huth     farg2.ll = arg2;
948fcf5ef2aSThomas Huth 
949fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(farg1.d) ||
950fcf5ef2aSThomas Huth                  float64_is_any_nan(farg2.d))) {
951fcf5ef2aSThomas Huth         ret = 0x01UL;
952fcf5ef2aSThomas Huth     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
953fcf5ef2aSThomas Huth         ret = 0x08UL;
954fcf5ef2aSThomas Huth     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
955fcf5ef2aSThomas Huth         ret = 0x04UL;
956fcf5ef2aSThomas Huth     } else {
957fcf5ef2aSThomas Huth         ret = 0x02UL;
958fcf5ef2aSThomas Huth     }
959fcf5ef2aSThomas Huth 
9605c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
9615c94dd38SPaul A. Clarke     env->fpscr |= ret << FPSCR_FPCC;
9625c94dd38SPaul A. Clarke     env->crf[crfD] = (uint32_t) ret;
963fcf5ef2aSThomas Huth     if (unlikely(ret == 0x01UL)) {
96413c9115fSRichard Henderson         float_invalid_op_vxvc(env, 1, GETPC());
965fcf5ef2aSThomas Huth         if (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
966fcf5ef2aSThomas Huth             float64_is_signaling_nan(farg2.d, &env->fp_status)) {
967fcf5ef2aSThomas Huth             /* sNaN comparison */
96813c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
969fcf5ef2aSThomas Huth         }
970fcf5ef2aSThomas Huth     }
971fcf5ef2aSThomas Huth }
972fcf5ef2aSThomas Huth 
973fcf5ef2aSThomas Huth /* Single-precision floating-point conversions */
974fcf5ef2aSThomas Huth static inline uint32_t efscfsi(CPUPPCState *env, uint32_t val)
975fcf5ef2aSThomas Huth {
976fcf5ef2aSThomas Huth     CPU_FloatU u;
977fcf5ef2aSThomas Huth 
978fcf5ef2aSThomas Huth     u.f = int32_to_float32(val, &env->vec_status);
979fcf5ef2aSThomas Huth 
980fcf5ef2aSThomas Huth     return u.l;
981fcf5ef2aSThomas Huth }
982fcf5ef2aSThomas Huth 
983fcf5ef2aSThomas Huth static inline uint32_t efscfui(CPUPPCState *env, uint32_t val)
984fcf5ef2aSThomas Huth {
985fcf5ef2aSThomas Huth     CPU_FloatU u;
986fcf5ef2aSThomas Huth 
987fcf5ef2aSThomas Huth     u.f = uint32_to_float32(val, &env->vec_status);
988fcf5ef2aSThomas Huth 
989fcf5ef2aSThomas Huth     return u.l;
990fcf5ef2aSThomas Huth }
991fcf5ef2aSThomas Huth 
992fcf5ef2aSThomas Huth static inline int32_t efsctsi(CPUPPCState *env, uint32_t val)
993fcf5ef2aSThomas Huth {
994fcf5ef2aSThomas Huth     CPU_FloatU u;
995fcf5ef2aSThomas Huth 
996fcf5ef2aSThomas Huth     u.l = val;
997fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
998fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
999fcf5ef2aSThomas Huth         return 0;
1000fcf5ef2aSThomas Huth     }
1001fcf5ef2aSThomas Huth 
1002fcf5ef2aSThomas Huth     return float32_to_int32(u.f, &env->vec_status);
1003fcf5ef2aSThomas Huth }
1004fcf5ef2aSThomas Huth 
1005fcf5ef2aSThomas Huth static inline uint32_t efsctui(CPUPPCState *env, uint32_t val)
1006fcf5ef2aSThomas Huth {
1007fcf5ef2aSThomas Huth     CPU_FloatU u;
1008fcf5ef2aSThomas Huth 
1009fcf5ef2aSThomas Huth     u.l = val;
1010fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1011fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1012fcf5ef2aSThomas Huth         return 0;
1013fcf5ef2aSThomas Huth     }
1014fcf5ef2aSThomas Huth 
1015fcf5ef2aSThomas Huth     return float32_to_uint32(u.f, &env->vec_status);
1016fcf5ef2aSThomas Huth }
1017fcf5ef2aSThomas Huth 
1018fcf5ef2aSThomas Huth static inline uint32_t efsctsiz(CPUPPCState *env, uint32_t val)
1019fcf5ef2aSThomas Huth {
1020fcf5ef2aSThomas Huth     CPU_FloatU u;
1021fcf5ef2aSThomas Huth 
1022fcf5ef2aSThomas Huth     u.l = val;
1023fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1024fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1025fcf5ef2aSThomas Huth         return 0;
1026fcf5ef2aSThomas Huth     }
1027fcf5ef2aSThomas Huth 
1028fcf5ef2aSThomas Huth     return float32_to_int32_round_to_zero(u.f, &env->vec_status);
1029fcf5ef2aSThomas Huth }
1030fcf5ef2aSThomas Huth 
1031fcf5ef2aSThomas Huth static inline uint32_t efsctuiz(CPUPPCState *env, uint32_t val)
1032fcf5ef2aSThomas Huth {
1033fcf5ef2aSThomas Huth     CPU_FloatU u;
1034fcf5ef2aSThomas Huth 
1035fcf5ef2aSThomas Huth     u.l = val;
1036fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1037fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1038fcf5ef2aSThomas Huth         return 0;
1039fcf5ef2aSThomas Huth     }
1040fcf5ef2aSThomas Huth 
1041fcf5ef2aSThomas Huth     return float32_to_uint32_round_to_zero(u.f, &env->vec_status);
1042fcf5ef2aSThomas Huth }
1043fcf5ef2aSThomas Huth 
1044fcf5ef2aSThomas Huth static inline uint32_t efscfsf(CPUPPCState *env, uint32_t val)
1045fcf5ef2aSThomas Huth {
1046fcf5ef2aSThomas Huth     CPU_FloatU u;
1047fcf5ef2aSThomas Huth     float32 tmp;
1048fcf5ef2aSThomas Huth 
1049fcf5ef2aSThomas Huth     u.f = int32_to_float32(val, &env->vec_status);
1050fcf5ef2aSThomas Huth     tmp = int64_to_float32(1ULL << 32, &env->vec_status);
1051fcf5ef2aSThomas Huth     u.f = float32_div(u.f, tmp, &env->vec_status);
1052fcf5ef2aSThomas Huth 
1053fcf5ef2aSThomas Huth     return u.l;
1054fcf5ef2aSThomas Huth }
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth static inline uint32_t efscfuf(CPUPPCState *env, uint32_t val)
1057fcf5ef2aSThomas Huth {
1058fcf5ef2aSThomas Huth     CPU_FloatU u;
1059fcf5ef2aSThomas Huth     float32 tmp;
1060fcf5ef2aSThomas Huth 
1061fcf5ef2aSThomas Huth     u.f = uint32_to_float32(val, &env->vec_status);
1062fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1063fcf5ef2aSThomas Huth     u.f = float32_div(u.f, tmp, &env->vec_status);
1064fcf5ef2aSThomas Huth 
1065fcf5ef2aSThomas Huth     return u.l;
1066fcf5ef2aSThomas Huth }
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth static inline uint32_t efsctsf(CPUPPCState *env, uint32_t val)
1069fcf5ef2aSThomas Huth {
1070fcf5ef2aSThomas Huth     CPU_FloatU u;
1071fcf5ef2aSThomas Huth     float32 tmp;
1072fcf5ef2aSThomas Huth 
1073fcf5ef2aSThomas Huth     u.l = val;
1074fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1075fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1076fcf5ef2aSThomas Huth         return 0;
1077fcf5ef2aSThomas Huth     }
1078fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1079fcf5ef2aSThomas Huth     u.f = float32_mul(u.f, tmp, &env->vec_status);
1080fcf5ef2aSThomas Huth 
1081fcf5ef2aSThomas Huth     return float32_to_int32(u.f, &env->vec_status);
1082fcf5ef2aSThomas Huth }
1083fcf5ef2aSThomas Huth 
1084fcf5ef2aSThomas Huth static inline uint32_t efsctuf(CPUPPCState *env, uint32_t val)
1085fcf5ef2aSThomas Huth {
1086fcf5ef2aSThomas Huth     CPU_FloatU u;
1087fcf5ef2aSThomas Huth     float32 tmp;
1088fcf5ef2aSThomas Huth 
1089fcf5ef2aSThomas Huth     u.l = val;
1090fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1091fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1092fcf5ef2aSThomas Huth         return 0;
1093fcf5ef2aSThomas Huth     }
1094fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1095fcf5ef2aSThomas Huth     u.f = float32_mul(u.f, tmp, &env->vec_status);
1096fcf5ef2aSThomas Huth 
1097fcf5ef2aSThomas Huth     return float32_to_uint32(u.f, &env->vec_status);
1098fcf5ef2aSThomas Huth }
1099fcf5ef2aSThomas Huth 
1100fcf5ef2aSThomas Huth #define HELPER_SPE_SINGLE_CONV(name)                              \
1101fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t val)       \
1102fcf5ef2aSThomas Huth     {                                                             \
1103fcf5ef2aSThomas Huth         return e##name(env, val);                                 \
1104fcf5ef2aSThomas Huth     }
1105fcf5ef2aSThomas Huth /* efscfsi */
1106fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfsi);
1107fcf5ef2aSThomas Huth /* efscfui */
1108fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfui);
1109fcf5ef2aSThomas Huth /* efscfuf */
1110fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfuf);
1111fcf5ef2aSThomas Huth /* efscfsf */
1112fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfsf);
1113fcf5ef2aSThomas Huth /* efsctsi */
1114fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsi);
1115fcf5ef2aSThomas Huth /* efsctui */
1116fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctui);
1117fcf5ef2aSThomas Huth /* efsctsiz */
1118fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsiz);
1119fcf5ef2aSThomas Huth /* efsctuiz */
1120fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctuiz);
1121fcf5ef2aSThomas Huth /* efsctsf */
1122fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsf);
1123fcf5ef2aSThomas Huth /* efsctuf */
1124fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctuf);
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth #define HELPER_SPE_VECTOR_CONV(name)                            \
1127fcf5ef2aSThomas Huth     uint64_t helper_ev##name(CPUPPCState *env, uint64_t val)    \
1128fcf5ef2aSThomas Huth     {                                                           \
1129fcf5ef2aSThomas Huth         return ((uint64_t)e##name(env, val >> 32) << 32) |      \
1130fcf5ef2aSThomas Huth             (uint64_t)e##name(env, val);                        \
1131fcf5ef2aSThomas Huth     }
1132fcf5ef2aSThomas Huth /* evfscfsi */
1133fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfsi);
1134fcf5ef2aSThomas Huth /* evfscfui */
1135fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfui);
1136fcf5ef2aSThomas Huth /* evfscfuf */
1137fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfuf);
1138fcf5ef2aSThomas Huth /* evfscfsf */
1139fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfsf);
1140fcf5ef2aSThomas Huth /* evfsctsi */
1141fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsi);
1142fcf5ef2aSThomas Huth /* evfsctui */
1143fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctui);
1144fcf5ef2aSThomas Huth /* evfsctsiz */
1145fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsiz);
1146fcf5ef2aSThomas Huth /* evfsctuiz */
1147fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctuiz);
1148fcf5ef2aSThomas Huth /* evfsctsf */
1149fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsf);
1150fcf5ef2aSThomas Huth /* evfsctuf */
1151fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctuf);
1152fcf5ef2aSThomas Huth 
1153fcf5ef2aSThomas Huth /* Single-precision floating-point arithmetic */
1154fcf5ef2aSThomas Huth static inline uint32_t efsadd(CPUPPCState *env, uint32_t op1, uint32_t op2)
1155fcf5ef2aSThomas Huth {
1156fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1157fcf5ef2aSThomas Huth 
1158fcf5ef2aSThomas Huth     u1.l = op1;
1159fcf5ef2aSThomas Huth     u2.l = op2;
1160fcf5ef2aSThomas Huth     u1.f = float32_add(u1.f, u2.f, &env->vec_status);
1161fcf5ef2aSThomas Huth     return u1.l;
1162fcf5ef2aSThomas Huth }
1163fcf5ef2aSThomas Huth 
1164fcf5ef2aSThomas Huth static inline uint32_t efssub(CPUPPCState *env, uint32_t op1, uint32_t op2)
1165fcf5ef2aSThomas Huth {
1166fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1167fcf5ef2aSThomas Huth 
1168fcf5ef2aSThomas Huth     u1.l = op1;
1169fcf5ef2aSThomas Huth     u2.l = op2;
1170fcf5ef2aSThomas Huth     u1.f = float32_sub(u1.f, u2.f, &env->vec_status);
1171fcf5ef2aSThomas Huth     return u1.l;
1172fcf5ef2aSThomas Huth }
1173fcf5ef2aSThomas Huth 
1174fcf5ef2aSThomas Huth static inline uint32_t efsmul(CPUPPCState *env, uint32_t op1, uint32_t op2)
1175fcf5ef2aSThomas Huth {
1176fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1177fcf5ef2aSThomas Huth 
1178fcf5ef2aSThomas Huth     u1.l = op1;
1179fcf5ef2aSThomas Huth     u2.l = op2;
1180fcf5ef2aSThomas Huth     u1.f = float32_mul(u1.f, u2.f, &env->vec_status);
1181fcf5ef2aSThomas Huth     return u1.l;
1182fcf5ef2aSThomas Huth }
1183fcf5ef2aSThomas Huth 
1184fcf5ef2aSThomas Huth static inline uint32_t efsdiv(CPUPPCState *env, uint32_t op1, uint32_t op2)
1185fcf5ef2aSThomas Huth {
1186fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1187fcf5ef2aSThomas Huth 
1188fcf5ef2aSThomas Huth     u1.l = op1;
1189fcf5ef2aSThomas Huth     u2.l = op2;
1190fcf5ef2aSThomas Huth     u1.f = float32_div(u1.f, u2.f, &env->vec_status);
1191fcf5ef2aSThomas Huth     return u1.l;
1192fcf5ef2aSThomas Huth }
1193fcf5ef2aSThomas Huth 
1194fcf5ef2aSThomas Huth #define HELPER_SPE_SINGLE_ARITH(name)                                   \
1195fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1196fcf5ef2aSThomas Huth     {                                                                   \
1197fcf5ef2aSThomas Huth         return e##name(env, op1, op2);                                  \
1198fcf5ef2aSThomas Huth     }
1199fcf5ef2aSThomas Huth /* efsadd */
1200fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsadd);
1201fcf5ef2aSThomas Huth /* efssub */
1202fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fssub);
1203fcf5ef2aSThomas Huth /* efsmul */
1204fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsmul);
1205fcf5ef2aSThomas Huth /* efsdiv */
1206fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsdiv);
1207fcf5ef2aSThomas Huth 
1208fcf5ef2aSThomas Huth #define HELPER_SPE_VECTOR_ARITH(name)                                   \
1209fcf5ef2aSThomas Huth     uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1210fcf5ef2aSThomas Huth     {                                                                   \
1211fcf5ef2aSThomas Huth         return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) |   \
1212fcf5ef2aSThomas Huth             (uint64_t)e##name(env, op1, op2);                           \
1213fcf5ef2aSThomas Huth     }
1214fcf5ef2aSThomas Huth /* evfsadd */
1215fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsadd);
1216fcf5ef2aSThomas Huth /* evfssub */
1217fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fssub);
1218fcf5ef2aSThomas Huth /* evfsmul */
1219fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsmul);
1220fcf5ef2aSThomas Huth /* evfsdiv */
1221fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsdiv);
1222fcf5ef2aSThomas Huth 
1223fcf5ef2aSThomas Huth /* Single-precision floating-point comparisons */
1224fcf5ef2aSThomas Huth static inline uint32_t efscmplt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1225fcf5ef2aSThomas Huth {
1226fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1227fcf5ef2aSThomas Huth 
1228fcf5ef2aSThomas Huth     u1.l = op1;
1229fcf5ef2aSThomas Huth     u2.l = op2;
1230fcf5ef2aSThomas Huth     return float32_lt(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1231fcf5ef2aSThomas Huth }
1232fcf5ef2aSThomas Huth 
1233fcf5ef2aSThomas Huth static inline uint32_t efscmpgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1234fcf5ef2aSThomas Huth {
1235fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1236fcf5ef2aSThomas Huth 
1237fcf5ef2aSThomas Huth     u1.l = op1;
1238fcf5ef2aSThomas Huth     u2.l = op2;
1239fcf5ef2aSThomas Huth     return float32_le(u1.f, u2.f, &env->vec_status) ? 0 : 4;
1240fcf5ef2aSThomas Huth }
1241fcf5ef2aSThomas Huth 
1242fcf5ef2aSThomas Huth static inline uint32_t efscmpeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1243fcf5ef2aSThomas Huth {
1244fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1245fcf5ef2aSThomas Huth 
1246fcf5ef2aSThomas Huth     u1.l = op1;
1247fcf5ef2aSThomas Huth     u2.l = op2;
1248fcf5ef2aSThomas Huth     return float32_eq(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1249fcf5ef2aSThomas Huth }
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth static inline uint32_t efststlt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1252fcf5ef2aSThomas Huth {
1253fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1254fcf5ef2aSThomas Huth     return efscmplt(env, op1, op2);
1255fcf5ef2aSThomas Huth }
1256fcf5ef2aSThomas Huth 
1257fcf5ef2aSThomas Huth static inline uint32_t efststgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1258fcf5ef2aSThomas Huth {
1259fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1260fcf5ef2aSThomas Huth     return efscmpgt(env, op1, op2);
1261fcf5ef2aSThomas Huth }
1262fcf5ef2aSThomas Huth 
1263fcf5ef2aSThomas Huth static inline uint32_t efststeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1264fcf5ef2aSThomas Huth {
1265fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1266fcf5ef2aSThomas Huth     return efscmpeq(env, op1, op2);
1267fcf5ef2aSThomas Huth }
1268fcf5ef2aSThomas Huth 
1269fcf5ef2aSThomas Huth #define HELPER_SINGLE_SPE_CMP(name)                                     \
1270fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1271fcf5ef2aSThomas Huth     {                                                                   \
1272fcf5ef2aSThomas Huth         return e##name(env, op1, op2);                                  \
1273fcf5ef2aSThomas Huth     }
1274fcf5ef2aSThomas Huth /* efststlt */
1275fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststlt);
1276fcf5ef2aSThomas Huth /* efststgt */
1277fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststgt);
1278fcf5ef2aSThomas Huth /* efststeq */
1279fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststeq);
1280fcf5ef2aSThomas Huth /* efscmplt */
1281fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmplt);
1282fcf5ef2aSThomas Huth /* efscmpgt */
1283fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmpgt);
1284fcf5ef2aSThomas Huth /* efscmpeq */
1285fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmpeq);
1286fcf5ef2aSThomas Huth 
1287fcf5ef2aSThomas Huth static inline uint32_t evcmp_merge(int t0, int t1)
1288fcf5ef2aSThomas Huth {
1289fcf5ef2aSThomas Huth     return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
1290fcf5ef2aSThomas Huth }
1291fcf5ef2aSThomas Huth 
1292fcf5ef2aSThomas Huth #define HELPER_VECTOR_SPE_CMP(name)                                     \
1293fcf5ef2aSThomas Huth     uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1294fcf5ef2aSThomas Huth     {                                                                   \
1295fcf5ef2aSThomas Huth         return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32),          \
1296fcf5ef2aSThomas Huth                            e##name(env, op1, op2));                     \
1297fcf5ef2aSThomas Huth     }
1298fcf5ef2aSThomas Huth /* evfststlt */
1299fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststlt);
1300fcf5ef2aSThomas Huth /* evfststgt */
1301fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststgt);
1302fcf5ef2aSThomas Huth /* evfststeq */
1303fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststeq);
1304fcf5ef2aSThomas Huth /* evfscmplt */
1305fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmplt);
1306fcf5ef2aSThomas Huth /* evfscmpgt */
1307fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmpgt);
1308fcf5ef2aSThomas Huth /* evfscmpeq */
1309fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmpeq);
1310fcf5ef2aSThomas Huth 
1311fcf5ef2aSThomas Huth /* Double-precision floating-point conversion */
1312fcf5ef2aSThomas Huth uint64_t helper_efdcfsi(CPUPPCState *env, uint32_t val)
1313fcf5ef2aSThomas Huth {
1314fcf5ef2aSThomas Huth     CPU_DoubleU u;
1315fcf5ef2aSThomas Huth 
1316fcf5ef2aSThomas Huth     u.d = int32_to_float64(val, &env->vec_status);
1317fcf5ef2aSThomas Huth 
1318fcf5ef2aSThomas Huth     return u.ll;
1319fcf5ef2aSThomas Huth }
1320fcf5ef2aSThomas Huth 
1321fcf5ef2aSThomas Huth uint64_t helper_efdcfsid(CPUPPCState *env, uint64_t val)
1322fcf5ef2aSThomas Huth {
1323fcf5ef2aSThomas Huth     CPU_DoubleU u;
1324fcf5ef2aSThomas Huth 
1325fcf5ef2aSThomas Huth     u.d = int64_to_float64(val, &env->vec_status);
1326fcf5ef2aSThomas Huth 
1327fcf5ef2aSThomas Huth     return u.ll;
1328fcf5ef2aSThomas Huth }
1329fcf5ef2aSThomas Huth 
1330fcf5ef2aSThomas Huth uint64_t helper_efdcfui(CPUPPCState *env, uint32_t val)
1331fcf5ef2aSThomas Huth {
1332fcf5ef2aSThomas Huth     CPU_DoubleU u;
1333fcf5ef2aSThomas Huth 
1334fcf5ef2aSThomas Huth     u.d = uint32_to_float64(val, &env->vec_status);
1335fcf5ef2aSThomas Huth 
1336fcf5ef2aSThomas Huth     return u.ll;
1337fcf5ef2aSThomas Huth }
1338fcf5ef2aSThomas Huth 
1339fcf5ef2aSThomas Huth uint64_t helper_efdcfuid(CPUPPCState *env, uint64_t val)
1340fcf5ef2aSThomas Huth {
1341fcf5ef2aSThomas Huth     CPU_DoubleU u;
1342fcf5ef2aSThomas Huth 
1343fcf5ef2aSThomas Huth     u.d = uint64_to_float64(val, &env->vec_status);
1344fcf5ef2aSThomas Huth 
1345fcf5ef2aSThomas Huth     return u.ll;
1346fcf5ef2aSThomas Huth }
1347fcf5ef2aSThomas Huth 
1348fcf5ef2aSThomas Huth uint32_t helper_efdctsi(CPUPPCState *env, uint64_t val)
1349fcf5ef2aSThomas Huth {
1350fcf5ef2aSThomas Huth     CPU_DoubleU u;
1351fcf5ef2aSThomas Huth 
1352fcf5ef2aSThomas Huth     u.ll = val;
1353fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1354fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1355fcf5ef2aSThomas Huth         return 0;
1356fcf5ef2aSThomas Huth     }
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth     return float64_to_int32(u.d, &env->vec_status);
1359fcf5ef2aSThomas Huth }
1360fcf5ef2aSThomas Huth 
1361fcf5ef2aSThomas Huth uint32_t helper_efdctui(CPUPPCState *env, uint64_t val)
1362fcf5ef2aSThomas Huth {
1363fcf5ef2aSThomas Huth     CPU_DoubleU u;
1364fcf5ef2aSThomas Huth 
1365fcf5ef2aSThomas Huth     u.ll = val;
1366fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1367fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1368fcf5ef2aSThomas Huth         return 0;
1369fcf5ef2aSThomas Huth     }
1370fcf5ef2aSThomas Huth 
1371fcf5ef2aSThomas Huth     return float64_to_uint32(u.d, &env->vec_status);
1372fcf5ef2aSThomas Huth }
1373fcf5ef2aSThomas Huth 
1374fcf5ef2aSThomas Huth uint32_t helper_efdctsiz(CPUPPCState *env, uint64_t val)
1375fcf5ef2aSThomas Huth {
1376fcf5ef2aSThomas Huth     CPU_DoubleU u;
1377fcf5ef2aSThomas Huth 
1378fcf5ef2aSThomas Huth     u.ll = val;
1379fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1380fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1381fcf5ef2aSThomas Huth         return 0;
1382fcf5ef2aSThomas Huth     }
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth     return float64_to_int32_round_to_zero(u.d, &env->vec_status);
1385fcf5ef2aSThomas Huth }
1386fcf5ef2aSThomas Huth 
1387fcf5ef2aSThomas Huth uint64_t helper_efdctsidz(CPUPPCState *env, uint64_t val)
1388fcf5ef2aSThomas Huth {
1389fcf5ef2aSThomas Huth     CPU_DoubleU u;
1390fcf5ef2aSThomas Huth 
1391fcf5ef2aSThomas Huth     u.ll = val;
1392fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1393fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1394fcf5ef2aSThomas Huth         return 0;
1395fcf5ef2aSThomas Huth     }
1396fcf5ef2aSThomas Huth 
1397fcf5ef2aSThomas Huth     return float64_to_int64_round_to_zero(u.d, &env->vec_status);
1398fcf5ef2aSThomas Huth }
1399fcf5ef2aSThomas Huth 
1400fcf5ef2aSThomas Huth uint32_t helper_efdctuiz(CPUPPCState *env, uint64_t val)
1401fcf5ef2aSThomas Huth {
1402fcf5ef2aSThomas Huth     CPU_DoubleU u;
1403fcf5ef2aSThomas Huth 
1404fcf5ef2aSThomas Huth     u.ll = val;
1405fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1406fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1407fcf5ef2aSThomas Huth         return 0;
1408fcf5ef2aSThomas Huth     }
1409fcf5ef2aSThomas Huth 
1410fcf5ef2aSThomas Huth     return float64_to_uint32_round_to_zero(u.d, &env->vec_status);
1411fcf5ef2aSThomas Huth }
1412fcf5ef2aSThomas Huth 
1413fcf5ef2aSThomas Huth uint64_t helper_efdctuidz(CPUPPCState *env, uint64_t val)
1414fcf5ef2aSThomas Huth {
1415fcf5ef2aSThomas Huth     CPU_DoubleU u;
1416fcf5ef2aSThomas Huth 
1417fcf5ef2aSThomas Huth     u.ll = val;
1418fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1419fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1420fcf5ef2aSThomas Huth         return 0;
1421fcf5ef2aSThomas Huth     }
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth     return float64_to_uint64_round_to_zero(u.d, &env->vec_status);
1424fcf5ef2aSThomas Huth }
1425fcf5ef2aSThomas Huth 
1426fcf5ef2aSThomas Huth uint64_t helper_efdcfsf(CPUPPCState *env, uint32_t val)
1427fcf5ef2aSThomas Huth {
1428fcf5ef2aSThomas Huth     CPU_DoubleU u;
1429fcf5ef2aSThomas Huth     float64 tmp;
1430fcf5ef2aSThomas Huth 
1431fcf5ef2aSThomas Huth     u.d = int32_to_float64(val, &env->vec_status);
1432fcf5ef2aSThomas Huth     tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1433fcf5ef2aSThomas Huth     u.d = float64_div(u.d, tmp, &env->vec_status);
1434fcf5ef2aSThomas Huth 
1435fcf5ef2aSThomas Huth     return u.ll;
1436fcf5ef2aSThomas Huth }
1437fcf5ef2aSThomas Huth 
1438fcf5ef2aSThomas Huth uint64_t helper_efdcfuf(CPUPPCState *env, uint32_t val)
1439fcf5ef2aSThomas Huth {
1440fcf5ef2aSThomas Huth     CPU_DoubleU u;
1441fcf5ef2aSThomas Huth     float64 tmp;
1442fcf5ef2aSThomas Huth 
1443fcf5ef2aSThomas Huth     u.d = uint32_to_float64(val, &env->vec_status);
1444fcf5ef2aSThomas Huth     tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1445fcf5ef2aSThomas Huth     u.d = float64_div(u.d, tmp, &env->vec_status);
1446fcf5ef2aSThomas Huth 
1447fcf5ef2aSThomas Huth     return u.ll;
1448fcf5ef2aSThomas Huth }
1449fcf5ef2aSThomas Huth 
1450fcf5ef2aSThomas Huth uint32_t helper_efdctsf(CPUPPCState *env, uint64_t val)
1451fcf5ef2aSThomas Huth {
1452fcf5ef2aSThomas Huth     CPU_DoubleU u;
1453fcf5ef2aSThomas Huth     float64 tmp;
1454fcf5ef2aSThomas Huth 
1455fcf5ef2aSThomas Huth     u.ll = val;
1456fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1457fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1458fcf5ef2aSThomas Huth         return 0;
1459fcf5ef2aSThomas Huth     }
1460fcf5ef2aSThomas Huth     tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1461fcf5ef2aSThomas Huth     u.d = float64_mul(u.d, tmp, &env->vec_status);
1462fcf5ef2aSThomas Huth 
1463fcf5ef2aSThomas Huth     return float64_to_int32(u.d, &env->vec_status);
1464fcf5ef2aSThomas Huth }
1465fcf5ef2aSThomas Huth 
1466fcf5ef2aSThomas Huth uint32_t helper_efdctuf(CPUPPCState *env, uint64_t val)
1467fcf5ef2aSThomas Huth {
1468fcf5ef2aSThomas Huth     CPU_DoubleU u;
1469fcf5ef2aSThomas Huth     float64 tmp;
1470fcf5ef2aSThomas Huth 
1471fcf5ef2aSThomas Huth     u.ll = val;
1472fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1473fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1474fcf5ef2aSThomas Huth         return 0;
1475fcf5ef2aSThomas Huth     }
1476fcf5ef2aSThomas Huth     tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1477fcf5ef2aSThomas Huth     u.d = float64_mul(u.d, tmp, &env->vec_status);
1478fcf5ef2aSThomas Huth 
1479fcf5ef2aSThomas Huth     return float64_to_uint32(u.d, &env->vec_status);
1480fcf5ef2aSThomas Huth }
1481fcf5ef2aSThomas Huth 
1482fcf5ef2aSThomas Huth uint32_t helper_efscfd(CPUPPCState *env, uint64_t val)
1483fcf5ef2aSThomas Huth {
1484fcf5ef2aSThomas Huth     CPU_DoubleU u1;
1485fcf5ef2aSThomas Huth     CPU_FloatU u2;
1486fcf5ef2aSThomas Huth 
1487fcf5ef2aSThomas Huth     u1.ll = val;
1488fcf5ef2aSThomas Huth     u2.f = float64_to_float32(u1.d, &env->vec_status);
1489fcf5ef2aSThomas Huth 
1490fcf5ef2aSThomas Huth     return u2.l;
1491fcf5ef2aSThomas Huth }
1492fcf5ef2aSThomas Huth 
1493fcf5ef2aSThomas Huth uint64_t helper_efdcfs(CPUPPCState *env, uint32_t val)
1494fcf5ef2aSThomas Huth {
1495fcf5ef2aSThomas Huth     CPU_DoubleU u2;
1496fcf5ef2aSThomas Huth     CPU_FloatU u1;
1497fcf5ef2aSThomas Huth 
1498fcf5ef2aSThomas Huth     u1.l = val;
1499fcf5ef2aSThomas Huth     u2.d = float32_to_float64(u1.f, &env->vec_status);
1500fcf5ef2aSThomas Huth 
1501fcf5ef2aSThomas Huth     return u2.ll;
1502fcf5ef2aSThomas Huth }
1503fcf5ef2aSThomas Huth 
1504fcf5ef2aSThomas Huth /* Double precision fixed-point arithmetic */
1505fcf5ef2aSThomas Huth uint64_t helper_efdadd(CPUPPCState *env, uint64_t op1, uint64_t op2)
1506fcf5ef2aSThomas Huth {
1507fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1508fcf5ef2aSThomas Huth 
1509fcf5ef2aSThomas Huth     u1.ll = op1;
1510fcf5ef2aSThomas Huth     u2.ll = op2;
1511fcf5ef2aSThomas Huth     u1.d = float64_add(u1.d, u2.d, &env->vec_status);
1512fcf5ef2aSThomas Huth     return u1.ll;
1513fcf5ef2aSThomas Huth }
1514fcf5ef2aSThomas Huth 
1515fcf5ef2aSThomas Huth uint64_t helper_efdsub(CPUPPCState *env, uint64_t op1, uint64_t op2)
1516fcf5ef2aSThomas Huth {
1517fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1518fcf5ef2aSThomas Huth 
1519fcf5ef2aSThomas Huth     u1.ll = op1;
1520fcf5ef2aSThomas Huth     u2.ll = op2;
1521fcf5ef2aSThomas Huth     u1.d = float64_sub(u1.d, u2.d, &env->vec_status);
1522fcf5ef2aSThomas Huth     return u1.ll;
1523fcf5ef2aSThomas Huth }
1524fcf5ef2aSThomas Huth 
1525fcf5ef2aSThomas Huth uint64_t helper_efdmul(CPUPPCState *env, uint64_t op1, uint64_t op2)
1526fcf5ef2aSThomas Huth {
1527fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1528fcf5ef2aSThomas Huth 
1529fcf5ef2aSThomas Huth     u1.ll = op1;
1530fcf5ef2aSThomas Huth     u2.ll = op2;
1531fcf5ef2aSThomas Huth     u1.d = float64_mul(u1.d, u2.d, &env->vec_status);
1532fcf5ef2aSThomas Huth     return u1.ll;
1533fcf5ef2aSThomas Huth }
1534fcf5ef2aSThomas Huth 
1535fcf5ef2aSThomas Huth uint64_t helper_efddiv(CPUPPCState *env, uint64_t op1, uint64_t op2)
1536fcf5ef2aSThomas Huth {
1537fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1538fcf5ef2aSThomas Huth 
1539fcf5ef2aSThomas Huth     u1.ll = op1;
1540fcf5ef2aSThomas Huth     u2.ll = op2;
1541fcf5ef2aSThomas Huth     u1.d = float64_div(u1.d, u2.d, &env->vec_status);
1542fcf5ef2aSThomas Huth     return u1.ll;
1543fcf5ef2aSThomas Huth }
1544fcf5ef2aSThomas Huth 
1545fcf5ef2aSThomas Huth /* Double precision floating point helpers */
1546fcf5ef2aSThomas Huth uint32_t helper_efdtstlt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1547fcf5ef2aSThomas Huth {
1548fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1549fcf5ef2aSThomas Huth 
1550fcf5ef2aSThomas Huth     u1.ll = op1;
1551fcf5ef2aSThomas Huth     u2.ll = op2;
1552fcf5ef2aSThomas Huth     return float64_lt(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1553fcf5ef2aSThomas Huth }
1554fcf5ef2aSThomas Huth 
1555fcf5ef2aSThomas Huth uint32_t helper_efdtstgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1556fcf5ef2aSThomas Huth {
1557fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1558fcf5ef2aSThomas Huth 
1559fcf5ef2aSThomas Huth     u1.ll = op1;
1560fcf5ef2aSThomas Huth     u2.ll = op2;
1561fcf5ef2aSThomas Huth     return float64_le(u1.d, u2.d, &env->vec_status) ? 0 : 4;
1562fcf5ef2aSThomas Huth }
1563fcf5ef2aSThomas Huth 
1564fcf5ef2aSThomas Huth uint32_t helper_efdtsteq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1565fcf5ef2aSThomas Huth {
1566fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1567fcf5ef2aSThomas Huth 
1568fcf5ef2aSThomas Huth     u1.ll = op1;
1569fcf5ef2aSThomas Huth     u2.ll = op2;
1570fcf5ef2aSThomas Huth     return float64_eq_quiet(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1571fcf5ef2aSThomas Huth }
1572fcf5ef2aSThomas Huth 
1573fcf5ef2aSThomas Huth uint32_t helper_efdcmplt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1574fcf5ef2aSThomas Huth {
1575fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1576fcf5ef2aSThomas Huth     return helper_efdtstlt(env, op1, op2);
1577fcf5ef2aSThomas Huth }
1578fcf5ef2aSThomas Huth 
1579fcf5ef2aSThomas Huth uint32_t helper_efdcmpgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1580fcf5ef2aSThomas Huth {
1581fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1582fcf5ef2aSThomas Huth     return helper_efdtstgt(env, op1, op2);
1583fcf5ef2aSThomas Huth }
1584fcf5ef2aSThomas Huth 
1585fcf5ef2aSThomas Huth uint32_t helper_efdcmpeq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1586fcf5ef2aSThomas Huth {
1587fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1588fcf5ef2aSThomas Huth     return helper_efdtsteq(env, op1, op2);
1589fcf5ef2aSThomas Huth }
1590fcf5ef2aSThomas Huth 
1591fcf5ef2aSThomas Huth #define float64_to_float64(x, env) x
1592fcf5ef2aSThomas Huth 
1593fcf5ef2aSThomas Huth 
1594fa9ebf8cSDavid Gibson /*
1595136fbf65Szhaolichang  * VSX_ADD_SUB - VSX floating point add/subtract
1596fcf5ef2aSThomas Huth  *   name  - instruction mnemonic
1597fcf5ef2aSThomas Huth  *   op    - operation (add or sub)
1598fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1599fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1600fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1601fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1602fcf5ef2aSThomas Huth  */
1603fcf5ef2aSThomas Huth #define VSX_ADD_SUB(name, op, nels, tp, fld, sfprf, r2sp)                    \
160499125c74SMark Cave-Ayland void helper_##name(CPUPPCState *env, ppc_vsr_t *xt,                          \
160599125c74SMark Cave-Ayland                    ppc_vsr_t *xa, ppc_vsr_t *xb)                             \
1606fcf5ef2aSThomas Huth {                                                                            \
1607cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
1608fcf5ef2aSThomas Huth     int i;                                                                   \
1609fcf5ef2aSThomas Huth                                                                              \
1610fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1611fcf5ef2aSThomas Huth                                                                              \
1612fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1613fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1614fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1615cf3b0334SMark Cave-Ayland         t.fld = tp##_##op(xa->fld, xb->fld, &tstat);                         \
1616fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1617fcf5ef2aSThomas Huth                                                                              \
1618fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
161957483867SRichard Henderson             float_invalid_op_addsub(env, sfprf, GETPC(),                     \
1620cf3b0334SMark Cave-Ayland                                     tp##_classify(xa->fld) |                 \
1621cf3b0334SMark Cave-Ayland                                     tp##_classify(xb->fld));                 \
1622fcf5ef2aSThomas Huth         }                                                                    \
1623fcf5ef2aSThomas Huth                                                                              \
1624fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
1625cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
1626fcf5ef2aSThomas Huth         }                                                                    \
1627fcf5ef2aSThomas Huth                                                                              \
1628fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
1629cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1630fcf5ef2aSThomas Huth         }                                                                    \
1631fcf5ef2aSThomas Huth     }                                                                        \
1632cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
16336525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
1634fcf5ef2aSThomas Huth }
1635fcf5ef2aSThomas Huth 
1636fcf5ef2aSThomas Huth VSX_ADD_SUB(xsadddp, add, 1, float64, VsrD(0), 1, 0)
1637fcf5ef2aSThomas Huth VSX_ADD_SUB(xsaddsp, add, 1, float64, VsrD(0), 1, 1)
1638fcf5ef2aSThomas Huth VSX_ADD_SUB(xvadddp, add, 2, float64, VsrD(i), 0, 0)
1639fcf5ef2aSThomas Huth VSX_ADD_SUB(xvaddsp, add, 4, float32, VsrW(i), 0, 0)
1640fcf5ef2aSThomas Huth VSX_ADD_SUB(xssubdp, sub, 1, float64, VsrD(0), 1, 0)
1641fcf5ef2aSThomas Huth VSX_ADD_SUB(xssubsp, sub, 1, float64, VsrD(0), 1, 1)
1642fcf5ef2aSThomas Huth VSX_ADD_SUB(xvsubdp, sub, 2, float64, VsrD(i), 0, 0)
1643fcf5ef2aSThomas Huth VSX_ADD_SUB(xvsubsp, sub, 4, float32, VsrW(i), 0, 0)
1644fcf5ef2aSThomas Huth 
164523d0766bSMark Cave-Ayland void helper_xsaddqp(CPUPPCState *env, uint32_t opcode,
164623d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
164707bdd247SBharata B Rao {
1648cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
164907bdd247SBharata B Rao     float_status tstat;
165007bdd247SBharata B Rao 
165107bdd247SBharata B Rao     helper_reset_fpstatus(env);
165207bdd247SBharata B Rao 
1653a8d411abSBharata B Rao     tstat = env->fp_status;
165407bdd247SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1655a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
165607bdd247SBharata B Rao     }
165707bdd247SBharata B Rao 
165807bdd247SBharata B Rao     set_float_exception_flags(0, &tstat);
1659cf3b0334SMark Cave-Ayland     t.f128 = float128_add(xa->f128, xb->f128, &tstat);
166007bdd247SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
166107bdd247SBharata B Rao 
166207bdd247SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
166357483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
1664cf3b0334SMark Cave-Ayland                                 float128_classify(xa->f128) |
1665cf3b0334SMark Cave-Ayland                                 float128_classify(xb->f128));
166607bdd247SBharata B Rao     }
166707bdd247SBharata B Rao 
1668cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
166907bdd247SBharata B Rao 
1670cf3b0334SMark Cave-Ayland     *xt = t;
16716525aadcSRichard Henderson     do_float_check_status(env, GETPC());
167207bdd247SBharata B Rao }
167307bdd247SBharata B Rao 
1674fa9ebf8cSDavid Gibson /*
1675fa9ebf8cSDavid Gibson  * VSX_MUL - VSX floating point multiply
1676fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1677fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1678fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1679fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1680fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1681fcf5ef2aSThomas Huth  */
1682fcf5ef2aSThomas Huth #define VSX_MUL(op, nels, tp, fld, sfprf, r2sp)                              \
168399125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                            \
168499125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                               \
1685fcf5ef2aSThomas Huth {                                                                            \
1686cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
1687fcf5ef2aSThomas Huth     int i;                                                                   \
1688fcf5ef2aSThomas Huth                                                                              \
1689fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1690fcf5ef2aSThomas Huth                                                                              \
1691fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1692fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1693fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1694cf3b0334SMark Cave-Ayland         t.fld = tp##_mul(xa->fld, xb->fld, &tstat);                          \
1695fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1696fcf5ef2aSThomas Huth                                                                              \
1697fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
16984f0da706SRichard Henderson             float_invalid_op_mul(env, sfprf, GETPC(),                        \
1699cf3b0334SMark Cave-Ayland                                  tp##_classify(xa->fld) |                    \
1700cf3b0334SMark Cave-Ayland                                  tp##_classify(xb->fld));                    \
1701fcf5ef2aSThomas Huth         }                                                                    \
1702fcf5ef2aSThomas Huth                                                                              \
1703fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
1704cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
1705fcf5ef2aSThomas Huth         }                                                                    \
1706fcf5ef2aSThomas Huth                                                                              \
1707fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
1708cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1709fcf5ef2aSThomas Huth         }                                                                    \
1710fcf5ef2aSThomas Huth     }                                                                        \
1711fcf5ef2aSThomas Huth                                                                              \
1712cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
17136525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
1714fcf5ef2aSThomas Huth }
1715fcf5ef2aSThomas Huth 
1716fcf5ef2aSThomas Huth VSX_MUL(xsmuldp, 1, float64, VsrD(0), 1, 0)
1717fcf5ef2aSThomas Huth VSX_MUL(xsmulsp, 1, float64, VsrD(0), 1, 1)
1718fcf5ef2aSThomas Huth VSX_MUL(xvmuldp, 2, float64, VsrD(i), 0, 0)
1719fcf5ef2aSThomas Huth VSX_MUL(xvmulsp, 4, float32, VsrW(i), 0, 0)
1720fcf5ef2aSThomas Huth 
172123d0766bSMark Cave-Ayland void helper_xsmulqp(CPUPPCState *env, uint32_t opcode,
172223d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
1723a811ec04SBharata B Rao {
1724cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
1725a8d411abSBharata B Rao     float_status tstat;
1726a811ec04SBharata B Rao 
1727a8d411abSBharata B Rao     helper_reset_fpstatus(env);
1728a8d411abSBharata B Rao     tstat = env->fp_status;
1729a811ec04SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1730a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
1731a811ec04SBharata B Rao     }
1732a811ec04SBharata B Rao 
1733a811ec04SBharata B Rao     set_float_exception_flags(0, &tstat);
1734cf3b0334SMark Cave-Ayland     t.f128 = float128_mul(xa->f128, xb->f128, &tstat);
1735a811ec04SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1736a811ec04SBharata B Rao 
1737a811ec04SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
17384f0da706SRichard Henderson         float_invalid_op_mul(env, 1, GETPC(),
1739cf3b0334SMark Cave-Ayland                              float128_classify(xa->f128) |
1740cf3b0334SMark Cave-Ayland                              float128_classify(xb->f128));
1741a811ec04SBharata B Rao     }
1742cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
1743a811ec04SBharata B Rao 
1744cf3b0334SMark Cave-Ayland     *xt = t;
17456525aadcSRichard Henderson     do_float_check_status(env, GETPC());
1746a811ec04SBharata B Rao }
1747a811ec04SBharata B Rao 
1748fa9ebf8cSDavid Gibson /*
1749fa9ebf8cSDavid Gibson  * VSX_DIV - VSX floating point divide
1750fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1751fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1752fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1753fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1754fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1755fcf5ef2aSThomas Huth  */
1756fcf5ef2aSThomas Huth #define VSX_DIV(op, nels, tp, fld, sfprf, r2sp)                               \
175799125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
175899125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                                \
1759fcf5ef2aSThomas Huth {                                                                             \
1760cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
1761fcf5ef2aSThomas Huth     int i;                                                                    \
1762fcf5ef2aSThomas Huth                                                                               \
1763fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
1764fcf5ef2aSThomas Huth                                                                               \
1765fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
1766fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                  \
1767fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                 \
1768cf3b0334SMark Cave-Ayland         t.fld = tp##_div(xa->fld, xb->fld, &tstat);                           \
1769fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags;  \
1770fcf5ef2aSThomas Huth                                                                               \
1771fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {     \
1772fec59ef3SRichard Henderson             float_invalid_op_div(env, sfprf, GETPC(),                         \
1773cf3b0334SMark Cave-Ayland                                  tp##_classify(xa->fld) |                     \
1774cf3b0334SMark Cave-Ayland                                  tp##_classify(xb->fld));                     \
1775fcf5ef2aSThomas Huth         }                                                                     \
1776ae13018dSRichard Henderson         if (unlikely(tstat.float_exception_flags & float_flag_divbyzero)) {   \
1777ae13018dSRichard Henderson             float_zero_divide_excp(env, GETPC());                             \
1778ae13018dSRichard Henderson         }                                                                     \
1779fcf5ef2aSThomas Huth                                                                               \
1780fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
1781cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                  \
1782fcf5ef2aSThomas Huth         }                                                                     \
1783fcf5ef2aSThomas Huth                                                                               \
1784fcf5ef2aSThomas Huth         if (sfprf) {                                                          \
1785cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
1786fcf5ef2aSThomas Huth         }                                                                     \
1787fcf5ef2aSThomas Huth     }                                                                         \
1788fcf5ef2aSThomas Huth                                                                               \
1789cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
17906525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
1791fcf5ef2aSThomas Huth }
1792fcf5ef2aSThomas Huth 
1793fcf5ef2aSThomas Huth VSX_DIV(xsdivdp, 1, float64, VsrD(0), 1, 0)
1794fcf5ef2aSThomas Huth VSX_DIV(xsdivsp, 1, float64, VsrD(0), 1, 1)
1795fcf5ef2aSThomas Huth VSX_DIV(xvdivdp, 2, float64, VsrD(i), 0, 0)
1796fcf5ef2aSThomas Huth VSX_DIV(xvdivsp, 4, float32, VsrW(i), 0, 0)
1797fcf5ef2aSThomas Huth 
179823d0766bSMark Cave-Ayland void helper_xsdivqp(CPUPPCState *env, uint32_t opcode,
179923d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
1800314c1163SBharata B Rao {
1801cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
1802a8d411abSBharata B Rao     float_status tstat;
1803314c1163SBharata B Rao 
1804a8d411abSBharata B Rao     helper_reset_fpstatus(env);
1805a8d411abSBharata B Rao     tstat = env->fp_status;
1806314c1163SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1807a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
1808314c1163SBharata B Rao     }
1809314c1163SBharata B Rao 
1810314c1163SBharata B Rao     set_float_exception_flags(0, &tstat);
1811cf3b0334SMark Cave-Ayland     t.f128 = float128_div(xa->f128, xb->f128, &tstat);
1812314c1163SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1813314c1163SBharata B Rao 
1814314c1163SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
1815fec59ef3SRichard Henderson         float_invalid_op_div(env, 1, GETPC(),
1816cf3b0334SMark Cave-Ayland                              float128_classify(xa->f128) |
1817cf3b0334SMark Cave-Ayland                              float128_classify(xb->f128));
1818314c1163SBharata B Rao     }
1819ae13018dSRichard Henderson     if (unlikely(tstat.float_exception_flags & float_flag_divbyzero)) {
1820ae13018dSRichard Henderson         float_zero_divide_excp(env, GETPC());
1821ae13018dSRichard Henderson     }
1822314c1163SBharata B Rao 
1823cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
1824cf3b0334SMark Cave-Ayland     *xt = t;
18256525aadcSRichard Henderson     do_float_check_status(env, GETPC());
1826314c1163SBharata B Rao }
1827314c1163SBharata B Rao 
1828fa9ebf8cSDavid Gibson /*
1829fa9ebf8cSDavid Gibson  * VSX_RE  - VSX floating point reciprocal estimate
1830fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1831fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1832fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1833fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1834fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1835fcf5ef2aSThomas Huth  */
1836fcf5ef2aSThomas Huth #define VSX_RE(op, nels, tp, fld, sfprf, r2sp)                                \
183775cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)              \
1838fcf5ef2aSThomas Huth {                                                                             \
1839cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
1840fcf5ef2aSThomas Huth     int i;                                                                    \
1841fcf5ef2aSThomas Huth                                                                               \
1842fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
1843fcf5ef2aSThomas Huth                                                                               \
1844fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
1845cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xb->fld, &env->fp_status))) {      \
184613c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
1847fcf5ef2aSThomas Huth         }                                                                     \
1848cf3b0334SMark Cave-Ayland         t.fld = tp##_div(tp##_one, xb->fld, &env->fp_status);                 \
1849fcf5ef2aSThomas Huth                                                                               \
1850fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
1851cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                  \
1852fcf5ef2aSThomas Huth         }                                                                     \
1853fcf5ef2aSThomas Huth                                                                               \
1854fcf5ef2aSThomas Huth         if (sfprf) {                                                          \
1855cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
1856fcf5ef2aSThomas Huth         }                                                                     \
1857fcf5ef2aSThomas Huth     }                                                                         \
1858fcf5ef2aSThomas Huth                                                                               \
1859cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
18606525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
1861fcf5ef2aSThomas Huth }
1862fcf5ef2aSThomas Huth 
1863fcf5ef2aSThomas Huth VSX_RE(xsredp, 1, float64, VsrD(0), 1, 0)
1864fcf5ef2aSThomas Huth VSX_RE(xsresp, 1, float64, VsrD(0), 1, 1)
1865fcf5ef2aSThomas Huth VSX_RE(xvredp, 2, float64, VsrD(i), 0, 0)
1866fcf5ef2aSThomas Huth VSX_RE(xvresp, 4, float32, VsrW(i), 0, 0)
1867fcf5ef2aSThomas Huth 
1868fa9ebf8cSDavid Gibson /*
1869fa9ebf8cSDavid Gibson  * VSX_SQRT - VSX floating point square root
1870fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1871fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1872fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1873fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1874fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1875fcf5ef2aSThomas Huth  */
1876fcf5ef2aSThomas Huth #define VSX_SQRT(op, nels, tp, fld, sfprf, r2sp)                             \
187775cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
1878fcf5ef2aSThomas Huth {                                                                            \
1879cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
1880fcf5ef2aSThomas Huth     int i;                                                                   \
1881fcf5ef2aSThomas Huth                                                                              \
1882fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1883fcf5ef2aSThomas Huth                                                                              \
1884fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1885fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1886fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1887cf3b0334SMark Cave-Ayland         t.fld = tp##_sqrt(xb->fld, &tstat);                                  \
1888fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1889fcf5ef2aSThomas Huth                                                                              \
1890fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
1891cf3b0334SMark Cave-Ayland             if (tp##_is_neg(xb->fld) && !tp##_is_zero(xb->fld)) {            \
189213c9115fSRichard Henderson                 float_invalid_op_vxsqrt(env, sfprf, GETPC());                \
1893cf3b0334SMark Cave-Ayland             } else if (tp##_is_signaling_nan(xb->fld, &tstat)) {             \
189413c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                       \
1895fcf5ef2aSThomas Huth             }                                                                \
1896fcf5ef2aSThomas Huth         }                                                                    \
1897fcf5ef2aSThomas Huth                                                                              \
1898fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
1899cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
1900fcf5ef2aSThomas Huth         }                                                                    \
1901fcf5ef2aSThomas Huth                                                                              \
1902fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
1903cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1904fcf5ef2aSThomas Huth         }                                                                    \
1905fcf5ef2aSThomas Huth     }                                                                        \
1906fcf5ef2aSThomas Huth                                                                              \
1907cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
19086525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
1909fcf5ef2aSThomas Huth }
1910fcf5ef2aSThomas Huth 
1911fcf5ef2aSThomas Huth VSX_SQRT(xssqrtdp, 1, float64, VsrD(0), 1, 0)
1912fcf5ef2aSThomas Huth VSX_SQRT(xssqrtsp, 1, float64, VsrD(0), 1, 1)
1913fcf5ef2aSThomas Huth VSX_SQRT(xvsqrtdp, 2, float64, VsrD(i), 0, 0)
1914fcf5ef2aSThomas Huth VSX_SQRT(xvsqrtsp, 4, float32, VsrW(i), 0, 0)
1915fcf5ef2aSThomas Huth 
1916fa9ebf8cSDavid Gibson /*
1917fa9ebf8cSDavid Gibson  *VSX_RSQRTE - VSX floating point reciprocal square root estimate
1918fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1919fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1920fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1921fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1922fcf5ef2aSThomas Huth  *   sfprf - set FPRF
1923fcf5ef2aSThomas Huth  */
1924fcf5ef2aSThomas Huth #define VSX_RSQRTE(op, nels, tp, fld, sfprf, r2sp)                           \
192575cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
1926fcf5ef2aSThomas Huth {                                                                            \
1927cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
1928fcf5ef2aSThomas Huth     int i;                                                                   \
1929fcf5ef2aSThomas Huth                                                                              \
1930fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1931fcf5ef2aSThomas Huth                                                                              \
1932fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1933fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1934fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1935cf3b0334SMark Cave-Ayland         t.fld = tp##_sqrt(xb->fld, &tstat);                                  \
1936cf3b0334SMark Cave-Ayland         t.fld = tp##_div(tp##_one, t.fld, &tstat);                           \
1937fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1938fcf5ef2aSThomas Huth                                                                              \
1939fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
1940cf3b0334SMark Cave-Ayland             if (tp##_is_neg(xb->fld) && !tp##_is_zero(xb->fld)) {            \
194113c9115fSRichard Henderson                 float_invalid_op_vxsqrt(env, sfprf, GETPC());                \
1942cf3b0334SMark Cave-Ayland             } else if (tp##_is_signaling_nan(xb->fld, &tstat)) {             \
194313c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                       \
1944fcf5ef2aSThomas Huth             }                                                                \
1945fcf5ef2aSThomas Huth         }                                                                    \
1946fcf5ef2aSThomas Huth                                                                              \
1947fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
1948cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                 \
1949fcf5ef2aSThomas Huth         }                                                                    \
1950fcf5ef2aSThomas Huth                                                                              \
1951fcf5ef2aSThomas Huth         if (sfprf) {                                                         \
1952cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1953fcf5ef2aSThomas Huth         }                                                                    \
1954fcf5ef2aSThomas Huth     }                                                                        \
1955fcf5ef2aSThomas Huth                                                                              \
1956cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
19576525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
1958fcf5ef2aSThomas Huth }
1959fcf5ef2aSThomas Huth 
1960fcf5ef2aSThomas Huth VSX_RSQRTE(xsrsqrtedp, 1, float64, VsrD(0), 1, 0)
1961fcf5ef2aSThomas Huth VSX_RSQRTE(xsrsqrtesp, 1, float64, VsrD(0), 1, 1)
1962fcf5ef2aSThomas Huth VSX_RSQRTE(xvrsqrtedp, 2, float64, VsrD(i), 0, 0)
1963fcf5ef2aSThomas Huth VSX_RSQRTE(xvrsqrtesp, 4, float32, VsrW(i), 0, 0)
1964fcf5ef2aSThomas Huth 
1965fa9ebf8cSDavid Gibson /*
1966fa9ebf8cSDavid Gibson  * VSX_TDIV - VSX floating point test for divide
1967fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1968fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1969fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1970fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1971fcf5ef2aSThomas Huth  *   emin  - minimum unbiased exponent
1972fcf5ef2aSThomas Huth  *   emax  - maximum unbiased exponent
1973fcf5ef2aSThomas Huth  *   nbits - number of fraction bits
1974fcf5ef2aSThomas Huth  */
1975fcf5ef2aSThomas Huth #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits)                  \
1976033e1fcdSMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
1977033e1fcdSMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                          \
1978fcf5ef2aSThomas Huth {                                                                       \
1979fcf5ef2aSThomas Huth     int i;                                                              \
1980fcf5ef2aSThomas Huth     int fe_flag = 0;                                                    \
1981fcf5ef2aSThomas Huth     int fg_flag = 0;                                                    \
1982fcf5ef2aSThomas Huth                                                                         \
1983fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
1984cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_infinity(xa->fld) ||                       \
1985cf3b0334SMark Cave-Ayland                      tp##_is_infinity(xb->fld) ||                       \
1986cf3b0334SMark Cave-Ayland                      tp##_is_zero(xb->fld))) {                          \
1987fcf5ef2aSThomas Huth             fe_flag = 1;                                                \
1988fcf5ef2aSThomas Huth             fg_flag = 1;                                                \
1989fcf5ef2aSThomas Huth         } else {                                                        \
1990cf3b0334SMark Cave-Ayland             int e_a = ppc_##tp##_get_unbiased_exp(xa->fld);             \
1991cf3b0334SMark Cave-Ayland             int e_b = ppc_##tp##_get_unbiased_exp(xb->fld);             \
1992fcf5ef2aSThomas Huth                                                                         \
1993cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_any_nan(xa->fld) ||                    \
1994cf3b0334SMark Cave-Ayland                          tp##_is_any_nan(xb->fld))) {                   \
1995fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
1996fcf5ef2aSThomas Huth             } else if ((e_b <= emin) || (e_b >= (emax - 2))) {          \
1997fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
1998cf3b0334SMark Cave-Ayland             } else if (!tp##_is_zero(xa->fld) &&                        \
1999fcf5ef2aSThomas Huth                        (((e_a - e_b) >= emax) ||                        \
2000fcf5ef2aSThomas Huth                         ((e_a - e_b) <= (emin + 1)) ||                  \
2001fcf5ef2aSThomas Huth                         (e_a <= (emin + nbits)))) {                     \
2002fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2003fcf5ef2aSThomas Huth             }                                                           \
2004fcf5ef2aSThomas Huth                                                                         \
2005cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_zero_or_denormal(xb->fld))) {          \
2006fa9ebf8cSDavid Gibson                 /*                                                      \
2007fa9ebf8cSDavid Gibson                  * XB is not zero because of the above check and so     \
2008fa9ebf8cSDavid Gibson                  * must be denormalized.                                \
2009fa9ebf8cSDavid Gibson                  */                                                     \
2010fcf5ef2aSThomas Huth                 fg_flag = 1;                                            \
2011fcf5ef2aSThomas Huth             }                                                           \
2012fcf5ef2aSThomas Huth         }                                                               \
2013fcf5ef2aSThomas Huth     }                                                                   \
2014fcf5ef2aSThomas Huth                                                                         \
2015fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2016fcf5ef2aSThomas Huth }
2017fcf5ef2aSThomas Huth 
2018fcf5ef2aSThomas Huth VSX_TDIV(xstdivdp, 1, float64, VsrD(0), -1022, 1023, 52)
2019fcf5ef2aSThomas Huth VSX_TDIV(xvtdivdp, 2, float64, VsrD(i), -1022, 1023, 52)
2020fcf5ef2aSThomas Huth VSX_TDIV(xvtdivsp, 4, float32, VsrW(i), -126, 127, 23)
2021fcf5ef2aSThomas Huth 
2022fa9ebf8cSDavid Gibson /*
2023fa9ebf8cSDavid Gibson  * VSX_TSQRT - VSX floating point test for square root
2024fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2025fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2026fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2027fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2028fcf5ef2aSThomas Huth  *   emin  - minimum unbiased exponent
2029fcf5ef2aSThomas Huth  *   emax  - maximum unbiased exponent
2030fcf5ef2aSThomas Huth  *   nbits - number of fraction bits
2031fcf5ef2aSThomas Huth  */
2032fcf5ef2aSThomas Huth #define VSX_TSQRT(op, nels, tp, fld, emin, nbits)                       \
20338d830485SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xb)      \
2034fcf5ef2aSThomas Huth {                                                                       \
2035fcf5ef2aSThomas Huth     int i;                                                              \
2036fcf5ef2aSThomas Huth     int fe_flag = 0;                                                    \
2037fcf5ef2aSThomas Huth     int fg_flag = 0;                                                    \
2038fcf5ef2aSThomas Huth                                                                         \
2039fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2040cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_infinity(xb->fld) ||                       \
2041cf3b0334SMark Cave-Ayland                      tp##_is_zero(xb->fld))) {                          \
2042fcf5ef2aSThomas Huth             fe_flag = 1;                                                \
2043fcf5ef2aSThomas Huth             fg_flag = 1;                                                \
2044fcf5ef2aSThomas Huth         } else {                                                        \
2045cf3b0334SMark Cave-Ayland             int e_b = ppc_##tp##_get_unbiased_exp(xb->fld);             \
2046fcf5ef2aSThomas Huth                                                                         \
2047cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_any_nan(xb->fld))) {                   \
2048fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2049cf3b0334SMark Cave-Ayland             } else if (unlikely(tp##_is_zero(xb->fld))) {               \
2050fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2051cf3b0334SMark Cave-Ayland             } else if (unlikely(tp##_is_neg(xb->fld))) {                \
2052fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2053cf3b0334SMark Cave-Ayland             } else if (!tp##_is_zero(xb->fld) &&                        \
2054fcf5ef2aSThomas Huth                        (e_b <= (emin + nbits))) {                       \
2055fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2056fcf5ef2aSThomas Huth             }                                                           \
2057fcf5ef2aSThomas Huth                                                                         \
2058cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_zero_or_denormal(xb->fld))) {          \
2059fa9ebf8cSDavid Gibson                 /*                                                      \
2060fa9ebf8cSDavid Gibson                  * XB is not zero because of the above check and        \
2061fa9ebf8cSDavid Gibson                  * therefore must be denormalized.                      \
2062fa9ebf8cSDavid Gibson                  */                                                     \
2063fcf5ef2aSThomas Huth                 fg_flag = 1;                                            \
2064fcf5ef2aSThomas Huth             }                                                           \
2065fcf5ef2aSThomas Huth         }                                                               \
2066fcf5ef2aSThomas Huth     }                                                                   \
2067fcf5ef2aSThomas Huth                                                                         \
2068fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2069fcf5ef2aSThomas Huth }
2070fcf5ef2aSThomas Huth 
2071fcf5ef2aSThomas Huth VSX_TSQRT(xstsqrtdp, 1, float64, VsrD(0), -1022, 52)
2072fcf5ef2aSThomas Huth VSX_TSQRT(xvtsqrtdp, 2, float64, VsrD(i), -1022, 52)
2073fcf5ef2aSThomas Huth VSX_TSQRT(xvtsqrtsp, 4, float32, VsrW(i), -126, 23)
2074fcf5ef2aSThomas Huth 
2075fa9ebf8cSDavid Gibson /*
2076fa9ebf8cSDavid Gibson  * VSX_MADD - VSX floating point muliply/add variations
2077fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2078fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2079fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2080fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2081fcf5ef2aSThomas Huth  *   maddflgs - flags for the float*muladd routine that control the
2082fcf5ef2aSThomas Huth  *           various forms (madd, msub, nmadd, nmsub)
2083fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2084fcf5ef2aSThomas Huth  */
2085c9f4e4d8SMark Cave-Ayland #define VSX_MADD(op, nels, tp, fld, maddflgs, sfprf, r2sp)                    \
208699125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
2087c9f4e4d8SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *b, ppc_vsr_t *c)                   \
2088fcf5ef2aSThomas Huth {                                                                             \
2089c9f4e4d8SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2090fcf5ef2aSThomas Huth     int i;                                                                    \
2091fcf5ef2aSThomas Huth                                                                               \
2092fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
2093fcf5ef2aSThomas Huth                                                                               \
2094fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2095fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                  \
2096fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                 \
2097fcf5ef2aSThomas Huth         if (r2sp && (tstat.float_rounding_mode == float_round_nearest_even)) {\
2098fa9ebf8cSDavid Gibson             /*                                                                \
2099fa9ebf8cSDavid Gibson              * Avoid double rounding errors by rounding the intermediate      \
2100fa9ebf8cSDavid Gibson              * result to odd.                                                 \
2101fa9ebf8cSDavid Gibson              */                                                               \
2102fcf5ef2aSThomas Huth             set_float_rounding_mode(float_round_to_zero, &tstat);             \
2103cf3b0334SMark Cave-Ayland             t.fld = tp##_muladd(xa->fld, b->fld, c->fld,                      \
2104fcf5ef2aSThomas Huth                                 maddflgs, &tstat);                            \
2105cf3b0334SMark Cave-Ayland             t.fld |= (get_float_exception_flags(&tstat) &                     \
2106fcf5ef2aSThomas Huth                       float_flag_inexact) != 0;                               \
2107fcf5ef2aSThomas Huth         } else {                                                              \
2108cf3b0334SMark Cave-Ayland             t.fld = tp##_muladd(xa->fld, b->fld, c->fld,                      \
2109fcf5ef2aSThomas Huth                                 maddflgs, &tstat);                            \
2110fcf5ef2aSThomas Huth         }                                                                     \
2111fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags;  \
2112fcf5ef2aSThomas Huth                                                                               \
2113fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {     \
2114cf3b0334SMark Cave-Ayland             tp##_maddsub_update_excp(env, xa->fld, b->fld,                    \
211513c9115fSRichard Henderson                                      c->fld, maddflgs, GETPC());              \
2116fcf5ef2aSThomas Huth         }                                                                     \
2117fcf5ef2aSThomas Huth                                                                               \
2118fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
2119cf3b0334SMark Cave-Ayland             t.fld = helper_frsp(env, t.fld);                                  \
2120fcf5ef2aSThomas Huth         }                                                                     \
2121fcf5ef2aSThomas Huth                                                                               \
2122fcf5ef2aSThomas Huth         if (sfprf) {                                                          \
2123cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
2124fcf5ef2aSThomas Huth         }                                                                     \
2125fcf5ef2aSThomas Huth     }                                                                         \
2126cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
21276525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2128fcf5ef2aSThomas Huth }
2129fcf5ef2aSThomas Huth 
2130c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmadddp, 1, float64, VsrD(0), MADD_FLGS, 1, 0)
2131c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmsubdp, 1, float64, VsrD(0), MSUB_FLGS, 1, 0)
2132c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmadddp, 1, float64, VsrD(0), NMADD_FLGS, 1, 0)
2133c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmsubdp, 1, float64, VsrD(0), NMSUB_FLGS, 1, 0)
2134c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmaddsp, 1, float64, VsrD(0), MADD_FLGS, 1, 1)
2135c9f4e4d8SMark Cave-Ayland VSX_MADD(xsmsubsp, 1, float64, VsrD(0), MSUB_FLGS, 1, 1)
2136c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmaddsp, 1, float64, VsrD(0), NMADD_FLGS, 1, 1)
2137c9f4e4d8SMark Cave-Ayland VSX_MADD(xsnmsubsp, 1, float64, VsrD(0), NMSUB_FLGS, 1, 1)
2138fcf5ef2aSThomas Huth 
2139c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmadddp, 2, float64, VsrD(i), MADD_FLGS, 0, 0)
2140c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmsubdp, 2, float64, VsrD(i), MSUB_FLGS, 0, 0)
2141c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmadddp, 2, float64, VsrD(i), NMADD_FLGS, 0, 0)
2142c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmsubdp, 2, float64, VsrD(i), NMSUB_FLGS, 0, 0)
2143fcf5ef2aSThomas Huth 
2144c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmaddsp, 4, float32, VsrW(i), MADD_FLGS, 0, 0)
2145c9f4e4d8SMark Cave-Ayland VSX_MADD(xvmsubsp, 4, float32, VsrW(i), MSUB_FLGS, 0, 0)
2146c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmaddsp, 4, float32, VsrW(i), NMADD_FLGS, 0, 0)
2147c9f4e4d8SMark Cave-Ayland VSX_MADD(xvnmsubsp, 4, float32, VsrW(i), NMSUB_FLGS, 0, 0)
2148fcf5ef2aSThomas Huth 
2149fa9ebf8cSDavid Gibson /*
2150fa9ebf8cSDavid Gibson  * VSX_SCALAR_CMP_DP - VSX scalar floating point compare double precision
2151fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2152fcf5ef2aSThomas Huth  *   cmp   - comparison operation
2153fcf5ef2aSThomas Huth  *   exp   - expected result of comparison
2154fcf5ef2aSThomas Huth  *   svxvc - set VXVC bit
2155fcf5ef2aSThomas Huth  */
2156fcf5ef2aSThomas Huth #define VSX_SCALAR_CMP_DP(op, cmp, exp, svxvc)                                \
215799125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
215899125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                                \
2159fcf5ef2aSThomas Huth {                                                                             \
2160cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2161fcf5ef2aSThomas Huth     bool vxsnan_flag = false, vxvc_flag = false, vex_flag = false;            \
2162fcf5ef2aSThomas Huth                                                                               \
2163cf3b0334SMark Cave-Ayland     if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||             \
2164cf3b0334SMark Cave-Ayland         float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {             \
2165fcf5ef2aSThomas Huth         vxsnan_flag = true;                                                   \
2166fcf5ef2aSThomas Huth         if (fpscr_ve == 0 && svxvc) {                                         \
2167fcf5ef2aSThomas Huth             vxvc_flag = true;                                                 \
2168fcf5ef2aSThomas Huth         }                                                                     \
2169fcf5ef2aSThomas Huth     } else if (svxvc) {                                                       \
2170cf3b0334SMark Cave-Ayland         vxvc_flag = float64_is_quiet_nan(xa->VsrD(0), &env->fp_status) ||     \
2171cf3b0334SMark Cave-Ayland             float64_is_quiet_nan(xb->VsrD(0), &env->fp_status);               \
2172fcf5ef2aSThomas Huth     }                                                                         \
2173fcf5ef2aSThomas Huth     if (vxsnan_flag) {                                                        \
217413c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
2175fcf5ef2aSThomas Huth     }                                                                         \
2176fcf5ef2aSThomas Huth     if (vxvc_flag) {                                                          \
217713c9115fSRichard Henderson         float_invalid_op_vxvc(env, 0, GETPC());                               \
2178fcf5ef2aSThomas Huth     }                                                                         \
2179fcf5ef2aSThomas Huth     vex_flag = fpscr_ve && (vxvc_flag || vxsnan_flag);                        \
2180fcf5ef2aSThomas Huth                                                                               \
2181fcf5ef2aSThomas Huth     if (!vex_flag) {                                                          \
2182cf3b0334SMark Cave-Ayland         if (float64_##cmp(xb->VsrD(0), xa->VsrD(0),                           \
2183cf3b0334SMark Cave-Ayland                           &env->fp_status) == exp) {                          \
2184cf3b0334SMark Cave-Ayland             t.VsrD(0) = -1;                                                   \
2185cf3b0334SMark Cave-Ayland             t.VsrD(1) = 0;                                                    \
2186fcf5ef2aSThomas Huth         } else {                                                              \
2187cf3b0334SMark Cave-Ayland             t.VsrD(0) = 0;                                                    \
2188cf3b0334SMark Cave-Ayland             t.VsrD(1) = 0;                                                    \
2189fcf5ef2aSThomas Huth         }                                                                     \
2190fcf5ef2aSThomas Huth     }                                                                         \
2191cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
21926525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2193fcf5ef2aSThomas Huth }
2194fcf5ef2aSThomas Huth 
2195fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpeqdp, eq, 1, 0)
2196fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpgedp, le, 1, 1)
2197fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpgtdp, lt, 1, 1)
2198fcf5ef2aSThomas Huth VSX_SCALAR_CMP_DP(xscmpnedp, eq, 0, 0)
2199fcf5ef2aSThomas Huth 
2200033e1fcdSMark Cave-Ayland void helper_xscmpexpdp(CPUPPCState *env, uint32_t opcode,
2201033e1fcdSMark Cave-Ayland                        ppc_vsr_t *xa, ppc_vsr_t *xb)
22023a20d11dSBharata B Rao {
22033a20d11dSBharata B Rao     int64_t exp_a, exp_b;
22043a20d11dSBharata B Rao     uint32_t cc;
22053a20d11dSBharata B Rao 
2206cf3b0334SMark Cave-Ayland     exp_a = extract64(xa->VsrD(0), 52, 11);
2207cf3b0334SMark Cave-Ayland     exp_b = extract64(xb->VsrD(0), 52, 11);
22083a20d11dSBharata B Rao 
2209cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)) ||
2210cf3b0334SMark Cave-Ayland                  float64_is_any_nan(xb->VsrD(0)))) {
22113a20d11dSBharata B Rao         cc = CRF_SO;
22123a20d11dSBharata B Rao     } else {
22133a20d11dSBharata B Rao         if (exp_a < exp_b) {
22143a20d11dSBharata B Rao             cc = CRF_LT;
22153a20d11dSBharata B Rao         } else if (exp_a > exp_b) {
22163a20d11dSBharata B Rao             cc = CRF_GT;
22173a20d11dSBharata B Rao         } else {
22183a20d11dSBharata B Rao             cc = CRF_EQ;
22193a20d11dSBharata B Rao         }
22203a20d11dSBharata B Rao     }
22213a20d11dSBharata B Rao 
22225c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
22235c94dd38SPaul A. Clarke     env->fpscr |= cc << FPSCR_FPCC;
22243a20d11dSBharata B Rao     env->crf[BF(opcode)] = cc;
22253a20d11dSBharata B Rao 
22266525aadcSRichard Henderson     do_float_check_status(env, GETPC());
22273a20d11dSBharata B Rao }
22283a20d11dSBharata B Rao 
22296ae4a57aSMark Cave-Ayland void helper_xscmpexpqp(CPUPPCState *env, uint32_t opcode,
22306ae4a57aSMark Cave-Ayland                        ppc_vsr_t *xa, ppc_vsr_t *xb)
22313a20d11dSBharata B Rao {
22323a20d11dSBharata B Rao     int64_t exp_a, exp_b;
22333a20d11dSBharata B Rao     uint32_t cc;
22343a20d11dSBharata B Rao 
2235cf3b0334SMark Cave-Ayland     exp_a = extract64(xa->VsrD(0), 48, 15);
2236cf3b0334SMark Cave-Ayland     exp_b = extract64(xb->VsrD(0), 48, 15);
22373a20d11dSBharata B Rao 
2238cf3b0334SMark Cave-Ayland     if (unlikely(float128_is_any_nan(xa->f128) ||
2239cf3b0334SMark Cave-Ayland                  float128_is_any_nan(xb->f128))) {
22403a20d11dSBharata B Rao         cc = CRF_SO;
22413a20d11dSBharata B Rao     } else {
22423a20d11dSBharata B Rao         if (exp_a < exp_b) {
22433a20d11dSBharata B Rao             cc = CRF_LT;
22443a20d11dSBharata B Rao         } else if (exp_a > exp_b) {
22453a20d11dSBharata B Rao             cc = CRF_GT;
22463a20d11dSBharata B Rao         } else {
22473a20d11dSBharata B Rao             cc = CRF_EQ;
22483a20d11dSBharata B Rao         }
22493a20d11dSBharata B Rao     }
22503a20d11dSBharata B Rao 
22515c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
22525c94dd38SPaul A. Clarke     env->fpscr |= cc << FPSCR_FPCC;
22533a20d11dSBharata B Rao     env->crf[BF(opcode)] = cc;
22543a20d11dSBharata B Rao 
22556525aadcSRichard Henderson     do_float_check_status(env, GETPC());
22563a20d11dSBharata B Rao }
22573a20d11dSBharata B Rao 
2258132954a8SGiuseppe Musacchio static inline void do_scalar_cmp(CPUPPCState *env, ppc_vsr_t *xa, ppc_vsr_t *xb,
2259132954a8SGiuseppe Musacchio                                  int crf_idx, bool ordered)
2260132954a8SGiuseppe Musacchio {
2261132954a8SGiuseppe Musacchio     uint32_t cc;
2262132954a8SGiuseppe Musacchio     bool vxsnan_flag = false, vxvc_flag = false;
2263132954a8SGiuseppe Musacchio 
2264132954a8SGiuseppe Musacchio     helper_reset_fpstatus(env);
2265132954a8SGiuseppe Musacchio 
2266132954a8SGiuseppe Musacchio     switch (float64_compare(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) {
2267132954a8SGiuseppe Musacchio     case float_relation_less:
2268132954a8SGiuseppe Musacchio         cc = CRF_LT;
2269132954a8SGiuseppe Musacchio         break;
2270132954a8SGiuseppe Musacchio     case float_relation_equal:
2271132954a8SGiuseppe Musacchio         cc = CRF_EQ;
2272132954a8SGiuseppe Musacchio         break;
2273132954a8SGiuseppe Musacchio     case float_relation_greater:
2274132954a8SGiuseppe Musacchio         cc = CRF_GT;
2275132954a8SGiuseppe Musacchio         break;
2276132954a8SGiuseppe Musacchio     case float_relation_unordered:
2277132954a8SGiuseppe Musacchio         cc = CRF_SO;
2278bc92c260SGiuseppe Musacchio 
2279bc92c260SGiuseppe Musacchio         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||
2280bc92c260SGiuseppe Musacchio             float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {
2281bc92c260SGiuseppe Musacchio             vxsnan_flag = true;
2282bc92c260SGiuseppe Musacchio             if (fpscr_ve == 0 && ordered) {
2283bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2284bc92c260SGiuseppe Musacchio             }
2285bc92c260SGiuseppe Musacchio         } else if (float64_is_quiet_nan(xa->VsrD(0), &env->fp_status) ||
2286bc92c260SGiuseppe Musacchio                    float64_is_quiet_nan(xb->VsrD(0), &env->fp_status)) {
2287bc92c260SGiuseppe Musacchio             if (ordered) {
2288bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2289bc92c260SGiuseppe Musacchio             }
2290bc92c260SGiuseppe Musacchio         }
2291bc92c260SGiuseppe Musacchio 
2292132954a8SGiuseppe Musacchio         break;
2293132954a8SGiuseppe Musacchio     default:
2294132954a8SGiuseppe Musacchio         g_assert_not_reached();
2295be0a4fafSBharata B Rao     }
2296be0a4fafSBharata B Rao 
2297132954a8SGiuseppe Musacchio     env->fpscr &= ~FP_FPCC;
2298132954a8SGiuseppe Musacchio     env->fpscr |= cc << FPSCR_FPCC;
2299132954a8SGiuseppe Musacchio     env->crf[crf_idx] = cc;
2300132954a8SGiuseppe Musacchio 
230191699dbfSGiuseppe Musacchio     if (vxsnan_flag) {
230291699dbfSGiuseppe Musacchio         float_invalid_op_vxsnan(env, GETPC());
230391699dbfSGiuseppe Musacchio     }
230491699dbfSGiuseppe Musacchio     if (vxvc_flag) {
230591699dbfSGiuseppe Musacchio         float_invalid_op_vxvc(env, 0, GETPC());
230691699dbfSGiuseppe Musacchio     }
230791699dbfSGiuseppe Musacchio 
2308132954a8SGiuseppe Musacchio     do_float_check_status(env, GETPC());
2309132954a8SGiuseppe Musacchio }
2310132954a8SGiuseppe Musacchio 
2311132954a8SGiuseppe Musacchio void helper_xscmpodp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2312132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2313132954a8SGiuseppe Musacchio {
2314132954a8SGiuseppe Musacchio     do_scalar_cmp(env, xa, xb, BF(opcode), true);
2315132954a8SGiuseppe Musacchio }
2316132954a8SGiuseppe Musacchio 
2317132954a8SGiuseppe Musacchio void helper_xscmpudp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2318132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2319132954a8SGiuseppe Musacchio {
2320132954a8SGiuseppe Musacchio     do_scalar_cmp(env, xa, xb, BF(opcode), false);
2321132954a8SGiuseppe Musacchio }
2322132954a8SGiuseppe Musacchio 
2323132954a8SGiuseppe Musacchio static inline void do_scalar_cmpq(CPUPPCState *env, ppc_vsr_t *xa,
2324132954a8SGiuseppe Musacchio                                   ppc_vsr_t *xb, int crf_idx, bool ordered)
2325132954a8SGiuseppe Musacchio {
2326132954a8SGiuseppe Musacchio     uint32_t cc;
2327132954a8SGiuseppe Musacchio     bool vxsnan_flag = false, vxvc_flag = false;
2328132954a8SGiuseppe Musacchio 
2329132954a8SGiuseppe Musacchio     helper_reset_fpstatus(env);
2330132954a8SGiuseppe Musacchio 
2331132954a8SGiuseppe Musacchio     switch (float128_compare(xa->f128, xb->f128, &env->fp_status)) {
2332132954a8SGiuseppe Musacchio     case float_relation_less:
2333132954a8SGiuseppe Musacchio         cc = CRF_LT;
2334132954a8SGiuseppe Musacchio         break;
2335132954a8SGiuseppe Musacchio     case float_relation_equal:
2336132954a8SGiuseppe Musacchio         cc = CRF_EQ;
2337132954a8SGiuseppe Musacchio         break;
2338132954a8SGiuseppe Musacchio     case float_relation_greater:
2339132954a8SGiuseppe Musacchio         cc = CRF_GT;
2340132954a8SGiuseppe Musacchio         break;
2341132954a8SGiuseppe Musacchio     case float_relation_unordered:
2342132954a8SGiuseppe Musacchio         cc = CRF_SO;
2343bc92c260SGiuseppe Musacchio 
2344bc92c260SGiuseppe Musacchio         if (float128_is_signaling_nan(xa->f128, &env->fp_status) ||
2345bc92c260SGiuseppe Musacchio             float128_is_signaling_nan(xb->f128, &env->fp_status)) {
2346bc92c260SGiuseppe Musacchio             vxsnan_flag = true;
2347bc92c260SGiuseppe Musacchio             if (fpscr_ve == 0 && ordered) {
2348bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2349bc92c260SGiuseppe Musacchio             }
2350bc92c260SGiuseppe Musacchio         } else if (float128_is_quiet_nan(xa->f128, &env->fp_status) ||
2351bc92c260SGiuseppe Musacchio                    float128_is_quiet_nan(xb->f128, &env->fp_status)) {
2352bc92c260SGiuseppe Musacchio             if (ordered) {
2353bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2354bc92c260SGiuseppe Musacchio             }
2355bc92c260SGiuseppe Musacchio         }
2356bc92c260SGiuseppe Musacchio 
2357132954a8SGiuseppe Musacchio         break;
2358132954a8SGiuseppe Musacchio     default:
2359132954a8SGiuseppe Musacchio         g_assert_not_reached();
2360132954a8SGiuseppe Musacchio     }
2361132954a8SGiuseppe Musacchio 
2362132954a8SGiuseppe Musacchio     env->fpscr &= ~FP_FPCC;
2363132954a8SGiuseppe Musacchio     env->fpscr |= cc << FPSCR_FPCC;
2364132954a8SGiuseppe Musacchio     env->crf[crf_idx] = cc;
2365132954a8SGiuseppe Musacchio 
236691699dbfSGiuseppe Musacchio     if (vxsnan_flag) {
236791699dbfSGiuseppe Musacchio         float_invalid_op_vxsnan(env, GETPC());
236891699dbfSGiuseppe Musacchio     }
236991699dbfSGiuseppe Musacchio     if (vxvc_flag) {
237091699dbfSGiuseppe Musacchio         float_invalid_op_vxvc(env, 0, GETPC());
237191699dbfSGiuseppe Musacchio     }
237291699dbfSGiuseppe Musacchio 
2373132954a8SGiuseppe Musacchio     do_float_check_status(env, GETPC());
2374132954a8SGiuseppe Musacchio }
2375132954a8SGiuseppe Musacchio 
2376132954a8SGiuseppe Musacchio void helper_xscmpoqp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2377132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2378132954a8SGiuseppe Musacchio {
2379132954a8SGiuseppe Musacchio     do_scalar_cmpq(env, xa, xb, BF(opcode), true);
2380132954a8SGiuseppe Musacchio }
2381132954a8SGiuseppe Musacchio 
2382132954a8SGiuseppe Musacchio void helper_xscmpuqp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2383132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2384132954a8SGiuseppe Musacchio {
2385132954a8SGiuseppe Musacchio     do_scalar_cmpq(env, xa, xb, BF(opcode), false);
2386132954a8SGiuseppe Musacchio }
2387be0a4fafSBharata B Rao 
2388fa9ebf8cSDavid Gibson /*
2389fa9ebf8cSDavid Gibson  * VSX_MAX_MIN - VSX floating point maximum/minimum
2390fcf5ef2aSThomas Huth  *   name  - instruction mnemonic
2391fcf5ef2aSThomas Huth  *   op    - operation (max or min)
2392fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2393fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2394fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2395fcf5ef2aSThomas Huth  */
2396fcf5ef2aSThomas Huth #define VSX_MAX_MIN(name, op, nels, tp, fld)                                  \
239799125c74SMark Cave-Ayland void helper_##name(CPUPPCState *env, ppc_vsr_t *xt,                           \
239899125c74SMark Cave-Ayland                    ppc_vsr_t *xa, ppc_vsr_t *xb)                              \
2399fcf5ef2aSThomas Huth {                                                                             \
2400cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2401fcf5ef2aSThomas Huth     int i;                                                                    \
2402fcf5ef2aSThomas Huth                                                                               \
2403fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2404cf3b0334SMark Cave-Ayland         t.fld = tp##_##op(xa->fld, xb->fld, &env->fp_status);                 \
2405cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xa->fld, &env->fp_status) ||       \
2406cf3b0334SMark Cave-Ayland                      tp##_is_signaling_nan(xb->fld, &env->fp_status))) {      \
240713c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
2408fcf5ef2aSThomas Huth         }                                                                     \
2409fcf5ef2aSThomas Huth     }                                                                         \
2410fcf5ef2aSThomas Huth                                                                               \
2411cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
24126525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                      \
2413fcf5ef2aSThomas Huth }
2414fcf5ef2aSThomas Huth 
2415fcf5ef2aSThomas Huth VSX_MAX_MIN(xsmaxdp, maxnum, 1, float64, VsrD(0))
2416fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmaxdp, maxnum, 2, float64, VsrD(i))
2417fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmaxsp, maxnum, 4, float32, VsrW(i))
2418fcf5ef2aSThomas Huth VSX_MAX_MIN(xsmindp, minnum, 1, float64, VsrD(0))
2419fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmindp, minnum, 2, float64, VsrD(i))
2420fcf5ef2aSThomas Huth VSX_MAX_MIN(xvminsp, minnum, 4, float32, VsrW(i))
2421fcf5ef2aSThomas Huth 
24222770deedSBharata B Rao #define VSX_MAX_MINC(name, max)                                               \
242323d0766bSMark Cave-Ayland void helper_##name(CPUPPCState *env, uint32_t opcode,                         \
242423d0766bSMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)               \
24252770deedSBharata B Rao {                                                                             \
2426cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
24272770deedSBharata B Rao     bool vxsnan_flag = false, vex_flag = false;                               \
24282770deedSBharata B Rao                                                                               \
2429cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)) ||                           \
2430cf3b0334SMark Cave-Ayland                  float64_is_any_nan(xb->VsrD(0)))) {                          \
2431cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||         \
2432cf3b0334SMark Cave-Ayland             float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {         \
24332770deedSBharata B Rao             vxsnan_flag = true;                                               \
24342770deedSBharata B Rao         }                                                                     \
2435cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
24362770deedSBharata B Rao     } else if ((max &&                                                        \
2437cf3b0334SMark Cave-Ayland                !float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) ||     \
24382770deedSBharata B Rao                (!max &&                                                       \
2439cf3b0334SMark Cave-Ayland                float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status))) {      \
2440cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
24412770deedSBharata B Rao     } else {                                                                  \
2442cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
24432770deedSBharata B Rao     }                                                                         \
24442770deedSBharata B Rao                                                                               \
24452770deedSBharata B Rao     vex_flag = fpscr_ve & vxsnan_flag;                                        \
24462770deedSBharata B Rao     if (vxsnan_flag) {                                                        \
244713c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
24482770deedSBharata B Rao     }                                                                         \
24492770deedSBharata B Rao     if (!vex_flag) {                                                          \
2450cf3b0334SMark Cave-Ayland         *xt = t;                                                              \
24512770deedSBharata B Rao     }                                                                         \
24522770deedSBharata B Rao }                                                                             \
24532770deedSBharata B Rao 
24542770deedSBharata B Rao VSX_MAX_MINC(xsmaxcdp, 1);
24552770deedSBharata B Rao VSX_MAX_MINC(xsmincdp, 0);
24562770deedSBharata B Rao 
2457d4ccd87eSBharata B Rao #define VSX_MAX_MINJ(name, max)                                               \
245823d0766bSMark Cave-Ayland void helper_##name(CPUPPCState *env, uint32_t opcode,                         \
245923d0766bSMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)               \
2460d4ccd87eSBharata B Rao {                                                                             \
2461cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2462d4ccd87eSBharata B Rao     bool vxsnan_flag = false, vex_flag = false;                               \
2463d4ccd87eSBharata B Rao                                                                               \
2464cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)))) {                          \
2465cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status)) {         \
2466d4ccd87eSBharata B Rao             vxsnan_flag = true;                                               \
2467d4ccd87eSBharata B Rao         }                                                                     \
2468cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
2469cf3b0334SMark Cave-Ayland     } else if (unlikely(float64_is_any_nan(xb->VsrD(0)))) {                   \
2470cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {         \
2471d4ccd87eSBharata B Rao             vxsnan_flag = true;                                               \
2472d4ccd87eSBharata B Rao         }                                                                     \
2473cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
2474cf3b0334SMark Cave-Ayland     } else if (float64_is_zero(xa->VsrD(0)) &&                                \
2475cf3b0334SMark Cave-Ayland                float64_is_zero(xb->VsrD(0))) {                                \
2476d4ccd87eSBharata B Rao         if (max) {                                                            \
2477cf3b0334SMark Cave-Ayland             if (!float64_is_neg(xa->VsrD(0)) ||                               \
2478cf3b0334SMark Cave-Ayland                 !float64_is_neg(xb->VsrD(0))) {                               \
2479cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0ULL;                                             \
2480d4ccd87eSBharata B Rao             } else {                                                          \
2481cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0x8000000000000000ULL;                            \
2482d4ccd87eSBharata B Rao             }                                                                 \
2483d4ccd87eSBharata B Rao         } else {                                                              \
2484cf3b0334SMark Cave-Ayland             if (float64_is_neg(xa->VsrD(0)) ||                                \
2485cf3b0334SMark Cave-Ayland                 float64_is_neg(xb->VsrD(0))) {                                \
2486cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0x8000000000000000ULL;                            \
2487d4ccd87eSBharata B Rao             } else {                                                          \
2488cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0ULL;                                             \
2489d4ccd87eSBharata B Rao             }                                                                 \
2490d4ccd87eSBharata B Rao         }                                                                     \
2491d4ccd87eSBharata B Rao     } else if ((max &&                                                        \
2492cf3b0334SMark Cave-Ayland                !float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) ||     \
2493d4ccd87eSBharata B Rao                (!max &&                                                       \
2494cf3b0334SMark Cave-Ayland                float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status))) {      \
2495cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
2496d4ccd87eSBharata B Rao     } else {                                                                  \
2497cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
2498d4ccd87eSBharata B Rao     }                                                                         \
2499d4ccd87eSBharata B Rao                                                                               \
2500d4ccd87eSBharata B Rao     vex_flag = fpscr_ve & vxsnan_flag;                                        \
2501d4ccd87eSBharata B Rao     if (vxsnan_flag) {                                                        \
250213c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
2503d4ccd87eSBharata B Rao     }                                                                         \
2504d4ccd87eSBharata B Rao     if (!vex_flag) {                                                          \
2505cf3b0334SMark Cave-Ayland         *xt = t;                                                              \
2506d4ccd87eSBharata B Rao     }                                                                         \
2507d4ccd87eSBharata B Rao }                                                                             \
2508d4ccd87eSBharata B Rao 
2509d4ccd87eSBharata B Rao VSX_MAX_MINJ(xsmaxjdp, 1);
2510d4ccd87eSBharata B Rao VSX_MAX_MINJ(xsminjdp, 0);
2511d4ccd87eSBharata B Rao 
2512fa9ebf8cSDavid Gibson /*
2513fa9ebf8cSDavid Gibson  * VSX_CMP - VSX floating point compare
2514fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2515fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2516fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2517fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2518fcf5ef2aSThomas Huth  *   cmp   - comparison operation
2519fcf5ef2aSThomas Huth  *   svxvc - set VXVC bit
2520fcf5ef2aSThomas Huth  *   exp   - expected result of comparison
2521fcf5ef2aSThomas Huth  */
2522fcf5ef2aSThomas Huth #define VSX_CMP(op, nels, tp, fld, cmp, svxvc, exp)                       \
252300084a25SMark Cave-Ayland uint32_t helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                     \
252400084a25SMark Cave-Ayland                      ppc_vsr_t *xa, ppc_vsr_t *xb)                        \
2525fcf5ef2aSThomas Huth {                                                                         \
2526cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                    \
252700084a25SMark Cave-Ayland     uint32_t crf6 = 0;                                                    \
2528fcf5ef2aSThomas Huth     int i;                                                                \
2529fcf5ef2aSThomas Huth     int all_true = 1;                                                     \
2530fcf5ef2aSThomas Huth     int all_false = 1;                                                    \
2531fcf5ef2aSThomas Huth                                                                           \
2532fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                          \
2533cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_any_nan(xa->fld) ||                          \
2534cf3b0334SMark Cave-Ayland                      tp##_is_any_nan(xb->fld))) {                         \
2535cf3b0334SMark Cave-Ayland             if (tp##_is_signaling_nan(xa->fld, &env->fp_status) ||        \
2536cf3b0334SMark Cave-Ayland                 tp##_is_signaling_nan(xb->fld, &env->fp_status)) {        \
253713c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                    \
2538fcf5ef2aSThomas Huth             }                                                             \
2539fcf5ef2aSThomas Huth             if (svxvc) {                                                  \
254013c9115fSRichard Henderson                 float_invalid_op_vxvc(env, 0, GETPC());                   \
2541fcf5ef2aSThomas Huth             }                                                             \
2542cf3b0334SMark Cave-Ayland             t.fld = 0;                                                    \
2543fcf5ef2aSThomas Huth             all_true = 0;                                                 \
2544fcf5ef2aSThomas Huth         } else {                                                          \
2545cf3b0334SMark Cave-Ayland             if (tp##_##cmp(xb->fld, xa->fld, &env->fp_status) == exp) {   \
2546cf3b0334SMark Cave-Ayland                 t.fld = -1;                                               \
2547fcf5ef2aSThomas Huth                 all_false = 0;                                            \
2548fcf5ef2aSThomas Huth             } else {                                                      \
2549cf3b0334SMark Cave-Ayland                 t.fld = 0;                                                \
2550fcf5ef2aSThomas Huth                 all_true = 0;                                             \
2551fcf5ef2aSThomas Huth             }                                                             \
2552fcf5ef2aSThomas Huth         }                                                                 \
2553fcf5ef2aSThomas Huth     }                                                                     \
2554fcf5ef2aSThomas Huth                                                                           \
2555cf3b0334SMark Cave-Ayland     *xt = t;                                                              \
255600084a25SMark Cave-Ayland     crf6 = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0);                  \
255700084a25SMark Cave-Ayland     return crf6;                                                          \
2558fcf5ef2aSThomas Huth }
2559fcf5ef2aSThomas Huth 
2560fcf5ef2aSThomas Huth VSX_CMP(xvcmpeqdp, 2, float64, VsrD(i), eq, 0, 1)
2561fcf5ef2aSThomas Huth VSX_CMP(xvcmpgedp, 2, float64, VsrD(i), le, 1, 1)
2562fcf5ef2aSThomas Huth VSX_CMP(xvcmpgtdp, 2, float64, VsrD(i), lt, 1, 1)
2563fcf5ef2aSThomas Huth VSX_CMP(xvcmpnedp, 2, float64, VsrD(i), eq, 0, 0)
2564fcf5ef2aSThomas Huth VSX_CMP(xvcmpeqsp, 4, float32, VsrW(i), eq, 0, 1)
2565fcf5ef2aSThomas Huth VSX_CMP(xvcmpgesp, 4, float32, VsrW(i), le, 1, 1)
2566fcf5ef2aSThomas Huth VSX_CMP(xvcmpgtsp, 4, float32, VsrW(i), lt, 1, 1)
2567fcf5ef2aSThomas Huth VSX_CMP(xvcmpnesp, 4, float32, VsrW(i), eq, 0, 0)
2568fcf5ef2aSThomas Huth 
2569fa9ebf8cSDavid Gibson /*
2570fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2571fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2572fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2573fcf5ef2aSThomas Huth  *   stp   - source type (float32 or float64)
2574fcf5ef2aSThomas Huth  *   ttp   - target type (float32 or float64)
2575fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2576fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field (f32 or f64)
2577fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2578fcf5ef2aSThomas Huth  */
2579fcf5ef2aSThomas Huth #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf)    \
258075cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)   \
2581fcf5ef2aSThomas Huth {                                                                  \
2582cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                             \
2583fcf5ef2aSThomas Huth     int i;                                                         \
2584fcf5ef2aSThomas Huth                                                                    \
2585fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                   \
2586cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);        \
2587cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,              \
2588fcf5ef2aSThomas Huth                                             &env->fp_status))) {   \
258913c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                 \
2590cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                   \
2591fcf5ef2aSThomas Huth         }                                                          \
2592fcf5ef2aSThomas Huth         if (sfprf) {                                               \
2593cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                \
2594fcf5ef2aSThomas Huth         }                                                          \
2595fcf5ef2aSThomas Huth     }                                                              \
2596fcf5ef2aSThomas Huth                                                                    \
2597cf3b0334SMark Cave-Ayland     *xt = t;                                                       \
25986525aadcSRichard Henderson     do_float_check_status(env, GETPC());                           \
2599fcf5ef2aSThomas Huth }
2600fcf5ef2aSThomas Huth 
2601fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xscvdpsp, 1, float64, float32, VsrD(0), VsrW(0), 1)
2602fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xscvspdp, 1, float32, float64, VsrW(0), VsrD(0), 1)
2603fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xvcvdpsp, 2, float64, float32, VsrD(i), VsrW(2 * i), 0)
2604fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xvcvspdp, 2, float32, float64, VsrW(2 * i), VsrD(i), 0)
2605fcf5ef2aSThomas Huth 
2606fa9ebf8cSDavid Gibson /*
2607fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP_VECTOR - VSX floating point/floating point conversion
2608e5487803SBharata B Rao  *   op    - instruction mnemonic
2609e5487803SBharata B Rao  *   nels  - number of elements (1, 2 or 4)
2610e5487803SBharata B Rao  *   stp   - source type (float32 or float64)
2611e5487803SBharata B Rao  *   ttp   - target type (float32 or float64)
2612e5487803SBharata B Rao  *   sfld  - source vsr_t field
2613e5487803SBharata B Rao  *   tfld  - target vsr_t field (f32 or f64)
2614e5487803SBharata B Rao  *   sfprf - set FPRF
2615e5487803SBharata B Rao  */
2616e5487803SBharata B Rao #define VSX_CVT_FP_TO_FP_VECTOR(op, nels, stp, ttp, sfld, tfld, sfprf)    \
261799229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                       \
261899229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                            \
2619e5487803SBharata B Rao {                                                                       \
2620cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
2621e5487803SBharata B Rao     int i;                                                              \
2622e5487803SBharata B Rao                                                                         \
2623e5487803SBharata B Rao     for (i = 0; i < nels; i++) {                                        \
2624cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);             \
2625cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,                   \
2626e5487803SBharata B Rao                                             &env->fp_status))) {        \
262713c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                      \
2628cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                        \
2629e5487803SBharata B Rao         }                                                               \
2630e5487803SBharata B Rao         if (sfprf) {                                                    \
2631cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                     \
2632e5487803SBharata B Rao         }                                                               \
2633e5487803SBharata B Rao     }                                                                   \
2634e5487803SBharata B Rao                                                                         \
2635cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
26366525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
2637e5487803SBharata B Rao }
2638e5487803SBharata B Rao 
2639e5487803SBharata B Rao VSX_CVT_FP_TO_FP_VECTOR(xscvdpqp, 1, float64, float128, VsrD(0), f128, 1)
2640e5487803SBharata B Rao 
2641fa9ebf8cSDavid Gibson /*
2642fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP_HP - VSX floating point/floating point conversion
2643f566c047SBharata B Rao  *                       involving one half precision value
2644f566c047SBharata B Rao  *   op    - instruction mnemonic
26458b920d8aSNikunj A Dadhania  *   nels  - number of elements (1, 2 or 4)
2646f566c047SBharata B Rao  *   stp   - source type
2647f566c047SBharata B Rao  *   ttp   - target type
2648f566c047SBharata B Rao  *   sfld  - source vsr_t field
2649f566c047SBharata B Rao  *   tfld  - target vsr_t field
26508b920d8aSNikunj A Dadhania  *   sfprf - set FPRF
2651f566c047SBharata B Rao  */
26528b920d8aSNikunj A Dadhania #define VSX_CVT_FP_TO_FP_HP(op, nels, stp, ttp, sfld, tfld, sfprf) \
265375cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)   \
2654f566c047SBharata B Rao {                                                                  \
2655cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                             \
26568b920d8aSNikunj A Dadhania     int i;                                                         \
2657f566c047SBharata B Rao                                                                    \
26588b920d8aSNikunj A Dadhania     for (i = 0; i < nels; i++) {                                   \
2659cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, 1, &env->fp_status);     \
2660cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,              \
2661f566c047SBharata B Rao                                             &env->fp_status))) {   \
266213c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                 \
2663cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                   \
2664f566c047SBharata B Rao         }                                                          \
26658b920d8aSNikunj A Dadhania         if (sfprf) {                                               \
2666cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                \
26678b920d8aSNikunj A Dadhania         }                                                          \
26688b920d8aSNikunj A Dadhania     }                                                              \
2669f566c047SBharata B Rao                                                                    \
2670cf3b0334SMark Cave-Ayland     *xt = t;                                                       \
26716525aadcSRichard Henderson     do_float_check_status(env, GETPC());                           \
2672f566c047SBharata B Rao }
2673f566c047SBharata B Rao 
26748b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xscvdphp, 1, float64, float16, VsrD(0), VsrH(3), 1)
26758b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xscvhpdp, 1, float16, float64, VsrH(3), VsrD(0), 1)
26768b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xvcvsphp, 4, float32, float16, VsrW(i), VsrH(2 * i  + 1), 0)
26778b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xvcvhpsp, 4, float16, float32, VsrH(2 * i + 1), VsrW(i), 0)
2678f566c047SBharata B Rao 
26792a084dadSBharata B Rao /*
26802a084dadSBharata B Rao  * xscvqpdp isn't using VSX_CVT_FP_TO_FP() because xscvqpdpo will be
26812a084dadSBharata B Rao  * added to this later.
26822a084dadSBharata B Rao  */
2683e0d6a362SMark Cave-Ayland void helper_xscvqpdp(CPUPPCState *env, uint32_t opcode,
2684e0d6a362SMark Cave-Ayland                      ppc_vsr_t *xt, ppc_vsr_t *xb)
26852a084dadSBharata B Rao {
2686cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
2687a8d411abSBharata B Rao     float_status tstat;
26882a084dadSBharata B Rao 
2689a8d411abSBharata B Rao     tstat = env->fp_status;
26902a084dadSBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
2691a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
26922a084dadSBharata B Rao     }
26932a084dadSBharata B Rao 
2694cf3b0334SMark Cave-Ayland     t.VsrD(0) = float128_to_float64(xb->f128, &tstat);
2695a8d411abSBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
2696cf3b0334SMark Cave-Ayland     if (unlikely(float128_is_signaling_nan(xb->f128, &tstat))) {
269713c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
2698cf3b0334SMark Cave-Ayland         t.VsrD(0) = float64_snan_to_qnan(t.VsrD(0));
26992a084dadSBharata B Rao     }
2700cf3b0334SMark Cave-Ayland     helper_compute_fprf_float64(env, t.VsrD(0));
27012a084dadSBharata B Rao 
2702cf3b0334SMark Cave-Ayland     *xt = t;
27036525aadcSRichard Henderson     do_float_check_status(env, GETPC());
27042a084dadSBharata B Rao }
27052a084dadSBharata B Rao 
2706fcf5ef2aSThomas Huth uint64_t helper_xscvdpspn(CPUPPCState *env, uint64_t xb)
2707fcf5ef2aSThomas Huth {
2708fa7d9cb9SPaul A. Clarke     uint64_t result, sign, exp, frac;
2709e6f1bfb2SPaul A. Clarke 
2710fcf5ef2aSThomas Huth     float_status tstat = env->fp_status;
2711fcf5ef2aSThomas Huth     set_float_exception_flags(0, &tstat);
2712fcf5ef2aSThomas Huth 
2713fa7d9cb9SPaul A. Clarke     sign = extract64(xb, 63,  1);
2714fa7d9cb9SPaul A. Clarke     exp  = extract64(xb, 52, 11);
2715fa7d9cb9SPaul A. Clarke     frac = extract64(xb,  0, 52) | 0x10000000000000ULL;
2716fa7d9cb9SPaul A. Clarke 
2717fa7d9cb9SPaul A. Clarke     if (unlikely(exp == 0 && extract64(frac, 0, 52) != 0)) {
2718fa7d9cb9SPaul A. Clarke         /* DP denormal operand.  */
2719fa7d9cb9SPaul A. Clarke         /* Exponent override to DP min exp.  */
2720fa7d9cb9SPaul A. Clarke         exp = 1;
2721fa7d9cb9SPaul A. Clarke         /* Implicit bit override to 0.  */
2722fa7d9cb9SPaul A. Clarke         frac = deposit64(frac, 53, 1, 0);
2723fa7d9cb9SPaul A. Clarke     }
2724fa7d9cb9SPaul A. Clarke 
2725fa7d9cb9SPaul A. Clarke     if (unlikely(exp < 897 && frac != 0)) {
2726fa7d9cb9SPaul A. Clarke         /* SP tiny operand.  */
2727fa7d9cb9SPaul A. Clarke         if (897 - exp > 63) {
2728fa7d9cb9SPaul A. Clarke             frac = 0;
2729fa7d9cb9SPaul A. Clarke         } else {
2730fa7d9cb9SPaul A. Clarke             /* Denormalize until exp = SP min exp.  */
2731fa7d9cb9SPaul A. Clarke             frac >>= (897 - exp);
2732fa7d9cb9SPaul A. Clarke         }
2733fa7d9cb9SPaul A. Clarke         /* Exponent override to SP min exp - 1.  */
2734fa7d9cb9SPaul A. Clarke         exp = 896;
2735fa7d9cb9SPaul A. Clarke     }
2736fa7d9cb9SPaul A. Clarke 
2737fa7d9cb9SPaul A. Clarke     result = sign << 31;
2738fa7d9cb9SPaul A. Clarke     result |= extract64(exp, 10, 1) << 30;
2739fa7d9cb9SPaul A. Clarke     result |= extract64(exp, 0, 7) << 23;
2740fa7d9cb9SPaul A. Clarke     result |= extract64(frac, 29, 23);
2741fa7d9cb9SPaul A. Clarke 
2742e6f1bfb2SPaul A. Clarke     /* hardware replicates result to both words of the doubleword result.  */
2743e6f1bfb2SPaul A. Clarke     return (result << 32) | result;
2744fcf5ef2aSThomas Huth }
2745fcf5ef2aSThomas Huth 
2746fcf5ef2aSThomas Huth uint64_t helper_xscvspdpn(CPUPPCState *env, uint64_t xb)
2747fcf5ef2aSThomas Huth {
2748fcf5ef2aSThomas Huth     float_status tstat = env->fp_status;
2749fcf5ef2aSThomas Huth     set_float_exception_flags(0, &tstat);
2750fcf5ef2aSThomas Huth 
2751fcf5ef2aSThomas Huth     return float32_to_float64(xb >> 32, &tstat);
2752fcf5ef2aSThomas Huth }
2753fcf5ef2aSThomas Huth 
2754fa9ebf8cSDavid Gibson /*
2755fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2756fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2757fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2758fcf5ef2aSThomas Huth  *   stp   - source type (float32 or float64)
2759fcf5ef2aSThomas Huth  *   ttp   - target type (int32, uint32, int64 or uint64)
2760fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2761fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field
2762fcf5ef2aSThomas Huth  *   rnan  - resulting NaN
2763fcf5ef2aSThomas Huth  */
2764fcf5ef2aSThomas Huth #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, rnan)              \
276575cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2766fcf5ef2aSThomas Huth {                                                                            \
2767a3dec427SRichard Henderson     int all_flags = env->fp_status.float_exception_flags, flags;             \
2768cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                       \
2769fcf5ef2aSThomas Huth     int i;                                                                   \
2770fcf5ef2aSThomas Huth                                                                              \
2771fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
2772a3dec427SRichard Henderson         env->fp_status.float_exception_flags = 0;                            \
2773cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp##_round_to_zero(xb->sfld, &env->fp_status);  \
2774a3dec427SRichard Henderson         flags = env->fp_status.float_exception_flags;                        \
2775a3dec427SRichard Henderson         if (unlikely(flags & float_flag_invalid)) {                          \
2776cf3b0334SMark Cave-Ayland             float_invalid_cvt(env, 0, GETPC(), stp##_classify(xb->sfld));    \
2777cf3b0334SMark Cave-Ayland             t.tfld = rnan;                                                   \
2778fcf5ef2aSThomas Huth         }                                                                    \
2779a3dec427SRichard Henderson         all_flags |= flags;                                                  \
2780fcf5ef2aSThomas Huth     }                                                                        \
2781fcf5ef2aSThomas Huth                                                                              \
2782cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
2783a3dec427SRichard Henderson     env->fp_status.float_exception_flags = all_flags;                        \
27846525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
2785fcf5ef2aSThomas Huth }
2786fcf5ef2aSThomas Huth 
2787fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpsxds, 1, float64, int64, VsrD(0), VsrD(0), \
2788fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
2789fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpsxws, 1, float64, int32, VsrD(0), VsrW(1), \
2790fcf5ef2aSThomas Huth                   0x80000000U)
2791fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpuxds, 1, float64, uint64, VsrD(0), VsrD(0), 0ULL)
2792fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xscvdpuxws, 1, float64, uint32, VsrD(0), VsrW(1), 0U)
2793fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpsxds, 2, float64, int64, VsrD(i), VsrD(i), \
2794fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
2795fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpsxws, 2, float64, int32, VsrD(i), VsrW(2 * i), \
2796fcf5ef2aSThomas Huth                   0x80000000U)
2797fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpuxds, 2, float64, uint64, VsrD(i), VsrD(i), 0ULL)
2798fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvdpuxws, 2, float64, uint32, VsrD(i), VsrW(2 * i), 0U)
2799fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspsxds, 2, float32, int64, VsrW(2 * i), VsrD(i), \
2800fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
2801fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspsxws, 4, float32, int32, VsrW(i), VsrW(i), 0x80000000U)
2802fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspuxds, 2, float32, uint64, VsrW(2 * i), VsrD(i), 0ULL)
2803fcf5ef2aSThomas Huth VSX_CVT_FP_TO_INT(xvcvspuxws, 4, float32, uint32, VsrW(i), VsrW(i), 0U)
2804fcf5ef2aSThomas Huth 
2805fa9ebf8cSDavid Gibson /*
2806fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_INT_VECTOR - VSX floating point to integer conversion
280705590b92SBharata B Rao  *   op    - instruction mnemonic
280805590b92SBharata B Rao  *   stp   - source type (float32 or float64)
280905590b92SBharata B Rao  *   ttp   - target type (int32, uint32, int64 or uint64)
281005590b92SBharata B Rao  *   sfld  - source vsr_t field
281105590b92SBharata B Rao  *   tfld  - target vsr_t field
281205590b92SBharata B Rao  *   rnan  - resulting NaN
281305590b92SBharata B Rao  */
281405590b92SBharata B Rao #define VSX_CVT_FP_TO_INT_VECTOR(op, stp, ttp, sfld, tfld, rnan)             \
281599229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                          \
281699229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                               \
281705590b92SBharata B Rao {                                                                            \
2818cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                                       \
281905590b92SBharata B Rao                                                                              \
2820cf3b0334SMark Cave-Ayland     t.tfld = stp##_to_##ttp##_round_to_zero(xb->sfld, &env->fp_status);      \
282105590b92SBharata B Rao     if (env->fp_status.float_exception_flags & float_flag_invalid) {         \
2822cf3b0334SMark Cave-Ayland         float_invalid_cvt(env, 0, GETPC(), stp##_classify(xb->sfld));        \
2823cf3b0334SMark Cave-Ayland         t.tfld = rnan;                                                       \
282405590b92SBharata B Rao     }                                                                        \
282505590b92SBharata B Rao                                                                              \
2826cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
28276525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                     \
282805590b92SBharata B Rao }
282905590b92SBharata B Rao 
283005590b92SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpsdz, float128, int64, f128, VsrD(0),          \
283105590b92SBharata B Rao                   0x8000000000000000ULL)
283205590b92SBharata B Rao 
283305590b92SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpswz, float128, int32, f128, VsrD(0),          \
283405590b92SBharata B Rao                   0xffffffff80000000ULL)
2835e0aee726SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpudz, float128, uint64, f128, VsrD(0), 0x0ULL)
2836e0aee726SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpuwz, float128, uint32, f128, VsrD(0), 0x0ULL)
283705590b92SBharata B Rao 
2838fa9ebf8cSDavid Gibson /*
2839fa9ebf8cSDavid Gibson  * VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
2840fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2841fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2842fcf5ef2aSThomas Huth  *   stp   - source type (int32, uint32, int64 or uint64)
2843fcf5ef2aSThomas Huth  *   ttp   - target type (float32 or float64)
2844fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2845fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field
2846fcf5ef2aSThomas Huth  *   jdef  - definition of the j index (i or 2*i)
2847fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2848fcf5ef2aSThomas Huth  */
2849fcf5ef2aSThomas Huth #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, sfprf, r2sp)  \
285075cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)        \
2851fcf5ef2aSThomas Huth {                                                                       \
2852cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
2853fcf5ef2aSThomas Huth     int i;                                                              \
2854fcf5ef2aSThomas Huth                                                                         \
2855fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2856cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);             \
2857fcf5ef2aSThomas Huth         if (r2sp) {                                                     \
2858cf3b0334SMark Cave-Ayland             t.tfld = helper_frsp(env, t.tfld);                          \
2859fcf5ef2aSThomas Huth         }                                                               \
2860fcf5ef2aSThomas Huth         if (sfprf) {                                                    \
2861cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.tfld);                   \
2862fcf5ef2aSThomas Huth         }                                                               \
2863fcf5ef2aSThomas Huth     }                                                                   \
2864fcf5ef2aSThomas Huth                                                                         \
2865cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
28666525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
2867fcf5ef2aSThomas Huth }
2868fcf5ef2aSThomas Huth 
2869fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvsxddp, 1, int64, float64, VsrD(0), VsrD(0), 1, 0)
2870fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvuxddp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 0)
2871fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvsxdsp, 1, int64, float64, VsrD(0), VsrD(0), 1, 1)
2872fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvuxdsp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 1)
2873fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxddp, 2, int64, float64, VsrD(i), VsrD(i), 0, 0)
2874fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxddp, 2, uint64, float64, VsrD(i), VsrD(i), 0, 0)
2875fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxwdp, 2, int32, float64, VsrW(2 * i), VsrD(i), 0, 0)
2876fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxwdp, 2, uint64, float64, VsrW(2 * i), VsrD(i), 0, 0)
2877fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxdsp, 2, int64, float32, VsrD(i), VsrW(2 * i), 0, 0)
2878fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxdsp, 2, uint64, float32, VsrD(i), VsrW(2 * i), 0, 0)
2879fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxwsp, 4, int32, float32, VsrW(i), VsrW(i), 0, 0)
2880fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxwsp, 4, uint32, float32, VsrW(i), VsrW(i), 0, 0)
2881fcf5ef2aSThomas Huth 
2882fa9ebf8cSDavid Gibson /*
2883fa9ebf8cSDavid Gibson  * VSX_CVT_INT_TO_FP_VECTOR - VSX integer to floating point conversion
288448ef23cbSBharata B Rao  *   op    - instruction mnemonic
288548ef23cbSBharata B Rao  *   stp   - source type (int32, uint32, int64 or uint64)
288648ef23cbSBharata B Rao  *   ttp   - target type (float32 or float64)
288748ef23cbSBharata B Rao  *   sfld  - source vsr_t field
288848ef23cbSBharata B Rao  *   tfld  - target vsr_t field
288948ef23cbSBharata B Rao  */
289048ef23cbSBharata B Rao #define VSX_CVT_INT_TO_FP_VECTOR(op, stp, ttp, sfld, tfld)              \
289199229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
289299229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                          \
289348ef23cbSBharata B Rao {                                                                       \
2894cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
289548ef23cbSBharata B Rao                                                                         \
2896cf3b0334SMark Cave-Ayland     t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);                 \
2897cf3b0334SMark Cave-Ayland     helper_compute_fprf_##ttp(env, t.tfld);                             \
289848ef23cbSBharata B Rao                                                                         \
2899cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
29006525aadcSRichard Henderson     do_float_check_status(env, GETPC());                                \
290148ef23cbSBharata B Rao }
290248ef23cbSBharata B Rao 
290348ef23cbSBharata B Rao VSX_CVT_INT_TO_FP_VECTOR(xscvsdqp, int64, float128, VsrD(0), f128)
290448ef23cbSBharata B Rao VSX_CVT_INT_TO_FP_VECTOR(xscvudqp, uint64, float128, VsrD(0), f128)
290548ef23cbSBharata B Rao 
2906fa9ebf8cSDavid Gibson /*
2907fa9ebf8cSDavid Gibson  * For "use current rounding mode", define a value that will not be
2908fa9ebf8cSDavid Gibson  * one of the existing rounding model enums.
2909fcf5ef2aSThomas Huth  */
2910fcf5ef2aSThomas Huth #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
2911fcf5ef2aSThomas Huth   float_round_up + float_round_to_zero)
2912fcf5ef2aSThomas Huth 
2913fa9ebf8cSDavid Gibson /*
2914fa9ebf8cSDavid Gibson  * VSX_ROUND - VSX floating point round
2915fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2916fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2917fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2918fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2919fcf5ef2aSThomas Huth  *   rmode - rounding mode
2920fcf5ef2aSThomas Huth  *   sfprf - set FPRF
2921fcf5ef2aSThomas Huth  */
2922fcf5ef2aSThomas Huth #define VSX_ROUND(op, nels, tp, fld, rmode, sfprf)                     \
292375cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)       \
2924fcf5ef2aSThomas Huth {                                                                      \
2925cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                 \
2926fcf5ef2aSThomas Huth     int i;                                                             \
292763d06e90SBruno Larsen (billionai)     FloatRoundMode curr_rounding_mode;                                 \
2928fcf5ef2aSThomas Huth                                                                        \
2929fcf5ef2aSThomas Huth     if (rmode != FLOAT_ROUND_CURRENT) {                                \
293063d06e90SBruno Larsen (billionai)         curr_rounding_mode = get_float_rounding_mode(&env->fp_status); \
2931fcf5ef2aSThomas Huth         set_float_rounding_mode(rmode, &env->fp_status);               \
2932fcf5ef2aSThomas Huth     }                                                                  \
2933fcf5ef2aSThomas Huth                                                                        \
2934fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                       \
2935cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xb->fld,                    \
2936fcf5ef2aSThomas Huth                                            &env->fp_status))) {        \
293713c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                     \
2938cf3b0334SMark Cave-Ayland             t.fld = tp##_snan_to_qnan(xb->fld);                        \
2939fcf5ef2aSThomas Huth         } else {                                                       \
2940cf3b0334SMark Cave-Ayland             t.fld = tp##_round_to_int(xb->fld, &env->fp_status);       \
2941fcf5ef2aSThomas Huth         }                                                              \
2942fcf5ef2aSThomas Huth         if (sfprf) {                                                   \
2943cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                   \
2944fcf5ef2aSThomas Huth         }                                                              \
2945fcf5ef2aSThomas Huth     }                                                                  \
2946fcf5ef2aSThomas Huth                                                                        \
2947fa9ebf8cSDavid Gibson     /*                                                                 \
2948fa9ebf8cSDavid Gibson      * If this is not a "use current rounding mode" instruction,       \
2949fcf5ef2aSThomas Huth      * then inhibit setting of the XX bit and restore rounding         \
2950fa9ebf8cSDavid Gibson      * mode from FPSCR                                                 \
2951fa9ebf8cSDavid Gibson      */                                                                \
2952fcf5ef2aSThomas Huth     if (rmode != FLOAT_ROUND_CURRENT) {                                \
295363d06e90SBruno Larsen (billionai)         set_float_rounding_mode(curr_rounding_mode, &env->fp_status);  \
2954fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags &= ~float_flag_inexact;   \
2955fcf5ef2aSThomas Huth     }                                                                  \
2956fcf5ef2aSThomas Huth                                                                        \
2957cf3b0334SMark Cave-Ayland     *xt = t;                                                           \
29586525aadcSRichard Henderson     do_float_check_status(env, GETPC());                               \
2959fcf5ef2aSThomas Huth }
2960fcf5ef2aSThomas Huth 
2961fcf5ef2aSThomas Huth VSX_ROUND(xsrdpi, 1, float64, VsrD(0), float_round_ties_away, 1)
2962fcf5ef2aSThomas Huth VSX_ROUND(xsrdpic, 1, float64, VsrD(0), FLOAT_ROUND_CURRENT, 1)
2963fcf5ef2aSThomas Huth VSX_ROUND(xsrdpim, 1, float64, VsrD(0), float_round_down, 1)
2964fcf5ef2aSThomas Huth VSX_ROUND(xsrdpip, 1, float64, VsrD(0), float_round_up, 1)
2965fcf5ef2aSThomas Huth VSX_ROUND(xsrdpiz, 1, float64, VsrD(0), float_round_to_zero, 1)
2966fcf5ef2aSThomas Huth 
2967fcf5ef2aSThomas Huth VSX_ROUND(xvrdpi, 2, float64, VsrD(i), float_round_ties_away, 0)
2968fcf5ef2aSThomas Huth VSX_ROUND(xvrdpic, 2, float64, VsrD(i), FLOAT_ROUND_CURRENT, 0)
2969fcf5ef2aSThomas Huth VSX_ROUND(xvrdpim, 2, float64, VsrD(i), float_round_down, 0)
2970fcf5ef2aSThomas Huth VSX_ROUND(xvrdpip, 2, float64, VsrD(i), float_round_up, 0)
2971fcf5ef2aSThomas Huth VSX_ROUND(xvrdpiz, 2, float64, VsrD(i), float_round_to_zero, 0)
2972fcf5ef2aSThomas Huth 
2973fcf5ef2aSThomas Huth VSX_ROUND(xvrspi, 4, float32, VsrW(i), float_round_ties_away, 0)
2974fcf5ef2aSThomas Huth VSX_ROUND(xvrspic, 4, float32, VsrW(i), FLOAT_ROUND_CURRENT, 0)
2975fcf5ef2aSThomas Huth VSX_ROUND(xvrspim, 4, float32, VsrW(i), float_round_down, 0)
2976fcf5ef2aSThomas Huth VSX_ROUND(xvrspip, 4, float32, VsrW(i), float_round_up, 0)
2977fcf5ef2aSThomas Huth VSX_ROUND(xvrspiz, 4, float32, VsrW(i), float_round_to_zero, 0)
2978fcf5ef2aSThomas Huth 
2979fcf5ef2aSThomas Huth uint64_t helper_xsrsp(CPUPPCState *env, uint64_t xb)
2980fcf5ef2aSThomas Huth {
2981fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);
2982fcf5ef2aSThomas Huth 
2983fcf5ef2aSThomas Huth     uint64_t xt = helper_frsp(env, xb);
2984fcf5ef2aSThomas Huth 
2985ffc67420SBharata B Rao     helper_compute_fprf_float64(env, xt);
29866525aadcSRichard Henderson     do_float_check_status(env, GETPC());
2987fcf5ef2aSThomas Huth     return xt;
2988fcf5ef2aSThomas Huth }
2989234068abSBharata B Rao 
2990234068abSBharata B Rao #define VSX_XXPERM(op, indexed)                                       \
299199125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                     \
299299125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *pcv)                       \
2993234068abSBharata B Rao {                                                                     \
2994cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                \
2995234068abSBharata B Rao     int i, idx;                                                       \
2996234068abSBharata B Rao                                                                       \
2997234068abSBharata B Rao     for (i = 0; i < 16; i++) {                                        \
2998cf3b0334SMark Cave-Ayland         idx = pcv->VsrB(i) & 0x1F;                                    \
2999234068abSBharata B Rao         if (indexed) {                                                \
3000234068abSBharata B Rao             idx = 31 - idx;                                           \
3001234068abSBharata B Rao         }                                                             \
3002cf3b0334SMark Cave-Ayland         t.VsrB(i) = (idx <= 15) ? xa->VsrB(idx)                       \
3003cf3b0334SMark Cave-Ayland                                 : xt->VsrB(idx - 16);                 \
3004234068abSBharata B Rao     }                                                                 \
3005cf3b0334SMark Cave-Ayland     *xt = t;                                                          \
3006234068abSBharata B Rao }
3007234068abSBharata B Rao 
3008234068abSBharata B Rao VSX_XXPERM(xxperm, 0)
3009234068abSBharata B Rao VSX_XXPERM(xxpermr, 1)
3010c5969d2eSNikunj A Dadhania 
301175cf84cbSMark Cave-Ayland void helper_xvxsigsp(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)
3012c5969d2eSNikunj A Dadhania {
3013cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3014c5969d2eSNikunj A Dadhania     uint32_t exp, i, fraction;
3015c5969d2eSNikunj A Dadhania 
3016c5969d2eSNikunj A Dadhania     for (i = 0; i < 4; i++) {
3017cf3b0334SMark Cave-Ayland         exp = (xb->VsrW(i) >> 23) & 0xFF;
3018cf3b0334SMark Cave-Ayland         fraction = xb->VsrW(i) & 0x7FFFFF;
3019c5969d2eSNikunj A Dadhania         if (exp != 0 && exp != 255) {
3020cf3b0334SMark Cave-Ayland             t.VsrW(i) = fraction | 0x00800000;
3021c5969d2eSNikunj A Dadhania         } else {
3022cf3b0334SMark Cave-Ayland             t.VsrW(i) = fraction;
3023c5969d2eSNikunj A Dadhania         }
3024c5969d2eSNikunj A Dadhania     }
3025cf3b0334SMark Cave-Ayland     *xt = t;
3026c5969d2eSNikunj A Dadhania }
3027403a884aSNikunj A Dadhania 
3028fa9ebf8cSDavid Gibson /*
3029fa9ebf8cSDavid Gibson  * VSX_TEST_DC - VSX floating point test data class
3030403a884aSNikunj A Dadhania  *   op    - instruction mnemonic
3031403a884aSNikunj A Dadhania  *   nels  - number of elements (1, 2 or 4)
3032403a884aSNikunj A Dadhania  *   xbn   - VSR register number
3033403a884aSNikunj A Dadhania  *   tp    - type (float32 or float64)
3034403a884aSNikunj A Dadhania  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
3035403a884aSNikunj A Dadhania  *   tfld   - target vsr_t field (VsrD(*) or VsrW(*))
3036403a884aSNikunj A Dadhania  *   fld_max - target field max
303778241762SNikunj A Dadhania  *   scrf - set result in CR and FPCC
3038403a884aSNikunj A Dadhania  */
303978241762SNikunj A Dadhania #define VSX_TEST_DC(op, nels, xbn, tp, fld, tfld, fld_max, scrf)  \
3040403a884aSNikunj A Dadhania void helper_##op(CPUPPCState *env, uint32_t opcode)         \
3041403a884aSNikunj A Dadhania {                                                           \
3042cf3b0334SMark Cave-Ayland     ppc_vsr_t *xt = &env->vsr[xT(opcode)];                  \
3043cf3b0334SMark Cave-Ayland     ppc_vsr_t *xb = &env->vsr[xbn];                         \
3044cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                      \
3045403a884aSNikunj A Dadhania     uint32_t i, sign, dcmx;                                 \
304678241762SNikunj A Dadhania     uint32_t cc, match = 0;                                 \
3047403a884aSNikunj A Dadhania                                                             \
304878241762SNikunj A Dadhania     if (!scrf) {                                            \
3049403a884aSNikunj A Dadhania         dcmx = DCMX_XV(opcode);                             \
305078241762SNikunj A Dadhania     } else {                                                \
3051cf3b0334SMark Cave-Ayland         t = *xt;                                            \
305278241762SNikunj A Dadhania         dcmx = DCMX(opcode);                                \
305378241762SNikunj A Dadhania     }                                                       \
3054403a884aSNikunj A Dadhania                                                             \
3055403a884aSNikunj A Dadhania     for (i = 0; i < nels; i++) {                            \
3056cf3b0334SMark Cave-Ayland         sign = tp##_is_neg(xb->fld);                        \
3057cf3b0334SMark Cave-Ayland         if (tp##_is_any_nan(xb->fld)) {                     \
3058403a884aSNikunj A Dadhania             match = extract32(dcmx, 6, 1);                  \
3059cf3b0334SMark Cave-Ayland         } else if (tp##_is_infinity(xb->fld)) {             \
3060403a884aSNikunj A Dadhania             match = extract32(dcmx, 4 + !sign, 1);          \
3061cf3b0334SMark Cave-Ayland         } else if (tp##_is_zero(xb->fld)) {                 \
3062403a884aSNikunj A Dadhania             match = extract32(dcmx, 2 + !sign, 1);          \
3063cf3b0334SMark Cave-Ayland         } else if (tp##_is_zero_or_denormal(xb->fld)) {     \
3064403a884aSNikunj A Dadhania             match = extract32(dcmx, 0 + !sign, 1);          \
3065403a884aSNikunj A Dadhania         }                                                   \
306678241762SNikunj A Dadhania                                                             \
306778241762SNikunj A Dadhania         if (scrf) {                                         \
306878241762SNikunj A Dadhania             cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT;  \
30695c94dd38SPaul A. Clarke             env->fpscr &= ~FP_FPCC;                         \
30705c94dd38SPaul A. Clarke             env->fpscr |= cc << FPSCR_FPCC;                 \
307178241762SNikunj A Dadhania             env->crf[BF(opcode)] = cc;                      \
307278241762SNikunj A Dadhania         } else {                                            \
3073cf3b0334SMark Cave-Ayland             t.tfld = match ? fld_max : 0;                   \
307478241762SNikunj A Dadhania         }                                                   \
3075403a884aSNikunj A Dadhania         match = 0;                                          \
3076403a884aSNikunj A Dadhania     }                                                       \
307778241762SNikunj A Dadhania     if (!scrf) {                                            \
3078cf3b0334SMark Cave-Ayland         *xt = t;                                            \
307978241762SNikunj A Dadhania     }                                                       \
3080403a884aSNikunj A Dadhania }
3081403a884aSNikunj A Dadhania 
308278241762SNikunj A Dadhania VSX_TEST_DC(xvtstdcdp, 2, xB(opcode), float64, VsrD(i), VsrD(i), UINT64_MAX, 0)
308378241762SNikunj A Dadhania VSX_TEST_DC(xvtstdcsp, 4, xB(opcode), float32, VsrW(i), VsrW(i), UINT32_MAX, 0)
308478241762SNikunj A Dadhania VSX_TEST_DC(xststdcdp, 1, xB(opcode), float64, VsrD(0), VsrD(0), 0, 1)
308578241762SNikunj A Dadhania VSX_TEST_DC(xststdcqp, 1, (rB(opcode) + 32), float128, f128, VsrD(0), 0, 1)
308678241762SNikunj A Dadhania 
30878d830485SMark Cave-Ayland void helper_xststdcsp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xb)
308878241762SNikunj A Dadhania {
308978241762SNikunj A Dadhania     uint32_t dcmx, sign, exp;
309078241762SNikunj A Dadhania     uint32_t cc, match = 0, not_sp = 0;
309178241762SNikunj A Dadhania 
309278241762SNikunj A Dadhania     dcmx = DCMX(opcode);
3093cf3b0334SMark Cave-Ayland     exp = (xb->VsrD(0) >> 52) & 0x7FF;
309478241762SNikunj A Dadhania 
3095cf3b0334SMark Cave-Ayland     sign = float64_is_neg(xb->VsrD(0));
3096cf3b0334SMark Cave-Ayland     if (float64_is_any_nan(xb->VsrD(0))) {
309778241762SNikunj A Dadhania         match = extract32(dcmx, 6, 1);
3098cf3b0334SMark Cave-Ayland     } else if (float64_is_infinity(xb->VsrD(0))) {
309978241762SNikunj A Dadhania         match = extract32(dcmx, 4 + !sign, 1);
3100cf3b0334SMark Cave-Ayland     } else if (float64_is_zero(xb->VsrD(0))) {
310178241762SNikunj A Dadhania         match = extract32(dcmx, 2 + !sign, 1);
3102cf3b0334SMark Cave-Ayland     } else if (float64_is_zero_or_denormal(xb->VsrD(0)) ||
310378241762SNikunj A Dadhania                (exp > 0 && exp < 0x381)) {
310478241762SNikunj A Dadhania         match = extract32(dcmx, 0 + !sign, 1);
310578241762SNikunj A Dadhania     }
310678241762SNikunj A Dadhania 
3107cf3b0334SMark Cave-Ayland     not_sp = !float64_eq(xb->VsrD(0),
310878241762SNikunj A Dadhania                          float32_to_float64(
3109cf3b0334SMark Cave-Ayland                              float64_to_float32(xb->VsrD(0), &env->fp_status),
311078241762SNikunj A Dadhania                              &env->fp_status), &env->fp_status);
311178241762SNikunj A Dadhania 
311278241762SNikunj A Dadhania     cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT | not_sp << CRF_SO_BIT;
31135c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
31145c94dd38SPaul A. Clarke     env->fpscr |= cc << FPSCR_FPCC;
311578241762SNikunj A Dadhania     env->crf[BF(opcode)] = cc;
311678241762SNikunj A Dadhania }
3117be07ad58SJose Ricardo Ziviani 
311899229620SMark Cave-Ayland void helper_xsrqpi(CPUPPCState *env, uint32_t opcode,
311999229620SMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xb)
3120be07ad58SJose Ricardo Ziviani {
3121cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3122be07ad58SJose Ricardo Ziviani     uint8_t r = Rrm(opcode);
3123be07ad58SJose Ricardo Ziviani     uint8_t ex = Rc(opcode);
3124be07ad58SJose Ricardo Ziviani     uint8_t rmc = RMC(opcode);
3125be07ad58SJose Ricardo Ziviani     uint8_t rmode = 0;
3126be07ad58SJose Ricardo Ziviani     float_status tstat;
3127be07ad58SJose Ricardo Ziviani 
3128be07ad58SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3129be07ad58SJose Ricardo Ziviani 
3130be07ad58SJose Ricardo Ziviani     if (r == 0 && rmc == 0) {
3131be07ad58SJose Ricardo Ziviani         rmode = float_round_ties_away;
3132be07ad58SJose Ricardo Ziviani     } else if (r == 0 && rmc == 0x3) {
3133be07ad58SJose Ricardo Ziviani         rmode = fpscr_rn;
3134be07ad58SJose Ricardo Ziviani     } else if (r == 1) {
3135be07ad58SJose Ricardo Ziviani         switch (rmc) {
3136be07ad58SJose Ricardo Ziviani         case 0:
3137be07ad58SJose Ricardo Ziviani             rmode = float_round_nearest_even;
3138be07ad58SJose Ricardo Ziviani             break;
3139be07ad58SJose Ricardo Ziviani         case 1:
3140be07ad58SJose Ricardo Ziviani             rmode = float_round_to_zero;
3141be07ad58SJose Ricardo Ziviani             break;
3142be07ad58SJose Ricardo Ziviani         case 2:
3143be07ad58SJose Ricardo Ziviani             rmode = float_round_up;
3144be07ad58SJose Ricardo Ziviani             break;
3145be07ad58SJose Ricardo Ziviani         case 3:
3146be07ad58SJose Ricardo Ziviani             rmode = float_round_down;
3147be07ad58SJose Ricardo Ziviani             break;
3148be07ad58SJose Ricardo Ziviani         default:
3149be07ad58SJose Ricardo Ziviani             abort();
3150be07ad58SJose Ricardo Ziviani         }
3151be07ad58SJose Ricardo Ziviani     }
3152be07ad58SJose Ricardo Ziviani 
3153be07ad58SJose Ricardo Ziviani     tstat = env->fp_status;
3154be07ad58SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3155be07ad58SJose Ricardo Ziviani     set_float_rounding_mode(rmode, &tstat);
3156cf3b0334SMark Cave-Ayland     t.f128 = float128_round_to_int(xb->f128, &tstat);
3157be07ad58SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3158be07ad58SJose Ricardo Ziviani 
3159be07ad58SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3160cf3b0334SMark Cave-Ayland         if (float128_is_signaling_nan(xb->f128, &tstat)) {
316113c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
3162cf3b0334SMark Cave-Ayland             t.f128 = float128_snan_to_qnan(t.f128);
3163be07ad58SJose Ricardo Ziviani         }
3164be07ad58SJose Ricardo Ziviani     }
3165be07ad58SJose Ricardo Ziviani 
3166be07ad58SJose Ricardo Ziviani     if (ex == 0 && (tstat.float_exception_flags & float_flag_inexact)) {
3167be07ad58SJose Ricardo Ziviani         env->fp_status.float_exception_flags &= ~float_flag_inexact;
3168be07ad58SJose Ricardo Ziviani     }
3169be07ad58SJose Ricardo Ziviani 
3170cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
31716525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3172cf3b0334SMark Cave-Ayland     *xt = t;
3173be07ad58SJose Ricardo Ziviani }
3174917950d7SJose Ricardo Ziviani 
317599229620SMark Cave-Ayland void helper_xsrqpxp(CPUPPCState *env, uint32_t opcode,
317699229620SMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xb)
3177917950d7SJose Ricardo Ziviani {
3178cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3179917950d7SJose Ricardo Ziviani     uint8_t r = Rrm(opcode);
3180917950d7SJose Ricardo Ziviani     uint8_t rmc = RMC(opcode);
3181917950d7SJose Ricardo Ziviani     uint8_t rmode = 0;
3182917950d7SJose Ricardo Ziviani     floatx80 round_res;
3183917950d7SJose Ricardo Ziviani     float_status tstat;
3184917950d7SJose Ricardo Ziviani 
3185917950d7SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3186917950d7SJose Ricardo Ziviani 
3187917950d7SJose Ricardo Ziviani     if (r == 0 && rmc == 0) {
3188917950d7SJose Ricardo Ziviani         rmode = float_round_ties_away;
3189917950d7SJose Ricardo Ziviani     } else if (r == 0 && rmc == 0x3) {
3190917950d7SJose Ricardo Ziviani         rmode = fpscr_rn;
3191917950d7SJose Ricardo Ziviani     } else if (r == 1) {
3192917950d7SJose Ricardo Ziviani         switch (rmc) {
3193917950d7SJose Ricardo Ziviani         case 0:
3194917950d7SJose Ricardo Ziviani             rmode = float_round_nearest_even;
3195917950d7SJose Ricardo Ziviani             break;
3196917950d7SJose Ricardo Ziviani         case 1:
3197917950d7SJose Ricardo Ziviani             rmode = float_round_to_zero;
3198917950d7SJose Ricardo Ziviani             break;
3199917950d7SJose Ricardo Ziviani         case 2:
3200917950d7SJose Ricardo Ziviani             rmode = float_round_up;
3201917950d7SJose Ricardo Ziviani             break;
3202917950d7SJose Ricardo Ziviani         case 3:
3203917950d7SJose Ricardo Ziviani             rmode = float_round_down;
3204917950d7SJose Ricardo Ziviani             break;
3205917950d7SJose Ricardo Ziviani         default:
3206917950d7SJose Ricardo Ziviani             abort();
3207917950d7SJose Ricardo Ziviani         }
3208917950d7SJose Ricardo Ziviani     }
3209917950d7SJose Ricardo Ziviani 
3210917950d7SJose Ricardo Ziviani     tstat = env->fp_status;
3211917950d7SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3212917950d7SJose Ricardo Ziviani     set_float_rounding_mode(rmode, &tstat);
3213cf3b0334SMark Cave-Ayland     round_res = float128_to_floatx80(xb->f128, &tstat);
3214cf3b0334SMark Cave-Ayland     t.f128 = floatx80_to_float128(round_res, &tstat);
3215917950d7SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3216917950d7SJose Ricardo Ziviani 
3217917950d7SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3218cf3b0334SMark Cave-Ayland         if (float128_is_signaling_nan(xb->f128, &tstat)) {
321913c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
3220cf3b0334SMark Cave-Ayland             t.f128 = float128_snan_to_qnan(t.f128);
3221917950d7SJose Ricardo Ziviani         }
3222917950d7SJose Ricardo Ziviani     }
3223917950d7SJose Ricardo Ziviani 
3224cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3225cf3b0334SMark Cave-Ayland     *xt = t;
32266525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3227917950d7SJose Ricardo Ziviani }
3228a4a68476SJose Ricardo Ziviani 
322999229620SMark Cave-Ayland void helper_xssqrtqp(CPUPPCState *env, uint32_t opcode,
323099229620SMark Cave-Ayland                      ppc_vsr_t *xt, ppc_vsr_t *xb)
3231a4a68476SJose Ricardo Ziviani {
3232cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3233a4a68476SJose Ricardo Ziviani     float_status tstat;
3234a4a68476SJose Ricardo Ziviani 
3235a4a68476SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3236a4a68476SJose Ricardo Ziviani 
3237a8d411abSBharata B Rao     tstat = env->fp_status;
3238a4a68476SJose Ricardo Ziviani     if (unlikely(Rc(opcode) != 0)) {
3239a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
3240a4a68476SJose Ricardo Ziviani     }
3241a4a68476SJose Ricardo Ziviani 
3242a4a68476SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3243cf3b0334SMark Cave-Ayland     t.f128 = float128_sqrt(xb->f128, &tstat);
3244a4a68476SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3245a4a68476SJose Ricardo Ziviani 
3246a4a68476SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3247cf3b0334SMark Cave-Ayland         if (float128_is_signaling_nan(xb->f128, &tstat)) {
324813c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
3249cf3b0334SMark Cave-Ayland             t.f128 = float128_snan_to_qnan(xb->f128);
3250cf3b0334SMark Cave-Ayland         } else if (float128_is_quiet_nan(xb->f128, &tstat)) {
3251cf3b0334SMark Cave-Ayland             t.f128 = xb->f128;
3252cf3b0334SMark Cave-Ayland         } else if (float128_is_neg(xb->f128) && !float128_is_zero(xb->f128)) {
325313c9115fSRichard Henderson             float_invalid_op_vxsqrt(env, 1, GETPC());
3254cf3b0334SMark Cave-Ayland             t.f128 = float128_default_nan(&env->fp_status);
3255a4a68476SJose Ricardo Ziviani         }
3256a4a68476SJose Ricardo Ziviani     }
3257a4a68476SJose Ricardo Ziviani 
3258cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3259cf3b0334SMark Cave-Ayland     *xt = t;
32606525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3261a4a68476SJose Ricardo Ziviani }
3262f6b99afdSJose Ricardo Ziviani 
326323d0766bSMark Cave-Ayland void helper_xssubqp(CPUPPCState *env, uint32_t opcode,
326423d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
3265f6b99afdSJose Ricardo Ziviani {
3266cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
3267f6b99afdSJose Ricardo Ziviani     float_status tstat;
3268f6b99afdSJose Ricardo Ziviani 
3269f6b99afdSJose Ricardo Ziviani     helper_reset_fpstatus(env);
3270f6b99afdSJose Ricardo Ziviani 
3271a8d411abSBharata B Rao     tstat = env->fp_status;
3272f6b99afdSJose Ricardo Ziviani     if (unlikely(Rc(opcode) != 0)) {
3273a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
3274f6b99afdSJose Ricardo Ziviani     }
3275f6b99afdSJose Ricardo Ziviani 
3276f6b99afdSJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3277cf3b0334SMark Cave-Ayland     t.f128 = float128_sub(xa->f128, xb->f128, &tstat);
3278f6b99afdSJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3279f6b99afdSJose Ricardo Ziviani 
3280f6b99afdSJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
328157483867SRichard Henderson         float_invalid_op_addsub(env, 1, GETPC(),
3282cf3b0334SMark Cave-Ayland                                 float128_classify(xa->f128) |
3283cf3b0334SMark Cave-Ayland                                 float128_classify(xb->f128));
3284f6b99afdSJose Ricardo Ziviani     }
3285f6b99afdSJose Ricardo Ziviani 
3286cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3287cf3b0334SMark Cave-Ayland     *xt = t;
32886525aadcSRichard Henderson     do_float_check_status(env, GETPC());
3289f6b99afdSJose Ricardo Ziviani }
3290