Browse Source

Add components array union to each vector definition

rexim 3 weeks ago
parent
commit
30f6609f2b
3 changed files with 59 additions and 16 deletions
  1. 48 12
      la.h
  2. 3 2
      nob.c
  3. 8 2
      src/lag.c

+ 48 - 12
la.h

@@ -18,18 +18,54 @@ LADEF double clampd(double x, double a, double b);
 LADEF int clampi(int x, int a, int b);
 LADEF int clampi(int x, int a, int b);
 LADEF unsigned int clampu(unsigned int x, unsigned int a, unsigned int b);
 LADEF unsigned int clampu(unsigned int x, unsigned int a, unsigned int b);
 
 
-typedef struct { float x, y; } V2f;
-typedef struct { double x, y; } V2d;
-typedef struct { int x, y; } V2i;
-typedef struct { unsigned int x, y; } V2u;
-typedef struct { float x, y, z; } V3f;
-typedef struct { double x, y, z; } V3d;
-typedef struct { int x, y, z; } V3i;
-typedef struct { unsigned int x, y, z; } V3u;
-typedef struct { float x, y, z, w; } V4f;
-typedef struct { double x, y, z, w; } V4d;
-typedef struct { int x, y, z, w; } V4i;
-typedef struct { unsigned int x, y, z, w; } V4u;
+typedef union {
+    struct { float x, y; };
+    float c[2];
+} V2f;
+typedef union {
+    struct { double x, y; };
+    double c[2];
+} V2d;
+typedef union {
+    struct { int x, y; };
+    int c[2];
+} V2i;
+typedef union {
+    struct { unsigned int x, y; };
+    unsigned int c[2];
+} V2u;
+typedef union {
+    struct { float x, y, z; };
+    float c[3];
+} V3f;
+typedef union {
+    struct { double x, y, z; };
+    double c[3];
+} V3d;
+typedef union {
+    struct { int x, y, z; };
+    int c[3];
+} V3i;
+typedef union {
+    struct { unsigned int x, y, z; };
+    unsigned int c[3];
+} V3u;
+typedef union {
+    struct { float x, y, z, w; };
+    float c[4];
+} V4f;
+typedef union {
+    struct { double x, y, z, w; };
+    double c[4];
+} V4d;
+typedef union {
+    struct { int x, y, z, w; };
+    int c[4];
+} V4i;
+typedef union {
+    struct { unsigned int x, y, z, w; };
+    unsigned int c[4];
+} V4u;
 
 
 #define V2f_Fmt "v2f(%f, %f)"
 #define V2f_Fmt "v2f(%f, %f)"
 #define V2f_Arg(v) (v).x, (v).y
 #define V2f_Arg(v) (v).x, (v).y

+ 3 - 2
nob.c

@@ -16,7 +16,6 @@ static void compile(void)
     cmd_append(&cmd, "-Wall");
     cmd_append(&cmd, "-Wall");
     cmd_append(&cmd, "-Wextra");
     cmd_append(&cmd, "-Wextra");
     cmd_append(&cmd, "-Wswitch-enum");
     cmd_append(&cmd, "-Wswitch-enum");
-    cmd_append(&cmd, "-pedantic");
     cmd_append(&cmd, "-I.");
     cmd_append(&cmd, "-I.");
     cmd_append(&cmd, "-I"THIRDPARTY_FOLDER);
     cmd_append(&cmd, "-I"THIRDPARTY_FOLDER);
 }
 }
@@ -38,6 +37,7 @@ int main(int argc, char **argv)
 
 
     {
     {
         compile();
         compile();
+        cmd_append(&cmd, "-pedantic");
         cmd_append(&cmd, "-std=c11");
         cmd_append(&cmd, "-std=c11");
         cmd_append(&cmd, "-ggdb");
         cmd_append(&cmd, "-ggdb");
         cmd_append(&cmd, "-o", BUILD_FOLDER"ball");
         cmd_append(&cmd, "-o", BUILD_FOLDER"ball");
@@ -54,7 +54,8 @@ int main(int argc, char **argv)
         if (!cmd_run(&cmd, .async = &procs)) return 1;
         if (!cmd_run(&cmd, .async = &procs)) return 1;
 
 
         compile();
         compile();
-        cmd_append(&cmd, "-std=c99");
+        cmd_append(&cmd, "-pedantic");
+        cmd_append(&cmd, "-std=c11");
         cmd_append(&cmd, "-DLA_IMPLEMENTATION");
         cmd_append(&cmd, "-DLA_IMPLEMENTATION");
         cmd_append(&cmd, "-x", "c");
         cmd_append(&cmd, "-x", "c");
         cmd_append(&cmd, "-o", BUILD_FOLDER"la.c.o");
         cmd_append(&cmd, "-o", BUILD_FOLDER"la.c.o");

+ 8 - 2
src/lag.c

@@ -79,13 +79,19 @@ static char *vector_comps[VECTOR_MAX_SIZE] = {"x", "y", "z", "w"};
 void gen_vector_def(FILE *stream, size_t n, Type_Def type_def)
 void gen_vector_def(FILE *stream, size_t n, Type_Def type_def)
 {
 {
     const char *vector_type = make_vector_type(n, type_def);
     const char *vector_type = make_vector_type(n, type_def);
-    fprintf(stream, "typedef struct { %s ", type_def.name);
+    fprintf(stream, "typedef union {\n");
+
+    fprintf(stream, "    struct { %s ", type_def.name);
     assert(n <= VECTOR_MAX_SIZE);
     assert(n <= VECTOR_MAX_SIZE);
     for (size_t i = 0; i < n; ++i) {
     for (size_t i = 0; i < n; ++i) {
         if (i > 0) fprintf(stream, ", ");
         if (i > 0) fprintf(stream, ", ");
         fprintf(stream, "%s", vector_comps[i]);
         fprintf(stream, "%s", vector_comps[i]);
     }
     }
-    fprintf(stream, "; } %s;\n", vector_type);
+    fprintf(stream, "; };\n");
+
+    fprintf(stream, "    %s c[%zu];\n", type_def.name, n);
+
+    fprintf(stream, "} %s;\n", vector_type);
 }
 }
 
 
 // Generates function signatures of the following form:
 // Generates function signatures of the following form: