Skip to content

Instantly share code, notes, and snippets.

@rikusalminen
rikusalminen / interleaver.py
Last active December 12, 2015 03:18
Triangle mesh magic in python
cube_vertices = [
( 1.0, 1.0, 1.0),
( -1.0, 1.0, 1.0),
( 1.0, -1.0, 1.0),
( -1.0, -1.0, 1.0),
( 1.0, 1.0, -1.0),
( -1.0, 1.0, -1.0),
( 1.0, -1.0, -1.0),
( -1.0, -1.0, -1.0),
]
@rikusalminen
rikusalminen / trampoline.ll
Created January 31, 2013 11:55
LLVM trampoline fun
declare void @llvm.init.trampoline(i8*, i8*, i8*);
declare i8* @llvm.adjust.trampoline(i8*);
define i32 @foo(i32* nest %ptr, i32 %val)
{
%x = load i32* %ptr
%sum = add i32 %x, %val
ret i32 %sum
}
@rikusalminen
rikusalminen / gist:4655394
Created January 28, 2013 13:11
Makefile experiments
CC=gcc
CFLAGS=-MMD
CFLAGS+=-W -Wall
CFLAGS+=-g -ggdb
LDLIBS=-lm
TARGETS=main bar/libbar.a
SRCS=main.c bar/bar.c baz/baz.c
#main: CFLAGS+=-DFOOBARZ
@rikusalminen
rikusalminen / whitetriangle.c
Created September 1, 2012 15:31
A white triangle
#include <assert.h>
#include <stdio.h>
#include <GL3/gl3w.h>
#include <GL/glfw3.h>
#include <GL3/gl3.h>
static GLuint vbo;
static GLuint vao;
@rikusalminen
rikusalminen / main.c
Created July 31, 2012 08:16
glfw bare bones
#include <stdio.h>
#include <GL3/gl3w.h>
#include <GL/glfw3.h>
#include <GL3/gl3.h>
int main_loop(GLFWwindow window)
{
while(!glfwGetKey(window, GLFW_KEY_ESCAPE))
{
@rikusalminen
rikusalminen / dot.c
Created July 3, 2012 14:55
SIMD dot products: ARM NEON, SSE3, SSE
#if defined(__ARM_NEON__)
vec4 dot(vec4 a, vec4 b)
{
vec4 prod = vmulq_f32(a, b);
vec4 sum1 = vaddq_f32(prod, vrev64q_f32(prod));
vec4 sum2 = vaddq_f32(sum1, vcombine_f32(vget_high_f32(sum1), vget_low_f32(sum1)));
return sum2;
}
#else if defined(__SSE3__)
static inline vec4 vdot(vec4 x, vec4 y)
@rikusalminen
rikusalminen / gist:3023343
Created June 30, 2012 10:35
SIMD quaternion product
static inline vec4 qprod(vec4 x, vec4 y)
{
vec4 negative = _mm_set_ss(-0.0);
return
vshuffle(x, x, 3, 3, 3, 3) * y +
_mm_xor_ps(vshuffle(negative, negative, 1, 1, 1, 0),
vshuffle(x, x, 0, 1, 2, 0) * vshuffle(y, y, 3, 3, 3, 0) +
vshuffle(x, x, 1, 2, 0, 1) * vshuffle(y, y, 2, 0, 1, 1)) -
vshuffle(x, x, 2, 0, 1, 2) * vshuffle(y, y, 1, 2, 0, 2);
}
@rikusalminen
rikusalminen / gist:2996438
Created June 26, 2012 15:29
This memory access is unwanted
vec4 qprod(vec4 x, vec4 y)
{
return
vshuffle(x, x, 3, 3, 3, 3) * y +
(vec(1.0, 1.0, 1.0, -1.0) *
(vshuffle(x, x, 0, 1, 2, 0) * vshuffle(y, y, 3, 3, 3, 0) +
vshuffle(x, x, 1, 2, 0, 1) * vshuffle(y, y, 2, 0, 1, 1))) -
vshuffle(x, x, 2, 0, 1, 2) * vshuffle(y, y, 1, 2, 0, 2);
}
@rikusalminen
rikusalminen / simd.c
Created June 26, 2012 07:48
Some 3D math with x86 SIMD: SSE, AVX, FMA, et al
#include <stdint.h>
#include <x86intrin.h>
#include <stdio.h>
void printv(__m128 vec)
{
float x[4];
@rikusalminen
rikusalminen / shader.frag
Created June 13, 2012 11:08
Simple GLSL lighting
#version 150
layout(std140) uniform Camera
{
mat4 view_matrix;
mat4 projection_matrix;
} camera;
layout(std140) uniform Light
{