PandA-2024.02
mandelbrot.c
Go to the documentation of this file.
1 // Mandelbrot kernel
2 // Author: Andrew Canis
3 // Date: July 1, 2012
4 
5 #include <stdio.h>
6 
7 #define DECIMAL_PLACES 28
8 #define int2fixed(num) ((num) << DECIMAL_PLACES)
9 #define fixedmul(a, b) ((((long long)a) * ((long long)b)) >> DECIMAL_PLACES)
10 #define fixed2int(num) ((num) >> DECIMAL_PLACES)
11 
12 #define WIDTH 128
13 #define HEIGHT 128
14 //#define MAX_ITER 10
15 //#define MAX_ITER 2
16 #define MAX_ITER 50
17 
18 #define LEGUP 1
19 
20 volatile unsigned char img[WIDTH][HEIGHT];
21 
22 int mandelbrot() {
23  int count = 0;
24 #pragma omp parallel for reduction(+:count)
25  for (int j = 0; j < HEIGHT; j++) {
26  for (int i = 0; i < WIDTH; i++) {
27 
28  int x_0 = int2fixed(-2) + ((((3 << 20) * i/WIDTH) ) << 8);
29  int y_0 = int2fixed(-1) + ((((2 << 20) * j/HEIGHT) ) << 8);
30 
31  int x = 0;
32  int y = 0;
33  int xtmp;
34  unsigned char fiter = 0;
35 
36  for (int iter = 0; iter < MAX_ITER; iter++) {
37  long long abs_squared = fixedmul(x,x) + fixedmul(y,y);
38 
39  xtmp = fixedmul(x,x) - fixedmul(y,y) + x_0;
40  y = fixedmul(int2fixed(2), fixedmul(x,y)) + y_0;
41  x = xtmp;
42  fiter += abs_squared <= int2fixed(4);
43  }
44 
45  //get black or white
46  unsigned char colour = (fiter >= MAX_ITER) ? 0 : 1;
47  //accumulate colour
48  count += colour;
49  //update image
50  img[i][j] = colour;
51  }
52  }
53  return count;
54 }
55 
56 
57 int main() {
58  int count = mandelbrot();
59  int res = 0;
60 
61  if (count == 12013) {
62  printf("PASS\n");
63  } else {
64  printf("FAIL\n");
65  res = 1;
66  }
67 
68  return res;
69 }
volatile unsigned char img[WIDTH][HEIGHT]
Definition: mandelbrot.c:20
#define fixedmul(a, b)
Definition: mandelbrot.c:9
int main()
Definition: mandelbrot.c:57
#define int2fixed(num)
Definition: mandelbrot.c:8
#define MAX_ITER
Definition: mandelbrot.c:16
#define WIDTH
Definition: mandelbrot.c:12
int mandelbrot()
Definition: mandelbrot.c:22
x
Return the smallest n such that 2^n >= _x.
#define HEIGHT
Definition: mandelbrot.c:13

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13