Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1162802AbbKTN5O (ORCPT ); Fri, 20 Nov 2015 08:57:14 -0500 Received: from mail-wm0-f53.google.com ([74.125.82.53]:36839 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162359AbbKTN5I convert rfc822-to-8bit (ORCPT ); Fri, 20 Nov 2015 08:57:08 -0500 From: Michal Nazarewicz To: Jia He , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Andrew Morton , Rasmus Villemoes , Denys Vlasenko , Kyungmin Park , Yury Norov , Tejun Heo , Martin Kepplinger , George Spelvin , Ingo Molnar , Arnd Bergmann , Jia He Subject: Re: [PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero} In-Reply-To: <1447900314-5727-3-git-send-email-hejianet@gmail.com> Organization: http://mina86.com/ References: <1447900314-5727-1-git-send-email-hejianet@gmail.com> <1447900314-5727-3-git-send-email-hejianet@gmail.com> User-Agent: Notmuch/0.19+53~g2e63a09 (http://notmuchmail.org) Emacs/25.0.50.1 (x86_64-unknown-linux-gnu) X-Face: PbkBB1w#)bOqd`iCe"Ds{e+!C7`pkC9a|f)Qo^BMQvy\q5x3?vDQJeN(DS?|-^$uMti[3D*#^_Ts"pU$jBQLq~Ud6iNwAw_r_o_4]|JO?]}P_}Nc&"p#D(ZgUb4uCNPe7~a[DbPG0T~!&c.y$Ur,=N4RT>]dNpd;KFrfMCylc}gc??'U2j,!8%xdD Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJFBMVEWbfGlUPDDHgE57V0jUupKjgIObY0PLrom9mH4dFRK4gmjPs41MxjOgAAACQElEQVQ4jW3TMWvbQBQHcBk1xE6WyALX1069oZBMlq+ouUwpEQQ6uRjttkWP4CmBgGM0BQLBdPFZYPsyFUo6uEtKDQ7oy/U96XR2Ux8ehH/89Z6enqxBcS7Lg81jmSuujrfCZcLI/TYYvbGj+jbgFpHJ/bqQAUISj8iLyu4LuFHJTosxsucO4jSDNE0Hq3hwK/ceQ5sx97b8LcUDsILfk+ovHkOIsMbBfg43VuQ5Ln9YAGCkUdKJoXR9EclFBhixy3EGVz1K6eEkhxCAkeMMnqoAhAKwhoUJkDrCqvbecaYINlFKSRS1i12VKH1XpUd4qxL876EkMcDvHj3s5RBajHHMlA5iK32e0C7VgG0RlzFPvoYHZLRmAC0BmNcBruhkE0KsMsbEc62ZwUJDxWUdMsMhVqovoT96i/DnX/ASvz/6hbCabELLk/6FF/8PNpPCGqcZTGFcBhhAaZZDbQPaAB3+KrWWy2XgbYDNIinkdWAFcCpraDE/knwe5DBqGmgzESl1p2E4MWAz0VUPgYYzmfWb9yS4vCvgsxJriNTHoIBz5YteBvg+VGISQWUqhMiByPIPpygeDBE6elD973xWwKkEiHZAHKjhuPsFnBuArrzxtakRcISv+XMIPl4aGBUJm8Emk7qBYU8IlgNEIpiJhk/No24jHwkKTFHDWfPniR4iw5vJaw2nzSjfq2zffcE/GDjRC2dn0J0XwPAbDL84TvaFCJEU4Oml9pRyEUhR3Cl2t01AoEjRbs0sYugp14/4X5n4pU4EHHnMAAAAAElFTkSuQmCC X-PGP: 50751FF4 X-PGP-FP: AC1F 5F5C D418 88F8 CC84 5858 2060 4012 5075 1FF4 X-Hashcash: 1:20:151120:yury.norov@gmail.com::tFQooljp1VstAcDm:0000000000000000000000000000000000000000005+S X-Hashcash: 1:20:151120:hejianet@gmail.com::EtNPZKCMxomOoCGo:00000000000000000000000000000000000000000000ocF X-Hashcash: 1:20:151120:martink@posteo.de::yZdBXWnKJbVUF4rp:000000000000000000000000000000000000000000000iZY X-Hashcash: 1:20:151120:dvlasenk@redhat.com::rzx4Cqq/SQ9pYUm9:0000000000000000000000000000000000000000001ZPF X-Hashcash: 1:20:151120:kyungmin.park@samsung.com::X9cJqt66OA5O9DYj:0000000000000000000000000000000000001wNZ X-Hashcash: 1:20:151120:tj@kernel.org::LpY8aDW413C+B96a:00002lql X-Hashcash: 1:20:151120:arnd@arndb.de::uxlRjbcZ+2esMT5s:00003DOL X-Hashcash: 1:20:151120:hejianet@gmail.com::lQLmX+lSscdm7tCP:0000000000000000000000000000000000000000000458E X-Hashcash: 1:20:151120:akpm@linux-foundation.org::jlXEsFMXPpcZWOEF:0000000000000000000000000000000000005wvY X-Hashcash: 1:20:151120:linux-kernel@vger.kernel.org::Ip8YtSJ1N0B2aOYZ:0000000000000000000000000000000005QnZ X-Hashcash: 1:20:151120:linux-arch@vger.kernel.org::dbCWq/vrBqZ6Hy+p:000000000000000000000000000000000006vum X-Hashcash: 1:20:151120:linux@rasmusvillemoes.dk::U7lGqfrNhEve3Bjg:00000000000000000000000000000000000006rwX X-Hashcash: 1:20:151120:mingo@kernel.org::XOpOQTvweYDVk0Ln:09dH4 X-Hashcash: 1:20:151120:linux@horizon.com::7uSXJYCrqpS8T0da:00000000000000000000000000000000000000000000BBbV Date: Fri, 20 Nov 2015 14:57:03 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3093 Lines: 112 On Thu, Nov 19 2015, Jia He wrote: > This patch introduces 2 lightweight bit api. > all_bit_is_zero return 1 if the bit string is all zero. > The addr is the start address, the size is the bit size of the bit string. > all_bit_is_one is the opposite. > > Signed-off-by: Jia He > --- > lib/find_bit.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 50 insertions(+) > > diff --git a/lib/find_bit.c b/lib/find_bit.c > index 18072ea..1d56d8d 100644 > --- a/lib/find_bit.c > +++ b/lib/find_bit.c > @@ -131,6 +131,56 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size) > EXPORT_SYMBOL(find_last_bit); > #endif > > +#ifndef all_bit_is_zero > +/* > + * return val: 1 means all bit is zero > + */ > +unsigned int all_bit_is_zero(const unsigned long *addr, unsigned > size) Why does it return unsigned int, not bool? > +{ > + unsigned long idx; > + unsigned long mask = size; > + > + if (unlikely(size == 0)) > + return 1; > + > + if (size > BITS_PER_LONG) { > + for (idx = 0; idx * BITS_PER_LONG < size; idx++) > + if (addr[idx]) > + return 0; > + > + mask = size - (idx - 1) * BITS_PER_LONG; Uh? > + } > + > + return !(*addr & BITMAP_LAST_WORD_MASK(mask)); BITMAP_LAST_WORD_MASK takes size not mask. > +} The whole implementation seems weird, this seems much better to me: unsigned int all_bit_is_zero(const unsigned long *addr, unsigned size) { for (; size > BITS_PER_LONG; size -= BITS_PER_LONG, ++addr) if (*addr) return 0; return !size || !(*addr & BITMAP_LAST_WORD_MASK(size)); } But to be honest I’m not entirely sure this is worth the effort. find_next_bit and find_next_zero_bit may do a little bit more work but some architectures have specialised optimised versions which will be lost if all_bit_is_zero and all_bit_is_one are introduced. > +EXPORT_SYMBOL(all_bit_is_zero); > +#endif > + > +#ifndef all_bit_is_one > +/* > + * return val: 1 means all bit is one > + */ > +unsigned int all_bit_is_one(const unsigned long *addr, unsigned size) > +{ > + unsigned long idx; > + unsigned long mask = size; > + > + if (unlikely(size == 0)) > + return 1; > + > + if (size > BITS_PER_LONG) { > + for (idx = 0; idx * BITS_PER_LONG < size; idx++) > + if (~addr[idx]) > + return 0; > + > + mask = size - (idx - 1) * BITS_PER_LONG; > + } > + > + return !(~(*addr) & BITMAP_LAST_WORD_MASK(mask)); > +} > +EXPORT_SYMBOL(all_bit_is_one); > +#endif > + > #ifdef __BIG_ENDIAN > > /* include/linux/byteorder does not support "unsigned long" type */ > -- > 2.5.0 > -- Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, ミハウ “mina86” ナザレヴイツ (o o) ooo +---------ooO--(_)--Ooo-- -- 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/