summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2007-10-12 22:47:02 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2007-10-12 22:47:02 (GMT)
commit1981484ab028768afa3b2b0b746a00e6ed739891 (patch)
tree322061b2e5db68bec9c88eaf6a6340b04df8091e
parentd1e50094aeeac30936cdea9c4109db800a1ef203 (diff)
downloadpbn-1981484ab028768afa3b2b0b746a00e6ed739891.zip
pbn-1981484ab028768afa3b2b0b746a00e6ed739891.tar.gz
pbn-1981484ab028768afa3b2b0b746a00e6ed739891.tar.bz2
pbn-1981484ab028768afa3b2b0b746a00e6ed739891.tar.xz
Add pbn_abs()
Add pbn_abs() to return the absolute value of a pbn.
-rw-r--r--Makefile3
-rw-r--r--pbn.h1
-rw-r--r--pbn_abs.c27
3 files changed, 30 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index e00843b..5aa0d90 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,8 @@ CFLAGS = -g -D_FORTIFY_SOURCE=2 -W -Wall
LDFLAGS =
LIBOBJ = pbn_add.o pbn_cmp.o pbn_dump.o pbn_init.o pbn_mul.o pbn_shift.o \
- pbn_and.o pbn_or.o pbn_xor.o pbn_bit.o
+ pbn_and.o pbn_or.o pbn_xor.o pbn_bit.o \
+ pbn_abs.o
TESTS = test
diff --git a/pbn.h b/pbn.h
index 491cd9d..aab69b6 100644
--- a/pbn.h
+++ b/pbn.h
@@ -63,6 +63,7 @@ struct pbn *pbn_addsub(struct pbn *, struct pbn *, int issub);
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_abs(struct pbn *);
struct pbn *pbn_shr(struct pbn *, int);
struct pbn *pbn_shl(struct pbn *, int);
diff --git a/pbn_abs.c b/pbn_abs.c
new file mode 100644
index 0000000..e78a77f
--- /dev/null
+++ b/pbn_abs.c
@@ -0,0 +1,27 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * 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_abs.c
+ */
+
+#include "pbnint.h"
+
+struct pbn *pbn_abs(struct pbn *pbn)
+{
+ if (!pbn->minus)
+ return pbn;
+
+ pbn = pbn_cow(pbn, pbn->len);
+ pbn->minus = 0;
+ return pbn;
+}