62 extern LONG numwrites;
65 extern LONG nummallocs;
75 char *toterms[] = {
" ",
" >>",
"-->" };
94 LONG millitime, y = 0x7FFFFFFFL >> 1;
98 if ( AT.SS == AT.S0 && AC.StatsFlag ) {
100 if ( AC.ThreadStats == 0 && identity > 0 )
return;
101 #elif defined(WITHMPI) 102 if ( AC.OldParallelStats )
return;
103 if ( ! AC.ProcessStats && PF.me != MASTER )
return;
105 if ( Expressions == 0 )
return;
109 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
111 AR.ShortSortCount = 0;
114 MLOCK(ErrorMessageLock);
115 if ( AC.ShortStats ) {}
118 if ( identity > 0 ) {
119 MesPrint(
" Thread %d reporting",identity);
124 #elif defined(WITHMPI) 125 if ( PF.me != MASTER ) {
126 MesPrint(
" Process %d reporting",PF.me);
135 millitime = TimeCPU(1);
136 timepart = (WORD)(millitime%1000);
139 if ( AC.ShortStats ) {
140 #if defined(WITHPTHREADS) || defined(WITHMPI) 142 if ( identity > 0 ) {
144 if ( PF.me != MASTER ) {
145 const int identity = PF.me;
147 if ( par == 0 || par == 2 ) {
148 SETBASEPOSITION(pp,y);
149 if ( ISLESSPOS(*plspace,pp) ) {
150 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%10p %s %s",identity,
151 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
152 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
161 SETBASEPOSITION(pp,y);
163 if ( ISLESSPOS(*plspace,pp) ) {
164 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%11p %s %s",identity,
165 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
166 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
170 if ( ISLESSPOS(*plspace,pp) ) {
171 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%12p %s %s",identity,
172 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
173 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
177 if ( ISLESSPOS(*plspace,pp) ) {
178 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%13p %s %s",identity,
179 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
180 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
184 if ( ISLESSPOS(*plspace,pp) ) {
185 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%14p %s %s",identity,
186 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
187 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
191 if ( ISLESSPOS(*plspace,pp) ) {
192 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%15p %s %s",identity,
193 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
194 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
198 if ( ISLESSPOS(*plspace,pp) ) {
199 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%16p %s %s",identity,
200 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
201 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
205 if ( ISLESSPOS(*plspace,pp) ) {
206 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%17p %s %s",identity,
207 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
208 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
214 else if ( par == 1 ) {
215 SETBASEPOSITION(pp,y);
216 if ( ISLESSPOS(*plspace,pp) ) {
217 MesPrint(
"%d: %7l.%2is %10l:%10p",identity,millitime,timepart,
218 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
222 SETBASEPOSITION(pp,y);
224 if ( ISLESSPOS(*plspace,pp) ) {
225 MesPrint(
"%d: %7l.%2is %10l:%11p",identity,millitime,timepart,
226 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
230 if ( ISLESSPOS(*plspace,pp) ) {
231 MesPrint(
"%d: %7l.%2is %10l:%12p",identity,millitime,timepart,
232 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
236 if ( ISLESSPOS(*plspace,pp) ) {
237 MesPrint(
"%d: %7l.%2is %10l:%13p",identity,millitime,timepart,
238 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
242 if ( ISLESSPOS(*plspace,pp) ) {
243 MesPrint(
"%d: %7l.%2is %10l:%14p",identity,millitime,timepart,
244 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
248 if ( ISLESSPOS(*plspace,pp) ) {
249 MesPrint(
"%d: %7l.%2is %10l:%15p",identity,millitime,timepart,
250 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
254 if ( ISLESSPOS(*plspace,pp) ) {
255 MesPrint(
"%d: %7l.%2is %10l:%16p",identity,millitime,timepart,
256 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
260 if ( ISLESSPOS(*plspace,pp) ) {
261 MesPrint(
"%d: %7l.%2is %10l:%17p",identity,millitime,timepart,
262 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
270 if ( par == 0 || par == 2 ) {
271 SETBASEPOSITION(pp,y);
272 if ( ISLESSPOS(*plspace,pp) ) {
273 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%10p %s %s",
274 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
275 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
284 SETBASEPOSITION(pp,y);
286 if ( ISLESSPOS(*plspace,pp) ) {
287 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%11p %s %s",
288 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
289 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
293 if ( ISLESSPOS(*plspace,pp) ) {
294 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%12p %s %s",
295 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
296 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
300 if ( ISLESSPOS(*plspace,pp) ) {
301 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%13p %s %s",
302 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
303 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
307 if ( ISLESSPOS(*plspace,pp) ) {
308 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%14p %s %s",
309 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
310 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
314 if ( ISLESSPOS(*plspace,pp) ) {
315 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%15p %s %s",
316 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
317 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
321 if ( ISLESSPOS(*plspace,pp) ) {
322 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%16p %s %s",
323 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
324 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
328 if ( ISLESSPOS(*plspace,pp) ) {
329 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%17p %s %s",
330 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
331 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
337 else if ( par == 1 ) {
338 SETBASEPOSITION(pp,y);
339 if ( ISLESSPOS(*plspace,pp) ) {
340 MesPrint(
"%7l.%2is %10l:%10p",millitime,timepart,
341 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
345 SETBASEPOSITION(pp,y);
347 if ( ISLESSPOS(*plspace,pp) ) {
348 MesPrint(
"%7l.%2is %10l:%11p",millitime,timepart,
349 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
353 if ( ISLESSPOS(*plspace,pp) ) {
354 MesPrint(
"%7l.%2is %10l:%12p",millitime,timepart,
355 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
359 if ( ISLESSPOS(*plspace,pp) ) {
360 MesPrint(
"%7l.%2is %10l:%13p",millitime,timepart,
361 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
365 if ( ISLESSPOS(*plspace,pp) ) {
366 MesPrint(
"%7l.%2is %10l:%14p",millitime,timepart,
367 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
371 if ( ISLESSPOS(*plspace,pp) ) {
372 MesPrint(
"%7l.%2is %10l:%15p",millitime,timepart,
373 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
377 if ( ISLESSPOS(*plspace,pp) ) {
378 MesPrint(
"%7l.%2is %10l:%16p",millitime,timepart,
379 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
383 if ( ISLESSPOS(*plspace,pp) ) {
384 MesPrint(
"%7l.%2is %10l:%17p",millitime,timepart,
385 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
394 MesPrint(
"Time = %7l.%2i sec",millitime,timepart);
397 #if ( BITSINLONG > 32 ) 398 if ( S->GenTerms >= 10000000000L ) {
399 MesPrint(
"Time = %7l.%2i sec Generated terms = %16l",
400 millitime,timepart,S->GenTerms);
403 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
404 millitime,timepart,S->GenTerms);
407 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
408 millitime,timepart,S->GenTerms);
411 #if ( BITSINLONG > 32 ) 413 if ( S->TermsLeft >= 10000000000L ) {
414 MesPrint(
"%16s%8l Terms %s = %16l",EXPRNAME(AR.CurExpr),
415 AN.ninterms,FG.swmes[par],S->TermsLeft);
418 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
419 AN.ninterms,FG.swmes[par],S->TermsLeft);
422 if ( S->TermsLeft >= 10000000000L ) {
424 if ( identity > 0 && par == 2 ) {
425 MesPrint(
"%16s Terms in thread = %16l",
426 EXPRNAME(AR.CurExpr),S->TermsLeft);
429 #elif defined(WITHMPI) 430 if ( PF.me != MASTER && par == 2 ) {
431 MesPrint(
"%16s Terms in process= %16l",
432 EXPRNAME(AR.CurExpr),S->TermsLeft);
437 MesPrint(
"%16s Terms %s = %16l",
438 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
443 if ( identity > 0 && par == 2 ) {
444 MesPrint(
"%16s Terms in thread = %10l",
445 EXPRNAME(AR.CurExpr),S->TermsLeft);
448 #elif defined(WITHMPI) 449 if ( PF.me != MASTER && par == 2 ) {
450 MesPrint(
"%16s Terms in process= %10l",
451 EXPRNAME(AR.CurExpr),S->TermsLeft);
456 MesPrint(
"%16s Terms %s = %10l",
457 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
463 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
464 AN.ninterms,FG.swmes[par],S->TermsLeft);
467 if ( identity > 0 && par == 2 ) {
468 MesPrint(
"%16s Terms in thread = %10l",
469 EXPRNAME(AR.CurExpr),S->TermsLeft);
472 #elif defined(WITHMPI) 473 if ( PF.me != MASTER && par == 2 ) {
474 MesPrint(
"%16s Terms in process= %10l",
475 EXPRNAME(AR.CurExpr),S->TermsLeft);
480 MesPrint(
"%16s Terms %s = %10l",
481 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
485 SETBASEPOSITION(pp,y);
486 if ( ISLESSPOS(*plspace,pp) ) {
487 MesPrint(
"%24s Bytes used = %10p",AC.Commercial,plspace);
491 SETBASEPOSITION(pp,y);
493 if ( ISLESSPOS(*plspace,pp) ) {
494 MesPrint(
"%24s Bytes used =%11p",AC.Commercial,plspace);
498 if ( ISLESSPOS(*plspace,pp) ) {
499 MesPrint(
"%24s Bytes used =%12p",AC.Commercial,plspace);
503 if ( ISLESSPOS(*plspace,pp) ) {
504 MesPrint(
"%24s Bytes used =%13p",AC.Commercial,plspace);
508 if ( ISLESSPOS(*plspace,pp) ) {
509 MesPrint(
"%24s Bytes used =%14p",AC.Commercial,plspace);
513 if ( ISLESSPOS(*plspace,pp) ) {
514 MesPrint(
"%24s Bytes used =%15p",AC.Commercial,plspace);
518 if ( ISLESSPOS(*plspace,pp) ) {
519 MesPrint(
"%24s Bytes used =%16p",AC.Commercial,plspace);
523 if ( ISLESSPOS(*plspace,pp) ) {
524 MesPrint(
"%24s Bytes used=%17p",AC.Commercial,plspace);
530 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l" 531 ,numwrites,numreads,numseeks);
532 MesPrint(
"Total number of mallocs: %l, frees: %l" 533 ,nummallocs,numfrees);
535 MUNLOCK(ErrorMessageLock);
558 if ( AN.SoScratC == 0 )
559 AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*
sizeof(UWORD),
"NewSort");
561 if ( AR.sLevel >= AN.NumFunSorts ) {
562 if ( AN.NumFunSorts == 0 ) newsize = 100;
563 else newsize = 2*AN.NumFunSorts;
564 newFS = (
SORTING **)Malloc1((newsize+1)*
sizeof(
SORTING *),
"FunSort pointers");
565 for ( i = 0; i < AN.NumFunSorts; i++ ) newFS[i] = AN.FunSorts[i];
566 for ( ; i <= newsize; i++ ) newFS[i] = 0;
567 if ( AN.FunSorts ) M_free(AN.FunSorts,
"FunSort pointers");
568 AN.FunSorts = newFS; AN.NumFunSorts = newsize;
570 if ( AR.sLevel == 0 ) {
571 AN.FunSorts[0] = AT.S0;
572 AT.S0->PolyFlag = ( AR.PolyFun != 0 ) ? AR.PolyFunType: 0;
573 AR.ShortSortCount = 0;
576 if ( AN.FunSorts[AR.sLevel] == 0 ) {
577 AN.FunSorts[AR.sLevel] = AllocSort(
578 AM.SLargeSize,AM.SSmallSize,AM.SSmallEsize,AM.STermsInSmall
579 ,AM.SMaxPatches,AM.SMaxFpatches,AM.SIOsize);
582 AT.SS = S = AN.FunSorts[AR.sLevel];
583 S->sFill = S->sBuffer;
584 S->lFill = S->lBuffer;
587 S->GenTerms = S->TermsLeft = S->GenSpace = S->SpaceLeft = 0;
588 S->PoinFill = S->sPointer;
589 *S->PoinFill = S->sFill;
590 PUTZERO(S->SizeInFile[0]); PUTZERO(S->SizeInFile[1]); PUTZERO(S->SizeInFile[2]);
592 PUTZERO(S->file.POposition);
594 if ( AR.sLevel > AN.MaxFunSorts ) AN.MaxFunSorts = AR.sLevel;
636 WORD j, **ss, *to, *t;
637 LONG sSpace, over, tover, spare, retval = 0, jj;
640 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
642 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
645 oldoutfile = AR.outfile;
655 oldoutfile = AR.outfile;
657 S->PolyFlag = ( AR.PolyFun != 0 ) ? AR.PolyFunType: 0;
661 S->PolyFlag = S->PolyWise = 0;
668 tover = over = S->sTerms;
671 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
674 S->TermsLeft -= over - spare;
675 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(); }
677 else if ( S != AT.S0 ) {
681 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
682 to = (WORD *)Malloc1(sSpace*
sizeof(WORD),
"$-sort space");
683 *((WORD **)buffer) = to;
685 while ( ( t = *ss++ ) != 0 ) {
686 j = *t;
while ( --j >= 0 ) *to++ = *t++;
694 while ( ( t = *ss++ ) != 0 ) {
696 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
697 MLOCK(ErrorMessageLock);
698 MesPrint(
"Sorted function argument too long.");
699 MUNLOCK(ErrorMessageLock);
700 retval = -1;
goto RetRetval;
702 while ( --j >= 0 ) *to++ = *t++;
712 *AR.CompressPointer = 0;
713 SeekScratch(AR.outfile,&position);
725 {
int oldgzipCompress = AR.gzipCompress;
731 while ( ( t = *ss++ ) != 0 ) {
732 if ( *t ) S->TermsLeft++;
734 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
737 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
738 retval = -1;
goto RetRetval;
743 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
747 retval = -1;
goto RetRetval;
750 AR.gzipCompress = oldgzipCompress;
754 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
757 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
759 DIFPOS(oldpos,position,oldpos);
760 S->SpaceLeft = BASEPOSITION(oldpos);
765 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(); }
767 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
770 SETBASEPOSITION(pp,lSpace);
771 MULPOS(pp,
sizeof(WORD));
772 if ( S->file.handle >= 0 ) {
773 ADD2POS(pp,S->fPatches[S->fPatchN]);
776 WORD oldLogHandle = AC.LogHandle;
777 if ( AC.LogHandle >= 0 && AM.LogType && ( ( S->lPatch > 0 )
778 || S->file.handle >= 0 ) ) AC.LogHandle = -1;
779 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
WriteStats(&pp,0); }
780 AC.LogHandle = oldLogHandle;
782 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(); }
783 if ( S->lPatch > 0 ) {
784 if ( ( S->lPatch >= S->MaxPatches ) ||
785 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
790 MLOCK(ErrorMessageLock);
791 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x" 792 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
793 MUNLOCK(ErrorMessageLock);
796 MLOCK(ErrorMessageLock);
798 MUNLOCK(ErrorMessageLock);
799 retval = -1;
goto RetRetval;
802 pp = S->SizeInFile[1];
803 MULPOS(pp,
sizeof(WORD));
808 WORD oldLogHandle = AC.LogHandle;
810 SETBASEPOSITION(pppp,0);
811 SeekFile(S->file.handle,&pppp,SEEK_CUR);
812 SeekFile(S->file.handle,&pp,SEEK_END);
813 SeekFile(S->file.handle,&pppp,SEEK_SET);
814 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
816 AC.LogHandle = oldLogHandle;
821 S->Patches[S->lPatch++] = S->lFill;
822 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
825 while ( ( t = *ss++ ) != 0 ) {
827 if ( j < 0 ) j = t[1] + 2;
828 while ( --j >= 0 ) *to++ = *t++;
833 if ( S->file.handle < 0 ) {
835 MLOCK(ErrorMessageLock);
837 MUNLOCK(ErrorMessageLock);
838 retval = -1;
goto RetRetval;
842 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
844 pp = S->SizeInFile[2];
845 MULPOS(pp,
sizeof(WORD));
850 if ( par == 2 && newout->handle >= 0 ) {
854 LOCK(newout->pthreadslock);
856 SeekFile(newout->handle,&zeropos,SEEK_SET);
857 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)
858 ,
"$-buffer reading");
859 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
860 BASEPOSITION(newout->filesize) ) {
861 MLOCK(ErrorMessageLock);
862 MesPrint(
"Error reading information for $ variable");
863 MUNLOCK(ErrorMessageLock);
864 M_free(to,
"$-buffer reading");
868 *((WORD **)buffer) = to;
869 retval /=
sizeof(WORD);
872 UNLOCK(newout->pthreadslock);
875 else if ( newout->handle >= 0 ) {
877 MLOCK(ErrorMessageLock);
878 MesPrint(
"Output should fit inside a single term. Increase MaxTermSize?");
880 MUNLOCK(ErrorMessageLock);
881 retval = -1;
goto RetRetval;
884 t = newout->PObuffer;
886 jj = newout->POfill - t;
887 to = (WORD *)Malloc1(jj*
sizeof(WORD),
"$-sort space");
888 *((WORD **)buffer) = to;
892 j = newout->POfill - t;
894 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
896 if ( j > AM.MaxTer )
goto TooLarge;
904 MLOCK(ErrorMessageLock);
906 MUNLOCK(ErrorMessageLock);
907 retval = -1;
goto RetRetval;
910 pp = S->SizeInFile[1];
911 MULPOS(pp,
sizeof(WORD));
916 WORD oldLogHandle = AC.LogHandle;
918 SETBASEPOSITION(pppp,0);
919 SeekFile(S->file.handle,&pppp,SEEK_CUR);
920 SeekFile(S->file.handle,&pp,SEEK_END);
921 SeekFile(S->file.handle,&pppp,SEEK_SET);
922 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
924 AC.LogHandle = oldLogHandle;
932 if ( newout->handle >= 0 )
goto TooLarge;
933 t = newout->PObuffer;
934 j = newout->POfill - t;
936 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
938 if ( j > AM.MaxTer )
goto TooLarge;
945 if ( S->file.handle >= 0 ) {
947 MLOCK(ErrorMessageLock);
948 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p" 949 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
950 MUNLOCK(ErrorMessageLock);
952 if ( S->lPatch <= 0 ) {
954 position = S->fPatches[S->fPatchN];
958 *AR.CompressPointer = 0;
959 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
960 S->fpcompressed[S->fPatchN] = 1;
962 S->fpcompressed[S->fPatchN] = 0;
963 SetupOutputGZIP(&(S->file));
965 while ( ( t = *ss++ ) != 0 ) {
966 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
967 retval = -1;
goto RetRetval;
970 if (
FlushOut(&position,&(S->file),1) ) {
971 retval = -1;
goto RetRetval;
974 S->fPatches[S->fPatchN] = position;
977 MLOCK(ErrorMessageLock);
978 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p" 979 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
980 MUNLOCK(ErrorMessageLock);
986 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
987 if ( S->file.handle >= 0 ) {
988 SynchFile(S->file.handle);
994 MLOCK(ErrorMessageLock);
996 MUNLOCK(ErrorMessageLock);
997 retval = -1;
goto RetRetval;
1001 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
1003 pp = S->SizeInFile[0];
1004 MULPOS(pp,
sizeof(WORD));
1012 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
1013 Expressions[AR.CurExpr].counter = S->TermsLeft;
1016 if ( AR.sLevel == 0 ) {
1017 Expressions[AR.CurExpr].counter = S->TermsLeft;
1021 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
1024 CloseFile(S->file.handle);
1025 S->file.handle = -1;
1026 remove(S->file.name);
1028 MLOCK(ErrorMessageLock);
1029 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
1030 MUNLOCK(ErrorMessageLock);
1033 AR.outfile = oldoutfile;
1035 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
1039 DeAllocFileHandle(newout);
1042 else if ( newout ) {
1043 if ( newout->handle >= 0 ) {
1044 MLOCK(ErrorMessageLock);
1045 MesPrint(
"Output should fit inside a single term. Increase MaxTermSize?");
1047 MUNLOCK(ErrorMessageLock);
1050 else if ( newout->POfill > newout->PObuffer ) {
1056 j = newout->POfill-newout->PObuffer;
1057 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
1058 goto WorkSpaceError;
1060 to = buffer; t = newout->PObuffer;
1061 while ( j-- > 0 ) *to++ = *t++;
1065 DeAllocFileHandle(newout);
1069 else if ( par == 2 ) {
1071 if ( retval == 0 ) {
1072 if ( newout->handle >= 0 ) {
1080 LOCK(newout->pthreadslock);
1082 SeekFile(newout->handle,&position,SEEK_END);
1084 SeekFile(newout->handle,&zeropos,SEEK_SET);
1085 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)
1086 ,
"$-buffer reading");
1087 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
1088 BASEPOSITION(position) ) {
1089 MLOCK(ErrorMessageLock);
1090 MesPrint(
"Error reading information for $ variable");
1091 MUNLOCK(ErrorMessageLock);
1092 M_free(to,
"$-buffer reading");
1096 *((WORD **)buffer) = to;
1097 retval /=
sizeof(WORD);
1100 UNLOCK(newout->pthreadslock);
1107 LONG wsiz = newout->POfill - newout->PObuffer;
1108 to = (WORD *)Malloc1(wsiz*
sizeof(WORD),
"$-buffer reading");
1109 *((WORD **)buffer) = to; t = newout->PObuffer;
1115 DeAllocFileHandle(newout);
1121 DeAllocFileHandle(newout);
1127 MLOCK(ErrorMessageLock);
1130 MUNLOCK(ErrorMessageLock);
1161 from = buffer + ( file->POsize *
sizeof(UBYTE) )/
sizeof(WORD);
1163 if ( i*((LONG)(
sizeof(WORD))) > AM.MaxTer ) {
1164 MLOCK(ErrorMessageLock);
1165 MesPrint(
"Problems in PutIn");
1166 MUNLOCK(ErrorMessageLock);
1170 while ( --i >= 0 ) *--to = *--from;
1173 if ( ( RetCode = FillInputGZIP(file,position,(UBYTE *)buffer
1174 ,file->POsize,npat) ) < 0 ) {
1175 MLOCK(ErrorMessageLock);
1176 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1177 RetCode,file->POsize);
1178 MUNLOCK(ErrorMessageLock);
1183 LOCK(file->pthreadslock);
1185 SeekFile(file->
handle,position,SEEK_SET);
1186 if ( ( RetCode = ReadFile(file->
handle,(UBYTE *)buffer,file->POsize) ) < 0 ) {
1188 UNLOCK(file->pthreadslock);
1190 MLOCK(ErrorMessageLock);
1191 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1192 RetCode,file->POsize);
1193 MUNLOCK(ErrorMessageLock);
1197 UNLOCK(file->pthreadslock);
1219 int dobracketindex = 0;
1220 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1221 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1224 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1226 MLOCK(ErrorMessageLock);
1227 MesPrint(
"%w Sflush created scratch file %s",fi->name);
1228 MUNLOCK(ErrorMessageLock);
1230 fi->
handle = (WORD)RetCode;
1231 PUTZERO(fi->filesize);
1232 PUTZERO(fi->POposition);
1235 MLOCK(ErrorMessageLock);
1236 MesPrint(
"Cannot create scratch file %s",fi->name);
1237 MUNLOCK(ErrorMessageLock);
1242 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1243 && dobracketindex == 0 ) {
1244 if ( FlushOutputGZIP(fi) )
return(-1);
1245 fi->POfill = fi->PObuffer;
1251 LOCK(fi->pthreadslock);
1253 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1254 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1255 if ( WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) != size ) {
1257 UNLOCK(fi->pthreadslock);
1259 MLOCK(ErrorMessageLock);
1260 MesPrint(
"Write error while finishing sort. Disk full?");
1261 MUNLOCK(ErrorMessageLock);
1264 ADDPOS(fi->filesize,size);
1265 ADDPOS(fi->POposition,size);
1266 fi->POfill = fi->PObuffer;
1268 UNLOCK(fi->pthreadslock);
1303 WORD i, *p, ret, *r, *rr, j, k, first;
1304 int dobracketindex = 0;
1307 if ( AT.SS != AT.S0 ) {
1311 if ( ( i = *term ) <= 0 )
return(0);
1313 ADDPOS(*position,i*
sizeof(WORD));
1316 if ( p >= fi->POstop ) {
1318 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1320 MLOCK(ErrorMessageLock);
1321 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1322 MUNLOCK(ErrorMessageLock);
1324 fi->
handle = (WORD)RetCode;
1325 PUTZERO(fi->filesize);
1326 PUTZERO(fi->POposition);
1329 MLOCK(ErrorMessageLock);
1330 MesPrint(
"Cannot create scratch file %s",fi->name);
1331 MUNLOCK(ErrorMessageLock);
1336 LOCK(fi->pthreadslock);
1338 if ( fi == AR.hidefile ) {
1339 LOCK(AS.inputslock);
1341 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1342 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1343 if ( fi == AR.hidefile ) {
1344 UNLOCK(AS.inputslock);
1347 UNLOCK(fi->pthreadslock);
1349 MLOCK(ErrorMessageLock);
1350 MesPrint(
"Write error during sort. Disk full?");
1351 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1352 fi->POsize,fi->
handle,&(fi->POposition));
1353 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1354 MUNLOCK(ErrorMessageLock);
1357 ADDPOS(fi->filesize,fi->POsize);
1359 ADDPOS(fi->POposition,fi->POsize);
1360 if ( fi == AR.hidefile ) {
1361 UNLOCK(AS.inputslock);
1364 UNLOCK(fi->pthreadslock);
1367 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1373 }
while ( --i > 0 );
1374 fi->POfull = fi->POfill = p;
1377 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1378 MLOCK(ErrorMessageLock);
1380 sprintf((
char *)(THRbuf),
"PutOut(%d)",AT.identity);
1381 PrintTerm(term,(
char *)(THRbuf));
1383 PrintTerm(term,
"PutOut");
1385 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1386 MesPrint(
"File %s, position %p",fi->name,position);
1387 MUNLOCK(ErrorMessageLock);
1390 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1391 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1392 r = rr = AR.CompressPointer;
1393 first = j = k = ret = 0;
1394 if ( ( i = *term ) != 0 ) {
1397 if ( fi == AR.outfile || fi == AR.hidefile ) {
1398 MLOCK(ErrorMessageLock);
1399 MesPrint(
"Ran into precompressed term");
1400 MUNLOCK(ErrorMessageLock);
1405 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1406 if ( dobracketindex ) {
1407 PutBracketInIndex(BHEAD term,position);
1413 WORD *polystop, *sa;
1417 while ( polystop < sa && *polystop != AR.PolyFun ) {
1418 polystop += polystop[1];
1420 if ( AR.PolyFunType == 2 ) polystop[2] &= ~CLEANPRF;
1421 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1422 i--; j--; k--; p++; r++;
1429 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1449 r[-(ABS(r[-1]))] = 0;
1450 if ( r >= AR.ComprTop ) {
1451 MLOCK(ErrorMessageLock);
1452 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1453 MUNLOCK(ErrorMessageLock);
1458 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1460 if ( dobracketindex ) {
1461 PutBracketInIndex(BHEAD term,position);
1467 WORD *polystop, *sa;
1471 while ( polystop < sa && *polystop != AR.PolyFun ) {
1472 polystop += polystop[1];
1474 if ( AR.PolyFunType == 2 ) polystop[2] &= ~CLEANPRF;
1475 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1476 i--; j--; k--; p++; r++;
1480 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1485 if ( AR.PolyFunType == 2 ) {
1487 tstop = term + *term;
1488 tstop -= ABS(tstop[-1]);
1490 while ( t < tstop ) {
1491 if ( *t == AR.PolyFun ) {
1497 if ( dobracketindex ) {
1498 PutBracketInIndex(BHEAD term,position);
1502 ADDPOS(*position,i*
sizeof(WORD));
1505 if ( p >= fi->POstop ) {
1507 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1509 sbuf->fill[sbuf->active] = fi->POstop;
1511 p = fi->PObuffer = fi->POfill = fi->POfull =
1512 sbuf->buff[sbuf->active];
1513 fi->POstop = sbuf->stop[sbuf->active];
1519 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1521 MLOCK(ErrorMessageLock);
1522 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1523 MUNLOCK(ErrorMessageLock);
1525 fi->
handle = (WORD)RetCode;
1526 PUTZERO(fi->filesize);
1527 PUTZERO(fi->POposition);
1530 MLOCK(ErrorMessageLock);
1531 MesPrint(
"Cannot create scratch file %s",fi->name);
1532 MUNLOCK(ErrorMessageLock);
1537 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1538 && dobracketindex == 0 ) {
1540 if ( PutOutputGZIP(fi) )
return(-1);
1547 LOCK(fi->pthreadslock);
1549 if ( fi == AR.hidefile ) {
1550 LOCK(AS.inputslock);
1552 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1553 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1554 if ( fi == AR.hidefile ) {
1555 UNLOCK(AS.inputslock);
1558 UNLOCK(fi->pthreadslock);
1560 MLOCK(ErrorMessageLock);
1561 MesPrint(
"Write error during sort. Disk full?");
1562 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1563 fi->POsize,fi->
handle,&(fi->POposition));
1564 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1565 MUNLOCK(ErrorMessageLock);
1568 ADDPOS(fi->filesize,fi->POsize);
1570 ADDPOS(fi->POposition,fi->POsize);
1571 if ( fi == AR.hidefile ) {
1572 UNLOCK(AS.inputslock);
1575 UNLOCK(fi->pthreadslock);
1578 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1586 if ( first == 2 ) *p++ = k;
1590 else *p++ = *term++;
1596 }
while ( --i > 0 );
1597 fi->POfull = fi->POfill = p;
1625 int dobracketindex = 0;
1629 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1630 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1632 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1634 if ( fi->POfill >= fi->POstop ){
1635 sbuf->fill[sbuf->active] = fi->POstop;
1637 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1638 fi->POstop = sbuf->stop[sbuf->active];
1640 *(fi->POfill)++ = 0;
1641 sbuf->fill[sbuf->active] = fi->POfill;
1643 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1644 fi->POstop = sbuf->stop[sbuf->active];
1648 if ( fi->POfill >= fi->POstop ) {
1650 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1652 MLOCK(ErrorMessageLock);
1653 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1654 MUNLOCK(ErrorMessageLock);
1656 PUTZERO(fi->filesize);
1657 PUTZERO(fi->POposition);
1658 fi->
handle = (WORD)RetCode;
1661 MLOCK(ErrorMessageLock);
1662 MesPrint(
"Cannot create scratch file %s",fi->name);
1663 MUNLOCK(ErrorMessageLock);
1668 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1669 && dobracketindex == 0 && ( compr > 0 ) ) {
1670 if ( PutOutputGZIP(fi) )
return(-1);
1671 fi->POfill = fi->PObuffer;
1677 LOCK(fi->pthreadslock);
1679 if ( fi == AR.hidefile ) {
1680 LOCK(AS.inputslock);
1682 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1683 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1685 UNLOCK(fi->pthreadslock);
1687 if ( fi == AR.hidefile ) {
1688 UNLOCK(AS.inputslock);
1690 MLOCK(ErrorMessageLock);
1691 MesPrint(
"Write error while sorting. Disk full?");
1692 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1693 fi->POsize,fi->
handle,&(fi->POposition));
1694 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1695 MUNLOCK(ErrorMessageLock);
1698 ADDPOS(fi->filesize,fi->POsize);
1699 fi->POfill = fi->PObuffer;
1700 ADDPOS(fi->POposition,fi->POsize);
1701 if ( fi == AR.hidefile ) {
1702 UNLOCK(AS.inputslock);
1705 UNLOCK(fi->pthreadslock);
1708 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1714 *(fi->POfill)++ = 0;
1715 fi->POfull = fi->POfill;
1728 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1731 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1732 && dobracketindex == 0 && ( compr > 0 ) ) {
1733 if ( FlushOutputGZIP(fi) )
return(-1);
1734 fi->POfill = fi->PObuffer;
1740 LOCK(fi->pthreadslock);
1742 if ( fi == AR.hidefile ) {
1743 LOCK(AS.inputslock);
1745 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1749 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1751 UNLOCK(fi->pthreadslock);
1753 if ( fi == AR.hidefile ) {
1754 UNLOCK(AS.inputslock);
1756 MLOCK(ErrorMessageLock);
1757 MesPrint(
"Write error while finishing sorting. Disk full?");
1758 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1759 size,fi->
handle,&(fi->POposition));
1760 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1761 MUNLOCK(ErrorMessageLock);
1764 ADDPOS(fi->filesize,size);
1765 ADDPOS(fi->POposition,size);
1766 fi->POfill = fi->PObuffer;
1767 if ( fi == AR.hidefile ) {
1768 UNLOCK(AS.inputslock);
1771 UNLOCK(fi->pthreadslock);
1774 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1780 if ( dobracketindex ) {
1781 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1782 if ( b->indexfill > 0 ) {
1783 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1787 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1788 && dobracketindex == 0 && ( compr > 0 ) ) {
1792 LOCK(fi->pthreadslock);
1794 SeekFile(fi->
handle,position,SEEK_END);
1796 UNLOCK(fi->pthreadslock);
1800 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1806 ADDPOS(*position,
sizeof(WORD));
1837 OutCoef = AN.SoScratC;
1838 s1 = *ps1; s2 = *ps2;
1841 if ( AddRat(BHEAD (UWORD *)s1,l1,(UWORD *)s2,l2,OutCoef,&OutLen) ) {
1842 MLOCK(ErrorMessageLock);
1844 MUNLOCK(ErrorMessageLock);
1847 if ( AN.ncmod != 0 ) {
1848 if ( ( AC.modmode & POSNEG ) != 0 ) {
1855 j = ABS(OutLen); OutCoef[j] = 1;
1856 for ( i = 1; i < j; i++ ) OutCoef[j+i] = 0;
1858 else if ( BigLong(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
1859 SubPLon(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod),OutCoef,&OutLen);
1860 OutCoef[OutLen] = 1;
1861 for ( i = 1; i < OutLen; i++ ) OutCoef[OutLen+i] = 0;
1864 if ( !OutLen ) { *ps1 = *ps2 = 0;
return(0); }
1866 if ( OutLen < 0 ) i = - ( --OutLen );
1868 if ( l1 < 0 ) l1 = -l1;
1873 s2 = (WORD *)OutCoef;
1874 while ( --i > 0 ) *s1++ = *s2++;
1876 while ( --l1 >= 0 ) *s1++ = 0;
1879 if ( l2 < 0 ) l2 = -l2;
1884 s1 = (WORD *)OutCoef;
1885 while ( --i > 0 ) *s2++ = *s1++;
1887 while ( --l2 >= 0 ) *s2++ = 0;
1897 if ( (S->sFill + j) >= S->sTop2 ) {
1904 if ( l1 < 0 ) l1 = - l1;
1909 while ( s1 < t ) *s2++ = *s1++;
1910 s1 = (WORD *)OutCoef;
1911 while ( --i > 0 ) *s2++ = *s1++;
1917 if ( **ps1 > AM.MaxTer/((LONG)(
sizeof(WORD))) ) {
1918 MLOCK(ErrorMessageLock);
1919 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
1920 AM.MaxTer/
sizeof(WORD));
1921 MUNLOCK(ErrorMessageLock);
1961 WORD *s1, *s2, *m, *w, *t, oldpw = S->PolyWise;
1962 s1 = *ps1 + S->PolyWise;
1963 s2 = *ps2 + S->PolyWise;
1968 if ( S->PolyFlag == 2 ) {
1969 WORD **oldSplitScratch = AN.SplitScratch;
1970 LONG oldSplitScratchSize = AN.SplitScratchSize;
1971 LONG oldInScratch = AN.InScratch;
1972 if ( (WORD *)((UBYTE *)w + AM.MaxTer) >= AT.WorkTop ) {
1973 MLOCK(ErrorMessageLock);
1974 MesPrint(
"Program was adding polyratfun arguments");
1976 MUNLOCK(ErrorMessageLock);
1979 AN.SplitScratch = AN.SplitScratch1;
1980 AN.SplitScratchSize = AN.SplitScratchSize1;
1981 AN.InScratch = AN.InScratch1;
1982 poly_ratfun_add(BHEAD s1,s2);
1983 S->PolyWise = oldpw;
1984 AN.SplitScratch1 = AN.SplitScratch;
1985 AN.SplitScratchSize1 = AN.SplitScratchSize;
1986 AN.InScratch1 = AN.InScratch;
1987 AN.SplitScratch = oldSplitScratch;
1988 AN.SplitScratchSize = oldSplitScratchSize;
1989 AN.InScratch = oldInScratch;
1991 if ( w[1] <= FUNHEAD ||
1992 ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
1993 *ps1 = *ps2 = 0;
return(0);
1997 if ( w + s1[1] + s2[1] + 12 + ARGHEAD >= AT.WorkTop ) {
1998 MLOCK(ErrorMessageLock);
1999 MesPrint(
"Program was adding polyfun arguments");
2001 MUNLOCK(ErrorMessageLock);
2008 if ( w[1] <= FUNHEAD ) { *ps1 = *ps2 = 0;
return(0); }
2009 if ( w[1] <= s1[1] || w[1] <= s2[1] ) {
2010 if ( w[1] > s1[1] ) {
2019 while ( t < m ) *s1++ = *t++;
2020 **ps1 = WORDDIF(s1,(*ps1));
2029 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2031 MesPrint(
"------Garbage collection-------");
2033 AT.WorkPointer += w[1];
2037 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2040 MLOCK(ErrorMessageLock);
2041 AO.OutFill = AO.OutputLine = OutBuf;
2045 while ( --i >= 0 ) {
2046 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2049 AO.OutFill = AO.OutputLine = OutBuf;
2054 while ( --i >= 0 ) {
2055 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2058 AO.OutFill = AO.OutputLine = OutBuf;
2063 while ( --i >= 0 ) {
2064 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2067 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2068 MUNLOCK(ErrorMessageLock);
2070 MLOCK(ErrorMessageLock);
2071 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2072 MUNLOCK(ErrorMessageLock);
2086 while ( t < w ) *s2++ = *t++;
2090 if ( *m > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
2091 MLOCK(ErrorMessageLock);
2092 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2093 AM.MaxTer/
sizeof(WORD));
2094 MUNLOCK(ErrorMessageLock);
2106 #define INSLENGTH(x) w[1] = FUNHEAD+ARGHEAD+x; w[FUNHEAD] = ARGHEAD+x; 2119 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2120 WORD tempterm[8+FUNHEAD];
2122 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2123 *m++ = 0; *m++ = 0; FILLARG(m)
2124 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2125 if ( s1[FUNHEAD] < 0 ) {
2126 if ( s2[FUNHEAD] < 0 ) {
2127 if ( s1[FUNHEAD] <= -FUNCTION ) {
2128 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2129 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2131 *m++ = 2; *m++ = 1; *m++ = 3;
2132 INSLENGTH(4+FUNHEAD)
2134 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2135 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2136 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2137 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2139 s2[FUNHEAD] = s1[FUNHEAD];
2142 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2144 *m++ = 1; *m++ = 1; *m++ = 3;
2145 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2147 *m++ = 1; *m++ = 1; *m++ = 3;
2148 INSLENGTH(8+2*FUNHEAD)
2150 else if ( s2[FUNHEAD] == -SYMBOL ) {
2151 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2152 *m++ = 1; *m++ = 1; *m++ = 3;
2153 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2155 *m++ = 1; *m++ = 1; *m++ = 3;
2156 INSLENGTH(12+FUNHEAD)
2160 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2161 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2163 *m++ = 1; *m++ = 1; *m++ = 3;
2164 INSLENGTH(8+FUNHEAD)
2167 else if ( s1[FUNHEAD] == -SYMBOL ) {
2168 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2169 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2170 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2171 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2175 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2176 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2177 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2178 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2179 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2180 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2181 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2185 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2186 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2187 *m++ = 1; *m++ = 1; *m++ = 3;
2188 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2190 *m++ = 1; *m++ = 1; *m++ = 3;
2191 INSLENGTH(12+FUNHEAD)
2195 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2196 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2197 *m++ = 1; *m++ = 1; *m++ = 3;
2202 if ( s2[FUNHEAD] <= -FUNCTION ) {
2204 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2205 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2207 *m++ = 1; *m++ = 1; *m++ = 3;
2208 INSLENGTH(8+FUNHEAD)
2210 else if ( s2[FUNHEAD] == -SYMBOL ) {
2212 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2213 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2214 *m++ = 1; *m++ = 1; *m++ = 3;
2219 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2220 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2221 else { i1 = (WORD)x1; i2 = 3; }
2222 if ( x1 && AN.ncmod != 0 ) {
2227 if ( Modulus(m) ) Terminate(-1);
2228 if ( *m == 0 ) w[1] = 0;
2230 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2248 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2255 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2268 if ( s1[FUNHEAD] <= -FUNCTION ) {
2269 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2271 *t++ = 1; *t++ = 1; *t++ = 3;
2273 else if ( s1[FUNHEAD] == -SYMBOL ) {
2274 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2275 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2276 *t++ = 1; *t++ = 1; *t++ = 3;
2279 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2280 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2295 tstop1 = s1 + s1[1];
2296 s1 += FUNHEAD+ARGHEAD;
2298 tstop2 = s2 + s2[1];
2299 s2 += FUNHEAD+ARGHEAD;
2303 AT.SS->PolyFlag = 0;
2304 while ( s1 < tstop1 && s2 < tstop2 ) {
2305 i1 = CompareTerms(BHEAD s1,s2,(WORD)(-1));
2310 else if ( i1 < 0 ) {
2333 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2334 MLOCK(ErrorMessageLock);
2335 MesPrint(
"Addition of coefficients of PolyFun");
2336 MUNLOCK(ErrorMessageLock);
2346 *mm = WORDDIF(m,mm);
2347 if ( AN.ncmod != 0 ) {
2348 if ( Modulus(mm) ) Terminate(-1);
2355 while ( s1 < tstop1 ) *m++ = *s1++;
2356 while ( s2 < tstop2 ) *m++ = *s2++;
2357 w[1] = WORDDIF(m,w);
2358 w[FUNHEAD] = w[1] - FUNHEAD;
2359 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2360 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2361 else w[1] = FUNHEAD+2;
2362 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2364 AT.SS->PolyFlag = AR.PolyFunType;
2401 WORD *stopper1, *stopper2, *t2;
2403 WORD *stopex1, *stopex2;
2406 WORD count = -1, localPoly, polyhit = -1;
2408 if ( S->PolyFlag ) {
2416 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2417 S->PolyFlag = AR.PolyFunType;
2419 else { localPoly = 0; }
2423 GETSTOP(term2,stopper2);
2426 while ( t1 < stopper1 && t2 < stopper2 ) {
2428 if ( *t1 == HAAKJE )
return(PREV(-1));
2429 if ( *t2 == HAAKJE )
return(PREV(1));
2430 if ( *t1 >= (FUNCTION-1) ) {
2431 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2432 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2433 if ( *t1 < FUNCTION )
return(PREV(1));
2434 if ( *t2 < FUNCTION )
return(PREV(-1));
2435 c1 = functions[*t1-FUNCTION].commute;
2436 c2 = functions[*t2-FUNCTION].commute;
2438 if ( c2 )
return(PREV(1));
2439 else return(PREV(*t2-*t1));
2442 if ( !c2 )
return(PREV(-1));
2443 else return(PREV(*t2-*t1));
2446 else return(PREV(*t2-*t1));
2453 if ( localPoly && c1 < FUNCTION ) {
2456 if ( c1 <= (FUNCTION-1)
2457 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec ) ) {
2458 if ( c1 == SYMBOL ) {
2459 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2460 && s1[-1] == 4 && s2[-1] == 4
2461 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2462 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2469 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2472 else if ( AR.SortType >= SORTPOWERFIRST ) {
2475 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2477 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2480 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2487 if ( AR.SortType==SORTLOWFIRST ) {
2488 return(PREV((s1[1]>0?-1:1)));
2491 return(PREV((s1[1]<0?-1:1)));
2496 if ( AR.SortType==SORTLOWFIRST ) {
2498 return(PREV((s1[1]<0?1:-1)));
2501 return(PREV((s2[1]<0?-1:1)));
2506 return(PREV((s1[1]<0?-1:1)));
2509 return(PREV((s2[1]<0?1:-1)));
2514 if ( *s1 != *s2 )
return(
2515 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2520 if ( AR.SortType==SORTLOWFIRST ) {
2521 return(PREV((s2[1]<0?-1:1)));
2524 return(PREV((s2[1]<0?1:-1)));
2528 else if ( c1 == DOTPRODUCT ) {
2529 if ( AR.SortType >= SORTPOWERFIRST ) {
2532 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2534 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2537 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2542 if ( s2 >= t2 )
return(PREV(1));
2543 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2545 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2547 if ( *s1 != *s2 )
return(
2548 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2551 if ( s2 < t2 )
return(PREV(-1));
2555 if ( s2 >= t2 )
return(PREV(1));
2556 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2559 if ( s2 < t2 )
return(PREV(-1));
2567 if ( localPoly && c1 == AR.PolyFun ) {
2569 if ( AR.PolyFunType == 1 ) {
2571 if ( *s1 > 0 ) i1 = *s1;
2572 else if ( *s1 <= -FUNCTION ) i1 = 1;
2574 if ( *s2 > 0 ) i2 = *s2;
2575 else if ( *s2 <= -FUNCTION ) i2 = 1;
2577 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2582 WORD *u1, *u2, *ustop;
2584 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2591 ustop = u2 - ABS(u2[-1]);
2593 while ( u1 < ustop ) {
2594 if ( *u1 == INDEX )
goto NoPoly;
2601 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2608 ustop = u2 - ABS(u2[-1]);
2610 while ( u1 < ustop ) {
2611 if ( *u1 == INDEX )
goto NoPoly;
2618 S->PolyWise = WORDDIF(s1,term1);
2619 S->PolyWise -= FUNHEAD;
2625 S->PolyWise = localPoly = 0;
2628 else if ( AR.PolyFunType == 2 ) {
2629 WORD i1, i2, i1a, i2a;
2630 if ( *s1 > 0 ) i1 = *s1;
2631 else if ( *s1 <= -FUNCTION ) i1 = 1;
2633 if ( *s2 > 0 ) i2 = *s2;
2634 else if ( *s2 <= -FUNCTION ) i2 = 1;
2636 if ( s1[i1] > 0 ) i1a = s1[i1];
2637 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2639 if ( s2[i2] > 0 ) i2a = s2[i2];
2640 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2642 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2647 WORD *u1, *u2, *ustop;
2649 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2654 while ( u1 < s1+i1 ) {
2656 ustop = u2 - ABS(u2[-1]);
2658 while ( u1 < ustop ) {
2659 if ( *u1 == INDEX )
goto NoPoly;
2666 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2670 u1 = s1 +i1 + ARGHEAD;
2673 ustop = u2 - ABS(u2[-1]);
2675 while ( u1 < ustop ) {
2676 if ( *u1 == INDEX )
goto NoPoly;
2683 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2688 while ( u1 < s2+i2 ) {
2690 ustop = u2 - ABS(u2[-1]);
2692 while ( u1 < ustop ) {
2693 if ( *u1 == INDEX )
goto NoPoly;
2700 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2704 u1 = s2 + i2 + ARGHEAD;
2707 ustop = u2 - ABS(u2[-1]);
2709 while ( u1 < ustop ) {
2710 if ( *u1 == INDEX )
goto NoPoly;
2717 S->PolyWise = WORDDIF(s1,term1);
2718 S->PolyWise -= FUNHEAD;
2723 S->PolyWise = localPoly = 0;
2727 S->PolyWise = localPoly = 0;
2731 t1 = term1 + S->PolyWise;
2732 t2 = term2 + S->PolyWise;
2742 if ( s2 >= t2 )
return(PREV(-1));
2749 if ( AC.properorderflag ) {
2750 if ( ( c2 = -CompArg(s1,s2) ) != 0 )
return(PREV(c2));
2758 if ( s2 >= t2 )
return(PREV(-1));
2760 s1 += ARGHEAD; s2 += ARGHEAD;
2761 while ( s1 < stopex1 ) {
2762 if ( s2 >= stopex2 )
return(PREV(-1));
2763 if ( ( c2 = CompareTerms(BHEAD s1,s2,(WORD)1) ) != 0 )
2768 if ( s2 < stopex2 )
return(PREV(1));
2770 else return(PREV(1));
2773 if ( *s2 > 0 )
return(PREV(-1));
2774 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2775 if ( *s1 > -FUNCTION ) {
2776 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2782 if ( s2 < t2 )
return(PREV(1));
2786 if ( AR.SortType != SORTLOWFIRST ) {
2787 if ( t1 < stopper1 )
return(PREV(1));
2788 if ( t2 < stopper2 )
return(PREV(-1));
2791 if ( t1 < stopper1 )
return(PREV(-1));
2792 if ( t2 < stopper2 )
return(PREV(1));
2795 if ( level == 3 )
return(
CompCoef(term1,term2));
2821 WORD *t1, *t2, *tt1, *tt2;
2823 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
2824 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
2825 if ( AN.polysortflag > 0 ) {
2827 while ( t1 < tt1 ) { sum1 += t1[1]; t1 += 2; }
2828 while ( t2 < tt2 ) { sum2 += t2[1]; t2 += 2; }
2829 if ( sum1 < sum2 )
return(-1);
2830 if ( sum1 > sum2 )
return(1);
2831 t1 = term1+3; t2 = term2 + 3;
2833 while ( t1 < tt1 && t2 < tt2 ) {
2834 if ( *t1 > *t2 )
return(-1);
2835 if ( *t1 < *t2 )
return(1);
2836 if ( t1[1] < t2[1] )
return(-1);
2837 if ( t1[1] > t2[1] )
return(1);
2840 if ( t1 < tt1 )
return(1);
2841 if ( t2 < tt2 )
return(-1);
2906 if ( AT.SS == AT.S0 && !AR.NoCompress ) {
2907 if ( AN.compressSize == 0 ) {
2908 if ( *ss ) { AN.compressSize = **ss + 64; }
2909 else { AN.compressSize = AM.MaxTer/
sizeof(WORD) + 2; }
2910 AN.compressSpace = (WORD *)Malloc1(AN.compressSize*
sizeof(WORD),
"Compression");
2912 AN.compressSpace[0] = 0;
2917 if ( j > AN.compressSize ) {
2919 t = (WORD *)Malloc1(newsize*
sizeof(WORD),
"Compression");
2921 if ( AN.compressSpace ) {
2922 for ( i = 0; i < *AN.compressSpace; i++ ) t[i] = AN.compressSpace[i];
2923 M_free(AN.compressSpace,
"Compression");
2925 AN.compressSpace = t;
2926 AN.compressSize = newsize;
2928 t = AN.compressSpace;
2932 WORD *polystop, *sa;
2936 while ( polystop < sa && *polystop != AR.PolyFun ) {
2937 polystop += polystop[1];
2939 while ( i > 0 && j > 0 && *s == *t && s < polystop ) {
2940 i--; j--; s++; t++; k--;
2947 while ( i > 0 && j > 0 && *s == *t && s < sa ) { i--; j--; s++; t++; k--; }
2956 size += *AN.compressSpace;
2957 if ( k == -1 ) { t--; s--; j++; }
2959 while ( --j >= 0 ) *t++ = *s++;
2961 t = AN.compressSpace + *AN.compressSpace;
2962 t[-(ABS(t[-1]))] = 0;
3031 #ifdef NEWSPLITMERGE 3037 WORD **pp3, **pp1, **pp2;
3038 LONG nleft, nright, i, newleft, newright;
3041 if ( number < 2 )
return(number);
3042 if ( number == 2 ) {
3043 pp1 = Pointer; pp2 = pp1 + 1;
3044 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3045 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3047 else if ( i == 0 ) {
3049 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3050 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3054 pptop = Pointer + number;
3055 nleft = number >> 1; nright = number - nleft;
3057 newright =
SplitMerge(BHEAD Pointer+nleft,nright);
3065 if ( newleft > 0 && newright > 0 &&
3066 ( i = CompareTerms(BHEAD Pointer[newleft-1],Pointer[nleft],(WORD)0) ) >= 0 ) {
3067 pp2 = Pointer+nleft; pp1 = Pointer+newleft-1;
3069 if ( S->PolyWise ) {
3070 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3074 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3077 *pp2++ = 0; newright--;
3080 newleft += newright;
3082 while ( --newright >= 0 ) *pp1++ = *pp2++;
3083 while ( pp1 < pptop ) *pp1++ = 0;
3087 if ( nleft > AN.SplitScratchSize ) {
3088 AN.SplitScratchSize = (nleft*3)/2+100;
3089 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3090 AN.SplitScratchSize = S->Terms2InSmall/2;
3091 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3092 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3094 pp3 = AN.SplitScratch; pp1 = Pointer; i = nleft;
3095 do { *pp3++ = *pp1; *pp1++ = 0; }
while ( *pp1 && --i > 0 );
3096 if ( i > 0 ) { *pp3 = 0; i--; }
3097 AN.InScratch = nleft - i;
3098 pp1 = AN.SplitScratch; pp2 = Pointer + nleft; pp3 = Pointer;
3099 while ( nleft > 0 && nright > 0 && *pp1 && *pp2 ) {
3100 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3111 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3112 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3113 *pp1++ = 0; *pp2++ = 0; nleft--; nright--;
3116 while ( --nleft >= 0 && *pp1 ) { *pp3++ = *pp1; *pp1++ = 0; }
3117 while ( --nright >= 0 && *pp2 ) { *pp3++ = *pp2++; }
3118 nleft = pp3 - Pointer;
3119 while ( pp3 < pptop ) *pp3++ = 0;
3130 WORD **pp3, **pp1, **pp2;
3131 LONG nleft, nright, i;
3134 if ( number < 2 )
return;
3135 if ( number == 2 ) {
3136 pp1 = Pointer; pp2 = pp1 + 1;
3137 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3138 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3140 else if ( i == 0 ) {
3141 if ( S->PolyWise ) {
if ( !
AddPoly(BHEAD pp1,pp2) ) { *pp1 = 0; } }
3142 else {
if ( !
AddCoef(BHEAD pp1,pp2) ) { *pp1 = 0; } }
3147 pptop = Pointer + number;
3148 nleft = number >> 1; nright = number - nleft;
3151 if ( nleft > AN.SplitScratchSize ) {
3152 AN.SplitScratchSize = (nleft*3)/2+100;
3153 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3154 AN.SplitScratchSize = S->Terms2InSmall/2;
3155 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3156 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3158 pp3 = AN.SplitScratch; pp1 = Pointer; i = nleft;
3159 do { *pp3++ = *pp1; *pp1++ = 0; }
while ( *pp1 && --i > 0 );
3160 if ( i > 0 ) { *pp3 = 0; i--; }
3161 AN.InScratch = nleft - i;
3162 pp1 = AN.SplitScratch; pp2 = Pointer + nleft; pp3 = Pointer;
3163 while ( *pp1 && *pp2 && nleft > 0 && nright > 0 ) {
3164 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3175 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3176 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3177 *pp1++ = 0; *pp2++ = 0; nleft--; nright--;
3180 while ( *pp1 && --nleft >= 0 ) { *pp3++ = *pp1; *pp1++ = 0; }
3181 while ( *pp2 && --nright >= 0 ) { *pp3++ = *pp2++; }
3182 while ( pp3 < pptop ) *pp3++ = 0;
3213 WORD **Point, *s2, *t, *garbuf, i;
3215 int tobereturned = 0;
3220 MLOCK(ErrorMessageLock);
3221 MesPrint(
"in: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3223 Point = S->sPointer;
3225 while ( --k >= 0 ) {
3226 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3228 Point = AN.SplitScratch;
3230 while ( --k >= 0 ) {
3231 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3234 MesPrint(
"total = %l, nterms = %l",2*total,AN.InScratch);
3235 MUNLOCK(ErrorMessageLock);
3241 if ( S->lBuffer != 0 && S->lFill + total <= S->lTop ) {
3245 garbuf = (WORD *)Malloc1(total*
sizeof(WORD),
"Garbage buffer");
3249 Point = S->sPointer;
3251 while ( --k >= 0 ) {
3259 Point = AN.SplitScratch;
3261 while ( --k >= 0 ) {
3271 Point = S->sPointer;
3273 while ( --k >= 0 ) {
3281 Point = AN.SplitScratch;
3283 while ( --k >= 0 ) {
3293 MLOCK(ErrorMessageLock);
3294 MesPrint(
"out: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3295 if ( S->sFill >= S->sTop2 ) {
3296 MesPrint(
"We are in deep trouble");
3298 MUNLOCK(ErrorMessageLock);
3300 if ( tobereturned ) M_free(garbuf,
"Garbage buffer");
3328 WORD **poin, **poin2, ul, k, i, im, *m1;
3329 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3330 WORD *m2, *m3, r31, r33, ki, *rr;
3340 int oldgzipCompress = AR.gzipCompress;
3342 AR.gzipCompress = 0;
3346 fout = &(AR.FoStage4[0]);
3347 S->PolyFlag = AR.PolyFun ? AR.PolyFunType: 0;
3350 poin = S->poina; poin2 = S->poin2a;
3351 rr = AR.CompressPointer;
3358 if ( fout->
handle < 0 ) {
3360 PUTZERO(AN.OldPosOut);
3361 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3362 MLOCK(ErrorMessageLock);
3363 MesPrint(
"Cannot create file %s",fout->name);
3364 MUNLOCK(ErrorMessageLock);
3368 MLOCK(ErrorMessageLock);
3369 MesPrint(
"%w MergePatches created output file %s",fout->name);
3370 MUNLOCK(ErrorMessageLock);
3373 PUTZERO(fout->filesize);
3374 PUTZERO(fout->POposition);
3376 LOCK(fout->pthreadslock);
3378 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3380 UNLOCK(fout->pthreadslock);
3383 PUTZERO(S->fPatches[0]);
3384 fout->POfill = fout->PObuffer;
3385 PUTZERO(fout->POposition);
3390 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3391 S->fpcompressed[S->fPatchN] = 1;
3393 S->fpcompressed[S->fPatchN] = 0;
3394 SetupOutputGZIP(fout);
3397 else if ( par == 0 && S->stage4 > 0 ) {
3409 S->iPatches = S->fPatches;
3410 S->fPatches = S->inPatches;
3411 S->inPatches = S->iPatches;
3412 (S->inNum) = S->fPatchN;
3413 AN.OldPosIn = AN.OldPosOut;
3415 m1 = S->fpincompressed;
3416 S->fpincompressed = S->fpcompressed;
3417 S->fpcompressed = m1;
3418 for ( i = 0; i < S->inNum; i++ ) {
3419 S->fPatchesStop[i] = S->iPatches[i+1];
3421 MLOCK(ErrorMessageLock);
3422 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3423 MUNLOCK(ErrorMessageLock);
3435 AR.gzipCompress = 0;
3438 S->iPatches = S->fPatches;
3439 S->inNum = S->fPatchN;
3441 m1 = S->fpincompressed;
3442 S->fpincompressed = S->fpcompressed;
3443 S->fpcompressed = m1;
3444 for ( i = 0; i < S->inNum; i++ ) {
3445 S->fPatchesStop[i] = S->fPatches[i+1];
3447 MLOCK(ErrorMessageLock);
3448 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3449 MUNLOCK(ErrorMessageLock);
3457 S->Patches[S->lPatch] = S->lFill;
3458 for ( i = 0; i < S->lPatch; i++ ) {
3459 S->pStop[i] = S->Patches[i+1]-1;
3460 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3464 S->lPatch = (S->inNum);
3466 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3468 if ( S->lPatch > 1 ) {
3471 SetupAllInputGZIP(S);
3474 for ( i = 0; i < S->lPatch; i++ ) {
3475 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3477 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3478 S->pStop[i] = m2 = p;
3480 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3482 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3487 if ( fout->
handle >= 0 ) {
3490 LOCK(fout->pthreadslock);
3492 SeekFile(fout->
handle,&position,SEEK_END);
3493 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3495 UNLOCK(fout->pthreadslock);
3499 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3508 if ( S->lPatch == 1 ) {
3511 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3517 m2 = m1 = *S->Patches;
3520 i = -(*m1++); m2 += i; im = *m1+i+1;
3521 while ( i > 0 ) { *m1-- = *m2--; i--; }
3525 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD m1); }
3528 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3529 ADDPOS(S->SizeInFile[par],im);
3534 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3537 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3538 ADDPOS(S->SizeInFile[par],1);
3541 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3542 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3544 ADDPOS(position,length);
3545 ADDPOS(fout->POposition,length);
3546 ADDPOS(fout->filesize,length);
3547 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3557 #define FRONTSIZE (2*AM.MaxTer) 3558 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3560 SetupOutputGZIP(fout);
3561 SetupAllInputGZIP(S);
3563 position2 = S->iPatches[0];
3564 while ( ( length = FillInputGZIP(fin,&position2,
3566 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3567 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3568 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3569 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3577 i = -(*m1++); m2 += i; im = *m1+i+1;
3578 while ( i > 0 ) { *m1-- = *m2--; i--; }
3582 if ( AS.MasterSort && ( fout == AR.outfile ) ) {
3583 im = PutToMaster(BHEAD m1);
3587 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3588 ADDPOS(S->SizeInFile[par],im);
3592 if ( m1 < copytop && *m1 == 0 )
break;
3598 while ( m1 > m2 ) *--m3 = *--m1;
3603 MLOCK(ErrorMessageLock);
3604 MesPrint(
"Readerror");
3608 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3611 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3612 ADDPOS(S->SizeInFile[par],1);
3615 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3616 while ( ( length = ReadFile(fin->
handle,
3617 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3618 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3620 ADDPOS(position,length);
3621 ADDPOS(fout->POposition,length);
3622 ADDPOS(fout->filesize,length);
3623 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3626 MLOCK(ErrorMessageLock);
3627 MesPrint(
"Readerror");
3634 else if ( S->lPatch > 0 ) {
3639 do { lpat <<= 1; }
while ( lpat < S->lPatch );
3640 mpat = ( lpat >> 1 ) - 1;
3641 k = lpat - S->lPatch;
3646 for ( i = 1; i < lpat; i++ ) {
3649 for ( i = 1; i <= k; i++ ) {
3650 im = ( i << 1 ) - 1;
3651 poin[im] = S->Patches[i-1];
3652 poin2[im] = poin[im] + *(poin[im]);
3655 S->tree[mpat+i] = 0;
3656 poin[im-1] = poin2[im-1] = 0;
3658 for ( i = (k<<1)+1; i <= lpat; i++ ) {
3661 poin[i] = S->Patches[i-k-1];
3662 poin2[i] = poin[i] + *(poin[i]);
3682 if ( !*(poin[k]) ) {
3683 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3684 if ( S->tree[i] == -1 ) {
3697 if ( S->tree[i] > 0 ) {
3698 if ( ( c = CompareTerms(BHEAD poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3702 S->used[level] = S->tree[i];
3712 l1 = *( m1 = poin[S->tree[i]] );
3713 l2 = *( m2 = poin[k] );
3714 if ( S->PolyWise ) {
3719 if ( S->PolyFlag == 2 ) {
3720 w = poly_ratfun_add(BHEAD m1,m2);
3721 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3722 MLOCK(ErrorMessageLock);
3723 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3724 MUNLOCK(ErrorMessageLock);
3731 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3732 MLOCK(ErrorMessageLock);
3733 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3734 MUNLOCK(ErrorMessageLock);
3741 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
3743 if ( r1 == m1[1] ) {
3746 else if ( r1 < m1[1] ) {
3750 while ( --r1 >= 0 ) *--m1 = *--m2;
3753 while ( --r1 >= 0 ) *--m1 = *--m2;
3755 poin[S->tree[i]] = m1;
3763 poin[S->tree[i]] = m2;
3770 r1 = *( m1 += l1 - 1 );
3772 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
3773 r2 = *( m2 += l2 - 1 );
3775 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
3777 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
3778 MLOCK(ErrorMessageLock);
3779 MesCall(
"MergePatches");
3780 MUNLOCK(ErrorMessageLock);
3784 if ( AN.ncmod != 0 ) {
3785 if ( ( AC.modmode & POSNEG ) != 0 ) {
3788 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
3790 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
3792 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
3796 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
3797 if ( r3 < 0 ) r3 = -r3;
3798 if ( r1 < 0 ) r1 = -r1;
3803 ul = S->used[level] = S->tree[i];
3810 r1 = poin2[ul][1] - im + 1;
3812 m2 = poin[ul] - im + 1;
3813 while ( ++im <= 0 ) *--m1 = *--m2;
3818 poin[ul] = poin2[ul];
3820 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
3823 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
3825 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
3826 S->Patches[ki],&(poin[ul]),ki));
3828 poin2[ul] = poin[ul] + im;
3833 S->used[++level] = k;
3839 else if ( r31 < 0 ) {
3848 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
3854 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3855 MLOCK(ErrorMessageLock);
3856 MesPrint(
"Coefficient overflow during sort");
3857 MUNLOCK(ErrorMessageLock);
3860 m2 = poin[S->tree[i]];
3861 m3 = ( poin[S->tree[i]] -= r31 );
3862 do { *m3++ = *m2++; }
while ( m2 < m1 );
3866 *(poin[S->tree[i]]) += r31;
3868 m2 = (WORD *)coef; im = r3;
3879 r1 = poin2[k][1] - im + 1;
3881 m2 = poin[k] - im + 1;
3882 while ( ++im <= 0 ) *--m1 = *--m2;
3889 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
3892 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
3894 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
3895 S->Patches[ki],&(poin[k]),ki));
3897 poin2[k] = poin[k] + im;
3905 else if ( S->tree[i] < 0 ) {
3916 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD poin[k]); }
3919 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
3920 MLOCK(ErrorMessageLock);
3921 MesPrint(
"Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
3922 MUNLOCK(ErrorMessageLock);
3925 ADDPOS(S->SizeInFile[par],im);
3933 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3936 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3937 ADDPOS(S->SizeInFile[par],1);
3941 SeekFile(fout->
handle,&position,SEEK_CUR);
3944 S->fPatches[S->fPatchN] = position;
3946 if ( par == 0 && fout != AR.outfile ) {
3955 S->fPatches[S->fPatchN] = position;
3956 if ( ISNOTZEROPOS(AN.OldPosIn) ) {
3958 SeekFile(fin->
handle,&(AN.OldPosIn),SEEK_SET);
3964 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
3966 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&AN.OldPosIn),(LONG)
sizeof(
POSITION)) !=
3968 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
3970 MLOCK(ErrorMessageLock);
3971 MesPrint(
"Read error fourth stage sorting");
3972 MUNLOCK(ErrorMessageLock);
3977 for ( i = 0; i < S->inNum; i++ ) {
3978 S->fPatchesStop[i] = S->iPatches[i+1];
3980 MLOCK(ErrorMessageLock);
3981 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3982 MUNLOCK(ErrorMessageLock);
4001 MLOCK(ErrorMessageLock);
4002 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4003 MUNLOCK(ErrorMessageLock);
4013 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4014 PUTZERO(S->SizeInFile[0]);
4025 MLOCK(ErrorMessageLock);
4026 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4027 MUNLOCK(ErrorMessageLock);
4032 AR.gzipCompress = oldgzipCompress;
4037 AR.gzipCompress = oldgzipCompress;
4042 MLOCK(ErrorMessageLock);
4043 MesPrint(
"Error while writing to file.");
4047 MLOCK(ErrorMessageLock);
4049 MesCall(
"MergePatches");
4050 MUNLOCK(ErrorMessageLock);
4052 AR.gzipCompress = oldgzipCompress;
4074 WORD **ss, *lfill, j, *t;
4076 LONG lSpace, sSpace, RetCode, over, tover;
4078 if ( ( ( AP.PreDebug & DUMPTOSORT ) == DUMPTOSORT ) && AR.sLevel == 0 ) {
4080 sprintf((
char *)(THRbuf),
"StoreTerm(%d)",AT.identity);
4081 PrintTerm(term,(
char *)(THRbuf));
4083 PrintTerm(term,
"StoreTerm");
4086 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
4087 S->sFill = *(S->PoinFill);
4088 if ( S->sTerms >= S->TermsInSmall || ( S->sFill + *term ) >= S->sTop ) {
4092 S->PolyFlag = ( AR.PolyFun != 0 ) ? AR.PolyFunType:0;
4093 tover = over = S->sTerms;
4104 S->TermsLeft -= over - RetCode;
4108 lSpace = sSpace + (S->lFill - S->lBuffer)
4109 - (AM.MaxTer/
sizeof(WORD))*((LONG)S->lPatch);
4110 SETBASEPOSITION(pp,lSpace);
4111 MULPOS(pp,
sizeof(WORD));
4112 if ( S->file.handle >= 0 ) {
4113 ADD2POS(pp,S->fPatches[S->fPatchN]);
4116 WORD oldLogHandle = AC.LogHandle;
4117 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4119 AC.LogHandle = oldLogHandle;
4121 if ( ( S->lPatch >= S->MaxPatches ) ||
4122 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer ) ) >= S->lTop ) ) {
4132 SETBASEPOSITION(pp,sSpace);
4133 MULPOS(pp,
sizeof(WORD));
4134 ADD2POS(pp,S->fPatches[S->fPatchN]);
4137 WORD oldLogHandle = AC.LogHandle;
4138 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4140 AC.LogHandle = oldLogHandle;
4143 S->lFill = S->lBuffer;
4145 S->Patches[S->lPatch++] = S->lFill;
4146 lfill = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
4149 while ( ( t = *ss++ ) != 0 ) {
4151 if ( j < 0 ) j = t[1] + 2;
4160 S->PoinFill = S->sPointer;
4161 *(S->PoinFill) = S->sFill = S->sBuffer;
4164 while ( --j >= 0 ) *S->sFill++ = *term++;
4168 *++S->PoinFill = S->sFill;
4173 MLOCK(ErrorMessageLock);
4174 MesCall(
"StoreTerm");
4175 MUNLOCK(ErrorMessageLock);
4193 if ( S->fPatchN >= S->MaxFpatches ) {
4196 MLOCK(ErrorMessageLock);
4198 MesPrint(
"StageSort in thread %d",identity);
4199 #elif defined(WITHMPI) 4200 MesPrint(
"StageSort in process %d",PF.me);
4202 MesPrint(
"StageSort");
4204 MUNLOCK(ErrorMessageLock);
4205 SeekFile(fout->
handle,&position,SEEK_END);
4210 if ( (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(S->fPatchN)),(LONG)
sizeof(WORD)) !=
4212 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(AN.OldPosOut)),(LONG)
sizeof(
POSITION)) !=
4214 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(S->fPatches),(LONG)(S->fPatchN+1)
4216 MLOCK(ErrorMessageLock);
4217 MesPrint(
"Write error while staging sort. Disk full?");
4218 MUNLOCK(ErrorMessageLock);
4221 AN.OldPosOut = position;
4222 fout->filesize = position;
4223 ADDPOS(fout->filesize,(S->fPatchN+2)*
sizeof(
POSITION) +
sizeof(WORD));
4224 fout->POposition = fout->filesize;
4225 S->fPatches[0] = fout->filesize;
4228 if ( AR.FoStage4[0].PObuffer == 0 ) {
4229 AR.FoStage4[0].PObuffer = (WORD *)Malloc1(AR.FoStage4[0].POsize*
sizeof(WORD)
4231 AR.FoStage4[0].POfill = AR.FoStage4[0].PObuffer;
4232 AR.FoStage4[0].POstop = AR.FoStage4[0].PObuffer
4233 + AR.FoStage4[0].POsize/
sizeof(WORD);
4235 AR.FoStage4[0].pthreadslock = dummylock;
4238 if ( AR.FoStage4[1].PObuffer == 0 ) {
4239 AR.FoStage4[1].PObuffer = (WORD *)Malloc1(AR.FoStage4[1].POsize*
sizeof(WORD)
4241 AR.FoStage4[1].POfill = AR.FoStage4[1].PObuffer;
4242 AR.FoStage4[1].POstop = AR.FoStage4[1].PObuffer
4243 + AR.FoStage4[1].POsize/
sizeof(WORD);
4245 AR.FoStage4[1].pthreadslock = dummylock;
4272 WORD *v, *s, *m, k, i;
4273 WORD *pScrat, *stop, *sv, error = 0;
4274 pScrat = AT.WorkPointer;
4275 if ( ( AT.WorkPointer + 8 * AM.MaxWildcards ) >= AT.WorkTop ) {
4276 MLOCK(ErrorMessageLock);
4278 MUNLOCK(ErrorMessageLock);
4286 while ( v < stop && (
4287 *v == FROMSET || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4288 while ( v < stop ) {
4290 if ( AM.Ordering[*v] < AM.Ordering[*m] ) {
4293 else if ( *v == *m ) {
4294 if ( v[2] < m[2] ) {
4297 else if ( v[2] == m[2] ) {
4300 if ( s < stop && ( *s == FROMSET
4301 || *s == SETTONUM || *s == LOADDOLLAR ) ) {
4302 if ( sv < stop && ( *sv == FROMSET
4303 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4304 if ( s[2] != sv[2] ) {
4306 MLOCK(ErrorMessageLock);
4307 MesPrint(
"&Wildcard set conflict");
4308 MUNLOCK(ErrorMessageLock);
4314 if ( sv < stop && ( *sv == FROMSET
4315 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4327 while ( v < stop && ( *v == FROMSET
4328 || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4334 while ( m < stop && ( *m == FROMSET
4335 || *m == SETTONUM || *m == LOADDOLLAR ) ) {
4342 while ( i < nw && ( w[i] < 0 || w[i] == FROMSET
4343 || w[i] == SETTONUM || w[i] == LOADDOLLAR ) ) i += w[i+1];
4345 AC.NwildC = k = WORDDIF(pScrat,AT.WorkPointer);
4365 int minnum = num, i;
4366 if ( AN.FunSorts ) {
4368 if ( AN.MaxFunSorts > 3 ) {
4369 minnum = (AN.MaxFunSorts+4)/2;
4373 else if ( minnum == 0 ) minnum = 1;
4374 for ( i = minnum; i < AN.NumFunSorts; i++ ) {
4377 if ( S->file.handle >= 0 ) {
4380 CloseFile(S->file.handle);
4381 S->file.handle = -1;
4382 remove(S->file.name);
4384 MLOCK(ErrorMessageLock);
4385 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4386 MUNLOCK(ErrorMessageLock);
4389 M_free(S,
"sorting struct");
4393 AN.MaxFunSorts = minnum;
4397 if ( S->file.handle >= 0 ) {
4400 CloseFile(S->file.handle);
4401 S->file.handle = -1;
4402 remove(S->file.name);
4404 MLOCK(ErrorMessageLock);
4405 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4406 MUNLOCK(ErrorMessageLock);
4412 for ( i = 0; i < 2; i++ ) {
4413 if ( AR.FoStage4[i].handle >= 0 ) {
4415 CloseFile(AR.FoStage4[i].handle);
4416 remove(AR.FoStage4[i].name);
4417 AR.FoStage4[i].handle = -1;
4419 MLOCK(ErrorMessageLock);
4420 MesPrint(
"%w CleanUpSort removed stage4 file %s",AR.FoStage4[i].name);
4421 MUNLOCK(ErrorMessageLock);
4438 if ( AR.sLevel >= 0 ) {
4440 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
int NormalModulus(UWORD *, WORD *)
LONG EndSort(PHEAD WORD *buffer, int par)
WORD StoreTerm(PHEAD WORD *term)
WORD Compare1(PHEAD WORD *term1, WORD *term2, WORD level)
WORD FlushOut(POSITION *position, FILEHANDLE *fi, int compr)
void CleanUpSort(int num)
int CompareSymbols(PHEAD WORD *term1, WORD *term2, WORD par)
LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
LONG PutIn(FILEHANDLE *file, POSITION *position, WORD *buffer, WORD **take, int npat)
VOID AddArgs(PHEAD WORD *s1, WORD *s2, WORD *m)
int PF_ISendSbuf(int to, int tag)
WORD PutOut(PHEAD WORD *term, POSITION *position, FILEHANDLE *fi, WORD ncomp)
WORD Sflush(FILEHANDLE *fi)
LONG ComPress(WORD **ss, LONG *n)
BRACKETINDEX * indexbuffer
VOID StageSort(FILEHANDLE *fout)
WORD AddPoly(PHEAD WORD **ps1, WORD **ps2)
WORD AddCoef(PHEAD WORD **ps1, WORD **ps2)
VOID WriteStats(POSITION *plspace, WORD par)
WORD CompCoef(WORD *, WORD *)
WORD MergePatches(WORD par)
WORD SortWild(WORD *w, WORD nw)