public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: RFC: booleans and the kernel
@ 2002-01-25 11:09 Helge Hafting
  2002-01-26  3:08 ` Jamie Lokier
  0 siblings, 1 reply; 73+ messages in thread
From: Helge Hafting @ 2002-01-25 11:09 UTC (permalink / raw)
  To: linux-kernel

Jeff Garzik wrote:
> 
> Oliver Xymoron wrote:
> > On Thu, 24 Jan 2002, Jeff Garzik wrote:
> > > Where variables are truly boolean use of a bool type makes the
> > > intentions of the code more clear.  And it also gives the compiler a
> > > slightly better chance to optimize code [I suspect].
> >
> > Unlikely. The compiler can already figure this sort of thing out from
> > context.
> 
> X, true, and false are of type int.
> If one tests X==false and then later on tests X==true, how does the
> compiler know the entire domain has been tested?  With a boolean, it

Why would anyone want to write   if (X==false) or if (X==true) ?
It is the "beginner's mistake" way of writing code.  Then people learn,
and write if (X) or if (!X).  Comparing to true/false is silly.
Nobody writes  if ( (a==b) == true) so why do it in the simpler cases?

> would.  Or a switch statement... if both true and false are covered,
A switch statement on a boolean value is stupid.  Use if - there
is only two cases.

Helge Hafting

^ permalink raw reply	[flat|nested] 73+ messages in thread
* Re: RFC: booleans and the kernel
@ 2002-01-26 15:48 Ben Bridgwater
  0 siblings, 0 replies; 73+ messages in thread
From: Ben Bridgwater @ 2002-01-26 15:48 UTC (permalink / raw)
  To: lkml

Rick Stevens wrote:

 > Granted.  "if (x)" is true if "x" is non-zero, regardless of type and
 > shoudn't even generate a warning if "x" is scalar.

Surely the major point of using the compiler's _Bool would be for type 
safety -  if you only want readability then you may as well just use:

enum bool {true = 1, false = 0};

Now since C has historically utilized integer expressions for 
conditionals (with 0, !0 truth semantics), so it would be obnoxious to 
*unconditionally* make if (x) where x is non _Bool generate warnings, 
but IMO the whole point of using _Bool would be to then choose to turn 
on warnings for non _Bool conditionals, and to suppress warnings one 
would then need to be explicit about intentions by writing code such as:

if ((_Bool) (x = y)) ;

and hopefully if one accidently wrote:

if ((_Bool) x = y) ; /* int x, y */

then the compiler would warn that you probably didn't really mean that, 
since it means:

if ((_Bool) (x = (int) (_Bool) y)) ;

Which would have converted your y to _Bool (0/1) before assigning it to x.

Ben

P.S. I'm not on the list - please CC any response to me if you want a reply.


^ permalink raw reply	[flat|nested] 73+ messages in thread
* Re: RFC: booleans and the kernel
@ 2002-01-25 11:28 Thomas Hood
  2002-01-25 11:39 ` Xavier Bestel
  0 siblings, 1 reply; 73+ messages in thread
From: Thomas Hood @ 2002-01-25 11:28 UTC (permalink / raw)
  To: linux-kernel

Jeff Garzik wrote:
> A small issue...

... bound therefore to generate the most discussion ...

> C99 introduced _Bool as a builtin type.  The gcc patch for
> it went into cvs around Dec 2000.  Any objections to
> propagating this type and usage of 'true' and 'false' around
> the kernel?

What concerns me is the question of casting.  Will truth always
cast to integer value 1 and falsehood always cast to integer
value 0, and vice versa?  If so then the bool type is a lot
like a "bit" type would be if C had one, i.e., a very short
integer variable limited to the values 0 and 1.  If the casts
are not guaranteed then bool is a lot like an enumerated type
where the compiler is free to choose whatever representations
it wants for truth and falsehood.

I assume the casts are guaranteed.  E.g., I take it that the
result of a logical comparison is considered to be of type
bool, but that the following will increment val by 1 if a > b
    val += (a > b)

In that case, perhaps it would be more perspicuous to define
a "bit" type rather than a "bool" type, and to use 0 and 1 as
its values rather than 'true' and 'false'.  (A "bit" type 
would have all the advantages mentioned earlier by Peter Anvin
http://marc.theaimsgroup.com/?l=linux-kernel&m=101191106124169&w=2 .)

--
Thomas Hood


^ permalink raw reply	[flat|nested] 73+ messages in thread
* RFC: booleans and the kernel
@ 2002-01-24 17:42 Jeff Garzik
  2002-01-24 18:22 ` Anton Altaparmakov
                   ` (4 more replies)
  0 siblings, 5 replies; 73+ messages in thread
From: Jeff Garzik @ 2002-01-24 17:42 UTC (permalink / raw)
  To: Linux-Kernel list

A small issue... 

C99 introduced _Bool as a builtin type.  The gcc patch for it went into
cvs around Dec 2000.  Any objections to propagating this type and usage
of 'true' and 'false' around the kernel?

Where variables are truly boolean use of a bool type makes the
intentions of the code more clear.  And it also gives the compiler a
slightly better chance to optimize code [I suspect].

Actually I prefer 'bool' to '_Bool', if this becomes a kernel standard.

	Jeff


-- 
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com

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

end of thread, other threads:[~2002-01-29  6:37 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-01-25 11:09 RFC: booleans and the kernel Helge Hafting
2002-01-26  3:08 ` Jamie Lokier
2002-01-26  9:13   ` Mark Zealey
2002-01-27 19:58     ` Jamie Lokier
2002-01-26 10:51   ` Gábor Lénárt
2002-01-26 16:27     ` Jamie Lokier
  -- strict thread matches above, loose matches on Subject: below --
2002-01-26 15:48 Ben Bridgwater
2002-01-25 11:28 Thomas Hood
2002-01-25 11:39 ` Xavier Bestel
2002-01-24 17:42 Jeff Garzik
2002-01-24 18:22 ` Anton Altaparmakov
2002-01-24 18:33   ` Arnaldo Carvalho de Melo
2002-01-24 19:28 ` H. Peter Anvin
2002-01-24 19:34   ` Arnaldo Carvalho de Melo
2002-01-24 19:43     ` H. Peter Anvin
2002-01-24 19:47       ` Arnaldo Carvalho de Melo
2002-01-24 19:46     ` Ingo Oeser
2002-01-24 19:52 ` Oliver Xymoron
2002-01-24 20:03   ` Jeff Garzik
2002-01-24 20:06     ` Oliver Xymoron
2002-01-24 20:14       ` Jeff Garzik
2002-01-24 20:23       ` Alexander Viro
2002-01-24 20:25         ` Oliver Xymoron
2002-01-24 20:35           ` John Levon
2002-01-24 20:15     ` Alexander Viro
2002-01-24 20:21   ` Richard B. Johnson
2002-01-24 20:39     ` Oliver Xymoron
2002-01-24 21:55       ` Richard B. Johnson
2002-01-24 21:57         ` Jeff Garzik
2002-01-24 22:05         ` H. Peter Anvin
2002-01-24 22:13         ` Robert Love
2002-01-25 21:24       ` Timothy Covell
2002-01-24 21:31         ` Oliver Xymoron
2002-01-25 21:43           ` Timothy Covell
2002-01-24 21:50             ` Oliver Xymoron
2002-01-24 22:21               ` H. Peter Anvin
2002-01-25 15:07                 ` Werner Almesberger
2002-01-25 15:21                   ` Jakub Jelinek
2002-01-25 16:45                   ` H. Peter Anvin
2002-01-24 22:19             ` Robert Love
2002-01-25 22:30               ` Timothy Covell
2002-01-24 22:36                 ` Alexander Viro
2002-01-24 22:38                 ` Robert Love
2002-01-25 22:44                   ` Timothy Covell
2002-01-25  3:52                     ` Ragnar Hojland Espinosa
2002-01-25 20:39                       ` Calin A. Culianu
2002-01-25 23:07                       ` Rick Stevens
2002-01-25  6:36                     ` Kai Henningsen
     [not found]                       ` <200201250900.g0P8xoL10082@home.ashavan.org.>
2002-01-25 19:02                         ` Kai Henningsen
2002-01-27  1:33                           ` Timothy Covell
2002-01-26  2:56                             ` Jamie Lokier
2002-01-27 11:18                               ` Kai Henningsen
2002-01-29  6:36                           ` Nix N. Nix
2002-01-24 22:33         ` Xavier Bestel
2002-01-25 22:47           ` Timothy Covell
2002-01-24 22:53             ` Xavier Bestel
2002-01-24 22:59             ` Robert Love
2002-01-25 23:09               ` Timothy Covell
2002-01-24 23:27                 ` Xavier Bestel
2002-01-25  6:13                   ` Alexander Viro
2002-01-25  8:00                     ` Momchil Velikov
2002-01-25 10:51                       ` Xavier Bestel
2002-01-25 16:11                     ` Olivier Galibert
2002-01-26  7:22                     ` Timothy Covell
2002-01-25  7:48                       ` Alexander Viro
2002-01-25 23:49                         ` J.A. Magallon
2002-01-27 11:27                           ` Kai Henningsen
2002-01-25  1:16                 ` John Levon
2002-01-25 11:07         ` Helge Hafting
2002-01-24 22:33 ` Chris Wedgwood
2002-01-24 22:44   ` H. Peter Anvin
2002-01-26 10:22     ` Chris Wedgwood
2002-01-25  2:00 ` Erik Andersen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox