unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Mikael Djurfeldt <djurfeldt@nada.kth.se>
Cc: guile-devel@gnu.org
Subject: Re: Nearly finished (re)integrating GMP for bignums.
Date: Mon, 03 Mar 2003 14:13:06 +0100	[thread overview]
Message-ID: <xy74r6k3865.fsf@nada.kth.se> (raw)
In-Reply-To: <87vfz6nw8i.fsf@raven.i.defaultvalue.org> (Rob Browning's message of "Wed, 26 Feb 2003 23:11:41 -0600")

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


  reply	other threads:[~2003-03-03 13:13 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-12 16:07 Nearly finished (re)integrating GMP for bignums Rob Browning
2003-02-12 16:26 ` Marius Vollmer
2003-02-12 17:32   ` Rob Browning
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
2003-03-03 13:13       ` Mikael Djurfeldt [this message]
2003-03-03 13:21         ` Mikael Djurfeldt
2003-03-06 17:31         ` Rob Browning
2003-03-06 18:13           ` Mikael Djurfeldt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xy74r6k3865.fsf@nada.kth.se \
    --to=djurfeldt@nada.kth.se \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).