Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Thomas Koenig via Fortran



On 07.01.22 22:48, Jakub Jelinek wrote:

On Fri, Jan 07, 2022 at 10:40:50PM +0100, Thomas Koenig wrote:

One thing that one has to watch out for is a big-endian IBM long double
file, so the byte swapping will have to be done before assigning
the value.


I've tried to handle that right, i.e. on unformatted read with
byte-swapping and r16 <-> r17 conversions first do byte-swapping
and then r16 <-> r17 conversions, while for unformatted writes
first r16 <-> r17 conversions and then byte-swapping.


I have tried to unravel the different cases here, I count six
(lumping together the environment variables, the CONVERT specifier
and -fconvert, and leaving out the byte swapping)

CompilerConvert   Read action Write action

IEEENone  NoneNone
IEEEIEEE  NoneNone
IEEEIBM   IBM->IEEE   IEEE->IBM

IBM None  NoneNone
IBM IEEE  IEEE->IBM   IBM->IEEE
IBM IBM   NoneNone

From this table, it is clear that the compiler has to inform
the library about the option it is using, I think it is best
encoded in the number passed to _gfortran_set_convert.

Old programs should continue to run with the new library, so
the absence of a call to _gfortran_set_convert, or a call
which sets byte swapping, should have the old meaning, i.e
IBM long double. A program which uses IEEE long double should
then call _gfortran_set_convert with a suitable argument to
let the library know what to do, just in case.

I think this is what I will start working on.

Best regards

Thomas


Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Jakub Jelinek via Fortran
On Sat, Jan 08, 2022 at 11:07:24AM +0100, Thomas Koenig wrote:
> I have tried to unravel the different cases here, I count six
> (lumping together the environment variables, the CONVERT specifier
> and -fconvert, and leaving out the byte swapping)
> 
> CompilerConvert   Read action Write action
> 
> IEEENone  NoneNone
> IEEEIEEE  NoneNone
> IEEEIBM   IBM->IEEE   IEEE->IBM
> 
> IBM None  NoneNone
> IBM IEEE  IEEE->IBM   IBM->IEEE
> IBM IBM   NoneNone
> 
> From this table, it is clear that the compiler has to inform
> the library about the option it is using, I think it is best
> encoded in the number passed to _gfortran_set_convert.

Whether the compiler is using IEEE or IBM real(kind=16) or
complex(kind=16) for a particular spot (which doesn't have to be
the same in the whole program) is known to the library by the
kind argument it provides to the I/O routines, if it is kind=16,
it is IBM, if it is kind=17, it is IEEE.
See the patch I've posted, which does one thing when the runtime
kind (i.e. abi_kind on the compiler side) is 17 and convert
says r16_ibm, and another thing when runtime kind is 16 and
convert says r16_ieee.  Other cases shouldn't need conversion.
And IMHO the default like for byte-swapping should be the native
format, i.e. the one the program actually used.
The only thing that should be encoded in _gfortran_set_convert
is -fconvertWHATEVER command line option IMO.

Jakub



Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Jakub Jelinek via Fortran
On Sat, Jan 08, 2022 at 12:00:38PM +0100, Jakub Jelinek via Gcc-patches wrote:
> And IMHO the default like for byte-swapping should be the native
> format, i.e. the one the program actually used.

One reason for that is that neither conversion is lossless, neither format
is a subset or superset of the other.  Yes, IEEE quad has both much bigger
exponent range (-16382..16383 vs. -1022..1023) and slightly bigger fixed
precision (113 vs. 106 bits).
But IBM extended has that weirdo numerically awful flexible precision where
certain numbers can have much bigger precision than those 106 bits, up to
2048+52 or so.  So there is rounding in both directions.
So, after distros switch to -mabi=ieeelongdouble by default or when people
use -mabi=ieeelongdouble on their programs, they'd better store that format
into data files by default, without the need of some magic CONVERT= options,
env vars or command line options.  Only in the case where they need to
interact with -mabi=ibmlongdouble environments, they need to take some
action.

Jakub



Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Jakub Jelinek via Fortran
On Sat, Jan 08, 2022 at 12:10:56PM +0100, Jakub Jelinek via Gcc-patches wrote:
> One reason for that is that neither conversion is lossless, neither format
> is a subset or superset of the other.  Yes, IEEE quad has both much bigger
> exponent range (-16382..16383 vs. -1022..1023) and slightly bigger fixed
> precision (113 vs. 106 bits).
> But IBM extended has that weirdo numerically awful flexible precision where
> certain numbers can have much bigger precision than those 106 bits, up to
> 2048+52 or so.  So there is rounding in both directions.
> So, after distros switch to -mabi=ieeelongdouble by default or when people
> use -mabi=ieeelongdouble on their programs, they'd better store that format
> into data files by default, without the need of some magic CONVERT= options,
> env vars or command line options.  Only in the case where they need to
> interact with -mabi=ibmlongdouble environments, they need to take some
> action.

Note, as for byteswapping, apparently it wasn't ever working right fox
the IBM extended real(kind=16) and complex(kind=16).
Because unlike IEEE extended or integral types, it seems powerpc*-*-*
doesn't actually fully byteswap those between little and big endian.
Proof:
long double a = 
0.L;
compiled little endian IBM long double:
.size   a, 16
a:
.long   1431655765
.long   1070945621
.long   1431655766
.long   1014322517
compiled big endian IBM long double:
.size   a, 16
a:
.long   1070945621
.long   1431655765
.long   1014322517
.long   1431655766
compiled little endian IEEE long double:
.size   a, 16
a:
.long   1431655765
.long   1431655765
.long   1431655765
.long   1073567061
compiled big endian IEEE long double:
.size   a, 16
a:
.long   1073567061
.long   1431655765
.long   1431655765
.long   1431655765
where the numbers in .long arguments are 32-bit numbers stored in the
selected endianity.  Compiled with -mlong-double-64 little endian:
.size   a, 8
a:
.long   1431655765
.long   1070945621
and big endian:
.size   a, 8
a:
.long   1070945621
.long   1431655765
Unless I'm misreading this, for IEEE long double, or double (and I bet float
too) byteswapping the whole numbers is what is needed for interoperability
between powerpc64{,le}-linux, for IBM long double we'd actually want to
byteswap it as 2 real(kind=8) numbers and not one real(kind=16) one, i.e.
the numbers are always stored as the more significant double followed by
less significant double in memory.

Jakub



Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Thomas Koenig via Fortran



On 08.01.22 15:02, Jakub Jelinek via Fortran wrote:

Note, as for byteswapping, apparently it wasn't ever working right fox
the IBM extended real(kind=16) and complex(kind=16).


The lack of bug reports since the conversion feature was introduced in
2006, more than 15 years ago, tells us something, I guess...


Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Jakub Jelinek via Fortran
On Sat, Jan 08, 2022 at 03:13:10PM +0100, Thomas Koenig wrote:
> 
> On 08.01.22 15:02, Jakub Jelinek via Fortran wrote:
> > Note, as for byteswapping, apparently it wasn't ever working right fox
> > the IBM extended real(kind=16) and complex(kind=16).
> 
> The lack of bug reports since the conversion feature was introduced in
> 2006, more than 15 years ago, tells us something, I guess...

powerpc64le was only introduced in GCC 4.8 in 2013, so slightly less
than that, but still.
Either nobody interchanges/shares fortran unformatted data between
powerpc big and little endian, or if they do, they don't use real(kind=16)
or complex(kind=16) in there...

Jakub



Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Michael Meissner via Fortran
On Sat, Jan 08, 2022 at 03:18:07PM +0100, Jakub Jelinek wrote:
> On Sat, Jan 08, 2022 at 03:13:10PM +0100, Thomas Koenig wrote:
> > 
> > On 08.01.22 15:02, Jakub Jelinek via Fortran wrote:
> > > Note, as for byteswapping, apparently it wasn't ever working right fox
> > > the IBM extended real(kind=16) and complex(kind=16).
> > 
> > The lack of bug reports since the conversion feature was introduced in
> > 2006, more than 15 years ago, tells us something, I guess...
> 
> powerpc64le was only introduced in GCC 4.8 in 2013, so slightly less
> than that, but still.
> Either nobody interchanges/shares fortran unformatted data between
> powerpc big and little endian, or if they do, they don't use real(kind=16)
> or complex(kind=16) in there...

I still wish I had had the forethought when we were setting up the LE ABI to
change the default 128-bit format to IEEE instead of IBM.  But alas, I didn't.
You would still need converters between the big endian IBM format and little
endian IEEE format, but it would have avoided a lot of the problems where GCC
assumes there is only one floating point format for each size.

-- 
Michael Meissner, IBM
PO Box 98, Ayer, Massachusetts, USA, 01432
email: meiss...@linux.ibm.com


Re: [power-ieee128] OPEN CONV

2022-01-08 Thread David Edelsohn via Fortran
On Sat, Jan 8, 2022 at 1:59 PM Michael Meissner  wrote:
>
> On Sat, Jan 08, 2022 at 03:18:07PM +0100, Jakub Jelinek wrote:
> > On Sat, Jan 08, 2022 at 03:13:10PM +0100, Thomas Koenig wrote:
> > >
> > > On 08.01.22 15:02, Jakub Jelinek via Fortran wrote:
> > > > Note, as for byteswapping, apparently it wasn't ever working right fox
> > > > the IBM extended real(kind=16) and complex(kind=16).
> > >
> > > The lack of bug reports since the conversion feature was introduced in
> > > 2006, more than 15 years ago, tells us something, I guess...
> >
> > powerpc64le was only introduced in GCC 4.8 in 2013, so slightly less
> > than that, but still.
> > Either nobody interchanges/shares fortran unformatted data between
> > powerpc big and little endian, or if they do, they don't use real(kind=16)
> > or complex(kind=16) in there...
>
> I still wish I had had the forethought when we were setting up the LE ABI to
> change the default 128-bit format to IEEE instead of IBM.  But alas, I didn't.
> You would still need converters between the big endian IBM format and little
> endian IEEE format, but it would have avoided a lot of the problems where GCC
> assumes there is only one floating point format for each size.

Mike,

The LE ABI initial target was Power8 and IEEE128 hardware support was
added to Power9.  The ABI was a conscious decision. IEEE 128 was not a
viable requirement for the LE ABI at the time of the transition.

Thanks, David


Re: [power-ieee128] OPEN CONV

2022-01-08 Thread Michael Meissner via Fortran
On Sat, Jan 08, 2022 at 02:15:14PM -0500, David Edelsohn wrote:
> On Sat, Jan 8, 2022 at 1:59 PM Michael Meissner  
> wrote:
> >
> > On Sat, Jan 08, 2022 at 03:18:07PM +0100, Jakub Jelinek wrote:
> > > On Sat, Jan 08, 2022 at 03:13:10PM +0100, Thomas Koenig wrote:
> > > >
> > > > On 08.01.22 15:02, Jakub Jelinek via Fortran wrote:
> > > > > Note, as for byteswapping, apparently it wasn't ever working right fox
> > > > > the IBM extended real(kind=16) and complex(kind=16).
> > > >
> > > > The lack of bug reports since the conversion feature was introduced in
> > > > 2006, more than 15 years ago, tells us something, I guess...
> > >
> > > powerpc64le was only introduced in GCC 4.8 in 2013, so slightly less
> > > than that, but still.
> > > Either nobody interchanges/shares fortran unformatted data between
> > > powerpc big and little endian, or if they do, they don't use real(kind=16)
> > > or complex(kind=16) in there...
> >
> > I still wish I had had the forethought when we were setting up the LE ABI to
> > change the default 128-bit format to IEEE instead of IBM.  But alas, I 
> > didn't.
> > You would still need converters between the big endian IBM format and little
> > endian IEEE format, but it would have avoided a lot of the problems where 
> > GCC
> > assumes there is only one floating point format for each size.
> 
> Mike,
> 
> The LE ABI initial target was Power8 and IEEE128 hardware support was
> added to Power9.  The ABI was a conscious decision. IEEE 128 was not a
> viable requirement for the LE ABI at the time of the transition.

Yes I know, but my memory is we (the GCC group within IBM) at least knew that
IEEE 128-bit was coming towards the end of the ABI definition period.  But
perhaps not.  In any case, it doesn't much matter now, as it is all ancient
history.

-- 
Michael Meissner, IBM
PO Box 98, Ayer, Massachusetts, USA, 01432
email: meiss...@linux.ibm.com