From: Martin Olsen <triplefault@gmail.com>
To: kernel-janitors@vger.kernel.org
Subject: Re: [KJ] powers of 2, and the boundary case of zero
Date: Wed, 10 Jan 2007 18:29:43 +0000 [thread overview]
Message-ID: <200701101929.44177.triplefault@gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0701090539520.7476@localhost.localdomain>
On Wednesday 10 January 2007 19:04, you wrote:
> On Wed, 10 Jan 2007, Martin Olsen wrote:
> > A quick search on Google Code found this snippet:
> >
> > /* rounding macros, assumes ALIGN is a power of two */
> > #define ROUND_UP(N,ALIGN) (((N) + ((ALIGN)-1)) &
> > ~((ALIGN)-1)) #define ROUND_DOWN(N,ALIGN) ((N) & ~((ALIGN)-1))
> >
> > I am no mathematician, but wouldn't the above code work if ALIGN
> > was set to 2?
>
> but that would only round up/down to the next *multiple* of two, not
> *power* of two. big difference.
You're right!
> > > at this point, i would submit this as an official patch to the KJ
> > > list. um ... you *have* tested this code to see what it does in
> > > various situations, right? :-)
> >
> > Heh.. No, I forgot that. A quick testcase compiled against my
> > header file correctly denies any knowledge of "bool" (not defined
> > in C). Should I include linux/types.h or just use "_Bool" (is _Bool
> > part of C? I cant find any definitions of it in my include
> > directories)?
>
> if you want to use "bool" in kernel space, you should include
> <linux/types.h>, which has the line:
>
> typedef _Bool bool;
>
> that should be sufficient. add that "include" line to your
> power_of_2.h header file.
>
> rday
Looks like it is almost there.
Should i change the short description at beginning of the file?
---
diff -uprN -X linux-2.6.19-vanilla/Documentation/dontdiff
linux-2.6.19-vanilla/include/linux/power_of_2.h
linux-2.6.19-changed/include/linux/power_of_2.h
--- linux-2.6.19-vanilla/include/linux/power_of_2.h 1970-01-01
01:00:00.000000000 +0100
+++ linux-2.6.19-changed/include/linux/power_of_2.h 2007-01-10
19:26:40.000000000 +0100
@@ -0,0 +1,54 @@
+/*
+ * linux/include/power_of_2.h - Power of 2 helper functions.
+ *
+ * Copyright (C) 2006 Martin Olsen <triplefault@gmail.com>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ *
+ */
+#ifndef _LINUX_POWER_OF_2_H
+#define _LINUX_POWER_OF_2_H
+
+#include <linux/types.h>
+
+static inline bool is_power_of_2_or_zero(unsigned long i)
+{
+ return (i & (i - 1)) = 0;
+}
+
+static inline bool is_power_of_2(unsigned long i)
+{
+ return i && is_power_of_2_or_zero(i);
+}
+
+static inline unsigned long round_up_to_power_of_2(unsigned long i)
+{
+ while ((i & (i - 1)) != 0)
+ i += i & ~(i - 1);
+
+ return i;
+}
+
+static inline unsigned long round_down_to_power_of_2(unsigned long i)
+{
+ while (i & (i - 1))
+ i &= (i - 1);
+
+ return i;
+}
+
+#endif
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
next prev parent reply other threads:[~2007-01-10 18:29 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-01-09 11:18 [KJ] powers of 2, and the boundary case of zero Robert P. J. Day
2007-01-09 12:16 ` Richard Knutsson
2007-01-09 12:25 ` Robert P. J. Day
2007-01-09 13:01 ` Richard Knutsson
2007-01-09 13:04 ` Robert P. J. Day
2007-01-09 13:57 ` Darren Jenkins
2007-01-09 14:27 ` Robert P. J. Day
2007-01-09 14:39 ` Jaco Kroon
2007-01-09 14:42 ` Robert P. J. Day
2007-01-09 14:46 ` Robert P. J. Day
2007-01-09 15:25 ` Darren Jenkins
2007-01-09 15:27 ` Robert P. J. Day
2007-01-09 16:16 ` Robert P. J. Day
2007-01-09 17:31 ` Randy Dunlap
2007-01-09 18:57 ` Robert P. J. Day
2007-01-09 20:03 ` Arnaldo Carvalho de Melo
2007-01-09 22:41 ` Martin Olsen
2007-01-10 6:08 ` Robert P. J. Day
2007-01-10 8:25 ` Robert P. J. Day
2007-01-10 11:50 ` Martin Olsen
2007-01-10 12:12 ` Jaco Kroon
2007-01-10 13:31 ` Martin Olsen
2007-01-10 13:36 ` Robert P. J. Day
2007-01-10 13:40 ` Robert P. J. Day
2007-01-10 13:51 ` Robert P. J. Day
2007-01-10 13:53 ` Robert P. J. Day
2007-01-10 15:25 ` Martin Olsen
2007-01-10 15:40 ` Robert P. J. Day
2007-01-10 16:44 ` Darren Jenkins
2007-01-10 17:53 ` Martin Olsen
2007-01-10 18:04 ` Robert P. J. Day
2007-01-10 18:29 ` Martin Olsen [this message]
2007-01-10 19:44 ` Alexey Dobriyan
2007-01-10 23:01 ` Paul Bonser
2007-01-10 23:30 ` Paul Bonser
2007-01-11 2:06 ` Darren Jenkins
2007-01-11 4:33 ` Robert P. J. Day
2007-01-11 6:41 ` Jaco Kroon
2007-01-11 6:44 ` Robert P. J. Day
2007-01-11 9:30 ` Robert P. J. Day
2007-01-11 11:37 ` Paul Bonser
2007-01-11 11:40 ` Paul Bonser
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200701101929.44177.triplefault@gmail.com \
--to=triplefault@gmail.com \
--cc=kernel-janitors@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.