1 1.1 joerg //===-- paritydi2_test.c - Test __paritydi2 -------------------------------===// 2 1.1 joerg // 3 1.1 joerg // The LLVM Compiler Infrastructure 4 1.1 joerg // 5 1.1 joerg // This file is dual licensed under the MIT and the University of Illinois Open 6 1.1 joerg // Source Licenses. See LICENSE.TXT for details. 7 1.1 joerg // 8 1.1 joerg //===----------------------------------------------------------------------===// 9 1.1 joerg // 10 1.1 joerg // This file tests __paritydi2 for the compiler_rt library. 11 1.1 joerg // 12 1.1 joerg //===----------------------------------------------------------------------===// 13 1.1 joerg 14 1.1 joerg #include "int_lib.h" 15 1.1 joerg #include <stdio.h> 16 1.1 joerg #include <stdlib.h> 17 1.1 joerg 18 1.1 joerg // Returns: 1 if number of bits is odd else returns 0 19 1.1 joerg 20 1.1 joerg si_int __paritydi2(di_int a); 21 1.1 joerg 22 1.1 joerg int naive_parity(di_int a) 23 1.1 joerg { 24 1.1 joerg int r = 0; 25 1.1 joerg for (; a; a = a & (a - 1)) 26 1.1 joerg r = ~r; 27 1.1 joerg return r & 1; 28 1.1 joerg } 29 1.1 joerg 30 1.1 joerg int test__paritydi2(di_int a) 31 1.1 joerg { 32 1.1 joerg si_int x = __paritydi2(a); 33 1.1 joerg si_int expected = naive_parity(a); 34 1.1 joerg if (x != expected) 35 1.1 joerg printf("error in __paritydi2(0x%llX) = %d, expected %d\n", 36 1.1 joerg a, x, expected); 37 1.1 joerg return x != expected; 38 1.1 joerg } 39 1.1 joerg 40 1.1 joerg char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0}; 41 1.1 joerg char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0}; 42 1.1 joerg 43 1.1 joerg int main() 44 1.1 joerg { 45 1.1 joerg int i; 46 1.1 joerg for (i = 0; i < 10000; ++i) 47 1.1 joerg if (test__paritydi2(((di_int)rand() << 32) + rand())) 48 1.1 joerg return 1; 49 1.1 joerg 50 1.1 joerg return 0; 51 1.1 joerg } 52