1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_clzti2
3// REQUIRES: int128
4
5#include "int_lib.h"
6#include <stdio.h>
7
8#ifdef CRT_HAS_128BIT
9
10// Returns: the number of leading 0-bits
11
12// Precondition: a != 0
13
14COMPILER_RT_ABI int __clzti2(ti_int a);
15
16int test__clzti2(ti_int a, int expected)
17{
18 int x = __clzti2(a);
19 if (x != expected)
20 {
21 twords at;
22 at.all = a;
23 printf(format: "error in __clzti2(0x%llX%.16llX) = %d, expected %d\n",
24 at.s.high, at.s.low, x, expected);
25 }
26 return x != expected;
27}
28
29char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
30
31#endif
32
33int main()
34{
35#ifdef CRT_HAS_128BIT
36 const int N = (int)(sizeof(ti_int) * CHAR_BIT);
37
38 if (test__clzti2(a: 0x00000001, expected: N-1))
39 return 1;
40 if (test__clzti2(a: 0x00000002, expected: N-2))
41 return 1;
42 if (test__clzti2(a: 0x00000003, expected: N-2))
43 return 1;
44 if (test__clzti2(a: 0x00000004, expected: N-3))
45 return 1;
46 if (test__clzti2(a: 0x00000005, expected: N-3))
47 return 1;
48 if (test__clzti2(a: 0x0000000A, expected: N-4))
49 return 1;
50 if (test__clzti2(a: 0x1000000A, expected: N*3/4+3))
51 return 1;
52 if (test__clzti2(a: 0x2000000A, expected: N*3/4+2))
53 return 1;
54 if (test__clzti2(a: 0x6000000A, expected: N*3/4+1))
55 return 1;
56 if (test__clzti2(a: 0x8000000AuLL, expected: N*3/4))
57 return 1;
58 if (test__clzti2(a: 0x000005008000000AuLL, expected: 85))
59 return 1;
60 if (test__clzti2(a: 0x020005008000000AuLL, expected: 70))
61 return 1;
62 if (test__clzti2(a: 0x720005008000000AuLL, expected: 65))
63 return 1;
64 if (test__clzti2(a: 0x820005008000000AuLL, expected: 64))
65 return 1;
66
67 if (test__clzti2(a: make_ti(h: 0x0000000080000000LL, l: 0x8000000800000000LL), expected: 32))
68 return 1;
69 if (test__clzti2(a: make_ti(h: 0x0000000100000000LL, l: 0x8000000800000000LL), expected: 31))
70 return 1;
71 if (test__clzti2(a: make_ti(h: 0x1000000100000000LL, l: 0x8000000800000000LL), expected: 3))
72 return 1;
73 if (test__clzti2(a: make_ti(h: 0x7000000100000000LL, l: 0x8000000800000000LL), expected: 1))
74 return 1;
75 if (test__clzti2(a: make_ti(h: 0x8000000100000000LL, l: 0x8000000800000000LL), expected: 0))
76 return 1;
77#else
78 printf("skipped\n");
79#endif
80 return 0;
81}
82

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