2 * MIPS ASE DSP Instruction emulation helpers for QEMU.
4 * Copyright (c) 2012 Jia Liu <proljc@gmail.com>
5 * Dongxue Zhang <elta.era@gmail.com>
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/>.
21 #include "exec/helper-proto.h"
22 #include "qemu/bitops.h"
24 /* As the byte ordering doesn't matter, i.e. all columns are treated
25 identically, these unions can be used directly. */
46 /*** MIPS DSP internal functions begin ***/
47 #define MIPSDSP_ABS(x) (((x) >= 0) ? x : -x)
48 #define MIPSDSP_OVERFLOW_ADD(a, b, c, d) (~(a ^ b) & (a ^ c) & d)
49 #define MIPSDSP_OVERFLOW_SUB(a, b, c, d) ((a ^ b) & (a ^ c) & d)
51 static inline void set_DSPControl_overflow_flag(uint32_t flag
, int position
,
54 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< position
;
57 static inline void set_DSPControl_carryflag(bool flag
, CPUMIPSState
*env
)
59 env
->active_tc
.DSPControl
&= ~(1 << 13);
60 env
->active_tc
.DSPControl
|= flag
<< 13;
63 static inline uint32_t get_DSPControl_carryflag(CPUMIPSState
*env
)
65 return (env
->active_tc
.DSPControl
>> 13) & 0x01;
68 static inline void set_DSPControl_24(uint32_t flag
, int len
, CPUMIPSState
*env
)
72 filter
= ((0x01 << len
) - 1) << 24;
75 env
->active_tc
.DSPControl
&= filter
;
76 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 24;
79 static inline uint32_t get_DSPControl_24(int len
, CPUMIPSState
*env
)
83 filter
= (0x01 << len
) - 1;
85 return (env
->active_tc
.DSPControl
>> 24) & filter
;
88 static inline void set_DSPControl_pos(uint32_t pos
, CPUMIPSState
*env
)
92 dspc
= env
->active_tc
.DSPControl
;
94 dspc
= dspc
& 0xFFFFFFC0;
97 dspc
= dspc
& 0xFFFFFF80;
100 env
->active_tc
.DSPControl
= dspc
;
103 static inline uint32_t get_DSPControl_pos(CPUMIPSState
*env
)
108 dspc
= env
->active_tc
.DSPControl
;
110 #ifndef TARGET_MIPS64
119 static inline void set_DSPControl_efi(uint32_t flag
, CPUMIPSState
*env
)
121 env
->active_tc
.DSPControl
&= 0xFFFFBFFF;
122 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 14;
125 #define DO_MIPS_SAT_ABS(size) \
126 static inline int##size##_t mipsdsp_sat_abs##size(int##size##_t a, \
129 if (a == INT##size##_MIN) { \
130 set_DSPControl_overflow_flag(1, 20, env); \
131 return INT##size##_MAX; \
133 return MIPSDSP_ABS(a); \
139 #undef DO_MIPS_SAT_ABS
142 static inline int16_t mipsdsp_add_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
148 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x8000)) {
149 set_DSPControl_overflow_flag(1, 20, env
);
155 static inline int16_t mipsdsp_sat_add_i16(int16_t a
, int16_t b
,
162 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempS
, 0x8000)) {
168 set_DSPControl_overflow_flag(1, 20, env
);
174 static inline int32_t mipsdsp_sat_add_i32(int32_t a
, int32_t b
,
181 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x80000000)) {
187 set_DSPControl_overflow_flag(1, 20, env
);
193 static inline uint8_t mipsdsp_add_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
197 temp
= (uint16_t)a
+ (uint16_t)b
;
200 set_DSPControl_overflow_flag(1, 20, env
);
206 static inline uint16_t mipsdsp_add_u16(uint16_t a
, uint16_t b
,
211 temp
= (uint32_t)a
+ (uint32_t)b
;
213 if (temp
& 0x00010000) {
214 set_DSPControl_overflow_flag(1, 20, env
);
217 return temp
& 0xFFFF;
220 static inline uint8_t mipsdsp_sat_add_u8(uint8_t a
, uint8_t b
,
226 temp
= (uint16_t)a
+ (uint16_t)b
;
227 result
= temp
& 0xFF;
231 set_DSPControl_overflow_flag(1, 20, env
);
237 static inline uint16_t mipsdsp_sat_add_u16(uint16_t a
, uint16_t b
,
243 temp
= (uint32_t)a
+ (uint32_t)b
;
244 result
= temp
& 0xFFFF;
246 if (0x00010000 & temp
) {
248 set_DSPControl_overflow_flag(1, 20, env
);
254 static inline int32_t mipsdsp_sat32_acc_q31(int32_t acc
, int32_t a
,
258 int32_t temp32
, temp31
, result
;
261 #ifndef TARGET_MIPS64
262 temp
= ((uint64_t)env
->active_tc
.HI
[acc
] << 32) |
263 (uint64_t)env
->active_tc
.LO
[acc
];
265 temp
= (uint64_t)env
->active_tc
.LO
[acc
];
268 temp_sum
= (int64_t)a
+ temp
;
270 temp32
= (temp_sum
>> 32) & 0x01;
271 temp31
= (temp_sum
>> 31) & 0x01;
272 result
= temp_sum
& 0xFFFFFFFF;
274 if (temp32
!= temp31
) {
280 set_DSPControl_overflow_flag(1, 16 + acc
, env
);
286 /* a[0] is LO, a[1] is HI. */
287 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret
,
294 ret
[0] = env
->active_tc
.LO
[ac
] + a
[0];
295 ret
[1] = env
->active_tc
.HI
[ac
] + a
[1];
297 if (((uint64_t)ret
[0] < (uint64_t)env
->active_tc
.LO
[ac
]) &&
298 ((uint64_t)ret
[0] < (uint64_t)a
[0])) {
302 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
304 ret
[0] = (0x01ull
<< 63);
307 ret
[0] = (0x01ull
<< 63) - 1;
310 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
314 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret
,
321 ret
[0] = env
->active_tc
.LO
[ac
] - a
[0];
322 ret
[1] = env
->active_tc
.HI
[ac
] - a
[1];
324 if ((uint64_t)ret
[0] > (uint64_t)env
->active_tc
.LO
[ac
]) {
328 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
330 ret
[0] = (0x01ull
<< 63);
333 ret
[0] = (0x01ull
<< 63) - 1;
336 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
340 static inline int32_t mipsdsp_mul_i16_i16(int16_t a
, int16_t b
,
345 temp
= (int32_t)a
* (int32_t)b
;
347 if ((temp
> (int)0x7FFF) || (temp
< (int)0xFFFF8000)) {
348 set_DSPControl_overflow_flag(1, 21, env
);
355 static inline int32_t mipsdsp_mul_u16_u16(int32_t a
, int32_t b
)
360 static inline int32_t mipsdsp_mul_i32_i32(int32_t a
, int32_t b
)
365 static inline int32_t mipsdsp_sat16_mul_i16_i16(int16_t a
, int16_t b
,
370 temp
= (int32_t)a
* (int32_t)b
;
372 if (temp
> (int)0x7FFF) {
374 set_DSPControl_overflow_flag(1, 21, env
);
375 } else if (temp
< (int)0xffff8000) {
377 set_DSPControl_overflow_flag(1, 21, env
);
384 static inline int32_t mipsdsp_mul_q15_q15_overflowflag21(uint16_t a
, uint16_t b
,
389 if ((a
== 0x8000) && (b
== 0x8000)) {
391 set_DSPControl_overflow_flag(1, 21, env
);
393 temp
= ((int16_t)a
* (int16_t)b
) << 1;
400 static inline uint8_t mipsdsp_rshift_u8(uint8_t a
, target_ulong mov
)
405 static inline uint16_t mipsdsp_rshift_u16(uint16_t a
, target_ulong mov
)
410 static inline int8_t mipsdsp_rashift8(int8_t a
, target_ulong mov
)
415 static inline int16_t mipsdsp_rashift16(int16_t a
, target_ulong mov
)
420 static inline int32_t mipsdsp_rashift32(int32_t a
, target_ulong mov
)
425 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a
, int16_t b
)
429 temp
= (int32_t)a
+ (int32_t)b
;
431 return (temp
>> 1) & 0xFFFF;
434 /* round right shift */
435 static inline int16_t mipsdsp_rrshift1_add_q16(int16_t a
, int16_t b
)
439 temp
= (int32_t)a
+ (int32_t)b
;
442 return (temp
>> 1) & 0xFFFF;
445 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a
, int32_t b
)
449 temp
= (int64_t)a
+ (int64_t)b
;
451 return (temp
>> 1) & 0xFFFFFFFF;
454 static inline int32_t mipsdsp_rrshift1_add_q32(int32_t a
, int32_t b
)
458 temp
= (int64_t)a
+ (int64_t)b
;
461 return (temp
>> 1) & 0xFFFFFFFF;
464 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a
, uint8_t b
)
468 temp
= (uint16_t)a
+ (uint16_t)b
;
470 return (temp
>> 1) & 0x00FF;
473 static inline uint8_t mipsdsp_rrshift1_add_u8(uint8_t a
, uint8_t b
)
477 temp
= (uint16_t)a
+ (uint16_t)b
+ 1;
479 return (temp
>> 1) & 0x00FF;
482 static inline uint8_t mipsdsp_rshift1_sub_u8(uint8_t a
, uint8_t b
)
486 temp
= (uint16_t)a
- (uint16_t)b
;
488 return (temp
>> 1) & 0x00FF;
491 static inline uint8_t mipsdsp_rrshift1_sub_u8(uint8_t a
, uint8_t b
)
495 temp
= (uint16_t)a
- (uint16_t)b
+ 1;
497 return (temp
>> 1) & 0x00FF;
500 /* 128 bits long. p[0] is LO, p[1] is HI. */
501 static inline void mipsdsp_rndrashift_short_acc(int64_t *p
,
508 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
509 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
510 p
[0] = (shift
== 0) ? (acc
<< 1) : (acc
>> (shift
- 1));
511 p
[1] = (acc
>> 63) & 0x01;
514 /* 128 bits long. p[0] is LO, p[1] is HI */
515 static inline void mipsdsp_rashift_acc(uint64_t *p
,
520 uint64_t tempB
, tempA
;
522 tempB
= env
->active_tc
.HI
[ac
];
523 tempA
= env
->active_tc
.LO
[ac
];
524 shift
= shift
& 0x1F;
530 p
[0] = (tempB
<< (64 - shift
)) | (tempA
>> shift
);
531 p
[1] = (int64_t)tempB
>> shift
;
535 /* 128 bits long. p[0] is LO, p[1] is HI , p[2] is sign of HI.*/
536 static inline void mipsdsp_rndrashift_acc(uint64_t *p
,
541 int64_t tempB
, tempA
;
543 tempB
= env
->active_tc
.HI
[ac
];
544 tempA
= env
->active_tc
.LO
[ac
];
545 shift
= shift
& 0x3F;
549 p
[1] = (tempB
<< 1) | (tempA
>> 63);
552 p
[0] = (tempB
<< (65 - shift
)) | (tempA
>> (shift
- 1));
553 p
[1] = (int64_t)tempB
>> (shift
- 1);
562 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac
, uint16_t a
, uint16_t b
,
567 if ((a
== 0x8000) && (b
== 0x8000)) {
569 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
571 temp
= ((int16_t)a
* (int16_t)b
) << 1;
577 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac
, uint32_t a
, uint32_t b
,
582 if ((a
== 0x80000000) && (b
== 0x80000000)) {
583 temp
= (0x01ull
<< 63) - 1;
584 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
586 temp
= ((int64_t)(int32_t)a
* (int32_t)b
) << 1;
592 static inline uint16_t mipsdsp_mul_u8_u8(uint8_t a
, uint8_t b
)
594 return (uint16_t)a
* (uint16_t)b
;
597 static inline uint16_t mipsdsp_mul_u8_u16(uint8_t a
, uint16_t b
,
602 tempI
= (uint32_t)a
* (uint32_t)b
;
603 if (tempI
> 0x0000FFFF) {
605 set_DSPControl_overflow_flag(1, 21, env
);
608 return tempI
& 0x0000FFFF;
611 static inline uint64_t mipsdsp_mul_u32_u32(uint32_t a
, uint32_t b
)
613 return (uint64_t)a
* (uint64_t)b
;
616 static inline int16_t mipsdsp_rndq15_mul_q15_q15(uint16_t a
, uint16_t b
,
621 if ((a
== 0x8000) && (b
== 0x8000)) {
623 set_DSPControl_overflow_flag(1, 21, env
);
625 temp
= ((int16_t)a
* (int16_t)b
) << 1;
626 temp
= temp
+ 0x00008000;
629 return (temp
& 0xFFFF0000) >> 16;
632 static inline int32_t mipsdsp_sat16_mul_q15_q15(uint16_t a
, uint16_t b
,
637 if ((a
== 0x8000) && (b
== 0x8000)) {
639 set_DSPControl_overflow_flag(1, 21, env
);
641 temp
= (int16_t)a
* (int16_t)b
;
645 return (temp
>> 16) & 0x0000FFFF;
648 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a
,
655 * The value 0x00008000 will be added to the input Q31 value, and the code
656 * needs to check if the addition causes an overflow. Since a positive value
657 * is added, overflow can happen in one direction only.
659 if (a
> 0x7FFF7FFF) {
661 set_DSPControl_overflow_flag(1, 22, env
);
663 temp
= ((a
+ 0x8000) >> 16) & 0xFFFF;
669 static inline uint8_t mipsdsp_sat8_reduce_precision(uint16_t a
,
675 sign
= (a
>> 15) & 0x01;
680 set_DSPControl_overflow_flag(1, 22, env
);
683 return (mag
>> 7) & 0xFFFF;
686 set_DSPControl_overflow_flag(1, 22, env
);
691 static inline uint8_t mipsdsp_lshift8(uint8_t a
, uint8_t s
, CPUMIPSState
*env
)
696 discard
= a
>> (8 - s
);
698 if (discard
!= 0x00) {
699 set_DSPControl_overflow_flag(1, 22, env
);
705 static inline uint16_t mipsdsp_lshift16(uint16_t a
, uint8_t s
,
711 discard
= (int16_t)a
>> (15 - s
);
713 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
714 set_DSPControl_overflow_flag(1, 22, env
);
721 static inline uint32_t mipsdsp_lshift32(uint32_t a
, uint8_t s
,
729 discard
= (int32_t)a
>> (31 - (s
- 1));
731 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
732 set_DSPControl_overflow_flag(1, 22, env
);
738 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a
, uint8_t s
,
747 sign
= (a
>> 15) & 0x01;
749 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
750 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
752 discard
= a
>> (14 - (s
- 1));
755 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
756 set_DSPControl_overflow_flag(1, 22, env
);
757 return (sign
== 0) ? 0x7FFF : 0x8000;
764 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a
, uint8_t s
,
773 sign
= (a
>> 31) & 0x01;
775 discard
= (((0x01 << (32 - s
)) - 1) << s
) |
776 ((a
>> (30 - (s
- 1))) & ((0x01 << s
) - 1));
778 discard
= a
>> (30 - (s
- 1));
781 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
782 set_DSPControl_overflow_flag(1, 22, env
);
783 return (sign
== 0) ? 0x7FFFFFFF : 0x80000000;
790 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a
, uint8_t s
)
795 temp
= (uint32_t)a
<< 1;
797 temp
= (int32_t)(int8_t)a
>> (s
- 1);
800 return (temp
+ 1) >> 1;
803 static inline uint16_t mipsdsp_rnd16_rashift(uint16_t a
, uint8_t s
)
808 temp
= (uint32_t)a
<< 1;
810 temp
= (int32_t)(int16_t)a
>> (s
- 1);
813 return (temp
+ 1) >> 1;
816 static inline uint32_t mipsdsp_rnd32_rashift(uint32_t a
, uint8_t s
)
821 temp
= (uint64_t)a
<< 1;
823 temp
= (int64_t)(int32_t)a
>> (s
- 1);
827 return (temp
>> 1) & 0xFFFFFFFFull
;
830 static inline uint16_t mipsdsp_sub_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
835 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
836 set_DSPControl_overflow_flag(1, 20, env
);
842 static inline uint16_t mipsdsp_sat16_sub(int16_t a
, int16_t b
,
848 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
854 set_DSPControl_overflow_flag(1, 20, env
);
860 static inline uint32_t mipsdsp_sat32_sub(int32_t a
, int32_t b
,
866 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
872 set_DSPControl_overflow_flag(1, 20, env
);
875 return temp
& 0xFFFFFFFFull
;
878 static inline uint16_t mipsdsp_rshift1_sub_q16(int16_t a
, int16_t b
)
882 temp
= (int32_t)a
- (int32_t)b
;
884 return (temp
>> 1) & 0x0000FFFF;
887 static inline uint16_t mipsdsp_rrshift1_sub_q16(int16_t a
, int16_t b
)
891 temp
= (int32_t)a
- (int32_t)b
;
894 return (temp
>> 1) & 0x0000FFFF;
897 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a
, int32_t b
)
901 temp
= (int64_t)a
- (int64_t)b
;
903 return (temp
>> 1) & 0xFFFFFFFFull
;
906 static inline uint32_t mipsdsp_rrshift1_sub_q32(int32_t a
, int32_t b
)
910 temp
= (int64_t)a
- (int64_t)b
;
913 return (temp
>> 1) & 0xFFFFFFFFull
;
916 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a
, uint16_t b
,
922 temp
= (uint32_t)a
- (uint32_t)b
;
923 temp16
= (temp
>> 16) & 0x01;
925 set_DSPControl_overflow_flag(1, 20, env
);
927 return temp
& 0x0000FFFF;
930 static inline uint16_t mipsdsp_satu16_sub_u16_u16(uint16_t a
, uint16_t b
,
936 temp
= (uint32_t)a
- (uint32_t)b
;
937 temp16
= (temp
>> 16) & 0x01;
941 set_DSPControl_overflow_flag(1, 20, env
);
944 return temp
& 0x0000FFFF;
947 static inline uint8_t mipsdsp_sub_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
952 temp
= (uint16_t)a
- (uint16_t)b
;
953 temp8
= (temp
>> 8) & 0x01;
955 set_DSPControl_overflow_flag(1, 20, env
);
958 return temp
& 0x00FF;
961 static inline uint8_t mipsdsp_satu8_sub(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
966 temp
= (uint16_t)a
- (uint16_t)b
;
967 temp8
= (temp
>> 8) & 0x01;
970 set_DSPControl_overflow_flag(1, 20, env
);
973 return temp
& 0x00FF;
976 static inline uint32_t mipsdsp_sub32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
981 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
982 set_DSPControl_overflow_flag(1, 20, env
);
988 static inline int32_t mipsdsp_add_i32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
994 if (MIPSDSP_OVERFLOW_ADD(a
, b
, temp
, 0x80000000)) {
995 set_DSPControl_overflow_flag(1, 20, env
);
1001 static inline int32_t mipsdsp_cmp_eq(int32_t a
, int32_t b
)
1006 static inline int32_t mipsdsp_cmp_le(int32_t a
, int32_t b
)
1011 static inline int32_t mipsdsp_cmp_lt(int32_t a
, int32_t b
)
1016 static inline int32_t mipsdsp_cmpu_eq(uint32_t a
, uint32_t b
)
1021 static inline int32_t mipsdsp_cmpu_le(uint32_t a
, uint32_t b
)
1026 static inline int32_t mipsdsp_cmpu_lt(uint32_t a
, uint32_t b
)
1030 /*** MIPS DSP internal functions end ***/
1032 #define MIPSDSP_LHI 0xFFFFFFFF00000000ull
1033 #define MIPSDSP_LLO 0x00000000FFFFFFFFull
1034 #define MIPSDSP_HI 0xFFFF0000
1035 #define MIPSDSP_LO 0x0000FFFF
1036 #define MIPSDSP_Q3 0xFF000000
1037 #define MIPSDSP_Q2 0x00FF0000
1038 #define MIPSDSP_Q1 0x0000FF00
1039 #define MIPSDSP_Q0 0x000000FF
1041 #define MIPSDSP_SPLIT32_8(num, a, b, c, d) \
1043 a = (num >> 24) & MIPSDSP_Q0; \
1044 b = (num >> 16) & MIPSDSP_Q0; \
1045 c = (num >> 8) & MIPSDSP_Q0; \
1046 d = num & MIPSDSP_Q0; \
1049 #define MIPSDSP_SPLIT32_16(num, a, b) \
1051 a = (num >> 16) & MIPSDSP_LO; \
1052 b = num & MIPSDSP_LO; \
1055 #define MIPSDSP_RETURN32_8(a, b, c, d) ((target_long)(int32_t) \
1056 (((uint32_t)a << 24) | \
1057 (((uint32_t)b << 16) | \
1058 (((uint32_t)c << 8) | \
1059 ((uint32_t)d & 0xFF)))))
1060 #define MIPSDSP_RETURN32_16(a, b) ((target_long)(int32_t) \
1061 (((uint32_t)a << 16) | \
1062 ((uint32_t)b & 0xFFFF)))
1064 #ifdef TARGET_MIPS64
1065 #define MIPSDSP_SPLIT64_16(num, a, b, c, d) \
1067 a = (num >> 48) & MIPSDSP_LO; \
1068 b = (num >> 32) & MIPSDSP_LO; \
1069 c = (num >> 16) & MIPSDSP_LO; \
1070 d = num & MIPSDSP_LO; \
1073 #define MIPSDSP_SPLIT64_32(num, a, b) \
1075 a = (num >> 32) & MIPSDSP_LLO; \
1076 b = num & MIPSDSP_LLO; \
1079 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)a << 48) | \
1080 ((uint64_t)b << 32) | \
1081 ((uint64_t)c << 16) | \
1083 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)a << 32) | (uint64_t)b)
1086 /** DSP Arithmetic Sub-class insns **/
1087 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1088 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1095 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1096 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1099 return (target_long)dt.sw[0]; \
1101 MIPSDSP32_UNOP_ENV(absq_s_ph
, sat_abs16
, sh
)
1102 MIPSDSP32_UNOP_ENV(absq_s_qb
, sat_abs8
, sb
)
1103 MIPSDSP32_UNOP_ENV(absq_s_w
, sat_abs32
, sw
)
1104 #undef MIPSDSP32_UNOP_ENV
1106 #if defined(TARGET_MIPS64)
1107 #define MIPSDSP64_UNOP_ENV(name, func, element) \
1108 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1115 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1116 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1121 MIPSDSP64_UNOP_ENV(absq_s_ob
, sat_abs8
, sb
)
1122 MIPSDSP64_UNOP_ENV(absq_s_qh
, sat_abs16
, sh
)
1123 MIPSDSP64_UNOP_ENV(absq_s_pw
, sat_abs32
, sw
)
1124 #undef MIPSDSP64_UNOP_ENV
1127 #define MIPSDSP32_BINOP(name, func, element) \
1128 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1130 DSP32Value ds, dt; \
1136 for (i = 0; i < ARRAY_SIZE(ds.element); i++) { \
1137 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1140 return (target_long)ds.sw[0]; \
1142 MIPSDSP32_BINOP(addqh_ph
, rshift1_add_q16
, sh
);
1143 MIPSDSP32_BINOP(addqh_r_ph
, rrshift1_add_q16
, sh
);
1144 MIPSDSP32_BINOP(addqh_r_w
, rrshift1_add_q32
, sw
);
1145 MIPSDSP32_BINOP(addqh_w
, rshift1_add_q32
, sw
);
1146 MIPSDSP32_BINOP(adduh_qb
, rshift1_add_u8
, ub
);
1147 MIPSDSP32_BINOP(adduh_r_qb
, rrshift1_add_u8
, ub
);
1148 MIPSDSP32_BINOP(subqh_ph
, rshift1_sub_q16
, sh
);
1149 MIPSDSP32_BINOP(subqh_r_ph
, rrshift1_sub_q16
, sh
);
1150 MIPSDSP32_BINOP(subqh_r_w
, rrshift1_sub_q32
, sw
);
1151 MIPSDSP32_BINOP(subqh_w
, rshift1_sub_q32
, sw
);
1152 #undef MIPSDSP32_BINOP
1154 #define MIPSDSP32_BINOP_ENV(name, func, element) \
1155 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1156 CPUMIPSState *env) \
1158 DSP32Value ds, dt; \
1164 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1165 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1168 return (target_long)ds.sw[0]; \
1170 MIPSDSP32_BINOP_ENV(addq_ph
, add_i16
, sh
)
1171 MIPSDSP32_BINOP_ENV(addq_s_ph
, sat_add_i16
, sh
)
1172 MIPSDSP32_BINOP_ENV(addq_s_w
, sat_add_i32
, sw
);
1173 MIPSDSP32_BINOP_ENV(addu_ph
, add_u16
, sh
)
1174 MIPSDSP32_BINOP_ENV(addu_qb
, add_u8
, ub
);
1175 MIPSDSP32_BINOP_ENV(addu_s_ph
, sat_add_u16
, sh
)
1176 MIPSDSP32_BINOP_ENV(addu_s_qb
, sat_add_u8
, ub
);
1177 MIPSDSP32_BINOP_ENV(subq_ph
, sub_i16
, sh
);
1178 MIPSDSP32_BINOP_ENV(subq_s_ph
, sat16_sub
, sh
);
1179 MIPSDSP32_BINOP_ENV(subq_s_w
, sat32_sub
, sw
);
1180 MIPSDSP32_BINOP_ENV(subu_ph
, sub_u16_u16
, sh
);
1181 MIPSDSP32_BINOP_ENV(subu_qb
, sub_u8
, ub
);
1182 MIPSDSP32_BINOP_ENV(subu_s_ph
, satu16_sub_u16_u16
, sh
);
1183 MIPSDSP32_BINOP_ENV(subu_s_qb
, satu8_sub
, ub
);
1184 #undef MIPSDSP32_BINOP_ENV
1186 #ifdef TARGET_MIPS64
1187 #define MIPSDSP64_BINOP(name, func, element) \
1188 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1190 DSP64Value ds, dt; \
1196 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1197 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1202 MIPSDSP64_BINOP(adduh_ob
, rshift1_add_u8
, ub
);
1203 MIPSDSP64_BINOP(adduh_r_ob
, rrshift1_add_u8
, ub
);
1204 MIPSDSP64_BINOP(subuh_ob
, rshift1_sub_u8
, ub
);
1205 MIPSDSP64_BINOP(subuh_r_ob
, rrshift1_sub_u8
, ub
);
1206 #undef MIPSDSP64_BINOP
1208 #define MIPSDSP64_BINOP_ENV(name, func, element) \
1209 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1210 CPUMIPSState *env) \
1212 DSP64Value ds, dt; \
1218 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1219 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1224 MIPSDSP64_BINOP_ENV(addq_pw
, add_i32
, sw
);
1225 MIPSDSP64_BINOP_ENV(addq_qh
, add_i16
, sh
);
1226 MIPSDSP64_BINOP_ENV(addq_s_pw
, sat_add_i32
, sw
);
1227 MIPSDSP64_BINOP_ENV(addq_s_qh
, sat_add_i16
, sh
);
1228 MIPSDSP64_BINOP_ENV(addu_ob
, add_u8
, uh
);
1229 MIPSDSP64_BINOP_ENV(addu_qh
, add_u16
, uh
);
1230 MIPSDSP64_BINOP_ENV(addu_s_ob
, sat_add_u8
, uh
);
1231 MIPSDSP64_BINOP_ENV(addu_s_qh
, sat_add_u16
, uh
);
1232 MIPSDSP64_BINOP_ENV(subq_pw
, sub32
, sw
);
1233 MIPSDSP64_BINOP_ENV(subq_qh
, sub_i16
, sh
);
1234 MIPSDSP64_BINOP_ENV(subq_s_pw
, sat32_sub
, sw
);
1235 MIPSDSP64_BINOP_ENV(subq_s_qh
, sat16_sub
, sh
);
1236 MIPSDSP64_BINOP_ENV(subu_ob
, sub_u8
, uh
);
1237 MIPSDSP64_BINOP_ENV(subu_qh
, sub_u16_u16
, uh
);
1238 MIPSDSP64_BINOP_ENV(subu_s_ob
, satu8_sub
, uh
);
1239 MIPSDSP64_BINOP_ENV(subu_s_qh
, satu16_sub_u16_u16
, uh
);
1240 #undef MIPSDSP64_BINOP_ENV
1244 #define SUBUH_QB(name, var) \
1245 target_ulong helper_##name##_qb(target_ulong rs, target_ulong rt) \
1247 uint8_t rs3, rs2, rs1, rs0; \
1248 uint8_t rt3, rt2, rt1, rt0; \
1249 uint8_t tempD, tempC, tempB, tempA; \
1251 MIPSDSP_SPLIT32_8(rs, rs3, rs2, rs1, rs0); \
1252 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1254 tempD = ((uint16_t)rs3 - (uint16_t)rt3 + var) >> 1; \
1255 tempC = ((uint16_t)rs2 - (uint16_t)rt2 + var) >> 1; \
1256 tempB = ((uint16_t)rs1 - (uint16_t)rt1 + var) >> 1; \
1257 tempA = ((uint16_t)rs0 - (uint16_t)rt0 + var) >> 1; \
1259 return ((uint32_t)tempD << 24) | ((uint32_t)tempC << 16) | \
1260 ((uint32_t)tempB << 8) | ((uint32_t)tempA); \
1264 SUBUH_QB(subuh_r
, 1);
1268 target_ulong
helper_addsc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1270 uint64_t temp
, tempRs
, tempRt
;
1273 tempRs
= (uint64_t)rs
& MIPSDSP_LLO
;
1274 tempRt
= (uint64_t)rt
& MIPSDSP_LLO
;
1276 temp
= tempRs
+ tempRt
;
1277 flag
= (temp
& 0x0100000000ull
) >> 32;
1278 set_DSPControl_carryflag(flag
, env
);
1280 return (target_long
)(int32_t)(temp
& MIPSDSP_LLO
);
1283 target_ulong
helper_addwc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1286 int32_t temp32
, temp31
;
1289 tempL
= (int64_t)(int32_t)rs
+ (int64_t)(int32_t)rt
+
1290 get_DSPControl_carryflag(env
);
1291 temp31
= (tempL
>> 31) & 0x01;
1292 temp32
= (tempL
>> 32) & 0x01;
1294 if (temp31
!= temp32
) {
1295 set_DSPControl_overflow_flag(1, 20, env
);
1298 rd
= tempL
& MIPSDSP_LLO
;
1300 return (target_long
)(int32_t)rd
;
1303 target_ulong
helper_modsub(target_ulong rs
, target_ulong rt
)
1309 decr
= rt
& MIPSDSP_Q0
;
1310 lastindex
= (rt
>> 8) & MIPSDSP_LO
;
1312 if ((rs
& MIPSDSP_LLO
) == 0x00000000) {
1313 rd
= (target_ulong
)lastindex
;
1321 target_ulong
helper_raddu_w_qb(target_ulong rs
)
1323 target_ulong ret
= 0;
1328 for (i
= 0; i
< 4; i
++) {
1334 #if defined(TARGET_MIPS64)
1335 target_ulong
helper_raddu_l_ob(target_ulong rs
)
1337 target_ulong ret
= 0;
1342 for (i
= 0; i
< 8; i
++) {
1349 #define PRECR_QB_PH(name, a, b)\
1350 target_ulong helper_##name##_qb_ph(target_ulong rs, target_ulong rt) \
1352 uint8_t tempD, tempC, tempB, tempA; \
1354 tempD = (rs >> a) & MIPSDSP_Q0; \
1355 tempC = (rs >> b) & MIPSDSP_Q0; \
1356 tempB = (rt >> a) & MIPSDSP_Q0; \
1357 tempA = (rt >> b) & MIPSDSP_Q0; \
1359 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA); \
1362 PRECR_QB_PH(precr
, 16, 0);
1363 PRECR_QB_PH(precrq
, 24, 8);
1367 target_ulong
helper_precr_sra_ph_w(uint32_t sa
, target_ulong rs
,
1370 uint16_t tempB
, tempA
;
1372 tempB
= ((int32_t)rt
>> sa
) & MIPSDSP_LO
;
1373 tempA
= ((int32_t)rs
>> sa
) & MIPSDSP_LO
;
1375 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1378 target_ulong
helper_precr_sra_r_ph_w(uint32_t sa
,
1379 target_ulong rs
, target_ulong rt
)
1381 uint64_t tempB
, tempA
;
1383 /* If sa = 0, then (sa - 1) = -1 will case shift error, so we need else. */
1385 tempB
= (rt
& MIPSDSP_LO
) << 1;
1386 tempA
= (rs
& MIPSDSP_LO
) << 1;
1388 tempB
= ((int32_t)rt
>> (sa
- 1)) + 1;
1389 tempA
= ((int32_t)rs
>> (sa
- 1)) + 1;
1391 rt
= (((tempB
>> 1) & MIPSDSP_LO
) << 16) | ((tempA
>> 1) & MIPSDSP_LO
);
1393 return (target_long
)(int32_t)rt
;
1396 target_ulong
helper_precrq_ph_w(target_ulong rs
, target_ulong rt
)
1398 uint16_t tempB
, tempA
;
1400 tempB
= (rs
& MIPSDSP_HI
) >> 16;
1401 tempA
= (rt
& MIPSDSP_HI
) >> 16;
1403 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1406 target_ulong
helper_precrq_rs_ph_w(target_ulong rs
, target_ulong rt
,
1409 uint16_t tempB
, tempA
;
1411 tempB
= mipsdsp_trunc16_sat16_round(rs
, env
);
1412 tempA
= mipsdsp_trunc16_sat16_round(rt
, env
);
1414 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1417 #if defined(TARGET_MIPS64)
1418 target_ulong
helper_precr_ob_qh(target_ulong rs
, target_ulong rt
)
1420 uint8_t rs6
, rs4
, rs2
, rs0
;
1421 uint8_t rt6
, rt4
, rt2
, rt0
;
1424 rs6
= (rs
>> 48) & MIPSDSP_Q0
;
1425 rs4
= (rs
>> 32) & MIPSDSP_Q0
;
1426 rs2
= (rs
>> 16) & MIPSDSP_Q0
;
1427 rs0
= rs
& MIPSDSP_Q0
;
1428 rt6
= (rt
>> 48) & MIPSDSP_Q0
;
1429 rt4
= (rt
>> 32) & MIPSDSP_Q0
;
1430 rt2
= (rt
>> 16) & MIPSDSP_Q0
;
1431 rt0
= rt
& MIPSDSP_Q0
;
1433 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1434 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1435 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1436 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1441 #define PRECR_QH_PW(name, var) \
1442 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, target_ulong rt, \
1445 uint16_t rs3, rs2, rs1, rs0; \
1446 uint16_t rt3, rt2, rt1, rt0; \
1447 uint16_t tempD, tempC, tempB, tempA; \
1449 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
1450 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1452 /* When sa = 0, we use rt2, rt0, rs2, rs0; \
1453 * when sa != 0, we use rt3, rt1, rs3, rs1. */ \
1455 tempD = rt2 << var; \
1456 tempC = rt0 << var; \
1457 tempB = rs2 << var; \
1458 tempA = rs0 << var; \
1460 tempD = (((int16_t)rt3 >> sa) + var) >> var; \
1461 tempC = (((int16_t)rt1 >> sa) + var) >> var; \
1462 tempB = (((int16_t)rs3 >> sa) + var) >> var; \
1463 tempA = (((int16_t)rs1 >> sa) + var) >> var; \
1466 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1469 PRECR_QH_PW(sra
, 0);
1470 PRECR_QH_PW(sra_r
, 1);
1474 target_ulong
helper_precrq_ob_qh(target_ulong rs
, target_ulong rt
)
1476 uint8_t rs6
, rs4
, rs2
, rs0
;
1477 uint8_t rt6
, rt4
, rt2
, rt0
;
1480 rs6
= (rs
>> 56) & MIPSDSP_Q0
;
1481 rs4
= (rs
>> 40) & MIPSDSP_Q0
;
1482 rs2
= (rs
>> 24) & MIPSDSP_Q0
;
1483 rs0
= (rs
>> 8) & MIPSDSP_Q0
;
1484 rt6
= (rt
>> 56) & MIPSDSP_Q0
;
1485 rt4
= (rt
>> 40) & MIPSDSP_Q0
;
1486 rt2
= (rt
>> 24) & MIPSDSP_Q0
;
1487 rt0
= (rt
>> 8) & MIPSDSP_Q0
;
1489 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1490 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1491 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1492 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1497 target_ulong
helper_precrq_qh_pw(target_ulong rs
, target_ulong rt
)
1499 uint16_t tempD
, tempC
, tempB
, tempA
;
1501 tempD
= (rs
>> 48) & MIPSDSP_LO
;
1502 tempC
= (rs
>> 16) & MIPSDSP_LO
;
1503 tempB
= (rt
>> 48) & MIPSDSP_LO
;
1504 tempA
= (rt
>> 16) & MIPSDSP_LO
;
1506 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1509 target_ulong
helper_precrq_rs_qh_pw(target_ulong rs
, target_ulong rt
,
1514 uint16_t tempD
, tempC
, tempB
, tempA
;
1516 rs2
= (rs
>> 32) & MIPSDSP_LLO
;
1517 rs0
= rs
& MIPSDSP_LLO
;
1518 rt2
= (rt
>> 32) & MIPSDSP_LLO
;
1519 rt0
= rt
& MIPSDSP_LLO
;
1521 tempD
= mipsdsp_trunc16_sat16_round(rs2
, env
);
1522 tempC
= mipsdsp_trunc16_sat16_round(rs0
, env
);
1523 tempB
= mipsdsp_trunc16_sat16_round(rt2
, env
);
1524 tempA
= mipsdsp_trunc16_sat16_round(rt0
, env
);
1526 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1529 target_ulong
helper_precrq_pw_l(target_ulong rs
, target_ulong rt
)
1531 uint32_t tempB
, tempA
;
1533 tempB
= (rs
>> 32) & MIPSDSP_LLO
;
1534 tempA
= (rt
>> 32) & MIPSDSP_LLO
;
1536 return MIPSDSP_RETURN64_32(tempB
, tempA
);
1540 target_ulong
helper_precrqu_s_qb_ph(target_ulong rs
, target_ulong rt
,
1543 uint8_t tempD
, tempC
, tempB
, tempA
;
1544 uint16_t rsh
, rsl
, rth
, rtl
;
1546 rsh
= (rs
& MIPSDSP_HI
) >> 16;
1547 rsl
= rs
& MIPSDSP_LO
;
1548 rth
= (rt
& MIPSDSP_HI
) >> 16;
1549 rtl
= rt
& MIPSDSP_LO
;
1551 tempD
= mipsdsp_sat8_reduce_precision(rsh
, env
);
1552 tempC
= mipsdsp_sat8_reduce_precision(rsl
, env
);
1553 tempB
= mipsdsp_sat8_reduce_precision(rth
, env
);
1554 tempA
= mipsdsp_sat8_reduce_precision(rtl
, env
);
1556 return MIPSDSP_RETURN32_8(tempD
, tempC
, tempB
, tempA
);
1559 #if defined(TARGET_MIPS64)
1560 target_ulong
helper_precrqu_s_ob_qh(target_ulong rs
, target_ulong rt
,
1564 uint16_t rs3
, rs2
, rs1
, rs0
;
1565 uint16_t rt3
, rt2
, rt1
, rt0
;
1571 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
1572 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
1574 temp
[7] = mipsdsp_sat8_reduce_precision(rs3
, env
);
1575 temp
[6] = mipsdsp_sat8_reduce_precision(rs2
, env
);
1576 temp
[5] = mipsdsp_sat8_reduce_precision(rs1
, env
);
1577 temp
[4] = mipsdsp_sat8_reduce_precision(rs0
, env
);
1578 temp
[3] = mipsdsp_sat8_reduce_precision(rt3
, env
);
1579 temp
[2] = mipsdsp_sat8_reduce_precision(rt2
, env
);
1580 temp
[1] = mipsdsp_sat8_reduce_precision(rt1
, env
);
1581 temp
[0] = mipsdsp_sat8_reduce_precision(rt0
, env
);
1583 for (i
= 0; i
< 8; i
++) {
1584 result
|= (uint64_t)temp
[i
] << (8 * i
);
1590 #define PRECEQ_PW(name, a, b) \
1591 target_ulong helper_preceq_pw_##name(target_ulong rt) \
1593 uint16_t tempB, tempA; \
1594 uint32_t tempBI, tempAI; \
1596 tempB = (rt >> a) & MIPSDSP_LO; \
1597 tempA = (rt >> b) & MIPSDSP_LO; \
1599 tempBI = (uint32_t)tempB << 16; \
1600 tempAI = (uint32_t)tempA << 16; \
1602 return MIPSDSP_RETURN64_32(tempBI, tempAI); \
1605 PRECEQ_PW(qhl
, 48, 32);
1606 PRECEQ_PW(qhr
, 16, 0);
1607 PRECEQ_PW(qhla
, 48, 16);
1608 PRECEQ_PW(qhra
, 32, 0);
1614 #define PRECEQU_PH(name, a, b) \
1615 target_ulong helper_precequ_ph_##name(target_ulong rt) \
1617 uint16_t tempB, tempA; \
1619 tempB = (rt >> a) & MIPSDSP_Q0; \
1620 tempA = (rt >> b) & MIPSDSP_Q0; \
1622 tempB = tempB << 7; \
1623 tempA = tempA << 7; \
1625 return MIPSDSP_RETURN32_16(tempB, tempA); \
1628 PRECEQU_PH(qbl
, 24, 16);
1629 PRECEQU_PH(qbr
, 8, 0);
1630 PRECEQU_PH(qbla
, 24, 8);
1631 PRECEQU_PH(qbra
, 16, 0);
1635 #if defined(TARGET_MIPS64)
1636 #define PRECEQU_QH(name, a, b, c, d) \
1637 target_ulong helper_precequ_qh_##name(target_ulong rt) \
1639 uint16_t tempD, tempC, tempB, tempA; \
1641 tempD = (rt >> a) & MIPSDSP_Q0; \
1642 tempC = (rt >> b) & MIPSDSP_Q0; \
1643 tempB = (rt >> c) & MIPSDSP_Q0; \
1644 tempA = (rt >> d) & MIPSDSP_Q0; \
1646 tempD = tempD << 7; \
1647 tempC = tempC << 7; \
1648 tempB = tempB << 7; \
1649 tempA = tempA << 7; \
1651 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1654 PRECEQU_QH(obl
, 56, 48, 40, 32);
1655 PRECEQU_QH(obr
, 24, 16, 8, 0);
1656 PRECEQU_QH(obla
, 56, 40, 24, 8);
1657 PRECEQU_QH(obra
, 48, 32, 16, 0);
1663 #define PRECEU_PH(name, a, b) \
1664 target_ulong helper_preceu_ph_##name(target_ulong rt) \
1666 uint16_t tempB, tempA; \
1668 tempB = (rt >> a) & MIPSDSP_Q0; \
1669 tempA = (rt >> b) & MIPSDSP_Q0; \
1671 return MIPSDSP_RETURN32_16(tempB, tempA); \
1674 PRECEU_PH(qbl
, 24, 16);
1675 PRECEU_PH(qbr
, 8, 0);
1676 PRECEU_PH(qbla
, 24, 8);
1677 PRECEU_PH(qbra
, 16, 0);
1681 #if defined(TARGET_MIPS64)
1682 #define PRECEU_QH(name, a, b, c, d) \
1683 target_ulong helper_preceu_qh_##name(target_ulong rt) \
1685 uint16_t tempD, tempC, tempB, tempA; \
1687 tempD = (rt >> a) & MIPSDSP_Q0; \
1688 tempC = (rt >> b) & MIPSDSP_Q0; \
1689 tempB = (rt >> c) & MIPSDSP_Q0; \
1690 tempA = (rt >> d) & MIPSDSP_Q0; \
1692 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1695 PRECEU_QH(obl
, 56, 48, 40, 32);
1696 PRECEU_QH(obr
, 24, 16, 8, 0);
1697 PRECEU_QH(obla
, 56, 40, 24, 8);
1698 PRECEU_QH(obra
, 48, 32, 16, 0);
1704 /** DSP GPR-Based Shift Sub-class insns **/
1705 #define SHIFT_QB(name, func) \
1706 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt) \
1708 uint8_t rt3, rt2, rt1, rt0; \
1712 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1714 rt3 = mipsdsp_##func(rt3, sa); \
1715 rt2 = mipsdsp_##func(rt2, sa); \
1716 rt1 = mipsdsp_##func(rt1, sa); \
1717 rt0 = mipsdsp_##func(rt0, sa); \
1719 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1722 #define SHIFT_QB_ENV(name, func) \
1723 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt,\
1724 CPUMIPSState *env) \
1726 uint8_t rt3, rt2, rt1, rt0; \
1730 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1732 rt3 = mipsdsp_##func(rt3, sa, env); \
1733 rt2 = mipsdsp_##func(rt2, sa, env); \
1734 rt1 = mipsdsp_##func(rt1, sa, env); \
1735 rt0 = mipsdsp_##func(rt0, sa, env); \
1737 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1740 SHIFT_QB_ENV(shll
, lshift8
);
1741 SHIFT_QB(shrl
, rshift_u8
);
1743 SHIFT_QB(shra
, rashift8
);
1744 SHIFT_QB(shra_r
, rnd8_rashift
);
1749 #if defined(TARGET_MIPS64)
1750 #define SHIFT_OB(name, func) \
1751 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1760 for (i = 0; i < 8; i++) { \
1761 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1762 rt_t[i] = mipsdsp_##func(rt_t[i], sa); \
1763 temp |= (uint64_t)rt_t[i] << (8 * i); \
1769 #define SHIFT_OB_ENV(name, func) \
1770 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1771 CPUMIPSState *env) \
1780 for (i = 0; i < 8; i++) { \
1781 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1782 rt_t[i] = mipsdsp_##func(rt_t[i], sa, env); \
1783 temp |= (uint64_t)rt_t[i] << (8 * i); \
1789 SHIFT_OB_ENV(shll
, lshift8
);
1790 SHIFT_OB(shrl
, rshift_u8
);
1792 SHIFT_OB(shra
, rashift8
);
1793 SHIFT_OB(shra_r
, rnd8_rashift
);
1800 #define SHIFT_PH(name, func) \
1801 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt, \
1802 CPUMIPSState *env) \
1804 uint16_t rth, rtl; \
1808 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1810 rth = mipsdsp_##func(rth, sa, env); \
1811 rtl = mipsdsp_##func(rtl, sa, env); \
1813 return MIPSDSP_RETURN32_16(rth, rtl); \
1816 SHIFT_PH(shll
, lshift16
);
1817 SHIFT_PH(shll_s
, sat16_lshift
);
1821 #if defined(TARGET_MIPS64)
1822 #define SHIFT_QH(name, func) \
1823 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa) \
1825 uint16_t rt3, rt2, rt1, rt0; \
1829 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1831 rt3 = mipsdsp_##func(rt3, sa); \
1832 rt2 = mipsdsp_##func(rt2, sa); \
1833 rt1 = mipsdsp_##func(rt1, sa); \
1834 rt0 = mipsdsp_##func(rt0, sa); \
1836 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1839 #define SHIFT_QH_ENV(name, func) \
1840 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa, \
1841 CPUMIPSState *env) \
1843 uint16_t rt3, rt2, rt1, rt0; \
1847 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1849 rt3 = mipsdsp_##func(rt3, sa, env); \
1850 rt2 = mipsdsp_##func(rt2, sa, env); \
1851 rt1 = mipsdsp_##func(rt1, sa, env); \
1852 rt0 = mipsdsp_##func(rt0, sa, env); \
1854 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1857 SHIFT_QH_ENV(shll
, lshift16
);
1858 SHIFT_QH_ENV(shll_s
, sat16_lshift
);
1860 SHIFT_QH(shrl
, rshift_u16
);
1861 SHIFT_QH(shra
, rashift16
);
1862 SHIFT_QH(shra_r
, rnd16_rashift
);
1869 #define SHIFT_W(name, func) \
1870 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1875 temp = mipsdsp_##func(rt, sa); \
1877 return (target_long)(int32_t)temp; \
1880 #define SHIFT_W_ENV(name, func) \
1881 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt, \
1882 CPUMIPSState *env) \
1887 temp = mipsdsp_##func(rt, sa, env); \
1889 return (target_long)(int32_t)temp; \
1892 SHIFT_W_ENV(shll_s
, sat32_lshift
);
1893 SHIFT_W(shra_r
, rnd32_rashift
);
1898 #if defined(TARGET_MIPS64)
1899 #define SHIFT_PW(name, func) \
1900 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa) \
1902 uint32_t rt1, rt0; \
1905 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1907 rt1 = mipsdsp_##func(rt1, sa); \
1908 rt0 = mipsdsp_##func(rt0, sa); \
1910 return MIPSDSP_RETURN64_32(rt1, rt0); \
1913 #define SHIFT_PW_ENV(name, func) \
1914 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa, \
1915 CPUMIPSState *env) \
1917 uint32_t rt1, rt0; \
1920 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1922 rt1 = mipsdsp_##func(rt1, sa, env); \
1923 rt0 = mipsdsp_##func(rt0, sa, env); \
1925 return MIPSDSP_RETURN64_32(rt1, rt0); \
1928 SHIFT_PW_ENV(shll
, lshift32
);
1929 SHIFT_PW_ENV(shll_s
, sat32_lshift
);
1931 SHIFT_PW(shra
, rashift32
);
1932 SHIFT_PW(shra_r
, rnd32_rashift
);
1939 #define SHIFT_PH(name, func) \
1940 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1942 uint16_t rth, rtl; \
1946 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1948 rth = mipsdsp_##func(rth, sa); \
1949 rtl = mipsdsp_##func(rtl, sa); \
1951 return MIPSDSP_RETURN32_16(rth, rtl); \
1954 SHIFT_PH(shrl
, rshift_u16
);
1955 SHIFT_PH(shra
, rashift16
);
1956 SHIFT_PH(shra_r
, rnd16_rashift
);
1960 /** DSP Multiply Sub-class insns **/
1961 /* Return value made up by two 16bits value.
1962 * FIXME give the macro a better name.
1964 #define MUL_RETURN32_16_PH(name, func, \
1965 rsmov1, rsmov2, rsfilter, \
1966 rtmov1, rtmov2, rtfilter) \
1967 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1968 CPUMIPSState *env) \
1970 uint16_t rsB, rsA, rtB, rtA; \
1972 rsB = (rs >> rsmov1) & rsfilter; \
1973 rsA = (rs >> rsmov2) & rsfilter; \
1974 rtB = (rt >> rtmov1) & rtfilter; \
1975 rtA = (rt >> rtmov2) & rtfilter; \
1977 rsB = mipsdsp_##func(rsB, rtB, env); \
1978 rsA = mipsdsp_##func(rsA, rtA, env); \
1980 return MIPSDSP_RETURN32_16(rsB, rsA); \
1983 MUL_RETURN32_16_PH(muleu_s_ph_qbl
, mul_u8_u16
, \
1984 24, 16, MIPSDSP_Q0
, \
1986 MUL_RETURN32_16_PH(muleu_s_ph_qbr
, mul_u8_u16
, \
1989 MUL_RETURN32_16_PH(mulq_rs_ph
, rndq15_mul_q15_q15
, \
1990 16, 0, MIPSDSP_LO
, \
1992 MUL_RETURN32_16_PH(mul_ph
, mul_i16_i16
, \
1993 16, 0, MIPSDSP_LO
, \
1995 MUL_RETURN32_16_PH(mul_s_ph
, sat16_mul_i16_i16
, \
1996 16, 0, MIPSDSP_LO
, \
1998 MUL_RETURN32_16_PH(mulq_s_ph
, sat16_mul_q15_q15
, \
1999 16, 0, MIPSDSP_LO
, \
2002 #undef MUL_RETURN32_16_PH
2004 #define MUL_RETURN32_32_ph(name, func, movbits) \
2005 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2006 CPUMIPSState *env) \
2011 rsh = (rs >> movbits) & MIPSDSP_LO; \
2012 rth = (rt >> movbits) & MIPSDSP_LO; \
2013 temp = mipsdsp_##func(rsh, rth, env); \
2015 return (target_long)(int32_t)temp; \
2018 MUL_RETURN32_32_ph(muleq_s_w_phl
, mul_q15_q15_overflowflag21
, 16);
2019 MUL_RETURN32_32_ph(muleq_s_w_phr
, mul_q15_q15_overflowflag21
, 0);
2021 #undef MUL_RETURN32_32_ph
2023 #define MUL_VOID_PH(name, use_ac_env) \
2024 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2025 CPUMIPSState *env) \
2027 int16_t rsh, rsl, rth, rtl; \
2028 int32_t tempB, tempA; \
2029 int64_t acc, dotp; \
2031 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2032 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2034 if (use_ac_env == 1) { \
2035 tempB = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2036 tempA = mipsdsp_mul_q15_q15(ac, rsl, rtl, env); \
2038 tempB = mipsdsp_mul_u16_u16(rsh, rth); \
2039 tempA = mipsdsp_mul_u16_u16(rsl, rtl); \
2042 dotp = (int64_t)tempB - (int64_t)tempA; \
2043 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2044 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2045 dotp = dotp + acc; \
2046 env->active_tc.HI[ac] = (target_long)(int32_t) \
2047 ((dotp & MIPSDSP_LHI) >> 32); \
2048 env->active_tc.LO[ac] = (target_long)(int32_t)(dotp & MIPSDSP_LLO); \
2051 MUL_VOID_PH(mulsaq_s_w_ph
, 1);
2052 MUL_VOID_PH(mulsa_w_ph
, 0);
2056 #if defined(TARGET_MIPS64)
2057 #define MUL_RETURN64_16_QH(name, func, \
2058 rsmov1, rsmov2, rsmov3, rsmov4, rsfilter, \
2059 rtmov1, rtmov2, rtmov3, rtmov4, rtfilter) \
2060 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2061 CPUMIPSState *env) \
2063 uint16_t rs3, rs2, rs1, rs0; \
2064 uint16_t rt3, rt2, rt1, rt0; \
2065 uint16_t tempD, tempC, tempB, tempA; \
2067 rs3 = (rs >> rsmov1) & rsfilter; \
2068 rs2 = (rs >> rsmov2) & rsfilter; \
2069 rs1 = (rs >> rsmov3) & rsfilter; \
2070 rs0 = (rs >> rsmov4) & rsfilter; \
2071 rt3 = (rt >> rtmov1) & rtfilter; \
2072 rt2 = (rt >> rtmov2) & rtfilter; \
2073 rt1 = (rt >> rtmov3) & rtfilter; \
2074 rt0 = (rt >> rtmov4) & rtfilter; \
2076 tempD = mipsdsp_##func(rs3, rt3, env); \
2077 tempC = mipsdsp_##func(rs2, rt2, env); \
2078 tempB = mipsdsp_##func(rs1, rt1, env); \
2079 tempA = mipsdsp_##func(rs0, rt0, env); \
2081 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
2084 MUL_RETURN64_16_QH(muleu_s_qh_obl
, mul_u8_u16
, \
2085 56, 48, 40, 32, MIPSDSP_Q0
, \
2086 48, 32, 16, 0, MIPSDSP_LO
);
2087 MUL_RETURN64_16_QH(muleu_s_qh_obr
, mul_u8_u16
, \
2088 24, 16, 8, 0, MIPSDSP_Q0
, \
2089 48, 32, 16, 0, MIPSDSP_LO
);
2090 MUL_RETURN64_16_QH(mulq_rs_qh
, rndq15_mul_q15_q15
, \
2091 48, 32, 16, 0, MIPSDSP_LO
, \
2092 48, 32, 16, 0, MIPSDSP_LO
);
2094 #undef MUL_RETURN64_16_QH
2096 #define MUL_RETURN64_32_QH(name, \
2099 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2100 CPUMIPSState *env) \
2102 uint16_t rsB, rsA; \
2103 uint16_t rtB, rtA; \
2104 uint32_t tempB, tempA; \
2106 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2107 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2108 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2109 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2111 tempB = mipsdsp_mul_q15_q15(5, rsB, rtB, env); \
2112 tempA = mipsdsp_mul_q15_q15(5, rsA, rtA, env); \
2114 return ((uint64_t)tempB << 32) | (uint64_t)tempA; \
2117 MUL_RETURN64_32_QH(muleq_s_pw_qhl
, 48, 32, 48, 32);
2118 MUL_RETURN64_32_QH(muleq_s_pw_qhr
, 16, 0, 16, 0);
2120 #undef MUL_RETURN64_32_QH
2122 void helper_mulsaq_s_w_qh(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2125 int16_t rs3
, rs2
, rs1
, rs0
;
2126 int16_t rt3
, rt2
, rt1
, rt0
;
2127 int32_t tempD
, tempC
, tempB
, tempA
;
2132 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
2133 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
2135 tempD
= mipsdsp_mul_q15_q15(ac
, rs3
, rt3
, env
);
2136 tempC
= mipsdsp_mul_q15_q15(ac
, rs2
, rt2
, env
);
2137 tempB
= mipsdsp_mul_q15_q15(ac
, rs1
, rt1
, env
);
2138 tempA
= mipsdsp_mul_q15_q15(ac
, rs0
, rt0
, env
);
2140 temp
[0] = ((int32_t)tempD
- (int32_t)tempC
) +
2141 ((int32_t)tempB
- (int32_t)tempA
);
2142 temp
[0] = (int64_t)(temp
[0] << 30) >> 30;
2143 if (((temp
[0] >> 33) & 0x01) == 0) {
2149 acc
[0] = env
->active_tc
.LO
[ac
];
2150 acc
[1] = env
->active_tc
.HI
[ac
];
2152 temp_sum
= acc
[0] + temp
[0];
2153 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2154 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2160 env
->active_tc
.HI
[ac
] = acc
[1];
2161 env
->active_tc
.LO
[ac
] = acc
[0];
2165 #define DP_QB(name, func, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2166 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2167 CPUMIPSState *env) \
2171 uint16_t tempB, tempA; \
2172 uint64_t tempC, dotp; \
2174 rs3 = (rs >> rsmov1) & MIPSDSP_Q0; \
2175 rs2 = (rs >> rsmov2) & MIPSDSP_Q0; \
2176 rt3 = (rt >> rtmov1) & MIPSDSP_Q0; \
2177 rt2 = (rt >> rtmov2) & MIPSDSP_Q0; \
2178 tempB = mipsdsp_##func(rs3, rt3); \
2179 tempA = mipsdsp_##func(rs2, rt2); \
2180 dotp = (int64_t)tempB + (int64_t)tempA; \
2182 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2183 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2186 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2187 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2191 env->active_tc.HI[ac] = (target_long)(int32_t) \
2192 ((tempC & MIPSDSP_LHI) >> 32); \
2193 env->active_tc.LO[ac] = (target_long)(int32_t)(tempC & MIPSDSP_LLO); \
2196 DP_QB(dpau_h_qbl
, mul_u8_u8
, 1, 24, 16, 24, 16);
2197 DP_QB(dpau_h_qbr
, mul_u8_u8
, 1, 8, 0, 8, 0);
2198 DP_QB(dpsu_h_qbl
, mul_u8_u8
, 0, 24, 16, 24, 16);
2199 DP_QB(dpsu_h_qbr
, mul_u8_u8
, 0, 8, 0, 8, 0);
2203 #if defined(TARGET_MIPS64)
2204 #define DP_OB(name, add_sub, \
2205 rsmov1, rsmov2, rsmov3, rsmov4, \
2206 rtmov1, rtmov2, rtmov3, rtmov4) \
2207 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2208 CPUMIPSState *env) \
2210 uint8_t rsD, rsC, rsB, rsA; \
2211 uint8_t rtD, rtC, rtB, rtA; \
2212 uint16_t tempD, tempC, tempB, tempA; \
2215 uint64_t temp_sum; \
2220 rsD = (rs >> rsmov1) & MIPSDSP_Q0; \
2221 rsC = (rs >> rsmov2) & MIPSDSP_Q0; \
2222 rsB = (rs >> rsmov3) & MIPSDSP_Q0; \
2223 rsA = (rs >> rsmov4) & MIPSDSP_Q0; \
2224 rtD = (rt >> rtmov1) & MIPSDSP_Q0; \
2225 rtC = (rt >> rtmov2) & MIPSDSP_Q0; \
2226 rtB = (rt >> rtmov3) & MIPSDSP_Q0; \
2227 rtA = (rt >> rtmov4) & MIPSDSP_Q0; \
2229 tempD = mipsdsp_mul_u8_u8(rsD, rtD); \
2230 tempC = mipsdsp_mul_u8_u8(rsC, rtC); \
2231 tempB = mipsdsp_mul_u8_u8(rsB, rtB); \
2232 tempA = mipsdsp_mul_u8_u8(rsA, rtA); \
2234 temp[0] = (uint64_t)tempD + (uint64_t)tempC + \
2235 (uint64_t)tempB + (uint64_t)tempA; \
2237 acc[0] = env->active_tc.LO[ac]; \
2238 acc[1] = env->active_tc.HI[ac]; \
2241 temp_sum = acc[0] + temp[0]; \
2242 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2243 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2246 temp[0] = temp_sum; \
2247 temp[1] = acc[1] + temp[1]; \
2249 temp_sum = acc[0] - temp[0]; \
2250 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2253 temp[0] = temp_sum; \
2254 temp[1] = acc[1] - temp[1]; \
2257 env->active_tc.HI[ac] = temp[1]; \
2258 env->active_tc.LO[ac] = temp[0]; \
2261 DP_OB(dpau_h_obl
, 1, 56, 48, 40, 32, 56, 48, 40, 32);
2262 DP_OB(dpau_h_obr
, 1, 24, 16, 8, 0, 24, 16, 8, 0);
2263 DP_OB(dpsu_h_obl
, 0, 56, 48, 40, 32, 56, 48, 40, 32);
2264 DP_OB(dpsu_h_obr
, 0, 24, 16, 8, 0, 24, 16, 8, 0);
2269 #define DP_NOFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2270 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2271 CPUMIPSState *env) \
2273 int16_t rsB, rsA, rtB, rtA; \
2274 int32_t tempA, tempB; \
2277 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2278 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2279 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2280 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2282 tempB = (int32_t)rsB * (int32_t)rtB; \
2283 tempA = (int32_t)rsA * (int32_t)rtA; \
2285 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2286 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2289 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2291 acc = acc - ((int64_t)tempB + (int64_t)tempA); \
2294 env->active_tc.HI[ac] = (target_long)(int32_t)((acc & MIPSDSP_LHI) >> 32); \
2295 env->active_tc.LO[ac] = (target_long)(int32_t)(acc & MIPSDSP_LLO); \
2298 DP_NOFUNC_PH(dpa_w_ph
, 1, 16, 0, 16, 0);
2299 DP_NOFUNC_PH(dpax_w_ph
, 1, 16, 0, 0, 16);
2300 DP_NOFUNC_PH(dps_w_ph
, 0, 16, 0, 16, 0);
2301 DP_NOFUNC_PH(dpsx_w_ph
, 0, 16, 0, 0, 16);
2304 #define DP_HASFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2305 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2306 CPUMIPSState *env) \
2308 int16_t rsB, rsA, rtB, rtA; \
2309 int32_t tempB, tempA; \
2310 int64_t acc, dotp; \
2312 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2313 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2314 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2315 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2317 tempB = mipsdsp_mul_q15_q15(ac, rsB, rtB, env); \
2318 tempA = mipsdsp_mul_q15_q15(ac, rsA, rtA, env); \
2320 dotp = (int64_t)tempB + (int64_t)tempA; \
2321 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2322 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2330 env->active_tc.HI[ac] = (target_long)(int32_t) \
2331 ((acc & MIPSDSP_LHI) >> 32); \
2332 env->active_tc.LO[ac] = (target_long)(int32_t) \
2333 (acc & MIPSDSP_LLO); \
2336 DP_HASFUNC_PH(dpaq_s_w_ph
, 1, 16, 0, 16, 0);
2337 DP_HASFUNC_PH(dpaqx_s_w_ph
, 1, 16, 0, 0, 16);
2338 DP_HASFUNC_PH(dpsq_s_w_ph
, 0, 16, 0, 16, 0);
2339 DP_HASFUNC_PH(dpsqx_s_w_ph
, 0, 16, 0, 0, 16);
2341 #undef DP_HASFUNC_PH
2343 #define DP_128OPERATION_PH(name, is_add) \
2344 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2345 CPUMIPSState *env) \
2347 int16_t rsh, rsl, rth, rtl; \
2348 int32_t tempB, tempA, tempC62_31, tempC63; \
2349 int64_t acc, dotp, tempC; \
2351 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2352 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2354 tempB = mipsdsp_mul_q15_q15(ac, rsh, rtl, env); \
2355 tempA = mipsdsp_mul_q15_q15(ac, rsl, rth, env); \
2357 dotp = (int64_t)tempB + (int64_t)tempA; \
2358 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2359 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2361 tempC = acc + dotp; \
2363 tempC = acc - dotp; \
2365 tempC63 = (tempC >> 63) & 0x01; \
2366 tempC62_31 = (tempC >> 31) & 0xFFFFFFFF; \
2368 if ((tempC63 == 0) && (tempC62_31 != 0x00000000)) { \
2369 tempC = 0x7FFFFFFF; \
2370 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2373 if ((tempC63 == 1) && (tempC62_31 != 0xFFFFFFFF)) { \
2374 tempC = (int64_t)(int32_t)0x80000000; \
2375 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2378 env->active_tc.HI[ac] = (target_long)(int32_t) \
2379 ((tempC & MIPSDSP_LHI) >> 32); \
2380 env->active_tc.LO[ac] = (target_long)(int32_t) \
2381 (tempC & MIPSDSP_LLO); \
2384 DP_128OPERATION_PH(dpaqx_sa_w_ph
, 1);
2385 DP_128OPERATION_PH(dpsqx_sa_w_ph
, 0);
2387 #undef DP_128OPERATION_HP
2389 #if defined(TARGET_MIPS64)
2390 #define DP_QH(name, is_add, use_ac_env) \
2391 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2392 CPUMIPSState *env) \
2394 int32_t rs3, rs2, rs1, rs0; \
2395 int32_t rt3, rt2, rt1, rt0; \
2396 int32_t tempD, tempC, tempB, tempA; \
2401 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2402 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2405 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env); \
2406 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env); \
2407 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env); \
2408 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env); \
2410 tempD = mipsdsp_mul_u16_u16(rs3, rt3); \
2411 tempC = mipsdsp_mul_u16_u16(rs2, rt2); \
2412 tempB = mipsdsp_mul_u16_u16(rs1, rt1); \
2413 tempA = mipsdsp_mul_u16_u16(rs0, rt0); \
2416 temp[0] = (int64_t)tempD + (int64_t)tempC + \
2417 (int64_t)tempB + (int64_t)tempA; \
2419 if (temp[0] >= 0) { \
2425 acc[1] = env->active_tc.HI[ac]; \
2426 acc[0] = env->active_tc.LO[ac]; \
2429 temp_sum = acc[0] + temp[0]; \
2430 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2431 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2432 acc[1] = acc[1] + 1; \
2434 temp[0] = temp_sum; \
2435 temp[1] = acc[1] + temp[1]; \
2437 temp_sum = acc[0] - temp[0]; \
2438 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2439 acc[1] = acc[1] - 1; \
2441 temp[0] = temp_sum; \
2442 temp[1] = acc[1] - temp[1]; \
2445 env->active_tc.HI[ac] = temp[1]; \
2446 env->active_tc.LO[ac] = temp[0]; \
2449 DP_QH(dpa_w_qh
, 1, 0);
2450 DP_QH(dpaq_s_w_qh
, 1, 1);
2451 DP_QH(dps_w_qh
, 0, 0);
2452 DP_QH(dpsq_s_w_qh
, 0, 1);
2458 #define DP_L_W(name, is_add) \
2459 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2460 CPUMIPSState *env) \
2463 int64_t dotp, acc; \
2467 dotp = mipsdsp_mul_q31_q31(ac, rs, rt, env); \
2468 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2469 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2471 temp = acc + dotp; \
2472 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2473 temp, (0x01ull << 63)); \
2475 temp = acc - dotp; \
2476 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2477 temp, (0x01ull << 63)); \
2481 temp63 = (temp >> 63) & 0x01; \
2482 if (temp63 == 1) { \
2483 temp = (0x01ull << 63) - 1; \
2485 temp = 0x01ull << 63; \
2488 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2491 env->active_tc.HI[ac] = (target_long)(int32_t) \
2492 ((temp & MIPSDSP_LHI) >> 32); \
2493 env->active_tc.LO[ac] = (target_long)(int32_t) \
2494 (temp & MIPSDSP_LLO); \
2497 DP_L_W(dpaq_sa_l_w
, 1);
2498 DP_L_W(dpsq_sa_l_w
, 0);
2502 #if defined(TARGET_MIPS64)
2503 #define DP_L_PW(name, func) \
2504 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2505 CPUMIPSState *env) \
2509 int64_t tempB[2], tempA[2]; \
2517 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2518 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2520 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env); \
2521 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env); \
2523 if (tempB[0] >= 0) { \
2529 if (tempA[0] >= 0) { \
2535 temp_sum = tempB[0] + tempA[0]; \
2536 if (((uint64_t)temp_sum < (uint64_t)tempB[0]) && \
2537 ((uint64_t)temp_sum < (uint64_t)tempA[0])) { \
2540 temp[0] = temp_sum; \
2541 temp[1] += tempB[1] + tempA[1]; \
2543 mipsdsp_##func(acc, ac, temp, env); \
2545 env->active_tc.HI[ac] = acc[1]; \
2546 env->active_tc.LO[ac] = acc[0]; \
2549 DP_L_PW(dpaq_sa_l_pw
, sat64_acc_add_q63
);
2550 DP_L_PW(dpsq_sa_l_pw
, sat64_acc_sub_q63
);
2554 void helper_mulsaq_s_l_pw(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2559 int64_t tempB
[2], tempA
[2];
2564 rs1
= (rs
>> 32) & MIPSDSP_LLO
;
2565 rs0
= rs
& MIPSDSP_LLO
;
2566 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
2567 rt0
= rt
& MIPSDSP_LLO
;
2569 tempB
[0] = mipsdsp_mul_q31_q31(ac
, rs1
, rt1
, env
);
2570 tempA
[0] = mipsdsp_mul_q31_q31(ac
, rs0
, rt0
, env
);
2572 if (tempB
[0] >= 0) {
2578 if (tempA
[0] >= 0) {
2584 acc
[0] = env
->active_tc
.LO
[ac
];
2585 acc
[1] = env
->active_tc
.HI
[ac
];
2587 temp_sum
= tempB
[0] - tempA
[0];
2588 if ((uint64_t)temp_sum
> (uint64_t)tempB
[0]) {
2592 temp
[1] = tempB
[1] - tempA
[1];
2594 if ((temp
[1] & 0x01) == 0) {
2600 temp_sum
= acc
[0] + temp
[0];
2601 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2602 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2608 env
->active_tc
.HI
[ac
] = acc
[1];
2609 env
->active_tc
.LO
[ac
] = acc
[0];
2613 #define MAQ_S_W(name, mov) \
2614 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2615 CPUMIPSState *env) \
2619 int64_t tempL, acc; \
2621 rsh = (rs >> mov) & MIPSDSP_LO; \
2622 rth = (rt >> mov) & MIPSDSP_LO; \
2623 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2624 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2625 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2626 tempL = (int64_t)tempA + acc; \
2627 env->active_tc.HI[ac] = (target_long)(int32_t) \
2628 ((tempL & MIPSDSP_LHI) >> 32); \
2629 env->active_tc.LO[ac] = (target_long)(int32_t) \
2630 (tempL & MIPSDSP_LLO); \
2633 MAQ_S_W(maq_s_w_phl
, 16);
2634 MAQ_S_W(maq_s_w_phr
, 0);
2638 #define MAQ_SA_W(name, mov) \
2639 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2640 CPUMIPSState *env) \
2645 rsh = (rs >> mov) & MIPSDSP_LO; \
2646 rth = (rt >> mov) & MIPSDSP_LO; \
2647 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2648 tempA = mipsdsp_sat32_acc_q31(ac, tempA, env); \
2650 env->active_tc.HI[ac] = (target_long)(int32_t)(((int64_t)tempA & \
2651 MIPSDSP_LHI) >> 32); \
2652 env->active_tc.LO[ac] = (target_long)(int32_t)((int64_t)tempA & \
2656 MAQ_SA_W(maq_sa_w_phl
, 16);
2657 MAQ_SA_W(maq_sa_w_phr
, 0);
2661 #define MULQ_W(name, addvar) \
2662 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2663 CPUMIPSState *env) \
2665 int32_t rs_t, rt_t; \
2669 rs_t = rs & MIPSDSP_LLO; \
2670 rt_t = rt & MIPSDSP_LLO; \
2672 if ((rs_t == 0x80000000) && (rt_t == 0x80000000)) { \
2673 tempL = 0x7FFFFFFF00000000ull; \
2674 set_DSPControl_overflow_flag(1, 21, env); \
2676 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2679 tempI = (tempL & MIPSDSP_LHI) >> 32; \
2681 return (target_long)(int32_t)tempI; \
2684 MULQ_W(mulq_s_w
, 0);
2685 MULQ_W(mulq_rs_w
, 0x80000000ull
);
2689 #if defined(TARGET_MIPS64)
2691 #define MAQ_S_W_QH(name, mov) \
2692 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2693 CPUMIPSState *env) \
2695 int16_t rs_t, rt_t; \
2704 rs_t = (rs >> mov) & MIPSDSP_LO; \
2705 rt_t = (rt >> mov) & MIPSDSP_LO; \
2706 temp_mul = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2708 temp[0] = (int64_t)temp_mul; \
2709 if (temp[0] >= 0) { \
2715 acc[0] = env->active_tc.LO[ac]; \
2716 acc[1] = env->active_tc.HI[ac]; \
2718 temp_sum = acc[0] + temp[0]; \
2719 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2720 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2723 acc[0] = temp_sum; \
2724 acc[1] += temp[1]; \
2726 env->active_tc.HI[ac] = acc[1]; \
2727 env->active_tc.LO[ac] = acc[0]; \
2730 MAQ_S_W_QH(maq_s_w_qhll
, 48);
2731 MAQ_S_W_QH(maq_s_w_qhlr
, 32);
2732 MAQ_S_W_QH(maq_s_w_qhrl
, 16);
2733 MAQ_S_W_QH(maq_s_w_qhrr
, 0);
2737 #define MAQ_SA_W(name, mov) \
2738 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2739 CPUMIPSState *env) \
2741 int16_t rs_t, rt_t; \
2745 rs_t = (rs >> mov) & MIPSDSP_LO; \
2746 rt_t = (rt >> mov) & MIPSDSP_LO; \
2747 temp = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2748 temp = mipsdsp_sat32_acc_q31(ac, temp, env); \
2750 acc[0] = (int64_t)(int32_t)temp; \
2751 if (acc[0] >= 0) { \
2757 env->active_tc.HI[ac] = acc[1]; \
2758 env->active_tc.LO[ac] = acc[0]; \
2761 MAQ_SA_W(maq_sa_w_qhll
, 48);
2762 MAQ_SA_W(maq_sa_w_qhlr
, 32);
2763 MAQ_SA_W(maq_sa_w_qhrl
, 16);
2764 MAQ_SA_W(maq_sa_w_qhrr
, 0);
2768 #define MAQ_S_L_PW(name, mov) \
2769 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2770 CPUMIPSState *env) \
2772 int32_t rs_t, rt_t; \
2780 rs_t = (rs >> mov) & MIPSDSP_LLO; \
2781 rt_t = (rt >> mov) & MIPSDSP_LLO; \
2783 temp[0] = mipsdsp_mul_q31_q31(ac, rs_t, rt_t, env); \
2784 if (temp[0] >= 0) { \
2790 acc[0] = env->active_tc.LO[ac]; \
2791 acc[1] = env->active_tc.HI[ac]; \
2793 temp_sum = acc[0] + temp[0]; \
2794 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2795 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2798 acc[0] = temp_sum; \
2799 acc[1] += temp[1]; \
2801 env->active_tc.HI[ac] = acc[1]; \
2802 env->active_tc.LO[ac] = acc[0]; \
2805 MAQ_S_L_PW(maq_s_l_pwl
, 32);
2806 MAQ_S_L_PW(maq_s_l_pwr
, 0);
2810 #define DM_OPERATE(name, func, is_add, sigext) \
2811 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2812 CPUMIPSState *env) \
2816 int64_t tempBL[2], tempAL[2]; \
2824 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2825 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2828 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2829 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2831 if (tempBL[0] >= 0) { \
2834 tempBL[1] = ~0ull; \
2837 if (tempAL[0] >= 0) { \
2840 tempAL[1] = ~0ull; \
2843 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2844 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2849 acc[1] = env->active_tc.HI[ac]; \
2850 acc[0] = env->active_tc.LO[ac]; \
2852 temp_sum = tempBL[0] + tempAL[0]; \
2853 if (((uint64_t)temp_sum < (uint64_t)tempBL[0]) && \
2854 ((uint64_t)temp_sum < (uint64_t)tempAL[0])) { \
2857 temp[0] = temp_sum; \
2858 temp[1] += tempBL[1] + tempAL[1]; \
2861 temp_sum = acc[0] + temp[0]; \
2862 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2863 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2866 temp[0] = temp_sum; \
2867 temp[1] = acc[1] + temp[1]; \
2869 temp_sum = acc[0] - temp[0]; \
2870 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2873 temp[0] = temp_sum; \
2874 temp[1] = acc[1] - temp[1]; \
2877 env->active_tc.HI[ac] = temp[1]; \
2878 env->active_tc.LO[ac] = temp[0]; \
2881 DM_OPERATE(dmadd
, mul_i32_i32
, 1, 1);
2882 DM_OPERATE(dmaddu
, mul_u32_u32
, 1, 0);
2883 DM_OPERATE(dmsub
, mul_i32_i32
, 0, 1);
2884 DM_OPERATE(dmsubu
, mul_u32_u32
, 0, 0);
2888 /** DSP Bit/Manipulation Sub-class insns **/
2889 target_ulong
helper_bitrev(target_ulong rt
)
2895 temp
= rt
& MIPSDSP_LO
;
2897 for (i
= 0; i
< 16; i
++) {
2898 rd
= (rd
<< 1) | (temp
& 1);
2902 return (target_ulong
)rd
;
2905 #define BIT_INSV(name, posfilter, ret_type) \
2906 target_ulong helper_##name(CPUMIPSState *env, target_ulong rs, \
2909 uint32_t pos, size, msb, lsb; \
2910 uint32_t const sizefilter = 0x3F; \
2911 target_ulong temp; \
2912 target_ulong dspc; \
2914 dspc = env->active_tc.DSPControl; \
2916 pos = dspc & posfilter; \
2917 size = (dspc >> 7) & sizefilter; \
2919 msb = pos + size - 1; \
2922 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2926 temp = deposit64(rt, pos, size, rs); \
2928 return (target_long)(ret_type)temp; \
2931 BIT_INSV(insv
, 0x1F, int32_t);
2932 #ifdef TARGET_MIPS64
2933 BIT_INSV(dinsv
, 0x7F, target_long
);
2939 /** DSP Compare-Pick Sub-class insns **/
2940 #define CMP_HAS_RET(name, func, split_num, filter, bit_size) \
2941 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
2943 uint32_t rs_t, rt_t; \
2945 uint32_t temp = 0; \
2948 for (i = 0; i < split_num; i++) { \
2949 rs_t = (rs >> (bit_size * i)) & filter; \
2950 rt_t = (rt >> (bit_size * i)) & filter; \
2951 cc = mipsdsp_##func(rs_t, rt_t); \
2955 return (target_ulong)temp; \
2958 CMP_HAS_RET(cmpgu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2959 CMP_HAS_RET(cmpgu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2960 CMP_HAS_RET(cmpgu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
2962 #ifdef TARGET_MIPS64
2963 CMP_HAS_RET(cmpgu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
2964 CMP_HAS_RET(cmpgu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
2965 CMP_HAS_RET(cmpgu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
2971 #define CMP_NO_RET(name, func, split_num, filter, bit_size) \
2972 void helper_##name(target_ulong rs, target_ulong rt, \
2973 CPUMIPSState *env) \
2975 int##bit_size##_t rs_t, rt_t; \
2976 int##bit_size##_t flag = 0; \
2977 int##bit_size##_t cc; \
2980 for (i = 0; i < split_num; i++) { \
2981 rs_t = (rs >> (bit_size * i)) & filter; \
2982 rt_t = (rt >> (bit_size * i)) & filter; \
2984 cc = mipsdsp_##func((int32_t)rs_t, (int32_t)rt_t); \
2988 set_DSPControl_24(flag, split_num, env); \
2991 CMP_NO_RET(cmpu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2992 CMP_NO_RET(cmpu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2993 CMP_NO_RET(cmpu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
2995 CMP_NO_RET(cmp_eq_ph
, cmp_eq
, 2, MIPSDSP_LO
, 16);
2996 CMP_NO_RET(cmp_lt_ph
, cmp_lt
, 2, MIPSDSP_LO
, 16);
2997 CMP_NO_RET(cmp_le_ph
, cmp_le
, 2, MIPSDSP_LO
, 16);
2999 #ifdef TARGET_MIPS64
3000 CMP_NO_RET(cmpu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
3001 CMP_NO_RET(cmpu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
3002 CMP_NO_RET(cmpu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
3004 CMP_NO_RET(cmp_eq_qh
, cmp_eq
, 4, MIPSDSP_LO
, 16);
3005 CMP_NO_RET(cmp_lt_qh
, cmp_lt
, 4, MIPSDSP_LO
, 16);
3006 CMP_NO_RET(cmp_le_qh
, cmp_le
, 4, MIPSDSP_LO
, 16);
3008 CMP_NO_RET(cmp_eq_pw
, cmp_eq
, 2, MIPSDSP_LLO
, 32);
3009 CMP_NO_RET(cmp_lt_pw
, cmp_lt
, 2, MIPSDSP_LLO
, 32);
3010 CMP_NO_RET(cmp_le_pw
, cmp_le
, 2, MIPSDSP_LLO
, 32);
3014 #if defined(TARGET_MIPS64)
3016 #define CMPGDU_OB(name) \
3017 target_ulong helper_cmpgdu_##name##_ob(target_ulong rs, target_ulong rt, \
3018 CPUMIPSState *env) \
3021 uint8_t rs_t, rt_t; \
3026 for (i = 0; i < 8; i++) { \
3027 rs_t = (rs >> (8 * i)) & MIPSDSP_Q0; \
3028 rt_t = (rt >> (8 * i)) & MIPSDSP_Q0; \
3030 if (mipsdsp_cmpu_##name(rs_t, rt_t)) { \
3031 cond |= 0x01 << i; \
3035 set_DSPControl_24(cond, 8, env); \
3037 return (uint64_t)cond; \
3046 #define PICK_INSN(name, split_num, filter, bit_size, ret32bit) \
3047 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
3048 CPUMIPSState *env) \
3050 uint32_t rs_t, rt_t; \
3054 target_ulong result = 0; \
3056 dsp = env->active_tc.DSPControl; \
3057 for (i = 0; i < split_num; i++) { \
3058 rs_t = (rs >> (bit_size * i)) & filter; \
3059 rt_t = (rt >> (bit_size * i)) & filter; \
3060 cc = (dsp >> (24 + i)) & 0x01; \
3061 cc = cc == 1 ? rs_t : rt_t; \
3063 result |= (target_ulong)cc << (bit_size * i); \
3067 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3073 PICK_INSN(pick_qb
, 4, MIPSDSP_Q0
, 8, 1);
3074 PICK_INSN(pick_ph
, 2, MIPSDSP_LO
, 16, 1);
3076 #ifdef TARGET_MIPS64
3077 PICK_INSN(pick_ob
, 8, MIPSDSP_Q0
, 8, 0);
3078 PICK_INSN(pick_qh
, 4, MIPSDSP_LO
, 16, 0);
3079 PICK_INSN(pick_pw
, 2, MIPSDSP_LLO
, 32, 0);
3083 target_ulong
helper_packrl_ph(target_ulong rs
, target_ulong rt
)
3087 rsl
= rs
& MIPSDSP_LO
;
3088 rth
= (rt
& MIPSDSP_HI
) >> 16;
3090 return (target_long
)(int32_t)((rsl
<< 16) | rth
);
3093 #if defined(TARGET_MIPS64)
3094 target_ulong
helper_packrl_pw(target_ulong rs
, target_ulong rt
)
3098 rs0
= rs
& MIPSDSP_LLO
;
3099 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
3101 return ((uint64_t)rs0
<< 32) | (uint64_t)rt1
;
3105 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3106 target_ulong
helper_extr_w(target_ulong ac
, target_ulong shift
,
3112 shift
= shift
& 0x1F;
3114 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3115 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3116 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3117 set_DSPControl_overflow_flag(1, 23, env
);
3120 tempI
= (tempDL
[0] >> 1) & MIPSDSP_LLO
;
3123 if (tempDL
[0] == 0) {
3127 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3128 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3129 set_DSPControl_overflow_flag(1, 23, env
);
3132 return (target_long
)tempI
;
3135 target_ulong
helper_extr_r_w(target_ulong ac
, target_ulong shift
,
3140 shift
= shift
& 0x1F;
3142 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3143 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3144 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3145 set_DSPControl_overflow_flag(1, 23, env
);
3149 if (tempDL
[0] == 0) {
3153 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3154 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3155 set_DSPControl_overflow_flag(1, 23, env
);
3158 return (target_long
)(int32_t)(tempDL
[0] >> 1);
3161 target_ulong
helper_extr_rs_w(target_ulong ac
, target_ulong shift
,
3164 int32_t tempI
, temp64
;
3167 shift
= shift
& 0x1F;
3169 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3170 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3171 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3172 set_DSPControl_overflow_flag(1, 23, env
);
3175 if (tempDL
[0] == 0) {
3178 tempI
= tempDL
[0] >> 1;
3180 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3181 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3182 temp64
= tempDL
[1] & 0x01;
3188 set_DSPControl_overflow_flag(1, 23, env
);
3191 return (target_long
)tempI
;
3194 #if defined(TARGET_MIPS64)
3195 target_ulong
helper_dextr_w(target_ulong ac
, target_ulong shift
,
3200 shift
= shift
& 0x3F;
3202 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3204 return (int64_t)(int32_t)(temp
[0] >> 1);
3207 target_ulong
helper_dextr_r_w(target_ulong ac
, target_ulong shift
,
3213 shift
= shift
& 0x3F;
3214 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3224 temp128
= temp
[2] & 0x01;
3226 if ((temp128
!= 0 || temp
[1] != 0) &&
3227 (temp128
!= 1 || temp
[1] != ~0ull)) {
3228 set_DSPControl_overflow_flag(1, 23, env
);
3231 return (int64_t)(int32_t)(temp
[0] >> 1);
3234 target_ulong
helper_dextr_rs_w(target_ulong ac
, target_ulong shift
,
3240 shift
= shift
& 0x3F;
3241 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3251 temp128
= temp
[2] & 0x01;
3253 if ((temp128
!= 0 || temp
[1] != 0) &&
3254 (temp128
!= 1 || temp
[1] != ~0ull)) {
3256 temp
[0] = 0x0FFFFFFFF;
3258 temp
[0] = 0x0100000000ULL
;
3260 set_DSPControl_overflow_flag(1, 23, env
);
3263 return (int64_t)(int32_t)(temp
[0] >> 1);
3266 target_ulong
helper_dextr_l(target_ulong ac
, target_ulong shift
,
3270 target_ulong result
;
3272 shift
= shift
& 0x3F;
3274 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3275 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3280 target_ulong
helper_dextr_r_l(target_ulong ac
, target_ulong shift
,
3285 target_ulong result
;
3287 shift
= shift
& 0x3F;
3288 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3298 temp128
= temp
[2] & 0x01;
3300 if ((temp128
!= 0 || temp
[1] != 0) &&
3301 (temp128
!= 1 || temp
[1] != ~0ull)) {
3302 set_DSPControl_overflow_flag(1, 23, env
);
3305 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3310 target_ulong
helper_dextr_rs_l(target_ulong ac
, target_ulong shift
,
3315 target_ulong result
;
3317 shift
= shift
& 0x3F;
3318 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3328 temp128
= temp
[2] & 0x01;
3330 if ((temp128
!= 0 || temp
[1] != 0) &&
3331 (temp128
!= 1 || temp
[1] != ~0ull)) {
3333 temp
[1] &= ~0x00ull
- 1;
3334 temp
[0] |= ~0x00ull
- 1;
3339 set_DSPControl_overflow_flag(1, 23, env
);
3341 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3347 target_ulong
helper_extr_s_h(target_ulong ac
, target_ulong shift
,
3352 shift
= shift
& 0x1F;
3354 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
3355 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
3357 temp
= acc
>> shift
;
3359 if (temp
> (int64_t)0x7FFF) {
3361 set_DSPControl_overflow_flag(1, 23, env
);
3362 } else if (temp
< (int64_t)0xFFFFFFFFFFFF8000ULL
) {
3364 set_DSPControl_overflow_flag(1, 23, env
);
3367 return (target_long
)(int32_t)(temp
& 0xFFFFFFFF);
3371 #if defined(TARGET_MIPS64)
3372 target_ulong
helper_dextr_s_h(target_ulong ac
, target_ulong shift
,
3378 shift
= shift
& 0x1F;
3380 mipsdsp_rashift_acc((uint64_t *)temp
, ac
, shift
, env
);
3382 temp127
= (temp
[1] >> 63) & 0x01;
3384 if ((temp127
== 0) && (temp
[1] > 0 || temp
[0] > 32767)) {
3385 temp
[0] &= 0xFFFF0000;
3386 temp
[0] |= 0x00007FFF;
3387 set_DSPControl_overflow_flag(1, 23, env
);
3388 } else if ((temp127
== 1) &&
3389 (temp
[1] < 0xFFFFFFFFFFFFFFFFll
3390 || temp
[0] < 0xFFFFFFFFFFFF1000ll
)) {
3391 temp
[0] &= 0xFFFF0000;
3392 temp
[0] |= 0x00008000;
3393 set_DSPControl_overflow_flag(1, 23, env
);
3396 return (int64_t)(int16_t)(temp
[0] & MIPSDSP_LO
);
3401 target_ulong
helper_extp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3411 start_pos
= get_DSPControl_pos(env
);
3412 sub
= start_pos
- (size
+ 1);
3414 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3415 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3416 temp
= (acc
>> (start_pos
- size
)) & (~0U >> (31 - size
));
3417 set_DSPControl_efi(0, env
);
3419 set_DSPControl_efi(1, env
);
3422 return (target_ulong
)temp
;
3425 target_ulong
helper_extpdp(target_ulong ac
, target_ulong size
,
3435 start_pos
= get_DSPControl_pos(env
);
3436 sub
= start_pos
- (size
+ 1);
3438 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3439 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3440 temp
= extract64(acc
, start_pos
- size
, size
+ 1);
3442 set_DSPControl_pos(sub
, env
);
3443 set_DSPControl_efi(0, env
);
3445 set_DSPControl_efi(1, env
);
3448 return (target_ulong
)temp
;
3452 #if defined(TARGET_MIPS64)
3453 target_ulong
helper_dextp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3458 uint64_t tempB
, tempA
;
3464 start_pos
= get_DSPControl_pos(env
);
3465 len
= start_pos
- size
;
3466 tempB
= env
->active_tc
.HI
[ac
];
3467 tempA
= env
->active_tc
.LO
[ac
];
3469 sub
= start_pos
- (size
+ 1);
3472 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3473 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3474 set_DSPControl_efi(0, env
);
3476 set_DSPControl_efi(1, env
);
3482 target_ulong
helper_dextpdp(target_ulong ac
, target_ulong size
,
3488 uint64_t tempB
, tempA
;
3493 start_pos
= get_DSPControl_pos(env
);
3494 len
= start_pos
- size
;
3495 tempB
= env
->active_tc
.HI
[ac
];
3496 tempA
= env
->active_tc
.LO
[ac
];
3498 sub
= start_pos
- (size
+ 1);
3501 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3502 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3503 set_DSPControl_pos(sub
, env
);
3504 set_DSPControl_efi(0, env
);
3506 set_DSPControl_efi(1, env
);
3514 void helper_shilo(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3520 rs5_0
= (int8_t)(rs5_0
<< 2) >> 2;
3522 if (unlikely(rs5_0
== 0)) {
3526 acc
= (((uint64_t)env
->active_tc
.HI
[ac
] << 32) & MIPSDSP_LHI
) |
3527 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3530 temp
= acc
>> rs5_0
;
3532 temp
= acc
<< -rs5_0
;
3535 env
->active_tc
.HI
[ac
] = (target_ulong
)(int32_t)((temp
& MIPSDSP_LHI
) >> 32);
3536 env
->active_tc
.LO
[ac
] = (target_ulong
)(int32_t)(temp
& MIPSDSP_LLO
);
3539 #if defined(TARGET_MIPS64)
3540 void helper_dshilo(target_ulong shift
, target_ulong ac
, CPUMIPSState
*env
)
3543 uint64_t tempB
, tempA
;
3545 shift_t
= (int8_t)(shift
<< 1) >> 1;
3547 tempB
= env
->active_tc
.HI
[ac
];
3548 tempA
= env
->active_tc
.LO
[ac
];
3552 tempA
= (tempB
<< (64 - shift_t
)) | (tempA
>> shift_t
);
3553 tempB
= tempB
>> shift_t
;
3556 tempB
= (tempB
<< shift_t
) | (tempA
>> (64 - shift_t
));
3557 tempA
= tempA
<< shift_t
;
3561 env
->active_tc
.HI
[ac
] = tempB
;
3562 env
->active_tc
.LO
[ac
] = tempA
;
3566 void helper_mthlip(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3568 int32_t tempA
, tempB
, pos
;
3571 tempB
= env
->active_tc
.LO
[ac
];
3572 env
->active_tc
.HI
[ac
] = (target_long
)tempB
;
3573 env
->active_tc
.LO
[ac
] = (target_long
)tempA
;
3574 pos
= get_DSPControl_pos(env
);
3579 set_DSPControl_pos(pos
+ 32, env
);
3583 #if defined(TARGET_MIPS64)
3584 void helper_dmthlip(target_ulong rs
, target_ulong ac
, CPUMIPSState
*env
)
3588 uint64_t tempB
, tempA
;
3593 tempB
= env
->active_tc
.LO
[ac_t
];
3595 env
->active_tc
.HI
[ac_t
] = tempB
;
3596 env
->active_tc
.LO
[ac_t
] = tempA
;
3598 pos
= get_DSPControl_pos(env
);
3602 set_DSPControl_pos(pos
, env
);
3607 void cpu_wrdsp(uint32_t rs
, uint32_t mask_num
, CPUMIPSState
*env
)
3611 uint32_t newbits
, overwrite
;
3615 overwrite
= 0xFFFFFFFF;
3616 dsp
= env
->active_tc
.DSPControl
;
3618 for (i
= 0; i
< 6; i
++) {
3619 mask
[i
] = (mask_num
>> i
) & 0x01;
3623 #if defined(TARGET_MIPS64)
3624 overwrite
&= 0xFFFFFF80;
3625 newbits
&= 0xFFFFFF80;
3626 newbits
|= 0x0000007F & rs
;
3628 overwrite
&= 0xFFFFFFC0;
3629 newbits
&= 0xFFFFFFC0;
3630 newbits
|= 0x0000003F & rs
;
3635 overwrite
&= 0xFFFFE07F;
3636 newbits
&= 0xFFFFE07F;
3637 newbits
|= 0x00001F80 & rs
;
3641 overwrite
&= 0xFFFFDFFF;
3642 newbits
&= 0xFFFFDFFF;
3643 newbits
|= 0x00002000 & rs
;
3647 overwrite
&= 0xFF00FFFF;
3648 newbits
&= 0xFF00FFFF;
3649 newbits
|= 0x00FF0000 & rs
;
3653 overwrite
&= 0x00FFFFFF;
3654 newbits
&= 0x00FFFFFF;
3655 #if defined(TARGET_MIPS64)
3656 newbits
|= 0xFF000000 & rs
;
3658 newbits
|= 0x0F000000 & rs
;
3663 overwrite
&= 0xFFFFBFFF;
3664 newbits
&= 0xFFFFBFFF;
3665 newbits
|= 0x00004000 & rs
;
3668 dsp
= dsp
& overwrite
;
3669 dsp
= dsp
| newbits
;
3670 env
->active_tc
.DSPControl
= dsp
;
3673 void helper_wrdsp(target_ulong rs
, target_ulong mask_num
, CPUMIPSState
*env
)
3675 return cpu_wrdsp(rs
, mask_num
, env
);
3678 uint32_t cpu_rddsp(uint32_t mask_num
, CPUMIPSState
*env
)
3686 for (i
= 0; i
< 6; i
++) {
3687 mask
[i
] = (mask_num
& ruler
) >> i
;
3692 dsp
= env
->active_tc
.DSPControl
;
3695 #if defined(TARGET_MIPS64)
3703 temp
|= dsp
& 0x1F80;
3707 temp
|= dsp
& 0x2000;
3711 temp
|= dsp
& 0x00FF0000;
3715 #if defined(TARGET_MIPS64)
3716 temp
|= dsp
& 0xFF000000;
3718 temp
|= dsp
& 0x0F000000;
3723 temp
|= dsp
& 0x4000;
3729 target_ulong
helper_rddsp(target_ulong mask_num
, CPUMIPSState
*env
)
3731 return cpu_rddsp(mask_num
, env
);
3744 #undef MIPSDSP_SPLIT32_8
3745 #undef MIPSDSP_SPLIT32_16
3747 #undef MIPSDSP_RETURN32_8
3748 #undef MIPSDSP_RETURN32_16
3750 #ifdef TARGET_MIPS64
3751 #undef MIPSDSP_SPLIT64_16
3752 #undef MIPSDSP_SPLIT64_32
3753 #undef MIPSDSP_RETURN64_16
3754 #undef MIPSDSP_RETURN64_32