2 /* MIPS Simulator FPU (CoProcessor 1) support.
3 Copyright (C) 2002, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Originally created by Cygnus Solutions. Extensive modifications,
6 including paired-single operation support and MIPS-3D support
7 contributed by Ed Satterthwaite and Chris Demetriou, of Broadcom
10 This file is part of GDB, the GNU debugger.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 /* XXX: The following notice should be removed as soon as is practical: */
26 /* Floating Point Support for gdb MIPS simulators
28 This file is part of the MIPS sim
30 THIS SOFTWARE IS NOT COPYRIGHTED
33 Cygnus offers the following for use in the public domain. Cygnus
34 makes no warranty with regard to the software or it's performance
35 and the user accepts the software "AS IS" with all faults.
37 CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
38 THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41 (Originally, this code was in interp.c)
46 /* Within cp1.c we refer to sim_cpu directly. */
48 #define SD CPU_STATE(cpu)
50 /*-- FPU support routines ---------------------------------------------------*/
52 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
53 formats conform to ANSI/IEEE Std 754-1985.
55 SINGLE precision floating:
56 seeeeeeeefffffffffffffffffffffff
61 SINGLE precision fixed:
62 siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
66 DOUBLE precision floating:
67 seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
72 DOUBLE precision fixed:
73 siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
77 PAIRED SINGLE precision floating:
78 seeeeeeeefffffffffffffffffffffffseeeeeeeefffffffffffffffffffffff
83 Note: upper = [63..32], lower = [31..0]
86 /* Extract packed single values: */
87 #define FP_PS_upper(v) (((v) >> 32) & (unsigned)0xFFFFFFFF)
88 #define FP_PS_lower(v) ((v) & (unsigned)0xFFFFFFFF)
89 #define FP_PS_cat(u,l) (((unsigned64)((u) & (unsigned)0xFFFFFFFF) << 32) \
90 | (unsigned64)((l) & 0xFFFFFFFF))
92 /* Explicit QNaN values. */
93 #define FPQNaN_SINGLE (0x7FBFFFFF)
94 #define FPQNaN_WORD (0x7FFFFFFF)
95 #define FPQNaN_DOUBLE (UNSIGNED64 (0x7FF7FFFFFFFFFFFF))
96 #define FPQNaN_LONG (UNSIGNED64 (0x7FFFFFFFFFFFFFFF))
97 #define FPQNaN_PS (FP_PS_cat (FPQNaN_SINGLE, FPQNaN_SINGLE))
99 static const char *fpu_format_name (FP_formats fmt);
101 static const char *fpu_rounding_mode_name (int rm);
105 value_fpr (sim_cpu *cpu,
113 /* Treat unused register values, as fixed-point 64bit values. */
114 if (fmt == fmt_unknown)
117 /* If request to read data as "unknown", then use the current
119 fmt = FPR_STATE[fpr];
125 /* For values not yet accessed, set to the desired format. */
126 if (fmt < fmt_uninterpreted)
128 if (FPR_STATE[fpr] == fmt_uninterpreted)
130 FPR_STATE[fpr] = fmt;
132 printf ("DBG: Register %d was fmt_uninterpreted. Now %s\n", fpr,
133 fpu_format_name (fmt));
136 else if (fmt != FPR_STATE[fpr])
138 sim_io_eprintf (SD, "FPR %d (format %s) being accessed with format %s - setting to unknown (PC = 0x%s)\n",
139 fpr, fpu_format_name (FPR_STATE[fpr]),
140 fpu_format_name (fmt), pr_addr (cia));
141 FPR_STATE[fpr] = fmt_unknown;
145 if (FPR_STATE[fpr] == fmt_unknown)
147 /* Set QNaN value: */
150 case fmt_single: value = FPQNaN_SINGLE; break;
151 case fmt_double: value = FPQNaN_DOUBLE; break;
152 case fmt_word: value = FPQNaN_WORD; break;
153 case fmt_long: value = FPQNaN_LONG; break;
154 case fmt_ps: value = FPQNaN_PS; break;
155 default: err = -1; break;
158 else if (SizeFGR () == 64)
162 case fmt_uninterpreted_32:
165 value = (FGR[fpr] & 0xFFFFFFFF);
168 case fmt_uninterpreted_64:
169 case fmt_uninterpreted:
185 case fmt_uninterpreted_32:
188 value = (FGR[fpr] & 0xFFFFFFFF);
191 case fmt_uninterpreted_64:
192 case fmt_uninterpreted:
197 /* Even register numbers only. */
199 printf ("DBG: ValueFPR: FGR[%d] = %s, FGR[%d] = %s\n",
200 fpr + 1, pr_uword64 ((uword64) FGR[fpr+1]),
201 fpr, pr_uword64 ((uword64) FGR[fpr]));
203 value = ((((uword64) FGR[fpr+1]) << 32)
204 | (FGR[fpr] & 0xFFFFFFFF));
208 SignalException (ReservedInstruction, 0);
213 SignalException (ReservedInstruction, 0);
223 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR ()");
226 printf ("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d\n",
227 fpr, fpu_format_name (fmt), pr_uword64 (value), pr_addr (cia),
235 store_fpr (sim_cpu *cpu,
244 printf ("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d, \n",
245 fpr, fpu_format_name (fmt), pr_uword64 (value), pr_addr (cia),
249 if (SizeFGR () == 64)
253 case fmt_uninterpreted_32:
254 fmt = fmt_uninterpreted;
257 if (STATE_VERBOSE_P (SD))
259 "Warning: PC 0x%s: interp.c store_fpr DEADCODE\n",
261 FGR[fpr] = (((uword64) 0xDEADC0DE << 32) | (value & 0xFFFFFFFF));
262 FPR_STATE[fpr] = fmt;
265 case fmt_uninterpreted_64:
266 fmt = fmt_uninterpreted;
267 case fmt_uninterpreted:
272 FPR_STATE[fpr] = fmt;
276 FPR_STATE[fpr] = fmt_unknown;
285 case fmt_uninterpreted_32:
286 fmt = fmt_uninterpreted;
289 FGR[fpr] = (value & 0xFFFFFFFF);
290 FPR_STATE[fpr] = fmt;
293 case fmt_uninterpreted_64:
294 fmt = fmt_uninterpreted;
295 case fmt_uninterpreted:
300 /* Even register numbers only. */
301 FGR[fpr+1] = (value >> 32);
302 FGR[fpr] = (value & 0xFFFFFFFF);
303 FPR_STATE[fpr + 1] = fmt;
304 FPR_STATE[fpr] = fmt;
308 FPR_STATE[fpr] = fmt_unknown;
309 FPR_STATE[fpr ^ 1] = fmt_unknown;
310 SignalException (ReservedInstruction, 0);
315 FPR_STATE[fpr] = fmt_unknown;
316 SignalException (ReservedInstruction, 0);
320 FPR_STATE[fpr] = fmt_unknown;
327 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR ()");
330 printf ("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",
331 fpr, pr_uword64 (FGR[fpr]), fpu_format_name (fmt));
338 /* CP1 control/status register access functions. */
341 test_fcsr (sim_cpu *cpu,
346 cause = (FCSR & fcsr_CAUSE_mask) >> fcsr_CAUSE_shift;
347 if ((cause & ((FCSR & fcsr_ENABLES_mask) >> fcsr_ENABLES_shift)) != 0
348 || (cause & (1 << UO)))
350 SignalExceptionFPE();
355 value_fcr(sim_cpu *cpu,
359 unsigned32 value = 0;
363 case 0: /* FP Implementation and Revision Register. */
366 case 25: /* FP Condition Codes Register (derived from FCSR). */
367 value = (FCR31 & fcsr_FCC_mask) >> fcsr_FCC_shift;
368 value = (value & 0x1) | (value >> 1); /* Close FCC gap. */
370 case 26: /* FP Exceptions Register (derived from FCSR). */
371 value = FCR31 & (fcsr_CAUSE_mask | fcsr_FLAGS_mask);
373 case 28: /* FP Enables Register (derived from FCSR). */
374 value = FCR31 & (fcsr_ENABLES_mask | fcsr_RM_mask);
375 if ((FCR31 & fcsr_FS) != 0)
378 case 31: /* FP Control/Status Register (FCSR). */
379 value = FCR31 & ~fcsr_ZERO_mask;
383 return (EXTEND32 (value));
387 store_fcr(sim_cpu *cpu,
397 case 25: /* FP Condition Codes Register (stored into FCSR). */
398 v = (v << 1) | (v & 0x1); /* Adjust for FCC gap. */
399 FCR31 &= ~fcsr_FCC_mask;
400 FCR31 |= ((v << fcsr_FCC_shift) & fcsr_FCC_mask);
402 case 26: /* FP Exceptions Register (stored into FCSR). */
403 FCR31 &= ~(fcsr_CAUSE_mask | fcsr_FLAGS_mask);
404 FCR31 |= (v & (fcsr_CAUSE_mask | fcsr_FLAGS_mask));
407 case 28: /* FP Enables Register (stored into FCSR). */
408 if ((v & fenr_FS) != 0)
412 FCR31 &= (fcsr_FCC_mask | fcsr_CAUSE_mask | fcsr_FLAGS_mask);
413 FCR31 |= (v & (fcsr_FS | fcsr_ENABLES_mask | fcsr_RM_mask));
416 case 31: /* FP Control/Status Register (FCSR). */
417 FCR31 = v & ~fcsr_ZERO_mask;
424 update_fcsr (sim_cpu *cpu,
426 sim_fpu_status status)
428 FCSR &= ~fcsr_CAUSE_mask;
432 unsigned int cause = 0;
434 /* map between sim_fpu codes and MIPS FCSR */
435 if (status & (sim_fpu_status_invalid_snan
436 | sim_fpu_status_invalid_isi
437 | sim_fpu_status_invalid_idi
438 | sim_fpu_status_invalid_zdz
439 | sim_fpu_status_invalid_imz
440 | sim_fpu_status_invalid_cmp
441 | sim_fpu_status_invalid_sqrt
442 | sim_fpu_status_invalid_cvi))
444 if (status & sim_fpu_status_invalid_div0)
446 if (status & sim_fpu_status_overflow)
448 if (status & sim_fpu_status_underflow)
450 if (status & sim_fpu_status_inexact)
453 /* Implicit clearing of other bits by unimplemented done by callers. */
454 if (status & sim_fpu_status_unimplemented)
458 FCSR |= (cause << fcsr_CAUSE_shift);
459 test_fcsr (cpu, cia);
460 FCSR |= ((cause & ~(1 << UO)) << fcsr_FLAGS_shift);
466 rounding_mode(int rm)
473 /* Round result to nearest representable value. When two
474 representable values are equally near, round to the value
475 that has a least significant bit of zero (i.e. is even). */
476 round = sim_fpu_round_near;
479 /* Round result to the value closest to, and not greater in
480 magnitude than, the result. */
481 round = sim_fpu_round_zero;
484 /* Round result to the value closest to, and not less than,
486 round = sim_fpu_round_up;
489 /* Round result to the value closest to, and not greater than,
491 round = sim_fpu_round_down;
495 fprintf (stderr, "Bad switch\n");
501 /* When the FS bit is set, MIPS processors return zero for
502 denormalized results and optionally replace denormalized inputs
503 with zero. When FS is clear, some implementation trap on input
504 and/or output, while other perform the operation in hardware. */
505 static sim_fpu_denorm
506 denorm_mode(sim_cpu *cpu)
508 sim_fpu_denorm denorm;
510 /* XXX: FIXME: Eventually should be CPU model dependent. */
512 denorm = sim_fpu_denorm_zero;
519 /* Comparison operations. */
521 static sim_fpu_status
522 fp_test(unsigned64 op1,
531 sim_fpu_status status = 0;
532 int less, equal, unordered;
534 /* The format type has already been checked: */
539 sim_fpu_32to (&wop1, op1);
540 sim_fpu_32to (&wop2, op2);
545 sim_fpu_64to (&wop1, op1);
546 sim_fpu_64to (&wop2, op2);
550 fprintf (stderr, "Bad switch\n");
554 if (sim_fpu_is_nan (&wop1) || sim_fpu_is_nan (&wop2))
556 if ((cond & (1 << 3)) ||
557 sim_fpu_is_snan (&wop1) || sim_fpu_is_snan (&wop2))
558 status = sim_fpu_status_invalid_snan;
567 status |= sim_fpu_abs (&wop1, &wop1);
568 status |= sim_fpu_abs (&wop2, &wop2);
570 equal = sim_fpu_is_eq (&wop1, &wop2);
571 less = !equal && sim_fpu_is_lt (&wop1, &wop2);
574 *condition = (((cond & (1 << 2)) && less)
575 || ((cond & (1 << 1)) && equal)
576 || ((cond & (1 << 0)) && unordered));
590 sim_fpu_status status = 0;
592 /* The format type should already have been checked. The FCSR is
593 updated before the condition codes so that any exceptions will
594 be signalled before the condition codes are changed. */
601 status = fp_test(op1, op2, fmt, abs, cond, &result);
602 update_fcsr (cpu, cia, status);
608 int result0, result1;
609 status = fp_test(FP_PS_lower (op1), FP_PS_lower (op2), fmt_single,
610 abs, cond, &result0);
611 status |= fp_test(FP_PS_upper (op1), FP_PS_upper (op2), fmt_single,
612 abs, cond, &result1);
613 update_fcsr (cpu, cia, status);
614 SETFCC (cc, result0);
615 SETFCC (cc+1, result1);
619 sim_io_eprintf (SD, "Bad switch\n");
625 /* Basic arithmetic operations. */
628 fp_unary(sim_cpu *cpu,
630 int (*sim_fpu_op)(sim_fpu *, const sim_fpu *),
636 sim_fpu_round round = rounding_mode (GETRM());
637 sim_fpu_denorm denorm = denorm_mode (cpu);
638 sim_fpu_status status = 0;
639 unsigned64 result = 0;
641 /* The format type has already been checked: */
647 sim_fpu_32to (&wop, op);
648 status |= (*sim_fpu_op) (&ans, &wop);
649 status |= sim_fpu_round_32 (&ans, round, denorm);
650 sim_fpu_to32 (&res, &ans);
657 sim_fpu_64to (&wop, op);
658 status |= (*sim_fpu_op) (&ans, &wop);
659 status |= sim_fpu_round_64 (&ans, round, denorm);
660 sim_fpu_to64 (&res, &ans);
666 int status_u = 0, status_l = 0;
667 unsigned32 res_u, res_l;
668 sim_fpu_32to (&wop, FP_PS_upper(op));
669 status_u |= (*sim_fpu_op) (&ans, &wop);
670 sim_fpu_to32 (&res_u, &ans);
671 sim_fpu_32to (&wop, FP_PS_lower(op));
672 status_l |= (*sim_fpu_op) (&ans, &wop);
673 sim_fpu_to32 (&res_l, &ans);
674 result = FP_PS_cat(res_u, res_l);
675 status = status_u | status_l;
679 sim_io_eprintf (SD, "Bad switch\n");
683 update_fcsr (cpu, cia, status);
688 fp_binary(sim_cpu *cpu,
690 int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
698 sim_fpu_round round = rounding_mode (GETRM());
699 sim_fpu_denorm denorm = denorm_mode (cpu);
700 sim_fpu_status status = 0;
701 unsigned64 result = 0;
703 /* The format type has already been checked: */
709 sim_fpu_32to (&wop1, op1);
710 sim_fpu_32to (&wop2, op2);
711 status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
712 status |= sim_fpu_round_32 (&ans, round, denorm);
713 sim_fpu_to32 (&res, &ans);
720 sim_fpu_64to (&wop1, op1);
721 sim_fpu_64to (&wop2, op2);
722 status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
723 status |= sim_fpu_round_64 (&ans, round, denorm);
724 sim_fpu_to64 (&res, &ans);
730 int status_u = 0, status_l = 0;
731 unsigned32 res_u, res_l;
732 sim_fpu_32to (&wop1, FP_PS_upper(op1));
733 sim_fpu_32to (&wop2, FP_PS_upper(op2));
734 status_u |= (*sim_fpu_op) (&ans, &wop1, &wop2);
735 sim_fpu_to32 (&res_u, &ans);
736 sim_fpu_32to (&wop1, FP_PS_lower(op1));
737 sim_fpu_32to (&wop2, FP_PS_lower(op2));
738 status_l |= (*sim_fpu_op) (&ans, &wop1, &wop2);
739 sim_fpu_to32 (&res_l, &ans);
740 result = FP_PS_cat(res_u, res_l);
741 status = status_u | status_l;
745 sim_io_eprintf (SD, "Bad switch\n");
749 update_fcsr (cpu, cia, status);
753 /* Common MAC code for single operands (.s or .d), defers setting FCSR. */
754 static sim_fpu_status
755 inner_mac(int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
763 sim_fpu_denorm denorm,
769 sim_fpu_status status = 0;
770 sim_fpu_status op_status;
778 sim_fpu_32to (&wop1, op1);
779 sim_fpu_32to (&wop2, op2);
780 status |= sim_fpu_mul (&ans, &wop1, &wop2);
781 if (scale != 0 && sim_fpu_is_number (&ans)) /* number or denorm */
782 ans.normal_exp += scale;
783 status |= sim_fpu_round_32 (&ans, round, denorm);
786 sim_fpu_32to (&wop2, op3);
787 op_status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
788 op_status |= sim_fpu_round_32 (&ans, round, denorm);
793 op_status = sim_fpu_neg (&ans, &wop1);
794 op_status |= sim_fpu_round_32 (&ans, round, denorm);
797 sim_fpu_to32 (&res, &ans);
804 sim_fpu_64to (&wop1, op1);
805 sim_fpu_64to (&wop2, op2);
806 status |= sim_fpu_mul (&ans, &wop1, &wop2);
807 if (scale != 0 && sim_fpu_is_number (&ans)) /* number or denorm */
808 ans.normal_exp += scale;
809 status |= sim_fpu_round_64 (&ans, round, denorm);
812 sim_fpu_64to (&wop2, op3);
813 op_status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
814 op_status |= sim_fpu_round_64 (&ans, round, denorm);
819 op_status = sim_fpu_neg (&ans, &wop1);
820 op_status |= sim_fpu_round_64 (&ans, round, denorm);
823 sim_fpu_to64 (&res, &ans);
828 fprintf (stderr, "Bad switch\n");
835 /* Common implementation of madd, nmadd, msub, nmsub that does
836 intermediate rounding per spec. Also used for recip2 and rsqrt2,
837 which are transformed into equivalent nmsub operations. The scale
838 argument is an adjustment to the exponent of the intermediate
839 product op1*op2. It is currently non-zero for rsqrt2 (-1), which
840 requires an effective division by 2. */
844 int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
852 sim_fpu_round round = rounding_mode (GETRM());
853 sim_fpu_denorm denorm = denorm_mode (cpu);
854 sim_fpu_status status = 0;
855 unsigned64 result = 0;
857 /* The format type has already been checked: */
862 status = inner_mac(sim_fpu_op, op1, op2, op3, scale,
863 negate, fmt, round, denorm, &result);
867 int status_u, status_l;
868 unsigned64 result_u, result_l;
869 status_u = inner_mac(sim_fpu_op, FP_PS_upper(op1), FP_PS_upper(op2),
870 FP_PS_upper(op3), scale, negate, fmt_single,
871 round, denorm, &result_u);
872 status_l = inner_mac(sim_fpu_op, FP_PS_lower(op1), FP_PS_lower(op2),
873 FP_PS_lower(op3), scale, negate, fmt_single,
874 round, denorm, &result_l);
875 result = FP_PS_cat(result_u, result_l);
876 status = status_u | status_l;
880 sim_io_eprintf (SD, "Bad switch\n");
884 update_fcsr (cpu, cia, status);
888 /* Common rsqrt code for single operands (.s or .d), intermediate rounding. */
889 static sim_fpu_status
890 inner_rsqrt(unsigned64 op1,
893 sim_fpu_denorm denorm,
898 sim_fpu_status status = 0;
899 sim_fpu_status op_status;
907 sim_fpu_32to (&wop1, op1);
908 status |= sim_fpu_sqrt (&ans, &wop1);
909 status |= sim_fpu_round_32 (&ans, status, round);
911 op_status = sim_fpu_inv (&ans, &wop1);
912 op_status |= sim_fpu_round_32 (&ans, round, denorm);
913 sim_fpu_to32 (&res, &ans);
921 sim_fpu_64to (&wop1, op1);
922 status |= sim_fpu_sqrt (&ans, &wop1);
923 status |= sim_fpu_round_64 (&ans, round, denorm);
925 op_status = sim_fpu_inv (&ans, &wop1);
926 op_status |= sim_fpu_round_64 (&ans, round, denorm);
927 sim_fpu_to64 (&res, &ans);
933 fprintf (stderr, "Bad switch\n");
941 fp_inv_sqrt(sim_cpu *cpu,
946 sim_fpu_round round = rounding_mode (GETRM());
947 sim_fpu_round denorm = denorm_mode (cpu);
948 sim_fpu_status status = 0;
949 unsigned64 result = 0;
951 /* The format type has already been checked: */
956 status = inner_rsqrt (op1, fmt, round, denorm, &result);
960 int status_u, status_l;
961 unsigned64 result_u, result_l;
962 status_u = inner_rsqrt (FP_PS_upper(op1), fmt_single, round, denorm,
964 status_l = inner_rsqrt (FP_PS_lower(op1), fmt_single, round, denorm,
966 result = FP_PS_cat(result_u, result_l);
967 status = status_u | status_l;
971 sim_io_eprintf (SD, "Bad switch\n");
975 update_fcsr (cpu, cia, status);
986 return fp_unary(cpu, cia, &sim_fpu_abs, op, fmt);
995 return fp_unary(cpu, cia, &sim_fpu_neg, op, fmt);
1005 return fp_binary(cpu, cia, &sim_fpu_add, op1, op2, fmt);
1009 fp_sub(sim_cpu *cpu,
1015 return fp_binary(cpu, cia, &sim_fpu_sub, op1, op2, fmt);
1019 fp_mul(sim_cpu *cpu,
1025 return fp_binary(cpu, cia, &sim_fpu_mul, op1, op2, fmt);
1029 fp_div(sim_cpu *cpu,
1035 return fp_binary(cpu, cia, &sim_fpu_div, op1, op2, fmt);
1039 fp_recip(sim_cpu *cpu,
1044 return fp_unary(cpu, cia, &sim_fpu_inv, op, fmt);
1048 fp_sqrt(sim_cpu *cpu,
1053 return fp_unary(cpu, cia, &sim_fpu_sqrt, op, fmt);
1057 fp_rsqrt(sim_cpu *cpu,
1062 return fp_inv_sqrt(cpu, cia, op, fmt);
1066 fp_madd(sim_cpu *cpu,
1073 return fp_mac(cpu, cia, &sim_fpu_add, op1, op2, op3, 0, 0, fmt);
1077 fp_msub(sim_cpu *cpu,
1084 return fp_mac(cpu, cia, &sim_fpu_sub, op1, op2, op3, 0, 0, fmt);
1088 fp_nmadd(sim_cpu *cpu,
1095 return fp_mac(cpu, cia, &sim_fpu_add, op1, op2, op3, 0, 1, fmt);
1099 fp_nmsub(sim_cpu *cpu,
1106 return fp_mac(cpu, cia, &sim_fpu_sub, op1, op2, op3, 0, 1, fmt);
1110 /* MIPS-3D ASE operations. */
1112 /* Variant of fp_binary for *r.ps MIPS-3D operations. */
1114 fp_binary_r(sim_cpu *cpu,
1116 int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
1123 sim_fpu_round round = rounding_mode (GETRM ());
1124 sim_fpu_denorm denorm = denorm_mode (cpu);
1125 sim_fpu_status status_u, status_l;
1127 unsigned32 res_u, res_l;
1129 /* The format must be fmt_ps. */
1131 sim_fpu_32to (&wop1, FP_PS_upper (op1));
1132 sim_fpu_32to (&wop2, FP_PS_lower (op1));
1133 status_u |= (*sim_fpu_op) (&ans, &wop1, &wop2);
1134 status_u |= sim_fpu_round_32 (&ans, round, denorm);
1135 sim_fpu_to32 (&res_u, &ans);
1137 sim_fpu_32to (&wop1, FP_PS_upper (op2));
1138 sim_fpu_32to (&wop2, FP_PS_lower (op2));
1139 status_l |= (*sim_fpu_op) (&ans, &wop1, &wop2);
1140 status_l |= sim_fpu_round_32 (&ans, round, denorm);
1141 sim_fpu_to32 (&res_l, &ans);
1142 result = FP_PS_cat (res_u, res_l);
1144 update_fcsr (cpu, cia, status_u | status_l);
1149 fp_add_r(sim_cpu *cpu,
1155 return fp_binary_r (cpu, cia, &sim_fpu_add, op1, op2);
1159 fp_mul_r(sim_cpu *cpu,
1165 return fp_binary_r (cpu, cia, &sim_fpu_mul, op1, op2);
1168 #define NR_FRAC_GUARD (60)
1169 #define IMPLICIT_1 LSBIT64 (NR_FRAC_GUARD)
1172 fpu_inv1(sim_fpu *f, const sim_fpu *l)
1174 static const sim_fpu sim_fpu_one = {
1175 sim_fpu_class_number, 0, IMPLICIT_1, 0
1180 if (sim_fpu_is_zero (l))
1184 return sim_fpu_status_invalid_div0;
1186 if (sim_fpu_is_infinity (l))
1192 status |= sim_fpu_div (f, &sim_fpu_one, l);
1197 fpu_inv1_32(sim_fpu *f, const sim_fpu *l)
1199 if (sim_fpu_is_zero (l))
1203 return sim_fpu_status_invalid_div0;
1205 return fpu_inv1 (f, l);
1209 fpu_inv1_64(sim_fpu *f, const sim_fpu *l)
1211 if (sim_fpu_is_zero (l))
1215 return sim_fpu_status_invalid_div0;
1217 return fpu_inv1 (f, l);
1221 fp_recip1(sim_cpu *cpu,
1230 return fp_unary (cpu, cia, &fpu_inv1_32, op, fmt);
1232 return fp_unary (cpu, cia, &fpu_inv1_64, op, fmt);
1238 fp_recip2(sim_cpu *cpu,
1244 static const unsigned64 one_single = UNSIGNED64 (0x3F800000);
1245 static const unsigned64 one_double = UNSIGNED64 (0x3FF0000000000000);
1246 static const unsigned64 one_ps = (UNSIGNED64 (0x3F800000) << 32 | UNSIGNED64 (0x3F800000));
1249 /* Implemented as nmsub fd, 1, fs, ft. */
1252 case fmt_single: one = one_single; break;
1253 case fmt_double: one = one_double; break;
1254 case fmt_ps: one = one_ps; break;
1255 default: one = 0; abort ();
1257 return fp_mac (cpu, cia, &sim_fpu_sub, op1, op2, one, 0, 1, fmt);
1261 fpu_inv_sqrt1(sim_fpu *f, const sim_fpu *l)
1263 static const sim_fpu sim_fpu_one = {
1264 sim_fpu_class_number, 0, IMPLICIT_1, 0
1269 if (sim_fpu_is_zero (l))
1273 return sim_fpu_status_invalid_div0;
1275 if (sim_fpu_is_infinity (l))
1279 f->class = sim_fpu_class_zero;
1285 status = sim_fpu_status_invalid_sqrt;
1289 status |= sim_fpu_sqrt (&t, l);
1290 status |= sim_fpu_div (f, &sim_fpu_one, &t);
1295 fpu_inv_sqrt1_32(sim_fpu *f, const sim_fpu *l)
1297 if (sim_fpu_is_zero (l))
1301 return sim_fpu_status_invalid_div0;
1303 return fpu_inv_sqrt1 (f, l);
1307 fpu_inv_sqrt1_64(sim_fpu *f, const sim_fpu *l)
1309 if (sim_fpu_is_zero (l))
1313 return sim_fpu_status_invalid_div0;
1315 return fpu_inv_sqrt1 (f, l);
1319 fp_rsqrt1(sim_cpu *cpu,
1328 return fp_unary (cpu, cia, &fpu_inv_sqrt1_32, op, fmt);
1330 return fp_unary (cpu, cia, &fpu_inv_sqrt1_64, op, fmt);
1336 fp_rsqrt2(sim_cpu *cpu,
1342 static const unsigned64 half_single = UNSIGNED64 (0x3F000000);
1343 static const unsigned64 half_double = UNSIGNED64 (0x3FE0000000000000);
1344 static const unsigned64 half_ps = (UNSIGNED64 (0x3F000000) << 32 | UNSIGNED64 (0x3F000000));
1347 /* Implemented as (nmsub fd, 0.5, fs, ft)/2, where the divide is
1348 done by scaling the exponent during multiply. */
1351 case fmt_single: half = half_single; break;
1352 case fmt_double: half = half_double; break;
1353 case fmt_ps: half = half_ps; break;
1354 default: half = 0; abort ();
1356 return fp_mac (cpu, cia, &sim_fpu_sub, op1, op2, half, -1, 1, fmt);
1360 /* Conversion operations. */
1363 convert (sim_cpu *cpu,
1371 sim_fpu_round round = rounding_mode (rm);
1372 sim_fpu_denorm denorm = denorm_mode (cpu);
1373 unsigned32 result32;
1374 unsigned64 result64;
1375 sim_fpu_status status = 0;
1377 /* Convert the input to sim_fpu internal format */
1381 sim_fpu_64to (&wop, op);
1384 sim_fpu_32to (&wop, op);
1387 status = sim_fpu_i32to (&wop, op, round);
1390 status = sim_fpu_i64to (&wop, op, round);
1393 sim_io_eprintf (SD, "Bad switch\n");
1397 /* Convert sim_fpu format into the output */
1398 /* The value WOP is converted to the destination format, rounding
1399 using mode RM. When the destination is a fixed-point format, then
1400 a source value of Infinity, NaN or one which would round to an
1401 integer outside the fixed point range then an IEEE Invalid Operation
1402 condition is raised. Not used if destination format is PS. */
1406 status |= sim_fpu_round_32 (&wop, round, denorm);
1407 /* For a NaN, normalize mantissa bits (cvt.s.d can't preserve them) */
1408 if (sim_fpu_is_qnan (&wop))
1410 sim_fpu_to32 (&result32, &wop);
1411 result64 = result32;
1414 status |= sim_fpu_round_64 (&wop, round, denorm);
1415 /* For a NaN, normalize mantissa bits (make cvt.d.s consistent) */
1416 if (sim_fpu_is_qnan (&wop))
1418 sim_fpu_to64 (&result64, &wop);
1421 status |= sim_fpu_to32i (&result32, &wop, round);
1422 result64 = result32;
1425 status |= sim_fpu_to64i (&result64, &wop, round);
1429 sim_io_eprintf (SD, "Bad switch\n");
1433 update_fcsr (cpu, cia, status);
1438 ps_lower(sim_cpu *cpu,
1442 return FP_PS_lower (op);
1446 ps_upper(sim_cpu *cpu,
1450 return FP_PS_upper(op);
1454 pack_ps(sim_cpu *cpu,
1460 unsigned64 result = 0;
1462 /* The registers must specify FPRs valid for operands of type
1463 "fmt". If they are not valid, the result is undefined. */
1465 /* The format type should already have been checked: */
1471 unsigned32 res_u, res_l;
1472 sim_fpu_32to (&wop, op1);
1473 sim_fpu_to32 (&res_u, &wop);
1474 sim_fpu_32to (&wop, op2);
1475 sim_fpu_to32 (&res_l, &wop);
1476 result = FP_PS_cat(res_u, res_l);
1480 sim_io_eprintf (SD, "Bad switch\n");
1488 convert_ps (sim_cpu *cpu,
1495 sim_fpu wop_u, wop_l;
1496 sim_fpu_round round = rounding_mode (rm);
1497 sim_fpu_denorm denorm = denorm_mode (cpu);
1498 unsigned32 res_u, res_l;
1500 sim_fpu_status status_u = 0, status_l = 0;
1502 /* As convert, but used only for paired values (formats PS, PW) */
1504 /* Convert the input to sim_fpu internal format */
1507 case fmt_word: /* fmt_pw */
1508 sim_fpu_i32to (&wop_u, (op >> 32) & (unsigned)0xFFFFFFFF, round);
1509 sim_fpu_i32to (&wop_l, op & (unsigned)0xFFFFFFFF, round);
1512 sim_fpu_32to (&wop_u, FP_PS_upper(op));
1513 sim_fpu_32to (&wop_l, FP_PS_lower(op));
1516 sim_io_eprintf (SD, "Bad switch\n");
1520 /* Convert sim_fpu format into the output */
1523 case fmt_word: /* fmt_pw */
1524 status_u |= sim_fpu_to32i (&res_u, &wop_u, round);
1525 status_l |= sim_fpu_to32i (&res_l, &wop_l, round);
1526 result = (((unsigned64)res_u) << 32) | (unsigned64)res_l;
1529 status_u |= sim_fpu_round_32 (&wop_u, 0, round);
1530 status_l |= sim_fpu_round_32 (&wop_l, 0, round);
1531 sim_fpu_to32 (&res_u, &wop_u);
1532 sim_fpu_to32 (&res_l, &wop_l);
1533 result = FP_PS_cat(res_u, res_l);
1537 sim_io_eprintf (SD, "Bad switch\n");
1541 update_fcsr (cpu, cia, status_u | status_l);
1546 fpu_format_name (FP_formats fmt)
1562 case fmt_uninterpreted:
1563 return "<uninterpreted>";
1564 case fmt_uninterpreted_32:
1565 return "<uninterpreted_32>";
1566 case fmt_uninterpreted_64:
1567 return "<uninterpreted_64>";
1569 return "<format error>";
1575 fpu_rounding_mode_name (int rm)
1588 return "<rounding mode error>";