Re: "Interesting" C behaviours

From: Rennie deGraaf (ca.ucalgary.cpsc_at_degraaf)
Date: 11/27/04


Date: Sat, 27 Nov 2004 07:08:01 GMT

Malcolm wrote:
> "Rennie deGraaf" <ca.ucalgary.cpsc@degraaf> wrote

>>2. In C99, the expression 'a%b' where a<0 and b>0 will return a negative
>>result (K&R lets this be machine dependent).
>>
>
> That would probably be driven by architecture. The idea is that a % b would
> compile to a single machine instruction (on K and R's original platform).
> Just a guess, but I suspect this is the motivation.

It was architecture dependent in K&R, but C99 standardized it (see
http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html#Semantics,
section 25). I know that the x86 idiv instruction spits out negative
residues, but is the fact that a common architecture does something
unusual a reason to standardize a programming language on an unusual
behaviour? It would make more sense to me to work around the
architecture when it does something weird.

GCC, for instance, frequently doesn't even compile a%b to a single idiv
instruction - it compiles it to a big mess of imul, shift, and leal
instructions.

Rennie



Relevant Pages

  • Re: Direct Linux syscalls
    ... that explains why the instruction isn't really optimized. ... _30 year_ lifespan...and become the architecture used in 90+% of machines ... As a simple example, when a process loads, a "jump table" of address to all ... whether you're calling other user mode code or actually calling into kernel ...
    (comp.os.linux.development.apps)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... > the existence of an instruction to draw the proper comparison. ... > memory address pointed to by register based on the tested condition. ... > case is precisely the differing object code for the unconditional branch, ... Won't compile as is ... ...
    (comp.lang.cobol)
  • Re: Question about re-designing an algorithm when hardware architecture is fixed
    ... Instructions - how many of my target machine's instruction does it take ... Intruction Set Architecture to the job at hand. ... measures cache stalls. ... useful with a really good compiler). ...
    (comp.dsp)
  • Re: what uC do you prefer?
    ... of code (buffer over-run exploits - spesh of Billy boi) but that is ... I do hanker for a contiguous RAM area where I ... versions of this architecture are the Coldfire parts. ... that makes instruction fetches slow. ...
    (sci.electronics.basics)
  • Re: Compiling for a different architecture?
    ... > This is starting to sound like the NetBSD supported architecture ... one "/usr/src", then compile for different CPU (PII, PIII, i586). ...
    (comp.unix.bsd.freebsd.misc)