RFC: small syscons and kbd patch

2008-12-05 Thread Garrett Cooper
On Thu, Dec 4, 2008 at 11:22 PM, Ed Schouten [EMAIL PROTECTED] wrote:
 * Maksim Yevmenkin [EMAIL PROTECTED] wrote:
 the idea was to ensure that kbd-kb_locked variable only takes values
 0 (zero) and 1 (one).

 I often use constructs like these to do that:

foo = bar ? 1 : 0;

 Maybe !!bar is a lot shorter to write, I think the line above is a lot
 easier to read.

Indeed. I had no idea (and I would assume that many people wouldn't in
my similar level of systems programming) what in the work you were
trying to do above with that line. The one-line conditional is
universal in almost all major high-level language dialects I've hit,
minus Python and Tcl.
-Garrett
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Erik Trulsson
On Fri, Dec 05, 2008 at 12:35:31AM -0800, Garrett Cooper wrote:
 On Thu, Dec 4, 2008 at 11:22 PM, Ed Schouten [EMAIL PROTECTED] wrote:
  * Maksim Yevmenkin [EMAIL PROTECTED] wrote:
  the idea was to ensure that kbd-kb_locked variable only takes values
  0 (zero) and 1 (one).
 
  I often use constructs like these to do that:
 
 foo = bar ? 1 : 0;
 
  Maybe !!bar is a lot shorter to write, I think the line above is a lot
  easier to read.
 
 Indeed. I had no idea (and I would assume that many people wouldn't in
 my similar level of systems programming) what in the work you were
 trying to do above with that line. The one-line conditional is
 universal in almost all major high-level language dialects I've hit,
 minus Python and Tcl.
 -Garrett

The !!bar construction to map {0, not-0} to {0,1} is fairly common in C
programming, and I would certainly expect any experienced C programmer to
recognize it.




-- 
Insert your favourite quote here.
Erik Trulsson
[EMAIL PROTECTED]
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Garrett Cooper
On Fri, Dec 5, 2008 at 12:44 AM, Erik Trulsson [EMAIL PROTECTED] wrote:
 On Fri, Dec 05, 2008 at 12:35:31AM -0800, Garrett Cooper wrote:
 On Thu, Dec 4, 2008 at 11:22 PM, Ed Schouten [EMAIL PROTECTED] wrote:
  * Maksim Yevmenkin [EMAIL PROTECTED] wrote:
  the idea was to ensure that kbd-kb_locked variable only takes values
  0 (zero) and 1 (one).
 
  I often use constructs like these to do that:
 
 foo = bar ? 1 : 0;
 
  Maybe !!bar is a lot shorter to write, I think the line above is a lot
  easier to read.

 Indeed. I had no idea (and I would assume that many people wouldn't in
 my similar level of systems programming) what in the work you were
 trying to do above with that line. The one-line conditional is
 universal in almost all major high-level language dialects I've hit,
 minus Python and Tcl.
 -Garrett

 The !!bar construction to map {0, not-0} to {0,1} is fairly common in C
 programming, and I would certainly expect any experienced C programmer to
 recognize it.

(I feel like I'm getting off on a bikeshed topic, but...)

1. What dialect of C was it defined in? Is it still used in the
standard dialect (honestly, this is the first time I've ever seen it
before, but then again I am a younger generation user)?
2. Is it still taught in schools (I didn't learn it when I was taught
C)? If not in schools, what about the Richie text (it's sort of like
the defacto C programming standard book of course)?
3. What's the real loss of going to `? :', beyond maybe 3 extra
keystrokes if it's easier for folks who may not be as experienced to
read?

-Garrett
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Christoph Mallon

Garrett Cooper schrieb:

(I feel like I'm getting off on a bikeshed topic, but...)

1. What dialect of C was it defined in? Is it still used in the
standard dialect (honestly, this is the first time I've ever seen it
before, but then again I am a younger generation user)?


Dialect? The ! operator is plain vanilla standard C. It takes a scalar 
operand and returns 1, if it compares equal to 0, otherwise it returns 
0. !!, i.e. two consecutive ! operators, is one of the oldest tricks in 
the book, right next to (a  b) - (a  b) for comparison functions and 
countless other idioms.



3. What's the real loss of going to `? :', beyond maybe 3 extra
keystrokes if it's easier for folks who may not be as experienced to
read?


I'd like my bikeshed grass green, please.

Christoph
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Erik Trulsson
On Fri, Dec 05, 2008 at 12:50:38AM -0800, Garrett Cooper wrote:
 On Fri, Dec 5, 2008 at 12:44 AM, Erik Trulsson [EMAIL PROTECTED] wrote:
  On Fri, Dec 05, 2008 at 12:35:31AM -0800, Garrett Cooper wrote:
  On Thu, Dec 4, 2008 at 11:22 PM, Ed Schouten [EMAIL PROTECTED] wrote:
   * Maksim Yevmenkin [EMAIL PROTECTED] wrote:
   the idea was to ensure that kbd-kb_locked variable only takes values
   0 (zero) and 1 (one).
  
   I often use constructs like these to do that:
  
  foo = bar ? 1 : 0;
  
   Maybe !!bar is a lot shorter to write, I think the line above is a lot
   easier to read.
 
  Indeed. I had no idea (and I would assume that many people wouldn't in
  my similar level of systems programming) what in the work you were
  trying to do above with that line. The one-line conditional is
  universal in almost all major high-level language dialects I've hit,
  minus Python and Tcl.
  -Garrett
 
  The !!bar construction to map {0, not-0} to {0,1} is fairly common in C
  programming, and I would certainly expect any experienced C programmer to
  recognize it.
 
 (I feel like I'm getting off on a bikeshed topic, but...)
 
 1. What dialect of C was it defined in? Is it still used in the
 standard dialect (honestly, this is the first time I've ever seen it
 before, but then again I am a younger generation user)?

The '!!x' construct is well defined in all dialects of C as far as I know.
It is after all just using the standard '!' logical negation operator twice
in a fairly straight-forward manner.
If you know what the '!' operator does it should not be too difficult to
figure out what applying it twice would do, even if one has never seen it
done that way before.


 2. Is it still taught in schools (I didn't learn it when I was taught
 C)? If not in schools, what about the Richie text (it's sort of like
 the defacto C programming standard book of course)?

Since I did not learn C in a school I have no idea what is (or has been)
taught in schools in that regard. As for KR I must admit to never having read 
it.



It is however one (of many) idiomatic constructions in C that I would not
really expect to be explicitly taught in a class.  It is rather something I
would expect programmers to either come up with on their own or to encounter
when reading other peoples programs.


 3. What's the real loss of going to `? :', beyond maybe 3 extra
 keystrokes if it's easier for folks who may not be as experienced to
 read?

An inexperienced C programmer who do not understand the '!!x' construct is
not a programmer I would count on being familiar with the '? :' operator
either.  Besides, I personally find the corresponding '? :' construction
harder to read.



-- 
Insert your favourite quote here.
Erik Trulsson
[EMAIL PROTECTED]
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Garrett Cooper
On Fri, Dec 5, 2008 at 1:11 AM, Christoph Mallon
[EMAIL PROTECTED] wrote:
 Garrett Cooper schrieb:

 (I feel like I'm getting off on a bikeshed topic, but...)

 1. What dialect of C was it defined in? Is it still used in the
 standard dialect (honestly, this is the first time I've ever seen it
 before, but then again I am a younger generation user)?

 Dialect? The ! operator is plain vanilla standard C. It takes a scalar
 operand and returns 1, if it compares equal to 0, otherwise it returns 0.
 !!, i.e. two consecutive ! operators, is one of the oldest tricks in the
 book, right next to (a  b) - (a  b) for comparison functions and countless
 other idioms.

 3. What's the real loss of going to `? :', beyond maybe 3 extra
 keystrokes if it's easier for folks who may not be as experienced to
 read?

 I'd like my bikeshed grass green, please.

Christoph

If you really want to split hairs, ! only negates the logic value,
whereas ~ actually negates the bits. So technically, you're not
flipping 0 to make 1 and vice versa, but instead flipping 0 to make
non-zero, etc. There is a clear distinction in hardware.

The point was that !! isn't obvious at first glancing the C code. It's
important for code to be readable as well as functional (that's why we
have style(9)). Getting down to it I'd like to see what the compiler
optimizes each as, because I can see dumb compilers saying `!!'
translates to `not, bne = set, else set, continue', whereas `? :'
could be translated to `bne, set, else set, continue'; I'm sure gcc
has moved passed these really minute details.

Hopefully this helps shed more light on where I'm coming from.

-Garrett
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Christoph Mallon

Garrett Cooper schrieb:

On Fri, Dec 5, 2008 at 1:11 AM, Christoph Mallon
[EMAIL PROTECTED] wrote:

Garrett Cooper schrieb:

(I feel like I'm getting off on a bikeshed topic, but...)

1. What dialect of C was it defined in? Is it still used in the
standard dialect (honestly, this is the first time I've ever seen it
before, but then again I am a younger generation user)?

Dialect? The ! operator is plain vanilla standard C. It takes a scalar
operand and returns 1, if it compares equal to 0, otherwise it returns 0.
!!, i.e. two consecutive ! operators, is one of the oldest tricks in the
book, right next to (a  b) - (a  b) for comparison functions and countless
other idioms.


3. What's the real loss of going to `? :', beyond maybe 3 extra
keystrokes if it's easier for folks who may not be as experienced to
read?

I'd like my bikeshed grass green, please.

   Christoph


If you really want to split hairs, ! only negates the logic value,
whereas ~ actually negates the bits. So technically, you're not
flipping 0 to make 1 and vice versa, but instead flipping 0 to make
non-zero, etc. There is a clear distinction in hardware.


I have no idea, what you are referring to. I did not even mention the 
bitwise complement operator ~. I just described, what ! does. I 
explicitely said, the operand is compared to 0. I just paraphrased, what 
the standard says. You can verify this by reading ISO/IEC 9899:1999 (E) 
§6.5.3.3:5.



The point was that !! isn't obvious at first glancing the C code. It's


I disagree. I sometimes even see stuff like (x ? 0 : 1) or worse (x ? 
false : true). I find this hard to read, because the true case returns 
false and vice versa. I clearly prefer a nice and concise ! or two. 
But today !! is not as important anymore, because C99 has a real bool 
type, which always is 0 or 1. See _Bool and the header stdbool.h.



important for code to be readable as well as functional (that's why we
have style(9)). Getting down to it I'd like to see what the compiler


Don't try to argue about style(9). IMO it's horribly outdated, but there 
are conservative forces, which would prefer to cling to KR. At least we 
got function prototypes!



optimizes each as, because I can see dumb compilers saying `!!'
translates to `not, bne = set, else set, continue', whereas `? :'
could be translated to `bne, set, else set, continue'; I'm sure gcc
has moved passed these really minute details.


If you use the classic approach to implement short circuit evaluation 
for  and || you also handle !. Otherwise the code generated for stuff 
like if (!x || !y) would be horrible. Here's a very short explanation: 
You generate jump labels for the true and false cases. When 
traversing a logical expression and encountering a !, you just swap the 
true and false jump labels - nothing more happens for a !. So at no 
point you generate code, which produces a 0/1 and compares it again. 
While evaluating a logical expression with short circuit evaluation, 
there are no intermediate 0 and 1 constants involved at all.
This is a very simpel model. Modern optimizing compilers do not generate 
code directly from the abstract syntax tree, but use a intermediate 
representation, which is more suited for optimization[1][2].


Hopefully this helps to shed a bit light on compiler construction.

Christoph



[1] (self advertisement) FIRM is a modern graph based IR, which uses SSA 
form: http://www.libfirm.org/

[2] LLVM has a nice IR, too: http://www.llvm.org/
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Dag-Erling Smørgrav
Garrett Cooper [EMAIL PROTECTED] writes:
 If you really want to split hairs, ! only negates the logic value,
 whereas ~ actually negates the bits. So technically, you're not
 flipping 0 to make 1 and vice versa, but instead flipping 0 to make
 non-zero, etc. There is a clear distinction in hardware.

He didn't say anything about flipping bits...  and you're wrong, !0 is
guaranteed to evaluate to 1.

 The point was that !! isn't obvious at first glancing the C code.

It is to an experienced C programmer.

 Getting down to it I'd like to see what the compiler optimizes each
 as, because I can see dumb compilers saying `!!'  translates to `not,
 bne = set, else set, continue', whereas `? :' could be translated to
 `bne, set, else set, continue'; I'm sure gcc has moved passed these
 really minute details.

Never try to second-guess the compiler.

DES
-- 
Dag-Erling Smørgrav - [EMAIL PROTECTED]
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Dag-Erling Smørgrav
Christoph Mallon [EMAIL PROTECTED] writes:
 Don't try to argue about style(9). IMO it's horribly outdated, but
 there are conservative forces, which would prefer to cling to KR. At
 least we got function prototypes!

If there's something specific in style(9) you don't like, you are
welcome to start a discussion about it, and we will consider the merits
of your arguments; talking about conservative forces that would prefer
to cling to KR will get you nowhere.

DES
-- 
Dag-Erling Smørgrav - [EMAIL PROTECTED]
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Andriy Gapon
on 05/12/2008 10:50 Garrett Cooper said the following:
 On Fri, Dec 5, 2008 at 12:44 AM, Erik Trulsson [EMAIL PROTECTED] wrote:
 On Fri, Dec 05, 2008 at 12:35:31AM -0800, Garrett Cooper wrote:
 On Thu, Dec 4, 2008 at 11:22 PM, Ed Schouten [EMAIL PROTECTED] wrote:
 * Maksim Yevmenkin [EMAIL PROTECTED] wrote:
 the idea was to ensure that kbd-kb_locked variable only takes values
 0 (zero) and 1 (one).
 I often use constructs like these to do that:

foo = bar ? 1 : 0;

 Maybe !!bar is a lot shorter to write, I think the line above is a lot
 easier to read.
 Indeed. I had no idea (and I would assume that many people wouldn't in
 my similar level of systems programming) what in the work you were
 trying to do above with that line. The one-line conditional is
 universal in almost all major high-level language dialects I've hit,
 minus Python and Tcl.
 -Garrett
 The !!bar construction to map {0, not-0} to {0,1} is fairly common in C
 programming, and I would certainly expect any experienced C programmer to
 recognize it.
 
 (I feel like I'm getting off on a bikeshed topic, but...)
 
 1. What dialect of C was it defined in? Is it still used in the
 standard dialect (honestly, this is the first time I've ever seen it
 before, but then again I am a younger generation user)?

I am not sure what you meant by dialect of C, just in case you meant
something different from others understood here's my personal
observation: you will quite a bit of '!!' in Linux kernel code, you
would see much much fewer of them in our code.

-- 
Andriy Gapon
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Eygene Ryabinkin
Garret,

Fri, Dec 05, 2008 at 12:50:38AM -0800, Garrett Cooper wrote:
 1. What dialect of C was it defined in? Is it still used in the
 standard dialect (honestly, this is the first time I've ever seen it
 before, but then again I am a younger generation user)?

It is the standard negation operator: !(expr) is equal to (expr == 0).

 2. Is it still taught in schools (I didn't learn it when I was taught
 C)?

Yes.  I am personally teaching the people in school and I am explaining
the concept of double negation every two years ;))

 If not in schools, what about the Richie text (it's sort of like
 the defacto C programming standard book of course)?

KR book is good but it at no means covers all tricks and idioms
of a C language.

 3. What's the real loss of going to `? :', beyond maybe 3 extra
 keystrokes if it's easier for folks who may not be as experienced to
 read?

No real loss, just easier to type and looks more compact.  It is the
matter of a personal taste, I think.

If one knows this idiom, he will recognize it at a glance.  If not, one
should think a bit about the logics behind it, but it shouldn't be hard:
almost everyone uses constructs like 'if (!ptr)'.  And there is only one
step from there to the double negation.
-- 
Eygene
 ____   _.--.   #
 \`.|\.....-'`   `-._.-'_.-'`   #  Remember that it is hard
 /  ' ` ,   __.--'  #  to read the on-line manual   
 )/' _/ \   `-_,   /#  while single-stepping the kernel.
 `-' `\_  ,_.-;_.-\_ ',  fsc/as   #
 _.-'_./   {_.'   ; /   #-- FreeBSD Developers handbook 
{_.-``-' {_/#


pgpsI9cudOk9G.pgp
Description: PGP signature


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Nate Eldredge

On Fri, 5 Dec 2008, Garrett Cooper wrote:


On Fri, Dec 5, 2008 at 1:11 AM, Christoph Mallon
[EMAIL PROTECTED] wrote:

Garrett Cooper schrieb:


(I feel like I'm getting off on a bikeshed topic, but...)

1. What dialect of C was it defined in? Is it still used in the
standard dialect (honestly, this is the first time I've ever seen it
before, but then again I am a younger generation user)?


Dialect? The ! operator is plain vanilla standard C. It takes a scalar
operand and returns 1, if it compares equal to 0, otherwise it returns 0.
!!, i.e. two consecutive ! operators, is one of the oldest tricks in the
book, right next to (a  b) - (a  b) for comparison functions and countless
other idioms.


3. What's the real loss of going to `? :', beyond maybe 3 extra
keystrokes if it's easier for folks who may not be as experienced to
read?


I'd like my bikeshed grass green, please.

   Christoph


If you really want to split hairs, ! only negates the logic value,
whereas ~ actually negates the bits. So technically, you're not
flipping 0 to make 1 and vice versa, but instead flipping 0 to make
non-zero, etc. There is a clear distinction in hardware.

The point was that !! isn't obvious at first glancing the C code. It's
important for code to be readable as well as functional (that's why we
have style(9)). Getting down to it I'd like to see what the compiler
optimizes each as, because I can see dumb compilers saying `!!'
translates to `not, bne = set, else set, continue', whereas `? :'
could be translated to `bne, set, else set, continue'; I'm sure gcc
has moved passed these really minute details.


Out of curiosity, I tried some various compilers, including gcc on i386, 
amd64, and sparc; Intel's C compiler on i386; tcc (tiny, non-optimizing C 
compiler) on i386; and Sun's compiler (old version) on sparc.


I compiled the following file:

int bangbang(int x) { return !!x; }
int ternary(int x) { return x ? 1 : 0; }

Intel's compiler generated different code for these two functions when 
optimization was turned off.  bangbang used a conditional set instruction, 
while ternary used conditional jumps.  With optimization on the two were 
identical.


All other compilers generated identical code for the two functions whether 
optimization was on or off.  (Of course, the generated code varied between 
compilers; tcc's in particular was decidedly non-optimized.)


I really don't think something as simple as this is worth worrying about 
in terms of code efficiency.  Even if they weren't identical, the 
difference is at most a couple of instructions and a pipeline flush, and 
if that's a serious problem you need to be using assembly anyway. 
Besides, it's not a piece of code that comes up all that often.


The only basis for arguing about it is style, and I think we've 
established that it's purely a matter of taste.  In particular, there 
isn't a clear favorite for which is easier to read.  IMHO, style(9) should 
remain agnostic and let the programmer decide.


However, if people really feel that consistency is necessary here, I 
propose the following: if the cents digit of the closing price of the Dow 
Jones Industrial Average on this coming Monday, December 8, 2008, is even, 
then style(9) shall be edited to indicate that `!!x' is preferred.  If 
odd, then style(9) shall prefer `x ? 1 : 0'.


:-)

--

Nate Eldredge
[EMAIL PROTECTED]
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Stephen Montgomery-Smith

Nate Eldredge wrote:


int bangbang(int x) { return !!x; }
int ternary(int x) { return x ? 1 : 0; }


Stylewise, I prefer

int notzero(int x) { return x!=0; }
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: RFC: small syscons and kbd patch

2008-12-05 Thread Nate Eldredge

On Fri, 5 Dec 2008, Stephen Montgomery-Smith wrote:


Nate Eldredge wrote:


int bangbang(int x) { return !!x; }
int ternary(int x) { return x ? 1 : 0; }


Stylewise, I prefer

int notzero(int x) { return x!=0; }


icc -O0 compiles notzero the same as bangbang (better than ternary).  tcc 
produces better code for notzero than the other two.  Sun cc without 
optimization produces slightly better code for notzero than the other two 
(one jump instead of two).  For everything else all three produce 
equivalent code.


`x  1' and `x || 0' are some other possibilities.

Anyway, maybe there is something more useful we could all be doing. :)

--

Nate Eldredge
[EMAIL PROTECTED]
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to [EMAIL PROTECTED]