Re: What the?



On Tue, 12 Feb 2008 23:10:55 -0500, Joseph M. Newcomer
<newcomer@xxxxxxxxxxxx> wrote:

Actually a++ and ++a generate the same code in this context. There is a common myth about
the relative efficiency of these operations; this mythology is largely based on the PDP-11
instruction set which had a pre-increment and post-decrement addressing mode built into
the instruction set. On the PDP-11, a++ and --a were the preferred forms because the
increment and decrement could be built into the instruction address bits (in fact, in the
early versions of the C compiler a++ existed but ++a was a syntax error, and a-- did not
exist and was a syntax error, but --a compiled. Eventually, the symmetry was introduced,
and the a++/++a controversy arose. In modern architectures that do not possess
auto-increment addressing modes, and in the presence of optimizing compilers, this rarely
matters, and when it does, remember that a 2.8GHz Pentium4 will execute a single extra
instruction in 175ps, not really a big deal (that's a 350ps clock dispatching two integer
instructions per clock cycle).

The modern C++ issue concerns postincrement returning a copy of the
original value. Since iterators can be arbitrary class types, this is
potentially more expensive than using preincrement, which returns the
incremented object by reference. Therefore, the standard advice is to use
preincrement when there's a choice. For reference, here are canonical
definitions of the operators:

// ++x;

T&
T::operator++()
{
inc();
return *this;
}

// x++;

T
T::operator++(int)
{
T temp(*this);
inc();
return temp;
}

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • [PATCH] x86-64 kprobes: handle %RIP-relative addressing mode
    ... The existing x86-64 kprobes implementation doesn't cope with the ... an instruction overwritten by a breakpoint. ... instruction that uses the %RIP-relative data addressing mode, ... This patch fixes the problem by recognizing the %RIP-relative addressing ...
    (Linux-Kernel)
  • Re: What the?
    ... the relative efficiency of these operations; this mythology is largely based on the PDP-11 ... instruction set which had a pre-increment and post-decrement addressing mode built into ... increment and decrement could be built into the instruction address bits (in fact, ...
    (microsoft.public.vc.mfc)
  • Re: "Complete exercise" of x86 example?
    ... bit instruction set (excluding 8086-specific and intel-specific quirks ... such as POP CS and AAD/AAM with custom operand). ... something, or screw up a lesser-known addressing mode, or some other ... A complete set of all possible instruction variants and addressing ...
    (comp.lang.asm.x86)
  • Re: New ARM Cortex Microcontroller Product Family from STMicroelectronics
    ... The data is embedded inside the instruction, ... into R0, using immediate addressing mode. ... to the ALU using an internal mux in the CPU core it is original harvard. ...
    (comp.arch.embedded)
  • Re: Highly used programs: any better replacements out there?
    ... What is at issue is changing a datum that is not an instruction. ... >Similarly overlooking a reference to a source code instruction ... and there's nothing wrong with source code that uses them. ...
    (bit.listserv.ibm-main)