www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - internal representation of struct

reply lenochware <lenochware gmail.com> writes:
Hello, I have array of type vertex_t vertices[] where vertex_t is:

struct vertex_t {
  float[3] xyz;
  ubyte[4] color;
  ...
}

Now, I would like use instead of array "float[3] xyz" "vec3f xyz", where vec3f
is:

struct vec3f {
  float x, y, z;

...some functions...
}

where I have defined some operators for adding and multipliing vectors etc.

Is it possible? "xyz" must be exactly 3 floats in memory because I am sending
vertices array pointer into OpenGL rendering pipeline.
Will be struct vec3f represented just like 3 floats - aren't here some
additional metadata - because structure contains also some functions?

My additional question: What about effectivity? I would like keep struct
vertex_t as simple as possible, because I need do calculations fast. Will have
replacing float array to structure some impact on speed?

Thank you.
Mar 18 2011
next sibling parent Simon Buerger <krox gmx.net> writes:
On 18.03.2011 17:34, lenochware wrote:
 Hello, I have array of type vertex_t vertices[] where vertex_t is:

 struct vertex_t {
    float[3] xyz;
    ubyte[4] color;
    ...
 }

 Now, I would like use instead of array "float[3] xyz" "vec3f xyz", where vec3f
is:

 struct vec3f {
    float x, y, z;

 ...some functions...
 }

 where I have defined some operators for adding and multipliing vectors etc.

 Is it possible? "xyz" must be exactly 3 floats in memory because I am sending
 vertices array pointer into OpenGL rendering pipeline.
 Will be struct vec3f represented just like 3 floats - aren't here some
 additional metadata - because structure contains also some functions?
There is no metadata in structs. The only thing there is padding in order to align the fields. But floats have a size of 4 and alignment the same, so your vec3f will be exactly 12 bytes without any problem (done it myself for OpenGL)
 My additional question: What about effectivity? I would like keep struct
 vertex_t as simple as possible, because I need do calculations fast. Will have
 replacing float array to structure some impact on speed?
depends: When you want to to a component-wise vector multiplication, you want the compiler to use SSE and alike. With float[3] a,b,c; c[] = a[]*b[]; That will probably work (that is one reason the []-notion exists in the first place). But with vec3f a,b,c; c.x = a.x*b.x; c.y=a.y*b.y; c.z=a.z*b.z; it really depends on the quality of the compiler if it sees the opportunity for optimisation. If you want to be sure, I suggest the following: struct vec3 { float[3] data; vec3 opMul(vec3 other) { vec3 tmp = this; tmp.data[] *= other.data[]; } } As a last note: When you do OpenGL, the biggest part of calculations should be done on the GPU, so the few on the CPU might not be performance critical at all. But that depends of course on the exact thing you want to do. - Krox
Mar 18 2011
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
On 03/18/2011 05:34 PM, lenochware wrote:
 Hello, I have array of type vertex_t vertices[] where vertex_t is:

 struct vertex_t {
    float[3] xyz;
    ubyte[4] color;
    ...
 }

 Now, I would like use instead of array "float[3] xyz" "vec3f xyz", where vec3f
is:

 struct vec3f {
    float x, y, z;

 ...some functions...
 }

 where I have defined some operators for adding and multipliing vectors etc.

 Is it possible? "xyz" must be exactly 3 floats in memory because I am sending
 vertices array pointer into OpenGL rendering pipeline.
I think --but not 100% sure-- you can count on having no diff in terms of memory layout. The gain in terms of code legibility is well worth it: vec.z vs vs[2] color.R vs color[0] Side-note: in the D community a coding style in starting to spread in which type names are capitalised: thus, would be Vertex instead of vertex_t, and Vec3f instead of vec3f. (This is for public code, indeed; I just inform.)
 Will be struct vec3f represented just like 3 floats - aren't here some
 additional metadata - because structure contains also some functions?
Since struct member functions/methods are static, their calls are translated at compile time. There should thus be no diff with an external, "free", function having a struct object as first argument. This is the main low-level diff with dynamic ("virtual") methods. But don't take my words for sure information, I'd like someone to confirm this.
 My additional question: What about effectivity? I would like keep struct
 vertex_t as simple as possible, because I need do calculations fast. Will have
 replacing float array to structure some impact on speed?
There should be no sensible difference noticeable. On modern architectures, pointer deref, array lookup and (static) member access are about the same speed. (Compared to speed of code that operates on data accessed that way). In fact, at a lower level, static member access is the same thing as array lookup (deref at base_pointer+offset). You may notice some difference on artificiallly "empty" benchmarks (doing nothing but accessing the data), but it's not representative. Denis -- _________________ vita es estrany spir.wikidot.com
Mar 18 2011
prev sibling next sibling parent reply Daniel Gibson <metalcaedes gmail.com> writes:
Am 18.03.2011 17:34, schrieb lenochware:
 Hello, I have array of type vertex_t vertices[] where vertex_t is:

 struct vertex_t {
    float[3] xyz;
    ubyte[4] color;
    ...
 }

 Now, I would like use instead of array "float[3] xyz" "vec3f xyz", where vec3f
is:

 struct vec3f {
    float x, y, z;

 ...some functions...
 }

 where I have defined some operators for adding and multipliing vectors etc.

 Is it possible? "xyz" must be exactly 3 floats in memory because I am sending
 vertices array pointer into OpenGL rendering pipeline.
 Will be struct vec3f represented just like 3 floats - aren't here some
 additional metadata - because structure contains also some functions?

 My additional question: What about effectivity? I would like keep struct
 vertex_t as simple as possible, because I need do calculations fast. Will have
 replacing float array to structure some impact on speed?

 Thank you.
Why not do something like: struct vec3f { float[3] xyz; property float x() { return xyz[0]; } // read x property float x(float val) { return xyz[0] = val; } // write x // and the same for y and z ... // ... and your own functions } This would allow to easily use array optimizations (SSE etc) - just add an overload for e.g. * and let it do xyz[] * othervec[] or similar.. And you'd make sure that it's really an array without any alignment (not sure if alignment on amd64 would be different from x86). And still you could write, because of the properties, vec3f v1; v1.x = 4.2; v1.y = 12.3; v1.z = v1.y-v1.x; and so on. Cheers, - Daniel
Mar 18 2011
parent Trass3r <un known.com> writes:
Am 18.03.2011, 18:33 Uhr, schrieb Daniel Gibson <metalcaedes gmail.com>:
 Why not do something like:
 struct vec3f {
    float[3] xyz;
     property float x() { return xyz[0]; } // read x
     property float x(float val) { return xyz[0] = val; } // write x
    // and the same for y and z ...
    // ... and your own functions
 }
You could also use unions to achieve that.
Mar 19 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, March 18, 2011 10:14:34 spir wrote:
 Side-note: in the D community a coding style in starting to spread in which
 type names are capitalised: thus, would be Vertex instead of vertex_t, and
 Vec3f instead of vec3f. (This is for public code, indeed; I just inform.)
LOL. That's the way that _most_ languages do it, I believe. C is one of the odd ones in that it isn't all that common there. Most everywhere else though it's normal to capitalize type names. So, I wouldn't say that it's really the case that the practice is starting to spread so much as anyone who hasn't been capitalizing their type names is likely from a C background and is likely the odd man out as far as what's typical goes. It's typical in most object-oriented languages to capitalize type names. - Jonathan M Davis
Mar 18 2011
prev sibling parent so <so so.so> writes:
On Fri, 18 Mar 2011 18:34:19 +0200, lenochware <lenochware gmail.com>  
wrote:

 Hello, I have array of type vertex_t vertices[] where vertex_t is:

 struct vertex_t {
   float[3] xyz;
   ubyte[4] color;
   ...
 }

 Now, I would like use instead of array "float[3] xyz" "vec3f xyz", where  
 vec3f is:

 struct vec3f {
   float x, y, z;

 ...some functions...
 }

 where I have defined some operators for adding and multipliing vectors  
 etc.

 Is it possible? "xyz" must be exactly 3 floats in memory because I am  
 sending
 vertices array pointer into OpenGL rendering pipeline.
 Will be struct vec3f represented just like 3 floats - aren't here some
 additional metadata - because structure contains also some functions?

 My additional question: What about effectivity? I would like keep struct
 vertex_t as simple as possible, because I need do calculations fast.  
 Will have
 replacing float array to structure some impact on speed?

 Thank you.
D struct is POD. If you replace it with a struct, the speed is up to you since an array implementation might use cpu specific extensions.
Mar 18 2011