-
Notifications
You must be signed in to change notification settings - Fork 2
/
mandelbrot.go
171 lines (126 loc) · 3.4 KB
/
mandelbrot.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//+build !ASSEMBLY
package blas
//go:generate govectool mandelbrot.go
import (
"github.com/sakjain92/govectool/govec"
"github.com/harrydb/go/img/pnm"
"os"
"log"
"image"
"image/color"
)
func mandel(c_re float32 , c_im float32 , count int) int32 {
var z_re float32
var z_im float32
z_re = c_re
z_im = c_im
var i int32
for i = 0; i < (int32)(count); i++ {
var new_re, new_im float32
if (z_re * z_re + z_im * z_im > 4.0) {
break
}
new_re = z_re*z_re - z_im*z_im
new_im = 2.0 * z_re * z_im
z_re = c_re + new_re
z_im = c_im + new_im
}
return i
}
func SerialMandelbrot(
x0 float32, y0 float32, x1 float32, y1 float32,
width int, height int,
startRow int, totalRows int,
maxIterations int,
output []int32) {
var dx, dy float32
var endRow, i, j int
dx = (x1 - x0) / float32(width)
dy = (y1 - y0) / float32(height)
endRow = startRow + totalRows
for j = startRow; j < endRow; j++ {
for i = 0; i < width; i++ {
var x, y float32
var index int
x = x0 + float32(i) * dx
y = y0 + float32(j) * dy
index = (j * width + i)
output[index] = mandel(x, y, maxIterations)
}
}
}
func __govecISPCMandel(c_re float32 , c_im float32 , count int) int32 {
var z_re float32
var z_im float32
z_re = c_re
z_im = c_im
var i int32
for i = 0; i < (int32)(count); i++ {
var new_re, new_im float32
if z_re * z_re + z_im * z_im > 4.0 {
break
}
new_re = z_re*z_re - z_im*z_im
new_im = 2.0 * z_re * z_im
z_re = c_re + new_re
z_im = c_im + new_im
}
return i
}
func _govecISPCMandelbrot(
x0 govec.UniformFloat32, y0 govec.UniformFloat32,
x1 govec.UniformFloat32, y1 govec.UniformFloat32,
width govec.UniformInt, height govec.UniformInt,
startRow govec.UniformInt, totalRows govec.UniformInt,
maxIterations govec.UniformInt,
output []govec.UniformInt32) {
var dx, dy float32
var i, j int
var endRow govec.UniformInt
dx = (float32)(x1 - x0) / (float32)(width)
dy = (float32)(y1 - y0) / (float32)(height)
endRow = startRow + totalRows
for govec.DoubleRange(j, startRow, endRow, i, 0, width) {
var x, y float32
var index int
x = (float32)(x0) + (float32)(i) * dx
y = (float32)(y0) + (float32)(j) * dy
index = (j * (int)(width) + i)
output[index] = (govec.UniformInt32)(__govecISPCMandel(x, y, (int)(maxIterations)))
}
}
type mandelImage struct {
output []int32
width int
height int
}
func (m mandelImage) ColorModel() color.Model {
return color.RGBAModel
}
func (m mandelImage) Bounds() image.Rectangle {
return image.Rect(0, 0, m.width, m.height)
}
func (m mandelImage) At(x, y int) color.Color {
var c color.RGBA
v := m.output[x + y * m.width]
c.R = uint8(v)
c.G = uint8(v)
c.B = uint8(v)
c.A = 1
return c
}
// Writes a mandelbrot image to .ppm file
func writeMandelBrotFile(output []int32, width int, height int, filename string) {
var m mandelImage
m.output = output
m.width = width
m.height = height
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
err = pnm.Encode(file, m, pnm.PPM)
if err != nil {
log.Fatal(err)
}
}