From patchwork Thu Dec 8 02:07:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jose Ricardo Ziviani X-Patchwork-Id: 703918 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tYzM14JcSz9t1H for ; Thu, 8 Dec 2016 13:09:17 +1100 (AEDT) Received: from localhost ([::1]:43569 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cEo95-0005Bf-Ep for incoming@patchwork.ozlabs.org; Wed, 07 Dec 2016 21:09:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56572) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cEo7j-00045F-8E for qemu-devel@nongnu.org; Wed, 07 Dec 2016 21:07:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cEo7f-0006Xo-Ha for qemu-devel@nongnu.org; Wed, 07 Dec 2016 21:07:51 -0500 Received: from 001b2d01.pphosted.com ([148.163.156.1]:36384 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cEo7f-0006Vv-2Y for qemu-devel@nongnu.org; Wed, 07 Dec 2016 21:07:47 -0500 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id uB823gRg013777 for ; Wed, 7 Dec 2016 21:07:46 -0500 Received: from e24smtp05.br.ibm.com (e24smtp05.br.ibm.com [32.104.18.26]) by mx0a-001b2d01.pphosted.com with ESMTP id 276qscac53-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 07 Dec 2016 21:07:45 -0500 Received: from localhost by e24smtp05.br.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 8 Dec 2016 00:07:43 -0200 Received: from d24dlp01.br.ibm.com (9.18.248.204) by e24smtp05.br.ibm.com (10.172.0.141) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 8 Dec 2016 00:07:40 -0200 Received: from d24relay03.br.ibm.com (d24relay03.br.ibm.com [9.18.232.225]) by d24dlp01.br.ibm.com (Postfix) with ESMTP id 0DE303520068; Wed, 7 Dec 2016 21:07:10 -0500 (EST) Received: from d24av02.br.ibm.com (d24av02.br.ibm.com [9.8.31.93]) by d24relay03.br.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id uB827eLa40501274; Thu, 8 Dec 2016 00:07:40 -0200 Received: from d24av02.br.ibm.com (localhost [127.0.0.1]) by d24av02.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id uB827d1r005870; Thu, 8 Dec 2016 00:07:40 -0200 Received: from pacoca.ibm.com ([9.85.166.207]) by d24av02.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id uB827XB3005815; Thu, 8 Dec 2016 00:07:37 -0200 From: Jose Ricardo Ziviani To: qemu-ppc@nongnu.org Date: Thu, 8 Dec 2016 00:07:03 -0200 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1481162828-2295-1-git-send-email-joserz@linux.vnet.ibm.com> References: <1481162828-2295-1-git-send-email-joserz@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16120802-0032-0000-0000-00000521E441 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16120802-0033-0000-0000-000011A278F0 Message-Id: <1481162828-2295-2-git-send-email-joserz@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-12-08_01:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609300000 definitions=main-1612080025 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v3 1/6] target-ppc: Implement unsigned quadword left/right shift and unit tests 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: bharata@linux.vnet.ibm.com, qemu-devel@nongnu.org, nikunj@linux.vnet.ibm.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This commit implements functions to right and left shifts and the unittest for them. Such functions is needed due to instructions that requires them. Today, there is already a right shift implementation in int128.h but it's designed for signed numbers. Signed-off-by: Jose Ricardo Ziviani --- include/qemu/host-utils.h | 3 ++ tests/Makefile.include | 5 ++- tests/test-shift128.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++ util/Makefile.objs | 2 +- util/host-utils.c | 44 +++++++++++++++++++++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 tests/test-shift128.c diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index 46187bb..e87de19 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -516,4 +516,7 @@ static inline uint64_t pow2ceil(uint64_t value) return 1ULL << (64 - nlz); } +void urshift(uint64_t *plow, uint64_t *phigh, uint32_t shift); +void ulshift(uint64_t *plow, uint64_t *phigh, uint32_t shift, bool *overflow); + #endif diff --git a/tests/Makefile.include b/tests/Makefile.include index e98d3b6..89e5e85 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -65,6 +65,8 @@ check-unit-$(CONFIG_POSIX) += tests/test-vmstate$(EXESUF) endif check-unit-y += tests/test-cutils$(EXESUF) gcov-files-test-cutils-y += util/cutils.c +check-unit-y += tests/test-shift128$(EXESUF) +gcov-files-test-shift128-y = util/host-utils.c check-unit-y += tests/test-mul64$(EXESUF) gcov-files-test-mul64-y = util/host-utils.c check-unit-y += tests/test-int128$(EXESUF) @@ -460,7 +462,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \ tests/test-opts-visitor.o tests/test-qmp-event.o \ tests/rcutorture.o tests/test-rcu-list.o \ - tests/test-qdist.o \ + tests/test-qdist.o tests/test-shift128.o \ tests/test-qht.o tests/qht-bench.o tests/test-qht-par.o \ tests/atomic_add-bench.o @@ -568,6 +570,7 @@ tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marsh tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y) tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y) +tests/test-shift128$(EXESUF): tests/test-shift128.o $(test-util-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-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) diff --git a/tests/test-shift128.c b/tests/test-shift128.c new file mode 100644 index 0000000..52be6a2 --- /dev/null +++ b/tests/test-shift128.c @@ -0,0 +1,98 @@ +/* + * Test unsigned left and right shift + * + * 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" + +typedef struct { + uint64_t low; + uint64_t high; + uint64_t rlow; + uint64_t rhigh; + int32_t shift; + bool overflow; +} test_data; + +static const test_data test_ltable[] = { + { 1223ULL, 0, 1223ULL, 0, 0, false }, + { 1ULL, 0, 2ULL, 0, 1, false }, + { 1ULL, 0, 4ULL, 0, 2, false }, + { 1ULL, 0, 16ULL, 0, 4, false }, + { 1ULL, 0, 256ULL, 0, 8, false }, + { 1ULL, 0, 65536ULL, 0, 16, false }, + { 1ULL, 0, 2147483648ULL, 0, 31, false }, + { 1ULL, 0, 35184372088832ULL, 0, 45, false }, + { 1ULL, 0, 1152921504606846976ULL, 0, 60, false }, + { 1ULL, 0, 0, 1ULL, 64, false }, + { 1ULL, 0, 0, 65536ULL, 80, false }, + { 1ULL, 0, 0, 9223372036854775808ULL, 127, false }, + { 0ULL, 1, 0, 0, 64, true }, + { 0x8888888888888888ULL, 0x9999999999999999ULL, + 0x8000000000000000ULL, 0x9888888888888888ULL, 60, true }, + { 0x8888888888888888ULL, 0x9999999999999999ULL, + 0, 0x8888888888888888ULL, 64, true }, + { 0x8ULL, 0, 0, 0x8ULL, 64, false }, + { 0x8ULL, 0, 0, 0x8000000000000000ULL, 124, false }, + { 0x1ULL, 0, 0, 0x4000000000000000ULL, 126, false }, + { 0x1ULL, 0, 0, 0x8000000000000000ULL, 127, false }, + { 0x1ULL, 0, 0x1ULL, 0, 128, true }, + { 0, 0, 0ULL, 0, 200, false }, +}; + +static const test_data test_rtable[] = { + { 1223ULL, 0, 1223ULL, 0, 0, false }, + { 9223372036854775808ULL, 9223372036854775808ULL, + 2147483648L, 2147483648ULL, 32, false }, + { 9223372036854775808ULL, 9223372036854775808ULL, + 9223372036854775808ULL, 0, 64, false }, + { 9223372036854775808ULL, 9223372036854775808ULL, + 36028797018963968ULL, 0, 72, false }, + { 9223372036854775808ULL, 9223372036854775808ULL, + 1ULL, 0, 127, false }, + { 9223372036854775808ULL, 0, 4611686018427387904ULL, 0, 1, false }, + { 9223372036854775808ULL, 0, 2305843009213693952ULL, 0, 2, false }, + { 9223372036854775808ULL, 0, 36028797018963968ULL, 0, 8, false }, + { 9223372036854775808ULL, 0, 140737488355328ULL, 0, 16, false }, + { 9223372036854775808ULL, 0, 2147483648ULL, 0, 32, false }, + { 9223372036854775808ULL, 0, 1ULL, 0, 63, false }, + { 9223372036854775808ULL, 0, 0ULL, 0, 64, false }, +}; + +static void test_lshift(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(test_ltable); ++i) { + bool overflow = false; + test_data tmp = test_ltable[i]; + ulshift(&tmp.low, &tmp.high, tmp.shift, &overflow); + g_assert_cmpuint(tmp.low, ==, tmp.rlow); + g_assert_cmpuint(tmp.high, ==, tmp.rhigh); + g_assert_cmpuint(tmp.overflow, ==, overflow); + } +} + +static void test_rshift(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(test_rtable); ++i) { + test_data tmp = test_rtable[i]; + urshift(&tmp.low, &tmp.high, tmp.shift); + g_assert_cmpuint(tmp.low, ==, tmp.rlow); + g_assert_cmpuint(tmp.high, ==, tmp.rhigh); + } +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_func("/host-utils/test_lshift", test_lshift); + g_test_add_func("/host-utils/test_rshift", test_rshift); + return g_test_run(); +} diff --git a/util/Makefile.objs b/util/Makefile.objs index ad0f9c7..39ae26e 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -11,7 +11,7 @@ util-obj-$(CONFIG_POSIX) += memfd.o util-obj-$(CONFIG_WIN32) += oslib-win32.o util-obj-$(CONFIG_WIN32) += qemu-thread-win32.o util-obj-y += envlist.o path.o module.o -util-obj-$(call lnot,$(CONFIG_INT128)) += host-utils.o +util-obj-y += host-utils.o util-obj-y += bitmap.o bitops.o hbitmap.o util-obj-y += fifo8.o util-obj-y += acl.o diff --git a/util/host-utils.c b/util/host-utils.c index b166e57..1ee2433 100644 --- a/util/host-utils.c +++ b/util/host-utils.c @@ -26,6 +26,7 @@ #include "qemu/osdep.h" #include "qemu/host-utils.h" +#ifndef CONFIG_INT128 /* Long integer helpers */ static inline void mul64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b) @@ -158,4 +159,47 @@ int divs128(int64_t *plow, int64_t *phigh, int64_t divisor) return overflow; } +#endif +void urshift(uint64_t *plow, uint64_t *phigh, uint32_t shift) +{ + shift &= 127; + uint64_t h = *phigh >> (shift & 63); + if (shift == 0) { + return; + } else if (shift >= 64) { + *plow = h; + *phigh = 0; + } else { + *plow = (*plow >> (shift & 63)) | (*phigh << (64 - (shift & 63))); + *phigh = h; + } +} + +void ulshift(uint64_t *plow, uint64_t *phigh, uint32_t shift, bool *overflow) +{ + uint64_t low = *plow; + uint64_t high = *phigh; + + if (shift > 127 && (low | high)) { + *overflow = true; + } + shift &= 127; + + if (shift == 0) { + return; + } + + urshift(&low, &high, 128 - shift); + if (low > 0 || high > 0) { + *overflow = true; + } + + if (shift >= 64) { + *phigh = *plow << (shift & 63); + *plow = 0; + } else { + *phigh = (*plow >> (64 - (shift & 63))) | (*phigh << (shift & 63)); + *plow = *plow << shift; + } +}