[BACK]Return to ox_pari.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_pari

Diff for /OpenXM/src/ox_pari/ox_pari.c between version 1.13 and 1.24

version 1.13, 2016/08/01 01:35:01 version 1.24, 2022/03/28 09:21:56
Line 1 
Line 1 
 /*  $OpenXM: OpenXM/src/ox_pari/ox_pari.c,v 1.12 2015/08/21 01:20:30 noro Exp $  */  /*  $OpenXM: OpenXM/src/ox_pari/ox_pari.c,v 1.23 2021/03/25 07:03:21 noro Exp $  */
   
   #include <signal.h>
 #include "ox_pari.h"  #include "ox_pari.h"
   
 OXFILE *fd_rw;  OXFILE *fd_rw;
Line 8  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 26  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);
     gmp_check();
 }  }
   
 int initialize_stack()  int initialize_stack()
Line 85  void pops(int n)
Line 88  void pops(int n)
   
 int sm_mathcap()  int sm_mathcap()
 {  {
   mathcap_init(OX_PARI_VERSION, ID_STRING, "ox_pari", NULL, NULL);  #if 0
     char *opts[] = {"no_ox_reset", NULL};
     mathcap_init2(OX_PARI_VERSION, ID_STRING, "ox_pari", NULL, NULL, opts);
   #else
     mathcap_init2(OX_PARI_VERSION, ID_STRING, "ox_pari", NULL, NULL, NULL);
   #endif
   push((cmo*)oxf_cmo_mathcap(fd_rw));    push((cmo*)oxf_cmo_mathcap(fd_rw));
   return 0;    return 0;
 }  }
Line 151  int sm_executeFunction()
Line 159  int sm_executeFunction()
   struct parif *parif;    struct parif *parif;
   unsigned long prec;    unsigned long prec;
   char buf[BUFSIZ];    char buf[BUFSIZ];
   
   if ( setjmp(GP_DATA->env) ) {  
     sprintf(buf,"sm_executeFunction : 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 172  int sm_executeFunction()
Line 175  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;
     }
     if ( !strcmp(func->s,"pari_setprec") ) {
       long n,prec;
   
       n = cmo_to_int(av[0]);
       setrealprecision(n,&prec);
       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 204  int sm_executeFunction()
Line 213  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));
Line 255  int receive_and_execute_sm_command()
Line 276  int receive_and_execute_sm_command()
     exit(0);      exit(0);
     break;      break;
   default:    default:
     printf("receive_and_execute_sm_command : code=%d\n",code);fflush(stdout);      ox_printf("receive_and_execute_sm_command : code=%d\n",code);
     break;      break;
   }    }
   return 0;    return 0;
Line 268  int receive()
Line 289  int receive()
   tag = receive_ox_tag(fd_rw);    tag = receive_ox_tag(fd_rw);
   switch(tag) {    switch(tag) {
   case OX_DATA:    case OX_DATA:
     printf("receive : ox_data %d\n",tag);fflush(stdout);      ox_printf("receive : ox_data %d\n",tag);
     push(receive_cmo(fd_rw));      push(receive_cmo(fd_rw));
     break;      break;
   case OX_COMMAND:    case OX_COMMAND:
     printf("receive : ox_command %d\n",tag);fflush(stdout);      ox_printf("receive : ox_command %d\n",tag);
     receive_and_execute_sm_command();      receive_and_execute_sm_command();
     break;      break;
   default:    default:
     printf("receive : tag=%d\n",tag);fflush(stdout);      ox_printf("receive : tag=%d\n",tag);
   }    }
   return 0;    return 0;
 }  }
   
   #if defined(ANDROID)
   jmp_buf ox_env;
   #else
   sigjmp_buf ox_env;
   #endif
   
   void usr1_handler(int sig)
   {
   #if defined(ANDROID)
     _longjmp(ox_env,1);
   #else
     siglongjmp(ox_env,1);
   #endif
   }
   
   #if defined(USE_OXPARI_LOG)
   #define LOGFILE "/tmp/oxpari.log"
   #endif
   
 int main()  int main()
 {  {
   init_gc();  #if defined(ANDROID)
   ox_stderr_init(stderr);    if ( _setjmp(ox_env) ) {
   initialize_stack();  #else
   init_pari();    if ( sigsetjmp(ox_env,~0) ) {
   #endif
       ox_printf("resetting libpari and sending OX_SYNC_BALL...");
       init_pari();
       initialize_stack();
       send_ox_tag(fd_rw,OX_SYNC_BALL);
       ox_printf("done\n");
     } else {
       init_gc();
   
   fprintf(stderr,"ox_pari\n");  #if defined(LOGFILE)
       ox_stderr_init(fopen(LOGFILE,"a"));
   #endif
       init_pari();
       initialize_stack();
   
       ox_printf("ox_pari\n");
   
   fd_rw = oxf_open(3);      fd_rw = oxf_open(3);
   oxf_determine_byteorder_server(fd_rw);      oxf_determine_byteorder_server(fd_rw);
     }
   
   #if defined(__CYGWIN__)
     void *mysignal(int sig,void (*handler)(int m));
     mysignal(SIGUSR1,usr1_handler);
   #else
     signal(SIGUSR1,usr1_handler);
   #endif
   
   while(1){    while(1){
     receive();      receive();

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.24

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>