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 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 "host-utils.h"
23 #include "helper_regs.h"
24 /*****************************************************************************/
25 /* Fixed point operations helpers */
26 #if defined(TARGET_PPC64)
28 /* multiply high word */
29 uint64_t helper_mulhd(uint64_t arg1
, uint64_t arg2
)
33 muls64(&tl
, &th
, arg1
, arg2
);
37 /* multiply high word unsigned */
38 uint64_t helper_mulhdu(uint64_t arg1
, uint64_t arg2
)
42 mulu64(&tl
, &th
, arg1
, arg2
);
46 uint64_t helper_mulldo(CPUPPCState
*env
, uint64_t arg1
, uint64_t arg2
)
51 muls64(&tl
, (uint64_t *)&th
, arg1
, arg2
);
52 /* If th != 0 && th != -1, then we had an overflow */
53 if (likely((uint64_t)(th
+ 1) <= 1)) {
54 env
->xer
&= ~(1 << XER_OV
);
56 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
62 target_ulong
helper_cntlzw(target_ulong t
)
67 #if defined(TARGET_PPC64)
68 target_ulong
helper_cntlzd(target_ulong t
)
74 /* shift right arithmetic helper */
75 target_ulong
helper_sraw(CPUPPCState
*env
, target_ulong value
,
80 if (likely(!(shift
& 0x20))) {
81 if (likely((uint32_t)shift
!= 0)) {
83 ret
= (int32_t)value
>> shift
;
84 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
85 env
->xer
&= ~(1 << XER_CA
);
87 env
->xer
|= (1 << XER_CA
);
91 env
->xer
&= ~(1 << XER_CA
);
94 ret
= (int32_t)value
>> 31;
96 env
->xer
|= (1 << XER_CA
);
98 env
->xer
&= ~(1 << XER_CA
);
101 return (target_long
)ret
;
104 #if defined(TARGET_PPC64)
105 target_ulong
helper_srad(CPUPPCState
*env
, target_ulong value
,
110 if (likely(!(shift
& 0x40))) {
111 if (likely((uint64_t)shift
!= 0)) {
113 ret
= (int64_t)value
>> shift
;
114 if (likely(ret
>= 0 || (value
& ((1 << shift
) - 1)) == 0)) {
115 env
->xer
&= ~(1 << XER_CA
);
117 env
->xer
|= (1 << XER_CA
);
120 ret
= (int64_t)value
;
121 env
->xer
&= ~(1 << XER_CA
);
124 ret
= (int64_t)value
>> 63;
126 env
->xer
|= (1 << XER_CA
);
128 env
->xer
&= ~(1 << XER_CA
);
135 #if defined(TARGET_PPC64)
136 target_ulong
helper_popcntb(target_ulong val
)
138 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
139 0x5555555555555555ULL
);
140 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
141 0x3333333333333333ULL
);
142 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
143 0x0f0f0f0f0f0f0f0fULL
);
147 target_ulong
helper_popcntw(target_ulong val
)
149 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) &
150 0x5555555555555555ULL
);
151 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) &
152 0x3333333333333333ULL
);
153 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) &
154 0x0f0f0f0f0f0f0f0fULL
);
155 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) &
156 0x00ff00ff00ff00ffULL
);
157 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) &
158 0x0000ffff0000ffffULL
);
162 target_ulong
helper_popcntd(target_ulong val
)
167 target_ulong
helper_popcntb(target_ulong val
)
169 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
170 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
171 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
175 target_ulong
helper_popcntw(target_ulong val
)
177 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
178 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
179 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
180 val
= (val
& 0x00ff00ff) + ((val
>> 8) & 0x00ff00ff);
181 val
= (val
& 0x0000ffff) + ((val
>> 16) & 0x0000ffff);
186 /*****************************************************************************/
187 /* PowerPC 601 specific instructions (POWER bridge) */
188 target_ulong
helper_div(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
)
190 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
192 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
193 (int32_t)arg2
== 0) {
194 env
->spr
[SPR_MQ
] = 0;
197 env
->spr
[SPR_MQ
] = tmp
% arg2
;
198 return tmp
/ (int32_t)arg2
;
202 target_ulong
helper_divo(CPUPPCState
*env
, target_ulong arg1
,
205 uint64_t tmp
= (uint64_t)arg1
<< 32 | env
->spr
[SPR_MQ
];
207 if (((int32_t)tmp
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
208 (int32_t)arg2
== 0) {
209 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
210 env
->spr
[SPR_MQ
] = 0;
213 env
->spr
[SPR_MQ
] = tmp
% arg2
;
214 tmp
/= (int32_t)arg2
;
215 if ((int32_t)tmp
!= tmp
) {
216 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
218 env
->xer
&= ~(1 << XER_OV
);
224 target_ulong
helper_divs(CPUPPCState
*env
, target_ulong arg1
,
227 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
228 (int32_t)arg2
== 0) {
229 env
->spr
[SPR_MQ
] = 0;
232 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
233 return (int32_t)arg1
/ (int32_t)arg2
;
237 target_ulong
helper_divso(CPUPPCState
*env
, target_ulong arg1
,
240 if (((int32_t)arg1
== INT32_MIN
&& (int32_t)arg2
== (int32_t)-1) ||
241 (int32_t)arg2
== 0) {
242 env
->xer
|= (1 << XER_OV
) | (1 << XER_SO
);
243 env
->spr
[SPR_MQ
] = 0;
246 env
->xer
&= ~(1 << XER_OV
);
247 env
->spr
[SPR_MQ
] = (int32_t)arg1
% (int32_t)arg2
;
248 return (int32_t)arg1
/ (int32_t)arg2
;
252 /*****************************************************************************/
253 /* 602 specific instructions */
254 /* mfrom is the most crazy instruction ever seen, imho ! */
255 /* Real implementation uses a ROM table. Do the same */
256 /* Extremely decomposed:
258 * return 256 * log10(10 + 1.0) + 0.5
260 #if !defined(CONFIG_USER_ONLY)
261 target_ulong
helper_602_mfrom(target_ulong arg
)
263 if (likely(arg
< 602)) {
264 #include "mfrom_table.c"
265 return mfrom_ROM_table
[arg
];
272 /*****************************************************************************/
273 /* Altivec extension helpers */
274 #if defined(HOST_WORDS_BIGENDIAN)
282 #if defined(HOST_WORDS_BIGENDIAN)
283 #define VECTOR_FOR_INORDER_I(index, element) \
284 for (index = 0; index < ARRAY_SIZE(r->element); index++)
286 #define VECTOR_FOR_INORDER_I(index, element) \
287 for (index = ARRAY_SIZE(r->element)-1; index >= 0; index--)
290 /* If X is a NaN, store the corresponding QNaN into RESULT. Otherwise,
291 * execute the following block. */
292 #define DO_HANDLE_NAN(result, x) \
293 if (float32_is_any_nan(x)) { \
296 __f.l = __f.l | (1 << 22); /* Set QNaN bit. */ \
300 #define HANDLE_NAN1(result, x) \
301 DO_HANDLE_NAN(result, x)
302 #define HANDLE_NAN2(result, x, y) \
303 DO_HANDLE_NAN(result, x) DO_HANDLE_NAN(result, y)
304 #define HANDLE_NAN3(result, x, y, z) \
305 DO_HANDLE_NAN(result, x) DO_HANDLE_NAN(result, y) DO_HANDLE_NAN(result, z)
307 /* Saturating arithmetic helpers. */
308 #define SATCVT(from, to, from_type, to_type, min, max) \
309 static inline to_type cvt##from##to(from_type x, int *sat) \
313 if (x < (from_type)min) { \
316 } else if (x > (from_type)max) { \
324 #define SATCVTU(from, to, from_type, to_type, min, max) \
325 static inline to_type cvt##from##to(from_type x, int *sat) \
329 if (x > (from_type)max) { \
337 SATCVT(sh
, sb
, int16_t, int8_t, INT8_MIN
, INT8_MAX
)
338 SATCVT(sw
, sh
, int32_t, int16_t, INT16_MIN
, INT16_MAX
)
339 SATCVT(sd
, sw
, int64_t, int32_t, INT32_MIN
, INT32_MAX
)
341 SATCVTU(uh
, ub
, uint16_t, uint8_t, 0, UINT8_MAX
)
342 SATCVTU(uw
, uh
, uint32_t, uint16_t, 0, UINT16_MAX
)
343 SATCVTU(ud
, uw
, uint64_t, uint32_t, 0, UINT32_MAX
)
344 SATCVT(sh
, ub
, int16_t, uint8_t, 0, UINT8_MAX
)
345 SATCVT(sw
, uh
, int32_t, uint16_t, 0, UINT16_MAX
)
346 SATCVT(sd
, uw
, int64_t, uint32_t, 0, UINT32_MAX
)
350 void helper_lvsl(ppc_avr_t
*r
, target_ulong sh
)
352 int i
, j
= (sh
& 0xf);
354 VECTOR_FOR_INORDER_I(i
, u8
) {
359 void helper_lvsr(ppc_avr_t
*r
, target_ulong sh
)
361 int i
, j
= 0x10 - (sh
& 0xf);
363 VECTOR_FOR_INORDER_I(i
, u8
) {
368 void helper_mtvscr(CPUPPCState
*env
, ppc_avr_t
*r
)
370 #if defined(HOST_WORDS_BIGENDIAN)
371 env
->vscr
= r
->u32
[3];
373 env
->vscr
= r
->u32
[0];
375 set_flush_to_zero(vscr_nj
, &env
->vec_status
);
378 void helper_vaddcuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
382 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
383 r
->u32
[i
] = ~a
->u32
[i
] < b
->u32
[i
];
387 #define VARITH_DO(name, op, element) \
388 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
392 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
393 r->element[i] = a->element[i] op b->element[i]; \
396 #define VARITH(suffix, element) \
397 VARITH_DO(add##suffix, +, element) \
398 VARITH_DO(sub##suffix, -, element)
405 #define VARITHFP(suffix, func) \
406 void helper_v##suffix(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
411 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
412 r->f[i] = func(a->f[i], b->f[i], &env->vec_status); \
415 VARITHFP(addfp
, float32_add
)
416 VARITHFP(subfp
, float32_sub
)
417 VARITHFP(minfp
, float32_min
)
418 VARITHFP(maxfp
, float32_max
)
421 #define VARITHSAT_CASE(type, op, cvt, element) \
423 type result = (type)a->element[i] op (type)b->element[i]; \
424 r->element[i] = cvt(result, &sat); \
427 #define VARITHSAT_DO(name, op, optype, cvt, element) \
428 void helper_v##name(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *a, \
434 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
435 switch (sizeof(r->element[0])) { \
437 VARITHSAT_CASE(optype, op, cvt, element); \
440 VARITHSAT_CASE(optype, op, cvt, element); \
443 VARITHSAT_CASE(optype, op, cvt, element); \
448 env->vscr |= (1 << VSCR_SAT); \
451 #define VARITHSAT_SIGNED(suffix, element, optype, cvt) \
452 VARITHSAT_DO(adds##suffix##s, +, optype, cvt, element) \
453 VARITHSAT_DO(subs##suffix##s, -, optype, cvt, element)
454 #define VARITHSAT_UNSIGNED(suffix, element, optype, cvt) \
455 VARITHSAT_DO(addu##suffix##s, +, optype, cvt, element) \
456 VARITHSAT_DO(subu##suffix##s, -, optype, cvt, element)
457 VARITHSAT_SIGNED(b
, s8
, int16_t, cvtshsb
)
458 VARITHSAT_SIGNED(h
, s16
, int32_t, cvtswsh
)
459 VARITHSAT_SIGNED(w
, s32
, int64_t, cvtsdsw
)
460 VARITHSAT_UNSIGNED(b
, u8
, uint16_t, cvtshub
)
461 VARITHSAT_UNSIGNED(h
, u16
, uint32_t, cvtswuh
)
462 VARITHSAT_UNSIGNED(w
, u32
, uint64_t, cvtsduw
)
463 #undef VARITHSAT_CASE
465 #undef VARITHSAT_SIGNED
466 #undef VARITHSAT_UNSIGNED
468 #define VAVG_DO(name, element, etype) \
469 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
473 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
474 etype x = (etype)a->element[i] + (etype)b->element[i] + 1; \
475 r->element[i] = x >> 1; \
479 #define VAVG(type, signed_element, signed_type, unsigned_element, \
481 VAVG_DO(avgs##type, signed_element, signed_type) \
482 VAVG_DO(avgu##type, unsigned_element, unsigned_type)
483 VAVG(b
, s8
, int16_t, u8
, uint16_t)
484 VAVG(h
, s16
, int32_t, u16
, uint32_t)
485 VAVG(w
, s32
, int64_t, u32
, uint64_t)
489 #define VCF(suffix, cvt, element) \
490 void helper_vcf##suffix(CPUPPCState *env, ppc_avr_t *r, \
491 ppc_avr_t *b, uint32_t uim) \
495 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
496 float32 t = cvt(b->element[i], &env->vec_status); \
497 r->f[i] = float32_scalbn(t, -uim, &env->vec_status); \
500 VCF(ux
, uint32_to_float32
, u32
)
501 VCF(sx
, int32_to_float32
, s32
)
504 #define VCMP_DO(suffix, compare, element, record) \
505 void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r, \
506 ppc_avr_t *a, ppc_avr_t *b) \
508 uint32_t ones = (uint32_t)-1; \
509 uint32_t all = ones; \
513 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
514 uint32_t result = (a->element[i] compare b->element[i] ? \
516 switch (sizeof(a->element[0])) { \
518 r->u32[i] = result; \
521 r->u16[i] = result; \
531 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
534 #define VCMP(suffix, compare, element) \
535 VCMP_DO(suffix, compare, element, 0) \
536 VCMP_DO(suffix##_dot, compare, element, 1)
549 #define VCMPFP_DO(suffix, compare, order, record) \
550 void helper_vcmp##suffix(CPUPPCState *env, ppc_avr_t *r, \
551 ppc_avr_t *a, ppc_avr_t *b) \
553 uint32_t ones = (uint32_t)-1; \
554 uint32_t all = ones; \
558 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
560 int rel = float32_compare_quiet(a->f[i], b->f[i], \
562 if (rel == float_relation_unordered) { \
564 } else if (rel compare order) { \
569 r->u32[i] = result; \
574 env->crf[6] = ((all != 0) << 3) | ((none == 0) << 1); \
577 #define VCMPFP(suffix, compare, order) \
578 VCMPFP_DO(suffix, compare, order, 0) \
579 VCMPFP_DO(suffix##_dot, compare, order, 1)
580 VCMPFP(eqfp
, ==, float_relation_equal
)
581 VCMPFP(gefp
, !=, float_relation_less
)
582 VCMPFP(gtfp
, ==, float_relation_greater
)
586 static inline void vcmpbfp_internal(CPUPPCState
*env
, ppc_avr_t
*r
,
587 ppc_avr_t
*a
, ppc_avr_t
*b
, int record
)
592 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
593 int le_rel
= float32_compare_quiet(a
->f
[i
], b
->f
[i
], &env
->vec_status
);
594 if (le_rel
== float_relation_unordered
) {
595 r
->u32
[i
] = 0xc0000000;
596 /* ALL_IN does not need to be updated here. */
598 float32 bneg
= float32_chs(b
->f
[i
]);
599 int ge_rel
= float32_compare_quiet(a
->f
[i
], bneg
, &env
->vec_status
);
600 int le
= le_rel
!= float_relation_greater
;
601 int ge
= ge_rel
!= float_relation_less
;
603 r
->u32
[i
] = ((!le
) << 31) | ((!ge
) << 30);
604 all_in
|= (!le
| !ge
);
608 env
->crf
[6] = (all_in
== 0) << 1;
612 void helper_vcmpbfp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
614 vcmpbfp_internal(env
, r
, a
, b
, 0);
617 void helper_vcmpbfp_dot(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
620 vcmpbfp_internal(env
, r
, a
, b
, 1);
623 #define VCT(suffix, satcvt, element) \
624 void helper_vct##suffix(CPUPPCState *env, ppc_avr_t *r, \
625 ppc_avr_t *b, uint32_t uim) \
629 float_status s = env->vec_status; \
631 set_float_rounding_mode(float_round_to_zero, &s); \
632 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
633 if (float32_is_any_nan(b->f[i])) { \
636 float64 t = float32_to_float64(b->f[i], &s); \
639 t = float64_scalbn(t, uim, &s); \
640 j = float64_to_int64(t, &s); \
641 r->element[i] = satcvt(j, &sat); \
645 env->vscr |= (1 << VSCR_SAT); \
648 VCT(uxs
, cvtsduw
, u32
)
649 VCT(sxs
, cvtsdsw
, s32
)
652 void helper_vmaddfp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
657 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
658 HANDLE_NAN3(r
->f
[i
], a
->f
[i
], b
->f
[i
], c
->f
[i
]) {
659 /* Need to do the computation in higher precision and round
660 * once at the end. */
661 float64 af
, bf
, cf
, t
;
663 af
= float32_to_float64(a
->f
[i
], &env
->vec_status
);
664 bf
= float32_to_float64(b
->f
[i
], &env
->vec_status
);
665 cf
= float32_to_float64(c
->f
[i
], &env
->vec_status
);
666 t
= float64_mul(af
, cf
, &env
->vec_status
);
667 t
= float64_add(t
, bf
, &env
->vec_status
);
668 r
->f
[i
] = float64_to_float32(t
, &env
->vec_status
);
673 void helper_vmhaddshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
674 ppc_avr_t
*b
, ppc_avr_t
*c
)
679 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
680 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
681 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
683 r
->s16
[i
] = cvtswsh(t
, &sat
);
687 env
->vscr
|= (1 << VSCR_SAT
);
691 void helper_vmhraddshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
692 ppc_avr_t
*b
, ppc_avr_t
*c
)
697 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
698 int32_t prod
= a
->s16
[i
] * b
->s16
[i
] + 0x00004000;
699 int32_t t
= (int32_t)c
->s16
[i
] + (prod
>> 15);
700 r
->s16
[i
] = cvtswsh(t
, &sat
);
704 env
->vscr
|= (1 << VSCR_SAT
);
708 #define VMINMAX_DO(name, compare, element) \
709 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
713 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
714 if (a->element[i] compare b->element[i]) { \
715 r->element[i] = b->element[i]; \
717 r->element[i] = a->element[i]; \
721 #define VMINMAX(suffix, element) \
722 VMINMAX_DO(min##suffix, >, element) \
723 VMINMAX_DO(max##suffix, <, element)
733 void helper_vmladduhm(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, ppc_avr_t
*c
)
737 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
738 int32_t prod
= a
->s16
[i
] * b
->s16
[i
];
739 r
->s16
[i
] = (int16_t) (prod
+ c
->s16
[i
]);
743 #define VMRG_DO(name, element, highp) \
744 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
748 size_t n_elems = ARRAY_SIZE(r->element); \
750 for (i = 0; i < n_elems / 2; i++) { \
752 result.element[i*2+HI_IDX] = a->element[i]; \
753 result.element[i*2+LO_IDX] = b->element[i]; \
755 result.element[n_elems - i * 2 - (1 + HI_IDX)] = \
756 b->element[n_elems - i - 1]; \
757 result.element[n_elems - i * 2 - (1 + LO_IDX)] = \
758 a->element[n_elems - i - 1]; \
763 #if defined(HOST_WORDS_BIGENDIAN)
770 #define VMRG(suffix, element) \
771 VMRG_DO(mrgl##suffix, element, MRGHI) \
772 VMRG_DO(mrgh##suffix, element, MRGLO)
781 void helper_vmsummbm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
782 ppc_avr_t
*b
, ppc_avr_t
*c
)
787 for (i
= 0; i
< ARRAY_SIZE(r
->s8
); i
++) {
788 prod
[i
] = (int32_t)a
->s8
[i
] * b
->u8
[i
];
791 VECTOR_FOR_INORDER_I(i
, s32
) {
792 r
->s32
[i
] = c
->s32
[i
] + prod
[4 * i
] + prod
[4 * i
+ 1] +
793 prod
[4 * i
+ 2] + prod
[4 * i
+ 3];
797 void helper_vmsumshm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
798 ppc_avr_t
*b
, ppc_avr_t
*c
)
803 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
804 prod
[i
] = a
->s16
[i
] * b
->s16
[i
];
807 VECTOR_FOR_INORDER_I(i
, s32
) {
808 r
->s32
[i
] = c
->s32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
812 void helper_vmsumshs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
813 ppc_avr_t
*b
, ppc_avr_t
*c
)
819 for (i
= 0; i
< ARRAY_SIZE(r
->s16
); i
++) {
820 prod
[i
] = (int32_t)a
->s16
[i
] * b
->s16
[i
];
823 VECTOR_FOR_INORDER_I(i
, s32
) {
824 int64_t t
= (int64_t)c
->s32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
826 r
->u32
[i
] = cvtsdsw(t
, &sat
);
830 env
->vscr
|= (1 << VSCR_SAT
);
834 void helper_vmsumubm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
835 ppc_avr_t
*b
, ppc_avr_t
*c
)
840 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
841 prod
[i
] = a
->u8
[i
] * b
->u8
[i
];
844 VECTOR_FOR_INORDER_I(i
, u32
) {
845 r
->u32
[i
] = c
->u32
[i
] + prod
[4 * i
] + prod
[4 * i
+ 1] +
846 prod
[4 * i
+ 2] + prod
[4 * i
+ 3];
850 void helper_vmsumuhm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
851 ppc_avr_t
*b
, ppc_avr_t
*c
)
856 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
857 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
860 VECTOR_FOR_INORDER_I(i
, u32
) {
861 r
->u32
[i
] = c
->u32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
865 void helper_vmsumuhs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
866 ppc_avr_t
*b
, ppc_avr_t
*c
)
872 for (i
= 0; i
< ARRAY_SIZE(r
->u16
); i
++) {
873 prod
[i
] = a
->u16
[i
] * b
->u16
[i
];
876 VECTOR_FOR_INORDER_I(i
, s32
) {
877 uint64_t t
= (uint64_t)c
->u32
[i
] + prod
[2 * i
] + prod
[2 * i
+ 1];
879 r
->u32
[i
] = cvtuduw(t
, &sat
);
883 env
->vscr
|= (1 << VSCR_SAT
);
887 #define VMUL_DO(name, mul_element, prod_element, evenp) \
888 void helper_v##name(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
892 VECTOR_FOR_INORDER_I(i, prod_element) { \
894 r->prod_element[i] = a->mul_element[i * 2 + HI_IDX] * \
895 b->mul_element[i * 2 + HI_IDX]; \
897 r->prod_element[i] = a->mul_element[i * 2 + LO_IDX] * \
898 b->mul_element[i * 2 + LO_IDX]; \
902 #define VMUL(suffix, mul_element, prod_element) \
903 VMUL_DO(mule##suffix, mul_element, prod_element, 1) \
904 VMUL_DO(mulo##suffix, mul_element, prod_element, 0)
912 void helper_vnmsubfp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
,
913 ppc_avr_t
*b
, ppc_avr_t
*c
)
917 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
918 HANDLE_NAN3(r
->f
[i
], a
->f
[i
], b
->f
[i
], c
->f
[i
]) {
919 /* Need to do the computation is higher precision and round
920 * once at the end. */
921 float64 af
, bf
, cf
, t
;
923 af
= float32_to_float64(a
->f
[i
], &env
->vec_status
);
924 bf
= float32_to_float64(b
->f
[i
], &env
->vec_status
);
925 cf
= float32_to_float64(c
->f
[i
], &env
->vec_status
);
926 t
= float64_mul(af
, cf
, &env
->vec_status
);
927 t
= float64_sub(t
, bf
, &env
->vec_status
);
929 r
->f
[i
] = float64_to_float32(t
, &env
->vec_status
);
934 void helper_vperm(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
940 VECTOR_FOR_INORDER_I(i
, u8
) {
941 int s
= c
->u8
[i
] & 0x1f;
942 #if defined(HOST_WORDS_BIGENDIAN)
945 int index
= 15 - (s
& 0xf);
949 result
.u8
[i
] = b
->u8
[index
];
951 result
.u8
[i
] = a
->u8
[index
];
957 #if defined(HOST_WORDS_BIGENDIAN)
962 void helper_vpkpx(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
966 #if defined(HOST_WORDS_BIGENDIAN)
967 const ppc_avr_t
*x
[2] = { a
, b
};
969 const ppc_avr_t
*x
[2] = { b
, a
};
972 VECTOR_FOR_INORDER_I(i
, u64
) {
973 VECTOR_FOR_INORDER_I(j
, u32
) {
974 uint32_t e
= x
[i
]->u32
[j
];
976 result
.u16
[4*i
+j
] = (((e
>> 9) & 0xfc00) |
984 #define VPK(suffix, from, to, cvt, dosat) \
985 void helper_vpk##suffix(CPUPPCState *env, ppc_avr_t *r, \
986 ppc_avr_t *a, ppc_avr_t *b) \
991 ppc_avr_t *a0 = PKBIG ? a : b; \
992 ppc_avr_t *a1 = PKBIG ? b : a; \
994 VECTOR_FOR_INORDER_I(i, from) { \
995 result.to[i] = cvt(a0->from[i], &sat); \
996 result.to[i+ARRAY_SIZE(r->from)] = cvt(a1->from[i], &sat); \
999 if (dosat && sat) { \
1000 env->vscr |= (1 << VSCR_SAT); \
1004 VPK(shss
, s16
, s8
, cvtshsb
, 1)
1005 VPK(shus
, s16
, u8
, cvtshub
, 1)
1006 VPK(swss
, s32
, s16
, cvtswsh
, 1)
1007 VPK(swus
, s32
, u16
, cvtswuh
, 1)
1008 VPK(uhus
, u16
, u8
, cvtuhub
, 1)
1009 VPK(uwus
, u32
, u16
, cvtuwuh
, 1)
1010 VPK(uhum
, u16
, u8
, I
, 0)
1011 VPK(uwum
, u32
, u16
, I
, 0)
1016 void helper_vrefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1020 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
1021 r
->f
[i
] = float32_div(float32_one
, b
->f
[i
], &env
->vec_status
);
1025 #define VRFI(suffix, rounding) \
1026 void helper_vrfi##suffix(CPUPPCState *env, ppc_avr_t *r, \
1030 float_status s = env->vec_status; \
1032 set_float_rounding_mode(rounding, &s); \
1033 for (i = 0; i < ARRAY_SIZE(r->f); i++) { \
1034 r->f[i] = float32_round_to_int (b->f[i], &s); \
1037 VRFI(n
, float_round_nearest_even
)
1038 VRFI(m
, float_round_down
)
1039 VRFI(p
, float_round_up
)
1040 VRFI(z
, float_round_to_zero
)
1043 #define VROTATE(suffix, element) \
1044 void helper_vrl##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1048 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1049 unsigned int mask = ((1 << \
1050 (3 + (sizeof(a->element[0]) >> 1))) \
1052 unsigned int shift = b->element[i] & mask; \
1053 r->element[i] = (a->element[i] << shift) | \
1054 (a->element[i] >> (sizeof(a->element[0]) * 8 - shift)); \
1062 void helper_vrsqrtefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1066 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
1067 float32 t
= float32_sqrt(b
->f
[i
], &env
->vec_status
);
1069 r
->f
[i
] = float32_div(float32_one
, t
, &env
->vec_status
);
1073 void helper_vsel(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
,
1076 r
->u64
[0] = (a
->u64
[0] & ~c
->u64
[0]) | (b
->u64
[0] & c
->u64
[0]);
1077 r
->u64
[1] = (a
->u64
[1] & ~c
->u64
[1]) | (b
->u64
[1] & c
->u64
[1]);
1080 void helper_vexptefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1084 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
1085 r
->f
[i
] = float32_exp2(b
->f
[i
], &env
->vec_status
);
1089 void helper_vlogefp(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*b
)
1093 for (i
= 0; i
< ARRAY_SIZE(r
->f
); i
++) {
1094 r
->f
[i
] = float32_log2(b
->f
[i
], &env
->vec_status
);
1098 #if defined(HOST_WORDS_BIGENDIAN)
1105 /* The specification says that the results are undefined if all of the
1106 * shift counts are not identical. We check to make sure that they are
1107 * to conform to what real hardware appears to do. */
1108 #define VSHIFT(suffix, leftp) \
1109 void helper_vs##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1111 int shift = b->u8[LO_IDX*15] & 0x7; \
1115 for (i = 0; i < ARRAY_SIZE(r->u8); i++) { \
1116 doit = doit && ((b->u8[i] & 0x7) == shift); \
1121 } else if (leftp) { \
1122 uint64_t carry = a->u64[LO_IDX] >> (64 - shift); \
1124 r->u64[HI_IDX] = (a->u64[HI_IDX] << shift) | carry; \
1125 r->u64[LO_IDX] = a->u64[LO_IDX] << shift; \
1127 uint64_t carry = a->u64[HI_IDX] << (64 - shift); \
1129 r->u64[LO_IDX] = (a->u64[LO_IDX] >> shift) | carry; \
1130 r->u64[HI_IDX] = a->u64[HI_IDX] >> shift; \
1140 #define VSL(suffix, element) \
1141 void helper_vsl##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1145 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1146 unsigned int mask = ((1 << \
1147 (3 + (sizeof(a->element[0]) >> 1))) \
1149 unsigned int shift = b->element[i] & mask; \
1151 r->element[i] = a->element[i] << shift; \
1159 void helper_vsldoi(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
, uint32_t shift
)
1161 int sh
= shift
& 0xf;
1165 #if defined(HOST_WORDS_BIGENDIAN)
1166 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1169 result
.u8
[i
] = b
->u8
[index
- 0x10];
1171 result
.u8
[i
] = a
->u8
[index
];
1175 for (i
= 0; i
< ARRAY_SIZE(r
->u8
); i
++) {
1176 int index
= (16 - sh
) + i
;
1178 result
.u8
[i
] = a
->u8
[index
- 0x10];
1180 result
.u8
[i
] = b
->u8
[index
];
1187 void helper_vslo(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1189 int sh
= (b
->u8
[LO_IDX
*0xf] >> 3) & 0xf;
1191 #if defined(HOST_WORDS_BIGENDIAN)
1192 memmove(&r
->u8
[0], &a
->u8
[sh
], 16 - sh
);
1193 memset(&r
->u8
[16-sh
], 0, sh
);
1195 memmove(&r
->u8
[sh
], &a
->u8
[0], 16 - sh
);
1196 memset(&r
->u8
[0], 0, sh
);
1200 /* Experimental testing shows that hardware masks the immediate. */
1201 #define _SPLAT_MASKED(element) (splat & (ARRAY_SIZE(r->element) - 1))
1202 #if defined(HOST_WORDS_BIGENDIAN)
1203 #define SPLAT_ELEMENT(element) _SPLAT_MASKED(element)
1205 #define SPLAT_ELEMENT(element) \
1206 (ARRAY_SIZE(r->element) - 1 - _SPLAT_MASKED(element))
1208 #define VSPLT(suffix, element) \
1209 void helper_vsplt##suffix(ppc_avr_t *r, ppc_avr_t *b, uint32_t splat) \
1211 uint32_t s = b->element[SPLAT_ELEMENT(element)]; \
1214 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1215 r->element[i] = s; \
1222 #undef SPLAT_ELEMENT
1223 #undef _SPLAT_MASKED
1225 #define VSPLTI(suffix, element, splat_type) \
1226 void helper_vspltis##suffix(ppc_avr_t *r, uint32_t splat) \
1228 splat_type x = (int8_t)(splat << 3) >> 3; \
1231 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1232 r->element[i] = x; \
1235 VSPLTI(b
, s8
, int8_t)
1236 VSPLTI(h
, s16
, int16_t)
1237 VSPLTI(w
, s32
, int32_t)
1240 #define VSR(suffix, element) \
1241 void helper_vsr##suffix(ppc_avr_t *r, ppc_avr_t *a, ppc_avr_t *b) \
1245 for (i = 0; i < ARRAY_SIZE(r->element); i++) { \
1246 unsigned int mask = ((1 << \
1247 (3 + (sizeof(a->element[0]) >> 1))) \
1249 unsigned int shift = b->element[i] & mask; \
1251 r->element[i] = a->element[i] >> shift; \
1262 void helper_vsro(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1264 int sh
= (b
->u8
[LO_IDX
* 0xf] >> 3) & 0xf;
1266 #if defined(HOST_WORDS_BIGENDIAN)
1267 memmove(&r
->u8
[sh
], &a
->u8
[0], 16 - sh
);
1268 memset(&r
->u8
[0], 0, sh
);
1270 memmove(&r
->u8
[0], &a
->u8
[sh
], 16 - sh
);
1271 memset(&r
->u8
[16 - sh
], 0, sh
);
1275 void helper_vsubcuw(ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1279 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
1280 r
->u32
[i
] = a
->u32
[i
] >= b
->u32
[i
];
1284 void helper_vsumsws(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1291 #if defined(HOST_WORDS_BIGENDIAN)
1292 upper
= ARRAY_SIZE(r
->s32
)-1;
1296 t
= (int64_t)b
->s32
[upper
];
1297 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1301 result
.s32
[upper
] = cvtsdsw(t
, &sat
);
1305 env
->vscr
|= (1 << VSCR_SAT
);
1309 void helper_vsum2sws(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1315 #if defined(HOST_WORDS_BIGENDIAN)
1320 for (i
= 0; i
< ARRAY_SIZE(r
->u64
); i
++) {
1321 int64_t t
= (int64_t)b
->s32
[upper
+ i
* 2];
1324 for (j
= 0; j
< ARRAY_SIZE(r
->u64
); j
++) {
1325 t
+= a
->s32
[2 * i
+ j
];
1327 result
.s32
[upper
+ i
* 2] = cvtsdsw(t
, &sat
);
1332 env
->vscr
|= (1 << VSCR_SAT
);
1336 void helper_vsum4sbs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1341 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1342 int64_t t
= (int64_t)b
->s32
[i
];
1344 for (j
= 0; j
< ARRAY_SIZE(r
->s32
); j
++) {
1345 t
+= a
->s8
[4 * i
+ j
];
1347 r
->s32
[i
] = cvtsdsw(t
, &sat
);
1351 env
->vscr
|= (1 << VSCR_SAT
);
1355 void helper_vsum4shs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1360 for (i
= 0; i
< ARRAY_SIZE(r
->s32
); i
++) {
1361 int64_t t
= (int64_t)b
->s32
[i
];
1363 t
+= a
->s16
[2 * i
] + a
->s16
[2 * i
+ 1];
1364 r
->s32
[i
] = cvtsdsw(t
, &sat
);
1368 env
->vscr
|= (1 << VSCR_SAT
);
1372 void helper_vsum4ubs(CPUPPCState
*env
, ppc_avr_t
*r
, ppc_avr_t
*a
, ppc_avr_t
*b
)
1377 for (i
= 0; i
< ARRAY_SIZE(r
->u32
); i
++) {
1378 uint64_t t
= (uint64_t)b
->u32
[i
];
1380 for (j
= 0; j
< ARRAY_SIZE(r
->u32
); j
++) {
1381 t
+= a
->u8
[4 * i
+ j
];
1383 r
->u32
[i
] = cvtuduw(t
, &sat
);
1387 env
->vscr
|= (1 << VSCR_SAT
);
1391 #if defined(HOST_WORDS_BIGENDIAN)
1398 #define VUPKPX(suffix, hi) \
1399 void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b) \
1404 for (i = 0; i < ARRAY_SIZE(r->u32); i++) { \
1405 uint16_t e = b->u16[hi ? i : i+4]; \
1406 uint8_t a = (e >> 15) ? 0xff : 0; \
1407 uint8_t r = (e >> 10) & 0x1f; \
1408 uint8_t g = (e >> 5) & 0x1f; \
1409 uint8_t b = e & 0x1f; \
1411 result.u32[i] = (a << 24) | (r << 16) | (g << 8) | b; \
1419 #define VUPK(suffix, unpacked, packee, hi) \
1420 void helper_vupk##suffix(ppc_avr_t *r, ppc_avr_t *b) \
1426 for (i = 0; i < ARRAY_SIZE(r->unpacked); i++) { \
1427 result.unpacked[i] = b->packee[i]; \
1430 for (i = ARRAY_SIZE(r->unpacked); i < ARRAY_SIZE(r->packee); \
1432 result.unpacked[i - ARRAY_SIZE(r->unpacked)] = b->packee[i]; \
1437 VUPK(hsb
, s16
, s8
, UPKHI
)
1438 VUPK(hsh
, s32
, s16
, UPKHI
)
1439 VUPK(lsb
, s16
, s8
, UPKLO
)
1440 VUPK(lsh
, s32
, s16
, UPKLO
)
1445 #undef DO_HANDLE_NAN
1449 #undef VECTOR_FOR_INORDER_I
1453 /*****************************************************************************/
1454 /* SPE extension helpers */
1455 /* Use a table to make this quicker */
1456 static const uint8_t hbrev
[16] = {
1457 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1458 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1461 static inline uint8_t byte_reverse(uint8_t val
)
1463 return hbrev
[val
>> 4] | (hbrev
[val
& 0xF] << 4);
1466 static inline uint32_t word_reverse(uint32_t val
)
1468 return byte_reverse(val
>> 24) | (byte_reverse(val
>> 16) << 8) |
1469 (byte_reverse(val
>> 8) << 16) | (byte_reverse(val
) << 24);
1472 #define MASKBITS 16 /* Random value - to be fixed (implementation dependent) */
1473 target_ulong
helper_brinc(target_ulong arg1
, target_ulong arg2
)
1475 uint32_t a
, b
, d
, mask
;
1477 mask
= UINT32_MAX
>> (32 - MASKBITS
);
1480 d
= word_reverse(1 + word_reverse(a
| ~b
));
1481 return (arg1
& ~mask
) | (d
& b
);
1484 uint32_t helper_cntlsw32(uint32_t val
)
1486 if (val
& 0x80000000) {
1493 uint32_t helper_cntlzw32(uint32_t val
)
1499 target_ulong
helper_dlmzb(CPUPPCState
*env
, target_ulong high
,
1500 target_ulong low
, uint32_t update_Rc
)
1506 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1507 if ((high
& mask
) == 0) {
1515 for (mask
= 0xFF000000; mask
!= 0; mask
= mask
>> 8) {
1516 if ((low
& mask
) == 0) {
1528 env
->xer
= (env
->xer
& ~0x7F) | i
;
1530 env
->crf
[0] |= xer_so
;