1 /*
2 * OpenRISC float helper routines
3 *
4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5 * Feng Gao <gf91597@gmail.com>
6 *
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.1 of the License, or (at your option) any later version.
11 *
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.
16 *
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/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "exec/helper-proto.h"
24 #include "fpu/softfloat.h"
25
ieee_ex_to_openrisc(int fexcp)26 static int ieee_ex_to_openrisc(int fexcp)
27 {
28 int ret = 0;
29 if (fexcp & float_flag_invalid) {
30 ret |= FPCSR_IVF;
31 }
32 if (fexcp & float_flag_overflow) {
33 ret |= FPCSR_OVF;
34 }
35 if (fexcp & float_flag_underflow) {
36 ret |= FPCSR_UNF;
37 }
38 if (fexcp & float_flag_divbyzero) {
39 ret |= FPCSR_DZF;
40 }
41 if (fexcp & float_flag_inexact) {
42 ret |= FPCSR_IXF;
43 }
44 return ret;
45 }
46
47 static G_NORETURN
do_fpe(CPUOpenRISCState * env,uintptr_t pc)48 void do_fpe(CPUOpenRISCState *env, uintptr_t pc)
49 {
50 CPUState *cs = env_cpu(env);
51
52 cs->exception_index = EXCP_FPE;
53 cpu_loop_exit_restore(cs, pc);
54 }
55
HELPER(update_fpcsr)56 void HELPER(update_fpcsr)(CPUOpenRISCState *env)
57 {
58 int tmp = get_float_exception_flags(&env->fp_status);
59
60 if (tmp) {
61 set_float_exception_flags(0, &env->fp_status);
62 tmp = ieee_ex_to_openrisc(tmp);
63 if (tmp) {
64 env->fpcsr |= tmp;
65 if (env->fpcsr & FPCSR_FPEE) {
66 do_fpe(env, GETPC());
67 }
68 }
69 }
70 }
71
cpu_set_fpcsr(CPUOpenRISCState * env,uint32_t val)72 void cpu_set_fpcsr(CPUOpenRISCState *env, uint32_t val)
73 {
74 static const int rm_to_sf[] = {
75 float_round_nearest_even,
76 float_round_to_zero,
77 float_round_up,
78 float_round_down
79 };
80
81 env->fpcsr = val & 0xfff;
82 set_float_rounding_mode(rm_to_sf[extract32(val, 1, 2)], &env->fp_status);
83 }
84
HELPER(itofd)85 uint64_t HELPER(itofd)(CPUOpenRISCState *env, uint64_t val)
86 {
87 return int64_to_float64(val, &env->fp_status);
88 }
89
HELPER(itofs)90 uint32_t HELPER(itofs)(CPUOpenRISCState *env, uint32_t val)
91 {
92 return int32_to_float32(val, &env->fp_status);
93 }
94
HELPER(ftoid)95 uint64_t HELPER(ftoid)(CPUOpenRISCState *env, uint64_t val)
96 {
97 return float64_to_int64_round_to_zero(val, &env->fp_status);
98 }
99
HELPER(ftois)100 uint32_t HELPER(ftois)(CPUOpenRISCState *env, uint32_t val)
101 {
102 return float32_to_int32_round_to_zero(val, &env->fp_status);
103 }
104
HELPER(stod)105 uint64_t HELPER(stod)(CPUOpenRISCState *env, uint32_t val)
106 {
107 return float32_to_float64(val, &env->fp_status);
108 }
109
HELPER(dtos)110 uint32_t HELPER(dtos)(CPUOpenRISCState *env, uint64_t val)
111 {
112 return float64_to_float32(val, &env->fp_status);
113 }
114
115 #define FLOAT_CALC(name) \
116 uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
117 uint64_t fdt0, uint64_t fdt1) \
118 { return float64_ ## name(fdt0, fdt1, &env->fp_status); } \
119 uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
120 uint32_t fdt0, uint32_t fdt1) \
121 { return float32_ ## name(fdt0, fdt1, &env->fp_status); }
122
123 FLOAT_CALC(add)
FLOAT_CALC(sub)124 FLOAT_CALC(sub)
125 FLOAT_CALC(mul)
126 FLOAT_CALC(div)
127 FLOAT_CALC(rem)
128 #undef FLOAT_CALC
129
130
131 uint64_t helper_float_madd_d(CPUOpenRISCState *env, uint64_t a,
132 uint64_t b, uint64_t c)
133 {
134 /* Note that or1ksim doesn't use fused operation. */
135 b = float64_mul(b, c, &env->fp_status);
136 return float64_add(a, b, &env->fp_status);
137 }
138
helper_float_madd_s(CPUOpenRISCState * env,uint32_t a,uint32_t b,uint32_t c)139 uint32_t helper_float_madd_s(CPUOpenRISCState *env, uint32_t a,
140 uint32_t b, uint32_t c)
141 {
142 /* Note that or1ksim doesn't use fused operation. */
143 b = float32_mul(b, c, &env->fp_status);
144 return float32_add(a, b, &env->fp_status);
145 }
146
147
148 #define FLOAT_CMP(name, impl) \
149 target_ulong helper_float_ ## name ## _d(CPUOpenRISCState *env, \
150 uint64_t fdt0, uint64_t fdt1) \
151 { return float64_ ## impl(fdt0, fdt1, &env->fp_status); } \
152 target_ulong helper_float_ ## name ## _s(CPUOpenRISCState *env, \
153 uint32_t fdt0, uint32_t fdt1) \
154 { return float32_ ## impl(fdt0, fdt1, &env->fp_status); }
155
156 FLOAT_CMP(le, le)
157 FLOAT_CMP(lt, lt)
158 FLOAT_CMP(eq, eq_quiet)
159 FLOAT_CMP(un, unordered_quiet)
160 #undef FLOAT_CMP
161
162 #define FLOAT_UCMP(name, expr) \
163 target_ulong helper_float_ ## name ## _d(CPUOpenRISCState *env, \
164 uint64_t fdt0, uint64_t fdt1) \
165 { \
166 FloatRelation r = float64_compare_quiet(fdt0, fdt1, &env->fp_status); \
167 return expr; \
168 } \
169 target_ulong helper_float_ ## name ## _s(CPUOpenRISCState *env, \
170 uint32_t fdt0, uint32_t fdt1) \
171 { \
172 FloatRelation r = float32_compare_quiet(fdt0, fdt1, &env->fp_status); \
173 return expr; \
174 }
175
176 FLOAT_UCMP(ueq, r == float_relation_equal || r == float_relation_unordered)
177 FLOAT_UCMP(ult, r == float_relation_less || r == float_relation_unordered)
178 FLOAT_UCMP(ule, r != float_relation_greater)
179 #undef FLOAT_UCMP
180