algorithms for programmers 2008

Algorithms for Programmers
ideas and source code

This document is work in progress: read the “important remarks” near the beginning

Jörg Arndt
arndt@jjj.de
Draft version1 of 2008-November-27

1

The latest version and the accompanying software is online at

http://www.jjj.de/fxt/.

ii

[fxtbook draft of 2008-November-27]

CONTENTS


iii

Contents
Important remarks about this document

xi

I

1

Low level algorithms

1 Bit wizardry
1.1 Trivia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Operations on individual bits . . . . . . . . . . . . . . . . .
1.3 Operations on low bits or blocks of a word . . . . . . . . .
1.4 Extraction of ones, zeros, or blocks near transitions . . . .
1.5 Computing the index of a single set bit . . . . . . . . . . .
1.6 Operations on high bits or blocks of a word . . . . . . . . .

1.7 Functions related to the base-2 logarithm . . . . . . . . . .
1.8 Counting bits and blocks of a word . . . . . . . . . . . . .
1.9 Counting bits of many words . . . . . . . . . . . . . . . . .
1.10 Words as bit sets . . . . . . . . . . . . . . . . . . . . . . . .
1.11 Avoiding branches . . . . . . . . . . . . . . . . . . . . . . .
1.12 Bit-wise rotation of a word . . . . . . . . . . . . . . . . . .
1.13 Functions related to bit-wise rotation and binary necklaces
1.14 Reversing the bits of a word . . . . . . . . . . . . . . . . .
1.15 Bit-wise zip . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.16 Gray code and parity . . . . . . . . . . . . . . . . . . . . .
1.17 Bit sequency . . . . . . . . . . . . . . . . . . . . . . . . . .
1.18 Powers of the Gray code . . . . . . . . . . . . . . . . . . .
1.19 Invertible transforms on words . . . . . . . . . . . . . . . .
1.20 Space filling curves . . . . . . . . . . . . . . . . . . . . . . .
1.21 Scanning for zero bytes . . . . . . . . . . . . . . . . . . . .
1.22 2-adic inverse and square root . . . . . . . . . . . . . . . .
1.23 Radix −2 representation . . . . . . . . . . . . . . . . . . .
1.24 A sparse signed binary representation . . . . . . . . . . . .
1.25 Generating bit combinations . . . . . . . . . . . . . . . . .
1.26 Generating bit subsets of a given word . . . . . . . . . . .

1.27 Binary words as subsets in lexicographic order . . . . . . .
1.28 Minimal-change bit combinations . . . . . . . . . . . . . .
1.29 Fibonacci words . . . . . . . . . . . . . . . . . . . . . . . .
1.30 Binary words and parentheses strings * . . . . . . . . . . .
1.31 Permutations via primitives . . . . . . . . . . . . . . . . . .
1.32 CPU instructions often missed . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


3
3
8
9
12
14
16
18
19
22
24
26
28
29
34
38
41
47
48
50

56
59
60
62
65
66
70
72
77
79
82
85
87

2 Permutations
2.1 The revbin permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 The radix permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

89
89

93

[fxtbook draft of 2008-November-27]

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

iv

CONTENTS
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

94
95
97
99
100
101
105
107

3 Sorting and searching
3.1 Sorting . . . . . . . . . . . . . . . . . . . . .
3.2 Binary search . . . . . . . . . . . . . . . . . .
3.3 Index sorting . . . . . . . . . . . . . . . . . .
3.4 Pointer sorting . . . . . . . . . . . . . . . . .
3.5 Sorting by a supplied comparison function .
3.6 Determination of unique elements . . . . . .
3.7 Unique elements with inexact types . . . . .
3.8 Determination of equivalence classes . . . . .
3.9 Determination of monotonicity and convexity
3.10 Heapsort . . . . . . . . . . . . . . . . . . . .
3.11 Counting sort and radix sort . . . . . . . . .
3.12 Searching in unsorted arrays . . . . . . . . .

. .
. .
. .
. .
. .
. .
. .
. .
*.
. .
. .
. .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

119
119
121
122
124
125
128
129
131
135
138
138
141

4 Data structures
4.1 Stack (LIFO) . . . . . . . . .
4.2 Ring buffer . . . . . . . . . .
4.3 Queue (FIFO) . . . . . . . .
4.4 Deque (double-ended queue)
4.5 Heap and priority queue . . .
4.6 Bit-array . . . . . . . . . . .
4.7 Left-right array . . . . . . .
4.8 Finite state machines . . . .
4.9 Emulation of coroutines . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

145
145
147
148
150
152
156
158
161
163

II

In-place matrix transposition . . . . . . . .
The triple reversion technique * . . . . . .
The zip permutation . . . . . . . . . . . . .
The reversed zip permutation . . . . . . . .
The XOR permutation . . . . . . . . . . .
The Gray code permutation . . . . . . . .
The reversed Gray code permutation . . .
General permutations and their operations

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

Combinatorial generation

167

5 Conventions and considerations
5.1 About representations and orders . . . . . . . . . .
5.2 Ranking, unranking, and counting . . . . . . . . . .
5.3 Characteristics of the algorithms . . . . . . . . . . .
5.4 Optimization techniques . . . . . . . . . . . . . . . .
5.5 The implementations, demo-programs, and timings

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

169
169
169
170
171
172

6 Combinations
6.1 Binomial coefficients . . . . . . . . . . . .
6.2 Lexicographic and co-lexicographic order
6.3 Order by prefix shifts (cool-lex) . . . . .
6.4 Minimal-change order . . . . . . . . . . .
6.5 The Eades-McKay strong minimal-change
6.6 Two-close orderings via endo/enup moves
6.7 Recursive generation of certain orderings

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

173
173
174
178
179
180
184
189

. . . .
. . . .
. . . .
. . . .
order
. . . .
. . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

7 Compositions

191

[fxtbook draft of 2008-November-27]

CONTENTS
7.1
7.2
7.3
7.4

v

Co-lexicographic order . . . . . . . . . . . . . . . . .
Co-lexicographic order for compositions into exactly
Compositions and combinations . . . . . . . . . . .
Minimal-change orders . . . . . . . . . . . . . . . .

.
k
.
.

. . . .
parts
. . . .
. . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

191
193
195
196

8 Subsets
8.1 Lexicographic order . . . . . . . . . . .
8.2 Minimal-change order . . . . . . . . . .
8.3 Ordering with De Bruijn sequences . .
8.4 Shifts-order for subsets . . . . . . . . .
8.5 k-subsets where k lies in a given range .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

199
199
202
205
206
208

9 Mixed radix numbers
9.1 Counting (lexicographic) order . .
9.2 Minimal-change (Gray code) order
9.3 gslex order . . . . . . . . . . . . .
9.4 endo order . . . . . . . . . . . . .
9.5 Gray code for endo order . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

217
217
220
224
227
228

10 Permutations
10.1 Lexicographic order . . . . . . . . . . . . . . . . . . .
10.2 Co-lexicographic order . . . . . . . . . . . . . . . . . .
10.3 Factorial representations of permutations . . . . . . .
10.4 An order from reversing prefixes . . . . . . . . . . . .
10.5 Minimal-change order (Heap’s algorithm) . . . . . . .
10.6 Lipski’s Minimal-change orders . . . . . . . . . . . . .
10.7 Strong minimal-change order (Trotter’s algorithm) . .
10.8 Minimal-change orders from factorial numbers . . . .
10.9 Orders where the smallest element always moves right
10.10 Single track orders . . . . . . . . . . . . . . . . . . . .
10.11 Star-transposition order . . . . . . . . . . . . . . . . .
10.12 Derangement order . . . . . . . . . . . . . . . . . . .
10.13 Recursive algorithm for cyclic permutations . . . . . .
10.14 Minimal-change order for cyclic permutations . . . . .
10.15 Permutations with special properties . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

231
231
233
234
244
247
249
252
257
263
269
273
275
277
279
281

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

11 Multisets
289
11.1 Subsets of a multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
11.2 Permutations of a multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
12 Gray codes for strings with restrictions
12.1 List recursions . . . . . . . . . . . . . . . . . .
12.2 Fibonacci words . . . . . . . . . . . . . . . . .
12.3 Generalized Fibonacci words . . . . . . . . . .
12.4 Digit x followed by at least x zeros . . . . . . .
12.5 Generalized Pell words . . . . . . . . . . . . .
12.6 Sparse signed binary words . . . . . . . . . . .
12.7 Strings with no two successive nonzero digits .
12.8 Strings with no two successive zeros . . . . . .
12.9 Binary strings without substrings 1x1 or 1xy1

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

299
299
300
302
305
306
308
310
312
313

13 Parenthesis strings
317
13.1 Co-lexicographic order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
13.2 Gray code via restricted growth strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

[fxtbook draft of 2008-November-27]

vi

CONTENTS
13.3 Order by prefix shifts (cool-lex) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
13.4 Catalan numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
13.5 Increment-i RGS and k-ary trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

14 Integer partitions
14.1 Solution of a generalized problem
14.2 Iterative algorithm . . . . . . . . .
14.3 Partitions into m parts . . . . . .
14.4 The number of integer partitions .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

331
331
333
334
336

15 Set partitions
341
15.1 Recursive generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
15.2 The number of set partitions: Stirling set numbers and Bell numbers . . . . . . . . . . . 345
15.3 Restricted growth strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
16 A string substitution engine

357

17 Necklaces and Lyndon words
361
17.1 Generating all necklaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
17.2 The number of binary necklaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
17.3 The number of binary necklaces with fixed content . . . . . . . . . . . . . . . . . . . . . . 370
18 Hadamard and conference matrices
373
18.1 Hadamard matrices via LFSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
18.2 Hadamard matrices via conference matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 375
18.3 Conference matrices via finite fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
19 Searching paths in directed graphs
19.1 Representation of digraphs . . . .
19.2 Searching full paths . . . . . . . .
19.3 Conditional search . . . . . . . . .
19.4 Edge sorting and lucky paths . . .
19.5 Gray codes for Lyndon words . . .

III

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

Fast orthogonal transforms

401

20 The Fourier transform
20.1 The discrete Fourier transform . . . . . . . .
20.2 Summary of definitions of Fourier transforms
20.3 Radix-2 FFT algorithms . . . . . . . . . . .
20.4 Saving trigonometric computations . . . . .
20.5 Higher radix FFT algorithms . . . . . . . . .
20.6 Split-radix Fourier transforms . . . . . . . .
20.7 Symmetries of the Fourier transform . . . . .
20.8 Inverse FFT for free . . . . . . . . . . . . . .
20.9 Real valued Fourier transforms . . . . . . . .
20.10 Multidimensional Fourier transforms . . . . .
20.11 The matrix Fourier algorithm (MFA) . . . .
21 Algorithms for fast convolution
21.1 Convolution . . . . . . . . . . .
21.2 Correlation . . . . . . . . . . . .
21.3 Weighted Fourier transforms and
21.4 Convolution using the MFA . . .

381
382
383
388
392
394

.
*
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

403
403
404
406
411
413
421
424
425
426
433
434

. . . . . . . .
. . . . . . . .
convolutions
. . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

437
437
442
445
447

[fxtbook draft of 2008-November-27]

CONTENTS

vii

21.5 The z-transform (ZT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
21.6 Prime length FFTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
22 The Walsh transform and its relatives
22.1 The Walsh transform: Walsh-Kronecker basis . .
22.2 Eigenvectors of the Walsh transform * . . . . . .
22.3 The Kronecker product . . . . . . . . . . . . . .
22.4 A variant of the Walsh transform * . . . . . . .
22.5 Higher radix Walsh transforms . . . . . . . . . .
22.6 Localized Walsh transforms . . . . . . . . . . . .
22.7 Dyadic (XOR) convolution . . . . . . . . . . . .
22.8 The Walsh transform: Walsh-Paley basis . . . .
22.9 Sequency ordered Walsh transforms . . . . . . .
22.10 Slant transform . . . . . . . . . . . . . . . . . .
22.11 Arithmetic transform . . . . . . . . . . . . . . .
22.12 Reed-Muller transform . . . . . . . . . . . . . .
22.13 The OR-convolution, and the AND-convolution
22.14 The MAX-convolution * . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

457
457
460
461
463
465
469
473
475
476
482
483
486
489
491

23 The
23.1
23.2
23.3
23.4
23.5
23.6
23.7

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

493
493
495
497
499
501
504
506

24 The Hartley transform
24.1 Definition and symmetries . . . . . . . . . . . . . .
24.2 Radix-2 FHT algorithms . . . . . . . . . . . . . . .
24.3 Complex FT by HT . . . . . . . . . . . . . . . . . .
24.4 Complex FT by complex HT and vice versa . . . . .
24.5 Real FT by HT and vice versa . . . . . . . . . . . .
24.6 Higher radix FHT algorithms . . . . . . . . . . . . .
24.7 Convolution via FHT . . . . . . . . . . . . . . . . .
24.8 Negacyclic convolution via FHT . . . . . . . . . . .
24.9 Localized FHT algorithms . . . . . . . . . . . . . .
24.10 Two-dimensional FHTs . . . . . . . . . . . . . . . .
24.11 Discrete sine and cosine transform by HT . . . . . .
24.12 Automatic generation of transform code . . . . . . .
24.13 Eigenvectors of the Fourier and Hartley transform *

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

511
511
512
517
518
519
520
521
525
525
527
528
530
532

Haar transform
The ‘standard’ Haar transform . . . . . . . . .
In-place Haar transform . . . . . . . . . . . . .
Non-normalized Haar transforms . . . . . . . .
Transposed Haar transforms . . . . . . . . . .
The reversed Haar transform . . . . . . . . . .
Relations between Walsh and Haar transforms
Nonstandard splitting schemes * . . . . . . . .

25 Number theoretic transforms
25.1 Prime moduli for NTTs . .
25.2 Implementation of NTTs .
25.3 Convolution with NTTs . .
26 Fast
26.1
26.2
26.3

.
.
.
.
.
.
.

(NTTs)
535
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542

wavelet transforms
543
Wavelet filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Moment conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546

[fxtbook draft of 2008-November-27]

viii

IV

CONTENTS

Fast arithmetic

27 Fast
27.1
27.2
27.3
27.4
27.5
27.6

549

multiplication and exponentiation
Asymptotics of algorithms . . . . . . . . . . . . . . . .
Splitting schemes for multiplication . . . . . . . . . . .
Fast multiplication via FFT . . . . . . . . . . . . . . .
Radix/precision considerations with FFT multiplication
The sum-of-digits test . . . . . . . . . . . . . . . . . . .
Binary exponentiation . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

551
551
552
560
562
564
565

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

569
569
572
575
578
579
584
585
589

29 Iterations for the inversion of a function
29.1 Iterations and their rate of convergence . . . . . . . . .
29.2 Schröder’s formula . . . . . . . . . . . . . . . . . . . . .
29.3 Schröder’s formula and series reversion . . . . . . . . .
29.4 Householder’s formula . . . . . . . . . . . . . . . . . . .
29.5 Dealing with multiple roots . . . . . . . . . . . . . . . .
29.6 More iterations . . . . . . . . . . . . . . . . . . . . . . .
29.7 Convergence improvement by the delta squared process

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

591
591
592
593
596
597
599
601

30 The
30.1
30.2
30.3
30.4
30.5

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

603
603
605
611
615
623

31 Logarithm and exponential function
31.1 Logarithm . . . . . . . . . . . . . . . . . . . . . . . . . .
31.2 Exponential function . . . . . . . . . . . . . . . . . . . .
31.3 Logarithm and exponential function of power series . . .
31.4 Simultaneous computation of logarithms of small primes

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

631
631
636
639
641

28 Root extraction
28.1 Division, square root and cube root . . . . . .
28.2 Root extraction for rationals . . . . . . . . . .
28.3 Divisionless iterations for the inverse a-th root
28.4 Initial approximations for iterations . . . . . .
28.5 Some applications of the matrix square root .
28.6 Goldschmidt’s algorithm . . . . . . . . . . . .
28.7 Products for the a-th root . . . . . . . . . . .
28.8 Divisionless iterations for polynomial roots . .

arithmetic-geometric mean (AGM)
The AGM . . . . . . . . . . . . . . . . .
The elliptic functions K and E . . . . . .
AGM-type algorithms for hypergeometric
Computation of π . . . . . . . . . . . . .
Arctangent relations for π * . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

. . . . . .
. . . . . .
functions
. . . . . .
. . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

32 Computing the elementary functions with limited resources
645
32.1 Shift-and-add algorithms for logb (x) and bx . . . . . . . . . . . . . . . . . . . . . . . . . . 645
32.2 CORDIC algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
33 Numerical evaluation of power series
657
33.1 The binary splitting algorithm for rational series . . . . . . . . . . . . . . . . . . . . . . . 657
33.2 Rectangular schemes for evaluation of power series . . . . . . . . . . . . . . . . . . . . . . 663
33.3 The magic sumalt algorithm for alternating series . . . . . . . . . . . . . . . . . . . . . . 667
34 Recurrences and Chebyshev polynomials
671
34.1 Recurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
34.2 Chebyshev polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681

[fxtbook draft of 2008-November-27]

CONTENTS

ix

35 Hypergeometric functions
35.1 Definition . . . . . . . . . . . . . . . . . .
35.2 Derivative and differential equation . . .
35.3 Evaluations for fixed argument . . . . . .
35.4 Extraction of even and odd part . . . . .
35.5 Extraction of terms at positions j mod M
35.6 Transformations . . . . . . . . . . . . . .
35.7 Examples: elementary functions . . . . .
35.8 The function xx . . . . . . . . . . . . . .
35.9 Elliptic K and E . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

691
691
692
693
693
694
694
701
707
708

36 Cyclotomic polynomials, product forms, and continued fractions
36.1 Cylotomic polynomials, Möbius inversion, Lambert series . . . . . . . . . . . . . . . . . .
36.2 Conversion of series to infinite products . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36.3 Continued fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

711
711
716
721

37 Synthetic Iterations *
37.1 A variation of the iteration for the inverse . . . . . . . . .
37.2 An iteration related to the Thue constant . . . . . . . . .
37.3 An iteration related to the Golay-Rudin-Shapiro sequence
37.4 Iterations related to the ruler function . . . . . . . . . . .
37.5 An iteration related to the period-doubling sequence . . .
37.6 An iteration from substitution rules with sign . . . . . .
37.7 Iterations related to the sum of digits . . . . . . . . . . .
37.8 Iterations related to the binary Gray code . . . . . . . . .
37.9 A function that encodes the Hilbert curve . . . . . . . . .
37.10 Sparse variants of the inverse . . . . . . . . . . . . . . . .
37.11 An iteration related to the Fibonacci numbers . . . . . .
37.12 Iterations related to the Pell numbers . . . . . . . . . . .

731
731
735
736
738
740
744
745
747
752
755
758
762

V

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

Algorithms for finite fields

769

38 Modular arithmetic and some number theory
38.1 Implementation of the arithmetic operations .
38.2 Modular reduction with structured primes . .
38.3 The sieve of Eratosthenes . . . . . . . . . . . .
38.4 The order of an element . . . . . . . . . . . . .
38.5 Prime modulus: the field Z/pZ = Fp = GF(p)
38.6 Composite modulus: the ring Z/mZ . . . . . .
38.7 The Chinese Remainder Theorem (CRT) . . .
38.8 Quadratic residues . . . . . . . . . . . . . . . .
38.9 Computation of a square root modulo m . . .
38.10 The Rabin-Miller test for compositeness . . . .
38.11 Proving primality . . . . . . . . . . . . . . . .
38.12 Complex moduli: GF(p2 ) . . . . . . . . . . . .
38.13 Solving the Pell equation . . . . . . . . . . . .
38.14 Multiplication of hypercomplex numbers * . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

771
771
775
777
779
780
781
786
788
791
793
799
811
819
822

39 Binary polynomials
39.1 The basic arithmetical operations . . . . . .
39.2 Multiplication for polynomials of high degree
39.3 Modular arithmetic with binary polynomials
39.4 Irreducible polynomials . . . . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

827
827
832
838
842

.
.
.
.

[fxtbook draft of 2008-November-27]

x

CONTENTS
39.5
39.6
39.7
39.8
39.9
39.10
39.11
39.12

Primitive polynomials . . . . . . . . . . . . . . . . . . .
The number of irreducible and primitive polynomials .
Transformations that preserve irreducibility . . . . . . .
Self-reciprocal polynomials . . . . . . . . . . . . . . . .
Irreducible and primitive polynomials of special forms *
Generating irreducible polynomials from Lyndon words
Irreducible and cyclotomic polynomials * . . . . . . . .
Factorization of binary polynomials . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

846
848
850
851
854
860
862
863

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

869
869
872
873
878
878
880
881
883
888

. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
representation
. . . . . . . . .
. . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

893
893
900
902
904
906
908
917
918
921

40 Shift registers
40.1 Linear feedback shift registers (LFSR) . . . . . . . . .
40.2 Galois and Fibonacci setup . . . . . . . . . . . . . . .
40.3 Error detection by hashing: the CRC . . . . . . . . .
40.4 Generating all revbin pairs . . . . . . . . . . . . . . .
40.5 The number of m-sequences and De Bruijn sequences
40.6 Auto correlation of m-sequences . . . . . . . . . . . .
40.7 Feedback carry shift register (FCSR) . . . . . . . . .
40.8 Linear hybrid cellular automata (LHCA) . . . . . . .
40.9 Additive linear hybrid cellular automata . . . . . . .
41 Binary finite fields: GF(2n )
41.1 Arithmetic and basic properties . . . . . .
41.2 Minimal polynomials . . . . . . . . . . . .
41.3 Fast computation of the trace vector . . . .
41.4 Solving quadratic equations . . . . . . . . .
41.5 Representation by matrices * . . . . . . . .
41.6 Representation by normal bases . . . . . .
41.7 Conversion between normal and polynomial
41.8 Optimal normal bases (ONB) . . . . . . . .
41.9 Gaussian normal bases . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

A The electronic version of the book

929

B Machine used for benchmarking

931

C The pseudo language Sprache

933

D The pari/gp language

935

Bibliography

943

Index

963

[fxtbook draft of 2008-November-27]

Important remarks
about this document
The copyright and license are given in appendix A on page 929.
This is a draft of a book about selected algorithms. The audience in mind are programmers who are
interested in the treated algorithms and actually want to create and understand working and reasonably
optimized code.
The style varies somewhat which I do not consider bad per se: While some topics (as fast Fourier
transforms) need a clear and explicit introduction others (like the bit wizardry chapter) seem to be best
presented by basically showing the code with just a few comments.
The pseudo language Sprache is used when I see a clear advantage to do so, mainly when the corresponding
C++ does not appear to be self explanatory. Larger pieces of code are presented in C++. C programmers
do not need to be shocked by the ‘++’ as only a rather minimal set of the C++ features is used. Some
of the code, especially in part 3 (Arithmetical algorithms), is given in the pari/gp language as the use of
other languages would likely bury the idea in technicalities.
A printable version of this book will always stay online for free download. The referenced sources are
online as part of FXT (fast transforms and low level routines [20]) and hfloat (high precision floating
point algorithms [21]).
The reader is welcome to criticize and suggest improvements. Please name the draft version (date) with
your feedback! This version is of 2008-November-27. Note that you can copy and paste from the
PDF and DVI versions. Thanks go to those1 who helped to improve this document so far!
In case you want to cite this document, please avoid referencing individual chapters or sections as their
numbers (and titles) may change.
Enjoy reading!

1 in particular Igal Aharonovich, Max Alekseyev, Marcus Blackburn, Nathan Bullock, Dominique Delande, Torsten Finke,
Sean Furlong, Almaz Gaifullin, Pedro Gimeno, Alexander Glyzov, Andreas Grünbacher, Christoph Haenel, Tony HardieBick, Laszlo Hars, Stephen Hartke, Jeff Hurchalla, Gideon Klimer, Richard B. Kreckel, Gál László, Dirk Lattermann,
Avery Lee, Brent Lehman, Marc Lehmann, Paul C. Leopardi, John Lien, Mirko Liss, Fred Lunnon, Johannes Middeke,
Doug Moore, Andrew Morris, David Nalepa, Matthew Oliver, Miroslaw Osys, Christoph Pacher, Scott Paine, Yves Paradis,
Edith Parzefall, Gunther Piez, André Piotrowski, David Garcı́a Quintas, Tony Reix, Johan Rönnblom, Thomas Schraitle,
Clive Scott, Michael Somos, Michal Staruch, Ralf Stephan, Mikko Tommila, Michael Roby Wetherfield, Jim White, Vinnie
Winkler, John Youngquist, Rui Zhang, and Paul Zimmermann.

“Why make things difficult, when it is possible to make them cryptic
and totally illogical, with just a little bit more effort?”
— Aksel Peter Jørgensen

1

Part I

Low level algorithms

[fxtbook draft of 2008-November-27]

3

Chapter 1

Bit wizardry
We present low-level functions that operate on the bits of a binary word. It is sometimes not obvious
what a particular function is good for. However, if you happen to have an application for a it then the
program using it may run significantly faster.
The C-type unsigned long is abbreviated as ulong as defined in [FXT: fxttypes.h]. It is assumed that
BITS_PER_LONG reflects the size of an unsigned long. It is defined in [FXT: bits/bitsperlong.h] and
usually equals the machine word size: it equals 32 on 32-bit architectures and 64 on 64-bit machines.
Further, the quantity BYTES_PER_LONG shall reflect the number of bytes in a machine word: it equals
BITS_PER_LONG divided by eight. For some functions it is assumed that long and ulong have the same
number of bits.
Many functions will only work on machines that use two’s complement.
The examples of assembler code are for the x86 and the AMD64 architecture. They should be simple
enough to be understandable for readers who know assembler for any CPU.

1.1

Trivia

1.1.1

Little endian versus big endian

The order in which the bytes of an integer are stored in memory can start with the least significant byte
(little endian machine) or with the most significant byte (big endian machine). The hexadecimal number
0x0D0C0B0A will be stored in the following manner when memory addresses grow from left to right:
adr:
mem:
mem:

z
0D
0A

z+1
0C
0B

z+2
0B
0C

z+3
0A
0D

// big endian
// little endian

The difference becomes visible when you cast pointers. Let V be the 32-bit integer with the value above.
Then the result of char c = *(char *)(&V); will be 0x0A (value modulo 256) on a little endian
machine but 0x0D (value divided by 224 ) on a big endian machine. Though friends of big endian way
sometimes refer to little endian as ‘wrong endian’, the desired result of the shown pointer cast is much
more often the modulo operation.
Whenever words are serialized into bytes, for example with transfer over a network or to a disk one will
need two code versions, one for big endian, and one for small endian machines. The C-type union (with
words and bytes) may also require separate treatment for big and little endian architectures.

[fxtbook draft of 2008-November-27]

4

Chapter 1: Bit wizardry

1.1.2

Size of pointer is size of long

On sane architectures a pointer fits into a type long integer. When programming for a 32-bit architecture
(where the size of int and long coincide) casting pointers to integers (and back) will work. The same
code will fail on 64-bit machines. If you have to cast pointers to an integer type, cast them to long. For
portable code better avoid casting pointers to integer types.

1.1.3

Shifts and division

With two’s complement arithmetic (that is: on likely every computer you’ll ever touch) division and
multiplication by powers of two is right and left shift, respectively. This is true for unsigned types and
for multiplication (left shift) with signed types. Division with signed types rounds toward zero, as one
would expect, but right shift is a division (by a power of two) that rounds to minus infinity:
int a = -1;
int c = a >> 1;
int d = a / 2;

// c == -1
// d == 0

The compiler still uses a shift instruction for the division, but with a ‘fix’ for negative values:
9:test.cc
@ int foo(int a)
10:test.cc
@ {
285 0003 8B442410
movl
11:test.cc
@
int s = a >> 1;
289 0007 89C1
movl
290 0009 D1F9
sarl
12:test.cc
@
int d = a / 2;
293 000b 89C2
movl
294 000d C1EA1F
shrl
295 0010 01D0
addl
296 0012 D1F8
sarl

16(%esp),%eax

// move argument to %eax

%eax,%ecx
$1,%ecx
%eax,%edx
$31,%edx // fix: %edx=(%edx