Blame tests/mpf/t-dm2exp.c

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