473,387 Members | 1,504 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,387 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 2660
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.