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