471,585 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

replacing array by vector type in existing code?

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[i] by
myvect.array[i]. 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.

Feb 14 '06 #1
3 2518
Lars Grobe wrote:
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[i] by
myvect.array[i]. 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 14 '06 #2
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.

Feb 14 '06 #3
Lars Grobe wrote:
_If_ I understand you correctly, you want to use an array
3 of float or an array N of float.

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by James | last post: by
18 posts views Thread by toton | last post: by
49 posts views Thread by vfunc | last post: by
4 posts views Thread by kungfuelmosan | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.