|
Packit |
5c3484 |
/* Test mpf_eq.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright 2009, 2012 Free Software Foundation, Inc.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
This file is part of the GNU MP Library test suite.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is free software; you can redistribute it
|
|
Packit |
5c3484 |
and/or modify it under the terms of the GNU General Public License as
|
|
Packit |
5c3484 |
published by the Free Software Foundation; either version 3 of the License,
|
|
Packit |
5c3484 |
or (at your option) any later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is distributed in the hope that it will be
|
|
Packit |
5c3484 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5c3484 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit |
5c3484 |
Public License for more details.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
You should have received a copy of the GNU General Public License along with
|
|
Packit |
5c3484 |
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include <stdio.h>
|
|
Packit |
5c3484 |
#include <stdlib.h>
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include "gmp.h"
|
|
Packit |
5c3484 |
#include "gmp-impl.h"
|
|
Packit |
5c3484 |
#include "tests.h"
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#define SZ (2 * sizeof(mp_limb_t))
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void insert_random_low_zero_limbs (mpf_t, gmp_randstate_ptr);
|
|
Packit |
5c3484 |
void dump_abort (mpf_t, mpf_t, int, int, int, int, int, long);
|
|
Packit |
5c3484 |
void hexdump (mpf_t);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
check_data (void)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
static const struct
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
struct {
|
|
Packit |
5c3484 |
int exp, size;
|
|
Packit |
5c3484 |
mp_limb_t d[10];
|
|
Packit |
5c3484 |
} x, y;
|
|
Packit |
5c3484 |
mp_bitcnt_t bits;
|
|
Packit |
5c3484 |
int want;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
} data[] = {
|
|
Packit |
5c3484 |
{ { 0, 0, { 0 } }, { 0, 0, { 0 } }, 0, 1 },
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
{ { 0, 1, { 7 } }, { 0, 1, { 7 } }, 0, 1 },
|
|
Packit |
5c3484 |
{ { 0, 1, { 7 } }, { 0, 1, { 7 } }, 17, 1 },
|
|
Packit |
5c3484 |
{ { 0, 1, { 7 } }, { 0, 1, { 7 } }, 4711, 1 },
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
{ { 0, 1, { 7 } }, { 0, 1, { 6 } }, 0, 1 },
|
|
Packit |
5c3484 |
{ { 0, 1, { 7 } }, { 0, 1, { 6 } }, 2, 1 },
|
|
Packit |
5c3484 |
{ { 0, 1, { 7 } }, { 0, 1, { 6 } }, 3, 0 },
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
{ { 0, 0, { 0 } }, { 0, 1, { 1 } }, 0, 0 },
|
|
Packit |
5c3484 |
{ { 0, 1, { 1 } }, { 0,-1 ,{ 1 } }, 0, 0 },
|
|
Packit |
5c3484 |
{ { 1, 1, { 1 } }, { 0, 1, { 1 } }, 0, 0 },
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
{ { 0, 1, { 8 } }, { 0, 1, { 4 } }, 0, 0 },
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
{ { 0, 2, { 0, 3 } }, { 0, 1, { 3 } }, 1000, 1 },
|
|
Packit |
5c3484 |
};
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpf_t x, y;
|
|
Packit |
5c3484 |
int got, got_swapped;
|
|
Packit |
5c3484 |
int i;
|
|
Packit |
5c3484 |
mp_trace_base = 16;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
for (i = 0; i < numberof (data); i++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
PTR(x) = (mp_ptr) data[i].x.d;
|
|
Packit |
5c3484 |
SIZ(x) = data[i].x.size;
|
|
Packit |
5c3484 |
EXP(x) = data[i].x.exp;
|
|
Packit |
5c3484 |
PREC(x) = numberof (data[i].x.d);
|
|
Packit |
5c3484 |
MPF_CHECK_FORMAT (x);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
PTR(y) = (mp_ptr) data[i].y.d;
|
|
Packit |
5c3484 |
SIZ(y) = data[i].y.size;
|
|
Packit |
5c3484 |
EXP(y) = data[i].y.exp;
|
|
Packit |
5c3484 |
PREC(y) = numberof (data[i].y.d);
|
|
Packit |
5c3484 |
MPF_CHECK_FORMAT (y);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
got = mpf_eq (x, y, data[i].bits);
|
|
Packit |
5c3484 |
got_swapped = mpf_eq (y, x, data[i].bits);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (got != got_swapped || got != data[i].want)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
printf ("check_data() wrong result at data[%d]\n", i);
|
|
Packit |
5c3484 |
mpf_trace ("x ", x);
|
|
Packit |
5c3484 |
mpf_trace ("y ", y);
|
|
Packit |
5c3484 |
printf ("got %d\n", got);
|
|
Packit |
5c3484 |
printf ("got_swapped %d\n", got_swapped);
|
|
Packit |
5c3484 |
printf ("want %d\n", data[i].want);
|
|
Packit |
5c3484 |
abort ();
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
check_random (long reps)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
unsigned long test;
|
|
Packit |
5c3484 |
gmp_randstate_ptr rands = RANDS;
|
|
Packit |
5c3484 |
mpf_t a, b, x;
|
|
Packit |
5c3484 |
mpz_t ds;
|
|
Packit |
5c3484 |
int hibits, lshift1, lshift2;
|
|
Packit |
5c3484 |
int xtra;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#define HIBITS 10
|
|
Packit |
5c3484 |
#define LSHIFT1 10
|
|
Packit |
5c3484 |
#define LSHIFT2 10
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpf_set_default_prec ((1 << HIBITS) + (1 << LSHIFT1) + (1 << LSHIFT2));
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_init (ds);
|
|
Packit |
5c3484 |
mpf_inits (a, b, x, NULL);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
for (test = 0; test < reps; test++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_urandomb (ds, rands, HIBITS);
|
|
Packit |
5c3484 |
hibits = mpz_get_ui (ds) + 1;
|
|
Packit |
5c3484 |
mpz_urandomb (ds, rands, hibits);
|
|
Packit |
5c3484 |
mpz_setbit (ds, hibits - 1); /* make sure msb is set */
|
|
Packit |
5c3484 |
mpf_set_z (a, ds);
|
|
Packit |
5c3484 |
mpf_set_z (b, ds);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_urandomb (ds, rands, LSHIFT1);
|
|
Packit |
5c3484 |
lshift1 = mpz_get_ui (ds);
|
|
Packit |
5c3484 |
mpf_mul_2exp (a, a, lshift1 + 1);
|
|
Packit |
5c3484 |
mpf_mul_2exp (b, b, lshift1 + 1);
|
|
Packit |
5c3484 |
mpf_add_ui (a, a, 1); /* make a one-bit difference */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_urandomb (ds, rands, LSHIFT2);
|
|
Packit |
5c3484 |
lshift2 = mpz_get_ui (ds);
|
|
Packit |
5c3484 |
mpf_mul_2exp (a, a, lshift2);
|
|
Packit |
5c3484 |
mpf_mul_2exp (b, b, lshift2);
|
|
Packit |
5c3484 |
mpz_urandomb (ds, rands, lshift2);
|
|
Packit |
5c3484 |
mpf_set_z (x, ds);
|
|
Packit |
5c3484 |
mpf_add (a, a, x);
|
|
Packit |
5c3484 |
mpf_add (b, b, x);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
insert_random_low_zero_limbs (a, rands);
|
|
Packit |
5c3484 |
insert_random_low_zero_limbs (b, rands);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (mpf_eq (a, b, lshift1 + hibits) == 0 ||
|
|
Packit |
5c3484 |
mpf_eq (b, a, lshift1 + hibits) == 0)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
dump_abort (a, b, lshift1 + hibits, lshift1, lshift2, hibits, 1, test);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
for (xtra = 1; xtra < 100; xtra++)
|
|
Packit |
5c3484 |
if (mpf_eq (a, b, lshift1 + hibits + xtra) != 0 ||
|
|
Packit |
5c3484 |
mpf_eq (b, a, lshift1 + hibits + xtra) != 0)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
dump_abort (a, b, lshift1 + hibits + xtra, lshift1, lshift2, hibits, 0, test);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpf_clears (a, b, x, NULL);
|
|
Packit |
5c3484 |
mpz_clear (ds);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
insert_random_low_zero_limbs (mpf_t x, gmp_randstate_ptr rands)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mp_size_t max = PREC(x) - SIZ(x);
|
|
Packit |
5c3484 |
mp_size_t s;
|
|
Packit |
5c3484 |
mpz_t ds; mpz_init (ds);
|
|
Packit |
5c3484 |
mpz_urandomb (ds, rands, 32);
|
|
Packit |
5c3484 |
s = mpz_get_ui (ds) % (max + 1);
|
|
Packit |
5c3484 |
MPN_COPY_DECR (PTR(x) + s, PTR(x), SIZ(x));
|
|
Packit |
5c3484 |
MPN_ZERO (PTR(x), s);
|
|
Packit |
5c3484 |
SIZ(x) += s;
|
|
Packit |
5c3484 |
mpz_clear (ds);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
dump_abort (mpf_t a, mpf_t b, int cmp_prec, int lshift1, int lshift2, int hibits, int want, long test)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
printf ("ERROR in test %ld\n", test);
|
|
Packit |
5c3484 |
printf ("want %d got %d from mpf_eq\n", want, 1-want);
|
|
Packit |
5c3484 |
printf ("cmp_prec = %d\n", cmp_prec);
|
|
Packit |
5c3484 |
printf ("lshift1 = %d\n", lshift1);
|
|
Packit |
5c3484 |
printf ("lshift2 = %d\n", lshift2);
|
|
Packit |
5c3484 |
printf ("hibits = %d\n", hibits);
|
|
Packit |
5c3484 |
hexdump (a); puts ("");
|
|
Packit |
5c3484 |
hexdump (b); puts ("");
|
|
Packit |
5c3484 |
abort ();
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
hexdump (mpf_t x)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mp_size_t i;
|
|
Packit |
5c3484 |
for (i = ABSIZ(x) - 1; i >= 0; i--)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
gmp_printf ("%0*MX", SZ, PTR(x)[i]);
|
|
Packit |
5c3484 |
if (i != 0)
|
|
Packit |
5c3484 |
printf (" ");
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
int
|
|
Packit |
5c3484 |
main (int argc, char *argv[])
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
long reps = 10000;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (argc == 2)
|
|
Packit |
5c3484 |
reps = strtol (argv[1], 0, 0);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
tests_start ();
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
check_data ();
|
|
Packit |
5c3484 |
check_random (reps);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
tests_end ();
|
|
Packit |
5c3484 |
exit (0);
|
|
Packit |
5c3484 |
}
|