Annotation of OpenXM/src/ruby/testserv1.c, Revision 1.2
1.1 ogino 1:
1.2 ! ogino 2: /* $OpenXM: OpenXM/src/ruby/testserv1.c,v 1.1 2000/01/28 07:04:25 ogino Exp $ */
1.1 ogino 3:
4: #include <stdio.h>
5: /* #include <pari.h> */
6: #include "../ox_toolkit/ox.h"
7:
8: int fd_rw = 3;
9:
10: static int stack_p = 0;
11: static cmo *stack[100];
12:
13: int explain_cmo(cmo *);
14:
15: int push(cmo* m)
16: {
17: stack[stack_p] = m;
18: stack_p++;
19: }
20:
21: cmo* pop()
22: {
23: if (stack_p > 0) {
24: stack_p--;
25: return stack[stack_p];
26: }
27: return new_cmo_null();
28: }
29:
30: void pops(int n)
31: {
32: stack_p -= n;
33: if (stack_p < 0) {
34: stack_p = 0;
35: }
36: }
37:
38: int sm_popCMO()
39: {
40: cmo* m = pop();
41:
42: if (m != NULL) {
43: send_ox_cmo(fd_rw, m);
44: return 0;
45: }
46: return SM_popCMO;
47: }
48:
49: cmo_error2 *make_error2(int code)
50: {
51: return new_cmo_int32(-1);
52: }
53:
54: int get_i()
55: {
56: cmo *c = pop();
57: if (c->tag == CMO_INT32) {
58: return ((cmo_int32 *)c)->i;
59: }else if (c->tag == CMO_ZZ) {
60: return mpz_get_si(((cmo_zz *)c)->mpz);
61: }
62: make_error2(-1);
63: return 0;
64: }
65:
66: /*
67: int getdisc()
68: {
69: GEN d, x;
70: int n;
71: n = get_i();
72: fprintf(stderr, " getdisc of %d ", n);
73: x = stoi(n);
74: d = quaddisc(x);
75: n = itos(d);
76: fprintf(stderr, " = %d\n", n);
77: push(new_cmo_int32(n));
78: }
79: */
80: int prms[] = {
81: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
82: };
83:
84: int sqfr(n)
85: {
86: int i;
87: for (i = 0; prms[i] < sqrt(n); i++) {
88: while(n%(prms[i]*prms[i]) == 0)
89: n = n / (prms[i]*prms[i]);
90: }
91: return n;
92: }
93:
94: int getdisc(n)
95: {
96: int sg = 1;
97: if (n < 0) { sg = -1; n = -n;}
98: n = sqfr(n);
99: if ((n % 4 == 1) && (sg == 1))
100: return n;
101: else if ((n % 4 == 3) && (sg == -1))
102: return -n;
103: else
104: return 4*n*sg;
105: }
106:
107: int pushdisc()
108: {
1.2 ! ogino 109: if (get_i() != 1) {
! 110: push(make_error2(0));
! 111: return -1;
! 112: }
1.1 ogino 113: push(new_cmo_int32(getdisc(get_i())));
114: return 0;
115: }
116:
117: int sm_executeFunction()
118: {
119: cmo_string *func = (cmo_string *)pop();
120: if (func->tag != CMO_STRING) {
121: push(make_error2(0));
122: return -1;
123: }
124: if (strcmp(func->s, "disc") == 0) {
125: /* fprintf(stderr, "%s\n", " getdisc"); */
126: pushdisc();
127: }else {
128: push(make_error2(0));
129: return -1;
130: }
131: }
132:
133:
134: int receive_and_execute_sm_command()
135: {
136: int code = receive_int32(fd_rw);
137: switch(code) {
138: case SM_popCMO:
139: fprintf(stderr, "%s\n", " SM_popCMO");
140: sm_popCMO();
141: break;
142: case SM_executeFunction:
143: fprintf(stderr, "%s\n", " SM_executeFunction");
144: sm_executeFunction();
145: break;
146: default:
147: }
148: }
149:
150: int receive()
151: {
152: int tag;
153:
154: tag = receive_ox_tag(fd_rw);
155: switch(tag) {
156: case OX_DATA:
157: push(receive_cmo(fd_rw));
158: break;
159: case OX_COMMAND:
160: receive_and_execute_sm_command();
161: break;
162: default:
163: }
164: return 0;
165: }
166:
167: int main()
168: {
169: decideByteOrderServer(fd_rw, 0);
170:
171: /* pari_init (200000, 2000); */
172: while(1) {
173: receive();
174: }
175: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>