Imported Upstream version 1.0
[platform/upstream/mpc.git] / tests / tfma.c
1 /* tfma -- test file for mpc_fma.
2
3 Copyright (C) 2011, 2012 INRIA
4
5 This file is part of GNU MPC.
6
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
19 */
20
21 #include "mpc-tests.h"
22
23 static void
24 cmpfma (mpc_srcptr a, mpc_srcptr b, mpc_srcptr c, mpc_rnd_t rnd)
25    /* computes a*b+c with the naive and fast functions using the rounding
26       mode rnd and compares the results and return values.                                          
27       In our current test suite, all input precisions are the same, and we
28       use this precision also for the result.
29    */
30 {
31    mpc_t z, t;
32    int   inex_z, inex_t;
33
34    mpc_init2 (z, MPC_MAX_PREC (a));
35    mpc_init2 (t, MPC_MAX_PREC (a));
36
37    inex_z = mpc_fma_naive (z, a, b, c, rnd);
38    inex_t = mpc_fma (t, a, b, c, rnd);
39
40    if (mpc_cmp (z, t) != 0 || inex_z != inex_t) {
41       fprintf (stderr, "fma_naive and fma differ for rnd=(%s,%s)\n",
42                mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
43                mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
44       MPC_OUT (a);
45       MPC_OUT (b);
46       MPC_OUT (c);
47       MPC_OUT (z);
48       MPC_OUT (t);
49       if (inex_z != inex_t) {
50          fprintf (stderr, "inex_re (z): %s\n", MPC_INEX_STR (inex_z));
51          fprintf (stderr, "inex_re (t): %s\n", MPC_INEX_STR (inex_t));
52       }
53       exit (1);
54    }
55
56   mpc_clear (z);
57   mpc_clear (t);
58 }
59
60
61 static void
62 check_random (void)
63 {
64    mpfr_prec_t prec;
65    int rnd_re, rnd_im;
66    mpc_t a, b, c;
67
68    mpc_init2 (a, 1000);
69    mpc_init2 (b, 1000);
70    mpc_init2 (c, 1000);
71
72    for (prec = 2; prec < 1000; prec = (mpfr_prec_t) (prec * 1.1 + 1)) {
73       mpc_set_prec (a, prec);
74       mpc_set_prec (b, prec);
75       mpc_set_prec (c, prec);
76
77       test_default_random (a, -1024, 1024, 128, 0);
78       test_default_random (b, -1024, 1024, 128, 0);
79       test_default_random (c, -1024, 1024, 128, 0);
80
81       for (rnd_re = 0; rnd_re < 4; rnd_re ++)
82          for (rnd_im = 0; rnd_im < 4; rnd_im ++)
83             cmpfma (a, b, c, MPC_RND (rnd_re, rnd_im));
84    }
85
86    mpc_clear (a);
87    mpc_clear (b);
88    mpc_clear (c);
89 }
90
91
92 int
93 main (void)
94 {
95   DECL_FUNC (CCCC, f, mpc_fma);
96
97   test_start ();
98
99   check_random ();
100
101   data_check (f, "fma.dat");
102   tgeneric (f, 2, 1024, 1, 256);
103
104   test_end ();
105
106   return 0;
107 }