Re: Immediate doubles (up to 2^256) and rationals coming to Guile 3

2019-06-08 Thread Hans Åberg


> On 8 Jun 2019, at 11:08, Chris Vine  wrote:
> 
> On Sat, 08 Jun 2019 10:07:45 +0200
> Arne Babenhauserheide  wrote:
> [snip]
>> 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?
> 
> This is C and not C++ and the provision to which you refer does not
> apply.
> 
> Reading from a member of a union other than the one last written to is
> implementation defined in C89/90, and defined in C99 (with Technical
> Corrigendum 3) and above, although it might include a trap
> representation (but wouldn't on any platform supported by guile).  You
> might want to see in particular footnote 95 of C11 (which isn't
> normative but is intended to explain the provisions of §6.2.6.1 which
> are).
> 
> gcc and clang have always supported type punning through unions.

FYI, the site mentioned above also covers C (there is a link the bottom of the 
C++ page above):
  https://en.cppreference.com/w/c/language/union





Re: Immediate doubles (up to 2^256) and rationals coming to Guile 3

2019-06-08 Thread Chris Vine
On Sat, 08 Jun 2019 11:46:10 +0200
Arne Babenhauserheide  wrote:
> Chris Vine  writes:
> > On Sat, 08 Jun 2019 10:07:45 +0200
> > Arne Babenhauserheide  wrote:
> > [snip]
> >> 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?
> >
> > This is C and not C++ and the provision to which you refer does not
> > apply.
> >
> > Reading from a member of a union other than the one last written to is
> > implementation defined in C89/90, and defined in C99 (with Technical
> > Corrigendum 3) and above
> 
> Thank you for the correction and explanation!

You have a good point though if visible type transformations were to
appear in a header rather than a *.c file, because guile headers are
(at the moment) intended to be in the common subset of C and C++ so that
libguile.h can be included in a C++ programme.

Having said that, gcc and clang support type punning through unions in
C++ as well as C.  I don't know if guile is supposed to compile with
other compilers nowadays: but frankly it would be perverse for some
other compiler which supports both C and C++ to invoke different
behaviour for unions in such cases.

Chris



Re: Immediate doubles (up to 2^256) and rationals coming to Guile 3

2019-06-08 Thread Arne Babenhauserheide

Chris Vine  writes:

> On Sat, 08 Jun 2019 10:07:45 +0200
> Arne Babenhauserheide  wrote:
> [snip]
>> 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?
>
> This is C and not C++ and the provision to which you refer does not
> apply.
>
> Reading from a member of a union other than the one last written to is
> implementation defined in C89/90, and defined in C99 (with Technical
> Corrigendum 3) and above

Thank you for the correction and explanation!

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


signature.asc
Description: PGP signature


Re: Immediate doubles (up to 2^256) and rationals coming to Guile 3

2019-06-08 Thread Chris Vine
On Sat, 08 Jun 2019 10:07:45 +0200
Arne Babenhauserheide  wrote:
[snip]
> 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?

This is C and not C++ and the provision to which you refer does not
apply.

Reading from a member of a union other than the one last written to is
implementation defined in C89/90, and defined in C99 (with Technical
Corrigendum 3) and above, although it might include a trap
representation (but wouldn't on any platform supported by guile).  You
might want to see in particular footnote 95 of C11 (which isn't
normative but is intended to explain the provisions of §6.2.6.1 which
are).

gcc and clang have always supported type punning through unions.

Chris



Re: Immediate doubles (up to 2^256) and rationals coming to Guile 3

2019-06-08 Thread Arne Babenhauserheide

Mark H Weaver  writes:

> Mark H Weaver  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 += 0xc010;
>   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


signature.asc
Description: PGP signature