|
Packit |
5c3484 |
/* Test mpz_set_f.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright 2001 Free Software Foundation, Inc.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
This file is part of the GNU MP Library test suite.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is free software; you can redistribute it
|
|
Packit |
5c3484 |
and/or modify it under the terms of the GNU General Public License as
|
|
Packit |
5c3484 |
published by the Free Software Foundation; either version 3 of the License,
|
|
Packit |
5c3484 |
or (at your option) any later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is distributed in the hope that it will be
|
|
Packit |
5c3484 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5c3484 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit |
5c3484 |
Public License for more details.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
You should have received a copy of the GNU General Public License along with
|
|
Packit |
5c3484 |
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include <stdio.h>
|
|
Packit |
5c3484 |
#include <stdlib.h>
|
|
Packit |
5c3484 |
#include "gmp.h"
|
|
Packit |
5c3484 |
#include "gmp-impl.h"
|
|
Packit |
5c3484 |
#include "tests.h"
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
check_one (mpz_srcptr z)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
static const int shift[] = {
|
|
Packit |
5c3484 |
0, 1, GMP_LIMB_BITS, 2*GMP_LIMB_BITS, 5*GMP_LIMB_BITS
|
|
Packit |
5c3484 |
};
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
int sh, shneg, neg;
|
|
Packit |
5c3484 |
mpf_t f;
|
|
Packit |
5c3484 |
mpz_t got, want;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpf_init2 (f, mpz_sizeinbase(z,2));
|
|
Packit |
5c3484 |
mpz_init (got);
|
|
Packit |
5c3484 |
mpz_init (want);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
for (sh = 0; sh < numberof(shift); sh++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
for (shneg = 0; shneg <= 1; shneg++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
for (neg = 0; neg <= 1; neg++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpf_set_z (f, z);
|
|
Packit |
5c3484 |
mpz_set (want, z);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (neg)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpf_neg (f, f);
|
|
Packit |
5c3484 |
mpz_neg (want, want);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (shneg)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_tdiv_q_2exp (want, want, shift[sh]);
|
|
Packit |
5c3484 |
mpf_div_2exp (f, f, shift[sh]);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
else
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_mul_2exp (want, want, shift[sh]);
|
|
Packit |
5c3484 |
mpf_mul_2exp (f, f, shift[sh]);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_set_f (got, f);
|
|
Packit |
5c3484 |
MPZ_CHECK_FORMAT (got);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (mpz_cmp (got, want) != 0)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
printf ("wrong result\n");
|
|
Packit |
5c3484 |
printf (" shift %d\n", shneg ? -shift[sh] : shift[sh]);
|
|
Packit |
5c3484 |
printf (" neg %d\n", neg);
|
|
Packit |
5c3484 |
mpf_trace (" f", f);
|
|
Packit |
5c3484 |
mpz_trace (" got", got);
|
|
Packit |
5c3484 |
mpz_trace (" want", want);
|
|
Packit |
5c3484 |
abort ();
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpf_clear (f);
|
|
Packit |
5c3484 |
mpz_clear (got);
|
|
Packit |
5c3484 |
mpz_clear (want);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
check_various (void)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mpz_t z;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_init (z);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_set_ui (z, 0L);
|
|
Packit |
5c3484 |
check_one (z);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_set_si (z, 123L);
|
|
Packit |
5c3484 |
check_one (z);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_rrandomb (z, RANDS, 2*GMP_LIMB_BITS);
|
|
Packit |
5c3484 |
check_one (z);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_rrandomb (z, RANDS, 5*GMP_LIMB_BITS);
|
|
Packit |
5c3484 |
check_one (z);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_clear (z);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
int
|
|
Packit |
5c3484 |
main (int argc, char *argv[])
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
#if GMP_NAIL_BITS == 0
|
|
Packit |
5c3484 |
tests_start ();
|
|
Packit |
5c3484 |
mp_trace_base = 16;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
check_various ();
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
tests_end ();
|
|
Packit |
5c3484 |
#endif
|
|
Packit |
5c3484 |
exit (0);
|
|
Packit |
5c3484 |
}
|