Re: (x * x) % y

New Message Reply About this list Date view Thread view Subject view Author view

Eric Young (eay@uq.net.au)
Fri, 23 Apr 1999 08:33:59 +1000 (GMT+1000)


On Thu, 22 Apr 1999, Olivier Langlois wrote:
> unsigned test( unsigned i, unsigned j )
> {
> return (i*i)%j;
> }
> my compiler generate the following assembler code
...
> The problem is that an overflow can occur during the multiplication
> while with this code
...
> My question is : Is it possible to write a legal C construction to would
> generate the correct assembler code ?

The thing is that the correct asm is being generated according to the C
syntax.
 32bit * 32bit -> 32bit
You need to hope that you C compiler supports the long long data type
(_int64 for Visual C) and do some casting.

        return(((unsigned long long)i*i)%j);

should give the correct result.

eric


New Message Reply About this list Date view Thread view Subject view Author view

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

The following archive was created by hippie-mail 7.98617-22 on Thu May 27 1999 - 23:44:22