/*----- Header files ------------------------------------------------------*/
-#include <limits.h>
#include <stdio.h>
#include "macros.h"
#include "tvec-bench.h"
#include "tvec-types.h"
-/*----- Main code ---------------------------------------------------------*/
-
-/* Stupid but traditional recursive Fibonacci. */
-static unsigned long recfib(unsigned n)
- { return (n <= 1 ? n : recfib(n - 1) + recfib(n - 2)); }
-
-/* Slightly less stupid but still traditional iterative Fibonacci. */
-static unsigned long iterfib(unsigned n)
-{
- unsigned long u, v, t;
-
- for (u = 0, v = 1; n--; t = v, v = u, u += t);
- return (u);
-}
+#include "example.h"
-/* Sadly nontraditional intelligent Fibonacci. */
-static unsigned long expfib(unsigned n)
-{
- unsigned long a, b, u, v, t;
-
- /* We work in %$\Q(\phi)$%, where %$\phi^2 = \phi + 1$%. I claim that
- * %$\phi^k = F_k \phi + F_{k-1} \pmod f(\phi))$%. Proof by induction:
- * note that * %$F_{-1} = F_1 - F_0 = 1$%, so %$\phi^0 = 1 = {}$%
- * %$F_0 \phi + F_{-1}$%; and %$\phi^{k+1} = F_k \phi^2 + {}$%
- * %$F_{k-1} \phi = F_k (\phi + 1) + F_{k-1} \phi = (F_k + {}$%
- * %$F_{k-1} \phi + F_k = F_{k+1} \phi + F_k$% as claimed.
- *
- * Now, notice that %$(a \phi + b) (c \phi + d) = a c \phi^2 + {}$%
- * $%(a d + b c) \phi + b d = a c (\phi + 1) + (a d + b c) \phi + {}$%
- * %$b d = (a c + a d + b c) \phi + (a c + b d)$%. In particular,
- * %$(u \phi + v)^2 \equiv (u^2 + 2 u v) \phi + (u^2 + v^2)$%.
- */
- a = 0, b = 1; u = 1, v = 0;
- if (n)
- for (;;) {
- if (n%2) { t = a*u; a = t + a*v + b*u; b = t + b*v; }
- n /= 2; if (!n) break;
- t = u*u; u = t + 2*u*v; v = t + v*v;
- }
- return (a);
-}
+/*----- Main code ---------------------------------------------------------*/
int main(int argc, char *argv[])
{
struct tvec_state tvstate;
int argpos;
unsigned long i, a, b, t;
+ dstr d = DSTR_INIT;
tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
if (argpos < argc) die(2, "no input files expected");
tvec_adhoc(&tvstate);
-#if (ULONG_MAX/65536 >> 16) >= 0xffffffff
-# define FIBLIMIT 94 /* F_94 = 19740274219868223167 > 2^64 */
-#else
-# define FIBLIMIT 48 /* F_48 = 4807526976 > 2^32 */
-#endif
-
TVEC_TESTGROUP(&tvstate, "fib-test") {
for (i = 0, a = 0, b = 1; i < FIBLIMIT; i++, t = b, b = a, a += t)
TVEC_TEST(&tvstate) {
if (tvec_benchprep(&tvstate, &tvec_benchstate, 0)) break;
- TVEC_BENCHMARK("recfib, n = 40", &tvstate, tvec_benchstate, BTU_OP, 1)
- while (_bench_n--) ADMIRE(recfib(40));
- TVEC_BENCHMARK("iterfib, n = " STR(FIBLIMIT),
- &tvstate, tvec_benchstate, BTU_OP, 1)
+ dstr_reset(&d); dstr_putf(&d, "recfib, n = %u", RECFIBLIMIT);
+ TVEC_BENCHMARK(d.buf, &tvstate, tvec_benchstate, BTU_OP, 1)
+ while (_bench_n--) ADMIRE(recfib(RECFIBLIMIT));
+
+ dstr_reset(&d); dstr_putf(&d, "iterfib, n = %u", FIBLIMIT);
+ TVEC_BENCHMARK(d.buf, &tvstate, tvec_benchstate, BTU_OP, 1)
while (_bench_n--) ADMIRE(iterfib(FIBLIMIT));
- TVEC_BENCHMARK("expfib, n = " STR(FIBLIMIT),
- &tvstate, tvec_benchstate, BTU_OP, 1)
+
+ dstr_reset(&d); dstr_putf(&d, "expfib, n = %u", FIBLIMIT);
+ TVEC_BENCHMARK(d.buf, &tvstate, tvec_benchstate, BTU_OP, 1)
while (_bench_n--) ADMIRE(expfib(FIBLIMIT));
}
+ dstr_destroy(&d);
return (tvec_end(&tvstate));
}