From patchwork Sat Dec 24 04:00:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 88990 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp3634586qgi; Fri, 23 Dec 2016 21:09:39 -0800 (PST) X-Received: by 10.55.163.17 with SMTP id m17mr7857545qke.285.1482556179688; Fri, 23 Dec 2016 21:09:39 -0800 (PST) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 11si13357472qkl.87.2016.12.23.21.09.39 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 23 Dec 2016 21:09:39 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org Received: from localhost ([::1]:42211 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cKeaQ-0008Eg-DH for patch@linaro.org; Sat, 24 Dec 2016 00:09:38 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55980) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cKdWZ-00079W-T8 for qemu-devel@nongnu.org; Fri, 23 Dec 2016 23:01:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cKdWY-00067G-Rd for qemu-devel@nongnu.org; Fri, 23 Dec 2016 23:01:35 -0500 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:34865) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cKdWY-00066z-KI for qemu-devel@nongnu.org; Fri, 23 Dec 2016 23:01:34 -0500 Received: by mail-pg0-x244.google.com with SMTP id i5so2481128pgh.2 for ; Fri, 23 Dec 2016 20:01:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Txyjij9aBZk0afonPVpmC+lYIhgw7mWr78wgnMnzTr8=; b=OnVnqpcRPq2Z61MXIKnUBHbFDqef4VxfBKc3SXfK03WdZtqc5gSBqhkEnXzJx12i1s iJMFvpLvEmBFjegl092k79fIAPH6gcbMzeAl2e6rlPfHlB7DIdjH6Q3HvopQmB1vLQtk ELYNQuENq24ifvckZLT/w8pnSXlmVOuP1BANYy0ZEREmDtUh9CQssRYTTpv6403gWBhs 3wn6tcG4/IsR+UVkfBm0CiH8GFgyXxAf07JgJ01ZOTfASWKtUSXGKfB7+MoRVUL543JQ iIA3vbyMtU8YgtQKID9MvFVm8jjzo3nMomSRa/1v0gu+LkS4atPw9XiD/F+RrtjLAva2 Hl9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Txyjij9aBZk0afonPVpmC+lYIhgw7mWr78wgnMnzTr8=; b=ivcXOl3oFH1mivbOcajSOMQlf+gifdTL/i5cDfkxzp6LrEr6XumcFUGvr0wk66yarO 64pf8QwpEAu2aFImpcz0NewLJ7l7NozBGwuKFnRfafbjT3pzdt2+h62ahGmZPTKHxHYR R9LxYLA9i/ucdpqYYwSToSXv23pfDdIOj9nwqgcWBa43L/PvFoO2GUpnNYzqEwK7Ohnw Wq7nZ5VqDKWJDG9dvnY0jvi/AQyzs1X1PNzKHP1vDHapFx/X6bA75WwOD26WEj+Ip/Vr j9Mr9LxgiQwfBMA1ENr8vH0LTdoEt/afRBAAorkAq1G50Zln43soVhdms11K/ogeAhdg H/lQ== X-Gm-Message-State: AIkVDXL/tc3iiNI6z7g/dg1wKl/737ZwQQ7R0MkxzZWJgtG2/MK5Ft9O8XpxcgLbNa63og== X-Received: by 10.98.23.88 with SMTP id 85mr16651496pfx.21.1482552093708; Fri, 23 Dec 2016 20:01:33 -0800 (PST) Received: from bigtime.domain ([2602:47:d954:1500:5e51:4fff:fe40:9c64]) by smtp.gmail.com with ESMTPSA id n25sm65339316pfi.33.2016.12.23.20.01.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Dec 2016 20:01:33 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 23 Dec 2016 20:00:39 -0800 Message-Id: <20161224040042.12654-63-rth@twiddle.net> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161224040042.12654-1-rth@twiddle.net> References: <20161224040042.12654-1-rth@twiddle.net> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH 62/65] tests: New test-bitcnt X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Alex Bennée Add some unit tests for bit count functions (currently only ctpop). As the routines are based on the Hackers Delight optimisations I based the test patterns on their tests. Signed-off-by: Alex Bennée Signed-off-by: Richard Henderson --- tests/.gitignore | 1 + tests/Makefile.include | 2 + tests/test-bitcnt.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 tests/test-bitcnt.c -- 2.9.3 diff --git a/tests/.gitignore b/tests/.gitignore index e9b182e..7357d0a 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -13,6 +13,7 @@ rcutorture test-aio test-base64 test-bitops +test-bitcnt test-blockjob test-blockjob-txn test-bufferiszero diff --git a/tests/Makefile.include b/tests/Makefile.include index 4841d58..42a5697 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -81,6 +81,7 @@ gcov-files-test-qht-y = util/qht.c check-unit-y += tests/test-qht-par$(EXESUF) gcov-files-test-qht-par-y = util/qht.c check-unit-y += tests/test-bitops$(EXESUF) +check-unit-y += tests/test-bitcnt$(EXESUF) check-unit-$(CONFIG_HAS_GLIB_SUBPROCESS_TESTS) += tests/test-qdev-global-props$(EXESUF) check-unit-y += tests/check-qom-interface$(EXESUF) gcov-files-check-qom-interface-y = qom/object.c @@ -571,6 +572,7 @@ tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y) tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-util-obj-y) tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y) +tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y) diff --git a/tests/test-bitcnt.c b/tests/test-bitcnt.c new file mode 100644 index 0000000..e153dcb --- /dev/null +++ b/tests/test-bitcnt.c @@ -0,0 +1,140 @@ +/* + * Test bit count routines + * + * This work is licensed under the terms of the GNU LGPL, version 2 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu/host-utils.h" + +struct bitcnt_test_data { + /* value to count */ + union { + uint8_t w8; + uint16_t w16; + uint32_t w32; + uint64_t w64; + } value; + /* expected result */ + int popct; +}; + +struct bitcnt_test_data eight_bit_data[] = { + { { .w8 = 0x00 }, .popct=0 }, + { { .w8 = 0x01 }, .popct=1 }, + { { .w8 = 0x03 }, .popct=2 }, + { { .w8 = 0x04 }, .popct=1 }, + { { .w8 = 0x0f }, .popct=4 }, + { { .w8 = 0x3f }, .popct=6 }, + { { .w8 = 0x40 }, .popct=1 }, + { { .w8 = 0xf0 }, .popct=4 }, + { { .w8 = 0x7f }, .popct=7 }, + { { .w8 = 0x80 }, .popct=1 }, + { { .w8 = 0xf1 }, .popct=5 }, + { { .w8 = 0xfe }, .popct=7 }, + { { .w8 = 0xff }, .popct=8 }, +}; + +static void test_ctpop8(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(eight_bit_data); i++) { + struct bitcnt_test_data *d = &eight_bit_data[i]; + g_assert(ctpop8(d->value.w8)==d->popct); + } +} + +struct bitcnt_test_data sixteen_bit_data[] = { + { { .w16 = 0x0000 }, .popct=0 }, + { { .w16 = 0x0001 }, .popct=1 }, + { { .w16 = 0x0003 }, .popct=2 }, + { { .w16 = 0x000f }, .popct=4 }, + { { .w16 = 0x003f }, .popct=6 }, + { { .w16 = 0x00f0 }, .popct=4 }, + { { .w16 = 0x0f0f }, .popct=8 }, + { { .w16 = 0x1f1f }, .popct=10 }, + { { .w16 = 0x4000 }, .popct=1 }, + { { .w16 = 0x4001 }, .popct=2 }, + { { .w16 = 0x7000 }, .popct=3 }, + { { .w16 = 0x7fff }, .popct=15 }, +}; + +static void test_ctpop16(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(sixteen_bit_data); i++) { + struct bitcnt_test_data *d = &sixteen_bit_data[i]; + g_assert(ctpop16(d->value.w16)==d->popct); + } +} + +struct bitcnt_test_data thirtytwo_bit_data[] = { + { { .w32 = 0x00000000 }, .popct=0 }, + { { .w32 = 0x00000001 }, .popct=1 }, + { { .w32 = 0x0000000f }, .popct=4 }, + { { .w32 = 0x00000f0f }, .popct=8 }, + { { .w32 = 0x00001f1f }, .popct=10 }, + { { .w32 = 0x00004001 }, .popct=2 }, + { { .w32 = 0x00007000 }, .popct=3 }, + { { .w32 = 0x00007fff }, .popct=15 }, + { { .w32 = 0x55555555 }, .popct=16 }, + { { .w32 = 0xaaaaaaaa }, .popct=16 }, + { { .w32 = 0xff000000 }, .popct=8 }, + { { .w32 = 0xc0c0c0c0 }, .popct=8 }, + { { .w32 = 0x0ffffff0 }, .popct=24 }, + { { .w32 = 0x80000000 }, .popct=1 }, + { { .w32 = 0xffffffff }, .popct=32 }, +}; + +static void test_ctpop32(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(thirtytwo_bit_data); i++) { + struct bitcnt_test_data *d = &thirtytwo_bit_data[i]; + g_assert(ctpop32(d->value.w32)==d->popct); + } +} + +struct bitcnt_test_data sixtyfour_bit_data[] = { + { { .w64 = 0x0000000000000000ULL }, .popct=0 }, + { { .w64 = 0x0000000000000001ULL }, .popct=1 }, + { { .w64 = 0x000000000000000fULL }, .popct=4 }, + { { .w64 = 0x0000000000000f0fULL }, .popct=8 }, + { { .w64 = 0x0000000000001f1fULL }, .popct=10 }, + { { .w64 = 0x0000000000004001ULL }, .popct=2 }, + { { .w64 = 0x0000000000007000ULL }, .popct=3 }, + { { .w64 = 0x0000000000007fffULL }, .popct=15 }, + { { .w64 = 0x0000005500555555ULL }, .popct=16 }, + { { .w64 = 0x00aa0000aaaa00aaULL }, .popct=16 }, + { { .w64 = 0x000f000000f00000ULL }, .popct=8 }, + { { .w64 = 0x0c0c0000c0c0c0c0ULL }, .popct=12 }, + { { .w64 = 0xf00f00f0f0f0f000ULL }, .popct=24 }, + { { .w64 = 0x8000000000000000ULL }, .popct=1 }, + { { .w64 = 0xf0f0f0f0f0f0f0f0ULL }, .popct=32 }, + { { .w64 = 0xffffffffffffffffULL }, .popct=64 }, +}; + +static void test_ctpop64(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(sixtyfour_bit_data); i++) { + struct bitcnt_test_data *d = &sixtyfour_bit_data[i]; + g_assert(ctpop64(d->value.w64)==d->popct); + } +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_func("/bitcnt/ctpop8", test_ctpop8); + g_test_add_func("/bitcnt/ctpop16", test_ctpop16); + g_test_add_func("/bitcnt/ctpop32", test_ctpop32); + g_test_add_func("/bitcnt/ctpop64", test_ctpop64); + return g_test_run(); +}