lkml.org 
[lkml]   [2015]   [Feb]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v3 1/3] lib: find_*_bit reimplementation
Sorry, I screwed up the bit-twiddling while messing with various options.
I was trying to get size == 32 to work; that should have been:

> tmp &= (2UL << ((size-1) % BITS_PER_LONG)) - 1; /* Mask last word */

And you're right that LAST_WORD_MASK is a good wrapper.

Vasrious working solutions include:
#define LAST_WORD_MASK(bits) ((2UL << (bits-1) % BITS_PER_LONG) - 1)
#define LAST_WORD_MASK(bits) ~(~0UL << bits % BITS_PER_LONG)
#define LAST_WORD_MASK(bits) (~0UL >> -bits % BITS_PER_LONG)

I'm not sure which generates the nicest code. It's 4 instructions
each way, with the last being 1 byte smaller:

0000000000000000 <lwm1>:
0: 8d 4f ff lea -0x1(%rdi),%ecx
3: b8 02 00 00 00 mov $0x2,%eax
8: 48 d3 e0 shl %cl,%rax
b: 48 83 e8 01 sub $0x1,%rax
f: c3 retq

0000000000000010 <lwm2>:
10: 48 c7 c0 ff ff ff ff mov $0xffffffffffffffff,%rax
17: 89 f9 mov %edi,%ecx
19: 48 d3 e0 shl %cl,%rax
1c: 48 f7 d0 not %rax
1f: c3 retq

0000000000000020 <lwm3>:
20: 89 f9 mov %edi,%ecx
22: f7 d9 neg %ecx
24: 48 c7 c0 ff ff ff ff mov $0xffffffffffffffff,%rax
2b: 48 d3 e8 shr %cl,%rax
2e: c3 retq


> Also, I think it is best to handle size==0 appropriately, meaning that
> one cannot dereference addr in any way (and certainly not addr[-1]).

Ah, okay; l I figured that was a safe case to omit. But your solution is nicer
than mine overall.

It may be that omitting the mask *is* safe, but it's a lot of wading through
callers to prove it.


\
 
 \ /
  Last update: 2015-02-09 18:01    [W:0.112 / U:0.124 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site