Skip to content

Commit 175580e

Browse files
authored
Merge pull request #411 from ethanuppal/add-inv-sqrt-benchmark
feat(benchmarks): Add fast inverse square root benchmark
2 parents 9376051 + 0bfb490 commit 175580e

4 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Implementation based off of: https://stackoverflow.com/questions/11644441/fast-inverse-square-root-on-x64
2+
# double invsqrtQuake( double number )
3+
# {
4+
# double y = number;
5+
# double x2 = y * 0.5;
6+
# std::int64_t i = *(std::int64_t *) &y;
7+
# // The magic number is for doubles is from https://cs.uwaterloo.ca/~m32rober/rsqrt.pdf
8+
# i = 0x5fe6eb50c7b537a9 - (i >> 1);
9+
# y = *(double *) &i;
10+
# y = y * (1.5 - (x2 * y * y)); // 1st iteration
11+
# // y = y * ( 1.5 - ( x2 * y * y ) ); // 2nd iteration, this can be removed
12+
# return y;
13+
# }
14+
# Don't @ me about code style; I took the above code verbatim
15+
16+
# ARGS: 2
17+
# this then computes 1/sqrt(2) approximately, so it should be around 0.7 since
18+
# sqrt(2) is 1.14
19+
@main(y: float) {
20+
one_half: float = const 0.5;
21+
one: int = const 1;
22+
one_and_half: float = const 1.5;
23+
magic: int = const 6910469410427058089;
24+
25+
x2: float = fmul y one_half;
26+
i: int = float2bits y;
27+
i2: int = call @RIGHTSHIFT i one;
28+
i3: int = sub magic i2;
29+
y2: float = bits2float i3;
30+
31+
y3: float = fmul y2 y2;
32+
y4: float = fmul x2 y3;
33+
y5: float = fsub one_and_half y4;
34+
y6: float = fmul y2 y5;
35+
36+
print y6;
37+
}
38+
39+
# from bitshift.bril:
40+
41+
@pow(x: int, n: int): int {
42+
v1: int = id n;
43+
v2: int = const 1;
44+
v3: bool = eq v1 v2;
45+
br v3 .then.0 .else.0;
46+
.then.0:
47+
v4: int = id x;
48+
ret v4;
49+
.else.0:
50+
v5: int = id x;
51+
v6: int = id n;
52+
v7: int = const 2;
53+
v8: int = div v6 v7;
54+
half: int = call @pow v5 v8;
55+
half: int = id half;
56+
v9: int = id half;
57+
v10: int = id half;
58+
v11: int = mul v9 v10;
59+
half2: int = id v11;
60+
v13: int = id n;
61+
v14: int = const 2;
62+
v15: int = call @mod v13 v14;
63+
v16: int = const 1;
64+
v17: bool = eq v15 v16;
65+
br v17 .then.12 .else.12;
66+
.then.12:
67+
v18: int = id half2;
68+
v19: int = id x;
69+
v20: int = mul v18 v19;
70+
ans: int = id v20;
71+
jmp .endif.12;
72+
.else.12:
73+
v21: int = id half2;
74+
ans: int = id v21;
75+
.endif.12:
76+
v22: int = id ans;
77+
ret v22;
78+
}
79+
@mod(a: int, b: int): int {
80+
v0: int = id a;
81+
v1: int = id a;
82+
v2: int = id b;
83+
v3: int = div v1 v2;
84+
v4: int = id b;
85+
v5: int = mul v3 v4;
86+
v6: int = sub v0 v5;
87+
ret v6;
88+
}
89+
@RIGHTSHIFT(x: int, step: int): int {
90+
v0: int = const 2;
91+
v1: int = id step;
92+
p: int = call @pow v0 v1;
93+
p: int = id p;
94+
v2: int = id x;
95+
v3: int = id p;
96+
v4: int = div v2 v3;
97+
ret v4;
98+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.70692965079546399
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
total_dyn_inst: 28

brilirs/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BENCHMARKS := ../benchmarks/core/*.bril \
1414
../benchmarks/float/*.bril \
1515
../benchmarks/mem/*.bril \
1616
../benchmarks/mixed/*.bril \
17+
../benchmarks/mixed/brilirs-only/*.bril \
1718

1819
LONGBENCHMARKS := ../benchmarks/long/*.bril \
1920
../benchmarks/core/ackermann.bril \

0 commit comments

Comments
 (0)