2 /* MIPS Simulator FPU (CoProcessor 1) support.
3 Copyright (C) 2002 Free Software Foundation, Inc.
4 Originally created by Cygnus Solutions, modified substially
5 by Broadcom Corporation (SiByte).
7 This file is part of GDB, the GNU debugger.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* XXX: The following notice should be removed as soon as is practical: */
24 /* Floating Point Support for gdb MIPS simulators
26 This file is part of the MIPS sim
28 THIS SOFTWARE IS NOT COPYRIGHTED
31 Cygnus offers the following for use in the public domain. Cygnus
32 makes no warranty with regard to the software or it's performance
33 and the user accepts the software "AS IS" with all faults.
35 CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
36 THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 (Originally, this code was in interp.c)
44 /* Within cp1.c we refer to sim_cpu directly. */
46 #define SD CPU_STATE(cpu)
48 /*-- FPU support routines ---------------------------------------------------*/
50 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
51 formats conform to ANSI/IEEE Std 754-1985.
53 SINGLE precision floating:
54 seeeeeeeefffffffffffffffffffffff
59 SINGLE precision fixed:
60 siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
64 DOUBLE precision floating:
65 seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
70 DOUBLE precision fixed:
71 siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
75 PAIRED SINGLE precision floating:
76 seeeeeeeefffffffffffffffffffffffseeeeeeeefffffffffffffffffffffff
81 Note: upper = [63..32], lower = [31..0]
84 /* Extract packed single values: */
85 #define FP_PS_upper(v) (((v) >> 32) & (unsigned)0xFFFFFFFF)
86 #define FP_PS_lower(v) ((v) & (unsigned)0xFFFFFFFF)
87 #define FP_PS_cat(u,l) (((unsigned64)((u) & (unsigned)0xFFFFFFFF) << 32) \
88 | (unsigned64)((l) & 0xFFFFFFFF))
90 /* Explicit QNaN values. */
91 #define FPQNaN_SINGLE (0x7FBFFFFF)
92 #define FPQNaN_WORD (0x7FFFFFFF)
93 #define FPQNaN_DOUBLE (UNSIGNED64 (0x7FF7FFFFFFFFFFFF))
94 #define FPQNaN_LONG (UNSIGNED64 (0x7FFFFFFFFFFFFFFF))
95 #define FPQNaN_PS (FP_PS_cat (FPQNaN_SINGLE, FPQNaN_SINGLE))
97 static const char *fpu_format_name (FP_formats fmt
);
99 static const char *fpu_rounding_mode_name (int rm
);
103 value_fpr (sim_cpu
*cpu
,
111 /* Treat unused register values, as fixed-point 64bit values. */
112 if ((fmt
== fmt_uninterpreted
) || (fmt
== fmt_unknown
))
115 /* If request to read data as "uninterpreted", then use the current
117 fmt
= FPR_STATE
[fpr
];
123 /* For values not yet accessed, set to the desired format. */
124 if (FPR_STATE
[fpr
] == fmt_uninterpreted
)
126 FPR_STATE
[fpr
] = fmt
;
128 printf ("DBG: Register %d was fmt_uninterpreted. Now %s\n", fpr
,
129 fpu_format_name (fmt
));
132 if (fmt
!= FPR_STATE
[fpr
])
134 sim_io_eprintf (SD
, "FPR %d (format %s) being accessed with format %s - setting to unknown (PC = 0x%s)\n",
135 fpr
, fpu_format_name (FPR_STATE
[fpr
]),
136 fpu_format_name (fmt
), pr_addr (cia
));
137 FPR_STATE
[fpr
] = fmt_unknown
;
140 if (FPR_STATE
[fpr
] == fmt_unknown
)
142 /* Set QNaN value: */
145 case fmt_single
: value
= FPQNaN_SINGLE
; break;
146 case fmt_double
: value
= FPQNaN_DOUBLE
; break;
147 case fmt_word
: value
= FPQNaN_WORD
; break;
148 case fmt_long
: value
= FPQNaN_LONG
; break;
149 case fmt_ps
: value
= FPQNaN_PS
; break;
150 default: err
= -1; break;
153 else if (SizeFGR () == 64)
159 value
= (FGR
[fpr
] & 0xFFFFFFFF);
162 case fmt_uninterpreted
:
180 value
= (FGR
[fpr
] & 0xFFFFFFFF);
183 case fmt_uninterpreted
:
188 /* Even register numbers only. */
190 printf ("DBG: ValueFPR: FGR[%d] = %s, FGR[%d] = %s\n",
191 fpr
+ 1, pr_uword64 ((uword64
) FGR
[fpr
+1]),
192 fpr
, pr_uword64 ((uword64
) FGR
[fpr
]));
194 value
= ((((uword64
) FGR
[fpr
+1]) << 32)
195 | (FGR
[fpr
] & 0xFFFFFFFF));
199 SignalException (ReservedInstruction
, 0);
204 SignalException (ReservedInstruction
, 0);
214 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR ()");
217 printf ("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d\n",
218 fpr
, fpu_format_name (fmt
), pr_uword64 (value
), pr_addr (cia
),
226 store_fpr (sim_cpu
*cpu
,
235 printf ("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d, \n",
236 fpr
, fpu_format_name (fmt
), pr_uword64 (value
), pr_addr (cia
),
240 if (SizeFGR () == 64)
244 case fmt_uninterpreted_32
:
245 fmt
= fmt_uninterpreted
;
248 if (STATE_VERBOSE_P (SD
))
250 "Warning: PC 0x%s: interp.c store_fpr DEADCODE\n",
252 FGR
[fpr
] = (((uword64
) 0xDEADC0DE << 32) | (value
& 0xFFFFFFFF));
253 FPR_STATE
[fpr
] = fmt
;
256 case fmt_uninterpreted_64
:
257 fmt
= fmt_uninterpreted
;
258 case fmt_uninterpreted
:
263 FPR_STATE
[fpr
] = fmt
;
267 FPR_STATE
[fpr
] = fmt_unknown
;
276 case fmt_uninterpreted_32
:
277 fmt
= fmt_uninterpreted
;
280 FGR
[fpr
] = (value
& 0xFFFFFFFF);
281 FPR_STATE
[fpr
] = fmt
;
284 case fmt_uninterpreted_64
:
285 fmt
= fmt_uninterpreted
;
286 case fmt_uninterpreted
:
291 /* Even register numbers only. */
292 FGR
[fpr
+1] = (value
>> 32);
293 FGR
[fpr
] = (value
& 0xFFFFFFFF);
294 FPR_STATE
[fpr
+ 1] = fmt
;
295 FPR_STATE
[fpr
] = fmt
;
299 FPR_STATE
[fpr
] = fmt_unknown
;
300 FPR_STATE
[fpr
+ 1] = fmt_unknown
;
301 SignalException (ReservedInstruction
, 0);
306 FPR_STATE
[fpr
] = fmt_unknown
;
307 SignalException (ReservedInstruction
, 0);
311 FPR_STATE
[fpr
] = fmt_unknown
;
318 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR ()");
321 printf ("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",
322 fpr
, pr_uword64 (FGR
[fpr
]), fpu_format_name (fmt
));
329 /* CP1 control/status register access functions. */
332 test_fcsr (sim_cpu
*cpu
,
337 cause
= (FCSR
& fcsr_CAUSE_mask
) >> fcsr_CAUSE_shift
;
338 if ((cause
& ((FCSR
& fcsr_ENABLES_mask
) >> fcsr_ENABLES_shift
)) != 0
339 || (cause
& (1 << UO
)))
341 SignalExceptionFPE();
346 value_fcr(sim_cpu
*cpu
,
350 unsigned32 value
= 0;
354 case 0: /* FP Implementation and Revision Register. */
357 case 25: /* FP Condition Codes Register (derived from FCSR). */
358 value
= (FCR31
& fcsr_FCC_mask
) >> fcsr_FCC_shift
;
359 value
= (value
& 0x1) | (value
>> 1); /* Close FCC gap. */
361 case 26: /* FP Exceptions Register (derived from FCSR). */
362 value
= FCR31
& (fcsr_CAUSE_mask
| fcsr_FLAGS_mask
);
364 case 28: /* FP Enables Register (derived from FCSR). */
365 value
= FCR31
& (fcsr_ENABLES_mask
| fcsr_RM_mask
);
366 if ((FCR31
& fcsr_FS
) != 0)
369 case 31: /* FP Control/Status Register (FCSR). */
370 value
= FCR31
& ~fcsr_ZERO_mask
;
374 return (EXTEND32 (value
));
378 store_fcr(sim_cpu
*cpu
,
388 case 25: /* FP Condition Codes Register (stored into FCSR). */
389 v
= (v
<< 1) | (v
& 0x1); /* Adjust for FCC gap. */
390 FCR31
&= ~fcsr_FCC_mask
;
391 FCR31
|= ((v
<< fcsr_FCC_shift
) & fcsr_FCC_mask
);
393 case 26: /* FP Exceptions Register (stored into FCSR). */
394 FCR31
&= ~(fcsr_CAUSE_mask
| fcsr_FLAGS_mask
);
395 FCR31
|= (v
& (fcsr_CAUSE_mask
| fcsr_FLAGS_mask
));
398 case 28: /* FP Enables Register (stored into FCSR). */
399 if ((v
& fenr_FS
) != 0)
403 FCR31
&= (fcsr_FCC_mask
| fcsr_CAUSE_mask
| fcsr_FLAGS_mask
);
404 FCR31
|= (v
& (fcsr_FS
| fcsr_ENABLES_mask
| fcsr_RM_mask
));
407 case 31: /* FP Control/Status Register (FCSR). */
408 FCR31
= v
& ~fcsr_ZERO_mask
;
415 update_fcsr (sim_cpu
*cpu
,
417 sim_fpu_status status
)
419 FCSR
&= ~fcsr_CAUSE_mask
;
423 unsigned int cause
= 0;
425 /* map between sim_fpu codes and MIPS FCSR */
426 if (status
& (sim_fpu_status_invalid_snan
427 | sim_fpu_status_invalid_isi
428 | sim_fpu_status_invalid_idi
429 | sim_fpu_status_invalid_zdz
430 | sim_fpu_status_invalid_imz
431 | sim_fpu_status_invalid_cmp
432 | sim_fpu_status_invalid_sqrt
433 | sim_fpu_status_invalid_cvi
))
435 if (status
& sim_fpu_status_invalid_div0
)
437 if (status
& sim_fpu_status_overflow
)
439 if (status
& sim_fpu_status_underflow
)
441 if (status
& sim_fpu_status_inexact
)
444 /* Implicit clearing of other bits by unimplemented done by callers. */
445 if (status
& sim_fpu_status_unimplemented
)
449 FCSR
|= (cause
<< fcsr_CAUSE_shift
);
450 test_fcsr (cpu
, cia
);
451 FCSR
|= ((cause
& ~(1 << UO
)) << fcsr_FLAGS_shift
);
457 rounding_mode(int rm
)
464 /* Round result to nearest representable value. When two
465 representable values are equally near, round to the value
466 that has a least significant bit of zero (i.e. is even). */
467 round
= sim_fpu_round_near
;
470 /* Round result to the value closest to, and not greater in
471 magnitude than, the result. */
472 round
= sim_fpu_round_zero
;
475 /* Round result to the value closest to, and not less than,
477 round
= sim_fpu_round_up
;
480 /* Round result to the value closest to, and not greater than,
482 round
= sim_fpu_round_down
;
486 fprintf (stderr
, "Bad switch\n");
492 /* When the FS bit is set, MIPS processors return zero for
493 denormalized results and optionally replace denormalized inputs
494 with zero. When FS is clear, some implementation trap on input
495 and/or output, while other perform the operation in hardware. */
496 static sim_fpu_denorm
497 denorm_mode(sim_cpu
*cpu
)
499 sim_fpu_denorm denorm
;
501 /* XXX: FIXME: Eventually should be CPU model dependent. */
503 denorm
= sim_fpu_denorm_zero
;
510 /* Comparison operations. */
512 static sim_fpu_status
513 fp_test(unsigned64 op1
,
522 sim_fpu_status status
= 0;
523 int less
, equal
, unordered
;
525 /* The format type has already been checked: */
530 sim_fpu_32to (&wop1
, op1
);
531 sim_fpu_32to (&wop2
, op2
);
536 sim_fpu_64to (&wop1
, op1
);
537 sim_fpu_64to (&wop2
, op2
);
541 fprintf (stderr
, "Bad switch\n");
545 if (sim_fpu_is_nan (&wop1
) || sim_fpu_is_nan (&wop2
))
547 if ((cond
& (1 << 3)) ||
548 sim_fpu_is_snan (&wop1
) || sim_fpu_is_snan (&wop2
))
549 status
= sim_fpu_status_invalid_snan
;
558 status
|= sim_fpu_abs (&wop1
, &wop1
);
559 status
|= sim_fpu_abs (&wop2
, &wop2
);
561 equal
= sim_fpu_is_eq (&wop1
, &wop2
);
562 less
= !equal
&& sim_fpu_is_lt (&wop1
, &wop2
);
565 *condition
= (((cond
& (1 << 2)) && less
)
566 || ((cond
& (1 << 1)) && equal
)
567 || ((cond
& (1 << 0)) && unordered
));
581 sim_fpu_status status
= 0;
583 /* The format type should already have been checked. The FCSR is
584 updated before the condition codes so that any exceptions will
585 be signalled before the condition codes are changed. */
592 status
= fp_test(op1
, op2
, fmt
, abs
, cond
, &result
);
593 update_fcsr (cpu
, cia
, status
);
599 int result0
, result1
;
600 status
= fp_test(FP_PS_lower (op1
), FP_PS_lower (op2
), fmt_single
,
601 abs
, cond
, &result0
);
602 status
|= fp_test(FP_PS_upper (op1
), FP_PS_upper (op2
), fmt_single
,
603 abs
, cond
, &result1
);
604 update_fcsr (cpu
, cia
, status
);
605 SETFCC (cc
, result0
);
606 SETFCC (cc
+1, result1
);
610 sim_io_eprintf (SD
, "Bad switch\n");
616 /* Basic arithmetic operations. */
619 fp_unary(sim_cpu
*cpu
,
621 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*),
627 sim_fpu_round round
= rounding_mode (GETRM());
628 sim_fpu_denorm denorm
= denorm_mode (cpu
);
629 sim_fpu_status status
= 0;
630 unsigned64 result
= 0;
632 /* The format type has already been checked: */
638 sim_fpu_32to (&wop
, op
);
639 status
|= (*sim_fpu_op
) (&ans
, &wop
);
640 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
641 sim_fpu_to32 (&res
, &ans
);
648 sim_fpu_64to (&wop
, op
);
649 status
|= (*sim_fpu_op
) (&ans
, &wop
);
650 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
651 sim_fpu_to64 (&res
, &ans
);
657 int status_u
= 0, status_l
= 0;
658 unsigned32 res_u
, res_l
;
659 sim_fpu_32to (&wop
, FP_PS_upper(op
));
660 status_u
|= (*sim_fpu_op
) (&ans
, &wop
);
661 sim_fpu_to32 (&res_u
, &ans
);
662 sim_fpu_32to (&wop
, FP_PS_lower(op
));
663 status_l
|= (*sim_fpu_op
) (&ans
, &wop
);
664 sim_fpu_to32 (&res_l
, &ans
);
665 result
= FP_PS_cat(res_u
, res_l
);
666 status
= status_u
| status_l
;
670 sim_io_eprintf (SD
, "Bad switch\n");
674 update_fcsr (cpu
, cia
, status
);
679 fp_binary(sim_cpu
*cpu
,
681 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
689 sim_fpu_round round
= rounding_mode (GETRM());
690 sim_fpu_denorm denorm
= denorm_mode (cpu
);
691 sim_fpu_status status
= 0;
692 unsigned64 result
= 0;
694 /* The format type has already been checked: */
700 sim_fpu_32to (&wop1
, op1
);
701 sim_fpu_32to (&wop2
, op2
);
702 status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
703 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
704 sim_fpu_to32 (&res
, &ans
);
711 sim_fpu_64to (&wop1
, op1
);
712 sim_fpu_64to (&wop2
, op2
);
713 status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
714 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
715 sim_fpu_to64 (&res
, &ans
);
721 int status_u
= 0, status_l
= 0;
722 unsigned32 res_u
, res_l
;
723 sim_fpu_32to (&wop1
, FP_PS_upper(op1
));
724 sim_fpu_32to (&wop2
, FP_PS_upper(op2
));
725 status_u
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
726 sim_fpu_to32 (&res_u
, &ans
);
727 sim_fpu_32to (&wop1
, FP_PS_lower(op1
));
728 sim_fpu_32to (&wop2
, FP_PS_lower(op2
));
729 status_l
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
730 sim_fpu_to32 (&res_l
, &ans
);
731 result
= FP_PS_cat(res_u
, res_l
);
732 status
= status_u
| status_l
;
736 sim_io_eprintf (SD
, "Bad switch\n");
740 update_fcsr (cpu
, cia
, status
);
744 /* Common MAC code for single operands (.s or .d), defers setting FCSR. */
745 static sim_fpu_status
746 inner_mac(int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
754 sim_fpu_denorm denorm
,
760 sim_fpu_status status
= 0;
761 sim_fpu_status op_status
;
769 sim_fpu_32to (&wop1
, op1
);
770 sim_fpu_32to (&wop2
, op2
);
771 status
|= sim_fpu_mul (&ans
, &wop1
, &wop2
);
772 if (scale
!= 0 && sim_fpu_is_number (&ans
)) /* number or denorm */
773 ans
.normal_exp
+= scale
;
774 status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
777 sim_fpu_32to (&wop2
, op3
);
778 op_status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
779 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
784 op_status
= sim_fpu_neg (&ans
, &wop1
);
785 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
788 sim_fpu_to32 (&res
, &ans
);
795 sim_fpu_64to (&wop1
, op1
);
796 sim_fpu_64to (&wop2
, op2
);
797 status
|= sim_fpu_mul (&ans
, &wop1
, &wop2
);
798 if (scale
!= 0 && sim_fpu_is_number (&ans
)) /* number or denorm */
799 ans
.normal_exp
+= scale
;
800 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
803 sim_fpu_64to (&wop2
, op3
);
804 op_status
|= (*sim_fpu_op
) (&ans
, &wop1
, &wop2
);
805 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
810 op_status
= sim_fpu_neg (&ans
, &wop1
);
811 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
814 sim_fpu_to64 (&res
, &ans
);
819 fprintf (stderr
, "Bad switch\n");
826 /* Common implementation of madd, nmadd, msub, nmsub that does
827 intermediate rounding per spec. Also used for recip2 and rsqrt2,
828 which are transformed into equivalent nmsub operations. The scale
829 argument is an adjustment to the exponent of the intermediate
830 product op1*op2. It is currently non-zero for rsqrt2 (-1), which
831 requires an effective division by 2. */
835 int (*sim_fpu_op
)(sim_fpu
*, const sim_fpu
*, const sim_fpu
*),
843 sim_fpu_round round
= rounding_mode (GETRM());
844 sim_fpu_denorm denorm
= denorm_mode (cpu
);
845 sim_fpu_status status
= 0;
846 unsigned64 result
= 0;
848 /* The format type has already been checked: */
853 status
= inner_mac(sim_fpu_op
, op1
, op2
, op3
, scale
,
854 negate
, fmt
, round
, denorm
, &result
);
858 int status_u
, status_l
;
859 unsigned64 result_u
, result_l
;
860 status_u
= inner_mac(sim_fpu_op
, FP_PS_upper(op1
), FP_PS_upper(op2
),
861 FP_PS_upper(op3
), scale
, negate
, fmt_single
,
862 round
, denorm
, &result_u
);
863 status_l
= inner_mac(sim_fpu_op
, FP_PS_lower(op1
), FP_PS_lower(op2
),
864 FP_PS_lower(op3
), scale
, negate
, fmt_single
,
865 round
, denorm
, &result_l
);
866 result
= FP_PS_cat(result_u
, result_l
);
867 status
= status_u
| status_l
;
871 sim_io_eprintf (SD
, "Bad switch\n");
875 update_fcsr (cpu
, cia
, status
);
879 /* Common rsqrt code for single operands (.s or .d), intermediate rounding. */
880 static sim_fpu_status
881 inner_rsqrt(unsigned64 op1
,
884 sim_fpu_denorm denorm
,
889 sim_fpu_status status
= 0;
890 sim_fpu_status op_status
;
898 sim_fpu_32to (&wop1
, op1
);
899 status
|= sim_fpu_sqrt (&ans
, &wop1
);
900 status
|= sim_fpu_round_32 (&ans
, status
, round
);
902 op_status
= sim_fpu_inv (&ans
, &wop1
);
903 op_status
|= sim_fpu_round_32 (&ans
, round
, denorm
);
904 sim_fpu_to32 (&res
, &ans
);
912 sim_fpu_64to (&wop1
, op1
);
913 status
|= sim_fpu_sqrt (&ans
, &wop1
);
914 status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
916 op_status
= sim_fpu_inv (&ans
, &wop1
);
917 op_status
|= sim_fpu_round_64 (&ans
, round
, denorm
);
918 sim_fpu_to64 (&res
, &ans
);
924 fprintf (stderr
, "Bad switch\n");
932 fp_inv_sqrt(sim_cpu
*cpu
,
937 sim_fpu_round round
= rounding_mode (GETRM());
938 sim_fpu_round denorm
= denorm_mode (cpu
);
939 sim_fpu_status status
= 0;
940 unsigned64 result
= 0;
942 /* The format type has already been checked: */
947 status
= inner_rsqrt (op1
, fmt
, round
, denorm
, &result
);
951 int status_u
, status_l
;
952 unsigned64 result_u
, result_l
;
953 status_u
= inner_rsqrt (FP_PS_upper(op1
), fmt_single
, round
, denorm
,
955 status_l
= inner_rsqrt (FP_PS_lower(op1
), fmt_single
, round
, denorm
,
957 result
= FP_PS_cat(result_u
, result_l
);
958 status
= status_u
| status_l
;
962 sim_io_eprintf (SD
, "Bad switch\n");
966 update_fcsr (cpu
, cia
, status
);
977 return fp_unary(cpu
, cia
, &sim_fpu_abs
, op
, fmt
);
986 return fp_unary(cpu
, cia
, &sim_fpu_neg
, op
, fmt
);
996 return fp_binary(cpu
, cia
, &sim_fpu_add
, op1
, op2
, fmt
);
1000 fp_sub(sim_cpu
*cpu
,
1006 return fp_binary(cpu
, cia
, &sim_fpu_sub
, op1
, op2
, fmt
);
1010 fp_mul(sim_cpu
*cpu
,
1016 return fp_binary(cpu
, cia
, &sim_fpu_mul
, op1
, op2
, fmt
);
1020 fp_div(sim_cpu
*cpu
,
1026 return fp_binary(cpu
, cia
, &sim_fpu_div
, op1
, op2
, fmt
);
1030 fp_recip(sim_cpu
*cpu
,
1035 return fp_unary(cpu
, cia
, &sim_fpu_inv
, op
, fmt
);
1039 fp_sqrt(sim_cpu
*cpu
,
1044 return fp_unary(cpu
, cia
, &sim_fpu_sqrt
, op
, fmt
);
1048 fp_rsqrt(sim_cpu
*cpu
,
1053 return fp_inv_sqrt(cpu
, cia
, op
, fmt
);
1057 fp_madd(sim_cpu
*cpu
,
1064 return fp_mac(cpu
, cia
, &sim_fpu_add
, op1
, op2
, op3
, 0, 0, fmt
);
1068 fp_msub(sim_cpu
*cpu
,
1075 return fp_mac(cpu
, cia
, &sim_fpu_sub
, op1
, op2
, op3
, 0, 0, fmt
);
1079 fp_nmadd(sim_cpu
*cpu
,
1086 return fp_mac(cpu
, cia
, &sim_fpu_add
, op1
, op2
, op3
, 0, 1, fmt
);
1090 fp_nmsub(sim_cpu
*cpu
,
1097 return fp_mac(cpu
, cia
, &sim_fpu_sub
, op1
, op2
, op3
, 0, 1, fmt
);
1101 /* Conversion operations. */
1104 convert (sim_cpu
*cpu
,
1112 sim_fpu_round round
= rounding_mode (rm
);
1113 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1114 unsigned32 result32
;
1115 unsigned64 result64
;
1116 sim_fpu_status status
= 0;
1118 /* Convert the input to sim_fpu internal format */
1122 sim_fpu_64to (&wop
, op
);
1125 sim_fpu_32to (&wop
, op
);
1128 status
= sim_fpu_i32to (&wop
, op
, round
);
1131 status
= sim_fpu_i64to (&wop
, op
, round
);
1134 sim_io_eprintf (SD
, "Bad switch\n");
1138 /* Convert sim_fpu format into the output */
1139 /* The value WOP is converted to the destination format, rounding
1140 using mode RM. When the destination is a fixed-point format, then
1141 a source value of Infinity, NaN or one which would round to an
1142 integer outside the fixed point range then an IEEE Invalid Operation
1143 condition is raised. Not used if destination format is PS. */
1147 status
|= sim_fpu_round_32 (&wop
, round
, denorm
);
1148 /* For a NaN, normalize mantissa bits (cvt.s.d can't preserve them) */
1149 if (sim_fpu_is_qnan (&wop
))
1151 sim_fpu_to32 (&result32
, &wop
);
1152 result64
= result32
;
1155 status
|= sim_fpu_round_64 (&wop
, round
, denorm
);
1156 /* For a NaN, normalize mantissa bits (make cvt.d.s consistent) */
1157 if (sim_fpu_is_qnan (&wop
))
1159 sim_fpu_to64 (&result64
, &wop
);
1162 status
|= sim_fpu_to32i (&result32
, &wop
, round
);
1163 result64
= result32
;
1166 status
|= sim_fpu_to64i (&result64
, &wop
, round
);
1170 sim_io_eprintf (SD
, "Bad switch\n");
1174 update_fcsr (cpu
, cia
, status
);
1179 ps_lower(sim_cpu
*cpu
,
1183 return FP_PS_lower (op
);
1187 ps_upper(sim_cpu
*cpu
,
1191 return FP_PS_upper(op
);
1195 pack_ps(sim_cpu
*cpu
,
1201 unsigned64 result
= 0;
1203 /* The registers must specify FPRs valid for operands of type
1204 "fmt". If they are not valid, the result is undefined. */
1206 /* The format type should already have been checked: */
1212 unsigned32 res_u
, res_l
;
1213 sim_fpu_32to (&wop
, op1
);
1214 sim_fpu_to32 (&res_u
, &wop
);
1215 sim_fpu_32to (&wop
, op2
);
1216 sim_fpu_to32 (&res_l
, &wop
);
1217 result
= FP_PS_cat(res_u
, res_l
);
1221 sim_io_eprintf (SD
, "Bad switch\n");
1229 convert_ps (sim_cpu
*cpu
,
1236 sim_fpu wop_u
, wop_l
;
1237 sim_fpu_round round
= rounding_mode (rm
);
1238 sim_fpu_denorm denorm
= denorm_mode (cpu
);
1239 unsigned32 res_u
, res_l
;
1241 sim_fpu_status status_u
= 0, status_l
= 0;
1243 /* As convert, but used only for paired values (formats PS, PW) */
1245 /* Convert the input to sim_fpu internal format */
1248 case fmt_word
: /* fmt_pw */
1249 sim_fpu_i32to (&wop_u
, (op
>> 32) & (unsigned)0xFFFFFFFF, round
);
1250 sim_fpu_i32to (&wop_l
, op
& (unsigned)0xFFFFFFFF, round
);
1253 sim_fpu_32to (&wop_u
, FP_PS_upper(op
));
1254 sim_fpu_32to (&wop_l
, FP_PS_lower(op
));
1257 sim_io_eprintf (SD
, "Bad switch\n");
1261 /* Convert sim_fpu format into the output */
1264 case fmt_word
: /* fmt_pw */
1265 status_u
|= sim_fpu_to32i (&res_u
, &wop_u
, round
);
1266 status_l
|= sim_fpu_to32i (&res_l
, &wop_l
, round
);
1267 result
= (((unsigned64
)res_u
) << 32) | (unsigned64
)res_l
;
1270 status_u
|= sim_fpu_round_32 (&wop_u
, 0, round
);
1271 status_l
|= sim_fpu_round_32 (&wop_l
, 0, round
);
1272 sim_fpu_to32 (&res_u
, &wop_u
);
1273 sim_fpu_to32 (&res_l
, &wop_l
);
1274 result
= FP_PS_cat(res_u
, res_l
);
1278 sim_io_eprintf (SD
, "Bad switch\n");
1282 update_fcsr (cpu
, cia
, status_u
| status_l
);
1287 fpu_format_name (FP_formats fmt
)
1303 case fmt_uninterpreted
:
1304 return "<uninterpreted>";
1305 case fmt_uninterpreted_32
:
1306 return "<uninterpreted_32>";
1307 case fmt_uninterpreted_64
:
1308 return "<uninterpreted_64>";
1310 return "<format error>";
1316 fpu_rounding_mode_name (int rm
)
1329 return "<rounding mode error>";