1 1.1 mrg /* read_line.c -- Read line of test data in file. 2 1.1 mrg 3 1.1 mrg Copyright (C) 2012, 2013, 2014 INRIA 4 1.1 mrg 5 1.1 mrg This file is part of GNU MPC. 6 1.1 mrg 7 1.1 mrg GNU MPC is free software; you can redistribute it and/or modify it under 8 1.1 mrg the terms of the GNU Lesser General Public License as published by the 9 1.1 mrg Free Software Foundation; either version 3 of the License, or (at your 10 1.1 mrg option) any later version. 11 1.1 mrg 12 1.1 mrg GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY 13 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 1.1 mrg FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 15 1.1 mrg more details. 16 1.1 mrg 17 1.1 mrg You should have received a copy of the GNU Lesser General Public License 18 1.1 mrg along with this program. If not, see http://www.gnu.org/licenses/ . 19 1.1 mrg */ 20 1.1 mrg 21 1.1 mrg #include "mpc-tests.h" 22 1.1 mrg 23 1.1 mrg static void 24 1.1 mrg read_param (mpc_datafile_context_t* datafile_context, 25 1.1 mrg mpc_operand_t* p, mpc_param_t t) 26 1.1 mrg { 27 1.1 mrg switch (t) 28 1.1 mrg { 29 1.1 mrg case NATIVE_INT: 30 1.1 mrg tpl_read_int (datafile_context, &(p->i),""); 31 1.1 mrg return; 32 1.1 mrg case NATIVE_UL: 33 1.1 mrg tpl_read_ui (datafile_context, &(p->ui)); 34 1.1 mrg return; 35 1.1 mrg case NATIVE_L: 36 1.1 mrg tpl_read_si (datafile_context, &(p->si)); 37 1.1 mrg return; 38 1.1 mrg 39 1.1 mrg case NATIVE_D: 40 1.1 mrg case NATIVE_LD: 41 1.1 mrg /* TODO */ 42 1.1 mrg fprintf (stderr, "read_param: type not implemented.\n"); 43 1.1 mrg exit (1); 44 1.1 mrg break; 45 1.1 mrg 46 1.1 mrg case NATIVE_DC: 47 1.1 mrg case NATIVE_LDC: 48 1.1 mrg #ifdef _Complex_I 49 1.1 mrg /* TODO */ 50 1.1 mrg fprintf (stderr, "read_param: type not implemented.\n"); 51 1.1 mrg exit (1); 52 1.1 mrg #endif 53 1.1 mrg break; 54 1.1 mrg 55 1.1 mrg case NATIVE_IM: 56 1.1 mrg case NATIVE_UIM: 57 1.1 mrg #ifdef _MPC_H_HAVE_INTMAX_T 58 1.1 mrg /* TODO */ 59 1.1 mrg fprintf (stderr, "read_param: type not implemented.\n"); 60 1.1 mrg exit (1); 61 1.1 mrg #endif 62 1.1 mrg break; 63 1.1 mrg 64 1.1 mrg case NATIVE_STRING: 65 1.1 mrg /* TODO */ 66 1.1 mrg fprintf (stderr, "read_param: type not implemented.\n"); 67 1.1 mrg exit (1); 68 1.1 mrg break; 69 1.1 mrg 70 1.1 mrg case GMP_Z: 71 1.1 mrg tpl_read_mpz (datafile_context, p->mpz); 72 1.1 mrg return; 73 1.1 mrg 74 1.1 mrg case GMP_Q: 75 1.1 mrg case GMP_F: 76 1.1 mrg /* TODO */ 77 1.1 mrg fprintf (stderr, "read_param: type not implemented.\n"); 78 1.1 mrg exit (1); 79 1.1 mrg break; 80 1.1 mrg 81 1.1 mrg case MPFR_INEX: 82 1.1 mrg tpl_read_mpfr_inex (datafile_context, &p->mpfr_inex); 83 1.1 mrg return; 84 1.1 mrg case MPFR: 85 1.1 mrg tpl_read_mpfr (datafile_context, 86 1.1 mrg p->mpfr_data.mpfr, &p->mpfr_data.known_sign); 87 1.1 mrg return; 88 1.1 mrg case MPFR_RND: 89 1.1 mrg tpl_read_mpfr_rnd (datafile_context, &p->mpfr_rnd); 90 1.1 mrg return; 91 1.1 mrg 92 1.1 mrg case MPC_INEX: 93 1.1 mrg tpl_read_mpc_inex (datafile_context, &p->mpc_inex_data); 94 1.1 mrg return; 95 1.1 mrg case MPC: 96 1.1 mrg tpl_read_mpc (datafile_context, &p->mpc_data); 97 1.1 mrg return; 98 1.1 mrg case MPC_RND: 99 1.1 mrg tpl_read_mpc_rnd (datafile_context, &p->mpc_rnd); 100 1.1 mrg return; 101 1.1 mrg 102 1.1 mrg case MPCC_INEX: 103 1.1 mrg /* TODO */ 104 1.1 mrg fprintf (stderr, "read_param: type not implemented.\n"); 105 1.1 mrg exit (1); 106 1.1 mrg break; 107 1.1 mrg } 108 1.1 mrg 109 1.1 mrg fprintf (stderr, "read_param: unsupported type.\n"); 110 1.1 mrg exit (1); 111 1.1 mrg } 112 1.1 mrg 113 1.1 mrg static void 114 1.1 mrg set_precision (mpc_fun_param_t* params, int index) 115 1.1 mrg { 116 1.1 mrg /* set output precision to reference precision */ 117 1.1 mrg int index_ref = index + params->nbout + params->nbin; 118 1.1 mrg 119 1.1 mrg switch (params->T[index]) 120 1.1 mrg { 121 1.1 mrg case MPFR: 122 1.1 mrg mpfr_set_prec (params->P[index].mpfr, 123 1.1 mrg mpfr_get_prec (params->P[index_ref].mpfr)); 124 1.1 mrg return; 125 1.1 mrg 126 1.1 mrg case MPC: 127 1.1 mrg mpfr_set_prec (mpc_realref (params->P[index].mpc), 128 1.1 mrg MPC_PREC_RE (params->P[index_ref].mpc)); 129 1.1 mrg mpfr_set_prec (mpc_imagref (params->P[index].mpc), 130 1.1 mrg MPC_PREC_IM (params->P[index_ref].mpc)); 131 1.1 mrg return; 132 1.1 mrg 133 1.1 mrg case NATIVE_INT: 134 1.1 mrg case NATIVE_UL: case NATIVE_L: 135 1.1 mrg case NATIVE_D: case NATIVE_LD: 136 1.1 mrg case NATIVE_DC: case NATIVE_LDC: 137 1.1 mrg case NATIVE_IM: case NATIVE_UIM: 138 1.1 mrg case NATIVE_STRING: 139 1.1 mrg case GMP_Z: case GMP_Q: 140 1.1 mrg case GMP_F: 141 1.1 mrg case MPFR_INEX: case MPFR_RND: 142 1.1 mrg case MPC_INEX: case MPC_RND: 143 1.1 mrg case MPCC_INEX: 144 1.1 mrg /* unsupported types */ 145 1.1 mrg break; 146 1.1 mrg } 147 1.1 mrg 148 1.1 mrg fprintf (stderr, "set_precision: unsupported type.\n"); 149 1.1 mrg exit (1); 150 1.1 mrg } 151 1.1 mrg 152 1.1 mrg void 153 1.1 mrg read_line (mpc_datafile_context_t* datafile_context, 154 1.1 mrg mpc_fun_param_t* params) 155 1.1 mrg { 156 1.1 mrg int in, out; 157 1.1 mrg int total = params->nbout + params->nbin; 158 1.1 mrg 159 1.1 mrg datafile_context->test_line_number = datafile_context->line_number; 160 1.1 mrg 161 1.1 mrg for (out = 0; out < params->nbout; out++) 162 1.1 mrg 163 1.1 mrg { 164 1.1 mrg read_param (datafile_context, &(params->P[total + out]), 165 1.1 mrg params->T[total + out]); 166 1.1 mrg if (params->T[out] == MPFR || params->T[out] == MPC) 167 1.1 mrg set_precision (params, out); 168 1.1 mrg } 169 1.1 mrg 170 1.1 mrg for (in = params->nbout; in < total; in++) 171 1.1 mrg { 172 1.1 mrg read_param (datafile_context, &(params->P[in]), params->T[in]); 173 1.1 mrg } 174 1.1 mrg } 175 1.1 mrg 176 1.1 mrg /* read primitives */ 177 1.1 mrg static void 178 1.1 mrg tpl_skip_line (mpc_datafile_context_t* datafile_context) 179 1.1 mrg /* skips characters until reaching '\n' or EOF; */ 180 1.1 mrg /* '\n' is skipped as well */ 181 1.1 mrg { 182 1.1 mrg while (datafile_context->nextchar != EOF && datafile_context->nextchar != '\n') 183 1.1 mrg datafile_context->nextchar = getc (datafile_context->fd); 184 1.1 mrg if (datafile_context->nextchar != EOF) 185 1.1 mrg { 186 1.1 mrg datafile_context->line_number ++; 187 1.1 mrg datafile_context->nextchar = getc (datafile_context->fd); 188 1.1 mrg } 189 1.1 mrg } 190 1.1 mrg 191 1.1 mrg static void 192 1.1 mrg tpl_skip_whitespace (mpc_datafile_context_t* datafile_context) 193 1.1 mrg /* skips over whitespace if any until reaching EOF */ 194 1.1 mrg /* or non-whitespace */ 195 1.1 mrg { 196 1.1 mrg while (isspace (datafile_context->nextchar)) 197 1.1 mrg { 198 1.1 mrg if (datafile_context->nextchar == '\n') 199 1.1 mrg datafile_context->line_number ++; 200 1.1 mrg datafile_context->nextchar = getc (datafile_context->fd); 201 1.1 mrg } 202 1.1 mrg } 203 1.1 mrg 204 1.1 mrg void 205 1.1 mrg tpl_skip_whitespace_comments (mpc_datafile_context_t* datafile_context) 206 1.1 mrg /* skips over all whitespace and comments, if any */ 207 1.1 mrg { 208 1.1 mrg tpl_skip_whitespace (datafile_context); 209 1.1 mrg while (datafile_context->nextchar == '#') { 210 1.1 mrg tpl_skip_line (datafile_context); 211 1.1 mrg if (datafile_context->nextchar != EOF) 212 1.1 mrg tpl_skip_whitespace (datafile_context); 213 1.1 mrg } 214 1.1 mrg } 215 1.1 mrg 216 1.1 mrg /* All following read routines skip over whitespace and comments; */ 217 1.1 mrg /* so after calling them, nextchar is either EOF or the beginning */ 218 1.1 mrg /* of a non-comment token. */ 219 1.1 mrg void 220 1.1 mrg tpl_read_ternary (mpc_datafile_context_t* datafile_context, int* ternary) 221 1.1 mrg { 222 1.1 mrg switch (datafile_context->nextchar) 223 1.1 mrg { 224 1.1 mrg case '!': 225 1.1 mrg *ternary = TERNARY_ERROR; 226 1.1 mrg break; 227 1.1 mrg case '?': 228 1.1 mrg *ternary = TERNARY_NOT_CHECKED; 229 1.1 mrg break; 230 1.1 mrg case '+': 231 1.1 mrg *ternary = +1; 232 1.1 mrg break; 233 1.1 mrg case '0': 234 1.1 mrg *ternary = 0; 235 1.1 mrg break; 236 1.1 mrg case '-': 237 1.1 mrg *ternary = -1; 238 1.1 mrg break; 239 1.1 mrg default: 240 1.1 mrg printf ("Error: Unexpected ternary value '%c' in file '%s' line %lu\n", 241 1.1 mrg datafile_context->nextchar, 242 1.1 mrg datafile_context->pathname, 243 1.1 mrg datafile_context->line_number); 244 1.1 mrg exit (1); 245 1.1 mrg } 246 1.1 mrg 247 1.1 mrg datafile_context->nextchar = getc (datafile_context->fd); 248 1.1 mrg tpl_skip_whitespace_comments (datafile_context); 249 1.1 mrg } 250