xor as a lazy comparison
Hi list,
I have seen this in kernel/signal.c:check_kill_permission()
&& (current->euid ^ t->suid) && (current->euid ^ t->uid)
If current->euid and t->suid are the same, the xor returns 0, so these statements are effectively the same as a !=
current->euid != t->suid ...
Why ^ ?
Jan Engelhardt
Quoted 11 lines · Jan Engelhardt
I have seen this in kernel/signal.c:check_kill_permission()
&& (current->euid ^ t->suid) && (current->euid ^ t->uid)If
current->euidandt->suidare the same, the xor returns 0, so these statements are effectively the same as a !=current->euid != t->suid ...Why ^ ?
To confuse you, coders with assembly or hardware background throw in equivalent bit operations to succinctly describe their visualisation of solution space... Perhaps the writer _wanted_ you to pause and think? Maybe the compiler produces better code? Try it and see.
Grant.
Quoted 1 line
To confuse you, coders with assembly or hardware background throw in
I doubt that. I'm good enough assembly to see this :)
Quoted 3 lines
equivalent bit operations to succinctly describe their visualisation of solution space... Perhaps the writer _wanted_ you to pause and think? Maybe the compiler produces better code? Try it and see.
It produces a simple CMP. Should not be inefficient, though.
Jan Engelhardt
Quoted 8 lines · Jan Engelhardt
Quoted 3 lines
To confuse you, coders with assembly or hardware background throw in
I doubt that. I'm good enough assembly to see this :)
Quoted 10 lines
Quoted 3 lines
equivalent bit operations to succinctly describe their visualisation of solution space... Perhaps the writer _wanted_ you to pause and think? Maybe the compiler produces better code? Try it and see.
It produces a simple CMP. Should not be inefficient, though.
Jan Engelhardt
I just compiled two identical program , one with "!=" and other with "^". The assembly output is identical.
On Sun, 2005-07-24 at 18:15 -0400, Puneet Vyas wrote:
[...]
Quoted 2 lines
I just compiled two identical program , one with "!=" and other with "^". The assembly output is identical.
Hmm, which compiler and which version?
You might want to try much older and other compilers.
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesQuoted 8 lines · Bernd Petrovitsch
On Sun, 2005-07-24 at 18:15 -0400, Puneet Vyas wrote:
[...]Quoted 2 lines
I just compiled two identical program , one with "!=" and other with "^". The assembly output is identical.
Hmm, which compiler and which version?
You might want to try much older and other compilers.
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Here's the patch to make the code more readable.
kernel/signal.c renamed from kernel/signal.c.orig +2 -2
@@ -665,8 +665,8 @@ static int check_kill_permission(int sig (unsigned long)info != 2 && SI_FROMUSER(info))) && ((sig !=SIGCONT ) || (current->signal->session != t->signal->session)) - && (current->euid ^ t->suid) && (current->euid ^ t->uid) - && (current->uid ^ t->suid) && (current->uid ^ t->uid) + && (current->euid != t->suid) && (current->euid != t->uid) + && (current->uid != t->suid) && (current->uid != t->uid) && !capable(CAP_KILL)) return error;
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
Lee
Quoted 6 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
Depends if you want to multiply by 2 or 4 :-)
Phil.
Quoted 10 lines · Philippe Troin
Quoted 6 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
Depends if you want to multiply by 2 or 4 :-)
I guess I just answered my own question ;-)
Lee
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
Depends if you want to multiply by 2 or 4 :-)
I guess I just answered my own question ;-)
To answer for x *= 2 vs x <<= 1:
Use * when you would logically want to do a multiplication, << if you're working on a bitfield. It's just for keeping the code clean enough so that others may understand it.
In the longshot, it does not matter, as gcc will optimize out multiplication with powers of two to bitops.
Jan Engelhardt
Signature · 1 line
| Alphagate Systems, http://alphagate.hopto.org/
On Tue, 2005-07-26 at 08:07 +0200, Jan Engelhardt wrote:
[...]
Quoted 1 line
To answer for x *= 2 vs x <<= 1:
and x += x
Quoted 5 lines
Use * when you would logically want to do a multiplication, << if you're working on a bitfield. It's just for keeping the code clean enough so that others may understand it.
In the longshot, it does not matter, as gcc will optimize out multiplication
^^^ the C compiler
Quoted 1 line
with powers of two to bitops.
And if the C compiler is not doing this properly, such tunings are probably in 100%-\eps cases worthless anyway.
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesQuoted 10 lines · Philippe Troin
Quoted 6 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
Depends if you want to multiply by 2 or 4 :-)
I guess this proves my point :-)
But lets look at the signal.c code as well:
if ((!info || ((unsigned long)info != 1 &&
(unsigned long)info != 2 && SI_FROMUSER(info)))
&& ((sig != SIGCONT) ||
(current->signal->session != t->signal->session))
&& (current->euid ^ t->suid) && (current->euid ^ t->uid)
&& (current->uid ^ t->suid) && (current->uid ^ t->uid)
&& !capable(CAP_KILL))
return error;Why did they do the (current->signal->session != t->signal->session) and not also do (current->signal->session ^ t->signal->session)?
Bit shifting for doubling (or quadrupling) may or may not be confusing, (I don't mind that), but using xor for non-equal is IMO past that line. Since, I usually use xor for bit masks. Looking at the above code, especially since it is not always used, I would think that euid, uid, and suid are all bitmasks.
-- Steve
Quoted 10 lines · Philippe Troin
Quoted 6 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
Depends if you want to multiply by 2 or 4 :-)
Ah, this explains why my drivers never worked...
Lee
Quoted 7 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
I guess this depends on what you logically want to do. If the problem requires you to shift some value N bits, then you should use a shift operation.
If what you want is to multiply a value by a certain ammount, you should just use a multiplication.
Using a shift to perform the multiplication should be left to the compiler IMHO.
The proof that the shift is not so clear is that even you got the shift wrong in your own example ;)
Signature · 5 lines
Paulo Marques - www.grupopie.com It is a mistake to think you can solve any major problems just with potatoes. Douglas Adams
Quoted 13 lines · Paulo Marques
Quoted 7 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
I guess this depends on what you logically want to do. If the problem requires you to shift some value N bits, then you should use a shift operation.
Quoted 9 lines
If what you want is to multiply a value by a certain ammount, you should just use a multiplication.
Using a shift to perform the multiplication should be left to the compiler IMHO.
The proof that the shift is not so clear is that even you got the shift wrong in your own example ;)
Yeah, that was going to be my point, but I made it inadvertently before I even got that far...
Lee
Quoted 8 lines · Lee Revell
Quoted 2 lines · Steven Rostedt
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Where do we draw the line with this? Is x *= 2 preferable to x <<= 2 as well?
In addition to the obvious error, let's not use x += x as well. If you want to multiple by two, do it.
Wasn't there a CPU where multiple was faster than add? Doesn't matter, let the compiler make the optimizations so you don't have to.
Signature · 3 lines
-bill davidsen ([email protected]) "The secret to procrastination is to put things off until the last possible moment - but no longer" -me
PATCH 1/1 make signal.c more readable (was: Re: xor as a lazy comparison)
In reply to Bernd PetrovitschSorry for the double post. I added [PATCH] and changed the subject, and added Linus.
Quoted 8 lines · Bernd Petrovitsch
On Sun, 2005-07-24 at 18:15 -0400, Puneet Vyas wrote:
[...]Quoted 2 lines
I just compiled two identical program , one with "!=" and other with "^". The assembly output is identical.
Hmm, which compiler and which version?
You might want to try much older and other compilers.
Doesn't matter. The cycles saved for old compilers is not rational to have obfuscated code.
Here's the patch to make the code more readable.
kernel/signal.c renamed from kernel/signal.c.orig +2 -2
@@ -665,8 +665,8 @@ static int check_kill_permission(int sig (unsigned long)info != 2 && SI_FROMUSER(info))) && ((sig !=SIGCONT ) || (current->signal->session != t->signal->session)) - && (current->euid ^ t->suid) && (current->euid ^ t->uid) - && (current->uid ^ t->suid) && (current->uid ^ t->uid) + && (current->euid != t->suid) && (current->euid != t->uid) + && (current->uid != t->suid) && (current->uid != t->uid) && !capable(CAP_KILL)) return error;