aboutsummaryrefslogtreecommitdiff
path: root/math/test/rtest/main.c
blob: 3d533c946f79be126fc0b427a9578effed68179a (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
/*
 * main.c
 *
 * Copyright (c) 1999-2019, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

#include "intern.h"

void gencases(Testable *fn, int number);
void docase(Testable *fn, uint32 *args);
void vet_for_decline(Testable *fn, uint32 *args, uint32 *result, int got_errno_in);
void seed_random(uint32 seed);

int check_declines = 0;
int lib_fo = 0;
int lib_no_arith = 0;
int ntests = 0;

int nargs_(Testable* f) {
    switch((f)->type) {
    case args2:
    case args2f:
    case semi2:
    case semi2f:
    case t_ldexp:
    case t_ldexpf:
    case args1c:
    case args1fc:
    case args1cr:
    case args1fcr:
    case compare:
    case comparef:
        return 2;
    case args2c:
    case args2fc:
        return 4;
    default:
        return 1;
    }
}

static int isdouble(Testable *f)
{
    switch (f->type) {
      case args1:
      case rred:
      case semi1:
      case t_frexp:
      case t_modf:
      case classify:
      case t_ldexp:
      case args2:
      case semi2:
      case args1c:
      case args1cr:
      case compare:
      case args2c:
        return 1;
      case args1f:
      case rredf:
      case semi1f:
      case t_frexpf:
      case t_modff:
      case classifyf:
      case args2f:
      case semi2f:
      case t_ldexpf:
      case comparef:
      case args1fc:
      case args1fcr:
      case args2fc:
        return 0;
      default:
        assert(0 && "Bad function type");
    }
}

Testable *find_function(const char *func)
{
    int i;
    for (i = 0; i < nfunctions; i++) {
        if (func && !strcmp(func, functions[i].name)) {
            return &functions[i];
        }
    }
    return NULL;
}

void get_operand(const char *str, Testable *f, uint32 *word0, uint32 *word1)
{
    struct special {
        unsigned dblword0, dblword1, sglword;
        const char *name;
    } specials[] = {
        {0x00000000,0x00000000,0x00000000,"0"},
        {0x3FF00000,0x00000000,0x3f800000,"1"},
        {0x7FF00000,0x00000000,0x7f800000,"inf"},
        {0x7FF80000,0x00000001,0x7fc00000,"qnan"},
        {0x7FF00000,0x00000001,0x7f800001,"snan"},
        {0x3ff921fb,0x54442d18,0x3fc90fdb,"pi2"},
        {0x400921fb,0x54442d18,0x40490fdb,"pi"},
        {0x3fe921fb,0x54442d18,0x3f490fdb,"pi4"},
        {0x4002d97c,0x7f3321d2,0x4016cbe4,"3pi4"},
    };
    int i;

    for (i = 0; i < (int)(sizeof(specials)/sizeof(*specials)); i++) {
        if (!strcmp(str, specials[i].name) ||
            ((str[0] == '-' || str[0] == '+') &&
             !strcmp(str+1, specials[i].name))) {
            assert(f);
            if (isdouble(f)) {
                *word0 = specials[i].dblword0;
                *word1 = specials[i].dblword1;
            } else {
                *word0 = specials[i].sglword;
                *word1 = 0;
            }
            if (str[0] == '-')
                *word0 |= 0x80000000U;
            return;
        }
    }

    sscanf(str, "%"I32"x.%"I32"x", word0, word1);
}

void dofile(FILE *fp, int translating) {
    char buf[1024], sparebuf[1024], *p;

    /*
     * Command syntax is:
     *
     *  - "seed <integer>" sets a random seed
     *
     *  - "test <function> <ntests>" generates random test lines
     *
     *  - "<function> op1=foo [op2=bar]" generates a specific test
     *  - "func=<function> op1=foo [op2=bar]" does the same
     *  - "func=<function> op1=foo result=bar" will just output the line as-is
     *
     *  - a semicolon or a blank line is ignored
     */
    while (fgets(buf, sizeof(buf), fp)) {
        buf[strcspn(buf, "\r\n")] = '\0';
        strcpy(sparebuf, buf);
        p = buf;
        while (*p && isspace(*p)) p++;
        if (!*p || *p == ';') {
            /* Comment or blank line. Only print if `translating' is set. */
            if (translating)
                printf("%s\n", buf);
            continue;
        }
        if (!strncmp(buf, "seed ", 5)) {
            seed_random(atoi(buf+5));
        } else if (!strncmp(buf, "random=", 7)) {
            /*
             * Copy 'random=on' / 'random=off' lines unconditionally
             * to the output, so that random test failures can be
             * accumulated into a recent-failures-list file and
             * still identified as random-in-origin when re-run the
             * next day.
             */
            printf("%s\n", buf);
        } else if (!strncmp(buf, "test ", 5)) {
            char *p = buf+5;
            char *q;
            int ntests, i;
            q = p;
            while (*p && !isspace(*p)) p++;
            if (*p) *p++ = '\0';
            while (*p && isspace(*p)) p++;
            if (*p)
                ntests = atoi(p);
            else
                ntests = 100;          /* *shrug* */
            for (i = 0; i < nfunctions; i++) {
                if (!strcmp(q, functions[i].name)) {
                    gencases(&functions[i], ntests);
                    break;
                }
            }
            if (i == nfunctions) {
                fprintf(stderr, "unknown test `%s'\n", q);
            }
        } else {
            /*
             * Parse a specific test line.
             */
            uint32 ops[8], result[8];
            int got_op = 0; /* &1 for got_op1, &4 for got_op3 etc. */
            Testable *f = 0;
            char *q, *r;
            int got_result = 0, got_errno_in = 0;

            for (q = strtok(p, " \t"); q; q = strtok(NULL, " \t")) {
                r = strchr(q, '=');
                if (!r) {
                    f = find_function(q);
                } else {
                    *r++ = '\0';

                    if (!strcmp(q, "func"))
                        f = find_function(r);
                    else if (!strcmp(q, "op1") || !strcmp(q, "op1r")) {
                        get_operand(r, f, &ops[0], &ops[1]);
                        got_op |= 1;
                    } else if (!strcmp(q, "op2") || !strcmp(q, "op1i")) {
                        get_operand(r, f, &ops[2], &ops[3]);
                        got_op |= 2;
                    } else if (!strcmp(q, "op2r")) {
                        get_operand(r, f, &ops[4], &ops[5]);
                        got_op |= 4;
                    } else if (!strcmp(q, "op2i")) {
                        get_operand(r, f, &ops[6], &ops[7]);
                        got_op |= 8;
                    } else if (!strcmp(q, "result") || !strcmp(q, "resultr")) {
                        get_operand(r, f, &result[0], &result[1]);
                        got_result |= 1;
                    } else if (!strcmp(q, "resulti")) {
                        get_operand(r, f, &result[4], &result[5]);
                        got_result |= 2;
                    } else if (!strcmp(q, "res2")) {
                        get_operand(r, f, &result[2], &result[3]);
                        got_result |= 4;
                    } else if (!strcmp(q, "errno_in")) {
                        got_errno_in = 1;
                    }
                }
            }

            /*
             * Test cases already set up by the input are not
             * reprocessed by default, unlike the fplib tests. (This
             * is mostly for historical reasons, because we used to
             * use a very slow and incomplete internal reference
             * implementation; now our ref impl is MPFR/MPC it
             * probably wouldn't be such a bad idea, though we'd still
             * have to make sure all the special cases came out
             * right.) If translating==2 (corresponding to the -T
             * command-line option) then we regenerate everything
             * regardless.
             */
            if (got_result && translating < 2) {
                if (f)
                    vet_for_decline(f, ops, result, got_errno_in);
                puts(sparebuf);
                continue;
            }

            if (f && got_op==(1<<nargs_(f))-1) {
                /*
                 * And do it!
                 */
                docase(f, ops);
            }
        }
    }
}

int main(int argc, char **argv) {
    int errs = 0, opts = 1, files = 0, translating = 0;
    unsigned int seed = 1; /* in case no explicit seed provided */

    seed_random(seed);

    setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* stops incomplete lines being printed when out of time */

    while (--argc) {
        FILE *fp;
        char *p = *++argv;

        if (opts && *p == '-') {
            if(*(p+1) == 0) { /* single -, read from stdin */
                break;
            } else if (!strcmp(p, "-t")) {
                translating = 1;
            } else if (!strcmp(p, "-T")) {
                translating = 2;
            } else if (!strcmp(p, "-c")) {
                check_declines = 1;
            } else if (!strcmp(p, "--")) {
                opts = 0;
            } else if (!strcmp(p,"--seed") && argc > 1 && 1==sscanf(*(argv+1),"%u",&seed)) {
                seed_random(seed);
                argv++; /* next in argv is seed value, so skip */
                --argc;
            } else if (!strcmp(p, "-fo")) {
                lib_fo = 1;
            } else if (!strcmp(p, "-noarith")) {
                lib_no_arith = 1;
            } else {
                fprintf(stderr,
                        "rtest: ignoring unrecognised option '%s'\n", p);
                errs = 1;
            }
        } else {
            files = 1;
            if (!errs) {
                fp = fopen(p, "r");
                if (fp) {
                    dofile(fp, translating);
                    fclose(fp);
                } else {
                    perror(p);
                    errs = 1;
                }
            }
        }
    }

    /*
     * If no filename arguments, use stdin.
     */
    if (!files && !errs) {
        dofile(stdin, translating);
    }

    if (check_declines) {
        fprintf(stderr, "Tests expected to run: %d\n", ntests);
        fflush(stderr);
    }

    return errs;
}