Volunteers for test: print NaN in MIPSPro/gcc

IRIX/Nekoware development, porting and related topics.
Forum rules
Any posts concerning pirated software or offering to buy/sell/trade commercial software are subject to removal.
User avatar
dexter1
Moderator
Moderator
Posts: 2735
Joined: Thu Feb 20, 2003 6:57 am
Location: Zoetermeer, The Netherlands

Volunteers for test: print NaN in MIPSPro/gcc

Unread postby dexter1 » Sun Jan 08, 2017 11:24 am

I'm close to finishing tcl 8.6.6 for nekoware, but i need to fix two test failures.
One is very basic and deals with printing invalid floating point results, a.k.a. NaN 's. Consider this program:

Code: Select all

#include <stdio.h>
#include <math.h>

int main(void)
{
        unsigned int i;
        unsigned long long x;
        float *f;
        double *d;

        x = 0xffffffffffffffff;
        d = (double *)&x;
        printf("double of %llx is %f \n",x,*d);

        x = 0x7fffffffffffffff;
        d = (double *)&x;
        printf("double of %llx is %f \n",x,*d);

        i = 0xffffffff;
        f = (float *)&i;
        printf("float of %x is %f \n",i,*f);

        i = 0x7fffffff;
        f = (float *)&i;
        printf("float of %x is %f \n",i,*f);

        i = 0xff800000;
        f = (float *)&i;
        printf("float of %x is %f \n",i,*f);

        return 0;
}

This program stored the bit patterns for several NaN's (and one negative infinity) in an integer and recasts it into a float or double and prints the result.
It produces the following output on MIPSPro 7.4.4 on my 6.5.22m R4400 ChallengeS:

Code: Select all

cyane:/home/frank/tcltest> ./floatnan
double of ffffffffffffffff is -nan0xffffffff
double of 7fffffffffffffff is nan0xffffffff
float of ffffffff is nan0x7fffffff
float of 7fffffff is nan0x7fffffff
float of ff800000 is -inf

In linux (gcc 5.4.0 Ubuntu 16.04.1) it outputs:

Code: Select all

frank@link:/tmp$ ./floatnan
double of ffffffffffffffff is -nan
double of 7fffffffffffffff is nan
float of ffffffff is -nan
float of 7fffffff is nan
float of ff800000 is -inf

Formally a NaN is not signed, since it does not represent a number. IEEE-754 standard mentions that the sign (first bit of the 4 or 8 byte floating point bitpattern) of a NaN is not used in most application. What is strange however, is that MIPSPro does print the sign of the NaN for double precision fp, but not for single precision.
Before i slag the tcl maintainers with this 'anomaly' i want to ask you guys to confirm this. Since i'm lazy and don't want to reinstall with 6.5.30 or boot my O2 to counter-test this with fw_gcc or neko_gcc, can people confirm this behavior in:

- MIPSpro 7.4 on 6.5.30m (patched, preferably)
- older compiler versions? (i don't have a 7.3.1.3 module installation at hand)

Also, what happens with this code on gcc on IRIX?

Appreciated :)
:Crimson: :PI: :Indigo: :O2: :Indy: :Indigo2: :Indigo2IMP:

User avatar
Krokodil
Donor
Donor
Posts: 478
Joined: Fri Apr 17, 2015 2:32 pm
Location: The House of Particular Individuals

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby Krokodil » Sun Jan 08, 2017 7:33 pm

I am willing to test it in gcc. I'll need to get the stuff out of the way of the keyboard though.
:Octane2: - :O2: - :Octane: - :Indigo2IMP:

User avatar
miod
Posts: 508
Joined: Fri Oct 09, 2009 2:44 am
Location: Clermont-Ferrand (France)
Contact:

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby miod » Sun Jan 08, 2017 11:49 pm

You have to keep in mind that printf() is a variadic function, and that float arguments to variadic functions get promoted to double.

If you look at the generated assembly code for the three printf() calls involving float, you can see that your float variable get converted to a double with the `cvt.d.s` instruction, and then passed as a double.

Depending on the particular MIPS processor you are running on, converting a NaN can either:
- implemented completely in hardware, and the result of the conversion shall be a NaN of the same signalling/non-signalling state, which can cause the sign bit to change as it is supposed to be meaningless.
- unimplemented in hardware, which will trap to the operating system. You will get a NaN as well, and depending upon the quality of the operating system completion code, it may have the same sign bit as the input.

Running your test on an R10000 under OpenBSD here, I get the same results as you do - the float nan are printed as positive.
:Indigo:R3000 (alas, dead) :Indigo:R4000 x4 :Indigo2:R4400 :Indigo2IMP:R4400 x2 :Indigo2:R8000 :Indigo2IMP:R10000 :Indy:R4000PC :Indy:R4000SC :Indy:R4400SC :Indy:R4600 :Indy:R5000SC :O2:R5000 x3 :O2:RM7000 :Octane:2xR10000 :Octane:R12000 :O200:2xR12000 :O200: - :O200:2x2xR10000 :Fuel:R16000 :O3x0:4xR16000 :A350:
among more than 150 machines : Apollo, Data General, Digital, HP, IBM, MIPS before SGI, Motorola, NeXT, SGI, Solbourne, Sun...

User avatar
dexter1
Moderator
Moderator
Posts: 2735
Joined: Thu Feb 20, 2003 6:57 am
Location: Zoetermeer, The Netherlands

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby dexter1 » Mon Jan 09, 2017 5:16 am

miod wrote:You have to keep in mind that printf() is a variadic function, and that float arguments to variadic functions get promoted to double.

Correct, i forgot about that.
I'm just trying to make certain that IRIX discards the sign in single precision NaN (or 'float NaN' as i call it) , either after conversion to double or when displayed by printf().
tcl8.6.6 displays 0xffffffff float NaN as nan(0x7fffffff) instead of C's nan0x7fffffff (or -nan0x7fffffff to be consistent with double FP), so i infer there is some processing going on before displaying the result.

Depending on the particular MIPS processor you are running on, converting a NaN can either:
- implemented completely in hardware, and the result of the conversion shall be a NaN of the same signalling/non-signalling state, which can cause the sign bit to change as it is supposed to be meaningless.

I'm not sure if any float NaN can be safely converted to double, either in hard- or software, but it is likely that the processor or libc has a conversion table for such 'loopy' numbers. And if that table is set up so that any float NaN is converted to either a signalling double NaN (0x7ff8xxxxxxxxxxxx in MIPS) or an Quiet NaN (0x7ff0xxxxxxxxxxxx in MIPS), then the sign is definately lost.

- unimplemented in hardware, which will trap to the operating system. You will get a NaN as well, and depending upon the quality of the operating system completion code, it may have the same sign bit as the input.

That i fear, since nobody wants to sift through that code and in the end necessitating to catch every printf/fprintf to create an exception for adding a sign to a float NaN.

Running your test on an R10000 under OpenBSD here, I get the same results as you do - the float nan are printed as positive.

From http://man.openbsd.org/printf.1 i read that openBSD is supposed to shown the sign of the NaN, so this indicates a hardware conversion in the Processor itself...

Thanks for that!
:Crimson: :PI: :Indigo: :O2: :Indy: :Indigo2: :Indigo2IMP:

User avatar
miod
Posts: 508
Joined: Fri Oct 09, 2009 2:44 am
Location: Clermont-Ferrand (France)
Contact:

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby miod » Mon Jan 09, 2017 6:35 am

dexter1 wrote:
Running your test on an R10000 under OpenBSD here, I get the same results as you do - the float nan are printed as positive.

From http://man.openbsd.org/printf.1 i read that openBSD is supposed to shown the sign of the NaN, so this indicates a hardware conversion in the Processor itself...

This is very likely. Under OpenBSD, floating-point completion is performed using John Hauser's SoftFloat code, which preserves the sign of NaNs during conversions. If it had been invoked, the double result would have had the same sign as the float.
:Indigo:R3000 (alas, dead) :Indigo:R4000 x4 :Indigo2:R4400 :Indigo2IMP:R4400 x2 :Indigo2:R8000 :Indigo2IMP:R10000 :Indy:R4000PC :Indy:R4000SC :Indy:R4400SC :Indy:R4600 :Indy:R5000SC :O2:R5000 x3 :O2:RM7000 :Octane:2xR10000 :Octane:R12000 :O200:2xR12000 :O200: - :O200:2x2xR10000 :Fuel:R16000 :O3x0:4xR16000 :A350:
among more than 150 machines : Apollo, Data General, Digital, HP, IBM, MIPS before SGI, Motorola, NeXT, SGI, Solbourne, Sun...

User avatar
Krokodil
Donor
Donor
Posts: 478
Joined: Fri Apr 17, 2015 2:32 pm
Location: The House of Particular Individuals

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby Krokodil » Mon Jan 09, 2017 10:53 am

With gcc4.7 from Nekoware I get the following. IRIX 6.5.30, Octane II, R14000.

Code: Select all

double of ffffffffffffffff is nan0x7fffffff
double of 7fffffffffffffff is nan0x7fffffff
float of ffffffff is nan0x7fffffff
float of 7fffffff is nan0x7fffffff
float of ff800000 is -inf
:Octane2: - :O2: - :Octane: - :Indigo2IMP:

User avatar
miod
Posts: 508
Joined: Fri Oct 09, 2009 2:44 am
Location: Clermont-Ferrand (France)
Contact:

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby miod » Mon Jan 09, 2017 1:45 pm

I have just checked R8000, cvt.d.s of NaN is not implemented in hardware and traps, and thus I get the proper sign.
:Indigo:R3000 (alas, dead) :Indigo:R4000 x4 :Indigo2:R4400 :Indigo2IMP:R4400 x2 :Indigo2:R8000 :Indigo2IMP:R10000 :Indy:R4000PC :Indy:R4000SC :Indy:R4400SC :Indy:R4600 :Indy:R5000SC :O2:R5000 x3 :O2:RM7000 :Octane:2xR10000 :Octane:R12000 :O200:2xR12000 :O200: - :O200:2x2xR10000 :Fuel:R16000 :O3x0:4xR16000 :A350:
among more than 150 machines : Apollo, Data General, Digital, HP, IBM, MIPS before SGI, Motorola, NeXT, SGI, Solbourne, Sun...

User avatar
dexter1
Moderator
Moderator
Posts: 2735
Joined: Thu Feb 20, 2003 6:57 am
Location: Zoetermeer, The Netherlands

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby dexter1 » Mon Jan 09, 2017 2:31 pm

Thanks Krokodil. Looks like gcc uses hardware for both conversions.
And the R8000 result from miod strengthens me to face the tcl devs and dissuade them from comparing results with signed NaN's
:Crimson: :PI: :Indigo: :O2: :Indy: :Indigo2: :Indigo2IMP:

User avatar
Krokodil
Donor
Donor
Posts: 478
Joined: Fri Apr 17, 2015 2:32 pm
Location: The House of Particular Individuals

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby Krokodil » Mon Jan 09, 2017 3:14 pm

dexter1 wrote:Thanks Krokodil. Looks like gcc uses hardware for both conversions.
And the R8000 result from miod strengthens me to face the tcl devs and dissuade them from comparing results with signed NaN's


You're welcome. Hopefully it helps you with getting updated software out. If you want me to test anymore stuff, let me know.
:Octane2: - :O2: - :Octane: - :Indigo2IMP:

User avatar
dexter1
Moderator
Moderator
Posts: 2735
Joined: Thu Feb 20, 2003 6:57 am
Location: Zoetermeer, The Netherlands

Re: Volunteers for test: print NaN in MIPSPro/gcc

Unread postby dexter1 » Wed Jan 11, 2017 3:03 pm

I've filed this in the tcl ticket database. You can see it here: https://core.tcl.tk/tcl/tktview?name=bf029ced86
:Crimson: :PI: :Indigo: :O2: :Indy: :Indigo2: :Indigo2IMP:


Return to “SGI: Development”

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest