(x * x) % y

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

Olivier Langlois (olanglois@sympatico.ca)
Thu, 22 Apr 1999 17:52:37 -0400


Hi,

For the following function

unsigned test( unsigned i, unsigned j )
{
 return (i*i)%j;
}

my compiler generate the following assembler code

 mov eax, DWORD PTR _i$[esp-4]
 xor edx, edx
 imul eax, eax
 div DWORD PTR _j$[esp-4]
 mov eax, edx

The problem is that an overflow can occur during the multiplication while
with this code

 mov edx, DWORD PTR _i$[esp-4]
 mul edx
 div DWORD PTR _j$[esp-4]
 mov eax, edx

The function would return a valid value for any value of i.

My question is : Is it possible to write a legal C construction to would
generate the correct assembler code ?

Olivier Langlois - olanglois@sympatico.ca -
http://www3.sympatico.ca/olanglois
Electrical Engineering Student
École de Technologie Supérieure - http://www.etsmtl.ca
Montreal, Canada


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