aboutsummaryrefslogtreecommitdiff
path: root/target/s390x/vec_fpu_helper.c
blob: 57b7fe4affce006e70e4009c5b10d1dcded95991 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * QEMU TCG support -- s390x vector floating point instruction support
 *
 * Copyright (C) 2019 Red Hat Inc
 *
 * Authors:
 *   David Hildenbrand <david@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "cpu.h"
#include "internal.h"
#include "vec.h"
#include "tcg_s390x.h"
#include "tcg/tcg-gvec-desc.h"
#include "exec/exec-all.h"
#include "exec/helper-proto.h"
#include "fpu/softfloat.h"

#define VIC_INVALID         0x1
#define VIC_DIVBYZERO       0x2
#define VIC_OVERFLOW        0x3
#define VIC_UNDERFLOW       0x4
#define VIC_INEXACT         0x5

/* returns the VEX. If the VEX is 0, there is no trap */
static uint8_t check_ieee_exc(CPUS390XState *env, uint8_t enr, bool XxC,
                              uint8_t *vec_exc)
{
    uint8_t vece_exc = 0, trap_exc;
    unsigned qemu_exc;

    /* Retrieve and clear the softfloat exceptions */
    qemu_exc = env->fpu_status.float_exception_flags;
    if (qemu_exc == 0) {
        return 0;
    }
    env->fpu_status.float_exception_flags = 0;

    vece_exc = s390_softfloat_exc_to_ieee(qemu_exc);

    /* Add them to the vector-wide s390x exception bits */
    *vec_exc |= vece_exc;

    /* Check for traps and construct the VXC */
    trap_exc = vece_exc & env->fpc >> 24;
    if (trap_exc) {
        if (trap_exc & S390_IEEE_MASK_INVALID) {
            return enr << 4 | VIC_INVALID;
        } else if (trap_exc & S390_IEEE_MASK_DIVBYZERO) {
            return enr << 4 | VIC_DIVBYZERO;
        } else if (trap_exc & S390_IEEE_MASK_OVERFLOW) {
            return enr << 4 | VIC_OVERFLOW;
        } else if (trap_exc & S390_IEEE_MASK_UNDERFLOW) {
            return enr << 4 | VIC_UNDERFLOW;
        } else if (!XxC) {
            g_assert(trap_exc & S390_IEEE_MASK_INEXACT);
            /* inexact has lowest priority on traps */
            return enr << 4 | VIC_INEXACT;
        }
    }
    return 0;
}

static void handle_ieee_exc(CPUS390XState *env, uint8_t vxc, uint8_t vec_exc,
                            uintptr_t retaddr)
{
    if (vxc) {
        /* on traps, the fpc flags are not updated, instruction is suppressed */
        tcg_s390_vector_exception(env, vxc, retaddr);
    }
    if (vec_exc) {
        /* indicate exceptions for all elements combined */
        env->fpc |= vec_exc << 16;
    }
}

typedef uint64_t (*vop64_2_fn)(uint64_t a, float_status *s);
static void vop64_2(S390Vector *v1, const S390Vector *v2, CPUS390XState *env,
                    bool s, bool XxC, uint8_t erm, vop64_2_fn fn,
                    uintptr_t retaddr)
{
    uint8_t vxc, vec_exc = 0;
    S390Vector tmp = {};
    int i, old_mode;

    old_mode = s390_swap_bfp_rounding_mode(env, erm);
    for (i = 0; i < 2; i++) {
        const uint64_t a = s390_vec_read_element64(v2, i);

        s390_vec_write_element64(&tmp, i, fn(a, &env->fpu_status));
        vxc = check_ieee_exc(env, i, XxC, &vec_exc);
        if (s || vxc) {
            break;
        }
    }
    s390_restore_bfp_rounding_mode(env, old_mode);
    handle_ieee_exc(env, vxc, vec_exc, retaddr);
    *v1 = tmp;
}

typedef uint64_t (*vop64_3_fn)(uint64_t a, uint64_t b, float_status *s);
static void vop64_3(S390Vector *v1, const S390Vector *v2, const S390Vector *v3,
                    CPUS390XState *env, bool s, vop64_3_fn fn,
                    uintptr_t retaddr)
{
    uint8_t vxc, vec_exc = 0;
    S390Vector tmp = {};
    int i;

    for (i = 0; i < 2; i++) {
        const uint64_t a = s390_vec_read_element64(v2, i);
        const uint64_t b = s390_vec_read_element64(v3, i);

        s390_vec_write_element64(&tmp, i, fn(a, b, &env->fpu_status));
        vxc = check_ieee_exc(env, i, false, &vec_exc);
        if (s || vxc) {
            break;
        }
    }
    handle_ieee_exc(env, vxc, vec_exc, retaddr);
    *v1 = tmp;
}

static uint64_t vfa64(uint64_t a, uint64_t b, float_status *s)
{
    return float64_add(a, b, s);
}

void HELPER(gvec_vfa64)(void *v1, const void *v2, const void *v3,
                        CPUS390XState *env, uint32_t desc)
{
    vop64_3(v1, v2, v3, env, false, vfa64, GETPC());
}

void HELPER(gvec_vfa64s)(void *v1, const void *v2, const void *v3,
                         CPUS390XState *env, uint32_t desc)
{
    vop64_3(v1, v2, v3, env, true, vfa64, GETPC());
}

static int wfc64(const S390Vector *v1, const S390Vector *v2,
                 CPUS390XState *env, bool signal, uintptr_t retaddr)
{
    /* only the zero-indexed elements are compared */
    const float64 a = s390_vec_read_element64(v1, 0);
    const float64 b = s390_vec_read_element64(v2, 0);
    uint8_t vxc, vec_exc = 0;
    int cmp;

    if (signal) {
        cmp = float64_compare(a, b, &env->fpu_status);
    } else {
        cmp = float64_compare_quiet(a, b, &env->fpu_status);
    }
    vxc = check_ieee_exc(env, 0, false, &vec_exc);
    handle_ieee_exc(env, vxc, vec_exc, retaddr);

    return float_comp_to_cc(env, cmp);
}

void HELPER(gvec_wfc64)(const void *v1, const void *v2, CPUS390XState *env,
                        uint32_t desc)
{
    env->cc_op = wfc64(v1, v2, env, false, GETPC());
}

void HELPER(gvec_wfk64)(const void *v1, const void *v2, CPUS390XState *env,
                        uint32_t desc)
{
    env->cc_op = wfc64(v1, v2, env, true, GETPC());
}

typedef int (*vfc64_fn)(float64 a, float64 b, float_status *status);
static int vfc64(S390Vector *v1, const S390Vector *v2, const S390Vector *v3,
                 CPUS390XState *env, bool s, vfc64_fn fn, uintptr_t retaddr)
{
    uint8_t vxc, vec_exc = 0;
    S390Vector tmp = {};
    int match = 0;
    int i;

    for (i = 0; i < 2; i++) {
        const float64 a = s390_vec_read_element64(v2, i);
        const float64 b = s390_vec_read_element64(v3, i);

        /* swap the order of the parameters, so we can use existing functions */
        if (fn(b, a, &env->fpu_status)) {
            match++;
            s390_vec_write_element64(&tmp, i, -1ull);
        }
        vxc = check_ieee_exc(env, i, false, &vec_exc);
        if (s || vxc) {
            break;
        }
    }

    handle_ieee_exc(env, vxc, vec_exc, retaddr);
    *v1 = tmp;
    if (match) {
        return s || match == 2 ? 0 : 1;
    }
    return 3;
}

void HELPER(gvec_vfce64)(void *v1, const void *v2, const void *v3,
                         CPUS390XState *env, uint32_t desc)
{
    vfc64(v1, v2, v3, env, false, float64_eq_quiet, GETPC());
}

void HELPER(gvec_vfce64s)(void *v1, const void *v2, const void *v3,
                          CPUS390XState *env, uint32_t desc)
{
    vfc64(v1, v2, v3, env, true, float64_eq_quiet, GETPC());
}

void HELPER(gvec_vfce64_cc)(void *v1, const void *v2, const void *v3,
                            CPUS390XState *env, uint32_t desc)
{
    env->cc_op = vfc64(v1, v2, v3, env, false, float64_eq_quiet, GETPC());
}

void HELPER(gvec_vfce64s_cc)(void *v1, const void *v2, const void *v3,
                            CPUS390XState *env, uint32_t desc)
{
    env->cc_op = vfc64(v1, v2, v3, env, true, float64_eq_quiet, GETPC());
}

void HELPER(gvec_vfch64)(void *v1, const void *v2, const void *v3,
                         CPUS390XState *env, uint32_t desc)
{
    vfc64(v1, v2, v3, env, false, float64_lt_quiet, GETPC());
}

void HELPER(gvec_vfch64s)(void *v1, const void *v2, const void *v3,
                          CPUS390XState *env, uint32_t desc)
{
    vfc64(v1, v2, v3, env, true, float64_lt_quiet, GETPC());
}

void HELPER(gvec_vfch64_cc)(void *v1, const void *v2, const void *v3,
                            CPUS390XState *env, uint32_t desc)
{
    env->cc_op = vfc64(v1, v2, v3, env, false, float64_lt_quiet, GETPC());
}

void HELPER(gvec_vfch64s_cc)(void *v1, const void *v2, const void *v3,
                             CPUS390XState *env, uint32_t desc)
{
    env->cc_op = vfc64(v1, v2, v3, env, true, float64_lt_quiet, GETPC());
}

void HELPER(gvec_vfche64)(void *v1, const void *v2, const void *v3,
                          CPUS390XState *env, uint32_t desc)
{
    vfc64(v1, v2, v3, env, false, float64_le_quiet, GETPC());
}

void HELPER(gvec_vfche64s)(void *v1, const void *v2, const void *v3,
                           CPUS390XState *env, uint32_t desc)
{
    vfc64(v1, v2, v3, env, true, float64_le_quiet, GETPC());
}

void HELPER(gvec_vfche64_cc)(void *v1, const void *v2, const void *v3,
                             CPUS390XState *env, uint32_t desc)
{
    env->cc_op = vfc64(v1, v2, v3, env, false, float64_le_quiet, GETPC());
}

void HELPER(gvec_vfche64s_cc)(void *v1, const void *v2, const void *v3,
                              CPUS390XState *env, uint32_t desc)
{
    env->cc_op = vfc64(v1, v2, v3, env, true, float64_le_quiet, GETPC());
}

static uint64_t vcdg64(uint64_t a, float_status *s)
{
    return int64_to_float64(a, s);
}

void HELPER(gvec_vcdg64)(void *v1, const void *v2, CPUS390XState *env,
                         uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, false, XxC, erm, vcdg64, GETPC());
}

void HELPER(gvec_vcdg64s)(void *v1, const void *v2, CPUS390XState *env,
                          uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, true, XxC, erm, vcdg64, GETPC());
}

static uint64_t vcdlg64(uint64_t a, float_status *s)
{
    return uint64_to_float64(a, s);
}

void HELPER(gvec_vcdlg64)(void *v1, const void *v2, CPUS390XState *env,
                          uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, false, XxC, erm, vcdlg64, GETPC());
}

void HELPER(gvec_vcdlg64s)(void *v1, const void *v2, CPUS390XState *env,
                           uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, true, XxC, erm, vcdlg64, GETPC());
}

static uint64_t vcgd64(uint64_t a, float_status *s)
{
    return float64_to_int64(a, s);
}

void HELPER(gvec_vcgd64)(void *v1, const void *v2, CPUS390XState *env,
                         uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, false, XxC, erm, vcgd64, GETPC());
}

void HELPER(gvec_vcgd64s)(void *v1, const void *v2, CPUS390XState *env,
                          uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, true, XxC, erm, vcgd64, GETPC());
}

static uint64_t vclgd64(uint64_t a, float_status *s)
{
    return float64_to_uint64(a, s);
}

void HELPER(gvec_vclgd64)(void *v1, const void *v2, CPUS390XState *env,
                          uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, false, XxC, erm, vclgd64, GETPC());
}

void HELPER(gvec_vclgd64s)(void *v1, const void *v2, CPUS390XState *env,
                           uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, true, XxC, erm, vclgd64, GETPC());
}

static uint64_t vfd64(uint64_t a, uint64_t b, float_status *s)
{
    return float64_div(a, b, s);
}

void HELPER(gvec_vfd64)(void *v1, const void *v2, const void *v3,
                        CPUS390XState *env, uint32_t desc)
{
    vop64_3(v1, v2, v3, env, false, vfd64, GETPC());
}

void HELPER(gvec_vfd64s)(void *v1, const void *v2, const void *v3,
                         CPUS390XState *env, uint32_t desc)
{
    vop64_3(v1, v2, v3, env, true, vfd64, GETPC());
}

static uint64_t vfi64(uint64_t a, float_status *s)
{
    return float64_round_to_int(a, s);
}

void HELPER(gvec_vfi64)(void *v1, const void *v2, CPUS390XState *env,
                        uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, false, XxC, erm, vfi64, GETPC());
}

void HELPER(gvec_vfi64s)(void *v1, const void *v2, CPUS390XState *env,
                         uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vop64_2(v1, v2, env, true, XxC, erm, vfi64, GETPC());
}

static void vfll32(S390Vector *v1, const S390Vector *v2, CPUS390XState *env,
                   bool s, uintptr_t retaddr)
{
    uint8_t vxc, vec_exc = 0;
    S390Vector tmp = {};
    int i;

    for (i = 0; i < 2; i++) {
        /* load from even element */
        const float32 a = s390_vec_read_element32(v2, i * 2);
        const uint64_t ret = float32_to_float64(a, &env->fpu_status);

        s390_vec_write_element64(&tmp, i, ret);
        /* indicate the source element */
        vxc = check_ieee_exc(env, i * 2, false, &vec_exc);
        if (s || vxc) {
            break;
        }
    }
    handle_ieee_exc(env, vxc, vec_exc, retaddr);
    *v1 = tmp;
}

void HELPER(gvec_vfll32)(void *v1, const void *v2, CPUS390XState *env,
                         uint32_t desc)
{
    vfll32(v1, v2, env, false, GETPC());
}

void HELPER(gvec_vfll32s)(void *v1, const void *v2, CPUS390XState *env,
                          uint32_t desc)
{
    vfll32(v1, v2, env, true, GETPC());
}

static void vflr64(S390Vector *v1, const S390Vector *v2, CPUS390XState *env,
                   bool s, bool XxC, uint8_t erm, uintptr_t retaddr)
{
    uint8_t vxc, vec_exc = 0;
    S390Vector tmp = {};
    int i, old_mode;

    old_mode = s390_swap_bfp_rounding_mode(env, erm);
    for (i = 0; i < 2; i++) {
        float64 a = s390_vec_read_element64(v2, i);
        uint32_t ret = float64_to_float32(a, &env->fpu_status);

        /* place at even element */
        s390_vec_write_element32(&tmp, i * 2, ret);
        /* indicate the source element */
        vxc = check_ieee_exc(env, i, XxC, &vec_exc);
        if (s || vxc) {
            break;
        }
    }
    s390_restore_bfp_rounding_mode(env, old_mode);
    handle_ieee_exc(env, vxc, vec_exc, retaddr);
    *v1 = tmp;
}

void HELPER(gvec_vflr64)(void *v1, const void *v2, CPUS390XState *env,
                         uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vflr64(v1, v2, env, false, XxC, erm, GETPC());
}

void HELPER(gvec_vflr64s)(void *v1, const void *v2, CPUS390XState *env,
                          uint32_t desc)
{
    const uint8_t erm = extract32(simd_data(desc), 4, 4);
    const bool XxC = extract32(simd_data(desc), 2, 1);

    vflr64(v1, v2, env, true, XxC, erm, GETPC());
}

static uint64_t vfm64(uint64_t a, uint64_t b, float_status *s)
{
    return float64_mul(a, b, s);
}

void HELPER(gvec_vfm64)(void *v1, const void *v2, const void *v3,
                        CPUS390XState *env, uint32_t desc)
{
    vop64_3(v1, v2, v3, env, false, vfm64, GETPC());
}

void HELPER(gvec_vfm64s)(void *v1, const void *v2, const void *v3,
                         CPUS390XState *env, uint32_t desc)
{
    vop64_3(v1, v2, v3, env, true, vfm64, GETPC());
}