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/>.
23 /* As the byte ordering doesn't matter, i.e. all columns are treated
24 identically, these unions can be used directly. */
45 /*** MIPS DSP internal functions begin ***/
46 #define MIPSDSP_ABS(x) (((x) >= 0) ? x : -x)
47 #define MIPSDSP_OVERFLOW_ADD(a, b, c, d) (~(a ^ b) & (a ^ c) & d)
48 #define MIPSDSP_OVERFLOW_SUB(a, b, c, d) ((a ^ b) & (a ^ c) & d)
50 static inline void set_DSPControl_overflow_flag(uint32_t flag
, int position
,
53 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< position
;
56 static inline void set_DSPControl_carryflag(uint32_t flag
, CPUMIPSState
*env
)
58 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 13;
61 static inline uint32_t get_DSPControl_carryflag(CPUMIPSState
*env
)
63 return (env
->active_tc
.DSPControl
>> 13) & 0x01;
66 static inline void set_DSPControl_24(uint32_t flag
, int len
, CPUMIPSState
*env
)
70 filter
= ((0x01 << len
) - 1) << 24;
73 env
->active_tc
.DSPControl
&= filter
;
74 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 24;
77 static inline uint32_t get_DSPControl_24(int len
, CPUMIPSState
*env
)
81 filter
= (0x01 << len
) - 1;
83 return (env
->active_tc
.DSPControl
>> 24) & filter
;
86 static inline void set_DSPControl_pos(uint32_t pos
, CPUMIPSState
*env
)
90 dspc
= env
->active_tc
.DSPControl
;
92 dspc
= dspc
& 0xFFFFFFC0;
95 dspc
= dspc
& 0xFFFFFF80;
98 env
->active_tc
.DSPControl
= dspc
;
101 static inline uint32_t get_DSPControl_pos(CPUMIPSState
*env
)
106 dspc
= env
->active_tc
.DSPControl
;
108 #ifndef TARGET_MIPS64
117 static inline void set_DSPControl_efi(uint32_t flag
, CPUMIPSState
*env
)
119 env
->active_tc
.DSPControl
&= 0xFFFFBFFF;
120 env
->active_tc
.DSPControl
|= (target_ulong
)flag
<< 14;
123 #define DO_MIPS_SAT_ABS(size) \
124 static inline int##size##_t mipsdsp_sat_abs##size(int##size##_t a, \
127 if (a == INT##size##_MIN) { \
128 set_DSPControl_overflow_flag(1, 20, env); \
129 return INT##size##_MAX; \
131 return MIPSDSP_ABS(a); \
137 #undef DO_MIPS_SAT_ABS
140 static inline int16_t mipsdsp_add_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
146 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x8000)) {
147 set_DSPControl_overflow_flag(1, 20, env
);
153 static inline int16_t mipsdsp_sat_add_i16(int16_t a
, int16_t b
,
160 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempS
, 0x8000)) {
166 set_DSPControl_overflow_flag(1, 20, env
);
172 static inline int32_t mipsdsp_sat_add_i32(int32_t a
, int32_t b
,
179 if (MIPSDSP_OVERFLOW_ADD(a
, b
, tempI
, 0x80000000)) {
185 set_DSPControl_overflow_flag(1, 20, env
);
191 static inline uint8_t mipsdsp_add_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
195 temp
= (uint16_t)a
+ (uint16_t)b
;
198 set_DSPControl_overflow_flag(1, 20, env
);
204 static inline uint16_t mipsdsp_add_u16(uint16_t a
, uint16_t b
,
209 temp
= (uint32_t)a
+ (uint32_t)b
;
211 if (temp
& 0x00010000) {
212 set_DSPControl_overflow_flag(1, 20, env
);
215 return temp
& 0xFFFF;
218 static inline uint8_t mipsdsp_sat_add_u8(uint8_t a
, uint8_t b
,
224 temp
= (uint16_t)a
+ (uint16_t)b
;
225 result
= temp
& 0xFF;
229 set_DSPControl_overflow_flag(1, 20, env
);
235 static inline uint16_t mipsdsp_sat_add_u16(uint16_t a
, uint16_t b
,
241 temp
= (uint32_t)a
+ (uint32_t)b
;
242 result
= temp
& 0xFFFF;
244 if (0x00010000 & temp
) {
246 set_DSPControl_overflow_flag(1, 20, env
);
252 static inline int32_t mipsdsp_sat32_acc_q31(int32_t acc
, int32_t a
,
256 int32_t temp32
, temp31
, result
;
259 #ifndef TARGET_MIPS64
260 temp
= ((uint64_t)env
->active_tc
.HI
[acc
] << 32) |
261 (uint64_t)env
->active_tc
.LO
[acc
];
263 temp
= (uint64_t)env
->active_tc
.LO
[acc
];
266 temp_sum
= (int64_t)a
+ temp
;
268 temp32
= (temp_sum
>> 32) & 0x01;
269 temp31
= (temp_sum
>> 31) & 0x01;
270 result
= temp_sum
& 0xFFFFFFFF;
273 This sat function may wrong, because user manual wrote:
274 temp127..0 ← temp + ( (signA) || a31..0
275 if ( temp32 ≠ temp31 ) then
276 if ( temp32 = 0 ) then
277 temp31..0 ← 0x80000000
279 temp31..0 ← 0x7FFFFFFF
281 DSPControlouflag:16+acc ← 1
284 if (temp32
!= temp31
) {
290 set_DSPControl_overflow_flag(1, 16 + acc
, env
);
296 /* a[0] is LO, a[1] is HI. */
297 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret
,
304 ret
[0] = env
->active_tc
.LO
[ac
] + a
[0];
305 ret
[1] = env
->active_tc
.HI
[ac
] + a
[1];
307 if (((uint64_t)ret
[0] < (uint64_t)env
->active_tc
.LO
[ac
]) &&
308 ((uint64_t)ret
[0] < (uint64_t)a
[0])) {
312 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
314 ret
[0] = (0x01ull
<< 63);
317 ret
[0] = (0x01ull
<< 63) - 1;
320 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
324 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret
,
331 ret
[0] = env
->active_tc
.LO
[ac
] - a
[0];
332 ret
[1] = env
->active_tc
.HI
[ac
] - a
[1];
334 if ((uint64_t)ret
[0] > (uint64_t)env
->active_tc
.LO
[ac
]) {
338 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
340 ret
[0] = (0x01ull
<< 63);
343 ret
[0] = (0x01ull
<< 63) - 1;
346 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
350 static inline int32_t mipsdsp_mul_i16_i16(int16_t a
, int16_t b
,
355 temp
= (int32_t)a
* (int32_t)b
;
357 if ((temp
> (int)0x7FFF) || (temp
< (int)0xFFFF8000)) {
358 set_DSPControl_overflow_flag(1, 21, env
);
365 static inline int32_t mipsdsp_mul_u16_u16(int32_t a
, int32_t b
)
370 static inline int32_t mipsdsp_mul_i32_i32(int32_t a
, int32_t b
)
375 static inline int32_t mipsdsp_sat16_mul_i16_i16(int16_t a
, int16_t b
,
380 temp
= (int32_t)a
* (int32_t)b
;
382 if (temp
> (int)0x7FFF) {
384 set_DSPControl_overflow_flag(1, 21, env
);
385 } else if (temp
< (int)0xffff8000) {
387 set_DSPControl_overflow_flag(1, 21, env
);
394 static inline int32_t mipsdsp_mul_q15_q15_overflowflag21(uint16_t a
, uint16_t b
,
399 if ((a
== 0x8000) && (b
== 0x8000)) {
401 set_DSPControl_overflow_flag(1, 21, env
);
403 temp
= ((int32_t)(int16_t)a
* (int32_t)(int16_t)b
) << 1;
410 static inline uint8_t mipsdsp_rshift_u8(uint8_t a
, target_ulong mov
)
415 static inline uint16_t mipsdsp_rshift_u16(uint16_t a
, target_ulong mov
)
420 static inline int8_t mipsdsp_rashift8(int8_t a
, target_ulong mov
)
425 static inline int16_t mipsdsp_rashift16(int16_t a
, target_ulong mov
)
430 static inline int32_t mipsdsp_rashift32(int32_t a
, target_ulong mov
)
435 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a
, int16_t b
)
439 temp
= (int32_t)a
+ (int32_t)b
;
441 return (temp
>> 1) & 0xFFFF;
444 /* round right shift */
445 static inline int16_t mipsdsp_rrshift1_add_q16(int16_t a
, int16_t b
)
449 temp
= (int32_t)a
+ (int32_t)b
;
452 return (temp
>> 1) & 0xFFFF;
455 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a
, int32_t b
)
459 temp
= (int64_t)a
+ (int64_t)b
;
461 return (temp
>> 1) & 0xFFFFFFFF;
464 static inline int32_t mipsdsp_rrshift1_add_q32(int32_t a
, int32_t b
)
468 temp
= (int64_t)a
+ (int64_t)b
;
471 return (temp
>> 1) & 0xFFFFFFFF;
474 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a
, uint8_t b
)
478 temp
= (uint16_t)a
+ (uint16_t)b
;
480 return (temp
>> 1) & 0x00FF;
483 static inline uint8_t mipsdsp_rrshift1_add_u8(uint8_t a
, uint8_t b
)
487 temp
= (uint16_t)a
+ (uint16_t)b
+ 1;
489 return (temp
>> 1) & 0x00FF;
492 static inline uint8_t mipsdsp_rshift1_sub_u8(uint8_t a
, uint8_t b
)
496 temp
= (uint16_t)a
- (uint16_t)b
;
498 return (temp
>> 1) & 0x00FF;
501 static inline uint8_t mipsdsp_rrshift1_sub_u8(uint8_t a
, uint8_t b
)
505 temp
= (uint16_t)a
- (uint16_t)b
+ 1;
507 return (temp
>> 1) & 0x00FF;
510 /* 128 bits long. p[0] is LO, p[1] is HI. */
511 static inline void mipsdsp_rndrashift_short_acc(int64_t *p
,
518 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
519 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
520 p
[0] = (shift
== 0) ? (acc
<< 1) : (acc
>> (shift
- 1));
521 p
[1] = (acc
>> 63) & 0x01;
524 /* 128 bits long. p[0] is LO, p[1] is HI */
525 static inline void mipsdsp_rashift_acc(uint64_t *p
,
530 uint64_t tempB
, tempA
;
532 tempB
= env
->active_tc
.HI
[ac
];
533 tempA
= env
->active_tc
.LO
[ac
];
534 shift
= shift
& 0x1F;
540 p
[0] = (tempB
<< (64 - shift
)) | (tempA
>> shift
);
541 p
[1] = (int64_t)tempB
>> shift
;
545 /* 128 bits long. p[0] is LO, p[1] is HI , p[2] is sign of HI.*/
546 static inline void mipsdsp_rndrashift_acc(uint64_t *p
,
551 int64_t tempB
, tempA
;
553 tempB
= env
->active_tc
.HI
[ac
];
554 tempA
= env
->active_tc
.LO
[ac
];
555 shift
= shift
& 0x3F;
559 p
[1] = (tempB
<< 1) | (tempA
>> 63);
562 p
[0] = (tempB
<< (65 - shift
)) | (tempA
>> (shift
- 1));
563 p
[1] = (int64_t)tempB
>> (shift
- 1);
572 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac
, uint16_t a
, uint16_t b
,
577 if ((a
== 0x8000) && (b
== 0x8000)) {
579 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
581 temp
= ((uint32_t)a
* (uint32_t)b
) << 1;
587 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac
, uint32_t a
, uint32_t b
,
592 if ((a
== 0x80000000) && (b
== 0x80000000)) {
593 temp
= (0x01ull
<< 63) - 1;
594 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
596 temp
= ((uint64_t)a
* (uint64_t)b
) << 1;
602 static inline uint16_t mipsdsp_mul_u8_u8(uint8_t a
, uint8_t b
)
604 return (uint16_t)a
* (uint16_t)b
;
607 static inline uint16_t mipsdsp_mul_u8_u16(uint8_t a
, uint16_t b
,
612 tempI
= (uint32_t)a
* (uint32_t)b
;
613 if (tempI
> 0x0000FFFF) {
615 set_DSPControl_overflow_flag(1, 21, env
);
618 return tempI
& 0x0000FFFF;
621 static inline uint64_t mipsdsp_mul_u32_u32(uint32_t a
, uint32_t b
)
623 return (uint64_t)a
* (uint64_t)b
;
626 static inline int16_t mipsdsp_rndq15_mul_q15_q15(uint16_t a
, uint16_t b
,
631 if ((a
== 0x8000) && (b
== 0x8000)) {
633 set_DSPControl_overflow_flag(1, 21, env
);
636 temp
= temp
+ 0x00008000;
639 return (temp
& 0xFFFF0000) >> 16;
642 static inline int32_t mipsdsp_sat16_mul_q15_q15(uint16_t a
, uint16_t b
,
647 if ((a
== 0x8000) && (b
== 0x8000)) {
649 set_DSPControl_overflow_flag(1, 21, env
);
651 temp
= (int16_t)a
* (int16_t)b
;
655 return (temp
>> 16) & 0x0000FFFF;
658 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a
,
663 temp
= (int32_t)a
+ 0x00008000;
665 if (a
> (int)0x7fff8000) {
667 set_DSPControl_overflow_flag(1, 22, env
);
670 return (temp
>> 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
)
703 sign
= (a
>> 7) & 0x01;
705 discard
= (((0x01 << (8 - s
)) - 1) << s
) |
706 ((a
>> (6 - (s
- 1))) & ((0x01 << s
) - 1));
708 discard
= a
>> (6 - (s
- 1));
711 if (discard
!= 0x00) {
712 set_DSPControl_overflow_flag(1, 22, env
);
718 static inline uint16_t mipsdsp_lshift16(uint16_t a
, uint8_t s
,
727 sign
= (a
>> 15) & 0x01;
729 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
730 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
732 discard
= a
>> (14 - (s
- 1));
735 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
736 set_DSPControl_overflow_flag(1, 22, env
);
743 static inline uint32_t mipsdsp_lshift32(uint32_t a
, uint8_t s
,
751 discard
= (int32_t)a
>> (31 - (s
- 1));
753 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
754 set_DSPControl_overflow_flag(1, 22, env
);
760 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a
, uint8_t s
,
769 sign
= (a
>> 15) & 0x01;
771 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
772 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
774 discard
= a
>> (14 - (s
- 1));
777 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
778 set_DSPControl_overflow_flag(1, 22, env
);
779 return (sign
== 0) ? 0x7FFF : 0x8000;
786 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a
, uint8_t s
,
795 sign
= (a
>> 31) & 0x01;
797 discard
= (((0x01 << (32 - s
)) - 1) << s
) |
798 ((a
>> (30 - (s
- 1))) & ((0x01 << s
) - 1));
800 discard
= a
>> (30 - (s
- 1));
803 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
804 set_DSPControl_overflow_flag(1, 22, env
);
805 return (sign
== 0) ? 0x7FFFFFFF : 0x80000000;
812 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a
, uint8_t s
)
817 temp
= (uint32_t)a
<< 1;
819 temp
= (int32_t)(int8_t)a
>> (s
- 1);
822 return (temp
+ 1) >> 1;
825 static inline uint16_t mipsdsp_rnd16_rashift(uint16_t a
, uint8_t s
)
830 temp
= (uint32_t)a
<< 1;
832 temp
= (int32_t)(int16_t)a
>> (s
- 1);
835 return (temp
+ 1) >> 1;
838 static inline uint32_t mipsdsp_rnd32_rashift(uint32_t a
, uint8_t s
)
843 temp
= (uint64_t)a
<< 1;
845 temp
= (int64_t)(int32_t)a
>> (s
- 1);
849 return (temp
>> 1) & 0xFFFFFFFFull
;
852 static inline uint16_t mipsdsp_sub_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
857 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
858 set_DSPControl_overflow_flag(1, 20, env
);
864 static inline uint16_t mipsdsp_sat16_sub(int16_t a
, int16_t b
,
870 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
876 set_DSPControl_overflow_flag(1, 20, env
);
882 static inline uint32_t mipsdsp_sat32_sub(int32_t a
, int32_t b
,
888 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
894 set_DSPControl_overflow_flag(1, 20, env
);
897 return temp
& 0xFFFFFFFFull
;
900 static inline uint16_t mipsdsp_rshift1_sub_q16(int16_t a
, int16_t b
)
904 temp
= (int32_t)a
- (int32_t)b
;
906 return (temp
>> 1) & 0x0000FFFF;
909 static inline uint16_t mipsdsp_rrshift1_sub_q16(int16_t a
, int16_t b
)
913 temp
= (int32_t)a
- (int32_t)b
;
916 return (temp
>> 1) & 0x0000FFFF;
919 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a
, int32_t b
)
923 temp
= (int64_t)a
- (int64_t)b
;
925 return (temp
>> 1) & 0xFFFFFFFFull
;
928 static inline uint32_t mipsdsp_rrshift1_sub_q32(int32_t a
, int32_t b
)
932 temp
= (int64_t)a
- (int64_t)b
;
935 return (temp
>> 1) & 0xFFFFFFFFull
;
938 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a
, uint16_t b
,
944 temp
= (uint32_t)a
- (uint32_t)b
;
945 temp16
= (temp
>> 16) & 0x01;
947 set_DSPControl_overflow_flag(1, 20, env
);
949 return temp
& 0x0000FFFF;
952 static inline uint16_t mipsdsp_satu16_sub_u16_u16(uint16_t a
, uint16_t b
,
958 temp
= (uint32_t)a
- (uint32_t)b
;
959 temp16
= (temp
>> 16) & 0x01;
963 set_DSPControl_overflow_flag(1, 20, env
);
966 return temp
& 0x0000FFFF;
969 static inline uint8_t mipsdsp_sub_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
974 temp
= (uint16_t)a
- (uint16_t)b
;
975 temp8
= (temp
>> 8) & 0x01;
977 set_DSPControl_overflow_flag(1, 20, env
);
980 return temp
& 0x00FF;
983 static inline uint8_t mipsdsp_satu8_sub(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
988 temp
= (uint16_t)a
- (uint16_t)b
;
989 temp8
= (temp
>> 8) & 0x01;
992 set_DSPControl_overflow_flag(1, 20, env
);
995 return temp
& 0x00FF;
998 static inline uint32_t mipsdsp_sub32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
1003 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
1004 set_DSPControl_overflow_flag(1, 20, env
);
1010 static inline int32_t mipsdsp_add_i32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
1016 if (MIPSDSP_OVERFLOW_ADD(a
, b
, temp
, 0x80000000)) {
1017 set_DSPControl_overflow_flag(1, 20, env
);
1023 static inline int32_t mipsdsp_cmp_eq(int32_t a
, int32_t b
)
1028 static inline int32_t mipsdsp_cmp_le(int32_t a
, int32_t b
)
1033 static inline int32_t mipsdsp_cmp_lt(int32_t a
, int32_t b
)
1038 static inline int32_t mipsdsp_cmpu_eq(uint32_t a
, uint32_t b
)
1043 static inline int32_t mipsdsp_cmpu_le(uint32_t a
, uint32_t b
)
1048 static inline int32_t mipsdsp_cmpu_lt(uint32_t a
, uint32_t b
)
1052 /*** MIPS DSP internal functions end ***/
1054 #define MIPSDSP_LHI 0xFFFFFFFF00000000ull
1055 #define MIPSDSP_LLO 0x00000000FFFFFFFFull
1056 #define MIPSDSP_HI 0xFFFF0000
1057 #define MIPSDSP_LO 0x0000FFFF
1058 #define MIPSDSP_Q3 0xFF000000
1059 #define MIPSDSP_Q2 0x00FF0000
1060 #define MIPSDSP_Q1 0x0000FF00
1061 #define MIPSDSP_Q0 0x000000FF
1063 #define MIPSDSP_SPLIT32_8(num, a, b, c, d) \
1065 a = (num >> 24) & MIPSDSP_Q0; \
1066 b = (num >> 16) & MIPSDSP_Q0; \
1067 c = (num >> 8) & MIPSDSP_Q0; \
1068 d = num & MIPSDSP_Q0; \
1071 #define MIPSDSP_SPLIT32_16(num, a, b) \
1073 a = (num >> 16) & MIPSDSP_LO; \
1074 b = num & MIPSDSP_LO; \
1077 #define MIPSDSP_RETURN32_8(a, b, c, d) ((target_long)(int32_t) \
1078 (((uint32_t)a << 24) | \
1079 (((uint32_t)b << 16) | \
1080 (((uint32_t)c << 8) | \
1081 ((uint32_t)d & 0xFF)))))
1082 #define MIPSDSP_RETURN32_16(a, b) ((target_long)(int32_t) \
1083 (((uint32_t)a << 16) | \
1084 ((uint32_t)b & 0xFFFF)))
1086 #ifdef TARGET_MIPS64
1087 #define MIPSDSP_SPLIT64_16(num, a, b, c, d) \
1089 a = (num >> 48) & MIPSDSP_LO; \
1090 b = (num >> 32) & MIPSDSP_LO; \
1091 c = (num >> 16) & MIPSDSP_LO; \
1092 d = num & MIPSDSP_LO; \
1095 #define MIPSDSP_SPLIT64_32(num, a, b) \
1097 a = (num >> 32) & MIPSDSP_LLO; \
1098 b = num & MIPSDSP_LLO; \
1101 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)a << 48) | \
1102 ((uint64_t)b << 32) | \
1103 ((uint64_t)c << 16) | \
1105 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)a << 32) | (uint64_t)b)
1108 /** DSP Arithmetic Sub-class insns **/
1109 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1110 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1113 unsigned int i, n; \
1115 n = sizeof(DSP32Value) / sizeof(dt.element[0]); \
1118 for (i = 0; i < n; i++) { \
1119 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1122 return (target_long)dt.sw[0]; \
1124 MIPSDSP32_UNOP_ENV(absq_s_ph
, sat_abs16
, sh
)
1125 MIPSDSP32_UNOP_ENV(absq_s_qb
, sat_abs8
, sb
)
1126 MIPSDSP32_UNOP_ENV(absq_s_w
, sat_abs32
, sw
)
1127 #undef MIPSDSP32_UNOP_ENV
1129 #if defined(TARGET_MIPS64)
1130 #define MIPSDSP64_UNOP_ENV(name, func, element) \
1131 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1134 unsigned int i, n; \
1136 n = sizeof(DSP64Value) / sizeof(dt.element[0]); \
1139 for (i = 0; i < n; i++) { \
1140 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1145 MIPSDSP64_UNOP_ENV(absq_s_ob
, sat_abs8
, sb
)
1146 MIPSDSP64_UNOP_ENV(absq_s_qh
, sat_abs16
, sh
)
1147 MIPSDSP64_UNOP_ENV(absq_s_pw
, sat_abs32
, sw
)
1148 #undef MIPSDSP64_UNOP_ENV
1151 #define MIPSDSP32_BINOP(name, func, element) \
1152 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1154 DSP32Value ds, dt; \
1155 unsigned int i, n; \
1157 n = sizeof(DSP32Value) / sizeof(ds.element[0]); \
1161 for (i = 0; i < n; i++) { \
1162 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1165 return (target_long)ds.sw[0]; \
1167 MIPSDSP32_BINOP(addqh_ph
, rshift1_add_q16
, sh
);
1168 MIPSDSP32_BINOP(addqh_r_ph
, rrshift1_add_q16
, sh
);
1169 MIPSDSP32_BINOP(addqh_r_w
, rrshift1_add_q32
, sw
);
1170 MIPSDSP32_BINOP(addqh_w
, rshift1_add_q32
, sw
);
1171 MIPSDSP32_BINOP(adduh_qb
, rshift1_add_u8
, ub
);
1172 MIPSDSP32_BINOP(adduh_r_qb
, rrshift1_add_u8
, ub
);
1173 MIPSDSP32_BINOP(subqh_ph
, rshift1_sub_q16
, sh
);
1174 MIPSDSP32_BINOP(subqh_r_ph
, rrshift1_sub_q16
, sh
);
1175 MIPSDSP32_BINOP(subqh_r_w
, rrshift1_sub_q32
, sw
);
1176 MIPSDSP32_BINOP(subqh_w
, rshift1_sub_q32
, sw
);
1177 #undef MIPSDSP32_BINOP
1179 #define MIPSDSP32_BINOP_ENV(name, func, element) \
1180 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1181 CPUMIPSState *env) \
1183 DSP32Value ds, dt; \
1184 unsigned int i, n; \
1186 n = sizeof(DSP32Value) / sizeof(ds.element[0]); \
1190 for (i = 0 ; i < n ; i++) { \
1191 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1194 return (target_long)ds.sw[0]; \
1196 MIPSDSP32_BINOP_ENV(addq_ph
, add_i16
, sh
)
1197 MIPSDSP32_BINOP_ENV(addq_s_ph
, sat_add_i16
, sh
)
1198 MIPSDSP32_BINOP_ENV(addq_s_w
, sat_add_i32
, sw
);
1199 MIPSDSP32_BINOP_ENV(addu_ph
, add_u16
, sh
)
1200 MIPSDSP32_BINOP_ENV(addu_qb
, add_u8
, ub
);
1201 MIPSDSP32_BINOP_ENV(addu_s_ph
, sat_add_u16
, sh
)
1202 MIPSDSP32_BINOP_ENV(addu_s_qb
, sat_add_u8
, ub
);
1203 MIPSDSP32_BINOP_ENV(subq_ph
, sub_i16
, sh
);
1204 MIPSDSP32_BINOP_ENV(subq_s_ph
, sat16_sub
, sh
);
1205 MIPSDSP32_BINOP_ENV(subq_s_w
, sat32_sub
, sw
);
1206 MIPSDSP32_BINOP_ENV(subu_ph
, sub_u16_u16
, sh
);
1207 MIPSDSP32_BINOP_ENV(subu_qb
, sub_u8
, ub
);
1208 MIPSDSP32_BINOP_ENV(subu_s_ph
, satu16_sub_u16_u16
, sh
);
1209 MIPSDSP32_BINOP_ENV(subu_s_qb
, satu8_sub
, ub
);
1210 #undef MIPSDSP32_BINOP_ENV
1212 #ifdef TARGET_MIPS64
1213 #define MIPSDSP64_BINOP(name, func, element) \
1214 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1216 DSP64Value ds, dt; \
1217 unsigned int i, n; \
1219 n = sizeof(DSP64Value) / sizeof(ds.element[0]); \
1223 for (i = 0 ; i < n ; i++) { \
1224 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1229 MIPSDSP64_BINOP(adduh_ob
, rshift1_add_u8
, ub
);
1230 MIPSDSP64_BINOP(adduh_r_ob
, rrshift1_add_u8
, ub
);
1231 MIPSDSP64_BINOP(subuh_ob
, rshift1_sub_u8
, ub
);
1232 MIPSDSP64_BINOP(subuh_r_ob
, rrshift1_sub_u8
, ub
);
1233 #undef MIPSDSP64_BINOP
1235 #define MIPSDSP64_BINOP_ENV(name, func, element) \
1236 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1237 CPUMIPSState *env) \
1239 DSP64Value ds, dt; \
1240 unsigned int i, n; \
1242 n = sizeof(DSP64Value) / sizeof(ds.element[0]); \
1246 for (i = 0 ; i < n ; i++) { \
1247 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1252 MIPSDSP64_BINOP_ENV(addq_pw
, add_i32
, sw
);
1253 MIPSDSP64_BINOP_ENV(addq_qh
, add_i16
, sh
);
1254 MIPSDSP64_BINOP_ENV(addq_s_pw
, sat_add_i32
, sw
);
1255 MIPSDSP64_BINOP_ENV(addq_s_qh
, sat_add_i16
, sh
);
1256 MIPSDSP64_BINOP_ENV(addu_ob
, add_u8
, uh
);
1257 MIPSDSP64_BINOP_ENV(addu_qh
, add_u16
, uh
);
1258 MIPSDSP64_BINOP_ENV(addu_s_ob
, sat_add_u8
, uh
);
1259 MIPSDSP64_BINOP_ENV(addu_s_qh
, sat_add_u16
, uh
);
1260 MIPSDSP64_BINOP_ENV(subq_pw
, sub32
, sw
);
1261 MIPSDSP64_BINOP_ENV(subq_qh
, sub_i16
, sh
);
1262 MIPSDSP64_BINOP_ENV(subq_s_pw
, sat32_sub
, sw
);
1263 MIPSDSP64_BINOP_ENV(subq_s_qh
, sat16_sub
, sh
);
1264 MIPSDSP64_BINOP_ENV(subu_ob
, sub_u8
, uh
);
1265 MIPSDSP64_BINOP_ENV(subu_qh
, sub_u16_u16
, uh
);
1266 MIPSDSP64_BINOP_ENV(subu_s_ob
, satu8_sub
, uh
);
1267 MIPSDSP64_BINOP_ENV(subu_s_qh
, satu16_sub_u16_u16
, uh
);
1268 #undef MIPSDSP64_BINOP_ENV
1272 #define SUBUH_QB(name, var) \
1273 target_ulong helper_##name##_qb(target_ulong rs, target_ulong rt) \
1275 uint8_t rs3, rs2, rs1, rs0; \
1276 uint8_t rt3, rt2, rt1, rt0; \
1277 uint8_t tempD, tempC, tempB, tempA; \
1279 MIPSDSP_SPLIT32_8(rs, rs3, rs2, rs1, rs0); \
1280 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1282 tempD = ((uint16_t)rs3 - (uint16_t)rt3 + var) >> 1; \
1283 tempC = ((uint16_t)rs2 - (uint16_t)rt2 + var) >> 1; \
1284 tempB = ((uint16_t)rs1 - (uint16_t)rt1 + var) >> 1; \
1285 tempA = ((uint16_t)rs0 - (uint16_t)rt0 + var) >> 1; \
1287 return ((uint32_t)tempD << 24) | ((uint32_t)tempC << 16) | \
1288 ((uint32_t)tempB << 8) | ((uint32_t)tempA); \
1292 SUBUH_QB(subuh_r
, 1);
1296 target_ulong
helper_addsc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1298 uint64_t temp
, tempRs
, tempRt
;
1301 tempRs
= (uint64_t)rs
& MIPSDSP_LLO
;
1302 tempRt
= (uint64_t)rt
& MIPSDSP_LLO
;
1304 temp
= tempRs
+ tempRt
;
1305 flag
= (temp
& 0x0100000000ull
) >> 32;
1306 set_DSPControl_carryflag(flag
, env
);
1308 return (target_long
)(int32_t)(temp
& MIPSDSP_LLO
);
1311 target_ulong
helper_addwc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1314 int32_t temp32
, temp31
;
1317 tempL
= (int64_t)(int32_t)rs
+ (int64_t)(int32_t)rt
+
1318 get_DSPControl_carryflag(env
);
1319 temp31
= (tempL
>> 31) & 0x01;
1320 temp32
= (tempL
>> 32) & 0x01;
1322 if (temp31
!= temp32
) {
1323 set_DSPControl_overflow_flag(1, 20, env
);
1326 rd
= tempL
& MIPSDSP_LLO
;
1328 return (target_long
)(int32_t)rd
;
1331 target_ulong
helper_modsub(target_ulong rs
, target_ulong rt
)
1337 decr
= rt
& MIPSDSP_Q0
;
1338 lastindex
= (rt
>> 8) & MIPSDSP_LO
;
1340 if ((rs
& MIPSDSP_LLO
) == 0x00000000) {
1341 rd
= (target_ulong
)lastindex
;
1349 target_ulong
helper_raddu_w_qb(target_ulong rs
)
1351 target_ulong ret
= 0;
1356 for (i
= 0; i
< 4; i
++) {
1362 #if defined(TARGET_MIPS64)
1363 target_ulong
helper_raddu_l_ob(target_ulong rs
)
1365 target_ulong ret
= 0;
1370 for (i
= 0; i
< 8; i
++) {
1377 #define PRECR_QB_PH(name, a, b)\
1378 target_ulong helper_##name##_qb_ph(target_ulong rs, target_ulong rt) \
1380 uint8_t tempD, tempC, tempB, tempA; \
1382 tempD = (rs >> a) & MIPSDSP_Q0; \
1383 tempC = (rs >> b) & MIPSDSP_Q0; \
1384 tempB = (rt >> a) & MIPSDSP_Q0; \
1385 tempA = (rt >> b) & MIPSDSP_Q0; \
1387 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA); \
1390 PRECR_QB_PH(precr
, 16, 0);
1391 PRECR_QB_PH(precrq
, 24, 8);
1395 target_ulong
helper_precr_sra_ph_w(uint32_t sa
, target_ulong rs
,
1398 uint16_t tempB
, tempA
;
1400 tempB
= ((int32_t)rt
>> sa
) & MIPSDSP_LO
;
1401 tempA
= ((int32_t)rs
>> sa
) & MIPSDSP_LO
;
1403 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1406 target_ulong
helper_precr_sra_r_ph_w(uint32_t sa
,
1407 target_ulong rs
, target_ulong rt
)
1409 uint64_t tempB
, tempA
;
1411 /* If sa = 0, then (sa - 1) = -1 will case shift error, so we need else. */
1413 tempB
= (rt
& MIPSDSP_LO
) << 1;
1414 tempA
= (rs
& MIPSDSP_LO
) << 1;
1416 tempB
= ((int32_t)rt
>> (sa
- 1)) + 1;
1417 tempA
= ((int32_t)rs
>> (sa
- 1)) + 1;
1419 rt
= (((tempB
>> 1) & MIPSDSP_LO
) << 16) | ((tempA
>> 1) & MIPSDSP_LO
);
1421 return (target_long
)(int32_t)rt
;
1424 target_ulong
helper_precrq_ph_w(target_ulong rs
, target_ulong rt
)
1426 uint16_t tempB
, tempA
;
1428 tempB
= (rs
& MIPSDSP_HI
) >> 16;
1429 tempA
= (rt
& MIPSDSP_HI
) >> 16;
1431 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1434 target_ulong
helper_precrq_rs_ph_w(target_ulong rs
, target_ulong rt
,
1437 uint16_t tempB
, tempA
;
1439 tempB
= mipsdsp_trunc16_sat16_round(rs
, env
);
1440 tempA
= mipsdsp_trunc16_sat16_round(rt
, env
);
1442 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1445 #if defined(TARGET_MIPS64)
1446 target_ulong
helper_precr_ob_qh(target_ulong rs
, target_ulong rt
)
1448 uint8_t rs6
, rs4
, rs2
, rs0
;
1449 uint8_t rt6
, rt4
, rt2
, rt0
;
1452 rs6
= (rs
>> 48) & MIPSDSP_Q0
;
1453 rs4
= (rs
>> 32) & MIPSDSP_Q0
;
1454 rs2
= (rs
>> 16) & MIPSDSP_Q0
;
1455 rs0
= rs
& MIPSDSP_Q0
;
1456 rt6
= (rt
>> 48) & MIPSDSP_Q0
;
1457 rt4
= (rt
>> 32) & MIPSDSP_Q0
;
1458 rt2
= (rt
>> 16) & MIPSDSP_Q0
;
1459 rt0
= rt
& MIPSDSP_Q0
;
1461 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1462 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1463 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1464 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1469 #define PRECR_QH_PW(name, var) \
1470 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, target_ulong rt, \
1473 uint16_t rs3, rs2, rs1, rs0; \
1474 uint16_t rt3, rt2, rt1, rt0; \
1475 uint16_t tempD, tempC, tempB, tempA; \
1477 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
1478 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1480 /* When sa = 0, we use rt2, rt0, rs2, rs0; \
1481 * when sa != 0, we use rt3, rt1, rs3, rs1. */ \
1483 tempD = rt2 << var; \
1484 tempC = rt0 << var; \
1485 tempB = rs2 << var; \
1486 tempA = rs0 << var; \
1488 tempD = (((int16_t)rt3 >> sa) + var) >> var; \
1489 tempC = (((int16_t)rt1 >> sa) + var) >> var; \
1490 tempB = (((int16_t)rs3 >> sa) + var) >> var; \
1491 tempA = (((int16_t)rs1 >> sa) + var) >> var; \
1494 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1497 PRECR_QH_PW(sra
, 0);
1498 PRECR_QH_PW(sra_r
, 1);
1502 target_ulong
helper_precrq_ob_qh(target_ulong rs
, target_ulong rt
)
1504 uint8_t rs6
, rs4
, rs2
, rs0
;
1505 uint8_t rt6
, rt4
, rt2
, rt0
;
1508 rs6
= (rs
>> 56) & MIPSDSP_Q0
;
1509 rs4
= (rs
>> 40) & MIPSDSP_Q0
;
1510 rs2
= (rs
>> 24) & MIPSDSP_Q0
;
1511 rs0
= (rs
>> 8) & MIPSDSP_Q0
;
1512 rt6
= (rt
>> 56) & MIPSDSP_Q0
;
1513 rt4
= (rt
>> 40) & MIPSDSP_Q0
;
1514 rt2
= (rt
>> 24) & MIPSDSP_Q0
;
1515 rt0
= (rt
>> 8) & MIPSDSP_Q0
;
1517 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1518 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1519 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1520 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1525 target_ulong
helper_precrq_qh_pw(target_ulong rs
, target_ulong rt
)
1527 uint16_t tempD
, tempC
, tempB
, tempA
;
1529 tempD
= (rs
>> 48) & MIPSDSP_LO
;
1530 tempC
= (rs
>> 16) & MIPSDSP_LO
;
1531 tempB
= (rt
>> 48) & MIPSDSP_LO
;
1532 tempA
= (rt
>> 16) & MIPSDSP_LO
;
1534 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1537 target_ulong
helper_precrq_rs_qh_pw(target_ulong rs
, target_ulong rt
,
1542 uint16_t tempD
, tempC
, tempB
, tempA
;
1544 rs2
= (rs
>> 32) & MIPSDSP_LLO
;
1545 rs0
= rs
& MIPSDSP_LLO
;
1546 rt2
= (rt
>> 32) & MIPSDSP_LLO
;
1547 rt0
= rt
& MIPSDSP_LLO
;
1549 tempD
= mipsdsp_trunc16_sat16_round(rs2
, env
);
1550 tempC
= mipsdsp_trunc16_sat16_round(rs0
, env
);
1551 tempB
= mipsdsp_trunc16_sat16_round(rt2
, env
);
1552 tempA
= mipsdsp_trunc16_sat16_round(rt0
, env
);
1554 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1557 target_ulong
helper_precrq_pw_l(target_ulong rs
, target_ulong rt
)
1559 uint32_t tempB
, tempA
;
1561 tempB
= (rs
>> 32) & MIPSDSP_LLO
;
1562 tempA
= (rt
>> 32) & MIPSDSP_LLO
;
1564 return MIPSDSP_RETURN64_32(tempB
, tempA
);
1568 target_ulong
helper_precrqu_s_qb_ph(target_ulong rs
, target_ulong rt
,
1571 uint8_t tempD
, tempC
, tempB
, tempA
;
1572 uint16_t rsh
, rsl
, rth
, rtl
;
1574 rsh
= (rs
& MIPSDSP_HI
) >> 16;
1575 rsl
= rs
& MIPSDSP_LO
;
1576 rth
= (rt
& MIPSDSP_HI
) >> 16;
1577 rtl
= rt
& MIPSDSP_LO
;
1579 tempD
= mipsdsp_sat8_reduce_precision(rsh
, env
);
1580 tempC
= mipsdsp_sat8_reduce_precision(rsl
, env
);
1581 tempB
= mipsdsp_sat8_reduce_precision(rth
, env
);
1582 tempA
= mipsdsp_sat8_reduce_precision(rtl
, env
);
1584 return MIPSDSP_RETURN32_8(tempD
, tempC
, tempB
, tempA
);
1587 #if defined(TARGET_MIPS64)
1588 target_ulong
helper_precrqu_s_ob_qh(target_ulong rs
, target_ulong rt
,
1592 uint16_t rs3
, rs2
, rs1
, rs0
;
1593 uint16_t rt3
, rt2
, rt1
, rt0
;
1599 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
1600 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
1602 temp
[7] = mipsdsp_sat8_reduce_precision(rs3
, env
);
1603 temp
[6] = mipsdsp_sat8_reduce_precision(rs2
, env
);
1604 temp
[5] = mipsdsp_sat8_reduce_precision(rs1
, env
);
1605 temp
[4] = mipsdsp_sat8_reduce_precision(rs0
, env
);
1606 temp
[3] = mipsdsp_sat8_reduce_precision(rt3
, env
);
1607 temp
[2] = mipsdsp_sat8_reduce_precision(rt2
, env
);
1608 temp
[1] = mipsdsp_sat8_reduce_precision(rt1
, env
);
1609 temp
[0] = mipsdsp_sat8_reduce_precision(rt0
, env
);
1611 for (i
= 0; i
< 8; i
++) {
1612 result
|= (uint64_t)temp
[i
] << (8 * i
);
1618 #define PRECEQ_PW(name, a, b) \
1619 target_ulong helper_preceq_pw_##name(target_ulong rt) \
1621 uint16_t tempB, tempA; \
1622 uint32_t tempBI, tempAI; \
1624 tempB = (rt >> a) & MIPSDSP_LO; \
1625 tempA = (rt >> b) & MIPSDSP_LO; \
1627 tempBI = (uint32_t)tempB << 16; \
1628 tempAI = (uint32_t)tempA << 16; \
1630 return MIPSDSP_RETURN64_32(tempBI, tempAI); \
1633 PRECEQ_PW(qhl
, 48, 32);
1634 PRECEQ_PW(qhr
, 16, 0);
1635 PRECEQ_PW(qhla
, 48, 16);
1636 PRECEQ_PW(qhra
, 32, 0);
1642 #define PRECEQU_PH(name, a, b) \
1643 target_ulong helper_precequ_ph_##name(target_ulong rt) \
1645 uint16_t tempB, tempA; \
1647 tempB = (rt >> a) & MIPSDSP_Q0; \
1648 tempA = (rt >> b) & MIPSDSP_Q0; \
1650 tempB = tempB << 7; \
1651 tempA = tempA << 7; \
1653 return MIPSDSP_RETURN32_16(tempB, tempA); \
1656 PRECEQU_PH(qbl
, 24, 16);
1657 PRECEQU_PH(qbr
, 8, 0);
1658 PRECEQU_PH(qbla
, 24, 8);
1659 PRECEQU_PH(qbra
, 16, 0);
1663 #if defined(TARGET_MIPS64)
1664 #define PRECEQU_QH(name, a, b, c, d) \
1665 target_ulong helper_precequ_qh_##name(target_ulong rt) \
1667 uint16_t tempD, tempC, tempB, tempA; \
1669 tempD = (rt >> a) & MIPSDSP_Q0; \
1670 tempC = (rt >> b) & MIPSDSP_Q0; \
1671 tempB = (rt >> c) & MIPSDSP_Q0; \
1672 tempA = (rt >> d) & MIPSDSP_Q0; \
1674 tempD = tempD << 7; \
1675 tempC = tempC << 7; \
1676 tempB = tempB << 7; \
1677 tempA = tempA << 7; \
1679 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1682 PRECEQU_QH(obl
, 56, 48, 40, 32);
1683 PRECEQU_QH(obr
, 24, 16, 8, 0);
1684 PRECEQU_QH(obla
, 56, 40, 24, 8);
1685 PRECEQU_QH(obra
, 48, 32, 16, 0);
1691 #define PRECEU_PH(name, a, b) \
1692 target_ulong helper_preceu_ph_##name(target_ulong rt) \
1694 uint16_t tempB, tempA; \
1696 tempB = (rt >> a) & MIPSDSP_Q0; \
1697 tempA = (rt >> b) & MIPSDSP_Q0; \
1699 return MIPSDSP_RETURN32_16(tempB, tempA); \
1702 PRECEU_PH(qbl
, 24, 16);
1703 PRECEU_PH(qbr
, 8, 0);
1704 PRECEU_PH(qbla
, 24, 8);
1705 PRECEU_PH(qbra
, 16, 0);
1709 #if defined(TARGET_MIPS64)
1710 #define PRECEU_QH(name, a, b, c, d) \
1711 target_ulong helper_preceu_qh_##name(target_ulong rt) \
1713 uint16_t tempD, tempC, tempB, tempA; \
1715 tempD = (rt >> a) & MIPSDSP_Q0; \
1716 tempC = (rt >> b) & MIPSDSP_Q0; \
1717 tempB = (rt >> c) & MIPSDSP_Q0; \
1718 tempA = (rt >> d) & MIPSDSP_Q0; \
1720 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1723 PRECEU_QH(obl
, 56, 48, 40, 32);
1724 PRECEU_QH(obr
, 24, 16, 8, 0);
1725 PRECEU_QH(obla
, 56, 40, 24, 8);
1726 PRECEU_QH(obra
, 48, 32, 16, 0);
1732 /** DSP GPR-Based Shift Sub-class insns **/
1733 #define SHIFT_QB(name, func) \
1734 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt) \
1736 uint8_t rt3, rt2, rt1, rt0; \
1740 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1742 rt3 = mipsdsp_##func(rt3, sa); \
1743 rt2 = mipsdsp_##func(rt2, sa); \
1744 rt1 = mipsdsp_##func(rt1, sa); \
1745 rt0 = mipsdsp_##func(rt0, sa); \
1747 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1750 #define SHIFT_QB_ENV(name, func) \
1751 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt,\
1752 CPUMIPSState *env) \
1754 uint8_t rt3, rt2, rt1, rt0; \
1758 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1760 rt3 = mipsdsp_##func(rt3, sa, env); \
1761 rt2 = mipsdsp_##func(rt2, sa, env); \
1762 rt1 = mipsdsp_##func(rt1, sa, env); \
1763 rt0 = mipsdsp_##func(rt0, sa, env); \
1765 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1768 SHIFT_QB_ENV(shll
, lshift8
);
1769 SHIFT_QB(shrl
, rshift_u8
);
1771 SHIFT_QB(shra
, rashift8
);
1772 SHIFT_QB(shra_r
, rnd8_rashift
);
1777 #if defined(TARGET_MIPS64)
1778 #define SHIFT_OB(name, func) \
1779 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1788 for (i = 0; i < 8; i++) { \
1789 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1790 rt_t[i] = mipsdsp_##func(rt_t[i], sa); \
1791 temp |= (uint64_t)rt_t[i] << (8 * i); \
1797 #define SHIFT_OB_ENV(name, func) \
1798 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1799 CPUMIPSState *env) \
1808 for (i = 0; i < 8; i++) { \
1809 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1810 rt_t[i] = mipsdsp_##func(rt_t[i], sa, env); \
1811 temp |= (uint64_t)rt_t[i] << (8 * i); \
1817 SHIFT_OB_ENV(shll
, lshift8
);
1818 SHIFT_OB(shrl
, rshift_u8
);
1820 SHIFT_OB(shra
, rashift8
);
1821 SHIFT_OB(shra_r
, rnd8_rashift
);
1828 #define SHIFT_PH(name, func) \
1829 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt, \
1830 CPUMIPSState *env) \
1832 uint16_t rth, rtl; \
1836 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1838 rth = mipsdsp_##func(rth, sa, env); \
1839 rtl = mipsdsp_##func(rtl, sa, env); \
1841 return MIPSDSP_RETURN32_16(rth, rtl); \
1844 SHIFT_PH(shll
, lshift16
);
1845 SHIFT_PH(shll_s
, sat16_lshift
);
1849 #if defined(TARGET_MIPS64)
1850 #define SHIFT_QH(name, func) \
1851 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa) \
1853 uint16_t rt3, rt2, rt1, rt0; \
1857 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1859 rt3 = mipsdsp_##func(rt3, sa); \
1860 rt2 = mipsdsp_##func(rt2, sa); \
1861 rt1 = mipsdsp_##func(rt1, sa); \
1862 rt0 = mipsdsp_##func(rt0, sa); \
1864 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1867 #define SHIFT_QH_ENV(name, func) \
1868 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa, \
1869 CPUMIPSState *env) \
1871 uint16_t rt3, rt2, rt1, rt0; \
1875 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1877 rt3 = mipsdsp_##func(rt3, sa, env); \
1878 rt2 = mipsdsp_##func(rt2, sa, env); \
1879 rt1 = mipsdsp_##func(rt1, sa, env); \
1880 rt0 = mipsdsp_##func(rt0, sa, env); \
1882 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1885 SHIFT_QH_ENV(shll
, lshift16
);
1886 SHIFT_QH_ENV(shll_s
, sat16_lshift
);
1888 SHIFT_QH(shrl
, rshift_u16
);
1889 SHIFT_QH(shra
, rashift16
);
1890 SHIFT_QH(shra_r
, rnd16_rashift
);
1897 #define SHIFT_W(name, func) \
1898 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1903 temp = mipsdsp_##func(rt, sa); \
1905 return (target_long)(int32_t)temp; \
1908 #define SHIFT_W_ENV(name, func) \
1909 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt, \
1910 CPUMIPSState *env) \
1915 temp = mipsdsp_##func(rt, sa, env); \
1917 return (target_long)(int32_t)temp; \
1920 SHIFT_W_ENV(shll_s
, sat32_lshift
);
1921 SHIFT_W(shra_r
, rnd32_rashift
);
1926 #if defined(TARGET_MIPS64)
1927 #define SHIFT_PW(name, func) \
1928 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa) \
1930 uint32_t rt1, rt0; \
1933 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1935 rt1 = mipsdsp_##func(rt1, sa); \
1936 rt0 = mipsdsp_##func(rt0, sa); \
1938 return MIPSDSP_RETURN64_32(rt1, rt0); \
1941 #define SHIFT_PW_ENV(name, func) \
1942 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa, \
1943 CPUMIPSState *env) \
1945 uint32_t rt1, rt0; \
1948 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1950 rt1 = mipsdsp_##func(rt1, sa, env); \
1951 rt0 = mipsdsp_##func(rt0, sa, env); \
1953 return MIPSDSP_RETURN64_32(rt1, rt0); \
1956 SHIFT_PW_ENV(shll
, lshift32
);
1957 SHIFT_PW_ENV(shll_s
, sat32_lshift
);
1959 SHIFT_PW(shra
, rashift32
);
1960 SHIFT_PW(shra_r
, rnd32_rashift
);
1967 #define SHIFT_PH(name, func) \
1968 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1970 uint16_t rth, rtl; \
1974 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1976 rth = mipsdsp_##func(rth, sa); \
1977 rtl = mipsdsp_##func(rtl, sa); \
1979 return MIPSDSP_RETURN32_16(rth, rtl); \
1982 SHIFT_PH(shrl
, rshift_u16
);
1983 SHIFT_PH(shra
, rashift16
);
1984 SHIFT_PH(shra_r
, rnd16_rashift
);
1988 /** DSP Multiply Sub-class insns **/
1989 /* Return value made up by two 16bits value.
1990 * FIXME give the macro a better name.
1992 #define MUL_RETURN32_16_PH(name, func, \
1993 rsmov1, rsmov2, rsfilter, \
1994 rtmov1, rtmov2, rtfilter) \
1995 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1996 CPUMIPSState *env) \
1998 uint16_t rsB, rsA, rtB, rtA; \
2000 rsB = (rs >> rsmov1) & rsfilter; \
2001 rsA = (rs >> rsmov2) & rsfilter; \
2002 rtB = (rt >> rtmov1) & rtfilter; \
2003 rtA = (rt >> rtmov2) & rtfilter; \
2005 rsB = mipsdsp_##func(rsB, rtB, env); \
2006 rsA = mipsdsp_##func(rsA, rtA, env); \
2008 return MIPSDSP_RETURN32_16(rsB, rsA); \
2011 MUL_RETURN32_16_PH(muleu_s_ph_qbl
, mul_u8_u16
, \
2012 24, 16, MIPSDSP_Q0
, \
2014 MUL_RETURN32_16_PH(muleu_s_ph_qbr
, mul_u8_u16
, \
2017 MUL_RETURN32_16_PH(mulq_rs_ph
, rndq15_mul_q15_q15
, \
2018 16, 0, MIPSDSP_LO
, \
2020 MUL_RETURN32_16_PH(mul_ph
, mul_i16_i16
, \
2021 16, 0, MIPSDSP_LO
, \
2023 MUL_RETURN32_16_PH(mul_s_ph
, sat16_mul_i16_i16
, \
2024 16, 0, MIPSDSP_LO
, \
2026 MUL_RETURN32_16_PH(mulq_s_ph
, sat16_mul_q15_q15
, \
2027 16, 0, MIPSDSP_LO
, \
2030 #undef MUL_RETURN32_16_PH
2032 #define MUL_RETURN32_32_ph(name, func, movbits) \
2033 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2034 CPUMIPSState *env) \
2039 rsh = (rs >> movbits) & MIPSDSP_LO; \
2040 rth = (rt >> movbits) & MIPSDSP_LO; \
2041 temp = mipsdsp_##func(rsh, rth, env); \
2043 return (target_long)(int32_t)temp; \
2046 MUL_RETURN32_32_ph(muleq_s_w_phl
, mul_q15_q15_overflowflag21
, 16);
2047 MUL_RETURN32_32_ph(muleq_s_w_phr
, mul_q15_q15_overflowflag21
, 0);
2049 #undef MUL_RETURN32_32_ph
2051 #define MUL_VOID_PH(name, use_ac_env) \
2052 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2053 CPUMIPSState *env) \
2055 int16_t rsh, rsl, rth, rtl; \
2056 int32_t tempB, tempA; \
2057 int64_t acc, dotp; \
2059 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2060 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2062 if (use_ac_env == 1) { \
2063 tempB = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2064 tempA = mipsdsp_mul_q15_q15(ac, rsl, rtl, env); \
2066 tempB = mipsdsp_mul_u16_u16(rsh, rth); \
2067 tempA = mipsdsp_mul_u16_u16(rsl, rtl); \
2070 dotp = (int64_t)tempB - (int64_t)tempA; \
2071 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2072 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2073 dotp = dotp + acc; \
2074 env->active_tc.HI[ac] = (target_long)(int32_t) \
2075 ((dotp & MIPSDSP_LHI) >> 32); \
2076 env->active_tc.LO[ac] = (target_long)(int32_t)(dotp & MIPSDSP_LLO); \
2079 MUL_VOID_PH(mulsaq_s_w_ph
, 1);
2080 MUL_VOID_PH(mulsa_w_ph
, 0);
2084 #if defined(TARGET_MIPS64)
2085 #define MUL_RETURN64_16_QH(name, func, \
2086 rsmov1, rsmov2, rsmov3, rsmov4, rsfilter, \
2087 rtmov1, rtmov2, rtmov3, rtmov4, rtfilter) \
2088 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2089 CPUMIPSState *env) \
2091 uint16_t rs3, rs2, rs1, rs0; \
2092 uint16_t rt3, rt2, rt1, rt0; \
2093 uint16_t tempD, tempC, tempB, tempA; \
2095 rs3 = (rs >> rsmov1) & rsfilter; \
2096 rs2 = (rs >> rsmov2) & rsfilter; \
2097 rs1 = (rs >> rsmov3) & rsfilter; \
2098 rs0 = (rs >> rsmov4) & rsfilter; \
2099 rt3 = (rt >> rtmov1) & rtfilter; \
2100 rt2 = (rt >> rtmov2) & rtfilter; \
2101 rt1 = (rt >> rtmov3) & rtfilter; \
2102 rt0 = (rt >> rtmov4) & rtfilter; \
2104 tempD = mipsdsp_##func(rs3, rt3, env); \
2105 tempC = mipsdsp_##func(rs2, rt2, env); \
2106 tempB = mipsdsp_##func(rs1, rt1, env); \
2107 tempA = mipsdsp_##func(rs0, rt0, env); \
2109 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
2112 MUL_RETURN64_16_QH(muleu_s_qh_obl
, mul_u8_u16
, \
2113 56, 48, 40, 32, MIPSDSP_Q0
, \
2114 48, 32, 16, 0, MIPSDSP_LO
);
2115 MUL_RETURN64_16_QH(muleu_s_qh_obr
, mul_u8_u16
, \
2116 24, 16, 8, 0, MIPSDSP_Q0
, \
2117 48, 32, 16, 0, MIPSDSP_LO
);
2118 MUL_RETURN64_16_QH(mulq_rs_qh
, rndq15_mul_q15_q15
, \
2119 48, 32, 16, 0, MIPSDSP_LO
, \
2120 48, 32, 16, 0, MIPSDSP_LO
);
2122 #undef MUL_RETURN64_16_QH
2124 #define MUL_RETURN64_32_QH(name, \
2127 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2128 CPUMIPSState *env) \
2130 uint16_t rsB, rsA; \
2131 uint16_t rtB, rtA; \
2132 uint32_t tempB, tempA; \
2134 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2135 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2136 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2137 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2139 tempB = mipsdsp_mul_q15_q15(5, rsB, rtB, env); \
2140 tempA = mipsdsp_mul_q15_q15(5, rsA, rtA, env); \
2142 return ((uint64_t)tempB << 32) | (uint64_t)tempA; \
2145 MUL_RETURN64_32_QH(muleq_s_pw_qhl
, 48, 32, 48, 32);
2146 MUL_RETURN64_32_QH(muleq_s_pw_qhr
, 16, 0, 16, 0);
2148 #undef MUL_RETURN64_32_QH
2150 void helper_mulsaq_s_w_qh(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2153 int16_t rs3
, rs2
, rs1
, rs0
;
2154 int16_t rt3
, rt2
, rt1
, rt0
;
2155 int32_t tempD
, tempC
, tempB
, tempA
;
2160 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
2161 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
2163 tempD
= mipsdsp_mul_q15_q15(ac
, rs3
, rt3
, env
);
2164 tempC
= mipsdsp_mul_q15_q15(ac
, rs2
, rt2
, env
);
2165 tempB
= mipsdsp_mul_q15_q15(ac
, rs1
, rt1
, env
);
2166 tempA
= mipsdsp_mul_q15_q15(ac
, rs0
, rt0
, env
);
2168 temp
[0] = ((int32_t)tempD
- (int32_t)tempC
) +
2169 ((int32_t)tempB
- (int32_t)tempA
);
2170 temp
[0] = (int64_t)(temp
[0] << 30) >> 30;
2171 if (((temp
[0] >> 33) & 0x01) == 0) {
2177 acc
[0] = env
->active_tc
.LO
[ac
];
2178 acc
[1] = env
->active_tc
.HI
[ac
];
2180 temp_sum
= acc
[0] + temp
[0];
2181 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2182 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2188 env
->active_tc
.HI
[ac
] = acc
[1];
2189 env
->active_tc
.LO
[ac
] = acc
[0];
2193 #define DP_QB(name, func, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2194 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2195 CPUMIPSState *env) \
2199 uint16_t tempB, tempA; \
2200 uint64_t tempC, dotp; \
2202 rs3 = (rs >> rsmov1) & MIPSDSP_Q0; \
2203 rs2 = (rs >> rsmov2) & MIPSDSP_Q0; \
2204 rt3 = (rt >> rtmov1) & MIPSDSP_Q0; \
2205 rt2 = (rt >> rtmov2) & MIPSDSP_Q0; \
2206 tempB = mipsdsp_##func(rs3, rt3); \
2207 tempA = mipsdsp_##func(rs2, rt2); \
2208 dotp = (int64_t)tempB + (int64_t)tempA; \
2210 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2211 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2214 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2215 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2219 env->active_tc.HI[ac] = (target_long)(int32_t) \
2220 ((tempC & MIPSDSP_LHI) >> 32); \
2221 env->active_tc.LO[ac] = (target_long)(int32_t)(tempC & MIPSDSP_LLO); \
2224 DP_QB(dpau_h_qbl
, mul_u8_u8
, 1, 24, 16, 24, 16);
2225 DP_QB(dpau_h_qbr
, mul_u8_u8
, 1, 8, 0, 8, 0);
2226 DP_QB(dpsu_h_qbl
, mul_u8_u8
, 0, 24, 16, 24, 16);
2227 DP_QB(dpsu_h_qbr
, mul_u8_u8
, 0, 8, 0, 8, 0);
2231 #if defined(TARGET_MIPS64)
2232 #define DP_OB(name, add_sub, \
2233 rsmov1, rsmov2, rsmov3, rsmov4, \
2234 rtmov1, rtmov2, rtmov3, rtmov4) \
2235 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2236 CPUMIPSState *env) \
2238 uint8_t rsD, rsC, rsB, rsA; \
2239 uint8_t rtD, rtC, rtB, rtA; \
2240 uint16_t tempD, tempC, tempB, tempA; \
2243 uint64_t temp_sum; \
2248 rsD = (rs >> rsmov1) & MIPSDSP_Q0; \
2249 rsC = (rs >> rsmov2) & MIPSDSP_Q0; \
2250 rsB = (rs >> rsmov3) & MIPSDSP_Q0; \
2251 rsA = (rs >> rsmov4) & MIPSDSP_Q0; \
2252 rtD = (rt >> rtmov1) & MIPSDSP_Q0; \
2253 rtC = (rt >> rtmov2) & MIPSDSP_Q0; \
2254 rtB = (rt >> rtmov3) & MIPSDSP_Q0; \
2255 rtA = (rt >> rtmov4) & MIPSDSP_Q0; \
2257 tempD = mipsdsp_mul_u8_u8(rsD, rtD); \
2258 tempC = mipsdsp_mul_u8_u8(rsC, rtC); \
2259 tempB = mipsdsp_mul_u8_u8(rsB, rtB); \
2260 tempA = mipsdsp_mul_u8_u8(rsA, rtA); \
2262 temp[0] = (uint64_t)tempD + (uint64_t)tempC + \
2263 (uint64_t)tempB + (uint64_t)tempA; \
2265 acc[0] = env->active_tc.LO[ac]; \
2266 acc[1] = env->active_tc.HI[ac]; \
2269 temp_sum = acc[0] + temp[0]; \
2270 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2271 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2274 temp[0] = temp_sum; \
2275 temp[1] = acc[1] + temp[1]; \
2277 temp_sum = acc[0] - temp[0]; \
2278 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2281 temp[0] = temp_sum; \
2282 temp[1] = acc[1] - temp[1]; \
2285 env->active_tc.HI[ac] = temp[1]; \
2286 env->active_tc.LO[ac] = temp[0]; \
2289 DP_OB(dpau_h_obl
, 1, 56, 48, 40, 32, 56, 48, 40, 32);
2290 DP_OB(dpau_h_obr
, 1, 24, 16, 8, 0, 24, 16, 8, 0);
2291 DP_OB(dpsu_h_obl
, 0, 56, 48, 40, 32, 56, 48, 40, 32);
2292 DP_OB(dpsu_h_obr
, 0, 24, 16, 8, 0, 24, 16, 8, 0);
2297 #define DP_NOFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2298 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2299 CPUMIPSState *env) \
2301 int16_t rsB, rsA, rtB, rtA; \
2302 int32_t tempA, tempB; \
2305 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2306 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2307 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2308 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2310 tempB = (int32_t)rsB * (int32_t)rtB; \
2311 tempA = (int32_t)rsA * (int32_t)rtA; \
2313 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2314 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2317 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2319 acc = acc - ((int64_t)tempB + (int64_t)tempA); \
2322 env->active_tc.HI[ac] = (target_long)(int32_t)((acc & MIPSDSP_LHI) >> 32); \
2323 env->active_tc.LO[ac] = (target_long)(int32_t)(acc & MIPSDSP_LLO); \
2326 DP_NOFUNC_PH(dpa_w_ph
, 1, 16, 0, 16, 0);
2327 DP_NOFUNC_PH(dpax_w_ph
, 1, 16, 0, 0, 16);
2328 DP_NOFUNC_PH(dps_w_ph
, 0, 16, 0, 16, 0);
2329 DP_NOFUNC_PH(dpsx_w_ph
, 0, 16, 0, 0, 16);
2332 #define DP_HASFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2333 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2334 CPUMIPSState *env) \
2336 int16_t rsB, rsA, rtB, rtA; \
2337 int32_t tempB, tempA; \
2338 int64_t acc, dotp; \
2340 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2341 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2342 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2343 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2345 tempB = mipsdsp_mul_q15_q15(ac, rsB, rtB, env); \
2346 tempA = mipsdsp_mul_q15_q15(ac, rsA, rtA, env); \
2348 dotp = (int64_t)tempB + (int64_t)tempA; \
2349 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2350 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2358 env->active_tc.HI[ac] = (target_long)(int32_t) \
2359 ((acc & MIPSDSP_LHI) >> 32); \
2360 env->active_tc.LO[ac] = (target_long)(int32_t) \
2361 (acc & MIPSDSP_LLO); \
2364 DP_HASFUNC_PH(dpaq_s_w_ph
, 1, 16, 0, 16, 0);
2365 DP_HASFUNC_PH(dpaqx_s_w_ph
, 1, 16, 0, 0, 16);
2366 DP_HASFUNC_PH(dpsq_s_w_ph
, 0, 16, 0, 16, 0);
2367 DP_HASFUNC_PH(dpsqx_s_w_ph
, 0, 16, 0, 0, 16);
2369 #undef DP_HASFUNC_PH
2371 #define DP_128OPERATION_PH(name, is_add) \
2372 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2373 CPUMIPSState *env) \
2375 int16_t rsh, rsl, rth, rtl; \
2376 int32_t tempB, tempA, tempC62_31, tempC63; \
2377 int64_t acc, dotp, tempC; \
2379 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2380 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2382 tempB = mipsdsp_mul_q15_q15(ac, rsh, rtl, env); \
2383 tempA = mipsdsp_mul_q15_q15(ac, rsl, rth, env); \
2385 dotp = (int64_t)tempB + (int64_t)tempA; \
2386 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2387 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2389 tempC = acc + dotp; \
2391 tempC = acc - dotp; \
2393 tempC63 = (tempC >> 63) & 0x01; \
2394 tempC62_31 = (tempC >> 31) & 0xFFFFFFFF; \
2396 if ((tempC63 == 0) && (tempC62_31 != 0x00000000)) { \
2397 tempC = 0x7FFFFFFF; \
2398 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2401 if ((tempC63 == 1) && (tempC62_31 != 0xFFFFFFFF)) { \
2402 tempC = (int64_t)(int32_t)0x80000000; \
2403 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2406 env->active_tc.HI[ac] = (target_long)(int32_t) \
2407 ((tempC & MIPSDSP_LHI) >> 32); \
2408 env->active_tc.LO[ac] = (target_long)(int32_t) \
2409 (tempC & MIPSDSP_LLO); \
2412 DP_128OPERATION_PH(dpaqx_sa_w_ph
, 1);
2413 DP_128OPERATION_PH(dpsqx_sa_w_ph
, 0);
2415 #undef DP_128OPERATION_HP
2417 #if defined(TARGET_MIPS64)
2418 #define DP_QH(name, is_add, use_ac_env) \
2419 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2420 CPUMIPSState *env) \
2422 int32_t rs3, rs2, rs1, rs0; \
2423 int32_t rt3, rt2, rt1, rt0; \
2424 int32_t tempD, tempC, tempB, tempA; \
2429 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2430 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2433 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env); \
2434 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env); \
2435 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env); \
2436 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env); \
2438 tempD = mipsdsp_mul_u16_u16(rs3, rt3); \
2439 tempC = mipsdsp_mul_u16_u16(rs2, rt2); \
2440 tempB = mipsdsp_mul_u16_u16(rs1, rt1); \
2441 tempA = mipsdsp_mul_u16_u16(rs0, rt0); \
2444 temp[0] = (int64_t)tempD + (int64_t)tempC + \
2445 (int64_t)tempB + (int64_t)tempA; \
2447 if (temp[0] >= 0) { \
2453 acc[1] = env->active_tc.HI[ac]; \
2454 acc[0] = env->active_tc.LO[ac]; \
2457 temp_sum = acc[0] + temp[0]; \
2458 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2459 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2460 acc[1] = acc[1] + 1; \
2462 temp[0] = temp_sum; \
2463 temp[1] = acc[1] + temp[1]; \
2465 temp_sum = acc[0] - temp[0]; \
2466 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2467 acc[1] = acc[1] - 1; \
2469 temp[0] = temp_sum; \
2470 temp[1] = acc[1] - temp[1]; \
2473 env->active_tc.HI[ac] = temp[1]; \
2474 env->active_tc.LO[ac] = temp[0]; \
2477 DP_QH(dpa_w_qh
, 1, 0);
2478 DP_QH(dpaq_s_w_qh
, 1, 1);
2479 DP_QH(dps_w_qh
, 0, 0);
2480 DP_QH(dpsq_s_w_qh
, 0, 1);
2486 #define DP_L_W(name, is_add) \
2487 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2488 CPUMIPSState *env) \
2491 int64_t dotp, acc; \
2495 dotp = mipsdsp_mul_q31_q31(ac, rs, rt, env); \
2496 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2497 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2499 temp = acc + dotp; \
2500 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2501 temp, (0x01ull << 63)); \
2503 temp = acc - dotp; \
2504 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2505 temp, (0x01ull << 63)); \
2509 temp63 = (temp >> 63) & 0x01; \
2510 if (temp63 == 1) { \
2511 temp = (0x01ull << 63) - 1; \
2513 temp = 0x01ull << 63; \
2516 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2519 env->active_tc.HI[ac] = (target_long)(int32_t) \
2520 ((temp & MIPSDSP_LHI) >> 32); \
2521 env->active_tc.LO[ac] = (target_long)(int32_t) \
2522 (temp & MIPSDSP_LLO); \
2525 DP_L_W(dpaq_sa_l_w
, 1);
2526 DP_L_W(dpsq_sa_l_w
, 0);
2530 #if defined(TARGET_MIPS64)
2531 #define DP_L_PW(name, func) \
2532 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2533 CPUMIPSState *env) \
2537 int64_t tempB[2], tempA[2]; \
2545 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2546 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2548 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env); \
2549 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env); \
2551 if (tempB[0] >= 0) { \
2557 if (tempA[0] >= 0) { \
2563 temp_sum = tempB[0] + tempA[0]; \
2564 if (((uint64_t)temp_sum < (uint64_t)tempB[0]) && \
2565 ((uint64_t)temp_sum < (uint64_t)tempA[0])) { \
2568 temp[0] = temp_sum; \
2569 temp[1] += tempB[1] + tempA[1]; \
2571 mipsdsp_##func(acc, ac, temp, env); \
2573 env->active_tc.HI[ac] = acc[1]; \
2574 env->active_tc.LO[ac] = acc[0]; \
2577 DP_L_PW(dpaq_sa_l_pw
, sat64_acc_add_q63
);
2578 DP_L_PW(dpsq_sa_l_pw
, sat64_acc_sub_q63
);
2582 void helper_mulsaq_s_l_pw(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2587 int64_t tempB
[2], tempA
[2];
2592 rs1
= (rs
>> 32) & MIPSDSP_LLO
;
2593 rs0
= rs
& MIPSDSP_LLO
;
2594 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
2595 rt0
= rt
& MIPSDSP_LLO
;
2597 tempB
[0] = mipsdsp_mul_q31_q31(ac
, rs1
, rt1
, env
);
2598 tempA
[0] = mipsdsp_mul_q31_q31(ac
, rs0
, rt0
, env
);
2600 if (tempB
[0] >= 0) {
2606 if (tempA
[0] >= 0) {
2612 acc
[0] = env
->active_tc
.LO
[ac
];
2613 acc
[1] = env
->active_tc
.HI
[ac
];
2615 temp_sum
= tempB
[0] - tempA
[0];
2616 if ((uint64_t)temp_sum
> (uint64_t)tempB
[0]) {
2620 temp
[1] = tempB
[1] - tempA
[1];
2622 if ((temp
[1] & 0x01) == 0) {
2628 temp_sum
= acc
[0] + temp
[0];
2629 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2630 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2636 env
->active_tc
.HI
[ac
] = acc
[1];
2637 env
->active_tc
.LO
[ac
] = acc
[0];
2641 #define MAQ_S_W(name, mov) \
2642 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2643 CPUMIPSState *env) \
2647 int64_t tempL, acc; \
2649 rsh = (rs >> mov) & MIPSDSP_LO; \
2650 rth = (rt >> mov) & MIPSDSP_LO; \
2651 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2652 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2653 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2654 tempL = (int64_t)tempA + acc; \
2655 env->active_tc.HI[ac] = (target_long)(int32_t) \
2656 ((tempL & MIPSDSP_LHI) >> 32); \
2657 env->active_tc.LO[ac] = (target_long)(int32_t) \
2658 (tempL & MIPSDSP_LLO); \
2661 MAQ_S_W(maq_s_w_phl
, 16);
2662 MAQ_S_W(maq_s_w_phr
, 0);
2666 #define MAQ_SA_W(name, mov) \
2667 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2668 CPUMIPSState *env) \
2673 rsh = (rs >> mov) & MIPSDSP_LO; \
2674 rth = (rt >> mov) & MIPSDSP_LO; \
2675 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2676 tempA = mipsdsp_sat32_acc_q31(ac, tempA, env); \
2678 env->active_tc.HI[ac] = (target_long)(int32_t)(((int64_t)tempA & \
2679 MIPSDSP_LHI) >> 32); \
2680 env->active_tc.LO[ac] = (target_long)(int32_t)((int64_t)tempA & \
2684 MAQ_SA_W(maq_sa_w_phl
, 16);
2685 MAQ_SA_W(maq_sa_w_phr
, 0);
2689 #define MULQ_W(name, addvar) \
2690 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2691 CPUMIPSState *env) \
2693 int32_t rs_t, rt_t; \
2697 rs_t = rs & MIPSDSP_LLO; \
2698 rt_t = rt & MIPSDSP_LLO; \
2700 if ((rs_t == 0x80000000) && (rt_t == 0x80000000)) { \
2701 tempL = 0x7FFFFFFF00000000ull; \
2702 set_DSPControl_overflow_flag(1, 21, env); \
2704 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2707 tempI = (tempL & MIPSDSP_LHI) >> 32; \
2709 return (target_long)(int32_t)tempI; \
2712 MULQ_W(mulq_s_w
, 0);
2713 MULQ_W(mulq_rs_w
, 0x80000000ull
);
2717 #if defined(TARGET_MIPS64)
2719 #define MAQ_S_W_QH(name, mov) \
2720 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2721 CPUMIPSState *env) \
2723 int16_t rs_t, rt_t; \
2732 rs_t = (rs >> mov) & MIPSDSP_LO; \
2733 rt_t = (rt >> mov) & MIPSDSP_LO; \
2734 temp_mul = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2736 temp[0] = (int64_t)temp_mul; \
2737 if (temp[0] >= 0) { \
2743 acc[0] = env->active_tc.LO[ac]; \
2744 acc[1] = env->active_tc.HI[ac]; \
2746 temp_sum = acc[0] + temp[0]; \
2747 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2748 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2751 acc[0] = temp_sum; \
2752 acc[1] += temp[1]; \
2754 env->active_tc.HI[ac] = acc[1]; \
2755 env->active_tc.LO[ac] = acc[0]; \
2758 MAQ_S_W_QH(maq_s_w_qhll
, 48);
2759 MAQ_S_W_QH(maq_s_w_qhlr
, 32);
2760 MAQ_S_W_QH(maq_s_w_qhrl
, 16);
2761 MAQ_S_W_QH(maq_s_w_qhrr
, 0);
2765 #define MAQ_SA_W(name, mov) \
2766 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2767 CPUMIPSState *env) \
2769 int16_t rs_t, rt_t; \
2773 rs_t = (rs >> mov) & MIPSDSP_LO; \
2774 rt_t = (rt >> mov) & MIPSDSP_LO; \
2775 temp = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2776 temp = mipsdsp_sat32_acc_q31(ac, temp, env); \
2778 acc[0] = (int64_t)(int32_t)temp; \
2779 if (acc[0] >= 0) { \
2785 env->active_tc.HI[ac] = acc[1]; \
2786 env->active_tc.LO[ac] = acc[0]; \
2789 MAQ_SA_W(maq_sa_w_qhll
, 48);
2790 MAQ_SA_W(maq_sa_w_qhlr
, 32);
2791 MAQ_SA_W(maq_sa_w_qhrl
, 16);
2792 MAQ_SA_W(maq_sa_w_qhrr
, 0);
2796 #define MAQ_S_L_PW(name, mov) \
2797 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2798 CPUMIPSState *env) \
2800 int32_t rs_t, rt_t; \
2808 rs_t = (rs >> mov) & MIPSDSP_LLO; \
2809 rt_t = (rt >> mov) & MIPSDSP_LLO; \
2811 temp[0] = mipsdsp_mul_q31_q31(ac, rs_t, rt_t, env); \
2812 if (temp[0] >= 0) { \
2818 acc[0] = env->active_tc.LO[ac]; \
2819 acc[1] = env->active_tc.HI[ac]; \
2821 temp_sum = acc[0] + temp[0]; \
2822 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2823 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2826 acc[0] = temp_sum; \
2827 acc[1] += temp[1]; \
2829 env->active_tc.HI[ac] = acc[1]; \
2830 env->active_tc.LO[ac] = acc[0]; \
2833 MAQ_S_L_PW(maq_s_l_pwl
, 32);
2834 MAQ_S_L_PW(maq_s_l_pwr
, 0);
2838 #define DM_OPERATE(name, func, is_add, sigext) \
2839 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2840 CPUMIPSState *env) \
2844 int64_t tempBL[2], tempAL[2]; \
2852 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2853 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2856 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2857 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2859 if (tempBL[0] >= 0) { \
2862 tempBL[1] = ~0ull; \
2865 if (tempAL[0] >= 0) { \
2868 tempAL[1] = ~0ull; \
2871 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2872 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2877 acc[1] = env->active_tc.HI[ac]; \
2878 acc[0] = env->active_tc.LO[ac]; \
2880 temp_sum = tempBL[0] + tempAL[0]; \
2881 if (((uint64_t)temp_sum < (uint64_t)tempBL[0]) && \
2882 ((uint64_t)temp_sum < (uint64_t)tempAL[0])) { \
2885 temp[0] = temp_sum; \
2886 temp[1] += tempBL[1] + tempAL[1]; \
2889 temp_sum = acc[0] + temp[0]; \
2890 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2891 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2894 temp[0] = temp_sum; \
2895 temp[1] = acc[1] + temp[1]; \
2897 temp_sum = acc[0] - temp[0]; \
2898 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2901 temp[0] = temp_sum; \
2902 temp[1] = acc[1] - temp[1]; \
2905 env->active_tc.HI[ac] = temp[1]; \
2906 env->active_tc.LO[ac] = temp[0]; \
2909 DM_OPERATE(dmadd
, mul_i32_i32
, 1, 1);
2910 DM_OPERATE(dmaddu
, mul_u32_u32
, 1, 0);
2911 DM_OPERATE(dmsub
, mul_i32_i32
, 0, 1);
2912 DM_OPERATE(dmsubu
, mul_u32_u32
, 0, 0);
2916 /** DSP Bit/Manipulation Sub-class insns **/
2917 target_ulong
helper_bitrev(target_ulong rt
)
2923 temp
= rt
& MIPSDSP_LO
;
2925 for (i
= 0; i
< 16; i
++) {
2926 rd
= (rd
<< 1) | (temp
& 1);
2930 return (target_ulong
)rd
;
2933 #define BIT_INSV(name, posfilter, sizefilter, ret_type) \
2934 target_ulong helper_##name(CPUMIPSState *env, target_ulong rs, \
2937 uint32_t pos, size, msb, lsb; \
2938 target_ulong filter; \
2939 target_ulong temp, temprs, temprt; \
2940 target_ulong dspc; \
2942 dspc = env->active_tc.DSPControl; \
2944 pos = dspc & posfilter; \
2945 size = (dspc >> 7) & sizefilter; \
2947 msb = pos + size - 1; \
2950 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2954 filter = ((int32_t)0x01 << size) - 1; \
2955 filter = filter << pos; \
2956 temprs = (rs << pos) & filter; \
2957 temprt = rt & ~filter; \
2958 temp = temprs | temprt; \
2960 return (target_long)(ret_type)temp; \
2963 BIT_INSV(insv
, 0x1F, 0x1F, int32_t);
2964 #ifdef TARGET_MIPS64
2965 BIT_INSV(dinsv
, 0x7F, 0x3F, target_long
);
2971 /** DSP Compare-Pick Sub-class insns **/
2972 #define CMP_HAS_RET(name, func, split_num, filter, bit_size) \
2973 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
2975 uint32_t rs_t, rt_t; \
2977 uint32_t temp = 0; \
2980 for (i = 0; i < split_num; i++) { \
2981 rs_t = (rs >> (bit_size * i)) & filter; \
2982 rt_t = (rt >> (bit_size * i)) & filter; \
2983 cc = mipsdsp_##func(rs_t, rt_t); \
2987 return (target_ulong)temp; \
2990 CMP_HAS_RET(cmpgu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2991 CMP_HAS_RET(cmpgu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2992 CMP_HAS_RET(cmpgu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
2994 #ifdef TARGET_MIPS64
2995 CMP_HAS_RET(cmpgu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
2996 CMP_HAS_RET(cmpgu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
2997 CMP_HAS_RET(cmpgu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
3003 #define CMP_NO_RET(name, func, split_num, filter, bit_size) \
3004 void helper_##name(target_ulong rs, target_ulong rt, \
3005 CPUMIPSState *env) \
3007 int##bit_size##_t rs_t, rt_t; \
3008 int##bit_size##_t flag = 0; \
3009 int##bit_size##_t cc; \
3012 for (i = 0; i < split_num; i++) { \
3013 rs_t = (rs >> (bit_size * i)) & filter; \
3014 rt_t = (rt >> (bit_size * i)) & filter; \
3016 cc = mipsdsp_##func((int32_t)rs_t, (int32_t)rt_t); \
3020 set_DSPControl_24(flag, split_num, env); \
3023 CMP_NO_RET(cmpu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
3024 CMP_NO_RET(cmpu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
3025 CMP_NO_RET(cmpu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
3027 CMP_NO_RET(cmp_eq_ph
, cmp_eq
, 2, MIPSDSP_LO
, 16);
3028 CMP_NO_RET(cmp_lt_ph
, cmp_lt
, 2, MIPSDSP_LO
, 16);
3029 CMP_NO_RET(cmp_le_ph
, cmp_le
, 2, MIPSDSP_LO
, 16);
3031 #ifdef TARGET_MIPS64
3032 CMP_NO_RET(cmpu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
3033 CMP_NO_RET(cmpu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
3034 CMP_NO_RET(cmpu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
3036 CMP_NO_RET(cmp_eq_qh
, cmp_eq
, 4, MIPSDSP_LO
, 16);
3037 CMP_NO_RET(cmp_lt_qh
, cmp_lt
, 4, MIPSDSP_LO
, 16);
3038 CMP_NO_RET(cmp_le_qh
, cmp_le
, 4, MIPSDSP_LO
, 16);
3040 CMP_NO_RET(cmp_eq_pw
, cmp_eq
, 2, MIPSDSP_LLO
, 32);
3041 CMP_NO_RET(cmp_lt_pw
, cmp_lt
, 2, MIPSDSP_LLO
, 32);
3042 CMP_NO_RET(cmp_le_pw
, cmp_le
, 2, MIPSDSP_LLO
, 32);
3046 #if defined(TARGET_MIPS64)
3048 #define CMPGDU_OB(name) \
3049 target_ulong helper_cmpgdu_##name##_ob(target_ulong rs, target_ulong rt, \
3050 CPUMIPSState *env) \
3053 uint8_t rs_t, rt_t; \
3058 for (i = 0; i < 8; i++) { \
3059 rs_t = (rs >> (8 * i)) & MIPSDSP_Q0; \
3060 rt_t = (rt >> (8 * i)) & MIPSDSP_Q0; \
3062 if (mipsdsp_cmpu_##name(rs_t, rt_t)) { \
3063 cond |= 0x01 << i; \
3067 set_DSPControl_24(cond, 8, env); \
3069 return (uint64_t)cond; \
3078 #define PICK_INSN(name, split_num, filter, bit_size, ret32bit) \
3079 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
3080 CPUMIPSState *env) \
3082 uint32_t rs_t, rt_t; \
3086 target_ulong result = 0; \
3088 dsp = env->active_tc.DSPControl; \
3089 for (i = 0; i < split_num; i++) { \
3090 rs_t = (rs >> (bit_size * i)) & filter; \
3091 rt_t = (rt >> (bit_size * i)) & filter; \
3092 cc = (dsp >> (24 + i)) & 0x01; \
3093 cc = cc == 1 ? rs_t : rt_t; \
3095 result |= (target_ulong)cc << (bit_size * i); \
3099 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3105 PICK_INSN(pick_qb
, 4, MIPSDSP_Q0
, 8, 1);
3106 PICK_INSN(pick_ph
, 2, MIPSDSP_LO
, 16, 1);
3108 #ifdef TARGET_MIPS64
3109 PICK_INSN(pick_ob
, 8, MIPSDSP_Q0
, 8, 0);
3110 PICK_INSN(pick_qh
, 4, MIPSDSP_LO
, 16, 0);
3111 PICK_INSN(pick_pw
, 2, MIPSDSP_LLO
, 32, 0);
3115 target_ulong
helper_packrl_ph(target_ulong rs
, target_ulong rt
)
3119 rsl
= rs
& MIPSDSP_LO
;
3120 rth
= (rt
& MIPSDSP_HI
) >> 16;
3122 return (target_long
)(int32_t)((rsl
<< 16) | rth
);
3125 #if defined(TARGET_MIPS64)
3126 target_ulong
helper_packrl_pw(target_ulong rs
, target_ulong rt
)
3130 rs0
= rs
& MIPSDSP_LLO
;
3131 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
3133 return ((uint64_t)rs0
<< 32) | (uint64_t)rt1
;
3137 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3138 target_ulong
helper_extr_w(target_ulong ac
, target_ulong shift
,
3144 shift
= shift
& 0x1F;
3146 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3147 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3148 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3149 set_DSPControl_overflow_flag(1, 23, env
);
3152 tempI
= (tempDL
[0] >> 1) & MIPSDSP_LLO
;
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
)tempI
;
3167 target_ulong
helper_extr_r_w(target_ulong ac
, target_ulong shift
,
3172 shift
= shift
& 0x1F;
3174 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3175 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3176 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3177 set_DSPControl_overflow_flag(1, 23, env
);
3181 if (tempDL
[0] == 0) {
3185 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3186 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3187 set_DSPControl_overflow_flag(1, 23, env
);
3190 return (target_long
)(int32_t)(tempDL
[0] >> 1);
3193 target_ulong
helper_extr_rs_w(target_ulong ac
, target_ulong shift
,
3196 int32_t tempI
, temp64
;
3199 shift
= shift
& 0x1F;
3201 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3202 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3203 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3204 set_DSPControl_overflow_flag(1, 23, env
);
3207 if (tempDL
[0] == 0) {
3210 tempI
= tempDL
[0] >> 1;
3212 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3213 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3214 temp64
= tempDL
[1] & 0x01;
3220 set_DSPControl_overflow_flag(1, 23, env
);
3223 return (target_long
)tempI
;
3226 #if defined(TARGET_MIPS64)
3227 target_ulong
helper_dextr_w(target_ulong ac
, target_ulong shift
,
3232 shift
= shift
& 0x3F;
3234 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3236 return (int64_t)(int32_t)(temp
[0] >> 1);
3239 target_ulong
helper_dextr_r_w(target_ulong ac
, target_ulong shift
,
3245 shift
= shift
& 0x3F;
3246 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3256 temp128
= temp
[2] & 0x01;
3258 if ((temp128
!= 0 || temp
[1] != 0) &&
3259 (temp128
!= 1 || temp
[1] != ~0ull)) {
3260 set_DSPControl_overflow_flag(1, 23, env
);
3263 return (int64_t)(int32_t)(temp
[0] >> 1);
3266 target_ulong
helper_dextr_rs_w(target_ulong ac
, target_ulong shift
,
3272 shift
= shift
& 0x3F;
3273 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3283 temp128
= temp
[2] & 0x01;
3285 if ((temp128
!= 0 || temp
[1] != 0) &&
3286 (temp128
!= 1 || temp
[1] != ~0ull)) {
3288 temp
[0] = 0x0FFFFFFFF;
3290 temp
[0] = 0x0100000000ULL
;
3292 set_DSPControl_overflow_flag(1, 23, env
);
3295 return (int64_t)(int32_t)(temp
[0] >> 1);
3298 target_ulong
helper_dextr_l(target_ulong ac
, target_ulong shift
,
3302 target_ulong result
;
3304 shift
= shift
& 0x3F;
3306 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3307 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3312 target_ulong
helper_dextr_r_l(target_ulong ac
, target_ulong shift
,
3317 target_ulong result
;
3319 shift
= shift
& 0x3F;
3320 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3330 temp128
= temp
[2] & 0x01;
3332 if ((temp128
!= 0 || temp
[1] != 0) &&
3333 (temp128
!= 1 || temp
[1] != ~0ull)) {
3334 set_DSPControl_overflow_flag(1, 23, env
);
3337 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3342 target_ulong
helper_dextr_rs_l(target_ulong ac
, target_ulong shift
,
3347 target_ulong result
;
3349 shift
= shift
& 0x3F;
3350 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3360 temp128
= temp
[2] & 0x01;
3362 if ((temp128
!= 0 || temp
[1] != 0) &&
3363 (temp128
!= 1 || temp
[1] != ~0ull)) {
3365 temp
[1] &= ~0x00ull
- 1;
3366 temp
[0] |= ~0x00ull
- 1;
3371 set_DSPControl_overflow_flag(1, 23, env
);
3373 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3379 target_ulong
helper_extr_s_h(target_ulong ac
, target_ulong shift
,
3384 shift
= shift
& 0x1F;
3386 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
3387 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
3389 temp
= acc
>> shift
;
3391 if (temp
> (int64_t)0x7FFF) {
3393 set_DSPControl_overflow_flag(1, 23, env
);
3394 } else if (temp
< (int64_t)0xFFFFFFFFFFFF8000ULL
) {
3396 set_DSPControl_overflow_flag(1, 23, env
);
3399 return (target_long
)(int32_t)(temp
& 0xFFFFFFFF);
3403 #if defined(TARGET_MIPS64)
3404 target_ulong
helper_dextr_s_h(target_ulong ac
, target_ulong shift
,
3410 shift
= shift
& 0x1F;
3412 mipsdsp_rashift_acc((uint64_t *)temp
, ac
, shift
, env
);
3414 temp127
= (temp
[1] >> 63) & 0x01;
3416 if ((temp127
== 0) && (temp
[1] > 0 || temp
[0] > 32767)) {
3417 temp
[0] &= 0xFFFF0000;
3418 temp
[0] |= 0x00007FFF;
3419 set_DSPControl_overflow_flag(1, 23, env
);
3420 } else if ((temp127
== 1) &&
3421 (temp
[1] < 0xFFFFFFFFFFFFFFFFll
3422 || temp
[0] < 0xFFFFFFFFFFFF1000ll
)) {
3423 temp
[0] &= 0xFFFF0000;
3424 temp
[0] |= 0x00008000;
3425 set_DSPControl_overflow_flag(1, 23, env
);
3428 return (int64_t)(int16_t)(temp
[0] & MIPSDSP_LO
);
3433 target_ulong
helper_extp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3443 start_pos
= get_DSPControl_pos(env
);
3444 sub
= start_pos
- (size
+ 1);
3446 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3447 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3448 temp
= (acc
>> (start_pos
- size
)) &
3449 (((uint32_t)0x01 << (size
+ 1)) - 1);
3450 set_DSPControl_efi(0, env
);
3452 set_DSPControl_efi(1, env
);
3455 return (target_ulong
)temp
;
3458 target_ulong
helper_extpdp(target_ulong ac
, target_ulong size
,
3468 start_pos
= get_DSPControl_pos(env
);
3469 sub
= start_pos
- (size
+ 1);
3471 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3472 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3473 temp
= (acc
>> (start_pos
- size
)) &
3474 (((uint32_t)0x01 << (size
+ 1)) - 1);
3476 set_DSPControl_pos(start_pos
- (size
+ 1), env
);
3477 set_DSPControl_efi(0, env
);
3479 set_DSPControl_efi(1, env
);
3482 return (target_ulong
)temp
;
3486 #if defined(TARGET_MIPS64)
3487 target_ulong
helper_dextp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3492 uint64_t tempB
, tempA
;
3498 start_pos
= get_DSPControl_pos(env
);
3499 len
= start_pos
- size
;
3500 tempB
= env
->active_tc
.HI
[ac
];
3501 tempA
= env
->active_tc
.LO
[ac
];
3503 sub
= start_pos
- (size
+ 1);
3506 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3507 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3508 set_DSPControl_efi(0, env
);
3510 set_DSPControl_efi(1, env
);
3516 target_ulong
helper_dextpdp(target_ulong ac
, target_ulong size
,
3522 uint64_t tempB
, tempA
;
3527 start_pos
= get_DSPControl_pos(env
);
3528 len
= start_pos
- size
;
3529 tempB
= env
->active_tc
.HI
[ac
];
3530 tempA
= env
->active_tc
.LO
[ac
];
3532 sub
= start_pos
- (size
+ 1);
3535 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3536 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3537 set_DSPControl_pos(sub
, env
);
3538 set_DSPControl_efi(0, env
);
3540 set_DSPControl_efi(1, env
);
3548 void helper_shilo(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3554 rs5_0
= (int8_t)(rs5_0
<< 2) >> 2;
3556 if (unlikely(rs5_0
== 0)) {
3560 acc
= (((uint64_t)env
->active_tc
.HI
[ac
] << 32) & MIPSDSP_LHI
) |
3561 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3564 temp
= acc
>> rs5_0
;
3566 temp
= acc
<< -rs5_0
;
3569 env
->active_tc
.HI
[ac
] = (target_ulong
)(int32_t)((temp
& MIPSDSP_LHI
) >> 32);
3570 env
->active_tc
.LO
[ac
] = (target_ulong
)(int32_t)(temp
& MIPSDSP_LLO
);
3573 #if defined(TARGET_MIPS64)
3574 void helper_dshilo(target_ulong shift
, target_ulong ac
, CPUMIPSState
*env
)
3577 uint64_t tempB
, tempA
;
3579 shift_t
= (int8_t)(shift
<< 1) >> 1;
3581 tempB
= env
->active_tc
.HI
[ac
];
3582 tempA
= env
->active_tc
.LO
[ac
];
3586 tempA
= (tempB
<< (64 - shift_t
)) | (tempA
>> shift_t
);
3587 tempB
= tempB
>> shift_t
;
3590 tempB
= (tempB
<< shift_t
) | (tempA
>> (64 - shift_t
));
3591 tempA
= tempA
<< shift_t
;
3595 env
->active_tc
.HI
[ac
] = tempB
;
3596 env
->active_tc
.LO
[ac
] = tempA
;
3600 void helper_mthlip(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3602 int32_t tempA
, tempB
, pos
;
3605 tempB
= env
->active_tc
.LO
[ac
];
3606 env
->active_tc
.HI
[ac
] = (target_long
)tempB
;
3607 env
->active_tc
.LO
[ac
] = (target_long
)tempA
;
3608 pos
= get_DSPControl_pos(env
);
3613 set_DSPControl_pos(pos
+ 32, env
);
3617 #if defined(TARGET_MIPS64)
3618 void helper_dmthlip(target_ulong rs
, target_ulong ac
, CPUMIPSState
*env
)
3622 uint64_t tempB
, tempA
;
3627 tempB
= env
->active_tc
.LO
[ac_t
];
3629 env
->active_tc
.HI
[ac_t
] = tempB
;
3630 env
->active_tc
.LO
[ac_t
] = tempA
;
3632 pos
= get_DSPControl_pos(env
);
3636 set_DSPControl_pos(pos
, env
);
3641 void cpu_wrdsp(uint32_t rs
, uint32_t mask_num
, CPUMIPSState
*env
)
3645 uint32_t newbits
, overwrite
;
3649 overwrite
= 0xFFFFFFFF;
3650 dsp
= env
->active_tc
.DSPControl
;
3652 for (i
= 0; i
< 6; i
++) {
3653 mask
[i
] = (mask_num
>> i
) & 0x01;
3657 #if defined(TARGET_MIPS64)
3658 overwrite
&= 0xFFFFFF80;
3659 newbits
&= 0xFFFFFF80;
3660 newbits
|= 0x0000007F & rs
;
3662 overwrite
&= 0xFFFFFFC0;
3663 newbits
&= 0xFFFFFFC0;
3664 newbits
|= 0x0000003F & rs
;
3669 overwrite
&= 0xFFFFE07F;
3670 newbits
&= 0xFFFFE07F;
3671 newbits
|= 0x00001F80 & rs
;
3675 overwrite
&= 0xFFFFDFFF;
3676 newbits
&= 0xFFFFDFFF;
3677 newbits
|= 0x00002000 & rs
;
3681 overwrite
&= 0xFF00FFFF;
3682 newbits
&= 0xFF00FFFF;
3683 newbits
|= 0x00FF0000 & rs
;
3687 overwrite
&= 0x00FFFFFF;
3688 newbits
&= 0x00FFFFFF;
3689 #if defined(TARGET_MIPS64)
3690 newbits
|= 0xFF000000 & rs
;
3692 newbits
|= 0x0F000000 & rs
;
3697 overwrite
&= 0xFFFFBFFF;
3698 newbits
&= 0xFFFFBFFF;
3699 newbits
|= 0x00004000 & rs
;
3702 dsp
= dsp
& overwrite
;
3703 dsp
= dsp
| newbits
;
3704 env
->active_tc
.DSPControl
= dsp
;
3707 void helper_wrdsp(target_ulong rs
, target_ulong mask_num
, CPUMIPSState
*env
)
3709 return cpu_wrdsp(rs
, mask_num
, env
);
3712 uint32_t cpu_rddsp(uint32_t mask_num
, CPUMIPSState
*env
)
3720 for (i
= 0; i
< 6; i
++) {
3721 mask
[i
] = (mask_num
& ruler
) >> i
;
3726 dsp
= env
->active_tc
.DSPControl
;
3729 #if defined(TARGET_MIPS64)
3737 temp
|= dsp
& 0x1F80;
3741 temp
|= dsp
& 0x2000;
3745 temp
|= dsp
& 0x00FF0000;
3749 #if defined(TARGET_MIPS64)
3750 temp
|= dsp
& 0xFF000000;
3752 temp
|= dsp
& 0x0F000000;
3757 temp
|= dsp
& 0x4000;
3763 target_ulong
helper_rddsp(target_ulong mask_num
, CPUMIPSState
*env
)
3765 return cpu_rddsp(mask_num
, env
);
3778 #undef MIPSDSP_SPLIT32_8
3779 #undef MIPSDSP_SPLIT32_16
3781 #undef MIPSDSP_RETURN32_8
3782 #undef MIPSDSP_RETURN32_16
3784 #ifdef TARGET_MIPS64
3785 #undef MIPSDSP_SPLIT64_16
3786 #undef MIPSDSP_SPLIT64_32
3787 #undef MIPSDSP_RETURN64_16
3788 #undef MIPSDSP_RETURN64_32