aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/i386/test-i386-snan-convert.c
blob: ed6d535ce20ec2aabd4b8f30ac2b002fbe2a5b87 (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
/* Test conversions of signaling NaNs to and from long double.  */

#include <stdint.h>
#include <stdio.h>

volatile float f_res;
volatile double d_res;
volatile long double ld_res;

volatile float f_snan = __builtin_nansf("");
volatile double d_snan = __builtin_nans("");
volatile long double ld_snan = __builtin_nansl("");

int issignaling_f(float x)
{
    union { float f; uint32_t u; } u = { .f = x };
    return (u.u & 0x7fffffff) > 0x7f800000 && (u.u & 0x400000) == 0;
}

int issignaling_d(double x)
{
    union { double d; uint64_t u; } u = { .d = x };
    return (((u.u & UINT64_C(0x7fffffffffffffff)) >
            UINT64_C(0x7ff0000000000000)) &&
            (u.u & UINT64_C(0x8000000000000)) == 0);
}

int issignaling_ld(long double x)
{
    union {
        long double ld;
        struct { uint64_t sig; uint16_t sign_exp; } s;
    } u = { .ld = x };
    return ((u.s.sign_exp & 0x7fff) == 0x7fff &&
            (u.s.sig >> 63) != 0 &&
            (u.s.sig & UINT64_C(0x4000000000000000)) == 0);
}

int main(void)
{
    int ret = 0;
    ld_res = f_snan;
    if (issignaling_ld(ld_res)) {
        printf("FAIL: float -> long double\n");
        ret = 1;
    }
    ld_res = d_snan;
    if (issignaling_ld(ld_res)) {
        printf("FAIL: double -> long double\n");
        ret = 1;
    }
    f_res = ld_snan;
    if (issignaling_d(f_res)) {
        printf("FAIL: long double -> float\n");
        ret = 1;
    }
    d_res = ld_snan;
    if (issignaling_d(d_res)) {
        printf("FAIL: long double -> double\n");
        ret = 1;
    }
    return ret;
}