From patchwork Mon Jul 31 21:27:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1815271 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=P6JdfsbC; dkim=pass (2048-bit key) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=P6JdfsbC; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RFBDx6LSRz1yfC for ; Tue, 1 Aug 2023 07:28:33 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1972186A8E; Mon, 31 Jul 2023 23:28:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.b="P6JdfsbC"; dkim=pass (2048-bit key) header.d=seco.com header.i=@seco.com header.b="P6JdfsbC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 976E986A85; Mon, 31 Jul 2023 23:28:27 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05hn2201.outbound.protection.outlook.com [52.100.174.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 254E1866F7 for ; Mon, 31 Jul 2023 23:28:25 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=P0igq5bIFBaf0c00d0Nme/PPlqwGrgW3NcZmMOBHc+U=; b=P6JdfsbCUjALdAIRsh/tl8Y+8pKKkDDY4WdJBo/+DvCCJwrQHAPyj2IfTxWPLk2OooCD+OHf7/GyDLrS3a8vEmd25SjzapK3wUjhfhy8b3Ubh+odMbzzp463g6iBVP0reW5EVioG3NkW4pcDMONnu7Rz9sxBxs5EF6ZwFR0HV+fEcF6iTErNag3wtneXlZDwBAhD00OQD9WgQOZmTkFfVtvweHH18JmGrqLNgK75V0DgWlmJ+eZbLRQ7AkBKGElPDux6Hah7D6Mb423CsZuziWbXl9sN1rf5bpVXUHeVSqfjUau3x/2kDAjA7jkbfMzRLMZu3g/4h+sQpPVnHHWRrQ== Received: from FR0P281CA0007.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:15::12) by DB9PR03MB8398.eurprd03.prod.outlook.com (2603:10a6:10:395::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.43; Mon, 31 Jul 2023 21:28:23 +0000 Received: from VI1EUR05FT028.eop-eur05.prod.protection.outlook.com (2603:10a6:d10:15:cafe::22) by FR0P281CA0007.outlook.office365.com (2603:10a6:d10:15::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6652.17 via Frontend Transport; Mon, 31 Jul 2023 21:28:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 20.160.56.87) smtp.mailfrom=seco.com; dkim=pass (signature was verified) header.d=seco.com;dmarc=pass action=none header.from=seco.com; Received-SPF: Pass (protection.outlook.com: domain of seco.com designates 20.160.56.87 as permitted sender) receiver=protection.outlook.com; client-ip=20.160.56.87; helo=inpost-eu.tmcas.trendmicro.com; pr=C Received: from inpost-eu.tmcas.trendmicro.com (20.160.56.87) by VI1EUR05FT028.mail.protection.outlook.com (10.233.243.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.44 via Frontend Transport; Mon, 31 Jul 2023 21:28:22 +0000 Received: from outmta (unknown [192.168.82.133]) by inpost-eu.tmcas.trendmicro.com (Trend Micro CAS) with ESMTP id 65DD52008008D; Mon, 31 Jul 2023 21:28:22 +0000 (UTC) Received: from EUR03-AM7-obe.outbound.protection.outlook.com (unknown [104.47.51.238]) by repre.tmcas.trendmicro.com (Trend Micro CAS) with ESMTPS id 3C1E62008006F; Mon, 31 Jul 2023 21:28:17 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a++mfv8FH5EGI+Sp0WFhyTOnlUjzyVzGZTwcc7T0zZ1Ur+6C7TfuuHEK+32nlX4M/gymPfy0N2xI7Zygr0xtVKxrKXcQVVSldq57UP43Q38okhtaAXE/Ze7iup2AYhOU69ilfHoRY9TuJlF7eC6vSMcFvqtvUuqGATOF8lMzgE8lJY/PiLAaPvJmSfH20vK9kDf1Orsxg+vnRoSdqr8KmyvRQQ7JMZAYUUZ0dtDB24GGPuj/2SUvT3U6C+l2JTOVWmuvv4og9miJbLHt8OYNsWwDeRdkBAnoIPNV1N/DIt9Pcqny4IuQHVpaAPoTWwW2HvpWZ4Mj+/7pw7WL3LpCNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=P0igq5bIFBaf0c00d0Nme/PPlqwGrgW3NcZmMOBHc+U=; b=BO+bIsS5U4JEBRkwyIaue66KJbNeW9HZ578A12fd1WAhwpH61ZANOtNEZFY9CuU6PUGCcm3XnVnExNKwFrX8M7E5GYd6Ht2GDgq6We16WaKUz6kqlP4Zb9DGLvPsIK8RJeKpd84U378ztyAkUXNOanomsxnP1e0FkK5ZI10oqeSWDgD7o8PRajKBZ35+t6sIyvYr7wmKt1bHmXn/zYxkqCRqj8UZqCIj4jphd8NhdTjEWAa1FBF0fXA+HX5dn2sMUTkVMThN3r4EsATVrp4NXfXIlBykdVK0i8r39Q3dUie+dkagKk868sXD+xhOXMBIHw0Pc6V/m+gaiKgtda5RaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=P0igq5bIFBaf0c00d0Nme/PPlqwGrgW3NcZmMOBHc+U=; b=P6JdfsbCUjALdAIRsh/tl8Y+8pKKkDDY4WdJBo/+DvCCJwrQHAPyj2IfTxWPLk2OooCD+OHf7/GyDLrS3a8vEmd25SjzapK3wUjhfhy8b3Ubh+odMbzzp463g6iBVP0reW5EVioG3NkW4pcDMONnu7Rz9sxBxs5EF6ZwFR0HV+fEcF6iTErNag3wtneXlZDwBAhD00OQD9WgQOZmTkFfVtvweHH18JmGrqLNgK75V0DgWlmJ+eZbLRQ7AkBKGElPDux6Hah7D6Mb423CsZuziWbXl9sN1rf5bpVXUHeVSqfjUau3x/2kDAjA7jkbfMzRLMZu3g/4h+sQpPVnHHWRrQ== Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=seco.com; Received: from DB9PR03MB8847.eurprd03.prod.outlook.com (2603:10a6:10:3dd::13) by AS2PR03MB9002.eurprd03.prod.outlook.com (2603:10a6:20b:5f4::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.43; Mon, 31 Jul 2023 21:28:15 +0000 Received: from DB9PR03MB8847.eurprd03.prod.outlook.com ([fe80::65d7:d7d8:711b:2514]) by DB9PR03MB8847.eurprd03.prod.outlook.com ([fe80::65d7:d7d8:711b:2514%5]) with mapi id 15.20.6631.043; Mon, 31 Jul 2023 21:28:15 +0000 From: Sean Anderson To: u-boot@lists.denx.de, Tom Rini Cc: Matthias Brugger , Peter Robinson , Eugen Hristev , Sean Anderson Subject: [PATCH] arm: Use builtins for ffs/fls Date: Mon, 31 Jul 2023 17:27:33 -0400 Message-Id: <20230731212734.7256-1-sean.anderson@seco.com> X-Mailer: git-send-email 2.40.1 X-ClientProxiedBy: SJ0PR03CA0249.namprd03.prod.outlook.com (2603:10b6:a03:3a0::14) To DB9PR03MB8847.eurprd03.prod.outlook.com (2603:10a6:10:3dd::13) MIME-Version: 1.0 X-MS-TrafficTypeDiagnostic: DB9PR03MB8847:EE_|AS2PR03MB9002:EE_|VI1EUR05FT028:EE_|DB9PR03MB8398:EE_ X-MS-Office365-Filtering-Correlation-Id: 131bec79-5c7e-4a46-cabd-08db920d116f X-TrendMicro-CAS-OUT-LOOP-IDENTIFIER: 656f966764b7fb185830381c646b41a1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 3+TJybpwFKJF2SsnBa1i4eXaXKE4JiYKu4Bnzz9yYb0LrTD5+QVZK7GdhD9zROVasL+EGQl4eestuYn//EgVrBLexgCIdSfwXtijzxHFPplCvL3J0G+qqBFlwbQk4ltZj9d1oSm3tmvohtaEmIYCOZAfXjKo/5WqQKt8hHjh8pftDJpS7keRfd5PzEODMbllMvM+d6Q2gu4i8Y/a6ktryf+CIlSsQv5KQrsi4nyalKScLx7pbQjmmsfkW1M5shh+kfmbEjLvqyJqQW18mXs4OI5oBdENcreWclHQMJy9PpfUlhh6G/eKqhJg066s7uuFh6sHy/YWhA3cfJWUlbvD4ApXwgRkGbB/zO93RzTZwTRamMhuRPDmztKiFOw6a24fHkvcyoC0FLtWXXPLwjfmeDfP8AwfJG+qqAHosnuttEZsEZd30zOu9AOICfRqlOkDsBfLowq4PRqCX/vc2Ki7ZOGgd1GY+645URQcY5/Ghv7VgFmBic3PpYz+AuE71G2WgDHus5BtpQqViyldZ608QBEXcZJSMaRuYa5hJYqJb+hELS7IKpK3s+iWsXdiehSfdT7e6rY1kAKLo7+6qDbQ4p8L422GNDzLyeZehV1/EFi1f77bxE6HUPQ7hPTv5TuW X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR03MB8847.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(366004)(136003)(346002)(376002)(39850400004)(396003)(451199021)(4326008)(41300700001)(5660300002)(66946007)(6916009)(8676002)(8936002)(66556008)(54906003)(478600001)(44832011)(316002)(66476007)(2906002)(107886003)(6486002)(6666004)(52116002)(6512007)(38350700002)(38100700002)(1076003)(6506007)(26005)(186003)(83380400001)(2616005)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR03MB9002 X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: VI1EUR05FT028.eop-eur05.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 0bad0aa3-377b-4de3-f560-08db920d0caf X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bS1rQkQna2cvapzX2mYrU6S3LAF8q74Jt+67bwrW/tqunVLYogOx/lhzKcdG2W3wMLOBedgNGz0oq0aXL/Ufm1rF4qr51PEJvktfMx2oYTtG0ZsFZUyIbH/ep2VzjM20wi3TJq028tw0sBtnMKCW+W22dMREHxgYbySyLTkfxip7Zb5nf5Ddk1ugXfTPyEEkKiz1633mE+Fok2kMMRgJMucodywV67WBP2kpOCrIm2Vsp3QbN1sO99E4m4Bx54y129f0JPtSiSJo5CUHgMktsoBZR1YxtSV6Rpd9Hcd3vJTm5vdJDwrcCiNvpnUkpyqCegKQcV75r7Nk2IaTnySzgxGooNi0g2nDo9r+EvPWPu7tPhgVHAcIsrbuhRdnyfdZsuXaVWsqkyjGnFso1mmiv183+6FKLfJV2AjQCpJ0hZdTBwhGLkFq7pt1kEbn5SR8vh7ZYEwEJ+g6BLZBIXidsckPF1KxEJEs3IIjmMYTxCIfJo4kjgIanEZmrrpVqwSLP0fC2sUONSWDgSv4tiXsZAJhVOKt3S2PpagHHnPbTkBQ3BHZIfVCtM/6+uCo1R2Xx8j3tKjP//m7wlqdjsIrJFTVUrSX/ytd85Nlz0QIjkB+xPvW4zLG0G/ZtoXeTxCiu4gmUUIHuoRJTUQoNC5lgB7NkWyAAA4OEcGAJkELQL8cYlXgCYH+bsuXX46YXoZUpF+X5DslgOLxXdop/0wHD0ymdHfey2LrU6QYOBf/enmoXV/a6Apz4XUjZW4v6OZrHqRNOKCcdVdF45sLZSR1LDi3D8OwWz3VteUENM7MFFI= X-Forefront-Antispam-Report: CIP:20.160.56.87; CTRY:NL; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:inpost-eu.tmcas.trendmicro.com; PTR:inpost-eu.tmcas.trendmicro.com; CAT:NONE; SFS:(13230028)(396003)(39850400004)(346002)(136003)(376002)(451199021)(5400799015)(82310400008)(46966006)(36840700001)(40470700004)(5660300002)(70586007)(6916009)(2906002)(70206006)(4326008)(44832011)(316002)(41300700001)(54906003)(6486002)(2616005)(34070700002)(6666004)(8676002)(6506007)(8936002)(107886003)(186003)(336012)(1076003)(26005)(36860700001)(47076005)(83380400001)(478600001)(7636003)(7596003)(356005)(82740400003)(36756003)(40480700001)(40460700003)(6512007)(86362001)(12100799036); DIR:OUT; SFP:1501; X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jul 2023 21:28:22.6732 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 131bec79-5c7e-4a46-cabd-08db920d116f X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=bebe97c3-6438-442e-ade3-ff17aa50e733; Ip=[20.160.56.87]; Helo=[inpost-eu.tmcas.trendmicro.com] X-MS-Exchange-CrossTenant-AuthSource: VI1EUR05FT028.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR03MB8398 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Since ARMv5, the clz instruction allows for efficient implementation of ffs/fls with builtins. Until ARMv7 (with Thumb-2), this instruction is only available in ARM mode. LTO makes it difficult to force specific functions to be in ARM mode, as it is effectively a form of very aggressive inlining. To work around this, fls/ffs are implemented in assembly for ARMv5 and ARMv6 when compiling U-Boot in Thumb mode. Overall, this saves around 75 bytes per call. Signed-off-by: Sean Anderson Reviewed-by: Tom Rini Tested-by: Tony Dinh --- I only tested this on ARMv8. If someone has an ARMv5 or ARMv6 board, please test this. arch/arm/include/asm/bitops.h | 27 ++++++++++++- arch/arm/lib/Makefile | 5 +++ arch/arm/lib/bitops.S | 45 ++++++++++++++++++++++ include/asm-generic/bitops/builtin-__ffs.h | 16 ++++++++ include/asm-generic/bitops/builtin-__fls.h | 16 ++++++++ include/asm-generic/bitops/builtin-ffs.h | 15 ++++++++ include/asm-generic/bitops/builtin-fls.h | 17 ++++++++ 7 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 arch/arm/lib/bitops.S create mode 100644 include/asm-generic/bitops/builtin-__ffs.h create mode 100644 include/asm-generic/bitops/builtin-__fls.h create mode 100644 include/asm-generic/bitops/builtin-ffs.h create mode 100644 include/asm-generic/bitops/builtin-fls.h diff --git a/arch/arm/include/asm/bitops.h b/arch/arm/include/asm/bitops.h index fa8548624a..8e897833bb 100644 --- a/arch/arm/include/asm/bitops.h +++ b/arch/arm/include/asm/bitops.h @@ -15,9 +15,34 @@ #ifndef __ASM_ARM_BITOPS_H #define __ASM_ARM_BITOPS_H +#if __LINUX_ARM_ARCH__ < 5 + #include #include #include + +#else + +#define PLATFORM_FFS +#define PLATFORM_FLS + +#if !IS_ENABLED(CONFIG_HAS_THUMB2) && CONFIG_IS_ENABLED(SYS_THUMB_BUILD) + +unsigned long __fls(unsigned long word); +unsigned long __ffs(unsigned long word); +int fls(unsigned int x); +int ffs(int x); + +#else + +#include +#include +#include +#include + +#endif +#endif + #include #ifdef __KERNEL__ @@ -113,7 +138,7 @@ static inline int test_bit(int nr, const void * addr) static inline int __ilog2(unsigned int x) { - return generic_fls(x) - 1; + return fls(x) - 1; } #define ffz(x) __ffs(~(x)) diff --git a/arch/arm/lib/Makefile b/arch/arm/lib/Makefile index 62cf80f373..b1bcd37466 100644 --- a/arch/arm/lib/Makefile +++ b/arch/arm/lib/Makefile @@ -113,6 +113,11 @@ AFLAGS_REMOVE_memset.o := -mthumb -mthumb-interwork AFLAGS_REMOVE_memcpy.o := -mthumb -mthumb-interwork AFLAGS_memset.o := -DMEMSET_NO_THUMB_BUILD AFLAGS_memcpy.o := -DMEMCPY_NO_THUMB_BUILD + +# This is only necessary to force ARM mode on THUMB1 targets. +ifneq ($(CONFIG_SYS_ARM_ARCH),4) +obj-y += bitops.o +endif endif endif diff --git a/arch/arm/lib/bitops.S b/arch/arm/lib/bitops.S new file mode 100644 index 0000000000..29d1524634 --- /dev/null +++ b/arch/arm/lib/bitops.S @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2023 Sean Anderson + * + * ARM bitops to call when using THUMB1, which doesn't have these instructions. + */ +#include +#include + +.pushsection .text.__fls +ENTRY(__fls) + clz r0, r0 + rsb r0, r0, #31 + ret lr +ENDPROC(__fls) +.popsection + +.pushsection .text.__ffs +ENTRY(__ffs) + rsb r3, r0, #0 + and r0, r0, r3 + clz r0, r0 + rsb r0, r0, #31 + ret lr +ENDPROC(__ffs) +.popsection + +.pushsection .text.fls +ENTRY(fls) + cmp r0, #0 + clzne r0, r0 + rsbne r0, r0, #32 + ret lr +ENDPROC(fls) +.popsection + +.pushsection .text.ffs +ENTRY(ffs) + rsb r3, r0, #0 + and r0, r0, r3 + clz r0, r0 + rsb r0, r0, #32 + ret lr +ENDPROC(ffs) +.popsection diff --git a/include/asm-generic/bitops/builtin-__ffs.h b/include/asm-generic/bitops/builtin-__ffs.h new file mode 100644 index 0000000000..87024da44d --- /dev/null +++ b/include/asm-generic/bitops/builtin-__ffs.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_BITOPS_BUILTIN___FFS_H_ +#define _ASM_GENERIC_BITOPS_BUILTIN___FFS_H_ + +/** + * __ffs - find first bit in word. + * @word: The word to search + * + * Undefined if no bit exists, so code should check against 0 first. + */ +static __always_inline unsigned long __ffs(unsigned long word) +{ + return __builtin_ctzl(word); +} + +#endif diff --git a/include/asm-generic/bitops/builtin-__fls.h b/include/asm-generic/bitops/builtin-__fls.h new file mode 100644 index 0000000000..43a5aa9afb --- /dev/null +++ b/include/asm-generic/bitops/builtin-__fls.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_BITOPS_BUILTIN___FLS_H_ +#define _ASM_GENERIC_BITOPS_BUILTIN___FLS_H_ + +/** + * __fls - find last (most-significant) set bit in a long word + * @word: the word to search + * + * Undefined if no set bit exists, so code should check against 0 first. + */ +static __always_inline unsigned long __fls(unsigned long word) +{ + return (sizeof(word) * 8) - 1 - __builtin_clzl(word); +} + +#endif diff --git a/include/asm-generic/bitops/builtin-ffs.h b/include/asm-generic/bitops/builtin-ffs.h new file mode 100644 index 0000000000..7b12932904 --- /dev/null +++ b/include/asm-generic/bitops/builtin-ffs.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_BITOPS_BUILTIN_FFS_H_ +#define _ASM_GENERIC_BITOPS_BUILTIN_FFS_H_ + +/** + * ffs - find first bit set + * @x: the word to search + * + * This is defined the same way as + * the libc and compiler builtin ffs routines, therefore + * differs in spirit from ffz (man ffs). + */ +#define ffs(x) __builtin_ffs(x) + +#endif diff --git a/include/asm-generic/bitops/builtin-fls.h b/include/asm-generic/bitops/builtin-fls.h new file mode 100644 index 0000000000..c8455cc288 --- /dev/null +++ b/include/asm-generic/bitops/builtin-fls.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_BITOPS_BUILTIN_FLS_H_ +#define _ASM_GENERIC_BITOPS_BUILTIN_FLS_H_ + +/** + * fls - find last (most-significant) bit set + * @x: the word to search + * + * This is defined the same way as ffs. + * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. + */ +static __always_inline int fls(unsigned int x) +{ + return x ? sizeof(x) * 8 - __builtin_clz(x) : 0; +} + +#endif