View Single Post
Old 2005-07-27, 16:21   #6
R.D. Silverman
 
R.D. Silverman's Avatar
 
Nov 2003

22·5·373 Posts
Thumbs up

Quote:
Originally Posted by akruppa
Oh. right. I hadn't noticed. If this is the code you're using

__inline int iceil(double a)
{
int d;
const double h = 0.5000001f;
_asm
{
fld a
fadd h
fist d
};
return d;
}

it's surprising it works at all. If you load a value onto the stack with FLD, you need to get rid of it again, so use FISTP. Otherwise you'll just keep pushing things onto the stack and any value that was on it will never see the light of day again. The FIST would be good if you had a way to tell the compiler that the value is staying there, which is easy with the gcc asm() block - I don't know how to do it with VC, tough. Also, make the _asm_ block volatile so the compiler keeps it in a single block - reordering might lead to trouble if the compiler does not know about what is going on on the FPU stack.

I REALLY recommend using the lrint() function instead.

Alex

Popping the is indeed correct. As for lrint(), Microsoft VC++ doesn't
seem to know about it. At least, it is not indexed under 'help'' at all.
R.D. Silverman is offline   Reply With Quote