long conversion of short expression.

R. Vuurboom roelof at idca.tds.PHILIPS.nl
Fri Jul 28 19:48:19 AEST 1989


In article <9092 at chinet.chi.il.us> pdg at chinet.chi.il.us (Paul Guthrie) writes:
>Here is a case where 68000 compilers don't seem to agree. 
>In the following code segment, the two shorts multiplied together
>exceed the size of a short, but the question is, is the result
>of the multiplication really a short to be converted to a
>long, or a long already?
>
>        main()
>        {
>
>                short x=0x18,y=0x588;
>                long z=0x100000;
>                printf("%lx\n",z+(x*y));
>        }
> 
Following the "usual arithmetic conversions" (cf. Ansi Draft 3.2, K&R v.2
pp. 197) x and y being shorts will be promoted to ints (not necessarily longs)
and a normal int multiplication will take place. Depending on what the
compiler considers to be natural int size the code below may or may not be
correct.

If the int size is the same as that of long then the
code is not correct since the multiplication with subsequent extl will give 
wrong answers if the result does not fit into a short (your example).

If on the other hand the int size is considered a short then 
the is correct since the extl extends the result of the multiplication 
to the long int size for the addition (again a "usual arithmetic conversion").


>Should the compiler generate
>	move -2(fp),d0
>	muls -4(fp),d0
>	extl d0
>	addl -8(fp),d0
>or should the extl instruction be left out?  

Conclusion: 
If your compiler considers the int size to be the same as that of long 
(normal for motorola compilers) then the generated code is incorrect.

If your compiler considers the natural int size to be the same as that
of short then the generated code can be considered correct. (But
maybe you should get yourself a new compiler :-).
-- 
I don't know what the question means, but the answer is yes...
(overheard on comp.lang.misc)
Roelof Vuurboom  SSP/V3   Philips TDS Apeldoorn, The Netherlands   +31 55 432226
domain: roelof at idca.tds.philips.nl             uucp:  ...!mcvax!philapd!roelof



More information about the Comp.lang.c mailing list