1 /************************************************************************
2 * Program: vadd_store.c
3 * Function: Add 2 vectors (lying in memory) and store the result in
4 * a another vector in memory.
5 * Used as a test for the simd optimization.
6 * Author: Andreas Schoesser
8 ************************************************************************/
17 /*Also possible: Local pointers instead of function parameters: */
18 //int *a = (int *) alloca(8), *b = (int *) alloca(8), *c = (int *) alloca(8);
22 printf("1. vload -> vadd -> vstore\n===================\n\n");
24 for(i = 0; i < 2; i++)
31 /* Also possible: Local arrays, but may be optimized so that no pattern is found
32 int a[2], b[2], c[2]; */
35 /* Version 1: Directly adding. */
42 /* Version 2: Adding of local variables
44 int a0, a1, b0, b1, c0, c1;
59 /* Version 3: Mixed adding of locals and pointers
71 for(i = 0; i < 2; i++)
72 printf("%d + %d = %d\n", a[i], b[i], c[i]);
81 /* Version 4: Manually unrolled loop */
84 int d[5][5], e[5][5], f[5][5];
86 //printf("2. vload -> vadd -> vstore, multi dimensional array, in loop\n==========================================\n\n");
88 for(j = 0; j < 4; j++)
89 for(i = 0; i < 2; i++)
91 e[j][i] = rand() % 10;
92 f[j][i] = rand() % 10;
95 for(j = 0; j < 4; j++)
96 //for(i = 0; i < 2; i ++ )
98 d[j][0] = e[j][0] + f[j][0];
99 d[j][1] = e[j][1] + f[j][1];
102 for(j = 0; j < 4; j++)
104 for(i = 0; i < 2; i++)
105 printf("%d + %d = %d\n", e[j][i], f[j][i], d[j][i]);