diff mbox series

[17/18] bcache: make bset_search_tree() be more understandable

Message ID 20190604155330.107927-2-colyli@suse.de (mailing list archive)
State New, archived
Headers show
Series bcache fixes before Linux v5.3 | expand

Commit Message

Coly Li June 4, 2019, 3:53 p.m. UTC
The purpose of following code in bset_search_tree() is to avoid a branch
instruction,
 994         if (likely(f->exponent != 127))
 995                 n = j * 2 + (((unsigned int)
 996                               (f->mantissa -
 997                                bfloat_mantissa(search, f))) >> 31);
 998         else
 999                 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
1000                         ? j * 2
1001                         : j * 2 + 1;

This piece of code is not very clear to understand, even when I tried to
add code comment for it, I made mistake. This patch removes the implict
bit operation and uses explicit branch to calculate next location in
binary tree search.

Signed-off-by: Coly Li <colyli@suse.de>
---
 drivers/md/bcache/bset.c | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)

Comments

Christoph Hellwig June 5, 2019, 5:43 a.m. UTC | #1
> -			n = j * 2 + (((unsigned int)
> -				      (f->mantissa -
> -				       bfloat_mantissa(search, f))) >> 31);
> +			n = (f->mantissa >= bfloat_mantissa(search, f))
> +				? j * 2
> +				: j * 2 + 1;

If you really want to make it more readable a good old if else would
help a lot.

>  		else
>  			n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
>  				? j * 2

Same here.
Coly Li June 5, 2019, 5:45 a.m. UTC | #2
On 2019/6/5 1:43 下午, Christoph Hellwig wrote:
>> -			n = j * 2 + (((unsigned int)
>> -				      (f->mantissa -
>> -				       bfloat_mantissa(search, f))) >> 31);
>> +			n = (f->mantissa >= bfloat_mantissa(search, f))
>> +				? j * 2
>> +				: j * 2 + 1;
> 
> If you really want to make it more readable a good old if else would
> help a lot.
> 
>>  		else
>>  			n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
>>  				? j * 2
> 
> Same here.
> 

Hi Christoph,

Thanks for the hint, will handle it soon.
diff mbox series

Patch

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index eedaf0f3e3f0..32e2e4d8fa6c 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -965,21 +965,10 @@  static struct bset_search_iter bset_search_tree(struct bset_tree *t,
 		j = n;
 		f = &t->tree[j];
 
-		/*
-		 * Similar bit trick, use subtract operation to avoid a branch
-		 * instruction.
-		 *
-		 * n = (f->mantissa > bfloat_mantissa())
-		 *	? j * 2
-		 *	: j * 2 + 1;
-		 *
-		 * We need to subtract 1 from f->mantissa for the sign bit trick
-		 * to work  - that's done in make_bfloat()
-		 */
 		if (likely(f->exponent != 127))
-			n = j * 2 + (((unsigned int)
-				      (f->mantissa -
-				       bfloat_mantissa(search, f))) >> 31);
+			n = (f->mantissa >= bfloat_mantissa(search, f))
+				? j * 2
+				: j * 2 + 1;
 		else
 			n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
 				? j * 2