[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Long division in Mintlibs



I started having some wierd problems with atol() and printf() at the
weekend, and eventually traced the problem to the integer long division
routines that are used to emulate the 68030 divs.l and divu.l instructions.

Could someone please try the following with the gcc port of the Mintlibs,
to see if the bug is in the Lattice version of the division code (it looks
like it was directly converted from the gcc version).

-----
#include <stdio.h>

void PrintLong(long val)
{
	union {
		long a;
		unsigned char b[4];
	} u;
	u.a = val;
	printf("%ld = 0x%08lx = 0x%02x%02x%02x%02x\n",
		u.a, u.a, u.b[0], u.b[1], u.b[2], u.b[3]);
}

long ten(void) { return (long)10; }

int main(void)
{
	PrintLong((long)0x7fffffff / ten());
	PrintLong((long)0x80000000 / ten());
	PrintLong((long)0x80000001 / ten());
}
-----

The ten() function is used to stop the compiler performing the division
itself. I don't know if gcc will need something similar.

If I build the above code for a 68030 and use 68030 versions of printf,
everything works fine. If I build for a 68000 printf starts printing
random characters, and the division results are wrong.

Chris