Loading...
Searching...
No Matches
bigintm.cc
Go to the documentation of this file.
#include "kernel/mod2.h"
#include "omalloc/omalloc.h"
#include "coeffs/coeffs.h"
#include "Singular/ipid.h"
#include "Singular/subexpr.h"
#include "Singular/tok.h"
#include "Singular/blackbox.h"
#include "Singular/ipshell.h"
#include "Singular/ipid.h"
// extern coeffs coeffs_BIGINT
#include "bigintm.h"
#define HAVE_BIGINTM 1
namespace
{
#ifdef HAVE_BIGINTM
STATIC_VAR int bigintm_type_id = -1;
#endif
#ifdef HAVE_BIGINTM
static char * bigintm_String(blackbox */*b*/, void *d)
{ if (d==NULL) return omStrDup("oo");
else
{
StringSetS("");
number n=(number)d; n_Write(n, coeffs_BIGINT); d=(void*)n;
return StringEndS();
}
}
static void * bigintm_Copy(blackbox*/*b*/, void *d)
{ number n=(number)d; return n_Copy(n, coeffs_BIGINT); }
static BOOLEAN bigintm_Assign(leftv l, leftv r)
{
assume( l->Typ() == bigintm_type_id );
// blackbox *ll=getBlackboxStuff(l->Typ());
if (r->Typ()>MAX_TOK)
{
if (bigintm_type_id == r->Typ())
{
// blackbox *rr=getBlackboxStuff(r->Typ());
if (l->Data()!=NULL) { number n1=(number)l->Data(); n_Delete(&n1,coeffs_BIGINT); }
number n2=(number)r->CopyD();
if (l->rtyp==IDHDL)
{
IDDATA((idhdl)l->data)=(char *)n2;
}
else
{
l->data=(void *)n2;
}
return FALSE;
}
else
{
Werror("bigintm_Assign: assign %s (%d) = %s (%d)",
getBlackboxName(l->Typ()), l->Typ(),
getBlackboxName(r->Typ()), r->Typ());
return TRUE;
}
}
else if (r->Typ()==INT_CMD)
{
if (l->Data()!=NULL) { number n1=(number)l->Data(); n_Delete(&n1,coeffs_BIGINT); }
number n2=n_Init((int)(long)r->Data(),coeffs_BIGINT);
if (l->rtyp==IDHDL)
{
IDDATA((idhdl)l->data)=(char *)n2;
}
else
{
l->data=(void *)n2;
}
return FALSE;
}
else
Werror("assign %d = %d",l->Typ(),r->Typ());
return TRUE;
}
BOOLEAN bigintm_Op1(int op,leftv l, leftv r)
{
// interpreter: a1 is ist bigintm
assume( r->Typ() == bigintm_type_id );
/*
// "typeof( <blackbox> )" is handled by 'blackboxDefaultOp1'
if (op==TYPEOF_CMD)
{
l->data=omStrDup(getBlackboxName(r->Typ()));
l->rtyp=STRING_CMD;
return FALSE;
}
*/
return blackboxDefaultOp1(op, l, r);
}
static BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2)
{
// interpreter: a1 is ist bigintm
assume( a1->Typ() == bigintm_type_id );
// blackbox *a=getBlackboxStuff(a1->Typ());
number n1=(number)a1->Data();
switch(op)
{
case '+':
{
if (a2->Typ()==INT_CMD)
{
number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT);
number n=n_Add(n1,n2, coeffs_BIGINT);
res->data=(void *)n;
res->rtyp=a1->Typ();
return FALSE;
}
else if (a2->Typ()==a1->Typ())
{
number n2=(number)a2->Data();
number n=n_Add(n1,n2, coeffs_BIGINT);
res->data=(void *)n;
res->rtyp=a1->Typ();
return FALSE;
}
Werror("bigintm_Op2: Op: '+': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ()));
return TRUE;
}
case '-':
{
if (a2->Typ()==INT_CMD)
{
number n2=n_Init((int)(long)a2->Data(),coeffs_BIGINT);
number n=n_Sub(n1,n2, coeffs_BIGINT);
res->data=(void *)n;
res->rtyp=a1->Typ();
return FALSE;
}
else if (a2->Typ()==a1->Typ())
{
number n2=(number)a2->Data();
number n=n_Sub(n1,n2, coeffs_BIGINT);
res->data=(void *)n;
res->rtyp=a1->Typ();
return FALSE;
}
Werror("bigintm_Op2: Op: '-': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ()));
return TRUE;
}
case '*':
{
if (a2->Typ()==INT_CMD)
{
number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT);
number n=n_Mult(n1,n2, coeffs_BIGINT);
res->data=(void *)n;
res->rtyp=a1->Typ();
return FALSE;
}
else if (a2->Typ()==a1->Typ())
{
number n2=(number)a2->Data();
number n=n_Mult(n1,n2, coeffs_BIGINT);
res->data=(void *)n;
res->rtyp=a1->Typ();
return FALSE;
}
Werror("bigintm_Op2: Op: '*': Sorry unsupported 2nd argument-type: '%s' in", Tok2Cmdname(a2->Typ()));
return TRUE;
}
case EQUAL_EQUAL:
{
if( a1 == a2)
{
res->data= (void *) (TRUE);
res->rtyp= INT_CMD;
return FALSE;
} else
if (a2->Typ()==INT_CMD)
{
number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT);
res->data=(void *) (long) n_Equal(n1,n2, coeffs_BIGINT);
res->rtyp= INT_CMD;
n_Delete(&n2,coeffs_BIGINT);
return FALSE;
}
else if (a2->Typ()==a1->Typ())
{
number n2=(number)a2->Data();
res->data=(void *) (long) n_Equal(n1,n2, coeffs_BIGINT);
res->rtyp= INT_CMD;
return FALSE;
}
Werror("bigintm_Op2: Op: '==': Sorry unsupported 2nd argument-type: '%s' in", Tok2Cmdname(a2->Typ()));
return TRUE;
}
case '.':
{
if (a2->name==NULL)
{
Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) should be a NAME", Tok2Cmdname(a2->Typ()), a2->Typ());
return TRUE;
}
Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) is called '%s' in ", Tok2Cmdname(a2->Typ()), a2->Typ(), a2->name);
return TRUE;
}
default:
return blackboxDefaultOp2(op,res,a1,a2);
}
}
// BOOLEAN opM(int op, leftv res, leftv args)
static BOOLEAN bigintm_OpM(int op, leftv res, leftv args)
{
// interpreter: args->1. arg is ist bigintm
assume( args->Typ() == bigintm_type_id );
blackbox *a=getBlackboxStuff(args->Typ());
switch(op)
{
case STRING_CMD:
{
res->data=(void *)a->blackbox_String(a,args->Data());
res->rtyp=STRING_CMD;
return FALSE;
}
default:
return blackboxDefaultOpM(op, res, args);
}
return blackboxDefaultOpM(op, res, args);
}
static void bigintm_destroy(blackbox */*b*/, void *d)
{
if (d!=NULL)
{
number n=(number)d;
n_Delete(&n, coeffs_BIGINT);
}
}
#endif
}
// this is only a demo
BOOLEAN bigintm_setup()
{
#ifndef HAVE_BIGINTM
Werror("bigintm_setup: Sorry BIGINTM was not compiled in!");
return TRUE; // ok, TRUE = error!
#else
if( bigintm_type_id == -1 )
{
blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
// all undefined entries will be set to default in setBlackboxStuff
// the default Print is quite usefule,
// all other are simply error messages
b->blackbox_destroy=bigintm_destroy;
b->blackbox_String=bigintm_String;
//b->blackbox_Print=blackbox_default_Print;
//b->blackbox_Init=blackbox_default_Init;
b->blackbox_Copy=bigintm_Copy;
b->blackbox_Assign=bigintm_Assign;
b->blackbox_Op1=bigintm_Op1;
b->blackbox_Op2=bigintm_Op2;
//b->blackbox_Op3=blackboxDefaultOp3;
b->blackbox_OpM=bigintm_OpM;
bigintm_type_id = setBlackboxStuff(b,"bigintm");
Print("bigintm_setup: created a blackbox type [%d] '%s'",bigintm_type_id, getBlackboxName(bigintm_type_id));
PrintLn();
return FALSE; // ok, TRUE = error!
}
else
{
Werror("bigintm_setup: Sorry should NOT be run twice!");
return TRUE; // ok, TRUE = error!
}
#endif
}
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
bigintm_setup
BOOLEAN bigintm_setup()
Definition bigintm.cc:271
bigintm.h
setBlackboxStuff
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition blackbox.cc:143
blackboxDefaultOp2
BOOLEAN blackboxDefaultOp2(int, leftv, leftv, leftv)
default procedure blackboxDefaultOp2, to be called as "default:" branch
Definition blackbox.cc:97
getBlackboxName
const char * getBlackboxName(const int t)
return the name to the type given by t (r/o)
Definition blackbox.cc:213
getBlackboxStuff
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition blackbox.cc:17
blackboxDefaultOpM
BOOLEAN blackboxDefaultOpM(int op, leftv res, leftv args)
default procedure blackboxDefaultOpM, to be called as "default:" branch
Definition blackbox.cc:107
blackboxDefaultOp1
BOOLEAN blackboxDefaultOp1(int op, leftv l, leftv r)
default procedure blackboxDefaultOp1, to be called as "default:" branch
Definition blackbox.cc:78
blackbox.h
l
int l
Definition cfEzgcd.cc:100
b
CanonicalForm b
Definition cfModGcd.cc:4111
sleftv::CopyD
void * CopyD(int t)
Definition subexpr.cc:714
sleftv::Typ
int Typ()
Definition subexpr.cc:1048
sleftv::name
const char * name
Definition subexpr.h:87
sleftv::Data
void * Data()
Definition subexpr.cc:1192
coeffs.h
Coefficient rings, fields and other domains suitable for Singular polynomials.
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:639
n_Copy
static FORCE_INLINE number n_Copy(number n, const coeffs r)
return a copy of 'n'
Definition coeffs.h:457
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:653
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:658
n_Delete
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition coeffs.h:461
n_Write
static FORCE_INLINE void n_Write(number n, const coeffs r, const BOOLEAN bShortOut=TRUE)
Definition coeffs.h:594
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:541
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:466
Print
#define Print
Definition emacs.cc:80
res
CanonicalForm res
Definition facAbsFact.cc:60
Tok2Cmdname
const char * Tok2Cmdname(int tok)
Definition gentable.cc:137
STATIC_VAR
#define STATIC_VAR
Definition globaldefs.h:7
EQUAL_EQUAL
@ EQUAL_EQUAL
Definition grammar.cc:268
ipid.h
IDDATA
#define IDDATA(a)
Definition ipid.h:126
ipshell.h
mod2.h
assume
#define assume(x)
Definition mod2.h:389
omStrDup
#define omStrDup(s)
Definition omAllocDecl.h:263
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
NULL
#define NULL
Definition omList.c:12
omalloc.h
coeffs_BIGINT
VAR coeffs coeffs_BIGINT
Definition polys.cc:14
StringSetS
void StringSetS(const char *st)
Definition reporter.cc:128
StringEndS
char * StringEndS()
Definition reporter.cc:151
PrintLn
void PrintLn()
Definition reporter.cc:314
Werror
void Werror(const char *fmt,...)
Definition reporter.cc:189
idhdl
idrec * idhdl
Definition ring.h:22
leftv
sleftv * leftv
Definition structs.h:53
subexpr.h
tok.h
IDHDL
#define IDHDL
Definition tok.h:31
STRING_CMD
@ STRING_CMD
Definition tok.h:187
INT_CMD
@ INT_CMD
Definition tok.h:96
MAX_TOK
@ MAX_TOK
Definition tok.h:220