1 /* Test fxtract instruction.  */
2 
3 #include <stdint.h>
4 #include <stdio.h>
5 
6 union u {
7     struct { uint64_t sig; uint16_t sign_exp; } s;
8     long double ld;
9 };
10 
11 volatile union u ld_pseudo_m16382 = { .s = { UINT64_C(1) << 63, 0 } };
12 volatile union u ld_invalid_1 = { .s = { 1, 1234 } };
13 volatile union u ld_invalid_2 = { .s = { 0, 1234 } };
14 volatile union u ld_invalid_3 = { .s = { 0, 0x7fff } };
15 volatile union u ld_invalid_4 = { .s = { (UINT64_C(1) << 63) - 1, 0x7fff } };
16 
17 volatile long double ld_sig, ld_exp;
18 
isnan_ld(long double x)19 int isnan_ld(long double x)
20 {
21   union u tmp = { .ld = x };
22   return ((tmp.s.sign_exp & 0x7fff) == 0x7fff &&
23           (tmp.s.sig >> 63) != 0 &&
24           (tmp.s.sig << 1) != 0);
25 }
26 
issignaling_ld(long double x)27 int issignaling_ld(long double x)
28 {
29     union u tmp = { .ld = x };
30     return isnan_ld(x) && (tmp.s.sig & UINT64_C(0x4000000000000000)) == 0;
31 }
32 
main(void)33 int main(void)
34 {
35     int ret = 0;
36     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) : "0" (2.5L));
37     if (ld_sig != 1.25L || ld_exp != 1.0L) {
38         printf("FAIL: fxtract 2.5\n");
39         ret = 1;
40     }
41     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) : "0" (0.0L));
42     if (ld_sig != 0.0L || __builtin_copysignl(1.0L, ld_sig) != 1.0L ||
43         ld_exp != -__builtin_infl()) {
44         printf("FAIL: fxtract 0.0\n");
45         ret = 1;
46     }
47     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) : "0" (-0.0L));
48     if (ld_sig != -0.0L || __builtin_copysignl(1.0L, ld_sig) != -1.0L ||
49         ld_exp != -__builtin_infl()) {
50         printf("FAIL: fxtract -0.0\n");
51         ret = 1;
52     }
53     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
54                       "0" (__builtin_infl()));
55     if (ld_sig != __builtin_infl() || ld_exp != __builtin_infl()) {
56         printf("FAIL: fxtract inf\n");
57         ret = 1;
58     }
59     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
60                       "0" (-__builtin_infl()));
61     if (ld_sig != -__builtin_infl() || ld_exp != __builtin_infl()) {
62         printf("FAIL: fxtract -inf\n");
63         ret = 1;
64     }
65     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
66                       "0" (__builtin_nanl("")));
67     if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
68         !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
69         printf("FAIL: fxtract qnan\n");
70         ret = 1;
71     }
72     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
73                       "0" (__builtin_nansl("")));
74     if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
75         !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
76         printf("FAIL: fxtract snan\n");
77         ret = 1;
78     }
79     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
80                       "0" (0x1p-16445L));
81     if (ld_sig != 1.0L || ld_exp != -16445.0L) {
82         printf("FAIL: fxtract subnormal\n");
83         ret = 1;
84     }
85     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
86                       "0" (ld_pseudo_m16382.ld));
87     if (ld_sig != 1.0L || ld_exp != -16382.0L) {
88         printf("FAIL: fxtract pseudo\n");
89         ret = 1;
90     }
91     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
92                       "0" (ld_invalid_1.ld));
93     if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
94         !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
95         printf("FAIL: fxtract invalid 1\n");
96         ret = 1;
97     }
98     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
99                       "0" (ld_invalid_2.ld));
100     if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
101         !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
102         printf("FAIL: fxtract invalid 2\n");
103         ret = 1;
104     }
105     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
106                       "0" (ld_invalid_3.ld));
107     if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
108         !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
109         printf("FAIL: fxtract invalid 3\n");
110         ret = 1;
111     }
112     __asm__ volatile ("fxtract" : "=t" (ld_sig), "=u" (ld_exp) :
113                       "0" (ld_invalid_4.ld));
114     if (!isnan_ld(ld_sig) || issignaling_ld(ld_sig) ||
115         !isnan_ld(ld_exp) || issignaling_ld(ld_exp)) {
116         printf("FAIL: fxtract invalid 4\n");
117         ret = 1;
118     }
119     return ret;
120 }
121