From nobody Mon Jun 24 07:27:52 2024 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 4W70145RVPz5NWyx; Mon, 24 Jun 2024 07:27:52 +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 4W70143300z4TjL; Mon, 24 Jun 2024 07:27:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1719214072; 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=zgtgGQ7Jtl9ZplG3NDL0uy0jRcd6AG5g2RGSygmSFn8=; b=DiSrgt9RtxY6Cn+H/o0FuehPZStgLZeSFeLeRW5lBXdyR9gwPD8N1MT1E33a3pFkSuYHUM 7e6jyXuAgYEMCAn3xvy52bTowIKJepUDkzT2fomKmctOzenebbPgMhih62GeCF4luxJFQQ /K8xe89X9iOhJpCHA0Dj3rwjjM3CDEBvVKhO1fCtrr8PiWWpT3yhn0Xn2V5m3UxIjJILzI aOSqL0aMFxoHNedXmleX5s0OmSljdKkTtxRrTkX3SAT1pxnyTEP7gSqabYQql6vlaeE7kO IVAscymjjGTD+iw3DeLn/RMWNu6l6029TdTT++okasaM3AutEf8yihZoK+yO5g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1719214072; a=rsa-sha256; cv=none; b=smtiJzoiPk5tsaFkJzE9FOWqFqF7Nd6b86W6IGAckGXnwEWcKl72Y2Xq6Pqo1YrPIEbplS ELhAcDnzyKLMh1lCtL8EUdwQm+L12ZoQ7W9mFl/+YgzcQPEYx5rzxx0ySoDz4u7MnKK2PX vsMxRvjsrRoQYsOtLiOx3LSZwkNoepXjKmCygL1/SzYThzEXkdc88npI3oODoprc6dslLm K6aV4Poz7imvBH96neDl9uA0nzxL8hRqz2zyfJ2lUwbfZqJxK7aHPl8Xe2n88Jpc99ShDS Q3m5RyTqoNu+Btz2nmYR+jDbwEzg5CYUJoGkWjAAKXmoBMkErgHnPCyMU+leug== 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=1719214072; 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=zgtgGQ7Jtl9ZplG3NDL0uy0jRcd6AG5g2RGSygmSFn8=; b=CARz0stJp7QdsG2jHcWp2nONVzGUWQjrcYx/+uVZzptyHgGm6y3gHdVHcbD1xpMhgMHlJ4 RCqcVH1iwAupfjwW1W57SNhIlIT/6x21uiIpO8HXhQRguqi6oq+LdvJom+YOsfEycvK19I ma71ktQvW8gaqf4atjzcGOGXQrLJzFcLv3u0pSJ6cbpBhIdpLGaPNd2IruyvD89yLtg4Cm nrc/HLcH8zM8yH94lvs3BZjGi5tpiHEGvYJKLH+19AmcjMG7z2D2WoIVCJTI50aZDzNSjh vaQxhLPmCzqhTBKyP6SJ11h7UcsTpE1e+c+EGJ0YWQm93gNP6YEAgRt/05Sk0w== 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 4W70142Pjhz18Rh; Mon, 24 Jun 2024 07:27:52 +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 45O7RqTo044343; Mon, 24 Jun 2024 07:27:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 45O7Rqvx044340; Mon, 24 Jun 2024 07:27:52 GMT (envelope-from git) Date: Mon, 24 Jun 2024 07:27:52 GMT Message-Id: <202406240727.45O7Rqvx044340@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Doug Moore Subject: git: c8b0c33b03ac - main - log2: move log2 functions from linuxkpi to libkern 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dougm X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c8b0c33b03ac072413b27bed2bdae2ae27426f3a Auto-Submitted: auto-generated The branch main has been updated by dougm: URL: https://cgit.FreeBSD.org/src/commit/?id=c8b0c33b03ac072413b27bed2bdae2ae27426f3a commit c8b0c33b03ac072413b27bed2bdae2ae27426f3a Author: Doug Moore AuthorDate: 2024-06-24 07:00:31 +0000 Commit: Doug Moore CommitDate: 2024-06-24 07:00:31 +0000 log2: move log2 functions from linuxkpi to libkern Linux has a header file that defines an ilog2 function and some simple functions/macros that use it: roundup_pow_of_two, is_power_of_2, rounddown_pow_of_two, and order_base_2. This change moves three of those simple functions (all but is_power_of_2) from linuxkpi to libkern. It also deletes a few implementations of these functions that have previously been copied into code for various device drivers, so that they can use the libkern version. The is_power_of_2 macro was not moved because powerof2 in param.h provides almost the same service already (except that they disagree about whether 0 is a power of two). Since the linux definitions of these functions were copied into FreeBSD 11 years ago, linux has improved them, and this change provides those improvements. In particular, a giant table of log values for evaluating ilog2 for constant values is no longer necessary. Reviewed by: alc, markj (previous version) Differential Revision: https://reviews.freebsd.org/D45536 --- sys/compat/linuxkpi/common/include/linux/log2.h | 23 ++----- sys/dev/drm2/drm_os_freebsd.h | 7 --- sys/dev/mana/gdma_util.h | 12 ---- sys/dev/qlnx/qlnxe/bcm_osal.h | 18 ------ sys/sys/libkern.h | 82 ++++--------------------- 5 files changed, 15 insertions(+), 127 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/log2.h b/sys/compat/linuxkpi/common/include/linux/log2.h index 2d54c75c7c23..660e9adb6fa9 100644 --- a/sys/compat/linuxkpi/common/include/linux/log2.h +++ b/sys/compat/linuxkpi/common/include/linux/log2.h @@ -33,24 +33,9 @@ #include -static inline unsigned long -roundup_pow_of_two(unsigned long x) -{ - return (1UL << flsl(x - 1)); -} - -static inline int -is_power_of_2(unsigned long n) -{ - return (n == roundup_pow_of_two(n)); -} - -static inline unsigned long -rounddown_pow_of_two(unsigned long x) -{ - return (1UL << (flsl(x) - 1)); -} - -#define order_base_2(x) ilog2(roundup_pow_of_two(x)) +#define is_power_of_2(n) ({ \ + __typeof(n) _n = (n); \ + _n != 0 && (_n & (_n - 1)) == 0; \ +}) #endif /* _LINUXKPI_LINUX_LOG2_H_ */ diff --git a/sys/dev/drm2/drm_os_freebsd.h b/sys/dev/drm2/drm_os_freebsd.h index 58523a33da2a..b2a2e82b748b 100644 --- a/sys/dev/drm2/drm_os_freebsd.h +++ b/sys/dev/drm2/drm_os_freebsd.h @@ -234,13 +234,6 @@ typedef void irqreturn_t; #define div_u64(n, d) ((n) / (d)) #define hweight32(i) bitcount32(i) -static inline unsigned long -roundup_pow_of_two(unsigned long x) -{ - - return (1UL << flsl(x - 1)); -} - /** * ror32 - rotate a 32-bit value right * @word: value to rotate diff --git a/sys/dev/mana/gdma_util.h b/sys/dev/mana/gdma_util.h index 37c2653d5ec9..1efa315bbcfe 100644 --- a/sys/dev/mana/gdma_util.h +++ b/sys/dev/mana/gdma_util.h @@ -170,18 +170,6 @@ find_first_zero_bit(const unsigned long *p, unsigned long max) return (max); } -static inline unsigned long -roundup_pow_of_two(unsigned long x) -{ - return (1UL << flsl(x - 1)); -} - -static inline int -is_power_of_2(unsigned long n) -{ - return (n == roundup_pow_of_two(n)); -} - struct completion { unsigned int done; struct mtx lock; diff --git a/sys/dev/qlnx/qlnxe/bcm_osal.h b/sys/dev/qlnx/qlnxe/bcm_osal.h index c820532c9e0a..7148fd3f6215 100644 --- a/sys/dev/qlnx/qlnxe/bcm_osal.h +++ b/sys/dev/qlnx/qlnxe/bcm_osal.h @@ -102,24 +102,6 @@ extern void qlnx_vf_flr_update(void *p_hwfn); #ifndef QLNX_RDMA -static __inline unsigned long -roundup_pow_of_two(unsigned long x) -{ - return (1UL << flsl(x - 1)); -} - -static __inline int -is_power_of_2(unsigned long n) -{ - return (n == roundup_pow_of_two(n)); -} - -static __inline unsigned long -rounddown_pow_of_two(unsigned long x) -{ - return (1UL << (flsl(x) - 1)); -} - #define max_t(type, val1, val2) \ ((type)(val1) > (type)(val2) ? (type)(val1) : (val2)) #define min_t(type, val1, val2) \ diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h index 6dc4bc87c3fe..7d2e24f1263b 100644 --- a/sys/sys/libkern.h +++ b/sys/sys/libkern.h @@ -221,77 +221,17 @@ ilog2_long_long(long long n) unsigned long long: ilog2_long_long \ )(n) -#define ilog2(n) \ -( \ - __builtin_constant_p(n) ? ( \ - (n) < 1 ? -1 : \ - (n) & (1ULL << 63) ? 63 : \ - (n) & (1ULL << 62) ? 62 : \ - (n) & (1ULL << 61) ? 61 : \ - (n) & (1ULL << 60) ? 60 : \ - (n) & (1ULL << 59) ? 59 : \ - (n) & (1ULL << 58) ? 58 : \ - (n) & (1ULL << 57) ? 57 : \ - (n) & (1ULL << 56) ? 56 : \ - (n) & (1ULL << 55) ? 55 : \ - (n) & (1ULL << 54) ? 54 : \ - (n) & (1ULL << 53) ? 53 : \ - (n) & (1ULL << 52) ? 52 : \ - (n) & (1ULL << 51) ? 51 : \ - (n) & (1ULL << 50) ? 50 : \ - (n) & (1ULL << 49) ? 49 : \ - (n) & (1ULL << 48) ? 48 : \ - (n) & (1ULL << 47) ? 47 : \ - (n) & (1ULL << 46) ? 46 : \ - (n) & (1ULL << 45) ? 45 : \ - (n) & (1ULL << 44) ? 44 : \ - (n) & (1ULL << 43) ? 43 : \ - (n) & (1ULL << 42) ? 42 : \ - (n) & (1ULL << 41) ? 41 : \ - (n) & (1ULL << 40) ? 40 : \ - (n) & (1ULL << 39) ? 39 : \ - (n) & (1ULL << 38) ? 38 : \ - (n) & (1ULL << 37) ? 37 : \ - (n) & (1ULL << 36) ? 36 : \ - (n) & (1ULL << 35) ? 35 : \ - (n) & (1ULL << 34) ? 34 : \ - (n) & (1ULL << 33) ? 33 : \ - (n) & (1ULL << 32) ? 32 : \ - (n) & (1ULL << 31) ? 31 : \ - (n) & (1ULL << 30) ? 30 : \ - (n) & (1ULL << 29) ? 29 : \ - (n) & (1ULL << 28) ? 28 : \ - (n) & (1ULL << 27) ? 27 : \ - (n) & (1ULL << 26) ? 26 : \ - (n) & (1ULL << 25) ? 25 : \ - (n) & (1ULL << 24) ? 24 : \ - (n) & (1ULL << 23) ? 23 : \ - (n) & (1ULL << 22) ? 22 : \ - (n) & (1ULL << 21) ? 21 : \ - (n) & (1ULL << 20) ? 20 : \ - (n) & (1ULL << 19) ? 19 : \ - (n) & (1ULL << 18) ? 18 : \ - (n) & (1ULL << 17) ? 17 : \ - (n) & (1ULL << 16) ? 16 : \ - (n) & (1ULL << 15) ? 15 : \ - (n) & (1ULL << 14) ? 14 : \ - (n) & (1ULL << 13) ? 13 : \ - (n) & (1ULL << 12) ? 12 : \ - (n) & (1ULL << 11) ? 11 : \ - (n) & (1ULL << 10) ? 10 : \ - (n) & (1ULL << 9) ? 9 : \ - (n) & (1ULL << 8) ? 8 : \ - (n) & (1ULL << 7) ? 7 : \ - (n) & (1ULL << 6) ? 6 : \ - (n) & (1ULL << 5) ? 5 : \ - (n) & (1ULL << 4) ? 4 : \ - (n) & (1ULL << 3) ? 3 : \ - (n) & (1ULL << 2) ? 2 : \ - (n) & (1ULL << 1) ? 1 : \ - (n) & (1ULL << 0) ? 0 : \ - -1) : \ - ilog2_var(n) \ -) +#define ilog2_const(n) \ + (8 * (int)sizeof(unsigned long long) - 1 - \ + __builtin_clzll(n)) + +#define ilog2(n) (__builtin_constant_p(n) ? ilog2_const(n) : ilog2_var(n)) +#define rounddown_pow_of_two(n) ((__typeof(n))1 << ilog2(n)) +#define order_base_2(n) ({ \ + __typeof(n) _n = (n); \ + _n == 1 ? 0 : 1 + ilog2(_n - 1); \ +}) +#define roundup_pow_of_two(n) ((__typeof(n))1 << order_base_2(n)) #define bitcount64(x) __bitcount64((uint64_t)(x)) #define bitcount32(x) __bitcount32((uint32_t)(x))