1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth * OpenRISC float helper routines
3fcf5ef2aSThomas Huth *
4fcf5ef2aSThomas Huth * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5fcf5ef2aSThomas Huth * Feng Gao <gf91597@gmail.com>
6fcf5ef2aSThomas Huth *
7fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either
10198a2d21SThomas Huth * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth *
12fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15fcf5ef2aSThomas Huth * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth *
17fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth */
20fcf5ef2aSThomas Huth
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23*9156ca76SStafford Horne #include "exec/exec-all.h"
24fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
2524f91e81SAlex Bennée #include "fpu/softfloat.h"
26fcf5ef2aSThomas Huth
ieee_ex_to_openrisc(int fexcp)274e2d3007SRichard Henderson static int ieee_ex_to_openrisc(int fexcp)
28fcf5ef2aSThomas Huth {
29fcf5ef2aSThomas Huth int ret = 0;
30fcf5ef2aSThomas Huth if (fexcp & float_flag_invalid) {
314e2d3007SRichard Henderson ret |= FPCSR_IVF;
32fcf5ef2aSThomas Huth }
33fcf5ef2aSThomas Huth if (fexcp & float_flag_overflow) {
344e2d3007SRichard Henderson ret |= FPCSR_OVF;
35fcf5ef2aSThomas Huth }
36fcf5ef2aSThomas Huth if (fexcp & float_flag_underflow) {
374e2d3007SRichard Henderson ret |= FPCSR_UNF;
38fcf5ef2aSThomas Huth }
39fcf5ef2aSThomas Huth if (fexcp & float_flag_divbyzero) {
404e2d3007SRichard Henderson ret |= FPCSR_DZF;
41fcf5ef2aSThomas Huth }
42fcf5ef2aSThomas Huth if (fexcp & float_flag_inexact) {
434e2d3007SRichard Henderson ret |= FPCSR_IXF;
44fcf5ef2aSThomas Huth }
45fcf5ef2aSThomas Huth return ret;
46fcf5ef2aSThomas Huth }
47fcf5ef2aSThomas Huth
48*9156ca76SStafford Horne static G_NORETURN
do_fpe(CPUOpenRISCState * env,uintptr_t pc)49*9156ca76SStafford Horne void do_fpe(CPUOpenRISCState *env, uintptr_t pc)
50*9156ca76SStafford Horne {
51*9156ca76SStafford Horne CPUState *cs = env_cpu(env);
52*9156ca76SStafford Horne
53*9156ca76SStafford Horne cs->exception_index = EXCP_FPE;
54*9156ca76SStafford Horne cpu_loop_exit_restore(cs, pc);
55*9156ca76SStafford Horne }
56*9156ca76SStafford Horne
HELPER(update_fpcsr)574e2d3007SRichard Henderson void HELPER(update_fpcsr)(CPUOpenRISCState *env)
58fcf5ef2aSThomas Huth {
594e2d3007SRichard Henderson int tmp = get_float_exception_flags(&env->fp_status);
60fcf5ef2aSThomas Huth
614e2d3007SRichard Henderson if (tmp) {
624e2d3007SRichard Henderson set_float_exception_flags(0, &env->fp_status);
634e2d3007SRichard Henderson tmp = ieee_ex_to_openrisc(tmp);
644e2d3007SRichard Henderson if (tmp) {
654e2d3007SRichard Henderson env->fpcsr |= tmp;
664e2d3007SRichard Henderson if (env->fpcsr & FPCSR_FPEE) {
67*9156ca76SStafford Horne do_fpe(env, GETPC());
684e2d3007SRichard Henderson }
694e2d3007SRichard Henderson }
70fcf5ef2aSThomas Huth }
71fcf5ef2aSThomas Huth }
72fcf5ef2aSThomas Huth
cpu_set_fpcsr(CPUOpenRISCState * env,uint32_t val)73a465772eSRichard Henderson void cpu_set_fpcsr(CPUOpenRISCState *env, uint32_t val)
74a465772eSRichard Henderson {
75a465772eSRichard Henderson static const int rm_to_sf[] = {
76a465772eSRichard Henderson float_round_nearest_even,
77a465772eSRichard Henderson float_round_to_zero,
78a465772eSRichard Henderson float_round_up,
79a465772eSRichard Henderson float_round_down
80a465772eSRichard Henderson };
81a465772eSRichard Henderson
8297a254b3SStafford Horne env->fpcsr = val & 0xfff;
83a465772eSRichard Henderson set_float_rounding_mode(rm_to_sf[extract32(val, 1, 2)], &env->fp_status);
84a465772eSRichard Henderson }
85a465772eSRichard Henderson
HELPER(itofd)86fcf5ef2aSThomas Huth uint64_t HELPER(itofd)(CPUOpenRISCState *env, uint64_t val)
87fcf5ef2aSThomas Huth {
8862f2b038SRichard Henderson return int64_to_float64(val, &env->fp_status);
89fcf5ef2aSThomas Huth }
90fcf5ef2aSThomas Huth
HELPER(itofs)91fcf5ef2aSThomas Huth uint32_t HELPER(itofs)(CPUOpenRISCState *env, uint32_t val)
92fcf5ef2aSThomas Huth {
934e2d3007SRichard Henderson return int32_to_float32(val, &env->fp_status);
94fcf5ef2aSThomas Huth }
95fcf5ef2aSThomas Huth
HELPER(ftoid)96fcf5ef2aSThomas Huth uint64_t HELPER(ftoid)(CPUOpenRISCState *env, uint64_t val)
97fcf5ef2aSThomas Huth {
9862f2b038SRichard Henderson return float64_to_int64_round_to_zero(val, &env->fp_status);
99fcf5ef2aSThomas Huth }
100fcf5ef2aSThomas Huth
HELPER(ftois)101fcf5ef2aSThomas Huth uint32_t HELPER(ftois)(CPUOpenRISCState *env, uint32_t val)
102fcf5ef2aSThomas Huth {
103091a3516SRichard Henderson return float32_to_int32_round_to_zero(val, &env->fp_status);
104fcf5ef2aSThomas Huth }
105fcf5ef2aSThomas Huth
HELPER(stod)10662f2b038SRichard Henderson uint64_t HELPER(stod)(CPUOpenRISCState *env, uint32_t val)
10762f2b038SRichard Henderson {
10862f2b038SRichard Henderson return float32_to_float64(val, &env->fp_status);
10962f2b038SRichard Henderson }
11062f2b038SRichard Henderson
HELPER(dtos)11162f2b038SRichard Henderson uint32_t HELPER(dtos)(CPUOpenRISCState *env, uint64_t val)
11262f2b038SRichard Henderson {
11362f2b038SRichard Henderson return float64_to_float32(val, &env->fp_status);
11462f2b038SRichard Henderson }
11562f2b038SRichard Henderson
116fcf5ef2aSThomas Huth #define FLOAT_CALC(name) \
117fcf5ef2aSThomas Huth uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
118fcf5ef2aSThomas Huth uint64_t fdt0, uint64_t fdt1) \
1194e2d3007SRichard Henderson { return float64_ ## name(fdt0, fdt1, &env->fp_status); } \
120fcf5ef2aSThomas Huth uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
121fcf5ef2aSThomas Huth uint32_t fdt0, uint32_t fdt1) \
1224e2d3007SRichard Henderson { return float32_ ## name(fdt0, fdt1, &env->fp_status); }
123fcf5ef2aSThomas Huth
124fcf5ef2aSThomas Huth FLOAT_CALC(add)
FLOAT_CALC(sub)125fcf5ef2aSThomas Huth FLOAT_CALC(sub)
126fcf5ef2aSThomas Huth FLOAT_CALC(mul)
127fcf5ef2aSThomas Huth FLOAT_CALC(div)
128fcf5ef2aSThomas Huth FLOAT_CALC(rem)
129fcf5ef2aSThomas Huth #undef FLOAT_CALC
130fcf5ef2aSThomas Huth
131762e22edSRichard Henderson
132762e22edSRichard Henderson uint64_t helper_float_madd_d(CPUOpenRISCState *env, uint64_t a,
133762e22edSRichard Henderson uint64_t b, uint64_t c)
134762e22edSRichard Henderson {
1354e2d3007SRichard Henderson /* Note that or1ksim doesn't use fused operation. */
1364e2d3007SRichard Henderson b = float64_mul(b, c, &env->fp_status);
1374e2d3007SRichard Henderson return float64_add(a, b, &env->fp_status);
138fcf5ef2aSThomas Huth }
139fcf5ef2aSThomas Huth
helper_float_madd_s(CPUOpenRISCState * env,uint32_t a,uint32_t b,uint32_t c)140762e22edSRichard Henderson uint32_t helper_float_madd_s(CPUOpenRISCState *env, uint32_t a,
141762e22edSRichard Henderson uint32_t b, uint32_t c)
142762e22edSRichard Henderson {
1434e2d3007SRichard Henderson /* Note that or1ksim doesn't use fused operation. */
1444e2d3007SRichard Henderson b = float32_mul(b, c, &env->fp_status);
1454e2d3007SRichard Henderson return float32_add(a, b, &env->fp_status);
146762e22edSRichard Henderson }
147fcf5ef2aSThomas Huth
148fcf5ef2aSThomas Huth
1494e2d3007SRichard Henderson #define FLOAT_CMP(name, impl) \
1504e2d3007SRichard Henderson target_ulong helper_float_ ## name ## _d(CPUOpenRISCState *env, \
151fcf5ef2aSThomas Huth uint64_t fdt0, uint64_t fdt1) \
1524e2d3007SRichard Henderson { return float64_ ## impl(fdt0, fdt1, &env->fp_status); } \
1534e2d3007SRichard Henderson target_ulong helper_float_ ## name ## _s(CPUOpenRISCState *env, \
154fcf5ef2aSThomas Huth uint32_t fdt0, uint32_t fdt1) \
1554e2d3007SRichard Henderson { return float32_ ## impl(fdt0, fdt1, &env->fp_status); }
156fcf5ef2aSThomas Huth
1574e2d3007SRichard Henderson FLOAT_CMP(le, le)
1584e2d3007SRichard Henderson FLOAT_CMP(lt, lt)
1594e2d3007SRichard Henderson FLOAT_CMP(eq, eq_quiet)
1602b13b4b9SRichard Henderson FLOAT_CMP(un, unordered_quiet)
161fcf5ef2aSThomas Huth #undef FLOAT_CMP
1622b13b4b9SRichard Henderson
1632b13b4b9SRichard Henderson #define FLOAT_UCMP(name, expr) \
1642b13b4b9SRichard Henderson target_ulong helper_float_ ## name ## _d(CPUOpenRISCState *env, \
1652b13b4b9SRichard Henderson uint64_t fdt0, uint64_t fdt1) \
1662b13b4b9SRichard Henderson { \
16771bfd65cSRichard Henderson FloatRelation r = float64_compare_quiet(fdt0, fdt1, &env->fp_status); \
1682b13b4b9SRichard Henderson return expr; \
1692b13b4b9SRichard Henderson } \
1702b13b4b9SRichard Henderson target_ulong helper_float_ ## name ## _s(CPUOpenRISCState *env, \
1712b13b4b9SRichard Henderson uint32_t fdt0, uint32_t fdt1) \
1722b13b4b9SRichard Henderson { \
17371bfd65cSRichard Henderson FloatRelation r = float32_compare_quiet(fdt0, fdt1, &env->fp_status); \
1742b13b4b9SRichard Henderson return expr; \
1752b13b4b9SRichard Henderson }
1762b13b4b9SRichard Henderson
1772b13b4b9SRichard Henderson FLOAT_UCMP(ueq, r == float_relation_equal || r == float_relation_unordered)
1782b13b4b9SRichard Henderson FLOAT_UCMP(ult, r == float_relation_less || r == float_relation_unordered)
1792b13b4b9SRichard Henderson FLOAT_UCMP(ule, r != float_relation_greater)
1802b13b4b9SRichard Henderson #undef FLOAT_UCMP
181