1 /* copy_parameter.c -- Copy of an input parameter into a parameter reused for
2    output.
3 
4 Copyright (C) 2012, 2013, 2014 INRIA
5 
6 This file is part of GNU MPC.
7 
8 GNU MPC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with this program. If not, see http://www.gnu.org/licenses/ .
20 */
21 
22 #include "mpc-tests.h"
23 
24 int
copy_parameter(mpc_fun_param_t * params,int index_dest,int index_src)25 copy_parameter (mpc_fun_param_t *params, int index_dest, int index_src)
26 {
27   mpfr_prec_t pre1, pim1;
28   mpfr_prec_t pre2, pim2;
29   int index_ref;
30 
31   if (params->T[index_src] != params->T[index_dest])
32     {
33       fprintf (stderr, "copy_parameter: types of parameters don't match.\n");
34       exit (1);
35     }
36 
37   switch (params->T[index_src])
38     {
39     case NATIVE_INT:
40       tpl_copy_int (&params->P[index_dest].i, &params->P[index_src].i);
41       return 0;
42     case NATIVE_UL:
43       tpl_copy_ui (&params->P[index_dest].ui, &params->P[index_src].ui);
44       return 0;
45     case NATIVE_L:
46       tpl_copy_si (&params->P[index_dest].si, &params->P[index_src].si);
47       return 0;
48     case NATIVE_D:
49       tpl_copy_d (&params->P[index_dest].d, &params->P[index_src].d);
50       return 0;
51 
52     case NATIVE_LD:
53       /* TODO */
54       fprintf (stderr, "copy_parameter: type not implemented.\n");
55       exit (1);
56       break;
57 
58     case NATIVE_DC:
59     case NATIVE_LDC:
60 #ifdef _Complex_I
61       /* TODO */
62       fprintf (stderr, "copy_parameter: type not implemented.\n");
63       exit (1);
64 #endif
65       break;
66 
67     case NATIVE_IM:
68     case NATIVE_UIM:
69 #ifdef _MPC_H_HAVE_INTMAX_T
70       /* TODO */
71       fprintf (stderr, "copy_parameter: type not implemented.\n");
72       exit (1);
73 #endif
74       break;
75 
76     case GMP_Z:
77       mpz_set (params->P[index_dest].mpz, params->P[index_src].mpz);
78       return 0;
79     case GMP_Q:
80       mpq_set (params->P[index_dest].mpq, params->P[index_src].mpq);
81       return 0;
82     case GMP_F:
83       mpf_set (params->P[index_dest].mpf, params->P[index_src].mpf);
84       return 0;
85 
86     case MPFR:
87       /* need same precision between source, destination, and reference */
88       pre1 = mpfr_get_prec (params->P[index_dest].mpfr);
89       pre2 = mpfr_get_prec (params->P[index_src].mpfr);
90       index_ref = index_dest + params->nbout + params->nbin;
91       if (pre1 != pre2
92           || pre1 != mpfr_get_prec (params->P[index_ref].mpfr))
93         return -1;
94 
95       tpl_copy_mpfr (params->P[index_dest].mpfr, params->P[index_src].mpfr);
96       return 0;
97 
98     case MPC:
99       mpc_get_prec2 (&pre1, &pim1, params->P[index_dest].mpc);
100       /* check same precision between source and destination */
101       mpc_get_prec2 (&pre2, &pim2, params->P[index_src].mpc);
102       if (pre1 != pre2 || pim1 != pim2)
103         return -1;
104       /* check same precision between source and reference */
105       index_ref = index_dest + params->nbout + params->nbin;
106       mpc_get_prec2 (&pre2, &pim2, params->P[index_ref].mpc);
107       if (pre1 != pre2 || pim1 != pim2)
108         return -1;
109 
110       tpl_copy_mpc (params->P[index_dest].mpc, params->P[index_src].mpc);
111       return 0;
112 
113     case NATIVE_STRING:
114     case MPFR_INEX:     case MPFR_RND:
115     case MPC_INEX:      case MPC_RND:
116     case MPCC_INEX:
117       /* no supported copy */
118       break;
119     }
120 
121   fprintf (stderr, "copy_parameter: unsupported type.\n");
122   exit (1);
123 }
124