2 * PowerPC integer and vector emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
23 #include "qemu/host-utils.h"
24 #include "qemu/main-loop.h"
25 #include "exec/helper-proto.h"
26 #include "crypto/aes.h"
27 #include "fpu/softfloat.h"
28 #include "qapi/error.h"
29 #include "qemu/guest-random.h"
31 #include "helper_regs.h"
32 /*****************************************************************************/
33 /* Fixed point operations helpers */
35 static inline void helper_update_ov_legacy(CPUPPCState
*env
, int ov
)
38 env
->so
= env
->ov
= 1;
44 target_ulong
helper_divweu(CPUPPCState
*env
, target_ulong ra
, target_ulong rb
,
50 uint64_t dividend
= (uint64_t)ra
<< 32;
51 uint64_t divisor
= (uint32_t)rb
;
53 if (unlikely(divisor
== 0)) {
56 rt
= dividend
/ divisor
;
57 overflow
= rt
> UINT32_MAX
;
60 if (unlikely(overflow
)) {
61 rt
= 0; /* Undefined */
65 helper_update_ov_legacy(env
, overflow
);
68 return (target_ulong
)rt
;
71 target_ulong
helper_divwe(CPUPPCState
*env
, target_ulong ra
, target_ulong rb
,
77 int64_t dividend
= (int64_t)ra
<< 32;
78 int64_t divisor
= (int64_t)((int32_t)rb
);
80 if (unlikely((divisor
== 0) ||
81 ((divisor
== -1ull) && (dividend
== INT64_MIN
)))) {
84 rt
= dividend
/ divisor
;
85 overflow
= rt
!= (int32_t)rt
;
88 if (unlikely(overflow
)) {
89 rt
= 0; /* Undefined */
93 helper_update_ov_legacy(env
, overflow
);
96 return (target_ulong
)rt
;
99 #if defined(TARGET_PPC64)
101 uint64_t helper_divdeu(CPUPPCState
*env
, uint64_t ra
, uint64_t rb
, uint32_t oe
)
106 overflow
= divu128(&rt
, &ra
, rb
);
108 if (unlikely(overflow
)) {
109 rt
= 0; /* Undefined */
113 helper_update_ov_legacy(env
, overflow
);
119 uint64_t helper_divde(CPUPPCState
*env
, uint64_t rau
, uint64_t rbu
, uint32_t oe
)
122 int64_t ra
= (int64_t)rau
;
123 int64_t rb
= (int64_t)rbu
;
124 int overflow
= divs128(&rt
, &ra
, rb
);
126 if (unlikely(overflow
)) {
127 rt
= 0; /* Undefined */
131 helper_update_ov_legacy(env
, overflow
);
140 #if defined(TARGET_PPC64)
141 /* if x = 0xab, returns 0xababababababababa */
142 #define pattern(x) (((x) & 0xff) * (~(target_ulong)0 / 0xff))
145 * subtract 1 from each byte, and with inverse, check if MSB is set at each
147 * i.e. ((0x00 - 0x01) & ~(0x00)) & 0x80
148 * (0xFF & 0xFF) & 0x80 = 0x80 (zero found)
150 #define haszero(v) (((v) - pattern(0x01)) & ~(v) & pattern(0x80))
152 /* When you XOR the pattern and there is a match, that byte will be zero */
153 #define hasvalue(x, n) (haszero((x) ^ pattern(n)))
155 uint32_t helper_cmpeqb(target_ulong ra
, target_ulong rb
)
157 return hasvalue(rb
, ra
) ? CRF_GT
: 0;
165 * Return a random number.
167 uint64_t helper_darn32(void)
172 if (qemu_guest_getrandom(&ret
, sizeof(ret
), &err
) < 0) {
173 qemu_log_mask(LOG_UNIMP
, "darn: Crypto failure: %s",
174 error_get_pretty(err
));
182 uint64_t helper_darn64(void)
187 if (qemu_guest_getrandom(&ret
, sizeof(ret
), &err
) < 0) {
188 qemu_log_mask(LOG_UNIMP
, "darn: Crypto failure: %s",
189 error_get_pretty(err
));
197 uint64_t helper_bpermd(uint64_t rs
, uint64_t rb
)
202 for (i
= 0; i
< 8; i
++) {
203 int index
= (rs
>> (i
* 8)) & 0xFF;
205 if (rb
& PPC_BIT(index
)) {
215 target_ulong
helper_cmpb(target_ulong rs
, target_ulong rb
)
217 target_ulong mask
= 0xff;
221 for (i
= 0; i
< sizeof(target_ulong
); i
++) {
222 if ((rs
& mask
) == (rb
& mask
)) {
230 /* shift right arithmetic helper */
231 target_ulong
helper_sraw(CPUPPCState
*env
, target_ulong value
,
236 if (likely(!(shift
& 0x20))) {
237 if (likely((uint32_t)shift
!= 0)) {
239 ret
= (int32_t)value
>> shift
;
240 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
241 env
->ca32
= env
->ca
= 0;
243 env
->ca32
= env
->ca
= 1;
246 ret
= (int32_t)value
;
247 env
->ca32
= env
->ca
= 0;
250 ret
= (int32_t)value
>> 31;
251 env
->ca32
= env
->ca
= (ret
!= 0);
253 return (target_long
)ret
;
256 #if defined(TARGET_PPC64)
257 target_ulong
helper_srad(CPUPPCState
*env
, target_ulong value
,
262 if (likely(!(shift
& 0x40))) {
263 if (likely((uint64_t)shift
!= 0)) {
265 ret
= (int64_t)value
>> shift
;
266 if (likely(ret
>= 0 || (value
& ((1ULL << shift
) - 1)) == 0)) {
267 env
->ca32
= env
->ca
= 0;
269 env
->ca32
= env
->ca
= 1;
272 ret
= (int64_t)value
;
273 env
->ca32
= env
->ca
= 0;
276 ret
= (int64_t)value
>> 63;
277 env
->ca32
= env
->ca
= (ret
!= 0);
283 #if defined(TARGET_PPC64)
284 target_ulong
helper_popcntb(target_ulong val
)
286 /* Note that we don't fold past bytes */
287 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
288 0x5555555555555555ULL
);
289 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
290 0x3333333333333333ULL
);
291 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
292 0x0f0f0f0f0f0f0f0fULL
);
296 target_ulong
helper_popcntw(target_ulong val
)
298 /* Note that we don't fold past words. */
299 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
300 0x5555555555555555ULL
);
301 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
302 0x3333333333333333ULL
);
303 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
304 0x0f0f0f0f0f0f0f0fULL
);
305 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) &
306 0x00ff00ff00ff00ffULL
);
307 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) &
308 0x0000ffff0000ffffULL
);
312 target_ulong
helper_popcntb(target_ulong val
)
314 /* Note that we don't fold past bytes */
315 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
316 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
317 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
322 /*****************************************************************************/
323 /* PowerPC 601 specific instructions (POWER bridge) */
324 target_ulong
helper_div(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
)
326 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
328 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
329 (int32_t)arg2
== 0) {
330 env
->spr
[SPR_MQ
] = 0;
333 env
->spr
[SPR_MQ
] = tmp
% arg2
;
334 return tmp
/ (int32_t)arg2
;
338 target_ulong
helper_divo(CPUPPCState
*env
, target_ulong arg1
,
341 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
343 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
344 (int32_t)arg2
== 0) {
345 env
->so
= env
->ov
= 1;
346 env
->spr
[SPR_MQ
] = 0;
349 env
->spr
[SPR_MQ
] = tmp
% arg2
;
350 tmp
/= (int32_t)arg2
;
351 if ((int32_t)tmp
!= tmp
) {
352 env
->so
= env
->ov
= 1;
360 target_ulong
helper_divs(CPUPPCState
*env
, target_ulong arg1
,
363 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
364 (int32_t)arg2
== 0) {
365 env
->spr
[SPR_MQ
] = 0;
368 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
369 return (int32_t)arg1
/ (int32_t)arg2
;
373 target_ulong
helper_divso(CPUPPCState
*env
, target_ulong arg1
,
376 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
377 (int32_t)arg2
== 0) {
378 env
->so
= env
->ov
= 1;
379 env
->spr
[SPR_MQ
] = 0;
383 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
384 return (int32_t)arg1
/ (int32_t)arg2
;
388 /*****************************************************************************/
389 /* 602 specific instructions */
390 /* mfrom is the most crazy instruction ever seen, imho ! */
391 /* Real implementation uses a ROM table. Do the same */
393 * Extremely decomposed:
395 * return 256 * log10(10 + 1.0) + 0.5
397 #if !defined(CONFIG_USER_ONLY)
398 target_ulong
helper_602_mfrom(target_ulong arg
)
400 if (likely(arg
< 602)) {
401 #include "mfrom_table.c.inc"
402 return mfrom_ROM_table
[arg
];
409 /*****************************************************************************/
410 /* Altivec extension helpers */
411 #if defined(HOST_WORDS_BIGENDIAN)
412 #define VECTOR_FOR_INORDER_I(index, element) \
413 for (index = 0; index < ARRAY_SIZE(r->element); index++)
415 #define VECTOR_FOR_INORDER_I(index, element) \
416 for (index = ARRAY_SIZE(r->element) - 1; index >= 0; index--)
419 /* Saturating arithmetic helpers. */
420 #define SATCVT(from, to, from_type, to_type, min, max) \
421 static inline to_type cvt##from##to(from_type x, int *sat) \
425 if (x < (from_type)min) { \
428 } else if (x > (from_type)max) { \
436 #define SATCVTU(from, to, from_type, to_type, min, max) \
437 static inline to_type cvt##from##to(from_type x, int *sat) \
441 if (x > (from_type)max) { \
449 SATCVT(sh
, sb
, int16_t, int8_t, INT8_MIN
, INT8_MAX
)
450 SATCVT(sw
, sh
, int32_t, int16_t, INT16_MIN
, INT16_MAX
)
451 SATCVT(sd
, sw
, int64_t, int32_t, INT32_MIN
, INT32_MAX
)
453 SATCVTU(uh
, ub
, uint16_t, uint8_t, 0, UINT8_MAX
)
454 SATCVTU(uw
, uh
, uint32_t, uint16_t, 0, UINT16_MAX
)
455 SATCVTU(ud
, uw
, uint64_t, uint32_t, 0, UINT32_MAX
)
456 SATCVT(sh
, ub
, int16_t, uint8_t, 0, UINT8_MAX
)
457 SATCVT(sw
, uh
, int32_t, uint16_t, 0, UINT16_MAX
)
458 SATCVT(sd
, uw
, int64_t, uint32_t, 0, UINT32_MAX
)
462 void helper_mtvscr(CPUPPCState
*env
, uint32_t vscr
)
464 env
->vscr
= vscr
& ~(1u << VSCR_SAT
);
465 /* Which bit we set is completely arbitrary, but clear the rest. */
466 env
->vscr_sat
.u64
[0] = vscr
& (1u << VSCR_SAT
);
467 env
->vscr_sat
.u64
[1] = 0;
468 set_flush_to_zero((vscr
>> VSCR_NJ
) & 1, &env
->vec_status
);
471 uint32_t helper_mfvscr(CPUPPCState
*env
)
473 uint32_t sat
= (env
->vscr_sat
.u64
[0] | env
->vscr_sat
.u64
[1]) != 0;
474 return env
->vscr
| (sat
<< VSCR_SAT
);
477 static inline void set_vscr_sat(CPUPPCState
*env
)
479 /* The choice of non-zero value is arbitrary. */
480 env
->vscr_sat
.u32
[0] = 1;
483 void helper_vaddcuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
487 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
488 r
->u32
[i
] = ~a
->u32
[i
] < b
->u32
[i
];
493 void helper_vprtybw(ppc_avr_t
*r
, ppc_avr_t
*b
)
496 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
497 uint64_t res
= b
->u32
[i
] ^ (b
->u32
[i
] >> 16);
504 void helper_vprtybd(ppc_avr_t
*r
, ppc_avr_t
*b
)
507 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
508 uint64_t res
= b
->u64
[i
] ^ (b
->u64
[i
] >> 32);
516 void helper_vprtybq(ppc_avr_t
*r
, ppc_avr_t
*b
)
518 uint64_t res
= b
->u64
[0] ^ b
->u64
[1];
522 r
->VsrD(1) = res
& 1;
526 #define VARITHFP(suffix, func) \
527 void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
532 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
533 r->f32[i] = func(a->f32[i], b->f32[i], &env->vec_status); \
536 VARITHFP(addfp
, float32_add
)
537 VARITHFP(subfp
, float32_sub
)
538 VARITHFP(minfp
, float32_min
)
539 VARITHFP(maxfp
, float32_max
)
542 #define VARITHFPFMA(suffix, type) \
543 void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
544 ppc_avr_t *b, ppc_avr_t *c) \
547 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
548 r->f32[i] = float32_muladd(a->f32[i], c->f32[i], b->f32[i], \
549 type, &env->vec_status); \
552 VARITHFPFMA(maddfp
, 0);
553 VARITHFPFMA(nmsubfp
, float_muladd_negate_result
| float_muladd_negate_c
);
556 #define VARITHSAT_CASE(type, op, cvt, element) \
558 type result = (type)a->element[i] op (type)b->element[i]; \
559 r->element[i] = cvt(result, &sat); \
562 #define VARITHSAT_DO(name, op, optype, cvt, element) \
563 void helper_v##name(ppc_avr_t *r, ppc_avr_t *vscr_sat, \
564 ppc_avr_t *a, ppc_avr_t *b, uint32_t desc) \
569 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
570 VARITHSAT_CASE(optype, op, cvt, element); \
573 vscr_sat->u32[0] = 1; \
576 #define VARITHSAT_SIGNED(suffix, element, optype, cvt) \
577 VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element) \
578 VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
579 #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt) \
580 VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element) \
581 VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
582 VARITHSAT_SIGNED(b
, s8
, int16_t, cvtshsb
)
583 VARITHSAT_SIGNED(h
, s16
, int32_t, cvtswsh
)
584 VARITHSAT_SIGNED(w
, s32
, int64_t, cvtsdsw
)
585 VARITHSAT_UNSIGNED(b
, u8
, uint16_t, cvtshub
)
586 VARITHSAT_UNSIGNED(h
, u16
, uint32_t, cvtswuh
)
587 VARITHSAT_UNSIGNED(w
, u32
, uint64_t, cvtsduw
)
588 #undef VARITHSAT_CASE
590 #undef VARITHSAT_SIGNED
591 #undef VARITHSAT_UNSIGNED
593 #define VAVG_DO(name, element, etype) \
594 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
598 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
599 etype x = (etype)a->element[i] + (etype)b->element[i] + 1; \
600 r->element[i] = x >> 1; \
604 #define VAVG(type, signed_element, signed_type, unsigned_element, \
606 VAVG_DO(avgs##type, signed_element, signed_type) \
607 VAVG_DO(avgu##type, unsigned_element, unsigned_type)
608 VAVG(b
, s8
, int16_t, u8
, uint16_t)
609 VAVG(h
, s16
, int32_t, u16
, uint32_t)
610 VAVG(w
, s32
, int64_t, u32
, uint64_t)
614 #define VABSDU_DO(name, element) \
615 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
619 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
620 r->element[i] = (a->element[i] > b->element[i]) ? \
621 (a->element[i] - b->element[i]) : \
622 (b->element[i] - a->element[i]); \
627 * VABSDU - Vector absolute difference unsigned
628 * name - instruction mnemonic suffix (b: byte, h: halfword, w: word)
629 * element - element type to access from vector
631 #define VABSDU(type, element) \
632 VABSDU_DO(absdu##type, element)
639 #define VCF(suffix, cvt, element) \
640 void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r, \
641 ppc_avr_t *b, uint32_t uim) \
645 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
646 float32 t = cvt(b->element[i], &env->vec_status); \
647 r->f32[i] = float32_scalbn(t, -uim, &env->vec_status); \
650 VCF(ux
, uint32_to_float32
, u32
)
651 VCF(sx
, int32_to_float32
, s32
)
654 #define VCMP_DO(suffix, compare, element, record) \
655 void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r, \
656 ppc_avr_t *a, ppc_avr_t *b) \
658 uint64_t ones = (uint64_t)-1; \
659 uint64_t all = ones; \
663 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
664 uint64_t result = (a->element[i] compare b->element[i] ? \
666 switch (sizeof(a->element[0])) { \
668 r->u64[i] = result; \
671 r->u32[i] = result; \
674 r->u16[i] = result; \
684 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
687 #define VCMP(suffix, compare, element) \
688 VCMP_DO(suffix, compare, element, 0) \
689 VCMP_DO(suffix##_dot, compare, element, 1)
705 #define VCMPNE_DO(suffix, element, etype, cmpzero, record) \
706 void helper_vcmpne##suffix(CPUPPCState *env, ppc_avr_t *r, \
707 ppc_avr_t *a, ppc_avr_t *b) \
709 etype ones = (etype)-1; \
711 etype result, none = 0; \
714 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
716 result = ((a->element[i] == 0) \
717 || (b->element[i] == 0) \
718 || (a->element[i] != b->element[i]) ? \
721 result = (a->element[i] != b->element[i]) ? ones : 0x0; \
723 r->element[i] = result; \
728 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
733 * VCMPNEZ - Vector compare not equal to zero
734 * suffix - instruction mnemonic suffix (b: byte, h: halfword, w: word)
735 * element - element type to access from vector
737 #define VCMPNE(suffix, element, etype, cmpzero) \
738 VCMPNE_DO(suffix, element, etype, cmpzero, 0) \
739 VCMPNE_DO(suffix##_dot, element, etype, cmpzero, 1)
740 VCMPNE(zb
, u8
, uint8_t, 1)
741 VCMPNE(zh
, u16
, uint16_t, 1)
742 VCMPNE(zw
, u32
, uint32_t, 1)
743 VCMPNE(b
, u8
, uint8_t, 0)
744 VCMPNE(h
, u16
, uint16_t, 0)
745 VCMPNE(w
, u32
, uint32_t, 0)
749 #define VCMPFP_DO(suffix, compare, order, record) \
750 void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r, \
751 ppc_avr_t *a, ppc_avr_t *b) \
753 uint32_t ones = (uint32_t)-1; \
754 uint32_t all = ones; \
758 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
760 FloatRelation rel = \
761 float32_compare_quiet(a->f32[i], b->f32[i], \
763 if (rel == float_relation_unordered) { \
765 } else if (rel compare order) { \
770 r->u32[i] = result; \
775 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
778 #define VCMPFP(suffix, compare, order) \
779 VCMPFP_DO(suffix, compare, order, 0) \
780 VCMPFP_DO(suffix##_dot, compare, order, 1)
781 VCMPFP(eqfp
, ==, float_relation_equal
)
782 VCMPFP(gefp
, !=, float_relation_less
)
783 VCMPFP(gtfp
, ==, float_relation_greater
)
787 static inline void vcmpbfp_internal(CPUPPCState
*env
, ppc_avr_t
*r
,
788 ppc_avr_t
*a
, ppc_avr_t
*b
, int record
)
793 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
794 FloatRelation le_rel
= float32_compare_quiet(a
->f32
[i
], b
->f32
[i
],
796 if (le_rel
== float_relation_unordered
) {
797 r
->u32
[i
] = 0xc0000000;
800 float32 bneg
= float32_chs(b
->f32
[i
]);
801 FloatRelation ge_rel
= float32_compare_quiet(a
->f32
[i
], bneg
,
803 int le
= le_rel
!= float_relation_greater
;
804 int ge
= ge_rel
!= float_relation_less
;
806 r
->u32
[i
] = ((!le
) << 31) | ((!ge
) << 30);
807 all_in
|= (!le
| !ge
);
811 env
->crf
[6] = (all_in
== 0) << 1;
815 void helper_vcmpbfp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
817 vcmpbfp_internal(env
, r
, a
, b
, 0);
820 void helper_vcmpbfp_dot(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
823 vcmpbfp_internal(env
, r
, a
, b
, 1);
826 #define VCT(suffix, satcvt, element) \
827 void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r, \
828 ppc_avr_t *b, uint32_t uim) \
832 float_status s = env->vec_status; \
834 set_float_rounding_mode(float_round_to_zero, &s); \
835 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
836 if (float32_is_any_nan(b->f32[i])) { \
839 float64 t = float32_to_float64(b->f32[i], &s); \
842 t = float64_scalbn(t, uim, &s); \
843 j = float64_to_int64(t, &s); \
844 r->element[i] = satcvt(j, &sat); \
851 VCT(uxs
, cvtsduw
, u32
)
852 VCT(sxs
, cvtsdsw
, s32
)
855 target_ulong
helper_vclzlsbb(ppc_avr_t
*r
)
857 target_ulong count
= 0;
859 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
860 if (r
->VsrB(i
) & 0x01) {
868 target_ulong
helper_vctzlsbb(ppc_avr_t
*r
)
870 target_ulong count
= 0;
872 for (i
= ARRAY_SIZE(r
->u8
) - 1; i
>= 0; i
--) {
873 if (r
->VsrB(i
) & 0x01) {
881 void helper_vmhaddshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
882 ppc_avr_t
*b
, ppc_avr_t
*c
)
887 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
888 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
889 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
891 r
->s16
[i
] = cvtswsh(t
, &sat
);
899 void helper_vmhraddshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
900 ppc_avr_t
*b
, ppc_avr_t
*c
)
905 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
906 int32_t prod
= a
->s16
[i
] * b
->s16
[i
] + 0x00004000;
907 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
908 r
->s16
[i
] = cvtswsh(t
, &sat
);
916 void helper_vmladduhm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
920 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
921 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
922 r
->s16
[i
] = (int16_t) (prod
+ c
->s16
[i
]);
926 #define VMRG_DO(name, element, access, ofs) \
927 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
930 int i, half = ARRAY_SIZE(r->element) / 2; \
932 for (i = 0; i < half; i++) { \
933 result.access(i * 2 + 0) = a->access(i + ofs); \
934 result.access(i * 2 + 1) = b->access(i + ofs); \
939 #define VMRG(suffix, element, access) \
940 VMRG_DO(mrgl##suffix, element, access, half) \
941 VMRG_DO(mrgh##suffix, element, access, 0)
948 void helper_vmsummbm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
949 ppc_avr_t
*b
, ppc_avr_t
*c
)
954 for (i
= 0; i
< ARRAY_SIZE(r
->s8
); i
++) {
955 prod
[i
] = (int32_t)a
->s8
[i
] * b
->u8
[i
];
958 VECTOR_FOR_INORDER_I(i
, s32
) {
959 r
->s32
[i
] = c
->s32
[i
] + prod
[4 * i
] + prod
[4 * i
+ 1] +
960 prod
[4 * i
+ 2] + prod
[4 * i
+ 3];
964 void helper_vmsumshm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
965 ppc_avr_t
*b
, ppc_avr_t
*c
)
970 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
971 prod
[i
] = a
->s16
[i
] * b
->s16
[i
];
974 VECTOR_FOR_INORDER_I(i
, s32
) {
975 r
->s32
[i
] = c
->s32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
979 void helper_vmsumshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
980 ppc_avr_t
*b
, ppc_avr_t
*c
)
986 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
987 prod
[i
] = (int32_t)a
->s16
[i
] * b
->s16
[i
];
990 VECTOR_FOR_INORDER_I(i
, s32
) {
991 int64_t t
= (int64_t)c
->s32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
993 r
->u32
[i
] = cvtsdsw(t
, &sat
);
1001 void helper_vmsumubm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1002 ppc_avr_t
*b
, ppc_avr_t
*c
)
1007 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1008 prod
[i
] = a
->u8
[i
] * b
->u8
[i
];
1011 VECTOR_FOR_INORDER_I(i
, u32
) {
1012 r
->u32
[i
] = c
->u32
[i
] + prod
[4 * i
] + prod
[4 * i
+ 1] +
1013 prod
[4 * i
+ 2] + prod
[4 * i
+ 3];
1017 void helper_vmsumuhm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1018 ppc_avr_t
*b
, ppc_avr_t
*c
)
1023 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
1024 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
1027 VECTOR_FOR_INORDER_I(i
, u32
) {
1028 r
->u32
[i
] = c
->u32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
1032 void helper_vmsumuhs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1033 ppc_avr_t
*b
, ppc_avr_t
*c
)
1039 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
1040 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
1043 VECTOR_FOR_INORDER_I(i
, s32
) {
1044 uint64_t t
= (uint64_t)c
->u32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
1046 r
->u32
[i
] = cvtuduw(t
, &sat
);
1054 #define VMUL_DO_EVN(name, mul_element, mul_access, prod_access, cast) \
1055 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1059 for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) { \
1060 r->prod_access(i >> 1) = (cast)a->mul_access(i) * \
1061 (cast)b->mul_access(i); \
1065 #define VMUL_DO_ODD(name, mul_element, mul_access, prod_access, cast) \
1066 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1070 for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) { \
1071 r->prod_access(i >> 1) = (cast)a->mul_access(i + 1) * \
1072 (cast)b->mul_access(i + 1); \
1076 #define VMUL(suffix, mul_element, mul_access, prod_access, cast) \
1077 VMUL_DO_EVN(mule##suffix, mul_element, mul_access, prod_access, cast) \
1078 VMUL_DO_ODD(mulo##suffix, mul_element, mul_access, prod_access, cast)
1079 VMUL(sb
, s8
, VsrSB
, VsrSH
, int16_t)
1080 VMUL(sh
, s16
, VsrSH
, VsrSW
, int32_t)
1081 VMUL(sw
, s32
, VsrSW
, VsrSD
, int64_t)
1082 VMUL(ub
, u8
, VsrB
, VsrH
, uint16_t)
1083 VMUL(uh
, u16
, VsrH
, VsrW
, uint32_t)
1084 VMUL(uw
, u32
, VsrW
, VsrD
, uint64_t)
1089 void helper_vmulhsw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1093 for (i
= 0; i
< 4; i
++) {
1094 r
->s32
[i
] = (int32_t)(((int64_t)a
->s32
[i
] * (int64_t)b
->s32
[i
]) >> 32);
1098 void helper_vmulhuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1102 for (i
= 0; i
< 4; i
++) {
1103 r
->u32
[i
] = (uint32_t)(((uint64_t)a
->u32
[i
] *
1104 (uint64_t)b
->u32
[i
]) >> 32);
1108 void helper_vmulhsd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1112 muls64(&discard
, &r
->u64
[0], a
->s64
[0], b
->s64
[0]);
1113 muls64(&discard
, &r
->u64
[1], a
->s64
[1], b
->s64
[1]);
1116 void helper_vmulhud(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1120 mulu64(&discard
, &r
->u64
[0], a
->u64
[0], b
->u64
[0]);
1121 mulu64(&discard
, &r
->u64
[1], a
->u64
[1], b
->u64
[1]);
1124 void helper_vperm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1130 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1131 int s
= c
->VsrB(i
) & 0x1f;
1132 int index
= s
& 0xf;
1135 result
.VsrB(i
) = b
->VsrB(index
);
1137 result
.VsrB(i
) = a
->VsrB(index
);
1143 void helper_vpermr(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1149 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1150 int s
= c
->VsrB(i
) & 0x1f;
1151 int index
= 15 - (s
& 0xf);
1154 result
.VsrB(i
) = a
->VsrB(index
);
1156 result
.VsrB(i
) = b
->VsrB(index
);
1162 #if defined(HOST_WORDS_BIGENDIAN)
1163 #define VBPERMQ_INDEX(avr, i) ((avr)->u8[(i)])
1164 #define VBPERMD_INDEX(i) (i)
1165 #define VBPERMQ_DW(index) (((index) & 0x40) != 0)
1166 #define EXTRACT_BIT(avr, i, index) (extract64((avr)->u64[i], index, 1))
1168 #define VBPERMQ_INDEX(avr, i) ((avr)->u8[15 - (i)])
1169 #define VBPERMD_INDEX(i) (1 - i)
1170 #define VBPERMQ_DW(index) (((index) & 0x40) == 0)
1171 #define EXTRACT_BIT(avr, i, index) \
1172 (extract64((avr)->u64[1 - i], 63 - index, 1))
1175 void helper_vbpermd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1178 ppc_avr_t result
= { .u64
= { 0, 0 } };
1179 VECTOR_FOR_INORDER_I(i
, u64
) {
1180 for (j
= 0; j
< 8; j
++) {
1181 int index
= VBPERMQ_INDEX(b
, (i
* 8) + j
);
1182 if (index
< 64 && EXTRACT_BIT(a
, i
, index
)) {
1183 result
.u64
[VBPERMD_INDEX(i
)] |= (0x80 >> j
);
1190 void helper_vbpermq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1195 VECTOR_FOR_INORDER_I(i
, u8
) {
1196 int index
= VBPERMQ_INDEX(b
, i
);
1199 uint64_t mask
= (1ull << (63 - (index
& 0x3F)));
1200 if (a
->u64
[VBPERMQ_DW(index
)] & mask
) {
1201 perm
|= (0x8000 >> i
);
1210 #undef VBPERMQ_INDEX
1213 #define PMSUM(name, srcfld, trgfld, trgtyp) \
1214 void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1217 trgtyp prod[sizeof(ppc_avr_t) / sizeof(a->srcfld[0])]; \
1219 VECTOR_FOR_INORDER_I(i, srcfld) { \
1221 for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) { \
1222 if (a->srcfld[i] & (1ull << j)) { \
1223 prod[i] ^= ((trgtyp)b->srcfld[i] << j); \
1228 VECTOR_FOR_INORDER_I(i, trgfld) { \
1229 r->trgfld[i] = prod[2 * i] ^ prod[2 * i + 1]; \
1233 PMSUM(vpmsumb
, u8
, u16
, uint16_t)
1234 PMSUM(vpmsumh
, u16
, u32
, uint32_t)
1235 PMSUM(vpmsumw
, u32
, u64
, uint64_t)
1237 void helper_vpmsumd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1240 #ifdef CONFIG_INT128
1242 __uint128_t prod
[2];
1244 VECTOR_FOR_INORDER_I(i
, u64
) {
1246 for (j
= 0; j
< 64; j
++) {
1247 if (a
->u64
[i
] & (1ull << j
)) {
1248 prod
[i
] ^= (((__uint128_t
)b
->u64
[i
]) << j
);
1253 r
->u128
= prod
[0] ^ prod
[1];
1259 VECTOR_FOR_INORDER_I(i
, u64
) {
1260 prod
[i
].VsrD(1) = prod
[i
].VsrD(0) = 0;
1261 for (j
= 0; j
< 64; j
++) {
1262 if (a
->u64
[i
] & (1ull << j
)) {
1266 bshift
.VsrD(1) = b
->u64
[i
];
1268 bshift
.VsrD(0) = b
->u64
[i
] >> (64 - j
);
1269 bshift
.VsrD(1) = b
->u64
[i
] << j
;
1271 prod
[i
].VsrD(1) ^= bshift
.VsrD(1);
1272 prod
[i
].VsrD(0) ^= bshift
.VsrD(0);
1277 r
->VsrD(1) = prod
[0].VsrD(1) ^ prod
[1].VsrD(1);
1278 r
->VsrD(0) = prod
[0].VsrD(0) ^ prod
[1].VsrD(0);
1283 #if defined(HOST_WORDS_BIGENDIAN)
1288 void helper_vpkpx(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1292 #if defined(HOST_WORDS_BIGENDIAN)
1293 const ppc_avr_t
*x
[2] = { a
, b
};
1295 const ppc_avr_t
*x
[2] = { b
, a
};
1298 VECTOR_FOR_INORDER_I(i
, u64
) {
1299 VECTOR_FOR_INORDER_I(j
, u32
) {
1300 uint32_t e
= x
[i
]->u32
[j
];
1302 result
.u16
[4 * i
+ j
] = (((e
>> 9) & 0xfc00) |
1303 ((e
>> 6) & 0x3e0) |
1310 #define VPK(suffix, from, to, cvt, dosat) \
1311 void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r, \
1312 ppc_avr_t *a, ppc_avr_t *b) \
1317 ppc_avr_t *a0 = PKBIG ? a : b; \
1318 ppc_avr_t *a1 = PKBIG ? b : a; \
1320 VECTOR_FOR_INORDER_I(i, from) { \
1321 result.to[i] = cvt(a0->from[i], &sat); \
1322 result.to[i + ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);\
1325 if (dosat && sat) { \
1326 set_vscr_sat(env); \
1330 VPK(shss
, s16
, s8
, cvtshsb
, 1)
1331 VPK(shus
, s16
, u8
, cvtshub
, 1)
1332 VPK(swss
, s32
, s16
, cvtswsh
, 1)
1333 VPK(swus
, s32
, u16
, cvtswuh
, 1)
1334 VPK(sdss
, s64
, s32
, cvtsdsw
, 1)
1335 VPK(sdus
, s64
, u32
, cvtsduw
, 1)
1336 VPK(uhus
, u16
, u8
, cvtuhub
, 1)
1337 VPK(uwus
, u32
, u16
, cvtuwuh
, 1)
1338 VPK(udus
, u64
, u32
, cvtuduw
, 1)
1339 VPK(uhum
, u16
, u8
, I
, 0)
1340 VPK(uwum
, u32
, u16
, I
, 0)
1341 VPK(udum
, u64
, u32
, I
, 0)
1346 void helper_vrefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1350 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1351 r
->f32
[i
] = float32_div(float32_one
, b
->f32
[i
], &env
->vec_status
);
1355 #define VRFI(suffix, rounding) \
1356 void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r, \
1360 float_status s = env->vec_status; \
1362 set_float_rounding_mode(rounding, &s); \
1363 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
1364 r->f32[i] = float32_round_to_int (b->f32[i], &s); \
1367 VRFI(n
, float_round_nearest_even
)
1368 VRFI(m
, float_round_down
)
1369 VRFI(p
, float_round_up
)
1370 VRFI(z
, float_round_to_zero
)
1373 void helper_vrsqrtefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1377 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1378 float32 t
= float32_sqrt(b
->f32
[i
], &env
->vec_status
);
1380 r
->f32
[i
] = float32_div(float32_one
, t
, &env
->vec_status
);
1384 #define VRLMI(name, size, element, insert) \
1385 void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1388 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1389 uint##size##_t src1 = a->element[i]; \
1390 uint##size##_t src2 = b->element[i]; \
1391 uint##size##_t src3 = r->element[i]; \
1392 uint##size##_t begin, end, shift, mask, rot_val; \
1394 shift = extract##size(src2, 0, 6); \
1395 end = extract##size(src2, 8, 6); \
1396 begin = extract##size(src2, 16, 6); \
1397 rot_val = rol##size(src1, shift); \
1398 mask = mask_u##size(begin, end); \
1400 r->element[i] = (rot_val & mask) | (src3 & ~mask); \
1402 r->element[i] = (rot_val & mask); \
1407 VRLMI(vrldmi
, 64, u64
, 1);
1408 VRLMI(vrlwmi
, 32, u32
, 1);
1409 VRLMI(vrldnm
, 64, u64
, 0);
1410 VRLMI(vrlwnm
, 32, u32
, 0);
1412 void helper_vsel(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1415 r
->u64
[0] = (a
->u64
[0] & ~c
->u64
[0]) | (b
->u64
[0] & c
->u64
[0]);
1416 r
->u64
[1] = (a
->u64
[1] & ~c
->u64
[1]) | (b
->u64
[1] & c
->u64
[1]);
1419 void helper_vexptefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1423 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1424 r
->f32
[i
] = float32_exp2(b
->f32
[i
], &env
->vec_status
);
1428 void helper_vlogefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1432 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1433 r
->f32
[i
] = float32_log2(b
->f32
[i
], &env
->vec_status
);
1437 #if defined(HOST_WORDS_BIGENDIAN)
1438 #define VEXTU_X_DO(name, size, left) \
1439 target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b) \
1443 index = (a & 0xf) * 8; \
1445 index = ((15 - (a & 0xf) + 1) * 8) - size; \
1447 return int128_getlo(int128_rshift(b->s128, index)) & \
1448 MAKE_64BIT_MASK(0, size); \
1451 #define VEXTU_X_DO(name, size, left) \
1452 target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b) \
1456 index = ((15 - (a & 0xf) + 1) * 8) - size; \
1458 index = (a & 0xf) * 8; \
1460 return int128_getlo(int128_rshift(b->s128, index)) & \
1461 MAKE_64BIT_MASK(0, size); \
1465 VEXTU_X_DO(vextublx
, 8, 1)
1466 VEXTU_X_DO(vextuhlx
, 16, 1)
1467 VEXTU_X_DO(vextuwlx
, 32, 1)
1468 VEXTU_X_DO(vextubrx
, 8, 0)
1469 VEXTU_X_DO(vextuhrx
, 16, 0)
1470 VEXTU_X_DO(vextuwrx
, 32, 0)
1473 void helper_vslv(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1476 unsigned int shift
, bytes
, size
;
1478 size
= ARRAY_SIZE(r
->u8
);
1479 for (i
= 0; i
< size
; i
++) {
1480 shift
= b
->VsrB(i
) & 0x7; /* extract shift value */
1481 bytes
= (a
->VsrB(i
) << 8) + /* extract adjacent bytes */
1482 (((i
+ 1) < size
) ? a
->VsrB(i
+ 1) : 0);
1483 r
->VsrB(i
) = (bytes
<< shift
) >> 8; /* shift and store result */
1487 void helper_vsrv(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1490 unsigned int shift
, bytes
;
1493 * Use reverse order, as destination and source register can be
1494 * same. Its being modified in place saving temporary, reverse
1495 * order will guarantee that computed result is not fed back.
1497 for (i
= ARRAY_SIZE(r
->u8
) - 1; i
>= 0; i
--) {
1498 shift
= b
->VsrB(i
) & 0x7; /* extract shift value */
1499 bytes
= ((i
? a
->VsrB(i
- 1) : 0) << 8) + a
->VsrB(i
);
1500 /* extract adjacent bytes */
1501 r
->VsrB(i
) = (bytes
>> shift
) & 0xFF; /* shift and store result */
1505 void helper_vsldoi(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t shift
)
1507 int sh
= shift
& 0xf;
1511 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1514 result
.VsrB(i
) = b
->VsrB(index
- 0x10);
1516 result
.VsrB(i
) = a
->VsrB(index
);
1522 void helper_vslo(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1524 int sh
= (b
->VsrB(0xf) >> 3) & 0xf;
1526 #if defined(HOST_WORDS_BIGENDIAN)
1527 memmove(&r
->u8
[0], &a
->u8
[sh
], 16 - sh
);
1528 memset(&r
->u8
[16 - sh
], 0, sh
);
1530 memmove(&r
->u8
[sh
], &a
->u8
[0], 16 - sh
);
1531 memset(&r
->u8
[0], 0, sh
);
1535 #if defined(HOST_WORDS_BIGENDIAN)
1536 #define VINSERT(suffix, element) \
1537 void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1539 memmove(&r->u8[index], &b->u8[8 - sizeof(r->element[0])], \
1540 sizeof(r->element[0])); \
1543 #define VINSERT(suffix, element) \
1544 void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1546 uint32_t d = (16 - index) - sizeof(r->element[0]); \
1547 memmove(&r->u8[d], &b->u8[8], sizeof(r->element[0])); \
1555 #if defined(HOST_WORDS_BIGENDIAN)
1556 #define VEXTRACT(suffix, element) \
1557 void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1559 uint32_t es = sizeof(r->element[0]); \
1560 memmove(&r->u8[8 - es], &b->u8[index], es); \
1561 memset(&r->u8[8], 0, 8); \
1562 memset(&r->u8[0], 0, 8 - es); \
1565 #define VEXTRACT(suffix, element) \
1566 void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1568 uint32_t es = sizeof(r->element[0]); \
1569 uint32_t s = (16 - index) - es; \
1570 memmove(&r->u8[8], &b->u8[s], es); \
1571 memset(&r->u8[0], 0, 8); \
1572 memset(&r->u8[8 + es], 0, 8 - es); \
1581 void helper_xxextractuw(CPUPPCState
*env
, ppc_vsr_t
*xt
,
1582 ppc_vsr_t
*xb
, uint32_t index
)
1585 size_t es
= sizeof(uint32_t);
1590 for (i
= 0; i
< es
; i
++, ext_index
++) {
1591 t
.VsrB(8 - es
+ i
) = xb
->VsrB(ext_index
% 16);
1597 void helper_xxinsertw(CPUPPCState
*env
, ppc_vsr_t
*xt
,
1598 ppc_vsr_t
*xb
, uint32_t index
)
1601 size_t es
= sizeof(uint32_t);
1602 int ins_index
, i
= 0;
1605 for (i
= 0; i
< es
&& ins_index
< 16; i
++, ins_index
++) {
1606 t
.VsrB(ins_index
) = xb
->VsrB(8 - es
+ i
);
1612 #define VEXT_SIGNED(name, element, cast) \
1613 void helper_##name(ppc_avr_t *r, ppc_avr_t *b) \
1616 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1617 r->element[i] = (cast)b->element[i]; \
1620 VEXT_SIGNED(vextsb2w
, s32
, int8_t)
1621 VEXT_SIGNED(vextsb2d
, s64
, int8_t)
1622 VEXT_SIGNED(vextsh2w
, s32
, int16_t)
1623 VEXT_SIGNED(vextsh2d
, s64
, int16_t)
1624 VEXT_SIGNED(vextsw2d
, s64
, int32_t)
1627 #define VNEG(name, element) \
1628 void helper_##name(ppc_avr_t *r, ppc_avr_t *b) \
1631 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1632 r->element[i] = -b->element[i]; \
1639 void helper_vsro(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1641 int sh
= (b
->VsrB(0xf) >> 3) & 0xf;
1643 #if defined(HOST_WORDS_BIGENDIAN)
1644 memmove(&r
->u8
[sh
], &a
->u8
[0], 16 - sh
);
1645 memset(&r
->u8
[0], 0, sh
);
1647 memmove(&r
->u8
[0], &a
->u8
[sh
], 16 - sh
);
1648 memset(&r
->u8
[16 - sh
], 0, sh
);
1652 void helper_vsubcuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1656 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
1657 r
->u32
[i
] = a
->u32
[i
] >= b
->u32
[i
];
1661 void helper_vsumsws(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1668 upper
= ARRAY_SIZE(r
->s32
) - 1;
1669 t
= (int64_t)b
->VsrSW(upper
);
1670 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1672 result
.VsrSW(i
) = 0;
1674 result
.VsrSW(upper
) = cvtsdsw(t
, &sat
);
1682 void helper_vsum2sws(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1689 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
1690 int64_t t
= (int64_t)b
->VsrSW(upper
+ i
* 2);
1693 for (j
= 0; j
< ARRAY_SIZE(r
->u64
); j
++) {
1694 t
+= a
->VsrSW(2 * i
+ j
);
1696 result
.VsrSW(upper
+ i
* 2) = cvtsdsw(t
, &sat
);
1705 void helper_vsum4sbs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1710 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1711 int64_t t
= (int64_t)b
->s32
[i
];
1713 for (j
= 0; j
< ARRAY_SIZE(r
->s32
); j
++) {
1714 t
+= a
->s8
[4 * i
+ j
];
1716 r
->s32
[i
] = cvtsdsw(t
, &sat
);
1724 void helper_vsum4shs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1729 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1730 int64_t t
= (int64_t)b
->s32
[i
];
1732 t
+= a
->s16
[2 * i
] + a
->s16
[2 * i
+ 1];
1733 r
->s32
[i
] = cvtsdsw(t
, &sat
);
1741 void helper_vsum4ubs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1746 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
1747 uint64_t t
= (uint64_t)b
->u32
[i
];
1749 for (j
= 0; j
< ARRAY_SIZE(r
->u32
); j
++) {
1750 t
+= a
->u8
[4 * i
+ j
];
1752 r
->u32
[i
] = cvtuduw(t
, &sat
);
1760 #if defined(HOST_WORDS_BIGENDIAN)
1767 #define VUPKPX(suffix, hi) \
1768 void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b) \
1773 for (i = 0; i < ARRAY_SIZE(r->u32); i++) { \
1774 uint16_t e = b->u16[hi ? i : i + 4]; \
1775 uint8_t a = (e >> 15) ? 0xff : 0; \
1776 uint8_t r = (e >> 10) & 0x1f; \
1777 uint8_t g = (e >> 5) & 0x1f; \
1778 uint8_t b = e & 0x1f; \
1780 result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b; \
1788 #define VUPK(suffix, unpacked, packee, hi) \
1789 void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b) \
1795 for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) { \
1796 result.unpacked[i] = b->packee[i]; \
1799 for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
1801 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
1806 VUPK(hsb
, s16
, s8
, UPKHI
)
1807 VUPK(hsh
, s32
, s16
, UPKHI
)
1808 VUPK(hsw
, s64
, s32
, UPKHI
)
1809 VUPK(lsb
, s16
, s8
, UPKLO
)
1810 VUPK(lsh
, s32
, s16
, UPKLO
)
1811 VUPK(lsw
, s64
, s32
, UPKLO
)
1816 #define VGENERIC_DO(name, element) \
1817 void helper_v##name(ppc_avr_t *r, ppc_avr_t *b) \
1821 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1822 r->element[i] = name(b->element[i]); \
1826 #define clzb(v) ((v) ? clz32((uint32_t)(v) << 24) : 8)
1827 #define clzh(v) ((v) ? clz32((uint32_t)(v) << 16) : 16)
1829 VGENERIC_DO(clzb
, u8
)
1830 VGENERIC_DO(clzh
, u16
)
1835 #define ctzb(v) ((v) ? ctz32(v) : 8)
1836 #define ctzh(v) ((v) ? ctz32(v) : 16)
1837 #define ctzw(v) ctz32((v))
1838 #define ctzd(v) ctz64((v))
1840 VGENERIC_DO(ctzb
, u8
)
1841 VGENERIC_DO(ctzh
, u16
)
1842 VGENERIC_DO(ctzw
, u32
)
1843 VGENERIC_DO(ctzd
, u64
)
1850 #define popcntb(v) ctpop8(v)
1851 #define popcnth(v) ctpop16(v)
1852 #define popcntw(v) ctpop32(v)
1853 #define popcntd(v) ctpop64(v)
1855 VGENERIC_DO(popcntb
, u8
)
1856 VGENERIC_DO(popcnth
, u16
)
1857 VGENERIC_DO(popcntw
, u32
)
1858 VGENERIC_DO(popcntd
, u64
)
1867 #if defined(HOST_WORDS_BIGENDIAN)
1868 #define QW_ONE { .u64 = { 0, 1 } }
1870 #define QW_ONE { .u64 = { 1, 0 } }
1873 #ifndef CONFIG_INT128
1875 static inline void avr_qw_not(ppc_avr_t
*t
, ppc_avr_t a
)
1877 t
->u64
[0] = ~a
.u64
[0];
1878 t
->u64
[1] = ~a
.u64
[1];
1881 static int avr_qw_cmpu(ppc_avr_t a
, ppc_avr_t b
)
1883 if (a
.VsrD(0) < b
.VsrD(0)) {
1885 } else if (a
.VsrD(0) > b
.VsrD(0)) {
1887 } else if (a
.VsrD(1) < b
.VsrD(1)) {
1889 } else if (a
.VsrD(1) > b
.VsrD(1)) {
1896 static void avr_qw_add(ppc_avr_t
*t
, ppc_avr_t a
, ppc_avr_t b
)
1898 t
->VsrD(1) = a
.VsrD(1) + b
.VsrD(1);
1899 t
->VsrD(0) = a
.VsrD(0) + b
.VsrD(0) +
1900 (~a
.VsrD(1) < b
.VsrD(1));
1903 static int avr_qw_addc(ppc_avr_t
*t
, ppc_avr_t a
, ppc_avr_t b
)
1906 t
->VsrD(1) = a
.VsrD(1) + b
.VsrD(1);
1907 t
->VsrD(0) = a
.VsrD(0) + b
.VsrD(0) +
1908 (~a
.VsrD(1) < b
.VsrD(1));
1909 avr_qw_not(¬_a
, a
);
1910 return avr_qw_cmpu(not_a
, b
) < 0;
1915 void helper_vadduqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1917 #ifdef CONFIG_INT128
1918 r
->u128
= a
->u128
+ b
->u128
;
1920 avr_qw_add(r
, *a
, *b
);
1924 void helper_vaddeuqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
1926 #ifdef CONFIG_INT128
1927 r
->u128
= a
->u128
+ b
->u128
+ (c
->u128
& 1);
1930 if (c
->VsrD(1) & 1) {
1934 tmp
.VsrD(1) = c
->VsrD(1) & 1;
1935 avr_qw_add(&tmp
, *a
, tmp
);
1936 avr_qw_add(r
, tmp
, *b
);
1938 avr_qw_add(r
, *a
, *b
);
1943 void helper_vaddcuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1945 #ifdef CONFIG_INT128
1946 r
->u128
= (~a
->u128
< b
->u128
);
1950 avr_qw_not(¬_a
, *a
);
1953 r
->VsrD(1) = (avr_qw_cmpu(not_a
, *b
) < 0);
1957 void helper_vaddecuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
1959 #ifdef CONFIG_INT128
1960 int carry_out
= (~a
->u128
< b
->u128
);
1961 if (!carry_out
&& (c
->u128
& 1)) {
1962 carry_out
= ((a
->u128
+ b
->u128
+ 1) == 0) &&
1963 ((a
->u128
!= 0) || (b
->u128
!= 0));
1965 r
->u128
= carry_out
;
1968 int carry_in
= c
->VsrD(1) & 1;
1972 carry_out
= avr_qw_addc(&tmp
, *a
, *b
);
1974 if (!carry_out
&& carry_in
) {
1975 ppc_avr_t one
= QW_ONE
;
1976 carry_out
= avr_qw_addc(&tmp
, tmp
, one
);
1979 r
->VsrD(1) = carry_out
;
1983 void helper_vsubuqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1985 #ifdef CONFIG_INT128
1986 r
->u128
= a
->u128
- b
->u128
;
1989 ppc_avr_t one
= QW_ONE
;
1991 avr_qw_not(&tmp
, *b
);
1992 avr_qw_add(&tmp
, *a
, tmp
);
1993 avr_qw_add(r
, tmp
, one
);
1997 void helper_vsubeuqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
1999 #ifdef CONFIG_INT128
2000 r
->u128
= a
->u128
+ ~b
->u128
+ (c
->u128
& 1);
2004 avr_qw_not(&tmp
, *b
);
2005 avr_qw_add(&sum
, *a
, tmp
);
2008 tmp
.VsrD(1) = c
->VsrD(1) & 1;
2009 avr_qw_add(r
, sum
, tmp
);
2013 void helper_vsubcuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2015 #ifdef CONFIG_INT128
2016 r
->u128
= (~a
->u128
< ~b
->u128
) ||
2017 (a
->u128
+ ~b
->u128
== (__uint128_t
)-1);
2019 int carry
= (avr_qw_cmpu(*a
, *b
) > 0);
2022 avr_qw_not(&tmp
, *b
);
2023 avr_qw_add(&tmp
, *a
, tmp
);
2024 carry
= ((tmp
.VsrSD(0) == -1ull) && (tmp
.VsrSD(1) == -1ull));
2031 void helper_vsubecuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2033 #ifdef CONFIG_INT128
2035 (~a
->u128
< ~b
->u128
) ||
2036 ((c
->u128
& 1) && (a
->u128
+ ~b
->u128
== (__uint128_t
)-1));
2038 int carry_in
= c
->VsrD(1) & 1;
2039 int carry_out
= (avr_qw_cmpu(*a
, *b
) > 0);
2040 if (!carry_out
&& carry_in
) {
2042 avr_qw_not(&tmp
, *b
);
2043 avr_qw_add(&tmp
, *a
, tmp
);
2044 carry_out
= ((tmp
.VsrD(0) == -1ull) && (tmp
.VsrD(1) == -1ull));
2048 r
->VsrD(1) = carry_out
;
2052 #define BCD_PLUS_PREF_1 0xC
2053 #define BCD_PLUS_PREF_2 0xF
2054 #define BCD_PLUS_ALT_1 0xA
2055 #define BCD_NEG_PREF 0xD
2056 #define BCD_NEG_ALT 0xB
2057 #define BCD_PLUS_ALT_2 0xE
2058 #define NATIONAL_PLUS 0x2B
2059 #define NATIONAL_NEG 0x2D
2061 #define BCD_DIG_BYTE(n) (15 - ((n) / 2))
2063 static int bcd_get_sgn(ppc_avr_t
*bcd
)
2065 switch (bcd
->VsrB(BCD_DIG_BYTE(0)) & 0xF) {
2066 case BCD_PLUS_PREF_1
:
2067 case BCD_PLUS_PREF_2
:
2068 case BCD_PLUS_ALT_1
:
2069 case BCD_PLUS_ALT_2
:
2087 static int bcd_preferred_sgn(int sgn
, int ps
)
2090 return (ps
== 0) ? BCD_PLUS_PREF_1
: BCD_PLUS_PREF_2
;
2092 return BCD_NEG_PREF
;
2096 static uint8_t bcd_get_digit(ppc_avr_t
*bcd
, int n
, int *invalid
)
2100 result
= bcd
->VsrB(BCD_DIG_BYTE(n
)) >> 4;
2102 result
= bcd
->VsrB(BCD_DIG_BYTE(n
)) & 0xF;
2105 if (unlikely(result
> 9)) {
2111 static void bcd_put_digit(ppc_avr_t
*bcd
, uint8_t digit
, int n
)
2114 bcd
->VsrB(BCD_DIG_BYTE(n
)) &= 0x0F;
2115 bcd
->VsrB(BCD_DIG_BYTE(n
)) |= (digit
<< 4);
2117 bcd
->VsrB(BCD_DIG_BYTE(n
)) &= 0xF0;
2118 bcd
->VsrB(BCD_DIG_BYTE(n
)) |= digit
;
2122 static bool bcd_is_valid(ppc_avr_t
*bcd
)
2127 if (bcd_get_sgn(bcd
) == 0) {
2131 for (i
= 1; i
< 32; i
++) {
2132 bcd_get_digit(bcd
, i
, &invalid
);
2133 if (unlikely(invalid
)) {
2140 static int bcd_cmp_zero(ppc_avr_t
*bcd
)
2142 if (bcd
->VsrD(0) == 0 && (bcd
->VsrD(1) >> 4) == 0) {
2145 return (bcd_get_sgn(bcd
) == 1) ? CRF_GT
: CRF_LT
;
2149 static uint16_t get_national_digit(ppc_avr_t
*reg
, int n
)
2151 return reg
->VsrH(7 - n
);
2154 static void set_national_digit(ppc_avr_t
*reg
, uint8_t val
, int n
)
2156 reg
->VsrH(7 - n
) = val
;
2159 static int bcd_cmp_mag(ppc_avr_t
*a
, ppc_avr_t
*b
)
2163 for (i
= 31; i
> 0; i
--) {
2164 uint8_t dig_a
= bcd_get_digit(a
, i
, &invalid
);
2165 uint8_t dig_b
= bcd_get_digit(b
, i
, &invalid
);
2166 if (unlikely(invalid
)) {
2167 return 0; /* doesn't matter */
2168 } else if (dig_a
> dig_b
) {
2170 } else if (dig_a
< dig_b
) {
2178 static void bcd_add_mag(ppc_avr_t
*t
, ppc_avr_t
*a
, ppc_avr_t
*b
, int *invalid
,
2183 for (i
= 1; i
<= 31; i
++) {
2184 uint8_t digit
= bcd_get_digit(a
, i
, invalid
) +
2185 bcd_get_digit(b
, i
, invalid
) + carry
;
2193 bcd_put_digit(t
, digit
, i
);
2199 static void bcd_sub_mag(ppc_avr_t
*t
, ppc_avr_t
*a
, ppc_avr_t
*b
, int *invalid
,
2205 for (i
= 1; i
<= 31; i
++) {
2206 uint8_t digit
= bcd_get_digit(a
, i
, invalid
) -
2207 bcd_get_digit(b
, i
, invalid
) + carry
;
2215 bcd_put_digit(t
, digit
, i
);
2221 uint32_t helper_bcdadd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2224 int sgna
= bcd_get_sgn(a
);
2225 int sgnb
= bcd_get_sgn(b
);
2226 int invalid
= (sgna
== 0) || (sgnb
== 0);
2229 ppc_avr_t result
= { .u64
= { 0, 0 } };
2233 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna
, ps
);
2234 bcd_add_mag(&result
, a
, b
, &invalid
, &overflow
);
2235 cr
= bcd_cmp_zero(&result
);
2237 int magnitude
= bcd_cmp_mag(a
, b
);
2238 if (magnitude
> 0) {
2239 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna
, ps
);
2240 bcd_sub_mag(&result
, a
, b
, &invalid
, &overflow
);
2241 cr
= (sgna
> 0) ? CRF_GT
: CRF_LT
;
2242 } else if (magnitude
< 0) {
2243 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgnb
, ps
);
2244 bcd_sub_mag(&result
, b
, a
, &invalid
, &overflow
);
2245 cr
= (sgnb
> 0) ? CRF_GT
: CRF_LT
;
2247 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(0, ps
);
2253 if (unlikely(invalid
)) {
2254 result
.VsrD(0) = result
.VsrD(1) = -1;
2256 } else if (overflow
) {
2265 uint32_t helper_bcdsub(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2267 ppc_avr_t bcopy
= *b
;
2268 int sgnb
= bcd_get_sgn(b
);
2270 bcd_put_digit(&bcopy
, BCD_PLUS_PREF_1
, 0);
2271 } else if (sgnb
> 0) {
2272 bcd_put_digit(&bcopy
, BCD_NEG_PREF
, 0);
2274 /* else invalid ... defer to bcdadd code for proper handling */
2276 return helper_bcdadd(r
, a
, &bcopy
, ps
);
2279 uint32_t helper_bcdcfn(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2283 uint16_t national
= 0;
2284 uint16_t sgnb
= get_national_digit(b
, 0);
2285 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2286 int invalid
= (sgnb
!= NATIONAL_PLUS
&& sgnb
!= NATIONAL_NEG
);
2288 for (i
= 1; i
< 8; i
++) {
2289 national
= get_national_digit(b
, i
);
2290 if (unlikely(national
< 0x30 || national
> 0x39)) {
2295 bcd_put_digit(&ret
, national
& 0xf, i
);
2298 if (sgnb
== NATIONAL_PLUS
) {
2299 bcd_put_digit(&ret
, (ps
== 0) ? BCD_PLUS_PREF_1
: BCD_PLUS_PREF_2
, 0);
2301 bcd_put_digit(&ret
, BCD_NEG_PREF
, 0);
2304 cr
= bcd_cmp_zero(&ret
);
2306 if (unlikely(invalid
)) {
2315 uint32_t helper_bcdctn(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2319 int sgnb
= bcd_get_sgn(b
);
2320 int invalid
= (sgnb
== 0);
2321 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2323 int ox_flag
= (b
->VsrD(0) != 0) || ((b
->VsrD(1) >> 32) != 0);
2325 for (i
= 1; i
< 8; i
++) {
2326 set_national_digit(&ret
, 0x30 + bcd_get_digit(b
, i
, &invalid
), i
);
2328 if (unlikely(invalid
)) {
2332 set_national_digit(&ret
, (sgnb
== -1) ? NATIONAL_NEG
: NATIONAL_PLUS
, 0);
2334 cr
= bcd_cmp_zero(b
);
2340 if (unlikely(invalid
)) {
2349 uint32_t helper_bcdcfz(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2355 int zone_lead
= ps
? 0xF : 0x3;
2357 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2358 int sgnb
= b
->VsrB(BCD_DIG_BYTE(0)) >> 4;
2360 if (unlikely((sgnb
< 0xA) && ps
)) {
2364 for (i
= 0; i
< 16; i
++) {
2365 zone_digit
= i
? b
->VsrB(BCD_DIG_BYTE(i
* 2)) >> 4 : zone_lead
;
2366 digit
= b
->VsrB(BCD_DIG_BYTE(i
* 2)) & 0xF;
2367 if (unlikely(zone_digit
!= zone_lead
|| digit
> 0x9)) {
2372 bcd_put_digit(&ret
, digit
, i
+ 1);
2375 if ((ps
&& (sgnb
== 0xB || sgnb
== 0xD)) ||
2376 (!ps
&& (sgnb
& 0x4))) {
2377 bcd_put_digit(&ret
, BCD_NEG_PREF
, 0);
2379 bcd_put_digit(&ret
, BCD_PLUS_PREF_1
, 0);
2382 cr
= bcd_cmp_zero(&ret
);
2384 if (unlikely(invalid
)) {
2393 uint32_t helper_bcdctz(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2398 int sgnb
= bcd_get_sgn(b
);
2399 int zone_lead
= (ps
) ? 0xF0 : 0x30;
2400 int invalid
= (sgnb
== 0);
2401 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2403 int ox_flag
= ((b
->VsrD(0) >> 4) != 0);
2405 for (i
= 0; i
< 16; i
++) {
2406 digit
= bcd_get_digit(b
, i
+ 1, &invalid
);
2408 if (unlikely(invalid
)) {
2412 ret
.VsrB(BCD_DIG_BYTE(i
* 2)) = zone_lead
+ digit
;
2416 bcd_put_digit(&ret
, (sgnb
== 1) ? 0xC : 0xD, 1);
2418 bcd_put_digit(&ret
, (sgnb
== 1) ? 0x3 : 0x7, 1);
2421 cr
= bcd_cmp_zero(b
);
2427 if (unlikely(invalid
)) {
2436 uint32_t helper_bcdcfsq(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2442 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2444 if (b
->VsrSD(0) < 0) {
2445 lo_value
= -b
->VsrSD(1);
2446 hi_value
= ~b
->VsrD(0) + !lo_value
;
2447 bcd_put_digit(&ret
, 0xD, 0);
2449 lo_value
= b
->VsrD(1);
2450 hi_value
= b
->VsrD(0);
2451 bcd_put_digit(&ret
, bcd_preferred_sgn(0, ps
), 0);
2454 if (divu128(&lo_value
, &hi_value
, 1000000000000000ULL) ||
2455 lo_value
> 9999999999999999ULL) {
2459 for (i
= 1; i
< 16; hi_value
/= 10, i
++) {
2460 bcd_put_digit(&ret
, hi_value
% 10, i
);
2463 for (; i
< 32; lo_value
/= 10, i
++) {
2464 bcd_put_digit(&ret
, lo_value
% 10, i
);
2467 cr
|= bcd_cmp_zero(&ret
);
2474 uint32_t helper_bcdctsq(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2481 uint64_t hi_value
= 0;
2482 int sgnb
= bcd_get_sgn(b
);
2483 int invalid
= (sgnb
== 0);
2485 lo_value
= bcd_get_digit(b
, 31, &invalid
);
2486 for (i
= 30; i
> 0; i
--) {
2487 mulu64(&lo_value
, &carry
, lo_value
, 10ULL);
2488 mulu64(&hi_value
, &unused
, hi_value
, 10ULL);
2489 lo_value
+= bcd_get_digit(b
, i
, &invalid
);
2492 if (unlikely(invalid
)) {
2498 r
->VsrSD(1) = -lo_value
;
2499 r
->VsrSD(0) = ~hi_value
+ !r
->VsrSD(1);
2501 r
->VsrSD(1) = lo_value
;
2502 r
->VsrSD(0) = hi_value
;
2505 cr
= bcd_cmp_zero(b
);
2507 if (unlikely(invalid
)) {
2514 uint32_t helper_bcdcpsgn(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2519 if (bcd_get_sgn(a
) == 0 || bcd_get_sgn(b
) == 0) {
2524 bcd_put_digit(r
, b
->VsrB(BCD_DIG_BYTE(0)) & 0xF, 0);
2526 for (i
= 1; i
< 32; i
++) {
2527 bcd_get_digit(a
, i
, &invalid
);
2528 bcd_get_digit(b
, i
, &invalid
);
2529 if (unlikely(invalid
)) {
2534 return bcd_cmp_zero(r
);
2537 uint32_t helper_bcdsetsgn(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2539 int sgnb
= bcd_get_sgn(b
);
2542 bcd_put_digit(r
, bcd_preferred_sgn(sgnb
, ps
), 0);
2544 if (bcd_is_valid(b
) == false) {
2548 return bcd_cmp_zero(r
);
2551 uint32_t helper_bcds(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2554 int i
= a
->VsrSB(7);
2555 bool ox_flag
= false;
2556 int sgnb
= bcd_get_sgn(b
);
2558 ret
.VsrD(1) &= ~0xf;
2560 if (bcd_is_valid(b
) == false) {
2564 if (unlikely(i
> 31)) {
2566 } else if (unlikely(i
< -31)) {
2571 ulshift(&ret
.VsrD(1), &ret
.VsrD(0), i
* 4, &ox_flag
);
2573 urshift(&ret
.VsrD(1), &ret
.VsrD(0), -i
* 4);
2575 bcd_put_digit(&ret
, bcd_preferred_sgn(sgnb
, ps
), 0);
2579 cr
= bcd_cmp_zero(r
);
2587 uint32_t helper_bcdus(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2592 bool ox_flag
= false;
2595 for (i
= 0; i
< 32; i
++) {
2596 bcd_get_digit(b
, i
, &invalid
);
2598 if (unlikely(invalid
)) {
2606 ret
.VsrD(1) = ret
.VsrD(0) = 0;
2607 } else if (i
<= -32) {
2608 ret
.VsrD(1) = ret
.VsrD(0) = 0;
2610 ulshift(&ret
.VsrD(1), &ret
.VsrD(0), i
* 4, &ox_flag
);
2612 urshift(&ret
.VsrD(1), &ret
.VsrD(0), -i
* 4);
2616 cr
= bcd_cmp_zero(r
);
2624 uint32_t helper_bcdsr(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2629 bool ox_flag
= false;
2630 int sgnb
= bcd_get_sgn(b
);
2632 ret
.VsrD(1) &= ~0xf;
2634 int i
= a
->VsrSB(7);
2637 bcd_one
.VsrD(0) = 0;
2638 bcd_one
.VsrD(1) = 0x10;
2640 if (bcd_is_valid(b
) == false) {
2644 if (unlikely(i
> 31)) {
2646 } else if (unlikely(i
< -31)) {
2651 ulshift(&ret
.VsrD(1), &ret
.VsrD(0), i
* 4, &ox_flag
);
2653 urshift(&ret
.VsrD(1), &ret
.VsrD(0), -i
* 4);
2655 if (bcd_get_digit(&ret
, 0, &invalid
) >= 5) {
2656 bcd_add_mag(&ret
, &ret
, &bcd_one
, &invalid
, &unused
);
2659 bcd_put_digit(&ret
, bcd_preferred_sgn(sgnb
, ps
), 0);
2661 cr
= bcd_cmp_zero(&ret
);
2670 uint32_t helper_bcdtrunc(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2673 uint32_t ox_flag
= 0;
2674 int i
= a
->VsrSH(3) + 1;
2677 if (bcd_is_valid(b
) == false) {
2681 if (i
> 16 && i
< 32) {
2682 mask
= (uint64_t)-1 >> (128 - i
* 4);
2683 if (ret
.VsrD(0) & ~mask
) {
2687 ret
.VsrD(0) &= mask
;
2688 } else if (i
>= 0 && i
<= 16) {
2689 mask
= (uint64_t)-1 >> (64 - i
* 4);
2690 if (ret
.VsrD(0) || (ret
.VsrD(1) & ~mask
)) {
2694 ret
.VsrD(1) &= mask
;
2697 bcd_put_digit(&ret
, bcd_preferred_sgn(bcd_get_sgn(b
), ps
), 0);
2700 return bcd_cmp_zero(&ret
) | ox_flag
;
2703 uint32_t helper_bcdutrunc(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2707 uint32_t ox_flag
= 0;
2711 for (i
= 0; i
< 32; i
++) {
2712 bcd_get_digit(b
, i
, &invalid
);
2714 if (unlikely(invalid
)) {
2720 if (i
> 16 && i
< 33) {
2721 mask
= (uint64_t)-1 >> (128 - i
* 4);
2722 if (ret
.VsrD(0) & ~mask
) {
2726 ret
.VsrD(0) &= mask
;
2727 } else if (i
> 0 && i
<= 16) {
2728 mask
= (uint64_t)-1 >> (64 - i
* 4);
2729 if (ret
.VsrD(0) || (ret
.VsrD(1) & ~mask
)) {
2733 ret
.VsrD(1) &= mask
;
2735 } else if (i
== 0) {
2736 if (ret
.VsrD(0) || ret
.VsrD(1)) {
2739 ret
.VsrD(0) = ret
.VsrD(1) = 0;
2743 if (r
->VsrD(0) == 0 && r
->VsrD(1) == 0) {
2744 return ox_flag
| CRF_EQ
;
2747 return ox_flag
| CRF_GT
;
2750 void helper_vsbox(ppc_avr_t
*r
, ppc_avr_t
*a
)
2753 VECTOR_FOR_INORDER_I(i
, u8
) {
2754 r
->u8
[i
] = AES_sbox
[a
->u8
[i
]];
2758 void helper_vcipher(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2763 VECTOR_FOR_INORDER_I(i
, u32
) {
2764 result
.VsrW(i
) = b
->VsrW(i
) ^
2765 (AES_Te0
[a
->VsrB(AES_shifts
[4 * i
+ 0])] ^
2766 AES_Te1
[a
->VsrB(AES_shifts
[4 * i
+ 1])] ^
2767 AES_Te2
[a
->VsrB(AES_shifts
[4 * i
+ 2])] ^
2768 AES_Te3
[a
->VsrB(AES_shifts
[4 * i
+ 3])]);
2773 void helper_vcipherlast(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2778 VECTOR_FOR_INORDER_I(i
, u8
) {
2779 result
.VsrB(i
) = b
->VsrB(i
) ^ (AES_sbox
[a
->VsrB(AES_shifts
[i
])]);
2784 void helper_vncipher(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2786 /* This differs from what is written in ISA V2.07. The RTL is */
2787 /* incorrect and will be fixed in V2.07B. */
2791 VECTOR_FOR_INORDER_I(i
, u8
) {
2792 tmp
.VsrB(i
) = b
->VsrB(i
) ^ AES_isbox
[a
->VsrB(AES_ishifts
[i
])];
2795 VECTOR_FOR_INORDER_I(i
, u32
) {
2797 AES_imc
[tmp
.VsrB(4 * i
+ 0)][0] ^
2798 AES_imc
[tmp
.VsrB(4 * i
+ 1)][1] ^
2799 AES_imc
[tmp
.VsrB(4 * i
+ 2)][2] ^
2800 AES_imc
[tmp
.VsrB(4 * i
+ 3)][3];
2804 void helper_vncipherlast(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2809 VECTOR_FOR_INORDER_I(i
, u8
) {
2810 result
.VsrB(i
) = b
->VsrB(i
) ^ (AES_isbox
[a
->VsrB(AES_ishifts
[i
])]);
2815 void helper_vshasigmaw(ppc_avr_t
*r
, ppc_avr_t
*a
, uint32_t st_six
)
2817 int st
= (st_six
& 0x10) != 0;
2818 int six
= st_six
& 0xF;
2821 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2823 if ((six
& (0x8 >> i
)) == 0) {
2824 r
->VsrW(i
) = ror32(a
->VsrW(i
), 7) ^
2825 ror32(a
->VsrW(i
), 18) ^
2827 } else { /* six.bit[i] == 1 */
2828 r
->VsrW(i
) = ror32(a
->VsrW(i
), 17) ^
2829 ror32(a
->VsrW(i
), 19) ^
2832 } else { /* st == 1 */
2833 if ((six
& (0x8 >> i
)) == 0) {
2834 r
->VsrW(i
) = ror32(a
->VsrW(i
), 2) ^
2835 ror32(a
->VsrW(i
), 13) ^
2836 ror32(a
->VsrW(i
), 22);
2837 } else { /* six.bit[i] == 1 */
2838 r
->VsrW(i
) = ror32(a
->VsrW(i
), 6) ^
2839 ror32(a
->VsrW(i
), 11) ^
2840 ror32(a
->VsrW(i
), 25);
2846 void helper_vshasigmad(ppc_avr_t
*r
, ppc_avr_t
*a
, uint32_t st_six
)
2848 int st
= (st_six
& 0x10) != 0;
2849 int six
= st_six
& 0xF;
2852 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
2854 if ((six
& (0x8 >> (2 * i
))) == 0) {
2855 r
->VsrD(i
) = ror64(a
->VsrD(i
), 1) ^
2856 ror64(a
->VsrD(i
), 8) ^
2858 } else { /* six.bit[2*i] == 1 */
2859 r
->VsrD(i
) = ror64(a
->VsrD(i
), 19) ^
2860 ror64(a
->VsrD(i
), 61) ^
2863 } else { /* st == 1 */
2864 if ((six
& (0x8 >> (2 * i
))) == 0) {
2865 r
->VsrD(i
) = ror64(a
->VsrD(i
), 28) ^
2866 ror64(a
->VsrD(i
), 34) ^
2867 ror64(a
->VsrD(i
), 39);
2868 } else { /* six.bit[2*i] == 1 */
2869 r
->VsrD(i
) = ror64(a
->VsrD(i
), 14) ^
2870 ror64(a
->VsrD(i
), 18) ^
2871 ror64(a
->VsrD(i
), 41);
2877 void helper_vpermxor(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2882 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2883 int indexA
= c
->VsrB(i
) >> 4;
2884 int indexB
= c
->VsrB(i
) & 0xF;
2886 result
.VsrB(i
) = a
->VsrB(indexA
) ^ b
->VsrB(indexB
);
2891 #undef VECTOR_FOR_INORDER_I
2893 /*****************************************************************************/
2894 /* SPE extension helpers */
2895 /* Use a table to make this quicker */
2896 static const uint8_t hbrev
[16] = {
2897 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
2898 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
2901 static inline uint8_t byte_reverse(uint8_t val
)
2903 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
2906 static inline uint32_t word_reverse(uint32_t val
)
2908 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
2909 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
2912 #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
2913 target_ulong
helper_brinc(target_ulong arg1
, target_ulong arg2
)
2915 uint32_t a
, b
, d
, mask
;
2917 mask
= UINT32_MAX
>> (32 - MASKBITS
);
2920 d
= word_reverse(1 + word_reverse(a
| ~b
));
2921 return (arg1
& ~mask
) | (d
& b
);
2924 uint32_t helper_cntlsw32(uint32_t val
)
2926 if (val
& 0x80000000) {
2933 uint32_t helper_cntlzw32(uint32_t val
)
2939 target_ulong
helper_dlmzb(CPUPPCState
*env
, target_ulong high
,
2940 target_ulong low
, uint32_t update_Rc
)
2946 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
2947 if ((high
& mask
) == 0) {
2955 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
2956 if ((low
& mask
) == 0) {
2969 env
->xer
= (env
->xer
& ~0x7F) | i
;
2971 env
->crf
[0] |= xer_so
;