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/helper-proto.h"
13 #include "fpu/softfloat.h"
15 #define SIGNBIT (uint32_t)0x80000000
16 #define SIGNBIT64 ((uint64_t)1 << 63)
18 #define SET_QC() env->vfp.qc[0] = 1
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_POP(pmin_s8
, neon_s8
, 4)
585 NEON_POP(pmin_u8
, neon_u8
, 4)
586 NEON_POP(pmin_s16
, neon_s16
, 2)
587 NEON_POP(pmin_u16
, neon_u16
, 2)
590 #define NEON_FN(dest, src1, src2) dest = (src1 > src2) ? src1 : src2
591 NEON_POP(pmax_s8
, neon_s8
, 4)
592 NEON_POP(pmax_u8
, neon_u8
, 4)
593 NEON_POP(pmax_s16
, neon_s16
, 2)
594 NEON_POP(pmax_u16
, neon_u16
, 2)
597 #define NEON_FN(dest, src1, src2) \
598 dest = (src1 > src2) ? (src1 - src2) : (src2 - src1)
599 NEON_VOP(abd_s8
, neon_s8
, 4)
600 NEON_VOP(abd_u8
, neon_u8
, 4)
601 NEON_VOP(abd_s16
, neon_s16
, 2)
602 NEON_VOP(abd_u16
, neon_u16
, 2)
603 NEON_VOP(abd_s32
, neon_s32
, 1)
604 NEON_VOP(abd_u32
, neon_u32
, 1)
607 #define NEON_FN(dest, src1, src2) do { \
609 tmp = (int8_t)src2; \
610 if (tmp >= (ssize_t)sizeof(src1) * 8 || \
611 tmp <= -(ssize_t)sizeof(src1) * 8) { \
613 } else if (tmp < 0) { \
614 dest = src1 >> -tmp; \
616 dest = src1 << tmp; \
618 NEON_VOP(shl_u8
, neon_u8
, 4)
619 NEON_VOP(shl_u16
, neon_u16
, 2)
620 NEON_VOP(shl_u32
, neon_u32
, 1)
623 uint64_t HELPER(neon_shl_u64
)(uint64_t val
, uint64_t shiftop
)
625 int8_t shift
= (int8_t)shiftop
;
626 if (shift
>= 64 || shift
<= -64) {
628 } else if (shift
< 0) {
636 #define NEON_FN(dest, src1, src2) do { \
638 tmp = (int8_t)src2; \
639 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
641 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
642 dest = src1 >> (sizeof(src1) * 8 - 1); \
643 } else if (tmp < 0) { \
644 dest = src1 >> -tmp; \
646 dest = src1 << tmp; \
648 NEON_VOP(shl_s8
, neon_s8
, 4)
649 NEON_VOP(shl_s16
, neon_s16
, 2)
650 NEON_VOP(shl_s32
, neon_s32
, 1)
653 uint64_t HELPER(neon_shl_s64
)(uint64_t valop
, uint64_t shiftop
)
655 int8_t shift
= (int8_t)shiftop
;
659 } else if (shift
<= -64) {
661 } else if (shift
< 0) {
669 #define NEON_FN(dest, src1, src2) do { \
671 tmp = (int8_t)src2; \
672 if ((tmp >= (ssize_t)sizeof(src1) * 8) \
673 || (tmp <= -(ssize_t)sizeof(src1) * 8)) { \
675 } else if (tmp < 0) { \
676 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
678 dest = src1 << tmp; \
680 NEON_VOP(rshl_s8
, neon_s8
, 4)
681 NEON_VOP(rshl_s16
, neon_s16
, 2)
684 /* The addition of the rounding constant may overflow, so we use an
685 * intermediate 64 bit accumulator. */
686 uint32_t HELPER(neon_rshl_s32
)(uint32_t valop
, uint32_t shiftop
)
689 int32_t val
= (int32_t)valop
;
690 int8_t shift
= (int8_t)shiftop
;
691 if ((shift
>= 32) || (shift
<= -32)) {
693 } else if (shift
< 0) {
694 int64_t big_dest
= ((int64_t)val
+ (1 << (-1 - shift
)));
695 dest
= big_dest
>> -shift
;
702 /* Handling addition overflow with 64 bit input values is more
703 * tricky than with 32 bit values. */
704 uint64_t HELPER(neon_rshl_s64
)(uint64_t valop
, uint64_t shiftop
)
706 int8_t shift
= (int8_t)shiftop
;
708 if ((shift
>= 64) || (shift
<= -64)) {
710 } else if (shift
< 0) {
711 val
>>= (-shift
- 1);
712 if (val
== INT64_MAX
) {
713 /* In this case, it means that the rounding constant is 1,
714 * and the addition would overflow. Return the actual
715 * result directly. */
716 val
= 0x4000000000000000LL
;
727 #define NEON_FN(dest, src1, src2) do { \
729 tmp = (int8_t)src2; \
730 if (tmp >= (ssize_t)sizeof(src1) * 8 || \
731 tmp < -(ssize_t)sizeof(src1) * 8) { \
733 } else if (tmp == -(ssize_t)sizeof(src1) * 8) { \
734 dest = src1 >> (-tmp - 1); \
735 } else if (tmp < 0) { \
736 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
738 dest = src1 << tmp; \
740 NEON_VOP(rshl_u8
, neon_u8
, 4)
741 NEON_VOP(rshl_u16
, neon_u16
, 2)
744 /* The addition of the rounding constant may overflow, so we use an
745 * intermediate 64 bit accumulator. */
746 uint32_t HELPER(neon_rshl_u32
)(uint32_t val
, uint32_t shiftop
)
749 int8_t shift
= (int8_t)shiftop
;
750 if (shift
>= 32 || shift
< -32) {
752 } else if (shift
== -32) {
754 } else if (shift
< 0) {
755 uint64_t big_dest
= ((uint64_t)val
+ (1 << (-1 - shift
)));
756 dest
= big_dest
>> -shift
;
763 /* Handling addition overflow with 64 bit input values is more
764 * tricky than with 32 bit values. */
765 uint64_t HELPER(neon_rshl_u64
)(uint64_t val
, uint64_t shiftop
)
767 int8_t shift
= (uint8_t)shiftop
;
768 if (shift
>= 64 || shift
< -64) {
770 } else if (shift
== -64) {
771 /* Rounding a 1-bit result just preserves that bit. */
773 } else if (shift
< 0) {
774 val
>>= (-shift
- 1);
775 if (val
== UINT64_MAX
) {
776 /* In this case, it means that the rounding constant is 1,
777 * and the addition would overflow. Return the actual
778 * result directly. */
779 val
= 0x8000000000000000ULL
;
790 #define NEON_FN(dest, src1, src2) do { \
792 tmp = (int8_t)src2; \
793 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
800 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
802 } else if (tmp < 0) { \
803 dest = src1 >> -tmp; \
805 dest = src1 << tmp; \
806 if ((dest >> tmp) != src1) { \
811 NEON_VOP_ENV(qshl_u8
, neon_u8
, 4)
812 NEON_VOP_ENV(qshl_u16
, neon_u16
, 2)
813 NEON_VOP_ENV(qshl_u32
, neon_u32
, 1)
816 uint64_t HELPER(neon_qshl_u64
)(CPUARMState
*env
, uint64_t val
, uint64_t shiftop
)
818 int8_t shift
= (int8_t)shiftop
;
824 } else if (shift
<= -64) {
826 } else if (shift
< 0) {
831 if ((val
>> shift
) != tmp
) {
839 #define NEON_FN(dest, src1, src2) do { \
841 tmp = (int8_t)src2; \
842 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
845 dest = (uint32_t)(1 << (sizeof(src1) * 8 - 1)); \
852 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
854 } else if (tmp < 0) { \
855 dest = src1 >> -tmp; \
857 dest = src1 << tmp; \
858 if ((dest >> tmp) != src1) { \
860 dest = (uint32_t)(1 << (sizeof(src1) * 8 - 1)); \
866 NEON_VOP_ENV(qshl_s8
, neon_s8
, 4)
867 NEON_VOP_ENV(qshl_s16
, neon_s16
, 2)
868 NEON_VOP_ENV(qshl_s32
, neon_s32
, 1)
871 uint64_t HELPER(neon_qshl_s64
)(CPUARMState
*env
, uint64_t valop
, uint64_t shiftop
)
873 int8_t shift
= (uint8_t)shiftop
;
878 val
= (val
>> 63) ^ ~SIGNBIT64
;
880 } else if (shift
<= -64) {
882 } else if (shift
< 0) {
887 if ((val
>> shift
) != tmp
) {
889 val
= (tmp
>> 63) ^ ~SIGNBIT64
;
895 #define NEON_FN(dest, src1, src2) do { \
896 if (src1 & (1 << (sizeof(src1) * 8 - 1))) { \
901 tmp = (int8_t)src2; \
902 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
909 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
911 } else if (tmp < 0) { \
912 dest = src1 >> -tmp; \
914 dest = src1 << tmp; \
915 if ((dest >> tmp) != src1) { \
921 NEON_VOP_ENV(qshlu_s8
, neon_u8
, 4)
922 NEON_VOP_ENV(qshlu_s16
, neon_u16
, 2)
925 uint32_t HELPER(neon_qshlu_s32
)(CPUARMState
*env
, uint32_t valop
, uint32_t shiftop
)
927 if ((int32_t)valop
< 0) {
931 return helper_neon_qshl_u32(env
, valop
, shiftop
);
934 uint64_t HELPER(neon_qshlu_s64
)(CPUARMState
*env
, uint64_t valop
, uint64_t shiftop
)
936 if ((int64_t)valop
< 0) {
940 return helper_neon_qshl_u64(env
, valop
, shiftop
);
943 #define NEON_FN(dest, src1, src2) do { \
945 tmp = (int8_t)src2; \
946 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
953 } else if (tmp < -(ssize_t)sizeof(src1) * 8) { \
955 } else if (tmp == -(ssize_t)sizeof(src1) * 8) { \
956 dest = src1 >> (sizeof(src1) * 8 - 1); \
957 } else if (tmp < 0) { \
958 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
960 dest = src1 << tmp; \
961 if ((dest >> tmp) != src1) { \
966 NEON_VOP_ENV(qrshl_u8
, neon_u8
, 4)
967 NEON_VOP_ENV(qrshl_u16
, neon_u16
, 2)
970 /* The addition of the rounding constant may overflow, so we use an
971 * intermediate 64 bit accumulator. */
972 uint32_t HELPER(neon_qrshl_u32
)(CPUARMState
*env
, uint32_t val
, uint32_t shiftop
)
975 int8_t shift
= (int8_t)shiftop
;
983 } else if (shift
< -32) {
985 } else if (shift
== -32) {
987 } else if (shift
< 0) {
988 uint64_t big_dest
= ((uint64_t)val
+ (1 << (-1 - shift
)));
989 dest
= big_dest
>> -shift
;
992 if ((dest
>> shift
) != val
) {
1000 /* Handling addition overflow with 64 bit input values is more
1001 * tricky than with 32 bit values. */
1002 uint64_t HELPER(neon_qrshl_u64
)(CPUARMState
*env
, uint64_t val
, uint64_t shiftop
)
1004 int8_t shift
= (int8_t)shiftop
;
1010 } else if (shift
< -64) {
1012 } else if (shift
== -64) {
1014 } else if (shift
< 0) {
1015 val
>>= (-shift
- 1);
1016 if (val
== UINT64_MAX
) {
1017 /* In this case, it means that the rounding constant is 1,
1018 * and the addition would overflow. Return the actual
1019 * result directly. */
1020 val
= 0x8000000000000000ULL
;
1028 if ((val
>> shift
) != tmp
) {
1036 #define NEON_FN(dest, src1, src2) do { \
1038 tmp = (int8_t)src2; \
1039 if (tmp >= (ssize_t)sizeof(src1) * 8) { \
1042 dest = (typeof(dest))(1 << (sizeof(src1) * 8 - 1)); \
1049 } else if (tmp <= -(ssize_t)sizeof(src1) * 8) { \
1051 } else if (tmp < 0) { \
1052 dest = (src1 + (1 << (-1 - tmp))) >> -tmp; \
1054 dest = src1 << tmp; \
1055 if ((dest >> tmp) != src1) { \
1057 dest = (uint32_t)(1 << (sizeof(src1) * 8 - 1)); \
1063 NEON_VOP_ENV(qrshl_s8
, neon_s8
, 4)
1064 NEON_VOP_ENV(qrshl_s16
, neon_s16
, 2)
1067 /* The addition of the rounding constant may overflow, so we use an
1068 * intermediate 64 bit accumulator. */
1069 uint32_t HELPER(neon_qrshl_s32
)(CPUARMState
*env
, uint32_t valop
, uint32_t shiftop
)
1072 int32_t val
= (int32_t)valop
;
1073 int8_t shift
= (int8_t)shiftop
;
1077 dest
= (val
>> 31) ^ ~SIGNBIT
;
1081 } else if (shift
<= -32) {
1083 } else if (shift
< 0) {
1084 int64_t big_dest
= ((int64_t)val
+ (1 << (-1 - shift
)));
1085 dest
= big_dest
>> -shift
;
1087 dest
= val
<< shift
;
1088 if ((dest
>> shift
) != val
) {
1090 dest
= (val
>> 31) ^ ~SIGNBIT
;
1096 /* Handling addition overflow with 64 bit input values is more
1097 * tricky than with 32 bit values. */
1098 uint64_t HELPER(neon_qrshl_s64
)(CPUARMState
*env
, uint64_t valop
, uint64_t shiftop
)
1100 int8_t shift
= (uint8_t)shiftop
;
1101 int64_t val
= valop
;
1106 val
= (val
>> 63) ^ ~SIGNBIT64
;
1108 } else if (shift
<= -64) {
1110 } else if (shift
< 0) {
1111 val
>>= (-shift
- 1);
1112 if (val
== INT64_MAX
) {
1113 /* In this case, it means that the rounding constant is 1,
1114 * and the addition would overflow. Return the actual
1115 * result directly. */
1116 val
= 0x4000000000000000ULL
;
1124 if ((val
>> shift
) != tmp
) {
1126 val
= (tmp
>> 63) ^ ~SIGNBIT64
;
1132 uint32_t HELPER(neon_add_u8
)(uint32_t a
, uint32_t b
)
1135 mask
= (a
^ b
) & 0x80808080u
;
1138 return (a
+ b
) ^ mask
;
1141 uint32_t HELPER(neon_add_u16
)(uint32_t a
, uint32_t b
)
1144 mask
= (a
^ b
) & 0x80008000u
;
1147 return (a
+ b
) ^ mask
;
1150 #define NEON_FN(dest, src1, src2) dest = src1 + src2
1151 NEON_POP(padd_u8
, neon_u8
, 4)
1152 NEON_POP(padd_u16
, neon_u16
, 2)
1155 #define NEON_FN(dest, src1, src2) dest = src1 - src2
1156 NEON_VOP(sub_u8
, neon_u8
, 4)
1157 NEON_VOP(sub_u16
, neon_u16
, 2)
1160 #define NEON_FN(dest, src1, src2) dest = src1 * src2
1161 NEON_VOP(mul_u8
, neon_u8
, 4)
1162 NEON_VOP(mul_u16
, neon_u16
, 2)
1165 /* Polynomial multiplication is like integer multiplication except the
1166 partial products are XORed, not added. */
1167 uint32_t HELPER(neon_mul_p8
)(uint32_t op1
, uint32_t op2
)
1177 mask
|= (0xff << 8);
1178 if (op1
& (1 << 16))
1179 mask
|= (0xff << 16);
1180 if (op1
& (1 << 24))
1181 mask
|= (0xff << 24);
1182 result
^= op2
& mask
;
1183 op1
= (op1
>> 1) & 0x7f7f7f7f;
1184 op2
= (op2
<< 1) & 0xfefefefe;
1189 uint64_t HELPER(neon_mull_p8
)(uint32_t op1
, uint32_t op2
)
1191 uint64_t result
= 0;
1193 uint64_t op2ex
= op2
;
1194 op2ex
= (op2ex
& 0xff) |
1195 ((op2ex
& 0xff00) << 8) |
1196 ((op2ex
& 0xff0000) << 16) |
1197 ((op2ex
& 0xff000000) << 24);
1203 if (op1
& (1 << 8)) {
1204 mask
|= (0xffffU
<< 16);
1206 if (op1
& (1 << 16)) {
1207 mask
|= (0xffffULL
<< 32);
1209 if (op1
& (1 << 24)) {
1210 mask
|= (0xffffULL
<< 48);
1212 result
^= op2ex
& mask
;
1213 op1
= (op1
>> 1) & 0x7f7f7f7f;
1219 #define NEON_FN(dest, src1, src2) dest = (src1 & src2) ? -1 : 0
1220 NEON_VOP(tst_u8
, neon_u8
, 4)
1221 NEON_VOP(tst_u16
, neon_u16
, 2)
1222 NEON_VOP(tst_u32
, neon_u32
, 1)
1225 #define NEON_FN(dest, src1, src2) dest = (src1 == src2) ? -1 : 0
1226 NEON_VOP(ceq_u8
, neon_u8
, 4)
1227 NEON_VOP(ceq_u16
, neon_u16
, 2)
1228 NEON_VOP(ceq_u32
, neon_u32
, 1)
1231 /* Count Leading Sign/Zero Bits. */
1232 static inline int do_clz8(uint8_t x
)
1240 static inline int do_clz16(uint16_t x
)
1243 for (n
= 16; x
; n
--)
1248 #define NEON_FN(dest, src, dummy) dest = do_clz8(src)
1249 NEON_VOP1(clz_u8
, neon_u8
, 4)
1252 #define NEON_FN(dest, src, dummy) dest = do_clz16(src)
1253 NEON_VOP1(clz_u16
, neon_u16
, 2)
1256 #define NEON_FN(dest, src, dummy) dest = do_clz8((src < 0) ? ~src : src) - 1
1257 NEON_VOP1(cls_s8
, neon_s8
, 4)
1260 #define NEON_FN(dest, src, dummy) dest = do_clz16((src < 0) ? ~src : src) - 1
1261 NEON_VOP1(cls_s16
, neon_s16
, 2)
1264 uint32_t HELPER(neon_cls_s32
)(uint32_t x
)
1269 for (count
= 32; x
; count
--)
1275 uint32_t HELPER(neon_cnt_u8
)(uint32_t x
)
1277 x
= (x
& 0x55555555) + ((x
>> 1) & 0x55555555);
1278 x
= (x
& 0x33333333) + ((x
>> 2) & 0x33333333);
1279 x
= (x
& 0x0f0f0f0f) + ((x
>> 4) & 0x0f0f0f0f);
1283 /* Reverse bits in each 8 bit word */
1284 uint32_t HELPER(neon_rbit_u8
)(uint32_t x
)
1286 x
= ((x
& 0xf0f0f0f0) >> 4)
1287 | ((x
& 0x0f0f0f0f) << 4);
1288 x
= ((x
& 0x88888888) >> 3)
1289 | ((x
& 0x44444444) >> 1)
1290 | ((x
& 0x22222222) << 1)
1291 | ((x
& 0x11111111) << 3);
1295 #define NEON_QDMULH16(dest, src1, src2, round) do { \
1296 uint32_t tmp = (int32_t)(int16_t) src1 * (int16_t) src2; \
1297 if ((tmp ^ (tmp << 1)) & SIGNBIT) { \
1299 tmp = (tmp >> 31) ^ ~SIGNBIT; \
1304 int32_t old = tmp; \
1306 if ((int32_t)tmp < old) { \
1308 tmp = SIGNBIT - 1; \
1313 #define NEON_FN(dest, src1, src2) NEON_QDMULH16(dest, src1, src2, 0)
1314 NEON_VOP_ENV(qdmulh_s16
, neon_s16
, 2)
1316 #define NEON_FN(dest, src1, src2) NEON_QDMULH16(dest, src1, src2, 1)
1317 NEON_VOP_ENV(qrdmulh_s16
, neon_s16
, 2)
1319 #undef NEON_QDMULH16
1321 #define NEON_QDMULH32(dest, src1, src2, round) do { \
1322 uint64_t tmp = (int64_t)(int32_t) src1 * (int32_t) src2; \
1323 if ((tmp ^ (tmp << 1)) & SIGNBIT64) { \
1325 tmp = (tmp >> 63) ^ ~SIGNBIT64; \
1330 int64_t old = tmp; \
1331 tmp += (int64_t)1 << 31; \
1332 if ((int64_t)tmp < old) { \
1334 tmp = SIGNBIT64 - 1; \
1339 #define NEON_FN(dest, src1, src2) NEON_QDMULH32(dest, src1, src2, 0)
1340 NEON_VOP_ENV(qdmulh_s32
, neon_s32
, 1)
1342 #define NEON_FN(dest, src1, src2) NEON_QDMULH32(dest, src1, src2, 1)
1343 NEON_VOP_ENV(qrdmulh_s32
, neon_s32
, 1)
1345 #undef NEON_QDMULH32
1347 uint32_t HELPER(neon_narrow_u8
)(uint64_t x
)
1349 return (x
& 0xffu
) | ((x
>> 8) & 0xff00u
) | ((x
>> 16) & 0xff0000u
)
1350 | ((x
>> 24) & 0xff000000u
);
1353 uint32_t HELPER(neon_narrow_u16
)(uint64_t x
)
1355 return (x
& 0xffffu
) | ((x
>> 16) & 0xffff0000u
);
1358 uint32_t HELPER(neon_narrow_high_u8
)(uint64_t x
)
1360 return ((x
>> 8) & 0xff) | ((x
>> 16) & 0xff00)
1361 | ((x
>> 24) & 0xff0000) | ((x
>> 32) & 0xff000000);
1364 uint32_t HELPER(neon_narrow_high_u16
)(uint64_t x
)
1366 return ((x
>> 16) & 0xffff) | ((x
>> 32) & 0xffff0000);
1369 uint32_t HELPER(neon_narrow_round_high_u8
)(uint64_t x
)
1371 x
&= 0xff80ff80ff80ff80ull
;
1372 x
+= 0x0080008000800080ull
;
1373 return ((x
>> 8) & 0xff) | ((x
>> 16) & 0xff00)
1374 | ((x
>> 24) & 0xff0000) | ((x
>> 32) & 0xff000000);
1377 uint32_t HELPER(neon_narrow_round_high_u16
)(uint64_t x
)
1379 x
&= 0xffff8000ffff8000ull
;
1380 x
+= 0x0000800000008000ull
;
1381 return ((x
>> 16) & 0xffff) | ((x
>> 32) & 0xffff0000);
1384 uint32_t HELPER(neon_unarrow_sat8
)(CPUARMState
*env
, uint64_t x
)
1400 res |= (uint32_t)d << (n / 2); \
1411 uint32_t HELPER(neon_narrow_sat_u8
)(CPUARMState
*env
, uint64_t x
)
1424 res |= (uint32_t)d << (n / 2);
1434 uint32_t HELPER(neon_narrow_sat_s8
)(CPUARMState
*env
, uint64_t x
)
1441 if (s != (int8_t)s) { \
1442 d = (s >> 15) ^ 0x7f; \
1447 res |= (uint32_t)d << (n / 2);
1457 uint32_t HELPER(neon_unarrow_sat16
)(CPUARMState
*env
, uint64_t x
)
1462 if (low
& 0x80000000) {
1465 } else if (low
> 0xffff) {
1470 if (high
& 0x80000000) {
1473 } else if (high
> 0xffff) {
1477 return low
| (high
<< 16);
1480 uint32_t HELPER(neon_narrow_sat_u16
)(CPUARMState
*env
, uint64_t x
)
1490 if (high
> 0xffff) {
1494 return low
| (high
<< 16);
1497 uint32_t HELPER(neon_narrow_sat_s16
)(CPUARMState
*env
, uint64_t x
)
1502 if (low
!= (int16_t)low
) {
1503 low
= (low
>> 31) ^ 0x7fff;
1507 if (high
!= (int16_t)high
) {
1508 high
= (high
>> 31) ^ 0x7fff;
1511 return (uint16_t)low
| (high
<< 16);
1514 uint32_t HELPER(neon_unarrow_sat32
)(CPUARMState
*env
, uint64_t x
)
1516 if (x
& 0x8000000000000000ull
) {
1520 if (x
> 0xffffffffu
) {
1527 uint32_t HELPER(neon_narrow_sat_u32
)(CPUARMState
*env
, uint64_t x
)
1529 if (x
> 0xffffffffu
) {
1536 uint32_t HELPER(neon_narrow_sat_s32
)(CPUARMState
*env
, uint64_t x
)
1538 if ((int64_t)x
!= (int32_t)x
) {
1540 return ((int64_t)x
>> 63) ^ 0x7fffffff;
1545 uint64_t HELPER(neon_widen_u8
)(uint32_t x
)
1550 tmp
= (uint8_t)(x
>> 8);
1552 tmp
= (uint8_t)(x
>> 16);
1554 tmp
= (uint8_t)(x
>> 24);
1559 uint64_t HELPER(neon_widen_s8
)(uint32_t x
)
1563 ret
= (uint16_t)(int8_t)x
;
1564 tmp
= (uint16_t)(int8_t)(x
>> 8);
1566 tmp
= (uint16_t)(int8_t)(x
>> 16);
1568 tmp
= (uint16_t)(int8_t)(x
>> 24);
1573 uint64_t HELPER(neon_widen_u16
)(uint32_t x
)
1575 uint64_t high
= (uint16_t)(x
>> 16);
1576 return ((uint16_t)x
) | (high
<< 32);
1579 uint64_t HELPER(neon_widen_s16
)(uint32_t x
)
1581 uint64_t high
= (int16_t)(x
>> 16);
1582 return ((uint32_t)(int16_t)x
) | (high
<< 32);
1585 uint64_t HELPER(neon_addl_u16
)(uint64_t a
, uint64_t b
)
1588 mask
= (a
^ b
) & 0x8000800080008000ull
;
1589 a
&= ~0x8000800080008000ull
;
1590 b
&= ~0x8000800080008000ull
;
1591 return (a
+ b
) ^ mask
;
1594 uint64_t HELPER(neon_addl_u32
)(uint64_t a
, uint64_t b
)
1597 mask
= (a
^ b
) & 0x8000000080000000ull
;
1598 a
&= ~0x8000000080000000ull
;
1599 b
&= ~0x8000000080000000ull
;
1600 return (a
+ b
) ^ mask
;
1603 uint64_t HELPER(neon_paddl_u16
)(uint64_t a
, uint64_t b
)
1608 tmp
= a
& 0x0000ffff0000ffffull
;
1609 tmp
+= (a
>> 16) & 0x0000ffff0000ffffull
;
1610 tmp2
= b
& 0xffff0000ffff0000ull
;
1611 tmp2
+= (b
<< 16) & 0xffff0000ffff0000ull
;
1612 return ( tmp
& 0xffff)
1613 | ((tmp
>> 16) & 0xffff0000ull
)
1614 | ((tmp2
<< 16) & 0xffff00000000ull
)
1615 | ( tmp2
& 0xffff000000000000ull
);
1618 uint64_t HELPER(neon_paddl_u32
)(uint64_t a
, uint64_t b
)
1620 uint32_t low
= a
+ (a
>> 32);
1621 uint32_t high
= b
+ (b
>> 32);
1622 return low
+ ((uint64_t)high
<< 32);
1625 uint64_t HELPER(neon_subl_u16
)(uint64_t a
, uint64_t b
)
1628 mask
= (a
^ ~b
) & 0x8000800080008000ull
;
1629 a
|= 0x8000800080008000ull
;
1630 b
&= ~0x8000800080008000ull
;
1631 return (a
- b
) ^ mask
;
1634 uint64_t HELPER(neon_subl_u32
)(uint64_t a
, uint64_t b
)
1637 mask
= (a
^ ~b
) & 0x8000000080000000ull
;
1638 a
|= 0x8000000080000000ull
;
1639 b
&= ~0x8000000080000000ull
;
1640 return (a
- b
) ^ mask
;
1643 uint64_t HELPER(neon_addl_saturate_s32
)(CPUARMState
*env
, uint64_t a
, uint64_t b
)
1651 if (((low
^ x
) & SIGNBIT
) && !((x
^ y
) & SIGNBIT
)) {
1653 low
= ((int32_t)x
>> 31) ^ ~SIGNBIT
;
1658 if (((high
^ x
) & SIGNBIT
) && !((x
^ y
) & SIGNBIT
)) {
1660 high
= ((int32_t)x
>> 31) ^ ~SIGNBIT
;
1662 return low
| ((uint64_t)high
<< 32);
1665 uint64_t HELPER(neon_addl_saturate_s64
)(CPUARMState
*env
, uint64_t a
, uint64_t b
)
1670 if (((result
^ a
) & SIGNBIT64
) && !((a
^ b
) & SIGNBIT64
)) {
1672 result
= ((int64_t)a
>> 63) ^ ~SIGNBIT64
;
1677 /* We have to do the arithmetic in a larger type than
1678 * the input type, because for example with a signed 32 bit
1679 * op the absolute difference can overflow a signed 32 bit value.
1681 #define DO_ABD(dest, x, y, intype, arithtype) do { \
1682 arithtype tmp_x = (intype)(x); \
1683 arithtype tmp_y = (intype)(y); \
1684 dest = ((tmp_x > tmp_y) ? tmp_x - tmp_y : tmp_y - tmp_x); \
1687 uint64_t HELPER(neon_abdl_u16
)(uint32_t a
, uint32_t b
)
1691 DO_ABD(result
, a
, b
, uint8_t, uint32_t);
1692 DO_ABD(tmp
, a
>> 8, b
>> 8, uint8_t, uint32_t);
1693 result
|= tmp
<< 16;
1694 DO_ABD(tmp
, a
>> 16, b
>> 16, uint8_t, uint32_t);
1695 result
|= tmp
<< 32;
1696 DO_ABD(tmp
, a
>> 24, b
>> 24, uint8_t, uint32_t);
1697 result
|= tmp
<< 48;
1701 uint64_t HELPER(neon_abdl_s16
)(uint32_t a
, uint32_t b
)
1705 DO_ABD(result
, a
, b
, int8_t, int32_t);
1706 DO_ABD(tmp
, a
>> 8, b
>> 8, int8_t, int32_t);
1707 result
|= tmp
<< 16;
1708 DO_ABD(tmp
, a
>> 16, b
>> 16, int8_t, int32_t);
1709 result
|= tmp
<< 32;
1710 DO_ABD(tmp
, a
>> 24, b
>> 24, int8_t, int32_t);
1711 result
|= tmp
<< 48;
1715 uint64_t HELPER(neon_abdl_u32
)(uint32_t a
, uint32_t b
)
1719 DO_ABD(result
, a
, b
, uint16_t, uint32_t);
1720 DO_ABD(tmp
, a
>> 16, b
>> 16, uint16_t, uint32_t);
1721 return result
| (tmp
<< 32);
1724 uint64_t HELPER(neon_abdl_s32
)(uint32_t a
, uint32_t b
)
1728 DO_ABD(result
, a
, b
, int16_t, int32_t);
1729 DO_ABD(tmp
, a
>> 16, b
>> 16, int16_t, int32_t);
1730 return result
| (tmp
<< 32);
1733 uint64_t HELPER(neon_abdl_u64
)(uint32_t a
, uint32_t b
)
1736 DO_ABD(result
, a
, b
, uint32_t, uint64_t);
1740 uint64_t HELPER(neon_abdl_s64
)(uint32_t a
, uint32_t b
)
1743 DO_ABD(result
, a
, b
, int32_t, int64_t);
1748 /* Widening multiply. Named type is the source type. */
1749 #define DO_MULL(dest, x, y, type1, type2) do { \
1752 dest = (type2)((type2)tmp_x * (type2)tmp_y); \
1755 uint64_t HELPER(neon_mull_u8
)(uint32_t a
, uint32_t b
)
1760 DO_MULL(result
, a
, b
, uint8_t, uint16_t);
1761 DO_MULL(tmp
, a
>> 8, b
>> 8, uint8_t, uint16_t);
1762 result
|= tmp
<< 16;
1763 DO_MULL(tmp
, a
>> 16, b
>> 16, uint8_t, uint16_t);
1764 result
|= tmp
<< 32;
1765 DO_MULL(tmp
, a
>> 24, b
>> 24, uint8_t, uint16_t);
1766 result
|= tmp
<< 48;
1770 uint64_t HELPER(neon_mull_s8
)(uint32_t a
, uint32_t b
)
1775 DO_MULL(result
, a
, b
, int8_t, uint16_t);
1776 DO_MULL(tmp
, a
>> 8, b
>> 8, int8_t, uint16_t);
1777 result
|= tmp
<< 16;
1778 DO_MULL(tmp
, a
>> 16, b
>> 16, int8_t, uint16_t);
1779 result
|= tmp
<< 32;
1780 DO_MULL(tmp
, a
>> 24, b
>> 24, int8_t, uint16_t);
1781 result
|= tmp
<< 48;
1785 uint64_t HELPER(neon_mull_u16
)(uint32_t a
, uint32_t b
)
1790 DO_MULL(result
, a
, b
, uint16_t, uint32_t);
1791 DO_MULL(tmp
, a
>> 16, b
>> 16, uint16_t, uint32_t);
1792 return result
| (tmp
<< 32);
1795 uint64_t HELPER(neon_mull_s16
)(uint32_t a
, uint32_t b
)
1800 DO_MULL(result
, a
, b
, int16_t, uint32_t);
1801 DO_MULL(tmp
, a
>> 16, b
>> 16, int16_t, uint32_t);
1802 return result
| (tmp
<< 32);
1805 uint64_t HELPER(neon_negl_u16
)(uint64_t x
)
1809 result
= (uint16_t)-x
;
1811 result
|= (uint64_t)tmp
<< 16;
1813 result
|= (uint64_t)tmp
<< 32;
1815 result
|= (uint64_t)tmp
<< 48;
1819 uint64_t HELPER(neon_negl_u32
)(uint64_t x
)
1822 uint32_t high
= -(x
>> 32);
1823 return low
| ((uint64_t)high
<< 32);
1826 /* Saturating sign manipulation. */
1827 /* ??? Make these use NEON_VOP1 */
1828 #define DO_QABS8(x) do { \
1829 if (x == (int8_t)0x80) { \
1832 } else if (x < 0) { \
1835 uint32_t HELPER(neon_qabs_s8
)(CPUARMState
*env
, uint32_t x
)
1838 NEON_UNPACK(neon_s8
, vec
, x
);
1843 NEON_PACK(neon_s8
, x
, vec
);
1848 #define DO_QNEG8(x) do { \
1849 if (x == (int8_t)0x80) { \
1855 uint32_t HELPER(neon_qneg_s8
)(CPUARMState
*env
, uint32_t x
)
1858 NEON_UNPACK(neon_s8
, vec
, x
);
1863 NEON_PACK(neon_s8
, x
, vec
);
1868 #define DO_QABS16(x) do { \
1869 if (x == (int16_t)0x8000) { \
1872 } else if (x < 0) { \
1875 uint32_t HELPER(neon_qabs_s16
)(CPUARMState
*env
, uint32_t x
)
1878 NEON_UNPACK(neon_s16
, vec
, x
);
1881 NEON_PACK(neon_s16
, x
, vec
);
1886 #define DO_QNEG16(x) do { \
1887 if (x == (int16_t)0x8000) { \
1893 uint32_t HELPER(neon_qneg_s16
)(CPUARMState
*env
, uint32_t x
)
1896 NEON_UNPACK(neon_s16
, vec
, x
);
1899 NEON_PACK(neon_s16
, x
, vec
);
1904 uint32_t HELPER(neon_qabs_s32
)(CPUARMState
*env
, uint32_t x
)
1909 } else if ((int32_t)x
< 0) {
1915 uint32_t HELPER(neon_qneg_s32
)(CPUARMState
*env
, uint32_t x
)
1926 uint64_t HELPER(neon_qabs_s64
)(CPUARMState
*env
, uint64_t x
)
1928 if (x
== SIGNBIT64
) {
1931 } else if ((int64_t)x
< 0) {
1937 uint64_t HELPER(neon_qneg_s64
)(CPUARMState
*env
, uint64_t x
)
1939 if (x
== SIGNBIT64
) {
1948 /* NEON Float helpers. */
1949 uint32_t HELPER(neon_abd_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1951 float_status
*fpst
= fpstp
;
1952 float32 f0
= make_float32(a
);
1953 float32 f1
= make_float32(b
);
1954 return float32_val(float32_abs(float32_sub(f0
, f1
, fpst
)));
1957 /* Floating point comparisons produce an integer result.
1958 * Note that EQ doesn't signal InvalidOp for QNaNs but GE and GT do.
1959 * Softfloat routines return 0/1, which we convert to the 0/-1 Neon requires.
1961 uint32_t HELPER(neon_ceq_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1963 float_status
*fpst
= fpstp
;
1964 return -float32_eq_quiet(make_float32(a
), make_float32(b
), fpst
);
1967 uint32_t HELPER(neon_cge_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1969 float_status
*fpst
= fpstp
;
1970 return -float32_le(make_float32(b
), make_float32(a
), fpst
);
1973 uint32_t HELPER(neon_cgt_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1975 float_status
*fpst
= fpstp
;
1976 return -float32_lt(make_float32(b
), make_float32(a
), fpst
);
1979 uint32_t HELPER(neon_acge_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1981 float_status
*fpst
= fpstp
;
1982 float32 f0
= float32_abs(make_float32(a
));
1983 float32 f1
= float32_abs(make_float32(b
));
1984 return -float32_le(f1
, f0
, fpst
);
1987 uint32_t HELPER(neon_acgt_f32
)(uint32_t a
, uint32_t b
, void *fpstp
)
1989 float_status
*fpst
= fpstp
;
1990 float32 f0
= float32_abs(make_float32(a
));
1991 float32 f1
= float32_abs(make_float32(b
));
1992 return -float32_lt(f1
, f0
, fpst
);
1995 uint64_t HELPER(neon_acge_f64
)(uint64_t a
, uint64_t b
, void *fpstp
)
1997 float_status
*fpst
= fpstp
;
1998 float64 f0
= float64_abs(make_float64(a
));
1999 float64 f1
= float64_abs(make_float64(b
));
2000 return -float64_le(f1
, f0
, fpst
);
2003 uint64_t HELPER(neon_acgt_f64
)(uint64_t a
, uint64_t b
, void *fpstp
)
2005 float_status
*fpst
= fpstp
;
2006 float64 f0
= float64_abs(make_float64(a
));
2007 float64 f1
= float64_abs(make_float64(b
));
2008 return -float64_lt(f1
, f0
, fpst
);
2011 #define ELEM(V, N, SIZE) (((V) >> ((N) * (SIZE))) & ((1ull << (SIZE)) - 1))
2013 void HELPER(neon_qunzip8
)(void *vd
, void *vm
)
2015 uint64_t *rd
= vd
, *rm
= vm
;
2016 uint64_t zd0
= rd
[0], zd1
= rd
[1];
2017 uint64_t zm0
= rm
[0], zm1
= rm
[1];
2019 uint64_t d0
= ELEM(zd0
, 0, 8) | (ELEM(zd0
, 2, 8) << 8)
2020 | (ELEM(zd0
, 4, 8) << 16) | (ELEM(zd0
, 6, 8) << 24)
2021 | (ELEM(zd1
, 0, 8) << 32) | (ELEM(zd1
, 2, 8) << 40)
2022 | (ELEM(zd1
, 4, 8) << 48) | (ELEM(zd1
, 6, 8) << 56);
2023 uint64_t d1
= ELEM(zm0
, 0, 8) | (ELEM(zm0
, 2, 8) << 8)
2024 | (ELEM(zm0
, 4, 8) << 16) | (ELEM(zm0
, 6, 8) << 24)
2025 | (ELEM(zm1
, 0, 8) << 32) | (ELEM(zm1
, 2, 8) << 40)
2026 | (ELEM(zm1
, 4, 8) << 48) | (ELEM(zm1
, 6, 8) << 56);
2027 uint64_t m0
= ELEM(zd0
, 1, 8) | (ELEM(zd0
, 3, 8) << 8)
2028 | (ELEM(zd0
, 5, 8) << 16) | (ELEM(zd0
, 7, 8) << 24)
2029 | (ELEM(zd1
, 1, 8) << 32) | (ELEM(zd1
, 3, 8) << 40)
2030 | (ELEM(zd1
, 5, 8) << 48) | (ELEM(zd1
, 7, 8) << 56);
2031 uint64_t m1
= ELEM(zm0
, 1, 8) | (ELEM(zm0
, 3, 8) << 8)
2032 | (ELEM(zm0
, 5, 8) << 16) | (ELEM(zm0
, 7, 8) << 24)
2033 | (ELEM(zm1
, 1, 8) << 32) | (ELEM(zm1
, 3, 8) << 40)
2034 | (ELEM(zm1
, 5, 8) << 48) | (ELEM(zm1
, 7, 8) << 56);
2042 void HELPER(neon_qunzip16
)(void *vd
, void *vm
)
2044 uint64_t *rd
= vd
, *rm
= vm
;
2045 uint64_t zd0
= rd
[0], zd1
= rd
[1];
2046 uint64_t zm0
= rm
[0], zm1
= rm
[1];
2048 uint64_t d0
= ELEM(zd0
, 0, 16) | (ELEM(zd0
, 2, 16) << 16)
2049 | (ELEM(zd1
, 0, 16) << 32) | (ELEM(zd1
, 2, 16) << 48);
2050 uint64_t d1
= ELEM(zm0
, 0, 16) | (ELEM(zm0
, 2, 16) << 16)
2051 | (ELEM(zm1
, 0, 16) << 32) | (ELEM(zm1
, 2, 16) << 48);
2052 uint64_t m0
= ELEM(zd0
, 1, 16) | (ELEM(zd0
, 3, 16) << 16)
2053 | (ELEM(zd1
, 1, 16) << 32) | (ELEM(zd1
, 3, 16) << 48);
2054 uint64_t m1
= ELEM(zm0
, 1, 16) | (ELEM(zm0
, 3, 16) << 16)
2055 | (ELEM(zm1
, 1, 16) << 32) | (ELEM(zm1
, 3, 16) << 48);
2063 void HELPER(neon_qunzip32
)(void *vd
, void *vm
)
2065 uint64_t *rd
= vd
, *rm
= vm
;
2066 uint64_t zd0
= rd
[0], zd1
= rd
[1];
2067 uint64_t zm0
= rm
[0], zm1
= rm
[1];
2069 uint64_t d0
= ELEM(zd0
, 0, 32) | (ELEM(zd1
, 0, 32) << 32);
2070 uint64_t d1
= ELEM(zm0
, 0, 32) | (ELEM(zm1
, 0, 32) << 32);
2071 uint64_t m0
= ELEM(zd0
, 1, 32) | (ELEM(zd1
, 1, 32) << 32);
2072 uint64_t m1
= ELEM(zm0
, 1, 32) | (ELEM(zm1
, 1, 32) << 32);
2080 void HELPER(neon_unzip8
)(void *vd
, void *vm
)
2082 uint64_t *rd
= vd
, *rm
= vm
;
2083 uint64_t zd
= rd
[0], zm
= rm
[0];
2085 uint64_t d0
= ELEM(zd
, 0, 8) | (ELEM(zd
, 2, 8) << 8)
2086 | (ELEM(zd
, 4, 8) << 16) | (ELEM(zd
, 6, 8) << 24)
2087 | (ELEM(zm
, 0, 8) << 32) | (ELEM(zm
, 2, 8) << 40)
2088 | (ELEM(zm
, 4, 8) << 48) | (ELEM(zm
, 6, 8) << 56);
2089 uint64_t m0
= ELEM(zd
, 1, 8) | (ELEM(zd
, 3, 8) << 8)
2090 | (ELEM(zd
, 5, 8) << 16) | (ELEM(zd
, 7, 8) << 24)
2091 | (ELEM(zm
, 1, 8) << 32) | (ELEM(zm
, 3, 8) << 40)
2092 | (ELEM(zm
, 5, 8) << 48) | (ELEM(zm
, 7, 8) << 56);
2098 void HELPER(neon_unzip16
)(void *vd
, void *vm
)
2100 uint64_t *rd
= vd
, *rm
= vm
;
2101 uint64_t zd
= rd
[0], zm
= rm
[0];
2103 uint64_t d0
= ELEM(zd
, 0, 16) | (ELEM(zd
, 2, 16) << 16)
2104 | (ELEM(zm
, 0, 16) << 32) | (ELEM(zm
, 2, 16) << 48);
2105 uint64_t m0
= ELEM(zd
, 1, 16) | (ELEM(zd
, 3, 16) << 16)
2106 | (ELEM(zm
, 1, 16) << 32) | (ELEM(zm
, 3, 16) << 48);
2112 void HELPER(neon_qzip8
)(void *vd
, void *vm
)
2114 uint64_t *rd
= vd
, *rm
= vm
;
2115 uint64_t zd0
= rd
[0], zd1
= rd
[1];
2116 uint64_t zm0
= rm
[0], zm1
= rm
[1];
2118 uint64_t d0
= ELEM(zd0
, 0, 8) | (ELEM(zm0
, 0, 8) << 8)
2119 | (ELEM(zd0
, 1, 8) << 16) | (ELEM(zm0
, 1, 8) << 24)
2120 | (ELEM(zd0
, 2, 8) << 32) | (ELEM(zm0
, 2, 8) << 40)
2121 | (ELEM(zd0
, 3, 8) << 48) | (ELEM(zm0
, 3, 8) << 56);
2122 uint64_t d1
= ELEM(zd0
, 4, 8) | (ELEM(zm0
, 4, 8) << 8)
2123 | (ELEM(zd0
, 5, 8) << 16) | (ELEM(zm0
, 5, 8) << 24)
2124 | (ELEM(zd0
, 6, 8) << 32) | (ELEM(zm0
, 6, 8) << 40)
2125 | (ELEM(zd0
, 7, 8) << 48) | (ELEM(zm0
, 7, 8) << 56);
2126 uint64_t m0
= ELEM(zd1
, 0, 8) | (ELEM(zm1
, 0, 8) << 8)
2127 | (ELEM(zd1
, 1, 8) << 16) | (ELEM(zm1
, 1, 8) << 24)
2128 | (ELEM(zd1
, 2, 8) << 32) | (ELEM(zm1
, 2, 8) << 40)
2129 | (ELEM(zd1
, 3, 8) << 48) | (ELEM(zm1
, 3, 8) << 56);
2130 uint64_t m1
= ELEM(zd1
, 4, 8) | (ELEM(zm1
, 4, 8) << 8)
2131 | (ELEM(zd1
, 5, 8) << 16) | (ELEM(zm1
, 5, 8) << 24)
2132 | (ELEM(zd1
, 6, 8) << 32) | (ELEM(zm1
, 6, 8) << 40)
2133 | (ELEM(zd1
, 7, 8) << 48) | (ELEM(zm1
, 7, 8) << 56);
2141 void HELPER(neon_qzip16
)(void *vd
, void *vm
)
2143 uint64_t *rd
= vd
, *rm
= vm
;
2144 uint64_t zd0
= rd
[0], zd1
= rd
[1];
2145 uint64_t zm0
= rm
[0], zm1
= rm
[1];
2147 uint64_t d0
= ELEM(zd0
, 0, 16) | (ELEM(zm0
, 0, 16) << 16)
2148 | (ELEM(zd0
, 1, 16) << 32) | (ELEM(zm0
, 1, 16) << 48);
2149 uint64_t d1
= ELEM(zd0
, 2, 16) | (ELEM(zm0
, 2, 16) << 16)
2150 | (ELEM(zd0
, 3, 16) << 32) | (ELEM(zm0
, 3, 16) << 48);
2151 uint64_t m0
= ELEM(zd1
, 0, 16) | (ELEM(zm1
, 0, 16) << 16)
2152 | (ELEM(zd1
, 1, 16) << 32) | (ELEM(zm1
, 1, 16) << 48);
2153 uint64_t m1
= ELEM(zd1
, 2, 16) | (ELEM(zm1
, 2, 16) << 16)
2154 | (ELEM(zd1
, 3, 16) << 32) | (ELEM(zm1
, 3, 16) << 48);
2162 void HELPER(neon_qzip32
)(void *vd
, void *vm
)
2164 uint64_t *rd
= vd
, *rm
= vm
;
2165 uint64_t zd0
= rd
[0], zd1
= rd
[1];
2166 uint64_t zm0
= rm
[0], zm1
= rm
[1];
2168 uint64_t d0
= ELEM(zd0
, 0, 32) | (ELEM(zm0
, 0, 32) << 32);
2169 uint64_t d1
= ELEM(zd0
, 1, 32) | (ELEM(zm0
, 1, 32) << 32);
2170 uint64_t m0
= ELEM(zd1
, 0, 32) | (ELEM(zm1
, 0, 32) << 32);
2171 uint64_t m1
= ELEM(zd1
, 1, 32) | (ELEM(zm1
, 1, 32) << 32);
2179 void HELPER(neon_zip8
)(void *vd
, void *vm
)
2181 uint64_t *rd
= vd
, *rm
= vm
;
2182 uint64_t zd
= rd
[0], zm
= rm
[0];
2184 uint64_t d0
= ELEM(zd
, 0, 8) | (ELEM(zm
, 0, 8) << 8)
2185 | (ELEM(zd
, 1, 8) << 16) | (ELEM(zm
, 1, 8) << 24)
2186 | (ELEM(zd
, 2, 8) << 32) | (ELEM(zm
, 2, 8) << 40)
2187 | (ELEM(zd
, 3, 8) << 48) | (ELEM(zm
, 3, 8) << 56);
2188 uint64_t m0
= ELEM(zd
, 4, 8) | (ELEM(zm
, 4, 8) << 8)
2189 | (ELEM(zd
, 5, 8) << 16) | (ELEM(zm
, 5, 8) << 24)
2190 | (ELEM(zd
, 6, 8) << 32) | (ELEM(zm
, 6, 8) << 40)
2191 | (ELEM(zd
, 7, 8) << 48) | (ELEM(zm
, 7, 8) << 56);
2197 void HELPER(neon_zip16
)(void *vd
, void *vm
)
2199 uint64_t *rd
= vd
, *rm
= vm
;
2200 uint64_t zd
= rd
[0], zm
= rm
[0];
2202 uint64_t d0
= ELEM(zd
, 0, 16) | (ELEM(zm
, 0, 16) << 16)
2203 | (ELEM(zd
, 1, 16) << 32) | (ELEM(zm
, 1, 16) << 48);
2204 uint64_t m0
= ELEM(zd
, 2, 16) | (ELEM(zm
, 2, 16) << 16)
2205 | (ELEM(zd
, 3, 16) << 32) | (ELEM(zm
, 3, 16) << 48);
2211 /* Helper function for 64 bit polynomial multiply case:
2212 * perform PolynomialMult(op1, op2) and return either the top or
2213 * bottom half of the 128 bit result.
2215 uint64_t HELPER(neon_pmull_64_lo
)(uint64_t op1
, uint64_t op2
)
2220 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
2221 if (op1
& (1ULL << bitnum
)) {
2222 res
^= op2
<< bitnum
;
2227 uint64_t HELPER(neon_pmull_64_hi
)(uint64_t op1
, uint64_t op2
)
2232 /* bit 0 of op1 can't influence the high 64 bits at all */
2233 for (bitnum
= 1; bitnum
< 64; bitnum
++) {
2234 if (op1
& (1ULL << bitnum
)) {
2235 res
^= op2
>> (64 - bitnum
);