1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/* Test mpf_eq.
Copyright 2009, 2012 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"
#define SZ (2 * sizeof(mp_limb_t))
void insert_random_low_zero_limbs (mpf_t, gmp_randstate_ptr);
void dump_abort (mpf_t, mpf_t, int, int, int, int, int, long);
void hexdump (mpf_t);
void
check_data (void)
{
static const struct
{
struct {
int exp, size;
mp_limb_t d[10];
} x, y;
mp_bitcnt_t bits;
int want;
} data[] = {
{ { 0, 0, { 0 } }, { 0, 0, { 0 } }, 0, 1 },
{ { 0, 1, { 7 } }, { 0, 1, { 7 } }, 0, 1 },
{ { 0, 1, { 7 } }, { 0, 1, { 7 } }, 17, 1 },
{ { 0, 1, { 7 } }, { 0, 1, { 7 } }, 4711, 1 },
{ { 0, 1, { 7 } }, { 0, 1, { 6 } }, 0, 1 },
{ { 0, 1, { 7 } }, { 0, 1, { 6 } }, 2, 1 },
{ { 0, 1, { 7 } }, { 0, 1, { 6 } }, 3, 0 },
{ { 0, 0, { 0 } }, { 0, 1, { 1 } }, 0, 0 },
{ { 0, 1, { 1 } }, { 0,-1 ,{ 1 } }, 0, 0 },
{ { 1, 1, { 1 } }, { 0, 1, { 1 } }, 0, 0 },
{ { 0, 1, { 8 } }, { 0, 1, { 4 } }, 0, 0 },
{ { 0, 2, { 0, 3 } }, { 0, 1, { 3 } }, 1000, 1 },
};
mpf_t x, y;
int got, got_swapped;
int i;
mp_trace_base = 16;
for (i = 0; i < numberof (data); i++)
{
PTR(x) = (mp_ptr) data[i].x.d;
SIZ(x) = data[i].x.size;
EXP(x) = data[i].x.exp;
PREC(x) = numberof (data[i].x.d);
MPF_CHECK_FORMAT (x);
PTR(y) = (mp_ptr) data[i].y.d;
SIZ(y) = data[i].y.size;
EXP(y) = data[i].y.exp;
PREC(y) = numberof (data[i].y.d);
MPF_CHECK_FORMAT (y);
got = mpf_eq (x, y, data[i].bits);
got_swapped = mpf_eq (y, x, data[i].bits);
if (got != got_swapped || got != data[i].want)
{
printf ("check_data() wrong result at data[%d]\n", i);
mpf_trace ("x ", x);
mpf_trace ("y ", y);
printf ("got %d\n", got);
printf ("got_swapped %d\n", got_swapped);
printf ("want %d\n", data[i].want);
abort ();
}
}
}
void
check_random (long reps)
{
unsigned long test;
gmp_randstate_ptr rands = RANDS;
mpf_t a, b, x;
mpz_t ds;
int hibits, lshift1, lshift2;
int xtra;
#define HIBITS 10
#define LSHIFT1 10
#define LSHIFT2 10
mpf_set_default_prec ((1 << HIBITS) + (1 << LSHIFT1) + (1 << LSHIFT2));
mpz_init (ds);
mpf_inits (a, b, x, NULL);
for (test = 0; test < reps; test++)
{
mpz_urandomb (ds, rands, HIBITS);
hibits = mpz_get_ui (ds) + 1;
mpz_urandomb (ds, rands, hibits);
mpz_setbit (ds, hibits - 1); /* make sure msb is set */
mpf_set_z (a, ds);
mpf_set_z (b, ds);
mpz_urandomb (ds, rands, LSHIFT1);
lshift1 = mpz_get_ui (ds);
mpf_mul_2exp (a, a, lshift1 + 1);
mpf_mul_2exp (b, b, lshift1 + 1);
mpf_add_ui (a, a, 1); /* make a one-bit difference */
mpz_urandomb (ds, rands, LSHIFT2);
lshift2 = mpz_get_ui (ds);
mpf_mul_2exp (a, a, lshift2);
mpf_mul_2exp (b, b, lshift2);
mpz_urandomb (ds, rands, lshift2);
mpf_set_z (x, ds);
mpf_add (a, a, x);
mpf_add (b, b, x);
insert_random_low_zero_limbs (a, rands);
insert_random_low_zero_limbs (b, rands);
if (mpf_eq (a, b, lshift1 + hibits) == 0 ||
mpf_eq (b, a, lshift1 + hibits) == 0)
{
dump_abort (a, b, lshift1 + hibits, lshift1, lshift2, hibits, 1, test);
}
for (xtra = 1; xtra < 100; xtra++)
if (mpf_eq (a, b, lshift1 + hibits + xtra) != 0 ||
mpf_eq (b, a, lshift1 + hibits + xtra) != 0)
{
dump_abort (a, b, lshift1 + hibits + xtra, lshift1, lshift2, hibits, 0, test);
}
}
mpf_clears (a, b, x, NULL);
mpz_clear (ds);
}
void
insert_random_low_zero_limbs (mpf_t x, gmp_randstate_ptr rands)
{
mp_size_t max = PREC(x) - SIZ(x);
mp_size_t s;
mpz_t ds; mpz_init (ds);
mpz_urandomb (ds, rands, 32);
s = mpz_get_ui (ds) % (max + 1);
MPN_COPY_DECR (PTR(x) + s, PTR(x), SIZ(x));
MPN_ZERO (PTR(x), s);
SIZ(x) += s;
mpz_clear (ds);
}
void
dump_abort (mpf_t a, mpf_t b, int cmp_prec, int lshift1, int lshift2, int hibits, int want, long test)
{
printf ("ERROR in test %ld\n", test);
printf ("want %d got %d from mpf_eq\n", want, 1-want);
printf ("cmp_prec = %d\n", cmp_prec);
printf ("lshift1 = %d\n", lshift1);
printf ("lshift2 = %d\n", lshift2);
printf ("hibits = %d\n", hibits);
hexdump (a); puts ("");
hexdump (b); puts ("");
abort ();
}
void
hexdump (mpf_t x)
{
mp_size_t i;
for (i = ABSIZ(x) - 1; i >= 0; i--)
{
gmp_printf ("%0*MX", SZ, PTR(x)[i]);
if (i != 0)
printf (" ");
}
}
int
main (int argc, char *argv[])
{
long reps = 10000;
if (argc == 2)
reps = strtol (argv[1], 0, 0);
tests_start ();
check_data ();
check_random (reps);
tests_end ();
exit (0);
}
|