From nobody Thu Dec 21 02:25:31 2023 X-Original-To: dev-commits-src-main@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 4SwZ633Cd9z548Hl; Thu, 21 Dec 2023 02:25:31 +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 4SwZ631zSFz4PT6; Thu, 21 Dec 2023 02:25:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703125531; 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=bUDMgwhT3LwxiESyq53zB9yNFayl6NSllpQNAltbr1Y=; b=JN2j+uPm/5nMyVDBAbjXZtJn2y4/rlHxaAWduHxN0MjllErI7iTnVGu3k9R7gTuQXNiX7o nqfKIfk1FQvxE0cjdk7mgmvKfobBTgvwSnCsLPu0G71mcXSfiJJOd+omIceqreY3AkZ5hU D+qeHtUIzCLv62tTb++IGJX07Jw0sm1tvuflNqz3jjCGeewfnJmqq5yv4tLQj6jCjybn4O k5Y6XXCPmJ++zBfR8XNNwERUlfES8r+zjb+7aAXdG+aDXEv8EMyFfl6PJE/gdsQx4gN36Q VIt5J4S93p9Tq3tRmsoEwSWGqr7K8gstD8jdPIpFsT6Al4A4TVcKpEdWKaOmZw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703125531; a=rsa-sha256; cv=none; b=sG24H8isI+rtw0YzByrwWAsyEbg0paokxE05I5pAmg6k0j3+lmIf9DeQny9tMij1701Y5F owA8ntCWfZBMp8nqQ17FbgdkBGgH05sjt0FCjxSX0bkMIBCeglLCdV15BoUMSI4TGG919y kl19rRo08TFo923zetIyPk1uoDCmlJQWe54Oboig9/YH7X/zElSN4q8gz2gq1L+Dd+0+On GOQVoFihvs9kCIthHST7vMj7Qj5V12Hm+ouOZYJTGz3k6M+NwFYp/1R9NR+QMiZj+t5F1V qOTi3lnZJAOE2aG6WnQrMChQf3iEqcEOR0GvcG21tfd+SrWB6rLwOPuSplRQwg== 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=1703125531; 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=bUDMgwhT3LwxiESyq53zB9yNFayl6NSllpQNAltbr1Y=; b=KamSm+ZfhcPf4o0IqHxfd3MmrW9O6b4AmBw/nZ69EkcbBi2qIhLLvMZfAfmhiijDBPriR/ OVQjgBldarhvw5SMDSi0Hss4/tqhO0YepqUKVhstsfDwVeme80FQrqFbOmbOxd4mJxMlT3 eQMyUHG7rag9mXsLGmXYuPaaz/ichqg7Q36UbTZAbjOg1FZ3rdk9f6esb4+iPkYeRe7RJ0 91Xt2+D4tLTce1uw5HTfrTtGt4HeEP0nk1zKbuV896QfH4H2ZueCaXnsq0vynXboK/5ixV jMFu+LgjswoGTvDT2l8pkYB8RGsQM+fK5b/bhAZKdAY70ZLDC1I+0ogEkIlMqA== 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 4SwZ6313kbzT9; Thu, 21 Dec 2023 02:25:31 +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 3BL2PVFA037784; Thu, 21 Dec 2023 02:25:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BL2PVoR037781; Thu, 21 Dec 2023 02:25:31 GMT (envelope-from git) Date: Thu, 21 Dec 2023 02:25:31 GMT Message-Id: <202312210225.3BL2PVoR037781@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: c91cd7d03a9d - main - lib/libc/amd64/string/strcspn.S: always return earliest match in 17--32 char case List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: c91cd7d03a9dee649ba3a1b9b4014df9de111bb8 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=c91cd7d03a9dee649ba3a1b9b4014df9de111bb8 commit c91cd7d03a9dee649ba3a1b9b4014df9de111bb8 Author: Robert Clausecker AuthorDate: 2023-12-19 17:28:57 +0000 Commit: Robert Clausecker CommitDate: 2023-12-21 02:17:17 +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 --- 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