unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Arne Babenhauserheide <arne_bab@web.de>
To: guile-devel@gnu.org
Subject: Re: Immediate doubles (up to 2^256) and rationals coming to Guile 3
Date: Sat, 08 Jun 2019 10:07:45 +0200	[thread overview]
Message-ID: <87blz8wlda.fsf@web.de> (raw)
In-Reply-To: <877e9wc210.fsf@netris.org>

[-- Attachment #1: Type: text/plain, Size: 1960 bytes --]


Mark H Weaver <mhw@netris.org> writes:

> Mark H Weaver <mhw@netris.org> writes:
>
>> Here's the format of fixrats on 64-bit systems:
> This allows for an elegant packing operation:
>
>   if (SCM_I_INUMP (numerator) && SCM_I_INUMP (denominator))
>     {
>       union { double f; uint64_t u; } u;
>       u.f  = SCM_I_INUM (numerator);
>       u.u += SCM_I_INUM (denominator);

Wow, I didn’t know that you could do that.

However: "The details of that allocation are implementation-defined, and
it's undefined behavior to read from the member of the union that wasn't
most recently written." https://en.cppreference.com/w/cpp/language/union

Can you guarantee that this works?

>       if ((scm_t_inum) u.f == SCM_I_INUM (numerator))
>         {
>           u.u += 0xc010000000000000;
>           u.u = (u.u << 6) | (u.u >> 58);
>           if ((u.u & 0x1f) == 0)
>             return SCM_PACK (u.u | scm_fixrat_tag);
>         }
>     }
>
> We start by converting the numerator into an IEEE double.  We then use
> unsigned integer addition to add the denominator to the 64-bit
> representation of that double.  We now check that this new IEEE double
> has integer part equal to the numerator.  If it doesn't, this indicates
> either that the numerator is too large to be represented exactly, or
> that the denominator is too large to fit in the remaining bits.
>
> The only thing that remains to be done at this point is to rotate left 6
> bits, so that the 5 highest exponent bits become the lowest 5 bits,
> where the fixrat tag will go, and to add a value which adjusts the IEEE
> biased exponent field to be 0 when the numerator is 1 or -1.

It is really cool to read these deep details — is there a chance that
when this lands you could re-vamp the emails you wrote here into a
blog-post we can easily link to?

Best wishes,
Arne
--
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1076 bytes --]

  reply	other threads:[~2019-06-08  8:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-06  9:40 Immediate doubles (up to 2^256) and rationals coming to Guile 3 Mark H Weaver
2019-06-06 12:56 ` Mark H Weaver
2019-06-06 19:37 ` Arne Babenhauserheide
2019-06-12  0:03   ` Mark H Weaver
2019-06-06 20:09 ` tomas
2019-06-07 19:46 ` Ludovic Courtès
2019-06-09 16:56   ` Mark H Weaver
2019-06-09 17:30     ` Mark H Weaver
2019-06-11  8:39     ` Ludovic Courtès
2019-06-11 10:58       ` Mark H Weaver
2019-06-11 12:21         ` Ludovic Courtès
2019-06-11 11:34       ` Mark H Weaver
2019-06-08  1:13 ` Mark H Weaver
2019-06-08  8:07   ` Arne Babenhauserheide [this message]
2019-06-08  9:08     ` Chris Vine
2019-06-08  9:46       ` Arne Babenhauserheide
2019-06-08 10:24         ` Chris Vine
2019-06-08 13:12       ` Hans Åberg

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=87blz8wlda.fsf@web.de \
    --to=arne_bab@web.de \
    --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).