/*   This is the tracing version of the AB benchmark, for use in 
 *   Lab 2 of CS&E 141L
 */

#include<stdio.h>

#define READ  0
#define WRITE 1

void printref(unsigned long ref, int r_w)
{
  char hex[]="0123456789ABCDEF";
  int i;
  if(r_w==READ) printf("R"); else printf("W");
  for(i=7;i>=0;i--)
    printf("%c",hex[(ref >> i*4) & 0xF]);
  printf("\n");
}

int main(argc, argv)
int argc; char *argv[];
{
   long i, j, k, lgn, n, r, reps;
   float *a, t1, t2; 

   /* This version uses a standard problem size. */

   lgn = 10;
   reps = 1;

   /* We've eliminated the printf's so the program output
      doesn't get confused with the trace output */

   /* Compute n = 2^lgn */
   n = 1;
   for (i=0; i<lgn; i++) n = 2*n;

   /* Allocate storage and set a few non-zeroes */
   a = (float *) calloc(n, sizeof(float));
   if (a == NULL) {
      fprintf (stderr, "calloc failed. Try a smaller size problem\n");
      exit(1);
   } 
   /* This records the writes done by calloc in initializing a */
    for (i=0; i<n; i++)
      printref(i*4,WRITE);

    for (r=0; r<reps; r++) {
      for (i=0; i<n; i += 100) {
        printref(i*4,WRITE);
        a[i] = i * 0.123;
      }

       /* Now the real work begins */
       for (i=1; i<n; i=i*2) {
           for (j=0; j<i; j++) {
               for (k=j; k<n; k+=2*i) {
                   t1 = a[k];
                   t2 = a[k+i];
                   a[k] = t1 + t2*.007;
                   a[k+i] = t1 - t2*.003;
                   printref(k*4,READ);
                   printref((k+i)*4,READ);
                   printref(k*4,WRITE);
                   printref((k+i)*4,WRITE);
               }
           }
       }
   }

   return (0);
}
	
