Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S262085AbVBARy2 (ORCPT ); Tue, 1 Feb 2005 12:54:28 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S262086AbVBARy2 (ORCPT ); Tue, 1 Feb 2005 12:54:28 -0500 Received: from news.suse.de ([195.135.220.2]:54665 "EHLO Cantor.suse.de") by vger.kernel.org with ESMTP id S262085AbVBARyT (ORCPT ); Tue, 1 Feb 2005 12:54:19 -0500 Subject: Re: [PATCH 1/8] lib/sort: Heapsort implementation of sort() From: Andreas Gruenbacher To: Paulo Marques Cc: Matt Mackall , Andrew Morton , "linux-kernel@vger.kernel.org" In-Reply-To: <41FE6B42.7010807@grupopie.com> References: <2.416337461@selenic.com> <1107191783.21706.124.camel@winden.suse.de> <41FE6B42.7010807@grupopie.com> Content-Type: text/plain Organization: SUSE Labs Message-Id: <1107280438.12050.118.camel@winden.suse.de> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 Date: Tue, 01 Feb 2005 18:54:18 +0100 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1941 Lines: 66 On Mon, 2005-01-31 at 18:30, Paulo Marques wrote: > Andreas Gruenbacher wrote: > > [...] > > > > static inline void swap(void *a, void *b, int size) > > { > > if (size % sizeof(long)) { > > char t; > > do { > > t = *(char *)a; > > *(char *)a++ = *(char *)b; > > *(char *)b++ = t; > > } while (--size > 0); > > } else { > > long t; > > do { > > t = *(long *)a; > > *(long *)a = *(long *)b; > > *(long *)b = t; > > size -= sizeof(long); > > } while (size > sizeof(long)); > > You forgot to increment a and b, and this should be "while (size);", no? Correct, yes. > Or better yet, > > static inline void swap(void *a, void *b, int size) > { > long tl; > char t; > > while (size >= sizeof(long)) { > tl = *(long *)a; > *(long *)a = *(long *)b; > *(long *)b = tl; > a += sizeof(long); > b += sizeof(long); > size -= sizeof(long); > } > while (size) { > t = *(char *)a; > *(char *)a++ = *(char *)b; > *(char *)b++ = t; > size--; > } > } > > This works better if the size is not a multiple of sizeof(long), but is > bigger than a long. In case size is not a multiple of sizeof(long) here, you'll have unaligned memory accesses most of the time anyway, so it probably doesn't really matter. Thanks, -- Andreas Gruenbacher SUSE Labs, SUSE LINUX GMBH - 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/