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
|
declare i32 @printf(i8*, ...)
declare void @putchar(i32)
@maxiter = internal constant i32 32
@lbound = internal constant double -2.0
@rbound = internal constant double 1.0
@tbound = internal constant double 1.5
@bbound = internal constant double -1.5
@hincr = internal constant double 0.03125
@vincr = internal constant double 0.0625
define internal i32 @mandeliter(double %x, double %y) {
entry:
br label %loopbody
loopbody:
%a = phi double [%x, %entry], [%na, %increment]
%b = phi double [%y, %entry], [%nb, %increment]
%n = phi i32 [0, %entry], [%nn, %increment]
%a2 = fmul double %a, %a
%b2 = fmul double %b, %b
%a2mb2 = fsub double %a2, %b2
%na = fadd double %a2mb2, %x
%ab = fmul double %a, %b
%ab2 = fadd double %ab, %ab
%nb = fadd double %ab2, %y
%norm = fadd double %a2, %b2
%cmp = fcmp ogt double %norm, 4.0
br i1 %cmp, label %afterloop, label %increment
increment:
%nn = add i32 %n, 1
%maxiter = load i32, i32* @maxiter
%icmp = icmp slt i32 %nn, %maxiter
br i1 %icmp, label %loopbody, label %afterloop
afterloop:
%retn = phi i32 [%n, %loopbody], [%nn, %increment]
ret i32 %retn
}
@num_formatstr = internal constant [4 x i8] c"%d \00"
define internal void @printnum32(i32 %n) {
entry:
%f = getelementptr [4 x i8], [4 x i8]* @num_formatstr, i64 0, i32 0
call i32(i8*, ...) @printf(i8* %f, i32 %n)
ret void
}
define internal void @printnum8(i8 %n) {
entry:
%n32 = sext i8 %n to i32
tail call void @printnum32(i32 %n32)
ret void
}
@shadestr = internal constant [10 x i8] c" .,-:!%@#\00"
define i32 @main() {
entry:
%lbound = load double, double* @lbound
%rbound = load double, double* @rbound
%tbound = load double, double* @tbound
%bbound = load double, double* @bbound
%hincr = load double, double* @hincr
%vincr = load double, double* @vincr
br label %yloop
yloop:
%y = phi double [%tbound, %entry], [%ny, %yloop2]
br label %xloop
xloop:
%x = phi double [%lbound, %yloop], [%nx, %xloop]
%n = call i32 @mandeliter(double %x, double %y)
%nL = mul i32 %n, 8
%maxiter = load i32, i32* @maxiter
%nLdM = sdiv i32 %nL, %maxiter
%shadechrp = getelementptr [10 x i8], [10 x i8]* @shadestr, i64 0, i32 %nLdM
%shadechr = load i8, i8* %shadechrp
%shadeint = sext i8 %shadechr to i32
call void @putchar(i32 %shadeint)
%nx = fadd double %x, %hincr
%xcmp = fcmp ole double %nx, %rbound
br i1 %xcmp, label %xloop, label %yloop2
yloop2:
%ny = fsub double %y, %vincr
call void @putchar(i32 10)
%ycmp = fcmp oge double %ny, %bbound
br i1 %ycmp, label %yloop, label %return
return:
ret i32 0
}
|