Add Chromium-only Blender WebEngine parity work

This commit is contained in:
mes123456
2026-08-12 04:47:48 -04:00
commit 9fd26010f6
18225 changed files with 11622124 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
## Process this file with automake to generate Makefile.in
# Copyright 1996, 1999-2004, 2015 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/.
AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/tests
AM_LDFLAGS = -no-install
LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la
check_PROGRAMS = t-dm2exp t-conv t-add t-sub t-sqrt t-sqrt_ui t-muldiv reuse \
t-cmp_d t-cmp_si t-div t-fits t-get_d t-get_d_2exp \
t-get_si t-get_ui t-gsprec t-inp_str t-int_p t-mul_ui \
t-set t-set_q t-set_si t-set_ui t-trunc t-ui_div t-eq t-pow_ui
TESTS = $(check_PROGRAMS)
$(top_builddir)/tests/libtests.la:
cd $(top_builddir)/tests; $(MAKE) $(AM_MAKEFLAGS) libtests.la

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,218 @@
/* Test that routines allow reusing a source variable as destination.
Copyright 1996, 2000-2002, 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 <string.h>
#include "gmp-impl.h"
#include "tests.h"
#if __GMP_LIBGMP_DLL
/* FIXME: When linking to a DLL libgmp, mpf_add etc can't be used as
initializers for global variables because they're effectively global
variables (function pointers) themselves. Perhaps calling a test
function successively with mpf_add etc would be better. */
int
main (void)
{
printf ("Test suppressed for windows DLL\n");
exit (0);
}
#else /* ! DLL_EXPORT */
#ifndef SIZE
#define SIZE 16
#endif
#ifndef EXPO
#define EXPO 32
#endif
void dump_abort (const char *, mpf_t, mpf_t);
typedef void (*dss_func) (mpf_ptr, mpf_srcptr, mpf_srcptr);
dss_func dss_funcs[] =
{
mpf_div, mpf_add, mpf_mul, mpf_sub,
};
const char *dss_func_names[] =
{
"mpf_div", "mpf_add", "mpf_mul", "mpf_sub",
};
typedef void (*dsi_func) (mpf_ptr, mpf_srcptr, unsigned long int);
dsi_func dsi_funcs[] =
{
mpf_div_ui, mpf_add_ui, mpf_mul_ui, mpf_sub_ui,
mpf_mul_2exp, mpf_div_2exp, mpf_pow_ui
};
const char *dsi_func_names[] =
{
"mpf_div_ui", "mpf_add_ui", "mpf_mul_ui", "mpf_sub_ui",
"mpf_mul_2exp", "mpf_div_2exp", "mpf_pow_ui"
};
typedef void (*dis_func) (mpf_ptr, unsigned long int, mpf_srcptr);
dis_func dis_funcs[] =
{
mpf_ui_div, mpf_ui_sub,
};
const char *dis_func_names[] =
{
"mpf_ui_div", "mpf_ui_sub",
};
int
main (int argc, char **argv)
{
int i;
int pass, reps = 10000;
mpf_t in1, in2, out1;
unsigned long int in1i, in2i;
mpf_t res1, res2, res3;
mp_size_t bprec = 100;
tests_start ();
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init (in1);
mpf_init (in2);
mpf_init (out1);
mpf_init (res1);
mpf_init (res2);
mpf_init (res3);
for (pass = 1; pass <= reps; pass++)
{
mpf_random2 (in1, urandom () % SIZE - SIZE/2, urandom () % EXPO);
mpf_random2 (in2, urandom () % SIZE - SIZE/2, urandom () % EXPO);
for (i = 0; i < sizeof (dss_funcs) / sizeof (dss_func); i++)
{
/* Don't divide by 0. */
if (i == 0 && mpf_cmp_ui (in2, 0) == 0)
continue;
(dss_funcs[i]) (res1, in1, in2);
mpf_set (out1, in1);
(dss_funcs[i]) (out1, out1, in2);
mpf_set (res2, out1);
mpf_set (out1, in2);
(dss_funcs[i]) (out1, in1, out1);
mpf_set (res3, out1);
if (mpf_cmp (res1, res2) != 0)
dump_abort (dss_func_names[i], res1, res2);
if (mpf_cmp (res1, res3) != 0)
dump_abort (dss_func_names[i], res1, res3);
}
in2i = urandom ();
for (i = 0; i < sizeof (dsi_funcs) / sizeof (dsi_func); i++)
{
unsigned long this_in2i = in2i;
/* Don't divide by 0. */
if (i == 0 && this_in2i == 0) /* dsi_funcs[i] == mpf_div_ui */
continue;
/* Avoid overflow/underflow in the exponent. */
if (dsi_funcs[i] == mpf_mul_2exp || dsi_funcs[i] == mpf_div_2exp)
this_in2i %= 0x100000;
else if (dsi_funcs[i] == mpf_pow_ui)
this_in2i %= 0x1000;
(dsi_funcs[i]) (res1, in1, this_in2i);
mpf_set (out1, in1);
(dsi_funcs[i]) (out1, out1, this_in2i);
mpf_set (res2, out1);
if (mpf_cmp (res1, res2) != 0)
dump_abort (dsi_func_names[i], res1, res2);
}
in1i = urandom ();
for (i = 0; i < sizeof (dis_funcs) / sizeof (dis_func); i++)
{
/* Don't divide by 0. */
if (i == 0 /* dis_funcs[i] == mpf_ui_div */
&& mpf_cmp_ui (in2, 0) == 0)
continue;
(dis_funcs[i]) (res1, in1i, in2);
mpf_set (out1, in2);
(dis_funcs[i]) (out1, in1i, out1);
mpf_set (res2, out1);
if (mpf_cmp (res1, res2) != 0)
dump_abort (dis_func_names[i], res1, res2);
}
}
mpf_clear (in1);
mpf_clear (in2);
mpf_clear (out1);
mpf_clear (res1);
mpf_clear (res2);
mpf_clear (res3);
tests_end ();
exit (0);
}
void
dump_abort (const char *name, mpf_t res1, mpf_t res2)
{
printf ("failure in %s:\n", name);
mpf_dump (res1);
mpf_dump (res2);
abort ();
}
#if 0
void mpf_abs (mpf_ptr, mpf_srcptr);
void mpf_sqrt (mpf_ptr, mpf_srcptr);
void mpf_neg (mpf_ptr, mpf_srcptr);
#endif
#endif /* ! DLL_EXPORT */

View File

@@ -0,0 +1,107 @@
/* Test mpf_add.
Copyright 1996, 2001 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"
#ifndef SIZE
#define SIZE 16
#endif
int
main (int argc, char **argv)
{
mp_size_t size;
mp_exp_t exp;
int reps = 20000;
int i;
mpf_t u, v, w, wref;
mp_size_t bprec = 100;
mpf_t rerr, max_rerr, limit_rerr;
tests_start ();
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init_set_ui (limit_rerr, 1);
mpf_div_2exp (limit_rerr, limit_rerr, bprec);
#if VERBOSE
mpf_dump (limit_rerr);
#endif
mpf_init (rerr);
mpf_init_set_ui (max_rerr, 0);
mpf_init (u);
mpf_init (v);
mpf_init (w);
mpf_init (wref);
for (i = 0; i < reps; i++)
{
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % SIZE;
mpf_random2 (u, size, exp);
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % SIZE;
mpf_random2 (v, size, exp);
mpf_add (w, u, v);
refmpf_add (wref, u, v);
mpf_reldiff (rerr, w, wref);
if (mpf_cmp (rerr, max_rerr) > 0)
{
mpf_set (max_rerr, rerr);
#if VERBOSE
mpf_dump (max_rerr);
#endif
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf ("wref = "); mpf_dump (wref);
printf (" w = "); mpf_dump (w);
abort ();
}
}
}
mpf_clear (limit_rerr);
mpf_clear (rerr);
mpf_clear (max_rerr);
mpf_clear (u);
mpf_clear (v);
mpf_clear (w);
mpf_clear (wref);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,103 @@
/* Test mpf_cmp_d.
Copyright 2001, 2003, 2003, 2005 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 <string.h>
#include "gmp-impl.h"
#include "tests.h"
#define SGN(n) ((n) > 0 ? 1 : (n) < 0 ? -1 : 0)
void
check_one (const char *name, mpf_srcptr x, double y, int cmp)
{
int got;
got = mpf_cmp_d (x, y);
if (SGN(got) != cmp)
{
int i;
printf ("mpf_cmp_d wrong (from %s)\n", name);
printf (" got %d\n", got);
printf (" want %d\n", cmp);
mpf_trace (" x", x);
printf (" y %g\n", y);
mp_trace_base=-16;
mpf_trace (" x", x);
printf (" y %g\n", y);
printf (" y");
for (i = 0; i < sizeof(y); i++)
printf (" %02X", (unsigned) ((unsigned char *) &y)[i]);
printf ("\n");
abort ();
}
}
void
check_infinity (void)
{
mpf_t x;
double y = tests_infinity_d ();
if (y == 0.0)
return;
mpf_init (x);
/* 0 cmp inf */
mpf_set_ui (x, 0L);
check_one ("check_infinity", x, y, -1);
check_one ("check_infinity", x, -y, 1);
/* 123 cmp inf */
mpf_set_ui (x, 123L);
check_one ("check_infinity", x, y, -1);
check_one ("check_infinity", x, -y, 1);
/* -123 cmp inf */
mpf_set_si (x, -123L);
check_one ("check_infinity", x, y, -1);
check_one ("check_infinity", x, -y, 1);
/* 2^5000 cmp inf */
mpf_set_ui (x, 1L);
mpf_mul_2exp (x, x, 5000L);
check_one ("check_infinity", x, y, -1);
check_one ("check_infinity", x, -y, 1);
/* -2^5000 cmp inf */
mpf_neg (x, x);
check_one ("check_infinity", x, y, -1);
check_one ("check_infinity", x, -y, 1);
mpf_clear (x);
}
int
main (int argc, char *argv[])
{
tests_start ();
check_infinity ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,134 @@
/* Test mpf_cmp_si and mpf_cmp_z.
Copyright 2000, 2001, 2004, 2015 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 SGN(x) ((x) < 0 ? -1 : (x) == 0 ? 0 : 1)
void
check_data (void)
{
static const struct {
int a_base;
const char *a;
const char *b;
int want;
} data[] = {
{ 10, "0", "1", -1 },
{ 10, "0", "0", 0 },
{ 10, "0", "-1", 1 },
{ 10, "1", "1", 0 },
{ 10, "1", "0", 1 },
{ 10, "1", "-1", 1 },
{ 10, "-1", "1", -1 },
{ 10, "-1", "0", -1 },
{ 10, "-1", "-1", 0 },
{ 10, "1.5", "2", -1 },
{ 10, "1.5", "1", 1 },
{ 10, "0.5", "1", -1 },
{ 10, "-1.5", "-2", 1 },
{ 10, "-1.5", "-1", -1 },
{ 10, "-0.5", "-1", 1 },
{ 16, "0", "-0x80000000", 1 },
{ 16, "80000000", "-0x80000000", 1 },
{ 16, "80000001", "-0x80000000", 1 },
{ 16, "-80000000", "-0x80000000", 0 },
{ 16, "-80000001", "-0x80000000", -1 },
{ 16, "-FF0080000001", "-0x80000000", -1 },
{ 16, "0", "-0x8000000000000000", 1 },
{ 16, "8000000000000000", "-0x8000000000000000", 1 },
{ 16, "8000000000000001", "-0x8000000000000000", 1 },
{ 16, "-8000000000000000", "-0x8000000000000000", 0 },
{ 16, "-8000000000000000.1", "-0x8000000000000000", -1 },
{ 16, "-FF008000000000000001", "-0x8000000000000000", -1 },
{ 16, "0", "-0x876543210FEDCBA9876543210000000", 1 },
{ 16, "876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000", 1 },
{ 16, "876543210FEDCBA9876543210000001", "-0x876543210FEDCBA9876543210000000", 1 },
{ 16, "-876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000", 0 },
{ 16, "-876543210FEDCBA9876543210000000.1", "-0x876543210FEDCBA9876543210000000", -1 },
{ 16, "-FF00876543210FEDCBA9876543210000000", "-0x876543210FEDCBA9876543210000000", -1 },
};
mpf_t a;
mpz_t bz;
long b;
int got;
int i;
mpf_init2 (a, 128);
mpz_init (bz);
for (i = 0; i < numberof (data); i++)
{
mpf_set_str_or_abort (a, data[i].a, data[i].a_base);
mpz_set_str_or_abort (bz, data[i].b, 0);
if (mpz_fits_slong_p (bz))
{
b = mpz_get_si (bz);
got = mpf_cmp_si (a, b);
if (SGN (got) != data[i].want)
{
printf ("mpf_cmp_si wrong on data[%d]\n", i);
printf (" a="); mpf_out_str (stdout, 10, 0, a);
printf (" (%s)\n", data[i].a);
printf (" b=%ld (%s)\n", b, data[i].b);
printf (" got=%d\n", got);
printf (" want=%d\n", data[i].want);
abort();
}
}
got = mpf_cmp_z (a, bz);
if (SGN (got) != data[i].want)
{
b = mpz_get_si (bz);
printf ("mpf_cmp_z wrong on data[%d]\n", i);
printf (" a="); mpf_out_str (stdout, 10, 0, a);
printf (" (%s)\n", data[i].a);
printf (" b=%ld (%s)\n", b, data[i].b);
printf (" got=%d\n", got);
printf (" want=%d\n", data[i].want);
abort();
}
}
mpf_clear (a);
mpz_clear (bz);
}
int
main (void)
{
tests_start ();
check_data ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,261 @@
/* Test mpf_get_str and mpf_set_str.
Copyright 1996, 2000, 2001, 2008, 2019, 2020 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 <string.h> /* for strlen */
#include "gmp-impl.h"
#include "tests.h"
#ifndef SIZE
#define SIZE 10
#endif
#ifndef EXPO
#define EXPO 200
#endif
int
main (int argc, char **argv)
{
mpf_t x, y;
int reps = 20000;
int i;
mp_size_t bprec = 100;
mpf_t d, rerr, max_rerr, limit_rerr;
char *str;
mp_exp_t bexp;
long size, exp;
int base;
char buf[SIZE * GMP_LIMB_BITS + 5];
tests_start ();
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init_set_ui (limit_rerr, 1);
mpf_div_2exp (limit_rerr, limit_rerr, bprec);
#if VERBOSE
mpf_dump (limit_rerr);
#endif
mpf_init (rerr);
mpf_init_set_ui (max_rerr, 0);
mpf_init (x);
mpf_init (y);
mpf_init (d);
/* First test some specific values. */
mpf_set_str (y, "1.23456", 0);
mpf_set_str (x, "1.23456", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "00000000000000000000000000000000000000001.23456", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0.000000000000000000000000000000000000000123456e40", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, ".000000000000000000000000000000000000000123456e40", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "00000000000000000000.00000000000000000000123456e21", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (y, "1.23456e1000", 0);
mpf_set_str (x, "1.23456e1000", 10);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "1.23456e+1000", 0);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "1.23456e+1000", 10);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "00000000000000000000000000000000000000001.23456e+1000", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0.000000000000000000000000000000000000000123456e+1040", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, ".000000000000000000000000000000000000000123456e+1040", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "00000000000000000000.00000000000000000000123456e+1021", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (y, "1.23456", 16);
mpf_set_str (x, "00000000000000000000000000000000000000001.23456", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0.000000000000000000000000000000000000000123456@28", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, ".000000000000000000000000000000000000000123456@28", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "00000000000000000000.00000000000000000000123456@15", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (y, " 0", 10);
mpf_set_str (x, "00000000000000000000000000000000000000000000000000000", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0000000000000000000000000000000000000000000000000000.", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "000000000000000000000000000000000000000000000000000.0", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, ".0000000000000000000000000000000000000000000000000000", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0.000000000000000000000000000000000000000000000000000", 10);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "00000000000000000000000000000000000000000000000000000", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0000000000000000000000000000000000000000000000000000.", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "000000000000000000000000000000000000000000000000000.0", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, ".0000000000000000000000000000000000000000000000000000", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "0.000000000000000000000000000000000000000000000000000", 16);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "+00000000000000000000000000000000000000000000000000000e-345", 9);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "-0000000000000000000000000000000000000000000000000000.@AB", 26);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "000000000000000000000000000000000000000000000000000.0@78", 19);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "+.0000000000000000000000000000000000000000000000000000e555", 6);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
mpf_set_str (x, "-0.000000000000000000000000000000000000000000000000000@-AAAAAAAAAAAAAAAAAAAAAAAA", 17);
MPF_CHECK_FORMAT (x);
if (mpf_cmp (x, y) != 0)
abort ();
/* Now test random values. */
for (i = 0; i < reps; i++)
{
if (i == 0)
{
/* exercise the special case in get_str for for x==0 */
mpf_set_ui (x, 0L);
base = 0;
}
else
{
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % EXPO;
mpf_random2 (x, size, exp);
base = urandom () % 62;
base += base > 0;
}
str = mpf_get_str (0, &bexp, base, 0, x);
if (str[0] == '-')
sprintf (buf, "-0.%s@%ld", str + 1, bexp);
else
sprintf (buf, "0.%s@%ld", str, bexp);
mpf_set_str_or_abort (y, buf, -base);
(*__gmp_free_func) (str, strlen (str) + 1);
mpf_reldiff (rerr, x, y);
if (mpf_cmp (rerr, max_rerr) > 0)
{
mpf_set (max_rerr, rerr);
#if VERBOSE
mpf_dump (max_rerr);
#endif
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR after %d tests\n", i);
printf ("base = %d\n", base);
printf (" x = "); mpf_dump (x);
printf (" y = "); mpf_dump (y);
abort ();
}
}
}
mpf_clear (limit_rerr);
mpf_clear (rerr);
mpf_clear (max_rerr);
mpf_clear (x);
mpf_clear (y);
mpf_clear (d);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,185 @@
/* Test mpf_div.
Copyright 2004 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"
void
check_one (const char *desc, mpf_ptr got, mpf_srcptr u, mpf_srcptr v)
{
if (! refmpf_validate_division ("mpf_div", got, u, v))
{
mp_trace_base = -16;
mpf_trace (" u", u);
mpf_trace (" v", v);
printf (" %s\n", desc);
abort ();
}
}
void
check_rand (void)
{
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
unsigned long prec;
mpf_t got, u, v;
int i;
mpf_init (got);
mpf_init (u);
mpf_init (v);
/* separate */
for (i = 0; i < 100; i++)
{
/* got precision */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (got, prec);
/* u */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (u, prec);
do {
mpf_random2 (u, PREC(u), (mp_exp_t) 20);
} while (SIZ(u) == 0);
if (gmp_urandomb_ui (rands, 1L))
mpf_neg (u, u);
/* v */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (v, prec);
do {
mpf_random2 (v, PREC(v), (mp_exp_t) 20);
} while (SIZ(v) == 0);
if (gmp_urandomb_ui (rands, 1L))
mpf_neg (v, v);
switch (i % 3) {
case 0:
mpf_div (got, u, v);
check_one ("separate", got, u, v);
break;
case 1:
prec = refmpf_set_overlap (got, u);
mpf_div (got, got, v);
check_one ("dst == u", got, u, v);
mpf_set_prec_raw (got, prec);
break;
case 2:
prec = refmpf_set_overlap (got, v);
mpf_div (got, u, got);
check_one ("dst == v", got, u, v);
mpf_set_prec_raw (got, prec);
break;
}
}
mpf_clear (got);
mpf_clear (u);
mpf_clear (v);
}
/* Exercise calls mpf(x,x,x) */
void
check_reuse_three (void)
{
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
unsigned long result_prec, input_prec, set_prec;
mpf_t got;
int i;
mpf_init (got);
for (i = 0; i < 8; i++)
{
result_prec = min_prec + gmp_urandomm_ui (rands, 15L);
input_prec = min_prec + gmp_urandomm_ui (rands, 15L);
set_prec = MAX (result_prec, input_prec);
refmpf_set_prec_limbs (got, set_prec);
/* input, non-zero, possibly negative */
PREC(got) = input_prec;
do {
mpf_random2 (got, input_prec, (mp_exp_t) 20);
} while (SIZ(got) == 0);
if (gmp_urandomb_ui (rands, 1L))
mpf_neg (got, got);
PREC(got) = result_prec;
mpf_div (got, got, got);
/* expect exactly 1.0 always */
ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0);
PREC(got) = set_prec;
}
mpf_clear (got);
}
void
check_various (void)
{
mpf_t got, u, v;
mpf_init (got);
mpf_init (u);
mpf_init (v);
/* 100/4 == 25 */
mpf_set_prec (got, 20L);
mpf_set_ui (u, 100L);
mpf_set_ui (v, 4L);
mpf_div (got, u, v);
MPF_CHECK_FORMAT (got);
ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0);
/* 1/(2^n+1), a case where truncating the divisor would be wrong */
mpf_set_prec (got, 500L);
mpf_set_prec (v, 900L);
mpf_set_ui (v, 1L);
mpf_mul_2exp (v, v, 800L);
mpf_add_ui (v, v, 1L);
mpf_div (got, u, v);
check_one ("1/2^n+1, separate", got, u, v);
mpf_clear (got);
mpf_clear (u);
mpf_clear (v);
}
int
main (void)
{
tests_start ();
check_various ();
check_rand ();
check_reuse_three ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,118 @@
/* Test mpf_div, mpf_div_2exp, mpf_mul_2exp.
Copyright 1996, 2000, 2001 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"
#ifndef SIZE
#define SIZE 16
#endif
int
main (int argc, char **argv)
{
int reps = 100000;
int i;
mpf_t u, v, w1, w2, w3;
mp_size_t bprec = 100;
mpf_t rerr, limit_rerr;
mp_size_t un;
mp_exp_t ue;
tests_start ();
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init (rerr);
mpf_init (limit_rerr);
mpf_init (u);
mpf_init (v);
mpf_init (w1);
mpf_init (w2);
mpf_init (w3);
for (i = 0; i < reps; i++)
{
unsigned long int res_prec;
unsigned long int pow2;
res_prec = urandom () % (bprec + 100);
mpf_set_prec (w1, res_prec);
mpf_set_prec (w2, res_prec);
mpf_set_prec (w3, res_prec);
mpf_set_ui (limit_rerr, 1);
mpf_div_2exp (limit_rerr, limit_rerr, res_prec);
pow2 = urandom () % 0x10000;
mpf_set_ui (v, 1);
mpf_mul_2exp (v, v, pow2);
un = urandom () % (2 * SIZE) - SIZE;
ue = urandom () % SIZE;
mpf_random2 (u, un, ue);
mpf_div_2exp (w1, u, pow2);
mpf_div (w2, u, v);
mpf_reldiff (rerr, w1, w2);
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR in mpf_div or mpf_div_2exp after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf (" w1 = "); mpf_dump (w1);
printf (" w2 = "); mpf_dump (w2);
abort ();
}
mpf_mul_2exp (w3, w1, pow2);
mpf_reldiff (rerr, u, w3);
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR in mpf_mul_2exp after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf (" w1 = "); mpf_dump (w1);
printf (" w3 = "); mpf_dump (w3);
abort ();
}
}
mpf_clear (rerr);
mpf_clear (limit_rerr);
mpf_clear (u);
mpf_clear (v);
mpf_clear (w1);
mpf_clear (w2);
mpf_clear (w3);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,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);
}

View File

@@ -0,0 +1,327 @@
/* Test mpf_fits_*_p
Copyright 2001, 2002, 2013 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"
/* Nothing sophisticated here, just exercise mpf_fits_*_p on a small amount
of data. */
#define EXPECT_S(fun,name,answer) \
got = fun (f); \
if (got != answer) \
{ \
printf ("%s (%s) got %d want %d\n", name, expr, got, answer); \
printf (" f size %d exp %ld\n", SIZ(f), EXP(f)); \
printf (" f dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); \
printf (" f hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); \
error = 1; \
}
#define EXPECT(fun,answer) EXPECT_S(fun,#fun,answer)
int
main (void)
{
mpf_t f, f0p5;
int got;
const char *expr;
int error = 0;
tests_start ();
mpf_init2 (f, 200L);
mpf_init2 (f0p5, 200L);
/* 0.5 */
mpf_set_ui (f0p5, 1L);
mpf_div_2exp (f0p5, f0p5, 1L);
mpf_set_ui (f, 0L);
expr = "0";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_ui (f, 1L);
expr = "1";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_si (f, -1L);
expr = "-1";
EXPECT (mpf_fits_ulong_p, 0);
EXPECT (mpf_fits_uint_p, 0);
EXPECT (mpf_fits_ushort_p, 0);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_ui (f, (unsigned long) USHRT_MAX);
expr = "USHRT_MAX";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
mpf_set_ui (f, (unsigned long) USHRT_MAX);
mpf_add (f, f, f0p5);
expr = "USHRT_MAX + 0.5";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
mpf_set_ui (f, (unsigned long) USHRT_MAX);
mpf_add_ui (f, f, 1L);
expr = "USHRT_MAX + 1";
EXPECT (mpf_fits_ushort_p, 0);
mpf_set_ui (f, (unsigned long) UINT_MAX);
expr = "UINT_MAX";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
mpf_set_ui (f, (unsigned long) UINT_MAX);
mpf_add (f, f, f0p5);
expr = "UINT_MAX + 0.5";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
mpf_set_ui (f, (unsigned long) UINT_MAX);
mpf_add_ui (f, f, 1L);
expr = "UINT_MAX + 1";
EXPECT (mpf_fits_uint_p, 0);
mpf_set_ui (f, ULONG_MAX);
expr = "ULONG_MAX";
EXPECT (mpf_fits_ulong_p, 1);
mpf_set_ui (f, ULONG_MAX);
mpf_add (f, f, f0p5);
expr = "ULONG_MAX + 0.5";
EXPECT (mpf_fits_ulong_p, 1);
mpf_set_ui (f, ULONG_MAX);
mpf_add_ui (f, f, 1L);
expr = "ULONG_MAX + 1";
EXPECT (mpf_fits_ulong_p, 0);
mpf_set_si (f, (long) SHRT_MAX);
expr = "SHRT_MAX";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_si (f, (long) SHRT_MAX);
expr = "SHRT_MAX + 0.5";
mpf_add (f, f, f0p5);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_si (f, (long) SHRT_MAX);
mpf_add_ui (f, f, 1L);
expr = "SHRT_MAX + 1";
EXPECT (mpf_fits_sshort_p, 0);
mpf_set_si (f, (long) INT_MAX);
expr = "INT_MAX";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
mpf_set_si (f, (long) INT_MAX);
mpf_add (f, f, f0p5);
expr = "INT_MAX + 0.5";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
mpf_set_si (f, (long) INT_MAX);
mpf_add_ui (f, f, 1L);
expr = "INT_MAX + 1";
EXPECT (mpf_fits_sint_p, 0);
mpf_set_si (f, LONG_MAX);
expr = "LONG_MAX";
EXPECT (mpf_fits_slong_p, 1);
mpf_set_si (f, LONG_MAX);
mpf_add (f, f, f0p5);
expr = "LONG_MAX + 0.5";
EXPECT (mpf_fits_slong_p, 1);
mpf_set_si (f, LONG_MAX);
mpf_add_ui (f, f, 1L);
expr = "LONG_MAX + 1";
EXPECT (mpf_fits_slong_p, 0);
mpf_set_si (f, (long) SHRT_MIN);
expr = "SHRT_MIN";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_si (f, (long) SHRT_MIN);
mpf_sub (f, f, f0p5);
expr = "SHRT_MIN - 0.5";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_si (f, (long) SHRT_MIN);
mpf_sub_ui (f, f, 1L);
expr = "SHRT_MIN - 1";
EXPECT (mpf_fits_sshort_p, 0);
mpf_set_si (f, (long) INT_MIN);
expr = "INT_MIN";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
mpf_set_si (f, (long) INT_MIN);
mpf_sub (f, f, f0p5);
expr = "INT_MIN - 0.5";
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
mpf_set_si (f, (long) INT_MIN);
mpf_sub_ui (f, f, 1L);
expr = "INT_MIN - 1";
EXPECT (mpf_fits_sint_p, 0);
mpf_set_si (f, LONG_MIN);
expr = "LONG_MIN";
EXPECT (mpf_fits_slong_p, 1);
mpf_set_si (f, LONG_MIN);
mpf_sub (f, f, f0p5);
expr = "LONG_MIN - 0.5";
EXPECT (mpf_fits_slong_p, 1);
mpf_set_si (f, LONG_MIN);
mpf_sub_ui (f, f, 1L);
expr = "LONG_MIN - 1";
EXPECT (mpf_fits_slong_p, 0);
mpf_set_str_or_abort (f, "0.5", 10);
expr = "0.5";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_str_or_abort (f, "-0.5", 10);
expr = "-0.5";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_str_or_abort (f, "-1.5", 10);
expr = "-1.5";
EXPECT (mpf_fits_ulong_p, 0);
EXPECT (mpf_fits_uint_p, 0);
EXPECT (mpf_fits_ushort_p, 0);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_str_or_abort (f, "1.000000000000000000000000000000000001", 16);
expr = "1.000000000000000000000000000000000001 base 16";
EXPECT (mpf_fits_ulong_p, 1);
EXPECT (mpf_fits_uint_p, 1);
EXPECT (mpf_fits_ushort_p, 1);
EXPECT (mpf_fits_slong_p, 1);
EXPECT (mpf_fits_sint_p, 1);
EXPECT (mpf_fits_sshort_p, 1);
mpf_set_str_or_abort (f, "1@1000", 16);
expr = "1@1000 base 16";
EXPECT (mpf_fits_ulong_p, 0);
EXPECT (mpf_fits_uint_p, 0);
EXPECT (mpf_fits_ushort_p, 0);
EXPECT (mpf_fits_slong_p, 0);
EXPECT (mpf_fits_sint_p, 0);
EXPECT (mpf_fits_sshort_p, 0);
mpf_set_ui (f, 1L);
mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
mpf_sub_ui (f, f, 1L);
expr = "2^(BITS_PER_ULONG+1) - 1";
EXPECT (mpf_fits_ulong_p, 0);
EXPECT (mpf_fits_uint_p, 0);
EXPECT (mpf_fits_ushort_p, 0);
EXPECT (mpf_fits_slong_p, 0);
EXPECT (mpf_fits_sint_p, 0);
EXPECT (mpf_fits_sshort_p, 0);
mpf_set_ui (f, 1L);
mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
mpf_ui_sub (f, 1L, f);
expr = "- (2^(BITS_PER_ULONG+1) - 1)";
EXPECT (mpf_fits_ulong_p, 0);
EXPECT (mpf_fits_uint_p, 0);
EXPECT (mpf_fits_ushort_p, 0);
EXPECT (mpf_fits_slong_p, 0);
EXPECT (mpf_fits_sint_p, 0);
EXPECT (mpf_fits_sshort_p, 0);
mpf_set_ui (f, 1L);
mpf_mul_2exp (f, f, BITS_PER_ULONG + 5);
mpf_sub_ui (f, f, 1L);
expr = "2^(BITS_PER_ULONG+5) - 1";
EXPECT (mpf_fits_ulong_p, 0);
EXPECT (mpf_fits_uint_p, 0);
EXPECT (mpf_fits_ushort_p, 0);
EXPECT (mpf_fits_slong_p, 0);
EXPECT (mpf_fits_sint_p, 0);
EXPECT (mpf_fits_sshort_p, 0);
if (error)
abort ();
mpf_clear (f);
mpf_clear (f0p5);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,106 @@
/* Test mpf_get_d and mpf_set_d.
Copyright 1996, 1999-2001, 2009 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"
#if defined (__vax) || defined (__vax__)
#define LOW_BOUND 1e-38
#define HIGH_BOUND 8e37
#endif
#if defined (_CRAY) && ! defined (_CRAYIEEE)
/* The range varies mysteriously between Cray version. On an SV1,
the range seem to be 1e-600..1e603, but a cfp (non-ieee) T90
has a much smaller range of 1e-240..1e240. */
#define LOW_BOUND 1e-240
#define HIGH_BOUND 1e240
#endif
#if ! defined (LOW_BOUND)
#define LOW_BOUND 1e-300
#define HIGH_BOUND 1e300
#endif
void
test_denorms (int prc)
{
#ifdef _GMP_IEEE_FLOATS
double d1, d2;
mpf_t f;
int i;
mpf_set_default_prec (prc);
mpf_init (f);
d1 = 1.9;
for (i = 0; i < 820; i++)
{
mpf_set_d (f, d1);
d2 = mpf_get_d (f);
if (d1 != d2)
abort ();
d1 *= 0.4;
}
mpf_clear (f);
#endif
}
int
main (int argc, char **argv)
{
double d, e, r;
mpf_t u, v;
tests_start ();
mpf_init (u);
mpf_init (v);
mpf_set_d (u, LOW_BOUND);
for (d = 2.0 * LOW_BOUND; d < HIGH_BOUND; d *= 1.01)
{
mpf_set_d (v, d);
if (mpf_cmp (u, v) >= 0)
abort ();
e = mpf_get_d (v);
r = e/d;
if (r < 0.99999999999999 || r > 1.00000000000001)
{
fprintf (stderr, "should be one ulp from 1: %.16f\n", r);
abort ();
}
mpf_set (u, v);
}
mpf_clear (u);
mpf_clear (v);
test_denorms (10);
test_denorms (32);
test_denorms (64);
test_denorms (100);
test_denorms (200);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,136 @@
/* Test mpf_get_d_2exp.
Copyright 2002, 2003, 2017, 2020 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"
static void
check_data (void)
{
mpf_t f;
double got, want;
long got_exp;
long exp;
struct {
int base;
int shift;
} data[] = {
{-1, 1}, {-3, 2}, {-5, 3}, {-7, 3}, { 1, 1}, { 3, 2}, { 5, 3}, { 7, 3}
};
mpf_init2 (f, 3);
got = mpf_get_d_2exp (&got_exp, f);
if (got != 0 || got_exp != 0)
{
printf ("mpf_get_d_2exp wrong on zero\n");
mpf_trace (" f ", f);
d_trace (" got ", got);
printf (" got exp %ld\n", got_exp);
abort();
}
for (exp = -513; exp <= 513; exp++)
{
size_t i;
for (i = 0; i < numberof (data); i++)
{
want = (double) data[i].base / (1 << data[i].shift);
mpf_set_d (f, want);
if (exp >= 0)
mpf_mul_2exp (f, f, exp);
else
mpf_div_2exp (f, f, -exp);
got = mpf_get_d_2exp (&got_exp, f);
if (got != want || got_exp != exp)
{
printf ("mpf_get_d_2exp wrong on 2**%ld\n", exp);
mpf_trace (" f ", f);
d_trace (" want ", want);
d_trace (" got ", got);
printf (" want exp %ld\n", exp);
printf (" got exp %ld\n", got_exp);
abort();
}
}
}
mpf_clear (f);
}
/* Check that hardware rounding doesn't make mpf_get_d_2exp return a value
outside its defined range. */
static void
check_round (void)
{
static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
mpf_t f;
double got;
long got_exp;
int i, rnd_mode, old_rnd_mode;
mpf_init2 (f, 1024L);
old_rnd_mode = tests_hardware_getround ();
for (rnd_mode = 0; rnd_mode < 4; rnd_mode++)
{
tests_hardware_setround (rnd_mode);
for (i = 0; i < numberof (data); i++)
{
mpf_set_ui (f, 1L);
mpf_mul_2exp (f, f, data[i]);
mpf_sub_ui (f, f, 1L);
got = mpf_get_d_2exp (&got_exp, f);
if (got < 0.5 || got >= 1.0)
{
printf ("mpf_get_d_2exp bad on 2**%lu-1\n", data[i]);
printf ("result out of range, expect 0.5 <= got < 1.0\n");
printf (" rnd_mode = %d\n", rnd_mode);
printf (" data[i] = %lu\n", data[i]);
mpf_trace (" f ", f);
d_trace (" got ", got);
printf (" got exp %ld\n", got_exp);
abort();
}
}
}
mpf_clear (f);
tests_hardware_setround (old_rnd_mode);
}
int
main (void)
{
tests_start ();
mp_trace_base = 16;
check_data ();
check_round ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,222 @@
/* Exercise mpz_get_si.
Copyright 2000, 2001 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"
void
check_data (void)
{
static const struct {
int base;
const char *f;
long want;
} data[] = {
{ 10, "0", 0L },
{ 10, "1", 1L },
{ 10, "-1", -1L },
{ 10, "2", 2L },
{ 10, "-2", -2L },
{ 10, "12345", 12345L },
{ 10, "-12345", -12345L },
/* fraction bits ignored */
{ 10, "0.5", 0L },
{ 10, "-0.5", 0L },
{ 10, "1.1", 1L },
{ 10, "-1.1", -1L },
{ 10, "1.9", 1L },
{ 10, "-1.9", -1L },
{ 16, "1.000000000000000000000000000000000000000000000000001", 1L },
{ 16, "-1.000000000000000000000000000000000000000000000000001", -1L },
/* low bits extracted (this is undocumented) */
{ 16, "1000000000000000000000000000000000000000000000000001", 1L },
{ 16, "-1000000000000000000000000000000000000000000000000001", -1L },
};
int i;
mpf_t f;
long got;
mpf_init2 (f, 2000L);
for (i = 0; i < numberof (data); i++)
{
mpf_set_str_or_abort (f, data[i].f, data[i].base);
got = mpf_get_si (f);
if (got != data[i].want)
{
printf ("mpf_get_si wrong at data[%d]\n", i);
printf (" f \"%s\"\n", data[i].f);
printf (" dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n");
printf (" hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n");
printf (" size %ld\n", (long) SIZ(f));
printf (" exp %ld\n", (long) EXP(f));
printf (" got %ld (0x%lX)\n", got, got);
printf (" want %ld (0x%lX)\n", data[i].want, data[i].want);
abort();
}
}
mpf_clear (f);
}
void
check_max (void)
{
mpf_t f;
long want;
long got;
mpf_init2 (f, 200L);
#define CHECK_MAX(name) \
if (got != want) \
{ \
printf ("mpf_get_si wrong on %s\n", name); \
printf (" f "); \
mpf_out_str (stdout, 10, 0, f); printf (", hex "); \
mpf_out_str (stdout, 16, 0, f); printf ("\n"); \
printf (" got %ld, hex %lX\n", got, got); \
printf (" want %ld, hex %lX\n", want, want); \
abort(); \
}
want = LONG_MAX;
mpf_set_si (f, want);
got = mpf_get_si (f);
CHECK_MAX ("LONG_MAX");
want = LONG_MIN;
mpf_set_si (f, want);
got = mpf_get_si (f);
CHECK_MAX ("LONG_MIN");
mpf_clear (f);
}
void
check_limbdata (void)
{
#define M GMP_NUMB_MAX
static const struct {
mp_exp_t exp;
mp_size_t size;
mp_limb_t d[10];
unsigned long want;
} data[] = {
/* in the comments here, a "_" indicates a digit (ie. limb) position not
included in the d data, and therefore zero */
{ 0, 0, { 0 }, 0L }, /* 0 */
{ 1, 1, { 1 }, 1L }, /* 1 */
{ 1, -1, { 1 }, -1UL }, /* -1 */
{ 0, 1, { 1 }, 0L }, /* .1 */
{ 0, -1, { 1 }, 0L }, /* -.1 */
{ -1, 1, { 1 }, 0L }, /* ._1 */
{ -1, -1, { 1 }, 0L }, /* -._1 */
{ -999, 1, { 1 }, 0L }, /* .___1 small */
{ MP_EXP_T_MIN, 1, { 1 }, 0L }, /* .____1 very small */
{ 999, 1, { 1 }, 0L }, /* 1____. big */
{ MP_EXP_T_MAX, 1, { 1 }, 0L }, /* 1_____. very big */
{ 1, 2, { 999, 2 }, 2L }, /* 2.9 */
{ 5, 8, { 7, 8, 9, 3, 0, 0, 0, 1 }, 3L }, /* 10003.987 */
{ 2, 2, { M, M }, LONG_MAX }, /* FF. */
{ 2, 2, { M, M, M }, LONG_MAX }, /* FF.F */
{ 3, 3, { M, M, M }, LONG_MAX }, /* FFF. */
#if GMP_NUMB_BITS >= BITS_PER_ULONG
/* normal case, numb bigger than long */
{ 2, 1, { 1 }, 0L }, /* 1_. */
{ 2, 2, { 0, 1 }, 0L }, /* 10. */
{ 2, 2, { 999, 1 }, 999L }, /* 19. */
{ 3, 2, { 999, 1 }, 0L }, /* 19_. */
#else
/* nails case, numb smaller than long */
{ 2, 1, { 1 }, 1L << GMP_NUMB_BITS }, /* 1_. */
{ 3, 1, { 1 }, 0L }, /* 1__. */
{ 2, 2, { 99, 1 }, 99L + (1L << GMP_NUMB_BITS) }, /* 19. */
{ 3, 2, { 1, 99 }, 1L << GMP_NUMB_BITS }, /* 91_. */
{ 3, 3, { 0, 1, 99 }, 1L << GMP_NUMB_BITS }, /* 910. */
#endif
};
mpf_t f;
unsigned long got;
int i;
mp_limb_t buf[20 + numberof(data[i].d)];
for (i = 0; i < numberof (data); i++)
{
refmpn_fill (buf, 10, CNST_LIMB(0xDEADBEEF));
refmpn_copy (buf+10, data[i].d, ABS(data[i].size));
refmpn_fill (buf+10+ABS(data[i].size), 10, CNST_LIMB(0xDEADBEEF));
PTR(f) = buf+10;
EXP(f) = data[i].exp;
SIZ(f) = data[i].size;
PREC(f) = numberof (data[i].d);
MPF_CHECK_FORMAT (f);
got = mpf_get_si (f);
if (got != data[i].want)
{
printf ("mpf_get_si wrong at limb data[%d]\n", i);
mpf_trace (" f", f);
mpn_trace (" d", data[i].d, data[i].size);
printf (" size %ld\n", (long) data[i].size);
printf (" exp %ld\n", (long) data[i].exp);
printf (" got %lu (0x%lX)\n", got, got);
printf (" want %lu (0x%lX)\n", data[i].want, data[i].want);
abort();
}
}
}
int
main (void)
{
tests_start ();
check_data ();
check_max ();
check_limbdata ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,127 @@
/* Exercise mpf_get_ui.
Copyright 2004 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"
void
check_limbdata (void)
{
#define M GMP_NUMB_MAX
static const struct {
mp_exp_t exp;
mp_size_t size;
mp_limb_t d[10];
unsigned long want;
} data[] = {
/* in the comments here, a "_" indicates a digit (ie. limb) position not
included in the d data, and therefore zero */
{ 0, 0, { 0 }, 0L }, /* 0 */
{ 1, 1, { 1 }, 1L }, /* 1 */
{ 1, -1, { 1 }, 1L }, /* -1 */
{ 0, 1, { 1 }, 0L }, /* .1 */
{ 0, -1, { 1 }, 0L }, /* -.1 */
{ -1, 1, { 1 }, 0L }, /* ._1 */
{ -1, -1, { 1 }, 0L }, /* -._1 */
{ -999, 1, { 1 }, 0L }, /* .___1 small */
{ MP_EXP_T_MIN, 1, { 1 }, 0L }, /* .____1 very small */
{ 999, 1, { 1 }, 0L }, /* 1____. big */
{ MP_EXP_T_MAX, 1, { 1 }, 0L }, /* 1_____. very big */
{ 1, 2, { 999, 2 }, 2L }, /* 2.9 */
{ 5, 8, { 7, 8, 9, 3, 0, 0, 0, 1 }, 3L }, /* 10003.987 */
{ 2, 2, { M, M }, ULONG_MAX }, /* FF. */
{ 2, 2, { M, M, M }, ULONG_MAX }, /* FF.F */
{ 3, 3, { M, M, M }, ULONG_MAX }, /* FFF. */
#if GMP_NUMB_BITS >= BITS_PER_ULONG
/* normal case, numb bigger than long */
{ 2, 1, { 1 }, 0L }, /* 1_. */
{ 2, 2, { 0, 1 }, 0L }, /* 10. */
{ 2, 2, { 999, 1 }, 999L }, /* 19. */
{ 3, 2, { 999, 1 }, 0L }, /* 19_. */
#else
/* nails case, numb smaller than long */
{ 2, 1, { 1 }, 1L << GMP_NUMB_BITS }, /* 1_. */
{ 3, 1, { 1 }, 0L }, /* 1__. */
{ 2, 2, { 99, 1 }, 99L + (1L << GMP_NUMB_BITS) }, /* 19. */
{ 3, 2, { 1, 99 }, 1L << GMP_NUMB_BITS }, /* 91_. */
{ 3, 3, { 0, 1, 99 }, 1L << GMP_NUMB_BITS }, /* 910. */
#endif
};
mpf_t f;
unsigned long got;
int i;
mp_limb_t buf[20 + numberof(data[i].d)];
for (i = 0; i < numberof (data); i++)
{
refmpn_fill (buf, 10, CNST_LIMB(0xDEADBEEF));
refmpn_copy (buf+10, data[i].d, ABS(data[i].size));
refmpn_fill (buf+10+ABS(data[i].size), 10, CNST_LIMB(0xDEADBEEF));
PTR(f) = buf+10;
EXP(f) = data[i].exp;
SIZ(f) = data[i].size;
PREC(f) = numberof (data[i].d);
MPF_CHECK_FORMAT (f);
got = mpf_get_ui (f);
if (got != data[i].want)
{
printf ("mpf_get_ui wrong at limb data[%d]\n", i);
mpf_trace (" f", f);
mpn_trace (" d", data[i].d, data[i].size);
printf (" size %ld\n", (long) data[i].size);
printf (" exp %ld\n", (long) data[i].exp);
printf (" got %lu (0x%lX)\n", got, got);
printf (" want %lu (0x%lX)\n", data[i].want, data[i].want);
abort();
}
}
}
int
main (void)
{
tests_start ();
mp_trace_base = 16;
check_limbdata ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,61 @@
/* Test mpf_get_prec and mpf_set_prec.
Copyright 2000, 2001 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"
void
check_consistency (void)
{
mpf_t x;
unsigned long i, a, b;
mpf_init (x);
for (i = 1; i < 2000; i++)
{
mpf_set_prec (x, i);
a = mpf_get_prec (x);
mpf_set_prec (x, a);
b = mpf_get_prec (x);
if (a != b)
{
printf ("mpf_get_prec / mpf_set_prec inconsistent\n");
printf (" set %lu gives %lu, but then set %lu gives %lu\n",
i, a,
a, b);
abort ();
}
}
mpf_clear (x);
}
int
main (void)
{
tests_start ();
check_consistency ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,191 @@
/* Test mpf_inp_str.
Copyright 2001, 2002 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 "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h> /* for unlink */
#endif
#include "gmp-impl.h"
#include "tests.h"
#define FILENAME "t-inp_str.tmp"
void
check_data (void)
{
static const struct {
const char *inp;
int base;
const char *want;
int want_nread;
} data[] = {
{ "0", 10, "0", 1 },
{ "abc", 10, "0", 0 },
{ "ghi", 16, "0", 0 },
{ "125", 10, "125", 3 },
{ "125e1", 10, "1250", 5 },
{ "12e+2", 10, "1200", 5 },
{ "125e-1", 10, "12.5", 6 },
{ "ff", 16, "255", 2 },
{ "-ff", 16, "-255", 3 },
{ "FF", 16, "255", 2 },
{ "-FF", 16, "-255", 3 },
{ "100", 16, "256", 3 },
{ "100@1", 16, "4096", 5 },
{ "100@10", 16, "4722366482869645213696", 6 },
{ "100@10", -16, "281474976710656", 6 },
{ "100@-1", 16, "16", 6 },
{ "10000000000000000@-10", 16, "1", 21 },
{ "10000000000@-10", -16, "1", 15 },
{ "z", 36, "35", 1 },
{ "Z", 36, "35", 1 },
{ "z@1", 36, "1260", 3 },
{ "Z@1", 36, "1260", 3 },
{ "0", 0, "0", 1 },
};
mpf_t got, want;
long ftell_nread;
int i, pre, post, j, got_nread, want_nread;
FILE *fp;
mpf_init (got);
mpf_init (want);
for (i = 0; i < numberof (data); i++)
{
for (pre = 0; pre <= 3; pre++)
{
for (post = 0; post <= 2; post++)
{
mpf_set_str_or_abort (want, data[i].want, 10);
MPF_CHECK_FORMAT (want);
/* create the file new each time to ensure its length is what
we want */
fp = fopen (FILENAME, "w+");
ASSERT_ALWAYS (fp != NULL);
for (j = 0; j < pre; j++)
putc (' ', fp);
fputs (data[i].inp, fp);
for (j = 0; j < post; j++)
putc (' ', fp);
fflush (fp);
ASSERT_ALWAYS (! ferror(fp));
rewind (fp);
got_nread = mpf_inp_str (got, fp, data[i].base);
if (got_nread != 0)
{
ftell_nread = ftell (fp);
if (got_nread != ftell_nread)
{
printf ("mpf_inp_str nread wrong\n");
printf (" inp \"%s\"\n", data[i].inp);
printf (" base %d\n", data[i].base);
printf (" pre %d\n", pre);
printf (" post %d\n", post);
printf (" got_nread %d\n", got_nread);
printf (" ftell_nread %ld\n", ftell_nread);
abort ();
}
}
/* if data[i].inp is a whole string to read and there's no post
whitespace then expect to have EOF */
if (post == 0 && data[i].want_nread == strlen(data[i].inp))
{
int c = getc(fp);
if (c != EOF)
{
printf ("mpf_inp_str didn't read to EOF\n");
printf (" inp \"%s\"\n", data[i].inp);
printf (" base %d\n", data[i].base);
printf (" pre %d\n", pre);
printf (" post %d\n", post);
printf (" c '%c' %#x\n", c, c);
abort ();
}
}
/* only expect "pre" included in the count when non-zero */
want_nread = data[i].want_nread;
if (want_nread != 0)
want_nread += pre;
if (got_nread != want_nread)
{
printf ("mpf_inp_str nread wrong\n");
printf (" inp \"%s\"\n", data[i].inp);
printf (" base %d\n", data[i].base);
printf (" pre %d\n", pre);
printf (" post %d\n", post);
printf (" got_nread %d\n", got_nread);
printf (" want_nread %d\n", want_nread);
abort ();
}
MPF_CHECK_FORMAT (got);
if (mpf_cmp (got, want) != 0)
{
printf ("mpf_inp_str wrong result\n");
printf (" inp \"%s\"\n", data[i].inp);
printf (" base %d\n", data[i].base);
mpf_trace (" got ", got);
mpf_trace (" want", want);
abort ();
}
ASSERT_ALWAYS (fclose (fp) == 0);
}
}
}
mpf_clear (got);
mpf_clear (want);
}
int
main (void)
{
tests_start ();
check_data ();
unlink (FILENAME);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,90 @@
/* Test mpf_integer_p.
Copyright 2001, 2014 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"
void
one (mpf_srcptr f, int want)
{
int got;
got = mpf_integer_p (f);
if (got != want)
{
printf ("mpf_integer_p got %d want %d\n", got, want);
mpf_trace (" f", f);
abort ();
}
}
void
all (mpf_ptr f, int want)
{
one (f, want);
mpf_neg (f, f);
one (f, want);
}
int
main (void)
{
mpf_t f;
tests_start ();
mpf_init2 (f, 200L);
mpf_set_ui (f, 0L);
one (f, 1);
mpf_set_ui (f, 1L);
all (f, 1);
mpf_set_ui (f, 1L);
mpf_div_2exp (f, f, 1L);
all (f, 0);
mpf_set_ui (f, 1L);
mpf_div_2exp (f, f, 5000L);
all (f, 0);
mpf_set_ui (f, 1L);
mpf_mul_2exp (f, f, 5000L);
all (f, 1);
mpf_set_str (f, "0.5", 10);
all (f, 0);
mpf_set_str (f, "2.5", 10);
all (f, 0);
mpf_set_ui (f, 1L);
mpf_div_ui (f, f, 3L);
all (f, 0);
mpf_set_ui (f, 7L);
mpf_div_ui (f, f, 3L);
all (f, 0);
mpf_clear (f);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,164 @@
/* Exercise mpf_mul_ui.
Copyright 2004 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"
void
check_one (const char *desc, mpf_ptr got, mpf_srcptr u, unsigned long v)
{
mp_size_t usize, usign;
mp_ptr wp;
mpf_t want;
MPF_CHECK_FORMAT (got);
/* this code not nailified yet */
ASSERT_ALWAYS (BITS_PER_ULONG <= GMP_NUMB_BITS);
usign = SIZ (u);
usize = ABS (usign);
wp = refmpn_malloc_limbs (usize + 1);
wp[usize] = mpn_mul_1 (wp, PTR(u), usize, (mp_limb_t) v);
PTR(want) = wp;
SIZ(want) = (usign >= 0 ? usize+1 : -(usize+1));
EXP(want) = EXP(u) + 1;
refmpf_normalize (want);
if (! refmpf_validate ("mpf_mul_ui", got, want))
{
mp_trace_base = -16;
printf (" %s\n", desc);
mpf_trace (" u", u);
printf (" v %ld 0x%lX\n", v, v);
abort ();
}
free (wp);
}
void
check_rand (void)
{
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
mpf_t got, u;
unsigned long prec, v;
int i;
/* The nails code in mpf_mul_ui currently isn't exact, so suppress these
tests for now. */
if (BITS_PER_ULONG > GMP_NUMB_BITS)
return;
mpf_init (got);
mpf_init (u);
for (i = 0; i < 200; i++)
{
/* got precision */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (got, prec);
/* u precision */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (u, prec);
/* u, possibly negative */
mpf_random2 (u, PREC(u), (mp_exp_t) 20);
if (gmp_urandomb_ui (rands, 1L))
mpf_neg (u, u);
/* v, 0 to BITS_PER_ULONG bits (inclusive) */
prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1);
v = gmp_urandomb_ui (rands, prec);
if ((i % 2) == 0)
{
/* separate */
mpf_mul_ui (got, u, v);
check_one ("separate", got, u, v);
}
else
{
/* overlap */
prec = refmpf_set_overlap (got, u);
mpf_mul_ui (got, got, v);
check_one ("overlap src==dst", got, u, v);
mpf_set_prec_raw (got, prec);
}
}
mpf_clear (got);
mpf_clear (u);
}
void
check_various (void)
{
mpf_t u, got, want;
const char *s;
mpf_init2 (u, 2*8*sizeof(long));
mpf_init2 (got, 2*8*sizeof(long));
mpf_init2 (want, 2*8*sizeof(long));
s = "0 * ULONG_MAX";
mpf_set_ui (u, 0L);
mpf_mul_ui (got, u, ULONG_MAX);
MPF_CHECK_FORMAT (got);
mpf_set_ui (want, 0L);
if (mpf_cmp (got, want) != 0)
{
error:
printf ("Wrong result from %s\n", s);
mpf_trace ("u ", u);
mpf_trace ("got ", got);
mpf_trace ("want", want);
abort ();
}
s = "1 * ULONG_MAX";
mpf_set_ui (u, 1L);
mpf_mul_ui (got, u, ULONG_MAX);
MPF_CHECK_FORMAT (got);
mpf_set_ui (want, ULONG_MAX);
if (mpf_cmp (got, want) != 0)
goto error;
mpf_clear (u);
mpf_clear (got);
mpf_clear (want);
}
int
main (void)
{
tests_start ();
check_various ();
check_rand ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,158 @@
/* Test mpf_mul, mpf_div, mpf_ui_div, and mpf_div_ui.
Copyright 1996, 2000, 2001, 2003 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"
#ifndef SIZE
#define SIZE 16
#endif
int
main (int argc, char **argv)
{
mp_size_t size;
mp_exp_t exp;
int reps = 10000;
int i;
mpf_t u, v, w, x;
mp_size_t bprec = SIZE * GMP_LIMB_BITS;
mpf_t rerr, limit_rerr;
unsigned long ulimb, vlimb;
int single_flag;
tests_start ();
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init (rerr);
mpf_init (limit_rerr);
mpf_init (u);
mpf_init (v);
mpf_init (w);
mpf_init (x);
for (i = 0; i < reps; i++)
{
mp_size_t res_prec;
res_prec = urandom () % bprec + 1;
mpf_set_prec (w, res_prec);
mpf_set_prec (x, res_prec);
mpf_set_ui (limit_rerr, 1);
mpf_div_2exp (limit_rerr, limit_rerr, res_prec - 1);
single_flag = 0;
if ((urandom () & 1) != 0)
{
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % SIZE;
mpf_random2 (u, size, exp);
}
else
{
ulimb = urandom ();
mpf_set_ui (u, ulimb);
single_flag = 1;
}
if ((urandom () & 1) != 0)
{
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % SIZE;
mpf_random2 (v, size, exp);
}
else
{
vlimb = urandom ();
mpf_set_ui (v, vlimb);
single_flag = 2;
}
if (mpf_sgn (v) == 0)
continue;
mpf_div (w, u, v);
mpf_mul (x, w, v);
mpf_reldiff (rerr, u, x);
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR in mpf_mul or mpf_div after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf (" x = "); mpf_dump (x);
printf (" w = "); mpf_dump (w);
abort ();
}
if (single_flag == 2)
{
mpf_div_ui (x, u, vlimb);
mpf_reldiff (rerr, w, x);
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR in mpf_div or mpf_div_ui after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf (" x = "); mpf_dump (x);
printf (" w = "); mpf_dump (w);
abort ();
}
}
if (single_flag == 1)
{
mpf_ui_div (x, ulimb, v);
mpf_reldiff (rerr, w, x);
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR in mpf_div or mpf_ui_div after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf (" x = "); mpf_dump (x);
printf (" w = "); mpf_dump (w);
abort ();
}
}
}
mpf_clear (rerr);
mpf_clear (limit_rerr);
mpf_clear (u);
mpf_clear (v);
mpf_clear (w);
mpf_clear (x);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,69 @@
/* Test mpf_pow_ui
Copyright 2015 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"
void
check_data (void)
{
unsigned int b, e;
mpf_t b1, r, r2, limit;
mpf_inits (b1, r, r2, NULL);
mpf_init_set_ui (limit, 1);
mpf_mul_2exp (limit, limit, MAX (GMP_NUMB_BITS, 53));
/* This test just test integers with results that fit in a single
limb or 53 bits. This avoids any rounding. */
for (b = 0; b <= 400; b++)
{
mpf_set_ui (b1, b);
mpf_set_ui (r2, 1);
for (e = 0; e <= GMP_LIMB_BITS; e++)
{
mpf_pow_ui (r, b1, e);
if (mpf_cmp (r, r2))
abort ();
mpf_mul_ui (r2, r2, b);
if (mpf_cmp (r2, limit) >= 0)
break;
}
}
mpf_clears (b1, r, r2, limit, NULL);
}
int
main (int argc, char **argv)
{
tests_start ();
check_data ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,112 @@
/* Test mpf_set, mpf_init_set.
Copyright 2004, 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"
void
check_reuse (void)
{
/* Try mpf_set(f,f) when f is bigger than prec. In the past this had
resulted in an MPN_COPY with invalid operand overlap. */
mpf_t f;
mp_size_t limbs = 20;
unsigned long bits = limbs * GMP_NUMB_BITS;
mpf_init2 (f, bits);
refmpf_fill (f, limbs, GMP_NUMB_MAX);
mpf_set_prec_raw (f, bits / 2);
mpf_set (f, f);
MPF_CHECK_FORMAT (f);
mpf_set_prec_raw (f, bits);
mpf_clear (f);
}
void
check_random (long reps)
{
unsigned long test;
gmp_randstate_ptr rands;
mpf_t a, b;
mpz_t z;
int precbits;
#define PRECBITS 10
rands = RANDS;
mpz_init (z);
mpf_init2 (a, 1 << PRECBITS);
for (test = 0; test < reps; test++)
{
mpz_urandomb (z, rands, PRECBITS + 1);
precbits = mpz_get_ui (z) + 1;
mpz_urandomb (z, rands, precbits);
mpz_setbit (z, precbits - 1); /* make sure msb is set */
mpf_set_z (a, z);
if (precbits & 1)
mpf_neg (a, a);
mpz_urandomb (z, rands, PRECBITS);
mpf_div_2exp (a, a, mpz_get_ui (z) + 1);
mpz_urandomb (z, rands, PRECBITS);
precbits -= mpz_get_ui (z);
if (precbits <= 0)
precbits = 1 - precbits;
mpf_set_default_prec (precbits);
mpf_init_set (b, a);
MPF_CHECK_FORMAT (b);
if (!mpf_eq (a, b, precbits))
{
printf ("mpf_init_set wrong.\n");
abort();
}
mpf_set_ui (b, 0);
mpf_set (b, a);
MPF_CHECK_FORMAT (b);
if (!mpf_eq (a, b, precbits))
{
printf ("mpf_set wrong.\n");
abort();
}
mpf_clear (b);
}
mpf_clear (a);
mpz_clear (z);
}
int
main (int argc, char *argv[])
{
long reps = 10000;
tests_start ();
TESTS_REPS (reps, argv, argc);
check_reuse ();
check_random (reps);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,126 @@
/* Test mpf_set_q.
Copyright 2004 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"
void
check_one (mpf_ptr got, mpq_srcptr q)
{
mpf_t n, d;
mpf_set_q (got, q);
PTR(n) = PTR(&q->_mp_num);
SIZ(n) = SIZ(&q->_mp_num);
EXP(n) = ABSIZ(&q->_mp_num);
PTR(d) = PTR(&q->_mp_den);
SIZ(d) = SIZ(&q->_mp_den);
EXP(d) = ABSIZ(&q->_mp_den);
if (! refmpf_validate_division ("mpf_set_q", got, n, d))
{
mp_trace_base = -16;
mpq_trace (" q", q);
abort ();
}
}
void
check_rand (void)
{
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
unsigned long prec;
mpf_t got;
mpq_t q;
int i;
mpf_init (got);
mpq_init (q);
for (i = 0; i < 400; i++)
{
/* result precision */
prec = min_prec + gmp_urandomm_ui (rands, 20L);
refmpf_set_prec_limbs (got, prec);
/* num */
prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
mpz_rrandomb (mpq_numref(q), rands, prec);
/* possibly negative num */
if (gmp_urandomb_ui (rands, 1L))
mpz_neg (mpq_numref(q), mpq_numref(q));
/* den, non-zero */
do {
prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
mpz_rrandomb (mpq_denref(q), rands, prec);
} while (mpz_sgn (mpq_denref(q)) <= 0);
check_one (got, q);
}
mpf_clear (got);
mpq_clear (q);
}
void
check_various (void)
{
mpf_t got;
mpq_t q;
mpf_init (got);
mpq_init (q);
/* 1/1 == 1 */
mpf_set_prec (got, 20L);
mpq_set_ui (q, 1L, 1L);
mpf_set_q (got, q);
MPF_CHECK_FORMAT (got);
ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0);
/* 1/(2^n+1), a case where truncating the divisor would be wrong */
mpf_set_prec (got, 500L);
mpq_set_ui (q, 1L, 1L);
mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L);
mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L);
check_one (got, q);
mpf_clear (got);
mpq_clear (q);
}
int
main (void)
{
tests_start ();
check_various ();
check_rand ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,90 @@
/* Test mpf_set_si and mpf_init_set_si.
Copyright 2000, 2001, 2003 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"
void
check_data (void)
{
static const struct {
long x;
mp_size_t want_size;
mp_limb_t want_data[2];
} data[] = {
{ 0L, 0 },
{ 1L, 1, { 1 } },
{ -1L, -1, { 1 } },
#if GMP_NUMB_BITS >= BITS_PER_ULONG
{ LONG_MAX, 1, { LONG_MAX, 0 } },
{ -LONG_MAX, -1, { LONG_MAX, 0 } },
{ LONG_HIGHBIT, -1, { ULONG_HIGHBIT, 0 } },
#else
{ LONG_MAX, 2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS } },
{ -LONG_MAX, -2, { LONG_MAX & GMP_NUMB_MASK, LONG_MAX >> GMP_NUMB_BITS }},
{ LONG_HIGHBIT, -2, { 0, ULONG_HIGHBIT >> GMP_NUMB_BITS } },
#endif
};
mpf_t x;
int i;
for (i = 0; i < numberof (data); i++)
{
mpf_init (x);
mpf_set_si (x, data[i].x);
MPF_CHECK_FORMAT (x);
if (x->_mp_size != data[i].want_size
|| refmpn_cmp_allowzero (x->_mp_d, data[i].want_data,
ABS (data[i].want_size)) != 0
|| x->_mp_exp != ABS (data[i].want_size))
{
printf ("mpf_set_si wrong on data[%d]\n", i);
abort();
}
mpf_clear (x);
mpf_init_set_si (x, data[i].x);
MPF_CHECK_FORMAT (x);
if (x->_mp_size != data[i].want_size
|| refmpn_cmp_allowzero (x->_mp_d, data[i].want_data,
ABS (data[i].want_size)) != 0
|| x->_mp_exp != ABS (data[i].want_size))
{
printf ("mpf_init_set_si wrong on data[%d]\n", i);
abort();
}
mpf_clear (x);
}
}
int
main (void)
{
tests_start ();
check_data ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,89 @@
/* Test mpf_set_ui and mpf_init_set_ui.
Copyright 2000, 2001, 2003 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"
void
check_data (void)
{
static const struct {
unsigned long x;
mp_size_t want_size;
mp_limb_t want_data[2];
} data[] = {
{ 0L, 0 },
{ 1L, 1, { 1 } },
#if GMP_NUMB_BITS >= BITS_PER_ULONG
{ ULONG_MAX, 1, { ULONG_MAX, 0 } },
{ ULONG_HIGHBIT, 1, { ULONG_HIGHBIT, 0 } },
#else
{ ULONG_MAX, 2, { ULONG_MAX & GMP_NUMB_MASK,
ULONG_MAX >> GMP_NUMB_BITS } },
{ ULONG_HIGHBIT, 2, { 0,
ULONG_HIGHBIT >> GMP_NUMB_BITS } },
#endif
};
mpf_t x;
int i;
for (i = 0; i < numberof (data); i++)
{
mpf_init (x);
mpf_set_ui (x, data[i].x);
MPF_CHECK_FORMAT (x);
if (x->_mp_size != data[i].want_size
|| refmpn_cmp_allowzero (x->_mp_d, data[i].want_data,
ABS (data[i].want_size)) != 0
|| x->_mp_exp != ABS (data[i].want_size))
{
printf ("mpf_set_ui wrong on data[%d]\n", i);
abort();
}
mpf_clear (x);
mpf_init_set_ui (x, data[i].x);
MPF_CHECK_FORMAT (x);
if (x->_mp_size != data[i].want_size
|| refmpn_cmp_allowzero (x->_mp_d, data[i].want_data,
ABS (data[i].want_size)) != 0
|| x->_mp_exp != ABS (data[i].want_size))
{
printf ("mpf_init_set_ui wrong on data[%d]\n", i);
abort();
}
mpf_clear (x);
}
}
int
main (void)
{
tests_start ();
check_data ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,193 @@
/* Test mpf_sqrt, mpf_mul.
Copyright 1996, 2001, 2004 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"
#ifndef SIZE
#define SIZE 16
#endif
void
check_rand1 (int argc, char **argv)
{
mp_size_t size;
mp_exp_t exp;
int reps = 20000;
int i;
mpf_t x, y, y2;
mp_size_t bprec = 100;
mpf_t rerr, max_rerr, limit_rerr;
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init_set_ui (limit_rerr, 1);
mpf_div_2exp (limit_rerr, limit_rerr, bprec);
#if VERBOSE
mpf_dump (limit_rerr);
#endif
mpf_init (rerr);
mpf_init_set_ui (max_rerr, 0);
mpf_init (x);
mpf_init (y);
mpf_init (y2);
for (i = 0; i < reps; i++)
{
size = urandom () % SIZE;
exp = urandom () % SIZE;
mpf_random2 (x, size, exp);
mpf_sqrt (y, x);
MPF_CHECK_FORMAT (y);
mpf_mul (y2, y, y);
mpf_reldiff (rerr, x, y2);
if (mpf_cmp (rerr, max_rerr) > 0)
{
mpf_set (max_rerr, rerr);
#if VERBOSE
mpf_dump (max_rerr);
#endif
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR after %d tests\n", i);
printf (" x = "); mpf_dump (x);
printf (" y = "); mpf_dump (y);
printf (" y2 = "); mpf_dump (y2);
printf (" rerr = "); mpf_dump (rerr);
printf (" limit_rerr = "); mpf_dump (limit_rerr);
printf ("in hex:\n");
mp_trace_base = 16;
mpf_trace (" x ", x);
mpf_trace (" y ", y);
mpf_trace (" y2 ", y2);
mpf_trace (" rerr ", rerr);
mpf_trace (" limit_rerr", limit_rerr);
abort ();
}
}
}
mpf_clear (limit_rerr);
mpf_clear (rerr);
mpf_clear (max_rerr);
mpf_clear (x);
mpf_clear (y);
mpf_clear (y2);
}
void
check_rand2 (void)
{
unsigned long max_prec = 20;
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
unsigned long x_prec, r_prec;
mpf_t x, r, s;
int i;
mpf_init (x);
mpf_init (r);
mpf_init (s);
refmpf_set_prec_limbs (s, 2*max_prec+10);
for (i = 0; i < 500; i++)
{
/* input precision */
x_prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
refmpf_set_prec_limbs (x, x_prec);
/* result precision */
r_prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
refmpf_set_prec_limbs (r, r_prec);
mpf_random2 (x, x_prec, 1000);
mpf_sqrt (r, x);
MPF_CHECK_FORMAT (r);
/* Expect to prec limbs of result.
In the current implementation there's no stripping of low zero
limbs in mpf_sqrt, so size should be exactly prec. */
if (SIZ(r) != r_prec)
{
printf ("mpf_sqrt wrong number of result limbs\n");
mpf_trace (" x", x);
mpf_trace (" r", r);
printf (" r_prec=%lu\n", r_prec);
printf (" SIZ(r) %ld\n", (long) SIZ(r));
printf (" PREC(r) %ld\n", (long) PREC(r));
abort ();
}
/* Must have r^2 <= x, since r has been truncated. */
mpf_mul (s, r, r);
if (! (mpf_cmp (s, x) <= 0))
{
printf ("mpf_sqrt result too big\n");
mpf_trace (" x", x);
printf (" r_prec=%lu\n", r_prec);
mpf_trace (" r", r);
mpf_trace (" s", s);
abort ();
}
/* Must have (r+ulp)^2 > x, or else r is too small. */
refmpf_add_ulp (r);
mpf_mul (s, r, r);
if (! (mpf_cmp (s, x) > 0))
{
printf ("mpf_sqrt result too small\n");
mpf_trace (" x", x);
printf (" r_prec=%lu\n", r_prec);
mpf_trace (" r+ulp", r);
mpf_trace (" s", s);
abort ();
}
}
mpf_clear (x);
mpf_clear (r);
mpf_clear (s);
}
int
main (int argc, char **argv)
{
tests_start ();
mp_trace_base = -16;
check_rand1 (argc, argv);
check_rand2 ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,125 @@
/* Test mpf_sqrt_ui.
Copyright 2004, 2015 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"
void
check_rand (void)
{
unsigned long max_prec = 15;
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
unsigned long x, prec;
mpf_t r, s;
int i;
mpf_init (r);
mpf_init (s);
refmpf_set_prec_limbs (s, 2*max_prec+10);
for (x = 0; x < 2; x++)
{
mpf_sqrt_ui (r, x);
MPF_CHECK_FORMAT (r);
if (mpf_cmp_ui (r, x) != 0)
{
printf ("mpf_sqrt_ui wrong for special case:\n");
printf (" x=%lu\n", x);
mpf_trace (" r", r);
abort ();
}
}
for (i = 0; i < 50; i++)
{
/* input, a random non-zero ulong, exponentially distributed */
do {
x = gmp_urandomb_ui (rands,
gmp_urandomm_ui (rands, BITS_PER_ULONG) + 1);
} while (x <= 1);
/* result precision */
prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
refmpf_set_prec_limbs (r, prec);
mpf_sqrt_ui (r, x);
MPF_CHECK_FORMAT (r);
/* Expect to prec limbs of result.
In the current implementation there's no stripping of low zero
limbs in mpf_sqrt_ui, not even on perfect squares, so size should
be exactly prec. */
if (SIZ(r) != prec)
{
printf ("mpf_sqrt_ui result not enough result limbs\n");
printf (" x=%lu\n", x);
printf (" want prec=%lu\n", prec);
mpf_trace (" r", r);
printf (" r size %ld\n", (long) SIZ(r));
printf (" r prec %ld\n", (long) PREC(r));
abort ();
}
/* Must have r^2 <= x, since r has been truncated. */
mpf_mul (s, r, r);
if (! (mpf_cmp_ui (s, x) <= 0))
{
printf ("mpf_sqrt_ui result too big\n");
printf (" x=%lu\n", x);
printf (" want prec=%lu\n", prec);
mpf_trace (" r", r);
mpf_trace (" s", s);
abort ();
}
/* Must have (r+ulp)^2 > x.
No overflow from refmpf_add_ulp since r is only prec limbs. */
refmpf_add_ulp (r);
mpf_mul (s, r, r);
if (! (mpf_cmp_ui (s, x) > 0))
{
printf ("mpf_sqrt_ui result too small\n");
printf (" x=%lu\n", x);
printf (" want prec=%lu\n", prec);
mpf_trace (" r+ulp", r);
mpf_trace (" s", s);
abort ();
}
}
mpf_clear (r);
mpf_clear (s);
}
int
main (int argc, char **argv)
{
tests_start ();
mp_trace_base = -16;
check_rand ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,287 @@
/* Test mpf_sub.
Copyright 1996, 2001, 2004, 2014 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"
#ifndef SIZE
#define SIZE 16
#endif
void
check_rand (int argc, char **argv)
{
mp_size_t size;
mp_exp_t exp;
int reps = 20000;
int i;
mpf_t u, v, w, wref;
mp_size_t bprec = 100;
mpf_t rerr, max_rerr, limit_rerr;
if (argc > 1)
{
reps = strtol (argv[1], 0, 0);
if (argc > 2)
bprec = strtol (argv[2], 0, 0);
}
mpf_set_default_prec (bprec);
mpf_init_set_ui (limit_rerr, 1);
mpf_div_2exp (limit_rerr, limit_rerr, bprec);
#if VERBOSE
mpf_dump (limit_rerr);
#endif
mpf_init (rerr);
mpf_init_set_ui (max_rerr, 0);
mpf_init (u);
mpf_init (v);
mpf_init (w);
mpf_init (wref);
for (i = 0; i < reps; i++)
{
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % SIZE;
mpf_random2 (u, size, exp);
size = urandom () % (2 * SIZE) - SIZE;
exp = urandom () % SIZE;
mpf_random2 (v, size, exp);
if ((urandom () & 1) != 0)
mpf_add_ui (u, v, 1);
else if ((urandom () & 1) != 0)
mpf_sub_ui (u, v, 1);
mpf_sub (w, u, v);
refmpf_sub (wref, u, v);
mpf_reldiff (rerr, w, wref);
if (mpf_cmp (rerr, max_rerr) > 0)
{
mpf_set (max_rerr, rerr);
#if VERBOSE
mpf_dump (max_rerr);
#endif
if (mpf_cmp (rerr, limit_rerr) > 0)
{
printf ("ERROR after %d tests\n", i);
printf (" u = "); mpf_dump (u);
printf (" v = "); mpf_dump (v);
printf ("wref = "); mpf_dump (wref);
printf (" w = "); mpf_dump (w);
abort ();
}
}
}
mpf_clear (limit_rerr);
mpf_clear (rerr);
mpf_clear (max_rerr);
mpf_clear (u);
mpf_clear (v);
mpf_clear (w);
mpf_clear (wref);
}
#define W GMP_NUMB_MAX
void
check_data (void)
{
static const struct {
struct {
int exp, size;
mp_limb_t d[10];
} x, y, want;
} data[] = {
{ { 123, 2, { 8, 9 } }, { 123, 1, { 9 } }, { 122, 1, { 8 } } },
{ { 1, 1, { 9 } }, { 1, 1, { 8 } }, { 1, 1, { 1 } } },
{ { 1, 1, { 9 } }, { 1, -1, { 6 } }, { 1, 1, { 15 } } },
{ { 1, 2, { 8, 9 } }, { 1, 1, { 8 } }, { 1, 2, { 8, 1 } } },
{ { 2, 2, { 8, 1 } }, { 1, 1, { 9 } }, { 1, 1, { W } } },
{ { 2, 2, { 9, 8 } }, { 1, 1, { 9 } }, { 2, 1, { 8 } } },
{ { 2, 1, { 1 } }, { 1, 1, { 1 } }, { 1, 1, { W } } },
{ { 2, 1, { 9 } }, { 1, 1, { W } }, { 2, 2, { 1, 8 } } },
{ { 1, 2, { W, 8 } }, { 1, 1, { 9 } }, { 0, -1, { 1 } } },
{ { 1, 2, { W, 7 } }, { 1, 1, { 9 } }, { 1, -2, { 1, 1 } } },
{ { 1, 2, { 1, 8 } }, { 1, 1, { 9 } }, { 0, -1, { W } } },
{ { 1, 2, { 1, 7 } }, { 1, 1, { 9 } }, { 1, -2, { W, 1 } } },
{ { 1, 2, { 0, 8 } }, { 1, 1, { 9 } }, { 1, -1, { 1 } } },
{ { 2, 3, { 5, 8, 1 } }, { 1, 1, { 9 } }, { 1, 2, { 5, W } } },
{ { 3, 1, { 1 } }, { 1, 1, { 1 } }, { 2, 2, { W, W } } },
{ { 1, 6, { W, W, W, W, W, 8 } }, { 1, 1, { 9 } }, { -4, -1, { 1 } } },
{ { 5, 5, { W-6, W, W, W, W } }, { 6, 1, { 1 } }, { 1, -1, { 7 } } },
/* f - f == 0, various sizes.
These exercise a past problem (gmp 4.1.3 and earlier) where the
result exponent was not zeroed on a zero result like this. */
{ { 0, 0 }, { 0, 0 }, { 0, 0 } },
{ { 99, 3, { 0, 0, 1 } }, { 99, 1, { 1 } }, { 0, 0 } },
{ { 99, 3, { 0, 123, 456 } }, { 99, 2, { 123, 456 } }, { 0, 0 } },
{ { 99, 3, { 123, 456, 789 } }, { 99, 3, { 123, 456, 789 } }, { 0, 0 } },
/* High limbs cancel, leaving just the low limbs of the longer operand.
This exercises a past problem (gmp 4.1.3 and earlier) where high zero
limbs on the remainder were not stripped before truncating to the
destination, causing loss of precision. */
{ { 123, 2, { 8, 9 } }, { 123, 1, { 9 } }, { 122, 1, { 8 } } },
{ { 123, 3, { 8, 0, 9 } }, { 123, 1, { 9 } }, { 121, 1, { 8 } } },
{ { 123, 4, { 8, 0, 0, 9 } }, { 123, 1, { 9 } }, { 120, 1, { 8 } } },
{ { 123, 5, { 8, 0, 0, 0, 9 } }, { 123, 1, { 9 } }, { 119, 1, { 8 } } },
{ { 123, 6, { 8, 0, 0, 0, 0, 9 } }, { 123, 1, { 9 } }, { 118, 1, { 8 } } },
/* { { 123, 6, { 8, 0, 0, 0, 0, 9 } }, { 123, 6, { 9, 0, 0, 0, 0, 8 } }, { 122, 5, { W, W, W, W, W } } }, */
};
mpf_t x, y, got, want;
int i, swap, fail;
fail = 0;
mp_trace_base = 16;
mpf_init (got);
for (i = 0; i < numberof (data); i++)
{
for (swap = 0; swap <= 7; swap++)
{
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);
PTR(want) = (mp_ptr) data[i].want.d;
SIZ(want) = data[i].want.size;
EXP(want) = data[i].want.exp;
PREC(want) = numberof (data[i].want.d);
MPF_CHECK_FORMAT (want);
if (swap & 4)
{
mpf_swap (want, y);
}
if ((SIZ (x) ^ SIZ (y)) < 0)
continue; /* It's an addition, not a subtraction (TO BE REMOVED) */
if (swap & 1)
{
mpf_swap (x, y);
SIZ(want) = - SIZ(want);
}
if (swap & 2)
{
SIZ(want) = - SIZ(want);
SIZ(x) = - SIZ(x);
SIZ(y) = - SIZ(y);
}
mpf_sub (got, x, y);
/* MPF_CHECK_FORMAT (got); */
if (! refmpf_validate ("mpf_sub", got, want))
{
printf ("check_data() wrong result at data[%d] (operands%s swapped)\n", i, swap ? "" : " not");
mpf_trace ("x ", x);
mpf_trace ("y ", y);
mpf_trace ("got ", got);
mpf_trace ("want", want);
fail = 1;
}
if (SIZ (x) == 1 || SIZ (x) == 0 )
{
if (SIZ (y)) EXP (y) -= EXP (x) - (mp_exp_t) SIZ (x);
if (SIZ (want)) EXP (want) -= EXP (x) - (mp_exp_t) SIZ (x);
EXP (x) = (mp_exp_t) SIZ (x);
if (mpf_fits_uint_p (x))
{
mpf_ui_sub (got, mpf_get_ui (x), y);
if (! refmpf_validate ("mpf_ui_sub", got, want))
{
printf ("check_data() wrong result at data[%d] (operands%s swapped)\n", i, swap ? "" : " not");
mpf_trace ("x ", x);
mpf_trace ("y ", y);
mpf_trace ("got ", got);
mpf_trace ("want", want);
fail = 1;
}
}
}
if (SIZ (y) == 1 || SIZ (y) == 0)
{
if (SIZ (x)) EXP (x) -= EXP (y) - (mp_exp_t) SIZ (y);
if (SIZ (want)) EXP (want) -= EXP (y) - (mp_exp_t) SIZ (y);
EXP (y) = (mp_exp_t) SIZ (y);
if (mpf_fits_uint_p (x))
{
mpf_sub_ui (got, x, mpf_get_ui (y));
if (! refmpf_validate ("mpf_sub_ui", got, want))
{
printf ("check_data() wrong result at data[%d] (operands%s swapped)\n", i, swap ? "" : " not");
mpf_trace ("x ", x);
mpf_trace ("y ", y);
mpf_trace ("got ", got);
mpf_trace ("want", want);
fail = 1;
}
}
}
}
}
mpf_clear (got);
if (fail)
abort ();
}
int
main (int argc, char **argv)
{
tests_start ();
check_data ();
check_rand (argc, argv);
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,270 @@
/* Test mpf_trunc, mpf_ceil, mpf_floor.
Copyright 2001, 2002, 2020 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"
void
check_print (mpf_srcptr src, mpf_srcptr got, mpf_srcptr want)
{
mp_trace_base = 16;
mpf_trace ("src ", src);
mpf_trace ("got ", got);
mpf_trace ("want", want);
printf ("got size=%d exp=%ld\n", SIZ(got), EXP(got));
mpn_trace (" limbs=", PTR(got), (mp_size_t) ABSIZ(got));
printf ("want size=%d exp=%ld\n", SIZ(want), EXP(want));
mpn_trace (" limbs=", PTR(want), (mp_size_t) ABSIZ(want));
}
void
check_one (mpf_srcptr src, mpf_srcptr trunc, mpf_srcptr ceil, mpf_srcptr floor)
{
mpf_t got;
mpf_init2 (got, mpf_get_prec (trunc));
ASSERT_ALWAYS (PREC(got) == PREC(trunc));
ASSERT_ALWAYS (PREC(got) == PREC(ceil));
ASSERT_ALWAYS (PREC(got) == PREC(floor));
#define CHECK_SEP(name, fun, want) \
mpf_set_ui (got, 54321L); /* initial junk */ \
fun (got, src); \
MPF_CHECK_FORMAT (got); \
if (mpf_cmp (got, want) != 0) \
{ \
printf ("%s wrong\n", name); \
check_print (src, got, want); \
abort (); \
}
CHECK_SEP ("mpf_trunc", mpf_trunc, trunc);
CHECK_SEP ("mpf_ceil", mpf_ceil, ceil);
CHECK_SEP ("mpf_floor", mpf_floor, floor);
#define CHECK_INPLACE(name, fun, want) \
mpf_set (got, src); \
fun (got, got); \
MPF_CHECK_FORMAT (got); \
if (mpf_cmp (got, want) != 0) \
{ \
printf ("%s wrong\n", name); \
check_print (src, got, want); \
abort (); \
}
CHECK_INPLACE ("mpf_trunc", mpf_trunc, trunc);
/* Can't do these unconditionally in case truncation by mpf_set strips
some low non-zero limbs which would have rounded the result. */
if (mpf_size (src) <= PREC(trunc)+1)
{
CHECK_INPLACE ("mpf_ceil", mpf_ceil, ceil);
CHECK_INPLACE ("mpf_floor", mpf_floor, floor);
}
mpf_clear (got);
}
void
check_all (mpf_ptr src, mpf_ptr trunc, mpf_ptr ceil, mpf_ptr floor)
{
/* some of these values are generated with direct field assignments */
MPF_CHECK_FORMAT (src);
MPF_CHECK_FORMAT (trunc);
MPF_CHECK_FORMAT (ceil);
MPF_CHECK_FORMAT (floor);
check_one (src, trunc, ceil, floor);
mpf_neg (src, src);
mpf_neg (trunc, trunc);
mpf_neg (ceil, ceil);
mpf_neg (floor, floor);
check_one (src, trunc, floor, ceil);
}
void
check_various (void)
{
mpf_t src, trunc, ceil, floor;
int n, i;
mpf_init2 (src, 512L);
mpf_init2 (trunc, 256L);
mpf_init2 (ceil, 256L);
mpf_init2 (floor, 256L);
/* 0 */
mpf_set_ui (src, 0L);
mpf_set_ui (trunc, 0L);
mpf_set_ui (ceil, 0L);
mpf_set_ui (floor, 0L);
check_all (src, trunc, ceil, floor);
/* 1 */
mpf_set_ui (src, 1L);
mpf_set_ui (trunc, 1L);
mpf_set_ui (ceil, 1L);
mpf_set_ui (floor, 1L);
check_all (src, trunc, ceil, floor);
/* 2^1024 */
mpf_set_ui (src, 1L);
mpf_mul_2exp (src, src, 1024L);
mpf_set (trunc, src);
mpf_set (ceil, src);
mpf_set (floor, src);
check_all (src, trunc, ceil, floor);
/* 1/2^1024, fraction only */
mpf_set_ui (src, 1L);
mpf_div_2exp (src, src, 1024L);
mpf_set_si (trunc, 0L);
mpf_set_si (ceil, 1L);
mpf_set_si (floor, 0L);
check_all (src, trunc, ceil, floor);
/* 1/2 */
mpf_set_ui (src, 1L);
mpf_div_2exp (src, src, 1L);
mpf_set_si (trunc, 0L);
mpf_set_si (ceil, 1L);
mpf_set_si (floor, 0L);
check_all (src, trunc, ceil, floor);
/* 123+1/2^64 */
mpf_set_ui (src, 1L);
mpf_div_2exp (src, src, 64L);
mpf_add_ui (src, src, 123L);
mpf_set_si (trunc, 123L);
mpf_set_si (ceil, 124L);
mpf_set_si (floor, 123L);
check_all (src, trunc, ceil, floor);
/* integer of full prec+1 limbs, unchanged */
n = PREC(trunc)+1;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+100;
mpf_set (trunc, src);
mpf_set (ceil, src);
mpf_set (floor, src);
check_all (src, trunc, ceil, floor);
/* full prec+1 limbs, 1 trimmed for integer */
n = PREC(trunc)+1;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n-1;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+200;
EXP(trunc) = n-1;
SIZ(trunc) = n-1;
for (i = 0; i < SIZ(trunc); i++)
PTR(trunc)[i] = i+201;
mpf_set (floor, trunc);
mpf_add_ui (ceil, trunc, 1L);
check_all (src, trunc, ceil, floor);
/* prec+3 limbs, 2 trimmed for size */
n = PREC(trunc)+3;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+300;
EXP(trunc) = n;
SIZ(trunc) = n-2;
for (i = 0; i < SIZ(trunc); i++)
PTR(trunc)[i] = i+302;
mpf_set (floor, trunc);
mpf_set (ceil, trunc);
PTR(ceil)[0]++;
check_all (src, trunc, ceil, floor);
/* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */
n = PREC(trunc)+4;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n-1;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+400;
EXP(trunc) = n-1;
SIZ(trunc) = n-3;
for (i = 0; i < SIZ(trunc); i++)
PTR(trunc)[i] = i+403;
mpf_set (floor, trunc);
mpf_set (ceil, trunc);
PTR(ceil)[0]++;
check_all (src, trunc, ceil, floor);
/* F.F, carry out of ceil */
EXP(src) = 1;
SIZ(src) = 2;
PTR(src)[0] = GMP_NUMB_MAX;
PTR(src)[1] = GMP_NUMB_MAX;
EXP(trunc) = 1;
SIZ(trunc) = 1;
PTR(trunc)[0] = GMP_NUMB_MAX;
mpf_set (floor, trunc);
EXP(ceil) = 2;
SIZ(ceil) = 1;
PTR(ceil)[0] = 1;
check_all (src, trunc, ceil, floor);
/* FF.F, carry out of ceil */
EXP(src) = 2;
SIZ(src) = 3;
PTR(src)[0] = GMP_NUMB_MAX;
PTR(src)[1] = GMP_NUMB_MAX;
PTR(src)[2] = GMP_NUMB_MAX;
EXP(trunc) = 2;
SIZ(trunc) = 2;
PTR(trunc)[0] = GMP_NUMB_MAX;
PTR(trunc)[1] = GMP_NUMB_MAX;
mpf_set (floor, trunc);
EXP(ceil) = 3;
SIZ(ceil) = 1;
PTR(ceil)[0] = 1;
check_all (src, trunc, ceil, floor);
mpf_clear (src);
mpf_clear (trunc);
mpf_clear (ceil);
mpf_clear (floor);
}
int
main (void)
{
tests_start ();
check_various ();
tests_end ();
exit (0);
}

View File

@@ -0,0 +1,151 @@
/* Test mpf_ui_div.
Copyright 2004 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"
void
check_one (const char *desc, mpf_ptr got, unsigned long u, mpf_srcptr v)
{
mpf_t uf;
mp_limb_t ulimbs[2];
mp_size_t usize;
ulimbs[0] = u & GMP_NUMB_MASK;
usize = (u != 0);
#if BITS_PER_ULONG > GMP_NUMB_BITS
u >>= GMP_NUMB_BITS;
ulimbs[1] = u;
usize += (u != 0);
#endif
PTR(uf) = ulimbs;
SIZ(uf) = usize;
EXP(uf) = usize;
if (! refmpf_validate_division ("mpf_ui_div", got, uf, v))
{
mp_trace_base = -16;
printf (" u 0x%lX (%lu)\n", u, u);
mpf_trace (" v", v);
printf (" %s\n", desc);
abort ();
}
}
void
check_rand (void)
{
unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
gmp_randstate_ptr rands = RANDS;
unsigned long prec, u;
mpf_t got, v;
int i;
mpf_init (got);
mpf_init (v);
for (i = 0; i < 200; i++)
{
/* got precision */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (got, prec);
/* u */
prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1);
u = gmp_urandomb_ui (rands, prec);
/* v precision */
prec = min_prec + gmp_urandomm_ui (rands, 15L);
refmpf_set_prec_limbs (v, prec);
/* v, non-zero */
do {
mpf_random2 (v, PREC(v), (mp_exp_t) 20);
} while (SIZ(v) == 0);
/* v possibly negative */
if (gmp_urandomb_ui (rands, 1L))
mpf_neg (v, v);
if ((i % 2) == 0)
{
/* src != dst */
mpf_ui_div (got, u, v);
check_one ("separate", got, u, v);
}
else
{
/* src == dst */
prec = refmpf_set_overlap (got, v);
mpf_ui_div (got, u, got);
check_one ("overlap src==dst", got, u, v);
mpf_set_prec_raw (got, prec);
}
}
mpf_clear (got);
mpf_clear (v);
}
void
check_various (void)
{
mpf_t got, v;
mpf_init (got);
mpf_init (v);
/* 100/4 == 25 */
mpf_set_prec (got, 20L);
mpf_set_ui (v, 4L);
mpf_ui_div (got, 100L, v);
MPF_CHECK_FORMAT (got);
ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0);
{
/* 1/(2^n+1), a case where truncating the divisor would be wrong */
unsigned long u = 1L;
mpf_set_prec (got, 500L);
mpf_set_prec (v, 900L);
mpf_set_ui (v, 1L);
mpf_mul_2exp (v, v, 800L);
mpf_add_ui (v, v, 1L);
mpf_ui_div (got, u, v);
check_one ("1/2^n+1, separate", got, u, v);
}
mpf_clear (got);
mpf_clear (v);
}
int
main (void)
{
tests_start ();
check_various ();
check_rand ();
tests_end ();
exit (0);
}