delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/17/14:40:29

From: Ben Pfaff <pfaffben AT pilot DOT msu DOT edu>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: An inline assembler RNG for C
Date: 17 Feb 1999 14:36:59 -0500
Organization: Michigan State University
Lines: 31
Sender: blp AT pfaffben DOT user DOT msu DOT edu
Message-ID: <87btisx1no.fsf@pfaffben.user.msu.edu>
References: <36CB0BF1 DOT 23FF9E2 AT stat DOT fsu DOT edu>
NNTP-Posting-Host: pfaffben.user.msu.edu
X-AUTHid: pfaff
X-Newsreader: Gnus v5.5/Emacs 20.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

George Marsaglia <geo AT stat DOT fsu DOT edu> writes:

   The method is Multiply-with-Carry for 32-bit integers.
   This is the idea.  Suppose you have
	     a current 32-bit integer x
	     a current 32-bit carry   c
	     a fixed multiplier, say, a=2083801278
   Now form  a*x+c in 64 bits.
   Return the bottom 32 bits as the new x,
   and keep the top 32 bits as the new carry, c.

If you allow use of long long, which IIRC will be in C9x, then you can
implement this cleanly in C:

	const unsigned long long a = 2083801278ull;
	unsigned long long x, c;

	[...]
	x = a * x + c;
	c = x >> 32;
	x &= 0xffffffffull;
	c &= 0xffffffffull;

(You only need the final AND operation if you're worried about the
target machine having an unsigned long long that's bigger than 64
bits.)
-- 
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms
Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019