summaryrefslogtreecommitdiffstats
path: root/mpn.h
blob: 98f488397a6dee1fd075a2df2184c3ef61830d19 (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.
 *
 * ----------------------------------------------------------------------- */

/*
 * mpn.h
 *
 * Simple multi-precision signed integer library
 */

#ifndef MPN_H
#define MPN_H

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>

/* Configurables */

#define MPN_ATOM_BITS	32
#define MPN_ATOM_xFMT	"08"PRIx32
typedef uint32_t mpn_atom_t;
typedef uint64_t mpn_2atom_t;
typedef int32_t  mpn_satom_t;
typedef int64_t  mpn_2satom_t;

struct mpn {
    int ref;
    int len;
    int bits;
    int minus;
    mpn_atom_t num[1];
};


#define _mpn_malloc(x)    malloc(x)
#define _mpn_zalloc(x)    calloc(x,1)
#define _mpn_free(x)      free(x)
#define _mpn_realloc(x,y) realloc(x,y)

#define mpn_ref(x) ((x)->ref++, (x))

#if defined(__GNUC__) && !defined(MPN_INIT)
extern inline void mpn_free(struct mpn *__mpn)
{
    if (! --__mpn->ref)
	_mpn_free(__mpn);
}
#else
void mpn_free(struct mpn *);
#endif
struct mpn *mpn_addsub(struct mpn *, struct mpn *, int issub);
#define mpn_add(x,y) mpn_addsub(x,y,0)
#define mpn_sub(x,y) mpn_addsub(x,y,1)
int mpn_cmp(struct mpn *, struct mpn *);
struct mpn *mpn_mul(struct mpn *, struct mpn *);
struct mpn *mpn_shr(struct mpn *, int);
struct mpn *mpn_shl(struct mpn *, int);
void mpn_dump(FILE *, struct mpn *);
struct mpn *mpn_int(mpn_satom_t);
struct mpn *mpn_new(int);
struct mpn *mpn_dup(const struct mpn *);
struct mpn *mpn_dupx(const struct mpn *, int);
struct mpn *mpn_cow(struct mpn *, int);

/* Internal functions */
void _mpn_adjust_bits(struct mpn *mpn);

#endif /* MPN_H */