473,225 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 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 2653
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
3
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in the map is called a Cell, a cell contains only a...
2
by: blueflyy | last post by:
Hello, I wish to initialize a vector<BYTE> from an existing BYTE array. Simple example: BYTE buf = {0,1,2,3,4,5}; std::vector<BYTE> vec(buf, buf + sizeof(buf)/sizeof(buf));
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.