3 * Demonstration of ad-doc testing
5 * (c) 2024 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
36 #include "tvec-adhoc.h"
37 #include "tvec-bench.h"
38 #include "tvec-types.h"
40 /*----- Main code ---------------------------------------------------------*/
42 /* Stupid but traditional recursive Fibonacci. */
43 static unsigned long recfib(unsigned n
)
44 { return (n
<= 1 ? n
: recfib(n
- 1) + recfib(n
- 2)); }
46 /* Slightly less stupid but still traditional iterative Fibonacci. */
47 static unsigned long iterfib(unsigned n
)
49 unsigned long u
, v
, t
;
51 for (u
= 0, v
= 1; n
--; t
= v
, v
= u
, u
+= t
);
55 /* Sadly nontraditional intelligent Fibonacci. */
56 static unsigned long expfib(unsigned n
)
58 unsigned long a
, b
, u
, v
, t
;
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.
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)$%.
72 a
= 0, b
= 1; u
= 1, v
= 0;
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
;
82 int main(int argc
, char *argv
[])
84 struct tvec_state tvstate
;
86 unsigned long i
, a
, b
, t
;
88 tvec_parseargs(argc
, argv
, &tvstate
, &argpos
, &tvec_adhocconfig
);
89 if (argpos
< argc
) die(2, "no input files expected");
92 #if (ULONG_MAX/65536 >> 16) >= 0xffffffff
93 # define FIBLIMIT 94 /* F_94 = 19740274219868223167 > 2^64 */
95 # define FIBLIMIT 48 /* F_48 = 4807526976 > 2^32 */
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
));
107 TVEC_TESTGROUP(&tvstate
, "fib-bench") {
108 TVEC_BENCHMARK_DECLS
;
110 if (tvec_benchprep(&tvstate
, &tvec_benchstate
, 0)) break;
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
));
122 return (tvec_end(&tvstate
));
125 /*----- That's all, folks -------------------------------------------------*/