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 void set_DSPControl_pos(uint32_t pos
, CPUMIPSState
*env
)
83 dspc
= env
->active_tc
.DSPControl
;
85 dspc
= dspc
& 0xFFFFFFC0;
88 dspc
= dspc
& 0xFFFFFF80;
91 env
->active_tc
.DSPControl
= dspc
;
94 static inline uint32_t get_DSPControl_pos(CPUMIPSState
*env
)
99 dspc
= env
->active_tc
.DSPControl
;
101 #ifndef TARGET_MIPS64
110 static inline void set_DSPControl_efi(uint32_t flag
, CPUMIPSState
*env
)
112 env
->active_tc
.DSPControl
&= 0xFFFFBFFF;
113 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 14;
116 #define DO_MIPS_SAT_ABS(size) \
117 static inline int##size##_t mipsdsp_sat_abs##size(int##size##_t a, \
120 if (a == INT##size##_MIN) { \
121 set_DSPControl_overflow_flag(1, 20, env); \
122 return INT##size##_MAX; \
124 return MIPSDSP_ABS(a); \
130 #undef DO_MIPS_SAT_ABS
133 static inline int16_t mipsdsp_add_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
139 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x8000)) {
140 set_DSPControl_overflow_flag(1, 20, env
);
146 static inline int16_t mipsdsp_sat_add_i16(int16_t a
, int16_t b
,
153 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempS
, 0x8000)) {
159 set_DSPControl_overflow_flag(1, 20, env
);
165 static inline int32_t mipsdsp_sat_add_i32(int32_t a
, int32_t b
,
172 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x80000000)) {
178 set_DSPControl_overflow_flag(1, 20, env
);
184 static inline uint8_t mipsdsp_add_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
188 temp
= (uint16_t)a
+ (uint16_t)b
;
191 set_DSPControl_overflow_flag(1, 20, env
);
197 static inline uint16_t mipsdsp_add_u16(uint16_t a
, uint16_t b
,
202 temp
= (uint32_t)a
+ (uint32_t)b
;
204 if (temp
& 0x00010000) {
205 set_DSPControl_overflow_flag(1, 20, env
);
208 return temp
& 0xFFFF;
211 static inline uint8_t mipsdsp_sat_add_u8(uint8_t a
, uint8_t b
,
217 temp
= (uint16_t)a
+ (uint16_t)b
;
218 result
= temp
& 0xFF;
222 set_DSPControl_overflow_flag(1, 20, env
);
228 static inline uint16_t mipsdsp_sat_add_u16(uint16_t a
, uint16_t b
,
234 temp
= (uint32_t)a
+ (uint32_t)b
;
235 result
= temp
& 0xFFFF;
237 if (0x00010000 & temp
) {
239 set_DSPControl_overflow_flag(1, 20, env
);
245 static inline int32_t mipsdsp_sat32_acc_q31(int32_t acc
, int32_t a
,
249 int32_t temp32
, temp31
, result
;
252 #ifndef TARGET_MIPS64
253 temp
= ((uint64_t)env
->active_tc
.HI
[acc
] << 32) |
254 (uint64_t)env
->active_tc
.LO
[acc
];
256 temp
= (uint64_t)env
->active_tc
.LO
[acc
];
259 temp_sum
= (int64_t)a
+ temp
;
261 temp32
= (temp_sum
>> 32) & 0x01;
262 temp31
= (temp_sum
>> 31) & 0x01;
263 result
= temp_sum
& 0xFFFFFFFF;
265 if (temp32
!= temp31
) {
271 set_DSPControl_overflow_flag(1, 16 + acc
, env
);
278 /* a[0] is LO, a[1] is HI. */
279 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret
,
286 ret
[0] = env
->active_tc
.LO
[ac
] + a
[0];
287 ret
[1] = env
->active_tc
.HI
[ac
] + a
[1];
289 if (((uint64_t)ret
[0] < (uint64_t)env
->active_tc
.LO
[ac
]) &&
290 ((uint64_t)ret
[0] < (uint64_t)a
[0])) {
294 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
296 ret
[0] = (0x01ull
<< 63);
299 ret
[0] = (0x01ull
<< 63) - 1;
302 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
306 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret
,
313 ret
[0] = env
->active_tc
.LO
[ac
] - a
[0];
314 ret
[1] = env
->active_tc
.HI
[ac
] - a
[1];
316 if ((uint64_t)ret
[0] > (uint64_t)env
->active_tc
.LO
[ac
]) {
320 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
322 ret
[0] = (0x01ull
<< 63);
325 ret
[0] = (0x01ull
<< 63) - 1;
328 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
333 static inline int32_t mipsdsp_mul_i16_i16(int16_t a
, int16_t b
,
338 temp
= (int32_t)a
* (int32_t)b
;
340 if ((temp
> (int)0x7FFF) || (temp
< (int)0xFFFF8000)) {
341 set_DSPControl_overflow_flag(1, 21, env
);
348 static inline int32_t mipsdsp_mul_u16_u16(int32_t a
, int32_t b
)
354 static inline int32_t mipsdsp_mul_i32_i32(int32_t a
, int32_t b
)
360 static inline int32_t mipsdsp_sat16_mul_i16_i16(int16_t a
, int16_t b
,
365 temp
= (int32_t)a
* (int32_t)b
;
367 if (temp
> (int)0x7FFF) {
369 set_DSPControl_overflow_flag(1, 21, env
);
370 } else if (temp
< (int)0xffff8000) {
372 set_DSPControl_overflow_flag(1, 21, env
);
379 static inline int32_t mipsdsp_mul_q15_q15_overflowflag21(uint16_t a
, uint16_t b
,
384 if ((a
== 0x8000) && (b
== 0x8000)) {
386 set_DSPControl_overflow_flag(1, 21, env
);
388 temp
= ((int16_t)a
* (int16_t)b
) << 1;
395 static inline uint8_t mipsdsp_rshift_u8(uint8_t a
, target_ulong mov
)
400 static inline uint16_t mipsdsp_rshift_u16(uint16_t a
, target_ulong mov
)
405 static inline int8_t mipsdsp_rashift8(int8_t a
, target_ulong mov
)
410 static inline int16_t mipsdsp_rashift16(int16_t a
, target_ulong mov
)
416 static inline int32_t mipsdsp_rashift32(int32_t a
, target_ulong mov
)
422 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a
, int16_t b
)
426 temp
= (int32_t)a
+ (int32_t)b
;
428 return (temp
>> 1) & 0xFFFF;
431 /* round right shift */
432 static inline int16_t mipsdsp_rrshift1_add_q16(int16_t a
, int16_t b
)
436 temp
= (int32_t)a
+ (int32_t)b
;
439 return (temp
>> 1) & 0xFFFF;
442 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a
, int32_t b
)
446 temp
= (int64_t)a
+ (int64_t)b
;
448 return (temp
>> 1) & 0xFFFFFFFF;
451 static inline int32_t mipsdsp_rrshift1_add_q32(int32_t a
, int32_t b
)
455 temp
= (int64_t)a
+ (int64_t)b
;
458 return (temp
>> 1) & 0xFFFFFFFF;
461 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a
, uint8_t b
)
465 temp
= (uint16_t)a
+ (uint16_t)b
;
467 return (temp
>> 1) & 0x00FF;
470 static inline uint8_t mipsdsp_rrshift1_add_u8(uint8_t a
, uint8_t b
)
474 temp
= (uint16_t)a
+ (uint16_t)b
+ 1;
476 return (temp
>> 1) & 0x00FF;
480 static inline uint8_t mipsdsp_rshift1_sub_u8(uint8_t a
, uint8_t b
)
484 temp
= (uint16_t)a
- (uint16_t)b
;
486 return (temp
>> 1) & 0x00FF;
489 static inline uint8_t mipsdsp_rrshift1_sub_u8(uint8_t a
, uint8_t b
)
493 temp
= (uint16_t)a
- (uint16_t)b
+ 1;
495 return (temp
>> 1) & 0x00FF;
499 /* 128 bits long. p[0] is LO, p[1] is HI. */
500 static inline void mipsdsp_rndrashift_short_acc(int64_t *p
,
507 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
508 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
509 p
[0] = (shift
== 0) ? (acc
<< 1) : (acc
>> (shift
- 1));
510 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);
563 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac
, uint16_t a
, uint16_t b
,
568 if ((a
== 0x8000) && (b
== 0x8000)) {
570 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
572 temp
= ((int16_t)a
* (int16_t)b
) << 1;
578 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac
, uint32_t a
, uint32_t b
,
583 if ((a
== 0x80000000) && (b
== 0x80000000)) {
584 temp
= (0x01ull
<< 63) - 1;
585 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
587 temp
= ((int64_t)(int32_t)a
* (int32_t)b
) << 1;
593 static inline uint16_t mipsdsp_mul_u8_u8(uint8_t a
, uint8_t b
)
595 return (uint16_t)a
* (uint16_t)b
;
598 static inline uint16_t mipsdsp_mul_u8_u16(uint8_t a
, uint16_t b
,
603 tempI
= (uint32_t)a
* (uint32_t)b
;
604 if (tempI
> 0x0000FFFF) {
606 set_DSPControl_overflow_flag(1, 21, env
);
609 return tempI
& 0x0000FFFF;
613 static inline uint64_t mipsdsp_mul_u32_u32(uint32_t a
, uint32_t b
)
615 return (uint64_t)a
* (uint64_t)b
;
619 static inline int16_t mipsdsp_rndq15_mul_q15_q15(uint16_t a
, uint16_t b
,
624 if ((a
== 0x8000) && (b
== 0x8000)) {
626 set_DSPControl_overflow_flag(1, 21, env
);
628 temp
= ((int16_t)a
* (int16_t)b
) << 1;
629 temp
= temp
+ 0x00008000;
632 return (temp
& 0xFFFF0000) >> 16;
635 static inline int32_t mipsdsp_sat16_mul_q15_q15(uint16_t a
, uint16_t b
,
640 if ((a
== 0x8000) && (b
== 0x8000)) {
642 set_DSPControl_overflow_flag(1, 21, env
);
644 temp
= (int16_t)a
* (int16_t)b
;
648 return (temp
>> 16) & 0x0000FFFF;
651 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a
,
658 * The value 0x00008000 will be added to the input Q31 value, and the code
659 * needs to check if the addition causes an overflow. Since a positive value
660 * is added, overflow can happen in one direction only.
662 if (a
> 0x7FFF7FFF) {
664 set_DSPControl_overflow_flag(1, 22, env
);
666 temp
= ((a
+ 0x8000) >> 16) & 0xFFFF;
672 static inline uint8_t mipsdsp_sat8_reduce_precision(uint16_t a
,
678 sign
= (a
>> 15) & 0x01;
683 set_DSPControl_overflow_flag(1, 22, env
);
686 return (mag
>> 7) & 0xFFFF;
689 set_DSPControl_overflow_flag(1, 22, env
);
694 static inline uint8_t mipsdsp_lshift8(uint8_t a
, uint8_t s
, CPUMIPSState
*env
)
699 discard
= a
>> (8 - s
);
701 if (discard
!= 0x00) {
702 set_DSPControl_overflow_flag(1, 22, env
);
708 static inline uint16_t mipsdsp_lshift16(uint16_t a
, uint8_t s
,
714 discard
= (int16_t)a
>> (15 - s
);
716 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
717 set_DSPControl_overflow_flag(1, 22, env
);
724 static inline uint32_t mipsdsp_lshift32(uint32_t a
, uint8_t s
,
732 discard
= (int32_t)a
>> (31 - (s
- 1));
734 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
735 set_DSPControl_overflow_flag(1, 22, env
);
742 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a
, uint8_t s
,
751 sign
= (a
>> 15) & 0x01;
753 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
754 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
756 discard
= a
>> (14 - (s
- 1));
759 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
760 set_DSPControl_overflow_flag(1, 22, env
);
761 return (sign
== 0) ? 0x7FFF : 0x8000;
768 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a
, uint8_t s
,
777 sign
= (a
>> 31) & 0x01;
779 discard
= (((0x01 << (32 - s
)) - 1) << s
) |
780 ((a
>> (30 - (s
- 1))) & ((0x01 << s
) - 1));
782 discard
= a
>> (30 - (s
- 1));
785 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
786 set_DSPControl_overflow_flag(1, 22, env
);
787 return (sign
== 0) ? 0x7FFFFFFF : 0x80000000;
794 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a
, uint8_t s
)
799 temp
= (uint32_t)a
<< 1;
801 temp
= (int32_t)(int8_t)a
>> (s
- 1);
804 return (temp
+ 1) >> 1;
807 static inline uint16_t mipsdsp_rnd16_rashift(uint16_t a
, uint8_t s
)
812 temp
= (uint32_t)a
<< 1;
814 temp
= (int32_t)(int16_t)a
>> (s
- 1);
817 return (temp
+ 1) >> 1;
820 static inline uint32_t mipsdsp_rnd32_rashift(uint32_t a
, uint8_t s
)
825 temp
= (uint64_t)a
<< 1;
827 temp
= (int64_t)(int32_t)a
>> (s
- 1);
831 return (temp
>> 1) & 0xFFFFFFFFull
;
834 static inline uint16_t mipsdsp_sub_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
839 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
840 set_DSPControl_overflow_flag(1, 20, env
);
846 static inline uint16_t mipsdsp_sat16_sub(int16_t a
, int16_t b
,
852 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
858 set_DSPControl_overflow_flag(1, 20, env
);
864 static inline uint32_t mipsdsp_sat32_sub(int32_t a
, int32_t b
,
870 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
876 set_DSPControl_overflow_flag(1, 20, env
);
879 return temp
& 0xFFFFFFFFull
;
882 static inline uint16_t mipsdsp_rshift1_sub_q16(int16_t a
, int16_t b
)
886 temp
= (int32_t)a
- (int32_t)b
;
888 return (temp
>> 1) & 0x0000FFFF;
891 static inline uint16_t mipsdsp_rrshift1_sub_q16(int16_t a
, int16_t b
)
895 temp
= (int32_t)a
- (int32_t)b
;
898 return (temp
>> 1) & 0x0000FFFF;
901 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a
, int32_t b
)
905 temp
= (int64_t)a
- (int64_t)b
;
907 return (temp
>> 1) & 0xFFFFFFFFull
;
910 static inline uint32_t mipsdsp_rrshift1_sub_q32(int32_t a
, int32_t b
)
914 temp
= (int64_t)a
- (int64_t)b
;
917 return (temp
>> 1) & 0xFFFFFFFFull
;
920 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a
, uint16_t b
,
926 temp
= (uint32_t)a
- (uint32_t)b
;
927 temp16
= (temp
>> 16) & 0x01;
929 set_DSPControl_overflow_flag(1, 20, env
);
931 return temp
& 0x0000FFFF;
934 static inline uint16_t mipsdsp_satu16_sub_u16_u16(uint16_t a
, uint16_t b
,
940 temp
= (uint32_t)a
- (uint32_t)b
;
941 temp16
= (temp
>> 16) & 0x01;
945 set_DSPControl_overflow_flag(1, 20, env
);
948 return temp
& 0x0000FFFF;
951 static inline uint8_t mipsdsp_sub_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
956 temp
= (uint16_t)a
- (uint16_t)b
;
957 temp8
= (temp
>> 8) & 0x01;
959 set_DSPControl_overflow_flag(1, 20, env
);
962 return temp
& 0x00FF;
965 static inline uint8_t mipsdsp_satu8_sub(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
970 temp
= (uint16_t)a
- (uint16_t)b
;
971 temp8
= (temp
>> 8) & 0x01;
974 set_DSPControl_overflow_flag(1, 20, env
);
977 return temp
& 0x00FF;
981 static inline uint32_t mipsdsp_sub32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
986 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
987 set_DSPControl_overflow_flag(1, 20, env
);
993 static inline int32_t mipsdsp_add_i32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
999 if (MIPSDSP_OVERFLOW_ADD(a
, b
, temp
, 0x80000000)) {
1000 set_DSPControl_overflow_flag(1, 20, env
);
1007 static inline int32_t mipsdsp_cmp_eq(int32_t a
, int32_t b
)
1012 static inline int32_t mipsdsp_cmp_le(int32_t a
, int32_t b
)
1017 static inline int32_t mipsdsp_cmp_lt(int32_t a
, int32_t b
)
1022 static inline int32_t mipsdsp_cmpu_eq(uint32_t a
, uint32_t b
)
1027 static inline int32_t mipsdsp_cmpu_le(uint32_t a
, uint32_t b
)
1032 static inline int32_t mipsdsp_cmpu_lt(uint32_t a
, uint32_t b
)
1036 /*** MIPS DSP internal functions end ***/
1038 #define MIPSDSP_LHI 0xFFFFFFFF00000000ull
1039 #define MIPSDSP_LLO 0x00000000FFFFFFFFull
1040 #define MIPSDSP_HI 0xFFFF0000
1041 #define MIPSDSP_LO 0x0000FFFF
1042 #define MIPSDSP_Q3 0xFF000000
1043 #define MIPSDSP_Q2 0x00FF0000
1044 #define MIPSDSP_Q1 0x0000FF00
1045 #define MIPSDSP_Q0 0x000000FF
1047 #define MIPSDSP_SPLIT32_8(num, a, b, c, d) \
1049 a = (num >> 24) & MIPSDSP_Q0; \
1050 b = (num >> 16) & MIPSDSP_Q0; \
1051 c = (num >> 8) & MIPSDSP_Q0; \
1052 d = num & MIPSDSP_Q0; \
1055 #define MIPSDSP_SPLIT32_16(num, a, b) \
1057 a = (num >> 16) & MIPSDSP_LO; \
1058 b = num & MIPSDSP_LO; \
1061 #define MIPSDSP_RETURN32_8(a, b, c, d) ((target_long)(int32_t) \
1062 (((uint32_t)a << 24) | \
1063 (((uint32_t)b << 16) | \
1064 (((uint32_t)c << 8) | \
1065 ((uint32_t)d & 0xFF)))))
1066 #define MIPSDSP_RETURN32_16(a, b) ((target_long)(int32_t) \
1067 (((uint32_t)a << 16) | \
1068 ((uint32_t)b & 0xFFFF)))
1070 #ifdef TARGET_MIPS64
1071 #define MIPSDSP_SPLIT64_16(num, a, b, c, d) \
1073 a = (num >> 48) & MIPSDSP_LO; \
1074 b = (num >> 32) & MIPSDSP_LO; \
1075 c = (num >> 16) & MIPSDSP_LO; \
1076 d = num & MIPSDSP_LO; \
1079 #define MIPSDSP_SPLIT64_32(num, a, b) \
1081 a = (num >> 32) & MIPSDSP_LLO; \
1082 b = num & MIPSDSP_LLO; \
1085 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)a << 48) | \
1086 ((uint64_t)b << 32) | \
1087 ((uint64_t)c << 16) | \
1089 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)a << 32) | (uint64_t)b)
1092 /** DSP Arithmetic Sub-class insns **/
1093 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1094 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1101 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1102 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1105 return (target_long)dt.sw[0]; \
1107 MIPSDSP32_UNOP_ENV(absq_s_ph
, sat_abs16
, sh
)
1108 MIPSDSP32_UNOP_ENV(absq_s_qb
, sat_abs8
, sb
)
1109 MIPSDSP32_UNOP_ENV(absq_s_w
, sat_abs32
, sw
)
1110 #undef MIPSDSP32_UNOP_ENV
1112 #if defined(TARGET_MIPS64)
1113 #define MIPSDSP64_UNOP_ENV(name, func, element) \
1114 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1121 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1122 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1127 MIPSDSP64_UNOP_ENV(absq_s_ob
, sat_abs8
, sb
)
1128 MIPSDSP64_UNOP_ENV(absq_s_qh
, sat_abs16
, sh
)
1129 MIPSDSP64_UNOP_ENV(absq_s_pw
, sat_abs32
, sw
)
1130 #undef MIPSDSP64_UNOP_ENV
1133 #define MIPSDSP32_BINOP(name, func, element) \
1134 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1136 DSP32Value ds, dt; \
1142 for (i = 0; i < ARRAY_SIZE(ds.element); i++) { \
1143 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1146 return (target_long)ds.sw[0]; \
1148 MIPSDSP32_BINOP(addqh_ph
, rshift1_add_q16
, sh
);
1149 MIPSDSP32_BINOP(addqh_r_ph
, rrshift1_add_q16
, sh
);
1150 MIPSDSP32_BINOP(addqh_r_w
, rrshift1_add_q32
, sw
);
1151 MIPSDSP32_BINOP(addqh_w
, rshift1_add_q32
, sw
);
1152 MIPSDSP32_BINOP(adduh_qb
, rshift1_add_u8
, ub
);
1153 MIPSDSP32_BINOP(adduh_r_qb
, rrshift1_add_u8
, ub
);
1154 MIPSDSP32_BINOP(subqh_ph
, rshift1_sub_q16
, sh
);
1155 MIPSDSP32_BINOP(subqh_r_ph
, rrshift1_sub_q16
, sh
);
1156 MIPSDSP32_BINOP(subqh_r_w
, rrshift1_sub_q32
, sw
);
1157 MIPSDSP32_BINOP(subqh_w
, rshift1_sub_q32
, sw
);
1158 #undef MIPSDSP32_BINOP
1160 #define MIPSDSP32_BINOP_ENV(name, func, element) \
1161 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1162 CPUMIPSState *env) \
1164 DSP32Value ds, dt; \
1170 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1171 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1174 return (target_long)ds.sw[0]; \
1176 MIPSDSP32_BINOP_ENV(addq_ph
, add_i16
, sh
)
1177 MIPSDSP32_BINOP_ENV(addq_s_ph
, sat_add_i16
, sh
)
1178 MIPSDSP32_BINOP_ENV(addq_s_w
, sat_add_i32
, sw
);
1179 MIPSDSP32_BINOP_ENV(addu_ph
, add_u16
, sh
)
1180 MIPSDSP32_BINOP_ENV(addu_qb
, add_u8
, ub
);
1181 MIPSDSP32_BINOP_ENV(addu_s_ph
, sat_add_u16
, sh
)
1182 MIPSDSP32_BINOP_ENV(addu_s_qb
, sat_add_u8
, ub
);
1183 MIPSDSP32_BINOP_ENV(subq_ph
, sub_i16
, sh
);
1184 MIPSDSP32_BINOP_ENV(subq_s_ph
, sat16_sub
, sh
);
1185 MIPSDSP32_BINOP_ENV(subq_s_w
, sat32_sub
, sw
);
1186 MIPSDSP32_BINOP_ENV(subu_ph
, sub_u16_u16
, sh
);
1187 MIPSDSP32_BINOP_ENV(subu_qb
, sub_u8
, ub
);
1188 MIPSDSP32_BINOP_ENV(subu_s_ph
, satu16_sub_u16_u16
, sh
);
1189 MIPSDSP32_BINOP_ENV(subu_s_qb
, satu8_sub
, ub
);
1190 #undef MIPSDSP32_BINOP_ENV
1192 #ifdef TARGET_MIPS64
1193 #define MIPSDSP64_BINOP(name, func, element) \
1194 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1196 DSP64Value ds, dt; \
1202 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1203 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1208 MIPSDSP64_BINOP(adduh_ob
, rshift1_add_u8
, ub
);
1209 MIPSDSP64_BINOP(adduh_r_ob
, rrshift1_add_u8
, ub
);
1210 MIPSDSP64_BINOP(subuh_ob
, rshift1_sub_u8
, ub
);
1211 MIPSDSP64_BINOP(subuh_r_ob
, rrshift1_sub_u8
, ub
);
1212 #undef MIPSDSP64_BINOP
1214 #define MIPSDSP64_BINOP_ENV(name, func, element) \
1215 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1216 CPUMIPSState *env) \
1218 DSP64Value ds, dt; \
1224 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1225 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1230 MIPSDSP64_BINOP_ENV(addq_pw
, add_i32
, sw
);
1231 MIPSDSP64_BINOP_ENV(addq_qh
, add_i16
, sh
);
1232 MIPSDSP64_BINOP_ENV(addq_s_pw
, sat_add_i32
, sw
);
1233 MIPSDSP64_BINOP_ENV(addq_s_qh
, sat_add_i16
, sh
);
1234 MIPSDSP64_BINOP_ENV(addu_ob
, add_u8
, uh
);
1235 MIPSDSP64_BINOP_ENV(addu_qh
, add_u16
, uh
);
1236 MIPSDSP64_BINOP_ENV(addu_s_ob
, sat_add_u8
, uh
);
1237 MIPSDSP64_BINOP_ENV(addu_s_qh
, sat_add_u16
, uh
);
1238 MIPSDSP64_BINOP_ENV(subq_pw
, sub32
, sw
);
1239 MIPSDSP64_BINOP_ENV(subq_qh
, sub_i16
, sh
);
1240 MIPSDSP64_BINOP_ENV(subq_s_pw
, sat32_sub
, sw
);
1241 MIPSDSP64_BINOP_ENV(subq_s_qh
, sat16_sub
, sh
);
1242 MIPSDSP64_BINOP_ENV(subu_ob
, sub_u8
, uh
);
1243 MIPSDSP64_BINOP_ENV(subu_qh
, sub_u16_u16
, uh
);
1244 MIPSDSP64_BINOP_ENV(subu_s_ob
, satu8_sub
, uh
);
1245 MIPSDSP64_BINOP_ENV(subu_s_qh
, satu16_sub_u16_u16
, uh
);
1246 #undef MIPSDSP64_BINOP_ENV
1250 #define SUBUH_QB(name, var) \
1251 target_ulong helper_##name##_qb(target_ulong rs, target_ulong rt) \
1253 uint8_t rs3, rs2, rs1, rs0; \
1254 uint8_t rt3, rt2, rt1, rt0; \
1255 uint8_t tempD, tempC, tempB, tempA; \
1257 MIPSDSP_SPLIT32_8(rs, rs3, rs2, rs1, rs0); \
1258 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1260 tempD = ((uint16_t)rs3 - (uint16_t)rt3 + var) >> 1; \
1261 tempC = ((uint16_t)rs2 - (uint16_t)rt2 + var) >> 1; \
1262 tempB = ((uint16_t)rs1 - (uint16_t)rt1 + var) >> 1; \
1263 tempA = ((uint16_t)rs0 - (uint16_t)rt0 + var) >> 1; \
1265 return ((uint32_t)tempD << 24) | ((uint32_t)tempC << 16) | \
1266 ((uint32_t)tempB << 8) | ((uint32_t)tempA); \
1270 SUBUH_QB(subuh_r
, 1);
1274 target_ulong
helper_addsc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1276 uint64_t temp
, tempRs
, tempRt
;
1279 tempRs
= (uint64_t)rs
& MIPSDSP_LLO
;
1280 tempRt
= (uint64_t)rt
& MIPSDSP_LLO
;
1282 temp
= tempRs
+ tempRt
;
1283 flag
= (temp
& 0x0100000000ull
) >> 32;
1284 set_DSPControl_carryflag(flag
, env
);
1286 return (target_long
)(int32_t)(temp
& MIPSDSP_LLO
);
1289 target_ulong
helper_addwc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1292 int32_t temp32
, temp31
;
1295 tempL
= (int64_t)(int32_t)rs
+ (int64_t)(int32_t)rt
+
1296 get_DSPControl_carryflag(env
);
1297 temp31
= (tempL
>> 31) & 0x01;
1298 temp32
= (tempL
>> 32) & 0x01;
1300 if (temp31
!= temp32
) {
1301 set_DSPControl_overflow_flag(1, 20, env
);
1304 rd
= tempL
& MIPSDSP_LLO
;
1306 return (target_long
)(int32_t)rd
;
1309 target_ulong
helper_modsub(target_ulong rs
, target_ulong rt
)
1315 decr
= rt
& MIPSDSP_Q0
;
1316 lastindex
= (rt
>> 8) & MIPSDSP_LO
;
1318 if ((rs
& MIPSDSP_LLO
) == 0x00000000) {
1319 rd
= (target_ulong
)lastindex
;
1327 target_ulong
helper_raddu_w_qb(target_ulong rs
)
1329 target_ulong ret
= 0;
1334 for (i
= 0; i
< 4; i
++) {
1340 #if defined(TARGET_MIPS64)
1341 target_ulong
helper_raddu_l_ob(target_ulong rs
)
1343 target_ulong ret
= 0;
1348 for (i
= 0; i
< 8; i
++) {
1355 #define PRECR_QB_PH(name, a, b)\
1356 target_ulong helper_##name##_qb_ph(target_ulong rs, target_ulong rt) \
1358 uint8_t tempD, tempC, tempB, tempA; \
1360 tempD = (rs >> a) & MIPSDSP_Q0; \
1361 tempC = (rs >> b) & MIPSDSP_Q0; \
1362 tempB = (rt >> a) & MIPSDSP_Q0; \
1363 tempA = (rt >> b) & MIPSDSP_Q0; \
1365 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA); \
1368 PRECR_QB_PH(precr
, 16, 0);
1369 PRECR_QB_PH(precrq
, 24, 8);
1373 target_ulong
helper_precr_sra_ph_w(uint32_t sa
, target_ulong rs
,
1376 uint16_t tempB
, tempA
;
1378 tempB
= ((int32_t)rt
>> sa
) & MIPSDSP_LO
;
1379 tempA
= ((int32_t)rs
>> sa
) & MIPSDSP_LO
;
1381 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1384 target_ulong
helper_precr_sra_r_ph_w(uint32_t sa
,
1385 target_ulong rs
, target_ulong rt
)
1387 uint64_t tempB
, tempA
;
1389 /* If sa = 0, then (sa - 1) = -1 will case shift error, so we need else. */
1391 tempB
= (rt
& MIPSDSP_LO
) << 1;
1392 tempA
= (rs
& MIPSDSP_LO
) << 1;
1394 tempB
= ((int32_t)rt
>> (sa
- 1)) + 1;
1395 tempA
= ((int32_t)rs
>> (sa
- 1)) + 1;
1397 rt
= (((tempB
>> 1) & MIPSDSP_LO
) << 16) | ((tempA
>> 1) & MIPSDSP_LO
);
1399 return (target_long
)(int32_t)rt
;
1402 target_ulong
helper_precrq_ph_w(target_ulong rs
, target_ulong rt
)
1404 uint16_t tempB
, tempA
;
1406 tempB
= (rs
& MIPSDSP_HI
) >> 16;
1407 tempA
= (rt
& MIPSDSP_HI
) >> 16;
1409 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1412 target_ulong
helper_precrq_rs_ph_w(target_ulong rs
, target_ulong rt
,
1415 uint16_t tempB
, tempA
;
1417 tempB
= mipsdsp_trunc16_sat16_round(rs
, env
);
1418 tempA
= mipsdsp_trunc16_sat16_round(rt
, env
);
1420 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1423 #if defined(TARGET_MIPS64)
1424 target_ulong
helper_precr_ob_qh(target_ulong rs
, target_ulong rt
)
1426 uint8_t rs6
, rs4
, rs2
, rs0
;
1427 uint8_t rt6
, rt4
, rt2
, rt0
;
1430 rs6
= (rs
>> 48) & MIPSDSP_Q0
;
1431 rs4
= (rs
>> 32) & MIPSDSP_Q0
;
1432 rs2
= (rs
>> 16) & MIPSDSP_Q0
;
1433 rs0
= rs
& MIPSDSP_Q0
;
1434 rt6
= (rt
>> 48) & MIPSDSP_Q0
;
1435 rt4
= (rt
>> 32) & MIPSDSP_Q0
;
1436 rt2
= (rt
>> 16) & MIPSDSP_Q0
;
1437 rt0
= rt
& MIPSDSP_Q0
;
1439 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1440 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1441 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1442 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1447 #define PRECR_QH_PW(name, var) \
1448 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, target_ulong rt, \
1451 uint16_t rs3, rs2, rs1, rs0; \
1452 uint16_t rt3, rt2, rt1, rt0; \
1453 uint16_t tempD, tempC, tempB, tempA; \
1455 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
1456 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1458 /* When sa = 0, we use rt2, rt0, rs2, rs0; \
1459 * when sa != 0, we use rt3, rt1, rs3, rs1. */ \
1461 tempD = rt2 << var; \
1462 tempC = rt0 << var; \
1463 tempB = rs2 << var; \
1464 tempA = rs0 << var; \
1466 tempD = (((int16_t)rt3 >> sa) + var) >> var; \
1467 tempC = (((int16_t)rt1 >> sa) + var) >> var; \
1468 tempB = (((int16_t)rs3 >> sa) + var) >> var; \
1469 tempA = (((int16_t)rs1 >> sa) + var) >> var; \
1472 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1475 PRECR_QH_PW(sra
, 0);
1476 PRECR_QH_PW(sra_r
, 1);
1480 target_ulong
helper_precrq_ob_qh(target_ulong rs
, target_ulong rt
)
1482 uint8_t rs6
, rs4
, rs2
, rs0
;
1483 uint8_t rt6
, rt4
, rt2
, rt0
;
1486 rs6
= (rs
>> 56) & MIPSDSP_Q0
;
1487 rs4
= (rs
>> 40) & MIPSDSP_Q0
;
1488 rs2
= (rs
>> 24) & MIPSDSP_Q0
;
1489 rs0
= (rs
>> 8) & MIPSDSP_Q0
;
1490 rt6
= (rt
>> 56) & MIPSDSP_Q0
;
1491 rt4
= (rt
>> 40) & MIPSDSP_Q0
;
1492 rt2
= (rt
>> 24) & MIPSDSP_Q0
;
1493 rt0
= (rt
>> 8) & MIPSDSP_Q0
;
1495 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1496 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1497 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1498 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1503 target_ulong
helper_precrq_qh_pw(target_ulong rs
, target_ulong rt
)
1505 uint16_t tempD
, tempC
, tempB
, tempA
;
1507 tempD
= (rs
>> 48) & MIPSDSP_LO
;
1508 tempC
= (rs
>> 16) & MIPSDSP_LO
;
1509 tempB
= (rt
>> 48) & MIPSDSP_LO
;
1510 tempA
= (rt
>> 16) & MIPSDSP_LO
;
1512 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1515 target_ulong
helper_precrq_rs_qh_pw(target_ulong rs
, target_ulong rt
,
1520 uint16_t tempD
, tempC
, tempB
, tempA
;
1522 rs2
= (rs
>> 32) & MIPSDSP_LLO
;
1523 rs0
= rs
& MIPSDSP_LLO
;
1524 rt2
= (rt
>> 32) & MIPSDSP_LLO
;
1525 rt0
= rt
& MIPSDSP_LLO
;
1527 tempD
= mipsdsp_trunc16_sat16_round(rs2
, env
);
1528 tempC
= mipsdsp_trunc16_sat16_round(rs0
, env
);
1529 tempB
= mipsdsp_trunc16_sat16_round(rt2
, env
);
1530 tempA
= mipsdsp_trunc16_sat16_round(rt0
, env
);
1532 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1535 target_ulong
helper_precrq_pw_l(target_ulong rs
, target_ulong rt
)
1537 uint32_t tempB
, tempA
;
1539 tempB
= (rs
>> 32) & MIPSDSP_LLO
;
1540 tempA
= (rt
>> 32) & MIPSDSP_LLO
;
1542 return MIPSDSP_RETURN64_32(tempB
, tempA
);
1546 target_ulong
helper_precrqu_s_qb_ph(target_ulong rs
, target_ulong rt
,
1549 uint8_t tempD
, tempC
, tempB
, tempA
;
1550 uint16_t rsh
, rsl
, rth
, rtl
;
1552 rsh
= (rs
& MIPSDSP_HI
) >> 16;
1553 rsl
= rs
& MIPSDSP_LO
;
1554 rth
= (rt
& MIPSDSP_HI
) >> 16;
1555 rtl
= rt
& MIPSDSP_LO
;
1557 tempD
= mipsdsp_sat8_reduce_precision(rsh
, env
);
1558 tempC
= mipsdsp_sat8_reduce_precision(rsl
, env
);
1559 tempB
= mipsdsp_sat8_reduce_precision(rth
, env
);
1560 tempA
= mipsdsp_sat8_reduce_precision(rtl
, env
);
1562 return MIPSDSP_RETURN32_8(tempD
, tempC
, tempB
, tempA
);
1565 #if defined(TARGET_MIPS64)
1566 target_ulong
helper_precrqu_s_ob_qh(target_ulong rs
, target_ulong rt
,
1570 uint16_t rs3
, rs2
, rs1
, rs0
;
1571 uint16_t rt3
, rt2
, rt1
, rt0
;
1577 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
1578 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
1580 temp
[7] = mipsdsp_sat8_reduce_precision(rs3
, env
);
1581 temp
[6] = mipsdsp_sat8_reduce_precision(rs2
, env
);
1582 temp
[5] = mipsdsp_sat8_reduce_precision(rs1
, env
);
1583 temp
[4] = mipsdsp_sat8_reduce_precision(rs0
, env
);
1584 temp
[3] = mipsdsp_sat8_reduce_precision(rt3
, env
);
1585 temp
[2] = mipsdsp_sat8_reduce_precision(rt2
, env
);
1586 temp
[1] = mipsdsp_sat8_reduce_precision(rt1
, env
);
1587 temp
[0] = mipsdsp_sat8_reduce_precision(rt0
, env
);
1589 for (i
= 0; i
< 8; i
++) {
1590 result
|= (uint64_t)temp
[i
] << (8 * i
);
1596 #define PRECEQ_PW(name, a, b) \
1597 target_ulong helper_preceq_pw_##name(target_ulong rt) \
1599 uint16_t tempB, tempA; \
1600 uint32_t tempBI, tempAI; \
1602 tempB = (rt >> a) & MIPSDSP_LO; \
1603 tempA = (rt >> b) & MIPSDSP_LO; \
1605 tempBI = (uint32_t)tempB << 16; \
1606 tempAI = (uint32_t)tempA << 16; \
1608 return MIPSDSP_RETURN64_32(tempBI, tempAI); \
1611 PRECEQ_PW(qhl
, 48, 32);
1612 PRECEQ_PW(qhr
, 16, 0);
1613 PRECEQ_PW(qhla
, 48, 16);
1614 PRECEQ_PW(qhra
, 32, 0);
1620 #define PRECEQU_PH(name, a, b) \
1621 target_ulong helper_precequ_ph_##name(target_ulong rt) \
1623 uint16_t tempB, tempA; \
1625 tempB = (rt >> a) & MIPSDSP_Q0; \
1626 tempA = (rt >> b) & MIPSDSP_Q0; \
1628 tempB = tempB << 7; \
1629 tempA = tempA << 7; \
1631 return MIPSDSP_RETURN32_16(tempB, tempA); \
1634 PRECEQU_PH(qbl
, 24, 16);
1635 PRECEQU_PH(qbr
, 8, 0);
1636 PRECEQU_PH(qbla
, 24, 8);
1637 PRECEQU_PH(qbra
, 16, 0);
1641 #if defined(TARGET_MIPS64)
1642 #define PRECEQU_QH(name, a, b, c, d) \
1643 target_ulong helper_precequ_qh_##name(target_ulong rt) \
1645 uint16_t tempD, tempC, tempB, tempA; \
1647 tempD = (rt >> a) & MIPSDSP_Q0; \
1648 tempC = (rt >> b) & MIPSDSP_Q0; \
1649 tempB = (rt >> c) & MIPSDSP_Q0; \
1650 tempA = (rt >> d) & MIPSDSP_Q0; \
1652 tempD = tempD << 7; \
1653 tempC = tempC << 7; \
1654 tempB = tempB << 7; \
1655 tempA = tempA << 7; \
1657 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1660 PRECEQU_QH(obl
, 56, 48, 40, 32);
1661 PRECEQU_QH(obr
, 24, 16, 8, 0);
1662 PRECEQU_QH(obla
, 56, 40, 24, 8);
1663 PRECEQU_QH(obra
, 48, 32, 16, 0);
1669 #define PRECEU_PH(name, a, b) \
1670 target_ulong helper_preceu_ph_##name(target_ulong rt) \
1672 uint16_t tempB, tempA; \
1674 tempB = (rt >> a) & MIPSDSP_Q0; \
1675 tempA = (rt >> b) & MIPSDSP_Q0; \
1677 return MIPSDSP_RETURN32_16(tempB, tempA); \
1680 PRECEU_PH(qbl
, 24, 16);
1681 PRECEU_PH(qbr
, 8, 0);
1682 PRECEU_PH(qbla
, 24, 8);
1683 PRECEU_PH(qbra
, 16, 0);
1687 #if defined(TARGET_MIPS64)
1688 #define PRECEU_QH(name, a, b, c, d) \
1689 target_ulong helper_preceu_qh_##name(target_ulong rt) \
1691 uint16_t tempD, tempC, tempB, tempA; \
1693 tempD = (rt >> a) & MIPSDSP_Q0; \
1694 tempC = (rt >> b) & MIPSDSP_Q0; \
1695 tempB = (rt >> c) & MIPSDSP_Q0; \
1696 tempA = (rt >> d) & MIPSDSP_Q0; \
1698 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1701 PRECEU_QH(obl
, 56, 48, 40, 32);
1702 PRECEU_QH(obr
, 24, 16, 8, 0);
1703 PRECEU_QH(obla
, 56, 40, 24, 8);
1704 PRECEU_QH(obra
, 48, 32, 16, 0);
1710 /** DSP GPR-Based Shift Sub-class insns **/
1711 #define SHIFT_QB(name, func) \
1712 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt) \
1714 uint8_t rt3, rt2, rt1, rt0; \
1718 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1720 rt3 = mipsdsp_##func(rt3, sa); \
1721 rt2 = mipsdsp_##func(rt2, sa); \
1722 rt1 = mipsdsp_##func(rt1, sa); \
1723 rt0 = mipsdsp_##func(rt0, sa); \
1725 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1728 #define SHIFT_QB_ENV(name, func) \
1729 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt,\
1730 CPUMIPSState *env) \
1732 uint8_t rt3, rt2, rt1, rt0; \
1736 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1738 rt3 = mipsdsp_##func(rt3, sa, env); \
1739 rt2 = mipsdsp_##func(rt2, sa, env); \
1740 rt1 = mipsdsp_##func(rt1, sa, env); \
1741 rt0 = mipsdsp_##func(rt0, sa, env); \
1743 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1746 SHIFT_QB_ENV(shll
, lshift8
);
1747 SHIFT_QB(shrl
, rshift_u8
);
1749 SHIFT_QB(shra
, rashift8
);
1750 SHIFT_QB(shra_r
, rnd8_rashift
);
1755 #if defined(TARGET_MIPS64)
1756 #define SHIFT_OB(name, func) \
1757 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1766 for (i = 0; i < 8; i++) { \
1767 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1768 rt_t[i] = mipsdsp_##func(rt_t[i], sa); \
1769 temp |= (uint64_t)rt_t[i] << (8 * i); \
1775 #define SHIFT_OB_ENV(name, func) \
1776 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1777 CPUMIPSState *env) \
1786 for (i = 0; i < 8; i++) { \
1787 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1788 rt_t[i] = mipsdsp_##func(rt_t[i], sa, env); \
1789 temp |= (uint64_t)rt_t[i] << (8 * i); \
1795 SHIFT_OB_ENV(shll
, lshift8
);
1796 SHIFT_OB(shrl
, rshift_u8
);
1798 SHIFT_OB(shra
, rashift8
);
1799 SHIFT_OB(shra_r
, rnd8_rashift
);
1806 #define SHIFT_PH(name, func) \
1807 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt, \
1808 CPUMIPSState *env) \
1810 uint16_t rth, rtl; \
1814 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1816 rth = mipsdsp_##func(rth, sa, env); \
1817 rtl = mipsdsp_##func(rtl, sa, env); \
1819 return MIPSDSP_RETURN32_16(rth, rtl); \
1822 SHIFT_PH(shll
, lshift16
);
1823 SHIFT_PH(shll_s
, sat16_lshift
);
1827 #if defined(TARGET_MIPS64)
1828 #define SHIFT_QH(name, func) \
1829 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa) \
1831 uint16_t rt3, rt2, rt1, rt0; \
1835 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1837 rt3 = mipsdsp_##func(rt3, sa); \
1838 rt2 = mipsdsp_##func(rt2, sa); \
1839 rt1 = mipsdsp_##func(rt1, sa); \
1840 rt0 = mipsdsp_##func(rt0, sa); \
1842 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1845 #define SHIFT_QH_ENV(name, func) \
1846 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa, \
1847 CPUMIPSState *env) \
1849 uint16_t rt3, rt2, rt1, rt0; \
1853 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1855 rt3 = mipsdsp_##func(rt3, sa, env); \
1856 rt2 = mipsdsp_##func(rt2, sa, env); \
1857 rt1 = mipsdsp_##func(rt1, sa, env); \
1858 rt0 = mipsdsp_##func(rt0, sa, env); \
1860 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1863 SHIFT_QH_ENV(shll
, lshift16
);
1864 SHIFT_QH_ENV(shll_s
, sat16_lshift
);
1866 SHIFT_QH(shrl
, rshift_u16
);
1867 SHIFT_QH(shra
, rashift16
);
1868 SHIFT_QH(shra_r
, rnd16_rashift
);
1875 #define SHIFT_W(name, func) \
1876 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1881 temp = mipsdsp_##func(rt, sa); \
1883 return (target_long)(int32_t)temp; \
1886 #define SHIFT_W_ENV(name, func) \
1887 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt, \
1888 CPUMIPSState *env) \
1893 temp = mipsdsp_##func(rt, sa, env); \
1895 return (target_long)(int32_t)temp; \
1898 SHIFT_W_ENV(shll_s
, sat32_lshift
);
1899 SHIFT_W(shra_r
, rnd32_rashift
);
1904 #if defined(TARGET_MIPS64)
1905 #define SHIFT_PW(name, func) \
1906 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa) \
1908 uint32_t rt1, rt0; \
1911 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1913 rt1 = mipsdsp_##func(rt1, sa); \
1914 rt0 = mipsdsp_##func(rt0, sa); \
1916 return MIPSDSP_RETURN64_32(rt1, rt0); \
1919 #define SHIFT_PW_ENV(name, func) \
1920 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa, \
1921 CPUMIPSState *env) \
1923 uint32_t rt1, rt0; \
1926 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1928 rt1 = mipsdsp_##func(rt1, sa, env); \
1929 rt0 = mipsdsp_##func(rt0, sa, env); \
1931 return MIPSDSP_RETURN64_32(rt1, rt0); \
1934 SHIFT_PW_ENV(shll
, lshift32
);
1935 SHIFT_PW_ENV(shll_s
, sat32_lshift
);
1937 SHIFT_PW(shra
, rashift32
);
1938 SHIFT_PW(shra_r
, rnd32_rashift
);
1945 #define SHIFT_PH(name, func) \
1946 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1948 uint16_t rth, rtl; \
1952 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1954 rth = mipsdsp_##func(rth, sa); \
1955 rtl = mipsdsp_##func(rtl, sa); \
1957 return MIPSDSP_RETURN32_16(rth, rtl); \
1960 SHIFT_PH(shrl
, rshift_u16
);
1961 SHIFT_PH(shra
, rashift16
);
1962 SHIFT_PH(shra_r
, rnd16_rashift
);
1966 /** DSP Multiply Sub-class insns **/
1967 /* Return value made up by two 16bits value.
1968 * FIXME give the macro a better name.
1970 #define MUL_RETURN32_16_PH(name, func, \
1971 rsmov1, rsmov2, rsfilter, \
1972 rtmov1, rtmov2, rtfilter) \
1973 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1974 CPUMIPSState *env) \
1976 uint16_t rsB, rsA, rtB, rtA; \
1978 rsB = (rs >> rsmov1) & rsfilter; \
1979 rsA = (rs >> rsmov2) & rsfilter; \
1980 rtB = (rt >> rtmov1) & rtfilter; \
1981 rtA = (rt >> rtmov2) & rtfilter; \
1983 rsB = mipsdsp_##func(rsB, rtB, env); \
1984 rsA = mipsdsp_##func(rsA, rtA, env); \
1986 return MIPSDSP_RETURN32_16(rsB, rsA); \
1989 MUL_RETURN32_16_PH(muleu_s_ph_qbl
, mul_u8_u16
, \
1990 24, 16, MIPSDSP_Q0
, \
1992 MUL_RETURN32_16_PH(muleu_s_ph_qbr
, mul_u8_u16
, \
1995 MUL_RETURN32_16_PH(mulq_rs_ph
, rndq15_mul_q15_q15
, \
1996 16, 0, MIPSDSP_LO
, \
1998 MUL_RETURN32_16_PH(mul_ph
, mul_i16_i16
, \
1999 16, 0, MIPSDSP_LO
, \
2001 MUL_RETURN32_16_PH(mul_s_ph
, sat16_mul_i16_i16
, \
2002 16, 0, MIPSDSP_LO
, \
2004 MUL_RETURN32_16_PH(mulq_s_ph
, sat16_mul_q15_q15
, \
2005 16, 0, MIPSDSP_LO
, \
2008 #undef MUL_RETURN32_16_PH
2010 #define MUL_RETURN32_32_ph(name, func, movbits) \
2011 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2012 CPUMIPSState *env) \
2017 rsh = (rs >> movbits) & MIPSDSP_LO; \
2018 rth = (rt >> movbits) & MIPSDSP_LO; \
2019 temp = mipsdsp_##func(rsh, rth, env); \
2021 return (target_long)(int32_t)temp; \
2024 MUL_RETURN32_32_ph(muleq_s_w_phl
, mul_q15_q15_overflowflag21
, 16);
2025 MUL_RETURN32_32_ph(muleq_s_w_phr
, mul_q15_q15_overflowflag21
, 0);
2027 #undef MUL_RETURN32_32_ph
2029 #define MUL_VOID_PH(name, use_ac_env) \
2030 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2031 CPUMIPSState *env) \
2033 int16_t rsh, rsl, rth, rtl; \
2034 int32_t tempB, tempA; \
2035 int64_t acc, dotp; \
2037 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2038 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2040 if (use_ac_env == 1) { \
2041 tempB = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2042 tempA = mipsdsp_mul_q15_q15(ac, rsl, rtl, env); \
2044 tempB = mipsdsp_mul_u16_u16(rsh, rth); \
2045 tempA = mipsdsp_mul_u16_u16(rsl, rtl); \
2048 dotp = (int64_t)tempB - (int64_t)tempA; \
2049 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2050 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2051 dotp = dotp + acc; \
2052 env->active_tc.HI[ac] = (target_long)(int32_t) \
2053 ((dotp & MIPSDSP_LHI) >> 32); \
2054 env->active_tc.LO[ac] = (target_long)(int32_t)(dotp & MIPSDSP_LLO); \
2057 MUL_VOID_PH(mulsaq_s_w_ph
, 1);
2058 MUL_VOID_PH(mulsa_w_ph
, 0);
2062 #if defined(TARGET_MIPS64)
2063 #define MUL_RETURN64_16_QH(name, func, \
2064 rsmov1, rsmov2, rsmov3, rsmov4, rsfilter, \
2065 rtmov1, rtmov2, rtmov3, rtmov4, rtfilter) \
2066 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2067 CPUMIPSState *env) \
2069 uint16_t rs3, rs2, rs1, rs0; \
2070 uint16_t rt3, rt2, rt1, rt0; \
2071 uint16_t tempD, tempC, tempB, tempA; \
2073 rs3 = (rs >> rsmov1) & rsfilter; \
2074 rs2 = (rs >> rsmov2) & rsfilter; \
2075 rs1 = (rs >> rsmov3) & rsfilter; \
2076 rs0 = (rs >> rsmov4) & rsfilter; \
2077 rt3 = (rt >> rtmov1) & rtfilter; \
2078 rt2 = (rt >> rtmov2) & rtfilter; \
2079 rt1 = (rt >> rtmov3) & rtfilter; \
2080 rt0 = (rt >> rtmov4) & rtfilter; \
2082 tempD = mipsdsp_##func(rs3, rt3, env); \
2083 tempC = mipsdsp_##func(rs2, rt2, env); \
2084 tempB = mipsdsp_##func(rs1, rt1, env); \
2085 tempA = mipsdsp_##func(rs0, rt0, env); \
2087 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
2090 MUL_RETURN64_16_QH(muleu_s_qh_obl
, mul_u8_u16
, \
2091 56, 48, 40, 32, MIPSDSP_Q0
, \
2092 48, 32, 16, 0, MIPSDSP_LO
);
2093 MUL_RETURN64_16_QH(muleu_s_qh_obr
, mul_u8_u16
, \
2094 24, 16, 8, 0, MIPSDSP_Q0
, \
2095 48, 32, 16, 0, MIPSDSP_LO
);
2096 MUL_RETURN64_16_QH(mulq_rs_qh
, rndq15_mul_q15_q15
, \
2097 48, 32, 16, 0, MIPSDSP_LO
, \
2098 48, 32, 16, 0, MIPSDSP_LO
);
2100 #undef MUL_RETURN64_16_QH
2102 #define MUL_RETURN64_32_QH(name, \
2105 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2106 CPUMIPSState *env) \
2108 uint16_t rsB, rsA; \
2109 uint16_t rtB, rtA; \
2110 uint32_t tempB, tempA; \
2112 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2113 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2114 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2115 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2117 tempB = mipsdsp_mul_q15_q15(5, rsB, rtB, env); \
2118 tempA = mipsdsp_mul_q15_q15(5, rsA, rtA, env); \
2120 return ((uint64_t)tempB << 32) | (uint64_t)tempA; \
2123 MUL_RETURN64_32_QH(muleq_s_pw_qhl
, 48, 32, 48, 32);
2124 MUL_RETURN64_32_QH(muleq_s_pw_qhr
, 16, 0, 16, 0);
2126 #undef MUL_RETURN64_32_QH
2128 void helper_mulsaq_s_w_qh(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2131 int16_t rs3
, rs2
, rs1
, rs0
;
2132 int16_t rt3
, rt2
, rt1
, rt0
;
2133 int32_t tempD
, tempC
, tempB
, tempA
;
2138 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
2139 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
2141 tempD
= mipsdsp_mul_q15_q15(ac
, rs3
, rt3
, env
);
2142 tempC
= mipsdsp_mul_q15_q15(ac
, rs2
, rt2
, env
);
2143 tempB
= mipsdsp_mul_q15_q15(ac
, rs1
, rt1
, env
);
2144 tempA
= mipsdsp_mul_q15_q15(ac
, rs0
, rt0
, env
);
2146 temp
[0] = ((int32_t)tempD
- (int32_t)tempC
) +
2147 ((int32_t)tempB
- (int32_t)tempA
);
2148 temp
[0] = (int64_t)(temp
[0] << 30) >> 30;
2149 if (((temp
[0] >> 33) & 0x01) == 0) {
2155 acc
[0] = env
->active_tc
.LO
[ac
];
2156 acc
[1] = env
->active_tc
.HI
[ac
];
2158 temp_sum
= acc
[0] + temp
[0];
2159 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2160 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2166 env
->active_tc
.HI
[ac
] = acc
[1];
2167 env
->active_tc
.LO
[ac
] = acc
[0];
2171 #define DP_QB(name, func, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2172 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2173 CPUMIPSState *env) \
2177 uint16_t tempB, tempA; \
2178 uint64_t tempC, dotp; \
2180 rs3 = (rs >> rsmov1) & MIPSDSP_Q0; \
2181 rs2 = (rs >> rsmov2) & MIPSDSP_Q0; \
2182 rt3 = (rt >> rtmov1) & MIPSDSP_Q0; \
2183 rt2 = (rt >> rtmov2) & MIPSDSP_Q0; \
2184 tempB = mipsdsp_##func(rs3, rt3); \
2185 tempA = mipsdsp_##func(rs2, rt2); \
2186 dotp = (int64_t)tempB + (int64_t)tempA; \
2188 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2189 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2192 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2193 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2197 env->active_tc.HI[ac] = (target_long)(int32_t) \
2198 ((tempC & MIPSDSP_LHI) >> 32); \
2199 env->active_tc.LO[ac] = (target_long)(int32_t)(tempC & MIPSDSP_LLO); \
2202 DP_QB(dpau_h_qbl
, mul_u8_u8
, 1, 24, 16, 24, 16);
2203 DP_QB(dpau_h_qbr
, mul_u8_u8
, 1, 8, 0, 8, 0);
2204 DP_QB(dpsu_h_qbl
, mul_u8_u8
, 0, 24, 16, 24, 16);
2205 DP_QB(dpsu_h_qbr
, mul_u8_u8
, 0, 8, 0, 8, 0);
2209 #if defined(TARGET_MIPS64)
2210 #define DP_OB(name, add_sub, \
2211 rsmov1, rsmov2, rsmov3, rsmov4, \
2212 rtmov1, rtmov2, rtmov3, rtmov4) \
2213 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2214 CPUMIPSState *env) \
2216 uint8_t rsD, rsC, rsB, rsA; \
2217 uint8_t rtD, rtC, rtB, rtA; \
2218 uint16_t tempD, tempC, tempB, tempA; \
2221 uint64_t temp_sum; \
2226 rsD = (rs >> rsmov1) & MIPSDSP_Q0; \
2227 rsC = (rs >> rsmov2) & MIPSDSP_Q0; \
2228 rsB = (rs >> rsmov3) & MIPSDSP_Q0; \
2229 rsA = (rs >> rsmov4) & MIPSDSP_Q0; \
2230 rtD = (rt >> rtmov1) & MIPSDSP_Q0; \
2231 rtC = (rt >> rtmov2) & MIPSDSP_Q0; \
2232 rtB = (rt >> rtmov3) & MIPSDSP_Q0; \
2233 rtA = (rt >> rtmov4) & MIPSDSP_Q0; \
2235 tempD = mipsdsp_mul_u8_u8(rsD, rtD); \
2236 tempC = mipsdsp_mul_u8_u8(rsC, rtC); \
2237 tempB = mipsdsp_mul_u8_u8(rsB, rtB); \
2238 tempA = mipsdsp_mul_u8_u8(rsA, rtA); \
2240 temp[0] = (uint64_t)tempD + (uint64_t)tempC + \
2241 (uint64_t)tempB + (uint64_t)tempA; \
2243 acc[0] = env->active_tc.LO[ac]; \
2244 acc[1] = env->active_tc.HI[ac]; \
2247 temp_sum = acc[0] + temp[0]; \
2248 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2249 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2252 temp[0] = temp_sum; \
2253 temp[1] = acc[1] + temp[1]; \
2255 temp_sum = acc[0] - temp[0]; \
2256 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2259 temp[0] = temp_sum; \
2260 temp[1] = acc[1] - temp[1]; \
2263 env->active_tc.HI[ac] = temp[1]; \
2264 env->active_tc.LO[ac] = temp[0]; \
2267 DP_OB(dpau_h_obl
, 1, 56, 48, 40, 32, 56, 48, 40, 32);
2268 DP_OB(dpau_h_obr
, 1, 24, 16, 8, 0, 24, 16, 8, 0);
2269 DP_OB(dpsu_h_obl
, 0, 56, 48, 40, 32, 56, 48, 40, 32);
2270 DP_OB(dpsu_h_obr
, 0, 24, 16, 8, 0, 24, 16, 8, 0);
2275 #define DP_NOFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2276 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2277 CPUMIPSState *env) \
2279 int16_t rsB, rsA, rtB, rtA; \
2280 int32_t tempA, tempB; \
2283 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2284 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2285 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2286 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2288 tempB = (int32_t)rsB * (int32_t)rtB; \
2289 tempA = (int32_t)rsA * (int32_t)rtA; \
2291 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2292 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2295 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2297 acc = acc - ((int64_t)tempB + (int64_t)tempA); \
2300 env->active_tc.HI[ac] = (target_long)(int32_t)((acc & MIPSDSP_LHI) >> 32); \
2301 env->active_tc.LO[ac] = (target_long)(int32_t)(acc & MIPSDSP_LLO); \
2304 DP_NOFUNC_PH(dpa_w_ph
, 1, 16, 0, 16, 0);
2305 DP_NOFUNC_PH(dpax_w_ph
, 1, 16, 0, 0, 16);
2306 DP_NOFUNC_PH(dps_w_ph
, 0, 16, 0, 16, 0);
2307 DP_NOFUNC_PH(dpsx_w_ph
, 0, 16, 0, 0, 16);
2310 #define DP_HASFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2311 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2312 CPUMIPSState *env) \
2314 int16_t rsB, rsA, rtB, rtA; \
2315 int32_t tempB, tempA; \
2316 int64_t acc, dotp; \
2318 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2319 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2320 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2321 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2323 tempB = mipsdsp_mul_q15_q15(ac, rsB, rtB, env); \
2324 tempA = mipsdsp_mul_q15_q15(ac, rsA, rtA, env); \
2326 dotp = (int64_t)tempB + (int64_t)tempA; \
2327 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2328 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2336 env->active_tc.HI[ac] = (target_long)(int32_t) \
2337 ((acc & MIPSDSP_LHI) >> 32); \
2338 env->active_tc.LO[ac] = (target_long)(int32_t) \
2339 (acc & MIPSDSP_LLO); \
2342 DP_HASFUNC_PH(dpaq_s_w_ph
, 1, 16, 0, 16, 0);
2343 DP_HASFUNC_PH(dpaqx_s_w_ph
, 1, 16, 0, 0, 16);
2344 DP_HASFUNC_PH(dpsq_s_w_ph
, 0, 16, 0, 16, 0);
2345 DP_HASFUNC_PH(dpsqx_s_w_ph
, 0, 16, 0, 0, 16);
2347 #undef DP_HASFUNC_PH
2349 #define DP_128OPERATION_PH(name, is_add) \
2350 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2351 CPUMIPSState *env) \
2353 int16_t rsh, rsl, rth, rtl; \
2354 int32_t tempB, tempA, tempC62_31, tempC63; \
2355 int64_t acc, dotp, tempC; \
2357 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2358 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2360 tempB = mipsdsp_mul_q15_q15(ac, rsh, rtl, env); \
2361 tempA = mipsdsp_mul_q15_q15(ac, rsl, rth, env); \
2363 dotp = (int64_t)tempB + (int64_t)tempA; \
2364 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2365 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2367 tempC = acc + dotp; \
2369 tempC = acc - dotp; \
2371 tempC63 = (tempC >> 63) & 0x01; \
2372 tempC62_31 = (tempC >> 31) & 0xFFFFFFFF; \
2374 if ((tempC63 == 0) && (tempC62_31 != 0x00000000)) { \
2375 tempC = 0x7FFFFFFF; \
2376 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2379 if ((tempC63 == 1) && (tempC62_31 != 0xFFFFFFFF)) { \
2380 tempC = (int64_t)(int32_t)0x80000000; \
2381 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2384 env->active_tc.HI[ac] = (target_long)(int32_t) \
2385 ((tempC & MIPSDSP_LHI) >> 32); \
2386 env->active_tc.LO[ac] = (target_long)(int32_t) \
2387 (tempC & MIPSDSP_LLO); \
2390 DP_128OPERATION_PH(dpaqx_sa_w_ph
, 1);
2391 DP_128OPERATION_PH(dpsqx_sa_w_ph
, 0);
2393 #undef DP_128OPERATION_HP
2395 #if defined(TARGET_MIPS64)
2396 #define DP_QH(name, is_add, use_ac_env) \
2397 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2398 CPUMIPSState *env) \
2400 int32_t rs3, rs2, rs1, rs0; \
2401 int32_t rt3, rt2, rt1, rt0; \
2402 int32_t tempD, tempC, tempB, tempA; \
2407 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2408 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2411 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env); \
2412 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env); \
2413 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env); \
2414 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env); \
2416 tempD = mipsdsp_mul_u16_u16(rs3, rt3); \
2417 tempC = mipsdsp_mul_u16_u16(rs2, rt2); \
2418 tempB = mipsdsp_mul_u16_u16(rs1, rt1); \
2419 tempA = mipsdsp_mul_u16_u16(rs0, rt0); \
2422 temp[0] = (int64_t)tempD + (int64_t)tempC + \
2423 (int64_t)tempB + (int64_t)tempA; \
2425 if (temp[0] >= 0) { \
2431 acc[1] = env->active_tc.HI[ac]; \
2432 acc[0] = env->active_tc.LO[ac]; \
2435 temp_sum = acc[0] + temp[0]; \
2436 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2437 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2438 acc[1] = acc[1] + 1; \
2440 temp[0] = temp_sum; \
2441 temp[1] = acc[1] + temp[1]; \
2443 temp_sum = acc[0] - temp[0]; \
2444 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2445 acc[1] = acc[1] - 1; \
2447 temp[0] = temp_sum; \
2448 temp[1] = acc[1] - temp[1]; \
2451 env->active_tc.HI[ac] = temp[1]; \
2452 env->active_tc.LO[ac] = temp[0]; \
2455 DP_QH(dpa_w_qh
, 1, 0);
2456 DP_QH(dpaq_s_w_qh
, 1, 1);
2457 DP_QH(dps_w_qh
, 0, 0);
2458 DP_QH(dpsq_s_w_qh
, 0, 1);
2464 #define DP_L_W(name, is_add) \
2465 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2466 CPUMIPSState *env) \
2469 int64_t dotp, acc; \
2473 dotp = mipsdsp_mul_q31_q31(ac, rs, rt, env); \
2474 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2475 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2477 temp = acc + dotp; \
2478 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2479 temp, (0x01ull << 63)); \
2481 temp = acc - dotp; \
2482 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2483 temp, (0x01ull << 63)); \
2487 temp63 = (temp >> 63) & 0x01; \
2488 if (temp63 == 1) { \
2489 temp = (0x01ull << 63) - 1; \
2491 temp = 0x01ull << 63; \
2494 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2497 env->active_tc.HI[ac] = (target_long)(int32_t) \
2498 ((temp & MIPSDSP_LHI) >> 32); \
2499 env->active_tc.LO[ac] = (target_long)(int32_t) \
2500 (temp & MIPSDSP_LLO); \
2503 DP_L_W(dpaq_sa_l_w
, 1);
2504 DP_L_W(dpsq_sa_l_w
, 0);
2508 #if defined(TARGET_MIPS64)
2509 #define DP_L_PW(name, func) \
2510 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2511 CPUMIPSState *env) \
2515 int64_t tempB[2], tempA[2]; \
2523 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2524 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2526 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env); \
2527 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env); \
2529 if (tempB[0] >= 0) { \
2535 if (tempA[0] >= 0) { \
2541 temp_sum = tempB[0] + tempA[0]; \
2542 if (((uint64_t)temp_sum < (uint64_t)tempB[0]) && \
2543 ((uint64_t)temp_sum < (uint64_t)tempA[0])) { \
2546 temp[0] = temp_sum; \
2547 temp[1] += tempB[1] + tempA[1]; \
2549 mipsdsp_##func(acc, ac, temp, env); \
2551 env->active_tc.HI[ac] = acc[1]; \
2552 env->active_tc.LO[ac] = acc[0]; \
2555 DP_L_PW(dpaq_sa_l_pw
, sat64_acc_add_q63
);
2556 DP_L_PW(dpsq_sa_l_pw
, sat64_acc_sub_q63
);
2560 void helper_mulsaq_s_l_pw(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2565 int64_t tempB
[2], tempA
[2];
2570 rs1
= (rs
>> 32) & MIPSDSP_LLO
;
2571 rs0
= rs
& MIPSDSP_LLO
;
2572 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
2573 rt0
= rt
& MIPSDSP_LLO
;
2575 tempB
[0] = mipsdsp_mul_q31_q31(ac
, rs1
, rt1
, env
);
2576 tempA
[0] = mipsdsp_mul_q31_q31(ac
, rs0
, rt0
, env
);
2578 if (tempB
[0] >= 0) {
2584 if (tempA
[0] >= 0) {
2590 acc
[0] = env
->active_tc
.LO
[ac
];
2591 acc
[1] = env
->active_tc
.HI
[ac
];
2593 temp_sum
= tempB
[0] - tempA
[0];
2594 if ((uint64_t)temp_sum
> (uint64_t)tempB
[0]) {
2598 temp
[1] = tempB
[1] - tempA
[1];
2600 if ((temp
[1] & 0x01) == 0) {
2606 temp_sum
= acc
[0] + temp
[0];
2607 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2608 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2614 env
->active_tc
.HI
[ac
] = acc
[1];
2615 env
->active_tc
.LO
[ac
] = acc
[0];
2619 #define MAQ_S_W(name, mov) \
2620 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2621 CPUMIPSState *env) \
2625 int64_t tempL, acc; \
2627 rsh = (rs >> mov) & MIPSDSP_LO; \
2628 rth = (rt >> mov) & MIPSDSP_LO; \
2629 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2630 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2631 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2632 tempL = (int64_t)tempA + acc; \
2633 env->active_tc.HI[ac] = (target_long)(int32_t) \
2634 ((tempL & MIPSDSP_LHI) >> 32); \
2635 env->active_tc.LO[ac] = (target_long)(int32_t) \
2636 (tempL & MIPSDSP_LLO); \
2639 MAQ_S_W(maq_s_w_phl
, 16);
2640 MAQ_S_W(maq_s_w_phr
, 0);
2644 #define MAQ_SA_W(name, mov) \
2645 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2646 CPUMIPSState *env) \
2651 rsh = (rs >> mov) & MIPSDSP_LO; \
2652 rth = (rt >> mov) & MIPSDSP_LO; \
2653 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2654 tempA = mipsdsp_sat32_acc_q31(ac, tempA, env); \
2656 env->active_tc.HI[ac] = (target_long)(int32_t)(((int64_t)tempA & \
2657 MIPSDSP_LHI) >> 32); \
2658 env->active_tc.LO[ac] = (target_long)(int32_t)((int64_t)tempA & \
2662 MAQ_SA_W(maq_sa_w_phl
, 16);
2663 MAQ_SA_W(maq_sa_w_phr
, 0);
2667 #define MULQ_W(name, addvar) \
2668 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2669 CPUMIPSState *env) \
2671 int32_t rs_t, rt_t; \
2675 rs_t = rs & MIPSDSP_LLO; \
2676 rt_t = rt & MIPSDSP_LLO; \
2678 if ((rs_t == 0x80000000) && (rt_t == 0x80000000)) { \
2679 tempL = 0x7FFFFFFF00000000ull; \
2680 set_DSPControl_overflow_flag(1, 21, env); \
2682 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2685 tempI = (tempL & MIPSDSP_LHI) >> 32; \
2687 return (target_long)(int32_t)tempI; \
2690 MULQ_W(mulq_s_w
, 0);
2691 MULQ_W(mulq_rs_w
, 0x80000000ull
);
2695 #if defined(TARGET_MIPS64)
2697 #define MAQ_S_W_QH(name, mov) \
2698 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2699 CPUMIPSState *env) \
2701 int16_t rs_t, rt_t; \
2710 rs_t = (rs >> mov) & MIPSDSP_LO; \
2711 rt_t = (rt >> mov) & MIPSDSP_LO; \
2712 temp_mul = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2714 temp[0] = (int64_t)temp_mul; \
2715 if (temp[0] >= 0) { \
2721 acc[0] = env->active_tc.LO[ac]; \
2722 acc[1] = env->active_tc.HI[ac]; \
2724 temp_sum = acc[0] + temp[0]; \
2725 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2726 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2729 acc[0] = temp_sum; \
2730 acc[1] += temp[1]; \
2732 env->active_tc.HI[ac] = acc[1]; \
2733 env->active_tc.LO[ac] = acc[0]; \
2736 MAQ_S_W_QH(maq_s_w_qhll
, 48);
2737 MAQ_S_W_QH(maq_s_w_qhlr
, 32);
2738 MAQ_S_W_QH(maq_s_w_qhrl
, 16);
2739 MAQ_S_W_QH(maq_s_w_qhrr
, 0);
2743 #define MAQ_SA_W(name, mov) \
2744 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2745 CPUMIPSState *env) \
2747 int16_t rs_t, rt_t; \
2751 rs_t = (rs >> mov) & MIPSDSP_LO; \
2752 rt_t = (rt >> mov) & MIPSDSP_LO; \
2753 temp = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2754 temp = mipsdsp_sat32_acc_q31(ac, temp, env); \
2756 acc[0] = (int64_t)(int32_t)temp; \
2757 if (acc[0] >= 0) { \
2763 env->active_tc.HI[ac] = acc[1]; \
2764 env->active_tc.LO[ac] = acc[0]; \
2767 MAQ_SA_W(maq_sa_w_qhll
, 48);
2768 MAQ_SA_W(maq_sa_w_qhlr
, 32);
2769 MAQ_SA_W(maq_sa_w_qhrl
, 16);
2770 MAQ_SA_W(maq_sa_w_qhrr
, 0);
2774 #define MAQ_S_L_PW(name, mov) \
2775 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2776 CPUMIPSState *env) \
2778 int32_t rs_t, rt_t; \
2786 rs_t = (rs >> mov) & MIPSDSP_LLO; \
2787 rt_t = (rt >> mov) & MIPSDSP_LLO; \
2789 temp[0] = mipsdsp_mul_q31_q31(ac, rs_t, rt_t, env); \
2790 if (temp[0] >= 0) { \
2796 acc[0] = env->active_tc.LO[ac]; \
2797 acc[1] = env->active_tc.HI[ac]; \
2799 temp_sum = acc[0] + temp[0]; \
2800 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2801 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2804 acc[0] = temp_sum; \
2805 acc[1] += temp[1]; \
2807 env->active_tc.HI[ac] = acc[1]; \
2808 env->active_tc.LO[ac] = acc[0]; \
2811 MAQ_S_L_PW(maq_s_l_pwl
, 32);
2812 MAQ_S_L_PW(maq_s_l_pwr
, 0);
2816 #define DM_OPERATE(name, func, is_add, sigext) \
2817 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2818 CPUMIPSState *env) \
2822 int64_t tempBL[2], tempAL[2]; \
2830 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2831 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2834 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2835 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2837 if (tempBL[0] >= 0) { \
2840 tempBL[1] = ~0ull; \
2843 if (tempAL[0] >= 0) { \
2846 tempAL[1] = ~0ull; \
2849 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2850 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2855 acc[1] = env->active_tc.HI[ac]; \
2856 acc[0] = env->active_tc.LO[ac]; \
2858 temp_sum = tempBL[0] + tempAL[0]; \
2859 if (((uint64_t)temp_sum < (uint64_t)tempBL[0]) && \
2860 ((uint64_t)temp_sum < (uint64_t)tempAL[0])) { \
2863 temp[0] = temp_sum; \
2864 temp[1] += tempBL[1] + tempAL[1]; \
2867 temp_sum = acc[0] + temp[0]; \
2868 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2869 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2872 temp[0] = temp_sum; \
2873 temp[1] = acc[1] + temp[1]; \
2875 temp_sum = acc[0] - temp[0]; \
2876 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2879 temp[0] = temp_sum; \
2880 temp[1] = acc[1] - temp[1]; \
2883 env->active_tc.HI[ac] = temp[1]; \
2884 env->active_tc.LO[ac] = temp[0]; \
2887 DM_OPERATE(dmadd
, mul_i32_i32
, 1, 1);
2888 DM_OPERATE(dmaddu
, mul_u32_u32
, 1, 0);
2889 DM_OPERATE(dmsub
, mul_i32_i32
, 0, 1);
2890 DM_OPERATE(dmsubu
, mul_u32_u32
, 0, 0);
2894 /** DSP Bit/Manipulation Sub-class insns **/
2895 target_ulong
helper_bitrev(target_ulong rt
)
2901 temp
= rt
& MIPSDSP_LO
;
2903 for (i
= 0; i
< 16; i
++) {
2904 rd
= (rd
<< 1) | (temp
& 1);
2908 return (target_ulong
)rd
;
2911 #define BIT_INSV(name, posfilter, ret_type) \
2912 target_ulong helper_##name(CPUMIPSState *env, target_ulong rs, \
2915 uint32_t pos, size, msb, lsb; \
2916 uint32_t const sizefilter = 0x3F; \
2917 target_ulong temp; \
2918 target_ulong dspc; \
2920 dspc = env->active_tc.DSPControl; \
2922 pos = dspc & posfilter; \
2923 size = (dspc >> 7) & sizefilter; \
2925 msb = pos + size - 1; \
2928 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2932 temp = deposit64(rt, pos, size, rs); \
2934 return (target_long)(ret_type)temp; \
2937 BIT_INSV(insv
, 0x1F, int32_t);
2938 #ifdef TARGET_MIPS64
2939 BIT_INSV(dinsv
, 0x7F, target_long
);
2945 /** DSP Compare-Pick Sub-class insns **/
2946 #define CMP_HAS_RET(name, func, split_num, filter, bit_size) \
2947 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
2949 uint32_t rs_t, rt_t; \
2951 uint32_t temp = 0; \
2954 for (i = 0; i < split_num; i++) { \
2955 rs_t = (rs >> (bit_size * i)) & filter; \
2956 rt_t = (rt >> (bit_size * i)) & filter; \
2957 cc = mipsdsp_##func(rs_t, rt_t); \
2961 return (target_ulong)temp; \
2964 CMP_HAS_RET(cmpgu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2965 CMP_HAS_RET(cmpgu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2966 CMP_HAS_RET(cmpgu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
2968 #ifdef TARGET_MIPS64
2969 CMP_HAS_RET(cmpgu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
2970 CMP_HAS_RET(cmpgu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
2971 CMP_HAS_RET(cmpgu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
2977 #define CMP_NO_RET(name, func, split_num, filter, bit_size) \
2978 void helper_##name(target_ulong rs, target_ulong rt, \
2979 CPUMIPSState *env) \
2981 int##bit_size##_t rs_t, rt_t; \
2982 int##bit_size##_t flag = 0; \
2983 int##bit_size##_t cc; \
2986 for (i = 0; i < split_num; i++) { \
2987 rs_t = (rs >> (bit_size * i)) & filter; \
2988 rt_t = (rt >> (bit_size * i)) & filter; \
2990 cc = mipsdsp_##func((int32_t)rs_t, (int32_t)rt_t); \
2994 set_DSPControl_24(flag, split_num, env); \
2997 CMP_NO_RET(cmpu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2998 CMP_NO_RET(cmpu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2999 CMP_NO_RET(cmpu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
3001 CMP_NO_RET(cmp_eq_ph
, cmp_eq
, 2, MIPSDSP_LO
, 16);
3002 CMP_NO_RET(cmp_lt_ph
, cmp_lt
, 2, MIPSDSP_LO
, 16);
3003 CMP_NO_RET(cmp_le_ph
, cmp_le
, 2, MIPSDSP_LO
, 16);
3005 #ifdef TARGET_MIPS64
3006 CMP_NO_RET(cmpu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
3007 CMP_NO_RET(cmpu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
3008 CMP_NO_RET(cmpu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
3010 CMP_NO_RET(cmp_eq_qh
, cmp_eq
, 4, MIPSDSP_LO
, 16);
3011 CMP_NO_RET(cmp_lt_qh
, cmp_lt
, 4, MIPSDSP_LO
, 16);
3012 CMP_NO_RET(cmp_le_qh
, cmp_le
, 4, MIPSDSP_LO
, 16);
3014 CMP_NO_RET(cmp_eq_pw
, cmp_eq
, 2, MIPSDSP_LLO
, 32);
3015 CMP_NO_RET(cmp_lt_pw
, cmp_lt
, 2, MIPSDSP_LLO
, 32);
3016 CMP_NO_RET(cmp_le_pw
, cmp_le
, 2, MIPSDSP_LLO
, 32);
3020 #if defined(TARGET_MIPS64)
3022 #define CMPGDU_OB(name) \
3023 target_ulong helper_cmpgdu_##name##_ob(target_ulong rs, target_ulong rt, \
3024 CPUMIPSState *env) \
3027 uint8_t rs_t, rt_t; \
3032 for (i = 0; i < 8; i++) { \
3033 rs_t = (rs >> (8 * i)) & MIPSDSP_Q0; \
3034 rt_t = (rt >> (8 * i)) & MIPSDSP_Q0; \
3036 if (mipsdsp_cmpu_##name(rs_t, rt_t)) { \
3037 cond |= 0x01 << i; \
3041 set_DSPControl_24(cond, 8, env); \
3043 return (uint64_t)cond; \
3052 #define PICK_INSN(name, split_num, filter, bit_size, ret32bit) \
3053 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
3054 CPUMIPSState *env) \
3056 uint32_t rs_t, rt_t; \
3060 target_ulong result = 0; \
3062 dsp = env->active_tc.DSPControl; \
3063 for (i = 0; i < split_num; i++) { \
3064 rs_t = (rs >> (bit_size * i)) & filter; \
3065 rt_t = (rt >> (bit_size * i)) & filter; \
3066 cc = (dsp >> (24 + i)) & 0x01; \
3067 cc = cc == 1 ? rs_t : rt_t; \
3069 result |= (target_ulong)cc << (bit_size * i); \
3073 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3079 PICK_INSN(pick_qb
, 4, MIPSDSP_Q0
, 8, 1);
3080 PICK_INSN(pick_ph
, 2, MIPSDSP_LO
, 16, 1);
3082 #ifdef TARGET_MIPS64
3083 PICK_INSN(pick_ob
, 8, MIPSDSP_Q0
, 8, 0);
3084 PICK_INSN(pick_qh
, 4, MIPSDSP_LO
, 16, 0);
3085 PICK_INSN(pick_pw
, 2, MIPSDSP_LLO
, 32, 0);
3089 target_ulong
helper_packrl_ph(target_ulong rs
, target_ulong rt
)
3093 rsl
= rs
& MIPSDSP_LO
;
3094 rth
= (rt
& MIPSDSP_HI
) >> 16;
3096 return (target_long
)(int32_t)((rsl
<< 16) | rth
);
3099 #if defined(TARGET_MIPS64)
3100 target_ulong
helper_packrl_pw(target_ulong rs
, target_ulong rt
)
3104 rs0
= rs
& MIPSDSP_LLO
;
3105 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
3107 return ((uint64_t)rs0
<< 32) | (uint64_t)rt1
;
3111 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3112 target_ulong
helper_extr_w(target_ulong ac
, target_ulong shift
,
3118 shift
= shift
& 0x1F;
3120 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3121 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3122 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3123 set_DSPControl_overflow_flag(1, 23, env
);
3126 tempI
= (tempDL
[0] >> 1) & MIPSDSP_LLO
;
3129 if (tempDL
[0] == 0) {
3133 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3134 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3135 set_DSPControl_overflow_flag(1, 23, env
);
3138 return (target_long
)tempI
;
3141 target_ulong
helper_extr_r_w(target_ulong ac
, target_ulong shift
,
3146 shift
= shift
& 0x1F;
3148 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3149 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3150 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3151 set_DSPControl_overflow_flag(1, 23, env
);
3155 if (tempDL
[0] == 0) {
3159 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3160 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3161 set_DSPControl_overflow_flag(1, 23, env
);
3164 return (target_long
)(int32_t)(tempDL
[0] >> 1);
3167 target_ulong
helper_extr_rs_w(target_ulong ac
, target_ulong shift
,
3170 int32_t tempI
, temp64
;
3173 shift
= shift
& 0x1F;
3175 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3176 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3177 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3178 set_DSPControl_overflow_flag(1, 23, env
);
3181 if (tempDL
[0] == 0) {
3184 tempI
= tempDL
[0] >> 1;
3186 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3187 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3188 temp64
= tempDL
[1] & 0x01;
3194 set_DSPControl_overflow_flag(1, 23, env
);
3197 return (target_long
)tempI
;
3200 #if defined(TARGET_MIPS64)
3201 target_ulong
helper_dextr_w(target_ulong ac
, target_ulong shift
,
3206 shift
= shift
& 0x3F;
3208 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3210 return (int64_t)(int32_t)(temp
[0] >> 1);
3213 target_ulong
helper_dextr_r_w(target_ulong ac
, target_ulong shift
,
3219 shift
= shift
& 0x3F;
3220 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3230 temp128
= temp
[2] & 0x01;
3232 if ((temp128
!= 0 || temp
[1] != 0) &&
3233 (temp128
!= 1 || temp
[1] != ~0ull)) {
3234 set_DSPControl_overflow_flag(1, 23, env
);
3237 return (int64_t)(int32_t)(temp
[0] >> 1);
3240 target_ulong
helper_dextr_rs_w(target_ulong ac
, target_ulong shift
,
3246 shift
= shift
& 0x3F;
3247 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3257 temp128
= temp
[2] & 0x01;
3259 if ((temp128
!= 0 || temp
[1] != 0) &&
3260 (temp128
!= 1 || temp
[1] != ~0ull)) {
3262 temp
[0] = 0x0FFFFFFFF;
3264 temp
[0] = 0x0100000000ULL
;
3266 set_DSPControl_overflow_flag(1, 23, env
);
3269 return (int64_t)(int32_t)(temp
[0] >> 1);
3272 target_ulong
helper_dextr_l(target_ulong ac
, target_ulong shift
,
3276 target_ulong result
;
3278 shift
= shift
& 0x3F;
3280 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3281 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3286 target_ulong
helper_dextr_r_l(target_ulong ac
, target_ulong shift
,
3291 target_ulong result
;
3293 shift
= shift
& 0x3F;
3294 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3304 temp128
= temp
[2] & 0x01;
3306 if ((temp128
!= 0 || temp
[1] != 0) &&
3307 (temp128
!= 1 || temp
[1] != ~0ull)) {
3308 set_DSPControl_overflow_flag(1, 23, env
);
3311 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3316 target_ulong
helper_dextr_rs_l(target_ulong ac
, target_ulong shift
,
3321 target_ulong result
;
3323 shift
= shift
& 0x3F;
3324 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3334 temp128
= temp
[2] & 0x01;
3336 if ((temp128
!= 0 || temp
[1] != 0) &&
3337 (temp128
!= 1 || temp
[1] != ~0ull)) {
3339 temp
[1] &= ~0x00ull
- 1;
3340 temp
[0] |= ~0x00ull
- 1;
3345 set_DSPControl_overflow_flag(1, 23, env
);
3347 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3353 target_ulong
helper_extr_s_h(target_ulong ac
, target_ulong shift
,
3358 shift
= shift
& 0x1F;
3360 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
3361 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
3363 temp
= acc
>> shift
;
3365 if (temp
> (int64_t)0x7FFF) {
3367 set_DSPControl_overflow_flag(1, 23, env
);
3368 } else if (temp
< (int64_t)0xFFFFFFFFFFFF8000ULL
) {
3370 set_DSPControl_overflow_flag(1, 23, env
);
3373 return (target_long
)(int32_t)(temp
& 0xFFFFFFFF);
3377 #if defined(TARGET_MIPS64)
3378 target_ulong
helper_dextr_s_h(target_ulong ac
, target_ulong shift
,
3384 shift
= shift
& 0x1F;
3386 mipsdsp_rashift_acc((uint64_t *)temp
, ac
, shift
, env
);
3388 temp127
= (temp
[1] >> 63) & 0x01;
3390 if ((temp127
== 0) && (temp
[1] > 0 || temp
[0] > 32767)) {
3391 temp
[0] &= 0xFFFF0000;
3392 temp
[0] |= 0x00007FFF;
3393 set_DSPControl_overflow_flag(1, 23, env
);
3394 } else if ((temp127
== 1) &&
3395 (temp
[1] < 0xFFFFFFFFFFFFFFFFll
3396 || temp
[0] < 0xFFFFFFFFFFFF1000ll
)) {
3397 temp
[0] &= 0xFFFF0000;
3398 temp
[0] |= 0x00008000;
3399 set_DSPControl_overflow_flag(1, 23, env
);
3402 return (int64_t)(int16_t)(temp
[0] & MIPSDSP_LO
);
3407 target_ulong
helper_extp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3417 start_pos
= get_DSPControl_pos(env
);
3418 sub
= start_pos
- (size
+ 1);
3420 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3421 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3422 temp
= (acc
>> (start_pos
- size
)) & (~0U >> (31 - size
));
3423 set_DSPControl_efi(0, env
);
3425 set_DSPControl_efi(1, env
);
3428 return (target_ulong
)temp
;
3431 target_ulong
helper_extpdp(target_ulong ac
, target_ulong size
,
3441 start_pos
= get_DSPControl_pos(env
);
3442 sub
= start_pos
- (size
+ 1);
3444 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3445 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3446 temp
= extract64(acc
, start_pos
- size
, size
+ 1);
3448 set_DSPControl_pos(sub
, env
);
3449 set_DSPControl_efi(0, env
);
3451 set_DSPControl_efi(1, env
);
3454 return (target_ulong
)temp
;
3458 #if defined(TARGET_MIPS64)
3459 target_ulong
helper_dextp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3464 uint64_t tempB
, tempA
;
3470 start_pos
= get_DSPControl_pos(env
);
3471 len
= start_pos
- size
;
3472 tempB
= env
->active_tc
.HI
[ac
];
3473 tempA
= env
->active_tc
.LO
[ac
];
3475 sub
= start_pos
- (size
+ 1);
3478 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3479 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3480 set_DSPControl_efi(0, env
);
3482 set_DSPControl_efi(1, env
);
3488 target_ulong
helper_dextpdp(target_ulong ac
, target_ulong size
,
3494 uint64_t tempB
, tempA
;
3499 start_pos
= get_DSPControl_pos(env
);
3500 len
= start_pos
- size
;
3501 tempB
= env
->active_tc
.HI
[ac
];
3502 tempA
= env
->active_tc
.LO
[ac
];
3504 sub
= start_pos
- (size
+ 1);
3507 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3508 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3509 set_DSPControl_pos(sub
, env
);
3510 set_DSPControl_efi(0, env
);
3512 set_DSPControl_efi(1, env
);
3520 void helper_shilo(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3526 rs5_0
= (int8_t)(rs5_0
<< 2) >> 2;
3528 if (unlikely(rs5_0
== 0)) {
3532 acc
= (((uint64_t)env
->active_tc
.HI
[ac
] << 32) & MIPSDSP_LHI
) |
3533 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3536 temp
= acc
>> rs5_0
;
3538 temp
= acc
<< -rs5_0
;
3541 env
->active_tc
.HI
[ac
] = (target_ulong
)(int32_t)((temp
& MIPSDSP_LHI
) >> 32);
3542 env
->active_tc
.LO
[ac
] = (target_ulong
)(int32_t)(temp
& MIPSDSP_LLO
);
3545 #if defined(TARGET_MIPS64)
3546 void helper_dshilo(target_ulong shift
, target_ulong ac
, CPUMIPSState
*env
)
3549 uint64_t tempB
, tempA
;
3551 shift_t
= (int8_t)(shift
<< 1) >> 1;
3553 tempB
= env
->active_tc
.HI
[ac
];
3554 tempA
= env
->active_tc
.LO
[ac
];
3558 tempA
= (tempB
<< (64 - shift_t
)) | (tempA
>> shift_t
);
3559 tempB
= tempB
>> shift_t
;
3562 tempB
= (tempB
<< shift_t
) | (tempA
>> (64 - shift_t
));
3563 tempA
= tempA
<< shift_t
;
3567 env
->active_tc
.HI
[ac
] = tempB
;
3568 env
->active_tc
.LO
[ac
] = tempA
;
3572 void helper_mthlip(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3574 int32_t tempA
, tempB
, pos
;
3577 tempB
= env
->active_tc
.LO
[ac
];
3578 env
->active_tc
.HI
[ac
] = (target_long
)tempB
;
3579 env
->active_tc
.LO
[ac
] = (target_long
)tempA
;
3580 pos
= get_DSPControl_pos(env
);
3585 set_DSPControl_pos(pos
+ 32, env
);
3589 #if defined(TARGET_MIPS64)
3590 void helper_dmthlip(target_ulong rs
, target_ulong ac
, CPUMIPSState
*env
)
3594 uint64_t tempB
, tempA
;
3599 tempB
= env
->active_tc
.LO
[ac_t
];
3601 env
->active_tc
.HI
[ac_t
] = tempB
;
3602 env
->active_tc
.LO
[ac_t
] = tempA
;
3604 pos
= get_DSPControl_pos(env
);
3608 set_DSPControl_pos(pos
, env
);
3613 void cpu_wrdsp(uint32_t rs
, uint32_t mask_num
, CPUMIPSState
*env
)
3617 uint32_t newbits
, overwrite
;
3621 overwrite
= 0xFFFFFFFF;
3622 dsp
= env
->active_tc
.DSPControl
;
3624 for (i
= 0; i
< 6; i
++) {
3625 mask
[i
] = (mask_num
>> i
) & 0x01;
3629 #if defined(TARGET_MIPS64)
3630 overwrite
&= 0xFFFFFF80;
3631 newbits
&= 0xFFFFFF80;
3632 newbits
|= 0x0000007F & rs
;
3634 overwrite
&= 0xFFFFFFC0;
3635 newbits
&= 0xFFFFFFC0;
3636 newbits
|= 0x0000003F & rs
;
3641 overwrite
&= 0xFFFFE07F;
3642 newbits
&= 0xFFFFE07F;
3643 newbits
|= 0x00001F80 & rs
;
3647 overwrite
&= 0xFFFFDFFF;
3648 newbits
&= 0xFFFFDFFF;
3649 newbits
|= 0x00002000 & rs
;
3653 overwrite
&= 0xFF00FFFF;
3654 newbits
&= 0xFF00FFFF;
3655 newbits
|= 0x00FF0000 & rs
;
3659 overwrite
&= 0x00FFFFFF;
3660 newbits
&= 0x00FFFFFF;
3661 #if defined(TARGET_MIPS64)
3662 newbits
|= 0xFF000000 & rs
;
3664 newbits
|= 0x0F000000 & rs
;
3669 overwrite
&= 0xFFFFBFFF;
3670 newbits
&= 0xFFFFBFFF;
3671 newbits
|= 0x00004000 & rs
;
3674 dsp
= dsp
& overwrite
;
3675 dsp
= dsp
| newbits
;
3676 env
->active_tc
.DSPControl
= dsp
;
3679 void helper_wrdsp(target_ulong rs
, target_ulong mask_num
, CPUMIPSState
*env
)
3681 cpu_wrdsp(rs
, mask_num
, env
);
3684 uint32_t cpu_rddsp(uint32_t mask_num
, CPUMIPSState
*env
)
3692 for (i
= 0; i
< 6; i
++) {
3693 mask
[i
] = (mask_num
& ruler
) >> i
;
3698 dsp
= env
->active_tc
.DSPControl
;
3701 #if defined(TARGET_MIPS64)
3709 temp
|= dsp
& 0x1F80;
3713 temp
|= dsp
& 0x2000;
3717 temp
|= dsp
& 0x00FF0000;
3721 #if defined(TARGET_MIPS64)
3722 temp
|= dsp
& 0xFF000000;
3724 temp
|= dsp
& 0x0F000000;
3729 temp
|= dsp
& 0x4000;
3735 target_ulong
helper_rddsp(target_ulong mask_num
, CPUMIPSState
*env
)
3737 return cpu_rddsp(mask_num
, env
);
3750 #undef MIPSDSP_SPLIT32_8
3751 #undef MIPSDSP_SPLIT32_16
3753 #undef MIPSDSP_RETURN32_8
3754 #undef MIPSDSP_RETURN32_16
3756 #ifdef TARGET_MIPS64
3757 #undef MIPSDSP_SPLIT64_16
3758 #undef MIPSDSP_SPLIT64_32
3759 #undef MIPSDSP_RETURN64_16
3760 #undef MIPSDSP_RETURN64_32