commit 0a7e36380717fe926d43ab30ef6162db9bd71723
parent 302edc17336dbd46e4040f77cb36c0f99b736743
Author: Mattias Andrée <maandree@kth.se>
Date: Fri, 4 Mar 2016 09:28:01 +0100
Add makefile and fix errors
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat:
51 files changed, 178 insertions(+), 98 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,71 @@
+include config.mk
+
+HDR =\
+ zahl.h\
+ src/internals.h
+
+FUN =\
+ zabs\
+ zadd\
+ zand\
+ zbits\
+ zbset\
+ zbtest\
+ zcmp\
+ zcmpi\
+ zcmpmag\
+ zcmpu\
+ zdiv\
+ zdivmod\
+ zfree\
+ zgcd\
+ zinit\
+ zload\
+ zlsb\
+ zlsh\
+ zmod\
+ zmodmul\
+ zmodpow\
+ zmodpowu\
+ zmodsqr\
+ zmul\
+ zneg\
+ znot\
+ zor\
+ zpow\
+ zpowu\
+ zptest\
+ zrand\
+ zrsh\
+ zsave\
+ zset\
+ zseti\
+ zsets\
+ zsetu\
+ zsetup\
+ zsplit\
+ zsqr\
+ zstr\
+ zstr_length\
+ zsub\
+ zswap\
+ ztrunc\
+ zunsetup\
+ zxor
+
+OBJ = $(FUN:=.o)
+MAN = $(foreach F,$(FUN),man/$(F).3) man/libzahl.7
+
+all: libzahl.a
+
+%.o: src/%.c $(HDR) config.mk
+ $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
+
+libzahl.a: $(OBJ)
+ $(AR) rc $@ $?
+ $(RANLIB) $@
+
+clean:
+ -rm -- *.o *.su *.a *.so 2>/dev/null
+
+.PHONY: all clean
diff --git a/config.mk b/config.mk
@@ -0,0 +1,18 @@
+VERSION = 0.0
+
+PREFIX = /usr/local
+EXECPREFIX = $(PREFIX)
+MANPREFIX = $(PREFIX)/shared/man
+
+CC = cc
+AR = ar
+RANLIB = ranlib
+
+# Unless /dev/urandom exists and is a non-blocking random number generator,
+# you have to add -DFAST_RANDOM_PATHNAME=... to CPPFLAGS, and
+# unless /dev/random exists and is a blocking secure random number generator
+# you have to add -DSECURE_RANDOM_PATHNAME=... to CPPFLAGS.
+
+CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
+CFLAGS = -std=c99 -Wall -pedantic
+LDFLAGS = -s
diff --git a/src/internals.h b/src/internals.h
@@ -3,6 +3,7 @@
#include <string.h>
#include <stdlib.h>
+#include <errno.h>
#define BITS_PER_CHAR 32
#define LB_BITS_PER_CHAR 5
@@ -61,13 +62,13 @@ extern int libzahl_set_up;
#define zmemcmp(a, b, n) memcmp(a, b, (n) * sizeof(zahl_char_t))
#define SET_SIGNUM(a, signum) ((a)->sign = (signum))
-#define SET(a, b) do { if (a != b) zset(a, b)} while (0)
+#define SET(a, b) do { if (a != b) zset(a, b); } while (0)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static inline void
-zahl_realloc(z_t *p, size_t n)
+zahl_realloc(z_t p, size_t n)
{
p->chars = realloc(p->chars, n * sizeof(zahl_char_t));
if (!p->chars)
diff --git a/src/zabs.c b/src/zabs.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zadd.c b/src/zadd.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
@@ -37,7 +37,7 @@ zadd_unsigned(z_t a, z_t b, z_t c)
a->used = b->used;
addend = c->chars;
} else {
- zmemcpy(a->chars, c->chars, c->used));
+ zmemcpy(a->chars, c->chars, c->used);
a->used = c->used;
addend = b->chars;
}
diff --git a/src/zand.c b/src/zand.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zbits.c b/src/zbits.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
size_t
diff --git a/src/zbset.c b/src/zbset.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zbtest.c b/src/zbtest.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
int
diff --git a/src/zcmp.c b/src/zcmp.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
int
diff --git a/src/zcmpi.c b/src/zcmpi.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
int
@@ -9,6 +9,6 @@ zcmpi(z_t a, long long int b)
return zsignum(a);
if (zzero(a))
return b > 0 ? -1 : b < 0;
- zseti(zahl_tmp_cmp, b);
- return zcmp(a, zahl_tmp_cmp);
+ zseti(libzahl_tmp_cmp, b);
+ return zcmp(a, libzahl_tmp_cmp);
}
diff --git a/src/zcmpmag.c b/src/zcmpmag.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
int
diff --git a/src/zcmpu.c b/src/zcmpu.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
int
@@ -9,6 +9,6 @@ zcmpu(z_t a, unsigned long long int b)
return zsignum(a);
if (zsignum(a) <= 0)
return -1;
- zsetu(zahl_tmp_cmp, b);
- return zcmp(a, zahl_tmp_cmp);
+ zsetu(libzahl_tmp_cmp, b);
+ return zcmp(a, libzahl_tmp_cmp);
}
diff --git a/src/zdiv.c b/src/zdiv.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zdivmod.c b/src/zdivmod.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#define ta libzahl_tmp_divmod_a
#define tb libzahl_tmp_divmod_b
@@ -74,13 +74,13 @@ zdivmod(z_t a, z_t b, z_t c, z_t d)
zrsh(tds[i], td, i);
for (;;) {
for (i = 0; i < BITS_PER_CHAR; i++) {
- if (zcmpmag(td[i], tb) <= 0) {
- zsub(tb, tb, td[i]);
+ if (zcmpmag(tds[i], tb) <= 0) {
+ zsub(tb, tb, tds[i]);
zbset(ta, ta, bit, 1);
}
if (!bit--)
goto done;
- zrsh(td[i], td[i], 1);
+ zrsh(tds[i], tds[i], 1);
}
for (i = MIN(bit, BITS_PER_CHAR); i--;)
zrsh(tds[i], tds[i], BITS_PER_CHAR);
diff --git a/src/zfree.c b/src/zfree.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zgcd.c b/src/zgcd.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#define u libzahl_tmp_gcd_u
#define v libzahl_tmp_gcd_v
@@ -35,7 +35,7 @@ zgcd(z_t a, z_t b, z_t c)
min = MIN(u->used, v->used);
for (; i < min; i++) {
- uv = u->chars[i] | v->used[i];
+ uv = u->chars[i] | v->chars[i];
for (bit = 1; bit; bit <<= 1, shifts++)
if (uv & bit)
goto loop_done;
diff --git a/src/zinit.c b/src/zinit.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zload.c b/src/zload.c
@@ -1,14 +1,14 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
size_t
zload(z_t a, const void *buffer)
{
const char *buf = buffer;
- a->sign = *((int *)buf), buf += sizeof(int);
- a->used = *((size_t *)buf), buf += sizeof(size_t);
- a->alloced = *((size_t *)buf), buf += sizeof(size_t);
+ a->sign = *((const int *)buf), buf += sizeof(int);
+ a->used = *((const size_t *)buf), buf += sizeof(size_t);
+ a->alloced = *((const size_t *)buf), buf += sizeof(size_t);
if (a->alloced)
zahl_realloc(a, a->alloced);
else
diff --git a/src/zlsb.c b/src/zlsb.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
size_t
diff --git a/src/zlsh.c b/src/zlsh.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
@@ -18,7 +18,7 @@ zlsh(z_t a, z_t b, size_t bits)
}
chars = FLOOR_BITS_TO_CHARS(bits);
- bits = BITS_IN_LAST_CHAR(bits)
+ bits = BITS_IN_LAST_CHAR(bits);
cbits = BITS_PER_CHAR - 1 - bits;
a->used = b->used + chars;
diff --git a/src/zmod.c b/src/zmod.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zmodmul.c b/src/zmodmul.c
@@ -1,9 +1,9 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
-zmodmul(z_t a, z_t b, z_t c)
+zmodmul(z_t a, z_t b, z_t c, z_t d)
{
/* TODO Montgomery modular multiplication */
if (a == d) {
diff --git a/src/zmodpow.c b/src/zmodpow.c
@@ -1,7 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
-
-#include <errno.h>
+#include "internals.h"
#define tb libzahl_tmp_pow_b
#define tc libzahl_tmp_pow_c
diff --git a/src/zmodpowu.c b/src/zmodpowu.c
@@ -1,7 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
-
-#include <errno.h>
+#include "internals.h"
#define tb libzahl_tmp_pow_b
#define td libzahl_tmp_pow_d
@@ -10,8 +8,6 @@
void
zmodpowu(z_t a, z_t b, unsigned long long int c, z_t d)
{
- size_t i, n;
-
if (!c) {
if (zzero(b)) {
errno = EDOM; /* Indeterminate form: 0:th power of 0 */
@@ -31,8 +27,6 @@ zmodpowu(z_t a, z_t b, unsigned long long int c, z_t d)
return;
}
- n = zbits(c);
-
zmod(tb, b, d);
zset(td, d);
zsetu(a, 1);
diff --git a/src/zmodsqr.c b/src/zmodsqr.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zmul.c b/src/zmul.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zneg.c b/src/zneg.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/znot.c b/src/znot.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
@@ -8,7 +8,7 @@ znot(z_t a, z_t b)
size_t bits, n;
if (zzero(b)) {
- SET_SIGNUM(a, 0)
+ SET_SIGNUM(a, 0);
return;
}
@@ -21,7 +21,7 @@ znot(z_t a, z_t b)
bits &= BITS_PER_CHAR - 1;
a->chars[a->used - 1] &= ((zahl_char_t)1 << bits) - 1;
- while (; a->used; a->used--)
+ for (; a->used; a->used--)
if (a->chars[a->used - 1])
break;
if (!a->used)
diff --git a/src/zor.c b/src/zor.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zpow.c b/src/zpow.c
@@ -1,7 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
-
-#include <errno.h>
+#include "internals.h"
#define tb libzahl_tmp_pow_b
#define tc libzahl_tmp_pow_c
@@ -41,7 +39,7 @@ zpow(z_t a, z_t b, z_t c)
for (i = 0; i < n; i++) {
x = tc->chars[i];
- for (j = BITS_PER_CHAR; j--; x >>= 1, j) {
+ for (j = BITS_PER_CHAR; j--; x >>= 1) {
if (x & 1)
zmul(a, a, tb);
zsqr(tb, tb);
diff --git a/src/zpowu.c b/src/zpowu.c
@@ -1,7 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
-
-#include <errno.h>
+#include "internals.h"
#define tb libzahl_tmp_pow_b
diff --git a/src/zptest.c b/src/zptest.c
@@ -1,11 +1,11 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#define x libzahl_tmp_ptest_x
#define a libzahl_tmp_ptest_a
#define d libzahl_tmp_ptest_d
#define n1 libzahl_tmp_ptest_n1
-#define n2 libzahl_tmp_ptest_n4
+#define n4 libzahl_tmp_ptest_n4
enum zprimality
@@ -39,16 +39,16 @@ zptest(z_t witness, z_t n, int t)
zrsh(d, n1, r);
while (t--) {
- zrand(a, n4, FAST_RANDOM, UNIFORM);
+ zrand(a, FAST_RANDOM, UNIFORM, n4);
zadd_unsigned(a, a, libzahl_const_2);
- zmodpow(x, a, d, tn);
+ zmodpow(x, a, d, n);
if (!zcmp(x, libzahl_const_1) || !zcmp(x, n1))
continue;
for (i = 1; i < r; i++) {
zsqr(x, x);
- zmod(x, x, tn);
+ zmod(x, x, n);
if (!zcmp(x, libzahl_const_1)) {
if (witness)
zswap(witness, a);
diff --git a/src/zrand.c b/src/zrand.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#include <fcntl.h>
#include <unistd.h>
@@ -27,12 +27,12 @@ zrand_get_random_bits(z_t r, size_t bits, int fd)
read_just = read(fd, (char *)(r->chars) + read_total, n);
if (read_just < 0)
FAILURE_JUMP();
- read_total += read_just;
- n -= read_just;
+ read_total += (size_t)read_just;
+ n -= (size_t)read_just;
}
- bit = BITS_IN_LAST_CHAR(bit)
- mask <<= bit;
+ bits = BITS_IN_LAST_CHAR(bits);
+ mask <<= bits;
mask -= 1;
r->chars[chars - 1] &= mask;
@@ -70,6 +70,8 @@ zrand(z_t r, enum zranddev dev, enum zranddist dist, z_t n)
}
fd = open(pathname, O_RDONLY);
+ if (fd < 0)
+ FAILURE_JUMP();
switch (dist) {
case QUASIUNIFORM:
diff --git a/src/zrsh.c b/src/zrsh.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
@@ -19,7 +19,7 @@ zrsh(z_t a, z_t b, size_t bits)
return;
}
- bits = BITS_IN_LAST_CHAR(bits)
+ bits = BITS_IN_LAST_CHAR(bits);
cbits = BITS_PER_CHAR - 1 - bits;
if (chars && a == b) {
@@ -28,7 +28,7 @@ zrsh(z_t a, z_t b, size_t bits)
} else if (a != b) {
a->used = b->used - chars;
if (a->alloced < a->used)
- zahl_realloc(a->chars, a->used);
+ zahl_realloc(a, a->used);
zmemcpy(a->chars, b->chars + chars, a->used);
}
diff --git a/src/zsave.c b/src/zsave.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
size_t
diff --git a/src/zset.c b/src/zset.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zseti.c b/src/zseti.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zsets.c b/src/zsets.c
@@ -1,7 +1,7 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
-#include <errno.h>
+#include <ctype.h>
int
@@ -30,7 +30,7 @@ zsets(z_t a, const char *str)
while (*str) {
#define X(n)\
case n:\
- temp *= 10, temp += *str++ & 15;\
+ temp *= 10, temp += *str++ & 15;
X(0) X(18) X(17) X(16) X(15) X(14) X(13) X(12) X(11)
X(10) X(9) X(8) X(7) X(6) X(5) X(4) X(3) X(2) X(1)
#undef X
diff --git a/src/zsetu.c b/src/zsetu.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#define SIZE_MULTIPLE(fit, in) ((sizeof(fit) + sizeof(in) - 1) / sizeof(in))
@@ -12,7 +12,7 @@ zsetu(z_t a, unsigned long long int b)
return;
}
if (a->alloced < SIZE_MULTIPLE(b, *(a->chars)))
- zahl_realloc(a, SIZE_MULTIPLE(b, *(a->chars)))
+ zahl_realloc(a, SIZE_MULTIPLE(b, *(a->chars)));
SET_SIGNUM(a, 1);
a->used = 0;
while (b) {
diff --git a/src/zsetup.c b/src/zsetup.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#define X(x) z_t x;
LIST_TEMPS
@@ -17,7 +17,7 @@ void
zsetup(jmp_buf env)
{
size_t i;
- libzahl_jmp_buf = jmp_buf;
+ *libzahl_jmp_buf = *env;
if (!libzahl_set_up) {
libzahl_set_up = 1;
diff --git a/src/zsplit.c b/src/zsplit.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zsqr.c b/src/zsqr.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zstr.c b/src/zstr.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#include <stdio.h>
@@ -39,7 +39,7 @@ zstr(z_t a, char *b)
neg = zsignum(a) < 0;
zabs(num, a);
- n -= neg;
+ n -= (size_t)neg;
n = n > 9 ? (n - 9) : 0;
b[0] = '-';
b += neg;
@@ -53,7 +53,7 @@ zstr(z_t a, char *b)
overridden = b[n + (9 - 1)];
n = n > 9 ? (n - 9) : 0;
} else {
- n += sprintf(b + n, "%lu", (unsigned long)(rem->chars[0]));
+ n += (size_t)sprintf(b + n, "%lu", (unsigned long)(rem->chars[0]));
b[n] = overridden;
break;
}
diff --git a/src/zstr_length.c b/src/zstr_length.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
#define num libzahl_tmp_str_num
#define mag libzahl_tmp_str_mag
diff --git a/src/zsub.c b/src/zsub.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zswap.c b/src/zswap.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
@@ -8,5 +8,5 @@ zswap(z_t a, z_t b)
z_t t;
*t = *a;
*a = *b;
- *b = t;
+ *b = *t;
}
diff --git a/src/ztrunc.c b/src/ztrunc.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
diff --git a/src/zunsetup.c b/src/zunsetup.c
@@ -1,9 +1,9 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
-zunsetup(jmp_buf env)
+zunsetup(void)
{
size_t i;
if (libzahl_set_up) {
diff --git a/src/zxor.c b/src/zxor.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "internals"
+#include "internals.h"
void
@@ -47,5 +47,5 @@ zxor(z_t a, z_t b, z_t c)
a->chars[n] ^= b->chars[n];
}
- SET_SIGNUM(a, 1 - 2 * ((zsignum(b) ^ zsignum(c)) < 0);
+ SET_SIGNUM(a, 1 - 2 * ((zsignum(b) ^ zsignum(c)) < 0));
}
diff --git a/zahl.h b/zahl.h
@@ -79,7 +79,7 @@ void zabs(z_t, z_t); /* a := |b| */
void zpow(z_t, z_t, z_t); /* a := b ↑ c */
void zmodpow(z_t, z_t, z_t, z_t); /* a := (b ↑ c) % d */
void zpowu(z_t, z_t, unsigned long long int);
-void zmodpowu(z_t, z_t, z_t, unsigned long long int);
+void zmodpowu(z_t, z_t, unsigned long long int, z_t);
/* These are used internally and may be removed in a future version. */
void zadd_unsigned(z_t, z_t, z_t); /* a := |b| + |c|, b and c must not be the same reference. */