Re: [AD] Borland C++ compilation errors |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
I think the following functions should work correctly for most
architectures. Number 0x7FFFFFFF is special for fceil, because itofix
of 0x8000 will give negative number.
int
fceil (long v)
{
if (v < 0)
return -(int) ((unsigned long) -v >> 16);
else
return (int) (((unsigned long) v + 0xFFFF) >> 16);
}
int
ffloor (long v)
{
if (v < 0)
return -(int) (((unsigned long) -v + 0xFFFF) >> 16);
else
return (int) ((unsigned long) v >> 16);
}
/*
* Test for fceil and ffloor (assumes int is at least 32-bit).
*/
int
main (void)
{
int a;
for (a = -0x8000; a < 0x8000; a++)
{
long x = (a << 16);
int i;
if (a != fceil (x))
printf ("fceil (%x) = %x\n", x, fceil (x));
if (a != ffloor (x))
printf ("ffloor (%x) = %x\n", x, ffloor (x));
for (i = 1; i < 0x10000; i++)
{
x = (a << 16) | i;
if (fceil (x) != (a + 1))
printf ("fceil (%x) = %x\n", x, fceil (x));
if (ffloor (x) != a)
printf ("ffloor (%x) = %x\n", x, ffloor (x));
}
}
return 0;
}
--
Michael Bukin