Blame view

lectures/tools-code/t2/proftest.c 2.74 KB
fbc083c2   John Markus Bjørndalen   added today's lec...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  /* 
   * Compiles cleanly with gcc proftest.c
   * 
   * For profiling / debugging: gcc -pg proftest.c, run the program (a.out) and 
   * inspect the output with gprof a.out
   * 
   * Warnings: -Wall - what happens? 
   * 
   * Static checking: try splint proftest.c
   * 
   *
   * The source code is from a simple Mandelbrot benchmark / mandatory exercise in inf-3201.  
   * 
   * NB: this version has a bug. How do you compare it with the original? 
   */ 
  
  #include <stdio.h>
  
  int zooms = 10;
  
  const int WIDTH = 500; 
  const int HEIGHT = 600; 
  
  int crc = 0; 
  
  enum fake_cols {
      WHITE,
      BLUE, 
      CYAN, 
      BLACK, 
      GREEN, 
      MAGENTA, 
      ORANGE, 
      PINK, 
      RED, 
      YELLOW, 
      };
  
  
  int colortable[] = {
      WHITE, 
      WHITE, 
      BLUE, 
      CYAN, 
      BLACK, 
      GREEN, 
      MAGENTA, 
      ORANGE, 
      PINK, 
      RED,
      YELLOW
  };
  
  int colorcount = 10;
  
  double box_x_min, box_x_max, box_y_min, box_y_max;
  
  inline double translate_x(int x) {       
      return (((box_x_max - box_x_min) / WIDTH) * x) + box_y_min;
  }
  
  inline double translate_y(int y) {
      return (((box_y_max - box_y_min) / HEIGHT) * y) + box_x_min;
  }
  
  // Simple Mandelbrot divergation test
  inline int solve(double x, double y)              
  {                                                 
      double r = 0.0, s = 0.0;
      double next_r, next_s;
      int itt = 0;
    
      while ((r * r + s * s) <= 4.0) {
          next_r = r * r - s * s + x;
          next_s = 2 * r * s + y;
          r = next_r; s = next_s;
          if (++itt == 100)
              break;
      }
    
      return itt;
  }
    
  //Our 'main' function
  void CreateMap()
  {        
      int x, y, color;                               //Holds the result of slove
    
      for(y = 0; y < HEIGHT; y++)              //Main loop for map generation
          for(x = 0; x < WIDTH; x++) {  
              color = solve(translate_x(x), translate_y(y)) * colorcount / 100;
              crc += colortable[color];
          }
  }
  
  
  int RoadMap()
  {
      int i; 
      double deltaxmin, deltaymin, deltaxmax,deltaymax;
  
      box_x_min = -1.5; box_x_max = 0.5;           //Set the map bounding box for total map
      box_y_min = -1.0; box_y_max = 1.0;
      
      deltaxmin = (-0.9 - box_x_min) / zooms;
      deltaxmax = (-0.65 - box_x_max) / zooms;
      deltaymin = (-0.4 - box_y_min) / zooms;
      deltaymax = (-0.1 - box_y_max) / zooms;
      
      CreateMap();                     //Call our main
      for (i = 0; i < zooms; i++){
          box_x_min += deltaxmin;
          box_x_max += deltaxmax;
          box_y_min += deltaymin;
          box_y_max += deltaymax;
          CreateMap();                     //Call our main
      }                       
      
      return 0;
  }
  
  int main(){
      char buf[256];
  
      RoadMap();
    
      printf("NB: This version has a bug... but where?\n"); 
      printf("CRC is %x\n", crc);
  
      return 0; 
  }