[Coco] OT: Compiler fun - Was Drivewire mixed results

David Gacke dgacke at ektarion.com
Mon Sep 29 18:53:02 EDT 2008


Hi Chuck,

I wrote a very simple, contrived program that took the below listed
function, and then jammed 4 calls to it into the _mm_slli_pi16 intrinsic
(basically shift 4 16-bit words logical left)

The only situation that I could create in which the int return variable went
sour, is when the function in question was placed inside a library.

The library interface then becomes hardwired to a 32 bit return value. This
then made the MS compiler I was using treat it as a 32 bit value in all
instances.

Every time I tried to con the compiler into believing that the return value
from a function that was actually "inside" the same program was 32 bits, it
never believed me. It was too smart. It would continue to keep things neat
and tidy, and  within registers.

It would always unroll the return code to the point where there was no
actual return, push, pop, etc.  (Again, this was a trivial example) 

With all this said, the library example alone makes it a good idea.

What happened in the case of my trivial library example was just a bunch of
extra used stack space and dirty registers (it had 32 bit values on the
stack after the returns, but was just truncating the upper 16 bits of
registers)



This was the function inside the library:

JUNKLIB_API int wrongreturn(char ugly1, char ugly2)
{
	short result=0;
	result=ugly1*ugly2;
	return (result);
}


Anyway, I still stand by my earlier suggestion of telling the compiler
exactly what you need, and then let "it" work out the details.


Best Regards,


Dave Gacke


P.S. On a different, unrelated note:

I saw earlier that you mentioned you were building an Ethernet adapter for
the CoCo. How is that going? And what were your plans for an IP stack? I'm
really curious about that one. 




     > -----Original Message-----
     > From: coco-bounces at maltedmedia.com [mailto:coco-
     > bounces at maltedmedia.com] On Behalf Of Chuck Youse
     > Sent: Monday, September 29, 2008 11:33 AM
     > To: CoCoList for Color Computer Enthusiasts
     > Subject: Re: [Coco] Drivewire mixed results...
     > 
     > On Mon, 2008-09-29 at 10:47 -0500, David Gacke wrote:
     > > Hi Chuck,
     > >
     > > This isn't quite accurate.
     > 
     > > While in this particular case it really doesn't apply, Alex is
     > correct.
     > >
     > > Especially with SSE(SIMD) enabled processors, you should pass
     > and return
     > > what you need.
     > >
     > > This lets the compiler make the most of its optimization
     > possibilities. If
     > > the compiler wants to promote the variable, so be it.
     > > If you force a variable promotion by returning something larger
     > than what
     > > you need, the compiler won't be able to take advantage of
     > knowing the true
     > > variable size, and might not be able to make an optimization
     > where one was
     > > available.
     > >
     > > Also, you are assuming in the above statement that the compiler
     > didn't
     > > inline the function on you. Bad assumption on certain compilers
     > with certain
     > > optimizations set.
     > 
     > Alright, conceded.  Compilers are explicitly allowed by standard
     > to skip
     > the promotions if they can guarantee the results will not differ.
     > In
     > conventional architectures and with conventional compilers,
     > promotions
     > are almost always performed because there's nothing to be gained
     > by
     > using shorter integers - in fact, it's often less efficient.
     > 
     > But then you had to go and bring up SIMD and compilers with
     > aggressive
     > global optimizations, and I'll grant that theoretically you have
     > some
     > ground there - but aside from the most obvious cases (the use of
     > constants, etc.) I don't see how an optimizer could make use of
     > any of
     > this information while still guaranteeing standard behavior.
     > 
     > Maybe you could provide me with some more concrete (non-trivial)
     > examples to put me in my place!  As many people on the list
     > probably
     > think (frequently), I could use a slice of humble pie once in a
     > while ... I gladly eat when it's deserved ..
     > 
     > C.
     > 
     > 
     > 
     > --
     > Coco mailing list
     > Coco at maltedmedia.com
     > http://five.pairlist.net/mailman/listinfo/coco




More information about the Coco mailing list