Re: Upper 32 bits (off topic)

Search this archive.

From: Sergio Masci (sergio@titan.demon.co.uk)
Date: Fri 10 Mar 2000 - 18:36:05 IST


> 
> 
> This is somewhat off-topic, but I figure if anyone can answer it, you guys
> can. Thanks.
> 
> ==========
> 
> Ok, you smart guys, how do I get the upper 32 bits of a number when using
> a 32-bit compiler? I.e., >> 32 would work on a 64-bit system, but you
> can't do that on an x86, right?
> 
> Here's an example using a "Watcom" compiler:
> 
> /*------------------------------------------------------------------------*
>   BIGMUL -> 32bit * 32bit = 64bit. We need the upper 32 bit.
>             On a 64-bit compiler, it would be (num1 * num2) >> 32;
>  *------------------------------------------------------------------------*/
> int BIGMUL(int a, int b);
> #pragma aux BIGMUL = \
>   "imul  edx"        \
>   parm [eax] [edx]   \
>   value [edx];
> 
> 
> How I rewrite this for gcc?
> 
> Thanks!
> 
> -Jay
> 

The simplest way is to use the 'long long' data type. This provides 64 bit
ints on 32 bit machines.

e.g.

long BIGMUL(int a, int b)
{
	long long xa, xb;
	long c;

	xa = a;
	xb = b;

	c = (xa * xb) >> 32

	return c;
}

you could of course do it like this:

long BIGMUL(int a, int b)
{
	return (long)(((long long)a * b) >> 32);
}

When I last looked, this was no where near as efficent as in line assembler
but if you're only using this occationally that wont make any difference.

Regards
Sergio


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:23 IST