From nobody Mon Feb 9 23:04:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1591978266; cv=none; d=zohomail.com; s=zohoarc; b=Ih0nrqCJD8QmFzCBVaPESb9wRWFiagnh9DTIM5tCM2ZbSaQ8y5HBe6k0A4OyT7T7UaNYTZoZGQkvxp5iREqEoUaRQzIhaCQFQJI3sBW2CjtWaBYxsRjGQg1r4zOVYw0T0wgsbupU7iLCnqdLhbx5fE3O/2bums+Oq4ubGUBF5zg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591978266; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5szLHKv698yoeNiyz5zZ4JBYKq/JLP6VyGAvk8fDs2E=; b=TR+QD5jWWeKFkoE4Mz0tMqFvJEW5HebnPHOG1660a7TWvj1uSwFiz2m7B4Aibs/qT+vrtxR8eqb3kKSZJPUWhowLGfZE8pgIdXq0Xvh+XmNYsXzYZNNwmGg4/hgC4Hwj4hFLIqki3lQTJcjIQeZpN6W2znO461sCWAbbnZbOrPc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1591978266565395.6884174669814; Fri, 12 Jun 2020 09:11:06 -0700 (PDT) Received: from localhost ([::1]:58674 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jjmGn-0000XY-6V for importer@patchew.org; Fri, 12 Jun 2020 12:11:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58576) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjmE3-0005d3-56 for qemu-devel@nongnu.org; Fri, 12 Jun 2020 12:08:15 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:32188 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jjmE1-00014S-4D for qemu-devel@nongnu.org; Fri, 12 Jun 2020 12:08:14 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-369-xQmIyfblN2iTKGrIWMA3yg-1; Fri, 12 Jun 2020 12:07:59 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 628B0464; Fri, 12 Jun 2020 16:07:58 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 887BB8FF91; Fri, 12 Jun 2020 16:07:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591978092; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5szLHKv698yoeNiyz5zZ4JBYKq/JLP6VyGAvk8fDs2E=; b=MFLUjr+WbT1KmkITPAHRGNgw3HYNcTUS8uV/EBtuRVePRNx57WL3gwgycXHh4uswHBkRHg BSxLUpl4Hde2n1Z94pTxVTqFZiSog3QTMYf324mUI3qcUP2+fdgDXhSoLi1KG71Q9amQPS XBMS46+eYu6LWThRCZpjr6Cf0jLgGPI= X-MC-Unique: xQmIyfblN2iTKGrIWMA3yg-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 082/116] target/i386: correct fix for pcmpxstrx substring search Date: Fri, 12 Jun 2020 12:07:52 -0400 Message-Id: <20200612160755.9597-2-pbonzini@redhat.com> In-Reply-To: <20200612160755.9597-1-pbonzini@redhat.com> References: <20200612160755.9597-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/12 08:07:17 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Joseph Myers Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Joseph Myers This corrects a bug introduced in my previous fix for SSE4.2 pcmpestri / pcmpestrm / pcmpistri / pcmpistrm substring search, commit ae35eea7e4a9f21dd147406dfbcd0c4c6aaf2a60. That commit fixed a bug that showed up in four GCC tests with one libc implementation. The tests in question generate random inputs to the intrinsics and compare results to a C implementation, but they only test 1024 possible random inputs, and when the tests use the cases of those instructions that work with word rather than byte inputs, it's easy to have problematic cases that show up much less frequently than that. Thus, testing with a different libc implementation, and so a different random number generator, showed up a problem with the previous patch. When investigating the previous test failures, I found the description of these instructions in the Intel manuals (starting from computing a 16x16 or 8x8 set of comparison results) confusing and hard to match up with the more optimized implementation in QEMU, and referred to AMD manuals which described the instructions in a different way. Those AMD descriptions are very explicit that the whole of the string being searched for must be found in the other operand, not running off the end of that operand; they say "If the prototype and the SUT are equal in length, the two strings must be identical for the comparison to be TRUE.". However, that statement is incorrect. In my previous commit message, I noted: The operation in this case is a search for a string (argument d to the helper) in another string (argument s to the helper); if a copy of d at a particular position would run off the end of s, the resulting output bit should be 0 whether or not the strings match in the region where they overlap, but the QEMU implementation was wrongly comparing only up to the point where s ends and counting it as a match if an initial segment of d matched a terminal segment of s. Here, "run off the end of s" means that some byte of d would overlap some byte outside of s; thus, if d has zero length, it is considered to match everywhere, including after the end of s. The description "some byte of d would overlap some byte outside of s" is accurate only when understood to refer to overlapping some byte *within the 16-byte operand* but at or after the zero terminator; it is valid to run over the end of s if the end of s is the end of the 16-byte operand. So the fix in the previous patch for the case of d being empty was correct, but the other part of that patch was not correct (as it never allowed partial matches even at the end of the 16-byte operand). Nor was the code before the previous patch correct for the case of d nonempty, as it would always have allowed partial matches at the end of s. Fix with a partial revert of my previous change, combined with inserting a check for the special case of s having maximum length to determine where it is necessary to check for matches. In the added test, test 1 is for the case of empty strings, which failed before my 2017 patch, test 2 is for the bug introduced by my 2017 patch and test 3 deals with the case where a match of an initial segment at the end of the string is not valid when the string ends before the end of the 16-byte operand (that is, the case that would be broken by a simple revert of the non-empty-string part of my 2017 patch). Signed-off-by: Joseph Myers Message-Id: Signed-off-by: Paolo Bonzini --- target/i386/ops_sse.h | 4 ++-- tests/tcg/i386/Makefile.target | 3 +++ tests/tcg/i386/test-i386-pcmpistri.c | 33 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/i386/test-i386-pcmpistri.c diff --git a/target/i386/ops_sse.h b/target/i386/ops_sse.h index 01d6017412..14f2b16abd 100644 --- a/target/i386/ops_sse.h +++ b/target/i386/ops_sse.h @@ -2089,10 +2089,10 @@ static inline unsigned pcmpxstrx(CPUX86State *env, = Reg *d, Reg *s, res =3D (2 << upper) - 1; break; } - for (j =3D valids - validd; j >=3D 0; j--) { + for (j =3D valids =3D=3D upper ? valids : valids - validd; j >=3D = 0; j--) { res <<=3D 1; v =3D 1; - for (i =3D validd; i >=3D 0; i--) { + for (i =3D MIN(valids - j, validd); i >=3D 0; i--) { v &=3D (pcmp_val(s, ctrl, i + j) =3D=3D pcmp_val(d, ctrl, = i)); } res |=3D v; diff --git a/tests/tcg/i386/Makefile.target b/tests/tcg/i386/Makefile.target index 43ee2e181e..53efec0668 100644 --- a/tests/tcg/i386/Makefile.target +++ b/tests/tcg/i386/Makefile.target @@ -10,6 +10,9 @@ ALL_X86_TESTS=3D$(I386_SRCS:.c=3D) SKIP_I386_TESTS=3Dtest-i386-ssse3 X86_64_TESTS:=3D$(filter test-i386-ssse3, $(ALL_X86_TESTS)) =20 +test-i386-pcmpistri: CFLAGS +=3D -msse4.2 +run-test-i386-pcmpistri: QEMU_OPTS +=3D -cpu max + # # hello-i386 is a barebones app # diff --git a/tests/tcg/i386/test-i386-pcmpistri.c b/tests/tcg/i386/test-i38= 6-pcmpistri.c new file mode 100644 index 0000000000..1e81ae611a --- /dev/null +++ b/tests/tcg/i386/test-i386-pcmpistri.c @@ -0,0 +1,33 @@ +/* Test pcmpistri instruction. */ + +#include +#include + +union u { + __m128i x; + unsigned char uc[16]; +}; + +union u s0 =3D { .uc =3D { 0 } }; +union u s1 =3D { .uc =3D "abcdefghijklmnop" }; +union u s2 =3D { .uc =3D "bcdefghijklmnopa" }; +union u s3 =3D { .uc =3D "bcdefghijklmnab" }; + +int +main(void) +{ + int ret =3D 0; + if (_mm_cmpistri(s0.x, s0.x, 0x4c) !=3D 15) { + printf("FAIL: pcmpistri test 1\n"); + ret =3D 1; + } + if (_mm_cmpistri(s1.x, s2.x, 0x4c) !=3D 15) { + printf("FAIL: pcmpistri test 2\n"); + ret =3D 1; + } + if (_mm_cmpistri(s1.x, s3.x, 0x4c) !=3D 16) { + printf("FAIL: pcmpistri test 3\n"); + ret =3D 1; + } + return ret; +} --=20 2.26.2