target-alpha: Implement missing MVI instructions.
[qemu/aliguori-queue.git] / target-alpha / op_helper.c
blob08d5924b3ba74f43b6688adcc7fa8e7023042846
1 /*
2 * Alpha emulation cpu micro-operations helpers for qemu.
4 * Copyright (c) 2007 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, see <http://www.gnu.org/licenses/>.
20 #include "exec.h"
21 #include "host-utils.h"
22 #include "softfloat.h"
23 #include "helper.h"
25 /*****************************************************************************/
26 /* Exceptions processing helpers */
27 void helper_excp (int excp, int error)
29 env->exception_index = excp;
30 env->error_code = error;
31 cpu_loop_exit();
34 uint64_t helper_load_pcc (void)
36 /* XXX: TODO */
37 return 0;
40 uint64_t helper_load_fpcr (void)
42 return cpu_alpha_load_fpcr (env);
45 void helper_store_fpcr (uint64_t val)
47 cpu_alpha_store_fpcr (env, val);
50 static spinlock_t intr_cpu_lock = SPIN_LOCK_UNLOCKED;
52 uint64_t helper_rs(void)
54 uint64_t tmp;
56 spin_lock(&intr_cpu_lock);
57 tmp = env->intr_flag;
58 env->intr_flag = 1;
59 spin_unlock(&intr_cpu_lock);
61 return tmp;
64 uint64_t helper_rc(void)
66 uint64_t tmp;
68 spin_lock(&intr_cpu_lock);
69 tmp = env->intr_flag;
70 env->intr_flag = 0;
71 spin_unlock(&intr_cpu_lock);
73 return tmp;
76 uint64_t helper_addqv (uint64_t op1, uint64_t op2)
78 uint64_t tmp = op1;
79 op1 += op2;
80 if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
81 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
83 return op1;
86 uint64_t helper_addlv (uint64_t op1, uint64_t op2)
88 uint64_t tmp = op1;
89 op1 = (uint32_t)(op1 + op2);
90 if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
91 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
93 return op1;
96 uint64_t helper_subqv (uint64_t op1, uint64_t op2)
98 uint64_t res;
99 res = op1 - op2;
100 if (unlikely((op1 ^ op2) & (res ^ op1) & (1ULL << 63))) {
101 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
103 return res;
106 uint64_t helper_sublv (uint64_t op1, uint64_t op2)
108 uint32_t res;
109 res = op1 - op2;
110 if (unlikely((op1 ^ op2) & (res ^ op1) & (1UL << 31))) {
111 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
113 return res;
116 uint64_t helper_mullv (uint64_t op1, uint64_t op2)
118 int64_t res = (int64_t)op1 * (int64_t)op2;
120 if (unlikely((int32_t)res != res)) {
121 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
123 return (int64_t)((int32_t)res);
126 uint64_t helper_mulqv (uint64_t op1, uint64_t op2)
128 uint64_t tl, th;
130 muls64(&tl, &th, op1, op2);
131 /* If th != 0 && th != -1, then we had an overflow */
132 if (unlikely((th + 1) > 1)) {
133 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
135 return tl;
138 uint64_t helper_umulh (uint64_t op1, uint64_t op2)
140 uint64_t tl, th;
142 mulu64(&tl, &th, op1, op2);
143 return th;
146 uint64_t helper_ctpop (uint64_t arg)
148 return ctpop64(arg);
151 uint64_t helper_ctlz (uint64_t arg)
153 return clz64(arg);
156 uint64_t helper_cttz (uint64_t arg)
158 return ctz64(arg);
161 static inline uint64_t byte_zap(uint64_t op, uint8_t mskb)
163 uint64_t mask;
165 mask = 0;
166 mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
167 mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
168 mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
169 mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
170 mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
171 mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
172 mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
173 mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;
175 return op & ~mask;
178 uint64_t helper_mskbl(uint64_t val, uint64_t mask)
180 return byte_zap(val, 0x01 << (mask & 7));
183 uint64_t helper_insbl(uint64_t val, uint64_t mask)
185 val <<= (mask & 7) * 8;
186 return byte_zap(val, ~(0x01 << (mask & 7)));
189 uint64_t helper_mskwl(uint64_t val, uint64_t mask)
191 return byte_zap(val, 0x03 << (mask & 7));
194 uint64_t helper_inswl(uint64_t val, uint64_t mask)
196 val <<= (mask & 7) * 8;
197 return byte_zap(val, ~(0x03 << (mask & 7)));
200 uint64_t helper_mskll(uint64_t val, uint64_t mask)
202 return byte_zap(val, 0x0F << (mask & 7));
205 uint64_t helper_insll(uint64_t val, uint64_t mask)
207 val <<= (mask & 7) * 8;
208 return byte_zap(val, ~(0x0F << (mask & 7)));
211 uint64_t helper_zap(uint64_t val, uint64_t mask)
213 return byte_zap(val, mask);
216 uint64_t helper_zapnot(uint64_t val, uint64_t mask)
218 return byte_zap(val, ~mask);
221 uint64_t helper_mskql(uint64_t val, uint64_t mask)
223 return byte_zap(val, 0xFF << (mask & 7));
226 uint64_t helper_insql(uint64_t val, uint64_t mask)
228 val <<= (mask & 7) * 8;
229 return byte_zap(val, ~(0xFF << (mask & 7)));
232 uint64_t helper_mskwh(uint64_t val, uint64_t mask)
234 return byte_zap(val, (0x03 << (mask & 7)) >> 8);
237 uint64_t helper_inswh(uint64_t val, uint64_t mask)
239 val >>= 64 - ((mask & 7) * 8);
240 return byte_zap(val, ~((0x03 << (mask & 7)) >> 8));
243 uint64_t helper_msklh(uint64_t val, uint64_t mask)
245 return byte_zap(val, (0x0F << (mask & 7)) >> 8);
248 uint64_t helper_inslh(uint64_t val, uint64_t mask)
250 val >>= 64 - ((mask & 7) * 8);
251 return byte_zap(val, ~((0x0F << (mask & 7)) >> 8));
254 uint64_t helper_mskqh(uint64_t val, uint64_t mask)
256 return byte_zap(val, (0xFF << (mask & 7)) >> 8);
259 uint64_t helper_insqh(uint64_t val, uint64_t mask)
261 val >>= 64 - ((mask & 7) * 8);
262 return byte_zap(val, ~((0xFF << (mask & 7)) >> 8));
265 uint64_t helper_cmpbge (uint64_t op1, uint64_t op2)
267 uint8_t opa, opb, res;
268 int i;
270 res = 0;
271 for (i = 0; i < 8; i++) {
272 opa = op1 >> (i * 8);
273 opb = op2 >> (i * 8);
274 if (opa >= opb)
275 res |= 1 << i;
277 return res;
280 uint64_t helper_minub8 (uint64_t op1, uint64_t op2)
282 uint64_t res = 0;
283 uint8_t opa, opb, opr;
284 int i;
286 for (i = 0; i < 8; ++i) {
287 opa = op1 >> (i * 8);
288 opb = op2 >> (i * 8);
289 opr = opa < opb ? opa : opb;
290 res |= (uint64_t)opr << (i * 8);
292 return res;
295 uint64_t helper_minsb8 (uint64_t op1, uint64_t op2)
297 uint64_t res = 0;
298 int8_t opa, opb;
299 uint8_t opr;
300 int i;
302 for (i = 0; i < 8; ++i) {
303 opa = op1 >> (i * 8);
304 opb = op2 >> (i * 8);
305 opr = opa < opb ? opa : opb;
306 res |= (uint64_t)opr << (i * 8);
308 return res;
311 uint64_t helper_minuw4 (uint64_t op1, uint64_t op2)
313 uint64_t res = 0;
314 uint16_t opa, opb, opr;
315 int i;
317 for (i = 0; i < 4; ++i) {
318 opa = op1 >> (i * 16);
319 opb = op2 >> (i * 16);
320 opr = opa < opb ? opa : opb;
321 res |= (uint64_t)opr << (i * 16);
323 return res;
326 uint64_t helper_minsw4 (uint64_t op1, uint64_t op2)
328 uint64_t res = 0;
329 int16_t opa, opb;
330 uint16_t opr;
331 int i;
333 for (i = 0; i < 4; ++i) {
334 opa = op1 >> (i * 16);
335 opb = op2 >> (i * 16);
336 opr = opa < opb ? opa : opb;
337 res |= (uint64_t)opr << (i * 16);
339 return res;
342 uint64_t helper_maxub8 (uint64_t op1, uint64_t op2)
344 uint64_t res = 0;
345 uint8_t opa, opb, opr;
346 int i;
348 for (i = 0; i < 8; ++i) {
349 opa = op1 >> (i * 8);
350 opb = op2 >> (i * 8);
351 opr = opa > opb ? opa : opb;
352 res |= (uint64_t)opr << (i * 8);
354 return res;
357 uint64_t helper_maxsb8 (uint64_t op1, uint64_t op2)
359 uint64_t res = 0;
360 int8_t opa, opb;
361 uint8_t opr;
362 int i;
364 for (i = 0; i < 8; ++i) {
365 opa = op1 >> (i * 8);
366 opb = op2 >> (i * 8);
367 opr = opa > opb ? opa : opb;
368 res |= (uint64_t)opr << (i * 8);
370 return res;
373 uint64_t helper_maxuw4 (uint64_t op1, uint64_t op2)
375 uint64_t res = 0;
376 uint16_t opa, opb, opr;
377 int i;
379 for (i = 0; i < 4; ++i) {
380 opa = op1 >> (i * 16);
381 opb = op2 >> (i * 16);
382 opr = opa > opb ? opa : opb;
383 res |= (uint64_t)opr << (i * 16);
385 return res;
388 uint64_t helper_maxsw4 (uint64_t op1, uint64_t op2)
390 uint64_t res = 0;
391 int16_t opa, opb;
392 uint16_t opr;
393 int i;
395 for (i = 0; i < 4; ++i) {
396 opa = op1 >> (i * 16);
397 opb = op2 >> (i * 16);
398 opr = opa > opb ? opa : opb;
399 res |= (uint64_t)opr << (i * 16);
401 return res;
404 uint64_t helper_perr (uint64_t op1, uint64_t op2)
406 uint64_t res = 0;
407 uint8_t opa, opb, opr;
408 int i;
410 for (i = 0; i < 8; ++i) {
411 opa = op1 >> (i * 8);
412 opb = op2 >> (i * 8);
413 if (opa >= opb)
414 opr = opa - opb;
415 else
416 opr = opb - opa;
417 res += opr;
419 return res;
422 uint64_t helper_pklb (uint64_t op1)
424 return (op1 & 0xff) | ((op1 >> 24) & 0xff00);
427 uint64_t helper_pkwb (uint64_t op1)
429 return ((op1 & 0xff)
430 | ((op1 >> 8) & 0xff00)
431 | ((op1 >> 16) & 0xff0000)
432 | ((op1 >> 24) & 0xff000000));
435 uint64_t helper_unpkbl (uint64_t op1)
437 return (op1 & 0xff) | ((op1 & 0xff00) << 24);
440 uint64_t helper_unpkbw (uint64_t op1)
442 return ((op1 & 0xff)
443 | ((op1 & 0xff00) << 8)
444 | ((op1 & 0xff0000) << 16)
445 | ((op1 & 0xff000000) << 24));
448 /* Floating point helpers */
450 /* F floating (VAX) */
451 static inline uint64_t float32_to_f(float32 fa)
453 uint64_t r, exp, mant, sig;
454 CPU_FloatU a;
456 a.f = fa;
457 sig = ((uint64_t)a.l & 0x80000000) << 32;
458 exp = (a.l >> 23) & 0xff;
459 mant = ((uint64_t)a.l & 0x007fffff) << 29;
461 if (exp == 255) {
462 /* NaN or infinity */
463 r = 1; /* VAX dirty zero */
464 } else if (exp == 0) {
465 if (mant == 0) {
466 /* Zero */
467 r = 0;
468 } else {
469 /* Denormalized */
470 r = sig | ((exp + 1) << 52) | mant;
472 } else {
473 if (exp >= 253) {
474 /* Overflow */
475 r = 1; /* VAX dirty zero */
476 } else {
477 r = sig | ((exp + 2) << 52);
481 return r;
484 static inline float32 f_to_float32(uint64_t a)
486 uint32_t exp, mant_sig;
487 CPU_FloatU r;
489 exp = ((a >> 55) & 0x80) | ((a >> 52) & 0x7f);
490 mant_sig = ((a >> 32) & 0x80000000) | ((a >> 29) & 0x007fffff);
492 if (unlikely(!exp && mant_sig)) {
493 /* Reserved operands / Dirty zero */
494 helper_excp(EXCP_OPCDEC, 0);
497 if (exp < 3) {
498 /* Underflow */
499 r.l = 0;
500 } else {
501 r.l = ((exp - 2) << 23) | mant_sig;
504 return r.f;
507 uint32_t helper_f_to_memory (uint64_t a)
509 uint32_t r;
510 r = (a & 0x00001fffe0000000ull) >> 13;
511 r |= (a & 0x07ffe00000000000ull) >> 45;
512 r |= (a & 0xc000000000000000ull) >> 48;
513 return r;
516 uint64_t helper_memory_to_f (uint32_t a)
518 uint64_t r;
519 r = ((uint64_t)(a & 0x0000c000)) << 48;
520 r |= ((uint64_t)(a & 0x003fffff)) << 45;
521 r |= ((uint64_t)(a & 0xffff0000)) << 13;
522 if (!(a & 0x00004000))
523 r |= 0x7ll << 59;
524 return r;
527 uint64_t helper_addf (uint64_t a, uint64_t b)
529 float32 fa, fb, fr;
531 fa = f_to_float32(a);
532 fb = f_to_float32(b);
533 fr = float32_add(fa, fb, &FP_STATUS);
534 return float32_to_f(fr);
537 uint64_t helper_subf (uint64_t a, uint64_t b)
539 float32 fa, fb, fr;
541 fa = f_to_float32(a);
542 fb = f_to_float32(b);
543 fr = float32_sub(fa, fb, &FP_STATUS);
544 return float32_to_f(fr);
547 uint64_t helper_mulf (uint64_t a, uint64_t b)
549 float32 fa, fb, fr;
551 fa = f_to_float32(a);
552 fb = f_to_float32(b);
553 fr = float32_mul(fa, fb, &FP_STATUS);
554 return float32_to_f(fr);
557 uint64_t helper_divf (uint64_t a, uint64_t b)
559 float32 fa, fb, fr;
561 fa = f_to_float32(a);
562 fb = f_to_float32(b);
563 fr = float32_div(fa, fb, &FP_STATUS);
564 return float32_to_f(fr);
567 uint64_t helper_sqrtf (uint64_t t)
569 float32 ft, fr;
571 ft = f_to_float32(t);
572 fr = float32_sqrt(ft, &FP_STATUS);
573 return float32_to_f(fr);
577 /* G floating (VAX) */
578 static inline uint64_t float64_to_g(float64 fa)
580 uint64_t r, exp, mant, sig;
581 CPU_DoubleU a;
583 a.d = fa;
584 sig = a.ll & 0x8000000000000000ull;
585 exp = (a.ll >> 52) & 0x7ff;
586 mant = a.ll & 0x000fffffffffffffull;
588 if (exp == 2047) {
589 /* NaN or infinity */
590 r = 1; /* VAX dirty zero */
591 } else if (exp == 0) {
592 if (mant == 0) {
593 /* Zero */
594 r = 0;
595 } else {
596 /* Denormalized */
597 r = sig | ((exp + 1) << 52) | mant;
599 } else {
600 if (exp >= 2045) {
601 /* Overflow */
602 r = 1; /* VAX dirty zero */
603 } else {
604 r = sig | ((exp + 2) << 52);
608 return r;
611 static inline float64 g_to_float64(uint64_t a)
613 uint64_t exp, mant_sig;
614 CPU_DoubleU r;
616 exp = (a >> 52) & 0x7ff;
617 mant_sig = a & 0x800fffffffffffffull;
619 if (!exp && mant_sig) {
620 /* Reserved operands / Dirty zero */
621 helper_excp(EXCP_OPCDEC, 0);
624 if (exp < 3) {
625 /* Underflow */
626 r.ll = 0;
627 } else {
628 r.ll = ((exp - 2) << 52) | mant_sig;
631 return r.d;
634 uint64_t helper_g_to_memory (uint64_t a)
636 uint64_t r;
637 r = (a & 0x000000000000ffffull) << 48;
638 r |= (a & 0x00000000ffff0000ull) << 16;
639 r |= (a & 0x0000ffff00000000ull) >> 16;
640 r |= (a & 0xffff000000000000ull) >> 48;
641 return r;
644 uint64_t helper_memory_to_g (uint64_t a)
646 uint64_t r;
647 r = (a & 0x000000000000ffffull) << 48;
648 r |= (a & 0x00000000ffff0000ull) << 16;
649 r |= (a & 0x0000ffff00000000ull) >> 16;
650 r |= (a & 0xffff000000000000ull) >> 48;
651 return r;
654 uint64_t helper_addg (uint64_t a, uint64_t b)
656 float64 fa, fb, fr;
658 fa = g_to_float64(a);
659 fb = g_to_float64(b);
660 fr = float64_add(fa, fb, &FP_STATUS);
661 return float64_to_g(fr);
664 uint64_t helper_subg (uint64_t a, uint64_t b)
666 float64 fa, fb, fr;
668 fa = g_to_float64(a);
669 fb = g_to_float64(b);
670 fr = float64_sub(fa, fb, &FP_STATUS);
671 return float64_to_g(fr);
674 uint64_t helper_mulg (uint64_t a, uint64_t b)
676 float64 fa, fb, fr;
678 fa = g_to_float64(a);
679 fb = g_to_float64(b);
680 fr = float64_mul(fa, fb, &FP_STATUS);
681 return float64_to_g(fr);
684 uint64_t helper_divg (uint64_t a, uint64_t b)
686 float64 fa, fb, fr;
688 fa = g_to_float64(a);
689 fb = g_to_float64(b);
690 fr = float64_div(fa, fb, &FP_STATUS);
691 return float64_to_g(fr);
694 uint64_t helper_sqrtg (uint64_t a)
696 float64 fa, fr;
698 fa = g_to_float64(a);
699 fr = float64_sqrt(fa, &FP_STATUS);
700 return float64_to_g(fr);
704 /* S floating (single) */
705 static inline uint64_t float32_to_s(float32 fa)
707 CPU_FloatU a;
708 uint64_t r;
710 a.f = fa;
712 r = (((uint64_t)(a.l & 0xc0000000)) << 32) | (((uint64_t)(a.l & 0x3fffffff)) << 29);
713 if (((a.l & 0x7f800000) != 0x7f800000) && (!(a.l & 0x40000000)))
714 r |= 0x7ll << 59;
715 return r;
718 static inline float32 s_to_float32(uint64_t a)
720 CPU_FloatU r;
721 r.l = ((a >> 32) & 0xc0000000) | ((a >> 29) & 0x3fffffff);
722 return r.f;
725 uint32_t helper_s_to_memory (uint64_t a)
727 /* Memory format is the same as float32 */
728 float32 fa = s_to_float32(a);
729 return *(uint32_t*)(&fa);
732 uint64_t helper_memory_to_s (uint32_t a)
734 /* Memory format is the same as float32 */
735 return float32_to_s(*(float32*)(&a));
738 uint64_t helper_adds (uint64_t a, uint64_t b)
740 float32 fa, fb, fr;
742 fa = s_to_float32(a);
743 fb = s_to_float32(b);
744 fr = float32_add(fa, fb, &FP_STATUS);
745 return float32_to_s(fr);
748 uint64_t helper_subs (uint64_t a, uint64_t b)
750 float32 fa, fb, fr;
752 fa = s_to_float32(a);
753 fb = s_to_float32(b);
754 fr = float32_sub(fa, fb, &FP_STATUS);
755 return float32_to_s(fr);
758 uint64_t helper_muls (uint64_t a, uint64_t b)
760 float32 fa, fb, fr;
762 fa = s_to_float32(a);
763 fb = s_to_float32(b);
764 fr = float32_mul(fa, fb, &FP_STATUS);
765 return float32_to_s(fr);
768 uint64_t helper_divs (uint64_t a, uint64_t b)
770 float32 fa, fb, fr;
772 fa = s_to_float32(a);
773 fb = s_to_float32(b);
774 fr = float32_div(fa, fb, &FP_STATUS);
775 return float32_to_s(fr);
778 uint64_t helper_sqrts (uint64_t a)
780 float32 fa, fr;
782 fa = s_to_float32(a);
783 fr = float32_sqrt(fa, &FP_STATUS);
784 return float32_to_s(fr);
788 /* T floating (double) */
789 static inline float64 t_to_float64(uint64_t a)
791 /* Memory format is the same as float64 */
792 CPU_DoubleU r;
793 r.ll = a;
794 return r.d;
797 static inline uint64_t float64_to_t(float64 fa)
799 /* Memory format is the same as float64 */
800 CPU_DoubleU r;
801 r.d = fa;
802 return r.ll;
805 uint64_t helper_addt (uint64_t a, uint64_t b)
807 float64 fa, fb, fr;
809 fa = t_to_float64(a);
810 fb = t_to_float64(b);
811 fr = float64_add(fa, fb, &FP_STATUS);
812 return float64_to_t(fr);
815 uint64_t helper_subt (uint64_t a, uint64_t b)
817 float64 fa, fb, fr;
819 fa = t_to_float64(a);
820 fb = t_to_float64(b);
821 fr = float64_sub(fa, fb, &FP_STATUS);
822 return float64_to_t(fr);
825 uint64_t helper_mult (uint64_t a, uint64_t b)
827 float64 fa, fb, fr;
829 fa = t_to_float64(a);
830 fb = t_to_float64(b);
831 fr = float64_mul(fa, fb, &FP_STATUS);
832 return float64_to_t(fr);
835 uint64_t helper_divt (uint64_t a, uint64_t b)
837 float64 fa, fb, fr;
839 fa = t_to_float64(a);
840 fb = t_to_float64(b);
841 fr = float64_div(fa, fb, &FP_STATUS);
842 return float64_to_t(fr);
845 uint64_t helper_sqrtt (uint64_t a)
847 float64 fa, fr;
849 fa = t_to_float64(a);
850 fr = float64_sqrt(fa, &FP_STATUS);
851 return float64_to_t(fr);
855 /* Sign copy */
856 uint64_t helper_cpys(uint64_t a, uint64_t b)
858 return (a & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
861 uint64_t helper_cpysn(uint64_t a, uint64_t b)
863 return ((~a) & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
866 uint64_t helper_cpyse(uint64_t a, uint64_t b)
868 return (a & 0xFFF0000000000000ULL) | (b & ~0xFFF0000000000000ULL);
872 /* Comparisons */
873 uint64_t helper_cmptun (uint64_t a, uint64_t b)
875 float64 fa, fb;
877 fa = t_to_float64(a);
878 fb = t_to_float64(b);
880 if (float64_is_nan(fa) || float64_is_nan(fb))
881 return 0x4000000000000000ULL;
882 else
883 return 0;
886 uint64_t helper_cmpteq(uint64_t a, uint64_t b)
888 float64 fa, fb;
890 fa = t_to_float64(a);
891 fb = t_to_float64(b);
893 if (float64_eq(fa, fb, &FP_STATUS))
894 return 0x4000000000000000ULL;
895 else
896 return 0;
899 uint64_t helper_cmptle(uint64_t a, uint64_t b)
901 float64 fa, fb;
903 fa = t_to_float64(a);
904 fb = t_to_float64(b);
906 if (float64_le(fa, fb, &FP_STATUS))
907 return 0x4000000000000000ULL;
908 else
909 return 0;
912 uint64_t helper_cmptlt(uint64_t a, uint64_t b)
914 float64 fa, fb;
916 fa = t_to_float64(a);
917 fb = t_to_float64(b);
919 if (float64_lt(fa, fb, &FP_STATUS))
920 return 0x4000000000000000ULL;
921 else
922 return 0;
925 uint64_t helper_cmpgeq(uint64_t a, uint64_t b)
927 float64 fa, fb;
929 fa = g_to_float64(a);
930 fb = g_to_float64(b);
932 if (float64_eq(fa, fb, &FP_STATUS))
933 return 0x4000000000000000ULL;
934 else
935 return 0;
938 uint64_t helper_cmpgle(uint64_t a, uint64_t b)
940 float64 fa, fb;
942 fa = g_to_float64(a);
943 fb = g_to_float64(b);
945 if (float64_le(fa, fb, &FP_STATUS))
946 return 0x4000000000000000ULL;
947 else
948 return 0;
951 uint64_t helper_cmpglt(uint64_t a, uint64_t b)
953 float64 fa, fb;
955 fa = g_to_float64(a);
956 fb = g_to_float64(b);
958 if (float64_lt(fa, fb, &FP_STATUS))
959 return 0x4000000000000000ULL;
960 else
961 return 0;
964 uint64_t helper_cmpfeq (uint64_t a)
966 return !(a & 0x7FFFFFFFFFFFFFFFULL);
969 uint64_t helper_cmpfne (uint64_t a)
971 return (a & 0x7FFFFFFFFFFFFFFFULL);
974 uint64_t helper_cmpflt (uint64_t a)
976 return (a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
979 uint64_t helper_cmpfle (uint64_t a)
981 return (a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
984 uint64_t helper_cmpfgt (uint64_t a)
986 return !(a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
989 uint64_t helper_cmpfge (uint64_t a)
991 return !(a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
995 /* Floating point format conversion */
996 uint64_t helper_cvtts (uint64_t a)
998 float64 fa;
999 float32 fr;
1001 fa = t_to_float64(a);
1002 fr = float64_to_float32(fa, &FP_STATUS);
1003 return float32_to_s(fr);
1006 uint64_t helper_cvtst (uint64_t a)
1008 float32 fa;
1009 float64 fr;
1011 fa = s_to_float32(a);
1012 fr = float32_to_float64(fa, &FP_STATUS);
1013 return float64_to_t(fr);
1016 uint64_t helper_cvtqs (uint64_t a)
1018 float32 fr = int64_to_float32(a, &FP_STATUS);
1019 return float32_to_s(fr);
1022 uint64_t helper_cvttq (uint64_t a)
1024 float64 fa = t_to_float64(a);
1025 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
1028 uint64_t helper_cvtqt (uint64_t a)
1030 float64 fr = int64_to_float64(a, &FP_STATUS);
1031 return float64_to_t(fr);
1034 uint64_t helper_cvtqf (uint64_t a)
1036 float32 fr = int64_to_float32(a, &FP_STATUS);
1037 return float32_to_f(fr);
1040 uint64_t helper_cvtgf (uint64_t a)
1042 float64 fa;
1043 float32 fr;
1045 fa = g_to_float64(a);
1046 fr = float64_to_float32(fa, &FP_STATUS);
1047 return float32_to_f(fr);
1050 uint64_t helper_cvtgq (uint64_t a)
1052 float64 fa = g_to_float64(a);
1053 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
1056 uint64_t helper_cvtqg (uint64_t a)
1058 float64 fr;
1059 fr = int64_to_float64(a, &FP_STATUS);
1060 return float64_to_g(fr);
1063 uint64_t helper_cvtlq (uint64_t a)
1065 return (int64_t)((int32_t)((a >> 32) | ((a >> 29) & 0x3FFFFFFF)));
1068 static inline uint64_t __helper_cvtql(uint64_t a, int s, int v)
1070 uint64_t r;
1072 r = ((uint64_t)(a & 0xC0000000)) << 32;
1073 r |= ((uint64_t)(a & 0x7FFFFFFF)) << 29;
1075 if (v && (int64_t)((int32_t)r) != (int64_t)r) {
1076 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
1078 if (s) {
1079 /* TODO */
1081 return r;
1084 uint64_t helper_cvtql (uint64_t a)
1086 return __helper_cvtql(a, 0, 0);
1089 uint64_t helper_cvtqlv (uint64_t a)
1091 return __helper_cvtql(a, 0, 1);
1094 uint64_t helper_cvtqlsv (uint64_t a)
1096 return __helper_cvtql(a, 1, 1);
1099 /* PALcode support special instructions */
1100 #if !defined (CONFIG_USER_ONLY)
1101 void helper_hw_rei (void)
1103 env->pc = env->ipr[IPR_EXC_ADDR] & ~3;
1104 env->ipr[IPR_EXC_ADDR] = env->ipr[IPR_EXC_ADDR] & 1;
1105 /* XXX: re-enable interrupts and memory mapping */
1108 void helper_hw_ret (uint64_t a)
1110 env->pc = a & ~3;
1111 env->ipr[IPR_EXC_ADDR] = a & 1;
1112 /* XXX: re-enable interrupts and memory mapping */
1115 uint64_t helper_mfpr (int iprn, uint64_t val)
1117 uint64_t tmp;
1119 if (cpu_alpha_mfpr(env, iprn, &tmp) == 0)
1120 val = tmp;
1122 return val;
1125 void helper_mtpr (int iprn, uint64_t val)
1127 cpu_alpha_mtpr(env, iprn, val, NULL);
1130 void helper_set_alt_mode (void)
1132 env->saved_mode = env->ps & 0xC;
1133 env->ps = (env->ps & ~0xC) | (env->ipr[IPR_ALT_MODE] & 0xC);
1136 void helper_restore_mode (void)
1138 env->ps = (env->ps & ~0xC) | env->saved_mode;
1141 #endif
1143 /*****************************************************************************/
1144 /* Softmmu support */
1145 #if !defined (CONFIG_USER_ONLY)
1147 /* XXX: the two following helpers are pure hacks.
1148 * Hopefully, we emulate the PALcode, then we should never see
1149 * HW_LD / HW_ST instructions.
1151 uint64_t helper_ld_virt_to_phys (uint64_t virtaddr)
1153 uint64_t tlb_addr, physaddr;
1154 int index, mmu_idx;
1155 void *retaddr;
1157 mmu_idx = cpu_mmu_index(env);
1158 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1159 redo:
1160 tlb_addr = env->tlb_table[mmu_idx][index].addr_read;
1161 if ((virtaddr & TARGET_PAGE_MASK) ==
1162 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1163 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1164 } else {
1165 /* the page is not in the TLB : fill it */
1166 retaddr = GETPC();
1167 tlb_fill(virtaddr, 0, mmu_idx, retaddr);
1168 goto redo;
1170 return physaddr;
1173 uint64_t helper_st_virt_to_phys (uint64_t virtaddr)
1175 uint64_t tlb_addr, physaddr;
1176 int index, mmu_idx;
1177 void *retaddr;
1179 mmu_idx = cpu_mmu_index(env);
1180 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1181 redo:
1182 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
1183 if ((virtaddr & TARGET_PAGE_MASK) ==
1184 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1185 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1186 } else {
1187 /* the page is not in the TLB : fill it */
1188 retaddr = GETPC();
1189 tlb_fill(virtaddr, 1, mmu_idx, retaddr);
1190 goto redo;
1192 return physaddr;
1195 void helper_ldl_raw(uint64_t t0, uint64_t t1)
1197 ldl_raw(t1, t0);
1200 void helper_ldq_raw(uint64_t t0, uint64_t t1)
1202 ldq_raw(t1, t0);
1205 void helper_ldl_l_raw(uint64_t t0, uint64_t t1)
1207 env->lock = t1;
1208 ldl_raw(t1, t0);
1211 void helper_ldq_l_raw(uint64_t t0, uint64_t t1)
1213 env->lock = t1;
1214 ldl_raw(t1, t0);
1217 void helper_ldl_kernel(uint64_t t0, uint64_t t1)
1219 ldl_kernel(t1, t0);
1222 void helper_ldq_kernel(uint64_t t0, uint64_t t1)
1224 ldq_kernel(t1, t0);
1227 void helper_ldl_data(uint64_t t0, uint64_t t1)
1229 ldl_data(t1, t0);
1232 void helper_ldq_data(uint64_t t0, uint64_t t1)
1234 ldq_data(t1, t0);
1237 void helper_stl_raw(uint64_t t0, uint64_t t1)
1239 stl_raw(t1, t0);
1242 void helper_stq_raw(uint64_t t0, uint64_t t1)
1244 stq_raw(t1, t0);
1247 uint64_t helper_stl_c_raw(uint64_t t0, uint64_t t1)
1249 uint64_t ret;
1251 if (t1 == env->lock) {
1252 stl_raw(t1, t0);
1253 ret = 0;
1254 } else
1255 ret = 1;
1257 env->lock = 1;
1259 return ret;
1262 uint64_t helper_stq_c_raw(uint64_t t0, uint64_t t1)
1264 uint64_t ret;
1266 if (t1 == env->lock) {
1267 stq_raw(t1, t0);
1268 ret = 0;
1269 } else
1270 ret = 1;
1272 env->lock = 1;
1274 return ret;
1277 #define MMUSUFFIX _mmu
1279 #define SHIFT 0
1280 #include "softmmu_template.h"
1282 #define SHIFT 1
1283 #include "softmmu_template.h"
1285 #define SHIFT 2
1286 #include "softmmu_template.h"
1288 #define SHIFT 3
1289 #include "softmmu_template.h"
1291 /* try to fill the TLB and return an exception if error. If retaddr is
1292 NULL, it means that the function was called in C code (i.e. not
1293 from generated code or from helper.c) */
1294 /* XXX: fix it to restore all registers */
1295 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1297 TranslationBlock *tb;
1298 CPUState *saved_env;
1299 unsigned long pc;
1300 int ret;
1302 /* XXX: hack to restore env in all cases, even if not called from
1303 generated code */
1304 saved_env = env;
1305 env = cpu_single_env;
1306 ret = cpu_alpha_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1307 if (!likely(ret == 0)) {
1308 if (likely(retaddr)) {
1309 /* now we have a real cpu fault */
1310 pc = (unsigned long)retaddr;
1311 tb = tb_find_pc(pc);
1312 if (likely(tb)) {
1313 /* the PC is inside the translated code. It means that we have
1314 a virtual CPU fault */
1315 cpu_restore_state(tb, env, pc, NULL);
1318 /* Exception index and error code are already set */
1319 cpu_loop_exit();
1321 env = saved_env;
1324 #endif