* Re: xor as a lazy comparison
@ 2005-07-27 18:24 Clayton Weaver
2005-07-27 19:58 ` linux-os (Dick Johnson)
0 siblings, 1 reply; 19+ messages in thread
From: Clayton Weaver @ 2005-07-27 18:24 UTC (permalink / raw)
To: linux-kernel
Is not xor (^) typically compiled to a
one cycle instruction regardless of
requested optimization level? (May not
always have been the case on every
target architecture for != equality
tests.)
Clayton Weaver
cgweav at fastmail dot fm
PS:
Anyone know where I can get
a waterproof, battery powered gps that will fit inside the handle of a Fenwick?
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-27 18:24 xor as a lazy comparison Clayton Weaver
@ 2005-07-27 19:58 ` linux-os (Dick Johnson)
2005-07-28 0:04 ` Grant Coady
0 siblings, 1 reply; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-07-27 19:58 UTC (permalink / raw)
To: Clayton Weaver; +Cc: linux-kernel
On Wed, 27 Jul 2005, Clayton Weaver wrote:
> Is not xor (^) typically compiled to a
> one cycle instruction regardless of
> requested optimization level? (May not
> always have been the case on every
> target architecture for != equality
> tests.)
> Clayton Weaver
> cgweav at fastmail dot fm
>
I think the XOR thread was started by somebody as a ruse or
a joke. XOR will always destroy the value of an operand. This
means that it needs to be loaded and perhaps reloaded. A 'TEST'
instruction or a 'CMP' instruction never destroys anything and
only affects the flags. TEST is an AND in which the result is
not saved, only the flags get changed. A CMP is a subtract in
which the result is not saved, only the flags. These are
ix86 instructions, but similar instructions exist for other
CPUs.
All these instructions in their simplest form (using registers)
are two-byte instructions!
Disassembly of section .text:
00000000 <.text>:
0: 39 c3 cmp %eax,%ebx
2: 31 c3 xor %eax,%ebx
4: 85 d8 test %ebx,%eax
Instructions that access memory use more bytes...
Cheers,
Dick Johnson
Penguin : Linux version 2.6.12 on an i686 machine (5537.79 BogoMips).
Warning : 98.36% of all statistics are fiction.
.
I apologize for the following. I tried to kill it with the above dot :
****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.
Thank you.
^ permalink raw reply [flat|nested] 19+ messages in thread
* xor as a lazy comparison
@ 2005-07-24 16:40 Jan Engelhardt
2005-07-24 20:07 ` Grant Coady
0 siblings, 1 reply; 19+ messages in thread
From: Jan Engelhardt @ 2005-07-24 16:40 UTC (permalink / raw)
To: Linux Kernel Mailing List
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
--
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: xor as a lazy comparison
2005-07-24 16:40 Jan Engelhardt
@ 2005-07-24 20:07 ` Grant Coady
2005-07-24 21:43 ` Jan Engelhardt
0 siblings, 1 reply; 19+ messages in thread
From: Grant Coady @ 2005-07-24 20:07 UTC (permalink / raw)
To: Jan Engelhardt; +Cc: Linux Kernel Mailing List
On Sun, 24 Jul 2005 18:40:25 +0200 (MEST), Jan Engelhardt <jengelh@linux01.gwdg.de> wrote:
>
>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 ^ ?
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.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-24 20:07 ` Grant Coady
@ 2005-07-24 21:43 ` Jan Engelhardt
2005-07-24 22:15 ` Puneet Vyas
0 siblings, 1 reply; 19+ messages in thread
From: Jan Engelhardt @ 2005-07-24 21:43 UTC (permalink / raw)
To: Grant Coady; +Cc: Linux Kernel Mailing List
>To confuse you, coders with assembly or hardware background throw in
I doubt that. I'm good enough assembly to see this :)
>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
--
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-24 21:43 ` Jan Engelhardt
@ 2005-07-24 22:15 ` Puneet Vyas
2005-07-25 8:57 ` Bernd Petrovitsch
0 siblings, 1 reply; 19+ messages in thread
From: Puneet Vyas @ 2005-07-24 22:15 UTC (permalink / raw)
To: Jan Engelhardt; +Cc: Grant Coady, Linux Kernel Mailing List
Jan Engelhardt wrote:
>>To confuse you, coders with assembly or hardware background throw in
>>
>>
>
>I doubt that. I'm good enough assembly to see this :)
>
>
>
>>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.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-24 22:15 ` Puneet Vyas
@ 2005-07-25 8:57 ` Bernd Petrovitsch
2005-07-25 17:55 ` Steven Rostedt
0 siblings, 1 reply; 19+ messages in thread
From: Bernd Petrovitsch @ 2005-07-25 8:57 UTC (permalink / raw)
To: Puneet Vyas; +Cc: Jan Engelhardt, Grant Coady, Linux Kernel Mailing List
On Sun, 2005-07-24 at 18:15 -0400, Puneet Vyas wrote:
[...]
> 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
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: xor as a lazy comparison
2005-07-25 8:57 ` Bernd Petrovitsch
@ 2005-07-25 17:55 ` Steven Rostedt
2005-07-25 19:10 ` Lee Revell
0 siblings, 1 reply; 19+ messages in thread
From: Steven Rostedt @ 2005-07-25 17:55 UTC (permalink / raw)
To: Bernd Petrovitsch
Cc: Andrew Morton, Linux Kernel Mailing List, Grant Coady,
Jan Engelhardt, Puneet Vyas
On Mon, 2005-07-25 at 10:57 +0200, Bernd Petrovitsch wrote:
> On Sun, 2005-07-24 at 18:15 -0400, Puneet Vyas wrote:
> [...]
> > 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.
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
--- linux-2.6.13-rc3/kernel/signal.c.orig 2005-07-25 13:50:20.000000000 -0400
+++ linux-2.6.13-rc3/kernel/signal.c 2005-07-25 13:50:51.000000000 -0400
@@ -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;
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-25 17:55 ` Steven Rostedt
@ 2005-07-25 19:10 ` Lee Revell
2005-07-25 19:16 ` Philippe Troin
` (2 more replies)
0 siblings, 3 replies; 19+ messages in thread
From: Lee Revell @ 2005-07-25 19:10 UTC (permalink / raw)
To: Steven Rostedt
Cc: Bernd Petrovitsch, Andrew Morton, Linux Kernel Mailing List,
Grant Coady, Jan Engelhardt, Puneet Vyas
On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
> 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
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: xor as a lazy comparison
2005-07-25 19:10 ` Lee Revell
@ 2005-07-25 19:16 ` Philippe Troin
2005-07-25 19:18 ` Lee Revell
` (2 more replies)
2005-07-25 19:23 ` Paulo Marques
2005-07-25 20:24 ` Bill Davidsen
2 siblings, 3 replies; 19+ messages in thread
From: Philippe Troin @ 2005-07-25 19:16 UTC (permalink / raw)
To: Lee Revell
Cc: Steven Rostedt, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Jan Engelhardt,
Puneet Vyas
Lee Revell <rlrevell@joe-job.com> writes:
> On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
> > 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.
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: xor as a lazy comparison
2005-07-25 19:16 ` Philippe Troin
@ 2005-07-25 19:18 ` Lee Revell
2005-07-26 6:07 ` Jan Engelhardt
2005-07-25 19:24 ` Steven Rostedt
2005-07-25 19:27 ` Lee Revell
2 siblings, 1 reply; 19+ messages in thread
From: Lee Revell @ 2005-07-25 19:18 UTC (permalink / raw)
To: Philippe Troin
Cc: Steven Rostedt, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Jan Engelhardt,
Puneet Vyas
On Mon, 2005-07-25 at 12:16 -0700, Philippe Troin wrote:
> Lee Revell <rlrevell@joe-job.com> writes:
>
> > On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
> > > 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
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-25 19:18 ` Lee Revell
@ 2005-07-26 6:07 ` Jan Engelhardt
2005-07-26 8:30 ` Bernd Petrovitsch
0 siblings, 1 reply; 19+ messages in thread
From: Jan Engelhardt @ 2005-07-26 6:07 UTC (permalink / raw)
To: Lee Revell
Cc: Philippe Troin, Steven Rostedt, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Puneet Vyas
>> > 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
--
| Alphagate Systems, http://alphagate.hopto.org/
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-26 6:07 ` Jan Engelhardt
@ 2005-07-26 8:30 ` Bernd Petrovitsch
0 siblings, 0 replies; 19+ messages in thread
From: Bernd Petrovitsch @ 2005-07-26 8:30 UTC (permalink / raw)
To: Jan Engelhardt
Cc: Lee Revell, Philippe Troin, Steven Rostedt, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Puneet Vyas
On Tue, 2005-07-26 at 08:07 +0200, Jan Engelhardt wrote:
[...]
> To answer for x *= 2 vs x <<= 1:
and x += x
> 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
> 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
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-25 19:16 ` Philippe Troin
2005-07-25 19:18 ` Lee Revell
@ 2005-07-25 19:24 ` Steven Rostedt
2005-07-25 19:27 ` Lee Revell
2 siblings, 0 replies; 19+ messages in thread
From: Steven Rostedt @ 2005-07-25 19:24 UTC (permalink / raw)
To: Philippe Troin
Cc: Lee Revell, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Jan Engelhardt,
Puneet Vyas
On Mon, 2005-07-25 at 12:16 -0700, Philippe Troin wrote:
> Lee Revell <rlrevell@joe-job.com> writes:
>
> > On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
> > > 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
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: xor as a lazy comparison
2005-07-25 19:16 ` Philippe Troin
2005-07-25 19:18 ` Lee Revell
2005-07-25 19:24 ` Steven Rostedt
@ 2005-07-25 19:27 ` Lee Revell
2 siblings, 0 replies; 19+ messages in thread
From: Lee Revell @ 2005-07-25 19:27 UTC (permalink / raw)
To: Philippe Troin
Cc: Steven Rostedt, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Jan Engelhardt,
Puneet Vyas
On Mon, 2005-07-25 at 12:16 -0700, Philippe Troin wrote:
> Lee Revell <rlrevell@joe-job.com> writes:
>
> > On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
> > > 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
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-25 19:10 ` Lee Revell
2005-07-25 19:16 ` Philippe Troin
@ 2005-07-25 19:23 ` Paulo Marques
2005-07-25 19:25 ` Lee Revell
2005-07-25 20:24 ` Bill Davidsen
2 siblings, 1 reply; 19+ messages in thread
From: Paulo Marques @ 2005-07-25 19:23 UTC (permalink / raw)
To: Lee Revell
Cc: Steven Rostedt, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Jan Engelhardt,
Puneet Vyas
Lee Revell wrote:
> On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
>
>>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 ;)
--
Paulo Marques - www.grupopie.com
It is a mistake to think you can solve any major problems
just with potatoes.
Douglas Adams
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-25 19:23 ` Paulo Marques
@ 2005-07-25 19:25 ` Lee Revell
0 siblings, 0 replies; 19+ messages in thread
From: Lee Revell @ 2005-07-25 19:25 UTC (permalink / raw)
To: Paulo Marques
Cc: Steven Rostedt, Bernd Petrovitsch, Andrew Morton,
Linux Kernel Mailing List, Grant Coady, Jan Engelhardt,
Puneet Vyas
On Mon, 2005-07-25 at 20:23 +0100, Paulo Marques wrote:
> Lee Revell wrote:
> > On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
> >
> >>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 ;)
>
Yeah, that was going to be my point, but I made it inadvertently before
I even got that far...
Lee
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: xor as a lazy comparison
2005-07-25 19:10 ` Lee Revell
2005-07-25 19:16 ` Philippe Troin
2005-07-25 19:23 ` Paulo Marques
@ 2005-07-25 20:24 ` Bill Davidsen
2 siblings, 0 replies; 19+ messages in thread
From: Bill Davidsen @ 2005-07-25 20:24 UTC (permalink / raw)
To: Lee Revell
Cc: Bernd Petrovitsch, Andrew Morton, Linux Kernel Mailing List,
Grant Coady, Jan Engelhardt, Puneet Vyas
Lee Revell wrote:
> On Mon, 2005-07-25 at 13:55 -0400, Steven Rostedt wrote:
>
>>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.
--
-bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
last possible moment - but no longer" -me
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2005-07-28 0:04 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-27 18:24 xor as a lazy comparison Clayton Weaver
2005-07-27 19:58 ` linux-os (Dick Johnson)
2005-07-28 0:04 ` Grant Coady
-- strict thread matches above, loose matches on Subject: below --
2005-07-24 16:40 Jan Engelhardt
2005-07-24 20:07 ` Grant Coady
2005-07-24 21:43 ` Jan Engelhardt
2005-07-24 22:15 ` Puneet Vyas
2005-07-25 8:57 ` Bernd Petrovitsch
2005-07-25 17:55 ` Steven Rostedt
2005-07-25 19:10 ` Lee Revell
2005-07-25 19:16 ` Philippe Troin
2005-07-25 19:18 ` Lee Revell
2005-07-26 6:07 ` Jan Engelhardt
2005-07-26 8:30 ` Bernd Petrovitsch
2005-07-25 19:24 ` Steven Rostedt
2005-07-25 19:27 ` Lee Revell
2005-07-25 19:23 ` Paulo Marques
2005-07-25 19:25 ` Lee Revell
2005-07-25 20:24 ` Bill Davidsen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox