Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1898 lines
41 KiB

  1. /* Copyright (C) 2011-2014 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* stackN.c */
  14. #include "conf.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <strings.h>
  19. #include <sys/types.h>
  20. #include <math.h>
  21. #include "nife.h"
  22. #include "mth.h"
  23. #include "err.h"
  24. #include "lib.h"
  25. #include "stackN.h"
  26. #include "stackL.h"
  27. #include "stackF.h"
  28. #include "stackV.h"
  29. #include "debug.h"
  30. #include "net.h"
  31. #include "scs.h"
  32. #define _VERIF_STACK_ if (StackN == VIDE) { messErr(2); return; }
  33. void IF_vars(void)
  34. {
  35. char * L;
  36. printf("DEBUG : ");
  37. if (Debug) printf("ON"); else printf("OFF");
  38. printf("\nDefault type=");
  39. if (DOUBLE) printf("REAL"); else printf("INTEGER");
  40. printf("\nDefault echo=");
  41. if (ECHOOFF) printf("OFF"); else printf("ON");
  42. printf("\nNetServer : \"%s\"",NetServer);
  43. printf("\nSCS Key : 0x%lx",(long)getScs());
  44. printf("\nNetKey : 0x%lx",(long)NetKey);
  45. printf("\nVARS : ");
  46. switch(VARS) {
  47. case 1 :
  48. printf("DOWN");
  49. break;
  50. case 2 :
  51. printf("UP");
  52. break;
  53. default :
  54. printf("OFF");
  55. break;
  56. }
  57. printf("\nVariable Function : ");
  58. if ((L=libByAddr(FCT_INST)) != NULL) printf("%s (std lib)",L);
  59. else {
  60. if ((L=fctByAddr(FCT_INST)) != NULL) printf("%s (user function)",L);
  61. else {
  62. if ((L=varByAddr(FCT_INST)) != NULL) printf("%s (variable)",L);
  63. else printf("none");
  64. }
  65. }
  66. printf("\nNBTAB=%d\nNBLIG=%d\n",NBTAB,NBLIG);
  67. }
  68. void IF_REAL(void) { _MODIF_DOUBLE_(1); }
  69. void IF_INTEGER(void) { _MODIF_DOUBLE_(0); }
  70. void IF_ECHOFF(void) { _MODIF_ECHOOFF_(1); }
  71. void IF_ECHOON(void) { _MODIF_ECHOOFF_(0); }
  72. /* IMPORTANT **************************
  73. la taille t est codee sur 30 bits + a droite
  74. B31 = 1 si Var
  75. B32 = 1 si REAL
  76. **********************/
  77. #define MSK_T (uint32_t)(0x3FFFFFFF)
  78. #define MSK_V (uint32_t)(0x40000000)
  79. #define MSK_R (uint32_t)(0x80000000)
  80. struct Num {
  81. uint32_t t; /* taille : cf precisions ci-dessus */
  82. uint32_t key; /* net key */
  83. void *n;
  84. union {
  85. long long l;
  86. double d;
  87. };
  88. };
  89. int lAdrNum(void)
  90. {
  91. struct Num N;
  92. return((char*)&(N.l) - (char*)&(N.n));
  93. }
  94. void putLong(long long l)
  95. {
  96. void * M;
  97. struct Num * N;
  98. if ((M = malloc(sizeof(struct Num))) == NULL) stopErr("putLong","malloc");
  99. N = (struct Num*)M;
  100. N->t = 1;
  101. N->n = StackN;
  102. N->l = l;
  103. _MODIF_STACKN_(M);
  104. }
  105. void putDouble(double d)
  106. {
  107. void * M;
  108. struct Num * N;
  109. if ((M = malloc(sizeof(struct Num))) == NULL) stopErr("putDouble","malloc");
  110. N = (struct Num*)M;
  111. N->t = 1 | MSK_R;
  112. N->n = StackN;
  113. N->d = d;
  114. _MODIF_STACKN_(M);
  115. }
  116. int putVal(char *V)
  117. {
  118. void * M;
  119. char * R;
  120. struct Num * N;
  121. long long l;
  122. double d;
  123. #ifdef DEBUG
  124. printf("putVal (%s) \n",V);
  125. #endif
  126. l = strtoll(V,&R,0);
  127. if (strlen(R)==0) {
  128. if ((M = malloc(sizeof(struct Num))) == NULL) stopErr("putVal","malloc");
  129. N = (struct Num*)M;
  130. N->t = 1;
  131. N->n = StackN;
  132. N->l = l;
  133. if (fctEnCours) makeFct(T_NUM,M);
  134. else _MODIF_STACKN_(M);
  135. return 1;
  136. } else {
  137. d = strtod(V,&R);
  138. if (strlen(R)==0) {
  139. if ((M=malloc(sizeof(struct Num))) == NULL) stopErr("putVal","malloc");
  140. N = (struct Num*)M;
  141. N->t = 1 | MSK_R;
  142. N->n = StackN;
  143. N->d = d;
  144. if (fctEnCours) makeFct(T_NUM,M);
  145. else _MODIF_STACKN_(M);
  146. return 1;
  147. }
  148. }
  149. return 0;
  150. }
  151. static int isScalar(void)
  152. {
  153. struct Num *Elt;
  154. int t;
  155. if(StackN == VIDE) return 0;
  156. Elt = (struct Num *)StackN;
  157. if ((t = Elt->t&MSK_T) == 1) return 1;
  158. return 0;
  159. }
  160. static int dropElt(void)
  161. {
  162. struct Num *Elt;
  163. if(StackN == VIDE) return 0;
  164. Elt = (struct Num *)StackN;
  165. _MODIF_STACKN_(Elt->n);
  166. if (!(Elt->t&MSK_V)) free((void*)Elt);
  167. return 1;
  168. }
  169. void IF_drop(void)
  170. {
  171. _VERIF_STACK_
  172. dropElt();
  173. }
  174. static long long getVal(void)
  175. {
  176. struct Num *Elt;
  177. Elt = (struct Num *)StackN;
  178. if (Elt->t & MSK_R) return((long long)Elt->d);
  179. else return(Elt->l);
  180. }
  181. void IF_vers(void)
  182. {
  183. putDouble(atof(VERSION));
  184. }
  185. /* fonction pour les autres */
  186. int getParLong(long *V)
  187. {
  188. if (StackN == VIDE) {
  189. messErr(2); return 0 ;
  190. }
  191. if (!isScalar()) {
  192. messErr(36); return 0 ;
  193. }
  194. *V = (long)getVal();
  195. dropElt();
  196. return 1;
  197. }
  198. void putVar(void * V)
  199. {
  200. struct Num *Elt;
  201. if (V==VIDE) return;
  202. Elt = (struct Num *)V;
  203. Elt->n = StackN;
  204. _MODIF_STACKN_(V);
  205. }
  206. void * getVar(void)
  207. {
  208. void * N;
  209. struct Num *Elt;
  210. N = StackN;
  211. if (N != VIDE) {
  212. Elt = (struct Num *)N;
  213. _MODIF_STACKN_(Elt->n); /* drop no free !! */
  214. Elt->n = VIDE;
  215. Elt->t = Elt->t|MSK_V; /* VARIABLE ! */
  216. }
  217. return N;
  218. }
  219. void IF_NBTAB(void)
  220. {
  221. long V;
  222. if (getParLong(&V)) _MODIF_NBTAB_(V);
  223. }
  224. void IF_NBLIG(void)
  225. {
  226. long V;
  227. if (getParLong(&V)) _MODIF_NBLIG_(V);
  228. }
  229. void IF_VAROFF(void) { _MODIF_VARS_(0); }
  230. void IF_VARDOWN(void) { _MODIF_VARS_(1); }
  231. void IF_VARUP(void) { _MODIF_VARS_(2); }
  232. void insertVal(void*A)
  233. {
  234. void * M;
  235. struct Num *Elt;
  236. if ((M = malloc(sizeof(struct Num))) == NULL) stopErr("insertVal","malloc");
  237. bcopy(A,M,sizeof(struct Num));
  238. Elt=(struct Num*)M;
  239. Elt->n = StackN;
  240. _MODIF_STACKN_(M);
  241. }
  242. static void Ramp(int D)
  243. {
  244. long n, i, dep=1;
  245. void * M;
  246. struct Num * N;
  247. long long *T;
  248. _VERIF_STACK_
  249. if (!isScalar()) {
  250. messErr(36); return;
  251. }
  252. n = (long)getVal();
  253. dropElt();
  254. if (n > 1) {
  255. if (D) { /* double ramp */
  256. dep = -n;
  257. n = (2*n) +1;
  258. }
  259. if ((M = malloc(sizeof(struct Num)+((n-1)*(sizeof(double))))) == NULL)
  260. stopErr("Ramp","malloc");
  261. N = (struct Num*)M;
  262. N->t = n;
  263. N->n = StackN;
  264. _MODIF_STACKN_(M);
  265. T = &(N->l);
  266. for(i=0;i<n;i++) T[i]=i+dep;
  267. } else messErr(30);
  268. }
  269. void IF_ramp(void)
  270. {
  271. Ramp(0);
  272. }
  273. void IF_dramp(void)
  274. {
  275. Ramp(1);
  276. }
  277. void IF_stack_clear(void)
  278. {
  279. while (StackN != VIDE) dropElt();
  280. }
  281. #define ELT_POINT -9
  282. static void printElt(struct Num * N, long I)
  283. {
  284. long n, i, m, nt, IB;
  285. long long *l;
  286. double *d;
  287. IB = I;
  288. n = N->t&MSK_T;
  289. if (IB < 0) nt = 3;
  290. else nt = NBTAB;
  291. if (n > nt) m=nt-1;
  292. else m=n-1;
  293. if (I==ELT_POINT) {
  294. IB=0;
  295. n=2;
  296. m=1;
  297. }
  298. if (IB) printf(" ");
  299. if(N->t & MSK_R) {
  300. if (n==1) printf("%g (REAL)",N->d);
  301. else {
  302. d = &N->d;
  303. for(i=0;i<m;i++) printf("%g ",*d++);
  304. if (I==ELT_POINT) return;
  305. if (n > nt) printf("... ");
  306. printf("%g (REAL)[%ld]",*(&N->d+(n-1)),n);
  307. }
  308. } else {
  309. if (n==1) printf("%lld (INTEGER)",N->l);
  310. else {
  311. l = &N->l;
  312. for(i=0;i<m;i++) printf("%lld ",*l++);
  313. if (I==ELT_POINT) return;
  314. if (n > nt) printf("... ");
  315. printf("%lld (INTEGER)[%ld]",*(&N->l+(n-1)),n);
  316. }
  317. }
  318. if ((IB>0) && (N->t&MSK_V)) printf(" Var. %s",varByAddrA((void*)N));
  319. if (IB==1) printf(" <- top");
  320. if (IB) printf("\n");
  321. }
  322. void printNumber(void * E)
  323. {
  324. printElt((struct Num*)E, 0);
  325. }
  326. void numVarOff(void * A)
  327. {
  328. struct Num * N;
  329. N = (struct Num*) A;
  330. N->t = N->t & ~MSK_V;
  331. }
  332. void IF_show_stack(void)
  333. {
  334. void * Next;
  335. struct Num * N;
  336. long i=0,Nbl;
  337. char s;
  338. Nbl=NBLIG;
  339. Next = StackN;
  340. while (Next != VIDE) {
  341. N = (struct Num*) Next;
  342. i++;
  343. if (i<=Nbl) printElt(N,i);
  344. Next = N->n;
  345. }
  346. if (i<=Nbl) printf("<end of stack>\n");
  347. else {
  348. if (i==Nbl+1) s = ' ';
  349. else s = 's';
  350. printf(" ... and %ld other%c element%c !\n",i-Nbl,s,s);
  351. }
  352. }
  353. void IF_point(void)
  354. {
  355. struct Num *Elt;
  356. _VERIF_STACK_
  357. Elt = (struct Num *)StackN;
  358. printElt(Elt,ELT_POINT);
  359. /* printf("\n"); */
  360. dropElt();
  361. }
  362. void * duplicateNum(void * S, int vSoff)
  363. {
  364. struct Num *Elt, *NElt;
  365. void * M;
  366. uint32_t n;
  367. int s;
  368. Elt = (struct Num*)S;
  369. n = Elt->t&MSK_T;
  370. s = sizeof(struct Num)+((n-1)*(sizeof(double)));
  371. if ((M = malloc(s)) == NULL) stopErr("dupElt","malloc");
  372. bcopy((void*)Elt,M,s);
  373. NElt = (struct Num *)M;
  374. NElt->n = VIDE;
  375. NElt->t = Elt->t;
  376. if (vSoff) Elt->t = Elt->t & ~MSK_V; /* Source no more a Var */
  377. return(M);
  378. }
  379. static void dupElt(struct Num * Elt)
  380. {
  381. struct Num *NElt;
  382. void * M;
  383. uint32_t n;
  384. int s;
  385. n = Elt->t&MSK_T;
  386. s = sizeof(struct Num)+((n-1)*(sizeof(double)));
  387. if ((M = malloc(s)) == NULL) stopErr("dupElt","malloc");
  388. bcopy((void*)Elt,M,s);
  389. NElt = (struct Num *)M;
  390. NElt->n = StackN;
  391. NElt->t = Elt->t & ~MSK_V; /* au cas ou Var */
  392. _MODIF_STACKN_(M);
  393. ;
  394. }
  395. void IF_dup(void)
  396. {
  397. _VERIF_STACK_
  398. dupElt((struct Num *)StackN);
  399. }
  400. void IF_swap(void)
  401. {
  402. struct Num *Elt, *Elt2;
  403. _VERIF_STACK_
  404. Elt = (struct Num *)StackN;
  405. if (Elt->n != VIDE) {
  406. _MODIF_STACKN_(Elt->n);
  407. Elt2 = (struct Num *)StackN;
  408. Elt->n = Elt2->n;
  409. Elt2->n = (void*)Elt;
  410. }
  411. else messErr(4);
  412. }
  413. void IF_over (void)
  414. {
  415. struct Num *Elt;
  416. _VERIF_STACK_
  417. Elt = (struct Num *)StackN;
  418. if (Elt->n != VIDE)
  419. dupElt((struct Num *)Elt->n);
  420. else messErr(4);
  421. }
  422. void IF_pick(void)
  423. {
  424. void * Next;
  425. struct Num * N;
  426. long n, i;
  427. _VERIF_STACK_
  428. if (!isScalar()) {
  429. messErr(36); return;
  430. }
  431. n = (long)getVal();
  432. dropElt();
  433. if (n>0) {
  434. Next = StackN;
  435. i=1;
  436. while (Next != VIDE) {
  437. if (i==n) break;
  438. N = (struct Num*) Next;
  439. Next = N->n;
  440. i++;
  441. }
  442. if (Next != VIDE) dupElt((struct Num *)Next);
  443. else messErr(4);
  444. } else messErr(29);
  445. }
  446. static int rotateBid(long n, int d) /* d=0 : rot d=1 : unrot */
  447. {
  448. void **ANext;
  449. struct Num * N, *N1;
  450. long i;
  451. ANext = _ADDR_STACKN_;
  452. i=1;
  453. while (*ANext != VIDE) {
  454. if (i==n) break;
  455. N = (struct Num*) *ANext;
  456. ANext = &N->n;
  457. i++;
  458. }
  459. if (*ANext != VIDE) {
  460. N = (struct Num*) *ANext;
  461. if (d) { /* unrot */
  462. N1 = (struct Num*) StackN;
  463. _MODIF_STACKN_(N1->n);
  464. N1->n = N->n;
  465. N->n = (void*)N1;
  466. } else { /* rot */
  467. *ANext = N->n;
  468. N->n = StackN;
  469. _MODIF_STACKN_((void*)N);
  470. }
  471. return 1;
  472. } else return 0;
  473. }
  474. void IF_rot(void)
  475. {
  476. if (!rotateBid(3L,0)) messErr(4);
  477. }
  478. void IF_unrot(void)
  479. {
  480. if (!rotateBid(3L,1)) messErr(4);
  481. }
  482. void IF_roll(void)
  483. {
  484. long n;
  485. _VERIF_STACK_
  486. if (!isScalar()) {
  487. messErr(36); return;
  488. }
  489. n = (long)getVal();
  490. dropElt();
  491. if (n>1) {
  492. if (!rotateBid(n,0)) messErr(4);
  493. } else messErr(30);
  494. }
  495. void IF_unroll(void)
  496. {
  497. long n;
  498. _VERIF_STACK_
  499. if (!isScalar()) {
  500. messErr(36); return;
  501. }
  502. n = (long)getVal();
  503. dropElt();
  504. if (n>1) {
  505. if (!rotateBid(n,1)) messErr(4);
  506. } else messErr(30);
  507. }
  508. void IF_depth(void)
  509. {
  510. void * Next;
  511. struct Num * N;
  512. long long i=0;
  513. Next = StackN;
  514. while (Next != VIDE) {
  515. N = (struct Num*) Next;
  516. i++;
  517. Next = N->n;
  518. }
  519. putLong(i);
  520. }
  521. int nbOnStack(void* A)
  522. {
  523. void * Next;
  524. struct Num * N;
  525. int i=0;
  526. Next = StackN;
  527. while (Next != VIDE) {
  528. N = (struct Num*) Next;
  529. if (Next == A) i++;
  530. Next = N->n;
  531. }
  532. return i;
  533. }
  534. static uint32_t nbSizeTypeOnStack(uint32_t n,uint32_t *T)
  535. {
  536. void * Next;
  537. struct Num * N;
  538. uint32_t i=0, D=0, S=0;
  539. Next = StackN;
  540. while (Next != VIDE) {
  541. N = (struct Num*) Next;
  542. S += N->t&MSK_T;
  543. if (N->t&MSK_R) D=1;
  544. Next = N->n;
  545. i++;
  546. if (i==n) break;
  547. }
  548. if (D) S = S|MSK_R;
  549. *T = S;
  550. return i;
  551. }
  552. void IF_Ndrop(void)
  553. {
  554. long n, i;
  555. _VERIF_STACK_
  556. if (!isScalar()) {
  557. messErr(36); return;
  558. }
  559. n = (long)getVal();
  560. dropElt();
  561. for(i=0;i<n;i++) if (!dropElt()) break;
  562. if (i<n) messErr(4); /* erreur pour la rigueur ! */
  563. }
  564. void IF_Ndup(void)
  565. {
  566. void *Next, *M, *NewS, **P;
  567. struct Num * Elt, * NElt;
  568. uint32_t n, i=0, t;
  569. int s;
  570. _VERIF_STACK_
  571. if (!isScalar()) {
  572. messErr(36); return;
  573. }
  574. n = (long)getVal();
  575. dropElt();
  576. if (n>0) {
  577. NewS=VIDE;
  578. P=&NewS;
  579. Next = StackN;
  580. while (Next != VIDE) {
  581. Elt = (struct Num*) Next;
  582. t = Elt->t&MSK_T;
  583. s = sizeof(struct Num)+((t-1)*(sizeof(double)));
  584. if ((M = malloc(s)) == NULL) stopErr("IF_Ndup","malloc");
  585. bcopy(Next,M,s);
  586. *P = M;
  587. NElt = (struct Num *)M;
  588. NElt->t = NElt->t & ~MSK_V; /* au cas ou Var */
  589. P=&NElt->n;
  590. i++;
  591. if (i==n) break;
  592. Next = Elt->n;
  593. }
  594. NElt->n = StackN;
  595. _MODIF_STACKN_(NewS);
  596. if (i<n) messErr(4); /* erreur pour la rigueur ! */
  597. } else messErr(29);
  598. }
  599. static void toDouble(struct Num * N)
  600. {
  601. uint32_t n;
  602. int i;
  603. double *d;
  604. long long *l;
  605. if (N->t & MSK_R) return;
  606. n = N->t&MSK_T;
  607. d = &N->d;
  608. l = &N->l;
  609. for(i=0; i<n; i++) *d++ = (double)*l++;
  610. N->t = N->t|MSK_R;
  611. }
  612. /* NET Functions for STSP */
  613. void IF_NetKey (void)
  614. {
  615. _VERIF_STACK_
  616. if (!isScalar()) {
  617. messErr(36); return;
  618. }
  619. _MODIF_NetKey_((uint32_t)getVal());
  620. dropElt();
  621. }
  622. void IF_NetErrVal (void)
  623. {
  624. putLong(-(long long)NetKey);
  625. }
  626. void StackToNet(long n)
  627. {
  628. struct Num *Elt;
  629. int i;
  630. uint32_t t, l;
  631. for (i=0; i<n ; i++) {
  632. if(StackN == VIDE) break;
  633. Elt = (struct Num *)StackN;
  634. t = Elt->t&MSK_T;
  635. l=(sizeof(struct Num)+((t-1)*(sizeof(double))));
  636. Elt->key = NetKey;
  637. sendDataC(StackN, l);
  638. dropElt();
  639. }
  640. }
  641. int NetDepth(uint32_t k)
  642. {
  643. void * Next;
  644. struct Num * N;
  645. int v=0;
  646. Next = StackN;
  647. while (Next != VIDE) {
  648. N = (struct Num*) Next;
  649. if (N->key == k) v++;
  650. Next = N->n;
  651. }
  652. return v;
  653. }
  654. void NetToStack(int s, uint32_t k)
  655. {
  656. void * Next;
  657. struct Num * N;
  658. uint32_t t, l;
  659. Next = StackN;
  660. while (Next != VIDE) {
  661. N = (struct Num*) Next;
  662. if (N->key == k) {
  663. t = N->t&MSK_T;
  664. l=(sizeof(struct Num)+((t-1)*(sizeof(double))));
  665. sendData(s, Next, l);
  666. }
  667. Next = N->n;
  668. }
  669. }
  670. void IF_show_netStack(uint32_t k)
  671. {
  672. void * Next;
  673. struct Num * N;
  674. Next = StackN;
  675. printf("<bottom of net stack>\n");
  676. while (Next != VIDE) {
  677. N = (struct Num*) Next;
  678. if (k == UNI_KEY) {
  679. printf("<0x%.8lx> ",(long)N->key);
  680. printElt(N,(long)-1);
  681. } else {
  682. if (N->key == k) printElt(N,(long)-1);
  683. }
  684. Next = N->n;
  685. }
  686. printf("<top of net stack> key=0x%lx\n",(long)k);
  687. }
  688. void IF_netDrop(uint32_t k)
  689. {
  690. void * Next, **ANext;
  691. struct Num * N;
  692. Next = StackN;
  693. ANext = _ADDR_STACKN_;
  694. while (Next != VIDE) {
  695. N = (struct Num*) Next;
  696. if (k == N->key) {
  697. *ANext = N->n;
  698. free(Next);
  699. Next = *ANext;
  700. continue;
  701. }
  702. Next = N->n;
  703. ANext = &(N->n);
  704. }
  705. }
  706. /* end of Net functions */
  707. static void IF_fct_2(char O)
  708. {
  709. struct Num *Elt, *Elt2;
  710. long long ConstL, *l1, *l2;
  711. double ConstD, *d1, *d2;
  712. int D1=0, D2=0, T1, T2, i;
  713. int M_C, M_D, M_S; /* Mode Const : 1 ou 0 | Double : 1 ou 0 | Swap : 1 ou 0 */
  714. bool B=TRUE;
  715. _VERIF_STACK_
  716. Elt = (struct Num *)StackN;
  717. T1 = Elt->t&MSK_T;
  718. D1 = Elt->t&MSK_R;
  719. if (Elt->n == VIDE) {
  720. messErr(4);
  721. return;
  722. }
  723. Elt2 = (struct Num *)Elt->n;
  724. T2 = Elt2->t&MSK_T;
  725. D2 = Elt2->t&MSK_R;
  726. /* si 2 tab de dim diff pas possible !! */
  727. if ((T1>1) && (T2>1) && (T1!=T2)) {
  728. messErr(3);
  729. return;
  730. }
  731. M_S = M_C = M_D = 0;
  732. if ((T1>1) && (T2==1)) { /* on swap */
  733. IF_swap();
  734. D1=D2=0;
  735. Elt = (struct Num *)StackN;
  736. T1 = Elt->t&MSK_T;
  737. D1 = Elt->t&MSK_R;
  738. Elt2 = (struct Num *)Elt->n;
  739. T2 = Elt2->t&MSK_T;
  740. D2 = Elt2->t&MSK_R;
  741. M_S=1;
  742. }
  743. if (D1!=D2) { /* on transforme long en double */
  744. if (D2) toDouble(Elt); else toDouble(Elt2);
  745. M_D = 1;
  746. } else if(D1) M_D = 1;
  747. l1 = &Elt->l;
  748. l2 = &Elt2->l;
  749. d1 = &Elt->d;
  750. d2 = &Elt2->d;
  751. if (T1==1) {
  752. M_C=1;
  753. if (M_D) ConstD = *d1;
  754. else ConstL = *l1;
  755. }
  756. /* pour debug
  757. printf("T1=%d T2=%d M_C=%d M_D=%d M_S=%d ",T1,T2,M_C,M_D,M_S);
  758. if (M_C)
  759. if (M_D) printf("ConstD=%g",ConstD); else printf("ConstL=%lld",ConstL);
  760. printf("\n");
  761. *****/
  762. switch(O) {
  763. case '+' :
  764. if (M_C) {
  765. if (M_D) for (i=0;i<T2;i++) *d2++ += ConstD;
  766. else for (i=0;i<T2;i++) *l2++ += ConstL;
  767. } else {
  768. if (M_D) for (i=0;i<T2;i++) *d2++ += *d1++;
  769. else for (i=0;i<T2;i++) *l2++ += *l1++;
  770. }
  771. break;
  772. case '-' :
  773. if (M_C) {
  774. if (M_S) {
  775. if (M_D) for (i=0;i<T2;i++) *d2++ = ConstD - *d2;
  776. else for (i=0;i<T2;i++) *l2++ = ConstL - *l2;
  777. } else {
  778. if (M_D) for (i=0;i<T2;i++) *d2++ -= ConstD;
  779. else for (i=0;i<T2;i++) *l2++ -= ConstL;
  780. }
  781. } else {
  782. if (M_D) for (i=0;i<T2;i++) *d2++ -= *d1++;
  783. else for (i=0;i<T2;i++) *l2++ -= *l1++;
  784. }
  785. break;
  786. case '*' :
  787. if (M_C) {
  788. if (M_D) for (i=0;i<T2;i++) *d2++ *= ConstD;
  789. else for (i=0;i<T2;i++) *l2++ *= ConstL;
  790. } else {
  791. if (M_D) for (i=0;i<T2;i++) *d2++ *= *d1++;
  792. else for (i=0;i<T2;i++) *l2++ *= *l1++;
  793. }
  794. break;
  795. case '/' :
  796. if (M_C) {
  797. if (M_S) {
  798. if (M_D) for (i=0;i<T2;i++) *d2++ = ConstD / *d2;
  799. else for (i=0;i<T2;i++) *l2++ = ConstL / *l2;
  800. } else {
  801. if (M_D) for (i=0;i<T2;i++) *d2++ /= ConstD;
  802. else for (i=0;i<T2;i++) *l2++ /= ConstL;
  803. }
  804. } else {
  805. if (M_D) for (i=0;i<T2;i++) *d2++ /= *d1++;
  806. else for (i=0;i<T2;i++) *l2++ /= *l1++;
  807. }
  808. break;
  809. case 'm' : /* min */
  810. if (M_C) {
  811. if (M_D) for(i=0;i<T2;i++) { if (*d2>ConstD) *d2=ConstD; d2++; }
  812. else for(i=0;i<T2;i++) { if (*l2>ConstL) *l2=ConstL; l2++; }
  813. } else {
  814. if (M_D) for (i=0;i<T2;i++) { if (*d2>*d1) *d2=*d1; d2++; d1++; }
  815. else for (i=0;i<T2;i++) { if (*l2>*l1) *l2=*l1; l2++; l1++; }
  816. }
  817. break;
  818. case 'M' : /* max */
  819. if (M_C) {
  820. if (M_D) for(i=0;i<T2;i++) { if (*d2<ConstD) *d2=ConstD; d2++; }
  821. else for(i=0;i<T2;i++) { if (*l2<ConstL) *l2=ConstL; l2++; }
  822. } else {
  823. if (M_D) for (i=0;i<T2;i++) { if (*d2<*d1) *d2=*d1; d2++; d1++; }
  824. else for (i=0;i<T2;i++) { if (*l2<*l1) *l2=*l1; l2++; l1++; }
  825. }
  826. break;
  827. case '%' : /* modulo */
  828. if (M_C) {
  829. if (M_S) {
  830. if (M_D) for (i=0;i<T2;i++) *d2++ = fmod(ConstD,*d2);
  831. else for (i=0;i<T2;i++) *l2++ = ConstL % *l2;
  832. } else {
  833. if (M_D) for (i=0;i<T2;i++) *d2++ = fmod(*d2,ConstD);
  834. else for (i=0;i<T2;i++) *l2++ %= ConstL;
  835. }
  836. } else {
  837. if (M_D) for (i=0;i<T2;i++) { *d2++ = fmod(*d2,*d1); d1++; }
  838. else for (i=0;i<T2;i++) *l2++ %= *l1++;
  839. }
  840. break;
  841. case '^' : /* puissance */
  842. if (! M_D) { /* passage en double force car + pratique */
  843. toDouble(Elt); toDouble(Elt2);
  844. if (M_C) ConstD=*d1;
  845. }
  846. if (M_C) {
  847. if (M_S) {
  848. for (i=0;i<T2;i++) *d2++ = pow(ConstD,*d2);
  849. } else {
  850. for (i=0;i<T2;i++) *d2++ = pow(*d2,ConstD);
  851. }
  852. } else {
  853. for (i=0;i<T2;i++) { *d2++ = pow(*d2,*d1); d1++; }
  854. }
  855. break;
  856. case '=' : /* test egal */
  857. if (M_C) {
  858. if (M_D) for(i=0;i<T2;i++) {
  859. if (*d2 != ConstD) {B=FALSE; break;} d2++; }
  860. else for (i=0;i<T2;i++) {
  861. if (*l2 != ConstL) {B=FALSE; break;} l2++; }
  862. } else {
  863. if (M_D) for (i=0;i<T2;i++) {
  864. if (*d2 != *d1) {B=FALSE; break;} d1++; d2++; }
  865. else for (i=0;i<T2;i++) {
  866. if (*l2 != *l1) {B=FALSE; break;} l1++; l2++; }
  867. }
  868. putBool(B);
  869. dropElt(); /* suppression des 2 !! */
  870. break;
  871. case '#' : /* test different */
  872. if (M_C) {
  873. if (M_D) for(i=0;i<T2;i++) {
  874. if (*d2 == ConstD) {B=FALSE; break;} d2++; }
  875. else for (i=0;i<T2;i++) {
  876. if (*l2 == ConstL) {B=FALSE; break;} l2++; }
  877. } else {
  878. if (M_D) for (i=0;i<T2;i++) {
  879. if (*d2 == *d1) {B=FALSE; break;} d1++; d2++; }
  880. else for (i=0;i<T2;i++) {
  881. if (*l2 == *l1) {B=FALSE; break;} l1++; l2++; }
  882. }
  883. putBool(B);
  884. dropElt(); /* suppression des 2 !! */
  885. break;
  886. case '<' : /* test inf */
  887. if (M_C) {
  888. if (M_S) {
  889. if (M_D) for(i=0;i<T2;i++) {
  890. if (*d2 < ConstD) {B=FALSE; break;} d2++; }
  891. else for (i=0;i<T2;i++) {
  892. if (*l2 < ConstL) {B=FALSE; break;} l2++; }
  893. } else {
  894. if (M_D) for(i=0;i<T2;i++) {
  895. if (*d2 >= ConstD) {B=FALSE; break;} d2++; }
  896. else for (i=0;i<T2;i++) {
  897. if (*l2 >= ConstL) {B=FALSE; break;} l2++; }
  898. }
  899. } else {
  900. if (M_D) for (i=0;i<T2;i++) {
  901. if (*d2 >= *d1) {B=FALSE; break;} d1++; d2++; }
  902. else for (i=0;i<T2;i++) {
  903. if (*l2 >= *l1) {B=FALSE; break;} l1++; l2++; }
  904. }
  905. putBool(B);
  906. dropElt(); /* suppression des 2 !! */
  907. break;
  908. case '>' : /* test sup */
  909. if (M_C) {
  910. if (M_S) {
  911. if (M_D) for(i=0;i<T2;i++) {
  912. if (*d2 > ConstD) {B=FALSE; break;} d2++; }
  913. else for (i=0;i<T2;i++) {
  914. if (*l2 > ConstL) {B=FALSE; break;} l2++; }
  915. } else {
  916. if (M_D) for(i=0;i<T2;i++) {
  917. if (*d2 <= ConstD) {B=FALSE; break;} d2++; }
  918. else for (i=0;i<T2;i++) {
  919. if (*l2 <= ConstL) {B=FALSE; break;} l2++; }
  920. }
  921. } else {
  922. if (M_D) for (i=0;i<T2;i++) {
  923. if (*d2 <= *d1) {B=FALSE; break;} d1++; d2++; }
  924. else for (i=0;i<T2;i++) {
  925. if (*l2 <= *l1) {B=FALSE; break;} l1++; l2++; }
  926. }
  927. putBool(B);
  928. dropElt(); /* suppression des 2 !! */
  929. break;
  930. case 'i' : /* test inf ou egal */
  931. if (M_C) {
  932. if (M_S) {
  933. if (M_D) for(i=0;i<T2;i++) {
  934. if (*d2 <= ConstD) {B=FALSE; break;} d2++; }
  935. else for (i=0;i<T2;i++) {
  936. if (*l2 <= ConstL) {B=FALSE; break;} l2++; }
  937. } else {
  938. if (M_D) for(i=0;i<T2;i++) {
  939. if (*d2 > ConstD) {B=FALSE; break;} d2++; }
  940. else for (i=0;i<T2;i++) {
  941. if (*l2 > ConstL) {B=FALSE; break;} l2++; }
  942. }
  943. } else {
  944. if (M_D) for (i=0;i<T2;i++) {
  945. if (*d2 > *d1) {B=FALSE; break;} d1++; d2++; }
  946. else for (i=0;i<T2;i++) {
  947. if (*l2 > *l1) {B=FALSE; break;} l1++; l2++; }
  948. }
  949. putBool(B);
  950. dropElt(); /* suppression des 2 !! */
  951. break;
  952. case 's' : /* test sup ou egal */
  953. if (M_C) {
  954. if (M_S) {
  955. if (M_D) for(i=0;i<T2;i++) {
  956. if (*d2 >= ConstD) {B=FALSE; break;} d2++; }
  957. else for (i=0;i<T2;i++) {
  958. if (*l2 >= ConstL) {B=FALSE; break;} l2++; }
  959. } else {
  960. if (M_D) for(i=0;i<T2;i++) {
  961. if (*d2 < ConstD) {B=FALSE; break;} d2++; }
  962. else for (i=0;i<T2;i++) {
  963. if (*l2 < ConstL) {B=FALSE; break;} l2++; }
  964. }
  965. } else {
  966. if (M_D) for (i=0;i<T2;i++) {
  967. if (*d2 < *d1) {B=FALSE; break;} d1++; d2++; }
  968. else for (i=0;i<T2;i++) {
  969. if (*l2 < *l1) {B=FALSE; break;} l1++; l2++; }
  970. }
  971. putBool(B);
  972. dropElt(); /* suppression des 2 !! */
  973. break;
  974. default :
  975. printf("%c : not yet implemented !\n",O);
  976. break;
  977. }
  978. dropElt(); /* suppression du 1er */
  979. }
  980. void IF_plus(void) { IF_fct_2('+'); }
  981. void IF_moins(void) { IF_fct_2('-'); }
  982. void IF_mult(void) { IF_fct_2('*'); }
  983. void IF_div(void) { IF_fct_2('/'); }
  984. void IF_min(void) { IF_fct_2('m'); }
  985. void IF_max(void) { IF_fct_2('M'); }
  986. void IF_modulo(void) { IF_fct_2('%'); }
  987. void IF_puiss(void) { IF_fct_2('^'); }
  988. void IF_neg(void)
  989. {
  990. putLong((long long)-1);
  991. IF_mult();
  992. }
  993. void IF_Legal(void) { IF_fct_2('='); }
  994. void IF_Ldiff(void) { IF_fct_2('#'); }
  995. void IF_Lsup(void) { IF_fct_2('>'); }
  996. void IF_Linf(void) { IF_fct_2('<'); }
  997. void IF_Lsupeg(void) { IF_fct_2('s'); }
  998. void IF_Linfeg(void) { IF_fct_2('i'); }
  999. void IF_fctD_1(double(*f)(double))
  1000. {
  1001. struct Num *Elt;
  1002. uint32_t n;
  1003. long long *L;
  1004. double *D;
  1005. int i;
  1006. _VERIF_STACK_
  1007. Elt = (struct Num *)StackN;
  1008. n = Elt->t&MSK_T;
  1009. if (Elt->t&MSK_R) { /* double */
  1010. D = &(Elt->d);
  1011. for(i=0;i<n;i++) *D++ = f(*D);
  1012. } else {
  1013. D = &(Elt->d);
  1014. L = &(Elt->l);
  1015. for(i=0;i<n;i++) *D++ = f((double)*L++);
  1016. Elt->t = Elt->t | MSK_R;
  1017. }
  1018. }
  1019. void IF_fctB_1(long long (*f1)(long long), double(*f2)(double))
  1020. {
  1021. struct Num *Elt;
  1022. uint32_t n;
  1023. long long *L;
  1024. double *D;
  1025. int i;
  1026. _VERIF_STACK_
  1027. Elt = (struct Num *)StackN;
  1028. n = Elt->t&MSK_T;
  1029. if (Elt->t&MSK_R) { /* double */
  1030. D = &(Elt->d);
  1031. for(i=0;i<n;i++) *D++ = f2(*D);
  1032. } else {
  1033. L = &(Elt->l);
  1034. for(i=0;i<n;i++) *L++ = f1(*L);
  1035. }
  1036. }
  1037. void IF_fctD_1L(long long(*f)(double))
  1038. {
  1039. struct Num *Elt;
  1040. uint32_t n;
  1041. long long *L;
  1042. double *D;
  1043. int i;
  1044. _VERIF_STACK_
  1045. Elt = (struct Num *)StackN;
  1046. n = Elt->t&MSK_T;
  1047. if (Elt->t&MSK_R) { /* double */
  1048. D = &(Elt->d);
  1049. L = &(Elt->l);
  1050. for(i=0;i<n;i++) *L++ = f(*D++);
  1051. Elt->t = Elt->t & ~MSK_R; /* change type */
  1052. }
  1053. /* rien si long */
  1054. }
  1055. void IF_fctD_1LB(long long(*f)(double))
  1056. {
  1057. struct Num *Elt;
  1058. uint32_t n;
  1059. long long *L;
  1060. double *D;
  1061. int i;
  1062. _VERIF_STACK_
  1063. Elt = (struct Num *)StackN;
  1064. n = Elt->t&MSK_T;
  1065. D = &(Elt->d);
  1066. L = &(Elt->l);
  1067. if (Elt->t&MSK_R) { /* double */
  1068. for(i=0;i<n;i++) *L++ = f(*D++);
  1069. Elt->t = Elt->t & ~MSK_R; /* change type */
  1070. } else {
  1071. for(i=0;i<n;i++) *L++ = f(*L);
  1072. }
  1073. }
  1074. void IF_inFile_1d(FILE * fd, char delim, int virg)
  1075. {
  1076. struct Num *Elt;
  1077. uint32_t n;
  1078. long long *L;
  1079. double *D;
  1080. int i;
  1081. char buf[40], *pt;
  1082. _VERIF_STACK_
  1083. Elt = (struct Num *)StackN;
  1084. n = Elt->t&MSK_T;
  1085. if (Elt->t&MSK_R) { /* double */
  1086. D = &(Elt->d);
  1087. for(i=0;i<n;i++) {
  1088. sprintf(buf,"%.15f%c",*D++,delim);
  1089. pt = buf + strlen(buf) - 2;
  1090. while (*pt == '0') pt--;
  1091. pt++;
  1092. *pt++ = delim;
  1093. *pt='\0';
  1094. if (virg)
  1095. if ((pt = strchr(buf, (int)'.')) != NULL) *pt = ',';
  1096. fprintf(fd,buf);
  1097. }
  1098. } else {
  1099. L = &(Elt->l);
  1100. for(i=0;i<n;i++) fprintf(fd,"%lld%c",*L++,delim);
  1101. }
  1102. fprintf(fd,"\n");
  1103. dropElt(); /* suppression */
  1104. }
  1105. void IF_inFile_1(FILE * fd)
  1106. {
  1107. struct Num *Elt;
  1108. uint32_t n;
  1109. long long *L;
  1110. double *D;
  1111. int i;
  1112. _VERIF_STACK_
  1113. Elt = (struct Num *)StackN;
  1114. n = Elt->t&MSK_T;
  1115. if (Elt->t&MSK_R) { /* double */
  1116. D = &(Elt->d);
  1117. for(i=0;i<n;i++) fprintf(fd,"%d %g\n",i,*D++);
  1118. } else {
  1119. L = &(Elt->l);
  1120. for(i=0;i<n;i++) fprintf(fd,"%d %lld\n",i,*L++);
  1121. }
  1122. }
  1123. void IF_inFile_2(FILE * fd)
  1124. {
  1125. struct Num *Elt, *Elt2;
  1126. uint32_t n, n2;
  1127. long long *L, *L2;
  1128. double *D, *D2;
  1129. int i;
  1130. _VERIF_STACK_
  1131. Elt = (struct Num *)StackN;
  1132. n = Elt->t&MSK_T;
  1133. Elt2 = Elt->n;
  1134. n2 = Elt2->t&MSK_T;
  1135. if (n>n2) n=n2;
  1136. if (Elt->t&MSK_R) { /* double */
  1137. D = &(Elt->d);
  1138. if (Elt2->t&MSK_R) { /* double */
  1139. D2 = &(Elt2->d);
  1140. for(i=0;i<n;i++) fprintf(fd,"%g %g\n",*D2++,*D++);
  1141. } else {
  1142. L2 = &(Elt2->l);
  1143. for(i=0;i<n;i++) fprintf(fd,"%lld %g\n",*L2++,*D++);
  1144. }
  1145. } else {
  1146. L = &(Elt->l);
  1147. if (Elt2->t&MSK_R) { /* double */
  1148. D2 = &(Elt2->d);
  1149. for(i=0;i<n;i++) fprintf(fd,"%g %lld\n",*D2++,*L++);
  1150. } else {
  1151. L2 = &(Elt2->l);
  1152. for(i=0;i<n;i++) fprintf(fd,"%lld %lld\n",*L2++,*L++);
  1153. }
  1154. }
  1155. }
  1156. static int dimTab(void * A)
  1157. {
  1158. struct Num *N;
  1159. uint32_t l;
  1160. if (A==VIDE) return 0L;
  1161. N = (struct Num*)A;
  1162. l = N->t&MSK_T;
  1163. return (int)l;
  1164. }
  1165. static long transLongTo(long long * C)
  1166. {
  1167. struct Num *Elt;
  1168. int i;
  1169. uint32_t l;
  1170. long long *L;
  1171. double *D;
  1172. Elt = (struct Num *)StackN;
  1173. l = Elt->t&MSK_T;
  1174. L = &(Elt->l)+l-1;
  1175. D = &(Elt->d)+l-1;
  1176. if (Elt->t & MSK_R)
  1177. for (i=0;i<l;i++) *C--=(long long)*D--;
  1178. else
  1179. for (i=0;i<l;i++) *C--=*L--;
  1180. dropElt();
  1181. return(l);
  1182. }
  1183. static long transDoubTo(double * C)
  1184. {
  1185. struct Num *Elt;
  1186. int i;
  1187. uint32_t l;
  1188. long long *L;
  1189. double *D;
  1190. Elt = (struct Num *)StackN;
  1191. l = Elt->t&MSK_T;
  1192. L = &(Elt->l)+l-1;
  1193. D = &(Elt->d)+l-1;
  1194. if (Elt->t & MSK_R)
  1195. for (i=0;i<l;i++) *C--=*D--;
  1196. else
  1197. for (i=0;i<l;i++) *C--=(double)*L--;
  1198. dropElt();
  1199. return(l);
  1200. }
  1201. void IF_toArray( void )
  1202. {
  1203. uint32_t n, i, l, T, t;
  1204. long long *L;
  1205. double *D;
  1206. void * M;
  1207. struct Num *N;
  1208. _VERIF_STACK_
  1209. if (!isScalar()) {
  1210. messErr(36); return;
  1211. }
  1212. n = (uint32_t)getVal();
  1213. dropElt();
  1214. if (n>1) {
  1215. i = nbSizeTypeOnStack(n,&t);
  1216. if (i<n) messErr(4);
  1217. else {
  1218. T = t & MSK_T;
  1219. if ((M = malloc(sizeof(struct Num)+((T-1)*(sizeof(double))))) == NULL)
  1220. stopErr("IF_toArray","malloc");
  1221. N = (struct Num*)M;
  1222. N->t = t;
  1223. if (t & MSK_R) {
  1224. D = &(N->d)+(T-1);
  1225. for(i=0;i<n;i++) { l=transDoubTo(D); D -= l; }
  1226. } else {
  1227. L = &(N->l)+(T-1);
  1228. for(i=0;i<n;i++) { l=transLongTo(L); L -= l; }
  1229. }
  1230. /* on the stack */
  1231. N->n = StackN;
  1232. _MODIF_STACKN_(M);
  1233. }
  1234. } else messErr(30);
  1235. }
  1236. static void toScalar( int s )
  1237. {
  1238. struct Num *Elt;
  1239. uint32_t l;
  1240. int i;
  1241. double *D;
  1242. long long * L;
  1243. _VERIF_STACK_
  1244. Elt = (struct Num *)StackN;
  1245. l = Elt->t&MSK_T;
  1246. if (l==1) return;
  1247. _MODIF_STACKN_(Elt->n); /* depile */
  1248. if (s) {
  1249. L = &(Elt->l);
  1250. D = &(Elt->d);
  1251. if (Elt->t & MSK_R)
  1252. for(i=0;i<l;i++) putDouble(*D++);
  1253. else
  1254. for(i=0;i<l;i++) putLong(*L++);
  1255. } else {
  1256. L = &(Elt->l)+l-1;
  1257. D = &(Elt->d)+l-1;
  1258. if (Elt->t & MSK_R)
  1259. for(i=0;i<l;i++) putDouble(*D--);
  1260. else
  1261. for(i=0;i<l;i++) putLong(*L--);
  1262. }
  1263. if (!(Elt->t&MSK_V)) free((void*)Elt);
  1264. }
  1265. void IF_toScalar( void )
  1266. {
  1267. toScalar(1);
  1268. }
  1269. void IF_toScalarR( void )
  1270. {
  1271. toScalar(0);
  1272. }
  1273. static void tabShift(void **A,long s)
  1274. {
  1275. struct Num *Elt, *NElt;
  1276. void * M;
  1277. long j, k;
  1278. uint32_t l;
  1279. long long *L, *NL;
  1280. _VERIF_STACK_
  1281. Elt = (struct Num *)*A;
  1282. l = Elt->t&MSK_T;
  1283. if (l==1) return;
  1284. if (s>0)
  1285. while (s>=l) s-=l;
  1286. else while (-s>=l) s+=l;
  1287. if (s==0) return;
  1288. if (s>0) j=s;
  1289. else j=l+s;
  1290. k = sizeof(struct Num)+((l-1)*(sizeof(double)));
  1291. if ((M = malloc(k)) == NULL) stopErr("tabShift","malloc");
  1292. NElt = (struct Num *)M;
  1293. *A = M;
  1294. NElt->t = Elt->t;
  1295. NElt->n = Elt->n;
  1296. L = &(Elt->l);
  1297. NL = &(NElt->l);
  1298. k=l-j;
  1299. bcopy((void*)&L[0],(void*)&NL[j],k*sizeof(long long));
  1300. bcopy((void*)&L[k],(void*)&NL[0],j*sizeof(long long));
  1301. if (!(Elt->t&MSK_V)) free((void*)Elt);
  1302. }
  1303. void IF_TShiftR( void )
  1304. {
  1305. tabShift(_ADDR_STACKN_,1);
  1306. }
  1307. void IF_TShiftL( void )
  1308. {
  1309. tabShift(_ADDR_STACKN_,-1);
  1310. }
  1311. static void nTabShift( int v )
  1312. {
  1313. void ** ANext;
  1314. struct Num * N;
  1315. long i=0, n;
  1316. _VERIF_STACK_
  1317. if (!isScalar()) {
  1318. messErr(36); return;
  1319. }
  1320. n = (long)getVal();
  1321. dropElt();
  1322. if (n>0) {
  1323. ANext = _ADDR_STACKN_;
  1324. while (*ANext != VIDE) {
  1325. tabShift(ANext,v);
  1326. N = (struct Num*) *ANext;
  1327. ANext = &(N->n);
  1328. i++;
  1329. if (i==n) break;
  1330. }
  1331. } else messErr(29);
  1332. }
  1333. void IF_NTShiftR( void )
  1334. {
  1335. nTabShift(1);
  1336. }
  1337. void IF_NTShiftL( void )
  1338. {
  1339. nTabShift(-1);
  1340. }
  1341. void IF_TNShiftR( void )
  1342. {
  1343. long n;
  1344. _VERIF_STACK_
  1345. if (!isScalar()) {
  1346. messErr(36); return;
  1347. }
  1348. n = (long)getVal();
  1349. dropElt();
  1350. tabShift(_ADDR_STACKN_,n);
  1351. }
  1352. void IF_TNShiftL( void )
  1353. {
  1354. long n;
  1355. _VERIF_STACK_
  1356. if (!isScalar()) {
  1357. messErr(36); return;
  1358. }
  1359. n = (long)getVal();
  1360. dropElt();
  1361. tabShift(_ADDR_STACKN_,-n);
  1362. }
  1363. static void nTNShift( int s )
  1364. {
  1365. long n;
  1366. _VERIF_STACK_
  1367. if (!isScalar()) {
  1368. messErr(36); return;
  1369. }
  1370. n = (long)getVal();
  1371. dropElt();
  1372. nTabShift(n*s);
  1373. }
  1374. void IF_NTNShiftR( void )
  1375. {
  1376. nTNShift(1);
  1377. }
  1378. void IF_NTNShiftL( void )
  1379. {
  1380. nTNShift(-1);
  1381. }
  1382. static void subTab(void **pA , long n, char r) /* r=1 : right else left */
  1383. {
  1384. struct Num *Elt, *Elt2;
  1385. uint32_t l;
  1386. long k;
  1387. long long *L, *Lf;
  1388. void *A, * M;
  1389. double *D, *Df;
  1390. _VERIF_STACK_
  1391. A = *pA;
  1392. Elt = (struct Num *)A;
  1393. l = Elt->t&MSK_T;
  1394. if (l==n) return;
  1395. k = sizeof(struct Num)+((n-1)*(sizeof(long long)));
  1396. if ((M = malloc(k)) == NULL) stopErr("subTab","malloc");
  1397. Elt2 = (struct Num *)M;
  1398. Elt2->n = Elt->n;
  1399. Elt2->t = (Elt->t&MSK_R) | n;
  1400. *pA = M;
  1401. if (n<l) {
  1402. L = &(Elt->l);
  1403. if (r) L += (l-n);
  1404. bcopy((void*)L,(void*)&(Elt2->l),n*sizeof(long long));
  1405. } else { /* fill with zero */
  1406. L = &(Elt2->l);
  1407. if (!r) L += (n-l);
  1408. bcopy((void*)&(Elt->l),(void*)L,l*sizeof(long long));
  1409. if (Elt->t&MSK_R) {
  1410. D = &(Elt2->d);
  1411. if (r) D += l;
  1412. Df = D + (n-l);
  1413. while (D < Df) *D++ = (double)0;
  1414. } else {
  1415. L = &(Elt2->l);
  1416. if (r) L += l;
  1417. Lf = L + (n-l);
  1418. while (L < Lf) *L++ = 0L;
  1419. }
  1420. }
  1421. if (!(Elt->t&MSK_V)) free(A);
  1422. }
  1423. void IF_subTab(void)
  1424. {
  1425. long n;
  1426. _VERIF_STACK_
  1427. if (!isScalar()) {
  1428. messErr(36); return;
  1429. }
  1430. n = (long)getVal();
  1431. dropElt();
  1432. if (n>0) subTab(_ADDR_STACKN_, n, 0);
  1433. else messErr(29);
  1434. }
  1435. void IF_subTabR(void)
  1436. {
  1437. long n;
  1438. _VERIF_STACK_
  1439. if (!isScalar()) {
  1440. messErr(36); return;
  1441. }
  1442. n = (long)getVal();
  1443. dropElt();
  1444. if (n>0) subTab(_ADDR_STACKN_, n, (char)1);
  1445. else messErr(29);
  1446. }
  1447. static void NSubTab( char r )
  1448. {
  1449. void **pNext;
  1450. struct Num * N;
  1451. long i=0, n, l;
  1452. _VERIF_STACK_
  1453. if (!isScalar()) {
  1454. messErr(36); return;
  1455. }
  1456. l = (long)getVal();
  1457. dropElt();
  1458. if (l>0) {
  1459. if (!isScalar()) {
  1460. messErr(36); return;
  1461. }
  1462. n = (long)getVal();
  1463. dropElt();
  1464. if (n>0) {
  1465. pNext = _ADDR_STACKN_;
  1466. while (*pNext != VIDE) {
  1467. subTab(pNext,l,r);
  1468. N = (struct Num*) *pNext;
  1469. pNext = &(N->n);
  1470. i++;
  1471. if (i==n) break;
  1472. }
  1473. } else messErr(29);
  1474. } else messErr(29);
  1475. }
  1476. void IF_NsubTab(void)
  1477. {
  1478. NSubTab((char)0);
  1479. }
  1480. void IF_NsubTabR(void)
  1481. {
  1482. NSubTab((char)1);
  1483. }
  1484. static void tabRev( void* A )
  1485. {
  1486. struct Num *Elt;
  1487. uint32_t l;
  1488. double *D, *FD, vD;
  1489. long long * L, *FL, vL;
  1490. _VERIF_STACK_
  1491. Elt = (struct Num *)A;
  1492. l = Elt->t&MSK_T;
  1493. if (l==1) return;
  1494. if (Elt->t & MSK_R) {
  1495. D = &(Elt->d);
  1496. FD = D+l-1;
  1497. while (D<FD) {
  1498. vD=*D; *D=*FD; *FD=vD;
  1499. D++; FD--;
  1500. }
  1501. } else {
  1502. L = &(Elt->l);
  1503. FL=L+l-1;
  1504. while (L<FL) {
  1505. vL=*L; *L=*FL; *FL=vL;
  1506. L++; FL--;
  1507. }
  1508. }
  1509. }
  1510. void IF_TabRev( void )
  1511. {
  1512. tabRev(StackN);
  1513. }
  1514. void IF_NTabRev( void )
  1515. {
  1516. void * Next;
  1517. struct Num * N;
  1518. long i=0, n;
  1519. _VERIF_STACK_
  1520. if (!isScalar()) {
  1521. messErr(36); return;
  1522. }
  1523. n = (long)getVal();
  1524. dropElt();
  1525. if (n>0) {
  1526. Next = StackN;
  1527. while (Next != VIDE) {
  1528. N = (struct Num*) Next;
  1529. tabRev(Next);
  1530. Next = N->n;
  1531. i++;
  1532. if (i==n) break;
  1533. }
  1534. } else messErr(29);
  1535. }
  1536. static void tabTransp (int sens)
  1537. {
  1538. void * Next, *Next2, **Suiv, *SNext, **Last;
  1539. struct Num * N, *N2;
  1540. long i=0, j, n;
  1541. uint32_t l;
  1542. short Doub=0;
  1543. double *D, *D2;
  1544. long long *L, *L2;
  1545. _VERIF_STACK_
  1546. if (!isScalar()) {
  1547. messErr(36); return;
  1548. }
  1549. n = (long)getVal();
  1550. dropElt();
  1551. if (n>0) {
  1552. if (n==1) {
  1553. if (sens) toScalar(1);
  1554. else toScalar(0);
  1555. return;
  1556. }
  1557. /* the n elts on stack must have the same dim */
  1558. Next = StackN;
  1559. while (Next != VIDE) {
  1560. N = (struct Num*) Next;
  1561. if (i) {
  1562. if (l != (N->t&MSK_T)) break;
  1563. } else l = N->t&MSK_T;
  1564. if (N->t&MSK_R) Doub=1;
  1565. i++;
  1566. if (i==n) break;
  1567. Next = N->n;
  1568. }
  1569. if (i!=n) {
  1570. if (Next == VIDE) messErr(4);
  1571. else messErr(3);
  1572. } else {
  1573. /* make l elts of dim n */
  1574. Suiv = &Next2;
  1575. for (i=0;i<l;i++) {
  1576. if ((*Suiv=malloc(sizeof(struct Num)+((n-1)*(sizeof(double)))))==NULL)
  1577. stopErr("tabTransp","malloc");
  1578. N2 = (struct Num*) *Suiv;
  1579. N2->t = n;
  1580. if (Doub) N2->t |= MSK_R;
  1581. /* remplissage */
  1582. if (sens) {
  1583. j=0;
  1584. if (sens==1) {
  1585. N2->n = SNext;
  1586. SNext = Next2;
  1587. if (i==0) Last = &(N2->n);
  1588. } else
  1589. Suiv = &N2->n;
  1590. } else {
  1591. j=n-1;
  1592. Suiv = &N2->n;
  1593. }
  1594. if (Doub) {
  1595. D2 = &(N2->d);
  1596. } else {
  1597. L2 = &(N2->l);
  1598. }
  1599. Next = StackN;
  1600. while (Next != VIDE) {
  1601. N = (struct Num*) Next;
  1602. if (Doub) {
  1603. if (N->t&MSK_R) {
  1604. D = &(N->d);
  1605. if (sens) D+=(l-i-1);
  1606. else D+=i;
  1607. *(D2+j) = *D;
  1608. } else {
  1609. L = &(N->l);
  1610. if (sens) L+=(l-i-1);
  1611. else L+=i;
  1612. *(D2+j) = (double)*L;
  1613. }
  1614. } else {
  1615. L = &(N->l);
  1616. if (sens) L+=(l-i-1);
  1617. else L+=i;
  1618. *(L2+j) = *L;
  1619. /* printf("%ld ",*L); */
  1620. }
  1621. if (sens) {
  1622. j++;
  1623. if (j>=n) break;
  1624. } else {
  1625. j--;
  1626. if (j<0) break;
  1627. }
  1628. Next = N->n;
  1629. }
  1630. /* printf("\n"); */
  1631. }
  1632. if (sens!=1) Last = &(N2->n);
  1633. /* drop n elts */
  1634. for (i=0;i<n;i++) dropElt();
  1635. /* put new elts on the stack */
  1636. *Last = StackN;
  1637. _MODIF_STACKN_(Next2);
  1638. }
  1639. } else messErr(29);
  1640. }
  1641. void IF_TabTransp (void) /* crot */
  1642. {
  1643. tabTransp(0);
  1644. }
  1645. void IF_TabTranspN (void) /* transpose */
  1646. {
  1647. tabTransp(1);
  1648. }
  1649. void IF_TabTranspT (void) /* trot */
  1650. {
  1651. tabTransp(-1);
  1652. }
  1653. int is1Tab(void)
  1654. {
  1655. if (dimTab(StackN) < 2L) return 0;
  1656. return 1;
  1657. }
  1658. int is2Tab(void)
  1659. {
  1660. struct Num *N;
  1661. if (dimTab(StackN) < 2L) return 0;
  1662. N = (struct Num*)StackN;
  1663. if (dimTab(N->n) < 2L) return 0;
  1664. return 1;
  1665. }
  1666. int isNTabSameDim(int n)
  1667. {
  1668. void * Next;
  1669. struct Num * N;
  1670. long i=0;
  1671. uint32_t l;
  1672. /* the n elts on stack must have the same dim */
  1673. Next = StackN;
  1674. while (Next != VIDE) {
  1675. N = (struct Num*) Next;
  1676. if (i) {
  1677. if (l != (long)(N->t&MSK_T)) break;
  1678. } else l = N->t&MSK_T;
  1679. i++;
  1680. if (i==n) return 1; /* OK */
  1681. Next = N->n;
  1682. }
  1683. return 0;
  1684. }
  1685. /* []functions ****/
  1686. static void TAB_Fct(char C)
  1687. {
  1688. struct Num *Elt;
  1689. uint32_t n;
  1690. long long *L, L1, L2;
  1691. double *D, D1, D2;
  1692. int i;
  1693. _VERIF_STACK_
  1694. Elt = (struct Num *)StackN;
  1695. n = Elt->t&MSK_T;
  1696. if (n>1) {
  1697. if (Elt->t&MSK_R) { /* double */
  1698. switch(C) {
  1699. case 'm' :
  1700. D1 = Elt->d;
  1701. D = &(Elt->d)+1;
  1702. for(i=1;i<n;i++) { if (*D<D1) D1=*D; D++; }
  1703. putDouble(D1);
  1704. break;
  1705. case 'M' :
  1706. D1 = Elt->d;
  1707. D = &(Elt->d)+1;
  1708. for(i=1;i<n;i++) { if (*D>D1) D1=*D; D++; }
  1709. putDouble(D1);
  1710. break;
  1711. case '+' :
  1712. D1 = Elt->d;
  1713. D = &(Elt->d)+1;
  1714. for(i=1;i<n;i++) D1 += *D++;
  1715. putDouble(D1);
  1716. break;
  1717. case '*' :
  1718. D1 = Elt->d;
  1719. D = &(Elt->d)+1;
  1720. for(i=1;i<n;i++) D1 *= *D++;
  1721. putDouble(D1);
  1722. break;
  1723. default : /* E = extrems */
  1724. D1 = D2 = Elt->d;
  1725. D = &(Elt->d)+1;
  1726. for(i=1;i<n;i++) {
  1727. if (*D>D2) D2=*D;
  1728. if (*D<D1) D1=*D; D++;
  1729. }
  1730. putDouble(D1);
  1731. IF_swap();
  1732. putDouble(D2);
  1733. break;
  1734. }
  1735. } else {
  1736. switch(C) {
  1737. case 'm' :
  1738. L1 = Elt->l;
  1739. L = &(Elt->l)+1;
  1740. for(i=1;i<n;i++) { if (*L<L1) L1=*L; L++; }
  1741. putLong(L1);
  1742. break;
  1743. case 'M' :
  1744. L1 = Elt->l;
  1745. L = &(Elt->l)+1;
  1746. for(i=1;i<n;i++) { if (*L>L1) L1=*L; L++; }
  1747. putLong(L1);
  1748. break;
  1749. case '+' :
  1750. L1 = Elt->l;
  1751. L = &(Elt->l)+1;
  1752. for(i=1;i<n;i++) L1 += *L++;
  1753. putLong(L1);
  1754. break;
  1755. case '*' :
  1756. L1 = Elt->l;
  1757. L = &(Elt->l)+1;
  1758. for(i=1;i<n;i++) L1 *= *L++;
  1759. putLong(L1);
  1760. break;
  1761. default : /* E = extrems */
  1762. L1 = L2 = Elt->l;
  1763. L = &(Elt->l)+1;
  1764. for(i=1;i<n;i++) {
  1765. if (*L>L2) L2=*L;
  1766. if (*L<L1) L1=*L; L++;
  1767. }
  1768. putLong(L1);
  1769. IF_swap();
  1770. putLong(L2);
  1771. break;
  1772. }
  1773. }
  1774. IF_swap();
  1775. dropElt();
  1776. } else messErr(12);
  1777. }
  1778. void IF_TABMin(void) { TAB_Fct('m'); }
  1779. void IF_TABMax(void) { TAB_Fct('M'); }
  1780. void IF_TABProd(void) { TAB_Fct('*'); }
  1781. void IF_TABSum(void) { TAB_Fct('+'); }
  1782. void IF_TABMinMax(void) { TAB_Fct('E'); }