internal representation of struct

Daniel Gibson metalcaedes at gmail.com
Fri Mar 18 10:33:57 PDT 2011


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



More information about the Digitalmars-d mailing list