1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_ffsti2
3// REQUIRES: int128
4
5#include "int_lib.h"
6#include <stdio.h>
7
8#ifdef CRT_HAS_128BIT
9
10// Returns: the index of the least significant 1-bit in a, or
11// the value zero if a is zero. The least significant bit is index one.
12
13COMPILER_RT_ABI int __ffsti2(ti_int a);
14
15int test__ffsti2(ti_int a, int expected)
16{
17 int x = __ffsti2(a);
18 if (x != expected)
19 {
20 twords at;
21 at.all = a;
22 printf(format: "error in __ffsti2(0x%llX%.16llX) = %d, expected %d\n",
23 at.s.high, at.s.low, x, expected);
24 }
25 return x != expected;
26}
27
28char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
29
30#endif
31
32int main()
33{
34#ifdef CRT_HAS_128BIT
35 if (test__ffsti2(a: 0x00000000, expected: 0))
36 return 1;
37 if (test__ffsti2(a: 0x00000001, expected: 1))
38 return 1;
39 if (test__ffsti2(a: 0x00000002, expected: 2))
40 return 1;
41 if (test__ffsti2(a: 0x00000003, expected: 1))
42 return 1;
43 if (test__ffsti2(a: 0x00000004, expected: 3))
44 return 1;
45 if (test__ffsti2(a: 0x00000005, expected: 1))
46 return 1;
47 if (test__ffsti2(a: 0x0000000A, expected: 2))
48 return 1;
49 if (test__ffsti2(a: 0x10000000, expected: 29))
50 return 1;
51 if (test__ffsti2(a: 0x20000000, expected: 30))
52 return 1;
53 if (test__ffsti2(a: 0x60000000, expected: 30))
54 return 1;
55 if (test__ffsti2(a: 0x80000000uLL, expected: 32))
56 return 1;
57 if (test__ffsti2(a: 0x0000050000000000uLL, expected: 41))
58 return 1;
59 if (test__ffsti2(a: 0x0200080000000000uLL, expected: 44))
60 return 1;
61 if (test__ffsti2(a: 0x7200000000000000uLL, expected: 58))
62 return 1;
63 if (test__ffsti2(a: 0x8000000000000000uLL, expected: 64))
64 return 1;
65 if (test__ffsti2(a: make_ti(h: 0x8000000800000000uLL, l: 0), expected: 100))
66 return 1;
67 if (test__ffsti2(a: make_ti(h: 0x8000000000000000uLL, l: 0), expected: 128))
68 return 1;
69
70#else
71 printf("skipped\n");
72#endif
73 return 0;
74}
75

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