Selaa lähdekoodia

Implement vector convertion functions

rexim 4 vuotta sitten
vanhempi
commit
020345c330
2 muutettua tiedostoa jossa 795 lisäystä ja 12 poistoa
  1. 729 8
      la.h
  2. 66 4
      lag.c

+ 729 - 8
la.h

@@ -7,10 +7,27 @@ float lerpf(float a, float b, float t);
 double lerp(double a, double b, double t);
 
 typedef struct { float x, y; } V2f;
+typedef struct { double x, y; } V2d;
+typedef struct { int x, y; } V2i;
+typedef struct { float x, y, z; } V3f;
+typedef struct { double x, y, z; } V3d;
+typedef struct { int x, y, z; } V3i;
+typedef struct { float x, y, z, w; } V4f;
+typedef struct { double x, y, z, w; } V4d;
+typedef struct { int x, y, z, w; } V4i;
+
 #define V2f_Fmt "v2f(%f, %f)"
 #define V2f_Arg(v) (v).x, (v).y
 V2f v2f(float x, float y);
 V2f v2ff(float x);
+V2f v2f2d(V2d a);
+V2f v2f2i(V2i a);
+V2f v2f3f(V3f a);
+V2f v2f3d(V3d a);
+V2f v2f3i(V3i a);
+V2f v2f4f(V4f a);
+V2f v2f4d(V4d a);
+V2f v2f4i(V4i a);
 V2f v2f_sum(V2f a, V2f b);
 V2f v2f_sub(V2f a, V2f b);
 V2f v2f_mul(V2f a, V2f b);
@@ -27,11 +44,18 @@ V2f v2f_ceil(V2f a);
 float v2f_sqrlen(V2f a);
 float v2f_len(V2f a);
 
-typedef struct { double x, y; } V2d;
 #define V2d_Fmt "v2d(%lf, %lf)"
 #define V2d_Arg(v) (v).x, (v).y
 V2d v2d(double x, double y);
 V2d v2dd(double x);
+V2d v2d2f(V2f a);
+V2d v2d2i(V2i a);
+V2d v2d3f(V3f a);
+V2d v2d3d(V3d a);
+V2d v2d3i(V3i a);
+V2d v2d4f(V4f a);
+V2d v2d4d(V4d a);
+V2d v2d4i(V4i a);
 V2d v2d_sum(V2d a, V2d b);
 V2d v2d_sub(V2d a, V2d b);
 V2d v2d_mul(V2d a, V2d b);
@@ -48,22 +72,36 @@ V2d v2d_ceil(V2d a);
 double v2d_sqrlen(V2d a);
 double v2d_len(V2d a);
 
-typedef struct { int x, y; } V2i;
 #define V2i_Fmt "v2i(%d, %d)"
 #define V2i_Arg(v) (v).x, (v).y
 V2i v2i(int x, int y);
 V2i v2ii(int x);
+V2i v2i2f(V2f a);
+V2i v2i2d(V2d a);
+V2i v2i3f(V3f a);
+V2i v2i3d(V3d a);
+V2i v2i3i(V3i a);
+V2i v2i4f(V4f a);
+V2i v2i4d(V4d a);
+V2i v2i4i(V4i a);
 V2i v2i_sum(V2i a, V2i b);
 V2i v2i_sub(V2i a, V2i b);
 V2i v2i_mul(V2i a, V2i b);
 V2i v2i_div(V2i a, V2i b);
 int v2i_sqrlen(V2i a);
 
-typedef struct { float x, y, z; } V3f;
 #define V3f_Fmt "v3f(%f, %f, %f)"
 #define V3f_Arg(v) (v).x, (v).y, (v).z
 V3f v3f(float x, float y, float z);
 V3f v3ff(float x);
+V3f v3f2f(V2f a);
+V3f v3f2d(V2d a);
+V3f v3f2i(V2i a);
+V3f v3f3d(V3d a);
+V3f v3f3i(V3i a);
+V3f v3f4f(V4f a);
+V3f v3f4d(V4d a);
+V3f v3f4i(V4i a);
 V3f v3f_sum(V3f a, V3f b);
 V3f v3f_sub(V3f a, V3f b);
 V3f v3f_mul(V3f a, V3f b);
@@ -80,11 +118,18 @@ V3f v3f_ceil(V3f a);
 float v3f_sqrlen(V3f a);
 float v3f_len(V3f a);
 
-typedef struct { double x, y, z; } V3d;
 #define V3d_Fmt "v3d(%lf, %lf, %lf)"
 #define V3d_Arg(v) (v).x, (v).y, (v).z
 V3d v3d(double x, double y, double z);
 V3d v3dd(double x);
+V3d v3d2f(V2f a);
+V3d v3d2d(V2d a);
+V3d v3d2i(V2i a);
+V3d v3d3f(V3f a);
+V3d v3d3i(V3i a);
+V3d v3d4f(V4f a);
+V3d v3d4d(V4d a);
+V3d v3d4i(V4i a);
 V3d v3d_sum(V3d a, V3d b);
 V3d v3d_sub(V3d a, V3d b);
 V3d v3d_mul(V3d a, V3d b);
@@ -101,22 +146,36 @@ V3d v3d_ceil(V3d a);
 double v3d_sqrlen(V3d a);
 double v3d_len(V3d a);
 
-typedef struct { int x, y, z; } V3i;
 #define V3i_Fmt "v3i(%d, %d, %d)"
 #define V3i_Arg(v) (v).x, (v).y, (v).z
 V3i v3i(int x, int y, int z);
 V3i v3ii(int x);
+V3i v3i2f(V2f a);
+V3i v3i2d(V2d a);
+V3i v3i2i(V2i a);
+V3i v3i3f(V3f a);
+V3i v3i3d(V3d a);
+V3i v3i4f(V4f a);
+V3i v3i4d(V4d a);
+V3i v3i4i(V4i a);
 V3i v3i_sum(V3i a, V3i b);
 V3i v3i_sub(V3i a, V3i b);
 V3i v3i_mul(V3i a, V3i b);
 V3i v3i_div(V3i a, V3i b);
 int v3i_sqrlen(V3i a);
 
-typedef struct { float x, y, z, w; } V4f;
 #define V4f_Fmt "v4f(%f, %f, %f, %f)"
 #define V4f_Arg(v) (v).x, (v).y, (v).z, (v).w
 V4f v4f(float x, float y, float z, float w);
 V4f v4ff(float x);
+V4f v4f2f(V2f a);
+V4f v4f2d(V2d a);
+V4f v4f2i(V2i a);
+V4f v4f3f(V3f a);
+V4f v4f3d(V3d a);
+V4f v4f3i(V3i a);
+V4f v4f4d(V4d a);
+V4f v4f4i(V4i a);
 V4f v4f_sum(V4f a, V4f b);
 V4f v4f_sub(V4f a, V4f b);
 V4f v4f_mul(V4f a, V4f b);
@@ -133,11 +192,18 @@ V4f v4f_ceil(V4f a);
 float v4f_sqrlen(V4f a);
 float v4f_len(V4f a);
 
-typedef struct { double x, y, z, w; } V4d;
 #define V4d_Fmt "v4d(%lf, %lf, %lf, %lf)"
 #define V4d_Arg(v) (v).x, (v).y, (v).z, (v).w
 V4d v4d(double x, double y, double z, double w);
 V4d v4dd(double x);
+V4d v4d2f(V2f a);
+V4d v4d2d(V2d a);
+V4d v4d2i(V2i a);
+V4d v4d3f(V3f a);
+V4d v4d3d(V3d a);
+V4d v4d3i(V3i a);
+V4d v4d4f(V4f a);
+V4d v4d4i(V4i a);
 V4d v4d_sum(V4d a, V4d b);
 V4d v4d_sub(V4d a, V4d b);
 V4d v4d_mul(V4d a, V4d b);
@@ -154,11 +220,18 @@ V4d v4d_ceil(V4d a);
 double v4d_sqrlen(V4d a);
 double v4d_len(V4d a);
 
-typedef struct { int x, y, z, w; } V4i;
 #define V4i_Fmt "v4i(%d, %d, %d, %d)"
 #define V4i_Arg(v) (v).x, (v).y, (v).z, (v).w
 V4i v4i(int x, int y, int z, int w);
 V4i v4ii(int x);
+V4i v4i2f(V2f a);
+V4i v4i2d(V2d a);
+V4i v4i2i(V2i a);
+V4i v4i3f(V3f a);
+V4i v4i3d(V3d a);
+V4i v4i3i(V3i a);
+V4i v4i4f(V4f a);
+V4i v4i4d(V4d a);
 V4i v4i_sum(V4i a, V4i b);
 V4i v4i_sub(V4i a, V4i b);
 V4i v4i_mul(V4i a, V4i b);
@@ -192,6 +265,70 @@ V2f v2ff(float x)
     return v2f(x, x);
 }
 
+V2f v2f2d(V2d a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f2i(V2i a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f3f(V3f a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f3d(V3d a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f3i(V3i a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f4f(V4f a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f4d(V4d a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
+V2f v2f4i(V4i a)
+{
+    V2f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    return result;
+}
+
 V2f v2f_sum(V2f a, V2f b)
 {
     a.x += b.x;
@@ -306,6 +443,70 @@ V2d v2dd(double x)
     return v2d(x, x);
 }
 
+V2d v2d2f(V2f a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d2i(V2i a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d3f(V3f a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d3d(V3d a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d3i(V3i a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d4f(V4f a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d4d(V4d a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
+V2d v2d4i(V4i a)
+{
+    V2d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    return result;
+}
+
 V2d v2d_sum(V2d a, V2d b)
 {
     a.x += b.x;
@@ -420,6 +621,70 @@ V2i v2ii(int x)
     return v2i(x, x);
 }
 
+V2i v2i2f(V2f a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i2d(V2d a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i3f(V3f a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i3d(V3d a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i3i(V3i a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i4f(V4f a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i4d(V4d a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
+V2i v2i4i(V4i a)
+{
+    V2i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    return result;
+}
+
 V2i v2i_sum(V2i a, V2i b)
 {
     a.x += b.x;
@@ -467,6 +732,78 @@ V3f v3ff(float x)
     return v3f(x, x, x);
 }
 
+V3f v3f2f(V2f a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = 0.0f;
+    return result;
+}
+
+V3f v3f2d(V2d a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = 0.0f;
+    return result;
+}
+
+V3f v3f2i(V2i a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = 0.0f;
+    return result;
+}
+
+V3f v3f3d(V3d a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    return result;
+}
+
+V3f v3f3i(V3i a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    return result;
+}
+
+V3f v3f4f(V4f a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    return result;
+}
+
+V3f v3f4d(V4d a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    return result;
+}
+
+V3f v3f4i(V4i a)
+{
+    V3f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    return result;
+}
+
 V3f v3f_sum(V3f a, V3f b)
 {
     a.x += b.x;
@@ -595,6 +932,78 @@ V3d v3dd(double x)
     return v3d(x, x, x);
 }
 
+V3d v3d2f(V2f a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = 0.0;
+    return result;
+}
+
+V3d v3d2d(V2d a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = 0.0;
+    return result;
+}
+
+V3d v3d2i(V2i a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = 0.0;
+    return result;
+}
+
+V3d v3d3f(V3f a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    return result;
+}
+
+V3d v3d3i(V3i a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    return result;
+}
+
+V3d v3d4f(V4f a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    return result;
+}
+
+V3d v3d4d(V4d a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    return result;
+}
+
+V3d v3d4i(V4i a)
+{
+    V3d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    return result;
+}
+
 V3d v3d_sum(V3d a, V3d b)
 {
     a.x += b.x;
@@ -723,6 +1132,78 @@ V3i v3ii(int x)
     return v3i(x, x, x);
 }
 
+V3i v3i2f(V2f a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = 0;
+    return result;
+}
+
+V3i v3i2d(V2d a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = 0;
+    return result;
+}
+
+V3i v3i2i(V2i a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = 0;
+    return result;
+}
+
+V3i v3i3f(V3f a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    return result;
+}
+
+V3i v3i3d(V3d a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    return result;
+}
+
+V3i v3i4f(V4f a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    return result;
+}
+
+V3i v3i4d(V4d a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    return result;
+}
+
+V3i v3i4i(V4i a)
+{
+    V3i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    return result;
+}
+
 V3i v3i_sum(V3i a, V3i b)
 {
     a.x += b.x;
@@ -775,6 +1256,86 @@ V4f v4ff(float x)
     return v4f(x, x, x, x);
 }
 
+V4f v4f2f(V2f a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = 0.0f;
+    result.w = 0.0f;
+    return result;
+}
+
+V4f v4f2d(V2d a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = 0.0f;
+    result.w = 0.0f;
+    return result;
+}
+
+V4f v4f2i(V2i a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = 0.0f;
+    result.w = 0.0f;
+    return result;
+}
+
+V4f v4f3f(V3f a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    result.w = 0.0f;
+    return result;
+}
+
+V4f v4f3d(V3d a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    result.w = 0.0f;
+    return result;
+}
+
+V4f v4f3i(V3i a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    result.w = 0.0f;
+    return result;
+}
+
+V4f v4f4d(V4d a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    result.w = (float) a.w;
+    return result;
+}
+
+V4f v4f4i(V4i a)
+{
+    V4f result;
+    result.x = (float) a.x;
+    result.y = (float) a.y;
+    result.z = (float) a.z;
+    result.w = (float) a.w;
+    return result;
+}
+
 V4f v4f_sum(V4f a, V4f b)
 {
     a.x += b.x;
@@ -917,6 +1478,86 @@ V4d v4dd(double x)
     return v4d(x, x, x, x);
 }
 
+V4d v4d2f(V2f a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = 0.0;
+    result.w = 0.0;
+    return result;
+}
+
+V4d v4d2d(V2d a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = 0.0;
+    result.w = 0.0;
+    return result;
+}
+
+V4d v4d2i(V2i a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = 0.0;
+    result.w = 0.0;
+    return result;
+}
+
+V4d v4d3f(V3f a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    result.w = 0.0;
+    return result;
+}
+
+V4d v4d3d(V3d a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    result.w = 0.0;
+    return result;
+}
+
+V4d v4d3i(V3i a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    result.w = 0.0;
+    return result;
+}
+
+V4d v4d4f(V4f a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    result.w = (double) a.w;
+    return result;
+}
+
+V4d v4d4i(V4i a)
+{
+    V4d result;
+    result.x = (double) a.x;
+    result.y = (double) a.y;
+    result.z = (double) a.z;
+    result.w = (double) a.w;
+    return result;
+}
+
 V4d v4d_sum(V4d a, V4d b)
 {
     a.x += b.x;
@@ -1059,6 +1700,86 @@ V4i v4ii(int x)
     return v4i(x, x, x, x);
 }
 
+V4i v4i2f(V2f a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = 0;
+    result.w = 0;
+    return result;
+}
+
+V4i v4i2d(V2d a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = 0;
+    result.w = 0;
+    return result;
+}
+
+V4i v4i2i(V2i a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = 0;
+    result.w = 0;
+    return result;
+}
+
+V4i v4i3f(V3f a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    result.w = 0;
+    return result;
+}
+
+V4i v4i3d(V3d a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    result.w = 0;
+    return result;
+}
+
+V4i v4i3i(V3i a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    result.w = 0;
+    return result;
+}
+
+V4i v4i4f(V4f a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    result.w = (int) a.w;
+    return result;
+}
+
+V4i v4i4d(V4d a)
+{
+    V4i result;
+    result.x = (int) a.x;
+    result.y = (int) a.y;
+    result.z = (int) a.z;
+    result.w = (int) a.w;
+    return result;
+}
+
 V4i v4i_sum(V4i a, V4i b)
 {
     a.x += b.x;

+ 66 - 4
lag.c

@@ -18,13 +18,14 @@ typedef struct {
     const char *name;
     const char *suffix;
     const char *fmt;
+    const char *zero_lit;
 } Type_Def;
 
 static_assert(COUNT_TYPES == 3, "The amount of type definitions have changed. Please update the array bellow accordingly");
 static Type_Def type_defs[COUNT_TYPES] = {
-    [TYPE_FLOAT]        = {.name = "float", .suffix = "f", .fmt = "f"},
-    [TYPE_DOUBLE]       = {.name = "double", .suffix = "d", .fmt = "lf"},
-    [TYPE_INT]          = {.name = "int", .suffix = "i", .fmt = "d"},
+    [TYPE_FLOAT]        = {.name = "float", .suffix = "f", .fmt = "f", .zero_lit = "0.0f"},
+    [TYPE_DOUBLE]       = {.name = "double", .suffix = "d", .fmt = "lf", .zero_lit = "0.0"},
+    [TYPE_INT]          = {.name = "int", .suffix = "i", .fmt = "d", .zero_lit = "0"},
 };
 
 typedef enum {
@@ -467,7 +468,48 @@ void gen_vector_len_impl(FILE *stream, size_t n, Type_Def type_def, const char *
     fprintf(stream, "}\n");
 }
 
-// TODO: conversions between the vector component types
+char *vector_convert_arg = "a";
+
+void gen_vector_convert_sig(FILE *stream,
+                            size_t dst_n, Type_Def dst_type_def,
+                            size_t src_n, Type_Def src_type_def)
+{
+    Short_String dst_type = make_vector_type(dst_n, dst_type_def);
+    Short_String src_type = make_vector_type(src_n, src_type_def);
+    Short_String name = shortf("v%zu%s%zu%s", dst_n, dst_type_def.suffix, src_n, src_type_def.suffix);
+    fprintf(stream, "%s %s(%s %s)", dst_type.cstr, name.cstr, src_type.cstr, vector_convert_arg);
+}
+
+void gen_vector_convert_decl(FILE *stream,
+                             size_t dst_n, Type_Def dst_type_def,
+                             size_t src_n, Type_Def src_type_def)
+{
+    gen_vector_convert_sig(stream, dst_n, dst_type_def, src_n, src_type_def);
+    fprintf(stream, ";\n");
+}
+
+void gen_vector_convert_impl(FILE *stream,
+                             size_t dst_n, Type_Def dst_type_def,
+                             size_t src_n, Type_Def src_type_def)
+{
+    Short_String dst_type = make_vector_type(dst_n, dst_type_def);
+
+    gen_vector_convert_sig(stream, dst_n, dst_type_def, src_n, src_type_def);
+    fprintf(stream, "\n");
+    fprintf(stream, "{\n");
+    fprintf(stream, "    %s result;\n", dst_type.cstr);
+    assert(dst_n <= VECTOR_MAX_SIZE);
+    for (size_t i = 0; i < dst_n; ++i) {
+        if (i < src_n) {
+            fprintf(stream, "    result.%s = (%s) %s.%s;\n", vector_comps[i], dst_type_def.name, vector_convert_arg, vector_comps[i]);
+        } else {
+            fprintf(stream, "    result.%s = %s;\n", vector_comps[i], dst_type_def.zero_lit);
+        }
+    }
+    fprintf(stream, "    return result;\n");
+    fprintf(stream, "}\n");
+}
+
 // TODO: matrices
 // TODO: documentation
 
@@ -488,9 +530,21 @@ int main()
         for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {
             for (Type type = 0; type < COUNT_TYPES; ++type) {
                 gen_vector_def(stream, n, type_defs[type]);
+            }
+        }
+        fprintf(stream, "\n");
+        for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {
+            for (Type type = 0; type < COUNT_TYPES; ++type) {
                 gen_vector_printf_macros(stream, n, type_defs[type]);
                 gen_vector_ctor_decl(stream, n, type_defs[type]);
                 gen_vector_scalar_ctor_decl(stream, n, type_defs[type]);
+                for (size_t src_n = VECTOR_MIN_SIZE; src_n <= VECTOR_MAX_SIZE; ++src_n) {
+                    for (Type src_type = 0; src_type < COUNT_TYPES; ++src_type) {
+                        if (src_n != n || src_type != type) {
+                            gen_vector_convert_decl(stream, n, type_defs[type], src_n, type_defs[src_type]);
+                        }
+                    }
+                }
                 for (Op_Type op = 0; op < COUNT_OPS; ++op) {
                     gen_vector_op_decl(stream, n, type_defs[type], op_defs[op]);
                 }
@@ -526,6 +580,14 @@ int main()
                 fputc('\n', stream);
                 gen_vector_scalar_ctor_impl(stream, n, type_defs[type]);
                 fputc('\n', stream);
+                for (size_t src_n = VECTOR_MIN_SIZE; src_n <= VECTOR_MAX_SIZE; ++src_n) {
+                    for (Type src_type = 0; src_type < COUNT_TYPES; ++src_type) {
+                        if (src_n != n || src_type != type) {
+                            gen_vector_convert_impl(stream, n, type_defs[type], src_n, type_defs[src_type]);
+                            fputc('\n', stream);
+                        }
+                    }
+                }
                 for (Op_Type op = 0; op < COUNT_OPS; ++op) {
                     gen_vector_op_impl(stream, n, type_defs[type], op_defs[op]);
                     fputc('\n', stream);