Re: Ternary operator and memory access



Richard wrote On 08/07/06 11:05,:
Possibly I overs implified it : there is no issue with HW if the
"result" of b=g() is used from a register. But clearly if expressions
(1) and (2) are of the form "*a" or "*b" then it would be madness for
the compiler to access *b if the condition is true. Or?

Or, more specifically, suppose we have:

int f(int x, int *ap, int *bp) {
return x ? *ap : *bp;
}

(which is of course just the conditional operator itself, written in
function form, with type restricted to "int" and pointers for the
values involved).

In article <1154965455.873251@news1nwk>
Eric Sosman <Eric.Sosman@xxxxxxx> wrote:
The compiler might not know whether *a,*b are or aren't
safe, but it might be able to tell that it makes no difference.

int *a = ...;
int *b = ...;
*a = 42; /* die here if *a unsafe */
*b = 56; /* die here if *b unsafe */
x = cond ? *a : *b;

The compiler might conclude that if *a,*b are unsafe the final
line won't be executed anyhow, hence a speculative fetch adds
no new failure mode to the program.

Or, the machine may have a "load from memory, but if there is a
fault during loading, ignore it" instruction -- often actually
spelled "prefetch" and having no target register -- in which case,
it can generate:

f_: .global f_
# inputs: r1 = x, r2 = ap, r3 = bp
pref (r2) # prefetch from *ap
pref (r3) # prefetch from *bp
tst r1 # which one should we use?
bz L1
ld r0,(r2) # x != 0, so fetch from *ap
ret
L1:
ld r0,(r3) # fetch from *bp
ret

The two "extra" instructions start RAM -> cache transfers, which
typically take dozens of CPU cycles, if needed. Meanwhile the CPU
can still execute instructions. This means that the during the
delay caused by testing r1 (i.e., x), something useful is still
going on.

(These "prefetch" instructions would be more useful if they were
placed further back in the instruction stream, which is of course
only possible if the routine f() is expanded in line.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.



Relevant Pages

  • Re: Value Types and Reference Types
    ... So your saying that an int is not special? ... What I mean is that the compiler ... but the main point was that boxing is slow. ... Actually a boxing operation takes 11 machine instruction, 10 instruction to allocate the object space on the heap and 1 instruction to move the value into the object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C language on 8-bit microcontrollers
    ... int type must contain at least 16 bits to hold the range of values). ... mean, compilers used on 8-bit processors are not standard compliant, or they ... The compiler must generate code that does the ... more) arithmetic just requires more than one instruction per ...
    (comp.lang.c)
  • Re: [OT] Casting float to int using (int)
    ... > float to int using is a bad idea. ... that this is nothing more than an advice to use 'frndint' instruction on ... applications when using an older compiler (i.e. a compiler that is not ...
    (comp.lang.cpp)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)