summaryrefslogtreecommitdiff
path: root/tests/read_line.c
blob: cde5db83ed4f67be43b88691cb0f1b227ba01046 (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
/* read_line.c -- Read line of test data in file.

Copyright (C) 2012, 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"

static void
read_param  (mpc_datafile_context_t* datafile_context,
             mpc_operand_t* p, mpc_param_t t)
{
  switch (t)
    {
    case NATIVE_INT:
      tpl_read_int (datafile_context, &(p->i),"");
      return;
    case NATIVE_UL:
      tpl_read_ui (datafile_context, &(p->ui));
      return;
    case NATIVE_L:
      tpl_read_si (datafile_context, &(p->si));
      return;

    case NATIVE_D:
    case NATIVE_LD:
      /* TODO */
      fprintf (stderr, "read_param: type not implemented.\n");
      exit (1);
      break;

    case NATIVE_DC:
    case NATIVE_LDC:
#ifdef _Complex_I
      /* TODO */
      fprintf (stderr, "read_param: type not implemented.\n");
      exit (1);
#endif
      break;

    case NATIVE_IM:
    case NATIVE_UIM:
#ifdef _MPC_H_HAVE_INTMAX_T
      /* TODO */
      fprintf (stderr, "read_param: type not implemented.\n");
      exit (1);
#endif
      break;

    case NATIVE_STRING:
      /* TODO */
      fprintf (stderr, "read_param: type not implemented.\n");
      exit (1);
      break;

    case GMP_Z:
      tpl_read_mpz (datafile_context, p->mpz);
      return;

    case GMP_Q:
    case GMP_F:
      /* TODO */
      fprintf (stderr, "read_param: type not implemented.\n");
      exit (1);
      break;

    case MPFR_INEX:
      tpl_read_mpfr_inex (datafile_context, &p->mpfr_inex);
      return;
    case MPFR:
      tpl_read_mpfr (datafile_context,
                     p->mpfr_data.mpfr, &p->mpfr_data.known_sign);
      return;
    case MPFR_RND:
      tpl_read_mpfr_rnd (datafile_context, &p->mpfr_rnd);
      return;

    case MPC_INEX:
      tpl_read_mpc_inex (datafile_context, &p->mpc_inex_data);
      return;
    case MPC:
      tpl_read_mpc (datafile_context, &p->mpc_data);
      return;
    case MPC_RND:
      tpl_read_mpc_rnd (datafile_context, &p->mpc_rnd);
      return;

    case MPCC_INEX:
      /* TODO */
      fprintf (stderr, "read_param: type not implemented.\n");
      exit (1);
      break;
    }

  fprintf (stderr, "read_param: unsupported type.\n");
  exit (1);
}

static void
set_precision (mpc_fun_param_t* params, int index)
{
  /* set output precision to reference precision */
  int index_ref = index + params->nbout + params->nbin;

  switch (params->T[index])
    {
    case MPFR:
      mpfr_set_prec (params->P[index].mpfr,
                     mpfr_get_prec (params->P[index_ref].mpfr));
      return;

    case MPC:
      mpfr_set_prec (mpc_realref (params->P[index].mpc),
                     MPC_PREC_RE (params->P[index_ref].mpc));
      mpfr_set_prec (mpc_imagref (params->P[index].mpc),
                     MPC_PREC_IM (params->P[index_ref].mpc));
      return;

    case NATIVE_INT:
    case NATIVE_UL:    case NATIVE_L:
    case NATIVE_D:     case NATIVE_LD:
    case NATIVE_DC:    case NATIVE_LDC:
    case NATIVE_IM:    case NATIVE_UIM:
    case NATIVE_STRING:
    case GMP_Z:        case GMP_Q:
    case GMP_F:
    case MPFR_INEX:    case MPFR_RND:
    case MPC_INEX:     case MPC_RND:
    case MPCC_INEX:
      /* unsupported types */
      break;
    }

  fprintf (stderr, "set_precision: unsupported type.\n");
  exit (1);
}

void
read_line (mpc_datafile_context_t* datafile_context,
           mpc_fun_param_t* params)
{
  int in, out;
  int total = params->nbout + params->nbin;

  datafile_context->test_line_number = datafile_context->line_number;

  for (out = 0; out < params->nbout; out++)

    {
      read_param (datafile_context, &(params->P[total + out]),
                  params->T[total + out]);
      if (params->T[out] == MPFR || params->T[out] == MPC)
        set_precision (params, out);
    }

  for (in = params->nbout; in < total; in++)
    {
      read_param (datafile_context, &(params->P[in]), params->T[in]);
    }
}

/* read primitives */
static void
tpl_skip_line (mpc_datafile_context_t* datafile_context)
   /* skips characters until reaching '\n' or EOF; */
   /* '\n' is skipped as well                      */
{
   while (datafile_context->nextchar != EOF && datafile_context->nextchar != '\n')
     datafile_context->nextchar = getc (datafile_context->fd);
   if (datafile_context->nextchar != EOF)
     {
       datafile_context->line_number ++;
       datafile_context->nextchar = getc (datafile_context->fd);
     }
}

static void
tpl_skip_whitespace (mpc_datafile_context_t* datafile_context)
   /* skips over whitespace if any until reaching EOF */
   /* or non-whitespace                               */
{
   while (isspace (datafile_context->nextchar))
     {
       if (datafile_context->nextchar == '\n')
         datafile_context->line_number ++;
       datafile_context->nextchar = getc (datafile_context->fd);
     }
}

void
tpl_skip_whitespace_comments (mpc_datafile_context_t* datafile_context)
   /* skips over all whitespace and comments, if any */
{
   tpl_skip_whitespace (datafile_context);
   while (datafile_context->nextchar == '#') {
      tpl_skip_line (datafile_context);
      if (datafile_context->nextchar != EOF)
         tpl_skip_whitespace (datafile_context);
   }
}

/* All following read routines skip over whitespace and comments; */
/* so after calling them, nextchar is either EOF or the beginning */
/* of a non-comment token.                                        */
void
tpl_read_ternary (mpc_datafile_context_t* datafile_context, int* ternary)
{
  switch (datafile_context->nextchar)
    {
    case '!':
      *ternary = TERNARY_ERROR;
      break;
    case '?':
      *ternary = TERNARY_NOT_CHECKED;
      break;
    case '+':
      *ternary = +1;
      break;
    case '0':
      *ternary = 0;
      break;
    case '-':
      *ternary = -1;
      break;
    default:
      printf ("Error: Unexpected ternary value '%c' in file '%s' line %lu\n",
              datafile_context->nextchar,
              datafile_context->pathname,
              datafile_context->line_number);
      exit (1);
    }

  datafile_context->nextchar = getc (datafile_context->fd);
  tpl_skip_whitespace_comments (datafile_context);
}