1// RUN: %clang_builtins %s %librt -o %t && %run %t 2// REQUIRES: int128 3//===-- popcountti2_test.c - Test __popcountti2 ----------------------------===// 4// 5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6// See https://llvm.org/LICENSE.txt for license information. 7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 8// 9//===----------------------------------------------------------------------===// 10// 11// This file tests __popcountti2 for the compiler_rt library. 12// 13//===----------------------------------------------------------------------===// 14 15#include "int_lib.h" 16#include <stdio.h> 17#include <stdlib.h> 18 19#ifdef CRT_HAS_128BIT 20 21// Returns: count of 1 bits 22 23COMPILER_RT_ABI si_int __popcountti2(ti_int a); 24 25int naive_popcount(ti_int a) 26{ 27 int r = 0; 28 for (; a; a = (tu_int)a >> 1) 29 r += a & 1; 30 return r; 31} 32 33int test__popcountti2(ti_int a) 34{ 35 si_int x = __popcountti2(a); 36 si_int expected = naive_popcount(a); 37 if (x != expected) 38 { 39 twords at; 40 at.all = a; 41 printf("error in __popcountti2(0x%.16llX%.16llX) = %d, expected %d\n", 42 at.s.high, at.s.low, x, expected); 43 } 44 return x != expected; 45} 46 47char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0}; 48char assumption_2[sizeof(di_int)*CHAR_BIT == 64] = {0}; 49 50#endif 51 52int main() 53{ 54#ifdef CRT_HAS_128BIT 55 if (test__popcountti2(0)) 56 return 1; 57 if (test__popcountti2(1)) 58 return 1; 59 if (test__popcountti2(2)) 60 return 1; 61 if (test__popcountti2(0xFFFFFFFFFFFFFFFDLL)) 62 return 1; 63 if (test__popcountti2(0xFFFFFFFFFFFFFFFELL)) 64 return 1; 65 if (test__popcountti2(0xFFFFFFFFFFFFFFFFLL)) 66 return 1; 67 if (test__popcountti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFDLL))) 68 return 1; 69 if (test__popcountti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFELL))) 70 return 1; 71 if (test__popcountti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL))) 72 return 1; 73 int i; 74 for (i = 0; i < 10000; ++i) 75 if (test__popcountti2(((ti_int)rand() << 96) | ((ti_int)rand() << 64) | 76 ((ti_int)rand() << 32) | rand())) 77 return 1; 78 79#else 80 printf("skipped\n"); 81#endif 82 return 0; 83} 84