version 1.19, 2019/12/19 08:34:41 |
version 1.21, 2020/11/10 01:11:38 |
|
|
/* $OpenXM: OpenXM/src/ox_pari/ox_pari.c,v 1.18 2019/07/25 05:59:15 noro Exp $ */ |
/* $OpenXM: OpenXM/src/ox_pari/ox_pari.c,v 1.20 2020/08/26 06:03:31 noro Exp $ */ |
|
|
#include <signal.h> |
#include <signal.h> |
#include "ox_pari.h" |
#include "ox_pari.h" |
Line 9 static int stack_size = 0; |
|
Line 9 static int stack_size = 0; |
|
static int stack_pointer = 0; |
static int stack_pointer = 0; |
static cmo **stack = NULL; |
static cmo **stack = NULL; |
extern int debug_print; |
extern int debug_print; |
|
extern unsigned long precreal; |
long paristack=10000000; |
long paristack=10000000; |
|
|
#define INIT_S_SIZE 2048 |
#define INIT_S_SIZE 2048 |
Line 27 void gc_free(void *p,size_t size) |
|
Line 28 void gc_free(void *p,size_t size) |
|
void init_gc() |
void init_gc() |
{ |
{ |
GC_INIT(); |
GC_INIT(); |
mp_set_memory_functions(GC_malloc,gc_realloc,gc_free); |
|
} |
} |
|
|
void init_pari() |
void init_pari() |
{ |
{ |
pari_init(paristack,2); |
pari_init(paristack,2); |
|
mp_set_memory_functions(GC_malloc,gc_realloc,gc_free); |
} |
} |
|
|
int initialize_stack() |
int initialize_stack() |
Line 157 int sm_executeFunction() |
|
Line 158 int sm_executeFunction() |
|
struct parif *parif; |
struct parif *parif; |
unsigned long prec; |
unsigned long prec; |
char buf[BUFSIZ]; |
char buf[BUFSIZ]; |
int status; |
|
char *err; |
|
|
|
if ( (status = setjmp(GP_DATA->env)) != 0 ) { |
|
err = errmessage[status]; |
|
if ( status == errpile ) { |
|
sprintf(buf,"%s\nIncrease PARI stack by pari(allocatemem,size).",err); |
|
init_pari(); |
|
} else if ( strlen(err) != 0 ) |
|
sprintf(buf,"An error occured in PARI :%s",err); |
|
else |
|
sprintf(buf,"An error occured in PARI."); |
|
push((cmo*)make_error2(buf)); |
|
return -1; |
|
} |
|
cmo_string *func = (cmo_string *)pop(); |
cmo_string *func = (cmo_string *)pop(); |
if(func->tag != CMO_STRING) { |
if(func->tag != CMO_STRING) { |
sprintf(buf,"sm_executeFunction : func->tag=%d is not CMO_STRING",func->tag); |
sprintf(buf,"sm_executeFunction : func->tag=%d is not CMO_STRING",func->tag); |
Line 187 int sm_executeFunction() |
|
Line 174 int sm_executeFunction() |
|
} |
} |
for ( i = 0; i < ac; i++ ) { |
for ( i = 0; i < ac; i++ ) { |
av[i] = (cmo *)pop(); |
av[i] = (cmo *)pop(); |
// fprintf(stderr,"arg%d:",i); |
|
// print_cmo(av[i]); |
|
// fprintf(stderr,"\n"); |
|
} |
} |
if( strcmp( func->s, "exit" ) == 0 ) |
if( strcmp( func->s, "exit" ) == 0 ) |
exit(0); |
exit(0); |
|
|
|
if ( !strcmp(func->s,"allocatemem") ) { |
|
paristack = cmo_to_int(av[0]); |
|
pari_close(); |
|
init_pari(); |
|
return 0; |
|
} |
parif =search_parif(func->s); |
parif =search_parif(func->s); |
if ( !parif ) { |
if ( !parif ) { |
sprintf(buf,"%s : not implemented",func->s); |
sprintf(buf,"%s : not implemented",func->s); |
push((cmo*)make_error2(buf)); |
push((cmo*)make_error2(buf)); |
return -1; |
return -1; |
} else if ( parif->type == 0 ) { |
} else if ( parif->type <= 2 ) { |
/* one long int variable */ |
/* type=1 => one GEN argument possibly with prec */ |
int a = cmo_to_int(av[0]); |
/* type=2 => one GEN argument with optarg */ |
a = (int)(parif->f)(a); |
/* type=3 => one GEN, return ulong */ |
ret = (cmo *)new_cmo_int32(a); |
|
push(ret); |
|
return 0; |
|
} else if ( parif->type == 1 ) { |
|
/* one number/poly/matrix argument possibly with prec */ |
|
av0 = avma; |
av0 = avma; |
z = cmo_to_GEN(av[0]); |
z = cmo_to_GEN(av[0]); |
prec = ac==2 ? cmo_to_int(av[1])*3.32193/32+3 : precreal; |
prec = ac==2 ? ndec2prec(cmo_to_int(av[1])) : nbits2prec(precreal); |
if ( ismatrix(z) ) { |
if ( ismatrix(z) ) { |
int i,len; |
int i,len; |
len = lg(z); |
len = lg(z); |
Line 219 int sm_executeFunction() |
|
Line 205 int sm_executeFunction() |
|
settyp(z,t_MAT); |
settyp(z,t_MAT); |
z = shallowtrans(z); |
z = shallowtrans(z); |
} |
} |
printf("input : "); output(z); |
pari_CATCH(CATCH_ALL) { |
m = (*parif->f)(z,prec); |
GEN E = pari_err_last(); |
ret = GEN_to_cmo(m); |
long code = err_get_num(E); |
avma = av0; |
char *err = pari_err2str(E); |
push(ret); |
if ( code == e_MEM || code == e_STACK ) { |
return 0; |
sprintf(buf,"%s\nIncrease PARI stack by pari(allocatemem,size).",err); |
} else if ( parif->type == 2 ) { |
} else |
/* one number/poly/matrix argument with flag=0 */ |
sprintf(buf,"An error occured in PARI :%s",err); |
av0 = avma; |
push((cmo*)make_error2(buf)); |
z = cmo_to_GEN(av[0]); |
pari_CATCH_reset(); |
if ( ismatrix(z) ) { |
avma = av0; |
int i,len; |
return -1; |
len = lg(z); |
|
for ( i = 1; i < len; i++ ) |
|
settyp(z[i],t_COL); |
|
settyp(z,t_MAT); |
|
z = shallowtrans(z); |
|
} |
} |
printf("input : "); output(z); |
pari_TRY { |
m = (*parif->f)(z,0); |
ret = 0; |
ret = GEN_to_cmo(m); |
if ( parif->type == 0 ) { |
avma = av0; |
gp_allocatemem(z); |
push(ret); |
ret = av[0]; |
return 0; |
/* allocatemem */ |
|
} else if ( parif->type == 1 ) { |
|
m = (*parif->f)(z,prec); |
|
ret = GEN_to_cmo(m); |
|
} else if ( parif->type == 2 ) { |
|
m = (*parif->f)(z,parif->opt); |
|
ret = GEN_to_cmo(m); |
|
} else if ( parif->type == 3 ) { |
|
/* XXX */ |
|
unsigned long a; |
|
a = (unsigned long)(*parif->f)(z); |
|
ret = (cmo *)new_cmo_int32((int)a); |
|
} |
|
avma = av0; |
|
push(ret); |
|
return 0; |
|
} |
|
pari_ENDCATCH |
} else { |
} else { |
sprintf(buf,"%s : not implemented",func->s); |
sprintf(buf,"%s : not implemented",func->s); |
push((cmo*)make_error2(buf)); |
push((cmo*)make_error2(buf)); |
|
|
if ( sigsetjmp(ox_env,~0) ) { |
if ( sigsetjmp(ox_env,~0) ) { |
#endif |
#endif |
fprintf(stderr,"resetting libpari and sending OX_SYNC_BALL..."); |
fprintf(stderr,"resetting libpari and sending OX_SYNC_BALL..."); |
initialize_stack(); |
|
init_pari(); |
init_pari(); |
|
initialize_stack(); |
send_ox_tag(fd_rw,OX_SYNC_BALL); |
send_ox_tag(fd_rw,OX_SYNC_BALL); |
fprintf(stderr,"done\n"); |
fprintf(stderr,"done\n"); |
} else { |
} else { |
init_gc(); |
init_gc(); |
ox_stderr_init(stderr); |
ox_stderr_init(stderr); |
initialize_stack(); |
|
init_pari(); |
init_pari(); |
|
initialize_stack(); |
|
|
fprintf(stderr,"ox_pari\n"); |
fprintf(stderr,"ox_pari\n"); |
|
|