blob: 157838b6bddc127a02d58763cd6427e6a32975e8 [file] [log] [blame]
Weiming Zhao9b7bbec2017-03-21 05:32:51 +00001// RUN: %clang_builtins %s %librt -o %t && %run %t
Daniel Dunbarfd089992009-06-26 16:47:03 +00002//===-- clzti2_test.c - Test __clzti2 -------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
Howard Hinnant5b791f62010-11-16 22:13:33 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Daniel Dunbarfd089992009-06-26 16:47:03 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file tests __clzti2 for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
Daniel Dunbarfd089992009-06-26 16:47:03 +000015#include "int_lib.h"
16#include <stdio.h>
17
Joerg Sonnenberger14743122014-03-18 21:35:30 +000018#ifdef CRT_HAS_128BIT
19
Daniel Dunbarfd089992009-06-26 16:47:03 +000020// Returns: the number of leading 0-bits
21
22// Precondition: a != 0
23
Derek Schuffeb0ebc32015-04-24 15:45:57 +000024COMPILER_RT_ABI si_int __clzti2(ti_int a);
Daniel Dunbarfd089992009-06-26 16:47:03 +000025
26int test__clzti2(ti_int a, si_int expected)
27{
28 si_int x = __clzti2(a);
29 if (x != expected)
30 {
31 twords at;
32 at.all = a;
33 printf("error in __clzti2(0x%llX%.16llX) = %d, expected %d\n",
Daniel Dunbar64857202009-10-27 17:49:07 +000034 at.s.high, at.s.low, x, expected);
Daniel Dunbarfd089992009-06-26 16:47:03 +000035 }
36 return x != expected;
37}
38
39char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
40
41#endif
42
43int main()
44{
Joerg Sonnenberger14743122014-03-18 21:35:30 +000045#ifdef CRT_HAS_128BIT
Daniel Dunbarfd089992009-06-26 16:47:03 +000046 const int N = (int)(sizeof(ti_int) * CHAR_BIT);
47
48 if (test__clzti2(0x00000001, N-1))
49 return 1;
50 if (test__clzti2(0x00000002, N-2))
51 return 1;
52 if (test__clzti2(0x00000003, N-2))
53 return 1;
54 if (test__clzti2(0x00000004, N-3))
55 return 1;
56 if (test__clzti2(0x00000005, N-3))
57 return 1;
58 if (test__clzti2(0x0000000A, N-4))
59 return 1;
60 if (test__clzti2(0x1000000A, N*3/4+3))
61 return 1;
62 if (test__clzti2(0x2000000A, N*3/4+2))
63 return 1;
64 if (test__clzti2(0x6000000A, N*3/4+1))
65 return 1;
66 if (test__clzti2(0x8000000AuLL, N*3/4))
67 return 1;
68 if (test__clzti2(0x000005008000000AuLL, 85))
69 return 1;
70 if (test__clzti2(0x020005008000000AuLL, 70))
71 return 1;
72 if (test__clzti2(0x720005008000000AuLL, 65))
73 return 1;
74 if (test__clzti2(0x820005008000000AuLL, 64))
75 return 1;
76
77 if (test__clzti2(make_ti(0x0000000080000000LL, 0x8000000800000000LL), 32))
78 return 1;
79 if (test__clzti2(make_ti(0x0000000100000000LL, 0x8000000800000000LL), 31))
80 return 1;
81 if (test__clzti2(make_ti(0x1000000100000000LL, 0x8000000800000000LL), 3))
82 return 1;
83 if (test__clzti2(make_ti(0x7000000100000000LL, 0x8000000800000000LL), 1))
84 return 1;
85 if (test__clzti2(make_ti(0x8000000100000000LL, 0x8000000800000000LL), 0))
86 return 1;
Joerg Sonnenbergerd9bcddd2011-05-29 21:43:29 +000087#else
88 printf("skipped\n");
Daniel Dunbarfd089992009-06-26 16:47:03 +000089#endif
90 return 0;
91}