kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* ternary vs double exclamation
@ 2014-12-30  0:25 Vinícius Tinti
  2014-12-30  0:34 ` Max Filippov
  2014-12-30  1:00 ` Valdis.Kletnieks at vt.edu
  0 siblings, 2 replies; 13+ messages in thread
From: Vinícius Tinti @ 2014-12-30  0:25 UTC (permalink / raw)
  To: kernelnewbies

Hi,

I was looking the kernel source code and there are a lot of places in
which either "(expression) ? 1 : 0" or "(expression) ? 0 : 1" appear.
As fair as I can tell both can be replaced by "!!expression" and
"!expression".

Moreover there it seems that using "!!" does not add a "nopl"
instruction at the end of the call. Does anybody knows why?

Anyway. Wouldn't be nice if kernel provides something like
"boolean(x)" macro and  "inv_boolean(x)" to do this operations?

#ifdef MOD_IF
int mod_if(int x) {
    return (x == 0) ? 0 : 1;
}
#endif

#ifdef MOD_X
int mod_x(int x) {
    return !!x;
}
#endif

0000000000000000 <mod_if>:
   0:   31 c0                   xor    %eax,%eax
   2:   85 ff                   test   %edi,%edi
   4:   0f 95 c0                setne  %al
   7:   c3                      retq
   8:   0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
   f:   00

0000000000000010 <mod_x>:
  10:   31 c0                   xor    %eax,%eax
  12:   85 ff                   test   %edi,%edi
  14:   0f 95 c0                setne  %al
  17:   c3                      retq


Regards,
Vin?cius

-- 
Simplicity is the ultimate sophistication

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

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

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-30  0:25 ternary vs double exclamation Vinícius Tinti
2014-12-30  0:34 ` Max Filippov
2014-12-30  0:40   ` Vinícius Tinti
2014-12-30  1:00 ` Valdis.Kletnieks at vt.edu
2014-12-30  1:04   ` Vinícius Tinti
2015-01-03 23:54   ` John de la Garza
2015-01-04  4:20     ` Valdis.Kletnieks at vt.edu
2015-01-04 23:43       ` John de la Garza
2015-01-05  0:50         ` Greg KH
2015-01-08  4:46           ` John de la Garza
2015-01-08  6:37             ` Greg KH
2015-01-05  1:17         ` Valdis.Kletnieks at vt.edu
2015-01-08  4:58           ` John de la Garza

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).