summaryrefslogtreecommitdiffstats
path: root/pbn.h
blob: 1dae30b636531a5c00509c6f8e94597db2f62777 (plain)
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
/* ----------------------------------------------------------------------- *
 *   
 *   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_ATOM_BITS	32
#define PBN_ATOM_xFMT	"08"PRIx32
typedef uint32_t pbn_atom_t;
typedef uint64_t pbn_2atom_t;
typedef int32_t  pbn_satom_t;
typedef int64_t  pbn_2satom_t;

struct pbn {
    int ref;
    int len;
    int bits;
    int minus;
    pbn_atom_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))

#if defined(__GNUC__) && !defined(PBN_INIT)
extern inline void pbn_free(struct pbn *__pbn)
{
    if (! --__pbn->ref)
	_pbn_free(__pbn);
}
#else
void pbn_free(struct pbn *);
#endif
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)
int pbn_cmp(struct pbn *, struct pbn *);
struct pbn *pbn_mul(struct pbn *, struct pbn *);
struct pbn *pbn_shr(struct pbn *, int);
struct pbn *pbn_shl(struct pbn *, int);
void pbn_dump(FILE *, struct pbn *);
struct pbn *pbn_int(pbn_satom_t);
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 */
void _pbn_adjust_bits(struct pbn *pbn);

#endif /* PBN_H */