* Re: Nearly finished (re)integrating GMP for bignums.
2003-02-12 17:34 ` Mikael Djurfeldt
@ 2003-02-12 18:57 ` Rob Browning
2003-02-12 21:13 ` Rob Browning
` (2 subsequent siblings)
3 siblings, 0 replies; 12+ messages in thread
From: Rob Browning @ 2003-02-12 18:57 UTC (permalink / raw)
Cc: Marius Vollmer
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
> 2. What do you mean by a "good" RNG? I'd say it is good if it is
> fast. Our generator is fast.
I wonder how fast the GMP RNGs are (I currently have no idea).
> Ideally, the bignum code would use the pluggable source of random
> bits in random.c and the GMP generators would be provided as
> optional plugins for random.c. Rob, is this possible?
I'm not sure yet -- what I was considering was if (in the short term)
I can just replace the one BIGNUM based chunk of code in there with
calls to manipulate GMPs (or just calls to scm_i_big* functions). At
the very least, the code has to be changed so that it doesn't try to
manipulate BIGDIG stuff directly anymore since all the implementation
specific stuff is gone.
--
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-02-12 17:34 ` Mikael Djurfeldt
2003-02-12 18:57 ` Rob Browning
@ 2003-02-12 21:13 ` Rob Browning
2003-02-12 23:00 ` Kevin Ryde
2003-02-27 5:11 ` Rob Browning
3 siblings, 0 replies; 12+ messages in thread
From: Rob Browning @ 2003-02-12 21:13 UTC (permalink / raw)
Cc: Marius Vollmer
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
> Ideally, the bignum code would use the pluggable source of random bits
> in random.c and the GMP generators would be provided as optional
> plugins for random.c. Rob, is this possible?
The main issue I have now is that in scm_c_random_bignum, the code
directly manipulates the old bignum internals. So I'll need to
convert that to GMP. i.e.:
LONG32 *bits, mask, w;
nd = SCM_NUMDIGS (m);
/* calculate mask for most significant digit */
#if SIZEOF_INT == 4
/* 16 bit digits */
if (nd & 1)
{
/* fix most significant 16 bits */
unsigned short s = SCM_BDIGITS (m)[nd - 1];
To be sure have the details right -- SCM_NUMDIGS gives the number of
elements in SCM_BDIGITS, where the last element is the most
significant digit right?
I'll need to figure out how this should map onto the available GMP
operations. Right now I don't expose much from numbers.c
"bignum-wise", but I'm thinking of adding some _i_ functions that map
onto gmp operations, and would probably also be appropriate for any
other bignum implementation, i.e.:
void scm_i_bigadd_ui (SCM dest, SCM src, unsigned long n);
void scm_i_bigsub_ui (SCM dest, SCM src, unsigned long n);
void scm_i_bigneg (SCM dest);
etc. These prototypes follow the GMP calling convention which can be
convenient since it allows you to express both "in-place"
modifications where dest == src, or copy operations where dest != src.
The ability to do "in-place" operations can be important when dealing
with large bignums because you don't necessarily want to have to
allocate a new bignum for each operation.
Alternately, I could just support in-place operations:
void scm_i_bigadd_ui (SCM big, unsigned long n);
or I could only support our more traditional "always copy on write"
style operations:
SCM scm_i_bigadd_ui (SCM big, unsigned long n);
The whole point of the scm_i_big* functions would be for use by other
parts of guile where the code needs to operate directly on bignums.
The alternative is to just expect the other code use SCM_I_BIG_MPZ(x),
to get hold of the mpz_t value and then manipulate that directly. The
direct approach could be faster, and gives more flexibility, but of
course it's less portable (presuming we ever change our bignum
implementation again).
In truth, ATM we don't have that much code outside numbers.c that
manipulates bignums directly. Aside from the GC and eq, it's mostly
random.c and socket.c so far. So it may not be a big deal either way.
--
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-02-12 17:34 ` Mikael Djurfeldt
2003-02-12 18:57 ` Rob Browning
2003-02-12 21:13 ` Rob Browning
@ 2003-02-12 23:00 ` Kevin Ryde
2003-02-27 5:11 ` Rob Browning
3 siblings, 0 replies; 12+ messages in thread
From: Kevin Ryde @ 2003-02-12 23:00 UTC (permalink / raw)
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
>
> 2. What do you mean by a "good" RNG? I'd say it is good if it is
> fast.
In gmp we're looking at a Blum-Blum-Shub algorithm for possible
inclusion, it's based on cryptographic principles, and so supposedly
suits such applications (which are rather specialized of course).
> Our generator is fast. Can you name any single other
> criterion which is not "religious" and which our generator doesn't
> fulfil? (I'm talking about something measurable here.)
For reference, the next gmp will be have a Mersenne Twister generator
contributed by Pedro Gimeno, and it'll be the default too
(gmp_randinit_default). It's fast, and very random according to those
who pay attention to such things (I'm no expert).
Rob Browning <rlb@defaultvalue.org> writes:
>
> I wonder how fast the GMP RNGs are (I currently have no idea).
The current linear congruential generator isn't as good as it could
be, it uses bignum operations even for the common case of a modulus
that fits in one or two machine words.
Mersenne Twister ought to end up being as fast or faster than L-C, and
the lack of randomness of L-C is well-known, so there oughtn't be any
direct call for the latter any more, not except maybe for special
purposes like experimenting with very un-random sequences.
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-02-12 17:34 ` Mikael Djurfeldt
` (2 preceding siblings ...)
2003-02-12 23:00 ` Kevin Ryde
@ 2003-02-27 5:11 ` Rob Browning
2003-03-03 13:13 ` Mikael Djurfeldt
3 siblings, 1 reply; 12+ messages in thread
From: Rob Browning @ 2003-02-27 5:11 UTC (permalink / raw)
Cc: Marius Vollmer
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
> Ideally, the bignum code would use the pluggable source of random bits
> in random.c and the GMP generators would be provided as optional
> plugins for random.c. Rob, is this possible?
For now, I think I'd like to consider just translating the existing
scm_c_random_bignum to manipulate mpz_t values rather than the old
bignum digits.
If you (or anyone else) is familiar with this function, could you
explain the critical parts? I can probably figure out how to
translate it eventually by just looking at the code, but that process
would probably go a lot faster if I had an overview.
I'd like to know things like how critical it is that it operate on
16-bit chunks -- what interdependencies does that create, etc. If
possible, it'll probably be faster and easier to work with larger
chunks when manipulating the mpz_t's.
Thanks
--
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-02-27 5:11 ` Rob Browning
@ 2003-03-03 13:13 ` Mikael Djurfeldt
2003-03-03 13:21 ` Mikael Djurfeldt
2003-03-06 17:31 ` Rob Browning
0 siblings, 2 replies; 12+ messages in thread
From: Mikael Djurfeldt @ 2003-03-03 13:13 UTC (permalink / raw)
Cc: guile-devel
Rob Browning <rlb@defaultvalue.org> writes:
> Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
>
>> Ideally, the bignum code would use the pluggable source of random bits
>> in random.c and the GMP generators would be provided as optional
>> plugins for random.c. Rob, is this possible?
>
> For now, I think I'd like to consider just translating the existing
> scm_c_random_bignum to manipulate mpz_t values rather than the old
> bignum digits.
>
> If you (or anyone else) is familiar with this function, could you
> explain the critical parts? I can probably figure out how to
> translate it eventually by just looking at the code, but that process
> would probably go a lot faster if I had an overview.
Sorry for not answering earlier. I've been away on vacation.
The logic is very simple:
SCM scm_c_random_bignum (scm_t_rstate *state, SCM m)
Takes a random state (source of random bits) and a bignum m.
Returns a bignum b, 0 <= b < m.
It does this by allocating a bignum b with as many base 65536 digits
as m, filling b with random bits (in 32 bit chunks) up to the most
significant 1 in m, and, finally checking if the resultant b is too
large (>= m). If too large, we simply repeat the process again. (It
is important to throw away all generated random bits if b >= m,
otherwise we'll end up with a distorted distribution.)
Here's the step-by-step description:
1. Compute a 32-bit bitmask for use when filling bits into the most
significant long word in b's bit space. A combination of
conditionals and an 8-bit lookup table (scm_masktab) is used.
2. Allocate b and assign the bit space address to the variable "bits".
3.1 Generate the most significant bits using the mask.
3.2 Fill up the rest of the bit space.
3.3 Use scm_i_normbig to cut away leading zeros and/or convert to inum.
3.4 If b >= m, go to 3.1.
4. return b
> I'd like to know things like how critical it is that it operate on
> 16-bit chunks -- what interdependencies does that create, etc.
No dependencies, except that you'd obviously have to adjust the
conditionals for generating the mask.
> If possible, it'll probably be faster and easier to work with larger
> chunks when manipulating the mpz_t's.
I suggest you work with 32-bit chunks since this is what is returned
by scm_the_rng.random_bits (state).
Best regards,
Mikael
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-03-03 13:13 ` Mikael Djurfeldt
@ 2003-03-03 13:21 ` Mikael Djurfeldt
2003-03-06 17:31 ` Rob Browning
1 sibling, 0 replies; 12+ messages in thread
From: Mikael Djurfeldt @ 2003-03-03 13:21 UTC (permalink / raw)
Cc: guile-devel
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
> The logic is very simple:
I've now inserted this description into proper places in random.c and
comitted it to CVS.
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-03-03 13:13 ` Mikael Djurfeldt
2003-03-03 13:21 ` Mikael Djurfeldt
@ 2003-03-06 17:31 ` Rob Browning
2003-03-06 18:13 ` Mikael Djurfeldt
1 sibling, 1 reply; 12+ messages in thread
From: Rob Browning @ 2003-03-06 17:31 UTC (permalink / raw)
Cc: Marius Vollmer
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
> It does this by allocating a bignum b with as many base 65536 digits
> as m, filling b with random bits (in 32 bit chunks) up to the most
> significant 1 in m, and, finally checking if the resultant b is too
> large (>= m). If too large, we simply repeat the process again.
> (It is important to throw away all generated random bits if b >= m,
> otherwise we'll end up with a distorted distribution.)
It looks like the old code handled 16-bit chunks at a time. I just
wanted to make sure it was OK to go ahead and use the full "unsigned
long" random_bits range per-chunk instead if that works out better.
Thanks
--
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Nearly finished (re)integrating GMP for bignums.
2003-03-06 17:31 ` Rob Browning
@ 2003-03-06 18:13 ` Mikael Djurfeldt
0 siblings, 0 replies; 12+ messages in thread
From: Mikael Djurfeldt @ 2003-03-06 18:13 UTC (permalink / raw)
Cc: guile-devel
Rob Browning <rlb@defaultvalue.org> writes:
> Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
>
>> It does this by allocating a bignum b with as many base 65536 digits
>> as m, filling b with random bits (in 32 bit chunks) up to the most
>> significant 1 in m, and, finally checking if the resultant b is too
>> large (>= m). If too large, we simply repeat the process again.
>> (It is important to throw away all generated random bits if b >= m,
>> otherwise we'll end up with a distorted distribution.)
>
> It looks like the old code handled 16-bit chunks at a time. I just
> wanted to make sure it was OK to go ahead and use the full "unsigned
> long" random_bits range per-chunk instead if that works out better.
No, in fact, the old code also used all 32 random bits a long word at
a time. And, yes, that should be OK with our RNG.
M
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] 12+ messages in thread