summaryrefslogtreecommitdiffstats
path: root/pbn.h
diff options
context:
space:
mode:
Diffstat (limited to 'pbn.h')
-rw-r--r--pbn.h78
1 files changed, 78 insertions, 0 deletions
diff --git a/pbn.h b/pbn.h
new file mode 100644
index 0000000..1dae30b
--- /dev/null
+++ b/pbn.h
@@ -0,0 +1,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 */