summaryrefslogtreecommitdiffstats
path: root/tests/tcg/arm/fcvt.c
blob: 7ac47b564e24ffc3113af9f29e7b1c884a78e118 (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
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
/*
 * Test Floating Point Conversion
 */

/* we want additional float type definitions */
#define __STDC_WANT_IEC_60559_BFP_EXT__
#define __STDC_WANT_IEC_60559_TYPES_EXT__

#include <stdio.h>
#include <inttypes.h>
#include <math.h>
#include <float.h>
#include <fenv.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

static char flag_str[256];

static char *get_flag_state(int flags)
{
    if (flags) {
        snprintf(flag_str, sizeof(flag_str), "%s %s %s %s %s",
                 flags & FE_OVERFLOW ? "OVERFLOW" : "",
                 flags & FE_UNDERFLOW ? "UNDERFLOW" : "",
                 flags & FE_DIVBYZERO ? "DIV0" : "",
                 flags & FE_INEXACT ? "INEXACT" : "",
                 flags & FE_INVALID ? "INVALID" : "");
    } else {
        snprintf(flag_str, sizeof(flag_str), "OK");
    }

    return flag_str;
}

static void print_double_number(int i, double num)
{
    uint64_t double_as_hex = *(uint64_t *) &num;
    int flags = fetestexcept(FE_ALL_EXCEPT);
    char *fstr = get_flag_state(flags);

    printf("%02d DOUBLE: %02.20e / %#020" PRIx64 " (%#x => %s)\n",
           i, num, double_as_hex, flags, fstr);
}

static void print_single_number(int i, float num)
{
    uint32_t single_as_hex = *(uint32_t *) &num;
    int flags = fetestexcept(FE_ALL_EXCEPT);
    char *fstr = get_flag_state(flags);

    printf("%02d SINGLE: %02.20e / %#010x  (%#x => %s)\n",
           i, num, single_as_hex, flags, fstr);
}

static void print_half_number(int i, uint16_t num)
{
    int flags = fetestexcept(FE_ALL_EXCEPT);
    char *fstr = get_flag_state(flags);

    printf("%02d   HALF: %#04x  (%#x => %s)\n",
           i, num, flags, fstr);
}

static void print_int64(int i, int64_t num)
{
    uint64_t int64_as_hex = *(uint64_t *) &num;
    int flags = fetestexcept(FE_ALL_EXCEPT);
    char *fstr = get_flag_state(flags);

    printf("%02d   INT64: %20" PRId64 "/%#020" PRIx64 " (%#x => %s)\n",
           i, num, int64_as_hex, flags, fstr);
}

#ifndef SNANF
/* Signaling NaN macros, if supported.  */
# define SNANF (__builtin_nansf (""))
# define SNAN (__builtin_nans (""))
# define SNANL (__builtin_nansl (""))
#endif

float single_numbers[] = { -SNANF,
                           -NAN,
                           -INFINITY,
                           -FLT_MAX,
                           -1.111E+31,
                           -1.111E+30,
                           -1.08700982e-12,
                           -1.78051176e-20,
                           -FLT_MIN,
                           0.0,
                           FLT_MIN,
                           2.98023224e-08,
                           5.96046E-8, /* min positive FP16 subnormal */
                           6.09756E-5, /* max subnormal FP16 */
                           6.10352E-5, /* min positive normal FP16 */
                           1.0,
                           1.0009765625, /* smallest float after 1.0 FP16 */
                           2.0,
                           M_E, M_PI,
                           65503.0,
                           65504.0, /* max FP16 */
                           65505.0,
                           131007.0,
                           131008.0, /* max AFP */
                           131009.0,
                           1.111E+30,
                           FLT_MAX,
                           INFINITY,
                           NAN,
                           SNANF };

static void convert_single_to_half(void)
{
    int i;

    printf("Converting single-precision to half-precision\n");

    for (i = 0; i < ARRAY_SIZE(single_numbers); ++i) {
        float input = single_numbers[i];

        feclearexcept(FE_ALL_EXCEPT);

        print_single_number(i, input);
#if defined(__arm__)
        uint32_t output;
        asm("vcvtb.f16.f32 %0, %1" : "=t" (output) : "x" (input));
#else
        uint16_t output;
        asm("fcvt %h0, %s1" : "=w" (output) : "x" (input));
#endif
        print_half_number(i, output);
    }
}

static void convert_single_to_double(void)
{
    int i;

    printf("Converting single-precision to double-precision\n");

    for (i = 0; i < ARRAY_SIZE(single_numbers); ++i) {
        float input = single_numbers[i];
        /* uint64_t output; */
        double output;

        feclearexcept(FE_ALL_EXCEPT);

        print_single_number(i, input);
#if defined(__arm__)
        asm("vcvt.f64.f32 %P0, %1" : "=w" (output) : "t" (input));
#else
        asm("fcvt %d0, %s1" : "=w" (output) : "x" (input));
#endif
        print_double_number(i, output);
    }
}

static void convert_single_to_integer(void)
{
    int i;

    printf("Converting single-precision to integer\n");

    for (i = 0; i < ARRAY_SIZE(single_numbers); ++i) {
        float input = single_numbers[i];
        int64_t output;

        feclearexcept(FE_ALL_EXCEPT);

        print_single_number(i, input);
#if defined(__arm__)
        /* asm("vcvt.s32.f32 %s0, %s1" : "=t" (output) : "t" (input)); */
        output = input;
#else
        asm("fcvtzs %0, %s1" : "=r" (output) : "w" (input));
#endif
        print_int64(i, output);
    }
}

/* This allows us to initialise some doubles as pure hex */
typedef union {
    double d;
    uint64_t h;
} test_doubles;

test_doubles double_numbers[] = {
    {SNAN},
    {-NAN},
    {-INFINITY},
    {-DBL_MAX},
    {-FLT_MAX-1.0},
    {-FLT_MAX},
    {-1.111E+31},
    {-1.111E+30}, /* half prec */
    {-2.0}, {-1.0},
    {-DBL_MIN},
    {-FLT_MIN},
    {0.0},
    {FLT_MIN},
    {2.98023224e-08},
    {5.96046E-8}, /* min positive FP16 subnormal */
    {6.09756E-5}, /* max subnormal FP16 */
    {6.10352E-5}, /* min positive normal FP16 */
    {1.0},
    {1.0009765625}, /* smallest float after 1.0 FP16 */
    {DBL_MIN},
    {1.3789972848607228e-308},
    {1.4914738736681624e-308},
    {1.0}, {2.0},
    {M_E}, {M_PI},
    {65503.0},
    {65504.0}, /* max FP16 */
    {65505.0},
    {131007.0},
    {131008.0}, /* max AFP */
    {131009.0},
    {.h = 0x41dfffffffc00000 }, /* to int = 0x7fffffff */
    {FLT_MAX},
    {FLT_MAX + 1.0},
    {DBL_MAX},
    {INFINITY},
    {NAN},
    {.h = 0x7ff0000000000001}, /* SNAN */
    {SNAN},
};

static void convert_double_to_half(void)
{
    int i;

    printf("Converting double-precision to half-precision\n");

    for (i = 0; i < ARRAY_SIZE(double_numbers); ++i) {
        double input = double_numbers[i].d;
        uint16_t output;

        feclearexcept(FE_ALL_EXCEPT);

        print_double_number(i, input);

        /* as we don't have _Float16 support */
#if defined(__arm__)
        /* asm("vcvtb.f16.f64 %0, %P1" : "=t" (output) : "x" (input)); */
        output = input;
#else
        asm("fcvt %h0, %d1" : "=w" (output) : "x" (input));
#endif
        print_half_number(i, output);
    }
}

static void convert_double_to_single(void)
{
    int i;

    printf("Converting double-precision to single-precision\n");

    for (i = 0; i < ARRAY_SIZE(double_numbers); ++i) {
        double input = double_numbers[i].d;
        uint32_t output;

        feclearexcept(FE_ALL_EXCEPT);

        print_double_number(i, input);

#if defined(__arm__)
        asm("vcvt.f32.f64 %0, %P1" : "=w" (output) : "x" (input));
#else
        asm("fcvt %s0, %d1" : "=w" (output) : "x" (input));
#endif

        print_single_number(i, output);
    }
}

static void convert_double_to_integer(void)
{
    int i;

    printf("Converting double-precision to integer\n");

    for (i = 0; i < ARRAY_SIZE(double_numbers); ++i) {
        double input = double_numbers[i].d;
        int64_t output;

        feclearexcept(FE_ALL_EXCEPT);

        print_double_number(i, input);
#if defined(__arm__)
        /* asm("vcvt.s32.f32 %s0, %s1" : "=t" (output) : "t" (input)); */
        output = input;
#else
        asm("fcvtzs %0, %d1" : "=r" (output) : "w" (input));
#endif
        print_int64(i, output);
    }
}

/* no handy defines for these numbers */
uint16_t half_numbers[] = {
    0xffff, /* -NaN / AHP -Max */
    0xfcff, /* -NaN / AHP */
    0xfc01, /* -NaN / AHP */
    0xfc00, /* -Inf */
    0xfbff, /* -Max */
    0xc000, /* -2 */
    0xbc00, /* -1 */
    0x8001, /* -MIN subnormal */
    0x8000, /* -0 */
    0x0000, /* +0 */
    0x0001, /* MIN subnormal */
    0x3c00, /* 1 */
    0x7bff, /* Max */
    0x7c00, /* Inf */
    0x7c01, /* NaN / AHP */
    0x7cff, /* NaN / AHP */
    0x7fff, /* NaN / AHP +Max*/
};

static void convert_half_to_double(void)
{
    int i;

    printf("Converting half-precision to double-precision\n");

    for (i = 0; i < ARRAY_SIZE(half_numbers); ++i) {
        uint16_t input = half_numbers[i];
        double output;

        feclearexcept(FE_ALL_EXCEPT);

        print_half_number(i, input);
#if defined(__arm__)
        /* asm("vcvtb.f64.f16 %P0, %1" : "=w" (output) : "t" (input)); */
        output = input;
#else
        asm("fcvt %d0, %h1" : "=w" (output) : "x" (input));
#endif
        print_double_number(i, output);
    }
}

static void convert_half_to_single(void)
{
    int i;

    printf("Converting half-precision to single-precision\n");

    for (i = 0; i < ARRAY_SIZE(half_numbers); ++i) {
        uint16_t input = half_numbers[i];
        float output;

        feclearexcept(FE_ALL_EXCEPT);

        print_half_number(i, input);
#if defined(__arm__)
        asm("vcvtb.f32.f16 %0, %1" : "=w" (output) : "x" ((uint32_t)input));
#else
        asm("fcvt %s0, %h1" : "=w" (output) : "x" (input));
#endif
        print_single_number(i, output);
    }
}

static void convert_half_to_integer(void)
{
    int i;

    printf("Converting half-precision to integer\n");

    for (i = 0; i < ARRAY_SIZE(half_numbers); ++i) {
        uint16_t input = half_numbers[i];
        int64_t output;

        feclearexcept(FE_ALL_EXCEPT);

        print_half_number(i, input);
#if defined(__arm__)
        /* asm("vcvt.s32.f16 %0, %1" : "=t" (output) : "t" (input)); v8.2*/
        output = input;
#else
        asm("fcvt %s0, %h1" : "=w" (output) : "x" (input));
#endif
        print_int64(i, output);
    }
}

typedef struct {
    int flag;
    char *desc;
} float_mapping;

float_mapping round_flags[] = {
    { FE_TONEAREST, "to nearest" },
    { FE_UPWARD, "upwards" },
    { FE_DOWNWARD, "downwards" },
    { FE_TOWARDZERO, "to zero" }
};

int main(int argc, char *argv[argc])
{
    int i;

    printf("#### Enabling IEEE Half Precision\n");

    for (i = 0; i < ARRAY_SIZE(round_flags); ++i) {
        fesetround(round_flags[i].flag);
        printf("### Rounding %s\n", round_flags[i].desc);
        convert_single_to_half();
        convert_single_to_double();
        convert_double_to_half();
        convert_double_to_single();
        convert_half_to_single();
        convert_half_to_double();
    }

    /* convert to integer */
    convert_single_to_integer();
    convert_double_to_integer();
    convert_half_to_integer();

    /* And now with ARM alternative FP16 */
#if defined(__arm__)
    /* See glibc sysdeps/arm/fpu_control.h */
    asm("mrc p10, 7, r1, cr1, cr0, 0\n\t"
        "orr r1, r1, %[flags]\n\t"
        "mcr p10, 7, r1, cr1, cr0, 0\n\t"
        : /* no output */ : [flags] "n" (1 << 26) : "r1" );
#else
    asm("mrs x1, fpcr\n\t"
        "orr x1, x1, %[flags]\n\t"
        "msr fpcr, x1\n\t"
        : /* no output */ : [flags] "n" (1 << 26) : "x1" );
#endif

    printf("#### Enabling ARM Alternative Half Precision\n");

    for (i = 0; i < ARRAY_SIZE(round_flags); ++i) {
        fesetround(round_flags[i].flag);
        printf("### Rounding %s\n", round_flags[i].desc);
        convert_single_to_half();
        convert_single_to_double();
        convert_double_to_half();
        convert_double_to_single();
        convert_half_to_single();
        convert_half_to_double();
    }

    /* convert to integer */
    convert_single_to_integer();
    convert_double_to_integer();
    convert_half_to_integer();

    return 0;
}