xref: /qemu/target/mips/fpu_helper.h (revision 5c3ba8105563010f3d4b1c79a90986f03368e534)
181ddae7cSPhilippe Mathieu-Daudé /*
281ddae7cSPhilippe Mathieu-Daudé  * Helpers for emulation of FPU-related MIPS instructions.
381ddae7cSPhilippe Mathieu-Daudé  *
481ddae7cSPhilippe Mathieu-Daudé  *  Copyright (C) 2004-2005  Jocelyn Mayer
581ddae7cSPhilippe Mathieu-Daudé  *
681ddae7cSPhilippe Mathieu-Daudé  * SPDX-License-Identifier: LGPL-2.1-or-later
781ddae7cSPhilippe Mathieu-Daudé  */
881ddae7cSPhilippe Mathieu-Daudé #include "fpu/softfloat-helpers.h"
981ddae7cSPhilippe Mathieu-Daudé #include "cpu.h"
1081ddae7cSPhilippe Mathieu-Daudé 
1181ddae7cSPhilippe Mathieu-Daudé extern const FloatRoundMode ieee_rm[4];
1281ddae7cSPhilippe Mathieu-Daudé 
1381ddae7cSPhilippe Mathieu-Daudé uint32_t float_class_s(uint32_t arg, float_status *fst);
1481ddae7cSPhilippe Mathieu-Daudé uint64_t float_class_d(uint64_t arg, float_status *fst);
1581ddae7cSPhilippe Mathieu-Daudé 
1681ddae7cSPhilippe Mathieu-Daudé static inline void restore_rounding_mode(CPUMIPSState *env)
1781ddae7cSPhilippe Mathieu-Daudé {
1881ddae7cSPhilippe Mathieu-Daudé     set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3],
1981ddae7cSPhilippe Mathieu-Daudé                             &env->active_fpu.fp_status);
2081ddae7cSPhilippe Mathieu-Daudé }
2181ddae7cSPhilippe Mathieu-Daudé 
2281ddae7cSPhilippe Mathieu-Daudé static inline void restore_flush_mode(CPUMIPSState *env)
2381ddae7cSPhilippe Mathieu-Daudé {
2481ddae7cSPhilippe Mathieu-Daudé     set_flush_to_zero((env->active_fpu.fcr31 & (1 << FCR31_FS)) != 0,
2581ddae7cSPhilippe Mathieu-Daudé                       &env->active_fpu.fp_status);
2681ddae7cSPhilippe Mathieu-Daudé }
2781ddae7cSPhilippe Mathieu-Daudé 
2881ddae7cSPhilippe Mathieu-Daudé static inline void restore_snan_bit_mode(CPUMIPSState *env)
2981ddae7cSPhilippe Mathieu-Daudé {
30e9e5534fSRichard Henderson     bool nan2008 = env->active_fpu.fcr31 & (1 << FCR31_NAN2008);
31a71492f7SPeter Maydell     FloatInfZeroNaNRule izn_rule;
323a453712SPeter Maydell     Float3NaNPropRule nan3_rule;
33e9e5534fSRichard Henderson 
34e9e5534fSRichard Henderson     /*
35e9e5534fSRichard Henderson      * With nan2008, SNaNs are silenced in the usual way.
36e9e5534fSRichard Henderson      * Before that, SNaNs are not silenced; default nans are produced.
37e9e5534fSRichard Henderson      */
38e9e5534fSRichard Henderson     set_snan_bit_is_one(!nan2008, &env->active_fpu.fp_status);
39e9e5534fSRichard Henderson     set_default_nan_mode(!nan2008, &env->active_fpu.fp_status);
40a71492f7SPeter Maydell     /*
41a71492f7SPeter Maydell      * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
42a71492f7SPeter Maydell      * case sets InvalidOp and returns the default NaN.
43a71492f7SPeter Maydell      * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
44a71492f7SPeter Maydell      * case sets InvalidOp and returns the input value 'c'.
45a71492f7SPeter Maydell      */
46a71492f7SPeter Maydell     izn_rule = nan2008 ? float_infzeronan_dnan_never : float_infzeronan_dnan_always;
47a71492f7SPeter Maydell     set_float_infzeronan_rule(izn_rule, &env->active_fpu.fp_status);
483a453712SPeter Maydell     nan3_rule = nan2008 ? float_3nan_prop_s_cab : float_3nan_prop_s_abc;
493a453712SPeter Maydell     set_float_3nan_prop_rule(nan3_rule, &env->active_fpu.fp_status);
50*5c3ba810SPeter Maydell     /*
51*5c3ba810SPeter Maydell      * With nan2008, the default NaN value has the sign bit clear and the
52*5c3ba810SPeter Maydell      * frac msb set; with the older mode, the sign bit is clear, and all
53*5c3ba810SPeter Maydell      * frac bits except the msb are set.
54*5c3ba810SPeter Maydell      */
55*5c3ba810SPeter Maydell     set_float_default_nan_pattern(nan2008 ? 0b01000000 : 0b00111111,
56*5c3ba810SPeter Maydell                                   &env->active_fpu.fp_status);
573a453712SPeter Maydell 
5881ddae7cSPhilippe Mathieu-Daudé }
5981ddae7cSPhilippe Mathieu-Daudé 
6081ddae7cSPhilippe Mathieu-Daudé static inline void restore_fp_status(CPUMIPSState *env)
6181ddae7cSPhilippe Mathieu-Daudé {
6281ddae7cSPhilippe Mathieu-Daudé     restore_rounding_mode(env);
6381ddae7cSPhilippe Mathieu-Daudé     restore_flush_mode(env);
6481ddae7cSPhilippe Mathieu-Daudé     restore_snan_bit_mode(env);
6581ddae7cSPhilippe Mathieu-Daudé }
6681ddae7cSPhilippe Mathieu-Daudé 
670c587f13SPeter Maydell static inline void fp_reset(CPUMIPSState *env)
680c587f13SPeter Maydell {
690c587f13SPeter Maydell     restore_fp_status(env);
700c587f13SPeter Maydell 
710c587f13SPeter Maydell     /*
720c587f13SPeter Maydell      * According to MIPS specifications, if one of the two operands is
730c587f13SPeter Maydell      * a sNaN, a new qNaN has to be generated. This is done in
740c587f13SPeter Maydell      * floatXX_silence_nan(). For qNaN inputs the specifications
750c587f13SPeter Maydell      * says: "When possible, this QNaN result is one of the operand QNaN
760c587f13SPeter Maydell      * values." In practice it seems that most implementations choose
770c587f13SPeter Maydell      * the first operand if both operands are qNaN. In short this gives
780c587f13SPeter Maydell      * the following rules:
790c587f13SPeter Maydell      *  1. A if it is signaling
800c587f13SPeter Maydell      *  2. B if it is signaling
810c587f13SPeter Maydell      *  3. A (quiet)
820c587f13SPeter Maydell      *  4. B (quiet)
830c587f13SPeter Maydell      * A signaling NaN is always silenced before returning it.
840c587f13SPeter Maydell      */
850c587f13SPeter Maydell     set_float_2nan_prop_rule(float_2nan_prop_s_ab,
860c587f13SPeter Maydell                              &env->active_fpu.fp_status);
870c587f13SPeter Maydell }
880c587f13SPeter Maydell 
8981ddae7cSPhilippe Mathieu-Daudé /* MSA */
9081ddae7cSPhilippe Mathieu-Daudé 
9181ddae7cSPhilippe Mathieu-Daudé enum CPUMIPSMSADataFormat {
9281ddae7cSPhilippe Mathieu-Daudé     DF_BYTE = 0,
9381ddae7cSPhilippe Mathieu-Daudé     DF_HALF,
9481ddae7cSPhilippe Mathieu-Daudé     DF_WORD,
9581ddae7cSPhilippe Mathieu-Daudé     DF_DOUBLE
9681ddae7cSPhilippe Mathieu-Daudé };
9781ddae7cSPhilippe Mathieu-Daudé 
9881ddae7cSPhilippe Mathieu-Daudé static inline void restore_msa_fp_status(CPUMIPSState *env)
9981ddae7cSPhilippe Mathieu-Daudé {
10081ddae7cSPhilippe Mathieu-Daudé     float_status *status = &env->active_tc.msa_fp_status;
10181ddae7cSPhilippe Mathieu-Daudé     int rounding_mode = (env->active_tc.msacsr & MSACSR_RM_MASK) >> MSACSR_RM;
10281ddae7cSPhilippe Mathieu-Daudé     bool flush_to_zero = (env->active_tc.msacsr & MSACSR_FS_MASK) != 0;
10381ddae7cSPhilippe Mathieu-Daudé 
10481ddae7cSPhilippe Mathieu-Daudé     set_float_rounding_mode(ieee_rm[rounding_mode], status);
10581ddae7cSPhilippe Mathieu-Daudé     set_flush_to_zero(flush_to_zero, status);
10681ddae7cSPhilippe Mathieu-Daudé     set_flush_inputs_to_zero(flush_to_zero, status);
10781ddae7cSPhilippe Mathieu-Daudé }
108