All of lore.kernel.org
 help / color / mirror / Atom feed
* [KJ] powers of 2, and the boundary case of zero
@ 2007-01-09 11:18 Robert P. J. Day
  2007-01-09 12:16 ` Richard Knutsson
                   ` (40 more replies)
  0 siblings, 41 replies; 42+ messages in thread
From: Robert P. J. Day @ 2007-01-09 11:18 UTC (permalink / raw)
  To: kernel-janitors


  yesterday, i made the off-the-cuff suggestion that you could get
more readable code by replacing the numerous tests of the form "(x &
(x - 1))" with the appropriate power-of-2 test macro, something like:

  #define is_power_of_2(x) (((x) & ((x) - 1)) = 0)

however, i didn't think far enough ahead to wonder how to treat the
boundary case of zero.

  should zero be considered a power of 2 or not?  according to the
above test, it *would* be.  however, there are already some
definitions of that macro in the kernel tree:

  $ grep -r is_power_of_2 .

where the tests specifically disqualify zero from being considered a
power of two, as in:

  #define ... ((x) != 0 && (((x) & ((x) - 1)) = 0))
               ^^^^^^^^
                 DON'T consider zero a power of two!

but all this makes one wonder ... if you see some code that just tests
the expression "(x & (x - 1))", did that programmer understand how
zero would be treated?

  in some cases, it's clear that he/she did, as in:

./fs/hfsplus/btree.c:   if (!size || size & (size - 1))

so whoever wrote the above code to recognize *invalid* values clearly
understood that boundary condition.  but what about, say:

  ./fs/jbd/revoke.c:      J_ASSERT ((hash_size & (hash_size-1)) = 0);

clearly, the above assertion is asserting that "hash_size" should be a
power of 2, but did the programmer realize that a value of zero would
*also* be considered valid?  i have no idea.  could this represent a
possible error?  who knows?  is a hash size of zero meaningful?
according to the above assertion, it would be.

  in any event, i didn't mean to ramble, but it struck me how making
what seemed like an innocuous change suddenly became a bit more
complicated given that boundary case of zero, and how different
programmers might have made different assumptions about it.

rday

p.s.  personally, i would define an "is_power_of_2" macro to
explicitly reject zero, the way the current definitions do.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 42+ messages in thread

end of thread, other threads:[~2007-01-11 11:40 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.