Re: (x * x) % y

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

Olivier Langlois (olanglois@sympatico.ca)
Fri, 23 Apr 1999 03:14:20 -0400


-----Original Message-----
From: Derek Atkins <warlord@MIT.EDU>
To: Olivier Langlois <olanglois@sympatico.ca>
Cc: CodherPlunks@toad.com <CodherPlunks@toad.com>
Date: 22 avril, 1999 20:07
Subject: Re: (x * x) % y

>This would be a compiler bug, not a 'C' bug (assuming it is
>a bug). What compiler are you using, and on what platform?

The code was generated by VC++ on Windows.
But I don't think it is a compiler bug. Like someone else said, 32 bits
multiplication produce a 32 bits product.

First I did a mistake. The correct assembler code would be:

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

The 1 operand version of the instruction mul multiply the operand (in that
case eax) with the register eax. My first version result was unpredictable
because eax wasn't initialized.

The idea of my post is that the x86 processor can expand a 32 bits
multiplication into a 64 bits value
that it stores in a pair of registers (EDX:EAX). and the instruction DIV
divide
this value. So the x86 architecture would allow you to perform (i*i)%j for
any value of i. But the compiler doesn't use this capability (or I don't
know how to write correctly the C statement). It XOR the
higher 32 bits in EDX and use a version of the MUL instruction (the 2
operands
version) that store the
product in a 32 bits register.

>GCC on Linux/x86 certainly gives different code.
>
>-derek
>--
> Derek Atkins, SB '93 MIT EE, SM '95 MIT Media Laboratory
> Member, MIT Student Information Processing Board (SIPB)
> URL: http://web.mit.edu/warlord/ PP-ASEL N1NWH
> warlord@MIT.EDU PGP key available
>


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