Long Longs

KW Heuer kwh at bentley.UUCP
Tue Mar 4 07:13:02 AEST 1986


In article <491 at faron.UUCP> faron!bs (Bob Silverman) writes:
>[How do I calculate without intermediate overflow]
>a = (b*c)/d;
>a = (b*c) % d;
>... Many machines CAN multiply two full words together giving a double
>length product placed in two adjacent registers. 

Many others can't.  If the feature were built into the language,
what code should it generate for "long long" arithmetic on a more
limited machine?

>Does C++ have long longs?

C++ cannot do anything that C cannot, since it produces C output.

>Does anyone have a good solution?

My prefered method (to maximize portability) is to have library
functions
	int muldiv(int, int, int)
	int mulrem(int, int, int)
which are written in assembly language; for specific applications
in tight loops I hand-optimize the .s file as described in article
<3408 at umcp-cs.UUCP> by umcp-cs!chris (Chris Torek) (not quoted here).

It seems to me that best solution for the future is for "int" to be
32 bits wide, while "long" is 64.  I expect the compilers will start
doing this once there is a little more support for 64-bit arithmetic
in the hardware.  (Like when 32-bit addresses aren't big enough.)
No smiley on this paragraph, I'm serious.

Incidentally, does anyone have a muldiv routine (in C or as) for a
32-bit machine without emul/ediv instructions?  Currently I'm using
(int)((double)x*(double)y/(double)z) which is quite slow on a 3b2
without floating-point hardware.

Karl W. Z. Heuer (ihnp4!bentley!kwh), The Walking Lint



More information about the Comp.lang.c mailing list