summaryrefslogtreecommitdiff
path: root/tests/tinternals.c
blob: 38c65140e42f16f7777eaa0bf5a18999e827c5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* tinternals -- Test for internals.

Copyright 2005-2021 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 MPFR Library 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#define MPFR_NEED_LONGLONG_H
#include "mpfr-test.h"

static void
test_int_ceil_log2 (void)
{
  int i;
  int val[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };

  for (i = 1; i < 17; i++)
    {
      if (MPFR_INT_CEIL_LOG2 (i) != val[i-1])
        {
          printf ("Error 1 in test_int_ceil_log2 for i = %d\n", i);
          exit (1);
        }
      if (MPFR_INT_CEIL_LOG2 (i) != __gmpfr_int_ceil_log2 (i))
        {
          printf ("Error 2 in test_int_ceil_log2 for i = %d\n", i);
          exit (1);
        }
    }
}

static void
test_round_near_x (void)
{
  mpfr_t x, y, z, eps;
  mpfr_exp_t e;
  int failures = 0, mx, neg, err, dir, r, inex, inex2;
  char buffer[7], *p;

  mpfr_inits (x, y, z, eps, (mpfr_ptr) 0);
  mpfr_set_prec (x, 5);
  mpfr_set_prec (y, 3);
  mpfr_set_prec (z, 3);
  mpfr_set_prec (eps, 2);
  mpfr_set_ui_2exp (eps, 1, -32, MPFR_RNDN);

  for (mx = 16; mx < 32; mx++)
    {
      mpfr_set_ui_2exp (x, mx, -2, MPFR_RNDN);
      for (p = buffer, neg = 0;
           neg <= 1;
           mpfr_neg (x, x, MPFR_RNDN), p++, neg++)
        for (err = 2; err <= 6; err++)
          for (dir = 0; dir <= 1; dir++)
            RND_LOOP_NO_RNDF (r)
              {
                inex = mpfr_round_near_x (y, x, err, dir, (mpfr_rnd_t) r);

                if (inex == 0 && err < 6)
                  {
                    /* The test is more restrictive than necessary.
                       So, no failure in this case. */
                    continue;
                  }

                inex2 = ((dir ^ neg) ? mpfr_add : mpfr_sub)
                  (z, x, eps, (mpfr_rnd_t) r);
                if (inex * inex2 <= 0)
                  printf ("Bad return value (%d instead of %d) for:\n",
                          inex, inex2);
                else if (mpfr_equal_p (y, z))
                  continue;  /* correct inex and y */
                else
                  {
                    printf ("Bad MPFR value (should have got ");
                    mpfr_out_str (stdout, 2, 3, z, MPFR_RNDZ);
                    printf (") for:\n");
                  }

                if (!mpfr_get_str (buffer, &e, 2, 5, x, MPFR_RNDZ) || e != 3)
                  {
                    printf ("mpfr_get_str failed in test_round_near_x\n");
                    exit (1);
                  }
                printf ("x = %c%c%c%c.%c%c, ", neg ? '-' : '+',
                        p[0], p[1], p[2], p[3], p[4]);
                printf ("err = %d, dir = %d, r = %s --> inex = %2d",
                        err, dir, mpfr_print_rnd_mode ((mpfr_rnd_t) r), inex);
                if (inex != 0)
                  {
                    printf (", y = ");
                    mpfr_out_str (stdout, 2, 3, y, MPFR_RNDZ);
                  }
                printf ("\n");
                if (inex == 0)
                  printf ("Rounding was possible!\n");
                if (++failures == 10)  /* show at most 10 failures */
                  exit (1);
              }
    }

  if (failures)
    exit (1);

  mpfr_clears (x, y, z, eps, (mpfr_ptr) 0);
}

static void
test_set_prec_raw (void)
{
  mpfr_t x;
  int i;

  mpfr_init2 (x, 53);
  for (i = 2; i < 11; i++)
    {
      mpfr_set_prec_raw (x, i);
      if (MPFR_PREC (x) != i)
        {
          printf ("[ERROR]: mpfr_set_prec_raw %d\n", i);
          exit (1);
        }
    }
  mpfr_clear (x);
}

int
main (int argc, char **argv)
{
  tests_start_mpfr ();

  test_int_ceil_log2 ();

  test_round_near_x ();
  test_set_prec_raw ();

  tests_end_mpfr ();
  return 0;
}