xref: /openbmc/qemu/target/ppc/int_helper.c (revision cec4090d9487be9afe937b055e02a82c33e53320)
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"
248a05fd9aSRichard Henderson #include "qemu/log.h"
25fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
26fcf5ef2aSThomas Huth #include "crypto/aes.h"
277df34e48SRichard Henderson #include "crypto/aes-round.h"
28*cec4090dSRichard Henderson #include "crypto/clmul.h"
2924f91e81SAlex Bennée #include "fpu/softfloat.h"
303f74b632SRichard Henderson #include "qapi/error.h"
313f74b632SRichard Henderson #include "qemu/guest-random.h"
321015fcabSMatheus Ferst #include "tcg/tcg-gvec-desc.h"
33fcf5ef2aSThomas Huth 
34fcf5ef2aSThomas Huth #include "helper_regs.h"
35fcf5ef2aSThomas Huth /*****************************************************************************/
36fcf5ef2aSThomas Huth /* Fixed point operations helpers */
37fcf5ef2aSThomas Huth 
38f32899deSNikunj A Dadhania static inline void helper_update_ov_legacy(CPUPPCState *env, int ov)
39f32899deSNikunj A Dadhania {
40f32899deSNikunj A Dadhania     if (unlikely(ov)) {
41af721a31SVíctor Colombo         env->so = env->ov = env->ov32 = 1;
42f32899deSNikunj A Dadhania     } else {
43af721a31SVíctor Colombo         env->ov = env->ov32 = 0;
44f32899deSNikunj A Dadhania     }
45f32899deSNikunj A Dadhania }
46f32899deSNikunj A Dadhania 
47fcf5ef2aSThomas Huth target_ulong helper_divweu(CPUPPCState *env, target_ulong ra, target_ulong rb,
48fcf5ef2aSThomas Huth                            uint32_t oe)
49fcf5ef2aSThomas Huth {
50fcf5ef2aSThomas Huth     uint64_t rt = 0;
51fcf5ef2aSThomas Huth     int overflow = 0;
52fcf5ef2aSThomas Huth 
53fcf5ef2aSThomas Huth     uint64_t dividend = (uint64_t)ra << 32;
54fcf5ef2aSThomas Huth     uint64_t divisor = (uint32_t)rb;
55fcf5ef2aSThomas Huth 
56fcf5ef2aSThomas Huth     if (unlikely(divisor == 0)) {
57fcf5ef2aSThomas Huth         overflow = 1;
58fcf5ef2aSThomas Huth     } else {
59fcf5ef2aSThomas Huth         rt = dividend / divisor;
60fcf5ef2aSThomas Huth         overflow = rt > UINT32_MAX;
61fcf5ef2aSThomas Huth     }
62fcf5ef2aSThomas Huth 
63fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
64fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
65fcf5ef2aSThomas Huth     }
66fcf5ef2aSThomas Huth 
67fcf5ef2aSThomas Huth     if (oe) {
68f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
69fcf5ef2aSThomas Huth     }
70fcf5ef2aSThomas Huth 
71fcf5ef2aSThomas Huth     return (target_ulong)rt;
72fcf5ef2aSThomas Huth }
73fcf5ef2aSThomas Huth 
74fcf5ef2aSThomas Huth target_ulong helper_divwe(CPUPPCState *env, target_ulong ra, target_ulong rb,
75fcf5ef2aSThomas Huth                           uint32_t oe)
76fcf5ef2aSThomas Huth {
77fcf5ef2aSThomas Huth     int64_t rt = 0;
78fcf5ef2aSThomas Huth     int overflow = 0;
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     int64_t dividend = (int64_t)ra << 32;
81fcf5ef2aSThomas Huth     int64_t divisor = (int64_t)((int32_t)rb);
82fcf5ef2aSThomas Huth 
83fcf5ef2aSThomas Huth     if (unlikely((divisor == 0) ||
84fcf5ef2aSThomas Huth                  ((divisor == -1ull) && (dividend == INT64_MIN)))) {
85fcf5ef2aSThomas Huth         overflow = 1;
86fcf5ef2aSThomas Huth     } else {
87fcf5ef2aSThomas Huth         rt = dividend / divisor;
88fcf5ef2aSThomas Huth         overflow = rt != (int32_t)rt;
89fcf5ef2aSThomas Huth     }
90fcf5ef2aSThomas Huth 
91fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
92fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
93fcf5ef2aSThomas Huth     }
94fcf5ef2aSThomas Huth 
95fcf5ef2aSThomas Huth     if (oe) {
96f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
97fcf5ef2aSThomas Huth     }
98fcf5ef2aSThomas Huth 
99fcf5ef2aSThomas Huth     return (target_ulong)rt;
100fcf5ef2aSThomas Huth }
101fcf5ef2aSThomas Huth 
102fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
103fcf5ef2aSThomas Huth 
104fcf5ef2aSThomas Huth uint64_t helper_divdeu(CPUPPCState *env, uint64_t ra, uint64_t rb, uint32_t oe)
105fcf5ef2aSThomas Huth {
106fcf5ef2aSThomas Huth     uint64_t rt = 0;
107fcf5ef2aSThomas Huth     int overflow = 0;
108fcf5ef2aSThomas Huth 
1099276a31cSLuis Pires     if (unlikely(rb == 0 || ra >= rb)) {
1109276a31cSLuis Pires         overflow = 1;
111fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
1129276a31cSLuis Pires     } else {
1139276a31cSLuis Pires         divu128(&rt, &ra, rb);
114fcf5ef2aSThomas Huth     }
115fcf5ef2aSThomas Huth 
116fcf5ef2aSThomas Huth     if (oe) {
117f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
118fcf5ef2aSThomas Huth     }
119fcf5ef2aSThomas Huth 
120fcf5ef2aSThomas Huth     return rt;
121fcf5ef2aSThomas Huth }
122fcf5ef2aSThomas Huth 
123fcf5ef2aSThomas Huth uint64_t helper_divde(CPUPPCState *env, uint64_t rau, uint64_t rbu, uint32_t oe)
124fcf5ef2aSThomas Huth {
12540f3e79aSLuis Pires     uint64_t rt = 0;
126fcf5ef2aSThomas Huth     int64_t ra = (int64_t)rau;
127fcf5ef2aSThomas Huth     int64_t rb = (int64_t)rbu;
1289276a31cSLuis Pires     int overflow = 0;
129fcf5ef2aSThomas Huth 
1309276a31cSLuis Pires     if (unlikely(rb == 0 || uabs64(ra) >= uabs64(rb))) {
1319276a31cSLuis Pires         overflow = 1;
132fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
1339276a31cSLuis Pires     } else {
1349276a31cSLuis Pires         divs128(&rt, &ra, rb);
135fcf5ef2aSThomas Huth     }
136fcf5ef2aSThomas Huth 
137fcf5ef2aSThomas Huth     if (oe) {
138f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
139fcf5ef2aSThomas Huth     }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth     return rt;
142fcf5ef2aSThomas Huth }
143fcf5ef2aSThomas Huth 
144fcf5ef2aSThomas Huth #endif
145fcf5ef2aSThomas Huth 
146fcf5ef2aSThomas Huth 
147fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
148fcf5ef2aSThomas Huth /* if x = 0xab, returns 0xababababababababa */
149fcf5ef2aSThomas Huth #define pattern(x) (((x) & 0xff) * (~(target_ulong)0 / 0xff))
150fcf5ef2aSThomas Huth 
151b6cb41b2SDavid Gibson /*
152b6cb41b2SDavid Gibson  * subtract 1 from each byte, and with inverse, check if MSB is set at each
153fcf5ef2aSThomas Huth  * byte.
154fcf5ef2aSThomas Huth  * i.e. ((0x00 - 0x01) & ~(0x00)) & 0x80
155fcf5ef2aSThomas Huth  *      (0xFF & 0xFF) & 0x80 = 0x80 (zero found)
156fcf5ef2aSThomas Huth  */
157fcf5ef2aSThomas Huth #define haszero(v) (((v) - pattern(0x01)) & ~(v) & pattern(0x80))
158fcf5ef2aSThomas Huth 
159fcf5ef2aSThomas Huth /* When you XOR the pattern and there is a match, that byte will be zero */
160fcf5ef2aSThomas Huth #define hasvalue(x, n)  (haszero((x) ^ pattern(n)))
161fcf5ef2aSThomas Huth 
162fcf5ef2aSThomas Huth uint32_t helper_cmpeqb(target_ulong ra, target_ulong rb)
163fcf5ef2aSThomas Huth {
164efa73196SNikunj A Dadhania     return hasvalue(rb, ra) ? CRF_GT : 0;
165fcf5ef2aSThomas Huth }
166fcf5ef2aSThomas Huth 
167fcf5ef2aSThomas Huth #undef pattern
168fcf5ef2aSThomas Huth #undef haszero
169fcf5ef2aSThomas Huth #undef hasvalue
170fcf5ef2aSThomas Huth 
171b6cb41b2SDavid Gibson /*
1723f74b632SRichard Henderson  * Return a random number.
173fcf5ef2aSThomas Huth  */
1743f74b632SRichard Henderson uint64_t helper_darn32(void)
175fcf5ef2aSThomas Huth {
1763f74b632SRichard Henderson     Error *err = NULL;
1773f74b632SRichard Henderson     uint32_t ret;
1783f74b632SRichard Henderson 
1793f74b632SRichard Henderson     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
1803f74b632SRichard Henderson         qemu_log_mask(LOG_UNIMP, "darn: Crypto failure: %s",
1813f74b632SRichard Henderson                       error_get_pretty(err));
1823f74b632SRichard Henderson         error_free(err);
183fcf5ef2aSThomas Huth         return -1;
184fcf5ef2aSThomas Huth     }
185fcf5ef2aSThomas Huth 
1863f74b632SRichard Henderson     return ret;
1873f74b632SRichard Henderson }
1883f74b632SRichard Henderson 
1893f74b632SRichard Henderson uint64_t helper_darn64(void)
190fcf5ef2aSThomas Huth {
1913f74b632SRichard Henderson     Error *err = NULL;
1923f74b632SRichard Henderson     uint64_t ret;
1933f74b632SRichard Henderson 
1943f74b632SRichard Henderson     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
1953f74b632SRichard Henderson         qemu_log_mask(LOG_UNIMP, "darn: Crypto failure: %s",
1963f74b632SRichard Henderson                       error_get_pretty(err));
1973f74b632SRichard Henderson         error_free(err);
198fcf5ef2aSThomas Huth         return -1;
199fcf5ef2aSThomas Huth     }
200fcf5ef2aSThomas Huth 
2013f74b632SRichard Henderson     return ret;
2023f74b632SRichard Henderson }
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth uint64_t helper_bpermd(uint64_t rs, uint64_t rb)
205fcf5ef2aSThomas Huth {
206fcf5ef2aSThomas Huth     int i;
207fcf5ef2aSThomas Huth     uint64_t ra = 0;
208fcf5ef2aSThomas Huth 
209fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
210fcf5ef2aSThomas Huth         int index = (rs >> (i * 8)) & 0xFF;
211fcf5ef2aSThomas Huth         if (index < 64) {
212a6a444a8SCédric Le Goater             if (rb & PPC_BIT(index)) {
213fcf5ef2aSThomas Huth                 ra |= 1 << i;
214fcf5ef2aSThomas Huth             }
215fcf5ef2aSThomas Huth         }
216fcf5ef2aSThomas Huth     }
217fcf5ef2aSThomas Huth     return ra;
218fcf5ef2aSThomas Huth }
219fcf5ef2aSThomas Huth 
220fcf5ef2aSThomas Huth #endif
221fcf5ef2aSThomas Huth 
222fcf5ef2aSThomas Huth target_ulong helper_cmpb(target_ulong rs, target_ulong rb)
223fcf5ef2aSThomas Huth {
224fcf5ef2aSThomas Huth     target_ulong mask = 0xff;
225fcf5ef2aSThomas Huth     target_ulong ra = 0;
226fcf5ef2aSThomas Huth     int i;
227fcf5ef2aSThomas Huth 
228fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong); i++) {
229fcf5ef2aSThomas Huth         if ((rs & mask) == (rb & mask)) {
230fcf5ef2aSThomas Huth             ra |= mask;
231fcf5ef2aSThomas Huth         }
232fcf5ef2aSThomas Huth         mask <<= 8;
233fcf5ef2aSThomas Huth     }
234fcf5ef2aSThomas Huth     return ra;
235fcf5ef2aSThomas Huth }
236fcf5ef2aSThomas Huth 
237fcf5ef2aSThomas Huth /* shift right arithmetic helper */
238fcf5ef2aSThomas Huth target_ulong helper_sraw(CPUPPCState *env, target_ulong value,
239fcf5ef2aSThomas Huth                          target_ulong shift)
240fcf5ef2aSThomas Huth {
241fcf5ef2aSThomas Huth     int32_t ret;
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth     if (likely(!(shift & 0x20))) {
244fcf5ef2aSThomas Huth         if (likely((uint32_t)shift != 0)) {
245fcf5ef2aSThomas Huth             shift &= 0x1f;
246fcf5ef2aSThomas Huth             ret = (int32_t)value >> shift;
247fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
248af1c259fSSandipan Das                 env->ca32 = env->ca = 0;
249fcf5ef2aSThomas Huth             } else {
250af1c259fSSandipan Das                 env->ca32 = env->ca = 1;
251fcf5ef2aSThomas Huth             }
252fcf5ef2aSThomas Huth         } else {
253fcf5ef2aSThomas Huth             ret = (int32_t)value;
254af1c259fSSandipan Das             env->ca32 = env->ca = 0;
255fcf5ef2aSThomas Huth         }
256fcf5ef2aSThomas Huth     } else {
257fcf5ef2aSThomas Huth         ret = (int32_t)value >> 31;
258af1c259fSSandipan Das         env->ca32 = env->ca = (ret != 0);
259fcf5ef2aSThomas Huth     }
260fcf5ef2aSThomas Huth     return (target_long)ret;
261fcf5ef2aSThomas Huth }
262fcf5ef2aSThomas Huth 
263fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
264fcf5ef2aSThomas Huth target_ulong helper_srad(CPUPPCState *env, target_ulong value,
265fcf5ef2aSThomas Huth                          target_ulong shift)
266fcf5ef2aSThomas Huth {
267fcf5ef2aSThomas Huth     int64_t ret;
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth     if (likely(!(shift & 0x40))) {
270fcf5ef2aSThomas Huth         if (likely((uint64_t)shift != 0)) {
271fcf5ef2aSThomas Huth             shift &= 0x3f;
272fcf5ef2aSThomas Huth             ret = (int64_t)value >> shift;
273fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1ULL << shift) - 1)) == 0)) {
274af1c259fSSandipan Das                 env->ca32 = env->ca = 0;
275fcf5ef2aSThomas Huth             } else {
276af1c259fSSandipan Das                 env->ca32 = env->ca = 1;
277fcf5ef2aSThomas Huth             }
278fcf5ef2aSThomas Huth         } else {
279fcf5ef2aSThomas Huth             ret = (int64_t)value;
280af1c259fSSandipan Das             env->ca32 = env->ca = 0;
281fcf5ef2aSThomas Huth         }
282fcf5ef2aSThomas Huth     } else {
283fcf5ef2aSThomas Huth         ret = (int64_t)value >> 63;
284af1c259fSSandipan Das         env->ca32 = env->ca = (ret != 0);
285fcf5ef2aSThomas Huth     }
286fcf5ef2aSThomas Huth     return ret;
287fcf5ef2aSThomas Huth }
288fcf5ef2aSThomas Huth #endif
289fcf5ef2aSThomas Huth 
290fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
291fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
292fcf5ef2aSThomas Huth {
29379770002SRichard Henderson     /* Note that we don't fold past bytes */
294fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
295fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
296fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
297fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
298fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
299fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
300fcf5ef2aSThomas Huth     return val;
301fcf5ef2aSThomas Huth }
302fcf5ef2aSThomas Huth 
303fcf5ef2aSThomas Huth target_ulong helper_popcntw(target_ulong val)
304fcf5ef2aSThomas Huth {
30579770002SRichard Henderson     /* Note that we don't fold past words.  */
306fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
307fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
308fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
309fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
310fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
311fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
312fcf5ef2aSThomas Huth     val = (val & 0x00ff00ff00ff00ffULL) + ((val >>  8) &
313fcf5ef2aSThomas Huth                                            0x00ff00ff00ff00ffULL);
314fcf5ef2aSThomas Huth     val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) &
315fcf5ef2aSThomas Huth                                            0x0000ffff0000ffffULL);
316fcf5ef2aSThomas Huth     return val;
317fcf5ef2aSThomas Huth }
318fcf5ef2aSThomas Huth #else
319fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
320fcf5ef2aSThomas Huth {
32179770002SRichard Henderson     /* Note that we don't fold past bytes */
322fcf5ef2aSThomas Huth     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
323fcf5ef2aSThomas Huth     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
324fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
325fcf5ef2aSThomas Huth     return val;
326fcf5ef2aSThomas Huth }
327fcf5ef2aSThomas Huth #endif
328fcf5ef2aSThomas Huth 
3296e0bbc40SMatheus Ferst uint64_t helper_CFUGED(uint64_t src, uint64_t mask)
33089ccd7dcSMatheus Ferst {
33189ccd7dcSMatheus Ferst     /*
33289ccd7dcSMatheus Ferst      * Instead of processing the mask bit-by-bit from the most significant to
33389ccd7dcSMatheus Ferst      * the least significant bit, as described in PowerISA, we'll handle it in
33489ccd7dcSMatheus Ferst      * blocks of 'n' zeros/ones from LSB to MSB. To avoid the decision to use
33589ccd7dcSMatheus Ferst      * ctz or cto, we negate the mask at the end of the loop.
33689ccd7dcSMatheus Ferst      */
33789ccd7dcSMatheus Ferst     target_ulong m, left = 0, right = 0;
33889ccd7dcSMatheus Ferst     unsigned int n, i = 64;
33989ccd7dcSMatheus Ferst     bool bit = false; /* tracks if we are processing zeros or ones */
34089ccd7dcSMatheus Ferst 
34189ccd7dcSMatheus Ferst     if (mask == 0 || mask == -1) {
34289ccd7dcSMatheus Ferst         return src;
34389ccd7dcSMatheus Ferst     }
34489ccd7dcSMatheus Ferst 
34589ccd7dcSMatheus Ferst     /* Processes the mask in blocks, from LSB to MSB */
34689ccd7dcSMatheus Ferst     while (i) {
34789ccd7dcSMatheus Ferst         /* Find how many bits we should take */
34889ccd7dcSMatheus Ferst         n = ctz64(mask);
34989ccd7dcSMatheus Ferst         if (n > i) {
35089ccd7dcSMatheus Ferst             n = i;
35189ccd7dcSMatheus Ferst         }
35289ccd7dcSMatheus Ferst 
35389ccd7dcSMatheus Ferst         /*
35489ccd7dcSMatheus Ferst          * Extracts 'n' trailing bits of src and put them on the leading 'n'
35589ccd7dcSMatheus Ferst          * bits of 'right' or 'left', pushing down the previously extracted
35689ccd7dcSMatheus Ferst          * values.
35789ccd7dcSMatheus Ferst          */
35889ccd7dcSMatheus Ferst         m = (1ll << n) - 1;
35989ccd7dcSMatheus Ferst         if (bit) {
36089ccd7dcSMatheus Ferst             right = ror64(right | (src & m), n);
36189ccd7dcSMatheus Ferst         } else {
36289ccd7dcSMatheus Ferst             left = ror64(left | (src & m), n);
36389ccd7dcSMatheus Ferst         }
36489ccd7dcSMatheus Ferst 
36589ccd7dcSMatheus Ferst         /*
36689ccd7dcSMatheus Ferst          * Discards the processed bits from 'src' and 'mask'. Note that we are
36789ccd7dcSMatheus Ferst          * removing 'n' trailing zeros from 'mask', but the logical shift will
36889ccd7dcSMatheus Ferst          * add 'n' leading zeros back, so the population count of 'mask' is kept
36989ccd7dcSMatheus Ferst          * the same.
37089ccd7dcSMatheus Ferst          */
37189ccd7dcSMatheus Ferst         src >>= n;
37289ccd7dcSMatheus Ferst         mask >>= n;
37389ccd7dcSMatheus Ferst         i -= n;
37489ccd7dcSMatheus Ferst         bit = !bit;
37589ccd7dcSMatheus Ferst         mask = ~mask;
37689ccd7dcSMatheus Ferst     }
37789ccd7dcSMatheus Ferst 
37889ccd7dcSMatheus Ferst     /*
37989ccd7dcSMatheus Ferst      * At the end, right was ror'ed ctpop(mask) times. To put it back in place,
38089ccd7dcSMatheus Ferst      * we'll shift it more 64-ctpop(mask) times.
38189ccd7dcSMatheus Ferst      */
38289ccd7dcSMatheus Ferst     if (bit) {
38389ccd7dcSMatheus Ferst         n = ctpop64(mask);
38489ccd7dcSMatheus Ferst     } else {
38589ccd7dcSMatheus Ferst         n = 64 - ctpop64(mask);
38689ccd7dcSMatheus Ferst     }
38789ccd7dcSMatheus Ferst 
38889ccd7dcSMatheus Ferst     return left | (right >> n);
38989ccd7dcSMatheus Ferst }
39089ccd7dcSMatheus Ferst 
39121ba6e58SMatheus Ferst uint64_t helper_PDEPD(uint64_t src, uint64_t mask)
39221ba6e58SMatheus Ferst {
39321ba6e58SMatheus Ferst     int i, o;
39421ba6e58SMatheus Ferst     uint64_t result = 0;
39521ba6e58SMatheus Ferst 
39621ba6e58SMatheus Ferst     if (mask == -1) {
39721ba6e58SMatheus Ferst         return src;
39821ba6e58SMatheus Ferst     }
39921ba6e58SMatheus Ferst 
40021ba6e58SMatheus Ferst     for (i = 0; mask != 0; i++) {
40121ba6e58SMatheus Ferst         o = ctz64(mask);
40221ba6e58SMatheus Ferst         mask &= mask - 1;
40321ba6e58SMatheus Ferst         result |= ((src >> i) & 1) << o;
40421ba6e58SMatheus Ferst     }
40521ba6e58SMatheus Ferst 
40621ba6e58SMatheus Ferst     return result;
40721ba6e58SMatheus Ferst }
4088bdb7606SMatheus Ferst 
4098bdb7606SMatheus Ferst uint64_t helper_PEXTD(uint64_t src, uint64_t mask)
4108bdb7606SMatheus Ferst {
4118bdb7606SMatheus Ferst     int i, o;
4128bdb7606SMatheus Ferst     uint64_t result = 0;
4138bdb7606SMatheus Ferst 
4148bdb7606SMatheus Ferst     if (mask == -1) {
4158bdb7606SMatheus Ferst         return src;
4168bdb7606SMatheus Ferst     }
4178bdb7606SMatheus Ferst 
4188bdb7606SMatheus Ferst     for (o = 0; mask != 0; o++) {
4198bdb7606SMatheus Ferst         i = ctz64(mask);
4208bdb7606SMatheus Ferst         mask &= mask - 1;
4218bdb7606SMatheus Ferst         result |= ((src >> i) & 1) << o;
4228bdb7606SMatheus Ferst     }
4238bdb7606SMatheus Ferst 
4248bdb7606SMatheus Ferst     return result;
4258bdb7606SMatheus Ferst }
42621ba6e58SMatheus Ferst 
427fcf5ef2aSThomas Huth /*****************************************************************************/
428fcf5ef2aSThomas Huth /* Altivec extension helpers */
429e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
430fcf5ef2aSThomas Huth #define VECTOR_FOR_INORDER_I(index, element)                    \
431fcf5ef2aSThomas Huth     for (index = 0; index < ARRAY_SIZE(r->element); index++)
432fcf5ef2aSThomas Huth #else
433fcf5ef2aSThomas Huth #define VECTOR_FOR_INORDER_I(index, element)                    \
434fcf5ef2aSThomas Huth     for (index = ARRAY_SIZE(r->element) - 1; index >= 0; index--)
435fcf5ef2aSThomas Huth #endif
436fcf5ef2aSThomas Huth 
437fcf5ef2aSThomas Huth /* Saturating arithmetic helpers.  */
438fcf5ef2aSThomas Huth #define SATCVT(from, to, from_type, to_type, min, max)          \
439fcf5ef2aSThomas Huth     static inline to_type cvt##from##to(from_type x, int *sat)  \
440fcf5ef2aSThomas Huth     {                                                           \
441fcf5ef2aSThomas Huth         to_type r;                                              \
442fcf5ef2aSThomas Huth                                                                 \
443fcf5ef2aSThomas Huth         if (x < (from_type)min) {                               \
444fcf5ef2aSThomas Huth             r = min;                                            \
445fcf5ef2aSThomas Huth             *sat = 1;                                           \
446fcf5ef2aSThomas Huth         } else if (x > (from_type)max) {                        \
447fcf5ef2aSThomas Huth             r = max;                                            \
448fcf5ef2aSThomas Huth             *sat = 1;                                           \
449fcf5ef2aSThomas Huth         } else {                                                \
450fcf5ef2aSThomas Huth             r = x;                                              \
451fcf5ef2aSThomas Huth         }                                                       \
452fcf5ef2aSThomas Huth         return r;                                               \
453fcf5ef2aSThomas Huth     }
454fcf5ef2aSThomas Huth #define SATCVTU(from, to, from_type, to_type, min, max)         \
455fcf5ef2aSThomas Huth     static inline to_type cvt##from##to(from_type x, int *sat)  \
456fcf5ef2aSThomas Huth     {                                                           \
457fcf5ef2aSThomas Huth         to_type r;                                              \
458fcf5ef2aSThomas Huth                                                                 \
459fcf5ef2aSThomas Huth         if (x > (from_type)max) {                               \
460fcf5ef2aSThomas Huth             r = max;                                            \
461fcf5ef2aSThomas Huth             *sat = 1;                                           \
462fcf5ef2aSThomas Huth         } else {                                                \
463fcf5ef2aSThomas Huth             r = x;                                              \
464fcf5ef2aSThomas Huth         }                                                       \
465fcf5ef2aSThomas Huth         return r;                                               \
466fcf5ef2aSThomas Huth     }
467fcf5ef2aSThomas Huth SATCVT(sh, sb, int16_t, int8_t, INT8_MIN, INT8_MAX)
468fcf5ef2aSThomas Huth SATCVT(sw, sh, int32_t, int16_t, INT16_MIN, INT16_MAX)
469fcf5ef2aSThomas Huth SATCVT(sd, sw, int64_t, int32_t, INT32_MIN, INT32_MAX)
470fcf5ef2aSThomas Huth 
471fcf5ef2aSThomas Huth SATCVTU(uh, ub, uint16_t, uint8_t, 0, UINT8_MAX)
472fcf5ef2aSThomas Huth SATCVTU(uw, uh, uint32_t, uint16_t, 0, UINT16_MAX)
473fcf5ef2aSThomas Huth SATCVTU(ud, uw, uint64_t, uint32_t, 0, UINT32_MAX)
474fcf5ef2aSThomas Huth SATCVT(sh, ub, int16_t, uint8_t, 0, UINT8_MAX)
475fcf5ef2aSThomas Huth SATCVT(sw, uh, int32_t, uint16_t, 0, UINT16_MAX)
476fcf5ef2aSThomas Huth SATCVT(sd, uw, int64_t, uint32_t, 0, UINT32_MAX)
477fcf5ef2aSThomas Huth #undef SATCVT
478fcf5ef2aSThomas Huth #undef SATCVTU
479fcf5ef2aSThomas Huth 
480dedfaac7SRichard Henderson void helper_mtvscr(CPUPPCState *env, uint32_t vscr)
481fcf5ef2aSThomas Huth {
482c19940dbSBruno Larsen (billionai)     ppc_store_vscr(env, vscr);
483fcf5ef2aSThomas Huth }
484fcf5ef2aSThomas Huth 
485cc2b90d7SRichard Henderson uint32_t helper_mfvscr(CPUPPCState *env)
486cc2b90d7SRichard Henderson {
487c19940dbSBruno Larsen (billionai)     return ppc_get_vscr(env);
488cc2b90d7SRichard Henderson }
489cc2b90d7SRichard Henderson 
4906175f5a0SRichard Henderson static inline void set_vscr_sat(CPUPPCState *env)
4916175f5a0SRichard Henderson {
4929b5b74daSRichard Henderson     /* The choice of non-zero value is arbitrary.  */
4939b5b74daSRichard Henderson     env->vscr_sat.u32[0] = 1;
4946175f5a0SRichard Henderson }
4956175f5a0SRichard Henderson 
496fcf5ef2aSThomas Huth /* vprtybq */
497d57fbd8fSLucas Mateus Castro (alqotel) void helper_VPRTYBQ(ppc_avr_t *r, ppc_avr_t *b, uint32_t v)
498fcf5ef2aSThomas Huth {
499fcf5ef2aSThomas Huth     uint64_t res = b->u64[0] ^ b->u64[1];
500fcf5ef2aSThomas Huth     res ^= res >> 32;
501fcf5ef2aSThomas Huth     res ^= res >> 16;
502fcf5ef2aSThomas Huth     res ^= res >> 8;
5033c385a93SMark Cave-Ayland     r->VsrD(1) = res & 1;
5043c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
505fcf5ef2aSThomas Huth }
506fcf5ef2aSThomas Huth 
507fcf5ef2aSThomas Huth #define VARITHFP(suffix, func)                                          \
508fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
509fcf5ef2aSThomas Huth                           ppc_avr_t *b)                                 \
510fcf5ef2aSThomas Huth     {                                                                   \
511fcf5ef2aSThomas Huth         int i;                                                          \
512fcf5ef2aSThomas Huth                                                                         \
51305ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
51405ee3e8aSMark Cave-Ayland             r->f32[i] = func(a->f32[i], b->f32[i], &env->vec_status);   \
515fcf5ef2aSThomas Huth         }                                                               \
516fcf5ef2aSThomas Huth     }
517fcf5ef2aSThomas Huth VARITHFP(addfp, float32_add)
518fcf5ef2aSThomas Huth VARITHFP(subfp, float32_sub)
519fcf5ef2aSThomas Huth VARITHFP(minfp, float32_min)
520fcf5ef2aSThomas Huth VARITHFP(maxfp, float32_max)
521fcf5ef2aSThomas Huth #undef VARITHFP
522fcf5ef2aSThomas Huth 
523fcf5ef2aSThomas Huth #define VARITHFPFMA(suffix, type)                                       \
524fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
525fcf5ef2aSThomas Huth                            ppc_avr_t *b, ppc_avr_t *c)                  \
526fcf5ef2aSThomas Huth     {                                                                   \
527fcf5ef2aSThomas Huth         int i;                                                          \
52805ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
52905ee3e8aSMark Cave-Ayland             r->f32[i] = float32_muladd(a->f32[i], c->f32[i], b->f32[i], \
530fcf5ef2aSThomas Huth                                        type, &env->vec_status);         \
531fcf5ef2aSThomas Huth         }                                                               \
532fcf5ef2aSThomas Huth     }
533fcf5ef2aSThomas Huth VARITHFPFMA(maddfp, 0);
534fcf5ef2aSThomas Huth VARITHFPFMA(nmsubfp, float_muladd_negate_result | float_muladd_negate_c);
535fcf5ef2aSThomas Huth #undef VARITHFPFMA
536fcf5ef2aSThomas Huth 
537fcf5ef2aSThomas Huth #define VARITHSAT_CASE(type, op, cvt, element)                          \
538fcf5ef2aSThomas Huth     {                                                                   \
539fcf5ef2aSThomas Huth         type result = (type)a->element[i] op (type)b->element[i];       \
540fcf5ef2aSThomas Huth         r->element[i] = cvt(result, &sat);                              \
541fcf5ef2aSThomas Huth     }
542fcf5ef2aSThomas Huth 
543fcf5ef2aSThomas Huth #define VARITHSAT_DO(name, op, optype, cvt, element)                    \
544fb11ae7dSRichard Henderson     void helper_v##name(ppc_avr_t *r, ppc_avr_t *vscr_sat,              \
545fb11ae7dSRichard Henderson                         ppc_avr_t *a, ppc_avr_t *b, uint32_t desc)      \
546fcf5ef2aSThomas Huth     {                                                                   \
547fcf5ef2aSThomas Huth         int sat = 0;                                                    \
548fcf5ef2aSThomas Huth         int i;                                                          \
549fcf5ef2aSThomas Huth                                                                         \
550fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
551fcf5ef2aSThomas Huth             VARITHSAT_CASE(optype, op, cvt, element);                   \
552fcf5ef2aSThomas Huth         }                                                               \
553fcf5ef2aSThomas Huth         if (sat) {                                                      \
554fb11ae7dSRichard Henderson             vscr_sat->u32[0] = 1;                                       \
555fcf5ef2aSThomas Huth         }                                                               \
556fcf5ef2aSThomas Huth     }
557fcf5ef2aSThomas Huth #define VARITHSAT_SIGNED(suffix, element, optype, cvt)          \
558fcf5ef2aSThomas Huth     VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element)      \
559fcf5ef2aSThomas Huth     VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
560fcf5ef2aSThomas Huth #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt)        \
561fcf5ef2aSThomas Huth     VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element)      \
562fcf5ef2aSThomas Huth     VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
563fcf5ef2aSThomas Huth VARITHSAT_SIGNED(b, s8, int16_t, cvtshsb)
564fcf5ef2aSThomas Huth VARITHSAT_SIGNED(h, s16, int32_t, cvtswsh)
565fcf5ef2aSThomas Huth VARITHSAT_SIGNED(w, s32, int64_t, cvtsdsw)
566fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(b, u8, uint16_t, cvtshub)
567fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(h, u16, uint32_t, cvtswuh)
568fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(w, u32, uint64_t, cvtsduw)
569fcf5ef2aSThomas Huth #undef VARITHSAT_CASE
570fcf5ef2aSThomas Huth #undef VARITHSAT_DO
571fcf5ef2aSThomas Huth #undef VARITHSAT_SIGNED
572fcf5ef2aSThomas Huth #undef VARITHSAT_UNSIGNED
573fcf5ef2aSThomas Huth 
574c85929b2SLucas Mateus Castro (alqotel) #define VAVG(name, element, etype)                                          \
575c85929b2SLucas Mateus Castro (alqotel)     void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t v)\
576fcf5ef2aSThomas Huth     {                                                                       \
577fcf5ef2aSThomas Huth         int i;                                                              \
578fcf5ef2aSThomas Huth                                                                             \
579fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
580fcf5ef2aSThomas Huth             etype x = (etype)a->element[i] + (etype)b->element[i] + 1;      \
581fcf5ef2aSThomas Huth             r->element[i] = x >> 1;                                         \
582fcf5ef2aSThomas Huth         }                                                                   \
583fcf5ef2aSThomas Huth     }
584fcf5ef2aSThomas Huth 
585c85929b2SLucas Mateus Castro (alqotel) VAVG(VAVGSB, s8, int16_t)
586c85929b2SLucas Mateus Castro (alqotel) VAVG(VAVGUB, u8, uint16_t)
587c85929b2SLucas Mateus Castro (alqotel) VAVG(VAVGSH, s16, int32_t)
588c85929b2SLucas Mateus Castro (alqotel) VAVG(VAVGUH, u16, uint32_t)
589c85929b2SLucas Mateus Castro (alqotel) VAVG(VAVGSW, s32, int64_t)
590c85929b2SLucas Mateus Castro (alqotel) VAVG(VAVGUW, u32, uint64_t)
591fcf5ef2aSThomas Huth #undef VAVG
592fcf5ef2aSThomas Huth 
59326c964f8SLucas Mateus Castro (alqotel) #define VABSDU(name, element)                                           \
59426c964f8SLucas Mateus Castro (alqotel) void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t v)\
595fcf5ef2aSThomas Huth {                                                                       \
596fcf5ef2aSThomas Huth     int i;                                                              \
597fcf5ef2aSThomas Huth                                                                         \
598fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
599fcf5ef2aSThomas Huth         r->element[i] = (a->element[i] > b->element[i]) ?               \
600fcf5ef2aSThomas Huth             (a->element[i] - b->element[i]) :                           \
601fcf5ef2aSThomas Huth             (b->element[i] - a->element[i]);                            \
602fcf5ef2aSThomas Huth     }                                                                   \
603fcf5ef2aSThomas Huth }
604fcf5ef2aSThomas Huth 
605b6cb41b2SDavid Gibson /*
606b6cb41b2SDavid Gibson  * VABSDU - Vector absolute difference unsigned
607fcf5ef2aSThomas Huth  *   name    - instruction mnemonic suffix (b: byte, h: halfword, w: word)
608fcf5ef2aSThomas Huth  *   element - element type to access from vector
609fcf5ef2aSThomas Huth  */
61026c964f8SLucas Mateus Castro (alqotel) VABSDU(VABSDUB, u8)
61126c964f8SLucas Mateus Castro (alqotel) VABSDU(VABSDUH, u16)
61226c964f8SLucas Mateus Castro (alqotel) VABSDU(VABSDUW, u32)
613fcf5ef2aSThomas Huth #undef VABSDU
614fcf5ef2aSThomas Huth 
615fcf5ef2aSThomas Huth #define VCF(suffix, cvt, element)                                       \
616fcf5ef2aSThomas Huth     void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r,             \
617fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
618fcf5ef2aSThomas Huth     {                                                                   \
619fcf5ef2aSThomas Huth         int i;                                                          \
620fcf5ef2aSThomas Huth                                                                         \
62105ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
622fcf5ef2aSThomas Huth             float32 t = cvt(b->element[i], &env->vec_status);           \
62305ee3e8aSMark Cave-Ayland             r->f32[i] = float32_scalbn(t, -uim, &env->vec_status);      \
624fcf5ef2aSThomas Huth         }                                                               \
625fcf5ef2aSThomas Huth     }
626fcf5ef2aSThomas Huth VCF(ux, uint32_to_float32, u32)
627fcf5ef2aSThomas Huth VCF(sx, int32_to_float32, s32)
628fcf5ef2aSThomas Huth #undef VCF
629fcf5ef2aSThomas Huth 
630eb936dc0SMatheus Ferst #define VCMPNEZ(NAME, ELEM) \
631eb936dc0SMatheus Ferst void helper_##NAME(ppc_vsr_t *t, ppc_vsr_t *a, ppc_vsr_t *b, uint32_t desc) \
632fcf5ef2aSThomas Huth {                                                                           \
633eb936dc0SMatheus Ferst     for (int i = 0; i < ARRAY_SIZE(t->ELEM); i++) {                         \
634eb936dc0SMatheus Ferst         t->ELEM[i] = ((a->ELEM[i] == 0) || (b->ELEM[i] == 0) ||             \
635eb936dc0SMatheus Ferst                       (a->ELEM[i] != b->ELEM[i])) ? -1 : 0;                 \
636fcf5ef2aSThomas Huth     }                                                                       \
637fcf5ef2aSThomas Huth }
638eb936dc0SMatheus Ferst VCMPNEZ(VCMPNEZB, u8)
639eb936dc0SMatheus Ferst VCMPNEZ(VCMPNEZH, u16)
640eb936dc0SMatheus Ferst VCMPNEZ(VCMPNEZW, u32)
641eb936dc0SMatheus Ferst #undef VCMPNEZ
642fcf5ef2aSThomas Huth 
643fcf5ef2aSThomas Huth #define VCMPFP_DO(suffix, compare, order, record)                       \
644fcf5ef2aSThomas Huth     void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r,            \
645fcf5ef2aSThomas Huth                              ppc_avr_t *a, ppc_avr_t *b)                \
646fcf5ef2aSThomas Huth     {                                                                   \
647fcf5ef2aSThomas Huth         uint32_t ones = (uint32_t)-1;                                   \
648fcf5ef2aSThomas Huth         uint32_t all = ones;                                            \
649fcf5ef2aSThomas Huth         uint32_t none = 0;                                              \
650fcf5ef2aSThomas Huth         int i;                                                          \
651fcf5ef2aSThomas Huth                                                                         \
65205ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
653fcf5ef2aSThomas Huth             uint32_t result;                                            \
65471bfd65cSRichard Henderson             FloatRelation rel =                                         \
65571bfd65cSRichard Henderson                 float32_compare_quiet(a->f32[i], b->f32[i],             \
656fcf5ef2aSThomas Huth                                       &env->vec_status);                \
657fcf5ef2aSThomas Huth             if (rel == float_relation_unordered) {                      \
658fcf5ef2aSThomas Huth                 result = 0;                                             \
659fcf5ef2aSThomas Huth             } else if (rel compare order) {                             \
660fcf5ef2aSThomas Huth                 result = ones;                                          \
661fcf5ef2aSThomas Huth             } else {                                                    \
662fcf5ef2aSThomas Huth                 result = 0;                                             \
663fcf5ef2aSThomas Huth             }                                                           \
664fcf5ef2aSThomas Huth             r->u32[i] = result;                                         \
665fcf5ef2aSThomas Huth             all &= result;                                              \
666fcf5ef2aSThomas Huth             none |= result;                                             \
667fcf5ef2aSThomas Huth         }                                                               \
668fcf5ef2aSThomas Huth         if (record) {                                                   \
669fcf5ef2aSThomas Huth             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
670fcf5ef2aSThomas Huth         }                                                               \
671fcf5ef2aSThomas Huth     }
672fcf5ef2aSThomas Huth #define VCMPFP(suffix, compare, order)          \
673fcf5ef2aSThomas Huth     VCMPFP_DO(suffix, compare, order, 0)        \
674fcf5ef2aSThomas Huth     VCMPFP_DO(suffix##_dot, compare, order, 1)
675fcf5ef2aSThomas Huth VCMPFP(eqfp, ==, float_relation_equal)
676fcf5ef2aSThomas Huth VCMPFP(gefp, !=, float_relation_less)
677fcf5ef2aSThomas Huth VCMPFP(gtfp, ==, float_relation_greater)
678fcf5ef2aSThomas Huth #undef VCMPFP_DO
679fcf5ef2aSThomas Huth #undef VCMPFP
680fcf5ef2aSThomas Huth 
681fcf5ef2aSThomas Huth static inline void vcmpbfp_internal(CPUPPCState *env, ppc_avr_t *r,
682fcf5ef2aSThomas Huth                                     ppc_avr_t *a, ppc_avr_t *b, int record)
683fcf5ef2aSThomas Huth {
684fcf5ef2aSThomas Huth     int i;
685fcf5ef2aSThomas Huth     int all_in = 0;
686fcf5ef2aSThomas Huth 
68705ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
68871bfd65cSRichard Henderson         FloatRelation le_rel = float32_compare_quiet(a->f32[i], b->f32[i],
68905ee3e8aSMark Cave-Ayland                                                      &env->vec_status);
690fcf5ef2aSThomas Huth         if (le_rel == float_relation_unordered) {
691fcf5ef2aSThomas Huth             r->u32[i] = 0xc0000000;
692fcf5ef2aSThomas Huth             all_in = 1;
693fcf5ef2aSThomas Huth         } else {
69405ee3e8aSMark Cave-Ayland             float32 bneg = float32_chs(b->f32[i]);
69571bfd65cSRichard Henderson             FloatRelation ge_rel = float32_compare_quiet(a->f32[i], bneg,
69605ee3e8aSMark Cave-Ayland                                                          &env->vec_status);
697fcf5ef2aSThomas Huth             int le = le_rel != float_relation_greater;
698fcf5ef2aSThomas Huth             int ge = ge_rel != float_relation_less;
699fcf5ef2aSThomas Huth 
700fcf5ef2aSThomas Huth             r->u32[i] = ((!le) << 31) | ((!ge) << 30);
701fcf5ef2aSThomas Huth             all_in |= (!le | !ge);
702fcf5ef2aSThomas Huth         }
703fcf5ef2aSThomas Huth     }
704fcf5ef2aSThomas Huth     if (record) {
705fcf5ef2aSThomas Huth         env->crf[6] = (all_in == 0) << 1;
706fcf5ef2aSThomas Huth     }
707fcf5ef2aSThomas Huth }
708fcf5ef2aSThomas Huth 
709fcf5ef2aSThomas Huth void helper_vcmpbfp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
710fcf5ef2aSThomas Huth {
711fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 0);
712fcf5ef2aSThomas Huth }
713fcf5ef2aSThomas Huth 
714fcf5ef2aSThomas Huth void helper_vcmpbfp_dot(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
715fcf5ef2aSThomas Huth                         ppc_avr_t *b)
716fcf5ef2aSThomas Huth {
717fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 1);
718fcf5ef2aSThomas Huth }
719fcf5ef2aSThomas Huth 
720fcf5ef2aSThomas Huth #define VCT(suffix, satcvt, element)                                    \
721fcf5ef2aSThomas Huth     void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r,             \
722fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
723fcf5ef2aSThomas Huth     {                                                                   \
724fcf5ef2aSThomas Huth         int i;                                                          \
725fcf5ef2aSThomas Huth         int sat = 0;                                                    \
726fcf5ef2aSThomas Huth         float_status s = env->vec_status;                               \
727fcf5ef2aSThomas Huth                                                                         \
728fcf5ef2aSThomas Huth         set_float_rounding_mode(float_round_to_zero, &s);               \
72905ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
73005ee3e8aSMark Cave-Ayland             if (float32_is_any_nan(b->f32[i])) {                        \
731fcf5ef2aSThomas Huth                 r->element[i] = 0;                                      \
732fcf5ef2aSThomas Huth             } else {                                                    \
73305ee3e8aSMark Cave-Ayland                 float64 t = float32_to_float64(b->f32[i], &s);          \
734fcf5ef2aSThomas Huth                 int64_t j;                                              \
735fcf5ef2aSThomas Huth                                                                         \
736fcf5ef2aSThomas Huth                 t = float64_scalbn(t, uim, &s);                         \
737fcf5ef2aSThomas Huth                 j = float64_to_int64(t, &s);                            \
738fcf5ef2aSThomas Huth                 r->element[i] = satcvt(j, &sat);                        \
739fcf5ef2aSThomas Huth             }                                                           \
740fcf5ef2aSThomas Huth         }                                                               \
741fcf5ef2aSThomas Huth         if (sat) {                                                      \
7426175f5a0SRichard Henderson             set_vscr_sat(env);                                          \
743fcf5ef2aSThomas Huth         }                                                               \
744fcf5ef2aSThomas Huth     }
745fcf5ef2aSThomas Huth VCT(uxs, cvtsduw, u32)
746fcf5ef2aSThomas Huth VCT(sxs, cvtsdsw, s32)
747fcf5ef2aSThomas Huth #undef VCT
748fcf5ef2aSThomas Huth 
74934553153SLucas Mateus Castro (alqotel) typedef int64_t do_ger(uint32_t, uint32_t, uint32_t);
75034553153SLucas Mateus Castro (alqotel) 
75134553153SLucas Mateus Castro (alqotel) static int64_t ger_rank8(uint32_t a, uint32_t b, uint32_t mask)
75234553153SLucas Mateus Castro (alqotel) {
75334553153SLucas Mateus Castro (alqotel)     int64_t psum = 0;
75434553153SLucas Mateus Castro (alqotel)     for (int i = 0; i < 8; i++, mask >>= 1) {
75534553153SLucas Mateus Castro (alqotel)         if (mask & 1) {
756feeef6b6SDaniel Henrique Barboza             psum += (int64_t)sextract32(a, 4 * i, 4) * sextract32(b, 4 * i, 4);
75734553153SLucas Mateus Castro (alqotel)         }
75834553153SLucas Mateus Castro (alqotel)     }
75934553153SLucas Mateus Castro (alqotel)     return psum;
76034553153SLucas Mateus Castro (alqotel) }
76134553153SLucas Mateus Castro (alqotel) 
76234553153SLucas Mateus Castro (alqotel) static int64_t ger_rank4(uint32_t a, uint32_t b, uint32_t mask)
76334553153SLucas Mateus Castro (alqotel) {
76434553153SLucas Mateus Castro (alqotel)     int64_t psum = 0;
76534553153SLucas Mateus Castro (alqotel)     for (int i = 0; i < 4; i++, mask >>= 1) {
76634553153SLucas Mateus Castro (alqotel)         if (mask & 1) {
76734553153SLucas Mateus Castro (alqotel)             psum += sextract32(a, 8 * i, 8) * (int64_t)extract32(b, 8 * i, 8);
76834553153SLucas Mateus Castro (alqotel)         }
76934553153SLucas Mateus Castro (alqotel)     }
77034553153SLucas Mateus Castro (alqotel)     return psum;
77134553153SLucas Mateus Castro (alqotel) }
77234553153SLucas Mateus Castro (alqotel) 
77334553153SLucas Mateus Castro (alqotel) static int64_t ger_rank2(uint32_t a, uint32_t b, uint32_t mask)
77434553153SLucas Mateus Castro (alqotel) {
77534553153SLucas Mateus Castro (alqotel)     int64_t psum = 0;
77634553153SLucas Mateus Castro (alqotel)     for (int i = 0; i < 2; i++, mask >>= 1) {
77734553153SLucas Mateus Castro (alqotel)         if (mask & 1) {
778feeef6b6SDaniel Henrique Barboza             psum += (int64_t)sextract32(a, 16 * i, 16) *
779feeef6b6SDaniel Henrique Barboza                              sextract32(b, 16 * i, 16);
78034553153SLucas Mateus Castro (alqotel)         }
78134553153SLucas Mateus Castro (alqotel)     }
78234553153SLucas Mateus Castro (alqotel)     return psum;
78334553153SLucas Mateus Castro (alqotel) }
78434553153SLucas Mateus Castro (alqotel) 
78534553153SLucas Mateus Castro (alqotel) static void xviger(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b, ppc_acc_t  *at,
78634553153SLucas Mateus Castro (alqotel)                    uint32_t mask, bool sat, bool acc, do_ger ger)
78734553153SLucas Mateus Castro (alqotel) {
78834553153SLucas Mateus Castro (alqotel)     uint8_t pmsk = FIELD_EX32(mask, GER_MSK, PMSK),
78934553153SLucas Mateus Castro (alqotel)             xmsk = FIELD_EX32(mask, GER_MSK, XMSK),
79034553153SLucas Mateus Castro (alqotel)             ymsk = FIELD_EX32(mask, GER_MSK, YMSK);
79134553153SLucas Mateus Castro (alqotel)     uint8_t xmsk_bit, ymsk_bit;
79234553153SLucas Mateus Castro (alqotel)     int64_t psum;
79334553153SLucas Mateus Castro (alqotel)     int i, j;
79434553153SLucas Mateus Castro (alqotel)     for (i = 0, xmsk_bit = 1 << 3; i < 4; i++, xmsk_bit >>= 1) {
79534553153SLucas Mateus Castro (alqotel)         for (j = 0, ymsk_bit = 1 << 3; j < 4; j++, ymsk_bit >>= 1) {
79634553153SLucas Mateus Castro (alqotel)             if ((xmsk_bit & xmsk) && (ymsk_bit & ymsk)) {
79734553153SLucas Mateus Castro (alqotel)                 psum = ger(a->VsrW(i), b->VsrW(j), pmsk);
79834553153SLucas Mateus Castro (alqotel)                 if (acc) {
79934553153SLucas Mateus Castro (alqotel)                     psum += at[i].VsrSW(j);
80034553153SLucas Mateus Castro (alqotel)                 }
80134553153SLucas Mateus Castro (alqotel)                 if (sat && psum > INT32_MAX) {
80234553153SLucas Mateus Castro (alqotel)                     set_vscr_sat(env);
80334553153SLucas Mateus Castro (alqotel)                     at[i].VsrSW(j) = INT32_MAX;
80434553153SLucas Mateus Castro (alqotel)                 } else if (sat && psum < INT32_MIN) {
80534553153SLucas Mateus Castro (alqotel)                     set_vscr_sat(env);
80634553153SLucas Mateus Castro (alqotel)                     at[i].VsrSW(j) = INT32_MIN;
80734553153SLucas Mateus Castro (alqotel)                 } else {
80834553153SLucas Mateus Castro (alqotel)                     at[i].VsrSW(j) = (int32_t) psum;
80934553153SLucas Mateus Castro (alqotel)                 }
81034553153SLucas Mateus Castro (alqotel)             } else {
81134553153SLucas Mateus Castro (alqotel)                 at[i].VsrSW(j) = 0;
81234553153SLucas Mateus Castro (alqotel)             }
81334553153SLucas Mateus Castro (alqotel)         }
81434553153SLucas Mateus Castro (alqotel)     }
81534553153SLucas Mateus Castro (alqotel) }
81634553153SLucas Mateus Castro (alqotel) 
81734553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
81834553153SLucas Mateus Castro (alqotel) void helper_XVI4GER8(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
81934553153SLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
82034553153SLucas Mateus Castro (alqotel) {
82134553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, false, ger_rank8);
82234553153SLucas Mateus Castro (alqotel) }
82334553153SLucas Mateus Castro (alqotel) 
82434553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
82534553153SLucas Mateus Castro (alqotel) void helper_XVI4GER8PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
82634553153SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
82734553153SLucas Mateus Castro (alqotel) {
82834553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, true, ger_rank8);
82934553153SLucas Mateus Castro (alqotel) }
83034553153SLucas Mateus Castro (alqotel) 
83134553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
83234553153SLucas Mateus Castro (alqotel) void helper_XVI8GER4(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
83334553153SLucas Mateus Castro (alqotel)                      ppc_acc_t *at, uint32_t mask)
83434553153SLucas Mateus Castro (alqotel) {
83534553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, false, ger_rank4);
83634553153SLucas Mateus Castro (alqotel) }
83734553153SLucas Mateus Castro (alqotel) 
83834553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
83934553153SLucas Mateus Castro (alqotel) void helper_XVI8GER4PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
84034553153SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
84134553153SLucas Mateus Castro (alqotel) {
84234553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, true, ger_rank4);
84334553153SLucas Mateus Castro (alqotel) }
84434553153SLucas Mateus Castro (alqotel) 
84534553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
84634553153SLucas Mateus Castro (alqotel) void helper_XVI8GER4SPP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
84734553153SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
84834553153SLucas Mateus Castro (alqotel) {
84934553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, true, true, ger_rank4);
85034553153SLucas Mateus Castro (alqotel) }
85134553153SLucas Mateus Castro (alqotel) 
85234553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
85334553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
85434553153SLucas Mateus Castro (alqotel)                       ppc_acc_t *at, uint32_t mask)
85534553153SLucas Mateus Castro (alqotel) {
85634553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, false, ger_rank2);
85734553153SLucas Mateus Castro (alqotel) }
85834553153SLucas Mateus Castro (alqotel) 
85934553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
86034553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2S(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
86134553153SLucas Mateus Castro (alqotel)                        ppc_acc_t *at, uint32_t mask)
86234553153SLucas Mateus Castro (alqotel) {
86334553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, true, false, ger_rank2);
86434553153SLucas Mateus Castro (alqotel) }
86534553153SLucas Mateus Castro (alqotel) 
86634553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
86734553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2PP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
86834553153SLucas Mateus Castro (alqotel)                         ppc_acc_t *at, uint32_t mask)
86934553153SLucas Mateus Castro (alqotel) {
87034553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, false, true, ger_rank2);
87134553153SLucas Mateus Castro (alqotel) }
87234553153SLucas Mateus Castro (alqotel) 
87334553153SLucas Mateus Castro (alqotel) QEMU_FLATTEN
87434553153SLucas Mateus Castro (alqotel) void helper_XVI16GER2SPP(CPUPPCState *env, ppc_vsr_t *a, ppc_vsr_t *b,
87534553153SLucas Mateus Castro (alqotel)                          ppc_acc_t *at, uint32_t mask)
87634553153SLucas Mateus Castro (alqotel) {
87734553153SLucas Mateus Castro (alqotel)     xviger(env, a, b, at, mask, true, true, ger_rank2);
87834553153SLucas Mateus Castro (alqotel) }
87934553153SLucas Mateus Castro (alqotel) 
880fcf5ef2aSThomas Huth target_ulong helper_vclzlsbb(ppc_avr_t *r)
881fcf5ef2aSThomas Huth {
882fcf5ef2aSThomas Huth     target_ulong count = 0;
883fcf5ef2aSThomas Huth     int i;
88460594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
88560594feaSMark Cave-Ayland         if (r->VsrB(i) & 0x01) {
886fcf5ef2aSThomas Huth             break;
887fcf5ef2aSThomas Huth         }
888fcf5ef2aSThomas Huth         count++;
889fcf5ef2aSThomas Huth     }
890fcf5ef2aSThomas Huth     return count;
891fcf5ef2aSThomas Huth }
892fcf5ef2aSThomas Huth 
893fcf5ef2aSThomas Huth target_ulong helper_vctzlsbb(ppc_avr_t *r)
894fcf5ef2aSThomas Huth {
895fcf5ef2aSThomas Huth     target_ulong count = 0;
896fcf5ef2aSThomas Huth     int i;
897fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
89860594feaSMark Cave-Ayland         if (r->VsrB(i) & 0x01) {
899fcf5ef2aSThomas Huth             break;
900fcf5ef2aSThomas Huth         }
901fcf5ef2aSThomas Huth         count++;
902fcf5ef2aSThomas Huth     }
903fcf5ef2aSThomas Huth     return count;
904fcf5ef2aSThomas Huth }
905fcf5ef2aSThomas Huth 
906306e4753SLucas Mateus Castro (alqotel) void helper_VMHADDSHS(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
907fcf5ef2aSThomas Huth                       ppc_avr_t *b, ppc_avr_t *c)
908fcf5ef2aSThomas Huth {
909fcf5ef2aSThomas Huth     int sat = 0;
910fcf5ef2aSThomas Huth     int i;
911fcf5ef2aSThomas Huth 
912fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
913fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
914fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
915fcf5ef2aSThomas Huth 
916fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
917fcf5ef2aSThomas Huth     }
918fcf5ef2aSThomas Huth 
919fcf5ef2aSThomas Huth     if (sat) {
9206175f5a0SRichard Henderson         set_vscr_sat(env);
921fcf5ef2aSThomas Huth     }
922fcf5ef2aSThomas Huth }
923fcf5ef2aSThomas Huth 
924306e4753SLucas Mateus Castro (alqotel) void helper_VMHRADDSHS(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
925fcf5ef2aSThomas Huth                        ppc_avr_t *b, ppc_avr_t *c)
926fcf5ef2aSThomas Huth {
927fcf5ef2aSThomas Huth     int sat = 0;
928fcf5ef2aSThomas Huth     int i;
929fcf5ef2aSThomas Huth 
930fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
931fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i] + 0x00004000;
932fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
933fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
934fcf5ef2aSThomas Huth     }
935fcf5ef2aSThomas Huth 
936fcf5ef2aSThomas Huth     if (sat) {
9376175f5a0SRichard Henderson         set_vscr_sat(env);
938fcf5ef2aSThomas Huth     }
939fcf5ef2aSThomas Huth }
940fcf5ef2aSThomas Huth 
941dc46167aSLucas Mateus Castro (alqotel) void helper_VMLADDUHM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c,
942dc46167aSLucas Mateus Castro (alqotel)                       uint32_t v)
943fcf5ef2aSThomas Huth {
944fcf5ef2aSThomas Huth     int i;
945fcf5ef2aSThomas Huth 
946fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
947fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
948fcf5ef2aSThomas Huth         r->s16[i] = (int16_t) (prod + c->s16[i]);
949fcf5ef2aSThomas Huth     }
950fcf5ef2aSThomas Huth }
951fcf5ef2aSThomas Huth 
952d81c2040SMark Cave-Ayland #define VMRG_DO(name, element, access, ofs)                                  \
953fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)            \
954fcf5ef2aSThomas Huth     {                                                                        \
955fcf5ef2aSThomas Huth         ppc_avr_t result;                                                    \
956d81c2040SMark Cave-Ayland         int i, half = ARRAY_SIZE(r->element) / 2;                            \
957fcf5ef2aSThomas Huth                                                                              \
958d81c2040SMark Cave-Ayland         for (i = 0; i < half; i++) {                                         \
959d81c2040SMark Cave-Ayland             result.access(i * 2 + 0) = a->access(i + ofs);                   \
960d81c2040SMark Cave-Ayland             result.access(i * 2 + 1) = b->access(i + ofs);                   \
961fcf5ef2aSThomas Huth         }                                                                    \
962fcf5ef2aSThomas Huth         *r = result;                                                         \
963fcf5ef2aSThomas Huth     }
964d81c2040SMark Cave-Ayland 
965d81c2040SMark Cave-Ayland #define VMRG(suffix, element, access)          \
966d81c2040SMark Cave-Ayland     VMRG_DO(mrgl##suffix, element, access, half)   \
967d81c2040SMark Cave-Ayland     VMRG_DO(mrgh##suffix, element, access, 0)
968d81c2040SMark Cave-Ayland VMRG(b, u8, VsrB)
969d81c2040SMark Cave-Ayland VMRG(h, u16, VsrH)
970d81c2040SMark Cave-Ayland VMRG(w, u32, VsrW)
971fcf5ef2aSThomas Huth #undef VMRG_DO
972fcf5ef2aSThomas Huth #undef VMRG
973fcf5ef2aSThomas Huth 
974b2dc03a5SMatheus Ferst void helper_VMSUMMBM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
975fcf5ef2aSThomas Huth {
976fcf5ef2aSThomas Huth     int32_t prod[16];
977fcf5ef2aSThomas Huth     int i;
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s8); i++) {
980fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s8[i] * b->u8[i];
981fcf5ef2aSThomas Huth     }
982fcf5ef2aSThomas Huth 
983fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
984fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[4 * i] + prod[4 * i + 1] +
985fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
986fcf5ef2aSThomas Huth     }
987fcf5ef2aSThomas Huth }
988fcf5ef2aSThomas Huth 
9896f52f731SMatheus Ferst void helper_VMSUMSHM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
990fcf5ef2aSThomas Huth {
991fcf5ef2aSThomas Huth     int32_t prod[8];
992fcf5ef2aSThomas Huth     int i;
993fcf5ef2aSThomas Huth 
994fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
995fcf5ef2aSThomas Huth         prod[i] = a->s16[i] * b->s16[i];
996fcf5ef2aSThomas Huth     }
997fcf5ef2aSThomas Huth 
998fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
999fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[2 * i] + prod[2 * i + 1];
1000fcf5ef2aSThomas Huth     }
1001fcf5ef2aSThomas Huth }
1002fcf5ef2aSThomas Huth 
10036f52f731SMatheus Ferst void helper_VMSUMSHS(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1004fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1005fcf5ef2aSThomas Huth {
1006fcf5ef2aSThomas Huth     int32_t prod[8];
1007fcf5ef2aSThomas Huth     int i;
1008fcf5ef2aSThomas Huth     int sat = 0;
1009fcf5ef2aSThomas Huth 
1010fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
1011fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s16[i] * b->s16[i];
1012fcf5ef2aSThomas Huth     }
1013fcf5ef2aSThomas Huth 
1014fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1015fcf5ef2aSThomas Huth         int64_t t = (int64_t)c->s32[i] + prod[2 * i] + prod[2 * i + 1];
1016fcf5ef2aSThomas Huth 
1017fcf5ef2aSThomas Huth         r->u32[i] = cvtsdsw(t, &sat);
1018fcf5ef2aSThomas Huth     }
1019fcf5ef2aSThomas Huth 
1020fcf5ef2aSThomas Huth     if (sat) {
10216175f5a0SRichard Henderson         set_vscr_sat(env);
1022fcf5ef2aSThomas Huth     }
1023fcf5ef2aSThomas Huth }
1024fcf5ef2aSThomas Huth 
1025b2dc03a5SMatheus Ferst void helper_VMSUMUBM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1026fcf5ef2aSThomas Huth {
1027fcf5ef2aSThomas Huth     uint16_t prod[16];
1028fcf5ef2aSThomas Huth     int i;
1029fcf5ef2aSThomas Huth 
1030fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1031fcf5ef2aSThomas Huth         prod[i] = a->u8[i] * b->u8[i];
1032fcf5ef2aSThomas Huth     }
1033fcf5ef2aSThomas Huth 
1034fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1035fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[4 * i] + prod[4 * i + 1] +
1036fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
1037fcf5ef2aSThomas Huth     }
1038fcf5ef2aSThomas Huth }
1039fcf5ef2aSThomas Huth 
104089a5a1aeSMatheus Ferst void helper_VMSUMUHM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1041fcf5ef2aSThomas Huth {
1042fcf5ef2aSThomas Huth     uint32_t prod[8];
1043fcf5ef2aSThomas Huth     int i;
1044fcf5ef2aSThomas Huth 
1045fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1046fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1047fcf5ef2aSThomas Huth     }
1048fcf5ef2aSThomas Huth 
1049fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1050fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1051fcf5ef2aSThomas Huth     }
1052fcf5ef2aSThomas Huth }
1053fcf5ef2aSThomas Huth 
105489a5a1aeSMatheus Ferst void helper_VMSUMUHS(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1055fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1056fcf5ef2aSThomas Huth {
1057fcf5ef2aSThomas Huth     uint32_t prod[8];
1058fcf5ef2aSThomas Huth     int i;
1059fcf5ef2aSThomas Huth     int sat = 0;
1060fcf5ef2aSThomas Huth 
1061fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1062fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1063fcf5ef2aSThomas Huth     }
1064fcf5ef2aSThomas Huth 
1065fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1066fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
1069fcf5ef2aSThomas Huth     }
1070fcf5ef2aSThomas Huth 
1071fcf5ef2aSThomas Huth     if (sat) {
10726175f5a0SRichard Henderson         set_vscr_sat(env);
1073fcf5ef2aSThomas Huth     }
1074fcf5ef2aSThomas Huth }
1075fcf5ef2aSThomas Huth 
10764fbc89edSMark Cave-Ayland #define VMUL_DO_EVN(name, mul_element, mul_access, prod_access, cast)   \
107780eca687SLucas Mateus Castro (alqotel)     void helper_V##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
1078fcf5ef2aSThomas Huth     {                                                                   \
1079fcf5ef2aSThomas Huth         int i;                                                          \
1080fcf5ef2aSThomas Huth                                                                         \
10814fbc89edSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) {           \
10824fbc89edSMark Cave-Ayland             r->prod_access(i >> 1) = (cast)a->mul_access(i) *           \
10834fbc89edSMark Cave-Ayland                                      (cast)b->mul_access(i);            \
1084fcf5ef2aSThomas Huth         }                                                               \
1085fcf5ef2aSThomas Huth     }
10864fbc89edSMark Cave-Ayland 
10874fbc89edSMark Cave-Ayland #define VMUL_DO_ODD(name, mul_element, mul_access, prod_access, cast)   \
108880eca687SLucas Mateus Castro (alqotel)     void helper_V##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
10894fbc89edSMark Cave-Ayland     {                                                                   \
10904fbc89edSMark Cave-Ayland         int i;                                                          \
10914fbc89edSMark Cave-Ayland                                                                         \
10924fbc89edSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) {           \
10934fbc89edSMark Cave-Ayland             r->prod_access(i >> 1) = (cast)a->mul_access(i + 1) *       \
10944fbc89edSMark Cave-Ayland                                      (cast)b->mul_access(i + 1);        \
10954fbc89edSMark Cave-Ayland         }                                                               \
10964fbc89edSMark Cave-Ayland     }
10974fbc89edSMark Cave-Ayland 
10984fbc89edSMark Cave-Ayland #define VMUL(suffix, mul_element, mul_access, prod_access, cast)       \
109980eca687SLucas Mateus Castro (alqotel)     VMUL_DO_EVN(MULE##suffix, mul_element, mul_access, prod_access, cast)  \
110080eca687SLucas Mateus Castro (alqotel)     VMUL_DO_ODD(MULO##suffix, mul_element, mul_access, prod_access, cast)
110180eca687SLucas Mateus Castro (alqotel) VMUL(SB, s8, VsrSB, VsrSH, int16_t)
110280eca687SLucas Mateus Castro (alqotel) VMUL(SH, s16, VsrSH, VsrSW, int32_t)
110380eca687SLucas Mateus Castro (alqotel) VMUL(SW, s32, VsrSW, VsrSD, int64_t)
110480eca687SLucas Mateus Castro (alqotel) VMUL(UB, u8, VsrB, VsrH, uint16_t)
110580eca687SLucas Mateus Castro (alqotel) VMUL(UH, u16, VsrH, VsrW, uint32_t)
110680eca687SLucas Mateus Castro (alqotel) VMUL(UW, u32, VsrW, VsrD, uint64_t)
11074fbc89edSMark Cave-Ayland #undef VMUL_DO_EVN
11084fbc89edSMark Cave-Ayland #undef VMUL_DO_ODD
1109fcf5ef2aSThomas Huth #undef VMUL
1110fcf5ef2aSThomas Huth 
111141c2877fSMatheus Ferst void helper_XXPERMX(ppc_vsr_t *t, ppc_vsr_t *s0, ppc_vsr_t *s1, ppc_vsr_t *pcv,
111241c2877fSMatheus Ferst                     target_ulong uim)
111341c2877fSMatheus Ferst {
111441c2877fSMatheus Ferst     int i, idx;
111541c2877fSMatheus Ferst     ppc_vsr_t tmp = { .u64 = {0, 0} };
111641c2877fSMatheus Ferst 
111741c2877fSMatheus Ferst     for (i = 0; i < ARRAY_SIZE(t->u8); i++) {
111841c2877fSMatheus Ferst         if ((pcv->VsrB(i) >> 5) == uim) {
111941c2877fSMatheus Ferst             idx = pcv->VsrB(i) & 0x1f;
112041c2877fSMatheus Ferst             if (idx < ARRAY_SIZE(t->u8)) {
112141c2877fSMatheus Ferst                 tmp.VsrB(i) = s0->VsrB(idx);
112241c2877fSMatheus Ferst             } else {
112341c2877fSMatheus Ferst                 tmp.VsrB(i) = s1->VsrB(idx - ARRAY_SIZE(t->u8));
112441c2877fSMatheus Ferst             }
112541c2877fSMatheus Ferst         }
112641c2877fSMatheus Ferst     }
112741c2877fSMatheus Ferst 
112841c2877fSMatheus Ferst     *t = tmp;
112941c2877fSMatheus Ferst }
113041c2877fSMatheus Ferst 
11311700f2bfSLucas Mateus Castro (alqotel) void helper_VDIVSQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
11321700f2bfSLucas Mateus Castro (alqotel) {
11331700f2bfSLucas Mateus Castro (alqotel)     Int128 neg1 = int128_makes64(-1);
11341700f2bfSLucas Mateus Castro (alqotel)     Int128 int128_min = int128_make128(0, INT64_MIN);
11351700f2bfSLucas Mateus Castro (alqotel)     if (likely(int128_nz(b->s128) &&
11361700f2bfSLucas Mateus Castro (alqotel)               (int128_ne(a->s128, int128_min) || int128_ne(b->s128, neg1)))) {
11371700f2bfSLucas Mateus Castro (alqotel)         t->s128 = int128_divs(a->s128, b->s128);
11381700f2bfSLucas Mateus Castro (alqotel)     } else {
11391700f2bfSLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
11401700f2bfSLucas Mateus Castro (alqotel)     }
11411700f2bfSLucas Mateus Castro (alqotel) }
11421700f2bfSLucas Mateus Castro (alqotel) 
11431700f2bfSLucas Mateus Castro (alqotel) void helper_VDIVUQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
11441700f2bfSLucas Mateus Castro (alqotel) {
11451700f2bfSLucas Mateus Castro (alqotel)     if (int128_nz(b->s128)) {
11461700f2bfSLucas Mateus Castro (alqotel)         t->s128 = int128_divu(a->s128, b->s128);
11471700f2bfSLucas Mateus Castro (alqotel)     } else {
11481700f2bfSLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
11491700f2bfSLucas Mateus Castro (alqotel)     }
11501700f2bfSLucas Mateus Castro (alqotel) }
11511700f2bfSLucas Mateus Castro (alqotel) 
1152a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVESD(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1153a173ba88SLucas Mateus Castro (alqotel) {
1154a173ba88SLucas Mateus Castro (alqotel)     int i;
1155a173ba88SLucas Mateus Castro (alqotel)     int64_t high;
1156a173ba88SLucas Mateus Castro (alqotel)     uint64_t low;
1157a173ba88SLucas Mateus Castro (alqotel)     for (i = 0; i < 2; i++) {
1158a173ba88SLucas Mateus Castro (alqotel)         high = a->s64[i];
1159a173ba88SLucas Mateus Castro (alqotel)         low = 0;
1160a173ba88SLucas Mateus Castro (alqotel)         if (unlikely((high == INT64_MIN && b->s64[i] == -1) || !b->s64[i])) {
1161a173ba88SLucas Mateus Castro (alqotel)             t->s64[i] = a->s64[i]; /* Undefined behavior */
1162a173ba88SLucas Mateus Castro (alqotel)         } else {
1163a173ba88SLucas Mateus Castro (alqotel)             divs128(&low, &high, b->s64[i]);
1164a173ba88SLucas Mateus Castro (alqotel)             t->s64[i] = low;
1165a173ba88SLucas Mateus Castro (alqotel)         }
1166a173ba88SLucas Mateus Castro (alqotel)     }
1167a173ba88SLucas Mateus Castro (alqotel) }
1168a173ba88SLucas Mateus Castro (alqotel) 
1169a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVEUD(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1170a173ba88SLucas Mateus Castro (alqotel) {
1171a173ba88SLucas Mateus Castro (alqotel)     int i;
1172a173ba88SLucas Mateus Castro (alqotel)     uint64_t high, low;
1173a173ba88SLucas Mateus Castro (alqotel)     for (i = 0; i < 2; i++) {
1174a173ba88SLucas Mateus Castro (alqotel)         high = a->u64[i];
1175a173ba88SLucas Mateus Castro (alqotel)         low = 0;
1176a173ba88SLucas Mateus Castro (alqotel)         if (unlikely(!b->u64[i])) {
1177a173ba88SLucas Mateus Castro (alqotel)             t->u64[i] = a->u64[i]; /* Undefined behavior */
1178a173ba88SLucas Mateus Castro (alqotel)         } else {
1179a173ba88SLucas Mateus Castro (alqotel)             divu128(&low, &high, b->u64[i]);
1180a173ba88SLucas Mateus Castro (alqotel)             t->u64[i] = low;
1181a173ba88SLucas Mateus Castro (alqotel)         }
1182a173ba88SLucas Mateus Castro (alqotel)     }
1183a173ba88SLucas Mateus Castro (alqotel) }
1184a173ba88SLucas Mateus Castro (alqotel) 
1185a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVESQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1186a173ba88SLucas Mateus Castro (alqotel) {
1187a173ba88SLucas Mateus Castro (alqotel)     Int128 high, low;
1188a173ba88SLucas Mateus Castro (alqotel)     Int128 int128_min = int128_make128(0, INT64_MIN);
1189a173ba88SLucas Mateus Castro (alqotel)     Int128 neg1 = int128_makes64(-1);
1190a173ba88SLucas Mateus Castro (alqotel) 
1191a173ba88SLucas Mateus Castro (alqotel)     high = a->s128;
1192a173ba88SLucas Mateus Castro (alqotel)     low = int128_zero();
1193a173ba88SLucas Mateus Castro (alqotel)     if (unlikely(!int128_nz(b->s128) ||
1194a173ba88SLucas Mateus Castro (alqotel)                  (int128_eq(b->s128, neg1) && int128_eq(high, int128_min)))) {
1195a173ba88SLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
1196a173ba88SLucas Mateus Castro (alqotel)     } else {
1197a173ba88SLucas Mateus Castro (alqotel)         divs256(&low, &high, b->s128);
1198a173ba88SLucas Mateus Castro (alqotel)         t->s128 = low;
1199a173ba88SLucas Mateus Castro (alqotel)     }
1200a173ba88SLucas Mateus Castro (alqotel) }
1201a173ba88SLucas Mateus Castro (alqotel) 
1202a173ba88SLucas Mateus Castro (alqotel) void helper_VDIVEUQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1203a173ba88SLucas Mateus Castro (alqotel) {
1204a173ba88SLucas Mateus Castro (alqotel)     Int128 high, low;
1205a173ba88SLucas Mateus Castro (alqotel) 
1206a173ba88SLucas Mateus Castro (alqotel)     high = a->s128;
1207a173ba88SLucas Mateus Castro (alqotel)     low = int128_zero();
1208a173ba88SLucas Mateus Castro (alqotel)     if (unlikely(!int128_nz(b->s128))) {
1209a173ba88SLucas Mateus Castro (alqotel)         t->s128 = a->s128; /* Undefined behavior */
1210a173ba88SLucas Mateus Castro (alqotel)     } else {
1211a173ba88SLucas Mateus Castro (alqotel)         divu256(&low, &high, b->s128);
1212a173ba88SLucas Mateus Castro (alqotel)         t->s128 = low;
1213a173ba88SLucas Mateus Castro (alqotel)     }
1214a173ba88SLucas Mateus Castro (alqotel) }
1215a173ba88SLucas Mateus Castro (alqotel) 
1216b80bec3aSLucas Mateus Castro (alqotel) void helper_VMODSQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1217b80bec3aSLucas Mateus Castro (alqotel) {
1218b80bec3aSLucas Mateus Castro (alqotel)     Int128 neg1 = int128_makes64(-1);
1219b80bec3aSLucas Mateus Castro (alqotel)     Int128 int128_min = int128_make128(0, INT64_MIN);
1220b80bec3aSLucas Mateus Castro (alqotel)     if (likely(int128_nz(b->s128) &&
1221b80bec3aSLucas Mateus Castro (alqotel)               (int128_ne(a->s128, int128_min) || int128_ne(b->s128, neg1)))) {
1222b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_rems(a->s128, b->s128);
1223b80bec3aSLucas Mateus Castro (alqotel)     } else {
1224b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_zero(); /* Undefined behavior */
1225b80bec3aSLucas Mateus Castro (alqotel)     }
1226b80bec3aSLucas Mateus Castro (alqotel) }
1227b80bec3aSLucas Mateus Castro (alqotel) 
1228b80bec3aSLucas Mateus Castro (alqotel) void helper_VMODUQ(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b)
1229b80bec3aSLucas Mateus Castro (alqotel) {
1230b80bec3aSLucas Mateus Castro (alqotel)     if (likely(int128_nz(b->s128))) {
1231b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_remu(a->s128, b->s128);
1232b80bec3aSLucas Mateus Castro (alqotel)     } else {
1233b80bec3aSLucas Mateus Castro (alqotel)         t->s128 = int128_zero(); /* Undefined behavior */
1234b80bec3aSLucas Mateus Castro (alqotel)     }
1235b80bec3aSLucas Mateus Castro (alqotel) }
1236b80bec3aSLucas Mateus Castro (alqotel) 
123728347fe2SMatheus Ferst void helper_VPERM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1238fcf5ef2aSThomas Huth {
1239fcf5ef2aSThomas Huth     ppc_avr_t result;
1240fcf5ef2aSThomas Huth     int i;
1241fcf5ef2aSThomas Huth 
124260594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
124360594feaSMark Cave-Ayland         int s = c->VsrB(i) & 0x1f;
1244fcf5ef2aSThomas Huth         int index = s & 0xf;
1245fcf5ef2aSThomas Huth 
1246fcf5ef2aSThomas Huth         if (s & 0x10) {
124760594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index);
1248fcf5ef2aSThomas Huth         } else {
124960594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1250fcf5ef2aSThomas Huth         }
1251fcf5ef2aSThomas Huth     }
1252fcf5ef2aSThomas Huth     *r = result;
1253fcf5ef2aSThomas Huth }
1254fcf5ef2aSThomas Huth 
125528347fe2SMatheus Ferst void helper_VPERMR(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1256fcf5ef2aSThomas Huth {
1257fcf5ef2aSThomas Huth     ppc_avr_t result;
1258fcf5ef2aSThomas Huth     int i;
1259fcf5ef2aSThomas Huth 
126060594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
126160594feaSMark Cave-Ayland         int s = c->VsrB(i) & 0x1f;
1262fcf5ef2aSThomas Huth         int index = 15 - (s & 0xf);
1263fcf5ef2aSThomas Huth 
1264fcf5ef2aSThomas Huth         if (s & 0x10) {
126560594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1266fcf5ef2aSThomas Huth         } else {
126760594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index);
1268fcf5ef2aSThomas Huth         }
1269fcf5ef2aSThomas Huth     }
1270fcf5ef2aSThomas Huth     *r = result;
1271fcf5ef2aSThomas Huth }
1272fcf5ef2aSThomas Huth 
1273618574ddSMatheus Ferst #define XXGENPCV_BE_EXP(NAME, SZ) \
1274b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _be_exp))(ppc_vsr_t *t, ppc_vsr_t *b) \
1275b090f4f1SMatheus Ferst {                                                                   \
1276b090f4f1SMatheus Ferst     ppc_vsr_t tmp;                                                  \
1277b090f4f1SMatheus Ferst                                                                     \
1278b090f4f1SMatheus Ferst     /* Initialize tmp with the result of an all-zeros mask */       \
1279b090f4f1SMatheus Ferst     tmp.VsrD(0) = 0x1011121314151617;                               \
1280b090f4f1SMatheus Ferst     tmp.VsrD(1) = 0x18191A1B1C1D1E1F;                               \
1281b090f4f1SMatheus Ferst                                                                     \
1282b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1283b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1284b090f4f1SMatheus Ferst         if (b->VsrB(i) & 0x80) {                                    \
1285b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1286b090f4f1SMatheus Ferst             for (int k = 0; k < SZ; k++) {                          \
1287b090f4f1SMatheus Ferst                 tmp.VsrB(i + k) = j + k;                            \
1288b090f4f1SMatheus Ferst             }                                                       \
1289b090f4f1SMatheus Ferst             j += SZ;                                                \
1290b090f4f1SMatheus Ferst         }                                                           \
1291b090f4f1SMatheus Ferst     }                                                               \
1292b090f4f1SMatheus Ferst                                                                     \
1293b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1294618574ddSMatheus Ferst }
1295618574ddSMatheus Ferst 
1296618574ddSMatheus Ferst #define XXGENPCV_BE_COMP(NAME, SZ) \
1297b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _be_comp))(ppc_vsr_t *t, ppc_vsr_t *b)\
1298b090f4f1SMatheus Ferst {                                                                   \
1299b090f4f1SMatheus Ferst     ppc_vsr_t tmp = { .u64 = { 0, 0 } };                            \
1300b090f4f1SMatheus Ferst                                                                     \
1301b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1302b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1303b090f4f1SMatheus Ferst         if (b->VsrB(i) & 0x80) {                                    \
1304b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1305b090f4f1SMatheus Ferst             for (int k = 0; k < SZ; k++) {                          \
1306b090f4f1SMatheus Ferst                 tmp.VsrB(j + k) = i + k;                            \
1307b090f4f1SMatheus Ferst             }                                                       \
1308b090f4f1SMatheus Ferst             j += SZ;                                                \
1309b090f4f1SMatheus Ferst         }                                                           \
1310b090f4f1SMatheus Ferst     }                                                               \
1311b090f4f1SMatheus Ferst                                                                     \
1312b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1313618574ddSMatheus Ferst }
1314618574ddSMatheus Ferst 
1315618574ddSMatheus Ferst #define XXGENPCV_LE_EXP(NAME, SZ) \
1316b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _le_exp))(ppc_vsr_t *t, ppc_vsr_t *b) \
1317b090f4f1SMatheus Ferst {                                                                   \
1318b090f4f1SMatheus Ferst     ppc_vsr_t tmp;                                                  \
1319b090f4f1SMatheus Ferst                                                                     \
1320b090f4f1SMatheus Ferst     /* Initialize tmp with the result of an all-zeros mask */       \
1321b090f4f1SMatheus Ferst     tmp.VsrD(0) = 0x1F1E1D1C1B1A1918;                               \
1322b090f4f1SMatheus Ferst     tmp.VsrD(1) = 0x1716151413121110;                               \
1323b090f4f1SMatheus Ferst                                                                     \
1324b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1325b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1326b090f4f1SMatheus Ferst         /* Reverse indexing of "i" */                               \
1327b090f4f1SMatheus Ferst         const int idx = ARRAY_SIZE(b->u8) - i - SZ;                 \
1328b090f4f1SMatheus Ferst         if (b->VsrB(idx) & 0x80) {                                  \
1329b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1330b090f4f1SMatheus Ferst             for (int k = 0, rk = SZ - 1; k < SZ; k++, rk--) {       \
1331b090f4f1SMatheus Ferst                 tmp.VsrB(idx + rk) = j + k;                         \
1332b090f4f1SMatheus Ferst             }                                                       \
1333b090f4f1SMatheus Ferst             j += SZ;                                                \
1334b090f4f1SMatheus Ferst         }                                                           \
1335b090f4f1SMatheus Ferst     }                                                               \
1336b090f4f1SMatheus Ferst                                                                     \
1337b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1338618574ddSMatheus Ferst }
1339618574ddSMatheus Ferst 
1340618574ddSMatheus Ferst #define XXGENPCV_LE_COMP(NAME, SZ) \
1341b090f4f1SMatheus Ferst void glue(helper_, glue(NAME, _le_comp))(ppc_vsr_t *t, ppc_vsr_t *b)\
1342b090f4f1SMatheus Ferst {                                                                   \
1343b090f4f1SMatheus Ferst     ppc_vsr_t tmp = { .u64 = { 0, 0 } };                            \
1344b090f4f1SMatheus Ferst                                                                     \
1345b090f4f1SMatheus Ferst     /* Iterate over the most significant byte of each element */    \
1346b090f4f1SMatheus Ferst     for (int i = 0, j = 0; i < ARRAY_SIZE(b->u8); i += SZ) {        \
1347b090f4f1SMatheus Ferst         if (b->VsrB(ARRAY_SIZE(b->u8) - i - SZ) & 0x80) {           \
1348b090f4f1SMatheus Ferst             /* Update each byte of the element */                   \
1349b090f4f1SMatheus Ferst             for (int k = 0, rk = SZ - 1; k < SZ; k++, rk--) {       \
1350b090f4f1SMatheus Ferst                 /* Reverse indexing of "j" */                       \
1351b090f4f1SMatheus Ferst                 const int idx = ARRAY_SIZE(b->u8) - j - SZ;         \
1352b090f4f1SMatheus Ferst                 tmp.VsrB(idx + rk) = i + k;                         \
1353b090f4f1SMatheus Ferst             }                                                       \
1354b090f4f1SMatheus Ferst             j += SZ;                                                \
1355b090f4f1SMatheus Ferst         }                                                           \
1356b090f4f1SMatheus Ferst     }                                                               \
1357b090f4f1SMatheus Ferst                                                                     \
1358b090f4f1SMatheus Ferst     *t = tmp;                                                       \
1359b090f4f1SMatheus Ferst }
1360b090f4f1SMatheus Ferst 
1361618574ddSMatheus Ferst #define XXGENPCV(NAME, SZ) \
1362618574ddSMatheus Ferst     XXGENPCV_BE_EXP(NAME, SZ)  \
1363618574ddSMatheus Ferst     XXGENPCV_BE_COMP(NAME, SZ) \
1364618574ddSMatheus Ferst     XXGENPCV_LE_EXP(NAME, SZ)  \
1365618574ddSMatheus Ferst     XXGENPCV_LE_COMP(NAME, SZ) \
1366618574ddSMatheus Ferst 
1367b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVBM, 1)
1368b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVHM, 2)
1369b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVWM, 4)
1370b090f4f1SMatheus Ferst XXGENPCV(XXGENPCVDM, 8)
1371618574ddSMatheus Ferst 
1372618574ddSMatheus Ferst #undef XXGENPCV_BE_EXP
1373618574ddSMatheus Ferst #undef XXGENPCV_BE_COMP
1374618574ddSMatheus Ferst #undef XXGENPCV_LE_EXP
1375618574ddSMatheus Ferst #undef XXGENPCV_LE_COMP
1376b090f4f1SMatheus Ferst #undef XXGENPCV
1377b090f4f1SMatheus Ferst 
1378e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1379fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[(i)])
1380fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (i)
1381fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) != 0)
1382fcf5ef2aSThomas Huth #else
1383fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[15 - (i)])
1384fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (1 - i)
1385fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) == 0)
1386fcf5ef2aSThomas Huth #endif
13878f7d41e0SMatheus Ferst #define EXTRACT_BIT(avr, i, index) \
13888f7d41e0SMatheus Ferst         (extract64((avr)->VsrD(i), 63 - index, 1))
1389fcf5ef2aSThomas Huth 
1390fcf5ef2aSThomas Huth void helper_vbpermd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1391fcf5ef2aSThomas Huth {
1392fcf5ef2aSThomas Huth     int i, j;
1393fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
1394fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1395fcf5ef2aSThomas Huth         for (j = 0; j < 8; j++) {
1396fcf5ef2aSThomas Huth             int index = VBPERMQ_INDEX(b, (i * 8) + j);
1397fcf5ef2aSThomas Huth             if (index < 64 && EXTRACT_BIT(a, i, index)) {
1398fcf5ef2aSThomas Huth                 result.u64[VBPERMD_INDEX(i)] |= (0x80 >> j);
1399fcf5ef2aSThomas Huth             }
1400fcf5ef2aSThomas Huth         }
1401fcf5ef2aSThomas Huth     }
1402fcf5ef2aSThomas Huth     *r = result;
1403fcf5ef2aSThomas Huth }
1404fcf5ef2aSThomas Huth 
1405fcf5ef2aSThomas Huth void helper_vbpermq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1406fcf5ef2aSThomas Huth {
1407fcf5ef2aSThomas Huth     int i;
1408fcf5ef2aSThomas Huth     uint64_t perm = 0;
1409fcf5ef2aSThomas Huth 
1410fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1411fcf5ef2aSThomas Huth         int index = VBPERMQ_INDEX(b, i);
1412fcf5ef2aSThomas Huth 
1413fcf5ef2aSThomas Huth         if (index < 128) {
1414fcf5ef2aSThomas Huth             uint64_t mask = (1ull << (63 - (index & 0x3F)));
1415fcf5ef2aSThomas Huth             if (a->u64[VBPERMQ_DW(index)] & mask) {
1416fcf5ef2aSThomas Huth                 perm |= (0x8000 >> i);
1417fcf5ef2aSThomas Huth             }
1418fcf5ef2aSThomas Huth         }
1419fcf5ef2aSThomas Huth     }
1420fcf5ef2aSThomas Huth 
14213c385a93SMark Cave-Ayland     r->VsrD(0) = perm;
14223c385a93SMark Cave-Ayland     r->VsrD(1) = 0;
1423fcf5ef2aSThomas Huth }
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth #undef VBPERMQ_INDEX
1426fcf5ef2aSThomas Huth #undef VBPERMQ_DW
1427fcf5ef2aSThomas Huth 
1428*cec4090dSRichard Henderson /*
1429*cec4090dSRichard Henderson  * There is no carry across the two doublewords, so their order does
1430*cec4090dSRichard Henderson  * not matter.  Nor is there partial overlap between registers.
1431*cec4090dSRichard Henderson  */
1432*cec4090dSRichard Henderson void helper_vpmsumb(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1433*cec4090dSRichard Henderson {
1434*cec4090dSRichard Henderson     for (int i = 0; i < 2; ++i) {
1435*cec4090dSRichard Henderson         uint64_t aa = a->u64[i], bb = b->u64[i];
1436*cec4090dSRichard Henderson         r->u64[i] = clmul_8x4_even(aa, bb) ^ clmul_8x4_odd(aa, bb);
1437*cec4090dSRichard Henderson     }
1438*cec4090dSRichard Henderson }
1439*cec4090dSRichard Henderson 
1440fcf5ef2aSThomas Huth #define PMSUM(name, srcfld, trgfld, trgtyp)                   \
1441fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
1442fcf5ef2aSThomas Huth {                                                             \
1443fcf5ef2aSThomas Huth     int i, j;                                                 \
1444fcf5ef2aSThomas Huth     trgtyp prod[sizeof(ppc_avr_t) / sizeof(a->srcfld[0])];    \
1445fcf5ef2aSThomas Huth                                                               \
1446fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, srcfld) {                         \
1447fcf5ef2aSThomas Huth         prod[i] = 0;                                          \
1448fcf5ef2aSThomas Huth         for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) {      \
1449fcf5ef2aSThomas Huth             if (a->srcfld[i] & (1ull << j)) {                 \
1450fcf5ef2aSThomas Huth                 prod[i] ^= ((trgtyp)b->srcfld[i] << j);       \
1451fcf5ef2aSThomas Huth             }                                                 \
1452fcf5ef2aSThomas Huth         }                                                     \
1453fcf5ef2aSThomas Huth     }                                                         \
1454fcf5ef2aSThomas Huth                                                               \
1455fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, trgfld) {                         \
1456fcf5ef2aSThomas Huth         r->trgfld[i] = prod[2 * i] ^ prod[2 * i + 1];         \
1457fcf5ef2aSThomas Huth     }                                                         \
1458fcf5ef2aSThomas Huth }
1459fcf5ef2aSThomas Huth 
1460fcf5ef2aSThomas Huth PMSUM(vpmsumh, u16, u32, uint32_t)
1461fcf5ef2aSThomas Huth PMSUM(vpmsumw, u32, u64, uint64_t)
1462fcf5ef2aSThomas Huth 
1463e82ca8acSMatheus Ferst void helper_VPMSUMD(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1464fcf5ef2aSThomas Huth {
1465fcf5ef2aSThomas Huth     int i, j;
1466e82ca8acSMatheus Ferst     Int128 tmp, prod[2] = {int128_zero(), int128_zero()};
1467fcf5ef2aSThomas Huth 
1468fcf5ef2aSThomas Huth     for (j = 0; j < 64; j++) {
1469e82ca8acSMatheus Ferst         for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
1470e82ca8acSMatheus Ferst             if (a->VsrD(i) & (1ull << j)) {
1471e82ca8acSMatheus Ferst                 tmp = int128_make64(b->VsrD(i));
1472e82ca8acSMatheus Ferst                 tmp = int128_lshift(tmp, j);
1473e82ca8acSMatheus Ferst                 prod[i] = int128_xor(prod[i], tmp);
1474fcf5ef2aSThomas Huth             }
1475fcf5ef2aSThomas Huth         }
1476fcf5ef2aSThomas Huth     }
1477fcf5ef2aSThomas Huth 
1478e82ca8acSMatheus Ferst     r->s128 = int128_xor(prod[0], prod[1]);
1479fcf5ef2aSThomas Huth }
1480fcf5ef2aSThomas Huth 
1481e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1482fcf5ef2aSThomas Huth #define PKBIG 1
1483fcf5ef2aSThomas Huth #else
1484fcf5ef2aSThomas Huth #define PKBIG 0
1485fcf5ef2aSThomas Huth #endif
1486fcf5ef2aSThomas Huth void helper_vpkpx(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1487fcf5ef2aSThomas Huth {
1488fcf5ef2aSThomas Huth     int i, j;
1489fcf5ef2aSThomas Huth     ppc_avr_t result;
1490e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1491fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { a, b };
1492fcf5ef2aSThomas Huth #else
1493fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { b, a };
1494fcf5ef2aSThomas Huth #endif
1495fcf5ef2aSThomas Huth 
1496fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1497fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(j, u32) {
1498fcf5ef2aSThomas Huth             uint32_t e = x[i]->u32[j];
1499fcf5ef2aSThomas Huth 
1500fcf5ef2aSThomas Huth             result.u16[4 * i + j] = (((e >> 9) & 0xfc00) |
1501fcf5ef2aSThomas Huth                                      ((e >> 6) & 0x3e0) |
1502fcf5ef2aSThomas Huth                                      ((e >> 3) & 0x1f));
1503fcf5ef2aSThomas Huth         }
1504fcf5ef2aSThomas Huth     }
1505fcf5ef2aSThomas Huth     *r = result;
1506fcf5ef2aSThomas Huth }
1507fcf5ef2aSThomas Huth 
1508fcf5ef2aSThomas Huth #define VPK(suffix, from, to, cvt, dosat)                               \
1509fcf5ef2aSThomas Huth     void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r,             \
1510fcf5ef2aSThomas Huth                             ppc_avr_t *a, ppc_avr_t *b)                 \
1511fcf5ef2aSThomas Huth     {                                                                   \
1512fcf5ef2aSThomas Huth         int i;                                                          \
1513fcf5ef2aSThomas Huth         int sat = 0;                                                    \
1514fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
1515fcf5ef2aSThomas Huth         ppc_avr_t *a0 = PKBIG ? a : b;                                  \
1516fcf5ef2aSThomas Huth         ppc_avr_t *a1 = PKBIG ? b : a;                                  \
1517fcf5ef2aSThomas Huth                                                                         \
1518fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(i, from) {                                 \
1519fcf5ef2aSThomas Huth             result.to[i] = cvt(a0->from[i], &sat);                      \
1520fcf5ef2aSThomas Huth             result.to[i + ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);\
1521fcf5ef2aSThomas Huth         }                                                               \
1522fcf5ef2aSThomas Huth         *r = result;                                                    \
1523fcf5ef2aSThomas Huth         if (dosat && sat) {                                             \
15246175f5a0SRichard Henderson             set_vscr_sat(env);                                          \
1525fcf5ef2aSThomas Huth         }                                                               \
1526fcf5ef2aSThomas Huth     }
1527fcf5ef2aSThomas Huth #define I(x, y) (x)
1528fcf5ef2aSThomas Huth VPK(shss, s16, s8, cvtshsb, 1)
1529fcf5ef2aSThomas Huth VPK(shus, s16, u8, cvtshub, 1)
1530fcf5ef2aSThomas Huth VPK(swss, s32, s16, cvtswsh, 1)
1531fcf5ef2aSThomas Huth VPK(swus, s32, u16, cvtswuh, 1)
1532fcf5ef2aSThomas Huth VPK(sdss, s64, s32, cvtsdsw, 1)
1533fcf5ef2aSThomas Huth VPK(sdus, s64, u32, cvtsduw, 1)
1534fcf5ef2aSThomas Huth VPK(uhus, u16, u8, cvtuhub, 1)
1535fcf5ef2aSThomas Huth VPK(uwus, u32, u16, cvtuwuh, 1)
1536fcf5ef2aSThomas Huth VPK(udus, u64, u32, cvtuduw, 1)
1537fcf5ef2aSThomas Huth VPK(uhum, u16, u8, I, 0)
1538fcf5ef2aSThomas Huth VPK(uwum, u32, u16, I, 0)
1539fcf5ef2aSThomas Huth VPK(udum, u64, u32, I, 0)
1540fcf5ef2aSThomas Huth #undef I
1541fcf5ef2aSThomas Huth #undef VPK
1542fcf5ef2aSThomas Huth #undef PKBIG
1543fcf5ef2aSThomas Huth 
1544fcf5ef2aSThomas Huth void helper_vrefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1545fcf5ef2aSThomas Huth {
1546fcf5ef2aSThomas Huth     int i;
1547fcf5ef2aSThomas Huth 
154805ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
154905ee3e8aSMark Cave-Ayland         r->f32[i] = float32_div(float32_one, b->f32[i], &env->vec_status);
1550fcf5ef2aSThomas Huth     }
1551fcf5ef2aSThomas Huth }
1552fcf5ef2aSThomas Huth 
1553fcf5ef2aSThomas Huth #define VRFI(suffix, rounding)                                  \
1554fcf5ef2aSThomas Huth     void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r,    \
1555fcf5ef2aSThomas Huth                              ppc_avr_t *b)                      \
1556fcf5ef2aSThomas Huth     {                                                           \
1557fcf5ef2aSThomas Huth         int i;                                                  \
1558fcf5ef2aSThomas Huth         float_status s = env->vec_status;                       \
1559fcf5ef2aSThomas Huth                                                                 \
1560fcf5ef2aSThomas Huth         set_float_rounding_mode(rounding, &s);                  \
156105ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {              \
156205ee3e8aSMark Cave-Ayland             r->f32[i] = float32_round_to_int (b->f32[i], &s);   \
1563fcf5ef2aSThomas Huth         }                                                       \
1564fcf5ef2aSThomas Huth     }
1565fcf5ef2aSThomas Huth VRFI(n, float_round_nearest_even)
1566fcf5ef2aSThomas Huth VRFI(m, float_round_down)
1567fcf5ef2aSThomas Huth VRFI(p, float_round_up)
1568fcf5ef2aSThomas Huth VRFI(z, float_round_to_zero)
1569fcf5ef2aSThomas Huth #undef VRFI
1570fcf5ef2aSThomas Huth 
1571fcf5ef2aSThomas Huth void helper_vrsqrtefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1572fcf5ef2aSThomas Huth {
1573fcf5ef2aSThomas Huth     int i;
1574fcf5ef2aSThomas Huth 
157505ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
157605ee3e8aSMark Cave-Ayland         float32 t = float32_sqrt(b->f32[i], &env->vec_status);
1577fcf5ef2aSThomas Huth 
157805ee3e8aSMark Cave-Ayland         r->f32[i] = float32_div(float32_one, t, &env->vec_status);
1579fcf5ef2aSThomas Huth     }
1580fcf5ef2aSThomas Huth }
1581fcf5ef2aSThomas Huth 
1582fcf5ef2aSThomas Huth #define VRLMI(name, size, element, insert)                                  \
158302c74f0eSMatheus Ferst void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t desc) \
1584fcf5ef2aSThomas Huth {                                                                           \
1585fcf5ef2aSThomas Huth     int i;                                                                  \
1586fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                          \
1587fcf5ef2aSThomas Huth         uint##size##_t src1 = a->element[i];                                \
1588fcf5ef2aSThomas Huth         uint##size##_t src2 = b->element[i];                                \
1589fcf5ef2aSThomas Huth         uint##size##_t src3 = r->element[i];                                \
1590fcf5ef2aSThomas Huth         uint##size##_t begin, end, shift, mask, rot_val;                    \
1591fcf5ef2aSThomas Huth                                                                             \
1592fcf5ef2aSThomas Huth         shift = extract##size(src2, 0, 6);                                  \
1593fcf5ef2aSThomas Huth         end   = extract##size(src2, 8, 6);                                  \
1594fcf5ef2aSThomas Huth         begin = extract##size(src2, 16, 6);                                 \
1595fcf5ef2aSThomas Huth         rot_val = rol##size(src1, shift);                                   \
1596fcf5ef2aSThomas Huth         mask = mask_u##size(begin, end);                                    \
1597fcf5ef2aSThomas Huth         if (insert) {                                                       \
1598fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask) | (src3 & ~mask);              \
1599fcf5ef2aSThomas Huth         } else {                                                            \
1600fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask);                               \
1601fcf5ef2aSThomas Huth         }                                                                   \
1602fcf5ef2aSThomas Huth     }                                                                       \
1603fcf5ef2aSThomas Huth }
1604fcf5ef2aSThomas Huth 
160502c74f0eSMatheus Ferst VRLMI(VRLDMI, 64, u64, 1);
160602c74f0eSMatheus Ferst VRLMI(VRLWMI, 32, u32, 1);
160702c74f0eSMatheus Ferst VRLMI(VRLDNM, 64, u64, 0);
160802c74f0eSMatheus Ferst VRLMI(VRLWNM, 32, u32, 0);
1609fcf5ef2aSThomas Huth 
1610fcf5ef2aSThomas Huth void helper_vexptefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1611fcf5ef2aSThomas Huth {
1612fcf5ef2aSThomas Huth     int i;
1613fcf5ef2aSThomas Huth 
161405ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
161505ee3e8aSMark Cave-Ayland         r->f32[i] = float32_exp2(b->f32[i], &env->vec_status);
1616fcf5ef2aSThomas Huth     }
1617fcf5ef2aSThomas Huth }
1618fcf5ef2aSThomas Huth 
1619fcf5ef2aSThomas Huth void helper_vlogefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1620fcf5ef2aSThomas Huth {
1621fcf5ef2aSThomas Huth     int i;
1622fcf5ef2aSThomas Huth 
162305ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
162405ee3e8aSMark Cave-Ayland         r->f32[i] = float32_log2(b->f32[i], &env->vec_status);
1625fcf5ef2aSThomas Huth     }
1626fcf5ef2aSThomas Huth }
1627fcf5ef2aSThomas Huth 
162860caf221SAvinesh Kumar #define VEXTU_X_DO(name, size, left)                            \
162960caf221SAvinesh Kumar target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
163060caf221SAvinesh Kumar {                                                               \
1631f297c4c6SMatheus Ferst     int index = (a & 0xf) * 8;                                  \
163260caf221SAvinesh Kumar     if (left) {                                                 \
1633f297c4c6SMatheus Ferst         index = 128 - index - size;                             \
163460caf221SAvinesh Kumar     }                                                           \
163560caf221SAvinesh Kumar     return int128_getlo(int128_rshift(b->s128, index)) &        \
163660caf221SAvinesh Kumar         MAKE_64BIT_MASK(0, size);                               \
163760caf221SAvinesh Kumar }
163860caf221SAvinesh Kumar VEXTU_X_DO(vextublx,  8, 1)
163960caf221SAvinesh Kumar VEXTU_X_DO(vextuhlx, 16, 1)
164060caf221SAvinesh Kumar VEXTU_X_DO(vextuwlx, 32, 1)
164160caf221SAvinesh Kumar VEXTU_X_DO(vextubrx,  8, 0)
164260caf221SAvinesh Kumar VEXTU_X_DO(vextuhrx, 16, 0)
164360caf221SAvinesh Kumar VEXTU_X_DO(vextuwrx, 32, 0)
164460caf221SAvinesh Kumar #undef VEXTU_X_DO
164560caf221SAvinesh Kumar 
1646fcf5ef2aSThomas Huth void helper_vslv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1647fcf5ef2aSThomas Huth {
1648fcf5ef2aSThomas Huth     int i;
1649fcf5ef2aSThomas Huth     unsigned int shift, bytes, size;
1650fcf5ef2aSThomas Huth 
1651fcf5ef2aSThomas Huth     size = ARRAY_SIZE(r->u8);
1652fcf5ef2aSThomas Huth     for (i = 0; i < size; i++) {
165363be02fcSAnton Blanchard         shift = b->VsrB(i) & 0x7;             /* extract shift value */
165463be02fcSAnton Blanchard         bytes = (a->VsrB(i) << 8) +           /* extract adjacent bytes */
165563be02fcSAnton Blanchard             (((i + 1) < size) ? a->VsrB(i + 1) : 0);
165663be02fcSAnton Blanchard         r->VsrB(i) = (bytes << shift) >> 8;   /* shift and store result */
1657fcf5ef2aSThomas Huth     }
1658fcf5ef2aSThomas Huth }
1659fcf5ef2aSThomas Huth 
1660fcf5ef2aSThomas Huth void helper_vsrv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1661fcf5ef2aSThomas Huth {
1662fcf5ef2aSThomas Huth     int i;
1663fcf5ef2aSThomas Huth     unsigned int shift, bytes;
1664fcf5ef2aSThomas Huth 
1665b6cb41b2SDavid Gibson     /*
1666b6cb41b2SDavid Gibson      * Use reverse order, as destination and source register can be
1667b6cb41b2SDavid Gibson      * same. Its being modified in place saving temporary, reverse
1668b6cb41b2SDavid Gibson      * order will guarantee that computed result is not fed back.
1669fcf5ef2aSThomas Huth      */
1670fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
167163be02fcSAnton Blanchard         shift = b->VsrB(i) & 0x7;               /* extract shift value */
167263be02fcSAnton Blanchard         bytes = ((i ? a->VsrB(i - 1) : 0) << 8) + a->VsrB(i);
1673fcf5ef2aSThomas Huth                                                 /* extract adjacent bytes */
167463be02fcSAnton Blanchard         r->VsrB(i) = (bytes >> shift) & 0xFF;   /* shift and store result */
1675fcf5ef2aSThomas Huth     }
1676fcf5ef2aSThomas Huth }
1677fcf5ef2aSThomas Huth 
1678fcf5ef2aSThomas Huth void helper_vsldoi(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t shift)
1679fcf5ef2aSThomas Huth {
1680fcf5ef2aSThomas Huth     int sh = shift & 0xf;
1681fcf5ef2aSThomas Huth     int i;
1682fcf5ef2aSThomas Huth     ppc_avr_t result;
1683fcf5ef2aSThomas Huth 
1684fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1685fcf5ef2aSThomas Huth         int index = sh + i;
1686fcf5ef2aSThomas Huth         if (index > 0xf) {
168760594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index - 0x10);
1688fcf5ef2aSThomas Huth         } else {
168960594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1690fcf5ef2aSThomas Huth         }
1691fcf5ef2aSThomas Huth     }
1692fcf5ef2aSThomas Huth     *r = result;
1693fcf5ef2aSThomas Huth }
1694fcf5ef2aSThomas Huth 
1695fcf5ef2aSThomas Huth void helper_vslo(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1696fcf5ef2aSThomas Huth {
16973c385a93SMark Cave-Ayland     int sh = (b->VsrB(0xf) >> 3) & 0xf;
1698fcf5ef2aSThomas Huth 
1699e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1700fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1701fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
1702fcf5ef2aSThomas Huth #else
1703fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1704fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1705fcf5ef2aSThomas Huth #endif
1706fcf5ef2aSThomas Huth }
1707fcf5ef2aSThomas Huth 
1708e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
17092cc12af3SMatheus Ferst #define ELEM_ADDR(VEC, IDX, SIZE) (&(VEC)->u8[IDX])
17102cc12af3SMatheus Ferst #else
17112cc12af3SMatheus Ferst #define ELEM_ADDR(VEC, IDX, SIZE) (&(VEC)->u8[15 - (IDX)] - (SIZE) + 1)
17122cc12af3SMatheus Ferst #endif
17132cc12af3SMatheus Ferst 
17142cc12af3SMatheus Ferst #define VINSX(SUFFIX, TYPE) \
17152cc12af3SMatheus Ferst void glue(glue(helper_VINS, SUFFIX), LX)(CPUPPCState *env, ppc_avr_t *t,       \
17162cc12af3SMatheus Ferst                                          uint64_t val, target_ulong index)     \
17172cc12af3SMatheus Ferst {                                                                              \
17182cc12af3SMatheus Ferst     const int maxidx = ARRAY_SIZE(t->u8) - sizeof(TYPE);                       \
17192cc12af3SMatheus Ferst     target_long idx = index;                                                   \
17202cc12af3SMatheus Ferst                                                                                \
17212cc12af3SMatheus Ferst     if (idx < 0 || idx > maxidx) {                                             \
17222cc12af3SMatheus Ferst         idx =  idx < 0 ? sizeof(TYPE) - idx : idx;                             \
17232cc12af3SMatheus Ferst         qemu_log_mask(LOG_GUEST_ERROR,                                         \
17242cc12af3SMatheus Ferst             "Invalid index for Vector Insert Element after 0x" TARGET_FMT_lx   \
17252cc12af3SMatheus Ferst             ", RA = " TARGET_FMT_ld " > %d\n", env->nip, idx, maxidx);         \
17262cc12af3SMatheus Ferst     } else {                                                                   \
17272cc12af3SMatheus Ferst         TYPE src = val;                                                        \
17282cc12af3SMatheus Ferst         memcpy(ELEM_ADDR(t, idx, sizeof(TYPE)), &src, sizeof(TYPE));           \
17292cc12af3SMatheus Ferst     }                                                                          \
17302cc12af3SMatheus Ferst }
17312cc12af3SMatheus Ferst VINSX(B, uint8_t)
17322cc12af3SMatheus Ferst VINSX(H, uint16_t)
17332cc12af3SMatheus Ferst VINSX(W, uint32_t)
17342cc12af3SMatheus Ferst VINSX(D, uint64_t)
17352cc12af3SMatheus Ferst #undef ELEM_ADDR
17362cc12af3SMatheus Ferst #undef VINSX
1737e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
173828110b72SMatheus Ferst #define VEXTDVLX(NAME, SIZE) \
173928110b72SMatheus Ferst void helper_##NAME(CPUPPCState *env, ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, \
174028110b72SMatheus Ferst                    target_ulong index)                                         \
174128110b72SMatheus Ferst {                                                                              \
174228110b72SMatheus Ferst     const target_long idx = index;                                             \
174328110b72SMatheus Ferst     ppc_avr_t tmp[2] = { *a, *b };                                             \
174428110b72SMatheus Ferst     memset(t, 0, sizeof(*t));                                                  \
174528110b72SMatheus Ferst     if (idx >= 0 && idx + SIZE <= sizeof(tmp)) {                               \
174628110b72SMatheus Ferst         memcpy(&t->u8[ARRAY_SIZE(t->u8) / 2 - SIZE], (void *)tmp + idx, SIZE); \
174728110b72SMatheus Ferst     } else {                                                                   \
174828110b72SMatheus Ferst         qemu_log_mask(LOG_GUEST_ERROR, "Invalid index for " #NAME " after 0x"  \
174928110b72SMatheus Ferst                       TARGET_FMT_lx ", RC = " TARGET_FMT_ld " > %d\n",         \
175028110b72SMatheus Ferst                       env->nip, idx < 0 ? SIZE - idx : idx, 32 - SIZE);        \
175128110b72SMatheus Ferst     }                                                                          \
175228110b72SMatheus Ferst }
175328110b72SMatheus Ferst #else
175428110b72SMatheus Ferst #define VEXTDVLX(NAME, SIZE) \
175528110b72SMatheus Ferst void helper_##NAME(CPUPPCState *env, ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, \
175628110b72SMatheus Ferst                    target_ulong index)                                         \
175728110b72SMatheus Ferst {                                                                              \
175828110b72SMatheus Ferst     const target_long idx = index;                                             \
175928110b72SMatheus Ferst     ppc_avr_t tmp[2] = { *b, *a };                                             \
176028110b72SMatheus Ferst     memset(t, 0, sizeof(*t));                                                  \
176128110b72SMatheus Ferst     if (idx >= 0 && idx + SIZE <= sizeof(tmp)) {                               \
176228110b72SMatheus Ferst         memcpy(&t->u8[ARRAY_SIZE(t->u8) / 2],                                  \
176328110b72SMatheus Ferst                (void *)tmp + sizeof(tmp) - SIZE - idx, SIZE);                  \
176428110b72SMatheus Ferst     } else {                                                                   \
176528110b72SMatheus Ferst         qemu_log_mask(LOG_GUEST_ERROR, "Invalid index for " #NAME " after 0x"  \
176628110b72SMatheus Ferst                       TARGET_FMT_lx ", RC = " TARGET_FMT_ld " > %d\n",         \
176728110b72SMatheus Ferst                       env->nip, idx < 0 ? SIZE - idx : idx, 32 - SIZE);        \
176828110b72SMatheus Ferst     }                                                                          \
176928110b72SMatheus Ferst }
177028110b72SMatheus Ferst #endif
177128110b72SMatheus Ferst VEXTDVLX(VEXTDUBVLX, 1)
177228110b72SMatheus Ferst VEXTDVLX(VEXTDUHVLX, 2)
177328110b72SMatheus Ferst VEXTDVLX(VEXTDUWVLX, 4)
177428110b72SMatheus Ferst VEXTDVLX(VEXTDDVLX, 8)
177528110b72SMatheus Ferst #undef VEXTDVLX
1776e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1777fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1778fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1779fcf5ef2aSThomas Huth     {                                                                        \
1780fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1781fcf5ef2aSThomas Huth         memmove(&r->u8[8 - es], &b->u8[index], es);                          \
1782fcf5ef2aSThomas Huth         memset(&r->u8[8], 0, 8);                                             \
1783fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8 - es);                                        \
1784fcf5ef2aSThomas Huth     }
1785fcf5ef2aSThomas Huth #else
1786fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1787fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1788fcf5ef2aSThomas Huth     {                                                                        \
1789fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1790fcf5ef2aSThomas Huth         uint32_t s = (16 - index) - es;                                      \
1791fcf5ef2aSThomas Huth         memmove(&r->u8[8], &b->u8[s], es);                                   \
1792fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8);                                             \
1793fcf5ef2aSThomas Huth         memset(&r->u8[8 + es], 0, 8 - es);                                   \
1794fcf5ef2aSThomas Huth     }
1795fcf5ef2aSThomas Huth #endif
1796fcf5ef2aSThomas Huth VEXTRACT(ub, u8)
1797fcf5ef2aSThomas Huth VEXTRACT(uh, u16)
1798fcf5ef2aSThomas Huth VEXTRACT(uw, u32)
1799fcf5ef2aSThomas Huth VEXTRACT(d, u64)
1800fcf5ef2aSThomas Huth #undef VEXTRACT
1801fcf5ef2aSThomas Huth 
1802fb5303ccSMatheus Ferst #define VSTRI(NAME, ELEM, NUM_ELEMS, LEFT) \
1803fb5303ccSMatheus Ferst uint32_t helper_##NAME(ppc_avr_t *t, ppc_avr_t *b) \
1804fb5303ccSMatheus Ferst {                                                   \
1805fb5303ccSMatheus Ferst     int i, idx, crf = 0;                            \
1806fb5303ccSMatheus Ferst                                                     \
1807fb5303ccSMatheus Ferst     for (i = 0; i < NUM_ELEMS; i++) {               \
1808fb5303ccSMatheus Ferst         idx = LEFT ? i : NUM_ELEMS - i - 1;         \
1809fb5303ccSMatheus Ferst         if (b->Vsr##ELEM(idx)) {                    \
1810fb5303ccSMatheus Ferst             t->Vsr##ELEM(idx) = b->Vsr##ELEM(idx);  \
1811fb5303ccSMatheus Ferst         } else {                                    \
1812fb5303ccSMatheus Ferst             crf = 0b0010;                           \
1813fb5303ccSMatheus Ferst             break;                                  \
1814fb5303ccSMatheus Ferst         }                                           \
1815fb5303ccSMatheus Ferst     }                                               \
1816fb5303ccSMatheus Ferst                                                     \
1817fb5303ccSMatheus Ferst     for (; i < NUM_ELEMS; i++) {                    \
1818fb5303ccSMatheus Ferst         idx = LEFT ? i : NUM_ELEMS - i - 1;         \
1819fb5303ccSMatheus Ferst         t->Vsr##ELEM(idx) = 0;                      \
1820fb5303ccSMatheus Ferst     }                                               \
1821fb5303ccSMatheus Ferst                                                     \
1822fb5303ccSMatheus Ferst     return crf;                                     \
1823fb5303ccSMatheus Ferst }
1824fb5303ccSMatheus Ferst VSTRI(VSTRIBL, B, 16, true)
1825fb5303ccSMatheus Ferst VSTRI(VSTRIBR, B, 16, false)
1826fb5303ccSMatheus Ferst VSTRI(VSTRIHL, H, 8, true)
1827fb5303ccSMatheus Ferst VSTRI(VSTRIHR, H, 8, false)
1828fb5303ccSMatheus Ferst #undef VSTRI
1829fb5303ccSMatheus Ferst 
18308f5eeee3SMatheus Ferst void helper_XXEXTRACTUW(ppc_vsr_t *xt, ppc_vsr_t *xb, uint32_t index)
18318ad901e5SNikunj A Dadhania {
183203b32c09SMark Cave-Ayland     ppc_vsr_t t = { };
18338ad901e5SNikunj A Dadhania     size_t es = sizeof(uint32_t);
18348ad901e5SNikunj A Dadhania     uint32_t ext_index;
18358ad901e5SNikunj A Dadhania     int i;
18368ad901e5SNikunj A Dadhania 
18378ad901e5SNikunj A Dadhania     ext_index = index;
18388ad901e5SNikunj A Dadhania     for (i = 0; i < es; i++, ext_index++) {
183903b32c09SMark Cave-Ayland         t.VsrB(8 - es + i) = xb->VsrB(ext_index % 16);
18408ad901e5SNikunj A Dadhania     }
18418ad901e5SNikunj A Dadhania 
184203b32c09SMark Cave-Ayland     *xt = t;
18438ad901e5SNikunj A Dadhania }
18448ad901e5SNikunj A Dadhania 
18458f5eeee3SMatheus Ferst void helper_XXINSERTW(ppc_vsr_t *xt, ppc_vsr_t *xb, uint32_t index)
18463398b742SNikunj A Dadhania {
184703b32c09SMark Cave-Ayland     ppc_vsr_t t = *xt;
18483398b742SNikunj A Dadhania     size_t es = sizeof(uint32_t);
18493398b742SNikunj A Dadhania     int ins_index, i = 0;
18503398b742SNikunj A Dadhania 
18513398b742SNikunj A Dadhania     ins_index = index;
18523398b742SNikunj A Dadhania     for (i = 0; i < es && ins_index < 16; i++, ins_index++) {
185303b32c09SMark Cave-Ayland         t.VsrB(ins_index) = xb->VsrB(8 - es + i);
18543398b742SNikunj A Dadhania     }
18553398b742SNikunj A Dadhania 
185603b32c09SMark Cave-Ayland     *xt = t;
18573398b742SNikunj A Dadhania }
18583398b742SNikunj A Dadhania 
18591015fcabSMatheus Ferst void helper_XXEVAL(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c,
18601015fcabSMatheus Ferst                    uint32_t desc)
18611015fcabSMatheus Ferst {
18621015fcabSMatheus Ferst     /*
18631015fcabSMatheus Ferst      * Instead of processing imm bit-by-bit, we'll skip the computation of
18641015fcabSMatheus Ferst      * conjunctions whose corresponding bit is unset.
18651015fcabSMatheus Ferst      */
18661015fcabSMatheus Ferst     int bit, imm = simd_data(desc);
18671015fcabSMatheus Ferst     Int128 conj, disj = int128_zero();
18681015fcabSMatheus Ferst 
18691015fcabSMatheus Ferst     /* Iterate over set bits from the least to the most significant bit */
18701015fcabSMatheus Ferst     while (imm) {
18711015fcabSMatheus Ferst         /*
18721015fcabSMatheus Ferst          * Get the next bit to be processed with ctz64. Invert the result of
18731015fcabSMatheus Ferst          * ctz64 to match the indexing used by PowerISA.
18741015fcabSMatheus Ferst          */
18751015fcabSMatheus Ferst         bit = 7 - ctzl(imm);
18761015fcabSMatheus Ferst         if (bit & 0x4) {
18771015fcabSMatheus Ferst             conj = a->s128;
18781015fcabSMatheus Ferst         } else {
18791015fcabSMatheus Ferst             conj = int128_not(a->s128);
18801015fcabSMatheus Ferst         }
18811015fcabSMatheus Ferst         if (bit & 0x2) {
18821015fcabSMatheus Ferst             conj = int128_and(conj, b->s128);
18831015fcabSMatheus Ferst         } else {
18841015fcabSMatheus Ferst             conj = int128_and(conj, int128_not(b->s128));
18851015fcabSMatheus Ferst         }
18861015fcabSMatheus Ferst         if (bit & 0x1) {
18871015fcabSMatheus Ferst             conj = int128_and(conj, c->s128);
18881015fcabSMatheus Ferst         } else {
18891015fcabSMatheus Ferst             conj = int128_and(conj, int128_not(c->s128));
18901015fcabSMatheus Ferst         }
18911015fcabSMatheus Ferst         disj = int128_or(disj, conj);
18921015fcabSMatheus Ferst 
18931015fcabSMatheus Ferst         /* Unset the least significant bit that is set */
18941015fcabSMatheus Ferst         imm &= imm - 1;
18951015fcabSMatheus Ferst     }
18961015fcabSMatheus Ferst 
18971015fcabSMatheus Ferst     t->s128 = disj;
18981015fcabSMatheus Ferst }
18991015fcabSMatheus Ferst 
1900788c6399SMatheus Ferst #define XXBLEND(name, sz) \
1901788c6399SMatheus Ferst void glue(helper_XXBLENDV, name)(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b,  \
1902788c6399SMatheus Ferst                                  ppc_avr_t *c, uint32_t desc)               \
1903788c6399SMatheus Ferst {                                                                           \
1904788c6399SMatheus Ferst     for (int i = 0; i < ARRAY_SIZE(t->glue(u, sz)); i++) {                  \
1905788c6399SMatheus Ferst         t->glue(u, sz)[i] = (c->glue(s, sz)[i] >> (sz - 1)) ?               \
1906788c6399SMatheus Ferst             b->glue(u, sz)[i] : a->glue(u, sz)[i];                          \
1907788c6399SMatheus Ferst     }                                                                       \
1908788c6399SMatheus Ferst }
1909788c6399SMatheus Ferst XXBLEND(B, 8)
1910788c6399SMatheus Ferst XXBLEND(H, 16)
1911788c6399SMatheus Ferst XXBLEND(W, 32)
1912788c6399SMatheus Ferst XXBLEND(D, 64)
1913788c6399SMatheus Ferst #undef XXBLEND
1914788c6399SMatheus Ferst 
1915fcf5ef2aSThomas Huth void helper_vsro(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1916fcf5ef2aSThomas Huth {
19173c385a93SMark Cave-Ayland     int sh = (b->VsrB(0xf) >> 3) & 0xf;
1918fcf5ef2aSThomas Huth 
1919e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
1920fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1921fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1922fcf5ef2aSThomas Huth #else
1923fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1924fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
1925fcf5ef2aSThomas Huth #endif
1926fcf5ef2aSThomas Huth }
1927fcf5ef2aSThomas Huth 
1928fcf5ef2aSThomas Huth void helper_vsumsws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1929fcf5ef2aSThomas Huth {
1930fcf5ef2aSThomas Huth     int64_t t;
1931fcf5ef2aSThomas Huth     int i, upper;
1932fcf5ef2aSThomas Huth     ppc_avr_t result;
1933fcf5ef2aSThomas Huth     int sat = 0;
1934fcf5ef2aSThomas Huth 
1935fcf5ef2aSThomas Huth     upper = ARRAY_SIZE(r->s32) - 1;
193660594feaSMark Cave-Ayland     t = (int64_t)b->VsrSW(upper);
1937fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
193860594feaSMark Cave-Ayland         t += a->VsrSW(i);
193960594feaSMark Cave-Ayland         result.VsrSW(i) = 0;
1940fcf5ef2aSThomas Huth     }
194160594feaSMark Cave-Ayland     result.VsrSW(upper) = cvtsdsw(t, &sat);
1942fcf5ef2aSThomas Huth     *r = result;
1943fcf5ef2aSThomas Huth 
1944fcf5ef2aSThomas Huth     if (sat) {
19456175f5a0SRichard Henderson         set_vscr_sat(env);
1946fcf5ef2aSThomas Huth     }
1947fcf5ef2aSThomas Huth }
1948fcf5ef2aSThomas Huth 
1949fcf5ef2aSThomas Huth void helper_vsum2sws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1950fcf5ef2aSThomas Huth {
1951fcf5ef2aSThomas Huth     int i, j, upper;
1952fcf5ef2aSThomas Huth     ppc_avr_t result;
1953fcf5ef2aSThomas Huth     int sat = 0;
1954fcf5ef2aSThomas Huth 
1955fcf5ef2aSThomas Huth     upper = 1;
1956fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
195760594feaSMark Cave-Ayland         int64_t t = (int64_t)b->VsrSW(upper + i * 2);
1958fcf5ef2aSThomas Huth 
19597fa0ddc1SAnton Blanchard         result.VsrD(i) = 0;
1960fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u64); j++) {
196160594feaSMark Cave-Ayland             t += a->VsrSW(2 * i + j);
1962fcf5ef2aSThomas Huth         }
196360594feaSMark Cave-Ayland         result.VsrSW(upper + i * 2) = cvtsdsw(t, &sat);
1964fcf5ef2aSThomas Huth     }
1965fcf5ef2aSThomas Huth 
1966fcf5ef2aSThomas Huth     *r = result;
1967fcf5ef2aSThomas Huth     if (sat) {
19686175f5a0SRichard Henderson         set_vscr_sat(env);
1969fcf5ef2aSThomas Huth     }
1970fcf5ef2aSThomas Huth }
1971fcf5ef2aSThomas Huth 
1972fcf5ef2aSThomas Huth void helper_vsum4sbs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1973fcf5ef2aSThomas Huth {
1974fcf5ef2aSThomas Huth     int i, j;
1975fcf5ef2aSThomas Huth     int sat = 0;
1976fcf5ef2aSThomas Huth 
1977fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
1978fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
1979fcf5ef2aSThomas Huth 
1980fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->s32); j++) {
1981fcf5ef2aSThomas Huth             t += a->s8[4 * i + j];
1982fcf5ef2aSThomas Huth         }
1983fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
1984fcf5ef2aSThomas Huth     }
1985fcf5ef2aSThomas Huth 
1986fcf5ef2aSThomas Huth     if (sat) {
19876175f5a0SRichard Henderson         set_vscr_sat(env);
1988fcf5ef2aSThomas Huth     }
1989fcf5ef2aSThomas Huth }
1990fcf5ef2aSThomas Huth 
1991fcf5ef2aSThomas Huth void helper_vsum4shs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1992fcf5ef2aSThomas Huth {
1993fcf5ef2aSThomas Huth     int sat = 0;
1994fcf5ef2aSThomas Huth     int i;
1995fcf5ef2aSThomas Huth 
1996fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
1997fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
1998fcf5ef2aSThomas Huth 
1999fcf5ef2aSThomas Huth         t += a->s16[2 * i] + a->s16[2 * i + 1];
2000fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
2001fcf5ef2aSThomas Huth     }
2002fcf5ef2aSThomas Huth 
2003fcf5ef2aSThomas Huth     if (sat) {
20046175f5a0SRichard Henderson         set_vscr_sat(env);
2005fcf5ef2aSThomas Huth     }
2006fcf5ef2aSThomas Huth }
2007fcf5ef2aSThomas Huth 
2008fcf5ef2aSThomas Huth void helper_vsum4ubs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2009fcf5ef2aSThomas Huth {
2010fcf5ef2aSThomas Huth     int i, j;
2011fcf5ef2aSThomas Huth     int sat = 0;
2012fcf5ef2aSThomas Huth 
2013fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2014fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)b->u32[i];
2015fcf5ef2aSThomas Huth 
2016fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u32); j++) {
2017fcf5ef2aSThomas Huth             t += a->u8[4 * i + j];
2018fcf5ef2aSThomas Huth         }
2019fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
2020fcf5ef2aSThomas Huth     }
2021fcf5ef2aSThomas Huth 
2022fcf5ef2aSThomas Huth     if (sat) {
20236175f5a0SRichard Henderson         set_vscr_sat(env);
2024fcf5ef2aSThomas Huth     }
2025fcf5ef2aSThomas Huth }
2026fcf5ef2aSThomas Huth 
2027e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
2028fcf5ef2aSThomas Huth #define UPKHI 1
2029fcf5ef2aSThomas Huth #define UPKLO 0
2030fcf5ef2aSThomas Huth #else
2031fcf5ef2aSThomas Huth #define UPKHI 0
2032fcf5ef2aSThomas Huth #define UPKLO 1
2033fcf5ef2aSThomas Huth #endif
2034fcf5ef2aSThomas Huth #define VUPKPX(suffix, hi)                                              \
2035fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
2036fcf5ef2aSThomas Huth     {                                                                   \
2037fcf5ef2aSThomas Huth         int i;                                                          \
2038fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
2039fcf5ef2aSThomas Huth                                                                         \
2040fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->u32); i++) {                      \
2041fcf5ef2aSThomas Huth             uint16_t e = b->u16[hi ? i : i + 4];                        \
2042fcf5ef2aSThomas Huth             uint8_t a = (e >> 15) ? 0xff : 0;                           \
2043fcf5ef2aSThomas Huth             uint8_t r = (e >> 10) & 0x1f;                               \
2044fcf5ef2aSThomas Huth             uint8_t g = (e >> 5) & 0x1f;                                \
2045fcf5ef2aSThomas Huth             uint8_t b = e & 0x1f;                                       \
2046fcf5ef2aSThomas Huth                                                                         \
2047fcf5ef2aSThomas Huth             result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b;       \
2048fcf5ef2aSThomas Huth         }                                                               \
2049fcf5ef2aSThomas Huth         *r = result;                                                    \
2050fcf5ef2aSThomas Huth     }
2051fcf5ef2aSThomas Huth VUPKPX(lpx, UPKLO)
2052fcf5ef2aSThomas Huth VUPKPX(hpx, UPKHI)
2053fcf5ef2aSThomas Huth #undef VUPKPX
2054fcf5ef2aSThomas Huth 
2055fcf5ef2aSThomas Huth #define VUPK(suffix, unpacked, packee, hi)                              \
2056fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
2057fcf5ef2aSThomas Huth     {                                                                   \
2058fcf5ef2aSThomas Huth         int i;                                                          \
2059fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
2060fcf5ef2aSThomas Huth                                                                         \
2061fcf5ef2aSThomas Huth         if (hi) {                                                       \
2062fcf5ef2aSThomas Huth             for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) {             \
2063fcf5ef2aSThomas Huth                 result.unpacked[i] = b->packee[i];                      \
2064fcf5ef2aSThomas Huth             }                                                           \
2065fcf5ef2aSThomas Huth         } else {                                                        \
2066fcf5ef2aSThomas Huth             for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
2067fcf5ef2aSThomas Huth                  i++) {                                                 \
2068fcf5ef2aSThomas Huth                 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
2069fcf5ef2aSThomas Huth             }                                                           \
2070fcf5ef2aSThomas Huth         }                                                               \
2071fcf5ef2aSThomas Huth         *r = result;                                                    \
2072fcf5ef2aSThomas Huth     }
2073fcf5ef2aSThomas Huth VUPK(hsb, s16, s8, UPKHI)
2074fcf5ef2aSThomas Huth VUPK(hsh, s32, s16, UPKHI)
2075fcf5ef2aSThomas Huth VUPK(hsw, s64, s32, UPKHI)
2076fcf5ef2aSThomas Huth VUPK(lsb, s16, s8, UPKLO)
2077fcf5ef2aSThomas Huth VUPK(lsh, s32, s16, UPKLO)
2078fcf5ef2aSThomas Huth VUPK(lsw, s64, s32, UPKLO)
2079fcf5ef2aSThomas Huth #undef VUPK
2080fcf5ef2aSThomas Huth #undef UPKHI
2081fcf5ef2aSThomas Huth #undef UPKLO
2082fcf5ef2aSThomas Huth 
2083fcf5ef2aSThomas Huth #define VGENERIC_DO(name, element)                                      \
2084fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *b)                     \
2085fcf5ef2aSThomas Huth     {                                                                   \
2086fcf5ef2aSThomas Huth         int i;                                                          \
2087fcf5ef2aSThomas Huth                                                                         \
208860594feaSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2089fcf5ef2aSThomas Huth             r->element[i] = name(b->element[i]);                        \
2090fcf5ef2aSThomas Huth         }                                                               \
2091fcf5ef2aSThomas Huth     }
2092fcf5ef2aSThomas Huth 
2093fcf5ef2aSThomas Huth #define clzb(v) ((v) ? clz32((uint32_t)(v) << 24) : 8)
2094fcf5ef2aSThomas Huth #define clzh(v) ((v) ? clz32((uint32_t)(v) << 16) : 16)
2095fcf5ef2aSThomas Huth 
2096fcf5ef2aSThomas Huth VGENERIC_DO(clzb, u8)
2097fcf5ef2aSThomas Huth VGENERIC_DO(clzh, u16)
2098fcf5ef2aSThomas Huth 
2099fcf5ef2aSThomas Huth #undef clzb
2100fcf5ef2aSThomas Huth #undef clzh
2101fcf5ef2aSThomas Huth 
2102fcf5ef2aSThomas Huth #define ctzb(v) ((v) ? ctz32(v) : 8)
2103fcf5ef2aSThomas Huth #define ctzh(v) ((v) ? ctz32(v) : 16)
2104fcf5ef2aSThomas Huth #define ctzw(v) ctz32((v))
2105fcf5ef2aSThomas Huth #define ctzd(v) ctz64((v))
2106fcf5ef2aSThomas Huth 
2107fcf5ef2aSThomas Huth VGENERIC_DO(ctzb, u8)
2108fcf5ef2aSThomas Huth VGENERIC_DO(ctzh, u16)
2109fcf5ef2aSThomas Huth VGENERIC_DO(ctzw, u32)
2110fcf5ef2aSThomas Huth VGENERIC_DO(ctzd, u64)
2111fcf5ef2aSThomas Huth 
2112fcf5ef2aSThomas Huth #undef ctzb
2113fcf5ef2aSThomas Huth #undef ctzh
2114fcf5ef2aSThomas Huth #undef ctzw
2115fcf5ef2aSThomas Huth #undef ctzd
2116fcf5ef2aSThomas Huth 
2117fcf5ef2aSThomas Huth #define popcntb(v) ctpop8(v)
2118fcf5ef2aSThomas Huth #define popcnth(v) ctpop16(v)
2119fcf5ef2aSThomas Huth #define popcntw(v) ctpop32(v)
2120fcf5ef2aSThomas Huth #define popcntd(v) ctpop64(v)
2121fcf5ef2aSThomas Huth 
2122fcf5ef2aSThomas Huth VGENERIC_DO(popcntb, u8)
2123fcf5ef2aSThomas Huth VGENERIC_DO(popcnth, u16)
2124fcf5ef2aSThomas Huth VGENERIC_DO(popcntw, u32)
2125fcf5ef2aSThomas Huth VGENERIC_DO(popcntd, u64)
2126fcf5ef2aSThomas Huth 
2127fcf5ef2aSThomas Huth #undef popcntb
2128fcf5ef2aSThomas Huth #undef popcnth
2129fcf5ef2aSThomas Huth #undef popcntw
2130fcf5ef2aSThomas Huth #undef popcntd
2131fcf5ef2aSThomas Huth 
2132fcf5ef2aSThomas Huth #undef VGENERIC_DO
2133fcf5ef2aSThomas Huth 
21347ca04286SMatheus Ferst void helper_VADDUQM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2135fcf5ef2aSThomas Huth {
21367ca04286SMatheus Ferst     r->s128 = int128_add(a->s128, b->s128);
2137fcf5ef2aSThomas Huth }
2138fcf5ef2aSThomas Huth 
2139896d92c8SMatheus Ferst void helper_VADDEUQM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2140fcf5ef2aSThomas Huth {
2141896d92c8SMatheus Ferst     r->s128 = int128_add(int128_add(a->s128, b->s128),
2142896d92c8SMatheus Ferst                          int128_make64(int128_getlo(c->s128) & 1));
2143fcf5ef2aSThomas Huth }
2144fcf5ef2aSThomas Huth 
21458290ea50SMatheus Ferst void helper_VADDCUQ(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2146fcf5ef2aSThomas Huth {
21478290ea50SMatheus Ferst     r->VsrD(1) = int128_ult(int128_not(a->s128), b->s128);
21483c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
2149fcf5ef2aSThomas Huth }
2150fcf5ef2aSThomas Huth 
2151896d92c8SMatheus Ferst void helper_VADDECUQ(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2152fcf5ef2aSThomas Huth {
2153896d92c8SMatheus Ferst     bool carry_out = int128_ult(int128_not(a->s128), b->s128),
2154896d92c8SMatheus Ferst          carry_in = int128_getlo(c->s128) & 1;
2155fcf5ef2aSThomas Huth 
2156fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
2157896d92c8SMatheus Ferst         carry_out = (int128_nz(a->s128) || int128_nz(b->s128)) &&
2158896d92c8SMatheus Ferst                     int128_eq(int128_add(a->s128, b->s128), int128_makes64(-1));
2159fcf5ef2aSThomas Huth     }
2160896d92c8SMatheus Ferst 
21613c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
21623c385a93SMark Cave-Ayland     r->VsrD(1) = carry_out;
2163fcf5ef2aSThomas Huth }
2164fcf5ef2aSThomas Huth 
2165b132be53SMatheus Ferst void helper_VSUBUQM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2166fcf5ef2aSThomas Huth {
2167b132be53SMatheus Ferst     r->s128 = int128_sub(a->s128, b->s128);
2168fcf5ef2aSThomas Huth }
2169fcf5ef2aSThomas Huth 
2170e6a5ad43SMatheus Ferst void helper_VSUBEUQM(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2171fcf5ef2aSThomas Huth {
2172e6a5ad43SMatheus Ferst     r->s128 = int128_add(int128_add(a->s128, int128_not(b->s128)),
2173e6a5ad43SMatheus Ferst                          int128_make64(int128_getlo(c->s128) & 1));
2174fcf5ef2aSThomas Huth }
2175fcf5ef2aSThomas Huth 
2176b7d30faeSMatheus Ferst void helper_VSUBCUQ(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2177fcf5ef2aSThomas Huth {
2178b7d30faeSMatheus Ferst     Int128 tmp = int128_not(b->s128);
2179b7d30faeSMatheus Ferst 
2180b7d30faeSMatheus Ferst     r->VsrD(1) = int128_ult(int128_not(a->s128), tmp) ||
2181b7d30faeSMatheus Ferst                  int128_eq(int128_add(a->s128, tmp), int128_makes64(-1));
21823c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
2183fcf5ef2aSThomas Huth }
2184fcf5ef2aSThomas Huth 
2185e6a5ad43SMatheus Ferst void helper_VSUBECUQ(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2186fcf5ef2aSThomas Huth {
2187e6a5ad43SMatheus Ferst     Int128 tmp = int128_not(b->s128);
2188e6a5ad43SMatheus Ferst     bool carry_out = int128_ult(int128_not(a->s128), tmp),
2189e6a5ad43SMatheus Ferst          carry_in = int128_getlo(c->s128) & 1;
2190fcf5ef2aSThomas Huth 
2191e6a5ad43SMatheus Ferst     r->VsrD(1) = carry_out || (carry_in && int128_eq(int128_add(a->s128, tmp),
2192e6a5ad43SMatheus Ferst                                                      int128_makes64(-1)));
21933c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
2194fcf5ef2aSThomas Huth }
2195fcf5ef2aSThomas Huth 
2196fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_1 0xC
2197fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_2 0xF
2198fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_1  0xA
2199fcf5ef2aSThomas Huth #define BCD_NEG_PREF    0xD
2200fcf5ef2aSThomas Huth #define BCD_NEG_ALT     0xB
2201fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_2  0xE
2202fcf5ef2aSThomas Huth #define NATIONAL_PLUS   0x2B
2203fcf5ef2aSThomas Huth #define NATIONAL_NEG    0x2D
2204fcf5ef2aSThomas Huth 
2205365206aeSJose Ricardo Ziviani #define BCD_DIG_BYTE(n) (15 - ((n) / 2))
2206fcf5ef2aSThomas Huth 
2207fcf5ef2aSThomas Huth static int bcd_get_sgn(ppc_avr_t *bcd)
2208fcf5ef2aSThomas Huth {
2209428115c3SMark Cave-Ayland     switch (bcd->VsrB(BCD_DIG_BYTE(0)) & 0xF) {
2210fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_1:
2211fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_2:
2212fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_1:
2213fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_2:
2214fcf5ef2aSThomas Huth     {
2215fcf5ef2aSThomas Huth         return 1;
2216fcf5ef2aSThomas Huth     }
2217fcf5ef2aSThomas Huth 
2218fcf5ef2aSThomas Huth     case BCD_NEG_PREF:
2219fcf5ef2aSThomas Huth     case BCD_NEG_ALT:
2220fcf5ef2aSThomas Huth     {
2221fcf5ef2aSThomas Huth         return -1;
2222fcf5ef2aSThomas Huth     }
2223fcf5ef2aSThomas Huth 
2224fcf5ef2aSThomas Huth     default:
2225fcf5ef2aSThomas Huth     {
2226fcf5ef2aSThomas Huth         return 0;
2227fcf5ef2aSThomas Huth     }
2228fcf5ef2aSThomas Huth     }
2229fcf5ef2aSThomas Huth }
2230fcf5ef2aSThomas Huth 
2231fcf5ef2aSThomas Huth static int bcd_preferred_sgn(int sgn, int ps)
2232fcf5ef2aSThomas Huth {
2233fcf5ef2aSThomas Huth     if (sgn >= 0) {
2234fcf5ef2aSThomas Huth         return (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2;
2235fcf5ef2aSThomas Huth     } else {
2236fcf5ef2aSThomas Huth         return BCD_NEG_PREF;
2237fcf5ef2aSThomas Huth     }
2238fcf5ef2aSThomas Huth }
2239fcf5ef2aSThomas Huth 
2240fcf5ef2aSThomas Huth static uint8_t bcd_get_digit(ppc_avr_t *bcd, int n, int *invalid)
2241fcf5ef2aSThomas Huth {
2242fcf5ef2aSThomas Huth     uint8_t result;
2243fcf5ef2aSThomas Huth     if (n & 1) {
2244428115c3SMark Cave-Ayland         result = bcd->VsrB(BCD_DIG_BYTE(n)) >> 4;
2245fcf5ef2aSThomas Huth     } else {
2246428115c3SMark Cave-Ayland        result = bcd->VsrB(BCD_DIG_BYTE(n)) & 0xF;
2247fcf5ef2aSThomas Huth     }
2248fcf5ef2aSThomas Huth 
2249fcf5ef2aSThomas Huth     if (unlikely(result > 9)) {
2250fcf5ef2aSThomas Huth         *invalid = true;
2251fcf5ef2aSThomas Huth     }
2252fcf5ef2aSThomas Huth     return result;
2253fcf5ef2aSThomas Huth }
2254fcf5ef2aSThomas Huth 
2255fcf5ef2aSThomas Huth static void bcd_put_digit(ppc_avr_t *bcd, uint8_t digit, int n)
2256fcf5ef2aSThomas Huth {
2257fcf5ef2aSThomas Huth     if (n & 1) {
2258428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) &= 0x0F;
2259428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) |= (digit << 4);
2260fcf5ef2aSThomas Huth     } else {
2261428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) &= 0xF0;
2262428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) |= digit;
2263fcf5ef2aSThomas Huth     }
2264fcf5ef2aSThomas Huth }
2265fcf5ef2aSThomas Huth 
2266071663dfSJose Ricardo Ziviani static bool bcd_is_valid(ppc_avr_t *bcd)
2267071663dfSJose Ricardo Ziviani {
2268071663dfSJose Ricardo Ziviani     int i;
2269071663dfSJose Ricardo Ziviani     int invalid = 0;
2270071663dfSJose Ricardo Ziviani 
2271071663dfSJose Ricardo Ziviani     if (bcd_get_sgn(bcd) == 0) {
2272071663dfSJose Ricardo Ziviani         return false;
2273071663dfSJose Ricardo Ziviani     }
2274071663dfSJose Ricardo Ziviani 
2275071663dfSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2276071663dfSJose Ricardo Ziviani         bcd_get_digit(bcd, i, &invalid);
2277071663dfSJose Ricardo Ziviani         if (unlikely(invalid)) {
2278071663dfSJose Ricardo Ziviani             return false;
2279071663dfSJose Ricardo Ziviani         }
2280071663dfSJose Ricardo Ziviani     }
2281071663dfSJose Ricardo Ziviani     return true;
2282071663dfSJose Ricardo Ziviani }
2283071663dfSJose Ricardo Ziviani 
2284fcf5ef2aSThomas Huth static int bcd_cmp_zero(ppc_avr_t *bcd)
2285fcf5ef2aSThomas Huth {
22863c385a93SMark Cave-Ayland     if (bcd->VsrD(0) == 0 && (bcd->VsrD(1) >> 4) == 0) {
2287efa73196SNikunj A Dadhania         return CRF_EQ;
2288fcf5ef2aSThomas Huth     } else {
2289efa73196SNikunj A Dadhania         return (bcd_get_sgn(bcd) == 1) ? CRF_GT : CRF_LT;
2290fcf5ef2aSThomas Huth     }
2291fcf5ef2aSThomas Huth }
2292fcf5ef2aSThomas Huth 
2293fcf5ef2aSThomas Huth static uint16_t get_national_digit(ppc_avr_t *reg, int n)
2294fcf5ef2aSThomas Huth {
229560594feaSMark Cave-Ayland     return reg->VsrH(7 - n);
2296fcf5ef2aSThomas Huth }
2297fcf5ef2aSThomas Huth 
2298fcf5ef2aSThomas Huth static void set_national_digit(ppc_avr_t *reg, uint8_t val, int n)
2299fcf5ef2aSThomas Huth {
230060594feaSMark Cave-Ayland     reg->VsrH(7 - n) = val;
2301fcf5ef2aSThomas Huth }
2302fcf5ef2aSThomas Huth 
2303fcf5ef2aSThomas Huth static int bcd_cmp_mag(ppc_avr_t *a, ppc_avr_t *b)
2304fcf5ef2aSThomas Huth {
2305fcf5ef2aSThomas Huth     int i;
2306fcf5ef2aSThomas Huth     int invalid = 0;
2307fcf5ef2aSThomas Huth     for (i = 31; i > 0; i--) {
2308fcf5ef2aSThomas Huth         uint8_t dig_a = bcd_get_digit(a, i, &invalid);
2309fcf5ef2aSThomas Huth         uint8_t dig_b = bcd_get_digit(b, i, &invalid);
2310fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2311fcf5ef2aSThomas Huth             return 0; /* doesn't matter */
2312fcf5ef2aSThomas Huth         } else if (dig_a > dig_b) {
2313fcf5ef2aSThomas Huth             return 1;
2314fcf5ef2aSThomas Huth         } else if (dig_a < dig_b) {
2315fcf5ef2aSThomas Huth             return -1;
2316fcf5ef2aSThomas Huth         }
2317fcf5ef2aSThomas Huth     }
2318fcf5ef2aSThomas Huth 
2319fcf5ef2aSThomas Huth     return 0;
2320fcf5ef2aSThomas Huth }
2321fcf5ef2aSThomas Huth 
2322936fda4dSFabiano Rosas static int bcd_add_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2323fcf5ef2aSThomas Huth                        int *overflow)
2324fcf5ef2aSThomas Huth {
2325fcf5ef2aSThomas Huth     int carry = 0;
2326fcf5ef2aSThomas Huth     int i;
2327936fda4dSFabiano Rosas     int is_zero = 1;
2328936fda4dSFabiano Rosas 
2329fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2330fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) +
2331fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2332936fda4dSFabiano Rosas         is_zero &= (digit == 0);
2333fcf5ef2aSThomas Huth         if (digit > 9) {
2334fcf5ef2aSThomas Huth             carry = 1;
2335fcf5ef2aSThomas Huth             digit -= 10;
2336fcf5ef2aSThomas Huth         } else {
2337fcf5ef2aSThomas Huth             carry = 0;
2338fcf5ef2aSThomas Huth         }
2339fcf5ef2aSThomas Huth 
2340fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2341fcf5ef2aSThomas Huth     }
2342fcf5ef2aSThomas Huth 
2343fcf5ef2aSThomas Huth     *overflow = carry;
2344936fda4dSFabiano Rosas     return is_zero;
2345fcf5ef2aSThomas Huth }
2346fcf5ef2aSThomas Huth 
2347d03b174aSYasmin Beatriz static void bcd_sub_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2348fcf5ef2aSThomas Huth                        int *overflow)
2349fcf5ef2aSThomas Huth {
2350fcf5ef2aSThomas Huth     int carry = 0;
2351fcf5ef2aSThomas Huth     int i;
2352d03b174aSYasmin Beatriz 
2353fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2354fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) -
2355fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2356fcf5ef2aSThomas Huth         if (digit & 0x80) {
2357fcf5ef2aSThomas Huth             carry = -1;
2358fcf5ef2aSThomas Huth             digit += 10;
2359fcf5ef2aSThomas Huth         } else {
2360fcf5ef2aSThomas Huth             carry = 0;
2361fcf5ef2aSThomas Huth         }
2362fcf5ef2aSThomas Huth 
2363fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2364fcf5ef2aSThomas Huth     }
2365fcf5ef2aSThomas Huth 
2366fcf5ef2aSThomas Huth     *overflow = carry;
2367fcf5ef2aSThomas Huth }
2368fcf5ef2aSThomas Huth 
2369fcf5ef2aSThomas Huth uint32_t helper_bcdadd(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2370fcf5ef2aSThomas Huth {
2371fcf5ef2aSThomas Huth 
2372fcf5ef2aSThomas Huth     int sgna = bcd_get_sgn(a);
2373fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2374fcf5ef2aSThomas Huth     int invalid = (sgna == 0) || (sgnb == 0);
2375fcf5ef2aSThomas Huth     int overflow = 0;
2376936fda4dSFabiano Rosas     int zero = 0;
2377fcf5ef2aSThomas Huth     uint32_t cr = 0;
2378fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
2379fcf5ef2aSThomas Huth 
2380fcf5ef2aSThomas Huth     if (!invalid) {
2381fcf5ef2aSThomas Huth         if (sgna == sgnb) {
2382428115c3SMark Cave-Ayland             result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna, ps);
2383936fda4dSFabiano Rosas             zero = bcd_add_mag(&result, a, b, &invalid, &overflow);
2384936fda4dSFabiano Rosas             cr = (sgna > 0) ? CRF_GT : CRF_LT;
2385fcf5ef2aSThomas Huth         } else {
2386d03b174aSYasmin Beatriz             int magnitude = bcd_cmp_mag(a, b);
2387d03b174aSYasmin Beatriz             if (magnitude > 0) {
2388428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna, ps);
2389d03b174aSYasmin Beatriz                 bcd_sub_mag(&result, a, b, &invalid, &overflow);
2390d03b174aSYasmin Beatriz                 cr = (sgna > 0) ? CRF_GT : CRF_LT;
2391d03b174aSYasmin Beatriz             } else if (magnitude < 0) {
2392428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgnb, ps);
2393d03b174aSYasmin Beatriz                 bcd_sub_mag(&result, b, a, &invalid, &overflow);
2394efa73196SNikunj A Dadhania                 cr = (sgnb > 0) ? CRF_GT : CRF_LT;
2395d03b174aSYasmin Beatriz             } else {
2396428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(0, ps);
2397d03b174aSYasmin Beatriz                 cr = CRF_EQ;
2398d03b174aSYasmin Beatriz             }
2399fcf5ef2aSThomas Huth         }
2400fcf5ef2aSThomas Huth     }
2401fcf5ef2aSThomas Huth 
2402fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
24033c385a93SMark Cave-Ayland         result.VsrD(0) = result.VsrD(1) = -1;
2404efa73196SNikunj A Dadhania         cr = CRF_SO;
2405fcf5ef2aSThomas Huth     } else if (overflow) {
2406efa73196SNikunj A Dadhania         cr |= CRF_SO;
2407936fda4dSFabiano Rosas     } else if (zero) {
2408936fda4dSFabiano Rosas         cr |= CRF_EQ;
2409fcf5ef2aSThomas Huth     }
2410fcf5ef2aSThomas Huth 
2411fcf5ef2aSThomas Huth     *r = result;
2412fcf5ef2aSThomas Huth 
2413fcf5ef2aSThomas Huth     return cr;
2414fcf5ef2aSThomas Huth }
2415fcf5ef2aSThomas Huth 
2416fcf5ef2aSThomas Huth uint32_t helper_bcdsub(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2417fcf5ef2aSThomas Huth {
2418fcf5ef2aSThomas Huth     ppc_avr_t bcopy = *b;
2419fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2420fcf5ef2aSThomas Huth     if (sgnb < 0) {
2421fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_PLUS_PREF_1, 0);
2422fcf5ef2aSThomas Huth     } else if (sgnb > 0) {
2423fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_NEG_PREF, 0);
2424fcf5ef2aSThomas Huth     }
2425fcf5ef2aSThomas Huth     /* else invalid ... defer to bcdadd code for proper handling */
2426fcf5ef2aSThomas Huth 
2427fcf5ef2aSThomas Huth     return helper_bcdadd(r, a, &bcopy, ps);
2428fcf5ef2aSThomas Huth }
2429fcf5ef2aSThomas Huth 
2430fcf5ef2aSThomas Huth uint32_t helper_bcdcfn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2431fcf5ef2aSThomas Huth {
2432fcf5ef2aSThomas Huth     int i;
2433fcf5ef2aSThomas Huth     int cr = 0;
2434fcf5ef2aSThomas Huth     uint16_t national = 0;
2435fcf5ef2aSThomas Huth     uint16_t sgnb = get_national_digit(b, 0);
2436fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2437fcf5ef2aSThomas Huth     int invalid = (sgnb != NATIONAL_PLUS && sgnb != NATIONAL_NEG);
2438fcf5ef2aSThomas Huth 
2439fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2440fcf5ef2aSThomas Huth         national = get_national_digit(b, i);
2441fcf5ef2aSThomas Huth         if (unlikely(national < 0x30 || national > 0x39)) {
2442fcf5ef2aSThomas Huth             invalid = 1;
2443fcf5ef2aSThomas Huth             break;
2444fcf5ef2aSThomas Huth         }
2445fcf5ef2aSThomas Huth 
2446fcf5ef2aSThomas Huth         bcd_put_digit(&ret, national & 0xf, i);
2447fcf5ef2aSThomas Huth     }
2448fcf5ef2aSThomas Huth 
2449fcf5ef2aSThomas Huth     if (sgnb == NATIONAL_PLUS) {
2450fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2, 0);
2451fcf5ef2aSThomas Huth     } else {
2452fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2453fcf5ef2aSThomas Huth     }
2454fcf5ef2aSThomas Huth 
2455fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2456fcf5ef2aSThomas Huth 
2457fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2458efa73196SNikunj A Dadhania         cr = CRF_SO;
2459fcf5ef2aSThomas Huth     }
2460fcf5ef2aSThomas Huth 
2461fcf5ef2aSThomas Huth     *r = ret;
2462fcf5ef2aSThomas Huth 
2463fcf5ef2aSThomas Huth     return cr;
2464fcf5ef2aSThomas Huth }
2465fcf5ef2aSThomas Huth 
2466fcf5ef2aSThomas Huth uint32_t helper_bcdctn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2467fcf5ef2aSThomas Huth {
2468fcf5ef2aSThomas Huth     int i;
2469fcf5ef2aSThomas Huth     int cr = 0;
2470fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2471fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2472fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2473fcf5ef2aSThomas Huth 
24743c385a93SMark Cave-Ayland     int ox_flag = (b->VsrD(0) != 0) || ((b->VsrD(1) >> 32) != 0);
2475fcf5ef2aSThomas Huth 
2476fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2477fcf5ef2aSThomas Huth         set_national_digit(&ret, 0x30 + bcd_get_digit(b, i, &invalid), i);
2478fcf5ef2aSThomas Huth 
2479fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2480fcf5ef2aSThomas Huth             break;
2481fcf5ef2aSThomas Huth         }
2482fcf5ef2aSThomas Huth     }
2483fcf5ef2aSThomas Huth     set_national_digit(&ret, (sgnb == -1) ? NATIONAL_NEG : NATIONAL_PLUS, 0);
2484fcf5ef2aSThomas Huth 
2485fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2486fcf5ef2aSThomas Huth 
2487fcf5ef2aSThomas Huth     if (ox_flag) {
2488efa73196SNikunj A Dadhania         cr |= CRF_SO;
2489fcf5ef2aSThomas Huth     }
2490fcf5ef2aSThomas Huth 
2491fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2492efa73196SNikunj A Dadhania         cr = CRF_SO;
2493fcf5ef2aSThomas Huth     }
2494fcf5ef2aSThomas Huth 
2495fcf5ef2aSThomas Huth     *r = ret;
2496fcf5ef2aSThomas Huth 
2497fcf5ef2aSThomas Huth     return cr;
2498fcf5ef2aSThomas Huth }
2499fcf5ef2aSThomas Huth 
2500fcf5ef2aSThomas Huth uint32_t helper_bcdcfz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2501fcf5ef2aSThomas Huth {
2502fcf5ef2aSThomas Huth     int i;
2503fcf5ef2aSThomas Huth     int cr = 0;
2504fcf5ef2aSThomas Huth     int invalid = 0;
2505fcf5ef2aSThomas Huth     int zone_digit = 0;
2506fcf5ef2aSThomas Huth     int zone_lead = ps ? 0xF : 0x3;
2507fcf5ef2aSThomas Huth     int digit = 0;
2508fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2509428115c3SMark Cave-Ayland     int sgnb = b->VsrB(BCD_DIG_BYTE(0)) >> 4;
2510fcf5ef2aSThomas Huth 
2511fcf5ef2aSThomas Huth     if (unlikely((sgnb < 0xA) && ps)) {
2512fcf5ef2aSThomas Huth         invalid = 1;
2513fcf5ef2aSThomas Huth     }
2514fcf5ef2aSThomas Huth 
2515fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2516428115c3SMark Cave-Ayland         zone_digit = i ? b->VsrB(BCD_DIG_BYTE(i * 2)) >> 4 : zone_lead;
2517428115c3SMark Cave-Ayland         digit = b->VsrB(BCD_DIG_BYTE(i * 2)) & 0xF;
2518fcf5ef2aSThomas Huth         if (unlikely(zone_digit != zone_lead || digit > 0x9)) {
2519fcf5ef2aSThomas Huth             invalid = 1;
2520fcf5ef2aSThomas Huth             break;
2521fcf5ef2aSThomas Huth         }
2522fcf5ef2aSThomas Huth 
2523fcf5ef2aSThomas Huth         bcd_put_digit(&ret, digit, i + 1);
2524fcf5ef2aSThomas Huth     }
2525fcf5ef2aSThomas Huth 
2526fcf5ef2aSThomas Huth     if ((ps && (sgnb == 0xB || sgnb == 0xD)) ||
2527fcf5ef2aSThomas Huth             (!ps && (sgnb & 0x4))) {
2528fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2529fcf5ef2aSThomas Huth     } else {
2530fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_PLUS_PREF_1, 0);
2531fcf5ef2aSThomas Huth     }
2532fcf5ef2aSThomas Huth 
2533fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2534fcf5ef2aSThomas Huth 
2535fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2536efa73196SNikunj A Dadhania         cr = CRF_SO;
2537fcf5ef2aSThomas Huth     }
2538fcf5ef2aSThomas Huth 
2539fcf5ef2aSThomas Huth     *r = ret;
2540fcf5ef2aSThomas Huth 
2541fcf5ef2aSThomas Huth     return cr;
2542fcf5ef2aSThomas Huth }
2543fcf5ef2aSThomas Huth 
2544fcf5ef2aSThomas Huth uint32_t helper_bcdctz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2545fcf5ef2aSThomas Huth {
2546fcf5ef2aSThomas Huth     int i;
2547fcf5ef2aSThomas Huth     int cr = 0;
2548fcf5ef2aSThomas Huth     uint8_t digit = 0;
2549fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2550fcf5ef2aSThomas Huth     int zone_lead = (ps) ? 0xF0 : 0x30;
2551fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2552fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2553fcf5ef2aSThomas Huth 
25543c385a93SMark Cave-Ayland     int ox_flag = ((b->VsrD(0) >> 4) != 0);
2555fcf5ef2aSThomas Huth 
2556fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2557fcf5ef2aSThomas Huth         digit = bcd_get_digit(b, i + 1, &invalid);
2558fcf5ef2aSThomas Huth 
2559fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2560fcf5ef2aSThomas Huth             break;
2561fcf5ef2aSThomas Huth         }
2562fcf5ef2aSThomas Huth 
2563428115c3SMark Cave-Ayland         ret.VsrB(BCD_DIG_BYTE(i * 2)) = zone_lead + digit;
2564fcf5ef2aSThomas Huth     }
2565fcf5ef2aSThomas Huth 
2566fcf5ef2aSThomas Huth     if (ps) {
2567fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0xC : 0xD, 1);
2568fcf5ef2aSThomas Huth     } else {
2569fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0x3 : 0x7, 1);
2570fcf5ef2aSThomas Huth     }
2571fcf5ef2aSThomas Huth 
2572fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2573fcf5ef2aSThomas Huth 
2574fcf5ef2aSThomas Huth     if (ox_flag) {
2575efa73196SNikunj A Dadhania         cr |= CRF_SO;
2576fcf5ef2aSThomas Huth     }
2577fcf5ef2aSThomas Huth 
2578fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2579efa73196SNikunj A Dadhania         cr = CRF_SO;
2580fcf5ef2aSThomas Huth     }
2581fcf5ef2aSThomas Huth 
2582fcf5ef2aSThomas Huth     *r = ret;
2583fcf5ef2aSThomas Huth 
2584fcf5ef2aSThomas Huth     return cr;
2585fcf5ef2aSThomas Huth }
2586fcf5ef2aSThomas Huth 
2587a3d67f3eSLuis Pires /**
2588a3d67f3eSLuis Pires  * Compare 2 128-bit unsigned integers, passed in as unsigned 64-bit pairs
2589a3d67f3eSLuis Pires  *
2590a3d67f3eSLuis Pires  * Returns:
2591a3d67f3eSLuis Pires  * > 0 if ahi|alo > bhi|blo,
2592a3d67f3eSLuis Pires  * 0 if ahi|alo == bhi|blo,
2593a3d67f3eSLuis Pires  * < 0 if ahi|alo < bhi|blo
2594a3d67f3eSLuis Pires  */
2595a3d67f3eSLuis Pires static inline int ucmp128(uint64_t alo, uint64_t ahi,
2596a3d67f3eSLuis Pires                           uint64_t blo, uint64_t bhi)
2597a3d67f3eSLuis Pires {
2598a3d67f3eSLuis Pires     return (ahi == bhi) ?
2599a3d67f3eSLuis Pires         (alo > blo ? 1 : (alo == blo ? 0 : -1)) :
2600a3d67f3eSLuis Pires         (ahi > bhi ? 1 : -1);
2601a3d67f3eSLuis Pires }
2602a3d67f3eSLuis Pires 
2603a406c058SJose Ricardo Ziviani uint32_t helper_bcdcfsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2604a406c058SJose Ricardo Ziviani {
2605a406c058SJose Ricardo Ziviani     int i;
2606a3d67f3eSLuis Pires     int cr;
2607a406c058SJose Ricardo Ziviani     uint64_t lo_value;
2608a406c058SJose Ricardo Ziviani     uint64_t hi_value;
260940f3e79aSLuis Pires     uint64_t rem;
2610a406c058SJose Ricardo Ziviani     ppc_avr_t ret = { .u64 = { 0, 0 } };
2611a406c058SJose Ricardo Ziviani 
26123c385a93SMark Cave-Ayland     if (b->VsrSD(0) < 0) {
26133c385a93SMark Cave-Ayland         lo_value = -b->VsrSD(1);
26143c385a93SMark Cave-Ayland         hi_value = ~b->VsrD(0) + !lo_value;
2615a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, 0xD, 0);
2616a3d67f3eSLuis Pires 
2617a3d67f3eSLuis Pires         cr = CRF_LT;
2618a406c058SJose Ricardo Ziviani     } else {
26193c385a93SMark Cave-Ayland         lo_value = b->VsrD(1);
26203c385a93SMark Cave-Ayland         hi_value = b->VsrD(0);
2621a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, bcd_preferred_sgn(0, ps), 0);
2622a3d67f3eSLuis Pires 
2623a3d67f3eSLuis Pires         if (hi_value == 0 && lo_value == 0) {
2624a3d67f3eSLuis Pires             cr = CRF_EQ;
2625a3d67f3eSLuis Pires         } else {
2626a3d67f3eSLuis Pires             cr = CRF_GT;
2627a3d67f3eSLuis Pires         }
2628a406c058SJose Ricardo Ziviani     }
2629a406c058SJose Ricardo Ziviani 
2630a3d67f3eSLuis Pires     /*
2631a3d67f3eSLuis Pires      * Check src limits: abs(src) <= 10^31 - 1
2632a3d67f3eSLuis Pires      *
2633a3d67f3eSLuis Pires      * 10^31 - 1 = 0x0000007e37be2022 c0914b267fffffff
2634a3d67f3eSLuis Pires      */
2635a3d67f3eSLuis Pires     if (ucmp128(lo_value, hi_value,
2636a3d67f3eSLuis Pires                 0xc0914b267fffffffULL, 0x7e37be2022ULL) > 0) {
2637a3d67f3eSLuis Pires         cr |= CRF_SO;
2638a3d67f3eSLuis Pires 
2639a3d67f3eSLuis Pires         /*
2640a3d67f3eSLuis Pires          * According to the ISA, if src wouldn't fit in the destination
2641a3d67f3eSLuis Pires          * register, the result is undefined.
2642a3d67f3eSLuis Pires          * In that case, we leave r unchanged.
2643a3d67f3eSLuis Pires          */
2644a3d67f3eSLuis Pires     } else {
264540f3e79aSLuis Pires         rem = divu128(&lo_value, &hi_value, 1000000000000000ULL);
2646a406c058SJose Ricardo Ziviani 
264740f3e79aSLuis Pires         for (i = 1; i < 16; rem /= 10, i++) {
264840f3e79aSLuis Pires             bcd_put_digit(&ret, rem % 10, i);
2649a406c058SJose Ricardo Ziviani         }
2650a406c058SJose Ricardo Ziviani 
2651a406c058SJose Ricardo Ziviani         for (; i < 32; lo_value /= 10, i++) {
2652a406c058SJose Ricardo Ziviani             bcd_put_digit(&ret, lo_value % 10, i);
2653a406c058SJose Ricardo Ziviani         }
2654a406c058SJose Ricardo Ziviani 
2655a406c058SJose Ricardo Ziviani         *r = ret;
2656a3d67f3eSLuis Pires     }
2657a406c058SJose Ricardo Ziviani 
2658a406c058SJose Ricardo Ziviani     return cr;
2659a406c058SJose Ricardo Ziviani }
2660a406c058SJose Ricardo Ziviani 
2661c85bc7ddSJose Ricardo Ziviani uint32_t helper_bcdctsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2662c85bc7ddSJose Ricardo Ziviani {
2663c85bc7ddSJose Ricardo Ziviani     uint8_t i;
2664c85bc7ddSJose Ricardo Ziviani     int cr;
2665c85bc7ddSJose Ricardo Ziviani     uint64_t carry;
2666c85bc7ddSJose Ricardo Ziviani     uint64_t unused;
2667c85bc7ddSJose Ricardo Ziviani     uint64_t lo_value;
2668c85bc7ddSJose Ricardo Ziviani     uint64_t hi_value = 0;
2669c85bc7ddSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2670c85bc7ddSJose Ricardo Ziviani     int invalid = (sgnb == 0);
2671c85bc7ddSJose Ricardo Ziviani 
2672c85bc7ddSJose Ricardo Ziviani     lo_value = bcd_get_digit(b, 31, &invalid);
2673c85bc7ddSJose Ricardo Ziviani     for (i = 30; i > 0; i--) {
2674c85bc7ddSJose Ricardo Ziviani         mulu64(&lo_value, &carry, lo_value, 10ULL);
2675c85bc7ddSJose Ricardo Ziviani         mulu64(&hi_value, &unused, hi_value, 10ULL);
2676c85bc7ddSJose Ricardo Ziviani         lo_value += bcd_get_digit(b, i, &invalid);
2677c85bc7ddSJose Ricardo Ziviani         hi_value += carry;
2678c85bc7ddSJose Ricardo Ziviani 
2679c85bc7ddSJose Ricardo Ziviani         if (unlikely(invalid)) {
2680c85bc7ddSJose Ricardo Ziviani             break;
2681c85bc7ddSJose Ricardo Ziviani         }
2682c85bc7ddSJose Ricardo Ziviani     }
2683c85bc7ddSJose Ricardo Ziviani 
2684c85bc7ddSJose Ricardo Ziviani     if (sgnb == -1) {
26853c385a93SMark Cave-Ayland         r->VsrSD(1) = -lo_value;
26863c385a93SMark Cave-Ayland         r->VsrSD(0) = ~hi_value + !r->VsrSD(1);
2687c85bc7ddSJose Ricardo Ziviani     } else {
26883c385a93SMark Cave-Ayland         r->VsrSD(1) = lo_value;
26893c385a93SMark Cave-Ayland         r->VsrSD(0) = hi_value;
2690c85bc7ddSJose Ricardo Ziviani     }
2691c85bc7ddSJose Ricardo Ziviani 
2692c85bc7ddSJose Ricardo Ziviani     cr = bcd_cmp_zero(b);
2693c85bc7ddSJose Ricardo Ziviani 
2694c85bc7ddSJose Ricardo Ziviani     if (unlikely(invalid)) {
2695c85bc7ddSJose Ricardo Ziviani         cr = CRF_SO;
2696c85bc7ddSJose Ricardo Ziviani     }
2697c85bc7ddSJose Ricardo Ziviani 
2698c85bc7ddSJose Ricardo Ziviani     return cr;
2699c85bc7ddSJose Ricardo Ziviani }
2700c85bc7ddSJose Ricardo Ziviani 
2701c3025c3bSJose Ricardo Ziviani uint32_t helper_bcdcpsgn(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2702c3025c3bSJose Ricardo Ziviani {
2703c3025c3bSJose Ricardo Ziviani     int i;
2704c3025c3bSJose Ricardo Ziviani     int invalid = 0;
2705c3025c3bSJose Ricardo Ziviani 
2706c3025c3bSJose Ricardo Ziviani     if (bcd_get_sgn(a) == 0 || bcd_get_sgn(b) == 0) {
2707c3025c3bSJose Ricardo Ziviani         return CRF_SO;
2708c3025c3bSJose Ricardo Ziviani     }
2709c3025c3bSJose Ricardo Ziviani 
2710c3025c3bSJose Ricardo Ziviani     *r = *a;
2711428115c3SMark Cave-Ayland     bcd_put_digit(r, b->VsrB(BCD_DIG_BYTE(0)) & 0xF, 0);
2712c3025c3bSJose Ricardo Ziviani 
2713c3025c3bSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2714c3025c3bSJose Ricardo Ziviani         bcd_get_digit(a, i, &invalid);
2715c3025c3bSJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
2716c3025c3bSJose Ricardo Ziviani         if (unlikely(invalid)) {
2717c3025c3bSJose Ricardo Ziviani             return CRF_SO;
2718c3025c3bSJose Ricardo Ziviani         }
2719c3025c3bSJose Ricardo Ziviani     }
2720c3025c3bSJose Ricardo Ziviani 
2721c3025c3bSJose Ricardo Ziviani     return bcd_cmp_zero(r);
2722c3025c3bSJose Ricardo Ziviani }
2723c3025c3bSJose Ricardo Ziviani 
2724466a3f9cSJose Ricardo Ziviani uint32_t helper_bcdsetsgn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2725466a3f9cSJose Ricardo Ziviani {
2726466a3f9cSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2727466a3f9cSJose Ricardo Ziviani 
2728466a3f9cSJose Ricardo Ziviani     *r = *b;
2729466a3f9cSJose Ricardo Ziviani     bcd_put_digit(r, bcd_preferred_sgn(sgnb, ps), 0);
2730466a3f9cSJose Ricardo Ziviani 
2731071663dfSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2732466a3f9cSJose Ricardo Ziviani         return CRF_SO;
2733466a3f9cSJose Ricardo Ziviani     }
2734466a3f9cSJose Ricardo Ziviani 
2735466a3f9cSJose Ricardo Ziviani     return bcd_cmp_zero(r);
2736466a3f9cSJose Ricardo Ziviani }
2737466a3f9cSJose Ricardo Ziviani 
2738e04797f7SJose Ricardo Ziviani uint32_t helper_bcds(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2739e04797f7SJose Ricardo Ziviani {
2740e04797f7SJose Ricardo Ziviani     int cr;
2741428115c3SMark Cave-Ayland     int i = a->VsrSB(7);
2742e04797f7SJose Ricardo Ziviani     bool ox_flag = false;
2743e04797f7SJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2744e04797f7SJose Ricardo Ziviani     ppc_avr_t ret = *b;
27453c385a93SMark Cave-Ayland     ret.VsrD(1) &= ~0xf;
2746e04797f7SJose Ricardo Ziviani 
2747e04797f7SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2748e04797f7SJose Ricardo Ziviani         return CRF_SO;
2749e04797f7SJose Ricardo Ziviani     }
2750e04797f7SJose Ricardo Ziviani 
2751e04797f7SJose Ricardo Ziviani     if (unlikely(i > 31)) {
2752e04797f7SJose Ricardo Ziviani         i = 31;
2753e04797f7SJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
2754e04797f7SJose Ricardo Ziviani         i = -31;
2755e04797f7SJose Ricardo Ziviani     }
2756e04797f7SJose Ricardo Ziviani 
2757e04797f7SJose Ricardo Ziviani     if (i > 0) {
27583c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2759e04797f7SJose Ricardo Ziviani     } else {
27603c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2761e04797f7SJose Ricardo Ziviani     }
2762e04797f7SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
2763e04797f7SJose Ricardo Ziviani 
2764e04797f7SJose Ricardo Ziviani     *r = ret;
2765e04797f7SJose Ricardo Ziviani 
2766e04797f7SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
2767e04797f7SJose Ricardo Ziviani     if (ox_flag) {
2768e04797f7SJose Ricardo Ziviani         cr |= CRF_SO;
2769e04797f7SJose Ricardo Ziviani     }
2770e04797f7SJose Ricardo Ziviani 
2771e04797f7SJose Ricardo Ziviani     return cr;
2772e04797f7SJose Ricardo Ziviani }
2773e04797f7SJose Ricardo Ziviani 
2774a49a95e9SJose Ricardo Ziviani uint32_t helper_bcdus(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2775a49a95e9SJose Ricardo Ziviani {
2776a49a95e9SJose Ricardo Ziviani     int cr;
2777a49a95e9SJose Ricardo Ziviani     int i;
2778a49a95e9SJose Ricardo Ziviani     int invalid = 0;
2779a49a95e9SJose Ricardo Ziviani     bool ox_flag = false;
2780a49a95e9SJose Ricardo Ziviani     ppc_avr_t ret = *b;
2781a49a95e9SJose Ricardo Ziviani 
2782a49a95e9SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
2783a49a95e9SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
2784a49a95e9SJose Ricardo Ziviani 
2785a49a95e9SJose Ricardo Ziviani         if (unlikely(invalid)) {
2786a49a95e9SJose Ricardo Ziviani             return CRF_SO;
2787a49a95e9SJose Ricardo Ziviani         }
2788a49a95e9SJose Ricardo Ziviani     }
2789a49a95e9SJose Ricardo Ziviani 
2790428115c3SMark Cave-Ayland     i = a->VsrSB(7);
2791a49a95e9SJose Ricardo Ziviani     if (i >= 32) {
2792a49a95e9SJose Ricardo Ziviani         ox_flag = true;
27933c385a93SMark Cave-Ayland         ret.VsrD(1) = ret.VsrD(0) = 0;
2794a49a95e9SJose Ricardo Ziviani     } else if (i <= -32) {
27953c385a93SMark Cave-Ayland         ret.VsrD(1) = ret.VsrD(0) = 0;
2796a49a95e9SJose Ricardo Ziviani     } else if (i > 0) {
27973c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2798a49a95e9SJose Ricardo Ziviani     } else {
27993c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2800a49a95e9SJose Ricardo Ziviani     }
2801a49a95e9SJose Ricardo Ziviani     *r = ret;
2802a49a95e9SJose Ricardo Ziviani 
2803a49a95e9SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
2804a49a95e9SJose Ricardo Ziviani     if (ox_flag) {
2805a49a95e9SJose Ricardo Ziviani         cr |= CRF_SO;
2806a49a95e9SJose Ricardo Ziviani     }
2807a49a95e9SJose Ricardo Ziviani 
2808a49a95e9SJose Ricardo Ziviani     return cr;
2809a49a95e9SJose Ricardo Ziviani }
2810a49a95e9SJose Ricardo Ziviani 
2811a54238adSJose Ricardo Ziviani uint32_t helper_bcdsr(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2812a54238adSJose Ricardo Ziviani {
2813a54238adSJose Ricardo Ziviani     int cr;
2814a54238adSJose Ricardo Ziviani     int unused = 0;
2815a54238adSJose Ricardo Ziviani     int invalid = 0;
2816a54238adSJose Ricardo Ziviani     bool ox_flag = false;
2817a54238adSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2818a54238adSJose Ricardo Ziviani     ppc_avr_t ret = *b;
28193c385a93SMark Cave-Ayland     ret.VsrD(1) &= ~0xf;
2820a54238adSJose Ricardo Ziviani 
2821428115c3SMark Cave-Ayland     int i = a->VsrSB(7);
2822428115c3SMark Cave-Ayland     ppc_avr_t bcd_one;
2823428115c3SMark Cave-Ayland 
2824428115c3SMark Cave-Ayland     bcd_one.VsrD(0) = 0;
2825428115c3SMark Cave-Ayland     bcd_one.VsrD(1) = 0x10;
2826a54238adSJose Ricardo Ziviani 
2827a54238adSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2828a54238adSJose Ricardo Ziviani         return CRF_SO;
2829a54238adSJose Ricardo Ziviani     }
2830a54238adSJose Ricardo Ziviani 
2831a54238adSJose Ricardo Ziviani     if (unlikely(i > 31)) {
2832a54238adSJose Ricardo Ziviani         i = 31;
2833a54238adSJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
2834a54238adSJose Ricardo Ziviani         i = -31;
2835a54238adSJose Ricardo Ziviani     }
2836a54238adSJose Ricardo Ziviani 
2837a54238adSJose Ricardo Ziviani     if (i > 0) {
28383c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2839a54238adSJose Ricardo Ziviani     } else {
28403c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2841a54238adSJose Ricardo Ziviani 
2842a54238adSJose Ricardo Ziviani         if (bcd_get_digit(&ret, 0, &invalid) >= 5) {
2843a54238adSJose Ricardo Ziviani             bcd_add_mag(&ret, &ret, &bcd_one, &invalid, &unused);
2844a54238adSJose Ricardo Ziviani         }
2845a54238adSJose Ricardo Ziviani     }
2846a54238adSJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
2847a54238adSJose Ricardo Ziviani 
2848a54238adSJose Ricardo Ziviani     cr = bcd_cmp_zero(&ret);
2849a54238adSJose Ricardo Ziviani     if (ox_flag) {
2850a54238adSJose Ricardo Ziviani         cr |= CRF_SO;
2851a54238adSJose Ricardo Ziviani     }
2852a54238adSJose Ricardo Ziviani     *r = ret;
2853a54238adSJose Ricardo Ziviani 
2854a54238adSJose Ricardo Ziviani     return cr;
2855a54238adSJose Ricardo Ziviani }
2856a54238adSJose Ricardo Ziviani 
285731bc4d11SJose Ricardo Ziviani uint32_t helper_bcdtrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
285831bc4d11SJose Ricardo Ziviani {
285931bc4d11SJose Ricardo Ziviani     uint64_t mask;
286031bc4d11SJose Ricardo Ziviani     uint32_t ox_flag = 0;
2861428115c3SMark Cave-Ayland     int i = a->VsrSH(3) + 1;
286231bc4d11SJose Ricardo Ziviani     ppc_avr_t ret = *b;
286331bc4d11SJose Ricardo Ziviani 
286431bc4d11SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
286531bc4d11SJose Ricardo Ziviani         return CRF_SO;
286631bc4d11SJose Ricardo Ziviani     }
286731bc4d11SJose Ricardo Ziviani 
286831bc4d11SJose Ricardo Ziviani     if (i > 16 && i < 32) {
286931bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
28703c385a93SMark Cave-Ayland         if (ret.VsrD(0) & ~mask) {
287131bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
287231bc4d11SJose Ricardo Ziviani         }
287331bc4d11SJose Ricardo Ziviani 
28743c385a93SMark Cave-Ayland         ret.VsrD(0) &= mask;
287531bc4d11SJose Ricardo Ziviani     } else if (i >= 0 && i <= 16) {
287631bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
28773c385a93SMark Cave-Ayland         if (ret.VsrD(0) || (ret.VsrD(1) & ~mask)) {
287831bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
287931bc4d11SJose Ricardo Ziviani         }
288031bc4d11SJose Ricardo Ziviani 
28813c385a93SMark Cave-Ayland         ret.VsrD(1) &= mask;
28823c385a93SMark Cave-Ayland         ret.VsrD(0) = 0;
288331bc4d11SJose Ricardo Ziviani     }
288431bc4d11SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(bcd_get_sgn(b), ps), 0);
288531bc4d11SJose Ricardo Ziviani     *r = ret;
288631bc4d11SJose Ricardo Ziviani 
288731bc4d11SJose Ricardo Ziviani     return bcd_cmp_zero(&ret) | ox_flag;
288831bc4d11SJose Ricardo Ziviani }
288931bc4d11SJose Ricardo Ziviani 
28905c32e2e4SJose Ricardo Ziviani uint32_t helper_bcdutrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
28915c32e2e4SJose Ricardo Ziviani {
28925c32e2e4SJose Ricardo Ziviani     int i;
28935c32e2e4SJose Ricardo Ziviani     uint64_t mask;
28945c32e2e4SJose Ricardo Ziviani     uint32_t ox_flag = 0;
28955c32e2e4SJose Ricardo Ziviani     int invalid = 0;
28965c32e2e4SJose Ricardo Ziviani     ppc_avr_t ret = *b;
28975c32e2e4SJose Ricardo Ziviani 
28985c32e2e4SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
28995c32e2e4SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
29005c32e2e4SJose Ricardo Ziviani 
29015c32e2e4SJose Ricardo Ziviani         if (unlikely(invalid)) {
29025c32e2e4SJose Ricardo Ziviani             return CRF_SO;
29035c32e2e4SJose Ricardo Ziviani         }
29045c32e2e4SJose Ricardo Ziviani     }
29055c32e2e4SJose Ricardo Ziviani 
2906428115c3SMark Cave-Ayland     i = a->VsrSH(3);
29075c32e2e4SJose Ricardo Ziviani     if (i > 16 && i < 33) {
29085c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
29093c385a93SMark Cave-Ayland         if (ret.VsrD(0) & ~mask) {
29105c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
29115c32e2e4SJose Ricardo Ziviani         }
29125c32e2e4SJose Ricardo Ziviani 
29133c385a93SMark Cave-Ayland         ret.VsrD(0) &= mask;
29145c32e2e4SJose Ricardo Ziviani     } else if (i > 0 && i <= 16) {
29155c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
29163c385a93SMark Cave-Ayland         if (ret.VsrD(0) || (ret.VsrD(1) & ~mask)) {
29175c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
29185c32e2e4SJose Ricardo Ziviani         }
29195c32e2e4SJose Ricardo Ziviani 
29203c385a93SMark Cave-Ayland         ret.VsrD(1) &= mask;
29213c385a93SMark Cave-Ayland         ret.VsrD(0) = 0;
29225c32e2e4SJose Ricardo Ziviani     } else if (i == 0) {
29233c385a93SMark Cave-Ayland         if (ret.VsrD(0) || ret.VsrD(1)) {
29245c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
29255c32e2e4SJose Ricardo Ziviani         }
29263c385a93SMark Cave-Ayland         ret.VsrD(0) = ret.VsrD(1) = 0;
29275c32e2e4SJose Ricardo Ziviani     }
29285c32e2e4SJose Ricardo Ziviani 
29295c32e2e4SJose Ricardo Ziviani     *r = ret;
29303c385a93SMark Cave-Ayland     if (r->VsrD(0) == 0 && r->VsrD(1) == 0) {
29315c32e2e4SJose Ricardo Ziviani         return ox_flag | CRF_EQ;
29325c32e2e4SJose Ricardo Ziviani     }
29335c32e2e4SJose Ricardo Ziviani 
29345c32e2e4SJose Ricardo Ziviani     return ox_flag | CRF_GT;
29355c32e2e4SJose Ricardo Ziviani }
29365c32e2e4SJose Ricardo Ziviani 
2937fcf5ef2aSThomas Huth void helper_vsbox(ppc_avr_t *r, ppc_avr_t *a)
2938fcf5ef2aSThomas Huth {
2939fcf5ef2aSThomas Huth     int i;
2940fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
2941fcf5ef2aSThomas Huth         r->u8[i] = AES_sbox[a->u8[i]];
2942fcf5ef2aSThomas Huth     }
2943fcf5ef2aSThomas Huth }
2944fcf5ef2aSThomas Huth 
2945fcf5ef2aSThomas Huth void helper_vcipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2946fcf5ef2aSThomas Huth {
2947ce9f5b37SRichard Henderson     AESState *ad = (AESState *)r;
2948ce9f5b37SRichard Henderson     AESState *st = (AESState *)a;
2949ce9f5b37SRichard Henderson     AESState *rk = (AESState *)b;
2950fcf5ef2aSThomas Huth 
2951ce9f5b37SRichard Henderson     aesenc_SB_SR_MC_AK(ad, st, rk, true);
2952fcf5ef2aSThomas Huth }
2953fcf5ef2aSThomas Huth 
2954fcf5ef2aSThomas Huth void helper_vcipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2955fcf5ef2aSThomas Huth {
29567df34e48SRichard Henderson     aesenc_SB_SR_AK((AESState *)r, (AESState *)a, (AESState *)b, true);
2957fcf5ef2aSThomas Huth }
2958fcf5ef2aSThomas Huth 
2959fcf5ef2aSThomas Huth void helper_vncipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2960fcf5ef2aSThomas Huth {
2961af4cb945SRichard Henderson     AESState *ad = (AESState *)r;
2962af4cb945SRichard Henderson     AESState *st = (AESState *)a;
2963af4cb945SRichard Henderson     AESState *rk = (AESState *)b;
2964fcf5ef2aSThomas Huth 
2965af4cb945SRichard Henderson     aesdec_ISB_ISR_AK_IMC(ad, st, rk, true);
2966fcf5ef2aSThomas Huth }
2967fcf5ef2aSThomas Huth 
2968fcf5ef2aSThomas Huth void helper_vncipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2969fcf5ef2aSThomas Huth {
29702cf44f3bSRichard Henderson     aesdec_ISB_ISR_AK((AESState *)r, (AESState *)a, (AESState *)b, true);
2971fcf5ef2aSThomas Huth }
2972fcf5ef2aSThomas Huth 
2973fcf5ef2aSThomas Huth void helper_vshasigmaw(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
2974fcf5ef2aSThomas Huth {
2975fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
2976fcf5ef2aSThomas Huth     int six = st_six & 0xF;
2977fcf5ef2aSThomas Huth     int i;
2978fcf5ef2aSThomas Huth 
2979730d2ca3SMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2980fcf5ef2aSThomas Huth         if (st == 0) {
2981fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
29820ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 7) ^
29830ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 18) ^
2984730d2ca3SMark Cave-Ayland                              (a->VsrW(i) >> 3);
2985fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
29860ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 17) ^
29870ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 19) ^
2988730d2ca3SMark Cave-Ayland                              (a->VsrW(i) >> 10);
2989fcf5ef2aSThomas Huth             }
2990fcf5ef2aSThomas Huth         } else { /* st == 1 */
2991fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
29920ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 2) ^
29930ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 13) ^
29940ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 22);
2995fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
29960ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 6) ^
29970ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 11) ^
29980ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 25);
2999fcf5ef2aSThomas Huth             }
3000fcf5ef2aSThomas Huth         }
3001fcf5ef2aSThomas Huth     }
3002fcf5ef2aSThomas Huth }
3003fcf5ef2aSThomas Huth 
3004fcf5ef2aSThomas Huth void helper_vshasigmad(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
3005fcf5ef2aSThomas Huth {
3006fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
3007fcf5ef2aSThomas Huth     int six = st_six & 0xF;
3008fcf5ef2aSThomas Huth     int i;
3009fcf5ef2aSThomas Huth 
3010730d2ca3SMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
3011fcf5ef2aSThomas Huth         if (st == 0) {
3012fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2 * i))) == 0) {
30130ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 1) ^
30140ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 8) ^
3015730d2ca3SMark Cave-Ayland                              (a->VsrD(i) >> 7);
3016fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
30170ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 19) ^
30180ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 61) ^
3019730d2ca3SMark Cave-Ayland                              (a->VsrD(i) >> 6);
3020fcf5ef2aSThomas Huth             }
3021fcf5ef2aSThomas Huth         } else { /* st == 1 */
3022fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2 * i))) == 0) {
30230ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 28) ^
30240ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 34) ^
30250ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 39);
3026fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
30270ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 14) ^
30280ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 18) ^
30290ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 41);
3030fcf5ef2aSThomas Huth             }
3031fcf5ef2aSThomas Huth         }
3032fcf5ef2aSThomas Huth     }
3033fcf5ef2aSThomas Huth }
3034fcf5ef2aSThomas Huth 
3035fcf5ef2aSThomas Huth void helper_vpermxor(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
3036fcf5ef2aSThomas Huth {
3037fcf5ef2aSThomas Huth     ppc_avr_t result;
3038fcf5ef2aSThomas Huth     int i;
3039fcf5ef2aSThomas Huth 
304060594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
304160594feaSMark Cave-Ayland         int indexA = c->VsrB(i) >> 4;
304260594feaSMark Cave-Ayland         int indexB = c->VsrB(i) & 0xF;
304360594feaSMark Cave-Ayland 
304460594feaSMark Cave-Ayland         result.VsrB(i) = a->VsrB(indexA) ^ b->VsrB(indexB);
3045fcf5ef2aSThomas Huth     }
3046fcf5ef2aSThomas Huth     *r = result;
3047fcf5ef2aSThomas Huth }
3048fcf5ef2aSThomas Huth 
3049fcf5ef2aSThomas Huth #undef VECTOR_FOR_INORDER_I
3050fcf5ef2aSThomas Huth 
3051fcf5ef2aSThomas Huth /*****************************************************************************/
3052fcf5ef2aSThomas Huth /* SPE extension helpers */
3053fcf5ef2aSThomas Huth /* Use a table to make this quicker */
3054fcf5ef2aSThomas Huth static const uint8_t hbrev[16] = {
3055fcf5ef2aSThomas Huth     0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
3056fcf5ef2aSThomas Huth     0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
3057fcf5ef2aSThomas Huth };
3058fcf5ef2aSThomas Huth 
3059fcf5ef2aSThomas Huth static inline uint8_t byte_reverse(uint8_t val)
3060fcf5ef2aSThomas Huth {
3061fcf5ef2aSThomas Huth     return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
3062fcf5ef2aSThomas Huth }
3063fcf5ef2aSThomas Huth 
3064fcf5ef2aSThomas Huth static inline uint32_t word_reverse(uint32_t val)
3065fcf5ef2aSThomas Huth {
3066fcf5ef2aSThomas Huth     return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
3067fcf5ef2aSThomas Huth         (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
3068fcf5ef2aSThomas Huth }
3069fcf5ef2aSThomas Huth 
3070fcf5ef2aSThomas Huth #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
3071fcf5ef2aSThomas Huth target_ulong helper_brinc(target_ulong arg1, target_ulong arg2)
3072fcf5ef2aSThomas Huth {
3073fcf5ef2aSThomas Huth     uint32_t a, b, d, mask;
3074fcf5ef2aSThomas Huth 
3075fcf5ef2aSThomas Huth     mask = UINT32_MAX >> (32 - MASKBITS);
3076fcf5ef2aSThomas Huth     a = arg1 & mask;
3077fcf5ef2aSThomas Huth     b = arg2 & mask;
3078fcf5ef2aSThomas Huth     d = word_reverse(1 + word_reverse(a | ~b));
3079fcf5ef2aSThomas Huth     return (arg1 & ~mask) | (d & b);
3080fcf5ef2aSThomas Huth }
3081fcf5ef2aSThomas Huth 
3082fcf5ef2aSThomas Huth uint32_t helper_cntlsw32(uint32_t val)
3083fcf5ef2aSThomas Huth {
3084fcf5ef2aSThomas Huth     if (val & 0x80000000) {
3085fcf5ef2aSThomas Huth         return clz32(~val);
3086fcf5ef2aSThomas Huth     } else {
3087fcf5ef2aSThomas Huth         return clz32(val);
3088fcf5ef2aSThomas Huth     }
3089fcf5ef2aSThomas Huth }
3090fcf5ef2aSThomas Huth 
3091fcf5ef2aSThomas Huth uint32_t helper_cntlzw32(uint32_t val)
3092fcf5ef2aSThomas Huth {
3093fcf5ef2aSThomas Huth     return clz32(val);
3094fcf5ef2aSThomas Huth }
3095fcf5ef2aSThomas Huth 
3096fcf5ef2aSThomas Huth /* 440 specific */
3097fcf5ef2aSThomas Huth target_ulong helper_dlmzb(CPUPPCState *env, target_ulong high,
3098fcf5ef2aSThomas Huth                           target_ulong low, uint32_t update_Rc)
3099fcf5ef2aSThomas Huth {
3100fcf5ef2aSThomas Huth     target_ulong mask;
3101fcf5ef2aSThomas Huth     int i;
3102fcf5ef2aSThomas Huth 
3103fcf5ef2aSThomas Huth     i = 1;
3104fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
3105fcf5ef2aSThomas Huth         if ((high & mask) == 0) {
3106fcf5ef2aSThomas Huth             if (update_Rc) {
3107fcf5ef2aSThomas Huth                 env->crf[0] = 0x4;
3108fcf5ef2aSThomas Huth             }
3109fcf5ef2aSThomas Huth             goto done;
3110fcf5ef2aSThomas Huth         }
3111fcf5ef2aSThomas Huth         i++;
3112fcf5ef2aSThomas Huth     }
3113fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
3114fcf5ef2aSThomas Huth         if ((low & mask) == 0) {
3115fcf5ef2aSThomas Huth             if (update_Rc) {
3116fcf5ef2aSThomas Huth                 env->crf[0] = 0x8;
3117fcf5ef2aSThomas Huth             }
3118fcf5ef2aSThomas Huth             goto done;
3119fcf5ef2aSThomas Huth         }
3120fcf5ef2aSThomas Huth         i++;
3121fcf5ef2aSThomas Huth     }
3122fcf5ef2aSThomas Huth     i = 8;
3123fcf5ef2aSThomas Huth     if (update_Rc) {
3124fcf5ef2aSThomas Huth         env->crf[0] = 0x2;
3125fcf5ef2aSThomas Huth     }
3126fcf5ef2aSThomas Huth  done:
3127fcf5ef2aSThomas Huth     env->xer = (env->xer & ~0x7F) | i;
3128fcf5ef2aSThomas Huth     if (update_Rc) {
3129fcf5ef2aSThomas Huth         env->crf[0] |= xer_so;
3130fcf5ef2aSThomas Huth     }
3131fcf5ef2aSThomas Huth     return i;
3132fcf5ef2aSThomas Huth }
3133