1 /************************************************************************
2 * Program: scalar_product.c
3 * Function: Calculates the scalar product of vector lying in memory
4 * Used as a test for the simd optimization.
5 * Author: Andreas Schoesser
7 ************************************************************************/
14 float scalar_product(float *a, float *b, unsigned int max_elements);
19 int i, max_elements = 100;
22 // Allocate memory and make sure pointers are aligned to 16 byte addresses
23 char *a = malloc(16 + max_elements * sizeof(float));
24 char *b = malloc(16 + max_elements * sizeof(float));
26 char *ca = &a[0] + 16 - (unsigned int) ((unsigned int) &a[0] % 16);
27 char *cb = &b[0] + 16 - (unsigned int) ((unsigned int) &b[0] % 16);
29 float *aa = (float *) ca;
30 float *ab = (float *) cb;
34 printf("Scalar product\n==============\n\n");
36 //printf("Array Position: %u, %u, %u, %u\n", a, b, aa, ba/*(unsigned int) &aa[0] % 16, (unsigned int) &ba[0] % 16*/);
38 // Fill both arrays with random values
39 for(i = 0; i < max_elements; i++)
41 aa[i] = (float) (rand() % 10);
42 ab[i] = (float) (rand() % 10);
44 //printf("(%g * %g) + ", a[i], b[i]);
47 res = scalar_product(aa, ab, max_elements);
49 printf("\nResult: %g\n", res);
53 float scalar_product(float * a, float * b, unsigned int max_elements)
58 for(i = 0; i < max_elements; i += 4) {
60 res += a[i + 1] * b[i + 1];
61 res += a[i + 2] * b[i + 2];
62 res += a[i + 3] * b[i + 3];