|
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift.org open source project
|
4 | 4 | //
|
5 |
| -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception
|
7 | 7 | //
|
8 | 8 | // See https://swift.org/LICENSE.txt for license information
|
|
14 | 14 | // for performance measuring.
|
15 | 15 | import TestsUtils
|
16 | 16 |
|
17 |
| -public let Ackermann = BenchmarkInfo( |
18 |
| - name: "Ackermann", |
19 |
| - runFunction: run_Ackermann, |
20 |
| - tags: [.algorithm]) |
| 17 | +public let benchmarks = |
| 18 | + BenchmarkInfo( |
| 19 | + name: "Ackermann", |
| 20 | + runFunction: run_Ackermann, |
| 21 | + tags: [.algorithm]) |
21 | 22 |
|
22 |
| -func ackermann(_ M: Int, _ N : Int) -> Int { |
23 |
| - if (M == 0) { return N + 1 } |
24 |
| - if (N == 0) { return ackermann(M - 1, 1) } |
25 |
| - return ackermann(M - 1, ackermann(M, N - 1)) |
| 23 | +func _ackermann(_ m: Int, _ n : Int) -> Int { |
| 24 | + if (m == 0) { return n + 1 } |
| 25 | + if (n == 0) { return _ackermann(m - 1, 1) } |
| 26 | + return _ackermann(m - 1, _ackermann(m, n - 1)) |
26 | 27 | }
|
27 | 28 |
|
28 | 29 | @inline(never)
|
29 |
| -func Ackermann(_ M: Int, _ N : Int) -> Int { |
| 30 | +func ackermann(_ m: Int, _ n : Int) -> Int { |
30 | 31 | // This if prevents optimizer from computing return value of Ackermann(3,9)
|
31 | 32 | // at compile time.
|
32 |
| - if False() { return 0 } |
33 |
| - if (M == 0) { return N + 1 } |
34 |
| - if (N == 0) { return ackermann(M - 1, 1) } |
35 |
| - return ackermann(M - 1, ackermann(M, N - 1)) |
| 33 | + if getFalse() { return 0 } |
| 34 | + if (m == 0) { return n + 1 } |
| 35 | + if (n == 0) { return _ackermann(m - 1, 1) } |
| 36 | + return _ackermann(m - 1, _ackermann(m, n - 1)) |
36 | 37 | }
|
37 | 38 |
|
38 | 39 | let ref_result = [5, 13, 29, 61, 125, 253, 509, 1021, 2045, 4093, 8189, 16381, 32765, 65533, 131069]
|
39 | 40 |
|
40 | 41 | @inline(never)
|
41 |
| -public func run_Ackermann(_ N: Int) { |
| 42 | +public func run_Ackermann(_ n: Int) { |
42 | 43 | let (m, n) = (3, 6)
|
43 | 44 | var result = 0
|
44 |
| - for _ in 1...N { |
45 |
| - result = Ackermann(m, n) |
| 45 | + for _ in 1...n { |
| 46 | + result = ackermann(m, n) |
46 | 47 | if result != ref_result[n] {
|
47 | 48 | break
|
48 | 49 | }
|
49 | 50 | }
|
50 |
| - CheckResults(result == ref_result[n]) |
| 51 | + check(result == ref_result[n]) |
51 | 52 | }
|
0 commit comments