1 // FIXME: run this (and other) UBSan tests in both 32- and 64-bit modes (?).
2 // RUN: %clangxx -fsanitize=float-cast-overflow %s -o %t
3 // RUN: %run %t _
4 // RUN: %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-0
5 // RUN: %run %t 1 2>&1 | FileCheck %s --check-prefix=CHECK-1
6 // RUN: %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-2
7 // RUN: %run %t 3 2>&1 | FileCheck %s --check-prefix=CHECK-3
8 // RUN: %run %t 4 2>&1 | FileCheck %s --check-prefix=CHECK-4
9 // RUN: %run %t 5 2>&1 | FileCheck %s --check-prefix=CHECK-5
10 // RUN: %run %t 6 2>&1 | FileCheck %s --check-prefix=CHECK-6
11 // FIXME: %run %t 7 2>&1 | FileCheck %s --check-prefix=CHECK-7
12 // FIXME: not %run %t 8 2>&1 | FileCheck %s --check-prefix=CHECK-8
13 // RUN: not %run %t 9 2>&1 | FileCheck %s --check-prefix=CHECK-9
14 
15 // This test assumes float and double are IEEE-754 single- and double-precision.
16 // XFAIL: armv7l-unknown-linux-gnueabihf
17 // XFAIL: aarch64
18 
19 #if defined(__APPLE__)
20 # include <machine/endian.h>
21 # define BYTE_ORDER __DARWIN_BYTE_ORDER
22 # define BIG_ENDIAN __DARWIN_BIG_ENDIAN
23 # define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
24 #elif defined(__FreeBSD__)
25 # include <sys/endian.h>
26 # define BYTE_ORDER _BYTE_ORDER
27 # define BIG_ENDIAN _BIG_ENDIAN
28 # define LITTLE_ENDIAN _LITTLE_ENDIAN
29 #else
30 # include <endian.h>
31 # define BYTE_ORDER __BYTE_ORDER
32 # define BIG_ENDIAN __BIG_ENDIAN
33 # define LITTLE_ENDIAN __LITTLE_ENDIAN
34 #endif  // __APPLE__
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 float Inf;
40 float NaN;
41 
42 int main(int argc, char **argv) {
43   float MaxFloatRepresentableAsInt = 0x7fffff80;
44   (int)MaxFloatRepresentableAsInt; // ok
45   (int)-MaxFloatRepresentableAsInt; // ok
46 
47   float MinFloatRepresentableAsInt = -0x7fffffff - 1;
48   (int)MinFloatRepresentableAsInt; // ok
49 
50   float MaxFloatRepresentableAsUInt = 0xffffff00u;
51   (unsigned int)MaxFloatRepresentableAsUInt; // ok
52 
53 #ifdef __SIZEOF_INT128__
54   unsigned __int128 FloatMaxAsUInt128 = -((unsigned __int128)1 << 104);
55   (void)(float)FloatMaxAsUInt128; // ok
56 #endif
57 
58   float NearlyMinusOne = -0.99999;
59   unsigned Zero = NearlyMinusOne; // ok
60 
61   // Build a '+Inf'.
62 #if BYTE_ORDER == LITTLE_ENDIAN
63   char InfVal[] = { 0x00, 0x00, 0x80, 0x7f };
64 #else
65   char InfVal[] = { 0x7f, 0x80, 0x00, 0x00 };
66 #endif
67   float Inf;
68   memcpy(&Inf, InfVal, 4);
69 
70   // Build a 'NaN'.
71 #if BYTE_ORDER == LITTLE_ENDIAN
72   char NaNVal[] = { 0x01, 0x00, 0x80, 0x7f };
73 #else
74   char NaNVal[] = { 0x7f, 0x80, 0x00, 0x01 };
75 #endif
76   float NaN;
77   memcpy(&NaN, NaNVal, 4);
78 
79   double DblInf = (double)Inf; // ok
80 
81   switch (argv[1][0]) {
82     // FIXME: Produce a source location for these checks and test for it here.
83 
84     // Floating point -> integer overflow.
85   case '0':
86     // Note that values between 0x7ffffe00 and 0x80000000 may or may not
87     // successfully round-trip, depending on the rounding mode.
88     // CHECK-0: runtime error: value 2.14748{{.*}} is outside the range of representable values of type 'int'
89     return MaxFloatRepresentableAsInt + 0x80;
90   case '1':
91     // CHECK-1: runtime error: value -2.14748{{.*}} is outside the range of representable values of type 'int'
92     return MinFloatRepresentableAsInt - 0x100;
93   case '2': {
94     // CHECK-2: runtime error: value -1 is outside the range of representable values of type 'unsigned int'
95     volatile float f = -1.0;
96     volatile unsigned u = (unsigned)f;
97     return 0;
98   }
99   case '3':
100     // CHECK-3: runtime error: value 4.2949{{.*}} is outside the range of representable values of type 'unsigned int'
101     return (unsigned)(MaxFloatRepresentableAsUInt + 0x100);
102 
103   case '4':
104     // CHECK-4: runtime error: value {{.*}} is outside the range of representable values of type 'int'
105     return Inf;
106   case '5':
107     // CHECK-5: runtime error: value {{.*}} is outside the range of representable values of type 'int'
108     return NaN;
109 
110     // Integer -> floating point overflow.
111   case '6':
112     // CHECK-6: {{runtime error: value 0xffffff00000000000000000000000001 is outside the range of representable values of type 'float'|__int128 not supported}}
113 #ifdef __SIZEOF_INT128__
114     return (float)(FloatMaxAsUInt128 + 1);
115 #else
116     puts("__int128 not supported");
117     return 0;
118 #endif
119   // FIXME: The backend cannot lower __fp16 operations on x86 yet.
120   //case '7':
121   //  (__fp16)65504; // ok
122   //  // CHECK-7: runtime error: value 65505 is outside the range of representable values of type '__fp16'
123   //  return (__fp16)65505;
124 
125     // Floating point -> floating point overflow.
126   case '8':
127     // CHECK-8: runtime error: value 1e+39 is outside the range of representable values of type 'float'
128     return (float)1e39;
129   case '9':
130     volatile long double ld = 300.0;
131     // CHECK-9: runtime error: value 300 is outside the range of representable values of type 'char'
132     char c = ld;
133     return c;
134   }
135 }
136