2 * RISC-V FPU Emulation Helpers for QEMU.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
22 #include "qemu/host-utils.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
26 target_ulong
cpu_riscv_get_fflags(CPURISCVState
*env
)
28 int soft
= get_float_exception_flags(&env
->fp_status
);
29 target_ulong hard
= 0;
31 hard
|= (soft
& float_flag_inexact
) ? FPEXC_NX
: 0;
32 hard
|= (soft
& float_flag_underflow
) ? FPEXC_UF
: 0;
33 hard
|= (soft
& float_flag_overflow
) ? FPEXC_OF
: 0;
34 hard
|= (soft
& float_flag_divbyzero
) ? FPEXC_DZ
: 0;
35 hard
|= (soft
& float_flag_invalid
) ? FPEXC_NV
: 0;
40 void cpu_riscv_set_fflags(CPURISCVState
*env
, target_ulong hard
)
44 soft
|= (hard
& FPEXC_NX
) ? float_flag_inexact
: 0;
45 soft
|= (hard
& FPEXC_UF
) ? float_flag_underflow
: 0;
46 soft
|= (hard
& FPEXC_OF
) ? float_flag_overflow
: 0;
47 soft
|= (hard
& FPEXC_DZ
) ? float_flag_divbyzero
: 0;
48 soft
|= (hard
& FPEXC_NV
) ? float_flag_invalid
: 0;
50 set_float_exception_flags(soft
, &env
->fp_status
);
53 void helper_set_rounding_mode(CPURISCVState
*env
, uint32_t rm
)
62 softrm
= float_round_nearest_even
;
65 softrm
= float_round_to_zero
;
68 softrm
= float_round_down
;
71 softrm
= float_round_up
;
74 softrm
= float_round_ties_away
;
77 do_raise_exception_err(env
, RISCV_EXCP_ILLEGAL_INST
, GETPC());
80 set_float_rounding_mode(softrm
, &env
->fp_status
);
83 uint64_t helper_fmadd_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
86 return float32_muladd(frs1
, frs2
, frs3
, 0, &env
->fp_status
);
89 uint64_t helper_fmadd_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
92 return float64_muladd(frs1
, frs2
, frs3
, 0, &env
->fp_status
);
95 uint64_t helper_fmsub_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
98 return float32_muladd(frs1
, frs2
, frs3
, float_muladd_negate_c
,
102 uint64_t helper_fmsub_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
105 return float64_muladd(frs1
, frs2
, frs3
, float_muladd_negate_c
,
109 uint64_t helper_fnmsub_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
112 return float32_muladd(frs1
, frs2
, frs3
, float_muladd_negate_product
,
116 uint64_t helper_fnmsub_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
119 return float64_muladd(frs1
, frs2
, frs3
, float_muladd_negate_product
,
123 uint64_t helper_fnmadd_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
126 return float32_muladd(frs1
, frs2
, frs3
, float_muladd_negate_c
|
127 float_muladd_negate_product
, &env
->fp_status
);
130 uint64_t helper_fnmadd_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
,
133 return float64_muladd(frs1
, frs2
, frs3
, float_muladd_negate_c
|
134 float_muladd_negate_product
, &env
->fp_status
);
137 uint64_t helper_fadd_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
139 return float32_add(frs1
, frs2
, &env
->fp_status
);
142 uint64_t helper_fsub_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
144 return float32_sub(frs1
, frs2
, &env
->fp_status
);
147 uint64_t helper_fmul_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
149 return float32_mul(frs1
, frs2
, &env
->fp_status
);
152 uint64_t helper_fdiv_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
154 return float32_div(frs1
, frs2
, &env
->fp_status
);
157 uint64_t helper_fmin_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
159 return float32_minnum(frs1
, frs2
, &env
->fp_status
);
162 uint64_t helper_fmax_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
164 return float32_maxnum(frs1
, frs2
, &env
->fp_status
);
167 uint64_t helper_fsqrt_s(CPURISCVState
*env
, uint64_t frs1
)
169 return float32_sqrt(frs1
, &env
->fp_status
);
172 target_ulong
helper_fle_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
174 return float32_le(frs1
, frs2
, &env
->fp_status
);
177 target_ulong
helper_flt_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
179 return float32_lt(frs1
, frs2
, &env
->fp_status
);
182 target_ulong
helper_feq_s(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
184 return float32_eq_quiet(frs1
, frs2
, &env
->fp_status
);
187 target_ulong
helper_fcvt_w_s(CPURISCVState
*env
, uint64_t frs1
)
189 return float32_to_int32(frs1
, &env
->fp_status
);
192 target_ulong
helper_fcvt_wu_s(CPURISCVState
*env
, uint64_t frs1
)
194 return (int32_t)float32_to_uint32(frs1
, &env
->fp_status
);
197 #if defined(TARGET_RISCV64)
198 uint64_t helper_fcvt_l_s(CPURISCVState
*env
, uint64_t frs1
)
200 return float32_to_int64(frs1
, &env
->fp_status
);
203 uint64_t helper_fcvt_lu_s(CPURISCVState
*env
, uint64_t frs1
)
205 return float32_to_uint64(frs1
, &env
->fp_status
);
209 uint64_t helper_fcvt_s_w(CPURISCVState
*env
, target_ulong rs1
)
211 return int32_to_float32((int32_t)rs1
, &env
->fp_status
);
214 uint64_t helper_fcvt_s_wu(CPURISCVState
*env
, target_ulong rs1
)
216 return uint32_to_float32((uint32_t)rs1
, &env
->fp_status
);
219 #if defined(TARGET_RISCV64)
220 uint64_t helper_fcvt_s_l(CPURISCVState
*env
, uint64_t rs1
)
222 return int64_to_float32(rs1
, &env
->fp_status
);
225 uint64_t helper_fcvt_s_lu(CPURISCVState
*env
, uint64_t rs1
)
227 return uint64_to_float32(rs1
, &env
->fp_status
);
231 target_ulong
helper_fclass_s(uint64_t frs1
)
234 bool sign
= float32_is_neg(f
);
236 if (float32_is_infinity(f
)) {
237 return sign
? 1 << 0 : 1 << 7;
238 } else if (float32_is_zero(f
)) {
239 return sign
? 1 << 3 : 1 << 4;
240 } else if (float32_is_zero_or_denormal(f
)) {
241 return sign
? 1 << 2 : 1 << 5;
242 } else if (float32_is_any_nan(f
)) {
243 float_status s
= { }; /* for snan_bit_is_one */
244 return float32_is_quiet_nan(f
, &s
) ? 1 << 9 : 1 << 8;
246 return sign
? 1 << 1 : 1 << 6;
250 uint64_t helper_fadd_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
252 return float64_add(frs1
, frs2
, &env
->fp_status
);
255 uint64_t helper_fsub_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
257 return float64_sub(frs1
, frs2
, &env
->fp_status
);
260 uint64_t helper_fmul_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
262 return float64_mul(frs1
, frs2
, &env
->fp_status
);
265 uint64_t helper_fdiv_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
267 return float64_div(frs1
, frs2
, &env
->fp_status
);
270 uint64_t helper_fmin_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
272 return float64_minnum(frs1
, frs2
, &env
->fp_status
);
275 uint64_t helper_fmax_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
277 return float64_maxnum(frs1
, frs2
, &env
->fp_status
);
280 uint64_t helper_fcvt_s_d(CPURISCVState
*env
, uint64_t rs1
)
282 rs1
= float64_to_float32(rs1
, &env
->fp_status
);
283 return float32_maybe_silence_nan(rs1
, &env
->fp_status
);
286 uint64_t helper_fcvt_d_s(CPURISCVState
*env
, uint64_t rs1
)
288 rs1
= float32_to_float64(rs1
, &env
->fp_status
);
289 return float64_maybe_silence_nan(rs1
, &env
->fp_status
);
292 uint64_t helper_fsqrt_d(CPURISCVState
*env
, uint64_t frs1
)
294 return float64_sqrt(frs1
, &env
->fp_status
);
297 target_ulong
helper_fle_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
299 return float64_le(frs1
, frs2
, &env
->fp_status
);
302 target_ulong
helper_flt_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
304 return float64_lt(frs1
, frs2
, &env
->fp_status
);
307 target_ulong
helper_feq_d(CPURISCVState
*env
, uint64_t frs1
, uint64_t frs2
)
309 return float64_eq_quiet(frs1
, frs2
, &env
->fp_status
);
312 target_ulong
helper_fcvt_w_d(CPURISCVState
*env
, uint64_t frs1
)
314 return float64_to_int32(frs1
, &env
->fp_status
);
317 target_ulong
helper_fcvt_wu_d(CPURISCVState
*env
, uint64_t frs1
)
319 return (int32_t)float64_to_uint32(frs1
, &env
->fp_status
);
322 #if defined(TARGET_RISCV64)
323 uint64_t helper_fcvt_l_d(CPURISCVState
*env
, uint64_t frs1
)
325 return float64_to_int64(frs1
, &env
->fp_status
);
328 uint64_t helper_fcvt_lu_d(CPURISCVState
*env
, uint64_t frs1
)
330 return float64_to_uint64(frs1
, &env
->fp_status
);
334 uint64_t helper_fcvt_d_w(CPURISCVState
*env
, target_ulong rs1
)
336 return int32_to_float64((int32_t)rs1
, &env
->fp_status
);
339 uint64_t helper_fcvt_d_wu(CPURISCVState
*env
, target_ulong rs1
)
341 return uint32_to_float64((uint32_t)rs1
, &env
->fp_status
);
344 #if defined(TARGET_RISCV64)
345 uint64_t helper_fcvt_d_l(CPURISCVState
*env
, uint64_t rs1
)
347 return int64_to_float64(rs1
, &env
->fp_status
);
350 uint64_t helper_fcvt_d_lu(CPURISCVState
*env
, uint64_t rs1
)
352 return uint64_to_float64(rs1
, &env
->fp_status
);
356 target_ulong
helper_fclass_d(uint64_t frs1
)
359 bool sign
= float64_is_neg(f
);
361 if (float64_is_infinity(f
)) {
362 return sign
? 1 << 0 : 1 << 7;
363 } else if (float64_is_zero(f
)) {
364 return sign
? 1 << 3 : 1 << 4;
365 } else if (float64_is_zero_or_denormal(f
)) {
366 return sign
? 1 << 2 : 1 << 5;
367 } else if (float64_is_any_nan(f
)) {
368 float_status s
= { }; /* for snan_bit_is_one */
369 return float64_is_quiet_nan(f
, &s
) ? 1 << 9 : 1 << 8;
371 return sign
? 1 << 1 : 1 << 6;