xref: /qemu/target/ppc/fpu_helper.c (revision 74177ec6)
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 
392d9cba74SLucas Mateus Castro (alqotel) static inline float32 bfp32_neg(float32 a)
402d9cba74SLucas Mateus Castro (alqotel) {
412d9cba74SLucas Mateus Castro (alqotel)     if (unlikely(float32_is_any_nan(a))) {
422d9cba74SLucas Mateus Castro (alqotel)         return a;
432d9cba74SLucas Mateus Castro (alqotel)     } else {
442d9cba74SLucas Mateus Castro (alqotel)         return float32_chs(a);
452d9cba74SLucas Mateus Castro (alqotel)     }
462d9cba74SLucas Mateus Castro (alqotel) }
472d9cba74SLucas Mateus Castro (alqotel) 
48e82c42b7SRichard Henderson static inline bool fp_exceptions_enabled(CPUPPCState *env)
49e82c42b7SRichard Henderson {
50e82c42b7SRichard Henderson #ifdef CONFIG_USER_ONLY
51e82c42b7SRichard Henderson     return true;
52e82c42b7SRichard Henderson #else
53e82c42b7SRichard Henderson     return (env->msr & ((1U << MSR_FE0) | (1U << MSR_FE1))) != 0;
54e82c42b7SRichard Henderson #endif
55e82c42b7SRichard Henderson }
56e82c42b7SRichard Henderson 
57fcf5ef2aSThomas Huth /*****************************************************************************/
58fcf5ef2aSThomas Huth /* Floating point operations helpers */
59fcf5ef2aSThomas Huth 
6086c0cab1SRichard Henderson /*
6186c0cab1SRichard Henderson  * This is the non-arithmatic conversion that happens e.g. on loads.
6286c0cab1SRichard Henderson  * In the Power ISA pseudocode, this is called DOUBLE.
6386c0cab1SRichard Henderson  */
6486c0cab1SRichard Henderson uint64_t helper_todouble(uint32_t arg)
6586c0cab1SRichard Henderson {
6686c0cab1SRichard Henderson     uint32_t abs_arg = arg & 0x7fffffff;
6786c0cab1SRichard Henderson     uint64_t ret;
6886c0cab1SRichard Henderson 
6986c0cab1SRichard Henderson     if (likely(abs_arg >= 0x00800000)) {
70a7b7b983SPaul A. Clarke         if (unlikely(extract32(arg, 23, 8) == 0xff)) {
71a7b7b983SPaul A. Clarke             /* Inf or NAN.  */
72a7b7b983SPaul A. Clarke             ret  = (uint64_t)extract32(arg, 31, 1) << 63;
73a7b7b983SPaul A. Clarke             ret |= (uint64_t)0x7ff << 52;
74a7b7b983SPaul A. Clarke             ret |= (uint64_t)extract32(arg, 0, 23) << 29;
75a7b7b983SPaul A. Clarke         } else {
76a7b7b983SPaul A. Clarke             /* Normalized operand.  */
7786c0cab1SRichard Henderson             ret  = (uint64_t)extract32(arg, 30, 2) << 62;
7886c0cab1SRichard Henderson             ret |= ((extract32(arg, 30, 1) ^ 1) * (uint64_t)7) << 59;
7986c0cab1SRichard Henderson             ret |= (uint64_t)extract32(arg, 0, 30) << 29;
80a7b7b983SPaul A. Clarke         }
8186c0cab1SRichard Henderson     } else {
8286c0cab1SRichard Henderson         /* Zero or Denormalized operand.  */
8386c0cab1SRichard Henderson         ret = (uint64_t)extract32(arg, 31, 1) << 63;
8486c0cab1SRichard Henderson         if (unlikely(abs_arg != 0)) {
85c0e6616bSPaul A. Clarke             /*
86c0e6616bSPaul A. Clarke              * Denormalized operand.
87c0e6616bSPaul A. Clarke              * Shift fraction so that the msb is in the implicit bit position.
88c0e6616bSPaul A. Clarke              * Thus, shift is in the range [1:23].
89c0e6616bSPaul A. Clarke              */
90c0e6616bSPaul A. Clarke             int shift = clz32(abs_arg) - 8;
91c0e6616bSPaul A. Clarke             /*
92c0e6616bSPaul A. Clarke              * The first 3 terms compute the float64 exponent.  We then bias
93c0e6616bSPaul A. Clarke              * this result by -1 so that we can swallow the implicit bit below.
94c0e6616bSPaul A. Clarke              */
95c0e6616bSPaul A. Clarke             int exp = -126 - shift + 1023 - 1;
96c0e6616bSPaul A. Clarke 
9786c0cab1SRichard Henderson             ret |= (uint64_t)exp << 52;
98c0e6616bSPaul A. Clarke             ret += (uint64_t)abs_arg << (52 - 23 + shift);
9986c0cab1SRichard Henderson         }
10086c0cab1SRichard Henderson     }
10186c0cab1SRichard Henderson     return ret;
102fcf5ef2aSThomas Huth }
103fcf5ef2aSThomas Huth 
10486c0cab1SRichard Henderson /*
10586c0cab1SRichard Henderson  * This is the non-arithmatic conversion that happens e.g. on stores.
10686c0cab1SRichard Henderson  * In the Power ISA pseudocode, this is called SINGLE.
10786c0cab1SRichard Henderson  */
10886c0cab1SRichard Henderson uint32_t helper_tosingle(uint64_t arg)
109fcf5ef2aSThomas Huth {
11086c0cab1SRichard Henderson     int exp = extract64(arg, 52, 11);
11186c0cab1SRichard Henderson     uint32_t ret;
112fcf5ef2aSThomas Huth 
11386c0cab1SRichard Henderson     if (likely(exp > 896)) {
11486c0cab1SRichard Henderson         /* No denormalization required (includes Inf, NaN).  */
11586c0cab1SRichard Henderson         ret  = extract64(arg, 62, 2) << 30;
11686c0cab1SRichard Henderson         ret |= extract64(arg, 29, 30);
11786c0cab1SRichard Henderson     } else {
118fa9ebf8cSDavid Gibson         /*
119fa9ebf8cSDavid Gibson          * Zero or Denormal result.  If the exponent is in bounds for
120fa9ebf8cSDavid Gibson          * a single-precision denormal result, extract the proper
121fa9ebf8cSDavid Gibson          * bits.  If the input is not zero, and the exponent is out of
122fa9ebf8cSDavid Gibson          * bounds, then the result is undefined; this underflows to
123fa9ebf8cSDavid Gibson          * zero.
12486c0cab1SRichard Henderson          */
12586c0cab1SRichard Henderson         ret = extract64(arg, 63, 1) << 31;
12686c0cab1SRichard Henderson         if (unlikely(exp >= 874)) {
12786c0cab1SRichard Henderson             /* Denormal result.  */
12886c0cab1SRichard Henderson             ret |= ((1ULL << 52) | extract64(arg, 0, 52)) >> (896 + 30 - exp);
12986c0cab1SRichard Henderson         }
13086c0cab1SRichard Henderson     }
13186c0cab1SRichard Henderson     return ret;
132fcf5ef2aSThomas Huth }
133fcf5ef2aSThomas Huth 
134fcf5ef2aSThomas Huth static inline int ppc_float32_get_unbiased_exp(float32 f)
135fcf5ef2aSThomas Huth {
136fcf5ef2aSThomas Huth     return ((f >> 23) & 0xFF) - 127;
137fcf5ef2aSThomas Huth }
138fcf5ef2aSThomas Huth 
139fcf5ef2aSThomas Huth static inline int ppc_float64_get_unbiased_exp(float64 f)
140fcf5ef2aSThomas Huth {
141fcf5ef2aSThomas Huth     return ((f >> 52) & 0x7FF) - 1023;
142fcf5ef2aSThomas Huth }
143fcf5ef2aSThomas Huth 
1440394d7a6SRichard Henderson /* Classify a floating-point number.  */
1450394d7a6SRichard Henderson enum {
1460394d7a6SRichard Henderson     is_normal   = 1,
1470394d7a6SRichard Henderson     is_zero     = 2,
1480394d7a6SRichard Henderson     is_denormal = 4,
1490394d7a6SRichard Henderson     is_inf      = 8,
1500394d7a6SRichard Henderson     is_qnan     = 16,
1510394d7a6SRichard Henderson     is_snan     = 32,
1520394d7a6SRichard Henderson     is_neg      = 64,
1530394d7a6SRichard Henderson };
1540394d7a6SRichard Henderson 
1550394d7a6SRichard Henderson #define COMPUTE_CLASS(tp)                                      \
1560394d7a6SRichard Henderson static int tp##_classify(tp arg)                               \
1570394d7a6SRichard Henderson {                                                              \
1580394d7a6SRichard Henderson     int ret = tp##_is_neg(arg) * is_neg;                       \
1590394d7a6SRichard Henderson     if (unlikely(tp##_is_any_nan(arg))) {                      \
1600394d7a6SRichard Henderson         float_status dummy = { };  /* snan_bit_is_one = 0 */   \
1610394d7a6SRichard Henderson         ret |= (tp##_is_signaling_nan(arg, &dummy)             \
1620394d7a6SRichard Henderson                 ? is_snan : is_qnan);                          \
1630394d7a6SRichard Henderson     } else if (unlikely(tp##_is_infinity(arg))) {              \
1640394d7a6SRichard Henderson         ret |= is_inf;                                         \
1650394d7a6SRichard Henderson     } else if (tp##_is_zero(arg)) {                            \
1660394d7a6SRichard Henderson         ret |= is_zero;                                        \
1670394d7a6SRichard Henderson     } else if (tp##_is_zero_or_denormal(arg)) {                \
1680394d7a6SRichard Henderson         ret |= is_denormal;                                    \
1690394d7a6SRichard Henderson     } else {                                                   \
1700394d7a6SRichard Henderson         ret |= is_normal;                                      \
1710394d7a6SRichard Henderson     }                                                          \
1720394d7a6SRichard Henderson     return ret;                                                \
1730394d7a6SRichard Henderson }
1740394d7a6SRichard Henderson 
1750394d7a6SRichard Henderson COMPUTE_CLASS(float16)
1760394d7a6SRichard Henderson COMPUTE_CLASS(float32)
1770394d7a6SRichard Henderson COMPUTE_CLASS(float64)
1780394d7a6SRichard Henderson COMPUTE_CLASS(float128)
1790394d7a6SRichard Henderson 
1800394d7a6SRichard Henderson static void set_fprf_from_class(CPUPPCState *env, int class)
1810394d7a6SRichard Henderson {
1820394d7a6SRichard Henderson     static const uint8_t fprf[6][2] = {
1830394d7a6SRichard Henderson         { 0x04, 0x08 },  /* normalized */
1840394d7a6SRichard Henderson         { 0x02, 0x12 },  /* zero */
1850394d7a6SRichard Henderson         { 0x14, 0x18 },  /* denormalized */
1860394d7a6SRichard Henderson         { 0x05, 0x09 },  /* infinity */
1870394d7a6SRichard Henderson         { 0x11, 0x11 },  /* qnan */
1880394d7a6SRichard Henderson         { 0x00, 0x00 },  /* snan -- flags are undefined */
1890394d7a6SRichard Henderson     };
1900394d7a6SRichard Henderson     bool isneg = class & is_neg;
1910394d7a6SRichard Henderson 
1925c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPRF;
1930394d7a6SRichard Henderson     env->fpscr |= fprf[ctz32(class)][isneg] << FPSCR_FPRF;
1940394d7a6SRichard Henderson }
1950394d7a6SRichard Henderson 
196ffc67420SBharata B Rao #define COMPUTE_FPRF(tp)                                \
197ffc67420SBharata B Rao void helper_compute_fprf_##tp(CPUPPCState *env, tp arg) \
198ffc67420SBharata B Rao {                                                       \
1990394d7a6SRichard Henderson     set_fprf_from_class(env, tp##_classify(arg));       \
200ffc67420SBharata B Rao }
201fcf5ef2aSThomas Huth 
202f566c047SBharata B Rao COMPUTE_FPRF(float16)
2039aeae8e1SBharata B Rao COMPUTE_FPRF(float32)
204ffc67420SBharata B Rao COMPUTE_FPRF(float64)
20507bdd247SBharata B Rao COMPUTE_FPRF(float128)
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth /* Floating-point invalid operations exception */
20813c9115fSRichard Henderson static void finish_invalid_op_excp(CPUPPCState *env, int op, uintptr_t retaddr)
209fcf5ef2aSThomas Huth {
21013c9115fSRichard Henderson     /* Update the floating-point invalid operation summary */
2115c94dd38SPaul A. Clarke     env->fpscr |= FP_VX;
21213c9115fSRichard Henderson     /* Update the floating-point exception summary */
21313c9115fSRichard Henderson     env->fpscr |= FP_FX;
214208d8033SVíctor Colombo     if (env->fpscr & FP_VE) {
21513c9115fSRichard Henderson         /* Update the floating-point enabled exception summary */
2165c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
21713c9115fSRichard Henderson         if (fp_exceptions_enabled(env)) {
21813c9115fSRichard Henderson             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
21913c9115fSRichard Henderson                                    POWERPC_EXCP_FP | op, retaddr);
22013c9115fSRichard Henderson         }
22113c9115fSRichard Henderson     }
22213c9115fSRichard Henderson }
223fcf5ef2aSThomas Huth 
22413c9115fSRichard Henderson static void finish_invalid_op_arith(CPUPPCState *env, int op,
22513c9115fSRichard Henderson                                     bool set_fpcc, uintptr_t retaddr)
22613c9115fSRichard Henderson {
2275c94dd38SPaul A. Clarke     env->fpscr &= ~(FP_FR | FP_FI);
228208d8033SVíctor Colombo     if (!(env->fpscr & FP_VE)) {
22913c9115fSRichard Henderson         if (set_fpcc) {
2305c94dd38SPaul A. Clarke             env->fpscr &= ~FP_FPCC;
2315c94dd38SPaul A. Clarke             env->fpscr |= (FP_C | FP_FU);
23213c9115fSRichard Henderson         }
23313c9115fSRichard Henderson     }
23413c9115fSRichard Henderson     finish_invalid_op_excp(env, op, retaddr);
23513c9115fSRichard Henderson }
23613c9115fSRichard Henderson 
23713c9115fSRichard Henderson /* Signalling NaN */
23813c9115fSRichard Henderson static void float_invalid_op_vxsnan(CPUPPCState *env, uintptr_t retaddr)
23913c9115fSRichard Henderson {
2405c94dd38SPaul A. Clarke     env->fpscr |= FP_VXSNAN;
24113c9115fSRichard Henderson     finish_invalid_op_excp(env, POWERPC_EXCP_FP_VXSNAN, retaddr);
24213c9115fSRichard Henderson }
24313c9115fSRichard Henderson 
244fcf5ef2aSThomas Huth /* Magnitude subtraction of infinities */
24513c9115fSRichard Henderson static void float_invalid_op_vxisi(CPUPPCState *env, bool set_fpcc,
24613c9115fSRichard Henderson                                    uintptr_t retaddr)
24713c9115fSRichard Henderson {
2485c94dd38SPaul A. Clarke     env->fpscr |= FP_VXISI;
24913c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXISI, set_fpcc, retaddr);
25013c9115fSRichard Henderson }
25113c9115fSRichard Henderson 
252fcf5ef2aSThomas Huth /* Division of infinity by infinity */
25313c9115fSRichard Henderson static void float_invalid_op_vxidi(CPUPPCState *env, bool set_fpcc,
25413c9115fSRichard Henderson                                    uintptr_t retaddr)
25513c9115fSRichard Henderson {
2565c94dd38SPaul A. Clarke     env->fpscr |= FP_VXIDI;
25713c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXIDI, set_fpcc, retaddr);
25813c9115fSRichard Henderson }
25913c9115fSRichard Henderson 
260fcf5ef2aSThomas Huth /* Division of zero by zero */
26113c9115fSRichard Henderson static void float_invalid_op_vxzdz(CPUPPCState *env, bool set_fpcc,
26213c9115fSRichard Henderson                                    uintptr_t retaddr)
26313c9115fSRichard Henderson {
2645c94dd38SPaul A. Clarke     env->fpscr |= FP_VXZDZ;
26513c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXZDZ, set_fpcc, retaddr);
26613c9115fSRichard Henderson }
26713c9115fSRichard Henderson 
268fcf5ef2aSThomas Huth /* Multiplication of zero by infinity */
26913c9115fSRichard Henderson static void float_invalid_op_vximz(CPUPPCState *env, bool set_fpcc,
27013c9115fSRichard Henderson                                    uintptr_t retaddr)
27113c9115fSRichard Henderson {
2725c94dd38SPaul A. Clarke     env->fpscr |= FP_VXIMZ;
27313c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXIMZ, set_fpcc, retaddr);
27413c9115fSRichard Henderson }
27513c9115fSRichard Henderson 
27613c9115fSRichard Henderson /* Square root of a negative number */
27713c9115fSRichard Henderson static void float_invalid_op_vxsqrt(CPUPPCState *env, bool set_fpcc,
27813c9115fSRichard Henderson                                     uintptr_t retaddr)
27913c9115fSRichard Henderson {
2805c94dd38SPaul A. Clarke     env->fpscr |= FP_VXSQRT;
28113c9115fSRichard Henderson     finish_invalid_op_arith(env, POWERPC_EXCP_FP_VXSQRT, set_fpcc, retaddr);
28213c9115fSRichard Henderson }
28313c9115fSRichard Henderson 
284fcf5ef2aSThomas Huth /* Ordered comparison of NaN */
28513c9115fSRichard Henderson static void float_invalid_op_vxvc(CPUPPCState *env, bool set_fpcc,
28613c9115fSRichard Henderson                                   uintptr_t retaddr)
28713c9115fSRichard Henderson {
2885c94dd38SPaul A. Clarke     env->fpscr |= FP_VXVC;
289fcf5ef2aSThomas Huth     if (set_fpcc) {
2905c94dd38SPaul A. Clarke         env->fpscr &= ~FP_FPCC;
2915c94dd38SPaul A. Clarke         env->fpscr |= (FP_C | FP_FU);
292fcf5ef2aSThomas Huth     }
29313c9115fSRichard Henderson     /* Update the floating-point invalid operation summary */
2945c94dd38SPaul A. Clarke     env->fpscr |= FP_VX;
29513c9115fSRichard Henderson     /* Update the floating-point exception summary */
29613c9115fSRichard Henderson     env->fpscr |= FP_FX;
297fcf5ef2aSThomas Huth     /* We must update the target FPR before raising the exception */
298208d8033SVíctor Colombo     if (env->fpscr & FP_VE) {
299db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
30013c9115fSRichard Henderson 
301fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
302fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
303fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3045c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
30592eeb004SBALATON Zoltan         /* Exception is deferred */
306fcf5ef2aSThomas Huth     }
307fcf5ef2aSThomas Huth }
30813c9115fSRichard Henderson 
309fcf5ef2aSThomas Huth /* Invalid conversion */
31013c9115fSRichard Henderson static void float_invalid_op_vxcvi(CPUPPCState *env, bool set_fpcc,
31113c9115fSRichard Henderson                                    uintptr_t retaddr)
31213c9115fSRichard Henderson {
3135c94dd38SPaul A. Clarke     env->fpscr |= FP_VXCVI;
3145c94dd38SPaul A. Clarke     env->fpscr &= ~(FP_FR | FP_FI);
315208d8033SVíctor Colombo     if (!(env->fpscr & FP_VE)) {
316fcf5ef2aSThomas Huth         if (set_fpcc) {
3175c94dd38SPaul A. Clarke             env->fpscr &= ~FP_FPCC;
3185c94dd38SPaul A. Clarke             env->fpscr |= (FP_C | FP_FU);
319fcf5ef2aSThomas Huth         }
320fcf5ef2aSThomas Huth     }
32113c9115fSRichard Henderson     finish_invalid_op_excp(env, POWERPC_EXCP_FP_VXCVI, retaddr);
322fcf5ef2aSThomas Huth }
323fcf5ef2aSThomas Huth 
324fcf5ef2aSThomas Huth static inline void float_zero_divide_excp(CPUPPCState *env, uintptr_t raddr)
325fcf5ef2aSThomas Huth {
3265c94dd38SPaul A. Clarke     env->fpscr |= FP_ZX;
3275c94dd38SPaul A. Clarke     env->fpscr &= ~(FP_FR | FP_FI);
328fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
329fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
330208d8033SVíctor Colombo     if (env->fpscr & FP_ZE) {
331fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3325c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
333e82c42b7SRichard Henderson         if (fp_exceptions_enabled(env)) {
334fcf5ef2aSThomas Huth             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
335fcf5ef2aSThomas Huth                                    POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX,
336fcf5ef2aSThomas Huth                                    raddr);
337fcf5ef2aSThomas Huth         }
338fcf5ef2aSThomas Huth     }
339fcf5ef2aSThomas Huth }
340fcf5ef2aSThomas Huth 
341c582a1dbSVíctor Colombo static inline int float_overflow_excp(CPUPPCState *env)
342fcf5ef2aSThomas Huth {
343db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
344fcf5ef2aSThomas Huth 
3455c94dd38SPaul A. Clarke     env->fpscr |= FP_OX;
346fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
347fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
348c582a1dbSVíctor Colombo 
349c582a1dbSVíctor Colombo     bool overflow_enabled = !!(env->fpscr & FP_OE);
350c582a1dbSVíctor Colombo     if (overflow_enabled) {
351fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3525c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
353fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
354fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
355fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
356fcf5ef2aSThomas Huth     }
357c582a1dbSVíctor Colombo 
358c582a1dbSVíctor Colombo     return overflow_enabled ? 0 : float_flag_inexact;
359fcf5ef2aSThomas Huth }
360fcf5ef2aSThomas Huth 
361fcf5ef2aSThomas Huth static inline void float_underflow_excp(CPUPPCState *env)
362fcf5ef2aSThomas Huth {
363db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
364fcf5ef2aSThomas Huth 
3655c94dd38SPaul A. Clarke     env->fpscr |= FP_UX;
366fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
367fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
368208d8033SVíctor Colombo     if (env->fpscr & FP_UE) {
369fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3705c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
371fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
372fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
373fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
374fcf5ef2aSThomas Huth     }
375fcf5ef2aSThomas Huth }
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth static inline void float_inexact_excp(CPUPPCState *env)
378fcf5ef2aSThomas Huth {
379db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
380fcf5ef2aSThomas Huth 
3815c94dd38SPaul A. Clarke     env->fpscr |= FP_XX;
382fcf5ef2aSThomas Huth     /* Update the floating-point exception summary */
383fcf5ef2aSThomas Huth     env->fpscr |= FP_FX;
384208d8033SVíctor Colombo     if (env->fpscr & FP_XE) {
385fcf5ef2aSThomas Huth         /* Update the floating-point enabled exception summary */
3865c94dd38SPaul A. Clarke         env->fpscr |= FP_FEX;
387fcf5ef2aSThomas Huth         /* We must update the target FPR before raising the exception */
388fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_PROGRAM;
389fcf5ef2aSThomas Huth         env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
390fcf5ef2aSThomas Huth     }
391fcf5ef2aSThomas Huth }
392fcf5ef2aSThomas Huth 
393fcf5ef2aSThomas Huth void helper_fpscr_clrbit(CPUPPCState *env, uint32_t bit)
394fcf5ef2aSThomas Huth {
395fe43ba97SBruno Larsen (billionai)     uint32_t mask = 1u << bit;
396fe43ba97SBruno Larsen (billionai)     if (env->fpscr & mask) {
397fe43ba97SBruno Larsen (billionai)         ppc_store_fpscr(env, env->fpscr & ~(target_ulong)mask);
398fcf5ef2aSThomas Huth     }
399fcf5ef2aSThomas Huth }
400fcf5ef2aSThomas Huth 
401fcf5ef2aSThomas Huth void helper_fpscr_setbit(CPUPPCState *env, uint32_t bit)
402fcf5ef2aSThomas Huth {
403fe43ba97SBruno Larsen (billionai)     uint32_t mask = 1u << bit;
404fe43ba97SBruno Larsen (billionai)     if (!(env->fpscr & mask)) {
405fe43ba97SBruno Larsen (billionai)         ppc_store_fpscr(env, env->fpscr | mask);
406fcf5ef2aSThomas Huth     }
407fcf5ef2aSThomas Huth }
408fcf5ef2aSThomas Huth 
409fe43ba97SBruno Larsen (billionai) void helper_store_fpscr(CPUPPCState *env, uint64_t val, uint32_t nibbles)
410fcf5ef2aSThomas Huth {
411fe43ba97SBruno Larsen (billionai)     target_ulong mask = 0;
412fcf5ef2aSThomas Huth     int i;
413fcf5ef2aSThomas Huth 
414fe43ba97SBruno Larsen (billionai)     /* TODO: push this extension back to translation time */
415fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong) * 2; i++) {
416fe43ba97SBruno Larsen (billionai)         if (nibbles & (1 << i)) {
417fe43ba97SBruno Larsen (billionai)             mask |= (target_ulong) 0xf << (4 * i);
418fcf5ef2aSThomas Huth         }
419fcf5ef2aSThomas Huth     }
420fe43ba97SBruno Larsen (billionai)     val = (val & mask) | (env->fpscr & ~mask);
421fe43ba97SBruno Larsen (billionai)     ppc_store_fpscr(env, val);
422fcf5ef2aSThomas Huth }
423fcf5ef2aSThomas Huth 
424c29018ccSLucas Mateus Castro (alqotel) static void do_fpscr_check_status(CPUPPCState *env, uintptr_t raddr)
425c3a824b0SLucas Mateus Castro (alqotel) {
426c3a824b0SLucas Mateus Castro (alqotel)     CPUState *cs = env_cpu(env);
427c3a824b0SLucas Mateus Castro (alqotel)     target_ulong fpscr = env->fpscr;
428c3a824b0SLucas Mateus Castro (alqotel)     int error = 0;
429c3a824b0SLucas Mateus Castro (alqotel) 
430c3a824b0SLucas Mateus Castro (alqotel)     if ((fpscr & FP_OX) && (fpscr & FP_OE)) {
431c3a824b0SLucas Mateus Castro (alqotel)         error = POWERPC_EXCP_FP_OX;
432c3a824b0SLucas Mateus Castro (alqotel)     } else if ((fpscr & FP_UX) && (fpscr & FP_UE)) {
433c3a824b0SLucas Mateus Castro (alqotel)         error = POWERPC_EXCP_FP_UX;
434c3a824b0SLucas Mateus Castro (alqotel)     } else if ((fpscr & FP_XX) && (fpscr & FP_XE)) {
435c3a824b0SLucas Mateus Castro (alqotel)         error = POWERPC_EXCP_FP_XX;
436c3a824b0SLucas Mateus Castro (alqotel)     } else if ((fpscr & FP_ZX) && (fpscr & FP_ZE)) {
437c3a824b0SLucas Mateus Castro (alqotel)         error = POWERPC_EXCP_FP_ZX;
438c3a824b0SLucas Mateus Castro (alqotel)     } else if (fpscr & FP_VE) {
439c3a824b0SLucas Mateus Castro (alqotel)         if (fpscr & FP_VXSOFT) {
440c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXSOFT;
441c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXSNAN) {
442c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXSNAN;
443c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXISI) {
444c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXISI;
445c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXIDI) {
446c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXIDI;
447c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXZDZ) {
448c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXZDZ;
449c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXIMZ) {
450c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXIMZ;
451c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXVC) {
452c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXVC;
453c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXSQRT) {
454c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXSQRT;
455c3a824b0SLucas Mateus Castro (alqotel)         } else if (fpscr & FP_VXCVI) {
456c3a824b0SLucas Mateus Castro (alqotel)             error = POWERPC_EXCP_FP_VXCVI;
457c3a824b0SLucas Mateus Castro (alqotel)         } else {
458c3a824b0SLucas Mateus Castro (alqotel)             return;
459c3a824b0SLucas Mateus Castro (alqotel)         }
460c3a824b0SLucas Mateus Castro (alqotel)     } else {
461c3a824b0SLucas Mateus Castro (alqotel)         return;
462c3a824b0SLucas Mateus Castro (alqotel)     }
463c3a824b0SLucas Mateus Castro (alqotel)     cs->exception_index = POWERPC_EXCP_PROGRAM;
464c3a824b0SLucas Mateus Castro (alqotel)     env->error_code = error | POWERPC_EXCP_FP;
4655980167eSDaniel Henrique Barboza     env->fpscr |= FP_FEX;
466c3a824b0SLucas Mateus Castro (alqotel)     /* Deferred floating-point exception after target FPSCR update */
467c3a824b0SLucas Mateus Castro (alqotel)     if (fp_exceptions_enabled(env)) {
468c3a824b0SLucas Mateus Castro (alqotel)         raise_exception_err_ra(env, cs->exception_index,
469c29018ccSLucas Mateus Castro (alqotel)                                env->error_code, raddr);
470c3a824b0SLucas Mateus Castro (alqotel)     }
471c3a824b0SLucas Mateus Castro (alqotel) }
472c3a824b0SLucas Mateus Castro (alqotel) 
473c29018ccSLucas Mateus Castro (alqotel) void helper_fpscr_check_status(CPUPPCState *env)
474c29018ccSLucas Mateus Castro (alqotel) {
475c29018ccSLucas Mateus Castro (alqotel)     do_fpscr_check_status(env, GETPC());
476c29018ccSLucas Mateus Castro (alqotel) }
477c29018ccSLucas Mateus Castro (alqotel) 
4783278677fSVíctor Colombo static void do_float_check_status(CPUPPCState *env, bool change_fi,
4793278677fSVíctor Colombo                                   uintptr_t raddr)
480fcf5ef2aSThomas Huth {
481db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
482fcf5ef2aSThomas Huth     int status = get_float_exception_flags(&env->fp_status);
483fcf5ef2aSThomas Huth 
484ae13018dSRichard Henderson     if (status & float_flag_overflow) {
485c582a1dbSVíctor Colombo         status |= float_overflow_excp(env);
486fcf5ef2aSThomas Huth     } else if (status & float_flag_underflow) {
487fcf5ef2aSThomas Huth         float_underflow_excp(env);
4889e430ca3SJohn Arbuckle     }
48916ce2fffSRichard Henderson     if (status & float_flag_inexact) {
49016ce2fffSRichard Henderson         float_inexact_excp(env);
4913278677fSVíctor Colombo     }
4923278677fSVíctor Colombo     if (change_fi) {
4933278677fSVíctor Colombo         env->fpscr = FIELD_DP64(env->fpscr, FPSCR, FI,
4943278677fSVíctor Colombo                                 !!(status & float_flag_inexact));
495fcf5ef2aSThomas Huth     }
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth     if (cs->exception_index == POWERPC_EXCP_PROGRAM &&
498fcf5ef2aSThomas Huth         (env->error_code & POWERPC_EXCP_FP)) {
49992eeb004SBALATON Zoltan         /* Deferred floating-point exception after target FPR update */
500e82c42b7SRichard Henderson         if (fp_exceptions_enabled(env)) {
501fcf5ef2aSThomas Huth             raise_exception_err_ra(env, cs->exception_index,
502fcf5ef2aSThomas Huth                                    env->error_code, raddr);
503fcf5ef2aSThomas Huth         }
504fcf5ef2aSThomas Huth     }
505fcf5ef2aSThomas Huth }
506fcf5ef2aSThomas Huth 
507fcf5ef2aSThomas Huth void helper_float_check_status(CPUPPCState *env)
508fcf5ef2aSThomas Huth {
5093278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
510fcf5ef2aSThomas Huth }
511fcf5ef2aSThomas Huth 
512fcf5ef2aSThomas Huth void helper_reset_fpstatus(CPUPPCState *env)
513fcf5ef2aSThomas Huth {
514fcf5ef2aSThomas Huth     set_float_exception_flags(0, &env->fp_status);
515fcf5ef2aSThomas Huth }
516fcf5ef2aSThomas Huth 
517941298ecSRichard Henderson static void float_invalid_op_addsub(CPUPPCState *env, int flags,
518941298ecSRichard Henderson                                     bool set_fpcc, uintptr_t retaddr)
51957483867SRichard Henderson {
520941298ecSRichard Henderson     if (flags & float_flag_invalid_isi) {
52157483867SRichard Henderson         float_invalid_op_vxisi(env, set_fpcc, retaddr);
522941298ecSRichard Henderson     } else if (flags & float_flag_invalid_snan) {
52357483867SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
52457483867SRichard Henderson     }
52557483867SRichard Henderson }
52657483867SRichard Henderson 
527fcf5ef2aSThomas Huth /* fadd - fadd. */
528ac43cec3SRichard Henderson float64 helper_fadd(CPUPPCState *env, float64 arg1, float64 arg2)
529fcf5ef2aSThomas Huth {
530ac43cec3SRichard Henderson     float64 ret = float64_add(arg1, arg2, &env->fp_status);
531941298ecSRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
532fcf5ef2aSThomas Huth 
533941298ecSRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
534941298ecSRichard Henderson         float_invalid_op_addsub(env, flags, 1, GETPC());
535fcf5ef2aSThomas Huth     }
536fcf5ef2aSThomas Huth 
537ac43cec3SRichard Henderson     return ret;
538fcf5ef2aSThomas Huth }
539fcf5ef2aSThomas Huth 
540d9e792a1SRichard Henderson /* fadds - fadds. */
541d9e792a1SRichard Henderson float64 helper_fadds(CPUPPCState *env, float64 arg1, float64 arg2)
542d9e792a1SRichard Henderson {
543d9e792a1SRichard Henderson     float64 ret = float64r32_add(arg1, arg2, &env->fp_status);
544d9e792a1SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
545d9e792a1SRichard Henderson 
546d9e792a1SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
547d9e792a1SRichard Henderson         float_invalid_op_addsub(env, flags, 1, GETPC());
548d9e792a1SRichard Henderson     }
549d9e792a1SRichard Henderson     return ret;
550d9e792a1SRichard Henderson }
551d9e792a1SRichard Henderson 
552fcf5ef2aSThomas Huth /* fsub - fsub. */
553ac43cec3SRichard Henderson float64 helper_fsub(CPUPPCState *env, float64 arg1, float64 arg2)
554fcf5ef2aSThomas Huth {
555ac43cec3SRichard Henderson     float64 ret = float64_sub(arg1, arg2, &env->fp_status);
556941298ecSRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
557fcf5ef2aSThomas Huth 
558941298ecSRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
559941298ecSRichard Henderson         float_invalid_op_addsub(env, flags, 1, GETPC());
560fcf5ef2aSThomas Huth     }
561fcf5ef2aSThomas Huth 
562ac43cec3SRichard Henderson     return ret;
563fcf5ef2aSThomas Huth }
564fcf5ef2aSThomas Huth 
565d9e792a1SRichard Henderson /* fsubs - fsubs. */
566d9e792a1SRichard Henderson float64 helper_fsubs(CPUPPCState *env, float64 arg1, float64 arg2)
567d9e792a1SRichard Henderson {
568d9e792a1SRichard Henderson     float64 ret = float64r32_sub(arg1, arg2, &env->fp_status);
569d9e792a1SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
570d9e792a1SRichard Henderson 
571d9e792a1SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
572d9e792a1SRichard Henderson         float_invalid_op_addsub(env, flags, 1, GETPC());
573d9e792a1SRichard Henderson     }
574d9e792a1SRichard Henderson     return ret;
575d9e792a1SRichard Henderson }
576d9e792a1SRichard Henderson 
5774edf5569SRichard Henderson static void float_invalid_op_mul(CPUPPCState *env, int flags,
5784edf5569SRichard Henderson                                  bool set_fprc, uintptr_t retaddr)
5794f0da706SRichard Henderson {
5804edf5569SRichard Henderson     if (flags & float_flag_invalid_imz) {
5814f0da706SRichard Henderson         float_invalid_op_vximz(env, set_fprc, retaddr);
5824edf5569SRichard Henderson     } else if (flags & float_flag_invalid_snan) {
5834f0da706SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
5844f0da706SRichard Henderson     }
5854f0da706SRichard Henderson }
5864f0da706SRichard Henderson 
587fcf5ef2aSThomas Huth /* fmul - fmul. */
58879f91633SRichard Henderson float64 helper_fmul(CPUPPCState *env, float64 arg1, float64 arg2)
589fcf5ef2aSThomas Huth {
59079f91633SRichard Henderson     float64 ret = float64_mul(arg1, arg2, &env->fp_status);
5914edf5569SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
592fcf5ef2aSThomas Huth 
5934edf5569SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
5944edf5569SRichard Henderson         float_invalid_op_mul(env, flags, 1, GETPC());
595fcf5ef2aSThomas Huth     }
596fcf5ef2aSThomas Huth 
59779f91633SRichard Henderson     return ret;
598fcf5ef2aSThomas Huth }
599fcf5ef2aSThomas Huth 
6007f87214eSRichard Henderson /* fmuls - fmuls. */
6017f87214eSRichard Henderson float64 helper_fmuls(CPUPPCState *env, float64 arg1, float64 arg2)
6027f87214eSRichard Henderson {
6037f87214eSRichard Henderson     float64 ret = float64r32_mul(arg1, arg2, &env->fp_status);
6047f87214eSRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
6057f87214eSRichard Henderson 
6067f87214eSRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
6077f87214eSRichard Henderson         float_invalid_op_mul(env, flags, 1, GETPC());
6087f87214eSRichard Henderson     }
6097f87214eSRichard Henderson     return ret;
6107f87214eSRichard Henderson }
6117f87214eSRichard Henderson 
612c07f8241SRichard Henderson static void float_invalid_op_div(CPUPPCState *env, int flags,
613c07f8241SRichard Henderson                                  bool set_fprc, uintptr_t retaddr)
614fec59ef3SRichard Henderson {
615c07f8241SRichard Henderson     if (flags & float_flag_invalid_idi) {
616fec59ef3SRichard Henderson         float_invalid_op_vxidi(env, set_fprc, retaddr);
617c07f8241SRichard Henderson     } else if (flags & float_flag_invalid_zdz) {
618fec59ef3SRichard Henderson         float_invalid_op_vxzdz(env, set_fprc, retaddr);
619c07f8241SRichard Henderson     } else if (flags & float_flag_invalid_snan) {
620fec59ef3SRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
621fec59ef3SRichard Henderson     }
622fec59ef3SRichard Henderson }
623fec59ef3SRichard Henderson 
624fcf5ef2aSThomas Huth /* fdiv - fdiv. */
625ae13018dSRichard Henderson float64 helper_fdiv(CPUPPCState *env, float64 arg1, float64 arg2)
626fcf5ef2aSThomas Huth {
627ae13018dSRichard Henderson     float64 ret = float64_div(arg1, arg2, &env->fp_status);
628c07f8241SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
629fcf5ef2aSThomas Huth 
630c07f8241SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
631c07f8241SRichard Henderson         float_invalid_op_div(env, flags, 1, GETPC());
632ae13018dSRichard Henderson     }
633c07f8241SRichard Henderson     if (unlikely(flags & float_flag_divbyzero)) {
634ae13018dSRichard Henderson         float_zero_divide_excp(env, GETPC());
635ae13018dSRichard Henderson     }
636fcf5ef2aSThomas Huth 
637ae13018dSRichard Henderson     return ret;
638fcf5ef2aSThomas Huth }
639fcf5ef2aSThomas Huth 
640d9e792a1SRichard Henderson /* fdivs - fdivs. */
641d9e792a1SRichard Henderson float64 helper_fdivs(CPUPPCState *env, float64 arg1, float64 arg2)
642d9e792a1SRichard Henderson {
643d9e792a1SRichard Henderson     float64 ret = float64r32_div(arg1, arg2, &env->fp_status);
644d9e792a1SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
645d9e792a1SRichard Henderson 
646d9e792a1SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
647d9e792a1SRichard Henderson         float_invalid_op_div(env, flags, 1, GETPC());
648d9e792a1SRichard Henderson     }
649d9e792a1SRichard Henderson     if (unlikely(flags & float_flag_divbyzero)) {
650d9e792a1SRichard Henderson         float_zero_divide_excp(env, GETPC());
651d9e792a1SRichard Henderson     }
652d9e792a1SRichard Henderson 
653d9e792a1SRichard Henderson     return ret;
654d9e792a1SRichard Henderson }
655d9e792a1SRichard Henderson 
656fed12f3bSRichard Henderson static uint64_t float_invalid_cvt(CPUPPCState *env, int flags,
657fed12f3bSRichard Henderson                                   uint64_t ret, uint64_t ret_nan,
658353464eaSRichard Henderson                                   bool set_fprc, uintptr_t retaddr)
659a3dec427SRichard Henderson {
660fed12f3bSRichard Henderson     /*
661fed12f3bSRichard Henderson      * VXCVI is different from most in that it sets two exception bits,
662fed12f3bSRichard Henderson      * VXCVI and VXSNAN for an SNaN input.
663fed12f3bSRichard Henderson      */
664353464eaSRichard Henderson     if (flags & float_flag_invalid_snan) {
665fed12f3bSRichard Henderson         env->fpscr |= FP_VXSNAN;
666a3dec427SRichard Henderson     }
667fed12f3bSRichard Henderson     float_invalid_op_vxcvi(env, set_fprc, retaddr);
668fed12f3bSRichard Henderson 
669fed12f3bSRichard Henderson     return flags & float_flag_invalid_cvti ? ret : ret_nan;
670a3dec427SRichard Henderson }
671fcf5ef2aSThomas Huth 
672fcf5ef2aSThomas Huth #define FPU_FCTI(op, cvt, nanval)                                      \
673a3dec427SRichard Henderson uint64_t helper_##op(CPUPPCState *env, float64 arg)                    \
674fcf5ef2aSThomas Huth {                                                                      \
675a3dec427SRichard Henderson     uint64_t ret = float64_to_##cvt(arg, &env->fp_status);             \
676353464eaSRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);            \
677353464eaSRichard Henderson     if (unlikely(flags & float_flag_invalid)) {                        \
678fed12f3bSRichard Henderson         ret = float_invalid_cvt(env, flags, ret, nanval, 1, GETPC());  \
679fcf5ef2aSThomas Huth     }                                                                  \
680a3dec427SRichard Henderson     return ret;                                                        \
681fcf5ef2aSThomas Huth }
682fcf5ef2aSThomas Huth 
683fcf5ef2aSThomas Huth FPU_FCTI(fctiw, int32, 0x80000000U)
684fcf5ef2aSThomas Huth FPU_FCTI(fctiwz, int32_round_to_zero, 0x80000000U)
685fcf5ef2aSThomas Huth FPU_FCTI(fctiwu, uint32, 0x00000000U)
686fcf5ef2aSThomas Huth FPU_FCTI(fctiwuz, uint32_round_to_zero, 0x00000000U)
687fcf5ef2aSThomas Huth FPU_FCTI(fctid, int64, 0x8000000000000000ULL)
688fcf5ef2aSThomas Huth FPU_FCTI(fctidz, int64_round_to_zero, 0x8000000000000000ULL)
689fcf5ef2aSThomas Huth FPU_FCTI(fctidu, uint64, 0x0000000000000000ULL)
690fcf5ef2aSThomas Huth FPU_FCTI(fctiduz, uint64_round_to_zero, 0x0000000000000000ULL)
691fcf5ef2aSThomas Huth 
692fcf5ef2aSThomas Huth #define FPU_FCFI(op, cvtr, is_single)                      \
693fcf5ef2aSThomas Huth uint64_t helper_##op(CPUPPCState *env, uint64_t arg)       \
694fcf5ef2aSThomas Huth {                                                          \
695fcf5ef2aSThomas Huth     CPU_DoubleU farg;                                      \
696fcf5ef2aSThomas Huth                                                            \
697fcf5ef2aSThomas Huth     if (is_single) {                                       \
698fcf5ef2aSThomas Huth         float32 tmp = cvtr(arg, &env->fp_status);          \
699fcf5ef2aSThomas Huth         farg.d = float32_to_float64(tmp, &env->fp_status); \
700fcf5ef2aSThomas Huth     } else {                                               \
701fcf5ef2aSThomas Huth         farg.d = cvtr(arg, &env->fp_status);               \
702fcf5ef2aSThomas Huth     }                                                      \
7033278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());             \
704fcf5ef2aSThomas Huth     return farg.ll;                                        \
705fcf5ef2aSThomas Huth }
706fcf5ef2aSThomas Huth 
707fcf5ef2aSThomas Huth FPU_FCFI(fcfid, int64_to_float64, 0)
708fcf5ef2aSThomas Huth FPU_FCFI(fcfids, int64_to_float32, 1)
709fcf5ef2aSThomas Huth FPU_FCFI(fcfidu, uint64_to_float64, 0)
710fcf5ef2aSThomas Huth FPU_FCFI(fcfidus, uint64_to_float32, 1)
711fcf5ef2aSThomas Huth 
712b891757eSRichard Henderson static uint64_t do_fri(CPUPPCState *env, uint64_t arg,
7136bce0777SRichard Henderson                        FloatRoundMode rounding_mode)
714fcf5ef2aSThomas Huth {
71563d06e90SBruno Larsen (billionai)     FloatRoundMode old_rounding_mode = get_float_rounding_mode(&env->fp_status);
716a4963527SRichard Henderson     int flags;
717fcf5ef2aSThomas Huth 
718fcf5ef2aSThomas Huth     set_float_rounding_mode(rounding_mode, &env->fp_status);
719a4963527SRichard Henderson     arg = float64_round_to_int(arg, &env->fp_status);
72063d06e90SBruno Larsen (billionai)     set_float_rounding_mode(old_rounding_mode, &env->fp_status);
721fcf5ef2aSThomas Huth 
722a4963527SRichard Henderson     flags = get_float_exception_flags(&env->fp_status);
723a4963527SRichard Henderson     if (flags & float_flag_invalid_snan) {
724a4963527SRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
725fcf5ef2aSThomas Huth     }
726a4963527SRichard Henderson 
727a4963527SRichard Henderson     /* fri* does not set FPSCR[XX] */
728a4963527SRichard Henderson     set_float_exception_flags(flags & ~float_flag_inexact, &env->fp_status);
7293278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
730a4963527SRichard Henderson 
731a4963527SRichard Henderson     return arg;
732fcf5ef2aSThomas Huth }
733fcf5ef2aSThomas Huth 
734fcf5ef2aSThomas Huth uint64_t helper_frin(CPUPPCState *env, uint64_t arg)
735fcf5ef2aSThomas Huth {
736fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_ties_away);
737fcf5ef2aSThomas Huth }
738fcf5ef2aSThomas Huth 
739fcf5ef2aSThomas Huth uint64_t helper_friz(CPUPPCState *env, uint64_t arg)
740fcf5ef2aSThomas Huth {
741fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_to_zero);
742fcf5ef2aSThomas Huth }
743fcf5ef2aSThomas Huth 
744fcf5ef2aSThomas Huth uint64_t helper_frip(CPUPPCState *env, uint64_t arg)
745fcf5ef2aSThomas Huth {
746fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_up);
747fcf5ef2aSThomas Huth }
748fcf5ef2aSThomas Huth 
749fcf5ef2aSThomas Huth uint64_t helper_frim(CPUPPCState *env, uint64_t arg)
750fcf5ef2aSThomas Huth {
751fcf5ef2aSThomas Huth     return do_fri(env, arg, float_round_down);
752fcf5ef2aSThomas Huth }
753fcf5ef2aSThomas Huth 
754e4052bb7SRichard Henderson static void float_invalid_op_madd(CPUPPCState *env, int flags,
755e4052bb7SRichard Henderson                                   bool set_fpcc, uintptr_t retaddr)
756e4052bb7SRichard Henderson {
757e4052bb7SRichard Henderson     if (flags & float_flag_invalid_imz) {
758e4052bb7SRichard Henderson         float_invalid_op_vximz(env, set_fpcc, retaddr);
759e4052bb7SRichard Henderson     } else {
760e4052bb7SRichard Henderson         float_invalid_op_addsub(env, flags, set_fpcc, retaddr);
761806c9d71SNikunj A Dadhania     }
762e4052bb7SRichard Henderson }
763fcf5ef2aSThomas Huth 
764ffdaff8eSRichard Henderson static float64 do_fmadd(CPUPPCState *env, float64 a, float64 b,
765ffdaff8eSRichard Henderson                          float64 c, int madd_flags, uintptr_t retaddr)
766ffdaff8eSRichard Henderson {
767ffdaff8eSRichard Henderson     float64 ret = float64_muladd(a, b, c, madd_flags, &env->fp_status);
768ffdaff8eSRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
769ffdaff8eSRichard Henderson 
7702125ac18SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
771ffdaff8eSRichard Henderson         float_invalid_op_madd(env, flags, 1, retaddr);
772ffdaff8eSRichard Henderson     }
773ffdaff8eSRichard Henderson     return ret;
774ffdaff8eSRichard Henderson }
775ffdaff8eSRichard Henderson 
776d04ca895SRichard Henderson static uint64_t do_fmadds(CPUPPCState *env, float64 a, float64 b,
777d04ca895SRichard Henderson                           float64 c, int madd_flags, uintptr_t retaddr)
778d04ca895SRichard Henderson {
779d04ca895SRichard Henderson     float64 ret = float64r32_muladd(a, b, c, madd_flags, &env->fp_status);
780d04ca895SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
781d04ca895SRichard Henderson 
782d04ca895SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
783d04ca895SRichard Henderson         float_invalid_op_madd(env, flags, 1, retaddr);
784d04ca895SRichard Henderson     }
785d04ca895SRichard Henderson     return ret;
786d04ca895SRichard Henderson }
787d04ca895SRichard Henderson 
788992d7e97SNikunj A Dadhania #define FPU_FMADD(op, madd_flags)                                    \
789992d7e97SNikunj A Dadhania     uint64_t helper_##op(CPUPPCState *env, uint64_t arg1,            \
790992d7e97SNikunj A Dadhania                          uint64_t arg2, uint64_t arg3)               \
791d04ca895SRichard Henderson     { return do_fmadd(env, arg1, arg2, arg3, madd_flags, GETPC()); } \
792d04ca895SRichard Henderson     uint64_t helper_##op##s(CPUPPCState *env, uint64_t arg1,         \
793d04ca895SRichard Henderson                          uint64_t arg2, uint64_t arg3)               \
794d04ca895SRichard Henderson     { return do_fmadds(env, arg1, arg2, arg3, madd_flags, GETPC()); }
795fcf5ef2aSThomas Huth 
796992d7e97SNikunj A Dadhania #define MADD_FLGS 0
797992d7e97SNikunj A Dadhania #define MSUB_FLGS float_muladd_negate_c
798992d7e97SNikunj A Dadhania #define NMADD_FLGS float_muladd_negate_result
799992d7e97SNikunj A Dadhania #define NMSUB_FLGS (float_muladd_negate_c | float_muladd_negate_result)
800fcf5ef2aSThomas Huth 
801992d7e97SNikunj A Dadhania FPU_FMADD(fmadd, MADD_FLGS)
802992d7e97SNikunj A Dadhania FPU_FMADD(fnmadd, NMADD_FLGS)
803992d7e97SNikunj A Dadhania FPU_FMADD(fmsub, MSUB_FLGS)
804992d7e97SNikunj A Dadhania FPU_FMADD(fnmsub, NMSUB_FLGS)
805fcf5ef2aSThomas Huth 
806fcf5ef2aSThomas Huth /* frsp - frsp. */
8077238e55bSRichard Henderson static uint64_t do_frsp(CPUPPCState *env, uint64_t arg, uintptr_t retaddr)
808fcf5ef2aSThomas Huth {
809734cfbd8SRichard Henderson     float32 f32 = float64_to_float32(arg, &env->fp_status);
810734cfbd8SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
811fcf5ef2aSThomas Huth 
812734cfbd8SRichard Henderson     if (unlikely(flags & float_flag_invalid_snan)) {
8137238e55bSRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
814fcf5ef2aSThomas Huth     }
81558c7edefSRichard Henderson     return helper_todouble(f32);
816fcf5ef2aSThomas Huth }
817fcf5ef2aSThomas Huth 
8187238e55bSRichard Henderson uint64_t helper_frsp(CPUPPCState *env, uint64_t arg)
8197238e55bSRichard Henderson {
8207238e55bSRichard Henderson     return do_frsp(env, arg, GETPC());
8217238e55bSRichard Henderson }
8227238e55bSRichard Henderson 
8233d3050ccSRichard Henderson static void float_invalid_op_sqrt(CPUPPCState *env, int flags,
8243d3050ccSRichard Henderson                                   bool set_fpcc, uintptr_t retaddr)
8253d3050ccSRichard Henderson {
8263d3050ccSRichard Henderson     if (unlikely(flags & float_flag_invalid_sqrt)) {
8273d3050ccSRichard Henderson         float_invalid_op_vxsqrt(env, set_fpcc, retaddr);
8283d3050ccSRichard Henderson     } else if (unlikely(flags & float_flag_invalid_snan)) {
8293d3050ccSRichard Henderson         float_invalid_op_vxsnan(env, retaddr);
8303d3050ccSRichard Henderson     }
8313d3050ccSRichard Henderson }
8323d3050ccSRichard Henderson 
83374177ec6SVíctor Colombo #define FPU_FSQRT(name, op)                                                   \
83474177ec6SVíctor Colombo float64 helper_##name(CPUPPCState *env, float64 arg)                          \
83574177ec6SVíctor Colombo {                                                                             \
83674177ec6SVíctor Colombo     float64 ret = op(arg, &env->fp_status);                                   \
83774177ec6SVíctor Colombo     int flags = get_float_exception_flags(&env->fp_status);                   \
83874177ec6SVíctor Colombo                                                                               \
83974177ec6SVíctor Colombo     if (unlikely(flags & float_flag_invalid)) {                               \
84074177ec6SVíctor Colombo         float_invalid_op_sqrt(env, flags, 1, GETPC());                        \
84174177ec6SVíctor Colombo     }                                                                         \
84274177ec6SVíctor Colombo                                                                               \
84374177ec6SVíctor Colombo     return ret;                                                               \
84449ab52efSRichard Henderson }
84549ab52efSRichard Henderson 
84674177ec6SVíctor Colombo FPU_FSQRT(FSQRT, float64_sqrt)
84774177ec6SVíctor Colombo FPU_FSQRT(FSQRTS, float64r32_sqrt)
84841ae890dSRichard Henderson 
849fcf5ef2aSThomas Huth /* fre - fre. */
85038434717SRichard Henderson float64 helper_fre(CPUPPCState *env, float64 arg)
851fcf5ef2aSThomas Huth {
85238434717SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
85338434717SRichard Henderson     float64 ret = float64_div(float64_one, arg, &env->fp_status);
8548ea0b140SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
855fcf5ef2aSThomas Huth 
8568ea0b140SRichard Henderson     if (unlikely(flags & float_flag_invalid_snan)) {
85713c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
858fcf5ef2aSThomas Huth     }
8598ea0b140SRichard Henderson     if (unlikely(flags & float_flag_divbyzero)) {
86038434717SRichard Henderson         float_zero_divide_excp(env, GETPC());
86138434717SRichard Henderson         /* For FPSCR.ZE == 0, the result is 1/2.  */
86238434717SRichard Henderson         ret = float64_set_sign(float64_half, float64_is_neg(arg));
86338434717SRichard Henderson     }
86438434717SRichard Henderson 
86538434717SRichard Henderson     return ret;
866fcf5ef2aSThomas Huth }
867fcf5ef2aSThomas Huth 
868fcf5ef2aSThomas Huth /* fres - fres. */
869fcf5ef2aSThomas Huth uint64_t helper_fres(CPUPPCState *env, uint64_t arg)
870fcf5ef2aSThomas Huth {
8717d82ea34SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
8727d82ea34SRichard Henderson     float64 ret = float64r32_div(float64_one, arg, &env->fp_status);
8737d82ea34SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
874fcf5ef2aSThomas Huth 
8757d82ea34SRichard Henderson     if (unlikely(flags & float_flag_invalid_snan)) {
87613c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
877fcf5ef2aSThomas Huth     }
8787d82ea34SRichard Henderson     if (unlikely(flags & float_flag_divbyzero)) {
8797d82ea34SRichard Henderson         float_zero_divide_excp(env, GETPC());
8807d82ea34SRichard Henderson         /* For FPSCR.ZE == 0, the result is 1/2.  */
8817d82ea34SRichard Henderson         ret = float64_set_sign(float64_half, float64_is_neg(arg));
8827d82ea34SRichard Henderson     }
883fcf5ef2aSThomas Huth 
8847d82ea34SRichard Henderson     return ret;
885fcf5ef2aSThomas Huth }
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth /* frsqrte  - frsqrte. */
88838434717SRichard Henderson float64 helper_frsqrte(CPUPPCState *env, float64 arg)
889fcf5ef2aSThomas Huth {
89038434717SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
89138434717SRichard Henderson     float64 rets = float64_sqrt(arg, &env->fp_status);
89238434717SRichard Henderson     float64 retd = float64_div(float64_one, rets, &env->fp_status);
8933d3050ccSRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
894fcf5ef2aSThomas Huth 
8953d3050ccSRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
8963d3050ccSRichard Henderson         float_invalid_op_sqrt(env, flags, 1, GETPC());
89738434717SRichard Henderson     }
8983d3050ccSRichard Henderson     if (unlikely(flags & float_flag_divbyzero)) {
89938434717SRichard Henderson         /* Reciprocal of (square root of) zero.  */
90038434717SRichard Henderson         float_zero_divide_excp(env, GETPC());
90138434717SRichard Henderson     }
902fcf5ef2aSThomas Huth 
90338434717SRichard Henderson     return retd;
904fcf5ef2aSThomas Huth }
905fcf5ef2aSThomas Huth 
906dedbfda7SRichard Henderson /* frsqrtes  - frsqrtes. */
907dedbfda7SRichard Henderson float64 helper_frsqrtes(CPUPPCState *env, float64 arg)
908dedbfda7SRichard Henderson {
909dedbfda7SRichard Henderson     /* "Estimate" the reciprocal with actual division.  */
910dedbfda7SRichard Henderson     float64 rets = float64_sqrt(arg, &env->fp_status);
911dedbfda7SRichard Henderson     float64 retd = float64r32_div(float64_one, rets, &env->fp_status);
912dedbfda7SRichard Henderson     int flags = get_float_exception_flags(&env->fp_status);
913dedbfda7SRichard Henderson 
914dedbfda7SRichard Henderson     if (unlikely(flags & float_flag_invalid)) {
915dedbfda7SRichard Henderson         float_invalid_op_sqrt(env, flags, 1, GETPC());
916dedbfda7SRichard Henderson     }
917dedbfda7SRichard Henderson     if (unlikely(flags & float_flag_divbyzero)) {
918dedbfda7SRichard Henderson         /* Reciprocal of (square root of) zero.  */
919dedbfda7SRichard Henderson         float_zero_divide_excp(env, GETPC());
920dedbfda7SRichard Henderson     }
921dedbfda7SRichard Henderson 
922dedbfda7SRichard Henderson     return retd;
923dedbfda7SRichard Henderson }
924dedbfda7SRichard Henderson 
925fcf5ef2aSThomas Huth /* fsel - fsel. */
926eb69a84bSMatheus Ferst uint64_t helper_FSEL(uint64_t a, uint64_t b, uint64_t c)
927fcf5ef2aSThomas Huth {
928eb69a84bSMatheus Ferst     CPU_DoubleU fa;
929fcf5ef2aSThomas Huth 
930eb69a84bSMatheus Ferst     fa.ll = a;
931fcf5ef2aSThomas Huth 
932eb69a84bSMatheus Ferst     if ((!float64_is_neg(fa.d) || float64_is_zero(fa.d)) &&
933eb69a84bSMatheus Ferst         !float64_is_any_nan(fa.d)) {
934eb69a84bSMatheus Ferst         return c;
935fcf5ef2aSThomas Huth     } else {
936eb69a84bSMatheus Ferst         return b;
937fcf5ef2aSThomas Huth     }
938fcf5ef2aSThomas Huth }
939fcf5ef2aSThomas Huth 
940fcf5ef2aSThomas Huth uint32_t helper_ftdiv(uint64_t fra, uint64_t frb)
941fcf5ef2aSThomas Huth {
942fcf5ef2aSThomas Huth     int fe_flag = 0;
943fcf5ef2aSThomas Huth     int fg_flag = 0;
944fcf5ef2aSThomas Huth 
945fcf5ef2aSThomas Huth     if (unlikely(float64_is_infinity(fra) ||
946fcf5ef2aSThomas Huth                  float64_is_infinity(frb) ||
947fcf5ef2aSThomas Huth                  float64_is_zero(frb))) {
948fcf5ef2aSThomas Huth         fe_flag = 1;
949fcf5ef2aSThomas Huth         fg_flag = 1;
950fcf5ef2aSThomas Huth     } else {
951fcf5ef2aSThomas Huth         int e_a = ppc_float64_get_unbiased_exp(fra);
952fcf5ef2aSThomas Huth         int e_b = ppc_float64_get_unbiased_exp(frb);
953fcf5ef2aSThomas Huth 
954fcf5ef2aSThomas Huth         if (unlikely(float64_is_any_nan(fra) ||
955fcf5ef2aSThomas Huth                      float64_is_any_nan(frb))) {
956fcf5ef2aSThomas Huth             fe_flag = 1;
957fcf5ef2aSThomas Huth         } else if ((e_b <= -1022) || (e_b >= 1021)) {
958fcf5ef2aSThomas Huth             fe_flag = 1;
959fcf5ef2aSThomas Huth         } else if (!float64_is_zero(fra) &&
960fcf5ef2aSThomas Huth                    (((e_a - e_b) >= 1023) ||
961fcf5ef2aSThomas Huth                     ((e_a - e_b) <= -1021) ||
962fcf5ef2aSThomas Huth                     (e_a <= -970))) {
963fcf5ef2aSThomas Huth             fe_flag = 1;
964fcf5ef2aSThomas Huth         }
965fcf5ef2aSThomas Huth 
966fcf5ef2aSThomas Huth         if (unlikely(float64_is_zero_or_denormal(frb))) {
967fcf5ef2aSThomas Huth             /* XB is not zero because of the above check and */
968fcf5ef2aSThomas Huth             /* so must be denormalized.                      */
969fcf5ef2aSThomas Huth             fg_flag = 1;
970fcf5ef2aSThomas Huth         }
971fcf5ef2aSThomas Huth     }
972fcf5ef2aSThomas Huth 
973fcf5ef2aSThomas Huth     return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
974fcf5ef2aSThomas Huth }
975fcf5ef2aSThomas Huth 
976fcf5ef2aSThomas Huth uint32_t helper_ftsqrt(uint64_t frb)
977fcf5ef2aSThomas Huth {
978fcf5ef2aSThomas Huth     int fe_flag = 0;
979fcf5ef2aSThomas Huth     int fg_flag = 0;
980fcf5ef2aSThomas Huth 
981fcf5ef2aSThomas Huth     if (unlikely(float64_is_infinity(frb) || float64_is_zero(frb))) {
982fcf5ef2aSThomas Huth         fe_flag = 1;
983fcf5ef2aSThomas Huth         fg_flag = 1;
984fcf5ef2aSThomas Huth     } else {
985fcf5ef2aSThomas Huth         int e_b = ppc_float64_get_unbiased_exp(frb);
986fcf5ef2aSThomas Huth 
987fcf5ef2aSThomas Huth         if (unlikely(float64_is_any_nan(frb))) {
988fcf5ef2aSThomas Huth             fe_flag = 1;
989fcf5ef2aSThomas Huth         } else if (unlikely(float64_is_zero(frb))) {
990fcf5ef2aSThomas Huth             fe_flag = 1;
991fcf5ef2aSThomas Huth         } else if (unlikely(float64_is_neg(frb))) {
992fcf5ef2aSThomas Huth             fe_flag = 1;
993fcf5ef2aSThomas Huth         } else if (!float64_is_zero(frb) && (e_b <= (-1022 + 52))) {
994fcf5ef2aSThomas Huth             fe_flag = 1;
995fcf5ef2aSThomas Huth         }
996fcf5ef2aSThomas Huth 
997fcf5ef2aSThomas Huth         if (unlikely(float64_is_zero_or_denormal(frb))) {
998fcf5ef2aSThomas Huth             /* XB is not zero because of the above check and */
999fcf5ef2aSThomas Huth             /* therefore must be denormalized.               */
1000fcf5ef2aSThomas Huth             fg_flag = 1;
1001fcf5ef2aSThomas Huth         }
1002fcf5ef2aSThomas Huth     }
1003fcf5ef2aSThomas Huth 
1004fcf5ef2aSThomas Huth     return 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0);
1005fcf5ef2aSThomas Huth }
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth void helper_fcmpu(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
1008fcf5ef2aSThomas Huth                   uint32_t crfD)
1009fcf5ef2aSThomas Huth {
1010fcf5ef2aSThomas Huth     CPU_DoubleU farg1, farg2;
1011fcf5ef2aSThomas Huth     uint32_t ret = 0;
1012fcf5ef2aSThomas Huth 
1013fcf5ef2aSThomas Huth     farg1.ll = arg1;
1014fcf5ef2aSThomas Huth     farg2.ll = arg2;
1015fcf5ef2aSThomas Huth 
1016fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(farg1.d) ||
1017fcf5ef2aSThomas Huth                  float64_is_any_nan(farg2.d))) {
1018fcf5ef2aSThomas Huth         ret = 0x01UL;
1019fcf5ef2aSThomas Huth     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1020fcf5ef2aSThomas Huth         ret = 0x08UL;
1021fcf5ef2aSThomas Huth     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1022fcf5ef2aSThomas Huth         ret = 0x04UL;
1023fcf5ef2aSThomas Huth     } else {
1024fcf5ef2aSThomas Huth         ret = 0x02UL;
1025fcf5ef2aSThomas Huth     }
1026fcf5ef2aSThomas Huth 
10275c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
10285c94dd38SPaul A. Clarke     env->fpscr |= ret << FPSCR_FPCC;
1029fcf5ef2aSThomas Huth     env->crf[crfD] = ret;
1030fcf5ef2aSThomas Huth     if (unlikely(ret == 0x01UL
1031fcf5ef2aSThomas Huth                  && (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
1032fcf5ef2aSThomas Huth                      float64_is_signaling_nan(farg2.d, &env->fp_status)))) {
1033fcf5ef2aSThomas Huth         /* sNaN comparison */
103413c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
1035fcf5ef2aSThomas Huth     }
1036fcf5ef2aSThomas Huth }
1037fcf5ef2aSThomas Huth 
1038fcf5ef2aSThomas Huth void helper_fcmpo(CPUPPCState *env, uint64_t arg1, uint64_t arg2,
1039fcf5ef2aSThomas Huth                   uint32_t crfD)
1040fcf5ef2aSThomas Huth {
1041fcf5ef2aSThomas Huth     CPU_DoubleU farg1, farg2;
1042fcf5ef2aSThomas Huth     uint32_t ret = 0;
1043fcf5ef2aSThomas Huth 
1044fcf5ef2aSThomas Huth     farg1.ll = arg1;
1045fcf5ef2aSThomas Huth     farg2.ll = arg2;
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(farg1.d) ||
1048fcf5ef2aSThomas Huth                  float64_is_any_nan(farg2.d))) {
1049fcf5ef2aSThomas Huth         ret = 0x01UL;
1050fcf5ef2aSThomas Huth     } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
1051fcf5ef2aSThomas Huth         ret = 0x08UL;
1052fcf5ef2aSThomas Huth     } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
1053fcf5ef2aSThomas Huth         ret = 0x04UL;
1054fcf5ef2aSThomas Huth     } else {
1055fcf5ef2aSThomas Huth         ret = 0x02UL;
1056fcf5ef2aSThomas Huth     }
1057fcf5ef2aSThomas Huth 
10585c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
10595c94dd38SPaul A. Clarke     env->fpscr |= ret << FPSCR_FPCC;
10605c94dd38SPaul A. Clarke     env->crf[crfD] = (uint32_t) ret;
1061fcf5ef2aSThomas Huth     if (unlikely(ret == 0x01UL)) {
106213c9115fSRichard Henderson         float_invalid_op_vxvc(env, 1, GETPC());
1063fcf5ef2aSThomas Huth         if (float64_is_signaling_nan(farg1.d, &env->fp_status) ||
1064fcf5ef2aSThomas Huth             float64_is_signaling_nan(farg2.d, &env->fp_status)) {
1065fcf5ef2aSThomas Huth             /* sNaN comparison */
106613c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());
1067fcf5ef2aSThomas Huth         }
1068fcf5ef2aSThomas Huth     }
1069fcf5ef2aSThomas Huth }
1070fcf5ef2aSThomas Huth 
1071fcf5ef2aSThomas Huth /* Single-precision floating-point conversions */
1072fcf5ef2aSThomas Huth static inline uint32_t efscfsi(CPUPPCState *env, uint32_t val)
1073fcf5ef2aSThomas Huth {
1074fcf5ef2aSThomas Huth     CPU_FloatU u;
1075fcf5ef2aSThomas Huth 
1076fcf5ef2aSThomas Huth     u.f = int32_to_float32(val, &env->vec_status);
1077fcf5ef2aSThomas Huth 
1078fcf5ef2aSThomas Huth     return u.l;
1079fcf5ef2aSThomas Huth }
1080fcf5ef2aSThomas Huth 
1081fcf5ef2aSThomas Huth static inline uint32_t efscfui(CPUPPCState *env, uint32_t val)
1082fcf5ef2aSThomas Huth {
1083fcf5ef2aSThomas Huth     CPU_FloatU u;
1084fcf5ef2aSThomas Huth 
1085fcf5ef2aSThomas Huth     u.f = uint32_to_float32(val, &env->vec_status);
1086fcf5ef2aSThomas Huth 
1087fcf5ef2aSThomas Huth     return u.l;
1088fcf5ef2aSThomas Huth }
1089fcf5ef2aSThomas Huth 
1090fcf5ef2aSThomas Huth static inline int32_t efsctsi(CPUPPCState *env, uint32_t val)
1091fcf5ef2aSThomas Huth {
1092fcf5ef2aSThomas Huth     CPU_FloatU u;
1093fcf5ef2aSThomas Huth 
1094fcf5ef2aSThomas Huth     u.l = val;
1095fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1096fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1097fcf5ef2aSThomas Huth         return 0;
1098fcf5ef2aSThomas Huth     }
1099fcf5ef2aSThomas Huth 
1100fcf5ef2aSThomas Huth     return float32_to_int32(u.f, &env->vec_status);
1101fcf5ef2aSThomas Huth }
1102fcf5ef2aSThomas Huth 
1103fcf5ef2aSThomas Huth static inline uint32_t efsctui(CPUPPCState *env, uint32_t val)
1104fcf5ef2aSThomas Huth {
1105fcf5ef2aSThomas Huth     CPU_FloatU u;
1106fcf5ef2aSThomas Huth 
1107fcf5ef2aSThomas Huth     u.l = val;
1108fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1109fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1110fcf5ef2aSThomas Huth         return 0;
1111fcf5ef2aSThomas Huth     }
1112fcf5ef2aSThomas Huth 
1113fcf5ef2aSThomas Huth     return float32_to_uint32(u.f, &env->vec_status);
1114fcf5ef2aSThomas Huth }
1115fcf5ef2aSThomas Huth 
1116fcf5ef2aSThomas Huth static inline uint32_t efsctsiz(CPUPPCState *env, uint32_t val)
1117fcf5ef2aSThomas Huth {
1118fcf5ef2aSThomas Huth     CPU_FloatU u;
1119fcf5ef2aSThomas Huth 
1120fcf5ef2aSThomas Huth     u.l = val;
1121fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1122fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1123fcf5ef2aSThomas Huth         return 0;
1124fcf5ef2aSThomas Huth     }
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth     return float32_to_int32_round_to_zero(u.f, &env->vec_status);
1127fcf5ef2aSThomas Huth }
1128fcf5ef2aSThomas Huth 
1129fcf5ef2aSThomas Huth static inline uint32_t efsctuiz(CPUPPCState *env, uint32_t val)
1130fcf5ef2aSThomas Huth {
1131fcf5ef2aSThomas Huth     CPU_FloatU u;
1132fcf5ef2aSThomas Huth 
1133fcf5ef2aSThomas Huth     u.l = val;
1134fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1135fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1136fcf5ef2aSThomas Huth         return 0;
1137fcf5ef2aSThomas Huth     }
1138fcf5ef2aSThomas Huth 
1139fcf5ef2aSThomas Huth     return float32_to_uint32_round_to_zero(u.f, &env->vec_status);
1140fcf5ef2aSThomas Huth }
1141fcf5ef2aSThomas Huth 
1142fcf5ef2aSThomas Huth static inline uint32_t efscfsf(CPUPPCState *env, uint32_t val)
1143fcf5ef2aSThomas Huth {
1144fcf5ef2aSThomas Huth     CPU_FloatU u;
1145fcf5ef2aSThomas Huth     float32 tmp;
1146fcf5ef2aSThomas Huth 
1147fcf5ef2aSThomas Huth     u.f = int32_to_float32(val, &env->vec_status);
1148fcf5ef2aSThomas Huth     tmp = int64_to_float32(1ULL << 32, &env->vec_status);
1149fcf5ef2aSThomas Huth     u.f = float32_div(u.f, tmp, &env->vec_status);
1150fcf5ef2aSThomas Huth 
1151fcf5ef2aSThomas Huth     return u.l;
1152fcf5ef2aSThomas Huth }
1153fcf5ef2aSThomas Huth 
1154fcf5ef2aSThomas Huth static inline uint32_t efscfuf(CPUPPCState *env, uint32_t val)
1155fcf5ef2aSThomas Huth {
1156fcf5ef2aSThomas Huth     CPU_FloatU u;
1157fcf5ef2aSThomas Huth     float32 tmp;
1158fcf5ef2aSThomas Huth 
1159fcf5ef2aSThomas Huth     u.f = uint32_to_float32(val, &env->vec_status);
1160fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1161fcf5ef2aSThomas Huth     u.f = float32_div(u.f, tmp, &env->vec_status);
1162fcf5ef2aSThomas Huth 
1163fcf5ef2aSThomas Huth     return u.l;
1164fcf5ef2aSThomas Huth }
1165fcf5ef2aSThomas Huth 
1166fcf5ef2aSThomas Huth static inline uint32_t efsctsf(CPUPPCState *env, uint32_t val)
1167fcf5ef2aSThomas Huth {
1168fcf5ef2aSThomas Huth     CPU_FloatU u;
1169fcf5ef2aSThomas Huth     float32 tmp;
1170fcf5ef2aSThomas Huth 
1171fcf5ef2aSThomas Huth     u.l = val;
1172fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1173fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1174fcf5ef2aSThomas Huth         return 0;
1175fcf5ef2aSThomas Huth     }
1176fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1177fcf5ef2aSThomas Huth     u.f = float32_mul(u.f, tmp, &env->vec_status);
1178fcf5ef2aSThomas Huth 
1179fcf5ef2aSThomas Huth     return float32_to_int32(u.f, &env->vec_status);
1180fcf5ef2aSThomas Huth }
1181fcf5ef2aSThomas Huth 
1182fcf5ef2aSThomas Huth static inline uint32_t efsctuf(CPUPPCState *env, uint32_t val)
1183fcf5ef2aSThomas Huth {
1184fcf5ef2aSThomas Huth     CPU_FloatU u;
1185fcf5ef2aSThomas Huth     float32 tmp;
1186fcf5ef2aSThomas Huth 
1187fcf5ef2aSThomas Huth     u.l = val;
1188fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1189fcf5ef2aSThomas Huth     if (unlikely(float32_is_quiet_nan(u.f, &env->vec_status))) {
1190fcf5ef2aSThomas Huth         return 0;
1191fcf5ef2aSThomas Huth     }
1192fcf5ef2aSThomas Huth     tmp = uint64_to_float32(1ULL << 32, &env->vec_status);
1193fcf5ef2aSThomas Huth     u.f = float32_mul(u.f, tmp, &env->vec_status);
1194fcf5ef2aSThomas Huth 
1195fcf5ef2aSThomas Huth     return float32_to_uint32(u.f, &env->vec_status);
1196fcf5ef2aSThomas Huth }
1197fcf5ef2aSThomas Huth 
1198fcf5ef2aSThomas Huth #define HELPER_SPE_SINGLE_CONV(name)                              \
1199fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t val)       \
1200fcf5ef2aSThomas Huth     {                                                             \
1201fcf5ef2aSThomas Huth         return e##name(env, val);                                 \
1202fcf5ef2aSThomas Huth     }
1203fcf5ef2aSThomas Huth /* efscfsi */
1204fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfsi);
1205fcf5ef2aSThomas Huth /* efscfui */
1206fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfui);
1207fcf5ef2aSThomas Huth /* efscfuf */
1208fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfuf);
1209fcf5ef2aSThomas Huth /* efscfsf */
1210fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fscfsf);
1211fcf5ef2aSThomas Huth /* efsctsi */
1212fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsi);
1213fcf5ef2aSThomas Huth /* efsctui */
1214fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctui);
1215fcf5ef2aSThomas Huth /* efsctsiz */
1216fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsiz);
1217fcf5ef2aSThomas Huth /* efsctuiz */
1218fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctuiz);
1219fcf5ef2aSThomas Huth /* efsctsf */
1220fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctsf);
1221fcf5ef2aSThomas Huth /* efsctuf */
1222fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_CONV(fsctuf);
1223fcf5ef2aSThomas Huth 
1224fcf5ef2aSThomas Huth #define HELPER_SPE_VECTOR_CONV(name)                            \
1225fcf5ef2aSThomas Huth     uint64_t helper_ev##name(CPUPPCState *env, uint64_t val)    \
1226fcf5ef2aSThomas Huth     {                                                           \
1227fcf5ef2aSThomas Huth         return ((uint64_t)e##name(env, val >> 32) << 32) |      \
1228fcf5ef2aSThomas Huth             (uint64_t)e##name(env, val);                        \
1229fcf5ef2aSThomas Huth     }
1230fcf5ef2aSThomas Huth /* evfscfsi */
1231fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfsi);
1232fcf5ef2aSThomas Huth /* evfscfui */
1233fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfui);
1234fcf5ef2aSThomas Huth /* evfscfuf */
1235fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfuf);
1236fcf5ef2aSThomas Huth /* evfscfsf */
1237fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fscfsf);
1238fcf5ef2aSThomas Huth /* evfsctsi */
1239fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsi);
1240fcf5ef2aSThomas Huth /* evfsctui */
1241fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctui);
1242fcf5ef2aSThomas Huth /* evfsctsiz */
1243fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsiz);
1244fcf5ef2aSThomas Huth /* evfsctuiz */
1245fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctuiz);
1246fcf5ef2aSThomas Huth /* evfsctsf */
1247fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctsf);
1248fcf5ef2aSThomas Huth /* evfsctuf */
1249fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_CONV(fsctuf);
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth /* Single-precision floating-point arithmetic */
1252fcf5ef2aSThomas Huth static inline uint32_t efsadd(CPUPPCState *env, uint32_t op1, uint32_t op2)
1253fcf5ef2aSThomas Huth {
1254fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1255fcf5ef2aSThomas Huth 
1256fcf5ef2aSThomas Huth     u1.l = op1;
1257fcf5ef2aSThomas Huth     u2.l = op2;
1258fcf5ef2aSThomas Huth     u1.f = float32_add(u1.f, u2.f, &env->vec_status);
1259fcf5ef2aSThomas Huth     return u1.l;
1260fcf5ef2aSThomas Huth }
1261fcf5ef2aSThomas Huth 
1262fcf5ef2aSThomas Huth static inline uint32_t efssub(CPUPPCState *env, uint32_t op1, uint32_t op2)
1263fcf5ef2aSThomas Huth {
1264fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1265fcf5ef2aSThomas Huth 
1266fcf5ef2aSThomas Huth     u1.l = op1;
1267fcf5ef2aSThomas Huth     u2.l = op2;
1268fcf5ef2aSThomas Huth     u1.f = float32_sub(u1.f, u2.f, &env->vec_status);
1269fcf5ef2aSThomas Huth     return u1.l;
1270fcf5ef2aSThomas Huth }
1271fcf5ef2aSThomas Huth 
1272fcf5ef2aSThomas Huth static inline uint32_t efsmul(CPUPPCState *env, uint32_t op1, uint32_t op2)
1273fcf5ef2aSThomas Huth {
1274fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1275fcf5ef2aSThomas Huth 
1276fcf5ef2aSThomas Huth     u1.l = op1;
1277fcf5ef2aSThomas Huth     u2.l = op2;
1278fcf5ef2aSThomas Huth     u1.f = float32_mul(u1.f, u2.f, &env->vec_status);
1279fcf5ef2aSThomas Huth     return u1.l;
1280fcf5ef2aSThomas Huth }
1281fcf5ef2aSThomas Huth 
1282fcf5ef2aSThomas Huth static inline uint32_t efsdiv(CPUPPCState *env, uint32_t op1, uint32_t op2)
1283fcf5ef2aSThomas Huth {
1284fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1285fcf5ef2aSThomas Huth 
1286fcf5ef2aSThomas Huth     u1.l = op1;
1287fcf5ef2aSThomas Huth     u2.l = op2;
1288fcf5ef2aSThomas Huth     u1.f = float32_div(u1.f, u2.f, &env->vec_status);
1289fcf5ef2aSThomas Huth     return u1.l;
1290fcf5ef2aSThomas Huth }
1291fcf5ef2aSThomas Huth 
1292fcf5ef2aSThomas Huth #define HELPER_SPE_SINGLE_ARITH(name)                                   \
1293fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1294fcf5ef2aSThomas Huth     {                                                                   \
1295fcf5ef2aSThomas Huth         return e##name(env, op1, op2);                                  \
1296fcf5ef2aSThomas Huth     }
1297fcf5ef2aSThomas Huth /* efsadd */
1298fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsadd);
1299fcf5ef2aSThomas Huth /* efssub */
1300fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fssub);
1301fcf5ef2aSThomas Huth /* efsmul */
1302fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsmul);
1303fcf5ef2aSThomas Huth /* efsdiv */
1304fcf5ef2aSThomas Huth HELPER_SPE_SINGLE_ARITH(fsdiv);
1305fcf5ef2aSThomas Huth 
1306fcf5ef2aSThomas Huth #define HELPER_SPE_VECTOR_ARITH(name)                                   \
1307fcf5ef2aSThomas Huth     uint64_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1308fcf5ef2aSThomas Huth     {                                                                   \
1309fcf5ef2aSThomas Huth         return ((uint64_t)e##name(env, op1 >> 32, op2 >> 32) << 32) |   \
1310fcf5ef2aSThomas Huth             (uint64_t)e##name(env, op1, op2);                           \
1311fcf5ef2aSThomas Huth     }
1312fcf5ef2aSThomas Huth /* evfsadd */
1313fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsadd);
1314fcf5ef2aSThomas Huth /* evfssub */
1315fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fssub);
1316fcf5ef2aSThomas Huth /* evfsmul */
1317fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsmul);
1318fcf5ef2aSThomas Huth /* evfsdiv */
1319fcf5ef2aSThomas Huth HELPER_SPE_VECTOR_ARITH(fsdiv);
1320fcf5ef2aSThomas Huth 
1321fcf5ef2aSThomas Huth /* Single-precision floating-point comparisons */
1322fcf5ef2aSThomas Huth static inline uint32_t efscmplt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1323fcf5ef2aSThomas Huth {
1324fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1325fcf5ef2aSThomas Huth 
1326fcf5ef2aSThomas Huth     u1.l = op1;
1327fcf5ef2aSThomas Huth     u2.l = op2;
1328fcf5ef2aSThomas Huth     return float32_lt(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1329fcf5ef2aSThomas Huth }
1330fcf5ef2aSThomas Huth 
1331fcf5ef2aSThomas Huth static inline uint32_t efscmpgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1332fcf5ef2aSThomas Huth {
1333fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1334fcf5ef2aSThomas Huth 
1335fcf5ef2aSThomas Huth     u1.l = op1;
1336fcf5ef2aSThomas Huth     u2.l = op2;
1337fcf5ef2aSThomas Huth     return float32_le(u1.f, u2.f, &env->vec_status) ? 0 : 4;
1338fcf5ef2aSThomas Huth }
1339fcf5ef2aSThomas Huth 
1340fcf5ef2aSThomas Huth static inline uint32_t efscmpeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1341fcf5ef2aSThomas Huth {
1342fcf5ef2aSThomas Huth     CPU_FloatU u1, u2;
1343fcf5ef2aSThomas Huth 
1344fcf5ef2aSThomas Huth     u1.l = op1;
1345fcf5ef2aSThomas Huth     u2.l = op2;
1346fcf5ef2aSThomas Huth     return float32_eq(u1.f, u2.f, &env->vec_status) ? 4 : 0;
1347fcf5ef2aSThomas Huth }
1348fcf5ef2aSThomas Huth 
1349fcf5ef2aSThomas Huth static inline uint32_t efststlt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1350fcf5ef2aSThomas Huth {
1351fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1352fcf5ef2aSThomas Huth     return efscmplt(env, op1, op2);
1353fcf5ef2aSThomas Huth }
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth static inline uint32_t efststgt(CPUPPCState *env, uint32_t op1, uint32_t op2)
1356fcf5ef2aSThomas Huth {
1357fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1358fcf5ef2aSThomas Huth     return efscmpgt(env, op1, op2);
1359fcf5ef2aSThomas Huth }
1360fcf5ef2aSThomas Huth 
1361fcf5ef2aSThomas Huth static inline uint32_t efststeq(CPUPPCState *env, uint32_t op1, uint32_t op2)
1362fcf5ef2aSThomas Huth {
1363fcf5ef2aSThomas Huth     /* XXX: TODO: ignore special values (NaN, infinites, ...) */
1364fcf5ef2aSThomas Huth     return efscmpeq(env, op1, op2);
1365fcf5ef2aSThomas Huth }
1366fcf5ef2aSThomas Huth 
1367fcf5ef2aSThomas Huth #define HELPER_SINGLE_SPE_CMP(name)                                     \
1368fcf5ef2aSThomas Huth     uint32_t helper_e##name(CPUPPCState *env, uint32_t op1, uint32_t op2) \
1369fcf5ef2aSThomas Huth     {                                                                   \
1370fcf5ef2aSThomas Huth         return e##name(env, op1, op2);                                  \
1371fcf5ef2aSThomas Huth     }
1372fcf5ef2aSThomas Huth /* efststlt */
1373fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststlt);
1374fcf5ef2aSThomas Huth /* efststgt */
1375fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststgt);
1376fcf5ef2aSThomas Huth /* efststeq */
1377fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fststeq);
1378fcf5ef2aSThomas Huth /* efscmplt */
1379fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmplt);
1380fcf5ef2aSThomas Huth /* efscmpgt */
1381fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmpgt);
1382fcf5ef2aSThomas Huth /* efscmpeq */
1383fcf5ef2aSThomas Huth HELPER_SINGLE_SPE_CMP(fscmpeq);
1384fcf5ef2aSThomas Huth 
1385fcf5ef2aSThomas Huth static inline uint32_t evcmp_merge(int t0, int t1)
1386fcf5ef2aSThomas Huth {
1387fcf5ef2aSThomas Huth     return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
1388fcf5ef2aSThomas Huth }
1389fcf5ef2aSThomas Huth 
1390fcf5ef2aSThomas Huth #define HELPER_VECTOR_SPE_CMP(name)                                     \
1391fcf5ef2aSThomas Huth     uint32_t helper_ev##name(CPUPPCState *env, uint64_t op1, uint64_t op2) \
1392fcf5ef2aSThomas Huth     {                                                                   \
1393fcf5ef2aSThomas Huth         return evcmp_merge(e##name(env, op1 >> 32, op2 >> 32),          \
1394fcf5ef2aSThomas Huth                            e##name(env, op1, op2));                     \
1395fcf5ef2aSThomas Huth     }
1396fcf5ef2aSThomas Huth /* evfststlt */
1397fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststlt);
1398fcf5ef2aSThomas Huth /* evfststgt */
1399fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststgt);
1400fcf5ef2aSThomas Huth /* evfststeq */
1401fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fststeq);
1402fcf5ef2aSThomas Huth /* evfscmplt */
1403fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmplt);
1404fcf5ef2aSThomas Huth /* evfscmpgt */
1405fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmpgt);
1406fcf5ef2aSThomas Huth /* evfscmpeq */
1407fcf5ef2aSThomas Huth HELPER_VECTOR_SPE_CMP(fscmpeq);
1408fcf5ef2aSThomas Huth 
1409fcf5ef2aSThomas Huth /* Double-precision floating-point conversion */
1410fcf5ef2aSThomas Huth uint64_t helper_efdcfsi(CPUPPCState *env, uint32_t val)
1411fcf5ef2aSThomas Huth {
1412fcf5ef2aSThomas Huth     CPU_DoubleU u;
1413fcf5ef2aSThomas Huth 
1414fcf5ef2aSThomas Huth     u.d = int32_to_float64(val, &env->vec_status);
1415fcf5ef2aSThomas Huth 
1416fcf5ef2aSThomas Huth     return u.ll;
1417fcf5ef2aSThomas Huth }
1418fcf5ef2aSThomas Huth 
1419fcf5ef2aSThomas Huth uint64_t helper_efdcfsid(CPUPPCState *env, uint64_t val)
1420fcf5ef2aSThomas Huth {
1421fcf5ef2aSThomas Huth     CPU_DoubleU u;
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth     u.d = int64_to_float64(val, &env->vec_status);
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth     return u.ll;
1426fcf5ef2aSThomas Huth }
1427fcf5ef2aSThomas Huth 
1428fcf5ef2aSThomas Huth uint64_t helper_efdcfui(CPUPPCState *env, uint32_t val)
1429fcf5ef2aSThomas Huth {
1430fcf5ef2aSThomas Huth     CPU_DoubleU u;
1431fcf5ef2aSThomas Huth 
1432fcf5ef2aSThomas Huth     u.d = uint32_to_float64(val, &env->vec_status);
1433fcf5ef2aSThomas Huth 
1434fcf5ef2aSThomas Huth     return u.ll;
1435fcf5ef2aSThomas Huth }
1436fcf5ef2aSThomas Huth 
1437fcf5ef2aSThomas Huth uint64_t helper_efdcfuid(CPUPPCState *env, uint64_t val)
1438fcf5ef2aSThomas Huth {
1439fcf5ef2aSThomas Huth     CPU_DoubleU u;
1440fcf5ef2aSThomas Huth 
1441fcf5ef2aSThomas Huth     u.d = uint64_to_float64(val, &env->vec_status);
1442fcf5ef2aSThomas Huth 
1443fcf5ef2aSThomas Huth     return u.ll;
1444fcf5ef2aSThomas Huth }
1445fcf5ef2aSThomas Huth 
1446fcf5ef2aSThomas Huth uint32_t helper_efdctsi(CPUPPCState *env, uint64_t val)
1447fcf5ef2aSThomas Huth {
1448fcf5ef2aSThomas Huth     CPU_DoubleU u;
1449fcf5ef2aSThomas Huth 
1450fcf5ef2aSThomas Huth     u.ll = val;
1451fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1452fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1453fcf5ef2aSThomas Huth         return 0;
1454fcf5ef2aSThomas Huth     }
1455fcf5ef2aSThomas Huth 
1456fcf5ef2aSThomas Huth     return float64_to_int32(u.d, &env->vec_status);
1457fcf5ef2aSThomas Huth }
1458fcf5ef2aSThomas Huth 
1459fcf5ef2aSThomas Huth uint32_t helper_efdctui(CPUPPCState *env, uint64_t val)
1460fcf5ef2aSThomas Huth {
1461fcf5ef2aSThomas Huth     CPU_DoubleU u;
1462fcf5ef2aSThomas Huth 
1463fcf5ef2aSThomas Huth     u.ll = val;
1464fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1465fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1466fcf5ef2aSThomas Huth         return 0;
1467fcf5ef2aSThomas Huth     }
1468fcf5ef2aSThomas Huth 
1469fcf5ef2aSThomas Huth     return float64_to_uint32(u.d, &env->vec_status);
1470fcf5ef2aSThomas Huth }
1471fcf5ef2aSThomas Huth 
1472fcf5ef2aSThomas Huth uint32_t helper_efdctsiz(CPUPPCState *env, uint64_t val)
1473fcf5ef2aSThomas Huth {
1474fcf5ef2aSThomas Huth     CPU_DoubleU u;
1475fcf5ef2aSThomas Huth 
1476fcf5ef2aSThomas Huth     u.ll = val;
1477fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1478fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1479fcf5ef2aSThomas Huth         return 0;
1480fcf5ef2aSThomas Huth     }
1481fcf5ef2aSThomas Huth 
1482fcf5ef2aSThomas Huth     return float64_to_int32_round_to_zero(u.d, &env->vec_status);
1483fcf5ef2aSThomas Huth }
1484fcf5ef2aSThomas Huth 
1485fcf5ef2aSThomas Huth uint64_t helper_efdctsidz(CPUPPCState *env, uint64_t val)
1486fcf5ef2aSThomas Huth {
1487fcf5ef2aSThomas Huth     CPU_DoubleU u;
1488fcf5ef2aSThomas Huth 
1489fcf5ef2aSThomas Huth     u.ll = val;
1490fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1491fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1492fcf5ef2aSThomas Huth         return 0;
1493fcf5ef2aSThomas Huth     }
1494fcf5ef2aSThomas Huth 
1495fcf5ef2aSThomas Huth     return float64_to_int64_round_to_zero(u.d, &env->vec_status);
1496fcf5ef2aSThomas Huth }
1497fcf5ef2aSThomas Huth 
1498fcf5ef2aSThomas Huth uint32_t helper_efdctuiz(CPUPPCState *env, uint64_t val)
1499fcf5ef2aSThomas Huth {
1500fcf5ef2aSThomas Huth     CPU_DoubleU u;
1501fcf5ef2aSThomas Huth 
1502fcf5ef2aSThomas Huth     u.ll = val;
1503fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1504fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1505fcf5ef2aSThomas Huth         return 0;
1506fcf5ef2aSThomas Huth     }
1507fcf5ef2aSThomas Huth 
1508fcf5ef2aSThomas Huth     return float64_to_uint32_round_to_zero(u.d, &env->vec_status);
1509fcf5ef2aSThomas Huth }
1510fcf5ef2aSThomas Huth 
1511fcf5ef2aSThomas Huth uint64_t helper_efdctuidz(CPUPPCState *env, uint64_t val)
1512fcf5ef2aSThomas Huth {
1513fcf5ef2aSThomas Huth     CPU_DoubleU u;
1514fcf5ef2aSThomas Huth 
1515fcf5ef2aSThomas Huth     u.ll = val;
1516fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1517fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1518fcf5ef2aSThomas Huth         return 0;
1519fcf5ef2aSThomas Huth     }
1520fcf5ef2aSThomas Huth 
1521fcf5ef2aSThomas Huth     return float64_to_uint64_round_to_zero(u.d, &env->vec_status);
1522fcf5ef2aSThomas Huth }
1523fcf5ef2aSThomas Huth 
1524fcf5ef2aSThomas Huth uint64_t helper_efdcfsf(CPUPPCState *env, uint32_t val)
1525fcf5ef2aSThomas Huth {
1526fcf5ef2aSThomas Huth     CPU_DoubleU u;
1527fcf5ef2aSThomas Huth     float64 tmp;
1528fcf5ef2aSThomas Huth 
1529fcf5ef2aSThomas Huth     u.d = int32_to_float64(val, &env->vec_status);
1530fcf5ef2aSThomas Huth     tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1531fcf5ef2aSThomas Huth     u.d = float64_div(u.d, tmp, &env->vec_status);
1532fcf5ef2aSThomas Huth 
1533fcf5ef2aSThomas Huth     return u.ll;
1534fcf5ef2aSThomas Huth }
1535fcf5ef2aSThomas Huth 
1536fcf5ef2aSThomas Huth uint64_t helper_efdcfuf(CPUPPCState *env, uint32_t val)
1537fcf5ef2aSThomas Huth {
1538fcf5ef2aSThomas Huth     CPU_DoubleU u;
1539fcf5ef2aSThomas Huth     float64 tmp;
1540fcf5ef2aSThomas Huth 
1541fcf5ef2aSThomas Huth     u.d = uint32_to_float64(val, &env->vec_status);
1542fcf5ef2aSThomas Huth     tmp = int64_to_float64(1ULL << 32, &env->vec_status);
1543fcf5ef2aSThomas Huth     u.d = float64_div(u.d, tmp, &env->vec_status);
1544fcf5ef2aSThomas Huth 
1545fcf5ef2aSThomas Huth     return u.ll;
1546fcf5ef2aSThomas Huth }
1547fcf5ef2aSThomas Huth 
1548fcf5ef2aSThomas Huth uint32_t helper_efdctsf(CPUPPCState *env, uint64_t val)
1549fcf5ef2aSThomas Huth {
1550fcf5ef2aSThomas Huth     CPU_DoubleU u;
1551fcf5ef2aSThomas Huth     float64 tmp;
1552fcf5ef2aSThomas Huth 
1553fcf5ef2aSThomas Huth     u.ll = val;
1554fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1555fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1556fcf5ef2aSThomas Huth         return 0;
1557fcf5ef2aSThomas Huth     }
1558fcf5ef2aSThomas Huth     tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1559fcf5ef2aSThomas Huth     u.d = float64_mul(u.d, tmp, &env->vec_status);
1560fcf5ef2aSThomas Huth 
1561fcf5ef2aSThomas Huth     return float64_to_int32(u.d, &env->vec_status);
1562fcf5ef2aSThomas Huth }
1563fcf5ef2aSThomas Huth 
1564fcf5ef2aSThomas Huth uint32_t helper_efdctuf(CPUPPCState *env, uint64_t val)
1565fcf5ef2aSThomas Huth {
1566fcf5ef2aSThomas Huth     CPU_DoubleU u;
1567fcf5ef2aSThomas Huth     float64 tmp;
1568fcf5ef2aSThomas Huth 
1569fcf5ef2aSThomas Huth     u.ll = val;
1570fcf5ef2aSThomas Huth     /* NaN are not treated the same way IEEE 754 does */
1571fcf5ef2aSThomas Huth     if (unlikely(float64_is_any_nan(u.d))) {
1572fcf5ef2aSThomas Huth         return 0;
1573fcf5ef2aSThomas Huth     }
1574fcf5ef2aSThomas Huth     tmp = uint64_to_float64(1ULL << 32, &env->vec_status);
1575fcf5ef2aSThomas Huth     u.d = float64_mul(u.d, tmp, &env->vec_status);
1576fcf5ef2aSThomas Huth 
1577fcf5ef2aSThomas Huth     return float64_to_uint32(u.d, &env->vec_status);
1578fcf5ef2aSThomas Huth }
1579fcf5ef2aSThomas Huth 
1580fcf5ef2aSThomas Huth uint32_t helper_efscfd(CPUPPCState *env, uint64_t val)
1581fcf5ef2aSThomas Huth {
1582fcf5ef2aSThomas Huth     CPU_DoubleU u1;
1583fcf5ef2aSThomas Huth     CPU_FloatU u2;
1584fcf5ef2aSThomas Huth 
1585fcf5ef2aSThomas Huth     u1.ll = val;
1586fcf5ef2aSThomas Huth     u2.f = float64_to_float32(u1.d, &env->vec_status);
1587fcf5ef2aSThomas Huth 
1588fcf5ef2aSThomas Huth     return u2.l;
1589fcf5ef2aSThomas Huth }
1590fcf5ef2aSThomas Huth 
1591fcf5ef2aSThomas Huth uint64_t helper_efdcfs(CPUPPCState *env, uint32_t val)
1592fcf5ef2aSThomas Huth {
1593fcf5ef2aSThomas Huth     CPU_DoubleU u2;
1594fcf5ef2aSThomas Huth     CPU_FloatU u1;
1595fcf5ef2aSThomas Huth 
1596fcf5ef2aSThomas Huth     u1.l = val;
1597fcf5ef2aSThomas Huth     u2.d = float32_to_float64(u1.f, &env->vec_status);
1598fcf5ef2aSThomas Huth 
1599fcf5ef2aSThomas Huth     return u2.ll;
1600fcf5ef2aSThomas Huth }
1601fcf5ef2aSThomas Huth 
1602fcf5ef2aSThomas Huth /* Double precision fixed-point arithmetic */
1603fcf5ef2aSThomas Huth uint64_t helper_efdadd(CPUPPCState *env, uint64_t op1, uint64_t op2)
1604fcf5ef2aSThomas Huth {
1605fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1606fcf5ef2aSThomas Huth 
1607fcf5ef2aSThomas Huth     u1.ll = op1;
1608fcf5ef2aSThomas Huth     u2.ll = op2;
1609fcf5ef2aSThomas Huth     u1.d = float64_add(u1.d, u2.d, &env->vec_status);
1610fcf5ef2aSThomas Huth     return u1.ll;
1611fcf5ef2aSThomas Huth }
1612fcf5ef2aSThomas Huth 
1613fcf5ef2aSThomas Huth uint64_t helper_efdsub(CPUPPCState *env, uint64_t op1, uint64_t op2)
1614fcf5ef2aSThomas Huth {
1615fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1616fcf5ef2aSThomas Huth 
1617fcf5ef2aSThomas Huth     u1.ll = op1;
1618fcf5ef2aSThomas Huth     u2.ll = op2;
1619fcf5ef2aSThomas Huth     u1.d = float64_sub(u1.d, u2.d, &env->vec_status);
1620fcf5ef2aSThomas Huth     return u1.ll;
1621fcf5ef2aSThomas Huth }
1622fcf5ef2aSThomas Huth 
1623fcf5ef2aSThomas Huth uint64_t helper_efdmul(CPUPPCState *env, uint64_t op1, uint64_t op2)
1624fcf5ef2aSThomas Huth {
1625fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1626fcf5ef2aSThomas Huth 
1627fcf5ef2aSThomas Huth     u1.ll = op1;
1628fcf5ef2aSThomas Huth     u2.ll = op2;
1629fcf5ef2aSThomas Huth     u1.d = float64_mul(u1.d, u2.d, &env->vec_status);
1630fcf5ef2aSThomas Huth     return u1.ll;
1631fcf5ef2aSThomas Huth }
1632fcf5ef2aSThomas Huth 
1633fcf5ef2aSThomas Huth uint64_t helper_efddiv(CPUPPCState *env, uint64_t op1, uint64_t op2)
1634fcf5ef2aSThomas Huth {
1635fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1636fcf5ef2aSThomas Huth 
1637fcf5ef2aSThomas Huth     u1.ll = op1;
1638fcf5ef2aSThomas Huth     u2.ll = op2;
1639fcf5ef2aSThomas Huth     u1.d = float64_div(u1.d, u2.d, &env->vec_status);
1640fcf5ef2aSThomas Huth     return u1.ll;
1641fcf5ef2aSThomas Huth }
1642fcf5ef2aSThomas Huth 
1643fcf5ef2aSThomas Huth /* Double precision floating point helpers */
1644fcf5ef2aSThomas Huth uint32_t helper_efdtstlt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1645fcf5ef2aSThomas Huth {
1646fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1647fcf5ef2aSThomas Huth 
1648fcf5ef2aSThomas Huth     u1.ll = op1;
1649fcf5ef2aSThomas Huth     u2.ll = op2;
1650fcf5ef2aSThomas Huth     return float64_lt(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1651fcf5ef2aSThomas Huth }
1652fcf5ef2aSThomas Huth 
1653fcf5ef2aSThomas Huth uint32_t helper_efdtstgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1654fcf5ef2aSThomas Huth {
1655fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1656fcf5ef2aSThomas Huth 
1657fcf5ef2aSThomas Huth     u1.ll = op1;
1658fcf5ef2aSThomas Huth     u2.ll = op2;
1659fcf5ef2aSThomas Huth     return float64_le(u1.d, u2.d, &env->vec_status) ? 0 : 4;
1660fcf5ef2aSThomas Huth }
1661fcf5ef2aSThomas Huth 
1662fcf5ef2aSThomas Huth uint32_t helper_efdtsteq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1663fcf5ef2aSThomas Huth {
1664fcf5ef2aSThomas Huth     CPU_DoubleU u1, u2;
1665fcf5ef2aSThomas Huth 
1666fcf5ef2aSThomas Huth     u1.ll = op1;
1667fcf5ef2aSThomas Huth     u2.ll = op2;
1668fcf5ef2aSThomas Huth     return float64_eq_quiet(u1.d, u2.d, &env->vec_status) ? 4 : 0;
1669fcf5ef2aSThomas Huth }
1670fcf5ef2aSThomas Huth 
1671fcf5ef2aSThomas Huth uint32_t helper_efdcmplt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1672fcf5ef2aSThomas Huth {
1673fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1674fcf5ef2aSThomas Huth     return helper_efdtstlt(env, op1, op2);
1675fcf5ef2aSThomas Huth }
1676fcf5ef2aSThomas Huth 
1677fcf5ef2aSThomas Huth uint32_t helper_efdcmpgt(CPUPPCState *env, uint64_t op1, uint64_t op2)
1678fcf5ef2aSThomas Huth {
1679fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1680fcf5ef2aSThomas Huth     return helper_efdtstgt(env, op1, op2);
1681fcf5ef2aSThomas Huth }
1682fcf5ef2aSThomas Huth 
1683fcf5ef2aSThomas Huth uint32_t helper_efdcmpeq(CPUPPCState *env, uint64_t op1, uint64_t op2)
1684fcf5ef2aSThomas Huth {
1685fcf5ef2aSThomas Huth     /* XXX: TODO: test special values (NaN, infinites, ...) */
1686fcf5ef2aSThomas Huth     return helper_efdtsteq(env, op1, op2);
1687fcf5ef2aSThomas Huth }
1688fcf5ef2aSThomas Huth 
1689fcf5ef2aSThomas Huth #define float64_to_float64(x, env) x
1690fcf5ef2aSThomas Huth 
1691fcf5ef2aSThomas Huth 
1692fa9ebf8cSDavid Gibson /*
1693136fbf65Szhaolichang  * VSX_ADD_SUB - VSX floating point add/subtract
1694fcf5ef2aSThomas Huth  *   name  - instruction mnemonic
1695fcf5ef2aSThomas Huth  *   op    - operation (add or sub)
1696fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1697fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1698fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1699dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
1700fcf5ef2aSThomas Huth  */
1701dd657a35SVíctor Colombo #define VSX_ADD_SUB(name, op, nels, tp, fld, sfifprf, r2sp)                  \
170299125c74SMark Cave-Ayland void helper_##name(CPUPPCState *env, ppc_vsr_t *xt,                          \
170399125c74SMark Cave-Ayland                    ppc_vsr_t *xa, ppc_vsr_t *xb)                             \
1704fcf5ef2aSThomas Huth {                                                                            \
1705205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                       \
1706fcf5ef2aSThomas Huth     int i;                                                                   \
1707fcf5ef2aSThomas Huth                                                                              \
1708fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1709fcf5ef2aSThomas Huth                                                                              \
1710fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1711fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1712fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1713cf3b0334SMark Cave-Ayland         t.fld = tp##_##op(xa->fld, xb->fld, &tstat);                         \
1714fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1715fcf5ef2aSThomas Huth                                                                              \
1716fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
1717941298ecSRichard Henderson             float_invalid_op_addsub(env, tstat.float_exception_flags,        \
1718dd657a35SVíctor Colombo                                     sfifprf, GETPC());                       \
1719fcf5ef2aSThomas Huth         }                                                                    \
1720fcf5ef2aSThomas Huth                                                                              \
1721fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
17227238e55bSRichard Henderson             t.fld = do_frsp(env, t.fld, GETPC());                            \
1723fcf5ef2aSThomas Huth         }                                                                    \
1724fcf5ef2aSThomas Huth                                                                              \
1725dd657a35SVíctor Colombo         if (sfifprf) {                                                       \
1726cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1727fcf5ef2aSThomas Huth         }                                                                    \
1728fcf5ef2aSThomas Huth     }                                                                        \
1729cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
1730dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                            \
1731fcf5ef2aSThomas Huth }
1732fcf5ef2aSThomas Huth 
1733fcf5ef2aSThomas Huth VSX_ADD_SUB(xsadddp, add, 1, float64, VsrD(0), 1, 0)
1734fcf5ef2aSThomas Huth VSX_ADD_SUB(xsaddsp, add, 1, float64, VsrD(0), 1, 1)
1735fcf5ef2aSThomas Huth VSX_ADD_SUB(xvadddp, add, 2, float64, VsrD(i), 0, 0)
1736fcf5ef2aSThomas Huth VSX_ADD_SUB(xvaddsp, add, 4, float32, VsrW(i), 0, 0)
1737fcf5ef2aSThomas Huth VSX_ADD_SUB(xssubdp, sub, 1, float64, VsrD(0), 1, 0)
1738fcf5ef2aSThomas Huth VSX_ADD_SUB(xssubsp, sub, 1, float64, VsrD(0), 1, 1)
1739fcf5ef2aSThomas Huth VSX_ADD_SUB(xvsubdp, sub, 2, float64, VsrD(i), 0, 0)
1740fcf5ef2aSThomas Huth VSX_ADD_SUB(xvsubsp, sub, 4, float32, VsrW(i), 0, 0)
1741fcf5ef2aSThomas Huth 
174223d0766bSMark Cave-Ayland void helper_xsaddqp(CPUPPCState *env, uint32_t opcode,
174323d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
174407bdd247SBharata B Rao {
1745cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
174607bdd247SBharata B Rao     float_status tstat;
174707bdd247SBharata B Rao 
174807bdd247SBharata B Rao     helper_reset_fpstatus(env);
174907bdd247SBharata B Rao 
1750a8d411abSBharata B Rao     tstat = env->fp_status;
175107bdd247SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1752a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
175307bdd247SBharata B Rao     }
175407bdd247SBharata B Rao 
175507bdd247SBharata B Rao     set_float_exception_flags(0, &tstat);
1756cf3b0334SMark Cave-Ayland     t.f128 = float128_add(xa->f128, xb->f128, &tstat);
175707bdd247SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
175807bdd247SBharata B Rao 
175907bdd247SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
1760941298ecSRichard Henderson         float_invalid_op_addsub(env, tstat.float_exception_flags, 1, GETPC());
176107bdd247SBharata B Rao     }
176207bdd247SBharata B Rao 
1763cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
176407bdd247SBharata B Rao 
1765cf3b0334SMark Cave-Ayland     *xt = t;
17663278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
176707bdd247SBharata B Rao }
176807bdd247SBharata B Rao 
1769fa9ebf8cSDavid Gibson /*
1770fa9ebf8cSDavid Gibson  * VSX_MUL - VSX floating point multiply
1771fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1772fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1773fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1774fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1775dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
1776fcf5ef2aSThomas Huth  */
1777dd657a35SVíctor Colombo #define VSX_MUL(op, nels, tp, fld, sfifprf, r2sp)                            \
177899125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                            \
177999125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                               \
1780fcf5ef2aSThomas Huth {                                                                            \
1781205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                       \
1782fcf5ef2aSThomas Huth     int i;                                                                   \
1783fcf5ef2aSThomas Huth                                                                              \
1784fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1785fcf5ef2aSThomas Huth                                                                              \
1786fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1787fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1788fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1789cf3b0334SMark Cave-Ayland         t.fld = tp##_mul(xa->fld, xb->fld, &tstat);                          \
1790fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1791fcf5ef2aSThomas Huth                                                                              \
1792fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
17934edf5569SRichard Henderson             float_invalid_op_mul(env, tstat.float_exception_flags,           \
1794dd657a35SVíctor Colombo                                  sfifprf, GETPC());                          \
1795fcf5ef2aSThomas Huth         }                                                                    \
1796fcf5ef2aSThomas Huth                                                                              \
1797fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
17987238e55bSRichard Henderson             t.fld = do_frsp(env, t.fld, GETPC());                            \
1799fcf5ef2aSThomas Huth         }                                                                    \
1800fcf5ef2aSThomas Huth                                                                              \
1801dd657a35SVíctor Colombo         if (sfifprf) {                                                       \
1802cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1803fcf5ef2aSThomas Huth         }                                                                    \
1804fcf5ef2aSThomas Huth     }                                                                        \
1805fcf5ef2aSThomas Huth                                                                              \
1806cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
1807dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                            \
1808fcf5ef2aSThomas Huth }
1809fcf5ef2aSThomas Huth 
1810fcf5ef2aSThomas Huth VSX_MUL(xsmuldp, 1, float64, VsrD(0), 1, 0)
1811fcf5ef2aSThomas Huth VSX_MUL(xsmulsp, 1, float64, VsrD(0), 1, 1)
1812fcf5ef2aSThomas Huth VSX_MUL(xvmuldp, 2, float64, VsrD(i), 0, 0)
1813fcf5ef2aSThomas Huth VSX_MUL(xvmulsp, 4, float32, VsrW(i), 0, 0)
1814fcf5ef2aSThomas Huth 
181523d0766bSMark Cave-Ayland void helper_xsmulqp(CPUPPCState *env, uint32_t opcode,
181623d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
1817a811ec04SBharata B Rao {
1818cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
1819a8d411abSBharata B Rao     float_status tstat;
1820a811ec04SBharata B Rao 
1821a8d411abSBharata B Rao     helper_reset_fpstatus(env);
1822a8d411abSBharata B Rao     tstat = env->fp_status;
1823a811ec04SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1824a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
1825a811ec04SBharata B Rao     }
1826a811ec04SBharata B Rao 
1827a811ec04SBharata B Rao     set_float_exception_flags(0, &tstat);
1828cf3b0334SMark Cave-Ayland     t.f128 = float128_mul(xa->f128, xb->f128, &tstat);
1829a811ec04SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1830a811ec04SBharata B Rao 
1831a811ec04SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
18324edf5569SRichard Henderson         float_invalid_op_mul(env, tstat.float_exception_flags, 1, GETPC());
1833a811ec04SBharata B Rao     }
1834cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
1835a811ec04SBharata B Rao 
1836cf3b0334SMark Cave-Ayland     *xt = t;
18373278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
1838a811ec04SBharata B Rao }
1839a811ec04SBharata B Rao 
1840fa9ebf8cSDavid Gibson /*
1841fa9ebf8cSDavid Gibson  * VSX_DIV - VSX floating point divide
1842fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1843fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1844fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1845fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1846dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
1847fcf5ef2aSThomas Huth  */
1848dd657a35SVíctor Colombo #define VSX_DIV(op, nels, tp, fld, sfifprf, r2sp)                             \
184999125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
185099125c74SMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                                \
1851fcf5ef2aSThomas Huth {                                                                             \
1852205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                        \
1853fcf5ef2aSThomas Huth     int i;                                                                    \
1854fcf5ef2aSThomas Huth                                                                               \
1855fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
1856fcf5ef2aSThomas Huth                                                                               \
1857fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
1858fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                  \
1859fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                 \
1860cf3b0334SMark Cave-Ayland         t.fld = tp##_div(xa->fld, xb->fld, &tstat);                           \
1861fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags;  \
1862fcf5ef2aSThomas Huth                                                                               \
1863fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {     \
1864c07f8241SRichard Henderson             float_invalid_op_div(env, tstat.float_exception_flags,            \
1865dd657a35SVíctor Colombo                                  sfifprf, GETPC());                           \
1866fcf5ef2aSThomas Huth         }                                                                     \
1867ae13018dSRichard Henderson         if (unlikely(tstat.float_exception_flags & float_flag_divbyzero)) {   \
1868ae13018dSRichard Henderson             float_zero_divide_excp(env, GETPC());                             \
1869ae13018dSRichard Henderson         }                                                                     \
1870fcf5ef2aSThomas Huth                                                                               \
1871fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
18727238e55bSRichard Henderson             t.fld = do_frsp(env, t.fld, GETPC());                             \
1873fcf5ef2aSThomas Huth         }                                                                     \
1874fcf5ef2aSThomas Huth                                                                               \
1875dd657a35SVíctor Colombo         if (sfifprf) {                                                        \
1876cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
1877fcf5ef2aSThomas Huth         }                                                                     \
1878fcf5ef2aSThomas Huth     }                                                                         \
1879fcf5ef2aSThomas Huth                                                                               \
1880cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
1881dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                             \
1882fcf5ef2aSThomas Huth }
1883fcf5ef2aSThomas Huth 
1884fcf5ef2aSThomas Huth VSX_DIV(xsdivdp, 1, float64, VsrD(0), 1, 0)
1885fcf5ef2aSThomas Huth VSX_DIV(xsdivsp, 1, float64, VsrD(0), 1, 1)
1886fcf5ef2aSThomas Huth VSX_DIV(xvdivdp, 2, float64, VsrD(i), 0, 0)
1887fcf5ef2aSThomas Huth VSX_DIV(xvdivsp, 4, float32, VsrW(i), 0, 0)
1888fcf5ef2aSThomas Huth 
188923d0766bSMark Cave-Ayland void helper_xsdivqp(CPUPPCState *env, uint32_t opcode,
189023d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
1891314c1163SBharata B Rao {
1892cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
1893a8d411abSBharata B Rao     float_status tstat;
1894314c1163SBharata B Rao 
1895a8d411abSBharata B Rao     helper_reset_fpstatus(env);
1896a8d411abSBharata B Rao     tstat = env->fp_status;
1897314c1163SBharata B Rao     if (unlikely(Rc(opcode) != 0)) {
1898a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
1899314c1163SBharata B Rao     }
1900314c1163SBharata B Rao 
1901314c1163SBharata B Rao     set_float_exception_flags(0, &tstat);
1902cf3b0334SMark Cave-Ayland     t.f128 = float128_div(xa->f128, xb->f128, &tstat);
1903314c1163SBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
1904314c1163SBharata B Rao 
1905314c1163SBharata B Rao     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
1906c07f8241SRichard Henderson         float_invalid_op_div(env, tstat.float_exception_flags, 1, GETPC());
1907314c1163SBharata B Rao     }
1908ae13018dSRichard Henderson     if (unlikely(tstat.float_exception_flags & float_flag_divbyzero)) {
1909ae13018dSRichard Henderson         float_zero_divide_excp(env, GETPC());
1910ae13018dSRichard Henderson     }
1911314c1163SBharata B Rao 
1912cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
1913cf3b0334SMark Cave-Ayland     *xt = t;
19143278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
1915314c1163SBharata B Rao }
1916314c1163SBharata B Rao 
1917fa9ebf8cSDavid Gibson /*
1918fa9ebf8cSDavid Gibson  * VSX_RE  - VSX floating point reciprocal estimate
1919fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1920fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1921fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1922fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1923dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
1924fcf5ef2aSThomas Huth  */
1925dd657a35SVíctor Colombo #define VSX_RE(op, nels, tp, fld, sfifprf, r2sp)                              \
192675cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)              \
1927fcf5ef2aSThomas Huth {                                                                             \
1928205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                        \
1929fcf5ef2aSThomas Huth     int i;                                                                    \
1930fcf5ef2aSThomas Huth                                                                               \
1931fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
1932fcf5ef2aSThomas Huth                                                                               \
1933fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
1934cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xb->fld, &env->fp_status))) {      \
193513c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
1936fcf5ef2aSThomas Huth         }                                                                     \
1937cf3b0334SMark Cave-Ayland         t.fld = tp##_div(tp##_one, xb->fld, &env->fp_status);                 \
1938fcf5ef2aSThomas Huth                                                                               \
1939fcf5ef2aSThomas Huth         if (r2sp) {                                                           \
19407238e55bSRichard Henderson             t.fld = do_frsp(env, t.fld, GETPC());                             \
1941fcf5ef2aSThomas Huth         }                                                                     \
1942fcf5ef2aSThomas Huth                                                                               \
1943dd657a35SVíctor Colombo         if (sfifprf) {                                                        \
1944cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
1945fcf5ef2aSThomas Huth         }                                                                     \
1946fcf5ef2aSThomas Huth     }                                                                         \
1947fcf5ef2aSThomas Huth                                                                               \
1948cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
1949dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                             \
1950fcf5ef2aSThomas Huth }
1951fcf5ef2aSThomas Huth 
1952fcf5ef2aSThomas Huth VSX_RE(xsredp, 1, float64, VsrD(0), 1, 0)
1953fcf5ef2aSThomas Huth VSX_RE(xsresp, 1, float64, VsrD(0), 1, 1)
1954fcf5ef2aSThomas Huth VSX_RE(xvredp, 2, float64, VsrD(i), 0, 0)
1955fcf5ef2aSThomas Huth VSX_RE(xvresp, 4, float32, VsrW(i), 0, 0)
1956fcf5ef2aSThomas Huth 
1957fa9ebf8cSDavid Gibson /*
1958fa9ebf8cSDavid Gibson  * VSX_SQRT - VSX floating point square root
1959fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
1960fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
1961fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
1962fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
1963dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
1964fcf5ef2aSThomas Huth  */
1965dd657a35SVíctor Colombo #define VSX_SQRT(op, nels, tp, fld, sfifprf, r2sp)                           \
196675cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
1967fcf5ef2aSThomas Huth {                                                                            \
1968205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                       \
1969fcf5ef2aSThomas Huth     int i;                                                                   \
1970fcf5ef2aSThomas Huth                                                                              \
1971fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
1972fcf5ef2aSThomas Huth                                                                              \
1973fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
1974fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
1975fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
1976cf3b0334SMark Cave-Ayland         t.fld = tp##_sqrt(xb->fld, &tstat);                                  \
1977fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
1978fcf5ef2aSThomas Huth                                                                              \
1979fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
19803d3050ccSRichard Henderson             float_invalid_op_sqrt(env, tstat.float_exception_flags,          \
1981dd657a35SVíctor Colombo                                   sfifprf, GETPC());                         \
1982fcf5ef2aSThomas Huth         }                                                                    \
1983fcf5ef2aSThomas Huth                                                                              \
1984fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
19857238e55bSRichard Henderson             t.fld = do_frsp(env, t.fld, GETPC());                            \
1986fcf5ef2aSThomas Huth         }                                                                    \
1987fcf5ef2aSThomas Huth                                                                              \
1988dd657a35SVíctor Colombo         if (sfifprf) {                                                       \
1989cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
1990fcf5ef2aSThomas Huth         }                                                                    \
1991fcf5ef2aSThomas Huth     }                                                                        \
1992fcf5ef2aSThomas Huth                                                                              \
1993cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
1994dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                            \
1995fcf5ef2aSThomas Huth }
1996fcf5ef2aSThomas Huth 
1997fcf5ef2aSThomas Huth VSX_SQRT(xssqrtdp, 1, float64, VsrD(0), 1, 0)
1998fcf5ef2aSThomas Huth VSX_SQRT(xssqrtsp, 1, float64, VsrD(0), 1, 1)
1999fcf5ef2aSThomas Huth VSX_SQRT(xvsqrtdp, 2, float64, VsrD(i), 0, 0)
2000fcf5ef2aSThomas Huth VSX_SQRT(xvsqrtsp, 4, float32, VsrW(i), 0, 0)
2001fcf5ef2aSThomas Huth 
2002fa9ebf8cSDavid Gibson /*
2003fa9ebf8cSDavid Gibson  *VSX_RSQRTE - VSX floating point reciprocal square root estimate
2004fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2005fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2006fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2007fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2008dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
2009fcf5ef2aSThomas Huth  */
2010dd657a35SVíctor Colombo #define VSX_RSQRTE(op, nels, tp, fld, sfifprf, r2sp)                         \
201175cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2012fcf5ef2aSThomas Huth {                                                                            \
2013205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                       \
2014fcf5ef2aSThomas Huth     int i;                                                                   \
2015fcf5ef2aSThomas Huth                                                                              \
2016fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                              \
2017fcf5ef2aSThomas Huth                                                                              \
2018fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
2019fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                 \
2020fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                \
2021cf3b0334SMark Cave-Ayland         t.fld = tp##_sqrt(xb->fld, &tstat);                                  \
2022cf3b0334SMark Cave-Ayland         t.fld = tp##_div(tp##_one, t.fld, &tstat);                           \
2023fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags; \
2024fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {    \
20253d3050ccSRichard Henderson             float_invalid_op_sqrt(env, tstat.float_exception_flags,          \
2026dd657a35SVíctor Colombo                                   sfifprf, GETPC());                         \
2027fcf5ef2aSThomas Huth         }                                                                    \
2028fcf5ef2aSThomas Huth         if (r2sp) {                                                          \
20297238e55bSRichard Henderson             t.fld = do_frsp(env, t.fld, GETPC());                            \
2030fcf5ef2aSThomas Huth         }                                                                    \
2031fcf5ef2aSThomas Huth                                                                              \
2032dd657a35SVíctor Colombo         if (sfifprf) {                                                       \
2033cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                         \
2034fcf5ef2aSThomas Huth         }                                                                    \
2035fcf5ef2aSThomas Huth     }                                                                        \
2036fcf5ef2aSThomas Huth                                                                              \
2037cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
2038dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                            \
2039fcf5ef2aSThomas Huth }
2040fcf5ef2aSThomas Huth 
2041fcf5ef2aSThomas Huth VSX_RSQRTE(xsrsqrtedp, 1, float64, VsrD(0), 1, 0)
2042fcf5ef2aSThomas Huth VSX_RSQRTE(xsrsqrtesp, 1, float64, VsrD(0), 1, 1)
2043fcf5ef2aSThomas Huth VSX_RSQRTE(xvrsqrtedp, 2, float64, VsrD(i), 0, 0)
2044fcf5ef2aSThomas Huth VSX_RSQRTE(xvrsqrtesp, 4, float32, VsrW(i), 0, 0)
2045fcf5ef2aSThomas Huth 
2046fa9ebf8cSDavid Gibson /*
2047fa9ebf8cSDavid Gibson  * VSX_TDIV - VSX floating point test for divide
2048fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2049fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2050fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2051fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2052fcf5ef2aSThomas Huth  *   emin  - minimum unbiased exponent
2053fcf5ef2aSThomas Huth  *   emax  - maximum unbiased exponent
2054fcf5ef2aSThomas Huth  *   nbits - number of fraction bits
2055fcf5ef2aSThomas Huth  */
2056fcf5ef2aSThomas Huth #define VSX_TDIV(op, nels, tp, fld, emin, emax, nbits)                  \
2057033e1fcdSMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
2058033e1fcdSMark Cave-Ayland                  ppc_vsr_t *xa, ppc_vsr_t *xb)                          \
2059fcf5ef2aSThomas Huth {                                                                       \
2060fcf5ef2aSThomas Huth     int i;                                                              \
2061fcf5ef2aSThomas Huth     int fe_flag = 0;                                                    \
2062fcf5ef2aSThomas Huth     int fg_flag = 0;                                                    \
2063fcf5ef2aSThomas Huth                                                                         \
2064fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2065cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_infinity(xa->fld) ||                       \
2066cf3b0334SMark Cave-Ayland                      tp##_is_infinity(xb->fld) ||                       \
2067cf3b0334SMark Cave-Ayland                      tp##_is_zero(xb->fld))) {                          \
2068fcf5ef2aSThomas Huth             fe_flag = 1;                                                \
2069fcf5ef2aSThomas Huth             fg_flag = 1;                                                \
2070fcf5ef2aSThomas Huth         } else {                                                        \
2071cf3b0334SMark Cave-Ayland             int e_a = ppc_##tp##_get_unbiased_exp(xa->fld);             \
2072cf3b0334SMark Cave-Ayland             int e_b = ppc_##tp##_get_unbiased_exp(xb->fld);             \
2073fcf5ef2aSThomas Huth                                                                         \
2074cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_any_nan(xa->fld) ||                    \
2075cf3b0334SMark Cave-Ayland                          tp##_is_any_nan(xb->fld))) {                   \
2076fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2077fcf5ef2aSThomas Huth             } else if ((e_b <= emin) || (e_b >= (emax - 2))) {          \
2078fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2079cf3b0334SMark Cave-Ayland             } else if (!tp##_is_zero(xa->fld) &&                        \
2080fcf5ef2aSThomas Huth                        (((e_a - e_b) >= emax) ||                        \
2081fcf5ef2aSThomas Huth                         ((e_a - e_b) <= (emin + 1)) ||                  \
2082fcf5ef2aSThomas Huth                         (e_a <= (emin + nbits)))) {                     \
2083fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2084fcf5ef2aSThomas Huth             }                                                           \
2085fcf5ef2aSThomas Huth                                                                         \
2086cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_zero_or_denormal(xb->fld))) {          \
2087fa9ebf8cSDavid Gibson                 /*                                                      \
2088fa9ebf8cSDavid Gibson                  * XB is not zero because of the above check and so     \
2089fa9ebf8cSDavid Gibson                  * must be denormalized.                                \
2090fa9ebf8cSDavid Gibson                  */                                                     \
2091fcf5ef2aSThomas Huth                 fg_flag = 1;                                            \
2092fcf5ef2aSThomas Huth             }                                                           \
2093fcf5ef2aSThomas Huth         }                                                               \
2094fcf5ef2aSThomas Huth     }                                                                   \
2095fcf5ef2aSThomas Huth                                                                         \
2096fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2097fcf5ef2aSThomas Huth }
2098fcf5ef2aSThomas Huth 
2099fcf5ef2aSThomas Huth VSX_TDIV(xstdivdp, 1, float64, VsrD(0), -1022, 1023, 52)
2100fcf5ef2aSThomas Huth VSX_TDIV(xvtdivdp, 2, float64, VsrD(i), -1022, 1023, 52)
2101fcf5ef2aSThomas Huth VSX_TDIV(xvtdivsp, 4, float32, VsrW(i), -126, 127, 23)
2102fcf5ef2aSThomas Huth 
2103fa9ebf8cSDavid Gibson /*
2104fa9ebf8cSDavid Gibson  * VSX_TSQRT - VSX floating point test for square root
2105fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2106fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2107fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2108fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2109fcf5ef2aSThomas Huth  *   emin  - minimum unbiased exponent
2110fcf5ef2aSThomas Huth  *   emax  - maximum unbiased exponent
2111fcf5ef2aSThomas Huth  *   nbits - number of fraction bits
2112fcf5ef2aSThomas Huth  */
2113fcf5ef2aSThomas Huth #define VSX_TSQRT(op, nels, tp, fld, emin, nbits)                       \
21148d830485SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xb)      \
2115fcf5ef2aSThomas Huth {                                                                       \
2116fcf5ef2aSThomas Huth     int i;                                                              \
2117fcf5ef2aSThomas Huth     int fe_flag = 0;                                                    \
2118fcf5ef2aSThomas Huth     int fg_flag = 0;                                                    \
2119fcf5ef2aSThomas Huth                                                                         \
2120fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
2121cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_infinity(xb->fld) ||                       \
2122cf3b0334SMark Cave-Ayland                      tp##_is_zero(xb->fld))) {                          \
2123fcf5ef2aSThomas Huth             fe_flag = 1;                                                \
2124fcf5ef2aSThomas Huth             fg_flag = 1;                                                \
2125fcf5ef2aSThomas Huth         } else {                                                        \
2126cf3b0334SMark Cave-Ayland             int e_b = ppc_##tp##_get_unbiased_exp(xb->fld);             \
2127fcf5ef2aSThomas Huth                                                                         \
2128cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_any_nan(xb->fld))) {                   \
2129fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2130cf3b0334SMark Cave-Ayland             } else if (unlikely(tp##_is_zero(xb->fld))) {               \
2131fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2132cf3b0334SMark Cave-Ayland             } else if (unlikely(tp##_is_neg(xb->fld))) {                \
2133fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2134cf3b0334SMark Cave-Ayland             } else if (!tp##_is_zero(xb->fld) &&                        \
2135fcf5ef2aSThomas Huth                        (e_b <= (emin + nbits))) {                       \
2136fcf5ef2aSThomas Huth                 fe_flag = 1;                                            \
2137fcf5ef2aSThomas Huth             }                                                           \
2138fcf5ef2aSThomas Huth                                                                         \
2139cf3b0334SMark Cave-Ayland             if (unlikely(tp##_is_zero_or_denormal(xb->fld))) {          \
2140fa9ebf8cSDavid Gibson                 /*                                                      \
2141fa9ebf8cSDavid Gibson                  * XB is not zero because of the above check and        \
2142fa9ebf8cSDavid Gibson                  * therefore must be denormalized.                      \
2143fa9ebf8cSDavid Gibson                  */                                                     \
2144fcf5ef2aSThomas Huth                 fg_flag = 1;                                            \
2145fcf5ef2aSThomas Huth             }                                                           \
2146fcf5ef2aSThomas Huth         }                                                               \
2147fcf5ef2aSThomas Huth     }                                                                   \
2148fcf5ef2aSThomas Huth                                                                         \
2149fcf5ef2aSThomas Huth     env->crf[BF(opcode)] = 0x8 | (fg_flag ? 4 : 0) | (fe_flag ? 2 : 0); \
2150fcf5ef2aSThomas Huth }
2151fcf5ef2aSThomas Huth 
2152fcf5ef2aSThomas Huth VSX_TSQRT(xstsqrtdp, 1, float64, VsrD(0), -1022, 52)
2153fcf5ef2aSThomas Huth VSX_TSQRT(xvtsqrtdp, 2, float64, VsrD(i), -1022, 52)
2154fcf5ef2aSThomas Huth VSX_TSQRT(xvtsqrtsp, 4, float32, VsrW(i), -126, 23)
2155fcf5ef2aSThomas Huth 
2156fa9ebf8cSDavid Gibson /*
2157fa9ebf8cSDavid Gibson  * VSX_MADD - VSX floating point muliply/add variations
2158fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2159fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2160fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2161fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2162fcf5ef2aSThomas Huth  *   maddflgs - flags for the float*muladd routine that control the
2163fcf5ef2aSThomas Huth  *           various forms (madd, msub, nmadd, nmsub)
2164dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
2165fcf5ef2aSThomas Huth  */
2166dd657a35SVíctor Colombo #define VSX_MADD(op, nels, tp, fld, maddflgs, sfifprf)                        \
216799125c74SMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                             \
2168e4318ab2SMatheus Ferst                  ppc_vsr_t *s1, ppc_vsr_t *s2, ppc_vsr_t *s3)                 \
2169fcf5ef2aSThomas Huth {                                                                             \
2170c9f4e4d8SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                        \
2171fcf5ef2aSThomas Huth     int i;                                                                    \
2172fcf5ef2aSThomas Huth                                                                               \
2173fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);                                               \
2174fcf5ef2aSThomas Huth                                                                               \
2175fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2176fcf5ef2aSThomas Huth         float_status tstat = env->fp_status;                                  \
2177fcf5ef2aSThomas Huth         set_float_exception_flags(0, &tstat);                                 \
21784e4b5a3eSMatheus Ferst         t.fld = tp##_muladd(s1->fld, s3->fld, s2->fld, maddflgs, &tstat);     \
2179fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags |= tstat.float_exception_flags;  \
2180fcf5ef2aSThomas Huth                                                                               \
2181fcf5ef2aSThomas Huth         if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {     \
2182e4052bb7SRichard Henderson             float_invalid_op_madd(env, tstat.float_exception_flags,           \
2183dd657a35SVíctor Colombo                                   sfifprf, GETPC());                          \
2184fcf5ef2aSThomas Huth         }                                                                     \
2185fcf5ef2aSThomas Huth                                                                               \
2186dd657a35SVíctor Colombo         if (sfifprf) {                                                        \
2187cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                          \
2188fcf5ef2aSThomas Huth         }                                                                     \
2189fcf5ef2aSThomas Huth     }                                                                         \
2190cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
2191dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                             \
2192fcf5ef2aSThomas Huth }
2193fcf5ef2aSThomas Huth 
21944e4b5a3eSMatheus Ferst VSX_MADD(XSMADDDP, 1, float64, VsrD(0), MADD_FLGS, 1)
21954e4b5a3eSMatheus Ferst VSX_MADD(XSMSUBDP, 1, float64, VsrD(0), MSUB_FLGS, 1)
21964e4b5a3eSMatheus Ferst VSX_MADD(XSNMADDDP, 1, float64, VsrD(0), NMADD_FLGS, 1)
21974e4b5a3eSMatheus Ferst VSX_MADD(XSNMSUBDP, 1, float64, VsrD(0), NMSUB_FLGS, 1)
21984e4b5a3eSMatheus Ferst VSX_MADD(XSMADDSP, 1, float64r32, VsrD(0), MADD_FLGS, 1)
21994e4b5a3eSMatheus Ferst VSX_MADD(XSMSUBSP, 1, float64r32, VsrD(0), MSUB_FLGS, 1)
22004e4b5a3eSMatheus Ferst VSX_MADD(XSNMADDSP, 1, float64r32, VsrD(0), NMADD_FLGS, 1)
22014e4b5a3eSMatheus Ferst VSX_MADD(XSNMSUBSP, 1, float64r32, VsrD(0), NMSUB_FLGS, 1)
2202fcf5ef2aSThomas Huth 
22034e4b5a3eSMatheus Ferst VSX_MADD(xvmadddp, 2, float64, VsrD(i), MADD_FLGS, 0)
22044e4b5a3eSMatheus Ferst VSX_MADD(xvmsubdp, 2, float64, VsrD(i), MSUB_FLGS, 0)
22054e4b5a3eSMatheus Ferst VSX_MADD(xvnmadddp, 2, float64, VsrD(i), NMADD_FLGS, 0)
22064e4b5a3eSMatheus Ferst VSX_MADD(xvnmsubdp, 2, float64, VsrD(i), NMSUB_FLGS, 0)
2207fcf5ef2aSThomas Huth 
22084e4b5a3eSMatheus Ferst VSX_MADD(xvmaddsp, 4, float32, VsrW(i), MADD_FLGS, 0)
22094e4b5a3eSMatheus Ferst VSX_MADD(xvmsubsp, 4, float32, VsrW(i), MSUB_FLGS, 0)
22104e4b5a3eSMatheus Ferst VSX_MADD(xvnmaddsp, 4, float32, VsrW(i), NMADD_FLGS, 0)
22114e4b5a3eSMatheus Ferst VSX_MADD(xvnmsubsp, 4, float32, VsrW(i), NMSUB_FLGS, 0)
2212fcf5ef2aSThomas Huth 
2213fa9ebf8cSDavid Gibson /*
22143bb1aed2SMatheus Ferst  * VSX_MADDQ - VSX floating point quad-precision muliply/add
22153bb1aed2SMatheus Ferst  *   op    - instruction mnemonic
22163bb1aed2SMatheus Ferst  *   maddflgs - flags for the float*muladd routine that control the
22173bb1aed2SMatheus Ferst  *           various forms (madd, msub, nmadd, nmsub)
22183bb1aed2SMatheus Ferst  *   ro    - round to odd
22193bb1aed2SMatheus Ferst  */
22203bb1aed2SMatheus Ferst #define VSX_MADDQ(op, maddflgs, ro)                                            \
22213bb1aed2SMatheus Ferst void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *s1, ppc_vsr_t *s2,\
22223bb1aed2SMatheus Ferst                  ppc_vsr_t *s3)                                                \
22233bb1aed2SMatheus Ferst {                                                                              \
22243bb1aed2SMatheus Ferst     ppc_vsr_t t = *xt;                                                         \
22253bb1aed2SMatheus Ferst                                                                                \
22263bb1aed2SMatheus Ferst     helper_reset_fpstatus(env);                                                \
22273bb1aed2SMatheus Ferst                                                                                \
22283bb1aed2SMatheus Ferst     float_status tstat = env->fp_status;                                       \
22293bb1aed2SMatheus Ferst     set_float_exception_flags(0, &tstat);                                      \
22303bb1aed2SMatheus Ferst     if (ro) {                                                                  \
22313bb1aed2SMatheus Ferst         tstat.float_rounding_mode = float_round_to_odd;                        \
22323bb1aed2SMatheus Ferst     }                                                                          \
22333bb1aed2SMatheus Ferst     t.f128 = float128_muladd(s1->f128, s3->f128, s2->f128, maddflgs, &tstat);  \
22343bb1aed2SMatheus Ferst     env->fp_status.float_exception_flags |= tstat.float_exception_flags;       \
22353bb1aed2SMatheus Ferst                                                                                \
22363bb1aed2SMatheus Ferst     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {          \
22373bb1aed2SMatheus Ferst         float_invalid_op_madd(env, tstat.float_exception_flags,                \
22383bb1aed2SMatheus Ferst                               false, GETPC());                                 \
22393bb1aed2SMatheus Ferst     }                                                                          \
22403bb1aed2SMatheus Ferst                                                                                \
22413bb1aed2SMatheus Ferst     helper_compute_fprf_float128(env, t.f128);                                 \
22423bb1aed2SMatheus Ferst     *xt = t;                                                                   \
22433278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());                                 \
22443bb1aed2SMatheus Ferst }
22453bb1aed2SMatheus Ferst 
22463bb1aed2SMatheus Ferst VSX_MADDQ(XSMADDQP, MADD_FLGS, 0)
22473bb1aed2SMatheus Ferst VSX_MADDQ(XSMADDQPO, MADD_FLGS, 1)
22483bb1aed2SMatheus Ferst VSX_MADDQ(XSMSUBQP, MSUB_FLGS, 0)
22493bb1aed2SMatheus Ferst VSX_MADDQ(XSMSUBQPO, MSUB_FLGS, 1)
22503bb1aed2SMatheus Ferst VSX_MADDQ(XSNMADDQP, NMADD_FLGS, 0)
22513bb1aed2SMatheus Ferst VSX_MADDQ(XSNMADDQPO, NMADD_FLGS, 1)
22523bb1aed2SMatheus Ferst VSX_MADDQ(XSNMSUBQP, NMSUB_FLGS, 0)
22533bb1aed2SMatheus Ferst VSX_MADDQ(XSNMSUBQPO, NMSUB_FLGS, 0)
22543bb1aed2SMatheus Ferst 
22553bb1aed2SMatheus Ferst /*
22564439586aSVíctor Colombo  * VSX_SCALAR_CMP - VSX scalar floating point compare
2257fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
22584439586aSVíctor Colombo  *   tp    - type
2259fcf5ef2aSThomas Huth  *   cmp   - comparison operation
22604439586aSVíctor Colombo  *   fld   - vsr_t field
2261fcf5ef2aSThomas Huth  *   svxvc - set VXVC bit
2262fcf5ef2aSThomas Huth  */
22634439586aSVíctor Colombo #define VSX_SCALAR_CMP(op, tp, cmp, fld, svxvc)                               \
226499125c74SMark Cave-Ayland         void helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                     \
226599125c74SMark Cave-Ayland                 ppc_vsr_t *xa, ppc_vsr_t *xb)                                 \
2266fcf5ef2aSThomas Huth {                                                                             \
22674439586aSVíctor Colombo     int flags;                                                                \
22684439586aSVíctor Colombo     bool r, vxvc;                                                             \
2269fcf5ef2aSThomas Huth                                                                               \
22704439586aSVíctor Colombo     helper_reset_fpstatus(env);                                               \
22714439586aSVíctor Colombo                                                                               \
22724439586aSVíctor Colombo     if (svxvc) {                                                              \
22734439586aSVíctor Colombo         r = tp##_##cmp(xb->fld, xa->fld, &env->fp_status);                    \
22744439586aSVíctor Colombo     } else {                                                                  \
22754439586aSVíctor Colombo         r = tp##_##cmp##_quiet(xb->fld, xa->fld, &env->fp_status);            \
2276fcf5ef2aSThomas Huth     }                                                                         \
22774439586aSVíctor Colombo                                                                               \
22784439586aSVíctor Colombo     flags = get_float_exception_flags(&env->fp_status);                       \
22794439586aSVíctor Colombo     if (unlikely(flags & float_flag_invalid)) {                               \
22804439586aSVíctor Colombo         vxvc = svxvc;                                                         \
22814439586aSVíctor Colombo         if (flags & float_flag_invalid_snan) {                                \
228213c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
2283208d8033SVíctor Colombo             vxvc &= !(env->fpscr & FP_VE);                                    \
2284fcf5ef2aSThomas Huth         }                                                                     \
22854439586aSVíctor Colombo         if (vxvc) {                                                           \
228613c9115fSRichard Henderson             float_invalid_op_vxvc(env, 0, GETPC());                           \
2287fcf5ef2aSThomas Huth         }                                                                     \
22884439586aSVíctor Colombo     }                                                                         \
2289fcf5ef2aSThomas Huth                                                                               \
22904439586aSVíctor Colombo     memset(xt, 0, sizeof(*xt));                                               \
22914439586aSVíctor Colombo     memset(&xt->fld, -r, sizeof(xt->fld));                                    \
22923278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());                               \
2293fcf5ef2aSThomas Huth }
2294fcf5ef2aSThomas Huth 
229572d24354SVíctor Colombo VSX_SCALAR_CMP(XSCMPEQDP, float64, eq, VsrD(0), 0)
229672d24354SVíctor Colombo VSX_SCALAR_CMP(XSCMPGEDP, float64, le, VsrD(0), 1)
229772d24354SVíctor Colombo VSX_SCALAR_CMP(XSCMPGTDP, float64, lt, VsrD(0), 1)
2298568e7c4dSVíctor Colombo VSX_SCALAR_CMP(XSCMPEQQP, float128, eq, f128, 0)
2299568e7c4dSVíctor Colombo VSX_SCALAR_CMP(XSCMPGEQP, float128, le, f128, 1)
2300568e7c4dSVíctor Colombo VSX_SCALAR_CMP(XSCMPGTQP, float128, lt, f128, 1)
2301fcf5ef2aSThomas Huth 
2302033e1fcdSMark Cave-Ayland void helper_xscmpexpdp(CPUPPCState *env, uint32_t opcode,
2303033e1fcdSMark Cave-Ayland                        ppc_vsr_t *xa, ppc_vsr_t *xb)
23043a20d11dSBharata B Rao {
23053a20d11dSBharata B Rao     int64_t exp_a, exp_b;
23063a20d11dSBharata B Rao     uint32_t cc;
23073a20d11dSBharata B Rao 
2308cf3b0334SMark Cave-Ayland     exp_a = extract64(xa->VsrD(0), 52, 11);
2309cf3b0334SMark Cave-Ayland     exp_b = extract64(xb->VsrD(0), 52, 11);
23103a20d11dSBharata B Rao 
2311cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)) ||
2312cf3b0334SMark Cave-Ayland                  float64_is_any_nan(xb->VsrD(0)))) {
23133a20d11dSBharata B Rao         cc = CRF_SO;
23143a20d11dSBharata B Rao     } else {
23153a20d11dSBharata B Rao         if (exp_a < exp_b) {
23163a20d11dSBharata B Rao             cc = CRF_LT;
23173a20d11dSBharata B Rao         } else if (exp_a > exp_b) {
23183a20d11dSBharata B Rao             cc = CRF_GT;
23193a20d11dSBharata B Rao         } else {
23203a20d11dSBharata B Rao             cc = CRF_EQ;
23213a20d11dSBharata B Rao         }
23223a20d11dSBharata B Rao     }
23233a20d11dSBharata B Rao 
23245c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
23255c94dd38SPaul A. Clarke     env->fpscr |= cc << FPSCR_FPCC;
23263a20d11dSBharata B Rao     env->crf[BF(opcode)] = cc;
23273a20d11dSBharata B Rao 
23283278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());
23293a20d11dSBharata B Rao }
23303a20d11dSBharata B Rao 
23316ae4a57aSMark Cave-Ayland void helper_xscmpexpqp(CPUPPCState *env, uint32_t opcode,
23326ae4a57aSMark Cave-Ayland                        ppc_vsr_t *xa, ppc_vsr_t *xb)
23333a20d11dSBharata B Rao {
23343a20d11dSBharata B Rao     int64_t exp_a, exp_b;
23353a20d11dSBharata B Rao     uint32_t cc;
23363a20d11dSBharata B Rao 
2337cf3b0334SMark Cave-Ayland     exp_a = extract64(xa->VsrD(0), 48, 15);
2338cf3b0334SMark Cave-Ayland     exp_b = extract64(xb->VsrD(0), 48, 15);
23393a20d11dSBharata B Rao 
2340cf3b0334SMark Cave-Ayland     if (unlikely(float128_is_any_nan(xa->f128) ||
2341cf3b0334SMark Cave-Ayland                  float128_is_any_nan(xb->f128))) {
23423a20d11dSBharata B Rao         cc = CRF_SO;
23433a20d11dSBharata B Rao     } else {
23443a20d11dSBharata B Rao         if (exp_a < exp_b) {
23453a20d11dSBharata B Rao             cc = CRF_LT;
23463a20d11dSBharata B Rao         } else if (exp_a > exp_b) {
23473a20d11dSBharata B Rao             cc = CRF_GT;
23483a20d11dSBharata B Rao         } else {
23493a20d11dSBharata B Rao             cc = CRF_EQ;
23503a20d11dSBharata B Rao         }
23513a20d11dSBharata B Rao     }
23523a20d11dSBharata B Rao 
23535c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
23545c94dd38SPaul A. Clarke     env->fpscr |= cc << FPSCR_FPCC;
23553a20d11dSBharata B Rao     env->crf[BF(opcode)] = cc;
23563a20d11dSBharata B Rao 
23573278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());
23583a20d11dSBharata B Rao }
23593a20d11dSBharata B Rao 
2360132954a8SGiuseppe Musacchio static inline void do_scalar_cmp(CPUPPCState *env, ppc_vsr_t *xa, ppc_vsr_t *xb,
2361132954a8SGiuseppe Musacchio                                  int crf_idx, bool ordered)
2362132954a8SGiuseppe Musacchio {
2363132954a8SGiuseppe Musacchio     uint32_t cc;
2364132954a8SGiuseppe Musacchio     bool vxsnan_flag = false, vxvc_flag = false;
2365132954a8SGiuseppe Musacchio 
2366132954a8SGiuseppe Musacchio     helper_reset_fpstatus(env);
2367132954a8SGiuseppe Musacchio 
2368132954a8SGiuseppe Musacchio     switch (float64_compare(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) {
2369132954a8SGiuseppe Musacchio     case float_relation_less:
2370132954a8SGiuseppe Musacchio         cc = CRF_LT;
2371132954a8SGiuseppe Musacchio         break;
2372132954a8SGiuseppe Musacchio     case float_relation_equal:
2373132954a8SGiuseppe Musacchio         cc = CRF_EQ;
2374132954a8SGiuseppe Musacchio         break;
2375132954a8SGiuseppe Musacchio     case float_relation_greater:
2376132954a8SGiuseppe Musacchio         cc = CRF_GT;
2377132954a8SGiuseppe Musacchio         break;
2378132954a8SGiuseppe Musacchio     case float_relation_unordered:
2379132954a8SGiuseppe Musacchio         cc = CRF_SO;
2380bc92c260SGiuseppe Musacchio 
2381bc92c260SGiuseppe Musacchio         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status) ||
2382bc92c260SGiuseppe Musacchio             float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {
2383bc92c260SGiuseppe Musacchio             vxsnan_flag = true;
2384208d8033SVíctor Colombo             if (!(env->fpscr & FP_VE) && ordered) {
2385bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2386bc92c260SGiuseppe Musacchio             }
2387bc92c260SGiuseppe Musacchio         } else if (float64_is_quiet_nan(xa->VsrD(0), &env->fp_status) ||
2388bc92c260SGiuseppe Musacchio                    float64_is_quiet_nan(xb->VsrD(0), &env->fp_status)) {
2389bc92c260SGiuseppe Musacchio             if (ordered) {
2390bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2391bc92c260SGiuseppe Musacchio             }
2392bc92c260SGiuseppe Musacchio         }
2393bc92c260SGiuseppe Musacchio 
2394132954a8SGiuseppe Musacchio         break;
2395132954a8SGiuseppe Musacchio     default:
2396132954a8SGiuseppe Musacchio         g_assert_not_reached();
2397be0a4fafSBharata B Rao     }
2398be0a4fafSBharata B Rao 
2399132954a8SGiuseppe Musacchio     env->fpscr &= ~FP_FPCC;
2400132954a8SGiuseppe Musacchio     env->fpscr |= cc << FPSCR_FPCC;
2401132954a8SGiuseppe Musacchio     env->crf[crf_idx] = cc;
2402132954a8SGiuseppe Musacchio 
240391699dbfSGiuseppe Musacchio     if (vxsnan_flag) {
240491699dbfSGiuseppe Musacchio         float_invalid_op_vxsnan(env, GETPC());
240591699dbfSGiuseppe Musacchio     }
240691699dbfSGiuseppe Musacchio     if (vxvc_flag) {
240791699dbfSGiuseppe Musacchio         float_invalid_op_vxvc(env, 0, GETPC());
240891699dbfSGiuseppe Musacchio     }
240991699dbfSGiuseppe Musacchio 
24103278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());
2411132954a8SGiuseppe Musacchio }
2412132954a8SGiuseppe Musacchio 
2413132954a8SGiuseppe Musacchio void helper_xscmpodp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2414132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2415132954a8SGiuseppe Musacchio {
2416132954a8SGiuseppe Musacchio     do_scalar_cmp(env, xa, xb, BF(opcode), true);
2417132954a8SGiuseppe Musacchio }
2418132954a8SGiuseppe Musacchio 
2419132954a8SGiuseppe Musacchio void helper_xscmpudp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2420132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2421132954a8SGiuseppe Musacchio {
2422132954a8SGiuseppe Musacchio     do_scalar_cmp(env, xa, xb, BF(opcode), false);
2423132954a8SGiuseppe Musacchio }
2424132954a8SGiuseppe Musacchio 
2425132954a8SGiuseppe Musacchio static inline void do_scalar_cmpq(CPUPPCState *env, ppc_vsr_t *xa,
2426132954a8SGiuseppe Musacchio                                   ppc_vsr_t *xb, int crf_idx, bool ordered)
2427132954a8SGiuseppe Musacchio {
2428132954a8SGiuseppe Musacchio     uint32_t cc;
2429132954a8SGiuseppe Musacchio     bool vxsnan_flag = false, vxvc_flag = false;
2430132954a8SGiuseppe Musacchio 
2431132954a8SGiuseppe Musacchio     helper_reset_fpstatus(env);
2432132954a8SGiuseppe Musacchio 
2433132954a8SGiuseppe Musacchio     switch (float128_compare(xa->f128, xb->f128, &env->fp_status)) {
2434132954a8SGiuseppe Musacchio     case float_relation_less:
2435132954a8SGiuseppe Musacchio         cc = CRF_LT;
2436132954a8SGiuseppe Musacchio         break;
2437132954a8SGiuseppe Musacchio     case float_relation_equal:
2438132954a8SGiuseppe Musacchio         cc = CRF_EQ;
2439132954a8SGiuseppe Musacchio         break;
2440132954a8SGiuseppe Musacchio     case float_relation_greater:
2441132954a8SGiuseppe Musacchio         cc = CRF_GT;
2442132954a8SGiuseppe Musacchio         break;
2443132954a8SGiuseppe Musacchio     case float_relation_unordered:
2444132954a8SGiuseppe Musacchio         cc = CRF_SO;
2445bc92c260SGiuseppe Musacchio 
2446bc92c260SGiuseppe Musacchio         if (float128_is_signaling_nan(xa->f128, &env->fp_status) ||
2447bc92c260SGiuseppe Musacchio             float128_is_signaling_nan(xb->f128, &env->fp_status)) {
2448bc92c260SGiuseppe Musacchio             vxsnan_flag = true;
2449208d8033SVíctor Colombo             if (!(env->fpscr & FP_VE) && ordered) {
2450bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2451bc92c260SGiuseppe Musacchio             }
2452bc92c260SGiuseppe Musacchio         } else if (float128_is_quiet_nan(xa->f128, &env->fp_status) ||
2453bc92c260SGiuseppe Musacchio                    float128_is_quiet_nan(xb->f128, &env->fp_status)) {
2454bc92c260SGiuseppe Musacchio             if (ordered) {
2455bc92c260SGiuseppe Musacchio                 vxvc_flag = true;
2456bc92c260SGiuseppe Musacchio             }
2457bc92c260SGiuseppe Musacchio         }
2458bc92c260SGiuseppe Musacchio 
2459132954a8SGiuseppe Musacchio         break;
2460132954a8SGiuseppe Musacchio     default:
2461132954a8SGiuseppe Musacchio         g_assert_not_reached();
2462132954a8SGiuseppe Musacchio     }
2463132954a8SGiuseppe Musacchio 
2464132954a8SGiuseppe Musacchio     env->fpscr &= ~FP_FPCC;
2465132954a8SGiuseppe Musacchio     env->fpscr |= cc << FPSCR_FPCC;
2466132954a8SGiuseppe Musacchio     env->crf[crf_idx] = cc;
2467132954a8SGiuseppe Musacchio 
246891699dbfSGiuseppe Musacchio     if (vxsnan_flag) {
246991699dbfSGiuseppe Musacchio         float_invalid_op_vxsnan(env, GETPC());
247091699dbfSGiuseppe Musacchio     }
247191699dbfSGiuseppe Musacchio     if (vxvc_flag) {
247291699dbfSGiuseppe Musacchio         float_invalid_op_vxvc(env, 0, GETPC());
247391699dbfSGiuseppe Musacchio     }
247491699dbfSGiuseppe Musacchio 
24753278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());
2476132954a8SGiuseppe Musacchio }
2477132954a8SGiuseppe Musacchio 
2478132954a8SGiuseppe Musacchio void helper_xscmpoqp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2479132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2480132954a8SGiuseppe Musacchio {
2481132954a8SGiuseppe Musacchio     do_scalar_cmpq(env, xa, xb, BF(opcode), true);
2482132954a8SGiuseppe Musacchio }
2483132954a8SGiuseppe Musacchio 
2484132954a8SGiuseppe Musacchio void helper_xscmpuqp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xa,
2485132954a8SGiuseppe Musacchio                      ppc_vsr_t *xb)
2486132954a8SGiuseppe Musacchio {
2487132954a8SGiuseppe Musacchio     do_scalar_cmpq(env, xa, xb, BF(opcode), false);
2488132954a8SGiuseppe Musacchio }
2489be0a4fafSBharata B Rao 
2490fa9ebf8cSDavid Gibson /*
2491fa9ebf8cSDavid Gibson  * VSX_MAX_MIN - VSX floating point maximum/minimum
2492fcf5ef2aSThomas Huth  *   name  - instruction mnemonic
2493fcf5ef2aSThomas Huth  *   op    - operation (max or min)
2494fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2495fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2496fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2497fcf5ef2aSThomas Huth  */
2498fcf5ef2aSThomas Huth #define VSX_MAX_MIN(name, op, nels, tp, fld)                                  \
249999125c74SMark Cave-Ayland void helper_##name(CPUPPCState *env, ppc_vsr_t *xt,                           \
250099125c74SMark Cave-Ayland                    ppc_vsr_t *xa, ppc_vsr_t *xb)                              \
2501fcf5ef2aSThomas Huth {                                                                             \
2502205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                        \
2503fcf5ef2aSThomas Huth     int i;                                                                    \
2504fcf5ef2aSThomas Huth                                                                               \
2505fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                              \
2506cf3b0334SMark Cave-Ayland         t.fld = tp##_##op(xa->fld, xb->fld, &env->fp_status);                 \
2507cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xa->fld, &env->fp_status) ||       \
2508cf3b0334SMark Cave-Ayland                      tp##_is_signaling_nan(xb->fld, &env->fp_status))) {      \
250913c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
2510fcf5ef2aSThomas Huth         }                                                                     \
2511fcf5ef2aSThomas Huth     }                                                                         \
2512fcf5ef2aSThomas Huth                                                                               \
2513cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
25143278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());                               \
2515fcf5ef2aSThomas Huth }
2516fcf5ef2aSThomas Huth 
2517fcf5ef2aSThomas Huth VSX_MAX_MIN(xsmaxdp, maxnum, 1, float64, VsrD(0))
2518fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmaxdp, maxnum, 2, float64, VsrD(i))
2519fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmaxsp, maxnum, 4, float32, VsrW(i))
2520fcf5ef2aSThomas Huth VSX_MAX_MIN(xsmindp, minnum, 1, float64, VsrD(0))
2521fcf5ef2aSThomas Huth VSX_MAX_MIN(xvmindp, minnum, 2, float64, VsrD(i))
2522fcf5ef2aSThomas Huth VSX_MAX_MIN(xvminsp, minnum, 4, float32, VsrW(i))
2523fcf5ef2aSThomas Huth 
2524da499405SVíctor Colombo #define VSX_MAX_MINC(name, max, tp, fld)                                      \
2525201fc774SVictor Colombo void helper_##name(CPUPPCState *env,                                          \
252623d0766bSMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)               \
25272770deedSBharata B Rao {                                                                             \
2528205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                        \
2529da499405SVíctor Colombo     bool first;                                                               \
25302770deedSBharata B Rao                                                                               \
2531e1428e5bSVíctor Colombo     helper_reset_fpstatus(env);                                               \
2532e1428e5bSVíctor Colombo                                                                               \
2533da499405SVíctor Colombo     if (max) {                                                                \
2534da499405SVíctor Colombo         first = tp##_le_quiet(xb->fld, xa->fld, &env->fp_status);             \
25352770deedSBharata B Rao     } else {                                                                  \
2536da499405SVíctor Colombo         first = tp##_lt_quiet(xa->fld, xb->fld, &env->fp_status);             \
25372770deedSBharata B Rao     }                                                                         \
25382770deedSBharata B Rao                                                                               \
2539da499405SVíctor Colombo     if (first) {                                                              \
2540da499405SVíctor Colombo         t.fld = xa->fld;                                                      \
2541da499405SVíctor Colombo     } else {                                                                  \
2542da499405SVíctor Colombo         t.fld = xb->fld;                                                      \
2543da499405SVíctor Colombo         if (env->fp_status.float_exception_flags & float_flag_invalid_snan) { \
254413c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                            \
25452770deedSBharata B Rao         }                                                                     \
2546da499405SVíctor Colombo     }                                                                         \
2547da499405SVíctor Colombo                                                                               \
2548cf3b0334SMark Cave-Ayland     *xt = t;                                                                  \
2549da499405SVíctor Colombo }
25502770deedSBharata B Rao 
2551da499405SVíctor Colombo VSX_MAX_MINC(XSMAXCDP, true, float64, VsrD(0));
2552da499405SVíctor Colombo VSX_MAX_MINC(XSMINCDP, false, float64, VsrD(0));
25537b8d6e3eSVíctor Colombo VSX_MAX_MINC(XSMAXCQP, true, float128, f128);
25547b8d6e3eSVíctor Colombo VSX_MAX_MINC(XSMINCQP, false, float128, f128);
25552770deedSBharata B Rao 
2556d4ccd87eSBharata B Rao #define VSX_MAX_MINJ(name, max)                                               \
2557201fc774SVictor Colombo void helper_##name(CPUPPCState *env,                                          \
255823d0766bSMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)               \
2559d4ccd87eSBharata B Rao {                                                                             \
2560205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                        \
2561d4ccd87eSBharata B Rao     bool vxsnan_flag = false, vex_flag = false;                               \
2562d4ccd87eSBharata B Rao                                                                               \
2563cf3b0334SMark Cave-Ayland     if (unlikely(float64_is_any_nan(xa->VsrD(0)))) {                          \
2564cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xa->VsrD(0), &env->fp_status)) {         \
2565d4ccd87eSBharata B Rao             vxsnan_flag = true;                                               \
2566d4ccd87eSBharata B Rao         }                                                                     \
2567cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
2568cf3b0334SMark Cave-Ayland     } else if (unlikely(float64_is_any_nan(xb->VsrD(0)))) {                   \
2569cf3b0334SMark Cave-Ayland         if (float64_is_signaling_nan(xb->VsrD(0), &env->fp_status)) {         \
2570d4ccd87eSBharata B Rao             vxsnan_flag = true;                                               \
2571d4ccd87eSBharata B Rao         }                                                                     \
2572cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
2573cf3b0334SMark Cave-Ayland     } else if (float64_is_zero(xa->VsrD(0)) &&                                \
2574cf3b0334SMark Cave-Ayland                float64_is_zero(xb->VsrD(0))) {                                \
2575d4ccd87eSBharata B Rao         if (max) {                                                            \
2576cf3b0334SMark Cave-Ayland             if (!float64_is_neg(xa->VsrD(0)) ||                               \
2577cf3b0334SMark Cave-Ayland                 !float64_is_neg(xb->VsrD(0))) {                               \
2578cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0ULL;                                             \
2579d4ccd87eSBharata B Rao             } else {                                                          \
2580cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0x8000000000000000ULL;                            \
2581d4ccd87eSBharata B Rao             }                                                                 \
2582d4ccd87eSBharata B Rao         } else {                                                              \
2583cf3b0334SMark Cave-Ayland             if (float64_is_neg(xa->VsrD(0)) ||                                \
2584cf3b0334SMark Cave-Ayland                 float64_is_neg(xb->VsrD(0))) {                                \
2585cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0x8000000000000000ULL;                            \
2586d4ccd87eSBharata B Rao             } else {                                                          \
2587cf3b0334SMark Cave-Ayland                 t.VsrD(0) = 0ULL;                                             \
2588d4ccd87eSBharata B Rao             }                                                                 \
2589d4ccd87eSBharata B Rao         }                                                                     \
2590d4ccd87eSBharata B Rao     } else if ((max &&                                                        \
2591cf3b0334SMark Cave-Ayland                !float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status)) ||     \
2592d4ccd87eSBharata B Rao                (!max &&                                                       \
2593cf3b0334SMark Cave-Ayland                float64_lt(xa->VsrD(0), xb->VsrD(0), &env->fp_status))) {      \
2594cf3b0334SMark Cave-Ayland         t.VsrD(0) = xa->VsrD(0);                                              \
2595d4ccd87eSBharata B Rao     } else {                                                                  \
2596cf3b0334SMark Cave-Ayland         t.VsrD(0) = xb->VsrD(0);                                              \
2597d4ccd87eSBharata B Rao     }                                                                         \
2598d4ccd87eSBharata B Rao                                                                               \
2599208d8033SVíctor Colombo     vex_flag = (env->fpscr & FP_VE) && vxsnan_flag;                           \
2600d4ccd87eSBharata B Rao     if (vxsnan_flag) {                                                        \
260113c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());                                \
2602d4ccd87eSBharata B Rao     }                                                                         \
2603d4ccd87eSBharata B Rao     if (!vex_flag) {                                                          \
2604cf3b0334SMark Cave-Ayland         *xt = t;                                                              \
2605d4ccd87eSBharata B Rao     }                                                                         \
2606d4ccd87eSBharata B Rao }                                                                             \
2607d4ccd87eSBharata B Rao 
26085307df8fSVíctor Colombo VSX_MAX_MINJ(XSMAXJDP, 1);
26095307df8fSVíctor Colombo VSX_MAX_MINJ(XSMINJDP, 0);
2610d4ccd87eSBharata B Rao 
2611fa9ebf8cSDavid Gibson /*
2612fa9ebf8cSDavid Gibson  * VSX_CMP - VSX floating point compare
2613fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2614fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2615fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
2616fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
2617fcf5ef2aSThomas Huth  *   cmp   - comparison operation
2618fcf5ef2aSThomas Huth  *   svxvc - set VXVC bit
2619fcf5ef2aSThomas Huth  *   exp   - expected result of comparison
2620fcf5ef2aSThomas Huth  */
2621fcf5ef2aSThomas Huth #define VSX_CMP(op, nels, tp, fld, cmp, svxvc, exp)                       \
262200084a25SMark Cave-Ayland uint32_t helper_##op(CPUPPCState *env, ppc_vsr_t *xt,                     \
262300084a25SMark Cave-Ayland                      ppc_vsr_t *xa, ppc_vsr_t *xb)                        \
2624fcf5ef2aSThomas Huth {                                                                         \
2625cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                    \
262600084a25SMark Cave-Ayland     uint32_t crf6 = 0;                                                    \
2627fcf5ef2aSThomas Huth     int i;                                                                \
2628fcf5ef2aSThomas Huth     int all_true = 1;                                                     \
2629fcf5ef2aSThomas Huth     int all_false = 1;                                                    \
2630fcf5ef2aSThomas Huth                                                                           \
2631fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                          \
2632cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_any_nan(xa->fld) ||                          \
2633cf3b0334SMark Cave-Ayland                      tp##_is_any_nan(xb->fld))) {                         \
2634cf3b0334SMark Cave-Ayland             if (tp##_is_signaling_nan(xa->fld, &env->fp_status) ||        \
2635cf3b0334SMark Cave-Ayland                 tp##_is_signaling_nan(xb->fld, &env->fp_status)) {        \
263613c9115fSRichard Henderson                 float_invalid_op_vxsnan(env, GETPC());                    \
2637fcf5ef2aSThomas Huth             }                                                             \
2638fcf5ef2aSThomas Huth             if (svxvc) {                                                  \
263913c9115fSRichard Henderson                 float_invalid_op_vxvc(env, 0, GETPC());                   \
2640fcf5ef2aSThomas Huth             }                                                             \
2641cf3b0334SMark Cave-Ayland             t.fld = 0;                                                    \
2642fcf5ef2aSThomas Huth             all_true = 0;                                                 \
2643fcf5ef2aSThomas Huth         } else {                                                          \
2644cf3b0334SMark Cave-Ayland             if (tp##_##cmp(xb->fld, xa->fld, &env->fp_status) == exp) {   \
2645cf3b0334SMark Cave-Ayland                 t.fld = -1;                                               \
2646fcf5ef2aSThomas Huth                 all_false = 0;                                            \
2647fcf5ef2aSThomas Huth             } else {                                                      \
2648cf3b0334SMark Cave-Ayland                 t.fld = 0;                                                \
2649fcf5ef2aSThomas Huth                 all_true = 0;                                             \
2650fcf5ef2aSThomas Huth             }                                                             \
2651fcf5ef2aSThomas Huth         }                                                                 \
2652fcf5ef2aSThomas Huth     }                                                                     \
2653fcf5ef2aSThomas Huth                                                                           \
2654cf3b0334SMark Cave-Ayland     *xt = t;                                                              \
265500084a25SMark Cave-Ayland     crf6 = (all_true ? 0x8 : 0) | (all_false ? 0x2 : 0);                  \
265600084a25SMark Cave-Ayland     return crf6;                                                          \
2657fcf5ef2aSThomas Huth }
2658fcf5ef2aSThomas Huth 
2659fcf5ef2aSThomas Huth VSX_CMP(xvcmpeqdp, 2, float64, VsrD(i), eq, 0, 1)
2660fcf5ef2aSThomas Huth VSX_CMP(xvcmpgedp, 2, float64, VsrD(i), le, 1, 1)
2661fcf5ef2aSThomas Huth VSX_CMP(xvcmpgtdp, 2, float64, VsrD(i), lt, 1, 1)
2662fcf5ef2aSThomas Huth VSX_CMP(xvcmpnedp, 2, float64, VsrD(i), eq, 0, 0)
2663fcf5ef2aSThomas Huth VSX_CMP(xvcmpeqsp, 4, float32, VsrW(i), eq, 0, 1)
2664fcf5ef2aSThomas Huth VSX_CMP(xvcmpgesp, 4, float32, VsrW(i), le, 1, 1)
2665fcf5ef2aSThomas Huth VSX_CMP(xvcmpgtsp, 4, float32, VsrW(i), lt, 1, 1)
2666fcf5ef2aSThomas Huth VSX_CMP(xvcmpnesp, 4, float32, VsrW(i), eq, 0, 0)
2667fcf5ef2aSThomas Huth 
2668fa9ebf8cSDavid Gibson /*
2669fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP - VSX floating point/floating point conversion
2670fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2671fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2672fcf5ef2aSThomas Huth  *   stp   - source type (float32 or float64)
2673fcf5ef2aSThomas Huth  *   ttp   - target type (float32 or float64)
2674fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2675fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field (f32 or f64)
2676dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
2677fcf5ef2aSThomas Huth  */
2678dd657a35SVíctor Colombo #define VSX_CVT_FP_TO_FP(op, nels, stp, ttp, sfld, tfld, sfifprf)  \
267975cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)   \
2680fcf5ef2aSThomas Huth {                                                                  \
2681205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                             \
2682fcf5ef2aSThomas Huth     int i;                                                         \
2683fcf5ef2aSThomas Huth                                                                    \
2684fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                   \
2685cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);        \
2686cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,              \
2687fcf5ef2aSThomas Huth                                             &env->fp_status))) {   \
268813c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                 \
2689cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                   \
2690fcf5ef2aSThomas Huth         }                                                          \
2691dd657a35SVíctor Colombo         if (sfifprf) {                                             \
2692cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                \
2693fcf5ef2aSThomas Huth         }                                                          \
2694fcf5ef2aSThomas Huth     }                                                              \
2695fcf5ef2aSThomas Huth                                                                    \
2696cf3b0334SMark Cave-Ayland     *xt = t;                                                       \
2697dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                  \
2698fcf5ef2aSThomas Huth }
2699fcf5ef2aSThomas Huth 
2700fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xscvspdp, 1, float32, float64, VsrW(0), VsrD(0), 1)
2701fcf5ef2aSThomas Huth VSX_CVT_FP_TO_FP(xvcvspdp, 2, float32, float64, VsrW(2 * i), VsrD(i), 0)
2702fcf5ef2aSThomas Huth 
2703dd657a35SVíctor Colombo #define VSX_CVT_FP_TO_FP2(op, nels, stp, ttp, sfifprf)                \
27043515553bSLucas Coutinho void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)      \
27053515553bSLucas Coutinho {                                                                     \
27063515553bSLucas Coutinho     ppc_vsr_t t = { };                                                \
27073515553bSLucas Coutinho     int i;                                                            \
27083515553bSLucas Coutinho                                                                       \
27093515553bSLucas Coutinho     for (i = 0; i < nels; i++) {                                      \
27103515553bSLucas Coutinho         t.VsrW(2 * i) = stp##_to_##ttp(xb->VsrD(i), &env->fp_status); \
27113515553bSLucas Coutinho         if (unlikely(stp##_is_signaling_nan(xb->VsrD(i),              \
27123515553bSLucas Coutinho                                             &env->fp_status))) {      \
27133515553bSLucas Coutinho             float_invalid_op_vxsnan(env, GETPC());                    \
27143515553bSLucas Coutinho             t.VsrW(2 * i) = ttp##_snan_to_qnan(t.VsrW(2 * i));        \
27153515553bSLucas Coutinho         }                                                             \
2716dd657a35SVíctor Colombo         if (sfifprf) {                                                \
27173515553bSLucas Coutinho             helper_compute_fprf_##ttp(env, t.VsrW(2 * i));            \
27183515553bSLucas Coutinho         }                                                             \
27193515553bSLucas Coutinho         t.VsrW(2 * i + 1) = t.VsrW(2 * i);                            \
27203515553bSLucas Coutinho     }                                                                 \
27213515553bSLucas Coutinho                                                                       \
27223515553bSLucas Coutinho     *xt = t;                                                          \
2723dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                     \
27243515553bSLucas Coutinho }
27253515553bSLucas Coutinho 
27263515553bSLucas Coutinho VSX_CVT_FP_TO_FP2(xvcvdpsp, 2, float64, float32, 0)
27273515553bSLucas Coutinho VSX_CVT_FP_TO_FP2(xscvdpsp, 1, float64, float32, 1)
27283515553bSLucas Coutinho 
2729fa9ebf8cSDavid Gibson /*
2730fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP_VECTOR - VSX floating point/floating point conversion
2731e5487803SBharata B Rao  *   op    - instruction mnemonic
2732e5487803SBharata B Rao  *   nels  - number of elements (1, 2 or 4)
2733e5487803SBharata B Rao  *   stp   - source type (float32 or float64)
2734e5487803SBharata B Rao  *   ttp   - target type (float32 or float64)
2735e5487803SBharata B Rao  *   sfld  - source vsr_t field
2736e5487803SBharata B Rao  *   tfld  - target vsr_t field (f32 or f64)
2737e5487803SBharata B Rao  *   sfprf - set FPRF
2738e5487803SBharata B Rao  */
2739e5487803SBharata B Rao #define VSX_CVT_FP_TO_FP_VECTOR(op, nels, stp, ttp, sfld, tfld, sfprf)  \
274099229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
274199229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                          \
2742e5487803SBharata B Rao {                                                                       \
2743cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
2744e5487803SBharata B Rao     int i;                                                              \
2745e5487803SBharata B Rao                                                                         \
2746e5487803SBharata B Rao     for (i = 0; i < nels; i++) {                                        \
2747cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);             \
2748cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,                   \
2749e5487803SBharata B Rao                                             &env->fp_status))) {        \
275013c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                      \
2751cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                        \
2752e5487803SBharata B Rao         }                                                               \
2753e5487803SBharata B Rao         if (sfprf) {                                                    \
2754cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                     \
2755e5487803SBharata B Rao         }                                                               \
2756e5487803SBharata B Rao     }                                                                   \
2757e5487803SBharata B Rao                                                                         \
2758cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
27593278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());                          \
2760e5487803SBharata B Rao }
2761e5487803SBharata B Rao 
2762e5487803SBharata B Rao VSX_CVT_FP_TO_FP_VECTOR(xscvdpqp, 1, float64, float128, VsrD(0), f128, 1)
2763e5487803SBharata B Rao 
2764fa9ebf8cSDavid Gibson /*
2765fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_FP_HP - VSX floating point/floating point conversion
2766f566c047SBharata B Rao  *                       involving one half precision value
2767f566c047SBharata B Rao  *   op    - instruction mnemonic
27688b920d8aSNikunj A Dadhania  *   nels  - number of elements (1, 2 or 4)
2769f566c047SBharata B Rao  *   stp   - source type
2770f566c047SBharata B Rao  *   ttp   - target type
2771f566c047SBharata B Rao  *   sfld  - source vsr_t field
2772f566c047SBharata B Rao  *   tfld  - target vsr_t field
2773dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
2774f566c047SBharata B Rao  */
2775dd657a35SVíctor Colombo #define VSX_CVT_FP_TO_FP_HP(op, nels, stp, ttp, sfld, tfld, sfifprf) \
277675cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)   \
2777f566c047SBharata B Rao {                                                                  \
2778cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                             \
27798b920d8aSNikunj A Dadhania     int i;                                                         \
2780f566c047SBharata B Rao                                                                    \
27818b920d8aSNikunj A Dadhania     for (i = 0; i < nels; i++) {                                   \
2782cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, 1, &env->fp_status);     \
2783cf3b0334SMark Cave-Ayland         if (unlikely(stp##_is_signaling_nan(xb->sfld,              \
2784f566c047SBharata B Rao                                             &env->fp_status))) {   \
278513c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                 \
2786cf3b0334SMark Cave-Ayland             t.tfld = ttp##_snan_to_qnan(t.tfld);                   \
2787f566c047SBharata B Rao         }                                                          \
2788dd657a35SVíctor Colombo         if (sfifprf) {                                             \
2789cf3b0334SMark Cave-Ayland             helper_compute_fprf_##ttp(env, t.tfld);                \
27908b920d8aSNikunj A Dadhania         }                                                          \
27918b920d8aSNikunj A Dadhania     }                                                              \
2792f566c047SBharata B Rao                                                                    \
2793cf3b0334SMark Cave-Ayland     *xt = t;                                                       \
2794dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                  \
2795f566c047SBharata B Rao }
2796f566c047SBharata B Rao 
27978b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xscvdphp, 1, float64, float16, VsrD(0), VsrH(3), 1)
27988b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xscvhpdp, 1, float16, float64, VsrH(3), VsrD(0), 1)
27998b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xvcvsphp, 4, float32, float16, VsrW(i), VsrH(2 * i  + 1), 0)
28008b920d8aSNikunj A Dadhania VSX_CVT_FP_TO_FP_HP(xvcvhpsp, 4, float16, float32, VsrH(2 * i + 1), VsrW(i), 0)
2801f566c047SBharata B Rao 
28023909ff1fSVíctor Colombo void helper_XVCVSPBF16(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)
28033909ff1fSVíctor Colombo {
28043909ff1fSVíctor Colombo     ppc_vsr_t t = { };
28053909ff1fSVíctor Colombo     int i, status;
28063909ff1fSVíctor Colombo 
2807a9eb5037SVíctor Colombo     helper_reset_fpstatus(env);
2808a9eb5037SVíctor Colombo 
28093909ff1fSVíctor Colombo     for (i = 0; i < 4; i++) {
28103909ff1fSVíctor Colombo         t.VsrH(2 * i + 1) = float32_to_bfloat16(xb->VsrW(i), &env->fp_status);
28113909ff1fSVíctor Colombo     }
28123909ff1fSVíctor Colombo 
28133909ff1fSVíctor Colombo     status = get_float_exception_flags(&env->fp_status);
28143909ff1fSVíctor Colombo     if (unlikely(status & float_flag_invalid_snan)) {
28153909ff1fSVíctor Colombo         float_invalid_op_vxsnan(env, GETPC());
28163909ff1fSVíctor Colombo     }
28173909ff1fSVíctor Colombo 
28183909ff1fSVíctor Colombo     *xt = t;
28193278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());
28203909ff1fSVíctor Colombo }
28213909ff1fSVíctor Colombo 
2822caf6f9b5SMatheus Ferst void helper_XSCVQPDP(CPUPPCState *env, uint32_t ro, ppc_vsr_t *xt,
2823caf6f9b5SMatheus Ferst                      ppc_vsr_t *xb)
28242a084dadSBharata B Rao {
2825cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
2826a8d411abSBharata B Rao     float_status tstat;
28272a084dadSBharata B Rao 
2828a8d411abSBharata B Rao     tstat = env->fp_status;
2829caf6f9b5SMatheus Ferst     if (ro != 0) {
2830a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
28312a084dadSBharata B Rao     }
28322a084dadSBharata B Rao 
2833cf3b0334SMark Cave-Ayland     t.VsrD(0) = float128_to_float64(xb->f128, &tstat);
2834a8d411abSBharata B Rao     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
2835cf3b0334SMark Cave-Ayland     if (unlikely(float128_is_signaling_nan(xb->f128, &tstat))) {
283613c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
2837cf3b0334SMark Cave-Ayland         t.VsrD(0) = float64_snan_to_qnan(t.VsrD(0));
28382a084dadSBharata B Rao     }
2839cf3b0334SMark Cave-Ayland     helper_compute_fprf_float64(env, t.VsrD(0));
28402a084dadSBharata B Rao 
2841cf3b0334SMark Cave-Ayland     *xt = t;
28423278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
28432a084dadSBharata B Rao }
28442a084dadSBharata B Rao 
2845fcf5ef2aSThomas Huth uint64_t helper_xscvdpspn(CPUPPCState *env, uint64_t xb)
2846fcf5ef2aSThomas Huth {
2847fa7d9cb9SPaul A. Clarke     uint64_t result, sign, exp, frac;
2848e6f1bfb2SPaul A. Clarke 
2849fcf5ef2aSThomas Huth     float_status tstat = env->fp_status;
2850fcf5ef2aSThomas Huth     set_float_exception_flags(0, &tstat);
2851fcf5ef2aSThomas Huth 
2852fa7d9cb9SPaul A. Clarke     sign = extract64(xb, 63,  1);
2853fa7d9cb9SPaul A. Clarke     exp  = extract64(xb, 52, 11);
2854fa7d9cb9SPaul A. Clarke     frac = extract64(xb,  0, 52) | 0x10000000000000ULL;
2855fa7d9cb9SPaul A. Clarke 
2856fa7d9cb9SPaul A. Clarke     if (unlikely(exp == 0 && extract64(frac, 0, 52) != 0)) {
2857fa7d9cb9SPaul A. Clarke         /* DP denormal operand.  */
2858fa7d9cb9SPaul A. Clarke         /* Exponent override to DP min exp.  */
2859fa7d9cb9SPaul A. Clarke         exp = 1;
2860fa7d9cb9SPaul A. Clarke         /* Implicit bit override to 0.  */
2861fa7d9cb9SPaul A. Clarke         frac = deposit64(frac, 53, 1, 0);
2862fa7d9cb9SPaul A. Clarke     }
2863fa7d9cb9SPaul A. Clarke 
2864fa7d9cb9SPaul A. Clarke     if (unlikely(exp < 897 && frac != 0)) {
2865fa7d9cb9SPaul A. Clarke         /* SP tiny operand.  */
2866fa7d9cb9SPaul A. Clarke         if (897 - exp > 63) {
2867fa7d9cb9SPaul A. Clarke             frac = 0;
2868fa7d9cb9SPaul A. Clarke         } else {
2869fa7d9cb9SPaul A. Clarke             /* Denormalize until exp = SP min exp.  */
2870fa7d9cb9SPaul A. Clarke             frac >>= (897 - exp);
2871fa7d9cb9SPaul A. Clarke         }
2872fa7d9cb9SPaul A. Clarke         /* Exponent override to SP min exp - 1.  */
2873fa7d9cb9SPaul A. Clarke         exp = 896;
2874fa7d9cb9SPaul A. Clarke     }
2875fa7d9cb9SPaul A. Clarke 
2876fa7d9cb9SPaul A. Clarke     result = sign << 31;
2877fa7d9cb9SPaul A. Clarke     result |= extract64(exp, 10, 1) << 30;
2878fa7d9cb9SPaul A. Clarke     result |= extract64(exp, 0, 7) << 23;
2879fa7d9cb9SPaul A. Clarke     result |= extract64(frac, 29, 23);
2880fa7d9cb9SPaul A. Clarke 
2881e6f1bfb2SPaul A. Clarke     /* hardware replicates result to both words of the doubleword result.  */
2882e6f1bfb2SPaul A. Clarke     return (result << 32) | result;
2883fcf5ef2aSThomas Huth }
2884fcf5ef2aSThomas Huth 
2885cf862beeSMatheus Ferst uint64_t helper_XSCVSPDPN(uint64_t xb)
2886fcf5ef2aSThomas Huth {
288784ade98eSMatheus Ferst     return helper_todouble(xb >> 32);
2888fcf5ef2aSThomas Huth }
2889fcf5ef2aSThomas Huth 
2890fa9ebf8cSDavid Gibson /*
2891fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_INT - VSX floating point to integer conversion
2892fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
2893fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
2894fcf5ef2aSThomas Huth  *   stp   - source type (float32 or float64)
2895fcf5ef2aSThomas Huth  *   ttp   - target type (int32, uint32, int64 or uint64)
2896fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
2897fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field
28983278677fSVíctor Colombo  *   sfi   - set FI
2899fcf5ef2aSThomas Huth  *   rnan  - resulting NaN
2900fcf5ef2aSThomas Huth  */
29013278677fSVíctor Colombo #define VSX_CVT_FP_TO_INT(op, nels, stp, ttp, sfld, tfld, sfi, rnan)         \
290275cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2903fcf5ef2aSThomas Huth {                                                                            \
2904a3dec427SRichard Henderson     int all_flags = env->fp_status.float_exception_flags, flags;             \
2905205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                       \
2906fcf5ef2aSThomas Huth     int i;                                                                   \
2907fcf5ef2aSThomas Huth                                                                              \
2908fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                             \
2909a3dec427SRichard Henderson         env->fp_status.float_exception_flags = 0;                            \
2910cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp##_round_to_zero(xb->sfld, &env->fp_status);  \
2911a3dec427SRichard Henderson         flags = env->fp_status.float_exception_flags;                        \
2912a3dec427SRichard Henderson         if (unlikely(flags & float_flag_invalid)) {                          \
2913fed12f3bSRichard Henderson             t.tfld = float_invalid_cvt(env, flags, t.tfld, rnan, 0, GETPC());\
2914fcf5ef2aSThomas Huth         }                                                                    \
2915a3dec427SRichard Henderson         all_flags |= flags;                                                  \
2916fcf5ef2aSThomas Huth     }                                                                        \
2917fcf5ef2aSThomas Huth                                                                              \
2918cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
2919a3dec427SRichard Henderson     env->fp_status.float_exception_flags = all_flags;                        \
29203278677fSVíctor Colombo     do_float_check_status(env, sfi, GETPC());                                \
2921fcf5ef2aSThomas Huth }
2922fcf5ef2aSThomas Huth 
29233278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xscvdpsxds, 1, float64, int64, VsrD(0), VsrD(0), true, \
2924fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
29253278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xscvdpuxds, 1, float64, uint64, VsrD(0), VsrD(0), true, 0ULL)
29263278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xvcvdpsxds, 2, float64, int64, VsrD(i), VsrD(i), false, \
2927fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
29283278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xvcvdpuxds, 2, float64, uint64, VsrD(i), VsrD(i), false, \
29293278677fSVíctor Colombo                   0ULL)
29303278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xvcvspsxds, 2, float32, int64, VsrW(2 * i), VsrD(i), false, \
2931fcf5ef2aSThomas Huth                   0x8000000000000000ULL)
29323278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xvcvspsxws, 4, float32, int32, VsrW(i), VsrW(i), false, \
29333278677fSVíctor Colombo                   0x80000000ULL)
29343278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xvcvspuxds, 2, float32, uint64, VsrW(2 * i), VsrD(i), \
29353278677fSVíctor Colombo                   false, 0ULL)
29363278677fSVíctor Colombo VSX_CVT_FP_TO_INT(xvcvspuxws, 4, float32, uint32, VsrW(i), VsrW(i), false, 0U)
2937fcf5ef2aSThomas Huth 
2938b3d45205SMatheus Ferst #define VSX_CVT_FP_TO_INT128(op, tp, rnan)                                     \
2939b3d45205SMatheus Ferst void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)               \
2940b3d45205SMatheus Ferst {                                                                              \
2941b3d45205SMatheus Ferst     ppc_vsr_t t;                                                               \
2942b3d45205SMatheus Ferst     int flags;                                                                 \
2943b3d45205SMatheus Ferst                                                                                \
2944b3d45205SMatheus Ferst     helper_reset_fpstatus(env);                                                \
2945b3d45205SMatheus Ferst     t.s128 = float128_to_##tp##_round_to_zero(xb->f128, &env->fp_status);      \
2946b3d45205SMatheus Ferst     flags = get_float_exception_flags(&env->fp_status);                        \
2947b3d45205SMatheus Ferst     if (unlikely(flags & float_flag_invalid)) {                                \
2948b3d45205SMatheus Ferst         t.VsrD(0) = float_invalid_cvt(env, flags, t.VsrD(0), rnan, 0, GETPC());\
2949b3d45205SMatheus Ferst         t.VsrD(1) = -(t.VsrD(0) & 1);                                          \
2950b3d45205SMatheus Ferst     }                                                                          \
2951b3d45205SMatheus Ferst                                                                                \
2952b3d45205SMatheus Ferst     *xt = t;                                                                   \
29533278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());                                 \
2954b3d45205SMatheus Ferst }
2955b3d45205SMatheus Ferst 
2956b3d45205SMatheus Ferst VSX_CVT_FP_TO_INT128(XSCVQPUQZ, uint128, 0)
2957b3d45205SMatheus Ferst VSX_CVT_FP_TO_INT128(XSCVQPSQZ, int128, 0x8000000000000000ULL);
2958b3d45205SMatheus Ferst 
2959fa9ebf8cSDavid Gibson /*
2960217979d3SRichard Henderson  * Likewise, except that the result is duplicated into both subwords.
2961217979d3SRichard Henderson  * Power ISA v3.1 has Programming Notes for these insns:
2962217979d3SRichard Henderson  *     Previous versions of the architecture allowed the contents of
2963217979d3SRichard Henderson  *     word 0 of the result register to be undefined. However, all
2964217979d3SRichard Henderson  *     processors that support this instruction write the result into
2965217979d3SRichard Henderson  *     words 0 and 1 (and words 2 and 3) of the result register, as
2966217979d3SRichard Henderson  *     is required by this version of the architecture.
2967217979d3SRichard Henderson  */
29683278677fSVíctor Colombo #define VSX_CVT_FP_TO_INT2(op, nels, stp, ttp, sfi, rnan)                    \
2969217979d3SRichard Henderson void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)             \
2970217979d3SRichard Henderson {                                                                            \
2971217979d3SRichard Henderson     int all_flags = env->fp_status.float_exception_flags, flags;             \
2972217979d3SRichard Henderson     ppc_vsr_t t = { };                                                       \
2973217979d3SRichard Henderson     int i;                                                                   \
2974217979d3SRichard Henderson                                                                              \
2975217979d3SRichard Henderson     for (i = 0; i < nels; i++) {                                             \
2976217979d3SRichard Henderson         env->fp_status.float_exception_flags = 0;                            \
2977217979d3SRichard Henderson         t.VsrW(2 * i) = stp##_to_##ttp##_round_to_zero(xb->VsrD(i),          \
2978217979d3SRichard Henderson                                                        &env->fp_status);     \
2979217979d3SRichard Henderson         flags = env->fp_status.float_exception_flags;                        \
2980217979d3SRichard Henderson         if (unlikely(flags & float_flag_invalid)) {                          \
2981217979d3SRichard Henderson             t.VsrW(2 * i) = float_invalid_cvt(env, flags, t.VsrW(2 * i),     \
2982217979d3SRichard Henderson                                               rnan, 0, GETPC());             \
2983217979d3SRichard Henderson         }                                                                    \
2984217979d3SRichard Henderson         t.VsrW(2 * i + 1) = t.VsrW(2 * i);                                   \
2985217979d3SRichard Henderson         all_flags |= flags;                                                  \
2986217979d3SRichard Henderson     }                                                                        \
2987217979d3SRichard Henderson                                                                              \
2988217979d3SRichard Henderson     *xt = t;                                                                 \
2989217979d3SRichard Henderson     env->fp_status.float_exception_flags = all_flags;                        \
29903278677fSVíctor Colombo     do_float_check_status(env, sfi, GETPC());                                \
2991217979d3SRichard Henderson }
2992217979d3SRichard Henderson 
29933278677fSVíctor Colombo VSX_CVT_FP_TO_INT2(xscvdpsxws, 1, float64, int32, true, 0x80000000U)
29943278677fSVíctor Colombo VSX_CVT_FP_TO_INT2(xscvdpuxws, 1, float64, uint32, true, 0U)
29953278677fSVíctor Colombo VSX_CVT_FP_TO_INT2(xvcvdpsxws, 2, float64, int32, false, 0x80000000U)
29963278677fSVíctor Colombo VSX_CVT_FP_TO_INT2(xvcvdpuxws, 2, float64, uint32, false, 0U)
2997217979d3SRichard Henderson 
2998217979d3SRichard Henderson /*
2999fa9ebf8cSDavid Gibson  * VSX_CVT_FP_TO_INT_VECTOR - VSX floating point to integer conversion
300005590b92SBharata B Rao  *   op    - instruction mnemonic
300105590b92SBharata B Rao  *   stp   - source type (float32 or float64)
300205590b92SBharata B Rao  *   ttp   - target type (int32, uint32, int64 or uint64)
300305590b92SBharata B Rao  *   sfld  - source vsr_t field
300405590b92SBharata B Rao  *   tfld  - target vsr_t field
300505590b92SBharata B Rao  *   rnan  - resulting NaN
300605590b92SBharata B Rao  */
300705590b92SBharata B Rao #define VSX_CVT_FP_TO_INT_VECTOR(op, stp, ttp, sfld, tfld, rnan)             \
300899229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                          \
300999229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                               \
301005590b92SBharata B Rao {                                                                            \
3011cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                                       \
3012353464eaSRichard Henderson     int flags;                                                               \
301305590b92SBharata B Rao                                                                              \
3014cf3b0334SMark Cave-Ayland     t.tfld = stp##_to_##ttp##_round_to_zero(xb->sfld, &env->fp_status);      \
3015353464eaSRichard Henderson     flags = get_float_exception_flags(&env->fp_status);                      \
3016353464eaSRichard Henderson     if (flags & float_flag_invalid) {                                        \
3017fed12f3bSRichard Henderson         t.tfld = float_invalid_cvt(env, flags, t.tfld, rnan, 0, GETPC());    \
301805590b92SBharata B Rao     }                                                                        \
301905590b92SBharata B Rao                                                                              \
3020cf3b0334SMark Cave-Ayland     *xt = t;                                                                 \
30213278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());                               \
302205590b92SBharata B Rao }
302305590b92SBharata B Rao 
302405590b92SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpsdz, float128, int64, f128, VsrD(0),          \
302505590b92SBharata B Rao                   0x8000000000000000ULL)
302605590b92SBharata B Rao 
302705590b92SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpswz, float128, int32, f128, VsrD(0),          \
302805590b92SBharata B Rao                   0xffffffff80000000ULL)
3029e0aee726SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpudz, float128, uint64, f128, VsrD(0), 0x0ULL)
3030e0aee726SBharata B Rao VSX_CVT_FP_TO_INT_VECTOR(xscvqpuwz, float128, uint32, f128, VsrD(0), 0x0ULL)
303105590b92SBharata B Rao 
3032fa9ebf8cSDavid Gibson /*
3033fa9ebf8cSDavid Gibson  * VSX_CVT_INT_TO_FP - VSX integer to floating point conversion
3034fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
3035fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
3036fcf5ef2aSThomas Huth  *   stp   - source type (int32, uint32, int64 or uint64)
3037fcf5ef2aSThomas Huth  *   ttp   - target type (float32 or float64)
3038fcf5ef2aSThomas Huth  *   sfld  - source vsr_t field
3039fcf5ef2aSThomas Huth  *   tfld  - target vsr_t field
3040fcf5ef2aSThomas Huth  *   jdef  - definition of the j index (i or 2*i)
3041dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
3042fcf5ef2aSThomas Huth  */
3043dd657a35SVíctor Colombo #define VSX_CVT_INT_TO_FP(op, nels, stp, ttp, sfld, tfld, sfifprf, r2sp)\
304475cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)        \
3045fcf5ef2aSThomas Huth {                                                                       \
3046205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                  \
3047fcf5ef2aSThomas Huth     int i;                                                              \
3048fcf5ef2aSThomas Huth                                                                         \
3049fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                        \
3050cf3b0334SMark Cave-Ayland         t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);             \
3051fcf5ef2aSThomas Huth         if (r2sp) {                                                     \
30527238e55bSRichard Henderson             t.tfld = do_frsp(env, t.tfld, GETPC());                     \
3053fcf5ef2aSThomas Huth         }                                                               \
3054dd657a35SVíctor Colombo         if (sfifprf) {                                                  \
3055cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.tfld);                   \
3056fcf5ef2aSThomas Huth         }                                                               \
3057fcf5ef2aSThomas Huth     }                                                                   \
3058fcf5ef2aSThomas Huth                                                                         \
3059cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
3060dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                       \
3061fcf5ef2aSThomas Huth }
3062fcf5ef2aSThomas Huth 
3063fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvsxddp, 1, int64, float64, VsrD(0), VsrD(0), 1, 0)
3064fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvuxddp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 0)
3065fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvsxdsp, 1, int64, float64, VsrD(0), VsrD(0), 1, 1)
3066fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xscvuxdsp, 1, uint64, float64, VsrD(0), VsrD(0), 1, 1)
3067fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxddp, 2, int64, float64, VsrD(i), VsrD(i), 0, 0)
3068fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxddp, 2, uint64, float64, VsrD(i), VsrD(i), 0, 0)
3069fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxwdp, 2, int32, float64, VsrW(2 * i), VsrD(i), 0, 0)
3070fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxwdp, 2, uint64, float64, VsrW(2 * i), VsrD(i), 0, 0)
3071fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvsxwsp, 4, int32, float32, VsrW(i), VsrW(i), 0, 0)
3072fcf5ef2aSThomas Huth VSX_CVT_INT_TO_FP(xvcvuxwsp, 4, uint32, float32, VsrW(i), VsrW(i), 0, 0)
3073fcf5ef2aSThomas Huth 
30743515553bSLucas Coutinho #define VSX_CVT_INT_TO_FP2(op, stp, ttp)                                \
30753515553bSLucas Coutinho void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)        \
30763515553bSLucas Coutinho {                                                                       \
30773515553bSLucas Coutinho     ppc_vsr_t t = { };                                                  \
30783515553bSLucas Coutinho     int i;                                                              \
30793515553bSLucas Coutinho                                                                         \
30803515553bSLucas Coutinho     for (i = 0; i < 2; i++) {                                           \
30813515553bSLucas Coutinho         t.VsrW(2 * i) = stp##_to_##ttp(xb->VsrD(i), &env->fp_status);   \
30823515553bSLucas Coutinho         t.VsrW(2 * i + 1) = t.VsrW(2 * i);                              \
30833515553bSLucas Coutinho     }                                                                   \
30843515553bSLucas Coutinho                                                                         \
30853515553bSLucas Coutinho     *xt = t;                                                            \
30863278677fSVíctor Colombo     do_float_check_status(env, false, GETPC());                         \
30873515553bSLucas Coutinho }
30883515553bSLucas Coutinho 
30893515553bSLucas Coutinho VSX_CVT_INT_TO_FP2(xvcvsxdsp, int64, float32)
30903515553bSLucas Coutinho VSX_CVT_INT_TO_FP2(xvcvuxdsp, uint64, float32)
30913515553bSLucas Coutinho 
309267332e07SMatheus Ferst #define VSX_CVT_INT128_TO_FP(op, tp)                            \
309367332e07SMatheus Ferst void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)\
309467332e07SMatheus Ferst {                                                               \
309567332e07SMatheus Ferst     helper_reset_fpstatus(env);                                 \
309667332e07SMatheus Ferst     xt->f128 = tp##_to_float128(xb->s128, &env->fp_status);     \
309767332e07SMatheus Ferst     helper_compute_fprf_float128(env, xt->f128);                \
30983278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());                  \
309967332e07SMatheus Ferst }
310067332e07SMatheus Ferst 
310167332e07SMatheus Ferst VSX_CVT_INT128_TO_FP(XSCVUQQP, uint128);
310267332e07SMatheus Ferst VSX_CVT_INT128_TO_FP(XSCVSQQP, int128);
310367332e07SMatheus Ferst 
3104fa9ebf8cSDavid Gibson /*
3105fa9ebf8cSDavid Gibson  * VSX_CVT_INT_TO_FP_VECTOR - VSX integer to floating point conversion
310648ef23cbSBharata B Rao  *   op    - instruction mnemonic
310748ef23cbSBharata B Rao  *   stp   - source type (int32, uint32, int64 or uint64)
310848ef23cbSBharata B Rao  *   ttp   - target type (float32 or float64)
310948ef23cbSBharata B Rao  *   sfld  - source vsr_t field
311048ef23cbSBharata B Rao  *   tfld  - target vsr_t field
311148ef23cbSBharata B Rao  */
311248ef23cbSBharata B Rao #define VSX_CVT_INT_TO_FP_VECTOR(op, stp, ttp, sfld, tfld)              \
311399229620SMark Cave-Ayland void helper_##op(CPUPPCState *env, uint32_t opcode,                     \
311499229620SMark Cave-Ayland                  ppc_vsr_t *xt, ppc_vsr_t *xb)                          \
311548ef23cbSBharata B Rao {                                                                       \
3116cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;                                                  \
311748ef23cbSBharata B Rao                                                                         \
3118cf3b0334SMark Cave-Ayland     t.tfld = stp##_to_##ttp(xb->sfld, &env->fp_status);                 \
3119cf3b0334SMark Cave-Ayland     helper_compute_fprf_##ttp(env, t.tfld);                             \
312048ef23cbSBharata B Rao                                                                         \
3121cf3b0334SMark Cave-Ayland     *xt = t;                                                            \
31223278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());                          \
312348ef23cbSBharata B Rao }
312448ef23cbSBharata B Rao 
312548ef23cbSBharata B Rao VSX_CVT_INT_TO_FP_VECTOR(xscvsdqp, int64, float128, VsrD(0), f128)
312648ef23cbSBharata B Rao VSX_CVT_INT_TO_FP_VECTOR(xscvudqp, uint64, float128, VsrD(0), f128)
312748ef23cbSBharata B Rao 
3128fa9ebf8cSDavid Gibson /*
3129fa9ebf8cSDavid Gibson  * For "use current rounding mode", define a value that will not be
3130fa9ebf8cSDavid Gibson  * one of the existing rounding model enums.
3131fcf5ef2aSThomas Huth  */
3132fcf5ef2aSThomas Huth #define FLOAT_ROUND_CURRENT (float_round_nearest_even + float_round_down + \
3133fcf5ef2aSThomas Huth   float_round_up + float_round_to_zero)
3134fcf5ef2aSThomas Huth 
3135fa9ebf8cSDavid Gibson /*
3136fa9ebf8cSDavid Gibson  * VSX_ROUND - VSX floating point round
3137fcf5ef2aSThomas Huth  *   op    - instruction mnemonic
3138fcf5ef2aSThomas Huth  *   nels  - number of elements (1, 2 or 4)
3139fcf5ef2aSThomas Huth  *   tp    - type (float32 or float64)
3140fcf5ef2aSThomas Huth  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
3141fcf5ef2aSThomas Huth  *   rmode - rounding mode
3142dd657a35SVíctor Colombo  *   sfifprf - set FI and FPRF
3143fcf5ef2aSThomas Huth  */
3144dd657a35SVíctor Colombo #define VSX_ROUND(op, nels, tp, fld, rmode, sfifprf)                   \
314575cf84cbSMark Cave-Ayland void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)       \
3146fcf5ef2aSThomas Huth {                                                                      \
3147205eb5a8SVíctor Colombo     ppc_vsr_t t = { };                                                 \
3148fcf5ef2aSThomas Huth     int i;                                                             \
314963d06e90SBruno Larsen (billionai)     FloatRoundMode curr_rounding_mode;                                 \
3150fcf5ef2aSThomas Huth                                                                        \
3151fcf5ef2aSThomas Huth     if (rmode != FLOAT_ROUND_CURRENT) {                                \
315263d06e90SBruno Larsen (billionai)         curr_rounding_mode = get_float_rounding_mode(&env->fp_status); \
3153fcf5ef2aSThomas Huth         set_float_rounding_mode(rmode, &env->fp_status);               \
3154fcf5ef2aSThomas Huth     }                                                                  \
3155fcf5ef2aSThomas Huth                                                                        \
3156fcf5ef2aSThomas Huth     for (i = 0; i < nels; i++) {                                       \
3157cf3b0334SMark Cave-Ayland         if (unlikely(tp##_is_signaling_nan(xb->fld,                    \
3158fcf5ef2aSThomas Huth                                            &env->fp_status))) {        \
315913c9115fSRichard Henderson             float_invalid_op_vxsnan(env, GETPC());                     \
3160cf3b0334SMark Cave-Ayland             t.fld = tp##_snan_to_qnan(xb->fld);                        \
3161fcf5ef2aSThomas Huth         } else {                                                       \
3162cf3b0334SMark Cave-Ayland             t.fld = tp##_round_to_int(xb->fld, &env->fp_status);       \
3163fcf5ef2aSThomas Huth         }                                                              \
3164dd657a35SVíctor Colombo         if (sfifprf) {                                                 \
3165cf3b0334SMark Cave-Ayland             helper_compute_fprf_float64(env, t.fld);                   \
3166fcf5ef2aSThomas Huth         }                                                              \
3167fcf5ef2aSThomas Huth     }                                                                  \
3168fcf5ef2aSThomas Huth                                                                        \
3169fa9ebf8cSDavid Gibson     /*                                                                 \
3170fa9ebf8cSDavid Gibson      * If this is not a "use current rounding mode" instruction,       \
3171fcf5ef2aSThomas Huth      * then inhibit setting of the XX bit and restore rounding         \
3172fa9ebf8cSDavid Gibson      * mode from FPSCR                                                 \
3173fa9ebf8cSDavid Gibson      */                                                                \
3174fcf5ef2aSThomas Huth     if (rmode != FLOAT_ROUND_CURRENT) {                                \
317563d06e90SBruno Larsen (billionai)         set_float_rounding_mode(curr_rounding_mode, &env->fp_status);  \
3176fcf5ef2aSThomas Huth         env->fp_status.float_exception_flags &= ~float_flag_inexact;   \
3177fcf5ef2aSThomas Huth     }                                                                  \
3178fcf5ef2aSThomas Huth                                                                        \
3179cf3b0334SMark Cave-Ayland     *xt = t;                                                           \
3180dd657a35SVíctor Colombo     do_float_check_status(env, sfifprf, GETPC());                      \
3181fcf5ef2aSThomas Huth }
3182fcf5ef2aSThomas Huth 
3183fcf5ef2aSThomas Huth VSX_ROUND(xsrdpi, 1, float64, VsrD(0), float_round_ties_away, 1)
3184fcf5ef2aSThomas Huth VSX_ROUND(xsrdpic, 1, float64, VsrD(0), FLOAT_ROUND_CURRENT, 1)
3185fcf5ef2aSThomas Huth VSX_ROUND(xsrdpim, 1, float64, VsrD(0), float_round_down, 1)
3186fcf5ef2aSThomas Huth VSX_ROUND(xsrdpip, 1, float64, VsrD(0), float_round_up, 1)
3187fcf5ef2aSThomas Huth VSX_ROUND(xsrdpiz, 1, float64, VsrD(0), float_round_to_zero, 1)
3188fcf5ef2aSThomas Huth 
3189fcf5ef2aSThomas Huth VSX_ROUND(xvrdpi, 2, float64, VsrD(i), float_round_ties_away, 0)
3190fcf5ef2aSThomas Huth VSX_ROUND(xvrdpic, 2, float64, VsrD(i), FLOAT_ROUND_CURRENT, 0)
3191fcf5ef2aSThomas Huth VSX_ROUND(xvrdpim, 2, float64, VsrD(i), float_round_down, 0)
3192fcf5ef2aSThomas Huth VSX_ROUND(xvrdpip, 2, float64, VsrD(i), float_round_up, 0)
3193fcf5ef2aSThomas Huth VSX_ROUND(xvrdpiz, 2, float64, VsrD(i), float_round_to_zero, 0)
3194fcf5ef2aSThomas Huth 
3195fcf5ef2aSThomas Huth VSX_ROUND(xvrspi, 4, float32, VsrW(i), float_round_ties_away, 0)
3196fcf5ef2aSThomas Huth VSX_ROUND(xvrspic, 4, float32, VsrW(i), FLOAT_ROUND_CURRENT, 0)
3197fcf5ef2aSThomas Huth VSX_ROUND(xvrspim, 4, float32, VsrW(i), float_round_down, 0)
3198fcf5ef2aSThomas Huth VSX_ROUND(xvrspip, 4, float32, VsrW(i), float_round_up, 0)
3199fcf5ef2aSThomas Huth VSX_ROUND(xvrspiz, 4, float32, VsrW(i), float_round_to_zero, 0)
3200fcf5ef2aSThomas Huth 
3201fcf5ef2aSThomas Huth uint64_t helper_xsrsp(CPUPPCState *env, uint64_t xb)
3202fcf5ef2aSThomas Huth {
3203fcf5ef2aSThomas Huth     helper_reset_fpstatus(env);
3204fcf5ef2aSThomas Huth 
32057238e55bSRichard Henderson     uint64_t xt = do_frsp(env, xb, GETPC());
3206fcf5ef2aSThomas Huth 
3207ffc67420SBharata B Rao     helper_compute_fprf_float64(env, xt);
32083278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
3209fcf5ef2aSThomas Huth     return xt;
3210fcf5ef2aSThomas Huth }
3211234068abSBharata B Rao 
3212c36ab970SMatheus Ferst void helper_XVXSIGSP(ppc_vsr_t *xt, ppc_vsr_t *xb)
3213c5969d2eSNikunj A Dadhania {
3214cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3215c5969d2eSNikunj A Dadhania     uint32_t exp, i, fraction;
3216c5969d2eSNikunj A Dadhania 
3217c5969d2eSNikunj A Dadhania     for (i = 0; i < 4; i++) {
3218cf3b0334SMark Cave-Ayland         exp = (xb->VsrW(i) >> 23) & 0xFF;
3219cf3b0334SMark Cave-Ayland         fraction = xb->VsrW(i) & 0x7FFFFF;
3220c5969d2eSNikunj A Dadhania         if (exp != 0 && exp != 255) {
3221cf3b0334SMark Cave-Ayland             t.VsrW(i) = fraction | 0x00800000;
3222c5969d2eSNikunj A Dadhania         } else {
3223cf3b0334SMark Cave-Ayland             t.VsrW(i) = fraction;
3224c5969d2eSNikunj A Dadhania         }
3225c5969d2eSNikunj A Dadhania     }
3226cf3b0334SMark Cave-Ayland     *xt = t;
3227c5969d2eSNikunj A Dadhania }
3228403a884aSNikunj A Dadhania 
3229fa9ebf8cSDavid Gibson /*
3230fa9ebf8cSDavid Gibson  * VSX_TEST_DC - VSX floating point test data class
3231403a884aSNikunj A Dadhania  *   op    - instruction mnemonic
3232403a884aSNikunj A Dadhania  *   nels  - number of elements (1, 2 or 4)
3233403a884aSNikunj A Dadhania  *   xbn   - VSR register number
3234403a884aSNikunj A Dadhania  *   tp    - type (float32 or float64)
3235403a884aSNikunj A Dadhania  *   fld   - vsr_t field (VsrD(*) or VsrW(*))
3236403a884aSNikunj A Dadhania  *   tfld   - target vsr_t field (VsrD(*) or VsrW(*))
3237403a884aSNikunj A Dadhania  *   fld_max - target field max
323878241762SNikunj A Dadhania  *   scrf - set result in CR and FPCC
3239403a884aSNikunj A Dadhania  */
324078241762SNikunj A Dadhania #define VSX_TEST_DC(op, nels, xbn, tp, fld, tfld, fld_max, scrf)  \
3241403a884aSNikunj A Dadhania void helper_##op(CPUPPCState *env, uint32_t opcode)         \
3242403a884aSNikunj A Dadhania {                                                           \
3243cf3b0334SMark Cave-Ayland     ppc_vsr_t *xt = &env->vsr[xT(opcode)];                  \
3244cf3b0334SMark Cave-Ayland     ppc_vsr_t *xb = &env->vsr[xbn];                         \
3245cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };                                      \
3246403a884aSNikunj A Dadhania     uint32_t i, sign, dcmx;                                 \
324778241762SNikunj A Dadhania     uint32_t cc, match = 0;                                 \
3248403a884aSNikunj A Dadhania                                                             \
324978241762SNikunj A Dadhania     if (!scrf) {                                            \
3250403a884aSNikunj A Dadhania         dcmx = DCMX_XV(opcode);                             \
325178241762SNikunj A Dadhania     } else {                                                \
3252cf3b0334SMark Cave-Ayland         t = *xt;                                            \
325378241762SNikunj A Dadhania         dcmx = DCMX(opcode);                                \
325478241762SNikunj A Dadhania     }                                                       \
3255403a884aSNikunj A Dadhania                                                             \
3256403a884aSNikunj A Dadhania     for (i = 0; i < nels; i++) {                            \
3257cf3b0334SMark Cave-Ayland         sign = tp##_is_neg(xb->fld);                        \
3258cf3b0334SMark Cave-Ayland         if (tp##_is_any_nan(xb->fld)) {                     \
3259403a884aSNikunj A Dadhania             match = extract32(dcmx, 6, 1);                  \
3260cf3b0334SMark Cave-Ayland         } else if (tp##_is_infinity(xb->fld)) {             \
3261403a884aSNikunj A Dadhania             match = extract32(dcmx, 4 + !sign, 1);          \
3262cf3b0334SMark Cave-Ayland         } else if (tp##_is_zero(xb->fld)) {                 \
3263403a884aSNikunj A Dadhania             match = extract32(dcmx, 2 + !sign, 1);          \
3264cf3b0334SMark Cave-Ayland         } else if (tp##_is_zero_or_denormal(xb->fld)) {     \
3265403a884aSNikunj A Dadhania             match = extract32(dcmx, 0 + !sign, 1);          \
3266403a884aSNikunj A Dadhania         }                                                   \
326778241762SNikunj A Dadhania                                                             \
326878241762SNikunj A Dadhania         if (scrf) {                                         \
326978241762SNikunj A Dadhania             cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT;  \
32705c94dd38SPaul A. Clarke             env->fpscr &= ~FP_FPCC;                         \
32715c94dd38SPaul A. Clarke             env->fpscr |= cc << FPSCR_FPCC;                 \
327278241762SNikunj A Dadhania             env->crf[BF(opcode)] = cc;                      \
327378241762SNikunj A Dadhania         } else {                                            \
3274cf3b0334SMark Cave-Ayland             t.tfld = match ? fld_max : 0;                   \
327578241762SNikunj A Dadhania         }                                                   \
3276403a884aSNikunj A Dadhania         match = 0;                                          \
3277403a884aSNikunj A Dadhania     }                                                       \
327878241762SNikunj A Dadhania     if (!scrf) {                                            \
3279cf3b0334SMark Cave-Ayland         *xt = t;                                            \
328078241762SNikunj A Dadhania     }                                                       \
3281403a884aSNikunj A Dadhania }
3282403a884aSNikunj A Dadhania 
328378241762SNikunj A Dadhania VSX_TEST_DC(xvtstdcdp, 2, xB(opcode), float64, VsrD(i), VsrD(i), UINT64_MAX, 0)
328478241762SNikunj A Dadhania VSX_TEST_DC(xvtstdcsp, 4, xB(opcode), float32, VsrW(i), VsrW(i), UINT32_MAX, 0)
328578241762SNikunj A Dadhania VSX_TEST_DC(xststdcdp, 1, xB(opcode), float64, VsrD(0), VsrD(0), 0, 1)
328678241762SNikunj A Dadhania VSX_TEST_DC(xststdcqp, 1, (rB(opcode) + 32), float128, f128, VsrD(0), 0, 1)
328778241762SNikunj A Dadhania 
32888d830485SMark Cave-Ayland void helper_xststdcsp(CPUPPCState *env, uint32_t opcode, ppc_vsr_t *xb)
328978241762SNikunj A Dadhania {
329078241762SNikunj A Dadhania     uint32_t dcmx, sign, exp;
329178241762SNikunj A Dadhania     uint32_t cc, match = 0, not_sp = 0;
3292a1f1c731SRichard Henderson     float64 arg = xb->VsrD(0);
3293a1f1c731SRichard Henderson     float64 arg_sp;
329478241762SNikunj A Dadhania 
329578241762SNikunj A Dadhania     dcmx = DCMX(opcode);
3296a1f1c731SRichard Henderson     exp = (arg >> 52) & 0x7FF;
3297a1f1c731SRichard Henderson     sign = float64_is_neg(arg);
329878241762SNikunj A Dadhania 
3299a1f1c731SRichard Henderson     if (float64_is_any_nan(arg)) {
330078241762SNikunj A Dadhania         match = extract32(dcmx, 6, 1);
3301a1f1c731SRichard Henderson     } else if (float64_is_infinity(arg)) {
330278241762SNikunj A Dadhania         match = extract32(dcmx, 4 + !sign, 1);
3303a1f1c731SRichard Henderson     } else if (float64_is_zero(arg)) {
330478241762SNikunj A Dadhania         match = extract32(dcmx, 2 + !sign, 1);
3305a1f1c731SRichard Henderson     } else if (float64_is_zero_or_denormal(arg) || (exp > 0 && exp < 0x381)) {
330678241762SNikunj A Dadhania         match = extract32(dcmx, 0 + !sign, 1);
330778241762SNikunj A Dadhania     }
330878241762SNikunj A Dadhania 
3309a1f1c731SRichard Henderson     arg_sp = helper_todouble(helper_tosingle(arg));
3310a1f1c731SRichard Henderson     not_sp = arg != arg_sp;
331178241762SNikunj A Dadhania 
331278241762SNikunj A Dadhania     cc = sign << CRF_LT_BIT | match << CRF_EQ_BIT | not_sp << CRF_SO_BIT;
33135c94dd38SPaul A. Clarke     env->fpscr &= ~FP_FPCC;
33145c94dd38SPaul A. Clarke     env->fpscr |= cc << FPSCR_FPCC;
331578241762SNikunj A Dadhania     env->crf[BF(opcode)] = cc;
331678241762SNikunj A Dadhania }
3317be07ad58SJose Ricardo Ziviani 
331899229620SMark Cave-Ayland void helper_xsrqpi(CPUPPCState *env, uint32_t opcode,
331999229620SMark Cave-Ayland                    ppc_vsr_t *xt, ppc_vsr_t *xb)
3320be07ad58SJose Ricardo Ziviani {
3321cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3322be07ad58SJose Ricardo Ziviani     uint8_t r = Rrm(opcode);
3323be07ad58SJose Ricardo Ziviani     uint8_t ex = Rc(opcode);
3324be07ad58SJose Ricardo Ziviani     uint8_t rmc = RMC(opcode);
3325be07ad58SJose Ricardo Ziviani     uint8_t rmode = 0;
3326be07ad58SJose Ricardo Ziviani     float_status tstat;
3327be07ad58SJose Ricardo Ziviani 
3328be07ad58SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3329be07ad58SJose Ricardo Ziviani 
3330be07ad58SJose Ricardo Ziviani     if (r == 0 && rmc == 0) {
3331be07ad58SJose Ricardo Ziviani         rmode = float_round_ties_away;
3332be07ad58SJose Ricardo Ziviani     } else if (r == 0 && rmc == 0x3) {
3333208d8033SVíctor Colombo         rmode = env->fpscr & FP_RN;
3334be07ad58SJose Ricardo Ziviani     } else if (r == 1) {
3335be07ad58SJose Ricardo Ziviani         switch (rmc) {
3336be07ad58SJose Ricardo Ziviani         case 0:
3337be07ad58SJose Ricardo Ziviani             rmode = float_round_nearest_even;
3338be07ad58SJose Ricardo Ziviani             break;
3339be07ad58SJose Ricardo Ziviani         case 1:
3340be07ad58SJose Ricardo Ziviani             rmode = float_round_to_zero;
3341be07ad58SJose Ricardo Ziviani             break;
3342be07ad58SJose Ricardo Ziviani         case 2:
3343be07ad58SJose Ricardo Ziviani             rmode = float_round_up;
3344be07ad58SJose Ricardo Ziviani             break;
3345be07ad58SJose Ricardo Ziviani         case 3:
3346be07ad58SJose Ricardo Ziviani             rmode = float_round_down;
3347be07ad58SJose Ricardo Ziviani             break;
3348be07ad58SJose Ricardo Ziviani         default:
3349be07ad58SJose Ricardo Ziviani             abort();
3350be07ad58SJose Ricardo Ziviani         }
3351be07ad58SJose Ricardo Ziviani     }
3352be07ad58SJose Ricardo Ziviani 
3353be07ad58SJose Ricardo Ziviani     tstat = env->fp_status;
3354be07ad58SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3355be07ad58SJose Ricardo Ziviani     set_float_rounding_mode(rmode, &tstat);
3356cf3b0334SMark Cave-Ayland     t.f128 = float128_round_to_int(xb->f128, &tstat);
3357be07ad58SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3358be07ad58SJose Ricardo Ziviani 
3359053e23a6SRichard Henderson     if (unlikely(tstat.float_exception_flags & float_flag_invalid_snan)) {
336013c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
3361be07ad58SJose Ricardo Ziviani     }
3362be07ad58SJose Ricardo Ziviani 
3363be07ad58SJose Ricardo Ziviani     if (ex == 0 && (tstat.float_exception_flags & float_flag_inexact)) {
3364be07ad58SJose Ricardo Ziviani         env->fp_status.float_exception_flags &= ~float_flag_inexact;
3365be07ad58SJose Ricardo Ziviani     }
3366be07ad58SJose Ricardo Ziviani 
3367cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
33683278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
3369cf3b0334SMark Cave-Ayland     *xt = t;
3370be07ad58SJose Ricardo Ziviani }
3371917950d7SJose Ricardo Ziviani 
337299229620SMark Cave-Ayland void helper_xsrqpxp(CPUPPCState *env, uint32_t opcode,
337399229620SMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xb)
3374917950d7SJose Ricardo Ziviani {
3375cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3376917950d7SJose Ricardo Ziviani     uint8_t r = Rrm(opcode);
3377917950d7SJose Ricardo Ziviani     uint8_t rmc = RMC(opcode);
3378917950d7SJose Ricardo Ziviani     uint8_t rmode = 0;
3379917950d7SJose Ricardo Ziviani     floatx80 round_res;
3380917950d7SJose Ricardo Ziviani     float_status tstat;
3381917950d7SJose Ricardo Ziviani 
3382917950d7SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3383917950d7SJose Ricardo Ziviani 
3384917950d7SJose Ricardo Ziviani     if (r == 0 && rmc == 0) {
3385917950d7SJose Ricardo Ziviani         rmode = float_round_ties_away;
3386917950d7SJose Ricardo Ziviani     } else if (r == 0 && rmc == 0x3) {
3387208d8033SVíctor Colombo         rmode = env->fpscr & FP_RN;
3388917950d7SJose Ricardo Ziviani     } else if (r == 1) {
3389917950d7SJose Ricardo Ziviani         switch (rmc) {
3390917950d7SJose Ricardo Ziviani         case 0:
3391917950d7SJose Ricardo Ziviani             rmode = float_round_nearest_even;
3392917950d7SJose Ricardo Ziviani             break;
3393917950d7SJose Ricardo Ziviani         case 1:
3394917950d7SJose Ricardo Ziviani             rmode = float_round_to_zero;
3395917950d7SJose Ricardo Ziviani             break;
3396917950d7SJose Ricardo Ziviani         case 2:
3397917950d7SJose Ricardo Ziviani             rmode = float_round_up;
3398917950d7SJose Ricardo Ziviani             break;
3399917950d7SJose Ricardo Ziviani         case 3:
3400917950d7SJose Ricardo Ziviani             rmode = float_round_down;
3401917950d7SJose Ricardo Ziviani             break;
3402917950d7SJose Ricardo Ziviani         default:
3403917950d7SJose Ricardo Ziviani             abort();
3404917950d7SJose Ricardo Ziviani         }
3405917950d7SJose Ricardo Ziviani     }
3406917950d7SJose Ricardo Ziviani 
3407917950d7SJose Ricardo Ziviani     tstat = env->fp_status;
3408917950d7SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3409917950d7SJose Ricardo Ziviani     set_float_rounding_mode(rmode, &tstat);
3410cf3b0334SMark Cave-Ayland     round_res = float128_to_floatx80(xb->f128, &tstat);
3411cf3b0334SMark Cave-Ayland     t.f128 = floatx80_to_float128(round_res, &tstat);
3412917950d7SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3413917950d7SJose Ricardo Ziviani 
3414053e23a6SRichard Henderson     if (unlikely(tstat.float_exception_flags & float_flag_invalid_snan)) {
341513c9115fSRichard Henderson         float_invalid_op_vxsnan(env, GETPC());
3416cf3b0334SMark Cave-Ayland         t.f128 = float128_snan_to_qnan(t.f128);
3417917950d7SJose Ricardo Ziviani     }
3418917950d7SJose Ricardo Ziviani 
3419cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3420cf3b0334SMark Cave-Ayland     *xt = t;
34213278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
3422917950d7SJose Ricardo Ziviani }
3423a4a68476SJose Ricardo Ziviani 
342499229620SMark Cave-Ayland void helper_xssqrtqp(CPUPPCState *env, uint32_t opcode,
342599229620SMark Cave-Ayland                      ppc_vsr_t *xt, ppc_vsr_t *xb)
3426a4a68476SJose Ricardo Ziviani {
3427cf3b0334SMark Cave-Ayland     ppc_vsr_t t = { };
3428a4a68476SJose Ricardo Ziviani     float_status tstat;
3429a4a68476SJose Ricardo Ziviani 
3430a4a68476SJose Ricardo Ziviani     helper_reset_fpstatus(env);
3431a4a68476SJose Ricardo Ziviani 
3432a8d411abSBharata B Rao     tstat = env->fp_status;
3433a4a68476SJose Ricardo Ziviani     if (unlikely(Rc(opcode) != 0)) {
3434a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
3435a4a68476SJose Ricardo Ziviani     }
3436a4a68476SJose Ricardo Ziviani 
3437a4a68476SJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3438cf3b0334SMark Cave-Ayland     t.f128 = float128_sqrt(xb->f128, &tstat);
3439a4a68476SJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3440a4a68476SJose Ricardo Ziviani 
3441a4a68476SJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
34423d3050ccSRichard Henderson         float_invalid_op_sqrt(env, tstat.float_exception_flags, 1, GETPC());
3443a4a68476SJose Ricardo Ziviani     }
3444a4a68476SJose Ricardo Ziviani 
3445cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3446cf3b0334SMark Cave-Ayland     *xt = t;
34473278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
3448a4a68476SJose Ricardo Ziviani }
3449f6b99afdSJose Ricardo Ziviani 
345023d0766bSMark Cave-Ayland void helper_xssubqp(CPUPPCState *env, uint32_t opcode,
345123d0766bSMark Cave-Ayland                     ppc_vsr_t *xt, ppc_vsr_t *xa, ppc_vsr_t *xb)
3452f6b99afdSJose Ricardo Ziviani {
3453cf3b0334SMark Cave-Ayland     ppc_vsr_t t = *xt;
3454f6b99afdSJose Ricardo Ziviani     float_status tstat;
3455f6b99afdSJose Ricardo Ziviani 
3456f6b99afdSJose Ricardo Ziviani     helper_reset_fpstatus(env);
3457f6b99afdSJose Ricardo Ziviani 
3458a8d411abSBharata B Rao     tstat = env->fp_status;
3459f6b99afdSJose Ricardo Ziviani     if (unlikely(Rc(opcode) != 0)) {
3460a8d411abSBharata B Rao         tstat.float_rounding_mode = float_round_to_odd;
3461f6b99afdSJose Ricardo Ziviani     }
3462f6b99afdSJose Ricardo Ziviani 
3463f6b99afdSJose Ricardo Ziviani     set_float_exception_flags(0, &tstat);
3464cf3b0334SMark Cave-Ayland     t.f128 = float128_sub(xa->f128, xb->f128, &tstat);
3465f6b99afdSJose Ricardo Ziviani     env->fp_status.float_exception_flags |= tstat.float_exception_flags;
3466f6b99afdSJose Ricardo Ziviani 
3467f6b99afdSJose Ricardo Ziviani     if (unlikely(tstat.float_exception_flags & float_flag_invalid)) {
3468941298ecSRichard Henderson         float_invalid_op_addsub(env, tstat.float_exception_flags, 1, GETPC());
3469f6b99afdSJose Ricardo Ziviani     }
3470f6b99afdSJose Ricardo Ziviani 
3471cf3b0334SMark Cave-Ayland     helper_compute_fprf_float128(env, t.f128);
3472cf3b0334SMark Cave-Ayland     *xt = t;
34733278677fSVíctor Colombo     do_float_check_status(env, true, GETPC());
3474f6b99afdSJose Ricardo Ziviani }
3475c29018ccSLucas Mateus Castro (alqotel) 
3476c29018ccSLucas Mateus Castro (alqotel) static inline void vsxger_excp(CPUPPCState *env, uintptr_t retaddr)
3477c29018ccSLucas Mateus Castro (alqotel) {
3478c29018ccSLucas Mateus Castro (alqotel)     /*
3479c29018ccSLucas Mateus Castro (alqotel)      * XV*GER instructions execute and set the FPSCR as if exceptions
3480c29018ccSLucas Mateus Castro (alqotel)      * are disabled and only at the end throw an exception
3481c29018ccSLucas Mateus Castro (alqotel)      */
3482c29018ccSLucas Mateus Castro (alqotel)     target_ulong enable;
3483c29018ccSLucas Mateus Castro (alqotel)     enable = env->fpscr & (FP_ENABLES | FP_FI | FP_FR);
3484c29018ccSLucas Mateus Castro (alqotel)     env->fpscr &= ~(FP_ENABLES | FP_FI | FP_FR);
3485c29018ccSLucas Mateus Castro (alqotel)     int status = get_float_exception_flags(&env->fp_status);
3486c29018ccSLucas Mateus Castro (alqotel)     if (unlikely(status & float_flag_invalid)) {
3487c29018ccSLucas Mateus Castro (alqotel)         if (status & float_flag_invalid_snan) {
3488c29018ccSLucas Mateus Castro (alqotel)             float_invalid_op_vxsnan(env, 0);
3489c29018ccSLucas Mateus Castro (alqotel)         }
3490c29018ccSLucas Mateus Castro (alqotel)         if (status & float_flag_invalid_imz) {
3491c29018ccSLucas Mateus Castro (alqotel)             float_invalid_op_vximz(env, false, 0);
3492c29018ccSLucas Mateus Castro (alqotel)         }
3493c29018ccSLucas Mateus Castro (alqotel)         if (status & float_flag_invalid_isi) {
3494c29018ccSLucas Mateus Castro (alqotel)             float_invalid_op_vxisi(env, false, 0);
3495c29018ccSLucas Mateus Castro (alqotel)         }
3496c29018ccSLucas Mateus Castro (alqotel)     }
3497c29018ccSLucas Mateus Castro (alqotel)     do_float_check_status(env, false, retaddr);
3498c29018ccSLucas Mateus Castro (alqotel)     env->fpscr |= enable;
3499c29018ccSLucas Mateus Castro (alqotel)     do_fpscr_check_status(env, retaddr);
3500c29018ccSLucas Mateus Castro (alqotel) }
3501c29018ccSLucas Mateus Castro (alqotel) 
35022d9cba74SLucas Mateus Castro (alqotel) typedef float64 extract_f16(float16, float_status *);
35032d9cba74SLucas Mateus Castro (alqotel) 
35042d9cba74SLucas Mateus Castro (alqotel) static float64 extract_hf16(float16 in, float_status *fp_status)
35052d9cba74SLucas Mateus Castro (alqotel) {
35062d9cba74SLucas Mateus Castro (alqotel)     return float16_to_float64(in, true, fp_status);
35072d9cba74SLucas Mateus Castro (alqotel) }
35082d9cba74SLucas Mateus Castro (alqotel) 
35095724e131SLucas Mateus Castro (alqotel) static float64 extract_bf16(bfloat16 in, float_status *fp_status)
35105724e131SLucas Mateus Castro (alqotel) {
35115724e131SLucas Mateus Castro (alqotel)     return bfloat16_to_float64(in, fp_status);
35125724e131SLucas Mateus Castro (alqotel) }
35135724e131SLucas Mateus Castro (alqotel) 
35142d9cba74SLucas Mateus Castro (alqotel) static void vsxger16(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
35152d9cba74SLucas Mateus Castro (alqotel)                      ppc_acc_t  *at, uint32_t mask, bool acc,
35162d9cba74SLucas Mateus Castro (alqotel)                      bool neg_mul, bool neg_acc, extract_f16 extract)
35172d9cba74SLucas Mateus Castro (alqotel) {
35182d9cba74SLucas Mateus Castro (alqotel)     float32 r, aux_acc;
35192d9cba74SLucas Mateus Castro (alqotel)     float64 psum, va, vb, vc, vd;
35202d9cba74SLucas Mateus Castro (alqotel)     int i, j, xmsk_bit, ymsk_bit;
35212d9cba74SLucas Mateus Castro (alqotel)     uint8_t pmsk = FIELD_EX32(mask, GER_MSK, PMSK),
35222d9cba74SLucas Mateus Castro (alqotel)             xmsk = FIELD_EX32(mask, GER_MSK, XMSK),
35232d9cba74SLucas Mateus Castro (alqotel)             ymsk = FIELD_EX32(mask, GER_MSK, YMSK);
35242d9cba74SLucas Mateus Castro (alqotel)     float_status *excp_ptr = &env->fp_status;
35252d9cba74SLucas Mateus Castro (alqotel)     for (i = 0, xmsk_bit = 1 << 3; i < 4; i++, xmsk_bit >>= 1) {
35262d9cba74SLucas Mateus Castro (alqotel)         for (j = 0, ymsk_bit = 1 << 3; j < 4; j++, ymsk_bit >>= 1) {
35272d9cba74SLucas Mateus Castro (alqotel)             if ((xmsk_bit & xmsk) && (ymsk_bit & ymsk)) {
35282d9cba74SLucas Mateus Castro (alqotel)                 va = !(pmsk & 2) ? float64_zero :
35292d9cba74SLucas Mateus Castro (alqotel)                                    extract(a->VsrHF(2 * i), excp_ptr);
35302d9cba74SLucas Mateus Castro (alqotel)                 vb = !(pmsk & 2) ? float64_zero :
35312d9cba74SLucas Mateus Castro (alqotel)                                    extract(b->VsrHF(2 * j), excp_ptr);
35322d9cba74SLucas Mateus Castro (alqotel)                 vc = !(pmsk & 1) ? float64_zero :
35332d9cba74SLucas Mateus Castro (alqotel)                                    extract(a->VsrHF(2 * i + 1), excp_ptr);
35342d9cba74SLucas Mateus Castro (alqotel)                 vd = !(pmsk & 1) ? float64_zero :
35352d9cba74SLucas Mateus Castro (alqotel)                                    extract(b->VsrHF(2 * j + 1), excp_ptr);
35362d9cba74SLucas Mateus Castro (alqotel)                 psum = float64_mul(va, vb, excp_ptr);
35372d9cba74SLucas Mateus Castro (alqotel)                 psum = float64r32_muladd(vc, vd, psum, 0, excp_ptr);
35382d9cba74SLucas Mateus Castro (alqotel)                 r = float64_to_float32(psum, excp_ptr);
35392d9cba74SLucas Mateus Castro (alqotel)                 if (acc) {
35402d9cba74SLucas Mateus Castro (alqotel)                     aux_acc = at[i].VsrSF(j);
35412d9cba74SLucas Mateus Castro (alqotel)                     if (neg_mul) {
35422d9cba74SLucas Mateus Castro (alqotel)                         r = bfp32_neg(r);
35432d9cba74SLucas Mateus Castro (alqotel)                     }
35442d9cba74SLucas Mateus Castro (alqotel)                     if (neg_acc) {
35452d9cba74SLucas Mateus Castro (alqotel)                         aux_acc = bfp32_neg(aux_acc);
35462d9cba74SLucas Mateus Castro (alqotel)                     }
35472d9cba74SLucas Mateus Castro (alqotel)                     r = float32_add(r, aux_acc, excp_ptr);
35482d9cba74SLucas Mateus Castro (alqotel)                 }
35492d9cba74SLucas Mateus Castro (alqotel)                 at[i].VsrSF(j) = r;
35502d9cba74SLucas Mateus Castro (alqotel)             } else {
35512d9cba74SLucas Mateus Castro (alqotel)                 at[i].VsrSF(j) = float32_zero;
35522d9cba74SLucas Mateus Castro (alqotel)             }
35532d9cba74SLucas Mateus Castro (alqotel)         }
35542d9cba74SLucas Mateus Castro (alqotel)     }
35552d9cba74SLucas Mateus Castro (alqotel)     vsxger_excp(env, GETPC());
35562d9cba74SLucas Mateus Castro (alqotel) }
35572d9cba74SLucas Mateus Castro (alqotel) 
3558c29018ccSLucas Mateus Castro (alqotel) typedef void vsxger_zero(ppc_vsr_t *at, int, int);
3559c29018ccSLucas Mateus Castro (alqotel) 
3560c29018ccSLucas Mateus Castro (alqotel) typedef void vsxger_muladd_f(ppc_vsr_t *, ppc_vsr_t *, ppc_vsr_t *, int, int,
3561c29018ccSLucas Mateus Castro (alqotel)                              int flags, float_status *s);
3562c29018ccSLucas Mateus Castro (alqotel) 
3563c29018ccSLucas Mateus Castro (alqotel) static void vsxger_muladd32(ppc_vsr_t *at, ppc_vsr_t *a, ppc_vsr_t *b, int i,
3564c29018ccSLucas Mateus Castro (alqotel)                             int j, int flags, float_status *s)
3565c29018ccSLucas Mateus Castro (alqotel) {
3566c29018ccSLucas Mateus Castro (alqotel)     at[i].VsrSF(j) = float32_muladd(a->VsrSF(i), b->VsrSF(j),
3567c29018ccSLucas Mateus Castro (alqotel)                                     at[i].VsrSF(j), flags, s);
3568c29018ccSLucas Mateus Castro (alqotel) }
3569c29018ccSLucas Mateus Castro (alqotel) 
3570c29018ccSLucas Mateus Castro (alqotel) static void vsxger_mul32(ppc_vsr_t *at, ppc_vsr_t *a, ppc_vsr_t *b, int i,
3571c29018ccSLucas Mateus Castro (alqotel)                          int j, int flags, float_status *s)
3572c29018ccSLucas Mateus Castro (alqotel) {
3573c29018ccSLucas Mateus Castro (alqotel)     at[i].VsrSF(j) = float32_mul(a->VsrSF(i), b->VsrSF(j), s);
3574c29018ccSLucas Mateus Castro (alqotel) }
3575c29018ccSLucas Mateus Castro (alqotel) 
3576c29018ccSLucas Mateus Castro (alqotel) static void vsxger_zero32(ppc_vsr_t *at, int i, int j)
3577c29018ccSLucas Mateus Castro (alqotel) {
3578c29018ccSLucas Mateus Castro (alqotel)     at[i].VsrSF(j) = float32_zero;
3579c29018ccSLucas Mateus Castro (alqotel) }
3580c29018ccSLucas Mateus Castro (alqotel) 
3581c29018ccSLucas Mateus Castro (alqotel) static void vsxger_muladd64(ppc_vsr_t *at, ppc_vsr_t *a, ppc_vsr_t *b, int i,
3582c29018ccSLucas Mateus Castro (alqotel)                             int j, int flags, float_status *s)
3583c29018ccSLucas Mateus Castro (alqotel) {
3584c29018ccSLucas Mateus Castro (alqotel)     if (j >= 2) {
3585c29018ccSLucas Mateus Castro (alqotel)         j -= 2;
3586c29018ccSLucas Mateus Castro (alqotel)         at[i].VsrDF(j) = float64_muladd(a[i / 2].VsrDF(i % 2), b->VsrDF(j),
3587c29018ccSLucas Mateus Castro (alqotel)                                         at[i].VsrDF(j), flags, s);
3588c29018ccSLucas Mateus Castro (alqotel)     }
3589c29018ccSLucas Mateus Castro (alqotel) }
3590c29018ccSLucas Mateus Castro (alqotel) 
3591c29018ccSLucas Mateus Castro (alqotel) static void vsxger_mul64(ppc_vsr_t *at, ppc_vsr_t *a, ppc_vsr_t *b, int i,
3592c29018ccSLucas Mateus Castro (alqotel)                          int j, int flags, float_status *s)
3593c29018ccSLucas Mateus Castro (alqotel) {
3594c29018ccSLucas Mateus Castro (alqotel)     if (j >= 2) {
3595c29018ccSLucas Mateus Castro (alqotel)         j -= 2;
3596c29018ccSLucas Mateus Castro (alqotel)         at[i].VsrDF(j) = float64_mul(a[i / 2].VsrDF(i % 2), b->VsrDF(j), s);
3597c29018ccSLucas Mateus Castro (alqotel)     }
3598c29018ccSLucas Mateus Castro (alqotel) }
3599c29018ccSLucas Mateus Castro (alqotel) 
3600c29018ccSLucas Mateus Castro (alqotel) static void vsxger_zero64(ppc_vsr_t *at, int i, int j)
3601c29018ccSLucas Mateus Castro (alqotel) {
3602c29018ccSLucas Mateus Castro (alqotel)     if (j >= 2) {
3603c29018ccSLucas Mateus Castro (alqotel)         j -= 2;
3604c29018ccSLucas Mateus Castro (alqotel)         at[i].VsrDF(j) = float64_zero;
3605c29018ccSLucas Mateus Castro (alqotel)     }
3606c29018ccSLucas Mateus Castro (alqotel) }
3607c29018ccSLucas Mateus Castro (alqotel) 
3608c29018ccSLucas Mateus Castro (alqotel) static void vsxger(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3609c29018ccSLucas Mateus Castro (alqotel)                    ppc_acc_t  *at, uint32_t mask, bool acc, bool neg_mul,
3610c29018ccSLucas Mateus Castro (alqotel)                    bool neg_acc, vsxger_muladd_f mul, vsxger_muladd_f muladd,
3611c29018ccSLucas Mateus Castro (alqotel)                    vsxger_zero zero)
3612c29018ccSLucas Mateus Castro (alqotel) {
3613c29018ccSLucas Mateus Castro (alqotel)     int i, j, xmsk_bit, ymsk_bit, op_flags;
3614c29018ccSLucas Mateus Castro (alqotel)     uint8_t xmsk = mask & 0x0F;
3615c29018ccSLucas Mateus Castro (alqotel)     uint8_t ymsk = (mask >> 4) & 0x0F;
3616c29018ccSLucas Mateus Castro (alqotel)     float_status *excp_ptr = &env->fp_status;
3617c29018ccSLucas Mateus Castro (alqotel)     op_flags = (neg_acc ^ neg_mul) ? float_muladd_negate_c : 0;
3618c29018ccSLucas Mateus Castro (alqotel)     op_flags |= (neg_mul) ? float_muladd_negate_result : 0;
3619c29018ccSLucas Mateus Castro (alqotel)     helper_reset_fpstatus(env);
3620c29018ccSLucas Mateus Castro (alqotel)     for (i = 0, xmsk_bit = 1 << 3; i < 4; i++, xmsk_bit >>= 1) {
3621c29018ccSLucas Mateus Castro (alqotel)         for (j = 0, ymsk_bit = 1 << 3; j < 4; j++, ymsk_bit >>= 1) {
3622c29018ccSLucas Mateus Castro (alqotel)             if ((xmsk_bit & xmsk) && (ymsk_bit & ymsk)) {
3623c29018ccSLucas Mateus Castro (alqotel)                 if (acc) {
3624c29018ccSLucas Mateus Castro (alqotel)                     muladd(at, a, b, i, j, op_flags, excp_ptr);
3625c29018ccSLucas Mateus Castro (alqotel)                 } else {
3626c29018ccSLucas Mateus Castro (alqotel)                     mul(at, a, b, i, j, op_flags, excp_ptr);
3627c29018ccSLucas Mateus Castro (alqotel)                 }
3628c29018ccSLucas Mateus Castro (alqotel)             } else {
3629c29018ccSLucas Mateus Castro (alqotel)                 zero(at, i, j);
3630c29018ccSLucas Mateus Castro (alqotel)             }
3631c29018ccSLucas Mateus Castro (alqotel)         }
3632c29018ccSLucas Mateus Castro (alqotel)     }
3633c29018ccSLucas Mateus Castro (alqotel)     vsxger_excp(env, GETPC());
3634c29018ccSLucas Mateus Castro (alqotel) }
3635c29018ccSLucas Mateus Castro (alqotel) 
3636c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
36375724e131SLucas Mateus Castro (alqotel) void helper_XVBF16GER2(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36385724e131SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
36395724e131SLucas Mateus Castro (alqotel) {
36405724e131SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, false, false, false, extract_bf16);
36415724e131SLucas Mateus Castro (alqotel) }
36425724e131SLucas Mateus Castro (alqotel) 
36435724e131SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36445724e131SLucas Mateus Castro (alqotel) void helper_XVBF16GER2PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36455724e131SLucas Mateus Castro (alqotel)                          ppc_acc_t *at, uint32_t mask)
36465724e131SLucas Mateus Castro (alqotel) {
36475724e131SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, false, false, extract_bf16);
36485724e131SLucas Mateus Castro (alqotel) }
36495724e131SLucas Mateus Castro (alqotel) 
36505724e131SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36515724e131SLucas Mateus Castro (alqotel) void helper_XVBF16GER2PN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36525724e131SLucas Mateus Castro (alqotel)                          ppc_acc_t *at, uint32_t mask)
36535724e131SLucas Mateus Castro (alqotel) {
36545724e131SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, false, true, extract_bf16);
36555724e131SLucas Mateus Castro (alqotel) }
36565724e131SLucas Mateus Castro (alqotel) 
36575724e131SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36585724e131SLucas Mateus Castro (alqotel) void helper_XVBF16GER2NP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36595724e131SLucas Mateus Castro (alqotel)                          ppc_acc_t *at, uint32_t mask)
36605724e131SLucas Mateus Castro (alqotel) {
36615724e131SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, true, false, extract_bf16);
36625724e131SLucas Mateus Castro (alqotel) }
36635724e131SLucas Mateus Castro (alqotel) 
36645724e131SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36655724e131SLucas Mateus Castro (alqotel) void helper_XVBF16GER2NN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36665724e131SLucas Mateus Castro (alqotel)                          ppc_acc_t *at, uint32_t mask)
36675724e131SLucas Mateus Castro (alqotel) {
36685724e131SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, true, true, extract_bf16);
36695724e131SLucas Mateus Castro (alqotel) }
36705724e131SLucas Mateus Castro (alqotel) 
36715724e131SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36722d9cba74SLucas Mateus Castro (alqotel) void helper_XVF16GER2(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36732d9cba74SLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
36742d9cba74SLucas Mateus Castro (alqotel) {
36752d9cba74SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, false, false, false, extract_hf16);
36762d9cba74SLucas Mateus Castro (alqotel) }
36772d9cba74SLucas Mateus Castro (alqotel) 
36782d9cba74SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36792d9cba74SLucas Mateus Castro (alqotel) void helper_XVF16GER2PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36802d9cba74SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
36812d9cba74SLucas Mateus Castro (alqotel) {
36822d9cba74SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, false, false, extract_hf16);
36832d9cba74SLucas Mateus Castro (alqotel) }
36842d9cba74SLucas Mateus Castro (alqotel) 
36852d9cba74SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36862d9cba74SLucas Mateus Castro (alqotel) void helper_XVF16GER2PN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36872d9cba74SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
36882d9cba74SLucas Mateus Castro (alqotel) {
36892d9cba74SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, false, true, extract_hf16);
36902d9cba74SLucas Mateus Castro (alqotel) }
36912d9cba74SLucas Mateus Castro (alqotel) 
36922d9cba74SLucas Mateus Castro (alqotel) QEMU_FLATTEN
36932d9cba74SLucas Mateus Castro (alqotel) void helper_XVF16GER2NP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
36942d9cba74SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
36952d9cba74SLucas Mateus Castro (alqotel) {
36962d9cba74SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, true, false, extract_hf16);
36972d9cba74SLucas Mateus Castro (alqotel) }
36982d9cba74SLucas Mateus Castro (alqotel) 
36992d9cba74SLucas Mateus Castro (alqotel) QEMU_FLATTEN
37002d9cba74SLucas Mateus Castro (alqotel) void helper_XVF16GER2NN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
37012d9cba74SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
37022d9cba74SLucas Mateus Castro (alqotel) {
37032d9cba74SLucas Mateus Castro (alqotel)     vsxger16(env, a, b, at, mask, true, true, true, extract_hf16);
37042d9cba74SLucas Mateus Castro (alqotel) }
37052d9cba74SLucas Mateus Castro (alqotel) 
37062d9cba74SLucas Mateus Castro (alqotel) QEMU_FLATTEN
3707c29018ccSLucas Mateus Castro (alqotel) void helper_XVF32GER(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3708c29018ccSLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
3709c29018ccSLucas Mateus Castro (alqotel) {
3710c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, false, false, false, vsxger_mul32,
3711c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd32, vsxger_zero32);
3712c29018ccSLucas Mateus Castro (alqotel) }
3713c29018ccSLucas Mateus Castro (alqotel) 
3714c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3715c29018ccSLucas Mateus Castro (alqotel) void helper_XVF32GERPP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3716c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3717c29018ccSLucas Mateus Castro (alqotel) {
3718c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, false, false, vsxger_mul32,
3719c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd32, vsxger_zero32);
3720c29018ccSLucas Mateus Castro (alqotel) }
3721c29018ccSLucas Mateus Castro (alqotel) 
3722c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3723c29018ccSLucas Mateus Castro (alqotel) void helper_XVF32GERPN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3724c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3725c29018ccSLucas Mateus Castro (alqotel) {
3726c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, false, true, vsxger_mul32,
3727c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd32, vsxger_zero32);
3728c29018ccSLucas Mateus Castro (alqotel) }
3729c29018ccSLucas Mateus Castro (alqotel) 
3730c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3731c29018ccSLucas Mateus Castro (alqotel) void helper_XVF32GERNP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3732c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3733c29018ccSLucas Mateus Castro (alqotel) {
3734c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, true, false, vsxger_mul32,
3735c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd32, vsxger_zero32);
3736c29018ccSLucas Mateus Castro (alqotel) }
3737c29018ccSLucas Mateus Castro (alqotel) 
3738c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3739c29018ccSLucas Mateus Castro (alqotel) void helper_XVF32GERNN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3740c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3741c29018ccSLucas Mateus Castro (alqotel) {
3742c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, true, true, vsxger_mul32,
3743c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd32, vsxger_zero32);
3744c29018ccSLucas Mateus Castro (alqotel) }
3745c29018ccSLucas Mateus Castro (alqotel) 
3746c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3747c29018ccSLucas Mateus Castro (alqotel) void helper_XVF64GER(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3748c29018ccSLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
3749c29018ccSLucas Mateus Castro (alqotel) {
3750c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, false, false, false, vsxger_mul64,
3751c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd64, vsxger_zero64);
3752c29018ccSLucas Mateus Castro (alqotel) }
3753c29018ccSLucas Mateus Castro (alqotel) 
3754c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3755c29018ccSLucas Mateus Castro (alqotel) void helper_XVF64GERPP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3756c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3757c29018ccSLucas Mateus Castro (alqotel) {
3758c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, false, false, vsxger_mul64,
3759c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd64, vsxger_zero64);
3760c29018ccSLucas Mateus Castro (alqotel) }
3761c29018ccSLucas Mateus Castro (alqotel) 
3762c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3763c29018ccSLucas Mateus Castro (alqotel) void helper_XVF64GERPN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3764c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3765c29018ccSLucas Mateus Castro (alqotel) {
3766c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, false, true, vsxger_mul64,
3767c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd64, vsxger_zero64);
3768c29018ccSLucas Mateus Castro (alqotel) }
3769c29018ccSLucas Mateus Castro (alqotel) 
3770c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3771c29018ccSLucas Mateus Castro (alqotel) void helper_XVF64GERNP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3772c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3773c29018ccSLucas Mateus Castro (alqotel) {
3774c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, true, false, vsxger_mul64,
3775c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd64, vsxger_zero64);
3776c29018ccSLucas Mateus Castro (alqotel) }
3777c29018ccSLucas Mateus Castro (alqotel) 
3778c29018ccSLucas Mateus Castro (alqotel) QEMU_FLATTEN
3779c29018ccSLucas Mateus Castro (alqotel) void helper_XVF64GERNN(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
3780c29018ccSLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
3781c29018ccSLucas Mateus Castro (alqotel) {
3782c29018ccSLucas Mateus Castro (alqotel)     vsxger(env, a, b, at, mask, true, true, true, vsxger_mul64,
3783c29018ccSLucas Mateus Castro (alqotel)            vsxger_muladd64, vsxger_zero64);
3784c29018ccSLucas Mateus Castro (alqotel) }
3785