From patchwork Mon Jun 11 23:17:25 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 11674 Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.72) (envelope-from ) id 1SeDrw-0002iQ-8j for patchwork@linuxtv.org; Tue, 12 Jun 2012 01:17:56 +0200 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.75/mailfrontend-4) with esmtp for id 1SeDrv-0000WP-BW; Tue, 12 Jun 2012 01:17:56 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752699Ab2FKXR2 (ORCPT ); Mon, 11 Jun 2012 19:17:28 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58228 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752349Ab2FKXR0 (ORCPT ); Mon, 11 Jun 2012 19:17:26 -0400 Received: from akpm.mtv.corp.google.com (216-239-45-4.google.com [216.239.45.4]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D42A37CA; Mon, 11 Jun 2012 23:17:25 +0000 (UTC) Date: Mon, 11 Jun 2012 16:17:25 -0700 From: Andrew Morton To: Akinobu Mita Cc: linux-kernel@vger.kernel.org, Anders Larsen , Alasdair Kergon , dm-devel@redhat.com, linux-fsdevel@vger.kernel.org, Laurent Pinchart , linux-media@vger.kernel.org, Mark Fasheh , Joel Becker , ocfs2-devel@oss.oracle.com, Jan Kara , linux-ext4@vger.kernel.org, Andreas Dilger , "Theodore Ts'o" , Matthew Wilcox Subject: Re: [PATCH v3 1/9] string: introduce memweight Message-Id: <20120611161725.84330925.akpm@linux-foundation.org> In-Reply-To: <1339203038-13069-1-git-send-email-akinobu.mita@gmail.com> References: <1339203038-13069-1-git-send-email-akinobu.mita@gmail.com> X-Mailer: Sylpheed 3.0.2 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 5.6.1.2065439, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2012.6.11.230920 X-PMX-Spam: Gauge=IIIIIIIII, Probability=9%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, MIME_LOWER_CASE 0.05, BODYTEXTP_SIZE_3000_LESS 0, BODY_SIZE_2000_2999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, DATE_TZ_NA 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __BOUNCE_CHALLENGE_SUBJ 0, __BOUNCE_NDR_SUBJ_EXEMPT 0, __CP_URI_IN_BODY 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __FRAUD_BODY_WEBMAIL 0, __FRAUD_WEBMAIL 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __MULTIPLE_RCPTS_CC_X2 0, __RUS_OBFU_PHONE 0, __SANE_MSGID 0, __SUBJ_ALPHA_END 0, __TO_MALFORMED_2 0, __URI_NO_WWW 0, __URI_NS ' On Sat, 9 Jun 2012 09:50:30 +0900 Akinobu Mita wrote: > memweight() is the function that counts the total number of bits set > in memory area. Unlike bitmap_weight(), memweight() takes pointer > and size in bytes to specify a memory area which does not need to be > aligned to long-word boundary. > > ... > > +/** > + * memweight - count the total number of bits set in memory area > + * @ptr: pointer to the start of the area > + * @bytes: the size of the area > + */ > +size_t memweight(const void *ptr, size_t bytes) > +{ > + size_t w = 0; Calling the return value "ret" is a useful convention and fits well here. > + size_t longs; > + const unsigned char *bitmap = ptr; > + > + for (; bytes > 0 && ((unsigned long)bitmap) % sizeof(long); > + bytes--, bitmap++) > + w += hweight8(*bitmap); > + > + longs = bytes / sizeof(long); > + if (longs) { > + BUG_ON(longs >= INT_MAX / BITS_PER_LONG); > + w += bitmap_weight((unsigned long *)bitmap, > + longs * BITS_PER_LONG); > + bytes -= longs * sizeof(long); > + bitmap += longs * sizeof(long); > + } > + /* > + * The reason that this last loop is distinct from the preceding > + * bitmap_weight() call is to compute 1-bits in the last region smaller > + * than sizeof(long) properly on big-endian systems. > + */ > + for (; bytes > 0; bytes--, bitmap++) > + w += hweight8(*bitmap); > + > + return w; > +} > +EXPORT_SYMBOL(memweight); diff -puN lib/string.c~string-introduce-memweight-fix lib/string.c --- a/lib/string.c~string-introduce-memweight-fix +++ a/lib/string.c @@ -833,18 +833,18 @@ EXPORT_SYMBOL(memchr_inv); */ size_t memweight(const void *ptr, size_t bytes) { - size_t w = 0; + size_t ret = 0; size_t longs; const unsigned char *bitmap = ptr; for (; bytes > 0 && ((unsigned long)bitmap) % sizeof(long); bytes--, bitmap++) - w += hweight8(*bitmap); + ret += hweight8(*bitmap); longs = bytes / sizeof(long); if (longs) { BUG_ON(longs >= INT_MAX / BITS_PER_LONG); - w += bitmap_weight((unsigned long *)bitmap, + ret += bitmap_weight((unsigned long *)bitmap, longs * BITS_PER_LONG); bytes -= longs * sizeof(long); bitmap += longs * sizeof(long); @@ -855,8 +855,8 @@ size_t memweight(const void *ptr, size_t * than sizeof(long) properly on big-endian systems. */ for (; bytes > 0; bytes--, bitmap++) - w += hweight8(*bitmap); + ret += hweight8(*bitmap); - return w; + return ret; } EXPORT_SYMBOL(memweight);