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"
26 #include "exec/helper-proto.h"
27 #include "crypto/aes.h"
28 #include "fpu/softfloat.h"
29 #include "qapi/error.h"
30 #include "qemu/guest-random.h"
32 #include "helper_regs.h"
33 /*****************************************************************************/
34 /* Fixed point operations helpers */
36 static inline void helper_update_ov_legacy(CPUPPCState
*env
, int ov
)
39 env
->so
= env
->ov
= 1;
45 target_ulong
helper_divweu(CPUPPCState
*env
, target_ulong ra
, target_ulong rb
,
51 uint64_t dividend
= (uint64_t)ra
<< 32;
52 uint64_t divisor
= (uint32_t)rb
;
54 if (unlikely(divisor
== 0)) {
57 rt
= dividend
/ divisor
;
58 overflow
= rt
> UINT32_MAX
;
61 if (unlikely(overflow
)) {
62 rt
= 0; /* Undefined */
66 helper_update_ov_legacy(env
, overflow
);
69 return (target_ulong
)rt
;
72 target_ulong
helper_divwe(CPUPPCState
*env
, target_ulong ra
, target_ulong rb
,
78 int64_t dividend
= (int64_t)ra
<< 32;
79 int64_t divisor
= (int64_t)((int32_t)rb
);
81 if (unlikely((divisor
== 0) ||
82 ((divisor
== -1ull) && (dividend
== INT64_MIN
)))) {
85 rt
= dividend
/ divisor
;
86 overflow
= rt
!= (int32_t)rt
;
89 if (unlikely(overflow
)) {
90 rt
= 0; /* Undefined */
94 helper_update_ov_legacy(env
, overflow
);
97 return (target_ulong
)rt
;
100 #if defined(TARGET_PPC64)
102 uint64_t helper_divdeu(CPUPPCState
*env
, uint64_t ra
, uint64_t rb
, uint32_t oe
)
107 overflow
= divu128(&rt
, &ra
, rb
);
109 if (unlikely(overflow
)) {
110 rt
= 0; /* Undefined */
114 helper_update_ov_legacy(env
, overflow
);
120 uint64_t helper_divde(CPUPPCState
*env
, uint64_t rau
, uint64_t rbu
, uint32_t oe
)
123 int64_t ra
= (int64_t)rau
;
124 int64_t rb
= (int64_t)rbu
;
125 int overflow
= divs128(&rt
, &ra
, rb
);
127 if (unlikely(overflow
)) {
128 rt
= 0; /* Undefined */
132 helper_update_ov_legacy(env
, overflow
);
141 #if defined(TARGET_PPC64)
142 /* if x = 0xab, returns 0xababababababababa */
143 #define pattern(x) (((x) & 0xff) * (~(target_ulong)0 / 0xff))
146 * subtract 1 from each byte, and with inverse, check if MSB is set at each
148 * i.e. ((0x00 - 0x01) & ~(0x00)) & 0x80
149 * (0xFF & 0xFF) & 0x80 = 0x80 (zero found)
151 #define haszero(v) (((v) - pattern(0x01)) & ~(v) & pattern(0x80))
153 /* When you XOR the pattern and there is a match, that byte will be zero */
154 #define hasvalue(x, n) (haszero((x) ^ pattern(n)))
156 uint32_t helper_cmpeqb(target_ulong ra
, target_ulong rb
)
158 return hasvalue(rb
, ra
) ? CRF_GT
: 0;
166 * Return a random number.
168 uint64_t helper_darn32(void)
173 if (qemu_guest_getrandom(&ret
, sizeof(ret
), &err
) < 0) {
174 qemu_log_mask(LOG_UNIMP
, "darn: Crypto failure: %s",
175 error_get_pretty(err
));
183 uint64_t helper_darn64(void)
188 if (qemu_guest_getrandom(&ret
, sizeof(ret
), &err
) < 0) {
189 qemu_log_mask(LOG_UNIMP
, "darn: Crypto failure: %s",
190 error_get_pretty(err
));
198 uint64_t helper_bpermd(uint64_t rs
, uint64_t rb
)
203 for (i
= 0; i
< 8; i
++) {
204 int index
= (rs
>> (i
* 8)) & 0xFF;
206 if (rb
& PPC_BIT(index
)) {
216 target_ulong
helper_cmpb(target_ulong rs
, target_ulong rb
)
218 target_ulong mask
= 0xff;
222 for (i
= 0; i
< sizeof(target_ulong
); i
++) {
223 if ((rs
& mask
) == (rb
& mask
)) {
231 /* shift right arithmetic helper */
232 target_ulong
helper_sraw(CPUPPCState
*env
, target_ulong value
,
237 if (likely(!(shift
& 0x20))) {
238 if (likely((uint32_t)shift
!= 0)) {
240 ret
= (int32_t)value
>> shift
;
241 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
242 env
->ca32
= env
->ca
= 0;
244 env
->ca32
= env
->ca
= 1;
247 ret
= (int32_t)value
;
248 env
->ca32
= env
->ca
= 0;
251 ret
= (int32_t)value
>> 31;
252 env
->ca32
= env
->ca
= (ret
!= 0);
254 return (target_long
)ret
;
257 #if defined(TARGET_PPC64)
258 target_ulong
helper_srad(CPUPPCState
*env
, target_ulong value
,
263 if (likely(!(shift
& 0x40))) {
264 if (likely((uint64_t)shift
!= 0)) {
266 ret
= (int64_t)value
>> shift
;
267 if (likely(ret
>= 0 || (value
& ((1ULL << shift
) - 1)) == 0)) {
268 env
->ca32
= env
->ca
= 0;
270 env
->ca32
= env
->ca
= 1;
273 ret
= (int64_t)value
;
274 env
->ca32
= env
->ca
= 0;
277 ret
= (int64_t)value
>> 63;
278 env
->ca32
= env
->ca
= (ret
!= 0);
284 #if defined(TARGET_PPC64)
285 target_ulong
helper_popcntb(target_ulong val
)
287 /* Note that we don't fold past bytes */
288 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
289 0x5555555555555555ULL
);
290 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
291 0x3333333333333333ULL
);
292 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
293 0x0f0f0f0f0f0f0f0fULL
);
297 target_ulong
helper_popcntw(target_ulong val
)
299 /* Note that we don't fold past words. */
300 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
301 0x5555555555555555ULL
);
302 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
303 0x3333333333333333ULL
);
304 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
305 0x0f0f0f0f0f0f0f0fULL
);
306 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) &
307 0x00ff00ff00ff00ffULL
);
308 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) &
309 0x0000ffff0000ffffULL
);
313 target_ulong
helper_popcntb(target_ulong val
)
315 /* Note that we don't fold past bytes */
316 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
317 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
318 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
323 uint64_t helper_cfuged(uint64_t src
, uint64_t mask
)
326 * Instead of processing the mask bit-by-bit from the most significant to
327 * the least significant bit, as described in PowerISA, we'll handle it in
328 * blocks of 'n' zeros/ones from LSB to MSB. To avoid the decision to use
329 * ctz or cto, we negate the mask at the end of the loop.
331 target_ulong m
, left
= 0, right
= 0;
332 unsigned int n
, i
= 64;
333 bool bit
= false; /* tracks if we are processing zeros or ones */
335 if (mask
== 0 || mask
== -1) {
339 /* Processes the mask in blocks, from LSB to MSB */
341 /* Find how many bits we should take */
348 * Extracts 'n' trailing bits of src and put them on the leading 'n'
349 * bits of 'right' or 'left', pushing down the previously extracted
354 right
= ror64(right
| (src
& m
), n
);
356 left
= ror64(left
| (src
& m
), n
);
360 * Discards the processed bits from 'src' and 'mask'. Note that we are
361 * removing 'n' trailing zeros from 'mask', but the logical shift will
362 * add 'n' leading zeros back, so the population count of 'mask' is kept
373 * At the end, right was ror'ed ctpop(mask) times. To put it back in place,
374 * we'll shift it more 64-ctpop(mask) times.
379 n
= 64 - ctpop64(mask
);
382 return left
| (right
>> n
);
385 /*****************************************************************************/
386 /* PowerPC 601 specific instructions (POWER bridge) */
387 target_ulong
helper_div(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
)
389 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
391 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
392 (int32_t)arg2
== 0) {
393 env
->spr
[SPR_MQ
] = 0;
396 env
->spr
[SPR_MQ
] = tmp
% arg2
;
397 return tmp
/ (int32_t)arg2
;
401 target_ulong
helper_divo(CPUPPCState
*env
, target_ulong arg1
,
404 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
406 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
407 (int32_t)arg2
== 0) {
408 env
->so
= env
->ov
= 1;
409 env
->spr
[SPR_MQ
] = 0;
412 env
->spr
[SPR_MQ
] = tmp
% arg2
;
413 tmp
/= (int32_t)arg2
;
414 if ((int32_t)tmp
!= tmp
) {
415 env
->so
= env
->ov
= 1;
423 target_ulong
helper_divs(CPUPPCState
*env
, target_ulong arg1
,
426 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
427 (int32_t)arg2
== 0) {
428 env
->spr
[SPR_MQ
] = 0;
431 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
432 return (int32_t)arg1
/ (int32_t)arg2
;
436 target_ulong
helper_divso(CPUPPCState
*env
, target_ulong arg1
,
439 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
440 (int32_t)arg2
== 0) {
441 env
->so
= env
->ov
= 1;
442 env
->spr
[SPR_MQ
] = 0;
446 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
447 return (int32_t)arg1
/ (int32_t)arg2
;
451 /*****************************************************************************/
452 /* 602 specific instructions */
453 /* mfrom is the most crazy instruction ever seen, imho ! */
454 /* Real implementation uses a ROM table. Do the same */
456 * Extremely decomposed:
458 * return 256 * log10(10 + 1.0) + 0.5
460 #if !defined(CONFIG_USER_ONLY)
461 target_ulong
helper_602_mfrom(target_ulong arg
)
463 if (likely(arg
< 602)) {
464 #include "mfrom_table.c.inc"
465 return mfrom_ROM_table
[arg
];
472 /*****************************************************************************/
473 /* Altivec extension helpers */
474 #if defined(HOST_WORDS_BIGENDIAN)
475 #define VECTOR_FOR_INORDER_I(index, element) \
476 for (index = 0; index < ARRAY_SIZE(r->element); index++)
478 #define VECTOR_FOR_INORDER_I(index, element) \
479 for (index = ARRAY_SIZE(r->element) - 1; index >= 0; index--)
482 /* Saturating arithmetic helpers. */
483 #define SATCVT(from, to, from_type, to_type, min, max) \
484 static inline to_type cvt##from##to(from_type x, int *sat) \
488 if (x < (from_type)min) { \
491 } else if (x > (from_type)max) { \
499 #define SATCVTU(from, to, from_type, to_type, min, max) \
500 static inline to_type cvt##from##to(from_type x, int *sat) \
504 if (x > (from_type)max) { \
512 SATCVT(sh
, sb
, int16_t, int8_t, INT8_MIN
, INT8_MAX
)
513 SATCVT(sw
, sh
, int32_t, int16_t, INT16_MIN
, INT16_MAX
)
514 SATCVT(sd
, sw
, int64_t, int32_t, INT32_MIN
, INT32_MAX
)
516 SATCVTU(uh
, ub
, uint16_t, uint8_t, 0, UINT8_MAX
)
517 SATCVTU(uw
, uh
, uint32_t, uint16_t, 0, UINT16_MAX
)
518 SATCVTU(ud
, uw
, uint64_t, uint32_t, 0, UINT32_MAX
)
519 SATCVT(sh
, ub
, int16_t, uint8_t, 0, UINT8_MAX
)
520 SATCVT(sw
, uh
, int32_t, uint16_t, 0, UINT16_MAX
)
521 SATCVT(sd
, uw
, int64_t, uint32_t, 0, UINT32_MAX
)
525 void helper_mtvscr(CPUPPCState
*env
, uint32_t vscr
)
527 ppc_store_vscr(env
, vscr
);
530 uint32_t helper_mfvscr(CPUPPCState
*env
)
532 return ppc_get_vscr(env
);
535 static inline void set_vscr_sat(CPUPPCState
*env
)
537 /* The choice of non-zero value is arbitrary. */
538 env
->vscr_sat
.u32
[0] = 1;
541 void helper_vaddcuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
545 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
546 r
->u32
[i
] = ~a
->u32
[i
] < b
->u32
[i
];
551 void helper_vprtybw(ppc_avr_t
*r
, ppc_avr_t
*b
)
554 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
555 uint64_t res
= b
->u32
[i
] ^ (b
->u32
[i
] >> 16);
562 void helper_vprtybd(ppc_avr_t
*r
, ppc_avr_t
*b
)
565 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
566 uint64_t res
= b
->u64
[i
] ^ (b
->u64
[i
] >> 32);
574 void helper_vprtybq(ppc_avr_t
*r
, ppc_avr_t
*b
)
576 uint64_t res
= b
->u64
[0] ^ b
->u64
[1];
580 r
->VsrD(1) = res
& 1;
584 #define VARITHFP(suffix, func) \
585 void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
590 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
591 r->f32[i] = func(a->f32[i], b->f32[i], &env->vec_status); \
594 VARITHFP(addfp
, float32_add
)
595 VARITHFP(subfp
, float32_sub
)
596 VARITHFP(minfp
, float32_min
)
597 VARITHFP(maxfp
, float32_max
)
600 #define VARITHFPFMA(suffix, type) \
601 void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
602 ppc_avr_t *b, ppc_avr_t *c) \
605 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
606 r->f32[i] = float32_muladd(a->f32[i], c->f32[i], b->f32[i], \
607 type, &env->vec_status); \
610 VARITHFPFMA(maddfp
, 0);
611 VARITHFPFMA(nmsubfp
, float_muladd_negate_result
| float_muladd_negate_c
);
614 #define VARITHSAT_CASE(type, op, cvt, element) \
616 type result = (type)a->element[i] op (type)b->element[i]; \
617 r->element[i] = cvt(result, &sat); \
620 #define VARITHSAT_DO(name, op, optype, cvt, element) \
621 void helper_v##name(ppc_avr_t *r, ppc_avr_t *vscr_sat, \
622 ppc_avr_t *a, ppc_avr_t *b, uint32_t desc) \
627 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
628 VARITHSAT_CASE(optype, op, cvt, element); \
631 vscr_sat->u32[0] = 1; \
634 #define VARITHSAT_SIGNED(suffix, element, optype, cvt) \
635 VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element) \
636 VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
637 #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt) \
638 VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element) \
639 VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
640 VARITHSAT_SIGNED(b
, s8
, int16_t, cvtshsb
)
641 VARITHSAT_SIGNED(h
, s16
, int32_t, cvtswsh
)
642 VARITHSAT_SIGNED(w
, s32
, int64_t, cvtsdsw
)
643 VARITHSAT_UNSIGNED(b
, u8
, uint16_t, cvtshub
)
644 VARITHSAT_UNSIGNED(h
, u16
, uint32_t, cvtswuh
)
645 VARITHSAT_UNSIGNED(w
, u32
, uint64_t, cvtsduw
)
646 #undef VARITHSAT_CASE
648 #undef VARITHSAT_SIGNED
649 #undef VARITHSAT_UNSIGNED
651 #define VAVG_DO(name, element, etype) \
652 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
656 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
657 etype x = (etype)a->element[i] + (etype)b->element[i] + 1; \
658 r->element[i] = x >> 1; \
662 #define VAVG(type, signed_element, signed_type, unsigned_element, \
664 VAVG_DO(avgs##type, signed_element, signed_type) \
665 VAVG_DO(avgu##type, unsigned_element, unsigned_type)
666 VAVG(b
, s8
, int16_t, u8
, uint16_t)
667 VAVG(h
, s16
, int32_t, u16
, uint32_t)
668 VAVG(w
, s32
, int64_t, u32
, uint64_t)
672 #define VABSDU_DO(name, element) \
673 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
677 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
678 r->element[i] = (a->element[i] > b->element[i]) ? \
679 (a->element[i] - b->element[i]) : \
680 (b->element[i] - a->element[i]); \
685 * VABSDU - Vector absolute difference unsigned
686 * name - instruction mnemonic suffix (b: byte, h: halfword, w: word)
687 * element - element type to access from vector
689 #define VABSDU(type, element) \
690 VABSDU_DO(absdu##type, element)
697 #define VCF(suffix, cvt, element) \
698 void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r, \
699 ppc_avr_t *b, uint32_t uim) \
703 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
704 float32 t = cvt(b->element[i], &env->vec_status); \
705 r->f32[i] = float32_scalbn(t, -uim, &env->vec_status); \
708 VCF(ux
, uint32_to_float32
, u32
)
709 VCF(sx
, int32_to_float32
, s32
)
712 #define VCMP_DO(suffix, compare, element, record) \
713 void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r, \
714 ppc_avr_t *a, ppc_avr_t *b) \
716 uint64_t ones = (uint64_t)-1; \
717 uint64_t all = ones; \
721 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
722 uint64_t result = (a->element[i] compare b->element[i] ? \
724 switch (sizeof(a->element[0])) { \
726 r->u64[i] = result; \
729 r->u32[i] = result; \
732 r->u16[i] = result; \
742 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
745 #define VCMP(suffix, compare, element) \
746 VCMP_DO(suffix, compare, element, 0) \
747 VCMP_DO(suffix##_dot, compare, element, 1)
763 #define VCMPNE_DO(suffix, element, etype, cmpzero, record) \
764 void helper_vcmpne##suffix(CPUPPCState *env, ppc_avr_t *r, \
765 ppc_avr_t *a, ppc_avr_t *b) \
767 etype ones = (etype)-1; \
769 etype result, none = 0; \
772 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
774 result = ((a->element[i] == 0) \
775 || (b->element[i] == 0) \
776 || (a->element[i] != b->element[i]) ? \
779 result = (a->element[i] != b->element[i]) ? ones : 0x0; \
781 r->element[i] = result; \
786 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
791 * VCMPNEZ - Vector compare not equal to zero
792 * suffix - instruction mnemonic suffix (b: byte, h: halfword, w: word)
793 * element - element type to access from vector
795 #define VCMPNE(suffix, element, etype, cmpzero) \
796 VCMPNE_DO(suffix, element, etype, cmpzero, 0) \
797 VCMPNE_DO(suffix##_dot, element, etype, cmpzero, 1)
798 VCMPNE(zb
, u8
, uint8_t, 1)
799 VCMPNE(zh
, u16
, uint16_t, 1)
800 VCMPNE(zw
, u32
, uint32_t, 1)
801 VCMPNE(b
, u8
, uint8_t, 0)
802 VCMPNE(h
, u16
, uint16_t, 0)
803 VCMPNE(w
, u32
, uint32_t, 0)
807 #define VCMPFP_DO(suffix, compare, order, record) \
808 void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r, \
809 ppc_avr_t *a, ppc_avr_t *b) \
811 uint32_t ones = (uint32_t)-1; \
812 uint32_t all = ones; \
816 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
818 FloatRelation rel = \
819 float32_compare_quiet(a->f32[i], b->f32[i], \
821 if (rel == float_relation_unordered) { \
823 } else if (rel compare order) { \
828 r->u32[i] = result; \
833 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
836 #define VCMPFP(suffix, compare, order) \
837 VCMPFP_DO(suffix, compare, order, 0) \
838 VCMPFP_DO(suffix##_dot, compare, order, 1)
839 VCMPFP(eqfp
, ==, float_relation_equal
)
840 VCMPFP(gefp
, !=, float_relation_less
)
841 VCMPFP(gtfp
, ==, float_relation_greater
)
845 static inline void vcmpbfp_internal(CPUPPCState
*env
, ppc_avr_t
*r
,
846 ppc_avr_t
*a
, ppc_avr_t
*b
, int record
)
851 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
852 FloatRelation le_rel
= float32_compare_quiet(a
->f32
[i
], b
->f32
[i
],
854 if (le_rel
== float_relation_unordered
) {
855 r
->u32
[i
] = 0xc0000000;
858 float32 bneg
= float32_chs(b
->f32
[i
]);
859 FloatRelation ge_rel
= float32_compare_quiet(a
->f32
[i
], bneg
,
861 int le
= le_rel
!= float_relation_greater
;
862 int ge
= ge_rel
!= float_relation_less
;
864 r
->u32
[i
] = ((!le
) << 31) | ((!ge
) << 30);
865 all_in
|= (!le
| !ge
);
869 env
->crf
[6] = (all_in
== 0) << 1;
873 void helper_vcmpbfp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
875 vcmpbfp_internal(env
, r
, a
, b
, 0);
878 void helper_vcmpbfp_dot(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
881 vcmpbfp_internal(env
, r
, a
, b
, 1);
884 #define VCT(suffix, satcvt, element) \
885 void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r, \
886 ppc_avr_t *b, uint32_t uim) \
890 float_status s = env->vec_status; \
892 set_float_rounding_mode(float_round_to_zero, &s); \
893 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
894 if (float32_is_any_nan(b->f32[i])) { \
897 float64 t = float32_to_float64(b->f32[i], &s); \
900 t = float64_scalbn(t, uim, &s); \
901 j = float64_to_int64(t, &s); \
902 r->element[i] = satcvt(j, &sat); \
909 VCT(uxs
, cvtsduw
, u32
)
910 VCT(sxs
, cvtsdsw
, s32
)
913 target_ulong
helper_vclzlsbb(ppc_avr_t
*r
)
915 target_ulong count
= 0;
917 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
918 if (r
->VsrB(i
) & 0x01) {
926 target_ulong
helper_vctzlsbb(ppc_avr_t
*r
)
928 target_ulong count
= 0;
930 for (i
= ARRAY_SIZE(r
->u8
) - 1; i
>= 0; i
--) {
931 if (r
->VsrB(i
) & 0x01) {
939 void helper_vmhaddshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
940 ppc_avr_t
*b
, ppc_avr_t
*c
)
945 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
946 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
947 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
949 r
->s16
[i
] = cvtswsh(t
, &sat
);
957 void helper_vmhraddshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
958 ppc_avr_t
*b
, ppc_avr_t
*c
)
963 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
964 int32_t prod
= a
->s16
[i
] * b
->s16
[i
] + 0x00004000;
965 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
966 r
->s16
[i
] = cvtswsh(t
, &sat
);
974 void helper_vmladduhm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
978 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
979 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
980 r
->s16
[i
] = (int16_t) (prod
+ c
->s16
[i
]);
984 #define VMRG_DO(name, element, access, ofs) \
985 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
988 int i, half = ARRAY_SIZE(r->element) / 2; \
990 for (i = 0; i < half; i++) { \
991 result.access(i * 2 + 0) = a->access(i + ofs); \
992 result.access(i * 2 + 1) = b->access(i + ofs); \
997 #define VMRG(suffix, element, access) \
998 VMRG_DO(mrgl##suffix, element, access, half) \
999 VMRG_DO(mrgh##suffix, element, access, 0)
1006 void helper_vmsummbm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1007 ppc_avr_t
*b
, ppc_avr_t
*c
)
1012 for (i
= 0; i
< ARRAY_SIZE(r
->s8
); i
++) {
1013 prod
[i
] = (int32_t)a
->s8
[i
] * b
->u8
[i
];
1016 VECTOR_FOR_INORDER_I(i
, s32
) {
1017 r
->s32
[i
] = c
->s32
[i
] + prod
[4 * i
] + prod
[4 * i
+ 1] +
1018 prod
[4 * i
+ 2] + prod
[4 * i
+ 3];
1022 void helper_vmsumshm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1023 ppc_avr_t
*b
, ppc_avr_t
*c
)
1028 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
1029 prod
[i
] = a
->s16
[i
] * b
->s16
[i
];
1032 VECTOR_FOR_INORDER_I(i
, s32
) {
1033 r
->s32
[i
] = c
->s32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
1037 void helper_vmsumshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1038 ppc_avr_t
*b
, ppc_avr_t
*c
)
1044 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
1045 prod
[i
] = (int32_t)a
->s16
[i
] * b
->s16
[i
];
1048 VECTOR_FOR_INORDER_I(i
, s32
) {
1049 int64_t t
= (int64_t)c
->s32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
1051 r
->u32
[i
] = cvtsdsw(t
, &sat
);
1059 void helper_vmsumubm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1060 ppc_avr_t
*b
, ppc_avr_t
*c
)
1065 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1066 prod
[i
] = a
->u8
[i
] * b
->u8
[i
];
1069 VECTOR_FOR_INORDER_I(i
, u32
) {
1070 r
->u32
[i
] = c
->u32
[i
] + prod
[4 * i
] + prod
[4 * i
+ 1] +
1071 prod
[4 * i
+ 2] + prod
[4 * i
+ 3];
1075 void helper_vmsumuhm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1076 ppc_avr_t
*b
, ppc_avr_t
*c
)
1081 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
1082 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
1085 VECTOR_FOR_INORDER_I(i
, u32
) {
1086 r
->u32
[i
] = c
->u32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
1090 void helper_vmsumuhs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
1091 ppc_avr_t
*b
, ppc_avr_t
*c
)
1097 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
1098 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
1101 VECTOR_FOR_INORDER_I(i
, s32
) {
1102 uint64_t t
= (uint64_t)c
->u32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
1104 r
->u32
[i
] = cvtuduw(t
, &sat
);
1112 #define VMUL_DO_EVN(name, mul_element, mul_access, prod_access, cast) \
1113 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1117 for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) { \
1118 r->prod_access(i >> 1) = (cast)a->mul_access(i) * \
1119 (cast)b->mul_access(i); \
1123 #define VMUL_DO_ODD(name, mul_element, mul_access, prod_access, cast) \
1124 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1128 for (i = 0; i < ARRAY_SIZE(r->mul_element); i += 2) { \
1129 r->prod_access(i >> 1) = (cast)a->mul_access(i + 1) * \
1130 (cast)b->mul_access(i + 1); \
1134 #define VMUL(suffix, mul_element, mul_access, prod_access, cast) \
1135 VMUL_DO_EVN(mule##suffix, mul_element, mul_access, prod_access, cast) \
1136 VMUL_DO_ODD(mulo##suffix, mul_element, mul_access, prod_access, cast)
1137 VMUL(sb
, s8
, VsrSB
, VsrSH
, int16_t)
1138 VMUL(sh
, s16
, VsrSH
, VsrSW
, int32_t)
1139 VMUL(sw
, s32
, VsrSW
, VsrSD
, int64_t)
1140 VMUL(ub
, u8
, VsrB
, VsrH
, uint16_t)
1141 VMUL(uh
, u16
, VsrH
, VsrW
, uint32_t)
1142 VMUL(uw
, u32
, VsrW
, VsrD
, uint64_t)
1147 void helper_vmulhsw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1151 for (i
= 0; i
< 4; i
++) {
1152 r
->s32
[i
] = (int32_t)(((int64_t)a
->s32
[i
] * (int64_t)b
->s32
[i
]) >> 32);
1156 void helper_vmulhuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1160 for (i
= 0; i
< 4; i
++) {
1161 r
->u32
[i
] = (uint32_t)(((uint64_t)a
->u32
[i
] *
1162 (uint64_t)b
->u32
[i
]) >> 32);
1166 void helper_vmulhsd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1170 muls64(&discard
, &r
->u64
[0], a
->s64
[0], b
->s64
[0]);
1171 muls64(&discard
, &r
->u64
[1], a
->s64
[1], b
->s64
[1]);
1174 void helper_vmulhud(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1178 mulu64(&discard
, &r
->u64
[0], a
->u64
[0], b
->u64
[0]);
1179 mulu64(&discard
, &r
->u64
[1], a
->u64
[1], b
->u64
[1]);
1182 void helper_vperm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1188 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1189 int s
= c
->VsrB(i
) & 0x1f;
1190 int index
= s
& 0xf;
1193 result
.VsrB(i
) = b
->VsrB(index
);
1195 result
.VsrB(i
) = a
->VsrB(index
);
1201 void helper_vpermr(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1207 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1208 int s
= c
->VsrB(i
) & 0x1f;
1209 int index
= 15 - (s
& 0xf);
1212 result
.VsrB(i
) = a
->VsrB(index
);
1214 result
.VsrB(i
) = b
->VsrB(index
);
1220 #if defined(HOST_WORDS_BIGENDIAN)
1221 #define VBPERMQ_INDEX(avr, i) ((avr)->u8[(i)])
1222 #define VBPERMD_INDEX(i) (i)
1223 #define VBPERMQ_DW(index) (((index) & 0x40) != 0)
1224 #define EXTRACT_BIT(avr, i, index) (extract64((avr)->u64[i], index, 1))
1226 #define VBPERMQ_INDEX(avr, i) ((avr)->u8[15 - (i)])
1227 #define VBPERMD_INDEX(i) (1 - i)
1228 #define VBPERMQ_DW(index) (((index) & 0x40) == 0)
1229 #define EXTRACT_BIT(avr, i, index) \
1230 (extract64((avr)->u64[1 - i], 63 - index, 1))
1233 void helper_vbpermd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1236 ppc_avr_t result
= { .u64
= { 0, 0 } };
1237 VECTOR_FOR_INORDER_I(i
, u64
) {
1238 for (j
= 0; j
< 8; j
++) {
1239 int index
= VBPERMQ_INDEX(b
, (i
* 8) + j
);
1240 if (index
< 64 && EXTRACT_BIT(a
, i
, index
)) {
1241 result
.u64
[VBPERMD_INDEX(i
)] |= (0x80 >> j
);
1248 void helper_vbpermq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1253 VECTOR_FOR_INORDER_I(i
, u8
) {
1254 int index
= VBPERMQ_INDEX(b
, i
);
1257 uint64_t mask
= (1ull << (63 - (index
& 0x3F)));
1258 if (a
->u64
[VBPERMQ_DW(index
)] & mask
) {
1259 perm
|= (0x8000 >> i
);
1268 #undef VBPERMQ_INDEX
1271 #define PMSUM(name, srcfld, trgfld, trgtyp) \
1272 void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1275 trgtyp prod[sizeof(ppc_avr_t) / sizeof(a->srcfld[0])]; \
1277 VECTOR_FOR_INORDER_I(i, srcfld) { \
1279 for (j = 0; j < sizeof(a->srcfld[0]) * 8; j++) { \
1280 if (a->srcfld[i] & (1ull << j)) { \
1281 prod[i] ^= ((trgtyp)b->srcfld[i] << j); \
1286 VECTOR_FOR_INORDER_I(i, trgfld) { \
1287 r->trgfld[i] = prod[2 * i] ^ prod[2 * i + 1]; \
1291 PMSUM(vpmsumb
, u8
, u16
, uint16_t)
1292 PMSUM(vpmsumh
, u16
, u32
, uint32_t)
1293 PMSUM(vpmsumw
, u32
, u64
, uint64_t)
1295 void helper_vpmsumd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1298 #ifdef CONFIG_INT128
1300 __uint128_t prod
[2];
1302 VECTOR_FOR_INORDER_I(i
, u64
) {
1304 for (j
= 0; j
< 64; j
++) {
1305 if (a
->u64
[i
] & (1ull << j
)) {
1306 prod
[i
] ^= (((__uint128_t
)b
->u64
[i
]) << j
);
1311 r
->u128
= prod
[0] ^ prod
[1];
1317 VECTOR_FOR_INORDER_I(i
, u64
) {
1318 prod
[i
].VsrD(1) = prod
[i
].VsrD(0) = 0;
1319 for (j
= 0; j
< 64; j
++) {
1320 if (a
->u64
[i
] & (1ull << j
)) {
1324 bshift
.VsrD(1) = b
->u64
[i
];
1326 bshift
.VsrD(0) = b
->u64
[i
] >> (64 - j
);
1327 bshift
.VsrD(1) = b
->u64
[i
] << j
;
1329 prod
[i
].VsrD(1) ^= bshift
.VsrD(1);
1330 prod
[i
].VsrD(0) ^= bshift
.VsrD(0);
1335 r
->VsrD(1) = prod
[0].VsrD(1) ^ prod
[1].VsrD(1);
1336 r
->VsrD(0) = prod
[0].VsrD(0) ^ prod
[1].VsrD(0);
1341 #if defined(HOST_WORDS_BIGENDIAN)
1346 void helper_vpkpx(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1350 #if defined(HOST_WORDS_BIGENDIAN)
1351 const ppc_avr_t
*x
[2] = { a
, b
};
1353 const ppc_avr_t
*x
[2] = { b
, a
};
1356 VECTOR_FOR_INORDER_I(i
, u64
) {
1357 VECTOR_FOR_INORDER_I(j
, u32
) {
1358 uint32_t e
= x
[i
]->u32
[j
];
1360 result
.u16
[4 * i
+ j
] = (((e
>> 9) & 0xfc00) |
1361 ((e
>> 6) & 0x3e0) |
1368 #define VPK(suffix, from, to, cvt, dosat) \
1369 void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r, \
1370 ppc_avr_t *a, ppc_avr_t *b) \
1375 ppc_avr_t *a0 = PKBIG ? a : b; \
1376 ppc_avr_t *a1 = PKBIG ? b : a; \
1378 VECTOR_FOR_INORDER_I(i, from) { \
1379 result.to[i] = cvt(a0->from[i], &sat); \
1380 result.to[i + ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat);\
1383 if (dosat && sat) { \
1384 set_vscr_sat(env); \
1388 VPK(shss
, s16
, s8
, cvtshsb
, 1)
1389 VPK(shus
, s16
, u8
, cvtshub
, 1)
1390 VPK(swss
, s32
, s16
, cvtswsh
, 1)
1391 VPK(swus
, s32
, u16
, cvtswuh
, 1)
1392 VPK(sdss
, s64
, s32
, cvtsdsw
, 1)
1393 VPK(sdus
, s64
, u32
, cvtsduw
, 1)
1394 VPK(uhus
, u16
, u8
, cvtuhub
, 1)
1395 VPK(uwus
, u32
, u16
, cvtuwuh
, 1)
1396 VPK(udus
, u64
, u32
, cvtuduw
, 1)
1397 VPK(uhum
, u16
, u8
, I
, 0)
1398 VPK(uwum
, u32
, u16
, I
, 0)
1399 VPK(udum
, u64
, u32
, I
, 0)
1404 void helper_vrefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1408 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1409 r
->f32
[i
] = float32_div(float32_one
, b
->f32
[i
], &env
->vec_status
);
1413 #define VRFI(suffix, rounding) \
1414 void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r, \
1418 float_status s = env->vec_status; \
1420 set_float_rounding_mode(rounding, &s); \
1421 for (i = 0; i < ARRAY_SIZE(r->f32); i++) { \
1422 r->f32[i] = float32_round_to_int (b->f32[i], &s); \
1425 VRFI(n
, float_round_nearest_even
)
1426 VRFI(m
, float_round_down
)
1427 VRFI(p
, float_round_up
)
1428 VRFI(z
, float_round_to_zero
)
1431 void helper_vrsqrtefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1435 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1436 float32 t
= float32_sqrt(b
->f32
[i
], &env
->vec_status
);
1438 r
->f32
[i
] = float32_div(float32_one
, t
, &env
->vec_status
);
1442 #define VRLMI(name, size, element, insert) \
1443 void helper_##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1446 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1447 uint##size##_t src1 = a->element[i]; \
1448 uint##size##_t src2 = b->element[i]; \
1449 uint##size##_t src3 = r->element[i]; \
1450 uint##size##_t begin, end, shift, mask, rot_val; \
1452 shift = extract##size(src2, 0, 6); \
1453 end = extract##size(src2, 8, 6); \
1454 begin = extract##size(src2, 16, 6); \
1455 rot_val = rol##size(src1, shift); \
1456 mask = mask_u##size(begin, end); \
1458 r->element[i] = (rot_val & mask) | (src3 & ~mask); \
1460 r->element[i] = (rot_val & mask); \
1465 VRLMI(vrldmi
, 64, u64
, 1);
1466 VRLMI(vrlwmi
, 32, u32
, 1);
1467 VRLMI(vrldnm
, 64, u64
, 0);
1468 VRLMI(vrlwnm
, 32, u32
, 0);
1470 void helper_vsel(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1473 r
->u64
[0] = (a
->u64
[0] & ~c
->u64
[0]) | (b
->u64
[0] & c
->u64
[0]);
1474 r
->u64
[1] = (a
->u64
[1] & ~c
->u64
[1]) | (b
->u64
[1] & c
->u64
[1]);
1477 void helper_vexptefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1481 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1482 r
->f32
[i
] = float32_exp2(b
->f32
[i
], &env
->vec_status
);
1486 void helper_vlogefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1490 for (i
= 0; i
< ARRAY_SIZE(r
->f32
); i
++) {
1491 r
->f32
[i
] = float32_log2(b
->f32
[i
], &env
->vec_status
);
1495 #define VEXTU_X_DO(name, size, left) \
1496 target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b) \
1498 int index = (a & 0xf) * 8; \
1500 index = 128 - index - size; \
1502 return int128_getlo(int128_rshift(b->s128, index)) & \
1503 MAKE_64BIT_MASK(0, size); \
1505 VEXTU_X_DO(vextublx
, 8, 1)
1506 VEXTU_X_DO(vextuhlx
, 16, 1)
1507 VEXTU_X_DO(vextuwlx
, 32, 1)
1508 VEXTU_X_DO(vextubrx
, 8, 0)
1509 VEXTU_X_DO(vextuhrx
, 16, 0)
1510 VEXTU_X_DO(vextuwrx
, 32, 0)
1513 void helper_vslv(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1516 unsigned int shift
, bytes
, size
;
1518 size
= ARRAY_SIZE(r
->u8
);
1519 for (i
= 0; i
< size
; i
++) {
1520 shift
= b
->VsrB(i
) & 0x7; /* extract shift value */
1521 bytes
= (a
->VsrB(i
) << 8) + /* extract adjacent bytes */
1522 (((i
+ 1) < size
) ? a
->VsrB(i
+ 1) : 0);
1523 r
->VsrB(i
) = (bytes
<< shift
) >> 8; /* shift and store result */
1527 void helper_vsrv(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1530 unsigned int shift
, bytes
;
1533 * Use reverse order, as destination and source register can be
1534 * same. Its being modified in place saving temporary, reverse
1535 * order will guarantee that computed result is not fed back.
1537 for (i
= ARRAY_SIZE(r
->u8
) - 1; i
>= 0; i
--) {
1538 shift
= b
->VsrB(i
) & 0x7; /* extract shift value */
1539 bytes
= ((i
? a
->VsrB(i
- 1) : 0) << 8) + a
->VsrB(i
);
1540 /* extract adjacent bytes */
1541 r
->VsrB(i
) = (bytes
>> shift
) & 0xFF; /* shift and store result */
1545 void helper_vsldoi(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t shift
)
1547 int sh
= shift
& 0xf;
1551 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1554 result
.VsrB(i
) = b
->VsrB(index
- 0x10);
1556 result
.VsrB(i
) = a
->VsrB(index
);
1562 void helper_vslo(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1564 int sh
= (b
->VsrB(0xf) >> 3) & 0xf;
1566 #if defined(HOST_WORDS_BIGENDIAN)
1567 memmove(&r
->u8
[0], &a
->u8
[sh
], 16 - sh
);
1568 memset(&r
->u8
[16 - sh
], 0, sh
);
1570 memmove(&r
->u8
[sh
], &a
->u8
[0], 16 - sh
);
1571 memset(&r
->u8
[0], 0, sh
);
1575 #if defined(HOST_WORDS_BIGENDIAN)
1576 #define VINSERT(suffix, element) \
1577 void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1579 memmove(&r->u8[index], &b->u8[8 - sizeof(r->element[0])], \
1580 sizeof(r->element[0])); \
1583 #define VINSERT(suffix, element) \
1584 void helper_vinsert##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1586 uint32_t d = (16 - index) - sizeof(r->element[0]); \
1587 memmove(&r->u8[d], &b->u8[8], sizeof(r->element[0])); \
1595 #if defined(HOST_WORDS_BIGENDIAN)
1596 #define VEXTRACT(suffix, element) \
1597 void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1599 uint32_t es = sizeof(r->element[0]); \
1600 memmove(&r->u8[8 - es], &b->u8[index], es); \
1601 memset(&r->u8[8], 0, 8); \
1602 memset(&r->u8[0], 0, 8 - es); \
1605 #define VEXTRACT(suffix, element) \
1606 void helper_vextract##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t index) \
1608 uint32_t es = sizeof(r->element[0]); \
1609 uint32_t s = (16 - index) - es; \
1610 memmove(&r->u8[8], &b->u8[s], es); \
1611 memset(&r->u8[0], 0, 8); \
1612 memset(&r->u8[8 + es], 0, 8 - es); \
1621 void helper_xxextractuw(CPUPPCState
*env
, ppc_vsr_t
*xt
,
1622 ppc_vsr_t
*xb
, uint32_t index
)
1625 size_t es
= sizeof(uint32_t);
1630 for (i
= 0; i
< es
; i
++, ext_index
++) {
1631 t
.VsrB(8 - es
+ i
) = xb
->VsrB(ext_index
% 16);
1637 void helper_xxinsertw(CPUPPCState
*env
, ppc_vsr_t
*xt
,
1638 ppc_vsr_t
*xb
, uint32_t index
)
1641 size_t es
= sizeof(uint32_t);
1642 int ins_index
, i
= 0;
1645 for (i
= 0; i
< es
&& ins_index
< 16; i
++, ins_index
++) {
1646 t
.VsrB(ins_index
) = xb
->VsrB(8 - es
+ i
);
1652 #define VEXT_SIGNED(name, element, cast) \
1653 void helper_##name(ppc_avr_t *r, ppc_avr_t *b) \
1656 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1657 r->element[i] = (cast)b->element[i]; \
1660 VEXT_SIGNED(vextsb2w
, s32
, int8_t)
1661 VEXT_SIGNED(vextsb2d
, s64
, int8_t)
1662 VEXT_SIGNED(vextsh2w
, s32
, int16_t)
1663 VEXT_SIGNED(vextsh2d
, s64
, int16_t)
1664 VEXT_SIGNED(vextsw2d
, s64
, int32_t)
1667 #define VNEG(name, element) \
1668 void helper_##name(ppc_avr_t *r, ppc_avr_t *b) \
1671 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1672 r->element[i] = -b->element[i]; \
1679 void helper_vsro(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1681 int sh
= (b
->VsrB(0xf) >> 3) & 0xf;
1683 #if defined(HOST_WORDS_BIGENDIAN)
1684 memmove(&r
->u8
[sh
], &a
->u8
[0], 16 - sh
);
1685 memset(&r
->u8
[0], 0, sh
);
1687 memmove(&r
->u8
[0], &a
->u8
[sh
], 16 - sh
);
1688 memset(&r
->u8
[16 - sh
], 0, sh
);
1692 void helper_vsubcuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1696 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
1697 r
->u32
[i
] = a
->u32
[i
] >= b
->u32
[i
];
1701 void helper_vsumsws(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1708 upper
= ARRAY_SIZE(r
->s32
) - 1;
1709 t
= (int64_t)b
->VsrSW(upper
);
1710 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1712 result
.VsrSW(i
) = 0;
1714 result
.VsrSW(upper
) = cvtsdsw(t
, &sat
);
1722 void helper_vsum2sws(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1729 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
1730 int64_t t
= (int64_t)b
->VsrSW(upper
+ i
* 2);
1733 for (j
= 0; j
< ARRAY_SIZE(r
->u64
); j
++) {
1734 t
+= a
->VsrSW(2 * i
+ j
);
1736 result
.VsrSW(upper
+ i
* 2) = cvtsdsw(t
, &sat
);
1745 void helper_vsum4sbs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1750 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1751 int64_t t
= (int64_t)b
->s32
[i
];
1753 for (j
= 0; j
< ARRAY_SIZE(r
->s32
); j
++) {
1754 t
+= a
->s8
[4 * i
+ j
];
1756 r
->s32
[i
] = cvtsdsw(t
, &sat
);
1764 void helper_vsum4shs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1769 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1770 int64_t t
= (int64_t)b
->s32
[i
];
1772 t
+= a
->s16
[2 * i
] + a
->s16
[2 * i
+ 1];
1773 r
->s32
[i
] = cvtsdsw(t
, &sat
);
1781 void helper_vsum4ubs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1786 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
1787 uint64_t t
= (uint64_t)b
->u32
[i
];
1789 for (j
= 0; j
< ARRAY_SIZE(r
->u32
); j
++) {
1790 t
+= a
->u8
[4 * i
+ j
];
1792 r
->u32
[i
] = cvtuduw(t
, &sat
);
1800 #if defined(HOST_WORDS_BIGENDIAN)
1807 #define VUPKPX(suffix, hi) \
1808 void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b) \
1813 for (i = 0; i < ARRAY_SIZE(r->u32); i++) { \
1814 uint16_t e = b->u16[hi ? i : i + 4]; \
1815 uint8_t a = (e >> 15) ? 0xff : 0; \
1816 uint8_t r = (e >> 10) & 0x1f; \
1817 uint8_t g = (e >> 5) & 0x1f; \
1818 uint8_t b = e & 0x1f; \
1820 result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b; \
1828 #define VUPK(suffix, unpacked, packee, hi) \
1829 void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b) \
1835 for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) { \
1836 result.unpacked[i] = b->packee[i]; \
1839 for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
1841 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
1846 VUPK(hsb
, s16
, s8
, UPKHI
)
1847 VUPK(hsh
, s32
, s16
, UPKHI
)
1848 VUPK(hsw
, s64
, s32
, UPKHI
)
1849 VUPK(lsb
, s16
, s8
, UPKLO
)
1850 VUPK(lsh
, s32
, s16
, UPKLO
)
1851 VUPK(lsw
, s64
, s32
, UPKLO
)
1856 #define VGENERIC_DO(name, element) \
1857 void helper_v##name(ppc_avr_t *r, ppc_avr_t *b) \
1861 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1862 r->element[i] = name(b->element[i]); \
1866 #define clzb(v) ((v) ? clz32((uint32_t)(v) << 24) : 8)
1867 #define clzh(v) ((v) ? clz32((uint32_t)(v) << 16) : 16)
1869 VGENERIC_DO(clzb
, u8
)
1870 VGENERIC_DO(clzh
, u16
)
1875 #define ctzb(v) ((v) ? ctz32(v) : 8)
1876 #define ctzh(v) ((v) ? ctz32(v) : 16)
1877 #define ctzw(v) ctz32((v))
1878 #define ctzd(v) ctz64((v))
1880 VGENERIC_DO(ctzb
, u8
)
1881 VGENERIC_DO(ctzh
, u16
)
1882 VGENERIC_DO(ctzw
, u32
)
1883 VGENERIC_DO(ctzd
, u64
)
1890 #define popcntb(v) ctpop8(v)
1891 #define popcnth(v) ctpop16(v)
1892 #define popcntw(v) ctpop32(v)
1893 #define popcntd(v) ctpop64(v)
1895 VGENERIC_DO(popcntb
, u8
)
1896 VGENERIC_DO(popcnth
, u16
)
1897 VGENERIC_DO(popcntw
, u32
)
1898 VGENERIC_DO(popcntd
, u64
)
1907 #if defined(HOST_WORDS_BIGENDIAN)
1908 #define QW_ONE { .u64 = { 0, 1 } }
1910 #define QW_ONE { .u64 = { 1, 0 } }
1913 #ifndef CONFIG_INT128
1915 static inline void avr_qw_not(ppc_avr_t
*t
, ppc_avr_t a
)
1917 t
->u64
[0] = ~a
.u64
[0];
1918 t
->u64
[1] = ~a
.u64
[1];
1921 static int avr_qw_cmpu(ppc_avr_t a
, ppc_avr_t b
)
1923 if (a
.VsrD(0) < b
.VsrD(0)) {
1925 } else if (a
.VsrD(0) > b
.VsrD(0)) {
1927 } else if (a
.VsrD(1) < b
.VsrD(1)) {
1929 } else if (a
.VsrD(1) > b
.VsrD(1)) {
1936 static void avr_qw_add(ppc_avr_t
*t
, ppc_avr_t a
, ppc_avr_t b
)
1938 t
->VsrD(1) = a
.VsrD(1) + b
.VsrD(1);
1939 t
->VsrD(0) = a
.VsrD(0) + b
.VsrD(0) +
1940 (~a
.VsrD(1) < b
.VsrD(1));
1943 static int avr_qw_addc(ppc_avr_t
*t
, ppc_avr_t a
, ppc_avr_t b
)
1946 t
->VsrD(1) = a
.VsrD(1) + b
.VsrD(1);
1947 t
->VsrD(0) = a
.VsrD(0) + b
.VsrD(0) +
1948 (~a
.VsrD(1) < b
.VsrD(1));
1949 avr_qw_not(¬_a
, a
);
1950 return avr_qw_cmpu(not_a
, b
) < 0;
1955 void helper_vadduqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1957 #ifdef CONFIG_INT128
1958 r
->u128
= a
->u128
+ b
->u128
;
1960 avr_qw_add(r
, *a
, *b
);
1964 void helper_vaddeuqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
1966 #ifdef CONFIG_INT128
1967 r
->u128
= a
->u128
+ b
->u128
+ (c
->u128
& 1);
1970 if (c
->VsrD(1) & 1) {
1974 tmp
.VsrD(1) = c
->VsrD(1) & 1;
1975 avr_qw_add(&tmp
, *a
, tmp
);
1976 avr_qw_add(r
, tmp
, *b
);
1978 avr_qw_add(r
, *a
, *b
);
1983 void helper_vaddcuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1985 #ifdef CONFIG_INT128
1986 r
->u128
= (~a
->u128
< b
->u128
);
1990 avr_qw_not(¬_a
, *a
);
1993 r
->VsrD(1) = (avr_qw_cmpu(not_a
, *b
) < 0);
1997 void helper_vaddecuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
1999 #ifdef CONFIG_INT128
2000 int carry_out
= (~a
->u128
< b
->u128
);
2001 if (!carry_out
&& (c
->u128
& 1)) {
2002 carry_out
= ((a
->u128
+ b
->u128
+ 1) == 0) &&
2003 ((a
->u128
!= 0) || (b
->u128
!= 0));
2005 r
->u128
= carry_out
;
2008 int carry_in
= c
->VsrD(1) & 1;
2012 carry_out
= avr_qw_addc(&tmp
, *a
, *b
);
2014 if (!carry_out
&& carry_in
) {
2015 ppc_avr_t one
= QW_ONE
;
2016 carry_out
= avr_qw_addc(&tmp
, tmp
, one
);
2019 r
->VsrD(1) = carry_out
;
2023 void helper_vsubuqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2025 #ifdef CONFIG_INT128
2026 r
->u128
= a
->u128
- b
->u128
;
2029 ppc_avr_t one
= QW_ONE
;
2031 avr_qw_not(&tmp
, *b
);
2032 avr_qw_add(&tmp
, *a
, tmp
);
2033 avr_qw_add(r
, tmp
, one
);
2037 void helper_vsubeuqm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2039 #ifdef CONFIG_INT128
2040 r
->u128
= a
->u128
+ ~b
->u128
+ (c
->u128
& 1);
2044 avr_qw_not(&tmp
, *b
);
2045 avr_qw_add(&sum
, *a
, tmp
);
2048 tmp
.VsrD(1) = c
->VsrD(1) & 1;
2049 avr_qw_add(r
, sum
, tmp
);
2053 void helper_vsubcuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2055 #ifdef CONFIG_INT128
2056 r
->u128
= (~a
->u128
< ~b
->u128
) ||
2057 (a
->u128
+ ~b
->u128
== (__uint128_t
)-1);
2059 int carry
= (avr_qw_cmpu(*a
, *b
) > 0);
2062 avr_qw_not(&tmp
, *b
);
2063 avr_qw_add(&tmp
, *a
, tmp
);
2064 carry
= ((tmp
.VsrSD(0) == -1ull) && (tmp
.VsrSD(1) == -1ull));
2071 void helper_vsubecuq(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2073 #ifdef CONFIG_INT128
2075 (~a
->u128
< ~b
->u128
) ||
2076 ((c
->u128
& 1) && (a
->u128
+ ~b
->u128
== (__uint128_t
)-1));
2078 int carry_in
= c
->VsrD(1) & 1;
2079 int carry_out
= (avr_qw_cmpu(*a
, *b
) > 0);
2080 if (!carry_out
&& carry_in
) {
2082 avr_qw_not(&tmp
, *b
);
2083 avr_qw_add(&tmp
, *a
, tmp
);
2084 carry_out
= ((tmp
.VsrD(0) == -1ull) && (tmp
.VsrD(1) == -1ull));
2088 r
->VsrD(1) = carry_out
;
2092 #define BCD_PLUS_PREF_1 0xC
2093 #define BCD_PLUS_PREF_2 0xF
2094 #define BCD_PLUS_ALT_1 0xA
2095 #define BCD_NEG_PREF 0xD
2096 #define BCD_NEG_ALT 0xB
2097 #define BCD_PLUS_ALT_2 0xE
2098 #define NATIONAL_PLUS 0x2B
2099 #define NATIONAL_NEG 0x2D
2101 #define BCD_DIG_BYTE(n) (15 - ((n) / 2))
2103 static int bcd_get_sgn(ppc_avr_t
*bcd
)
2105 switch (bcd
->VsrB(BCD_DIG_BYTE(0)) & 0xF) {
2106 case BCD_PLUS_PREF_1
:
2107 case BCD_PLUS_PREF_2
:
2108 case BCD_PLUS_ALT_1
:
2109 case BCD_PLUS_ALT_2
:
2127 static int bcd_preferred_sgn(int sgn
, int ps
)
2130 return (ps
== 0) ? BCD_PLUS_PREF_1
: BCD_PLUS_PREF_2
;
2132 return BCD_NEG_PREF
;
2136 static uint8_t bcd_get_digit(ppc_avr_t
*bcd
, int n
, int *invalid
)
2140 result
= bcd
->VsrB(BCD_DIG_BYTE(n
)) >> 4;
2142 result
= bcd
->VsrB(BCD_DIG_BYTE(n
)) & 0xF;
2145 if (unlikely(result
> 9)) {
2151 static void bcd_put_digit(ppc_avr_t
*bcd
, uint8_t digit
, int n
)
2154 bcd
->VsrB(BCD_DIG_BYTE(n
)) &= 0x0F;
2155 bcd
->VsrB(BCD_DIG_BYTE(n
)) |= (digit
<< 4);
2157 bcd
->VsrB(BCD_DIG_BYTE(n
)) &= 0xF0;
2158 bcd
->VsrB(BCD_DIG_BYTE(n
)) |= digit
;
2162 static bool bcd_is_valid(ppc_avr_t
*bcd
)
2167 if (bcd_get_sgn(bcd
) == 0) {
2171 for (i
= 1; i
< 32; i
++) {
2172 bcd_get_digit(bcd
, i
, &invalid
);
2173 if (unlikely(invalid
)) {
2180 static int bcd_cmp_zero(ppc_avr_t
*bcd
)
2182 if (bcd
->VsrD(0) == 0 && (bcd
->VsrD(1) >> 4) == 0) {
2185 return (bcd_get_sgn(bcd
) == 1) ? CRF_GT
: CRF_LT
;
2189 static uint16_t get_national_digit(ppc_avr_t
*reg
, int n
)
2191 return reg
->VsrH(7 - n
);
2194 static void set_national_digit(ppc_avr_t
*reg
, uint8_t val
, int n
)
2196 reg
->VsrH(7 - n
) = val
;
2199 static int bcd_cmp_mag(ppc_avr_t
*a
, ppc_avr_t
*b
)
2203 for (i
= 31; i
> 0; i
--) {
2204 uint8_t dig_a
= bcd_get_digit(a
, i
, &invalid
);
2205 uint8_t dig_b
= bcd_get_digit(b
, i
, &invalid
);
2206 if (unlikely(invalid
)) {
2207 return 0; /* doesn't matter */
2208 } else if (dig_a
> dig_b
) {
2210 } else if (dig_a
< dig_b
) {
2218 static int bcd_add_mag(ppc_avr_t
*t
, ppc_avr_t
*a
, ppc_avr_t
*b
, int *invalid
,
2225 for (i
= 1; i
<= 31; i
++) {
2226 uint8_t digit
= bcd_get_digit(a
, i
, invalid
) +
2227 bcd_get_digit(b
, i
, invalid
) + carry
;
2228 is_zero
&= (digit
== 0);
2236 bcd_put_digit(t
, digit
, i
);
2243 static void bcd_sub_mag(ppc_avr_t
*t
, ppc_avr_t
*a
, ppc_avr_t
*b
, int *invalid
,
2249 for (i
= 1; i
<= 31; i
++) {
2250 uint8_t digit
= bcd_get_digit(a
, i
, invalid
) -
2251 bcd_get_digit(b
, i
, invalid
) + carry
;
2259 bcd_put_digit(t
, digit
, i
);
2265 uint32_t helper_bcdadd(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2268 int sgna
= bcd_get_sgn(a
);
2269 int sgnb
= bcd_get_sgn(b
);
2270 int invalid
= (sgna
== 0) || (sgnb
== 0);
2274 ppc_avr_t result
= { .u64
= { 0, 0 } };
2278 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna
, ps
);
2279 zero
= bcd_add_mag(&result
, a
, b
, &invalid
, &overflow
);
2280 cr
= (sgna
> 0) ? CRF_GT
: CRF_LT
;
2282 int magnitude
= bcd_cmp_mag(a
, b
);
2283 if (magnitude
> 0) {
2284 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgna
, ps
);
2285 bcd_sub_mag(&result
, a
, b
, &invalid
, &overflow
);
2286 cr
= (sgna
> 0) ? CRF_GT
: CRF_LT
;
2287 } else if (magnitude
< 0) {
2288 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(sgnb
, ps
);
2289 bcd_sub_mag(&result
, b
, a
, &invalid
, &overflow
);
2290 cr
= (sgnb
> 0) ? CRF_GT
: CRF_LT
;
2292 result
.VsrB(BCD_DIG_BYTE(0)) = bcd_preferred_sgn(0, ps
);
2298 if (unlikely(invalid
)) {
2299 result
.VsrD(0) = result
.VsrD(1) = -1;
2301 } else if (overflow
) {
2312 uint32_t helper_bcdsub(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2314 ppc_avr_t bcopy
= *b
;
2315 int sgnb
= bcd_get_sgn(b
);
2317 bcd_put_digit(&bcopy
, BCD_PLUS_PREF_1
, 0);
2318 } else if (sgnb
> 0) {
2319 bcd_put_digit(&bcopy
, BCD_NEG_PREF
, 0);
2321 /* else invalid ... defer to bcdadd code for proper handling */
2323 return helper_bcdadd(r
, a
, &bcopy
, ps
);
2326 uint32_t helper_bcdcfn(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2330 uint16_t national
= 0;
2331 uint16_t sgnb
= get_national_digit(b
, 0);
2332 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2333 int invalid
= (sgnb
!= NATIONAL_PLUS
&& sgnb
!= NATIONAL_NEG
);
2335 for (i
= 1; i
< 8; i
++) {
2336 national
= get_national_digit(b
, i
);
2337 if (unlikely(national
< 0x30 || national
> 0x39)) {
2342 bcd_put_digit(&ret
, national
& 0xf, i
);
2345 if (sgnb
== NATIONAL_PLUS
) {
2346 bcd_put_digit(&ret
, (ps
== 0) ? BCD_PLUS_PREF_1
: BCD_PLUS_PREF_2
, 0);
2348 bcd_put_digit(&ret
, BCD_NEG_PREF
, 0);
2351 cr
= bcd_cmp_zero(&ret
);
2353 if (unlikely(invalid
)) {
2362 uint32_t helper_bcdctn(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2366 int sgnb
= bcd_get_sgn(b
);
2367 int invalid
= (sgnb
== 0);
2368 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2370 int ox_flag
= (b
->VsrD(0) != 0) || ((b
->VsrD(1) >> 32) != 0);
2372 for (i
= 1; i
< 8; i
++) {
2373 set_national_digit(&ret
, 0x30 + bcd_get_digit(b
, i
, &invalid
), i
);
2375 if (unlikely(invalid
)) {
2379 set_national_digit(&ret
, (sgnb
== -1) ? NATIONAL_NEG
: NATIONAL_PLUS
, 0);
2381 cr
= bcd_cmp_zero(b
);
2387 if (unlikely(invalid
)) {
2396 uint32_t helper_bcdcfz(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2402 int zone_lead
= ps
? 0xF : 0x3;
2404 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2405 int sgnb
= b
->VsrB(BCD_DIG_BYTE(0)) >> 4;
2407 if (unlikely((sgnb
< 0xA) && ps
)) {
2411 for (i
= 0; i
< 16; i
++) {
2412 zone_digit
= i
? b
->VsrB(BCD_DIG_BYTE(i
* 2)) >> 4 : zone_lead
;
2413 digit
= b
->VsrB(BCD_DIG_BYTE(i
* 2)) & 0xF;
2414 if (unlikely(zone_digit
!= zone_lead
|| digit
> 0x9)) {
2419 bcd_put_digit(&ret
, digit
, i
+ 1);
2422 if ((ps
&& (sgnb
== 0xB || sgnb
== 0xD)) ||
2423 (!ps
&& (sgnb
& 0x4))) {
2424 bcd_put_digit(&ret
, BCD_NEG_PREF
, 0);
2426 bcd_put_digit(&ret
, BCD_PLUS_PREF_1
, 0);
2429 cr
= bcd_cmp_zero(&ret
);
2431 if (unlikely(invalid
)) {
2440 uint32_t helper_bcdctz(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2445 int sgnb
= bcd_get_sgn(b
);
2446 int zone_lead
= (ps
) ? 0xF0 : 0x30;
2447 int invalid
= (sgnb
== 0);
2448 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2450 int ox_flag
= ((b
->VsrD(0) >> 4) != 0);
2452 for (i
= 0; i
< 16; i
++) {
2453 digit
= bcd_get_digit(b
, i
+ 1, &invalid
);
2455 if (unlikely(invalid
)) {
2459 ret
.VsrB(BCD_DIG_BYTE(i
* 2)) = zone_lead
+ digit
;
2463 bcd_put_digit(&ret
, (sgnb
== 1) ? 0xC : 0xD, 1);
2465 bcd_put_digit(&ret
, (sgnb
== 1) ? 0x3 : 0x7, 1);
2468 cr
= bcd_cmp_zero(b
);
2474 if (unlikely(invalid
)) {
2483 uint32_t helper_bcdcfsq(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2489 ppc_avr_t ret
= { .u64
= { 0, 0 } };
2491 if (b
->VsrSD(0) < 0) {
2492 lo_value
= -b
->VsrSD(1);
2493 hi_value
= ~b
->VsrD(0) + !lo_value
;
2494 bcd_put_digit(&ret
, 0xD, 0);
2496 lo_value
= b
->VsrD(1);
2497 hi_value
= b
->VsrD(0);
2498 bcd_put_digit(&ret
, bcd_preferred_sgn(0, ps
), 0);
2501 if (divu128(&lo_value
, &hi_value
, 1000000000000000ULL) ||
2502 lo_value
> 9999999999999999ULL) {
2506 for (i
= 1; i
< 16; hi_value
/= 10, i
++) {
2507 bcd_put_digit(&ret
, hi_value
% 10, i
);
2510 for (; i
< 32; lo_value
/= 10, i
++) {
2511 bcd_put_digit(&ret
, lo_value
% 10, i
);
2514 cr
|= bcd_cmp_zero(&ret
);
2521 uint32_t helper_bcdctsq(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2528 uint64_t hi_value
= 0;
2529 int sgnb
= bcd_get_sgn(b
);
2530 int invalid
= (sgnb
== 0);
2532 lo_value
= bcd_get_digit(b
, 31, &invalid
);
2533 for (i
= 30; i
> 0; i
--) {
2534 mulu64(&lo_value
, &carry
, lo_value
, 10ULL);
2535 mulu64(&hi_value
, &unused
, hi_value
, 10ULL);
2536 lo_value
+= bcd_get_digit(b
, i
, &invalid
);
2539 if (unlikely(invalid
)) {
2545 r
->VsrSD(1) = -lo_value
;
2546 r
->VsrSD(0) = ~hi_value
+ !r
->VsrSD(1);
2548 r
->VsrSD(1) = lo_value
;
2549 r
->VsrSD(0) = hi_value
;
2552 cr
= bcd_cmp_zero(b
);
2554 if (unlikely(invalid
)) {
2561 uint32_t helper_bcdcpsgn(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2566 if (bcd_get_sgn(a
) == 0 || bcd_get_sgn(b
) == 0) {
2571 bcd_put_digit(r
, b
->VsrB(BCD_DIG_BYTE(0)) & 0xF, 0);
2573 for (i
= 1; i
< 32; i
++) {
2574 bcd_get_digit(a
, i
, &invalid
);
2575 bcd_get_digit(b
, i
, &invalid
);
2576 if (unlikely(invalid
)) {
2581 return bcd_cmp_zero(r
);
2584 uint32_t helper_bcdsetsgn(ppc_avr_t
*r
, ppc_avr_t
*b
, uint32_t ps
)
2586 int sgnb
= bcd_get_sgn(b
);
2589 bcd_put_digit(r
, bcd_preferred_sgn(sgnb
, ps
), 0);
2591 if (bcd_is_valid(b
) == false) {
2595 return bcd_cmp_zero(r
);
2598 uint32_t helper_bcds(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2601 int i
= a
->VsrSB(7);
2602 bool ox_flag
= false;
2603 int sgnb
= bcd_get_sgn(b
);
2605 ret
.VsrD(1) &= ~0xf;
2607 if (bcd_is_valid(b
) == false) {
2611 if (unlikely(i
> 31)) {
2613 } else if (unlikely(i
< -31)) {
2618 ulshift(&ret
.VsrD(1), &ret
.VsrD(0), i
* 4, &ox_flag
);
2620 urshift(&ret
.VsrD(1), &ret
.VsrD(0), -i
* 4);
2622 bcd_put_digit(&ret
, bcd_preferred_sgn(sgnb
, ps
), 0);
2626 cr
= bcd_cmp_zero(r
);
2634 uint32_t helper_bcdus(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2639 bool ox_flag
= false;
2642 for (i
= 0; i
< 32; i
++) {
2643 bcd_get_digit(b
, i
, &invalid
);
2645 if (unlikely(invalid
)) {
2653 ret
.VsrD(1) = ret
.VsrD(0) = 0;
2654 } else if (i
<= -32) {
2655 ret
.VsrD(1) = ret
.VsrD(0) = 0;
2657 ulshift(&ret
.VsrD(1), &ret
.VsrD(0), i
* 4, &ox_flag
);
2659 urshift(&ret
.VsrD(1), &ret
.VsrD(0), -i
* 4);
2663 cr
= bcd_cmp_zero(r
);
2671 uint32_t helper_bcdsr(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2676 bool ox_flag
= false;
2677 int sgnb
= bcd_get_sgn(b
);
2679 ret
.VsrD(1) &= ~0xf;
2681 int i
= a
->VsrSB(7);
2684 bcd_one
.VsrD(0) = 0;
2685 bcd_one
.VsrD(1) = 0x10;
2687 if (bcd_is_valid(b
) == false) {
2691 if (unlikely(i
> 31)) {
2693 } else if (unlikely(i
< -31)) {
2698 ulshift(&ret
.VsrD(1), &ret
.VsrD(0), i
* 4, &ox_flag
);
2700 urshift(&ret
.VsrD(1), &ret
.VsrD(0), -i
* 4);
2702 if (bcd_get_digit(&ret
, 0, &invalid
) >= 5) {
2703 bcd_add_mag(&ret
, &ret
, &bcd_one
, &invalid
, &unused
);
2706 bcd_put_digit(&ret
, bcd_preferred_sgn(sgnb
, ps
), 0);
2708 cr
= bcd_cmp_zero(&ret
);
2717 uint32_t helper_bcdtrunc(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2720 uint32_t ox_flag
= 0;
2721 int i
= a
->VsrSH(3) + 1;
2724 if (bcd_is_valid(b
) == false) {
2728 if (i
> 16 && i
< 32) {
2729 mask
= (uint64_t)-1 >> (128 - i
* 4);
2730 if (ret
.VsrD(0) & ~mask
) {
2734 ret
.VsrD(0) &= mask
;
2735 } else if (i
>= 0 && i
<= 16) {
2736 mask
= (uint64_t)-1 >> (64 - i
* 4);
2737 if (ret
.VsrD(0) || (ret
.VsrD(1) & ~mask
)) {
2741 ret
.VsrD(1) &= mask
;
2744 bcd_put_digit(&ret
, bcd_preferred_sgn(bcd_get_sgn(b
), ps
), 0);
2747 return bcd_cmp_zero(&ret
) | ox_flag
;
2750 uint32_t helper_bcdutrunc(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t ps
)
2754 uint32_t ox_flag
= 0;
2758 for (i
= 0; i
< 32; i
++) {
2759 bcd_get_digit(b
, i
, &invalid
);
2761 if (unlikely(invalid
)) {
2767 if (i
> 16 && i
< 33) {
2768 mask
= (uint64_t)-1 >> (128 - i
* 4);
2769 if (ret
.VsrD(0) & ~mask
) {
2773 ret
.VsrD(0) &= mask
;
2774 } else if (i
> 0 && i
<= 16) {
2775 mask
= (uint64_t)-1 >> (64 - i
* 4);
2776 if (ret
.VsrD(0) || (ret
.VsrD(1) & ~mask
)) {
2780 ret
.VsrD(1) &= mask
;
2782 } else if (i
== 0) {
2783 if (ret
.VsrD(0) || ret
.VsrD(1)) {
2786 ret
.VsrD(0) = ret
.VsrD(1) = 0;
2790 if (r
->VsrD(0) == 0 && r
->VsrD(1) == 0) {
2791 return ox_flag
| CRF_EQ
;
2794 return ox_flag
| CRF_GT
;
2797 void helper_vsbox(ppc_avr_t
*r
, ppc_avr_t
*a
)
2800 VECTOR_FOR_INORDER_I(i
, u8
) {
2801 r
->u8
[i
] = AES_sbox
[a
->u8
[i
]];
2805 void helper_vcipher(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2810 VECTOR_FOR_INORDER_I(i
, u32
) {
2811 result
.VsrW(i
) = b
->VsrW(i
) ^
2812 (AES_Te0
[a
->VsrB(AES_shifts
[4 * i
+ 0])] ^
2813 AES_Te1
[a
->VsrB(AES_shifts
[4 * i
+ 1])] ^
2814 AES_Te2
[a
->VsrB(AES_shifts
[4 * i
+ 2])] ^
2815 AES_Te3
[a
->VsrB(AES_shifts
[4 * i
+ 3])]);
2820 void helper_vcipherlast(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2825 VECTOR_FOR_INORDER_I(i
, u8
) {
2826 result
.VsrB(i
) = b
->VsrB(i
) ^ (AES_sbox
[a
->VsrB(AES_shifts
[i
])]);
2831 void helper_vncipher(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2833 /* This differs from what is written in ISA V2.07. The RTL is */
2834 /* incorrect and will be fixed in V2.07B. */
2838 VECTOR_FOR_INORDER_I(i
, u8
) {
2839 tmp
.VsrB(i
) = b
->VsrB(i
) ^ AES_isbox
[a
->VsrB(AES_ishifts
[i
])];
2842 VECTOR_FOR_INORDER_I(i
, u32
) {
2844 AES_imc
[tmp
.VsrB(4 * i
+ 0)][0] ^
2845 AES_imc
[tmp
.VsrB(4 * i
+ 1)][1] ^
2846 AES_imc
[tmp
.VsrB(4 * i
+ 2)][2] ^
2847 AES_imc
[tmp
.VsrB(4 * i
+ 3)][3];
2851 void helper_vncipherlast(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
2856 VECTOR_FOR_INORDER_I(i
, u8
) {
2857 result
.VsrB(i
) = b
->VsrB(i
) ^ (AES_isbox
[a
->VsrB(AES_ishifts
[i
])]);
2862 void helper_vshasigmaw(ppc_avr_t
*r
, ppc_avr_t
*a
, uint32_t st_six
)
2864 int st
= (st_six
& 0x10) != 0;
2865 int six
= st_six
& 0xF;
2868 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
2870 if ((six
& (0x8 >> i
)) == 0) {
2871 r
->VsrW(i
) = ror32(a
->VsrW(i
), 7) ^
2872 ror32(a
->VsrW(i
), 18) ^
2874 } else { /* six.bit[i] == 1 */
2875 r
->VsrW(i
) = ror32(a
->VsrW(i
), 17) ^
2876 ror32(a
->VsrW(i
), 19) ^
2879 } else { /* st == 1 */
2880 if ((six
& (0x8 >> i
)) == 0) {
2881 r
->VsrW(i
) = ror32(a
->VsrW(i
), 2) ^
2882 ror32(a
->VsrW(i
), 13) ^
2883 ror32(a
->VsrW(i
), 22);
2884 } else { /* six.bit[i] == 1 */
2885 r
->VsrW(i
) = ror32(a
->VsrW(i
), 6) ^
2886 ror32(a
->VsrW(i
), 11) ^
2887 ror32(a
->VsrW(i
), 25);
2893 void helper_vshasigmad(ppc_avr_t
*r
, ppc_avr_t
*a
, uint32_t st_six
)
2895 int st
= (st_six
& 0x10) != 0;
2896 int six
= st_six
& 0xF;
2899 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
2901 if ((six
& (0x8 >> (2 * i
))) == 0) {
2902 r
->VsrD(i
) = ror64(a
->VsrD(i
), 1) ^
2903 ror64(a
->VsrD(i
), 8) ^
2905 } else { /* six.bit[2*i] == 1 */
2906 r
->VsrD(i
) = ror64(a
->VsrD(i
), 19) ^
2907 ror64(a
->VsrD(i
), 61) ^
2910 } else { /* st == 1 */
2911 if ((six
& (0x8 >> (2 * i
))) == 0) {
2912 r
->VsrD(i
) = ror64(a
->VsrD(i
), 28) ^
2913 ror64(a
->VsrD(i
), 34) ^
2914 ror64(a
->VsrD(i
), 39);
2915 } else { /* six.bit[2*i] == 1 */
2916 r
->VsrD(i
) = ror64(a
->VsrD(i
), 14) ^
2917 ror64(a
->VsrD(i
), 18) ^
2918 ror64(a
->VsrD(i
), 41);
2924 void helper_vpermxor(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
2929 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
2930 int indexA
= c
->VsrB(i
) >> 4;
2931 int indexB
= c
->VsrB(i
) & 0xF;
2933 result
.VsrB(i
) = a
->VsrB(indexA
) ^ b
->VsrB(indexB
);
2938 #undef VECTOR_FOR_INORDER_I
2940 /*****************************************************************************/
2941 /* SPE extension helpers */
2942 /* Use a table to make this quicker */
2943 static const uint8_t hbrev
[16] = {
2944 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
2945 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
2948 static inline uint8_t byte_reverse(uint8_t val
)
2950 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
2953 static inline uint32_t word_reverse(uint32_t val
)
2955 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
2956 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
2959 #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
2960 target_ulong
helper_brinc(target_ulong arg1
, target_ulong arg2
)
2962 uint32_t a
, b
, d
, mask
;
2964 mask
= UINT32_MAX
>> (32 - MASKBITS
);
2967 d
= word_reverse(1 + word_reverse(a
| ~b
));
2968 return (arg1
& ~mask
) | (d
& b
);
2971 uint32_t helper_cntlsw32(uint32_t val
)
2973 if (val
& 0x80000000) {
2980 uint32_t helper_cntlzw32(uint32_t val
)
2986 target_ulong
helper_dlmzb(CPUPPCState
*env
, target_ulong high
,
2987 target_ulong low
, uint32_t update_Rc
)
2993 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
2994 if ((high
& mask
) == 0) {
3002 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
3003 if ((low
& mask
) == 0) {
3016 env
->xer
= (env
->xer
& ~0x7F) | i
;
3018 env
->crf
[0] |= xer_so
;