This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Long long usage question
- From: Tim Prince <tprince at computer dot org>
- To: Claus Fischer <claus dot fischer at clausfischer dot com>, gcc at gcc dot gnu dot org
- Date: Sat, 16 Mar 2002 23:55:43 -0800
- Subject: Re: Long long usage question
- References: <20020317001910.A8902@clausfischer.com>
- Reply-to: tprince at computer dot org
On Saturday 16 March 2002 15:19, Claus Fischer wrote:
> I need to write a function that takes four signed 32 bit values,
> builds two products, and compares the size, thusly:
>
> 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.
>
> Questions:
>
> 1. Does the compiler know that the cast results have their highest
> 32 bits zeroed?
If you want that, rather than sign extension, declare unsigned long int
a,b,c,d
>Does it pick a fast instruction for that case?
If you mean, does it take advantage of the fact that the high 32 bits of the
operands are all coming from sign extension, or all 0 if you change to
unsigned, probably not. Easily checked for your favorite targets.
>
> 2. If the answer is 'no', does it help to use
>
> long long p1 = (long long) a * b;
>
That 's a different result, except where a*b < LONG_MAX, and it could easily
be faster to specify that only long int results are wanted this way. But
what's the point? You get garbage when the result doesn't fit long int.
> 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?
asm()
--
Tim Prince