xref: /openbmc/qemu/target/ppc/int_helper.c (revision 5c32e2e4)
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
9fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth #include "qemu/osdep.h"
20fcf5ef2aSThomas Huth #include "cpu.h"
21fcf5ef2aSThomas Huth #include "internal.h"
22fcf5ef2aSThomas Huth #include "exec/exec-all.h"
23fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
24fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
25fcf5ef2aSThomas Huth #include "crypto/aes.h"
26fcf5ef2aSThomas Huth 
27fcf5ef2aSThomas Huth #include "helper_regs.h"
28fcf5ef2aSThomas Huth /*****************************************************************************/
29fcf5ef2aSThomas Huth /* Fixed point operations helpers */
30fcf5ef2aSThomas Huth 
31fcf5ef2aSThomas Huth target_ulong helper_divweu(CPUPPCState *env, target_ulong ra, target_ulong rb,
32fcf5ef2aSThomas Huth                            uint32_t oe)
33fcf5ef2aSThomas Huth {
34fcf5ef2aSThomas Huth     uint64_t rt = 0;
35fcf5ef2aSThomas Huth     int overflow = 0;
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth     uint64_t dividend = (uint64_t)ra << 32;
38fcf5ef2aSThomas Huth     uint64_t divisor = (uint32_t)rb;
39fcf5ef2aSThomas Huth 
40fcf5ef2aSThomas Huth     if (unlikely(divisor == 0)) {
41fcf5ef2aSThomas Huth         overflow = 1;
42fcf5ef2aSThomas Huth     } else {
43fcf5ef2aSThomas Huth         rt = dividend / divisor;
44fcf5ef2aSThomas Huth         overflow = rt > UINT32_MAX;
45fcf5ef2aSThomas Huth     }
46fcf5ef2aSThomas Huth 
47fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
48fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
49fcf5ef2aSThomas Huth     }
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     if (oe) {
52fcf5ef2aSThomas Huth         if (unlikely(overflow)) {
53fcf5ef2aSThomas Huth             env->so = env->ov = 1;
54fcf5ef2aSThomas Huth         } else {
55fcf5ef2aSThomas Huth             env->ov = 0;
56fcf5ef2aSThomas Huth         }
57fcf5ef2aSThomas Huth     }
58fcf5ef2aSThomas Huth 
59fcf5ef2aSThomas Huth     return (target_ulong)rt;
60fcf5ef2aSThomas Huth }
61fcf5ef2aSThomas Huth 
62fcf5ef2aSThomas Huth target_ulong helper_divwe(CPUPPCState *env, target_ulong ra, target_ulong rb,
63fcf5ef2aSThomas Huth                           uint32_t oe)
64fcf5ef2aSThomas Huth {
65fcf5ef2aSThomas Huth     int64_t rt = 0;
66fcf5ef2aSThomas Huth     int overflow = 0;
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth     int64_t dividend = (int64_t)ra << 32;
69fcf5ef2aSThomas Huth     int64_t divisor = (int64_t)((int32_t)rb);
70fcf5ef2aSThomas Huth 
71fcf5ef2aSThomas Huth     if (unlikely((divisor == 0) ||
72fcf5ef2aSThomas Huth                  ((divisor == -1ull) && (dividend == INT64_MIN)))) {
73fcf5ef2aSThomas Huth         overflow = 1;
74fcf5ef2aSThomas Huth     } else {
75fcf5ef2aSThomas Huth         rt = dividend / divisor;
76fcf5ef2aSThomas Huth         overflow = rt != (int32_t)rt;
77fcf5ef2aSThomas Huth     }
78fcf5ef2aSThomas Huth 
79fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
80fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
81fcf5ef2aSThomas Huth     }
82fcf5ef2aSThomas Huth 
83fcf5ef2aSThomas Huth     if (oe) {
84fcf5ef2aSThomas Huth         if (unlikely(overflow)) {
85fcf5ef2aSThomas Huth             env->so = env->ov = 1;
86fcf5ef2aSThomas Huth         } else {
87fcf5ef2aSThomas Huth             env->ov = 0;
88fcf5ef2aSThomas Huth         }
89fcf5ef2aSThomas Huth     }
90fcf5ef2aSThomas Huth 
91fcf5ef2aSThomas Huth     return (target_ulong)rt;
92fcf5ef2aSThomas Huth }
93fcf5ef2aSThomas Huth 
94fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
95fcf5ef2aSThomas Huth 
96fcf5ef2aSThomas Huth uint64_t helper_divdeu(CPUPPCState *env, uint64_t ra, uint64_t rb, uint32_t oe)
97fcf5ef2aSThomas Huth {
98fcf5ef2aSThomas Huth     uint64_t rt = 0;
99fcf5ef2aSThomas Huth     int overflow = 0;
100fcf5ef2aSThomas Huth 
101fcf5ef2aSThomas Huth     overflow = divu128(&rt, &ra, rb);
102fcf5ef2aSThomas Huth 
103fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
104fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
105fcf5ef2aSThomas Huth     }
106fcf5ef2aSThomas Huth 
107fcf5ef2aSThomas Huth     if (oe) {
108fcf5ef2aSThomas Huth         if (unlikely(overflow)) {
109fcf5ef2aSThomas Huth             env->so = env->ov = 1;
110fcf5ef2aSThomas Huth         } else {
111fcf5ef2aSThomas Huth             env->ov = 0;
112fcf5ef2aSThomas Huth         }
113fcf5ef2aSThomas Huth     }
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth     return rt;
116fcf5ef2aSThomas Huth }
117fcf5ef2aSThomas Huth 
118fcf5ef2aSThomas Huth uint64_t helper_divde(CPUPPCState *env, uint64_t rau, uint64_t rbu, uint32_t oe)
119fcf5ef2aSThomas Huth {
120fcf5ef2aSThomas Huth     int64_t rt = 0;
121fcf5ef2aSThomas Huth     int64_t ra = (int64_t)rau;
122fcf5ef2aSThomas Huth     int64_t rb = (int64_t)rbu;
123fcf5ef2aSThomas Huth     int overflow = divs128(&rt, &ra, rb);
124fcf5ef2aSThomas Huth 
125fcf5ef2aSThomas Huth     if (unlikely(overflow)) {
126fcf5ef2aSThomas Huth         rt = 0; /* Undefined */
127fcf5ef2aSThomas Huth     }
128fcf5ef2aSThomas Huth 
129fcf5ef2aSThomas Huth     if (oe) {
130fcf5ef2aSThomas Huth 
131fcf5ef2aSThomas Huth         if (unlikely(overflow)) {
132fcf5ef2aSThomas Huth             env->so = env->ov = 1;
133fcf5ef2aSThomas Huth         } else {
134fcf5ef2aSThomas Huth             env->ov = 0;
135fcf5ef2aSThomas Huth         }
136fcf5ef2aSThomas Huth     }
137fcf5ef2aSThomas Huth 
138fcf5ef2aSThomas Huth     return rt;
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth #endif
142fcf5ef2aSThomas Huth 
143fcf5ef2aSThomas Huth 
144fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
145fcf5ef2aSThomas Huth /* if x = 0xab, returns 0xababababababababa */
146fcf5ef2aSThomas Huth #define pattern(x) (((x) & 0xff) * (~(target_ulong)0 / 0xff))
147fcf5ef2aSThomas Huth 
148fcf5ef2aSThomas Huth /* substract 1 from each byte, and with inverse, check if MSB is set at each
149fcf5ef2aSThomas Huth  * byte.
150fcf5ef2aSThomas Huth  * i.e. ((0x00 - 0x01) & ~(0x00)) & 0x80
151fcf5ef2aSThomas Huth  *      (0xFF & 0xFF) & 0x80 = 0x80 (zero found)
152fcf5ef2aSThomas Huth  */
153fcf5ef2aSThomas Huth #define haszero(v) (((v) - pattern(0x01)) & ~(v) & pattern(0x80))
154fcf5ef2aSThomas Huth 
155fcf5ef2aSThomas Huth /* When you XOR the pattern and there is a match, that byte will be zero */
156fcf5ef2aSThomas Huth #define hasvalue(x, n)  (haszero((x) ^ pattern(n)))
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth uint32_t helper_cmpeqb(target_ulong ra, target_ulong rb)
159fcf5ef2aSThomas Huth {
160efa73196SNikunj A Dadhania     return hasvalue(rb, ra) ? CRF_GT : 0;
161fcf5ef2aSThomas Huth }
162fcf5ef2aSThomas Huth 
163fcf5ef2aSThomas Huth #undef pattern
164fcf5ef2aSThomas Huth #undef haszero
165fcf5ef2aSThomas Huth #undef hasvalue
166fcf5ef2aSThomas Huth 
167fcf5ef2aSThomas Huth /* Return invalid random number.
168fcf5ef2aSThomas Huth  *
169fcf5ef2aSThomas Huth  * FIXME: Add rng backend or other mechanism to get cryptographically suitable
170fcf5ef2aSThomas Huth  * random number
171fcf5ef2aSThomas Huth  */
172fcf5ef2aSThomas Huth target_ulong helper_darn32(void)
173fcf5ef2aSThomas Huth {
174fcf5ef2aSThomas Huth     return -1;
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
177fcf5ef2aSThomas Huth target_ulong helper_darn64(void)
178fcf5ef2aSThomas Huth {
179fcf5ef2aSThomas Huth     return -1;
180fcf5ef2aSThomas Huth }
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth #endif
183fcf5ef2aSThomas Huth 
184fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
185fcf5ef2aSThomas Huth 
186fcf5ef2aSThomas Huth uint64_t helper_bpermd(uint64_t rs, uint64_t rb)
187fcf5ef2aSThomas Huth {
188fcf5ef2aSThomas Huth     int i;
189fcf5ef2aSThomas Huth     uint64_t ra = 0;
190fcf5ef2aSThomas Huth 
191fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
192fcf5ef2aSThomas Huth         int index = (rs >> (i*8)) & 0xFF;
193fcf5ef2aSThomas Huth         if (index < 64) {
194fcf5ef2aSThomas Huth             if (rb & (1ull << (63-index))) {
195fcf5ef2aSThomas Huth                 ra |= 1 << i;
196fcf5ef2aSThomas Huth             }
197fcf5ef2aSThomas Huth         }
198fcf5ef2aSThomas Huth     }
199fcf5ef2aSThomas Huth     return ra;
200fcf5ef2aSThomas Huth }
201fcf5ef2aSThomas Huth 
202fcf5ef2aSThomas Huth #endif
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth target_ulong helper_cmpb(target_ulong rs, target_ulong rb)
205fcf5ef2aSThomas Huth {
206fcf5ef2aSThomas Huth     target_ulong mask = 0xff;
207fcf5ef2aSThomas Huth     target_ulong ra = 0;
208fcf5ef2aSThomas Huth     int i;
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth     for (i = 0; i < sizeof(target_ulong); i++) {
211fcf5ef2aSThomas Huth         if ((rs & mask) == (rb & mask)) {
212fcf5ef2aSThomas Huth             ra |= mask;
213fcf5ef2aSThomas Huth         }
214fcf5ef2aSThomas Huth         mask <<= 8;
215fcf5ef2aSThomas Huth     }
216fcf5ef2aSThomas Huth     return ra;
217fcf5ef2aSThomas Huth }
218fcf5ef2aSThomas Huth 
219fcf5ef2aSThomas Huth /* shift right arithmetic helper */
220fcf5ef2aSThomas Huth target_ulong helper_sraw(CPUPPCState *env, target_ulong value,
221fcf5ef2aSThomas Huth                          target_ulong shift)
222fcf5ef2aSThomas Huth {
223fcf5ef2aSThomas Huth     int32_t ret;
224fcf5ef2aSThomas Huth 
225fcf5ef2aSThomas Huth     if (likely(!(shift & 0x20))) {
226fcf5ef2aSThomas Huth         if (likely((uint32_t)shift != 0)) {
227fcf5ef2aSThomas Huth             shift &= 0x1f;
228fcf5ef2aSThomas Huth             ret = (int32_t)value >> shift;
229fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
230fcf5ef2aSThomas Huth                 env->ca = 0;
231fcf5ef2aSThomas Huth             } else {
232fcf5ef2aSThomas Huth                 env->ca = 1;
233fcf5ef2aSThomas Huth             }
234fcf5ef2aSThomas Huth         } else {
235fcf5ef2aSThomas Huth             ret = (int32_t)value;
236fcf5ef2aSThomas Huth             env->ca = 0;
237fcf5ef2aSThomas Huth         }
238fcf5ef2aSThomas Huth     } else {
239fcf5ef2aSThomas Huth         ret = (int32_t)value >> 31;
240fcf5ef2aSThomas Huth         env->ca = (ret != 0);
241fcf5ef2aSThomas Huth     }
242fcf5ef2aSThomas Huth     return (target_long)ret;
243fcf5ef2aSThomas Huth }
244fcf5ef2aSThomas Huth 
245fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
246fcf5ef2aSThomas Huth target_ulong helper_srad(CPUPPCState *env, target_ulong value,
247fcf5ef2aSThomas Huth                          target_ulong shift)
248fcf5ef2aSThomas Huth {
249fcf5ef2aSThomas Huth     int64_t ret;
250fcf5ef2aSThomas Huth 
251fcf5ef2aSThomas Huth     if (likely(!(shift & 0x40))) {
252fcf5ef2aSThomas Huth         if (likely((uint64_t)shift != 0)) {
253fcf5ef2aSThomas Huth             shift &= 0x3f;
254fcf5ef2aSThomas Huth             ret = (int64_t)value >> shift;
255fcf5ef2aSThomas Huth             if (likely(ret >= 0 || (value & ((1ULL << shift) - 1)) == 0)) {
256fcf5ef2aSThomas Huth                 env->ca = 0;
257fcf5ef2aSThomas Huth             } else {
258fcf5ef2aSThomas Huth                 env->ca = 1;
259fcf5ef2aSThomas Huth             }
260fcf5ef2aSThomas Huth         } else {
261fcf5ef2aSThomas Huth             ret = (int64_t)value;
262fcf5ef2aSThomas Huth             env->ca = 0;
263fcf5ef2aSThomas Huth         }
264fcf5ef2aSThomas Huth     } else {
265fcf5ef2aSThomas Huth         ret = (int64_t)value >> 63;
266fcf5ef2aSThomas Huth         env->ca = (ret != 0);
267fcf5ef2aSThomas Huth     }
268fcf5ef2aSThomas Huth     return ret;
269fcf5ef2aSThomas Huth }
270fcf5ef2aSThomas Huth #endif
271fcf5ef2aSThomas Huth 
272fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
273fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
274fcf5ef2aSThomas Huth {
27579770002SRichard Henderson     /* Note that we don't fold past bytes */
276fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
277fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
278fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
279fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
280fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
281fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
282fcf5ef2aSThomas Huth     return val;
283fcf5ef2aSThomas Huth }
284fcf5ef2aSThomas Huth 
285fcf5ef2aSThomas Huth target_ulong helper_popcntw(target_ulong val)
286fcf5ef2aSThomas Huth {
28779770002SRichard Henderson     /* Note that we don't fold past words.  */
288fcf5ef2aSThomas Huth     val = (val & 0x5555555555555555ULL) + ((val >>  1) &
289fcf5ef2aSThomas Huth                                            0x5555555555555555ULL);
290fcf5ef2aSThomas Huth     val = (val & 0x3333333333333333ULL) + ((val >>  2) &
291fcf5ef2aSThomas Huth                                            0x3333333333333333ULL);
292fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) &
293fcf5ef2aSThomas Huth                                            0x0f0f0f0f0f0f0f0fULL);
294fcf5ef2aSThomas Huth     val = (val & 0x00ff00ff00ff00ffULL) + ((val >>  8) &
295fcf5ef2aSThomas Huth                                            0x00ff00ff00ff00ffULL);
296fcf5ef2aSThomas Huth     val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) &
297fcf5ef2aSThomas Huth                                            0x0000ffff0000ffffULL);
298fcf5ef2aSThomas Huth     return val;
299fcf5ef2aSThomas Huth }
300fcf5ef2aSThomas Huth #else
301fcf5ef2aSThomas Huth target_ulong helper_popcntb(target_ulong val)
302fcf5ef2aSThomas Huth {
30379770002SRichard Henderson     /* Note that we don't fold past bytes */
304fcf5ef2aSThomas Huth     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
305fcf5ef2aSThomas Huth     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
306fcf5ef2aSThomas Huth     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
307fcf5ef2aSThomas Huth     return val;
308fcf5ef2aSThomas Huth }
309fcf5ef2aSThomas Huth #endif
310fcf5ef2aSThomas Huth 
311fcf5ef2aSThomas Huth /*****************************************************************************/
312fcf5ef2aSThomas Huth /* PowerPC 601 specific instructions (POWER bridge) */
313fcf5ef2aSThomas Huth target_ulong helper_div(CPUPPCState *env, target_ulong arg1, target_ulong arg2)
314fcf5ef2aSThomas Huth {
315fcf5ef2aSThomas Huth     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
318fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
319fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
320fcf5ef2aSThomas Huth         return INT32_MIN;
321fcf5ef2aSThomas Huth     } else {
322fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = tmp % arg2;
323fcf5ef2aSThomas Huth         return  tmp / (int32_t)arg2;
324fcf5ef2aSThomas Huth     }
325fcf5ef2aSThomas Huth }
326fcf5ef2aSThomas Huth 
327fcf5ef2aSThomas Huth target_ulong helper_divo(CPUPPCState *env, target_ulong arg1,
328fcf5ef2aSThomas Huth                          target_ulong arg2)
329fcf5ef2aSThomas Huth {
330fcf5ef2aSThomas Huth     uint64_t tmp = (uint64_t)arg1 << 32 | env->spr[SPR_MQ];
331fcf5ef2aSThomas Huth 
332fcf5ef2aSThomas Huth     if (((int32_t)tmp == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
333fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
334fcf5ef2aSThomas Huth         env->so = env->ov = 1;
335fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
336fcf5ef2aSThomas Huth         return INT32_MIN;
337fcf5ef2aSThomas Huth     } else {
338fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = tmp % arg2;
339fcf5ef2aSThomas Huth         tmp /= (int32_t)arg2;
340fcf5ef2aSThomas Huth         if ((int32_t)tmp != tmp) {
341fcf5ef2aSThomas Huth             env->so = env->ov = 1;
342fcf5ef2aSThomas Huth         } else {
343fcf5ef2aSThomas Huth             env->ov = 0;
344fcf5ef2aSThomas Huth         }
345fcf5ef2aSThomas Huth         return tmp;
346fcf5ef2aSThomas Huth     }
347fcf5ef2aSThomas Huth }
348fcf5ef2aSThomas Huth 
349fcf5ef2aSThomas Huth target_ulong helper_divs(CPUPPCState *env, target_ulong arg1,
350fcf5ef2aSThomas Huth                          target_ulong arg2)
351fcf5ef2aSThomas Huth {
352fcf5ef2aSThomas Huth     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
353fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
354fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
355fcf5ef2aSThomas Huth         return INT32_MIN;
356fcf5ef2aSThomas Huth     } else {
357fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
358fcf5ef2aSThomas Huth         return (int32_t)arg1 / (int32_t)arg2;
359fcf5ef2aSThomas Huth     }
360fcf5ef2aSThomas Huth }
361fcf5ef2aSThomas Huth 
362fcf5ef2aSThomas Huth target_ulong helper_divso(CPUPPCState *env, target_ulong arg1,
363fcf5ef2aSThomas Huth                           target_ulong arg2)
364fcf5ef2aSThomas Huth {
365fcf5ef2aSThomas Huth     if (((int32_t)arg1 == INT32_MIN && (int32_t)arg2 == (int32_t)-1) ||
366fcf5ef2aSThomas Huth         (int32_t)arg2 == 0) {
367fcf5ef2aSThomas Huth         env->so = env->ov = 1;
368fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = 0;
369fcf5ef2aSThomas Huth         return INT32_MIN;
370fcf5ef2aSThomas Huth     } else {
371fcf5ef2aSThomas Huth         env->ov = 0;
372fcf5ef2aSThomas Huth         env->spr[SPR_MQ] = (int32_t)arg1 % (int32_t)arg2;
373fcf5ef2aSThomas Huth         return (int32_t)arg1 / (int32_t)arg2;
374fcf5ef2aSThomas Huth     }
375fcf5ef2aSThomas Huth }
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth /*****************************************************************************/
378fcf5ef2aSThomas Huth /* 602 specific instructions */
379fcf5ef2aSThomas Huth /* mfrom is the most crazy instruction ever seen, imho ! */
380fcf5ef2aSThomas Huth /* Real implementation uses a ROM table. Do the same */
381fcf5ef2aSThomas Huth /* Extremely decomposed:
382fcf5ef2aSThomas Huth  *                      -arg / 256
383fcf5ef2aSThomas Huth  * return 256 * log10(10           + 1.0) + 0.5
384fcf5ef2aSThomas Huth  */
385fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
386fcf5ef2aSThomas Huth target_ulong helper_602_mfrom(target_ulong arg)
387fcf5ef2aSThomas Huth {
388fcf5ef2aSThomas Huth     if (likely(arg < 602)) {
389fcf5ef2aSThomas Huth #include "mfrom_table.c"
390fcf5ef2aSThomas Huth         return mfrom_ROM_table[arg];
391fcf5ef2aSThomas Huth     } else {
392fcf5ef2aSThomas Huth         return 0;
393fcf5ef2aSThomas Huth     }
394fcf5ef2aSThomas Huth }
395fcf5ef2aSThomas Huth #endif
396fcf5ef2aSThomas Huth 
397fcf5ef2aSThomas Huth /*****************************************************************************/
398fcf5ef2aSThomas Huth /* Altivec extension helpers */
399fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
400fcf5ef2aSThomas Huth #define HI_IDX 0
401fcf5ef2aSThomas Huth #define LO_IDX 1
402fcf5ef2aSThomas Huth #define AVRB(i) u8[i]
403fcf5ef2aSThomas Huth #define AVRW(i) u32[i]
404fcf5ef2aSThomas Huth #else
405fcf5ef2aSThomas Huth #define HI_IDX 1
406fcf5ef2aSThomas Huth #define LO_IDX 0
407fcf5ef2aSThomas Huth #define AVRB(i) u8[15-(i)]
408fcf5ef2aSThomas Huth #define AVRW(i) u32[3-(i)]
409fcf5ef2aSThomas Huth #endif
410fcf5ef2aSThomas Huth 
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 
462fcf5ef2aSThomas Huth void helper_lvsl(ppc_avr_t *r, target_ulong sh)
463fcf5ef2aSThomas Huth {
464fcf5ef2aSThomas Huth     int i, j = (sh & 0xf);
465fcf5ef2aSThomas Huth 
466fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
467fcf5ef2aSThomas Huth         r->u8[i] = j++;
468fcf5ef2aSThomas Huth     }
469fcf5ef2aSThomas Huth }
470fcf5ef2aSThomas Huth 
471fcf5ef2aSThomas Huth void helper_lvsr(ppc_avr_t *r, target_ulong sh)
472fcf5ef2aSThomas Huth {
473fcf5ef2aSThomas Huth     int i, j = 0x10 - (sh & 0xf);
474fcf5ef2aSThomas Huth 
475fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
476fcf5ef2aSThomas Huth         r->u8[i] = j++;
477fcf5ef2aSThomas Huth     }
478fcf5ef2aSThomas Huth }
479fcf5ef2aSThomas Huth 
480fcf5ef2aSThomas Huth void helper_mtvscr(CPUPPCState *env, ppc_avr_t *r)
481fcf5ef2aSThomas Huth {
482fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
483fcf5ef2aSThomas Huth     env->vscr = r->u32[3];
484fcf5ef2aSThomas Huth #else
485fcf5ef2aSThomas Huth     env->vscr = r->u32[0];
486fcf5ef2aSThomas Huth #endif
487fcf5ef2aSThomas Huth     set_flush_to_zero(vscr_nj, &env->vec_status);
488fcf5ef2aSThomas Huth }
489fcf5ef2aSThomas Huth 
490fcf5ef2aSThomas Huth void helper_vaddcuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
491fcf5ef2aSThomas Huth {
492fcf5ef2aSThomas Huth     int i;
493fcf5ef2aSThomas Huth 
494fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
495fcf5ef2aSThomas Huth         r->u32[i] = ~a->u32[i] < b->u32[i];
496fcf5ef2aSThomas Huth     }
497fcf5ef2aSThomas Huth }
498fcf5ef2aSThomas Huth 
499fcf5ef2aSThomas Huth /* vprtybw */
500fcf5ef2aSThomas Huth void helper_vprtybw(ppc_avr_t *r, ppc_avr_t *b)
501fcf5ef2aSThomas Huth {
502fcf5ef2aSThomas Huth     int i;
503fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
504fcf5ef2aSThomas Huth         uint64_t res = b->u32[i] ^ (b->u32[i] >> 16);
505fcf5ef2aSThomas Huth         res ^= res >> 8;
506fcf5ef2aSThomas Huth         r->u32[i] = res & 1;
507fcf5ef2aSThomas Huth     }
508fcf5ef2aSThomas Huth }
509fcf5ef2aSThomas Huth 
510fcf5ef2aSThomas Huth /* vprtybd */
511fcf5ef2aSThomas Huth void helper_vprtybd(ppc_avr_t *r, ppc_avr_t *b)
512fcf5ef2aSThomas Huth {
513fcf5ef2aSThomas Huth     int i;
514fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
515fcf5ef2aSThomas Huth         uint64_t res = b->u64[i] ^ (b->u64[i] >> 32);
516fcf5ef2aSThomas Huth         res ^= res >> 16;
517fcf5ef2aSThomas Huth         res ^= res >> 8;
518fcf5ef2aSThomas Huth         r->u64[i] = res & 1;
519fcf5ef2aSThomas Huth     }
520fcf5ef2aSThomas Huth }
521fcf5ef2aSThomas Huth 
522fcf5ef2aSThomas Huth /* vprtybq */
523fcf5ef2aSThomas Huth void helper_vprtybq(ppc_avr_t *r, ppc_avr_t *b)
524fcf5ef2aSThomas Huth {
525fcf5ef2aSThomas Huth     uint64_t res = b->u64[0] ^ b->u64[1];
526fcf5ef2aSThomas Huth     res ^= res >> 32;
527fcf5ef2aSThomas Huth     res ^= res >> 16;
528fcf5ef2aSThomas Huth     res ^= res >> 8;
529fcf5ef2aSThomas Huth     r->u64[LO_IDX] = res & 1;
530fcf5ef2aSThomas Huth     r->u64[HI_IDX] = 0;
531fcf5ef2aSThomas Huth }
532fcf5ef2aSThomas Huth 
533fcf5ef2aSThomas Huth #define VARITH_DO(name, op, element)                                    \
534fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
535fcf5ef2aSThomas Huth     {                                                                   \
536fcf5ef2aSThomas Huth         int i;                                                          \
537fcf5ef2aSThomas Huth                                                                         \
538fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
539fcf5ef2aSThomas Huth             r->element[i] = a->element[i] op b->element[i];             \
540fcf5ef2aSThomas Huth         }                                                               \
541fcf5ef2aSThomas Huth     }
542fcf5ef2aSThomas Huth #define VARITH(suffix, element)                 \
543fcf5ef2aSThomas Huth     VARITH_DO(add##suffix, +, element)          \
544fcf5ef2aSThomas Huth     VARITH_DO(sub##suffix, -, element)
545fcf5ef2aSThomas Huth VARITH(ubm, u8)
546fcf5ef2aSThomas Huth VARITH(uhm, u16)
547fcf5ef2aSThomas Huth VARITH(uwm, u32)
548fcf5ef2aSThomas Huth VARITH(udm, u64)
549fcf5ef2aSThomas Huth VARITH_DO(muluwm, *, u32)
550fcf5ef2aSThomas Huth #undef VARITH_DO
551fcf5ef2aSThomas Huth #undef VARITH
552fcf5ef2aSThomas Huth 
553fcf5ef2aSThomas Huth #define VARITHFP(suffix, func)                                          \
554fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
555fcf5ef2aSThomas Huth                           ppc_avr_t *b)                                 \
556fcf5ef2aSThomas Huth     {                                                                   \
557fcf5ef2aSThomas Huth         int i;                                                          \
558fcf5ef2aSThomas Huth                                                                         \
559fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->f); i++) {                        \
560fcf5ef2aSThomas Huth             r->f[i] = func(a->f[i], b->f[i], &env->vec_status);         \
561fcf5ef2aSThomas Huth         }                                                               \
562fcf5ef2aSThomas Huth     }
563fcf5ef2aSThomas Huth VARITHFP(addfp, float32_add)
564fcf5ef2aSThomas Huth VARITHFP(subfp, float32_sub)
565fcf5ef2aSThomas Huth VARITHFP(minfp, float32_min)
566fcf5ef2aSThomas Huth VARITHFP(maxfp, float32_max)
567fcf5ef2aSThomas Huth #undef VARITHFP
568fcf5ef2aSThomas Huth 
569fcf5ef2aSThomas Huth #define VARITHFPFMA(suffix, type)                                       \
570fcf5ef2aSThomas Huth     void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
571fcf5ef2aSThomas Huth                            ppc_avr_t *b, ppc_avr_t *c)                  \
572fcf5ef2aSThomas Huth     {                                                                   \
573fcf5ef2aSThomas Huth         int i;                                                          \
574fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->f); i++) {                        \
575fcf5ef2aSThomas Huth             r->f[i] = float32_muladd(a->f[i], c->f[i], b->f[i],         \
576fcf5ef2aSThomas Huth                                      type, &env->vec_status);           \
577fcf5ef2aSThomas Huth         }                                                               \
578fcf5ef2aSThomas Huth     }
579fcf5ef2aSThomas Huth VARITHFPFMA(maddfp, 0);
580fcf5ef2aSThomas Huth VARITHFPFMA(nmsubfp, float_muladd_negate_result | float_muladd_negate_c);
581fcf5ef2aSThomas Huth #undef VARITHFPFMA
582fcf5ef2aSThomas Huth 
583fcf5ef2aSThomas Huth #define VARITHSAT_CASE(type, op, cvt, element)                          \
584fcf5ef2aSThomas Huth     {                                                                   \
585fcf5ef2aSThomas Huth         type result = (type)a->element[i] op (type)b->element[i];       \
586fcf5ef2aSThomas Huth         r->element[i] = cvt(result, &sat);                              \
587fcf5ef2aSThomas Huth     }
588fcf5ef2aSThomas Huth 
589fcf5ef2aSThomas Huth #define VARITHSAT_DO(name, op, optype, cvt, element)                    \
590fcf5ef2aSThomas Huth     void helper_v##name(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,   \
591fcf5ef2aSThomas Huth                         ppc_avr_t *b)                                   \
592fcf5ef2aSThomas Huth     {                                                                   \
593fcf5ef2aSThomas Huth         int sat = 0;                                                    \
594fcf5ef2aSThomas Huth         int i;                                                          \
595fcf5ef2aSThomas Huth                                                                         \
596fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
597fcf5ef2aSThomas Huth             switch (sizeof(r->element[0])) {                            \
598fcf5ef2aSThomas Huth             case 1:                                                     \
599fcf5ef2aSThomas Huth                 VARITHSAT_CASE(optype, op, cvt, element);               \
600fcf5ef2aSThomas Huth                 break;                                                  \
601fcf5ef2aSThomas Huth             case 2:                                                     \
602fcf5ef2aSThomas Huth                 VARITHSAT_CASE(optype, op, cvt, element);               \
603fcf5ef2aSThomas Huth                 break;                                                  \
604fcf5ef2aSThomas Huth             case 4:                                                     \
605fcf5ef2aSThomas Huth                 VARITHSAT_CASE(optype, op, cvt, element);               \
606fcf5ef2aSThomas Huth                 break;                                                  \
607fcf5ef2aSThomas Huth             }                                                           \
608fcf5ef2aSThomas Huth         }                                                               \
609fcf5ef2aSThomas Huth         if (sat) {                                                      \
610fcf5ef2aSThomas Huth             env->vscr |= (1 << VSCR_SAT);                               \
611fcf5ef2aSThomas Huth         }                                                               \
612fcf5ef2aSThomas Huth     }
613fcf5ef2aSThomas Huth #define VARITHSAT_SIGNED(suffix, element, optype, cvt)          \
614fcf5ef2aSThomas Huth     VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element)      \
615fcf5ef2aSThomas Huth     VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
616fcf5ef2aSThomas Huth #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt)        \
617fcf5ef2aSThomas Huth     VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element)      \
618fcf5ef2aSThomas Huth     VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
619fcf5ef2aSThomas Huth VARITHSAT_SIGNED(b, s8, int16_t, cvtshsb)
620fcf5ef2aSThomas Huth VARITHSAT_SIGNED(h, s16, int32_t, cvtswsh)
621fcf5ef2aSThomas Huth VARITHSAT_SIGNED(w, s32, int64_t, cvtsdsw)
622fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(b, u8, uint16_t, cvtshub)
623fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(h, u16, uint32_t, cvtswuh)
624fcf5ef2aSThomas Huth VARITHSAT_UNSIGNED(w, u32, uint64_t, cvtsduw)
625fcf5ef2aSThomas Huth #undef VARITHSAT_CASE
626fcf5ef2aSThomas Huth #undef VARITHSAT_DO
627fcf5ef2aSThomas Huth #undef VARITHSAT_SIGNED
628fcf5ef2aSThomas Huth #undef VARITHSAT_UNSIGNED
629fcf5ef2aSThomas Huth 
630fcf5ef2aSThomas Huth #define VAVG_DO(name, element, etype)                                   \
631fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
632fcf5ef2aSThomas Huth     {                                                                   \
633fcf5ef2aSThomas Huth         int i;                                                          \
634fcf5ef2aSThomas Huth                                                                         \
635fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
636fcf5ef2aSThomas Huth             etype x = (etype)a->element[i] + (etype)b->element[i] + 1;  \
637fcf5ef2aSThomas Huth             r->element[i] = x >> 1;                                     \
638fcf5ef2aSThomas Huth         }                                                               \
639fcf5ef2aSThomas Huth     }
640fcf5ef2aSThomas Huth 
641fcf5ef2aSThomas Huth #define VAVG(type, signed_element, signed_type, unsigned_element,       \
642fcf5ef2aSThomas Huth              unsigned_type)                                             \
643fcf5ef2aSThomas Huth     VAVG_DO(avgs##type, signed_element, signed_type)                    \
644fcf5ef2aSThomas Huth     VAVG_DO(avgu##type, unsigned_element, unsigned_type)
645fcf5ef2aSThomas Huth VAVG(b, s8, int16_t, u8, uint16_t)
646fcf5ef2aSThomas Huth VAVG(h, s16, int32_t, u16, uint32_t)
647fcf5ef2aSThomas Huth VAVG(w, s32, int64_t, u32, uint64_t)
648fcf5ef2aSThomas Huth #undef VAVG_DO
649fcf5ef2aSThomas Huth #undef VAVG
650fcf5ef2aSThomas Huth 
651fcf5ef2aSThomas Huth #define VABSDU_DO(name, element)                                        \
652fcf5ef2aSThomas Huth void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)           \
653fcf5ef2aSThomas Huth {                                                                       \
654fcf5ef2aSThomas Huth     int i;                                                              \
655fcf5ef2aSThomas Huth                                                                         \
656fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
657fcf5ef2aSThomas Huth         r->element[i] = (a->element[i] > b->element[i]) ?               \
658fcf5ef2aSThomas Huth             (a->element[i] - b->element[i]) :                           \
659fcf5ef2aSThomas Huth             (b->element[i] - a->element[i]);                            \
660fcf5ef2aSThomas Huth     }                                                                   \
661fcf5ef2aSThomas Huth }
662fcf5ef2aSThomas Huth 
663fcf5ef2aSThomas Huth /* VABSDU - Vector absolute difference unsigned
664fcf5ef2aSThomas Huth  *   name    - instruction mnemonic suffix (b: byte, h: halfword, w: word)
665fcf5ef2aSThomas Huth  *   element - element type to access from vector
666fcf5ef2aSThomas Huth  */
667fcf5ef2aSThomas Huth #define VABSDU(type, element)                   \
668fcf5ef2aSThomas Huth     VABSDU_DO(absdu##type, element)
669fcf5ef2aSThomas Huth VABSDU(b, u8)
670fcf5ef2aSThomas Huth VABSDU(h, u16)
671fcf5ef2aSThomas Huth VABSDU(w, u32)
672fcf5ef2aSThomas Huth #undef VABSDU_DO
673fcf5ef2aSThomas Huth #undef VABSDU
674fcf5ef2aSThomas Huth 
675fcf5ef2aSThomas Huth #define VCF(suffix, cvt, element)                                       \
676fcf5ef2aSThomas Huth     void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r,             \
677fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
678fcf5ef2aSThomas Huth     {                                                                   \
679fcf5ef2aSThomas Huth         int i;                                                          \
680fcf5ef2aSThomas Huth                                                                         \
681fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->f); i++) {                        \
682fcf5ef2aSThomas Huth             float32 t = cvt(b->element[i], &env->vec_status);           \
683fcf5ef2aSThomas Huth             r->f[i] = float32_scalbn(t, -uim, &env->vec_status);        \
684fcf5ef2aSThomas Huth         }                                                               \
685fcf5ef2aSThomas Huth     }
686fcf5ef2aSThomas Huth VCF(ux, uint32_to_float32, u32)
687fcf5ef2aSThomas Huth VCF(sx, int32_to_float32, s32)
688fcf5ef2aSThomas Huth #undef VCF
689fcf5ef2aSThomas Huth 
690fcf5ef2aSThomas Huth #define VCMP_DO(suffix, compare, element, record)                       \
691fcf5ef2aSThomas Huth     void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r,            \
692fcf5ef2aSThomas Huth                              ppc_avr_t *a, ppc_avr_t *b)                \
693fcf5ef2aSThomas Huth     {                                                                   \
694fcf5ef2aSThomas Huth         uint64_t ones = (uint64_t)-1;                                   \
695fcf5ef2aSThomas Huth         uint64_t all = ones;                                            \
696fcf5ef2aSThomas Huth         uint64_t none = 0;                                              \
697fcf5ef2aSThomas Huth         int i;                                                          \
698fcf5ef2aSThomas Huth                                                                         \
699fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
700fcf5ef2aSThomas Huth             uint64_t result = (a->element[i] compare b->element[i] ?    \
701fcf5ef2aSThomas Huth                                ones : 0x0);                             \
702fcf5ef2aSThomas Huth             switch (sizeof(a->element[0])) {                            \
703fcf5ef2aSThomas Huth             case 8:                                                     \
704fcf5ef2aSThomas Huth                 r->u64[i] = result;                                     \
705fcf5ef2aSThomas Huth                 break;                                                  \
706fcf5ef2aSThomas Huth             case 4:                                                     \
707fcf5ef2aSThomas Huth                 r->u32[i] = result;                                     \
708fcf5ef2aSThomas Huth                 break;                                                  \
709fcf5ef2aSThomas Huth             case 2:                                                     \
710fcf5ef2aSThomas Huth                 r->u16[i] = result;                                     \
711fcf5ef2aSThomas Huth                 break;                                                  \
712fcf5ef2aSThomas Huth             case 1:                                                     \
713fcf5ef2aSThomas Huth                 r->u8[i] = result;                                      \
714fcf5ef2aSThomas Huth                 break;                                                  \
715fcf5ef2aSThomas Huth             }                                                           \
716fcf5ef2aSThomas Huth             all &= result;                                              \
717fcf5ef2aSThomas Huth             none |= result;                                             \
718fcf5ef2aSThomas Huth         }                                                               \
719fcf5ef2aSThomas Huth         if (record) {                                                   \
720fcf5ef2aSThomas Huth             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
721fcf5ef2aSThomas Huth         }                                                               \
722fcf5ef2aSThomas Huth     }
723fcf5ef2aSThomas Huth #define VCMP(suffix, compare, element)          \
724fcf5ef2aSThomas Huth     VCMP_DO(suffix, compare, element, 0)        \
725fcf5ef2aSThomas Huth     VCMP_DO(suffix##_dot, compare, element, 1)
726fcf5ef2aSThomas Huth VCMP(equb, ==, u8)
727fcf5ef2aSThomas Huth VCMP(equh, ==, u16)
728fcf5ef2aSThomas Huth VCMP(equw, ==, u32)
729fcf5ef2aSThomas Huth VCMP(equd, ==, u64)
730fcf5ef2aSThomas Huth VCMP(gtub, >, u8)
731fcf5ef2aSThomas Huth VCMP(gtuh, >, u16)
732fcf5ef2aSThomas Huth VCMP(gtuw, >, u32)
733fcf5ef2aSThomas Huth VCMP(gtud, >, u64)
734fcf5ef2aSThomas Huth VCMP(gtsb, >, s8)
735fcf5ef2aSThomas Huth VCMP(gtsh, >, s16)
736fcf5ef2aSThomas Huth VCMP(gtsw, >, s32)
737fcf5ef2aSThomas Huth VCMP(gtsd, >, s64)
738fcf5ef2aSThomas Huth #undef VCMP_DO
739fcf5ef2aSThomas Huth #undef VCMP
740fcf5ef2aSThomas Huth 
741fcf5ef2aSThomas Huth #define VCMPNE_DO(suffix, element, etype, cmpzero, record)              \
742fcf5ef2aSThomas Huth void helper_vcmpne##suffix(CPUPPCState *env, ppc_avr_t *r,              \
743fcf5ef2aSThomas Huth                             ppc_avr_t *a, ppc_avr_t *b)                 \
744fcf5ef2aSThomas Huth {                                                                       \
745fcf5ef2aSThomas Huth     etype ones = (etype)-1;                                             \
746fcf5ef2aSThomas Huth     etype all = ones;                                                   \
747fcf5ef2aSThomas Huth     etype result, none = 0;                                             \
748fcf5ef2aSThomas Huth     int i;                                                              \
749fcf5ef2aSThomas Huth                                                                         \
750fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                      \
751fcf5ef2aSThomas Huth         if (cmpzero) {                                                  \
752fcf5ef2aSThomas Huth             result = ((a->element[i] == 0)                              \
753fcf5ef2aSThomas Huth                            || (b->element[i] == 0)                      \
754fcf5ef2aSThomas Huth                            || (a->element[i] != b->element[i]) ?        \
755fcf5ef2aSThomas Huth                            ones : 0x0);                                 \
756fcf5ef2aSThomas Huth         } else {                                                        \
757fcf5ef2aSThomas Huth             result = (a->element[i] != b->element[i]) ? ones : 0x0;     \
758fcf5ef2aSThomas Huth         }                                                               \
759fcf5ef2aSThomas Huth         r->element[i] = result;                                         \
760fcf5ef2aSThomas Huth         all &= result;                                                  \
761fcf5ef2aSThomas Huth         none |= result;                                                 \
762fcf5ef2aSThomas Huth     }                                                                   \
763fcf5ef2aSThomas Huth     if (record) {                                                       \
764fcf5ef2aSThomas Huth         env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);           \
765fcf5ef2aSThomas Huth     }                                                                   \
766fcf5ef2aSThomas Huth }
767fcf5ef2aSThomas Huth 
768fcf5ef2aSThomas Huth /* VCMPNEZ - Vector compare not equal to zero
769fcf5ef2aSThomas Huth  *   suffix  - instruction mnemonic suffix (b: byte, h: halfword, w: word)
770fcf5ef2aSThomas Huth  *   element - element type to access from vector
771fcf5ef2aSThomas Huth  */
772fcf5ef2aSThomas Huth #define VCMPNE(suffix, element, etype, cmpzero)         \
773fcf5ef2aSThomas Huth     VCMPNE_DO(suffix, element, etype, cmpzero, 0)       \
774fcf5ef2aSThomas Huth     VCMPNE_DO(suffix##_dot, element, etype, cmpzero, 1)
775fcf5ef2aSThomas Huth VCMPNE(zb, u8, uint8_t, 1)
776fcf5ef2aSThomas Huth VCMPNE(zh, u16, uint16_t, 1)
777fcf5ef2aSThomas Huth VCMPNE(zw, u32, uint32_t, 1)
778fcf5ef2aSThomas Huth VCMPNE(b, u8, uint8_t, 0)
779fcf5ef2aSThomas Huth VCMPNE(h, u16, uint16_t, 0)
780fcf5ef2aSThomas Huth VCMPNE(w, u32, uint32_t, 0)
781fcf5ef2aSThomas Huth #undef VCMPNE_DO
782fcf5ef2aSThomas Huth #undef VCMPNE
783fcf5ef2aSThomas Huth 
784fcf5ef2aSThomas Huth #define VCMPFP_DO(suffix, compare, order, record)                       \
785fcf5ef2aSThomas Huth     void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r,            \
786fcf5ef2aSThomas Huth                              ppc_avr_t *a, ppc_avr_t *b)                \
787fcf5ef2aSThomas Huth     {                                                                   \
788fcf5ef2aSThomas Huth         uint32_t ones = (uint32_t)-1;                                   \
789fcf5ef2aSThomas Huth         uint32_t all = ones;                                            \
790fcf5ef2aSThomas Huth         uint32_t none = 0;                                              \
791fcf5ef2aSThomas Huth         int i;                                                          \
792fcf5ef2aSThomas Huth                                                                         \
793fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->f); i++) {                        \
794fcf5ef2aSThomas Huth             uint32_t result;                                            \
795fcf5ef2aSThomas Huth             int rel = float32_compare_quiet(a->f[i], b->f[i],           \
796fcf5ef2aSThomas Huth                                             &env->vec_status);          \
797fcf5ef2aSThomas Huth             if (rel == float_relation_unordered) {                      \
798fcf5ef2aSThomas Huth                 result = 0;                                             \
799fcf5ef2aSThomas Huth             } else if (rel compare order) {                             \
800fcf5ef2aSThomas Huth                 result = ones;                                          \
801fcf5ef2aSThomas Huth             } else {                                                    \
802fcf5ef2aSThomas Huth                 result = 0;                                             \
803fcf5ef2aSThomas Huth             }                                                           \
804fcf5ef2aSThomas Huth             r->u32[i] = result;                                         \
805fcf5ef2aSThomas Huth             all &= result;                                              \
806fcf5ef2aSThomas Huth             none |= result;                                             \
807fcf5ef2aSThomas Huth         }                                                               \
808fcf5ef2aSThomas Huth         if (record) {                                                   \
809fcf5ef2aSThomas Huth             env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1);       \
810fcf5ef2aSThomas Huth         }                                                               \
811fcf5ef2aSThomas Huth     }
812fcf5ef2aSThomas Huth #define VCMPFP(suffix, compare, order)          \
813fcf5ef2aSThomas Huth     VCMPFP_DO(suffix, compare, order, 0)        \
814fcf5ef2aSThomas Huth     VCMPFP_DO(suffix##_dot, compare, order, 1)
815fcf5ef2aSThomas Huth VCMPFP(eqfp, ==, float_relation_equal)
816fcf5ef2aSThomas Huth VCMPFP(gefp, !=, float_relation_less)
817fcf5ef2aSThomas Huth VCMPFP(gtfp, ==, float_relation_greater)
818fcf5ef2aSThomas Huth #undef VCMPFP_DO
819fcf5ef2aSThomas Huth #undef VCMPFP
820fcf5ef2aSThomas Huth 
821fcf5ef2aSThomas Huth static inline void vcmpbfp_internal(CPUPPCState *env, ppc_avr_t *r,
822fcf5ef2aSThomas Huth                                     ppc_avr_t *a, ppc_avr_t *b, int record)
823fcf5ef2aSThomas Huth {
824fcf5ef2aSThomas Huth     int i;
825fcf5ef2aSThomas Huth     int all_in = 0;
826fcf5ef2aSThomas Huth 
827fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->f); i++) {
828fcf5ef2aSThomas Huth         int le_rel = float32_compare_quiet(a->f[i], b->f[i], &env->vec_status);
829fcf5ef2aSThomas Huth         if (le_rel == float_relation_unordered) {
830fcf5ef2aSThomas Huth             r->u32[i] = 0xc0000000;
831fcf5ef2aSThomas Huth             all_in = 1;
832fcf5ef2aSThomas Huth         } else {
833fcf5ef2aSThomas Huth             float32 bneg = float32_chs(b->f[i]);
834fcf5ef2aSThomas Huth             int ge_rel = float32_compare_quiet(a->f[i], bneg, &env->vec_status);
835fcf5ef2aSThomas Huth             int le = le_rel != float_relation_greater;
836fcf5ef2aSThomas Huth             int ge = ge_rel != float_relation_less;
837fcf5ef2aSThomas Huth 
838fcf5ef2aSThomas Huth             r->u32[i] = ((!le) << 31) | ((!ge) << 30);
839fcf5ef2aSThomas Huth             all_in |= (!le | !ge);
840fcf5ef2aSThomas Huth         }
841fcf5ef2aSThomas Huth     }
842fcf5ef2aSThomas Huth     if (record) {
843fcf5ef2aSThomas Huth         env->crf[6] = (all_in == 0) << 1;
844fcf5ef2aSThomas Huth     }
845fcf5ef2aSThomas Huth }
846fcf5ef2aSThomas Huth 
847fcf5ef2aSThomas Huth void helper_vcmpbfp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
848fcf5ef2aSThomas Huth {
849fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 0);
850fcf5ef2aSThomas Huth }
851fcf5ef2aSThomas Huth 
852fcf5ef2aSThomas Huth void helper_vcmpbfp_dot(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
853fcf5ef2aSThomas Huth                         ppc_avr_t *b)
854fcf5ef2aSThomas Huth {
855fcf5ef2aSThomas Huth     vcmpbfp_internal(env, r, a, b, 1);
856fcf5ef2aSThomas Huth }
857fcf5ef2aSThomas Huth 
858fcf5ef2aSThomas Huth #define VCT(suffix, satcvt, element)                                    \
859fcf5ef2aSThomas Huth     void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r,             \
860fcf5ef2aSThomas Huth                             ppc_avr_t *b, uint32_t uim)                 \
861fcf5ef2aSThomas Huth     {                                                                   \
862fcf5ef2aSThomas Huth         int i;                                                          \
863fcf5ef2aSThomas Huth         int sat = 0;                                                    \
864fcf5ef2aSThomas Huth         float_status s = env->vec_status;                               \
865fcf5ef2aSThomas Huth                                                                         \
866fcf5ef2aSThomas Huth         set_float_rounding_mode(float_round_to_zero, &s);               \
867fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->f); i++) {                        \
868fcf5ef2aSThomas Huth             if (float32_is_any_nan(b->f[i])) {                          \
869fcf5ef2aSThomas Huth                 r->element[i] = 0;                                      \
870fcf5ef2aSThomas Huth             } else {                                                    \
871fcf5ef2aSThomas Huth                 float64 t = float32_to_float64(b->f[i], &s);            \
872fcf5ef2aSThomas Huth                 int64_t j;                                              \
873fcf5ef2aSThomas Huth                                                                         \
874fcf5ef2aSThomas Huth                 t = float64_scalbn(t, uim, &s);                         \
875fcf5ef2aSThomas Huth                 j = float64_to_int64(t, &s);                            \
876fcf5ef2aSThomas Huth                 r->element[i] = satcvt(j, &sat);                        \
877fcf5ef2aSThomas Huth             }                                                           \
878fcf5ef2aSThomas Huth         }                                                               \
879fcf5ef2aSThomas Huth         if (sat) {                                                      \
880fcf5ef2aSThomas Huth             env->vscr |= (1 << VSCR_SAT);                               \
881fcf5ef2aSThomas Huth         }                                                               \
882fcf5ef2aSThomas Huth     }
883fcf5ef2aSThomas Huth VCT(uxs, cvtsduw, u32)
884fcf5ef2aSThomas Huth VCT(sxs, cvtsdsw, s32)
885fcf5ef2aSThomas Huth #undef VCT
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth target_ulong helper_vclzlsbb(ppc_avr_t *r)
888fcf5ef2aSThomas Huth {
889fcf5ef2aSThomas Huth     target_ulong count = 0;
890fcf5ef2aSThomas Huth     int i;
891fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
892fcf5ef2aSThomas Huth         if (r->u8[i] & 0x01) {
893fcf5ef2aSThomas Huth             break;
894fcf5ef2aSThomas Huth         }
895fcf5ef2aSThomas Huth         count++;
896fcf5ef2aSThomas Huth     }
897fcf5ef2aSThomas Huth     return count;
898fcf5ef2aSThomas Huth }
899fcf5ef2aSThomas Huth 
900fcf5ef2aSThomas Huth target_ulong helper_vctzlsbb(ppc_avr_t *r)
901fcf5ef2aSThomas Huth {
902fcf5ef2aSThomas Huth     target_ulong count = 0;
903fcf5ef2aSThomas Huth     int i;
904fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
905fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
906fcf5ef2aSThomas Huth #else
907fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
908fcf5ef2aSThomas Huth #endif
909fcf5ef2aSThomas Huth         if (r->u8[i] & 0x01) {
910fcf5ef2aSThomas Huth             break;
911fcf5ef2aSThomas Huth         }
912fcf5ef2aSThomas Huth         count++;
913fcf5ef2aSThomas Huth     }
914fcf5ef2aSThomas Huth     return count;
915fcf5ef2aSThomas Huth }
916fcf5ef2aSThomas Huth 
917fcf5ef2aSThomas Huth void helper_vmhaddshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
918fcf5ef2aSThomas Huth                       ppc_avr_t *b, ppc_avr_t *c)
919fcf5ef2aSThomas Huth {
920fcf5ef2aSThomas Huth     int sat = 0;
921fcf5ef2aSThomas Huth     int i;
922fcf5ef2aSThomas Huth 
923fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
924fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
925fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
926fcf5ef2aSThomas Huth 
927fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
928fcf5ef2aSThomas Huth     }
929fcf5ef2aSThomas Huth 
930fcf5ef2aSThomas Huth     if (sat) {
931fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
932fcf5ef2aSThomas Huth     }
933fcf5ef2aSThomas Huth }
934fcf5ef2aSThomas Huth 
935fcf5ef2aSThomas Huth void helper_vmhraddshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
936fcf5ef2aSThomas Huth                        ppc_avr_t *b, ppc_avr_t *c)
937fcf5ef2aSThomas Huth {
938fcf5ef2aSThomas Huth     int sat = 0;
939fcf5ef2aSThomas Huth     int i;
940fcf5ef2aSThomas Huth 
941fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
942fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i] + 0x00004000;
943fcf5ef2aSThomas Huth         int32_t t = (int32_t)c->s16[i] + (prod >> 15);
944fcf5ef2aSThomas Huth         r->s16[i] = cvtswsh(t, &sat);
945fcf5ef2aSThomas Huth     }
946fcf5ef2aSThomas Huth 
947fcf5ef2aSThomas Huth     if (sat) {
948fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
949fcf5ef2aSThomas Huth     }
950fcf5ef2aSThomas Huth }
951fcf5ef2aSThomas Huth 
952fcf5ef2aSThomas Huth #define VMINMAX_DO(name, compare, element)                              \
953fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
954fcf5ef2aSThomas Huth     {                                                                   \
955fcf5ef2aSThomas Huth         int i;                                                          \
956fcf5ef2aSThomas Huth                                                                         \
957fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
958fcf5ef2aSThomas Huth             if (a->element[i] compare b->element[i]) {                  \
959fcf5ef2aSThomas Huth                 r->element[i] = b->element[i];                          \
960fcf5ef2aSThomas Huth             } else {                                                    \
961fcf5ef2aSThomas Huth                 r->element[i] = a->element[i];                          \
962fcf5ef2aSThomas Huth             }                                                           \
963fcf5ef2aSThomas Huth         }                                                               \
964fcf5ef2aSThomas Huth     }
965fcf5ef2aSThomas Huth #define VMINMAX(suffix, element)                \
966fcf5ef2aSThomas Huth     VMINMAX_DO(min##suffix, >, element)         \
967fcf5ef2aSThomas Huth     VMINMAX_DO(max##suffix, <, element)
968fcf5ef2aSThomas Huth VMINMAX(sb, s8)
969fcf5ef2aSThomas Huth VMINMAX(sh, s16)
970fcf5ef2aSThomas Huth VMINMAX(sw, s32)
971fcf5ef2aSThomas Huth VMINMAX(sd, s64)
972fcf5ef2aSThomas Huth VMINMAX(ub, u8)
973fcf5ef2aSThomas Huth VMINMAX(uh, u16)
974fcf5ef2aSThomas Huth VMINMAX(uw, u32)
975fcf5ef2aSThomas Huth VMINMAX(ud, u64)
976fcf5ef2aSThomas Huth #undef VMINMAX_DO
977fcf5ef2aSThomas Huth #undef VMINMAX
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth void helper_vmladduhm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
980fcf5ef2aSThomas Huth {
981fcf5ef2aSThomas Huth     int i;
982fcf5ef2aSThomas Huth 
983fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
984fcf5ef2aSThomas Huth         int32_t prod = a->s16[i] * b->s16[i];
985fcf5ef2aSThomas Huth         r->s16[i] = (int16_t) (prod + c->s16[i]);
986fcf5ef2aSThomas Huth     }
987fcf5ef2aSThomas Huth }
988fcf5ef2aSThomas Huth 
989fcf5ef2aSThomas Huth #define VMRG_DO(name, element, highp)                                   \
990fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
991fcf5ef2aSThomas Huth     {                                                                   \
992fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
993fcf5ef2aSThomas Huth         int i;                                                          \
994fcf5ef2aSThomas Huth         size_t n_elems = ARRAY_SIZE(r->element);                        \
995fcf5ef2aSThomas Huth                                                                         \
996fcf5ef2aSThomas Huth         for (i = 0; i < n_elems / 2; i++) {                             \
997fcf5ef2aSThomas Huth             if (highp) {                                                \
998fcf5ef2aSThomas Huth                 result.element[i*2+HI_IDX] = a->element[i];             \
999fcf5ef2aSThomas Huth                 result.element[i*2+LO_IDX] = b->element[i];             \
1000fcf5ef2aSThomas Huth             } else {                                                    \
1001fcf5ef2aSThomas Huth                 result.element[n_elems - i * 2 - (1 + HI_IDX)] =        \
1002fcf5ef2aSThomas Huth                     b->element[n_elems - i - 1];                        \
1003fcf5ef2aSThomas Huth                 result.element[n_elems - i * 2 - (1 + LO_IDX)] =        \
1004fcf5ef2aSThomas Huth                     a->element[n_elems - i - 1];                        \
1005fcf5ef2aSThomas Huth             }                                                           \
1006fcf5ef2aSThomas Huth         }                                                               \
1007fcf5ef2aSThomas Huth         *r = result;                                                    \
1008fcf5ef2aSThomas Huth     }
1009fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1010fcf5ef2aSThomas Huth #define MRGHI 0
1011fcf5ef2aSThomas Huth #define MRGLO 1
1012fcf5ef2aSThomas Huth #else
1013fcf5ef2aSThomas Huth #define MRGHI 1
1014fcf5ef2aSThomas Huth #define MRGLO 0
1015fcf5ef2aSThomas Huth #endif
1016fcf5ef2aSThomas Huth #define VMRG(suffix, element)                   \
1017fcf5ef2aSThomas Huth     VMRG_DO(mrgl##suffix, element, MRGHI)       \
1018fcf5ef2aSThomas Huth     VMRG_DO(mrgh##suffix, element, MRGLO)
1019fcf5ef2aSThomas Huth VMRG(b, u8)
1020fcf5ef2aSThomas Huth VMRG(h, u16)
1021fcf5ef2aSThomas Huth VMRG(w, u32)
1022fcf5ef2aSThomas Huth #undef VMRG_DO
1023fcf5ef2aSThomas Huth #undef VMRG
1024fcf5ef2aSThomas Huth #undef MRGHI
1025fcf5ef2aSThomas Huth #undef MRGLO
1026fcf5ef2aSThomas Huth 
1027fcf5ef2aSThomas Huth void helper_vmsummbm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1028fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1029fcf5ef2aSThomas Huth {
1030fcf5ef2aSThomas Huth     int32_t prod[16];
1031fcf5ef2aSThomas Huth     int i;
1032fcf5ef2aSThomas Huth 
1033fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s8); i++) {
1034fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s8[i] * b->u8[i];
1035fcf5ef2aSThomas Huth     }
1036fcf5ef2aSThomas Huth 
1037fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1038fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[4 * i] + prod[4 * i + 1] +
1039fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
1040fcf5ef2aSThomas Huth     }
1041fcf5ef2aSThomas Huth }
1042fcf5ef2aSThomas Huth 
1043fcf5ef2aSThomas Huth void helper_vmsumshm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1044fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1045fcf5ef2aSThomas Huth {
1046fcf5ef2aSThomas Huth     int32_t prod[8];
1047fcf5ef2aSThomas Huth     int i;
1048fcf5ef2aSThomas Huth 
1049fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
1050fcf5ef2aSThomas Huth         prod[i] = a->s16[i] * b->s16[i];
1051fcf5ef2aSThomas Huth     }
1052fcf5ef2aSThomas Huth 
1053fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1054fcf5ef2aSThomas Huth         r->s32[i] = c->s32[i] + prod[2 * i] + prod[2 * i + 1];
1055fcf5ef2aSThomas Huth     }
1056fcf5ef2aSThomas Huth }
1057fcf5ef2aSThomas Huth 
1058fcf5ef2aSThomas Huth void helper_vmsumshs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1059fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1060fcf5ef2aSThomas Huth {
1061fcf5ef2aSThomas Huth     int32_t prod[8];
1062fcf5ef2aSThomas Huth     int i;
1063fcf5ef2aSThomas Huth     int sat = 0;
1064fcf5ef2aSThomas Huth 
1065fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s16); i++) {
1066fcf5ef2aSThomas Huth         prod[i] = (int32_t)a->s16[i] * b->s16[i];
1067fcf5ef2aSThomas Huth     }
1068fcf5ef2aSThomas Huth 
1069fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1070fcf5ef2aSThomas Huth         int64_t t = (int64_t)c->s32[i] + prod[2 * i] + prod[2 * i + 1];
1071fcf5ef2aSThomas Huth 
1072fcf5ef2aSThomas Huth         r->u32[i] = cvtsdsw(t, &sat);
1073fcf5ef2aSThomas Huth     }
1074fcf5ef2aSThomas Huth 
1075fcf5ef2aSThomas Huth     if (sat) {
1076fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
1077fcf5ef2aSThomas Huth     }
1078fcf5ef2aSThomas Huth }
1079fcf5ef2aSThomas Huth 
1080fcf5ef2aSThomas Huth void helper_vmsumubm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1081fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1082fcf5ef2aSThomas Huth {
1083fcf5ef2aSThomas Huth     uint16_t prod[16];
1084fcf5ef2aSThomas Huth     int i;
1085fcf5ef2aSThomas Huth 
1086fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1087fcf5ef2aSThomas Huth         prod[i] = a->u8[i] * b->u8[i];
1088fcf5ef2aSThomas Huth     }
1089fcf5ef2aSThomas Huth 
1090fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1091fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[4 * i] + prod[4 * i + 1] +
1092fcf5ef2aSThomas Huth             prod[4 * i + 2] + prod[4 * i + 3];
1093fcf5ef2aSThomas Huth     }
1094fcf5ef2aSThomas Huth }
1095fcf5ef2aSThomas Huth 
1096fcf5ef2aSThomas Huth void helper_vmsumuhm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1097fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1098fcf5ef2aSThomas Huth {
1099fcf5ef2aSThomas Huth     uint32_t prod[8];
1100fcf5ef2aSThomas Huth     int i;
1101fcf5ef2aSThomas Huth 
1102fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1103fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1104fcf5ef2aSThomas Huth     }
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
1107fcf5ef2aSThomas Huth         r->u32[i] = c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1108fcf5ef2aSThomas Huth     }
1109fcf5ef2aSThomas Huth }
1110fcf5ef2aSThomas Huth 
1111fcf5ef2aSThomas Huth void helper_vmsumuhs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a,
1112fcf5ef2aSThomas Huth                      ppc_avr_t *b, ppc_avr_t *c)
1113fcf5ef2aSThomas Huth {
1114fcf5ef2aSThomas Huth     uint32_t prod[8];
1115fcf5ef2aSThomas Huth     int i;
1116fcf5ef2aSThomas Huth     int sat = 0;
1117fcf5ef2aSThomas Huth 
1118fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u16); i++) {
1119fcf5ef2aSThomas Huth         prod[i] = a->u16[i] * b->u16[i];
1120fcf5ef2aSThomas Huth     }
1121fcf5ef2aSThomas Huth 
1122fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, s32) {
1123fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)c->u32[i] + prod[2 * i] + prod[2 * i + 1];
1124fcf5ef2aSThomas Huth 
1125fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
1126fcf5ef2aSThomas Huth     }
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth     if (sat) {
1129fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
1130fcf5ef2aSThomas Huth     }
1131fcf5ef2aSThomas Huth }
1132fcf5ef2aSThomas Huth 
1133fcf5ef2aSThomas Huth #define VMUL_DO(name, mul_element, prod_element, cast, evenp)           \
1134fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)       \
1135fcf5ef2aSThomas Huth     {                                                                   \
1136fcf5ef2aSThomas Huth         int i;                                                          \
1137fcf5ef2aSThomas Huth                                                                         \
1138fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(i, prod_element) {                         \
1139fcf5ef2aSThomas Huth             if (evenp) {                                                \
1140fcf5ef2aSThomas Huth                 r->prod_element[i] =                                    \
1141fcf5ef2aSThomas Huth                     (cast)a->mul_element[i * 2 + HI_IDX] *              \
1142fcf5ef2aSThomas Huth                     (cast)b->mul_element[i * 2 + HI_IDX];               \
1143fcf5ef2aSThomas Huth             } else {                                                    \
1144fcf5ef2aSThomas Huth                 r->prod_element[i] =                                    \
1145fcf5ef2aSThomas Huth                     (cast)a->mul_element[i * 2 + LO_IDX] *              \
1146fcf5ef2aSThomas Huth                     (cast)b->mul_element[i * 2 + LO_IDX];               \
1147fcf5ef2aSThomas Huth             }                                                           \
1148fcf5ef2aSThomas Huth         }                                                               \
1149fcf5ef2aSThomas Huth     }
1150fcf5ef2aSThomas Huth #define VMUL(suffix, mul_element, prod_element, cast)            \
1151fcf5ef2aSThomas Huth     VMUL_DO(mule##suffix, mul_element, prod_element, cast, 1)    \
1152fcf5ef2aSThomas Huth     VMUL_DO(mulo##suffix, mul_element, prod_element, cast, 0)
1153fcf5ef2aSThomas Huth VMUL(sb, s8, s16, int16_t)
1154fcf5ef2aSThomas Huth VMUL(sh, s16, s32, int32_t)
1155fcf5ef2aSThomas Huth VMUL(sw, s32, s64, int64_t)
1156fcf5ef2aSThomas Huth VMUL(ub, u8, u16, uint16_t)
1157fcf5ef2aSThomas Huth VMUL(uh, u16, u32, uint32_t)
1158fcf5ef2aSThomas Huth VMUL(uw, u32, u64, uint64_t)
1159fcf5ef2aSThomas Huth #undef VMUL_DO
1160fcf5ef2aSThomas Huth #undef VMUL
1161fcf5ef2aSThomas Huth 
1162fcf5ef2aSThomas Huth void helper_vperm(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b,
1163fcf5ef2aSThomas Huth                   ppc_avr_t *c)
1164fcf5ef2aSThomas Huth {
1165fcf5ef2aSThomas Huth     ppc_avr_t result;
1166fcf5ef2aSThomas Huth     int i;
1167fcf5ef2aSThomas Huth 
1168fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1169fcf5ef2aSThomas Huth         int s = c->u8[i] & 0x1f;
1170fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1171fcf5ef2aSThomas Huth         int index = s & 0xf;
1172fcf5ef2aSThomas Huth #else
1173fcf5ef2aSThomas Huth         int index = 15 - (s & 0xf);
1174fcf5ef2aSThomas Huth #endif
1175fcf5ef2aSThomas Huth 
1176fcf5ef2aSThomas Huth         if (s & 0x10) {
1177fcf5ef2aSThomas Huth             result.u8[i] = b->u8[index];
1178fcf5ef2aSThomas Huth         } else {
1179fcf5ef2aSThomas Huth             result.u8[i] = a->u8[index];
1180fcf5ef2aSThomas Huth         }
1181fcf5ef2aSThomas Huth     }
1182fcf5ef2aSThomas Huth     *r = result;
1183fcf5ef2aSThomas Huth }
1184fcf5ef2aSThomas Huth 
1185fcf5ef2aSThomas Huth void helper_vpermr(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b,
1186fcf5ef2aSThomas Huth                   ppc_avr_t *c)
1187fcf5ef2aSThomas Huth {
1188fcf5ef2aSThomas Huth     ppc_avr_t result;
1189fcf5ef2aSThomas Huth     int i;
1190fcf5ef2aSThomas Huth 
1191fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1192fcf5ef2aSThomas Huth         int s = c->u8[i] & 0x1f;
1193fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1194fcf5ef2aSThomas Huth         int index = 15 - (s & 0xf);
1195fcf5ef2aSThomas Huth #else
1196fcf5ef2aSThomas Huth         int index = s & 0xf;
1197fcf5ef2aSThomas Huth #endif
1198fcf5ef2aSThomas Huth 
1199fcf5ef2aSThomas Huth         if (s & 0x10) {
1200fcf5ef2aSThomas Huth             result.u8[i] = a->u8[index];
1201fcf5ef2aSThomas Huth         } else {
1202fcf5ef2aSThomas Huth             result.u8[i] = b->u8[index];
1203fcf5ef2aSThomas Huth         }
1204fcf5ef2aSThomas Huth     }
1205fcf5ef2aSThomas Huth     *r = result;
1206fcf5ef2aSThomas Huth }
1207fcf5ef2aSThomas Huth 
1208fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1209fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[(i)])
1210fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (i)
1211fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) != 0)
1212fcf5ef2aSThomas Huth #define EXTRACT_BIT(avr, i, index) (extract64((avr)->u64[i], index, 1))
1213fcf5ef2aSThomas Huth #else
1214fcf5ef2aSThomas Huth #define VBPERMQ_INDEX(avr, i) ((avr)->u8[15-(i)])
1215fcf5ef2aSThomas Huth #define VBPERMD_INDEX(i) (1 - i)
1216fcf5ef2aSThomas Huth #define VBPERMQ_DW(index) (((index) & 0x40) == 0)
1217fcf5ef2aSThomas Huth #define EXTRACT_BIT(avr, i, index) \
1218fcf5ef2aSThomas Huth         (extract64((avr)->u64[1 - i], 63 - index, 1))
1219fcf5ef2aSThomas Huth #endif
1220fcf5ef2aSThomas Huth 
1221fcf5ef2aSThomas Huth void helper_vbpermd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1222fcf5ef2aSThomas Huth {
1223fcf5ef2aSThomas Huth     int i, j;
1224fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
1225fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1226fcf5ef2aSThomas Huth         for (j = 0; j < 8; j++) {
1227fcf5ef2aSThomas Huth             int index = VBPERMQ_INDEX(b, (i * 8) + j);
1228fcf5ef2aSThomas Huth             if (index < 64 && EXTRACT_BIT(a, i, index)) {
1229fcf5ef2aSThomas Huth                 result.u64[VBPERMD_INDEX(i)] |= (0x80 >> j);
1230fcf5ef2aSThomas Huth             }
1231fcf5ef2aSThomas Huth         }
1232fcf5ef2aSThomas Huth     }
1233fcf5ef2aSThomas Huth     *r = result;
1234fcf5ef2aSThomas Huth }
1235fcf5ef2aSThomas Huth 
1236fcf5ef2aSThomas Huth void helper_vbpermq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1237fcf5ef2aSThomas Huth {
1238fcf5ef2aSThomas Huth     int i;
1239fcf5ef2aSThomas Huth     uint64_t perm = 0;
1240fcf5ef2aSThomas Huth 
1241fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1242fcf5ef2aSThomas Huth         int index = VBPERMQ_INDEX(b, i);
1243fcf5ef2aSThomas Huth 
1244fcf5ef2aSThomas Huth         if (index < 128) {
1245fcf5ef2aSThomas Huth             uint64_t mask = (1ull << (63-(index & 0x3F)));
1246fcf5ef2aSThomas Huth             if (a->u64[VBPERMQ_DW(index)] & mask) {
1247fcf5ef2aSThomas Huth                 perm |= (0x8000 >> i);
1248fcf5ef2aSThomas Huth             }
1249fcf5ef2aSThomas Huth         }
1250fcf5ef2aSThomas Huth     }
1251fcf5ef2aSThomas Huth 
1252fcf5ef2aSThomas Huth     r->u64[HI_IDX] = perm;
1253fcf5ef2aSThomas Huth     r->u64[LO_IDX] = 0;
1254fcf5ef2aSThomas Huth }
1255fcf5ef2aSThomas Huth 
1256fcf5ef2aSThomas Huth #undef VBPERMQ_INDEX
1257fcf5ef2aSThomas Huth #undef VBPERMQ_DW
1258fcf5ef2aSThomas Huth 
1259fcf5ef2aSThomas Huth static const uint64_t VGBBD_MASKS[256] = {
1260fcf5ef2aSThomas Huth     0x0000000000000000ull, /* 00 */
1261fcf5ef2aSThomas Huth     0x0000000000000080ull, /* 01 */
1262fcf5ef2aSThomas Huth     0x0000000000008000ull, /* 02 */
1263fcf5ef2aSThomas Huth     0x0000000000008080ull, /* 03 */
1264fcf5ef2aSThomas Huth     0x0000000000800000ull, /* 04 */
1265fcf5ef2aSThomas Huth     0x0000000000800080ull, /* 05 */
1266fcf5ef2aSThomas Huth     0x0000000000808000ull, /* 06 */
1267fcf5ef2aSThomas Huth     0x0000000000808080ull, /* 07 */
1268fcf5ef2aSThomas Huth     0x0000000080000000ull, /* 08 */
1269fcf5ef2aSThomas Huth     0x0000000080000080ull, /* 09 */
1270fcf5ef2aSThomas Huth     0x0000000080008000ull, /* 0A */
1271fcf5ef2aSThomas Huth     0x0000000080008080ull, /* 0B */
1272fcf5ef2aSThomas Huth     0x0000000080800000ull, /* 0C */
1273fcf5ef2aSThomas Huth     0x0000000080800080ull, /* 0D */
1274fcf5ef2aSThomas Huth     0x0000000080808000ull, /* 0E */
1275fcf5ef2aSThomas Huth     0x0000000080808080ull, /* 0F */
1276fcf5ef2aSThomas Huth     0x0000008000000000ull, /* 10 */
1277fcf5ef2aSThomas Huth     0x0000008000000080ull, /* 11 */
1278fcf5ef2aSThomas Huth     0x0000008000008000ull, /* 12 */
1279fcf5ef2aSThomas Huth     0x0000008000008080ull, /* 13 */
1280fcf5ef2aSThomas Huth     0x0000008000800000ull, /* 14 */
1281fcf5ef2aSThomas Huth     0x0000008000800080ull, /* 15 */
1282fcf5ef2aSThomas Huth     0x0000008000808000ull, /* 16 */
1283fcf5ef2aSThomas Huth     0x0000008000808080ull, /* 17 */
1284fcf5ef2aSThomas Huth     0x0000008080000000ull, /* 18 */
1285fcf5ef2aSThomas Huth     0x0000008080000080ull, /* 19 */
1286fcf5ef2aSThomas Huth     0x0000008080008000ull, /* 1A */
1287fcf5ef2aSThomas Huth     0x0000008080008080ull, /* 1B */
1288fcf5ef2aSThomas Huth     0x0000008080800000ull, /* 1C */
1289fcf5ef2aSThomas Huth     0x0000008080800080ull, /* 1D */
1290fcf5ef2aSThomas Huth     0x0000008080808000ull, /* 1E */
1291fcf5ef2aSThomas Huth     0x0000008080808080ull, /* 1F */
1292fcf5ef2aSThomas Huth     0x0000800000000000ull, /* 20 */
1293fcf5ef2aSThomas Huth     0x0000800000000080ull, /* 21 */
1294fcf5ef2aSThomas Huth     0x0000800000008000ull, /* 22 */
1295fcf5ef2aSThomas Huth     0x0000800000008080ull, /* 23 */
1296fcf5ef2aSThomas Huth     0x0000800000800000ull, /* 24 */
1297fcf5ef2aSThomas Huth     0x0000800000800080ull, /* 25 */
1298fcf5ef2aSThomas Huth     0x0000800000808000ull, /* 26 */
1299fcf5ef2aSThomas Huth     0x0000800000808080ull, /* 27 */
1300fcf5ef2aSThomas Huth     0x0000800080000000ull, /* 28 */
1301fcf5ef2aSThomas Huth     0x0000800080000080ull, /* 29 */
1302fcf5ef2aSThomas Huth     0x0000800080008000ull, /* 2A */
1303fcf5ef2aSThomas Huth     0x0000800080008080ull, /* 2B */
1304fcf5ef2aSThomas Huth     0x0000800080800000ull, /* 2C */
1305fcf5ef2aSThomas Huth     0x0000800080800080ull, /* 2D */
1306fcf5ef2aSThomas Huth     0x0000800080808000ull, /* 2E */
1307fcf5ef2aSThomas Huth     0x0000800080808080ull, /* 2F */
1308fcf5ef2aSThomas Huth     0x0000808000000000ull, /* 30 */
1309fcf5ef2aSThomas Huth     0x0000808000000080ull, /* 31 */
1310fcf5ef2aSThomas Huth     0x0000808000008000ull, /* 32 */
1311fcf5ef2aSThomas Huth     0x0000808000008080ull, /* 33 */
1312fcf5ef2aSThomas Huth     0x0000808000800000ull, /* 34 */
1313fcf5ef2aSThomas Huth     0x0000808000800080ull, /* 35 */
1314fcf5ef2aSThomas Huth     0x0000808000808000ull, /* 36 */
1315fcf5ef2aSThomas Huth     0x0000808000808080ull, /* 37 */
1316fcf5ef2aSThomas Huth     0x0000808080000000ull, /* 38 */
1317fcf5ef2aSThomas Huth     0x0000808080000080ull, /* 39 */
1318fcf5ef2aSThomas Huth     0x0000808080008000ull, /* 3A */
1319fcf5ef2aSThomas Huth     0x0000808080008080ull, /* 3B */
1320fcf5ef2aSThomas Huth     0x0000808080800000ull, /* 3C */
1321fcf5ef2aSThomas Huth     0x0000808080800080ull, /* 3D */
1322fcf5ef2aSThomas Huth     0x0000808080808000ull, /* 3E */
1323fcf5ef2aSThomas Huth     0x0000808080808080ull, /* 3F */
1324fcf5ef2aSThomas Huth     0x0080000000000000ull, /* 40 */
1325fcf5ef2aSThomas Huth     0x0080000000000080ull, /* 41 */
1326fcf5ef2aSThomas Huth     0x0080000000008000ull, /* 42 */
1327fcf5ef2aSThomas Huth     0x0080000000008080ull, /* 43 */
1328fcf5ef2aSThomas Huth     0x0080000000800000ull, /* 44 */
1329fcf5ef2aSThomas Huth     0x0080000000800080ull, /* 45 */
1330fcf5ef2aSThomas Huth     0x0080000000808000ull, /* 46 */
1331fcf5ef2aSThomas Huth     0x0080000000808080ull, /* 47 */
1332fcf5ef2aSThomas Huth     0x0080000080000000ull, /* 48 */
1333fcf5ef2aSThomas Huth     0x0080000080000080ull, /* 49 */
1334fcf5ef2aSThomas Huth     0x0080000080008000ull, /* 4A */
1335fcf5ef2aSThomas Huth     0x0080000080008080ull, /* 4B */
1336fcf5ef2aSThomas Huth     0x0080000080800000ull, /* 4C */
1337fcf5ef2aSThomas Huth     0x0080000080800080ull, /* 4D */
1338fcf5ef2aSThomas Huth     0x0080000080808000ull, /* 4E */
1339fcf5ef2aSThomas Huth     0x0080000080808080ull, /* 4F */
1340fcf5ef2aSThomas Huth     0x0080008000000000ull, /* 50 */
1341fcf5ef2aSThomas Huth     0x0080008000000080ull, /* 51 */
1342fcf5ef2aSThomas Huth     0x0080008000008000ull, /* 52 */
1343fcf5ef2aSThomas Huth     0x0080008000008080ull, /* 53 */
1344fcf5ef2aSThomas Huth     0x0080008000800000ull, /* 54 */
1345fcf5ef2aSThomas Huth     0x0080008000800080ull, /* 55 */
1346fcf5ef2aSThomas Huth     0x0080008000808000ull, /* 56 */
1347fcf5ef2aSThomas Huth     0x0080008000808080ull, /* 57 */
1348fcf5ef2aSThomas Huth     0x0080008080000000ull, /* 58 */
1349fcf5ef2aSThomas Huth     0x0080008080000080ull, /* 59 */
1350fcf5ef2aSThomas Huth     0x0080008080008000ull, /* 5A */
1351fcf5ef2aSThomas Huth     0x0080008080008080ull, /* 5B */
1352fcf5ef2aSThomas Huth     0x0080008080800000ull, /* 5C */
1353fcf5ef2aSThomas Huth     0x0080008080800080ull, /* 5D */
1354fcf5ef2aSThomas Huth     0x0080008080808000ull, /* 5E */
1355fcf5ef2aSThomas Huth     0x0080008080808080ull, /* 5F */
1356fcf5ef2aSThomas Huth     0x0080800000000000ull, /* 60 */
1357fcf5ef2aSThomas Huth     0x0080800000000080ull, /* 61 */
1358fcf5ef2aSThomas Huth     0x0080800000008000ull, /* 62 */
1359fcf5ef2aSThomas Huth     0x0080800000008080ull, /* 63 */
1360fcf5ef2aSThomas Huth     0x0080800000800000ull, /* 64 */
1361fcf5ef2aSThomas Huth     0x0080800000800080ull, /* 65 */
1362fcf5ef2aSThomas Huth     0x0080800000808000ull, /* 66 */
1363fcf5ef2aSThomas Huth     0x0080800000808080ull, /* 67 */
1364fcf5ef2aSThomas Huth     0x0080800080000000ull, /* 68 */
1365fcf5ef2aSThomas Huth     0x0080800080000080ull, /* 69 */
1366fcf5ef2aSThomas Huth     0x0080800080008000ull, /* 6A */
1367fcf5ef2aSThomas Huth     0x0080800080008080ull, /* 6B */
1368fcf5ef2aSThomas Huth     0x0080800080800000ull, /* 6C */
1369fcf5ef2aSThomas Huth     0x0080800080800080ull, /* 6D */
1370fcf5ef2aSThomas Huth     0x0080800080808000ull, /* 6E */
1371fcf5ef2aSThomas Huth     0x0080800080808080ull, /* 6F */
1372fcf5ef2aSThomas Huth     0x0080808000000000ull, /* 70 */
1373fcf5ef2aSThomas Huth     0x0080808000000080ull, /* 71 */
1374fcf5ef2aSThomas Huth     0x0080808000008000ull, /* 72 */
1375fcf5ef2aSThomas Huth     0x0080808000008080ull, /* 73 */
1376fcf5ef2aSThomas Huth     0x0080808000800000ull, /* 74 */
1377fcf5ef2aSThomas Huth     0x0080808000800080ull, /* 75 */
1378fcf5ef2aSThomas Huth     0x0080808000808000ull, /* 76 */
1379fcf5ef2aSThomas Huth     0x0080808000808080ull, /* 77 */
1380fcf5ef2aSThomas Huth     0x0080808080000000ull, /* 78 */
1381fcf5ef2aSThomas Huth     0x0080808080000080ull, /* 79 */
1382fcf5ef2aSThomas Huth     0x0080808080008000ull, /* 7A */
1383fcf5ef2aSThomas Huth     0x0080808080008080ull, /* 7B */
1384fcf5ef2aSThomas Huth     0x0080808080800000ull, /* 7C */
1385fcf5ef2aSThomas Huth     0x0080808080800080ull, /* 7D */
1386fcf5ef2aSThomas Huth     0x0080808080808000ull, /* 7E */
1387fcf5ef2aSThomas Huth     0x0080808080808080ull, /* 7F */
1388fcf5ef2aSThomas Huth     0x8000000000000000ull, /* 80 */
1389fcf5ef2aSThomas Huth     0x8000000000000080ull, /* 81 */
1390fcf5ef2aSThomas Huth     0x8000000000008000ull, /* 82 */
1391fcf5ef2aSThomas Huth     0x8000000000008080ull, /* 83 */
1392fcf5ef2aSThomas Huth     0x8000000000800000ull, /* 84 */
1393fcf5ef2aSThomas Huth     0x8000000000800080ull, /* 85 */
1394fcf5ef2aSThomas Huth     0x8000000000808000ull, /* 86 */
1395fcf5ef2aSThomas Huth     0x8000000000808080ull, /* 87 */
1396fcf5ef2aSThomas Huth     0x8000000080000000ull, /* 88 */
1397fcf5ef2aSThomas Huth     0x8000000080000080ull, /* 89 */
1398fcf5ef2aSThomas Huth     0x8000000080008000ull, /* 8A */
1399fcf5ef2aSThomas Huth     0x8000000080008080ull, /* 8B */
1400fcf5ef2aSThomas Huth     0x8000000080800000ull, /* 8C */
1401fcf5ef2aSThomas Huth     0x8000000080800080ull, /* 8D */
1402fcf5ef2aSThomas Huth     0x8000000080808000ull, /* 8E */
1403fcf5ef2aSThomas Huth     0x8000000080808080ull, /* 8F */
1404fcf5ef2aSThomas Huth     0x8000008000000000ull, /* 90 */
1405fcf5ef2aSThomas Huth     0x8000008000000080ull, /* 91 */
1406fcf5ef2aSThomas Huth     0x8000008000008000ull, /* 92 */
1407fcf5ef2aSThomas Huth     0x8000008000008080ull, /* 93 */
1408fcf5ef2aSThomas Huth     0x8000008000800000ull, /* 94 */
1409fcf5ef2aSThomas Huth     0x8000008000800080ull, /* 95 */
1410fcf5ef2aSThomas Huth     0x8000008000808000ull, /* 96 */
1411fcf5ef2aSThomas Huth     0x8000008000808080ull, /* 97 */
1412fcf5ef2aSThomas Huth     0x8000008080000000ull, /* 98 */
1413fcf5ef2aSThomas Huth     0x8000008080000080ull, /* 99 */
1414fcf5ef2aSThomas Huth     0x8000008080008000ull, /* 9A */
1415fcf5ef2aSThomas Huth     0x8000008080008080ull, /* 9B */
1416fcf5ef2aSThomas Huth     0x8000008080800000ull, /* 9C */
1417fcf5ef2aSThomas Huth     0x8000008080800080ull, /* 9D */
1418fcf5ef2aSThomas Huth     0x8000008080808000ull, /* 9E */
1419fcf5ef2aSThomas Huth     0x8000008080808080ull, /* 9F */
1420fcf5ef2aSThomas Huth     0x8000800000000000ull, /* A0 */
1421fcf5ef2aSThomas Huth     0x8000800000000080ull, /* A1 */
1422fcf5ef2aSThomas Huth     0x8000800000008000ull, /* A2 */
1423fcf5ef2aSThomas Huth     0x8000800000008080ull, /* A3 */
1424fcf5ef2aSThomas Huth     0x8000800000800000ull, /* A4 */
1425fcf5ef2aSThomas Huth     0x8000800000800080ull, /* A5 */
1426fcf5ef2aSThomas Huth     0x8000800000808000ull, /* A6 */
1427fcf5ef2aSThomas Huth     0x8000800000808080ull, /* A7 */
1428fcf5ef2aSThomas Huth     0x8000800080000000ull, /* A8 */
1429fcf5ef2aSThomas Huth     0x8000800080000080ull, /* A9 */
1430fcf5ef2aSThomas Huth     0x8000800080008000ull, /* AA */
1431fcf5ef2aSThomas Huth     0x8000800080008080ull, /* AB */
1432fcf5ef2aSThomas Huth     0x8000800080800000ull, /* AC */
1433fcf5ef2aSThomas Huth     0x8000800080800080ull, /* AD */
1434fcf5ef2aSThomas Huth     0x8000800080808000ull, /* AE */
1435fcf5ef2aSThomas Huth     0x8000800080808080ull, /* AF */
1436fcf5ef2aSThomas Huth     0x8000808000000000ull, /* B0 */
1437fcf5ef2aSThomas Huth     0x8000808000000080ull, /* B1 */
1438fcf5ef2aSThomas Huth     0x8000808000008000ull, /* B2 */
1439fcf5ef2aSThomas Huth     0x8000808000008080ull, /* B3 */
1440fcf5ef2aSThomas Huth     0x8000808000800000ull, /* B4 */
1441fcf5ef2aSThomas Huth     0x8000808000800080ull, /* B5 */
1442fcf5ef2aSThomas Huth     0x8000808000808000ull, /* B6 */
1443fcf5ef2aSThomas Huth     0x8000808000808080ull, /* B7 */
1444fcf5ef2aSThomas Huth     0x8000808080000000ull, /* B8 */
1445fcf5ef2aSThomas Huth     0x8000808080000080ull, /* B9 */
1446fcf5ef2aSThomas Huth     0x8000808080008000ull, /* BA */
1447fcf5ef2aSThomas Huth     0x8000808080008080ull, /* BB */
1448fcf5ef2aSThomas Huth     0x8000808080800000ull, /* BC */
1449fcf5ef2aSThomas Huth     0x8000808080800080ull, /* BD */
1450fcf5ef2aSThomas Huth     0x8000808080808000ull, /* BE */
1451fcf5ef2aSThomas Huth     0x8000808080808080ull, /* BF */
1452fcf5ef2aSThomas Huth     0x8080000000000000ull, /* C0 */
1453fcf5ef2aSThomas Huth     0x8080000000000080ull, /* C1 */
1454fcf5ef2aSThomas Huth     0x8080000000008000ull, /* C2 */
1455fcf5ef2aSThomas Huth     0x8080000000008080ull, /* C3 */
1456fcf5ef2aSThomas Huth     0x8080000000800000ull, /* C4 */
1457fcf5ef2aSThomas Huth     0x8080000000800080ull, /* C5 */
1458fcf5ef2aSThomas Huth     0x8080000000808000ull, /* C6 */
1459fcf5ef2aSThomas Huth     0x8080000000808080ull, /* C7 */
1460fcf5ef2aSThomas Huth     0x8080000080000000ull, /* C8 */
1461fcf5ef2aSThomas Huth     0x8080000080000080ull, /* C9 */
1462fcf5ef2aSThomas Huth     0x8080000080008000ull, /* CA */
1463fcf5ef2aSThomas Huth     0x8080000080008080ull, /* CB */
1464fcf5ef2aSThomas Huth     0x8080000080800000ull, /* CC */
1465fcf5ef2aSThomas Huth     0x8080000080800080ull, /* CD */
1466fcf5ef2aSThomas Huth     0x8080000080808000ull, /* CE */
1467fcf5ef2aSThomas Huth     0x8080000080808080ull, /* CF */
1468fcf5ef2aSThomas Huth     0x8080008000000000ull, /* D0 */
1469fcf5ef2aSThomas Huth     0x8080008000000080ull, /* D1 */
1470fcf5ef2aSThomas Huth     0x8080008000008000ull, /* D2 */
1471fcf5ef2aSThomas Huth     0x8080008000008080ull, /* D3 */
1472fcf5ef2aSThomas Huth     0x8080008000800000ull, /* D4 */
1473fcf5ef2aSThomas Huth     0x8080008000800080ull, /* D5 */
1474fcf5ef2aSThomas Huth     0x8080008000808000ull, /* D6 */
1475fcf5ef2aSThomas Huth     0x8080008000808080ull, /* D7 */
1476fcf5ef2aSThomas Huth     0x8080008080000000ull, /* D8 */
1477fcf5ef2aSThomas Huth     0x8080008080000080ull, /* D9 */
1478fcf5ef2aSThomas Huth     0x8080008080008000ull, /* DA */
1479fcf5ef2aSThomas Huth     0x8080008080008080ull, /* DB */
1480fcf5ef2aSThomas Huth     0x8080008080800000ull, /* DC */
1481fcf5ef2aSThomas Huth     0x8080008080800080ull, /* DD */
1482fcf5ef2aSThomas Huth     0x8080008080808000ull, /* DE */
1483fcf5ef2aSThomas Huth     0x8080008080808080ull, /* DF */
1484fcf5ef2aSThomas Huth     0x8080800000000000ull, /* E0 */
1485fcf5ef2aSThomas Huth     0x8080800000000080ull, /* E1 */
1486fcf5ef2aSThomas Huth     0x8080800000008000ull, /* E2 */
1487fcf5ef2aSThomas Huth     0x8080800000008080ull, /* E3 */
1488fcf5ef2aSThomas Huth     0x8080800000800000ull, /* E4 */
1489fcf5ef2aSThomas Huth     0x8080800000800080ull, /* E5 */
1490fcf5ef2aSThomas Huth     0x8080800000808000ull, /* E6 */
1491fcf5ef2aSThomas Huth     0x8080800000808080ull, /* E7 */
1492fcf5ef2aSThomas Huth     0x8080800080000000ull, /* E8 */
1493fcf5ef2aSThomas Huth     0x8080800080000080ull, /* E9 */
1494fcf5ef2aSThomas Huth     0x8080800080008000ull, /* EA */
1495fcf5ef2aSThomas Huth     0x8080800080008080ull, /* EB */
1496fcf5ef2aSThomas Huth     0x8080800080800000ull, /* EC */
1497fcf5ef2aSThomas Huth     0x8080800080800080ull, /* ED */
1498fcf5ef2aSThomas Huth     0x8080800080808000ull, /* EE */
1499fcf5ef2aSThomas Huth     0x8080800080808080ull, /* EF */
1500fcf5ef2aSThomas Huth     0x8080808000000000ull, /* F0 */
1501fcf5ef2aSThomas Huth     0x8080808000000080ull, /* F1 */
1502fcf5ef2aSThomas Huth     0x8080808000008000ull, /* F2 */
1503fcf5ef2aSThomas Huth     0x8080808000008080ull, /* F3 */
1504fcf5ef2aSThomas Huth     0x8080808000800000ull, /* F4 */
1505fcf5ef2aSThomas Huth     0x8080808000800080ull, /* F5 */
1506fcf5ef2aSThomas Huth     0x8080808000808000ull, /* F6 */
1507fcf5ef2aSThomas Huth     0x8080808000808080ull, /* F7 */
1508fcf5ef2aSThomas Huth     0x8080808080000000ull, /* F8 */
1509fcf5ef2aSThomas Huth     0x8080808080000080ull, /* F9 */
1510fcf5ef2aSThomas Huth     0x8080808080008000ull, /* FA */
1511fcf5ef2aSThomas Huth     0x8080808080008080ull, /* FB */
1512fcf5ef2aSThomas Huth     0x8080808080800000ull, /* FC */
1513fcf5ef2aSThomas Huth     0x8080808080800080ull, /* FD */
1514fcf5ef2aSThomas Huth     0x8080808080808000ull, /* FE */
1515fcf5ef2aSThomas Huth     0x8080808080808080ull, /* FF */
1516fcf5ef2aSThomas Huth };
1517fcf5ef2aSThomas Huth 
1518fcf5ef2aSThomas Huth void helper_vgbbd(ppc_avr_t *r, ppc_avr_t *b)
1519fcf5ef2aSThomas Huth {
1520fcf5ef2aSThomas Huth     int i;
1521fcf5ef2aSThomas Huth     uint64_t t[2] = { 0, 0 };
1522fcf5ef2aSThomas Huth 
1523fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
1524fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1525fcf5ef2aSThomas Huth         t[i>>3] |= VGBBD_MASKS[b->u8[i]] >> (i & 7);
1526fcf5ef2aSThomas Huth #else
1527fcf5ef2aSThomas Huth         t[i>>3] |= VGBBD_MASKS[b->u8[i]] >> (7-(i & 7));
1528fcf5ef2aSThomas Huth #endif
1529fcf5ef2aSThomas Huth     }
1530fcf5ef2aSThomas Huth 
1531fcf5ef2aSThomas Huth     r->u64[0] = t[0];
1532fcf5ef2aSThomas Huth     r->u64[1] = t[1];
1533fcf5ef2aSThomas Huth }
1534fcf5ef2aSThomas Huth 
1535fcf5ef2aSThomas Huth #define PMSUM(name, srcfld, trgfld, trgtyp)                   \
1536fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)  \
1537fcf5ef2aSThomas Huth {                                                             \
1538fcf5ef2aSThomas Huth     int i, j;                                                 \
1539fcf5ef2aSThomas Huth     trgtyp prod[sizeof(ppc_avr_t)/sizeof(a->srcfld[0])];      \
1540fcf5ef2aSThomas Huth                                                               \
1541fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, srcfld) {                         \
1542fcf5ef2aSThomas Huth         prod[i] = 0;                                          \
1543fcf5ef2aSThomas Huth         for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) {      \
1544fcf5ef2aSThomas Huth             if (a->srcfld[i] & (1ull<<j)) {                   \
1545fcf5ef2aSThomas Huth                 prod[i] ^= ((trgtyp)b->srcfld[i] << j);       \
1546fcf5ef2aSThomas Huth             }                                                 \
1547fcf5ef2aSThomas Huth         }                                                     \
1548fcf5ef2aSThomas Huth     }                                                         \
1549fcf5ef2aSThomas Huth                                                               \
1550fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, trgfld) {                         \
1551fcf5ef2aSThomas Huth         r->trgfld[i] = prod[2*i] ^ prod[2*i+1];               \
1552fcf5ef2aSThomas Huth     }                                                         \
1553fcf5ef2aSThomas Huth }
1554fcf5ef2aSThomas Huth 
1555fcf5ef2aSThomas Huth PMSUM(vpmsumb, u8, u16, uint16_t)
1556fcf5ef2aSThomas Huth PMSUM(vpmsumh, u16, u32, uint32_t)
1557fcf5ef2aSThomas Huth PMSUM(vpmsumw, u32, u64, uint64_t)
1558fcf5ef2aSThomas Huth 
1559fcf5ef2aSThomas Huth void helper_vpmsumd(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1560fcf5ef2aSThomas Huth {
1561fcf5ef2aSThomas Huth 
1562fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
1563fcf5ef2aSThomas Huth     int i, j;
1564fcf5ef2aSThomas Huth     __uint128_t prod[2];
1565fcf5ef2aSThomas Huth 
1566fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1567fcf5ef2aSThomas Huth         prod[i] = 0;
1568fcf5ef2aSThomas Huth         for (j = 0; j < 64; j++) {
1569fcf5ef2aSThomas Huth             if (a->u64[i] & (1ull<<j)) {
1570fcf5ef2aSThomas Huth                 prod[i] ^= (((__uint128_t)b->u64[i]) << j);
1571fcf5ef2aSThomas Huth             }
1572fcf5ef2aSThomas Huth         }
1573fcf5ef2aSThomas Huth     }
1574fcf5ef2aSThomas Huth 
1575fcf5ef2aSThomas Huth     r->u128 = prod[0] ^ prod[1];
1576fcf5ef2aSThomas Huth 
1577fcf5ef2aSThomas Huth #else
1578fcf5ef2aSThomas Huth     int i, j;
1579fcf5ef2aSThomas Huth     ppc_avr_t prod[2];
1580fcf5ef2aSThomas Huth 
1581fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1582fcf5ef2aSThomas Huth         prod[i].u64[LO_IDX] = prod[i].u64[HI_IDX] = 0;
1583fcf5ef2aSThomas Huth         for (j = 0; j < 64; j++) {
1584fcf5ef2aSThomas Huth             if (a->u64[i] & (1ull<<j)) {
1585fcf5ef2aSThomas Huth                 ppc_avr_t bshift;
1586fcf5ef2aSThomas Huth                 if (j == 0) {
1587fcf5ef2aSThomas Huth                     bshift.u64[HI_IDX] = 0;
1588fcf5ef2aSThomas Huth                     bshift.u64[LO_IDX] = b->u64[i];
1589fcf5ef2aSThomas Huth                 } else {
1590fcf5ef2aSThomas Huth                     bshift.u64[HI_IDX] = b->u64[i] >> (64-j);
1591fcf5ef2aSThomas Huth                     bshift.u64[LO_IDX] = b->u64[i] << j;
1592fcf5ef2aSThomas Huth                 }
1593fcf5ef2aSThomas Huth                 prod[i].u64[LO_IDX] ^= bshift.u64[LO_IDX];
1594fcf5ef2aSThomas Huth                 prod[i].u64[HI_IDX] ^= bshift.u64[HI_IDX];
1595fcf5ef2aSThomas Huth             }
1596fcf5ef2aSThomas Huth         }
1597fcf5ef2aSThomas Huth     }
1598fcf5ef2aSThomas Huth 
1599fcf5ef2aSThomas Huth     r->u64[LO_IDX] = prod[0].u64[LO_IDX] ^ prod[1].u64[LO_IDX];
1600fcf5ef2aSThomas Huth     r->u64[HI_IDX] = prod[0].u64[HI_IDX] ^ prod[1].u64[HI_IDX];
1601fcf5ef2aSThomas Huth #endif
1602fcf5ef2aSThomas Huth }
1603fcf5ef2aSThomas Huth 
1604fcf5ef2aSThomas Huth 
1605fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1606fcf5ef2aSThomas Huth #define PKBIG 1
1607fcf5ef2aSThomas Huth #else
1608fcf5ef2aSThomas Huth #define PKBIG 0
1609fcf5ef2aSThomas Huth #endif
1610fcf5ef2aSThomas Huth void helper_vpkpx(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1611fcf5ef2aSThomas Huth {
1612fcf5ef2aSThomas Huth     int i, j;
1613fcf5ef2aSThomas Huth     ppc_avr_t result;
1614fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1615fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { a, b };
1616fcf5ef2aSThomas Huth #else
1617fcf5ef2aSThomas Huth     const ppc_avr_t *x[2] = { b, a };
1618fcf5ef2aSThomas Huth #endif
1619fcf5ef2aSThomas Huth 
1620fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
1621fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(j, u32) {
1622fcf5ef2aSThomas Huth             uint32_t e = x[i]->u32[j];
1623fcf5ef2aSThomas Huth 
1624fcf5ef2aSThomas Huth             result.u16[4*i+j] = (((e >> 9) & 0xfc00) |
1625fcf5ef2aSThomas Huth                                  ((e >> 6) & 0x3e0) |
1626fcf5ef2aSThomas Huth                                  ((e >> 3) & 0x1f));
1627fcf5ef2aSThomas Huth         }
1628fcf5ef2aSThomas Huth     }
1629fcf5ef2aSThomas Huth     *r = result;
1630fcf5ef2aSThomas Huth }
1631fcf5ef2aSThomas Huth 
1632fcf5ef2aSThomas Huth #define VPK(suffix, from, to, cvt, dosat)                               \
1633fcf5ef2aSThomas Huth     void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r,             \
1634fcf5ef2aSThomas Huth                             ppc_avr_t *a, ppc_avr_t *b)                 \
1635fcf5ef2aSThomas Huth     {                                                                   \
1636fcf5ef2aSThomas Huth         int i;                                                          \
1637fcf5ef2aSThomas Huth         int sat = 0;                                                    \
1638fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
1639fcf5ef2aSThomas Huth         ppc_avr_t *a0 = PKBIG ? a : b;                                  \
1640fcf5ef2aSThomas Huth         ppc_avr_t *a1 = PKBIG ? b : a;                                  \
1641fcf5ef2aSThomas Huth                                                                         \
1642fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(i, from) {                                 \
1643fcf5ef2aSThomas Huth             result.to[i] = cvt(a0->from[i], &sat);                      \
1644fcf5ef2aSThomas Huth             result.to[i+ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);  \
1645fcf5ef2aSThomas Huth         }                                                               \
1646fcf5ef2aSThomas Huth         *r = result;                                                    \
1647fcf5ef2aSThomas Huth         if (dosat && sat) {                                             \
1648fcf5ef2aSThomas Huth             env->vscr |= (1 << VSCR_SAT);                               \
1649fcf5ef2aSThomas Huth         }                                                               \
1650fcf5ef2aSThomas Huth     }
1651fcf5ef2aSThomas Huth #define I(x, y) (x)
1652fcf5ef2aSThomas Huth VPK(shss, s16, s8, cvtshsb, 1)
1653fcf5ef2aSThomas Huth VPK(shus, s16, u8, cvtshub, 1)
1654fcf5ef2aSThomas Huth VPK(swss, s32, s16, cvtswsh, 1)
1655fcf5ef2aSThomas Huth VPK(swus, s32, u16, cvtswuh, 1)
1656fcf5ef2aSThomas Huth VPK(sdss, s64, s32, cvtsdsw, 1)
1657fcf5ef2aSThomas Huth VPK(sdus, s64, u32, cvtsduw, 1)
1658fcf5ef2aSThomas Huth VPK(uhus, u16, u8, cvtuhub, 1)
1659fcf5ef2aSThomas Huth VPK(uwus, u32, u16, cvtuwuh, 1)
1660fcf5ef2aSThomas Huth VPK(udus, u64, u32, cvtuduw, 1)
1661fcf5ef2aSThomas Huth VPK(uhum, u16, u8, I, 0)
1662fcf5ef2aSThomas Huth VPK(uwum, u32, u16, I, 0)
1663fcf5ef2aSThomas Huth VPK(udum, u64, u32, I, 0)
1664fcf5ef2aSThomas Huth #undef I
1665fcf5ef2aSThomas Huth #undef VPK
1666fcf5ef2aSThomas Huth #undef PKBIG
1667fcf5ef2aSThomas Huth 
1668fcf5ef2aSThomas Huth void helper_vrefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1669fcf5ef2aSThomas Huth {
1670fcf5ef2aSThomas Huth     int i;
1671fcf5ef2aSThomas Huth 
1672fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->f); i++) {
1673fcf5ef2aSThomas Huth         r->f[i] = float32_div(float32_one, b->f[i], &env->vec_status);
1674fcf5ef2aSThomas Huth     }
1675fcf5ef2aSThomas Huth }
1676fcf5ef2aSThomas Huth 
1677fcf5ef2aSThomas Huth #define VRFI(suffix, rounding)                                  \
1678fcf5ef2aSThomas Huth     void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r,    \
1679fcf5ef2aSThomas Huth                              ppc_avr_t *b)                      \
1680fcf5ef2aSThomas Huth     {                                                           \
1681fcf5ef2aSThomas Huth         int i;                                                  \
1682fcf5ef2aSThomas Huth         float_status s = env->vec_status;                       \
1683fcf5ef2aSThomas Huth                                                                 \
1684fcf5ef2aSThomas Huth         set_float_rounding_mode(rounding, &s);                  \
1685fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->f); i++) {                \
1686fcf5ef2aSThomas Huth             r->f[i] = float32_round_to_int (b->f[i], &s);       \
1687fcf5ef2aSThomas Huth         }                                                       \
1688fcf5ef2aSThomas Huth     }
1689fcf5ef2aSThomas Huth VRFI(n, float_round_nearest_even)
1690fcf5ef2aSThomas Huth VRFI(m, float_round_down)
1691fcf5ef2aSThomas Huth VRFI(p, float_round_up)
1692fcf5ef2aSThomas Huth VRFI(z, float_round_to_zero)
1693fcf5ef2aSThomas Huth #undef VRFI
1694fcf5ef2aSThomas Huth 
1695fcf5ef2aSThomas Huth #define VROTATE(suffix, element, mask)                                  \
1696fcf5ef2aSThomas Huth     void helper_vrl##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)   \
1697fcf5ef2aSThomas Huth     {                                                                   \
1698fcf5ef2aSThomas Huth         int i;                                                          \
1699fcf5ef2aSThomas Huth                                                                         \
1700fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1701fcf5ef2aSThomas Huth             unsigned int shift = b->element[i] & mask;                  \
1702fcf5ef2aSThomas Huth             r->element[i] = (a->element[i] << shift) |                  \
1703fcf5ef2aSThomas Huth                 (a->element[i] >> (sizeof(a->element[0]) * 8 - shift)); \
1704fcf5ef2aSThomas Huth         }                                                               \
1705fcf5ef2aSThomas Huth     }
1706fcf5ef2aSThomas Huth VROTATE(b, u8, 0x7)
1707fcf5ef2aSThomas Huth VROTATE(h, u16, 0xF)
1708fcf5ef2aSThomas Huth VROTATE(w, u32, 0x1F)
1709fcf5ef2aSThomas Huth VROTATE(d, u64, 0x3F)
1710fcf5ef2aSThomas Huth #undef VROTATE
1711fcf5ef2aSThomas Huth 
1712fcf5ef2aSThomas Huth void helper_vrsqrtefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1713fcf5ef2aSThomas Huth {
1714fcf5ef2aSThomas Huth     int i;
1715fcf5ef2aSThomas Huth 
1716fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->f); i++) {
1717fcf5ef2aSThomas Huth         float32 t = float32_sqrt(b->f[i], &env->vec_status);
1718fcf5ef2aSThomas Huth 
1719fcf5ef2aSThomas Huth         r->f[i] = float32_div(float32_one, t, &env->vec_status);
1720fcf5ef2aSThomas Huth     }
1721fcf5ef2aSThomas Huth }
1722fcf5ef2aSThomas Huth 
1723fcf5ef2aSThomas Huth #define VRLMI(name, size, element, insert)                            \
1724fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)          \
1725fcf5ef2aSThomas Huth {                                                                     \
1726fcf5ef2aSThomas Huth     int i;                                                            \
1727fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->element); i++) {                    \
1728fcf5ef2aSThomas Huth         uint##size##_t src1 = a->element[i];                          \
1729fcf5ef2aSThomas Huth         uint##size##_t src2 = b->element[i];                          \
1730fcf5ef2aSThomas Huth         uint##size##_t src3 = r->element[i];                          \
1731fcf5ef2aSThomas Huth         uint##size##_t begin, end, shift, mask, rot_val;              \
1732fcf5ef2aSThomas Huth                                                                       \
1733fcf5ef2aSThomas Huth         shift = extract##size(src2, 0, 6);                            \
1734fcf5ef2aSThomas Huth         end   = extract##size(src2, 8, 6);                            \
1735fcf5ef2aSThomas Huth         begin = extract##size(src2, 16, 6);                           \
1736fcf5ef2aSThomas Huth         rot_val = rol##size(src1, shift);                             \
1737fcf5ef2aSThomas Huth         mask = mask_u##size(begin, end);                              \
1738fcf5ef2aSThomas Huth         if (insert) {                                                 \
1739fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask) | (src3 & ~mask);        \
1740fcf5ef2aSThomas Huth         } else {                                                      \
1741fcf5ef2aSThomas Huth             r->element[i] = (rot_val & mask);                         \
1742fcf5ef2aSThomas Huth         }                                                             \
1743fcf5ef2aSThomas Huth     }                                                                 \
1744fcf5ef2aSThomas Huth }
1745fcf5ef2aSThomas Huth 
1746fcf5ef2aSThomas Huth VRLMI(vrldmi, 64, u64, 1);
1747fcf5ef2aSThomas Huth VRLMI(vrlwmi, 32, u32, 1);
1748fcf5ef2aSThomas Huth VRLMI(vrldnm, 64, u64, 0);
1749fcf5ef2aSThomas Huth VRLMI(vrlwnm, 32, u32, 0);
1750fcf5ef2aSThomas Huth 
1751fcf5ef2aSThomas Huth void helper_vsel(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b,
1752fcf5ef2aSThomas Huth                  ppc_avr_t *c)
1753fcf5ef2aSThomas Huth {
1754fcf5ef2aSThomas Huth     r->u64[0] = (a->u64[0] & ~c->u64[0]) | (b->u64[0] & c->u64[0]);
1755fcf5ef2aSThomas Huth     r->u64[1] = (a->u64[1] & ~c->u64[1]) | (b->u64[1] & c->u64[1]);
1756fcf5ef2aSThomas Huth }
1757fcf5ef2aSThomas Huth 
1758fcf5ef2aSThomas Huth void helper_vexptefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1759fcf5ef2aSThomas Huth {
1760fcf5ef2aSThomas Huth     int i;
1761fcf5ef2aSThomas Huth 
1762fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->f); i++) {
1763fcf5ef2aSThomas Huth         r->f[i] = float32_exp2(b->f[i], &env->vec_status);
1764fcf5ef2aSThomas Huth     }
1765fcf5ef2aSThomas Huth }
1766fcf5ef2aSThomas Huth 
1767fcf5ef2aSThomas Huth void helper_vlogefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
1768fcf5ef2aSThomas Huth {
1769fcf5ef2aSThomas Huth     int i;
1770fcf5ef2aSThomas Huth 
1771fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->f); i++) {
1772fcf5ef2aSThomas Huth         r->f[i] = float32_log2(b->f[i], &env->vec_status);
1773fcf5ef2aSThomas Huth     }
1774fcf5ef2aSThomas Huth }
1775fcf5ef2aSThomas Huth 
177660caf221SAvinesh Kumar #if defined(HOST_WORDS_BIGENDIAN)
177760caf221SAvinesh Kumar #define VEXTU_X_DO(name, size, left)                                \
177860caf221SAvinesh Kumar     target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
177960caf221SAvinesh Kumar     {                                                               \
178060caf221SAvinesh Kumar         int index;                                                  \
178160caf221SAvinesh Kumar         if (left) {                                                 \
178260caf221SAvinesh Kumar             index = (a & 0xf) * 8;                                  \
178360caf221SAvinesh Kumar         } else {                                                    \
178460caf221SAvinesh Kumar             index = ((15 - (a & 0xf) + 1) * 8) - size;              \
178560caf221SAvinesh Kumar         }                                                           \
178660caf221SAvinesh Kumar         return int128_getlo(int128_rshift(b->s128, index)) &        \
178760caf221SAvinesh Kumar             MAKE_64BIT_MASK(0, size);                               \
178860caf221SAvinesh Kumar     }
178960caf221SAvinesh Kumar #else
179060caf221SAvinesh Kumar #define VEXTU_X_DO(name, size, left)                                \
179160caf221SAvinesh Kumar     target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
179260caf221SAvinesh Kumar     {                                                               \
179360caf221SAvinesh Kumar         int index;                                                  \
179460caf221SAvinesh Kumar         if (left) {                                                 \
179560caf221SAvinesh Kumar             index = ((15 - (a & 0xf) + 1) * 8) - size;              \
179660caf221SAvinesh Kumar         } else {                                                    \
179760caf221SAvinesh Kumar             index = (a & 0xf) * 8;                                  \
179860caf221SAvinesh Kumar         }                                                           \
179960caf221SAvinesh Kumar         return int128_getlo(int128_rshift(b->s128, index)) &        \
180060caf221SAvinesh Kumar             MAKE_64BIT_MASK(0, size);                               \
180160caf221SAvinesh Kumar     }
180260caf221SAvinesh Kumar #endif
180360caf221SAvinesh Kumar 
180460caf221SAvinesh Kumar VEXTU_X_DO(vextublx,  8, 1)
180560caf221SAvinesh Kumar VEXTU_X_DO(vextuhlx, 16, 1)
180660caf221SAvinesh Kumar VEXTU_X_DO(vextuwlx, 32, 1)
180760caf221SAvinesh Kumar VEXTU_X_DO(vextubrx,  8, 0)
180860caf221SAvinesh Kumar VEXTU_X_DO(vextuhrx, 16, 0)
180960caf221SAvinesh Kumar VEXTU_X_DO(vextuwrx, 32, 0)
181060caf221SAvinesh Kumar #undef VEXTU_X_DO
181160caf221SAvinesh Kumar 
1812fcf5ef2aSThomas Huth /* The specification says that the results are undefined if all of the
1813fcf5ef2aSThomas Huth  * shift counts are not identical.  We check to make sure that they are
1814fcf5ef2aSThomas Huth  * to conform to what real hardware appears to do.  */
1815fcf5ef2aSThomas Huth #define VSHIFT(suffix, leftp)                                           \
1816fcf5ef2aSThomas Huth     void helper_vs##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)    \
1817fcf5ef2aSThomas Huth     {                                                                   \
1818fcf5ef2aSThomas Huth         int shift = b->u8[LO_IDX*15] & 0x7;                             \
1819fcf5ef2aSThomas Huth         int doit = 1;                                                   \
1820fcf5ef2aSThomas Huth         int i;                                                          \
1821fcf5ef2aSThomas Huth                                                                         \
1822fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->u8); i++) {                       \
1823fcf5ef2aSThomas Huth             doit = doit && ((b->u8[i] & 0x7) == shift);                 \
1824fcf5ef2aSThomas Huth         }                                                               \
1825fcf5ef2aSThomas Huth         if (doit) {                                                     \
1826fcf5ef2aSThomas Huth             if (shift == 0) {                                           \
1827fcf5ef2aSThomas Huth                 *r = *a;                                                \
1828fcf5ef2aSThomas Huth             } else if (leftp) {                                         \
1829fcf5ef2aSThomas Huth                 uint64_t carry = a->u64[LO_IDX] >> (64 - shift);        \
1830fcf5ef2aSThomas Huth                                                                         \
1831fcf5ef2aSThomas Huth                 r->u64[HI_IDX] = (a->u64[HI_IDX] << shift) | carry;     \
1832fcf5ef2aSThomas Huth                 r->u64[LO_IDX] = a->u64[LO_IDX] << shift;               \
1833fcf5ef2aSThomas Huth             } else {                                                    \
1834fcf5ef2aSThomas Huth                 uint64_t carry = a->u64[HI_IDX] << (64 - shift);        \
1835fcf5ef2aSThomas Huth                                                                         \
1836fcf5ef2aSThomas Huth                 r->u64[LO_IDX] = (a->u64[LO_IDX] >> shift) | carry;     \
1837fcf5ef2aSThomas Huth                 r->u64[HI_IDX] = a->u64[HI_IDX] >> shift;               \
1838fcf5ef2aSThomas Huth             }                                                           \
1839fcf5ef2aSThomas Huth         }                                                               \
1840fcf5ef2aSThomas Huth     }
1841fcf5ef2aSThomas Huth VSHIFT(l, 1)
1842fcf5ef2aSThomas Huth VSHIFT(r, 0)
1843fcf5ef2aSThomas Huth #undef VSHIFT
1844fcf5ef2aSThomas Huth 
1845fcf5ef2aSThomas Huth #define VSL(suffix, element, mask)                                      \
1846fcf5ef2aSThomas Huth     void helper_vsl##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)   \
1847fcf5ef2aSThomas Huth     {                                                                   \
1848fcf5ef2aSThomas Huth         int i;                                                          \
1849fcf5ef2aSThomas Huth                                                                         \
1850fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1851fcf5ef2aSThomas Huth             unsigned int shift = b->element[i] & mask;                  \
1852fcf5ef2aSThomas Huth                                                                         \
1853fcf5ef2aSThomas Huth             r->element[i] = a->element[i] << shift;                     \
1854fcf5ef2aSThomas Huth         }                                                               \
1855fcf5ef2aSThomas Huth     }
1856fcf5ef2aSThomas Huth VSL(b, u8, 0x7)
1857fcf5ef2aSThomas Huth VSL(h, u16, 0x0F)
1858fcf5ef2aSThomas Huth VSL(w, u32, 0x1F)
1859fcf5ef2aSThomas Huth VSL(d, u64, 0x3F)
1860fcf5ef2aSThomas Huth #undef VSL
1861fcf5ef2aSThomas Huth 
1862fcf5ef2aSThomas Huth void helper_vslv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1863fcf5ef2aSThomas Huth {
1864fcf5ef2aSThomas Huth     int i;
1865fcf5ef2aSThomas Huth     unsigned int shift, bytes, size;
1866fcf5ef2aSThomas Huth 
1867fcf5ef2aSThomas Huth     size = ARRAY_SIZE(r->u8);
1868fcf5ef2aSThomas Huth     for (i = 0; i < size; i++) {
1869fcf5ef2aSThomas Huth         shift = b->u8[i] & 0x7;             /* extract shift value */
1870fcf5ef2aSThomas Huth         bytes = (a->u8[i] << 8) +             /* extract adjacent bytes */
1871fcf5ef2aSThomas Huth             (((i + 1) < size) ? a->u8[i + 1] : 0);
1872fcf5ef2aSThomas Huth         r->u8[i] = (bytes << shift) >> 8;   /* shift and store result */
1873fcf5ef2aSThomas Huth     }
1874fcf5ef2aSThomas Huth }
1875fcf5ef2aSThomas Huth 
1876fcf5ef2aSThomas Huth void helper_vsrv(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1877fcf5ef2aSThomas Huth {
1878fcf5ef2aSThomas Huth     int i;
1879fcf5ef2aSThomas Huth     unsigned int shift, bytes;
1880fcf5ef2aSThomas Huth 
1881fcf5ef2aSThomas Huth     /* Use reverse order, as destination and source register can be same. Its
1882fcf5ef2aSThomas Huth      * being modified in place saving temporary, reverse order will guarantee
1883fcf5ef2aSThomas Huth      * that computed result is not fed back.
1884fcf5ef2aSThomas Huth      */
1885fcf5ef2aSThomas Huth     for (i = ARRAY_SIZE(r->u8) - 1; i >= 0; i--) {
1886fcf5ef2aSThomas Huth         shift = b->u8[i] & 0x7;                 /* extract shift value */
1887fcf5ef2aSThomas Huth         bytes = ((i ? a->u8[i - 1] : 0) << 8) + a->u8[i];
1888fcf5ef2aSThomas Huth                                                 /* extract adjacent bytes */
1889fcf5ef2aSThomas Huth         r->u8[i] = (bytes >> shift) & 0xFF;     /* shift and store result */
1890fcf5ef2aSThomas Huth     }
1891fcf5ef2aSThomas Huth }
1892fcf5ef2aSThomas Huth 
1893fcf5ef2aSThomas Huth void helper_vsldoi(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t shift)
1894fcf5ef2aSThomas Huth {
1895fcf5ef2aSThomas Huth     int sh = shift & 0xf;
1896fcf5ef2aSThomas Huth     int i;
1897fcf5ef2aSThomas Huth     ppc_avr_t result;
1898fcf5ef2aSThomas Huth 
1899fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1900fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1901fcf5ef2aSThomas Huth         int index = sh + i;
1902fcf5ef2aSThomas Huth         if (index > 0xf) {
1903fcf5ef2aSThomas Huth             result.u8[i] = b->u8[index - 0x10];
1904fcf5ef2aSThomas Huth         } else {
1905fcf5ef2aSThomas Huth             result.u8[i] = a->u8[index];
1906fcf5ef2aSThomas Huth         }
1907fcf5ef2aSThomas Huth     }
1908fcf5ef2aSThomas Huth #else
1909fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u8); i++) {
1910fcf5ef2aSThomas Huth         int index = (16 - sh) + i;
1911fcf5ef2aSThomas Huth         if (index > 0xf) {
1912fcf5ef2aSThomas Huth             result.u8[i] = a->u8[index - 0x10];
1913fcf5ef2aSThomas Huth         } else {
1914fcf5ef2aSThomas Huth             result.u8[i] = b->u8[index];
1915fcf5ef2aSThomas Huth         }
1916fcf5ef2aSThomas Huth     }
1917fcf5ef2aSThomas Huth #endif
1918fcf5ef2aSThomas Huth     *r = result;
1919fcf5ef2aSThomas Huth }
1920fcf5ef2aSThomas Huth 
1921fcf5ef2aSThomas Huth void helper_vslo(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
1922fcf5ef2aSThomas Huth {
1923fcf5ef2aSThomas Huth     int sh = (b->u8[LO_IDX*0xf] >> 3) & 0xf;
1924fcf5ef2aSThomas Huth 
1925fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1926fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
1927fcf5ef2aSThomas Huth     memset(&r->u8[16-sh], 0, sh);
1928fcf5ef2aSThomas Huth #else
1929fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
1930fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
1931fcf5ef2aSThomas Huth #endif
1932fcf5ef2aSThomas Huth }
1933fcf5ef2aSThomas Huth 
1934fcf5ef2aSThomas Huth /* Experimental testing shows that hardware masks the immediate.  */
1935fcf5ef2aSThomas Huth #define _SPLAT_MASKED(element) (splat & (ARRAY_SIZE(r->element) - 1))
1936fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1937fcf5ef2aSThomas Huth #define SPLAT_ELEMENT(element) _SPLAT_MASKED(element)
1938fcf5ef2aSThomas Huth #else
1939fcf5ef2aSThomas Huth #define SPLAT_ELEMENT(element)                                  \
1940fcf5ef2aSThomas Huth     (ARRAY_SIZE(r->element) - 1 - _SPLAT_MASKED(element))
1941fcf5ef2aSThomas Huth #endif
1942fcf5ef2aSThomas Huth #define VSPLT(suffix, element)                                          \
1943fcf5ef2aSThomas Huth     void helper_vsplt##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t splat) \
1944fcf5ef2aSThomas Huth     {                                                                   \
1945fcf5ef2aSThomas Huth         uint32_t s = b->element[SPLAT_ELEMENT(element)];                \
1946fcf5ef2aSThomas Huth         int i;                                                          \
1947fcf5ef2aSThomas Huth                                                                         \
1948fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
1949fcf5ef2aSThomas Huth             r->element[i] = s;                                          \
1950fcf5ef2aSThomas Huth         }                                                               \
1951fcf5ef2aSThomas Huth     }
1952fcf5ef2aSThomas Huth VSPLT(b, u8)
1953fcf5ef2aSThomas Huth VSPLT(h, u16)
1954fcf5ef2aSThomas Huth VSPLT(w, u32)
1955fcf5ef2aSThomas Huth #undef VSPLT
1956fcf5ef2aSThomas Huth #undef SPLAT_ELEMENT
1957fcf5ef2aSThomas Huth #undef _SPLAT_MASKED
1958fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1959fcf5ef2aSThomas Huth #define VINSERT(suffix, element)                                            \
1960fcf5ef2aSThomas Huth     void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1961fcf5ef2aSThomas Huth     {                                                                       \
1962fcf5ef2aSThomas Huth         memmove(&r->u8[index], &b->u8[8 - sizeof(r->element)],              \
1963fcf5ef2aSThomas Huth                sizeof(r->element[0]));                                      \
1964fcf5ef2aSThomas Huth     }
1965fcf5ef2aSThomas Huth #else
1966fcf5ef2aSThomas Huth #define VINSERT(suffix, element)                                            \
1967fcf5ef2aSThomas Huth     void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1968fcf5ef2aSThomas Huth     {                                                                       \
1969fcf5ef2aSThomas Huth         uint32_t d = (16 - index) - sizeof(r->element[0]);                  \
1970fcf5ef2aSThomas Huth         memmove(&r->u8[d], &b->u8[8], sizeof(r->element[0]));               \
1971fcf5ef2aSThomas Huth     }
1972fcf5ef2aSThomas Huth #endif
1973fcf5ef2aSThomas Huth VINSERT(b, u8)
1974fcf5ef2aSThomas Huth VINSERT(h, u16)
1975fcf5ef2aSThomas Huth VINSERT(w, u32)
1976fcf5ef2aSThomas Huth VINSERT(d, u64)
1977fcf5ef2aSThomas Huth #undef VINSERT
1978fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
1979fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1980fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1981fcf5ef2aSThomas Huth     {                                                                        \
1982fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1983fcf5ef2aSThomas Huth         memmove(&r->u8[8 - es], &b->u8[index], es);                          \
1984fcf5ef2aSThomas Huth         memset(&r->u8[8], 0, 8);                                             \
1985fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8 - es);                                        \
1986fcf5ef2aSThomas Huth     }
1987fcf5ef2aSThomas Huth #else
1988fcf5ef2aSThomas Huth #define VEXTRACT(suffix, element)                                            \
1989fcf5ef2aSThomas Huth     void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1990fcf5ef2aSThomas Huth     {                                                                        \
1991fcf5ef2aSThomas Huth         uint32_t es = sizeof(r->element[0]);                                 \
1992fcf5ef2aSThomas Huth         uint32_t s = (16 - index) - es;                                      \
1993fcf5ef2aSThomas Huth         memmove(&r->u8[8], &b->u8[s], es);                                   \
1994fcf5ef2aSThomas Huth         memset(&r->u8[0], 0, 8);                                             \
1995fcf5ef2aSThomas Huth         memset(&r->u8[8 + es], 0, 8 - es);                                   \
1996fcf5ef2aSThomas Huth     }
1997fcf5ef2aSThomas Huth #endif
1998fcf5ef2aSThomas Huth VEXTRACT(ub, u8)
1999fcf5ef2aSThomas Huth VEXTRACT(uh, u16)
2000fcf5ef2aSThomas Huth VEXTRACT(uw, u32)
2001fcf5ef2aSThomas Huth VEXTRACT(d, u64)
2002fcf5ef2aSThomas Huth #undef VEXTRACT
2003fcf5ef2aSThomas Huth 
20048ad901e5SNikunj A Dadhania void helper_xxextractuw(CPUPPCState *env, target_ulong xtn,
20058ad901e5SNikunj A Dadhania                         target_ulong xbn, uint32_t index)
20068ad901e5SNikunj A Dadhania {
20078ad901e5SNikunj A Dadhania     ppc_vsr_t xt, xb;
20088ad901e5SNikunj A Dadhania     size_t es = sizeof(uint32_t);
20098ad901e5SNikunj A Dadhania     uint32_t ext_index;
20108ad901e5SNikunj A Dadhania     int i;
20118ad901e5SNikunj A Dadhania 
20128ad901e5SNikunj A Dadhania     getVSR(xbn, &xb, env);
20138ad901e5SNikunj A Dadhania     memset(&xt, 0, sizeof(xt));
20148ad901e5SNikunj A Dadhania 
20158ad901e5SNikunj A Dadhania #if defined(HOST_WORDS_BIGENDIAN)
20168ad901e5SNikunj A Dadhania     ext_index = index;
20178ad901e5SNikunj A Dadhania     for (i = 0; i < es; i++, ext_index++) {
20188ad901e5SNikunj A Dadhania         xt.u8[8 - es + i] = xb.u8[ext_index % 16];
20198ad901e5SNikunj A Dadhania     }
20208ad901e5SNikunj A Dadhania #else
20218ad901e5SNikunj A Dadhania     ext_index = 15 - index;
20228ad901e5SNikunj A Dadhania     for (i = es - 1; i >= 0; i--, ext_index--) {
20238ad901e5SNikunj A Dadhania         xt.u8[8 + i] = xb.u8[ext_index % 16];
20248ad901e5SNikunj A Dadhania     }
20258ad901e5SNikunj A Dadhania #endif
20268ad901e5SNikunj A Dadhania 
20278ad901e5SNikunj A Dadhania     putVSR(xtn, &xt, env);
20288ad901e5SNikunj A Dadhania }
20298ad901e5SNikunj A Dadhania 
20303398b742SNikunj A Dadhania void helper_xxinsertw(CPUPPCState *env, target_ulong xtn,
20313398b742SNikunj A Dadhania                       target_ulong xbn, uint32_t index)
20323398b742SNikunj A Dadhania {
20333398b742SNikunj A Dadhania     ppc_vsr_t xt, xb;
20343398b742SNikunj A Dadhania     size_t es = sizeof(uint32_t);
20353398b742SNikunj A Dadhania     int ins_index, i = 0;
20363398b742SNikunj A Dadhania 
20373398b742SNikunj A Dadhania     getVSR(xbn, &xb, env);
20383398b742SNikunj A Dadhania     getVSR(xtn, &xt, env);
20393398b742SNikunj A Dadhania 
20403398b742SNikunj A Dadhania #if defined(HOST_WORDS_BIGENDIAN)
20413398b742SNikunj A Dadhania     ins_index = index;
20423398b742SNikunj A Dadhania     for (i = 0; i < es && ins_index < 16; i++, ins_index++) {
20433398b742SNikunj A Dadhania         xt.u8[ins_index] = xb.u8[8 - es + i];
20443398b742SNikunj A Dadhania     }
20453398b742SNikunj A Dadhania #else
20463398b742SNikunj A Dadhania     ins_index = 15 - index;
20473398b742SNikunj A Dadhania     for (i = es - 1; i >= 0 && ins_index >= 0; i--, ins_index--) {
20483398b742SNikunj A Dadhania         xt.u8[ins_index] = xb.u8[8 + i];
20493398b742SNikunj A Dadhania     }
20503398b742SNikunj A Dadhania #endif
20513398b742SNikunj A Dadhania 
20523398b742SNikunj A Dadhania     putVSR(xtn, &xt, env);
20533398b742SNikunj A Dadhania }
20543398b742SNikunj A Dadhania 
2055fcf5ef2aSThomas Huth #define VEXT_SIGNED(name, element, mask, cast, recast)              \
2056fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *b)                      \
2057fcf5ef2aSThomas Huth {                                                                   \
2058fcf5ef2aSThomas Huth     int i;                                                          \
2059fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, element) {                              \
2060fcf5ef2aSThomas Huth         r->element[i] = (recast)((cast)(b->element[i] & mask));     \
2061fcf5ef2aSThomas Huth     }                                                               \
2062fcf5ef2aSThomas Huth }
2063fcf5ef2aSThomas Huth VEXT_SIGNED(vextsb2w, s32, UINT8_MAX, int8_t, int32_t)
2064fcf5ef2aSThomas Huth VEXT_SIGNED(vextsb2d, s64, UINT8_MAX, int8_t, int64_t)
2065fcf5ef2aSThomas Huth VEXT_SIGNED(vextsh2w, s32, UINT16_MAX, int16_t, int32_t)
2066fcf5ef2aSThomas Huth VEXT_SIGNED(vextsh2d, s64, UINT16_MAX, int16_t, int64_t)
2067fcf5ef2aSThomas Huth VEXT_SIGNED(vextsw2d, s64, UINT32_MAX, int32_t, int64_t)
2068fcf5ef2aSThomas Huth #undef VEXT_SIGNED
2069fcf5ef2aSThomas Huth 
2070fcf5ef2aSThomas Huth #define VNEG(name, element)                                         \
2071fcf5ef2aSThomas Huth void helper_##name(ppc_avr_t *r, ppc_avr_t *b)                      \
2072fcf5ef2aSThomas Huth {                                                                   \
2073fcf5ef2aSThomas Huth     int i;                                                          \
2074fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, element) {                              \
2075fcf5ef2aSThomas Huth         r->element[i] = -b->element[i];                             \
2076fcf5ef2aSThomas Huth     }                                                               \
2077fcf5ef2aSThomas Huth }
2078fcf5ef2aSThomas Huth VNEG(vnegw, s32)
2079fcf5ef2aSThomas Huth VNEG(vnegd, s64)
2080fcf5ef2aSThomas Huth #undef VNEG
2081fcf5ef2aSThomas Huth 
2082fcf5ef2aSThomas Huth #define VSPLTI(suffix, element, splat_type)                     \
2083fcf5ef2aSThomas Huth     void helper_vspltis##suffix(ppc_avr_t *r, uint32_t splat)   \
2084fcf5ef2aSThomas Huth     {                                                           \
2085fcf5ef2aSThomas Huth         splat_type x = (int8_t)(splat << 3) >> 3;               \
2086fcf5ef2aSThomas Huth         int i;                                                  \
2087fcf5ef2aSThomas Huth                                                                 \
2088fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {          \
2089fcf5ef2aSThomas Huth             r->element[i] = x;                                  \
2090fcf5ef2aSThomas Huth         }                                                       \
2091fcf5ef2aSThomas Huth     }
2092fcf5ef2aSThomas Huth VSPLTI(b, s8, int8_t)
2093fcf5ef2aSThomas Huth VSPLTI(h, s16, int16_t)
2094fcf5ef2aSThomas Huth VSPLTI(w, s32, int32_t)
2095fcf5ef2aSThomas Huth #undef VSPLTI
2096fcf5ef2aSThomas Huth 
2097fcf5ef2aSThomas Huth #define VSR(suffix, element, mask)                                      \
2098fcf5ef2aSThomas Huth     void helper_vsr##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)   \
2099fcf5ef2aSThomas Huth     {                                                                   \
2100fcf5ef2aSThomas Huth         int i;                                                          \
2101fcf5ef2aSThomas Huth                                                                         \
2102fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->element); i++) {                  \
2103fcf5ef2aSThomas Huth             unsigned int shift = b->element[i] & mask;                  \
2104fcf5ef2aSThomas Huth             r->element[i] = a->element[i] >> shift;                     \
2105fcf5ef2aSThomas Huth         }                                                               \
2106fcf5ef2aSThomas Huth     }
2107fcf5ef2aSThomas Huth VSR(ab, s8, 0x7)
2108fcf5ef2aSThomas Huth VSR(ah, s16, 0xF)
2109fcf5ef2aSThomas Huth VSR(aw, s32, 0x1F)
2110fcf5ef2aSThomas Huth VSR(ad, s64, 0x3F)
2111fcf5ef2aSThomas Huth VSR(b, u8, 0x7)
2112fcf5ef2aSThomas Huth VSR(h, u16, 0xF)
2113fcf5ef2aSThomas Huth VSR(w, u32, 0x1F)
2114fcf5ef2aSThomas Huth VSR(d, u64, 0x3F)
2115fcf5ef2aSThomas Huth #undef VSR
2116fcf5ef2aSThomas Huth 
2117fcf5ef2aSThomas Huth void helper_vsro(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2118fcf5ef2aSThomas Huth {
2119fcf5ef2aSThomas Huth     int sh = (b->u8[LO_IDX * 0xf] >> 3) & 0xf;
2120fcf5ef2aSThomas Huth 
2121fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2122fcf5ef2aSThomas Huth     memmove(&r->u8[sh], &a->u8[0], 16 - sh);
2123fcf5ef2aSThomas Huth     memset(&r->u8[0], 0, sh);
2124fcf5ef2aSThomas Huth #else
2125fcf5ef2aSThomas Huth     memmove(&r->u8[0], &a->u8[sh], 16 - sh);
2126fcf5ef2aSThomas Huth     memset(&r->u8[16 - sh], 0, sh);
2127fcf5ef2aSThomas Huth #endif
2128fcf5ef2aSThomas Huth }
2129fcf5ef2aSThomas Huth 
2130fcf5ef2aSThomas Huth void helper_vsubcuw(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2131fcf5ef2aSThomas Huth {
2132fcf5ef2aSThomas Huth     int i;
2133fcf5ef2aSThomas Huth 
2134fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2135fcf5ef2aSThomas Huth         r->u32[i] = a->u32[i] >= b->u32[i];
2136fcf5ef2aSThomas Huth     }
2137fcf5ef2aSThomas Huth }
2138fcf5ef2aSThomas Huth 
2139fcf5ef2aSThomas Huth void helper_vsumsws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2140fcf5ef2aSThomas Huth {
2141fcf5ef2aSThomas Huth     int64_t t;
2142fcf5ef2aSThomas Huth     int i, upper;
2143fcf5ef2aSThomas Huth     ppc_avr_t result;
2144fcf5ef2aSThomas Huth     int sat = 0;
2145fcf5ef2aSThomas Huth 
2146fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2147fcf5ef2aSThomas Huth     upper = ARRAY_SIZE(r->s32)-1;
2148fcf5ef2aSThomas Huth #else
2149fcf5ef2aSThomas Huth     upper = 0;
2150fcf5ef2aSThomas Huth #endif
2151fcf5ef2aSThomas Huth     t = (int64_t)b->s32[upper];
2152fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2153fcf5ef2aSThomas Huth         t += a->s32[i];
2154fcf5ef2aSThomas Huth         result.s32[i] = 0;
2155fcf5ef2aSThomas Huth     }
2156fcf5ef2aSThomas Huth     result.s32[upper] = cvtsdsw(t, &sat);
2157fcf5ef2aSThomas Huth     *r = result;
2158fcf5ef2aSThomas Huth 
2159fcf5ef2aSThomas Huth     if (sat) {
2160fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
2161fcf5ef2aSThomas Huth     }
2162fcf5ef2aSThomas Huth }
2163fcf5ef2aSThomas Huth 
2164fcf5ef2aSThomas Huth void helper_vsum2sws(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2165fcf5ef2aSThomas Huth {
2166fcf5ef2aSThomas Huth     int i, j, upper;
2167fcf5ef2aSThomas Huth     ppc_avr_t result;
2168fcf5ef2aSThomas Huth     int sat = 0;
2169fcf5ef2aSThomas Huth 
2170fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2171fcf5ef2aSThomas Huth     upper = 1;
2172fcf5ef2aSThomas Huth #else
2173fcf5ef2aSThomas Huth     upper = 0;
2174fcf5ef2aSThomas Huth #endif
2175fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u64); i++) {
2176fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[upper + i * 2];
2177fcf5ef2aSThomas Huth 
2178fcf5ef2aSThomas Huth         result.u64[i] = 0;
2179fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u64); j++) {
2180fcf5ef2aSThomas Huth             t += a->s32[2 * i + j];
2181fcf5ef2aSThomas Huth         }
2182fcf5ef2aSThomas Huth         result.s32[upper + i * 2] = cvtsdsw(t, &sat);
2183fcf5ef2aSThomas Huth     }
2184fcf5ef2aSThomas Huth 
2185fcf5ef2aSThomas Huth     *r = result;
2186fcf5ef2aSThomas Huth     if (sat) {
2187fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
2188fcf5ef2aSThomas Huth     }
2189fcf5ef2aSThomas Huth }
2190fcf5ef2aSThomas Huth 
2191fcf5ef2aSThomas Huth void helper_vsum4sbs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2192fcf5ef2aSThomas Huth {
2193fcf5ef2aSThomas Huth     int i, j;
2194fcf5ef2aSThomas Huth     int sat = 0;
2195fcf5ef2aSThomas Huth 
2196fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2197fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
2198fcf5ef2aSThomas Huth 
2199fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->s32); j++) {
2200fcf5ef2aSThomas Huth             t += a->s8[4 * i + j];
2201fcf5ef2aSThomas Huth         }
2202fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
2203fcf5ef2aSThomas Huth     }
2204fcf5ef2aSThomas Huth 
2205fcf5ef2aSThomas Huth     if (sat) {
2206fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
2207fcf5ef2aSThomas Huth     }
2208fcf5ef2aSThomas Huth }
2209fcf5ef2aSThomas Huth 
2210fcf5ef2aSThomas Huth void helper_vsum4shs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2211fcf5ef2aSThomas Huth {
2212fcf5ef2aSThomas Huth     int sat = 0;
2213fcf5ef2aSThomas Huth     int i;
2214fcf5ef2aSThomas Huth 
2215fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->s32); i++) {
2216fcf5ef2aSThomas Huth         int64_t t = (int64_t)b->s32[i];
2217fcf5ef2aSThomas Huth 
2218fcf5ef2aSThomas Huth         t += a->s16[2 * i] + a->s16[2 * i + 1];
2219fcf5ef2aSThomas Huth         r->s32[i] = cvtsdsw(t, &sat);
2220fcf5ef2aSThomas Huth     }
2221fcf5ef2aSThomas Huth 
2222fcf5ef2aSThomas Huth     if (sat) {
2223fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
2224fcf5ef2aSThomas Huth     }
2225fcf5ef2aSThomas Huth }
2226fcf5ef2aSThomas Huth 
2227fcf5ef2aSThomas Huth void helper_vsum4ubs(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2228fcf5ef2aSThomas Huth {
2229fcf5ef2aSThomas Huth     int i, j;
2230fcf5ef2aSThomas Huth     int sat = 0;
2231fcf5ef2aSThomas Huth 
2232fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(r->u32); i++) {
2233fcf5ef2aSThomas Huth         uint64_t t = (uint64_t)b->u32[i];
2234fcf5ef2aSThomas Huth 
2235fcf5ef2aSThomas Huth         for (j = 0; j < ARRAY_SIZE(r->u32); j++) {
2236fcf5ef2aSThomas Huth             t += a->u8[4 * i + j];
2237fcf5ef2aSThomas Huth         }
2238fcf5ef2aSThomas Huth         r->u32[i] = cvtuduw(t, &sat);
2239fcf5ef2aSThomas Huth     }
2240fcf5ef2aSThomas Huth 
2241fcf5ef2aSThomas Huth     if (sat) {
2242fcf5ef2aSThomas Huth         env->vscr |= (1 << VSCR_SAT);
2243fcf5ef2aSThomas Huth     }
2244fcf5ef2aSThomas Huth }
2245fcf5ef2aSThomas Huth 
2246fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2247fcf5ef2aSThomas Huth #define UPKHI 1
2248fcf5ef2aSThomas Huth #define UPKLO 0
2249fcf5ef2aSThomas Huth #else
2250fcf5ef2aSThomas Huth #define UPKHI 0
2251fcf5ef2aSThomas Huth #define UPKLO 1
2252fcf5ef2aSThomas Huth #endif
2253fcf5ef2aSThomas Huth #define VUPKPX(suffix, hi)                                              \
2254fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
2255fcf5ef2aSThomas Huth     {                                                                   \
2256fcf5ef2aSThomas Huth         int i;                                                          \
2257fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
2258fcf5ef2aSThomas Huth                                                                         \
2259fcf5ef2aSThomas Huth         for (i = 0; i < ARRAY_SIZE(r->u32); i++) {                      \
2260fcf5ef2aSThomas Huth             uint16_t e = b->u16[hi ? i : i+4];                          \
2261fcf5ef2aSThomas Huth             uint8_t a = (e >> 15) ? 0xff : 0;                           \
2262fcf5ef2aSThomas Huth             uint8_t r = (e >> 10) & 0x1f;                               \
2263fcf5ef2aSThomas Huth             uint8_t g = (e >> 5) & 0x1f;                                \
2264fcf5ef2aSThomas Huth             uint8_t b = e & 0x1f;                                       \
2265fcf5ef2aSThomas Huth                                                                         \
2266fcf5ef2aSThomas Huth             result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b;       \
2267fcf5ef2aSThomas Huth         }                                                               \
2268fcf5ef2aSThomas Huth         *r = result;                                                    \
2269fcf5ef2aSThomas Huth     }
2270fcf5ef2aSThomas Huth VUPKPX(lpx, UPKLO)
2271fcf5ef2aSThomas Huth VUPKPX(hpx, UPKHI)
2272fcf5ef2aSThomas Huth #undef VUPKPX
2273fcf5ef2aSThomas Huth 
2274fcf5ef2aSThomas Huth #define VUPK(suffix, unpacked, packee, hi)                              \
2275fcf5ef2aSThomas Huth     void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b)                \
2276fcf5ef2aSThomas Huth     {                                                                   \
2277fcf5ef2aSThomas Huth         int i;                                                          \
2278fcf5ef2aSThomas Huth         ppc_avr_t result;                                               \
2279fcf5ef2aSThomas Huth                                                                         \
2280fcf5ef2aSThomas Huth         if (hi) {                                                       \
2281fcf5ef2aSThomas Huth             for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) {             \
2282fcf5ef2aSThomas Huth                 result.unpacked[i] = b->packee[i];                      \
2283fcf5ef2aSThomas Huth             }                                                           \
2284fcf5ef2aSThomas Huth         } else {                                                        \
2285fcf5ef2aSThomas Huth             for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
2286fcf5ef2aSThomas Huth                  i++) {                                                 \
2287fcf5ef2aSThomas Huth                 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
2288fcf5ef2aSThomas Huth             }                                                           \
2289fcf5ef2aSThomas Huth         }                                                               \
2290fcf5ef2aSThomas Huth         *r = result;                                                    \
2291fcf5ef2aSThomas Huth     }
2292fcf5ef2aSThomas Huth VUPK(hsb, s16, s8, UPKHI)
2293fcf5ef2aSThomas Huth VUPK(hsh, s32, s16, UPKHI)
2294fcf5ef2aSThomas Huth VUPK(hsw, s64, s32, UPKHI)
2295fcf5ef2aSThomas Huth VUPK(lsb, s16, s8, UPKLO)
2296fcf5ef2aSThomas Huth VUPK(lsh, s32, s16, UPKLO)
2297fcf5ef2aSThomas Huth VUPK(lsw, s64, s32, UPKLO)
2298fcf5ef2aSThomas Huth #undef VUPK
2299fcf5ef2aSThomas Huth #undef UPKHI
2300fcf5ef2aSThomas Huth #undef UPKLO
2301fcf5ef2aSThomas Huth 
2302fcf5ef2aSThomas Huth #define VGENERIC_DO(name, element)                                      \
2303fcf5ef2aSThomas Huth     void helper_v##name(ppc_avr_t *r, ppc_avr_t *b)                     \
2304fcf5ef2aSThomas Huth     {                                                                   \
2305fcf5ef2aSThomas Huth         int i;                                                          \
2306fcf5ef2aSThomas Huth                                                                         \
2307fcf5ef2aSThomas Huth         VECTOR_FOR_INORDER_I(i, element) {                              \
2308fcf5ef2aSThomas Huth             r->element[i] = name(b->element[i]);                        \
2309fcf5ef2aSThomas Huth         }                                                               \
2310fcf5ef2aSThomas Huth     }
2311fcf5ef2aSThomas Huth 
2312fcf5ef2aSThomas Huth #define clzb(v) ((v) ? clz32((uint32_t)(v) << 24) : 8)
2313fcf5ef2aSThomas Huth #define clzh(v) ((v) ? clz32((uint32_t)(v) << 16) : 16)
2314fcf5ef2aSThomas Huth #define clzw(v) clz32((v))
2315fcf5ef2aSThomas Huth #define clzd(v) clz64((v))
2316fcf5ef2aSThomas Huth 
2317fcf5ef2aSThomas Huth VGENERIC_DO(clzb, u8)
2318fcf5ef2aSThomas Huth VGENERIC_DO(clzh, u16)
2319fcf5ef2aSThomas Huth VGENERIC_DO(clzw, u32)
2320fcf5ef2aSThomas Huth VGENERIC_DO(clzd, u64)
2321fcf5ef2aSThomas Huth 
2322fcf5ef2aSThomas Huth #undef clzb
2323fcf5ef2aSThomas Huth #undef clzh
2324fcf5ef2aSThomas Huth #undef clzw
2325fcf5ef2aSThomas Huth #undef clzd
2326fcf5ef2aSThomas Huth 
2327fcf5ef2aSThomas Huth #define ctzb(v) ((v) ? ctz32(v) : 8)
2328fcf5ef2aSThomas Huth #define ctzh(v) ((v) ? ctz32(v) : 16)
2329fcf5ef2aSThomas Huth #define ctzw(v) ctz32((v))
2330fcf5ef2aSThomas Huth #define ctzd(v) ctz64((v))
2331fcf5ef2aSThomas Huth 
2332fcf5ef2aSThomas Huth VGENERIC_DO(ctzb, u8)
2333fcf5ef2aSThomas Huth VGENERIC_DO(ctzh, u16)
2334fcf5ef2aSThomas Huth VGENERIC_DO(ctzw, u32)
2335fcf5ef2aSThomas Huth VGENERIC_DO(ctzd, u64)
2336fcf5ef2aSThomas Huth 
2337fcf5ef2aSThomas Huth #undef ctzb
2338fcf5ef2aSThomas Huth #undef ctzh
2339fcf5ef2aSThomas Huth #undef ctzw
2340fcf5ef2aSThomas Huth #undef ctzd
2341fcf5ef2aSThomas Huth 
2342fcf5ef2aSThomas Huth #define popcntb(v) ctpop8(v)
2343fcf5ef2aSThomas Huth #define popcnth(v) ctpop16(v)
2344fcf5ef2aSThomas Huth #define popcntw(v) ctpop32(v)
2345fcf5ef2aSThomas Huth #define popcntd(v) ctpop64(v)
2346fcf5ef2aSThomas Huth 
2347fcf5ef2aSThomas Huth VGENERIC_DO(popcntb, u8)
2348fcf5ef2aSThomas Huth VGENERIC_DO(popcnth, u16)
2349fcf5ef2aSThomas Huth VGENERIC_DO(popcntw, u32)
2350fcf5ef2aSThomas Huth VGENERIC_DO(popcntd, u64)
2351fcf5ef2aSThomas Huth 
2352fcf5ef2aSThomas Huth #undef popcntb
2353fcf5ef2aSThomas Huth #undef popcnth
2354fcf5ef2aSThomas Huth #undef popcntw
2355fcf5ef2aSThomas Huth #undef popcntd
2356fcf5ef2aSThomas Huth 
2357fcf5ef2aSThomas Huth #undef VGENERIC_DO
2358fcf5ef2aSThomas Huth 
2359fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2360fcf5ef2aSThomas Huth #define QW_ONE { .u64 = { 0, 1 } }
2361fcf5ef2aSThomas Huth #else
2362fcf5ef2aSThomas Huth #define QW_ONE { .u64 = { 1, 0 } }
2363fcf5ef2aSThomas Huth #endif
2364fcf5ef2aSThomas Huth 
2365fcf5ef2aSThomas Huth #ifndef CONFIG_INT128
2366fcf5ef2aSThomas Huth 
2367fcf5ef2aSThomas Huth static inline void avr_qw_not(ppc_avr_t *t, ppc_avr_t a)
2368fcf5ef2aSThomas Huth {
2369fcf5ef2aSThomas Huth     t->u64[0] = ~a.u64[0];
2370fcf5ef2aSThomas Huth     t->u64[1] = ~a.u64[1];
2371fcf5ef2aSThomas Huth }
2372fcf5ef2aSThomas Huth 
2373fcf5ef2aSThomas Huth static int avr_qw_cmpu(ppc_avr_t a, ppc_avr_t b)
2374fcf5ef2aSThomas Huth {
2375fcf5ef2aSThomas Huth     if (a.u64[HI_IDX] < b.u64[HI_IDX]) {
2376fcf5ef2aSThomas Huth         return -1;
2377fcf5ef2aSThomas Huth     } else if (a.u64[HI_IDX] > b.u64[HI_IDX]) {
2378fcf5ef2aSThomas Huth         return 1;
2379fcf5ef2aSThomas Huth     } else if (a.u64[LO_IDX] < b.u64[LO_IDX]) {
2380fcf5ef2aSThomas Huth         return -1;
2381fcf5ef2aSThomas Huth     } else if (a.u64[LO_IDX] > b.u64[LO_IDX]) {
2382fcf5ef2aSThomas Huth         return 1;
2383fcf5ef2aSThomas Huth     } else {
2384fcf5ef2aSThomas Huth         return 0;
2385fcf5ef2aSThomas Huth     }
2386fcf5ef2aSThomas Huth }
2387fcf5ef2aSThomas Huth 
2388fcf5ef2aSThomas Huth static void avr_qw_add(ppc_avr_t *t, ppc_avr_t a, ppc_avr_t b)
2389fcf5ef2aSThomas Huth {
2390fcf5ef2aSThomas Huth     t->u64[LO_IDX] = a.u64[LO_IDX] + b.u64[LO_IDX];
2391fcf5ef2aSThomas Huth     t->u64[HI_IDX] = a.u64[HI_IDX] + b.u64[HI_IDX] +
2392fcf5ef2aSThomas Huth                      (~a.u64[LO_IDX] < b.u64[LO_IDX]);
2393fcf5ef2aSThomas Huth }
2394fcf5ef2aSThomas Huth 
2395fcf5ef2aSThomas Huth static int avr_qw_addc(ppc_avr_t *t, ppc_avr_t a, ppc_avr_t b)
2396fcf5ef2aSThomas Huth {
2397fcf5ef2aSThomas Huth     ppc_avr_t not_a;
2398fcf5ef2aSThomas Huth     t->u64[LO_IDX] = a.u64[LO_IDX] + b.u64[LO_IDX];
2399fcf5ef2aSThomas Huth     t->u64[HI_IDX] = a.u64[HI_IDX] + b.u64[HI_IDX] +
2400fcf5ef2aSThomas Huth                      (~a.u64[LO_IDX] < b.u64[LO_IDX]);
2401fcf5ef2aSThomas Huth     avr_qw_not(&not_a, a);
2402fcf5ef2aSThomas Huth     return avr_qw_cmpu(not_a, b) < 0;
2403fcf5ef2aSThomas Huth }
2404fcf5ef2aSThomas Huth 
2405fcf5ef2aSThomas Huth #endif
2406fcf5ef2aSThomas Huth 
2407fcf5ef2aSThomas Huth void helper_vadduqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2408fcf5ef2aSThomas Huth {
2409fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2410fcf5ef2aSThomas Huth     r->u128 = a->u128 + b->u128;
2411fcf5ef2aSThomas Huth #else
2412fcf5ef2aSThomas Huth     avr_qw_add(r, *a, *b);
2413fcf5ef2aSThomas Huth #endif
2414fcf5ef2aSThomas Huth }
2415fcf5ef2aSThomas Huth 
2416fcf5ef2aSThomas Huth void helper_vaddeuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2417fcf5ef2aSThomas Huth {
2418fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2419fcf5ef2aSThomas Huth     r->u128 = a->u128 + b->u128 + (c->u128 & 1);
2420fcf5ef2aSThomas Huth #else
2421fcf5ef2aSThomas Huth 
2422fcf5ef2aSThomas Huth     if (c->u64[LO_IDX] & 1) {
2423fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2424fcf5ef2aSThomas Huth 
2425fcf5ef2aSThomas Huth         tmp.u64[HI_IDX] = 0;
2426fcf5ef2aSThomas Huth         tmp.u64[LO_IDX] = c->u64[LO_IDX] & 1;
2427fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
2428fcf5ef2aSThomas Huth         avr_qw_add(r, tmp, *b);
2429fcf5ef2aSThomas Huth     } else {
2430fcf5ef2aSThomas Huth         avr_qw_add(r, *a, *b);
2431fcf5ef2aSThomas Huth     }
2432fcf5ef2aSThomas Huth #endif
2433fcf5ef2aSThomas Huth }
2434fcf5ef2aSThomas Huth 
2435fcf5ef2aSThomas Huth void helper_vaddcuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2436fcf5ef2aSThomas Huth {
2437fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2438fcf5ef2aSThomas Huth     r->u128 = (~a->u128 < b->u128);
2439fcf5ef2aSThomas Huth #else
2440fcf5ef2aSThomas Huth     ppc_avr_t not_a;
2441fcf5ef2aSThomas Huth 
2442fcf5ef2aSThomas Huth     avr_qw_not(&not_a, *a);
2443fcf5ef2aSThomas Huth 
2444fcf5ef2aSThomas Huth     r->u64[HI_IDX] = 0;
2445fcf5ef2aSThomas Huth     r->u64[LO_IDX] = (avr_qw_cmpu(not_a, *b) < 0);
2446fcf5ef2aSThomas Huth #endif
2447fcf5ef2aSThomas Huth }
2448fcf5ef2aSThomas Huth 
2449fcf5ef2aSThomas Huth void helper_vaddecuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2450fcf5ef2aSThomas Huth {
2451fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2452fcf5ef2aSThomas Huth     int carry_out = (~a->u128 < b->u128);
2453fcf5ef2aSThomas Huth     if (!carry_out && (c->u128 & 1)) {
2454fcf5ef2aSThomas Huth         carry_out = ((a->u128 + b->u128 + 1) == 0) &&
2455fcf5ef2aSThomas Huth                     ((a->u128 != 0) || (b->u128 != 0));
2456fcf5ef2aSThomas Huth     }
2457fcf5ef2aSThomas Huth     r->u128 = carry_out;
2458fcf5ef2aSThomas Huth #else
2459fcf5ef2aSThomas Huth 
2460fcf5ef2aSThomas Huth     int carry_in = c->u64[LO_IDX] & 1;
2461fcf5ef2aSThomas Huth     int carry_out = 0;
2462fcf5ef2aSThomas Huth     ppc_avr_t tmp;
2463fcf5ef2aSThomas Huth 
2464fcf5ef2aSThomas Huth     carry_out = avr_qw_addc(&tmp, *a, *b);
2465fcf5ef2aSThomas Huth 
2466fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
2467fcf5ef2aSThomas Huth         ppc_avr_t one = QW_ONE;
2468fcf5ef2aSThomas Huth         carry_out = avr_qw_addc(&tmp, tmp, one);
2469fcf5ef2aSThomas Huth     }
2470fcf5ef2aSThomas Huth     r->u64[HI_IDX] = 0;
2471fcf5ef2aSThomas Huth     r->u64[LO_IDX] = carry_out;
2472fcf5ef2aSThomas Huth #endif
2473fcf5ef2aSThomas Huth }
2474fcf5ef2aSThomas Huth 
2475fcf5ef2aSThomas Huth void helper_vsubuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2476fcf5ef2aSThomas Huth {
2477fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2478fcf5ef2aSThomas Huth     r->u128 = a->u128 - b->u128;
2479fcf5ef2aSThomas Huth #else
2480fcf5ef2aSThomas Huth     ppc_avr_t tmp;
2481fcf5ef2aSThomas Huth     ppc_avr_t one = QW_ONE;
2482fcf5ef2aSThomas Huth 
2483fcf5ef2aSThomas Huth     avr_qw_not(&tmp, *b);
2484fcf5ef2aSThomas Huth     avr_qw_add(&tmp, *a, tmp);
2485fcf5ef2aSThomas Huth     avr_qw_add(r, tmp, one);
2486fcf5ef2aSThomas Huth #endif
2487fcf5ef2aSThomas Huth }
2488fcf5ef2aSThomas Huth 
2489fcf5ef2aSThomas Huth void helper_vsubeuqm(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2490fcf5ef2aSThomas Huth {
2491fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2492fcf5ef2aSThomas Huth     r->u128 = a->u128 + ~b->u128 + (c->u128 & 1);
2493fcf5ef2aSThomas Huth #else
2494fcf5ef2aSThomas Huth     ppc_avr_t tmp, sum;
2495fcf5ef2aSThomas Huth 
2496fcf5ef2aSThomas Huth     avr_qw_not(&tmp, *b);
2497fcf5ef2aSThomas Huth     avr_qw_add(&sum, *a, tmp);
2498fcf5ef2aSThomas Huth 
2499fcf5ef2aSThomas Huth     tmp.u64[HI_IDX] = 0;
2500fcf5ef2aSThomas Huth     tmp.u64[LO_IDX] = c->u64[LO_IDX] & 1;
2501fcf5ef2aSThomas Huth     avr_qw_add(r, sum, tmp);
2502fcf5ef2aSThomas Huth #endif
2503fcf5ef2aSThomas Huth }
2504fcf5ef2aSThomas Huth 
2505fcf5ef2aSThomas Huth void helper_vsubcuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
2506fcf5ef2aSThomas Huth {
2507fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2508fcf5ef2aSThomas Huth     r->u128 = (~a->u128 < ~b->u128) ||
2509fcf5ef2aSThomas Huth                  (a->u128 + ~b->u128 == (__uint128_t)-1);
2510fcf5ef2aSThomas Huth #else
2511fcf5ef2aSThomas Huth     int carry = (avr_qw_cmpu(*a, *b) > 0);
2512fcf5ef2aSThomas Huth     if (!carry) {
2513fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2514fcf5ef2aSThomas Huth         avr_qw_not(&tmp, *b);
2515fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
2516fcf5ef2aSThomas Huth         carry = ((tmp.s64[HI_IDX] == -1ull) && (tmp.s64[LO_IDX] == -1ull));
2517fcf5ef2aSThomas Huth     }
2518fcf5ef2aSThomas Huth     r->u64[HI_IDX] = 0;
2519fcf5ef2aSThomas Huth     r->u64[LO_IDX] = carry;
2520fcf5ef2aSThomas Huth #endif
2521fcf5ef2aSThomas Huth }
2522fcf5ef2aSThomas Huth 
2523fcf5ef2aSThomas Huth void helper_vsubecuq(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
2524fcf5ef2aSThomas Huth {
2525fcf5ef2aSThomas Huth #ifdef CONFIG_INT128
2526fcf5ef2aSThomas Huth     r->u128 =
2527fcf5ef2aSThomas Huth         (~a->u128 < ~b->u128) ||
2528fcf5ef2aSThomas Huth         ((c->u128 & 1) && (a->u128 + ~b->u128 == (__uint128_t)-1));
2529fcf5ef2aSThomas Huth #else
2530fcf5ef2aSThomas Huth     int carry_in = c->u64[LO_IDX] & 1;
2531fcf5ef2aSThomas Huth     int carry_out = (avr_qw_cmpu(*a, *b) > 0);
2532fcf5ef2aSThomas Huth     if (!carry_out && carry_in) {
2533fcf5ef2aSThomas Huth         ppc_avr_t tmp;
2534fcf5ef2aSThomas Huth         avr_qw_not(&tmp, *b);
2535fcf5ef2aSThomas Huth         avr_qw_add(&tmp, *a, tmp);
2536fcf5ef2aSThomas Huth         carry_out = ((tmp.u64[HI_IDX] == -1ull) && (tmp.u64[LO_IDX] == -1ull));
2537fcf5ef2aSThomas Huth     }
2538fcf5ef2aSThomas Huth 
2539fcf5ef2aSThomas Huth     r->u64[HI_IDX] = 0;
2540fcf5ef2aSThomas Huth     r->u64[LO_IDX] = carry_out;
2541fcf5ef2aSThomas Huth #endif
2542fcf5ef2aSThomas Huth }
2543fcf5ef2aSThomas Huth 
2544fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_1 0xC
2545fcf5ef2aSThomas Huth #define BCD_PLUS_PREF_2 0xF
2546fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_1  0xA
2547fcf5ef2aSThomas Huth #define BCD_NEG_PREF    0xD
2548fcf5ef2aSThomas Huth #define BCD_NEG_ALT     0xB
2549fcf5ef2aSThomas Huth #define BCD_PLUS_ALT_2  0xE
2550fcf5ef2aSThomas Huth #define NATIONAL_PLUS   0x2B
2551fcf5ef2aSThomas Huth #define NATIONAL_NEG    0x2D
2552fcf5ef2aSThomas Huth 
2553fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2554365206aeSJose Ricardo Ziviani #define BCD_DIG_BYTE(n) (15 - ((n) / 2))
2555fcf5ef2aSThomas Huth #else
2556365206aeSJose Ricardo Ziviani #define BCD_DIG_BYTE(n) ((n) / 2)
2557fcf5ef2aSThomas Huth #endif
2558fcf5ef2aSThomas Huth 
2559fcf5ef2aSThomas Huth static int bcd_get_sgn(ppc_avr_t *bcd)
2560fcf5ef2aSThomas Huth {
2561fcf5ef2aSThomas Huth     switch (bcd->u8[BCD_DIG_BYTE(0)] & 0xF) {
2562fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_1:
2563fcf5ef2aSThomas Huth     case BCD_PLUS_PREF_2:
2564fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_1:
2565fcf5ef2aSThomas Huth     case BCD_PLUS_ALT_2:
2566fcf5ef2aSThomas Huth     {
2567fcf5ef2aSThomas Huth         return 1;
2568fcf5ef2aSThomas Huth     }
2569fcf5ef2aSThomas Huth 
2570fcf5ef2aSThomas Huth     case BCD_NEG_PREF:
2571fcf5ef2aSThomas Huth     case BCD_NEG_ALT:
2572fcf5ef2aSThomas Huth     {
2573fcf5ef2aSThomas Huth         return -1;
2574fcf5ef2aSThomas Huth     }
2575fcf5ef2aSThomas Huth 
2576fcf5ef2aSThomas Huth     default:
2577fcf5ef2aSThomas Huth     {
2578fcf5ef2aSThomas Huth         return 0;
2579fcf5ef2aSThomas Huth     }
2580fcf5ef2aSThomas Huth     }
2581fcf5ef2aSThomas Huth }
2582fcf5ef2aSThomas Huth 
2583fcf5ef2aSThomas Huth static int bcd_preferred_sgn(int sgn, int ps)
2584fcf5ef2aSThomas Huth {
2585fcf5ef2aSThomas Huth     if (sgn >= 0) {
2586fcf5ef2aSThomas Huth         return (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2;
2587fcf5ef2aSThomas Huth     } else {
2588fcf5ef2aSThomas Huth         return BCD_NEG_PREF;
2589fcf5ef2aSThomas Huth     }
2590fcf5ef2aSThomas Huth }
2591fcf5ef2aSThomas Huth 
2592fcf5ef2aSThomas Huth static uint8_t bcd_get_digit(ppc_avr_t *bcd, int n, int *invalid)
2593fcf5ef2aSThomas Huth {
2594fcf5ef2aSThomas Huth     uint8_t result;
2595fcf5ef2aSThomas Huth     if (n & 1) {
2596fcf5ef2aSThomas Huth         result = bcd->u8[BCD_DIG_BYTE(n)] >> 4;
2597fcf5ef2aSThomas Huth     } else {
2598fcf5ef2aSThomas Huth        result = bcd->u8[BCD_DIG_BYTE(n)] & 0xF;
2599fcf5ef2aSThomas Huth     }
2600fcf5ef2aSThomas Huth 
2601fcf5ef2aSThomas Huth     if (unlikely(result > 9)) {
2602fcf5ef2aSThomas Huth         *invalid = true;
2603fcf5ef2aSThomas Huth     }
2604fcf5ef2aSThomas Huth     return result;
2605fcf5ef2aSThomas Huth }
2606fcf5ef2aSThomas Huth 
2607fcf5ef2aSThomas Huth static void bcd_put_digit(ppc_avr_t *bcd, uint8_t digit, int n)
2608fcf5ef2aSThomas Huth {
2609fcf5ef2aSThomas Huth     if (n & 1) {
2610fcf5ef2aSThomas Huth         bcd->u8[BCD_DIG_BYTE(n)] &= 0x0F;
2611fcf5ef2aSThomas Huth         bcd->u8[BCD_DIG_BYTE(n)] |= (digit<<4);
2612fcf5ef2aSThomas Huth     } else {
2613fcf5ef2aSThomas Huth         bcd->u8[BCD_DIG_BYTE(n)] &= 0xF0;
2614fcf5ef2aSThomas Huth         bcd->u8[BCD_DIG_BYTE(n)] |= digit;
2615fcf5ef2aSThomas Huth     }
2616fcf5ef2aSThomas Huth }
2617fcf5ef2aSThomas Huth 
2618071663dfSJose Ricardo Ziviani static bool bcd_is_valid(ppc_avr_t *bcd)
2619071663dfSJose Ricardo Ziviani {
2620071663dfSJose Ricardo Ziviani     int i;
2621071663dfSJose Ricardo Ziviani     int invalid = 0;
2622071663dfSJose Ricardo Ziviani 
2623071663dfSJose Ricardo Ziviani     if (bcd_get_sgn(bcd) == 0) {
2624071663dfSJose Ricardo Ziviani         return false;
2625071663dfSJose Ricardo Ziviani     }
2626071663dfSJose Ricardo Ziviani 
2627071663dfSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
2628071663dfSJose Ricardo Ziviani         bcd_get_digit(bcd, i, &invalid);
2629071663dfSJose Ricardo Ziviani         if (unlikely(invalid)) {
2630071663dfSJose Ricardo Ziviani             return false;
2631071663dfSJose Ricardo Ziviani         }
2632071663dfSJose Ricardo Ziviani     }
2633071663dfSJose Ricardo Ziviani     return true;
2634071663dfSJose Ricardo Ziviani }
2635071663dfSJose Ricardo Ziviani 
2636fcf5ef2aSThomas Huth static int bcd_cmp_zero(ppc_avr_t *bcd)
2637fcf5ef2aSThomas Huth {
2638fcf5ef2aSThomas Huth     if (bcd->u64[HI_IDX] == 0 && (bcd->u64[LO_IDX] >> 4) == 0) {
2639efa73196SNikunj A Dadhania         return CRF_EQ;
2640fcf5ef2aSThomas Huth     } else {
2641efa73196SNikunj A Dadhania         return (bcd_get_sgn(bcd) == 1) ? CRF_GT : CRF_LT;
2642fcf5ef2aSThomas Huth     }
2643fcf5ef2aSThomas Huth }
2644fcf5ef2aSThomas Huth 
2645fcf5ef2aSThomas Huth static uint16_t get_national_digit(ppc_avr_t *reg, int n)
2646fcf5ef2aSThomas Huth {
2647fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2648fcf5ef2aSThomas Huth     return reg->u16[7 - n];
2649fcf5ef2aSThomas Huth #else
2650fcf5ef2aSThomas Huth     return reg->u16[n];
2651fcf5ef2aSThomas Huth #endif
2652fcf5ef2aSThomas Huth }
2653fcf5ef2aSThomas Huth 
2654fcf5ef2aSThomas Huth static void set_national_digit(ppc_avr_t *reg, uint8_t val, int n)
2655fcf5ef2aSThomas Huth {
2656fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
2657fcf5ef2aSThomas Huth     reg->u16[7 - n] = val;
2658fcf5ef2aSThomas Huth #else
2659fcf5ef2aSThomas Huth     reg->u16[n] = val;
2660fcf5ef2aSThomas Huth #endif
2661fcf5ef2aSThomas Huth }
2662fcf5ef2aSThomas Huth 
2663fcf5ef2aSThomas Huth static int bcd_cmp_mag(ppc_avr_t *a, ppc_avr_t *b)
2664fcf5ef2aSThomas Huth {
2665fcf5ef2aSThomas Huth     int i;
2666fcf5ef2aSThomas Huth     int invalid = 0;
2667fcf5ef2aSThomas Huth     for (i = 31; i > 0; i--) {
2668fcf5ef2aSThomas Huth         uint8_t dig_a = bcd_get_digit(a, i, &invalid);
2669fcf5ef2aSThomas Huth         uint8_t dig_b = bcd_get_digit(b, i, &invalid);
2670fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2671fcf5ef2aSThomas Huth             return 0; /* doesn't matter */
2672fcf5ef2aSThomas Huth         } else if (dig_a > dig_b) {
2673fcf5ef2aSThomas Huth             return 1;
2674fcf5ef2aSThomas Huth         } else if (dig_a < dig_b) {
2675fcf5ef2aSThomas Huth             return -1;
2676fcf5ef2aSThomas Huth         }
2677fcf5ef2aSThomas Huth     }
2678fcf5ef2aSThomas Huth 
2679fcf5ef2aSThomas Huth     return 0;
2680fcf5ef2aSThomas Huth }
2681fcf5ef2aSThomas Huth 
2682fcf5ef2aSThomas Huth static int bcd_add_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2683fcf5ef2aSThomas Huth                        int *overflow)
2684fcf5ef2aSThomas Huth {
2685fcf5ef2aSThomas Huth     int carry = 0;
2686fcf5ef2aSThomas Huth     int i;
2687fcf5ef2aSThomas Huth     int is_zero = 1;
2688fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2689fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) +
2690fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2691fcf5ef2aSThomas Huth         is_zero &= (digit == 0);
2692fcf5ef2aSThomas Huth         if (digit > 9) {
2693fcf5ef2aSThomas Huth             carry = 1;
2694fcf5ef2aSThomas Huth             digit -= 10;
2695fcf5ef2aSThomas Huth         } else {
2696fcf5ef2aSThomas Huth             carry = 0;
2697fcf5ef2aSThomas Huth         }
2698fcf5ef2aSThomas Huth 
2699fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2700fcf5ef2aSThomas Huth 
2701fcf5ef2aSThomas Huth         if (unlikely(*invalid)) {
2702fcf5ef2aSThomas Huth             return -1;
2703fcf5ef2aSThomas Huth         }
2704fcf5ef2aSThomas Huth     }
2705fcf5ef2aSThomas Huth 
2706fcf5ef2aSThomas Huth     *overflow = carry;
2707fcf5ef2aSThomas Huth     return is_zero;
2708fcf5ef2aSThomas Huth }
2709fcf5ef2aSThomas Huth 
2710fcf5ef2aSThomas Huth static int bcd_sub_mag(ppc_avr_t *t, ppc_avr_t *a, ppc_avr_t *b, int *invalid,
2711fcf5ef2aSThomas Huth                        int *overflow)
2712fcf5ef2aSThomas Huth {
2713fcf5ef2aSThomas Huth     int carry = 0;
2714fcf5ef2aSThomas Huth     int i;
2715fcf5ef2aSThomas Huth     int is_zero = 1;
2716fcf5ef2aSThomas Huth     for (i = 1; i <= 31; i++) {
2717fcf5ef2aSThomas Huth         uint8_t digit = bcd_get_digit(a, i, invalid) -
2718fcf5ef2aSThomas Huth                         bcd_get_digit(b, i, invalid) + carry;
2719fcf5ef2aSThomas Huth         is_zero &= (digit == 0);
2720fcf5ef2aSThomas Huth         if (digit & 0x80) {
2721fcf5ef2aSThomas Huth             carry = -1;
2722fcf5ef2aSThomas Huth             digit += 10;
2723fcf5ef2aSThomas Huth         } else {
2724fcf5ef2aSThomas Huth             carry = 0;
2725fcf5ef2aSThomas Huth         }
2726fcf5ef2aSThomas Huth 
2727fcf5ef2aSThomas Huth         bcd_put_digit(t, digit, i);
2728fcf5ef2aSThomas Huth 
2729fcf5ef2aSThomas Huth         if (unlikely(*invalid)) {
2730fcf5ef2aSThomas Huth             return -1;
2731fcf5ef2aSThomas Huth         }
2732fcf5ef2aSThomas Huth     }
2733fcf5ef2aSThomas Huth 
2734fcf5ef2aSThomas Huth     *overflow = carry;
2735fcf5ef2aSThomas Huth     return is_zero;
2736fcf5ef2aSThomas Huth }
2737fcf5ef2aSThomas Huth 
2738fcf5ef2aSThomas Huth uint32_t helper_bcdadd(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2739fcf5ef2aSThomas Huth {
2740fcf5ef2aSThomas Huth 
2741fcf5ef2aSThomas Huth     int sgna = bcd_get_sgn(a);
2742fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2743fcf5ef2aSThomas Huth     int invalid = (sgna == 0) || (sgnb == 0);
2744fcf5ef2aSThomas Huth     int overflow = 0;
2745fcf5ef2aSThomas Huth     int zero = 0;
2746fcf5ef2aSThomas Huth     uint32_t cr = 0;
2747fcf5ef2aSThomas Huth     ppc_avr_t result = { .u64 = { 0, 0 } };
2748fcf5ef2aSThomas Huth 
2749fcf5ef2aSThomas Huth     if (!invalid) {
2750fcf5ef2aSThomas Huth         if (sgna == sgnb) {
2751fcf5ef2aSThomas Huth             result.u8[BCD_DIG_BYTE(0)] = bcd_preferred_sgn(sgna, ps);
2752fcf5ef2aSThomas Huth             zero = bcd_add_mag(&result, a, b, &invalid, &overflow);
2753efa73196SNikunj A Dadhania             cr = (sgna > 0) ? CRF_GT : CRF_LT;
2754fcf5ef2aSThomas Huth         } else if (bcd_cmp_mag(a, b) > 0) {
2755fcf5ef2aSThomas Huth             result.u8[BCD_DIG_BYTE(0)] = bcd_preferred_sgn(sgna, ps);
2756fcf5ef2aSThomas Huth             zero = bcd_sub_mag(&result, a, b, &invalid, &overflow);
2757efa73196SNikunj A Dadhania             cr = (sgna > 0) ? CRF_GT : CRF_LT;
2758fcf5ef2aSThomas Huth         } else {
2759fcf5ef2aSThomas Huth             result.u8[BCD_DIG_BYTE(0)] = bcd_preferred_sgn(sgnb, ps);
2760fcf5ef2aSThomas Huth             zero = bcd_sub_mag(&result, b, a, &invalid, &overflow);
2761efa73196SNikunj A Dadhania             cr = (sgnb > 0) ? CRF_GT : CRF_LT;
2762fcf5ef2aSThomas Huth         }
2763fcf5ef2aSThomas Huth     }
2764fcf5ef2aSThomas Huth 
2765fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2766fcf5ef2aSThomas Huth         result.u64[HI_IDX] = result.u64[LO_IDX] = -1;
2767efa73196SNikunj A Dadhania         cr = CRF_SO;
2768fcf5ef2aSThomas Huth     } else if (overflow) {
2769efa73196SNikunj A Dadhania         cr |= CRF_SO;
2770fcf5ef2aSThomas Huth     } else if (zero) {
2771efa73196SNikunj A Dadhania         cr = CRF_EQ;
2772fcf5ef2aSThomas Huth     }
2773fcf5ef2aSThomas Huth 
2774fcf5ef2aSThomas Huth     *r = result;
2775fcf5ef2aSThomas Huth 
2776fcf5ef2aSThomas Huth     return cr;
2777fcf5ef2aSThomas Huth }
2778fcf5ef2aSThomas Huth 
2779fcf5ef2aSThomas Huth uint32_t helper_bcdsub(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
2780fcf5ef2aSThomas Huth {
2781fcf5ef2aSThomas Huth     ppc_avr_t bcopy = *b;
2782fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2783fcf5ef2aSThomas Huth     if (sgnb < 0) {
2784fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_PLUS_PREF_1, 0);
2785fcf5ef2aSThomas Huth     } else if (sgnb > 0) {
2786fcf5ef2aSThomas Huth         bcd_put_digit(&bcopy, BCD_NEG_PREF, 0);
2787fcf5ef2aSThomas Huth     }
2788fcf5ef2aSThomas Huth     /* else invalid ... defer to bcdadd code for proper handling */
2789fcf5ef2aSThomas Huth 
2790fcf5ef2aSThomas Huth     return helper_bcdadd(r, a, &bcopy, ps);
2791fcf5ef2aSThomas Huth }
2792fcf5ef2aSThomas Huth 
2793fcf5ef2aSThomas Huth uint32_t helper_bcdcfn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2794fcf5ef2aSThomas Huth {
2795fcf5ef2aSThomas Huth     int i;
2796fcf5ef2aSThomas Huth     int cr = 0;
2797fcf5ef2aSThomas Huth     uint16_t national = 0;
2798fcf5ef2aSThomas Huth     uint16_t sgnb = get_national_digit(b, 0);
2799fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2800fcf5ef2aSThomas Huth     int invalid = (sgnb != NATIONAL_PLUS && sgnb != NATIONAL_NEG);
2801fcf5ef2aSThomas Huth 
2802fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2803fcf5ef2aSThomas Huth         national = get_national_digit(b, i);
2804fcf5ef2aSThomas Huth         if (unlikely(national < 0x30 || national > 0x39)) {
2805fcf5ef2aSThomas Huth             invalid = 1;
2806fcf5ef2aSThomas Huth             break;
2807fcf5ef2aSThomas Huth         }
2808fcf5ef2aSThomas Huth 
2809fcf5ef2aSThomas Huth         bcd_put_digit(&ret, national & 0xf, i);
2810fcf5ef2aSThomas Huth     }
2811fcf5ef2aSThomas Huth 
2812fcf5ef2aSThomas Huth     if (sgnb == NATIONAL_PLUS) {
2813fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (ps == 0) ? BCD_PLUS_PREF_1 : BCD_PLUS_PREF_2, 0);
2814fcf5ef2aSThomas Huth     } else {
2815fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2816fcf5ef2aSThomas Huth     }
2817fcf5ef2aSThomas Huth 
2818fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2819fcf5ef2aSThomas Huth 
2820fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2821efa73196SNikunj A Dadhania         cr = CRF_SO;
2822fcf5ef2aSThomas Huth     }
2823fcf5ef2aSThomas Huth 
2824fcf5ef2aSThomas Huth     *r = ret;
2825fcf5ef2aSThomas Huth 
2826fcf5ef2aSThomas Huth     return cr;
2827fcf5ef2aSThomas Huth }
2828fcf5ef2aSThomas Huth 
2829fcf5ef2aSThomas Huth uint32_t helper_bcdctn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2830fcf5ef2aSThomas Huth {
2831fcf5ef2aSThomas Huth     int i;
2832fcf5ef2aSThomas Huth     int cr = 0;
2833fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2834fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2835fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2836fcf5ef2aSThomas Huth 
2837fcf5ef2aSThomas Huth     int ox_flag = (b->u64[HI_IDX] != 0) || ((b->u64[LO_IDX] >> 32) != 0);
2838fcf5ef2aSThomas Huth 
2839fcf5ef2aSThomas Huth     for (i = 1; i < 8; i++) {
2840fcf5ef2aSThomas Huth         set_national_digit(&ret, 0x30 + bcd_get_digit(b, i, &invalid), i);
2841fcf5ef2aSThomas Huth 
2842fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2843fcf5ef2aSThomas Huth             break;
2844fcf5ef2aSThomas Huth         }
2845fcf5ef2aSThomas Huth     }
2846fcf5ef2aSThomas Huth     set_national_digit(&ret, (sgnb == -1) ? NATIONAL_NEG : NATIONAL_PLUS, 0);
2847fcf5ef2aSThomas Huth 
2848fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2849fcf5ef2aSThomas Huth 
2850fcf5ef2aSThomas Huth     if (ox_flag) {
2851efa73196SNikunj A Dadhania         cr |= CRF_SO;
2852fcf5ef2aSThomas Huth     }
2853fcf5ef2aSThomas Huth 
2854fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2855efa73196SNikunj A Dadhania         cr = CRF_SO;
2856fcf5ef2aSThomas Huth     }
2857fcf5ef2aSThomas Huth 
2858fcf5ef2aSThomas Huth     *r = ret;
2859fcf5ef2aSThomas Huth 
2860fcf5ef2aSThomas Huth     return cr;
2861fcf5ef2aSThomas Huth }
2862fcf5ef2aSThomas Huth 
2863fcf5ef2aSThomas Huth uint32_t helper_bcdcfz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2864fcf5ef2aSThomas Huth {
2865fcf5ef2aSThomas Huth     int i;
2866fcf5ef2aSThomas Huth     int cr = 0;
2867fcf5ef2aSThomas Huth     int invalid = 0;
2868fcf5ef2aSThomas Huth     int zone_digit = 0;
2869fcf5ef2aSThomas Huth     int zone_lead = ps ? 0xF : 0x3;
2870fcf5ef2aSThomas Huth     int digit = 0;
2871fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2872fcf5ef2aSThomas Huth     int sgnb = b->u8[BCD_DIG_BYTE(0)] >> 4;
2873fcf5ef2aSThomas Huth 
2874fcf5ef2aSThomas Huth     if (unlikely((sgnb < 0xA) && ps)) {
2875fcf5ef2aSThomas Huth         invalid = 1;
2876fcf5ef2aSThomas Huth     }
2877fcf5ef2aSThomas Huth 
2878fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2879365206aeSJose Ricardo Ziviani         zone_digit = i ? b->u8[BCD_DIG_BYTE(i * 2)] >> 4 : zone_lead;
2880fcf5ef2aSThomas Huth         digit = b->u8[BCD_DIG_BYTE(i * 2)] & 0xF;
2881fcf5ef2aSThomas Huth         if (unlikely(zone_digit != zone_lead || digit > 0x9)) {
2882fcf5ef2aSThomas Huth             invalid = 1;
2883fcf5ef2aSThomas Huth             break;
2884fcf5ef2aSThomas Huth         }
2885fcf5ef2aSThomas Huth 
2886fcf5ef2aSThomas Huth         bcd_put_digit(&ret, digit, i + 1);
2887fcf5ef2aSThomas Huth     }
2888fcf5ef2aSThomas Huth 
2889fcf5ef2aSThomas Huth     if ((ps && (sgnb == 0xB || sgnb == 0xD)) ||
2890fcf5ef2aSThomas Huth             (!ps && (sgnb & 0x4))) {
2891fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_NEG_PREF, 0);
2892fcf5ef2aSThomas Huth     } else {
2893fcf5ef2aSThomas Huth         bcd_put_digit(&ret, BCD_PLUS_PREF_1, 0);
2894fcf5ef2aSThomas Huth     }
2895fcf5ef2aSThomas Huth 
2896fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(&ret);
2897fcf5ef2aSThomas Huth 
2898fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2899efa73196SNikunj A Dadhania         cr = CRF_SO;
2900fcf5ef2aSThomas Huth     }
2901fcf5ef2aSThomas Huth 
2902fcf5ef2aSThomas Huth     *r = ret;
2903fcf5ef2aSThomas Huth 
2904fcf5ef2aSThomas Huth     return cr;
2905fcf5ef2aSThomas Huth }
2906fcf5ef2aSThomas Huth 
2907fcf5ef2aSThomas Huth uint32_t helper_bcdctz(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2908fcf5ef2aSThomas Huth {
2909fcf5ef2aSThomas Huth     int i;
2910fcf5ef2aSThomas Huth     int cr = 0;
2911fcf5ef2aSThomas Huth     uint8_t digit = 0;
2912fcf5ef2aSThomas Huth     int sgnb = bcd_get_sgn(b);
2913fcf5ef2aSThomas Huth     int zone_lead = (ps) ? 0xF0 : 0x30;
2914fcf5ef2aSThomas Huth     int invalid = (sgnb == 0);
2915fcf5ef2aSThomas Huth     ppc_avr_t ret = { .u64 = { 0, 0 } };
2916fcf5ef2aSThomas Huth 
2917fcf5ef2aSThomas Huth     int ox_flag = ((b->u64[HI_IDX] >> 4) != 0);
2918fcf5ef2aSThomas Huth 
2919fcf5ef2aSThomas Huth     for (i = 0; i < 16; i++) {
2920fcf5ef2aSThomas Huth         digit = bcd_get_digit(b, i + 1, &invalid);
2921fcf5ef2aSThomas Huth 
2922fcf5ef2aSThomas Huth         if (unlikely(invalid)) {
2923fcf5ef2aSThomas Huth             break;
2924fcf5ef2aSThomas Huth         }
2925fcf5ef2aSThomas Huth 
2926fcf5ef2aSThomas Huth         ret.u8[BCD_DIG_BYTE(i * 2)] = zone_lead + digit;
2927fcf5ef2aSThomas Huth     }
2928fcf5ef2aSThomas Huth 
2929fcf5ef2aSThomas Huth     if (ps) {
2930fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0xC : 0xD, 1);
2931fcf5ef2aSThomas Huth     } else {
2932fcf5ef2aSThomas Huth         bcd_put_digit(&ret, (sgnb == 1) ? 0x3 : 0x7, 1);
2933fcf5ef2aSThomas Huth     }
2934fcf5ef2aSThomas Huth 
2935fcf5ef2aSThomas Huth     cr = bcd_cmp_zero(b);
2936fcf5ef2aSThomas Huth 
2937fcf5ef2aSThomas Huth     if (ox_flag) {
2938efa73196SNikunj A Dadhania         cr |= CRF_SO;
2939fcf5ef2aSThomas Huth     }
2940fcf5ef2aSThomas Huth 
2941fcf5ef2aSThomas Huth     if (unlikely(invalid)) {
2942efa73196SNikunj A Dadhania         cr = CRF_SO;
2943fcf5ef2aSThomas Huth     }
2944fcf5ef2aSThomas Huth 
2945fcf5ef2aSThomas Huth     *r = ret;
2946fcf5ef2aSThomas Huth 
2947fcf5ef2aSThomas Huth     return cr;
2948fcf5ef2aSThomas Huth }
2949fcf5ef2aSThomas Huth 
2950a406c058SJose Ricardo Ziviani uint32_t helper_bcdcfsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2951a406c058SJose Ricardo Ziviani {
2952a406c058SJose Ricardo Ziviani     int i;
2953a406c058SJose Ricardo Ziviani     int cr = 0;
2954a406c058SJose Ricardo Ziviani     uint64_t lo_value;
2955a406c058SJose Ricardo Ziviani     uint64_t hi_value;
2956a406c058SJose Ricardo Ziviani     ppc_avr_t ret = { .u64 = { 0, 0 } };
2957a406c058SJose Ricardo Ziviani 
2958a406c058SJose Ricardo Ziviani     if (b->s64[HI_IDX] < 0) {
2959a406c058SJose Ricardo Ziviani         lo_value = -b->s64[LO_IDX];
2960a406c058SJose Ricardo Ziviani         hi_value = ~b->u64[HI_IDX] + !lo_value;
2961a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, 0xD, 0);
2962a406c058SJose Ricardo Ziviani     } else {
2963a406c058SJose Ricardo Ziviani         lo_value = b->u64[LO_IDX];
2964a406c058SJose Ricardo Ziviani         hi_value = b->u64[HI_IDX];
2965a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, bcd_preferred_sgn(0, ps), 0);
2966a406c058SJose Ricardo Ziviani     }
2967a406c058SJose Ricardo Ziviani 
2968a406c058SJose Ricardo Ziviani     if (divu128(&lo_value, &hi_value, 1000000000000000ULL) ||
2969a406c058SJose Ricardo Ziviani             lo_value > 9999999999999999ULL) {
2970a406c058SJose Ricardo Ziviani         cr = CRF_SO;
2971a406c058SJose Ricardo Ziviani     }
2972a406c058SJose Ricardo Ziviani 
2973a406c058SJose Ricardo Ziviani     for (i = 1; i < 16; hi_value /= 10, i++) {
2974a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, hi_value % 10, i);
2975a406c058SJose Ricardo Ziviani     }
2976a406c058SJose Ricardo Ziviani 
2977a406c058SJose Ricardo Ziviani     for (; i < 32; lo_value /= 10, i++) {
2978a406c058SJose Ricardo Ziviani         bcd_put_digit(&ret, lo_value % 10, i);
2979a406c058SJose Ricardo Ziviani     }
2980a406c058SJose Ricardo Ziviani 
2981a406c058SJose Ricardo Ziviani     cr |= bcd_cmp_zero(&ret);
2982a406c058SJose Ricardo Ziviani 
2983a406c058SJose Ricardo Ziviani     *r = ret;
2984a406c058SJose Ricardo Ziviani 
2985a406c058SJose Ricardo Ziviani     return cr;
2986a406c058SJose Ricardo Ziviani }
2987a406c058SJose Ricardo Ziviani 
2988c85bc7ddSJose Ricardo Ziviani uint32_t helper_bcdctsq(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
2989c85bc7ddSJose Ricardo Ziviani {
2990c85bc7ddSJose Ricardo Ziviani     uint8_t i;
2991c85bc7ddSJose Ricardo Ziviani     int cr;
2992c85bc7ddSJose Ricardo Ziviani     uint64_t carry;
2993c85bc7ddSJose Ricardo Ziviani     uint64_t unused;
2994c85bc7ddSJose Ricardo Ziviani     uint64_t lo_value;
2995c85bc7ddSJose Ricardo Ziviani     uint64_t hi_value = 0;
2996c85bc7ddSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
2997c85bc7ddSJose Ricardo Ziviani     int invalid = (sgnb == 0);
2998c85bc7ddSJose Ricardo Ziviani 
2999c85bc7ddSJose Ricardo Ziviani     lo_value = bcd_get_digit(b, 31, &invalid);
3000c85bc7ddSJose Ricardo Ziviani     for (i = 30; i > 0; i--) {
3001c85bc7ddSJose Ricardo Ziviani         mulu64(&lo_value, &carry, lo_value, 10ULL);
3002c85bc7ddSJose Ricardo Ziviani         mulu64(&hi_value, &unused, hi_value, 10ULL);
3003c85bc7ddSJose Ricardo Ziviani         lo_value += bcd_get_digit(b, i, &invalid);
3004c85bc7ddSJose Ricardo Ziviani         hi_value += carry;
3005c85bc7ddSJose Ricardo Ziviani 
3006c85bc7ddSJose Ricardo Ziviani         if (unlikely(invalid)) {
3007c85bc7ddSJose Ricardo Ziviani             break;
3008c85bc7ddSJose Ricardo Ziviani         }
3009c85bc7ddSJose Ricardo Ziviani     }
3010c85bc7ddSJose Ricardo Ziviani 
3011c85bc7ddSJose Ricardo Ziviani     if (sgnb == -1) {
3012c85bc7ddSJose Ricardo Ziviani         r->s64[LO_IDX] = -lo_value;
3013c85bc7ddSJose Ricardo Ziviani         r->s64[HI_IDX] = ~hi_value + !r->s64[LO_IDX];
3014c85bc7ddSJose Ricardo Ziviani     } else {
3015c85bc7ddSJose Ricardo Ziviani         r->s64[LO_IDX] = lo_value;
3016c85bc7ddSJose Ricardo Ziviani         r->s64[HI_IDX] = hi_value;
3017c85bc7ddSJose Ricardo Ziviani     }
3018c85bc7ddSJose Ricardo Ziviani 
3019c85bc7ddSJose Ricardo Ziviani     cr = bcd_cmp_zero(b);
3020c85bc7ddSJose Ricardo Ziviani 
3021c85bc7ddSJose Ricardo Ziviani     if (unlikely(invalid)) {
3022c85bc7ddSJose Ricardo Ziviani         cr = CRF_SO;
3023c85bc7ddSJose Ricardo Ziviani     }
3024c85bc7ddSJose Ricardo Ziviani 
3025c85bc7ddSJose Ricardo Ziviani     return cr;
3026c85bc7ddSJose Ricardo Ziviani }
3027c85bc7ddSJose Ricardo Ziviani 
3028c3025c3bSJose Ricardo Ziviani uint32_t helper_bcdcpsgn(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
3029c3025c3bSJose Ricardo Ziviani {
3030c3025c3bSJose Ricardo Ziviani     int i;
3031c3025c3bSJose Ricardo Ziviani     int invalid = 0;
3032c3025c3bSJose Ricardo Ziviani 
3033c3025c3bSJose Ricardo Ziviani     if (bcd_get_sgn(a) == 0 || bcd_get_sgn(b) == 0) {
3034c3025c3bSJose Ricardo Ziviani         return CRF_SO;
3035c3025c3bSJose Ricardo Ziviani     }
3036c3025c3bSJose Ricardo Ziviani 
3037c3025c3bSJose Ricardo Ziviani     *r = *a;
3038c3025c3bSJose Ricardo Ziviani     bcd_put_digit(r, b->u8[BCD_DIG_BYTE(0)] & 0xF, 0);
3039c3025c3bSJose Ricardo Ziviani 
3040c3025c3bSJose Ricardo Ziviani     for (i = 1; i < 32; i++) {
3041c3025c3bSJose Ricardo Ziviani         bcd_get_digit(a, i, &invalid);
3042c3025c3bSJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
3043c3025c3bSJose Ricardo Ziviani         if (unlikely(invalid)) {
3044c3025c3bSJose Ricardo Ziviani             return CRF_SO;
3045c3025c3bSJose Ricardo Ziviani         }
3046c3025c3bSJose Ricardo Ziviani     }
3047c3025c3bSJose Ricardo Ziviani 
3048c3025c3bSJose Ricardo Ziviani     return bcd_cmp_zero(r);
3049c3025c3bSJose Ricardo Ziviani }
3050c3025c3bSJose Ricardo Ziviani 
3051466a3f9cSJose Ricardo Ziviani uint32_t helper_bcdsetsgn(ppc_avr_t *r, ppc_avr_t *b, uint32_t ps)
3052466a3f9cSJose Ricardo Ziviani {
3053466a3f9cSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
3054466a3f9cSJose Ricardo Ziviani 
3055466a3f9cSJose Ricardo Ziviani     *r = *b;
3056466a3f9cSJose Ricardo Ziviani     bcd_put_digit(r, bcd_preferred_sgn(sgnb, ps), 0);
3057466a3f9cSJose Ricardo Ziviani 
3058071663dfSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
3059466a3f9cSJose Ricardo Ziviani         return CRF_SO;
3060466a3f9cSJose Ricardo Ziviani     }
3061466a3f9cSJose Ricardo Ziviani 
3062466a3f9cSJose Ricardo Ziviani     return bcd_cmp_zero(r);
3063466a3f9cSJose Ricardo Ziviani }
3064466a3f9cSJose Ricardo Ziviani 
3065e04797f7SJose Ricardo Ziviani uint32_t helper_bcds(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
3066e04797f7SJose Ricardo Ziviani {
3067e04797f7SJose Ricardo Ziviani     int cr;
3068e04797f7SJose Ricardo Ziviani #if defined(HOST_WORDS_BIGENDIAN)
3069e04797f7SJose Ricardo Ziviani     int i = a->s8[7];
3070e04797f7SJose Ricardo Ziviani #else
3071e04797f7SJose Ricardo Ziviani     int i = a->s8[8];
3072e04797f7SJose Ricardo Ziviani #endif
3073e04797f7SJose Ricardo Ziviani     bool ox_flag = false;
3074e04797f7SJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
3075e04797f7SJose Ricardo Ziviani     ppc_avr_t ret = *b;
3076e04797f7SJose Ricardo Ziviani     ret.u64[LO_IDX] &= ~0xf;
3077e04797f7SJose Ricardo Ziviani 
3078e04797f7SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
3079e04797f7SJose Ricardo Ziviani         return CRF_SO;
3080e04797f7SJose Ricardo Ziviani     }
3081e04797f7SJose Ricardo Ziviani 
3082e04797f7SJose Ricardo Ziviani     if (unlikely(i > 31)) {
3083e04797f7SJose Ricardo Ziviani         i = 31;
3084e04797f7SJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
3085e04797f7SJose Ricardo Ziviani         i = -31;
3086e04797f7SJose Ricardo Ziviani     }
3087e04797f7SJose Ricardo Ziviani 
3088e04797f7SJose Ricardo Ziviani     if (i > 0) {
3089e04797f7SJose Ricardo Ziviani         ulshift(&ret.u64[LO_IDX], &ret.u64[HI_IDX], i * 4, &ox_flag);
3090e04797f7SJose Ricardo Ziviani     } else {
3091e04797f7SJose Ricardo Ziviani         urshift(&ret.u64[LO_IDX], &ret.u64[HI_IDX], -i * 4);
3092e04797f7SJose Ricardo Ziviani     }
3093e04797f7SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
3094e04797f7SJose Ricardo Ziviani 
3095e04797f7SJose Ricardo Ziviani     *r = ret;
3096e04797f7SJose Ricardo Ziviani 
3097e04797f7SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
3098e04797f7SJose Ricardo Ziviani     if (ox_flag) {
3099e04797f7SJose Ricardo Ziviani         cr |= CRF_SO;
3100e04797f7SJose Ricardo Ziviani     }
3101e04797f7SJose Ricardo Ziviani 
3102e04797f7SJose Ricardo Ziviani     return cr;
3103e04797f7SJose Ricardo Ziviani }
3104e04797f7SJose Ricardo Ziviani 
3105a49a95e9SJose Ricardo Ziviani uint32_t helper_bcdus(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
3106a49a95e9SJose Ricardo Ziviani {
3107a49a95e9SJose Ricardo Ziviani     int cr;
3108a49a95e9SJose Ricardo Ziviani     int i;
3109a49a95e9SJose Ricardo Ziviani     int invalid = 0;
3110a49a95e9SJose Ricardo Ziviani     bool ox_flag = false;
3111a49a95e9SJose Ricardo Ziviani     ppc_avr_t ret = *b;
3112a49a95e9SJose Ricardo Ziviani 
3113a49a95e9SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
3114a49a95e9SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
3115a49a95e9SJose Ricardo Ziviani 
3116a49a95e9SJose Ricardo Ziviani         if (unlikely(invalid)) {
3117a49a95e9SJose Ricardo Ziviani             return CRF_SO;
3118a49a95e9SJose Ricardo Ziviani         }
3119a49a95e9SJose Ricardo Ziviani     }
3120a49a95e9SJose Ricardo Ziviani 
3121a49a95e9SJose Ricardo Ziviani #if defined(HOST_WORDS_BIGENDIAN)
3122a49a95e9SJose Ricardo Ziviani     i = a->s8[7];
3123a49a95e9SJose Ricardo Ziviani #else
3124a49a95e9SJose Ricardo Ziviani     i = a->s8[8];
3125a49a95e9SJose Ricardo Ziviani #endif
3126a49a95e9SJose Ricardo Ziviani     if (i >= 32) {
3127a49a95e9SJose Ricardo Ziviani         ox_flag = true;
3128a49a95e9SJose Ricardo Ziviani         ret.u64[LO_IDX] = ret.u64[HI_IDX] = 0;
3129a49a95e9SJose Ricardo Ziviani     } else if (i <= -32) {
3130a49a95e9SJose Ricardo Ziviani         ret.u64[LO_IDX] = ret.u64[HI_IDX] = 0;
3131a49a95e9SJose Ricardo Ziviani     } else if (i > 0) {
3132a49a95e9SJose Ricardo Ziviani         ulshift(&ret.u64[LO_IDX], &ret.u64[HI_IDX], i * 4, &ox_flag);
3133a49a95e9SJose Ricardo Ziviani     } else {
3134a49a95e9SJose Ricardo Ziviani         urshift(&ret.u64[LO_IDX], &ret.u64[HI_IDX], -i * 4);
3135a49a95e9SJose Ricardo Ziviani     }
3136a49a95e9SJose Ricardo Ziviani     *r = ret;
3137a49a95e9SJose Ricardo Ziviani 
3138a49a95e9SJose Ricardo Ziviani     cr = bcd_cmp_zero(r);
3139a49a95e9SJose Ricardo Ziviani     if (ox_flag) {
3140a49a95e9SJose Ricardo Ziviani         cr |= CRF_SO;
3141a49a95e9SJose Ricardo Ziviani     }
3142a49a95e9SJose Ricardo Ziviani 
3143a49a95e9SJose Ricardo Ziviani     return cr;
3144a49a95e9SJose Ricardo Ziviani }
3145a49a95e9SJose Ricardo Ziviani 
3146a54238adSJose Ricardo Ziviani uint32_t helper_bcdsr(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
3147a54238adSJose Ricardo Ziviani {
3148a54238adSJose Ricardo Ziviani     int cr;
3149a54238adSJose Ricardo Ziviani     int unused = 0;
3150a54238adSJose Ricardo Ziviani     int invalid = 0;
3151a54238adSJose Ricardo Ziviani     bool ox_flag = false;
3152a54238adSJose Ricardo Ziviani     int sgnb = bcd_get_sgn(b);
3153a54238adSJose Ricardo Ziviani     ppc_avr_t ret = *b;
3154a54238adSJose Ricardo Ziviani     ret.u64[LO_IDX] &= ~0xf;
3155a54238adSJose Ricardo Ziviani 
3156a54238adSJose Ricardo Ziviani #if defined(HOST_WORDS_BIGENDIAN)
3157a54238adSJose Ricardo Ziviani     int i = a->s8[7];
3158a54238adSJose Ricardo Ziviani     ppc_avr_t bcd_one = { .u64 = { 0, 0x10 } };
3159a54238adSJose Ricardo Ziviani #else
3160a54238adSJose Ricardo Ziviani     int i = a->s8[8];
3161a54238adSJose Ricardo Ziviani     ppc_avr_t bcd_one = { .u64 = { 0x10, 0 } };
3162a54238adSJose Ricardo Ziviani #endif
3163a54238adSJose Ricardo Ziviani 
3164a54238adSJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
3165a54238adSJose Ricardo Ziviani         return CRF_SO;
3166a54238adSJose Ricardo Ziviani     }
3167a54238adSJose Ricardo Ziviani 
3168a54238adSJose Ricardo Ziviani     if (unlikely(i > 31)) {
3169a54238adSJose Ricardo Ziviani         i = 31;
3170a54238adSJose Ricardo Ziviani     } else if (unlikely(i < -31)) {
3171a54238adSJose Ricardo Ziviani         i = -31;
3172a54238adSJose Ricardo Ziviani     }
3173a54238adSJose Ricardo Ziviani 
3174a54238adSJose Ricardo Ziviani     if (i > 0) {
3175a54238adSJose Ricardo Ziviani         ulshift(&ret.u64[LO_IDX], &ret.u64[HI_IDX], i * 4, &ox_flag);
3176a54238adSJose Ricardo Ziviani     } else {
3177a54238adSJose Ricardo Ziviani         urshift(&ret.u64[LO_IDX], &ret.u64[HI_IDX], -i * 4);
3178a54238adSJose Ricardo Ziviani 
3179a54238adSJose Ricardo Ziviani         if (bcd_get_digit(&ret, 0, &invalid) >= 5) {
3180a54238adSJose Ricardo Ziviani             bcd_add_mag(&ret, &ret, &bcd_one, &invalid, &unused);
3181a54238adSJose Ricardo Ziviani         }
3182a54238adSJose Ricardo Ziviani     }
3183a54238adSJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(sgnb, ps), 0);
3184a54238adSJose Ricardo Ziviani 
3185a54238adSJose Ricardo Ziviani     cr = bcd_cmp_zero(&ret);
3186a54238adSJose Ricardo Ziviani     if (ox_flag) {
3187a54238adSJose Ricardo Ziviani         cr |= CRF_SO;
3188a54238adSJose Ricardo Ziviani     }
3189a54238adSJose Ricardo Ziviani     *r = ret;
3190a54238adSJose Ricardo Ziviani 
3191a54238adSJose Ricardo Ziviani     return cr;
3192a54238adSJose Ricardo Ziviani }
3193a54238adSJose Ricardo Ziviani 
319431bc4d11SJose Ricardo Ziviani uint32_t helper_bcdtrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
319531bc4d11SJose Ricardo Ziviani {
319631bc4d11SJose Ricardo Ziviani     uint64_t mask;
319731bc4d11SJose Ricardo Ziviani     uint32_t ox_flag = 0;
319831bc4d11SJose Ricardo Ziviani #if defined(HOST_WORDS_BIGENDIAN)
319931bc4d11SJose Ricardo Ziviani     int i = a->s16[3] + 1;
320031bc4d11SJose Ricardo Ziviani #else
320131bc4d11SJose Ricardo Ziviani     int i = a->s16[4] + 1;
320231bc4d11SJose Ricardo Ziviani #endif
320331bc4d11SJose Ricardo Ziviani     ppc_avr_t ret = *b;
320431bc4d11SJose Ricardo Ziviani 
320531bc4d11SJose Ricardo Ziviani     if (bcd_is_valid(b) == false) {
320631bc4d11SJose Ricardo Ziviani         return CRF_SO;
320731bc4d11SJose Ricardo Ziviani     }
320831bc4d11SJose Ricardo Ziviani 
320931bc4d11SJose Ricardo Ziviani     if (i > 16 && i < 32) {
321031bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
321131bc4d11SJose Ricardo Ziviani         if (ret.u64[HI_IDX] & ~mask) {
321231bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
321331bc4d11SJose Ricardo Ziviani         }
321431bc4d11SJose Ricardo Ziviani 
321531bc4d11SJose Ricardo Ziviani         ret.u64[HI_IDX] &= mask;
321631bc4d11SJose Ricardo Ziviani     } else if (i >= 0 && i <= 16) {
321731bc4d11SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
321831bc4d11SJose Ricardo Ziviani         if (ret.u64[HI_IDX] || (ret.u64[LO_IDX] & ~mask)) {
321931bc4d11SJose Ricardo Ziviani             ox_flag = CRF_SO;
322031bc4d11SJose Ricardo Ziviani         }
322131bc4d11SJose Ricardo Ziviani 
322231bc4d11SJose Ricardo Ziviani         ret.u64[LO_IDX] &= mask;
322331bc4d11SJose Ricardo Ziviani         ret.u64[HI_IDX] = 0;
322431bc4d11SJose Ricardo Ziviani     }
322531bc4d11SJose Ricardo Ziviani     bcd_put_digit(&ret, bcd_preferred_sgn(bcd_get_sgn(b), ps), 0);
322631bc4d11SJose Ricardo Ziviani     *r = ret;
322731bc4d11SJose Ricardo Ziviani 
322831bc4d11SJose Ricardo Ziviani     return bcd_cmp_zero(&ret) | ox_flag;
322931bc4d11SJose Ricardo Ziviani }
323031bc4d11SJose Ricardo Ziviani 
3231*5c32e2e4SJose Ricardo Ziviani uint32_t helper_bcdutrunc(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b, uint32_t ps)
3232*5c32e2e4SJose Ricardo Ziviani {
3233*5c32e2e4SJose Ricardo Ziviani     int i;
3234*5c32e2e4SJose Ricardo Ziviani     uint64_t mask;
3235*5c32e2e4SJose Ricardo Ziviani     uint32_t ox_flag = 0;
3236*5c32e2e4SJose Ricardo Ziviani     int invalid = 0;
3237*5c32e2e4SJose Ricardo Ziviani     ppc_avr_t ret = *b;
3238*5c32e2e4SJose Ricardo Ziviani 
3239*5c32e2e4SJose Ricardo Ziviani     for (i = 0; i < 32; i++) {
3240*5c32e2e4SJose Ricardo Ziviani         bcd_get_digit(b, i, &invalid);
3241*5c32e2e4SJose Ricardo Ziviani 
3242*5c32e2e4SJose Ricardo Ziviani         if (unlikely(invalid)) {
3243*5c32e2e4SJose Ricardo Ziviani             return CRF_SO;
3244*5c32e2e4SJose Ricardo Ziviani         }
3245*5c32e2e4SJose Ricardo Ziviani     }
3246*5c32e2e4SJose Ricardo Ziviani 
3247*5c32e2e4SJose Ricardo Ziviani #if defined(HOST_WORDS_BIGENDIAN)
3248*5c32e2e4SJose Ricardo Ziviani     i = a->s16[3];
3249*5c32e2e4SJose Ricardo Ziviani #else
3250*5c32e2e4SJose Ricardo Ziviani     i = a->s16[4];
3251*5c32e2e4SJose Ricardo Ziviani #endif
3252*5c32e2e4SJose Ricardo Ziviani     if (i > 16 && i < 33) {
3253*5c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (128 - i * 4);
3254*5c32e2e4SJose Ricardo Ziviani         if (ret.u64[HI_IDX] & ~mask) {
3255*5c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
3256*5c32e2e4SJose Ricardo Ziviani         }
3257*5c32e2e4SJose Ricardo Ziviani 
3258*5c32e2e4SJose Ricardo Ziviani         ret.u64[HI_IDX] &= mask;
3259*5c32e2e4SJose Ricardo Ziviani     } else if (i > 0 && i <= 16) {
3260*5c32e2e4SJose Ricardo Ziviani         mask = (uint64_t)-1 >> (64 - i * 4);
3261*5c32e2e4SJose Ricardo Ziviani         if (ret.u64[HI_IDX] || (ret.u64[LO_IDX] & ~mask)) {
3262*5c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
3263*5c32e2e4SJose Ricardo Ziviani         }
3264*5c32e2e4SJose Ricardo Ziviani 
3265*5c32e2e4SJose Ricardo Ziviani         ret.u64[LO_IDX] &= mask;
3266*5c32e2e4SJose Ricardo Ziviani         ret.u64[HI_IDX] = 0;
3267*5c32e2e4SJose Ricardo Ziviani     } else if (i == 0) {
3268*5c32e2e4SJose Ricardo Ziviani         if (ret.u64[HI_IDX] || ret.u64[LO_IDX]) {
3269*5c32e2e4SJose Ricardo Ziviani             ox_flag = CRF_SO;
3270*5c32e2e4SJose Ricardo Ziviani         }
3271*5c32e2e4SJose Ricardo Ziviani         ret.u64[HI_IDX] = ret.u64[LO_IDX] = 0;
3272*5c32e2e4SJose Ricardo Ziviani     }
3273*5c32e2e4SJose Ricardo Ziviani 
3274*5c32e2e4SJose Ricardo Ziviani     *r = ret;
3275*5c32e2e4SJose Ricardo Ziviani     if (r->u64[HI_IDX] == 0 && r->u64[LO_IDX] == 0) {
3276*5c32e2e4SJose Ricardo Ziviani         return ox_flag | CRF_EQ;
3277*5c32e2e4SJose Ricardo Ziviani     }
3278*5c32e2e4SJose Ricardo Ziviani 
3279*5c32e2e4SJose Ricardo Ziviani     return ox_flag | CRF_GT;
3280*5c32e2e4SJose Ricardo Ziviani }
3281*5c32e2e4SJose Ricardo Ziviani 
3282fcf5ef2aSThomas Huth void helper_vsbox(ppc_avr_t *r, ppc_avr_t *a)
3283fcf5ef2aSThomas Huth {
3284fcf5ef2aSThomas Huth     int i;
3285fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
3286fcf5ef2aSThomas Huth         r->u8[i] = AES_sbox[a->u8[i]];
3287fcf5ef2aSThomas Huth     }
3288fcf5ef2aSThomas Huth }
3289fcf5ef2aSThomas Huth 
3290fcf5ef2aSThomas Huth void helper_vcipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3291fcf5ef2aSThomas Huth {
3292fcf5ef2aSThomas Huth     ppc_avr_t result;
3293fcf5ef2aSThomas Huth     int i;
3294fcf5ef2aSThomas Huth 
3295fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
3296fcf5ef2aSThomas Huth         result.AVRW(i) = b->AVRW(i) ^
3297fcf5ef2aSThomas Huth             (AES_Te0[a->AVRB(AES_shifts[4*i + 0])] ^
3298fcf5ef2aSThomas Huth              AES_Te1[a->AVRB(AES_shifts[4*i + 1])] ^
3299fcf5ef2aSThomas Huth              AES_Te2[a->AVRB(AES_shifts[4*i + 2])] ^
3300fcf5ef2aSThomas Huth              AES_Te3[a->AVRB(AES_shifts[4*i + 3])]);
3301fcf5ef2aSThomas Huth     }
3302fcf5ef2aSThomas Huth     *r = result;
3303fcf5ef2aSThomas Huth }
3304fcf5ef2aSThomas Huth 
3305fcf5ef2aSThomas Huth void helper_vcipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3306fcf5ef2aSThomas Huth {
3307fcf5ef2aSThomas Huth     ppc_avr_t result;
3308fcf5ef2aSThomas Huth     int i;
3309fcf5ef2aSThomas Huth 
3310fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
3311fcf5ef2aSThomas Huth         result.AVRB(i) = b->AVRB(i) ^ (AES_sbox[a->AVRB(AES_shifts[i])]);
3312fcf5ef2aSThomas Huth     }
3313fcf5ef2aSThomas Huth     *r = result;
3314fcf5ef2aSThomas Huth }
3315fcf5ef2aSThomas Huth 
3316fcf5ef2aSThomas Huth void helper_vncipher(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3317fcf5ef2aSThomas Huth {
3318fcf5ef2aSThomas Huth     /* This differs from what is written in ISA V2.07.  The RTL is */
3319fcf5ef2aSThomas Huth     /* incorrect and will be fixed in V2.07B.                      */
3320fcf5ef2aSThomas Huth     int i;
3321fcf5ef2aSThomas Huth     ppc_avr_t tmp;
3322fcf5ef2aSThomas Huth 
3323fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
3324fcf5ef2aSThomas Huth         tmp.AVRB(i) = b->AVRB(i) ^ AES_isbox[a->AVRB(AES_ishifts[i])];
3325fcf5ef2aSThomas Huth     }
3326fcf5ef2aSThomas Huth 
3327fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
3328fcf5ef2aSThomas Huth         r->AVRW(i) =
3329fcf5ef2aSThomas Huth             AES_imc[tmp.AVRB(4*i + 0)][0] ^
3330fcf5ef2aSThomas Huth             AES_imc[tmp.AVRB(4*i + 1)][1] ^
3331fcf5ef2aSThomas Huth             AES_imc[tmp.AVRB(4*i + 2)][2] ^
3332fcf5ef2aSThomas Huth             AES_imc[tmp.AVRB(4*i + 3)][3];
3333fcf5ef2aSThomas Huth     }
3334fcf5ef2aSThomas Huth }
3335fcf5ef2aSThomas Huth 
3336fcf5ef2aSThomas Huth void helper_vncipherlast(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b)
3337fcf5ef2aSThomas Huth {
3338fcf5ef2aSThomas Huth     ppc_avr_t result;
3339fcf5ef2aSThomas Huth     int i;
3340fcf5ef2aSThomas Huth 
3341fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
3342fcf5ef2aSThomas Huth         result.AVRB(i) = b->AVRB(i) ^ (AES_isbox[a->AVRB(AES_ishifts[i])]);
3343fcf5ef2aSThomas Huth     }
3344fcf5ef2aSThomas Huth     *r = result;
3345fcf5ef2aSThomas Huth }
3346fcf5ef2aSThomas Huth 
3347fcf5ef2aSThomas Huth #define ROTRu32(v, n) (((v) >> (n)) | ((v) << (32-n)))
3348fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
3349fcf5ef2aSThomas Huth #define EL_IDX(i) (i)
3350fcf5ef2aSThomas Huth #else
3351fcf5ef2aSThomas Huth #define EL_IDX(i) (3 - (i))
3352fcf5ef2aSThomas Huth #endif
3353fcf5ef2aSThomas Huth 
3354fcf5ef2aSThomas Huth void helper_vshasigmaw(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
3355fcf5ef2aSThomas Huth {
3356fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
3357fcf5ef2aSThomas Huth     int six = st_six & 0xF;
3358fcf5ef2aSThomas Huth     int i;
3359fcf5ef2aSThomas Huth 
3360fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u32) {
3361fcf5ef2aSThomas Huth         if (st == 0) {
3362fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
3363fcf5ef2aSThomas Huth                 r->u32[EL_IDX(i)] = ROTRu32(a->u32[EL_IDX(i)], 7) ^
3364fcf5ef2aSThomas Huth                                     ROTRu32(a->u32[EL_IDX(i)], 18) ^
3365fcf5ef2aSThomas Huth                                     (a->u32[EL_IDX(i)] >> 3);
3366fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
3367fcf5ef2aSThomas Huth                 r->u32[EL_IDX(i)] = ROTRu32(a->u32[EL_IDX(i)], 17) ^
3368fcf5ef2aSThomas Huth                                     ROTRu32(a->u32[EL_IDX(i)], 19) ^
3369fcf5ef2aSThomas Huth                                     (a->u32[EL_IDX(i)] >> 10);
3370fcf5ef2aSThomas Huth             }
3371fcf5ef2aSThomas Huth         } else { /* st == 1 */
3372fcf5ef2aSThomas Huth             if ((six & (0x8 >> i)) == 0) {
3373fcf5ef2aSThomas Huth                 r->u32[EL_IDX(i)] = ROTRu32(a->u32[EL_IDX(i)], 2) ^
3374fcf5ef2aSThomas Huth                                     ROTRu32(a->u32[EL_IDX(i)], 13) ^
3375fcf5ef2aSThomas Huth                                     ROTRu32(a->u32[EL_IDX(i)], 22);
3376fcf5ef2aSThomas Huth             } else { /* six.bit[i] == 1 */
3377fcf5ef2aSThomas Huth                 r->u32[EL_IDX(i)] = ROTRu32(a->u32[EL_IDX(i)], 6) ^
3378fcf5ef2aSThomas Huth                                     ROTRu32(a->u32[EL_IDX(i)], 11) ^
3379fcf5ef2aSThomas Huth                                     ROTRu32(a->u32[EL_IDX(i)], 25);
3380fcf5ef2aSThomas Huth             }
3381fcf5ef2aSThomas Huth         }
3382fcf5ef2aSThomas Huth     }
3383fcf5ef2aSThomas Huth }
3384fcf5ef2aSThomas Huth 
3385fcf5ef2aSThomas Huth #undef ROTRu32
3386fcf5ef2aSThomas Huth #undef EL_IDX
3387fcf5ef2aSThomas Huth 
3388fcf5ef2aSThomas Huth #define ROTRu64(v, n) (((v) >> (n)) | ((v) << (64-n)))
3389fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
3390fcf5ef2aSThomas Huth #define EL_IDX(i) (i)
3391fcf5ef2aSThomas Huth #else
3392fcf5ef2aSThomas Huth #define EL_IDX(i) (1 - (i))
3393fcf5ef2aSThomas Huth #endif
3394fcf5ef2aSThomas Huth 
3395fcf5ef2aSThomas Huth void helper_vshasigmad(ppc_avr_t *r,  ppc_avr_t *a, uint32_t st_six)
3396fcf5ef2aSThomas Huth {
3397fcf5ef2aSThomas Huth     int st = (st_six & 0x10) != 0;
3398fcf5ef2aSThomas Huth     int six = st_six & 0xF;
3399fcf5ef2aSThomas Huth     int i;
3400fcf5ef2aSThomas Huth 
3401fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u64) {
3402fcf5ef2aSThomas Huth         if (st == 0) {
3403fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2*i))) == 0) {
3404fcf5ef2aSThomas Huth                 r->u64[EL_IDX(i)] = ROTRu64(a->u64[EL_IDX(i)], 1) ^
3405fcf5ef2aSThomas Huth                                     ROTRu64(a->u64[EL_IDX(i)], 8) ^
3406fcf5ef2aSThomas Huth                                     (a->u64[EL_IDX(i)] >> 7);
3407fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
3408fcf5ef2aSThomas Huth                 r->u64[EL_IDX(i)] = ROTRu64(a->u64[EL_IDX(i)], 19) ^
3409fcf5ef2aSThomas Huth                                     ROTRu64(a->u64[EL_IDX(i)], 61) ^
3410fcf5ef2aSThomas Huth                                     (a->u64[EL_IDX(i)] >> 6);
3411fcf5ef2aSThomas Huth             }
3412fcf5ef2aSThomas Huth         } else { /* st == 1 */
3413fcf5ef2aSThomas Huth             if ((six & (0x8 >> (2*i))) == 0) {
3414fcf5ef2aSThomas Huth                 r->u64[EL_IDX(i)] = ROTRu64(a->u64[EL_IDX(i)], 28) ^
3415fcf5ef2aSThomas Huth                                     ROTRu64(a->u64[EL_IDX(i)], 34) ^
3416fcf5ef2aSThomas Huth                                     ROTRu64(a->u64[EL_IDX(i)], 39);
3417fcf5ef2aSThomas Huth             } else { /* six.bit[2*i] == 1 */
3418fcf5ef2aSThomas Huth                 r->u64[EL_IDX(i)] = ROTRu64(a->u64[EL_IDX(i)], 14) ^
3419fcf5ef2aSThomas Huth                                     ROTRu64(a->u64[EL_IDX(i)], 18) ^
3420fcf5ef2aSThomas Huth                                     ROTRu64(a->u64[EL_IDX(i)], 41);
3421fcf5ef2aSThomas Huth             }
3422fcf5ef2aSThomas Huth         }
3423fcf5ef2aSThomas Huth     }
3424fcf5ef2aSThomas Huth }
3425fcf5ef2aSThomas Huth 
3426fcf5ef2aSThomas Huth #undef ROTRu64
3427fcf5ef2aSThomas Huth #undef EL_IDX
3428fcf5ef2aSThomas Huth 
3429fcf5ef2aSThomas Huth void helper_vpermxor(ppc_avr_t *r,  ppc_avr_t *a, ppc_avr_t *b, ppc_avr_t *c)
3430fcf5ef2aSThomas Huth {
3431fcf5ef2aSThomas Huth     ppc_avr_t result;
3432fcf5ef2aSThomas Huth     int i;
3433fcf5ef2aSThomas Huth 
3434fcf5ef2aSThomas Huth     VECTOR_FOR_INORDER_I(i, u8) {
3435fcf5ef2aSThomas Huth         int indexA = c->u8[i] >> 4;
3436fcf5ef2aSThomas Huth         int indexB = c->u8[i] & 0xF;
3437fcf5ef2aSThomas Huth #if defined(HOST_WORDS_BIGENDIAN)
3438fcf5ef2aSThomas Huth         result.u8[i] = a->u8[indexA] ^ b->u8[indexB];
3439fcf5ef2aSThomas Huth #else
3440fcf5ef2aSThomas Huth         result.u8[i] = a->u8[15-indexA] ^ b->u8[15-indexB];
3441fcf5ef2aSThomas Huth #endif
3442fcf5ef2aSThomas Huth     }
3443fcf5ef2aSThomas Huth     *r = result;
3444fcf5ef2aSThomas Huth }
3445fcf5ef2aSThomas Huth 
3446fcf5ef2aSThomas Huth #undef VECTOR_FOR_INORDER_I
3447fcf5ef2aSThomas Huth #undef HI_IDX
3448fcf5ef2aSThomas Huth #undef LO_IDX
3449fcf5ef2aSThomas Huth 
3450fcf5ef2aSThomas Huth /*****************************************************************************/
3451fcf5ef2aSThomas Huth /* SPE extension helpers */
3452fcf5ef2aSThomas Huth /* Use a table to make this quicker */
3453fcf5ef2aSThomas Huth static const uint8_t hbrev[16] = {
3454fcf5ef2aSThomas Huth     0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
3455fcf5ef2aSThomas Huth     0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
3456fcf5ef2aSThomas Huth };
3457fcf5ef2aSThomas Huth 
3458fcf5ef2aSThomas Huth static inline uint8_t byte_reverse(uint8_t val)
3459fcf5ef2aSThomas Huth {
3460fcf5ef2aSThomas Huth     return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
3461fcf5ef2aSThomas Huth }
3462fcf5ef2aSThomas Huth 
3463fcf5ef2aSThomas Huth static inline uint32_t word_reverse(uint32_t val)
3464fcf5ef2aSThomas Huth {
3465fcf5ef2aSThomas Huth     return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
3466fcf5ef2aSThomas Huth         (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
3467fcf5ef2aSThomas Huth }
3468fcf5ef2aSThomas Huth 
3469fcf5ef2aSThomas Huth #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
3470fcf5ef2aSThomas Huth target_ulong helper_brinc(target_ulong arg1, target_ulong arg2)
3471fcf5ef2aSThomas Huth {
3472fcf5ef2aSThomas Huth     uint32_t a, b, d, mask;
3473fcf5ef2aSThomas Huth 
3474fcf5ef2aSThomas Huth     mask = UINT32_MAX >> (32 - MASKBITS);
3475fcf5ef2aSThomas Huth     a = arg1 & mask;
3476fcf5ef2aSThomas Huth     b = arg2 & mask;
3477fcf5ef2aSThomas Huth     d = word_reverse(1 + word_reverse(a | ~b));
3478fcf5ef2aSThomas Huth     return (arg1 & ~mask) | (d & b);
3479fcf5ef2aSThomas Huth }
3480fcf5ef2aSThomas Huth 
3481fcf5ef2aSThomas Huth uint32_t helper_cntlsw32(uint32_t val)
3482fcf5ef2aSThomas Huth {
3483fcf5ef2aSThomas Huth     if (val & 0x80000000) {
3484fcf5ef2aSThomas Huth         return clz32(~val);
3485fcf5ef2aSThomas Huth     } else {
3486fcf5ef2aSThomas Huth         return clz32(val);
3487fcf5ef2aSThomas Huth     }
3488fcf5ef2aSThomas Huth }
3489fcf5ef2aSThomas Huth 
3490fcf5ef2aSThomas Huth uint32_t helper_cntlzw32(uint32_t val)
3491fcf5ef2aSThomas Huth {
3492fcf5ef2aSThomas Huth     return clz32(val);
3493fcf5ef2aSThomas Huth }
3494fcf5ef2aSThomas Huth 
3495fcf5ef2aSThomas Huth /* 440 specific */
3496fcf5ef2aSThomas Huth target_ulong helper_dlmzb(CPUPPCState *env, target_ulong high,
3497fcf5ef2aSThomas Huth                           target_ulong low, uint32_t update_Rc)
3498fcf5ef2aSThomas Huth {
3499fcf5ef2aSThomas Huth     target_ulong mask;
3500fcf5ef2aSThomas Huth     int i;
3501fcf5ef2aSThomas Huth 
3502fcf5ef2aSThomas Huth     i = 1;
3503fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
3504fcf5ef2aSThomas Huth         if ((high & mask) == 0) {
3505fcf5ef2aSThomas Huth             if (update_Rc) {
3506fcf5ef2aSThomas Huth                 env->crf[0] = 0x4;
3507fcf5ef2aSThomas Huth             }
3508fcf5ef2aSThomas Huth             goto done;
3509fcf5ef2aSThomas Huth         }
3510fcf5ef2aSThomas Huth         i++;
3511fcf5ef2aSThomas Huth     }
3512fcf5ef2aSThomas Huth     for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
3513fcf5ef2aSThomas Huth         if ((low & mask) == 0) {
3514fcf5ef2aSThomas Huth             if (update_Rc) {
3515fcf5ef2aSThomas Huth                 env->crf[0] = 0x8;
3516fcf5ef2aSThomas Huth             }
3517fcf5ef2aSThomas Huth             goto done;
3518fcf5ef2aSThomas Huth         }
3519fcf5ef2aSThomas Huth         i++;
3520fcf5ef2aSThomas Huth     }
3521fcf5ef2aSThomas Huth     i = 8;
3522fcf5ef2aSThomas Huth     if (update_Rc) {
3523fcf5ef2aSThomas Huth         env->crf[0] = 0x2;
3524fcf5ef2aSThomas Huth     }
3525fcf5ef2aSThomas Huth  done:
3526fcf5ef2aSThomas Huth     env->xer = (env->xer & ~0x7F) | i;
3527fcf5ef2aSThomas Huth     if (update_Rc) {
3528fcf5ef2aSThomas Huth         env->crf[0] |= xer_so;
3529fcf5ef2aSThomas Huth     }
3530fcf5ef2aSThomas Huth     return i;
3531fcf5ef2aSThomas Huth }
3532