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
|
/* ----------------------------------------------------------------------- *
*
* Copyright 2007 H. Peter Anvin - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, Inc.,
* 59 Temple Place Ste 330, Boston MA 02111-1307, USA; version 2.1,
* incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* pbn.h
*
* Simple multi-precision signed integer library
*/
#ifndef PBN_H
#define PBN_H
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
/* Configurables */
#define PBN_LIMB_BITS 32
#define PBN_LIMB_SHIFT 5
#define PBN_LIMB_xFMT "08"PRIx32
#define PBN_LIMB_dFMT "08"PRId32
#define PBN_LIMB_uFMT "08"PRIu32
typedef uint32_t pbn_limb_t;
typedef uint64_t pbn_2limb_t;
typedef int32_t pbn_slimb_t;
typedef int64_t pbn_2slimb_t;
struct pbn {
int ref;
int len;
int bits;
int minus;
pbn_limb_t num[1];
};
#define _pbn_malloc(x) malloc(x)
#define _pbn_zalloc(x) calloc(x,1)
#define _pbn_free(x) free(x)
#define _pbn_realloc(x,y) realloc(x,y)
#define pbn_ref(x) ((x)->ref++, (x))
#define pbn_free(x) \
do { \
struct pbn *__pbn = (x); \
if (! --__pbn->ref) \
_pbn_free(__pbn); \
} while (0)
struct pbn *pbn_addsub(struct pbn *, struct pbn *, int issub);
#define pbn_add(x,y) pbn_addsub(x,y,0)
#define pbn_sub(x,y) pbn_addsub(x,y,1)
/* pbn_cmp() and pbn_abscmp() do NOT consume a reference! */
int pbn_cmp(const struct pbn *, const struct pbn *);
int pbn_abscmp(const struct pbn *, const struct pbn *);
struct pbn *pbn_mul(struct pbn *, struct pbn *);
struct pbn *pbn_muls(struct pbn *, pbn_slimb_t);
int pbn_div(struct pbn **, struct pbn **, struct pbn *, struct pbn *);
int pbn_divs(struct pbn **, pbn_slimb_t *, struct pbn *, pbn_slimb_t);
struct pbn *pbn_abs(struct pbn *);
struct pbn *pbn_shr(struct pbn *, int);
struct pbn *pbn_shl(struct pbn *, int);
struct pbn *pbn_and(struct pbn *, struct pbn *);
struct pbn *pbn_or(struct pbn *, struct pbn *);
struct pbn *pbn_xor(struct pbn *, struct pbn *);
struct pbn *pbn_set_bit(struct pbn *, int);
struct pbn *pbn_clr_bit(struct pbn *, int);
/* pbn_bit() does NOT consume a reference! */
int pbn_bit(const struct pbn *, int);
struct pbn *pbn_int(pbn_slimb_t);
struct pbn *pbn_uint(pbn_limb_t);
/* Primarily for debugging; does NOT consume a reference */
void pbn_dump(FILE *, const struct pbn *);
struct pbn *pbn_new(int);
struct pbn *pbn_dup(const struct pbn *);
struct pbn *pbn_dupx(const struct pbn *, int);
struct pbn *pbn_cow(struct pbn *, int);
/* Internal functions */
struct pbn *pbn_adjust_bits(struct pbn *pbn);
#endif /* PBN_H */
|