blob: b6a4a68ea4fc9a8671993f7823e59371ed645eb4 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -emit-llvm -o %t %s
Daniel Dunbard417d962008-09-03 21:17:21 +00002// RUN: not grep __builtin %t
John McCalldb7b72a2010-02-28 13:00:19 +00003// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple | FileCheck %s
Daniel Dunbard417d962008-09-03 21:17:21 +00004
Daniel Dunbar23afaad2009-11-17 08:57:36 +00005int printf(const char *, ...);
Daniel Dunbard417d962008-09-03 21:17:21 +00006
7void p(char *str, int x) {
8 printf("%s: %d\n", str, x);
9}
10void q(char *str, double x) {
11 printf("%s: %f\n", str, x);
12}
John McCalldb7b72a2010-02-28 13:00:19 +000013void r(char *str, void *ptr) {
14 printf("%s: %p\n", str, ptr);
15}
16
17int random(void);
Daniel Dunbard417d962008-09-03 21:17:21 +000018
19int main() {
20 int N = random();
21#define P(n,args) p(#n #args, __builtin_##n args)
22#define Q(n,args) q(#n #args, __builtin_##n args)
John McCalldb7b72a2010-02-28 13:00:19 +000023#define R(n,args) r(#n #args, __builtin_##n args)
Daniel Dunbard417d962008-09-03 21:17:21 +000024#define V(n,args) p(#n #args, (__builtin_##n args, 0))
25 P(types_compatible_p, (int, float));
26 P(choose_expr, (0, 10, 20));
27 P(constant_p, (sizeof(10)));
28 P(expect, (N == 12, 0));
29 V(prefetch, (&N));
30 V(prefetch, (&N, 1));
31 V(prefetch, (&N, 1, 0));
32
33 // Numeric Constants
34
35 Q(huge_val, ());
36 Q(huge_valf, ());
37 Q(huge_vall, ());
38 Q(inf, ());
39 Q(inff, ());
40 Q(infl, ());
41
42 // FIXME:
43 // XXX note funny semantics for the (last) argument
44 // P(fpclassify, (FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, 1.0));
45 // P(isinf_sign, (1.0));
46
Daniel Dunbar1768a622008-10-05 06:34:45 +000047 Q(nan, (""));
48 Q(nanf, (""));
49 Q(nanl, (""));
50 Q(nans, (""));
51 Q(nan, ("10"));
52 Q(nanf, ("10"));
53 Q(nanl, ("10"));
54 Q(nans, ("10"));
55
56 P(isgreater, (1., 2.));
57 P(isgreaterequal, (1., 2.));
58 P(isless, (1., 2.));
59 P(islessequal, (1., 2.));
60 P(islessgreater, (1., 2.));
61 P(isunordered, (1., 2.));
Daniel Dunbard417d962008-09-03 21:17:21 +000062
Eli Friedmand6139892009-09-01 04:19:44 +000063 P(isnan, (1.));
64
Daniel Dunbard417d962008-09-03 21:17:21 +000065 // Bitwise & Numeric Functions
66
Daniel Dunbar1768a622008-10-05 06:34:45 +000067 P(abs, (N));
68
Daniel Dunbard417d962008-09-03 21:17:21 +000069 P(clz, (N));
70 P(clzl, (N));
71 P(clzll, (N));
72 P(ctz, (N));
73 P(ctzl, (N));
74 P(ctzll, (N));
75 P(ffs, (N));
76 P(ffsl, (N));
77 P(ffsll, (N));
78 P(parity, (N));
79 P(parityl, (N));
80 P(parityll, (N));
81 P(popcount, (N));
82 P(popcountl, (N));
83 P(popcountll, (N));
84 Q(powi, (1.2f, N));
85 Q(powif, (1.2f, N));
86 Q(powil, (1.2f, N));
87
Daniel Dunbarf8ebb412009-02-20 06:36:40 +000088 // Lib functions
Daniel Dunbard417d962008-09-03 21:17:21 +000089 int a, b, n = random(); // Avoid optimizing out.
90 char s0[10], s1[] = "Hello";
Daniel Dunbarf8ebb412009-02-20 06:36:40 +000091 V(strcat, (s0, s1));
Daniel Dunbar7a0048b2009-04-09 16:42:50 +000092 V(strcmp, (s0, s1));
Daniel Dunbarf8ebb412009-02-20 06:36:40 +000093 V(strncat, (s0, s1, n));
94 V(strchr, (s0, s1[0]));
95 V(strrchr, (s0, s1[0]));
96 V(strcpy, (s0, s1));
97 V(strncpy, (s0, s1, n));
98
99 // Object size checking
Daniel Dunbard417d962008-09-03 21:17:21 +0000100 V(__memset_chk, (s0, 0, sizeof s0, n));
101 V(__memcpy_chk, (s0, s1, sizeof s0, n));
102 V(__memmove_chk, (s0, s1, sizeof s0, n));
103 V(__mempcpy_chk, (s0, s1, sizeof s0, n));
104 V(__strncpy_chk, (s0, s1, sizeof s0, n));
105 V(__strcpy_chk, (s0, s1, n));
106 s0[0] = 0;
107 V(__strcat_chk, (s0, s1, n));
108 P(object_size, (s0, 0));
109 P(object_size, (s0, 1));
110 P(object_size, (s0, 2));
111 P(object_size, (s0, 3));
112
113 // Whatever
114
115 P(bswap32, (N));
116 P(bswap64, (N));
117 // FIXME
118 // V(clear_cache, (&N, &N+1));
119 V(trap, ());
John McCalldb7b72a2010-02-28 13:00:19 +0000120 R(extract_return_addr, (&N));
Eli Friedmanba68b082010-03-06 02:17:52 +0000121 P(signbit, (1.0));
Daniel Dunbard417d962008-09-03 21:17:21 +0000122
123 return 0;
124}
125
Chris Lattnerb808c952009-03-22 21:56:56 +0000126
127
Chris Lattnerb808c952009-03-22 21:56:56 +0000128void foo() {
129 __builtin_strcat(0, 0);
130}
131
John McCalldb7b72a2010-02-28 13:00:19 +0000132// CHECK: define void @bar(
133void bar() {
134 float f;
135 double d;
136 long double ld;
137
138 // LLVM's hex representation of float constants is really unfortunate;
139 // basically it does a float-to-double "conversion" and then prints the
140 // hex form of that. That gives us wierd artifacts like exponents
141 // that aren't numerically similar to the original exponent and
142 // significand bit-patterns that are offset by three bits (because
143 // the exponent was expanded from 8 bits to 11).
144 //
145 // 0xAE98 == 1010111010011000
146 // 0x15D3 == 1010111010011
147
148 f = __builtin_huge_valf(); // CHECK: float 0x7FF0000000000000
149 d = __builtin_huge_val(); // CHECK: double 0x7FF0000000000000
150 ld = __builtin_huge_vall(); // CHECK: x86_fp80 0xK7FFF8000000000000000
151 f = __builtin_nanf(""); // CHECK: float 0x7FF8000000000000
152 d = __builtin_nan(""); // CHECK: double 0x7FF8000000000000
153 ld = __builtin_nanl(""); // CHECK: x86_fp80 0xK7FFFC000000000000000
154 f = __builtin_nanf("0xAE98"); // CHECK: float 0x7FF815D300000000
155 d = __builtin_nan("0xAE98"); // CHECK: double 0x7FF800000000AE98
156 ld = __builtin_nanl("0xAE98"); // CHECK: x86_fp80 0xK7FFFC00000000000AE98
157 f = __builtin_nansf(""); // CHECK: float 0x7FF4000000000000
158 d = __builtin_nans(""); // CHECK: double 0x7FF4000000000000
159 ld = __builtin_nansl(""); // CHECK: x86_fp80 0xK7FFFA000000000000000
160 f = __builtin_nansf("0xAE98"); // CHECK: float 0x7FF015D300000000
161 d = __builtin_nans("0xAE98"); // CHECK: double 0x7FF000000000AE98
162 ld = __builtin_nansl("0xAE98");// CHECK: x86_fp80 0xK7FFF800000000000AE98
163
164}
165// CHECK: }
Chris Lattner81368fb2010-05-06 05:50:07 +0000166
167
Chris Lattnered074152010-05-06 06:04:13 +0000168// CHECK: define void @test_float_builtins
169void test_float_builtins(float F, double D, long double LD) {
Chris Lattner81368fb2010-05-06 05:50:07 +0000170 volatile int res;
171 res = __builtin_isinf(F);
172 // CHECK: call float @fabsf(float
173 // CHECK: fcmp oeq float {{.*}}, 0x7FF0000000000000
174
175 res = __builtin_isinf(D);
176 // CHECK: call double @fabs(double
177 // CHECK: fcmp oeq double {{.*}}, 0x7FF0000000000000
178
179 res = __builtin_isinf(LD);
180 // CHECK: call x86_fp80 @fabsl(x86_fp80
181 // CHECK: fcmp oeq x86_fp80 {{.*}}, 0xK7FFF8000000000000000
Chris Lattnered074152010-05-06 06:04:13 +0000182
183 res = __builtin_isfinite(F);
184 // CHECK: fcmp oeq float
185 // CHECK: call float @fabsf
186 // CHECK: fcmp une float {{.*}}, 0x7FF0000000000000
187 // CHECK: and i1
Chris Lattner81368fb2010-05-06 05:50:07 +0000188}
189