Bug 103030 - floating point to int inconsistency
Summary: floating point to int inconsistency
Status: RESOLVED DUPLICATE of bug 323
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2021-11-01 18:51 UTC by disabled-20211108
Modified: 2021-11-02 18:55 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-11-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description disabled-20211108 2021-11-01 18:51:02 UTC
#include <math.h>
typedef long long llong;
int main(int argc,char **argv){;
int ex=0;
llong num_z=0xf70694892bddbb3f,num_x=0x0000000000f88640;double *z_p=(void *)&num_z,*x_p=(void *)&num_x;
static int le_v=1;char *le=(void *)&le_v;if(le[0])
{	llong num_z2=0,num_x2=0;int n=0;while(n<sizeof(llong))
{		num_z2<<=8;num_z2|=num_z&0xFF;num_z>>=8;
		num_x2<<=8;num_x2|=num_x&0xFF;num_x>>=8;n+=1;
}	num_z=num_z2;num_x=num_x2;
}double c_f=z_p[0]*x_p[0];int a=floor(z_p[0]*x_p[0]),b=z_p[0]*x_p[0],c=c_f;if(a!=b)dprintf(2,"%d %d %d\n",a,b,c);
if(1)dprintf(2,"%.15lf %.15lf %.15lf\n",z_p[0],x_p[0],c_f);
return ex;};
/*fn=;gcc $fn.c -o $fn -lm -include stdio.h&&$fn*/
Comment 1 Andrew Pinski 2021-11-01 20:02:14 UTC
I get the result of 80 for a, b and c on x86_64-linux-gnu (even with -m32) and across compilers, GCC, clang and ICC.

What target are you using?
Comment 2 disabled-20211108 2021-11-02 06:11:42 UTC
i686-linux-gnu
if change c_f to long double and use llf, brings c_f down and brings c down to b
Comment 3 Andrew Pinski 2021-11-02 07:17:31 UTC
This is just an artifact of x87 and how everything is done in 80bit fp and sometimes you get different answers than you are expected.  If SSE2 is turned on, then the integer to fp conversion happens in the SSE2 unit and is converted to 64bit fp before the conversion.

Again this is not really a bug, but a partial misunderstanding of floating point and how x87 floating point works.

*** This bug has been marked as a duplicate of bug 323 ***
Comment 4 disabled-20211108 2021-11-02 18:55:26 UTC
it works with -mpc64
so the default was set to the highest, but I guess should have been limited by the target