summaryrefslogtreecommitdiff
path: root/test/c/unroll.sail
blob: c68bb49dec89167966fa3673077aaf56dfc6c5c1 (plain)
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
default Order dec

$include <prelude.sail>

/* It's hard to test that this optimization does the right thing, but
we can at least test that it doesn't do the wrong thing. */

$optimize unroll 20
val fac : forall 'n, 'n >= 0. int('n) -> int
function fac(n) = {
  if n == 0 then {
    1
  } else {
    n * fac(n - 1)
  }
}

$optimize unroll 2
val fac2 : forall 'n, 'n >= 0. int('n) -> int
function fac2(n) = {
  if n == 0 then {
    1
  } else {
    n * fac2(n - 1)
  }
}

val "print_int" : (string, int) -> unit

function main((): unit) -> unit = {
  print_int("fac(4) = ", fac(4));
  print_int("fac(5) = ", fac(5));
  print_int("fac(6) = ", fac(6));
  print_int("fac2(4) = ", fac2(4));
  print_int("fac2(5) = ", fac2(5));
  print_int("fac2(6) = ", fac2(6))
}