From nobody Mon Jul 03 20:23:43 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 4Qvy741ZBTz4ln4Q; Mon, 3 Jul 2023 20:23:44 +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 4Qvy740lQjz4NQJ; Mon, 3 Jul 2023 20:23:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688415824; 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=r27bSu4qlg3cvU1CdlgfD8bJBRFoo3fFalNvEgGE1t4=; b=BHovsyIFPrh0ovwuHptmbRlezGv0Wb/FgnsXqstKtvLcKvae28Wi4Z5+MlO2qDQJXqDGdC E6KdEqllmMp8ZJjsh5tQ/PVkpr5mRVbNSSCheY4wyQy3jCL1Ls+F9ec7EXKReqt0BbLAkT 3jpiaolsooip2ZVqMpItKXa8OdCX9QDjzK4hWoTC3U/Jg3dR/FYf9TnU70Wyhp818CfxVx nNmPWTg52EiMBvcabFAkznKYJL9tea5YkNYZJ7OAHOIgskzhi2s3IZKw4nbNr+BkB0M49N LDPkBsf2A/lBWiz0DKDguFhEHEYSloJjQs+ZafErwJJvP00Djf6X9jFWq8K21A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688415824; 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=r27bSu4qlg3cvU1CdlgfD8bJBRFoo3fFalNvEgGE1t4=; b=UJtFgFWj7TyeDFuV9Ajc2lEBdcijCsyl4lAl4+uCmEvBIFJtCnEiIOGA3NDiXz76KG1u2/ cJFFFddATU5XuBELOLwHQtW7rgX6FFY4aNoHSapo3o5jpy3KNWfP9TOd+GP61FkXA5u09o y+0x19iZMT1S3uK3Wrk/kmBo09QxiIqwVE2Lyn9Q97O8FhkZ9plwvjPX28kJGbToQ1tKWl lpS36Fec1e8YQDlOQFl4EYNfKQ/UU2ke7dMWmOpdPMqFB0IMY1RZt3uRrNnqVRV5u5c7qI RPOsJPJ65sBOCFsHG1Sqc9dIFNvQqJqVV8UmY2bKIpMxhMrAOpxtCfacOWQkHg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1688415824; a=rsa-sha256; cv=none; b=Ta853JTsjoGz+OKYdS6M+wBmyu0tzfK4lIT7ld4Xpq5G4G2qQ351+jSWSuGD2RBLFNywzN d8E91o9sljjYMPpbgGukX3+XcdQVbF5+u5r1/B5OHa7EGPUTYP0dnjChSiDSvnqTbgj1nK a6wU82q88LACcctLf5qv1kVQktF2kVN9McLq7EcSUiH9LDAFZgqBlut3n6encMknT6l0zu XasDO+kEJFBf8jMv2AbVc/vtjaSiNASBj2xa2/B7SPadh9shhLIvnMlkTtW86B33H+/ZtB kLGU0fgKfjjmRs1eWR4Xa3Zb0aDKdGbU8B6n82KzCsq767SvL+30MlIoN3FIEQ== 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 4Qvy736xh8z14KG; Mon, 3 Jul 2023 20:23:43 +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 363KNh5K031915; Mon, 3 Jul 2023 20:23:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 363KNhu6031914; Mon, 3 Jul 2023 20:23:43 GMT (envelope-from git) Date: Mon, 3 Jul 2023 20:23:43 GMT Message-Id: <202307032023.363KNhu6031914@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: ee8b0c436d72 - main - lib/libc/string: replace ffs/fls implementations with clang builtins 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: ee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=ee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16 commit ee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16 Author: Robert Clausecker AuthorDate: 2023-06-23 16:22:54 +0000 Commit: Robert Clausecker CommitDate: 2023-07-03 20:18:27 +0000 lib/libc/string: replace ffs/fls implementations with clang builtins Most architectures we support (except for riscv64) have instructions to compute these functions very quickly. Replace old code with the ffs and clz builtin functions, allowing clang to generate good code for all architectures. As a consequence, toss out arm and i386 ffs() implementations. Sponsored by: FreeBSD Foundation Approved by: mhorne MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D40730 --- lib/libc/arm/string/Makefile.inc | 1 - lib/libc/arm/string/ffs.S | 57 --------------------------------------- lib/libc/i386/string/Makefile.inc | 1 - lib/libc/i386/string/ffs.S | 56 -------------------------------------- lib/libc/string/ffs.c | 12 ++++----- lib/libc/string/ffsl.c | 12 ++++----- lib/libc/string/ffsll.c | 12 ++++----- lib/libc/string/fls.c | 13 +++++---- lib/libc/string/flsl.c | 14 +++++----- lib/libc/string/flsll.c | 13 +++++---- 10 files changed, 34 insertions(+), 157 deletions(-) diff --git a/lib/libc/arm/string/Makefile.inc b/lib/libc/arm/string/Makefile.inc index cf3175ea4e8c..550de34adb0e 100644 --- a/lib/libc/arm/string/Makefile.inc +++ b/lib/libc/arm/string/Makefile.inc @@ -3,7 +3,6 @@ MDSRCS+= \ bcopy.S \ bzero.S \ - ffs.S \ memcmp.S \ memcpy.S \ memmove.S \ diff --git a/lib/libc/arm/string/ffs.S b/lib/libc/arm/string/ffs.S deleted file mode 100644 index 5756dd81d21d..000000000000 --- a/lib/libc/arm/string/ffs.S +++ /dev/null @@ -1,57 +0,0 @@ -/* $NetBSD: ffs.S,v 1.5 2003/04/05 23:08:52 bjh21 Exp $ */ -/* - * Copyright (c) 2001 Christopher Gilbert - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the company nor the name of the author may be used to - * endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -__FBSDID("$FreeBSD$"); - -.syntax unified - -/* - * ffs - find first set bit, this algorithm isolates the first set - * bit, then multiplies the number by 0x0450fbaf which leaves the top - * 6 bits as an index into the table. This algorithm should be a win - * over the checking each bit in turn as per the C compiled version. - * - * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on - * 16 Feb 1994. - */ - -ENTRY(ffs) - /* Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry */ - rsb r1, r0, #0 - ands r0, r0, r1 - itt ne - clzne r0, r0 - rsbne r0, r0, #32 - RET -END(ffs) - - .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/i386/string/Makefile.inc b/lib/libc/i386/string/Makefile.inc index a8c50a285cbd..f3678b93bec3 100644 --- a/lib/libc/i386/string/Makefile.inc +++ b/lib/libc/i386/string/Makefile.inc @@ -4,7 +4,6 @@ MDSRCS+= \ bcmp.S \ bcopy.S \ bzero.S \ - ffs.S \ memcmp.S \ memcpy.S \ memmove.S \ diff --git a/lib/libc/i386/string/ffs.S b/lib/libc/i386/string/ffs.S deleted file mode 100644 index 3a0431c6a845..000000000000 --- a/lib/libc/i386/string/ffs.S +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 1993 Winning Strategies, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by Winning Strategies, Inc. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -__FBSDID("$FreeBSD$"); - -/* - * ffs(value) - * finds the first bit set in value and returns the index of - * that bit. Bits are numbered starting from 1, starting at the - * rightmost bit. A return value of 0 means that the argument - * was zero. - * - * Written by: - * J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc. - */ - -ENTRY(ffs) - bsfl 4(%esp),%eax - jz L1 /* ZF is set if all bits are 0 */ - incl %eax /* bits numbered from 1, not 0 */ - ret - - .align 2 -L1: xorl %eax,%eax /* clear result */ - ret -END(ffs) - - .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/string/ffs.c b/lib/libc/string/ffs.c index 738ef90ce091..c011b3390612 100644 --- a/lib/libc/string/ffs.c +++ b/lib/libc/string/ffs.c @@ -3,6 +3,10 @@ * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2023 The FreeBSD Foundation + * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -43,11 +47,5 @@ __FBSDID("$FreeBSD$"); int ffs(int mask) { - int bit; - - if (mask == 0) - return(0); - for (bit = 1; !(mask & 1); bit++) - mask = (unsigned int)mask >> 1; - return (bit); + return (__builtin_ffs(mask)); } diff --git a/lib/libc/string/ffsl.c b/lib/libc/string/ffsl.c index dbd894b9655b..6e1ac8ec45c1 100644 --- a/lib/libc/string/ffsl.c +++ b/lib/libc/string/ffsl.c @@ -3,6 +3,10 @@ * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2023 The FreeBSD Foundation + * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -40,11 +44,5 @@ __FBSDID("$FreeBSD$"); int ffsl(long mask) { - int bit; - - if (mask == 0) - return (0); - for (bit = 1; !(mask & 1); bit++) - mask = (unsigned long)mask >> 1; - return (bit); + return (__builtin_ffsl(mask)); } diff --git a/lib/libc/string/ffsll.c b/lib/libc/string/ffsll.c index 91886de2f127..b945658b9008 100644 --- a/lib/libc/string/ffsll.c +++ b/lib/libc/string/ffsll.c @@ -3,6 +3,10 @@ * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2023 The FreeBSD Foundation + * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -40,11 +44,5 @@ __FBSDID("$FreeBSD$"); int ffsll(long long mask) { - int bit; - - if (mask == 0) - return (0); - for (bit = 1; !(mask & 1); bit++) - mask = (unsigned long long)mask >> 1; - return (bit); + return (__builtin_ffsll(mask)); } diff --git a/lib/libc/string/fls.c b/lib/libc/string/fls.c index d9edc41f9599..3c4719776778 100644 --- a/lib/libc/string/fls.c +++ b/lib/libc/string/fls.c @@ -3,6 +3,10 @@ * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2023 The FreeBSD Foundation + * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,6 +36,7 @@ #include __FBSDID("$FreeBSD$"); +#include #include /* @@ -40,11 +45,5 @@ __FBSDID("$FreeBSD$"); int fls(int mask) { - int bit; - - if (mask == 0) - return (0); - for (bit = 1; mask != 1; bit++) - mask = (unsigned int)mask >> 1; - return (bit); + return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clz(mask)); } diff --git a/lib/libc/string/flsl.c b/lib/libc/string/flsl.c index 60370cf7d832..f5280b77a4e2 100644 --- a/lib/libc/string/flsl.c +++ b/lib/libc/string/flsl.c @@ -3,6 +3,11 @@ * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2023 The FreeBSD Foundation + + * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,6 +37,7 @@ #include __FBSDID("$FreeBSD$"); +#include #include /* @@ -40,11 +46,5 @@ __FBSDID("$FreeBSD$"); int flsl(long mask) { - int bit; - - if (mask == 0) - return (0); - for (bit = 1; mask != 1; bit++) - mask = (unsigned long)mask >> 1; - return (bit); + return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clzl(mask)); } diff --git a/lib/libc/string/flsll.c b/lib/libc/string/flsll.c index 275aaa0e2e15..ab504b8e592f 100644 --- a/lib/libc/string/flsll.c +++ b/lib/libc/string/flsll.c @@ -3,6 +3,10 @@ * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2023 The FreeBSD Foundation + * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,6 +36,7 @@ #include __FBSDID("$FreeBSD$"); +#include #include /* @@ -40,11 +45,5 @@ __FBSDID("$FreeBSD$"); int flsll(long long mask) { - int bit; - - if (mask == 0) - return (0); - for (bit = 1; mask != 1; bit++) - mask = (unsigned long long)mask >> 1; - return (bit); + return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clzll(mask)); }