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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
|
\chapter{Not implemented}
\label{chap:Not implemented}
In this chapter we maintain a list of
features we have chosen not to implement at
the moment, but may add when libzahl matures,
but to a separate library that could be made
to support multiple bignum libraries. Functions
listed herein will only be implemented if it
is shown that it would be overwhelmingly
advantageous. For each feature, a sample
implementation or a mathematical expression
on which you can base your implementation is
included. The sample implementations create
temporary integer references to simplify the
examples. You should try to use dedicated
variables; in case of recursion, a robust
program should store temporary variables on
a stack, so they can be cleaned up if
something happens.
Research problems, like prime factorisation
and discrete logarithms, do not fit in the
scope of bignum libraries % Unless they are extraordinarily bloated with vague mission-scope, like systemd.
and therefore do not fit into libzahl,
and will not be included in this chapter.
Operators and functions that grow so
ridiculously fast that a tiny lookup table
can be constructed to cover all practical
input will also not be included in this
chapter, nor in libzahl.
\vspace{1cm}
\minitoc
\newpage
\section{Extended greatest common divisor}
\label{sec:Extended greatest common divisor}
\begin{alltt}
void
extgcd(z_t bézout_coeff_1, z_t bézout_coeff_2, z_t gcd
z_t quotient_1, z_t quotient_2, z_t a, z_t b)
\{
#define old_r gcd
#define old_s bézout_coeff_1
#define old_t bézout_coeff_2
#define s quotient_2
#define t quotient_1
z_t r, q, qs, qt;
int odd = 0;
zinit(r), zinit(q), zinit(qs), zinit(qt);
zset(r, b), zset(old_r, a);
zseti(s, 0), zseti(old_s, 1);
zseti(t, 1), zseti(old_t, 0);
while (!zzero(r)) \{
odd ^= 1;
zdivmod(q, old_r, old_r, r), zswap(old_r, r);
zmul(qs, q, s), zsub(old_s, old_s, qs);
zmul(qt, q, t), zsub(old_t, old_t, qt);
zswap(old_s, s), zswap(old_t, t);
\}
odd ? abs(s, s) : abs(t, t);
zfree(r), zfree(q), zfree(qs), zfree(qt);
\}
\end{alltt}
Perhaps you are asking yourself ``wait a minute,
doesn't the extended Euclidean algorithm only
have three outputs if you include the greatest
common divisor, what is this shenanigans?''
No\footnote{Well, technically yes, but it calculates
two values for free in the same ways as division
calculates the remainder for free.}, it has five
outputs, most implementations just ignore two of
them. If this confuses you, or you want to know
more about this, I refer you to Wikipeida.
\newpage
\section{Least common multiple}
\label{sec:Least common multiple}
\( \displaystyle{
\mbox{lcm}(a, b) = \frac{\lvert a \cdot b \rvert}{\mbox{gcd}(a, b)}
}\)
\vspace{1em}
$\mbox{lcm}(a, b)$ is undefined when $a$ or
$b$ is zero, because division by zero is
undefined. Note however that $\mbox{gcd}(a, b)$
is only zero when both $a$ and $b$ is zero.
\newpage
\section{Modular multiplicative inverse}
\label{sec:Modular multiplicative inverse}
\begin{alltt}
int
modinv(z_t inv, z_t a, z_t m)
\{
z_t x, _1, _2, _3, gcd, mabs, apos;
int invertible, aneg = zsignum(a) < 0;
zinit(x), zinit(_1), zinit(_2), zinit(_3), zinit(gcd);
*mabs = *m;
zabs(mabs, mabs);
if (aneg) \{
zinit(apos);
zset(apos, a);
if (zcmpmag(apos, mabs))
zmod(apos, apos, mabs);
zadd(apos, apos, mabs);
\}
extgcd(inv, _1, _2, _3, gcd, apos, mabs);
if ((invertible = !zcmpi(gcd, 1))) \{
if (zsignum(inv) < 0)
(zsignum(m) < 0 ? zsub : zadd)(x, x, m);
zswap(x, inv);
\}
if (aneg)
zfree(apos);
zfree(x), zfree(_1), zfree(_2), zfree(_3), zfree(gcd);
return invertible;
\}
\end{alltt}
\newpage
\section{Random prime number generation}
\label{sec:Random prime number generation}
TODO
\newpage
\section{Symbols}
\label{sec:Symbols}
\subsection{Legendre symbol}
\label{sec:Legendre symbol}
\( \displaystyle{
\left ( \frac{a}{p} \right ) \equiv a^{\frac{p - 1}{2}} ~(\text{Mod}~p),~
\left ( \frac{a}{p} \right ) \in \{-1,~0,~1\},~
p \in \textbf{P},~ p > 2
}\)
\vspace{1em}
\noindent
That is, unless $\displaystyle{a^{\frac{p - 1}{2}} \mod p \le 1}$,
$\displaystyle{a^{\frac{p - 1}{2}} \mod p = p - 1}$, so
$\displaystyle{\left ( \frac{a}{p} \right ) = -1}$.
It should be noted that
\( \displaystyle{
\left ( \frac{a}{p} \right ) =
\left ( \frac{a ~\text{Mod}~ p}{p} \right ),
}\)
so a compressed lookup table can be used for small $p$.
\subsection{Jacobi symbol}
\label{sec:Jacobi symbol}
\( \displaystyle{
\left ( \frac{a}{n} \right ) =
\prod_k \left ( \frac{a}{p_k} \right )^{n_k},
}\)
where $\displaystyle{n = \prod_k p_k^{n_k} > 0}$,
and $p_k \in \textbf{P}$.
\vspace{1em}
Like the Legendre symbol, the Jacobi symbol is $n$-periodic over $a$.
If $n$, is prime, the Jacobi symbol is the Legendre symbol, but
the Jacobi symbol is defined for all odd numbers $n$, where the
Legendre symbol is defined only for odd primes $n$.
Use the following algorithm to calculate the Jacobi symbol:
\vspace{1em}
\hspace{-2.8ex}
\begin{minipage}{\linewidth}
\begin{algorithmic}
\STATE $a \gets a \mod n$
\STATE $r \gets \mbox{lsb}~ a$
\STATE $a \gets a \cdot 2^{-r}$
\STATE \(\displaystyle{
r \gets \left \lbrace \begin{array}{rl}
1 &
\text{if}~ n \equiv 1, 7 ~(\text{Mod}~ 8)
~\text{or}~ r \equiv 0 ~(\text{Mod}~ 2) \\
-1 & \text{otherwise} \\
\end{array} \right .
}\)
\IF{$a = 1$}
\RETURN $r$
\ELSIF{$\gcd(a, n) \neq 1$}
\RETURN $0$
\ENDIF
\STATE $(a, n) = (n, a)$
\STATE \textbf{start over}
\end{algorithmic}
\end{minipage}
\subsection{Kronecker symbol}
\label{sec:Kronecker symbol}
The Kronecker symbol
$\displaystyle{\left ( \frac{a}{n} \right )}$
is a generalisation of the Jacobi symbol,
where $n$ can be any integer. For positive
odd $n$, the Kronecker symbol is equal to
the Jacobi symbol. For even $n$, the
Kronecker symbol is $2n$-periodic over $a$,
the Kronecker symbol is zero for all
$(a, n)$ with both $a$ and $n$ are even.
\vspace{1em}
\noindent
\( \displaystyle{
\left ( \frac{a}{2^k \cdot n} \right ) =
\left ( \frac{a}{n} \right ) \cdot \left ( \frac{a}{2} \right )^k,
}\)
where
\( \displaystyle{
\left ( \frac{a}{2} \right ) =
\left \lbrace \begin{array}{rl}
1 & \text{if}~ a \equiv 1, 7 ~(\text{Mod}~ 8) \\
-1 & \text{if}~ a \equiv 3, 5 ~(\text{Mod}~ 8) \\
0 & \text{otherwise}
\end{array} \right .
}\)
\vspace{1em}
\noindent
\( \displaystyle{
\left ( \frac{-a}{n} \right ) =
\left ( \frac{a}{n} \right ) \cdot \left ( \frac{a}{-1} \right ),
}\)
where
\( \displaystyle{
\left ( \frac{a}{-1} \right ) =
\left \lbrace \begin{array}{rl}
1 & \text{if}~ a \ge 0 \\
-1 & \text{if}~ a < 0
\end{array} \right .
}\)
\vspace{1em}
\noindent
However, for $n = 0$, the symbol is defined as
\vspace{1em}
\noindent
\( \displaystyle{
\left ( \frac{a}{0} \right ) =
\left \lbrace \begin{array}{rl}
1 & \text{if}~ a = \pm 1 \\
0 & \text{otherwise.}
\end{array} \right .
}\)
\subsection{Power residue symbol}
\label{sec:Power residue symbol}
TODO
\subsection{Pochhammer \emph{k}-symbol}
\label{sec:Pochhammer k-symbol}
\( \displaystyle{
(x)_{n,k} = \prod_{i = 1}^n (x + (i - 1)k)
}\)
\newpage
\section{Logarithm}
\label{sec:Logarithm}
TODO
\newpage
\section{Roots}
\label{sec:Roots}
TODO
\newpage
\section{Modular roots}
\label{sec:Modular roots}
TODO % Square: Cipolla's algorithm, Pocklington's algorithm, Tonelli–Shanks algorithm
\newpage
\section{Combinatorial}
\label{sec:Combinatorial}
\subsection{Factorial}
\label{sec:Factorial}
\( \displaystyle{
n! = \left \lbrace \begin{array}{ll}
\displaystyle{\prod_{i = 1}^n i} & \textrm{if}~ n \ge 0 \\
\textrm{undefined} & \textrm{otherwise}
\end{array} \right .
}\)
\vspace{1em}
This can be implemented much more efficiently
than using the naïve method, and is a very
important function for many combinatorial
applications, therefore it may be implemented
in the future if the demand is high enough.
An efficient, yet not optimal, implementation
of factorials that about halves the number of
required multiplications compared to the naïve
method can be derived from the observation
\vspace{1em}
\( \displaystyle{
n! = n!! ~ \lfloor n / 2 \rfloor! ~ 2^{\lfloor n / 2 \rfloor}
}\), $n$ odd.
\vspace{1em}
\noindent
The resulting algorithm can be expressed as
\begin{alltt}
void
fact(z_t r, uint64_t n)
\{
z_t p, f, two;
uint64_t *ns, s = 1, i = 1;
zinit(p), zinit(f), zinit(two);
zseti(r, 1), zseti(p, 1), zseti(f, n), zseti(two, 2);
ns = alloca(zbits(f) * sizeof(*ns));
while (n > 1) \{
if (n & 1) \{
ns[i++] = n;
s += n >>= 1;
\} else \{
zmul(r, r, (zsetu(f, n), f));
n -= 1;
\}
\}
for (zseti(f, 1); i-- > 0; zmul(r, r, p);)
for (n = ns[i]; zcmpu(f, n); zadd(f, f, two))
zmul(p, p, f);
zlsh(r, r, s);
zfree(two), zfree(f), zfree(p);
\}
\end{alltt}
\subsection{Subfactorial}
\label{sec:Subfactorial}
\( \displaystyle{
!n = \left \lbrace \begin{array}{ll}
n(!(n - 1)) + (-1)^n & \textrm{if}~ n > 0 \\
1 & \textrm{if}~ n = 0 \\
\textrm{undefined} & \textrm{otherwise}
\end{array} \right . =
n! \sum_{i = 0}^n \frac{(-1)^i}{i!}
}\)
\subsection{Alternating factorial}
\label{sec:Alternating factorial}
\( \displaystyle{
\mbox{af}(n) = \sum_{i = 1}^n {(-1)^{n - i} i!}
}\)
\subsection{Multifactorial}
\label{sec:Multifactorial}
\( \displaystyle{
n!^{(k)} = \left \lbrace \begin{array}{ll}
1 & \textrm{if}~ n = 0 \\
n & \textrm{if}~ 0 < n \le k \\
n((n - k)!^{(k)}) & \textrm{if}~ n > k \\
\textrm{undefined} & \textrm{otherwise}
\end{array} \right .
}\)
\subsection{Quadruple factorial}
\label{sec:Quadruple factorial}
\( \displaystyle{
(4n - 2)!^{(4)}
}\)
\subsection{Superfactorial}
\label{sec:Superfactorial}
\( \displaystyle{
\mbox{sf}(n) = \prod_{k = 1}^n k^{1 + n - k}
}\), undefined for $n < 0$.
\subsection{Hyperfactorial}
\label{sec:Hyperfactorial}
\( \displaystyle{
H(n) = \prod_{k = 1}^n k^k
}\), undefined for $n < 0$.
\subsection{Raising factorial}
\label{sec:Raising factorial}
\( \displaystyle{
x^{(n)} = \frac{(x + n - 1)!}{(x - 1)!}
}\), undefined for $n < 0$.
\subsection{Falling factorial}
\label{sec:Falling factorial}
\( \displaystyle{
(x)_n = \frac{x!}{(x - n)!}
}\), undefined for $n < 0$.
\subsection{Primorial}
\label{sec:Primorial}
\( \displaystyle{
n\# = \prod_{\lbrace i \in \textbf{P} ~:~ i \le n \rbrace} i
}\)
\vspace{1em}
\noindent
\( \displaystyle{
p_n\# = \prod_{i \in \textbf{P}_{\pi(n)}} i
}\)
\subsection{Gamma function}
\label{sec:Gamma function}
$\Gamma(n) = (n - 1)!$, undefined for $n \le 0$.
\subsection{K-function}
\label{sec:K-function}
\( \displaystyle{
K(n) = \left \lbrace \begin{array}{ll}
\displaystyle{\prod_{i = 1}^{n - 1} i^i} & \textrm{if}~ n \ge 0 \\
1 & \textrm{if}~ n = -1 \\
0 & \textrm{otherwise (result is truncated)}
\end{array} \right .
}\)
\subsection{Binomial coefficient}
\label{sec:Binomial coefficient}
\( \displaystyle{
\binom{n}{k} = \frac{n!}{k!(n - k)!}
= \frac{1}{(n - k)!} \prod_{i = k + 1}^n i
= \frac{1}{k!} \prod_{i = n - k + 1}^n i
}\)
\subsection{Catalan number}
\label{sec:Catalan number}
\( \displaystyle{
C_n = \left . \binom{2n}{n} \middle / (n + 1) \right .
}\)
\subsection{Fuss–Catalan number}
\label{sec:Fuss-Catalan number} % not en dash
\( \displaystyle{
A_m(p, r) = \frac{r}{mp + r} \binom{mp + r}{m}
}\)
\newpage
\section{Fibonacci numbers}
\label{sec:Fibonacci numbers}
Fibonacci numbers can be computed efficiently
using the following algorithm:
\begin{alltt}
static void
fib_ll(z_t f, z_t g, z_t n)
\{
z_t a, k;
int odd;
if (zcmpi(n, 1) <= 0) \{
zseti(f, !zzero(n));
zseti(f, zzero(n));
return;
\}
zinit(a), zinit(k);
zrsh(k, n, 1);
if (zodd(n)) \{
odd = zodd(k);
fib_ll(a, g, k);
zadd(f, a, a);
zadd(k, f, g);
zsub(f, f, g);
zmul(f, f, k);
zseti(k, odd ? -2 : +2);
zadd(f, f, k);
zadd(g, g, g);
zadd(g, g, a);
zmul(g, g, a);
\} else \{
fib_ll(g, a, k);
zadd(f, a, a);
zadd(f, f, g);
zmul(f, f, g);
zsqr(a, a);
zsqr(g, g);
zadd(g, a);
\}
zfree(k), zfree(a);
\}
\end{alltt}
\newpage
\begin{alltt}
void
fib(z_t f, z_t n)
\{
z_t tmp, k;
zinit(tmp), zinit(k);
zset(k, n);
fib_ll(f, tmp, k);
zfree(k), zfree(tmp);
\}
\end{alltt}
\noindent
This algorithm is based on the rules
\vspace{1em}
\( \displaystyle{
F_{2k + 1} = 4F_k^2 - F_{k - 1}^2 + 2(-1)^k = (2F_k + F_{k-1})(2F_k - F_{k-1}) + 2(-1)^k
}\)
\vspace{1em}
\( \displaystyle{
F_{2k} = F_k \cdot (F_k + 2F_{k - 1})
}\)
\vspace{1em}
\( \displaystyle{
F_{2k - 1} = F_k^2 + F_{k - 1}^2
}\)
\vspace{1em}
\noindent
Each call to {\tt fib\_ll} returns $F_n$ and $F_{n - 1}$
for any input $n$. $F_{k}$ is only correctly returned
for $k \ge 0$. $F_n$ and $F_{n - 1}$ is used for
calculating $F_{2n}$ or $F_{2n + 1}$. The algorithm
can be sped up with a larger lookup table than one
covering just the base cases. Alternatively, a naïve
calculation could be used for sufficiently small input.
\newpage
\section{Lucas numbers}
\label{sec:Lucas numbers}
Lucas [lyk\textscripta] numbers can be calculated by utilising
{\tt fib\_ll} from \secref{sec:Fibonacci numbers}:
\begin{alltt}
void
lucas(z_t l, z_t n)
\{
z_t k;
int odd;
if (zcmp(n, 1) <= 0) \{
zset(l, 1 + zzero(n));
return;
\}
zinit(k);
zrsh(k, n, 1);
if (zeven(n)) \{
lucas(l, k);
zsqr(l, l);
zseti(k, zodd(k) ? +2 : -2);
zadd(l, k);
\} else \{
odd = zodd(k);
fib_ll(l, k, k);
zadd(l, l, l);
zadd(l, l, k);
zmul(l, l, k);
zseti(k, 5);
zmul(l, l, k);
zseti(k, odd ? +4 : -4);
zadd(l, l, k);
\}
zfree(k);
\}
\end{alltt}
\noindent
This algorithm is based on the rules
\vspace{1em}
\( \displaystyle{
L_{2k} = L_k^2 - 2(-1)^k
}\)
\vspace{1ex}
\( \displaystyle{
L_{2k + 1} = 5F_{k - 1} \cdot (2F_k + F_{k - 1}) - 4(-1)^k
}\)
\vspace{1em}
\noindent
Alternatively, the function can be implemented
trivially using the rule
\vspace{1em}
\( \displaystyle{
L_k = F_k + 2F_{k - 1}
}\)
\newpage
\section{Bit operation}
\label{sec:Bit operation unimplemented}
\subsection{Bit scanning}
\label{sec:Bit scanning}
Scanning for the next set or unset bit can be
trivially implemented using {\tt zbtest}. A
more efficient, although not optimally efficient,
implementation would be
\begin{alltt}
size_t
bscan(z_t a, size_t whence, int direction, int value)
\{
size_t ret;
z_t t;
zinit(t);
value ? zset(t, a) : znot(t, a);
ret = direction < 0
? (ztrunc(t, t, whence + 1), zbits(t) - 1)
: (zrsh(t, t, whence), zlsb(t) + whence);
zfree(t);
return ret;
\}
\end{alltt}
\subsection{Population count}
\label{sec:Population count}
The following function can be used to compute
the population count, the number of set bits,
in an integer, counting the sign bit:
\begin{alltt}
size_t
popcount(z_t a)
\{
size_t i, ret = zsignum(a) < 0;
for (i = 0; i < a->used; i++) \{
ret += __builtin_popcountll(a->chars[i]);
\}
return ret;
\}
\end{alltt}
\noindent
It requires a compiler extension; if it's not
available, there are other ways to computer the
population count for a word: manually bit-by-bit,
or with a fully unrolled
\begin{alltt}
int s;
for (s = 1; s < 64; s <<= 1)
w = (w >> s) + w;
\end{alltt}
\subsection{Hamming distance}
\label{sec:Hamming distance}
A simple way to compute the Hamming distance,
the number of differing bits between two
numbers is with the function
\begin{alltt}
size_t
hammdist(z_t a, z_t b)
\{
size_t ret;
z_t t;
zinit(t);
zxor(t, a, b);
ret = popcount(t);
zfree(t);
return ret;
\}
\end{alltt}
\noindent
The performance of this function could
be improved by comparing character by
character manually using {\tt zxor}.
\newpage
\section{Miscellaneous}
\label{sec:Miscellaneous}
\subsection{Character retrieval}
\label{sec:Character retrieval}
\begin{alltt}
uint64_t
getu(z_t a)
\{
return zzero(a) ? 0 : a->chars[0];
\}
\end{alltt}
\subsection{Fit test}
\label{sec:Fit test}
Some libraries have functions for testing
whether a big integer is small enough to
fit into an intrinsic type. Since libzahl
does not provide conversion to intrinsic
types this is irrelevant. But additionally,
it can be implemented with a single
one-line macro that does not have any
side-effects.
\begin{alltt}
#define fits_in(a, type) (zbits(a) <= 8 * sizeof(type))
\textcolor{c}{/* \textrm{Just be sure the type is integral.} */}
\end{alltt}
\subsection{Reference duplication}
\label{sec:Reference duplication}
This could be useful for creating duplicates
with modified sign, but only if neither
{\tt r} nor {\tt a} will be modified whilst
both are in use. Because it is unsafe,
fairly simple to create an implementation
with acceptable performance — {\tt *r = *a},
— and probably seldom useful, this has not
been implemented.
\begin{alltt}
void
refdup(z_t r, z_t a)
\{
\textcolor{c}{/* \textrm{Almost fully optimised, but perfectly portable} *r = *a; */}
r->sign = a->sign;
r->used = a->used;
r->alloced = a->alloced;
r->chars = a->chars;
\}
\end{alltt}
\subsection{Variadic initialisation}
\label{sec:Variadic initialisation}
Most bignum libraries have variadic functions
for initialisation and uninitialisation. This
is not available in libzahl, because it is
not useful enough and has performance overhead.
And what's next, support {\tt va\_list},
variadic addition, variadic multiplication,
power towers, set manipulation? Anyone can
implement variadic wrapper for {\tt zinit} and
{\tt zfree} if they really need it. But if
you want to avoid the overhead, you can use
something like this:
\begin{alltt}
/* \textrm{Call like this:} MANY(zinit, (a), (b), (c)) */
#define MANY(f, ...) (_MANY1(f, __VA_ARGS__,,,,,,,,,))
#define _MANY1(f, a, ...) (void)f a, _MANY2(f, __VA_ARGS__)
#define _MANY2(f, a, ...) (void)f a, _MANY3(f, __VA_ARGS__)
#define _MANY3(f, a, ...) (void)f a, _MANY4(f, __VA_ARGS__)
#define _MANY4(f, a, ...) (void)f a, _MANY5(f, __VA_ARGS__)
#define _MANY5(f, a, ...) (void)f a, _MANY6(f, __VA_ARGS__)
#define _MANY6(f, a, ...) (void)f a, _MANY7(f, __VA_ARGS__)
#define _MANY7(f, a, ...) (void)f a, _MANY8(f, __VA_ARGS__)
#define _MANY8(f, a, ...) (void)f a, _MANY9(f, __VA_ARGS__)
#define _MANY9(f, a, ...) (void)f a
\end{alltt}
|