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); \
149 #define FLOAT_TERNOP(name1, name2) \
150 uint64_t helper_float_ ## name1 ## name2 ## _d(CPUOpenRISCState *env, \
154 uint64_t result, temp, hi, lo; \
155 uint32_t val1, val2; \
156 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
157 hi = env->fpmaddhi; \
158 lo = env->fpmaddlo; \
159 set_float_exception_flags(0, &cpu->env.fp_status); \
160 result = float64_ ## name1(fdt0, fdt1, &cpu->env.fp_status); \
163 temp = (hi << 32) | lo; \
164 result = float64_ ## name2(result, temp, &cpu->env.fp_status); \
165 val1 = result >> 32; \
166 val2 = (uint32_t) (result & 0xffffffff); \
168 cpu->env.fpmaddlo = val2; \
169 cpu->env.fpmaddhi = val1; \
173 uint32_t helper_float_ ## name1 ## name2 ## _s(CPUOpenRISCState *env, \
174 uint32_t fdt0, uint32_t fdt1) \
176 uint64_t result, temp, hi, lo; \
177 uint32_t val1, val2; \
178 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
179 hi = cpu->env.fpmaddhi; \
180 lo = cpu->env.fpmaddlo; \
181 set_float_exception_flags(0, &cpu->env.fp_status); \
182 result = float64_ ## name1(fdt0, fdt1, &cpu->env.fp_status); \
183 temp = (hi << 32) | lo; \
184 result = float64_ ## name2(result, temp, &cpu->env.fp_status); \
185 val1 = result >> 32; \
186 val2 = (uint32_t) (result & 0xffffffff); \
188 cpu->env.fpmaddlo = val2; \
189 cpu->env.fpmaddhi = val1; \
193 FLOAT_TERNOP(mul
, add
)
197 #define FLOAT_CMP(name) \
198 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
199 uint64_t fdt0, uint64_t fdt1) \
202 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
203 set_float_exception_flags(0, &cpu->env.fp_status); \
204 res = float64_ ## name(fdt0, fdt1, &cpu->env.fp_status); \
209 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
210 uint32_t fdt0, uint32_t fdt1)\
213 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
214 set_float_exception_flags(0, &cpu->env.fp_status); \
215 res = float32_ ## name(fdt0, fdt1, &cpu->env.fp_status); \
226 #define FLOAT_CMPNE(name) \
227 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
228 uint64_t fdt0, uint64_t fdt1) \
231 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
232 set_float_exception_flags(0, &cpu->env.fp_status); \
233 res = !float64_eq_quiet(fdt0, fdt1, &cpu->env.fp_status); \
238 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
239 uint32_t fdt0, uint32_t fdt1) \
242 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
243 set_float_exception_flags(0, &cpu->env.fp_status); \
244 res = !float32_eq_quiet(fdt0, fdt1, &cpu->env.fp_status); \
252 #define FLOAT_CMPGT(name) \
253 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
254 uint64_t fdt0, uint64_t fdt1) \
257 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
258 set_float_exception_flags(0, &cpu->env.fp_status); \
259 res = !float64_le(fdt0, fdt1, &cpu->env.fp_status); \
264 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
265 uint32_t fdt0, uint32_t fdt1) \
268 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
269 set_float_exception_flags(0, &cpu->env.fp_status); \
270 res = !float32_le(fdt0, fdt1, &cpu->env.fp_status); \
277 #define FLOAT_CMPGE(name) \
278 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
279 uint64_t fdt0, uint64_t fdt1) \
282 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
283 set_float_exception_flags(0, &cpu->env.fp_status); \
284 res = !float64_lt(fdt0, fdt1, &cpu->env.fp_status); \
289 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
290 uint32_t fdt0, uint32_t fdt1) \
293 OpenRISCCPU *cpu = openrisc_env_get_cpu(env); \
294 set_float_exception_flags(0, &cpu->env.fp_status); \
295 res = !float32_lt(fdt0, fdt1, &cpu->env.fp_status); \