long conversion of short expression.

Chris Torek chris at mimsy.UUCP
Fri Jul 28 22:14:30 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. 

Most likely because 68000 compilers do not agree as to whether `int'
is the same as `short' or as `long'.

>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?

Neither.

The result of shortvar*shortvar has type int.  (If one variable is
unsigned short, then according to the pANS, the result type is machine
dependent, although the result value is not, provided it does not
overflow.)  If the result overflows, the value is undefined; a clever
int-is-16-bits-style 68000 compiler may produce the correct value by
noticing that the 68000 `muls' instruction produces a 32 bit result,
and carrying that around even though its result type (int) was supposed
to have been only 16 bits.  This is not required by any C standard,
but is permitted since the result value is undefined.  Of course, if
you stuff the 32-bit value into a 16-bit slot, something has to give;
usually it is the value that breaks, rather than the slot.

The result of (intexpr) + (longexpr) has type long.

Anyway:

>	short x=0x18,y=0x588;
>	long z=0x100000;
>	printf("%lx\n",z+(x*y));

>Should the compiler generate
>	move -2(fp),d0
>	muls -4(fp),d0
>	extl d0
>	addl -8(fp),d0

This sequence is typical of a dumb int-is-16-bits compiler.

>or should the extl instruction be left out?

The sequence without the extl is typical of a smarter compiler
of either flavour.

>Both the Sun and GNU compilers do not use the extl, but I have
>seen other compilers that would.

Sun's cc is a moderatly intelligent int-is-32-bit 680x0 compiler
derived from PCC.  GCC as configured for a 680x0 is a smart
int-is-32-bit compiler.  Both realise that there is no point in doing

	extl	-2(fp),d0
	extl	-4(fp),d1
	jsr	lmult		| or some other name
	addl	-8(fp),d0

when the result of both `extl's will have either 0 or 0xffff in
the top 16 bits, and therefore the result from `muls' will be the
same as that from the `multiply two longs' subroutine.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list