diff --git a/.gitignore b/.gitignore index 256cccf67..3587e3dac 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,8 @@ __pycache__ .vscode **/*.o -.DS_Store \ No newline at end of file +.DS_Store + +# vim swap files +*.swp +*.swo diff --git a/benchmarks/float/euler.bril b/benchmarks/float/euler.bril new file mode 100644 index 000000000..e50ea264a --- /dev/null +++ b/benchmarks/float/euler.bril @@ -0,0 +1,83 @@ +# Approximate Euler's number using the Taylor series + +# ARGS: 18 +@main(n: float) { + v0: float = id n; + e: float = call @taylor_series_euler v0; + e: float = id e; + v1: float = id e; + print v1; + v2: int = const 0; +} + +@factorial(n: float): float { + v1: float = id n; + v2: float = const 1; + v3: bool = fle v1 v2; + br v3 .then.0 .else.0; +.then.0: + v4: float = const 1; + ret v4; + jmp .endif.0; +.else.0: +.endif.0: + v5: float = id n; + v6: float = const 1; + v7: float = fsub v5 v6; + v8: float = call @factorial v7; + v9: float = id n; + v10: float = fmul v8 v9; + ret v10; +} + +@taylor_series_euler(n: float): float { + v0: float = const 0; + e: float = id v0; + v2: float = const 0; + i: float = id v2; +.for.cond.1: + v3: float = id i; + v4: float = id n; + v5: bool = flt v3 v4; + br v5 .for.body.1 .for.end.1; +.for.body.1: + v6: float = const 1; + v7: float = id i; + v8: float = call @factorial v7; + v9: float = fdiv v6 v8; + v10: float = id e; + v11: float = fadd v9 v10; + e: float = id v11; + v12: float = id i; + v13: float = const 1; + v14: float = fadd v12 v13; + i: float = id v14; + jmp .for.cond.1; +.for.end.1: + v15: float = id e; + ret v15; +} + +# The Typescript file that generated the above code: +# ts2bril euler.ts | bril2txt > euler.bril + +# function main(n: number) { +# var e: number = taylor_series_euler(n); +# console.log(e); +# } +# +# function factorial(n: number): number { +# if n <= 1 { +# return 1; +# } +# return factorial(n-1)*n; +# } +# +# function taylor_series_euler(n: number): number { +# var e: number = 0; +# var i: number; +# for(let i = 0; i < n; i=i+1) { +# e = 1 / factorial(i) + e; +# } +# return e; +# } diff --git a/benchmarks/float/euler.out b/benchmarks/float/euler.out new file mode 100644 index 000000000..fe0cc23e6 --- /dev/null +++ b/benchmarks/float/euler.out @@ -0,0 +1 @@ +2.71828182845904553 diff --git a/benchmarks/float/euler.prof b/benchmarks/float/euler.prof new file mode 100644 index 000000000..569421c3a --- /dev/null +++ b/benchmarks/float/euler.prof @@ -0,0 +1 @@ +total_dyn_inst: 1908 diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 4cbcf3906..d08e138c6 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -22,6 +22,7 @@ The current benchmarks are: * `digial-root`: Computes the digital root of the input number. * `eight-queens`: Counts the number of solutions for *n* queens problem, a generalization of [Eight queens puzzle][eight_queens]. * `euclid`: Calculates the greatest common divisor between two large numbers using the [Euclidean Algorithm][euclid] with a helper function for the modulo operator. +* `euler`: Approximates [Euler's number][euler] using the Taylor series. * `fact`: Prints the factorial of *n*, computing it recursively. * `factors`: Print the factors of the *n* using the [trial division][trialdivision] method. * `fib`: Calculate the *n*th Fibonacci number by allocating and filling an [array](../lang/memory.md) of numbers up to that point. @@ -78,3 +79,4 @@ Credit for several of these benchmarks goes to Alexa VanHattum and Gregory Yaune [uparrow]: https://en.wikipedia.org/wiki/Knuth%27s_up-arrow_notation [riemann]: https://en.wikipedia.org/wiki/Riemann_sum [mandelbrot]: https://en.wikipedia.org/wiki/Mandelbrot_set +[euler]: https://en.wikipedia.org/wiki/E_(mathematical_constant)