From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933511AbYEUKtT (ORCPT ); Wed, 21 May 2008 06:49:19 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1755220AbYEUKtK (ORCPT ); Wed, 21 May 2008 06:49:10 -0400 Received: from bombadil.infradead.org ([18.85.46.34]:49579 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754594AbYEUKtK (ORCPT ); Wed, 21 May 2008 06:49:10 -0400 Subject: Re: [PATCH] consolidate all within() implementations From: Peter Zijlstra To: Peter 1 Oberparleiter Cc: Andrew Morton , Harvey Harrison , linux-kernel@vger.kernel.org In-Reply-To: References: Content-Type: text/plain Date: Wed, 21 May 2008 12:48:52 +0200 Message-Id: <1211366932.6463.81.camel@lappy.programming.kicks-ass.net> Mime-Version: 1.0 X-Mailer: Evolution 2.22.1 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2008-05-21 at 12:33 +0200, Peter 1 Oberparleiter wrote: > Peter Zijlstra wrote on 21.05.2008 12:04:26: > > > +static inline int addr_within_len(const void *addr, const void > *start, > > > + size_t len) > > > +{ > > > + return ((unsigned long) addr >= (unsigned long) start) && > > > + ((unsigned long) addr < ((unsigned long) start + len)); > > > +} > > > > might be my braindamage, but I'd have written it like: > > > > static inline int > > addr_within_len(const void *addr, const void *start, size_t len) > > { > > return (unsigned long)addr - (unsigned long)start < len; > > } > > Definitely another way to put it. In my opinion the intention of the > implementation is more easily understood though when spelling it out > as (a>=b) && (a= (unsigned long) start) && ((unsigned long) addr < ((unsigned long) start + len)); } int within2(const void *addr, const void *start, const void *end) { return ((unsigned long) addr >= (unsigned long) start) && ((unsigned long) addr < ((unsigned long) end)); } peter@lappy:~/tmp$ gcc -S -Os cmp*.c peter@lappy:~/tmp$ ls -la cmp*.o -rw-r--r-- 1 peter peter 752 2008-05-21 12:43 cmp2.o -rw-r--r-- 1 peter peter 743 2008-05-21 12:43 cmp.o Also look at the .s output and notice mine doesn't have any additional branches ;-) > > static inline int > > addr_within(const void *add, const void *start, const void *end) > > { > > return addr_within_len(addr, start, > > (unsigned long)end - (unsigned long)start); > > } > > For empty ranges (start > end), this produces different (less expected) > results than the previous version. agreed, do we care about those?