target/riscv: rationalise softfloat includes
[qemu/ar7.git] / target / riscv / fpu_helper.c
blob0b79562a690b28de3399596ec624ff79bf53c091
1 /*
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
13 * more details.
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"
20 #include "cpu.h"
21 #include "qemu/host-utils.h"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "fpu/softfloat.h"
26 target_ulong riscv_cpu_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;
37 return hard;
40 void riscv_cpu_set_fflags(CPURISCVState *env, target_ulong hard)
42 int soft = 0;
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)
55 int softrm;
57 if (rm == 7) {
58 rm = env->frm;
60 switch (rm) {
61 case 0:
62 softrm = float_round_nearest_even;
63 break;
64 case 1:
65 softrm = float_round_to_zero;
66 break;
67 case 2:
68 softrm = float_round_down;
69 break;
70 case 3:
71 softrm = float_round_up;
72 break;
73 case 4:
74 softrm = float_round_ties_away;
75 break;
76 default:
77 riscv_raise_exception(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,
84 uint64_t frs3)
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,
90 uint64_t frs3)
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,
96 uint64_t frs3)
98 return float32_muladd(frs1, frs2, frs3, float_muladd_negate_c,
99 &env->fp_status);
102 uint64_t helper_fmsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
103 uint64_t frs3)
105 return float64_muladd(frs1, frs2, frs3, float_muladd_negate_c,
106 &env->fp_status);
109 uint64_t helper_fnmsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
110 uint64_t frs3)
112 return float32_muladd(frs1, frs2, frs3, float_muladd_negate_product,
113 &env->fp_status);
116 uint64_t helper_fnmsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
117 uint64_t frs3)
119 return float64_muladd(frs1, frs2, frs3, float_muladd_negate_product,
120 &env->fp_status);
123 uint64_t helper_fnmadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
124 uint64_t frs3)
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,
131 uint64_t frs3)
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);
207 #endif
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);
229 #endif
231 target_ulong helper_fclass_s(uint64_t frs1)
233 float32 f = 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;
245 } else {
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 return float64_to_float32(rs1, &env->fp_status);
285 uint64_t helper_fcvt_d_s(CPURISCVState *env, uint64_t rs1)
287 return float32_to_float64(rs1, &env->fp_status);
290 uint64_t helper_fsqrt_d(CPURISCVState *env, uint64_t frs1)
292 return float64_sqrt(frs1, &env->fp_status);
295 target_ulong helper_fle_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
297 return float64_le(frs1, frs2, &env->fp_status);
300 target_ulong helper_flt_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
302 return float64_lt(frs1, frs2, &env->fp_status);
305 target_ulong helper_feq_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
307 return float64_eq_quiet(frs1, frs2, &env->fp_status);
310 target_ulong helper_fcvt_w_d(CPURISCVState *env, uint64_t frs1)
312 return float64_to_int32(frs1, &env->fp_status);
315 target_ulong helper_fcvt_wu_d(CPURISCVState *env, uint64_t frs1)
317 return (int32_t)float64_to_uint32(frs1, &env->fp_status);
320 #if defined(TARGET_RISCV64)
321 uint64_t helper_fcvt_l_d(CPURISCVState *env, uint64_t frs1)
323 return float64_to_int64(frs1, &env->fp_status);
326 uint64_t helper_fcvt_lu_d(CPURISCVState *env, uint64_t frs1)
328 return float64_to_uint64(frs1, &env->fp_status);
330 #endif
332 uint64_t helper_fcvt_d_w(CPURISCVState *env, target_ulong rs1)
334 return int32_to_float64((int32_t)rs1, &env->fp_status);
337 uint64_t helper_fcvt_d_wu(CPURISCVState *env, target_ulong rs1)
339 return uint32_to_float64((uint32_t)rs1, &env->fp_status);
342 #if defined(TARGET_RISCV64)
343 uint64_t helper_fcvt_d_l(CPURISCVState *env, uint64_t rs1)
345 return int64_to_float64(rs1, &env->fp_status);
348 uint64_t helper_fcvt_d_lu(CPURISCVState *env, uint64_t rs1)
350 return uint64_to_float64(rs1, &env->fp_status);
352 #endif
354 target_ulong helper_fclass_d(uint64_t frs1)
356 float64 f = frs1;
357 bool sign = float64_is_neg(f);
359 if (float64_is_infinity(f)) {
360 return sign ? 1 << 0 : 1 << 7;
361 } else if (float64_is_zero(f)) {
362 return sign ? 1 << 3 : 1 << 4;
363 } else if (float64_is_zero_or_denormal(f)) {
364 return sign ? 1 << 2 : 1 << 5;
365 } else if (float64_is_any_nan(f)) {
366 float_status s = { }; /* for snan_bit_is_one */
367 return float64_is_quiet_nan(f, &s) ? 1 << 9 : 1 << 8;
368 } else {
369 return sign ? 1 << 1 : 1 << 6;