Unexport ticks_per_sec variable. Create get_ticks_per_sec() function
[qemu.git] / target-alpha / op_helper.c
blobe9d2732dfb541f5b6bfb9d17788ab05d615f4f16
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 void helper_tb_flush (void)
27 tb_flush(env);
30 /*****************************************************************************/
31 /* Exceptions processing helpers */
32 void helper_excp (int excp, int error)
34 env->exception_index = excp;
35 env->error_code = error;
36 cpu_loop_exit();
39 uint64_t helper_load_pcc (void)
41 /* XXX: TODO */
42 return 0;
45 uint64_t helper_load_fpcr (void)
47 uint64_t ret = 0;
48 #ifdef CONFIG_SOFTFLOAT
49 ret |= env->fp_status.float_exception_flags << 52;
50 if (env->fp_status.float_exception_flags)
51 ret |= 1ULL << 63;
52 env->ipr[IPR_EXC_SUM] &= ~0x3E:
53 env->ipr[IPR_EXC_SUM] |= env->fp_status.float_exception_flags << 1;
54 #endif
55 switch (env->fp_status.float_rounding_mode) {
56 case float_round_nearest_even:
57 ret |= 2ULL << 58;
58 break;
59 case float_round_down:
60 ret |= 1ULL << 58;
61 break;
62 case float_round_up:
63 ret |= 3ULL << 58;
64 break;
65 case float_round_to_zero:
66 break;
68 return ret;
71 void helper_store_fpcr (uint64_t val)
73 #ifdef CONFIG_SOFTFLOAT
74 set_float_exception_flags((val >> 52) & 0x3F, &FP_STATUS);
75 #endif
76 switch ((val >> 58) & 3) {
77 case 0:
78 set_float_rounding_mode(float_round_to_zero, &FP_STATUS);
79 break;
80 case 1:
81 set_float_rounding_mode(float_round_down, &FP_STATUS);
82 break;
83 case 2:
84 set_float_rounding_mode(float_round_nearest_even, &FP_STATUS);
85 break;
86 case 3:
87 set_float_rounding_mode(float_round_up, &FP_STATUS);
88 break;
92 spinlock_t intr_cpu_lock = SPIN_LOCK_UNLOCKED;
94 uint64_t helper_rs(void)
96 uint64_t tmp;
98 spin_lock(&intr_cpu_lock);
99 tmp = env->intr_flag;
100 env->intr_flag = 1;
101 spin_unlock(&intr_cpu_lock);
103 return tmp;
106 uint64_t helper_rc(void)
108 uint64_t tmp;
110 spin_lock(&intr_cpu_lock);
111 tmp = env->intr_flag;
112 env->intr_flag = 0;
113 spin_unlock(&intr_cpu_lock);
115 return tmp;
118 uint64_t helper_addqv (uint64_t op1, uint64_t op2)
120 uint64_t tmp = op1;
121 op1 += op2;
122 if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
123 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
125 return op1;
128 uint64_t helper_addlv (uint64_t op1, uint64_t op2)
130 uint64_t tmp = op1;
131 op1 = (uint32_t)(op1 + op2);
132 if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
133 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
135 return op1;
138 uint64_t helper_subqv (uint64_t op1, uint64_t op2)
140 uint64_t res;
141 res = op1 - op2;
142 if (unlikely((op1 ^ op2) & (res ^ op1) & (1ULL << 63))) {
143 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
145 return res;
148 uint64_t helper_sublv (uint64_t op1, uint64_t op2)
150 uint32_t res;
151 res = op1 - op2;
152 if (unlikely((op1 ^ op2) & (res ^ op1) & (1UL << 31))) {
153 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
155 return res;
158 uint64_t helper_mullv (uint64_t op1, uint64_t op2)
160 int64_t res = (int64_t)op1 * (int64_t)op2;
162 if (unlikely((int32_t)res != res)) {
163 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
165 return (int64_t)((int32_t)res);
168 uint64_t helper_mulqv (uint64_t op1, uint64_t op2)
170 uint64_t tl, th;
172 muls64(&tl, &th, op1, op2);
173 /* If th != 0 && th != -1, then we had an overflow */
174 if (unlikely((th + 1) > 1)) {
175 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
177 return tl;
180 uint64_t helper_umulh (uint64_t op1, uint64_t op2)
182 uint64_t tl, th;
184 mulu64(&tl, &th, op1, op2);
185 return th;
188 uint64_t helper_ctpop (uint64_t arg)
190 return ctpop64(arg);
193 uint64_t helper_ctlz (uint64_t arg)
195 return clz64(arg);
198 uint64_t helper_cttz (uint64_t arg)
200 return ctz64(arg);
203 static inline uint64_t byte_zap(uint64_t op, uint8_t mskb)
205 uint64_t mask;
207 mask = 0;
208 mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
209 mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
210 mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
211 mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
212 mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
213 mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
214 mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
215 mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;
217 return op & ~mask;
220 uint64_t helper_mskbl(uint64_t val, uint64_t mask)
222 return byte_zap(val, 0x01 << (mask & 7));
225 uint64_t helper_insbl(uint64_t val, uint64_t mask)
227 val <<= (mask & 7) * 8;
228 return byte_zap(val, ~(0x01 << (mask & 7)));
231 uint64_t helper_mskwl(uint64_t val, uint64_t mask)
233 return byte_zap(val, 0x03 << (mask & 7));
236 uint64_t helper_inswl(uint64_t val, uint64_t mask)
238 val <<= (mask & 7) * 8;
239 return byte_zap(val, ~(0x03 << (mask & 7)));
242 uint64_t helper_mskll(uint64_t val, uint64_t mask)
244 return byte_zap(val, 0x0F << (mask & 7));
247 uint64_t helper_insll(uint64_t val, uint64_t mask)
249 val <<= (mask & 7) * 8;
250 return byte_zap(val, ~(0x0F << (mask & 7)));
253 uint64_t helper_zap(uint64_t val, uint64_t mask)
255 return byte_zap(val, mask);
258 uint64_t helper_zapnot(uint64_t val, uint64_t mask)
260 return byte_zap(val, ~mask);
263 uint64_t helper_mskql(uint64_t val, uint64_t mask)
265 return byte_zap(val, 0xFF << (mask & 7));
268 uint64_t helper_insql(uint64_t val, uint64_t mask)
270 val <<= (mask & 7) * 8;
271 return byte_zap(val, ~(0xFF << (mask & 7)));
274 uint64_t helper_mskwh(uint64_t val, uint64_t mask)
276 return byte_zap(val, (0x03 << (mask & 7)) >> 8);
279 uint64_t helper_inswh(uint64_t val, uint64_t mask)
281 val >>= 64 - ((mask & 7) * 8);
282 return byte_zap(val, ~((0x03 << (mask & 7)) >> 8));
285 uint64_t helper_msklh(uint64_t val, uint64_t mask)
287 return byte_zap(val, (0x0F << (mask & 7)) >> 8);
290 uint64_t helper_inslh(uint64_t val, uint64_t mask)
292 val >>= 64 - ((mask & 7) * 8);
293 return byte_zap(val, ~((0x0F << (mask & 7)) >> 8));
296 uint64_t helper_mskqh(uint64_t val, uint64_t mask)
298 return byte_zap(val, (0xFF << (mask & 7)) >> 8);
301 uint64_t helper_insqh(uint64_t val, uint64_t mask)
303 val >>= 64 - ((mask & 7) * 8);
304 return byte_zap(val, ~((0xFF << (mask & 7)) >> 8));
307 uint64_t helper_cmpbge (uint64_t op1, uint64_t op2)
309 uint8_t opa, opb, res;
310 int i;
312 res = 0;
313 for (i = 0; i < 8; i++) {
314 opa = op1 >> (i * 8);
315 opb = op2 >> (i * 8);
316 if (opa >= opb)
317 res |= 1 << i;
319 return res;
322 /* Floating point helpers */
324 /* F floating (VAX) */
325 static inline uint64_t float32_to_f(float32 fa)
327 uint64_t r, exp, mant, sig;
328 CPU_FloatU a;
330 a.f = fa;
331 sig = ((uint64_t)a.l & 0x80000000) << 32;
332 exp = (a.l >> 23) & 0xff;
333 mant = ((uint64_t)a.l & 0x007fffff) << 29;
335 if (exp == 255) {
336 /* NaN or infinity */
337 r = 1; /* VAX dirty zero */
338 } else if (exp == 0) {
339 if (mant == 0) {
340 /* Zero */
341 r = 0;
342 } else {
343 /* Denormalized */
344 r = sig | ((exp + 1) << 52) | mant;
346 } else {
347 if (exp >= 253) {
348 /* Overflow */
349 r = 1; /* VAX dirty zero */
350 } else {
351 r = sig | ((exp + 2) << 52);
355 return r;
358 static inline float32 f_to_float32(uint64_t a)
360 uint32_t exp, mant_sig;
361 CPU_FloatU r;
363 exp = ((a >> 55) & 0x80) | ((a >> 52) & 0x7f);
364 mant_sig = ((a >> 32) & 0x80000000) | ((a >> 29) & 0x007fffff);
366 if (unlikely(!exp && mant_sig)) {
367 /* Reserved operands / Dirty zero */
368 helper_excp(EXCP_OPCDEC, 0);
371 if (exp < 3) {
372 /* Underflow */
373 r.l = 0;
374 } else {
375 r.l = ((exp - 2) << 23) | mant_sig;
378 return r.f;
381 uint32_t helper_f_to_memory (uint64_t a)
383 uint32_t r;
384 r = (a & 0x00001fffe0000000ull) >> 13;
385 r |= (a & 0x07ffe00000000000ull) >> 45;
386 r |= (a & 0xc000000000000000ull) >> 48;
387 return r;
390 uint64_t helper_memory_to_f (uint32_t a)
392 uint64_t r;
393 r = ((uint64_t)(a & 0x0000c000)) << 48;
394 r |= ((uint64_t)(a & 0x003fffff)) << 45;
395 r |= ((uint64_t)(a & 0xffff0000)) << 13;
396 if (!(a & 0x00004000))
397 r |= 0x7ll << 59;
398 return r;
401 uint64_t helper_addf (uint64_t a, uint64_t b)
403 float32 fa, fb, fr;
405 fa = f_to_float32(a);
406 fb = f_to_float32(b);
407 fr = float32_add(fa, fb, &FP_STATUS);
408 return float32_to_f(fr);
411 uint64_t helper_subf (uint64_t a, uint64_t b)
413 float32 fa, fb, fr;
415 fa = f_to_float32(a);
416 fb = f_to_float32(b);
417 fr = float32_sub(fa, fb, &FP_STATUS);
418 return float32_to_f(fr);
421 uint64_t helper_mulf (uint64_t a, uint64_t b)
423 float32 fa, fb, fr;
425 fa = f_to_float32(a);
426 fb = f_to_float32(b);
427 fr = float32_mul(fa, fb, &FP_STATUS);
428 return float32_to_f(fr);
431 uint64_t helper_divf (uint64_t a, uint64_t b)
433 float32 fa, fb, fr;
435 fa = f_to_float32(a);
436 fb = f_to_float32(b);
437 fr = float32_div(fa, fb, &FP_STATUS);
438 return float32_to_f(fr);
441 uint64_t helper_sqrtf (uint64_t t)
443 float32 ft, fr;
445 ft = f_to_float32(t);
446 fr = float32_sqrt(ft, &FP_STATUS);
447 return float32_to_f(fr);
451 /* G floating (VAX) */
452 static inline uint64_t float64_to_g(float64 fa)
454 uint64_t r, exp, mant, sig;
455 CPU_DoubleU a;
457 a.d = fa;
458 sig = a.ll & 0x8000000000000000ull;
459 exp = (a.ll >> 52) & 0x7ff;
460 mant = a.ll & 0x000fffffffffffffull;
462 if (exp == 2047) {
463 /* NaN or infinity */
464 r = 1; /* VAX dirty zero */
465 } else if (exp == 0) {
466 if (mant == 0) {
467 /* Zero */
468 r = 0;
469 } else {
470 /* Denormalized */
471 r = sig | ((exp + 1) << 52) | mant;
473 } else {
474 if (exp >= 2045) {
475 /* Overflow */
476 r = 1; /* VAX dirty zero */
477 } else {
478 r = sig | ((exp + 2) << 52);
482 return r;
485 static inline float64 g_to_float64(uint64_t a)
487 uint64_t exp, mant_sig;
488 CPU_DoubleU r;
490 exp = (a >> 52) & 0x7ff;
491 mant_sig = a & 0x800fffffffffffffull;
493 if (!exp && mant_sig) {
494 /* Reserved operands / Dirty zero */
495 helper_excp(EXCP_OPCDEC, 0);
498 if (exp < 3) {
499 /* Underflow */
500 r.ll = 0;
501 } else {
502 r.ll = ((exp - 2) << 52) | mant_sig;
505 return r.d;
508 uint64_t helper_g_to_memory (uint64_t a)
510 uint64_t r;
511 r = (a & 0x000000000000ffffull) << 48;
512 r |= (a & 0x00000000ffff0000ull) << 16;
513 r |= (a & 0x0000ffff00000000ull) >> 16;
514 r |= (a & 0xffff000000000000ull) >> 48;
515 return r;
518 uint64_t helper_memory_to_g (uint64_t a)
520 uint64_t r;
521 r = (a & 0x000000000000ffffull) << 48;
522 r |= (a & 0x00000000ffff0000ull) << 16;
523 r |= (a & 0x0000ffff00000000ull) >> 16;
524 r |= (a & 0xffff000000000000ull) >> 48;
525 return r;
528 uint64_t helper_addg (uint64_t a, uint64_t b)
530 float64 fa, fb, fr;
532 fa = g_to_float64(a);
533 fb = g_to_float64(b);
534 fr = float64_add(fa, fb, &FP_STATUS);
535 return float64_to_g(fr);
538 uint64_t helper_subg (uint64_t a, uint64_t b)
540 float64 fa, fb, fr;
542 fa = g_to_float64(a);
543 fb = g_to_float64(b);
544 fr = float64_sub(fa, fb, &FP_STATUS);
545 return float64_to_g(fr);
548 uint64_t helper_mulg (uint64_t a, uint64_t b)
550 float64 fa, fb, fr;
552 fa = g_to_float64(a);
553 fb = g_to_float64(b);
554 fr = float64_mul(fa, fb, &FP_STATUS);
555 return float64_to_g(fr);
558 uint64_t helper_divg (uint64_t a, uint64_t b)
560 float64 fa, fb, fr;
562 fa = g_to_float64(a);
563 fb = g_to_float64(b);
564 fr = float64_div(fa, fb, &FP_STATUS);
565 return float64_to_g(fr);
568 uint64_t helper_sqrtg (uint64_t a)
570 float64 fa, fr;
572 fa = g_to_float64(a);
573 fr = float64_sqrt(fa, &FP_STATUS);
574 return float64_to_g(fr);
578 /* S floating (single) */
579 static inline uint64_t float32_to_s(float32 fa)
581 CPU_FloatU a;
582 uint64_t r;
584 a.f = fa;
586 r = (((uint64_t)(a.l & 0xc0000000)) << 32) | (((uint64_t)(a.l & 0x3fffffff)) << 29);
587 if (((a.l & 0x7f800000) != 0x7f800000) && (!(a.l & 0x40000000)))
588 r |= 0x7ll << 59;
589 return r;
592 static inline float32 s_to_float32(uint64_t a)
594 CPU_FloatU r;
595 r.l = ((a >> 32) & 0xc0000000) | ((a >> 29) & 0x3fffffff);
596 return r.f;
599 uint32_t helper_s_to_memory (uint64_t a)
601 /* Memory format is the same as float32 */
602 float32 fa = s_to_float32(a);
603 return *(uint32_t*)(&fa);
606 uint64_t helper_memory_to_s (uint32_t a)
608 /* Memory format is the same as float32 */
609 return float32_to_s(*(float32*)(&a));
612 uint64_t helper_adds (uint64_t a, uint64_t b)
614 float32 fa, fb, fr;
616 fa = s_to_float32(a);
617 fb = s_to_float32(b);
618 fr = float32_add(fa, fb, &FP_STATUS);
619 return float32_to_s(fr);
622 uint64_t helper_subs (uint64_t a, uint64_t b)
624 float32 fa, fb, fr;
626 fa = s_to_float32(a);
627 fb = s_to_float32(b);
628 fr = float32_sub(fa, fb, &FP_STATUS);
629 return float32_to_s(fr);
632 uint64_t helper_muls (uint64_t a, uint64_t b)
634 float32 fa, fb, fr;
636 fa = s_to_float32(a);
637 fb = s_to_float32(b);
638 fr = float32_mul(fa, fb, &FP_STATUS);
639 return float32_to_s(fr);
642 uint64_t helper_divs (uint64_t a, uint64_t b)
644 float32 fa, fb, fr;
646 fa = s_to_float32(a);
647 fb = s_to_float32(b);
648 fr = float32_div(fa, fb, &FP_STATUS);
649 return float32_to_s(fr);
652 uint64_t helper_sqrts (uint64_t a)
654 float32 fa, fr;
656 fa = s_to_float32(a);
657 fr = float32_sqrt(fa, &FP_STATUS);
658 return float32_to_s(fr);
662 /* T floating (double) */
663 static inline float64 t_to_float64(uint64_t a)
665 /* Memory format is the same as float64 */
666 CPU_DoubleU r;
667 r.ll = a;
668 return r.d;
671 static inline uint64_t float64_to_t(float64 fa)
673 /* Memory format is the same as float64 */
674 CPU_DoubleU r;
675 r.d = fa;
676 return r.ll;
679 uint64_t helper_addt (uint64_t a, uint64_t b)
681 float64 fa, fb, fr;
683 fa = t_to_float64(a);
684 fb = t_to_float64(b);
685 fr = float64_add(fa, fb, &FP_STATUS);
686 return float64_to_t(fr);
689 uint64_t helper_subt (uint64_t a, uint64_t b)
691 float64 fa, fb, fr;
693 fa = t_to_float64(a);
694 fb = t_to_float64(b);
695 fr = float64_sub(fa, fb, &FP_STATUS);
696 return float64_to_t(fr);
699 uint64_t helper_mult (uint64_t a, uint64_t b)
701 float64 fa, fb, fr;
703 fa = t_to_float64(a);
704 fb = t_to_float64(b);
705 fr = float64_mul(fa, fb, &FP_STATUS);
706 return float64_to_t(fr);
709 uint64_t helper_divt (uint64_t a, uint64_t b)
711 float64 fa, fb, fr;
713 fa = t_to_float64(a);
714 fb = t_to_float64(b);
715 fr = float64_div(fa, fb, &FP_STATUS);
716 return float64_to_t(fr);
719 uint64_t helper_sqrtt (uint64_t a)
721 float64 fa, fr;
723 fa = t_to_float64(a);
724 fr = float64_sqrt(fa, &FP_STATUS);
725 return float64_to_t(fr);
729 /* Sign copy */
730 uint64_t helper_cpys(uint64_t a, uint64_t b)
732 return (a & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
735 uint64_t helper_cpysn(uint64_t a, uint64_t b)
737 return ((~a) & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
740 uint64_t helper_cpyse(uint64_t a, uint64_t b)
742 return (a & 0xFFF0000000000000ULL) | (b & ~0xFFF0000000000000ULL);
746 /* Comparisons */
747 uint64_t helper_cmptun (uint64_t a, uint64_t b)
749 float64 fa, fb;
751 fa = t_to_float64(a);
752 fb = t_to_float64(b);
754 if (float64_is_nan(fa) || float64_is_nan(fb))
755 return 0x4000000000000000ULL;
756 else
757 return 0;
760 uint64_t helper_cmpteq(uint64_t a, uint64_t b)
762 float64 fa, fb;
764 fa = t_to_float64(a);
765 fb = t_to_float64(b);
767 if (float64_eq(fa, fb, &FP_STATUS))
768 return 0x4000000000000000ULL;
769 else
770 return 0;
773 uint64_t helper_cmptle(uint64_t a, uint64_t b)
775 float64 fa, fb;
777 fa = t_to_float64(a);
778 fb = t_to_float64(b);
780 if (float64_le(fa, fb, &FP_STATUS))
781 return 0x4000000000000000ULL;
782 else
783 return 0;
786 uint64_t helper_cmptlt(uint64_t a, uint64_t b)
788 float64 fa, fb;
790 fa = t_to_float64(a);
791 fb = t_to_float64(b);
793 if (float64_lt(fa, fb, &FP_STATUS))
794 return 0x4000000000000000ULL;
795 else
796 return 0;
799 uint64_t helper_cmpgeq(uint64_t a, uint64_t b)
801 float64 fa, fb;
803 fa = g_to_float64(a);
804 fb = g_to_float64(b);
806 if (float64_eq(fa, fb, &FP_STATUS))
807 return 0x4000000000000000ULL;
808 else
809 return 0;
812 uint64_t helper_cmpgle(uint64_t a, uint64_t b)
814 float64 fa, fb;
816 fa = g_to_float64(a);
817 fb = g_to_float64(b);
819 if (float64_le(fa, fb, &FP_STATUS))
820 return 0x4000000000000000ULL;
821 else
822 return 0;
825 uint64_t helper_cmpglt(uint64_t a, uint64_t b)
827 float64 fa, fb;
829 fa = g_to_float64(a);
830 fb = g_to_float64(b);
832 if (float64_lt(fa, fb, &FP_STATUS))
833 return 0x4000000000000000ULL;
834 else
835 return 0;
838 uint64_t helper_cmpfeq (uint64_t a)
840 return !(a & 0x7FFFFFFFFFFFFFFFULL);
843 uint64_t helper_cmpfne (uint64_t a)
845 return (a & 0x7FFFFFFFFFFFFFFFULL);
848 uint64_t helper_cmpflt (uint64_t a)
850 return (a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
853 uint64_t helper_cmpfle (uint64_t a)
855 return (a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
858 uint64_t helper_cmpfgt (uint64_t a)
860 return !(a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
863 uint64_t helper_cmpfge (uint64_t a)
865 return !(a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
869 /* Floating point format conversion */
870 uint64_t helper_cvtts (uint64_t a)
872 float64 fa;
873 float32 fr;
875 fa = t_to_float64(a);
876 fr = float64_to_float32(fa, &FP_STATUS);
877 return float32_to_s(fr);
880 uint64_t helper_cvtst (uint64_t a)
882 float32 fa;
883 float64 fr;
885 fa = s_to_float32(a);
886 fr = float32_to_float64(fa, &FP_STATUS);
887 return float64_to_t(fr);
890 uint64_t helper_cvtqs (uint64_t a)
892 float32 fr = int64_to_float32(a, &FP_STATUS);
893 return float32_to_s(fr);
896 uint64_t helper_cvttq (uint64_t a)
898 float64 fa = t_to_float64(a);
899 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
902 uint64_t helper_cvtqt (uint64_t a)
904 float64 fr = int64_to_float64(a, &FP_STATUS);
905 return float64_to_t(fr);
908 uint64_t helper_cvtqf (uint64_t a)
910 float32 fr = int64_to_float32(a, &FP_STATUS);
911 return float32_to_f(fr);
914 uint64_t helper_cvtgf (uint64_t a)
916 float64 fa;
917 float32 fr;
919 fa = g_to_float64(a);
920 fr = float64_to_float32(fa, &FP_STATUS);
921 return float32_to_f(fr);
924 uint64_t helper_cvtgq (uint64_t a)
926 float64 fa = g_to_float64(a);
927 return float64_to_int64_round_to_zero(fa, &FP_STATUS);
930 uint64_t helper_cvtqg (uint64_t a)
932 float64 fr;
933 fr = int64_to_float64(a, &FP_STATUS);
934 return float64_to_g(fr);
937 uint64_t helper_cvtlq (uint64_t a)
939 return (int64_t)((int32_t)((a >> 32) | ((a >> 29) & 0x3FFFFFFF)));
942 static inline uint64_t __helper_cvtql(uint64_t a, int s, int v)
944 uint64_t r;
946 r = ((uint64_t)(a & 0xC0000000)) << 32;
947 r |= ((uint64_t)(a & 0x7FFFFFFF)) << 29;
949 if (v && (int64_t)((int32_t)r) != (int64_t)r) {
950 helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
952 if (s) {
953 /* TODO */
955 return r;
958 uint64_t helper_cvtql (uint64_t a)
960 return __helper_cvtql(a, 0, 0);
963 uint64_t helper_cvtqlv (uint64_t a)
965 return __helper_cvtql(a, 0, 1);
968 uint64_t helper_cvtqlsv (uint64_t a)
970 return __helper_cvtql(a, 1, 1);
973 /* PALcode support special instructions */
974 #if !defined (CONFIG_USER_ONLY)
975 void helper_hw_rei (void)
977 env->pc = env->ipr[IPR_EXC_ADDR] & ~3;
978 env->ipr[IPR_EXC_ADDR] = env->ipr[IPR_EXC_ADDR] & 1;
979 /* XXX: re-enable interrupts and memory mapping */
982 void helper_hw_ret (uint64_t a)
984 env->pc = a & ~3;
985 env->ipr[IPR_EXC_ADDR] = a & 1;
986 /* XXX: re-enable interrupts and memory mapping */
989 uint64_t helper_mfpr (int iprn, uint64_t val)
991 uint64_t tmp;
993 if (cpu_alpha_mfpr(env, iprn, &tmp) == 0)
994 val = tmp;
996 return val;
999 void helper_mtpr (int iprn, uint64_t val)
1001 cpu_alpha_mtpr(env, iprn, val, NULL);
1004 void helper_set_alt_mode (void)
1006 env->saved_mode = env->ps & 0xC;
1007 env->ps = (env->ps & ~0xC) | (env->ipr[IPR_ALT_MODE] & 0xC);
1010 void helper_restore_mode (void)
1012 env->ps = (env->ps & ~0xC) | env->saved_mode;
1015 #endif
1017 /*****************************************************************************/
1018 /* Softmmu support */
1019 #if !defined (CONFIG_USER_ONLY)
1021 /* XXX: the two following helpers are pure hacks.
1022 * Hopefully, we emulate the PALcode, then we should never see
1023 * HW_LD / HW_ST instructions.
1025 uint64_t helper_ld_virt_to_phys (uint64_t virtaddr)
1027 uint64_t tlb_addr, physaddr;
1028 int index, mmu_idx;
1029 void *retaddr;
1031 mmu_idx = cpu_mmu_index(env);
1032 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1033 redo:
1034 tlb_addr = env->tlb_table[mmu_idx][index].addr_read;
1035 if ((virtaddr & TARGET_PAGE_MASK) ==
1036 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1037 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1038 } else {
1039 /* the page is not in the TLB : fill it */
1040 retaddr = GETPC();
1041 tlb_fill(virtaddr, 0, mmu_idx, retaddr);
1042 goto redo;
1044 return physaddr;
1047 uint64_t helper_st_virt_to_phys (uint64_t virtaddr)
1049 uint64_t tlb_addr, physaddr;
1050 int index, mmu_idx;
1051 void *retaddr;
1053 mmu_idx = cpu_mmu_index(env);
1054 index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1055 redo:
1056 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
1057 if ((virtaddr & TARGET_PAGE_MASK) ==
1058 (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1059 physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1060 } else {
1061 /* the page is not in the TLB : fill it */
1062 retaddr = GETPC();
1063 tlb_fill(virtaddr, 1, mmu_idx, retaddr);
1064 goto redo;
1066 return physaddr;
1069 void helper_ldl_raw(uint64_t t0, uint64_t t1)
1071 ldl_raw(t1, t0);
1074 void helper_ldq_raw(uint64_t t0, uint64_t t1)
1076 ldq_raw(t1, t0);
1079 void helper_ldl_l_raw(uint64_t t0, uint64_t t1)
1081 env->lock = t1;
1082 ldl_raw(t1, t0);
1085 void helper_ldq_l_raw(uint64_t t0, uint64_t t1)
1087 env->lock = t1;
1088 ldl_raw(t1, t0);
1091 void helper_ldl_kernel(uint64_t t0, uint64_t t1)
1093 ldl_kernel(t1, t0);
1096 void helper_ldq_kernel(uint64_t t0, uint64_t t1)
1098 ldq_kernel(t1, t0);
1101 void helper_ldl_data(uint64_t t0, uint64_t t1)
1103 ldl_data(t1, t0);
1106 void helper_ldq_data(uint64_t t0, uint64_t t1)
1108 ldq_data(t1, t0);
1111 void helper_stl_raw(uint64_t t0, uint64_t t1)
1113 stl_raw(t1, t0);
1116 void helper_stq_raw(uint64_t t0, uint64_t t1)
1118 stq_raw(t1, t0);
1121 uint64_t helper_stl_c_raw(uint64_t t0, uint64_t t1)
1123 uint64_t ret;
1125 if (t1 == env->lock) {
1126 stl_raw(t1, t0);
1127 ret = 0;
1128 } else
1129 ret = 1;
1131 env->lock = 1;
1133 return ret;
1136 uint64_t helper_stq_c_raw(uint64_t t0, uint64_t t1)
1138 uint64_t ret;
1140 if (t1 == env->lock) {
1141 stq_raw(t1, t0);
1142 ret = 0;
1143 } else
1144 ret = 1;
1146 env->lock = 1;
1148 return ret;
1151 #define MMUSUFFIX _mmu
1153 #define SHIFT 0
1154 #include "softmmu_template.h"
1156 #define SHIFT 1
1157 #include "softmmu_template.h"
1159 #define SHIFT 2
1160 #include "softmmu_template.h"
1162 #define SHIFT 3
1163 #include "softmmu_template.h"
1165 /* try to fill the TLB and return an exception if error. If retaddr is
1166 NULL, it means that the function was called in C code (i.e. not
1167 from generated code or from helper.c) */
1168 /* XXX: fix it to restore all registers */
1169 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1171 TranslationBlock *tb;
1172 CPUState *saved_env;
1173 unsigned long pc;
1174 int ret;
1176 /* XXX: hack to restore env in all cases, even if not called from
1177 generated code */
1178 saved_env = env;
1179 env = cpu_single_env;
1180 ret = cpu_alpha_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1181 if (!likely(ret == 0)) {
1182 if (likely(retaddr)) {
1183 /* now we have a real cpu fault */
1184 pc = (unsigned long)retaddr;
1185 tb = tb_find_pc(pc);
1186 if (likely(tb)) {
1187 /* the PC is inside the translated code. It means that we have
1188 a virtual CPU fault */
1189 cpu_restore_state(tb, env, pc, NULL);
1192 /* Exception index and error code are already set */
1193 cpu_loop_exit();
1195 env = saved_env;
1198 #endif