Mail Archives: djgpp/1996/11/30/18:59:54
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 -