1 // RUN: %clang -fsanitize=float-cast-overflow %s -o %t
2 // RUN: %t _
3 // RUN: %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-0
4 // RUN: %t 1 2>&1 | FileCheck %s --check-prefix=CHECK-1
5 // RUN: %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-2
6 // RUN: %t 3 2>&1 | FileCheck %s --check-prefix=CHECK-3
7 // RUN: %t 4 2>&1 | FileCheck %s --check-prefix=CHECK-4
8 // RUN: %t 5 2>&1 | FileCheck %s --check-prefix=CHECK-5
9 // RUN: %t 6 2>&1 | FileCheck %s --check-prefix=CHECK-6
10 // FIXME: %t 7 2>&1 | FileCheck %s --check-prefix=CHECK-7
11 // RUN: %t 8 2>&1 | FileCheck %s --check-prefix=CHECK-8
12 // RUN: %t 9 2>&1 | FileCheck %s --check-prefix=CHECK-9
13
14 // This test assumes float and double are IEEE-754 single- and double-precision.
15
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 float Inf;
21 float NaN;
22
main(int argc,char ** argv)23 int main(int argc, char **argv) {
24 float MaxFloatRepresentableAsInt = 0x7fffff80;
25 (int)MaxFloatRepresentableAsInt; // ok
26 (int)-MaxFloatRepresentableAsInt; // ok
27
28 float MinFloatRepresentableAsInt = -0x7fffffff - 1;
29 (int)MinFloatRepresentableAsInt; // ok
30
31 float MaxFloatRepresentableAsUInt = 0xffffff00u;
32 (unsigned int)MaxFloatRepresentableAsUInt; // ok
33
34 #ifdef __SIZEOF_INT128__
35 unsigned __int128 FloatMaxAsUInt128 = -((unsigned __int128)1 << 104);
36 (void)(float)FloatMaxAsUInt128; // ok
37 #endif
38
39 // Build a '+Inf'.
40 char InfVal[] = { 0x00, 0x00, 0x80, 0x7f };
41 float Inf;
42 memcpy(&Inf, InfVal, 4);
43
44 // Build a 'NaN'.
45 char NaNVal[] = { 0x01, 0x00, 0x80, 0x7f };
46 float NaN;
47 memcpy(&NaN, NaNVal, 4);
48
49 switch (argv[1][0]) {
50 // FIXME: Produce a source location for these checks and test for it here.
51
52 // Floating point -> integer overflow.
53 case '0':
54 // Note that values between 0x7ffffe00 and 0x80000000 may or may not
55 // successfully round-trip, depending on the rounding mode.
56 // CHECK-0: runtime error: value 2.14748{{.*}} is outside the range of representable values of type 'int'
57 return MaxFloatRepresentableAsInt + 0x80;
58 case '1':
59 // CHECK-1: runtime error: value -2.14748{{.*}} is outside the range of representable values of type 'int'
60 return MinFloatRepresentableAsInt - 0x100;
61 case '2':
62 // CHECK-2: runtime error: value -0.001 is outside the range of representable values of type 'unsigned int'
63 return (unsigned)-0.001;
64 case '3':
65 // CHECK-3: runtime error: value 4.2949{{.*}} is outside the range of representable values of type 'unsigned int'
66 return (unsigned)(MaxFloatRepresentableAsUInt + 0x100);
67
68 case '4':
69 // CHECK-4: runtime error: value {{.*}} is outside the range of representable values of type 'int'
70 return Inf;
71 case '5':
72 // CHECK-5: runtime error: value {{.*}} is outside the range of representable values of type 'int'
73 return NaN;
74
75 // Integer -> floating point overflow.
76 case '6':
77 // CHECK-6: {{runtime error: value 0xffffff00000000000000000000000001 is outside the range of representable values of type 'float'|__int128 not supported}}
78 #ifdef __SIZEOF_INT128__
79 return (float)(FloatMaxAsUInt128 + 1);
80 #else
81 puts("__int128 not supported");
82 return 0;
83 #endif
84 // FIXME: The backend cannot lower __fp16 operations on x86 yet.
85 //case '7':
86 // (__fp16)65504; // ok
87 // // CHECK-7: runtime error: value 65505 is outside the range of representable values of type '__fp16'
88 // return (__fp16)65505;
89
90 // Floating point -> floating point overflow.
91 case '8':
92 // CHECK-8: runtime error: value 1e+39 is outside the range of representable values of type 'float'
93 return (float)1e39;
94 case '9':
95 // CHECK-9: runtime error: value {{.*}} is outside the range of representable values of type 'double'
96 return (double)Inf;
97 }
98 }
99