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;
272 if (temp32
!= temp31
) {
278 set_DSPControl_overflow_flag(1, 16 + acc
, env
);
284 /* a[0] is LO, a[1] is HI. */
285 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret
,
292 ret
[0] = env
->active_tc
.LO
[ac
] + a
[0];
293 ret
[1] = env
->active_tc
.HI
[ac
] + a
[1];
295 if (((uint64_t)ret
[0] < (uint64_t)env
->active_tc
.LO
[ac
]) &&
296 ((uint64_t)ret
[0] < (uint64_t)a
[0])) {
300 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
302 ret
[0] = (0x01ull
<< 63);
305 ret
[0] = (0x01ull
<< 63) - 1;
308 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
312 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret
,
319 ret
[0] = env
->active_tc
.LO
[ac
] - a
[0];
320 ret
[1] = env
->active_tc
.HI
[ac
] - a
[1];
322 if ((uint64_t)ret
[0] > (uint64_t)env
->active_tc
.LO
[ac
]) {
326 if (temp64
!= ((ret
[0] >> 63) & 0x01)) {
328 ret
[0] = (0x01ull
<< 63);
331 ret
[0] = (0x01ull
<< 63) - 1;
334 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
338 static inline int32_t mipsdsp_mul_i16_i16(int16_t a
, int16_t b
,
343 temp
= (int32_t)a
* (int32_t)b
;
345 if ((temp
> (int)0x7FFF) || (temp
< (int)0xFFFF8000)) {
346 set_DSPControl_overflow_flag(1, 21, env
);
353 static inline int32_t mipsdsp_mul_u16_u16(int32_t a
, int32_t b
)
358 static inline int32_t mipsdsp_mul_i32_i32(int32_t a
, int32_t b
)
363 static inline int32_t mipsdsp_sat16_mul_i16_i16(int16_t a
, int16_t b
,
368 temp
= (int32_t)a
* (int32_t)b
;
370 if (temp
> (int)0x7FFF) {
372 set_DSPControl_overflow_flag(1, 21, env
);
373 } else if (temp
< (int)0xffff8000) {
375 set_DSPControl_overflow_flag(1, 21, env
);
382 static inline int32_t mipsdsp_mul_q15_q15_overflowflag21(uint16_t a
, uint16_t b
,
387 if ((a
== 0x8000) && (b
== 0x8000)) {
389 set_DSPControl_overflow_flag(1, 21, env
);
391 temp
= ((int32_t)(int16_t)a
* (int32_t)(int16_t)b
) << 1;
398 static inline uint8_t mipsdsp_rshift_u8(uint8_t a
, target_ulong mov
)
403 static inline uint16_t mipsdsp_rshift_u16(uint16_t a
, target_ulong mov
)
408 static inline int8_t mipsdsp_rashift8(int8_t a
, target_ulong mov
)
413 static inline int16_t mipsdsp_rashift16(int16_t a
, target_ulong mov
)
418 static inline int32_t mipsdsp_rashift32(int32_t a
, target_ulong mov
)
423 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a
, int16_t b
)
427 temp
= (int32_t)a
+ (int32_t)b
;
429 return (temp
>> 1) & 0xFFFF;
432 /* round right shift */
433 static inline int16_t mipsdsp_rrshift1_add_q16(int16_t a
, int16_t b
)
437 temp
= (int32_t)a
+ (int32_t)b
;
440 return (temp
>> 1) & 0xFFFF;
443 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a
, int32_t b
)
447 temp
= (int64_t)a
+ (int64_t)b
;
449 return (temp
>> 1) & 0xFFFFFFFF;
452 static inline int32_t mipsdsp_rrshift1_add_q32(int32_t a
, int32_t b
)
456 temp
= (int64_t)a
+ (int64_t)b
;
459 return (temp
>> 1) & 0xFFFFFFFF;
462 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a
, uint8_t b
)
466 temp
= (uint16_t)a
+ (uint16_t)b
;
468 return (temp
>> 1) & 0x00FF;
471 static inline uint8_t mipsdsp_rrshift1_add_u8(uint8_t a
, uint8_t b
)
475 temp
= (uint16_t)a
+ (uint16_t)b
+ 1;
477 return (temp
>> 1) & 0x00FF;
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;
498 /* 128 bits long. p[0] is LO, p[1] is HI. */
499 static inline void mipsdsp_rndrashift_short_acc(int64_t *p
,
506 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
507 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
508 p
[0] = (shift
== 0) ? (acc
<< 1) : (acc
>> (shift
- 1));
509 p
[1] = (acc
>> 63) & 0x01;
512 /* 128 bits long. p[0] is LO, p[1] is HI */
513 static inline void mipsdsp_rashift_acc(uint64_t *p
,
518 uint64_t tempB
, tempA
;
520 tempB
= env
->active_tc
.HI
[ac
];
521 tempA
= env
->active_tc
.LO
[ac
];
522 shift
= shift
& 0x1F;
528 p
[0] = (tempB
<< (64 - shift
)) | (tempA
>> shift
);
529 p
[1] = (int64_t)tempB
>> shift
;
533 /* 128 bits long. p[0] is LO, p[1] is HI , p[2] is sign of HI.*/
534 static inline void mipsdsp_rndrashift_acc(uint64_t *p
,
539 int64_t tempB
, tempA
;
541 tempB
= env
->active_tc
.HI
[ac
];
542 tempA
= env
->active_tc
.LO
[ac
];
543 shift
= shift
& 0x3F;
547 p
[1] = (tempB
<< 1) | (tempA
>> 63);
550 p
[0] = (tempB
<< (65 - shift
)) | (tempA
>> (shift
- 1));
551 p
[1] = (int64_t)tempB
>> (shift
- 1);
560 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac
, uint16_t a
, uint16_t b
,
565 if ((a
== 0x8000) && (b
== 0x8000)) {
567 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
569 temp
= ((int16_t)a
* (int16_t)b
) << 1;
575 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac
, uint32_t a
, uint32_t b
,
580 if ((a
== 0x80000000) && (b
== 0x80000000)) {
581 temp
= (0x01ull
<< 63) - 1;
582 set_DSPControl_overflow_flag(1, 16 + ac
, env
);
584 temp
= ((uint64_t)a
* (uint64_t)b
) << 1;
590 static inline uint16_t mipsdsp_mul_u8_u8(uint8_t a
, uint8_t b
)
592 return (uint16_t)a
* (uint16_t)b
;
595 static inline uint16_t mipsdsp_mul_u8_u16(uint8_t a
, uint16_t b
,
600 tempI
= (uint32_t)a
* (uint32_t)b
;
601 if (tempI
> 0x0000FFFF) {
603 set_DSPControl_overflow_flag(1, 21, env
);
606 return tempI
& 0x0000FFFF;
609 static inline uint64_t mipsdsp_mul_u32_u32(uint32_t a
, uint32_t b
)
611 return (uint64_t)a
* (uint64_t)b
;
614 static inline int16_t mipsdsp_rndq15_mul_q15_q15(uint16_t a
, uint16_t b
,
619 if ((a
== 0x8000) && (b
== 0x8000)) {
621 set_DSPControl_overflow_flag(1, 21, env
);
624 temp
= temp
+ 0x00008000;
627 return (temp
& 0xFFFF0000) >> 16;
630 static inline int32_t mipsdsp_sat16_mul_q15_q15(uint16_t a
, uint16_t b
,
635 if ((a
== 0x8000) && (b
== 0x8000)) {
637 set_DSPControl_overflow_flag(1, 21, env
);
639 temp
= (int16_t)a
* (int16_t)b
;
643 return (temp
>> 16) & 0x0000FFFF;
646 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a
,
651 temp
= (int32_t)a
+ 0x00008000;
653 if (a
> (int)0x7fff8000) {
655 set_DSPControl_overflow_flag(1, 22, env
);
658 return (temp
>> 16) & 0xFFFF;
661 static inline uint8_t mipsdsp_sat8_reduce_precision(uint16_t a
,
667 sign
= (a
>> 15) & 0x01;
672 set_DSPControl_overflow_flag(1, 22, env
);
675 return (mag
>> 7) & 0xFFFF;
678 set_DSPControl_overflow_flag(1, 22, env
);
683 static inline uint8_t mipsdsp_lshift8(uint8_t a
, uint8_t s
, CPUMIPSState
*env
)
691 sign
= (a
>> 7) & 0x01;
693 discard
= (((0x01 << (8 - s
)) - 1) << s
) |
694 ((a
>> (6 - (s
- 1))) & ((0x01 << s
) - 1));
696 discard
= a
>> (6 - (s
- 1));
699 if (discard
!= 0x00) {
700 set_DSPControl_overflow_flag(1, 22, env
);
706 static inline uint16_t mipsdsp_lshift16(uint16_t a
, uint8_t s
,
715 sign
= (a
>> 15) & 0x01;
717 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
718 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
720 discard
= a
>> (14 - (s
- 1));
723 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
724 set_DSPControl_overflow_flag(1, 22, env
);
731 static inline uint32_t mipsdsp_lshift32(uint32_t a
, uint8_t s
,
739 discard
= (int32_t)a
>> (31 - (s
- 1));
741 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
742 set_DSPControl_overflow_flag(1, 22, env
);
748 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a
, uint8_t s
,
757 sign
= (a
>> 15) & 0x01;
759 discard
= (((0x01 << (16 - s
)) - 1) << s
) |
760 ((a
>> (14 - (s
- 1))) & ((0x01 << s
) - 1));
762 discard
= a
>> (14 - (s
- 1));
765 if ((discard
!= 0x0000) && (discard
!= 0xFFFF)) {
766 set_DSPControl_overflow_flag(1, 22, env
);
767 return (sign
== 0) ? 0x7FFF : 0x8000;
774 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a
, uint8_t s
,
783 sign
= (a
>> 31) & 0x01;
785 discard
= (((0x01 << (32 - s
)) - 1) << s
) |
786 ((a
>> (30 - (s
- 1))) & ((0x01 << s
) - 1));
788 discard
= a
>> (30 - (s
- 1));
791 if ((discard
!= 0x00000000) && (discard
!= 0xFFFFFFFF)) {
792 set_DSPControl_overflow_flag(1, 22, env
);
793 return (sign
== 0) ? 0x7FFFFFFF : 0x80000000;
800 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a
, uint8_t s
)
805 temp
= (uint32_t)a
<< 1;
807 temp
= (int32_t)(int8_t)a
>> (s
- 1);
810 return (temp
+ 1) >> 1;
813 static inline uint16_t mipsdsp_rnd16_rashift(uint16_t a
, uint8_t s
)
818 temp
= (uint32_t)a
<< 1;
820 temp
= (int32_t)(int16_t)a
>> (s
- 1);
823 return (temp
+ 1) >> 1;
826 static inline uint32_t mipsdsp_rnd32_rashift(uint32_t a
, uint8_t s
)
831 temp
= (uint64_t)a
<< 1;
833 temp
= (int64_t)(int32_t)a
>> (s
- 1);
837 return (temp
>> 1) & 0xFFFFFFFFull
;
840 static inline uint16_t mipsdsp_sub_i16(int16_t a
, int16_t b
, CPUMIPSState
*env
)
845 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
846 set_DSPControl_overflow_flag(1, 20, env
);
852 static inline uint16_t mipsdsp_sat16_sub(int16_t a
, int16_t b
,
858 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x8000)) {
864 set_DSPControl_overflow_flag(1, 20, env
);
870 static inline uint32_t mipsdsp_sat32_sub(int32_t a
, int32_t b
,
876 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
882 set_DSPControl_overflow_flag(1, 20, env
);
885 return temp
& 0xFFFFFFFFull
;
888 static inline uint16_t mipsdsp_rshift1_sub_q16(int16_t a
, int16_t b
)
892 temp
= (int32_t)a
- (int32_t)b
;
894 return (temp
>> 1) & 0x0000FFFF;
897 static inline uint16_t mipsdsp_rrshift1_sub_q16(int16_t a
, int16_t b
)
901 temp
= (int32_t)a
- (int32_t)b
;
904 return (temp
>> 1) & 0x0000FFFF;
907 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a
, int32_t b
)
911 temp
= (int64_t)a
- (int64_t)b
;
913 return (temp
>> 1) & 0xFFFFFFFFull
;
916 static inline uint32_t mipsdsp_rrshift1_sub_q32(int32_t a
, int32_t b
)
920 temp
= (int64_t)a
- (int64_t)b
;
923 return (temp
>> 1) & 0xFFFFFFFFull
;
926 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a
, uint16_t b
,
932 temp
= (uint32_t)a
- (uint32_t)b
;
933 temp16
= (temp
>> 16) & 0x01;
935 set_DSPControl_overflow_flag(1, 20, env
);
937 return temp
& 0x0000FFFF;
940 static inline uint16_t mipsdsp_satu16_sub_u16_u16(uint16_t a
, uint16_t b
,
946 temp
= (uint32_t)a
- (uint32_t)b
;
947 temp16
= (temp
>> 16) & 0x01;
951 set_DSPControl_overflow_flag(1, 20, env
);
954 return temp
& 0x0000FFFF;
957 static inline uint8_t mipsdsp_sub_u8(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
962 temp
= (uint16_t)a
- (uint16_t)b
;
963 temp8
= (temp
>> 8) & 0x01;
965 set_DSPControl_overflow_flag(1, 20, env
);
968 return temp
& 0x00FF;
971 static inline uint8_t mipsdsp_satu8_sub(uint8_t a
, uint8_t b
, CPUMIPSState
*env
)
976 temp
= (uint16_t)a
- (uint16_t)b
;
977 temp8
= (temp
>> 8) & 0x01;
980 set_DSPControl_overflow_flag(1, 20, env
);
983 return temp
& 0x00FF;
986 static inline uint32_t mipsdsp_sub32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
991 if (MIPSDSP_OVERFLOW_SUB(a
, b
, temp
, 0x80000000)) {
992 set_DSPControl_overflow_flag(1, 20, env
);
998 static inline int32_t mipsdsp_add_i32(int32_t a
, int32_t b
, CPUMIPSState
*env
)
1004 if (MIPSDSP_OVERFLOW_ADD(a
, b
, temp
, 0x80000000)) {
1005 set_DSPControl_overflow_flag(1, 20, env
);
1011 static inline int32_t mipsdsp_cmp_eq(int32_t a
, int32_t b
)
1016 static inline int32_t mipsdsp_cmp_le(int32_t a
, int32_t b
)
1021 static inline int32_t mipsdsp_cmp_lt(int32_t a
, int32_t b
)
1026 static inline int32_t mipsdsp_cmpu_eq(uint32_t a
, uint32_t b
)
1031 static inline int32_t mipsdsp_cmpu_le(uint32_t a
, uint32_t b
)
1036 static inline int32_t mipsdsp_cmpu_lt(uint32_t a
, uint32_t b
)
1040 /*** MIPS DSP internal functions end ***/
1042 #define MIPSDSP_LHI 0xFFFFFFFF00000000ull
1043 #define MIPSDSP_LLO 0x00000000FFFFFFFFull
1044 #define MIPSDSP_HI 0xFFFF0000
1045 #define MIPSDSP_LO 0x0000FFFF
1046 #define MIPSDSP_Q3 0xFF000000
1047 #define MIPSDSP_Q2 0x00FF0000
1048 #define MIPSDSP_Q1 0x0000FF00
1049 #define MIPSDSP_Q0 0x000000FF
1051 #define MIPSDSP_SPLIT32_8(num, a, b, c, d) \
1053 a = (num >> 24) & MIPSDSP_Q0; \
1054 b = (num >> 16) & MIPSDSP_Q0; \
1055 c = (num >> 8) & MIPSDSP_Q0; \
1056 d = num & MIPSDSP_Q0; \
1059 #define MIPSDSP_SPLIT32_16(num, a, b) \
1061 a = (num >> 16) & MIPSDSP_LO; \
1062 b = num & MIPSDSP_LO; \
1065 #define MIPSDSP_RETURN32_8(a, b, c, d) ((target_long)(int32_t) \
1066 (((uint32_t)a << 24) | \
1067 (((uint32_t)b << 16) | \
1068 (((uint32_t)c << 8) | \
1069 ((uint32_t)d & 0xFF)))))
1070 #define MIPSDSP_RETURN32_16(a, b) ((target_long)(int32_t) \
1071 (((uint32_t)a << 16) | \
1072 ((uint32_t)b & 0xFFFF)))
1074 #ifdef TARGET_MIPS64
1075 #define MIPSDSP_SPLIT64_16(num, a, b, c, d) \
1077 a = (num >> 48) & MIPSDSP_LO; \
1078 b = (num >> 32) & MIPSDSP_LO; \
1079 c = (num >> 16) & MIPSDSP_LO; \
1080 d = num & MIPSDSP_LO; \
1083 #define MIPSDSP_SPLIT64_32(num, a, b) \
1085 a = (num >> 32) & MIPSDSP_LLO; \
1086 b = num & MIPSDSP_LLO; \
1089 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)a << 48) | \
1090 ((uint64_t)b << 32) | \
1091 ((uint64_t)c << 16) | \
1093 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)a << 32) | (uint64_t)b)
1096 /** DSP Arithmetic Sub-class insns **/
1097 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1098 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1101 unsigned int i, n; \
1103 n = sizeof(DSP32Value) / sizeof(dt.element[0]); \
1106 for (i = 0; i < n; i++) { \
1107 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1110 return (target_long)dt.sw[0]; \
1112 MIPSDSP32_UNOP_ENV(absq_s_ph
, sat_abs16
, sh
)
1113 MIPSDSP32_UNOP_ENV(absq_s_qb
, sat_abs8
, sb
)
1114 MIPSDSP32_UNOP_ENV(absq_s_w
, sat_abs32
, sw
)
1115 #undef MIPSDSP32_UNOP_ENV
1117 #if defined(TARGET_MIPS64)
1118 #define MIPSDSP64_UNOP_ENV(name, func, element) \
1119 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1122 unsigned int i, n; \
1124 n = sizeof(DSP64Value) / sizeof(dt.element[0]); \
1127 for (i = 0; i < n; i++) { \
1128 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1133 MIPSDSP64_UNOP_ENV(absq_s_ob
, sat_abs8
, sb
)
1134 MIPSDSP64_UNOP_ENV(absq_s_qh
, sat_abs16
, sh
)
1135 MIPSDSP64_UNOP_ENV(absq_s_pw
, sat_abs32
, sw
)
1136 #undef MIPSDSP64_UNOP_ENV
1139 #define MIPSDSP32_BINOP(name, func, element) \
1140 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1142 DSP32Value ds, dt; \
1143 unsigned int i, n; \
1145 n = sizeof(DSP32Value) / sizeof(ds.element[0]); \
1149 for (i = 0; i < n; i++) { \
1150 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1153 return (target_long)ds.sw[0]; \
1155 MIPSDSP32_BINOP(addqh_ph
, rshift1_add_q16
, sh
);
1156 MIPSDSP32_BINOP(addqh_r_ph
, rrshift1_add_q16
, sh
);
1157 MIPSDSP32_BINOP(addqh_r_w
, rrshift1_add_q32
, sw
);
1158 MIPSDSP32_BINOP(addqh_w
, rshift1_add_q32
, sw
);
1159 MIPSDSP32_BINOP(adduh_qb
, rshift1_add_u8
, ub
);
1160 MIPSDSP32_BINOP(adduh_r_qb
, rrshift1_add_u8
, ub
);
1161 MIPSDSP32_BINOP(subqh_ph
, rshift1_sub_q16
, sh
);
1162 MIPSDSP32_BINOP(subqh_r_ph
, rrshift1_sub_q16
, sh
);
1163 MIPSDSP32_BINOP(subqh_r_w
, rrshift1_sub_q32
, sw
);
1164 MIPSDSP32_BINOP(subqh_w
, rshift1_sub_q32
, sw
);
1165 #undef MIPSDSP32_BINOP
1167 #define MIPSDSP32_BINOP_ENV(name, func, element) \
1168 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1169 CPUMIPSState *env) \
1171 DSP32Value ds, dt; \
1172 unsigned int i, n; \
1174 n = sizeof(DSP32Value) / sizeof(ds.element[0]); \
1178 for (i = 0 ; i < n ; i++) { \
1179 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1182 return (target_long)ds.sw[0]; \
1184 MIPSDSP32_BINOP_ENV(addq_ph
, add_i16
, sh
)
1185 MIPSDSP32_BINOP_ENV(addq_s_ph
, sat_add_i16
, sh
)
1186 MIPSDSP32_BINOP_ENV(addq_s_w
, sat_add_i32
, sw
);
1187 MIPSDSP32_BINOP_ENV(addu_ph
, add_u16
, sh
)
1188 MIPSDSP32_BINOP_ENV(addu_qb
, add_u8
, ub
);
1189 MIPSDSP32_BINOP_ENV(addu_s_ph
, sat_add_u16
, sh
)
1190 MIPSDSP32_BINOP_ENV(addu_s_qb
, sat_add_u8
, ub
);
1191 MIPSDSP32_BINOP_ENV(subq_ph
, sub_i16
, sh
);
1192 MIPSDSP32_BINOP_ENV(subq_s_ph
, sat16_sub
, sh
);
1193 MIPSDSP32_BINOP_ENV(subq_s_w
, sat32_sub
, sw
);
1194 MIPSDSP32_BINOP_ENV(subu_ph
, sub_u16_u16
, sh
);
1195 MIPSDSP32_BINOP_ENV(subu_qb
, sub_u8
, ub
);
1196 MIPSDSP32_BINOP_ENV(subu_s_ph
, satu16_sub_u16_u16
, sh
);
1197 MIPSDSP32_BINOP_ENV(subu_s_qb
, satu8_sub
, ub
);
1198 #undef MIPSDSP32_BINOP_ENV
1200 #ifdef TARGET_MIPS64
1201 #define MIPSDSP64_BINOP(name, func, element) \
1202 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1204 DSP64Value ds, dt; \
1205 unsigned int i, n; \
1207 n = sizeof(DSP64Value) / sizeof(ds.element[0]); \
1211 for (i = 0 ; i < n ; i++) { \
1212 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1217 MIPSDSP64_BINOP(adduh_ob
, rshift1_add_u8
, ub
);
1218 MIPSDSP64_BINOP(adduh_r_ob
, rrshift1_add_u8
, ub
);
1219 MIPSDSP64_BINOP(subuh_ob
, rshift1_sub_u8
, ub
);
1220 MIPSDSP64_BINOP(subuh_r_ob
, rrshift1_sub_u8
, ub
);
1221 #undef MIPSDSP64_BINOP
1223 #define MIPSDSP64_BINOP_ENV(name, func, element) \
1224 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1225 CPUMIPSState *env) \
1227 DSP64Value ds, dt; \
1228 unsigned int i, n; \
1230 n = sizeof(DSP64Value) / sizeof(ds.element[0]); \
1234 for (i = 0 ; i < n ; i++) { \
1235 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1240 MIPSDSP64_BINOP_ENV(addq_pw
, add_i32
, sw
);
1241 MIPSDSP64_BINOP_ENV(addq_qh
, add_i16
, sh
);
1242 MIPSDSP64_BINOP_ENV(addq_s_pw
, sat_add_i32
, sw
);
1243 MIPSDSP64_BINOP_ENV(addq_s_qh
, sat_add_i16
, sh
);
1244 MIPSDSP64_BINOP_ENV(addu_ob
, add_u8
, uh
);
1245 MIPSDSP64_BINOP_ENV(addu_qh
, add_u16
, uh
);
1246 MIPSDSP64_BINOP_ENV(addu_s_ob
, sat_add_u8
, uh
);
1247 MIPSDSP64_BINOP_ENV(addu_s_qh
, sat_add_u16
, uh
);
1248 MIPSDSP64_BINOP_ENV(subq_pw
, sub32
, sw
);
1249 MIPSDSP64_BINOP_ENV(subq_qh
, sub_i16
, sh
);
1250 MIPSDSP64_BINOP_ENV(subq_s_pw
, sat32_sub
, sw
);
1251 MIPSDSP64_BINOP_ENV(subq_s_qh
, sat16_sub
, sh
);
1252 MIPSDSP64_BINOP_ENV(subu_ob
, sub_u8
, uh
);
1253 MIPSDSP64_BINOP_ENV(subu_qh
, sub_u16_u16
, uh
);
1254 MIPSDSP64_BINOP_ENV(subu_s_ob
, satu8_sub
, uh
);
1255 MIPSDSP64_BINOP_ENV(subu_s_qh
, satu16_sub_u16_u16
, uh
);
1256 #undef MIPSDSP64_BINOP_ENV
1260 #define SUBUH_QB(name, var) \
1261 target_ulong helper_##name##_qb(target_ulong rs, target_ulong rt) \
1263 uint8_t rs3, rs2, rs1, rs0; \
1264 uint8_t rt3, rt2, rt1, rt0; \
1265 uint8_t tempD, tempC, tempB, tempA; \
1267 MIPSDSP_SPLIT32_8(rs, rs3, rs2, rs1, rs0); \
1268 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1270 tempD = ((uint16_t)rs3 - (uint16_t)rt3 + var) >> 1; \
1271 tempC = ((uint16_t)rs2 - (uint16_t)rt2 + var) >> 1; \
1272 tempB = ((uint16_t)rs1 - (uint16_t)rt1 + var) >> 1; \
1273 tempA = ((uint16_t)rs0 - (uint16_t)rt0 + var) >> 1; \
1275 return ((uint32_t)tempD << 24) | ((uint32_t)tempC << 16) | \
1276 ((uint32_t)tempB << 8) | ((uint32_t)tempA); \
1280 SUBUH_QB(subuh_r
, 1);
1284 target_ulong
helper_addsc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1286 uint64_t temp
, tempRs
, tempRt
;
1289 tempRs
= (uint64_t)rs
& MIPSDSP_LLO
;
1290 tempRt
= (uint64_t)rt
& MIPSDSP_LLO
;
1292 temp
= tempRs
+ tempRt
;
1293 flag
= (temp
& 0x0100000000ull
) >> 32;
1294 set_DSPControl_carryflag(flag
, env
);
1296 return (target_long
)(int32_t)(temp
& MIPSDSP_LLO
);
1299 target_ulong
helper_addwc(target_ulong rs
, target_ulong rt
, CPUMIPSState
*env
)
1302 int32_t temp32
, temp31
;
1305 tempL
= (int64_t)(int32_t)rs
+ (int64_t)(int32_t)rt
+
1306 get_DSPControl_carryflag(env
);
1307 temp31
= (tempL
>> 31) & 0x01;
1308 temp32
= (tempL
>> 32) & 0x01;
1310 if (temp31
!= temp32
) {
1311 set_DSPControl_overflow_flag(1, 20, env
);
1314 rd
= tempL
& MIPSDSP_LLO
;
1316 return (target_long
)(int32_t)rd
;
1319 target_ulong
helper_modsub(target_ulong rs
, target_ulong rt
)
1325 decr
= rt
& MIPSDSP_Q0
;
1326 lastindex
= (rt
>> 8) & MIPSDSP_LO
;
1328 if ((rs
& MIPSDSP_LLO
) == 0x00000000) {
1329 rd
= (target_ulong
)lastindex
;
1337 target_ulong
helper_raddu_w_qb(target_ulong rs
)
1339 target_ulong ret
= 0;
1344 for (i
= 0; i
< 4; i
++) {
1350 #if defined(TARGET_MIPS64)
1351 target_ulong
helper_raddu_l_ob(target_ulong rs
)
1353 target_ulong ret
= 0;
1358 for (i
= 0; i
< 8; i
++) {
1365 #define PRECR_QB_PH(name, a, b)\
1366 target_ulong helper_##name##_qb_ph(target_ulong rs, target_ulong rt) \
1368 uint8_t tempD, tempC, tempB, tempA; \
1370 tempD = (rs >> a) & MIPSDSP_Q0; \
1371 tempC = (rs >> b) & MIPSDSP_Q0; \
1372 tempB = (rt >> a) & MIPSDSP_Q0; \
1373 tempA = (rt >> b) & MIPSDSP_Q0; \
1375 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA); \
1378 PRECR_QB_PH(precr
, 16, 0);
1379 PRECR_QB_PH(precrq
, 24, 8);
1383 target_ulong
helper_precr_sra_ph_w(uint32_t sa
, target_ulong rs
,
1386 uint16_t tempB
, tempA
;
1388 tempB
= ((int32_t)rt
>> sa
) & MIPSDSP_LO
;
1389 tempA
= ((int32_t)rs
>> sa
) & MIPSDSP_LO
;
1391 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1394 target_ulong
helper_precr_sra_r_ph_w(uint32_t sa
,
1395 target_ulong rs
, target_ulong rt
)
1397 uint64_t tempB
, tempA
;
1399 /* If sa = 0, then (sa - 1) = -1 will case shift error, so we need else. */
1401 tempB
= (rt
& MIPSDSP_LO
) << 1;
1402 tempA
= (rs
& MIPSDSP_LO
) << 1;
1404 tempB
= ((int32_t)rt
>> (sa
- 1)) + 1;
1405 tempA
= ((int32_t)rs
>> (sa
- 1)) + 1;
1407 rt
= (((tempB
>> 1) & MIPSDSP_LO
) << 16) | ((tempA
>> 1) & MIPSDSP_LO
);
1409 return (target_long
)(int32_t)rt
;
1412 target_ulong
helper_precrq_ph_w(target_ulong rs
, target_ulong rt
)
1414 uint16_t tempB
, tempA
;
1416 tempB
= (rs
& MIPSDSP_HI
) >> 16;
1417 tempA
= (rt
& MIPSDSP_HI
) >> 16;
1419 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1422 target_ulong
helper_precrq_rs_ph_w(target_ulong rs
, target_ulong rt
,
1425 uint16_t tempB
, tempA
;
1427 tempB
= mipsdsp_trunc16_sat16_round(rs
, env
);
1428 tempA
= mipsdsp_trunc16_sat16_round(rt
, env
);
1430 return MIPSDSP_RETURN32_16(tempB
, tempA
);
1433 #if defined(TARGET_MIPS64)
1434 target_ulong
helper_precr_ob_qh(target_ulong rs
, target_ulong rt
)
1436 uint8_t rs6
, rs4
, rs2
, rs0
;
1437 uint8_t rt6
, rt4
, rt2
, rt0
;
1440 rs6
= (rs
>> 48) & MIPSDSP_Q0
;
1441 rs4
= (rs
>> 32) & MIPSDSP_Q0
;
1442 rs2
= (rs
>> 16) & MIPSDSP_Q0
;
1443 rs0
= rs
& MIPSDSP_Q0
;
1444 rt6
= (rt
>> 48) & MIPSDSP_Q0
;
1445 rt4
= (rt
>> 32) & MIPSDSP_Q0
;
1446 rt2
= (rt
>> 16) & MIPSDSP_Q0
;
1447 rt0
= rt
& MIPSDSP_Q0
;
1449 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1450 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1451 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1452 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1457 #define PRECR_QH_PW(name, var) \
1458 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, target_ulong rt, \
1461 uint16_t rs3, rs2, rs1, rs0; \
1462 uint16_t rt3, rt2, rt1, rt0; \
1463 uint16_t tempD, tempC, tempB, tempA; \
1465 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
1466 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1468 /* When sa = 0, we use rt2, rt0, rs2, rs0; \
1469 * when sa != 0, we use rt3, rt1, rs3, rs1. */ \
1471 tempD = rt2 << var; \
1472 tempC = rt0 << var; \
1473 tempB = rs2 << var; \
1474 tempA = rs0 << var; \
1476 tempD = (((int16_t)rt3 >> sa) + var) >> var; \
1477 tempC = (((int16_t)rt1 >> sa) + var) >> var; \
1478 tempB = (((int16_t)rs3 >> sa) + var) >> var; \
1479 tempA = (((int16_t)rs1 >> sa) + var) >> var; \
1482 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1485 PRECR_QH_PW(sra
, 0);
1486 PRECR_QH_PW(sra_r
, 1);
1490 target_ulong
helper_precrq_ob_qh(target_ulong rs
, target_ulong rt
)
1492 uint8_t rs6
, rs4
, rs2
, rs0
;
1493 uint8_t rt6
, rt4
, rt2
, rt0
;
1496 rs6
= (rs
>> 56) & MIPSDSP_Q0
;
1497 rs4
= (rs
>> 40) & MIPSDSP_Q0
;
1498 rs2
= (rs
>> 24) & MIPSDSP_Q0
;
1499 rs0
= (rs
>> 8) & MIPSDSP_Q0
;
1500 rt6
= (rt
>> 56) & MIPSDSP_Q0
;
1501 rt4
= (rt
>> 40) & MIPSDSP_Q0
;
1502 rt2
= (rt
>> 24) & MIPSDSP_Q0
;
1503 rt0
= (rt
>> 8) & MIPSDSP_Q0
;
1505 temp
= ((uint64_t)rs6
<< 56) | ((uint64_t)rs4
<< 48) |
1506 ((uint64_t)rs2
<< 40) | ((uint64_t)rs0
<< 32) |
1507 ((uint64_t)rt6
<< 24) | ((uint64_t)rt4
<< 16) |
1508 ((uint64_t)rt2
<< 8) | (uint64_t)rt0
;
1513 target_ulong
helper_precrq_qh_pw(target_ulong rs
, target_ulong rt
)
1515 uint16_t tempD
, tempC
, tempB
, tempA
;
1517 tempD
= (rs
>> 48) & MIPSDSP_LO
;
1518 tempC
= (rs
>> 16) & MIPSDSP_LO
;
1519 tempB
= (rt
>> 48) & MIPSDSP_LO
;
1520 tempA
= (rt
>> 16) & MIPSDSP_LO
;
1522 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1525 target_ulong
helper_precrq_rs_qh_pw(target_ulong rs
, target_ulong rt
,
1530 uint16_t tempD
, tempC
, tempB
, tempA
;
1532 rs2
= (rs
>> 32) & MIPSDSP_LLO
;
1533 rs0
= rs
& MIPSDSP_LLO
;
1534 rt2
= (rt
>> 32) & MIPSDSP_LLO
;
1535 rt0
= rt
& MIPSDSP_LLO
;
1537 tempD
= mipsdsp_trunc16_sat16_round(rs2
, env
);
1538 tempC
= mipsdsp_trunc16_sat16_round(rs0
, env
);
1539 tempB
= mipsdsp_trunc16_sat16_round(rt2
, env
);
1540 tempA
= mipsdsp_trunc16_sat16_round(rt0
, env
);
1542 return MIPSDSP_RETURN64_16(tempD
, tempC
, tempB
, tempA
);
1545 target_ulong
helper_precrq_pw_l(target_ulong rs
, target_ulong rt
)
1547 uint32_t tempB
, tempA
;
1549 tempB
= (rs
>> 32) & MIPSDSP_LLO
;
1550 tempA
= (rt
>> 32) & MIPSDSP_LLO
;
1552 return MIPSDSP_RETURN64_32(tempB
, tempA
);
1556 target_ulong
helper_precrqu_s_qb_ph(target_ulong rs
, target_ulong rt
,
1559 uint8_t tempD
, tempC
, tempB
, tempA
;
1560 uint16_t rsh
, rsl
, rth
, rtl
;
1562 rsh
= (rs
& MIPSDSP_HI
) >> 16;
1563 rsl
= rs
& MIPSDSP_LO
;
1564 rth
= (rt
& MIPSDSP_HI
) >> 16;
1565 rtl
= rt
& MIPSDSP_LO
;
1567 tempD
= mipsdsp_sat8_reduce_precision(rsh
, env
);
1568 tempC
= mipsdsp_sat8_reduce_precision(rsl
, env
);
1569 tempB
= mipsdsp_sat8_reduce_precision(rth
, env
);
1570 tempA
= mipsdsp_sat8_reduce_precision(rtl
, env
);
1572 return MIPSDSP_RETURN32_8(tempD
, tempC
, tempB
, tempA
);
1575 #if defined(TARGET_MIPS64)
1576 target_ulong
helper_precrqu_s_ob_qh(target_ulong rs
, target_ulong rt
,
1580 uint16_t rs3
, rs2
, rs1
, rs0
;
1581 uint16_t rt3
, rt2
, rt1
, rt0
;
1587 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
1588 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
1590 temp
[7] = mipsdsp_sat8_reduce_precision(rs3
, env
);
1591 temp
[6] = mipsdsp_sat8_reduce_precision(rs2
, env
);
1592 temp
[5] = mipsdsp_sat8_reduce_precision(rs1
, env
);
1593 temp
[4] = mipsdsp_sat8_reduce_precision(rs0
, env
);
1594 temp
[3] = mipsdsp_sat8_reduce_precision(rt3
, env
);
1595 temp
[2] = mipsdsp_sat8_reduce_precision(rt2
, env
);
1596 temp
[1] = mipsdsp_sat8_reduce_precision(rt1
, env
);
1597 temp
[0] = mipsdsp_sat8_reduce_precision(rt0
, env
);
1599 for (i
= 0; i
< 8; i
++) {
1600 result
|= (uint64_t)temp
[i
] << (8 * i
);
1606 #define PRECEQ_PW(name, a, b) \
1607 target_ulong helper_preceq_pw_##name(target_ulong rt) \
1609 uint16_t tempB, tempA; \
1610 uint32_t tempBI, tempAI; \
1612 tempB = (rt >> a) & MIPSDSP_LO; \
1613 tempA = (rt >> b) & MIPSDSP_LO; \
1615 tempBI = (uint32_t)tempB << 16; \
1616 tempAI = (uint32_t)tempA << 16; \
1618 return MIPSDSP_RETURN64_32(tempBI, tempAI); \
1621 PRECEQ_PW(qhl
, 48, 32);
1622 PRECEQ_PW(qhr
, 16, 0);
1623 PRECEQ_PW(qhla
, 48, 16);
1624 PRECEQ_PW(qhra
, 32, 0);
1630 #define PRECEQU_PH(name, a, b) \
1631 target_ulong helper_precequ_ph_##name(target_ulong rt) \
1633 uint16_t tempB, tempA; \
1635 tempB = (rt >> a) & MIPSDSP_Q0; \
1636 tempA = (rt >> b) & MIPSDSP_Q0; \
1638 tempB = tempB << 7; \
1639 tempA = tempA << 7; \
1641 return MIPSDSP_RETURN32_16(tempB, tempA); \
1644 PRECEQU_PH(qbl
, 24, 16);
1645 PRECEQU_PH(qbr
, 8, 0);
1646 PRECEQU_PH(qbla
, 24, 8);
1647 PRECEQU_PH(qbra
, 16, 0);
1651 #if defined(TARGET_MIPS64)
1652 #define PRECEQU_QH(name, a, b, c, d) \
1653 target_ulong helper_precequ_qh_##name(target_ulong rt) \
1655 uint16_t tempD, tempC, tempB, tempA; \
1657 tempD = (rt >> a) & MIPSDSP_Q0; \
1658 tempC = (rt >> b) & MIPSDSP_Q0; \
1659 tempB = (rt >> c) & MIPSDSP_Q0; \
1660 tempA = (rt >> d) & MIPSDSP_Q0; \
1662 tempD = tempD << 7; \
1663 tempC = tempC << 7; \
1664 tempB = tempB << 7; \
1665 tempA = tempA << 7; \
1667 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1670 PRECEQU_QH(obl
, 56, 48, 40, 32);
1671 PRECEQU_QH(obr
, 24, 16, 8, 0);
1672 PRECEQU_QH(obla
, 56, 40, 24, 8);
1673 PRECEQU_QH(obra
, 48, 32, 16, 0);
1679 #define PRECEU_PH(name, a, b) \
1680 target_ulong helper_preceu_ph_##name(target_ulong rt) \
1682 uint16_t tempB, tempA; \
1684 tempB = (rt >> a) & MIPSDSP_Q0; \
1685 tempA = (rt >> b) & MIPSDSP_Q0; \
1687 return MIPSDSP_RETURN32_16(tempB, tempA); \
1690 PRECEU_PH(qbl
, 24, 16);
1691 PRECEU_PH(qbr
, 8, 0);
1692 PRECEU_PH(qbla
, 24, 8);
1693 PRECEU_PH(qbra
, 16, 0);
1697 #if defined(TARGET_MIPS64)
1698 #define PRECEU_QH(name, a, b, c, d) \
1699 target_ulong helper_preceu_qh_##name(target_ulong rt) \
1701 uint16_t tempD, tempC, tempB, tempA; \
1703 tempD = (rt >> a) & MIPSDSP_Q0; \
1704 tempC = (rt >> b) & MIPSDSP_Q0; \
1705 tempB = (rt >> c) & MIPSDSP_Q0; \
1706 tempA = (rt >> d) & MIPSDSP_Q0; \
1708 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1711 PRECEU_QH(obl
, 56, 48, 40, 32);
1712 PRECEU_QH(obr
, 24, 16, 8, 0);
1713 PRECEU_QH(obla
, 56, 40, 24, 8);
1714 PRECEU_QH(obra
, 48, 32, 16, 0);
1720 /** DSP GPR-Based Shift Sub-class insns **/
1721 #define SHIFT_QB(name, func) \
1722 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt) \
1724 uint8_t rt3, rt2, rt1, rt0; \
1728 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1730 rt3 = mipsdsp_##func(rt3, sa); \
1731 rt2 = mipsdsp_##func(rt2, sa); \
1732 rt1 = mipsdsp_##func(rt1, sa); \
1733 rt0 = mipsdsp_##func(rt0, sa); \
1735 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1738 #define SHIFT_QB_ENV(name, func) \
1739 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt,\
1740 CPUMIPSState *env) \
1742 uint8_t rt3, rt2, rt1, rt0; \
1746 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1748 rt3 = mipsdsp_##func(rt3, sa, env); \
1749 rt2 = mipsdsp_##func(rt2, sa, env); \
1750 rt1 = mipsdsp_##func(rt1, sa, env); \
1751 rt0 = mipsdsp_##func(rt0, sa, env); \
1753 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1756 SHIFT_QB_ENV(shll
, lshift8
);
1757 SHIFT_QB(shrl
, rshift_u8
);
1759 SHIFT_QB(shra
, rashift8
);
1760 SHIFT_QB(shra_r
, rnd8_rashift
);
1765 #if defined(TARGET_MIPS64)
1766 #define SHIFT_OB(name, func) \
1767 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1776 for (i = 0; i < 8; i++) { \
1777 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1778 rt_t[i] = mipsdsp_##func(rt_t[i], sa); \
1779 temp |= (uint64_t)rt_t[i] << (8 * i); \
1785 #define SHIFT_OB_ENV(name, func) \
1786 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1787 CPUMIPSState *env) \
1796 for (i = 0; i < 8; i++) { \
1797 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1798 rt_t[i] = mipsdsp_##func(rt_t[i], sa, env); \
1799 temp |= (uint64_t)rt_t[i] << (8 * i); \
1805 SHIFT_OB_ENV(shll
, lshift8
);
1806 SHIFT_OB(shrl
, rshift_u8
);
1808 SHIFT_OB(shra
, rashift8
);
1809 SHIFT_OB(shra_r
, rnd8_rashift
);
1816 #define SHIFT_PH(name, func) \
1817 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt, \
1818 CPUMIPSState *env) \
1820 uint16_t rth, rtl; \
1824 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1826 rth = mipsdsp_##func(rth, sa, env); \
1827 rtl = mipsdsp_##func(rtl, sa, env); \
1829 return MIPSDSP_RETURN32_16(rth, rtl); \
1832 SHIFT_PH(shll
, lshift16
);
1833 SHIFT_PH(shll_s
, sat16_lshift
);
1837 #if defined(TARGET_MIPS64)
1838 #define SHIFT_QH(name, func) \
1839 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa) \
1841 uint16_t rt3, rt2, rt1, rt0; \
1845 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1847 rt3 = mipsdsp_##func(rt3, sa); \
1848 rt2 = mipsdsp_##func(rt2, sa); \
1849 rt1 = mipsdsp_##func(rt1, sa); \
1850 rt0 = mipsdsp_##func(rt0, sa); \
1852 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1855 #define SHIFT_QH_ENV(name, func) \
1856 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa, \
1857 CPUMIPSState *env) \
1859 uint16_t rt3, rt2, rt1, rt0; \
1863 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1865 rt3 = mipsdsp_##func(rt3, sa, env); \
1866 rt2 = mipsdsp_##func(rt2, sa, env); \
1867 rt1 = mipsdsp_##func(rt1, sa, env); \
1868 rt0 = mipsdsp_##func(rt0, sa, env); \
1870 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1873 SHIFT_QH_ENV(shll
, lshift16
);
1874 SHIFT_QH_ENV(shll_s
, sat16_lshift
);
1876 SHIFT_QH(shrl
, rshift_u16
);
1877 SHIFT_QH(shra
, rashift16
);
1878 SHIFT_QH(shra_r
, rnd16_rashift
);
1885 #define SHIFT_W(name, func) \
1886 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1891 temp = mipsdsp_##func(rt, sa); \
1893 return (target_long)(int32_t)temp; \
1896 #define SHIFT_W_ENV(name, func) \
1897 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt, \
1898 CPUMIPSState *env) \
1903 temp = mipsdsp_##func(rt, sa, env); \
1905 return (target_long)(int32_t)temp; \
1908 SHIFT_W_ENV(shll_s
, sat32_lshift
);
1909 SHIFT_W(shra_r
, rnd32_rashift
);
1914 #if defined(TARGET_MIPS64)
1915 #define SHIFT_PW(name, func) \
1916 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa) \
1918 uint32_t rt1, rt0; \
1921 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1923 rt1 = mipsdsp_##func(rt1, sa); \
1924 rt0 = mipsdsp_##func(rt0, sa); \
1926 return MIPSDSP_RETURN64_32(rt1, rt0); \
1929 #define SHIFT_PW_ENV(name, func) \
1930 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa, \
1931 CPUMIPSState *env) \
1933 uint32_t rt1, rt0; \
1936 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1938 rt1 = mipsdsp_##func(rt1, sa, env); \
1939 rt0 = mipsdsp_##func(rt0, sa, env); \
1941 return MIPSDSP_RETURN64_32(rt1, rt0); \
1944 SHIFT_PW_ENV(shll
, lshift32
);
1945 SHIFT_PW_ENV(shll_s
, sat32_lshift
);
1947 SHIFT_PW(shra
, rashift32
);
1948 SHIFT_PW(shra_r
, rnd32_rashift
);
1955 #define SHIFT_PH(name, func) \
1956 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1958 uint16_t rth, rtl; \
1962 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1964 rth = mipsdsp_##func(rth, sa); \
1965 rtl = mipsdsp_##func(rtl, sa); \
1967 return MIPSDSP_RETURN32_16(rth, rtl); \
1970 SHIFT_PH(shrl
, rshift_u16
);
1971 SHIFT_PH(shra
, rashift16
);
1972 SHIFT_PH(shra_r
, rnd16_rashift
);
1976 /** DSP Multiply Sub-class insns **/
1977 /* Return value made up by two 16bits value.
1978 * FIXME give the macro a better name.
1980 #define MUL_RETURN32_16_PH(name, func, \
1981 rsmov1, rsmov2, rsfilter, \
1982 rtmov1, rtmov2, rtfilter) \
1983 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1984 CPUMIPSState *env) \
1986 uint16_t rsB, rsA, rtB, rtA; \
1988 rsB = (rs >> rsmov1) & rsfilter; \
1989 rsA = (rs >> rsmov2) & rsfilter; \
1990 rtB = (rt >> rtmov1) & rtfilter; \
1991 rtA = (rt >> rtmov2) & rtfilter; \
1993 rsB = mipsdsp_##func(rsB, rtB, env); \
1994 rsA = mipsdsp_##func(rsA, rtA, env); \
1996 return MIPSDSP_RETURN32_16(rsB, rsA); \
1999 MUL_RETURN32_16_PH(muleu_s_ph_qbl
, mul_u8_u16
, \
2000 24, 16, MIPSDSP_Q0
, \
2002 MUL_RETURN32_16_PH(muleu_s_ph_qbr
, mul_u8_u16
, \
2005 MUL_RETURN32_16_PH(mulq_rs_ph
, rndq15_mul_q15_q15
, \
2006 16, 0, MIPSDSP_LO
, \
2008 MUL_RETURN32_16_PH(mul_ph
, mul_i16_i16
, \
2009 16, 0, MIPSDSP_LO
, \
2011 MUL_RETURN32_16_PH(mul_s_ph
, sat16_mul_i16_i16
, \
2012 16, 0, MIPSDSP_LO
, \
2014 MUL_RETURN32_16_PH(mulq_s_ph
, sat16_mul_q15_q15
, \
2015 16, 0, MIPSDSP_LO
, \
2018 #undef MUL_RETURN32_16_PH
2020 #define MUL_RETURN32_32_ph(name, func, movbits) \
2021 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2022 CPUMIPSState *env) \
2027 rsh = (rs >> movbits) & MIPSDSP_LO; \
2028 rth = (rt >> movbits) & MIPSDSP_LO; \
2029 temp = mipsdsp_##func(rsh, rth, env); \
2031 return (target_long)(int32_t)temp; \
2034 MUL_RETURN32_32_ph(muleq_s_w_phl
, mul_q15_q15_overflowflag21
, 16);
2035 MUL_RETURN32_32_ph(muleq_s_w_phr
, mul_q15_q15_overflowflag21
, 0);
2037 #undef MUL_RETURN32_32_ph
2039 #define MUL_VOID_PH(name, use_ac_env) \
2040 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2041 CPUMIPSState *env) \
2043 int16_t rsh, rsl, rth, rtl; \
2044 int32_t tempB, tempA; \
2045 int64_t acc, dotp; \
2047 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2048 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2050 if (use_ac_env == 1) { \
2051 tempB = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2052 tempA = mipsdsp_mul_q15_q15(ac, rsl, rtl, env); \
2054 tempB = mipsdsp_mul_u16_u16(rsh, rth); \
2055 tempA = mipsdsp_mul_u16_u16(rsl, rtl); \
2058 dotp = (int64_t)tempB - (int64_t)tempA; \
2059 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2060 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2061 dotp = dotp + acc; \
2062 env->active_tc.HI[ac] = (target_long)(int32_t) \
2063 ((dotp & MIPSDSP_LHI) >> 32); \
2064 env->active_tc.LO[ac] = (target_long)(int32_t)(dotp & MIPSDSP_LLO); \
2067 MUL_VOID_PH(mulsaq_s_w_ph
, 1);
2068 MUL_VOID_PH(mulsa_w_ph
, 0);
2072 #if defined(TARGET_MIPS64)
2073 #define MUL_RETURN64_16_QH(name, func, \
2074 rsmov1, rsmov2, rsmov3, rsmov4, rsfilter, \
2075 rtmov1, rtmov2, rtmov3, rtmov4, rtfilter) \
2076 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2077 CPUMIPSState *env) \
2079 uint16_t rs3, rs2, rs1, rs0; \
2080 uint16_t rt3, rt2, rt1, rt0; \
2081 uint16_t tempD, tempC, tempB, tempA; \
2083 rs3 = (rs >> rsmov1) & rsfilter; \
2084 rs2 = (rs >> rsmov2) & rsfilter; \
2085 rs1 = (rs >> rsmov3) & rsfilter; \
2086 rs0 = (rs >> rsmov4) & rsfilter; \
2087 rt3 = (rt >> rtmov1) & rtfilter; \
2088 rt2 = (rt >> rtmov2) & rtfilter; \
2089 rt1 = (rt >> rtmov3) & rtfilter; \
2090 rt0 = (rt >> rtmov4) & rtfilter; \
2092 tempD = mipsdsp_##func(rs3, rt3, env); \
2093 tempC = mipsdsp_##func(rs2, rt2, env); \
2094 tempB = mipsdsp_##func(rs1, rt1, env); \
2095 tempA = mipsdsp_##func(rs0, rt0, env); \
2097 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
2100 MUL_RETURN64_16_QH(muleu_s_qh_obl
, mul_u8_u16
, \
2101 56, 48, 40, 32, MIPSDSP_Q0
, \
2102 48, 32, 16, 0, MIPSDSP_LO
);
2103 MUL_RETURN64_16_QH(muleu_s_qh_obr
, mul_u8_u16
, \
2104 24, 16, 8, 0, MIPSDSP_Q0
, \
2105 48, 32, 16, 0, MIPSDSP_LO
);
2106 MUL_RETURN64_16_QH(mulq_rs_qh
, rndq15_mul_q15_q15
, \
2107 48, 32, 16, 0, MIPSDSP_LO
, \
2108 48, 32, 16, 0, MIPSDSP_LO
);
2110 #undef MUL_RETURN64_16_QH
2112 #define MUL_RETURN64_32_QH(name, \
2115 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2116 CPUMIPSState *env) \
2118 uint16_t rsB, rsA; \
2119 uint16_t rtB, rtA; \
2120 uint32_t tempB, tempA; \
2122 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2123 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2124 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2125 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2127 tempB = mipsdsp_mul_q15_q15(5, rsB, rtB, env); \
2128 tempA = mipsdsp_mul_q15_q15(5, rsA, rtA, env); \
2130 return ((uint64_t)tempB << 32) | (uint64_t)tempA; \
2133 MUL_RETURN64_32_QH(muleq_s_pw_qhl
, 48, 32, 48, 32);
2134 MUL_RETURN64_32_QH(muleq_s_pw_qhr
, 16, 0, 16, 0);
2136 #undef MUL_RETURN64_32_QH
2138 void helper_mulsaq_s_w_qh(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2141 int16_t rs3
, rs2
, rs1
, rs0
;
2142 int16_t rt3
, rt2
, rt1
, rt0
;
2143 int32_t tempD
, tempC
, tempB
, tempA
;
2148 MIPSDSP_SPLIT64_16(rs
, rs3
, rs2
, rs1
, rs0
);
2149 MIPSDSP_SPLIT64_16(rt
, rt3
, rt2
, rt1
, rt0
);
2151 tempD
= mipsdsp_mul_q15_q15(ac
, rs3
, rt3
, env
);
2152 tempC
= mipsdsp_mul_q15_q15(ac
, rs2
, rt2
, env
);
2153 tempB
= mipsdsp_mul_q15_q15(ac
, rs1
, rt1
, env
);
2154 tempA
= mipsdsp_mul_q15_q15(ac
, rs0
, rt0
, env
);
2156 temp
[0] = ((int32_t)tempD
- (int32_t)tempC
) +
2157 ((int32_t)tempB
- (int32_t)tempA
);
2158 temp
[0] = (int64_t)(temp
[0] << 30) >> 30;
2159 if (((temp
[0] >> 33) & 0x01) == 0) {
2165 acc
[0] = env
->active_tc
.LO
[ac
];
2166 acc
[1] = env
->active_tc
.HI
[ac
];
2168 temp_sum
= acc
[0] + temp
[0];
2169 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2170 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2176 env
->active_tc
.HI
[ac
] = acc
[1];
2177 env
->active_tc
.LO
[ac
] = acc
[0];
2181 #define DP_QB(name, func, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2182 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2183 CPUMIPSState *env) \
2187 uint16_t tempB, tempA; \
2188 uint64_t tempC, dotp; \
2190 rs3 = (rs >> rsmov1) & MIPSDSP_Q0; \
2191 rs2 = (rs >> rsmov2) & MIPSDSP_Q0; \
2192 rt3 = (rt >> rtmov1) & MIPSDSP_Q0; \
2193 rt2 = (rt >> rtmov2) & MIPSDSP_Q0; \
2194 tempB = mipsdsp_##func(rs3, rt3); \
2195 tempA = mipsdsp_##func(rs2, rt2); \
2196 dotp = (int64_t)tempB + (int64_t)tempA; \
2198 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2199 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2202 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2203 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2207 env->active_tc.HI[ac] = (target_long)(int32_t) \
2208 ((tempC & MIPSDSP_LHI) >> 32); \
2209 env->active_tc.LO[ac] = (target_long)(int32_t)(tempC & MIPSDSP_LLO); \
2212 DP_QB(dpau_h_qbl
, mul_u8_u8
, 1, 24, 16, 24, 16);
2213 DP_QB(dpau_h_qbr
, mul_u8_u8
, 1, 8, 0, 8, 0);
2214 DP_QB(dpsu_h_qbl
, mul_u8_u8
, 0, 24, 16, 24, 16);
2215 DP_QB(dpsu_h_qbr
, mul_u8_u8
, 0, 8, 0, 8, 0);
2219 #if defined(TARGET_MIPS64)
2220 #define DP_OB(name, add_sub, \
2221 rsmov1, rsmov2, rsmov3, rsmov4, \
2222 rtmov1, rtmov2, rtmov3, rtmov4) \
2223 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2224 CPUMIPSState *env) \
2226 uint8_t rsD, rsC, rsB, rsA; \
2227 uint8_t rtD, rtC, rtB, rtA; \
2228 uint16_t tempD, tempC, tempB, tempA; \
2231 uint64_t temp_sum; \
2236 rsD = (rs >> rsmov1) & MIPSDSP_Q0; \
2237 rsC = (rs >> rsmov2) & MIPSDSP_Q0; \
2238 rsB = (rs >> rsmov3) & MIPSDSP_Q0; \
2239 rsA = (rs >> rsmov4) & MIPSDSP_Q0; \
2240 rtD = (rt >> rtmov1) & MIPSDSP_Q0; \
2241 rtC = (rt >> rtmov2) & MIPSDSP_Q0; \
2242 rtB = (rt >> rtmov3) & MIPSDSP_Q0; \
2243 rtA = (rt >> rtmov4) & MIPSDSP_Q0; \
2245 tempD = mipsdsp_mul_u8_u8(rsD, rtD); \
2246 tempC = mipsdsp_mul_u8_u8(rsC, rtC); \
2247 tempB = mipsdsp_mul_u8_u8(rsB, rtB); \
2248 tempA = mipsdsp_mul_u8_u8(rsA, rtA); \
2250 temp[0] = (uint64_t)tempD + (uint64_t)tempC + \
2251 (uint64_t)tempB + (uint64_t)tempA; \
2253 acc[0] = env->active_tc.LO[ac]; \
2254 acc[1] = env->active_tc.HI[ac]; \
2257 temp_sum = acc[0] + temp[0]; \
2258 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2259 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2262 temp[0] = temp_sum; \
2263 temp[1] = acc[1] + temp[1]; \
2265 temp_sum = acc[0] - temp[0]; \
2266 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2269 temp[0] = temp_sum; \
2270 temp[1] = acc[1] - temp[1]; \
2273 env->active_tc.HI[ac] = temp[1]; \
2274 env->active_tc.LO[ac] = temp[0]; \
2277 DP_OB(dpau_h_obl
, 1, 56, 48, 40, 32, 56, 48, 40, 32);
2278 DP_OB(dpau_h_obr
, 1, 24, 16, 8, 0, 24, 16, 8, 0);
2279 DP_OB(dpsu_h_obl
, 0, 56, 48, 40, 32, 56, 48, 40, 32);
2280 DP_OB(dpsu_h_obr
, 0, 24, 16, 8, 0, 24, 16, 8, 0);
2285 #define DP_NOFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2286 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2287 CPUMIPSState *env) \
2289 int16_t rsB, rsA, rtB, rtA; \
2290 int32_t tempA, tempB; \
2293 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2294 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2295 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2296 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2298 tempB = (int32_t)rsB * (int32_t)rtB; \
2299 tempA = (int32_t)rsA * (int32_t)rtA; \
2301 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2302 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2305 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2307 acc = acc - ((int64_t)tempB + (int64_t)tempA); \
2310 env->active_tc.HI[ac] = (target_long)(int32_t)((acc & MIPSDSP_LHI) >> 32); \
2311 env->active_tc.LO[ac] = (target_long)(int32_t)(acc & MIPSDSP_LLO); \
2314 DP_NOFUNC_PH(dpa_w_ph
, 1, 16, 0, 16, 0);
2315 DP_NOFUNC_PH(dpax_w_ph
, 1, 16, 0, 0, 16);
2316 DP_NOFUNC_PH(dps_w_ph
, 0, 16, 0, 16, 0);
2317 DP_NOFUNC_PH(dpsx_w_ph
, 0, 16, 0, 0, 16);
2320 #define DP_HASFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2321 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2322 CPUMIPSState *env) \
2324 int16_t rsB, rsA, rtB, rtA; \
2325 int32_t tempB, tempA; \
2326 int64_t acc, dotp; \
2328 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2329 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2330 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2331 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2333 tempB = mipsdsp_mul_q15_q15(ac, rsB, rtB, env); \
2334 tempA = mipsdsp_mul_q15_q15(ac, rsA, rtA, env); \
2336 dotp = (int64_t)tempB + (int64_t)tempA; \
2337 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2338 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2346 env->active_tc.HI[ac] = (target_long)(int32_t) \
2347 ((acc & MIPSDSP_LHI) >> 32); \
2348 env->active_tc.LO[ac] = (target_long)(int32_t) \
2349 (acc & MIPSDSP_LLO); \
2352 DP_HASFUNC_PH(dpaq_s_w_ph
, 1, 16, 0, 16, 0);
2353 DP_HASFUNC_PH(dpaqx_s_w_ph
, 1, 16, 0, 0, 16);
2354 DP_HASFUNC_PH(dpsq_s_w_ph
, 0, 16, 0, 16, 0);
2355 DP_HASFUNC_PH(dpsqx_s_w_ph
, 0, 16, 0, 0, 16);
2357 #undef DP_HASFUNC_PH
2359 #define DP_128OPERATION_PH(name, is_add) \
2360 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2361 CPUMIPSState *env) \
2363 int16_t rsh, rsl, rth, rtl; \
2364 int32_t tempB, tempA, tempC62_31, tempC63; \
2365 int64_t acc, dotp, tempC; \
2367 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2368 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2370 tempB = mipsdsp_mul_q15_q15(ac, rsh, rtl, env); \
2371 tempA = mipsdsp_mul_q15_q15(ac, rsl, rth, env); \
2373 dotp = (int64_t)tempB + (int64_t)tempA; \
2374 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2375 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2377 tempC = acc + dotp; \
2379 tempC = acc - dotp; \
2381 tempC63 = (tempC >> 63) & 0x01; \
2382 tempC62_31 = (tempC >> 31) & 0xFFFFFFFF; \
2384 if ((tempC63 == 0) && (tempC62_31 != 0x00000000)) { \
2385 tempC = 0x7FFFFFFF; \
2386 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2389 if ((tempC63 == 1) && (tempC62_31 != 0xFFFFFFFF)) { \
2390 tempC = (int64_t)(int32_t)0x80000000; \
2391 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2394 env->active_tc.HI[ac] = (target_long)(int32_t) \
2395 ((tempC & MIPSDSP_LHI) >> 32); \
2396 env->active_tc.LO[ac] = (target_long)(int32_t) \
2397 (tempC & MIPSDSP_LLO); \
2400 DP_128OPERATION_PH(dpaqx_sa_w_ph
, 1);
2401 DP_128OPERATION_PH(dpsqx_sa_w_ph
, 0);
2403 #undef DP_128OPERATION_HP
2405 #if defined(TARGET_MIPS64)
2406 #define DP_QH(name, is_add, use_ac_env) \
2407 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2408 CPUMIPSState *env) \
2410 int32_t rs3, rs2, rs1, rs0; \
2411 int32_t rt3, rt2, rt1, rt0; \
2412 int32_t tempD, tempC, tempB, tempA; \
2417 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2418 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2421 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env); \
2422 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env); \
2423 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env); \
2424 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env); \
2426 tempD = mipsdsp_mul_u16_u16(rs3, rt3); \
2427 tempC = mipsdsp_mul_u16_u16(rs2, rt2); \
2428 tempB = mipsdsp_mul_u16_u16(rs1, rt1); \
2429 tempA = mipsdsp_mul_u16_u16(rs0, rt0); \
2432 temp[0] = (int64_t)tempD + (int64_t)tempC + \
2433 (int64_t)tempB + (int64_t)tempA; \
2435 if (temp[0] >= 0) { \
2441 acc[1] = env->active_tc.HI[ac]; \
2442 acc[0] = env->active_tc.LO[ac]; \
2445 temp_sum = acc[0] + temp[0]; \
2446 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2447 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2448 acc[1] = acc[1] + 1; \
2450 temp[0] = temp_sum; \
2451 temp[1] = acc[1] + temp[1]; \
2453 temp_sum = acc[0] - temp[0]; \
2454 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2455 acc[1] = acc[1] - 1; \
2457 temp[0] = temp_sum; \
2458 temp[1] = acc[1] - temp[1]; \
2461 env->active_tc.HI[ac] = temp[1]; \
2462 env->active_tc.LO[ac] = temp[0]; \
2465 DP_QH(dpa_w_qh
, 1, 0);
2466 DP_QH(dpaq_s_w_qh
, 1, 1);
2467 DP_QH(dps_w_qh
, 0, 0);
2468 DP_QH(dpsq_s_w_qh
, 0, 1);
2474 #define DP_L_W(name, is_add) \
2475 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2476 CPUMIPSState *env) \
2479 int64_t dotp, acc; \
2483 dotp = mipsdsp_mul_q31_q31(ac, rs, rt, env); \
2484 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2485 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2487 temp = acc + dotp; \
2488 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2489 temp, (0x01ull << 63)); \
2491 temp = acc - dotp; \
2492 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2493 temp, (0x01ull << 63)); \
2497 temp63 = (temp >> 63) & 0x01; \
2498 if (temp63 == 1) { \
2499 temp = (0x01ull << 63) - 1; \
2501 temp = 0x01ull << 63; \
2504 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2507 env->active_tc.HI[ac] = (target_long)(int32_t) \
2508 ((temp & MIPSDSP_LHI) >> 32); \
2509 env->active_tc.LO[ac] = (target_long)(int32_t) \
2510 (temp & MIPSDSP_LLO); \
2513 DP_L_W(dpaq_sa_l_w
, 1);
2514 DP_L_W(dpsq_sa_l_w
, 0);
2518 #if defined(TARGET_MIPS64)
2519 #define DP_L_PW(name, func) \
2520 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2521 CPUMIPSState *env) \
2525 int64_t tempB[2], tempA[2]; \
2533 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2534 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2536 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env); \
2537 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env); \
2539 if (tempB[0] >= 0) { \
2545 if (tempA[0] >= 0) { \
2551 temp_sum = tempB[0] + tempA[0]; \
2552 if (((uint64_t)temp_sum < (uint64_t)tempB[0]) && \
2553 ((uint64_t)temp_sum < (uint64_t)tempA[0])) { \
2556 temp[0] = temp_sum; \
2557 temp[1] += tempB[1] + tempA[1]; \
2559 mipsdsp_##func(acc, ac, temp, env); \
2561 env->active_tc.HI[ac] = acc[1]; \
2562 env->active_tc.LO[ac] = acc[0]; \
2565 DP_L_PW(dpaq_sa_l_pw
, sat64_acc_add_q63
);
2566 DP_L_PW(dpsq_sa_l_pw
, sat64_acc_sub_q63
);
2570 void helper_mulsaq_s_l_pw(target_ulong rs
, target_ulong rt
, uint32_t ac
,
2575 int64_t tempB
[2], tempA
[2];
2580 rs1
= (rs
>> 32) & MIPSDSP_LLO
;
2581 rs0
= rs
& MIPSDSP_LLO
;
2582 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
2583 rt0
= rt
& MIPSDSP_LLO
;
2585 tempB
[0] = mipsdsp_mul_q31_q31(ac
, rs1
, rt1
, env
);
2586 tempA
[0] = mipsdsp_mul_q31_q31(ac
, rs0
, rt0
, env
);
2588 if (tempB
[0] >= 0) {
2594 if (tempA
[0] >= 0) {
2600 acc
[0] = env
->active_tc
.LO
[ac
];
2601 acc
[1] = env
->active_tc
.HI
[ac
];
2603 temp_sum
= tempB
[0] - tempA
[0];
2604 if ((uint64_t)temp_sum
> (uint64_t)tempB
[0]) {
2608 temp
[1] = tempB
[1] - tempA
[1];
2610 if ((temp
[1] & 0x01) == 0) {
2616 temp_sum
= acc
[0] + temp
[0];
2617 if (((uint64_t)temp_sum
< (uint64_t)acc
[0]) &&
2618 ((uint64_t)temp_sum
< (uint64_t)temp
[0])) {
2624 env
->active_tc
.HI
[ac
] = acc
[1];
2625 env
->active_tc
.LO
[ac
] = acc
[0];
2629 #define MAQ_S_W(name, mov) \
2630 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2631 CPUMIPSState *env) \
2635 int64_t tempL, acc; \
2637 rsh = (rs >> mov) & MIPSDSP_LO; \
2638 rth = (rt >> mov) & MIPSDSP_LO; \
2639 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2640 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2641 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2642 tempL = (int64_t)tempA + acc; \
2643 env->active_tc.HI[ac] = (target_long)(int32_t) \
2644 ((tempL & MIPSDSP_LHI) >> 32); \
2645 env->active_tc.LO[ac] = (target_long)(int32_t) \
2646 (tempL & MIPSDSP_LLO); \
2649 MAQ_S_W(maq_s_w_phl
, 16);
2650 MAQ_S_W(maq_s_w_phr
, 0);
2654 #define MAQ_SA_W(name, mov) \
2655 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2656 CPUMIPSState *env) \
2661 rsh = (rs >> mov) & MIPSDSP_LO; \
2662 rth = (rt >> mov) & MIPSDSP_LO; \
2663 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2664 tempA = mipsdsp_sat32_acc_q31(ac, tempA, env); \
2666 env->active_tc.HI[ac] = (target_long)(int32_t)(((int64_t)tempA & \
2667 MIPSDSP_LHI) >> 32); \
2668 env->active_tc.LO[ac] = (target_long)(int32_t)((int64_t)tempA & \
2672 MAQ_SA_W(maq_sa_w_phl
, 16);
2673 MAQ_SA_W(maq_sa_w_phr
, 0);
2677 #define MULQ_W(name, addvar) \
2678 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2679 CPUMIPSState *env) \
2681 int32_t rs_t, rt_t; \
2685 rs_t = rs & MIPSDSP_LLO; \
2686 rt_t = rt & MIPSDSP_LLO; \
2688 if ((rs_t == 0x80000000) && (rt_t == 0x80000000)) { \
2689 tempL = 0x7FFFFFFF00000000ull; \
2690 set_DSPControl_overflow_flag(1, 21, env); \
2692 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2695 tempI = (tempL & MIPSDSP_LHI) >> 32; \
2697 return (target_long)(int32_t)tempI; \
2700 MULQ_W(mulq_s_w
, 0);
2701 MULQ_W(mulq_rs_w
, 0x80000000ull
);
2705 #if defined(TARGET_MIPS64)
2707 #define MAQ_S_W_QH(name, mov) \
2708 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2709 CPUMIPSState *env) \
2711 int16_t rs_t, rt_t; \
2720 rs_t = (rs >> mov) & MIPSDSP_LO; \
2721 rt_t = (rt >> mov) & MIPSDSP_LO; \
2722 temp_mul = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2724 temp[0] = (int64_t)temp_mul; \
2725 if (temp[0] >= 0) { \
2731 acc[0] = env->active_tc.LO[ac]; \
2732 acc[1] = env->active_tc.HI[ac]; \
2734 temp_sum = acc[0] + temp[0]; \
2735 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2736 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2739 acc[0] = temp_sum; \
2740 acc[1] += temp[1]; \
2742 env->active_tc.HI[ac] = acc[1]; \
2743 env->active_tc.LO[ac] = acc[0]; \
2746 MAQ_S_W_QH(maq_s_w_qhll
, 48);
2747 MAQ_S_W_QH(maq_s_w_qhlr
, 32);
2748 MAQ_S_W_QH(maq_s_w_qhrl
, 16);
2749 MAQ_S_W_QH(maq_s_w_qhrr
, 0);
2753 #define MAQ_SA_W(name, mov) \
2754 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2755 CPUMIPSState *env) \
2757 int16_t rs_t, rt_t; \
2761 rs_t = (rs >> mov) & MIPSDSP_LO; \
2762 rt_t = (rt >> mov) & MIPSDSP_LO; \
2763 temp = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2764 temp = mipsdsp_sat32_acc_q31(ac, temp, env); \
2766 acc[0] = (int64_t)(int32_t)temp; \
2767 if (acc[0] >= 0) { \
2773 env->active_tc.HI[ac] = acc[1]; \
2774 env->active_tc.LO[ac] = acc[0]; \
2777 MAQ_SA_W(maq_sa_w_qhll
, 48);
2778 MAQ_SA_W(maq_sa_w_qhlr
, 32);
2779 MAQ_SA_W(maq_sa_w_qhrl
, 16);
2780 MAQ_SA_W(maq_sa_w_qhrr
, 0);
2784 #define MAQ_S_L_PW(name, mov) \
2785 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2786 CPUMIPSState *env) \
2788 int32_t rs_t, rt_t; \
2796 rs_t = (rs >> mov) & MIPSDSP_LLO; \
2797 rt_t = (rt >> mov) & MIPSDSP_LLO; \
2799 temp[0] = mipsdsp_mul_q31_q31(ac, rs_t, rt_t, env); \
2800 if (temp[0] >= 0) { \
2806 acc[0] = env->active_tc.LO[ac]; \
2807 acc[1] = env->active_tc.HI[ac]; \
2809 temp_sum = acc[0] + temp[0]; \
2810 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2811 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2814 acc[0] = temp_sum; \
2815 acc[1] += temp[1]; \
2817 env->active_tc.HI[ac] = acc[1]; \
2818 env->active_tc.LO[ac] = acc[0]; \
2821 MAQ_S_L_PW(maq_s_l_pwl
, 32);
2822 MAQ_S_L_PW(maq_s_l_pwr
, 0);
2826 #define DM_OPERATE(name, func, is_add, sigext) \
2827 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2828 CPUMIPSState *env) \
2832 int64_t tempBL[2], tempAL[2]; \
2840 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2841 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2844 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2845 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2847 if (tempBL[0] >= 0) { \
2850 tempBL[1] = ~0ull; \
2853 if (tempAL[0] >= 0) { \
2856 tempAL[1] = ~0ull; \
2859 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2860 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2865 acc[1] = env->active_tc.HI[ac]; \
2866 acc[0] = env->active_tc.LO[ac]; \
2868 temp_sum = tempBL[0] + tempAL[0]; \
2869 if (((uint64_t)temp_sum < (uint64_t)tempBL[0]) && \
2870 ((uint64_t)temp_sum < (uint64_t)tempAL[0])) { \
2873 temp[0] = temp_sum; \
2874 temp[1] += tempBL[1] + tempAL[1]; \
2877 temp_sum = acc[0] + temp[0]; \
2878 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2879 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2882 temp[0] = temp_sum; \
2883 temp[1] = acc[1] + temp[1]; \
2885 temp_sum = acc[0] - temp[0]; \
2886 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2889 temp[0] = temp_sum; \
2890 temp[1] = acc[1] - temp[1]; \
2893 env->active_tc.HI[ac] = temp[1]; \
2894 env->active_tc.LO[ac] = temp[0]; \
2897 DM_OPERATE(dmadd
, mul_i32_i32
, 1, 1);
2898 DM_OPERATE(dmaddu
, mul_u32_u32
, 1, 0);
2899 DM_OPERATE(dmsub
, mul_i32_i32
, 0, 1);
2900 DM_OPERATE(dmsubu
, mul_u32_u32
, 0, 0);
2904 /** DSP Bit/Manipulation Sub-class insns **/
2905 target_ulong
helper_bitrev(target_ulong rt
)
2911 temp
= rt
& MIPSDSP_LO
;
2913 for (i
= 0; i
< 16; i
++) {
2914 rd
= (rd
<< 1) | (temp
& 1);
2918 return (target_ulong
)rd
;
2921 #define BIT_INSV(name, posfilter, sizefilter, ret_type) \
2922 target_ulong helper_##name(CPUMIPSState *env, target_ulong rs, \
2925 uint32_t pos, size, msb, lsb; \
2926 target_ulong filter; \
2927 target_ulong temp, temprs, temprt; \
2928 target_ulong dspc; \
2930 dspc = env->active_tc.DSPControl; \
2932 pos = dspc & posfilter; \
2933 size = (dspc >> 7) & sizefilter; \
2935 msb = pos + size - 1; \
2938 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2942 filter = ((int32_t)0x01 << size) - 1; \
2943 filter = filter << pos; \
2944 temprs = (rs << pos) & filter; \
2945 temprt = rt & ~filter; \
2946 temp = temprs | temprt; \
2948 return (target_long)(ret_type)temp; \
2951 BIT_INSV(insv
, 0x1F, 0x1F, int32_t);
2952 #ifdef TARGET_MIPS64
2953 BIT_INSV(dinsv
, 0x7F, 0x3F, target_long
);
2959 /** DSP Compare-Pick Sub-class insns **/
2960 #define CMP_HAS_RET(name, func, split_num, filter, bit_size) \
2961 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
2963 uint32_t rs_t, rt_t; \
2965 uint32_t temp = 0; \
2968 for (i = 0; i < split_num; i++) { \
2969 rs_t = (rs >> (bit_size * i)) & filter; \
2970 rt_t = (rt >> (bit_size * i)) & filter; \
2971 cc = mipsdsp_##func(rs_t, rt_t); \
2975 return (target_ulong)temp; \
2978 CMP_HAS_RET(cmpgu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
2979 CMP_HAS_RET(cmpgu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
2980 CMP_HAS_RET(cmpgu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
2982 #ifdef TARGET_MIPS64
2983 CMP_HAS_RET(cmpgu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
2984 CMP_HAS_RET(cmpgu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
2985 CMP_HAS_RET(cmpgu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
2991 #define CMP_NO_RET(name, func, split_num, filter, bit_size) \
2992 void helper_##name(target_ulong rs, target_ulong rt, \
2993 CPUMIPSState *env) \
2995 int##bit_size##_t rs_t, rt_t; \
2996 int##bit_size##_t flag = 0; \
2997 int##bit_size##_t cc; \
3000 for (i = 0; i < split_num; i++) { \
3001 rs_t = (rs >> (bit_size * i)) & filter; \
3002 rt_t = (rt >> (bit_size * i)) & filter; \
3004 cc = mipsdsp_##func((int32_t)rs_t, (int32_t)rt_t); \
3008 set_DSPControl_24(flag, split_num, env); \
3011 CMP_NO_RET(cmpu_eq_qb
, cmpu_eq
, 4, MIPSDSP_Q0
, 8);
3012 CMP_NO_RET(cmpu_lt_qb
, cmpu_lt
, 4, MIPSDSP_Q0
, 8);
3013 CMP_NO_RET(cmpu_le_qb
, cmpu_le
, 4, MIPSDSP_Q0
, 8);
3015 CMP_NO_RET(cmp_eq_ph
, cmp_eq
, 2, MIPSDSP_LO
, 16);
3016 CMP_NO_RET(cmp_lt_ph
, cmp_lt
, 2, MIPSDSP_LO
, 16);
3017 CMP_NO_RET(cmp_le_ph
, cmp_le
, 2, MIPSDSP_LO
, 16);
3019 #ifdef TARGET_MIPS64
3020 CMP_NO_RET(cmpu_eq_ob
, cmpu_eq
, 8, MIPSDSP_Q0
, 8);
3021 CMP_NO_RET(cmpu_lt_ob
, cmpu_lt
, 8, MIPSDSP_Q0
, 8);
3022 CMP_NO_RET(cmpu_le_ob
, cmpu_le
, 8, MIPSDSP_Q0
, 8);
3024 CMP_NO_RET(cmp_eq_qh
, cmp_eq
, 4, MIPSDSP_LO
, 16);
3025 CMP_NO_RET(cmp_lt_qh
, cmp_lt
, 4, MIPSDSP_LO
, 16);
3026 CMP_NO_RET(cmp_le_qh
, cmp_le
, 4, MIPSDSP_LO
, 16);
3028 CMP_NO_RET(cmp_eq_pw
, cmp_eq
, 2, MIPSDSP_LLO
, 32);
3029 CMP_NO_RET(cmp_lt_pw
, cmp_lt
, 2, MIPSDSP_LLO
, 32);
3030 CMP_NO_RET(cmp_le_pw
, cmp_le
, 2, MIPSDSP_LLO
, 32);
3034 #if defined(TARGET_MIPS64)
3036 #define CMPGDU_OB(name) \
3037 target_ulong helper_cmpgdu_##name##_ob(target_ulong rs, target_ulong rt, \
3038 CPUMIPSState *env) \
3041 uint8_t rs_t, rt_t; \
3046 for (i = 0; i < 8; i++) { \
3047 rs_t = (rs >> (8 * i)) & MIPSDSP_Q0; \
3048 rt_t = (rt >> (8 * i)) & MIPSDSP_Q0; \
3050 if (mipsdsp_cmpu_##name(rs_t, rt_t)) { \
3051 cond |= 0x01 << i; \
3055 set_DSPControl_24(cond, 8, env); \
3057 return (uint64_t)cond; \
3066 #define PICK_INSN(name, split_num, filter, bit_size, ret32bit) \
3067 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
3068 CPUMIPSState *env) \
3070 uint32_t rs_t, rt_t; \
3074 target_ulong result = 0; \
3076 dsp = env->active_tc.DSPControl; \
3077 for (i = 0; i < split_num; i++) { \
3078 rs_t = (rs >> (bit_size * i)) & filter; \
3079 rt_t = (rt >> (bit_size * i)) & filter; \
3080 cc = (dsp >> (24 + i)) & 0x01; \
3081 cc = cc == 1 ? rs_t : rt_t; \
3083 result |= (target_ulong)cc << (bit_size * i); \
3087 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3093 PICK_INSN(pick_qb
, 4, MIPSDSP_Q0
, 8, 1);
3094 PICK_INSN(pick_ph
, 2, MIPSDSP_LO
, 16, 1);
3096 #ifdef TARGET_MIPS64
3097 PICK_INSN(pick_ob
, 8, MIPSDSP_Q0
, 8, 0);
3098 PICK_INSN(pick_qh
, 4, MIPSDSP_LO
, 16, 0);
3099 PICK_INSN(pick_pw
, 2, MIPSDSP_LLO
, 32, 0);
3103 target_ulong
helper_packrl_ph(target_ulong rs
, target_ulong rt
)
3107 rsl
= rs
& MIPSDSP_LO
;
3108 rth
= (rt
& MIPSDSP_HI
) >> 16;
3110 return (target_long
)(int32_t)((rsl
<< 16) | rth
);
3113 #if defined(TARGET_MIPS64)
3114 target_ulong
helper_packrl_pw(target_ulong rs
, target_ulong rt
)
3118 rs0
= rs
& MIPSDSP_LLO
;
3119 rt1
= (rt
>> 32) & MIPSDSP_LLO
;
3121 return ((uint64_t)rs0
<< 32) | (uint64_t)rt1
;
3125 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3126 target_ulong
helper_extr_w(target_ulong ac
, target_ulong shift
,
3132 shift
= shift
& 0x1F;
3134 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3135 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3136 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3137 set_DSPControl_overflow_flag(1, 23, env
);
3140 tempI
= (tempDL
[0] >> 1) & MIPSDSP_LLO
;
3143 if (tempDL
[0] == 0) {
3147 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3148 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3149 set_DSPControl_overflow_flag(1, 23, env
);
3152 return (target_long
)tempI
;
3155 target_ulong
helper_extr_r_w(target_ulong ac
, target_ulong shift
,
3160 shift
= shift
& 0x1F;
3162 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3163 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3164 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3165 set_DSPControl_overflow_flag(1, 23, env
);
3169 if (tempDL
[0] == 0) {
3173 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3174 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3175 set_DSPControl_overflow_flag(1, 23, env
);
3178 return (target_long
)(int32_t)(tempDL
[0] >> 1);
3181 target_ulong
helper_extr_rs_w(target_ulong ac
, target_ulong shift
,
3184 int32_t tempI
, temp64
;
3187 shift
= shift
& 0x1F;
3189 mipsdsp_rndrashift_short_acc(tempDL
, ac
, shift
, env
);
3190 if ((tempDL
[1] != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3191 (tempDL
[1] != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3192 set_DSPControl_overflow_flag(1, 23, env
);
3195 if (tempDL
[0] == 0) {
3198 tempI
= tempDL
[0] >> 1;
3200 if (((tempDL
[1] & 0x01) != 0 || (tempDL
[0] & MIPSDSP_LHI
) != 0) &&
3201 ((tempDL
[1] & 0x01) != 1 || (tempDL
[0] & MIPSDSP_LHI
) != MIPSDSP_LHI
)) {
3202 temp64
= tempDL
[1] & 0x01;
3208 set_DSPControl_overflow_flag(1, 23, env
);
3211 return (target_long
)tempI
;
3214 #if defined(TARGET_MIPS64)
3215 target_ulong
helper_dextr_w(target_ulong ac
, target_ulong shift
,
3220 shift
= shift
& 0x3F;
3222 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3224 return (int64_t)(int32_t)(temp
[0] >> 1);
3227 target_ulong
helper_dextr_r_w(target_ulong ac
, target_ulong shift
,
3233 shift
= shift
& 0x3F;
3234 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3244 temp128
= temp
[2] & 0x01;
3246 if ((temp128
!= 0 || temp
[1] != 0) &&
3247 (temp128
!= 1 || temp
[1] != ~0ull)) {
3248 set_DSPControl_overflow_flag(1, 23, env
);
3251 return (int64_t)(int32_t)(temp
[0] >> 1);
3254 target_ulong
helper_dextr_rs_w(target_ulong ac
, target_ulong shift
,
3260 shift
= shift
& 0x3F;
3261 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3271 temp128
= temp
[2] & 0x01;
3273 if ((temp128
!= 0 || temp
[1] != 0) &&
3274 (temp128
!= 1 || temp
[1] != ~0ull)) {
3276 temp
[0] = 0x0FFFFFFFF;
3278 temp
[0] = 0x0100000000ULL
;
3280 set_DSPControl_overflow_flag(1, 23, env
);
3283 return (int64_t)(int32_t)(temp
[0] >> 1);
3286 target_ulong
helper_dextr_l(target_ulong ac
, target_ulong shift
,
3290 target_ulong result
;
3292 shift
= shift
& 0x3F;
3294 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3295 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3300 target_ulong
helper_dextr_r_l(target_ulong ac
, target_ulong shift
,
3305 target_ulong result
;
3307 shift
= shift
& 0x3F;
3308 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3318 temp128
= temp
[2] & 0x01;
3320 if ((temp128
!= 0 || temp
[1] != 0) &&
3321 (temp128
!= 1 || temp
[1] != ~0ull)) {
3322 set_DSPControl_overflow_flag(1, 23, env
);
3325 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3330 target_ulong
helper_dextr_rs_l(target_ulong ac
, target_ulong shift
,
3335 target_ulong result
;
3337 shift
= shift
& 0x3F;
3338 mipsdsp_rndrashift_acc(temp
, ac
, shift
, env
);
3348 temp128
= temp
[2] & 0x01;
3350 if ((temp128
!= 0 || temp
[1] != 0) &&
3351 (temp128
!= 1 || temp
[1] != ~0ull)) {
3353 temp
[1] &= ~0x00ull
- 1;
3354 temp
[0] |= ~0x00ull
- 1;
3359 set_DSPControl_overflow_flag(1, 23, env
);
3361 result
= (temp
[1] << 63) | (temp
[0] >> 1);
3367 target_ulong
helper_extr_s_h(target_ulong ac
, target_ulong shift
,
3372 shift
= shift
& 0x1F;
3374 acc
= ((int64_t)env
->active_tc
.HI
[ac
] << 32) |
3375 ((int64_t)env
->active_tc
.LO
[ac
] & 0xFFFFFFFF);
3377 temp
= acc
>> shift
;
3379 if (temp
> (int64_t)0x7FFF) {
3381 set_DSPControl_overflow_flag(1, 23, env
);
3382 } else if (temp
< (int64_t)0xFFFFFFFFFFFF8000ULL
) {
3384 set_DSPControl_overflow_flag(1, 23, env
);
3387 return (target_long
)(int32_t)(temp
& 0xFFFFFFFF);
3391 #if defined(TARGET_MIPS64)
3392 target_ulong
helper_dextr_s_h(target_ulong ac
, target_ulong shift
,
3398 shift
= shift
& 0x1F;
3400 mipsdsp_rashift_acc((uint64_t *)temp
, ac
, shift
, env
);
3402 temp127
= (temp
[1] >> 63) & 0x01;
3404 if ((temp127
== 0) && (temp
[1] > 0 || temp
[0] > 32767)) {
3405 temp
[0] &= 0xFFFF0000;
3406 temp
[0] |= 0x00007FFF;
3407 set_DSPControl_overflow_flag(1, 23, env
);
3408 } else if ((temp127
== 1) &&
3409 (temp
[1] < 0xFFFFFFFFFFFFFFFFll
3410 || temp
[0] < 0xFFFFFFFFFFFF1000ll
)) {
3411 temp
[0] &= 0xFFFF0000;
3412 temp
[0] |= 0x00008000;
3413 set_DSPControl_overflow_flag(1, 23, env
);
3416 return (int64_t)(int16_t)(temp
[0] & MIPSDSP_LO
);
3421 target_ulong
helper_extp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3431 start_pos
= get_DSPControl_pos(env
);
3432 sub
= start_pos
- (size
+ 1);
3434 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3435 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3436 temp
= (acc
>> (start_pos
- size
)) &
3437 (((uint32_t)0x01 << (size
+ 1)) - 1);
3438 set_DSPControl_efi(0, env
);
3440 set_DSPControl_efi(1, env
);
3443 return (target_ulong
)temp
;
3446 target_ulong
helper_extpdp(target_ulong ac
, target_ulong size
,
3456 start_pos
= get_DSPControl_pos(env
);
3457 sub
= start_pos
- (size
+ 1);
3459 acc
= ((uint64_t)env
->active_tc
.HI
[ac
] << 32) |
3460 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3461 temp
= (acc
>> (start_pos
- size
)) &
3462 (((uint32_t)0x01 << (size
+ 1)) - 1);
3464 set_DSPControl_pos(start_pos
- (size
+ 1), env
);
3465 set_DSPControl_efi(0, env
);
3467 set_DSPControl_efi(1, env
);
3470 return (target_ulong
)temp
;
3474 #if defined(TARGET_MIPS64)
3475 target_ulong
helper_dextp(target_ulong ac
, target_ulong size
, CPUMIPSState
*env
)
3480 uint64_t tempB
, tempA
;
3486 start_pos
= get_DSPControl_pos(env
);
3487 len
= start_pos
- size
;
3488 tempB
= env
->active_tc
.HI
[ac
];
3489 tempA
= env
->active_tc
.LO
[ac
];
3491 sub
= start_pos
- (size
+ 1);
3494 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3495 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3496 set_DSPControl_efi(0, env
);
3498 set_DSPControl_efi(1, env
);
3504 target_ulong
helper_dextpdp(target_ulong ac
, target_ulong size
,
3510 uint64_t tempB
, tempA
;
3515 start_pos
= get_DSPControl_pos(env
);
3516 len
= start_pos
- size
;
3517 tempB
= env
->active_tc
.HI
[ac
];
3518 tempA
= env
->active_tc
.LO
[ac
];
3520 sub
= start_pos
- (size
+ 1);
3523 temp
= (tempB
<< (64 - len
)) | (tempA
>> len
);
3524 temp
= temp
& ((0x01 << (size
+ 1)) - 1);
3525 set_DSPControl_pos(sub
, env
);
3526 set_DSPControl_efi(0, env
);
3528 set_DSPControl_efi(1, env
);
3536 void helper_shilo(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3542 rs5_0
= (int8_t)(rs5_0
<< 2) >> 2;
3544 if (unlikely(rs5_0
== 0)) {
3548 acc
= (((uint64_t)env
->active_tc
.HI
[ac
] << 32) & MIPSDSP_LHI
) |
3549 ((uint64_t)env
->active_tc
.LO
[ac
] & MIPSDSP_LLO
);
3552 temp
= acc
>> rs5_0
;
3554 temp
= acc
<< -rs5_0
;
3557 env
->active_tc
.HI
[ac
] = (target_ulong
)(int32_t)((temp
& MIPSDSP_LHI
) >> 32);
3558 env
->active_tc
.LO
[ac
] = (target_ulong
)(int32_t)(temp
& MIPSDSP_LLO
);
3561 #if defined(TARGET_MIPS64)
3562 void helper_dshilo(target_ulong shift
, target_ulong ac
, CPUMIPSState
*env
)
3565 uint64_t tempB
, tempA
;
3567 shift_t
= (int8_t)(shift
<< 1) >> 1;
3569 tempB
= env
->active_tc
.HI
[ac
];
3570 tempA
= env
->active_tc
.LO
[ac
];
3574 tempA
= (tempB
<< (64 - shift_t
)) | (tempA
>> shift_t
);
3575 tempB
= tempB
>> shift_t
;
3578 tempB
= (tempB
<< shift_t
) | (tempA
>> (64 - shift_t
));
3579 tempA
= tempA
<< shift_t
;
3583 env
->active_tc
.HI
[ac
] = tempB
;
3584 env
->active_tc
.LO
[ac
] = tempA
;
3588 void helper_mthlip(target_ulong ac
, target_ulong rs
, CPUMIPSState
*env
)
3590 int32_t tempA
, tempB
, pos
;
3593 tempB
= env
->active_tc
.LO
[ac
];
3594 env
->active_tc
.HI
[ac
] = (target_long
)tempB
;
3595 env
->active_tc
.LO
[ac
] = (target_long
)tempA
;
3596 pos
= get_DSPControl_pos(env
);
3601 set_DSPControl_pos(pos
+ 32, env
);
3605 #if defined(TARGET_MIPS64)
3606 void helper_dmthlip(target_ulong rs
, target_ulong ac
, CPUMIPSState
*env
)
3610 uint64_t tempB
, tempA
;
3615 tempB
= env
->active_tc
.LO
[ac_t
];
3617 env
->active_tc
.HI
[ac_t
] = tempB
;
3618 env
->active_tc
.LO
[ac_t
] = tempA
;
3620 pos
= get_DSPControl_pos(env
);
3624 set_DSPControl_pos(pos
, env
);
3629 void cpu_wrdsp(uint32_t rs
, uint32_t mask_num
, CPUMIPSState
*env
)
3633 uint32_t newbits
, overwrite
;
3637 overwrite
= 0xFFFFFFFF;
3638 dsp
= env
->active_tc
.DSPControl
;
3640 for (i
= 0; i
< 6; i
++) {
3641 mask
[i
] = (mask_num
>> i
) & 0x01;
3645 #if defined(TARGET_MIPS64)
3646 overwrite
&= 0xFFFFFF80;
3647 newbits
&= 0xFFFFFF80;
3648 newbits
|= 0x0000007F & rs
;
3650 overwrite
&= 0xFFFFFFC0;
3651 newbits
&= 0xFFFFFFC0;
3652 newbits
|= 0x0000003F & rs
;
3657 overwrite
&= 0xFFFFE07F;
3658 newbits
&= 0xFFFFE07F;
3659 newbits
|= 0x00001F80 & rs
;
3663 overwrite
&= 0xFFFFDFFF;
3664 newbits
&= 0xFFFFDFFF;
3665 newbits
|= 0x00002000 & rs
;
3669 overwrite
&= 0xFF00FFFF;
3670 newbits
&= 0xFF00FFFF;
3671 newbits
|= 0x00FF0000 & rs
;
3675 overwrite
&= 0x00FFFFFF;
3676 newbits
&= 0x00FFFFFF;
3677 #if defined(TARGET_MIPS64)
3678 newbits
|= 0xFF000000 & rs
;
3680 newbits
|= 0x0F000000 & rs
;
3685 overwrite
&= 0xFFFFBFFF;
3686 newbits
&= 0xFFFFBFFF;
3687 newbits
|= 0x00004000 & rs
;
3690 dsp
= dsp
& overwrite
;
3691 dsp
= dsp
| newbits
;
3692 env
->active_tc
.DSPControl
= dsp
;
3695 void helper_wrdsp(target_ulong rs
, target_ulong mask_num
, CPUMIPSState
*env
)
3697 return cpu_wrdsp(rs
, mask_num
, env
);
3700 uint32_t cpu_rddsp(uint32_t mask_num
, CPUMIPSState
*env
)
3708 for (i
= 0; i
< 6; i
++) {
3709 mask
[i
] = (mask_num
& ruler
) >> i
;
3714 dsp
= env
->active_tc
.DSPControl
;
3717 #if defined(TARGET_MIPS64)
3725 temp
|= dsp
& 0x1F80;
3729 temp
|= dsp
& 0x2000;
3733 temp
|= dsp
& 0x00FF0000;
3737 #if defined(TARGET_MIPS64)
3738 temp
|= dsp
& 0xFF000000;
3740 temp
|= dsp
& 0x0F000000;
3745 temp
|= dsp
& 0x4000;
3751 target_ulong
helper_rddsp(target_ulong mask_num
, CPUMIPSState
*env
)
3753 return cpu_rddsp(mask_num
, env
);
3766 #undef MIPSDSP_SPLIT32_8
3767 #undef MIPSDSP_SPLIT32_16
3769 #undef MIPSDSP_RETURN32_8
3770 #undef MIPSDSP_RETURN32_16
3772 #ifdef TARGET_MIPS64
3773 #undef MIPSDSP_SPLIT64_16
3774 #undef MIPSDSP_SPLIT64_32
3775 #undef MIPSDSP_RETURN64_16
3776 #undef MIPSDSP_RETURN64_32