replacing array by vector type in existing code?

L

Lars Grobe

Hi,

first hello, I am new to the list, and I guess my question will show
that clearly.

I want to use some vector operations (at the moment altivec) in
existing code. It is a raytracing-based application, and for now, I
would like to replace the operations on my data type vect3 (which is a
float[3] array) by operations on a vector float. The vect3 is simply a
typedef to an array of floats so far.

Now I thought that I could use a union of vector and array of floats.
As these share memory, I could access the value by myvect.array[0] as
well as by myvect.vect, right? But I want to keep the interface to the
existing app, and it would be inconvenient to replace all myvect by
myvect.array. Here I hope that either the preprocessor or typedef
could help me, I just don't know how. Can I "redirect" calls to an
element of an array to a element of an array in an union? If not, I
would have to browse all code for useage of the vector type and replace
the array by the array in union.

And is the whole idea of using the vector and the array in the union to
get two interfaces to the same data a possible solution? I came to this
as it is proposed to keep arrays aligned, but I am not sure if I also
are allowed to use both.

TIA+CU, Lars.
 
M

Michael Mair

Lars said:
Hi,

first hello, I am new to the list, and I guess my question will show
that clearly.

I want to use some vector operations (at the moment altivec) in
existing code. It is a raytracing-based application, and for now, I
would like to replace the operations on my data type vect3 (which is a
float[3] array) by operations on a vector float. The vect3 is simply a
typedef to an array of floats so far.

Now I thought that I could use a union of vector and array of floats.
As these share memory, I could access the value by myvect.array[0] as
well as by myvect.vect, right? But I want to keep the interface to the
existing app, and it would be inconvenient to replace all myvect by
myvect.array. Here I hope that either the preprocessor or typedef
could help me, I just don't know how. Can I "redirect" calls to an
element of an array to a element of an array in an union? If not, I
would have to browse all code for useage of the vector type and replace
the array by the array in union.

And is the whole idea of using the vector and the array in the union to
get two interfaces to the same data a possible solution? I came to this
as it is proposed to keep arrays aligned, but I am not sure if I also
are allowed to use both.


_If_ I understand you correctly, you want to use an array
3 of float or an array N of float. If this is not what you
mean: Provide compiling example code demonstrating what
you mean -- ideally a minimal example.

As long as you can guarantee N >= 3, the old functions will
work without fail on the first three elements as the
underlying type is the same.

Are we talking about the following?

typedef float vect3[3];

union {
vect3 fixvect;
float arrayN[42];
} vararray;

int ye_aulde_function (vect3 v1, vect3 v2, vect3 dest)
{
if (!v1 || !v2 || !v3)
return WRONG_ARG_NR;
/* Compute dest */
return NUM_OK;
}

....

Notes:
- vect3 still degenerates into a float * when passed as
argument
- in C99, you can express "at least 3 elements" by using
static in the prototype:
int foo (float vec1[static 3])
- vect3 *foo;
is the same as
float (*foo)[3];


Cheers
Michael
 
L

Lars Grobe

Hi!

Thanks for the reply!!!
_If_ I understand you correctly, you want to use an array
3 of float or an array N of float.

My vector type is an aligned "array N", which can hold 128bit in
Altivec. So it is in fact like array[4] and should work, right?

I have to do the following:

Instead of defining vect3 as float[3], I have to define it as

typedef union { float array_values[3]; vector float vector_values; }
vect3;

That way, I ensure that the data is always aligned, important for
altivec, and in my functions, I can use vect3.vector_values for
altivec-operations.

But now, my existing functions still expect vect3 to be an array. They
do not know that they have to use vect3.array_values[], they want tu
use vect3[]. I could search for all occurances of vect3 in the code and
change them, but I wonder if this could not be done by a simple macro
or even typedef?

Sorry, I cannot send code right now, will have to try a bit tonight.
But I am not good in typedefs and preprocessor macros, that's why I
ask.

Thanks...CU Lars.
 
M

Michael Mair

Please do not snip attributions -- in long discussions,
it can be helpful to know who said what.
My vector type is an aligned "array N", which can hold 128bit in
Altivec. So it is in fact like array[4] and should work, right?

I have to do the following:

Instead of defining vect3 as float[3], I have to define it as

typedef union { float array_values[3]; vector float vector_values; }
vect3;

That way, I ensure that the data is always aligned, important for
altivec, and in my functions, I can use vect3.vector_values for
altivec-operations.

Do not change the underlying types if the old routines rely on
a certain representation.
If they use access macros, say
VECT3_COMP(my_vect, 2)
instead of direct access
my_vect[2]
you can easily replace the underlying type, as you just have to
replace the access macro definitions along with the typedefs.
Otherwise, you have to wrap vect3:
typedef union {
vect3 ye_aulde_vect3;
aligned_vectype aligned_vect4;
} vect_wrapper;

If for aligned_vectype and vect3 holds that they are essentially
"array N of float" and "array M of float" -- special alignment
nonwithstanding -- and you usually pass vect3 (which means passing
float *) then you can just pass the address of vect_wrapper
cast to float * instead of vect3.
Even if you malloc() new single vect3 objects, these are guaranteed
to be suitably aligned.
Problems arise for arrays of vect3 only.
But now, my existing functions still expect vect3 to be an array. They
do not know that they have to use vect3.array_values[], they want tu
use vect3[]. I could search for all occurances of vect3 in the code and
change them, but I wonder if this could not be done by a simple macro
or even typedef?

No.

If you are working with arrays of vect3, too, you will have to
change the typedef of vect3 to
typedef float vect3[sizeof (aligned_vectype)/sizeof (float)];
and you have to make sure that your old routines do not give
you non-malloc()ed arrays of vect3 when interacting with the
new ones (possible, e.g., if they use static storage duration
arrays of vect3; these will have to be changed).

Sorry, I cannot send code right now, will have to try a bit tonight.
But I am not good in typedefs and preprocessor macros, that's why I
ask.

Just post your best shot at it; try to make it as short as
possible. Actual, compiling code often eliminates potential
misunderstandings.


Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top