blob: f9e34fa2cc26f2b85a12abb56ffb7a93ee5d4fc1 [file] [log] [blame]
Jia Liu5b569502012-07-20 15:50:44 +08001/*
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
Thomas Huth198a2d22019-02-13 14:46:50 +010010 * version 2.1 of the License, or (at your option) any later version.
Jia Liu5b569502012-07-20 15:50:44 +080011 *
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
Peter Maydelled2decc2016-01-26 18:17:22 +000021#include "qemu/osdep.h"
Jia Liu5b569502012-07-20 15:50:44 +080022#include "cpu.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070023#include "exec/helper-proto.h"
Jia Liu5b569502012-07-20 15:50:44 +080024#include "exception.h"
Alex Bennée24f91e82018-01-19 18:24:22 +000025#include "fpu/softfloat.h"
Jia Liu5b569502012-07-20 15:50:44 +080026
Richard Henderson4e2d3002015-02-18 20:40:38 -080027static int ieee_ex_to_openrisc(int fexcp)
Jia Liu5b569502012-07-20 15:50:44 +080028{
29 int ret = 0;
Richard Henderson4e2d3002015-02-18 20:40:38 -080030 if (fexcp & float_flag_invalid) {
31 ret |= FPCSR_IVF;
Jia Liu5b569502012-07-20 15:50:44 +080032 }
Richard Henderson4e2d3002015-02-18 20:40:38 -080033 if (fexcp & float_flag_overflow) {
34 ret |= FPCSR_OVF;
35 }
36 if (fexcp & float_flag_underflow) {
37 ret |= FPCSR_UNF;
38 }
39 if (fexcp & float_flag_divbyzero) {
40 ret |= FPCSR_DZF;
41 }
42 if (fexcp & float_flag_inexact) {
43 ret |= FPCSR_IXF;
44 }
Jia Liu5b569502012-07-20 15:50:44 +080045 return ret;
46}
47
Richard Henderson4e2d3002015-02-18 20:40:38 -080048void HELPER(update_fpcsr)(CPUOpenRISCState *env)
Jia Liu5b569502012-07-20 15:50:44 +080049{
Richard Henderson4e2d3002015-02-18 20:40:38 -080050 int tmp = get_float_exception_flags(&env->fp_status);
Jia Liu5b569502012-07-20 15:50:44 +080051
Richard Henderson4e2d3002015-02-18 20:40:38 -080052 if (tmp) {
53 set_float_exception_flags(0, &env->fp_status);
54 tmp = ieee_ex_to_openrisc(tmp);
55 if (tmp) {
56 env->fpcsr |= tmp;
57 if (env->fpcsr & FPCSR_FPEE) {
58 helper_exception(env, EXCP_FPE);
59 }
60 }
Jia Liu5b569502012-07-20 15:50:44 +080061 }
62}
63
Richard Hendersona4657722019-08-26 15:10:10 -070064void cpu_set_fpcsr(CPUOpenRISCState *env, uint32_t val)
65{
66 static const int rm_to_sf[] = {
67 float_round_nearest_even,
68 float_round_to_zero,
69 float_round_up,
70 float_round_down
71 };
72
Stafford Horne97a254b2020-01-11 06:28:43 +090073 env->fpcsr = val & 0xfff;
Richard Hendersona4657722019-08-26 15:10:10 -070074 set_float_rounding_mode(rm_to_sf[extract32(val, 1, 2)], &env->fp_status);
75}
76
Jia Liu5b569502012-07-20 15:50:44 +080077uint64_t HELPER(itofd)(CPUOpenRISCState *env, uint64_t val)
78{
Richard Henderson62f2b032019-05-06 14:49:25 -070079 return int64_to_float64(val, &env->fp_status);
Jia Liu5b569502012-07-20 15:50:44 +080080}
81
82uint32_t HELPER(itofs)(CPUOpenRISCState *env, uint32_t val)
83{
Richard Henderson4e2d3002015-02-18 20:40:38 -080084 return int32_to_float32(val, &env->fp_status);
Jia Liu5b569502012-07-20 15:50:44 +080085}
86
87uint64_t HELPER(ftoid)(CPUOpenRISCState *env, uint64_t val)
88{
Richard Henderson62f2b032019-05-06 14:49:25 -070089 return float64_to_int64_round_to_zero(val, &env->fp_status);
Jia Liu5b569502012-07-20 15:50:44 +080090}
91
92uint32_t HELPER(ftois)(CPUOpenRISCState *env, uint32_t val)
93{
Richard Henderson091a3512019-08-26 15:15:05 -070094 return float32_to_int32_round_to_zero(val, &env->fp_status);
Jia Liu5b569502012-07-20 15:50:44 +080095}
96
Richard Henderson62f2b032019-05-06 14:49:25 -070097uint64_t HELPER(stod)(CPUOpenRISCState *env, uint32_t val)
98{
99 return float32_to_float64(val, &env->fp_status);
100}
101
102uint32_t HELPER(dtos)(CPUOpenRISCState *env, uint64_t val)
103{
104 return float64_to_float32(val, &env->fp_status);
105}
106
Jia Liu5b569502012-07-20 15:50:44 +0800107#define FLOAT_CALC(name) \
108uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env, \
109 uint64_t fdt0, uint64_t fdt1) \
Richard Henderson4e2d3002015-02-18 20:40:38 -0800110{ return float64_ ## name(fdt0, fdt1, &env->fp_status); } \
Jia Liu5b569502012-07-20 15:50:44 +0800111uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env, \
112 uint32_t fdt0, uint32_t fdt1) \
Richard Henderson4e2d3002015-02-18 20:40:38 -0800113{ return float32_ ## name(fdt0, fdt1, &env->fp_status); }
Jia Liu5b569502012-07-20 15:50:44 +0800114
115FLOAT_CALC(add)
116FLOAT_CALC(sub)
117FLOAT_CALC(mul)
118FLOAT_CALC(div)
119FLOAT_CALC(rem)
120#undef FLOAT_CALC
121
Richard Henderson762e22e2015-02-18 18:47:35 -0800122
123uint64_t helper_float_madd_d(CPUOpenRISCState *env, uint64_t a,
124 uint64_t b, uint64_t c)
125{
Richard Henderson4e2d3002015-02-18 20:40:38 -0800126 /* Note that or1ksim doesn't use fused operation. */
127 b = float64_mul(b, c, &env->fp_status);
128 return float64_add(a, b, &env->fp_status);
Jia Liu5b569502012-07-20 15:50:44 +0800129}
130
Richard Henderson762e22e2015-02-18 18:47:35 -0800131uint32_t helper_float_madd_s(CPUOpenRISCState *env, uint32_t a,
132 uint32_t b, uint32_t c)
133{
Richard Henderson4e2d3002015-02-18 20:40:38 -0800134 /* Note that or1ksim doesn't use fused operation. */
135 b = float32_mul(b, c, &env->fp_status);
136 return float32_add(a, b, &env->fp_status);
Richard Henderson762e22e2015-02-18 18:47:35 -0800137}
Jia Liu5b569502012-07-20 15:50:44 +0800138
139
Richard Henderson4e2d3002015-02-18 20:40:38 -0800140#define FLOAT_CMP(name, impl) \
141target_ulong helper_float_ ## name ## _d(CPUOpenRISCState *env, \
142 uint64_t fdt0, uint64_t fdt1) \
143{ return float64_ ## impl(fdt0, fdt1, &env->fp_status); } \
144target_ulong helper_float_ ## name ## _s(CPUOpenRISCState *env, \
145 uint32_t fdt0, uint32_t fdt1) \
146{ return float32_ ## impl(fdt0, fdt1, &env->fp_status); }
Jia Liu5b569502012-07-20 15:50:44 +0800147
Richard Henderson4e2d3002015-02-18 20:40:38 -0800148FLOAT_CMP(le, le)
149FLOAT_CMP(lt, lt)
150FLOAT_CMP(eq, eq_quiet)
Richard Henderson2b13b4b2019-05-14 06:39:47 -0700151FLOAT_CMP(un, unordered_quiet)
Jia Liu5b569502012-07-20 15:50:44 +0800152#undef FLOAT_CMP
Richard Henderson2b13b4b2019-05-14 06:39:47 -0700153
154#define FLOAT_UCMP(name, expr) \
155target_ulong helper_float_ ## name ## _d(CPUOpenRISCState *env, \
156 uint64_t fdt0, uint64_t fdt1) \
157{ \
Richard Henderson71bfd652020-05-05 10:22:05 -0700158 FloatRelation r = float64_compare_quiet(fdt0, fdt1, &env->fp_status); \
Richard Henderson2b13b4b2019-05-14 06:39:47 -0700159 return expr; \
160} \
161target_ulong helper_float_ ## name ## _s(CPUOpenRISCState *env, \
162 uint32_t fdt0, uint32_t fdt1) \
163{ \
Richard Henderson71bfd652020-05-05 10:22:05 -0700164 FloatRelation r = float32_compare_quiet(fdt0, fdt1, &env->fp_status); \
Richard Henderson2b13b4b2019-05-14 06:39:47 -0700165 return expr; \
166}
167
168FLOAT_UCMP(ueq, r == float_relation_equal || r == float_relation_unordered)
169FLOAT_UCMP(ult, r == float_relation_less || r == float_relation_unordered)
170FLOAT_UCMP(ule, r != float_relation_greater)
171#undef FLOAT_UCMP