Re: 63/64 bit version of Delphi's function random( aRange : integer ) : integer; ?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


sci.crypt archive

Re: 63/64 bit version of Delphi's function random( aRange : integer ) : integer; ?

From: Skybuck Flying <spam@hotmail.com>
Date: Fri Apr 28 2006 - 08:21:29 CEST

Ok,

I am close to producing a working pure delphi code only version of the
random assembler function.

The only problem so far is that one instruction line is producing an integer
overflow.

So far I don't understand that.

I tried to fix it by simply using another variable but that doesn't seem to
fix it...

So now I must dig into understanding integer overfow problems related to
multiplications and variable size.

However I would like to share the code (routines and test code) up to this
point:

*** Begin of Code ***

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

// version 0.01, problem integer overflow.
function PureDelphiRandom1( ParaRange : integer ) : integer;
type
 TcombinedRegisters = packed record
 case integer of
  0 :
  (
   EAX : longword;
   EDX : longword;
  );
  1 :
  (
   Large : int64;
  );
 end;
var
 vCombinedRegisters : TcombinedRegisters;
begin

 with vCombinedRegisters do
 begin

  // EAX is probably initialized with the parameter of the function.
  EAX := ParaRange;

// PUSH EBX
  // not necessary

// XOR EBX, EBX
  // not necessary

// IMUL EDX,[EBX].RandSeed,08088405H
  EDX := RandSeed * $08088405;

// INC EDX
  EDX := EDX + 1;

// MOV [EBX].RandSeed,EDX
  RandSeed := EDX;

// MUL EDX
// EDX:EAX := EAX * EDX;

  // *** problem code, integer overflow ***
  Large := EAX * EDX;

// MOV EAX,EDX
  Result := EDX;

// POP EBX
  // not necessary
 end;
end;

// version 0.02, problem still integer overflow.
function PureDelphiRandom2( ParaRange : integer ) : integer;
type
 TcombinedRegisters = packed record
 case integer of
  0 :
  (
   EAX : longword;
   EDX : longword;
  );
  1 :
  (
   Large : int64;
  );
 end;
var
 vCombinedRegisters1 : TcombinedRegisters;
 vCombinedRegisters2 : TcombinedRegisters;
begin

 with vCombinedRegisters1 do
 begin

  // EAX is probably initialized with the parameter of the function.
  EAX := ParaRange;

// PUSH EBX
  // not necessary

// XOR EBX, EBX
  // not necessary

// IMUL EDX,[EBX].RandSeed,08088405H
  EDX := RandSeed * $08088405;

// INC EDX
  EDX := EDX + 1;

// MOV [EBX].RandSeed,EDX
  RandSeed := EDX;

// MUL EDX
// EDX:EAX := EAX * EDX;

  // *** problem code, still integer overflow ***
  // problem seems to be with multiplication itself
  vCombinedRegisters2.Large := EAX * EDX;

// MOV EAX,EDX
  Result := vCombinedRegisters2.EDX;

// POP EBX
  // not necessary
 end;
end;

var
 mTransferSize : int64;
 mOffset : int64;
 CopyOfRandSeed : longint;
 I : integer;
begin
 mTransferSize := 1024*1024*10; // 10 MB ;) well in range of 32 bits.

 CopyOfRandSeed := 0;

 for I := 1 to 10 do
 begin
  CopyOfRandSeed := RandSeed; // store original randseed
  mOffset := Random( mTransferSize );
  writeln( 'Original Random: ', mOffset );

  // call to flawed version
{
  RandSeed := CopyOfRandSeed; // restore original rand seed
  mOffset := PureDelphiRandom1( mTransferSize );
  writeln( 'Pure Delphi Random: ', mOffset );
}

  // call to still flawed version
  RandSeed := CopyOfRandSeed; // restore original rand seed
  mOffset := PureDelphiRandom2( mTransferSize );
  writeln( 'Pure Delphi Random: ', mOffset );

 end;

 writeln( 'press enter to continue' );
 readln;
end.

*** End of Code ***

How to fix the problem ? :)

Bye,
  Skybuck.
Received on Mon May 1 02:05:28 2006