summaryrefslogtreecommitdiff
path: root/tests/taway.c
blob: a748b04cd6a92ab631ee34b17840baf36447c7a8 (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* Test file for round away.

Copyright 2000-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. */

#include "mpfr-test.h"

#define DISP(s,t)                                       \
  do                                                    \
    {                                                   \
      printf (s);                                       \
      mpfr_out_str (stdout, 2, 0, t, MPFR_RNDN);        \
    }                                                   \
  while (0)

#define DISP2(s,t) do { DISP(s,t); putchar ('\n'); } while (0)

#define SPECIAL_MAX 12

static void
set_special (mpfr_ptr x, unsigned int select)
{
  MPFR_ASSERTN (select < SPECIAL_MAX);
  switch (select)
    {
    case 0:
      MPFR_SET_NAN (x);
      break;
    case 1:
      MPFR_SET_INF (x);
      MPFR_SET_POS (x);
      break;
    case 2:
      MPFR_SET_INF (x);
      MPFR_SET_NEG (x);
      break;
    case 3:
      MPFR_SET_ZERO (x);
      MPFR_SET_POS  (x);
      break;
    case 4:
      MPFR_SET_ZERO (x);
      MPFR_SET_NEG  (x);
      break;
    case 5:
      mpfr_set_str_binary (x, "1");
      break;
    case 6:
      mpfr_set_str_binary (x, "-1");
      break;
    case 7:
      mpfr_set_str_binary (x, "1e-1");
      break;
    case 8:
      mpfr_set_str_binary (x, "1e+1");
      break;
    case 9:
      mpfr_const_pi (x, MPFR_RNDN);
      break;
    case 10:
      mpfr_const_pi (x, MPFR_RNDN);
      MPFR_SET_EXP (x, MPFR_GET_EXP (x)-1);
      break;
    default:
      mpfr_urandomb (x, RANDS);
      break;
    }
}
/* same than mpfr_cmp, but returns 0 for both NaN's */
static int
mpfr_compare (mpfr_srcptr a, mpfr_srcptr b)
{
  return (MPFR_IS_NAN(a)) ? !MPFR_IS_NAN(b) :
    (MPFR_IS_NAN(b) || mpfr_cmp(a, b));
}

static void
test3 (int (*testfunc)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t),
       const char *foo)
{
  mpfr_t ref1, ref2, ref3;
  mpfr_t res1;
  mpfr_prec_t p1, p2, p3;
  int i, inexa, inexd;
  mpfr_rnd_t r;

  p1 = (randlimb () % 200) + MPFR_PREC_MIN;
  p2 = (randlimb () % 200) + MPFR_PREC_MIN;
  p3 = (randlimb () % 200) + MPFR_PREC_MIN;

  mpfr_init2 (ref1, p1);
  mpfr_init2 (ref2, p2);
  mpfr_init2 (ref3, p3);
  mpfr_init2 (res1, p1);

  /* for each variable, consider each of the following 6 possibilities:
     NaN, +Infinity, -Infinity, +0, -0 or a random number */
  for (i = 0; i < SPECIAL_MAX * SPECIAL_MAX; i++)
    {
      set_special (ref2, i%SPECIAL_MAX);
      set_special (ref3, i/SPECIAL_MAX);

      inexa = testfunc (res1, ref2, ref3, MPFR_RNDA);
      r = MPFR_IS_POS (res1) ? MPFR_RNDU : MPFR_RNDD;
      inexd = testfunc (ref1, ref2, ref3, r);

      if (mpfr_compare (res1, ref1) || inexa != inexd)
        {
          printf ("Error with RNDA for %s with ", foo);
          DISP("x=",ref2); DISP2(", y=",ref3);
          printf ("inexa=%d inexd=%d\n", inexa, inexd);
          printf ("expected "); mpfr_dump (ref1);
          printf ("got      "); mpfr_dump (res1);
          exit (1);
        }
    }

  mpfr_clear (ref1);
  mpfr_clear (ref2);
  mpfr_clear (ref3);
  mpfr_clear (res1);
}

static void
test4 (int (*testfunc)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr,
                       mpfr_rnd_t), const char *foo)
{
  mpfr_t ref, op1, op2, op3;
  mpfr_prec_t pout, p1, p2, p3;
  mpfr_t res;
  int i, j, k, inexa, inexd;
  mpfr_rnd_t r;

  pout = (randlimb () % 200) + MPFR_PREC_MIN;
  p1 = (randlimb () % 200) + MPFR_PREC_MIN;
  p2 = (randlimb () % 200) + MPFR_PREC_MIN;
  p3 = (randlimb () % 200) + MPFR_PREC_MIN;

  mpfr_init2 (ref, pout);
  mpfr_init2 (res, pout);
  mpfr_init2 (op1, p1);
  mpfr_init2 (op2, p2);
  mpfr_init2 (op3, p3);

  /* for each variable, consider each of the following 6 possibilities:
     NaN, +Infinity, -Infinity, +0, -0 or a random number */

  for (i = 0; i < SPECIAL_MAX; i++)
    {
      set_special (op1, i);
      for (j = 0; j < SPECIAL_MAX; j++)
        {
          set_special (op2, j);
          for (k = 0; k < SPECIAL_MAX; k++)
            {
              set_special (op3, k);

              inexa = testfunc (res, op1, op2, op3, MPFR_RNDA);
              r = MPFR_IS_POS (res) ? MPFR_RNDU : MPFR_RNDD;
              inexd = testfunc (ref, op1, op2, op3, r);

              if (mpfr_compare (res, ref) || inexa != inexd)
                {
                  printf ("Error with RNDA for %s with ", foo);
                  DISP("a=", op1); DISP(", b=", op2); DISP2(", c=", op3);
                  printf ("inexa=%d inexd=%d\n", inexa, inexd);
                  DISP("expected ", ref); DISP2(", got ", res);
                  exit (1);
                }
            }
        }
    }

  mpfr_clear (ref);
  mpfr_clear (op1);
  mpfr_clear (op2);
  mpfr_clear (op3);
  mpfr_clear (res);
}

static void
test2ui (int (*testfunc)(mpfr_ptr, mpfr_srcptr, unsigned long int, mpfr_rnd_t),
         const char *foo)
{
  mpfr_t ref1, ref2;
  unsigned int ref3;
  mpfr_t res1;
  mpfr_prec_t p1, p2;
  int i, inexa, inexd;
  mpfr_rnd_t r;

  p1 = (randlimb () % 200) + MPFR_PREC_MIN;
  p2 = (randlimb () % 200) + MPFR_PREC_MIN;

  mpfr_init2 (ref1, p1);
  mpfr_init2 (ref2, p2);
  mpfr_init2 (res1, p1);

  /* ref2 can be NaN, +Inf, -Inf, +0, -0 or any number
     ref3 can be 0 or any number */
  for (i = 0; i < SPECIAL_MAX * 2; i++)
    {
      set_special (ref2, i % SPECIAL_MAX);
      ref3 = i / SPECIAL_MAX == 0 ? 0 : randlimb ();

      inexa = testfunc (res1, ref2, ref3, MPFR_RNDA);
      r = MPFR_IS_POS (res1) ? MPFR_RNDU : MPFR_RNDD;
      inexd = testfunc (ref1, ref2, ref3, r);

      if (mpfr_compare (res1, ref1) || inexa != inexd)
        {
          printf ("Error with RNDA for %s for c=%u\n", foo, ref3);
          DISP2("a=",ref2);
          printf ("inexa=%d inexd=%d\n", inexa, inexd);
          printf ("expected "); mpfr_dump (ref1);
          printf ("got      "); mpfr_dump (res1);
          exit (1);
        }
    }

  mpfr_clear (ref1);
  mpfr_clear (ref2);
  mpfr_clear (res1);
}

static void
testui2 (int (*testfunc)(mpfr_ptr, unsigned long int, mpfr_srcptr, mpfr_rnd_t),
         const char *foo)
{
  mpfr_t ref1, ref3;
  unsigned int ref2;
  mpfr_t res1;
  mpfr_prec_t p1, p3;
  int i, inexa, inexd;
  mpfr_rnd_t r;

  p1 = (randlimb () % 200) + MPFR_PREC_MIN;
  p3 = (randlimb () % 200) + MPFR_PREC_MIN;

  mpfr_init2 (ref1, p1);
  mpfr_init2 (ref3, p3);
  mpfr_init2 (res1, p1);

  for (i = 0; i < SPECIAL_MAX * 2; i++)
    {
      set_special (ref3, i % SPECIAL_MAX);
      ref2 = i / SPECIAL_MAX == 0 ? 0 : randlimb ();

      inexa = testfunc (res1, ref2, ref3, MPFR_RNDA);
      r = MPFR_IS_POS (res1) ? MPFR_RNDU : MPFR_RNDD;
      inexd = testfunc (ref1, ref2, ref3, r);

      if (mpfr_compare (res1, ref1) || inexa != inexd)
        {
          printf ("Error with RNDA for %s for b=%u\n", foo, ref2);
          DISP2("a=", ref3);
          printf ("inexa=%d inexd=%d\n", inexa, inexd);
          DISP("expected ", ref1); DISP2(", got ", res1);
          exit (1);
        }
    }

  mpfr_clear (ref1);
  mpfr_clear (ref3);
  mpfr_clear (res1);
}

/* foo(mpfr_ptr, mpfr_srcptr, mp_rndt) */
static void
test2 (int (*testfunc)(mpfr_ptr, mpfr_srcptr, mpfr_rnd_t), const char *foo)
{
  mpfr_t ref1, ref2;
  mpfr_t res1;
  mpfr_prec_t p1, p2;
  int i, inexa, inexd;
  mpfr_rnd_t r;

  p1 = (randlimb () % 200) + MPFR_PREC_MIN;
  p2 = (randlimb () % 200) + MPFR_PREC_MIN;

  mpfr_init2 (ref1, p1);
  mpfr_init2 (ref2, p2);
  mpfr_init2 (res1, p1);

  for (i = 0; i < SPECIAL_MAX; i++)
    {
      set_special (ref2, i);

      /* first round to away */
      inexa = testfunc (res1, ref2, MPFR_RNDA);

      r = MPFR_IS_POS (res1) ? MPFR_RNDU : MPFR_RNDD;
      inexd = testfunc (ref1, ref2, r);
      if (mpfr_compare (res1, ref1) || inexa != inexd)
        {
          printf ("Error with RNDA for %s with ", foo);
          DISP2("x=", ref2);
          printf ("inexa=%d inexd=%d\n", inexa, inexd);
          DISP("expected ", ref1); DISP2(", got ", res1);
          exit (1);
        }
    }

  mpfr_clear (ref1);
  mpfr_clear (ref2);
  mpfr_clear (res1);
}

/* one operand, two results, like mpfr_sin_cos */
static void
test3a (int (*testfunc)(mpfr_ptr, mpfr_ptr, mpfr_srcptr, mpfr_rnd_t),
        const char *foo)
{
  mpfr_t ref1, ref2, ref3;
  mpfr_t res1, res2;
  mpfr_prec_t p1, p2, p3;
  int i, inexa, inexd;
  mpfr_rnd_t r;

  p1 = (randlimb () % 200) + MPFR_PREC_MIN;
  p2 = (randlimb () % 200) + MPFR_PREC_MIN;
  p3 = (randlimb () % 200) + MPFR_PREC_MIN;

  mpfr_init2 (ref1, p1);
  mpfr_init2 (ref2, p2);
  mpfr_init2 (ref3, p3);
  mpfr_init2 (res1, p1);
  mpfr_init2 (res2, p2);

  for (i = 0; i < SPECIAL_MAX; i++)
    {
      set_special (ref3, i);

      inexa = testfunc (res1, res2, ref3, MPFR_RNDA);

      /* first check wrt the first operand */
      r = MPFR_IS_POS (res1) ? MPFR_RNDU : MPFR_RNDD;
      inexd = testfunc (ref1, ref2, ref3, r);
      /* the low 2 bits of the inexact flag concern the 1st operand */
      if (mpfr_compare (res1, ref1) || (inexa & 3) != (inexd & 3))
        {
          printf ("Error with RNDA for %s (1st operand)\n", foo);
          DISP2("a=",ref3);
          DISP("expected ", ref1); printf ("\n");
          DISP("got      ", res1); printf ("\n");
          printf ("inexa=%d inexd=%d\n", inexa & 3, inexd & 3);
          exit (1);
        }

      /* now check wrt the second operand */
      r = MPFR_IS_POS (res2) ? MPFR_RNDU : MPFR_RNDD;
      inexd = testfunc (ref1, ref2, ref3, r);
      /* bits 2..3 of the inexact flag concern the 2nd operand */
      if (mpfr_compare (res2, ref2) || (inexa >> 2) != (inexd >> 2))
        {
          printf ("Error with RNDA for %s (2nd operand)\n", foo);
          DISP2("a=",ref3);
          DISP("expected ", ref2); printf ("\n");
          DISP("got      ", res2); printf ("\n");
          printf ("inexa=%d inexd=%d\n", inexa >> 2, inexd >> 2);
          exit (1);
        }

    }

  mpfr_clear (ref1);
  mpfr_clear (ref2);
  mpfr_clear (ref3);
  mpfr_clear (res1);
  mpfr_clear (res2);
}

int
main (void)
{
  int N = 20;

  tests_start_mpfr ();

  while (N--)
    {
      /* no need to test mpfr_round, mpfr_ceil, mpfr_floor, mpfr_trunc since
         they take no rounding mode */

      test2ui (mpfr_add_ui, "mpfr_add_ui");
      test2ui (mpfr_div_2exp, "mpfr_div_2exp");
      test2ui (mpfr_div_2ui, "mpfr_div_2ui");
      test2ui (mpfr_div_ui, "mpfr_div_ui");
      test2ui (mpfr_mul_2exp, "mpfr_mul_2exp");
      test2ui (mpfr_mul_2ui, "mpfr_mul_2ui");
      test2ui (mpfr_mul_ui, "mpfr_mul_ui");
      test2ui (mpfr_pow_ui, "mpfr_pow_ui");
      test2ui (mpfr_sub_ui, "mpfr_sub_ui");

      testui2 (mpfr_ui_div, "mpfr_ui_div");
      testui2 (mpfr_ui_sub, "mpfr_ui_sub");
      testui2 (mpfr_ui_pow, "mpfr_ui_pow");

      test2 (mpfr_sqr, "mpfr_sqr");
      test2 (mpfr_sqrt, "mpfr_sqrt");
      test2 (mpfr_abs, "mpfr_abs");
      test2 (mpfr_neg, "mpfr_neg");

      test2 (mpfr_log, "mpfr_log");
      test2 (mpfr_log2, "mpfr_log2");
      test2 (mpfr_log10, "mpfr_log10");
      test2 (mpfr_log1p, "mpfr_log1p");

      test2 (mpfr_exp, "mpfr_exp");
      test2 (mpfr_exp2, "mpfr_exp2");
      test2 (mpfr_exp10, "mpfr_exp10");
      test2 (mpfr_expm1, "mpfr_expm1");
      test2 (mpfr_eint, "mpfr_eint");

      test2 (mpfr_sinh, "mpfr_sinh");
      test2 (mpfr_cosh, "mpfr_cosh");
      test2 (mpfr_tanh, "mpfr_tanh");
      test2 (mpfr_asinh, "mpfr_asinh");
      test2 (mpfr_acosh, "mpfr_acosh");
      test2 (mpfr_atanh, "mpfr_atanh");
      test2 (mpfr_sech, "mpfr_sech");
      test2 (mpfr_csch, "mpfr_csch");
      test2 (mpfr_coth, "mpfr_coth");

      test2 (mpfr_asin, "mpfr_asin");
      test2 (mpfr_acos, "mpfr_acos");
      test2 (mpfr_atan, "mpfr_atan");
      test2 (mpfr_cos, "mpfr_cos");
      test2 (mpfr_sin, "mpfr_sin");
      test2 (mpfr_tan, "mpfr_tan");
      test2 (mpfr_sec, "mpfr_sec");
      test2 (mpfr_csc, "mpfr_csc");
      test2 (mpfr_cot, "mpfr_cot");

      test2 (mpfr_erf,  "mpfr_erf");
      test2 (mpfr_erfc, "mpfr_erfc");
      test2 (mpfr_j0,   "mpfr_j0");
      test2 (mpfr_j1,   "mpfr_j1");
      test2 (mpfr_y0,   "mpfr_y0");
      test2 (mpfr_y1,   "mpfr_y1");
      test2 (mpfr_zeta, "mpfr_zeta");
      test2 (mpfr_gamma, "mpfr_gamma");
      test2 (mpfr_lngamma, "mpfr_lngamma");

      test2 (mpfr_rint, "mpfr_rint");
      test2 (mpfr_rint_ceil, "mpfr_rint_ceil");
      test2 (mpfr_rint_floor, "mpfr_rint_floor");
      test2 (mpfr_rint_round, "mpfr_rint_round");
      test2 (mpfr_rint_trunc, "mpfr_rint_trunc");
      test2 (mpfr_frac, "mpfr_frac");

      test3 (mpfr_add, "mpfr_add");
      test3 (mpfr_sub, "mpfr_sub");
      test3 (mpfr_mul, "mpfr_mul");
      test3 (mpfr_div, "mpfr_div");

      test3 (mpfr_agm, "mpfr_agm");
      test3 (mpfr_min, "mpfr_min");
      test3 (mpfr_max, "mpfr_max");

      /* we don't test reldiff since it does not guarantee correct rounding,
         thus we can get different results with RNDA and RNDU or RNDD. */
      test3 (mpfr_dim, "mpfr_dim");

      test3 (mpfr_remainder, "mpfr_remainder");
      test3 (mpfr_pow, "mpfr_pow");
      test3 (mpfr_atan2, "mpfr_atan2");
      test3 (mpfr_hypot, "mpfr_hypot");

      test3a (mpfr_sin_cos, "mpfr_sin_cos");

      test4 (mpfr_fma, "mpfr_fma");
      test4 (mpfr_fms, "mpfr_fms");

      test2 (mpfr_li2, "mpfr_li2");
      test2 (mpfr_rec_sqrt, "mpfr_rec_sqrt");
      test3 (mpfr_fmod, "mpfr_fmod");
      test3a (mpfr_modf, "mpfr_modf");
      test3a (mpfr_sinh_cosh, "mpfr_sinh_cosh");

#if MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0)
      test2 (mpfr_ai, "mpfr_ai");
      test2 (mpfr_digamma, "mpfr_digamma");
#endif
    }

  tests_end_mpfr ();
  return 0;
}