summaryrefslogtreecommitdiff
path: root/tests/tget_sj.c
blob: 4f400937c4134ef6d52046946fb1c8f4f3c94e5a (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Test file for mpfr_get_sj and mpfr_get_uj.

Copyright 2004-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_INTMAX_H
#include "mpfr-test.h"

#ifndef _MPFR_H_HAVE_INTMAX_T

int
main (void)
{
  return 77;
}

#else

#ifndef NPRINTF_J
#define PRMAX(SPEC,V) printf (" %j" SPEC ",", V)
#else
#define PRMAX(SPEC,V) (void) 0
#endif

static void
check_sj (intmax_t s, mpfr_ptr x)
{
  mpfr_exp_t emin, emax;
  mpfr_t y;
  int i;

  mpfr_init2 (y, MPFR_PREC (x) + 2);

  emin = mpfr_get_emin ();
  emax = mpfr_get_emax ();

  for (i = -1; i <= 1; i++)
    {
      int rnd;
      int inex;
      int fi, e;
      mpfr_flags_t flags[2] = { 0, MPFR_FLAGS_ALL }, ex_flags, gt_flags;

      inex = mpfr_set_si_2exp (y, i, -2, MPFR_RNDN);
      MPFR_ASSERTN (inex == 0);
      inex = mpfr_add (y, y, x, MPFR_RNDN);
      MPFR_ASSERTN (inex == 0);
      /* y = x + i/4, with -1 <= i <= 1 */
      RND_LOOP (rnd)
        for (fi = 0; fi < numberof (flags); fi++)
          {
            intmax_t r;

            if (rnd == MPFR_RNDZ && i < 0 && s >= 0)
              continue;
            if (rnd == MPFR_RNDZ && i > 0 && s <= 0)
              continue;
            if (rnd == MPFR_RNDD && i < 0)
              continue;
            if (rnd == MPFR_RNDU && i > 0)
              continue;
            if (rnd == MPFR_RNDA && ((MPFR_IS_POS(y) && i > 0) ||
                                     (MPFR_IS_NEG(y) && i < 0)))
              continue;

            for (e = 0; e < 2; e++)
              {
                if (e)
                  {
                    mpfr_exp_t ey;

                    if (MPFR_IS_ZERO (y))
                      break;
                    ey = MPFR_GET_EXP (y);
                    set_emin (ey);
                    set_emax (ey);
                  }
                /* rint (y) == x == s */
                __gmpfr_flags = ex_flags = flags[fi];
                if (i != 0)
                  ex_flags |= MPFR_FLAGS_INEXACT;
                r = mpfr_get_sj (y, (mpfr_rnd_t) rnd);
                gt_flags = __gmpfr_flags;
                set_emin (emin);
                set_emax (emax);
                if ((r != s || gt_flags != ex_flags) && rnd != MPFR_RNDF)
                  {
                    printf ("Error in check_sj for fi = %d, y = ", fi);
                    mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
                    printf (" in %s%s\n",
                            mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
                            e ? ", reduced exponent range" : "");
                    printf ("Expected:");
                    PRMAX ("d", s);
                    flags_out (ex_flags);
                    printf ("Got:     ");
                    PRMAX ("d", r);
                    flags_out (gt_flags);
                    exit (1);
                  }
              }
          }
    }

  mpfr_clear (y);
}

static void
check_uj (uintmax_t u, mpfr_ptr x)
{
  mpfr_exp_t emin, emax;
  mpfr_t y;
  int i;

  mpfr_init2 (y, MPFR_PREC (x) + 2);

  emin = mpfr_get_emin ();
  emax = mpfr_get_emax ();

  for (i = -1; i <= 1; i++)
    {
      int rnd;
      int inex;
      int fi, e;
      mpfr_flags_t flags[2] = { 0, MPFR_FLAGS_ALL }, ex_flags, gt_flags;

      inex = mpfr_set_si_2exp (y, i, -2, MPFR_RNDN);
      MPFR_ASSERTN (inex == 0);
      inex = mpfr_add (y, y, x, MPFR_RNDN);
      MPFR_ASSERTN (inex == 0);
      /* y = x + i/4, with -1 <= i <= 1 */
      RND_LOOP (rnd)
        for (fi = 0; fi < numberof (flags); fi++)
          {
            uintmax_t r;

            if (rnd == MPFR_RNDZ && i < 0)
              continue;
            if (rnd == MPFR_RNDD && i < 0)
              continue;
            if (rnd == MPFR_RNDU && i > 0)
              continue;
            if (rnd == MPFR_RNDA && ((MPFR_IS_POS(y) && i > 0) ||
                                     (MPFR_IS_NEG(y) && i < 0)))
              continue;

            for (e = 0; e < 2; e++)
              {
                if (e)
                  {
                    mpfr_exp_t ey;

                    if (MPFR_IS_ZERO (y))
                      break;
                    ey = MPFR_GET_EXP (y);
                    set_emin (ey);
                    set_emax (ey);
                  }
                /* rint (y) == x == u */
                __gmpfr_flags = ex_flags = flags[fi];
                if (i != 0)
                  ex_flags |= MPFR_FLAGS_INEXACT;
                r = mpfr_get_uj (y, (mpfr_rnd_t) rnd);
                gt_flags = __gmpfr_flags;
                set_emin (emin);
                set_emax (emax);
                if ((r != u || gt_flags != ex_flags) && rnd != MPFR_RNDF)
                  {
                    printf ("Error in check_uj for fi = %d, y = ", fi);
                    mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
                    printf (" in %s%s\n",
                            mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
                            e ? ", reduced exponent range" : "");
                    printf ("Expected:");
                    PRMAX ("u", u);
                    flags_out (ex_flags);
                    printf ("Got:     ");
                    PRMAX ("u", r);
                    flags_out (gt_flags);
                    exit (1);
                  }
              }
          }
    }

  mpfr_clear (y);
}

#define CHECK_ERANGE(F,FMT,RES,INPUT,VALUE,E)                           \
  do                                                                    \
    {                                                                   \
      __gmpfr_flags = ex_flags = flags[fi];                             \
      RES = F (x, (mpfr_rnd_t) rnd);                                    \
      gt_flags = __gmpfr_flags;                                         \
      if (E)                                                            \
        ex_flags |= MPFR_FLAGS_ERANGE;                                  \
      if (RES == VALUE && gt_flags == ex_flags)                         \
        continue;                                                       \
      printf ("Error in check_erange for %s, %s, fi = %d on %s\n",      \
              #F, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), fi, INPUT);   \
      printf ("Expected:");                                             \
      PRMAX (FMT, VALUE);                                               \
      flags_out (ex_flags);                                             \
      printf ("Got:     ");                                             \
      PRMAX (FMT, RES);                                                 \
      flags_out (gt_flags);                                             \
      exit (1);                                                         \
    }                                                                   \
  while (0)

#define CHECK_ERANGE_U(INPUT,VALUE,E) \
  CHECK_ERANGE (mpfr_get_uj, "u", u, INPUT, (uintmax_t) VALUE, E)
#define CHECK_ERANGE_S(INPUT,VALUE,E) \
  CHECK_ERANGE (mpfr_get_sj, "d", d, INPUT, (intmax_t) VALUE, E)

static void
check_erange (void)
{
  mpfr_t x;
  uintmax_t u;
  intmax_t d;
  int rnd;
  int fi;
  mpfr_flags_t flags[3] = { 0, MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE,
                            MPFR_FLAGS_ALL }, ex_flags, gt_flags;

  /* Test for ERANGE flag + correct behavior if overflow */

  mpfr_init2 (x, 256);

  RND_LOOP (rnd)
    for (fi = 0; fi < numberof (flags); fi++)
      {
        mpfr_set_uj (x, UINTMAX_MAX, MPFR_RNDN);
        CHECK_ERANGE_U ("UINTMAX_MAX", UINTMAX_MAX, 0);
        mpfr_add_ui (x, x, 1, MPFR_RNDN);
        CHECK_ERANGE_U ("UINTMAX_MAX+1", UINTMAX_MAX, 1);
        mpfr_set_sj (x, -1, MPFR_RNDN);
        CHECK_ERANGE_U ("-1", 0, 1);
        mpfr_set_sj (x, INTMAX_MAX, MPFR_RNDN);
        CHECK_ERANGE_S ("INTMAX_MAX", INTMAX_MAX, 0);
        mpfr_add_ui (x, x, 1, MPFR_RNDN);
        CHECK_ERANGE_S ("INTMAX_MAX+1", INTMAX_MAX, 1);
        mpfr_set_sj (x, INTMAX_MIN, MPFR_RNDN);
        CHECK_ERANGE_S ("INTMAX_MIN", INTMAX_MIN, 0);
        mpfr_sub_ui (x, x, 1, MPFR_RNDN);
        CHECK_ERANGE_S ("INTMAX_MIN-1", INTMAX_MIN, 1);
        mpfr_set_nan (x);
        CHECK_ERANGE_U ("NaN", 0, 1);
        CHECK_ERANGE_S ("NaN", 0, 1);
      }

  mpfr_clear (x);
}

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

  mpfr_init2 (x, 64);

  for (i = 1; i <= 4; i++)
    {
      int r;

      mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN);
      RND_LOOP (r)
        {
          intmax_t s;
          uintmax_t u;

          mpfr_clear_erangeflag ();
          s = mpfr_get_sj (x, r != MPFR_RNDF ? (mpfr_rnd_t) r : MPFR_RNDA);
          if (mpfr_erangeflag_p ())
            {
              printf ("ERROR for get_sj + ERANGE + small negative op"
                      " for rnd = %s and x = -%d/4\n",
                      mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
              exit (1);
            }
          u = mpfr_get_uj (x, (mpfr_rnd_t) r);
          if (u != 0)
            {
              printf ("ERROR for get_uj + ERANGE + small negative op"
                      " for rnd = %s and x = -%d/4\n",
                      mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
#ifndef NPRINTF_J
              printf ("Expected 0, got %ju\n", u);
#endif
              exit (1);
            }
          if ((s == 0) ^ !mpfr_erangeflag_p ())
            {
              const char *Not = s == 0 ? "" : " not";

              printf ("ERROR for get_uj + ERANGE + small negative op"
                      " for rnd = %s and x = -%d/4\n",
                      mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
              printf ("The rounded integer ");
#ifndef NPRINTF_J
              printf("(%jd) ", s);
#endif
              printf("is%s representable in unsigned long,\n"
                     "but the erange flag is%s set.\n", Not, Not);
              exit (1);
            }
        }
    }

  mpfr_clear (x);
}

int
main (void)
{
  mpfr_prec_t prec;
  mpfr_t x, y;
  intmax_t s;
  uintmax_t u;

  tests_start_mpfr ();

  for (u = UINTMAX_MAX, prec = 0; u != 0; u /= 2, prec++)
    { }

  mpfr_init2 (x, prec + 4);
  mpfr_init2 (y, prec + 4);

  mpfr_set_ui (x, 0, MPFR_RNDN);
  check_sj (0, x);
  check_uj (0, x);

  mpfr_set_ui (x, 1, MPFR_RNDN);
  check_sj (1, x);
  check_uj (1, x);

  mpfr_neg (x, x, MPFR_RNDN);
  check_sj (-1, x);

  mpfr_set_si_2exp (x, 1, prec, MPFR_RNDN);
  mpfr_sub_ui (x, x, 1, MPFR_RNDN); /* UINTMAX_MAX */

  mpfr_div_ui (y, x, 2, MPFR_RNDZ);
  mpfr_trunc (y, y); /* INTMAX_MAX */
  for (s = INTMAX_MAX; s != 0; s /= 17)
    {
      check_sj (s, y);
      mpfr_div_ui (y, y, 17, MPFR_RNDZ);
      mpfr_trunc (y, y);
    }

  mpfr_div_ui (y, x, 2, MPFR_RNDZ);
  mpfr_trunc (y, y); /* INTMAX_MAX */
  mpfr_neg (y, y, MPFR_RNDN);
  if (INTMAX_MIN + INTMAX_MAX != 0)
    mpfr_sub_ui (y, y, 1, MPFR_RNDN); /* INTMAX_MIN */
  for (s = INTMAX_MIN; s != 0; s /= 17)
    {
      check_sj (s, y);
      mpfr_div_ui (y, y, 17, MPFR_RNDZ);
      mpfr_trunc (y, y);
    }

  for (u = UINTMAX_MAX; u != 0; u /= 17)
    {
      check_uj (u, x);
      mpfr_div_ui (x, x, 17, MPFR_RNDZ);
      mpfr_trunc (x, x);
    }

  mpfr_clear (x);
  mpfr_clear (y);

  check_erange ();
  test_get_uj_smallneg ();

  tests_end_mpfr ();
  return 0;
}

#endif