From nobody Thu Dec 28 17:20:15 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4T1FdC4NQfz56CDs; Thu, 28 Dec 2023 17:20:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4T1FdC2NBnz3Zjc; Thu, 28 Dec 2023 17:20:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703784015; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Tpx9dDELM5PruNkNeK2DDW32wPbR43hnTsEWMT3uT8k=; b=EDJewL4DI6CRCDaEyHRBxj2cj4c6n8kCBASElQ0mU/7s5UvUrzrkz4+Bdm+k6ckAC/vz39 YvKFu0WYsg78l54rtr+nOlxAl2ljKiQB3WxgGjNeE44BPmY76IAzDj5DGPWbDd6M1GsdG2 rzAN/LYOLCghqak8MlfpjDEshHilpTeA3b6kO8NOFz/szAPB4N+wKsxaedjm+rvj5iL2Dx mj+MBulIZH8XfbnVp9vIc/OMDkYcLvRhP/xN2SwchmH1CTxgnFczbb3zyW6ZBMsO6VGHQC 0KruEZdUrwps+p+EfcEIZEUBh/qRBhYqfLmla1+CdYElksjpBXfll1LO1sAkSQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703784015; a=rsa-sha256; cv=none; b=lsZO8khG6rLKqaVT++xDw+vn5JTfCyRhSU0UJb0kPuTcJ7e5w0U1sUByrgF4dAECqVrQiM ScAAQ28xLtHQQ6HgiyPBCs59v8Tacpu2HcVsh6higAlvoVWBbF4XVDGKLe9CpFjkGkEMoQ CGVXJ5ocdN3qX4ki6uZX2Egm2WI9GZFWLeiLVSxkDaySIEfnpd/cK6J5kGI6ECJnzbPKgN kK2R8wTpBqaFBv/CxeSRrqa3aIUnVLQGMtX0hnYR29E+8QDEyaDFeN1q6LO9HxBW8RwAgf hpA6+VjIfjyL5LfXN8dWLmsIgKolgwHqeKN6EzLpWsiO8Wiya+VFqJJPggM2HQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703784015; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Tpx9dDELM5PruNkNeK2DDW32wPbR43hnTsEWMT3uT8k=; b=nM545a24Hs6d2rY2+bpskN0tKrQeoWKmqqd0HK9txg+HgJ/n+FLNGg3T1X1K6g7uhyBOi3 quT2V3O+Si/CkL1heGwkT5sbXE6QiEWU5UZCdnnn04HBSh+8rKO92QgO/LcoHWJ9i7J60q /T9oO/eA20FEo9E4jlt5NfYlQRuDNP/0ThcnJx5JRguGshdudeCxShPiX5G+tX7J7F9glq dvk8y9NtD9S8Zhc2Z1U2YVKkYC282xfSBsOR09hlJRDf4KfiVIxxZwgyzNDdiQHN2MjBzN 8DMbzbLJnvujRHj8XRXdL297MXDwP2Fkt7pkh7cF3Wmk28IYaNvIfbZ1PeIrCQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4T1FdC1JQQzXBj; Thu, 28 Dec 2023 17:20:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BSHKFEi080741; Thu, 28 Dec 2023 17:20:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BSHKFE6080732; Thu, 28 Dec 2023 17:20:15 GMT (envelope-from git) Date: Thu, 28 Dec 2023 17:20:15 GMT Message-Id: <202312281720.3BSHKFE6080732@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Robert Clausecker Subject: git: cec0236976cd - stable/14 - lib/libc/amd64/string/strcspn.S: always return earliest match in 17--32 char case List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: cec0236976cd0cce0c7c05b1031b7f4215ce9178 Auto-Submitted: auto-generated The branch stable/14 has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=cec0236976cd0cce0c7c05b1031b7f4215ce9178 commit cec0236976cd0cce0c7c05b1031b7f4215ce9178 Author: Robert Clausecker AuthorDate: 2023-12-19 17:28:57 +0000 Commit: Robert Clausecker CommitDate: 2023-12-28 17:02:41 +0000 lib/libc/amd64/string/strcspn.S: always return earliest match in 17--32 char case When matching against a set of 17--32 characters, strcspn() uses two invocations of PCMPISTRI to match against the first 16 characters of the set and then the remaining characters. If a match was found in the first half of the set, the code originally immediately returned that match. However, it is possible for a match in the second half of the set to occur earlier in the vector, leading to that match being overlooked. Fix the code by checking if there is a match in the second half of the set and taking the earlier of the two matches. The correctness of the function has been verified with extended unit tests and test runs against the glibc test suite. Approved by: mjg (implicit, via IRC) MFC after: 1 week MFC to: stable/14 (cherry picked from commit c91cd7d03a9dee649ba3a1b9b4014df9de111bb8) --- lib/libc/amd64/string/strcspn.S | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/libc/amd64/string/strcspn.S b/lib/libc/amd64/string/strcspn.S index 53100eeea9a5..648635529e5b 100644 --- a/lib/libc/amd64/string/strcspn.S +++ b/lib/libc/amd64/string/strcspn.S @@ -255,19 +255,28 @@ ARCHENTRY(strcspn, x86_64_v2) leave ret + /* match in first set half during head */ +.Lheadmatchv2first: + mov %ecx, %eax + pcmpistri $0, %xmm0, %xmm3 # match in second set half? + cmp %ecx, %eax # before the first half match? + cmova %ecx, %eax # use the earlier match + leave + ret + .Lgt16v2: movdqu 48(%rsp, %rcx, 1), %xmm3 # second part of set /* set is 17--32 bytes in size */ pcmpistri $0, %xmm0, %xmm2 # match in first set half? - jb .Lheadmatchv2 + jb .Lheadmatchv2first pcmpistri $0, %xmm0, %xmm3 # match in second set half or end of string? jbe .Lheadmatchv2 ALIGN_TEXT 0: movdqa (%rax), %xmm0 pcmpistri $0, %xmm0, %xmm2 - jb 2f # match in first set half? + jb 4f # match in first set half? pcmpistri $0, %xmm0, %xmm3 jbe 1f # match in second set half or end of string? movdqa 16(%rax), %xmm0 @@ -277,7 +286,8 @@ ARCHENTRY(strcspn, x86_64_v2) pcmpistri $0, %xmm0, %xmm3 ja 0b # neither match in 2nd half nor string end? -3: lea -16(%rax), %rax # go back to second half + /* match in second half or NUL */ + lea -16(%rax), %rax # go back to second half 1: jc 2f # jump if match found pxor %xmm1, %xmm1 pcmpeqb %xmm1, %xmm0 # where is the NUL byte? @@ -288,6 +298,17 @@ ARCHENTRY(strcspn, x86_64_v2) leave ret + /* match in first half */ +3: sub $16, %rax # go back to second half +4: sub %rdi, %rax # offset of %xmm0 from beginning of string + mov %ecx, %edx + pcmpistri $0, %xmm0, %xmm3 # match in second set half? + cmp %ecx, %edx # before the first half match? + cmova %ecx, %edx # use the earlier match + add %rdx, %rax # return full ofset + leave + ret + /* set is empty, degrades to strlen */ .Lstrlenv2: leave