From nobody Thu Aug 18 01:46:43 2022 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 4M7SRR4kY0z4YvMZ; Thu, 18 Aug 2022 01:46:43 +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 4M7SRR4Fsdz3lSX; Thu, 18 Aug 2022 01:46:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660787203; 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=Ljwiywj034tuKWyuj72vVB/CFgsrPG4szO3z82QRD4Y=; b=PwC8ed3Dw83jZoZYA8UoMRuz9ZSuKGSWa5TNcm7/5XZrWxfbKR8DooumkRTb0GbR3WtVau wBRUAIYiFQ49Gg34GXshVUYuJHFjjs0ZXXm1sO6St28Naf8/pVqQSwSACYXK4DMyzUGXHy bj5yCbCq+9HRR1BwMnTLn0Jy/sTUmdMoXgsqp23zVJFS2Q7/FJzyLouYit0FmCNJPQyaJc nVYPO/Qw3v0QmnauqchUmXjKK4CYMryMDXoGufsatECLuUqQrB34+gpDc3UVCLHBXQi6nC p9jersYXlwLwQz/hQIxHpFubPXW/u5zxuYyb+HFUgEBiHfOp2myDGhdtRGZfxA== 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 4M7SRR3L6pz19tv; Thu, 18 Aug 2022 01:46:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 27I1khkK095041; Thu, 18 Aug 2022 01:46:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 27I1khuV095040; Thu, 18 Aug 2022 01:46:43 GMT (envelope-from git) Date: Thu, 18 Aug 2022 01:46:43 GMT Message-Id: <202208180146.27I1khuV095040@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: 92fc50b496ee - main - sys: Extract __bitcount* from sys/types.h to new sys/bitcount.h 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: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 92fc50b496ee8a5d0a0fc2025283e66351ae03d4 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660787203; 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=Ljwiywj034tuKWyuj72vVB/CFgsrPG4szO3z82QRD4Y=; b=iBJCNlH/SNFAamCY2bCrenB3tzbXOw86x17GzR+7KHJGXpbVTLKEOe5ZAQ8j4BCO5SUi+y lnryGMw/bXTrnkZE7UxXCjskqBmQ/y9IRnol20U5XFzUA8o+a3mnbzdHOP/ysT8qRwV9ut AGnbpSiaWlWxZ46a8aU+LEWhxHJwTM2K5HPK6qUqCo0BlH8O5S1Oz3eoeL01Zl4/UFdVzi GTmhQs2UxXBbHuw9oW37jZORNCkFFuMgU6TTUzo+fMpWxIhcqM5oSSt22ebMqv7tftMb4w aG+EET+eB8V+HPZcdb/XS/iiRUlee7bFKUOyb51kioiGSizuerHoITGwA7NyXA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1660787203; a=rsa-sha256; cv=none; b=nxMQd11slkwVOfo3yCzJpNagil+xepQp//a4DNPW9ryvVJOc5qNweeWSXzldt2UVM+eq5J rre7F7DnFJDJjutGdUZJ+l3iE9GZO1mSicTeDXjiqBDLHt2Oal5EwghTp+nBITnypcNlgv jCNjZ0kWhHokO9dsG72vc/09tYdOYtKSi4412JQ61g0v7SQ0/N65B58UsvBIY06KtU1HET kWcxn1+rogmwS0jhJZFwcgQPjdFcRqD8m+K2Bba9rldUr7cJisIszso4DhfvSIZfh0AATO sqV5rotSKhyE54qxE5dkXcomZ+qn/uHXUkapRvj3F4SPNqloO4nFk3IvxfLGhg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=92fc50b496ee8a5d0a0fc2025283e66351ae03d4 commit 92fc50b496ee8a5d0a0fc2025283e66351ae03d4 Author: Jessica Clarke AuthorDate: 2022-08-18 01:46:27 +0000 Commit: Jessica Clarke CommitDate: 2022-08-18 01:46:27 +0000 sys: Extract __bitcount* from sys/types.h to new sys/bitcount.h This will allow the code to be reused by the cross-build sys/types.h wrapper in order to provide the APIs for greater compatibility. This also provides a path towards eventually removing the definitions from sys/types.h altogether if so desired by gradually migrating users to including sys/bitcount.h explicitly, but that is not the primary goal here. Note that the copyright header is a direct copu of sys/types.h's given that's where this code comes from. This could be replaced in future with a more specific one restricted to just the code in question, depending on what the copyright for that code is. Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D36132 --- sys/sys/bitcount.h | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sys/sys/types.h | 63 +------------------------------ 2 files changed, 108 insertions(+), 62 deletions(-) diff --git a/sys/sys/bitcount.h b/sys/sys/bitcount.h new file mode 100644 index 000000000000..7c792506bce5 --- /dev/null +++ b/sys/sys/bitcount.h @@ -0,0 +1,107 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 1982, 1986, 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * 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. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS 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. + * + * @(#)types.h 8.6 (Berkeley) 2/19/95 + * $FreeBSD$ + */ + +#ifndef _SYS_BITCOUNT_H_ +#define _SYS_BITCOUNT_H_ + +#include + +#ifdef __POPCNT__ +#define __bitcount64(x) __builtin_popcountll((__uint64_t)(x)) +#define __bitcount32(x) __builtin_popcount((__uint32_t)(x)) +#define __bitcount16(x) __builtin_popcount((__uint16_t)(x)) +#define __bitcountl(x) __builtin_popcountl((unsigned long)(x)) +#define __bitcount(x) __builtin_popcount((unsigned int)(x)) +#else +/* + * Population count algorithm using SWAR approach + * - "SIMD Within A Register". + */ +static __inline __uint16_t +__bitcount16(__uint16_t _x) +{ + + _x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1); + _x = (_x & 0x3333) + ((_x & 0xcccc) >> 2); + _x = (_x + (_x >> 4)) & 0x0f0f; + _x = (_x + (_x >> 8)) & 0x00ff; + return (_x); +} + +static __inline __uint32_t +__bitcount32(__uint32_t _x) +{ + + _x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1); + _x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2); + _x = (_x + (_x >> 4)) & 0x0f0f0f0f; + _x = (_x + (_x >> 8)); + _x = (_x + (_x >> 16)) & 0x000000ff; + return (_x); +} + +#ifdef __LP64__ +static __inline __uint64_t +__bitcount64(__uint64_t _x) +{ + + _x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1); + _x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2); + _x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f; + _x = (_x + (_x >> 8)); + _x = (_x + (_x >> 16)); + _x = (_x + (_x >> 32)) & 0x000000ff; + return (_x); +} + +#define __bitcountl(x) __bitcount64((unsigned long)(x)) +#else +static __inline __uint64_t +__bitcount64(__uint64_t _x) +{ + + return (__bitcount32(_x >> 32) + __bitcount32(_x)); +} + +#define __bitcountl(x) __bitcount32((unsigned long)(x)) +#endif +#define __bitcount(x) __bitcount32((unsigned int)(x)) +#endif + +#endif /* !_SYS_BITCOUNT_H_ */ diff --git a/sys/sys/types.h b/sys/sys/types.h index d37ab8b823dc..5db5679b11b5 100644 --- a/sys/sys/types.h +++ b/sys/sys/types.h @@ -310,68 +310,7 @@ typedef _Bool bool; * The following are all things that really shouldn't exist in this header, * since its purpose is to provide typedefs, not miscellaneous doodads. */ - -#ifdef __POPCNT__ -#define __bitcount64(x) __builtin_popcountll((__uint64_t)(x)) -#define __bitcount32(x) __builtin_popcount((__uint32_t)(x)) -#define __bitcount16(x) __builtin_popcount((__uint16_t)(x)) -#define __bitcountl(x) __builtin_popcountl((unsigned long)(x)) -#define __bitcount(x) __builtin_popcount((unsigned int)(x)) -#else -/* - * Population count algorithm using SWAR approach - * - "SIMD Within A Register". - */ -static __inline __uint16_t -__bitcount16(__uint16_t _x) -{ - - _x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1); - _x = (_x & 0x3333) + ((_x & 0xcccc) >> 2); - _x = (_x + (_x >> 4)) & 0x0f0f; - _x = (_x + (_x >> 8)) & 0x00ff; - return (_x); -} - -static __inline __uint32_t -__bitcount32(__uint32_t _x) -{ - - _x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1); - _x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2); - _x = (_x + (_x >> 4)) & 0x0f0f0f0f; - _x = (_x + (_x >> 8)); - _x = (_x + (_x >> 16)) & 0x000000ff; - return (_x); -} - -#ifdef __LP64__ -static __inline __uint64_t -__bitcount64(__uint64_t _x) -{ - - _x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1); - _x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2); - _x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f; - _x = (_x + (_x >> 8)); - _x = (_x + (_x >> 16)); - _x = (_x + (_x >> 32)) & 0x000000ff; - return (_x); -} - -#define __bitcountl(x) __bitcount64((unsigned long)(x)) -#else -static __inline __uint64_t -__bitcount64(__uint64_t _x) -{ - - return (__bitcount32(_x >> 32) + __bitcount32(_x)); -} - -#define __bitcountl(x) __bitcount32((unsigned long)(x)) -#endif -#define __bitcount(x) __bitcount32((unsigned int)(x)) -#endif +#include #if __BSD_VISIBLE