Blame tests/mpf/t-get_d_2exp.c

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