2 * MIPS ASE DSP Instruction emulation helpers for QEMU.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/helper-proto.h"
23 #include "qemu/bitops.h"
26 * As the byte ordering doesn't matter, i.e. all columns are treated
27 * identically, these unions can be used directly.
49 /*** MIPS DSP internal functions begin ***/
50 #define MIPSDSP_ABS(x) (((x) >= 0) ? (x) : -(x))
51 #define MIPSDSP_OVERFLOW_ADD(a, b, c, d) (~((a) ^ (b)) & ((a) ^ (c)) & (d))
52 #define MIPSDSP_OVERFLOW_SUB(a, b, c, d) (((a) ^ (b)) & ((a) ^ (c)) & (d))
54 static inline void set_DSPControl_overflow_flag(uint32_t flag, int position,
57 env->active_tc.DSPControl |= (target_ulong)flag << position;
60 static inline void set_DSPControl_carryflag(bool flag, CPUMIPSState *env)
62 env->active_tc.DSPControl &= ~(1 << 13);
63 env->active_tc.DSPControl |= flag << 13;
66 static inline uint32_t get_DSPControl_carryflag(CPUMIPSState *env)
68 return (env->active_tc.DSPControl >> 13) & 0x01;
71 static inline void set_DSPControl_24(uint32_t flag, int len, CPUMIPSState *env)
75 filter = ((0x01 << len) - 1) << 24;
78 env->active_tc.DSPControl &= filter;
79 env->active_tc.DSPControl |= (target_ulong)flag << 24;
82 static inline void set_DSPControl_pos(uint32_t pos, CPUMIPSState *env)
86 dspc = env->active_tc.DSPControl;
88 dspc = dspc & 0xFFFFFFC0;
91 dspc = dspc & 0xFFFFFF80;
94 env->active_tc.DSPControl = dspc;
97 static inline uint32_t get_DSPControl_pos(CPUMIPSState *env)
102 dspc = env->active_tc.DSPControl;
104 #ifndef TARGET_MIPS64
113 static inline void set_DSPControl_efi(uint32_t flag, CPUMIPSState *env)
115 env->active_tc.DSPControl &= 0xFFFFBFFF;
116 env->active_tc.DSPControl |= (target_ulong)flag << 14;
119 #define DO_MIPS_SAT_ABS(size) \
120 static inline int##size##_t mipsdsp_sat_abs##size(int##size##_t a, \
123 if (a == INT##size##_MIN) { \
124 set_DSPControl_overflow_flag(1, 20, env); \
125 return INT##size##_MAX; \
127 return MIPSDSP_ABS(a); \
133 #undef DO_MIPS_SAT_ABS
136 static inline int16_t mipsdsp_add_i16(int16_t a, int16_t b, CPUMIPSState *env)
142 if (MIPSDSP_OVERFLOW_ADD(a, b, tempI, 0x8000)) {
143 set_DSPControl_overflow_flag(1, 20, env);
149 static inline int16_t mipsdsp_sat_add_i16(int16_t a, int16_t b,
156 if (MIPSDSP_OVERFLOW_ADD(a, b, tempS, 0x8000)) {
162 set_DSPControl_overflow_flag(1, 20, env);
168 static inline int32_t mipsdsp_sat_add_i32(int32_t a, int32_t b,
175 if (MIPSDSP_OVERFLOW_ADD(a, b, tempI, 0x80000000)) {
181 set_DSPControl_overflow_flag(1, 20, env);
187 static inline uint8_t mipsdsp_add_u8(uint8_t a, uint8_t b, CPUMIPSState *env)
191 temp = (uint16_t)a + (uint16_t)b;
194 set_DSPControl_overflow_flag(1, 20, env);
200 static inline uint16_t mipsdsp_add_u16(uint16_t a, uint16_t b,
205 temp = (uint32_t)a + (uint32_t)b;
207 if (temp & 0x00010000) {
208 set_DSPControl_overflow_flag(1, 20, env);
211 return temp & 0xFFFF;
214 static inline uint8_t mipsdsp_sat_add_u8(uint8_t a, uint8_t b,
220 temp = (uint16_t)a + (uint16_t)b;
221 result = temp & 0xFF;
225 set_DSPControl_overflow_flag(1, 20, env);
231 static inline uint16_t mipsdsp_sat_add_u16(uint16_t a, uint16_t b,
237 temp = (uint32_t)a + (uint32_t)b;
238 result = temp & 0xFFFF;
240 if (0x00010000 & temp) {
242 set_DSPControl_overflow_flag(1, 20, env);
248 static inline int32_t mipsdsp_sat32_acc_q31(int32_t acc, int32_t a,
252 int32_t temp32, temp31, result;
255 #ifndef TARGET_MIPS64
256 temp = ((uint64_t)env->active_tc.HI[acc] << 32) |
257 (uint64_t)env->active_tc.LO[acc];
259 temp = (uint64_t)env->active_tc.LO[acc];
262 temp_sum = (int64_t)a + temp;
264 temp32 = (temp_sum >> 32) & 0x01;
265 temp31 = (temp_sum >> 31) & 0x01;
266 result = temp_sum & 0xFFFFFFFF;
268 if (temp32 != temp31) {
274 set_DSPControl_overflow_flag(1, 16 + acc, env);
281 /* a[0] is LO, a[1] is HI. */
282 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret,
289 ret[0] = env->active_tc.LO[ac] + a[0];
290 ret[1] = env->active_tc.HI[ac] + a[1];
292 if (((uint64_t)ret[0] < (uint64_t)env->active_tc.LO[ac]) &&
293 ((uint64_t)ret[0] < (uint64_t)a[0])) {
297 if (temp64 != ((ret[0] >> 63) & 0x01)) {
299 ret[0] = (0x01ull << 63);
302 ret[0] = (0x01ull << 63) - 1;
305 set_DSPControl_overflow_flag(1, 16 + ac, env);
309 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret,
316 ret[0] = env->active_tc.LO[ac] - a[0];
317 ret[1] = env->active_tc.HI[ac] - a[1];
319 if ((uint64_t)ret[0] > (uint64_t)env->active_tc.LO[ac]) {
323 if (temp64 != ((ret[0] >> 63) & 0x01)) {
325 ret[0] = (0x01ull << 63);
328 ret[0] = (0x01ull << 63) - 1;
331 set_DSPControl_overflow_flag(1, 16 + ac, env);
336 static inline int32_t mipsdsp_mul_i16_i16(int16_t a, int16_t b,
341 temp = (int32_t)a * (int32_t)b;
343 if ((temp > (int)0x7FFF) || (temp < (int)0xFFFF8000)) {
344 set_DSPControl_overflow_flag(1, 21, env);
351 static inline int32_t mipsdsp_mul_u16_u16(int32_t a, int32_t b)
357 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 = ((int16_t)a * (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)
419 static inline int32_t mipsdsp_rashift32(int32_t a, target_ulong mov)
425 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a, int16_t b)
429 temp = (int32_t)a + (int32_t)b;
431 return (temp >> 1) & 0xFFFF;
434 /* round right shift */
435 static inline int16_t mipsdsp_rrshift1_add_q16(int16_t a, int16_t b)
439 temp = (int32_t)a + (int32_t)b;
442 return (temp >> 1) & 0xFFFF;
445 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a, int32_t b)
449 temp = (int64_t)a + (int64_t)b;
451 return (temp >> 1) & 0xFFFFFFFF;
454 static inline int32_t mipsdsp_rrshift1_add_q32(int32_t a, int32_t b)
458 temp = (int64_t)a + (int64_t)b;
461 return (temp >> 1) & 0xFFFFFFFF;
464 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a, uint8_t b)
468 temp = (uint16_t)a + (uint16_t)b;
470 return (temp >> 1) & 0x00FF;
473 static inline uint8_t mipsdsp_rrshift1_add_u8(uint8_t a, uint8_t b)
477 temp = (uint16_t)a + (uint16_t)b + 1;
479 return (temp >> 1) & 0x00FF;
483 static inline uint8_t mipsdsp_rshift1_sub_u8(uint8_t a, uint8_t b)
487 temp = (uint16_t)a - (uint16_t)b;
489 return (temp >> 1) & 0x00FF;
492 static inline uint8_t mipsdsp_rrshift1_sub_u8(uint8_t a, uint8_t b)
496 temp = (uint16_t)a - (uint16_t)b + 1;
498 return (temp >> 1) & 0x00FF;
502 /* 128 bits long. p[0] is LO, p[1] is HI. */
503 static inline void mipsdsp_rndrashift_short_acc(int64_t *p,
510 acc = ((int64_t)env->active_tc.HI[ac] << 32) |
511 ((int64_t)env->active_tc.LO[ac] & 0xFFFFFFFF);
512 p[0] = (shift == 0) ? (acc << 1) : (acc >> (shift - 1));
513 p[1] = (acc >> 63) & 0x01;
517 /* 128 bits long. p[0] is LO, p[1] is HI */
518 static inline void mipsdsp_rashift_acc(uint64_t *p,
523 uint64_t tempB, tempA;
525 tempB = env->active_tc.HI[ac];
526 tempA = env->active_tc.LO[ac];
527 shift = shift & 0x1F;
533 p[0] = (tempB << (64 - shift)) | (tempA >> shift);
534 p[1] = (int64_t)tempB >> shift;
538 /* 128 bits long. p[0] is LO, p[1] is HI , p[2] is sign of HI.*/
539 static inline void mipsdsp_rndrashift_acc(uint64_t *p,
544 int64_t tempB, tempA;
546 tempB = env->active_tc.HI[ac];
547 tempA = env->active_tc.LO[ac];
548 shift = shift & 0x3F;
552 p[1] = (tempB << 1) | (tempA >> 63);
555 p[0] = (tempB << (65 - shift)) | (tempA >> (shift - 1));
556 p[1] = (int64_t)tempB >> (shift - 1);
566 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac, uint16_t a, uint16_t b,
571 if ((a == 0x8000) && (b == 0x8000)) {
573 set_DSPControl_overflow_flag(1, 16 + ac, env);
575 temp = ((int16_t)a * (int16_t)b) << 1;
581 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac, uint32_t a, uint32_t b,
586 if ((a == 0x80000000) && (b == 0x80000000)) {
587 temp = (0x01ull << 63) - 1;
588 set_DSPControl_overflow_flag(1, 16 + ac, env);
590 temp = ((int64_t)(int32_t)a * (int32_t)b) << 1;
596 static inline uint16_t mipsdsp_mul_u8_u8(uint8_t a, uint8_t b)
598 return (uint16_t)a * (uint16_t)b;
601 static inline uint16_t mipsdsp_mul_u8_u16(uint8_t a, uint16_t b,
606 tempI = (uint32_t)a * (uint32_t)b;
607 if (tempI > 0x0000FFFF) {
609 set_DSPControl_overflow_flag(1, 21, env);
612 return tempI & 0x0000FFFF;
616 static inline uint64_t mipsdsp_mul_u32_u32(uint32_t a, uint32_t b)
618 return (uint64_t)a * (uint64_t)b;
622 static inline int16_t mipsdsp_rndq15_mul_q15_q15(uint16_t a, uint16_t b,
627 if ((a == 0x8000) && (b == 0x8000)) {
629 set_DSPControl_overflow_flag(1, 21, env);
631 temp = ((int16_t)a * (int16_t)b) << 1;
632 temp = temp + 0x00008000;
635 return (temp & 0xFFFF0000) >> 16;
638 static inline int32_t mipsdsp_sat16_mul_q15_q15(uint16_t a, uint16_t b,
643 if ((a == 0x8000) && (b == 0x8000)) {
645 set_DSPControl_overflow_flag(1, 21, env);
647 temp = (int16_t)a * (int16_t)b;
651 return (temp >> 16) & 0x0000FFFF;
654 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a,
661 * The value 0x00008000 will be added to the input Q31 value, and the code
662 * needs to check if the addition causes an overflow. Since a positive value
663 * is added, overflow can happen in one direction only.
665 if (a > 0x7FFF7FFF) {
667 set_DSPControl_overflow_flag(1, 22, env);
669 temp = ((a + 0x8000) >> 16) & 0xFFFF;
675 static inline uint8_t mipsdsp_sat8_reduce_precision(uint16_t a,
681 sign = (a >> 15) & 0x01;
686 set_DSPControl_overflow_flag(1, 22, env);
689 return (mag >> 7) & 0xFFFF;
692 set_DSPControl_overflow_flag(1, 22, env);
697 static inline uint8_t mipsdsp_lshift8(uint8_t a, uint8_t s, CPUMIPSState *env)
702 discard = a >> (8 - s);
704 if (discard != 0x00) {
705 set_DSPControl_overflow_flag(1, 22, env);
711 static inline uint16_t mipsdsp_lshift16(uint16_t a, uint8_t s,
717 discard = (int16_t)a >> (15 - s);
719 if ((discard != 0x0000) && (discard != 0xFFFF)) {
720 set_DSPControl_overflow_flag(1, 22, env);
727 static inline uint32_t mipsdsp_lshift32(uint32_t a, uint8_t s,
735 discard = (int32_t)a >> (31 - (s - 1));
737 if ((discard != 0x00000000) && (discard != 0xFFFFFFFF)) {
738 set_DSPControl_overflow_flag(1, 22, env);
745 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a, uint8_t s,
754 sign = (a >> 15) & 0x01;
756 discard = (((0x01 << (16 - s)) - 1) << s) |
757 ((a >> (14 - (s - 1))) & ((0x01 << s) - 1));
759 discard = a >> (14 - (s - 1));
762 if ((discard != 0x0000) && (discard != 0xFFFF)) {
763 set_DSPControl_overflow_flag(1, 22, env);
764 return (sign == 0) ? 0x7FFF : 0x8000;
771 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a, uint8_t s,
780 sign = (a >> 31) & 0x01;
782 discard = (((0x01 << (32 - s)) - 1) << s) |
783 ((a >> (30 - (s - 1))) & ((0x01 << s) - 1));
785 discard = a >> (30 - (s - 1));
788 if ((discard != 0x00000000) && (discard != 0xFFFFFFFF)) {
789 set_DSPControl_overflow_flag(1, 22, env);
790 return (sign == 0) ? 0x7FFFFFFF : 0x80000000;
797 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a, uint8_t s)
802 temp = (uint32_t)a << 1;
804 temp = (int32_t)(int8_t)a >> (s - 1);
807 return (temp + 1) >> 1;
810 static inline uint16_t mipsdsp_rnd16_rashift(uint16_t a, uint8_t s)
815 temp = (uint32_t)a << 1;
817 temp = (int32_t)(int16_t)a >> (s - 1);
820 return (temp + 1) >> 1;
823 static inline uint32_t mipsdsp_rnd32_rashift(uint32_t a, uint8_t s)
828 temp = (uint64_t)a << 1;
830 temp = (int64_t)(int32_t)a >> (s - 1);
834 return (temp >> 1) & 0xFFFFFFFFull;
837 static inline uint16_t mipsdsp_sub_i16(int16_t a, int16_t b, CPUMIPSState *env)
842 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x8000)) {
843 set_DSPControl_overflow_flag(1, 20, env);
849 static inline uint16_t mipsdsp_sat16_sub(int16_t a, int16_t b,
855 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x8000)) {
861 set_DSPControl_overflow_flag(1, 20, env);
867 static inline uint32_t mipsdsp_sat32_sub(int32_t a, int32_t b,
873 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x80000000)) {
879 set_DSPControl_overflow_flag(1, 20, env);
882 return temp & 0xFFFFFFFFull;
885 static inline uint16_t mipsdsp_rshift1_sub_q16(int16_t a, int16_t b)
889 temp = (int32_t)a - (int32_t)b;
891 return (temp >> 1) & 0x0000FFFF;
894 static inline uint16_t mipsdsp_rrshift1_sub_q16(int16_t a, int16_t b)
898 temp = (int32_t)a - (int32_t)b;
901 return (temp >> 1) & 0x0000FFFF;
904 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a, int32_t b)
908 temp = (int64_t)a - (int64_t)b;
910 return (temp >> 1) & 0xFFFFFFFFull;
913 static inline uint32_t mipsdsp_rrshift1_sub_q32(int32_t a, int32_t b)
917 temp = (int64_t)a - (int64_t)b;
920 return (temp >> 1) & 0xFFFFFFFFull;
923 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a, uint16_t b,
929 temp = (uint32_t)a - (uint32_t)b;
930 temp16 = (temp >> 16) & 0x01;
932 set_DSPControl_overflow_flag(1, 20, env);
934 return temp & 0x0000FFFF;
937 static inline uint16_t mipsdsp_satu16_sub_u16_u16(uint16_t a, uint16_t b,
943 temp = (uint32_t)a - (uint32_t)b;
944 temp16 = (temp >> 16) & 0x01;
948 set_DSPControl_overflow_flag(1, 20, env);
951 return temp & 0x0000FFFF;
954 static inline uint8_t mipsdsp_sub_u8(uint8_t a, uint8_t b, CPUMIPSState *env)
959 temp = (uint16_t)a - (uint16_t)b;
960 temp8 = (temp >> 8) & 0x01;
962 set_DSPControl_overflow_flag(1, 20, env);
965 return temp & 0x00FF;
968 static inline uint8_t mipsdsp_satu8_sub(uint8_t a, uint8_t b, CPUMIPSState *env)
973 temp = (uint16_t)a - (uint16_t)b;
974 temp8 = (temp >> 8) & 0x01;
977 set_DSPControl_overflow_flag(1, 20, env);
980 return temp & 0x00FF;
984 static inline uint32_t mipsdsp_sub32(int32_t a, int32_t b, CPUMIPSState *env)
989 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x80000000)) {
990 set_DSPControl_overflow_flag(1, 20, env);
996 static inline int32_t mipsdsp_add_i32(int32_t a, int32_t b, CPUMIPSState *env)
1002 if (MIPSDSP_OVERFLOW_ADD(a, b, temp, 0x80000000)) {
1003 set_DSPControl_overflow_flag(1, 20, env);
1010 static inline int32_t mipsdsp_cmp_eq(int32_t a, int32_t b)
1015 static inline int32_t mipsdsp_cmp_le(int32_t a, int32_t b)
1020 static inline int32_t mipsdsp_cmp_lt(int32_t a, int32_t b)
1025 static inline int32_t mipsdsp_cmpu_eq(uint32_t a, uint32_t b)
1030 static inline int32_t mipsdsp_cmpu_le(uint32_t a, uint32_t b)
1035 static inline int32_t mipsdsp_cmpu_lt(uint32_t a, uint32_t b)
1039 /*** MIPS DSP internal functions end ***/
1041 #define MIPSDSP_LHI 0xFFFFFFFF00000000ull
1042 #define MIPSDSP_LLO 0x00000000FFFFFFFFull
1043 #define MIPSDSP_HI 0xFFFF0000
1044 #define MIPSDSP_LO 0x0000FFFF
1045 #define MIPSDSP_Q3 0xFF000000
1046 #define MIPSDSP_Q2 0x00FF0000
1047 #define MIPSDSP_Q1 0x0000FF00
1048 #define MIPSDSP_Q0 0x000000FF
1050 #define MIPSDSP_SPLIT32_8(num, a, b, c, d) \
1052 a = ((num) >> 24) & MIPSDSP_Q0; \
1053 b = ((num) >> 16) & MIPSDSP_Q0; \
1054 c = ((num) >> 8) & MIPSDSP_Q0; \
1055 d = (num) & MIPSDSP_Q0; \
1058 #define MIPSDSP_SPLIT32_16(num, a, b) \
1060 a = ((num) >> 16) & MIPSDSP_LO; \
1061 b = (num) & MIPSDSP_LO; \
1064 #define MIPSDSP_RETURN32_8(a, b, c, d) ((target_long)(int32_t) \
1065 (((uint32_t)(a) << 24) | \
1066 ((uint32_t)(b) << 16) | \
1067 ((uint32_t)(c) << 8) | \
1068 ((uint32_t)(d) & 0xFF)))
1069 #define MIPSDSP_RETURN32_16(a, b) ((target_long)(int32_t) \
1070 (((uint32_t)(a) << 16) | \
1071 ((uint32_t)(b) & 0xFFFF)))
1073 #ifdef TARGET_MIPS64
1074 #define MIPSDSP_SPLIT64_16(num, a, b, c, d) \
1076 a = ((num) >> 48) & MIPSDSP_LO; \
1077 b = ((num) >> 32) & MIPSDSP_LO; \
1078 c = ((num) >> 16) & MIPSDSP_LO; \
1079 d = (num) & MIPSDSP_LO; \
1082 #define MIPSDSP_SPLIT64_32(num, a, b) \
1084 a = ((num) >> 32) & MIPSDSP_LLO; \
1085 b = (num) & MIPSDSP_LLO; \
1088 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)(a) << 48) | \
1089 ((uint64_t)(b) << 32) | \
1090 ((uint64_t)(c) << 16) | \
1092 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)(a) << 32) | (uint64_t)(b))
1095 /** DSP Arithmetic Sub-class insns **/
1096 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1097 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1104 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1105 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1108 return (target_long)dt.sw[0]; \
1110 MIPSDSP32_UNOP_ENV(absq_s_ph, sat_abs16, sh)
1111 MIPSDSP32_UNOP_ENV(absq_s_qb, sat_abs8, sb)
1112 MIPSDSP32_UNOP_ENV(absq_s_w, sat_abs32, sw)
1113 #undef MIPSDSP32_UNOP_ENV
1115 #if defined(TARGET_MIPS64)
1116 #define MIPSDSP64_UNOP_ENV(name, func, element) \
1117 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1124 for (i = 0; i < ARRAY_SIZE(dt.element); i++) { \
1125 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1130 MIPSDSP64_UNOP_ENV(absq_s_ob, sat_abs8, sb)
1131 MIPSDSP64_UNOP_ENV(absq_s_qh, sat_abs16, sh)
1132 MIPSDSP64_UNOP_ENV(absq_s_pw, sat_abs32, sw)
1133 #undef MIPSDSP64_UNOP_ENV
1136 #define MIPSDSP32_BINOP(name, func, element) \
1137 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1139 DSP32Value ds, dt; \
1145 for (i = 0; i < ARRAY_SIZE(ds.element); i++) { \
1146 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1149 return (target_long)ds.sw[0]; \
1151 MIPSDSP32_BINOP(addqh_ph, rshift1_add_q16, sh);
1152 MIPSDSP32_BINOP(addqh_r_ph, rrshift1_add_q16, sh);
1153 MIPSDSP32_BINOP(addqh_r_w, rrshift1_add_q32, sw);
1154 MIPSDSP32_BINOP(addqh_w, rshift1_add_q32, sw);
1155 MIPSDSP32_BINOP(adduh_qb, rshift1_add_u8, ub);
1156 MIPSDSP32_BINOP(adduh_r_qb, rrshift1_add_u8, ub);
1157 MIPSDSP32_BINOP(subqh_ph, rshift1_sub_q16, sh);
1158 MIPSDSP32_BINOP(subqh_r_ph, rrshift1_sub_q16, sh);
1159 MIPSDSP32_BINOP(subqh_r_w, rrshift1_sub_q32, sw);
1160 MIPSDSP32_BINOP(subqh_w, rshift1_sub_q32, sw);
1161 #undef MIPSDSP32_BINOP
1163 #define MIPSDSP32_BINOP_ENV(name, func, element) \
1164 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1165 CPUMIPSState *env) \
1167 DSP32Value ds, dt; \
1173 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1174 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1177 return (target_long)ds.sw[0]; \
1179 MIPSDSP32_BINOP_ENV(addq_ph, add_i16, sh)
1180 MIPSDSP32_BINOP_ENV(addq_s_ph, sat_add_i16, sh)
1181 MIPSDSP32_BINOP_ENV(addq_s_w, sat_add_i32, sw);
1182 MIPSDSP32_BINOP_ENV(addu_ph, add_u16, sh)
1183 MIPSDSP32_BINOP_ENV(addu_qb, add_u8, ub);
1184 MIPSDSP32_BINOP_ENV(addu_s_ph, sat_add_u16, sh)
1185 MIPSDSP32_BINOP_ENV(addu_s_qb, sat_add_u8, ub);
1186 MIPSDSP32_BINOP_ENV(subq_ph, sub_i16, sh);
1187 MIPSDSP32_BINOP_ENV(subq_s_ph, sat16_sub, sh);
1188 MIPSDSP32_BINOP_ENV(subq_s_w, sat32_sub, sw);
1189 MIPSDSP32_BINOP_ENV(subu_ph, sub_u16_u16, sh);
1190 MIPSDSP32_BINOP_ENV(subu_qb, sub_u8, ub);
1191 MIPSDSP32_BINOP_ENV(subu_s_ph, satu16_sub_u16_u16, sh);
1192 MIPSDSP32_BINOP_ENV(subu_s_qb, satu8_sub, ub);
1193 #undef MIPSDSP32_BINOP_ENV
1195 #ifdef TARGET_MIPS64
1196 #define MIPSDSP64_BINOP(name, func, element) \
1197 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
1199 DSP64Value ds, dt; \
1205 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1206 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1211 MIPSDSP64_BINOP(adduh_ob, rshift1_add_u8, ub);
1212 MIPSDSP64_BINOP(adduh_r_ob, rrshift1_add_u8, ub);
1213 MIPSDSP64_BINOP(subuh_ob, rshift1_sub_u8, ub);
1214 MIPSDSP64_BINOP(subuh_r_ob, rrshift1_sub_u8, ub);
1215 #undef MIPSDSP64_BINOP
1217 #define MIPSDSP64_BINOP_ENV(name, func, element) \
1218 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1219 CPUMIPSState *env) \
1221 DSP64Value ds, dt; \
1227 for (i = 0 ; i < ARRAY_SIZE(ds.element); i++) { \
1228 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1233 MIPSDSP64_BINOP_ENV(addq_pw, add_i32, sw);
1234 MIPSDSP64_BINOP_ENV(addq_qh, add_i16, sh);
1235 MIPSDSP64_BINOP_ENV(addq_s_pw, sat_add_i32, sw);
1236 MIPSDSP64_BINOP_ENV(addq_s_qh, sat_add_i16, sh);
1237 MIPSDSP64_BINOP_ENV(addu_ob, add_u8, uh);
1238 MIPSDSP64_BINOP_ENV(addu_qh, add_u16, uh);
1239 MIPSDSP64_BINOP_ENV(addu_s_ob, sat_add_u8, uh);
1240 MIPSDSP64_BINOP_ENV(addu_s_qh, sat_add_u16, uh);
1241 MIPSDSP64_BINOP_ENV(subq_pw, sub32, sw);
1242 MIPSDSP64_BINOP_ENV(subq_qh, sub_i16, sh);
1243 MIPSDSP64_BINOP_ENV(subq_s_pw, sat32_sub, sw);
1244 MIPSDSP64_BINOP_ENV(subq_s_qh, sat16_sub, sh);
1245 MIPSDSP64_BINOP_ENV(subu_ob, sub_u8, uh);
1246 MIPSDSP64_BINOP_ENV(subu_qh, sub_u16_u16, uh);
1247 MIPSDSP64_BINOP_ENV(subu_s_ob, satu8_sub, uh);
1248 MIPSDSP64_BINOP_ENV(subu_s_qh, satu16_sub_u16_u16, uh);
1249 #undef MIPSDSP64_BINOP_ENV
1253 #define SUBUH_QB(name, var) \
1254 target_ulong helper_##name##_qb(target_ulong rs, target_ulong rt) \
1256 uint8_t rs3, rs2, rs1, rs0; \
1257 uint8_t rt3, rt2, rt1, rt0; \
1258 uint8_t tempD, tempC, tempB, tempA; \
1260 MIPSDSP_SPLIT32_8(rs, rs3, rs2, rs1, rs0); \
1261 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1263 tempD = ((uint16_t)rs3 - (uint16_t)rt3 + var) >> 1; \
1264 tempC = ((uint16_t)rs2 - (uint16_t)rt2 + var) >> 1; \
1265 tempB = ((uint16_t)rs1 - (uint16_t)rt1 + var) >> 1; \
1266 tempA = ((uint16_t)rs0 - (uint16_t)rt0 + var) >> 1; \
1268 return ((uint32_t)tempD << 24) | ((uint32_t)tempC << 16) | \
1269 ((uint32_t)tempB << 8) | ((uint32_t)tempA); \
1273 SUBUH_QB(subuh_r, 1);
1277 target_ulong helper_addsc(target_ulong rs, target_ulong rt, CPUMIPSState *env)
1279 uint64_t temp, tempRs, tempRt;
1282 tempRs = (uint64_t)rs & MIPSDSP_LLO;
1283 tempRt = (uint64_t)rt & MIPSDSP_LLO;
1285 temp = tempRs + tempRt;
1286 flag = (temp & 0x0100000000ull) >> 32;
1287 set_DSPControl_carryflag(flag, env);
1289 return (target_long)(int32_t)(temp & MIPSDSP_LLO);
1292 target_ulong helper_addwc(target_ulong rs, target_ulong rt, CPUMIPSState *env)
1295 int32_t temp32, temp31;
1298 tempL = (int64_t)(int32_t)rs + (int64_t)(int32_t)rt +
1299 get_DSPControl_carryflag(env);
1300 temp31 = (tempL >> 31) & 0x01;
1301 temp32 = (tempL >> 32) & 0x01;
1303 if (temp31 != temp32) {
1304 set_DSPControl_overflow_flag(1, 20, env);
1307 rd = tempL & MIPSDSP_LLO;
1309 return (target_long)(int32_t)rd;
1312 target_ulong helper_modsub(target_ulong rs, target_ulong rt)
1318 decr = rt & MIPSDSP_Q0;
1319 lastindex = (rt >> 8) & MIPSDSP_LO;
1321 if ((rs & MIPSDSP_LLO) == 0x00000000) {
1322 rd = (target_ulong)lastindex;
1330 target_ulong helper_raddu_w_qb(target_ulong rs)
1332 target_ulong ret = 0;
1337 for (i = 0; i < 4; i++) {
1343 #if defined(TARGET_MIPS64)
1344 target_ulong helper_raddu_l_ob(target_ulong rs)
1346 target_ulong ret = 0;
1351 for (i = 0; i < 8; i++) {
1358 #define PRECR_QB_PH(name, a, b)\
1359 target_ulong helper_##name##_qb_ph(target_ulong rs, target_ulong rt) \
1361 uint8_t tempD, tempC, tempB, tempA; \
1363 tempD = (rs >> a) & MIPSDSP_Q0; \
1364 tempC = (rs >> b) & MIPSDSP_Q0; \
1365 tempB = (rt >> a) & MIPSDSP_Q0; \
1366 tempA = (rt >> b) & MIPSDSP_Q0; \
1368 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA); \
1371 PRECR_QB_PH(precr, 16, 0);
1372 PRECR_QB_PH(precrq, 24, 8);
1376 target_ulong helper_precr_sra_ph_w(uint32_t sa, target_ulong rs,
1379 uint16_t tempB, tempA;
1381 tempB = ((int32_t)rt >> sa) & MIPSDSP_LO;
1382 tempA = ((int32_t)rs >> sa) & MIPSDSP_LO;
1384 return MIPSDSP_RETURN32_16(tempB, tempA);
1387 target_ulong helper_precr_sra_r_ph_w(uint32_t sa,
1388 target_ulong rs, target_ulong rt)
1390 uint64_t tempB, tempA;
1392 /* If sa = 0, then (sa - 1) = -1 will case shift error, so we need else. */
1394 tempB = (rt & MIPSDSP_LO) << 1;
1395 tempA = (rs & MIPSDSP_LO) << 1;
1397 tempB = ((int32_t)rt >> (sa - 1)) + 1;
1398 tempA = ((int32_t)rs >> (sa - 1)) + 1;
1400 rt = (((tempB >> 1) & MIPSDSP_LO) << 16) | ((tempA >> 1) & MIPSDSP_LO);
1402 return (target_long)(int32_t)rt;
1405 target_ulong helper_precrq_ph_w(target_ulong rs, target_ulong rt)
1407 uint16_t tempB, tempA;
1409 tempB = (rs & MIPSDSP_HI) >> 16;
1410 tempA = (rt & MIPSDSP_HI) >> 16;
1412 return MIPSDSP_RETURN32_16(tempB, tempA);
1415 target_ulong helper_precrq_rs_ph_w(target_ulong rs, target_ulong rt,
1418 uint16_t tempB, tempA;
1420 tempB = mipsdsp_trunc16_sat16_round(rs, env);
1421 tempA = mipsdsp_trunc16_sat16_round(rt, env);
1423 return MIPSDSP_RETURN32_16(tempB, tempA);
1426 #if defined(TARGET_MIPS64)
1427 target_ulong helper_precr_ob_qh(target_ulong rs, target_ulong rt)
1429 uint8_t rs6, rs4, rs2, rs0;
1430 uint8_t rt6, rt4, rt2, rt0;
1433 rs6 = (rs >> 48) & MIPSDSP_Q0;
1434 rs4 = (rs >> 32) & MIPSDSP_Q0;
1435 rs2 = (rs >> 16) & MIPSDSP_Q0;
1436 rs0 = rs & MIPSDSP_Q0;
1437 rt6 = (rt >> 48) & MIPSDSP_Q0;
1438 rt4 = (rt >> 32) & MIPSDSP_Q0;
1439 rt2 = (rt >> 16) & MIPSDSP_Q0;
1440 rt0 = rt & MIPSDSP_Q0;
1442 temp = ((uint64_t)rs6 << 56) | ((uint64_t)rs4 << 48) |
1443 ((uint64_t)rs2 << 40) | ((uint64_t)rs0 << 32) |
1444 ((uint64_t)rt6 << 24) | ((uint64_t)rt4 << 16) |
1445 ((uint64_t)rt2 << 8) | (uint64_t)rt0;
1452 * In case sa == 0, use rt2, rt0, rs2, rs0.
1453 * In case sa != 0, use rt3, rt1, rs3, rs1.
1455 #define PRECR_QH_PW(name, var) \
1456 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, \
1460 uint16_t rs3, rs2, rs1, rs0; \
1461 uint16_t rt3, rt2, rt1, rt0; \
1462 uint16_t tempD, tempC, tempB, tempA; \
1464 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
1465 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1468 tempD = rt2 << var; \
1469 tempC = rt0 << var; \
1470 tempB = rs2 << var; \
1471 tempA = rs0 << var; \
1473 tempD = (((int16_t)rt3 >> sa) + var) >> var; \
1474 tempC = (((int16_t)rt1 >> sa) + var) >> var; \
1475 tempB = (((int16_t)rs3 >> sa) + var) >> var; \
1476 tempA = (((int16_t)rs1 >> sa) + var) >> var; \
1479 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1482 PRECR_QH_PW(sra, 0);
1483 PRECR_QH_PW(sra_r, 1);
1487 target_ulong helper_precrq_ob_qh(target_ulong rs, target_ulong rt)
1489 uint8_t rs6, rs4, rs2, rs0;
1490 uint8_t rt6, rt4, rt2, rt0;
1493 rs6 = (rs >> 56) & MIPSDSP_Q0;
1494 rs4 = (rs >> 40) & MIPSDSP_Q0;
1495 rs2 = (rs >> 24) & MIPSDSP_Q0;
1496 rs0 = (rs >> 8) & MIPSDSP_Q0;
1497 rt6 = (rt >> 56) & MIPSDSP_Q0;
1498 rt4 = (rt >> 40) & MIPSDSP_Q0;
1499 rt2 = (rt >> 24) & MIPSDSP_Q0;
1500 rt0 = (rt >> 8) & MIPSDSP_Q0;
1502 temp = ((uint64_t)rs6 << 56) | ((uint64_t)rs4 << 48) |
1503 ((uint64_t)rs2 << 40) | ((uint64_t)rs0 << 32) |
1504 ((uint64_t)rt6 << 24) | ((uint64_t)rt4 << 16) |
1505 ((uint64_t)rt2 << 8) | (uint64_t)rt0;
1510 target_ulong helper_precrq_qh_pw(target_ulong rs, target_ulong rt)
1512 uint16_t tempD, tempC, tempB, tempA;
1514 tempD = (rs >> 48) & MIPSDSP_LO;
1515 tempC = (rs >> 16) & MIPSDSP_LO;
1516 tempB = (rt >> 48) & MIPSDSP_LO;
1517 tempA = (rt >> 16) & MIPSDSP_LO;
1519 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA);
1522 target_ulong helper_precrq_rs_qh_pw(target_ulong rs, target_ulong rt,
1527 uint16_t tempD, tempC, tempB, tempA;
1529 rs2 = (rs >> 32) & MIPSDSP_LLO;
1530 rs0 = rs & MIPSDSP_LLO;
1531 rt2 = (rt >> 32) & MIPSDSP_LLO;
1532 rt0 = rt & MIPSDSP_LLO;
1534 tempD = mipsdsp_trunc16_sat16_round(rs2, env);
1535 tempC = mipsdsp_trunc16_sat16_round(rs0, env);
1536 tempB = mipsdsp_trunc16_sat16_round(rt2, env);
1537 tempA = mipsdsp_trunc16_sat16_round(rt0, env);
1539 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA);
1542 target_ulong helper_precrq_pw_l(target_ulong rs, target_ulong rt)
1544 uint32_t tempB, tempA;
1546 tempB = (rs >> 32) & MIPSDSP_LLO;
1547 tempA = (rt >> 32) & MIPSDSP_LLO;
1549 return MIPSDSP_RETURN64_32(tempB, tempA);
1553 target_ulong helper_precrqu_s_qb_ph(target_ulong rs, target_ulong rt,
1556 uint8_t tempD, tempC, tempB, tempA;
1557 uint16_t rsh, rsl, rth, rtl;
1559 rsh = (rs & MIPSDSP_HI) >> 16;
1560 rsl = rs & MIPSDSP_LO;
1561 rth = (rt & MIPSDSP_HI) >> 16;
1562 rtl = rt & MIPSDSP_LO;
1564 tempD = mipsdsp_sat8_reduce_precision(rsh, env);
1565 tempC = mipsdsp_sat8_reduce_precision(rsl, env);
1566 tempB = mipsdsp_sat8_reduce_precision(rth, env);
1567 tempA = mipsdsp_sat8_reduce_precision(rtl, env);
1569 return MIPSDSP_RETURN32_8(tempD, tempC, tempB, tempA);
1572 #if defined(TARGET_MIPS64)
1573 target_ulong helper_precrqu_s_ob_qh(target_ulong rs, target_ulong rt,
1577 uint16_t rs3, rs2, rs1, rs0;
1578 uint16_t rt3, rt2, rt1, rt0;
1584 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0);
1585 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0);
1587 temp[7] = mipsdsp_sat8_reduce_precision(rs3, env);
1588 temp[6] = mipsdsp_sat8_reduce_precision(rs2, env);
1589 temp[5] = mipsdsp_sat8_reduce_precision(rs1, env);
1590 temp[4] = mipsdsp_sat8_reduce_precision(rs0, env);
1591 temp[3] = mipsdsp_sat8_reduce_precision(rt3, env);
1592 temp[2] = mipsdsp_sat8_reduce_precision(rt2, env);
1593 temp[1] = mipsdsp_sat8_reduce_precision(rt1, env);
1594 temp[0] = mipsdsp_sat8_reduce_precision(rt0, env);
1596 for (i = 0; i < 8; i++) {
1597 result |= (uint64_t)temp[i] << (8 * i);
1603 #define PRECEQ_PW(name, a, b) \
1604 target_ulong helper_preceq_pw_##name(target_ulong rt) \
1606 uint16_t tempB, tempA; \
1607 uint32_t tempBI, tempAI; \
1609 tempB = (rt >> a) & MIPSDSP_LO; \
1610 tempA = (rt >> b) & MIPSDSP_LO; \
1612 tempBI = (uint32_t)tempB << 16; \
1613 tempAI = (uint32_t)tempA << 16; \
1615 return MIPSDSP_RETURN64_32(tempBI, tempAI); \
1618 PRECEQ_PW(qhl, 48, 32);
1619 PRECEQ_PW(qhr, 16, 0);
1620 PRECEQ_PW(qhla, 48, 16);
1621 PRECEQ_PW(qhra, 32, 0);
1627 #define PRECEQU_PH(name, a, b) \
1628 target_ulong helper_precequ_ph_##name(target_ulong rt) \
1630 uint16_t tempB, tempA; \
1632 tempB = (rt >> a) & MIPSDSP_Q0; \
1633 tempA = (rt >> b) & MIPSDSP_Q0; \
1635 tempB = tempB << 7; \
1636 tempA = tempA << 7; \
1638 return MIPSDSP_RETURN32_16(tempB, tempA); \
1641 PRECEQU_PH(qbl, 24, 16);
1642 PRECEQU_PH(qbr, 8, 0);
1643 PRECEQU_PH(qbla, 24, 8);
1644 PRECEQU_PH(qbra, 16, 0);
1648 #if defined(TARGET_MIPS64)
1649 #define PRECEQU_QH(name, a, b, c, d) \
1650 target_ulong helper_precequ_qh_##name(target_ulong rt) \
1652 uint16_t tempD, tempC, tempB, tempA; \
1654 tempD = (rt >> a) & MIPSDSP_Q0; \
1655 tempC = (rt >> b) & MIPSDSP_Q0; \
1656 tempB = (rt >> c) & MIPSDSP_Q0; \
1657 tempA = (rt >> d) & MIPSDSP_Q0; \
1659 tempD = tempD << 7; \
1660 tempC = tempC << 7; \
1661 tempB = tempB << 7; \
1662 tempA = tempA << 7; \
1664 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1667 PRECEQU_QH(obl, 56, 48, 40, 32);
1668 PRECEQU_QH(obr, 24, 16, 8, 0);
1669 PRECEQU_QH(obla, 56, 40, 24, 8);
1670 PRECEQU_QH(obra, 48, 32, 16, 0);
1676 #define PRECEU_PH(name, a, b) \
1677 target_ulong helper_preceu_ph_##name(target_ulong rt) \
1679 uint16_t tempB, tempA; \
1681 tempB = (rt >> a) & MIPSDSP_Q0; \
1682 tempA = (rt >> b) & MIPSDSP_Q0; \
1684 return MIPSDSP_RETURN32_16(tempB, tempA); \
1687 PRECEU_PH(qbl, 24, 16);
1688 PRECEU_PH(qbr, 8, 0);
1689 PRECEU_PH(qbla, 24, 8);
1690 PRECEU_PH(qbra, 16, 0);
1694 #if defined(TARGET_MIPS64)
1695 #define PRECEU_QH(name, a, b, c, d) \
1696 target_ulong helper_preceu_qh_##name(target_ulong rt) \
1698 uint16_t tempD, tempC, tempB, tempA; \
1700 tempD = (rt >> a) & MIPSDSP_Q0; \
1701 tempC = (rt >> b) & MIPSDSP_Q0; \
1702 tempB = (rt >> c) & MIPSDSP_Q0; \
1703 tempA = (rt >> d) & MIPSDSP_Q0; \
1705 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
1708 PRECEU_QH(obl, 56, 48, 40, 32);
1709 PRECEU_QH(obr, 24, 16, 8, 0);
1710 PRECEU_QH(obla, 56, 40, 24, 8);
1711 PRECEU_QH(obra, 48, 32, 16, 0);
1717 /** DSP GPR-Based Shift Sub-class insns **/
1718 #define SHIFT_QB(name, func) \
1719 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt) \
1721 uint8_t rt3, rt2, rt1, rt0; \
1725 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1727 rt3 = mipsdsp_##func(rt3, sa); \
1728 rt2 = mipsdsp_##func(rt2, sa); \
1729 rt1 = mipsdsp_##func(rt1, sa); \
1730 rt0 = mipsdsp_##func(rt0, sa); \
1732 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1735 #define SHIFT_QB_ENV(name, func) \
1736 target_ulong helper_##name##_qb(target_ulong sa, target_ulong rt,\
1737 CPUMIPSState *env) \
1739 uint8_t rt3, rt2, rt1, rt0; \
1743 MIPSDSP_SPLIT32_8(rt, rt3, rt2, rt1, rt0); \
1745 rt3 = mipsdsp_##func(rt3, sa, env); \
1746 rt2 = mipsdsp_##func(rt2, sa, env); \
1747 rt1 = mipsdsp_##func(rt1, sa, env); \
1748 rt0 = mipsdsp_##func(rt0, sa, env); \
1750 return MIPSDSP_RETURN32_8(rt3, rt2, rt1, rt0); \
1753 SHIFT_QB_ENV(shll, lshift8);
1754 SHIFT_QB(shrl, rshift_u8);
1756 SHIFT_QB(shra, rashift8);
1757 SHIFT_QB(shra_r, rnd8_rashift);
1762 #if defined(TARGET_MIPS64)
1763 #define SHIFT_OB(name, func) \
1764 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1773 for (i = 0; i < 8; i++) { \
1774 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1775 rt_t[i] = mipsdsp_##func(rt_t[i], sa); \
1776 temp |= (uint64_t)rt_t[i] << (8 * i); \
1782 #define SHIFT_OB_ENV(name, func) \
1783 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1784 CPUMIPSState *env) \
1793 for (i = 0; i < 8; i++) { \
1794 rt_t[i] = (rt >> (8 * i)) & MIPSDSP_Q0; \
1795 rt_t[i] = mipsdsp_##func(rt_t[i], sa, env); \
1796 temp |= (uint64_t)rt_t[i] << (8 * i); \
1802 SHIFT_OB_ENV(shll, lshift8);
1803 SHIFT_OB(shrl, rshift_u8);
1805 SHIFT_OB(shra, rashift8);
1806 SHIFT_OB(shra_r, rnd8_rashift);
1813 #define SHIFT_PH(name, func) \
1814 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt, \
1815 CPUMIPSState *env) \
1817 uint16_t rth, rtl; \
1821 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1823 rth = mipsdsp_##func(rth, sa, env); \
1824 rtl = mipsdsp_##func(rtl, sa, env); \
1826 return MIPSDSP_RETURN32_16(rth, rtl); \
1829 SHIFT_PH(shll, lshift16);
1830 SHIFT_PH(shll_s, sat16_lshift);
1834 #if defined(TARGET_MIPS64)
1835 #define SHIFT_QH(name, func) \
1836 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa) \
1838 uint16_t rt3, rt2, rt1, rt0; \
1842 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1844 rt3 = mipsdsp_##func(rt3, sa); \
1845 rt2 = mipsdsp_##func(rt2, sa); \
1846 rt1 = mipsdsp_##func(rt1, sa); \
1847 rt0 = mipsdsp_##func(rt0, sa); \
1849 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1852 #define SHIFT_QH_ENV(name, func) \
1853 target_ulong helper_##name##_qh(target_ulong rt, target_ulong sa, \
1854 CPUMIPSState *env) \
1856 uint16_t rt3, rt2, rt1, rt0; \
1860 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
1862 rt3 = mipsdsp_##func(rt3, sa, env); \
1863 rt2 = mipsdsp_##func(rt2, sa, env); \
1864 rt1 = mipsdsp_##func(rt1, sa, env); \
1865 rt0 = mipsdsp_##func(rt0, sa, env); \
1867 return MIPSDSP_RETURN64_16(rt3, rt2, rt1, rt0); \
1870 SHIFT_QH_ENV(shll, lshift16);
1871 SHIFT_QH_ENV(shll_s, sat16_lshift);
1873 SHIFT_QH(shrl, rshift_u16);
1874 SHIFT_QH(shra, rashift16);
1875 SHIFT_QH(shra_r, rnd16_rashift);
1882 #define SHIFT_W(name, func) \
1883 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1888 temp = mipsdsp_##func(rt, sa); \
1890 return (target_long)(int32_t)temp; \
1893 #define SHIFT_W_ENV(name, func) \
1894 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt, \
1895 CPUMIPSState *env) \
1900 temp = mipsdsp_##func(rt, sa, env); \
1902 return (target_long)(int32_t)temp; \
1905 SHIFT_W_ENV(shll_s, sat32_lshift);
1906 SHIFT_W(shra_r, rnd32_rashift);
1911 #if defined(TARGET_MIPS64)
1912 #define SHIFT_PW(name, func) \
1913 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa) \
1915 uint32_t rt1, rt0; \
1918 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1920 rt1 = mipsdsp_##func(rt1, sa); \
1921 rt0 = mipsdsp_##func(rt0, sa); \
1923 return MIPSDSP_RETURN64_32(rt1, rt0); \
1926 #define SHIFT_PW_ENV(name, func) \
1927 target_ulong helper_##name##_pw(target_ulong rt, target_ulong sa, \
1928 CPUMIPSState *env) \
1930 uint32_t rt1, rt0; \
1933 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
1935 rt1 = mipsdsp_##func(rt1, sa, env); \
1936 rt0 = mipsdsp_##func(rt0, sa, env); \
1938 return MIPSDSP_RETURN64_32(rt1, rt0); \
1941 SHIFT_PW_ENV(shll, lshift32);
1942 SHIFT_PW_ENV(shll_s, sat32_lshift);
1944 SHIFT_PW(shra, rashift32);
1945 SHIFT_PW(shra_r, rnd32_rashift);
1952 #define SHIFT_PH(name, func) \
1953 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1955 uint16_t rth, rtl; \
1959 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
1961 rth = mipsdsp_##func(rth, sa); \
1962 rtl = mipsdsp_##func(rtl, sa); \
1964 return MIPSDSP_RETURN32_16(rth, rtl); \
1967 SHIFT_PH(shrl, rshift_u16);
1968 SHIFT_PH(shra, rashift16);
1969 SHIFT_PH(shra_r, rnd16_rashift);
1973 /** DSP Multiply Sub-class insns **/
1975 * Return value made up by two 16bits value.
1976 * FIXME give the macro a better name.
1978 #define MUL_RETURN32_16_PH(name, func, \
1979 rsmov1, rsmov2, rsfilter, \
1980 rtmov1, rtmov2, rtfilter) \
1981 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
1982 CPUMIPSState *env) \
1984 uint16_t rsB, rsA, rtB, rtA; \
1986 rsB = (rs >> rsmov1) & rsfilter; \
1987 rsA = (rs >> rsmov2) & rsfilter; \
1988 rtB = (rt >> rtmov1) & rtfilter; \
1989 rtA = (rt >> rtmov2) & rtfilter; \
1991 rsB = mipsdsp_##func(rsB, rtB, env); \
1992 rsA = mipsdsp_##func(rsA, rtA, env); \
1994 return MIPSDSP_RETURN32_16(rsB, rsA); \
1997 MUL_RETURN32_16_PH(muleu_s_ph_qbl, mul_u8_u16, \
1998 24, 16, MIPSDSP_Q0, \
2000 MUL_RETURN32_16_PH(muleu_s_ph_qbr, mul_u8_u16, \
2003 MUL_RETURN32_16_PH(mulq_rs_ph, rndq15_mul_q15_q15, \
2004 16, 0, MIPSDSP_LO, \
2006 MUL_RETURN32_16_PH(mul_ph, mul_i16_i16, \
2007 16, 0, MIPSDSP_LO, \
2009 MUL_RETURN32_16_PH(mul_s_ph, sat16_mul_i16_i16, \
2010 16, 0, MIPSDSP_LO, \
2012 MUL_RETURN32_16_PH(mulq_s_ph, sat16_mul_q15_q15, \
2013 16, 0, MIPSDSP_LO, \
2016 #undef MUL_RETURN32_16_PH
2018 #define MUL_RETURN32_32_ph(name, func, movbits) \
2019 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2020 CPUMIPSState *env) \
2025 rsh = (rs >> movbits) & MIPSDSP_LO; \
2026 rth = (rt >> movbits) & MIPSDSP_LO; \
2027 temp = mipsdsp_##func(rsh, rth, env); \
2029 return (target_long)(int32_t)temp; \
2032 MUL_RETURN32_32_ph(muleq_s_w_phl, mul_q15_q15_overflowflag21, 16);
2033 MUL_RETURN32_32_ph(muleq_s_w_phr, mul_q15_q15_overflowflag21, 0);
2035 #undef MUL_RETURN32_32_ph
2037 #define MUL_VOID_PH(name, use_ac_env) \
2038 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2039 CPUMIPSState *env) \
2041 int16_t rsh, rsl, rth, rtl; \
2042 int32_t tempB, tempA; \
2043 int64_t acc, dotp; \
2045 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2046 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2048 if (use_ac_env == 1) { \
2049 tempB = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2050 tempA = mipsdsp_mul_q15_q15(ac, rsl, rtl, env); \
2052 tempB = mipsdsp_mul_u16_u16(rsh, rth); \
2053 tempA = mipsdsp_mul_u16_u16(rsl, rtl); \
2056 dotp = (int64_t)tempB - (int64_t)tempA; \
2057 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2058 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2059 dotp = dotp + acc; \
2060 env->active_tc.HI[ac] = (target_long)(int32_t) \
2061 ((dotp & MIPSDSP_LHI) >> 32); \
2062 env->active_tc.LO[ac] = (target_long)(int32_t)(dotp & MIPSDSP_LLO); \
2065 MUL_VOID_PH(mulsaq_s_w_ph, 1);
2066 MUL_VOID_PH(mulsa_w_ph, 0);
2070 #if defined(TARGET_MIPS64)
2071 #define MUL_RETURN64_16_QH(name, func, \
2072 rsmov1, rsmov2, rsmov3, rsmov4, rsfilter, \
2073 rtmov1, rtmov2, rtmov3, rtmov4, rtfilter) \
2074 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2075 CPUMIPSState *env) \
2077 uint16_t rs3, rs2, rs1, rs0; \
2078 uint16_t rt3, rt2, rt1, rt0; \
2079 uint16_t tempD, tempC, tempB, tempA; \
2081 rs3 = (rs >> rsmov1) & rsfilter; \
2082 rs2 = (rs >> rsmov2) & rsfilter; \
2083 rs1 = (rs >> rsmov3) & rsfilter; \
2084 rs0 = (rs >> rsmov4) & rsfilter; \
2085 rt3 = (rt >> rtmov1) & rtfilter; \
2086 rt2 = (rt >> rtmov2) & rtfilter; \
2087 rt1 = (rt >> rtmov3) & rtfilter; \
2088 rt0 = (rt >> rtmov4) & rtfilter; \
2090 tempD = mipsdsp_##func(rs3, rt3, env); \
2091 tempC = mipsdsp_##func(rs2, rt2, env); \
2092 tempB = mipsdsp_##func(rs1, rt1, env); \
2093 tempA = mipsdsp_##func(rs0, rt0, env); \
2095 return MIPSDSP_RETURN64_16(tempD, tempC, tempB, tempA); \
2098 MUL_RETURN64_16_QH(muleu_s_qh_obl, mul_u8_u16, \
2099 56, 48, 40, 32, MIPSDSP_Q0, \
2100 48, 32, 16, 0, MIPSDSP_LO);
2101 MUL_RETURN64_16_QH(muleu_s_qh_obr, mul_u8_u16, \
2102 24, 16, 8, 0, MIPSDSP_Q0, \
2103 48, 32, 16, 0, MIPSDSP_LO);
2104 MUL_RETURN64_16_QH(mulq_rs_qh, rndq15_mul_q15_q15, \
2105 48, 32, 16, 0, MIPSDSP_LO, \
2106 48, 32, 16, 0, MIPSDSP_LO);
2108 #undef MUL_RETURN64_16_QH
2110 #define MUL_RETURN64_32_QH(name, \
2113 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2114 CPUMIPSState *env) \
2116 uint16_t rsB, rsA; \
2117 uint16_t rtB, rtA; \
2118 uint32_t tempB, tempA; \
2120 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2121 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2122 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2123 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2125 tempB = mipsdsp_mul_q15_q15(5, rsB, rtB, env); \
2126 tempA = mipsdsp_mul_q15_q15(5, rsA, rtA, env); \
2128 return ((uint64_t)tempB << 32) | (uint64_t)tempA; \
2131 MUL_RETURN64_32_QH(muleq_s_pw_qhl, 48, 32, 48, 32);
2132 MUL_RETURN64_32_QH(muleq_s_pw_qhr, 16, 0, 16, 0);
2134 #undef MUL_RETURN64_32_QH
2136 void helper_mulsaq_s_w_qh(target_ulong rs, target_ulong rt, uint32_t ac,
2139 int16_t rs3, rs2, rs1, rs0;
2140 int16_t rt3, rt2, rt1, rt0;
2141 int32_t tempD, tempC, tempB, tempA;
2146 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0);
2147 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0);
2149 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env);
2150 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env);
2151 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env);
2152 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env);
2154 temp[0] = ((int32_t)tempD - (int32_t)tempC) +
2155 ((int32_t)tempB - (int32_t)tempA);
2156 temp[0] = (int64_t)(temp[0] << 30) >> 30;
2157 if (((temp[0] >> 33) & 0x01) == 0) {
2163 acc[0] = env->active_tc.LO[ac];
2164 acc[1] = env->active_tc.HI[ac];
2166 temp_sum = acc[0] + temp[0];
2167 if (((uint64_t)temp_sum < (uint64_t)acc[0]) &&
2168 ((uint64_t)temp_sum < (uint64_t)temp[0])) {
2174 env->active_tc.HI[ac] = acc[1];
2175 env->active_tc.LO[ac] = acc[0];
2179 #define DP_QB(name, func, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2180 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2181 CPUMIPSState *env) \
2185 uint16_t tempB, tempA; \
2186 uint64_t tempC, dotp; \
2188 rs3 = (rs >> rsmov1) & MIPSDSP_Q0; \
2189 rs2 = (rs >> rsmov2) & MIPSDSP_Q0; \
2190 rt3 = (rt >> rtmov1) & MIPSDSP_Q0; \
2191 rt2 = (rt >> rtmov2) & MIPSDSP_Q0; \
2192 tempB = mipsdsp_##func(rs3, rt3); \
2193 tempA = mipsdsp_##func(rs2, rt2); \
2194 dotp = (int64_t)tempB + (int64_t)tempA; \
2196 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2197 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2200 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2201 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2205 env->active_tc.HI[ac] = (target_long)(int32_t) \
2206 ((tempC & MIPSDSP_LHI) >> 32); \
2207 env->active_tc.LO[ac] = (target_long)(int32_t)(tempC & MIPSDSP_LLO); \
2210 DP_QB(dpau_h_qbl, mul_u8_u8, 1, 24, 16, 24, 16);
2211 DP_QB(dpau_h_qbr, mul_u8_u8, 1, 8, 0, 8, 0);
2212 DP_QB(dpsu_h_qbl, mul_u8_u8, 0, 24, 16, 24, 16);
2213 DP_QB(dpsu_h_qbr, mul_u8_u8, 0, 8, 0, 8, 0);
2217 #if defined(TARGET_MIPS64)
2218 #define DP_OB(name, add_sub, \
2219 rsmov1, rsmov2, rsmov3, rsmov4, \
2220 rtmov1, rtmov2, rtmov3, rtmov4) \
2221 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2222 CPUMIPSState *env) \
2224 uint8_t rsD, rsC, rsB, rsA; \
2225 uint8_t rtD, rtC, rtB, rtA; \
2226 uint16_t tempD, tempC, tempB, tempA; \
2229 uint64_t temp_sum; \
2234 rsD = (rs >> rsmov1) & MIPSDSP_Q0; \
2235 rsC = (rs >> rsmov2) & MIPSDSP_Q0; \
2236 rsB = (rs >> rsmov3) & MIPSDSP_Q0; \
2237 rsA = (rs >> rsmov4) & MIPSDSP_Q0; \
2238 rtD = (rt >> rtmov1) & MIPSDSP_Q0; \
2239 rtC = (rt >> rtmov2) & MIPSDSP_Q0; \
2240 rtB = (rt >> rtmov3) & MIPSDSP_Q0; \
2241 rtA = (rt >> rtmov4) & MIPSDSP_Q0; \
2243 tempD = mipsdsp_mul_u8_u8(rsD, rtD); \
2244 tempC = mipsdsp_mul_u8_u8(rsC, rtC); \
2245 tempB = mipsdsp_mul_u8_u8(rsB, rtB); \
2246 tempA = mipsdsp_mul_u8_u8(rsA, rtA); \
2248 temp[0] = (uint64_t)tempD + (uint64_t)tempC + \
2249 (uint64_t)tempB + (uint64_t)tempA; \
2251 acc[0] = env->active_tc.LO[ac]; \
2252 acc[1] = env->active_tc.HI[ac]; \
2255 temp_sum = acc[0] + temp[0]; \
2256 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2257 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2260 temp[0] = temp_sum; \
2261 temp[1] = acc[1] + temp[1]; \
2263 temp_sum = acc[0] - temp[0]; \
2264 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2267 temp[0] = temp_sum; \
2268 temp[1] = acc[1] - temp[1]; \
2271 env->active_tc.HI[ac] = temp[1]; \
2272 env->active_tc.LO[ac] = temp[0]; \
2275 DP_OB(dpau_h_obl, 1, 56, 48, 40, 32, 56, 48, 40, 32);
2276 DP_OB(dpau_h_obr, 1, 24, 16, 8, 0, 24, 16, 8, 0);
2277 DP_OB(dpsu_h_obl, 0, 56, 48, 40, 32, 56, 48, 40, 32);
2278 DP_OB(dpsu_h_obr, 0, 24, 16, 8, 0, 24, 16, 8, 0);
2283 #define DP_NOFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2284 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2285 CPUMIPSState *env) \
2287 int16_t rsB, rsA, rtB, rtA; \
2288 int32_t tempA, tempB; \
2291 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2292 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2293 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2294 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2296 tempB = (int32_t)rsB * (int32_t)rtB; \
2297 tempA = (int32_t)rsA * (int32_t)rtA; \
2299 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2300 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2303 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2305 acc = acc - ((int64_t)tempB + (int64_t)tempA); \
2308 env->active_tc.HI[ac] = (target_long)(int32_t)((acc & MIPSDSP_LHI) >> 32); \
2309 env->active_tc.LO[ac] = (target_long)(int32_t)(acc & MIPSDSP_LLO); \
2312 DP_NOFUNC_PH(dpa_w_ph, 1, 16, 0, 16, 0);
2313 DP_NOFUNC_PH(dpax_w_ph, 1, 16, 0, 0, 16);
2314 DP_NOFUNC_PH(dps_w_ph, 0, 16, 0, 16, 0);
2315 DP_NOFUNC_PH(dpsx_w_ph, 0, 16, 0, 0, 16);
2318 #define DP_HASFUNC_PH(name, is_add, rsmov1, rsmov2, rtmov1, rtmov2) \
2319 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2320 CPUMIPSState *env) \
2322 int16_t rsB, rsA, rtB, rtA; \
2323 int32_t tempB, tempA; \
2324 int64_t acc, dotp; \
2326 rsB = (rs >> rsmov1) & MIPSDSP_LO; \
2327 rsA = (rs >> rsmov2) & MIPSDSP_LO; \
2328 rtB = (rt >> rtmov1) & MIPSDSP_LO; \
2329 rtA = (rt >> rtmov2) & MIPSDSP_LO; \
2331 tempB = mipsdsp_mul_q15_q15(ac, rsB, rtB, env); \
2332 tempA = mipsdsp_mul_q15_q15(ac, rsA, rtA, env); \
2334 dotp = (int64_t)tempB + (int64_t)tempA; \
2335 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2336 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2344 env->active_tc.HI[ac] = (target_long)(int32_t) \
2345 ((acc & MIPSDSP_LHI) >> 32); \
2346 env->active_tc.LO[ac] = (target_long)(int32_t) \
2347 (acc & MIPSDSP_LLO); \
2350 DP_HASFUNC_PH(dpaq_s_w_ph, 1, 16, 0, 16, 0);
2351 DP_HASFUNC_PH(dpaqx_s_w_ph, 1, 16, 0, 0, 16);
2352 DP_HASFUNC_PH(dpsq_s_w_ph, 0, 16, 0, 16, 0);
2353 DP_HASFUNC_PH(dpsqx_s_w_ph, 0, 16, 0, 0, 16);
2355 #undef DP_HASFUNC_PH
2357 #define DP_128OPERATION_PH(name, is_add) \
2358 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2359 CPUMIPSState *env) \
2361 int16_t rsh, rsl, rth, rtl; \
2362 int32_t tempB, tempA, tempC62_31, tempC63; \
2363 int64_t acc, dotp, tempC; \
2365 MIPSDSP_SPLIT32_16(rs, rsh, rsl); \
2366 MIPSDSP_SPLIT32_16(rt, rth, rtl); \
2368 tempB = mipsdsp_mul_q15_q15(ac, rsh, rtl, env); \
2369 tempA = mipsdsp_mul_q15_q15(ac, rsl, rth, env); \
2371 dotp = (int64_t)tempB + (int64_t)tempA; \
2372 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2373 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2375 tempC = acc + dotp; \
2377 tempC = acc - dotp; \
2379 tempC63 = (tempC >> 63) & 0x01; \
2380 tempC62_31 = (tempC >> 31) & 0xFFFFFFFF; \
2382 if ((tempC63 == 0) && (tempC62_31 != 0x00000000)) { \
2383 tempC = 0x7FFFFFFF; \
2384 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2387 if ((tempC63 == 1) && (tempC62_31 != 0xFFFFFFFF)) { \
2388 tempC = (int64_t)(int32_t)0x80000000; \
2389 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2392 env->active_tc.HI[ac] = (target_long)(int32_t) \
2393 ((tempC & MIPSDSP_LHI) >> 32); \
2394 env->active_tc.LO[ac] = (target_long)(int32_t) \
2395 (tempC & MIPSDSP_LLO); \
2398 DP_128OPERATION_PH(dpaqx_sa_w_ph, 1);
2399 DP_128OPERATION_PH(dpsqx_sa_w_ph, 0);
2401 #undef DP_128OPERATION_HP
2403 #if defined(TARGET_MIPS64)
2404 #define DP_QH(name, is_add, use_ac_env) \
2405 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2406 CPUMIPSState *env) \
2408 int32_t rs3, rs2, rs1, rs0; \
2409 int32_t rt3, rt2, rt1, rt0; \
2410 int32_t tempD, tempC, tempB, tempA; \
2415 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2416 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2419 tempD = mipsdsp_mul_q15_q15(ac, rs3, rt3, env); \
2420 tempC = mipsdsp_mul_q15_q15(ac, rs2, rt2, env); \
2421 tempB = mipsdsp_mul_q15_q15(ac, rs1, rt1, env); \
2422 tempA = mipsdsp_mul_q15_q15(ac, rs0, rt0, env); \
2424 tempD = mipsdsp_mul_u16_u16(rs3, rt3); \
2425 tempC = mipsdsp_mul_u16_u16(rs2, rt2); \
2426 tempB = mipsdsp_mul_u16_u16(rs1, rt1); \
2427 tempA = mipsdsp_mul_u16_u16(rs0, rt0); \
2430 temp[0] = (int64_t)tempD + (int64_t)tempC + \
2431 (int64_t)tempB + (int64_t)tempA; \
2433 if (temp[0] >= 0) { \
2439 acc[1] = env->active_tc.HI[ac]; \
2440 acc[0] = env->active_tc.LO[ac]; \
2443 temp_sum = acc[0] + temp[0]; \
2444 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2445 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2446 acc[1] = acc[1] + 1; \
2448 temp[0] = temp_sum; \
2449 temp[1] = acc[1] + temp[1]; \
2451 temp_sum = acc[0] - temp[0]; \
2452 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2453 acc[1] = acc[1] - 1; \
2455 temp[0] = temp_sum; \
2456 temp[1] = acc[1] - temp[1]; \
2459 env->active_tc.HI[ac] = temp[1]; \
2460 env->active_tc.LO[ac] = temp[0]; \
2463 DP_QH(dpa_w_qh, 1, 0);
2464 DP_QH(dpaq_s_w_qh, 1, 1);
2465 DP_QH(dps_w_qh, 0, 0);
2466 DP_QH(dpsq_s_w_qh, 0, 1);
2472 #define DP_L_W(name, is_add) \
2473 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2474 CPUMIPSState *env) \
2477 int64_t dotp, acc; \
2481 dotp = mipsdsp_mul_q31_q31(ac, rs, rt, env); \
2482 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2483 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2485 temp = acc + dotp; \
2486 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2487 temp, (0x01ull << 63)); \
2489 temp = acc - dotp; \
2490 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2491 temp, (0x01ull << 63)); \
2495 temp63 = (temp >> 63) & 0x01; \
2496 if (temp63 == 1) { \
2497 temp = (0x01ull << 63) - 1; \
2499 temp = 0x01ull << 63; \
2502 set_DSPControl_overflow_flag(1, 16 + ac, env); \
2505 env->active_tc.HI[ac] = (target_long)(int32_t) \
2506 ((temp & MIPSDSP_LHI) >> 32); \
2507 env->active_tc.LO[ac] = (target_long)(int32_t) \
2508 (temp & MIPSDSP_LLO); \
2511 DP_L_W(dpaq_sa_l_w, 1);
2512 DP_L_W(dpsq_sa_l_w, 0);
2516 #if defined(TARGET_MIPS64)
2517 #define DP_L_PW(name, func) \
2518 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2519 CPUMIPSState *env) \
2523 int64_t tempB[2], tempA[2]; \
2531 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2532 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2534 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env); \
2535 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env); \
2537 if (tempB[0] >= 0) { \
2543 if (tempA[0] >= 0) { \
2549 temp_sum = tempB[0] + tempA[0]; \
2550 if (((uint64_t)temp_sum < (uint64_t)tempB[0]) && \
2551 ((uint64_t)temp_sum < (uint64_t)tempA[0])) { \
2554 temp[0] = temp_sum; \
2555 temp[1] += tempB[1] + tempA[1]; \
2557 mipsdsp_##func(acc, ac, temp, env); \
2559 env->active_tc.HI[ac] = acc[1]; \
2560 env->active_tc.LO[ac] = acc[0]; \
2563 DP_L_PW(dpaq_sa_l_pw, sat64_acc_add_q63);
2564 DP_L_PW(dpsq_sa_l_pw, sat64_acc_sub_q63);
2568 void helper_mulsaq_s_l_pw(target_ulong rs, target_ulong rt, uint32_t ac,
2573 int64_t tempB[2], tempA[2];
2578 rs1 = (rs >> 32) & MIPSDSP_LLO;
2579 rs0 = rs & MIPSDSP_LLO;
2580 rt1 = (rt >> 32) & MIPSDSP_LLO;
2581 rt0 = rt & MIPSDSP_LLO;
2583 tempB[0] = mipsdsp_mul_q31_q31(ac, rs1, rt1, env);
2584 tempA[0] = mipsdsp_mul_q31_q31(ac, rs0, rt0, env);
2586 if (tempB[0] >= 0) {
2592 if (tempA[0] >= 0) {
2598 acc[0] = env->active_tc.LO[ac];
2599 acc[1] = env->active_tc.HI[ac];
2601 temp_sum = tempB[0] - tempA[0];
2602 if ((uint64_t)temp_sum > (uint64_t)tempB[0]) {
2606 temp[1] = tempB[1] - tempA[1];
2608 if ((temp[1] & 0x01) == 0) {
2614 temp_sum = acc[0] + temp[0];
2615 if (((uint64_t)temp_sum < (uint64_t)acc[0]) &&
2616 ((uint64_t)temp_sum < (uint64_t)temp[0])) {
2622 env->active_tc.HI[ac] = acc[1];
2623 env->active_tc.LO[ac] = acc[0];
2627 #define MAQ_S_W(name, mov) \
2628 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2629 CPUMIPSState *env) \
2633 int64_t tempL, acc; \
2635 rsh = (rs >> mov) & MIPSDSP_LO; \
2636 rth = (rt >> mov) & MIPSDSP_LO; \
2637 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2638 acc = ((uint64_t)env->active_tc.HI[ac] << 32) | \
2639 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO); \
2640 tempL = (int64_t)tempA + acc; \
2641 env->active_tc.HI[ac] = (target_long)(int32_t) \
2642 ((tempL & MIPSDSP_LHI) >> 32); \
2643 env->active_tc.LO[ac] = (target_long)(int32_t) \
2644 (tempL & MIPSDSP_LLO); \
2647 MAQ_S_W(maq_s_w_phl, 16);
2648 MAQ_S_W(maq_s_w_phr, 0);
2652 #define MAQ_SA_W(name, mov) \
2653 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2654 CPUMIPSState *env) \
2659 rsh = (rs >> mov) & MIPSDSP_LO; \
2660 rth = (rt >> mov) & MIPSDSP_LO; \
2661 tempA = mipsdsp_mul_q15_q15(ac, rsh, rth, env); \
2662 tempA = mipsdsp_sat32_acc_q31(ac, tempA, env); \
2664 env->active_tc.HI[ac] = (target_long)(int32_t)(((int64_t)tempA & \
2665 MIPSDSP_LHI) >> 32); \
2666 env->active_tc.LO[ac] = (target_long)(int32_t)((int64_t)tempA & \
2670 MAQ_SA_W(maq_sa_w_phl, 16);
2671 MAQ_SA_W(maq_sa_w_phr, 0);
2675 #define MULQ_W(name, addvar) \
2676 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
2677 CPUMIPSState *env) \
2679 int32_t rs_t, rt_t; \
2683 rs_t = rs & MIPSDSP_LLO; \
2684 rt_t = rt & MIPSDSP_LLO; \
2686 if ((rs_t == 0x80000000) && (rt_t == 0x80000000)) { \
2687 tempL = 0x7FFFFFFF00000000ull; \
2688 set_DSPControl_overflow_flag(1, 21, env); \
2690 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2693 tempI = (tempL & MIPSDSP_LHI) >> 32; \
2695 return (target_long)(int32_t)tempI; \
2698 MULQ_W(mulq_s_w, 0);
2699 MULQ_W(mulq_rs_w, 0x80000000ull);
2703 #if defined(TARGET_MIPS64)
2705 #define MAQ_S_W_QH(name, mov) \
2706 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2707 CPUMIPSState *env) \
2709 int16_t rs_t, rt_t; \
2718 rs_t = (rs >> mov) & MIPSDSP_LO; \
2719 rt_t = (rt >> mov) & MIPSDSP_LO; \
2720 temp_mul = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2722 temp[0] = (int64_t)temp_mul; \
2723 if (temp[0] >= 0) { \
2729 acc[0] = env->active_tc.LO[ac]; \
2730 acc[1] = env->active_tc.HI[ac]; \
2732 temp_sum = acc[0] + temp[0]; \
2733 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2734 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2737 acc[0] = temp_sum; \
2738 acc[1] += temp[1]; \
2740 env->active_tc.HI[ac] = acc[1]; \
2741 env->active_tc.LO[ac] = acc[0]; \
2744 MAQ_S_W_QH(maq_s_w_qhll, 48);
2745 MAQ_S_W_QH(maq_s_w_qhlr, 32);
2746 MAQ_S_W_QH(maq_s_w_qhrl, 16);
2747 MAQ_S_W_QH(maq_s_w_qhrr, 0);
2751 #define MAQ_SA_W(name, mov) \
2752 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2753 CPUMIPSState *env) \
2755 int16_t rs_t, rt_t; \
2759 rs_t = (rs >> mov) & MIPSDSP_LO; \
2760 rt_t = (rt >> mov) & MIPSDSP_LO; \
2761 temp = mipsdsp_mul_q15_q15(ac, rs_t, rt_t, env); \
2762 temp = mipsdsp_sat32_acc_q31(ac, temp, env); \
2764 acc[0] = (int64_t)(int32_t)temp; \
2765 if (acc[0] >= 0) { \
2771 env->active_tc.HI[ac] = acc[1]; \
2772 env->active_tc.LO[ac] = acc[0]; \
2775 MAQ_SA_W(maq_sa_w_qhll, 48);
2776 MAQ_SA_W(maq_sa_w_qhlr, 32);
2777 MAQ_SA_W(maq_sa_w_qhrl, 16);
2778 MAQ_SA_W(maq_sa_w_qhrr, 0);
2782 #define MAQ_S_L_PW(name, mov) \
2783 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2784 CPUMIPSState *env) \
2786 int32_t rs_t, rt_t; \
2794 rs_t = (rs >> mov) & MIPSDSP_LLO; \
2795 rt_t = (rt >> mov) & MIPSDSP_LLO; \
2797 temp[0] = mipsdsp_mul_q31_q31(ac, rs_t, rt_t, env); \
2798 if (temp[0] >= 0) { \
2804 acc[0] = env->active_tc.LO[ac]; \
2805 acc[1] = env->active_tc.HI[ac]; \
2807 temp_sum = acc[0] + temp[0]; \
2808 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2809 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2812 acc[0] = temp_sum; \
2813 acc[1] += temp[1]; \
2815 env->active_tc.HI[ac] = acc[1]; \
2816 env->active_tc.LO[ac] = acc[0]; \
2819 MAQ_S_L_PW(maq_s_l_pwl, 32);
2820 MAQ_S_L_PW(maq_s_l_pwr, 0);
2824 #define DM_OPERATE(name, func, is_add, sigext) \
2825 void helper_##name(target_ulong rs, target_ulong rt, uint32_t ac, \
2826 CPUMIPSState *env) \
2830 int64_t tempBL[2], tempAL[2]; \
2838 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2839 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2842 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2843 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2845 if (tempBL[0] >= 0) { \
2848 tempBL[1] = ~0ull; \
2851 if (tempAL[0] >= 0) { \
2854 tempAL[1] = ~0ull; \
2857 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2858 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2863 acc[1] = env->active_tc.HI[ac]; \
2864 acc[0] = env->active_tc.LO[ac]; \
2866 temp_sum = tempBL[0] + tempAL[0]; \
2867 if (((uint64_t)temp_sum < (uint64_t)tempBL[0]) && \
2868 ((uint64_t)temp_sum < (uint64_t)tempAL[0])) { \
2871 temp[0] = temp_sum; \
2872 temp[1] += tempBL[1] + tempAL[1]; \
2875 temp_sum = acc[0] + temp[0]; \
2876 if (((uint64_t)temp_sum < (uint64_t)acc[0]) && \
2877 ((uint64_t)temp_sum < (uint64_t)temp[0])) { \
2880 temp[0] = temp_sum; \
2881 temp[1] = acc[1] + temp[1]; \
2883 temp_sum = acc[0] - temp[0]; \
2884 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2887 temp[0] = temp_sum; \
2888 temp[1] = acc[1] - temp[1]; \
2891 env->active_tc.HI[ac] = temp[1]; \
2892 env->active_tc.LO[ac] = temp[0]; \
2895 DM_OPERATE(dmadd, mul_i32_i32, 1, 1);
2896 DM_OPERATE(dmaddu, mul_u32_u32, 1, 0);
2897 DM_OPERATE(dmsub, mul_i32_i32, 0, 1);
2898 DM_OPERATE(dmsubu, mul_u32_u32, 0, 0);
2902 /** DSP Bit/Manipulation Sub-class insns **/
2903 target_ulong helper_bitrev(target_ulong rt)
2909 temp = rt & MIPSDSP_LO;
2911 for (i = 0; i < 16; i++) {
2912 rd = (rd << 1) | (temp & 1);
2916 return (target_ulong)rd;
2919 #define BIT_INSV(name, posfilter, ret_type) \
2920 target_ulong helper_##name(CPUMIPSState *env, target_ulong rs, \
2923 uint32_t pos, size, msb, lsb; \
2924 uint32_t const sizefilter = 0x3F; \
2925 target_ulong temp; \
2926 target_ulong dspc; \
2928 dspc = env->active_tc.DSPControl; \
2930 pos = dspc & posfilter; \
2931 size = (dspc >> 7) & sizefilter; \
2933 msb = pos + size - 1; \
2936 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2940 temp = deposit64(rt, pos, size, rs); \
2942 return (target_long)(ret_type)temp; \
2945 BIT_INSV(insv, 0x1F, int32_t);
2946 #ifdef TARGET_MIPS64
2947 BIT_INSV(dinsv, 0x7F, target_long);
2953 /** DSP Compare-Pick Sub-class insns **/
2954 #define CMP_HAS_RET(name, func, split_num, filter, bit_size) \
2955 target_ulong helper_##name(target_ulong rs, target_ulong rt) \
2957 uint32_t rs_t, rt_t; \
2959 uint32_t temp = 0; \
2962 for (i = 0; i < split_num; i++) { \
2963 rs_t = (rs >> (bit_size * i)) & filter; \
2964 rt_t = (rt >> (bit_size * i)) & filter; \
2965 cc = mipsdsp_##func(rs_t, rt_t); \
2969 return (target_ulong)temp; \
2972 CMP_HAS_RET(cmpgu_eq_qb, cmpu_eq, 4, MIPSDSP_Q0, 8);
2973 CMP_HAS_RET(cmpgu_lt_qb, cmpu_lt, 4, MIPSDSP_Q0, 8);
2974 CMP_HAS_RET(cmpgu_le_qb, cmpu_le, 4, MIPSDSP_Q0, 8);
2976 #ifdef TARGET_MIPS64
2977 CMP_HAS_RET(cmpgu_eq_ob, cmpu_eq, 8, MIPSDSP_Q0, 8);
2978 CMP_HAS_RET(cmpgu_lt_ob, cmpu_lt, 8, MIPSDSP_Q0, 8);
2979 CMP_HAS_RET(cmpgu_le_ob, cmpu_le, 8, MIPSDSP_Q0, 8);
2985 #define CMP_NO_RET(name, func, split_num, filter, bit_size) \
2986 void helper_##name(target_ulong rs, target_ulong rt, \
2987 CPUMIPSState *env) \
2989 int##bit_size##_t rs_t, rt_t; \
2990 int##bit_size##_t flag = 0; \
2991 int##bit_size##_t cc; \
2994 for (i = 0; i < split_num; i++) { \
2995 rs_t = (rs >> (bit_size * i)) & filter; \
2996 rt_t = (rt >> (bit_size * i)) & filter; \
2998 cc = mipsdsp_##func((int32_t)rs_t, (int32_t)rt_t); \
3002 set_DSPControl_24(flag, split_num, env); \
3005 CMP_NO_RET(cmpu_eq_qb, cmpu_eq, 4, MIPSDSP_Q0, 8);
3006 CMP_NO_RET(cmpu_lt_qb, cmpu_lt, 4, MIPSDSP_Q0, 8);
3007 CMP_NO_RET(cmpu_le_qb, cmpu_le, 4, MIPSDSP_Q0, 8);
3009 CMP_NO_RET(cmp_eq_ph, cmp_eq, 2, MIPSDSP_LO, 16);
3010 CMP_NO_RET(cmp_lt_ph, cmp_lt, 2, MIPSDSP_LO, 16);
3011 CMP_NO_RET(cmp_le_ph, cmp_le, 2, MIPSDSP_LO, 16);
3013 #ifdef TARGET_MIPS64
3014 CMP_NO_RET(cmpu_eq_ob, cmpu_eq, 8, MIPSDSP_Q0, 8);
3015 CMP_NO_RET(cmpu_lt_ob, cmpu_lt, 8, MIPSDSP_Q0, 8);
3016 CMP_NO_RET(cmpu_le_ob, cmpu_le, 8, MIPSDSP_Q0, 8);
3018 CMP_NO_RET(cmp_eq_qh, cmp_eq, 4, MIPSDSP_LO, 16);
3019 CMP_NO_RET(cmp_lt_qh, cmp_lt, 4, MIPSDSP_LO, 16);
3020 CMP_NO_RET(cmp_le_qh, cmp_le, 4, MIPSDSP_LO, 16);
3022 CMP_NO_RET(cmp_eq_pw, cmp_eq, 2, MIPSDSP_LLO, 32);
3023 CMP_NO_RET(cmp_lt_pw, cmp_lt, 2, MIPSDSP_LLO, 32);
3024 CMP_NO_RET(cmp_le_pw, cmp_le, 2, MIPSDSP_LLO, 32);
3028 #if defined(TARGET_MIPS64)
3030 #define CMPGDU_OB(name) \
3031 target_ulong helper_cmpgdu_##name##_ob(target_ulong rs, target_ulong rt, \
3032 CPUMIPSState *env) \
3035 uint8_t rs_t, rt_t; \
3040 for (i = 0; i < 8; i++) { \
3041 rs_t = (rs >> (8 * i)) & MIPSDSP_Q0; \
3042 rt_t = (rt >> (8 * i)) & MIPSDSP_Q0; \
3044 if (mipsdsp_cmpu_##name(rs_t, rt_t)) { \
3045 cond |= 0x01 << i; \
3049 set_DSPControl_24(cond, 8, env); \
3051 return (uint64_t)cond; \
3060 #define PICK_INSN(name, split_num, filter, bit_size, ret32bit) \
3061 target_ulong helper_##name(target_ulong rs, target_ulong rt, \
3062 CPUMIPSState *env) \
3064 uint32_t rs_t, rt_t; \
3068 target_ulong result = 0; \
3070 dsp = env->active_tc.DSPControl; \
3071 for (i = 0; i < split_num; i++) { \
3072 rs_t = (rs >> (bit_size * i)) & filter; \
3073 rt_t = (rt >> (bit_size * i)) & filter; \
3074 cc = (dsp >> (24 + i)) & 0x01; \
3075 cc = cc == 1 ? rs_t : rt_t; \
3077 result |= (target_ulong)cc << (bit_size * i); \
3081 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3087 PICK_INSN(pick_qb, 4, MIPSDSP_Q0, 8, 1);
3088 PICK_INSN(pick_ph, 2, MIPSDSP_LO, 16, 1);
3090 #ifdef TARGET_MIPS64
3091 PICK_INSN(pick_ob, 8, MIPSDSP_Q0, 8, 0);
3092 PICK_INSN(pick_qh, 4, MIPSDSP_LO, 16, 0);
3093 PICK_INSN(pick_pw, 2, MIPSDSP_LLO, 32, 0);
3097 target_ulong helper_packrl_ph(target_ulong rs, target_ulong rt)
3101 rsl = rs & MIPSDSP_LO;
3102 rth = (rt & MIPSDSP_HI) >> 16;
3104 return (target_long)(int32_t)((rsl << 16) | rth);
3107 #if defined(TARGET_MIPS64)
3108 target_ulong helper_packrl_pw(target_ulong rs, target_ulong rt)
3112 rs0 = rs & MIPSDSP_LLO;
3113 rt1 = (rt >> 32) & MIPSDSP_LLO;
3115 return ((uint64_t)rs0 << 32) | (uint64_t)rt1;
3119 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3120 target_ulong helper_extr_w(target_ulong ac, target_ulong shift,
3126 shift = shift & 0x1F;
3128 mipsdsp_rndrashift_short_acc(tempDL, ac, shift, env);
3129 if ((tempDL[1] != 0 || (tempDL[0] & MIPSDSP_LHI) != 0) &&
3130 (tempDL[1] != 1 || (tempDL[0] & MIPSDSP_LHI) != MIPSDSP_LHI)) {
3131 set_DSPControl_overflow_flag(1, 23, env);
3134 tempI = (tempDL[0] >> 1) & MIPSDSP_LLO;
3137 if (tempDL[0] == 0) {
3141 if (((tempDL[1] & 0x01) != 0 || (tempDL[0] & MIPSDSP_LHI) != 0) &&
3142 ((tempDL[1] & 0x01) != 1 || (tempDL[0] & MIPSDSP_LHI) != MIPSDSP_LHI)) {
3143 set_DSPControl_overflow_flag(1, 23, env);
3146 return (target_long)tempI;
3149 target_ulong helper_extr_r_w(target_ulong ac, target_ulong shift,
3154 shift = shift & 0x1F;
3156 mipsdsp_rndrashift_short_acc(tempDL, ac, shift, env);
3157 if ((tempDL[1] != 0 || (tempDL[0] & MIPSDSP_LHI) != 0) &&
3158 (tempDL[1] != 1 || (tempDL[0] & MIPSDSP_LHI) != MIPSDSP_LHI)) {
3159 set_DSPControl_overflow_flag(1, 23, env);
3163 if (tempDL[0] == 0) {
3167 if (((tempDL[1] & 0x01) != 0 || (tempDL[0] & MIPSDSP_LHI) != 0) &&
3168 ((tempDL[1] & 0x01) != 1 || (tempDL[0] & MIPSDSP_LHI) != MIPSDSP_LHI)) {
3169 set_DSPControl_overflow_flag(1, 23, env);
3172 return (target_long)(int32_t)(tempDL[0] >> 1);
3175 target_ulong helper_extr_rs_w(target_ulong ac, target_ulong shift,
3178 int32_t tempI, temp64;
3181 shift = shift & 0x1F;
3183 mipsdsp_rndrashift_short_acc(tempDL, ac, shift, env);
3184 if ((tempDL[1] != 0 || (tempDL[0] & MIPSDSP_LHI) != 0) &&
3185 (tempDL[1] != 1 || (tempDL[0] & MIPSDSP_LHI) != MIPSDSP_LHI)) {
3186 set_DSPControl_overflow_flag(1, 23, env);
3189 if (tempDL[0] == 0) {
3192 tempI = tempDL[0] >> 1;
3194 if (((tempDL[1] & 0x01) != 0 || (tempDL[0] & MIPSDSP_LHI) != 0) &&
3195 ((tempDL[1] & 0x01) != 1 || (tempDL[0] & MIPSDSP_LHI) != MIPSDSP_LHI)) {
3196 temp64 = tempDL[1] & 0x01;
3202 set_DSPControl_overflow_flag(1, 23, env);
3205 return (target_long)tempI;
3208 #if defined(TARGET_MIPS64)
3209 target_ulong helper_dextr_w(target_ulong ac, target_ulong shift,
3214 shift = shift & 0x3F;
3216 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3218 return (int64_t)(int32_t)(temp[0] >> 1);
3221 target_ulong helper_dextr_r_w(target_ulong ac, target_ulong shift,
3227 shift = shift & 0x3F;
3228 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3238 temp128 = temp[2] & 0x01;
3240 if ((temp128 != 0 || temp[1] != 0) &&
3241 (temp128 != 1 || temp[1] != ~0ull)) {
3242 set_DSPControl_overflow_flag(1, 23, env);
3245 return (int64_t)(int32_t)(temp[0] >> 1);
3248 target_ulong helper_dextr_rs_w(target_ulong ac, target_ulong shift,
3254 shift = shift & 0x3F;
3255 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3265 temp128 = temp[2] & 0x01;
3267 if ((temp128 != 0 || temp[1] != 0) &&
3268 (temp128 != 1 || temp[1] != ~0ull)) {
3270 temp[0] = 0x0FFFFFFFF;
3272 temp[0] = 0x0100000000ULL;
3274 set_DSPControl_overflow_flag(1, 23, env);
3277 return (int64_t)(int32_t)(temp[0] >> 1);
3280 target_ulong helper_dextr_l(target_ulong ac, target_ulong shift,
3286 shift = shift & 0x3F;
3288 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3290 ret = (temp[1] << 63) | (temp[0] >> 1);
3295 target_ulong helper_dextr_r_l(target_ulong ac, target_ulong shift,
3302 shift = shift & 0x3F;
3303 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3313 temp128 = temp[2] & 0x01;
3315 if ((temp128 != 0 || temp[1] != 0) &&
3316 (temp128 != 1 || temp[1] != ~0ull)) {
3317 set_DSPControl_overflow_flag(1, 23, env);
3320 ret = (temp[1] << 63) | (temp[0] >> 1);
3325 target_ulong helper_dextr_rs_l(target_ulong ac, target_ulong shift,
3332 shift = shift & 0x3F;
3333 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3343 temp128 = temp[2] & 0x01;
3345 if ((temp128 != 0 || temp[1] != 0) &&
3346 (temp128 != 1 || temp[1] != ~0ull)) {
3348 temp[1] &= ~0x00ull - 1;
3349 temp[0] |= ~0x00ull - 1;
3354 set_DSPControl_overflow_flag(1, 23, env);
3357 ret = (temp[1] << 63) | (temp[0] >> 1);
3363 target_ulong helper_extr_s_h(target_ulong ac, target_ulong shift,
3368 shift = shift & 0x1F;
3370 acc = ((int64_t)env->active_tc.HI[ac] << 32) |
3371 ((int64_t)env->active_tc.LO[ac] & 0xFFFFFFFF);
3373 temp = acc >> shift;
3375 if (temp > (int64_t)0x7FFF) {
3377 set_DSPControl_overflow_flag(1, 23, env);
3378 } else if (temp < (int64_t)0xFFFFFFFFFFFF8000ULL) {
3380 set_DSPControl_overflow_flag(1, 23, env);
3383 return (target_long)(int32_t)(temp & 0xFFFFFFFF);
3387 #if defined(TARGET_MIPS64)
3388 target_ulong helper_dextr_s_h(target_ulong ac, target_ulong shift,
3394 shift = shift & 0x1F;
3396 mipsdsp_rashift_acc((uint64_t *)temp, ac, shift, env);
3398 temp127 = (temp[1] >> 63) & 0x01;
3400 if ((temp127 == 0) && (temp[1] > 0 || temp[0] > 32767)) {
3401 temp[0] &= 0xFFFF0000;
3402 temp[0] |= 0x00007FFF;
3403 set_DSPControl_overflow_flag(1, 23, env);
3404 } else if ((temp127 == 1) &&
3405 (temp[1] < 0xFFFFFFFFFFFFFFFFll
3406 || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
3407 temp[0] &= 0xFFFF0000;
3408 temp[0] |= 0x00008000;
3409 set_DSPControl_overflow_flag(1, 23, env);
3412 return (int64_t)(int16_t)(temp[0] & MIPSDSP_LO);
3417 target_ulong helper_extp(target_ulong ac, target_ulong size, CPUMIPSState *env)
3427 start_pos = get_DSPControl_pos(env);
3428 sub = start_pos - (size + 1);
3430 acc = ((uint64_t)env->active_tc.HI[ac] << 32) |
3431 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
3432 temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
3433 set_DSPControl_efi(0, env);
3435 set_DSPControl_efi(1, env);
3438 return (target_ulong)temp;
3441 target_ulong helper_extpdp(target_ulong ac, target_ulong size,
3451 start_pos = get_DSPControl_pos(env);
3452 sub = start_pos - (size + 1);
3454 acc = ((uint64_t)env->active_tc.HI[ac] << 32) |
3455 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
3456 temp = extract64(acc, start_pos - size, size + 1);
3458 set_DSPControl_pos(sub, env);
3459 set_DSPControl_efi(0, env);
3461 set_DSPControl_efi(1, env);
3464 return (target_ulong)temp;
3468 #if defined(TARGET_MIPS64)
3469 target_ulong helper_dextp(target_ulong ac, target_ulong size, CPUMIPSState *env)
3474 uint64_t tempB, tempA;
3480 start_pos = get_DSPControl_pos(env);
3481 len = start_pos - size;
3482 tempB = env->active_tc.HI[ac];
3483 tempA = env->active_tc.LO[ac];
3485 sub = start_pos - (size + 1);
3488 temp = (tempB << (64 - len)) | (tempA >> len);
3489 temp = temp & ((1ULL << (size + 1)) - 1);
3490 set_DSPControl_efi(0, env);
3492 set_DSPControl_efi(1, env);
3498 target_ulong helper_dextpdp(target_ulong ac, target_ulong size,
3504 uint64_t tempB, tempA;
3509 start_pos = get_DSPControl_pos(env);
3510 len = start_pos - size;
3511 tempB = env->active_tc.HI[ac];
3512 tempA = env->active_tc.LO[ac];
3514 sub = start_pos - (size + 1);
3517 temp = (tempB << (64 - len)) | (tempA >> len);
3518 temp = temp & ((1ULL << (size + 1)) - 1);
3519 set_DSPControl_pos(sub, env);
3520 set_DSPControl_efi(0, env);
3522 set_DSPControl_efi(1, env);
3530 void helper_shilo(target_ulong ac, target_ulong rs, CPUMIPSState *env)
3536 rs5_0 = (int8_t)(rs5_0 << 2) >> 2;
3538 if (unlikely(rs5_0 == 0)) {
3542 acc = (((uint64_t)env->active_tc.HI[ac] << 32) & MIPSDSP_LHI) |
3543 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
3546 temp = acc >> rs5_0;
3548 temp = acc << -rs5_0;
3551 env->active_tc.HI[ac] = (target_ulong)(int32_t)((temp & MIPSDSP_LHI) >> 32);
3552 env->active_tc.LO[ac] = (target_ulong)(int32_t)(temp & MIPSDSP_LLO);
3555 #if defined(TARGET_MIPS64)
3556 void helper_dshilo(target_ulong shift, target_ulong ac, CPUMIPSState *env)
3559 uint64_t tempB, tempA;
3561 shift_t = (int8_t)(shift << 1) >> 1;
3563 tempB = env->active_tc.HI[ac];
3564 tempA = env->active_tc.LO[ac];
3568 tempA = (tempB << (64 - shift_t)) | (tempA >> shift_t);
3569 tempB = tempB >> shift_t;
3572 tempB = (tempB << shift_t) | (tempA >> (64 - shift_t));
3573 tempA = tempA << shift_t;
3577 env->active_tc.HI[ac] = tempB;
3578 env->active_tc.LO[ac] = tempA;
3582 void helper_mthlip(target_ulong ac, target_ulong rs, CPUMIPSState *env)
3584 int32_t tempA, tempB, pos;
3587 tempB = env->active_tc.LO[ac];
3588 env->active_tc.HI[ac] = (target_long)tempB;
3589 env->active_tc.LO[ac] = (target_long)tempA;
3590 pos = get_DSPControl_pos(env);
3595 set_DSPControl_pos(pos + 32, env);
3599 #if defined(TARGET_MIPS64)
3600 void helper_dmthlip(target_ulong rs, target_ulong ac, CPUMIPSState *env)
3604 uint64_t tempB, tempA;
3609 tempB = env->active_tc.LO[ac_t];
3611 env->active_tc.HI[ac_t] = tempB;
3612 env->active_tc.LO[ac_t] = tempA;
3614 pos = get_DSPControl_pos(env);
3618 set_DSPControl_pos(pos, env);
3623 void cpu_wrdsp(uint32_t rs, uint32_t mask_num, CPUMIPSState *env)
3627 uint32_t newbits, overwrite;
3631 overwrite = 0xFFFFFFFF;
3632 dsp = env->active_tc.DSPControl;
3634 for (i = 0; i < 6; i++) {
3635 mask[i] = (mask_num >> i) & 0x01;
3639 #if defined(TARGET_MIPS64)
3640 overwrite &= 0xFFFFFF80;
3641 newbits &= 0xFFFFFF80;
3642 newbits |= 0x0000007F & rs;
3644 overwrite &= 0xFFFFFFC0;
3645 newbits &= 0xFFFFFFC0;
3646 newbits |= 0x0000003F & rs;
3651 overwrite &= 0xFFFFE07F;
3652 newbits &= 0xFFFFE07F;
3653 newbits |= 0x00001F80 & rs;
3657 overwrite &= 0xFFFFDFFF;
3658 newbits &= 0xFFFFDFFF;
3659 newbits |= 0x00002000 & rs;
3663 overwrite &= 0xFF00FFFF;
3664 newbits &= 0xFF00FFFF;
3665 newbits |= 0x00FF0000 & rs;
3669 overwrite &= 0x00FFFFFF;
3670 newbits &= 0x00FFFFFF;
3671 #if defined(TARGET_MIPS64)
3672 newbits |= 0xFF000000 & rs;
3674 newbits |= 0x0F000000 & rs;
3679 overwrite &= 0xFFFFBFFF;
3680 newbits &= 0xFFFFBFFF;
3681 newbits |= 0x00004000 & rs;
3684 dsp = dsp & overwrite;
3685 dsp = dsp | newbits;
3686 env->active_tc.DSPControl = dsp;
3689 void helper_wrdsp(target_ulong rs, target_ulong mask_num, CPUMIPSState *env)
3691 cpu_wrdsp(rs, mask_num, env);
3694 uint32_t cpu_rddsp(uint32_t mask_num, CPUMIPSState *env)
3702 for (i = 0; i < 6; i++) {
3703 mask[i] = (mask_num & ruler) >> i ;
3708 dsp = env->active_tc.DSPControl;
3711 #if defined(TARGET_MIPS64)
3719 temp |= dsp & 0x1F80;
3723 temp |= dsp & 0x2000;
3727 temp |= dsp & 0x00FF0000;
3731 #if defined(TARGET_MIPS64)
3732 temp |= dsp & 0xFF000000;
3734 temp |= dsp & 0x0F000000;
3739 temp |= dsp & 0x4000;
3745 target_ulong helper_rddsp(target_ulong mask_num, CPUMIPSState *env)
3747 return cpu_rddsp(mask_num, env);
3760 #undef MIPSDSP_SPLIT32_8
3761 #undef MIPSDSP_SPLIT32_16
3763 #undef MIPSDSP_RETURN32_8
3764 #undef MIPSDSP_RETURN32_16
3766 #ifdef TARGET_MIPS64
3767 #undef MIPSDSP_SPLIT64_16
3768 #undef MIPSDSP_SPLIT64_32
3769 #undef MIPSDSP_RETURN64_16
3770 #undef MIPSDSP_RETURN64_32