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