2 /* MIPS Simulator FPU (CoProcessor 1) support.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 Originally created by Cygnus Solutions. Extensive modifications,
5 including paired-single operation support and MIPS-3D support
6 contributed by Ed Satterthwaite and Chris Demetriou, of Broadcom
9 This file is part of GDB, the GNU debugger.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 /* XXX: The following notice should be removed as soon as is practical: */
25 /* Floating Point Support for gdb MIPS simulators
27 This file is part of the MIPS sim
29 THIS SOFTWARE IS NOT COPYRIGHTED
32 Cygnus offers the following for use in the public domain. Cygnus
33 makes no warranty with regard to the software or it's performance
34 and the user accepts the software "AS IS" with all faults.
36 CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
37 THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
40 (Originally, this code was in interp.c)
43 /* This must come before any other includes. */
50 /* Within cp1.c we refer to sim_cpu directly. */
52 #define SD CPU_STATE(cpu)
54 /*-- FPU support routines ---------------------------------------------------*/
56 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
57 formats conform to ANSI/IEEE Std 754-1985.
59 SINGLE precision floating:
60 seeeeeeeefffffffffffffffffffffff
65 SINGLE precision fixed:
66 siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
70 DOUBLE precision floating:
71 seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
76 DOUBLE precision fixed:
77 siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
81 PAIRED SINGLE precision floating:
82 seeeeeeeefffffffffffffffffffffffseeeeeeeefffffffffffffffffffffff
87 Note: upper = [63..32], lower = [31..0]
90 /* Extract packed single values: */
91 #define FP_PS_upper(v) (((v) >> 32) & (unsigned)0xFFFFFFFF)
92 #define FP_PS_lower(v) ((v) & (unsigned)0xFFFFFFFF)
93 #define FP_PS_cat(u,l) (((uint64_t)((u) & (unsigned)0xFFFFFFFF) << 32) \
94 | (uint64_t)((l) & 0xFFFFFFFF))
96 /* Explicit QNaN values. */
97 #define FPQNaN_SINGLE (0x7FBFFFFF)
98 #define FPQNaN_WORD (0x7FFFFFFF)
99 #define FPQNaN_DOUBLE (UNSIGNED64 (0x7FF7FFFFFFFFFFFF))
100 #define FPQNaN_LONG (UNSIGNED64 (0x7FFFFFFFFFFFFFFF))
101 #define FPQNaN_PS (FP_PS_cat (FPQNaN_SINGLE, FPQNaN_SINGLE))
103 static void update_fcsr (sim_cpu
*, address_word
, sim_fpu_status
);
105 static const char *fpu_format_name (FP_formats fmt
);
107 static const char *fpu_rounding_mode_name (int rm
);
111 value_fpr (sim_cpu
*cpu
,
119 /* Treat unused register values, as fixed-point 64bit values. */
120 if (fmt
== fmt_unknown
)
123 /* If request to read data as "unknown", then use the current
125 fmt
= FPR_STATE
[fpr
];
131 /* For values not yet accessed, set to the desired format. */
132 if (fmt
< fmt_uninterpreted
&& fmt
!= fmt_dc32
)
134 if (FPR_STATE
[fpr
] == fmt_uninterpreted
)
136 FPR_STATE
[fpr
] = fmt
;
138 printf ("DBG: Register %d was fmt_uninterpreted. Now %s\n", fpr
,
139 fpu_format_name (fmt
));
142 else if (fmt
!= FPR_STATE
[fpr
]
143 && !(fmt
== fmt_single
144 && FPR_STATE
[fpr
] == fmt_double
145 && (FGR
[fpr
] == 0 || FGR
[fpr
] == 0xFFFFFFFF)))
147 sim_io_eprintf (SD
, "FPR %d (format %s) being accessed with format %s - setting to unknown (PC = 0x%s)\n",
148 fpr
, fpu_format_name (FPR_STATE
[fpr
]),
149 fpu_format_name (fmt
), pr_addr (cia
));
150 FPR_STATE
[fpr
] = fmt_unknown
;
154 if (FPR_STATE
[fpr
] == fmt_unknown
)
156 /* Set QNaN value: */
159 case fmt_single
: value
= FPQNaN_SINGLE
; break;
160 case fmt_double
: value
= FPQNaN_DOUBLE
; break;
161 case fmt_word
: value
= FPQNaN_WORD
; break;
162 case fmt_long
: value
= FPQNaN_LONG
; break;
163 case fmt_ps
: value
= FPQNaN_PS
; break;
164 default: err
= -1; break;
167 else if (SizeFGR () == 64)
171 case fmt_uninterpreted_32
:
175 value
= (FGR
[fpr
] & 0xFFFFFFFF);
178 case fmt_uninterpreted_64
:
179 case fmt_uninterpreted
:
195 case fmt_uninterpreted_32
:
198 value
= (FGR
[fpr
] & 0xFFFFFFFF);
201 case fmt_uninterpreted_64
:
202 case fmt_uninterpreted
:
207 /* Even register numbers only. */
209 printf ("DBG: ValueFPR: FGR[%d] = %s, FGR[%d] = %s\n",
210 fpr
+ 1, pr_uword64 ((uword64
) FGR
[fpr
+1]),
211 fpr
, pr_uword64 ((uword64
) FGR
[fpr
]));
213 value
= ((((uword64
) FGR
[fpr
+1]) << 32)
214 | (FGR
[fpr
] & 0xFFFFFFFF));
218 SignalException (ReservedInstruction
, 0);
223 SignalException (ReservedInstruction
, 0);
233 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR ()");
236 printf ("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d\n",
237 fpr
, fpu_format_name (fmt
), pr_uword64 (value
), pr_addr (cia
),
245 store_fpr (sim_cpu
*cpu
,
254 printf ("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d, \n",
255 fpr
, fpu_format_name (fmt
), pr_uword64 (value
), pr_addr (cia
),
259 if (SizeFGR () == 64)
263 case fmt_uninterpreted_32
:
264 fmt
= fmt_uninterpreted
;
265 ATTRIBUTE_FALLTHROUGH
;
268 if (STATE_VERBOSE_P (SD
))
270 "Warning: PC 0x%s: interp.c store_fpr DEADCODE\n",
272 FGR
[fpr
] = (((uword64
) 0xDEADC0DE << 32) | (value
& 0xFFFFFFFF));
273 FPR_STATE
[fpr
] = fmt
;
276 case fmt_uninterpreted_64
:
277 fmt
= fmt_uninterpreted
;
278 ATTRIBUTE_FALLTHROUGH
;
279 case fmt_uninterpreted
:
284 FPR_STATE
[fpr
] = fmt
;
288 FPR_STATE
[fpr
] = fmt_unknown
;
297 case fmt_uninterpreted_32
:
298 fmt
= fmt_uninterpreted
;
299 ATTRIBUTE_FALLTHROUGH
;
302 FGR
[fpr
] = (value
& 0xFFFFFFFF);
303 FPR_STATE
[fpr
] = fmt
;
306 case fmt_uninterpreted_64
:
307 fmt
= fmt_uninterpreted
;
308 ATTRIBUTE_FALLTHROUGH
;
309 case fmt_uninterpreted
:
314 /* Even register numbers only. */
315 FGR
[fpr
+1] = (value
>> 32);
316 FGR
[fpr
] = (value
& 0xFFFFFFFF);
317 FPR_STATE
[fpr
+ 1] = fmt
;
318 FPR_STATE
[fpr
] = fmt
;
322 FPR_STATE
[fpr
] = fmt_unknown
;
323 FPR_STATE
[fpr
^ 1] = fmt_unknown
;
324 SignalException (ReservedInstruction
, 0);
329 FPR_STATE
[fpr
] = fmt_unknown
;
330 SignalException (ReservedInstruction
, 0);
334 FPR_STATE
[fpr
] = fmt_unknown
;
341 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR ()");
344 printf ("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",
345 fpr
, pr_uword64 (FGR
[fpr
]), fpu_format_name (fmt
));
352 /* CP1 control/status register access functions. */
355 test_fcsr (sim_cpu
*cpu
,
360 cause
= (FCSR
& fcsr_CAUSE_mask
) >> fcsr_CAUSE_shift
;
361 if ((cause
& ((FCSR
& fcsr_ENABLES_mask
) >> fcsr_ENABLES_shift
)) != 0
362 || (cause
& (1 << UO
)))
364 SignalExceptionFPE();
369 value_fcr(sim_cpu
*cpu
,
377 case 0: /* FP Implementation and Revision Register. */
380 case 25: /* FP Condition Codes Register (derived from FCSR). */
381 value
= (FCR31
& fcsr_FCC_mask
) >> fcsr_FCC_shift
;
382 value
= (value
& 0x1) | (value
>> 1); /* Close FCC gap. */
384 case 26: /* FP Exceptions Register (derived from FCSR). */
385 value
= FCR31
& (fcsr_CAUSE_mask
| fcsr_FLAGS_mask
);
387 case 28: /* FP Enables Register (derived from FCSR). */
388 value
= FCR31
& (fcsr_ENABLES_mask
| fcsr_RM_mask
);
389 if ((FCR31
& fcsr_FS
) != 0)
392 case 31: /* FP Control/Status Register (FCSR). */
393 value
= FCR31
& ~fcsr_ZERO_mask
;
397 return (EXTEND32 (value
));
401 store_fcr(sim_cpu
*cpu
,
411 case 25: /* FP Condition Codes Register (stored into FCSR). */
412 v
= (v
<< 1) | (v
& 0x1); /* Adjust for FCC gap. */
413 FCR31
&= ~fcsr_FCC_mask
;
414 FCR31
|= ((v
<< fcsr_FCC_shift
) & fcsr_FCC_mask
);
416 case 26: /* FP Exceptions Register (stored into FCSR). */
417 FCR31
&= ~(fcsr_CAUSE_mask
| fcsr_FLAGS_mask
);
418 FCR31
|= (v
& (fcsr_CAUSE_mask
| fcsr_FLAGS_mask
));
421 case 28: /* FP Enables Register (stored into FCSR). */
422 if ((v
& fenr_FS
) != 0)
426 FCR31
&= (fcsr_FCC_mask
| fcsr_CAUSE_mask
| fcsr_FLAGS_mask
);
427 FCR31
|= (v
& (fcsr_FS
| fcsr_ENABLES_mask
| fcsr_RM_mask
));
430 case 31: /* FP Control/Status Register (FCSR). */
431 FCR31
= v
& ~fcsr_ZERO_mask
;
438 update_fcsr (sim_cpu
*cpu
,
440 sim_fpu_status status
)
442 FCSR
&= ~fcsr_CAUSE_mask
;
446 unsigned int cause
= 0;
448 /* map between sim_fpu codes and MIPS FCSR */
449 if (status
& (sim_fpu_status_invalid_snan
450 | sim_fpu_status_invalid_isi
451 | sim_fpu_status_invalid_idi
452 | sim_fpu_status_invalid_zdz
453 | sim_fpu_status_invalid_imz
454 | sim_fpu_status_invalid_cmp
455 | sim_fpu_status_invalid_sqrt
456 | sim_fpu_status_invalid_cvi
))
458 if (status
& sim_fpu_status_invalid_div0
)
460 if (status
& sim_fpu_status_overflow
)
462 if (status
& sim_fpu_status_underflow
)
464 if (status
& sim_fpu_status_inexact
)
467 /* Implicit clearing of other bits by unimplemented done by callers. */
468 if (status
& sim_fpu_status_unimplemented
)
472 FCSR
|= (cause
<< fcsr_CAUSE_shift
);
473 test_fcsr (cpu
, cia
);
474 FCSR
|= ((cause
& ~(1 << UO
)) << fcsr_FLAGS_shift
);
480 rounding_mode(int rm
)
487 /* Round result to nearest representable value. When two
488 representable values are equally near, round to the value
489 that has a least significant bit of zero (i.e. is even). */
490 round
= sim_fpu_round_near
;
493 /* Round result to the value closest to, and not greater in
494 magnitude than, the result. */
495 round
= sim_fpu_round_zero
;
498 /* Round result to the value closest to, and not less than,
500 round
= sim_fpu_round_up
;
503 /* Round result to the value closest to, and not greater than,
505 round
= sim_fpu_round_down
;
509 fprintf (stderr
, "Bad switch\n");
515 /* When the FS bit is set, MIPS processors return zero for
516 denormalized results and optionally replace denormalized inputs
517 with zero. When FS is clear, some implementation trap on input
518 and/or output, while other perform the operation in hardware. */
519 static sim_fpu_denorm
520 denorm_mode(sim_cpu
*cpu
)
522 sim_fpu_denorm denorm
;
524 /* XXX: FIXME: Eventually should be CPU model dependent. */
526 denorm
= sim_fpu_denorm_zero
;
533 /* Comparison operations. */
535 static sim_fpu_status
536 fp_test(uint64_t op1
,
545 sim_fpu_status status
= 0;
546 int less
, equal
, unordered
;
548 /* The format type has already been checked: */
553 sim_fpu_32to (&wop1
, op1
);
554 sim_fpu_32to (&wop2
, op2
);
559 sim_fpu_64to (&wop1
, op1
);
560 sim_fpu_64to (&wop2
, op2
);
564 fprintf (stderr
, "Bad switch\n");
568 if (sim_fpu_is_nan (&wop1
) || sim_fpu_is_nan (&wop2
))
570 if ((cond
& (1 << 3))
571 || sim_fpu_is_snan (&wop1
) || sim_fpu_is_snan (&wop2
))
572 status
= sim_fpu_status_invalid_snan
;
581 status
|= sim_fpu_abs (&wop1
, &wop1
);
582 status
|= sim_fpu_abs (&wop2
, &wop2
);
584 equal
= sim_fpu_is_eq (&wop1
, &wop2
);
585 less
= !equal
&& sim_fpu_is_lt (&wop1
, &wop2
);
588 *condition
= (((cond
& (1 << 2)) && less
)
589 || ((cond
& (1 << 1)) && equal
)
590 || ((cond
& (1 << 0)) && unordered
));
594 static const int sim_fpu_class_mips_mapping
[] = {
595 FP_R6CLASS_SNAN
, /* SIM_FPU_IS_SNAN = 1, Noisy not-a-number */
596 FP_R6CLASS_QNAN
, /* SIM_FPU_IS_QNAN = 2, Quiet not-a-number */
597 FP_R6CLASS_NEGINF
, /* SIM_FPU_IS_NINF = 3, -infinity */
598 FP_R6CLASS_POSINF
, /* SIM_FPU_IS_PINF = 4, +infinity */
599 FP_R6CLASS_NEGNORM
, /* SIM_FPU_IS_NNUMBER = 5, -num - [-MAX .. -MIN] */
600 FP_R6CLASS_POSNORM
, /* SIM_FPU_IS_PNUMBER = 6, +num - [+MIN .. +MAX] */
601 FP_R6CLASS_NEGSUB
, /* SIM_FPU_IS_NDENORM = 7, -denorm - (MIN .. 0) */
602 FP_R6CLASS_POSSUB
, /* SIM_FPU_IS_PDENORM = 8, +denorm - (0 .. MIN) */
603 FP_R6CLASS_NEGZERO
, /* SIM_FPU_IS_NZERO = 9, -0 */
604 FP_R6CLASS_POSZERO
/* SIM_FPU_IS_PZERO = 10, +0 */
608 fp_classify (sim_cpu
*cpu
,
618 sim_fpu_32to (&wop
, op
);
621 sim_fpu_64to (&wop
, op
);
624 sim_io_error (SD
, "Bad switch\n");
626 return sim_fpu_class_mips_mapping
[sim_fpu_classify (&wop
) - 1];
630 fp_rint (sim_cpu
*cpu
,
636 sim_fpu wop
= {0}, wtemp
= {0}, wmagic
= {0}, wans
= {0};
638 sim_fpu_round round
= rounding_mode (GETRM());
643 sim_fpu_32to (&wop
, op
);
644 sim_fpu_32to (&wmagic
, 0x4b000000);
647 sim_fpu_64to (&wop
, op
);
648 sim_fpu_64to (&wmagic
, 0x4330000000000000);
651 sim_io_error (SD
, "Bad switch\n");
654 if (sim_fpu_is_nan (&wop
) || sim_fpu_is_infinity (&wop
))
656 status
= sim_fpu_status_invalid_cvi
;
657 update_fcsr (cpu
, cia
, status
);
664 if (sim_fpu_is_ge (&wop
, &wmagic
))
668 sim_fpu_add (&wtemp
, &wop
, &wmagic
);
669 sim_fpu_round_32 (&wtemp
, round
, sim_fpu_denorm_default
);
670 sim_fpu_sub (&wans
, &wtemp
, &wmagic
);
672 sim_fpu_to32 ((uint32_t *) ans
, &wans
);
675 if (sim_fpu_is_ge (&wop
, &wmagic
))
679 sim_fpu_add (&wtemp
, &wop
, &wmagic
);
680 sim_fpu_round_64 (&wtemp
, round
, sim_fpu_denorm_default
);
681 sim_fpu_sub (&wans
, &wtemp
, &wmagic
);
683 sim_fpu_to64 (ans
, &wans
);
686 sim_io_error (SD
, "Bad switch\n");
689 if (*ans
!= op
&& status
== 0)
690 status
= sim_fpu_status_inexact
;
692 update_fcsr (cpu
, cia
, status
);
706 sim_fpu_status status
= 0;
708 /* The format type should already have been checked. The FCSR is
709 updated before the condition codes so that any exceptions will
710 be signalled before the condition codes are changed. */
717 status
= fp_test(op1
, op2
, fmt
, abs
, cond
, &result
);
718 update_fcsr (cpu
, cia
, status
);
724 int result0
, result1
;
725 status
= fp_test(FP_PS_lower (op1
), FP_PS_lower (op2
), fmt_single
,
726 abs
, cond
, &result0
);
727 status
|= fp_test(FP_PS_upper (op1
), FP_PS_upper (op2
), fmt_single
,
728 abs
, cond
, &result1
);
729 update_fcsr (cpu
, cia
, status
);
730 SETFCC (cc
, result0
);
731 SETFCC (cc
+1, result1
);
735 sim_io_error (SD
, "Bad switch\n");
740 fp_r6_cmp (sim_cpu
*cpu
,
753 sim_fpu_32to (&wop1
, op1
);
754 sim_fpu_32to (&wop2
, op2
);
757 sim_fpu_64to (&wop1
, op1
);
758 sim_fpu_64to (&wop2
, op2
);
761 sim_io_error (SD
, "Bad switch\n");
770 result
= sim_fpu_is_un (&wop1
, &wop2
);
773 result
= sim_fpu_is_or (&wop1
, &wop2
);
776 result
= sim_fpu_is_eq (&wop1
, &wop2
);
779 result
= sim_fpu_is_ne (&wop1
, &wop2
);
782 result
= sim_fpu_is_lt (&wop1
, &wop2
);
785 result
= sim_fpu_is_le (&wop1
, &wop2
);
788 result
= sim_fpu_is_un (&wop1
, &wop2
) || sim_fpu_is_eq (&wop1
, &wop2
);
791 result
= sim_fpu_is_un (&wop1
, &wop2
) || sim_fpu_is_ne (&wop1
, &wop2
);
794 result
= sim_fpu_is_un (&wop1
, &wop2
) || sim_fpu_is_lt (&wop1
, &wop2
);
797 result
= sim_fpu_is_un (&wop1
, &wop2
) || sim_fpu_is_le (&wop1
, &wop2
);
800 update_fcsr (cpu
, cia
, sim_fpu_status_invalid_cmp
);
811 return 0xFFFFFFFFFFFFFFFF;
813 sim_io_error (SD
, "Bad switch\n");
820 /* Basic arithmetic operations. */
823 fp_unary(sim_cpu
*cpu
,
825 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*),
831 sim_fpu_round round
= rounding_mode (GETRM());
832 sim_fpu_denorm denorm
= denorm_mode (cpu
);
833 sim_fpu_status status
= 0;
836 /* The format type has already been checked: */
842 sim_fpu_32to (&wop
, op
);
843 status
|= (*sim_fpu_op
) (&ans
, &wop
);
844 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
845 sim_fpu_to32 (&res
, &ans
);
852 sim_fpu_64to (&wop
, op
);
853 status
|= (*sim_fpu_op
) (&ans
, &wop
);
854 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
855 sim_fpu_to64 (&res
, &ans
);
861 int status_u
= 0, status_l
= 0;
862 uint32_t res_u
, res_l
;
863 sim_fpu_32to (&wop
, FP_PS_upper(op
));
864 status_u
|= (*sim_fpu_op
) (&ans
, &wop
);
865 sim_fpu_to32 (&res_u
, &ans
);
866 sim_fpu_32to (&wop
, FP_PS_lower(op
));
867 status_l
|= (*sim_fpu_op
) (&ans
, &wop
);
868 sim_fpu_to32 (&res_l
, &ans
);
869 result
= FP_PS_cat(res_u
, res_l
);
870 status
= status_u
| status_l
;
874 sim_io_error (SD
, "Bad switch\n");
877 update_fcsr (cpu
, cia
, status
);
882 fp_binary(sim_cpu
*cpu
,
884 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
892 sim_fpu_round round
= rounding_mode (GETRM());
893 sim_fpu_denorm denorm
= denorm_mode (cpu
);
894 sim_fpu_status status
= 0;
897 /* The format type has already been checked: */
903 sim_fpu_32to (&wop1
, op1
);
904 sim_fpu_32to (&wop2
, op2
);
905 status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
906 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
907 sim_fpu_to32 (&res
, &ans
);
914 sim_fpu_64to (&wop1
, op1
);
915 sim_fpu_64to (&wop2
, op2
);
916 status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
917 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
918 sim_fpu_to64 (&res
, &ans
);
924 int status_u
= 0, status_l
= 0;
925 uint32_t res_u
, res_l
;
926 sim_fpu_32to (&wop1
, FP_PS_upper(op1
));
927 sim_fpu_32to (&wop2
, FP_PS_upper(op2
));
928 status_u
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
929 sim_fpu_to32 (&res_u
, &ans
);
930 sim_fpu_32to (&wop1
, FP_PS_lower(op1
));
931 sim_fpu_32to (&wop2
, FP_PS_lower(op2
));
932 status_l
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
933 sim_fpu_to32 (&res_l
, &ans
);
934 result
= FP_PS_cat(res_u
, res_l
);
935 status
= status_u
| status_l
;
939 sim_io_error (SD
, "Bad switch\n");
942 update_fcsr (cpu
, cia
, status
);
946 /* Common MAC code for single operands (.s or .d), defers setting FCSR. */
947 static sim_fpu_status
948 inner_mac(int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
956 sim_fpu_denorm denorm
,
962 sim_fpu_status status
= 0;
963 sim_fpu_status op_status
;
971 sim_fpu_32to (&wop1
, op1
);
972 sim_fpu_32to (&wop2
, op2
);
973 status
|= sim_fpu_mul (&ans
, &wop1
, &wop2
);
974 if (scale
!= 0 && sim_fpu_is_number (&ans
)) /* number or denorm */
975 ans
.normal_exp
+= scale
;
976 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
979 sim_fpu_32to (&wop2
, op3
);
980 op_status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
981 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
986 op_status
= sim_fpu_neg (&ans
, &wop1
);
987 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
990 sim_fpu_to32 (&res
, &ans
);
997 sim_fpu_64to (&wop1
, op1
);
998 sim_fpu_64to (&wop2
, op2
);
999 status
|= sim_fpu_mul (&ans
, &wop1
, &wop2
);
1000 if (scale
!= 0 && sim_fpu_is_number (&ans
)) /* number or denorm */
1001 ans
.normal_exp
+= scale
;
1002 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
1005 sim_fpu_64to (&wop2
, op3
);
1006 op_status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
1007 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
1008 status
|= op_status
;
1012 op_status
= sim_fpu_neg (&ans
, &wop1
);
1013 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
1014 status
|= op_status
;
1016 sim_fpu_to64 (&res
, &ans
);
1021 fprintf (stderr
, "Bad switch\n");
1028 /* Common implementation of madd, nmadd, msub, nmsub that does
1029 intermediate rounding per spec. Also used for recip2 and rsqrt2,
1030 which are transformed into equivalent nmsub operations. The scale
1031 argument is an adjustment to the exponent of the intermediate
1032 product op1*op2. It is currently non-zero for rsqrt2 (-1), which
1033 requires an effective division by 2. */
1035 fp_mac(sim_cpu
*cpu
,
1037 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
1045 sim_fpu_round round
= rounding_mode (GETRM());
1046 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1047 sim_fpu_status status
= 0;
1048 uint64_t result
= 0;
1050 /* The format type has already been checked: */
1055 status
= inner_mac(sim_fpu_op
, op1
, op2
, op3
, scale
,
1056 negate
, fmt
, round
, denorm
, &result
);
1060 int status_u
, status_l
;
1061 uint64_t result_u
, result_l
;
1062 status_u
= inner_mac(sim_fpu_op
, FP_PS_upper(op1
), FP_PS_upper(op2
),
1063 FP_PS_upper(op3
), scale
, negate
, fmt_single
,
1064 round
, denorm
, &result_u
);
1065 status_l
= inner_mac(sim_fpu_op
, FP_PS_lower(op1
), FP_PS_lower(op2
),
1066 FP_PS_lower(op3
), scale
, negate
, fmt_single
,
1067 round
, denorm
, &result_l
);
1068 result
= FP_PS_cat(result_u
, result_l
);
1069 status
= status_u
| status_l
;
1073 sim_io_error (SD
, "Bad switch\n");
1076 update_fcsr (cpu
, cia
, status
);
1080 /* Common FMAC code for .s, .d. Defers setting FCSR to caller. */
1081 static sim_fpu_status
1082 inner_fmac (sim_cpu
*cpu
,
1083 int (*sim_fpu_op
) (sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
1087 sim_fpu_round round
,
1088 sim_fpu_denorm denorm
,
1092 sim_fpu wop1
, wop2
, ans
;
1093 sim_fpu_status status
= 0;
1094 sim_fpu_status op_status
;
1101 sim_fpu_32to (&wop1
, op1
);
1102 sim_fpu_32to (&wop2
, op2
);
1103 status
|= sim_fpu_mul (&ans
, &wop1
, &wop2
);
1106 sim_fpu_32to (&wop2
, op3
);
1107 op_status
|= (*sim_fpu_op
) (&ans
, &wop2
, &wop1
);
1108 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
1109 status
|= op_status
;
1110 sim_fpu_to32 (&t32
, &ans
);
1114 sim_fpu_64to (&wop1
, op1
);
1115 sim_fpu_64to (&wop2
, op2
);
1116 status
|= sim_fpu_mul (&ans
, &wop1
, &wop2
);
1119 sim_fpu_64to (&wop2
, op3
);
1120 op_status
|= (*sim_fpu_op
) (&ans
, &wop2
, &wop1
);
1121 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
1122 status
|= op_status
;
1123 sim_fpu_to64 (&t64
, &ans
);
1126 sim_io_error (SD
, "Bad switch\n");
1134 fp_fmac (sim_cpu
*cpu
,
1136 int (*sim_fpu_op
) (sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
1142 sim_fpu_round round
= rounding_mode (GETRM());
1143 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1144 sim_fpu_status status
= 0;
1145 uint64_t result
= 0;
1151 status
= inner_fmac (cpu
, sim_fpu_op
, op1
, op2
, op3
,
1152 round
, denorm
, fmt
, &result
);
1155 sim_io_error (SD
, "Bad switch\n");
1158 update_fcsr (cpu
, cia
, status
);
1162 /* Common rsqrt code for single operands (.s or .d), intermediate rounding. */
1163 static sim_fpu_status
1164 inner_rsqrt(uint64_t op1
,
1166 sim_fpu_round round
,
1167 sim_fpu_denorm denorm
,
1172 sim_fpu_status status
= 0;
1173 sim_fpu_status op_status
;
1181 sim_fpu_32to (&wop1
, op1
);
1182 status
|= sim_fpu_sqrt (&ans
, &wop1
);
1183 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
1185 op_status
= sim_fpu_inv (&ans
, &wop1
);
1186 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
1187 sim_fpu_to32 (&res
, &ans
);
1189 status
|= op_status
;
1195 sim_fpu_64to (&wop1
, op1
);
1196 status
|= sim_fpu_sqrt (&ans
, &wop1
);
1197 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
1199 op_status
= sim_fpu_inv (&ans
, &wop1
);
1200 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
1201 sim_fpu_to64 (&res
, &ans
);
1203 status
|= op_status
;
1207 fprintf (stderr
, "Bad switch\n");
1215 fp_inv_sqrt(sim_cpu
*cpu
,
1220 sim_fpu_round round
= rounding_mode (GETRM());
1221 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1222 sim_fpu_status status
= 0;
1223 uint64_t result
= 0;
1225 /* The format type has already been checked: */
1230 status
= inner_rsqrt (op1
, fmt
, round
, denorm
, &result
);
1234 int status_u
, status_l
;
1235 uint64_t result_u
, result_l
;
1236 status_u
= inner_rsqrt (FP_PS_upper(op1
), fmt_single
, round
, denorm
,
1238 status_l
= inner_rsqrt (FP_PS_lower(op1
), fmt_single
, round
, denorm
,
1240 result
= FP_PS_cat(result_u
, result_l
);
1241 status
= status_u
| status_l
;
1245 sim_io_error (SD
, "Bad switch\n");
1248 update_fcsr (cpu
, cia
, status
);
1254 fp_abs(sim_cpu
*cpu
,
1259 return fp_unary(cpu
, cia
, &sim_fpu_abs
, op
, fmt
);
1263 fp_neg(sim_cpu
*cpu
,
1268 return fp_unary(cpu
, cia
, &sim_fpu_neg
, op
, fmt
);
1272 fp_add(sim_cpu
*cpu
,
1278 return fp_binary(cpu
, cia
, &sim_fpu_add
, op1
, op2
, fmt
);
1282 fp_sub(sim_cpu
*cpu
,
1288 return fp_binary(cpu
, cia
, &sim_fpu_sub
, op1
, op2
, fmt
);
1292 fp_mul(sim_cpu
*cpu
,
1298 return fp_binary(cpu
, cia
, &sim_fpu_mul
, op1
, op2
, fmt
);
1302 fp_div(sim_cpu
*cpu
,
1308 return fp_binary(cpu
, cia
, &sim_fpu_div
, op1
, op2
, fmt
);
1312 fp_min (sim_cpu
*cpu
,
1318 return fp_binary (cpu
, cia
, &sim_fpu_min
, op1
, op2
, fmt
);
1322 fp_max (sim_cpu
*cpu
,
1328 return fp_binary (cpu
, cia
, &sim_fpu_max
, op1
, op2
, fmt
);
1332 fp_mina (sim_cpu
*cpu
,
1339 sim_fpu wop1
= {0}, wop2
= {0}, waop1
, waop2
, wans
;
1340 sim_fpu_status status
= 0;
1345 sim_fpu_32to (&wop1
, op1
);
1346 sim_fpu_32to (&wop2
, op2
);
1349 sim_fpu_64to (&wop1
, op1
);
1350 sim_fpu_64to (&wop2
, op2
);
1353 sim_io_error (SD
, "Bad switch\n");
1356 status
|= sim_fpu_abs (&waop1
, &wop1
);
1357 status
|= sim_fpu_abs (&waop2
, &wop2
);
1358 status
|= sim_fpu_min (&wans
, &waop1
, &waop2
);
1359 ret
= (sim_fpu_is_eq (&wans
, &waop1
)) ? op1
: op2
;
1361 update_fcsr (cpu
, cia
, status
);
1366 fp_maxa (sim_cpu
*cpu
,
1373 sim_fpu wop1
= {0}, wop2
= {0}, waop1
, waop2
, wans
;
1374 sim_fpu_status status
= 0;
1379 sim_fpu_32to (&wop1
, op1
);
1380 sim_fpu_32to (&wop2
, op2
);
1383 sim_fpu_64to (&wop1
, op1
);
1384 sim_fpu_64to (&wop2
, op2
);
1387 sim_io_error (SD
, "Bad switch\n");
1390 status
|= sim_fpu_abs (&waop1
, &wop1
);
1391 status
|= sim_fpu_abs (&waop2
, &wop2
);
1392 status
|= sim_fpu_max (&wans
, &waop1
, &waop2
);
1393 ret
= (sim_fpu_is_eq (&wans
, &waop1
)) ? op1
: op2
;
1395 update_fcsr (cpu
, cia
, status
);
1400 fp_recip(sim_cpu
*cpu
,
1405 return fp_unary(cpu
, cia
, &sim_fpu_inv
, op
, fmt
);
1409 fp_sqrt(sim_cpu
*cpu
,
1414 return fp_unary(cpu
, cia
, &sim_fpu_sqrt
, op
, fmt
);
1418 fp_rsqrt(sim_cpu
*cpu
,
1423 return fp_inv_sqrt(cpu
, cia
, op
, fmt
);
1427 fp_madd(sim_cpu
*cpu
,
1434 return fp_mac(cpu
, cia
, &sim_fpu_add
, op1
, op2
, op3
, 0, 0, fmt
);
1438 fp_msub(sim_cpu
*cpu
,
1445 return fp_mac(cpu
, cia
, &sim_fpu_sub
, op1
, op2
, op3
, 0, 0, fmt
);
1449 fp_fmadd (sim_cpu
*cpu
,
1456 return fp_fmac (cpu
, cia
, &sim_fpu_add
, op1
, op2
, op3
, fmt
);
1460 fp_fmsub (sim_cpu
*cpu
,
1467 return fp_fmac (cpu
, cia
, &sim_fpu_sub
, op1
, op2
, op3
, fmt
);
1471 fp_nmadd(sim_cpu
*cpu
,
1478 return fp_mac(cpu
, cia
, &sim_fpu_add
, op1
, op2
, op3
, 0, 1, fmt
);
1482 fp_nmsub(sim_cpu
*cpu
,
1489 return fp_mac(cpu
, cia
, &sim_fpu_sub
, op1
, op2
, op3
, 0, 1, fmt
);
1493 /* MIPS-3D ASE operations. */
1495 /* Variant of fp_binary for *r.ps MIPS-3D operations. */
1497 fp_binary_r(sim_cpu
*cpu
,
1499 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
1506 sim_fpu_round round
= rounding_mode (GETRM ());
1507 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1508 sim_fpu_status status_u
, status_l
;
1510 uint32_t res_u
, res_l
;
1512 /* The format must be fmt_ps. */
1514 sim_fpu_32to (&wop1
, FP_PS_upper (op1
));
1515 sim_fpu_32to (&wop2
, FP_PS_lower (op1
));
1516 status_u
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
1517 status_u
|= sim_fpu_round_32 (&ans
, round
, denorm
);
1518 sim_fpu_to32 (&res_u
, &ans
);
1520 sim_fpu_32to (&wop1
, FP_PS_upper (op2
));
1521 sim_fpu_32to (&wop2
, FP_PS_lower (op2
));
1522 status_l
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
1523 status_l
|= sim_fpu_round_32 (&ans
, round
, denorm
);
1524 sim_fpu_to32 (&res_l
, &ans
);
1525 result
= FP_PS_cat (res_u
, res_l
);
1527 update_fcsr (cpu
, cia
, status_u
| status_l
);
1532 fp_add_r(sim_cpu
*cpu
,
1538 return fp_binary_r (cpu
, cia
, &sim_fpu_add
, op1
, op2
);
1542 fp_mul_r(sim_cpu
*cpu
,
1548 return fp_binary_r (cpu
, cia
, &sim_fpu_mul
, op1
, op2
);
1551 #define NR_FRAC_GUARD (60)
1552 #define IMPLICIT_1 LSBIT64 (NR_FRAC_GUARD)
1555 fpu_inv1(sim_fpu
*f
, const sim_fpu
*l
)
1557 static const sim_fpu sim_fpu_one
= {
1558 sim_fpu_class_number
, 0, IMPLICIT_1
, 0
1562 if (sim_fpu_is_zero (l
))
1566 return sim_fpu_status_invalid_div0
;
1568 if (sim_fpu_is_infinity (l
))
1574 status
|= sim_fpu_div (f
, &sim_fpu_one
, l
);
1579 fpu_inv1_32(sim_fpu
*f
, const sim_fpu
*l
)
1581 if (sim_fpu_is_zero (l
))
1585 return sim_fpu_status_invalid_div0
;
1587 return fpu_inv1 (f
, l
);
1591 fpu_inv1_64(sim_fpu
*f
, const sim_fpu
*l
)
1593 if (sim_fpu_is_zero (l
))
1597 return sim_fpu_status_invalid_div0
;
1599 return fpu_inv1 (f
, l
);
1603 fp_recip1(sim_cpu
*cpu
,
1612 return fp_unary (cpu
, cia
, &fpu_inv1_32
, op
, fmt
);
1614 return fp_unary (cpu
, cia
, &fpu_inv1_64
, op
, fmt
);
1620 fp_recip2(sim_cpu
*cpu
,
1626 static const uint64_t one_single
= UNSIGNED64 (0x3F800000);
1627 static const uint64_t one_double
= UNSIGNED64 (0x3FF0000000000000);
1628 static const uint64_t one_ps
= (UNSIGNED64 (0x3F800000) << 32 | UNSIGNED64 (0x3F800000));
1631 /* Implemented as nmsub fd, 1, fs, ft. */
1634 case fmt_single
: one
= one_single
; break;
1635 case fmt_double
: one
= one_double
; break;
1636 case fmt_ps
: one
= one_ps
; break;
1637 default: one
= 0; abort ();
1639 return fp_mac (cpu
, cia
, &sim_fpu_sub
, op1
, op2
, one
, 0, 1, fmt
);
1643 fpu_inv_sqrt1(sim_fpu
*f
, const sim_fpu
*l
)
1645 static const sim_fpu sim_fpu_one
= {
1646 sim_fpu_class_number
, 0, IMPLICIT_1
, 0
1651 if (sim_fpu_is_zero (l
))
1655 return sim_fpu_status_invalid_div0
;
1657 if (sim_fpu_is_infinity (l
))
1661 f
->class = sim_fpu_class_zero
;
1667 status
= sim_fpu_status_invalid_sqrt
;
1671 status
|= sim_fpu_sqrt (&t
, l
);
1672 status
|= sim_fpu_div (f
, &sim_fpu_one
, &t
);
1677 fpu_inv_sqrt1_32(sim_fpu
*f
, const sim_fpu
*l
)
1679 if (sim_fpu_is_zero (l
))
1683 return sim_fpu_status_invalid_div0
;
1685 return fpu_inv_sqrt1 (f
, l
);
1689 fpu_inv_sqrt1_64(sim_fpu
*f
, const sim_fpu
*l
)
1691 if (sim_fpu_is_zero (l
))
1695 return sim_fpu_status_invalid_div0
;
1697 return fpu_inv_sqrt1 (f
, l
);
1701 fp_rsqrt1(sim_cpu
*cpu
,
1710 return fp_unary (cpu
, cia
, &fpu_inv_sqrt1_32
, op
, fmt
);
1712 return fp_unary (cpu
, cia
, &fpu_inv_sqrt1_64
, op
, fmt
);
1718 fp_rsqrt2(sim_cpu
*cpu
,
1724 static const uint64_t half_single
= UNSIGNED64 (0x3F000000);
1725 static const uint64_t half_double
= UNSIGNED64 (0x3FE0000000000000);
1726 static const uint64_t half_ps
= (UNSIGNED64 (0x3F000000) << 32 | UNSIGNED64 (0x3F000000));
1729 /* Implemented as (nmsub fd, 0.5, fs, ft)/2, where the divide is
1730 done by scaling the exponent during multiply. */
1733 case fmt_single
: half
= half_single
; break;
1734 case fmt_double
: half
= half_double
; break;
1735 case fmt_ps
: half
= half_ps
; break;
1736 default: half
= 0; abort ();
1738 return fp_mac (cpu
, cia
, &sim_fpu_sub
, op1
, op2
, half
, -1, 1, fmt
);
1742 /* Conversion operations. */
1745 convert (sim_cpu
*cpu
,
1753 sim_fpu_round round
= rounding_mode (rm
);
1754 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1757 sim_fpu_status status
= 0;
1759 /* Convert the input to sim_fpu internal format */
1763 sim_fpu_64to (&wop
, op
);
1766 sim_fpu_32to (&wop
, op
);
1769 status
= sim_fpu_i32to (&wop
, op
, round
);
1772 status
= sim_fpu_i64to (&wop
, op
, round
);
1775 sim_io_error (SD
, "Bad switch\n");
1778 /* Convert sim_fpu format into the output */
1779 /* The value WOP is converted to the destination format, rounding
1780 using mode RM. When the destination is a fixed-point format, then
1781 a source value of Infinity, NaN or one which would round to an
1782 integer outside the fixed point range then an IEEE Invalid Operation
1783 condition is raised. Not used if destination format is PS. */
1787 status
|= sim_fpu_round_32 (&wop
, round
, denorm
);
1788 /* For a NaN, normalize mantissa bits (cvt.s.d can't preserve them) */
1789 if (sim_fpu_is_qnan (&wop
))
1791 sim_fpu_to32 (&result32
, &wop
);
1792 result64
= result32
;
1795 status
|= sim_fpu_round_64 (&wop
, round
, denorm
);
1796 /* For a NaN, normalize mantissa bits (make cvt.d.s consistent) */
1797 if (sim_fpu_is_qnan (&wop
))
1799 sim_fpu_to64 (&result64
, &wop
);
1802 status
|= sim_fpu_to32u (&result32
, &wop
, round
);
1803 result64
= result32
;
1806 status
|= sim_fpu_to64u (&result64
, &wop
, round
);
1810 sim_io_error (SD
, "Bad switch\n");
1813 update_fcsr (cpu
, cia
, status
);
1818 ps_lower(sim_cpu
*cpu
,
1822 return FP_PS_lower (op
);
1826 ps_upper(sim_cpu
*cpu
,
1830 return FP_PS_upper(op
);
1834 pack_ps(sim_cpu
*cpu
,
1840 uint64_t result
= 0;
1842 /* The registers must specify FPRs valid for operands of type
1843 "fmt". If they are not valid, the result is undefined. */
1845 /* The format type should already have been checked: */
1851 uint32_t res_u
, res_l
;
1852 sim_fpu_32to (&wop
, op1
);
1853 sim_fpu_to32 (&res_u
, &wop
);
1854 sim_fpu_32to (&wop
, op2
);
1855 sim_fpu_to32 (&res_l
, &wop
);
1856 result
= FP_PS_cat(res_u
, res_l
);
1860 sim_io_error (SD
, "Bad switch\n");
1867 convert_ps (sim_cpu
*cpu
,
1874 sim_fpu wop_u
, wop_l
;
1875 sim_fpu_round round
= rounding_mode (rm
);
1876 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1877 uint32_t res_u
, res_l
;
1879 sim_fpu_status status_u
= 0, status_l
= 0;
1881 /* As convert, but used only for paired values (formats PS, PW) */
1883 /* Convert the input to sim_fpu internal format */
1886 case fmt_word
: /* fmt_pw */
1887 sim_fpu_i32to (&wop_u
, (op
>> 32) & (unsigned)0xFFFFFFFF, round
);
1888 sim_fpu_i32to (&wop_l
, op
& (unsigned)0xFFFFFFFF, round
);
1891 sim_fpu_32to (&wop_u
, FP_PS_upper(op
));
1892 sim_fpu_32to (&wop_l
, FP_PS_lower(op
));
1895 sim_io_error (SD
, "Bad switch\n");
1898 /* Convert sim_fpu format into the output */
1901 case fmt_word
: /* fmt_pw */
1902 status_u
|= sim_fpu_to32u (&res_u
, &wop_u
, round
);
1903 status_l
|= sim_fpu_to32u (&res_l
, &wop_l
, round
);
1904 result
= (((uint64_t)res_u
) << 32) | (uint64_t)res_l
;
1907 status_u
|= sim_fpu_round_32 (&wop_u
, round
, denorm
);
1908 status_l
|= sim_fpu_round_32 (&wop_l
, round
, denorm
);
1909 sim_fpu_to32 (&res_u
, &wop_u
);
1910 sim_fpu_to32 (&res_l
, &wop_l
);
1911 result
= FP_PS_cat(res_u
, res_l
);
1915 sim_io_error (SD
, "Bad switch\n");
1918 update_fcsr (cpu
, cia
, status_u
| status_l
);
1923 fpu_format_name (FP_formats fmt
)
1939 case fmt_uninterpreted
:
1940 return "<uninterpreted>";
1941 case fmt_uninterpreted_32
:
1942 return "<uninterpreted_32>";
1943 case fmt_uninterpreted_64
:
1944 return "<uninterpreted_64>";
1946 return "<format error>";
1952 fpu_rounding_mode_name (int rm
)
1965 return "<rounding mode error>";