Skip to contentSkip to conversation

mtd: spi-nor: use is_power_of_2()

PATCH Applied Michael Walle 3 participants 3 messages Mar 5, 2021 – Mar 8, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: spi-nor: use is_power_of_2()

There is already a function to check if an integer is a power of 2. Use it.

---
 drivers/mtd/spi-nor/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/mtd/spi-nor/core.c +1 -4
index 0522304f52fa..4a315cb1c4db 100644
@@ -2336,11 +2336,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 {
 			uint64_t aux = addr;
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Michael Walle
Quoted 15 lines · Michael Walle

There is already a function to check if an integer is a power of 2. Use it.

---
 drivers/mtd/spi-nor/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/mtd/spi-nor/core.c +0 -0
index 0522304f52fa..4a315cb1c4db 100644
@@ -2336,11 +2336,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

drivers/mtd/spi-nor/core.c static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len, · line 2343

Quoted 10 lines
drivers/mtd/spi-nor/core.c +1 -4
 		 * 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 {
 			uint64_t aux = addr;
Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments Inc.