My Project  UNKNOWN_GIT_VERSION
bigintm.cc
Go to the documentation of this file.
1 
2 
3 
4 
5 #include "kernel/mod2.h"
6 
7 #include "omalloc/omalloc.h"
8 #include "coeffs/coeffs.h"
9 
10 #include "Singular/ipid.h"
11 #include "Singular/subexpr.h"
12 #include "Singular/tok.h"
13 #include "Singular/blackbox.h"
14 #include "Singular/ipshell.h"
15 
16 #include "Singular/ipid.h"
17 // extern coeffs coeffs_BIGINT
18 
19 
20 #include "bigintm.h"
21 
22 
23 #define HAVE_BIGINTM 1
24 
25 namespace
26 {
27 
28 #ifdef HAVE_BIGINTM
29 static int bigintm_type_id = -1;
30 #endif
31 
32 #ifdef HAVE_BIGINTM
33 static char * bigintm_String(blackbox */*b*/, void *d)
34 { if (d==NULL) return omStrDup("oo");
35  else
36  {
37  StringSetS("");
38  number n=(number)d; n_Write(n, coeffs_BIGINT); d=(void*)n;
39  return StringEndS();
40  }
41 }
42 static void * bigintm_Copy(blackbox*/*b*/, void *d)
43 { number n=(number)d; return n_Copy(n, coeffs_BIGINT); }
44 
45 static BOOLEAN bigintm_Assign(leftv l, leftv r)
46 {
47  assume( l->Typ() == bigintm_type_id );
48 
49  // blackbox *ll=getBlackboxStuff(l->Typ());
50 
51  if (r->Typ()>MAX_TOK)
52  {
53  if (bigintm_type_id == r->Typ())
54  {
55  // blackbox *rr=getBlackboxStuff(r->Typ());
56 
57  if (l->Data()!=NULL) { number n1=(number)l->Data(); n_Delete(&n1,coeffs_BIGINT); }
58  number n2=(number)r->CopyD();
59  if (l->rtyp==IDHDL)
60  {
61  IDDATA((idhdl)l->data)=(char *)n2;
62  }
63  else
64  {
65  l->data=(void *)n2;
66  }
67  return FALSE;
68  }
69  else
70  {
71  Werror("bigintm_Assign: assign %s (%d) = %s (%d)",
72  getBlackboxName(l->Typ()), l->Typ(),
73  getBlackboxName(r->Typ()), r->Typ());
74  return TRUE;
75  }
76  }
77  else if (r->Typ()==INT_CMD)
78  {
79  if (l->Data()!=NULL) { number n1=(number)l->Data(); n_Delete(&n1,coeffs_BIGINT); }
80  number n2=n_Init((int)(long)r->Data(),coeffs_BIGINT);
81  if (l->rtyp==IDHDL)
82  {
83  IDDATA((idhdl)l->data)=(char *)n2;
84  }
85  else
86  {
87  l->data=(void *)n2;
88  }
89  return FALSE;
90  }
91  else
92  Werror("assign %d = %d",l->Typ(),r->Typ());
93 
94  return TRUE;
95 }
96 
97 BOOLEAN bigintm_Op1(int op,leftv l, leftv r)
98 {
99  // interpreter: a1 is ist bigintm
100  assume( r->Typ() == bigintm_type_id );
101 /*
102  // "typeof( <blackbox> )" is handled by 'blackboxDefaultOp1'
103  if (op==TYPEOF_CMD)
104  {
105  l->data=omStrDup(getBlackboxName(r->Typ()));
106  l->rtyp=STRING_CMD;
107  return FALSE;
108  }
109 */
110 
111  return blackboxDefaultOp1(op, l, r);
112 }
113 
114 
115 static BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
116 
117 
118 static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2)
119 {
120  // interpreter: a1 is ist bigintm
121  assume( a1->Typ() == bigintm_type_id );
122 
123  // blackbox *a=getBlackboxStuff(a1->Typ());
124  number n1=(number)a1->Data();
125  switch(op)
126  {
127  case '+':
128  {
129  if (a2->Typ()==INT_CMD)
130  {
131  number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT);
132  number n=n_Add(n1,n2, coeffs_BIGINT);
133  res->data=(void *)n;
134  res->rtyp=a1->Typ();
135  return FALSE;
136  }
137  else if (a2->Typ()==a1->Typ())
138  {
139  number n2=(number)a2->Data();
140  number n=n_Add(n1,n2, coeffs_BIGINT);
141  res->data=(void *)n;
142  res->rtyp=a1->Typ();
143  return FALSE;
144  }
145 
146  Werror("bigintm_Op2: Op: '+': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ()));
147  return TRUE;
148  }
149 
150  case '-':
151  {
152  if (a2->Typ()==INT_CMD)
153  {
154  number n2=n_Init((int)(long)a2->Data(),coeffs_BIGINT);
155  number n=n_Sub(n1,n2, coeffs_BIGINT);
156  res->data=(void *)n;
157  res->rtyp=a1->Typ();
158  return FALSE;
159  }
160  else if (a2->Typ()==a1->Typ())
161  {
162  number n2=(number)a2->Data();
163  number n=n_Sub(n1,n2, coeffs_BIGINT);
164  res->data=(void *)n;
165  res->rtyp=a1->Typ();
166  return FALSE;
167  }
168 
169  Werror("bigintm_Op2: Op: '-': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ()));
170  return TRUE;
171  }
172 
173 
174  case '*':
175  {
176  if (a2->Typ()==INT_CMD)
177  {
178  number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT);
179  number n=n_Mult(n1,n2, coeffs_BIGINT);
180  res->data=(void *)n;
181  res->rtyp=a1->Typ();
182  return FALSE;
183  }
184  else if (a2->Typ()==a1->Typ())
185  {
186  number n2=(number)a2->Data();
187  number n=n_Mult(n1,n2, coeffs_BIGINT);
188  res->data=(void *)n;
189  res->rtyp=a1->Typ();
190  return FALSE;
191  }
192  Werror("bigintm_Op2: Op: '*': Sorry unsupported 2nd argument-type: '%s' in", Tok2Cmdname(a2->Typ()));
193  return TRUE;
194  }
195 
196  case EQUAL_EQUAL:
197  {
198  if( a1 == a2)
199  {
200  res->data= (void *) (TRUE);
201  res->rtyp= INT_CMD;
202  return FALSE;
203  } else
204  if (a2->Typ()==INT_CMD)
205  {
206  number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT);
207  res->data=(void *) (long) n_Equal(n1,n2, coeffs_BIGINT);
208  res->rtyp= INT_CMD;
209  n_Delete(&n2,coeffs_BIGINT);
210  return FALSE;
211  }
212  else if (a2->Typ()==a1->Typ())
213  {
214  number n2=(number)a2->Data();
215  res->data=(void *) (long) n_Equal(n1,n2, coeffs_BIGINT);
216  res->rtyp= INT_CMD;
217  return FALSE;
218  }
219 
220  Werror("bigintm_Op2: Op: '==': Sorry unsupported 2nd argument-type: '%s' in", Tok2Cmdname(a2->Typ()));
221  return TRUE;
222  }
223 
224  case '.':
225  {
226 
227  if (a2->name==NULL)
228  {
229  Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) should be a NAME", Tok2Cmdname(a2->Typ()), a2->Typ());
230  return TRUE;
231  }
232 
233  Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) is called '%s' in ", Tok2Cmdname(a2->Typ()), a2->Typ(), a2->name);
234  return TRUE;
235  }
236 
237  default:
238  return blackboxDefaultOp2(op,res,a1,a2);
239  }
240 }
241 // BOOLEAN opM(int op, leftv res, leftv args)
242 static BOOLEAN bigintm_OpM(int op, leftv res, leftv args)
243 {
244  // interpreter: args->1. arg is ist bigintm
245  assume( args->Typ() == bigintm_type_id );
246  blackbox *a=getBlackboxStuff(args->Typ());
247  switch(op)
248  {
249  case STRING_CMD:
250  {
251  res->data=(void *)a->blackbox_String(a,args->Data());
252  res->rtyp=STRING_CMD;
253  return FALSE;
254  }
255 
256  default:
257  return blackboxDefaultOpM(op, res, args);
258  }
259  return blackboxDefaultOpM(op, res, args);
260 }
261 
262 static void bigintm_destroy(blackbox */*b*/, void *d)
263 {
264  if (d!=NULL)
265  {
266  number n=(number)d;
267  n_Delete(&n, coeffs_BIGINT);
268  }
269 }
270 
271 #endif
272 
273 }
274 
275 // this is only a demo
277 {
278 #ifndef HAVE_BIGINTM
279  Werror("bigintm_setup: Sorry BIGINTM was not compiled in!");
280  return TRUE; // ok, TRUE = error!
281 #else
282 
283  if( bigintm_type_id == -1 )
284  {
285  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
286  // all undefined entries will be set to default in setBlackboxStuff
287  // the default Print is quite usefule,
288  // all other are simply error messages
289  b->blackbox_destroy=bigintm_destroy;
290  b->blackbox_String=bigintm_String;
291  //b->blackbox_Print=blackbox_default_Print;
292  //b->blackbox_Init=blackbox_default_Init;
293  b->blackbox_Copy=bigintm_Copy;
294  b->blackbox_Assign=bigintm_Assign; // TO ASK: no default?!
295  b->blackbox_Op1=bigintm_Op1;
296  b->blackbox_Op2=bigintm_Op2;
297  //b->blackbox_Op3=blackboxDefaultOp3;
298  b->blackbox_OpM=bigintm_OpM;
299 
300  bigintm_type_id = setBlackboxStuff(b,"bigintm");
301 
302  Print("bigintm_setup: created a blackbox type [%d] '%s'",bigintm_type_id, getBlackboxName(bigintm_type_id));
303  PrintLn();
304 
305  return FALSE; // ok, TRUE = error!
306  }
307  else
308  {
309  Werror("bigintm_setup: Sorry should NOT be run twice!");
310  return TRUE; // ok, TRUE = error!
311  }
312 
313 #endif
314 }
315 
316 
317 
FALSE
#define FALSE
Definition: auxiliary.h:94
blackboxDefaultOp2
BOOLEAN blackboxDefaultOp2(int, leftv, leftv, leftv)
default procedure blackboxDefaultOp2, to be called as "default:" branch
Definition: blackbox.cc:81
omalloc.h
sleftv::Data
void * Data()
Definition: subexpr.cc:1182
STRING_CMD
@ STRING_CMD
Definition: tok.h:183
MAX_TOK
@ MAX_TOK
Definition: tok.h:216
IDDATA
#define IDDATA(a)
Definition: ipid.h:121
omStrDup
#define omStrDup(s)
Definition: omAllocDecl.h:263
n_Delete
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition: coeffs.h:455
StringEndS
char * StringEndS()
Definition: reporter.cc:151
sleftv
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
bigintm_setup
BOOLEAN bigintm_setup()
Definition: bigintm.cc:276
b
CanonicalForm b
Definition: cfModGcd.cc:4044
n_Add
static FORCE_INLINE number n_Add(number a, number b, const coeffs r)
return the sum of 'a' and 'b', i.e., a+b
Definition: coeffs.h:656
sleftv::name
const char * name
Definition: subexpr.h:87
TRUE
#define TRUE
Definition: auxiliary.h:98
res
CanonicalForm res
Definition: facAbsFact.cc:64
INT_CMD
@ INT_CMD
Definition: tok.h:96
n_Write
static FORCE_INLINE void n_Write(number n, const coeffs r, const BOOLEAN bShortOut=TRUE)
Definition: coeffs.h:591
BOOLEAN
int BOOLEAN
Definition: auxiliary.h:85
mod2.h
EQUAL_EQUAL
@ EQUAL_EQUAL
Definition: grammar.cc:268
blackbox.h
n_Mult
static FORCE_INLINE number n_Mult(number a, number b, const coeffs r)
return the product of 'a' and 'b', i.e., a*b
Definition: coeffs.h:636
n_Init
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition: coeffs.h:538
subexpr.h
n_Sub
static FORCE_INLINE number n_Sub(number a, number b, const coeffs r)
return the difference of 'a' and 'b', i.e., a-b
Definition: coeffs.h:669
blackboxDefaultOp1
BOOLEAN blackboxDefaultOp1(int op, leftv l, leftv r)
default procedure blackboxDefaultOp1, to be called as "default:" branch
Definition: blackbox.cc:62
coeffs_BIGINT
coeffs coeffs_BIGINT
Definition: ipid.cc:52
idrec
Definition: idrec.h:35
StringSetS
void StringSetS(const char *st)
Definition: reporter.cc:128
Print
#define Print
Definition: emacs.cc:80
Werror
void Werror(const char *fmt,...)
Definition: reporter.cc:189
bigintm.h
tok.h
IDHDL
#define IDHDL
Definition: tok.h:31
n_Copy
static FORCE_INLINE number n_Copy(number n, const coeffs r)
return a copy of 'n'
Definition: coeffs.h:451
sleftv::Typ
int Typ()
Definition: subexpr.cc:1039
assume
#define assume(x)
Definition: mod2.h:390
NULL
#define NULL
Definition: omList.c:10
sleftv::CopyD
void * CopyD(int t)
Definition: subexpr.cc:745
l
int l
Definition: cfEzgcd.cc:93
n_Equal
static FORCE_INLINE BOOLEAN n_Equal(number a, number b, const coeffs r)
TRUE iff 'a' and 'b' represent the same number; they may have different representations.
Definition: coeffs.h:460
Tok2Cmdname
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:140
ipshell.h
PrintLn
void PrintLn()
Definition: reporter.cc:310
blackboxDefaultOpM
BOOLEAN blackboxDefaultOpM(int op, leftv res, leftv args)
default procedure blackboxDefaultOpM, to be called as "default:" branch
Definition: blackbox.cc:91
getBlackboxStuff
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:16
getBlackboxName
const char * getBlackboxName(const int t)
return the name to the type given by t (r/o)
Definition: blackbox.cc:186
ipid.h
omAlloc0
#define omAlloc0(size)
Definition: omAllocDecl.h:211
coeffs.h
Coefficient rings, fields and other domains suitable for Singular polynomials.
setBlackboxStuff
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:126