03_struct.c (735B)
1 /* Aggregates: struct-by-value args/returns, arrays, pointer walks. 2 * Returns a value in [0,127]. */ 3 struct vec3 { 4 long x, y, z; 5 }; 6 7 static struct vec3 add3(struct vec3 a, struct vec3 b) { 8 struct vec3 r = {a.x + b.x, a.y + b.y, a.z + b.z}; 9 return r; 10 } 11 12 static long dot(struct vec3 a, struct vec3 b) { 13 return a.x * b.x + a.y * b.y + a.z * b.z; 14 } 15 16 int bounce_main(void) { 17 struct vec3 acc = {0, 0, 0}; 18 struct vec3 step = {1, 2, 3}; 19 for (int i = 0; i < 16; ++i) { 20 acc = add3(acc, step); 21 step.x += i; 22 step.z -= 1; 23 } 24 long arr[8]; 25 for (int i = 0; i < 8; ++i) arr[i] = acc.x + i * acc.y - acc.z; 26 long s = 0; 27 for (long* p = arr; p < arr + 8; ++p) s += *p; 28 s += dot(acc, step); 29 return (int)(s & 0x7f); 30 }