2020-05-23 15:54:19

by chengkaitao

[permalink] [raw]
Subject: [PATCH] linux/blkdev.h: Use ilog2() directly in blksize_bits()

blksize_bits() can be achieved through ilog2(), and ilog2() is
more efficient.

Signed-off-by: Kaitao Cheng <[email protected]>
---
include/linux/blkdev.h | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 2b33166b9daf..cce96abac4d4 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1505,12 +1505,7 @@ static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
/* assumes size > 256 */
static inline unsigned int blksize_bits(unsigned int size)
{
- unsigned int bits = 8;
- do {
- bits++;
- size >>= 1;
- } while (size > 256);
- return bits;
+ return ilog2(size);
}

static inline unsigned int block_size(struct block_device *bdev)
--
2.20.1


2020-05-23 17:12:13

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH] linux/blkdev.h: Use ilog2() directly in blksize_bits()

On 5/23/20 9:50 AM, Kaitao Cheng wrote:
> blksize_bits() can be achieved through ilog2(), and ilog2() is
> more efficient.

I like the simplification, but do you have any results to back up
that claim? Is the generated code shorter? Runs faster?

--
Jens Axboe

2020-05-23 18:36:12

by Bart Van Assche

[permalink] [raw]
Subject: Re: [PATCH] linux/blkdev.h: Use ilog2() directly in blksize_bits()

On 2020-05-23 08:50, Kaitao Cheng wrote:
> blksize_bits() can be achieved through ilog2(), and ilog2() is
> more efficient.

If Jens agrees, how about removing the blksize_bits() function entirely
and to make all callers use ilog2() instead of blksize_bits()?

Thanks,

Bart.

2020-05-23 18:53:10

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH] linux/blkdev.h: Use ilog2() directly in blksize_bits()

On 5/23/20 12:33 PM, Bart Van Assche wrote:
> On 2020-05-23 08:50, Kaitao Cheng wrote:
>> blksize_bits() can be achieved through ilog2(), and ilog2() is
>> more efficient.
>
> If Jens agrees, how about removing the blksize_bits() function entirely
> and to make all callers use ilog2() instead of blksize_bits()?

Yeah, if we are going this path, then let's just kill the function
completely.

--
Jens Axboe