2 * ARM NEON vector operations.
4 * Copyright (c) 2007, 2008 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GNU GPL v2.
9 #include "qemu/osdep.h"
12 #include "exec/exec-all.h"
13 #include "exec/helper-proto.h"
15 #define SIGNBIT (uint32_t)0x80000000
16 #define SIGNBIT64 ((uint64_t)1 << 63)
18 #define SET_QC() env->vfp.xregs[ARM_VFP_FPSCR] |= CPSR_Q
20 #define NEON_TYPE1(name, type) \
25 #ifdef HOST_WORDS_BIGENDIAN
26 #define NEON_TYPE2(name, type) \
32 #define NEON_TYPE4(name, type) \
41 #define NEON_TYPE2(name, type) \
47 #define NEON_TYPE4(name, type) \
57 NEON_TYPE4(s8
, int8_t)
58 NEON_TYPE4(u8
, uint8_t)
59 NEON_TYPE2(s16
, int16_t)
60 NEON_TYPE2(u16
, uint16_t)
61 NEON_TYPE1(s32
, int32_t)
62 NEON_TYPE1(u32
, uint32_t)
67 /* Copy from a uint32_t to a vector structure type. */
68 #define NEON_UNPACK(vtype, dest, val) do { \
77 /* Copy from a vector structure type to a uint32_t. */
78 #define NEON_PACK(vtype, dest, val) do { \
88 NEON_FN(vdest.v1, vsrc1.v1, vsrc2.v1);
90 NEON_FN(vdest.v1, vsrc1.v1, vsrc2.v1); \
91 NEON_FN(vdest.v2, vsrc1.v2, vsrc2.v2);
93 NEON_FN(vdest.v1, vsrc1.v1, vsrc2.v1); \
94 NEON_FN(vdest.v2, vsrc1.v2, vsrc2.v2); \
95 NEON_FN(vdest.v3, vsrc1.v3, vsrc2.v3); \
96 NEON_FN(vdest.v4, vsrc1.v4, vsrc2.v4);
98 #define NEON_VOP_BODY(vtype, n) \
104 NEON_UNPACK(vtype, vsrc1, arg1); \
105 NEON_UNPACK(vtype, vsrc2, arg2); \
107 NEON_PACK(vtype, res, vdest); \
111 #define NEON_VOP(name, vtype, n) \
112 uint32_t HELPER(glue(neon_,name))(uint32_t arg1, uint32_t arg2) \
113 NEON_VOP_BODY(vtype, n)
115 #define NEON_VOP_ENV(name, vtype, n) \
116 uint32_t HELPER(glue(neon_,name))(CPUARMState *env, uint32_t arg1, uint32_t arg2) \
117 NEON_VOP_BODY(vtype, n)
119 /* Pairwise operations. */
120 /* For 32-bit elements each segment only contains a single element, so
121 the elementwise and pairwise operations are the same. */
123 NEON_FN(vdest.v1, vsrc1.v1, vsrc1.v2); \
124 NEON_FN(vdest.v2, vsrc2.v1, vsrc2.v2);
126 NEON_FN(vdest.v1, vsrc1.v1, vsrc1.v2); \
127 NEON_FN(vdest.v2, vsrc1.v3, vsrc1.v4); \
128 NEON_FN(vdest.v3, vsrc2.v1, vsrc2.v2); \
129 NEON_FN(vdest.v4, vsrc2.v3, vsrc2.v4); \
131 #define NEON_POP(name, vtype, n) \
132 uint32_t HELPER(glue(neon_,name))(uint32_t arg1, uint32_t arg2) \
138 NEON_UNPACK(vtype, vsrc1, arg1); \
139 NEON_UNPACK(vtype, vsrc2, arg2); \
141 NEON_PACK(vtype, res, vdest); \
145 /* Unary operators. */
146 #define NEON_VOP1(name, vtype, n) \
147 uint32_t HELPER(glue(neon_,name))(uint32_t arg) \
151 NEON_UNPACK(vtype, vsrc1, arg); \
153 NEON_PACK(vtype, arg, vdest); \
158 #define NEON_USAT(dest, src1, src2, type) do { \
159 uint32_t tmp = (uint32_t)src1 + (uint32_t)src2; \
160 if (tmp != (type)tmp) { \
166 #define NEON_FN(dest, src1, src2) NEON_USAT(dest, src1, src2, uint8_t)
167 NEON_VOP_ENV(qadd_u8
, neon_u8
, 4)
169 #define NEON_FN(dest, src1, src2) NEON_USAT(dest, src1, src2, uint16_t)
170 NEON_VOP_ENV(qadd_u16
, neon_u16
, 2)
174 uint32_t HELPER(neon_qadd_u32
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
176 uint32_t res
= a
+ b
;
184 uint64_t HELPER(neon_qadd_u64
)(CPUARMState
*env
, uint64_t src1
, uint64_t src2
)
196 #define NEON_SSAT(dest, src1, src2, type) do { \
197 int32_t tmp = (uint32_t)src1 + (uint32_t)src2; \
198 if (tmp != (type)tmp) { \
201 tmp = (1 << (sizeof(type) * 8 - 1)) - 1; \
203 tmp = 1 << (sizeof(type) * 8 - 1); \
208 #define NEON_FN(dest, src1, src2) NEON_SSAT(dest, src1, src2, int8_t)
209 NEON_VOP_ENV(qadd_s8
, neon_s8
, 4)
211 #define NEON_FN(dest, src1, src2) NEON_SSAT(dest, src1, src2, int16_t)
212 NEON_VOP_ENV(qadd_s16
, neon_s16
, 2)
216 uint32_t HELPER(neon_qadd_s32
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
218 uint32_t res
= a
+ b
;
219 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
)) {
221 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
226 uint64_t HELPER(neon_qadd_s64
)(CPUARMState
*env
, uint64_t src1
, uint64_t src2
)
231 if (((res
^ src1
) & SIGNBIT64
) && !((src1
^ src2
) & SIGNBIT64
)) {
233 res
= ((int64_t)src1
>> 63) ^ ~SIGNBIT64
;
238 /* Unsigned saturating accumulate of signed value
240 * Op1/Rn is treated as signed
241 * Op2/Rd is treated as unsigned
243 * Explicit casting is used to ensure the correct sign extension of
244 * inputs. The result is treated as a unsigned value and saturated as such.
246 * We use a macro for the 8/16 bit cases which expects signed integers of va,
247 * vb, and vr for interim calculation and an unsigned 32 bit result value r.
250 #define USATACC(bits, shift) \
252 va = sextract32(a, shift, bits); \
253 vb = extract32(b, shift, bits); \
255 if (vr > UINT##bits##_MAX) { \
257 vr = UINT##bits##_MAX; \
258 } else if (vr < 0) { \
262 r = deposit32(r, shift, bits, vr); \
265 uint32_t HELPER(neon_uqadd_s8
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
277 uint32_t HELPER(neon_uqadd_s16
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
289 uint32_t HELPER(neon_uqadd_s32
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
291 int64_t va
= (int32_t)a
;
292 int64_t vb
= (uint32_t)b
;
293 int64_t vr
= va
+ vb
;
294 if (vr
> UINT32_MAX
) {
304 uint64_t HELPER(neon_uqadd_s64
)(CPUARMState
*env
, uint64_t a
, uint64_t b
)
308 /* We only need to look at the pattern of SIGN bits to detect
311 if (~a
& b
& ~res
& SIGNBIT64
) {
314 } else if (a
& ~b
& res
& SIGNBIT64
) {
321 /* Signed saturating accumulate of unsigned value
323 * Op1/Rn is treated as unsigned
324 * Op2/Rd is treated as signed
326 * The result is treated as a signed value and saturated as such
328 * We use a macro for the 8/16 bit cases which expects signed integers of va,
329 * vb, and vr for interim calculation and an unsigned 32 bit result value r.
332 #define SSATACC(bits, shift) \
334 va = extract32(a, shift, bits); \
335 vb = sextract32(b, shift, bits); \
337 if (vr > INT##bits##_MAX) { \
339 vr = INT##bits##_MAX; \
340 } else if (vr < INT##bits##_MIN) { \
342 vr = INT##bits##_MIN; \
344 r = deposit32(r, shift, bits, vr); \
347 uint32_t HELPER(neon_sqadd_u8
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
359 uint32_t HELPER(neon_sqadd_u16
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
372 uint32_t HELPER(neon_sqadd_u32
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
375 int64_t op1
= (uint32_t)a
;
376 int64_t op2
= (int32_t)b
;
378 if (res
> INT32_MAX
) {
381 } else if (res
< INT32_MIN
) {
388 uint64_t HELPER(neon_sqadd_u64
)(CPUARMState
*env
, uint64_t a
, uint64_t b
)
392 /* We only need to look at the pattern of SIGN bits to detect an overflow */
395 | (a
& ~b
)) & SIGNBIT64
) {
403 #define NEON_USAT(dest, src1, src2, type) do { \
404 uint32_t tmp = (uint32_t)src1 - (uint32_t)src2; \
405 if (tmp != (type)tmp) { \
411 #define NEON_FN(dest, src1, src2) NEON_USAT(dest, src1, src2, uint8_t)
412 NEON_VOP_ENV(qsub_u8
, neon_u8
, 4)
414 #define NEON_FN(dest, src1, src2) NEON_USAT(dest, src1, src2, uint16_t)
415 NEON_VOP_ENV(qsub_u16
, neon_u16
, 2)
419 uint32_t HELPER(neon_qsub_u32
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
421 uint32_t res
= a
- b
;
429 uint64_t HELPER(neon_qsub_u64
)(CPUARMState
*env
, uint64_t src1
, uint64_t src2
)
442 #define NEON_SSAT(dest, src1, src2, type) do { \
443 int32_t tmp = (uint32_t)src1 - (uint32_t)src2; \
444 if (tmp != (type)tmp) { \
447 tmp = (1 << (sizeof(type) * 8 - 1)) - 1; \
449 tmp = 1 << (sizeof(type) * 8 - 1); \
454 #define NEON_FN(dest, src1, src2) NEON_SSAT(dest, src1, src2, int8_t)
455 NEON_VOP_ENV(qsub_s8
, neon_s8
, 4)
457 #define NEON_FN(dest, src1, src2) NEON_SSAT(dest, src1, src2, int16_t)
458 NEON_VOP_ENV(qsub_s16
, neon_s16
, 2)
462 uint32_t HELPER(neon_qsub_s32
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
464 uint32_t res
= a
- b
;
465 if (((res
^ a
) & SIGNBIT
) && ((a
^ b
) & SIGNBIT
)) {
467 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
472 uint64_t HELPER(neon_qsub_s64
)(CPUARMState
*env
, uint64_t src1
, uint64_t src2
)
477 if (((res
^ src1
) & SIGNBIT64
) && ((src1
^ src2
) & SIGNBIT64
)) {
479 res
= ((int64_t)src1
>> 63) ^ ~SIGNBIT64
;
484 #define NEON_FN(dest, src1, src2) dest = (src1 + src2) >> 1
485 NEON_VOP(hadd_s8
, neon_s8
, 4)
486 NEON_VOP(hadd_u8
, neon_u8
, 4)
487 NEON_VOP(hadd_s16
, neon_s16
, 2)
488 NEON_VOP(hadd_u16
, neon_u16
, 2)
491 int32_t HELPER(neon_hadd_s32
)(int32_t src1
, int32_t src2
)
495 dest
= (src1
>> 1) + (src2
>> 1);
501 uint32_t HELPER(neon_hadd_u32
)(uint32_t src1
, uint32_t src2
)
505 dest
= (src1
>> 1) + (src2
>> 1);
511 #define NEON_FN(dest, src1, src2) dest = (src1 + src2 + 1) >> 1
512 NEON_VOP(rhadd_s8
, neon_s8
, 4)
513 NEON_VOP(rhadd_u8
, neon_u8
, 4)
514 NEON_VOP(rhadd_s16
, neon_s16
, 2)
515 NEON_VOP(rhadd_u16
, neon_u16
, 2)
518 int32_t HELPER(neon_rhadd_s32
)(int32_t src1
, int32_t src2
)
522 dest
= (src1
>> 1) + (src2
>> 1);
523 if ((src1
| src2
) & 1)
528 uint32_t HELPER(neon_rhadd_u32
)(uint32_t src1
, uint32_t src2
)
532 dest
= (src1
>> 1) + (src2
>> 1);
533 if ((src1
| src2
) & 1)
538 #define NEON_FN(dest, src1, src2) dest = (src1 - src2) >> 1
539 NEON_VOP(hsub_s8
, neon_s8
, 4)
540 NEON_VOP(hsub_u8
, neon_u8
, 4)
541 NEON_VOP(hsub_s16
, neon_s16
, 2)
542 NEON_VOP(hsub_u16
, neon_u16
, 2)
545 int32_t HELPER(neon_hsub_s32
)(int32_t src1
, int32_t src2
)
549 dest
= (src1
>> 1) - (src2
>> 1);
550 if ((~src1
) & src2
& 1)
555 uint32_t HELPER(neon_hsub_u32
)(uint32_t src1
, uint32_t src2
)
559 dest
= (src1
>> 1) - (src2
>> 1);
560 if ((~src1
) & src2
& 1)
565 #define NEON_FN(dest, src1, src2) dest = (src1 > src2) ? ~0 : 0
566 NEON_VOP(cgt_s8
, neon_s8
, 4)
567 NEON_VOP(cgt_u8
, neon_u8
, 4)
568 NEON_VOP(cgt_s16
, neon_s16
, 2)
569 NEON_VOP(cgt_u16
, neon_u16
, 2)
570 NEON_VOP(cgt_s32
, neon_s32
, 1)
571 NEON_VOP(cgt_u32
, neon_u32
, 1)
574 #define NEON_FN(dest, src1, src2) dest = (src1 >= src2) ? ~0 : 0
575 NEON_VOP(cge_s8
, neon_s8
, 4)
576 NEON_VOP(cge_u8
, neon_u8
, 4)
577 NEON_VOP(cge_s16
, neon_s16
, 2)
578 NEON_VOP(cge_u16
, neon_u16
, 2)
579 NEON_VOP(cge_s32
, neon_s32
, 1)
580 NEON_VOP(cge_u32
, neon_u32
, 1)
583 #define NEON_FN(dest, src1, src2) dest = (src1 < src2) ? src1 : src2
584 NEON_VOP(min_s8
, neon_s8
, 4)
585 NEON_VOP(min_u8
, neon_u8
, 4)
586 NEON_VOP(min_s16
, neon_s16
, 2)
587 NEON_VOP(min_u16
, neon_u16
, 2)
588 NEON_VOP(min_s32
, neon_s32
, 1)
589 NEON_VOP(min_u32
, neon_u32
, 1)
590 NEON_POP(pmin_s8
, neon_s8
, 4)
591 NEON_POP(pmin_u8
, neon_u8
, 4)
592 NEON_POP(pmin_s16
, neon_s16
, 2)
593 NEON_POP(pmin_u16
, neon_u16
, 2)
596 #define NEON_FN(dest, src1, src2) dest = (src1 > src2) ? src1 : src2
597 NEON_VOP(max_s8
, neon_s8
, 4)
598 NEON_VOP(max_u8
, neon_u8
, 4)
599 NEON_VOP(max_s16
, neon_s16
, 2)
600 NEON_VOP(max_u16
, neon_u16
, 2)
601 NEON_VOP(max_s32
, neon_s32
, 1)
602 NEON_VOP(max_u32
, neon_u32
, 1)
603 NEON_POP(pmax_s8
, neon_s8
, 4)
604 NEON_POP(pmax_u8
, neon_u8
, 4)
605 NEON_POP(pmax_s16
, neon_s16
, 2)
606 NEON_POP(pmax_u16
, neon_u16
, 2)
609 #define NEON_FN(dest, src1, src2) \
610 dest = (src1 > src2) ? (src1 - src2) : (src2 - src1)
611 NEON_VOP(abd_s8
, neon_s8
, 4)
612 NEON_VOP(abd_u8
, neon_u8
, 4)
613 NEON_VOP(abd_s16
, neon_s16
, 2)
614 NEON_VOP(abd_u16
, neon_u16
, 2)
615 NEON_VOP(abd_s32
, neon_s32
, 1)
616 NEON_VOP(abd_u32
, neon_u32
, 1)
619 #define NEON_FN(dest, src1, src2) do { \
621 tmp = (int8_t)src2; \
622 if (tmp >= (ssize_t)sizeof(src1) * 8 || \
623 tmp <= -(ssize_t)sizeof(src1) * 8) { \
625 } else if (tmp < 0) { \
626 dest = src1 >> -tmp; \
628 dest = src1 << tmp; \
630 NEON_VOP(shl_u8
, neon_u8
, 4)
631 NEON_VOP(shl_u16
, neon_u16
, 2)
632 NEON_VOP(shl_u32
, neon_u32
, 1)
635 uint64_t HELPER(neon_shl_u64
)(uint64_t val
, uint64_t shiftop
)
637 int8_t shift
= (int8_t)shiftop
;
638 if (shift
>= 64 || shift
<= -64) {
640 } else if (shift
< 0) {
648 #define NEON_FN(dest, src1, src2) do { \
650 tmp = (int8_t)src2; \
651 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
653 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
654 dest = src1 >> (sizeof(src1) * 8 - 1); \
655 } else if (tmp < 0) { \
656 dest = src1 >> -tmp; \
658 dest = src1 << tmp; \
660 NEON_VOP(shl_s8
, neon_s8
, 4)
661 NEON_VOP(shl_s16
, neon_s16
, 2)
662 NEON_VOP(shl_s32
, neon_s32
, 1)
665 uint64_t HELPER(neon_shl_s64
)(uint64_t valop
, uint64_t shiftop
)
667 int8_t shift
= (int8_t)shiftop
;
671 } else if (shift
<= -64) {
673 } else if (shift
< 0) {
681 #define NEON_FN(dest, src1, src2) do { \
683 tmp = (int8_t)src2; \
684 if ((tmp >= (ssize_t)sizeof(src1) * 8) \
685 || (tmp <= -(ssize_t)sizeof(src1) * 8)) { \
687 } else if (tmp < 0) { \
688 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
690 dest = src1 << tmp; \
692 NEON_VOP(rshl_s8
, neon_s8
, 4)
693 NEON_VOP(rshl_s16
, neon_s16
, 2)
696 /* The addition of the rounding constant may overflow, so we use an
697 * intermediate 64 bit accumulator. */
698 uint32_t HELPER(neon_rshl_s32
)(uint32_t valop
, uint32_t shiftop
)
701 int32_t val
= (int32_t)valop
;
702 int8_t shift
= (int8_t)shiftop
;
703 if ((shift
>= 32) || (shift
<= -32)) {
705 } else if (shift
< 0) {
706 int64_t big_dest
= ((int64_t)val
+ (1 << (-1 - shift
)));
707 dest
= big_dest
>> -shift
;
714 /* Handling addition overflow with 64 bit input values is more
715 * tricky than with 32 bit values. */
716 uint64_t HELPER(neon_rshl_s64
)(uint64_t valop
, uint64_t shiftop
)
718 int8_t shift
= (int8_t)shiftop
;
720 if ((shift
>= 64) || (shift
<= -64)) {
722 } else if (shift
< 0) {
723 val
>>= (-shift
- 1);
724 if (val
== INT64_MAX
) {
725 /* In this case, it means that the rounding constant is 1,
726 * and the addition would overflow. Return the actual
727 * result directly. */
728 val
= 0x4000000000000000LL
;
739 #define NEON_FN(dest, src1, src2) do { \
741 tmp = (int8_t)src2; \
742 if (tmp >= (ssize_t)sizeof(src1) * 8 || \
743 tmp < -(ssize_t)sizeof(src1) * 8) { \
745 } else if (tmp == -(ssize_t)sizeof(src1) * 8) { \
746 dest = src1 >> (-tmp - 1); \
747 } else if (tmp < 0) { \
748 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
750 dest = src1 << tmp; \
752 NEON_VOP(rshl_u8
, neon_u8
, 4)
753 NEON_VOP(rshl_u16
, neon_u16
, 2)
756 /* The addition of the rounding constant may overflow, so we use an
757 * intermediate 64 bit accumulator. */
758 uint32_t HELPER(neon_rshl_u32
)(uint32_t val
, uint32_t shiftop
)
761 int8_t shift
= (int8_t)shiftop
;
762 if (shift
>= 32 || shift
< -32) {
764 } else if (shift
== -32) {
766 } else if (shift
< 0) {
767 uint64_t big_dest
= ((uint64_t)val
+ (1 << (-1 - shift
)));
768 dest
= big_dest
>> -shift
;
775 /* Handling addition overflow with 64 bit input values is more
776 * tricky than with 32 bit values. */
777 uint64_t HELPER(neon_rshl_u64
)(uint64_t val
, uint64_t shiftop
)
779 int8_t shift
= (uint8_t)shiftop
;
780 if (shift
>= 64 || shift
< -64) {
782 } else if (shift
== -64) {
783 /* Rounding a 1-bit result just preserves that bit. */
785 } else if (shift
< 0) {
786 val
>>= (-shift
- 1);
787 if (val
== UINT64_MAX
) {
788 /* In this case, it means that the rounding constant is 1,
789 * and the addition would overflow. Return the actual
790 * result directly. */
791 val
= 0x8000000000000000ULL
;
802 #define NEON_FN(dest, src1, src2) do { \
804 tmp = (int8_t)src2; \
805 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
812 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
814 } else if (tmp < 0) { \
815 dest = src1 >> -tmp; \
817 dest = src1 << tmp; \
818 if ((dest >> tmp) != src1) { \
823 NEON_VOP_ENV(qshl_u8
, neon_u8
, 4)
824 NEON_VOP_ENV(qshl_u16
, neon_u16
, 2)
825 NEON_VOP_ENV(qshl_u32
, neon_u32
, 1)
828 uint64_t HELPER(neon_qshl_u64
)(CPUARMState
*env
, uint64_t val
, uint64_t shiftop
)
830 int8_t shift
= (int8_t)shiftop
;
836 } else if (shift
<= -64) {
838 } else if (shift
< 0) {
843 if ((val
>> shift
) != tmp
) {
851 #define NEON_FN(dest, src1, src2) do { \
853 tmp = (int8_t)src2; \
854 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
857 dest = (uint32_t)(1 << (sizeof(src1) * 8 - 1)); \
864 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
866 } else if (tmp < 0) { \
867 dest = src1 >> -tmp; \
869 dest = src1 << tmp; \
870 if ((dest >> tmp) != src1) { \
872 dest = (uint32_t)(1 << (sizeof(src1) * 8 - 1)); \
878 NEON_VOP_ENV(qshl_s8
, neon_s8
, 4)
879 NEON_VOP_ENV(qshl_s16
, neon_s16
, 2)
880 NEON_VOP_ENV(qshl_s32
, neon_s32
, 1)
883 uint64_t HELPER(neon_qshl_s64
)(CPUARMState
*env
, uint64_t valop
, uint64_t shiftop
)
885 int8_t shift
= (uint8_t)shiftop
;
890 val
= (val
>> 63) ^ ~SIGNBIT64
;
892 } else if (shift
<= -64) {
894 } else if (shift
< 0) {
899 if ((val
>> shift
) != tmp
) {
901 val
= (tmp
>> 63) ^ ~SIGNBIT64
;
907 #define NEON_FN(dest, src1, src2) do { \
908 if (src1 & (1 << (sizeof(src1) * 8 - 1))) { \
913 tmp = (int8_t)src2; \
914 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
921 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
923 } else if (tmp < 0) { \
924 dest = src1 >> -tmp; \
926 dest = src1 << tmp; \
927 if ((dest >> tmp) != src1) { \
933 NEON_VOP_ENV(qshlu_s8
, neon_u8
, 4)
934 NEON_VOP_ENV(qshlu_s16
, neon_u16
, 2)
937 uint32_t HELPER(neon_qshlu_s32
)(CPUARMState
*env
, uint32_t valop
, uint32_t shiftop
)
939 if ((int32_t)valop
< 0) {
943 return helper_neon_qshl_u32(env
, valop
, shiftop
);
946 uint64_t HELPER(neon_qshlu_s64
)(CPUARMState
*env
, uint64_t valop
, uint64_t shiftop
)
948 if ((int64_t)valop
< 0) {
952 return helper_neon_qshl_u64(env
, valop
, shiftop
);
955 #define NEON_FN(dest, src1, src2) do { \
957 tmp = (int8_t)src2; \
958 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
965 } else if (tmp < -(ssize_t)sizeof(src1) * 8) { \
967 } else if (tmp == -(ssize_t)sizeof(src1) * 8) { \
968 dest = src1 >> (sizeof(src1) * 8 - 1); \
969 } else if (tmp < 0) { \
970 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
972 dest = src1 << tmp; \
973 if ((dest >> tmp) != src1) { \
978 NEON_VOP_ENV(qrshl_u8
, neon_u8
, 4)
979 NEON_VOP_ENV(qrshl_u16
, neon_u16
, 2)
982 /* The addition of the rounding constant may overflow, so we use an
983 * intermediate 64 bit accumulator. */
984 uint32_t HELPER(neon_qrshl_u32
)(CPUARMState
*env
, uint32_t val
, uint32_t shiftop
)
987 int8_t shift
= (int8_t)shiftop
;
995 } else if (shift
< -32) {
997 } else if (shift
== -32) {
999 } else if (shift
< 0) {
1000 uint64_t big_dest
= ((uint64_t)val
+ (1 << (-1 - shift
)));
1001 dest
= big_dest
>> -shift
;
1003 dest
= val
<< shift
;
1004 if ((dest
>> shift
) != val
) {
1012 /* Handling addition overflow with 64 bit input values is more
1013 * tricky than with 32 bit values. */
1014 uint64_t HELPER(neon_qrshl_u64
)(CPUARMState
*env
, uint64_t val
, uint64_t shiftop
)
1016 int8_t shift
= (int8_t)shiftop
;
1022 } else if (shift
< -64) {
1024 } else if (shift
== -64) {
1026 } else if (shift
< 0) {
1027 val
>>= (-shift
- 1);
1028 if (val
== UINT64_MAX
) {
1029 /* In this case, it means that the rounding constant is 1,
1030 * and the addition would overflow. Return the actual
1031 * result directly. */
1032 val
= 0x8000000000000000ULL
;
1040 if ((val
>> shift
) != tmp
) {
1048 #define NEON_FN(dest, src1, src2) do { \
1050 tmp = (int8_t)src2; \
1051 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
1054 dest = (1 << (sizeof(src1) * 8 - 1)); \
1061 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
1063 } else if (tmp < 0) { \
1064 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
1066 dest = src1 << tmp; \
1067 if ((dest >> tmp) != src1) { \
1069 dest = (uint32_t)(1 << (sizeof(src1) * 8 - 1)); \
1075 NEON_VOP_ENV(qrshl_s8
, neon_s8
, 4)
1076 NEON_VOP_ENV(qrshl_s16
, neon_s16
, 2)
1079 /* The addition of the rounding constant may overflow, so we use an
1080 * intermediate 64 bit accumulator. */
1081 uint32_t HELPER(neon_qrshl_s32
)(CPUARMState
*env
, uint32_t valop
, uint32_t shiftop
)
1084 int32_t val
= (int32_t)valop
;
1085 int8_t shift
= (int8_t)shiftop
;
1089 dest
= (val
>> 31) ^ ~SIGNBIT
;
1093 } else if (shift
<= -32) {
1095 } else if (shift
< 0) {
1096 int64_t big_dest
= ((int64_t)val
+ (1 << (-1 - shift
)));
1097 dest
= big_dest
>> -shift
;
1099 dest
= val
<< shift
;
1100 if ((dest
>> shift
) != val
) {
1102 dest
= (val
>> 31) ^ ~SIGNBIT
;
1108 /* Handling addition overflow with 64 bit input values is more
1109 * tricky than with 32 bit values. */
1110 uint64_t HELPER(neon_qrshl_s64
)(CPUARMState
*env
, uint64_t valop
, uint64_t shiftop
)
1112 int8_t shift
= (uint8_t)shiftop
;
1113 int64_t val
= valop
;
1118 val
= (val
>> 63) ^ ~SIGNBIT64
;
1120 } else if (shift
<= -64) {
1122 } else if (shift
< 0) {
1123 val
>>= (-shift
- 1);
1124 if (val
== INT64_MAX
) {
1125 /* In this case, it means that the rounding constant is 1,
1126 * and the addition would overflow. Return the actual
1127 * result directly. */
1128 val
= 0x4000000000000000ULL
;
1136 if ((val
>> shift
) != tmp
) {
1138 val
= (tmp
>> 63) ^ ~SIGNBIT64
;
1144 uint32_t HELPER(neon_add_u8
)(uint32_t a
, uint32_t b
)
1147 mask
= (a
^ b
) & 0x80808080u
;
1150 return (a
+ b
) ^ mask
;
1153 uint32_t HELPER(neon_add_u16
)(uint32_t a
, uint32_t b
)
1156 mask
= (a
^ b
) & 0x80008000u
;
1159 return (a
+ b
) ^ mask
;
1162 #define NEON_FN(dest, src1, src2) dest = src1 + src2
1163 NEON_POP(padd_u8
, neon_u8
, 4)
1164 NEON_POP(padd_u16
, neon_u16
, 2)
1167 #define NEON_FN(dest, src1, src2) dest = src1 - src2
1168 NEON_VOP(sub_u8
, neon_u8
, 4)
1169 NEON_VOP(sub_u16
, neon_u16
, 2)
1172 #define NEON_FN(dest, src1, src2) dest = src1 * src2
1173 NEON_VOP(mul_u8
, neon_u8
, 4)
1174 NEON_VOP(mul_u16
, neon_u16
, 2)
1177 /* Polynomial multiplication is like integer multiplication except the
1178 partial products are XORed, not added. */
1179 uint32_t HELPER(neon_mul_p8
)(uint32_t op1
, uint32_t op2
)
1189 mask
|= (0xff << 8);
1190 if (op1
& (1 << 16))
1191 mask
|= (0xff << 16);
1192 if (op1
& (1 << 24))
1193 mask
|= (0xff << 24);
1194 result
^= op2
& mask
;
1195 op1
= (op1
>> 1) & 0x7f7f7f7f;
1196 op2
= (op2
<< 1) & 0xfefefefe;
1201 uint64_t HELPER(neon_mull_p8
)(uint32_t op1
, uint32_t op2
)
1203 uint64_t result
= 0;
1205 uint64_t op2ex
= op2
;
1206 op2ex
= (op2ex
& 0xff) |
1207 ((op2ex
& 0xff00) << 8) |
1208 ((op2ex
& 0xff0000) << 16) |
1209 ((op2ex
& 0xff000000) << 24);
1215 if (op1
& (1 << 8)) {
1216 mask
|= (0xffffU
<< 16);
1218 if (op1
& (1 << 16)) {
1219 mask
|= (0xffffULL
<< 32);
1221 if (op1
& (1 << 24)) {
1222 mask
|= (0xffffULL
<< 48);
1224 result
^= op2ex
& mask
;
1225 op1
= (op1
>> 1) & 0x7f7f7f7f;
1231 #define NEON_FN(dest, src1, src2) dest = (src1 & src2) ? -1 : 0
1232 NEON_VOP(tst_u8
, neon_u8
, 4)
1233 NEON_VOP(tst_u16
, neon_u16
, 2)
1234 NEON_VOP(tst_u32
, neon_u32
, 1)
1237 #define NEON_FN(dest, src1, src2) dest = (src1 == src2) ? -1 : 0
1238 NEON_VOP(ceq_u8
, neon_u8
, 4)
1239 NEON_VOP(ceq_u16
, neon_u16
, 2)
1240 NEON_VOP(ceq_u32
, neon_u32
, 1)
1243 #define NEON_FN(dest, src, dummy) dest = (src < 0) ? -src : src
1244 NEON_VOP1(abs_s8
, neon_s8
, 4)
1245 NEON_VOP1(abs_s16
, neon_s16
, 2)
1248 /* Count Leading Sign/Zero Bits. */
1249 static inline int do_clz8(uint8_t x
)
1257 static inline int do_clz16(uint16_t x
)
1260 for (n
= 16; x
; n
--)
1265 #define NEON_FN(dest, src, dummy) dest = do_clz8(src)
1266 NEON_VOP1(clz_u8
, neon_u8
, 4)
1269 #define NEON_FN(dest, src, dummy) dest = do_clz16(src)
1270 NEON_VOP1(clz_u16
, neon_u16
, 2)
1273 #define NEON_FN(dest, src, dummy) dest = do_clz8((src < 0) ? ~src : src) - 1
1274 NEON_VOP1(cls_s8
, neon_s8
, 4)
1277 #define NEON_FN(dest, src, dummy) dest = do_clz16((src < 0) ? ~src : src) - 1
1278 NEON_VOP1(cls_s16
, neon_s16
, 2)
1281 uint32_t HELPER(neon_cls_s32
)(uint32_t x
)
1286 for (count
= 32; x
; count
--)
1292 uint32_t HELPER(neon_cnt_u8
)(uint32_t x
)
1294 x
= (x
& 0x55555555) + ((x
>> 1) & 0x55555555);
1295 x
= (x
& 0x33333333) + ((x
>> 2) & 0x33333333);
1296 x
= (x
& 0x0f0f0f0f) + ((x
>> 4) & 0x0f0f0f0f);
1300 /* Reverse bits in each 8 bit word */
1301 uint32_t HELPER(neon_rbit_u8
)(uint32_t x
)
1303 x
= ((x
& 0xf0f0f0f0) >> 4)
1304 | ((x
& 0x0f0f0f0f) << 4);
1305 x
= ((x
& 0x88888888) >> 3)
1306 | ((x
& 0x44444444) >> 1)
1307 | ((x
& 0x22222222) << 1)
1308 | ((x
& 0x11111111) << 3);
1312 #define NEON_QDMULH16(dest, src1, src2, round) do { \
1313 uint32_t tmp = (int32_t)(int16_t) src1 * (int16_t) src2; \
1314 if ((tmp ^ (tmp << 1)) & SIGNBIT) { \
1316 tmp = (tmp >> 31) ^ ~SIGNBIT; \
1321 int32_t old = tmp; \
1323 if ((int32_t)tmp < old) { \
1325 tmp = SIGNBIT - 1; \
1330 #define NEON_FN(dest, src1, src2) NEON_QDMULH16(dest, src1, src2, 0)
1331 NEON_VOP_ENV(qdmulh_s16
, neon_s16
, 2)
1333 #define NEON_FN(dest, src1, src2) NEON_QDMULH16(dest, src1, src2, 1)
1334 NEON_VOP_ENV(qrdmulh_s16
, neon_s16
, 2)
1336 #undef NEON_QDMULH16
1338 #define NEON_QDMULH32(dest, src1, src2, round) do { \
1339 uint64_t tmp = (int64_t)(int32_t) src1 * (int32_t) src2; \
1340 if ((tmp ^ (tmp << 1)) & SIGNBIT64) { \
1342 tmp = (tmp >> 63) ^ ~SIGNBIT64; \
1347 int64_t old = tmp; \
1348 tmp += (int64_t)1 << 31; \
1349 if ((int64_t)tmp < old) { \
1351 tmp = SIGNBIT64 - 1; \
1356 #define NEON_FN(dest, src1, src2) NEON_QDMULH32(dest, src1, src2, 0)
1357 NEON_VOP_ENV(qdmulh_s32
, neon_s32
, 1)
1359 #define NEON_FN(dest, src1, src2) NEON_QDMULH32(dest, src1, src2, 1)
1360 NEON_VOP_ENV(qrdmulh_s32
, neon_s32
, 1)
1362 #undef NEON_QDMULH32
1364 uint32_t HELPER(neon_narrow_u8
)(uint64_t x
)
1366 return (x
& 0xffu
) | ((x
>> 8) & 0xff00u
) | ((x
>> 16) & 0xff0000u
)
1367 | ((x
>> 24) & 0xff000000u
);
1370 uint32_t HELPER(neon_narrow_u16
)(uint64_t x
)
1372 return (x
& 0xffffu
) | ((x
>> 16) & 0xffff0000u
);
1375 uint32_t HELPER(neon_narrow_high_u8
)(uint64_t x
)
1377 return ((x
>> 8) & 0xff) | ((x
>> 16) & 0xff00)
1378 | ((x
>> 24) & 0xff0000) | ((x
>> 32) & 0xff000000);
1381 uint32_t HELPER(neon_narrow_high_u16
)(uint64_t x
)
1383 return ((x
>> 16) & 0xffff) | ((x
>> 32) & 0xffff0000);
1386 uint32_t HELPER(neon_narrow_round_high_u8
)(uint64_t x
)
1388 x
&= 0xff80ff80ff80ff80ull
;
1389 x
+= 0x0080008000800080ull
;
1390 return ((x
>> 8) & 0xff) | ((x
>> 16) & 0xff00)
1391 | ((x
>> 24) & 0xff0000) | ((x
>> 32) & 0xff000000);
1394 uint32_t HELPER(neon_narrow_round_high_u16
)(uint64_t x
)
1396 x
&= 0xffff8000ffff8000ull
;
1397 x
+= 0x0000800000008000ull
;
1398 return ((x
>> 16) & 0xffff) | ((x
>> 32) & 0xffff0000);
1401 uint32_t HELPER(neon_unarrow_sat8
)(CPUARMState
*env
, uint64_t x
)
1417 res |= (uint32_t)d << (n / 2); \
1428 uint32_t HELPER(neon_narrow_sat_u8
)(CPUARMState
*env
, uint64_t x
)
1441 res |= (uint32_t)d << (n / 2);
1451 uint32_t HELPER(neon_narrow_sat_s8
)(CPUARMState
*env
, uint64_t x
)
1458 if (s != (int8_t)s) { \
1459 d = (s >> 15) ^ 0x7f; \
1464 res |= (uint32_t)d << (n / 2);
1474 uint32_t HELPER(neon_unarrow_sat16
)(CPUARMState
*env
, uint64_t x
)
1479 if (low
& 0x80000000) {
1482 } else if (low
> 0xffff) {
1487 if (high
& 0x80000000) {
1490 } else if (high
> 0xffff) {
1494 return low
| (high
<< 16);
1497 uint32_t HELPER(neon_narrow_sat_u16
)(CPUARMState
*env
, uint64_t x
)
1507 if (high
> 0xffff) {
1511 return low
| (high
<< 16);
1514 uint32_t HELPER(neon_narrow_sat_s16
)(CPUARMState
*env
, uint64_t x
)
1519 if (low
!= (int16_t)low
) {
1520 low
= (low
>> 31) ^ 0x7fff;
1524 if (high
!= (int16_t)high
) {
1525 high
= (high
>> 31) ^ 0x7fff;
1528 return (uint16_t)low
| (high
<< 16);
1531 uint32_t HELPER(neon_unarrow_sat32
)(CPUARMState
*env
, uint64_t x
)
1533 if (x
& 0x8000000000000000ull
) {
1537 if (x
> 0xffffffffu
) {
1544 uint32_t HELPER(neon_narrow_sat_u32
)(CPUARMState
*env
, uint64_t x
)
1546 if (x
> 0xffffffffu
) {
1553 uint32_t HELPER(neon_narrow_sat_s32
)(CPUARMState
*env
, uint64_t x
)
1555 if ((int64_t)x
!= (int32_t)x
) {
1557 return ((int64_t)x
>> 63) ^ 0x7fffffff;
1562 uint64_t HELPER(neon_widen_u8
)(uint32_t x
)
1567 tmp
= (uint8_t)(x
>> 8);
1569 tmp
= (uint8_t)(x
>> 16);
1571 tmp
= (uint8_t)(x
>> 24);
1576 uint64_t HELPER(neon_widen_s8
)(uint32_t x
)
1580 ret
= (uint16_t)(int8_t)x
;
1581 tmp
= (uint16_t)(int8_t)(x
>> 8);
1583 tmp
= (uint16_t)(int8_t)(x
>> 16);
1585 tmp
= (uint16_t)(int8_t)(x
>> 24);
1590 uint64_t HELPER(neon_widen_u16
)(uint32_t x
)
1592 uint64_t high
= (uint16_t)(x
>> 16);
1593 return ((uint16_t)x
) | (high
<< 32);
1596 uint64_t HELPER(neon_widen_s16
)(uint32_t x
)
1598 uint64_t high
= (int16_t)(x
>> 16);
1599 return ((uint32_t)(int16_t)x
) | (high
<< 32);
1602 uint64_t HELPER(neon_addl_u16
)(uint64_t a
, uint64_t b
)
1605 mask
= (a
^ b
) & 0x8000800080008000ull
;
1606 a
&= ~0x8000800080008000ull
;
1607 b
&= ~0x8000800080008000ull
;
1608 return (a
+ b
) ^ mask
;
1611 uint64_t HELPER(neon_addl_u32
)(uint64_t a
, uint64_t b
)
1614 mask
= (a
^ b
) & 0x8000000080000000ull
;
1615 a
&= ~0x8000000080000000ull
;
1616 b
&= ~0x8000000080000000ull
;
1617 return (a
+ b
) ^ mask
;
1620 uint64_t HELPER(neon_paddl_u16
)(uint64_t a
, uint64_t b
)
1625 tmp
= a
& 0x0000ffff0000ffffull
;
1626 tmp
+= (a
>> 16) & 0x0000ffff0000ffffull
;
1627 tmp2
= b
& 0xffff0000ffff0000ull
;
1628 tmp2
+= (b
<< 16) & 0xffff0000ffff0000ull
;
1629 return ( tmp
& 0xffff)
1630 | ((tmp
>> 16) & 0xffff0000ull
)
1631 | ((tmp2
<< 16) & 0xffff00000000ull
)
1632 | ( tmp2
& 0xffff000000000000ull
);
1635 uint64_t HELPER(neon_paddl_u32
)(uint64_t a
, uint64_t b
)
1637 uint32_t low
= a
+ (a
>> 32);
1638 uint32_t high
= b
+ (b
>> 32);
1639 return low
+ ((uint64_t)high
<< 32);
1642 uint64_t HELPER(neon_subl_u16
)(uint64_t a
, uint64_t b
)
1645 mask
= (a
^ ~b
) & 0x8000800080008000ull
;
1646 a
|= 0x8000800080008000ull
;
1647 b
&= ~0x8000800080008000ull
;
1648 return (a
- b
) ^ mask
;
1651 uint64_t HELPER(neon_subl_u32
)(uint64_t a
, uint64_t b
)
1654 mask
= (a
^ ~b
) & 0x8000000080000000ull
;
1655 a
|= 0x8000000080000000ull
;
1656 b
&= ~0x8000000080000000ull
;
1657 return (a
- b
) ^ mask
;
1660 uint64_t HELPER(neon_addl_saturate_s32
)(CPUARMState
*env
, uint64_t a
, uint64_t b
)
1668 if (((low
^ x
) & SIGNBIT
) && !((x
^ y
) & SIGNBIT
)) {
1670 low
= ((int32_t)x
>> 31) ^ ~SIGNBIT
;
1675 if (((high
^ x
) & SIGNBIT
) && !((x
^ y
) & SIGNBIT
)) {
1677 high
= ((int32_t)x
>> 31) ^ ~SIGNBIT
;
1679 return low
| ((uint64_t)high
<< 32);
1682 uint64_t HELPER(neon_addl_saturate_s64
)(CPUARMState
*env
, uint64_t a
, uint64_t b
)
1687 if (((result
^ a
) & SIGNBIT64
) && !((a
^ b
) & SIGNBIT64
)) {
1689 result
= ((int64_t)a
>> 63) ^ ~SIGNBIT64
;
1694 /* We have to do the arithmetic in a larger type than
1695 * the input type, because for example with a signed 32 bit
1696 * op the absolute difference can overflow a signed 32 bit value.
1698 #define DO_ABD(dest, x, y, intype, arithtype) do { \
1699 arithtype tmp_x = (intype)(x); \
1700 arithtype tmp_y = (intype)(y); \
1701 dest = ((tmp_x > tmp_y) ? tmp_x - tmp_y : tmp_y - tmp_x); \
1704 uint64_t HELPER(neon_abdl_u16
)(uint32_t a
, uint32_t b
)
1708 DO_ABD(result
, a
, b
, uint8_t, uint32_t);
1709 DO_ABD(tmp
, a
>> 8, b
>> 8, uint8_t, uint32_t);
1710 result
|= tmp
<< 16;
1711 DO_ABD(tmp
, a
>> 16, b
>> 16, uint8_t, uint32_t);
1712 result
|= tmp
<< 32;
1713 DO_ABD(tmp
, a
>> 24, b
>> 24, uint8_t, uint32_t);
1714 result
|= tmp
<< 48;
1718 uint64_t HELPER(neon_abdl_s16
)(uint32_t a
, uint32_t b
)
1722 DO_ABD(result
, a
, b
, int8_t, int32_t);
1723 DO_ABD(tmp
, a
>> 8, b
>> 8, int8_t, int32_t);
1724 result
|= tmp
<< 16;
1725 DO_ABD(tmp
, a
>> 16, b
>> 16, int8_t, int32_t);
1726 result
|= tmp
<< 32;
1727 DO_ABD(tmp
, a
>> 24, b
>> 24, int8_t, int32_t);
1728 result
|= tmp
<< 48;
1732 uint64_t HELPER(neon_abdl_u32
)(uint32_t a
, uint32_t b
)
1736 DO_ABD(result
, a
, b
, uint16_t, uint32_t);
1737 DO_ABD(tmp
, a
>> 16, b
>> 16, uint16_t, uint32_t);
1738 return result
| (tmp
<< 32);
1741 uint64_t HELPER(neon_abdl_s32
)(uint32_t a
, uint32_t b
)
1745 DO_ABD(result
, a
, b
, int16_t, int32_t);
1746 DO_ABD(tmp
, a
>> 16, b
>> 16, int16_t, int32_t);
1747 return result
| (tmp
<< 32);
1750 uint64_t HELPER(neon_abdl_u64
)(uint32_t a
, uint32_t b
)
1753 DO_ABD(result
, a
, b
, uint32_t, uint64_t);
1757 uint64_t HELPER(neon_abdl_s64
)(uint32_t a
, uint32_t b
)
1760 DO_ABD(result
, a
, b
, int32_t, int64_t);
1765 /* Widening multiply. Named type is the source type. */
1766 #define DO_MULL(dest, x, y, type1, type2) do { \
1769 dest = (type2)((type2)tmp_x * (type2)tmp_y); \
1772 uint64_t HELPER(neon_mull_u8
)(uint32_t a
, uint32_t b
)
1777 DO_MULL(result
, a
, b
, uint8_t, uint16_t);
1778 DO_MULL(tmp
, a
>> 8, b
>> 8, uint8_t, uint16_t);
1779 result
|= tmp
<< 16;
1780 DO_MULL(tmp
, a
>> 16, b
>> 16, uint8_t, uint16_t);
1781 result
|= tmp
<< 32;
1782 DO_MULL(tmp
, a
>> 24, b
>> 24, uint8_t, uint16_t);
1783 result
|= tmp
<< 48;
1787 uint64_t HELPER(neon_mull_s8
)(uint32_t a
, uint32_t b
)
1792 DO_MULL(result
, a
, b
, int8_t, uint16_t);
1793 DO_MULL(tmp
, a
>> 8, b
>> 8, int8_t, uint16_t);
1794 result
|= tmp
<< 16;
1795 DO_MULL(tmp
, a
>> 16, b
>> 16, int8_t, uint16_t);
1796 result
|= tmp
<< 32;
1797 DO_MULL(tmp
, a
>> 24, b
>> 24, int8_t, uint16_t);
1798 result
|= tmp
<< 48;
1802 uint64_t HELPER(neon_mull_u16
)(uint32_t a
, uint32_t b
)
1807 DO_MULL(result
, a
, b
, uint16_t, uint32_t);
1808 DO_MULL(tmp
, a
>> 16, b
>> 16, uint16_t, uint32_t);
1809 return result
| (tmp
<< 32);
1812 uint64_t HELPER(neon_mull_s16
)(uint32_t a
, uint32_t b
)
1817 DO_MULL(result
, a
, b
, int16_t, uint32_t);
1818 DO_MULL(tmp
, a
>> 16, b
>> 16, int16_t, uint32_t);
1819 return result
| (tmp
<< 32);
1822 uint64_t HELPER(neon_negl_u16
)(uint64_t x
)
1826 result
= (uint16_t)-x
;
1828 result
|= (uint64_t)tmp
<< 16;
1830 result
|= (uint64_t)tmp
<< 32;
1832 result
|= (uint64_t)tmp
<< 48;
1836 uint64_t HELPER(neon_negl_u32
)(uint64_t x
)
1839 uint32_t high
= -(x
>> 32);
1840 return low
| ((uint64_t)high
<< 32);
1843 /* Saturating sign manipulation. */
1844 /* ??? Make these use NEON_VOP1 */
1845 #define DO_QABS8(x) do { \
1846 if (x == (int8_t)0x80) { \
1849 } else if (x < 0) { \
1852 uint32_t HELPER(neon_qabs_s8
)(CPUARMState
*env
, uint32_t x
)
1855 NEON_UNPACK(neon_s8
, vec
, x
);
1860 NEON_PACK(neon_s8
, x
, vec
);
1865 #define DO_QNEG8(x) do { \
1866 if (x == (int8_t)0x80) { \
1872 uint32_t HELPER(neon_qneg_s8
)(CPUARMState
*env
, uint32_t x
)
1875 NEON_UNPACK(neon_s8
, vec
, x
);
1880 NEON_PACK(neon_s8
, x
, vec
);
1885 #define DO_QABS16(x) do { \
1886 if (x == (int16_t)0x8000) { \
1889 } else if (x < 0) { \
1892 uint32_t HELPER(neon_qabs_s16
)(CPUARMState
*env
, uint32_t x
)
1895 NEON_UNPACK(neon_s16
, vec
, x
);
1898 NEON_PACK(neon_s16
, x
, vec
);
1903 #define DO_QNEG16(x) do { \
1904 if (x == (int16_t)0x8000) { \
1910 uint32_t HELPER(neon_qneg_s16
)(CPUARMState
*env
, uint32_t x
)
1913 NEON_UNPACK(neon_s16
, vec
, x
);
1916 NEON_PACK(neon_s16
, x
, vec
);
1921 uint32_t HELPER(neon_qabs_s32
)(CPUARMState
*env
, uint32_t x
)
1926 } else if ((int32_t)x
< 0) {
1932 uint32_t HELPER(neon_qneg_s32
)(CPUARMState
*env
, uint32_t x
)
1943 uint64_t HELPER(neon_qabs_s64
)(CPUARMState
*env
, uint64_t x
)
1945 if (x
== SIGNBIT64
) {
1948 } else if ((int64_t)x
< 0) {
1954 uint64_t HELPER(neon_qneg_s64
)(CPUARMState
*env
, uint64_t x
)
1956 if (x
== SIGNBIT64
) {
1965 /* NEON Float helpers. */
1966 uint32_t HELPER(neon_abd_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1968 float_status
*fpst
= fpstp
;
1969 float32 f0
= make_float32(a
);
1970 float32 f1
= make_float32(b
);
1971 return float32_val(float32_abs(float32_sub(f0
, f1
, fpst
)));
1974 /* Floating point comparisons produce an integer result.
1975 * Note that EQ doesn't signal InvalidOp for QNaNs but GE and GT do.
1976 * Softfloat routines return 0/1, which we convert to the 0/-1 Neon requires.
1978 uint32_t HELPER(neon_ceq_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1980 float_status
*fpst
= fpstp
;
1981 return -float32_eq_quiet(make_float32(a
), make_float32(b
), fpst
);
1984 uint32_t HELPER(neon_cge_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1986 float_status
*fpst
= fpstp
;
1987 return -float32_le(make_float32(b
), make_float32(a
), fpst
);
1990 uint32_t HELPER(neon_cgt_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1992 float_status
*fpst
= fpstp
;
1993 return -float32_lt(make_float32(b
), make_float32(a
), fpst
);
1996 uint32_t HELPER(neon_acge_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1998 float_status
*fpst
= fpstp
;
1999 float32 f0
= float32_abs(make_float32(a
));
2000 float32 f1
= float32_abs(make_float32(b
));
2001 return -float32_le(f1
, f0
, fpst
);
2004 uint32_t HELPER(neon_acgt_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
2006 float_status
*fpst
= fpstp
;
2007 float32 f0
= float32_abs(make_float32(a
));
2008 float32 f1
= float32_abs(make_float32(b
));
2009 return -float32_lt(f1
, f0
, fpst
);
2012 uint64_t HELPER(neon_acge_f64
)(uint64_t a
, uint64_t b
, void *fpstp
)
2014 float_status
*fpst
= fpstp
;
2015 float64 f0
= float64_abs(make_float64(a
));
2016 float64 f1
= float64_abs(make_float64(b
));
2017 return -float64_le(f1
, f0
, fpst
);
2020 uint64_t HELPER(neon_acgt_f64
)(uint64_t a
, uint64_t b
, void *fpstp
)
2022 float_status
*fpst
= fpstp
;
2023 float64 f0
= float64_abs(make_float64(a
));
2024 float64 f1
= float64_abs(make_float64(b
));
2025 return -float64_lt(f1
, f0
, fpst
);
2028 #define ELEM(V, N, SIZE) (((V) >> ((N) * (SIZE))) & ((1ull << (SIZE)) - 1))
2030 void HELPER(neon_qunzip8
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2032 uint64_t zm0
= float64_val(env
->vfp
.regs
[rm
]);
2033 uint64_t zm1
= float64_val(env
->vfp
.regs
[rm
+ 1]);
2034 uint64_t zd0
= float64_val(env
->vfp
.regs
[rd
]);
2035 uint64_t zd1
= float64_val(env
->vfp
.regs
[rd
+ 1]);
2036 uint64_t d0
= ELEM(zd0
, 0, 8) | (ELEM(zd0
, 2, 8) << 8)
2037 | (ELEM(zd0
, 4, 8) << 16) | (ELEM(zd0
, 6, 8) << 24)
2038 | (ELEM(zd1
, 0, 8) << 32) | (ELEM(zd1
, 2, 8) << 40)
2039 | (ELEM(zd1
, 4, 8) << 48) | (ELEM(zd1
, 6, 8) << 56);
2040 uint64_t d1
= ELEM(zm0
, 0, 8) | (ELEM(zm0
, 2, 8) << 8)
2041 | (ELEM(zm0
, 4, 8) << 16) | (ELEM(zm0
, 6, 8) << 24)
2042 | (ELEM(zm1
, 0, 8) << 32) | (ELEM(zm1
, 2, 8) << 40)
2043 | (ELEM(zm1
, 4, 8) << 48) | (ELEM(zm1
, 6, 8) << 56);
2044 uint64_t m0
= ELEM(zd0
, 1, 8) | (ELEM(zd0
, 3, 8) << 8)
2045 | (ELEM(zd0
, 5, 8) << 16) | (ELEM(zd0
, 7, 8) << 24)
2046 | (ELEM(zd1
, 1, 8) << 32) | (ELEM(zd1
, 3, 8) << 40)
2047 | (ELEM(zd1
, 5, 8) << 48) | (ELEM(zd1
, 7, 8) << 56);
2048 uint64_t m1
= ELEM(zm0
, 1, 8) | (ELEM(zm0
, 3, 8) << 8)
2049 | (ELEM(zm0
, 5, 8) << 16) | (ELEM(zm0
, 7, 8) << 24)
2050 | (ELEM(zm1
, 1, 8) << 32) | (ELEM(zm1
, 3, 8) << 40)
2051 | (ELEM(zm1
, 5, 8) << 48) | (ELEM(zm1
, 7, 8) << 56);
2052 env
->vfp
.regs
[rm
] = make_float64(m0
);
2053 env
->vfp
.regs
[rm
+ 1] = make_float64(m1
);
2054 env
->vfp
.regs
[rd
] = make_float64(d0
);
2055 env
->vfp
.regs
[rd
+ 1] = make_float64(d1
);
2058 void HELPER(neon_qunzip16
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2060 uint64_t zm0
= float64_val(env
->vfp
.regs
[rm
]);
2061 uint64_t zm1
= float64_val(env
->vfp
.regs
[rm
+ 1]);
2062 uint64_t zd0
= float64_val(env
->vfp
.regs
[rd
]);
2063 uint64_t zd1
= float64_val(env
->vfp
.regs
[rd
+ 1]);
2064 uint64_t d0
= ELEM(zd0
, 0, 16) | (ELEM(zd0
, 2, 16) << 16)
2065 | (ELEM(zd1
, 0, 16) << 32) | (ELEM(zd1
, 2, 16) << 48);
2066 uint64_t d1
= ELEM(zm0
, 0, 16) | (ELEM(zm0
, 2, 16) << 16)
2067 | (ELEM(zm1
, 0, 16) << 32) | (ELEM(zm1
, 2, 16) << 48);
2068 uint64_t m0
= ELEM(zd0
, 1, 16) | (ELEM(zd0
, 3, 16) << 16)
2069 | (ELEM(zd1
, 1, 16) << 32) | (ELEM(zd1
, 3, 16) << 48);
2070 uint64_t m1
= ELEM(zm0
, 1, 16) | (ELEM(zm0
, 3, 16) << 16)
2071 | (ELEM(zm1
, 1, 16) << 32) | (ELEM(zm1
, 3, 16) << 48);
2072 env
->vfp
.regs
[rm
] = make_float64(m0
);
2073 env
->vfp
.regs
[rm
+ 1] = make_float64(m1
);
2074 env
->vfp
.regs
[rd
] = make_float64(d0
);
2075 env
->vfp
.regs
[rd
+ 1] = make_float64(d1
);
2078 void HELPER(neon_qunzip32
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2080 uint64_t zm0
= float64_val(env
->vfp
.regs
[rm
]);
2081 uint64_t zm1
= float64_val(env
->vfp
.regs
[rm
+ 1]);
2082 uint64_t zd0
= float64_val(env
->vfp
.regs
[rd
]);
2083 uint64_t zd1
= float64_val(env
->vfp
.regs
[rd
+ 1]);
2084 uint64_t d0
= ELEM(zd0
, 0, 32) | (ELEM(zd1
, 0, 32) << 32);
2085 uint64_t d1
= ELEM(zm0
, 0, 32) | (ELEM(zm1
, 0, 32) << 32);
2086 uint64_t m0
= ELEM(zd0
, 1, 32) | (ELEM(zd1
, 1, 32) << 32);
2087 uint64_t m1
= ELEM(zm0
, 1, 32) | (ELEM(zm1
, 1, 32) << 32);
2088 env
->vfp
.regs
[rm
] = make_float64(m0
);
2089 env
->vfp
.regs
[rm
+ 1] = make_float64(m1
);
2090 env
->vfp
.regs
[rd
] = make_float64(d0
);
2091 env
->vfp
.regs
[rd
+ 1] = make_float64(d1
);
2094 void HELPER(neon_unzip8
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2096 uint64_t zm
= float64_val(env
->vfp
.regs
[rm
]);
2097 uint64_t zd
= float64_val(env
->vfp
.regs
[rd
]);
2098 uint64_t d0
= ELEM(zd
, 0, 8) | (ELEM(zd
, 2, 8) << 8)
2099 | (ELEM(zd
, 4, 8) << 16) | (ELEM(zd
, 6, 8) << 24)
2100 | (ELEM(zm
, 0, 8) << 32) | (ELEM(zm
, 2, 8) << 40)
2101 | (ELEM(zm
, 4, 8) << 48) | (ELEM(zm
, 6, 8) << 56);
2102 uint64_t m0
= ELEM(zd
, 1, 8) | (ELEM(zd
, 3, 8) << 8)
2103 | (ELEM(zd
, 5, 8) << 16) | (ELEM(zd
, 7, 8) << 24)
2104 | (ELEM(zm
, 1, 8) << 32) | (ELEM(zm
, 3, 8) << 40)
2105 | (ELEM(zm
, 5, 8) << 48) | (ELEM(zm
, 7, 8) << 56);
2106 env
->vfp
.regs
[rm
] = make_float64(m0
);
2107 env
->vfp
.regs
[rd
] = make_float64(d0
);
2110 void HELPER(neon_unzip16
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2112 uint64_t zm
= float64_val(env
->vfp
.regs
[rm
]);
2113 uint64_t zd
= float64_val(env
->vfp
.regs
[rd
]);
2114 uint64_t d0
= ELEM(zd
, 0, 16) | (ELEM(zd
, 2, 16) << 16)
2115 | (ELEM(zm
, 0, 16) << 32) | (ELEM(zm
, 2, 16) << 48);
2116 uint64_t m0
= ELEM(zd
, 1, 16) | (ELEM(zd
, 3, 16) << 16)
2117 | (ELEM(zm
, 1, 16) << 32) | (ELEM(zm
, 3, 16) << 48);
2118 env
->vfp
.regs
[rm
] = make_float64(m0
);
2119 env
->vfp
.regs
[rd
] = make_float64(d0
);
2122 void HELPER(neon_qzip8
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2124 uint64_t zm0
= float64_val(env
->vfp
.regs
[rm
]);
2125 uint64_t zm1
= float64_val(env
->vfp
.regs
[rm
+ 1]);
2126 uint64_t zd0
= float64_val(env
->vfp
.regs
[rd
]);
2127 uint64_t zd1
= float64_val(env
->vfp
.regs
[rd
+ 1]);
2128 uint64_t d0
= ELEM(zd0
, 0, 8) | (ELEM(zm0
, 0, 8) << 8)
2129 | (ELEM(zd0
, 1, 8) << 16) | (ELEM(zm0
, 1, 8) << 24)
2130 | (ELEM(zd0
, 2, 8) << 32) | (ELEM(zm0
, 2, 8) << 40)
2131 | (ELEM(zd0
, 3, 8) << 48) | (ELEM(zm0
, 3, 8) << 56);
2132 uint64_t d1
= ELEM(zd0
, 4, 8) | (ELEM(zm0
, 4, 8) << 8)
2133 | (ELEM(zd0
, 5, 8) << 16) | (ELEM(zm0
, 5, 8) << 24)
2134 | (ELEM(zd0
, 6, 8) << 32) | (ELEM(zm0
, 6, 8) << 40)
2135 | (ELEM(zd0
, 7, 8) << 48) | (ELEM(zm0
, 7, 8) << 56);
2136 uint64_t m0
= ELEM(zd1
, 0, 8) | (ELEM(zm1
, 0, 8) << 8)
2137 | (ELEM(zd1
, 1, 8) << 16) | (ELEM(zm1
, 1, 8) << 24)
2138 | (ELEM(zd1
, 2, 8) << 32) | (ELEM(zm1
, 2, 8) << 40)
2139 | (ELEM(zd1
, 3, 8) << 48) | (ELEM(zm1
, 3, 8) << 56);
2140 uint64_t m1
= ELEM(zd1
, 4, 8) | (ELEM(zm1
, 4, 8) << 8)
2141 | (ELEM(zd1
, 5, 8) << 16) | (ELEM(zm1
, 5, 8) << 24)
2142 | (ELEM(zd1
, 6, 8) << 32) | (ELEM(zm1
, 6, 8) << 40)
2143 | (ELEM(zd1
, 7, 8) << 48) | (ELEM(zm1
, 7, 8) << 56);
2144 env
->vfp
.regs
[rm
] = make_float64(m0
);
2145 env
->vfp
.regs
[rm
+ 1] = make_float64(m1
);
2146 env
->vfp
.regs
[rd
] = make_float64(d0
);
2147 env
->vfp
.regs
[rd
+ 1] = make_float64(d1
);
2150 void HELPER(neon_qzip16
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2152 uint64_t zm0
= float64_val(env
->vfp
.regs
[rm
]);
2153 uint64_t zm1
= float64_val(env
->vfp
.regs
[rm
+ 1]);
2154 uint64_t zd0
= float64_val(env
->vfp
.regs
[rd
]);
2155 uint64_t zd1
= float64_val(env
->vfp
.regs
[rd
+ 1]);
2156 uint64_t d0
= ELEM(zd0
, 0, 16) | (ELEM(zm0
, 0, 16) << 16)
2157 | (ELEM(zd0
, 1, 16) << 32) | (ELEM(zm0
, 1, 16) << 48);
2158 uint64_t d1
= ELEM(zd0
, 2, 16) | (ELEM(zm0
, 2, 16) << 16)
2159 | (ELEM(zd0
, 3, 16) << 32) | (ELEM(zm0
, 3, 16) << 48);
2160 uint64_t m0
= ELEM(zd1
, 0, 16) | (ELEM(zm1
, 0, 16) << 16)
2161 | (ELEM(zd1
, 1, 16) << 32) | (ELEM(zm1
, 1, 16) << 48);
2162 uint64_t m1
= ELEM(zd1
, 2, 16) | (ELEM(zm1
, 2, 16) << 16)
2163 | (ELEM(zd1
, 3, 16) << 32) | (ELEM(zm1
, 3, 16) << 48);
2164 env
->vfp
.regs
[rm
] = make_float64(m0
);
2165 env
->vfp
.regs
[rm
+ 1] = make_float64(m1
);
2166 env
->vfp
.regs
[rd
] = make_float64(d0
);
2167 env
->vfp
.regs
[rd
+ 1] = make_float64(d1
);
2170 void HELPER(neon_qzip32
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2172 uint64_t zm0
= float64_val(env
->vfp
.regs
[rm
]);
2173 uint64_t zm1
= float64_val(env
->vfp
.regs
[rm
+ 1]);
2174 uint64_t zd0
= float64_val(env
->vfp
.regs
[rd
]);
2175 uint64_t zd1
= float64_val(env
->vfp
.regs
[rd
+ 1]);
2176 uint64_t d0
= ELEM(zd0
, 0, 32) | (ELEM(zm0
, 0, 32) << 32);
2177 uint64_t d1
= ELEM(zd0
, 1, 32) | (ELEM(zm0
, 1, 32) << 32);
2178 uint64_t m0
= ELEM(zd1
, 0, 32) | (ELEM(zm1
, 0, 32) << 32);
2179 uint64_t m1
= ELEM(zd1
, 1, 32) | (ELEM(zm1
, 1, 32) << 32);
2180 env
->vfp
.regs
[rm
] = make_float64(m0
);
2181 env
->vfp
.regs
[rm
+ 1] = make_float64(m1
);
2182 env
->vfp
.regs
[rd
] = make_float64(d0
);
2183 env
->vfp
.regs
[rd
+ 1] = make_float64(d1
);
2186 void HELPER(neon_zip8
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2188 uint64_t zm
= float64_val(env
->vfp
.regs
[rm
]);
2189 uint64_t zd
= float64_val(env
->vfp
.regs
[rd
]);
2190 uint64_t d0
= ELEM(zd
, 0, 8) | (ELEM(zm
, 0, 8) << 8)
2191 | (ELEM(zd
, 1, 8) << 16) | (ELEM(zm
, 1, 8) << 24)
2192 | (ELEM(zd
, 2, 8) << 32) | (ELEM(zm
, 2, 8) << 40)
2193 | (ELEM(zd
, 3, 8) << 48) | (ELEM(zm
, 3, 8) << 56);
2194 uint64_t m0
= ELEM(zd
, 4, 8) | (ELEM(zm
, 4, 8) << 8)
2195 | (ELEM(zd
, 5, 8) << 16) | (ELEM(zm
, 5, 8) << 24)
2196 | (ELEM(zd
, 6, 8) << 32) | (ELEM(zm
, 6, 8) << 40)
2197 | (ELEM(zd
, 7, 8) << 48) | (ELEM(zm
, 7, 8) << 56);
2198 env
->vfp
.regs
[rm
] = make_float64(m0
);
2199 env
->vfp
.regs
[rd
] = make_float64(d0
);
2202 void HELPER(neon_zip16
)(CPUARMState
*env
, uint32_t rd
, uint32_t rm
)
2204 uint64_t zm
= float64_val(env
->vfp
.regs
[rm
]);
2205 uint64_t zd
= float64_val(env
->vfp
.regs
[rd
]);
2206 uint64_t d0
= ELEM(zd
, 0, 16) | (ELEM(zm
, 0, 16) << 16)
2207 | (ELEM(zd
, 1, 16) << 32) | (ELEM(zm
, 1, 16) << 48);
2208 uint64_t m0
= ELEM(zd
, 2, 16) | (ELEM(zm
, 2, 16) << 16)
2209 | (ELEM(zd
, 3, 16) << 32) | (ELEM(zm
, 3, 16) << 48);
2210 env
->vfp
.regs
[rm
] = make_float64(m0
);
2211 env
->vfp
.regs
[rd
] = make_float64(d0
);
2214 /* Helper function for 64 bit polynomial multiply case:
2215 * perform PolynomialMult(op1, op2) and return either the top or
2216 * bottom half of the 128 bit result.
2218 uint64_t HELPER(neon_pmull_64_lo
)(uint64_t op1
, uint64_t op2
)
2223 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
2224 if (op1
& (1ULL << bitnum
)) {
2225 res
^= op2
<< bitnum
;
2230 uint64_t HELPER(neon_pmull_64_hi
)(uint64_t op1
, uint64_t op2
)
2235 /* bit 0 of op1 can't influence the high 64 bits at all */
2236 for (bitnum
= 1; bitnum
< 64; bitnum
++) {
2237 if (op1
& (1ULL << bitnum
)) {
2238 res
^= op2
>> (64 - bitnum
);