Commit | Line | Data |
---|---|---|
b1a20bee MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Demonstration of ad-doc testing | |
4 | * | |
5 | * (c) 2024 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software: you can redistribute it and/or modify it under | |
13 | * the terms of the GNU Library General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * mLib is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | |
20 | * License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Library General Public | |
23 | * License along with mLib. If not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
25 | * USA. | |
26 | */ | |
27 | ||
28 | /*----- Header files ------------------------------------------------------*/ | |
29 | ||
30 | #include <limits.h> | |
31 | #include <stdio.h> | |
32 | ||
33 | #include "macros.h" | |
34 | #include "report.h" | |
35 | #include "tvec.h" | |
36 | #include "tvec-adhoc.h" | |
37 | #include "tvec-bench.h" | |
38 | #include "tvec-types.h" | |
39 | ||
40 | /*----- Main code ---------------------------------------------------------*/ | |
41 | ||
42 | /* Stupid but traditional recursive Fibonacci. */ | |
43 | static unsigned long recfib(unsigned n) | |
44 | { return (n <= 1 ? n : recfib(n - 1) + recfib(n - 2)); } | |
45 | ||
46 | /* Slightly less stupid but still traditional iterative Fibonacci. */ | |
47 | static unsigned long iterfib(unsigned n) | |
48 | { | |
49 | unsigned long u, v, t; | |
50 | ||
51 | for (u = 0, v = 1; n--; t = v, v = u, u += t); | |
52 | return (u); | |
53 | } | |
54 | ||
55 | /* Sadly nontraditional intelligent Fibonacci. */ | |
56 | static unsigned long expfib(unsigned n) | |
57 | { | |
58 | unsigned long a, b, u, v, t; | |
59 | ||
60 | /* We work in %$\Q(\phi)$%, where %$\phi^2 = \phi + 1$%. I claim that | |
61 | * %$\phi^k = F_k \phi + F_{k-1} \pmod f(\phi))$%. Proof by induction: | |
62 | * note that * %$F_{-1} = F_1 - F_0 = 1$%, so %$\phi^0 = 1 = {}$% | |
63 | * %$F_0 \phi + F_{-1}$%; and %$\phi^{k+1} = F_k \phi^2 + {}$% | |
64 | * %$F_{k-1} \phi = F_k (\phi + 1) + F_{k-1} \phi = (F_k + {}$% | |
65 | * %$F_{k-1} \phi + F_k = F_{k+1} \phi + F_k$% as claimed. | |
66 | * | |
67 | * Now, notice that %$(a \phi + b) (c \phi + d) = a c \phi^2 + {}$% | |
68 | * $%(a d + b c) \phi + b d = a c (\phi + 1) + (a d + b c) \phi + {}$% | |
69 | * %$b d = (a c + a d + b c) \phi + (a c + b d)$%. In particular, | |
70 | * %$(u \phi + v)^2 \equiv (u^2 + 2 u v) \phi + (u^2 + v^2)$%. | |
71 | */ | |
72 | a = 0, b = 1; u = 1, v = 0; | |
73 | if (n) | |
74 | for (;;) { | |
75 | if (n%2) { t = a*u; a = t + a*v + b*u; b = t + b*v; } | |
76 | n /= 2; if (!n) break; | |
77 | t = u*u; u = t + 2*u*v; v = t + v*v; | |
78 | } | |
79 | return (a); | |
80 | } | |
81 | ||
82 | int main(int argc, char *argv[]) | |
83 | { | |
84 | struct tvec_state tvstate; | |
85 | int argpos; | |
86 | unsigned long i, a, b, t; | |
87 | ||
88 | tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig); | |
89 | if (argpos < argc) die(2, "no input files expected"); | |
90 | tvec_adhoc(&tvstate); | |
91 | ||
92 | #if (ULONG_MAX/65536 >> 16) >= 0xffffffff | |
93 | # define FIBLIMIT 94 /* F_94 = 19740274219868223167 > 2^64 */ | |
94 | #else | |
95 | # define FIBLIMIT 48 /* F_48 = 4807526976 > 2^32 */ | |
96 | #endif | |
97 | ||
98 | TVEC_TESTGROUP(&tvstate, "fib-test") { | |
99 | for (i = 0, a = 0, b = 1; i < FIBLIMIT; i++, t = b, b = a, a += t) | |
100 | TVEC_TEST(&tvstate) { | |
101 | if (i < 40) TVEC_CLAIMEQ_UINT(&tvstate, a, recfib(i)); | |
102 | TVEC_CLAIMEQ_UINT(&tvstate, a, iterfib(i)); | |
103 | TVEC_CLAIMEQ_UINT(&tvstate, a, expfib(i)); | |
104 | } | |
105 | } | |
106 | ||
107 | TVEC_TESTGROUP(&tvstate, "fib-bench") { | |
108 | TVEC_BENCHMARK_DECLS; | |
109 | ||
110 | if (tvec_benchprep(&tvstate, &tvec_benchstate, 0)) break; | |
111 | ||
112 | TVEC_BENCHMARK("recfib, n = 40", &tvstate, tvec_benchstate, BTU_OP, 1) | |
113 | while (_bench_n--) ADMIRE(recfib(40)); | |
114 | TVEC_BENCHMARK("iterfib, n = " STR(FIBLIMIT), | |
115 | &tvstate, tvec_benchstate, BTU_OP, 1) | |
116 | while (_bench_n--) ADMIRE(iterfib(FIBLIMIT)); | |
117 | TVEC_BENCHMARK("expfib, n = " STR(FIBLIMIT), | |
118 | &tvstate, tvec_benchstate, BTU_OP, 1) | |
119 | while (_bench_n--) ADMIRE(expfib(FIBLIMIT)); | |
120 | } | |
121 | ||
122 | return (tvec_end(&tvstate)); | |
123 | } | |
124 | ||
125 | /*----- That's all, folks -------------------------------------------------*/ |