xref: /openbmc/qemu/target/ppc/int_helper.c (revision 6bd039cd)
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
9*6bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19db725815SMarkus Armbruster 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "internal.h"
23fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
24db725815SMarkus Armbruster #include "qemu/main-loop.h"
25fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
26fcf5ef2aSThomas Huth #include "crypto/aes.h"
2724f91e81SAlex Bennée #include "fpu/softfloat.h"
283f74b632SRichard Henderson #include "qapi/error.h"
293f74b632SRichard Henderson #include "qemu/guest-random.h"
30fcf5ef2aSThomas Huth 
31fcf5ef2aSThomas Huth #include "helper_regs.h"
32fcf5ef2aSThomas Huth /*****************************************************************************/
33fcf5ef2aSThomas Huth /* Fixed point operations helpers */
34fcf5ef2aSThomas Huth 
35f32899deSNikunj A Dadhania static inline void helper_update_ov_legacy(CPUPPCState *env, int ov)
36f32899deSNikunj A Dadhania {
37f32899deSNikunj A Dadhania     if (unlikely(ov)) {
38f32899deSNikunj A Dadhania         env->so = env->ov = 1;
39f32899deSNikunj A Dadhania     } else {
40f32899deSNikunj A Dadhania         env->ov = 0;
41f32899deSNikunj A Dadhania     }
42f32899deSNikunj A Dadhania }
43f32899deSNikunj A Dadhania 
44fcf5ef2aSThomas Huth target_ulong helper_divweu(CPUPPCState *env, target_ulong ra, target_ulong rb,
45fcf5ef2aSThomas Huth                            uint32_t oe)
46fcf5ef2aSThomas Huth {
47fcf5ef2aSThomas Huth     uint64_t rt = 0;
48fcf5ef2aSThomas Huth     int overflow = 0;
49fcf5ef2aSThomas Huth 
50fcf5ef2aSThomas Huth     uint64_t dividend = (uint64_t)ra << 32;
51fcf5ef2aSThomas Huth     uint64_t divisor = (uint32_t)rb;
52fcf5ef2aSThomas Huth 
53fcf5ef2aSThomas Huth     if (unlikely(divisor == 0)) {
54fcf5ef2aSThomas Huth         overflow = 1;
55fcf5ef2aSThomas Huth     } else {
56fcf5ef2aSThomas Huth         rt = dividend / divisor;
57fcf5ef2aSThomas Huth         overflow = rt > UINT32_MAX;
58fcf5ef2aSThomas Huth     }
59fcf5ef2aSThomas Huth 
60fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
61fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
62fcf5ef2aSThomas Huth     }
63fcf5ef2aSThomas Huth 
64fcf5ef2aSThomas Huth     if (oe) {
65f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
66fcf5ef2aSThomas Huth     }
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth     return (target_ulong)rt;
69fcf5ef2aSThomas Huth }
70fcf5ef2aSThomas Huth 
71fcf5ef2aSThomas Huth target_ulong helper_divwe(CPUPPCState *env, target_ulong ra, target_ulong rb,
72fcf5ef2aSThomas Huth                           uint32_t oe)
73fcf5ef2aSThomas Huth {
74fcf5ef2aSThomas Huth     int64_t rt = 0;
75fcf5ef2aSThomas Huth     int overflow = 0;
76fcf5ef2aSThomas Huth 
77fcf5ef2aSThomas Huth     int64_t dividend = (int64_t)ra << 32;
78fcf5ef2aSThomas Huth     int64_t divisor = (int64_t)((int32_t)rb);
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     if (unlikely((divisor == 0) ||
81fcf5ef2aSThomas Huth                  ((divisor == -1ull) && (dividend == INT64_MIN)))) {
82fcf5ef2aSThomas Huth         overflow = 1;
83fcf5ef2aSThomas Huth     } else {
84fcf5ef2aSThomas Huth         rt = dividend / divisor;
85fcf5ef2aSThomas Huth         overflow = rt != (int32_t)rt;
86fcf5ef2aSThomas Huth     }
87fcf5ef2aSThomas Huth 
88fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
89fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
90fcf5ef2aSThomas Huth     }
91fcf5ef2aSThomas Huth 
92fcf5ef2aSThomas Huth     if (oe) {
93f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
94fcf5ef2aSThomas Huth     }
95fcf5ef2aSThomas Huth 
96fcf5ef2aSThomas Huth     return (target_ulong)rt;
97fcf5ef2aSThomas Huth }
98fcf5ef2aSThomas Huth 
99fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
100fcf5ef2aSThomas Huth 
101fcf5ef2aSThomas Huth uint64_t helper_divdeu(CPUPPCState *env, uint64_t ra, uint64_t rb, uint32_t oe)
102fcf5ef2aSThomas Huth {
103fcf5ef2aSThomas Huth     uint64_t rt = 0;
104fcf5ef2aSThomas Huth     int overflow = 0;
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth     overflow = divu128(&rt, &ra, rb);
107fcf5ef2aSThomas Huth 
108fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
109fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
110fcf5ef2aSThomas Huth     }
111fcf5ef2aSThomas Huth 
112fcf5ef2aSThomas Huth     if (oe) {
113f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
114fcf5ef2aSThomas Huth     }
115fcf5ef2aSThomas Huth 
116fcf5ef2aSThomas Huth     return rt;
117fcf5ef2aSThomas Huth }
118fcf5ef2aSThomas Huth 
119fcf5ef2aSThomas Huth uint64_t helper_divde(CPUPPCState *env, uint64_t rau, uint64_t rbu, uint32_t oe)
120fcf5ef2aSThomas Huth {
121fcf5ef2aSThomas Huth     int64_t rt = 0;
122fcf5ef2aSThomas Huth     int64_t ra = (int64_t)rau;
123fcf5ef2aSThomas Huth     int64_t rb = (int64_t)rbu;
124fcf5ef2aSThomas Huth     int overflow = divs128(&rt, &ra, rb);
125fcf5ef2aSThomas Huth 
126fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
127fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
128fcf5ef2aSThomas Huth     }
129fcf5ef2aSThomas Huth 
130fcf5ef2aSThomas Huth     if (oe) {
131f32899deSNikunj A Dadhania         helper_update_ov_legacy(env, overflow);
132fcf5ef2aSThomas Huth     }
133fcf5ef2aSThomas Huth 
134fcf5ef2aSThomas Huth     return rt;
135fcf5ef2aSThomas Huth }
136fcf5ef2aSThomas Huth 
137fcf5ef2aSThomas Huth #endif
138fcf5ef2aSThomas Huth 
139fcf5ef2aSThomas Huth 
140fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
141fcf5ef2aSThomas Huth /* if x = 0xab, returns 0xababababababababa */
142fcf5ef2aSThomas Huth #define pattern(x) (((x) & 0xff) * (~(target_ulong)0 / 0xff))
143fcf5ef2aSThomas Huth 
144b6cb41b2SDavid Gibson /*
145b6cb41b2SDavid Gibson  * subtract 1 from each byte, and with inverse, check if MSB is set at each
146fcf5ef2aSThomas Huth  * byte.
147fcf5ef2aSThomas Huth  * i.e. ((0x00 - 0x01) & ~(0x00)) & 0x80
148fcf5ef2aSThomas Huth  *      (0xFF & 0xFF) & 0x80 = 0x80 (zero found)
149fcf5ef2aSThomas Huth  */
150fcf5ef2aSThomas Huth #define haszero(v) (((v) - pattern(0x01)) & ~(v) & pattern(0x80))
151fcf5ef2aSThomas Huth 
152fcf5ef2aSThomas Huth /* When you XOR the pattern and there is a match, that byte will be zero */
153fcf5ef2aSThomas Huth #define hasvalue(x, n)  (haszero((x) ^ pattern(n)))
154fcf5ef2aSThomas Huth 
155fcf5ef2aSThomas Huth uint32_t helper_cmpeqb(target_ulong ra, target_ulong rb)
156fcf5ef2aSThomas Huth {
157efa73196SNikunj A Dadhania     return hasvalue(rb, ra) ? CRF_GT : 0;
158fcf5ef2aSThomas Huth }
159fcf5ef2aSThomas Huth 
160fcf5ef2aSThomas Huth #undef pattern
161fcf5ef2aSThomas Huth #undef haszero
162fcf5ef2aSThomas Huth #undef hasvalue
163fcf5ef2aSThomas Huth 
164b6cb41b2SDavid Gibson /*
1653f74b632SRichard Henderson  * Return a random number.
166fcf5ef2aSThomas Huth  */
1673f74b632SRichard Henderson uint64_t helper_darn32(void)
168fcf5ef2aSThomas Huth {
1693f74b632SRichard Henderson     Error *err = NULL;
1703f74b632SRichard Henderson     uint32_t ret;
1713f74b632SRichard Henderson 
1723f74b632SRichard Henderson     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
1733f74b632SRichard Henderson         qemu_log_mask(LOG_UNIMP, "darn: Crypto failure: %s",
1743f74b632SRichard Henderson                       error_get_pretty(err));
1753f74b632SRichard Henderson         error_free(err);
176fcf5ef2aSThomas Huth         return -1;
177fcf5ef2aSThomas Huth     }
178fcf5ef2aSThomas Huth 
1793f74b632SRichard Henderson     return ret;
1803f74b632SRichard Henderson }
1813f74b632SRichard Henderson 
1823f74b632SRichard Henderson uint64_t helper_darn64(void)
183fcf5ef2aSThomas Huth {
1843f74b632SRichard Henderson     Error *err = NULL;
1853f74b632SRichard Henderson     uint64_t ret;
1863f74b632SRichard Henderson 
1873f74b632SRichard Henderson     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
1883f74b632SRichard Henderson         qemu_log_mask(LOG_UNIMP, "darn: Crypto failure: %s",
1893f74b632SRichard Henderson                       error_get_pretty(err));
1903f74b632SRichard Henderson         error_free(err);
191fcf5ef2aSThomas Huth         return -1;
192fcf5ef2aSThomas Huth     }
193fcf5ef2aSThomas Huth 
1943f74b632SRichard Henderson     return ret;
1953f74b632SRichard Henderson }
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth uint64_t helper_bpermd(uint64_t rs, uint64_t rb)
198fcf5ef2aSThomas Huth {
199fcf5ef2aSThomas Huth     int i;
200fcf5ef2aSThomas Huth     uint64_t ra = 0;
201fcf5ef2aSThomas Huth 
202fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
203fcf5ef2aSThomas Huth         int index = (rs >> (i * 8)) & 0xFF;
204fcf5ef2aSThomas Huth         if (index < 64) {
205a6a444a8SCédric Le Goater             if (rb & PPC_BIT(index)) {
206fcf5ef2aSThomas Huth                 ra |= 1 << i;
207fcf5ef2aSThomas Huth             }
208fcf5ef2aSThomas Huth         }
209fcf5ef2aSThomas Huth     }
210fcf5ef2aSThomas Huth     return ra;
211fcf5ef2aSThomas Huth }
212fcf5ef2aSThomas Huth 
213fcf5ef2aSThomas Huth #endif
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth target_ulong helper_cmpb(target_ulong rs, target_ulong rb)
216fcf5ef2aSThomas Huth {
217fcf5ef2aSThomas Huth     target_ulong mask = 0xff;
218fcf5ef2aSThomas Huth     target_ulong ra = 0;
219fcf5ef2aSThomas Huth     int i;
220fcf5ef2aSThomas Huth 
221fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong); i++) {
222fcf5ef2aSThomas Huth         if ((rs & mask) == (rb & mask)) {
223fcf5ef2aSThomas Huth             ra |= mask;
224fcf5ef2aSThomas Huth         }
225fcf5ef2aSThomas Huth         mask <<= 8;
226fcf5ef2aSThomas Huth     }
227fcf5ef2aSThomas Huth     return ra;
228fcf5ef2aSThomas Huth }
229fcf5ef2aSThomas Huth 
230fcf5ef2aSThomas Huth /* shift right arithmetic helper */
231fcf5ef2aSThomas Huth target_ulong helper_sraw(CPUPPCState *env, target_ulong value,
232fcf5ef2aSThomas Huth                          target_ulong shift)
233fcf5ef2aSThomas Huth {
234fcf5ef2aSThomas Huth     int32_t ret;
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth     if (likely(!(shift & 0x20))) {
237fcf5ef2aSThomas Huth         if (likely((uint32_t)shift != 0)) {
238fcf5ef2aSThomas Huth             shift &= 0x1f;
239fcf5ef2aSThomas Huth             ret = (int32_t)value >> shift;
240fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
241af1c259fSSandipan Das                 env->ca32 = env->ca = 0;
242fcf5ef2aSThomas Huth             } else {
243af1c259fSSandipan Das                 env->ca32 = env->ca = 1;
244fcf5ef2aSThomas Huth             }
245fcf5ef2aSThomas Huth         } else {
246fcf5ef2aSThomas Huth             ret = (int32_t)value;
247af1c259fSSandipan Das             env->ca32 = env->ca = 0;
248fcf5ef2aSThomas Huth         }
249fcf5ef2aSThomas Huth     } else {
250fcf5ef2aSThomas Huth         ret = (int32_t)value >> 31;
251af1c259fSSandipan Das         env->ca32 = env->ca = (ret != 0);
252fcf5ef2aSThomas Huth     }
253fcf5ef2aSThomas Huth     return (target_long)ret;
254fcf5ef2aSThomas Huth }
255fcf5ef2aSThomas Huth 
256fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
257fcf5ef2aSThomas Huth target_ulong helper_srad(CPUPPCState *env, target_ulong value,
258fcf5ef2aSThomas Huth                          target_ulong shift)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     int64_t ret;
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth     if (likely(!(shift & 0x40))) {
263fcf5ef2aSThomas Huth         if (likely((uint64_t)shift != 0)) {
264fcf5ef2aSThomas Huth             shift &= 0x3f;
265fcf5ef2aSThomas Huth             ret = (int64_t)value >> shift;
266fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1ULL << shift) - 1)) == 0)) {
267af1c259fSSandipan Das                 env->ca32 = env->ca = 0;
268fcf5ef2aSThomas Huth             } else {
269af1c259fSSandipan Das                 env->ca32 = env->ca = 1;
270fcf5ef2aSThomas Huth             }
271fcf5ef2aSThomas Huth         } else {
272fcf5ef2aSThomas Huth             ret = (int64_t)value;
273af1c259fSSandipan Das             env->ca32 = env->ca = 0;
274fcf5ef2aSThomas Huth         }
275fcf5ef2aSThomas Huth     } else {
276fcf5ef2aSThomas Huth         ret = (int64_t)value >> 63;
277af1c259fSSandipan Das         env->ca32 = env->ca = (ret != 0);
278fcf5ef2aSThomas Huth     }
279fcf5ef2aSThomas Huth     return ret;
280fcf5ef2aSThomas Huth }
281fcf5ef2aSThomas Huth #endif
282fcf5ef2aSThomas Huth 
283fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
284fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
285fcf5ef2aSThomas Huth {
28679770002SRichard Henderson     /* Note that we don't fold past bytes */
287fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
288fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
289fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
290fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
291fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
292fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
293fcf5ef2aSThomas Huth     return val;
294fcf5ef2aSThomas Huth }
295fcf5ef2aSThomas Huth 
296fcf5ef2aSThomas Huth target_ulong helper_popcntw(target_ulong val)
297fcf5ef2aSThomas Huth {
29879770002SRichard Henderson     /* Note that we don't fold past words.  */
299fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
300fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
301fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
302fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
303fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
304fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
305fcf5ef2aSThomas Huth     val = (val & 0x00ff00ff00ff00ffULL) + ((val >>  8) &
306fcf5ef2aSThomas Huth                                            0x00ff00ff00ff00ffULL);
307fcf5ef2aSThomas Huth     val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) &
308fcf5ef2aSThomas Huth                                            0x0000ffff0000ffffULL);
309fcf5ef2aSThomas Huth     return val;
310fcf5ef2aSThomas Huth }
311fcf5ef2aSThomas Huth #else
312fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
313fcf5ef2aSThomas Huth {
31479770002SRichard Henderson     /* Note that we don't fold past bytes */
315fcf5ef2aSThomas Huth     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
316fcf5ef2aSThomas Huth     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
317fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
318fcf5ef2aSThomas Huth     return val;
319fcf5ef2aSThomas Huth }
320fcf5ef2aSThomas Huth #endif
321fcf5ef2aSThomas Huth 
322fcf5ef2aSThomas Huth /*****************************************************************************/
323fcf5ef2aSThomas Huth /* PowerPC 601 specific instructions (POWER bridge) */
324fcf5ef2aSThomas Huth target_ulong helper_div(CPUPPCState *env, target_ulong arg1, target_ulong arg2)
325fcf5ef2aSThomas Huth {
326fcf5ef2aSThomas Huth     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
327fcf5ef2aSThomas Huth 
328fcf5ef2aSThomas Huth     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
329fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
330fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
331fcf5ef2aSThomas Huth         return INT32_MIN;
332fcf5ef2aSThomas Huth     } else {
333fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = tmp % arg2;
334fcf5ef2aSThomas Huth         return  tmp / (int32_t)arg2;
335fcf5ef2aSThomas Huth     }
336fcf5ef2aSThomas Huth }
337fcf5ef2aSThomas Huth 
338fcf5ef2aSThomas Huth target_ulong helper_divo(CPUPPCState *env, target_ulong arg1,
339fcf5ef2aSThomas Huth                          target_ulong arg2)
340fcf5ef2aSThomas Huth {
341fcf5ef2aSThomas Huth     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
342fcf5ef2aSThomas Huth 
343fcf5ef2aSThomas Huth     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
344fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
345fcf5ef2aSThomas Huth         env->so = env->ov = 1;
346fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
347fcf5ef2aSThomas Huth         return INT32_MIN;
348fcf5ef2aSThomas Huth     } else {
349fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = tmp % arg2;
350fcf5ef2aSThomas Huth         tmp /= (int32_t)arg2;
351fcf5ef2aSThomas Huth         if ((int32_t)tmp != tmp) {
352fcf5ef2aSThomas Huth             env->so = env->ov = 1;
353fcf5ef2aSThomas Huth         } else {
354fcf5ef2aSThomas Huth             env->ov = 0;
355fcf5ef2aSThomas Huth         }
356fcf5ef2aSThomas Huth         return tmp;
357fcf5ef2aSThomas Huth     }
358fcf5ef2aSThomas Huth }
359fcf5ef2aSThomas Huth 
360fcf5ef2aSThomas Huth target_ulong helper_divs(CPUPPCState *env, target_ulong arg1,
361fcf5ef2aSThomas Huth                          target_ulong arg2)
362fcf5ef2aSThomas Huth {
363fcf5ef2aSThomas Huth     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
364fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
365fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
366fcf5ef2aSThomas Huth         return INT32_MIN;
367fcf5ef2aSThomas Huth     } else {
368fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
369fcf5ef2aSThomas Huth         return (int32_t)arg1 / (int32_t)arg2;
370fcf5ef2aSThomas Huth     }
371fcf5ef2aSThomas Huth }
372fcf5ef2aSThomas Huth 
373fcf5ef2aSThomas Huth target_ulong helper_divso(CPUPPCState *env, target_ulong arg1,
374fcf5ef2aSThomas Huth                           target_ulong arg2)
375fcf5ef2aSThomas Huth {
376fcf5ef2aSThomas Huth     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
377fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
378fcf5ef2aSThomas Huth         env->so = env->ov = 1;
379fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
380fcf5ef2aSThomas Huth         return INT32_MIN;
381fcf5ef2aSThomas Huth     } else {
382fcf5ef2aSThomas Huth         env->ov = 0;
383fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
384fcf5ef2aSThomas Huth         return (int32_t)arg1 / (int32_t)arg2;
385fcf5ef2aSThomas Huth     }
386fcf5ef2aSThomas Huth }
387fcf5ef2aSThomas Huth 
388fcf5ef2aSThomas Huth /*****************************************************************************/
389fcf5ef2aSThomas Huth /* 602 specific instructions */
390fcf5ef2aSThomas Huth /* mfrom is the most crazy instruction ever seen, imho ! */
391fcf5ef2aSThomas Huth /* Real implementation uses a ROM table. Do the same */
392b6cb41b2SDavid Gibson /*
393b6cb41b2SDavid Gibson  * Extremely decomposed:
394fcf5ef2aSThomas Huth  *                      -arg / 256
395fcf5ef2aSThomas Huth  * return 256 * log10(10           + 1.0) + 0.5
396fcf5ef2aSThomas Huth  */
397fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
398fcf5ef2aSThomas Huth target_ulong helper_602_mfrom(target_ulong arg)
399fcf5ef2aSThomas Huth {
400fcf5ef2aSThomas Huth     if (likely(arg < 602)) {
401139c1837SPaolo Bonzini #include "mfrom_table.c.inc"
402fcf5ef2aSThomas Huth         return mfrom_ROM_table[arg];
403fcf5ef2aSThomas Huth     } else {
404fcf5ef2aSThomas Huth         return 0;
405fcf5ef2aSThomas Huth     }
406fcf5ef2aSThomas Huth }
407fcf5ef2aSThomas Huth #endif
408fcf5ef2aSThomas Huth 
409fcf5ef2aSThomas Huth /*****************************************************************************/
410fcf5ef2aSThomas Huth /* Altivec extension helpers */
411fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
412fcf5ef2aSThomas Huth #define VECTOR_FOR_INORDER_I(index, element)                    \
413fcf5ef2aSThomas Huth     for (index = 0; index < ARRAY_SIZE(r->element); index++)
414fcf5ef2aSThomas Huth #else
415fcf5ef2aSThomas Huth #define VECTOR_FOR_INORDER_I(index, element)                    \
416fcf5ef2aSThomas Huth     for (index = ARRAY_SIZE(r->element) - 1; index >= 0; index--)
417fcf5ef2aSThomas Huth #endif
418fcf5ef2aSThomas Huth 
419fcf5ef2aSThomas Huth /* Saturating arithmetic helpers.  */
420fcf5ef2aSThomas Huth #define SATCVT(from, to, from_type, to_type, min, max)          \
421fcf5ef2aSThomas Huth     static inline to_type cvt##from##to(from_type x, int *sat)  \
422fcf5ef2aSThomas Huth     {                                                           \
423fcf5ef2aSThomas Huth         to_type r;                                              \
424fcf5ef2aSThomas Huth                                                                 \
425fcf5ef2aSThomas Huth         if (x < (from_type)min) {                               \
426fcf5ef2aSThomas Huth             r = min;                                            \
427fcf5ef2aSThomas Huth             *sat = 1;                                           \
428fcf5ef2aSThomas Huth         } else if (x > (from_type)max) {                        \
429fcf5ef2aSThomas Huth             r = max;                                            \
430fcf5ef2aSThomas Huth             *sat = 1;                                           \
431fcf5ef2aSThomas Huth         } else {                                                \
432fcf5ef2aSThomas Huth             r = x;                                              \
433fcf5ef2aSThomas Huth         }                                                       \
434fcf5ef2aSThomas Huth         return r;                                               \
435fcf5ef2aSThomas Huth     }
436fcf5ef2aSThomas Huth #define SATCVTU(from, to, from_type, to_type, min, max)         \
437fcf5ef2aSThomas Huth     static inline to_type cvt##from##to(from_type x, int *sat)  \
438fcf5ef2aSThomas Huth     {                                                           \
439fcf5ef2aSThomas Huth         to_type r;                                              \
440fcf5ef2aSThomas Huth                                                                 \
441fcf5ef2aSThomas Huth         if (x > (from_type)max) {                               \
442fcf5ef2aSThomas Huth             r = max;                                            \
443fcf5ef2aSThomas Huth             *sat = 1;                                           \
444fcf5ef2aSThomas Huth         } else {                                                \
445fcf5ef2aSThomas Huth             r = x;                                              \
446fcf5ef2aSThomas Huth         }                                                       \
447fcf5ef2aSThomas Huth         return r;                                               \
448fcf5ef2aSThomas Huth     }
449fcf5ef2aSThomas Huth SATCVT(sh, sb, int16_t, int8_t, INT8_MIN, INT8_MAX)
450fcf5ef2aSThomas Huth SATCVT(sw, sh, int32_t, int16_t, INT16_MIN, INT16_MAX)
451fcf5ef2aSThomas Huth SATCVT(sd, sw, int64_t, int32_t, INT32_MIN, INT32_MAX)
452fcf5ef2aSThomas Huth 
453fcf5ef2aSThomas Huth SATCVTU(uh, ub, uint16_t, uint8_t, 0, UINT8_MAX)
454fcf5ef2aSThomas Huth SATCVTU(uw, uh, uint32_t, uint16_t, 0, UINT16_MAX)
455fcf5ef2aSThomas Huth SATCVTU(ud, uw, uint64_t, uint32_t, 0, UINT32_MAX)
456fcf5ef2aSThomas Huth SATCVT(sh, ub, int16_t, uint8_t, 0, UINT8_MAX)
457fcf5ef2aSThomas Huth SATCVT(sw, uh, int32_t, uint16_t, 0, UINT16_MAX)
458fcf5ef2aSThomas Huth SATCVT(sd, uw, int64_t, uint32_t, 0, UINT32_MAX)
459fcf5ef2aSThomas Huth #undef SATCVT
460fcf5ef2aSThomas Huth #undef SATCVTU
461fcf5ef2aSThomas Huth 
462dedfaac7SRichard Henderson void helper_mtvscr(CPUPPCState *env, uint32_t vscr)
463fcf5ef2aSThomas Huth {
4649b5b74daSRichard Henderson     env->vscr = vscr & ~(1u << VSCR_SAT);
4659b5b74daSRichard Henderson     /* Which bit we set is completely arbitrary, but clear the rest.  */
4669b5b74daSRichard Henderson     env->vscr_sat.u64[0] = vscr & (1u << VSCR_SAT);
4679b5b74daSRichard Henderson     env->vscr_sat.u64[1] = 0;
468dedfaac7SRichard Henderson     set_flush_to_zero((vscr >> VSCR_NJ) & 1, &env->vec_status);
469fcf5ef2aSThomas Huth }
470fcf5ef2aSThomas Huth 
471cc2b90d7SRichard Henderson uint32_t helper_mfvscr(CPUPPCState *env)
472cc2b90d7SRichard Henderson {
4739b5b74daSRichard Henderson     uint32_t sat = (env->vscr_sat.u64[0] | env->vscr_sat.u64[1]) != 0;
4749b5b74daSRichard Henderson     return env->vscr | (sat << VSCR_SAT);
475cc2b90d7SRichard Henderson }
476cc2b90d7SRichard Henderson 
4776175f5a0SRichard Henderson static inline void set_vscr_sat(CPUPPCState *env)
4786175f5a0SRichard Henderson {
4799b5b74daSRichard Henderson     /* The choice of non-zero value is arbitrary.  */
4809b5b74daSRichard Henderson     env->vscr_sat.u32[0] = 1;
4816175f5a0SRichard Henderson }
4826175f5a0SRichard Henderson 
483fcf5ef2aSThomas Huth void helper_vaddcuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
484fcf5ef2aSThomas Huth {
485fcf5ef2aSThomas Huth     int i;
486fcf5ef2aSThomas Huth 
487fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
488fcf5ef2aSThomas Huth         r->u32[i] = ~a->u32[i] < b->u32[i];
489fcf5ef2aSThomas Huth     }
490fcf5ef2aSThomas Huth }
491fcf5ef2aSThomas Huth 
492fcf5ef2aSThomas Huth /* vprtybw */
493fcf5ef2aSThomas Huth void helper_vprtybw(ppc_avr_t *r, ppc_avr_t *b)
494fcf5ef2aSThomas Huth {
495fcf5ef2aSThomas Huth     int i;
496fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
497fcf5ef2aSThomas Huth         uint64_t res = b->u32[i] ^ (b->u32[i] >> 16);
498fcf5ef2aSThomas Huth         res ^= res >> 8;
499fcf5ef2aSThomas Huth         r->u32[i] = res & 1;
500fcf5ef2aSThomas Huth     }
501fcf5ef2aSThomas Huth }
502fcf5ef2aSThomas Huth 
503fcf5ef2aSThomas Huth /* vprtybd */
504fcf5ef2aSThomas Huth void helper_vprtybd(ppc_avr_t *r, ppc_avr_t *b)
505fcf5ef2aSThomas Huth {
506fcf5ef2aSThomas Huth     int i;
507fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
508fcf5ef2aSThomas Huth         uint64_t res = b->u64[i] ^ (b->u64[i] >> 32);
509fcf5ef2aSThomas Huth         res ^= res >> 16;
510fcf5ef2aSThomas Huth         res ^= res >> 8;
511fcf5ef2aSThomas Huth         r->u64[i] = res & 1;
512fcf5ef2aSThomas Huth     }
513fcf5ef2aSThomas Huth }
514fcf5ef2aSThomas Huth 
515fcf5ef2aSThomas Huth /* vprtybq */
516fcf5ef2aSThomas Huth void helper_vprtybq(ppc_avr_t *r, ppc_avr_t *b)
517fcf5ef2aSThomas Huth {
518fcf5ef2aSThomas Huth     uint64_t res = b->u64[0] ^ b->u64[1];
519fcf5ef2aSThomas Huth     res ^= res >> 32;
520fcf5ef2aSThomas Huth     res ^= res >> 16;
521fcf5ef2aSThomas Huth     res ^= res >> 8;
5223c385a93SMark Cave-Ayland     r->VsrD(1) = res & 1;
5233c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
524fcf5ef2aSThomas Huth }
525fcf5ef2aSThomas Huth 
526fcf5ef2aSThomas Huth #define VARITHFP(suffix, func)                                          \
527fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
528fcf5ef2aSThomas Huth                           ppc_avr_t *b)                                 \
529fcf5ef2aSThomas Huth     {                                                                   \
530fcf5ef2aSThomas Huth         int i;                                                          \
531fcf5ef2aSThomas Huth                                                                         \
53205ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
53305ee3e8aSMark Cave-Ayland             r->f32[i] = func(a->f32[i], b->f32[i], &env->vec_status);   \
534fcf5ef2aSThomas Huth         }                                                               \
535fcf5ef2aSThomas Huth     }
536fcf5ef2aSThomas Huth VARITHFP(addfp, float32_add)
537fcf5ef2aSThomas Huth VARITHFP(subfp, float32_sub)
538fcf5ef2aSThomas Huth VARITHFP(minfp, float32_min)
539fcf5ef2aSThomas Huth VARITHFP(maxfp, float32_max)
540fcf5ef2aSThomas Huth #undef VARITHFP
541fcf5ef2aSThomas Huth 
542fcf5ef2aSThomas Huth #define VARITHFPFMA(suffix, type)                                       \
543fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
544fcf5ef2aSThomas Huth                            ppc_avr_t *b, ppc_avr_t *c)                  \
545fcf5ef2aSThomas Huth     {                                                                   \
546fcf5ef2aSThomas Huth         int i;                                                          \
54705ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
54805ee3e8aSMark Cave-Ayland             r->f32[i] = float32_muladd(a->f32[i], c->f32[i], b->f32[i], \
549fcf5ef2aSThomas Huth                                        type, &env->vec_status);         \
550fcf5ef2aSThomas Huth         }                                                               \
551fcf5ef2aSThomas Huth     }
552fcf5ef2aSThomas Huth VARITHFPFMA(maddfp, 0);
553fcf5ef2aSThomas Huth VARITHFPFMA(nmsubfp, float_muladd_negate_result | float_muladd_negate_c);
554fcf5ef2aSThomas Huth #undef VARITHFPFMA
555fcf5ef2aSThomas Huth 
556fcf5ef2aSThomas Huth #define VARITHSAT_CASE(type, op, cvt, element)                          \
557fcf5ef2aSThomas Huth     {                                                                   \
558fcf5ef2aSThomas Huth         type result = (type)a->element[i] op (type)b->element[i];       \
559fcf5ef2aSThomas Huth         r->element[i] = cvt(result, &sat);                              \
560fcf5ef2aSThomas Huth     }
561fcf5ef2aSThomas Huth 
562fcf5ef2aSThomas Huth #define VARITHSAT_DO(name, op, optype, cvt, element)                    \
563fb11ae7dSRichard Henderson     void helper_v##name(ppc_avr_t *r, ppc_avr_t *vscr_sat,              \
564fb11ae7dSRichard Henderson                         ppc_avr_t *a, ppc_avr_t *b, uint32_t desc)      \
565fcf5ef2aSThomas Huth     {                                                                   \
566fcf5ef2aSThomas Huth         int sat = 0;                                                    \
567fcf5ef2aSThomas Huth         int i;                                                          \
568fcf5ef2aSThomas Huth                                                                         \
569fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
570fcf5ef2aSThomas Huth             VARITHSAT_CASE(optype, op, cvt, element);                   \
571fcf5ef2aSThomas Huth         }                                                               \
572fcf5ef2aSThomas Huth         if (sat) {                                                      \
573fb11ae7dSRichard Henderson             vscr_sat->u32[0] = 1;                                       \
574fcf5ef2aSThomas Huth         }                                                               \
575fcf5ef2aSThomas Huth     }
576fcf5ef2aSThomas Huth #define VARITHSAT_SIGNED(suffix, element, optype, cvt)          \
577fcf5ef2aSThomas Huth     VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element)      \
578fcf5ef2aSThomas Huth     VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
579fcf5ef2aSThomas Huth #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt)        \
580fcf5ef2aSThomas Huth     VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element)      \
581fcf5ef2aSThomas Huth     VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
582fcf5ef2aSThomas Huth VARITHSAT_SIGNED(b, s8, int16_t, cvtshsb)
583fcf5ef2aSThomas Huth VARITHSAT_SIGNED(h, s16, int32_t, cvtswsh)
584fcf5ef2aSThomas Huth VARITHSAT_SIGNED(w, s32, int64_t, cvtsdsw)
585fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(b, u8, uint16_t, cvtshub)
586fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(h, u16, uint32_t, cvtswuh)
587fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(w, u32, uint64_t, cvtsduw)
588fcf5ef2aSThomas Huth #undef VARITHSAT_CASE
589fcf5ef2aSThomas Huth #undef VARITHSAT_DO
590fcf5ef2aSThomas Huth #undef VARITHSAT_SIGNED
591fcf5ef2aSThomas Huth #undef VARITHSAT_UNSIGNED
592fcf5ef2aSThomas Huth 
593fcf5ef2aSThomas Huth #define VAVG_DO(name, element, etype)                                   \
594fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
595fcf5ef2aSThomas Huth     {                                                                   \
596fcf5ef2aSThomas Huth         int i;                                                          \
597fcf5ef2aSThomas Huth                                                                         \
598fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
599fcf5ef2aSThomas Huth             etype x = (etype)a->element[i] + (etype)b->element[i] + 1;  \
600fcf5ef2aSThomas Huth             r->element[i] = x >> 1;                                     \
601fcf5ef2aSThomas Huth         }                                                               \
602fcf5ef2aSThomas Huth     }
603fcf5ef2aSThomas Huth 
604fcf5ef2aSThomas Huth #define VAVG(type, signed_element, signed_type, unsigned_element,       \
605fcf5ef2aSThomas Huth              unsigned_type)                                             \
606fcf5ef2aSThomas Huth     VAVG_DO(avgs##type, signed_element, signed_type)                    \
607fcf5ef2aSThomas Huth     VAVG_DO(avgu##type, unsigned_element, unsigned_type)
608fcf5ef2aSThomas Huth VAVG(b, s8, int16_t, u8, uint16_t)
609fcf5ef2aSThomas Huth VAVG(h, s16, int32_t, u16, uint32_t)
610fcf5ef2aSThomas Huth VAVG(w, s32, int64_t, u32, uint64_t)
611fcf5ef2aSThomas Huth #undef VAVG_DO
612fcf5ef2aSThomas Huth #undef VAVG
613fcf5ef2aSThomas Huth 
614fcf5ef2aSThomas Huth #define VABSDU_DO(name, element)                                        \
615fcf5ef2aSThomas Huth void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)           \
616fcf5ef2aSThomas Huth {                                                                       \
617fcf5ef2aSThomas Huth     int i;                                                              \
618fcf5ef2aSThomas Huth                                                                         \
619fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
620fcf5ef2aSThomas Huth         r->element[i] = (a->element[i] > b->element[i]) ?               \
621fcf5ef2aSThomas Huth             (a->element[i] - b->element[i]) :                           \
622fcf5ef2aSThomas Huth             (b->element[i] - a->element[i]);                            \
623fcf5ef2aSThomas Huth     }                                                                   \
624fcf5ef2aSThomas Huth }
625fcf5ef2aSThomas Huth 
626b6cb41b2SDavid Gibson /*
627b6cb41b2SDavid Gibson  * VABSDU - Vector absolute difference unsigned
628fcf5ef2aSThomas Huth  *   name    - instruction mnemonic suffix (b: byte, h: halfword, w: word)
629fcf5ef2aSThomas Huth  *   element - element type to access from vector
630fcf5ef2aSThomas Huth  */
631fcf5ef2aSThomas Huth #define VABSDU(type, element)                   \
632fcf5ef2aSThomas Huth     VABSDU_DO(absdu##type, element)
633fcf5ef2aSThomas Huth VABSDU(b, u8)
634fcf5ef2aSThomas Huth VABSDU(h, u16)
635fcf5ef2aSThomas Huth VABSDU(w, u32)
636fcf5ef2aSThomas Huth #undef VABSDU_DO
637fcf5ef2aSThomas Huth #undef VABSDU
638fcf5ef2aSThomas Huth 
639fcf5ef2aSThomas Huth #define VCF(suffix, cvt, element)                                       \
640fcf5ef2aSThomas Huth     void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r,             \
641fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
642fcf5ef2aSThomas Huth     {                                                                   \
643fcf5ef2aSThomas Huth         int i;                                                          \
644fcf5ef2aSThomas Huth                                                                         \
64505ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
646fcf5ef2aSThomas Huth             float32 t = cvt(b->element[i], &env->vec_status);           \
64705ee3e8aSMark Cave-Ayland             r->f32[i] = float32_scalbn(t, -uim, &env->vec_status);      \
648fcf5ef2aSThomas Huth         }                                                               \
649fcf5ef2aSThomas Huth     }
650fcf5ef2aSThomas Huth VCF(ux, uint32_to_float32, u32)
651fcf5ef2aSThomas Huth VCF(sx, int32_to_float32, s32)
652fcf5ef2aSThomas Huth #undef VCF
653fcf5ef2aSThomas Huth 
654fcf5ef2aSThomas Huth #define VCMP_DO(suffix, compare, element, record)                       \
655fcf5ef2aSThomas Huth     void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r,            \
656fcf5ef2aSThomas Huth                              ppc_avr_t *a, ppc_avr_t *b)                \
657fcf5ef2aSThomas Huth     {                                                                   \
658fcf5ef2aSThomas Huth         uint64_t ones = (uint64_t)-1;                                   \
659fcf5ef2aSThomas Huth         uint64_t all = ones;                                            \
660fcf5ef2aSThomas Huth         uint64_t none = 0;                                              \
661fcf5ef2aSThomas Huth         int i;                                                          \
662fcf5ef2aSThomas Huth                                                                         \
663fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
664fcf5ef2aSThomas Huth             uint64_t result = (a->element[i] compare b->element[i] ?    \
665fcf5ef2aSThomas Huth                                ones : 0x0);                             \
666fcf5ef2aSThomas Huth             switch (sizeof(a->element[0])) {                            \
667fcf5ef2aSThomas Huth             case 8:                                                     \
668fcf5ef2aSThomas Huth                 r->u64[i] = result;                                     \
669fcf5ef2aSThomas Huth                 break;                                                  \
670fcf5ef2aSThomas Huth             case 4:                                                     \
671fcf5ef2aSThomas Huth                 r->u32[i] = result;                                     \
672fcf5ef2aSThomas Huth                 break;                                                  \
673fcf5ef2aSThomas Huth             case 2:                                                     \
674fcf5ef2aSThomas Huth                 r->u16[i] = result;                                     \
675fcf5ef2aSThomas Huth                 break;                                                  \
676fcf5ef2aSThomas Huth             case 1:                                                     \
677fcf5ef2aSThomas Huth                 r->u8[i] = result;                                      \
678fcf5ef2aSThomas Huth                 break;                                                  \
679fcf5ef2aSThomas Huth             }                                                           \
680fcf5ef2aSThomas Huth             all &= result;                                              \
681fcf5ef2aSThomas Huth             none |= result;                                             \
682fcf5ef2aSThomas Huth         }                                                               \
683fcf5ef2aSThomas Huth         if (record) {                                                   \
684fcf5ef2aSThomas Huth             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
685fcf5ef2aSThomas Huth         }                                                               \
686fcf5ef2aSThomas Huth     }
687fcf5ef2aSThomas Huth #define VCMP(suffix, compare, element)          \
688fcf5ef2aSThomas Huth     VCMP_DO(suffix, compare, element, 0)        \
689fcf5ef2aSThomas Huth     VCMP_DO(suffix##_dot, compare, element, 1)
690fcf5ef2aSThomas Huth VCMP(equb, ==, u8)
691fcf5ef2aSThomas Huth VCMP(equh, ==, u16)
692fcf5ef2aSThomas Huth VCMP(equw, ==, u32)
693fcf5ef2aSThomas Huth VCMP(equd, ==, u64)
694fcf5ef2aSThomas Huth VCMP(gtub, >, u8)
695fcf5ef2aSThomas Huth VCMP(gtuh, >, u16)
696fcf5ef2aSThomas Huth VCMP(gtuw, >, u32)
697fcf5ef2aSThomas Huth VCMP(gtud, >, u64)
698fcf5ef2aSThomas Huth VCMP(gtsb, >, s8)
699fcf5ef2aSThomas Huth VCMP(gtsh, >, s16)
700fcf5ef2aSThomas Huth VCMP(gtsw, >, s32)
701fcf5ef2aSThomas Huth VCMP(gtsd, >, s64)
702fcf5ef2aSThomas Huth #undef VCMP_DO
703fcf5ef2aSThomas Huth #undef VCMP
704fcf5ef2aSThomas Huth 
705fcf5ef2aSThomas Huth #define VCMPNE_DO(suffix, element, etype, cmpzero, record)              \
706fcf5ef2aSThomas Huth void helper_vcmpne##suffix(CPUPPCState *env, ppc_avr_t *r,              \
707fcf5ef2aSThomas Huth                             ppc_avr_t *a, ppc_avr_t *b)                 \
708fcf5ef2aSThomas Huth {                                                                       \
709fcf5ef2aSThomas Huth     etype ones = (etype)-1;                                             \
710fcf5ef2aSThomas Huth     etype all = ones;                                                   \
711fcf5ef2aSThomas Huth     etype result, none = 0;                                             \
712fcf5ef2aSThomas Huth     int i;                                                              \
713fcf5ef2aSThomas Huth                                                                         \
714fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
715fcf5ef2aSThomas Huth         if (cmpzero) {                                                  \
716fcf5ef2aSThomas Huth             result = ((a->element[i] == 0)                              \
717fcf5ef2aSThomas Huth                            || (b->element[i] == 0)                      \
718fcf5ef2aSThomas Huth                            || (a->element[i] != b->element[i]) ?        \
719fcf5ef2aSThomas Huth                            ones : 0x0);                                 \
720fcf5ef2aSThomas Huth         } else {                                                        \
721fcf5ef2aSThomas Huth             result = (a->element[i] != b->element[i]) ? ones : 0x0;     \
722fcf5ef2aSThomas Huth         }                                                               \
723fcf5ef2aSThomas Huth         r->element[i] = result;                                         \
724fcf5ef2aSThomas Huth         all &= result;                                                  \
725fcf5ef2aSThomas Huth         none |= result;                                                 \
726fcf5ef2aSThomas Huth     }                                                                   \
727fcf5ef2aSThomas Huth     if (record) {                                                       \
728fcf5ef2aSThomas Huth         env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);           \
729fcf5ef2aSThomas Huth     }                                                                   \
730fcf5ef2aSThomas Huth }
731fcf5ef2aSThomas Huth 
732b6cb41b2SDavid Gibson /*
733b6cb41b2SDavid Gibson  * VCMPNEZ - Vector compare not equal to zero
734fcf5ef2aSThomas Huth  *   suffix  - instruction mnemonic suffix (b: byte, h: halfword, w: word)
735fcf5ef2aSThomas Huth  *   element - element type to access from vector
736fcf5ef2aSThomas Huth  */
737fcf5ef2aSThomas Huth #define VCMPNE(suffix, element, etype, cmpzero)         \
738fcf5ef2aSThomas Huth     VCMPNE_DO(suffix, element, etype, cmpzero, 0)       \
739fcf5ef2aSThomas Huth     VCMPNE_DO(suffix##_dot, element, etype, cmpzero, 1)
740fcf5ef2aSThomas Huth VCMPNE(zb, u8, uint8_t, 1)
741fcf5ef2aSThomas Huth VCMPNE(zh, u16, uint16_t, 1)
742fcf5ef2aSThomas Huth VCMPNE(zw, u32, uint32_t, 1)
743fcf5ef2aSThomas Huth VCMPNE(b, u8, uint8_t, 0)
744fcf5ef2aSThomas Huth VCMPNE(h, u16, uint16_t, 0)
745fcf5ef2aSThomas Huth VCMPNE(w, u32, uint32_t, 0)
746fcf5ef2aSThomas Huth #undef VCMPNE_DO
747fcf5ef2aSThomas Huth #undef VCMPNE
748fcf5ef2aSThomas Huth 
749fcf5ef2aSThomas Huth #define VCMPFP_DO(suffix, compare, order, record)                       \
750fcf5ef2aSThomas Huth     void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r,            \
751fcf5ef2aSThomas Huth                              ppc_avr_t *a, ppc_avr_t *b)                \
752fcf5ef2aSThomas Huth     {                                                                   \
753fcf5ef2aSThomas Huth         uint32_t ones = (uint32_t)-1;                                   \
754fcf5ef2aSThomas Huth         uint32_t all = ones;                                            \
755fcf5ef2aSThomas Huth         uint32_t none = 0;                                              \
756fcf5ef2aSThomas Huth         int i;                                                          \
757fcf5ef2aSThomas Huth                                                                         \
75805ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
759fcf5ef2aSThomas Huth             uint32_t result;                                            \
76071bfd65cSRichard Henderson             FloatRelation rel =                                         \
76171bfd65cSRichard Henderson                 float32_compare_quiet(a->f32[i], b->f32[i],             \
762fcf5ef2aSThomas Huth                                       &env->vec_status);                \
763fcf5ef2aSThomas Huth             if (rel == float_relation_unordered) {                      \
764fcf5ef2aSThomas Huth                 result = 0;                                             \
765fcf5ef2aSThomas Huth             } else if (rel compare order) {                             \
766fcf5ef2aSThomas Huth                 result = ones;                                          \
767fcf5ef2aSThomas Huth             } else {                                                    \
768fcf5ef2aSThomas Huth                 result = 0;                                             \
769fcf5ef2aSThomas Huth             }                                                           \
770fcf5ef2aSThomas Huth             r->u32[i] = result;                                         \
771fcf5ef2aSThomas Huth             all &= result;                                              \
772fcf5ef2aSThomas Huth             none |= result;                                             \
773fcf5ef2aSThomas Huth         }                                                               \
774fcf5ef2aSThomas Huth         if (record) {                                                   \
775fcf5ef2aSThomas Huth             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
776fcf5ef2aSThomas Huth         }                                                               \
777fcf5ef2aSThomas Huth     }
778fcf5ef2aSThomas Huth #define VCMPFP(suffix, compare, order)          \
779fcf5ef2aSThomas Huth     VCMPFP_DO(suffix, compare, order, 0)        \
780fcf5ef2aSThomas Huth     VCMPFP_DO(suffix##_dot, compare, order, 1)
781fcf5ef2aSThomas Huth VCMPFP(eqfp, ==, float_relation_equal)
782fcf5ef2aSThomas Huth VCMPFP(gefp, !=, float_relation_less)
783fcf5ef2aSThomas Huth VCMPFP(gtfp, ==, float_relation_greater)
784fcf5ef2aSThomas Huth #undef VCMPFP_DO
785fcf5ef2aSThomas Huth #undef VCMPFP
786fcf5ef2aSThomas Huth 
787fcf5ef2aSThomas Huth static inline void vcmpbfp_internal(CPUPPCState *env, ppc_avr_t *r,
788fcf5ef2aSThomas Huth                                     ppc_avr_t *a, ppc_avr_t *b, int record)
789fcf5ef2aSThomas Huth {
790fcf5ef2aSThomas Huth     int i;
791fcf5ef2aSThomas Huth     int all_in = 0;
792fcf5ef2aSThomas Huth 
79305ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
79471bfd65cSRichard Henderson         FloatRelation le_rel = float32_compare_quiet(a->f32[i], b->f32[i],
79505ee3e8aSMark Cave-Ayland                                                      &env->vec_status);
796fcf5ef2aSThomas Huth         if (le_rel == float_relation_unordered) {
797fcf5ef2aSThomas Huth             r->u32[i] = 0xc0000000;
798fcf5ef2aSThomas Huth             all_in = 1;
799fcf5ef2aSThomas Huth         } else {
80005ee3e8aSMark Cave-Ayland             float32 bneg = float32_chs(b->f32[i]);
80171bfd65cSRichard Henderson             FloatRelation ge_rel = float32_compare_quiet(a->f32[i], bneg,
80205ee3e8aSMark Cave-Ayland                                                          &env->vec_status);
803fcf5ef2aSThomas Huth             int le = le_rel != float_relation_greater;
804fcf5ef2aSThomas Huth             int ge = ge_rel != float_relation_less;
805fcf5ef2aSThomas Huth 
806fcf5ef2aSThomas Huth             r->u32[i] = ((!le) << 31) | ((!ge) << 30);
807fcf5ef2aSThomas Huth             all_in |= (!le | !ge);
808fcf5ef2aSThomas Huth         }
809fcf5ef2aSThomas Huth     }
810fcf5ef2aSThomas Huth     if (record) {
811fcf5ef2aSThomas Huth         env->crf[6] = (all_in == 0) << 1;
812fcf5ef2aSThomas Huth     }
813fcf5ef2aSThomas Huth }
814fcf5ef2aSThomas Huth 
815fcf5ef2aSThomas Huth void helper_vcmpbfp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
816fcf5ef2aSThomas Huth {
817fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 0);
818fcf5ef2aSThomas Huth }
819fcf5ef2aSThomas Huth 
820fcf5ef2aSThomas Huth void helper_vcmpbfp_dot(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
821fcf5ef2aSThomas Huth                         ppc_avr_t *b)
822fcf5ef2aSThomas Huth {
823fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 1);
824fcf5ef2aSThomas Huth }
825fcf5ef2aSThomas Huth 
826fcf5ef2aSThomas Huth #define VCT(suffix, satcvt, element)                                    \
827fcf5ef2aSThomas Huth     void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r,             \
828fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
829fcf5ef2aSThomas Huth     {                                                                   \
830fcf5ef2aSThomas Huth         int i;                                                          \
831fcf5ef2aSThomas Huth         int sat = 0;                                                    \
832fcf5ef2aSThomas Huth         float_status s = env->vec_status;                               \
833fcf5ef2aSThomas Huth                                                                         \
834fcf5ef2aSThomas Huth         set_float_rounding_mode(float_round_to_zero, &s);               \
83505ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {                      \
83605ee3e8aSMark Cave-Ayland             if (float32_is_any_nan(b->f32[i])) {                        \
837fcf5ef2aSThomas Huth                 r->element[i] = 0;                                      \
838fcf5ef2aSThomas Huth             } else {                                                    \
83905ee3e8aSMark Cave-Ayland                 float64 t = float32_to_float64(b->f32[i], &s);          \
840fcf5ef2aSThomas Huth                 int64_t j;                                              \
841fcf5ef2aSThomas Huth                                                                         \
842fcf5ef2aSThomas Huth                 t = float64_scalbn(t, uim, &s);                         \
843fcf5ef2aSThomas Huth                 j = float64_to_int64(t, &s);                            \
844fcf5ef2aSThomas Huth                 r->element[i] = satcvt(j, &sat);                        \
845fcf5ef2aSThomas Huth             }                                                           \
846fcf5ef2aSThomas Huth         }                                                               \
847fcf5ef2aSThomas Huth         if (sat) {                                                      \
8486175f5a0SRichard Henderson             set_vscr_sat(env);                                          \
849fcf5ef2aSThomas Huth         }                                                               \
850fcf5ef2aSThomas Huth     }
851fcf5ef2aSThomas Huth VCT(uxs, cvtsduw, u32)
852fcf5ef2aSThomas Huth VCT(sxs, cvtsdsw, s32)
853fcf5ef2aSThomas Huth #undef VCT
854fcf5ef2aSThomas Huth 
855fcf5ef2aSThomas Huth target_ulong helper_vclzlsbb(ppc_avr_t *r)
856fcf5ef2aSThomas Huth {
857fcf5ef2aSThomas Huth     target_ulong count = 0;
858fcf5ef2aSThomas Huth     int i;
85960594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
86060594feaSMark Cave-Ayland         if (r->VsrB(i) & 0x01) {
861fcf5ef2aSThomas Huth             break;
862fcf5ef2aSThomas Huth         }
863fcf5ef2aSThomas Huth         count++;
864fcf5ef2aSThomas Huth     }
865fcf5ef2aSThomas Huth     return count;
866fcf5ef2aSThomas Huth }
867fcf5ef2aSThomas Huth 
868fcf5ef2aSThomas Huth target_ulong helper_vctzlsbb(ppc_avr_t *r)
869fcf5ef2aSThomas Huth {
870fcf5ef2aSThomas Huth     target_ulong count = 0;
871fcf5ef2aSThomas Huth     int i;
872fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
87360594feaSMark Cave-Ayland         if (r->VsrB(i) & 0x01) {
874fcf5ef2aSThomas Huth             break;
875fcf5ef2aSThomas Huth         }
876fcf5ef2aSThomas Huth         count++;
877fcf5ef2aSThomas Huth     }
878fcf5ef2aSThomas Huth     return count;
879fcf5ef2aSThomas Huth }
880fcf5ef2aSThomas Huth 
881fcf5ef2aSThomas Huth void helper_vmhaddshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
882fcf5ef2aSThomas Huth                       ppc_avr_t *b, ppc_avr_t *c)
883fcf5ef2aSThomas Huth {
884fcf5ef2aSThomas Huth     int sat = 0;
885fcf5ef2aSThomas Huth     int i;
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
888fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
889fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
890fcf5ef2aSThomas Huth 
891fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
892fcf5ef2aSThomas Huth     }
893fcf5ef2aSThomas Huth 
894fcf5ef2aSThomas Huth     if (sat) {
8956175f5a0SRichard Henderson         set_vscr_sat(env);
896fcf5ef2aSThomas Huth     }
897fcf5ef2aSThomas Huth }
898fcf5ef2aSThomas Huth 
899fcf5ef2aSThomas Huth void helper_vmhraddshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
900fcf5ef2aSThomas Huth                        ppc_avr_t *b, ppc_avr_t *c)
901fcf5ef2aSThomas Huth {
902fcf5ef2aSThomas Huth     int sat = 0;
903fcf5ef2aSThomas Huth     int i;
904fcf5ef2aSThomas Huth 
905fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
906fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i] + 0x00004000;
907fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
908fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
909fcf5ef2aSThomas Huth     }
910fcf5ef2aSThomas Huth 
911fcf5ef2aSThomas Huth     if (sat) {
9126175f5a0SRichard Henderson         set_vscr_sat(env);
913fcf5ef2aSThomas Huth     }
914fcf5ef2aSThomas Huth }
915fcf5ef2aSThomas Huth 
916fcf5ef2aSThomas Huth void helper_vmladduhm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
917fcf5ef2aSThomas Huth {
918fcf5ef2aSThomas Huth     int i;
919fcf5ef2aSThomas Huth 
920fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
921fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
922fcf5ef2aSThomas Huth         r->s16[i] = (int16_t) (prod + c->s16[i]);
923fcf5ef2aSThomas Huth     }
924fcf5ef2aSThomas Huth }
925fcf5ef2aSThomas Huth 
926d81c2040SMark Cave-Ayland #define VMRG_DO(name, element, access, ofs)                                  \
927fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)            \
928fcf5ef2aSThomas Huth     {                                                                        \
929fcf5ef2aSThomas Huth         ppc_avr_t result;                                                    \
930d81c2040SMark Cave-Ayland         int i, half = ARRAY_SIZE(r->element) / 2;                            \
931fcf5ef2aSThomas Huth                                                                              \
932d81c2040SMark Cave-Ayland         for (i = 0; i < half; i++) {                                         \
933d81c2040SMark Cave-Ayland             result.access(i * 2 + 0) = a->access(i + ofs);                   \
934d81c2040SMark Cave-Ayland             result.access(i * 2 + 1) = b->access(i + ofs);                   \
935fcf5ef2aSThomas Huth         }                                                                    \
936fcf5ef2aSThomas Huth         *r = result;                                                         \
937fcf5ef2aSThomas Huth     }
938d81c2040SMark Cave-Ayland 
939d81c2040SMark Cave-Ayland #define VMRG(suffix, element, access)          \
940d81c2040SMark Cave-Ayland     VMRG_DO(mrgl##suffix, element, access, half)   \
941d81c2040SMark Cave-Ayland     VMRG_DO(mrgh##suffix, element, access, 0)
942d81c2040SMark Cave-Ayland VMRG(b, u8, VsrB)
943d81c2040SMark Cave-Ayland VMRG(h, u16, VsrH)
944d81c2040SMark Cave-Ayland VMRG(w, u32, VsrW)
945fcf5ef2aSThomas Huth #undef VMRG_DO
946fcf5ef2aSThomas Huth #undef VMRG
947fcf5ef2aSThomas Huth 
948fcf5ef2aSThomas Huth void helper_vmsummbm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
949fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
950fcf5ef2aSThomas Huth {
951fcf5ef2aSThomas Huth     int32_t prod[16];
952fcf5ef2aSThomas Huth     int i;
953fcf5ef2aSThomas Huth 
954fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s8); i++) {
955fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s8[i] * b->u8[i];
956fcf5ef2aSThomas Huth     }
957fcf5ef2aSThomas Huth 
958fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
959fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[4 * i] + prod[4 * i + 1] +
960fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
961fcf5ef2aSThomas Huth     }
962fcf5ef2aSThomas Huth }
963fcf5ef2aSThomas Huth 
964fcf5ef2aSThomas Huth void helper_vmsumshm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
965fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
966fcf5ef2aSThomas Huth {
967fcf5ef2aSThomas Huth     int32_t prod[8];
968fcf5ef2aSThomas Huth     int i;
969fcf5ef2aSThomas Huth 
970fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
971fcf5ef2aSThomas Huth         prod[i] = a->s16[i] * b->s16[i];
972fcf5ef2aSThomas Huth     }
973fcf5ef2aSThomas Huth 
974fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
975fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[2 * i] + prod[2 * i + 1];
976fcf5ef2aSThomas Huth     }
977fcf5ef2aSThomas Huth }
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth void helper_vmsumshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
980fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
981fcf5ef2aSThomas Huth {
982fcf5ef2aSThomas Huth     int32_t prod[8];
983fcf5ef2aSThomas Huth     int i;
984fcf5ef2aSThomas Huth     int sat = 0;
985fcf5ef2aSThomas Huth 
986fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
987fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s16[i] * b->s16[i];
988fcf5ef2aSThomas Huth     }
989fcf5ef2aSThomas Huth 
990fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
991fcf5ef2aSThomas Huth         int64_t t = (int64_t)c->s32[i] + prod[2 * i] + prod[2 * i + 1];
992fcf5ef2aSThomas Huth 
993fcf5ef2aSThomas Huth         r->u32[i] = cvtsdsw(t, &sat);
994fcf5ef2aSThomas Huth     }
995fcf5ef2aSThomas Huth 
996fcf5ef2aSThomas Huth     if (sat) {
9976175f5a0SRichard Henderson         set_vscr_sat(env);
998fcf5ef2aSThomas Huth     }
999fcf5ef2aSThomas Huth }
1000fcf5ef2aSThomas Huth 
1001fcf5ef2aSThomas Huth void helper_vmsumubm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1002fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1003fcf5ef2aSThomas Huth {
1004fcf5ef2aSThomas Huth     uint16_t prod[16];
1005fcf5ef2aSThomas Huth     int i;
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1008fcf5ef2aSThomas Huth         prod[i] = a->u8[i] * b->u8[i];
1009fcf5ef2aSThomas Huth     }
1010fcf5ef2aSThomas Huth 
1011fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1012fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[4 * i] + prod[4 * i + 1] +
1013fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
1014fcf5ef2aSThomas Huth     }
1015fcf5ef2aSThomas Huth }
1016fcf5ef2aSThomas Huth 
1017fcf5ef2aSThomas Huth void helper_vmsumuhm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1018fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1019fcf5ef2aSThomas Huth {
1020fcf5ef2aSThomas Huth     uint32_t prod[8];
1021fcf5ef2aSThomas Huth     int i;
1022fcf5ef2aSThomas Huth 
1023fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1024fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1025fcf5ef2aSThomas Huth     }
1026fcf5ef2aSThomas Huth 
1027fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1028fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1029fcf5ef2aSThomas Huth     }
1030fcf5ef2aSThomas Huth }
1031fcf5ef2aSThomas Huth 
1032fcf5ef2aSThomas Huth void helper_vmsumuhs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1033fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1034fcf5ef2aSThomas Huth {
1035fcf5ef2aSThomas Huth     uint32_t prod[8];
1036fcf5ef2aSThomas Huth     int i;
1037fcf5ef2aSThomas Huth     int sat = 0;
1038fcf5ef2aSThomas Huth 
1039fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1040fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1041fcf5ef2aSThomas Huth     }
1042fcf5ef2aSThomas Huth 
1043fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1044fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1045fcf5ef2aSThomas Huth 
1046fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
1047fcf5ef2aSThomas Huth     }
1048fcf5ef2aSThomas Huth 
1049fcf5ef2aSThomas Huth     if (sat) {
10506175f5a0SRichard Henderson         set_vscr_sat(env);
1051fcf5ef2aSThomas Huth     }
1052fcf5ef2aSThomas Huth }
1053fcf5ef2aSThomas Huth 
10544fbc89edSMark Cave-Ayland #define VMUL_DO_EVN(name, mul_element, mul_access, prod_access, cast)   \
1055fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
1056fcf5ef2aSThomas Huth     {                                                                   \
1057fcf5ef2aSThomas Huth         int i;                                                          \
1058fcf5ef2aSThomas Huth                                                                         \
10594fbc89edSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) {           \
10604fbc89edSMark Cave-Ayland             r->prod_access(i >> 1) = (cast)a->mul_access(i) *           \
10614fbc89edSMark Cave-Ayland                                      (cast)b->mul_access(i);            \
1062fcf5ef2aSThomas Huth         }                                                               \
1063fcf5ef2aSThomas Huth     }
10644fbc89edSMark Cave-Ayland 
10654fbc89edSMark Cave-Ayland #define VMUL_DO_ODD(name, mul_element, mul_access, prod_access, cast)   \
10664fbc89edSMark Cave-Ayland     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
10674fbc89edSMark Cave-Ayland     {                                                                   \
10684fbc89edSMark Cave-Ayland         int i;                                                          \
10694fbc89edSMark Cave-Ayland                                                                         \
10704fbc89edSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) {           \
10714fbc89edSMark Cave-Ayland             r->prod_access(i >> 1) = (cast)a->mul_access(i + 1) *       \
10724fbc89edSMark Cave-Ayland                                      (cast)b->mul_access(i + 1);        \
10734fbc89edSMark Cave-Ayland         }                                                               \
10744fbc89edSMark Cave-Ayland     }
10754fbc89edSMark Cave-Ayland 
10764fbc89edSMark Cave-Ayland #define VMUL(suffix, mul_element, mul_access, prod_access, cast)       \
10774fbc89edSMark Cave-Ayland     VMUL_DO_EVN(mule##suffix, mul_element, mul_access, prod_access, cast)  \
10784fbc89edSMark Cave-Ayland     VMUL_DO_ODD(mulo##suffix, mul_element, mul_access, prod_access, cast)
10794fbc89edSMark Cave-Ayland VMUL(sb, s8, VsrSB, VsrSH, int16_t)
10804fbc89edSMark Cave-Ayland VMUL(sh, s16, VsrSH, VsrSW, int32_t)
10814fbc89edSMark Cave-Ayland VMUL(sw, s32, VsrSW, VsrSD, int64_t)
10824fbc89edSMark Cave-Ayland VMUL(ub, u8, VsrB, VsrH, uint16_t)
10834fbc89edSMark Cave-Ayland VMUL(uh, u16, VsrH, VsrW, uint32_t)
10844fbc89edSMark Cave-Ayland VMUL(uw, u32, VsrW, VsrD, uint64_t)
10854fbc89edSMark Cave-Ayland #undef VMUL_DO_EVN
10864fbc89edSMark Cave-Ayland #undef VMUL_DO_ODD
1087fcf5ef2aSThomas Huth #undef VMUL
1088fcf5ef2aSThomas Huth 
1089f3e0d864SLijun Pan void helper_vmulhsw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1090f3e0d864SLijun Pan {
1091f3e0d864SLijun Pan     int i;
1092f3e0d864SLijun Pan 
1093f3e0d864SLijun Pan     for (i = 0; i < 4; i++) {
1094f3e0d864SLijun Pan         r->s32[i] = (int32_t)(((int64_t)a->s32[i] * (int64_t)b->s32[i]) >> 32);
1095f3e0d864SLijun Pan     }
1096f3e0d864SLijun Pan }
1097f3e0d864SLijun Pan 
1098f3e0d864SLijun Pan void helper_vmulhuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1099f3e0d864SLijun Pan {
1100f3e0d864SLijun Pan     int i;
1101f3e0d864SLijun Pan 
1102f3e0d864SLijun Pan     for (i = 0; i < 4; i++) {
1103f3e0d864SLijun Pan         r->u32[i] = (uint32_t)(((uint64_t)a->u32[i] *
1104f3e0d864SLijun Pan                                (uint64_t)b->u32[i]) >> 32);
1105f3e0d864SLijun Pan     }
1106f3e0d864SLijun Pan }
1107f3e0d864SLijun Pan 
1108c4b8b49dSLijun Pan void helper_vmulhsd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1109c4b8b49dSLijun Pan {
1110c4b8b49dSLijun Pan     uint64_t discard;
1111c4b8b49dSLijun Pan 
1112c4b8b49dSLijun Pan     muls64(&discard, &r->u64[0], a->s64[0], b->s64[0]);
1113c4b8b49dSLijun Pan     muls64(&discard, &r->u64[1], a->s64[1], b->s64[1]);
1114c4b8b49dSLijun Pan }
1115c4b8b49dSLijun Pan 
1116c4b8b49dSLijun Pan void helper_vmulhud(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1117c4b8b49dSLijun Pan {
1118c4b8b49dSLijun Pan     uint64_t discard;
1119c4b8b49dSLijun Pan 
1120c4b8b49dSLijun Pan     mulu64(&discard, &r->u64[0], a->u64[0], b->u64[0]);
1121c4b8b49dSLijun Pan     mulu64(&discard, &r->u64[1], a->u64[1], b->u64[1]);
1122c4b8b49dSLijun Pan }
1123c4b8b49dSLijun Pan 
1124fcf5ef2aSThomas Huth void helper_vperm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b,
1125fcf5ef2aSThomas Huth                   ppc_avr_t *c)
1126fcf5ef2aSThomas Huth {
1127fcf5ef2aSThomas Huth     ppc_avr_t result;
1128fcf5ef2aSThomas Huth     int i;
1129fcf5ef2aSThomas Huth 
113060594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
113160594feaSMark Cave-Ayland         int s = c->VsrB(i) & 0x1f;
1132fcf5ef2aSThomas Huth         int index = s & 0xf;
1133fcf5ef2aSThomas Huth 
1134fcf5ef2aSThomas Huth         if (s & 0x10) {
113560594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index);
1136fcf5ef2aSThomas Huth         } else {
113760594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1138fcf5ef2aSThomas Huth         }
1139fcf5ef2aSThomas Huth     }
1140fcf5ef2aSThomas Huth     *r = result;
1141fcf5ef2aSThomas Huth }
1142fcf5ef2aSThomas Huth 
1143fcf5ef2aSThomas Huth void helper_vpermr(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b,
1144fcf5ef2aSThomas Huth                   ppc_avr_t *c)
1145fcf5ef2aSThomas Huth {
1146fcf5ef2aSThomas Huth     ppc_avr_t result;
1147fcf5ef2aSThomas Huth     int i;
1148fcf5ef2aSThomas Huth 
114960594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
115060594feaSMark Cave-Ayland         int s = c->VsrB(i) & 0x1f;
1151fcf5ef2aSThomas Huth         int index = 15 - (s & 0xf);
1152fcf5ef2aSThomas Huth 
1153fcf5ef2aSThomas Huth         if (s & 0x10) {
115460594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1155fcf5ef2aSThomas Huth         } else {
115660594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index);
1157fcf5ef2aSThomas Huth         }
1158fcf5ef2aSThomas Huth     }
1159fcf5ef2aSThomas Huth     *r = result;
1160fcf5ef2aSThomas Huth }
1161fcf5ef2aSThomas Huth 
1162fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1163fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[(i)])
1164fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (i)
1165fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) != 0)
1166fcf5ef2aSThomas Huth #define EXTRACT_BIT(avr, i, index) (extract64((avr)->u64[i], index, 1))
1167fcf5ef2aSThomas Huth #else
1168fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[15 - (i)])
1169fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (1 - i)
1170fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) == 0)
1171fcf5ef2aSThomas Huth #define EXTRACT_BIT(avr, i, index) \
1172fcf5ef2aSThomas Huth         (extract64((avr)->u64[1 - i], 63 - index, 1))
1173fcf5ef2aSThomas Huth #endif
1174fcf5ef2aSThomas Huth 
1175fcf5ef2aSThomas Huth void helper_vbpermd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1176fcf5ef2aSThomas Huth {
1177fcf5ef2aSThomas Huth     int i, j;
1178fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
1179fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1180fcf5ef2aSThomas Huth         for (j = 0; j < 8; j++) {
1181fcf5ef2aSThomas Huth             int index = VBPERMQ_INDEX(b, (i * 8) + j);
1182fcf5ef2aSThomas Huth             if (index < 64 && EXTRACT_BIT(a, i, index)) {
1183fcf5ef2aSThomas Huth                 result.u64[VBPERMD_INDEX(i)] |= (0x80 >> j);
1184fcf5ef2aSThomas Huth             }
1185fcf5ef2aSThomas Huth         }
1186fcf5ef2aSThomas Huth     }
1187fcf5ef2aSThomas Huth     *r = result;
1188fcf5ef2aSThomas Huth }
1189fcf5ef2aSThomas Huth 
1190fcf5ef2aSThomas Huth void helper_vbpermq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1191fcf5ef2aSThomas Huth {
1192fcf5ef2aSThomas Huth     int i;
1193fcf5ef2aSThomas Huth     uint64_t perm = 0;
1194fcf5ef2aSThomas Huth 
1195fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1196fcf5ef2aSThomas Huth         int index = VBPERMQ_INDEX(b, i);
1197fcf5ef2aSThomas Huth 
1198fcf5ef2aSThomas Huth         if (index < 128) {
1199fcf5ef2aSThomas Huth             uint64_t mask = (1ull << (63 - (index & 0x3F)));
1200fcf5ef2aSThomas Huth             if (a->u64[VBPERMQ_DW(index)] & mask) {
1201fcf5ef2aSThomas Huth                 perm |= (0x8000 >> i);
1202fcf5ef2aSThomas Huth             }
1203fcf5ef2aSThomas Huth         }
1204fcf5ef2aSThomas Huth     }
1205fcf5ef2aSThomas Huth 
12063c385a93SMark Cave-Ayland     r->VsrD(0) = perm;
12073c385a93SMark Cave-Ayland     r->VsrD(1) = 0;
1208fcf5ef2aSThomas Huth }
1209fcf5ef2aSThomas Huth 
1210fcf5ef2aSThomas Huth #undef VBPERMQ_INDEX
1211fcf5ef2aSThomas Huth #undef VBPERMQ_DW
1212fcf5ef2aSThomas Huth 
1213fcf5ef2aSThomas Huth #define PMSUM(name, srcfld, trgfld, trgtyp)                   \
1214fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
1215fcf5ef2aSThomas Huth {                                                             \
1216fcf5ef2aSThomas Huth     int i, j;                                                 \
1217fcf5ef2aSThomas Huth     trgtyp prod[sizeof(ppc_avr_t) / sizeof(a->srcfld[0])];    \
1218fcf5ef2aSThomas Huth                                                               \
1219fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, srcfld) {                         \
1220fcf5ef2aSThomas Huth         prod[i] = 0;                                          \
1221fcf5ef2aSThomas Huth         for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) {      \
1222fcf5ef2aSThomas Huth             if (a->srcfld[i] & (1ull << j)) {                 \
1223fcf5ef2aSThomas Huth                 prod[i] ^= ((trgtyp)b->srcfld[i] << j);       \
1224fcf5ef2aSThomas Huth             }                                                 \
1225fcf5ef2aSThomas Huth         }                                                     \
1226fcf5ef2aSThomas Huth     }                                                         \
1227fcf5ef2aSThomas Huth                                                               \
1228fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, trgfld) {                         \
1229fcf5ef2aSThomas Huth         r->trgfld[i] = prod[2 * i] ^ prod[2 * i + 1];         \
1230fcf5ef2aSThomas Huth     }                                                         \
1231fcf5ef2aSThomas Huth }
1232fcf5ef2aSThomas Huth 
1233fcf5ef2aSThomas Huth PMSUM(vpmsumb, u8, u16, uint16_t)
1234fcf5ef2aSThomas Huth PMSUM(vpmsumh, u16, u32, uint32_t)
1235fcf5ef2aSThomas Huth PMSUM(vpmsumw, u32, u64, uint64_t)
1236fcf5ef2aSThomas Huth 
1237fcf5ef2aSThomas Huth void helper_vpmsumd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1238fcf5ef2aSThomas Huth {
1239fcf5ef2aSThomas Huth 
1240fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1241fcf5ef2aSThomas Huth     int i, j;
1242fcf5ef2aSThomas Huth     __uint128_t prod[2];
1243fcf5ef2aSThomas Huth 
1244fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1245fcf5ef2aSThomas Huth         prod[i] = 0;
1246fcf5ef2aSThomas Huth         for (j = 0; j < 64; j++) {
1247fcf5ef2aSThomas Huth             if (a->u64[i] & (1ull << j)) {
1248fcf5ef2aSThomas Huth                 prod[i] ^= (((__uint128_t)b->u64[i]) << j);
1249fcf5ef2aSThomas Huth             }
1250fcf5ef2aSThomas Huth         }
1251fcf5ef2aSThomas Huth     }
1252fcf5ef2aSThomas Huth 
1253fcf5ef2aSThomas Huth     r->u128 = prod[0] ^ prod[1];
1254fcf5ef2aSThomas Huth 
1255fcf5ef2aSThomas Huth #else
1256fcf5ef2aSThomas Huth     int i, j;
1257fcf5ef2aSThomas Huth     ppc_avr_t prod[2];
1258fcf5ef2aSThomas Huth 
1259fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
12603c385a93SMark Cave-Ayland         prod[i].VsrD(1) = prod[i].VsrD(0) = 0;
1261fcf5ef2aSThomas Huth         for (j = 0; j < 64; j++) {
1262fcf5ef2aSThomas Huth             if (a->u64[i] & (1ull << j)) {
1263fcf5ef2aSThomas Huth                 ppc_avr_t bshift;
1264fcf5ef2aSThomas Huth                 if (j == 0) {
12653c385a93SMark Cave-Ayland                     bshift.VsrD(0) = 0;
12663c385a93SMark Cave-Ayland                     bshift.VsrD(1) = b->u64[i];
1267fcf5ef2aSThomas Huth                 } else {
12683c385a93SMark Cave-Ayland                     bshift.VsrD(0) = b->u64[i] >> (64 - j);
12693c385a93SMark Cave-Ayland                     bshift.VsrD(1) = b->u64[i] << j;
1270fcf5ef2aSThomas Huth                 }
12713c385a93SMark Cave-Ayland                 prod[i].VsrD(1) ^= bshift.VsrD(1);
12723c385a93SMark Cave-Ayland                 prod[i].VsrD(0) ^= bshift.VsrD(0);
1273fcf5ef2aSThomas Huth             }
1274fcf5ef2aSThomas Huth         }
1275fcf5ef2aSThomas Huth     }
1276fcf5ef2aSThomas Huth 
12773c385a93SMark Cave-Ayland     r->VsrD(1) = prod[0].VsrD(1) ^ prod[1].VsrD(1);
12783c385a93SMark Cave-Ayland     r->VsrD(0) = prod[0].VsrD(0) ^ prod[1].VsrD(0);
1279fcf5ef2aSThomas Huth #endif
1280fcf5ef2aSThomas Huth }
1281fcf5ef2aSThomas Huth 
1282fcf5ef2aSThomas Huth 
1283fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1284fcf5ef2aSThomas Huth #define PKBIG 1
1285fcf5ef2aSThomas Huth #else
1286fcf5ef2aSThomas Huth #define PKBIG 0
1287fcf5ef2aSThomas Huth #endif
1288fcf5ef2aSThomas Huth void helper_vpkpx(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1289fcf5ef2aSThomas Huth {
1290fcf5ef2aSThomas Huth     int i, j;
1291fcf5ef2aSThomas Huth     ppc_avr_t result;
1292fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1293fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { a, b };
1294fcf5ef2aSThomas Huth #else
1295fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { b, a };
1296fcf5ef2aSThomas Huth #endif
1297fcf5ef2aSThomas Huth 
1298fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1299fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(j, u32) {
1300fcf5ef2aSThomas Huth             uint32_t e = x[i]->u32[j];
1301fcf5ef2aSThomas Huth 
1302fcf5ef2aSThomas Huth             result.u16[4 * i + j] = (((e >> 9) & 0xfc00) |
1303fcf5ef2aSThomas Huth                                      ((e >> 6) & 0x3e0) |
1304fcf5ef2aSThomas Huth                                      ((e >> 3) & 0x1f));
1305fcf5ef2aSThomas Huth         }
1306fcf5ef2aSThomas Huth     }
1307fcf5ef2aSThomas Huth     *r = result;
1308fcf5ef2aSThomas Huth }
1309fcf5ef2aSThomas Huth 
1310fcf5ef2aSThomas Huth #define VPK(suffix, from, to, cvt, dosat)                               \
1311fcf5ef2aSThomas Huth     void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r,             \
1312fcf5ef2aSThomas Huth                             ppc_avr_t *a, ppc_avr_t *b)                 \
1313fcf5ef2aSThomas Huth     {                                                                   \
1314fcf5ef2aSThomas Huth         int i;                                                          \
1315fcf5ef2aSThomas Huth         int sat = 0;                                                    \
1316fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
1317fcf5ef2aSThomas Huth         ppc_avr_t *a0 = PKBIG ? a : b;                                  \
1318fcf5ef2aSThomas Huth         ppc_avr_t *a1 = PKBIG ? b : a;                                  \
1319fcf5ef2aSThomas Huth                                                                         \
1320fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(i, from) {                                 \
1321fcf5ef2aSThomas Huth             result.to[i] = cvt(a0->from[i], &sat);                      \
1322fcf5ef2aSThomas Huth             result.to[i + ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);\
1323fcf5ef2aSThomas Huth         }                                                               \
1324fcf5ef2aSThomas Huth         *r = result;                                                    \
1325fcf5ef2aSThomas Huth         if (dosat && sat) {                                             \
13266175f5a0SRichard Henderson             set_vscr_sat(env);                                          \
1327fcf5ef2aSThomas Huth         }                                                               \
1328fcf5ef2aSThomas Huth     }
1329fcf5ef2aSThomas Huth #define I(x, y) (x)
1330fcf5ef2aSThomas Huth VPK(shss, s16, s8, cvtshsb, 1)
1331fcf5ef2aSThomas Huth VPK(shus, s16, u8, cvtshub, 1)
1332fcf5ef2aSThomas Huth VPK(swss, s32, s16, cvtswsh, 1)
1333fcf5ef2aSThomas Huth VPK(swus, s32, u16, cvtswuh, 1)
1334fcf5ef2aSThomas Huth VPK(sdss, s64, s32, cvtsdsw, 1)
1335fcf5ef2aSThomas Huth VPK(sdus, s64, u32, cvtsduw, 1)
1336fcf5ef2aSThomas Huth VPK(uhus, u16, u8, cvtuhub, 1)
1337fcf5ef2aSThomas Huth VPK(uwus, u32, u16, cvtuwuh, 1)
1338fcf5ef2aSThomas Huth VPK(udus, u64, u32, cvtuduw, 1)
1339fcf5ef2aSThomas Huth VPK(uhum, u16, u8, I, 0)
1340fcf5ef2aSThomas Huth VPK(uwum, u32, u16, I, 0)
1341fcf5ef2aSThomas Huth VPK(udum, u64, u32, I, 0)
1342fcf5ef2aSThomas Huth #undef I
1343fcf5ef2aSThomas Huth #undef VPK
1344fcf5ef2aSThomas Huth #undef PKBIG
1345fcf5ef2aSThomas Huth 
1346fcf5ef2aSThomas Huth void helper_vrefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1347fcf5ef2aSThomas Huth {
1348fcf5ef2aSThomas Huth     int i;
1349fcf5ef2aSThomas Huth 
135005ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
135105ee3e8aSMark Cave-Ayland         r->f32[i] = float32_div(float32_one, b->f32[i], &env->vec_status);
1352fcf5ef2aSThomas Huth     }
1353fcf5ef2aSThomas Huth }
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth #define VRFI(suffix, rounding)                                  \
1356fcf5ef2aSThomas Huth     void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r,    \
1357fcf5ef2aSThomas Huth                              ppc_avr_t *b)                      \
1358fcf5ef2aSThomas Huth     {                                                           \
1359fcf5ef2aSThomas Huth         int i;                                                  \
1360fcf5ef2aSThomas Huth         float_status s = env->vec_status;                       \
1361fcf5ef2aSThomas Huth                                                                 \
1362fcf5ef2aSThomas Huth         set_float_rounding_mode(rounding, &s);                  \
136305ee3e8aSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->f32); i++) {              \
136405ee3e8aSMark Cave-Ayland             r->f32[i] = float32_round_to_int (b->f32[i], &s);   \
1365fcf5ef2aSThomas Huth         }                                                       \
1366fcf5ef2aSThomas Huth     }
1367fcf5ef2aSThomas Huth VRFI(n, float_round_nearest_even)
1368fcf5ef2aSThomas Huth VRFI(m, float_round_down)
1369fcf5ef2aSThomas Huth VRFI(p, float_round_up)
1370fcf5ef2aSThomas Huth VRFI(z, float_round_to_zero)
1371fcf5ef2aSThomas Huth #undef VRFI
1372fcf5ef2aSThomas Huth 
1373fcf5ef2aSThomas Huth void helper_vrsqrtefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1374fcf5ef2aSThomas Huth {
1375fcf5ef2aSThomas Huth     int i;
1376fcf5ef2aSThomas Huth 
137705ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
137805ee3e8aSMark Cave-Ayland         float32 t = float32_sqrt(b->f32[i], &env->vec_status);
1379fcf5ef2aSThomas Huth 
138005ee3e8aSMark Cave-Ayland         r->f32[i] = float32_div(float32_one, t, &env->vec_status);
1381fcf5ef2aSThomas Huth     }
1382fcf5ef2aSThomas Huth }
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth #define VRLMI(name, size, element, insert)                            \
1385fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)          \
1386fcf5ef2aSThomas Huth {                                                                     \
1387fcf5ef2aSThomas Huth     int i;                                                            \
1388fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                    \
1389fcf5ef2aSThomas Huth         uint##size##_t src1 = a->element[i];                          \
1390fcf5ef2aSThomas Huth         uint##size##_t src2 = b->element[i];                          \
1391fcf5ef2aSThomas Huth         uint##size##_t src3 = r->element[i];                          \
1392fcf5ef2aSThomas Huth         uint##size##_t begin, end, shift, mask, rot_val;              \
1393fcf5ef2aSThomas Huth                                                                       \
1394fcf5ef2aSThomas Huth         shift = extract##size(src2, 0, 6);                            \
1395fcf5ef2aSThomas Huth         end   = extract##size(src2, 8, 6);                            \
1396fcf5ef2aSThomas Huth         begin = extract##size(src2, 16, 6);                           \
1397fcf5ef2aSThomas Huth         rot_val = rol##size(src1, shift);                             \
1398fcf5ef2aSThomas Huth         mask = mask_u##size(begin, end);                              \
1399fcf5ef2aSThomas Huth         if (insert) {                                                 \
1400fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask) | (src3 & ~mask);        \
1401fcf5ef2aSThomas Huth         } else {                                                      \
1402fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask);                         \
1403fcf5ef2aSThomas Huth         }                                                             \
1404fcf5ef2aSThomas Huth     }                                                                 \
1405fcf5ef2aSThomas Huth }
1406fcf5ef2aSThomas Huth 
1407fcf5ef2aSThomas Huth VRLMI(vrldmi, 64, u64, 1);
1408fcf5ef2aSThomas Huth VRLMI(vrlwmi, 32, u32, 1);
1409fcf5ef2aSThomas Huth VRLMI(vrldnm, 64, u64, 0);
1410fcf5ef2aSThomas Huth VRLMI(vrlwnm, 32, u32, 0);
1411fcf5ef2aSThomas Huth 
1412fcf5ef2aSThomas Huth void helper_vsel(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b,
1413fcf5ef2aSThomas Huth                  ppc_avr_t *c)
1414fcf5ef2aSThomas Huth {
1415fcf5ef2aSThomas Huth     r->u64[0] = (a->u64[0] & ~c->u64[0]) | (b->u64[0] & c->u64[0]);
1416fcf5ef2aSThomas Huth     r->u64[1] = (a->u64[1] & ~c->u64[1]) | (b->u64[1] & c->u64[1]);
1417fcf5ef2aSThomas Huth }
1418fcf5ef2aSThomas Huth 
1419fcf5ef2aSThomas Huth void helper_vexptefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1420fcf5ef2aSThomas Huth {
1421fcf5ef2aSThomas Huth     int i;
1422fcf5ef2aSThomas Huth 
142305ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
142405ee3e8aSMark Cave-Ayland         r->f32[i] = float32_exp2(b->f32[i], &env->vec_status);
1425fcf5ef2aSThomas Huth     }
1426fcf5ef2aSThomas Huth }
1427fcf5ef2aSThomas Huth 
1428fcf5ef2aSThomas Huth void helper_vlogefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1429fcf5ef2aSThomas Huth {
1430fcf5ef2aSThomas Huth     int i;
1431fcf5ef2aSThomas Huth 
143205ee3e8aSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->f32); i++) {
143305ee3e8aSMark Cave-Ayland         r->f32[i] = float32_log2(b->f32[i], &env->vec_status);
1434fcf5ef2aSThomas Huth     }
1435fcf5ef2aSThomas Huth }
1436fcf5ef2aSThomas Huth 
143760caf221SAvinesh Kumar #if defined(HOST_WORDS_BIGENDIAN)
143860caf221SAvinesh Kumar #define VEXTU_X_DO(name, size, left)                                \
143960caf221SAvinesh Kumar     target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
144060caf221SAvinesh Kumar     {                                                               \
144160caf221SAvinesh Kumar         int index;                                                  \
144260caf221SAvinesh Kumar         if (left) {                                                 \
144360caf221SAvinesh Kumar             index = (a & 0xf) * 8;                                  \
144460caf221SAvinesh Kumar         } else {                                                    \
144560caf221SAvinesh Kumar             index = ((15 - (a & 0xf) + 1) * 8) - size;              \
144660caf221SAvinesh Kumar         }                                                           \
144760caf221SAvinesh Kumar         return int128_getlo(int128_rshift(b->s128, index)) &        \
144860caf221SAvinesh Kumar             MAKE_64BIT_MASK(0, size);                               \
144960caf221SAvinesh Kumar     }
145060caf221SAvinesh Kumar #else
145160caf221SAvinesh Kumar #define VEXTU_X_DO(name, size, left)                                \
145260caf221SAvinesh Kumar     target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
145360caf221SAvinesh Kumar     {                                                               \
145460caf221SAvinesh Kumar         int index;                                                  \
145560caf221SAvinesh Kumar         if (left) {                                                 \
145660caf221SAvinesh Kumar             index = ((15 - (a & 0xf) + 1) * 8) - size;              \
145760caf221SAvinesh Kumar         } else {                                                    \
145860caf221SAvinesh Kumar             index = (a & 0xf) * 8;                                  \
145960caf221SAvinesh Kumar         }                                                           \
146060caf221SAvinesh Kumar         return int128_getlo(int128_rshift(b->s128, index)) &        \
146160caf221SAvinesh Kumar             MAKE_64BIT_MASK(0, size);                               \
146260caf221SAvinesh Kumar     }
146360caf221SAvinesh Kumar #endif
146460caf221SAvinesh Kumar 
146560caf221SAvinesh Kumar VEXTU_X_DO(vextublx,  8, 1)
146660caf221SAvinesh Kumar VEXTU_X_DO(vextuhlx, 16, 1)
146760caf221SAvinesh Kumar VEXTU_X_DO(vextuwlx, 32, 1)
146860caf221SAvinesh Kumar VEXTU_X_DO(vextubrx,  8, 0)
146960caf221SAvinesh Kumar VEXTU_X_DO(vextuhrx, 16, 0)
147060caf221SAvinesh Kumar VEXTU_X_DO(vextuwrx, 32, 0)
147160caf221SAvinesh Kumar #undef VEXTU_X_DO
147260caf221SAvinesh Kumar 
1473fcf5ef2aSThomas Huth void helper_vslv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1474fcf5ef2aSThomas Huth {
1475fcf5ef2aSThomas Huth     int i;
1476fcf5ef2aSThomas Huth     unsigned int shift, bytes, size;
1477fcf5ef2aSThomas Huth 
1478fcf5ef2aSThomas Huth     size = ARRAY_SIZE(r->u8);
1479fcf5ef2aSThomas Huth     for (i = 0; i < size; i++) {
148063be02fcSAnton Blanchard         shift = b->VsrB(i) & 0x7;             /* extract shift value */
148163be02fcSAnton Blanchard         bytes = (a->VsrB(i) << 8) +           /* extract adjacent bytes */
148263be02fcSAnton Blanchard             (((i + 1) < size) ? a->VsrB(i + 1) : 0);
148363be02fcSAnton Blanchard         r->VsrB(i) = (bytes << shift) >> 8;   /* shift and store result */
1484fcf5ef2aSThomas Huth     }
1485fcf5ef2aSThomas Huth }
1486fcf5ef2aSThomas Huth 
1487fcf5ef2aSThomas Huth void helper_vsrv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1488fcf5ef2aSThomas Huth {
1489fcf5ef2aSThomas Huth     int i;
1490fcf5ef2aSThomas Huth     unsigned int shift, bytes;
1491fcf5ef2aSThomas Huth 
1492b6cb41b2SDavid Gibson     /*
1493b6cb41b2SDavid Gibson      * Use reverse order, as destination and source register can be
1494b6cb41b2SDavid Gibson      * same. Its being modified in place saving temporary, reverse
1495b6cb41b2SDavid Gibson      * order will guarantee that computed result is not fed back.
1496fcf5ef2aSThomas Huth      */
1497fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
149863be02fcSAnton Blanchard         shift = b->VsrB(i) & 0x7;               /* extract shift value */
149963be02fcSAnton Blanchard         bytes = ((i ? a->VsrB(i - 1) : 0) << 8) + a->VsrB(i);
1500fcf5ef2aSThomas Huth                                                 /* extract adjacent bytes */
150163be02fcSAnton Blanchard         r->VsrB(i) = (bytes >> shift) & 0xFF;   /* shift and store result */
1502fcf5ef2aSThomas Huth     }
1503fcf5ef2aSThomas Huth }
1504fcf5ef2aSThomas Huth 
1505fcf5ef2aSThomas Huth void helper_vsldoi(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t shift)
1506fcf5ef2aSThomas Huth {
1507fcf5ef2aSThomas Huth     int sh = shift & 0xf;
1508fcf5ef2aSThomas Huth     int i;
1509fcf5ef2aSThomas Huth     ppc_avr_t result;
1510fcf5ef2aSThomas Huth 
1511fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1512fcf5ef2aSThomas Huth         int index = sh + i;
1513fcf5ef2aSThomas Huth         if (index > 0xf) {
151460594feaSMark Cave-Ayland             result.VsrB(i) = b->VsrB(index - 0x10);
1515fcf5ef2aSThomas Huth         } else {
151660594feaSMark Cave-Ayland             result.VsrB(i) = a->VsrB(index);
1517fcf5ef2aSThomas Huth         }
1518fcf5ef2aSThomas Huth     }
1519fcf5ef2aSThomas Huth     *r = result;
1520fcf5ef2aSThomas Huth }
1521fcf5ef2aSThomas Huth 
1522fcf5ef2aSThomas Huth void helper_vslo(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1523fcf5ef2aSThomas Huth {
15243c385a93SMark Cave-Ayland     int sh = (b->VsrB(0xf) >> 3) & 0xf;
1525fcf5ef2aSThomas Huth 
1526fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1527fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1528fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
1529fcf5ef2aSThomas Huth #else
1530fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1531fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1532fcf5ef2aSThomas Huth #endif
1533fcf5ef2aSThomas Huth }
1534fcf5ef2aSThomas Huth 
1535fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1536fcf5ef2aSThomas Huth #define VINSERT(suffix, element)                                            \
1537fcf5ef2aSThomas Huth     void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1538fcf5ef2aSThomas Huth     {                                                                       \
15394fff7218SLaurent Vivier         memmove(&r->u8[index], &b->u8[8 - sizeof(r->element[0])],           \
1540fcf5ef2aSThomas Huth                sizeof(r->element[0]));                                      \
1541fcf5ef2aSThomas Huth     }
1542fcf5ef2aSThomas Huth #else
1543fcf5ef2aSThomas Huth #define VINSERT(suffix, element)                                            \
1544fcf5ef2aSThomas Huth     void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1545fcf5ef2aSThomas Huth     {                                                                       \
1546fcf5ef2aSThomas Huth         uint32_t d = (16 - index) - sizeof(r->element[0]);                  \
1547fcf5ef2aSThomas Huth         memmove(&r->u8[d], &b->u8[8], sizeof(r->element[0]));               \
1548fcf5ef2aSThomas Huth     }
1549fcf5ef2aSThomas Huth #endif
1550fcf5ef2aSThomas Huth VINSERT(b, u8)
1551fcf5ef2aSThomas Huth VINSERT(h, u16)
1552fcf5ef2aSThomas Huth VINSERT(w, u32)
1553fcf5ef2aSThomas Huth VINSERT(d, u64)
1554fcf5ef2aSThomas Huth #undef VINSERT
1555fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1556fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1557fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1558fcf5ef2aSThomas Huth     {                                                                        \
1559fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1560fcf5ef2aSThomas Huth         memmove(&r->u8[8 - es], &b->u8[index], es);                          \
1561fcf5ef2aSThomas Huth         memset(&r->u8[8], 0, 8);                                             \
1562fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8 - es);                                        \
1563fcf5ef2aSThomas Huth     }
1564fcf5ef2aSThomas Huth #else
1565fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1566fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1567fcf5ef2aSThomas Huth     {                                                                        \
1568fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1569fcf5ef2aSThomas Huth         uint32_t s = (16 - index) - es;                                      \
1570fcf5ef2aSThomas Huth         memmove(&r->u8[8], &b->u8[s], es);                                   \
1571fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8);                                             \
1572fcf5ef2aSThomas Huth         memset(&r->u8[8 + es], 0, 8 - es);                                   \
1573fcf5ef2aSThomas Huth     }
1574fcf5ef2aSThomas Huth #endif
1575fcf5ef2aSThomas Huth VEXTRACT(ub, u8)
1576fcf5ef2aSThomas Huth VEXTRACT(uh, u16)
1577fcf5ef2aSThomas Huth VEXTRACT(uw, u32)
1578fcf5ef2aSThomas Huth VEXTRACT(d, u64)
1579fcf5ef2aSThomas Huth #undef VEXTRACT
1580fcf5ef2aSThomas Huth 
15815ba5335dSMark Cave-Ayland void helper_xxextractuw(CPUPPCState *env, ppc_vsr_t *xt,
15825ba5335dSMark Cave-Ayland                         ppc_vsr_t *xb, uint32_t index)
15838ad901e5SNikunj A Dadhania {
158403b32c09SMark Cave-Ayland     ppc_vsr_t t = { };
15858ad901e5SNikunj A Dadhania     size_t es = sizeof(uint32_t);
15868ad901e5SNikunj A Dadhania     uint32_t ext_index;
15878ad901e5SNikunj A Dadhania     int i;
15888ad901e5SNikunj A Dadhania 
15898ad901e5SNikunj A Dadhania     ext_index = index;
15908ad901e5SNikunj A Dadhania     for (i = 0; i < es; i++, ext_index++) {
159103b32c09SMark Cave-Ayland         t.VsrB(8 - es + i) = xb->VsrB(ext_index % 16);
15928ad901e5SNikunj A Dadhania     }
15938ad901e5SNikunj A Dadhania 
159403b32c09SMark Cave-Ayland     *xt = t;
15958ad901e5SNikunj A Dadhania }
15968ad901e5SNikunj A Dadhania 
15975ba5335dSMark Cave-Ayland void helper_xxinsertw(CPUPPCState *env, ppc_vsr_t *xt,
15985ba5335dSMark Cave-Ayland                       ppc_vsr_t *xb, uint32_t index)
15993398b742SNikunj A Dadhania {
160003b32c09SMark Cave-Ayland     ppc_vsr_t t = *xt;
16013398b742SNikunj A Dadhania     size_t es = sizeof(uint32_t);
16023398b742SNikunj A Dadhania     int ins_index, i = 0;
16033398b742SNikunj A Dadhania 
16043398b742SNikunj A Dadhania     ins_index = index;
16053398b742SNikunj A Dadhania     for (i = 0; i < es && ins_index < 16; i++, ins_index++) {
160603b32c09SMark Cave-Ayland         t.VsrB(ins_index) = xb->VsrB(8 - es + i);
16073398b742SNikunj A Dadhania     }
16083398b742SNikunj A Dadhania 
160903b32c09SMark Cave-Ayland     *xt = t;
16103398b742SNikunj A Dadhania }
16113398b742SNikunj A Dadhania 
1612634c5835SMark Cave-Ayland #define VEXT_SIGNED(name, element, cast)                            \
1613fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *b)                      \
1614fcf5ef2aSThomas Huth {                                                                   \
1615fcf5ef2aSThomas Huth     int i;                                                          \
161660594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1617634c5835SMark Cave-Ayland         r->element[i] = (cast)b->element[i];                        \
1618fcf5ef2aSThomas Huth     }                                                               \
1619fcf5ef2aSThomas Huth }
1620634c5835SMark Cave-Ayland VEXT_SIGNED(vextsb2w, s32, int8_t)
1621634c5835SMark Cave-Ayland VEXT_SIGNED(vextsb2d, s64, int8_t)
1622634c5835SMark Cave-Ayland VEXT_SIGNED(vextsh2w, s32, int16_t)
1623634c5835SMark Cave-Ayland VEXT_SIGNED(vextsh2d, s64, int16_t)
1624634c5835SMark Cave-Ayland VEXT_SIGNED(vextsw2d, s64, int32_t)
1625fcf5ef2aSThomas Huth #undef VEXT_SIGNED
1626fcf5ef2aSThomas Huth 
1627fcf5ef2aSThomas Huth #define VNEG(name, element)                                         \
1628fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *b)                      \
1629fcf5ef2aSThomas Huth {                                                                   \
1630fcf5ef2aSThomas Huth     int i;                                                          \
163160594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1632fcf5ef2aSThomas Huth         r->element[i] = -b->element[i];                             \
1633fcf5ef2aSThomas Huth     }                                                               \
1634fcf5ef2aSThomas Huth }
1635fcf5ef2aSThomas Huth VNEG(vnegw, s32)
1636fcf5ef2aSThomas Huth VNEG(vnegd, s64)
1637fcf5ef2aSThomas Huth #undef VNEG
1638fcf5ef2aSThomas Huth 
1639fcf5ef2aSThomas Huth void helper_vsro(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1640fcf5ef2aSThomas Huth {
16413c385a93SMark Cave-Ayland     int sh = (b->VsrB(0xf) >> 3) & 0xf;
1642fcf5ef2aSThomas Huth 
1643fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1644fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1645fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1646fcf5ef2aSThomas Huth #else
1647fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1648fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
1649fcf5ef2aSThomas Huth #endif
1650fcf5ef2aSThomas Huth }
1651fcf5ef2aSThomas Huth 
1652fcf5ef2aSThomas Huth void helper_vsubcuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1653fcf5ef2aSThomas Huth {
1654fcf5ef2aSThomas Huth     int i;
1655fcf5ef2aSThomas Huth 
1656fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
1657fcf5ef2aSThomas Huth         r->u32[i] = a->u32[i] >= b->u32[i];
1658fcf5ef2aSThomas Huth     }
1659fcf5ef2aSThomas Huth }
1660fcf5ef2aSThomas Huth 
1661fcf5ef2aSThomas Huth void helper_vsumsws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1662fcf5ef2aSThomas Huth {
1663fcf5ef2aSThomas Huth     int64_t t;
1664fcf5ef2aSThomas Huth     int i, upper;
1665fcf5ef2aSThomas Huth     ppc_avr_t result;
1666fcf5ef2aSThomas Huth     int sat = 0;
1667fcf5ef2aSThomas Huth 
1668fcf5ef2aSThomas Huth     upper = ARRAY_SIZE(r->s32) - 1;
166960594feaSMark Cave-Ayland     t = (int64_t)b->VsrSW(upper);
1670fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
167160594feaSMark Cave-Ayland         t += a->VsrSW(i);
167260594feaSMark Cave-Ayland         result.VsrSW(i) = 0;
1673fcf5ef2aSThomas Huth     }
167460594feaSMark Cave-Ayland     result.VsrSW(upper) = cvtsdsw(t, &sat);
1675fcf5ef2aSThomas Huth     *r = result;
1676fcf5ef2aSThomas Huth 
1677fcf5ef2aSThomas Huth     if (sat) {
16786175f5a0SRichard Henderson         set_vscr_sat(env);
1679fcf5ef2aSThomas Huth     }
1680fcf5ef2aSThomas Huth }
1681fcf5ef2aSThomas Huth 
1682fcf5ef2aSThomas Huth void helper_vsum2sws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1683fcf5ef2aSThomas Huth {
1684fcf5ef2aSThomas Huth     int i, j, upper;
1685fcf5ef2aSThomas Huth     ppc_avr_t result;
1686fcf5ef2aSThomas Huth     int sat = 0;
1687fcf5ef2aSThomas Huth 
1688fcf5ef2aSThomas Huth     upper = 1;
1689fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
169060594feaSMark Cave-Ayland         int64_t t = (int64_t)b->VsrSW(upper + i * 2);
1691fcf5ef2aSThomas Huth 
16927fa0ddc1SAnton Blanchard         result.VsrD(i) = 0;
1693fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u64); j++) {
169460594feaSMark Cave-Ayland             t += a->VsrSW(2 * i + j);
1695fcf5ef2aSThomas Huth         }
169660594feaSMark Cave-Ayland         result.VsrSW(upper + i * 2) = cvtsdsw(t, &sat);
1697fcf5ef2aSThomas Huth     }
1698fcf5ef2aSThomas Huth 
1699fcf5ef2aSThomas Huth     *r = result;
1700fcf5ef2aSThomas Huth     if (sat) {
17016175f5a0SRichard Henderson         set_vscr_sat(env);
1702fcf5ef2aSThomas Huth     }
1703fcf5ef2aSThomas Huth }
1704fcf5ef2aSThomas Huth 
1705fcf5ef2aSThomas Huth void helper_vsum4sbs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1706fcf5ef2aSThomas Huth {
1707fcf5ef2aSThomas Huth     int i, j;
1708fcf5ef2aSThomas Huth     int sat = 0;
1709fcf5ef2aSThomas Huth 
1710fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
1711fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
1712fcf5ef2aSThomas Huth 
1713fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->s32); j++) {
1714fcf5ef2aSThomas Huth             t += a->s8[4 * i + j];
1715fcf5ef2aSThomas Huth         }
1716fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
1717fcf5ef2aSThomas Huth     }
1718fcf5ef2aSThomas Huth 
1719fcf5ef2aSThomas Huth     if (sat) {
17206175f5a0SRichard Henderson         set_vscr_sat(env);
1721fcf5ef2aSThomas Huth     }
1722fcf5ef2aSThomas Huth }
1723fcf5ef2aSThomas Huth 
1724fcf5ef2aSThomas Huth void helper_vsum4shs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1725fcf5ef2aSThomas Huth {
1726fcf5ef2aSThomas Huth     int sat = 0;
1727fcf5ef2aSThomas Huth     int i;
1728fcf5ef2aSThomas Huth 
1729fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
1730fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
1731fcf5ef2aSThomas Huth 
1732fcf5ef2aSThomas Huth         t += a->s16[2 * i] + a->s16[2 * i + 1];
1733fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
1734fcf5ef2aSThomas Huth     }
1735fcf5ef2aSThomas Huth 
1736fcf5ef2aSThomas Huth     if (sat) {
17376175f5a0SRichard Henderson         set_vscr_sat(env);
1738fcf5ef2aSThomas Huth     }
1739fcf5ef2aSThomas Huth }
1740fcf5ef2aSThomas Huth 
1741fcf5ef2aSThomas Huth void helper_vsum4ubs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1742fcf5ef2aSThomas Huth {
1743fcf5ef2aSThomas Huth     int i, j;
1744fcf5ef2aSThomas Huth     int sat = 0;
1745fcf5ef2aSThomas Huth 
1746fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
1747fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)b->u32[i];
1748fcf5ef2aSThomas Huth 
1749fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u32); j++) {
1750fcf5ef2aSThomas Huth             t += a->u8[4 * i + j];
1751fcf5ef2aSThomas Huth         }
1752fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
1753fcf5ef2aSThomas Huth     }
1754fcf5ef2aSThomas Huth 
1755fcf5ef2aSThomas Huth     if (sat) {
17566175f5a0SRichard Henderson         set_vscr_sat(env);
1757fcf5ef2aSThomas Huth     }
1758fcf5ef2aSThomas Huth }
1759fcf5ef2aSThomas Huth 
1760fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1761fcf5ef2aSThomas Huth #define UPKHI 1
1762fcf5ef2aSThomas Huth #define UPKLO 0
1763fcf5ef2aSThomas Huth #else
1764fcf5ef2aSThomas Huth #define UPKHI 0
1765fcf5ef2aSThomas Huth #define UPKLO 1
1766fcf5ef2aSThomas Huth #endif
1767fcf5ef2aSThomas Huth #define VUPKPX(suffix, hi)                                              \
1768fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
1769fcf5ef2aSThomas Huth     {                                                                   \
1770fcf5ef2aSThomas Huth         int i;                                                          \
1771fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
1772fcf5ef2aSThomas Huth                                                                         \
1773fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->u32); i++) {                      \
1774fcf5ef2aSThomas Huth             uint16_t e = b->u16[hi ? i : i + 4];                        \
1775fcf5ef2aSThomas Huth             uint8_t a = (e >> 15) ? 0xff : 0;                           \
1776fcf5ef2aSThomas Huth             uint8_t r = (e >> 10) & 0x1f;                               \
1777fcf5ef2aSThomas Huth             uint8_t g = (e >> 5) & 0x1f;                                \
1778fcf5ef2aSThomas Huth             uint8_t b = e & 0x1f;                                       \
1779fcf5ef2aSThomas Huth                                                                         \
1780fcf5ef2aSThomas Huth             result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b;       \
1781fcf5ef2aSThomas Huth         }                                                               \
1782fcf5ef2aSThomas Huth         *r = result;                                                    \
1783fcf5ef2aSThomas Huth     }
1784fcf5ef2aSThomas Huth VUPKPX(lpx, UPKLO)
1785fcf5ef2aSThomas Huth VUPKPX(hpx, UPKHI)
1786fcf5ef2aSThomas Huth #undef VUPKPX
1787fcf5ef2aSThomas Huth 
1788fcf5ef2aSThomas Huth #define VUPK(suffix, unpacked, packee, hi)                              \
1789fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
1790fcf5ef2aSThomas Huth     {                                                                   \
1791fcf5ef2aSThomas Huth         int i;                                                          \
1792fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
1793fcf5ef2aSThomas Huth                                                                         \
1794fcf5ef2aSThomas Huth         if (hi) {                                                       \
1795fcf5ef2aSThomas Huth             for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) {             \
1796fcf5ef2aSThomas Huth                 result.unpacked[i] = b->packee[i];                      \
1797fcf5ef2aSThomas Huth             }                                                           \
1798fcf5ef2aSThomas Huth         } else {                                                        \
1799fcf5ef2aSThomas Huth             for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
1800fcf5ef2aSThomas Huth                  i++) {                                                 \
1801fcf5ef2aSThomas Huth                 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
1802fcf5ef2aSThomas Huth             }                                                           \
1803fcf5ef2aSThomas Huth         }                                                               \
1804fcf5ef2aSThomas Huth         *r = result;                                                    \
1805fcf5ef2aSThomas Huth     }
1806fcf5ef2aSThomas Huth VUPK(hsb, s16, s8, UPKHI)
1807fcf5ef2aSThomas Huth VUPK(hsh, s32, s16, UPKHI)
1808fcf5ef2aSThomas Huth VUPK(hsw, s64, s32, UPKHI)
1809fcf5ef2aSThomas Huth VUPK(lsb, s16, s8, UPKLO)
1810fcf5ef2aSThomas Huth VUPK(lsh, s32, s16, UPKLO)
1811fcf5ef2aSThomas Huth VUPK(lsw, s64, s32, UPKLO)
1812fcf5ef2aSThomas Huth #undef VUPK
1813fcf5ef2aSThomas Huth #undef UPKHI
1814fcf5ef2aSThomas Huth #undef UPKLO
1815fcf5ef2aSThomas Huth 
1816fcf5ef2aSThomas Huth #define VGENERIC_DO(name, element)                                      \
1817fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *b)                     \
1818fcf5ef2aSThomas Huth     {                                                                   \
1819fcf5ef2aSThomas Huth         int i;                                                          \
1820fcf5ef2aSThomas Huth                                                                         \
182160594feaSMark Cave-Ayland         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1822fcf5ef2aSThomas Huth             r->element[i] = name(b->element[i]);                        \
1823fcf5ef2aSThomas Huth         }                                                               \
1824fcf5ef2aSThomas Huth     }
1825fcf5ef2aSThomas Huth 
1826fcf5ef2aSThomas Huth #define clzb(v) ((v) ? clz32((uint32_t)(v) << 24) : 8)
1827fcf5ef2aSThomas Huth #define clzh(v) ((v) ? clz32((uint32_t)(v) << 16) : 16)
1828fcf5ef2aSThomas Huth 
1829fcf5ef2aSThomas Huth VGENERIC_DO(clzb, u8)
1830fcf5ef2aSThomas Huth VGENERIC_DO(clzh, u16)
1831fcf5ef2aSThomas Huth 
1832fcf5ef2aSThomas Huth #undef clzb
1833fcf5ef2aSThomas Huth #undef clzh
1834fcf5ef2aSThomas Huth 
1835fcf5ef2aSThomas Huth #define ctzb(v) ((v) ? ctz32(v) : 8)
1836fcf5ef2aSThomas Huth #define ctzh(v) ((v) ? ctz32(v) : 16)
1837fcf5ef2aSThomas Huth #define ctzw(v) ctz32((v))
1838fcf5ef2aSThomas Huth #define ctzd(v) ctz64((v))
1839fcf5ef2aSThomas Huth 
1840fcf5ef2aSThomas Huth VGENERIC_DO(ctzb, u8)
1841fcf5ef2aSThomas Huth VGENERIC_DO(ctzh, u16)
1842fcf5ef2aSThomas Huth VGENERIC_DO(ctzw, u32)
1843fcf5ef2aSThomas Huth VGENERIC_DO(ctzd, u64)
1844fcf5ef2aSThomas Huth 
1845fcf5ef2aSThomas Huth #undef ctzb
1846fcf5ef2aSThomas Huth #undef ctzh
1847fcf5ef2aSThomas Huth #undef ctzw
1848fcf5ef2aSThomas Huth #undef ctzd
1849fcf5ef2aSThomas Huth 
1850fcf5ef2aSThomas Huth #define popcntb(v) ctpop8(v)
1851fcf5ef2aSThomas Huth #define popcnth(v) ctpop16(v)
1852fcf5ef2aSThomas Huth #define popcntw(v) ctpop32(v)
1853fcf5ef2aSThomas Huth #define popcntd(v) ctpop64(v)
1854fcf5ef2aSThomas Huth 
1855fcf5ef2aSThomas Huth VGENERIC_DO(popcntb, u8)
1856fcf5ef2aSThomas Huth VGENERIC_DO(popcnth, u16)
1857fcf5ef2aSThomas Huth VGENERIC_DO(popcntw, u32)
1858fcf5ef2aSThomas Huth VGENERIC_DO(popcntd, u64)
1859fcf5ef2aSThomas Huth 
1860fcf5ef2aSThomas Huth #undef popcntb
1861fcf5ef2aSThomas Huth #undef popcnth
1862fcf5ef2aSThomas Huth #undef popcntw
1863fcf5ef2aSThomas Huth #undef popcntd
1864fcf5ef2aSThomas Huth 
1865fcf5ef2aSThomas Huth #undef VGENERIC_DO
1866fcf5ef2aSThomas Huth 
1867fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1868fcf5ef2aSThomas Huth #define QW_ONE { .u64 = { 0, 1 } }
1869fcf5ef2aSThomas Huth #else
1870fcf5ef2aSThomas Huth #define QW_ONE { .u64 = { 1, 0 } }
1871fcf5ef2aSThomas Huth #endif
1872fcf5ef2aSThomas Huth 
1873fcf5ef2aSThomas Huth #ifndef CONFIG_INT128
1874fcf5ef2aSThomas Huth 
1875fcf5ef2aSThomas Huth static inline void avr_qw_not(ppc_avr_t *t, ppc_avr_t a)
1876fcf5ef2aSThomas Huth {
1877fcf5ef2aSThomas Huth     t->u64[0] = ~a.u64[0];
1878fcf5ef2aSThomas Huth     t->u64[1] = ~a.u64[1];
1879fcf5ef2aSThomas Huth }
1880fcf5ef2aSThomas Huth 
1881fcf5ef2aSThomas Huth static int avr_qw_cmpu(ppc_avr_t a, ppc_avr_t b)
1882fcf5ef2aSThomas Huth {
18833c385a93SMark Cave-Ayland     if (a.VsrD(0) < b.VsrD(0)) {
1884fcf5ef2aSThomas Huth         return -1;
18853c385a93SMark Cave-Ayland     } else if (a.VsrD(0) > b.VsrD(0)) {
1886fcf5ef2aSThomas Huth         return 1;
18873c385a93SMark Cave-Ayland     } else if (a.VsrD(1) < b.VsrD(1)) {
1888fcf5ef2aSThomas Huth         return -1;
18893c385a93SMark Cave-Ayland     } else if (a.VsrD(1) > b.VsrD(1)) {
1890fcf5ef2aSThomas Huth         return 1;
1891fcf5ef2aSThomas Huth     } else {
1892fcf5ef2aSThomas Huth         return 0;
1893fcf5ef2aSThomas Huth     }
1894fcf5ef2aSThomas Huth }
1895fcf5ef2aSThomas Huth 
1896fcf5ef2aSThomas Huth static void avr_qw_add(ppc_avr_t *t, ppc_avr_t a, ppc_avr_t b)
1897fcf5ef2aSThomas Huth {
18983c385a93SMark Cave-Ayland     t->VsrD(1) = a.VsrD(1) + b.VsrD(1);
18993c385a93SMark Cave-Ayland     t->VsrD(0) = a.VsrD(0) + b.VsrD(0) +
19003c385a93SMark Cave-Ayland                      (~a.VsrD(1) < b.VsrD(1));
1901fcf5ef2aSThomas Huth }
1902fcf5ef2aSThomas Huth 
1903fcf5ef2aSThomas Huth static int avr_qw_addc(ppc_avr_t *t, ppc_avr_t a, ppc_avr_t b)
1904fcf5ef2aSThomas Huth {
1905fcf5ef2aSThomas Huth     ppc_avr_t not_a;
19063c385a93SMark Cave-Ayland     t->VsrD(1) = a.VsrD(1) + b.VsrD(1);
19073c385a93SMark Cave-Ayland     t->VsrD(0) = a.VsrD(0) + b.VsrD(0) +
19083c385a93SMark Cave-Ayland                      (~a.VsrD(1) < b.VsrD(1));
1909fcf5ef2aSThomas Huth     avr_qw_not(&not_a, a);
1910fcf5ef2aSThomas Huth     return avr_qw_cmpu(not_a, b) < 0;
1911fcf5ef2aSThomas Huth }
1912fcf5ef2aSThomas Huth 
1913fcf5ef2aSThomas Huth #endif
1914fcf5ef2aSThomas Huth 
1915fcf5ef2aSThomas Huth void helper_vadduqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1916fcf5ef2aSThomas Huth {
1917fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1918fcf5ef2aSThomas Huth     r->u128 = a->u128 + b->u128;
1919fcf5ef2aSThomas Huth #else
1920fcf5ef2aSThomas Huth     avr_qw_add(r, *a, *b);
1921fcf5ef2aSThomas Huth #endif
1922fcf5ef2aSThomas Huth }
1923fcf5ef2aSThomas Huth 
1924fcf5ef2aSThomas Huth void helper_vaddeuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1925fcf5ef2aSThomas Huth {
1926fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1927fcf5ef2aSThomas Huth     r->u128 = a->u128 + b->u128 + (c->u128 & 1);
1928fcf5ef2aSThomas Huth #else
1929fcf5ef2aSThomas Huth 
19303c385a93SMark Cave-Ayland     if (c->VsrD(1) & 1) {
1931fcf5ef2aSThomas Huth         ppc_avr_t tmp;
1932fcf5ef2aSThomas Huth 
19333c385a93SMark Cave-Ayland         tmp.VsrD(0) = 0;
19343c385a93SMark Cave-Ayland         tmp.VsrD(1) = c->VsrD(1) & 1;
1935fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
1936fcf5ef2aSThomas Huth         avr_qw_add(r, tmp, *b);
1937fcf5ef2aSThomas Huth     } else {
1938fcf5ef2aSThomas Huth         avr_qw_add(r, *a, *b);
1939fcf5ef2aSThomas Huth     }
1940fcf5ef2aSThomas Huth #endif
1941fcf5ef2aSThomas Huth }
1942fcf5ef2aSThomas Huth 
1943fcf5ef2aSThomas Huth void helper_vaddcuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1944fcf5ef2aSThomas Huth {
1945fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1946fcf5ef2aSThomas Huth     r->u128 = (~a->u128 < b->u128);
1947fcf5ef2aSThomas Huth #else
1948fcf5ef2aSThomas Huth     ppc_avr_t not_a;
1949fcf5ef2aSThomas Huth 
1950fcf5ef2aSThomas Huth     avr_qw_not(&not_a, *a);
1951fcf5ef2aSThomas Huth 
19523c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
19533c385a93SMark Cave-Ayland     r->VsrD(1) = (avr_qw_cmpu(not_a, *b) < 0);
1954fcf5ef2aSThomas Huth #endif
1955fcf5ef2aSThomas Huth }
1956fcf5ef2aSThomas Huth 
1957fcf5ef2aSThomas Huth void helper_vaddecuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1958fcf5ef2aSThomas Huth {
1959fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1960fcf5ef2aSThomas Huth     int carry_out = (~a->u128 < b->u128);
1961fcf5ef2aSThomas Huth     if (!carry_out && (c->u128 & 1)) {
1962fcf5ef2aSThomas Huth         carry_out = ((a->u128 + b->u128 + 1) == 0) &&
1963fcf5ef2aSThomas Huth                     ((a->u128 != 0) || (b->u128 != 0));
1964fcf5ef2aSThomas Huth     }
1965fcf5ef2aSThomas Huth     r->u128 = carry_out;
1966fcf5ef2aSThomas Huth #else
1967fcf5ef2aSThomas Huth 
19683c385a93SMark Cave-Ayland     int carry_in = c->VsrD(1) & 1;
1969fcf5ef2aSThomas Huth     int carry_out = 0;
1970fcf5ef2aSThomas Huth     ppc_avr_t tmp;
1971fcf5ef2aSThomas Huth 
1972fcf5ef2aSThomas Huth     carry_out = avr_qw_addc(&tmp, *a, *b);
1973fcf5ef2aSThomas Huth 
1974fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
1975fcf5ef2aSThomas Huth         ppc_avr_t one = QW_ONE;
1976fcf5ef2aSThomas Huth         carry_out = avr_qw_addc(&tmp, tmp, one);
1977fcf5ef2aSThomas Huth     }
19783c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
19793c385a93SMark Cave-Ayland     r->VsrD(1) = carry_out;
1980fcf5ef2aSThomas Huth #endif
1981fcf5ef2aSThomas Huth }
1982fcf5ef2aSThomas Huth 
1983fcf5ef2aSThomas Huth void helper_vsubuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1984fcf5ef2aSThomas Huth {
1985fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1986fcf5ef2aSThomas Huth     r->u128 = a->u128 - b->u128;
1987fcf5ef2aSThomas Huth #else
1988fcf5ef2aSThomas Huth     ppc_avr_t tmp;
1989fcf5ef2aSThomas Huth     ppc_avr_t one = QW_ONE;
1990fcf5ef2aSThomas Huth 
1991fcf5ef2aSThomas Huth     avr_qw_not(&tmp, *b);
1992fcf5ef2aSThomas Huth     avr_qw_add(&tmp, *a, tmp);
1993fcf5ef2aSThomas Huth     avr_qw_add(r, tmp, one);
1994fcf5ef2aSThomas Huth #endif
1995fcf5ef2aSThomas Huth }
1996fcf5ef2aSThomas Huth 
1997fcf5ef2aSThomas Huth void helper_vsubeuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
1998fcf5ef2aSThomas Huth {
1999fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2000fcf5ef2aSThomas Huth     r->u128 = a->u128 + ~b->u128 + (c->u128 & 1);
2001fcf5ef2aSThomas Huth #else
2002fcf5ef2aSThomas Huth     ppc_avr_t tmp, sum;
2003fcf5ef2aSThomas Huth 
2004fcf5ef2aSThomas Huth     avr_qw_not(&tmp, *b);
2005fcf5ef2aSThomas Huth     avr_qw_add(&sum, *a, tmp);
2006fcf5ef2aSThomas Huth 
20073c385a93SMark Cave-Ayland     tmp.VsrD(0) = 0;
20083c385a93SMark Cave-Ayland     tmp.VsrD(1) = c->VsrD(1) & 1;
2009fcf5ef2aSThomas Huth     avr_qw_add(r, sum, tmp);
2010fcf5ef2aSThomas Huth #endif
2011fcf5ef2aSThomas Huth }
2012fcf5ef2aSThomas Huth 
2013fcf5ef2aSThomas Huth void helper_vsubcuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2014fcf5ef2aSThomas Huth {
2015fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2016fcf5ef2aSThomas Huth     r->u128 = (~a->u128 < ~b->u128) ||
2017fcf5ef2aSThomas Huth                  (a->u128 + ~b->u128 == (__uint128_t)-1);
2018fcf5ef2aSThomas Huth #else
2019fcf5ef2aSThomas Huth     int carry = (avr_qw_cmpu(*a, *b) > 0);
2020fcf5ef2aSThomas Huth     if (!carry) {
2021fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2022fcf5ef2aSThomas Huth         avr_qw_not(&tmp, *b);
2023fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
20243c385a93SMark Cave-Ayland         carry = ((tmp.VsrSD(0) == -1ull) && (tmp.VsrSD(1) == -1ull));
2025fcf5ef2aSThomas Huth     }
20263c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
20273c385a93SMark Cave-Ayland     r->VsrD(1) = carry;
2028fcf5ef2aSThomas Huth #endif
2029fcf5ef2aSThomas Huth }
2030fcf5ef2aSThomas Huth 
2031fcf5ef2aSThomas Huth void helper_vsubecuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2032fcf5ef2aSThomas Huth {
2033fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2034fcf5ef2aSThomas Huth     r->u128 =
2035fcf5ef2aSThomas Huth         (~a->u128 < ~b->u128) ||
2036fcf5ef2aSThomas Huth         ((c->u128 & 1) && (a->u128 + ~b->u128 == (__uint128_t)-1));
2037fcf5ef2aSThomas Huth #else
20383c385a93SMark Cave-Ayland     int carry_in = c->VsrD(1) & 1;
2039fcf5ef2aSThomas Huth     int carry_out = (avr_qw_cmpu(*a, *b) > 0);
2040fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
2041fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2042fcf5ef2aSThomas Huth         avr_qw_not(&tmp, *b);
2043fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
20443c385a93SMark Cave-Ayland         carry_out = ((tmp.VsrD(0) == -1ull) && (tmp.VsrD(1) == -1ull));
2045fcf5ef2aSThomas Huth     }
2046fcf5ef2aSThomas Huth 
20473c385a93SMark Cave-Ayland     r->VsrD(0) = 0;
20483c385a93SMark Cave-Ayland     r->VsrD(1) = carry_out;
2049fcf5ef2aSThomas Huth #endif
2050fcf5ef2aSThomas Huth }
2051fcf5ef2aSThomas Huth 
2052fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_1 0xC
2053fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_2 0xF
2054fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_1  0xA
2055fcf5ef2aSThomas Huth #define BCD_NEG_PREF    0xD
2056fcf5ef2aSThomas Huth #define BCD_NEG_ALT     0xB
2057fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_2  0xE
2058fcf5ef2aSThomas Huth #define NATIONAL_PLUS   0x2B
2059fcf5ef2aSThomas Huth #define NATIONAL_NEG    0x2D
2060fcf5ef2aSThomas Huth 
2061365206aeSJose Ricardo Ziviani #define BCD_DIG_BYTE(n) (15 - ((n) / 2))
2062fcf5ef2aSThomas Huth 
2063fcf5ef2aSThomas Huth static int bcd_get_sgn(ppc_avr_t *bcd)
2064fcf5ef2aSThomas Huth {
2065428115c3SMark Cave-Ayland     switch (bcd->VsrB(BCD_DIG_BYTE(0)) & 0xF) {
2066fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_1:
2067fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_2:
2068fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_1:
2069fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_2:
2070fcf5ef2aSThomas Huth     {
2071fcf5ef2aSThomas Huth         return 1;
2072fcf5ef2aSThomas Huth     }
2073fcf5ef2aSThomas Huth 
2074fcf5ef2aSThomas Huth     case BCD_NEG_PREF:
2075fcf5ef2aSThomas Huth     case BCD_NEG_ALT:
2076fcf5ef2aSThomas Huth     {
2077fcf5ef2aSThomas Huth         return -1;
2078fcf5ef2aSThomas Huth     }
2079fcf5ef2aSThomas Huth 
2080fcf5ef2aSThomas Huth     default:
2081fcf5ef2aSThomas Huth     {
2082fcf5ef2aSThomas Huth         return 0;
2083fcf5ef2aSThomas Huth     }
2084fcf5ef2aSThomas Huth     }
2085fcf5ef2aSThomas Huth }
2086fcf5ef2aSThomas Huth 
2087fcf5ef2aSThomas Huth static int bcd_preferred_sgn(int sgn, int ps)
2088fcf5ef2aSThomas Huth {
2089fcf5ef2aSThomas Huth     if (sgn >= 0) {
2090fcf5ef2aSThomas Huth         return (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2;
2091fcf5ef2aSThomas Huth     } else {
2092fcf5ef2aSThomas Huth         return BCD_NEG_PREF;
2093fcf5ef2aSThomas Huth     }
2094fcf5ef2aSThomas Huth }
2095fcf5ef2aSThomas Huth 
2096fcf5ef2aSThomas Huth static uint8_t bcd_get_digit(ppc_avr_t *bcd, int n, int *invalid)
2097fcf5ef2aSThomas Huth {
2098fcf5ef2aSThomas Huth     uint8_t result;
2099fcf5ef2aSThomas Huth     if (n & 1) {
2100428115c3SMark Cave-Ayland         result = bcd->VsrB(BCD_DIG_BYTE(n)) >> 4;
2101fcf5ef2aSThomas Huth     } else {
2102428115c3SMark Cave-Ayland        result = bcd->VsrB(BCD_DIG_BYTE(n)) & 0xF;
2103fcf5ef2aSThomas Huth     }
2104fcf5ef2aSThomas Huth 
2105fcf5ef2aSThomas Huth     if (unlikely(result > 9)) {
2106fcf5ef2aSThomas Huth         *invalid = true;
2107fcf5ef2aSThomas Huth     }
2108fcf5ef2aSThomas Huth     return result;
2109fcf5ef2aSThomas Huth }
2110fcf5ef2aSThomas Huth 
2111fcf5ef2aSThomas Huth static void bcd_put_digit(ppc_avr_t *bcd, uint8_t digit, int n)
2112fcf5ef2aSThomas Huth {
2113fcf5ef2aSThomas Huth     if (n & 1) {
2114428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) &= 0x0F;
2115428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) |= (digit << 4);
2116fcf5ef2aSThomas Huth     } else {
2117428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) &= 0xF0;
2118428115c3SMark Cave-Ayland         bcd->VsrB(BCD_DIG_BYTE(n)) |= digit;
2119fcf5ef2aSThomas Huth     }
2120fcf5ef2aSThomas Huth }
2121fcf5ef2aSThomas Huth 
2122071663dfSJose Ricardo Ziviani static bool bcd_is_valid(ppc_avr_t *bcd)
2123071663dfSJose Ricardo Ziviani {
2124071663dfSJose Ricardo Ziviani     int i;
2125071663dfSJose Ricardo Ziviani     int invalid = 0;
2126071663dfSJose Ricardo Ziviani 
2127071663dfSJose Ricardo Ziviani     if (bcd_get_sgn(bcd) == 0) {
2128071663dfSJose Ricardo Ziviani         return false;
2129071663dfSJose Ricardo Ziviani     }
2130071663dfSJose Ricardo Ziviani 
2131071663dfSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2132071663dfSJose Ricardo Ziviani         bcd_get_digit(bcd, i, &invalid);
2133071663dfSJose Ricardo Ziviani         if (unlikely(invalid)) {
2134071663dfSJose Ricardo Ziviani             return false;
2135071663dfSJose Ricardo Ziviani         }
2136071663dfSJose Ricardo Ziviani     }
2137071663dfSJose Ricardo Ziviani     return true;
2138071663dfSJose Ricardo Ziviani }
2139071663dfSJose Ricardo Ziviani 
2140fcf5ef2aSThomas Huth static int bcd_cmp_zero(ppc_avr_t *bcd)
2141fcf5ef2aSThomas Huth {
21423c385a93SMark Cave-Ayland     if (bcd->VsrD(0) == 0 && (bcd->VsrD(1) >> 4) == 0) {
2143efa73196SNikunj A Dadhania         return CRF_EQ;
2144fcf5ef2aSThomas Huth     } else {
2145efa73196SNikunj A Dadhania         return (bcd_get_sgn(bcd) == 1) ? CRF_GT : CRF_LT;
2146fcf5ef2aSThomas Huth     }
2147fcf5ef2aSThomas Huth }
2148fcf5ef2aSThomas Huth 
2149fcf5ef2aSThomas Huth static uint16_t get_national_digit(ppc_avr_t *reg, int n)
2150fcf5ef2aSThomas Huth {
215160594feaSMark Cave-Ayland     return reg->VsrH(7 - n);
2152fcf5ef2aSThomas Huth }
2153fcf5ef2aSThomas Huth 
2154fcf5ef2aSThomas Huth static void set_national_digit(ppc_avr_t *reg, uint8_t val, int n)
2155fcf5ef2aSThomas Huth {
215660594feaSMark Cave-Ayland     reg->VsrH(7 - n) = val;
2157fcf5ef2aSThomas Huth }
2158fcf5ef2aSThomas Huth 
2159fcf5ef2aSThomas Huth static int bcd_cmp_mag(ppc_avr_t *a, ppc_avr_t *b)
2160fcf5ef2aSThomas Huth {
2161fcf5ef2aSThomas Huth     int i;
2162fcf5ef2aSThomas Huth     int invalid = 0;
2163fcf5ef2aSThomas Huth     for (i = 31; i > 0; i--) {
2164fcf5ef2aSThomas Huth         uint8_t dig_a = bcd_get_digit(a, i, &invalid);
2165fcf5ef2aSThomas Huth         uint8_t dig_b = bcd_get_digit(b, i, &invalid);
2166fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2167fcf5ef2aSThomas Huth             return 0; /* doesn't matter */
2168fcf5ef2aSThomas Huth         } else if (dig_a > dig_b) {
2169fcf5ef2aSThomas Huth             return 1;
2170fcf5ef2aSThomas Huth         } else if (dig_a < dig_b) {
2171fcf5ef2aSThomas Huth             return -1;
2172fcf5ef2aSThomas Huth         }
2173fcf5ef2aSThomas Huth     }
2174fcf5ef2aSThomas Huth 
2175fcf5ef2aSThomas Huth     return 0;
2176fcf5ef2aSThomas Huth }
2177fcf5ef2aSThomas Huth 
2178d03b174aSYasmin Beatriz static void bcd_add_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2179fcf5ef2aSThomas Huth                        int *overflow)
2180fcf5ef2aSThomas Huth {
2181fcf5ef2aSThomas Huth     int carry = 0;
2182fcf5ef2aSThomas Huth     int i;
2183fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2184fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) +
2185fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2186fcf5ef2aSThomas Huth         if (digit > 9) {
2187fcf5ef2aSThomas Huth             carry = 1;
2188fcf5ef2aSThomas Huth             digit -= 10;
2189fcf5ef2aSThomas Huth         } else {
2190fcf5ef2aSThomas Huth             carry = 0;
2191fcf5ef2aSThomas Huth         }
2192fcf5ef2aSThomas Huth 
2193fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2194fcf5ef2aSThomas Huth     }
2195fcf5ef2aSThomas Huth 
2196fcf5ef2aSThomas Huth     *overflow = carry;
2197fcf5ef2aSThomas Huth }
2198fcf5ef2aSThomas Huth 
2199d03b174aSYasmin Beatriz static void bcd_sub_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2200fcf5ef2aSThomas Huth                        int *overflow)
2201fcf5ef2aSThomas Huth {
2202fcf5ef2aSThomas Huth     int carry = 0;
2203fcf5ef2aSThomas Huth     int i;
2204d03b174aSYasmin Beatriz 
2205fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2206fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) -
2207fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2208fcf5ef2aSThomas Huth         if (digit & 0x80) {
2209fcf5ef2aSThomas Huth             carry = -1;
2210fcf5ef2aSThomas Huth             digit += 10;
2211fcf5ef2aSThomas Huth         } else {
2212fcf5ef2aSThomas Huth             carry = 0;
2213fcf5ef2aSThomas Huth         }
2214fcf5ef2aSThomas Huth 
2215fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2216fcf5ef2aSThomas Huth     }
2217fcf5ef2aSThomas Huth 
2218fcf5ef2aSThomas Huth     *overflow = carry;
2219fcf5ef2aSThomas Huth }
2220fcf5ef2aSThomas Huth 
2221fcf5ef2aSThomas Huth uint32_t helper_bcdadd(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2222fcf5ef2aSThomas Huth {
2223fcf5ef2aSThomas Huth 
2224fcf5ef2aSThomas Huth     int sgna = bcd_get_sgn(a);
2225fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2226fcf5ef2aSThomas Huth     int invalid = (sgna == 0) || (sgnb == 0);
2227fcf5ef2aSThomas Huth     int overflow = 0;
2228fcf5ef2aSThomas Huth     uint32_t cr = 0;
2229fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
2230fcf5ef2aSThomas Huth 
2231fcf5ef2aSThomas Huth     if (!invalid) {
2232fcf5ef2aSThomas Huth         if (sgna == sgnb) {
2233428115c3SMark Cave-Ayland             result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna, ps);
2234d03b174aSYasmin Beatriz             bcd_add_mag(&result, a, b, &invalid, &overflow);
2235d03b174aSYasmin Beatriz             cr = bcd_cmp_zero(&result);
2236fcf5ef2aSThomas Huth         } else {
2237d03b174aSYasmin Beatriz             int magnitude = bcd_cmp_mag(a, b);
2238d03b174aSYasmin Beatriz             if (magnitude > 0) {
2239428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna, ps);
2240d03b174aSYasmin Beatriz                 bcd_sub_mag(&result, a, b, &invalid, &overflow);
2241d03b174aSYasmin Beatriz                 cr = (sgna > 0) ? CRF_GT : CRF_LT;
2242d03b174aSYasmin Beatriz             } else if (magnitude < 0) {
2243428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgnb, ps);
2244d03b174aSYasmin Beatriz                 bcd_sub_mag(&result, b, a, &invalid, &overflow);
2245efa73196SNikunj A Dadhania                 cr = (sgnb > 0) ? CRF_GT : CRF_LT;
2246d03b174aSYasmin Beatriz             } else {
2247428115c3SMark Cave-Ayland                 result.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(0, ps);
2248d03b174aSYasmin Beatriz                 cr = CRF_EQ;
2249d03b174aSYasmin Beatriz             }
2250fcf5ef2aSThomas Huth         }
2251fcf5ef2aSThomas Huth     }
2252fcf5ef2aSThomas Huth 
2253fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
22543c385a93SMark Cave-Ayland         result.VsrD(0) = result.VsrD(1) = -1;
2255efa73196SNikunj A Dadhania         cr = CRF_SO;
2256fcf5ef2aSThomas Huth     } else if (overflow) {
2257efa73196SNikunj A Dadhania         cr |= CRF_SO;
2258fcf5ef2aSThomas Huth     }
2259fcf5ef2aSThomas Huth 
2260fcf5ef2aSThomas Huth     *r = result;
2261fcf5ef2aSThomas Huth 
2262fcf5ef2aSThomas Huth     return cr;
2263fcf5ef2aSThomas Huth }
2264fcf5ef2aSThomas Huth 
2265fcf5ef2aSThomas Huth uint32_t helper_bcdsub(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2266fcf5ef2aSThomas Huth {
2267fcf5ef2aSThomas Huth     ppc_avr_t bcopy = *b;
2268fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2269fcf5ef2aSThomas Huth     if (sgnb < 0) {
2270fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_PLUS_PREF_1, 0);
2271fcf5ef2aSThomas Huth     } else if (sgnb > 0) {
2272fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_NEG_PREF, 0);
2273fcf5ef2aSThomas Huth     }
2274fcf5ef2aSThomas Huth     /* else invalid ... defer to bcdadd code for proper handling */
2275fcf5ef2aSThomas Huth 
2276fcf5ef2aSThomas Huth     return helper_bcdadd(r, a, &bcopy, ps);
2277fcf5ef2aSThomas Huth }
2278fcf5ef2aSThomas Huth 
2279fcf5ef2aSThomas Huth uint32_t helper_bcdcfn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2280fcf5ef2aSThomas Huth {
2281fcf5ef2aSThomas Huth     int i;
2282fcf5ef2aSThomas Huth     int cr = 0;
2283fcf5ef2aSThomas Huth     uint16_t national = 0;
2284fcf5ef2aSThomas Huth     uint16_t sgnb = get_national_digit(b, 0);
2285fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2286fcf5ef2aSThomas Huth     int invalid = (sgnb != NATIONAL_PLUS && sgnb != NATIONAL_NEG);
2287fcf5ef2aSThomas Huth 
2288fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2289fcf5ef2aSThomas Huth         national = get_national_digit(b, i);
2290fcf5ef2aSThomas Huth         if (unlikely(national < 0x30 || national > 0x39)) {
2291fcf5ef2aSThomas Huth             invalid = 1;
2292fcf5ef2aSThomas Huth             break;
2293fcf5ef2aSThomas Huth         }
2294fcf5ef2aSThomas Huth 
2295fcf5ef2aSThomas Huth         bcd_put_digit(&ret, national & 0xf, i);
2296fcf5ef2aSThomas Huth     }
2297fcf5ef2aSThomas Huth 
2298fcf5ef2aSThomas Huth     if (sgnb == NATIONAL_PLUS) {
2299fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2, 0);
2300fcf5ef2aSThomas Huth     } else {
2301fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2302fcf5ef2aSThomas Huth     }
2303fcf5ef2aSThomas Huth 
2304fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2305fcf5ef2aSThomas Huth 
2306fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2307efa73196SNikunj A Dadhania         cr = CRF_SO;
2308fcf5ef2aSThomas Huth     }
2309fcf5ef2aSThomas Huth 
2310fcf5ef2aSThomas Huth     *r = ret;
2311fcf5ef2aSThomas Huth 
2312fcf5ef2aSThomas Huth     return cr;
2313fcf5ef2aSThomas Huth }
2314fcf5ef2aSThomas Huth 
2315fcf5ef2aSThomas Huth uint32_t helper_bcdctn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2316fcf5ef2aSThomas Huth {
2317fcf5ef2aSThomas Huth     int i;
2318fcf5ef2aSThomas Huth     int cr = 0;
2319fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2320fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2321fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2322fcf5ef2aSThomas Huth 
23233c385a93SMark Cave-Ayland     int ox_flag = (b->VsrD(0) != 0) || ((b->VsrD(1) >> 32) != 0);
2324fcf5ef2aSThomas Huth 
2325fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2326fcf5ef2aSThomas Huth         set_national_digit(&ret, 0x30 + bcd_get_digit(b, i, &invalid), i);
2327fcf5ef2aSThomas Huth 
2328fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2329fcf5ef2aSThomas Huth             break;
2330fcf5ef2aSThomas Huth         }
2331fcf5ef2aSThomas Huth     }
2332fcf5ef2aSThomas Huth     set_national_digit(&ret, (sgnb == -1) ? NATIONAL_NEG : NATIONAL_PLUS, 0);
2333fcf5ef2aSThomas Huth 
2334fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2335fcf5ef2aSThomas Huth 
2336fcf5ef2aSThomas Huth     if (ox_flag) {
2337efa73196SNikunj A Dadhania         cr |= CRF_SO;
2338fcf5ef2aSThomas Huth     }
2339fcf5ef2aSThomas Huth 
2340fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2341efa73196SNikunj A Dadhania         cr = CRF_SO;
2342fcf5ef2aSThomas Huth     }
2343fcf5ef2aSThomas Huth 
2344fcf5ef2aSThomas Huth     *r = ret;
2345fcf5ef2aSThomas Huth 
2346fcf5ef2aSThomas Huth     return cr;
2347fcf5ef2aSThomas Huth }
2348fcf5ef2aSThomas Huth 
2349fcf5ef2aSThomas Huth uint32_t helper_bcdcfz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2350fcf5ef2aSThomas Huth {
2351fcf5ef2aSThomas Huth     int i;
2352fcf5ef2aSThomas Huth     int cr = 0;
2353fcf5ef2aSThomas Huth     int invalid = 0;
2354fcf5ef2aSThomas Huth     int zone_digit = 0;
2355fcf5ef2aSThomas Huth     int zone_lead = ps ? 0xF : 0x3;
2356fcf5ef2aSThomas Huth     int digit = 0;
2357fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2358428115c3SMark Cave-Ayland     int sgnb = b->VsrB(BCD_DIG_BYTE(0)) >> 4;
2359fcf5ef2aSThomas Huth 
2360fcf5ef2aSThomas Huth     if (unlikely((sgnb < 0xA) && ps)) {
2361fcf5ef2aSThomas Huth         invalid = 1;
2362fcf5ef2aSThomas Huth     }
2363fcf5ef2aSThomas Huth 
2364fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2365428115c3SMark Cave-Ayland         zone_digit = i ? b->VsrB(BCD_DIG_BYTE(i * 2)) >> 4 : zone_lead;
2366428115c3SMark Cave-Ayland         digit = b->VsrB(BCD_DIG_BYTE(i * 2)) & 0xF;
2367fcf5ef2aSThomas Huth         if (unlikely(zone_digit != zone_lead || digit > 0x9)) {
2368fcf5ef2aSThomas Huth             invalid = 1;
2369fcf5ef2aSThomas Huth             break;
2370fcf5ef2aSThomas Huth         }
2371fcf5ef2aSThomas Huth 
2372fcf5ef2aSThomas Huth         bcd_put_digit(&ret, digit, i + 1);
2373fcf5ef2aSThomas Huth     }
2374fcf5ef2aSThomas Huth 
2375fcf5ef2aSThomas Huth     if ((ps && (sgnb == 0xB || sgnb == 0xD)) ||
2376fcf5ef2aSThomas Huth             (!ps && (sgnb & 0x4))) {
2377fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2378fcf5ef2aSThomas Huth     } else {
2379fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_PLUS_PREF_1, 0);
2380fcf5ef2aSThomas Huth     }
2381fcf5ef2aSThomas Huth 
2382fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2383fcf5ef2aSThomas Huth 
2384fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2385efa73196SNikunj A Dadhania         cr = CRF_SO;
2386fcf5ef2aSThomas Huth     }
2387fcf5ef2aSThomas Huth 
2388fcf5ef2aSThomas Huth     *r = ret;
2389fcf5ef2aSThomas Huth 
2390fcf5ef2aSThomas Huth     return cr;
2391fcf5ef2aSThomas Huth }
2392fcf5ef2aSThomas Huth 
2393fcf5ef2aSThomas Huth uint32_t helper_bcdctz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2394fcf5ef2aSThomas Huth {
2395fcf5ef2aSThomas Huth     int i;
2396fcf5ef2aSThomas Huth     int cr = 0;
2397fcf5ef2aSThomas Huth     uint8_t digit = 0;
2398fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2399fcf5ef2aSThomas Huth     int zone_lead = (ps) ? 0xF0 : 0x30;
2400fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2401fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2402fcf5ef2aSThomas Huth 
24033c385a93SMark Cave-Ayland     int ox_flag = ((b->VsrD(0) >> 4) != 0);
2404fcf5ef2aSThomas Huth 
2405fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2406fcf5ef2aSThomas Huth         digit = bcd_get_digit(b, i + 1, &invalid);
2407fcf5ef2aSThomas Huth 
2408fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2409fcf5ef2aSThomas Huth             break;
2410fcf5ef2aSThomas Huth         }
2411fcf5ef2aSThomas Huth 
2412428115c3SMark Cave-Ayland         ret.VsrB(BCD_DIG_BYTE(i * 2)) = zone_lead + digit;
2413fcf5ef2aSThomas Huth     }
2414fcf5ef2aSThomas Huth 
2415fcf5ef2aSThomas Huth     if (ps) {
2416fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0xC : 0xD, 1);
2417fcf5ef2aSThomas Huth     } else {
2418fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0x3 : 0x7, 1);
2419fcf5ef2aSThomas Huth     }
2420fcf5ef2aSThomas Huth 
2421fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2422fcf5ef2aSThomas Huth 
2423fcf5ef2aSThomas Huth     if (ox_flag) {
2424efa73196SNikunj A Dadhania         cr |= CRF_SO;
2425fcf5ef2aSThomas Huth     }
2426fcf5ef2aSThomas Huth 
2427fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2428efa73196SNikunj A Dadhania         cr = CRF_SO;
2429fcf5ef2aSThomas Huth     }
2430fcf5ef2aSThomas Huth 
2431fcf5ef2aSThomas Huth     *r = ret;
2432fcf5ef2aSThomas Huth 
2433fcf5ef2aSThomas Huth     return cr;
2434fcf5ef2aSThomas Huth }
2435fcf5ef2aSThomas Huth 
2436a406c058SJose Ricardo Ziviani uint32_t helper_bcdcfsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2437a406c058SJose Ricardo Ziviani {
2438a406c058SJose Ricardo Ziviani     int i;
2439a406c058SJose Ricardo Ziviani     int cr = 0;
2440a406c058SJose Ricardo Ziviani     uint64_t lo_value;
2441a406c058SJose Ricardo Ziviani     uint64_t hi_value;
2442a406c058SJose Ricardo Ziviani     ppc_avr_t ret = { .u64 = { 0, 0 } };
2443a406c058SJose Ricardo Ziviani 
24443c385a93SMark Cave-Ayland     if (b->VsrSD(0) < 0) {
24453c385a93SMark Cave-Ayland         lo_value = -b->VsrSD(1);
24463c385a93SMark Cave-Ayland         hi_value = ~b->VsrD(0) + !lo_value;
2447a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, 0xD, 0);
2448a406c058SJose Ricardo Ziviani     } else {
24493c385a93SMark Cave-Ayland         lo_value = b->VsrD(1);
24503c385a93SMark Cave-Ayland         hi_value = b->VsrD(0);
2451a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, bcd_preferred_sgn(0, ps), 0);
2452a406c058SJose Ricardo Ziviani     }
2453a406c058SJose Ricardo Ziviani 
2454a406c058SJose Ricardo Ziviani     if (divu128(&lo_value, &hi_value, 1000000000000000ULL) ||
2455a406c058SJose Ricardo Ziviani             lo_value > 9999999999999999ULL) {
2456a406c058SJose Ricardo Ziviani         cr = CRF_SO;
2457a406c058SJose Ricardo Ziviani     }
2458a406c058SJose Ricardo Ziviani 
2459a406c058SJose Ricardo Ziviani     for (i = 1; i < 16; hi_value /= 10, i++) {
2460a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, hi_value % 10, i);
2461a406c058SJose Ricardo Ziviani     }
2462a406c058SJose Ricardo Ziviani 
2463a406c058SJose Ricardo Ziviani     for (; i < 32; lo_value /= 10, i++) {
2464a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, lo_value % 10, i);
2465a406c058SJose Ricardo Ziviani     }
2466a406c058SJose Ricardo Ziviani 
2467a406c058SJose Ricardo Ziviani     cr |= bcd_cmp_zero(&ret);
2468a406c058SJose Ricardo Ziviani 
2469a406c058SJose Ricardo Ziviani     *r = ret;
2470a406c058SJose Ricardo Ziviani 
2471a406c058SJose Ricardo Ziviani     return cr;
2472a406c058SJose Ricardo Ziviani }
2473a406c058SJose Ricardo Ziviani 
2474c85bc7ddSJose Ricardo Ziviani uint32_t helper_bcdctsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2475c85bc7ddSJose Ricardo Ziviani {
2476c85bc7ddSJose Ricardo Ziviani     uint8_t i;
2477c85bc7ddSJose Ricardo Ziviani     int cr;
2478c85bc7ddSJose Ricardo Ziviani     uint64_t carry;
2479c85bc7ddSJose Ricardo Ziviani     uint64_t unused;
2480c85bc7ddSJose Ricardo Ziviani     uint64_t lo_value;
2481c85bc7ddSJose Ricardo Ziviani     uint64_t hi_value = 0;
2482c85bc7ddSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2483c85bc7ddSJose Ricardo Ziviani     int invalid = (sgnb == 0);
2484c85bc7ddSJose Ricardo Ziviani 
2485c85bc7ddSJose Ricardo Ziviani     lo_value = bcd_get_digit(b, 31, &invalid);
2486c85bc7ddSJose Ricardo Ziviani     for (i = 30; i > 0; i--) {
2487c85bc7ddSJose Ricardo Ziviani         mulu64(&lo_value, &carry, lo_value, 10ULL);
2488c85bc7ddSJose Ricardo Ziviani         mulu64(&hi_value, &unused, hi_value, 10ULL);
2489c85bc7ddSJose Ricardo Ziviani         lo_value += bcd_get_digit(b, i, &invalid);
2490c85bc7ddSJose Ricardo Ziviani         hi_value += carry;
2491c85bc7ddSJose Ricardo Ziviani 
2492c85bc7ddSJose Ricardo Ziviani         if (unlikely(invalid)) {
2493c85bc7ddSJose Ricardo Ziviani             break;
2494c85bc7ddSJose Ricardo Ziviani         }
2495c85bc7ddSJose Ricardo Ziviani     }
2496c85bc7ddSJose Ricardo Ziviani 
2497c85bc7ddSJose Ricardo Ziviani     if (sgnb == -1) {
24983c385a93SMark Cave-Ayland         r->VsrSD(1) = -lo_value;
24993c385a93SMark Cave-Ayland         r->VsrSD(0) = ~hi_value + !r->VsrSD(1);
2500c85bc7ddSJose Ricardo Ziviani     } else {
25013c385a93SMark Cave-Ayland         r->VsrSD(1) = lo_value;
25023c385a93SMark Cave-Ayland         r->VsrSD(0) = hi_value;
2503c85bc7ddSJose Ricardo Ziviani     }
2504c85bc7ddSJose Ricardo Ziviani 
2505c85bc7ddSJose Ricardo Ziviani     cr = bcd_cmp_zero(b);
2506c85bc7ddSJose Ricardo Ziviani 
2507c85bc7ddSJose Ricardo Ziviani     if (unlikely(invalid)) {
2508c85bc7ddSJose Ricardo Ziviani         cr = CRF_SO;
2509c85bc7ddSJose Ricardo Ziviani     }
2510c85bc7ddSJose Ricardo Ziviani 
2511c85bc7ddSJose Ricardo Ziviani     return cr;
2512c85bc7ddSJose Ricardo Ziviani }
2513c85bc7ddSJose Ricardo Ziviani 
2514c3025c3bSJose Ricardo Ziviani uint32_t helper_bcdcpsgn(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2515c3025c3bSJose Ricardo Ziviani {
2516c3025c3bSJose Ricardo Ziviani     int i;
2517c3025c3bSJose Ricardo Ziviani     int invalid = 0;
2518c3025c3bSJose Ricardo Ziviani 
2519c3025c3bSJose Ricardo Ziviani     if (bcd_get_sgn(a) == 0 || bcd_get_sgn(b) == 0) {
2520c3025c3bSJose Ricardo Ziviani         return CRF_SO;
2521c3025c3bSJose Ricardo Ziviani     }
2522c3025c3bSJose Ricardo Ziviani 
2523c3025c3bSJose Ricardo Ziviani     *r = *a;
2524428115c3SMark Cave-Ayland     bcd_put_digit(r, b->VsrB(BCD_DIG_BYTE(0)) & 0xF, 0);
2525c3025c3bSJose Ricardo Ziviani 
2526c3025c3bSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2527c3025c3bSJose Ricardo Ziviani         bcd_get_digit(a, i, &invalid);
2528c3025c3bSJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
2529c3025c3bSJose Ricardo Ziviani         if (unlikely(invalid)) {
2530c3025c3bSJose Ricardo Ziviani             return CRF_SO;
2531c3025c3bSJose Ricardo Ziviani         }
2532c3025c3bSJose Ricardo Ziviani     }
2533c3025c3bSJose Ricardo Ziviani 
2534c3025c3bSJose Ricardo Ziviani     return bcd_cmp_zero(r);
2535c3025c3bSJose Ricardo Ziviani }
2536c3025c3bSJose Ricardo Ziviani 
2537466a3f9cSJose Ricardo Ziviani uint32_t helper_bcdsetsgn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2538466a3f9cSJose Ricardo Ziviani {
2539466a3f9cSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2540466a3f9cSJose Ricardo Ziviani 
2541466a3f9cSJose Ricardo Ziviani     *r = *b;
2542466a3f9cSJose Ricardo Ziviani     bcd_put_digit(r, bcd_preferred_sgn(sgnb, ps), 0);
2543466a3f9cSJose Ricardo Ziviani 
2544071663dfSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2545466a3f9cSJose Ricardo Ziviani         return CRF_SO;
2546466a3f9cSJose Ricardo Ziviani     }
2547466a3f9cSJose Ricardo Ziviani 
2548466a3f9cSJose Ricardo Ziviani     return bcd_cmp_zero(r);
2549466a3f9cSJose Ricardo Ziviani }
2550466a3f9cSJose Ricardo Ziviani 
2551e04797f7SJose Ricardo Ziviani uint32_t helper_bcds(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2552e04797f7SJose Ricardo Ziviani {
2553e04797f7SJose Ricardo Ziviani     int cr;
2554428115c3SMark Cave-Ayland     int i = a->VsrSB(7);
2555e04797f7SJose Ricardo Ziviani     bool ox_flag = false;
2556e04797f7SJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2557e04797f7SJose Ricardo Ziviani     ppc_avr_t ret = *b;
25583c385a93SMark Cave-Ayland     ret.VsrD(1) &= ~0xf;
2559e04797f7SJose Ricardo Ziviani 
2560e04797f7SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2561e04797f7SJose Ricardo Ziviani         return CRF_SO;
2562e04797f7SJose Ricardo Ziviani     }
2563e04797f7SJose Ricardo Ziviani 
2564e04797f7SJose Ricardo Ziviani     if (unlikely(i > 31)) {
2565e04797f7SJose Ricardo Ziviani         i = 31;
2566e04797f7SJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
2567e04797f7SJose Ricardo Ziviani         i = -31;
2568e04797f7SJose Ricardo Ziviani     }
2569e04797f7SJose Ricardo Ziviani 
2570e04797f7SJose Ricardo Ziviani     if (i > 0) {
25713c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2572e04797f7SJose Ricardo Ziviani     } else {
25733c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2574e04797f7SJose Ricardo Ziviani     }
2575e04797f7SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
2576e04797f7SJose Ricardo Ziviani 
2577e04797f7SJose Ricardo Ziviani     *r = ret;
2578e04797f7SJose Ricardo Ziviani 
2579e04797f7SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
2580e04797f7SJose Ricardo Ziviani     if (ox_flag) {
2581e04797f7SJose Ricardo Ziviani         cr |= CRF_SO;
2582e04797f7SJose Ricardo Ziviani     }
2583e04797f7SJose Ricardo Ziviani 
2584e04797f7SJose Ricardo Ziviani     return cr;
2585e04797f7SJose Ricardo Ziviani }
2586e04797f7SJose Ricardo Ziviani 
2587a49a95e9SJose Ricardo Ziviani uint32_t helper_bcdus(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2588a49a95e9SJose Ricardo Ziviani {
2589a49a95e9SJose Ricardo Ziviani     int cr;
2590a49a95e9SJose Ricardo Ziviani     int i;
2591a49a95e9SJose Ricardo Ziviani     int invalid = 0;
2592a49a95e9SJose Ricardo Ziviani     bool ox_flag = false;
2593a49a95e9SJose Ricardo Ziviani     ppc_avr_t ret = *b;
2594a49a95e9SJose Ricardo Ziviani 
2595a49a95e9SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
2596a49a95e9SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
2597a49a95e9SJose Ricardo Ziviani 
2598a49a95e9SJose Ricardo Ziviani         if (unlikely(invalid)) {
2599a49a95e9SJose Ricardo Ziviani             return CRF_SO;
2600a49a95e9SJose Ricardo Ziviani         }
2601a49a95e9SJose Ricardo Ziviani     }
2602a49a95e9SJose Ricardo Ziviani 
2603428115c3SMark Cave-Ayland     i = a->VsrSB(7);
2604a49a95e9SJose Ricardo Ziviani     if (i >= 32) {
2605a49a95e9SJose Ricardo Ziviani         ox_flag = true;
26063c385a93SMark Cave-Ayland         ret.VsrD(1) = ret.VsrD(0) = 0;
2607a49a95e9SJose Ricardo Ziviani     } else if (i <= -32) {
26083c385a93SMark Cave-Ayland         ret.VsrD(1) = ret.VsrD(0) = 0;
2609a49a95e9SJose Ricardo Ziviani     } else if (i > 0) {
26103c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2611a49a95e9SJose Ricardo Ziviani     } else {
26123c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2613a49a95e9SJose Ricardo Ziviani     }
2614a49a95e9SJose Ricardo Ziviani     *r = ret;
2615a49a95e9SJose Ricardo Ziviani 
2616a49a95e9SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
2617a49a95e9SJose Ricardo Ziviani     if (ox_flag) {
2618a49a95e9SJose Ricardo Ziviani         cr |= CRF_SO;
2619a49a95e9SJose Ricardo Ziviani     }
2620a49a95e9SJose Ricardo Ziviani 
2621a49a95e9SJose Ricardo Ziviani     return cr;
2622a49a95e9SJose Ricardo Ziviani }
2623a49a95e9SJose Ricardo Ziviani 
2624a54238adSJose Ricardo Ziviani uint32_t helper_bcdsr(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2625a54238adSJose Ricardo Ziviani {
2626a54238adSJose Ricardo Ziviani     int cr;
2627a54238adSJose Ricardo Ziviani     int unused = 0;
2628a54238adSJose Ricardo Ziviani     int invalid = 0;
2629a54238adSJose Ricardo Ziviani     bool ox_flag = false;
2630a54238adSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2631a54238adSJose Ricardo Ziviani     ppc_avr_t ret = *b;
26323c385a93SMark Cave-Ayland     ret.VsrD(1) &= ~0xf;
2633a54238adSJose Ricardo Ziviani 
2634428115c3SMark Cave-Ayland     int i = a->VsrSB(7);
2635428115c3SMark Cave-Ayland     ppc_avr_t bcd_one;
2636428115c3SMark Cave-Ayland 
2637428115c3SMark Cave-Ayland     bcd_one.VsrD(0) = 0;
2638428115c3SMark Cave-Ayland     bcd_one.VsrD(1) = 0x10;
2639a54238adSJose Ricardo Ziviani 
2640a54238adSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
2641a54238adSJose Ricardo Ziviani         return CRF_SO;
2642a54238adSJose Ricardo Ziviani     }
2643a54238adSJose Ricardo Ziviani 
2644a54238adSJose Ricardo Ziviani     if (unlikely(i > 31)) {
2645a54238adSJose Ricardo Ziviani         i = 31;
2646a54238adSJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
2647a54238adSJose Ricardo Ziviani         i = -31;
2648a54238adSJose Ricardo Ziviani     }
2649a54238adSJose Ricardo Ziviani 
2650a54238adSJose Ricardo Ziviani     if (i > 0) {
26513c385a93SMark Cave-Ayland         ulshift(&ret.VsrD(1), &ret.VsrD(0), i * 4, &ox_flag);
2652a54238adSJose Ricardo Ziviani     } else {
26533c385a93SMark Cave-Ayland         urshift(&ret.VsrD(1), &ret.VsrD(0), -i * 4);
2654a54238adSJose Ricardo Ziviani 
2655a54238adSJose Ricardo Ziviani         if (bcd_get_digit(&ret, 0, &invalid) >= 5) {
2656a54238adSJose Ricardo Ziviani             bcd_add_mag(&ret, &ret, &bcd_one, &invalid, &unused);
2657a54238adSJose Ricardo Ziviani         }
2658a54238adSJose Ricardo Ziviani     }
2659a54238adSJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
2660a54238adSJose Ricardo Ziviani 
2661a54238adSJose Ricardo Ziviani     cr = bcd_cmp_zero(&ret);
2662a54238adSJose Ricardo Ziviani     if (ox_flag) {
2663a54238adSJose Ricardo Ziviani         cr |= CRF_SO;
2664a54238adSJose Ricardo Ziviani     }
2665a54238adSJose Ricardo Ziviani     *r = ret;
2666a54238adSJose Ricardo Ziviani 
2667a54238adSJose Ricardo Ziviani     return cr;
2668a54238adSJose Ricardo Ziviani }
2669a54238adSJose Ricardo Ziviani 
267031bc4d11SJose Ricardo Ziviani uint32_t helper_bcdtrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
267131bc4d11SJose Ricardo Ziviani {
267231bc4d11SJose Ricardo Ziviani     uint64_t mask;
267331bc4d11SJose Ricardo Ziviani     uint32_t ox_flag = 0;
2674428115c3SMark Cave-Ayland     int i = a->VsrSH(3) + 1;
267531bc4d11SJose Ricardo Ziviani     ppc_avr_t ret = *b;
267631bc4d11SJose Ricardo Ziviani 
267731bc4d11SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
267831bc4d11SJose Ricardo Ziviani         return CRF_SO;
267931bc4d11SJose Ricardo Ziviani     }
268031bc4d11SJose Ricardo Ziviani 
268131bc4d11SJose Ricardo Ziviani     if (i > 16 && i < 32) {
268231bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
26833c385a93SMark Cave-Ayland         if (ret.VsrD(0) & ~mask) {
268431bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
268531bc4d11SJose Ricardo Ziviani         }
268631bc4d11SJose Ricardo Ziviani 
26873c385a93SMark Cave-Ayland         ret.VsrD(0) &= mask;
268831bc4d11SJose Ricardo Ziviani     } else if (i >= 0 && i <= 16) {
268931bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
26903c385a93SMark Cave-Ayland         if (ret.VsrD(0) || (ret.VsrD(1) & ~mask)) {
269131bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
269231bc4d11SJose Ricardo Ziviani         }
269331bc4d11SJose Ricardo Ziviani 
26943c385a93SMark Cave-Ayland         ret.VsrD(1) &= mask;
26953c385a93SMark Cave-Ayland         ret.VsrD(0) = 0;
269631bc4d11SJose Ricardo Ziviani     }
269731bc4d11SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(bcd_get_sgn(b), ps), 0);
269831bc4d11SJose Ricardo Ziviani     *r = ret;
269931bc4d11SJose Ricardo Ziviani 
270031bc4d11SJose Ricardo Ziviani     return bcd_cmp_zero(&ret) | ox_flag;
270131bc4d11SJose Ricardo Ziviani }
270231bc4d11SJose Ricardo Ziviani 
27035c32e2e4SJose Ricardo Ziviani uint32_t helper_bcdutrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
27045c32e2e4SJose Ricardo Ziviani {
27055c32e2e4SJose Ricardo Ziviani     int i;
27065c32e2e4SJose Ricardo Ziviani     uint64_t mask;
27075c32e2e4SJose Ricardo Ziviani     uint32_t ox_flag = 0;
27085c32e2e4SJose Ricardo Ziviani     int invalid = 0;
27095c32e2e4SJose Ricardo Ziviani     ppc_avr_t ret = *b;
27105c32e2e4SJose Ricardo Ziviani 
27115c32e2e4SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
27125c32e2e4SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
27135c32e2e4SJose Ricardo Ziviani 
27145c32e2e4SJose Ricardo Ziviani         if (unlikely(invalid)) {
27155c32e2e4SJose Ricardo Ziviani             return CRF_SO;
27165c32e2e4SJose Ricardo Ziviani         }
27175c32e2e4SJose Ricardo Ziviani     }
27185c32e2e4SJose Ricardo Ziviani 
2719428115c3SMark Cave-Ayland     i = a->VsrSH(3);
27205c32e2e4SJose Ricardo Ziviani     if (i > 16 && i < 33) {
27215c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
27223c385a93SMark Cave-Ayland         if (ret.VsrD(0) & ~mask) {
27235c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
27245c32e2e4SJose Ricardo Ziviani         }
27255c32e2e4SJose Ricardo Ziviani 
27263c385a93SMark Cave-Ayland         ret.VsrD(0) &= mask;
27275c32e2e4SJose Ricardo Ziviani     } else if (i > 0 && i <= 16) {
27285c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
27293c385a93SMark Cave-Ayland         if (ret.VsrD(0) || (ret.VsrD(1) & ~mask)) {
27305c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
27315c32e2e4SJose Ricardo Ziviani         }
27325c32e2e4SJose Ricardo Ziviani 
27333c385a93SMark Cave-Ayland         ret.VsrD(1) &= mask;
27343c385a93SMark Cave-Ayland         ret.VsrD(0) = 0;
27355c32e2e4SJose Ricardo Ziviani     } else if (i == 0) {
27363c385a93SMark Cave-Ayland         if (ret.VsrD(0) || ret.VsrD(1)) {
27375c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
27385c32e2e4SJose Ricardo Ziviani         }
27393c385a93SMark Cave-Ayland         ret.VsrD(0) = ret.VsrD(1) = 0;
27405c32e2e4SJose Ricardo Ziviani     }
27415c32e2e4SJose Ricardo Ziviani 
27425c32e2e4SJose Ricardo Ziviani     *r = ret;
27433c385a93SMark Cave-Ayland     if (r->VsrD(0) == 0 && r->VsrD(1) == 0) {
27445c32e2e4SJose Ricardo Ziviani         return ox_flag | CRF_EQ;
27455c32e2e4SJose Ricardo Ziviani     }
27465c32e2e4SJose Ricardo Ziviani 
27475c32e2e4SJose Ricardo Ziviani     return ox_flag | CRF_GT;
27485c32e2e4SJose Ricardo Ziviani }
27495c32e2e4SJose Ricardo Ziviani 
2750fcf5ef2aSThomas Huth void helper_vsbox(ppc_avr_t *r, ppc_avr_t *a)
2751fcf5ef2aSThomas Huth {
2752fcf5ef2aSThomas Huth     int i;
2753fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
2754fcf5ef2aSThomas Huth         r->u8[i] = AES_sbox[a->u8[i]];
2755fcf5ef2aSThomas Huth     }
2756fcf5ef2aSThomas Huth }
2757fcf5ef2aSThomas Huth 
2758fcf5ef2aSThomas Huth void helper_vcipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2759fcf5ef2aSThomas Huth {
2760fcf5ef2aSThomas Huth     ppc_avr_t result;
2761fcf5ef2aSThomas Huth     int i;
2762fcf5ef2aSThomas Huth 
2763fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
27642dea57dbSMark Cave-Ayland         result.VsrW(i) = b->VsrW(i) ^
27652dea57dbSMark Cave-Ayland             (AES_Te0[a->VsrB(AES_shifts[4 * i + 0])] ^
27662dea57dbSMark Cave-Ayland              AES_Te1[a->VsrB(AES_shifts[4 * i + 1])] ^
27672dea57dbSMark Cave-Ayland              AES_Te2[a->VsrB(AES_shifts[4 * i + 2])] ^
27682dea57dbSMark Cave-Ayland              AES_Te3[a->VsrB(AES_shifts[4 * i + 3])]);
2769fcf5ef2aSThomas Huth     }
2770fcf5ef2aSThomas Huth     *r = result;
2771fcf5ef2aSThomas Huth }
2772fcf5ef2aSThomas Huth 
2773fcf5ef2aSThomas Huth void helper_vcipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2774fcf5ef2aSThomas Huth {
2775fcf5ef2aSThomas Huth     ppc_avr_t result;
2776fcf5ef2aSThomas Huth     int i;
2777fcf5ef2aSThomas Huth 
2778fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
27792dea57dbSMark Cave-Ayland         result.VsrB(i) = b->VsrB(i) ^ (AES_sbox[a->VsrB(AES_shifts[i])]);
2780fcf5ef2aSThomas Huth     }
2781fcf5ef2aSThomas Huth     *r = result;
2782fcf5ef2aSThomas Huth }
2783fcf5ef2aSThomas Huth 
2784fcf5ef2aSThomas Huth void helper_vncipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2785fcf5ef2aSThomas Huth {
2786fcf5ef2aSThomas Huth     /* This differs from what is written in ISA V2.07.  The RTL is */
2787fcf5ef2aSThomas Huth     /* incorrect and will be fixed in V2.07B.                      */
2788fcf5ef2aSThomas Huth     int i;
2789fcf5ef2aSThomas Huth     ppc_avr_t tmp;
2790fcf5ef2aSThomas Huth 
2791fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
27922dea57dbSMark Cave-Ayland         tmp.VsrB(i) = b->VsrB(i) ^ AES_isbox[a->VsrB(AES_ishifts[i])];
2793fcf5ef2aSThomas Huth     }
2794fcf5ef2aSThomas Huth 
2795fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
27962dea57dbSMark Cave-Ayland         r->VsrW(i) =
27972dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 0)][0] ^
27982dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 1)][1] ^
27992dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 2)][2] ^
28002dea57dbSMark Cave-Ayland             AES_imc[tmp.VsrB(4 * i + 3)][3];
2801fcf5ef2aSThomas Huth     }
2802fcf5ef2aSThomas Huth }
2803fcf5ef2aSThomas Huth 
2804fcf5ef2aSThomas Huth void helper_vncipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2805fcf5ef2aSThomas Huth {
2806fcf5ef2aSThomas Huth     ppc_avr_t result;
2807fcf5ef2aSThomas Huth     int i;
2808fcf5ef2aSThomas Huth 
2809fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
28102dea57dbSMark Cave-Ayland         result.VsrB(i) = b->VsrB(i) ^ (AES_isbox[a->VsrB(AES_ishifts[i])]);
2811fcf5ef2aSThomas Huth     }
2812fcf5ef2aSThomas Huth     *r = result;
2813fcf5ef2aSThomas Huth }
2814fcf5ef2aSThomas Huth 
2815fcf5ef2aSThomas Huth void helper_vshasigmaw(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
2816fcf5ef2aSThomas Huth {
2817fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
2818fcf5ef2aSThomas Huth     int six = st_six & 0xF;
2819fcf5ef2aSThomas Huth     int i;
2820fcf5ef2aSThomas Huth 
2821730d2ca3SMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2822fcf5ef2aSThomas Huth         if (st == 0) {
2823fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
28240ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 7) ^
28250ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 18) ^
2826730d2ca3SMark Cave-Ayland                              (a->VsrW(i) >> 3);
2827fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
28280ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 17) ^
28290ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 19) ^
2830730d2ca3SMark Cave-Ayland                              (a->VsrW(i) >> 10);
2831fcf5ef2aSThomas Huth             }
2832fcf5ef2aSThomas Huth         } else { /* st == 1 */
2833fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
28340ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 2) ^
28350ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 13) ^
28360ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 22);
2837fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
28380ef83bf2SMark Cave-Ayland                 r->VsrW(i) = ror32(a->VsrW(i), 6) ^
28390ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 11) ^
28400ef83bf2SMark Cave-Ayland                              ror32(a->VsrW(i), 25);
2841fcf5ef2aSThomas Huth             }
2842fcf5ef2aSThomas Huth         }
2843fcf5ef2aSThomas Huth     }
2844fcf5ef2aSThomas Huth }
2845fcf5ef2aSThomas Huth 
2846fcf5ef2aSThomas Huth void helper_vshasigmad(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
2847fcf5ef2aSThomas Huth {
2848fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
2849fcf5ef2aSThomas Huth     int six = st_six & 0xF;
2850fcf5ef2aSThomas Huth     int i;
2851fcf5ef2aSThomas Huth 
2852730d2ca3SMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
2853fcf5ef2aSThomas Huth         if (st == 0) {
2854fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2 * i))) == 0) {
28550ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 1) ^
28560ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 8) ^
2857730d2ca3SMark Cave-Ayland                              (a->VsrD(i) >> 7);
2858fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
28590ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 19) ^
28600ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 61) ^
2861730d2ca3SMark Cave-Ayland                              (a->VsrD(i) >> 6);
2862fcf5ef2aSThomas Huth             }
2863fcf5ef2aSThomas Huth         } else { /* st == 1 */
2864fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2 * i))) == 0) {
28650ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 28) ^
28660ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 34) ^
28670ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 39);
2868fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
28690ef83bf2SMark Cave-Ayland                 r->VsrD(i) = ror64(a->VsrD(i), 14) ^
28700ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 18) ^
28710ef83bf2SMark Cave-Ayland                              ror64(a->VsrD(i), 41);
2872fcf5ef2aSThomas Huth             }
2873fcf5ef2aSThomas Huth         }
2874fcf5ef2aSThomas Huth     }
2875fcf5ef2aSThomas Huth }
2876fcf5ef2aSThomas Huth 
2877fcf5ef2aSThomas Huth void helper_vpermxor(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2878fcf5ef2aSThomas Huth {
2879fcf5ef2aSThomas Huth     ppc_avr_t result;
2880fcf5ef2aSThomas Huth     int i;
2881fcf5ef2aSThomas Huth 
288260594feaSMark Cave-Ayland     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
288360594feaSMark Cave-Ayland         int indexA = c->VsrB(i) >> 4;
288460594feaSMark Cave-Ayland         int indexB = c->VsrB(i) & 0xF;
288560594feaSMark Cave-Ayland 
288660594feaSMark Cave-Ayland         result.VsrB(i) = a->VsrB(indexA) ^ b->VsrB(indexB);
2887fcf5ef2aSThomas Huth     }
2888fcf5ef2aSThomas Huth     *r = result;
2889fcf5ef2aSThomas Huth }
2890fcf5ef2aSThomas Huth 
2891fcf5ef2aSThomas Huth #undef VECTOR_FOR_INORDER_I
2892fcf5ef2aSThomas Huth 
2893fcf5ef2aSThomas Huth /*****************************************************************************/
2894fcf5ef2aSThomas Huth /* SPE extension helpers */
2895fcf5ef2aSThomas Huth /* Use a table to make this quicker */
2896fcf5ef2aSThomas Huth static const uint8_t hbrev[16] = {
2897fcf5ef2aSThomas Huth     0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
2898fcf5ef2aSThomas Huth     0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
2899fcf5ef2aSThomas Huth };
2900fcf5ef2aSThomas Huth 
2901fcf5ef2aSThomas Huth static inline uint8_t byte_reverse(uint8_t val)
2902fcf5ef2aSThomas Huth {
2903fcf5ef2aSThomas Huth     return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
2904fcf5ef2aSThomas Huth }
2905fcf5ef2aSThomas Huth 
2906fcf5ef2aSThomas Huth static inline uint32_t word_reverse(uint32_t val)
2907fcf5ef2aSThomas Huth {
2908fcf5ef2aSThomas Huth     return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
2909fcf5ef2aSThomas Huth         (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
2910fcf5ef2aSThomas Huth }
2911fcf5ef2aSThomas Huth 
2912fcf5ef2aSThomas Huth #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
2913fcf5ef2aSThomas Huth target_ulong helper_brinc(target_ulong arg1, target_ulong arg2)
2914fcf5ef2aSThomas Huth {
2915fcf5ef2aSThomas Huth     uint32_t a, b, d, mask;
2916fcf5ef2aSThomas Huth 
2917fcf5ef2aSThomas Huth     mask = UINT32_MAX >> (32 - MASKBITS);
2918fcf5ef2aSThomas Huth     a = arg1 & mask;
2919fcf5ef2aSThomas Huth     b = arg2 & mask;
2920fcf5ef2aSThomas Huth     d = word_reverse(1 + word_reverse(a | ~b));
2921fcf5ef2aSThomas Huth     return (arg1 & ~mask) | (d & b);
2922fcf5ef2aSThomas Huth }
2923fcf5ef2aSThomas Huth 
2924fcf5ef2aSThomas Huth uint32_t helper_cntlsw32(uint32_t val)
2925fcf5ef2aSThomas Huth {
2926fcf5ef2aSThomas Huth     if (val & 0x80000000) {
2927fcf5ef2aSThomas Huth         return clz32(~val);
2928fcf5ef2aSThomas Huth     } else {
2929fcf5ef2aSThomas Huth         return clz32(val);
2930fcf5ef2aSThomas Huth     }
2931fcf5ef2aSThomas Huth }
2932fcf5ef2aSThomas Huth 
2933fcf5ef2aSThomas Huth uint32_t helper_cntlzw32(uint32_t val)
2934fcf5ef2aSThomas Huth {
2935fcf5ef2aSThomas Huth     return clz32(val);
2936fcf5ef2aSThomas Huth }
2937fcf5ef2aSThomas Huth 
2938fcf5ef2aSThomas Huth /* 440 specific */
2939fcf5ef2aSThomas Huth target_ulong helper_dlmzb(CPUPPCState *env, target_ulong high,
2940fcf5ef2aSThomas Huth                           target_ulong low, uint32_t update_Rc)
2941fcf5ef2aSThomas Huth {
2942fcf5ef2aSThomas Huth     target_ulong mask;
2943fcf5ef2aSThomas Huth     int i;
2944fcf5ef2aSThomas Huth 
2945fcf5ef2aSThomas Huth     i = 1;
2946fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
2947fcf5ef2aSThomas Huth         if ((high & mask) == 0) {
2948fcf5ef2aSThomas Huth             if (update_Rc) {
2949fcf5ef2aSThomas Huth                 env->crf[0] = 0x4;
2950fcf5ef2aSThomas Huth             }
2951fcf5ef2aSThomas Huth             goto done;
2952fcf5ef2aSThomas Huth         }
2953fcf5ef2aSThomas Huth         i++;
2954fcf5ef2aSThomas Huth     }
2955fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
2956fcf5ef2aSThomas Huth         if ((low & mask) == 0) {
2957fcf5ef2aSThomas Huth             if (update_Rc) {
2958fcf5ef2aSThomas Huth                 env->crf[0] = 0x8;
2959fcf5ef2aSThomas Huth             }
2960fcf5ef2aSThomas Huth             goto done;
2961fcf5ef2aSThomas Huth         }
2962fcf5ef2aSThomas Huth         i++;
2963fcf5ef2aSThomas Huth     }
2964fcf5ef2aSThomas Huth     i = 8;
2965fcf5ef2aSThomas Huth     if (update_Rc) {
2966fcf5ef2aSThomas Huth         env->crf[0] = 0x2;
2967fcf5ef2aSThomas Huth     }
2968fcf5ef2aSThomas Huth  done:
2969fcf5ef2aSThomas Huth     env->xer = (env->xer & ~0x7F) | i;
2970fcf5ef2aSThomas Huth     if (update_Rc) {
2971fcf5ef2aSThomas Huth         env->crf[0] |= xer_so;
2972fcf5ef2aSThomas Huth     }
2973fcf5ef2aSThomas Huth     return i;
2974fcf5ef2aSThomas Huth }
2975