summaryrefslogtreecommitdiff
path: root/tests/rounding.c
blob: 86c43218791c36797a128d0abb74db4f8f5ec4c4 (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
/* rounding.c -- file for functions iterating over rounding modes.

Copyright (C) 2013, 2014 INRIA

This file is part of GNU MPC.

GNU MPC 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.

GNU MPC 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 this program. If not, see http://www.gnu.org/licenses/ .
*/

#include "mpc-tests.h"

/* helper functions for iterating over mpfr rounding modes */

#define FIRST_MPFR_RND_MODE MPFR_RNDN

static mpfr_rnd_t
next_mpfr_rnd_mode (mpfr_rnd_t curr)
{
  switch (curr)
    {
    case MPFR_RNDN:
      return MPFR_RNDZ;
    case MPFR_RNDZ:
      return MPFR_RNDU;
    case MPFR_RNDU:
      return MPFR_RNDD;
    default:
      /* return invalid guard value in mpfr_rnd_t */
#if MPFR_VERSION_MAJOR < 3
      return MPFR_RNDNA;
#else
      return MPFR_RNDA; /* valid rounding type, but not used in mpc */
#endif
    }
}

static int
is_valid_mpfr_rnd_mode (mpfr_rnd_t curr)
/* returns 1 if curr is a valid rounding mode, and 0 otherwise */
{
   if (   curr == MPFR_RNDN || curr == MPFR_RNDZ
       || curr == MPFR_RNDU || curr == MPFR_RNDD)
      return 1;
   else
      return 0;
}

static mpc_rnd_t
next_mpc_rnd_mode (mpc_rnd_t rnd)
{
  mpfr_rnd_t rnd_re = MPC_RND_RE (rnd);
  mpfr_rnd_t rnd_im = MPC_RND_IM (rnd);

  rnd_im = next_mpfr_rnd_mode (rnd_im);
  if (!is_valid_mpfr_rnd_mode (rnd_im))
    {
      rnd_re = next_mpfr_rnd_mode (rnd_re);
      rnd_im = FIRST_MPFR_RND_MODE;
    }

  return MPC_RND(rnd_re, rnd_im);
}

static int
is_valid_mpc_rnd_mode (mpc_rnd_t rnd)
/* returns 1 if curr is a valid rounding mode, and 0 otherwise */
{
  mpfr_rnd_t rnd_re = MPC_RND_RE (rnd);
  mpfr_rnd_t rnd_im = MPC_RND_IM (rnd);

  return is_valid_mpfr_rnd_mode (rnd_re) && is_valid_mpfr_rnd_mode (rnd_im);
}

/* functions using abstract parameters */

static void
first_mode (mpc_fun_param_t *params, int index)
{
  switch (params->T[index])
    {
    case MPC_RND:
      params->P[index].mpc_rnd =
        MPC_RND(FIRST_MPFR_RND_MODE, FIRST_MPFR_RND_MODE);
      break;
    case MPFR_RND:
      params->P[index].mpfr_rnd = FIRST_MPFR_RND_MODE;
      break;
    default:
      printf ("The rounding mode is expected to be"
              " the last input parameter.\n");
      exit (-1);
    }
}

static void
next_mode (mpc_fun_param_t *params, int index)
{
  switch (params->T[index])
    {
    case MPC_RND:
      params->P[index].mpc_rnd =
        next_mpc_rnd_mode (params->P[index].mpc_rnd);
      break;
    case MPFR_RND:
      params->P[index].mpfr_rnd =
        next_mpfr_rnd_mode (params->P[index].mpfr_rnd);
      break;
    default:
      printf ("The rounding mode is expected to be"
              " the last input parameter.\n");
      exit (-1);
    }
}

static int
is_valid_mode (mpc_fun_param_t *params, int index)
/* returns 1 if params->P[index] is a valid rounding mode, and 0 otherwise */
{
  switch (params->T[index])
    {
    case MPC_RND:
      return is_valid_mpc_rnd_mode (params->P[index].mpc_rnd);
    case MPFR_RND:
      return is_valid_mpfr_rnd_mode (params->P[index].mpfr_rnd);
    default:
      printf ("The rounding mode is expected to be"
              " the last input parameter.\n");
      exit (-1);
    }
}

void
first_rnd_mode (mpc_fun_param_t *params)
{
  int rnd_mode_index;

  for (rnd_mode_index = params->nbout + params->nbin - params->nbrnd;
       rnd_mode_index < params->nbout + params->nbin;
       rnd_mode_index++)
    {
      first_mode (params, rnd_mode_index);
    }
}


void
next_rnd_mode (mpc_fun_param_t *params)
/* cycle through all valid rounding modes and finish with an invalid one */
{
  int last = params->nbout + params->nbin - 1;
  int index = params->nbout + params->nbin - params->nbrnd;
  int carry = 1;

  while (carry && index <= last) {
    next_mode (params, index);
    if (!is_valid_mode (params, index) && index < last)
      first_mode (params, index);
    else
      carry = 0;
    index++;
  }
}

int
is_valid_rnd_mode (mpc_fun_param_t *params)
/* returns 1 if all rounding parameters are set to a valid rounding mode,
   and 0 otherwise */
{
  int index;

  for (index = params->nbout + params->nbin - params->nbrnd;
       index < params->nbout + params->nbin;
       index++)
    if (! is_valid_mode (params, index))
      return 0;

  return 1;
}