473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2672
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_functi on (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_va lues 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_val ues[], 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_va lues 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_v ect, 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_val ues[], 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_vectyp e)/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
misunderstandin gs.
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
3324
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
3199
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 also checked out the boost::multi_array.hpp, and Giovanni Bavistrelli's Array code. Neither of these seem to allow dynamic array dimensions. For example, the user selects a 2 dimensional array, I want to create: MyObject** array = new...
18
4199
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 use my specific constructor to initialize the class. How to do it without using malloc? Something like Point* pt = new Point; I want it to reserve the space for N points only, and not to call default constructor. I dont have a default...
49
3149
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 acceptable alternative.
23
7394
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
4
3424
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 error: error: expected identifier before numeric constant error: expected ';' or '...' before numeric constant But i can declare vector<stringtestArray(50); inside my main
3
2408
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 position struct with it's own x,y and an int called CellType which determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e. empty space. Given... <code>
2
12126
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
4493
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 function: x = new int;
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.