Path: utzoo!yunexus!geac!syntron!jtsv16!uunet!peregrine!elroy!ames!ncar!mailrus!uflorida!novavax!proxftl!bill From: bill@proxftl.UUCP (T. William Wells) Newsgroups: comp.lang.c Subject: Re: Multiplying two shorts... Message-ID: <576@proxftl.UUCP> Date: 12 Aug 88 12:37:48 GMT Article-I.D.: proxftl.576 References: <948@srs.UUCP> <8101@alice.UUCP> Reply-To: bill@proxftl.UUCP (T. William Wells) Organization: Proximity Technology, Ft. Lauderdale Lines: 46 Summary: Expires: Sender: Followup-To: Distribution: Keywords: In article <8101@alice.UUCP> ark@alice.UUCP writes: : In article <948@srs.UUCP>, dan@srs.UUCP writes: : : > Sun's compilers seem to let you multiply two short integers and get all : > 32 product bits, if you code like this: : > register short x, y; : > register long z; : : > z = x * y; : : > If the compiler is nice, it emits the correct (16x16=32) multiply instruction. : > If it isn't nice, it emits the slower (32x32=64) instruction and throws : > away the top 32 bits. : : > Do most modern compilers perform this optimization? : > It would be nice to be able to depend on it. : : The effect of the statment above is undefined. That is not true. The bit of code above is equivalent to: register short x, y; register long z; int tmp1; int tmp2; int tmp3; tmp1 = (int)x; tmp2 = (int)y; tmp3 = tmp1 * tmp2; z = (long)tmp3; What this means is that if the product of the values of the two shorts will fit in an int, the code will work as expected. If not, then the result IS undefined. To make this statement work do: : z = (long) x * y; as suggested. And, as long as the product of the two shorts will fit in a long, this will work. --- Bill novavax!proxftl!bill