summaryrefslogtreecommitdiffstats
path: root/target-openrisc/fpu_helper.c
blob: c54404b80dce3e797f837274bf566ae8fa558e52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * OpenRISC float helper routines
 *
 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
 *                         Feng Gao <gf91597@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include "cpu.h"
#include "exec/helper-proto.h"
#include "exception.h"

static inline uint32_t ieee_ex_to_openrisc(OpenRISCCPU *cpu, int fexcp)
{
    int ret = 0;
    if (fexcp) {
        if (fexcp & float_flag_invalid) {
            cpu->env.fpcsr |= FPCSR_IVF;
            ret = 1;
        }
        if (fexcp & float_flag_overflow) {
            cpu->env.fpcsr |= FPCSR_OVF;
            ret = 1;
        }
        if (fexcp & float_flag_underflow) {
            cpu->env.fpcsr |= FPCSR_UNF;
            ret = 1;
        }
        if (fexcp & float_flag_divbyzero) {
            cpu->env.fpcsr |= FPCSR_DZF;
            ret = 1;
        }
        if (fexcp & float_flag_inexact) {
            cpu->env.fpcsr |= FPCSR_IXF;
            ret = 1;
        }
    }

    return ret;
}

static inline void update_fpcsr(OpenRISCCPU *cpu)
{
    int tmp = ieee_ex_to_openrisc(cpu,
                              get_float_exception_flags(&cpu->env.fp_status));

    SET_FP_CAUSE(cpu->env.fpcsr, tmp);
    if ((GET_FP_ENABLE(cpu->env.fpcsr) & tmp) &&
        (cpu->env.fpcsr & FPCSR_FPEE)) {
        helper_exception(&cpu->env, EXCP_FPE);
    } else {
        UPDATE_FP_FLAGS(cpu->env.fpcsr, tmp);
    }
}

uint64_t HELPER(itofd)(CPUOpenRISCState *env, uint64_t val)
{
    uint64_t itofd;
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);

    set_float_exception_flags(0, &cpu->env.fp_status);
    itofd = int32_to_float64(val, &cpu->env.fp_status);
    update_fpcsr(cpu);

    return itofd;
}

uint32_t HELPER(itofs)(CPUOpenRISCState *env, uint32_t val)
{
    uint32_t itofs;
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);

    set_float_exception_flags(0, &cpu->env.fp_status);
    itofs = int32_to_float32(val, &cpu->env.fp_status);
    update_fpcsr(cpu);

    return itofs;
}

uint64_t HELPER(ftoid)(CPUOpenRISCState *env, uint64_t val)
{
    uint64_t ftoid;
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);

    set_float_exception_flags(0, &cpu->env.fp_status);
    ftoid = float32_to_int64(val, &cpu->env.fp_status);
    update_fpcsr(cpu);

    return ftoid;
}

uint32_t HELPER(ftois)(CPUOpenRISCState *env, uint32_t val)
{
    uint32_t ftois;
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);

    set_float_exception_flags(0, &cpu->env.fp_status);
    ftois = float32_to_int32(val, &cpu->env.fp_status);
    update_fpcsr(cpu);

    return ftois;
}

#define FLOAT_OP(name, p) void helper_float_##_##p(void)

#define FLOAT_CALC(name)                                                  \
uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env,               \
                                     uint64_t fdt0, uint64_t fdt1)        \
{                                                                         \
    uint64_t result;                                                      \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    result = float64_ ## name(fdt0, fdt1, &cpu->env.fp_status);           \
    update_fpcsr(cpu);                                                    \
    return result;                                                        \
}                                                                         \
                                                                          \
uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env,               \
                                     uint32_t fdt0, uint32_t fdt1)        \
{                                                                         \
    uint32_t result;                                                      \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    result = float32_ ## name(fdt0, fdt1, &cpu->env.fp_status);           \
    update_fpcsr(cpu);                                                    \
    return result;                                                        \
}                                                                         \

FLOAT_CALC(add)
FLOAT_CALC(sub)
FLOAT_CALC(mul)
FLOAT_CALC(div)
FLOAT_CALC(rem)
#undef FLOAT_CALC

#define FLOAT_TERNOP(name1, name2)                                        \
uint64_t helper_float_ ## name1 ## name2 ## _d(CPUOpenRISCState *env,     \
                                               uint64_t fdt0,             \
                                               uint64_t fdt1)             \
{                                                                         \
    uint64_t result, temp, hi, lo;                                        \
    uint32_t val1, val2;                                                  \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    hi = env->fpmaddhi;                                                   \
    lo = env->fpmaddlo;                                                   \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    result = float64_ ## name1(fdt0, fdt1, &cpu->env.fp_status);          \
    lo &= 0xffffffff;                                                     \
    hi &= 0xffffffff;                                                     \
    temp = (hi << 32) | lo;                                               \
    result = float64_ ## name2(result, temp, &cpu->env.fp_status);        \
    val1 = result >> 32;                                                  \
    val2 = (uint32_t) (result & 0xffffffff);                              \
    update_fpcsr(cpu);                                                    \
    cpu->env.fpmaddlo = val2;                                             \
    cpu->env.fpmaddhi = val1;                                             \
    return 0;                                                             \
}                                                                         \
                                                                          \
uint32_t helper_float_ ## name1 ## name2 ## _s(CPUOpenRISCState *env,     \
                                            uint32_t fdt0, uint32_t fdt1) \
{                                                                         \
    uint64_t result, temp, hi, lo;                                        \
    uint32_t val1, val2;                                                  \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    hi = cpu->env.fpmaddhi;                                               \
    lo = cpu->env.fpmaddlo;                                               \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    result = float64_ ## name1(fdt0, fdt1, &cpu->env.fp_status);          \
    temp = (hi << 32) | lo;                                               \
    result = float64_ ## name2(result, temp, &cpu->env.fp_status);        \
    val1 = result >> 32;                                                  \
    val2 = (uint32_t) (result & 0xffffffff);                              \
    update_fpcsr(cpu);                                                    \
    cpu->env.fpmaddlo = val2;                                             \
    cpu->env.fpmaddhi = val1;                                             \
    return 0;                                                             \
}

FLOAT_TERNOP(mul, add)
#undef FLOAT_TERNOP


#define FLOAT_CMP(name)                                                   \
uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env,               \
                                     uint64_t fdt0, uint64_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = float64_ ## name(fdt0, fdt1, &cpu->env.fp_status);              \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}                                                                         \
                                                                          \
uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env,               \
                                             uint32_t fdt0, uint32_t fdt1)\
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = float32_ ## name(fdt0, fdt1, &cpu->env.fp_status);              \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}

FLOAT_CMP(le)
FLOAT_CMP(eq)
FLOAT_CMP(lt)
#undef FLOAT_CMP


#define FLOAT_CMPNE(name)                                                 \
uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env,               \
                                     uint64_t fdt0, uint64_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = !float64_eq_quiet(fdt0, fdt1, &cpu->env.fp_status);             \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}                                                                         \
                                                                          \
uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env,               \
                                     uint32_t fdt0, uint32_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = !float32_eq_quiet(fdt0, fdt1, &cpu->env.fp_status);             \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}

FLOAT_CMPNE(ne)
#undef FLOAT_CMPNE

#define FLOAT_CMPGT(name)                                                 \
uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env,               \
                                     uint64_t fdt0, uint64_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = !float64_le(fdt0, fdt1, &cpu->env.fp_status);                   \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}                                                                         \
                                                                          \
uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env,               \
                                     uint32_t fdt0, uint32_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = !float32_le(fdt0, fdt1, &cpu->env.fp_status);                   \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}
FLOAT_CMPGT(gt)
#undef FLOAT_CMPGT

#define FLOAT_CMPGE(name)                                                 \
uint64_t helper_float_ ## name ## _d(CPUOpenRISCState *env,               \
                                     uint64_t fdt0, uint64_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = !float64_lt(fdt0, fdt1, &cpu->env.fp_status);                   \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}                                                                         \
                                                                          \
uint32_t helper_float_ ## name ## _s(CPUOpenRISCState *env,               \
                                     uint32_t fdt0, uint32_t fdt1)        \
{                                                                         \
    int res;                                                              \
    OpenRISCCPU *cpu = openrisc_env_get_cpu(env);                         \
    set_float_exception_flags(0, &cpu->env.fp_status);                    \
    res = !float32_lt(fdt0, fdt1, &cpu->env.fp_status);                   \
    update_fpcsr(cpu);                                                    \
    return res;                                                           \
}

FLOAT_CMPGE(ge)
#undef FLOAT_CMPGE