in reply to Re: [OT] Endianness and extended precision (80-bit) long doubles
in thread [OT] Endianness and extended precision (80-bit) long doubles

It would be helpful to show some code

Yes, it might have been clearer if I'd phrased the question in terms of pack/unpack.
On my 64-bit little-endian machine (Ubuntu-18.04), running a perl whose NV is the extended precision (80-bit) long double, I get:
$ perl -le 'print unpack "h*", pack "D", sqrt 2;' 4846ed9f333f405bfff3000000000000
If I ran the identical one-liner on a 64-bit *big-endian* machine,running a perl whose NV is the extended precision (80-bit) long double, where would that substring of 12 consecutive zeroes appear ? Would it still appear at the end of the string (as was the case on my little-endian machine), or would it appear at the beginning ?

I have long-standing C code in Math-MPFR (MPFR.xs) that presumes that the substring in question will appear at the end of the string, irrespective of endianess:
.... n = bits == 64 ? 10 : 16; #ifdef MPFR_HAVE_BENDIAN /* Big Endian architecture */ for (i = 0; i < n; i++) { #else for (i = n - 1; i >= 0; i--) { #endif // code that does something with byte i
but I don't recall having ever established the correctness of that code for the big-endian case.

If it were incorrect, then the test suite should expose the error - but then I don't know if Math::MPFR has ever been built on a big-endian machine, using a perl whose NV is the 80-bit long double.

Cheers,
Rob

Replies are listed 'Best First'.
Re^3: [OT] Endianness and extended precision (80-bit) long doubles
by Marshall (Canon) on Aug 25, 2019 at 01:38 UTC
    I am not sure that I can help. current Pack docs. I am on Perl 5.24 and it appears to me that you are using a feature from Perl 5.30? This "D" option to pack() appears in red in my browser.

    If Perl source code of:

    $ perl -le 'print unpack "h*", pack "D", sqrt 2;' 4846ed9f333f405bfff3000000000000
    works? I am also unsure about what will happen on a different architecture. My main machine is Intel (little endian). I have a Raspberry PI which I think is big endian, but I have a lot of hardware cables to screw around with to get it working. Sorry.
    D A float of long-double precision in native format. (Long doubles are available only if your system supports long double values _and_ if Perl has been compiled to support those. Raises an exception otherwise. Note that there are different long double formats.) h A hex string (low nybble first). H A hex string (high nybble first).

      Quoth  perldoc -f pack on my 5.8.9 Perl:

      D   A long double-precision float in the native format.
          (Long doubles are available only if your system supports long
          double values _and_ if Perl has been compiled to support those.
          Causes a fatal error otherwise.)
      Also see pack. I'm not sure about the red coloration; maybe it flags a feature not supported by all word widths/compilation options. (Actually, I suspect a documentation problem since everything in the table (update: including things like  x X @ that are as old as the hills) from line 41 on is both red and italic.)


      Give a man a fish:  <%-{-{-{-<

        Long doubles are available only if your system supports long double values _and_ if Perl has been compiled to support those.

        Yes - I ran the one liner on a build of perl-5.30.0 whose NV was long double.
        Not sure how far back the "D" option goes, but it's available in 5.8.8 onwards, at least.

        With recent perls we can ask for either little endian or big endian outputs - eg (on Windows):
        C:\_32>perl -le "print unpack 'h*', pack 'D<', sqrt 2;" 4846ed9f333f405bfff3000000000000 C:\_32>perl -le "print unpack 'h*', pack 'D>', sqrt 2;" 000000000000f3ff5b403f339fed4648
        However, I think the "big endian" output given here is simply the reverse of what was found on my machine.
        I don't think there's any basis to assume that it displays what's stored on an actual big endian machine.

        Cheers,
        Rob
        Yes, this red color gave me pause. Since this D feature appears in 5.8.9, I have no idea what the difference in text color means. Maybe if nobody knows what it means, the red color should be removed as a doc fix?

        oh...I see .. perhaps the red means that Perl has to be compiled in a certain way for the feature to "work"? Right now I'm using Perl 25.24 compiled for 64 bit. I suspect that means that long double is available? Almost all of my work is with integers and text. as another "oh...", I remember some benchmark testing that a Phd student did for me from ASM class on float performance with modern Intel processors. There are more transistors in the math unit than in the main CPU. The result was that float operations are about the same as integer ops which surprised me.