[OT] Re: choice of operation (-- or =-1)
- From: Nate Eldredge <nate@xxxxxxxxxx>
- Date: Sun, 05 Apr 2009 23:42:22 -0700
luserXtrog <mijoryx@xxxxxxxxx> writes:
On Apr 5, 5:59 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:
Cross wrote:
Hello
When the value of a variable is zero,is decrement preferable or assigment?
Generalizing the question, how do I know which operation takes how much time to
complete on an exact scale or may be a relative scale?
It will depend on your implementation.
(x = 0, --x) and (x = 0, x = -1) should both be pretty quick.
A long long time ago,
one of my instructors told me of a system where it was faster
to bitwise xor a register with itself
(resulting with a value of zero in that register),
than it was to assign a value of zero to that register,
but I have never seen a system like that.
Actually, I think this is likely to be true with CISC archs
(ie. x86). If the instruction refers solely to registers, it
will likely be shorter than a MOV with a literal. But it is
seriously trivial. It *might* save a byte. One measly little
byte. Maybe. On a RISC or VLIW arch, there may be no difference
(size/speed-wise).
Indeed, this is a common idiom on x86. On the 386 family in 32-bit
mode, zeroing a register in the "obvious" way with "mov reg, 0" requires
a six-byte instruction: one byte for the opcode, one byte which
specifies the destination register, and four bytes for the 32-bit
immediate operand. But "xor reg, reg" or "sub reg, reg" are only two
bytes. So you save *four* measly little bytes :) This is significant in
situations where code space is at a premium, or in general to fit more
code in cache, and compilers will generally do it.
This is common to the point that seeing "mov reg, 0" in an assembly
language listing is generally a tipoff that it was written by a novice
programmer, or a poor compiler. Programmers have stylistic disputes
over whether "xor reg, reg" or "sub reg, reg" is preferable, though
"xor" seems to be more popular (as far as I know, they are equivalent
performance-wise). The trick has been around since the 16-bit 8086
days, where the savings was only two bytes.
(This is also convenient for people writing shellcode to exploit buffer
overflows: the instruction "mov reg, 0" contains four zero bytes, which
can be a problem if overflowing a null-terminated string. "xor reg, reg"
doesn't have this problem.)
In fact, if moving 1 into a register, one has the option to do
xor reg, reg
inc reg
which is three (nonzero!) bytes, instead of six for "mov reg, 1".
However, it would normally take more cycles (4 instead of 2 on i386).
-1 can be achieved by using "dec reg" or "not reg" (one's complement).
Some architectures, including, I believe, SPARC, have the clever feature
of a virtual "register zero" whose value is always zero. Thus one can
zero out a register by moving r0 into it, using a fast and small
register-to-register move instruction.
(This also brings to mind the x86 programmer's pastime of finding "long
no-ops": instructions which occupy several bytes and do nothing, but
execute faster than the corresponding sequence of one-byte "nop"
instructions. These could be useful for inserting padding, if a
particular instruction (perhaps a jump target) should be aligned on some
power-of-two boundary. Indeed, the "nop" opcode itself logically
belongs to the instruction "xchg eax, eax" which swaps the eax register
with itself.)
Sorry for the digression.
.
- Follow-Ups:
- Re: [OT] Re: choice of operation (-- or =-1)
- From: Coos Haak
- Re: [OT] Re: choice of operation (-- or =-1)
- From: Beej Jorgensen
- Re: [OT] Re: choice of operation (-- or =-1)
- References:
- choice of operation (-- or =-1)
- From: Cross
- Re: choice of operation (-- or =-1)
- From: pete
- Re: choice of operation (-- or =-1)
- From: luserXtrog
- choice of operation (-- or =-1)
- Prev by Date: Ed Hardy Swimwear Eternal Love Bikini.–Boutique to You.(www.trade8.cc)
- Next by Date: Re: choice of operation (-- or =-1)
- Previous by thread: Re: choice of operation (-- or =-1)
- Next by thread: Re: [OT] Re: choice of operation (-- or =-1)
- Index(es):
Relevant Pages
|