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