19.12 x 1.14

The compiler could do a 16x16-->32 unsigned multiply, though.

Personally, I would use &0xffff instead of the cast.

-- glen

Reply to
Glen Herrmannsfeldt
Loading thread data ...

Thanks. But I have looked at 16-bit c compiler cases and examined the output. So while what you say is true for 32-bit platforms, it isn't everywhere true for c compilers.

Jon

Reply to
Jon Kirwan

IIRC "long long" is part of C99. Perhaps that standard says something specifically about the above expression, but what you describe does not seem consistent with the spirit of earlier C standards. I would think you would have to promote the operands.

Steve

Reply to
Steve Pope

Assuming the multiply instruction gives the same result as the promoted operands, it should be fine.

Most high-level languages define multiply such that the product has the same width as the operands. (PL/I is one exception.)

For the unsigned case, to do it right, it would have to multiply zero by the low 32 bits of both operands, shift left 32 bits and add. Slightly more complicated for the signed case.

You do have to have the cast, otherwise it converts the

32 bit product to 64 bits after bits have been lost.

(In case you didn't notice, cast has higher precedence than multiply, so a is promoted, then b to match a.)

-- glen

Reply to
Glen Herrmannsfeldt

Actually it is perfectly consistent. But maybe you understood "single multiply instruction" to mean something different from what Glen and I did: a 32*32->64 bit multiply.

C has always had its spirit regarding integer operators defined by the following rules:

1) no arithmetic with anything smaller than int 2) binary operators cast up to the least common superset type (beware if you mix signed and unsigned!) 3) results have the same type as that least common superset type

The cast pushes 'a' up to long long, so the binary operator casts 'b' up to long long, too, since that's the least common superset of int and long long. The result is long long because of rule 3, so there's nothing else to do before the result is assigned to c.

Even so, the machine operation doesn't need all those extra zero bits spelled out for it. A 32*32->64 bit MUL instruction (or call to the equivalent runtime helper function) is exactly what's called for, and what every self-respecting compiler would generate.

Not actually, only "as-if".

Reply to
Hans-Bernhard Bröker

Yes that's right. A "single multiply" in my mind is 32*32->32, in the C world.

Other than that, I am in agreement.

Correct. The machine code has to have the right logic. Which exact instructions are used are not relevant to compliance.

Steve

Reply to
Steve Pope

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.