Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761245AbbBIQpp (ORCPT ); Mon, 9 Feb 2015 11:45:45 -0500 Received: from ns.horizon.com ([71.41.210.147]:15921 "HELO ns.horizon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1760959AbbBIQpn (ORCPT ); Mon, 9 Feb 2015 11:45:43 -0500 Date: 9 Feb 2015 11:45:42 -0500 Message-ID: <20150209164542.10207.qmail@ns.horizon.com> From: "George Spelvin" To: linux@horizon.com, linux@rasmusvillemoes.dk Subject: Re: [PATCH v3 1/3] lib: find_*_bit reimplementation Cc: akpm@linux-foundation.org, chris@chris-wilson.co.uk, davem@davemloft.net, dborkman@redhat.com, hannes@stressinduktion.org, klimov.linux@gmail.com, laijs@cn.fujitsu.com, linux-kernel@vger.kernel.org, msalter@redhat.com, takahiro.akashi@linaro.org, tgraf@suug.ch, valentinrothberg@gmail.com, yury.norov@gmail.com In-Reply-To: <87wq3rs3lp.fsf@rasmusvillemoes.dk> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2011 Lines: 50 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 : 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 : 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 : 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. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/