Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754624AbbBKWOO (ORCPT ); Wed, 11 Feb 2015 17:14:14 -0500 Received: from mail-la0-f51.google.com ([209.85.215.51]:40402 "EHLO mail-la0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753990AbbBKWOM (ORCPT ); Wed, 11 Feb 2015 17:14:12 -0500 From: Rasmus Villemoes To: "George Spelvin" 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 Subject: Re: [PATCH v3 1/3] lib: find_*_bit reimplementation Organization: D03 References: <20150209164542.10207.qmail@ns.horizon.com> X-Hashcash: 1:20:150211:laijs@cn.fujitsu.com::opJZ0Zz2+YrX/1g5:000000000000000000000000000000000000000001GPa X-Hashcash: 1:20:150211:chris@chris-wilson.co.uk::mFw6VlzjaifCHkd4:000000000000000000000000000000000000022Hc X-Hashcash: 1:20:150211:msalter@redhat.com::NC4gXdvx7FQ+hzxf:00000000000000000000000000000000000000000001z3C X-Hashcash: 1:20:150211:akpm@linux-foundation.org::tknEYjxwY8h05394:0000000000000000000000000000000000002BBW X-Hashcash: 1:20:150211:tgraf@suug.ch::H+qfZ6JNoaSApRon:00002QTO X-Hashcash: 1:20:150211:dborkman@redhat.com::GGI6/8ULWIpp0lFh:0000000000000000000000000000000000000000003pPN X-Hashcash: 1:20:150211:linux@horizon.com::69sFQb+6LySWr3ay:000000000000000000000000000000000000000000004P8W X-Hashcash: 1:20:150211:takahiro.akashi@linaro.org::kiKJ5NYYkn01UGIg:0000000000000000000000000000000000051TC X-Hashcash: 1:20:150211:klimov.linux@gmail.com::6eYUIre+Ypem4izu:0000000000000000000000000000000000000006Yc9 X-Hashcash: 1:20:150211:valentinrothberg@gmail.com::/OG54mnK3D8Ax3Z2:000000000000000000000000000000000006Ijf X-Hashcash: 1:20:150211:yury.norov@gmail.com::xr0tNv3cdU7XTtzt:0000000000000000000000000000000000000000082aJ X-Hashcash: 1:20:150211:linux-kernel@vger.kernel.org::F3YjYEmgMDpFxHiC:0000000000000000000000000000000007OJH X-Hashcash: 1:20:150211:hannes@stressinduktion.org::08D8iisGwPTb7jic:000000000000000000000000000000000008WaN X-Hashcash: 1:20:150211:davem@davemloft.net::S2YDoGw/07XTMbQA:0000000000000000000000000000000000000000009HCK Date: Wed, 11 Feb 2015 23:14:07 +0100 In-Reply-To: <20150209164542.10207.qmail@ns.horizon.com> (George Spelvin's message of "9 Feb 2015 11:45:42 -0500") Message-ID: <877fvow0y8.fsf@rasmusvillemoes.dk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2246 Lines: 55 [for some reason google decided to put this in my spam folder, hrmpf] On Mon, Feb 09 2015, "George Spelvin" wrote: > 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. > Well, it's not my invention, I just misremembered the name. linux/bitmap.h already exposes BITMAP_LAST_WORD_MASK. > 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) Incidentally, I had a patch lying around for replacing BITMAP_LAST_WORD_MASK by something like the last of these (it is currently using a ?:). But to allow bits to have signed type it is safer to spell it #define BITMAP_LAST_WORD_MASK(bits) (~0UL >> ((-(bits)) & (BITS_PER_LONG-1))) [also adding lots of parentheses so I don't have to worry about precedence]. > I'm not sure which generates the nicest code. It's 4 instructions > each way, with the last being 1 byte smaller: I think one would have to look at effects on real code; when just compiling a function doing nothing but this gcc has to use specific registers for in and out. >> 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. I think generic library code like this should provide both safety checks, and only if some true performance bottleneck is found can one start looking at implementing __shortcuts which have further constraints on the caller. Rasmus -- 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/