This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Long long usage question


> int prodcomp(long a, long b, long c, long d)
> {
>     long long p1 = (long long) a * (long long) b;
>     long long p2 = (long long) c * (long long) d;
>     return p1 < p2;
> }
> 
> I'm assuming that most 32 bit CPU's have an instruction for
> multiplying two 32 bit values.

Good assumption.

> Questions:
> 
> 1. Does the compiler know that the cast results have their highest
>    32 bits zeroed? Does it pick a fast instruction for that case?

Not zeroed, but sign extended.  Yes it picks fast instructions.

> 2. If the answer is 'no', does it help to use
> 
>     long long p1 = (long long) a * b;

This is exactly the same.  b will be explicitly cast to long long.

> 3. Is there a way of telling the compiler what I really want:
>     long long p1 = a * b;
>    but the multiplication performed in the long long realm?

Yes; both your preceding examples do just that.

> 4. Is there documentation covering how to best do such arithmetic
>    in GNU C on glibc? I'm speaking of 32 bit arithmetics with
>    occasional products and divisions leading in the 64 bit domain.

(long long)a / (long) b will compute a long long result, even when
the compiler could potentially see that the result will fit into
a plain long.


Segher


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]