Help with conversion of code to modern equivalent...

Hello,

I am trying to convert some old code I found online to a modern equivalent.

I can easily convert the vec4d_length function to glm::length() equivalents; what is throwing me off is the "vector_new" function.

How do I convert that to a modern equivalent using a glm or c++ based function? The way it is written I am not understanding it.

Thank you.

Here is the older code that I want converted:

1
2
3
4
5
6
7

#ifndef M_PHI
#define M_PHI 1.618033988749895  //((1 + sqrt(5)) / 2)
#endif

const float radius = vec4d_length((vec4d)vector_new(1, M_PHI));


And each of those functions are defined as:

1
2
3

#define vector_new(...) {.vex = {__VA_ARGS__}}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

double vec4d_sum(vec4d v)
{
  return v.ptr[0] + v.ptr[1] + v.ptr[2] + v.ptr[3];
}

double vec4d_dot_product(vec4d u, vec4d v)
{
  return vec4d_sum(vector_multiply(u, v));
}

double vec4d_length(vec4d v)
{
  return sqrt(vec4d_dot_product(v, v));
}


The "vex" data type is defined as:

1
2
3
4
5
6

typedef union { float ptr[4]; float vex __attribute__((vector_size(16))); } vec4f;
typedef union { float ptr[16]; float vex __attribute__((vector_size(64))); } mat4f;
typedef union { double ptr[4]; double vex __attribute__((vector_size(32))); } vec4d;
typedef union { double ptr[16]; double vex __attribute__((vector_size(128))); } mat4d;
Last edited on
"vector_new" is a macro that is taking in the arguments and making an array initialization for the ".vex" data member of what ever union. That's not really a type cast on Line 6, it's more like it's creating a temporary object with the '.vex' data member set to those values.

I'd say make the '.vex' data member of your version of those structs into a template. You might be able to get away with making it a double in a virtual parent class, but I don't know how that might affect the rest of your code.
Thank you for the explaination. So, if an array of four (4) items is initialized with two variables such as:

1
2
3
4
5

typedef union {float ptr[4], float vex __attribute__((vector_size(16))); } vec4f;

vec4f vex {1.0f ,2.0f};


Then this array is 4 floats with values of (1.0f, 2.0f, 1.0f, 2.0f)?

What is the "__attribute__" thing and what is the "(vector_size(16)" thing?

I guess that is the confusing part from this old code.

Thank you.
Last edited on
Functions attributes. They aren't standard C or C++ per say, more so a feature of the specific compiler: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Topic archived. No new replies allowed.