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