Skip to content

Instantly share code, notes, and snippets.

@kravchik
Last active September 9, 2016 13:53
Show Gist options
  • Save kravchik/de8ac895fe73bfc98a717ab12fff905f to your computer and use it in GitHub Desktop.
Save kravchik/de8ac895fe73bfc98a717ab12fff905f to your computer and use it in GitHub Desktop.
First example of my optimizing code generator.
//Test of the automatic program optimization generator.
//Here is function multiply44 is deeply inlined and optimized.
//It uses just one call with constant parameters and it resolves to series of recursive inlines,
// constants calculations and other optimizations.
public static void set(int ww, float[] data, int row, int column, float d) {
data[(row-1) * ww + column-1] = d;
}
public static float get(int ww, float[] data, int row, int column) {
return data[(row-1) * ww + column-1];
}
//multiply matrices a and b of wxh, and store result in matrix ab
//all matrices are just arrays of float (requirements of this test)
public static void multiply(int w, int h, float[] ab, float[] a, float[] b) {
for(int i = 1;i <= w;i++){
for(int j = 1;j <= h;j++){
float r = 0;
for(int k = 1;k <= h;k++) r += get(w, a, i, k) * get(w, b, k, j);
set(w, ab, i, j, r);
}
}
}
public static void multiply44(float[] ab, float[] a, float[] b) {
multiply(4, 4, ab, a, b);
}
// Result of automatic optimization.
public static void multiply44(float[] ab, float[] a, float[] b) {
ab[0] = a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12];
ab[1] = a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13];
ab[2] = a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14];
ab[3] = a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15];
ab[4] = a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12];
ab[5] = a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13];
ab[6] = a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14];
ab[7] = a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15];
ab[8] = a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12];
ab[9] = a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13];
ab[10] = a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14];
ab[11] = a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15];
ab[12] = a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12];
ab[13] = a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13];
ab[14] = a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14];
ab[15] = a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment