aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/s390x/fma.c
blob: 6872f59a7a6d5b0a2359841dfe84f69649458449 (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
/*
 * Test floating-point multiply-and-add instructions.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include <fenv.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "float.h"

union val {
    float e;
    double d;
    long double x;
    char buf[16];
};

/*
 * PoP tables as close to the original as possible.
 */
static const char *table1[N_SIGNED_CLASSES][N_SIGNED_CLASSES] = {
     /*         -inf           -Fn          -0             +0             +Fn          +inf           QNaN         SNaN     */
    {/* -inf */ "P(+inf)",     "P(+inf)",   "Xi: T(dNaN)", "Xi: T(dNaN)", "P(-inf)",   "P(-inf)",     "P(b)",      "Xi: T(b*)"},
    {/* -Fn  */ "P(+inf)",     "P(a*b)",    "P(+0)",       "P(-0)",       "P(a*b)",    "P(-inf)",     "P(b)",      "Xi: T(b*)"},
    {/* -0   */ "Xi: T(dNaN)", "P(+0)",     "P(+0)",       "P(-0)",       "P(-0)",     "Xi: T(dNaN)", "P(b)",      "Xi: T(b*)"},
    {/* +0   */ "Xi: T(dNaN)", "P(-0)",     "P(-0)",       "P(+0)",       "P(+0)",     "Xi: T(dNaN)", "P(b)",      "Xi: T(b*)"},
    {/* +Fn  */ "P(-inf)",     "P(a*b)",    "P(-0)",       "P(+0)",       "P(a*b)",    "P(+inf)",     "P(b)",      "Xi: T(b*)"},
    {/* +inf */ "P(-inf)",     "P(-inf)",   "Xi: T(dNaN)", "Xi: T(dNaN)", "P(+inf)",   "P(+inf)",     "P(b)",      "Xi: T(b*)"},
    {/* QNaN */ "P(a)",        "P(a)",      "P(a)",        "P(a)",        "P(a)",      "P(a)",        "P(a)",      "Xi: T(b*)"},
    {/* SNaN */ "Xi: T(a*)",   "Xi: T(a*)", "Xi: T(a*)",   "Xi: T(a*)",   "Xi: T(a*)", "Xi: T(a*)",   "Xi: T(a*)", "Xi: T(a*)"},
};

static const char *table2[N_SIGNED_CLASSES][N_SIGNED_CLASSES] = {
     /*         -inf           -Fn        -0         +0         +Fn        +inf           QNaN    SNaN     */
    {/* -inf */ "T(-inf)",     "T(-inf)", "T(-inf)", "T(-inf)", "T(-inf)", "Xi: T(dNaN)", "T(c)", "Xi: T(c*)"},
    {/* -Fn  */ "T(-inf)",     "R(p+c)",  "R(p)",    "R(p)",    "R(p+c)",  "T(+inf)",     "T(c)", "Xi: T(c*)"},
    {/* -0   */ "T(-inf)",     "R(c)",    "T(-0)",   "Rezd",    "R(c)",    "T(+inf)",     "T(c)", "Xi: T(c*)"},
    {/* +0   */ "T(-inf)",     "R(c)",    "Rezd",    "T(+0)",   "R(c)",    "T(+inf)",     "T(c)", "Xi: T(c*)"},
    {/* +Fn  */ "T(-inf)",     "R(p+c)",  "R(p)",    "R(p)",    "R(p+c)",  "T(+inf)",     "T(c)", "Xi: T(c*)"},
    {/* +inf */ "Xi: T(dNaN)", "T(+inf)", "T(+inf)", "T(+inf)", "T(+inf)", "T(+inf)",     "T(c)", "Xi: T(c*)"},
    {/* QNaN */ "T(p)",        "T(p)",    "T(p)",    "T(p)",    "T(p)",    "T(p)",        "T(p)", "Xi: T(c*)"},
     /* SNaN: can't happen */
};

static void interpret_tables(union val *r, bool *xi, int fmt,
                             int cls_a, const union val *a,
                             int cls_b, const union val *b,
                             int cls_c, const union val *c)
{
    const char *spec1 = table1[cls_a][cls_b];
    const char *spec2;
    union val p;
    int cls_p;

    *xi = false;

    if (strcmp(spec1, "P(-inf)") == 0) {
        cls_p = CLASS_MINUS_INF;
    } else if (strcmp(spec1, "P(+inf)") == 0) {
        cls_p = CLASS_PLUS_INF;
    } else if (strcmp(spec1, "P(-0)") == 0) {
        cls_p = CLASS_MINUS_ZERO;
    } else if (strcmp(spec1, "P(+0)") == 0) {
        cls_p = CLASS_PLUS_ZERO;
    } else if (strcmp(spec1, "P(a)") == 0) {
        cls_p = cls_a;
        memcpy(&p, a, sizeof(p));
    } else if (strcmp(spec1, "P(b)") == 0) {
        cls_p = cls_b;
        memcpy(&p, b, sizeof(p));
    } else if (strcmp(spec1, "P(a*b)") == 0) {
        /*
         * In the general case splitting fma into multiplication and addition
         * doesn't work, but this is the case with our test inputs.
         */
        cls_p = cls_a == cls_b ? CLASS_PLUS_FN : CLASS_MINUS_FN;
        switch (fmt) {
        case 0:
            p.e = a->e * b->e;
            break;
        case 1:
            p.d = a->d * b->d;
            break;
        case 2:
            p.x = a->x * b->x;
            break;
        default:
            fprintf(stderr, "Unsupported fmt: %d\n", fmt);
            exit(1);
        }
    } else if (strcmp(spec1, "Xi: T(dNaN)") == 0) {
        memcpy(r, default_nans[fmt], sizeof(*r));
        *xi = true;
        return;
    } else if (strcmp(spec1, "Xi: T(a*)") == 0) {
        memcpy(r, a, sizeof(*r));
        snan_to_qnan(r->buf, fmt);
        *xi = true;
        return;
    } else if (strcmp(spec1, "Xi: T(b*)") == 0) {
        memcpy(r, b, sizeof(*r));
        snan_to_qnan(r->buf, fmt);
        *xi = true;
        return;
    } else {
        fprintf(stderr, "Unsupported spec1: %s\n", spec1);
        exit(1);
    }

    spec2 = table2[cls_p][cls_c];
    if (strcmp(spec2, "T(-inf)") == 0) {
        memcpy(r, signed_floats[fmt][CLASS_MINUS_INF].v[0], sizeof(*r));
    } else if (strcmp(spec2, "T(+inf)") == 0) {
        memcpy(r, signed_floats[fmt][CLASS_PLUS_INF].v[0], sizeof(*r));
    } else if (strcmp(spec2, "T(-0)") == 0) {
        memcpy(r, signed_floats[fmt][CLASS_MINUS_ZERO].v[0], sizeof(*r));
    } else if (strcmp(spec2, "T(+0)") == 0 || strcmp(spec2, "Rezd") == 0) {
        memcpy(r, signed_floats[fmt][CLASS_PLUS_ZERO].v[0], sizeof(*r));
    } else if (strcmp(spec2, "R(c)") == 0 || strcmp(spec2, "T(c)") == 0) {
        memcpy(r, c, sizeof(*r));
    } else if (strcmp(spec2, "R(p)") == 0 || strcmp(spec2, "T(p)") == 0) {
        memcpy(r, &p, sizeof(*r));
    } else if (strcmp(spec2, "R(p+c)") == 0 || strcmp(spec2, "T(p+c)") == 0) {
        switch (fmt) {
        case 0:
            r->e = p.e + c->e;
            break;
        case 1:
            r->d = p.d + c->d;
            break;
        case 2:
            r->x = p.x + c->x;
            break;
        default:
            fprintf(stderr, "Unsupported fmt: %d\n", fmt);
            exit(1);
        }
    } else if (strcmp(spec2, "Xi: T(dNaN)") == 0) {
        memcpy(r, default_nans[fmt], sizeof(*r));
        *xi = true;
    } else if (strcmp(spec2, "Xi: T(c*)") == 0) {
        memcpy(r, c, sizeof(*r));
        snan_to_qnan(r->buf, fmt);
        *xi = true;
    } else {
        fprintf(stderr, "Unsupported spec2: %s\n", spec2);
        exit(1);
    }
}

struct iter {
    int fmt;
    int cls[3];
    int val[3];
};

static bool iter_next(struct iter *it)
{
    int i;

    for (i = 2; i >= 0; i--) {
        if (++it->val[i] != signed_floats[it->fmt][it->cls[i]].n) {
            return true;
        }
        it->val[i] = 0;

        if (++it->cls[i] != N_SIGNED_CLASSES) {
            return true;
        }
        it->cls[i] = 0;
    }

    return ++it->fmt != N_FORMATS;
}

int main(void)
{
    int ret = EXIT_SUCCESS;
    struct iter it = {};

    do {
        size_t n = float_sizes[it.fmt];
        union val a, b, c, exp, res;
        bool xi_exp, xi;

        memcpy(&a, signed_floats[it.fmt][it.cls[0]].v[it.val[0]], sizeof(a));
        memcpy(&b, signed_floats[it.fmt][it.cls[1]].v[it.val[1]], sizeof(b));
        memcpy(&c, signed_floats[it.fmt][it.cls[2]].v[it.val[2]], sizeof(c));

        interpret_tables(&exp, &xi_exp, it.fmt,
                         it.cls[1], &b, it.cls[2], &c, it.cls[0], &a);

        memcpy(&res, &a, sizeof(res));
        feclearexcept(FE_ALL_EXCEPT);
        switch (it.fmt) {
        case 0:
            asm("maebr %[a],%[b],%[c]"
                : [a] "+f" (res.e) : [b] "f" (b.e), [c] "f" (c.e));
            break;
        case 1:
            asm("madbr %[a],%[b],%[c]"
                : [a] "+f" (res.d) : [b] "f" (b.d), [c] "f" (c.d));
            break;
        case 2:
            asm("wfmaxb %[a],%[c],%[b],%[a]"
                : [a] "+v" (res.x) : [b] "v" (b.x), [c] "v" (c.x));
            break;
        default:
            fprintf(stderr, "Unsupported fmt: %d\n", it.fmt);
            exit(1);
        }
        xi = fetestexcept(FE_ALL_EXCEPT) == FE_INVALID;

        if (memcmp(&res, &exp, n) != 0 || xi != xi_exp) {
            fprintf(stderr, "[  FAILED  ] ");
            dump_v(stderr, &b, n);
            fprintf(stderr, " * ");
            dump_v(stderr, &c, n);
            fprintf(stderr, " + ");
            dump_v(stderr, &a, n);
            fprintf(stderr, ": actual=");
            dump_v(stderr, &res, n);
            fprintf(stderr, "/%d, expected=", (int)xi);
            dump_v(stderr, &exp, n);
            fprintf(stderr, "/%d\n", (int)xi_exp);
            ret = EXIT_FAILURE;
        }
    } while (iter_next(&it));

    return ret;
}