1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_ctzsi2
3
4#include "int_lib.h"
5#include <stdio.h>
6
7// Returns: the number of trailing 0-bits
8
9// Precondition: a != 0
10
11COMPILER_RT_ABI int __ctzsi2(si_int a);
12
13int test__ctzsi2(si_int a, int expected)
14{
15 int x = __ctzsi2(a);
16 if (x != expected)
17 printf(format: "error in __ctzsi2(0x%X) = %d, expected %d\n", a, x, expected);
18 return x != expected;
19}
20
21char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0};
22char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0};
23
24int main()
25{
26// if (test__ctzsi2(0x00000000, 32)) // undefined
27// return 1;
28 if (test__ctzsi2(a: 0x00000001, expected: 0))
29 return 1;
30 if (test__ctzsi2(a: 0x00000002, expected: 1))
31 return 1;
32 if (test__ctzsi2(a: 0x00000003, expected: 0))
33 return 1;
34 if (test__ctzsi2(a: 0x00000004, expected: 2))
35 return 1;
36 if (test__ctzsi2(a: 0x00000005, expected: 0))
37 return 1;
38 if (test__ctzsi2(a: 0x00000006, expected: 1))
39 return 1;
40 if (test__ctzsi2(a: 0x00000007, expected: 0))
41 return 1;
42 if (test__ctzsi2(a: 0x00000008, expected: 3))
43 return 1;
44 if (test__ctzsi2(a: 0x00000009, expected: 0))
45 return 1;
46 if (test__ctzsi2(a: 0x0000000A, expected: 1))
47 return 1;
48 if (test__ctzsi2(a: 0x0000000B, expected: 0))
49 return 1;
50 if (test__ctzsi2(a: 0x0000000C, expected: 2))
51 return 1;
52 if (test__ctzsi2(a: 0x0000000D, expected: 0))
53 return 1;
54 if (test__ctzsi2(a: 0x0000000E, expected: 1))
55 return 1;
56 if (test__ctzsi2(a: 0x0000000F, expected: 0))
57 return 1;
58 if (test__ctzsi2(a: 0x00000010, expected: 4))
59 return 1;
60 if (test__ctzsi2(a: 0x00000012, expected: 1))
61 return 1;
62 if (test__ctzsi2(a: 0x00000013, expected: 0))
63 return 1;
64 if (test__ctzsi2(a: 0x00000014, expected: 2))
65 return 1;
66 if (test__ctzsi2(a: 0x00000015, expected: 0))
67 return 1;
68 if (test__ctzsi2(a: 0x00000016, expected: 1))
69 return 1;
70 if (test__ctzsi2(a: 0x00000017, expected: 0))
71 return 1;
72 if (test__ctzsi2(a: 0x00000018, expected: 3))
73 return 1;
74 if (test__ctzsi2(a: 0x00000019, expected: 0))
75 return 1;
76 if (test__ctzsi2(a: 0x0000001A, expected: 1))
77 return 1;
78 if (test__ctzsi2(a: 0x0000001B, expected: 0))
79 return 1;
80 if (test__ctzsi2(a: 0x0000001C, expected: 2))
81 return 1;
82 if (test__ctzsi2(a: 0x0000001D, expected: 0))
83 return 1;
84 if (test__ctzsi2(a: 0x0000001E, expected: 1))
85 return 1;
86 if (test__ctzsi2(a: 0x0000001F, expected: 0))
87 return 1;
88 if (test__ctzsi2(a: 0x00000020, expected: 5))
89 return 1;
90 if (test__ctzsi2(a: 0x00000022, expected: 1))
91 return 1;
92 if (test__ctzsi2(a: 0x00000023, expected: 0))
93 return 1;
94 if (test__ctzsi2(a: 0x00000024, expected: 2))
95 return 1;
96 if (test__ctzsi2(a: 0x00000025, expected: 0))
97 return 1;
98 if (test__ctzsi2(a: 0x00000026, expected: 1))
99 return 1;
100 if (test__ctzsi2(a: 0x00000027, expected: 0))
101 return 1;
102 if (test__ctzsi2(a: 0x00000028, expected: 3))
103 return 1;
104 if (test__ctzsi2(a: 0x00000029, expected: 0))
105 return 1;
106 if (test__ctzsi2(a: 0x0000002A, expected: 1))
107 return 1;
108 if (test__ctzsi2(a: 0x0000002B, expected: 0))
109 return 1;
110 if (test__ctzsi2(a: 0x0000002C, expected: 2))
111 return 1;
112 if (test__ctzsi2(a: 0x0000002D, expected: 0))
113 return 1;
114 if (test__ctzsi2(a: 0x0000002E, expected: 1))
115 return 1;
116 if (test__ctzsi2(a: 0x0000002F, expected: 0))
117 return 1;
118 if (test__ctzsi2(a: 0x00000030, expected: 4))
119 return 1;
120 if (test__ctzsi2(a: 0x00000032, expected: 1))
121 return 1;
122 if (test__ctzsi2(a: 0x00000033, expected: 0))
123 return 1;
124 if (test__ctzsi2(a: 0x00000034, expected: 2))
125 return 1;
126 if (test__ctzsi2(a: 0x00000035, expected: 0))
127 return 1;
128 if (test__ctzsi2(a: 0x00000036, expected: 1))
129 return 1;
130 if (test__ctzsi2(a: 0x00000037, expected: 0))
131 return 1;
132 if (test__ctzsi2(a: 0x00000038, expected: 3))
133 return 1;
134 if (test__ctzsi2(a: 0x00000039, expected: 0))
135 return 1;
136 if (test__ctzsi2(a: 0x0000003A, expected: 1))
137 return 1;
138 if (test__ctzsi2(a: 0x0000003B, expected: 0))
139 return 1;
140 if (test__ctzsi2(a: 0x0000003C, expected: 2))
141 return 1;
142 if (test__ctzsi2(a: 0x0000003D, expected: 0))
143 return 1;
144 if (test__ctzsi2(a: 0x0000003E, expected: 1))
145 return 1;
146 if (test__ctzsi2(a: 0x0000003F, expected: 0))
147 return 1;
148 if (test__ctzsi2(a: 0x00000040, expected: 6))
149 return 1;
150 if (test__ctzsi2(a: 0x00000042, expected: 1))
151 return 1;
152 if (test__ctzsi2(a: 0x00000043, expected: 0))
153 return 1;
154 if (test__ctzsi2(a: 0x00000044, expected: 2))
155 return 1;
156 if (test__ctzsi2(a: 0x00000045, expected: 0))
157 return 1;
158 if (test__ctzsi2(a: 0x00000046, expected: 1))
159 return 1;
160 if (test__ctzsi2(a: 0x00000047, expected: 0))
161 return 1;
162 if (test__ctzsi2(a: 0x00000048, expected: 3))
163 return 1;
164 if (test__ctzsi2(a: 0x00000049, expected: 0))
165 return 1;
166 if (test__ctzsi2(a: 0x0000004A, expected: 1))
167 return 1;
168 if (test__ctzsi2(a: 0x0000004B, expected: 0))
169 return 1;
170 if (test__ctzsi2(a: 0x0000004C, expected: 2))
171 return 1;
172 if (test__ctzsi2(a: 0x0000004D, expected: 0))
173 return 1;
174 if (test__ctzsi2(a: 0x0000004E, expected: 1))
175 return 1;
176 if (test__ctzsi2(a: 0x0000004F, expected: 0))
177 return 1;
178 if (test__ctzsi2(a: 0x00000050, expected: 4))
179 return 1;
180 if (test__ctzsi2(a: 0x00000052, expected: 1))
181 return 1;
182 if (test__ctzsi2(a: 0x00000053, expected: 0))
183 return 1;
184 if (test__ctzsi2(a: 0x00000054, expected: 2))
185 return 1;
186 if (test__ctzsi2(a: 0x00000055, expected: 0))
187 return 1;
188 if (test__ctzsi2(a: 0x00000056, expected: 1))
189 return 1;
190 if (test__ctzsi2(a: 0x00000057, expected: 0))
191 return 1;
192 if (test__ctzsi2(a: 0x00000058, expected: 3))
193 return 1;
194 if (test__ctzsi2(a: 0x00000059, expected: 0))
195 return 1;
196 if (test__ctzsi2(a: 0x0000005A, expected: 1))
197 return 1;
198 if (test__ctzsi2(a: 0x0000005B, expected: 0))
199 return 1;
200 if (test__ctzsi2(a: 0x0000005C, expected: 2))
201 return 1;
202 if (test__ctzsi2(a: 0x0000005D, expected: 0))
203 return 1;
204 if (test__ctzsi2(a: 0x0000005E, expected: 1))
205 return 1;
206 if (test__ctzsi2(a: 0x0000005F, expected: 0))
207 return 1;
208 if (test__ctzsi2(a: 0x00000060, expected: 5))
209 return 1;
210 if (test__ctzsi2(a: 0x00000062, expected: 1))
211 return 1;
212 if (test__ctzsi2(a: 0x00000063, expected: 0))
213 return 1;
214 if (test__ctzsi2(a: 0x00000064, expected: 2))
215 return 1;
216 if (test__ctzsi2(a: 0x00000065, expected: 0))
217 return 1;
218 if (test__ctzsi2(a: 0x00000066, expected: 1))
219 return 1;
220 if (test__ctzsi2(a: 0x00000067, expected: 0))
221 return 1;
222 if (test__ctzsi2(a: 0x00000068, expected: 3))
223 return 1;
224 if (test__ctzsi2(a: 0x00000069, expected: 0))
225 return 1;
226 if (test__ctzsi2(a: 0x0000006A, expected: 1))
227 return 1;
228 if (test__ctzsi2(a: 0x0000006B, expected: 0))
229 return 1;
230 if (test__ctzsi2(a: 0x0000006C, expected: 2))
231 return 1;
232 if (test__ctzsi2(a: 0x0000006D, expected: 0))
233 return 1;
234 if (test__ctzsi2(a: 0x0000006E, expected: 1))
235 return 1;
236 if (test__ctzsi2(a: 0x0000006F, expected: 0))
237 return 1;
238 if (test__ctzsi2(a: 0x00000070, expected: 4))
239 return 1;
240 if (test__ctzsi2(a: 0x00000072, expected: 1))
241 return 1;
242 if (test__ctzsi2(a: 0x00000073, expected: 0))
243 return 1;
244 if (test__ctzsi2(a: 0x00000074, expected: 2))
245 return 1;
246 if (test__ctzsi2(a: 0x00000075, expected: 0))
247 return 1;
248 if (test__ctzsi2(a: 0x00000076, expected: 1))
249 return 1;
250 if (test__ctzsi2(a: 0x00000077, expected: 0))
251 return 1;
252 if (test__ctzsi2(a: 0x00000078, expected: 3))
253 return 1;
254 if (test__ctzsi2(a: 0x00000079, expected: 0))
255 return 1;
256 if (test__ctzsi2(a: 0x0000007A, expected: 1))
257 return 1;
258 if (test__ctzsi2(a: 0x0000007B, expected: 0))
259 return 1;
260 if (test__ctzsi2(a: 0x0000007C, expected: 2))
261 return 1;
262 if (test__ctzsi2(a: 0x0000007D, expected: 0))
263 return 1;
264 if (test__ctzsi2(a: 0x0000007E, expected: 1))
265 return 1;
266 if (test__ctzsi2(a: 0x0000007F, expected: 0))
267 return 1;
268 if (test__ctzsi2(a: 0x00000080, expected: 7))
269 return 1;
270 if (test__ctzsi2(a: 0x00000082, expected: 1))
271 return 1;
272 if (test__ctzsi2(a: 0x00000083, expected: 0))
273 return 1;
274 if (test__ctzsi2(a: 0x00000084, expected: 2))
275 return 1;
276 if (test__ctzsi2(a: 0x00000085, expected: 0))
277 return 1;
278 if (test__ctzsi2(a: 0x00000086, expected: 1))
279 return 1;
280 if (test__ctzsi2(a: 0x00000087, expected: 0))
281 return 1;
282 if (test__ctzsi2(a: 0x00000088, expected: 3))
283 return 1;
284 if (test__ctzsi2(a: 0x00000089, expected: 0))
285 return 1;
286 if (test__ctzsi2(a: 0x0000008A, expected: 1))
287 return 1;
288 if (test__ctzsi2(a: 0x0000008B, expected: 0))
289 return 1;
290 if (test__ctzsi2(a: 0x0000008C, expected: 2))
291 return 1;
292 if (test__ctzsi2(a: 0x0000008D, expected: 0))
293 return 1;
294 if (test__ctzsi2(a: 0x0000008E, expected: 1))
295 return 1;
296 if (test__ctzsi2(a: 0x0000008F, expected: 0))
297 return 1;
298 if (test__ctzsi2(a: 0x00000090, expected: 4))
299 return 1;
300 if (test__ctzsi2(a: 0x00000092, expected: 1))
301 return 1;
302 if (test__ctzsi2(a: 0x00000093, expected: 0))
303 return 1;
304 if (test__ctzsi2(a: 0x00000094, expected: 2))
305 return 1;
306 if (test__ctzsi2(a: 0x00000095, expected: 0))
307 return 1;
308 if (test__ctzsi2(a: 0x00000096, expected: 1))
309 return 1;
310 if (test__ctzsi2(a: 0x00000097, expected: 0))
311 return 1;
312 if (test__ctzsi2(a: 0x00000098, expected: 3))
313 return 1;
314 if (test__ctzsi2(a: 0x00000099, expected: 0))
315 return 1;
316 if (test__ctzsi2(a: 0x0000009A, expected: 1))
317 return 1;
318 if (test__ctzsi2(a: 0x0000009B, expected: 0))
319 return 1;
320 if (test__ctzsi2(a: 0x0000009C, expected: 2))
321 return 1;
322 if (test__ctzsi2(a: 0x0000009D, expected: 0))
323 return 1;
324 if (test__ctzsi2(a: 0x0000009E, expected: 1))
325 return 1;
326 if (test__ctzsi2(a: 0x0000009F, expected: 0))
327 return 1;
328 if (test__ctzsi2(a: 0x000000A0, expected: 5))
329 return 1;
330 if (test__ctzsi2(a: 0x000000A2, expected: 1))
331 return 1;
332 if (test__ctzsi2(a: 0x000000A3, expected: 0))
333 return 1;
334 if (test__ctzsi2(a: 0x000000A4, expected: 2))
335 return 1;
336 if (test__ctzsi2(a: 0x000000A5, expected: 0))
337 return 1;
338 if (test__ctzsi2(a: 0x000000A6, expected: 1))
339 return 1;
340 if (test__ctzsi2(a: 0x000000A7, expected: 0))
341 return 1;
342 if (test__ctzsi2(a: 0x000000A8, expected: 3))
343 return 1;
344 if (test__ctzsi2(a: 0x000000A9, expected: 0))
345 return 1;
346 if (test__ctzsi2(a: 0x000000AA, expected: 1))
347 return 1;
348 if (test__ctzsi2(a: 0x000000AB, expected: 0))
349 return 1;
350 if (test__ctzsi2(a: 0x000000AC, expected: 2))
351 return 1;
352 if (test__ctzsi2(a: 0x000000AD, expected: 0))
353 return 1;
354 if (test__ctzsi2(a: 0x000000AE, expected: 1))
355 return 1;
356 if (test__ctzsi2(a: 0x000000AF, expected: 0))
357 return 1;
358 if (test__ctzsi2(a: 0x000000B0, expected: 4))
359 return 1;
360 if (test__ctzsi2(a: 0x000000B2, expected: 1))
361 return 1;
362 if (test__ctzsi2(a: 0x000000B3, expected: 0))
363 return 1;
364 if (test__ctzsi2(a: 0x000000B4, expected: 2))
365 return 1;
366 if (test__ctzsi2(a: 0x000000B5, expected: 0))
367 return 1;
368 if (test__ctzsi2(a: 0x000000B6, expected: 1))
369 return 1;
370 if (test__ctzsi2(a: 0x000000B7, expected: 0))
371 return 1;
372 if (test__ctzsi2(a: 0x000000B8, expected: 3))
373 return 1;
374 if (test__ctzsi2(a: 0x000000B9, expected: 0))
375 return 1;
376 if (test__ctzsi2(a: 0x000000BA, expected: 1))
377 return 1;
378 if (test__ctzsi2(a: 0x000000BB, expected: 0))
379 return 1;
380 if (test__ctzsi2(a: 0x000000BC, expected: 2))
381 return 1;
382 if (test__ctzsi2(a: 0x000000BD, expected: 0))
383 return 1;
384 if (test__ctzsi2(a: 0x000000BE, expected: 1))
385 return 1;
386 if (test__ctzsi2(a: 0x000000BF, expected: 0))
387 return 1;
388 if (test__ctzsi2(a: 0x000000C0, expected: 6))
389 return 1;
390 if (test__ctzsi2(a: 0x000000C2, expected: 1))
391 return 1;
392 if (test__ctzsi2(a: 0x000000C3, expected: 0))
393 return 1;
394 if (test__ctzsi2(a: 0x000000C4, expected: 2))
395 return 1;
396 if (test__ctzsi2(a: 0x000000C5, expected: 0))
397 return 1;
398 if (test__ctzsi2(a: 0x000000C6, expected: 1))
399 return 1;
400 if (test__ctzsi2(a: 0x000000C7, expected: 0))
401 return 1;
402 if (test__ctzsi2(a: 0x000000C8, expected: 3))
403 return 1;
404 if (test__ctzsi2(a: 0x000000C9, expected: 0))
405 return 1;
406 if (test__ctzsi2(a: 0x000000CA, expected: 1))
407 return 1;
408 if (test__ctzsi2(a: 0x000000CB, expected: 0))
409 return 1;
410 if (test__ctzsi2(a: 0x000000CC, expected: 2))
411 return 1;
412 if (test__ctzsi2(a: 0x000000CD, expected: 0))
413 return 1;
414 if (test__ctzsi2(a: 0x000000CE, expected: 1))
415 return 1;
416 if (test__ctzsi2(a: 0x000000CF, expected: 0))
417 return 1;
418 if (test__ctzsi2(a: 0x000000D0, expected: 4))
419 return 1;
420 if (test__ctzsi2(a: 0x000000D2, expected: 1))
421 return 1;
422 if (test__ctzsi2(a: 0x000000D3, expected: 0))
423 return 1;
424 if (test__ctzsi2(a: 0x000000D4, expected: 2))
425 return 1;
426 if (test__ctzsi2(a: 0x000000D5, expected: 0))
427 return 1;
428 if (test__ctzsi2(a: 0x000000D6, expected: 1))
429 return 1;
430 if (test__ctzsi2(a: 0x000000D7, expected: 0))
431 return 1;
432 if (test__ctzsi2(a: 0x000000D8, expected: 3))
433 return 1;
434 if (test__ctzsi2(a: 0x000000D9, expected: 0))
435 return 1;
436 if (test__ctzsi2(a: 0x000000DA, expected: 1))
437 return 1;
438 if (test__ctzsi2(a: 0x000000DB, expected: 0))
439 return 1;
440 if (test__ctzsi2(a: 0x000000DC, expected: 2))
441 return 1;
442 if (test__ctzsi2(a: 0x000000DD, expected: 0))
443 return 1;
444 if (test__ctzsi2(a: 0x000000DE, expected: 1))
445 return 1;
446 if (test__ctzsi2(a: 0x000000DF, expected: 0))
447 return 1;
448 if (test__ctzsi2(a: 0x000000E0, expected: 5))
449 return 1;
450 if (test__ctzsi2(a: 0x000000E2, expected: 1))
451 return 1;
452 if (test__ctzsi2(a: 0x000000E3, expected: 0))
453 return 1;
454 if (test__ctzsi2(a: 0x000000E4, expected: 2))
455 return 1;
456 if (test__ctzsi2(a: 0x000000E5, expected: 0))
457 return 1;
458 if (test__ctzsi2(a: 0x000000E6, expected: 1))
459 return 1;
460 if (test__ctzsi2(a: 0x000000E7, expected: 0))
461 return 1;
462 if (test__ctzsi2(a: 0x000000E8, expected: 3))
463 return 1;
464 if (test__ctzsi2(a: 0x000000E9, expected: 0))
465 return 1;
466 if (test__ctzsi2(a: 0x000000EA, expected: 1))
467 return 1;
468 if (test__ctzsi2(a: 0x000000EB, expected: 0))
469 return 1;
470 if (test__ctzsi2(a: 0x000000EC, expected: 2))
471 return 1;
472 if (test__ctzsi2(a: 0x000000ED, expected: 0))
473 return 1;
474 if (test__ctzsi2(a: 0x000000EE, expected: 1))
475 return 1;
476 if (test__ctzsi2(a: 0x000000EF, expected: 0))
477 return 1;
478 if (test__ctzsi2(a: 0x000000F0, expected: 4))
479 return 1;
480 if (test__ctzsi2(a: 0x000000F2, expected: 1))
481 return 1;
482 if (test__ctzsi2(a: 0x000000F3, expected: 0))
483 return 1;
484 if (test__ctzsi2(a: 0x000000F4, expected: 2))
485 return 1;
486 if (test__ctzsi2(a: 0x000000F5, expected: 0))
487 return 1;
488 if (test__ctzsi2(a: 0x000000F6, expected: 1))
489 return 1;
490 if (test__ctzsi2(a: 0x000000F7, expected: 0))
491 return 1;
492 if (test__ctzsi2(a: 0x000000F8, expected: 3))
493 return 1;
494 if (test__ctzsi2(a: 0x000000F9, expected: 0))
495 return 1;
496 if (test__ctzsi2(a: 0x000000FA, expected: 1))
497 return 1;
498 if (test__ctzsi2(a: 0x000000FB, expected: 0))
499 return 1;
500 if (test__ctzsi2(a: 0x000000FC, expected: 2))
501 return 1;
502 if (test__ctzsi2(a: 0x000000FD, expected: 0))
503 return 1;
504 if (test__ctzsi2(a: 0x000000FE, expected: 1))
505 return 1;
506 if (test__ctzsi2(a: 0x000000FF, expected: 0))
507 return 1;
508
509 if (test__ctzsi2(a: 0x00000100, expected: 8))
510 return 1;
511 if (test__ctzsi2(a: 0x00000200, expected: 9))
512 return 1;
513 if (test__ctzsi2(a: 0x00000400, expected: 10))
514 return 1;
515 if (test__ctzsi2(a: 0x00000800, expected: 11))
516 return 1;
517 if (test__ctzsi2(a: 0x00001000, expected: 12))
518 return 1;
519 if (test__ctzsi2(a: 0x00002000, expected: 13))
520 return 1;
521 if (test__ctzsi2(a: 0x00004000, expected: 14))
522 return 1;
523 if (test__ctzsi2(a: 0x00008000, expected: 15))
524 return 1;
525 if (test__ctzsi2(a: 0x00010000, expected: 16))
526 return 1;
527 if (test__ctzsi2(a: 0x00020000, expected: 17))
528 return 1;
529 if (test__ctzsi2(a: 0x00040000, expected: 18))
530 return 1;
531 if (test__ctzsi2(a: 0x00080000, expected: 19))
532 return 1;
533 if (test__ctzsi2(a: 0x00100000, expected: 20))
534 return 1;
535 if (test__ctzsi2(a: 0x00200000, expected: 21))
536 return 1;
537 if (test__ctzsi2(a: 0x00400000, expected: 22))
538 return 1;
539 if (test__ctzsi2(a: 0x00800000, expected: 23))
540 return 1;
541 if (test__ctzsi2(a: 0x01000000, expected: 24))
542 return 1;
543 if (test__ctzsi2(a: 0x02000000, expected: 25))
544 return 1;
545 if (test__ctzsi2(a: 0x04000000, expected: 26))
546 return 1;
547 if (test__ctzsi2(a: 0x08000000, expected: 27))
548 return 1;
549 if (test__ctzsi2(a: 0x10000000, expected: 28))
550 return 1;
551 if (test__ctzsi2(a: 0x20000000, expected: 29))
552 return 1;
553 if (test__ctzsi2(a: 0x40000000, expected: 30))
554 return 1;
555 if (test__ctzsi2(a: 0x80000000, expected: 31))
556 return 1;
557
558 return 0;
559}
560

source code of compiler-rt/test/builtins/Unit/ctzsi2_test.c