xref: /openbmc/qemu/target/ppc/int_helper.c (revision 7ca042868744a5efca902473d600d205e9e104b2)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  PowerPC integer and vector 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  */
19db725815SMarkus Armbruster 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "internal.h"
23fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
24db725815SMarkus Armbruster #include "qemu/main-loop.h"
258a05fd9aSRichard Henderson #include "qemu/log.h"
26fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
27fcf5ef2aSThomas Huth #include "crypto/aes.h"
2824f91e81SAlex Bennée #include "fpu/softfloat.h"
293f74b632SRichard Henderson #include "qapi/error.h"
303f74b632SRichard Henderson #include "qemu/guest-random.h"
311015fcabSMatheus Ferst #include "tcg/tcg-gvec-desc.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth #include "helper_regs.h"
34fcf5ef2aSThomas Huth /*****************************************************************************/
35fcf5ef2aSThomas Huth /* Fixed point operations helpers */
36fcf5ef2aSThomas Huth 
37f32899deSNikunj A Dadhania static inline void helper_update_ov_legacy(CPUPPCState *env, int ov)
38f32899deSNikunj A Dadhania {
39f32899deSNikunj A Dadhania     if (unlikely(ov)) {
40f32899deSNikunj A Dadhania         env->so = env->ov = 1;
41f32899deSNikunj A Dadhania     } else {
42f32899deSNikunj A Dadhania         env->ov = 0;
43f32899deSNikunj A Dadhania     }
44f32899deSNikunj A Dadhania }
45f32899deSNikunj A Dadhania 
46fcf5ef2aSThomas Huth target_ulong helper_divweu(CPUPPCState *env, target_ulong ra, target_ulong rb,
47fcf5ef2aSThomas Huth                            uint32_t oe)
48fcf5ef2aSThomas Huth {
49fcf5ef2aSThomas Huth     uint64_t rt = 0;
50fcf5ef2aSThomas Huth     int overflow = 0;
51fcf5ef2aSThomas Huth 
52fcf5ef2aSThomas Huth     uint64_t dividend = (uint64_t)ra << 32;
53fcf5ef2aSThomas Huth     uint64_t divisor = (uint32_t)rb;
54fcf5ef2aSThomas Huth 
55fcf5ef2aSThomas Huth     if (unlikely(divisor == 0)) {
56fcf5ef2aSThomas Huth         overflow = 1;
57fcf5ef2aSThomas Huth     } else {
58fcf5ef2aSThomas Huth         rt = dividend / divisor;
59fcf5ef2aSThomas Huth         overflow = rt > UINT32_MAX;
60fcf5ef2aSThomas Huth     }
61fcf5ef2aSThomas Huth 
62fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
63fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
64fcf5ef2aSThomas Huth     }
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth     if (oe) {
67f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
68fcf5ef2aSThomas Huth     }
69fcf5ef2aSThomas Huth 
70fcf5ef2aSThomas Huth     return (target_ulong)rt;
71fcf5ef2aSThomas Huth }
72fcf5ef2aSThomas Huth 
73fcf5ef2aSThomas Huth target_ulong helper_divwe(CPUPPCState *env, target_ulong ra, target_ulong rb,
74fcf5ef2aSThomas Huth                           uint32_t oe)
75fcf5ef2aSThomas Huth {
76fcf5ef2aSThomas Huth     int64_t rt = 0;
77fcf5ef2aSThomas Huth     int overflow = 0;
78fcf5ef2aSThomas Huth 
79fcf5ef2aSThomas Huth     int64_t dividend = (int64_t)ra << 32;
80fcf5ef2aSThomas Huth     int64_t divisor = (int64_t)((int32_t)rb);
81fcf5ef2aSThomas Huth 
82fcf5ef2aSThomas Huth     if (unlikely((divisor == 0) ||
83fcf5ef2aSThomas Huth                  ((divisor == -1ull) && (dividend == INT64_MIN)))) {
84fcf5ef2aSThomas Huth         overflow = 1;
85fcf5ef2aSThomas Huth     } else {
86fcf5ef2aSThomas Huth         rt = dividend / divisor;
87fcf5ef2aSThomas Huth         overflow = rt != (int32_t)rt;
88fcf5ef2aSThomas Huth     }
89fcf5ef2aSThomas Huth 
90fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
91fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
92fcf5ef2aSThomas Huth     }
93fcf5ef2aSThomas Huth 
94fcf5ef2aSThomas Huth     if (oe) {
95f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
96fcf5ef2aSThomas Huth     }
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth     return (target_ulong)rt;
99fcf5ef2aSThomas Huth }
100fcf5ef2aSThomas Huth 
101fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
102fcf5ef2aSThomas Huth 
103fcf5ef2aSThomas Huth uint64_t helper_divdeu(CPUPPCState *env, uint64_t ra, uint64_t rb, uint32_t oe)
104fcf5ef2aSThomas Huth {
105fcf5ef2aSThomas Huth     uint64_t rt = 0;
106fcf5ef2aSThomas Huth     int overflow = 0;
107fcf5ef2aSThomas Huth 
1089276a31cSLuis Pires     if (unlikely(rb == 0 || ra >= rb)) {
1099276a31cSLuis Pires         overflow = 1;
110fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
1119276a31cSLuis Pires     } else {
1129276a31cSLuis Pires         divu128(&rt, &ra, rb);
113fcf5ef2aSThomas Huth     }
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth     if (oe) {
116f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
117fcf5ef2aSThomas Huth     }
118fcf5ef2aSThomas Huth 
119fcf5ef2aSThomas Huth     return rt;
120fcf5ef2aSThomas Huth }
121fcf5ef2aSThomas Huth 
122fcf5ef2aSThomas Huth uint64_t helper_divde(CPUPPCState *env, uint64_t rau, uint64_t rbu, uint32_t oe)
123fcf5ef2aSThomas Huth {
12440f3e79aSLuis Pires     uint64_t rt = 0;
125fcf5ef2aSThomas Huth     int64_t ra = (int64_t)rau;
126fcf5ef2aSThomas Huth     int64_t rb = (int64_t)rbu;
1279276a31cSLuis Pires     int overflow = 0;
128fcf5ef2aSThomas Huth 
1299276a31cSLuis Pires     if (unlikely(rb == 0 || uabs64(ra) >= uabs64(rb))) {
1309276a31cSLuis Pires         overflow = 1;
131fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
1329276a31cSLuis Pires     } else {
1339276a31cSLuis Pires         divs128(&rt, &ra, rb);
134fcf5ef2aSThomas Huth     }
135fcf5ef2aSThomas Huth 
136fcf5ef2aSThomas Huth     if (oe) {
137f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
138fcf5ef2aSThomas Huth     }
139fcf5ef2aSThomas Huth 
140fcf5ef2aSThomas Huth     return rt;
141fcf5ef2aSThomas Huth }
142fcf5ef2aSThomas Huth 
143fcf5ef2aSThomas Huth #endif
144fcf5ef2aSThomas Huth 
145fcf5ef2aSThomas Huth 
146fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
147fcf5ef2aSThomas Huth /* if x = 0xab, returns 0xababababababababa */
148fcf5ef2aSThomas Huth #define pattern(x) (((x) & 0xff) * (~(target_ulong)0 / 0xff))
149fcf5ef2aSThomas Huth 
150b6cb41b2SDavid Gibson /*
151b6cb41b2SDavid Gibson  * subtract 1 from each byte, and with inverse, check if MSB is set at each
152fcf5ef2aSThomas Huth  * byte.
153fcf5ef2aSThomas Huth  * i.e. ((0x00 - 0x01) & ~(0x00)) & 0x80
154fcf5ef2aSThomas Huth  *      (0xFF & 0xFF) & 0x80 = 0x80 (zero found)
155fcf5ef2aSThomas Huth  */
156fcf5ef2aSThomas Huth #define haszero(v) (((v) - pattern(0x01)) & ~(v) & pattern(0x80))
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth /* When you XOR the pattern and there is a match, that byte will be zero */
159fcf5ef2aSThomas Huth #define hasvalue(x, n)  (haszero((x) ^ pattern(n)))
160fcf5ef2aSThomas Huth 
161fcf5ef2aSThomas Huth uint32_t helper_cmpeqb(target_ulong ra, target_ulong rb)
162fcf5ef2aSThomas Huth {
163efa73196SNikunj A Dadhania     return hasvalue(rb, ra) ? CRF_GT : 0;
164fcf5ef2aSThomas Huth }
165fcf5ef2aSThomas Huth 
166fcf5ef2aSThomas Huth #undef pattern
167fcf5ef2aSThomas Huth #undef haszero
168fcf5ef2aSThomas Huth #undef hasvalue
169fcf5ef2aSThomas Huth 
170b6cb41b2SDavid Gibson /*
1713f74b632SRichard Henderson  * Return a random number.
172fcf5ef2aSThomas Huth  */
1733f74b632SRichard Henderson uint64_t helper_darn32(void)
174fcf5ef2aSThomas Huth {
1753f74b632SRichard Henderson     Error *err = NULL;
1763f74b632SRichard Henderson     uint32_t ret;
1773f74b632SRichard Henderson 
1783f74b632SRichard Henderson     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
1793f74b632SRichard Henderson         qemu_log_mask(LOG_UNIMP, "darn: Crypto failure: %s",
1803f74b632SRichard Henderson                       error_get_pretty(err));
1813f74b632SRichard Henderson         error_free(err);
182fcf5ef2aSThomas Huth         return -1;
183fcf5ef2aSThomas Huth     }
184fcf5ef2aSThomas Huth 
1853f74b632SRichard Henderson     return ret;
1863f74b632SRichard Henderson }
1873f74b632SRichard Henderson 
1883f74b632SRichard Henderson uint64_t helper_darn64(void)
189fcf5ef2aSThomas Huth {
1903f74b632SRichard Henderson     Error *err = NULL;
1913f74b632SRichard Henderson     uint64_t ret;
1923f74b632SRichard Henderson 
1933f74b632SRichard Henderson     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
1943f74b632SRichard Henderson         qemu_log_mask(LOG_UNIMP, "darn: Crypto failure: %s",
1953f74b632SRichard Henderson                       error_get_pretty(err));
1963f74b632SRichard Henderson         error_free(err);
197fcf5ef2aSThomas Huth         return -1;
198fcf5ef2aSThomas Huth     }
199fcf5ef2aSThomas Huth 
2003f74b632SRichard Henderson     return ret;
2013f74b632SRichard Henderson }
202fcf5ef2aSThomas Huth 
203fcf5ef2aSThomas Huth uint64_t helper_bpermd(uint64_t rs, uint64_t rb)
204fcf5ef2aSThomas Huth {
205fcf5ef2aSThomas Huth     int i;
206fcf5ef2aSThomas Huth     uint64_t ra = 0;
207fcf5ef2aSThomas Huth 
208fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
209fcf5ef2aSThomas Huth         int index = (rs >> (i * 8)) & 0xFF;
210fcf5ef2aSThomas Huth         if (index < 64) {
211a6a444a8SCédric Le Goater             if (rb & PPC_BIT(index)) {
212fcf5ef2aSThomas Huth                 ra |= 1 << i;
213fcf5ef2aSThomas Huth             }
214fcf5ef2aSThomas Huth         }
215fcf5ef2aSThomas Huth     }
216fcf5ef2aSThomas Huth     return ra;
217fcf5ef2aSThomas Huth }
218fcf5ef2aSThomas Huth 
219fcf5ef2aSThomas Huth #endif
220fcf5ef2aSThomas Huth 
221fcf5ef2aSThomas Huth target_ulong helper_cmpb(target_ulong rs, target_ulong rb)
222fcf5ef2aSThomas Huth {
223fcf5ef2aSThomas Huth     target_ulong mask = 0xff;
224fcf5ef2aSThomas Huth     target_ulong ra = 0;
225fcf5ef2aSThomas Huth     int i;
226fcf5ef2aSThomas Huth 
227fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong); i++) {
228fcf5ef2aSThomas Huth         if ((rs & mask) == (rb & mask)) {
229fcf5ef2aSThomas Huth             ra |= mask;
230fcf5ef2aSThomas Huth         }
231fcf5ef2aSThomas Huth         mask <<= 8;
232fcf5ef2aSThomas Huth     }
233fcf5ef2aSThomas Huth     return ra;
234fcf5ef2aSThomas Huth }
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth /* shift right arithmetic helper */
237fcf5ef2aSThomas Huth target_ulong helper_sraw(CPUPPCState *env, target_ulong value,
238fcf5ef2aSThomas Huth                          target_ulong shift)
239fcf5ef2aSThomas Huth {
240fcf5ef2aSThomas Huth     int32_t ret;
241fcf5ef2aSThomas Huth 
242fcf5ef2aSThomas Huth     if (likely(!(shift & 0x20))) {
243fcf5ef2aSThomas Huth         if (likely((uint32_t)shift != 0)) {
244fcf5ef2aSThomas Huth             shift &= 0x1f;
245fcf5ef2aSThomas Huth             ret = (int32_t)value >> shift;
246fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
247af1c259fSSandipan Das                 env->ca32 = env->ca = 0;
248fcf5ef2aSThomas Huth             } else {
249af1c259fSSandipan Das                 env->ca32 = env->ca = 1;
250fcf5ef2aSThomas Huth             }
251fcf5ef2aSThomas Huth         } else {
252fcf5ef2aSThomas Huth             ret = (int32_t)value;
253af1c259fSSandipan Das             env->ca32 = env->ca = 0;
254fcf5ef2aSThomas Huth         }
255fcf5ef2aSThomas Huth     } else {
256fcf5ef2aSThomas Huth         ret = (int32_t)value >> 31;
257af1c259fSSandipan Das         env->ca32 = env->ca = (ret != 0);
258fcf5ef2aSThomas Huth     }
259fcf5ef2aSThomas Huth     return (target_long)ret;
260fcf5ef2aSThomas Huth }
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
263fcf5ef2aSThomas Huth target_ulong helper_srad(CPUPPCState *env, target_ulong value,
264fcf5ef2aSThomas Huth                          target_ulong shift)
265fcf5ef2aSThomas Huth {
266fcf5ef2aSThomas Huth     int64_t ret;
267fcf5ef2aSThomas Huth 
268fcf5ef2aSThomas Huth     if (likely(!(shift & 0x40))) {
269fcf5ef2aSThomas Huth         if (likely((uint64_t)shift != 0)) {
270fcf5ef2aSThomas Huth             shift &= 0x3f;
271fcf5ef2aSThomas Huth             ret = (int64_t)value >> shift;
272fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1ULL << shift) - 1)) == 0)) {
273af1c259fSSandipan Das                 env->ca32 = env->ca = 0;
274fcf5ef2aSThomas Huth             } else {
275af1c259fSSandipan Das                 env->ca32 = env->ca = 1;
276fcf5ef2aSThomas Huth             }
277fcf5ef2aSThomas Huth         } else {
278fcf5ef2aSThomas Huth             ret = (int64_t)value;
279af1c259fSSandipan Das             env->ca32 = env->ca = 0;
280fcf5ef2aSThomas Huth         }
281fcf5ef2aSThomas Huth     } else {
282fcf5ef2aSThomas Huth         ret = (int64_t)value >> 63;
283af1c259fSSandipan Das         env->ca32 = env->ca = (ret != 0);
284fcf5ef2aSThomas Huth     }
285fcf5ef2aSThomas Huth     return ret;
286fcf5ef2aSThomas Huth }
287fcf5ef2aSThomas Huth #endif
288fcf5ef2aSThomas Huth 
289fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
290fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
291fcf5ef2aSThomas Huth {
29279770002SRichard Henderson     /* Note that we don't fold past bytes */
293fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
294fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
295fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
296fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
297fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
298fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
299fcf5ef2aSThomas Huth     return val;
300fcf5ef2aSThomas Huth }
301fcf5ef2aSThomas Huth 
302fcf5ef2aSThomas Huth target_ulong helper_popcntw(target_ulong val)
303fcf5ef2aSThomas Huth {
30479770002SRichard Henderson     /* Note that we don't fold past words.  */
305fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
306fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
307fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
308fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
309fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
310fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
311fcf5ef2aSThomas Huth     val = (val & 0x00ff00ff00ff00ffULL) + ((val >>  8) &
312fcf5ef2aSThomas Huth                                            0x00ff00ff00ff00ffULL);
313fcf5ef2aSThomas Huth     val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) &
314fcf5ef2aSThomas Huth                                            0x0000ffff0000ffffULL);
315fcf5ef2aSThomas Huth     return val;
316fcf5ef2aSThomas Huth }
317fcf5ef2aSThomas Huth #else
318fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
319fcf5ef2aSThomas Huth {
32079770002SRichard Henderson     /* Note that we don't fold past bytes */
321fcf5ef2aSThomas Huth     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
322fcf5ef2aSThomas Huth     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
323fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
324fcf5ef2aSThomas Huth     return val;
325fcf5ef2aSThomas Huth }
326fcf5ef2aSThomas Huth #endif
327fcf5ef2aSThomas Huth 
3286e0bbc40SMatheus Ferst uint64_t helper_CFUGED(uint64_t src, uint64_t mask)
32989ccd7dcSMatheus Ferst {
33089ccd7dcSMatheus Ferst     /*
33189ccd7dcSMatheus Ferst      * Instead of processing the mask bit-by-bit from the most significant to
33289ccd7dcSMatheus Ferst      * the least significant bit, as described in PowerISA, we'll handle it in
33389ccd7dcSMatheus Ferst      * blocks of 'n' zeros/ones from LSB to MSB. To avoid the decision to use
33489ccd7dcSMatheus Ferst      * ctz or cto, we negate the mask at the end of the loop.
33589ccd7dcSMatheus Ferst      */
33689ccd7dcSMatheus Ferst     target_ulong m, left = 0, right = 0;
33789ccd7dcSMatheus Ferst     unsigned int n, i = 64;
33889ccd7dcSMatheus Ferst     bool bit = false; /* tracks if we are processing zeros or ones */
33989ccd7dcSMatheus Ferst 
34089ccd7dcSMatheus Ferst     if (mask == 0 || mask == -1) {
34189ccd7dcSMatheus Ferst         return src;
34289ccd7dcSMatheus Ferst     }
34389ccd7dcSMatheus Ferst 
34489ccd7dcSMatheus Ferst     /* Processes the mask in blocks, from LSB to MSB */
34589ccd7dcSMatheus Ferst     while (i) {
34689ccd7dcSMatheus Ferst         /* Find how many bits we should take */
34789ccd7dcSMatheus Ferst         n = ctz64(mask);
34889ccd7dcSMatheus Ferst         if (n > i) {
34989ccd7dcSMatheus Ferst             n = i;
35089ccd7dcSMatheus Ferst         }
35189ccd7dcSMatheus Ferst 
35289ccd7dcSMatheus Ferst         /*
35389ccd7dcSMatheus Ferst          * Extracts 'n' trailing bits of src and put them on the leading 'n'
35489ccd7dcSMatheus Ferst          * bits of 'right' or 'left', pushing down the previously extracted
35589ccd7dcSMatheus Ferst          * values.
35689ccd7dcSMatheus Ferst          */
35789ccd7dcSMatheus Ferst         m = (1ll << n) - 1;
35889ccd7dcSMatheus Ferst         if (bit) {
35989ccd7dcSMatheus Ferst             right = ror64(right | (src & m), n);
36089ccd7dcSMatheus Ferst         } else {
36189ccd7dcSMatheus Ferst             left = ror64(left | (src & m), n);
36289ccd7dcSMatheus Ferst         }
36389ccd7dcSMatheus Ferst 
36489ccd7dcSMatheus Ferst         /*
36589ccd7dcSMatheus Ferst          * Discards the processed bits from 'src' and 'mask'. Note that we are
36689ccd7dcSMatheus Ferst          * removing 'n' trailing zeros from 'mask', but the logical shift will
36789ccd7dcSMatheus Ferst          * add 'n' leading zeros back, so the population count of 'mask' is kept
36889ccd7dcSMatheus Ferst          * the same.
36989ccd7dcSMatheus Ferst          */
37089ccd7dcSMatheus Ferst         src >>= n;
37189ccd7dcSMatheus Ferst         mask >>= n;
37289ccd7dcSMatheus Ferst         i -= n;
37389ccd7dcSMatheus Ferst         bit = !bit;
37489ccd7dcSMatheus Ferst         mask = ~mask;
37589ccd7dcSMatheus Ferst     }
37689ccd7dcSMatheus Ferst 
37789ccd7dcSMatheus Ferst     /*
37889ccd7dcSMatheus Ferst      * At the end, right was ror'ed ctpop(mask) times. To put it back in place,
37989ccd7dcSMatheus Ferst      * we'll shift it more 64-ctpop(mask) times.
38089ccd7dcSMatheus Ferst      */
38189ccd7dcSMatheus Ferst     if (bit) {
38289ccd7dcSMatheus Ferst         n = ctpop64(mask);
38389ccd7dcSMatheus Ferst     } else {
38489ccd7dcSMatheus Ferst         n = 64 - ctpop64(mask);
38589ccd7dcSMatheus Ferst     }
38689ccd7dcSMatheus Ferst 
38789ccd7dcSMatheus Ferst     return left | (right >> n);
38889ccd7dcSMatheus Ferst }
38989ccd7dcSMatheus Ferst 
39021ba6e58SMatheus Ferst uint64_t helper_PDEPD(uint64_t src, uint64_t mask)
39121ba6e58SMatheus Ferst {
39221ba6e58SMatheus Ferst     int i, o;
39321ba6e58SMatheus Ferst     uint64_t result = 0;
39421ba6e58SMatheus Ferst 
39521ba6e58SMatheus Ferst     if (mask == -1) {
39621ba6e58SMatheus Ferst         return src;
39721ba6e58SMatheus Ferst     }
39821ba6e58SMatheus Ferst 
39921ba6e58SMatheus Ferst     for (i = 0; mask != 0; i++) {
40021ba6e58SMatheus Ferst         o = ctz64(mask);
40121ba6e58SMatheus Ferst         mask &= mask - 1;
40221ba6e58SMatheus Ferst         result |= ((src >> i) & 1) << o;
40321ba6e58SMatheus Ferst     }
40421ba6e58SMatheus Ferst 
40521ba6e58SMatheus Ferst     return result;
40621ba6e58SMatheus Ferst }
4078bdb7606SMatheus Ferst 
4088bdb7606SMatheus Ferst uint64_t helper_PEXTD(uint64_t src, uint64_t mask)
4098bdb7606SMatheus Ferst {
4108bdb7606SMatheus Ferst     int i, o;
4118bdb7606SMatheus Ferst     uint64_t result = 0;
4128bdb7606SMatheus Ferst 
4138bdb7606SMatheus Ferst     if (mask == -1) {
4148bdb7606SMatheus Ferst         return src;
4158bdb7606SMatheus Ferst     }
4168bdb7606SMatheus Ferst 
4178bdb7606SMatheus Ferst     for (o = 0; mask != 0; o++) {
4188bdb7606SMatheus Ferst         i = ctz64(mask);
4198bdb7606SMatheus Ferst         mask &= mask - 1;
4208bdb7606SMatheus Ferst         result |= ((src >> i) & 1) << o;
4218bdb7606SMatheus Ferst     }
4228bdb7606SMatheus Ferst 
4238bdb7606SMatheus Ferst     return result;
4248bdb7606SMatheus Ferst }
42521ba6e58SMatheus Ferst 
426fcf5ef2aSThomas Huth /*****************************************************************************/
427fcf5ef2aSThomas Huth /* Altivec extension helpers */
428e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
429fcf5ef2aSThomas Huth #define VECTOR_FOR_INORDER_I(index, element)                    \
430fcf5ef2aSThomas Huth     for (index = 0; index < ARRAY_SIZE(r->element); index++)
431fcf5ef2aSThomas Huth #else
432fcf5ef2aSThomas Huth #define VECTOR_FOR_INORDER_I(index, element)                    \
433fcf5ef2aSThomas Huth     for (index = ARRAY_SIZE(r->element) - 1; index >= 0; index--)
434fcf5ef2aSThomas Huth #endif
435fcf5ef2aSThomas Huth 
436fcf5ef2aSThomas Huth /* Saturating arithmetic helpers.  */
437fcf5ef2aSThomas Huth #define SATCVT(from, to, from_type, to_type, min, max)          \
438fcf5ef2aSThomas Huth     static inline to_type cvt##from##to(from_type x, int *sat)  \
439fcf5ef2aSThomas Huth     {                                                           \
440fcf5ef2aSThomas Huth         to_type r;                                              \
441fcf5ef2aSThomas Huth                                                                 \
442fcf5ef2aSThomas Huth         if (x < (from_type)min) {                               \
443fcf5ef2aSThomas Huth             r = min;                                            \
444fcf5ef2aSThomas Huth             *sat = 1;                                           \
445fcf5ef2aSThomas Huth         } else if (x > (from_type)max) {                        \
446fcf5ef2aSThomas Huth             r = max;                                            \
447fcf5ef2aSThomas Huth             *sat = 1;                                           \
448fcf5ef2aSThomas Huth         } else {                                                \
449fcf5ef2aSThomas Huth             r = x;                                              \
450fcf5ef2aSThomas Huth         }                                                       \
451fcf5ef2aSThomas Huth         return r;                                               \
452fcf5ef2aSThomas Huth     }
453fcf5ef2aSThomas Huth #define SATCVTU(from, to, from_type, to_type, min, max)         \
454fcf5ef2aSThomas Huth     static inline to_type cvt##from##to(from_type x, int *sat)  \
455fcf5ef2aSThomas Huth     {                                                           \
456fcf5ef2aSThomas Huth         to_type r;                                              \
457fcf5ef2aSThomas Huth                                                                 \
458fcf5ef2aSThomas Huth         if (x > (from_type)max) {                               \
459fcf5ef2aSThomas Huth             r = max;                                            \
460fcf5ef2aSThomas Huth             *sat = 1;                                           \
461fcf5ef2aSThomas Huth         } else {                                                \
462fcf5ef2aSThomas Huth             r = x;                                              \
463fcf5ef2aSThomas Huth         }                                                       \
464fcf5ef2aSThomas Huth         return r;                                               \
465fcf5ef2aSThomas Huth     }
466fcf5ef2aSThomas Huth SATCVT(sh, sb, int16_t, int8_t, INT8_MIN, INT8_MAX)
467fcf5ef2aSThomas Huth SATCVT(sw, sh, int32_t, int16_t, INT16_MIN, INT16_MAX)
468fcf5ef2aSThomas Huth SATCVT(sd, sw, int64_t, int32_t, INT32_MIN, INT32_MAX)
469fcf5ef2aSThomas Huth 
470fcf5ef2aSThomas Huth SATCVTU(uh, ub, uint16_t, uint8_t, 0, UINT8_MAX)
471fcf5ef2aSThomas Huth SATCVTU(uw, uh, uint32_t, uint16_t, 0, UINT16_MAX)
472fcf5ef2aSThomas Huth SATCVTU(ud, uw, uint64_t, uint32_t, 0, UINT32_MAX)
473fcf5ef2aSThomas Huth SATCVT(sh, ub, int16_t, uint8_t, 0, UINT8_MAX)
474fcf5ef2aSThomas Huth SATCVT(sw, uh, int32_t, uint16_t, 0, UINT16_MAX)
475fcf5ef2aSThomas Huth SATCVT(sd, uw, int64_t, uint32_t, 0, UINT32_MAX)
476fcf5ef2aSThomas Huth #undef SATCVT
477fcf5ef2aSThomas Huth #undef SATCVTU
478fcf5ef2aSThomas Huth 
479dedfaac7SRichard Henderson void helper_mtvscr(CPUPPCState *env, uint32_t vscr)
480fcf5ef2aSThomas Huth {
481c19940dbSBruno Larsen (billionai)     ppc_store_vscr(env, vscr);
482fcf5ef2aSThomas Huth }
483fcf5ef2aSThomas Huth 
484cc2b90d7SRichard Henderson uint32_t helper_mfvscr(CPUPPCState *env)
485cc2b90d7SRichard Henderson {
486c19940dbSBruno Larsen (billionai)     return ppc_get_vscr(env);
487cc2b90d7SRichard Henderson }
488cc2b90d7SRichard Henderson 
4896175f5a0SRichard Henderson static inline void set_vscr_sat(CPUPPCState *env)
4906175f5a0SRichard Henderson {
4919b5b74daSRichard Henderson     /* The choice of non-zero value is arbitrary.  */
4929b5b74daSRichard Henderson     env->vscr_sat.u32[0] = 1;
4936175f5a0SRichard Henderson }
4946175f5a0SRichard Henderson 
495fcf5ef2aSThomas Huth void helper_vaddcuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
496fcf5ef2aSThomas Huth {
497fcf5ef2aSThomas Huth     int i;
498fcf5ef2aSThomas Huth 
499fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
500fcf5ef2aSThomas Huth         r->u32[i] = ~a->u32[i] < b->u32[i];
501fcf5ef2aSThomas Huth     }
502fcf5ef2aSThomas Huth }
503fcf5ef2aSThomas Huth 
504fcf5ef2aSThomas Huth /* vprtybw */
505fcf5ef2aSThomas Huth void helper_vprtybw(ppc_avr_t *r, ppc_avr_t *b)
506fcf5ef2aSThomas Huth {
507fcf5ef2aSThomas Huth     int i;
508fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
509fcf5ef2aSThomas Huth         uint64_t res = b->u32[i] ^ (b->u32[i] >> 16);
510fcf5ef2aSThomas Huth         res ^= res >> 8;
511fcf5ef2aSThomas Huth         r->u32[i] = res & 1;
512fcf5ef2aSThomas Huth     }
513fcf5ef2aSThomas Huth }
514fcf5ef2aSThomas Huth 
515fcf5ef2aSThomas Huth /* vprtybd */
516fcf5ef2aSThomas Huth void helper_vprtybd(ppc_avr_t *r, ppc_avr_t *b)
517fcf5ef2aSThomas Huth {
518fcf5ef2aSThomas Huth     int i;
519fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
520fcf5ef2aSThomas Huth         uint64_t res = b->u64[i] ^ (b->u64[i] >> 32);
521fcf5ef2aSThomas Huth         res ^= res >> 16;
522fcf5ef2aSThomas Huth         res ^= res >> 8;
523fcf5ef2aSThomas Huth         r->u64[i] = res & 1;
524fcf5ef2aSThomas Huth     }
525fcf5ef2aSThomas Huth }
526fcf5ef2aSThomas Huth 
527fcf5ef2aSThomas Huth /* vprtybq */
528fcf5ef2aSThomas Huth void helper_vprtybq(ppc_avr_t *r, ppc_avr_t *b)
529fcf5ef2aSThomas Huth {
530fcf5ef2aSThomas Huth     uint64_t res = b->u64[0] ^ b->u64[1];
531fcf5ef2aSThomas Huth     res ^= res >> 32;
532fcf5ef2aSThomas Huth     res ^= res >> 16;
533fcf5ef2aSThomas Huth     res ^= res >> 8;
5343c385a93SMark Cave-Ayland     r->VsrD(1) = res & 1;
5353c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
536fcf5ef2aSThomas Huth }
537fcf5ef2aSThomas Huth 
538fcf5ef2aSThomas Huth #define VARITHFP(suffix, func)                                          \
539fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
540fcf5ef2aSThomas Huth                           ppc_avr_t *b)                                 \
541fcf5ef2aSThomas Huth     {                                                                   \
542fcf5ef2aSThomas Huth         int i;                                                          \
543fcf5ef2aSThomas Huth                                                                         \
54405ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
54505ee3e8aSMark Cave-Ayland             r->f32[i] = func(a->f32[i], b->f32[i], &env->vec_status);   \
546fcf5ef2aSThomas Huth         }                                                               \
547fcf5ef2aSThomas Huth     }
548fcf5ef2aSThomas Huth VARITHFP(addfp, float32_add)
549fcf5ef2aSThomas Huth VARITHFP(subfp, float32_sub)
550fcf5ef2aSThomas Huth VARITHFP(minfp, float32_min)
551fcf5ef2aSThomas Huth VARITHFP(maxfp, float32_max)
552fcf5ef2aSThomas Huth #undef VARITHFP
553fcf5ef2aSThomas Huth 
554fcf5ef2aSThomas Huth #define VARITHFPFMA(suffix, type)                                       \
555fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
556fcf5ef2aSThomas Huth                            ppc_avr_t *b, ppc_avr_t *c)                  \
557fcf5ef2aSThomas Huth     {                                                                   \
558fcf5ef2aSThomas Huth         int i;                                                          \
55905ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
56005ee3e8aSMark Cave-Ayland             r->f32[i] = float32_muladd(a->f32[i], c->f32[i], b->f32[i], \
561fcf5ef2aSThomas Huth                                        type, &env->vec_status);         \
562fcf5ef2aSThomas Huth         }                                                               \
563fcf5ef2aSThomas Huth     }
564fcf5ef2aSThomas Huth VARITHFPFMA(maddfp, 0);
565fcf5ef2aSThomas Huth VARITHFPFMA(nmsubfp, float_muladd_negate_result | float_muladd_negate_c);
566fcf5ef2aSThomas Huth #undef VARITHFPFMA
567fcf5ef2aSThomas Huth 
568fcf5ef2aSThomas Huth #define VARITHSAT_CASE(type, op, cvt, element)                          \
569fcf5ef2aSThomas Huth     {                                                                   \
570fcf5ef2aSThomas Huth         type result = (type)a->element[i] op (type)b->element[i];       \
571fcf5ef2aSThomas Huth         r->element[i] = cvt(result, &sat);                              \
572fcf5ef2aSThomas Huth     }
573fcf5ef2aSThomas Huth 
574fcf5ef2aSThomas Huth #define VARITHSAT_DO(name, op, optype, cvt, element)                    \
575fb11ae7dSRichard Henderson     void helper_v##name(ppc_avr_t *r, ppc_avr_t *vscr_sat,              \
576fb11ae7dSRichard Henderson                         ppc_avr_t *a, ppc_avr_t *b, uint32_t desc)      \
577fcf5ef2aSThomas Huth     {                                                                   \
578fcf5ef2aSThomas Huth         int sat = 0;                                                    \
579fcf5ef2aSThomas Huth         int i;                                                          \
580fcf5ef2aSThomas Huth                                                                         \
581fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
582fcf5ef2aSThomas Huth             VARITHSAT_CASE(optype, op, cvt, element);                   \
583fcf5ef2aSThomas Huth         }                                                               \
584fcf5ef2aSThomas Huth         if (sat) {                                                      \
585fb11ae7dSRichard Henderson             vscr_sat->u32[0] = 1;                                       \
586fcf5ef2aSThomas Huth         }                                                               \
587fcf5ef2aSThomas Huth     }
588fcf5ef2aSThomas Huth #define VARITHSAT_SIGNED(suffix, element, optype, cvt)          \
589fcf5ef2aSThomas Huth     VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element)      \
590fcf5ef2aSThomas Huth     VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
591fcf5ef2aSThomas Huth #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt)        \
592fcf5ef2aSThomas Huth     VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element)      \
593fcf5ef2aSThomas Huth     VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
594fcf5ef2aSThomas Huth VARITHSAT_SIGNED(b, s8, int16_t, cvtshsb)
595fcf5ef2aSThomas Huth VARITHSAT_SIGNED(h, s16, int32_t, cvtswsh)
596fcf5ef2aSThomas Huth VARITHSAT_SIGNED(w, s32, int64_t, cvtsdsw)
597fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(b, u8, uint16_t, cvtshub)
598fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(h, u16, uint32_t, cvtswuh)
599fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(w, u32, uint64_t, cvtsduw)
600fcf5ef2aSThomas Huth #undef VARITHSAT_CASE
601fcf5ef2aSThomas Huth #undef VARITHSAT_DO
602fcf5ef2aSThomas Huth #undef VARITHSAT_SIGNED
603fcf5ef2aSThomas Huth #undef VARITHSAT_UNSIGNED
604fcf5ef2aSThomas Huth 
605fcf5ef2aSThomas Huth #define VAVG_DO(name, element, etype)                                   \
606fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
607fcf5ef2aSThomas Huth     {                                                                   \
608fcf5ef2aSThomas Huth         int i;                                                          \
609fcf5ef2aSThomas Huth                                                                         \
610fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
611fcf5ef2aSThomas Huth             etype x = (etype)a->element[i] + (etype)b->element[i] + 1;  \
612fcf5ef2aSThomas Huth             r->element[i] = x >> 1;                                     \
613fcf5ef2aSThomas Huth         }                                                               \
614fcf5ef2aSThomas Huth     }
615fcf5ef2aSThomas Huth 
616fcf5ef2aSThomas Huth #define VAVG(type, signed_element, signed_type, unsigned_element,       \
617fcf5ef2aSThomas Huth              unsigned_type)                                             \
618fcf5ef2aSThomas Huth     VAVG_DO(avgs##type, signed_element, signed_type)                    \
619fcf5ef2aSThomas Huth     VAVG_DO(avgu##type, unsigned_element, unsigned_type)
620fcf5ef2aSThomas Huth VAVG(b, s8, int16_t, u8, uint16_t)
621fcf5ef2aSThomas Huth VAVG(h, s16, int32_t, u16, uint32_t)
622fcf5ef2aSThomas Huth VAVG(w, s32, int64_t, u32, uint64_t)
623fcf5ef2aSThomas Huth #undef VAVG_DO
624fcf5ef2aSThomas Huth #undef VAVG
625fcf5ef2aSThomas Huth 
626fcf5ef2aSThomas Huth #define VABSDU_DO(name, element)                                        \
627fcf5ef2aSThomas Huth void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)           \
628fcf5ef2aSThomas Huth {                                                                       \
629fcf5ef2aSThomas Huth     int i;                                                              \
630fcf5ef2aSThomas Huth                                                                         \
631fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
632fcf5ef2aSThomas Huth         r->element[i] = (a->element[i] > b->element[i]) ?               \
633fcf5ef2aSThomas Huth             (a->element[i] - b->element[i]) :                           \
634fcf5ef2aSThomas Huth             (b->element[i] - a->element[i]);                            \
635fcf5ef2aSThomas Huth     }                                                                   \
636fcf5ef2aSThomas Huth }
637fcf5ef2aSThomas Huth 
638b6cb41b2SDavid Gibson /*
639b6cb41b2SDavid Gibson  * VABSDU - Vector absolute difference unsigned
640fcf5ef2aSThomas Huth  *   name    - instruction mnemonic suffix (b: byte, h: halfword, w: word)
641fcf5ef2aSThomas Huth  *   element - element type to access from vector
642fcf5ef2aSThomas Huth  */
643fcf5ef2aSThomas Huth #define VABSDU(type, element)                   \
644fcf5ef2aSThomas Huth     VABSDU_DO(absdu##type, element)
645fcf5ef2aSThomas Huth VABSDU(b, u8)
646fcf5ef2aSThomas Huth VABSDU(h, u16)
647fcf5ef2aSThomas Huth VABSDU(w, u32)
648fcf5ef2aSThomas Huth #undef VABSDU_DO
649fcf5ef2aSThomas Huth #undef VABSDU
650fcf5ef2aSThomas Huth 
651fcf5ef2aSThomas Huth #define VCF(suffix, cvt, element)                                       \
652fcf5ef2aSThomas Huth     void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r,             \
653fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
654fcf5ef2aSThomas Huth     {                                                                   \
655fcf5ef2aSThomas Huth         int i;                                                          \
656fcf5ef2aSThomas Huth                                                                         \
65705ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
658fcf5ef2aSThomas Huth             float32 t = cvt(b->element[i], &env->vec_status);           \
65905ee3e8aSMark Cave-Ayland             r->f32[i] = float32_scalbn(t, -uim, &env->vec_status);      \
660fcf5ef2aSThomas Huth         }                                                               \
661fcf5ef2aSThomas Huth     }
662fcf5ef2aSThomas Huth VCF(ux, uint32_to_float32, u32)
663fcf5ef2aSThomas Huth VCF(sx, int32_to_float32, s32)
664fcf5ef2aSThomas Huth #undef VCF
665fcf5ef2aSThomas Huth 
666eb936dc0SMatheus Ferst #define VCMPNEZ(NAME, ELEM) \
667eb936dc0SMatheus Ferst void helper_##NAME(ppc_vsr_t *t, ppc_vsr_t *a, ppc_vsr_t *b, uint32_t desc) \
668fcf5ef2aSThomas Huth {                                                                           \
669eb936dc0SMatheus Ferst     for (int i = 0; i < ARRAY_SIZE(t->ELEM); i++) {                         \
670eb936dc0SMatheus Ferst         t->ELEM[i] = ((a->ELEM[i] == 0) || (b->ELEM[i] == 0) ||             \
671eb936dc0SMatheus Ferst                       (a->ELEM[i] != b->ELEM[i])) ? -1 : 0;                 \
672fcf5ef2aSThomas Huth     }                                                                       \
673fcf5ef2aSThomas Huth }
674eb936dc0SMatheus Ferst VCMPNEZ(VCMPNEZB, u8)
675eb936dc0SMatheus Ferst VCMPNEZ(VCMPNEZH, u16)
676eb936dc0SMatheus Ferst VCMPNEZ(VCMPNEZW, u32)
677eb936dc0SMatheus Ferst #undef VCMPNEZ
678fcf5ef2aSThomas Huth 
679fcf5ef2aSThomas Huth #define VCMPFP_DO(suffix, compare, order, record)                       \
680fcf5ef2aSThomas Huth     void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r,            \
681fcf5ef2aSThomas Huth                              ppc_avr_t *a, ppc_avr_t *b)                \
682fcf5ef2aSThomas Huth     {                                                                   \
683fcf5ef2aSThomas Huth         uint32_t ones = (uint32_t)-1;                                   \
684fcf5ef2aSThomas Huth         uint32_t all = ones;                                            \
685fcf5ef2aSThomas Huth         uint32_t none = 0;                                              \
686fcf5ef2aSThomas Huth         int i;                                                          \
687fcf5ef2aSThomas Huth                                                                         \
68805ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
689fcf5ef2aSThomas Huth             uint32_t result;                                            \
69071bfd65cSRichard Henderson             FloatRelation rel =                                         \
69171bfd65cSRichard Henderson                 float32_compare_quiet(a->f32[i], b->f32[i],             \
692fcf5ef2aSThomas Huth                                       &env->vec_status);                \
693fcf5ef2aSThomas Huth             if (rel == float_relation_unordered) {                      \
694fcf5ef2aSThomas Huth                 result = 0;                                             \
695fcf5ef2aSThomas Huth             } else if (rel compare order) {                             \
696fcf5ef2aSThomas Huth                 result = ones;                                          \
697fcf5ef2aSThomas Huth             } else {                                                    \
698fcf5ef2aSThomas Huth                 result = 0;                                             \
699fcf5ef2aSThomas Huth             }                                                           \
700fcf5ef2aSThomas Huth             r->u32[i] = result;                                         \
701fcf5ef2aSThomas Huth             all &= result;                                              \
702fcf5ef2aSThomas Huth             none |= result;                                             \
703fcf5ef2aSThomas Huth         }                                                               \
704fcf5ef2aSThomas Huth         if (record) {                                                   \
705fcf5ef2aSThomas Huth             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
706fcf5ef2aSThomas Huth         }                                                               \
707fcf5ef2aSThomas Huth     }
708fcf5ef2aSThomas Huth #define VCMPFP(suffix, compare, order)          \
709fcf5ef2aSThomas Huth     VCMPFP_DO(suffix, compare, order, 0)        \
710fcf5ef2aSThomas Huth     VCMPFP_DO(suffix##_dot, compare, order, 1)
711fcf5ef2aSThomas Huth VCMPFP(eqfp, ==, float_relation_equal)
712fcf5ef2aSThomas Huth VCMPFP(gefp, !=, float_relation_less)
713fcf5ef2aSThomas Huth VCMPFP(gtfp, ==, float_relation_greater)
714fcf5ef2aSThomas Huth #undef VCMPFP_DO
715fcf5ef2aSThomas Huth #undef VCMPFP
716fcf5ef2aSThomas Huth 
717fcf5ef2aSThomas Huth static inline void vcmpbfp_internal(CPUPPCState *env, ppc_avr_t *r,
718fcf5ef2aSThomas Huth                                     ppc_avr_t *a, ppc_avr_t *b, int record)
719fcf5ef2aSThomas Huth {
720fcf5ef2aSThomas Huth     int i;
721fcf5ef2aSThomas Huth     int all_in = 0;
722fcf5ef2aSThomas Huth 
72305ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
72471bfd65cSRichard Henderson         FloatRelation le_rel = float32_compare_quiet(a->f32[i], b->f32[i],
72505ee3e8aSMark Cave-Ayland                                                      &env->vec_status);
726fcf5ef2aSThomas Huth         if (le_rel == float_relation_unordered) {
727fcf5ef2aSThomas Huth             r->u32[i] = 0xc0000000;
728fcf5ef2aSThomas Huth             all_in = 1;
729fcf5ef2aSThomas Huth         } else {
73005ee3e8aSMark Cave-Ayland             float32 bneg = float32_chs(b->f32[i]);
73171bfd65cSRichard Henderson             FloatRelation ge_rel = float32_compare_quiet(a->f32[i], bneg,
73205ee3e8aSMark Cave-Ayland                                                          &env->vec_status);
733fcf5ef2aSThomas Huth             int le = le_rel != float_relation_greater;
734fcf5ef2aSThomas Huth             int ge = ge_rel != float_relation_less;
735fcf5ef2aSThomas Huth 
736fcf5ef2aSThomas Huth             r->u32[i] = ((!le) << 31) | ((!ge) << 30);
737fcf5ef2aSThomas Huth             all_in |= (!le | !ge);
738fcf5ef2aSThomas Huth         }
739fcf5ef2aSThomas Huth     }
740fcf5ef2aSThomas Huth     if (record) {
741fcf5ef2aSThomas Huth         env->crf[6] = (all_in == 0) << 1;
742fcf5ef2aSThomas Huth     }
743fcf5ef2aSThomas Huth }
744fcf5ef2aSThomas Huth 
745fcf5ef2aSThomas Huth void helper_vcmpbfp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
746fcf5ef2aSThomas Huth {
747fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 0);
748fcf5ef2aSThomas Huth }
749fcf5ef2aSThomas Huth 
750fcf5ef2aSThomas Huth void helper_vcmpbfp_dot(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
751fcf5ef2aSThomas Huth                         ppc_avr_t *b)
752fcf5ef2aSThomas Huth {
753fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 1);
754fcf5ef2aSThomas Huth }
755fcf5ef2aSThomas Huth 
756fcf5ef2aSThomas Huth #define VCT(suffix, satcvt, element)                                    \
757fcf5ef2aSThomas Huth     void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r,             \
758fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
759fcf5ef2aSThomas Huth     {                                                                   \
760fcf5ef2aSThomas Huth         int i;                                                          \
761fcf5ef2aSThomas Huth         int sat = 0;                                                    \
762fcf5ef2aSThomas Huth         float_status s = env->vec_status;                               \
763fcf5ef2aSThomas Huth                                                                         \
764fcf5ef2aSThomas Huth         set_float_rounding_mode(float_round_to_zero, &s);               \
76505ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
76605ee3e8aSMark Cave-Ayland             if (float32_is_any_nan(b->f32[i])) {                        \
767fcf5ef2aSThomas Huth                 r->element[i] = 0;                                      \
768fcf5ef2aSThomas Huth             } else {                                                    \
76905ee3e8aSMark Cave-Ayland                 float64 t = float32_to_float64(b->f32[i], &s);          \
770fcf5ef2aSThomas Huth                 int64_t j;                                              \
771fcf5ef2aSThomas Huth                                                                         \
772fcf5ef2aSThomas Huth                 t = float64_scalbn(t, uim, &s);                         \
773fcf5ef2aSThomas Huth                 j = float64_to_int64(t, &s);                            \
774fcf5ef2aSThomas Huth                 r->element[i] = satcvt(j, &sat);                        \
775fcf5ef2aSThomas Huth             }                                                           \
776fcf5ef2aSThomas Huth         }                                                               \
777fcf5ef2aSThomas Huth         if (sat) {                                                      \
7786175f5a0SRichard Henderson             set_vscr_sat(env);                                          \
779fcf5ef2aSThomas Huth         }                                                               \
780fcf5ef2aSThomas Huth     }
781fcf5ef2aSThomas Huth VCT(uxs, cvtsduw, u32)
782fcf5ef2aSThomas Huth VCT(sxs, cvtsdsw, s32)
783fcf5ef2aSThomas Huth #undef VCT
784fcf5ef2aSThomas Huth 
78534553153SLucas Mateus Castro (alqotel) typedef int64_t do_ger(uint32_t, uint32_t, uint32_t);
78634553153SLucas Mateus Castro (alqotel) 
78734553153SLucas Mateus Castro (alqotel) static int64_t ger_rank8(uint32_t a, uint32_t b, uint32_t mask)
78834553153SLucas Mateus Castro (alqotel) {
78934553153SLucas Mateus Castro (alqotel)     int64_t psum = 0;
79034553153SLucas Mateus Castro (alqotel)     for (int i = 0; i < 8; i++, mask >>= 1) {
79134553153SLucas Mateus Castro (alqotel)         if (mask & 1) {
792feeef6b6SDaniel Henrique Barboza             psum += (int64_t)sextract32(a, 4 * i, 4) * sextract32(b, 4 * i, 4);
79334553153SLucas Mateus Castro (alqotel)         }
79434553153SLucas Mateus Castro (alqotel)     }
79534553153SLucas Mateus Castro (alqotel)     return psum;
79634553153SLucas Mateus Castro (alqotel) }
79734553153SLucas Mateus Castro (alqotel) 
79834553153SLucas Mateus Castro (alqotel) static int64_t ger_rank4(uint32_t a, uint32_t b, uint32_t mask)
79934553153SLucas Mateus Castro (alqotel) {
80034553153SLucas Mateus Castro (alqotel)     int64_t psum = 0;
80134553153SLucas Mateus Castro (alqotel)     for (int i = 0; i < 4; i++, mask >>= 1) {
80234553153SLucas Mateus Castro (alqotel)         if (mask & 1) {
80334553153SLucas Mateus Castro (alqotel)             psum += sextract32(a, 8 * i, 8) * (int64_t)extract32(b, 8 * i, 8);
80434553153SLucas Mateus Castro (alqotel)         }
80534553153SLucas Mateus Castro (alqotel)     }
80634553153SLucas Mateus Castro (alqotel)     return psum;
80734553153SLucas Mateus Castro (alqotel) }
80834553153SLucas Mateus Castro (alqotel) 
80934553153SLucas Mateus Castro (alqotel) static int64_t ger_rank2(uint32_t a, uint32_t b, uint32_t mask)
81034553153SLucas Mateus Castro (alqotel) {
81134553153SLucas Mateus Castro (alqotel)     int64_t psum = 0;
81234553153SLucas Mateus Castro (alqotel)     for (int i = 0; i < 2; i++, mask >>= 1) {
81334553153SLucas Mateus Castro (alqotel)         if (mask & 1) {
814feeef6b6SDaniel Henrique Barboza             psum += (int64_t)sextract32(a, 16 * i, 16) *
815feeef6b6SDaniel Henrique Barboza                              sextract32(b, 16 * i, 16);
81634553153SLucas Mateus Castro (alqotel)         }
81734553153SLucas Mateus Castro (alqotel)     }
81834553153SLucas Mateus Castro (alqotel)     return psum;
81934553153SLucas Mateus Castro (alqotel) }
82034553153SLucas Mateus Castro (alqotel) 
82134553153SLucas Mateus Castro (alqotel) static void xviger(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b, ppc_acc_t  *at,
82234553153SLucas Mateus Castro (alqotel)                    uint32_t mask, bool sat, bool acc, do_ger ger)
82334553153SLucas Mateus Castro (alqotel) {
82434553153SLucas Mateus Castro (alqotel)     uint8_t pmsk = FIELD_EX32(mask, GER_MSK, PMSK),
82534553153SLucas Mateus Castro (alqotel)             xmsk = FIELD_EX32(mask, GER_MSK, XMSK),
82634553153SLucas Mateus Castro (alqotel)             ymsk = FIELD_EX32(mask, GER_MSK, YMSK);
82734553153SLucas Mateus Castro (alqotel)     uint8_t xmsk_bit, ymsk_bit;
82834553153SLucas Mateus Castro (alqotel)     int64_t psum;
82934553153SLucas Mateus Castro (alqotel)     int i, j;
83034553153SLucas Mateus Castro (alqotel)     for (i = 0, xmsk_bit = 1 << 3; i < 4; i++, xmsk_bit >>= 1) {
83134553153SLucas Mateus Castro (alqotel)         for (j = 0, ymsk_bit = 1 << 3; j < 4; j++, ymsk_bit >>= 1) {
83234553153SLucas Mateus Castro (alqotel)             if ((xmsk_bit & xmsk) && (ymsk_bit & ymsk)) {
83334553153SLucas Mateus Castro (alqotel)                 psum = ger(a->VsrW(i), b->VsrW(j), pmsk);
83434553153SLucas Mateus Castro (alqotel)                 if (acc) {
83534553153SLucas Mateus Castro (alqotel)                     psum += at[i].VsrSW(j);
83634553153SLucas Mateus Castro (alqotel)                 }
83734553153SLucas Mateus Castro (alqotel)                 if (sat && psum > INT32_MAX) {
83834553153SLucas Mateus Castro (alqotel)                     set_vscr_sat(env);
83934553153SLucas Mateus Castro (alqotel)                     at[i].VsrSW(j) = INT32_MAX;
84034553153SLucas Mateus Castro (alqotel)                 } else if (sat && psum < INT32_MIN) {
84134553153SLucas Mateus Castro (alqotel)                     set_vscr_sat(env);
84234553153SLucas Mateus Castro (alqotel)                     at[i].VsrSW(j) = INT32_MIN;
84334553153SLucas Mateus Castro (alqotel)                 } else {
84434553153SLucas Mateus Castro (alqotel)                     at[i].VsrSW(j) = (int32_t) psum;
84534553153SLucas Mateus Castro (alqotel)                 }
84634553153SLucas Mateus Castro (alqotel)             } else {
84734553153SLucas Mateus Castro (alqotel)                 at[i].VsrSW(j) = 0;
84834553153SLucas Mateus Castro (alqotel)             }
84934553153SLucas Mateus Castro (alqotel)         }
85034553153SLucas Mateus Castro (alqotel)     }
85134553153SLucas Mateus Castro (alqotel) }
85234553153SLucas Mateus Castro (alqotel) 
85334553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
85434553153SLucas Mateus Castro (alqotel) void helper_XVI4GER8(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
85534553153SLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
85634553153SLucas Mateus Castro (alqotel) {
85734553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, false, ger_rank8);
85834553153SLucas Mateus Castro (alqotel) }
85934553153SLucas Mateus Castro (alqotel) 
86034553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
86134553153SLucas Mateus Castro (alqotel) void helper_XVI4GER8PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
86234553153SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
86334553153SLucas Mateus Castro (alqotel) {
86434553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, true, ger_rank8);
86534553153SLucas Mateus Castro (alqotel) }
86634553153SLucas Mateus Castro (alqotel) 
86734553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
86834553153SLucas Mateus Castro (alqotel) void helper_XVI8GER4(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
86934553153SLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
87034553153SLucas Mateus Castro (alqotel) {
87134553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, false, ger_rank4);
87234553153SLucas Mateus Castro (alqotel) }
87334553153SLucas Mateus Castro (alqotel) 
87434553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
87534553153SLucas Mateus Castro (alqotel) void helper_XVI8GER4PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
87634553153SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
87734553153SLucas Mateus Castro (alqotel) {
87834553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, true, ger_rank4);
87934553153SLucas Mateus Castro (alqotel) }
88034553153SLucas Mateus Castro (alqotel) 
88134553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
88234553153SLucas Mateus Castro (alqotel) void helper_XVI8GER4SPP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
88334553153SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
88434553153SLucas Mateus Castro (alqotel) {
88534553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, true, true, ger_rank4);
88634553153SLucas Mateus Castro (alqotel) }
88734553153SLucas Mateus Castro (alqotel) 
88834553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
88934553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
89034553153SLucas Mateus Castro (alqotel)                       ppc_acc_t *at, uint32_t mask)
89134553153SLucas Mateus Castro (alqotel) {
89234553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, false, ger_rank2);
89334553153SLucas Mateus Castro (alqotel) }
89434553153SLucas Mateus Castro (alqotel) 
89534553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
89634553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2S(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
89734553153SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
89834553153SLucas Mateus Castro (alqotel) {
89934553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, true, false, ger_rank2);
90034553153SLucas Mateus Castro (alqotel) }
90134553153SLucas Mateus Castro (alqotel) 
90234553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
90334553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
90434553153SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
90534553153SLucas Mateus Castro (alqotel) {
90634553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, true, ger_rank2);
90734553153SLucas Mateus Castro (alqotel) }
90834553153SLucas Mateus Castro (alqotel) 
90934553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
91034553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2SPP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
91134553153SLucas Mateus Castro (alqotel)                          ppc_acc_t *at, uint32_t mask)
91234553153SLucas Mateus Castro (alqotel) {
91334553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, true, true, ger_rank2);
91434553153SLucas Mateus Castro (alqotel) }
91534553153SLucas Mateus Castro (alqotel) 
916fcf5ef2aSThomas Huth target_ulong helper_vclzlsbb(ppc_avr_t *r)
917fcf5ef2aSThomas Huth {
918fcf5ef2aSThomas Huth     target_ulong count = 0;
919fcf5ef2aSThomas Huth     int i;
92060594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
92160594feaSMark Cave-Ayland         if (r->VsrB(i) & 0x01) {
922fcf5ef2aSThomas Huth             break;
923fcf5ef2aSThomas Huth         }
924fcf5ef2aSThomas Huth         count++;
925fcf5ef2aSThomas Huth     }
926fcf5ef2aSThomas Huth     return count;
927fcf5ef2aSThomas Huth }
928fcf5ef2aSThomas Huth 
929fcf5ef2aSThomas Huth target_ulong helper_vctzlsbb(ppc_avr_t *r)
930fcf5ef2aSThomas Huth {
931fcf5ef2aSThomas Huth     target_ulong count = 0;
932fcf5ef2aSThomas Huth     int i;
933fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
93460594feaSMark Cave-Ayland         if (r->VsrB(i) & 0x01) {
935fcf5ef2aSThomas Huth             break;
936fcf5ef2aSThomas Huth         }
937fcf5ef2aSThomas Huth         count++;
938fcf5ef2aSThomas Huth     }
939fcf5ef2aSThomas Huth     return count;
940fcf5ef2aSThomas Huth }
941fcf5ef2aSThomas Huth 
942fcf5ef2aSThomas Huth void helper_vmhaddshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
943fcf5ef2aSThomas Huth                       ppc_avr_t *b, ppc_avr_t *c)
944fcf5ef2aSThomas Huth {
945fcf5ef2aSThomas Huth     int sat = 0;
946fcf5ef2aSThomas Huth     int i;
947fcf5ef2aSThomas Huth 
948fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
949fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
950fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
951fcf5ef2aSThomas Huth 
952fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
953fcf5ef2aSThomas Huth     }
954fcf5ef2aSThomas Huth 
955fcf5ef2aSThomas Huth     if (sat) {
9566175f5a0SRichard Henderson         set_vscr_sat(env);
957fcf5ef2aSThomas Huth     }
958fcf5ef2aSThomas Huth }
959fcf5ef2aSThomas Huth 
960fcf5ef2aSThomas Huth void helper_vmhraddshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
961fcf5ef2aSThomas Huth                        ppc_avr_t *b, ppc_avr_t *c)
962fcf5ef2aSThomas Huth {
963fcf5ef2aSThomas Huth     int sat = 0;
964fcf5ef2aSThomas Huth     int i;
965fcf5ef2aSThomas Huth 
966fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
967fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i] + 0x00004000;
968fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
969fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
970fcf5ef2aSThomas Huth     }
971fcf5ef2aSThomas Huth 
972fcf5ef2aSThomas Huth     if (sat) {
9736175f5a0SRichard Henderson         set_vscr_sat(env);
974fcf5ef2aSThomas Huth     }
975fcf5ef2aSThomas Huth }
976fcf5ef2aSThomas Huth 
977fcf5ef2aSThomas Huth void helper_vmladduhm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
978fcf5ef2aSThomas Huth {
979fcf5ef2aSThomas Huth     int i;
980fcf5ef2aSThomas Huth 
981fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
982fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
983fcf5ef2aSThomas Huth         r->s16[i] = (int16_t) (prod + c->s16[i]);
984fcf5ef2aSThomas Huth     }
985fcf5ef2aSThomas Huth }
986fcf5ef2aSThomas Huth 
987d81c2040SMark Cave-Ayland #define VMRG_DO(name, element, access, ofs)                                  \
988fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)            \
989fcf5ef2aSThomas Huth     {                                                                        \
990fcf5ef2aSThomas Huth         ppc_avr_t result;                                                    \
991d81c2040SMark Cave-Ayland         int i, half = ARRAY_SIZE(r->element) / 2;                            \
992fcf5ef2aSThomas Huth                                                                              \
993d81c2040SMark Cave-Ayland         for (i = 0; i < half; i++) {                                         \
994d81c2040SMark Cave-Ayland             result.access(i * 2 + 0) = a->access(i + ofs);                   \
995d81c2040SMark Cave-Ayland             result.access(i * 2 + 1) = b->access(i + ofs);                   \
996fcf5ef2aSThomas Huth         }                                                                    \
997fcf5ef2aSThomas Huth         *r = result;                                                         \
998fcf5ef2aSThomas Huth     }
999d81c2040SMark Cave-Ayland 
1000d81c2040SMark Cave-Ayland #define VMRG(suffix, element, access)          \
1001d81c2040SMark Cave-Ayland     VMRG_DO(mrgl##suffix, element, access, half)   \
1002d81c2040SMark Cave-Ayland     VMRG_DO(mrgh##suffix, element, access, 0)
1003d81c2040SMark Cave-Ayland VMRG(b, u8, VsrB)
1004d81c2040SMark Cave-Ayland VMRG(h, u16, VsrH)
1005d81c2040SMark Cave-Ayland VMRG(w, u32, VsrW)
1006fcf5ef2aSThomas Huth #undef VMRG_DO
1007fcf5ef2aSThomas Huth #undef VMRG
1008fcf5ef2aSThomas Huth 
1009b2dc03a5SMatheus Ferst void helper_VMSUMMBM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1010fcf5ef2aSThomas Huth {
1011fcf5ef2aSThomas Huth     int32_t prod[16];
1012fcf5ef2aSThomas Huth     int i;
1013fcf5ef2aSThomas Huth 
1014fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s8); i++) {
1015fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s8[i] * b->u8[i];
1016fcf5ef2aSThomas Huth     }
1017fcf5ef2aSThomas Huth 
1018fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1019fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[4 * i] + prod[4 * i + 1] +
1020fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
1021fcf5ef2aSThomas Huth     }
1022fcf5ef2aSThomas Huth }
1023fcf5ef2aSThomas Huth 
10246f52f731SMatheus Ferst void helper_VMSUMSHM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1025fcf5ef2aSThomas Huth {
1026fcf5ef2aSThomas Huth     int32_t prod[8];
1027fcf5ef2aSThomas Huth     int i;
1028fcf5ef2aSThomas Huth 
1029fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
1030fcf5ef2aSThomas Huth         prod[i] = a->s16[i] * b->s16[i];
1031fcf5ef2aSThomas Huth     }
1032fcf5ef2aSThomas Huth 
1033fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1034fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[2 * i] + prod[2 * i + 1];
1035fcf5ef2aSThomas Huth     }
1036fcf5ef2aSThomas Huth }
1037fcf5ef2aSThomas Huth 
10386f52f731SMatheus Ferst void helper_VMSUMSHS(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1039fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1040fcf5ef2aSThomas Huth {
1041fcf5ef2aSThomas Huth     int32_t prod[8];
1042fcf5ef2aSThomas Huth     int i;
1043fcf5ef2aSThomas Huth     int sat = 0;
1044fcf5ef2aSThomas Huth 
1045fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
1046fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s16[i] * b->s16[i];
1047fcf5ef2aSThomas Huth     }
1048fcf5ef2aSThomas Huth 
1049fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1050fcf5ef2aSThomas Huth         int64_t t = (int64_t)c->s32[i] + prod[2 * i] + prod[2 * i + 1];
1051fcf5ef2aSThomas Huth 
1052fcf5ef2aSThomas Huth         r->u32[i] = cvtsdsw(t, &sat);
1053fcf5ef2aSThomas Huth     }
1054fcf5ef2aSThomas Huth 
1055fcf5ef2aSThomas Huth     if (sat) {
10566175f5a0SRichard Henderson         set_vscr_sat(env);
1057fcf5ef2aSThomas Huth     }
1058fcf5ef2aSThomas Huth }
1059fcf5ef2aSThomas Huth 
1060b2dc03a5SMatheus Ferst void helper_VMSUMUBM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1061fcf5ef2aSThomas Huth {
1062fcf5ef2aSThomas Huth     uint16_t prod[16];
1063fcf5ef2aSThomas Huth     int i;
1064fcf5ef2aSThomas Huth 
1065fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1066fcf5ef2aSThomas Huth         prod[i] = a->u8[i] * b->u8[i];
1067fcf5ef2aSThomas Huth     }
1068fcf5ef2aSThomas Huth 
1069fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1070fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[4 * i] + prod[4 * i + 1] +
1071fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
1072fcf5ef2aSThomas Huth     }
1073fcf5ef2aSThomas Huth }
1074fcf5ef2aSThomas Huth 
107589a5a1aeSMatheus Ferst void helper_VMSUMUHM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1076fcf5ef2aSThomas Huth {
1077fcf5ef2aSThomas Huth     uint32_t prod[8];
1078fcf5ef2aSThomas Huth     int i;
1079fcf5ef2aSThomas Huth 
1080fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1081fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1082fcf5ef2aSThomas Huth     }
1083fcf5ef2aSThomas Huth 
1084fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1085fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1086fcf5ef2aSThomas Huth     }
1087fcf5ef2aSThomas Huth }
1088fcf5ef2aSThomas Huth 
108989a5a1aeSMatheus Ferst void helper_VMSUMUHS(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1090fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1091fcf5ef2aSThomas Huth {
1092fcf5ef2aSThomas Huth     uint32_t prod[8];
1093fcf5ef2aSThomas Huth     int i;
1094fcf5ef2aSThomas Huth     int sat = 0;
1095fcf5ef2aSThomas Huth 
1096fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1097fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1098fcf5ef2aSThomas Huth     }
1099fcf5ef2aSThomas Huth 
1100fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1101fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1102fcf5ef2aSThomas Huth 
1103fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
1104fcf5ef2aSThomas Huth     }
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth     if (sat) {
11076175f5a0SRichard Henderson         set_vscr_sat(env);
1108fcf5ef2aSThomas Huth     }
1109fcf5ef2aSThomas Huth }
1110fcf5ef2aSThomas Huth 
11114fbc89edSMark Cave-Ayland #define VMUL_DO_EVN(name, mul_element, mul_access, prod_access, cast)   \
111280eca687SLucas Mateus Castro (alqotel)     void helper_V##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
1113fcf5ef2aSThomas Huth     {                                                                   \
1114fcf5ef2aSThomas Huth         int i;                                                          \
1115fcf5ef2aSThomas Huth                                                                         \
11164fbc89edSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) {           \
11174fbc89edSMark Cave-Ayland             r->prod_access(i >> 1) = (cast)a->mul_access(i) *           \
11184fbc89edSMark Cave-Ayland                                      (cast)b->mul_access(i);            \
1119fcf5ef2aSThomas Huth         }                                                               \
1120fcf5ef2aSThomas Huth     }
11214fbc89edSMark Cave-Ayland 
11224fbc89edSMark Cave-Ayland #define VMUL_DO_ODD(name, mul_element, mul_access, prod_access, cast)   \
112380eca687SLucas Mateus Castro (alqotel)     void helper_V##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
11244fbc89edSMark Cave-Ayland     {                                                                   \
11254fbc89edSMark Cave-Ayland         int i;                                                          \
11264fbc89edSMark Cave-Ayland                                                                         \
11274fbc89edSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) {           \
11284fbc89edSMark Cave-Ayland             r->prod_access(i >> 1) = (cast)a->mul_access(i + 1) *       \
11294fbc89edSMark Cave-Ayland                                      (cast)b->mul_access(i + 1);        \
11304fbc89edSMark Cave-Ayland         }                                                               \
11314fbc89edSMark Cave-Ayland     }
11324fbc89edSMark Cave-Ayland 
11334fbc89edSMark Cave-Ayland #define VMUL(suffix, mul_element, mul_access, prod_access, cast)       \
113480eca687SLucas Mateus Castro (alqotel)     VMUL_DO_EVN(MULE##suffix, mul_element, mul_access, prod_access, cast)  \
113580eca687SLucas Mateus Castro (alqotel)     VMUL_DO_ODD(MULO##suffix, mul_element, mul_access, prod_access, cast)
113680eca687SLucas Mateus Castro (alqotel) VMUL(SB, s8, VsrSB, VsrSH, int16_t)
113780eca687SLucas Mateus Castro (alqotel) VMUL(SH, s16, VsrSH, VsrSW, int32_t)
113880eca687SLucas Mateus Castro (alqotel) VMUL(SW, s32, VsrSW, VsrSD, int64_t)
113980eca687SLucas Mateus Castro (alqotel) VMUL(UB, u8, VsrB, VsrH, uint16_t)
114080eca687SLucas Mateus Castro (alqotel) VMUL(UH, u16, VsrH, VsrW, uint32_t)
114180eca687SLucas Mateus Castro (alqotel) VMUL(UW, u32, VsrW, VsrD, uint64_t)
11424fbc89edSMark Cave-Ayland #undef VMUL_DO_EVN
11434fbc89edSMark Cave-Ayland #undef VMUL_DO_ODD
1144fcf5ef2aSThomas Huth #undef VMUL
1145fcf5ef2aSThomas Huth 
114641c2877fSMatheus Ferst void helper_XXPERMX(ppc_vsr_t *t, ppc_vsr_t *s0, ppc_vsr_t *s1, ppc_vsr_t *pcv,
114741c2877fSMatheus Ferst                     target_ulong uim)
114841c2877fSMatheus Ferst {
114941c2877fSMatheus Ferst     int i, idx;
115041c2877fSMatheus Ferst     ppc_vsr_t tmp = { .u64 = {0, 0} };
115141c2877fSMatheus Ferst 
115241c2877fSMatheus Ferst     for (i = 0; i < ARRAY_SIZE(t->u8); i++) {
115341c2877fSMatheus Ferst         if ((pcv->VsrB(i) >> 5) == uim) {
115441c2877fSMatheus Ferst             idx = pcv->VsrB(i) & 0x1f;
115541c2877fSMatheus Ferst             if (idx < ARRAY_SIZE(t->u8)) {
115641c2877fSMatheus Ferst                 tmp.VsrB(i) = s0->VsrB(idx);
115741c2877fSMatheus Ferst             } else {
115841c2877fSMatheus Ferst                 tmp.VsrB(i) = s1->VsrB(idx - ARRAY_SIZE(t->u8));
115941c2877fSMatheus Ferst             }
116041c2877fSMatheus Ferst         }
116141c2877fSMatheus Ferst     }
116241c2877fSMatheus Ferst 
116341c2877fSMatheus Ferst     *t = tmp;
116441c2877fSMatheus Ferst }
116541c2877fSMatheus Ferst 
11661700f2bfSLucas Mateus Castro (alqotel) void helper_VDIVSQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
11671700f2bfSLucas Mateus Castro (alqotel) {
11681700f2bfSLucas Mateus Castro (alqotel)     Int128 neg1 = int128_makes64(-1);
11691700f2bfSLucas Mateus Castro (alqotel)     Int128 int128_min = int128_make128(0, INT64_MIN);
11701700f2bfSLucas Mateus Castro (alqotel)     if (likely(int128_nz(b->s128) &&
11711700f2bfSLucas Mateus Castro (alqotel)               (int128_ne(a->s128, int128_min) || int128_ne(b->s128, neg1)))) {
11721700f2bfSLucas Mateus Castro (alqotel)         t->s128 = int128_divs(a->s128, b->s128);
11731700f2bfSLucas Mateus Castro (alqotel)     } else {
11741700f2bfSLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
11751700f2bfSLucas Mateus Castro (alqotel)     }
11761700f2bfSLucas Mateus Castro (alqotel) }
11771700f2bfSLucas Mateus Castro (alqotel) 
11781700f2bfSLucas Mateus Castro (alqotel) void helper_VDIVUQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
11791700f2bfSLucas Mateus Castro (alqotel) {
11801700f2bfSLucas Mateus Castro (alqotel)     if (int128_nz(b->s128)) {
11811700f2bfSLucas Mateus Castro (alqotel)         t->s128 = int128_divu(a->s128, b->s128);
11821700f2bfSLucas Mateus Castro (alqotel)     } else {
11831700f2bfSLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
11841700f2bfSLucas Mateus Castro (alqotel)     }
11851700f2bfSLucas Mateus Castro (alqotel) }
11861700f2bfSLucas Mateus Castro (alqotel) 
1187a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVESD(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1188a173ba88SLucas Mateus Castro (alqotel) {
1189a173ba88SLucas Mateus Castro (alqotel)     int i;
1190a173ba88SLucas Mateus Castro (alqotel)     int64_t high;
1191a173ba88SLucas Mateus Castro (alqotel)     uint64_t low;
1192a173ba88SLucas Mateus Castro (alqotel)     for (i = 0; i < 2; i++) {
1193a173ba88SLucas Mateus Castro (alqotel)         high = a->s64[i];
1194a173ba88SLucas Mateus Castro (alqotel)         low = 0;
1195a173ba88SLucas Mateus Castro (alqotel)         if (unlikely((high == INT64_MIN && b->s64[i] == -1) || !b->s64[i])) {
1196a173ba88SLucas Mateus Castro (alqotel)             t->s64[i] = a->s64[i]; /* Undefined behavior */
1197a173ba88SLucas Mateus Castro (alqotel)         } else {
1198a173ba88SLucas Mateus Castro (alqotel)             divs128(&low, &high, b->s64[i]);
1199a173ba88SLucas Mateus Castro (alqotel)             t->s64[i] = low;
1200a173ba88SLucas Mateus Castro (alqotel)         }
1201a173ba88SLucas Mateus Castro (alqotel)     }
1202a173ba88SLucas Mateus Castro (alqotel) }
1203a173ba88SLucas Mateus Castro (alqotel) 
1204a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVEUD(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1205a173ba88SLucas Mateus Castro (alqotel) {
1206a173ba88SLucas Mateus Castro (alqotel)     int i;
1207a173ba88SLucas Mateus Castro (alqotel)     uint64_t high, low;
1208a173ba88SLucas Mateus Castro (alqotel)     for (i = 0; i < 2; i++) {
1209a173ba88SLucas Mateus Castro (alqotel)         high = a->u64[i];
1210a173ba88SLucas Mateus Castro (alqotel)         low = 0;
1211a173ba88SLucas Mateus Castro (alqotel)         if (unlikely(!b->u64[i])) {
1212a173ba88SLucas Mateus Castro (alqotel)             t->u64[i] = a->u64[i]; /* Undefined behavior */
1213a173ba88SLucas Mateus Castro (alqotel)         } else {
1214a173ba88SLucas Mateus Castro (alqotel)             divu128(&low, &high, b->u64[i]);
1215a173ba88SLucas Mateus Castro (alqotel)             t->u64[i] = low;
1216a173ba88SLucas Mateus Castro (alqotel)         }
1217a173ba88SLucas Mateus Castro (alqotel)     }
1218a173ba88SLucas Mateus Castro (alqotel) }
1219a173ba88SLucas Mateus Castro (alqotel) 
1220a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVESQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1221a173ba88SLucas Mateus Castro (alqotel) {
1222a173ba88SLucas Mateus Castro (alqotel)     Int128 high, low;
1223a173ba88SLucas Mateus Castro (alqotel)     Int128 int128_min = int128_make128(0, INT64_MIN);
1224a173ba88SLucas Mateus Castro (alqotel)     Int128 neg1 = int128_makes64(-1);
1225a173ba88SLucas Mateus Castro (alqotel) 
1226a173ba88SLucas Mateus Castro (alqotel)     high = a->s128;
1227a173ba88SLucas Mateus Castro (alqotel)     low = int128_zero();
1228a173ba88SLucas Mateus Castro (alqotel)     if (unlikely(!int128_nz(b->s128) ||
1229a173ba88SLucas Mateus Castro (alqotel)                  (int128_eq(b->s128, neg1) && int128_eq(high, int128_min)))) {
1230a173ba88SLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
1231a173ba88SLucas Mateus Castro (alqotel)     } else {
1232a173ba88SLucas Mateus Castro (alqotel)         divs256(&low, &high, b->s128);
1233a173ba88SLucas Mateus Castro (alqotel)         t->s128 = low;
1234a173ba88SLucas Mateus Castro (alqotel)     }
1235a173ba88SLucas Mateus Castro (alqotel) }
1236a173ba88SLucas Mateus Castro (alqotel) 
1237a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVEUQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1238a173ba88SLucas Mateus Castro (alqotel) {
1239a173ba88SLucas Mateus Castro (alqotel)     Int128 high, low;
1240a173ba88SLucas Mateus Castro (alqotel) 
1241a173ba88SLucas Mateus Castro (alqotel)     high = a->s128;
1242a173ba88SLucas Mateus Castro (alqotel)     low = int128_zero();
1243a173ba88SLucas Mateus Castro (alqotel)     if (unlikely(!int128_nz(b->s128))) {
1244a173ba88SLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
1245a173ba88SLucas Mateus Castro (alqotel)     } else {
1246a173ba88SLucas Mateus Castro (alqotel)         divu256(&low, &high, b->s128);
1247a173ba88SLucas Mateus Castro (alqotel)         t->s128 = low;
1248a173ba88SLucas Mateus Castro (alqotel)     }
1249a173ba88SLucas Mateus Castro (alqotel) }
1250a173ba88SLucas Mateus Castro (alqotel) 
1251b80bec3aSLucas Mateus Castro (alqotel) void helper_VMODSQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1252b80bec3aSLucas Mateus Castro (alqotel) {
1253b80bec3aSLucas Mateus Castro (alqotel)     Int128 neg1 = int128_makes64(-1);
1254b80bec3aSLucas Mateus Castro (alqotel)     Int128 int128_min = int128_make128(0, INT64_MIN);
1255b80bec3aSLucas Mateus Castro (alqotel)     if (likely(int128_nz(b->s128) &&
1256b80bec3aSLucas Mateus Castro (alqotel)               (int128_ne(a->s128, int128_min) || int128_ne(b->s128, neg1)))) {
1257b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_rems(a->s128, b->s128);
1258b80bec3aSLucas Mateus Castro (alqotel)     } else {
1259b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_zero(); /* Undefined behavior */
1260b80bec3aSLucas Mateus Castro (alqotel)     }
1261b80bec3aSLucas Mateus Castro (alqotel) }
1262b80bec3aSLucas Mateus Castro (alqotel) 
1263b80bec3aSLucas Mateus Castro (alqotel) void helper_VMODUQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1264b80bec3aSLucas Mateus Castro (alqotel) {
1265b80bec3aSLucas Mateus Castro (alqotel)     if (likely(int128_nz(b->s128))) {
1266b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_remu(a->s128, b->s128);
1267b80bec3aSLucas Mateus Castro (alqotel)     } else {
1268b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_zero(); /* Undefined behavior */
1269b80bec3aSLucas Mateus Castro (alqotel)     }
1270b80bec3aSLucas Mateus Castro (alqotel) }
1271b80bec3aSLucas Mateus Castro (alqotel) 
127228347fe2SMatheus Ferst void helper_VPERM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1273fcf5ef2aSThomas Huth {
1274fcf5ef2aSThomas Huth     ppc_avr_t result;
1275fcf5ef2aSThomas Huth     int i;
1276fcf5ef2aSThomas Huth 
127760594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
127860594feaSMark Cave-Ayland         int s = c->VsrB(i) & 0x1f;
1279fcf5ef2aSThomas Huth         int index = s & 0xf;
1280fcf5ef2aSThomas Huth 
1281fcf5ef2aSThomas Huth         if (s & 0x10) {
128260594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index);
1283fcf5ef2aSThomas Huth         } else {
128460594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1285fcf5ef2aSThomas Huth         }
1286fcf5ef2aSThomas Huth     }
1287fcf5ef2aSThomas Huth     *r = result;
1288fcf5ef2aSThomas Huth }
1289fcf5ef2aSThomas Huth 
129028347fe2SMatheus Ferst void helper_VPERMR(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1291fcf5ef2aSThomas Huth {
1292fcf5ef2aSThomas Huth     ppc_avr_t result;
1293fcf5ef2aSThomas Huth     int i;
1294fcf5ef2aSThomas Huth 
129560594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
129660594feaSMark Cave-Ayland         int s = c->VsrB(i) & 0x1f;
1297fcf5ef2aSThomas Huth         int index = 15 - (s & 0xf);
1298fcf5ef2aSThomas Huth 
1299fcf5ef2aSThomas Huth         if (s & 0x10) {
130060594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1301fcf5ef2aSThomas Huth         } else {
130260594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index);
1303fcf5ef2aSThomas Huth         }
1304fcf5ef2aSThomas Huth     }
1305fcf5ef2aSThomas Huth     *r = result;
1306fcf5ef2aSThomas Huth }
1307fcf5ef2aSThomas Huth 
1308618574ddSMatheus Ferst #define XXGENPCV_BE_EXP(NAME, SZ) \
1309b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _be_exp))(ppc_vsr_t *t, ppc_vsr_t *b) \
1310b090f4f1SMatheus Ferst {                                                                   \
1311b090f4f1SMatheus Ferst     ppc_vsr_t tmp;                                                  \
1312b090f4f1SMatheus Ferst                                                                     \
1313b090f4f1SMatheus Ferst     /* Initialize tmp with the result of an all-zeros mask */       \
1314b090f4f1SMatheus Ferst     tmp.VsrD(0) = 0x1011121314151617;                               \
1315b090f4f1SMatheus Ferst     tmp.VsrD(1) = 0x18191A1B1C1D1E1F;                               \
1316b090f4f1SMatheus Ferst                                                                     \
1317b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1318b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1319b090f4f1SMatheus Ferst         if (b->VsrB(i) & 0x80) {                                    \
1320b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1321b090f4f1SMatheus Ferst             for (int k = 0; k < SZ; k++) {                          \
1322b090f4f1SMatheus Ferst                 tmp.VsrB(i + k) = j + k;                            \
1323b090f4f1SMatheus Ferst             }                                                       \
1324b090f4f1SMatheus Ferst             j += SZ;                                                \
1325b090f4f1SMatheus Ferst         }                                                           \
1326b090f4f1SMatheus Ferst     }                                                               \
1327b090f4f1SMatheus Ferst                                                                     \
1328b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1329618574ddSMatheus Ferst }
1330618574ddSMatheus Ferst 
1331618574ddSMatheus Ferst #define XXGENPCV_BE_COMP(NAME, SZ) \
1332b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _be_comp))(ppc_vsr_t *t, ppc_vsr_t *b)\
1333b090f4f1SMatheus Ferst {                                                                   \
1334b090f4f1SMatheus Ferst     ppc_vsr_t tmp = { .u64 = { 0, 0 } };                            \
1335b090f4f1SMatheus Ferst                                                                     \
1336b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1337b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1338b090f4f1SMatheus Ferst         if (b->VsrB(i) & 0x80) {                                    \
1339b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1340b090f4f1SMatheus Ferst             for (int k = 0; k < SZ; k++) {                          \
1341b090f4f1SMatheus Ferst                 tmp.VsrB(j + k) = i + k;                            \
1342b090f4f1SMatheus Ferst             }                                                       \
1343b090f4f1SMatheus Ferst             j += SZ;                                                \
1344b090f4f1SMatheus Ferst         }                                                           \
1345b090f4f1SMatheus Ferst     }                                                               \
1346b090f4f1SMatheus Ferst                                                                     \
1347b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1348618574ddSMatheus Ferst }
1349618574ddSMatheus Ferst 
1350618574ddSMatheus Ferst #define XXGENPCV_LE_EXP(NAME, SZ) \
1351b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _le_exp))(ppc_vsr_t *t, ppc_vsr_t *b) \
1352b090f4f1SMatheus Ferst {                                                                   \
1353b090f4f1SMatheus Ferst     ppc_vsr_t tmp;                                                  \
1354b090f4f1SMatheus Ferst                                                                     \
1355b090f4f1SMatheus Ferst     /* Initialize tmp with the result of an all-zeros mask */       \
1356b090f4f1SMatheus Ferst     tmp.VsrD(0) = 0x1F1E1D1C1B1A1918;                               \
1357b090f4f1SMatheus Ferst     tmp.VsrD(1) = 0x1716151413121110;                               \
1358b090f4f1SMatheus Ferst                                                                     \
1359b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1360b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1361b090f4f1SMatheus Ferst         /* Reverse indexing of "i" */                               \
1362b090f4f1SMatheus Ferst         const int idx = ARRAY_SIZE(b->u8) - i - SZ;                 \
1363b090f4f1SMatheus Ferst         if (b->VsrB(idx) & 0x80) {                                  \
1364b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1365b090f4f1SMatheus Ferst             for (int k = 0, rk = SZ - 1; k < SZ; k++, rk--) {       \
1366b090f4f1SMatheus Ferst                 tmp.VsrB(idx + rk) = j + k;                         \
1367b090f4f1SMatheus Ferst             }                                                       \
1368b090f4f1SMatheus Ferst             j += SZ;                                                \
1369b090f4f1SMatheus Ferst         }                                                           \
1370b090f4f1SMatheus Ferst     }                                                               \
1371b090f4f1SMatheus Ferst                                                                     \
1372b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1373618574ddSMatheus Ferst }
1374618574ddSMatheus Ferst 
1375618574ddSMatheus Ferst #define XXGENPCV_LE_COMP(NAME, SZ) \
1376b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _le_comp))(ppc_vsr_t *t, ppc_vsr_t *b)\
1377b090f4f1SMatheus Ferst {                                                                   \
1378b090f4f1SMatheus Ferst     ppc_vsr_t tmp = { .u64 = { 0, 0 } };                            \
1379b090f4f1SMatheus Ferst                                                                     \
1380b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1381b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1382b090f4f1SMatheus Ferst         if (b->VsrB(ARRAY_SIZE(b->u8) - i - SZ) & 0x80) {           \
1383b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1384b090f4f1SMatheus Ferst             for (int k = 0, rk = SZ - 1; k < SZ; k++, rk--) {       \
1385b090f4f1SMatheus Ferst                 /* Reverse indexing of "j" */                       \
1386b090f4f1SMatheus Ferst                 const int idx = ARRAY_SIZE(b->u8) - j - SZ;         \
1387b090f4f1SMatheus Ferst                 tmp.VsrB(idx + rk) = i + k;                         \
1388b090f4f1SMatheus Ferst             }                                                       \
1389b090f4f1SMatheus Ferst             j += SZ;                                                \
1390b090f4f1SMatheus Ferst         }                                                           \
1391b090f4f1SMatheus Ferst     }                                                               \
1392b090f4f1SMatheus Ferst                                                                     \
1393b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1394b090f4f1SMatheus Ferst }
1395b090f4f1SMatheus Ferst 
1396618574ddSMatheus Ferst #define XXGENPCV(NAME, SZ) \
1397618574ddSMatheus Ferst     XXGENPCV_BE_EXP(NAME, SZ)  \
1398618574ddSMatheus Ferst     XXGENPCV_BE_COMP(NAME, SZ) \
1399618574ddSMatheus Ferst     XXGENPCV_LE_EXP(NAME, SZ)  \
1400618574ddSMatheus Ferst     XXGENPCV_LE_COMP(NAME, SZ) \
1401618574ddSMatheus Ferst 
1402b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVBM, 1)
1403b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVHM, 2)
1404b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVWM, 4)
1405b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVDM, 8)
1406618574ddSMatheus Ferst 
1407618574ddSMatheus Ferst #undef XXGENPCV_BE_EXP
1408618574ddSMatheus Ferst #undef XXGENPCV_BE_COMP
1409618574ddSMatheus Ferst #undef XXGENPCV_LE_EXP
1410618574ddSMatheus Ferst #undef XXGENPCV_LE_COMP
1411b090f4f1SMatheus Ferst #undef XXGENPCV
1412b090f4f1SMatheus Ferst 
1413e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1414fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[(i)])
1415fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (i)
1416fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) != 0)
1417fcf5ef2aSThomas Huth #else
1418fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[15 - (i)])
1419fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (1 - i)
1420fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) == 0)
1421fcf5ef2aSThomas Huth #endif
14228f7d41e0SMatheus Ferst #define EXTRACT_BIT(avr, i, index) \
14238f7d41e0SMatheus Ferst         (extract64((avr)->VsrD(i), 63 - index, 1))
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth void helper_vbpermd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1426fcf5ef2aSThomas Huth {
1427fcf5ef2aSThomas Huth     int i, j;
1428fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
1429fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1430fcf5ef2aSThomas Huth         for (j = 0; j < 8; j++) {
1431fcf5ef2aSThomas Huth             int index = VBPERMQ_INDEX(b, (i * 8) + j);
1432fcf5ef2aSThomas Huth             if (index < 64 && EXTRACT_BIT(a, i, index)) {
1433fcf5ef2aSThomas Huth                 result.u64[VBPERMD_INDEX(i)] |= (0x80 >> j);
1434fcf5ef2aSThomas Huth             }
1435fcf5ef2aSThomas Huth         }
1436fcf5ef2aSThomas Huth     }
1437fcf5ef2aSThomas Huth     *r = result;
1438fcf5ef2aSThomas Huth }
1439fcf5ef2aSThomas Huth 
1440fcf5ef2aSThomas Huth void helper_vbpermq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1441fcf5ef2aSThomas Huth {
1442fcf5ef2aSThomas Huth     int i;
1443fcf5ef2aSThomas Huth     uint64_t perm = 0;
1444fcf5ef2aSThomas Huth 
1445fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1446fcf5ef2aSThomas Huth         int index = VBPERMQ_INDEX(b, i);
1447fcf5ef2aSThomas Huth 
1448fcf5ef2aSThomas Huth         if (index < 128) {
1449fcf5ef2aSThomas Huth             uint64_t mask = (1ull << (63 - (index & 0x3F)));
1450fcf5ef2aSThomas Huth             if (a->u64[VBPERMQ_DW(index)] & mask) {
1451fcf5ef2aSThomas Huth                 perm |= (0x8000 >> i);
1452fcf5ef2aSThomas Huth             }
1453fcf5ef2aSThomas Huth         }
1454fcf5ef2aSThomas Huth     }
1455fcf5ef2aSThomas Huth 
14563c385a93SMark Cave-Ayland     r->VsrD(0) = perm;
14573c385a93SMark Cave-Ayland     r->VsrD(1) = 0;
1458fcf5ef2aSThomas Huth }
1459fcf5ef2aSThomas Huth 
1460fcf5ef2aSThomas Huth #undef VBPERMQ_INDEX
1461fcf5ef2aSThomas Huth #undef VBPERMQ_DW
1462fcf5ef2aSThomas Huth 
1463fcf5ef2aSThomas Huth #define PMSUM(name, srcfld, trgfld, trgtyp)                   \
1464fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
1465fcf5ef2aSThomas Huth {                                                             \
1466fcf5ef2aSThomas Huth     int i, j;                                                 \
1467fcf5ef2aSThomas Huth     trgtyp prod[sizeof(ppc_avr_t) / sizeof(a->srcfld[0])];    \
1468fcf5ef2aSThomas Huth                                                               \
1469fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, srcfld) {                         \
1470fcf5ef2aSThomas Huth         prod[i] = 0;                                          \
1471fcf5ef2aSThomas Huth         for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) {      \
1472fcf5ef2aSThomas Huth             if (a->srcfld[i] & (1ull << j)) {                 \
1473fcf5ef2aSThomas Huth                 prod[i] ^= ((trgtyp)b->srcfld[i] << j);       \
1474fcf5ef2aSThomas Huth             }                                                 \
1475fcf5ef2aSThomas Huth         }                                                     \
1476fcf5ef2aSThomas Huth     }                                                         \
1477fcf5ef2aSThomas Huth                                                               \
1478fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, trgfld) {                         \
1479fcf5ef2aSThomas Huth         r->trgfld[i] = prod[2 * i] ^ prod[2 * i + 1];         \
1480fcf5ef2aSThomas Huth     }                                                         \
1481fcf5ef2aSThomas Huth }
1482fcf5ef2aSThomas Huth 
1483fcf5ef2aSThomas Huth PMSUM(vpmsumb, u8, u16, uint16_t)
1484fcf5ef2aSThomas Huth PMSUM(vpmsumh, u16, u32, uint32_t)
1485fcf5ef2aSThomas Huth PMSUM(vpmsumw, u32, u64, uint64_t)
1486fcf5ef2aSThomas Huth 
1487e82ca8acSMatheus Ferst void helper_VPMSUMD(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1488fcf5ef2aSThomas Huth {
1489fcf5ef2aSThomas Huth     int i, j;
1490e82ca8acSMatheus Ferst     Int128 tmp, prod[2] = {int128_zero(), int128_zero()};
1491fcf5ef2aSThomas Huth 
1492fcf5ef2aSThomas Huth     for (j = 0; j < 64; j++) {
1493e82ca8acSMatheus Ferst         for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
1494e82ca8acSMatheus Ferst             if (a->VsrD(i) & (1ull << j)) {
1495e82ca8acSMatheus Ferst                 tmp = int128_make64(b->VsrD(i));
1496e82ca8acSMatheus Ferst                 tmp = int128_lshift(tmp, j);
1497e82ca8acSMatheus Ferst                 prod[i] = int128_xor(prod[i], tmp);
1498fcf5ef2aSThomas Huth             }
1499fcf5ef2aSThomas Huth         }
1500fcf5ef2aSThomas Huth     }
1501fcf5ef2aSThomas Huth 
1502e82ca8acSMatheus Ferst     r->s128 = int128_xor(prod[0], prod[1]);
1503fcf5ef2aSThomas Huth }
1504fcf5ef2aSThomas Huth 
1505e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1506fcf5ef2aSThomas Huth #define PKBIG 1
1507fcf5ef2aSThomas Huth #else
1508fcf5ef2aSThomas Huth #define PKBIG 0
1509fcf5ef2aSThomas Huth #endif
1510fcf5ef2aSThomas Huth void helper_vpkpx(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1511fcf5ef2aSThomas Huth {
1512fcf5ef2aSThomas Huth     int i, j;
1513fcf5ef2aSThomas Huth     ppc_avr_t result;
1514e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1515fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { a, b };
1516fcf5ef2aSThomas Huth #else
1517fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { b, a };
1518fcf5ef2aSThomas Huth #endif
1519fcf5ef2aSThomas Huth 
1520fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1521fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(j, u32) {
1522fcf5ef2aSThomas Huth             uint32_t e = x[i]->u32[j];
1523fcf5ef2aSThomas Huth 
1524fcf5ef2aSThomas Huth             result.u16[4 * i + j] = (((e >> 9) & 0xfc00) |
1525fcf5ef2aSThomas Huth                                      ((e >> 6) & 0x3e0) |
1526fcf5ef2aSThomas Huth                                      ((e >> 3) & 0x1f));
1527fcf5ef2aSThomas Huth         }
1528fcf5ef2aSThomas Huth     }
1529fcf5ef2aSThomas Huth     *r = result;
1530fcf5ef2aSThomas Huth }
1531fcf5ef2aSThomas Huth 
1532fcf5ef2aSThomas Huth #define VPK(suffix, from, to, cvt, dosat)                               \
1533fcf5ef2aSThomas Huth     void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r,             \
1534fcf5ef2aSThomas Huth                             ppc_avr_t *a, ppc_avr_t *b)                 \
1535fcf5ef2aSThomas Huth     {                                                                   \
1536fcf5ef2aSThomas Huth         int i;                                                          \
1537fcf5ef2aSThomas Huth         int sat = 0;                                                    \
1538fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
1539fcf5ef2aSThomas Huth         ppc_avr_t *a0 = PKBIG ? a : b;                                  \
1540fcf5ef2aSThomas Huth         ppc_avr_t *a1 = PKBIG ? b : a;                                  \
1541fcf5ef2aSThomas Huth                                                                         \
1542fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(i, from) {                                 \
1543fcf5ef2aSThomas Huth             result.to[i] = cvt(a0->from[i], &sat);                      \
1544fcf5ef2aSThomas Huth             result.to[i + ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);\
1545fcf5ef2aSThomas Huth         }                                                               \
1546fcf5ef2aSThomas Huth         *r = result;                                                    \
1547fcf5ef2aSThomas Huth         if (dosat && sat) {                                             \
15486175f5a0SRichard Henderson             set_vscr_sat(env);                                          \
1549fcf5ef2aSThomas Huth         }                                                               \
1550fcf5ef2aSThomas Huth     }
1551fcf5ef2aSThomas Huth #define I(x, y) (x)
1552fcf5ef2aSThomas Huth VPK(shss, s16, s8, cvtshsb, 1)
1553fcf5ef2aSThomas Huth VPK(shus, s16, u8, cvtshub, 1)
1554fcf5ef2aSThomas Huth VPK(swss, s32, s16, cvtswsh, 1)
1555fcf5ef2aSThomas Huth VPK(swus, s32, u16, cvtswuh, 1)
1556fcf5ef2aSThomas Huth VPK(sdss, s64, s32, cvtsdsw, 1)
1557fcf5ef2aSThomas Huth VPK(sdus, s64, u32, cvtsduw, 1)
1558fcf5ef2aSThomas Huth VPK(uhus, u16, u8, cvtuhub, 1)
1559fcf5ef2aSThomas Huth VPK(uwus, u32, u16, cvtuwuh, 1)
1560fcf5ef2aSThomas Huth VPK(udus, u64, u32, cvtuduw, 1)
1561fcf5ef2aSThomas Huth VPK(uhum, u16, u8, I, 0)
1562fcf5ef2aSThomas Huth VPK(uwum, u32, u16, I, 0)
1563fcf5ef2aSThomas Huth VPK(udum, u64, u32, I, 0)
1564fcf5ef2aSThomas Huth #undef I
1565fcf5ef2aSThomas Huth #undef VPK
1566fcf5ef2aSThomas Huth #undef PKBIG
1567fcf5ef2aSThomas Huth 
1568fcf5ef2aSThomas Huth void helper_vrefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1569fcf5ef2aSThomas Huth {
1570fcf5ef2aSThomas Huth     int i;
1571fcf5ef2aSThomas Huth 
157205ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
157305ee3e8aSMark Cave-Ayland         r->f32[i] = float32_div(float32_one, b->f32[i], &env->vec_status);
1574fcf5ef2aSThomas Huth     }
1575fcf5ef2aSThomas Huth }
1576fcf5ef2aSThomas Huth 
1577fcf5ef2aSThomas Huth #define VRFI(suffix, rounding)                                  \
1578fcf5ef2aSThomas Huth     void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r,    \
1579fcf5ef2aSThomas Huth                              ppc_avr_t *b)                      \
1580fcf5ef2aSThomas Huth     {                                                           \
1581fcf5ef2aSThomas Huth         int i;                                                  \
1582fcf5ef2aSThomas Huth         float_status s = env->vec_status;                       \
1583fcf5ef2aSThomas Huth                                                                 \
1584fcf5ef2aSThomas Huth         set_float_rounding_mode(rounding, &s);                  \
158505ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {              \
158605ee3e8aSMark Cave-Ayland             r->f32[i] = float32_round_to_int (b->f32[i], &s);   \
1587fcf5ef2aSThomas Huth         }                                                       \
1588fcf5ef2aSThomas Huth     }
1589fcf5ef2aSThomas Huth VRFI(n, float_round_nearest_even)
1590fcf5ef2aSThomas Huth VRFI(m, float_round_down)
1591fcf5ef2aSThomas Huth VRFI(p, float_round_up)
1592fcf5ef2aSThomas Huth VRFI(z, float_round_to_zero)
1593fcf5ef2aSThomas Huth #undef VRFI
1594fcf5ef2aSThomas Huth 
1595fcf5ef2aSThomas Huth void helper_vrsqrtefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1596fcf5ef2aSThomas Huth {
1597fcf5ef2aSThomas Huth     int i;
1598fcf5ef2aSThomas Huth 
159905ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
160005ee3e8aSMark Cave-Ayland         float32 t = float32_sqrt(b->f32[i], &env->vec_status);
1601fcf5ef2aSThomas Huth 
160205ee3e8aSMark Cave-Ayland         r->f32[i] = float32_div(float32_one, t, &env->vec_status);
1603fcf5ef2aSThomas Huth     }
1604fcf5ef2aSThomas Huth }
1605fcf5ef2aSThomas Huth 
1606fcf5ef2aSThomas Huth #define VRLMI(name, size, element, insert)                                  \
160702c74f0eSMatheus Ferst void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t desc) \
1608fcf5ef2aSThomas Huth {                                                                           \
1609fcf5ef2aSThomas Huth     int i;                                                                  \
1610fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                          \
1611fcf5ef2aSThomas Huth         uint##size##_t src1 = a->element[i];                                \
1612fcf5ef2aSThomas Huth         uint##size##_t src2 = b->element[i];                                \
1613fcf5ef2aSThomas Huth         uint##size##_t src3 = r->element[i];                                \
1614fcf5ef2aSThomas Huth         uint##size##_t begin, end, shift, mask, rot_val;                    \
1615fcf5ef2aSThomas Huth                                                                             \
1616fcf5ef2aSThomas Huth         shift = extract##size(src2, 0, 6);                                  \
1617fcf5ef2aSThomas Huth         end   = extract##size(src2, 8, 6);                                  \
1618fcf5ef2aSThomas Huth         begin = extract##size(src2, 16, 6);                                 \
1619fcf5ef2aSThomas Huth         rot_val = rol##size(src1, shift);                                   \
1620fcf5ef2aSThomas Huth         mask = mask_u##size(begin, end);                                    \
1621fcf5ef2aSThomas Huth         if (insert) {                                                       \
1622fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask) | (src3 & ~mask);              \
1623fcf5ef2aSThomas Huth         } else {                                                            \
1624fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask);                               \
1625fcf5ef2aSThomas Huth         }                                                                   \
1626fcf5ef2aSThomas Huth     }                                                                       \
1627fcf5ef2aSThomas Huth }
1628fcf5ef2aSThomas Huth 
162902c74f0eSMatheus Ferst VRLMI(VRLDMI, 64, u64, 1);
163002c74f0eSMatheus Ferst VRLMI(VRLWMI, 32, u32, 1);
163102c74f0eSMatheus Ferst VRLMI(VRLDNM, 64, u64, 0);
163202c74f0eSMatheus Ferst VRLMI(VRLWNM, 32, u32, 0);
1633fcf5ef2aSThomas Huth 
1634fcf5ef2aSThomas Huth void helper_vexptefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1635fcf5ef2aSThomas Huth {
1636fcf5ef2aSThomas Huth     int i;
1637fcf5ef2aSThomas Huth 
163805ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
163905ee3e8aSMark Cave-Ayland         r->f32[i] = float32_exp2(b->f32[i], &env->vec_status);
1640fcf5ef2aSThomas Huth     }
1641fcf5ef2aSThomas Huth }
1642fcf5ef2aSThomas Huth 
1643fcf5ef2aSThomas Huth void helper_vlogefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1644fcf5ef2aSThomas Huth {
1645fcf5ef2aSThomas Huth     int i;
1646fcf5ef2aSThomas Huth 
164705ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
164805ee3e8aSMark Cave-Ayland         r->f32[i] = float32_log2(b->f32[i], &env->vec_status);
1649fcf5ef2aSThomas Huth     }
1650fcf5ef2aSThomas Huth }
1651fcf5ef2aSThomas Huth 
165260caf221SAvinesh Kumar #define VEXTU_X_DO(name, size, left)                            \
165360caf221SAvinesh Kumar target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
165460caf221SAvinesh Kumar {                                                               \
1655f297c4c6SMatheus Ferst     int index = (a & 0xf) * 8;                                  \
165660caf221SAvinesh Kumar     if (left) {                                                 \
1657f297c4c6SMatheus Ferst         index = 128 - index - size;                             \
165860caf221SAvinesh Kumar     }                                                           \
165960caf221SAvinesh Kumar     return int128_getlo(int128_rshift(b->s128, index)) &        \
166060caf221SAvinesh Kumar         MAKE_64BIT_MASK(0, size);                               \
166160caf221SAvinesh Kumar }
166260caf221SAvinesh Kumar VEXTU_X_DO(vextublx,  8, 1)
166360caf221SAvinesh Kumar VEXTU_X_DO(vextuhlx, 16, 1)
166460caf221SAvinesh Kumar VEXTU_X_DO(vextuwlx, 32, 1)
166560caf221SAvinesh Kumar VEXTU_X_DO(vextubrx,  8, 0)
166660caf221SAvinesh Kumar VEXTU_X_DO(vextuhrx, 16, 0)
166760caf221SAvinesh Kumar VEXTU_X_DO(vextuwrx, 32, 0)
166860caf221SAvinesh Kumar #undef VEXTU_X_DO
166960caf221SAvinesh Kumar 
1670fcf5ef2aSThomas Huth void helper_vslv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1671fcf5ef2aSThomas Huth {
1672fcf5ef2aSThomas Huth     int i;
1673fcf5ef2aSThomas Huth     unsigned int shift, bytes, size;
1674fcf5ef2aSThomas Huth 
1675fcf5ef2aSThomas Huth     size = ARRAY_SIZE(r->u8);
1676fcf5ef2aSThomas Huth     for (i = 0; i < size; i++) {
167763be02fcSAnton Blanchard         shift = b->VsrB(i) & 0x7;             /* extract shift value */
167863be02fcSAnton Blanchard         bytes = (a->VsrB(i) << 8) +           /* extract adjacent bytes */
167963be02fcSAnton Blanchard             (((i + 1) < size) ? a->VsrB(i + 1) : 0);
168063be02fcSAnton Blanchard         r->VsrB(i) = (bytes << shift) >> 8;   /* shift and store result */
1681fcf5ef2aSThomas Huth     }
1682fcf5ef2aSThomas Huth }
1683fcf5ef2aSThomas Huth 
1684fcf5ef2aSThomas Huth void helper_vsrv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1685fcf5ef2aSThomas Huth {
1686fcf5ef2aSThomas Huth     int i;
1687fcf5ef2aSThomas Huth     unsigned int shift, bytes;
1688fcf5ef2aSThomas Huth 
1689b6cb41b2SDavid Gibson     /*
1690b6cb41b2SDavid Gibson      * Use reverse order, as destination and source register can be
1691b6cb41b2SDavid Gibson      * same. Its being modified in place saving temporary, reverse
1692b6cb41b2SDavid Gibson      * order will guarantee that computed result is not fed back.
1693fcf5ef2aSThomas Huth      */
1694fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
169563be02fcSAnton Blanchard         shift = b->VsrB(i) & 0x7;               /* extract shift value */
169663be02fcSAnton Blanchard         bytes = ((i ? a->VsrB(i - 1) : 0) << 8) + a->VsrB(i);
1697fcf5ef2aSThomas Huth                                                 /* extract adjacent bytes */
169863be02fcSAnton Blanchard         r->VsrB(i) = (bytes >> shift) & 0xFF;   /* shift and store result */
1699fcf5ef2aSThomas Huth     }
1700fcf5ef2aSThomas Huth }
1701fcf5ef2aSThomas Huth 
1702fcf5ef2aSThomas Huth void helper_vsldoi(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t shift)
1703fcf5ef2aSThomas Huth {
1704fcf5ef2aSThomas Huth     int sh = shift & 0xf;
1705fcf5ef2aSThomas Huth     int i;
1706fcf5ef2aSThomas Huth     ppc_avr_t result;
1707fcf5ef2aSThomas Huth 
1708fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1709fcf5ef2aSThomas Huth         int index = sh + i;
1710fcf5ef2aSThomas Huth         if (index > 0xf) {
171160594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index - 0x10);
1712fcf5ef2aSThomas Huth         } else {
171360594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1714fcf5ef2aSThomas Huth         }
1715fcf5ef2aSThomas Huth     }
1716fcf5ef2aSThomas Huth     *r = result;
1717fcf5ef2aSThomas Huth }
1718fcf5ef2aSThomas Huth 
1719fcf5ef2aSThomas Huth void helper_vslo(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1720fcf5ef2aSThomas Huth {
17213c385a93SMark Cave-Ayland     int sh = (b->VsrB(0xf) >> 3) & 0xf;
1722fcf5ef2aSThomas Huth 
1723e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1724fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1725fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
1726fcf5ef2aSThomas Huth #else
1727fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1728fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1729fcf5ef2aSThomas Huth #endif
1730fcf5ef2aSThomas Huth }
1731fcf5ef2aSThomas Huth 
1732e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
17332cc12af3SMatheus Ferst #define ELEM_ADDR(VEC, IDX, SIZE) (&(VEC)->u8[IDX])
17342cc12af3SMatheus Ferst #else
17352cc12af3SMatheus Ferst #define ELEM_ADDR(VEC, IDX, SIZE) (&(VEC)->u8[15 - (IDX)] - (SIZE) + 1)
17362cc12af3SMatheus Ferst #endif
17372cc12af3SMatheus Ferst 
17382cc12af3SMatheus Ferst #define VINSX(SUFFIX, TYPE) \
17392cc12af3SMatheus Ferst void glue(glue(helper_VINS, SUFFIX), LX)(CPUPPCState *env, ppc_avr_t *t,       \
17402cc12af3SMatheus Ferst                                          uint64_t val, target_ulong index)     \
17412cc12af3SMatheus Ferst {                                                                              \
17422cc12af3SMatheus Ferst     const int maxidx = ARRAY_SIZE(t->u8) - sizeof(TYPE);                       \
17432cc12af3SMatheus Ferst     target_long idx = index;                                                   \
17442cc12af3SMatheus Ferst                                                                                \
17452cc12af3SMatheus Ferst     if (idx < 0 || idx > maxidx) {                                             \
17462cc12af3SMatheus Ferst         idx =  idx < 0 ? sizeof(TYPE) - idx : idx;                             \
17472cc12af3SMatheus Ferst         qemu_log_mask(LOG_GUEST_ERROR,                                         \
17482cc12af3SMatheus Ferst             "Invalid index for Vector Insert Element after 0x" TARGET_FMT_lx   \
17492cc12af3SMatheus Ferst             ", RA = " TARGET_FMT_ld " > %d\n", env->nip, idx, maxidx);         \
17502cc12af3SMatheus Ferst     } else {                                                                   \
17512cc12af3SMatheus Ferst         TYPE src = val;                                                        \
17522cc12af3SMatheus Ferst         memcpy(ELEM_ADDR(t, idx, sizeof(TYPE)), &src, sizeof(TYPE));           \
17532cc12af3SMatheus Ferst     }                                                                          \
17542cc12af3SMatheus Ferst }
17552cc12af3SMatheus Ferst VINSX(B, uint8_t)
17562cc12af3SMatheus Ferst VINSX(H, uint16_t)
17572cc12af3SMatheus Ferst VINSX(W, uint32_t)
17582cc12af3SMatheus Ferst VINSX(D, uint64_t)
17592cc12af3SMatheus Ferst #undef ELEM_ADDR
17602cc12af3SMatheus Ferst #undef VINSX
1761e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
176228110b72SMatheus Ferst #define VEXTDVLX(NAME, SIZE) \
176328110b72SMatheus Ferst void helper_##NAME(CPUPPCState *env, ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, \
176428110b72SMatheus Ferst                    target_ulong index)                                         \
176528110b72SMatheus Ferst {                                                                              \
176628110b72SMatheus Ferst     const target_long idx = index;                                             \
176728110b72SMatheus Ferst     ppc_avr_t tmp[2] = { *a, *b };                                             \
176828110b72SMatheus Ferst     memset(t, 0, sizeof(*t));                                                  \
176928110b72SMatheus Ferst     if (idx >= 0 && idx + SIZE <= sizeof(tmp)) {                               \
177028110b72SMatheus Ferst         memcpy(&t->u8[ARRAY_SIZE(t->u8) / 2 - SIZE], (void *)tmp + idx, SIZE); \
177128110b72SMatheus Ferst     } else {                                                                   \
177228110b72SMatheus Ferst         qemu_log_mask(LOG_GUEST_ERROR, "Invalid index for " #NAME " after 0x"  \
177328110b72SMatheus Ferst                       TARGET_FMT_lx ", RC = " TARGET_FMT_ld " > %d\n",         \
177428110b72SMatheus Ferst                       env->nip, idx < 0 ? SIZE - idx : idx, 32 - SIZE);        \
177528110b72SMatheus Ferst     }                                                                          \
177628110b72SMatheus Ferst }
177728110b72SMatheus Ferst #else
177828110b72SMatheus Ferst #define VEXTDVLX(NAME, SIZE) \
177928110b72SMatheus Ferst void helper_##NAME(CPUPPCState *env, ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, \
178028110b72SMatheus Ferst                    target_ulong index)                                         \
178128110b72SMatheus Ferst {                                                                              \
178228110b72SMatheus Ferst     const target_long idx = index;                                             \
178328110b72SMatheus Ferst     ppc_avr_t tmp[2] = { *b, *a };                                             \
178428110b72SMatheus Ferst     memset(t, 0, sizeof(*t));                                                  \
178528110b72SMatheus Ferst     if (idx >= 0 && idx + SIZE <= sizeof(tmp)) {                               \
178628110b72SMatheus Ferst         memcpy(&t->u8[ARRAY_SIZE(t->u8) / 2],                                  \
178728110b72SMatheus Ferst                (void *)tmp + sizeof(tmp) - SIZE - idx, SIZE);                  \
178828110b72SMatheus Ferst     } else {                                                                   \
178928110b72SMatheus Ferst         qemu_log_mask(LOG_GUEST_ERROR, "Invalid index for " #NAME " after 0x"  \
179028110b72SMatheus Ferst                       TARGET_FMT_lx ", RC = " TARGET_FMT_ld " > %d\n",         \
179128110b72SMatheus Ferst                       env->nip, idx < 0 ? SIZE - idx : idx, 32 - SIZE);        \
179228110b72SMatheus Ferst     }                                                                          \
179328110b72SMatheus Ferst }
179428110b72SMatheus Ferst #endif
179528110b72SMatheus Ferst VEXTDVLX(VEXTDUBVLX, 1)
179628110b72SMatheus Ferst VEXTDVLX(VEXTDUHVLX, 2)
179728110b72SMatheus Ferst VEXTDVLX(VEXTDUWVLX, 4)
179828110b72SMatheus Ferst VEXTDVLX(VEXTDDVLX, 8)
179928110b72SMatheus Ferst #undef VEXTDVLX
1800e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1801fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1802fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1803fcf5ef2aSThomas Huth     {                                                                        \
1804fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1805fcf5ef2aSThomas Huth         memmove(&r->u8[8 - es], &b->u8[index], es);                          \
1806fcf5ef2aSThomas Huth         memset(&r->u8[8], 0, 8);                                             \
1807fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8 - es);                                        \
1808fcf5ef2aSThomas Huth     }
1809fcf5ef2aSThomas Huth #else
1810fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1811fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1812fcf5ef2aSThomas Huth     {                                                                        \
1813fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1814fcf5ef2aSThomas Huth         uint32_t s = (16 - index) - es;                                      \
1815fcf5ef2aSThomas Huth         memmove(&r->u8[8], &b->u8[s], es);                                   \
1816fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8);                                             \
1817fcf5ef2aSThomas Huth         memset(&r->u8[8 + es], 0, 8 - es);                                   \
1818fcf5ef2aSThomas Huth     }
1819fcf5ef2aSThomas Huth #endif
1820fcf5ef2aSThomas Huth VEXTRACT(ub, u8)
1821fcf5ef2aSThomas Huth VEXTRACT(uh, u16)
1822fcf5ef2aSThomas Huth VEXTRACT(uw, u32)
1823fcf5ef2aSThomas Huth VEXTRACT(d, u64)
1824fcf5ef2aSThomas Huth #undef VEXTRACT
1825fcf5ef2aSThomas Huth 
1826fb5303ccSMatheus Ferst #define VSTRI(NAME, ELEM, NUM_ELEMS, LEFT) \
1827fb5303ccSMatheus Ferst uint32_t helper_##NAME(ppc_avr_t *t, ppc_avr_t *b) \
1828fb5303ccSMatheus Ferst {                                                   \
1829fb5303ccSMatheus Ferst     int i, idx, crf = 0;                            \
1830fb5303ccSMatheus Ferst                                                     \
1831fb5303ccSMatheus Ferst     for (i = 0; i < NUM_ELEMS; i++) {               \
1832fb5303ccSMatheus Ferst         idx = LEFT ? i : NUM_ELEMS - i - 1;         \
1833fb5303ccSMatheus Ferst         if (b->Vsr##ELEM(idx)) {                    \
1834fb5303ccSMatheus Ferst             t->Vsr##ELEM(idx) = b->Vsr##ELEM(idx);  \
1835fb5303ccSMatheus Ferst         } else {                                    \
1836fb5303ccSMatheus Ferst             crf = 0b0010;                           \
1837fb5303ccSMatheus Ferst             break;                                  \
1838fb5303ccSMatheus Ferst         }                                           \
1839fb5303ccSMatheus Ferst     }                                               \
1840fb5303ccSMatheus Ferst                                                     \
1841fb5303ccSMatheus Ferst     for (; i < NUM_ELEMS; i++) {                    \
1842fb5303ccSMatheus Ferst         idx = LEFT ? i : NUM_ELEMS - i - 1;         \
1843fb5303ccSMatheus Ferst         t->Vsr##ELEM(idx) = 0;                      \
1844fb5303ccSMatheus Ferst     }                                               \
1845fb5303ccSMatheus Ferst                                                     \
1846fb5303ccSMatheus Ferst     return crf;                                     \
1847fb5303ccSMatheus Ferst }
1848fb5303ccSMatheus Ferst VSTRI(VSTRIBL, B, 16, true)
1849fb5303ccSMatheus Ferst VSTRI(VSTRIBR, B, 16, false)
1850fb5303ccSMatheus Ferst VSTRI(VSTRIHL, H, 8, true)
1851fb5303ccSMatheus Ferst VSTRI(VSTRIHR, H, 8, false)
1852fb5303ccSMatheus Ferst #undef VSTRI
1853fb5303ccSMatheus Ferst 
18548f5eeee3SMatheus Ferst void helper_XXEXTRACTUW(ppc_vsr_t *xt, ppc_vsr_t *xb, uint32_t index)
18558ad901e5SNikunj A Dadhania {
185603b32c09SMark Cave-Ayland     ppc_vsr_t t = { };
18578ad901e5SNikunj A Dadhania     size_t es = sizeof(uint32_t);
18588ad901e5SNikunj A Dadhania     uint32_t ext_index;
18598ad901e5SNikunj A Dadhania     int i;
18608ad901e5SNikunj A Dadhania 
18618ad901e5SNikunj A Dadhania     ext_index = index;
18628ad901e5SNikunj A Dadhania     for (i = 0; i < es; i++, ext_index++) {
186303b32c09SMark Cave-Ayland         t.VsrB(8 - es + i) = xb->VsrB(ext_index % 16);
18648ad901e5SNikunj A Dadhania     }
18658ad901e5SNikunj A Dadhania 
186603b32c09SMark Cave-Ayland     *xt = t;
18678ad901e5SNikunj A Dadhania }
18688ad901e5SNikunj A Dadhania 
18698f5eeee3SMatheus Ferst void helper_XXINSERTW(ppc_vsr_t *xt, ppc_vsr_t *xb, uint32_t index)
18703398b742SNikunj A Dadhania {
187103b32c09SMark Cave-Ayland     ppc_vsr_t t = *xt;
18723398b742SNikunj A Dadhania     size_t es = sizeof(uint32_t);
18733398b742SNikunj A Dadhania     int ins_index, i = 0;
18743398b742SNikunj A Dadhania 
18753398b742SNikunj A Dadhania     ins_index = index;
18763398b742SNikunj A Dadhania     for (i = 0; i < es && ins_index < 16; i++, ins_index++) {
187703b32c09SMark Cave-Ayland         t.VsrB(ins_index) = xb->VsrB(8 - es + i);
18783398b742SNikunj A Dadhania     }
18793398b742SNikunj A Dadhania 
188003b32c09SMark Cave-Ayland     *xt = t;
18813398b742SNikunj A Dadhania }
18823398b742SNikunj A Dadhania 
18831015fcabSMatheus Ferst void helper_XXEVAL(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c,
18841015fcabSMatheus Ferst                    uint32_t desc)
18851015fcabSMatheus Ferst {
18861015fcabSMatheus Ferst     /*
18871015fcabSMatheus Ferst      * Instead of processing imm bit-by-bit, we'll skip the computation of
18881015fcabSMatheus Ferst      * conjunctions whose corresponding bit is unset.
18891015fcabSMatheus Ferst      */
18901015fcabSMatheus Ferst     int bit, imm = simd_data(desc);
18911015fcabSMatheus Ferst     Int128 conj, disj = int128_zero();
18921015fcabSMatheus Ferst 
18931015fcabSMatheus Ferst     /* Iterate over set bits from the least to the most significant bit */
18941015fcabSMatheus Ferst     while (imm) {
18951015fcabSMatheus Ferst         /*
18961015fcabSMatheus Ferst          * Get the next bit to be processed with ctz64. Invert the result of
18971015fcabSMatheus Ferst          * ctz64 to match the indexing used by PowerISA.
18981015fcabSMatheus Ferst          */
18991015fcabSMatheus Ferst         bit = 7 - ctzl(imm);
19001015fcabSMatheus Ferst         if (bit & 0x4) {
19011015fcabSMatheus Ferst             conj = a->s128;
19021015fcabSMatheus Ferst         } else {
19031015fcabSMatheus Ferst             conj = int128_not(a->s128);
19041015fcabSMatheus Ferst         }
19051015fcabSMatheus Ferst         if (bit & 0x2) {
19061015fcabSMatheus Ferst             conj = int128_and(conj, b->s128);
19071015fcabSMatheus Ferst         } else {
19081015fcabSMatheus Ferst             conj = int128_and(conj, int128_not(b->s128));
19091015fcabSMatheus Ferst         }
19101015fcabSMatheus Ferst         if (bit & 0x1) {
19111015fcabSMatheus Ferst             conj = int128_and(conj, c->s128);
19121015fcabSMatheus Ferst         } else {
19131015fcabSMatheus Ferst             conj = int128_and(conj, int128_not(c->s128));
19141015fcabSMatheus Ferst         }
19151015fcabSMatheus Ferst         disj = int128_or(disj, conj);
19161015fcabSMatheus Ferst 
19171015fcabSMatheus Ferst         /* Unset the least significant bit that is set */
19181015fcabSMatheus Ferst         imm &= imm - 1;
19191015fcabSMatheus Ferst     }
19201015fcabSMatheus Ferst 
19211015fcabSMatheus Ferst     t->s128 = disj;
19221015fcabSMatheus Ferst }
19231015fcabSMatheus Ferst 
1924788c6399SMatheus Ferst #define XXBLEND(name, sz) \
1925788c6399SMatheus Ferst void glue(helper_XXBLENDV, name)(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b,  \
1926788c6399SMatheus Ferst                                  ppc_avr_t *c, uint32_t desc)               \
1927788c6399SMatheus Ferst {                                                                           \
1928788c6399SMatheus Ferst     for (int i = 0; i < ARRAY_SIZE(t->glue(u, sz)); i++) {                  \
1929788c6399SMatheus Ferst         t->glue(u, sz)[i] = (c->glue(s, sz)[i] >> (sz - 1)) ?               \
1930788c6399SMatheus Ferst             b->glue(u, sz)[i] : a->glue(u, sz)[i];                          \
1931788c6399SMatheus Ferst     }                                                                       \
1932788c6399SMatheus Ferst }
1933788c6399SMatheus Ferst XXBLEND(B, 8)
1934788c6399SMatheus Ferst XXBLEND(H, 16)
1935788c6399SMatheus Ferst XXBLEND(W, 32)
1936788c6399SMatheus Ferst XXBLEND(D, 64)
1937788c6399SMatheus Ferst #undef XXBLEND
1938788c6399SMatheus Ferst 
1939fcf5ef2aSThomas Huth #define VNEG(name, element)                                         \
1940fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *b)                      \
1941fcf5ef2aSThomas Huth {                                                                   \
1942fcf5ef2aSThomas Huth     int i;                                                          \
194360594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1944fcf5ef2aSThomas Huth         r->element[i] = -b->element[i];                             \
1945fcf5ef2aSThomas Huth     }                                                               \
1946fcf5ef2aSThomas Huth }
1947fcf5ef2aSThomas Huth VNEG(vnegw, s32)
1948fcf5ef2aSThomas Huth VNEG(vnegd, s64)
1949fcf5ef2aSThomas Huth #undef VNEG
1950fcf5ef2aSThomas Huth 
1951fcf5ef2aSThomas Huth void helper_vsro(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1952fcf5ef2aSThomas Huth {
19533c385a93SMark Cave-Ayland     int sh = (b->VsrB(0xf) >> 3) & 0xf;
1954fcf5ef2aSThomas Huth 
1955e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1956fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1957fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1958fcf5ef2aSThomas Huth #else
1959fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1960fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
1961fcf5ef2aSThomas Huth #endif
1962fcf5ef2aSThomas Huth }
1963fcf5ef2aSThomas Huth 
1964fcf5ef2aSThomas Huth void helper_vsubcuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1965fcf5ef2aSThomas Huth {
1966fcf5ef2aSThomas Huth     int i;
1967fcf5ef2aSThomas Huth 
1968fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
1969fcf5ef2aSThomas Huth         r->u32[i] = a->u32[i] >= b->u32[i];
1970fcf5ef2aSThomas Huth     }
1971fcf5ef2aSThomas Huth }
1972fcf5ef2aSThomas Huth 
1973fcf5ef2aSThomas Huth void helper_vsumsws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1974fcf5ef2aSThomas Huth {
1975fcf5ef2aSThomas Huth     int64_t t;
1976fcf5ef2aSThomas Huth     int i, upper;
1977fcf5ef2aSThomas Huth     ppc_avr_t result;
1978fcf5ef2aSThomas Huth     int sat = 0;
1979fcf5ef2aSThomas Huth 
1980fcf5ef2aSThomas Huth     upper = ARRAY_SIZE(r->s32) - 1;
198160594feaSMark Cave-Ayland     t = (int64_t)b->VsrSW(upper);
1982fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
198360594feaSMark Cave-Ayland         t += a->VsrSW(i);
198460594feaSMark Cave-Ayland         result.VsrSW(i) = 0;
1985fcf5ef2aSThomas Huth     }
198660594feaSMark Cave-Ayland     result.VsrSW(upper) = cvtsdsw(t, &sat);
1987fcf5ef2aSThomas Huth     *r = result;
1988fcf5ef2aSThomas Huth 
1989fcf5ef2aSThomas Huth     if (sat) {
19906175f5a0SRichard Henderson         set_vscr_sat(env);
1991fcf5ef2aSThomas Huth     }
1992fcf5ef2aSThomas Huth }
1993fcf5ef2aSThomas Huth 
1994fcf5ef2aSThomas Huth void helper_vsum2sws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1995fcf5ef2aSThomas Huth {
1996fcf5ef2aSThomas Huth     int i, j, upper;
1997fcf5ef2aSThomas Huth     ppc_avr_t result;
1998fcf5ef2aSThomas Huth     int sat = 0;
1999fcf5ef2aSThomas Huth 
2000fcf5ef2aSThomas Huth     upper = 1;
2001fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
200260594feaSMark Cave-Ayland         int64_t t = (int64_t)b->VsrSW(upper + i * 2);
2003fcf5ef2aSThomas Huth 
20047fa0ddc1SAnton Blanchard         result.VsrD(i) = 0;
2005fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u64); j++) {
200660594feaSMark Cave-Ayland             t += a->VsrSW(2 * i + j);
2007fcf5ef2aSThomas Huth         }
200860594feaSMark Cave-Ayland         result.VsrSW(upper + i * 2) = cvtsdsw(t, &sat);
2009fcf5ef2aSThomas Huth     }
2010fcf5ef2aSThomas Huth 
2011fcf5ef2aSThomas Huth     *r = result;
2012fcf5ef2aSThomas Huth     if (sat) {
20136175f5a0SRichard Henderson         set_vscr_sat(env);
2014fcf5ef2aSThomas Huth     }
2015fcf5ef2aSThomas Huth }
2016fcf5ef2aSThomas Huth 
2017fcf5ef2aSThomas Huth void helper_vsum4sbs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2018fcf5ef2aSThomas Huth {
2019fcf5ef2aSThomas Huth     int i, j;
2020fcf5ef2aSThomas Huth     int sat = 0;
2021fcf5ef2aSThomas Huth 
2022fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2023fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
2024fcf5ef2aSThomas Huth 
2025fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->s32); j++) {
2026fcf5ef2aSThomas Huth             t += a->s8[4 * i + j];
2027fcf5ef2aSThomas Huth         }
2028fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
2029fcf5ef2aSThomas Huth     }
2030fcf5ef2aSThomas Huth 
2031fcf5ef2aSThomas Huth     if (sat) {
20326175f5a0SRichard Henderson         set_vscr_sat(env);
2033fcf5ef2aSThomas Huth     }
2034fcf5ef2aSThomas Huth }
2035fcf5ef2aSThomas Huth 
2036fcf5ef2aSThomas Huth void helper_vsum4shs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2037fcf5ef2aSThomas Huth {
2038fcf5ef2aSThomas Huth     int sat = 0;
2039fcf5ef2aSThomas Huth     int i;
2040fcf5ef2aSThomas Huth 
2041fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2042fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
2043fcf5ef2aSThomas Huth 
2044fcf5ef2aSThomas Huth         t += a->s16[2 * i] + a->s16[2 * i + 1];
2045fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
2046fcf5ef2aSThomas Huth     }
2047fcf5ef2aSThomas Huth 
2048fcf5ef2aSThomas Huth     if (sat) {
20496175f5a0SRichard Henderson         set_vscr_sat(env);
2050fcf5ef2aSThomas Huth     }
2051fcf5ef2aSThomas Huth }
2052fcf5ef2aSThomas Huth 
2053fcf5ef2aSThomas Huth void helper_vsum4ubs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2054fcf5ef2aSThomas Huth {
2055fcf5ef2aSThomas Huth     int i, j;
2056fcf5ef2aSThomas Huth     int sat = 0;
2057fcf5ef2aSThomas Huth 
2058fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2059fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)b->u32[i];
2060fcf5ef2aSThomas Huth 
2061fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u32); j++) {
2062fcf5ef2aSThomas Huth             t += a->u8[4 * i + j];
2063fcf5ef2aSThomas Huth         }
2064fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
2065fcf5ef2aSThomas Huth     }
2066fcf5ef2aSThomas Huth 
2067fcf5ef2aSThomas Huth     if (sat) {
20686175f5a0SRichard Henderson         set_vscr_sat(env);
2069fcf5ef2aSThomas Huth     }
2070fcf5ef2aSThomas Huth }
2071fcf5ef2aSThomas Huth 
2072e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
2073fcf5ef2aSThomas Huth #define UPKHI 1
2074fcf5ef2aSThomas Huth #define UPKLO 0
2075fcf5ef2aSThomas Huth #else
2076fcf5ef2aSThomas Huth #define UPKHI 0
2077fcf5ef2aSThomas Huth #define UPKLO 1
2078fcf5ef2aSThomas Huth #endif
2079fcf5ef2aSThomas Huth #define VUPKPX(suffix, hi)                                              \
2080fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
2081fcf5ef2aSThomas Huth     {                                                                   \
2082fcf5ef2aSThomas Huth         int i;                                                          \
2083fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
2084fcf5ef2aSThomas Huth                                                                         \
2085fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->u32); i++) {                      \
2086fcf5ef2aSThomas Huth             uint16_t e = b->u16[hi ? i : i + 4];                        \
2087fcf5ef2aSThomas Huth             uint8_t a = (e >> 15) ? 0xff : 0;                           \
2088fcf5ef2aSThomas Huth             uint8_t r = (e >> 10) & 0x1f;                               \
2089fcf5ef2aSThomas Huth             uint8_t g = (e >> 5) & 0x1f;                                \
2090fcf5ef2aSThomas Huth             uint8_t b = e & 0x1f;                                       \
2091fcf5ef2aSThomas Huth                                                                         \
2092fcf5ef2aSThomas Huth             result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b;       \
2093fcf5ef2aSThomas Huth         }                                                               \
2094fcf5ef2aSThomas Huth         *r = result;                                                    \
2095fcf5ef2aSThomas Huth     }
2096fcf5ef2aSThomas Huth VUPKPX(lpx, UPKLO)
2097fcf5ef2aSThomas Huth VUPKPX(hpx, UPKHI)
2098fcf5ef2aSThomas Huth #undef VUPKPX
2099fcf5ef2aSThomas Huth 
2100fcf5ef2aSThomas Huth #define VUPK(suffix, unpacked, packee, hi)                              \
2101fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
2102fcf5ef2aSThomas Huth     {                                                                   \
2103fcf5ef2aSThomas Huth         int i;                                                          \
2104fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
2105fcf5ef2aSThomas Huth                                                                         \
2106fcf5ef2aSThomas Huth         if (hi) {                                                       \
2107fcf5ef2aSThomas Huth             for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) {             \
2108fcf5ef2aSThomas Huth                 result.unpacked[i] = b->packee[i];                      \
2109fcf5ef2aSThomas Huth             }                                                           \
2110fcf5ef2aSThomas Huth         } else {                                                        \
2111fcf5ef2aSThomas Huth             for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
2112fcf5ef2aSThomas Huth                  i++) {                                                 \
2113fcf5ef2aSThomas Huth                 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
2114fcf5ef2aSThomas Huth             }                                                           \
2115fcf5ef2aSThomas Huth         }                                                               \
2116fcf5ef2aSThomas Huth         *r = result;                                                    \
2117fcf5ef2aSThomas Huth     }
2118fcf5ef2aSThomas Huth VUPK(hsb, s16, s8, UPKHI)
2119fcf5ef2aSThomas Huth VUPK(hsh, s32, s16, UPKHI)
2120fcf5ef2aSThomas Huth VUPK(hsw, s64, s32, UPKHI)
2121fcf5ef2aSThomas Huth VUPK(lsb, s16, s8, UPKLO)
2122fcf5ef2aSThomas Huth VUPK(lsh, s32, s16, UPKLO)
2123fcf5ef2aSThomas Huth VUPK(lsw, s64, s32, UPKLO)
2124fcf5ef2aSThomas Huth #undef VUPK
2125fcf5ef2aSThomas Huth #undef UPKHI
2126fcf5ef2aSThomas Huth #undef UPKLO
2127fcf5ef2aSThomas Huth 
2128fcf5ef2aSThomas Huth #define VGENERIC_DO(name, element)                                      \
2129fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *b)                     \
2130fcf5ef2aSThomas Huth     {                                                                   \
2131fcf5ef2aSThomas Huth         int i;                                                          \
2132fcf5ef2aSThomas Huth                                                                         \
213360594feaSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2134fcf5ef2aSThomas Huth             r->element[i] = name(b->element[i]);                        \
2135fcf5ef2aSThomas Huth         }                                                               \
2136fcf5ef2aSThomas Huth     }
2137fcf5ef2aSThomas Huth 
2138fcf5ef2aSThomas Huth #define clzb(v) ((v) ? clz32((uint32_t)(v) << 24) : 8)
2139fcf5ef2aSThomas Huth #define clzh(v) ((v) ? clz32((uint32_t)(v) << 16) : 16)
2140fcf5ef2aSThomas Huth 
2141fcf5ef2aSThomas Huth VGENERIC_DO(clzb, u8)
2142fcf5ef2aSThomas Huth VGENERIC_DO(clzh, u16)
2143fcf5ef2aSThomas Huth 
2144fcf5ef2aSThomas Huth #undef clzb
2145fcf5ef2aSThomas Huth #undef clzh
2146fcf5ef2aSThomas Huth 
2147fcf5ef2aSThomas Huth #define ctzb(v) ((v) ? ctz32(v) : 8)
2148fcf5ef2aSThomas Huth #define ctzh(v) ((v) ? ctz32(v) : 16)
2149fcf5ef2aSThomas Huth #define ctzw(v) ctz32((v))
2150fcf5ef2aSThomas Huth #define ctzd(v) ctz64((v))
2151fcf5ef2aSThomas Huth 
2152fcf5ef2aSThomas Huth VGENERIC_DO(ctzb, u8)
2153fcf5ef2aSThomas Huth VGENERIC_DO(ctzh, u16)
2154fcf5ef2aSThomas Huth VGENERIC_DO(ctzw, u32)
2155fcf5ef2aSThomas Huth VGENERIC_DO(ctzd, u64)
2156fcf5ef2aSThomas Huth 
2157fcf5ef2aSThomas Huth #undef ctzb
2158fcf5ef2aSThomas Huth #undef ctzh
2159fcf5ef2aSThomas Huth #undef ctzw
2160fcf5ef2aSThomas Huth #undef ctzd
2161fcf5ef2aSThomas Huth 
2162fcf5ef2aSThomas Huth #define popcntb(v) ctpop8(v)
2163fcf5ef2aSThomas Huth #define popcnth(v) ctpop16(v)
2164fcf5ef2aSThomas Huth #define popcntw(v) ctpop32(v)
2165fcf5ef2aSThomas Huth #define popcntd(v) ctpop64(v)
2166fcf5ef2aSThomas Huth 
2167fcf5ef2aSThomas Huth VGENERIC_DO(popcntb, u8)
2168fcf5ef2aSThomas Huth VGENERIC_DO(popcnth, u16)
2169fcf5ef2aSThomas Huth VGENERIC_DO(popcntw, u32)
2170fcf5ef2aSThomas Huth VGENERIC_DO(popcntd, u64)
2171fcf5ef2aSThomas Huth 
2172fcf5ef2aSThomas Huth #undef popcntb
2173fcf5ef2aSThomas Huth #undef popcnth
2174fcf5ef2aSThomas Huth #undef popcntw
2175fcf5ef2aSThomas Huth #undef popcntd
2176fcf5ef2aSThomas Huth 
2177fcf5ef2aSThomas Huth #undef VGENERIC_DO
2178fcf5ef2aSThomas Huth 
2179e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
2180fcf5ef2aSThomas Huth #define QW_ONE { .u64 = { 0, 1 } }
2181fcf5ef2aSThomas Huth #else
2182fcf5ef2aSThomas Huth #define QW_ONE { .u64 = { 1, 0 } }
2183fcf5ef2aSThomas Huth #endif
2184fcf5ef2aSThomas Huth 
2185fcf5ef2aSThomas Huth #ifndef CONFIG_INT128
2186fcf5ef2aSThomas Huth 
2187fcf5ef2aSThomas Huth static inline void avr_qw_not(ppc_avr_t *t, ppc_avr_t a)
2188fcf5ef2aSThomas Huth {
2189fcf5ef2aSThomas Huth     t->u64[0] = ~a.u64[0];
2190fcf5ef2aSThomas Huth     t->u64[1] = ~a.u64[1];
2191fcf5ef2aSThomas Huth }
2192fcf5ef2aSThomas Huth 
2193fcf5ef2aSThomas Huth static int avr_qw_cmpu(ppc_avr_t a, ppc_avr_t b)
2194fcf5ef2aSThomas Huth {
21953c385a93SMark Cave-Ayland     if (a.VsrD(0) < b.VsrD(0)) {
2196fcf5ef2aSThomas Huth         return -1;
21973c385a93SMark Cave-Ayland     } else if (a.VsrD(0) > b.VsrD(0)) {
2198fcf5ef2aSThomas Huth         return 1;
21993c385a93SMark Cave-Ayland     } else if (a.VsrD(1) < b.VsrD(1)) {
2200fcf5ef2aSThomas Huth         return -1;
22013c385a93SMark Cave-Ayland     } else if (a.VsrD(1) > b.VsrD(1)) {
2202fcf5ef2aSThomas Huth         return 1;
2203fcf5ef2aSThomas Huth     } else {
2204fcf5ef2aSThomas Huth         return 0;
2205fcf5ef2aSThomas Huth     }
2206fcf5ef2aSThomas Huth }
2207fcf5ef2aSThomas Huth 
2208fcf5ef2aSThomas Huth static void avr_qw_add(ppc_avr_t *t, ppc_avr_t a, ppc_avr_t b)
2209fcf5ef2aSThomas Huth {
22103c385a93SMark Cave-Ayland     t->VsrD(1) = a.VsrD(1) + b.VsrD(1);
22113c385a93SMark Cave-Ayland     t->VsrD(0) = a.VsrD(0) + b.VsrD(0) +
22123c385a93SMark Cave-Ayland                      (~a.VsrD(1) < b.VsrD(1));
2213fcf5ef2aSThomas Huth }
2214fcf5ef2aSThomas Huth 
2215fcf5ef2aSThomas Huth static int avr_qw_addc(ppc_avr_t *t, ppc_avr_t a, ppc_avr_t b)
2216fcf5ef2aSThomas Huth {
2217fcf5ef2aSThomas Huth     ppc_avr_t not_a;
22183c385a93SMark Cave-Ayland     t->VsrD(1) = a.VsrD(1) + b.VsrD(1);
22193c385a93SMark Cave-Ayland     t->VsrD(0) = a.VsrD(0) + b.VsrD(0) +
22203c385a93SMark Cave-Ayland                      (~a.VsrD(1) < b.VsrD(1));
2221fcf5ef2aSThomas Huth     avr_qw_not(&not_a, a);
2222fcf5ef2aSThomas Huth     return avr_qw_cmpu(not_a, b) < 0;
2223fcf5ef2aSThomas Huth }
2224fcf5ef2aSThomas Huth 
2225fcf5ef2aSThomas Huth #endif
2226fcf5ef2aSThomas Huth 
2227*7ca04286SMatheus Ferst void helper_VADDUQM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2228fcf5ef2aSThomas Huth {
2229*7ca04286SMatheus Ferst     r->s128 = int128_add(a->s128, b->s128);
2230fcf5ef2aSThomas Huth }
2231fcf5ef2aSThomas Huth 
2232fcf5ef2aSThomas Huth void helper_vaddeuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2233fcf5ef2aSThomas Huth {
2234fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2235fcf5ef2aSThomas Huth     r->u128 = a->u128 + b->u128 + (c->u128 & 1);
2236fcf5ef2aSThomas Huth #else
2237fcf5ef2aSThomas Huth 
22383c385a93SMark Cave-Ayland     if (c->VsrD(1) & 1) {
2239fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2240fcf5ef2aSThomas Huth 
22413c385a93SMark Cave-Ayland         tmp.VsrD(0) = 0;
22423c385a93SMark Cave-Ayland         tmp.VsrD(1) = c->VsrD(1) & 1;
2243fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
2244fcf5ef2aSThomas Huth         avr_qw_add(r, tmp, *b);
2245fcf5ef2aSThomas Huth     } else {
2246fcf5ef2aSThomas Huth         avr_qw_add(r, *a, *b);
2247fcf5ef2aSThomas Huth     }
2248fcf5ef2aSThomas Huth #endif
2249fcf5ef2aSThomas Huth }
2250fcf5ef2aSThomas Huth 
2251fcf5ef2aSThomas Huth void helper_vaddcuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2252fcf5ef2aSThomas Huth {
2253fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2254fcf5ef2aSThomas Huth     r->u128 = (~a->u128 < b->u128);
2255fcf5ef2aSThomas Huth #else
2256fcf5ef2aSThomas Huth     ppc_avr_t not_a;
2257fcf5ef2aSThomas Huth 
2258fcf5ef2aSThomas Huth     avr_qw_not(&not_a, *a);
2259fcf5ef2aSThomas Huth 
22603c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
22613c385a93SMark Cave-Ayland     r->VsrD(1) = (avr_qw_cmpu(not_a, *b) < 0);
2262fcf5ef2aSThomas Huth #endif
2263fcf5ef2aSThomas Huth }
2264fcf5ef2aSThomas Huth 
2265fcf5ef2aSThomas Huth void helper_vaddecuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2266fcf5ef2aSThomas Huth {
2267fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2268fcf5ef2aSThomas Huth     int carry_out = (~a->u128 < b->u128);
2269fcf5ef2aSThomas Huth     if (!carry_out && (c->u128 & 1)) {
2270fcf5ef2aSThomas Huth         carry_out = ((a->u128 + b->u128 + 1) == 0) &&
2271fcf5ef2aSThomas Huth                     ((a->u128 != 0) || (b->u128 != 0));
2272fcf5ef2aSThomas Huth     }
2273fcf5ef2aSThomas Huth     r->u128 = carry_out;
2274fcf5ef2aSThomas Huth #else
2275fcf5ef2aSThomas Huth 
22763c385a93SMark Cave-Ayland     int carry_in = c->VsrD(1) & 1;
2277fcf5ef2aSThomas Huth     int carry_out = 0;
2278fcf5ef2aSThomas Huth     ppc_avr_t tmp;
2279fcf5ef2aSThomas Huth 
2280fcf5ef2aSThomas Huth     carry_out = avr_qw_addc(&tmp, *a, *b);
2281fcf5ef2aSThomas Huth 
2282fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
2283fcf5ef2aSThomas Huth         ppc_avr_t one = QW_ONE;
2284fcf5ef2aSThomas Huth         carry_out = avr_qw_addc(&tmp, tmp, one);
2285fcf5ef2aSThomas Huth     }
22863c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
22873c385a93SMark Cave-Ayland     r->VsrD(1) = carry_out;
2288fcf5ef2aSThomas Huth #endif
2289fcf5ef2aSThomas Huth }
2290fcf5ef2aSThomas Huth 
2291fcf5ef2aSThomas Huth void helper_vsubuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2292fcf5ef2aSThomas Huth {
2293fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2294fcf5ef2aSThomas Huth     r->u128 = a->u128 - b->u128;
2295fcf5ef2aSThomas Huth #else
2296fcf5ef2aSThomas Huth     ppc_avr_t tmp;
2297fcf5ef2aSThomas Huth     ppc_avr_t one = QW_ONE;
2298fcf5ef2aSThomas Huth 
2299fcf5ef2aSThomas Huth     avr_qw_not(&tmp, *b);
2300fcf5ef2aSThomas Huth     avr_qw_add(&tmp, *a, tmp);
2301fcf5ef2aSThomas Huth     avr_qw_add(r, tmp, one);
2302fcf5ef2aSThomas Huth #endif
2303fcf5ef2aSThomas Huth }
2304fcf5ef2aSThomas Huth 
2305fcf5ef2aSThomas Huth void helper_vsubeuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2306fcf5ef2aSThomas Huth {
2307fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2308fcf5ef2aSThomas Huth     r->u128 = a->u128 + ~b->u128 + (c->u128 & 1);
2309fcf5ef2aSThomas Huth #else
2310fcf5ef2aSThomas Huth     ppc_avr_t tmp, sum;
2311fcf5ef2aSThomas Huth 
2312fcf5ef2aSThomas Huth     avr_qw_not(&tmp, *b);
2313fcf5ef2aSThomas Huth     avr_qw_add(&sum, *a, tmp);
2314fcf5ef2aSThomas Huth 
23153c385a93SMark Cave-Ayland     tmp.VsrD(0) = 0;
23163c385a93SMark Cave-Ayland     tmp.VsrD(1) = c->VsrD(1) & 1;
2317fcf5ef2aSThomas Huth     avr_qw_add(r, sum, tmp);
2318fcf5ef2aSThomas Huth #endif
2319fcf5ef2aSThomas Huth }
2320fcf5ef2aSThomas Huth 
2321fcf5ef2aSThomas Huth void helper_vsubcuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2322fcf5ef2aSThomas Huth {
2323fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2324fcf5ef2aSThomas Huth     r->u128 = (~a->u128 < ~b->u128) ||
2325fcf5ef2aSThomas Huth                  (a->u128 + ~b->u128 == (__uint128_t)-1);
2326fcf5ef2aSThomas Huth #else
2327fcf5ef2aSThomas Huth     int carry = (avr_qw_cmpu(*a, *b) > 0);
2328fcf5ef2aSThomas Huth     if (!carry) {
2329fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2330fcf5ef2aSThomas Huth         avr_qw_not(&tmp, *b);
2331fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
23323c385a93SMark Cave-Ayland         carry = ((tmp.VsrSD(0) == -1ull) && (tmp.VsrSD(1) == -1ull));
2333fcf5ef2aSThomas Huth     }
23343c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
23353c385a93SMark Cave-Ayland     r->VsrD(1) = carry;
2336fcf5ef2aSThomas Huth #endif
2337fcf5ef2aSThomas Huth }
2338fcf5ef2aSThomas Huth 
2339fcf5ef2aSThomas Huth void helper_vsubecuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2340fcf5ef2aSThomas Huth {
2341fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2342fcf5ef2aSThomas Huth     r->u128 =
2343fcf5ef2aSThomas Huth         (~a->u128 < ~b->u128) ||
2344fcf5ef2aSThomas Huth         ((c->u128 & 1) && (a->u128 + ~b->u128 == (__uint128_t)-1));
2345fcf5ef2aSThomas Huth #else
23463c385a93SMark Cave-Ayland     int carry_in = c->VsrD(1) & 1;
2347fcf5ef2aSThomas Huth     int carry_out = (avr_qw_cmpu(*a, *b) > 0);
2348fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
2349fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2350fcf5ef2aSThomas Huth         avr_qw_not(&tmp, *b);
2351fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
23523c385a93SMark Cave-Ayland         carry_out = ((tmp.VsrD(0) == -1ull) && (tmp.VsrD(1) == -1ull));
2353fcf5ef2aSThomas Huth     }
2354fcf5ef2aSThomas Huth 
23553c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
23563c385a93SMark Cave-Ayland     r->VsrD(1) = carry_out;
2357fcf5ef2aSThomas Huth #endif
2358fcf5ef2aSThomas Huth }
2359fcf5ef2aSThomas Huth 
2360fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_1 0xC
2361fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_2 0xF
2362fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_1  0xA
2363fcf5ef2aSThomas Huth #define BCD_NEG_PREF    0xD
2364fcf5ef2aSThomas Huth #define BCD_NEG_ALT     0xB
2365fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_2  0xE
2366fcf5ef2aSThomas Huth #define NATIONAL_PLUS   0x2B
2367fcf5ef2aSThomas Huth #define NATIONAL_NEG    0x2D
2368fcf5ef2aSThomas Huth 
2369365206aeSJose Ricardo Ziviani #define BCD_DIG_BYTE(n) (15 - ((n) / 2))
2370fcf5ef2aSThomas Huth 
2371fcf5ef2aSThomas Huth static int bcd_get_sgn(ppc_avr_t *bcd)
2372fcf5ef2aSThomas Huth {
2373428115c3SMark Cave-Ayland     switch (bcd->VsrB(BCD_DIG_BYTE(0)) & 0xF) {
2374fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_1:
2375fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_2:
2376fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_1:
2377fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_2:
2378fcf5ef2aSThomas Huth     {
2379fcf5ef2aSThomas Huth         return 1;
2380fcf5ef2aSThomas Huth     }
2381fcf5ef2aSThomas Huth 
2382fcf5ef2aSThomas Huth     case BCD_NEG_PREF:
2383fcf5ef2aSThomas Huth     case BCD_NEG_ALT:
2384fcf5ef2aSThomas Huth     {
2385fcf5ef2aSThomas Huth         return -1;
2386fcf5ef2aSThomas Huth     }
2387fcf5ef2aSThomas Huth 
2388fcf5ef2aSThomas Huth     default:
2389fcf5ef2aSThomas Huth     {
2390fcf5ef2aSThomas Huth         return 0;
2391fcf5ef2aSThomas Huth     }
2392fcf5ef2aSThomas Huth     }
2393fcf5ef2aSThomas Huth }
2394fcf5ef2aSThomas Huth 
2395fcf5ef2aSThomas Huth static int bcd_preferred_sgn(int sgn, int ps)
2396fcf5ef2aSThomas Huth {
2397fcf5ef2aSThomas Huth     if (sgn >= 0) {
2398fcf5ef2aSThomas Huth         return (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2;
2399fcf5ef2aSThomas Huth     } else {
2400fcf5ef2aSThomas Huth         return BCD_NEG_PREF;
2401fcf5ef2aSThomas Huth     }
2402fcf5ef2aSThomas Huth }
2403fcf5ef2aSThomas Huth 
2404fcf5ef2aSThomas Huth static uint8_t bcd_get_digit(ppc_avr_t *bcd, int n, int *invalid)
2405fcf5ef2aSThomas Huth {
2406fcf5ef2aSThomas Huth     uint8_t result;
2407fcf5ef2aSThomas Huth     if (n & 1) {
2408428115c3SMark Cave-Ayland         result = bcd->VsrB(BCD_DIG_BYTE(n)) >> 4;
2409fcf5ef2aSThomas Huth     } else {
2410428115c3SMark Cave-Ayland        result = bcd->VsrB(BCD_DIG_BYTE(n)) & 0xF;
2411fcf5ef2aSThomas Huth     }
2412fcf5ef2aSThomas Huth 
2413fcf5ef2aSThomas Huth     if (unlikely(result > 9)) {
2414fcf5ef2aSThomas Huth         *invalid = true;
2415fcf5ef2aSThomas Huth     }
2416fcf5ef2aSThomas Huth     return result;
2417fcf5ef2aSThomas Huth }
2418fcf5ef2aSThomas Huth 
2419fcf5ef2aSThomas Huth static void bcd_put_digit(ppc_avr_t *bcd, uint8_t digit, int n)
2420fcf5ef2aSThomas Huth {
2421fcf5ef2aSThomas Huth     if (n & 1) {
2422428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) &= 0x0F;
2423428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) |= (digit << 4);
2424fcf5ef2aSThomas Huth     } else {
2425428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) &= 0xF0;
2426428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) |= digit;
2427fcf5ef2aSThomas Huth     }
2428fcf5ef2aSThomas Huth }
2429fcf5ef2aSThomas Huth 
2430071663dfSJose Ricardo Ziviani static bool bcd_is_valid(ppc_avr_t *bcd)
2431071663dfSJose Ricardo Ziviani {
2432071663dfSJose Ricardo Ziviani     int i;
2433071663dfSJose Ricardo Ziviani     int invalid = 0;
2434071663dfSJose Ricardo Ziviani 
2435071663dfSJose Ricardo Ziviani     if (bcd_get_sgn(bcd) == 0) {
2436071663dfSJose Ricardo Ziviani         return false;
2437071663dfSJose Ricardo Ziviani     }
2438071663dfSJose Ricardo Ziviani 
2439071663dfSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2440071663dfSJose Ricardo Ziviani         bcd_get_digit(bcd, i, &invalid);
2441071663dfSJose Ricardo Ziviani         if (unlikely(invalid)) {
2442071663dfSJose Ricardo Ziviani             return false;
2443071663dfSJose Ricardo Ziviani         }
2444071663dfSJose Ricardo Ziviani     }
2445071663dfSJose Ricardo Ziviani     return true;
2446071663dfSJose Ricardo Ziviani }
2447071663dfSJose Ricardo Ziviani 
2448fcf5ef2aSThomas Huth static int bcd_cmp_zero(ppc_avr_t *bcd)
2449fcf5ef2aSThomas Huth {
24503c385a93SMark Cave-Ayland     if (bcd->VsrD(0) == 0 && (bcd->VsrD(1) >> 4) == 0) {
2451efa73196SNikunj A Dadhania         return CRF_EQ;
2452fcf5ef2aSThomas Huth     } else {
2453efa73196SNikunj A Dadhania         return (bcd_get_sgn(bcd) == 1) ? CRF_GT : CRF_LT;
2454fcf5ef2aSThomas Huth     }
2455fcf5ef2aSThomas Huth }
2456fcf5ef2aSThomas Huth 
2457fcf5ef2aSThomas Huth static uint16_t get_national_digit(ppc_avr_t *reg, int n)
2458fcf5ef2aSThomas Huth {
245960594feaSMark Cave-Ayland     return reg->VsrH(7 - n);
2460fcf5ef2aSThomas Huth }
2461fcf5ef2aSThomas Huth 
2462fcf5ef2aSThomas Huth static void set_national_digit(ppc_avr_t *reg, uint8_t val, int n)
2463fcf5ef2aSThomas Huth {
246460594feaSMark Cave-Ayland     reg->VsrH(7 - n) = val;
2465fcf5ef2aSThomas Huth }
2466fcf5ef2aSThomas Huth 
2467fcf5ef2aSThomas Huth static int bcd_cmp_mag(ppc_avr_t *a, ppc_avr_t *b)
2468fcf5ef2aSThomas Huth {
2469fcf5ef2aSThomas Huth     int i;
2470fcf5ef2aSThomas Huth     int invalid = 0;
2471fcf5ef2aSThomas Huth     for (i = 31; i > 0; i--) {
2472fcf5ef2aSThomas Huth         uint8_t dig_a = bcd_get_digit(a, i, &invalid);
2473fcf5ef2aSThomas Huth         uint8_t dig_b = bcd_get_digit(b, i, &invalid);
2474fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2475fcf5ef2aSThomas Huth             return 0; /* doesn't matter */
2476fcf5ef2aSThomas Huth         } else if (dig_a > dig_b) {
2477fcf5ef2aSThomas Huth             return 1;
2478fcf5ef2aSThomas Huth         } else if (dig_a < dig_b) {
2479fcf5ef2aSThomas Huth             return -1;
2480fcf5ef2aSThomas Huth         }
2481fcf5ef2aSThomas Huth     }
2482fcf5ef2aSThomas Huth 
2483fcf5ef2aSThomas Huth     return 0;
2484fcf5ef2aSThomas Huth }
2485fcf5ef2aSThomas Huth 
2486936fda4dSFabiano Rosas static int bcd_add_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2487fcf5ef2aSThomas Huth                        int *overflow)
2488fcf5ef2aSThomas Huth {
2489fcf5ef2aSThomas Huth     int carry = 0;
2490fcf5ef2aSThomas Huth     int i;
2491936fda4dSFabiano Rosas     int is_zero = 1;
2492936fda4dSFabiano Rosas 
2493fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2494fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) +
2495fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2496936fda4dSFabiano Rosas         is_zero &= (digit == 0);
2497fcf5ef2aSThomas Huth         if (digit > 9) {
2498fcf5ef2aSThomas Huth             carry = 1;
2499fcf5ef2aSThomas Huth             digit -= 10;
2500fcf5ef2aSThomas Huth         } else {
2501fcf5ef2aSThomas Huth             carry = 0;
2502fcf5ef2aSThomas Huth         }
2503fcf5ef2aSThomas Huth 
2504fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2505fcf5ef2aSThomas Huth     }
2506fcf5ef2aSThomas Huth 
2507fcf5ef2aSThomas Huth     *overflow = carry;
2508936fda4dSFabiano Rosas     return is_zero;
2509fcf5ef2aSThomas Huth }
2510fcf5ef2aSThomas Huth 
2511d03b174aSYasmin Beatriz static void bcd_sub_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2512fcf5ef2aSThomas Huth                        int *overflow)
2513fcf5ef2aSThomas Huth {
2514fcf5ef2aSThomas Huth     int carry = 0;
2515fcf5ef2aSThomas Huth     int i;
2516d03b174aSYasmin Beatriz 
2517fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2518fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) -
2519fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2520fcf5ef2aSThomas Huth         if (digit & 0x80) {
2521fcf5ef2aSThomas Huth             carry = -1;
2522fcf5ef2aSThomas Huth             digit += 10;
2523fcf5ef2aSThomas Huth         } else {
2524fcf5ef2aSThomas Huth             carry = 0;
2525fcf5ef2aSThomas Huth         }
2526fcf5ef2aSThomas Huth 
2527fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2528fcf5ef2aSThomas Huth     }
2529fcf5ef2aSThomas Huth 
2530fcf5ef2aSThomas Huth     *overflow = carry;
2531fcf5ef2aSThomas Huth }
2532fcf5ef2aSThomas Huth 
2533fcf5ef2aSThomas Huth uint32_t helper_bcdadd(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2534fcf5ef2aSThomas Huth {
2535fcf5ef2aSThomas Huth 
2536fcf5ef2aSThomas Huth     int sgna = bcd_get_sgn(a);
2537fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2538fcf5ef2aSThomas Huth     int invalid = (sgna == 0) || (sgnb == 0);
2539fcf5ef2aSThomas Huth     int overflow = 0;
2540936fda4dSFabiano Rosas     int zero = 0;
2541fcf5ef2aSThomas Huth     uint32_t cr = 0;
2542fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
2543fcf5ef2aSThomas Huth 
2544fcf5ef2aSThomas Huth     if (!invalid) {
2545fcf5ef2aSThomas Huth         if (sgna == sgnb) {
2546428115c3SMark Cave-Ayland             result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna, ps);
2547936fda4dSFabiano Rosas             zero = bcd_add_mag(&result, a, b, &invalid, &overflow);
2548936fda4dSFabiano Rosas             cr = (sgna > 0) ? CRF_GT : CRF_LT;
2549fcf5ef2aSThomas Huth         } else {
2550d03b174aSYasmin Beatriz             int magnitude = bcd_cmp_mag(a, b);
2551d03b174aSYasmin Beatriz             if (magnitude > 0) {
2552428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna, ps);
2553d03b174aSYasmin Beatriz                 bcd_sub_mag(&result, a, b, &invalid, &overflow);
2554d03b174aSYasmin Beatriz                 cr = (sgna > 0) ? CRF_GT : CRF_LT;
2555d03b174aSYasmin Beatriz             } else if (magnitude < 0) {
2556428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgnb, ps);
2557d03b174aSYasmin Beatriz                 bcd_sub_mag(&result, b, a, &invalid, &overflow);
2558efa73196SNikunj A Dadhania                 cr = (sgnb > 0) ? CRF_GT : CRF_LT;
2559d03b174aSYasmin Beatriz             } else {
2560428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(0, ps);
2561d03b174aSYasmin Beatriz                 cr = CRF_EQ;
2562d03b174aSYasmin Beatriz             }
2563fcf5ef2aSThomas Huth         }
2564fcf5ef2aSThomas Huth     }
2565fcf5ef2aSThomas Huth 
2566fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
25673c385a93SMark Cave-Ayland         result.VsrD(0) = result.VsrD(1) = -1;
2568efa73196SNikunj A Dadhania         cr = CRF_SO;
2569fcf5ef2aSThomas Huth     } else if (overflow) {
2570efa73196SNikunj A Dadhania         cr |= CRF_SO;
2571936fda4dSFabiano Rosas     } else if (zero) {
2572936fda4dSFabiano Rosas         cr |= CRF_EQ;
2573fcf5ef2aSThomas Huth     }
2574fcf5ef2aSThomas Huth 
2575fcf5ef2aSThomas Huth     *r = result;
2576fcf5ef2aSThomas Huth 
2577fcf5ef2aSThomas Huth     return cr;
2578fcf5ef2aSThomas Huth }
2579fcf5ef2aSThomas Huth 
2580fcf5ef2aSThomas Huth uint32_t helper_bcdsub(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2581fcf5ef2aSThomas Huth {
2582fcf5ef2aSThomas Huth     ppc_avr_t bcopy = *b;
2583fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2584fcf5ef2aSThomas Huth     if (sgnb < 0) {
2585fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_PLUS_PREF_1, 0);
2586fcf5ef2aSThomas Huth     } else if (sgnb > 0) {
2587fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_NEG_PREF, 0);
2588fcf5ef2aSThomas Huth     }
2589fcf5ef2aSThomas Huth     /* else invalid ... defer to bcdadd code for proper handling */
2590fcf5ef2aSThomas Huth 
2591fcf5ef2aSThomas Huth     return helper_bcdadd(r, a, &bcopy, ps);
2592fcf5ef2aSThomas Huth }
2593fcf5ef2aSThomas Huth 
2594fcf5ef2aSThomas Huth uint32_t helper_bcdcfn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2595fcf5ef2aSThomas Huth {
2596fcf5ef2aSThomas Huth     int i;
2597fcf5ef2aSThomas Huth     int cr = 0;
2598fcf5ef2aSThomas Huth     uint16_t national = 0;
2599fcf5ef2aSThomas Huth     uint16_t sgnb = get_national_digit(b, 0);
2600fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2601fcf5ef2aSThomas Huth     int invalid = (sgnb != NATIONAL_PLUS && sgnb != NATIONAL_NEG);
2602fcf5ef2aSThomas Huth 
2603fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2604fcf5ef2aSThomas Huth         national = get_national_digit(b, i);
2605fcf5ef2aSThomas Huth         if (unlikely(national < 0x30 || national > 0x39)) {
2606fcf5ef2aSThomas Huth             invalid = 1;
2607fcf5ef2aSThomas Huth             break;
2608fcf5ef2aSThomas Huth         }
2609fcf5ef2aSThomas Huth 
2610fcf5ef2aSThomas Huth         bcd_put_digit(&ret, national & 0xf, i);
2611fcf5ef2aSThomas Huth     }
2612fcf5ef2aSThomas Huth 
2613fcf5ef2aSThomas Huth     if (sgnb == NATIONAL_PLUS) {
2614fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2, 0);
2615fcf5ef2aSThomas Huth     } else {
2616fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2617fcf5ef2aSThomas Huth     }
2618fcf5ef2aSThomas Huth 
2619fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2620fcf5ef2aSThomas Huth 
2621fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2622efa73196SNikunj A Dadhania         cr = CRF_SO;
2623fcf5ef2aSThomas Huth     }
2624fcf5ef2aSThomas Huth 
2625fcf5ef2aSThomas Huth     *r = ret;
2626fcf5ef2aSThomas Huth 
2627fcf5ef2aSThomas Huth     return cr;
2628fcf5ef2aSThomas Huth }
2629fcf5ef2aSThomas Huth 
2630fcf5ef2aSThomas Huth uint32_t helper_bcdctn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2631fcf5ef2aSThomas Huth {
2632fcf5ef2aSThomas Huth     int i;
2633fcf5ef2aSThomas Huth     int cr = 0;
2634fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2635fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2636fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2637fcf5ef2aSThomas Huth 
26383c385a93SMark Cave-Ayland     int ox_flag = (b->VsrD(0) != 0) || ((b->VsrD(1) >> 32) != 0);
2639fcf5ef2aSThomas Huth 
2640fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2641fcf5ef2aSThomas Huth         set_national_digit(&ret, 0x30 + bcd_get_digit(b, i, &invalid), i);
2642fcf5ef2aSThomas Huth 
2643fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2644fcf5ef2aSThomas Huth             break;
2645fcf5ef2aSThomas Huth         }
2646fcf5ef2aSThomas Huth     }
2647fcf5ef2aSThomas Huth     set_national_digit(&ret, (sgnb == -1) ? NATIONAL_NEG : NATIONAL_PLUS, 0);
2648fcf5ef2aSThomas Huth 
2649fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2650fcf5ef2aSThomas Huth 
2651fcf5ef2aSThomas Huth     if (ox_flag) {
2652efa73196SNikunj A Dadhania         cr |= CRF_SO;
2653fcf5ef2aSThomas Huth     }
2654fcf5ef2aSThomas Huth 
2655fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2656efa73196SNikunj A Dadhania         cr = CRF_SO;
2657fcf5ef2aSThomas Huth     }
2658fcf5ef2aSThomas Huth 
2659fcf5ef2aSThomas Huth     *r = ret;
2660fcf5ef2aSThomas Huth 
2661fcf5ef2aSThomas Huth     return cr;
2662fcf5ef2aSThomas Huth }
2663fcf5ef2aSThomas Huth 
2664fcf5ef2aSThomas Huth uint32_t helper_bcdcfz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2665fcf5ef2aSThomas Huth {
2666fcf5ef2aSThomas Huth     int i;
2667fcf5ef2aSThomas Huth     int cr = 0;
2668fcf5ef2aSThomas Huth     int invalid = 0;
2669fcf5ef2aSThomas Huth     int zone_digit = 0;
2670fcf5ef2aSThomas Huth     int zone_lead = ps ? 0xF : 0x3;
2671fcf5ef2aSThomas Huth     int digit = 0;
2672fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2673428115c3SMark Cave-Ayland     int sgnb = b->VsrB(BCD_DIG_BYTE(0)) >> 4;
2674fcf5ef2aSThomas Huth 
2675fcf5ef2aSThomas Huth     if (unlikely((sgnb < 0xA) && ps)) {
2676fcf5ef2aSThomas Huth         invalid = 1;
2677fcf5ef2aSThomas Huth     }
2678fcf5ef2aSThomas Huth 
2679fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2680428115c3SMark Cave-Ayland         zone_digit = i ? b->VsrB(BCD_DIG_BYTE(i * 2)) >> 4 : zone_lead;
2681428115c3SMark Cave-Ayland         digit = b->VsrB(BCD_DIG_BYTE(i * 2)) & 0xF;
2682fcf5ef2aSThomas Huth         if (unlikely(zone_digit != zone_lead || digit > 0x9)) {
2683fcf5ef2aSThomas Huth             invalid = 1;
2684fcf5ef2aSThomas Huth             break;
2685fcf5ef2aSThomas Huth         }
2686fcf5ef2aSThomas Huth 
2687fcf5ef2aSThomas Huth         bcd_put_digit(&ret, digit, i + 1);
2688fcf5ef2aSThomas Huth     }
2689fcf5ef2aSThomas Huth 
2690fcf5ef2aSThomas Huth     if ((ps && (sgnb == 0xB || sgnb == 0xD)) ||
2691fcf5ef2aSThomas Huth             (!ps && (sgnb & 0x4))) {
2692fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2693fcf5ef2aSThomas Huth     } else {
2694fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_PLUS_PREF_1, 0);
2695fcf5ef2aSThomas Huth     }
2696fcf5ef2aSThomas Huth 
2697fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2698fcf5ef2aSThomas Huth 
2699fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2700efa73196SNikunj A Dadhania         cr = CRF_SO;
2701fcf5ef2aSThomas Huth     }
2702fcf5ef2aSThomas Huth 
2703fcf5ef2aSThomas Huth     *r = ret;
2704fcf5ef2aSThomas Huth 
2705fcf5ef2aSThomas Huth     return cr;
2706fcf5ef2aSThomas Huth }
2707fcf5ef2aSThomas Huth 
2708fcf5ef2aSThomas Huth uint32_t helper_bcdctz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2709fcf5ef2aSThomas Huth {
2710fcf5ef2aSThomas Huth     int i;
2711fcf5ef2aSThomas Huth     int cr = 0;
2712fcf5ef2aSThomas Huth     uint8_t digit = 0;
2713fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2714fcf5ef2aSThomas Huth     int zone_lead = (ps) ? 0xF0 : 0x30;
2715fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2716fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2717fcf5ef2aSThomas Huth 
27183c385a93SMark Cave-Ayland     int ox_flag = ((b->VsrD(0) >> 4) != 0);
2719fcf5ef2aSThomas Huth 
2720fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2721fcf5ef2aSThomas Huth         digit = bcd_get_digit(b, i + 1, &invalid);
2722fcf5ef2aSThomas Huth 
2723fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2724fcf5ef2aSThomas Huth             break;
2725fcf5ef2aSThomas Huth         }
2726fcf5ef2aSThomas Huth 
2727428115c3SMark Cave-Ayland         ret.VsrB(BCD_DIG_BYTE(i * 2)) = zone_lead + digit;
2728fcf5ef2aSThomas Huth     }
2729fcf5ef2aSThomas Huth 
2730fcf5ef2aSThomas Huth     if (ps) {
2731fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0xC : 0xD, 1);
2732fcf5ef2aSThomas Huth     } else {
2733fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0x3 : 0x7, 1);
2734fcf5ef2aSThomas Huth     }
2735fcf5ef2aSThomas Huth 
2736fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2737fcf5ef2aSThomas Huth 
2738fcf5ef2aSThomas Huth     if (ox_flag) {
2739efa73196SNikunj A Dadhania         cr |= CRF_SO;
2740fcf5ef2aSThomas Huth     }
2741fcf5ef2aSThomas Huth 
2742fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2743efa73196SNikunj A Dadhania         cr = CRF_SO;
2744fcf5ef2aSThomas Huth     }
2745fcf5ef2aSThomas Huth 
2746fcf5ef2aSThomas Huth     *r = ret;
2747fcf5ef2aSThomas Huth 
2748fcf5ef2aSThomas Huth     return cr;
2749fcf5ef2aSThomas Huth }
2750fcf5ef2aSThomas Huth 
2751a3d67f3eSLuis Pires /**
2752a3d67f3eSLuis Pires  * Compare 2 128-bit unsigned integers, passed in as unsigned 64-bit pairs
2753a3d67f3eSLuis Pires  *
2754a3d67f3eSLuis Pires  * Returns:
2755a3d67f3eSLuis Pires  * > 0 if ahi|alo > bhi|blo,
2756a3d67f3eSLuis Pires  * 0 if ahi|alo == bhi|blo,
2757a3d67f3eSLuis Pires  * < 0 if ahi|alo < bhi|blo
2758a3d67f3eSLuis Pires  */
2759a3d67f3eSLuis Pires static inline int ucmp128(uint64_t alo, uint64_t ahi,
2760a3d67f3eSLuis Pires                           uint64_t blo, uint64_t bhi)
2761a3d67f3eSLuis Pires {
2762a3d67f3eSLuis Pires     return (ahi == bhi) ?
2763a3d67f3eSLuis Pires         (alo > blo ? 1 : (alo == blo ? 0 : -1)) :
2764a3d67f3eSLuis Pires         (ahi > bhi ? 1 : -1);
2765a3d67f3eSLuis Pires }
2766a3d67f3eSLuis Pires 
2767a406c058SJose Ricardo Ziviani uint32_t helper_bcdcfsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2768a406c058SJose Ricardo Ziviani {
2769a406c058SJose Ricardo Ziviani     int i;
2770a3d67f3eSLuis Pires     int cr;
2771a406c058SJose Ricardo Ziviani     uint64_t lo_value;
2772a406c058SJose Ricardo Ziviani     uint64_t hi_value;
277340f3e79aSLuis Pires     uint64_t rem;
2774a406c058SJose Ricardo Ziviani     ppc_avr_t ret = { .u64 = { 0, 0 } };
2775a406c058SJose Ricardo Ziviani 
27763c385a93SMark Cave-Ayland     if (b->VsrSD(0) < 0) {
27773c385a93SMark Cave-Ayland         lo_value = -b->VsrSD(1);
27783c385a93SMark Cave-Ayland         hi_value = ~b->VsrD(0) + !lo_value;
2779a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, 0xD, 0);
2780a3d67f3eSLuis Pires 
2781a3d67f3eSLuis Pires         cr = CRF_LT;
2782a406c058SJose Ricardo Ziviani     } else {
27833c385a93SMark Cave-Ayland         lo_value = b->VsrD(1);
27843c385a93SMark Cave-Ayland         hi_value = b->VsrD(0);
2785a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, bcd_preferred_sgn(0, ps), 0);
2786a3d67f3eSLuis Pires 
2787a3d67f3eSLuis Pires         if (hi_value == 0 && lo_value == 0) {
2788a3d67f3eSLuis Pires             cr = CRF_EQ;
2789a3d67f3eSLuis Pires         } else {
2790a3d67f3eSLuis Pires             cr = CRF_GT;
2791a3d67f3eSLuis Pires         }
2792a406c058SJose Ricardo Ziviani     }
2793a406c058SJose Ricardo Ziviani 
2794a3d67f3eSLuis Pires     /*
2795a3d67f3eSLuis Pires      * Check src limits: abs(src) <= 10^31 - 1
2796a3d67f3eSLuis Pires      *
2797a3d67f3eSLuis Pires      * 10^31 - 1 = 0x0000007e37be2022 c0914b267fffffff
2798a3d67f3eSLuis Pires      */
2799a3d67f3eSLuis Pires     if (ucmp128(lo_value, hi_value,
2800a3d67f3eSLuis Pires                 0xc0914b267fffffffULL, 0x7e37be2022ULL) > 0) {
2801a3d67f3eSLuis Pires         cr |= CRF_SO;
2802a3d67f3eSLuis Pires 
2803a3d67f3eSLuis Pires         /*
2804a3d67f3eSLuis Pires          * According to the ISA, if src wouldn't fit in the destination
2805a3d67f3eSLuis Pires          * register, the result is undefined.
2806a3d67f3eSLuis Pires          * In that case, we leave r unchanged.
2807a3d67f3eSLuis Pires          */
2808a3d67f3eSLuis Pires     } else {
280940f3e79aSLuis Pires         rem = divu128(&lo_value, &hi_value, 1000000000000000ULL);
2810a406c058SJose Ricardo Ziviani 
281140f3e79aSLuis Pires         for (i = 1; i < 16; rem /= 10, i++) {
281240f3e79aSLuis Pires             bcd_put_digit(&ret, rem % 10, i);
2813a406c058SJose Ricardo Ziviani         }
2814a406c058SJose Ricardo Ziviani 
2815a406c058SJose Ricardo Ziviani         for (; i < 32; lo_value /= 10, i++) {
2816a406c058SJose Ricardo Ziviani             bcd_put_digit(&ret, lo_value % 10, i);
2817a406c058SJose Ricardo Ziviani         }
2818a406c058SJose Ricardo Ziviani 
2819a406c058SJose Ricardo Ziviani         *r = ret;
2820a3d67f3eSLuis Pires     }
2821a406c058SJose Ricardo Ziviani 
2822a406c058SJose Ricardo Ziviani     return cr;
2823a406c058SJose Ricardo Ziviani }
2824a406c058SJose Ricardo Ziviani 
2825c85bc7ddSJose Ricardo Ziviani uint32_t helper_bcdctsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2826c85bc7ddSJose Ricardo Ziviani {
2827c85bc7ddSJose Ricardo Ziviani     uint8_t i;
2828c85bc7ddSJose Ricardo Ziviani     int cr;
2829c85bc7ddSJose Ricardo Ziviani     uint64_t carry;
2830c85bc7ddSJose Ricardo Ziviani     uint64_t unused;
2831c85bc7ddSJose Ricardo Ziviani     uint64_t lo_value;
2832c85bc7ddSJose Ricardo Ziviani     uint64_t hi_value = 0;
2833c85bc7ddSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2834c85bc7ddSJose Ricardo Ziviani     int invalid = (sgnb == 0);
2835c85bc7ddSJose Ricardo Ziviani 
2836c85bc7ddSJose Ricardo Ziviani     lo_value = bcd_get_digit(b, 31, &invalid);
2837c85bc7ddSJose Ricardo Ziviani     for (i = 30; i > 0; i--) {
2838c85bc7ddSJose Ricardo Ziviani         mulu64(&lo_value, &carry, lo_value, 10ULL);
2839c85bc7ddSJose Ricardo Ziviani         mulu64(&hi_value, &unused, hi_value, 10ULL);
2840c85bc7ddSJose Ricardo Ziviani         lo_value += bcd_get_digit(b, i, &invalid);
2841c85bc7ddSJose Ricardo Ziviani         hi_value += carry;
2842c85bc7ddSJose Ricardo Ziviani 
2843c85bc7ddSJose Ricardo Ziviani         if (unlikely(invalid)) {
2844c85bc7ddSJose Ricardo Ziviani             break;
2845c85bc7ddSJose Ricardo Ziviani         }
2846c85bc7ddSJose Ricardo Ziviani     }
2847c85bc7ddSJose Ricardo Ziviani 
2848c85bc7ddSJose Ricardo Ziviani     if (sgnb == -1) {
28493c385a93SMark Cave-Ayland         r->VsrSD(1) = -lo_value;
28503c385a93SMark Cave-Ayland         r->VsrSD(0) = ~hi_value + !r->VsrSD(1);
2851c85bc7ddSJose Ricardo Ziviani     } else {
28523c385a93SMark Cave-Ayland         r->VsrSD(1) = lo_value;
28533c385a93SMark Cave-Ayland         r->VsrSD(0) = hi_value;
2854c85bc7ddSJose Ricardo Ziviani     }
2855c85bc7ddSJose Ricardo Ziviani 
2856c85bc7ddSJose Ricardo Ziviani     cr = bcd_cmp_zero(b);
2857c85bc7ddSJose Ricardo Ziviani 
2858c85bc7ddSJose Ricardo Ziviani     if (unlikely(invalid)) {
2859c85bc7ddSJose Ricardo Ziviani         cr = CRF_SO;
2860c85bc7ddSJose Ricardo Ziviani     }
2861c85bc7ddSJose Ricardo Ziviani 
2862c85bc7ddSJose Ricardo Ziviani     return cr;
2863c85bc7ddSJose Ricardo Ziviani }
2864c85bc7ddSJose Ricardo Ziviani 
2865c3025c3bSJose Ricardo Ziviani uint32_t helper_bcdcpsgn(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2866c3025c3bSJose Ricardo Ziviani {
2867c3025c3bSJose Ricardo Ziviani     int i;
2868c3025c3bSJose Ricardo Ziviani     int invalid = 0;
2869c3025c3bSJose Ricardo Ziviani 
2870c3025c3bSJose Ricardo Ziviani     if (bcd_get_sgn(a) == 0 || bcd_get_sgn(b) == 0) {
2871c3025c3bSJose Ricardo Ziviani         return CRF_SO;
2872c3025c3bSJose Ricardo Ziviani     }
2873c3025c3bSJose Ricardo Ziviani 
2874c3025c3bSJose Ricardo Ziviani     *r = *a;
2875428115c3SMark Cave-Ayland     bcd_put_digit(r, b->VsrB(BCD_DIG_BYTE(0)) & 0xF, 0);
2876c3025c3bSJose Ricardo Ziviani 
2877c3025c3bSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2878c3025c3bSJose Ricardo Ziviani         bcd_get_digit(a, i, &invalid);
2879c3025c3bSJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
2880c3025c3bSJose Ricardo Ziviani         if (unlikely(invalid)) {
2881c3025c3bSJose Ricardo Ziviani             return CRF_SO;
2882c3025c3bSJose Ricardo Ziviani         }
2883c3025c3bSJose Ricardo Ziviani     }
2884c3025c3bSJose Ricardo Ziviani 
2885c3025c3bSJose Ricardo Ziviani     return bcd_cmp_zero(r);
2886c3025c3bSJose Ricardo Ziviani }
2887c3025c3bSJose Ricardo Ziviani 
2888466a3f9cSJose Ricardo Ziviani uint32_t helper_bcdsetsgn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2889466a3f9cSJose Ricardo Ziviani {
2890466a3f9cSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2891466a3f9cSJose Ricardo Ziviani 
2892466a3f9cSJose Ricardo Ziviani     *r = *b;
2893466a3f9cSJose Ricardo Ziviani     bcd_put_digit(r, bcd_preferred_sgn(sgnb, ps), 0);
2894466a3f9cSJose Ricardo Ziviani 
2895071663dfSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2896466a3f9cSJose Ricardo Ziviani         return CRF_SO;
2897466a3f9cSJose Ricardo Ziviani     }
2898466a3f9cSJose Ricardo Ziviani 
2899466a3f9cSJose Ricardo Ziviani     return bcd_cmp_zero(r);
2900466a3f9cSJose Ricardo Ziviani }
2901466a3f9cSJose Ricardo Ziviani 
2902e04797f7SJose Ricardo Ziviani uint32_t helper_bcds(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2903e04797f7SJose Ricardo Ziviani {
2904e04797f7SJose Ricardo Ziviani     int cr;
2905428115c3SMark Cave-Ayland     int i = a->VsrSB(7);
2906e04797f7SJose Ricardo Ziviani     bool ox_flag = false;
2907e04797f7SJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2908e04797f7SJose Ricardo Ziviani     ppc_avr_t ret = *b;
29093c385a93SMark Cave-Ayland     ret.VsrD(1) &= ~0xf;
2910e04797f7SJose Ricardo Ziviani 
2911e04797f7SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2912e04797f7SJose Ricardo Ziviani         return CRF_SO;
2913e04797f7SJose Ricardo Ziviani     }
2914e04797f7SJose Ricardo Ziviani 
2915e04797f7SJose Ricardo Ziviani     if (unlikely(i > 31)) {
2916e04797f7SJose Ricardo Ziviani         i = 31;
2917e04797f7SJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
2918e04797f7SJose Ricardo Ziviani         i = -31;
2919e04797f7SJose Ricardo Ziviani     }
2920e04797f7SJose Ricardo Ziviani 
2921e04797f7SJose Ricardo Ziviani     if (i > 0) {
29223c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2923e04797f7SJose Ricardo Ziviani     } else {
29243c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2925e04797f7SJose Ricardo Ziviani     }
2926e04797f7SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
2927e04797f7SJose Ricardo Ziviani 
2928e04797f7SJose Ricardo Ziviani     *r = ret;
2929e04797f7SJose Ricardo Ziviani 
2930e04797f7SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
2931e04797f7SJose Ricardo Ziviani     if (ox_flag) {
2932e04797f7SJose Ricardo Ziviani         cr |= CRF_SO;
2933e04797f7SJose Ricardo Ziviani     }
2934e04797f7SJose Ricardo Ziviani 
2935e04797f7SJose Ricardo Ziviani     return cr;
2936e04797f7SJose Ricardo Ziviani }
2937e04797f7SJose Ricardo Ziviani 
2938a49a95e9SJose Ricardo Ziviani uint32_t helper_bcdus(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2939a49a95e9SJose Ricardo Ziviani {
2940a49a95e9SJose Ricardo Ziviani     int cr;
2941a49a95e9SJose Ricardo Ziviani     int i;
2942a49a95e9SJose Ricardo Ziviani     int invalid = 0;
2943a49a95e9SJose Ricardo Ziviani     bool ox_flag = false;
2944a49a95e9SJose Ricardo Ziviani     ppc_avr_t ret = *b;
2945a49a95e9SJose Ricardo Ziviani 
2946a49a95e9SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
2947a49a95e9SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
2948a49a95e9SJose Ricardo Ziviani 
2949a49a95e9SJose Ricardo Ziviani         if (unlikely(invalid)) {
2950a49a95e9SJose Ricardo Ziviani             return CRF_SO;
2951a49a95e9SJose Ricardo Ziviani         }
2952a49a95e9SJose Ricardo Ziviani     }
2953a49a95e9SJose Ricardo Ziviani 
2954428115c3SMark Cave-Ayland     i = a->VsrSB(7);
2955a49a95e9SJose Ricardo Ziviani     if (i >= 32) {
2956a49a95e9SJose Ricardo Ziviani         ox_flag = true;
29573c385a93SMark Cave-Ayland         ret.VsrD(1) = ret.VsrD(0) = 0;
2958a49a95e9SJose Ricardo Ziviani     } else if (i <= -32) {
29593c385a93SMark Cave-Ayland         ret.VsrD(1) = ret.VsrD(0) = 0;
2960a49a95e9SJose Ricardo Ziviani     } else if (i > 0) {
29613c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2962a49a95e9SJose Ricardo Ziviani     } else {
29633c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2964a49a95e9SJose Ricardo Ziviani     }
2965a49a95e9SJose Ricardo Ziviani     *r = ret;
2966a49a95e9SJose Ricardo Ziviani 
2967a49a95e9SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
2968a49a95e9SJose Ricardo Ziviani     if (ox_flag) {
2969a49a95e9SJose Ricardo Ziviani         cr |= CRF_SO;
2970a49a95e9SJose Ricardo Ziviani     }
2971a49a95e9SJose Ricardo Ziviani 
2972a49a95e9SJose Ricardo Ziviani     return cr;
2973a49a95e9SJose Ricardo Ziviani }
2974a49a95e9SJose Ricardo Ziviani 
2975a54238adSJose Ricardo Ziviani uint32_t helper_bcdsr(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2976a54238adSJose Ricardo Ziviani {
2977a54238adSJose Ricardo Ziviani     int cr;
2978a54238adSJose Ricardo Ziviani     int unused = 0;
2979a54238adSJose Ricardo Ziviani     int invalid = 0;
2980a54238adSJose Ricardo Ziviani     bool ox_flag = false;
2981a54238adSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2982a54238adSJose Ricardo Ziviani     ppc_avr_t ret = *b;
29833c385a93SMark Cave-Ayland     ret.VsrD(1) &= ~0xf;
2984a54238adSJose Ricardo Ziviani 
2985428115c3SMark Cave-Ayland     int i = a->VsrSB(7);
2986428115c3SMark Cave-Ayland     ppc_avr_t bcd_one;
2987428115c3SMark Cave-Ayland 
2988428115c3SMark Cave-Ayland     bcd_one.VsrD(0) = 0;
2989428115c3SMark Cave-Ayland     bcd_one.VsrD(1) = 0x10;
2990a54238adSJose Ricardo Ziviani 
2991a54238adSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2992a54238adSJose Ricardo Ziviani         return CRF_SO;
2993a54238adSJose Ricardo Ziviani     }
2994a54238adSJose Ricardo Ziviani 
2995a54238adSJose Ricardo Ziviani     if (unlikely(i > 31)) {
2996a54238adSJose Ricardo Ziviani         i = 31;
2997a54238adSJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
2998a54238adSJose Ricardo Ziviani         i = -31;
2999a54238adSJose Ricardo Ziviani     }
3000a54238adSJose Ricardo Ziviani 
3001a54238adSJose Ricardo Ziviani     if (i > 0) {
30023c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
3003a54238adSJose Ricardo Ziviani     } else {
30043c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
3005a54238adSJose Ricardo Ziviani 
3006a54238adSJose Ricardo Ziviani         if (bcd_get_digit(&ret, 0, &invalid) >= 5) {
3007a54238adSJose Ricardo Ziviani             bcd_add_mag(&ret, &ret, &bcd_one, &invalid, &unused);
3008a54238adSJose Ricardo Ziviani         }
3009a54238adSJose Ricardo Ziviani     }
3010a54238adSJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
3011a54238adSJose Ricardo Ziviani 
3012a54238adSJose Ricardo Ziviani     cr = bcd_cmp_zero(&ret);
3013a54238adSJose Ricardo Ziviani     if (ox_flag) {
3014a54238adSJose Ricardo Ziviani         cr |= CRF_SO;
3015a54238adSJose Ricardo Ziviani     }
3016a54238adSJose Ricardo Ziviani     *r = ret;
3017a54238adSJose Ricardo Ziviani 
3018a54238adSJose Ricardo Ziviani     return cr;
3019a54238adSJose Ricardo Ziviani }
3020a54238adSJose Ricardo Ziviani 
302131bc4d11SJose Ricardo Ziviani uint32_t helper_bcdtrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
302231bc4d11SJose Ricardo Ziviani {
302331bc4d11SJose Ricardo Ziviani     uint64_t mask;
302431bc4d11SJose Ricardo Ziviani     uint32_t ox_flag = 0;
3025428115c3SMark Cave-Ayland     int i = a->VsrSH(3) + 1;
302631bc4d11SJose Ricardo Ziviani     ppc_avr_t ret = *b;
302731bc4d11SJose Ricardo Ziviani 
302831bc4d11SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
302931bc4d11SJose Ricardo Ziviani         return CRF_SO;
303031bc4d11SJose Ricardo Ziviani     }
303131bc4d11SJose Ricardo Ziviani 
303231bc4d11SJose Ricardo Ziviani     if (i > 16 && i < 32) {
303331bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
30343c385a93SMark Cave-Ayland         if (ret.VsrD(0) & ~mask) {
303531bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
303631bc4d11SJose Ricardo Ziviani         }
303731bc4d11SJose Ricardo Ziviani 
30383c385a93SMark Cave-Ayland         ret.VsrD(0) &= mask;
303931bc4d11SJose Ricardo Ziviani     } else if (i >= 0 && i <= 16) {
304031bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
30413c385a93SMark Cave-Ayland         if (ret.VsrD(0) || (ret.VsrD(1) & ~mask)) {
304231bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
304331bc4d11SJose Ricardo Ziviani         }
304431bc4d11SJose Ricardo Ziviani 
30453c385a93SMark Cave-Ayland         ret.VsrD(1) &= mask;
30463c385a93SMark Cave-Ayland         ret.VsrD(0) = 0;
304731bc4d11SJose Ricardo Ziviani     }
304831bc4d11SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(bcd_get_sgn(b), ps), 0);
304931bc4d11SJose Ricardo Ziviani     *r = ret;
305031bc4d11SJose Ricardo Ziviani 
305131bc4d11SJose Ricardo Ziviani     return bcd_cmp_zero(&ret) | ox_flag;
305231bc4d11SJose Ricardo Ziviani }
305331bc4d11SJose Ricardo Ziviani 
30545c32e2e4SJose Ricardo Ziviani uint32_t helper_bcdutrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
30555c32e2e4SJose Ricardo Ziviani {
30565c32e2e4SJose Ricardo Ziviani     int i;
30575c32e2e4SJose Ricardo Ziviani     uint64_t mask;
30585c32e2e4SJose Ricardo Ziviani     uint32_t ox_flag = 0;
30595c32e2e4SJose Ricardo Ziviani     int invalid = 0;
30605c32e2e4SJose Ricardo Ziviani     ppc_avr_t ret = *b;
30615c32e2e4SJose Ricardo Ziviani 
30625c32e2e4SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
30635c32e2e4SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
30645c32e2e4SJose Ricardo Ziviani 
30655c32e2e4SJose Ricardo Ziviani         if (unlikely(invalid)) {
30665c32e2e4SJose Ricardo Ziviani             return CRF_SO;
30675c32e2e4SJose Ricardo Ziviani         }
30685c32e2e4SJose Ricardo Ziviani     }
30695c32e2e4SJose Ricardo Ziviani 
3070428115c3SMark Cave-Ayland     i = a->VsrSH(3);
30715c32e2e4SJose Ricardo Ziviani     if (i > 16 && i < 33) {
30725c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
30733c385a93SMark Cave-Ayland         if (ret.VsrD(0) & ~mask) {
30745c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
30755c32e2e4SJose Ricardo Ziviani         }
30765c32e2e4SJose Ricardo Ziviani 
30773c385a93SMark Cave-Ayland         ret.VsrD(0) &= mask;
30785c32e2e4SJose Ricardo Ziviani     } else if (i > 0 && i <= 16) {
30795c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
30803c385a93SMark Cave-Ayland         if (ret.VsrD(0) || (ret.VsrD(1) & ~mask)) {
30815c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
30825c32e2e4SJose Ricardo Ziviani         }
30835c32e2e4SJose Ricardo Ziviani 
30843c385a93SMark Cave-Ayland         ret.VsrD(1) &= mask;
30853c385a93SMark Cave-Ayland         ret.VsrD(0) = 0;
30865c32e2e4SJose Ricardo Ziviani     } else if (i == 0) {
30873c385a93SMark Cave-Ayland         if (ret.VsrD(0) || ret.VsrD(1)) {
30885c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
30895c32e2e4SJose Ricardo Ziviani         }
30903c385a93SMark Cave-Ayland         ret.VsrD(0) = ret.VsrD(1) = 0;
30915c32e2e4SJose Ricardo Ziviani     }
30925c32e2e4SJose Ricardo Ziviani 
30935c32e2e4SJose Ricardo Ziviani     *r = ret;
30943c385a93SMark Cave-Ayland     if (r->VsrD(0) == 0 && r->VsrD(1) == 0) {
30955c32e2e4SJose Ricardo Ziviani         return ox_flag | CRF_EQ;
30965c32e2e4SJose Ricardo Ziviani     }
30975c32e2e4SJose Ricardo Ziviani 
30985c32e2e4SJose Ricardo Ziviani     return ox_flag | CRF_GT;
30995c32e2e4SJose Ricardo Ziviani }
31005c32e2e4SJose Ricardo Ziviani 
3101fcf5ef2aSThomas Huth void helper_vsbox(ppc_avr_t *r, ppc_avr_t *a)
3102fcf5ef2aSThomas Huth {
3103fcf5ef2aSThomas Huth     int i;
3104fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
3105fcf5ef2aSThomas Huth         r->u8[i] = AES_sbox[a->u8[i]];
3106fcf5ef2aSThomas Huth     }
3107fcf5ef2aSThomas Huth }
3108fcf5ef2aSThomas Huth 
3109fcf5ef2aSThomas Huth void helper_vcipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3110fcf5ef2aSThomas Huth {
3111fcf5ef2aSThomas Huth     ppc_avr_t result;
3112fcf5ef2aSThomas Huth     int i;
3113fcf5ef2aSThomas Huth 
3114fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
31152dea57dbSMark Cave-Ayland         result.VsrW(i) = b->VsrW(i) ^
31162dea57dbSMark Cave-Ayland             (AES_Te0[a->VsrB(AES_shifts[4 * i + 0])] ^
31172dea57dbSMark Cave-Ayland              AES_Te1[a->VsrB(AES_shifts[4 * i + 1])] ^
31182dea57dbSMark Cave-Ayland              AES_Te2[a->VsrB(AES_shifts[4 * i + 2])] ^
31192dea57dbSMark Cave-Ayland              AES_Te3[a->VsrB(AES_shifts[4 * i + 3])]);
3120fcf5ef2aSThomas Huth     }
3121fcf5ef2aSThomas Huth     *r = result;
3122fcf5ef2aSThomas Huth }
3123fcf5ef2aSThomas Huth 
3124fcf5ef2aSThomas Huth void helper_vcipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3125fcf5ef2aSThomas Huth {
3126fcf5ef2aSThomas Huth     ppc_avr_t result;
3127fcf5ef2aSThomas Huth     int i;
3128fcf5ef2aSThomas Huth 
3129fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
31302dea57dbSMark Cave-Ayland         result.VsrB(i) = b->VsrB(i) ^ (AES_sbox[a->VsrB(AES_shifts[i])]);
3131fcf5ef2aSThomas Huth     }
3132fcf5ef2aSThomas Huth     *r = result;
3133fcf5ef2aSThomas Huth }
3134fcf5ef2aSThomas Huth 
3135fcf5ef2aSThomas Huth void helper_vncipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3136fcf5ef2aSThomas Huth {
3137fcf5ef2aSThomas Huth     /* This differs from what is written in ISA V2.07.  The RTL is */
3138fcf5ef2aSThomas Huth     /* incorrect and will be fixed in V2.07B.                      */
3139fcf5ef2aSThomas Huth     int i;
3140fcf5ef2aSThomas Huth     ppc_avr_t tmp;
3141fcf5ef2aSThomas Huth 
3142fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
31432dea57dbSMark Cave-Ayland         tmp.VsrB(i) = b->VsrB(i) ^ AES_isbox[a->VsrB(AES_ishifts[i])];
3144fcf5ef2aSThomas Huth     }
3145fcf5ef2aSThomas Huth 
3146fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
31472dea57dbSMark Cave-Ayland         r->VsrW(i) =
31482dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 0)][0] ^
31492dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 1)][1] ^
31502dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 2)][2] ^
31512dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 3)][3];
3152fcf5ef2aSThomas Huth     }
3153fcf5ef2aSThomas Huth }
3154fcf5ef2aSThomas Huth 
3155fcf5ef2aSThomas Huth void helper_vncipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3156fcf5ef2aSThomas Huth {
3157fcf5ef2aSThomas Huth     ppc_avr_t result;
3158fcf5ef2aSThomas Huth     int i;
3159fcf5ef2aSThomas Huth 
3160fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
31612dea57dbSMark Cave-Ayland         result.VsrB(i) = b->VsrB(i) ^ (AES_isbox[a->VsrB(AES_ishifts[i])]);
3162fcf5ef2aSThomas Huth     }
3163fcf5ef2aSThomas Huth     *r = result;
3164fcf5ef2aSThomas Huth }
3165fcf5ef2aSThomas Huth 
3166fcf5ef2aSThomas Huth void helper_vshasigmaw(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
3167fcf5ef2aSThomas Huth {
3168fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
3169fcf5ef2aSThomas Huth     int six = st_six & 0xF;
3170fcf5ef2aSThomas Huth     int i;
3171fcf5ef2aSThomas Huth 
3172730d2ca3SMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
3173fcf5ef2aSThomas Huth         if (st == 0) {
3174fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
31750ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 7) ^
31760ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 18) ^
3177730d2ca3SMark Cave-Ayland                              (a->VsrW(i) >> 3);
3178fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
31790ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 17) ^
31800ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 19) ^
3181730d2ca3SMark Cave-Ayland                              (a->VsrW(i) >> 10);
3182fcf5ef2aSThomas Huth             }
3183fcf5ef2aSThomas Huth         } else { /* st == 1 */
3184fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
31850ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 2) ^
31860ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 13) ^
31870ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 22);
3188fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
31890ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 6) ^
31900ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 11) ^
31910ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 25);
3192fcf5ef2aSThomas Huth             }
3193fcf5ef2aSThomas Huth         }
3194fcf5ef2aSThomas Huth     }
3195fcf5ef2aSThomas Huth }
3196fcf5ef2aSThomas Huth 
3197fcf5ef2aSThomas Huth void helper_vshasigmad(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
3198fcf5ef2aSThomas Huth {
3199fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
3200fcf5ef2aSThomas Huth     int six = st_six & 0xF;
3201fcf5ef2aSThomas Huth     int i;
3202fcf5ef2aSThomas Huth 
3203730d2ca3SMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
3204fcf5ef2aSThomas Huth         if (st == 0) {
3205fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2 * i))) == 0) {
32060ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 1) ^
32070ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 8) ^
3208730d2ca3SMark Cave-Ayland                              (a->VsrD(i) >> 7);
3209fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
32100ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 19) ^
32110ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 61) ^
3212730d2ca3SMark Cave-Ayland                              (a->VsrD(i) >> 6);
3213fcf5ef2aSThomas Huth             }
3214fcf5ef2aSThomas Huth         } else { /* st == 1 */
3215fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2 * i))) == 0) {
32160ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 28) ^
32170ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 34) ^
32180ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 39);
3219fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
32200ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 14) ^
32210ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 18) ^
32220ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 41);
3223fcf5ef2aSThomas Huth             }
3224fcf5ef2aSThomas Huth         }
3225fcf5ef2aSThomas Huth     }
3226fcf5ef2aSThomas Huth }
3227fcf5ef2aSThomas Huth 
3228fcf5ef2aSThomas Huth void helper_vpermxor(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
3229fcf5ef2aSThomas Huth {
3230fcf5ef2aSThomas Huth     ppc_avr_t result;
3231fcf5ef2aSThomas Huth     int i;
3232fcf5ef2aSThomas Huth 
323360594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
323460594feaSMark Cave-Ayland         int indexA = c->VsrB(i) >> 4;
323560594feaSMark Cave-Ayland         int indexB = c->VsrB(i) & 0xF;
323660594feaSMark Cave-Ayland 
323760594feaSMark Cave-Ayland         result.VsrB(i) = a->VsrB(indexA) ^ b->VsrB(indexB);
3238fcf5ef2aSThomas Huth     }
3239fcf5ef2aSThomas Huth     *r = result;
3240fcf5ef2aSThomas Huth }
3241fcf5ef2aSThomas Huth 
3242fcf5ef2aSThomas Huth #undef VECTOR_FOR_INORDER_I
3243fcf5ef2aSThomas Huth 
3244fcf5ef2aSThomas Huth /*****************************************************************************/
3245fcf5ef2aSThomas Huth /* SPE extension helpers */
3246fcf5ef2aSThomas Huth /* Use a table to make this quicker */
3247fcf5ef2aSThomas Huth static const uint8_t hbrev[16] = {
3248fcf5ef2aSThomas Huth     0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
3249fcf5ef2aSThomas Huth     0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
3250fcf5ef2aSThomas Huth };
3251fcf5ef2aSThomas Huth 
3252fcf5ef2aSThomas Huth static inline uint8_t byte_reverse(uint8_t val)
3253fcf5ef2aSThomas Huth {
3254fcf5ef2aSThomas Huth     return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
3255fcf5ef2aSThomas Huth }
3256fcf5ef2aSThomas Huth 
3257fcf5ef2aSThomas Huth static inline uint32_t word_reverse(uint32_t val)
3258fcf5ef2aSThomas Huth {
3259fcf5ef2aSThomas Huth     return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
3260fcf5ef2aSThomas Huth         (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
3261fcf5ef2aSThomas Huth }
3262fcf5ef2aSThomas Huth 
3263fcf5ef2aSThomas Huth #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
3264fcf5ef2aSThomas Huth target_ulong helper_brinc(target_ulong arg1, target_ulong arg2)
3265fcf5ef2aSThomas Huth {
3266fcf5ef2aSThomas Huth     uint32_t a, b, d, mask;
3267fcf5ef2aSThomas Huth 
3268fcf5ef2aSThomas Huth     mask = UINT32_MAX >> (32 - MASKBITS);
3269fcf5ef2aSThomas Huth     a = arg1 & mask;
3270fcf5ef2aSThomas Huth     b = arg2 & mask;
3271fcf5ef2aSThomas Huth     d = word_reverse(1 + word_reverse(a | ~b));
3272fcf5ef2aSThomas Huth     return (arg1 & ~mask) | (d & b);
3273fcf5ef2aSThomas Huth }
3274fcf5ef2aSThomas Huth 
3275fcf5ef2aSThomas Huth uint32_t helper_cntlsw32(uint32_t val)
3276fcf5ef2aSThomas Huth {
3277fcf5ef2aSThomas Huth     if (val & 0x80000000) {
3278fcf5ef2aSThomas Huth         return clz32(~val);
3279fcf5ef2aSThomas Huth     } else {
3280fcf5ef2aSThomas Huth         return clz32(val);
3281fcf5ef2aSThomas Huth     }
3282fcf5ef2aSThomas Huth }
3283fcf5ef2aSThomas Huth 
3284fcf5ef2aSThomas Huth uint32_t helper_cntlzw32(uint32_t val)
3285fcf5ef2aSThomas Huth {
3286fcf5ef2aSThomas Huth     return clz32(val);
3287fcf5ef2aSThomas Huth }
3288fcf5ef2aSThomas Huth 
3289fcf5ef2aSThomas Huth /* 440 specific */
3290fcf5ef2aSThomas Huth target_ulong helper_dlmzb(CPUPPCState *env, target_ulong high,
3291fcf5ef2aSThomas Huth                           target_ulong low, uint32_t update_Rc)
3292fcf5ef2aSThomas Huth {
3293fcf5ef2aSThomas Huth     target_ulong mask;
3294fcf5ef2aSThomas Huth     int i;
3295fcf5ef2aSThomas Huth 
3296fcf5ef2aSThomas Huth     i = 1;
3297fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
3298fcf5ef2aSThomas Huth         if ((high & mask) == 0) {
3299fcf5ef2aSThomas Huth             if (update_Rc) {
3300fcf5ef2aSThomas Huth                 env->crf[0] = 0x4;
3301fcf5ef2aSThomas Huth             }
3302fcf5ef2aSThomas Huth             goto done;
3303fcf5ef2aSThomas Huth         }
3304fcf5ef2aSThomas Huth         i++;
3305fcf5ef2aSThomas Huth     }
3306fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
3307fcf5ef2aSThomas Huth         if ((low & mask) == 0) {
3308fcf5ef2aSThomas Huth             if (update_Rc) {
3309fcf5ef2aSThomas Huth                 env->crf[0] = 0x8;
3310fcf5ef2aSThomas Huth             }
3311fcf5ef2aSThomas Huth             goto done;
3312fcf5ef2aSThomas Huth         }
3313fcf5ef2aSThomas Huth         i++;
3314fcf5ef2aSThomas Huth     }
3315fcf5ef2aSThomas Huth     i = 8;
3316fcf5ef2aSThomas Huth     if (update_Rc) {
3317fcf5ef2aSThomas Huth         env->crf[0] = 0x2;
3318fcf5ef2aSThomas Huth     }
3319fcf5ef2aSThomas Huth  done:
3320fcf5ef2aSThomas Huth     env->xer = (env->xer & ~0x7F) | i;
3321fcf5ef2aSThomas Huth     if (update_Rc) {
3322fcf5ef2aSThomas Huth         env->crf[0] |= xer_so;
3323fcf5ef2aSThomas Huth     }
3324fcf5ef2aSThomas Huth     return i;
3325fcf5ef2aSThomas Huth }
3326