Page 226 - ARM 64 Bit Assembly Language
P. 226
214 Chapter 7
Listing 7.8 C source code file for a big integer abstract data type.
1 #include <bigint.h>
2 #include <string.h>
3 #include <math.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <stdint.h>
8
9 /* A big integer is an array of "chunks" of bits. The following
10 section defines the chunk, and defines a bigchunk to have twice as
11 many bits as a chunk. It also defines a mask that can be used to
12 select bits out of a bigchunk. When compiling this code, you can
13 define the chunk size by defining EIGHT_BIT, SIXTEEN_BIT, or
14 SIXTYFOUR_BIT. If none of those are defined, then the chunk size
15 is THIRTYTWO_BIT.
16 */
17 #ifdef EIGHT_BIT
18 typedef uint8_t chunk;
19 typedef int8_t schunk;
20 typedef uint16_t bigchunk;
21 #define CHUNKMASK 0xFFU
22 #else
23 #ifdef SIXTEEN_BIT
24 typedef uint16_t chunk;
25 typedef int16_t schunk;
26 typedef uint32_t bigchunk;
27 #define CHUNKMASK 0xFFFFU
28 #else
29 #ifdef SIXTYFOUR_BIT
30 typedef uint64_t chunk;
31 typedef int64_t schunk;
32 typedef __uint128_t bigchunk;
33 #define CHUNKMASK 0xFFFFFFFFFFFFFFFFULL
34 #else
35 typedef uint32_t chunk;
36 typedef int32_t schunk;
37 typedef uint64_t bigchunk;
38 #define CHUNKMASK 0xFFFFFFFFU
39 #endif
40 #endif
41 #endif
42
43 /* Macros */
44 #define BITSPERCHUNK ((sizeof(chunk)<<3))
45 #define MAX(a,b) ((a<b)?b:a)
46
47 /* A bigint is an array of chunks of bits */