Annotation of OpenXM/src/ox_pari/ox_pari.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM/src/ox_pari/ox_pari.c,v 1.1 2015/08/04 05:24:44 noro Exp $ */
1.1 noro 2:
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <string.h>
1.2 ! noro 6: #include "pari/pari.h"
1.1 noro 7: #include "gmp.h"
1.2 ! noro 8: #include "gmp-impl.h"
1.1 noro 9: #include "ox_toolkit.h"
10: OXFILE *fd_rw;
11:
12: static int stack_size = 0;
13: static int stack_pointer = 0;
14: static cmo **stack = NULL;
15: extern int debug_print;
1.2 ! noro 16: long paristack=10000000;
1.1 noro 17:
18: void init_pari(void);
1.2 ! noro 19: cmo *GEN_to_cmo(GEN z);
! 20: cmo_zz *GEN_to_cmo_zz(GEN z);
! 21: cmo_list *GEN_to_cmo_list(GEN z);
! 22: GEN cmo_to_GEN(cmo *c);
! 23: GEN cmo_zz_to_GEN(cmo_zz *c);
1.1 noro 24:
25: #define INIT_S_SIZE 2048
26: #define EXT_S_SIZE 2048
27:
28: void init_pari()
29: {
1.2 ! noro 30: pari_init(paristack,2);
1.1 noro 31: }
32:
33: int initialize_stack()
34: {
35: stack_pointer = 0;
36: stack_size = INIT_S_SIZE;
37: stack = MALLOC(stack_size*sizeof(cmo*));
38: return 0;
39: }
40:
41: static int extend_stack()
42: {
43: int size2 = stack_size + EXT_S_SIZE;
44: cmo **stack2 = MALLOC(size2*sizeof(cmo*));
45: memcpy(stack2, stack, stack_size*sizeof(cmo *));
46: free(stack);
47: stack = stack2;
48: stack_size = size2;
49: return 0;
50: }
51:
52: int push(cmo* m)
53: {
54: stack[stack_pointer] = m;
55: stack_pointer++;
56: if(stack_pointer >= stack_size) {
57: extend_stack();
58: }
59: return 0;
60: }
61:
62: cmo* pop()
63: {
64: if(stack_pointer > 0) {
65: stack_pointer--;
66: return stack[stack_pointer];
67: }
68: return new_cmo_null();
69: }
70:
71: void pops(int n)
72: {
73: stack_pointer -= n;
74: if(stack_pointer < 0) {
75: stack_pointer = 0;
76: }
77: }
78:
79: #define OX_PARI_VERSION 20150731
80: #define ID_STRING "2015/07/31 15:00:00"
81:
82: int sm_mathcap()
83: {
1.2 ! noro 84: mathcap_init(OX_PARI_VERSION, ID_STRING, "ox_pari", NULL, NULL);
1.1 noro 85: push((cmo*)oxf_cmo_mathcap(fd_rw));
86: return 0;
87: }
88:
89: int sm_popCMO()
90: {
91: cmo* m = pop();
92:
93: if(m != NULL) {
94: send_ox_cmo(fd_rw, m);
95: return 0;
96: }
97: return SM_popCMO;
98: }
99:
100: cmo_error2 *make_error2(int code)
101: {
1.2 ! noro 102: return (cmo_error2 *) new_cmo_int32(code);
1.1 noro 103: }
104:
105: int get_i()
106: {
107: cmo *c = pop();
108: if(c->tag == CMO_INT32) {
109: return ((cmo_int32 *)c)->i;
110: }else if(c->tag == CMO_ZZ) {
111: return mpz_get_si(((cmo_zz *)c)->mpz);
112: }
113: make_error2(-1);
114: return 0;
115: }
116:
117: char *get_str()
118: {
119: cmo *c = pop();
120: if(c->tag == CMO_STRING) {
121: return ((cmo_string *)c)->s;
122: }
123: make_error2(-1);
124: return "";
125: }
126:
127: int cmo2int(cmo *c)
128: {
129: if(c->tag == CMO_INT32) {
130: return ((cmo_int32 *)c)->i;
131: }else if(c->tag == CMO_ZZ) {
132: return mpz_get_si(((cmo_zz *)c)->mpz);
133: } else if(c->tag == CMO_NULL){
134: return 0;
135: }
136:
137: return 0;
138: }
139:
1.2 ! noro 140: GEN cmo_zz_to_GEN(cmo_zz *c)
! 141: {
! 142: mpz_ptr mpz;
! 143: GEN z;
! 144: long *ptr;
! 145: int j,sgn,len;
! 146:
! 147: mpz = c->mpz;
! 148: sgn = mpz_sgn(mpz);
! 149: len = ABSIZ(mpz);
! 150: ptr = (long *)PTR(mpz);
! 151: z = cgeti(len+2);
! 152: for ( j = 0; j < len; j++ )
! 153: z[len-j+1] = ptr[j];
! 154: setsigne(z,sgn);
! 155: setlgefint(z,lg(z));
! 156: return z;
! 157: }
! 158:
! 159: cmo_zz *GEN_to_cmo_zz(GEN z)
! 160: {
! 161: cmo_zz *c;
! 162:
! 163: c = new_cmo_zz();
! 164: mpz_import(c->mpz,lgef(z)-2,1,sizeof(long),0,0,&z[2]);
! 165: if ( signe(z) < 0 )
! 166: mpz_neg(c->mpz,c->mpz);
! 167: return c;
! 168: }
! 169:
! 170: cmo_list *GEN_to_cmo_list(GEN z)
! 171: {
! 172: cmo_list *c;
! 173: cmo *ob;
! 174: int i,len;
! 175:
! 176: c = new_cmo_list();
! 177: len = lg(z)-1;
! 178: for ( i = 1; i <= len; i++ ) {
! 179: ob = GEN_to_cmo((GEN)z[i]);
! 180: c = list_append(c,ob);
! 181: }
! 182: return c;
! 183: }
! 184:
! 185:
! 186: GEN cmo_to_GEN(cmo *c)
! 187: {
! 188: switch ( c->tag ) {
! 189: case CMO_ZERO:
! 190: case CMO_ZZ: /* int */
! 191: return cmo_zz_to_GEN((cmo_zz *)c);
! 192: default:
! 193: return 0;
! 194: }
! 195: }
! 196:
! 197: cmo *GEN_to_cmo(GEN z)
! 198: {
! 199: if ( gcmp0(z) )
! 200: return new_cmo_zero();
! 201: switch ( typ(z) ) {
! 202: case 1: /* int */
! 203: return (cmo *)GEN_to_cmo_zz(z);
! 204: case 17: case 18: /* vector */
! 205: return (cmo *)GEN_to_cmo_list(z);
! 206: case 19: /* matrix */
! 207: return (cmo *)GEN_to_cmo_list(shallowtrans(z));
! 208: default:
! 209: return (cmo *)make_error2(typ(z));
! 210: }
! 211: }
! 212:
! 213:
! 214: #define PARI_MAX_AC 64
! 215:
1.1 noro 216: int sm_executeFunction()
217: {
1.2 ! noro 218: int ac,i;
! 219: cmo_int32 *c;
! 220: cmo *av[PARI_MAX_AC];
! 221: cmo *ret;
! 222: GEN z,m;
! 223:
1.1 noro 224: cmo_string *func = (cmo_string *)pop();
225: if(func->tag != CMO_STRING) {
226: printf("sm_executeFunction : func->tag is not CMO_STRING");fflush(stdout);
227: push((cmo*)make_error2(0));
228: return -1;
229: }
230:
1.2 ! noro 231: c = (cmo_int32 *)pop();
! 232: ac = c->i;
! 233: if ( ac > PARI_MAX_AC ) {
! 234: push((cmo*)make_error2(0));
! 235: return -1;
! 236: }
! 237: for ( i = 0; i < ac; i++ ) {
! 238: av[i] = (cmo *)pop();
! 239: fprintf(stderr,"arg%d:",i);
! 240: print_cmo(av[i]);
! 241: fprintf(stderr,"\n");
! 242: }
! 243: if(strcmp(func->s, "factor") == 0) {
! 244: z = cmo_to_GEN(av[0]);
! 245: m = Z_factor(z);
! 246: ret = GEN_to_cmo(m);
! 247: push(ret);
! 248: return 0;
! 249: } else if(strcmp(func->s, "nextprime") == 0) {
! 250: z = cmo_to_GEN(av[0]);
! 251: m = nextprime(z);
! 252: ret = GEN_to_cmo(m);
! 253: push(ret);
! 254: return 0;
! 255: } else if(strcmp(func->s, "det") == 0) {
! 256: z = cmo_to_GEN(av[0]);
! 257: m = det(z);
! 258: ret = GEN_to_cmo(m);
! 259: push(ret);
1.1 noro 260: return 0;
261: } else if( strcmp( func->s, "exit" ) == 0 ){
262: pop();
263: exit(0);
264: return 0;
265: } else {
266: push((cmo*)make_error2(0));
267: return -1;
268: }
269: }
270:
271: int receive_and_execute_sm_command()
272: {
273: int code = receive_int32(fd_rw);
274: switch(code) {
275: case SM_popCMO:
276: sm_popCMO();
277: break;
278: case SM_executeFunction:
279: sm_executeFunction();
280: break;
281: case SM_mathcap:
282: sm_mathcap();
283: break;
284: case SM_setMathCap:
285: pop();
286: break;
287: default:
288: printf("receive_and_execute_sm_command : code=%d\n",code);fflush(stdout);
289: break;
290: }
291: return 0;
292: }
293:
294: int receive()
295: {
296: int tag;
297:
298: tag = receive_ox_tag(fd_rw);
299: switch(tag) {
300: case OX_DATA:
1.2 ! noro 301: printf("receive : ox_data %d\n",tag);fflush(stdout);
1.1 noro 302: push(receive_cmo(fd_rw));
303: break;
304: case OX_COMMAND:
1.2 ! noro 305: printf("receive : ox_command %d\n",tag);fflush(stdout);
1.1 noro 306: receive_and_execute_sm_command();
307: break;
308: default:
309: printf("receive : tag=%d\n",tag);fflush(stdout);
310: }
311: return 0;
312: }
313:
314: int main()
315: {
316: GC_INIT();
317: ox_stderr_init(stderr);
318: initialize_stack();
319: init_pari();
320:
321: fprintf(stderr,"ox_pari\n");
322:
323: fd_rw = oxf_open(3);
324: oxf_determine_byteorder_server(fd_rw);
325:
326: while(1){
327: receive();
328: }
329: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>