Abort on attempts to allocate zero bytes
[qemu/mini2440.git] / target-mips / op_helper.c
blobb4c98fc059631d78ecd2c2aad38c0fb8a46e489e
1 /*
2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
20 #include <stdlib.h>
21 #include "exec.h"
23 #include "host-utils.h"
25 #include "helper.h"
26 /*****************************************************************************/
27 /* Exceptions processing helpers */
29 void helper_raise_exception_err (uint32_t exception, int error_code)
31 #if 1
32 if (exception < 0x100)
33 qemu_log("%s: %d %d\n", __func__, exception, error_code);
34 #endif
35 env->exception_index = exception;
36 env->error_code = error_code;
37 cpu_loop_exit();
40 void helper_raise_exception (uint32_t exception)
42 helper_raise_exception_err(exception, 0);
45 void helper_interrupt_restart (void)
47 if (!(env->CP0_Status & (1 << CP0St_EXL)) &&
48 !(env->CP0_Status & (1 << CP0St_ERL)) &&
49 !(env->hflags & MIPS_HFLAG_DM) &&
50 (env->CP0_Status & (1 << CP0St_IE)) &&
51 (env->CP0_Status & env->CP0_Cause & CP0Ca_IP_mask)) {
52 env->CP0_Cause &= ~(0x1f << CP0Ca_EC);
53 helper_raise_exception(EXCP_EXT_INTERRUPT);
57 #if !defined(CONFIG_USER_ONLY)
58 static void do_restore_state (void *pc_ptr)
60 TranslationBlock *tb;
61 unsigned long pc = (unsigned long) pc_ptr;
63 tb = tb_find_pc (pc);
64 if (tb) {
65 cpu_restore_state (tb, env, pc, NULL);
68 #endif
70 target_ulong helper_clo (target_ulong arg1)
72 return clo32(arg1);
75 target_ulong helper_clz (target_ulong arg1)
77 return clz32(arg1);
80 #if defined(TARGET_MIPS64)
81 target_ulong helper_dclo (target_ulong arg1)
83 return clo64(arg1);
86 target_ulong helper_dclz (target_ulong arg1)
88 return clz64(arg1);
90 #endif /* TARGET_MIPS64 */
92 /* 64 bits arithmetic for 32 bits hosts */
93 static inline uint64_t get_HILO (void)
95 return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
98 static inline void set_HILO (uint64_t HILO)
100 env->active_tc.LO[0] = (int32_t)HILO;
101 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
104 static inline void set_HIT0_LO (target_ulong arg1, uint64_t HILO)
106 env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
107 arg1 = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
110 static inline void set_HI_LOT0 (target_ulong arg1, uint64_t HILO)
112 arg1 = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
113 env->active_tc.HI[0] = (int32_t)(HILO >> 32);
116 /* Multiplication variants of the vr54xx. */
117 target_ulong helper_muls (target_ulong arg1, target_ulong arg2)
119 set_HI_LOT0(arg1, 0 - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
121 return arg1;
124 target_ulong helper_mulsu (target_ulong arg1, target_ulong arg2)
126 set_HI_LOT0(arg1, 0 - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
128 return arg1;
131 target_ulong helper_macc (target_ulong arg1, target_ulong arg2)
133 set_HI_LOT0(arg1, ((int64_t)get_HILO()) + ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
135 return arg1;
138 target_ulong helper_macchi (target_ulong arg1, target_ulong arg2)
140 set_HIT0_LO(arg1, ((int64_t)get_HILO()) + ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
142 return arg1;
145 target_ulong helper_maccu (target_ulong arg1, target_ulong arg2)
147 set_HI_LOT0(arg1, ((uint64_t)get_HILO()) + ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
149 return arg1;
152 target_ulong helper_macchiu (target_ulong arg1, target_ulong arg2)
154 set_HIT0_LO(arg1, ((uint64_t)get_HILO()) + ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
156 return arg1;
159 target_ulong helper_msac (target_ulong arg1, target_ulong arg2)
161 set_HI_LOT0(arg1, ((int64_t)get_HILO()) - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
163 return arg1;
166 target_ulong helper_msachi (target_ulong arg1, target_ulong arg2)
168 set_HIT0_LO(arg1, ((int64_t)get_HILO()) - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
170 return arg1;
173 target_ulong helper_msacu (target_ulong arg1, target_ulong arg2)
175 set_HI_LOT0(arg1, ((uint64_t)get_HILO()) - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
177 return arg1;
180 target_ulong helper_msachiu (target_ulong arg1, target_ulong arg2)
182 set_HIT0_LO(arg1, ((uint64_t)get_HILO()) - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
184 return arg1;
187 target_ulong helper_mulhi (target_ulong arg1, target_ulong arg2)
189 set_HIT0_LO(arg1, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
191 return arg1;
194 target_ulong helper_mulhiu (target_ulong arg1, target_ulong arg2)
196 set_HIT0_LO(arg1, (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
198 return arg1;
201 target_ulong helper_mulshi (target_ulong arg1, target_ulong arg2)
203 set_HIT0_LO(arg1, 0 - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
205 return arg1;
208 target_ulong helper_mulshiu (target_ulong arg1, target_ulong arg2)
210 set_HIT0_LO(arg1, 0 - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
212 return arg1;
215 #ifdef TARGET_MIPS64
216 void helper_dmult (target_ulong arg1, target_ulong arg2)
218 muls64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
221 void helper_dmultu (target_ulong arg1, target_ulong arg2)
223 mulu64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
225 #endif
227 #ifdef TARGET_WORDS_BIGENDIAN
228 #define GET_LMASK(v) ((v) & 3)
229 #define GET_OFFSET(addr, offset) (addr + (offset))
230 #else
231 #define GET_LMASK(v) (((v) & 3) ^ 3)
232 #define GET_OFFSET(addr, offset) (addr - (offset))
233 #endif
235 target_ulong helper_lwl(target_ulong arg1, target_ulong arg2, int mem_idx)
237 target_ulong tmp;
239 #ifdef CONFIG_USER_ONLY
240 #define ldfun ldub_raw
241 #else
242 int (*ldfun)(target_ulong);
244 switch (mem_idx)
246 case 0: ldfun = ldub_kernel; break;
247 case 1: ldfun = ldub_super; break;
248 default:
249 case 2: ldfun = ldub_user; break;
251 #endif
252 tmp = ldfun(arg2);
253 arg1 = (arg1 & 0x00FFFFFF) | (tmp << 24);
255 if (GET_LMASK(arg2) <= 2) {
256 tmp = ldfun(GET_OFFSET(arg2, 1));
257 arg1 = (arg1 & 0xFF00FFFF) | (tmp << 16);
260 if (GET_LMASK(arg2) <= 1) {
261 tmp = ldfun(GET_OFFSET(arg2, 2));
262 arg1 = (arg1 & 0xFFFF00FF) | (tmp << 8);
265 if (GET_LMASK(arg2) == 0) {
266 tmp = ldfun(GET_OFFSET(arg2, 3));
267 arg1 = (arg1 & 0xFFFFFF00) | tmp;
269 return (int32_t)arg1;
272 target_ulong helper_lwr(target_ulong arg1, target_ulong arg2, int mem_idx)
274 target_ulong tmp;
276 #ifdef CONFIG_USER_ONLY
277 #define ldfun ldub_raw
278 #else
279 int (*ldfun)(target_ulong);
281 switch (mem_idx)
283 case 0: ldfun = ldub_kernel; break;
284 case 1: ldfun = ldub_super; break;
285 default:
286 case 2: ldfun = ldub_user; break;
288 #endif
289 tmp = ldfun(arg2);
290 arg1 = (arg1 & 0xFFFFFF00) | tmp;
292 if (GET_LMASK(arg2) >= 1) {
293 tmp = ldfun(GET_OFFSET(arg2, -1));
294 arg1 = (arg1 & 0xFFFF00FF) | (tmp << 8);
297 if (GET_LMASK(arg2) >= 2) {
298 tmp = ldfun(GET_OFFSET(arg2, -2));
299 arg1 = (arg1 & 0xFF00FFFF) | (tmp << 16);
302 if (GET_LMASK(arg2) == 3) {
303 tmp = ldfun(GET_OFFSET(arg2, -3));
304 arg1 = (arg1 & 0x00FFFFFF) | (tmp << 24);
306 return (int32_t)arg1;
309 void helper_swl(target_ulong arg1, target_ulong arg2, int mem_idx)
311 #ifdef CONFIG_USER_ONLY
312 #define stfun stb_raw
313 #else
314 void (*stfun)(target_ulong, int);
316 switch (mem_idx)
318 case 0: stfun = stb_kernel; break;
319 case 1: stfun = stb_super; break;
320 default:
321 case 2: stfun = stb_user; break;
323 #endif
324 stfun(arg2, (uint8_t)(arg1 >> 24));
326 if (GET_LMASK(arg2) <= 2)
327 stfun(GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16));
329 if (GET_LMASK(arg2) <= 1)
330 stfun(GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8));
332 if (GET_LMASK(arg2) == 0)
333 stfun(GET_OFFSET(arg2, 3), (uint8_t)arg1);
336 void helper_swr(target_ulong arg1, target_ulong arg2, int mem_idx)
338 #ifdef CONFIG_USER_ONLY
339 #define stfun stb_raw
340 #else
341 void (*stfun)(target_ulong, int);
343 switch (mem_idx)
345 case 0: stfun = stb_kernel; break;
346 case 1: stfun = stb_super; break;
347 default:
348 case 2: stfun = stb_user; break;
350 #endif
351 stfun(arg2, (uint8_t)arg1);
353 if (GET_LMASK(arg2) >= 1)
354 stfun(GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8));
356 if (GET_LMASK(arg2) >= 2)
357 stfun(GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16));
359 if (GET_LMASK(arg2) == 3)
360 stfun(GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24));
363 #if defined(TARGET_MIPS64)
364 /* "half" load and stores. We must do the memory access inline,
365 or fault handling won't work. */
367 #ifdef TARGET_WORDS_BIGENDIAN
368 #define GET_LMASK64(v) ((v) & 7)
369 #else
370 #define GET_LMASK64(v) (((v) & 7) ^ 7)
371 #endif
373 target_ulong helper_ldl(target_ulong arg1, target_ulong arg2, int mem_idx)
375 uint64_t tmp;
377 #ifdef CONFIG_USER_ONLY
378 #define ldfun ldub_raw
379 #else
380 int (*ldfun)(target_ulong);
382 switch (mem_idx)
384 case 0: ldfun = ldub_kernel; break;
385 case 1: ldfun = ldub_super; break;
386 default:
387 case 2: ldfun = ldub_user; break;
389 #endif
390 tmp = ldfun(arg2);
391 arg1 = (arg1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
393 if (GET_LMASK64(arg2) <= 6) {
394 tmp = ldfun(GET_OFFSET(arg2, 1));
395 arg1 = (arg1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
398 if (GET_LMASK64(arg2) <= 5) {
399 tmp = ldfun(GET_OFFSET(arg2, 2));
400 arg1 = (arg1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
403 if (GET_LMASK64(arg2) <= 4) {
404 tmp = ldfun(GET_OFFSET(arg2, 3));
405 arg1 = (arg1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
408 if (GET_LMASK64(arg2) <= 3) {
409 tmp = ldfun(GET_OFFSET(arg2, 4));
410 arg1 = (arg1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
413 if (GET_LMASK64(arg2) <= 2) {
414 tmp = ldfun(GET_OFFSET(arg2, 5));
415 arg1 = (arg1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
418 if (GET_LMASK64(arg2) <= 1) {
419 tmp = ldfun(GET_OFFSET(arg2, 6));
420 arg1 = (arg1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
423 if (GET_LMASK64(arg2) == 0) {
424 tmp = ldfun(GET_OFFSET(arg2, 7));
425 arg1 = (arg1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
428 return arg1;
431 target_ulong helper_ldr(target_ulong arg1, target_ulong arg2, int mem_idx)
433 uint64_t tmp;
435 #ifdef CONFIG_USER_ONLY
436 #define ldfun ldub_raw
437 #else
438 int (*ldfun)(target_ulong);
440 switch (mem_idx)
442 case 0: ldfun = ldub_kernel; break;
443 case 1: ldfun = ldub_super; break;
444 default:
445 case 2: ldfun = ldub_user; break;
447 #endif
448 tmp = ldfun(arg2);
449 arg1 = (arg1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
451 if (GET_LMASK64(arg2) >= 1) {
452 tmp = ldfun(GET_OFFSET(arg2, -1));
453 arg1 = (arg1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
456 if (GET_LMASK64(arg2) >= 2) {
457 tmp = ldfun(GET_OFFSET(arg2, -2));
458 arg1 = (arg1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
461 if (GET_LMASK64(arg2) >= 3) {
462 tmp = ldfun(GET_OFFSET(arg2, -3));
463 arg1 = (arg1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
466 if (GET_LMASK64(arg2) >= 4) {
467 tmp = ldfun(GET_OFFSET(arg2, -4));
468 arg1 = (arg1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
471 if (GET_LMASK64(arg2) >= 5) {
472 tmp = ldfun(GET_OFFSET(arg2, -5));
473 arg1 = (arg1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
476 if (GET_LMASK64(arg2) >= 6) {
477 tmp = ldfun(GET_OFFSET(arg2, -6));
478 arg1 = (arg1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
481 if (GET_LMASK64(arg2) == 7) {
482 tmp = ldfun(GET_OFFSET(arg2, -7));
483 arg1 = (arg1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
486 return arg1;
489 void helper_sdl(target_ulong arg1, target_ulong arg2, int mem_idx)
491 #ifdef CONFIG_USER_ONLY
492 #define stfun stb_raw
493 #else
494 void (*stfun)(target_ulong, int);
496 switch (mem_idx)
498 case 0: stfun = stb_kernel; break;
499 case 1: stfun = stb_super; break;
500 default:
501 case 2: stfun = stb_user; break;
503 #endif
504 stfun(arg2, (uint8_t)(arg1 >> 56));
506 if (GET_LMASK64(arg2) <= 6)
507 stfun(GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48));
509 if (GET_LMASK64(arg2) <= 5)
510 stfun(GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40));
512 if (GET_LMASK64(arg2) <= 4)
513 stfun(GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32));
515 if (GET_LMASK64(arg2) <= 3)
516 stfun(GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24));
518 if (GET_LMASK64(arg2) <= 2)
519 stfun(GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16));
521 if (GET_LMASK64(arg2) <= 1)
522 stfun(GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8));
524 if (GET_LMASK64(arg2) <= 0)
525 stfun(GET_OFFSET(arg2, 7), (uint8_t)arg1);
528 void helper_sdr(target_ulong arg1, target_ulong arg2, int mem_idx)
530 #ifdef CONFIG_USER_ONLY
531 #define stfun stb_raw
532 #else
533 void (*stfun)(target_ulong, int);
535 switch (mem_idx)
537 case 0: stfun = stb_kernel; break;
538 case 1: stfun = stb_super; break;
539 default:
540 case 2: stfun = stb_user; break;
542 #endif
543 stfun(arg2, (uint8_t)arg1);
545 if (GET_LMASK64(arg2) >= 1)
546 stfun(GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8));
548 if (GET_LMASK64(arg2) >= 2)
549 stfun(GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16));
551 if (GET_LMASK64(arg2) >= 3)
552 stfun(GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24));
554 if (GET_LMASK64(arg2) >= 4)
555 stfun(GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32));
557 if (GET_LMASK64(arg2) >= 5)
558 stfun(GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40));
560 if (GET_LMASK64(arg2) >= 6)
561 stfun(GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48));
563 if (GET_LMASK64(arg2) == 7)
564 stfun(GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56));
566 #endif /* TARGET_MIPS64 */
568 #ifndef CONFIG_USER_ONLY
569 /* CP0 helpers */
570 target_ulong helper_mfc0_mvpcontrol (void)
572 return env->mvp->CP0_MVPControl;
575 target_ulong helper_mfc0_mvpconf0 (void)
577 return env->mvp->CP0_MVPConf0;
580 target_ulong helper_mfc0_mvpconf1 (void)
582 return env->mvp->CP0_MVPConf1;
585 target_ulong helper_mfc0_random (void)
587 return (int32_t)cpu_mips_get_random(env);
590 target_ulong helper_mfc0_tcstatus (void)
592 return env->active_tc.CP0_TCStatus;
595 target_ulong helper_mftc0_tcstatus(void)
597 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
599 if (other_tc == env->current_tc)
600 return env->active_tc.CP0_TCStatus;
601 else
602 return env->tcs[other_tc].CP0_TCStatus;
605 target_ulong helper_mfc0_tcbind (void)
607 return env->active_tc.CP0_TCBind;
610 target_ulong helper_mftc0_tcbind(void)
612 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
614 if (other_tc == env->current_tc)
615 return env->active_tc.CP0_TCBind;
616 else
617 return env->tcs[other_tc].CP0_TCBind;
620 target_ulong helper_mfc0_tcrestart (void)
622 return env->active_tc.PC;
625 target_ulong helper_mftc0_tcrestart(void)
627 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
629 if (other_tc == env->current_tc)
630 return env->active_tc.PC;
631 else
632 return env->tcs[other_tc].PC;
635 target_ulong helper_mfc0_tchalt (void)
637 return env->active_tc.CP0_TCHalt;
640 target_ulong helper_mftc0_tchalt(void)
642 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
644 if (other_tc == env->current_tc)
645 return env->active_tc.CP0_TCHalt;
646 else
647 return env->tcs[other_tc].CP0_TCHalt;
650 target_ulong helper_mfc0_tccontext (void)
652 return env->active_tc.CP0_TCContext;
655 target_ulong helper_mftc0_tccontext(void)
657 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
659 if (other_tc == env->current_tc)
660 return env->active_tc.CP0_TCContext;
661 else
662 return env->tcs[other_tc].CP0_TCContext;
665 target_ulong helper_mfc0_tcschedule (void)
667 return env->active_tc.CP0_TCSchedule;
670 target_ulong helper_mftc0_tcschedule(void)
672 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
674 if (other_tc == env->current_tc)
675 return env->active_tc.CP0_TCSchedule;
676 else
677 return env->tcs[other_tc].CP0_TCSchedule;
680 target_ulong helper_mfc0_tcschefback (void)
682 return env->active_tc.CP0_TCScheFBack;
685 target_ulong helper_mftc0_tcschefback(void)
687 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
689 if (other_tc == env->current_tc)
690 return env->active_tc.CP0_TCScheFBack;
691 else
692 return env->tcs[other_tc].CP0_TCScheFBack;
695 target_ulong helper_mfc0_count (void)
697 return (int32_t)cpu_mips_get_count(env);
700 target_ulong helper_mftc0_entryhi(void)
702 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
703 int32_t tcstatus;
705 if (other_tc == env->current_tc)
706 tcstatus = env->active_tc.CP0_TCStatus;
707 else
708 tcstatus = env->tcs[other_tc].CP0_TCStatus;
710 return (env->CP0_EntryHi & ~0xff) | (tcstatus & 0xff);
713 target_ulong helper_mftc0_status(void)
715 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
716 target_ulong t0;
717 int32_t tcstatus;
719 if (other_tc == env->current_tc)
720 tcstatus = env->active_tc.CP0_TCStatus;
721 else
722 tcstatus = env->tcs[other_tc].CP0_TCStatus;
724 t0 = env->CP0_Status & ~0xf1000018;
725 t0 |= tcstatus & (0xf << CP0TCSt_TCU0);
726 t0 |= (tcstatus & (1 << CP0TCSt_TMX)) >> (CP0TCSt_TMX - CP0St_MX);
727 t0 |= (tcstatus & (0x3 << CP0TCSt_TKSU)) >> (CP0TCSt_TKSU - CP0St_KSU);
729 return t0;
732 target_ulong helper_mfc0_lladdr (void)
734 return (int32_t)env->CP0_LLAddr >> 4;
737 target_ulong helper_mfc0_watchlo (uint32_t sel)
739 return (int32_t)env->CP0_WatchLo[sel];
742 target_ulong helper_mfc0_watchhi (uint32_t sel)
744 return env->CP0_WatchHi[sel];
747 target_ulong helper_mfc0_debug (void)
749 target_ulong t0 = env->CP0_Debug;
750 if (env->hflags & MIPS_HFLAG_DM)
751 t0 |= 1 << CP0DB_DM;
753 return t0;
756 target_ulong helper_mftc0_debug(void)
758 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
759 int32_t tcstatus;
761 if (other_tc == env->current_tc)
762 tcstatus = env->active_tc.CP0_Debug_tcstatus;
763 else
764 tcstatus = env->tcs[other_tc].CP0_Debug_tcstatus;
766 /* XXX: Might be wrong, check with EJTAG spec. */
767 return (env->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
768 (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
771 #if defined(TARGET_MIPS64)
772 target_ulong helper_dmfc0_tcrestart (void)
774 return env->active_tc.PC;
777 target_ulong helper_dmfc0_tchalt (void)
779 return env->active_tc.CP0_TCHalt;
782 target_ulong helper_dmfc0_tccontext (void)
784 return env->active_tc.CP0_TCContext;
787 target_ulong helper_dmfc0_tcschedule (void)
789 return env->active_tc.CP0_TCSchedule;
792 target_ulong helper_dmfc0_tcschefback (void)
794 return env->active_tc.CP0_TCScheFBack;
797 target_ulong helper_dmfc0_lladdr (void)
799 return env->CP0_LLAddr >> 4;
802 target_ulong helper_dmfc0_watchlo (uint32_t sel)
804 return env->CP0_WatchLo[sel];
806 #endif /* TARGET_MIPS64 */
808 void helper_mtc0_index (target_ulong arg1)
810 int num = 1;
811 unsigned int tmp = env->tlb->nb_tlb;
813 do {
814 tmp >>= 1;
815 num <<= 1;
816 } while (tmp);
817 env->CP0_Index = (env->CP0_Index & 0x80000000) | (arg1 & (num - 1));
820 void helper_mtc0_mvpcontrol (target_ulong arg1)
822 uint32_t mask = 0;
823 uint32_t newval;
825 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
826 mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
827 (1 << CP0MVPCo_EVP);
828 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
829 mask |= (1 << CP0MVPCo_STLB);
830 newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
832 // TODO: Enable/disable shared TLB, enable/disable VPEs.
834 env->mvp->CP0_MVPControl = newval;
837 void helper_mtc0_vpecontrol (target_ulong arg1)
839 uint32_t mask;
840 uint32_t newval;
842 mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
843 (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
844 newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
846 /* Yield scheduler intercept not implemented. */
847 /* Gating storage scheduler intercept not implemented. */
849 // TODO: Enable/disable TCs.
851 env->CP0_VPEControl = newval;
854 void helper_mtc0_vpeconf0 (target_ulong arg1)
856 uint32_t mask = 0;
857 uint32_t newval;
859 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
860 if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
861 mask |= (0xff << CP0VPEC0_XTC);
862 mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
864 newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
866 // TODO: TC exclusive handling due to ERL/EXL.
868 env->CP0_VPEConf0 = newval;
871 void helper_mtc0_vpeconf1 (target_ulong arg1)
873 uint32_t mask = 0;
874 uint32_t newval;
876 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
877 mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
878 (0xff << CP0VPEC1_NCP1);
879 newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
881 /* UDI not implemented. */
882 /* CP2 not implemented. */
884 // TODO: Handle FPU (CP1) binding.
886 env->CP0_VPEConf1 = newval;
889 void helper_mtc0_yqmask (target_ulong arg1)
891 /* Yield qualifier inputs not implemented. */
892 env->CP0_YQMask = 0x00000000;
895 void helper_mtc0_vpeopt (target_ulong arg1)
897 env->CP0_VPEOpt = arg1 & 0x0000ffff;
900 void helper_mtc0_entrylo0 (target_ulong arg1)
902 /* Large physaddr (PABITS) not implemented */
903 /* 1k pages not implemented */
904 env->CP0_EntryLo0 = arg1 & 0x3FFFFFFF;
907 void helper_mtc0_tcstatus (target_ulong arg1)
909 uint32_t mask = env->CP0_TCStatus_rw_bitmask;
910 uint32_t newval;
912 newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
914 // TODO: Sync with CP0_Status.
916 env->active_tc.CP0_TCStatus = newval;
919 void helper_mttc0_tcstatus (target_ulong arg1)
921 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
923 // TODO: Sync with CP0_Status.
925 if (other_tc == env->current_tc)
926 env->active_tc.CP0_TCStatus = arg1;
927 else
928 env->tcs[other_tc].CP0_TCStatus = arg1;
931 void helper_mtc0_tcbind (target_ulong arg1)
933 uint32_t mask = (1 << CP0TCBd_TBE);
934 uint32_t newval;
936 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
937 mask |= (1 << CP0TCBd_CurVPE);
938 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
939 env->active_tc.CP0_TCBind = newval;
942 void helper_mttc0_tcbind (target_ulong arg1)
944 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
945 uint32_t mask = (1 << CP0TCBd_TBE);
946 uint32_t newval;
948 if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
949 mask |= (1 << CP0TCBd_CurVPE);
950 if (other_tc == env->current_tc) {
951 newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
952 env->active_tc.CP0_TCBind = newval;
953 } else {
954 newval = (env->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
955 env->tcs[other_tc].CP0_TCBind = newval;
959 void helper_mtc0_tcrestart (target_ulong arg1)
961 env->active_tc.PC = arg1;
962 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
963 env->CP0_LLAddr = 0ULL;
964 /* MIPS16 not implemented. */
967 void helper_mttc0_tcrestart (target_ulong arg1)
969 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
971 if (other_tc == env->current_tc) {
972 env->active_tc.PC = arg1;
973 env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
974 env->CP0_LLAddr = 0ULL;
975 /* MIPS16 not implemented. */
976 } else {
977 env->tcs[other_tc].PC = arg1;
978 env->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
979 env->CP0_LLAddr = 0ULL;
980 /* MIPS16 not implemented. */
984 void helper_mtc0_tchalt (target_ulong arg1)
986 env->active_tc.CP0_TCHalt = arg1 & 0x1;
988 // TODO: Halt TC / Restart (if allocated+active) TC.
991 void helper_mttc0_tchalt (target_ulong arg1)
993 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
995 // TODO: Halt TC / Restart (if allocated+active) TC.
997 if (other_tc == env->current_tc)
998 env->active_tc.CP0_TCHalt = arg1;
999 else
1000 env->tcs[other_tc].CP0_TCHalt = arg1;
1003 void helper_mtc0_tccontext (target_ulong arg1)
1005 env->active_tc.CP0_TCContext = arg1;
1008 void helper_mttc0_tccontext (target_ulong arg1)
1010 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1012 if (other_tc == env->current_tc)
1013 env->active_tc.CP0_TCContext = arg1;
1014 else
1015 env->tcs[other_tc].CP0_TCContext = arg1;
1018 void helper_mtc0_tcschedule (target_ulong arg1)
1020 env->active_tc.CP0_TCSchedule = arg1;
1023 void helper_mttc0_tcschedule (target_ulong arg1)
1025 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1027 if (other_tc == env->current_tc)
1028 env->active_tc.CP0_TCSchedule = arg1;
1029 else
1030 env->tcs[other_tc].CP0_TCSchedule = arg1;
1033 void helper_mtc0_tcschefback (target_ulong arg1)
1035 env->active_tc.CP0_TCScheFBack = arg1;
1038 void helper_mttc0_tcschefback (target_ulong arg1)
1040 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1042 if (other_tc == env->current_tc)
1043 env->active_tc.CP0_TCScheFBack = arg1;
1044 else
1045 env->tcs[other_tc].CP0_TCScheFBack = arg1;
1048 void helper_mtc0_entrylo1 (target_ulong arg1)
1050 /* Large physaddr (PABITS) not implemented */
1051 /* 1k pages not implemented */
1052 env->CP0_EntryLo1 = arg1 & 0x3FFFFFFF;
1055 void helper_mtc0_context (target_ulong arg1)
1057 env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1060 void helper_mtc0_pagemask (target_ulong arg1)
1062 /* 1k pages not implemented */
1063 env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1066 void helper_mtc0_pagegrain (target_ulong arg1)
1068 /* SmartMIPS not implemented */
1069 /* Large physaddr (PABITS) not implemented */
1070 /* 1k pages not implemented */
1071 env->CP0_PageGrain = 0;
1074 void helper_mtc0_wired (target_ulong arg1)
1076 env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1079 void helper_mtc0_srsconf0 (target_ulong arg1)
1081 env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1084 void helper_mtc0_srsconf1 (target_ulong arg1)
1086 env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1089 void helper_mtc0_srsconf2 (target_ulong arg1)
1091 env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1094 void helper_mtc0_srsconf3 (target_ulong arg1)
1096 env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1099 void helper_mtc0_srsconf4 (target_ulong arg1)
1101 env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1104 void helper_mtc0_hwrena (target_ulong arg1)
1106 env->CP0_HWREna = arg1 & 0x0000000F;
1109 void helper_mtc0_count (target_ulong arg1)
1111 cpu_mips_store_count(env, arg1);
1114 void helper_mtc0_entryhi (target_ulong arg1)
1116 target_ulong old, val;
1118 /* 1k pages not implemented */
1119 val = arg1 & ((TARGET_PAGE_MASK << 1) | 0xFF);
1120 #if defined(TARGET_MIPS64)
1121 val &= env->SEGMask;
1122 #endif
1123 old = env->CP0_EntryHi;
1124 env->CP0_EntryHi = val;
1125 if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1126 uint32_t tcst = env->active_tc.CP0_TCStatus & ~0xff;
1127 env->active_tc.CP0_TCStatus = tcst | (val & 0xff);
1129 /* If the ASID changes, flush qemu's TLB. */
1130 if ((old & 0xFF) != (val & 0xFF))
1131 cpu_mips_tlb_flush(env, 1);
1134 void helper_mttc0_entryhi(target_ulong arg1)
1136 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1137 int32_t tcstatus;
1139 env->CP0_EntryHi = (env->CP0_EntryHi & 0xff) | (arg1 & ~0xff);
1140 if (other_tc == env->current_tc) {
1141 tcstatus = (env->active_tc.CP0_TCStatus & ~0xff) | (arg1 & 0xff);
1142 env->active_tc.CP0_TCStatus = tcstatus;
1143 } else {
1144 tcstatus = (env->tcs[other_tc].CP0_TCStatus & ~0xff) | (arg1 & 0xff);
1145 env->tcs[other_tc].CP0_TCStatus = tcstatus;
1149 void helper_mtc0_compare (target_ulong arg1)
1151 cpu_mips_store_compare(env, arg1);
1154 void helper_mtc0_status (target_ulong arg1)
1156 uint32_t val, old;
1157 uint32_t mask = env->CP0_Status_rw_bitmask;
1159 val = arg1 & mask;
1160 old = env->CP0_Status;
1161 env->CP0_Status = (env->CP0_Status & ~mask) | val;
1162 compute_hflags(env);
1163 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1164 qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
1165 old, old & env->CP0_Cause & CP0Ca_IP_mask,
1166 val, val & env->CP0_Cause & CP0Ca_IP_mask,
1167 env->CP0_Cause);
1168 switch (env->hflags & MIPS_HFLAG_KSU) {
1169 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1170 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1171 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1172 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
1175 cpu_mips_update_irq(env);
1178 void helper_mttc0_status(target_ulong arg1)
1180 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1181 int32_t tcstatus = env->tcs[other_tc].CP0_TCStatus;
1183 env->CP0_Status = arg1 & ~0xf1000018;
1184 tcstatus = (tcstatus & ~(0xf << CP0TCSt_TCU0)) | (arg1 & (0xf << CP0St_CU0));
1185 tcstatus = (tcstatus & ~(1 << CP0TCSt_TMX)) | ((arg1 & (1 << CP0St_MX)) << (CP0TCSt_TMX - CP0St_MX));
1186 tcstatus = (tcstatus & ~(0x3 << CP0TCSt_TKSU)) | ((arg1 & (0x3 << CP0St_KSU)) << (CP0TCSt_TKSU - CP0St_KSU));
1187 if (other_tc == env->current_tc)
1188 env->active_tc.CP0_TCStatus = tcstatus;
1189 else
1190 env->tcs[other_tc].CP0_TCStatus = tcstatus;
1193 void helper_mtc0_intctl (target_ulong arg1)
1195 /* vectored interrupts not implemented, no performance counters. */
1196 env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000002e0) | (arg1 & 0x000002e0);
1199 void helper_mtc0_srsctl (target_ulong arg1)
1201 uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1202 env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1205 void helper_mtc0_cause (target_ulong arg1)
1207 uint32_t mask = 0x00C00300;
1208 uint32_t old = env->CP0_Cause;
1210 if (env->insn_flags & ISA_MIPS32R2)
1211 mask |= 1 << CP0Ca_DC;
1213 env->CP0_Cause = (env->CP0_Cause & ~mask) | (arg1 & mask);
1215 if ((old ^ env->CP0_Cause) & (1 << CP0Ca_DC)) {
1216 if (env->CP0_Cause & (1 << CP0Ca_DC))
1217 cpu_mips_stop_count(env);
1218 else
1219 cpu_mips_start_count(env);
1222 /* Handle the software interrupt as an hardware one, as they
1223 are very similar */
1224 if (arg1 & CP0Ca_IP_mask) {
1225 cpu_mips_update_irq(env);
1229 void helper_mtc0_ebase (target_ulong arg1)
1231 /* vectored interrupts not implemented */
1232 /* Multi-CPU not implemented */
1233 env->CP0_EBase = 0x80000000 | (arg1 & 0x3FFFF000);
1236 void helper_mtc0_config0 (target_ulong arg1)
1238 env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1241 void helper_mtc0_config2 (target_ulong arg1)
1243 /* tertiary/secondary caches not implemented */
1244 env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
1247 void helper_mtc0_watchlo (target_ulong arg1, uint32_t sel)
1249 /* Watch exceptions for instructions, data loads, data stores
1250 not implemented. */
1251 env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1254 void helper_mtc0_watchhi (target_ulong arg1, uint32_t sel)
1256 env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
1257 env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1260 void helper_mtc0_xcontext (target_ulong arg1)
1262 target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1263 env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1266 void helper_mtc0_framemask (target_ulong arg1)
1268 env->CP0_Framemask = arg1; /* XXX */
1271 void helper_mtc0_debug (target_ulong arg1)
1273 env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
1274 if (arg1 & (1 << CP0DB_DM))
1275 env->hflags |= MIPS_HFLAG_DM;
1276 else
1277 env->hflags &= ~MIPS_HFLAG_DM;
1280 void helper_mttc0_debug(target_ulong arg1)
1282 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1283 uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1285 /* XXX: Might be wrong, check with EJTAG spec. */
1286 if (other_tc == env->current_tc)
1287 env->active_tc.CP0_Debug_tcstatus = val;
1288 else
1289 env->tcs[other_tc].CP0_Debug_tcstatus = val;
1290 env->CP0_Debug = (env->CP0_Debug & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1291 (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1294 void helper_mtc0_performance0 (target_ulong arg1)
1296 env->CP0_Performance0 = arg1 & 0x000007ff;
1299 void helper_mtc0_taglo (target_ulong arg1)
1301 env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1304 void helper_mtc0_datalo (target_ulong arg1)
1306 env->CP0_DataLo = arg1; /* XXX */
1309 void helper_mtc0_taghi (target_ulong arg1)
1311 env->CP0_TagHi = arg1; /* XXX */
1314 void helper_mtc0_datahi (target_ulong arg1)
1316 env->CP0_DataHi = arg1; /* XXX */
1319 /* MIPS MT functions */
1320 target_ulong helper_mftgpr(uint32_t sel)
1322 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1324 if (other_tc == env->current_tc)
1325 return env->active_tc.gpr[sel];
1326 else
1327 return env->tcs[other_tc].gpr[sel];
1330 target_ulong helper_mftlo(uint32_t sel)
1332 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1334 if (other_tc == env->current_tc)
1335 return env->active_tc.LO[sel];
1336 else
1337 return env->tcs[other_tc].LO[sel];
1340 target_ulong helper_mfthi(uint32_t sel)
1342 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1344 if (other_tc == env->current_tc)
1345 return env->active_tc.HI[sel];
1346 else
1347 return env->tcs[other_tc].HI[sel];
1350 target_ulong helper_mftacx(uint32_t sel)
1352 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1354 if (other_tc == env->current_tc)
1355 return env->active_tc.ACX[sel];
1356 else
1357 return env->tcs[other_tc].ACX[sel];
1360 target_ulong helper_mftdsp(void)
1362 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1364 if (other_tc == env->current_tc)
1365 return env->active_tc.DSPControl;
1366 else
1367 return env->tcs[other_tc].DSPControl;
1370 void helper_mttgpr(target_ulong arg1, uint32_t sel)
1372 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1374 if (other_tc == env->current_tc)
1375 env->active_tc.gpr[sel] = arg1;
1376 else
1377 env->tcs[other_tc].gpr[sel] = arg1;
1380 void helper_mttlo(target_ulong arg1, uint32_t sel)
1382 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1384 if (other_tc == env->current_tc)
1385 env->active_tc.LO[sel] = arg1;
1386 else
1387 env->tcs[other_tc].LO[sel] = arg1;
1390 void helper_mtthi(target_ulong arg1, uint32_t sel)
1392 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1394 if (other_tc == env->current_tc)
1395 env->active_tc.HI[sel] = arg1;
1396 else
1397 env->tcs[other_tc].HI[sel] = arg1;
1400 void helper_mttacx(target_ulong arg1, uint32_t sel)
1402 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1404 if (other_tc == env->current_tc)
1405 env->active_tc.ACX[sel] = arg1;
1406 else
1407 env->tcs[other_tc].ACX[sel] = arg1;
1410 void helper_mttdsp(target_ulong arg1)
1412 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1414 if (other_tc == env->current_tc)
1415 env->active_tc.DSPControl = arg1;
1416 else
1417 env->tcs[other_tc].DSPControl = arg1;
1420 /* MIPS MT functions */
1421 target_ulong helper_dmt(target_ulong arg1)
1423 // TODO
1424 arg1 = 0;
1425 // rt = arg1
1427 return arg1;
1430 target_ulong helper_emt(target_ulong arg1)
1432 // TODO
1433 arg1 = 0;
1434 // rt = arg1
1436 return arg1;
1439 target_ulong helper_dvpe(target_ulong arg1)
1441 // TODO
1442 arg1 = 0;
1443 // rt = arg1
1445 return arg1;
1448 target_ulong helper_evpe(target_ulong arg1)
1450 // TODO
1451 arg1 = 0;
1452 // rt = arg1
1454 return arg1;
1456 #endif /* !CONFIG_USER_ONLY */
1458 void helper_fork(target_ulong arg1, target_ulong arg2)
1460 // arg1 = rt, arg2 = rs
1461 arg1 = 0;
1462 // TODO: store to TC register
1465 target_ulong helper_yield(target_ulong arg1)
1467 if (arg1 < 0) {
1468 /* No scheduling policy implemented. */
1469 if (arg1 != -2) {
1470 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1471 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1472 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1473 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1474 helper_raise_exception(EXCP_THREAD);
1477 } else if (arg1 == 0) {
1478 if (0 /* TODO: TC underflow */) {
1479 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1480 helper_raise_exception(EXCP_THREAD);
1481 } else {
1482 // TODO: Deallocate TC
1484 } else if (arg1 > 0) {
1485 /* Yield qualifier inputs not implemented. */
1486 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1487 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1488 helper_raise_exception(EXCP_THREAD);
1490 return env->CP0_YQMask;
1493 #ifndef CONFIG_USER_ONLY
1494 /* TLB management */
1495 void cpu_mips_tlb_flush (CPUState *env, int flush_global)
1497 /* Flush qemu's TLB and discard all shadowed entries. */
1498 tlb_flush (env, flush_global);
1499 env->tlb->tlb_in_use = env->tlb->nb_tlb;
1502 static void r4k_mips_tlb_flush_extra (CPUState *env, int first)
1504 /* Discard entries from env->tlb[first] onwards. */
1505 while (env->tlb->tlb_in_use > first) {
1506 r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1510 static void r4k_fill_tlb (int idx)
1512 r4k_tlb_t *tlb;
1514 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1515 tlb = &env->tlb->mmu.r4k.tlb[idx];
1516 tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1517 #if defined(TARGET_MIPS64)
1518 tlb->VPN &= env->SEGMask;
1519 #endif
1520 tlb->ASID = env->CP0_EntryHi & 0xFF;
1521 tlb->PageMask = env->CP0_PageMask;
1522 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1523 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
1524 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
1525 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
1526 tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
1527 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
1528 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
1529 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
1530 tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
1533 void r4k_helper_tlbwi (void)
1535 int idx;
1537 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1539 /* Discard cached TLB entries. We could avoid doing this if the
1540 tlbwi is just upgrading access permissions on the current entry;
1541 that might be a further win. */
1542 r4k_mips_tlb_flush_extra (env, env->tlb->nb_tlb);
1544 r4k_invalidate_tlb(env, idx, 0);
1545 r4k_fill_tlb(idx);
1548 void r4k_helper_tlbwr (void)
1550 int r = cpu_mips_get_random(env);
1552 r4k_invalidate_tlb(env, r, 1);
1553 r4k_fill_tlb(r);
1556 void r4k_helper_tlbp (void)
1558 r4k_tlb_t *tlb;
1559 target_ulong mask;
1560 target_ulong tag;
1561 target_ulong VPN;
1562 uint8_t ASID;
1563 int i;
1565 ASID = env->CP0_EntryHi & 0xFF;
1566 for (i = 0; i < env->tlb->nb_tlb; i++) {
1567 tlb = &env->tlb->mmu.r4k.tlb[i];
1568 /* 1k pages are not supported. */
1569 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1570 tag = env->CP0_EntryHi & ~mask;
1571 VPN = tlb->VPN & ~mask;
1572 /* Check ASID, virtual page number & size */
1573 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1574 /* TLB match */
1575 env->CP0_Index = i;
1576 break;
1579 if (i == env->tlb->nb_tlb) {
1580 /* No match. Discard any shadow entries, if any of them match. */
1581 for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
1582 tlb = &env->tlb->mmu.r4k.tlb[i];
1583 /* 1k pages are not supported. */
1584 mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
1585 tag = env->CP0_EntryHi & ~mask;
1586 VPN = tlb->VPN & ~mask;
1587 /* Check ASID, virtual page number & size */
1588 if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1589 r4k_mips_tlb_flush_extra (env, i);
1590 break;
1594 env->CP0_Index |= 0x80000000;
1598 void r4k_helper_tlbr (void)
1600 r4k_tlb_t *tlb;
1601 uint8_t ASID;
1602 int idx;
1604 ASID = env->CP0_EntryHi & 0xFF;
1605 idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
1606 tlb = &env->tlb->mmu.r4k.tlb[idx];
1608 /* If this will change the current ASID, flush qemu's TLB. */
1609 if (ASID != tlb->ASID)
1610 cpu_mips_tlb_flush (env, 1);
1612 r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
1614 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
1615 env->CP0_PageMask = tlb->PageMask;
1616 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
1617 (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
1618 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
1619 (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
1622 void helper_tlbwi(void)
1624 env->tlb->helper_tlbwi();
1627 void helper_tlbwr(void)
1629 env->tlb->helper_tlbwr();
1632 void helper_tlbp(void)
1634 env->tlb->helper_tlbp();
1637 void helper_tlbr(void)
1639 env->tlb->helper_tlbr();
1642 /* Specials */
1643 target_ulong helper_di (void)
1645 target_ulong t0 = env->CP0_Status;
1647 env->CP0_Status = t0 & ~(1 << CP0St_IE);
1648 cpu_mips_update_irq(env);
1650 return t0;
1653 target_ulong helper_ei (void)
1655 target_ulong t0 = env->CP0_Status;
1657 env->CP0_Status = t0 | (1 << CP0St_IE);
1658 cpu_mips_update_irq(env);
1660 return t0;
1663 static void debug_pre_eret (void)
1665 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1666 qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
1667 env->active_tc.PC, env->CP0_EPC);
1668 if (env->CP0_Status & (1 << CP0St_ERL))
1669 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
1670 if (env->hflags & MIPS_HFLAG_DM)
1671 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
1672 qemu_log("\n");
1676 static void debug_post_eret (void)
1678 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1679 qemu_log(" => PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
1680 env->active_tc.PC, env->CP0_EPC);
1681 if (env->CP0_Status & (1 << CP0St_ERL))
1682 qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
1683 if (env->hflags & MIPS_HFLAG_DM)
1684 qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
1685 switch (env->hflags & MIPS_HFLAG_KSU) {
1686 case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
1687 case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
1688 case MIPS_HFLAG_KM: qemu_log("\n"); break;
1689 default: cpu_abort(env, "Invalid MMU mode!\n"); break;
1694 void helper_eret (void)
1696 debug_pre_eret();
1697 if (env->CP0_Status & (1 << CP0St_ERL)) {
1698 env->active_tc.PC = env->CP0_ErrorEPC;
1699 env->CP0_Status &= ~(1 << CP0St_ERL);
1700 } else {
1701 env->active_tc.PC = env->CP0_EPC;
1702 env->CP0_Status &= ~(1 << CP0St_EXL);
1704 compute_hflags(env);
1705 debug_post_eret();
1706 env->CP0_LLAddr = 1;
1709 void helper_deret (void)
1711 debug_pre_eret();
1712 env->active_tc.PC = env->CP0_DEPC;
1713 env->hflags &= MIPS_HFLAG_DM;
1714 compute_hflags(env);
1715 debug_post_eret();
1716 env->CP0_LLAddr = 1;
1718 #endif /* !CONFIG_USER_ONLY */
1720 target_ulong helper_rdhwr_cpunum(void)
1722 if ((env->hflags & MIPS_HFLAG_CP0) ||
1723 (env->CP0_HWREna & (1 << 0)))
1724 return env->CP0_EBase & 0x3ff;
1725 else
1726 helper_raise_exception(EXCP_RI);
1728 return 0;
1731 target_ulong helper_rdhwr_synci_step(void)
1733 if ((env->hflags & MIPS_HFLAG_CP0) ||
1734 (env->CP0_HWREna & (1 << 1)))
1735 return env->SYNCI_Step;
1736 else
1737 helper_raise_exception(EXCP_RI);
1739 return 0;
1742 target_ulong helper_rdhwr_cc(void)
1744 if ((env->hflags & MIPS_HFLAG_CP0) ||
1745 (env->CP0_HWREna & (1 << 2)))
1746 return env->CP0_Count;
1747 else
1748 helper_raise_exception(EXCP_RI);
1750 return 0;
1753 target_ulong helper_rdhwr_ccres(void)
1755 if ((env->hflags & MIPS_HFLAG_CP0) ||
1756 (env->CP0_HWREna & (1 << 3)))
1757 return env->CCRes;
1758 else
1759 helper_raise_exception(EXCP_RI);
1761 return 0;
1764 void helper_pmon (int function)
1766 function /= 2;
1767 switch (function) {
1768 case 2: /* TODO: char inbyte(int waitflag); */
1769 if (env->active_tc.gpr[4] == 0)
1770 env->active_tc.gpr[2] = -1;
1771 /* Fall through */
1772 case 11: /* TODO: char inbyte (void); */
1773 env->active_tc.gpr[2] = -1;
1774 break;
1775 case 3:
1776 case 12:
1777 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
1778 break;
1779 case 17:
1780 break;
1781 case 158:
1783 unsigned char *fmt = (void *)(unsigned long)env->active_tc.gpr[4];
1784 printf("%s", fmt);
1786 break;
1790 void helper_wait (void)
1792 env->halted = 1;
1793 helper_raise_exception(EXCP_HLT);
1796 #if !defined(CONFIG_USER_ONLY)
1798 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
1800 #define MMUSUFFIX _mmu
1801 #define ALIGNED_ONLY
1803 #define SHIFT 0
1804 #include "softmmu_template.h"
1806 #define SHIFT 1
1807 #include "softmmu_template.h"
1809 #define SHIFT 2
1810 #include "softmmu_template.h"
1812 #define SHIFT 3
1813 #include "softmmu_template.h"
1815 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
1817 env->CP0_BadVAddr = addr;
1818 do_restore_state (retaddr);
1819 helper_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
1822 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1824 TranslationBlock *tb;
1825 CPUState *saved_env;
1826 unsigned long pc;
1827 int ret;
1829 /* XXX: hack to restore env in all cases, even if not called from
1830 generated code */
1831 saved_env = env;
1832 env = cpu_single_env;
1833 ret = cpu_mips_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1834 if (ret) {
1835 if (retaddr) {
1836 /* now we have a real cpu fault */
1837 pc = (unsigned long)retaddr;
1838 tb = tb_find_pc(pc);
1839 if (tb) {
1840 /* the PC is inside the translated code. It means that we have
1841 a virtual CPU fault */
1842 cpu_restore_state(tb, env, pc, NULL);
1845 helper_raise_exception_err(env->exception_index, env->error_code);
1847 env = saved_env;
1850 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1851 int unused, int size)
1853 if (is_exec)
1854 helper_raise_exception(EXCP_IBE);
1855 else
1856 helper_raise_exception(EXCP_DBE);
1858 #endif /* !CONFIG_USER_ONLY */
1860 /* Complex FPU operations which may need stack space. */
1862 #define FLOAT_ONE32 make_float32(0x3f8 << 20)
1863 #define FLOAT_ONE64 make_float64(0x3ffULL << 52)
1864 #define FLOAT_TWO32 make_float32(1 << 30)
1865 #define FLOAT_TWO64 make_float64(1ULL << 62)
1866 #define FLOAT_QNAN32 0x7fbfffff
1867 #define FLOAT_QNAN64 0x7ff7ffffffffffffULL
1868 #define FLOAT_SNAN32 0x7fffffff
1869 #define FLOAT_SNAN64 0x7fffffffffffffffULL
1871 /* convert MIPS rounding mode in FCR31 to IEEE library */
1872 unsigned int ieee_rm[] = {
1873 float_round_nearest_even,
1874 float_round_to_zero,
1875 float_round_up,
1876 float_round_down
1879 #define RESTORE_ROUNDING_MODE \
1880 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1882 #define RESTORE_FLUSH_MODE \
1883 set_flush_to_zero((env->active_fpu.fcr31 & (1 << 24)) != 0, &env->active_fpu.fp_status);
1885 target_ulong helper_cfc1 (uint32_t reg)
1887 target_ulong arg1;
1889 switch (reg) {
1890 case 0:
1891 arg1 = (int32_t)env->active_fpu.fcr0;
1892 break;
1893 case 25:
1894 arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
1895 break;
1896 case 26:
1897 arg1 = env->active_fpu.fcr31 & 0x0003f07c;
1898 break;
1899 case 28:
1900 arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
1901 break;
1902 default:
1903 arg1 = (int32_t)env->active_fpu.fcr31;
1904 break;
1907 return arg1;
1910 void helper_ctc1 (target_ulong arg1, uint32_t reg)
1912 switch(reg) {
1913 case 25:
1914 if (arg1 & 0xffffff00)
1915 return;
1916 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
1917 ((arg1 & 0x1) << 23);
1918 break;
1919 case 26:
1920 if (arg1 & 0x007c0000)
1921 return;
1922 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
1923 break;
1924 case 28:
1925 if (arg1 & 0x007c0000)
1926 return;
1927 env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
1928 ((arg1 & 0x4) << 22);
1929 break;
1930 case 31:
1931 if (arg1 & 0x007c0000)
1932 return;
1933 env->active_fpu.fcr31 = arg1;
1934 break;
1935 default:
1936 return;
1938 /* set rounding mode */
1939 RESTORE_ROUNDING_MODE;
1940 /* set flush-to-zero mode */
1941 RESTORE_FLUSH_MODE;
1942 set_float_exception_flags(0, &env->active_fpu.fp_status);
1943 if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
1944 helper_raise_exception(EXCP_FPE);
1947 static inline char ieee_ex_to_mips(char xcpt)
1949 return (xcpt & float_flag_inexact) >> 5 |
1950 (xcpt & float_flag_underflow) >> 3 |
1951 (xcpt & float_flag_overflow) >> 1 |
1952 (xcpt & float_flag_divbyzero) << 1 |
1953 (xcpt & float_flag_invalid) << 4;
1956 static inline char mips_ex_to_ieee(char xcpt)
1958 return (xcpt & FP_INEXACT) << 5 |
1959 (xcpt & FP_UNDERFLOW) << 3 |
1960 (xcpt & FP_OVERFLOW) << 1 |
1961 (xcpt & FP_DIV0) >> 1 |
1962 (xcpt & FP_INVALID) >> 4;
1965 static inline void update_fcr31(void)
1967 int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
1969 SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
1970 if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp)
1971 helper_raise_exception(EXCP_FPE);
1972 else
1973 UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
1976 /* Float support.
1977 Single precition routines have a "s" suffix, double precision a
1978 "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
1979 paired single lower "pl", paired single upper "pu". */
1981 /* unary operations, modifying fp status */
1982 uint64_t helper_float_sqrt_d(uint64_t fdt0)
1984 return float64_sqrt(fdt0, &env->active_fpu.fp_status);
1987 uint32_t helper_float_sqrt_s(uint32_t fst0)
1989 return float32_sqrt(fst0, &env->active_fpu.fp_status);
1992 uint64_t helper_float_cvtd_s(uint32_t fst0)
1994 uint64_t fdt2;
1996 set_float_exception_flags(0, &env->active_fpu.fp_status);
1997 fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
1998 update_fcr31();
1999 return fdt2;
2002 uint64_t helper_float_cvtd_w(uint32_t wt0)
2004 uint64_t fdt2;
2006 set_float_exception_flags(0, &env->active_fpu.fp_status);
2007 fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2008 update_fcr31();
2009 return fdt2;
2012 uint64_t helper_float_cvtd_l(uint64_t dt0)
2014 uint64_t fdt2;
2016 set_float_exception_flags(0, &env->active_fpu.fp_status);
2017 fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2018 update_fcr31();
2019 return fdt2;
2022 uint64_t helper_float_cvtl_d(uint64_t fdt0)
2024 uint64_t dt2;
2026 set_float_exception_flags(0, &env->active_fpu.fp_status);
2027 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2028 update_fcr31();
2029 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2030 dt2 = FLOAT_SNAN64;
2031 return dt2;
2034 uint64_t helper_float_cvtl_s(uint32_t fst0)
2036 uint64_t dt2;
2038 set_float_exception_flags(0, &env->active_fpu.fp_status);
2039 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2040 update_fcr31();
2041 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2042 dt2 = FLOAT_SNAN64;
2043 return dt2;
2046 uint64_t helper_float_cvtps_pw(uint64_t dt0)
2048 uint32_t fst2;
2049 uint32_t fsth2;
2051 set_float_exception_flags(0, &env->active_fpu.fp_status);
2052 fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2053 fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2054 update_fcr31();
2055 return ((uint64_t)fsth2 << 32) | fst2;
2058 uint64_t helper_float_cvtpw_ps(uint64_t fdt0)
2060 uint32_t wt2;
2061 uint32_t wth2;
2063 set_float_exception_flags(0, &env->active_fpu.fp_status);
2064 wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2065 wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2066 update_fcr31();
2067 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID)) {
2068 wt2 = FLOAT_SNAN32;
2069 wth2 = FLOAT_SNAN32;
2071 return ((uint64_t)wth2 << 32) | wt2;
2074 uint32_t helper_float_cvts_d(uint64_t fdt0)
2076 uint32_t fst2;
2078 set_float_exception_flags(0, &env->active_fpu.fp_status);
2079 fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2080 update_fcr31();
2081 return fst2;
2084 uint32_t helper_float_cvts_w(uint32_t wt0)
2086 uint32_t fst2;
2088 set_float_exception_flags(0, &env->active_fpu.fp_status);
2089 fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2090 update_fcr31();
2091 return fst2;
2094 uint32_t helper_float_cvts_l(uint64_t dt0)
2096 uint32_t fst2;
2098 set_float_exception_flags(0, &env->active_fpu.fp_status);
2099 fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2100 update_fcr31();
2101 return fst2;
2104 uint32_t helper_float_cvts_pl(uint32_t wt0)
2106 uint32_t wt2;
2108 set_float_exception_flags(0, &env->active_fpu.fp_status);
2109 wt2 = wt0;
2110 update_fcr31();
2111 return wt2;
2114 uint32_t helper_float_cvts_pu(uint32_t wth0)
2116 uint32_t wt2;
2118 set_float_exception_flags(0, &env->active_fpu.fp_status);
2119 wt2 = wth0;
2120 update_fcr31();
2121 return wt2;
2124 uint32_t helper_float_cvtw_s(uint32_t fst0)
2126 uint32_t wt2;
2128 set_float_exception_flags(0, &env->active_fpu.fp_status);
2129 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2130 update_fcr31();
2131 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2132 wt2 = FLOAT_SNAN32;
2133 return wt2;
2136 uint32_t helper_float_cvtw_d(uint64_t fdt0)
2138 uint32_t wt2;
2140 set_float_exception_flags(0, &env->active_fpu.fp_status);
2141 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2142 update_fcr31();
2143 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2144 wt2 = FLOAT_SNAN32;
2145 return wt2;
2148 uint64_t helper_float_roundl_d(uint64_t fdt0)
2150 uint64_t dt2;
2152 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2153 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2154 RESTORE_ROUNDING_MODE;
2155 update_fcr31();
2156 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2157 dt2 = FLOAT_SNAN64;
2158 return dt2;
2161 uint64_t helper_float_roundl_s(uint32_t fst0)
2163 uint64_t dt2;
2165 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2166 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2167 RESTORE_ROUNDING_MODE;
2168 update_fcr31();
2169 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2170 dt2 = FLOAT_SNAN64;
2171 return dt2;
2174 uint32_t helper_float_roundw_d(uint64_t fdt0)
2176 uint32_t wt2;
2178 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2179 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2180 RESTORE_ROUNDING_MODE;
2181 update_fcr31();
2182 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2183 wt2 = FLOAT_SNAN32;
2184 return wt2;
2187 uint32_t helper_float_roundw_s(uint32_t fst0)
2189 uint32_t wt2;
2191 set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
2192 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2193 RESTORE_ROUNDING_MODE;
2194 update_fcr31();
2195 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2196 wt2 = FLOAT_SNAN32;
2197 return wt2;
2200 uint64_t helper_float_truncl_d(uint64_t fdt0)
2202 uint64_t dt2;
2204 dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2205 update_fcr31();
2206 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2207 dt2 = FLOAT_SNAN64;
2208 return dt2;
2211 uint64_t helper_float_truncl_s(uint32_t fst0)
2213 uint64_t dt2;
2215 dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2216 update_fcr31();
2217 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2218 dt2 = FLOAT_SNAN64;
2219 return dt2;
2222 uint32_t helper_float_truncw_d(uint64_t fdt0)
2224 uint32_t wt2;
2226 wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2227 update_fcr31();
2228 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2229 wt2 = FLOAT_SNAN32;
2230 return wt2;
2233 uint32_t helper_float_truncw_s(uint32_t fst0)
2235 uint32_t wt2;
2237 wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2238 update_fcr31();
2239 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2240 wt2 = FLOAT_SNAN32;
2241 return wt2;
2244 uint64_t helper_float_ceill_d(uint64_t fdt0)
2246 uint64_t dt2;
2248 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2249 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2250 RESTORE_ROUNDING_MODE;
2251 update_fcr31();
2252 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2253 dt2 = FLOAT_SNAN64;
2254 return dt2;
2257 uint64_t helper_float_ceill_s(uint32_t fst0)
2259 uint64_t dt2;
2261 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2262 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2263 RESTORE_ROUNDING_MODE;
2264 update_fcr31();
2265 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2266 dt2 = FLOAT_SNAN64;
2267 return dt2;
2270 uint32_t helper_float_ceilw_d(uint64_t fdt0)
2272 uint32_t wt2;
2274 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2275 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2276 RESTORE_ROUNDING_MODE;
2277 update_fcr31();
2278 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2279 wt2 = FLOAT_SNAN32;
2280 return wt2;
2283 uint32_t helper_float_ceilw_s(uint32_t fst0)
2285 uint32_t wt2;
2287 set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
2288 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2289 RESTORE_ROUNDING_MODE;
2290 update_fcr31();
2291 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2292 wt2 = FLOAT_SNAN32;
2293 return wt2;
2296 uint64_t helper_float_floorl_d(uint64_t fdt0)
2298 uint64_t dt2;
2300 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2301 dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2302 RESTORE_ROUNDING_MODE;
2303 update_fcr31();
2304 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2305 dt2 = FLOAT_SNAN64;
2306 return dt2;
2309 uint64_t helper_float_floorl_s(uint32_t fst0)
2311 uint64_t dt2;
2313 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2314 dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2315 RESTORE_ROUNDING_MODE;
2316 update_fcr31();
2317 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2318 dt2 = FLOAT_SNAN64;
2319 return dt2;
2322 uint32_t helper_float_floorw_d(uint64_t fdt0)
2324 uint32_t wt2;
2326 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2327 wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2328 RESTORE_ROUNDING_MODE;
2329 update_fcr31();
2330 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2331 wt2 = FLOAT_SNAN32;
2332 return wt2;
2335 uint32_t helper_float_floorw_s(uint32_t fst0)
2337 uint32_t wt2;
2339 set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
2340 wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2341 RESTORE_ROUNDING_MODE;
2342 update_fcr31();
2343 if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2344 wt2 = FLOAT_SNAN32;
2345 return wt2;
2348 /* unary operations, not modifying fp status */
2349 #define FLOAT_UNOP(name) \
2350 uint64_t helper_float_ ## name ## _d(uint64_t fdt0) \
2352 return float64_ ## name(fdt0); \
2354 uint32_t helper_float_ ## name ## _s(uint32_t fst0) \
2356 return float32_ ## name(fst0); \
2358 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0) \
2360 uint32_t wt0; \
2361 uint32_t wth0; \
2363 wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF); \
2364 wth0 = float32_ ## name(fdt0 >> 32); \
2365 return ((uint64_t)wth0 << 32) | wt0; \
2367 FLOAT_UNOP(abs)
2368 FLOAT_UNOP(chs)
2369 #undef FLOAT_UNOP
2371 /* MIPS specific unary operations */
2372 uint64_t helper_float_recip_d(uint64_t fdt0)
2374 uint64_t fdt2;
2376 set_float_exception_flags(0, &env->active_fpu.fp_status);
2377 fdt2 = float64_div(FLOAT_ONE64, fdt0, &env->active_fpu.fp_status);
2378 update_fcr31();
2379 return fdt2;
2382 uint32_t helper_float_recip_s(uint32_t fst0)
2384 uint32_t fst2;
2386 set_float_exception_flags(0, &env->active_fpu.fp_status);
2387 fst2 = float32_div(FLOAT_ONE32, fst0, &env->active_fpu.fp_status);
2388 update_fcr31();
2389 return fst2;
2392 uint64_t helper_float_rsqrt_d(uint64_t fdt0)
2394 uint64_t fdt2;
2396 set_float_exception_flags(0, &env->active_fpu.fp_status);
2397 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2398 fdt2 = float64_div(FLOAT_ONE64, fdt2, &env->active_fpu.fp_status);
2399 update_fcr31();
2400 return fdt2;
2403 uint32_t helper_float_rsqrt_s(uint32_t fst0)
2405 uint32_t fst2;
2407 set_float_exception_flags(0, &env->active_fpu.fp_status);
2408 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2409 fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
2410 update_fcr31();
2411 return fst2;
2414 uint64_t helper_float_recip1_d(uint64_t fdt0)
2416 uint64_t fdt2;
2418 set_float_exception_flags(0, &env->active_fpu.fp_status);
2419 fdt2 = float64_div(FLOAT_ONE64, fdt0, &env->active_fpu.fp_status);
2420 update_fcr31();
2421 return fdt2;
2424 uint32_t helper_float_recip1_s(uint32_t fst0)
2426 uint32_t fst2;
2428 set_float_exception_flags(0, &env->active_fpu.fp_status);
2429 fst2 = float32_div(FLOAT_ONE32, fst0, &env->active_fpu.fp_status);
2430 update_fcr31();
2431 return fst2;
2434 uint64_t helper_float_recip1_ps(uint64_t fdt0)
2436 uint32_t fst2;
2437 uint32_t fsth2;
2439 set_float_exception_flags(0, &env->active_fpu.fp_status);
2440 fst2 = float32_div(FLOAT_ONE32, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2441 fsth2 = float32_div(FLOAT_ONE32, fdt0 >> 32, &env->active_fpu.fp_status);
2442 update_fcr31();
2443 return ((uint64_t)fsth2 << 32) | fst2;
2446 uint64_t helper_float_rsqrt1_d(uint64_t fdt0)
2448 uint64_t fdt2;
2450 set_float_exception_flags(0, &env->active_fpu.fp_status);
2451 fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
2452 fdt2 = float64_div(FLOAT_ONE64, fdt2, &env->active_fpu.fp_status);
2453 update_fcr31();
2454 return fdt2;
2457 uint32_t helper_float_rsqrt1_s(uint32_t fst0)
2459 uint32_t fst2;
2461 set_float_exception_flags(0, &env->active_fpu.fp_status);
2462 fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
2463 fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
2464 update_fcr31();
2465 return fst2;
2468 uint64_t helper_float_rsqrt1_ps(uint64_t fdt0)
2470 uint32_t fst2;
2471 uint32_t fsth2;
2473 set_float_exception_flags(0, &env->active_fpu.fp_status);
2474 fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
2475 fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
2476 fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
2477 fsth2 = float32_div(FLOAT_ONE32, fsth2, &env->active_fpu.fp_status);
2478 update_fcr31();
2479 return ((uint64_t)fsth2 << 32) | fst2;
2482 #define FLOAT_OP(name, p) void helper_float_##name##_##p(void)
2484 /* binary operations */
2485 #define FLOAT_BINOP(name) \
2486 uint64_t helper_float_ ## name ## _d(uint64_t fdt0, uint64_t fdt1) \
2488 uint64_t dt2; \
2490 set_float_exception_flags(0, &env->active_fpu.fp_status); \
2491 dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status); \
2492 update_fcr31(); \
2493 if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) \
2494 dt2 = FLOAT_QNAN64; \
2495 return dt2; \
2498 uint32_t helper_float_ ## name ## _s(uint32_t fst0, uint32_t fst1) \
2500 uint32_t wt2; \
2502 set_float_exception_flags(0, &env->active_fpu.fp_status); \
2503 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
2504 update_fcr31(); \
2505 if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) \
2506 wt2 = FLOAT_QNAN32; \
2507 return wt2; \
2510 uint64_t helper_float_ ## name ## _ps(uint64_t fdt0, uint64_t fdt1) \
2512 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
2513 uint32_t fsth0 = fdt0 >> 32; \
2514 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
2515 uint32_t fsth1 = fdt1 >> 32; \
2516 uint32_t wt2; \
2517 uint32_t wth2; \
2519 set_float_exception_flags(0, &env->active_fpu.fp_status); \
2520 wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status); \
2521 wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status); \
2522 update_fcr31(); \
2523 if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) { \
2524 wt2 = FLOAT_QNAN32; \
2525 wth2 = FLOAT_QNAN32; \
2527 return ((uint64_t)wth2 << 32) | wt2; \
2530 FLOAT_BINOP(add)
2531 FLOAT_BINOP(sub)
2532 FLOAT_BINOP(mul)
2533 FLOAT_BINOP(div)
2534 #undef FLOAT_BINOP
2536 /* ternary operations */
2537 #define FLOAT_TERNOP(name1, name2) \
2538 uint64_t helper_float_ ## name1 ## name2 ## _d(uint64_t fdt0, uint64_t fdt1, \
2539 uint64_t fdt2) \
2541 fdt0 = float64_ ## name1 (fdt0, fdt1, &env->active_fpu.fp_status); \
2542 return float64_ ## name2 (fdt0, fdt2, &env->active_fpu.fp_status); \
2545 uint32_t helper_float_ ## name1 ## name2 ## _s(uint32_t fst0, uint32_t fst1, \
2546 uint32_t fst2) \
2548 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
2549 return float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
2552 uint64_t helper_float_ ## name1 ## name2 ## _ps(uint64_t fdt0, uint64_t fdt1, \
2553 uint64_t fdt2) \
2555 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
2556 uint32_t fsth0 = fdt0 >> 32; \
2557 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
2558 uint32_t fsth1 = fdt1 >> 32; \
2559 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
2560 uint32_t fsth2 = fdt2 >> 32; \
2562 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
2563 fsth0 = float32_ ## name1 (fsth0, fsth1, &env->active_fpu.fp_status); \
2564 fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
2565 fsth2 = float32_ ## name2 (fsth0, fsth2, &env->active_fpu.fp_status); \
2566 return ((uint64_t)fsth2 << 32) | fst2; \
2569 FLOAT_TERNOP(mul, add)
2570 FLOAT_TERNOP(mul, sub)
2571 #undef FLOAT_TERNOP
2573 /* negated ternary operations */
2574 #define FLOAT_NTERNOP(name1, name2) \
2575 uint64_t helper_float_n ## name1 ## name2 ## _d(uint64_t fdt0, uint64_t fdt1, \
2576 uint64_t fdt2) \
2578 fdt0 = float64_ ## name1 (fdt0, fdt1, &env->active_fpu.fp_status); \
2579 fdt2 = float64_ ## name2 (fdt0, fdt2, &env->active_fpu.fp_status); \
2580 return float64_chs(fdt2); \
2583 uint32_t helper_float_n ## name1 ## name2 ## _s(uint32_t fst0, uint32_t fst1, \
2584 uint32_t fst2) \
2586 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
2587 fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
2588 return float32_chs(fst2); \
2591 uint64_t helper_float_n ## name1 ## name2 ## _ps(uint64_t fdt0, uint64_t fdt1,\
2592 uint64_t fdt2) \
2594 uint32_t fst0 = fdt0 & 0XFFFFFFFF; \
2595 uint32_t fsth0 = fdt0 >> 32; \
2596 uint32_t fst1 = fdt1 & 0XFFFFFFFF; \
2597 uint32_t fsth1 = fdt1 >> 32; \
2598 uint32_t fst2 = fdt2 & 0XFFFFFFFF; \
2599 uint32_t fsth2 = fdt2 >> 32; \
2601 fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status); \
2602 fsth0 = float32_ ## name1 (fsth0, fsth1, &env->active_fpu.fp_status); \
2603 fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status); \
2604 fsth2 = float32_ ## name2 (fsth0, fsth2, &env->active_fpu.fp_status); \
2605 fst2 = float32_chs(fst2); \
2606 fsth2 = float32_chs(fsth2); \
2607 return ((uint64_t)fsth2 << 32) | fst2; \
2610 FLOAT_NTERNOP(mul, add)
2611 FLOAT_NTERNOP(mul, sub)
2612 #undef FLOAT_NTERNOP
2614 /* MIPS specific binary operations */
2615 uint64_t helper_float_recip2_d(uint64_t fdt0, uint64_t fdt2)
2617 set_float_exception_flags(0, &env->active_fpu.fp_status);
2618 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
2619 fdt2 = float64_chs(float64_sub(fdt2, FLOAT_ONE64, &env->active_fpu.fp_status));
2620 update_fcr31();
2621 return fdt2;
2624 uint32_t helper_float_recip2_s(uint32_t fst0, uint32_t fst2)
2626 set_float_exception_flags(0, &env->active_fpu.fp_status);
2627 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2628 fst2 = float32_chs(float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status));
2629 update_fcr31();
2630 return fst2;
2633 uint64_t helper_float_recip2_ps(uint64_t fdt0, uint64_t fdt2)
2635 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2636 uint32_t fsth0 = fdt0 >> 32;
2637 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
2638 uint32_t fsth2 = fdt2 >> 32;
2640 set_float_exception_flags(0, &env->active_fpu.fp_status);
2641 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2642 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
2643 fst2 = float32_chs(float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status));
2644 fsth2 = float32_chs(float32_sub(fsth2, FLOAT_ONE32, &env->active_fpu.fp_status));
2645 update_fcr31();
2646 return ((uint64_t)fsth2 << 32) | fst2;
2649 uint64_t helper_float_rsqrt2_d(uint64_t fdt0, uint64_t fdt2)
2651 set_float_exception_flags(0, &env->active_fpu.fp_status);
2652 fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
2653 fdt2 = float64_sub(fdt2, FLOAT_ONE64, &env->active_fpu.fp_status);
2654 fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
2655 update_fcr31();
2656 return fdt2;
2659 uint32_t helper_float_rsqrt2_s(uint32_t fst0, uint32_t fst2)
2661 set_float_exception_flags(0, &env->active_fpu.fp_status);
2662 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2663 fst2 = float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status);
2664 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
2665 update_fcr31();
2666 return fst2;
2669 uint64_t helper_float_rsqrt2_ps(uint64_t fdt0, uint64_t fdt2)
2671 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2672 uint32_t fsth0 = fdt0 >> 32;
2673 uint32_t fst2 = fdt2 & 0XFFFFFFFF;
2674 uint32_t fsth2 = fdt2 >> 32;
2676 set_float_exception_flags(0, &env->active_fpu.fp_status);
2677 fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
2678 fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
2679 fst2 = float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status);
2680 fsth2 = float32_sub(fsth2, FLOAT_ONE32, &env->active_fpu.fp_status);
2681 fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
2682 fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
2683 update_fcr31();
2684 return ((uint64_t)fsth2 << 32) | fst2;
2687 uint64_t helper_float_addr_ps(uint64_t fdt0, uint64_t fdt1)
2689 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2690 uint32_t fsth0 = fdt0 >> 32;
2691 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
2692 uint32_t fsth1 = fdt1 >> 32;
2693 uint32_t fst2;
2694 uint32_t fsth2;
2696 set_float_exception_flags(0, &env->active_fpu.fp_status);
2697 fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
2698 fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
2699 update_fcr31();
2700 return ((uint64_t)fsth2 << 32) | fst2;
2703 uint64_t helper_float_mulr_ps(uint64_t fdt0, uint64_t fdt1)
2705 uint32_t fst0 = fdt0 & 0XFFFFFFFF;
2706 uint32_t fsth0 = fdt0 >> 32;
2707 uint32_t fst1 = fdt1 & 0XFFFFFFFF;
2708 uint32_t fsth1 = fdt1 >> 32;
2709 uint32_t fst2;
2710 uint32_t fsth2;
2712 set_float_exception_flags(0, &env->active_fpu.fp_status);
2713 fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
2714 fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
2715 update_fcr31();
2716 return ((uint64_t)fsth2 << 32) | fst2;
2719 /* compare operations */
2720 #define FOP_COND_D(op, cond) \
2721 void helper_cmp_d_ ## op (uint64_t fdt0, uint64_t fdt1, int cc) \
2723 int c = cond; \
2724 update_fcr31(); \
2725 if (c) \
2726 SET_FP_COND(cc, env->active_fpu); \
2727 else \
2728 CLEAR_FP_COND(cc, env->active_fpu); \
2730 void helper_cmpabs_d_ ## op (uint64_t fdt0, uint64_t fdt1, int cc) \
2732 int c; \
2733 fdt0 = float64_abs(fdt0); \
2734 fdt1 = float64_abs(fdt1); \
2735 c = cond; \
2736 update_fcr31(); \
2737 if (c) \
2738 SET_FP_COND(cc, env->active_fpu); \
2739 else \
2740 CLEAR_FP_COND(cc, env->active_fpu); \
2743 static int float64_is_unordered(int sig, float64 a, float64 b STATUS_PARAM)
2745 if (float64_is_signaling_nan(a) ||
2746 float64_is_signaling_nan(b) ||
2747 (sig && (float64_is_nan(a) || float64_is_nan(b)))) {
2748 float_raise(float_flag_invalid, status);
2749 return 1;
2750 } else if (float64_is_nan(a) || float64_is_nan(b)) {
2751 return 1;
2752 } else {
2753 return 0;
2757 /* NOTE: the comma operator will make "cond" to eval to false,
2758 * but float*_is_unordered() is still called. */
2759 FOP_COND_D(f, (float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status), 0))
2760 FOP_COND_D(un, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status))
2761 FOP_COND_D(eq, !float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) && float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
2762 FOP_COND_D(ueq, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
2763 FOP_COND_D(olt, !float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) && float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
2764 FOP_COND_D(ult, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
2765 FOP_COND_D(ole, !float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) && float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
2766 FOP_COND_D(ule, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
2767 /* NOTE: the comma operator will make "cond" to eval to false,
2768 * but float*_is_unordered() is still called. */
2769 FOP_COND_D(sf, (float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status), 0))
2770 FOP_COND_D(ngle,float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status))
2771 FOP_COND_D(seq, !float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) && float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
2772 FOP_COND_D(ngl, float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
2773 FOP_COND_D(lt, !float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) && float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
2774 FOP_COND_D(nge, float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
2775 FOP_COND_D(le, !float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) && float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
2776 FOP_COND_D(ngt, float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
2778 #define FOP_COND_S(op, cond) \
2779 void helper_cmp_s_ ## op (uint32_t fst0, uint32_t fst1, int cc) \
2781 int c = cond; \
2782 update_fcr31(); \
2783 if (c) \
2784 SET_FP_COND(cc, env->active_fpu); \
2785 else \
2786 CLEAR_FP_COND(cc, env->active_fpu); \
2788 void helper_cmpabs_s_ ## op (uint32_t fst0, uint32_t fst1, int cc) \
2790 int c; \
2791 fst0 = float32_abs(fst0); \
2792 fst1 = float32_abs(fst1); \
2793 c = cond; \
2794 update_fcr31(); \
2795 if (c) \
2796 SET_FP_COND(cc, env->active_fpu); \
2797 else \
2798 CLEAR_FP_COND(cc, env->active_fpu); \
2801 static flag float32_is_unordered(int sig, float32 a, float32 b STATUS_PARAM)
2803 if (float32_is_signaling_nan(a) ||
2804 float32_is_signaling_nan(b) ||
2805 (sig && (float32_is_nan(a) || float32_is_nan(b)))) {
2806 float_raise(float_flag_invalid, status);
2807 return 1;
2808 } else if (float32_is_nan(a) || float32_is_nan(b)) {
2809 return 1;
2810 } else {
2811 return 0;
2815 /* NOTE: the comma operator will make "cond" to eval to false,
2816 * but float*_is_unordered() is still called. */
2817 FOP_COND_S(f, (float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status), 0))
2818 FOP_COND_S(un, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status))
2819 FOP_COND_S(eq, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_eq(fst0, fst1, &env->active_fpu.fp_status))
2820 FOP_COND_S(ueq, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
2821 FOP_COND_S(olt, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_lt(fst0, fst1, &env->active_fpu.fp_status))
2822 FOP_COND_S(ult, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
2823 FOP_COND_S(ole, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_le(fst0, fst1, &env->active_fpu.fp_status))
2824 FOP_COND_S(ule, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
2825 /* NOTE: the comma operator will make "cond" to eval to false,
2826 * but float*_is_unordered() is still called. */
2827 FOP_COND_S(sf, (float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status), 0))
2828 FOP_COND_S(ngle,float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status))
2829 FOP_COND_S(seq, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_eq(fst0, fst1, &env->active_fpu.fp_status))
2830 FOP_COND_S(ngl, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
2831 FOP_COND_S(lt, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_lt(fst0, fst1, &env->active_fpu.fp_status))
2832 FOP_COND_S(nge, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
2833 FOP_COND_S(le, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_le(fst0, fst1, &env->active_fpu.fp_status))
2834 FOP_COND_S(ngt, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status))
2836 #define FOP_COND_PS(op, condl, condh) \
2837 void helper_cmp_ps_ ## op (uint64_t fdt0, uint64_t fdt1, int cc) \
2839 uint32_t fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
2840 uint32_t fsth0 = float32_abs(fdt0 >> 32); \
2841 uint32_t fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
2842 uint32_t fsth1 = float32_abs(fdt1 >> 32); \
2843 int cl = condl; \
2844 int ch = condh; \
2846 update_fcr31(); \
2847 if (cl) \
2848 SET_FP_COND(cc, env->active_fpu); \
2849 else \
2850 CLEAR_FP_COND(cc, env->active_fpu); \
2851 if (ch) \
2852 SET_FP_COND(cc + 1, env->active_fpu); \
2853 else \
2854 CLEAR_FP_COND(cc + 1, env->active_fpu); \
2856 void helper_cmpabs_ps_ ## op (uint64_t fdt0, uint64_t fdt1, int cc) \
2858 uint32_t fst0 = float32_abs(fdt0 & 0XFFFFFFFF); \
2859 uint32_t fsth0 = float32_abs(fdt0 >> 32); \
2860 uint32_t fst1 = float32_abs(fdt1 & 0XFFFFFFFF); \
2861 uint32_t fsth1 = float32_abs(fdt1 >> 32); \
2862 int cl = condl; \
2863 int ch = condh; \
2865 update_fcr31(); \
2866 if (cl) \
2867 SET_FP_COND(cc, env->active_fpu); \
2868 else \
2869 CLEAR_FP_COND(cc, env->active_fpu); \
2870 if (ch) \
2871 SET_FP_COND(cc + 1, env->active_fpu); \
2872 else \
2873 CLEAR_FP_COND(cc + 1, env->active_fpu); \
2876 /* NOTE: the comma operator will make "cond" to eval to false,
2877 * but float*_is_unordered() is still called. */
2878 FOP_COND_PS(f, (float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status), 0),
2879 (float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status), 0))
2880 FOP_COND_PS(un, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status),
2881 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status))
2882 FOP_COND_PS(eq, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_eq(fst0, fst1, &env->active_fpu.fp_status),
2883 !float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) && float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
2884 FOP_COND_PS(ueq, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
2885 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
2886 FOP_COND_PS(olt, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_lt(fst0, fst1, &env->active_fpu.fp_status),
2887 !float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) && float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
2888 FOP_COND_PS(ult, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
2889 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
2890 FOP_COND_PS(ole, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_le(fst0, fst1, &env->active_fpu.fp_status),
2891 !float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) && float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
2892 FOP_COND_PS(ule, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
2893 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
2894 /* NOTE: the comma operator will make "cond" to eval to false,
2895 * but float*_is_unordered() is still called. */
2896 FOP_COND_PS(sf, (float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status), 0),
2897 (float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status), 0))
2898 FOP_COND_PS(ngle,float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status),
2899 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status))
2900 FOP_COND_PS(seq, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_eq(fst0, fst1, &env->active_fpu.fp_status),
2901 !float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) && float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
2902 FOP_COND_PS(ngl, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
2903 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
2904 FOP_COND_PS(lt, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_lt(fst0, fst1, &env->active_fpu.fp_status),
2905 !float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) && float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
2906 FOP_COND_PS(nge, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
2907 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
2908 FOP_COND_PS(le, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_le(fst0, fst1, &env->active_fpu.fp_status),
2909 !float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) && float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
2910 FOP_COND_PS(ngt, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) || float32_le(fst0, fst1, &env->active_fpu.fp_status),
2911 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))