jrv@mitre-bedford.ARPA (08/27/84)
I was using a FORTH (pardon the profanity) the other day and found a neat new operator: /*. It has the effect of multiplying two integers, then dividing by a third WITH A DOUBLE LENGTH INTERMEDIATE, so it won't overflow unless the final result does. It's handy when floating point operations are unavailable or too slow (such as in graphics). Most machines with multiplication give a double length result, and most with division can start with a double length dividend. However, C doesn't presently give us access to these operations. My question: what's a reasonable C syntax for it? The obvious one is a/b*c, but it's already spoken for. Are there compilers that let you get this effect by explicitly declaring the intermediate: short a,b,c,d; long temp; ... temp=a*b; d=temp/c; Of course, you can define this as a a function in assembly language, but the calling overhead would reduce the speed advantage. Any thoughts would be welcome. - Jim Van Zandt
BLARSON@ecld.#eclnet (08/27/84)
From: Bob Larson <BLARSON@ecld.#eclnet> There is no need for a temporary: d=((long)(a*b))/c; of course, both this and your example have the disadvantage of the conversion to long is done AFTER the multiply. Better would be: d= (long)a*b/c; where all computation is done in long. Best would be to specify optional (to compiler writer) optimization algorythms: Multiplying two ints resulting in long should use special opcode to do this where practical. Dividing a long by an int resulting in int should use special opcode where practical. Obviously this should also apply to int/short and short/char where applicable. Bob Larson <Blarson@Usc-Ecl.arpa> -------
gwyn@BRL-VLD.ARPA (08/27/84)
From: Doug Gwyn (VLD/VMB) <gwyn@BRL-VLD.ARPA> Try short a, b, c, d; d = (long)a * b / c; The one typecast is sufficient to force all the arithmetic to be done with long arithmetic, then the result is coerced back into short on assignment to `d'. If your compiler is sufficiently clever, it will take advantage of the fact that the result of the division is only needed as a short; in any case it is obligated to return the correct answer.