Guest User

Scalar average

a guest
Jul 9th, 2015
226
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/time.h>
  2. #include <sys/param.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <xmmintrin.h>
  7.  
  8. #define N 400000
  9.  
  10. double gettime ()
  11. {
  12.     struct timeval tv;
  13.     gettimeofday (&tv, 0);
  14.     return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
  15. }
  16.  
  17. float calc_avg1 (const float array[], int n)
  18. {
  19.     int i;
  20.     float avg = 0;
  21.     for (i = 0; i < n; i++) avg += array[i];
  22.     return avg / n;
  23. }
  24.  
  25. float calc_avg2 (const float array[], int n)
  26. {
  27.     int i;
  28.     float avg = 0;
  29.     for (i = 0; i < n; i+=4)
  30.     {
  31.         avg += array[i+0];
  32.         avg += array[i+1];
  33.         avg += array[i+2];
  34.         avg += array[i+3];
  35.     }
  36.     return avg / n;
  37. }
  38.  
  39. float calc_avg3 (const float array[], int n)
  40. {
  41.     int i;
  42.     __v4sf r = _mm_set1_ps (0.0);
  43.     for (i=0; i<n; i+=4) r += _mm_load_ps (&(array[i]));
  44.     /*r = _mm_hadd_ps (r, r);
  45.       r = _mm_hadd_ps (r, r);
  46.       return r[0] / n;*/
  47.     return (r[0] + r[1] + r[2] + r[3]) / n;
  48. }
  49.  
  50. int main ()
  51. {
  52.     void *space = malloc (N*sizeof(float) + 15);
  53.     float *array = (float*)(((unsigned long)space + 15) & ~(unsigned long)15);
  54.     int i;
  55.     double time;
  56.     float avg;
  57.  
  58.     for (i = 0; i < N; i++) array[i] = 1.0 * random();
  59.  
  60.     time = gettime();
  61.     avg = calc_avg1 (array, N);
  62.     time = gettime() - time;
  63.     printf ("%f %f\n", time, avg);
  64.  
  65.     time = gettime();
  66.     avg = calc_avg2 (array, N);
  67.     time = gettime() - time;
  68.     printf ("%f %f\n", time, avg);
  69.  
  70.     time = gettime();
  71.     avg = calc_avg3 (array, N);
  72.     time = gettime() - time;
  73.     printf ("%f %f\n", time, avg);
  74.  
  75.     float a[4] __attribute__((aligned(16))) = {1,2,3,4};
  76.     avg = calc_avg3 (a, 4);
  77.     printf ("%f\n", avg);
  78.     return 0;
  79. }
RAW Paste Data