2 * OpenRISC float helper routines
4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5 * Feng Gao <gf91597@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "exec/helper-proto.h"
24 #include "exception.h"
26 static inline uint32_t ieee_ex_to_openrisc(OpenRISCCPU
*cpu
, int fexcp
)
30 if (fexcp
& float_flag_invalid
) {
31 cpu
->env
.fpcsr
|= FPCSR_IVF
;
34 if (fexcp
& float_flag_overflow
) {
35 cpu
->env
.fpcsr
|= FPCSR_OVF
;
38 if (fexcp
& float_flag_underflow
) {
39 cpu
->env
.fpcsr
|= FPCSR_UNF
;
42 if (fexcp
& float_flag_divbyzero
) {
43 cpu
->env
.fpcsr
|= FPCSR_DZF
;
46 if (fexcp
& float_flag_inexact
) {
47 cpu
->env
.fpcsr
|= FPCSR_IXF
;
55 static inline void update_fpcsr(OpenRISCCPU
*cpu
)
57 int tmp
= ieee_ex_to_openrisc(cpu
,
58 get_float_exception_flags(&cpu
->env
.fp_status
));
60 SET_FP_CAUSE(cpu
->env
.fpcsr
, tmp
);
61 if ((GET_FP_ENABLE(cpu
->env
.fpcsr
) & tmp
) &&
62 (cpu
->env
.fpcsr
& FPCSR_FPEE
)) {
63 helper_exception(&cpu
->env
, EXCP_FPE
);
65 UPDATE_FP_FLAGS(cpu
->env
.fpcsr
, tmp
);
69 uint64_t HELPER(itofd
)(CPUOpenRISCState
*env
, uint64_t val
)
72 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
74 set_float_exception_flags(0, &cpu
->env
.fp_status
);
75 itofd
= int32_to_float64(val
, &cpu
->env
.fp_status
);
81 uint32_t HELPER(itofs
)(CPUOpenRISCState
*env
, uint32_t val
)
84 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
86 set_float_exception_flags(0, &cpu
->env
.fp_status
);
87 itofs
= int32_to_float32(val
, &cpu
->env
.fp_status
);
93 uint64_t HELPER(ftoid
)(CPUOpenRISCState
*env
, uint64_t val
)
96 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
98 set_float_exception_flags(0, &cpu
->env
.fp_status
);
99 ftoid
= float32_to_int64(val
, &cpu
->env
.fp_status
);
105 uint32_t HELPER(ftois
)(CPUOpenRISCState
*env
, uint32_t val
)
108 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
110 set_float_exception_flags(0, &cpu
->env
.fp_status
);
111 ftois
= float32_to_int32(val
, &cpu
->env
.fp_status
);
117 #define FLOAT_OP(name, p) void helper_float_##_##p(void)
119 #define FLOAT_CALC(name) \
120 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
121 uint64_t fdt0, uint64_t fdt1) \
124 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
125 set_float_exception_flags(0, &cpu->env.fp_status); \
126 result = float64_ ## name(fdt0, fdt1, &cpu->env.fp_status); \
131 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
132 uint32_t fdt0, uint32_t fdt1) \
135 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
136 set_float_exception_flags(0, &cpu->env.fp_status); \
137 result = float32_ ## name(fdt0, fdt1, &cpu->env.fp_status); \
150 uint64_t helper_float_madd_d(CPUOpenRISCState
*env
, uint64_t a
,
151 uint64_t b
, uint64_t c
)
153 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
155 set_float_exception_flags(0, &cpu
->env
.fp_status
);
156 /* Note that or1ksim doesn't use merged operation. */
157 result
= float64_mul(b
, c
, &cpu
->env
.fp_status
);
158 result
= float64_add(result
, a
, &cpu
->env
.fp_status
);
163 uint32_t helper_float_madd_s(CPUOpenRISCState
*env
, uint32_t a
,
164 uint32_t b
, uint32_t c
)
166 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
168 set_float_exception_flags(0, &cpu
->env
.fp_status
);
169 /* Note that or1ksim doesn't use merged operation. */
170 result
= float32_mul(b
, c
, &cpu
->env
.fp_status
);
171 result
= float32_add(result
, a
, &cpu
->env
.fp_status
);
177 #define FLOAT_CMP(name) \
178 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
179 uint64_t fdt0, uint64_t fdt1) \
182 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
183 set_float_exception_flags(0, &cpu->env.fp_status); \
184 res = float64_ ## name(fdt0, fdt1, &cpu->env.fp_status); \
189 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
190 uint32_t fdt0, uint32_t fdt1)\
193 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
194 set_float_exception_flags(0, &cpu->env.fp_status); \
195 res = float32_ ## name(fdt0, fdt1, &cpu->env.fp_status); \
206 #define FLOAT_CMPNE(name) \
207 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
208 uint64_t fdt0, uint64_t fdt1) \
211 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
212 set_float_exception_flags(0, &cpu->env.fp_status); \
213 res = !float64_eq_quiet(fdt0, fdt1, &cpu->env.fp_status); \
218 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
219 uint32_t fdt0, uint32_t fdt1) \
222 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
223 set_float_exception_flags(0, &cpu->env.fp_status); \
224 res = !float32_eq_quiet(fdt0, fdt1, &cpu->env.fp_status); \
232 #define FLOAT_CMPGT(name) \
233 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
234 uint64_t fdt0, uint64_t fdt1) \
237 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
238 set_float_exception_flags(0, &cpu->env.fp_status); \
239 res = !float64_le(fdt0, fdt1, &cpu->env.fp_status); \
244 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
245 uint32_t fdt0, uint32_t fdt1) \
248 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
249 set_float_exception_flags(0, &cpu->env.fp_status); \
250 res = !float32_le(fdt0, fdt1, &cpu->env.fp_status); \
257 #define FLOAT_CMPGE(name) \
258 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
259 uint64_t fdt0, uint64_t fdt1) \
262 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
263 set_float_exception_flags(0, &cpu->env.fp_status); \
264 res = !float64_lt(fdt0, fdt1, &cpu->env.fp_status); \
269 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
270 uint32_t fdt0, uint32_t fdt1) \
273 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
274 set_float_exception_flags(0, &cpu->env.fp_status); \
275 res = !float32_lt(fdt0, fdt1, &cpu->env.fp_status); \