Skip to contentSkip to conversation

FPU, i386

DISCUSSION Andrey Ulanov 5 participants 5 messages Apr 17, 2002

lore
Share & tools
lore
#0 lore raw

Look at this:

$ cat test.c
#include <stdio.h>

main()

{
	double h = 0.2;
	
	if(1/h == 5.0)
	    printf("1/h == 5.0\n");

	if(1/h < 5.0)
	    printf("1/h < 5.0\n");
	return 0;
}
$ gcc test.c
$ ./a.out
1/h < 5.0
$ 

I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.

ps. cc to me

Signature · 2 lines
with best regards, Andrey Ulanov.
[email protected]
#1 lore raw

When you use -O2 it works.

Assembly with -O2:

        .file   "t1.c"
        .version        "01.01"
gcc2_compiled.:
.section        .rodata
.LC2:
        .string "1/h == 5.0\n"
.LC3:
        .string "1/h < 5.0\n"
.LC4:
        .string "%llx %llx\n"
        .align 8
.LC1:
        .long 0x0,0x40140000

.text
.align 4
.globl main
.type main,@function
main:

        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        addl $-12,%esp
        pushl $.LC2
        call printf
        addl $16,%esp
        addl $-12,%esp
        pushl .LC1+4
        pushl .LC1
        pushl .LC1+4
        pushl .LC1
        pushl $.LC4
        call printf
        xorl %eax,%eax
        movl %ebp,%esp
        popl %ebp
        ret
.Lfe1:
        .size    main,.Lfe1-main
        .ident  "GCC: (GNU) 2.95.3 20010315 (release)"

Assembly with a plain compile:
.file "t1.c"
.version "01.01"
gcc2_compiled.:
.section .rodata
.LC2:
.string "1/h == 5.0\n"
.LC3:
.string "1/h < 5.0\n"
.LC4:
.string "%llx %llx\n"
.align 8
.LC0:
.long 0x9999999a,0x3fc99999
.align 8
.LC1:
.long 0x0,0x40140000
.text
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
subl $24,%esp
fldl .LC0
fstpl -8(%ebp)
fld1
fdivl -8(%ebp)
fldl .LC1
fucompp
fnstsw %ax
andb $68,%ah
xorb $64,%ah
jne .L3
addl $-12,%esp
pushl $.LC2
call printf
addl $16,%esp
.L3:
fld1
fdivl -8(%ebp)
fldl .LC1
fcompp
fnstsw %ax
andb $69,%ah
jne .L4
addl $-12,%esp
pushl $.LC3
call printf
addl $16,%esp
.L4:
addl $-12,%esp
fldl .LC1
subl $8,%esp
fstpl (%esp)
fld1
fdivl -8(%ebp)
subl $8,%esp
fstpl (%esp)
pushl $.LC4
call printf
addl $32,%esp
xorl %eax,%eax
jmp .L2
.p2align 4,,7
.L2:
movl %ebp,%esp
popl %ebp
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 2.95.3 20010315 (release)"

________________________________________
Michael D. Black Principal Engineer
[email protected] 321-676-2923,x203
http://www.csihq.com Computer Science Innovations
http://www.csihq.com/~mike My home page FAX 321-676-2355
----- Original Message -----
From: "Andrey Ulanov" <[email protected]>
To: <[email protected]>
Sent: Wednesday, April 17, 2002 10:05 AM
Subject: FPU, i386

Look at this:

$ cat test.c
#include <stdio.h>

main()
{
double h = 0.2;

if(1/h == 5.0)

    printf("1/h == 5.0\n");

if(1/h < 5.0)

    printf("1/h < 5.0\n");
return 0;
}
$ gcc test.c
$ ./a.out
1/h < 5.0
$ 

I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.

ps. cc to me

Signature · 7 lines
with best regards, Andrey Ulanov.
[email protected]
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#2 lore raw
In reply to Andrey Ulanov
Quoted 21 lines · Andrey Ulanov

Look at this:

$ cat test.c
#include <stdio.h>

main()

{
	double h = 0.2;
	
	if(1/h == 5.0)
	    printf("1/h == 5.0\n");

	if(1/h < 5.0)
	    printf("1/h < 5.0\n");
	return 0;
}
$ gcc test.c
$ ./a.out
1/h < 5.0
$ 
Quoted 4 lines

I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.

No. No. No. Read something about basic programming of floating-point operations before complaining about something you obviously know nothing about. "==" has no use in floating-point operations. Any code that uses "==", referencing floating-point numbers is broken.

If the BSD 'C' runtime library fortuously says that 1.0/0.2 == 5, than you just got lucky. Time to play the lottery. FYI, you can set the per-process FPU initialization anyway you want. FYI, by default it ignores 1/0 errors:

Cheers,
Dick Johnson

Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).

Windows-2000/Professional isn't.

/*
* Note FPU control only exists per process. Therefore, you have
* to set up the FPU before you use it in any program. */
#include <i386/fpu_control.h>

#define FPU_MASK (_FPU_MASK_IM |\
                  _FPU_MASK_DM |\
                  _FPU_MASK_ZM |\
                  _FPU_MASK_OM |\
                  _FPU_MASK_UM |\
                  _FPU_MASK_PM)

void fpu()

{
    __setfpucw(_FPU_DEFAULT & ~FPU_MASK);
}

main() {
double zero=0.0;
double one=1.0;
fpu();

one /=zero;
}

#3 lore raw
In reply to Andrey Ulanov
Quoted 6 lines · Andrey Ulanov

Look at this:

$ cat test.c
#include <stdio.h>

main()

Quoted 11 lines
{
	double h = 0.2;
	
	if(1/h == 5.0)
	    printf("1/h == 5.0\n");

	if(1/h < 5.0)
	    printf("1/h < 5.0\n");
	return 0;
}
$ gcc test.c
$ gcc -O test.c
$ ./a.out
1/h == 5.0

without -O, gcc initializes h to 0.2000000000000000111

Quoted 12 lines
$ ./a.out
1/h < 5.0
$ 

I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.

ps. cc to me

Signature · 2 lines
with best regards, Andrey Ulanov.
[email protected]
#4 lore raw
In reply to Andrey Ulanov
Quoted 21 lines · Andrey Ulanov

Look at this:

$ cat test.c
#include <stdio.h>

main()

{
        double h = 0.2;

        if(1/h == 5.0)
            printf("1/h == 5.0\n");

        if(1/h < 5.0)
            printf("1/h < 5.0\n");
        return 0;
}
$ gcc test.c
$ ./a.out
1/h < 5.0
$
Quoted 9 lines

I also ran same a.out under FreeBSD. It says "1/h == 5.0". It seems there is difference somewhere in FPU initialization code. And I think it should be fixed.

ps. cc to me
--
with best regards, Andrey Ulanov.
[email protected]

google for every scientist should know about floating point