Skip to content

Instantly share code, notes, and snippets.

@ynagatomo
Created June 1, 2022 04:29
Show Gist options
  • Save ynagatomo/785049721bc043fe12a783e77dff8a98 to your computer and use it in GitHub Desktop.
Save ynagatomo/785049721bc043fe12a783e77dff8a98 to your computer and use it in GitHub Desktop.
import simd
// Create vectors
vector_uchar2(0, 0) // typealias
simd_make_uchar4(255) // (Uint8)
simd_make_uchar4(1, 2, 3, 4) // (Uint8 x4)
vector4(1, 2, 3, 4) // (Uint8 x4)
simd_make_uchar4(simd_make_uchar3(1, 2, 3), 4)
simd_uchar(simd_int4(1, 2, 3, 4))
simd_uchar(simd_int4(255, 256, 257, -1))
simd_uchar_sat(simd_int4(255, 256, 257, -1))
simd_clamp(simd_uchar4(10, 20, 30, 40), simd_uchar4(repeating: 20), simd_uchar4(repeating: 30)) // x, min, max
simd_equal(simd_uchar4(1, 2, 3, 4), simd_uchar4(1, 2, 3, 4))
// Extrema Functions
simd_min(simd_uchar4(1, 2, 3, 4), simd_uchar4(4, 3, 2, 1))
simd_max(simd_uchar4(1, 2, 3, 4), simd_uchar4(4, 3, 2, 1))
// Logic and Bitwise Functions
simd_any(simd_uchar4(128, 0, 0, 0))
simd_all(simd_uchar4(128, 128, 128, 128))
simd_bitselect(simd_uchar4(1, 2, 3, 4), simd_uchar4(11, 12, 13, 14), simd_char4(-1, 0, -1, 0)) // x, y, mask
// init, methods, and operators From SIMD Vector Types
simd_uchar4(repeating: 0)
simd_uchar4.zero
simd_uchar4.random(in: 0...200)
simd_uchar4(1, 2, 3, 4) &* 2
simd_uchar4(1, 2, 3, 4) / 2
simd_uchar4(1, 2, 3, 4) &+ 10
simd_uchar4(1, 2, 3, 4) == simd_uchar4(1, 2, 3, 4)
simd_uchar4(1, 2, 3, 4) .== simd_uchar4(1, 2, 10, 20)
import simd
// Create floating-point vectors
vector_float2(0, 0) // typealias
simd_make_float2(1)
simd_make_float2(1, 2)
simd_make_float4(simd_make_float3(1, 2, 3), 4)
simd_make_float4_undef(1)
simd_float(simd_int4(1, 2, 3, 4))
// Common Functions
simd_abs(simd_float2(-1, -2))
simd_equal(simd_float2(1, 2), simd_float2(1, 3))
simd_clamp(simd_float2(-1, 2), simd_float2.zero, simd_float2.one) // x, min, max
simd_fract(simd_float2(1.2, 1.3)) // fractional part
simd_sign(simd_float2(-2.5, 2.5)) // sign
simd_step(simd_float2(10, 20), simd_float2(8, 21)) // edge, x (0: less than edge)
// Reduce Functions
simd_reduce_add(simd_float2(1, 2)) // sum of all elements
simd_reduce_max(simd_float2(-1, 1)) // max value of all elements
simd_reduce_min(simd_float2(-1, 1)) // min value of all elements
// Interpolation Functions
simd_mix(simd_float2.zero, simd_float2.one, simd_float2(0.3, 0.5)) // linear: x, y, t
simd_smoothstep(simd_float2.zero, simd_float2.one, simd_float2(0.1, 0.9)) // smooth: edge0, edge1, x
// Extrema Functions
simd_max(simd_float2(1, 2), simd_float2(3, 1))
simd_min(simd_float2(-1, -2), simd_float2(0, -3))
// Reciprocal and Reciprocal Square Root Functions
simd_recip(simd_float2(2, 3)) // reciprocal
simd_fast_recip(simd_float2(2, 3)) // reciprocal
simd_rsqrt(simd_float2(2, 3)) // reciprocal square root
simd_fast_rsqrt(simd_float2(2, 3)) // reciprocal square root
// Exponential and Logarithmic Functions
exp(simd_float2(1, 2)) // e raised to the power
exp2(simd_float2(1, 2)) // 2 raised to the power
exp10(simd_float2(-1, 2)) // 10 raised to the power
expm1(simd_float2(1, 2)) // e^x -1
log(simd_float2(1, 2)) // natural logarithm
log2(simd_float2(1, 256)) // base 2 logarithm
log10(simd_float2(1, 100)) // base 10 logarithm
log1p(simd_float2(0, 1)) // log(1+x)
// Geometry Functions
simd_cross(simd_float2(1, 0), simd_float2(0, 1)) // cross product
simd_dot(simd_float2(1, 2), simd_float2(3, 4)) // dot product
simd_incircle(simd_float2.zero, simd_float2(0, 1), simd_float2(1, 0), simd_float2(0, -1)) // x, a, b, c: tests if in circle
simd_incircle(simd_float2(-1, 0), simd_float2(0, 1), simd_float2(1, 0), simd_float2(0, -1)) // x, a, b, c
simd_incircle(simd_float2(2, 0), simd_float2(0, 1), simd_float2(1, 0), simd_float2(0, -1)) // x, a, b, c
simd_normalize(simd_float2(2, 3)) // normalizes
simd_orient(simd_float2(1, 0), simd_float2(0.5, 0.5)) // x, y: tests the orientation
simd_orient(simd_float2(1, 0), simd_float2(0.5, -0.5))
simd_project(simd_float2(0.5, 0.5), simd_float2(1, 0)) // 1st projected onto 2nd
simd_reflect(simd_float2(0.5, -0.5), simd_float2(0, 1)) // x, n (unit normal): reflection direction
simd_refract(simd_float2(0.5, -0.5), simd_float2(0, 1), 0.5) // x, n(unit normal), eta: refraction direction
// Vector Norm Functions
simd_norm_one(simd_float2(-2, 2)) // sum of absolute values
simd_norm_inf(simd_float2(-10, 2)) // max absolute value
// Length and Distance Functions
simd_length(simd_float2(-3, 4)) // length
simd_length_squared(simd_float2(-3, 4)) // square of the length
simd_distance(simd_float2.zero, simd_float2.one) // distance of the two vectors
simd_distance_squared(simd_float2.zero, simd_float2.one) // square of the distance
// Hyperbolic Functions
acosh(simd_float2(5, 3)) // inverse hyperbolic cosine
asinh(simd_float2(-5, 5)) // inverse hyperbolic sine
atanh(simd_float2(-0.1, 0.1)) // inverse hyperbolic tangent
cosh(simd_float2(-1, 1)) // hyperbolic cosine
sinh(simd_float2(-1, 1)) // hyperbolic sine
tanh(simd_float2(-10, 10)) // hyperbolic tangent
// Logic Functions
simd_select(simd_float2.zero, simd_float2.one, simd_int2(-1, 1)) // x, y, mask(high-order bit)
simd_bitselect(simd_float2.zero, simd_float2.one, simd_int2(-1, 1)) // x, y, mask
// Methods for SIMD Vector Type
simd_float2.one.addingProduct(simd_float2.one, 2) // a + b x c
simd_float2(2, 5).squareRoot()
simd_float2(2, 5).squareRoot().rounded(.towardZero)
simd_float2.random(in: 0...0.5)
simd_float2(1, 2) * simd_float2(3, 4)
simd_float2(1, 1) == simd_float2.one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment