www.digitalmars.com         C & C++   DMDScript  

D - bug : float in a struct

reply yaneurao sun-inet.or.jp writes:
phenomenon:
float is used in struct , and if long variable exists ahead of it ,
then a bad floating code would generate.

//	invalid struct
struct a {
long a;		//	must be alignment to quad word ahead of float var.
float x,y,z,w;
}

//	valid struct
struct b {
long a,b;   // ok
float x,y,z,w;
}

//	valid struct
struct c {
float x,y,z,w;
long a;		// ok
}

here , simple example:
I test in WinXp.

// ---------------------------------------
private import std.c.stdlib;

struct i_part
{
long start;
float phase,r,g,b,a;
};
void i_rst(int i_x)
{
rays[i_x].phase=0.180*(float)(rand()%1000);
rays[i_x].r=0.001f*((float)(rand()%1000));
rays[i_x].g=0.001f*((float)(rand()%1000));
rays[i_x].b=0.001f*((float)(rand()%1000));
rays[i_x].a=0.0005f*((float)(rand()%1000));
}

i_part rays[100];

int	main(){
for (int i=0;i<100;i++) i_rst(i);

for (int i=0;i<1;i++) {
int j = 0.0*(cast(float)i); // here , 'fistp [esp]' fails.
}

return 0;
}
Jan 10 2004
next sibling parent "Robert" <no spam.ne.jp> writes:
I guess that allocated sizes of global arrays are wrong
when they have integers on the top and
floating points elsewhere.

==========================
struct Foo {
    long   a;
    double b;
};

Foo array[4];

int main(){
    char* str = " ";
    printf("%p\n", &array[0]);
    printf("%p\n", &array[3].b);
    printf("%p\n", str);
    return 0;
}
==========================
0040E090
0040E0C8
0040E0B8
==========================
" " and array[2].b have the same address 0040E0B8!



~~~~~~~~~~~~~~~~~~~~~~~~~~
struct Foo {
    double a;
    long   b;
};

Foo array[4];

int main(){
    char* str = " ";
    printf("%p\n", &array[0]);
    printf("%p\n", &array[3].b);
    printf("%p\n", str);
    return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~
0040E090
0040E0C8
0040E0D0
~~~~~~~~~~~~~~~~~~~~~~~~~~
It's OK.



<<<<<<<<<<<<<<<<<<<<<<<<<<
struct Foo {
    long   a;
    long   b;
};

Foo array[4];

int main(){
    char* str = " ";
    printf("%p\n", &array[0]);
    printf("%p\n", &array[3].b);
    printf("%p\n", str);
    return 0;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<
00410D10
00410D48
0040E080
<<<<<<<<<<<<<<<<<<<<<<<<<<
It's OK.



++++++++++++++++++++++++++
struct Foo {
    long   a;
    double b;
    long   c;
};

Foo array[4];

int main(){
    char* str = " ";
    printf("%p\n", &array[0]);
    printf("%p\n", &array[3].c);
    printf("%p\n", str);
    return 0;
}
++++++++++++++++++++++++++
0040E098
0040E0F0
0040E0C8
++++++++++++++++++++++++++
" " and array[2].a have the same address 0040E0F0!




struct Foo { long a; double b; }; int main(){ foo(0); return 0; } void foo(int a) { Foo array[4]; int b; printf("%p\n", &array[0]); printf("%p\n", &array[3].b); printf("%p\n", &a); printf("%p\n", &b); }

0012FEF0 0012FF28 0012FEEC 0012FF30

It's OK.
Jan 10 2004
prev sibling next sibling parent reply "Walter" <walter digitalmars.com> writes:
Can you be more specific? Exactly what fails?

<yaneurao sun-inet.or.jp> wrote in message
news:bton25$18rg$1 digitaldaemon.com...
 phenomenon:
 float is used in struct , and if long variable exists ahead of it ,
 then a bad floating code would generate.

 // invalid struct
 struct a {
 long a; // must be alignment to quad word ahead of float var.
 float x,y,z,w;
 }

 // valid struct
 struct b {
 long a,b;   // ok
 float x,y,z,w;
 }

 // valid struct
 struct c {
 float x,y,z,w;
 long a; // ok
 }

 here , simple example:
 I test in WinXp.

 // ---------------------------------------
 private import std.c.stdlib;

 struct i_part
 {
 long start;
 float phase,r,g,b,a;
 };
 void i_rst(int i_x)
 {
 rays[i_x].phase=0.180*(float)(rand()%1000);
 rays[i_x].r=0.001f*((float)(rand()%1000));
 rays[i_x].g=0.001f*((float)(rand()%1000));
 rays[i_x].b=0.001f*((float)(rand()%1000));
 rays[i_x].a=0.0005f*((float)(rand()%1000));
 }

 i_part rays[100];

 int main(){
 for (int i=0;i<100;i++) i_rst(i);

 for (int i=0;i<1;i++) {
 int j = 0.0*(cast(float)i); // here , 'fistp [esp]' fails.
 }

 return 0;
 }
Jan 10 2004
parent "Robert" <no spam.ne.jp> writes:
Please read http://www.digitalmars.com/drn-bin/wwwnews?D/21511

"Walter" <walter digitalmars.com> wrote in message
news:btpmuo$30b1$1 digitaldaemon.com...
 Can you be more specific? Exactly what fails?

 <yaneurao sun-inet.or.jp> wrote in message
 news:bton25$18rg$1 digitaldaemon.com...
 phenomenon:
 float is used in struct , and if long variable exists ahead of it ,
 then a bad floating code would generate.

 // invalid struct
 struct a {
 long a; // must be alignment to quad word ahead of float var.
 float x,y,z,w;
 }

 // valid struct
 struct b {
 long a,b;   // ok
 float x,y,z,w;
 }

 // valid struct
 struct c {
 float x,y,z,w;
 long a; // ok
 }

 here , simple example:
 I test in WinXp.

 // ---------------------------------------
 private import std.c.stdlib;

 struct i_part
 {
 long start;
 float phase,r,g,b,a;
 };
 void i_rst(int i_x)
 {
 rays[i_x].phase=0.180*(float)(rand()%1000);
 rays[i_x].r=0.001f*((float)(rand()%1000));
 rays[i_x].g=0.001f*((float)(rand()%1000));
 rays[i_x].b=0.001f*((float)(rand()%1000));
 rays[i_x].a=0.0005f*((float)(rand()%1000));
 }

 i_part rays[100];

 int main(){
 for (int i=0;i<100;i++) i_rst(i);

 for (int i=0;i<1;i++) {
 int j = 0.0*(cast(float)i); // here , 'fistp [esp]' fails.
 }

 return 0;
 }
Jan 10 2004
prev sibling parent "Walter" <walter digitalmars.com> writes:
Found the problem and fixed it. Thanks, guys. -Walter
Jan 11 2004