diff mbox series

spi: use is_power_of_2 instead of hweight32 in spi_nor_write()

Message ID 20200313000638.10548-1-rasmus.villemoes@prevas.dk
State Accepted
Commit 7ddea756540b5adddfde3b890fa46a40c649e68f
Headers show
Series spi: use is_power_of_2 instead of hweight32 in spi_nor_write() | expand

Commit Message

Rasmus Villemoes March 13, 2020, 12:06 a.m. UTC
hweight32 is a somewhat expensive way to check for power-of-2. Use the
is_power_of_2 helper, which does the standard and cheap idiom
foo&(foo-1)==0.

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-96 (-96)
Function                                     old     new   delta
spi_nor_write                                388     292     -96

Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 drivers/mtd/spi/spi-nor-core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

Comments

Vignesh Raghavendra March 16, 2020, 9:48 a.m. UTC | #1
Hi,

On 13/03/20 5:36 am, Rasmus Villemoes wrote:
> hweight32 is a somewhat expensive way to check for power-of-2. Use the
> is_power_of_2 helper, which does the standard and cheap idiom
> foo&(foo-1)==0.
> 
> add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-96 (-96)
> Function                                     old     new   delta
> spi_nor_write                                388     292     -96
> 
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>

Acked-by: Vignesh Raghavendra <vigneshr at ti.com>

> ---
>  drivers/mtd/spi/spi-nor-core.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/drivers/mtd/spi/spi-nor-core.c b/drivers/mtd/spi/spi-nor-core.c
> index eb49a6c11c..4076646225 100644
> --- a/drivers/mtd/spi/spi-nor-core.c
> +++ b/drivers/mtd/spi/spi-nor-core.c
> @@ -1240,11 +1240,8 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
>  		 * If page_size is a power of two, the offset can be quickly
>  		 * calculated with an AND operation. On the other cases we
>  		 * need to do a modulus operation (more expensive).
> -		 * Power of two numbers have only one bit set and we can use
> -		 * the instruction hweight32 to detect if we need to do a
> -		 * modulus (do_div()) or not.
>  		 */
> -		if (hweight32(nor->page_size) == 1) {
> +		if (is_power_of_2(nor->page_size)) {
>  			page_offset = addr & (nor->page_size - 1);
>  		} else {
>  			u64 aux = addr;
>
diff mbox series

Patch

diff --git a/drivers/mtd/spi/spi-nor-core.c b/drivers/mtd/spi/spi-nor-core.c
index eb49a6c11c..4076646225 100644
--- a/drivers/mtd/spi/spi-nor-core.c
+++ b/drivers/mtd/spi/spi-nor-core.c
@@ -1240,11 +1240,8 @@  static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 		 * If page_size is a power of two, the offset can be quickly
 		 * calculated with an AND operation. On the other cases we
 		 * need to do a modulus operation (more expensive).
-		 * Power of two numbers have only one bit set and we can use
-		 * the instruction hweight32 to detect if we need to do a
-		 * modulus (do_div()) or not.
 		 */
-		if (hweight32(nor->page_size) == 1) {
+		if (is_power_of_2(nor->page_size)) {
 			page_offset = addr & (nor->page_size - 1);
 		} else {
 			u64 aux = addr;