blob: 417ca7def5f2bc412b232a59bea494b0748a6dcd [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));
Daniel Dunbard417d962008-09-03 21:17:21 +0000121
122 return 0;
123}
124
Chris Lattnerb808c952009-03-22 21:56:56 +0000125
126
Chris Lattnerb808c952009-03-22 21:56:56 +0000127void foo() {
128 __builtin_strcat(0, 0);
129}
130
John McCalldb7b72a2010-02-28 13:00:19 +0000131// CHECK: define void @bar(
132void bar() {
133 float f;
134 double d;
135 long double ld;
136
137 // LLVM's hex representation of float constants is really unfortunate;
138 // basically it does a float-to-double "conversion" and then prints the
139 // hex form of that. That gives us wierd artifacts like exponents
140 // that aren't numerically similar to the original exponent and
141 // significand bit-patterns that are offset by three bits (because
142 // the exponent was expanded from 8 bits to 11).
143 //
144 // 0xAE98 == 1010111010011000
145 // 0x15D3 == 1010111010011
146
147 f = __builtin_huge_valf(); // CHECK: float 0x7FF0000000000000
148 d = __builtin_huge_val(); // CHECK: double 0x7FF0000000000000
149 ld = __builtin_huge_vall(); // CHECK: x86_fp80 0xK7FFF8000000000000000
150 f = __builtin_nanf(""); // CHECK: float 0x7FF8000000000000
151 d = __builtin_nan(""); // CHECK: double 0x7FF8000000000000
152 ld = __builtin_nanl(""); // CHECK: x86_fp80 0xK7FFFC000000000000000
153 f = __builtin_nanf("0xAE98"); // CHECK: float 0x7FF815D300000000
154 d = __builtin_nan("0xAE98"); // CHECK: double 0x7FF800000000AE98
155 ld = __builtin_nanl("0xAE98"); // CHECK: x86_fp80 0xK7FFFC00000000000AE98
156 f = __builtin_nansf(""); // CHECK: float 0x7FF4000000000000
157 d = __builtin_nans(""); // CHECK: double 0x7FF4000000000000
158 ld = __builtin_nansl(""); // CHECK: x86_fp80 0xK7FFFA000000000000000
159 f = __builtin_nansf("0xAE98"); // CHECK: float 0x7FF015D300000000
160 d = __builtin_nans("0xAE98"); // CHECK: double 0x7FF000000000AE98
161 ld = __builtin_nansl("0xAE98");// CHECK: x86_fp80 0xK7FFF800000000000AE98
162
163}
164// CHECK: }