Skip to content

Instantly share code, notes, and snippets.

@khlorghaal
Last active January 3, 2019 23:59
Show Gist options
  • Save khlorghaal/a7214761c6c9f97b9c0c485931bf2d97 to your computer and use it in GitHub Desktop.
Save khlorghaal/a7214761c6c9f97b9c0c485931bf2d97 to your computer and use it in GitHub Desktop.
#define VECTORIZE_UNARY_FLOAT(f) \
vec2 f(vec2 a){ return vec2(f(a.x),f(a.y)); }
vec3 f(vec3 a){ return vec3(f(a.x),f(a.y),f(a.z)); }
vec4 f(vec4 a){ return vec4(f(a.x),f(a.y),f(a.z),f(a.w)); }
#define VECTORIZE_BINARY_FLOAT(f) \
vec2 f(vec2 a, vec2 b){ return vec2(f(a.x,b.x),f(a.y,b.y)); }
vec3 f(vec3 a, vec3 b){ return vec3(f(a.x,b.x),f(a.y,b.y),f(a.z,b.z)); }
vec4 f(vec4 a, vec4 b){ return vec4(f(a.x,b.x),f(a.y,b.y),f(a.z,b.z),f(a.w,b.w)); }
#define VECTORIZE_UNARY_INT(f) \
ivec2 f(ivec2 a){ return ivec2(f(a.x),f(a.y)); }
ivec3 f(ivec3 a){ return ivec3(f(a.x),f(a.y),f(a.z)); }
ivec4 f(ivec4 a){ return ivec4(f(a.x),f(a.y),f(a.z),f(a.w)); }
#define VECTORIZE_BINARY_INT(f) \
ivec2 f(ivec2 a, ivec2 b){ return ivec2(f(a.x,b.x),f(a.y,b.y)); }
ivec3 f(ivec3 a, ivec3 b){ return ivec3(f(a.x,b.x),f(a.y,b.y),f(a.z,b.z)); }
ivec4 f(ivec4 a, ivec4 b){ return ivec4(f(a.x,b.x),f(a.y,b.y),f(a.z,b.z),f(a.w,b.w)); }
//use
float accumulate(float x){ return acc+= x; }
VECTORIZE_UNARY_FLOAT(accumulate);
//An impure function is a weird but valid example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment