Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C - Union of struct and array, ugly syntax

Name: Anonymous 2013-08-16 23:57

I'm writing some floating pointer vector types in C. Using a union, I can get both struct and array representations of the same type, depending on which is more convenient. The syntax is very ugly, though:

#include <stdio.h>
#include <tgmath.h>
typedef union {
    struct { float x, y; };
    float val[2];
} vec2;
int main(int argc, char *argv[]) {
    vec2 herp = {{3, 4}}; // using {3, 4} throws an error when -Wall is enabled
    printf("Vector: {%f, %f}\n", herp.x, herp.y);
    float len = sqrt(herp.x * herp.x + herp.y * herp.y);
    printf("Length: %f\n", len);
    vec2 normalized = {{herp.val[0] / len, herp.val[1] / len}};
    printf("Normalized: {%f, %f}\n", normalized.val[0], normalized.val[1]);
}


Is there any way, using C99, C11, or possibly even GNU C extensions, that I can get syntax like the following?

vec2 derp = {6, 20}; // no need for two sets of braces in initializers
derp[0] = 5; // "anonymous" array


or am I better off making my vectors only structs or only arrays, and casting them to the other type when the alternative syntax is more convenient? (.x and .y are more convenient most of the time, but treating them as arrays will be more convenient for operations between vectors and matrices)

I know this is trivial shit but I want to get it right before I go any further.

Name: Anonymous 2013-08-17 1:53

>>5 I was going to look up GCC SIMD intrinsics later, but hoo-lee-shit this makes life easier for me. Simple vector-to-vector arithmetic, predefined vector data types (that also, I assume, have the proper static alignment already), and likely much better SIMD code generation than uses standard aligned structs or arrays would have given, and it's that easy? Thanks a billion for coming through for me /prog/, I was this close to ripping John Carmack's preprocessor macros from the Quake 3 engine source like an idiot. Instead, I can use very similar syntax to what I can do in GLSL or OpenCL, and I barely have to implement anything.

Though I must say, just from reading that document, this doesn't necessarily seem like an advantage of Clang over GCC to me, because GCC seems to support almost the same syntax for its vector intrinsics:
http://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html

>>7 That's a smart idea, too. This way, I can easily use the vector types for colors as well: enum { A, B, G, R};

>>8 That might tempt me to switch to "C-style C++" when it's released, if something similar isn't available as a GCC or Clang extension for C programs, but I guess I can get by with __builtin_shuffle / __builtin_shufflevector and some C11 type generic macros for now.

>>9 That wouldn't help at all for 3 element or 4 element vectors, though.

Name: Anonymous 2013-08-17 5:20

Thread full of idiots
- "Use C++"
- "NO I DONT WANT TO USE SUPERIOR TECHNOLOGY"
- "LOL EVERYONE KNOWS C IS BETTER"

Haha, idiot C++ haters. Enjoy your inferior language. I can reimplement your C shit with half smaller C++ code and better runtime performance.

Idiots.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List