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/>.
20 #include "qemu/osdep.h"
22 #include "exec/helper-proto.h"
23 #include "qemu/bitops.h"
25 /* As the byte ordering doesn't matter, i.e. all columns are treated
26 identically, these unions can be used directly. */
47 /*** MIPS DSP internal functions begin ***/
48 #define MIPSDSP_ABS(x) (((x) >= 0) ? x : -x)
49 #define MIPSDSP_OVERFLOW_ADD(a, b, c, d) (~(a ^ b) & (a ^ c) & d)
50 #define MIPSDSP_OVERFLOW_SUB(a, b, c, d) ((a ^ b) & (a ^ c) & d)
52 static inline void set_DSPControl_overflow_flag(uint32_t flag
, int position
,
55 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< position
;
58 static inline void set_DSPControl_carryflag(bool flag
, CPUMIPSState
*env
)
60 env
->active_tc
.DSPControl
&= ~(1 << 13);
61 env
->active_tc
.DSPControl
|= flag
<< 13;
64 static inline uint32_t get_DSPControl_carryflag(CPUMIPSState
*env
)
66 return (env
->active_tc
.DSPControl
>> 13) & 0x01;
69 static inline void set_DSPControl_24(uint32_t flag
, int len
, CPUMIPSState
*env
)
73 filter
= ((0x01 << len
) - 1) << 24;
76 env
->active_tc
.DSPControl
&= filter
;
77 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 24;
80 static inline void set_DSPControl_pos(uint32_t pos
, CPUMIPSState
*env
)
84 dspc
= env
->active_tc
.DSPControl
;
86 dspc
= dspc
& 0xFFFFFFC0;
89 dspc
= dspc
& 0xFFFFFF80;
92 env
->active_tc
.DSPControl
= dspc
;
95 static inline uint32_t get_DSPControl_pos(CPUMIPSState
*env
)
100 dspc
= env
->active_tc
.DSPControl
;
102 #ifndef TARGET_MIPS64
111 static inline void set_DSPControl_efi(uint32_t flag
, CPUMIPSState
*env
)
113 env
->active_tc
.DSPControl
&= 0xFFFFBFFF;
114 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 14;
117 #define DO_MIPS_SAT_ABS(size) \
118 static inline int##size##_t mipsdsp_sat_abs##size(int##size##_t a, \
121 if (a == INT##size##_MIN) { \
122 set_DSPControl_overflow_flag(1, 20, env); \
123 return INT##size##_MAX; \
125 return MIPSDSP_ABS(a); \
131 #undef DO_MIPS_SAT_ABS
134 static inline int16_t mipsdsp_add_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
140 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x8000)) {
141 set_DSPControl_overflow_flag(1, 20, env
);
147 static inline int16_t mipsdsp_sat_add_i16(int16_t a
, int16_t b
,
154 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempS
, 0x8000)) {
160 set_DSPControl_overflow_flag(1, 20, env
);
166 static inline int32_t mipsdsp_sat_add_i32(int32_t a
, int32_t b
,
173 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x80000000)) {
179 set_DSPControl_overflow_flag(1, 20, env
);
185 static inline uint8_t mipsdsp_add_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
189 temp
= (uint16_t)a
+ (uint16_t)b
;
192 set_DSPControl_overflow_flag(1, 20, env
);
198 static inline uint16_t mipsdsp_add_u16(uint16_t a
, uint16_t b
,
203 temp
= (uint32_t)a
+ (uint32_t)b
;
205 if (temp
& 0x00010000) {
206 set_DSPControl_overflow_flag(1, 20, env
);
209 return temp
& 0xFFFF;
212 static inline uint8_t mipsdsp_sat_add_u8(uint8_t a
, uint8_t b
,
218 temp
= (uint16_t)a
+ (uint16_t)b
;
219 result
= temp
& 0xFF;
223 set_DSPControl_overflow_flag(1, 20, env
);
229 static inline uint16_t mipsdsp_sat_add_u16(uint16_t a
, uint16_t b
,
235 temp
= (uint32_t)a
+ (uint32_t)b
;
236 result
= temp
& 0xFFFF;
238 if (0x00010000 & temp
) {
240 set_DSPControl_overflow_flag(1, 20, env
);
246 static inline int32_t mipsdsp_sat32_acc_q31(int32_t acc
, int32_t a
,
250 int32_t temp32
, temp31
, result
;
253 #ifndef TARGET_MIPS64
254 temp
= ((uint64_t)env
->active_tc
.HI
[acc
] << 32) |
255 (uint64_t)env
->active_tc
.LO
[acc
];
257 temp
= (uint64_t)env
->active_tc
.LO
[acc
];
260 temp_sum
= (int64_t)a
+ temp
;
262 temp32
= (temp_sum
>> 32) & 0x01;
263 temp31
= (temp_sum
>> 31) & 0x01;
264 result
= temp_sum
& 0xFFFFFFFF;
266 if (temp32
!= temp31
) {
272 set_DSPControl_overflow_flag(1, 16 + acc
, env
);
279 /* a[0] is LO, a[1] is HI. */
280 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret
,
287 ret
[0] = env
->active_tc
.LO
[ac
] + a
[0];
288 ret
[1] = env
->active_tc
.HI
[ac
] + a
[1];
290 if (((uint64_t)ret
[0] < (uint64_t)env
->active_tc
.LO
[ac
]) &&
291 ((uint64_t)ret
[0] < (uint64_t)a
[0])) {
295 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
297 ret
[0] = (0x01ull
<< 63);
300 ret
[0] = (0x01ull
<< 63) - 1;
303 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
307 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret
,
314 ret
[0] = env
->active_tc
.LO
[ac
] - a
[0];
315 ret
[1] = env
->active_tc
.HI
[ac
] - a
[1];
317 if ((uint64_t)ret
[0] > (uint64_t)env
->active_tc
.LO
[ac
]) {
321 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
323 ret
[0] = (0x01ull
<< 63);
326 ret
[0] = (0x01ull
<< 63) - 1;
329 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
334 static inline int32_t mipsdsp_mul_i16_i16(int16_t a
, int16_t b
,
339 temp
= (int32_t)a
* (int32_t)b
;
341 if ((temp
> (int)0x7FFF) || (temp
< (int)0xFFFF8000)) {
342 set_DSPControl_overflow_flag(1, 21, env
);
349 static inline int32_t mipsdsp_mul_u16_u16(int32_t a
, int32_t b
)
355 static inline int32_t mipsdsp_mul_i32_i32(int32_t a
, int32_t b
)
361 static inline int32_t mipsdsp_sat16_mul_i16_i16(int16_t a
, int16_t b
,
366 temp
= (int32_t)a
* (int32_t)b
;
368 if (temp
> (int)0x7FFF) {
370 set_DSPControl_overflow_flag(1, 21, env
);
371 } else if (temp
< (int)0xffff8000) {
373 set_DSPControl_overflow_flag(1, 21, env
);
380 static inline int32_t mipsdsp_mul_q15_q15_overflowflag21(uint16_t a
, uint16_t b
,
385 if ((a
== 0x8000) && (b
== 0x8000)) {
387 set_DSPControl_overflow_flag(1, 21, env
);
389 temp
= ((int16_t)a
* (int16_t)b
) << 1;
396 static inline uint8_t mipsdsp_rshift_u8(uint8_t a
, target_ulong mov
)
401 static inline uint16_t mipsdsp_rshift_u16(uint16_t a
, target_ulong mov
)
406 static inline int8_t mipsdsp_rashift8(int8_t a
, target_ulong mov
)
411 static inline int16_t mipsdsp_rashift16(int16_t a
, target_ulong mov
)
417 static inline int32_t mipsdsp_rashift32(int32_t a
, target_ulong mov
)
423 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a
, int16_t b
)
427 temp
= (int32_t)a
+ (int32_t)b
;
429 return (temp
>> 1) & 0xFFFF;
432 /* round right shift */
433 static inline int16_t mipsdsp_rrshift1_add_q16(int16_t a
, int16_t b
)
437 temp
= (int32_t)a
+ (int32_t)b
;
440 return (temp
>> 1) & 0xFFFF;
443 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a
, int32_t b
)
447 temp
= (int64_t)a
+ (int64_t)b
;
449 return (temp
>> 1) & 0xFFFFFFFF;
452 static inline int32_t mipsdsp_rrshift1_add_q32(int32_t a
, int32_t b
)
456 temp
= (int64_t)a
+ (int64_t)b
;
459 return (temp
>> 1) & 0xFFFFFFFF;
462 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a
, uint8_t b
)
466 temp
= (uint16_t)a
+ (uint16_t)b
;
468 return (temp
>> 1) & 0x00FF;
471 static inline uint8_t mipsdsp_rrshift1_add_u8(uint8_t a
, uint8_t b
)
475 temp
= (uint16_t)a
+ (uint16_t)b
+ 1;
477 return (temp
>> 1) & 0x00FF;
481 static inline uint8_t mipsdsp_rshift1_sub_u8(uint8_t a
, uint8_t b
)
485 temp
= (uint16_t)a
- (uint16_t)b
;
487 return (temp
>> 1) & 0x00FF;
490 static inline uint8_t mipsdsp_rrshift1_sub_u8(uint8_t a
, uint8_t b
)
494 temp
= (uint16_t)a
- (uint16_t)b
+ 1;
496 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;
515 /* 128 bits long. p[0] is LO, p[1] is HI */
516 static inline void mipsdsp_rashift_acc(uint64_t *p
,
521 uint64_t tempB
, tempA
;
523 tempB
= env
->active_tc
.HI
[ac
];
524 tempA
= env
->active_tc
.LO
[ac
];
525 shift
= shift
& 0x1F;
531 p
[0] = (tempB
<< (64 - shift
)) | (tempA
>> shift
);
532 p
[1] = (int64_t)tempB
>> shift
;
536 /* 128 bits long. p[0] is LO, p[1] is HI , p[2] is sign of HI.*/
537 static inline void mipsdsp_rndrashift_acc(uint64_t *p
,
542 int64_t tempB
, tempA
;
544 tempB
= env
->active_tc
.HI
[ac
];
545 tempA
= env
->active_tc
.LO
[ac
];
546 shift
= shift
& 0x3F;
550 p
[1] = (tempB
<< 1) | (tempA
>> 63);
553 p
[0] = (tempB
<< (65 - shift
)) | (tempA
>> (shift
- 1));
554 p
[1] = (int64_t)tempB
>> (shift
- 1);
564 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac
, uint16_t a
, uint16_t b
,
569 if ((a
== 0x8000) && (b
== 0x8000)) {
571 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
573 temp
= ((int16_t)a
* (int16_t)b
) << 1;
579 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac
, uint32_t a
, uint32_t b
,
584 if ((a
== 0x80000000) && (b
== 0x80000000)) {
585 temp
= (0x01ull
<< 63) - 1;
586 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
588 temp
= ((int64_t)(int32_t)a
* (int32_t)b
) << 1;
594 static inline uint16_t mipsdsp_mul_u8_u8(uint8_t a
, uint8_t b
)
596 return (uint16_t)a
* (uint16_t)b
;
599 static inline uint16_t mipsdsp_mul_u8_u16(uint8_t a
, uint16_t b
,
604 tempI
= (uint32_t)a
* (uint32_t)b
;
605 if (tempI
> 0x0000FFFF) {
607 set_DSPControl_overflow_flag(1, 21, env
);
610 return tempI
& 0x0000FFFF;
614 static inline uint64_t mipsdsp_mul_u32_u32(uint32_t a
, uint32_t b
)
616 return (uint64_t)a
* (uint64_t)b
;
620 static inline int16_t mipsdsp_rndq15_mul_q15_q15(uint16_t a
, uint16_t b
,
625 if ((a
== 0x8000) && (b
== 0x8000)) {
627 set_DSPControl_overflow_flag(1, 21, env
);
629 temp
= ((int16_t)a
* (int16_t)b
) << 1;
630 temp
= temp
+ 0x00008000;
633 return (temp
& 0xFFFF0000) >> 16;
636 static inline int32_t mipsdsp_sat16_mul_q15_q15(uint16_t a
, uint16_t b
,
641 if ((a
== 0x8000) && (b
== 0x8000)) {
643 set_DSPControl_overflow_flag(1, 21, env
);
645 temp
= (int16_t)a
* (int16_t)b
;
649 return (temp
>> 16) & 0x0000FFFF;
652 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a
,
659 * The value 0x00008000 will be added to the input Q31 value, and the code
660 * needs to check if the addition causes an overflow. Since a positive value
661 * is added, overflow can happen in one direction only.
663 if (a
> 0x7FFF7FFF) {
665 set_DSPControl_overflow_flag(1, 22, env
);
667 temp
= ((a
+ 0x8000) >> 16) & 0xFFFF;
673 static inline uint8_t mipsdsp_sat8_reduce_precision(uint16_t a
,
679 sign
= (a
>> 15) & 0x01;
684 set_DSPControl_overflow_flag(1, 22, env
);
687 return (mag
>> 7) & 0xFFFF;
690 set_DSPControl_overflow_flag(1, 22, env
);
695 static inline uint8_t mipsdsp_lshift8(uint8_t a
, uint8_t s
, CPUMIPSState
*env
)
700 discard
= a
>> (8 - s
);
702 if (discard
!= 0x00) {
703 set_DSPControl_overflow_flag(1, 22, env
);
709 static inline uint16_t mipsdsp_lshift16(uint16_t a
, uint8_t s
,
715 discard
= (int16_t)a
>> (15 - s
);
717 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
718 set_DSPControl_overflow_flag(1, 22, env
);
725 static inline uint32_t mipsdsp_lshift32(uint32_t a
, uint8_t s
,
733 discard
= (int32_t)a
>> (31 - (s
- 1));
735 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
736 set_DSPControl_overflow_flag(1, 22, env
);
743 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a
, uint8_t s
,
752 sign
= (a
>> 15) & 0x01;
754 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
755 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
757 discard
= a
>> (14 - (s
- 1));
760 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
761 set_DSPControl_overflow_flag(1, 22, env
);
762 return (sign
== 0) ? 0x7FFF : 0x8000;
769 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a
, uint8_t s
,
778 sign
= (a
>> 31) & 0x01;
780 discard
= (((0x01 << (32 - s
)) - 1) << s
) |
781 ((a
>> (30 - (s
- 1))) & ((0x01 << s
) - 1));
783 discard
= a
>> (30 - (s
- 1));
786 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
787 set_DSPControl_overflow_flag(1, 22, env
);
788 return (sign
== 0) ? 0x7FFFFFFF : 0x80000000;
795 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a
, uint8_t s
)
800 temp
= (uint32_t)a
<< 1;
802 temp
= (int32_t)(int8_t)a
>> (s
- 1);
805 return (temp
+ 1) >> 1;
808 static inline uint16_t mipsdsp_rnd16_rashift(uint16_t a
, uint8_t s
)
813 temp
= (uint32_t)a
<< 1;
815 temp
= (int32_t)(int16_t)a
>> (s
- 1);
818 return (temp
+ 1) >> 1;
821 static inline uint32_t mipsdsp_rnd32_rashift(uint32_t a
, uint8_t s
)
826 temp
= (uint64_t)a
<< 1;
828 temp
= (int64_t)(int32_t)a
>> (s
- 1);
832 return (temp
>> 1) & 0xFFFFFFFFull
;
835 static inline uint16_t mipsdsp_sub_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
840 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
841 set_DSPControl_overflow_flag(1, 20, env
);
847 static inline uint16_t mipsdsp_sat16_sub(int16_t a
, int16_t b
,
853 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
859 set_DSPControl_overflow_flag(1, 20, env
);
865 static inline uint32_t mipsdsp_sat32_sub(int32_t a
, int32_t b
,
871 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
877 set_DSPControl_overflow_flag(1, 20, env
);
880 return temp
& 0xFFFFFFFFull
;
883 static inline uint16_t mipsdsp_rshift1_sub_q16(int16_t a
, int16_t b
)
887 temp
= (int32_t)a
- (int32_t)b
;
889 return (temp
>> 1) & 0x0000FFFF;
892 static inline uint16_t mipsdsp_rrshift1_sub_q16(int16_t a
, int16_t b
)
896 temp
= (int32_t)a
- (int32_t)b
;
899 return (temp
>> 1) & 0x0000FFFF;
902 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a
, int32_t b
)
906 temp
= (int64_t)a
- (int64_t)b
;
908 return (temp
>> 1) & 0xFFFFFFFFull
;
911 static inline uint32_t mipsdsp_rrshift1_sub_q32(int32_t a
, int32_t b
)
915 temp
= (int64_t)a
- (int64_t)b
;
918 return (temp
>> 1) & 0xFFFFFFFFull
;
921 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a
, uint16_t b
,
927 temp
= (uint32_t)a
- (uint32_t)b
;
928 temp16
= (temp
>> 16) & 0x01;
930 set_DSPControl_overflow_flag(1, 20, env
);
932 return temp
& 0x0000FFFF;
935 static inline uint16_t mipsdsp_satu16_sub_u16_u16(uint16_t a
, uint16_t b
,
941 temp
= (uint32_t)a
- (uint32_t)b
;
942 temp16
= (temp
>> 16) & 0x01;
946 set_DSPControl_overflow_flag(1, 20, env
);
949 return temp
& 0x0000FFFF;
952 static inline uint8_t mipsdsp_sub_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
957 temp
= (uint16_t)a
- (uint16_t)b
;
958 temp8
= (temp
>> 8) & 0x01;
960 set_DSPControl_overflow_flag(1, 20, env
);
963 return temp
& 0x00FF;
966 static inline uint8_t mipsdsp_satu8_sub(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
971 temp
= (uint16_t)a
- (uint16_t)b
;
972 temp8
= (temp
>> 8) & 0x01;
975 set_DSPControl_overflow_flag(1, 20, env
);
978 return temp
& 0x00FF;
982 static inline uint32_t mipsdsp_sub32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
987 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
988 set_DSPControl_overflow_flag(1, 20, env
);
994 static inline int32_t mipsdsp_add_i32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
1000 if (MIPSDSP_OVERFLOW_ADD(a
, b
, temp
, 0x80000000)) {
1001 set_DSPControl_overflow_flag(1, 20, env
);
1008 static inline int32_t mipsdsp_cmp_eq(int32_t a
, int32_t b
)
1013 static inline int32_t mipsdsp_cmp_le(int32_t a
, int32_t b
)
1018 static inline int32_t mipsdsp_cmp_lt(int32_t a
, int32_t b
)
1023 static inline int32_t mipsdsp_cmpu_eq(uint32_t a
, uint32_t b
)
1028 static inline int32_t mipsdsp_cmpu_le(uint32_t a
, uint32_t b
)
1033 static inline int32_t mipsdsp_cmpu_lt(uint32_t a
, uint32_t b
)
1037 /*** MIPS DSP internal functions end ***/
1039 #define MIPSDSP_LHI 0xFFFFFFFF00000000ull
1040 #define MIPSDSP_LLO 0x00000000FFFFFFFFull
1041 #define MIPSDSP_HI 0xFFFF0000
1042 #define MIPSDSP_LO 0x0000FFFF
1043 #define MIPSDSP_Q3 0xFF000000
1044 #define MIPSDSP_Q2 0x00FF0000
1045 #define MIPSDSP_Q1 0x0000FF00
1046 #define MIPSDSP_Q0 0x000000FF
1048 #define MIPSDSP_SPLIT32_8(num, a, b, c, d) \
1050 a = (num >> 24) & MIPSDSP_Q0; \
1051 b = (num >> 16) & MIPSDSP_Q0; \
1052 c = (num >> 8) & MIPSDSP_Q0; \
1053 d = num & MIPSDSP_Q0; \
1056 #define MIPSDSP_SPLIT32_16(num, a, b) \
1058 a = (num >> 16) & MIPSDSP_LO; \
1059 b = num & MIPSDSP_LO; \
1062 #define MIPSDSP_RETURN32_8(a, b, c, d) ((target_long)(int32_t) \
1063 (((uint32_t)a << 24) | \
1064 (((uint32_t)b << 16) | \
1065 (((uint32_t)c << 8) | \
1066 ((uint32_t)d & 0xFF)))))
1067 #define MIPSDSP_RETURN32_16(a, b) ((target_long)(int32_t) \
1068 (((uint32_t)a << 16) | \
1069 ((uint32_t)b & 0xFFFF)))
1071 #ifdef TARGET_MIPS64
1072 #define MIPSDSP_SPLIT64_16(num, a, b, c, d) \
1074 a = (num >> 48) & MIPSDSP_LO; \
1075 b = (num >> 32) & MIPSDSP_LO; \
1076 c = (num >> 16) & MIPSDSP_LO; \
1077 d = num & MIPSDSP_LO; \
1080 #define MIPSDSP_SPLIT64_32(num, a, b) \
1082 a = (num >> 32) & MIPSDSP_LLO; \
1083 b = num & MIPSDSP_LLO; \
1086 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)a << 48) | \
1087 ((uint64_t)b << 32) | \
1088 ((uint64_t)c << 16) | \
1090 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)a << 32) | (uint64_t)b)
1093 /** DSP Arithmetic Sub-class insns **/
1094 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1095 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1102 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1103 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1106 return (target_long)dt.sw[0]; \
1108 MIPSDSP32_UNOP_ENV(absq_s_ph
, sat_abs16
, sh
)
1109 MIPSDSP32_UNOP_ENV(absq_s_qb
, sat_abs8
, sb
)
1110 MIPSDSP32_UNOP_ENV(absq_s_w
, sat_abs32
, sw
)
1111 #undef MIPSDSP32_UNOP_ENV
1113 #if defined(TARGET_MIPS64)
1114 #define MIPSDSP64_UNOP_ENV(name, func, element) \
1115 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1122 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1123 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1128 MIPSDSP64_UNOP_ENV(absq_s_ob
, sat_abs8
, sb
)
1129 MIPSDSP64_UNOP_ENV(absq_s_qh
, sat_abs16
, sh
)
1130 MIPSDSP64_UNOP_ENV(absq_s_pw
, sat_abs32
, sw
)
1131 #undef MIPSDSP64_UNOP_ENV
1134 #define MIPSDSP32_BINOP(name, func, element) \
1135 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1137 DSP32Value ds, dt; \
1143 for (i = 0; i < ARRAY_SIZE(ds.element); i++) { \
1144 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1147 return (target_long)ds.sw[0]; \
1149 MIPSDSP32_BINOP(addqh_ph
, rshift1_add_q16
, sh
);
1150 MIPSDSP32_BINOP(addqh_r_ph
, rrshift1_add_q16
, sh
);
1151 MIPSDSP32_BINOP(addqh_r_w
, rrshift1_add_q32
, sw
);
1152 MIPSDSP32_BINOP(addqh_w
, rshift1_add_q32
, sw
);
1153 MIPSDSP32_BINOP(adduh_qb
, rshift1_add_u8
, ub
);
1154 MIPSDSP32_BINOP(adduh_r_qb
, rrshift1_add_u8
, ub
);
1155 MIPSDSP32_BINOP(subqh_ph
, rshift1_sub_q16
, sh
);
1156 MIPSDSP32_BINOP(subqh_r_ph
, rrshift1_sub_q16
, sh
);
1157 MIPSDSP32_BINOP(subqh_r_w
, rrshift1_sub_q32
, sw
);
1158 MIPSDSP32_BINOP(subqh_w
, rshift1_sub_q32
, sw
);
1159 #undef MIPSDSP32_BINOP
1161 #define MIPSDSP32_BINOP_ENV(name, func, element) \
1162 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1163 CPUMIPSState *env) \
1165 DSP32Value ds, dt; \
1171 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1172 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1175 return (target_long)ds.sw[0]; \
1177 MIPSDSP32_BINOP_ENV(addq_ph
, add_i16
, sh
)
1178 MIPSDSP32_BINOP_ENV(addq_s_ph
, sat_add_i16
, sh
)
1179 MIPSDSP32_BINOP_ENV(addq_s_w
, sat_add_i32
, sw
);
1180 MIPSDSP32_BINOP_ENV(addu_ph
, add_u16
, sh
)
1181 MIPSDSP32_BINOP_ENV(addu_qb
, add_u8
, ub
);
1182 MIPSDSP32_BINOP_ENV(addu_s_ph
, sat_add_u16
, sh
)
1183 MIPSDSP32_BINOP_ENV(addu_s_qb
, sat_add_u8
, ub
);
1184 MIPSDSP32_BINOP_ENV(subq_ph
, sub_i16
, sh
);
1185 MIPSDSP32_BINOP_ENV(subq_s_ph
, sat16_sub
, sh
);
1186 MIPSDSP32_BINOP_ENV(subq_s_w
, sat32_sub
, sw
);
1187 MIPSDSP32_BINOP_ENV(subu_ph
, sub_u16_u16
, sh
);
1188 MIPSDSP32_BINOP_ENV(subu_qb
, sub_u8
, ub
);
1189 MIPSDSP32_BINOP_ENV(subu_s_ph
, satu16_sub_u16_u16
, sh
);
1190 MIPSDSP32_BINOP_ENV(subu_s_qb
, satu8_sub
, ub
);
1191 #undef MIPSDSP32_BINOP_ENV
1193 #ifdef TARGET_MIPS64
1194 #define MIPSDSP64_BINOP(name, func, element) \
1195 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1197 DSP64Value ds, dt; \
1203 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1204 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1209 MIPSDSP64_BINOP(adduh_ob
, rshift1_add_u8
, ub
);
1210 MIPSDSP64_BINOP(adduh_r_ob
, rrshift1_add_u8
, ub
);
1211 MIPSDSP64_BINOP(subuh_ob
, rshift1_sub_u8
, ub
);
1212 MIPSDSP64_BINOP(subuh_r_ob
, rrshift1_sub_u8
, ub
);
1213 #undef MIPSDSP64_BINOP
1215 #define MIPSDSP64_BINOP_ENV(name, func, element) \
1216 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1217 CPUMIPSState *env) \
1219 DSP64Value ds, dt; \
1225 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1226 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1231 MIPSDSP64_BINOP_ENV(addq_pw
, add_i32
, sw
);
1232 MIPSDSP64_BINOP_ENV(addq_qh
, add_i16
, sh
);
1233 MIPSDSP64_BINOP_ENV(addq_s_pw
, sat_add_i32
, sw
);
1234 MIPSDSP64_BINOP_ENV(addq_s_qh
, sat_add_i16
, sh
);
1235 MIPSDSP64_BINOP_ENV(addu_ob
, add_u8
, uh
);
1236 MIPSDSP64_BINOP_ENV(addu_qh
, add_u16
, uh
);
1237 MIPSDSP64_BINOP_ENV(addu_s_ob
, sat_add_u8
, uh
);
1238 MIPSDSP64_BINOP_ENV(addu_s_qh
, sat_add_u16
, uh
);
1239 MIPSDSP64_BINOP_ENV(subq_pw
, sub32
, sw
);
1240 MIPSDSP64_BINOP_ENV(subq_qh
, sub_i16
, sh
);
1241 MIPSDSP64_BINOP_ENV(subq_s_pw
, sat32_sub
, sw
);
1242 MIPSDSP64_BINOP_ENV(subq_s_qh
, sat16_sub
, sh
);
1243 MIPSDSP64_BINOP_ENV(subu_ob
, sub_u8
, uh
);
1244 MIPSDSP64_BINOP_ENV(subu_qh
, sub_u16_u16
, uh
);
1245 MIPSDSP64_BINOP_ENV(subu_s_ob
, satu8_sub
, uh
);
1246 MIPSDSP64_BINOP_ENV(subu_s_qh
, satu16_sub_u16_u16
, uh
);
1247 #undef MIPSDSP64_BINOP_ENV
1251 #define SUBUH_QB(name, var) \
1252 target_ulong helper_##name##_qb(target_ulong rs, target_ulong rt) \
1254 uint8_t rs3, rs2, rs1, rs0; \
1255 uint8_t rt3, rt2, rt1, rt0; \
1256 uint8_t tempD, tempC, tempB, tempA; \
1258 MIPSDSP_SPLIT32_8(rs, rs3, rs2, rs1, rs0); \
1259 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1261 tempD = ((uint16_t)rs3 - (uint16_t)rt3 + var) >> 1; \
1262 tempC = ((uint16_t)rs2 - (uint16_t)rt2 + var) >> 1; \
1263 tempB = ((uint16_t)rs1 - (uint16_t)rt1 + var) >> 1; \
1264 tempA = ((uint16_t)rs0 - (uint16_t)rt0 + var) >> 1; \
1266 return ((uint32_t)tempD << 24) | ((uint32_t)tempC << 16) | \
1267 ((uint32_t)tempB << 8) | ((uint32_t)tempA); \
1271 SUBUH_QB(subuh_r
, 1);
1275 target_ulong
helper_addsc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1277 uint64_t temp
, tempRs
, tempRt
;
1280 tempRs
= (uint64_t)rs
& MIPSDSP_LLO
;
1281 tempRt
= (uint64_t)rt
& MIPSDSP_LLO
;
1283 temp
= tempRs
+ tempRt
;
1284 flag
= (temp
& 0x0100000000ull
) >> 32;
1285 set_DSPControl_carryflag(flag
, env
);
1287 return (target_long
)(int32_t)(temp
& MIPSDSP_LLO
);
1290 target_ulong
helper_addwc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1293 int32_t temp32
, temp31
;
1296 tempL
= (int64_t)(int32_t)rs
+ (int64_t)(int32_t)rt
+
1297 get_DSPControl_carryflag(env
);
1298 temp31
= (tempL
>> 31) & 0x01;
1299 temp32
= (tempL
>> 32) & 0x01;
1301 if (temp31
!= temp32
) {
1302 set_DSPControl_overflow_flag(1, 20, env
);
1305 rd
= tempL
& MIPSDSP_LLO
;
1307 return (target_long
)(int32_t)rd
;
1310 target_ulong
helper_modsub(target_ulong rs
, target_ulong rt
)
1316 decr
= rt
& MIPSDSP_Q0
;
1317 lastindex
= (rt
>> 8) & MIPSDSP_LO
;
1319 if ((rs
& MIPSDSP_LLO
) == 0x00000000) {
1320 rd
= (target_ulong
)lastindex
;
1328 target_ulong
helper_raddu_w_qb(target_ulong rs
)
1330 target_ulong ret
= 0;
1335 for (i
= 0; i
< 4; i
++) {
1341 #if defined(TARGET_MIPS64)
1342 target_ulong
helper_raddu_l_ob(target_ulong rs
)
1344 target_ulong ret
= 0;
1349 for (i
= 0; i
< 8; i
++) {
1356 #define PRECR_QB_PH(name, a, b)\
1357 target_ulong helper_##name##_qb_ph(target_ulong rs, target_ulong rt) \
1359 uint8_t tempD, tempC, tempB, tempA; \
1361 tempD = (rs >> a) & MIPSDSP_Q0; \
1362 tempC = (rs >> b) & MIPSDSP_Q0; \
1363 tempB = (rt >> a) & MIPSDSP_Q0; \
1364 tempA = (rt >> b) & MIPSDSP_Q0; \
1366 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA); \
1369 PRECR_QB_PH(precr
, 16, 0);
1370 PRECR_QB_PH(precrq
, 24, 8);
1374 target_ulong
helper_precr_sra_ph_w(uint32_t sa
, target_ulong rs
,
1377 uint16_t tempB
, tempA
;
1379 tempB
= ((int32_t)rt
>> sa
) & MIPSDSP_LO
;
1380 tempA
= ((int32_t)rs
>> sa
) & MIPSDSP_LO
;
1382 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1385 target_ulong
helper_precr_sra_r_ph_w(uint32_t sa
,
1386 target_ulong rs
, target_ulong rt
)
1388 uint64_t tempB
, tempA
;
1390 /* If sa = 0, then (sa - 1) = -1 will case shift error, so we need else. */
1392 tempB
= (rt
& MIPSDSP_LO
) << 1;
1393 tempA
= (rs
& MIPSDSP_LO
) << 1;
1395 tempB
= ((int32_t)rt
>> (sa
- 1)) + 1;
1396 tempA
= ((int32_t)rs
>> (sa
- 1)) + 1;
1398 rt
= (((tempB
>> 1) & MIPSDSP_LO
) << 16) | ((tempA
>> 1) & MIPSDSP_LO
);
1400 return (target_long
)(int32_t)rt
;
1403 target_ulong
helper_precrq_ph_w(target_ulong rs
, target_ulong rt
)
1405 uint16_t tempB
, tempA
;
1407 tempB
= (rs
& MIPSDSP_HI
) >> 16;
1408 tempA
= (rt
& MIPSDSP_HI
) >> 16;
1410 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1413 target_ulong
helper_precrq_rs_ph_w(target_ulong rs
, target_ulong rt
,
1416 uint16_t tempB
, tempA
;
1418 tempB
= mipsdsp_trunc16_sat16_round(rs
, env
);
1419 tempA
= mipsdsp_trunc16_sat16_round(rt
, env
);
1421 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1424 #if defined(TARGET_MIPS64)
1425 target_ulong
helper_precr_ob_qh(target_ulong rs
, target_ulong rt
)
1427 uint8_t rs6
, rs4
, rs2
, rs0
;
1428 uint8_t rt6
, rt4
, rt2
, rt0
;
1431 rs6
= (rs
>> 48) & MIPSDSP_Q0
;
1432 rs4
= (rs
>> 32) & MIPSDSP_Q0
;
1433 rs2
= (rs
>> 16) & MIPSDSP_Q0
;
1434 rs0
= rs
& MIPSDSP_Q0
;
1435 rt6
= (rt
>> 48) & MIPSDSP_Q0
;
1436 rt4
= (rt
>> 32) & MIPSDSP_Q0
;
1437 rt2
= (rt
>> 16) & MIPSDSP_Q0
;
1438 rt0
= rt
& MIPSDSP_Q0
;
1440 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1441 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1442 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1443 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1448 #define PRECR_QH_PW(name, var) \
1449 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, target_ulong rt, \
1452 uint16_t rs3, rs2, rs1, rs0; \
1453 uint16_t rt3, rt2, rt1, rt0; \
1454 uint16_t tempD, tempC, tempB, tempA; \
1456 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
1457 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1459 /* When sa = 0, we use rt2, rt0, rs2, rs0; \
1460 * when sa != 0, we use rt3, rt1, rs3, rs1. */ \
1462 tempD = rt2 << var; \
1463 tempC = rt0 << var; \
1464 tempB = rs2 << var; \
1465 tempA = rs0 << var; \
1467 tempD = (((int16_t)rt3 >> sa) + var) >> var; \
1468 tempC = (((int16_t)rt1 >> sa) + var) >> var; \
1469 tempB = (((int16_t)rs3 >> sa) + var) >> var; \
1470 tempA = (((int16_t)rs1 >> sa) + var) >> var; \
1473 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1476 PRECR_QH_PW(sra
, 0);
1477 PRECR_QH_PW(sra_r
, 1);
1481 target_ulong
helper_precrq_ob_qh(target_ulong rs
, target_ulong rt
)
1483 uint8_t rs6
, rs4
, rs2
, rs0
;
1484 uint8_t rt6
, rt4
, rt2
, rt0
;
1487 rs6
= (rs
>> 56) & MIPSDSP_Q0
;
1488 rs4
= (rs
>> 40) & MIPSDSP_Q0
;
1489 rs2
= (rs
>> 24) & MIPSDSP_Q0
;
1490 rs0
= (rs
>> 8) & MIPSDSP_Q0
;
1491 rt6
= (rt
>> 56) & MIPSDSP_Q0
;
1492 rt4
= (rt
>> 40) & MIPSDSP_Q0
;
1493 rt2
= (rt
>> 24) & MIPSDSP_Q0
;
1494 rt0
= (rt
>> 8) & MIPSDSP_Q0
;
1496 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1497 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1498 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1499 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1504 target_ulong
helper_precrq_qh_pw(target_ulong rs
, target_ulong rt
)
1506 uint16_t tempD
, tempC
, tempB
, tempA
;
1508 tempD
= (rs
>> 48) & MIPSDSP_LO
;
1509 tempC
= (rs
>> 16) & MIPSDSP_LO
;
1510 tempB
= (rt
>> 48) & MIPSDSP_LO
;
1511 tempA
= (rt
>> 16) & MIPSDSP_LO
;
1513 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1516 target_ulong
helper_precrq_rs_qh_pw(target_ulong rs
, target_ulong rt
,
1521 uint16_t tempD
, tempC
, tempB
, tempA
;
1523 rs2
= (rs
>> 32) & MIPSDSP_LLO
;
1524 rs0
= rs
& MIPSDSP_LLO
;
1525 rt2
= (rt
>> 32) & MIPSDSP_LLO
;
1526 rt0
= rt
& MIPSDSP_LLO
;
1528 tempD
= mipsdsp_trunc16_sat16_round(rs2
, env
);
1529 tempC
= mipsdsp_trunc16_sat16_round(rs0
, env
);
1530 tempB
= mipsdsp_trunc16_sat16_round(rt2
, env
);
1531 tempA
= mipsdsp_trunc16_sat16_round(rt0
, env
);
1533 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1536 target_ulong
helper_precrq_pw_l(target_ulong rs
, target_ulong rt
)
1538 uint32_t tempB
, tempA
;
1540 tempB
= (rs
>> 32) & MIPSDSP_LLO
;
1541 tempA
= (rt
>> 32) & MIPSDSP_LLO
;
1543 return MIPSDSP_RETURN64_32(tempB
, tempA
);
1547 target_ulong
helper_precrqu_s_qb_ph(target_ulong rs
, target_ulong rt
,
1550 uint8_t tempD
, tempC
, tempB
, tempA
;
1551 uint16_t rsh
, rsl
, rth
, rtl
;
1553 rsh
= (rs
& MIPSDSP_HI
) >> 16;
1554 rsl
= rs
& MIPSDSP_LO
;
1555 rth
= (rt
& MIPSDSP_HI
) >> 16;
1556 rtl
= rt
& MIPSDSP_LO
;
1558 tempD
= mipsdsp_sat8_reduce_precision(rsh
, env
);
1559 tempC
= mipsdsp_sat8_reduce_precision(rsl
, env
);
1560 tempB
= mipsdsp_sat8_reduce_precision(rth
, env
);
1561 tempA
= mipsdsp_sat8_reduce_precision(rtl
, env
);
1563 return MIPSDSP_RETURN32_8(tempD
, tempC
, tempB
, tempA
);
1566 #if defined(TARGET_MIPS64)
1567 target_ulong
helper_precrqu_s_ob_qh(target_ulong rs
, target_ulong rt
,
1571 uint16_t rs3
, rs2
, rs1
, rs0
;
1572 uint16_t rt3
, rt2
, rt1
, rt0
;
1578 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
1579 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
1581 temp
[7] = mipsdsp_sat8_reduce_precision(rs3
, env
);
1582 temp
[6] = mipsdsp_sat8_reduce_precision(rs2
, env
);
1583 temp
[5] = mipsdsp_sat8_reduce_precision(rs1
, env
);
1584 temp
[4] = mipsdsp_sat8_reduce_precision(rs0
, env
);
1585 temp
[3] = mipsdsp_sat8_reduce_precision(rt3
, env
);
1586 temp
[2] = mipsdsp_sat8_reduce_precision(rt2
, env
);
1587 temp
[1] = mipsdsp_sat8_reduce_precision(rt1
, env
);
1588 temp
[0] = mipsdsp_sat8_reduce_precision(rt0
, env
);
1590 for (i
= 0; i
< 8; i
++) {
1591 result
|= (uint64_t)temp
[i
] << (8 * i
);
1597 #define PRECEQ_PW(name, a, b) \
1598 target_ulong helper_preceq_pw_##name(target_ulong rt) \
1600 uint16_t tempB, tempA; \
1601 uint32_t tempBI, tempAI; \
1603 tempB = (rt >> a) & MIPSDSP_LO; \
1604 tempA = (rt >> b) & MIPSDSP_LO; \
1606 tempBI = (uint32_t)tempB << 16; \
1607 tempAI = (uint32_t)tempA << 16; \
1609 return MIPSDSP_RETURN64_32(tempBI, tempAI); \
1612 PRECEQ_PW(qhl
, 48, 32);
1613 PRECEQ_PW(qhr
, 16, 0);
1614 PRECEQ_PW(qhla
, 48, 16);
1615 PRECEQ_PW(qhra
, 32, 0);
1621 #define PRECEQU_PH(name, a, b) \
1622 target_ulong helper_precequ_ph_##name(target_ulong rt) \
1624 uint16_t tempB, tempA; \
1626 tempB = (rt >> a) & MIPSDSP_Q0; \
1627 tempA = (rt >> b) & MIPSDSP_Q0; \
1629 tempB = tempB << 7; \
1630 tempA = tempA << 7; \
1632 return MIPSDSP_RETURN32_16(tempB, tempA); \
1635 PRECEQU_PH(qbl
, 24, 16);
1636 PRECEQU_PH(qbr
, 8, 0);
1637 PRECEQU_PH(qbla
, 24, 8);
1638 PRECEQU_PH(qbra
, 16, 0);
1642 #if defined(TARGET_MIPS64)
1643 #define PRECEQU_QH(name, a, b, c, d) \
1644 target_ulong helper_precequ_qh_##name(target_ulong rt) \
1646 uint16_t tempD, tempC, tempB, tempA; \
1648 tempD = (rt >> a) & MIPSDSP_Q0; \
1649 tempC = (rt >> b) & MIPSDSP_Q0; \
1650 tempB = (rt >> c) & MIPSDSP_Q0; \
1651 tempA = (rt >> d) & MIPSDSP_Q0; \
1653 tempD = tempD << 7; \
1654 tempC = tempC << 7; \
1655 tempB = tempB << 7; \
1656 tempA = tempA << 7; \
1658 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1661 PRECEQU_QH(obl
, 56, 48, 40, 32);
1662 PRECEQU_QH(obr
, 24, 16, 8, 0);
1663 PRECEQU_QH(obla
, 56, 40, 24, 8);
1664 PRECEQU_QH(obra
, 48, 32, 16, 0);
1670 #define PRECEU_PH(name, a, b) \
1671 target_ulong helper_preceu_ph_##name(target_ulong rt) \
1673 uint16_t tempB, tempA; \
1675 tempB = (rt >> a) & MIPSDSP_Q0; \
1676 tempA = (rt >> b) & MIPSDSP_Q0; \
1678 return MIPSDSP_RETURN32_16(tempB, tempA); \
1681 PRECEU_PH(qbl
, 24, 16);
1682 PRECEU_PH(qbr
, 8, 0);
1683 PRECEU_PH(qbla
, 24, 8);
1684 PRECEU_PH(qbra
, 16, 0);
1688 #if defined(TARGET_MIPS64)
1689 #define PRECEU_QH(name, a, b, c, d) \
1690 target_ulong helper_preceu_qh_##name(target_ulong rt) \
1692 uint16_t tempD, tempC, tempB, tempA; \
1694 tempD = (rt >> a) & MIPSDSP_Q0; \
1695 tempC = (rt >> b) & MIPSDSP_Q0; \
1696 tempB = (rt >> c) & MIPSDSP_Q0; \
1697 tempA = (rt >> d) & MIPSDSP_Q0; \
1699 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1702 PRECEU_QH(obl
, 56, 48, 40, 32);
1703 PRECEU_QH(obr
, 24, 16, 8, 0);
1704 PRECEU_QH(obla
, 56, 40, 24, 8);
1705 PRECEU_QH(obra
, 48, 32, 16, 0);
1711 /** DSP GPR-Based Shift Sub-class insns **/
1712 #define SHIFT_QB(name, func) \
1713 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt) \
1715 uint8_t rt3, rt2, rt1, rt0; \
1719 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1721 rt3 = mipsdsp_##func(rt3, sa); \
1722 rt2 = mipsdsp_##func(rt2, sa); \
1723 rt1 = mipsdsp_##func(rt1, sa); \
1724 rt0 = mipsdsp_##func(rt0, sa); \
1726 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1729 #define SHIFT_QB_ENV(name, func) \
1730 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt,\
1731 CPUMIPSState *env) \
1733 uint8_t rt3, rt2, rt1, rt0; \
1737 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1739 rt3 = mipsdsp_##func(rt3, sa, env); \
1740 rt2 = mipsdsp_##func(rt2, sa, env); \
1741 rt1 = mipsdsp_##func(rt1, sa, env); \
1742 rt0 = mipsdsp_##func(rt0, sa, env); \
1744 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1747 SHIFT_QB_ENV(shll
, lshift8
);
1748 SHIFT_QB(shrl
, rshift_u8
);
1750 SHIFT_QB(shra
, rashift8
);
1751 SHIFT_QB(shra_r
, rnd8_rashift
);
1756 #if defined(TARGET_MIPS64)
1757 #define SHIFT_OB(name, func) \
1758 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1767 for (i = 0; i < 8; i++) { \
1768 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1769 rt_t[i] = mipsdsp_##func(rt_t[i], sa); \
1770 temp |= (uint64_t)rt_t[i] << (8 * i); \
1776 #define SHIFT_OB_ENV(name, func) \
1777 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1778 CPUMIPSState *env) \
1787 for (i = 0; i < 8; i++) { \
1788 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1789 rt_t[i] = mipsdsp_##func(rt_t[i], sa, env); \
1790 temp |= (uint64_t)rt_t[i] << (8 * i); \
1796 SHIFT_OB_ENV(shll
, lshift8
);
1797 SHIFT_OB(shrl
, rshift_u8
);
1799 SHIFT_OB(shra
, rashift8
);
1800 SHIFT_OB(shra_r
, rnd8_rashift
);
1807 #define SHIFT_PH(name, func) \
1808 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt, \
1809 CPUMIPSState *env) \
1811 uint16_t rth, rtl; \
1815 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1817 rth = mipsdsp_##func(rth, sa, env); \
1818 rtl = mipsdsp_##func(rtl, sa, env); \
1820 return MIPSDSP_RETURN32_16(rth, rtl); \
1823 SHIFT_PH(shll
, lshift16
);
1824 SHIFT_PH(shll_s
, sat16_lshift
);
1828 #if defined(TARGET_MIPS64)
1829 #define SHIFT_QH(name, func) \
1830 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa) \
1832 uint16_t rt3, rt2, rt1, rt0; \
1836 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1838 rt3 = mipsdsp_##func(rt3, sa); \
1839 rt2 = mipsdsp_##func(rt2, sa); \
1840 rt1 = mipsdsp_##func(rt1, sa); \
1841 rt0 = mipsdsp_##func(rt0, sa); \
1843 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1846 #define SHIFT_QH_ENV(name, func) \
1847 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa, \
1848 CPUMIPSState *env) \
1850 uint16_t rt3, rt2, rt1, rt0; \
1854 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1856 rt3 = mipsdsp_##func(rt3, sa, env); \
1857 rt2 = mipsdsp_##func(rt2, sa, env); \
1858 rt1 = mipsdsp_##func(rt1, sa, env); \
1859 rt0 = mipsdsp_##func(rt0, sa, env); \
1861 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1864 SHIFT_QH_ENV(shll
, lshift16
);
1865 SHIFT_QH_ENV(shll_s
, sat16_lshift
);
1867 SHIFT_QH(shrl
, rshift_u16
);
1868 SHIFT_QH(shra
, rashift16
);
1869 SHIFT_QH(shra_r
, rnd16_rashift
);
1876 #define SHIFT_W(name, func) \
1877 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1882 temp = mipsdsp_##func(rt, sa); \
1884 return (target_long)(int32_t)temp; \
1887 #define SHIFT_W_ENV(name, func) \
1888 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt, \
1889 CPUMIPSState *env) \
1894 temp = mipsdsp_##func(rt, sa, env); \
1896 return (target_long)(int32_t)temp; \
1899 SHIFT_W_ENV(shll_s
, sat32_lshift
);
1900 SHIFT_W(shra_r
, rnd32_rashift
);
1905 #if defined(TARGET_MIPS64)
1906 #define SHIFT_PW(name, func) \
1907 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa) \
1909 uint32_t rt1, rt0; \
1912 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1914 rt1 = mipsdsp_##func(rt1, sa); \
1915 rt0 = mipsdsp_##func(rt0, sa); \
1917 return MIPSDSP_RETURN64_32(rt1, rt0); \
1920 #define SHIFT_PW_ENV(name, func) \
1921 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa, \
1922 CPUMIPSState *env) \
1924 uint32_t rt1, rt0; \
1927 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1929 rt1 = mipsdsp_##func(rt1, sa, env); \
1930 rt0 = mipsdsp_##func(rt0, sa, env); \
1932 return MIPSDSP_RETURN64_32(rt1, rt0); \
1935 SHIFT_PW_ENV(shll
, lshift32
);
1936 SHIFT_PW_ENV(shll_s
, sat32_lshift
);
1938 SHIFT_PW(shra
, rashift32
);
1939 SHIFT_PW(shra_r
, rnd32_rashift
);
1946 #define SHIFT_PH(name, func) \
1947 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1949 uint16_t rth, rtl; \
1953 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1955 rth = mipsdsp_##func(rth, sa); \
1956 rtl = mipsdsp_##func(rtl, sa); \
1958 return MIPSDSP_RETURN32_16(rth, rtl); \
1961 SHIFT_PH(shrl
, rshift_u16
);
1962 SHIFT_PH(shra
, rashift16
);
1963 SHIFT_PH(shra_r
, rnd16_rashift
);
1967 /** DSP Multiply Sub-class insns **/
1968 /* Return value made up by two 16bits value.
1969 * FIXME give the macro a better name.
1971 #define MUL_RETURN32_16_PH(name, func, \
1972 rsmov1, rsmov2, rsfilter, \
1973 rtmov1, rtmov2, rtfilter) \
1974 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1975 CPUMIPSState *env) \
1977 uint16_t rsB, rsA, rtB, rtA; \
1979 rsB = (rs >> rsmov1) & rsfilter; \
1980 rsA = (rs >> rsmov2) & rsfilter; \
1981 rtB = (rt >> rtmov1) & rtfilter; \
1982 rtA = (rt >> rtmov2) & rtfilter; \
1984 rsB = mipsdsp_##func(rsB, rtB, env); \
1985 rsA = mipsdsp_##func(rsA, rtA, env); \
1987 return MIPSDSP_RETURN32_16(rsB, rsA); \
1990 MUL_RETURN32_16_PH(muleu_s_ph_qbl
, mul_u8_u16
, \
1991 24, 16, MIPSDSP_Q0
, \
1993 MUL_RETURN32_16_PH(muleu_s_ph_qbr
, mul_u8_u16
, \
1996 MUL_RETURN32_16_PH(mulq_rs_ph
, rndq15_mul_q15_q15
, \
1997 16, 0, MIPSDSP_LO
, \
1999 MUL_RETURN32_16_PH(mul_ph
, mul_i16_i16
, \
2000 16, 0, MIPSDSP_LO
, \
2002 MUL_RETURN32_16_PH(mul_s_ph
, sat16_mul_i16_i16
, \
2003 16, 0, MIPSDSP_LO
, \
2005 MUL_RETURN32_16_PH(mulq_s_ph
, sat16_mul_q15_q15
, \
2006 16, 0, MIPSDSP_LO
, \
2009 #undef MUL_RETURN32_16_PH
2011 #define MUL_RETURN32_32_ph(name, func, movbits) \
2012 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2013 CPUMIPSState *env) \
2018 rsh = (rs >> movbits) & MIPSDSP_LO; \
2019 rth = (rt >> movbits) & MIPSDSP_LO; \
2020 temp = mipsdsp_##func(rsh, rth, env); \
2022 return (target_long)(int32_t)temp; \
2025 MUL_RETURN32_32_ph(muleq_s_w_phl
, mul_q15_q15_overflowflag21
, 16);
2026 MUL_RETURN32_32_ph(muleq_s_w_phr
, mul_q15_q15_overflowflag21
, 0);
2028 #undef MUL_RETURN32_32_ph
2030 #define MUL_VOID_PH(name, use_ac_env) \
2031 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2032 CPUMIPSState *env) \
2034 int16_t rsh, rsl, rth, rtl; \
2035 int32_t tempB, tempA; \
2036 int64_t acc, dotp; \
2038 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2039 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2041 if (use_ac_env == 1) { \
2042 tempB = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2043 tempA = mipsdsp_mul_q15_q15(ac, rsl, rtl, env); \
2045 tempB = mipsdsp_mul_u16_u16(rsh, rth); \
2046 tempA = mipsdsp_mul_u16_u16(rsl, rtl); \
2049 dotp = (int64_t)tempB - (int64_t)tempA; \
2050 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2051 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2052 dotp = dotp + acc; \
2053 env->active_tc.HI[ac] = (target_long)(int32_t) \
2054 ((dotp & MIPSDSP_LHI) >> 32); \
2055 env->active_tc.LO[ac] = (target_long)(int32_t)(dotp & MIPSDSP_LLO); \
2058 MUL_VOID_PH(mulsaq_s_w_ph
, 1);
2059 MUL_VOID_PH(mulsa_w_ph
, 0);
2063 #if defined(TARGET_MIPS64)
2064 #define MUL_RETURN64_16_QH(name, func, \
2065 rsmov1, rsmov2, rsmov3, rsmov4, rsfilter, \
2066 rtmov1, rtmov2, rtmov3, rtmov4, rtfilter) \
2067 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2068 CPUMIPSState *env) \
2070 uint16_t rs3, rs2, rs1, rs0; \
2071 uint16_t rt3, rt2, rt1, rt0; \
2072 uint16_t tempD, tempC, tempB, tempA; \
2074 rs3 = (rs >> rsmov1) & rsfilter; \
2075 rs2 = (rs >> rsmov2) & rsfilter; \
2076 rs1 = (rs >> rsmov3) & rsfilter; \
2077 rs0 = (rs >> rsmov4) & rsfilter; \
2078 rt3 = (rt >> rtmov1) & rtfilter; \
2079 rt2 = (rt >> rtmov2) & rtfilter; \
2080 rt1 = (rt >> rtmov3) & rtfilter; \
2081 rt0 = (rt >> rtmov4) & rtfilter; \
2083 tempD = mipsdsp_##func(rs3, rt3, env); \
2084 tempC = mipsdsp_##func(rs2, rt2, env); \
2085 tempB = mipsdsp_##func(rs1, rt1, env); \
2086 tempA = mipsdsp_##func(rs0, rt0, env); \
2088 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
2091 MUL_RETURN64_16_QH(muleu_s_qh_obl
, mul_u8_u16
, \
2092 56, 48, 40, 32, MIPSDSP_Q0
, \
2093 48, 32, 16, 0, MIPSDSP_LO
);
2094 MUL_RETURN64_16_QH(muleu_s_qh_obr
, mul_u8_u16
, \
2095 24, 16, 8, 0, MIPSDSP_Q0
, \
2096 48, 32, 16, 0, MIPSDSP_LO
);
2097 MUL_RETURN64_16_QH(mulq_rs_qh
, rndq15_mul_q15_q15
, \
2098 48, 32, 16, 0, MIPSDSP_LO
, \
2099 48, 32, 16, 0, MIPSDSP_LO
);
2101 #undef MUL_RETURN64_16_QH
2103 #define MUL_RETURN64_32_QH(name, \
2106 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2107 CPUMIPSState *env) \
2109 uint16_t rsB, rsA; \
2110 uint16_t rtB, rtA; \
2111 uint32_t tempB, tempA; \
2113 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2114 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2115 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2116 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2118 tempB = mipsdsp_mul_q15_q15(5, rsB, rtB, env); \
2119 tempA = mipsdsp_mul_q15_q15(5, rsA, rtA, env); \
2121 return ((uint64_t)tempB << 32) | (uint64_t)tempA; \
2124 MUL_RETURN64_32_QH(muleq_s_pw_qhl
, 48, 32, 48, 32);
2125 MUL_RETURN64_32_QH(muleq_s_pw_qhr
, 16, 0, 16, 0);
2127 #undef MUL_RETURN64_32_QH
2129 void helper_mulsaq_s_w_qh(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2132 int16_t rs3
, rs2
, rs1
, rs0
;
2133 int16_t rt3
, rt2
, rt1
, rt0
;
2134 int32_t tempD
, tempC
, tempB
, tempA
;
2139 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
2140 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
2142 tempD
= mipsdsp_mul_q15_q15(ac
, rs3
, rt3
, env
);
2143 tempC
= mipsdsp_mul_q15_q15(ac
, rs2
, rt2
, env
);
2144 tempB
= mipsdsp_mul_q15_q15(ac
, rs1
, rt1
, env
);
2145 tempA
= mipsdsp_mul_q15_q15(ac
, rs0
, rt0
, env
);
2147 temp
[0] = ((int32_t)tempD
- (int32_t)tempC
) +
2148 ((int32_t)tempB
- (int32_t)tempA
);
2149 temp
[0] = (int64_t)(temp
[0] << 30) >> 30;
2150 if (((temp
[0] >> 33) & 0x01) == 0) {
2156 acc
[0] = env
->active_tc
.LO
[ac
];
2157 acc
[1] = env
->active_tc
.HI
[ac
];
2159 temp_sum
= acc
[0] + temp
[0];
2160 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2161 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2167 env
->active_tc
.HI
[ac
] = acc
[1];
2168 env
->active_tc
.LO
[ac
] = acc
[0];
2172 #define DP_QB(name, func, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2173 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2174 CPUMIPSState *env) \
2178 uint16_t tempB, tempA; \
2179 uint64_t tempC, dotp; \
2181 rs3 = (rs >> rsmov1) & MIPSDSP_Q0; \
2182 rs2 = (rs >> rsmov2) & MIPSDSP_Q0; \
2183 rt3 = (rt >> rtmov1) & MIPSDSP_Q0; \
2184 rt2 = (rt >> rtmov2) & MIPSDSP_Q0; \
2185 tempB = mipsdsp_##func(rs3, rt3); \
2186 tempA = mipsdsp_##func(rs2, rt2); \
2187 dotp = (int64_t)tempB + (int64_t)tempA; \
2189 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2190 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2193 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2194 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2198 env->active_tc.HI[ac] = (target_long)(int32_t) \
2199 ((tempC & MIPSDSP_LHI) >> 32); \
2200 env->active_tc.LO[ac] = (target_long)(int32_t)(tempC & MIPSDSP_LLO); \
2203 DP_QB(dpau_h_qbl
, mul_u8_u8
, 1, 24, 16, 24, 16);
2204 DP_QB(dpau_h_qbr
, mul_u8_u8
, 1, 8, 0, 8, 0);
2205 DP_QB(dpsu_h_qbl
, mul_u8_u8
, 0, 24, 16, 24, 16);
2206 DP_QB(dpsu_h_qbr
, mul_u8_u8
, 0, 8, 0, 8, 0);
2210 #if defined(TARGET_MIPS64)
2211 #define DP_OB(name, add_sub, \
2212 rsmov1, rsmov2, rsmov3, rsmov4, \
2213 rtmov1, rtmov2, rtmov3, rtmov4) \
2214 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2215 CPUMIPSState *env) \
2217 uint8_t rsD, rsC, rsB, rsA; \
2218 uint8_t rtD, rtC, rtB, rtA; \
2219 uint16_t tempD, tempC, tempB, tempA; \
2222 uint64_t temp_sum; \
2227 rsD = (rs >> rsmov1) & MIPSDSP_Q0; \
2228 rsC = (rs >> rsmov2) & MIPSDSP_Q0; \
2229 rsB = (rs >> rsmov3) & MIPSDSP_Q0; \
2230 rsA = (rs >> rsmov4) & MIPSDSP_Q0; \
2231 rtD = (rt >> rtmov1) & MIPSDSP_Q0; \
2232 rtC = (rt >> rtmov2) & MIPSDSP_Q0; \
2233 rtB = (rt >> rtmov3) & MIPSDSP_Q0; \
2234 rtA = (rt >> rtmov4) & MIPSDSP_Q0; \
2236 tempD = mipsdsp_mul_u8_u8(rsD, rtD); \
2237 tempC = mipsdsp_mul_u8_u8(rsC, rtC); \
2238 tempB = mipsdsp_mul_u8_u8(rsB, rtB); \
2239 tempA = mipsdsp_mul_u8_u8(rsA, rtA); \
2241 temp[0] = (uint64_t)tempD + (uint64_t)tempC + \
2242 (uint64_t)tempB + (uint64_t)tempA; \
2244 acc[0] = env->active_tc.LO[ac]; \
2245 acc[1] = env->active_tc.HI[ac]; \
2248 temp_sum = acc[0] + temp[0]; \
2249 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2250 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2253 temp[0] = temp_sum; \
2254 temp[1] = acc[1] + temp[1]; \
2256 temp_sum = acc[0] - temp[0]; \
2257 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2260 temp[0] = temp_sum; \
2261 temp[1] = acc[1] - temp[1]; \
2264 env->active_tc.HI[ac] = temp[1]; \
2265 env->active_tc.LO[ac] = temp[0]; \
2268 DP_OB(dpau_h_obl
, 1, 56, 48, 40, 32, 56, 48, 40, 32);
2269 DP_OB(dpau_h_obr
, 1, 24, 16, 8, 0, 24, 16, 8, 0);
2270 DP_OB(dpsu_h_obl
, 0, 56, 48, 40, 32, 56, 48, 40, 32);
2271 DP_OB(dpsu_h_obr
, 0, 24, 16, 8, 0, 24, 16, 8, 0);
2276 #define DP_NOFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2277 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2278 CPUMIPSState *env) \
2280 int16_t rsB, rsA, rtB, rtA; \
2281 int32_t tempA, tempB; \
2284 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2285 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2286 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2287 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2289 tempB = (int32_t)rsB * (int32_t)rtB; \
2290 tempA = (int32_t)rsA * (int32_t)rtA; \
2292 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2293 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2296 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2298 acc = acc - ((int64_t)tempB + (int64_t)tempA); \
2301 env->active_tc.HI[ac] = (target_long)(int32_t)((acc & MIPSDSP_LHI) >> 32); \
2302 env->active_tc.LO[ac] = (target_long)(int32_t)(acc & MIPSDSP_LLO); \
2305 DP_NOFUNC_PH(dpa_w_ph
, 1, 16, 0, 16, 0);
2306 DP_NOFUNC_PH(dpax_w_ph
, 1, 16, 0, 0, 16);
2307 DP_NOFUNC_PH(dps_w_ph
, 0, 16, 0, 16, 0);
2308 DP_NOFUNC_PH(dpsx_w_ph
, 0, 16, 0, 0, 16);
2311 #define DP_HASFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2312 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2313 CPUMIPSState *env) \
2315 int16_t rsB, rsA, rtB, rtA; \
2316 int32_t tempB, tempA; \
2317 int64_t acc, dotp; \
2319 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2320 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2321 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2322 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2324 tempB = mipsdsp_mul_q15_q15(ac, rsB, rtB, env); \
2325 tempA = mipsdsp_mul_q15_q15(ac, rsA, rtA, env); \
2327 dotp = (int64_t)tempB + (int64_t)tempA; \
2328 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2329 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2337 env->active_tc.HI[ac] = (target_long)(int32_t) \
2338 ((acc & MIPSDSP_LHI) >> 32); \
2339 env->active_tc.LO[ac] = (target_long)(int32_t) \
2340 (acc & MIPSDSP_LLO); \
2343 DP_HASFUNC_PH(dpaq_s_w_ph
, 1, 16, 0, 16, 0);
2344 DP_HASFUNC_PH(dpaqx_s_w_ph
, 1, 16, 0, 0, 16);
2345 DP_HASFUNC_PH(dpsq_s_w_ph
, 0, 16, 0, 16, 0);
2346 DP_HASFUNC_PH(dpsqx_s_w_ph
, 0, 16, 0, 0, 16);
2348 #undef DP_HASFUNC_PH
2350 #define DP_128OPERATION_PH(name, is_add) \
2351 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2352 CPUMIPSState *env) \
2354 int16_t rsh, rsl, rth, rtl; \
2355 int32_t tempB, tempA, tempC62_31, tempC63; \
2356 int64_t acc, dotp, tempC; \
2358 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2359 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2361 tempB = mipsdsp_mul_q15_q15(ac, rsh, rtl, env); \
2362 tempA = mipsdsp_mul_q15_q15(ac, rsl, rth, env); \
2364 dotp = (int64_t)tempB + (int64_t)tempA; \
2365 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2366 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2368 tempC = acc + dotp; \
2370 tempC = acc - dotp; \
2372 tempC63 = (tempC >> 63) & 0x01; \
2373 tempC62_31 = (tempC >> 31) & 0xFFFFFFFF; \
2375 if ((tempC63 == 0) && (tempC62_31 != 0x00000000)) { \
2376 tempC = 0x7FFFFFFF; \
2377 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2380 if ((tempC63 == 1) && (tempC62_31 != 0xFFFFFFFF)) { \
2381 tempC = (int64_t)(int32_t)0x80000000; \
2382 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2385 env->active_tc.HI[ac] = (target_long)(int32_t) \
2386 ((tempC & MIPSDSP_LHI) >> 32); \
2387 env->active_tc.LO[ac] = (target_long)(int32_t) \
2388 (tempC & MIPSDSP_LLO); \
2391 DP_128OPERATION_PH(dpaqx_sa_w_ph
, 1);
2392 DP_128OPERATION_PH(dpsqx_sa_w_ph
, 0);
2394 #undef DP_128OPERATION_HP
2396 #if defined(TARGET_MIPS64)
2397 #define DP_QH(name, is_add, use_ac_env) \
2398 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2399 CPUMIPSState *env) \
2401 int32_t rs3, rs2, rs1, rs0; \
2402 int32_t rt3, rt2, rt1, rt0; \
2403 int32_t tempD, tempC, tempB, tempA; \
2408 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2409 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2412 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env); \
2413 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env); \
2414 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env); \
2415 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env); \
2417 tempD = mipsdsp_mul_u16_u16(rs3, rt3); \
2418 tempC = mipsdsp_mul_u16_u16(rs2, rt2); \
2419 tempB = mipsdsp_mul_u16_u16(rs1, rt1); \
2420 tempA = mipsdsp_mul_u16_u16(rs0, rt0); \
2423 temp[0] = (int64_t)tempD + (int64_t)tempC + \
2424 (int64_t)tempB + (int64_t)tempA; \
2426 if (temp[0] >= 0) { \
2432 acc[1] = env->active_tc.HI[ac]; \
2433 acc[0] = env->active_tc.LO[ac]; \
2436 temp_sum = acc[0] + temp[0]; \
2437 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2438 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2439 acc[1] = acc[1] + 1; \
2441 temp[0] = temp_sum; \
2442 temp[1] = acc[1] + temp[1]; \
2444 temp_sum = acc[0] - temp[0]; \
2445 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2446 acc[1] = acc[1] - 1; \
2448 temp[0] = temp_sum; \
2449 temp[1] = acc[1] - temp[1]; \
2452 env->active_tc.HI[ac] = temp[1]; \
2453 env->active_tc.LO[ac] = temp[0]; \
2456 DP_QH(dpa_w_qh
, 1, 0);
2457 DP_QH(dpaq_s_w_qh
, 1, 1);
2458 DP_QH(dps_w_qh
, 0, 0);
2459 DP_QH(dpsq_s_w_qh
, 0, 1);
2465 #define DP_L_W(name, is_add) \
2466 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2467 CPUMIPSState *env) \
2470 int64_t dotp, acc; \
2474 dotp = mipsdsp_mul_q31_q31(ac, rs, rt, env); \
2475 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2476 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2478 temp = acc + dotp; \
2479 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2480 temp, (0x01ull << 63)); \
2482 temp = acc - dotp; \
2483 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2484 temp, (0x01ull << 63)); \
2488 temp63 = (temp >> 63) & 0x01; \
2489 if (temp63 == 1) { \
2490 temp = (0x01ull << 63) - 1; \
2492 temp = 0x01ull << 63; \
2495 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2498 env->active_tc.HI[ac] = (target_long)(int32_t) \
2499 ((temp & MIPSDSP_LHI) >> 32); \
2500 env->active_tc.LO[ac] = (target_long)(int32_t) \
2501 (temp & MIPSDSP_LLO); \
2504 DP_L_W(dpaq_sa_l_w
, 1);
2505 DP_L_W(dpsq_sa_l_w
, 0);
2509 #if defined(TARGET_MIPS64)
2510 #define DP_L_PW(name, func) \
2511 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2512 CPUMIPSState *env) \
2516 int64_t tempB[2], tempA[2]; \
2524 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2525 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2527 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env); \
2528 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env); \
2530 if (tempB[0] >= 0) { \
2536 if (tempA[0] >= 0) { \
2542 temp_sum = tempB[0] + tempA[0]; \
2543 if (((uint64_t)temp_sum < (uint64_t)tempB[0]) && \
2544 ((uint64_t)temp_sum < (uint64_t)tempA[0])) { \
2547 temp[0] = temp_sum; \
2548 temp[1] += tempB[1] + tempA[1]; \
2550 mipsdsp_##func(acc, ac, temp, env); \
2552 env->active_tc.HI[ac] = acc[1]; \
2553 env->active_tc.LO[ac] = acc[0]; \
2556 DP_L_PW(dpaq_sa_l_pw
, sat64_acc_add_q63
);
2557 DP_L_PW(dpsq_sa_l_pw
, sat64_acc_sub_q63
);
2561 void helper_mulsaq_s_l_pw(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2566 int64_t tempB
[2], tempA
[2];
2571 rs1
= (rs
>> 32) & MIPSDSP_LLO
;
2572 rs0
= rs
& MIPSDSP_LLO
;
2573 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
2574 rt0
= rt
& MIPSDSP_LLO
;
2576 tempB
[0] = mipsdsp_mul_q31_q31(ac
, rs1
, rt1
, env
);
2577 tempA
[0] = mipsdsp_mul_q31_q31(ac
, rs0
, rt0
, env
);
2579 if (tempB
[0] >= 0) {
2585 if (tempA
[0] >= 0) {
2591 acc
[0] = env
->active_tc
.LO
[ac
];
2592 acc
[1] = env
->active_tc
.HI
[ac
];
2594 temp_sum
= tempB
[0] - tempA
[0];
2595 if ((uint64_t)temp_sum
> (uint64_t)tempB
[0]) {
2599 temp
[1] = tempB
[1] - tempA
[1];
2601 if ((temp
[1] & 0x01) == 0) {
2607 temp_sum
= acc
[0] + temp
[0];
2608 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2609 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2615 env
->active_tc
.HI
[ac
] = acc
[1];
2616 env
->active_tc
.LO
[ac
] = acc
[0];
2620 #define MAQ_S_W(name, mov) \
2621 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2622 CPUMIPSState *env) \
2626 int64_t tempL, acc; \
2628 rsh = (rs >> mov) & MIPSDSP_LO; \
2629 rth = (rt >> mov) & MIPSDSP_LO; \
2630 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2631 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2632 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2633 tempL = (int64_t)tempA + acc; \
2634 env->active_tc.HI[ac] = (target_long)(int32_t) \
2635 ((tempL & MIPSDSP_LHI) >> 32); \
2636 env->active_tc.LO[ac] = (target_long)(int32_t) \
2637 (tempL & MIPSDSP_LLO); \
2640 MAQ_S_W(maq_s_w_phl
, 16);
2641 MAQ_S_W(maq_s_w_phr
, 0);
2645 #define MAQ_SA_W(name, mov) \
2646 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2647 CPUMIPSState *env) \
2652 rsh = (rs >> mov) & MIPSDSP_LO; \
2653 rth = (rt >> mov) & MIPSDSP_LO; \
2654 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2655 tempA = mipsdsp_sat32_acc_q31(ac, tempA, env); \
2657 env->active_tc.HI[ac] = (target_long)(int32_t)(((int64_t)tempA & \
2658 MIPSDSP_LHI) >> 32); \
2659 env->active_tc.LO[ac] = (target_long)(int32_t)((int64_t)tempA & \
2663 MAQ_SA_W(maq_sa_w_phl
, 16);
2664 MAQ_SA_W(maq_sa_w_phr
, 0);
2668 #define MULQ_W(name, addvar) \
2669 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2670 CPUMIPSState *env) \
2672 int32_t rs_t, rt_t; \
2676 rs_t = rs & MIPSDSP_LLO; \
2677 rt_t = rt & MIPSDSP_LLO; \
2679 if ((rs_t == 0x80000000) && (rt_t == 0x80000000)) { \
2680 tempL = 0x7FFFFFFF00000000ull; \
2681 set_DSPControl_overflow_flag(1, 21, env); \
2683 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2686 tempI = (tempL & MIPSDSP_LHI) >> 32; \
2688 return (target_long)(int32_t)tempI; \
2691 MULQ_W(mulq_s_w
, 0);
2692 MULQ_W(mulq_rs_w
, 0x80000000ull
);
2696 #if defined(TARGET_MIPS64)
2698 #define MAQ_S_W_QH(name, mov) \
2699 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2700 CPUMIPSState *env) \
2702 int16_t rs_t, rt_t; \
2711 rs_t = (rs >> mov) & MIPSDSP_LO; \
2712 rt_t = (rt >> mov) & MIPSDSP_LO; \
2713 temp_mul = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2715 temp[0] = (int64_t)temp_mul; \
2716 if (temp[0] >= 0) { \
2722 acc[0] = env->active_tc.LO[ac]; \
2723 acc[1] = env->active_tc.HI[ac]; \
2725 temp_sum = acc[0] + temp[0]; \
2726 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2727 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2730 acc[0] = temp_sum; \
2731 acc[1] += temp[1]; \
2733 env->active_tc.HI[ac] = acc[1]; \
2734 env->active_tc.LO[ac] = acc[0]; \
2737 MAQ_S_W_QH(maq_s_w_qhll
, 48);
2738 MAQ_S_W_QH(maq_s_w_qhlr
, 32);
2739 MAQ_S_W_QH(maq_s_w_qhrl
, 16);
2740 MAQ_S_W_QH(maq_s_w_qhrr
, 0);
2744 #define MAQ_SA_W(name, mov) \
2745 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2746 CPUMIPSState *env) \
2748 int16_t rs_t, rt_t; \
2752 rs_t = (rs >> mov) & MIPSDSP_LO; \
2753 rt_t = (rt >> mov) & MIPSDSP_LO; \
2754 temp = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2755 temp = mipsdsp_sat32_acc_q31(ac, temp, env); \
2757 acc[0] = (int64_t)(int32_t)temp; \
2758 if (acc[0] >= 0) { \
2764 env->active_tc.HI[ac] = acc[1]; \
2765 env->active_tc.LO[ac] = acc[0]; \
2768 MAQ_SA_W(maq_sa_w_qhll
, 48);
2769 MAQ_SA_W(maq_sa_w_qhlr
, 32);
2770 MAQ_SA_W(maq_sa_w_qhrl
, 16);
2771 MAQ_SA_W(maq_sa_w_qhrr
, 0);
2775 #define MAQ_S_L_PW(name, mov) \
2776 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2777 CPUMIPSState *env) \
2779 int32_t rs_t, rt_t; \
2787 rs_t = (rs >> mov) & MIPSDSP_LLO; \
2788 rt_t = (rt >> mov) & MIPSDSP_LLO; \
2790 temp[0] = mipsdsp_mul_q31_q31(ac, rs_t, rt_t, env); \
2791 if (temp[0] >= 0) { \
2797 acc[0] = env->active_tc.LO[ac]; \
2798 acc[1] = env->active_tc.HI[ac]; \
2800 temp_sum = acc[0] + temp[0]; \
2801 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2802 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2805 acc[0] = temp_sum; \
2806 acc[1] += temp[1]; \
2808 env->active_tc.HI[ac] = acc[1]; \
2809 env->active_tc.LO[ac] = acc[0]; \
2812 MAQ_S_L_PW(maq_s_l_pwl
, 32);
2813 MAQ_S_L_PW(maq_s_l_pwr
, 0);
2817 #define DM_OPERATE(name, func, is_add, sigext) \
2818 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2819 CPUMIPSState *env) \
2823 int64_t tempBL[2], tempAL[2]; \
2831 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2832 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2835 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2836 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2838 if (tempBL[0] >= 0) { \
2841 tempBL[1] = ~0ull; \
2844 if (tempAL[0] >= 0) { \
2847 tempAL[1] = ~0ull; \
2850 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2851 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2856 acc[1] = env->active_tc.HI[ac]; \
2857 acc[0] = env->active_tc.LO[ac]; \
2859 temp_sum = tempBL[0] + tempAL[0]; \
2860 if (((uint64_t)temp_sum < (uint64_t)tempBL[0]) && \
2861 ((uint64_t)temp_sum < (uint64_t)tempAL[0])) { \
2864 temp[0] = temp_sum; \
2865 temp[1] += tempBL[1] + tempAL[1]; \
2868 temp_sum = acc[0] + temp[0]; \
2869 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2870 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2873 temp[0] = temp_sum; \
2874 temp[1] = acc[1] + temp[1]; \
2876 temp_sum = acc[0] - temp[0]; \
2877 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2880 temp[0] = temp_sum; \
2881 temp[1] = acc[1] - temp[1]; \
2884 env->active_tc.HI[ac] = temp[1]; \
2885 env->active_tc.LO[ac] = temp[0]; \
2888 DM_OPERATE(dmadd
, mul_i32_i32
, 1, 1);
2889 DM_OPERATE(dmaddu
, mul_u32_u32
, 1, 0);
2890 DM_OPERATE(dmsub
, mul_i32_i32
, 0, 1);
2891 DM_OPERATE(dmsubu
, mul_u32_u32
, 0, 0);
2895 /** DSP Bit/Manipulation Sub-class insns **/
2896 target_ulong
helper_bitrev(target_ulong rt
)
2902 temp
= rt
& MIPSDSP_LO
;
2904 for (i
= 0; i
< 16; i
++) {
2905 rd
= (rd
<< 1) | (temp
& 1);
2909 return (target_ulong
)rd
;
2912 #define BIT_INSV(name, posfilter, ret_type) \
2913 target_ulong helper_##name(CPUMIPSState *env, target_ulong rs, \
2916 uint32_t pos, size, msb, lsb; \
2917 uint32_t const sizefilter = 0x3F; \
2918 target_ulong temp; \
2919 target_ulong dspc; \
2921 dspc = env->active_tc.DSPControl; \
2923 pos = dspc & posfilter; \
2924 size = (dspc >> 7) & sizefilter; \
2926 msb = pos + size - 1; \
2929 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2933 temp = deposit64(rt, pos, size, rs); \
2935 return (target_long)(ret_type)temp; \
2938 BIT_INSV(insv
, 0x1F, int32_t);
2939 #ifdef TARGET_MIPS64
2940 BIT_INSV(dinsv
, 0x7F, target_long
);
2946 /** DSP Compare-Pick Sub-class insns **/
2947 #define CMP_HAS_RET(name, func, split_num, filter, bit_size) \
2948 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
2950 uint32_t rs_t, rt_t; \
2952 uint32_t temp = 0; \
2955 for (i = 0; i < split_num; i++) { \
2956 rs_t = (rs >> (bit_size * i)) & filter; \
2957 rt_t = (rt >> (bit_size * i)) & filter; \
2958 cc = mipsdsp_##func(rs_t, rt_t); \
2962 return (target_ulong)temp; \
2965 CMP_HAS_RET(cmpgu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2966 CMP_HAS_RET(cmpgu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2967 CMP_HAS_RET(cmpgu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
2969 #ifdef TARGET_MIPS64
2970 CMP_HAS_RET(cmpgu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
2971 CMP_HAS_RET(cmpgu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
2972 CMP_HAS_RET(cmpgu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
2978 #define CMP_NO_RET(name, func, split_num, filter, bit_size) \
2979 void helper_##name(target_ulong rs, target_ulong rt, \
2980 CPUMIPSState *env) \
2982 int##bit_size##_t rs_t, rt_t; \
2983 int##bit_size##_t flag = 0; \
2984 int##bit_size##_t cc; \
2987 for (i = 0; i < split_num; i++) { \
2988 rs_t = (rs >> (bit_size * i)) & filter; \
2989 rt_t = (rt >> (bit_size * i)) & filter; \
2991 cc = mipsdsp_##func((int32_t)rs_t, (int32_t)rt_t); \
2995 set_DSPControl_24(flag, split_num, env); \
2998 CMP_NO_RET(cmpu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2999 CMP_NO_RET(cmpu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
3000 CMP_NO_RET(cmpu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
3002 CMP_NO_RET(cmp_eq_ph
, cmp_eq
, 2, MIPSDSP_LO
, 16);
3003 CMP_NO_RET(cmp_lt_ph
, cmp_lt
, 2, MIPSDSP_LO
, 16);
3004 CMP_NO_RET(cmp_le_ph
, cmp_le
, 2, MIPSDSP_LO
, 16);
3006 #ifdef TARGET_MIPS64
3007 CMP_NO_RET(cmpu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
3008 CMP_NO_RET(cmpu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
3009 CMP_NO_RET(cmpu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
3011 CMP_NO_RET(cmp_eq_qh
, cmp_eq
, 4, MIPSDSP_LO
, 16);
3012 CMP_NO_RET(cmp_lt_qh
, cmp_lt
, 4, MIPSDSP_LO
, 16);
3013 CMP_NO_RET(cmp_le_qh
, cmp_le
, 4, MIPSDSP_LO
, 16);
3015 CMP_NO_RET(cmp_eq_pw
, cmp_eq
, 2, MIPSDSP_LLO
, 32);
3016 CMP_NO_RET(cmp_lt_pw
, cmp_lt
, 2, MIPSDSP_LLO
, 32);
3017 CMP_NO_RET(cmp_le_pw
, cmp_le
, 2, MIPSDSP_LLO
, 32);
3021 #if defined(TARGET_MIPS64)
3023 #define CMPGDU_OB(name) \
3024 target_ulong helper_cmpgdu_##name##_ob(target_ulong rs, target_ulong rt, \
3025 CPUMIPSState *env) \
3028 uint8_t rs_t, rt_t; \
3033 for (i = 0; i < 8; i++) { \
3034 rs_t = (rs >> (8 * i)) & MIPSDSP_Q0; \
3035 rt_t = (rt >> (8 * i)) & MIPSDSP_Q0; \
3037 if (mipsdsp_cmpu_##name(rs_t, rt_t)) { \
3038 cond |= 0x01 << i; \
3042 set_DSPControl_24(cond, 8, env); \
3044 return (uint64_t)cond; \
3053 #define PICK_INSN(name, split_num, filter, bit_size, ret32bit) \
3054 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
3055 CPUMIPSState *env) \
3057 uint32_t rs_t, rt_t; \
3061 target_ulong result = 0; \
3063 dsp = env->active_tc.DSPControl; \
3064 for (i = 0; i < split_num; i++) { \
3065 rs_t = (rs >> (bit_size * i)) & filter; \
3066 rt_t = (rt >> (bit_size * i)) & filter; \
3067 cc = (dsp >> (24 + i)) & 0x01; \
3068 cc = cc == 1 ? rs_t : rt_t; \
3070 result |= (target_ulong)cc << (bit_size * i); \
3074 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3080 PICK_INSN(pick_qb
, 4, MIPSDSP_Q0
, 8, 1);
3081 PICK_INSN(pick_ph
, 2, MIPSDSP_LO
, 16, 1);
3083 #ifdef TARGET_MIPS64
3084 PICK_INSN(pick_ob
, 8, MIPSDSP_Q0
, 8, 0);
3085 PICK_INSN(pick_qh
, 4, MIPSDSP_LO
, 16, 0);
3086 PICK_INSN(pick_pw
, 2, MIPSDSP_LLO
, 32, 0);
3090 target_ulong
helper_packrl_ph(target_ulong rs
, target_ulong rt
)
3094 rsl
= rs
& MIPSDSP_LO
;
3095 rth
= (rt
& MIPSDSP_HI
) >> 16;
3097 return (target_long
)(int32_t)((rsl
<< 16) | rth
);
3100 #if defined(TARGET_MIPS64)
3101 target_ulong
helper_packrl_pw(target_ulong rs
, target_ulong rt
)
3105 rs0
= rs
& MIPSDSP_LLO
;
3106 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
3108 return ((uint64_t)rs0
<< 32) | (uint64_t)rt1
;
3112 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3113 target_ulong
helper_extr_w(target_ulong ac
, target_ulong shift
,
3119 shift
= shift
& 0x1F;
3121 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3122 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3123 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3124 set_DSPControl_overflow_flag(1, 23, env
);
3127 tempI
= (tempDL
[0] >> 1) & MIPSDSP_LLO
;
3130 if (tempDL
[0] == 0) {
3134 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3135 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3136 set_DSPControl_overflow_flag(1, 23, env
);
3139 return (target_long
)tempI
;
3142 target_ulong
helper_extr_r_w(target_ulong ac
, target_ulong shift
,
3147 shift
= shift
& 0x1F;
3149 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3150 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3151 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3152 set_DSPControl_overflow_flag(1, 23, env
);
3156 if (tempDL
[0] == 0) {
3160 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3161 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3162 set_DSPControl_overflow_flag(1, 23, env
);
3165 return (target_long
)(int32_t)(tempDL
[0] >> 1);
3168 target_ulong
helper_extr_rs_w(target_ulong ac
, target_ulong shift
,
3171 int32_t tempI
, temp64
;
3174 shift
= shift
& 0x1F;
3176 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3177 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3178 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3179 set_DSPControl_overflow_flag(1, 23, env
);
3182 if (tempDL
[0] == 0) {
3185 tempI
= tempDL
[0] >> 1;
3187 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3188 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3189 temp64
= tempDL
[1] & 0x01;
3195 set_DSPControl_overflow_flag(1, 23, env
);
3198 return (target_long
)tempI
;
3201 #if defined(TARGET_MIPS64)
3202 target_ulong
helper_dextr_w(target_ulong ac
, target_ulong shift
,
3207 shift
= shift
& 0x3F;
3209 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3211 return (int64_t)(int32_t)(temp
[0] >> 1);
3214 target_ulong
helper_dextr_r_w(target_ulong ac
, target_ulong shift
,
3220 shift
= shift
& 0x3F;
3221 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3231 temp128
= temp
[2] & 0x01;
3233 if ((temp128
!= 0 || temp
[1] != 0) &&
3234 (temp128
!= 1 || temp
[1] != ~0ull)) {
3235 set_DSPControl_overflow_flag(1, 23, env
);
3238 return (int64_t)(int32_t)(temp
[0] >> 1);
3241 target_ulong
helper_dextr_rs_w(target_ulong ac
, target_ulong shift
,
3247 shift
= shift
& 0x3F;
3248 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3258 temp128
= temp
[2] & 0x01;
3260 if ((temp128
!= 0 || temp
[1] != 0) &&
3261 (temp128
!= 1 || temp
[1] != ~0ull)) {
3263 temp
[0] = 0x0FFFFFFFF;
3265 temp
[0] = 0x0100000000ULL
;
3267 set_DSPControl_overflow_flag(1, 23, env
);
3270 return (int64_t)(int32_t)(temp
[0] >> 1);
3273 target_ulong
helper_dextr_l(target_ulong ac
, target_ulong shift
,
3277 target_ulong result
;
3279 shift
= shift
& 0x3F;
3281 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3282 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3287 target_ulong
helper_dextr_r_l(target_ulong ac
, target_ulong shift
,
3292 target_ulong result
;
3294 shift
= shift
& 0x3F;
3295 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3305 temp128
= temp
[2] & 0x01;
3307 if ((temp128
!= 0 || temp
[1] != 0) &&
3308 (temp128
!= 1 || temp
[1] != ~0ull)) {
3309 set_DSPControl_overflow_flag(1, 23, env
);
3312 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3317 target_ulong
helper_dextr_rs_l(target_ulong ac
, target_ulong shift
,
3322 target_ulong result
;
3324 shift
= shift
& 0x3F;
3325 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3335 temp128
= temp
[2] & 0x01;
3337 if ((temp128
!= 0 || temp
[1] != 0) &&
3338 (temp128
!= 1 || temp
[1] != ~0ull)) {
3340 temp
[1] &= ~0x00ull
- 1;
3341 temp
[0] |= ~0x00ull
- 1;
3346 set_DSPControl_overflow_flag(1, 23, env
);
3348 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3354 target_ulong
helper_extr_s_h(target_ulong ac
, target_ulong shift
,
3359 shift
= shift
& 0x1F;
3361 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
3362 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
3364 temp
= acc
>> shift
;
3366 if (temp
> (int64_t)0x7FFF) {
3368 set_DSPControl_overflow_flag(1, 23, env
);
3369 } else if (temp
< (int64_t)0xFFFFFFFFFFFF8000ULL
) {
3371 set_DSPControl_overflow_flag(1, 23, env
);
3374 return (target_long
)(int32_t)(temp
& 0xFFFFFFFF);
3378 #if defined(TARGET_MIPS64)
3379 target_ulong
helper_dextr_s_h(target_ulong ac
, target_ulong shift
,
3385 shift
= shift
& 0x1F;
3387 mipsdsp_rashift_acc((uint64_t *)temp
, ac
, shift
, env
);
3389 temp127
= (temp
[1] >> 63) & 0x01;
3391 if ((temp127
== 0) && (temp
[1] > 0 || temp
[0] > 32767)) {
3392 temp
[0] &= 0xFFFF0000;
3393 temp
[0] |= 0x00007FFF;
3394 set_DSPControl_overflow_flag(1, 23, env
);
3395 } else if ((temp127
== 1) &&
3396 (temp
[1] < 0xFFFFFFFFFFFFFFFFll
3397 || temp
[0] < 0xFFFFFFFFFFFF1000ll
)) {
3398 temp
[0] &= 0xFFFF0000;
3399 temp
[0] |= 0x00008000;
3400 set_DSPControl_overflow_flag(1, 23, env
);
3403 return (int64_t)(int16_t)(temp
[0] & MIPSDSP_LO
);
3408 target_ulong
helper_extp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3418 start_pos
= get_DSPControl_pos(env
);
3419 sub
= start_pos
- (size
+ 1);
3421 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3422 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3423 temp
= (acc
>> (start_pos
- size
)) & (~0U >> (31 - size
));
3424 set_DSPControl_efi(0, env
);
3426 set_DSPControl_efi(1, env
);
3429 return (target_ulong
)temp
;
3432 target_ulong
helper_extpdp(target_ulong ac
, target_ulong size
,
3442 start_pos
= get_DSPControl_pos(env
);
3443 sub
= start_pos
- (size
+ 1);
3445 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3446 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3447 temp
= extract64(acc
, start_pos
- size
, size
+ 1);
3449 set_DSPControl_pos(sub
, env
);
3450 set_DSPControl_efi(0, env
);
3452 set_DSPControl_efi(1, env
);
3455 return (target_ulong
)temp
;
3459 #if defined(TARGET_MIPS64)
3460 target_ulong
helper_dextp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3465 uint64_t tempB
, tempA
;
3471 start_pos
= get_DSPControl_pos(env
);
3472 len
= start_pos
- size
;
3473 tempB
= env
->active_tc
.HI
[ac
];
3474 tempA
= env
->active_tc
.LO
[ac
];
3476 sub
= start_pos
- (size
+ 1);
3479 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3480 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3481 set_DSPControl_efi(0, env
);
3483 set_DSPControl_efi(1, env
);
3489 target_ulong
helper_dextpdp(target_ulong ac
, target_ulong size
,
3495 uint64_t tempB
, tempA
;
3500 start_pos
= get_DSPControl_pos(env
);
3501 len
= start_pos
- size
;
3502 tempB
= env
->active_tc
.HI
[ac
];
3503 tempA
= env
->active_tc
.LO
[ac
];
3505 sub
= start_pos
- (size
+ 1);
3508 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3509 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3510 set_DSPControl_pos(sub
, env
);
3511 set_DSPControl_efi(0, env
);
3513 set_DSPControl_efi(1, env
);
3521 void helper_shilo(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3527 rs5_0
= (int8_t)(rs5_0
<< 2) >> 2;
3529 if (unlikely(rs5_0
== 0)) {
3533 acc
= (((uint64_t)env
->active_tc
.HI
[ac
] << 32) & MIPSDSP_LHI
) |
3534 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3537 temp
= acc
>> rs5_0
;
3539 temp
= acc
<< -rs5_0
;
3542 env
->active_tc
.HI
[ac
] = (target_ulong
)(int32_t)((temp
& MIPSDSP_LHI
) >> 32);
3543 env
->active_tc
.LO
[ac
] = (target_ulong
)(int32_t)(temp
& MIPSDSP_LLO
);
3546 #if defined(TARGET_MIPS64)
3547 void helper_dshilo(target_ulong shift
, target_ulong ac
, CPUMIPSState
*env
)
3550 uint64_t tempB
, tempA
;
3552 shift_t
= (int8_t)(shift
<< 1) >> 1;
3554 tempB
= env
->active_tc
.HI
[ac
];
3555 tempA
= env
->active_tc
.LO
[ac
];
3559 tempA
= (tempB
<< (64 - shift_t
)) | (tempA
>> shift_t
);
3560 tempB
= tempB
>> shift_t
;
3563 tempB
= (tempB
<< shift_t
) | (tempA
>> (64 - shift_t
));
3564 tempA
= tempA
<< shift_t
;
3568 env
->active_tc
.HI
[ac
] = tempB
;
3569 env
->active_tc
.LO
[ac
] = tempA
;
3573 void helper_mthlip(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3575 int32_t tempA
, tempB
, pos
;
3578 tempB
= env
->active_tc
.LO
[ac
];
3579 env
->active_tc
.HI
[ac
] = (target_long
)tempB
;
3580 env
->active_tc
.LO
[ac
] = (target_long
)tempA
;
3581 pos
= get_DSPControl_pos(env
);
3586 set_DSPControl_pos(pos
+ 32, env
);
3590 #if defined(TARGET_MIPS64)
3591 void helper_dmthlip(target_ulong rs
, target_ulong ac
, CPUMIPSState
*env
)
3595 uint64_t tempB
, tempA
;
3600 tempB
= env
->active_tc
.LO
[ac_t
];
3602 env
->active_tc
.HI
[ac_t
] = tempB
;
3603 env
->active_tc
.LO
[ac_t
] = tempA
;
3605 pos
= get_DSPControl_pos(env
);
3609 set_DSPControl_pos(pos
, env
);
3614 void cpu_wrdsp(uint32_t rs
, uint32_t mask_num
, CPUMIPSState
*env
)
3618 uint32_t newbits
, overwrite
;
3622 overwrite
= 0xFFFFFFFF;
3623 dsp
= env
->active_tc
.DSPControl
;
3625 for (i
= 0; i
< 6; i
++) {
3626 mask
[i
] = (mask_num
>> i
) & 0x01;
3630 #if defined(TARGET_MIPS64)
3631 overwrite
&= 0xFFFFFF80;
3632 newbits
&= 0xFFFFFF80;
3633 newbits
|= 0x0000007F & rs
;
3635 overwrite
&= 0xFFFFFFC0;
3636 newbits
&= 0xFFFFFFC0;
3637 newbits
|= 0x0000003F & rs
;
3642 overwrite
&= 0xFFFFE07F;
3643 newbits
&= 0xFFFFE07F;
3644 newbits
|= 0x00001F80 & rs
;
3648 overwrite
&= 0xFFFFDFFF;
3649 newbits
&= 0xFFFFDFFF;
3650 newbits
|= 0x00002000 & rs
;
3654 overwrite
&= 0xFF00FFFF;
3655 newbits
&= 0xFF00FFFF;
3656 newbits
|= 0x00FF0000 & rs
;
3660 overwrite
&= 0x00FFFFFF;
3661 newbits
&= 0x00FFFFFF;
3662 #if defined(TARGET_MIPS64)
3663 newbits
|= 0xFF000000 & rs
;
3665 newbits
|= 0x0F000000 & rs
;
3670 overwrite
&= 0xFFFFBFFF;
3671 newbits
&= 0xFFFFBFFF;
3672 newbits
|= 0x00004000 & rs
;
3675 dsp
= dsp
& overwrite
;
3676 dsp
= dsp
| newbits
;
3677 env
->active_tc
.DSPControl
= dsp
;
3680 void helper_wrdsp(target_ulong rs
, target_ulong mask_num
, CPUMIPSState
*env
)
3682 cpu_wrdsp(rs
, mask_num
, env
);
3685 uint32_t cpu_rddsp(uint32_t mask_num
, CPUMIPSState
*env
)
3693 for (i
= 0; i
< 6; i
++) {
3694 mask
[i
] = (mask_num
& ruler
) >> i
;
3699 dsp
= env
->active_tc
.DSPControl
;
3702 #if defined(TARGET_MIPS64)
3710 temp
|= dsp
& 0x1F80;
3714 temp
|= dsp
& 0x2000;
3718 temp
|= dsp
& 0x00FF0000;
3722 #if defined(TARGET_MIPS64)
3723 temp
|= dsp
& 0xFF000000;
3725 temp
|= dsp
& 0x0F000000;
3730 temp
|= dsp
& 0x4000;
3736 target_ulong
helper_rddsp(target_ulong mask_num
, CPUMIPSState
*env
)
3738 return cpu_rddsp(mask_num
, env
);
3751 #undef MIPSDSP_SPLIT32_8
3752 #undef MIPSDSP_SPLIT32_16
3754 #undef MIPSDSP_RETURN32_8
3755 #undef MIPSDSP_RETURN32_16
3757 #ifdef TARGET_MIPS64
3758 #undef MIPSDSP_SPLIT64_16
3759 #undef MIPSDSP_SPLIT64_32
3760 #undef MIPSDSP_RETURN64_16
3761 #undef MIPSDSP_RETURN64_32