-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmandelbrot.c
87 lines (74 loc) · 1.76 KB
/
mandelbrot.c
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
#include "bench.h"
void
mandelbrot_scalar_f32(size_t width, size_t maxIter, uint32_t *res)
{
for (size_t y = 0; y < width; ++y)
for (size_t x = 0; x < width; ++x) {
float cx = x * 2.0f / width - 1.5;
float cy = y * 2.0f / width - 1;
size_t iter = 0;
float zx = 0, zy = 0, zxS = 0, zyS = 0;
BENCH_VOLATILE_REG(cy);
while (zxS + zyS <= 4 && iter < maxIter) {
zxS = zxS - zyS + cx;
zy = 2 * zx * zy + cy;
zx = zxS;
zxS = zx*zx;
zyS = zy*zy;
++iter;
}
*res++ = iter;
}
}
#if __riscv_flen == 64
void
mandelbrot_scalar_f64(size_t width, size_t maxIter, uint32_t *res)
{
for (size_t y = 0; y < width; ++y)
for (size_t x = 0; x < width; ++x) {
double cx = x * 2.0 / width - 1.5;
double cy = y * 2.0 / width - 1;
size_t iter = 0;
double zx = 0, zy = 0, zxS = 0, zyS = 0;
BENCH_VOLATILE_REG(cy);
while (zxS + zyS <= 4 && iter < maxIter) {
zxS = zxS - zyS + cx;
zy = 2 * zx * zy + cy;
zx = zxS;
zxS = zx*zx;
zyS = zy*zy;
++iter;
}
*res++ = iter;
}
}
#endif
#define IMPLS(f) \
f(scalar_f32) \
IF_F64(f(scalar_f64)) \
IF_VF16(f(rvv_f16_m1)) \
IF_VF16(f(rvv_f16_m2)) \
f(rvv_f32_m1) \
f(rvv_f32_m2) \
IF_VF64(f(rvv_f64_m1)) \
IF_VF64(f(rvv_f64_m2)) \
typedef void Func(size_t width, size_t maxIter, uint32_t *res);
#define DECLARE(f) extern Func mandelbrot_##f;
IMPLS(DECLARE)
#define EXTRACT(f) { #f, &mandelbrot_##f },
Impl impls[] = { IMPLS(EXTRACT) };
void init(void) { }
/* disabled, because of rounding errors, please independently verify */
ux checksum(size_t n) { return 0; }
BENCH_BEG(base) {
n = usqrt(n);
TIME f(n, mandelbrot_ITER, (uint32_t*)mem);
} BENCH_END
Bench benches[] = {
BENCH(
impls,
SCALE_mandelbrot(MAX_MEM / 4),
"mandelbrot "STR(mandelbrot_ITER),
bench_base
)
}; BENCH_MAIN(benches)