-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriemann.bril
89 lines (84 loc) · 2.36 KB
/
riemann.bril
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
# riemann sums from wikipedia article on riemann sums
@main {
a: float = const 2.0;
b: float = const 10.0;
n: float = const 8.0;
left : float = call @left_riemann a b n;
print left;
midpoint: float = call @midpoint_riemann a b n;
print midpoint;
right : float = call @right_riemann a b n;
print right;
}
@square_function(x: float): float {
square : float = fmul x x;
ret square;
}
@left_riemann(a: float, b:float, n:float): float {
zero : float = const 0.0;
one : float = const 1.0;
negative_one : float = fsub zero one;
diff : float = fsub b a;
delta : float = fdiv diff n;
i : float = fsub n one;
sum : float = const 0.0;
.while.header:
cond : bool = feq i negative_one;
br cond .while.end .while.body;
.while.body:
offset : float = fmul delta i;
x : float = fadd a offset;
f_x : float = call @square_function x;
sum : float = fadd sum f_x;
i : float = fsub i one;
jmp .while.header;
.while.end:
sum : float = fmul sum delta;
ret sum;
}
@right_riemann(a: float, b:float, n:float): float {
zero : float = const 0.0;
one : float = const 1.0;
diff : float = fsub b a;
delta : float = fdiv diff n;
i : float = id n;
sum : float = const 0.0;
.while.header:
cond : bool = feq i zero;
br cond .while.end .while.body;
.while.body:
offset : float = fmul delta i;
x : float = fadd a offset;
f_x : float = call @square_function x;
sum : float = fadd sum f_x;
i : float = fsub i one;
jmp .while.header;
.while.end:
sum : float = fmul sum delta;
ret sum;
}
@midpoint_riemann(a: float, b:float, n:float): float {
zero : float = const 0.0;
one : float = const 1.0;
negative_one : float = fsub zero one;
two : float = const 2.0;
diff : float = fsub b a;
delta : float = fdiv diff n;
i : float = fsub n one;
sum : float = const 0.0;
.while.header:
cond : bool = feq i negative_one;
br cond .while.end .while.body;
.while.body:
offset : float = fmul delta i;
half_delta : float = fdiv delta two;
offset : float = fadd offset half_delta;
x : float = fadd a offset;
f_x : float = call @square_function x;
sum : float = fadd sum f_x;
i : float = fsub i one;
jmp .while.header;
.while.end:
sum : float = fmul sum delta;
ret sum;
}