[Gmp-commit] /var/hg/gmp-proj/mini-gmp: Test for mpz_tdiv_q_2exp.
mercurial at gmplib.org
mercurial at gmplib.org
Fri Jan 13 19:21:51 CET 2012
details: /var/hg/gmp-proj/mini-gmp/rev/ce396c502706
changeset: 76:ce396c502706
user: Niels M?ller <nisse at lysator.liu.se>
date: Fri Jan 13 19:21:44 2012 +0100
description:
Test for mpz_tdiv_q_2exp.
diffstat:
.hgignore | 1 +
tests/Makefile | 5 ++-
tests/hex-random.c | 18 ++++++++++++
tests/hex-random.h | 12 ++++++--
tests/t-div_2exp.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 106 insertions(+), 5 deletions(-)
diffs (162 lines):
diff -r a0c947043620 -r ce396c502706 .hgignore
--- a/.hgignore Fri Jan 13 17:43:17 2012 +0100
+++ b/.hgignore Fri Jan 13 19:21:44 2012 +0100
@@ -8,6 +8,7 @@
^tests/t-mul$
^tests/t-invert$
^tests/t-div$
+^tests/t-div_2exp$
^tests/t-double$
^tests/t-gcd$
^tests/t-lcm$
diff -r a0c947043620 -r ce396c502706 tests/Makefile
--- a/tests/Makefile Fri Jan 13 17:43:17 2012 +0100
+++ b/tests/Makefile Fri Jan 13 19:21:44 2012 +0100
@@ -7,7 +7,8 @@
LIBS = -lgmp -lm -lmcheck
-CHECK_PROGRAMS = t-add t-sub t-mul t-invert t-div t-double t-gcd \
+CHECK_PROGRAMS = t-add t-sub t-mul t-invert t-div t-div_2exp \
+ t-double t-gcd \
t-sqrt t-powm t-logops t-bitops t-str t-lcm
MISC_OBJS = hex-random.o mini-random.o mini-gmp.o
@@ -23,7 +24,7 @@
# Keep object files
.PRECIOUS: %.o
-%.o: %.c ../mini-gmp.h
+%.o: %.c ../mini-gmp.h hex-random.h mini-random.h
$(CC) $(CFLAGS) -c $< -o $@
mini-gmp.o: ../mini-gmp.c ../mini-gmp.h
diff -r a0c947043620 -r ce396c502706 tests/hex-random.c
--- a/tests/hex-random.c Fri Jan 13 17:43:17 2012 +0100
+++ b/tests/hex-random.c Fri Jan 13 19:21:44 2012 +0100
@@ -296,6 +296,24 @@
mpz_set (r, a);
mpz_combit (r, bbits);
break;
+ case OP_CDIV_Q_2:
+ mpz_cdiv_q_2exp (r, a, bbits);
+ break;
+ case OP_CDIV_R_2:
+ mpz_cdiv_r_2exp (r, a, bbits);
+ break;
+ case OP_FDIV_Q_2:
+ mpz_fdiv_q_2exp (r, a, bbits);
+ break;
+ case OP_FDIV_R_2:
+ mpz_fdiv_r_2exp (r, a, bbits);
+ break;
+ case OP_TDIV_Q_2:
+ mpz_tdiv_q_2exp (r, a, bbits);
+ break;
+ case OP_TDIV_R_2:
+ mpz_tdiv_r_2exp (r, a, bbits);
+ break;
}
gmp_asprintf (ap, "%Zx", a);
diff -r a0c947043620 -r ce396c502706 tests/hex-random.h
--- a/tests/hex-random.h Fri Jan 13 17:43:17 2012 +0100
+++ b/tests/hex-random.h Fri Jan 13 19:21:44 2012 +0100
@@ -17,9 +17,15 @@
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
-enum hex_random_op { OP_ADD, OP_SUB, OP_MUL, OP_CDIV, OP_FDIV, OP_TDIV,
- OP_GCD, OP_LCM, OP_POWM, OP_AND, OP_IOR, OP_XOR,
- OP_SETBIT, OP_CLRBIT, OP_COMBIT };
+enum hex_random_op
+ {
+ OP_ADD, OP_SUB, OP_MUL, OP_CDIV, OP_FDIV, OP_TDIV,
+ OP_CDIV_Q_2, OP_CDIV_R_2,
+ OP_FDIV_Q_2, OP_FDIV_R_2,
+ OP_TDIV_Q_2, OP_TDIV_R_2,
+ OP_GCD, OP_LCM, OP_POWM, OP_AND, OP_IOR, OP_XOR,
+ OP_SETBIT, OP_CLRBIT, OP_COMBIT
+ };
void hex_random_init (void);
char *hex_urandomb (unsigned long bits);
diff -r a0c947043620 -r ce396c502706 tests/t-div_2exp.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/t-div_2exp.c Fri Jan 13 19:21:44 2012 +0100
@@ -0,0 +1,75 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "mini-random.h"
+
+#define MAXBITS 400
+#define COUNT 10000
+
+static void
+dump (const char *label, const mpz_t x)
+{
+ char *buf = mpz_get_str (NULL, 16, x);
+ fprintf (stderr, "%s: %s\n", label, buf);
+ free (buf);
+}
+
+typedef void div_func (mpz_t, const mpz_t, mp_bitcnt_t);
+
+int
+main (int argc, char **argv)
+{
+ unsigned i;
+ mpz_t a, res, ref;
+ mp_bitcnt_t b;
+
+ hex_random_init ();
+
+ mpz_init (a);
+ mpz_init (res);
+ mpz_init (ref);
+
+ for (i = 0; i < COUNT; i++)
+ {
+ unsigned j;
+ for (j = 0; j < 1; j++)
+ {
+ static const enum hex_random_op ops[6] =
+ {
+ /* OP_CDIV_Q_2, OP_CDIV_R_2,
+ OP_FDIV_Q_2, OP_FDIV_R_2, */
+ OP_TDIV_Q_2, /* OP_TDIV_R_2 */
+ };
+ static const char *name[6] =
+ {
+ /* "cdiv_q", "cdiv_r",
+ "fdiv_q", "fdiv_r", */
+ "tdiv_q", /* "tdiv_r" */
+ };
+ static div_func * const div [6] =
+ {
+ /* mpz_cdiv_q_2exp, mpz_cdiv_r_2exp,
+ mpz_fdiv_q_2exp, mpz_fdiv_r_2exp, */
+ mpz_tdiv_q_2exp, /* mpz_tdiv_r_2exp */
+ };
+
+ mini_random_bit_op (ops[j], MAXBITS, a, &b, ref);
+ div[j] (res, a, b);
+ if (mpz_cmp (ref, res))
+ {
+ fprintf (stderr, "mpz_%s_2exp failed:\n", name[j]);
+ dump ("a", a);
+ fprintf (stderr, "b: %lu\n", b);
+ dump ("r", res);
+ dump ("ref", ref);
+ abort ();
+ }
+ }
+ }
+ mpz_clear (a);
+ mpz_clear (res);
+ mpz_clear (ref);
+
+ return 0;
+}
More information about the gmp-commit
mailing list