This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Long long usage question
- From: Segher Boessenkool <segher at chello dot nl>
- To: Claus Fischer <claus dot fischer at clausfischer dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Sun, 17 Mar 2002 14:24:15 +0100
- Subject: Re: Long long usage question
- References: <20020317001910.A8902@clausfischer.com>
- Reply-to: segher at chello dot nl
> 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