blob: d0a05c45e5447596a0dd0aeb18a098023ff9a4ae [file] [log] [blame]
Daniel Dunbarb3a69012009-06-26 16:47:03 +00001//===-- popcountsi2_test.c - Test __popcountsi2 ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Daniel Dunbarb3a69012009-06-26 16:47:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file tests __popcountsi2 for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "int_lib.h"
15#include <stdio.h>
16#include <stdlib.h>
17
18// Returns: count of 1 bits
19
20si_int __popcountsi2(si_int a);
21
22int naive_popcount(si_int a)
23{
24 int r = 0;
25 for (; a; a = (su_int)a >> 1)
26 r += a & 1;
27 return r;
28}
29
30int test__popcountsi2(si_int a)
31{
32 si_int x = __popcountsi2(a);
33 si_int expected = naive_popcount(a);
34 if (x != expected)
35 printf("error in __popcountsi2(0x%X) = %d, expected %d\n",
36 a, x, expected);
37 return x != expected;
38}
39
40char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0};
41
42int main()
43{
44 if (test__popcountsi2(0))
45 return 1;
46 if (test__popcountsi2(1))
47 return 1;
48 if (test__popcountsi2(2))
49 return 1;
50 if (test__popcountsi2(0xFFFFFFFD))
51 return 1;
52 if (test__popcountsi2(0xFFFFFFFE))
53 return 1;
54 if (test__popcountsi2(0xFFFFFFFF))
55 return 1;
56 int i;
57 for (i = 0; i < 10000; ++i)
58 if (test__popcountsi2(rand()))
59 return 1;
60
61 return 0;
62}