ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/30/18:59:54

Message-ID: <32A0C63A.7D17@pobox.oleane.com>
Date: Sun, 01 Dec 1996 00:41:46 +0100
From: Francois Charton <deef AT pobox DOT oleane DOT com>
Organization: CCMSA
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Float code bug [Was: Re: Problems with DJGPP V2.01 - atof() function]
References: <329e68a5 DOT 10316617 AT news DOT ua DOT pt> <57mtq1$4mo AT vidar DOT diku DOT dk> <32A02DD1 DOT 1157 AT pobox DOT oleane DOT com>

As explained in a previous message, there seem to be some trouble with 
the way DJGPP handles casts from floats to ints.

The program:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char ch[8]="1.13";
float f;
int i,j;
i=(int)(atof(ch)*100.0);   /* 1 */
f=atof(ch)*100.0;          /* 2 */
j=(int)f;
printf("%d %d\n",i,j);
return 0;
}

will output (on my 486DX) "112 113"
so, if you print (int)(1.13*100.0) you get 112
but with
    f=1.13*100.0
    printf("%d",(int)f)
you get 113...

This seems to be linked with the way DJGPP outputs the asm code:
Here is the asm code generated for 1:
        fldl LC2
        fmulp %st,%st(1)
        fnstcw -24(%ebp)
        movl -24(%ebp),%edx
        movb $12,%dh
        movl %edx,-28(%ebp)
        fldcw -28(%ebp)
        fistpl -16(%ebp)
        fldcw -24(%ebp)

and here is the one for 2:
        fldl LC2
        fmulp %st,%st(1)
        fstps -12(%ebp)
        flds -12(%ebp)
        fnstcw -24(%ebp)
        movl -24(%ebp),%edx
        movb $12,%dh
        movl %edx,-28(%ebp)
        fldcw -28(%ebp)
        fistpl -20(%ebp)
        fldcw -24(%ebp)

As you can see, they are just the same, except the second one (the one 
which gives the right answer) has one "fstps" and "flds" more than the 
first... So it would seem that storing a FPU register in an CPU register, 
and then putting it back into the FPU register cures the problem(???)

Could anyone explain why is this? and how it should be fixed?

Francois

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019