source: git/MP/MPT/MPT_ApNumber.cc @ 4d9c27

spielwiese
Last change on this file since 4d9c27 was 4d9c27, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* added .cc files and new GP stuff git-svn-id: file:///usr/local/Singular/svn/trunk@2649 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/******************************************************************
2 *
3 * File:    MPT_ApNumber.c
4 * Purpose: Routines which deal with the handling of abritrary
5 *          precision numbers
6 * Author:  Olaf Bachmann (obachman@mathematik.uni-kl.de)
7 * Created: 12/96
8 *
9 * Change History (most recent first):
10 *     
11 ******************************************************************/
12#include "MPT.h"
13
14MPT_Status_t MPT_GetApInt(MP_Link_pt link, MPT_Arg_t *arg)
15{
16  *arg = NULL;
17  mp_return(IMP_GetApInt(link, (MP_ApInt_t *) arg));
18}
19
20
21MPT_Status_t MPT_PutApInt(MP_Link_pt link, MPT_Arg_t arg)
22{
23  mp_return(IMP_PutApInt(link, (MP_ApInt_t) arg));
24}
25
26MPT_Status_t MPT_GetApReal(MP_Link_pt link, MPT_Arg_t *apreal)
27{
28  *apreal = NULL;
29  mp_return(IMP_GetApReal(link, (MP_ApReal_t *) apreal));
30}
31
32MPT_Status_t MPT_PutApReal(MP_Link_pt link, MPT_Arg_t arg)
33{
34  mp_return(IMP_PutApReal(link, (MP_ApReal_t) arg));
35}
36
37
38/******************************************************************
39 *
40 * Copy and Deletetion
41 *     
42 ******************************************************************/
43#ifdef MP_HAVE_GMP_APINT
44static void MPT_InitCopyGmpApInt(MPT_Arg_t *dest, MPT_Arg_t src);
45static void MPT_DeleteGmpApInt(MPT_Arg_t apint);
46#endif
47#ifdef MP_HAVE_PARI
48static void MPT_InitCopyPariApInt(MPT_Arg_t *dest, MPT_Arg_t src);
49static void MPT_DeletePariApInt(MPT_Arg_t apint);
50#endif
51static void MPT_InitCopyDummyApInt(MPT_Arg_t *dest, MPT_Arg_t src);
52static void MPT_DeleteDummyApInt(MPT_Arg_t apint);
53
54#if MP_DEFAULT_APINT_FORMAT == MP_PARI
55
56#define MPT_DefaultApIntFormat MP_PARI
57#define MPT_InitCopyDefaultApInt MPT_InitCopyPariApInt
58#define MPT_DeleteDefaultApInt   MPT_DeletePariApInt
59void (*MPT_InitCopyApInt)(MPT_Arg_t *dest, MPT_Arg_t src)
60  =  MPT_InitCopyPariApInt;
61void (*MPT_DeleteApInt)(MPT_Arg_t *apint) 
62  = MPT_DeletePariApInt;
63static int MPT_ApIntFormat = MP_PARI;
64
65#elif MP_DEFAULT_APINT_FORMAT == MP_GMP
66
67#define MPT_InitCopyDefaultApInt MPT_InitCopyGmpApInt
68#define MPT_DeleteDefaultApInt  MPT_DeleteGmpApInt
69#define MPT_DefaultApIntFormat MP_GMP
70void (*MPT_InitCopyApInt)(MPT_Arg_t *dest, MPT_Arg_t src)
71  =  MPT_InitCopyGmpApInt;
72void (*MPT_DeleteApInt)(MPT_Arg_t apint) 
73  = MPT_DeleteGmpApInt;
74static int MPT_ApIntFormat = MP_GMP;
75
76#else
77
78#define MPT_InitCopyDefaultApInt MPT_InitCopyDummyApInt
79#define MPT_DeleteDefaultApInt  MPT_DeleteDummyApInt
80#define MPT_DefaultApIntFormat MP_DUMMY
81void (*MPT_InitCopyApInt)(MPT_Arg_t *dest, MPT_Arg_t src)
82  =  MPT_InitCopyDummyApInt;
83void (*MPT_DeleteApInt)(MPT_Arg_t apint) 
84  = MPT_DeleteDummyApInt;
85static int MPT_ApIntFormat = MP_DUMMY;
86
87#endif
88
89MPT_Status_t MPT_Init(MP_Env_pt env)
90{
91  if (env != NULL)
92  {
93    if (env->bignum.native_bigreal_format == MP_GMP)
94    {
95#ifdef MP_HAVE_GMP_APINT
96      MPT_InitCopyApInt = MPT_InitCopyGmpApInt;
97      MPT_DeleteApInt  = MPT_DeleteGmpApInt;
98      MPT_ApIntFormat = MP_GMP;
99      return MPT_ClearError();
100#else
101      return MPT_SetError(MPT_WrongApIntFormat);
102#endif
103    }
104    else if (env->bignum.native_bigreal_format == MP_PARI)
105    {
106#ifdef MP_HAVE_PARI
107      MPT_InitCopyApInt = MPT_InitCopyPariApInt;
108      MPT_DeleteApInt  = MPT_DeletePariApInt;
109      MPT_ApIntFormat = MP_PARI;
110      return MPT_ClearError();
111#else
112       return MPT_SetError(MPT_WrongApIntFormat); 
113#endif
114    }
115    else
116    {
117      if (env->bignum.native_bigreal_format == MP_DUMMY)
118      {
119        MPT_InitCopyApInt = MPT_InitCopyDummyApInt;
120        MPT_DeleteApInt  = MPT_DeleteDummyApInt;
121        MPT_ApIntFormat = MP_DUMMY;
122        return MPT_ClearError();
123      }
124      else
125        return MPT_SetError(MPT_WrongApIntFormat);
126    }
127  }
128 
129  MPT_InitCopyApInt = MPT_InitCopyDefaultApInt;
130  MPT_DeleteApInt  = MPT_DeleteDefaultApInt;
131  MPT_ApIntFormat = MPT_DefaultApIntFormat;
132  return MPT_ClearError();
133}
134
135int MPT_GetApIntFormat()
136{
137  return MPT_ApIntFormat;
138}
139
140#ifdef MP_HAVE_GMP_APINT     
141#include "gmp.h"
142
143static void MPT_DeleteGmpApInt(MPT_Arg_t arg)
144{
145  if (arg != NULL)
146  {
147    mpz_clear((mpz_ptr) arg);
148    IMP_MemFreeFnc(arg, sizeof(*((mpz_ptr) arg)));
149  }
150}
151
152static void MPT_InitCopyGmpApInt(MPT_Arg_t *odest, MPT_Arg_t src)
153{
154  if (src != NULL)
155  {
156    mpz_ptr dest = (mpz_ptr) IMP_MemAllocFnc(sizeof(*((mpz_ptr) src)));
157    mpz_init_set(dest, (mpz_ptr) src);
158    *odest = (MPT_Arg_t) dest;
159  }
160  else
161    *odest = NULL;
162}
163#endif
164
165#ifdef MP_HAVE_PARI
166void MPT_DeletePariApInt(MPT_Arg_t arg)
167{
168  if (arg != NULL)
169    IMP_FreeCgeti((GEN) arg);
170}
171
172void MPT_InitCopyPariApInt(MPT_Arg_t *odest, MPT_Arg_t src)
173{
174  int length;
175  GEN number;
176 
177  if (src != NULL)
178  {
179    length = lgef((GEN) src);
180    number = IMP_AllocCgeti(length);
181    memcpy(number, (void*) src, length*sizeof(long));
182    *odest = (MPT_Arg_t) number;
183  }
184  else
185    *odest = NULL;
186}
187#endif
188
189/* Dummy Ints */
190void MPT_DeleteDummyApInt(MPT_Arg_t arg) {}
191void MPT_InitCopyDummyApInt(MPT_Arg_t *odest, MPT_Arg_t src)
192{
193  *odest = NULL;
194}
195
196
197#ifdef MP_HAVE_GMP_APREAL
198void MPT_InitCopyApReal(MPT_Arg_t *odest, MPT_Arg_t src)
199{
200  if (src != NULL)
201  {
202    mpf_ptr dest =  (mpf_ptr) IMP_MemAllocFnc(sizeof(*((mpf_ptr) src)));
203    mpf_init_set(dest, (mpf_ptr) src);
204    *odest = (MPT_Arg_t) dest;
205  }
206  else
207    *odest = NULL;
208}
209
210void MPT_DeleteApReal(MPT_Arg_t arg)
211{
212  if (arg != NULL)
213  {
214    mpf_clear((mpf_ptr) arg);
215    IMP_MemFreeFnc(arg, sizeof((mpf_ptr) arg));
216  }
217}
218#else
219/* Dummy Reals */
220void MPT_DeleteApReal(MPT_Arg_t arg) {}
221void MPT_InitCopyApReal(MPT_Arg_t *odest, MPT_Arg_t src)
222{
223  *odest = NULL;
224}
225
226#endif /* HAVE_GMP_APREAL */
227
228
Note: See TracBrowser for help on using the repository browser.