target-ppc: emulate lfiwax instruction
[qemu/ar7.git] / target-mips / dsp_helper.c
blobf975da08c4717afa53ff2ebd8d9b7d2d0f028d01
1 /*
2 * MIPS ASE DSP Instruction emulation helpers for QEMU.
4 * Copyright (c) 2012 Jia Liu <proljc@gmail.com>
5 * Dongxue Zhang <elta.era@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "cpu.h"
21 #include "helper.h"
23 /* As the byte ordering doesn't matter, i.e. all columns are treated
24 identically, these unions can be used directly. */
25 typedef union {
26 uint8_t ub[4];
27 int8_t sb[4];
28 uint16_t uh[2];
29 int16_t sh[2];
30 uint32_t uw[1];
31 int32_t sw[1];
32 } DSP32Value;
34 typedef union {
35 uint8_t ub[8];
36 int8_t sb[8];
37 uint16_t uh[4];
38 int16_t sh[4];
39 uint32_t uw[2];
40 int32_t sw[2];
41 uint64_t ul[1];
42 int64_t sl[1];
43 } DSP64Value;
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,
51 CPUMIPSState *env)
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)
68 uint32_t filter;
70 filter = ((0x01 << len) - 1) << 24;
71 filter = ~filter;
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)
79 uint32_t filter;
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)
88 target_ulong dspc;
90 dspc = env->active_tc.DSPControl;
91 #ifndef TARGET_MIPS64
92 dspc = dspc & 0xFFFFFFC0;
93 dspc |= pos;
94 #else
95 dspc = dspc & 0xFFFFFF80;
96 dspc |= pos;
97 #endif
98 env->active_tc.DSPControl = dspc;
101 static inline uint32_t get_DSPControl_pos(CPUMIPSState *env)
103 target_ulong dspc;
104 uint32_t pos;
106 dspc = env->active_tc.DSPControl;
108 #ifndef TARGET_MIPS64
109 pos = dspc & 0x3F;
110 #else
111 pos = dspc & 0x7F;
112 #endif
114 return pos;
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, \
125 CPUMIPSState *env) \
127 if (a == INT##size##_MIN) { \
128 set_DSPControl_overflow_flag(1, 20, env); \
129 return INT##size##_MAX; \
130 } else { \
131 return MIPSDSP_ABS(a); \
134 DO_MIPS_SAT_ABS(8)
135 DO_MIPS_SAT_ABS(16)
136 DO_MIPS_SAT_ABS(32)
137 #undef DO_MIPS_SAT_ABS
139 /* get sum value */
140 static inline int16_t mipsdsp_add_i16(int16_t a, int16_t b, CPUMIPSState *env)
142 int16_t tempI;
144 tempI = a + b;
146 if (MIPSDSP_OVERFLOW_ADD(a, b, tempI, 0x8000)) {
147 set_DSPControl_overflow_flag(1, 20, env);
150 return tempI;
153 static inline int16_t mipsdsp_sat_add_i16(int16_t a, int16_t b,
154 CPUMIPSState *env)
156 int16_t tempS;
158 tempS = a + b;
160 if (MIPSDSP_OVERFLOW_ADD(a, b, tempS, 0x8000)) {
161 if (a > 0) {
162 tempS = 0x7FFF;
163 } else {
164 tempS = 0x8000;
166 set_DSPControl_overflow_flag(1, 20, env);
169 return tempS;
172 static inline int32_t mipsdsp_sat_add_i32(int32_t a, int32_t b,
173 CPUMIPSState *env)
175 int32_t tempI;
177 tempI = a + b;
179 if (MIPSDSP_OVERFLOW_ADD(a, b, tempI, 0x80000000)) {
180 if (a > 0) {
181 tempI = 0x7FFFFFFF;
182 } else {
183 tempI = 0x80000000;
185 set_DSPControl_overflow_flag(1, 20, env);
188 return tempI;
191 static inline uint8_t mipsdsp_add_u8(uint8_t a, uint8_t b, CPUMIPSState *env)
193 uint16_t temp;
195 temp = (uint16_t)a + (uint16_t)b;
197 if (temp & 0x0100) {
198 set_DSPControl_overflow_flag(1, 20, env);
201 return temp & 0xFF;
204 static inline uint16_t mipsdsp_add_u16(uint16_t a, uint16_t b,
205 CPUMIPSState *env)
207 uint32_t temp;
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,
219 CPUMIPSState *env)
221 uint8_t result;
222 uint16_t temp;
224 temp = (uint16_t)a + (uint16_t)b;
225 result = temp & 0xFF;
227 if (0x0100 & temp) {
228 result = 0xFF;
229 set_DSPControl_overflow_flag(1, 20, env);
232 return result;
235 static inline uint16_t mipsdsp_sat_add_u16(uint16_t a, uint16_t b,
236 CPUMIPSState *env)
238 uint16_t result;
239 uint32_t temp;
241 temp = (uint32_t)a + (uint32_t)b;
242 result = temp & 0xFFFF;
244 if (0x00010000 & temp) {
245 result = 0xFFFF;
246 set_DSPControl_overflow_flag(1, 20, env);
249 return result;
252 static inline int32_t mipsdsp_sat32_acc_q31(int32_t acc, int32_t a,
253 CPUMIPSState *env)
255 int64_t temp;
256 int32_t temp32, temp31, result;
257 int64_t temp_sum;
259 #ifndef TARGET_MIPS64
260 temp = ((uint64_t)env->active_tc.HI[acc] << 32) |
261 (uint64_t)env->active_tc.LO[acc];
262 #else
263 temp = (uint64_t)env->active_tc.LO[acc];
264 #endif
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) {
273 if (temp32 == 0) {
274 result = 0x7FFFFFFF;
275 } else {
276 result = 0x80000000;
278 set_DSPControl_overflow_flag(1, 16 + acc, env);
281 return result;
284 /* a[0] is LO, a[1] is HI. */
285 static inline void mipsdsp_sat64_acc_add_q63(int64_t *ret,
286 int32_t ac,
287 int64_t *a,
288 CPUMIPSState *env)
290 bool temp64;
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])) {
297 ret[1] += 1;
299 temp64 = ret[1] & 1;
300 if (temp64 != ((ret[0] >> 63) & 0x01)) {
301 if (temp64) {
302 ret[0] = (0x01ull << 63);
303 ret[1] = ~0ull;
304 } else {
305 ret[0] = (0x01ull << 63) - 1;
306 ret[1] = 0x00;
308 set_DSPControl_overflow_flag(1, 16 + ac, env);
312 static inline void mipsdsp_sat64_acc_sub_q63(int64_t *ret,
313 int32_t ac,
314 int64_t *a,
315 CPUMIPSState *env)
317 bool temp64;
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]) {
323 ret[1] -= 1;
325 temp64 = ret[1] & 1;
326 if (temp64 != ((ret[0] >> 63) & 0x01)) {
327 if (temp64) {
328 ret[0] = (0x01ull << 63);
329 ret[1] = ~0ull;
330 } else {
331 ret[0] = (0x01ull << 63) - 1;
332 ret[1] = 0x00;
334 set_DSPControl_overflow_flag(1, 16 + ac, env);
338 static inline int32_t mipsdsp_mul_i16_i16(int16_t a, int16_t b,
339 CPUMIPSState *env)
341 int32_t temp;
343 temp = (int32_t)a * (int32_t)b;
345 if ((temp > (int)0x7FFF) || (temp < (int)0xFFFF8000)) {
346 set_DSPControl_overflow_flag(1, 21, env);
348 temp &= 0x0000FFFF;
350 return temp;
353 static inline int32_t mipsdsp_mul_u16_u16(int32_t a, int32_t b)
355 return a * b;
358 static inline int32_t mipsdsp_mul_i32_i32(int32_t a, int32_t b)
360 return a * b;
363 static inline int32_t mipsdsp_sat16_mul_i16_i16(int16_t a, int16_t b,
364 CPUMIPSState *env)
366 int32_t temp;
368 temp = (int32_t)a * (int32_t)b;
370 if (temp > (int)0x7FFF) {
371 temp = 0x00007FFF;
372 set_DSPControl_overflow_flag(1, 21, env);
373 } else if (temp < (int)0xffff8000) {
374 temp = 0xFFFF8000;
375 set_DSPControl_overflow_flag(1, 21, env);
377 temp &= 0x0000FFFF;
379 return temp;
382 static inline int32_t mipsdsp_mul_q15_q15_overflowflag21(uint16_t a, uint16_t b,
383 CPUMIPSState *env)
385 int32_t temp;
387 if ((a == 0x8000) && (b == 0x8000)) {
388 temp = 0x7FFFFFFF;
389 set_DSPControl_overflow_flag(1, 21, env);
390 } else {
391 temp = ((int32_t)(int16_t)a * (int32_t)(int16_t)b) << 1;
394 return temp;
397 /* right shift */
398 static inline uint8_t mipsdsp_rshift_u8(uint8_t a, target_ulong mov)
400 return a >> mov;
403 static inline uint16_t mipsdsp_rshift_u16(uint16_t a, target_ulong mov)
405 return a >> mov;
408 static inline int8_t mipsdsp_rashift8(int8_t a, target_ulong mov)
410 return a >> mov;
413 static inline int16_t mipsdsp_rashift16(int16_t a, target_ulong mov)
415 return a >> mov;
418 static inline int32_t mipsdsp_rashift32(int32_t a, target_ulong mov)
420 return a >> mov;
423 static inline int16_t mipsdsp_rshift1_add_q16(int16_t a, int16_t b)
425 int32_t temp;
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)
435 int32_t temp;
437 temp = (int32_t)a + (int32_t)b;
438 temp += 1;
440 return (temp >> 1) & 0xFFFF;
443 static inline int32_t mipsdsp_rshift1_add_q32(int32_t a, int32_t b)
445 int64_t temp;
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)
454 int64_t temp;
456 temp = (int64_t)a + (int64_t)b;
457 temp += 1;
459 return (temp >> 1) & 0xFFFFFFFF;
462 static inline uint8_t mipsdsp_rshift1_add_u8(uint8_t a, uint8_t b)
464 uint16_t temp;
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)
473 uint16_t temp;
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)
482 uint16_t temp;
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)
491 uint16_t temp;
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,
500 int32_t ac,
501 int32_t shift,
502 CPUMIPSState *env)
504 int64_t acc;
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,
514 uint32_t ac,
515 uint32_t shift,
516 CPUMIPSState *env)
518 uint64_t tempB, tempA;
520 tempB = env->active_tc.HI[ac];
521 tempA = env->active_tc.LO[ac];
522 shift = shift & 0x1F;
524 if (shift == 0) {
525 p[1] = tempB;
526 p[0] = tempA;
527 } else {
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,
535 uint32_t ac,
536 uint32_t shift,
537 CPUMIPSState *env)
539 int64_t tempB, tempA;
541 tempB = env->active_tc.HI[ac];
542 tempA = env->active_tc.LO[ac];
543 shift = shift & 0x3F;
545 if (shift == 0) {
546 p[2] = tempB >> 63;
547 p[1] = (tempB << 1) | (tempA >> 63);
548 p[0] = tempA << 1;
549 } else {
550 p[0] = (tempB << (65 - shift)) | (tempA >> (shift - 1));
551 p[1] = (int64_t)tempB >> (shift - 1);
552 if (tempB >= 0) {
553 p[2] = 0x0;
554 } else {
555 p[2] = ~0ull;
560 static inline int32_t mipsdsp_mul_q15_q15(int32_t ac, uint16_t a, uint16_t b,
561 CPUMIPSState *env)
563 int32_t temp;
565 if ((a == 0x8000) && (b == 0x8000)) {
566 temp = 0x7FFFFFFF;
567 set_DSPControl_overflow_flag(1, 16 + ac, env);
568 } else {
569 temp = ((int16_t)a * (int16_t)b) << 1;
572 return temp;
575 static inline int64_t mipsdsp_mul_q31_q31(int32_t ac, uint32_t a, uint32_t b,
576 CPUMIPSState *env)
578 uint64_t temp;
580 if ((a == 0x80000000) && (b == 0x80000000)) {
581 temp = (0x01ull << 63) - 1;
582 set_DSPControl_overflow_flag(1, 16 + ac, env);
583 } else {
584 temp = ((uint64_t)a * (uint64_t)b) << 1;
587 return temp;
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,
596 CPUMIPSState *env)
598 uint32_t tempI;
600 tempI = (uint32_t)a * (uint32_t)b;
601 if (tempI > 0x0000FFFF) {
602 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,
615 CPUMIPSState *env)
617 uint32_t temp;
619 if ((a == 0x8000) && (b == 0x8000)) {
620 temp = 0x7FFF0000;
621 set_DSPControl_overflow_flag(1, 21, env);
622 } else {
623 temp = (a * b) << 1;
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,
631 CPUMIPSState *env)
633 int32_t temp;
635 if ((a == 0x8000) && (b == 0x8000)) {
636 temp = 0x7FFF0000;
637 set_DSPControl_overflow_flag(1, 21, env);
638 } else {
639 temp = (int16_t)a * (int16_t)b;
640 temp = temp << 1;
643 return (temp >> 16) & 0x0000FFFF;
646 static inline uint16_t mipsdsp_trunc16_sat16_round(int32_t a,
647 CPUMIPSState *env)
649 int64_t temp;
651 temp = (int32_t)a + 0x00008000;
653 if (a > (int)0x7fff8000) {
654 temp = 0x7FFFFFFF;
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,
662 CPUMIPSState *env)
664 uint16_t mag;
665 uint32_t sign;
667 sign = (a >> 15) & 0x01;
668 mag = a & 0x7FFF;
670 if (sign == 0) {
671 if (mag > 0x7F80) {
672 set_DSPControl_overflow_flag(1, 22, env);
673 return 0xFF;
674 } else {
675 return (mag >> 7) & 0xFFFF;
677 } else {
678 set_DSPControl_overflow_flag(1, 22, env);
679 return 0x00;
683 static inline uint8_t mipsdsp_lshift8(uint8_t a, uint8_t s, CPUMIPSState *env)
685 uint8_t sign;
686 uint8_t discard;
688 if (s == 0) {
689 return a;
690 } else {
691 sign = (a >> 7) & 0x01;
692 if (sign != 0) {
693 discard = (((0x01 << (8 - s)) - 1) << s) |
694 ((a >> (6 - (s - 1))) & ((0x01 << s) - 1));
695 } else {
696 discard = a >> (6 - (s - 1));
699 if (discard != 0x00) {
700 set_DSPControl_overflow_flag(1, 22, env);
702 return a << s;
706 static inline uint16_t mipsdsp_lshift16(uint16_t a, uint8_t s,
707 CPUMIPSState *env)
709 uint8_t sign;
710 uint16_t discard;
712 if (s == 0) {
713 return a;
714 } else {
715 sign = (a >> 15) & 0x01;
716 if (sign != 0) {
717 discard = (((0x01 << (16 - s)) - 1) << s) |
718 ((a >> (14 - (s - 1))) & ((0x01 << s) - 1));
719 } else {
720 discard = a >> (14 - (s - 1));
723 if ((discard != 0x0000) && (discard != 0xFFFF)) {
724 set_DSPControl_overflow_flag(1, 22, env);
726 return a << s;
731 static inline uint32_t mipsdsp_lshift32(uint32_t a, uint8_t s,
732 CPUMIPSState *env)
734 uint32_t discard;
736 if (s == 0) {
737 return a;
738 } else {
739 discard = (int32_t)a >> (31 - (s - 1));
741 if ((discard != 0x00000000) && (discard != 0xFFFFFFFF)) {
742 set_DSPControl_overflow_flag(1, 22, env);
744 return a << s;
748 static inline uint16_t mipsdsp_sat16_lshift(uint16_t a, uint8_t s,
749 CPUMIPSState *env)
751 uint8_t sign;
752 uint16_t discard;
754 if (s == 0) {
755 return a;
756 } else {
757 sign = (a >> 15) & 0x01;
758 if (sign != 0) {
759 discard = (((0x01 << (16 - s)) - 1) << s) |
760 ((a >> (14 - (s - 1))) & ((0x01 << s) - 1));
761 } else {
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;
768 } else {
769 return a << s;
774 static inline uint32_t mipsdsp_sat32_lshift(uint32_t a, uint8_t s,
775 CPUMIPSState *env)
777 uint8_t sign;
778 uint32_t discard;
780 if (s == 0) {
781 return a;
782 } else {
783 sign = (a >> 31) & 0x01;
784 if (sign != 0) {
785 discard = (((0x01 << (32 - s)) - 1) << s) |
786 ((a >> (30 - (s - 1))) & ((0x01 << s) - 1));
787 } else {
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;
794 } else {
795 return a << s;
800 static inline uint8_t mipsdsp_rnd8_rashift(uint8_t a, uint8_t s)
802 uint32_t temp;
804 if (s == 0) {
805 temp = (uint32_t)a << 1;
806 } else {
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)
815 uint32_t temp;
817 if (s == 0) {
818 temp = (uint32_t)a << 1;
819 } else {
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)
828 int64_t temp;
830 if (s == 0) {
831 temp = (uint64_t)a << 1;
832 } else {
833 temp = (int64_t)(int32_t)a >> (s - 1);
835 temp += 1;
837 return (temp >> 1) & 0xFFFFFFFFull;
840 static inline uint16_t mipsdsp_sub_i16(int16_t a, int16_t b, CPUMIPSState *env)
842 int16_t temp;
844 temp = a - b;
845 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x8000)) {
846 set_DSPControl_overflow_flag(1, 20, env);
849 return temp;
852 static inline uint16_t mipsdsp_sat16_sub(int16_t a, int16_t b,
853 CPUMIPSState *env)
855 int16_t temp;
857 temp = a - b;
858 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x8000)) {
859 if (a >= 0) {
860 temp = 0x7FFF;
861 } else {
862 temp = 0x8000;
864 set_DSPControl_overflow_flag(1, 20, env);
867 return temp;
870 static inline uint32_t mipsdsp_sat32_sub(int32_t a, int32_t b,
871 CPUMIPSState *env)
873 int32_t temp;
875 temp = a - b;
876 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x80000000)) {
877 if (a >= 0) {
878 temp = 0x7FFFFFFF;
879 } else {
880 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)
890 int32_t temp;
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)
899 int32_t temp;
901 temp = (int32_t)a - (int32_t)b;
902 temp += 1;
904 return (temp >> 1) & 0x0000FFFF;
907 static inline uint32_t mipsdsp_rshift1_sub_q32(int32_t a, int32_t b)
909 int64_t temp;
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)
918 int64_t temp;
920 temp = (int64_t)a - (int64_t)b;
921 temp += 1;
923 return (temp >> 1) & 0xFFFFFFFFull;
926 static inline uint16_t mipsdsp_sub_u16_u16(uint16_t a, uint16_t b,
927 CPUMIPSState *env)
929 uint8_t temp16;
930 uint32_t temp;
932 temp = (uint32_t)a - (uint32_t)b;
933 temp16 = (temp >> 16) & 0x01;
934 if (temp16 == 1) {
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,
941 CPUMIPSState *env)
943 uint8_t temp16;
944 uint32_t temp;
946 temp = (uint32_t)a - (uint32_t)b;
947 temp16 = (temp >> 16) & 0x01;
949 if (temp16 == 1) {
950 temp = 0x0000;
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)
959 uint8_t temp8;
960 uint16_t temp;
962 temp = (uint16_t)a - (uint16_t)b;
963 temp8 = (temp >> 8) & 0x01;
964 if (temp8 == 1) {
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)
973 uint8_t temp8;
974 uint16_t temp;
976 temp = (uint16_t)a - (uint16_t)b;
977 temp8 = (temp >> 8) & 0x01;
978 if (temp8 == 1) {
979 temp = 0x00;
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)
988 int32_t temp;
990 temp = a - b;
991 if (MIPSDSP_OVERFLOW_SUB(a, b, temp, 0x80000000)) {
992 set_DSPControl_overflow_flag(1, 20, env);
995 return temp;
998 static inline int32_t mipsdsp_add_i32(int32_t a, int32_t b, CPUMIPSState *env)
1000 int32_t temp;
1002 temp = a + b;
1004 if (MIPSDSP_OVERFLOW_ADD(a, b, temp, 0x80000000)) {
1005 set_DSPControl_overflow_flag(1, 20, env);
1008 return temp;
1011 static inline int32_t mipsdsp_cmp_eq(int32_t a, int32_t b)
1013 return a == b;
1016 static inline int32_t mipsdsp_cmp_le(int32_t a, int32_t b)
1018 return a <= b;
1021 static inline int32_t mipsdsp_cmp_lt(int32_t a, int32_t b)
1023 return a < b;
1026 static inline int32_t mipsdsp_cmpu_eq(uint32_t a, uint32_t b)
1028 return a == b;
1031 static inline int32_t mipsdsp_cmpu_le(uint32_t a, uint32_t b)
1033 return a <= b;
1036 static inline int32_t mipsdsp_cmpu_lt(uint32_t a, uint32_t b)
1038 return a < 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) \
1052 do { \
1053 a = (num >> 24) & MIPSDSP_Q0; \
1054 b = (num >> 16) & MIPSDSP_Q0; \
1055 c = (num >> 8) & MIPSDSP_Q0; \
1056 d = num & MIPSDSP_Q0; \
1057 } while (0)
1059 #define MIPSDSP_SPLIT32_16(num, a, b) \
1060 do { \
1061 a = (num >> 16) & MIPSDSP_LO; \
1062 b = num & MIPSDSP_LO; \
1063 } while (0)
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) \
1076 do { \
1077 a = (num >> 48) & MIPSDSP_LO; \
1078 b = (num >> 32) & MIPSDSP_LO; \
1079 c = (num >> 16) & MIPSDSP_LO; \
1080 d = num & MIPSDSP_LO; \
1081 } while (0)
1083 #define MIPSDSP_SPLIT64_32(num, a, b) \
1084 do { \
1085 a = (num >> 32) & MIPSDSP_LLO; \
1086 b = num & MIPSDSP_LLO; \
1087 } while (0)
1089 #define MIPSDSP_RETURN64_16(a, b, c, d) (((uint64_t)a << 48) | \
1090 ((uint64_t)b << 32) | \
1091 ((uint64_t)c << 16) | \
1092 (uint64_t)d)
1093 #define MIPSDSP_RETURN64_32(a, b) (((uint64_t)a << 32) | (uint64_t)b)
1094 #endif
1096 /** DSP Arithmetic Sub-class insns **/
1097 #define MIPSDSP32_UNOP_ENV(name, func, element) \
1098 target_ulong helper_##name(target_ulong rt, CPUMIPSState *env) \
1100 DSP32Value dt; \
1101 unsigned int i, n; \
1103 n = sizeof(DSP32Value) / sizeof(dt.element[0]); \
1104 dt.sw[0] = rt; \
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) \
1121 DSP64Value dt; \
1122 unsigned int i, n; \
1124 n = sizeof(DSP64Value) / sizeof(dt.element[0]); \
1125 dt.sl[0] = rt; \
1127 for (i = 0; i < n; i++) { \
1128 dt.element[i] = mipsdsp_##func(dt.element[i], env); \
1131 return dt.sl[0]; \
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
1137 #endif
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]); \
1146 ds.sw[0] = rs; \
1147 dt.sw[0] = rt; \
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]); \
1175 ds.sw[0] = rs; \
1176 dt.sw[0] = rt; \
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]); \
1208 ds.sl[0] = rs; \
1209 dt.sl[0] = rt; \
1211 for (i = 0 ; i < n ; i++) { \
1212 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i]); \
1215 return ds.sl[0]; \
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]); \
1231 ds.sl[0] = rs; \
1232 dt.sl[0] = rt; \
1234 for (i = 0 ; i < n ; i++) { \
1235 ds.element[i] = mipsdsp_##func(ds.element[i], dt.element[i], env); \
1238 return ds.sl[0]; \
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
1258 #endif
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); \
1279 SUBUH_QB(subuh, 0);
1280 SUBUH_QB(subuh_r, 1);
1282 #undef SUBUH_QB
1284 target_ulong helper_addsc(target_ulong rs, target_ulong rt, CPUMIPSState *env)
1286 uint64_t temp, tempRs, tempRt;
1287 int32_t flag;
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)
1301 uint32_t rd;
1302 int32_t temp32, temp31;
1303 int64_t tempL;
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)
1321 int32_t decr;
1322 uint16_t lastindex;
1323 target_ulong rd;
1325 decr = rt & MIPSDSP_Q0;
1326 lastindex = (rt >> 8) & MIPSDSP_LO;
1328 if ((rs & MIPSDSP_LLO) == 0x00000000) {
1329 rd = (target_ulong)lastindex;
1330 } else {
1331 rd = rs - decr;
1334 return rd;
1337 target_ulong helper_raddu_w_qb(target_ulong rs)
1339 target_ulong ret = 0;
1340 DSP32Value ds;
1341 unsigned int i;
1343 ds.uw[0] = rs;
1344 for (i = 0; i < 4; i++) {
1345 ret += ds.ub[i];
1347 return ret;
1350 #if defined(TARGET_MIPS64)
1351 target_ulong helper_raddu_l_ob(target_ulong rs)
1353 target_ulong ret = 0;
1354 DSP64Value ds;
1355 unsigned int i;
1357 ds.ul[0] = rs;
1358 for (i = 0; i < 8; i++) {
1359 ret += ds.ub[i];
1361 return ret;
1363 #endif
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);
1381 #undef PRECR_QB_OH
1383 target_ulong helper_precr_sra_ph_w(uint32_t sa, target_ulong rs,
1384 target_ulong rt)
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. */
1400 if (sa == 0) {
1401 tempB = (rt & MIPSDSP_LO) << 1;
1402 tempA = (rs & MIPSDSP_LO) << 1;
1403 } else {
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,
1423 CPUMIPSState *env)
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;
1438 uint64_t temp;
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;
1454 return temp;
1457 #define PRECR_QH_PW(name, var) \
1458 target_ulong helper_precr_##name##_qh_pw(target_ulong rs, target_ulong rt, \
1459 uint32_t sa) \
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. */ \
1470 if (sa == 0) { \
1471 tempD = rt2 << var; \
1472 tempC = rt0 << var; \
1473 tempB = rs2 << var; \
1474 tempA = rs0 << var; \
1475 } else { \
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);
1488 #undef PRECR_QH_PW
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;
1494 uint64_t temp;
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;
1510 return temp;
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,
1526 CPUMIPSState *env)
1528 uint32_t rs2, rs0;
1529 uint32_t rt2, rt0;
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);
1554 #endif
1556 target_ulong helper_precrqu_s_qb_ph(target_ulong rs, target_ulong rt,
1557 CPUMIPSState *env)
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,
1577 CPUMIPSState *env)
1579 int i;
1580 uint16_t rs3, rs2, rs1, rs0;
1581 uint16_t rt3, rt2, rt1, rt0;
1582 uint8_t temp[8];
1583 uint64_t result;
1585 result = 0;
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);
1603 return result;
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);
1626 #undef PRECEQ_PW
1628 #endif
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);
1649 #undef PRECEQU_PH
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);
1675 #undef PRECEQU_QH
1677 #endif
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);
1695 #undef PRECEU_PH
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);
1716 #undef PRECEU_QH
1718 #endif
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; \
1726 sa = sa & 0x07; \
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; \
1744 sa = sa & 0x07; \
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);
1762 #undef SHIFT_QB
1763 #undef SHIFT_QB_ENV
1765 #if defined(TARGET_MIPS64)
1766 #define SHIFT_OB(name, func) \
1767 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa) \
1769 int i; \
1770 uint8_t rt_t[8]; \
1771 uint64_t temp; \
1773 sa = sa & 0x07; \
1774 temp = 0; \
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); \
1782 return temp; \
1785 #define SHIFT_OB_ENV(name, func) \
1786 target_ulong helper_##name##_ob(target_ulong rt, target_ulong sa, \
1787 CPUMIPSState *env) \
1789 int i; \
1790 uint8_t rt_t[8]; \
1791 uint64_t temp; \
1793 sa = sa & 0x07; \
1794 temp = 0; \
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); \
1802 return temp; \
1805 SHIFT_OB_ENV(shll, lshift8);
1806 SHIFT_OB(shrl, rshift_u8);
1808 SHIFT_OB(shra, rashift8);
1809 SHIFT_OB(shra_r, rnd8_rashift);
1811 #undef SHIFT_OB
1812 #undef SHIFT_OB_ENV
1814 #endif
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; \
1822 sa = sa & 0x0F; \
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);
1835 #undef SHIFT_PH
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; \
1843 sa = sa & 0x0F; \
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; \
1861 sa = sa & 0x0F; \
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);
1880 #undef SHIFT_QH
1881 #undef SHIFT_QH_ENV
1883 #endif
1885 #define SHIFT_W(name, func) \
1886 target_ulong helper_##name##_w(target_ulong sa, target_ulong rt) \
1888 uint32_t temp; \
1890 sa = sa & 0x1F; \
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) \
1900 uint32_t temp; \
1902 sa = sa & 0x1F; \
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);
1911 #undef SHIFT_W
1912 #undef SHIFT_W_ENV
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; \
1920 sa = sa & 0x1F; \
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; \
1935 sa = sa & 0x1F; \
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);
1950 #undef SHIFT_PW
1951 #undef SHIFT_PW_ENV
1953 #endif
1955 #define SHIFT_PH(name, func) \
1956 target_ulong helper_##name##_ph(target_ulong sa, target_ulong rt) \
1958 uint16_t rth, rtl; \
1960 sa = sa & 0x0F; \
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);
1974 #undef SHIFT_PH
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, \
2001 16, 0, MIPSDSP_LO);
2002 MUL_RETURN32_16_PH(muleu_s_ph_qbr, mul_u8_u16, \
2003 8, 0, MIPSDSP_Q0, \
2004 16, 0, MIPSDSP_LO);
2005 MUL_RETURN32_16_PH(mulq_rs_ph, rndq15_mul_q15_q15, \
2006 16, 0, MIPSDSP_LO, \
2007 16, 0, MIPSDSP_LO);
2008 MUL_RETURN32_16_PH(mul_ph, mul_i16_i16, \
2009 16, 0, MIPSDSP_LO, \
2010 16, 0, MIPSDSP_LO);
2011 MUL_RETURN32_16_PH(mul_s_ph, sat16_mul_i16_i16, \
2012 16, 0, MIPSDSP_LO, \
2013 16, 0, MIPSDSP_LO);
2014 MUL_RETURN32_16_PH(mulq_s_ph, sat16_mul_q15_q15, \
2015 16, 0, MIPSDSP_LO, \
2016 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) \
2024 int16_t rsh, rth; \
2025 int32_t temp; \
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); \
2053 } else { \
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);
2070 #undef MUL_VOID_PH
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, \
2113 rsmov1, rsmov2, \
2114 rtmov1, rtmov2) \
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,
2139 CPUMIPSState *env)
2141 int16_t rs3, rs2, rs1, rs0;
2142 int16_t rt3, rt2, rt1, rt0;
2143 int32_t tempD, tempC, tempB, tempA;
2144 int64_t acc[2];
2145 int64_t temp[2];
2146 int64_t temp_sum;
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) {
2160 temp[1] = 0x00;
2161 } else {
2162 temp[1] = ~0ull;
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])) {
2171 acc[1] += 1;
2173 acc[0] = temp_sum;
2174 acc[1] += temp[1];
2176 env->active_tc.HI[ac] = acc[1];
2177 env->active_tc.LO[ac] = acc[0];
2179 #endif
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) \
2185 uint8_t rs3, rs2; \
2186 uint8_t rt3, rt2; \
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; \
2197 if (is_add) { \
2198 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2199 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2200 + dotp; \
2201 } else { \
2202 tempC = (((uint64_t)env->active_tc.HI[ac] << 32) | \
2203 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO)) \
2204 - dotp; \
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);
2217 #undef DP_QB
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; \
2229 uint64_t temp[2]; \
2230 uint64_t acc[2]; \
2231 uint64_t temp_sum; \
2233 temp[0] = 0; \
2234 temp[1] = 0; \
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]; \
2256 if (add_sub) { \
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])) { \
2260 acc[1] += 1; \
2262 temp[0] = temp_sum; \
2263 temp[1] = acc[1] + temp[1]; \
2264 } else { \
2265 temp_sum = acc[0] - temp[0]; \
2266 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2267 acc[1] -= 1; \
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);
2282 #undef DP_OB
2283 #endif
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; \
2291 int64_t acc; \
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); \
2304 if (is_add) { \
2305 acc = acc + ((int64_t)tempB + (int64_t)tempA); \
2306 } else { \
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);
2318 #undef DP_NOFUNC_PH
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); \
2340 if (is_add) { \
2341 acc = acc + dotp; \
2342 } else { \
2343 acc = acc - dotp; \
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); \
2376 if (is_add) { \
2377 tempC = acc + dotp; \
2378 } else { \
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; \
2413 int64_t acc[2]; \
2414 int64_t temp[2]; \
2415 int64_t temp_sum; \
2417 MIPSDSP_SPLIT64_16(rs, rs3, rs2, rs1, rs0); \
2418 MIPSDSP_SPLIT64_16(rt, rt3, rt2, rt1, rt0); \
2420 if (use_ac_env) { \
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); \
2425 } else { \
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) { \
2436 temp[1] = 0; \
2437 } else { \
2438 temp[1] = ~0ull; \
2441 acc[1] = env->active_tc.HI[ac]; \
2442 acc[0] = env->active_tc.LO[ac]; \
2444 if (is_add) { \
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]; \
2452 } else { \
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);
2470 #undef DP_QH
2472 #endif
2474 #define DP_L_W(name, is_add) \
2475 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2476 CPUMIPSState *env) \
2478 int32_t temp63; \
2479 int64_t dotp, acc; \
2480 uint64_t temp; \
2481 bool overflow; \
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); \
2486 if (is_add) { \
2487 temp = acc + dotp; \
2488 overflow = MIPSDSP_OVERFLOW_ADD((uint64_t)acc, (uint64_t)dotp, \
2489 temp, (0x01ull << 63)); \
2490 } else { \
2491 temp = acc - dotp; \
2492 overflow = MIPSDSP_OVERFLOW_SUB((uint64_t)acc, (uint64_t)dotp, \
2493 temp, (0x01ull << 63)); \
2496 if (overflow) { \
2497 temp63 = (temp >> 63) & 0x01; \
2498 if (temp63 == 1) { \
2499 temp = (0x01ull << 63) - 1; \
2500 } else { \
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);
2516 #undef DP_L_W
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) \
2523 int32_t rs1, rs0; \
2524 int32_t rt1, rt0; \
2525 int64_t tempB[2], tempA[2]; \
2526 int64_t temp[2]; \
2527 int64_t acc[2]; \
2528 int64_t temp_sum; \
2530 temp[0] = 0; \
2531 temp[1] = 0; \
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) { \
2540 tempB[1] = 0x00; \
2541 } else { \
2542 tempB[1] = ~0ull; \
2545 if (tempA[0] >= 0) { \
2546 tempA[1] = 0x00; \
2547 } else { \
2548 tempA[1] = ~0ull; \
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])) { \
2554 temp[1] += 1; \
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);
2568 #undef DP_L_PW
2570 void helper_mulsaq_s_l_pw(target_ulong rs, target_ulong rt, uint32_t ac,
2571 CPUMIPSState *env)
2573 int32_t rs1, rs0;
2574 int32_t rt1, rt0;
2575 int64_t tempB[2], tempA[2];
2576 int64_t temp[2];
2577 int64_t acc[2];
2578 int64_t temp_sum;
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) {
2589 tempB[1] = 0x00;
2590 } else {
2591 tempB[1] = ~0ull;
2594 if (tempA[0] >= 0) {
2595 tempA[1] = 0x00;
2596 } else {
2597 tempA[1] = ~0ull;
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]) {
2605 tempB[1] -= 1;
2607 temp[0] = temp_sum;
2608 temp[1] = tempB[1] - tempA[1];
2610 if ((temp[1] & 0x01) == 0) {
2611 temp[1] = 0x00;
2612 } else {
2613 temp[1] = ~0ull;
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])) {
2619 acc[1] += 1;
2621 acc[0] = temp_sum;
2622 acc[1] += temp[1];
2624 env->active_tc.HI[ac] = acc[1];
2625 env->active_tc.LO[ac] = acc[0];
2627 #endif
2629 #define MAQ_S_W(name, mov) \
2630 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2631 CPUMIPSState *env) \
2633 int16_t rsh, rth; \
2634 int32_t tempA; \
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);
2652 #undef MAQ_S_W
2654 #define MAQ_SA_W(name, mov) \
2655 void helper_##name(uint32_t ac, target_ulong rs, target_ulong rt, \
2656 CPUMIPSState *env) \
2658 int16_t rsh, rth; \
2659 int32_t tempA; \
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 & \
2669 MIPSDSP_LLO); \
2672 MAQ_SA_W(maq_sa_w_phl, 16);
2673 MAQ_SA_W(maq_sa_w_phr, 0);
2675 #undef MAQ_SA_W
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; \
2682 int32_t tempI; \
2683 int64_t tempL; \
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); \
2691 } else { \
2692 tempL = ((int64_t)rs_t * (int64_t)rt_t) << 1; \
2693 tempL += addvar; \
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);
2703 #undef MULQ_W
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; \
2712 int32_t temp_mul; \
2713 int64_t temp[2]; \
2714 int64_t acc[2]; \
2715 int64_t temp_sum; \
2717 temp[0] = 0; \
2718 temp[1] = 0; \
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) { \
2726 temp[1] = 0x00; \
2727 } else { \
2728 temp[1] = ~0ull; \
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])) { \
2737 acc[1] += 1; \
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);
2751 #undef MAQ_S_W_QH
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; \
2758 int32_t temp; \
2759 int64_t acc[2]; \
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) { \
2768 acc[1] = 0x00; \
2769 } else { \
2770 acc[1] = ~0ull; \
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);
2782 #undef MAQ_SA_W
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; \
2789 int64_t temp[2]; \
2790 int64_t acc[2]; \
2791 int64_t temp_sum; \
2793 temp[0] = 0; \
2794 temp[1] = 0; \
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) { \
2801 temp[1] = 0x00; \
2802 } else { \
2803 temp[1] = ~0ull; \
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])) { \
2812 acc[1] += 1; \
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);
2824 #undef MAQ_S_L_PW
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) \
2830 int32_t rs1, rs0; \
2831 int32_t rt1, rt0; \
2832 int64_t tempBL[2], tempAL[2]; \
2833 int64_t acc[2]; \
2834 int64_t temp[2]; \
2835 int64_t temp_sum; \
2837 temp[0] = 0x00; \
2838 temp[1] = 0x00; \
2840 MIPSDSP_SPLIT64_32(rs, rs1, rs0); \
2841 MIPSDSP_SPLIT64_32(rt, rt1, rt0); \
2843 if (sigext) { \
2844 tempBL[0] = (int64_t)mipsdsp_##func(rs1, rt1); \
2845 tempAL[0] = (int64_t)mipsdsp_##func(rs0, rt0); \
2847 if (tempBL[0] >= 0) { \
2848 tempBL[1] = 0x0; \
2849 } else { \
2850 tempBL[1] = ~0ull; \
2853 if (tempAL[0] >= 0) { \
2854 tempAL[1] = 0x0; \
2855 } else { \
2856 tempAL[1] = ~0ull; \
2858 } else { \
2859 tempBL[0] = mipsdsp_##func(rs1, rt1); \
2860 tempAL[0] = mipsdsp_##func(rs0, rt0); \
2861 tempBL[1] = 0; \
2862 tempAL[1] = 0; \
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])) { \
2871 temp[1] += 1; \
2873 temp[0] = temp_sum; \
2874 temp[1] += tempBL[1] + tempAL[1]; \
2876 if (is_add) { \
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])) { \
2880 acc[1] += 1; \
2882 temp[0] = temp_sum; \
2883 temp[1] = acc[1] + temp[1]; \
2884 } else { \
2885 temp_sum = acc[0] - temp[0]; \
2886 if ((uint64_t)temp_sum > (uint64_t)acc[0]) { \
2887 acc[1] -= 1; \
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);
2901 #undef DM_OPERATE
2902 #endif
2904 /** DSP Bit/Manipulation Sub-class insns **/
2905 target_ulong helper_bitrev(target_ulong rt)
2907 int32_t temp;
2908 uint32_t rd;
2909 int i;
2911 temp = rt & MIPSDSP_LO;
2912 rd = 0;
2913 for (i = 0; i < 16; i++) {
2914 rd = (rd << 1) | (temp & 1);
2915 temp = 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, \
2923 target_ulong rt) \
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; \
2936 lsb = pos; \
2938 if (lsb > msb || (msb > TARGET_LONG_BITS)) { \
2939 return rt; \
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);
2954 #endif
2956 #undef BIT_INSV
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; \
2964 uint8_t cc; \
2965 uint32_t temp = 0; \
2966 int i; \
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); \
2972 temp |= cc << i; \
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);
2986 #endif
2988 #undef CMP_HAS_RET
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; \
2998 int i; \
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); \
3005 flag |= cc << i; \
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);
3031 #endif
3032 #undef CMP_NO_RET
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) \
3040 int i; \
3041 uint8_t rs_t, rt_t; \
3042 uint32_t cond; \
3044 cond = 0; \
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; \
3060 CMPGDU_OB(eq)
3061 CMPGDU_OB(lt)
3062 CMPGDU_OB(le)
3063 #undef CMPGDU_OB
3064 #endif
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; \
3071 uint32_t cc; \
3072 target_ulong dsp; \
3073 int i; \
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); \
3086 if (ret32bit) { \
3087 result = (target_long)(int32_t)(result & MIPSDSP_LLO); \
3090 return result; \
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);
3100 #endif
3101 #undef PICK_INSN
3103 target_ulong helper_packrl_ph(target_ulong rs, target_ulong rt)
3105 uint32_t rsl, rth;
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)
3116 uint32_t rs0, rt1;
3118 rs0 = rs & MIPSDSP_LLO;
3119 rt1 = (rt >> 32) & MIPSDSP_LLO;
3121 return ((uint64_t)rs0 << 32) | (uint64_t)rt1;
3123 #endif
3125 /** DSP Accumulator and DSPControl Access Sub-class insns **/
3126 target_ulong helper_extr_w(target_ulong ac, target_ulong shift,
3127 CPUMIPSState *env)
3129 int32_t tempI;
3130 int64_t tempDL[2];
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;
3142 tempDL[0] += 1;
3143 if (tempDL[0] == 0) {
3144 tempDL[1] += 1;
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,
3156 CPUMIPSState *env)
3158 int64_t tempDL[2];
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);
3168 tempDL[0] += 1;
3169 if (tempDL[0] == 0) {
3170 tempDL[1] += 1;
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,
3182 CPUMIPSState *env)
3184 int32_t tempI, temp64;
3185 int64_t tempDL[2];
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);
3194 tempDL[0] += 1;
3195 if (tempDL[0] == 0) {
3196 tempDL[1] += 1;
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;
3203 if (temp64 == 0) {
3204 tempI = 0x7FFFFFFF;
3205 } else {
3206 tempI = 0x80000000;
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,
3216 CPUMIPSState *env)
3218 uint64_t temp[3];
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,
3228 CPUMIPSState *env)
3230 uint64_t temp[3];
3231 uint32_t temp128;
3233 shift = shift & 0x3F;
3234 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3236 temp[0] += 1;
3237 if (temp[0] == 0) {
3238 temp[1] += 1;
3239 if (temp[1] == 0) {
3240 temp[2] += 1;
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,
3255 CPUMIPSState *env)
3257 uint64_t temp[3];
3258 uint32_t temp128;
3260 shift = shift & 0x3F;
3261 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3263 temp[0] += 1;
3264 if (temp[0] == 0) {
3265 temp[1] += 1;
3266 if (temp[1] == 0) {
3267 temp[2] += 1;
3271 temp128 = temp[2] & 0x01;
3273 if ((temp128 != 0 || temp[1] != 0) &&
3274 (temp128 != 1 || temp[1] != ~0ull)) {
3275 if (temp128 == 0) {
3276 temp[0] = 0x0FFFFFFFF;
3277 } else {
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,
3287 CPUMIPSState *env)
3289 uint64_t temp[3];
3290 target_ulong result;
3292 shift = shift & 0x3F;
3294 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3295 result = (temp[1] << 63) | (temp[0] >> 1);
3297 return result;
3300 target_ulong helper_dextr_r_l(target_ulong ac, target_ulong shift,
3301 CPUMIPSState *env)
3303 uint64_t temp[3];
3304 uint32_t temp128;
3305 target_ulong result;
3307 shift = shift & 0x3F;
3308 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3310 temp[0] += 1;
3311 if (temp[0] == 0) {
3312 temp[1] += 1;
3313 if (temp[1] == 0) {
3314 temp[2] += 1;
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);
3327 return result;
3330 target_ulong helper_dextr_rs_l(target_ulong ac, target_ulong shift,
3331 CPUMIPSState *env)
3333 uint64_t temp[3];
3334 uint32_t temp128;
3335 target_ulong result;
3337 shift = shift & 0x3F;
3338 mipsdsp_rndrashift_acc(temp, ac, shift, env);
3340 temp[0] += 1;
3341 if (temp[0] == 0) {
3342 temp[1] += 1;
3343 if (temp[1] == 0) {
3344 temp[2] += 1;
3348 temp128 = temp[2] & 0x01;
3350 if ((temp128 != 0 || temp[1] != 0) &&
3351 (temp128 != 1 || temp[1] != ~0ull)) {
3352 if (temp128 == 0) {
3353 temp[1] &= ~0x00ull - 1;
3354 temp[0] |= ~0x00ull - 1;
3355 } else {
3356 temp[1] |= 0x01;
3357 temp[0] &= 0x01;
3359 set_DSPControl_overflow_flag(1, 23, env);
3361 result = (temp[1] << 63) | (temp[0] >> 1);
3363 return result;
3365 #endif
3367 target_ulong helper_extr_s_h(target_ulong ac, target_ulong shift,
3368 CPUMIPSState *env)
3370 int64_t temp, acc;
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) {
3380 temp = 0x00007FFF;
3381 set_DSPControl_overflow_flag(1, 23, env);
3382 } else if (temp < (int64_t)0xFFFFFFFFFFFF8000ULL) {
3383 temp = 0xFFFF8000;
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,
3393 CPUMIPSState *env)
3395 int64_t temp[2];
3396 uint32_t temp127;
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);
3419 #endif
3421 target_ulong helper_extp(target_ulong ac, target_ulong size, CPUMIPSState *env)
3423 int32_t start_pos;
3424 int sub;
3425 uint32_t temp;
3426 uint64_t acc;
3428 size = size & 0x1F;
3430 temp = 0;
3431 start_pos = get_DSPControl_pos(env);
3432 sub = start_pos - (size + 1);
3433 if (sub >= -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);
3439 } else {
3440 set_DSPControl_efi(1, env);
3443 return (target_ulong)temp;
3446 target_ulong helper_extpdp(target_ulong ac, target_ulong size,
3447 CPUMIPSState *env)
3449 int32_t start_pos;
3450 int sub;
3451 uint32_t temp;
3452 uint64_t acc;
3454 size = size & 0x1F;
3455 temp = 0;
3456 start_pos = get_DSPControl_pos(env);
3457 sub = start_pos - (size + 1);
3458 if (sub >= -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);
3466 } else {
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)
3477 int start_pos;
3478 int len;
3479 int sub;
3480 uint64_t tempB, tempA;
3481 uint64_t temp;
3483 temp = 0;
3485 size = size & 0x3F;
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);
3493 if (sub >= -1) {
3494 temp = (tempB << (64 - len)) | (tempA >> len);
3495 temp = temp & ((0x01 << (size + 1)) - 1);
3496 set_DSPControl_efi(0, env);
3497 } else {
3498 set_DSPControl_efi(1, env);
3501 return temp;
3504 target_ulong helper_dextpdp(target_ulong ac, target_ulong size,
3505 CPUMIPSState *env)
3507 int start_pos;
3508 int len;
3509 int sub;
3510 uint64_t tempB, tempA;
3511 uint64_t temp;
3513 temp = 0;
3514 size = size & 0x3F;
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);
3522 if (sub >= -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);
3527 } else {
3528 set_DSPControl_efi(1, env);
3531 return temp;
3534 #endif
3536 void helper_shilo(target_ulong ac, target_ulong rs, CPUMIPSState *env)
3538 int8_t rs5_0;
3539 uint64_t temp, acc;
3541 rs5_0 = rs & 0x3F;
3542 rs5_0 = (int8_t)(rs5_0 << 2) >> 2;
3544 if (unlikely(rs5_0 == 0)) {
3545 return;
3548 acc = (((uint64_t)env->active_tc.HI[ac] << 32) & MIPSDSP_LHI) |
3549 ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
3551 if (rs5_0 > 0) {
3552 temp = acc >> rs5_0;
3553 } else {
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)
3564 int8_t shift_t;
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];
3572 if (shift_t != 0) {
3573 if (shift_t >= 0) {
3574 tempA = (tempB << (64 - shift_t)) | (tempA >> shift_t);
3575 tempB = tempB >> shift_t;
3576 } else {
3577 shift_t = -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;
3587 #endif
3588 void helper_mthlip(target_ulong ac, target_ulong rs, CPUMIPSState *env)
3590 int32_t tempA, tempB, pos;
3592 tempA = rs;
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);
3598 if (pos > 32) {
3599 return;
3600 } else {
3601 set_DSPControl_pos(pos + 32, env);
3605 #if defined(TARGET_MIPS64)
3606 void helper_dmthlip(target_ulong rs, target_ulong ac, CPUMIPSState *env)
3608 uint8_t ac_t;
3609 uint8_t pos;
3610 uint64_t tempB, tempA;
3612 ac_t = ac & 0x3;
3614 tempA = rs;
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);
3622 if (pos <= 64) {
3623 pos = pos + 64;
3624 set_DSPControl_pos(pos, env);
3627 #endif
3629 void cpu_wrdsp(uint32_t rs, uint32_t mask_num, CPUMIPSState *env)
3631 uint8_t mask[6];
3632 uint8_t i;
3633 uint32_t newbits, overwrite;
3634 target_ulong dsp;
3636 newbits = 0x00;
3637 overwrite = 0xFFFFFFFF;
3638 dsp = env->active_tc.DSPControl;
3640 for (i = 0; i < 6; i++) {
3641 mask[i] = (mask_num >> i) & 0x01;
3644 if (mask[0] == 1) {
3645 #if defined(TARGET_MIPS64)
3646 overwrite &= 0xFFFFFF80;
3647 newbits &= 0xFFFFFF80;
3648 newbits |= 0x0000007F & rs;
3649 #else
3650 overwrite &= 0xFFFFFFC0;
3651 newbits &= 0xFFFFFFC0;
3652 newbits |= 0x0000003F & rs;
3653 #endif
3656 if (mask[1] == 1) {
3657 overwrite &= 0xFFFFE07F;
3658 newbits &= 0xFFFFE07F;
3659 newbits |= 0x00001F80 & rs;
3662 if (mask[2] == 1) {
3663 overwrite &= 0xFFFFDFFF;
3664 newbits &= 0xFFFFDFFF;
3665 newbits |= 0x00002000 & rs;
3668 if (mask[3] == 1) {
3669 overwrite &= 0xFF00FFFF;
3670 newbits &= 0xFF00FFFF;
3671 newbits |= 0x00FF0000 & rs;
3674 if (mask[4] == 1) {
3675 overwrite &= 0x00FFFFFF;
3676 newbits &= 0x00FFFFFF;
3677 #if defined(TARGET_MIPS64)
3678 newbits |= 0xFF000000 & rs;
3679 #else
3680 newbits |= 0x0F000000 & rs;
3681 #endif
3684 if (mask[5] == 1) {
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)
3702 uint8_t mask[6];
3703 uint32_t ruler, i;
3704 target_ulong temp;
3705 target_ulong dsp;
3707 ruler = 0x01;
3708 for (i = 0; i < 6; i++) {
3709 mask[i] = (mask_num & ruler) >> i ;
3710 ruler = ruler << 1;
3713 temp = 0x00;
3714 dsp = env->active_tc.DSPControl;
3716 if (mask[0] == 1) {
3717 #if defined(TARGET_MIPS64)
3718 temp |= dsp & 0x7F;
3719 #else
3720 temp |= dsp & 0x3F;
3721 #endif
3724 if (mask[1] == 1) {
3725 temp |= dsp & 0x1F80;
3728 if (mask[2] == 1) {
3729 temp |= dsp & 0x2000;
3732 if (mask[3] == 1) {
3733 temp |= dsp & 0x00FF0000;
3736 if (mask[4] == 1) {
3737 #if defined(TARGET_MIPS64)
3738 temp |= dsp & 0xFF000000;
3739 #else
3740 temp |= dsp & 0x0F000000;
3741 #endif
3744 if (mask[5] == 1) {
3745 temp |= dsp & 0x4000;
3748 return temp;
3751 target_ulong helper_rddsp(target_ulong mask_num, CPUMIPSState *env)
3753 return cpu_rddsp(mask_num, env);
3757 #undef MIPSDSP_LHI
3758 #undef MIPSDSP_LLO
3759 #undef MIPSDSP_HI
3760 #undef MIPSDSP_LO
3761 #undef MIPSDSP_Q3
3762 #undef MIPSDSP_Q2
3763 #undef MIPSDSP_Q1
3764 #undef MIPSDSP_Q0
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
3777 #endif