65extern LONG nummallocs;
77char *toterms[] = {
" ",
" >>",
"-->" };
96 LONG millitime, y = 0x7FFFFFFFL >> 1;
101 if ( AT.SS == AT.S0 && AC.StatsFlag ) {
103 if ( AC.ThreadStats == 0 && identity > 0 )
return;
104#elif defined(WITHMPI)
105 if ( AC.OldParallelStats )
return;
106 if ( ! AC.ProcessStats && PF.me != MASTER )
return;
108 if ( Expressions == 0 )
return;
111 if ( AC.ShortStatsMax == 0 )
return;
113 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
115 AR.ShortSortCount = 0;
118 MLOCK(ErrorMessageLock);
119 if ( AC.ShortStats ) {}
122 if ( identity > 0 ) {
123 MesPrint(
" Thread %d reporting",identity);
128#elif defined(WITHMPI)
129 if ( PF.me != MASTER ) {
130 MesPrint(
" Process %d reporting",PF.me);
145 use_wtime = AC.WTimeStatsFlag;
146#if defined(WITHPTHREADS)
147 if ( use_wtime && identity > 0 ) use_wtime = 0;
148#elif defined(WITHMPI)
149 if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
152 timepart = (WORD)(millitime%1000);
155 if ( AC.ShortStats ) {
156#if defined(WITHPTHREADS) || defined(WITHMPI)
158 if ( identity > 0 ) {
160 if ( PF.me != MASTER ) {
161 const int identity = PF.me;
163 if ( par == 0 || par == 2 ) {
164 SETBASEPOSITION(pp,y);
165 if ( ISLESSPOS(*plspace,pp) ) {
166 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%10p %s %s",identity,
167 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
168 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
177 SETBASEPOSITION(pp,y);
179 if ( ISLESSPOS(*plspace,pp) ) {
180 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%11p %s %s",identity,
181 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
182 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
186 if ( ISLESSPOS(*plspace,pp) ) {
187 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%12p %s %s",identity,
188 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
189 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
193 if ( ISLESSPOS(*plspace,pp) ) {
194 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%13p %s %s",identity,
195 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
196 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
200 if ( ISLESSPOS(*plspace,pp) ) {
201 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%14p %s %s",identity,
202 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
203 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
207 if ( ISLESSPOS(*plspace,pp) ) {
208 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%15p %s %s",identity,
209 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
210 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
214 if ( ISLESSPOS(*plspace,pp) ) {
215 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%16p %s %s",identity,
216 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
217 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
221 if ( ISLESSPOS(*plspace,pp) ) {
222 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%17p %s %s",identity,
223 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
224 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
230 else if ( par == 1 ) {
231 SETBASEPOSITION(pp,y);
232 if ( ISLESSPOS(*plspace,pp) ) {
233 MesPrint(
"%d: %7l.%2is %10l:%10p",identity,millitime,timepart,
234 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
238 SETBASEPOSITION(pp,y);
240 if ( ISLESSPOS(*plspace,pp) ) {
241 MesPrint(
"%d: %7l.%2is %10l:%11p",identity,millitime,timepart,
242 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
246 if ( ISLESSPOS(*plspace,pp) ) {
247 MesPrint(
"%d: %7l.%2is %10l:%12p",identity,millitime,timepart,
248 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
252 if ( ISLESSPOS(*plspace,pp) ) {
253 MesPrint(
"%d: %7l.%2is %10l:%13p",identity,millitime,timepart,
254 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
258 if ( ISLESSPOS(*plspace,pp) ) {
259 MesPrint(
"%d: %7l.%2is %10l:%14p",identity,millitime,timepart,
260 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
264 if ( ISLESSPOS(*plspace,pp) ) {
265 MesPrint(
"%d: %7l.%2is %10l:%15p",identity,millitime,timepart,
266 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
270 if ( ISLESSPOS(*plspace,pp) ) {
271 MesPrint(
"%d: %7l.%2is %10l:%16p",identity,millitime,timepart,
272 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
276 if ( ISLESSPOS(*plspace,pp) ) {
277 MesPrint(
"%d: %7l.%2is %10l:%17p",identity,millitime,timepart,
278 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
286 if ( par == 0 || par == 2 ) {
287 SETBASEPOSITION(pp,y);
288 if ( ISLESSPOS(*plspace,pp) ) {
289 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%10p %s %s",
290 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
291 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
300 SETBASEPOSITION(pp,y);
302 if ( ISLESSPOS(*plspace,pp) ) {
303 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%11p %s %s",
304 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
305 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
309 if ( ISLESSPOS(*plspace,pp) ) {
310 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%12p %s %s",
311 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
312 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
316 if ( ISLESSPOS(*plspace,pp) ) {
317 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%13p %s %s",
318 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
319 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
323 if ( ISLESSPOS(*plspace,pp) ) {
324 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%14p %s %s",
325 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
326 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
330 if ( ISLESSPOS(*plspace,pp) ) {
331 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%15p %s %s",
332 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
333 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
337 if ( ISLESSPOS(*plspace,pp) ) {
338 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%16p %s %s",
339 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
340 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
344 if ( ISLESSPOS(*plspace,pp) ) {
345 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%17p %s %s",
346 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
347 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
353 else if ( par == 1 ) {
354 SETBASEPOSITION(pp,y);
355 if ( ISLESSPOS(*plspace,pp) ) {
356 MesPrint(
"%7l.%2is %10l:%10p",millitime,timepart,
357 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
361 SETBASEPOSITION(pp,y);
363 if ( ISLESSPOS(*plspace,pp) ) {
364 MesPrint(
"%7l.%2is %10l:%11p",millitime,timepart,
365 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
369 if ( ISLESSPOS(*plspace,pp) ) {
370 MesPrint(
"%7l.%2is %10l:%12p",millitime,timepart,
371 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
375 if ( ISLESSPOS(*plspace,pp) ) {
376 MesPrint(
"%7l.%2is %10l:%13p",millitime,timepart,
377 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
381 if ( ISLESSPOS(*plspace,pp) ) {
382 MesPrint(
"%7l.%2is %10l:%14p",millitime,timepart,
383 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
387 if ( ISLESSPOS(*plspace,pp) ) {
388 MesPrint(
"%7l.%2is %10l:%15p",millitime,timepart,
389 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
393 if ( ISLESSPOS(*plspace,pp) ) {
394 MesPrint(
"%7l.%2is %10l:%16p",millitime,timepart,
395 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
399 if ( ISLESSPOS(*plspace,pp) ) {
400 MesPrint(
"%7l.%2is %10l:%17p",millitime,timepart,
401 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
411 MesPrint(
"WTime = %7l.%2i sec",millitime,timepart);
414 MesPrint(
"Time = %7l.%2i sec",millitime,timepart);
418#if ( BITSINLONG > 32 )
419 if ( S->GenTerms >= 10000000000L ) {
421 MesPrint(
"WTime = %7l.%2i sec Generated terms = %16l",
422 millitime,timepart,S->GenTerms);
425 MesPrint(
"Time = %7l.%2i sec Generated terms = %16l",
426 millitime,timepart,S->GenTerms);
431 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
432 millitime,timepart,S->GenTerms);
435 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
436 millitime,timepart,S->GenTerms);
441 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
442 millitime,timepart,S->GenTerms);
445 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
446 millitime,timepart,S->GenTerms);
450#if ( BITSINLONG > 32 )
452 if ( S->TermsLeft >= 10000000000L ) {
453 MesPrint(
"%16s%8l Terms %s = %16l",EXPRNAME(AR.CurExpr),
454 AN.ninterms,FG.swmes[par],S->TermsLeft);
457 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
458 AN.ninterms,FG.swmes[par],S->TermsLeft);
461 if ( S->TermsLeft >= 10000000000L ) {
463 if ( identity > 0 && par == 2 ) {
464 MesPrint(
"%16s Terms in thread = %16l",
465 EXPRNAME(AR.CurExpr),S->TermsLeft);
468#elif defined(WITHMPI)
469 if ( PF.me != MASTER && par == 2 ) {
470 MesPrint(
"%16s Terms in process= %16l",
471 EXPRNAME(AR.CurExpr),S->TermsLeft);
476 MesPrint(
"%16s Terms %s = %16l",
477 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
482 if ( identity > 0 && par == 2 ) {
483 MesPrint(
"%16s Terms in thread = %10l",
484 EXPRNAME(AR.CurExpr),S->TermsLeft);
487#elif defined(WITHMPI)
488 if ( PF.me != MASTER && par == 2 ) {
489 MesPrint(
"%16s Terms in process= %10l",
490 EXPRNAME(AR.CurExpr),S->TermsLeft);
495 MesPrint(
"%16s Terms %s = %10l",
496 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
502 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
503 AN.ninterms,FG.swmes[par],S->TermsLeft);
506 if ( identity > 0 && par == 2 ) {
507 MesPrint(
"%16s Terms in thread = %10l",
508 EXPRNAME(AR.CurExpr),S->TermsLeft);
511#elif defined(WITHMPI)
512 if ( PF.me != MASTER && par == 2 ) {
513 MesPrint(
"%16s Terms in process= %10l",
514 EXPRNAME(AR.CurExpr),S->TermsLeft);
519 MesPrint(
"%16s Terms %s = %10l",
520 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
524 SETBASEPOSITION(pp,y);
525 if ( ISLESSPOS(*plspace,pp) ) {
526 MesPrint(
"%24s Bytes used = %10p",AC.Commercial,plspace);
530 SETBASEPOSITION(pp,y);
532 if ( ISLESSPOS(*plspace,pp) ) {
533 MesPrint(
"%24s Bytes used =%11p",AC.Commercial,plspace);
537 if ( ISLESSPOS(*plspace,pp) ) {
538 MesPrint(
"%24s Bytes used =%12p",AC.Commercial,plspace);
542 if ( ISLESSPOS(*plspace,pp) ) {
543 MesPrint(
"%24s Bytes used =%13p",AC.Commercial,plspace);
547 if ( ISLESSPOS(*plspace,pp) ) {
548 MesPrint(
"%24s Bytes used =%14p",AC.Commercial,plspace);
552 if ( ISLESSPOS(*plspace,pp) ) {
553 MesPrint(
"%24s Bytes used =%15p",AC.Commercial,plspace);
557 if ( ISLESSPOS(*plspace,pp) ) {
558 MesPrint(
"%24s Bytes used =%16p",AC.Commercial,plspace);
562 if ( ISLESSPOS(*plspace,pp) ) {
563 MesPrint(
"%24s Bytes used=%17p",AC.Commercial,plspace);
569 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l"
570 ,numwrites,numreads,numseeks);
571 MesPrint(
"Total number of mallocs: %l, frees: %l"
572 ,nummallocs,numfrees);
574 MUNLOCK(ErrorMessageLock);
597 if ( AN.SoScratC == 0 )
598 AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*
sizeof(UWORD),
"NewSort");
600 if ( AR.sLevel >= AN.NumFunSorts ) {
601 if ( AN.NumFunSorts == 0 ) newsize = 100;
602 else newsize = 2*AN.NumFunSorts;
603 newFS = (
SORTING **)Malloc1((newsize+1)*
sizeof(
SORTING *),
"FunSort pointers");
604 for ( i = 0; i < AN.NumFunSorts; i++ ) newFS[i] = AN.FunSorts[i];
605 for ( ; i <= newsize; i++ ) newFS[i] = 0;
606 if ( AN.FunSorts ) M_free(AN.FunSorts,
"FunSort pointers");
607 AN.FunSorts = newFS; AN.NumFunSorts = newsize;
609 if ( AR.sLevel == 0 ) {
613 AN.FunSorts[0] = AT.S0;
614 if ( AR.PolyFun == 0 ) { AT.S0->PolyFlag = 0; }
615 else if ( AR.PolyFunType == 1 ) { AT.S0->PolyFlag = 1; }
616 else if ( AR.PolyFunType == 2 ) {
617 if ( AR.PolyFunExp == 2
618 || AR.PolyFunExp == 3 ) AT.S0->PolyFlag = 1;
619 else AT.S0->PolyFlag = 2;
621 AR.ShortSortCount = 0;
624 if ( AN.FunSorts[AR.sLevel] == 0 ) {
625 AN.FunSorts[AR.sLevel] = AllocSort(
626 AM.SLargeSize,AM.SSmallSize,AM.SSmallEsize,AM.STermsInSmall
627 ,AM.SMaxPatches,AM.SMaxFpatches,AM.SIOsize);
629 AN.FunSorts[AR.sLevel]->PolyFlag = 0;
631 AT.SS = S = AN.FunSorts[AR.sLevel];
632 S->sFill = S->sBuffer;
633 S->lFill = S->lBuffer;
636 S->GenTerms = S->TermsLeft = S->GenSpace = S->SpaceLeft = 0;
637 S->PoinFill = S->sPointer;
638 *S->PoinFill = S->sFill;
639 if ( AR.sLevel > 0 ) { S->PolyWise = 0; }
640 PUTZERO(S->SizeInFile[0]); PUTZERO(S->SizeInFile[1]); PUTZERO(S->SizeInFile[2]);
642 PUTZERO(S->file.POposition);
644 if ( AR.sLevel > AN.MaxFunSorts ) AN.MaxFunSorts = AR.sLevel;
686 WORD j, **ss, *to, *t;
687 LONG sSpace, over, tover, spare, retval = 0, jj;
690 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
692 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
695 oldoutfile = AR.outfile;
705 oldoutfile = AR.outfile;
724 PrintTime((UBYTE *)
"EndSort, before SplitMerge");
726 S->sPointer[
SplitMerge(BHEAD S->sPointer,S->sTerms)] = 0;
728 PrintTime((UBYTE *)
"Endsort, after SplitMerge");
731 tover = over = S->sTerms;
734 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
737 S->TermsLeft -= over - spare;
738 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
740 else if ( S != AT.S0 ) {
744 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
745 if ( AN.tryterm > 0 && ( (sSpace+1)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
746 to = TermMalloc(
"$-sort space");
749 LONG allocsp = sSpace+1;
750 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
751 allocsp = ((allocsp+7)/8)*8;
752 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
753 if ( AN.tryterm > 0 ) AN.tryterm = 0;
755 *((WORD **)buffer) = to;
757 while ( ( t = *ss++ ) != 0 ) {
758 j = *t;
while ( --j >= 0 ) *to++ = *t++;
766 while ( ( t = *ss++ ) != 0 ) {
768 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
769 MLOCK(ErrorMessageLock);
770 MesPrint(
"Sorted function argument too long.");
771 MUNLOCK(ErrorMessageLock);
772 retval = -1;
goto RetRetval;
774 while ( --j >= 0 ) *to++ = *t++;
784 *AR.CompressPointer = 0;
785 SeekScratch(AR.outfile,&position);
797 {
int oldgzipCompress = AR.gzipCompress;
802 while ( ( t = *ss++ ) != 0 ) {
803 if ( *t ) S->TermsLeft++;
805 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
808 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
809 retval = -1;
goto RetRetval;
814 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
818 retval = -1;
goto RetRetval;
821 AR.gzipCompress = oldgzipCompress;
825 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
828 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
830 DIFPOS(oldpos,position,oldpos);
831 S->SpaceLeft = BASEPOSITION(oldpos);
837 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
839 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
842 SETBASEPOSITION(pp,lSpace);
843 MULPOS(pp,
sizeof(WORD));
844 if ( S->file.handle >= 0 ) {
845 ADD2POS(pp,S->fPatches[S->fPatchN]);
848 WORD oldLogHandle = AC.LogHandle;
849 if ( AC.LogHandle >= 0 && AM.LogType && ( ( S->lPatch > 0 )
850 || S->file.handle >= 0 ) ) AC.LogHandle = -1;
851 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
WriteStats(&pp,0); }
852 AC.LogHandle = oldLogHandle;
854 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
855 if ( S->lPatch > 0 ) {
856 if ( ( S->lPatch >= S->MaxPatches ) ||
857 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
862 MLOCK(ErrorMessageLock);
863 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x"
864 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
865 MUNLOCK(ErrorMessageLock);
869 MLOCK(ErrorMessageLock);
871 MUNLOCK(ErrorMessageLock);
872 retval = -1;
goto RetRetval;
875 pp = S->SizeInFile[1];
876 MULPOS(pp,
sizeof(WORD));
881 WORD oldLogHandle = AC.LogHandle;
883 SETBASEPOSITION(pppp,0);
884 SeekFile(S->file.handle,&pppp,SEEK_CUR);
885 SeekFile(S->file.handle,&pp,SEEK_END);
886 SeekFile(S->file.handle,&pppp,SEEK_SET);
887 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
889 AC.LogHandle = oldLogHandle;
894 S->Patches[S->lPatch++] = S->lFill;
895 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
898 while ( ( t = *ss++ ) != 0 ) {
900 if ( j < 0 ) j = t[1] + 2;
901 while ( --j >= 0 ) *to++ = *t++;
906 if ( S->file.handle < 0 ) {
908 MLOCK(ErrorMessageLock);
910 MUNLOCK(ErrorMessageLock);
911 retval = -1;
goto RetRetval;
914 pp = S->SizeInFile[2];
915 MULPOS(pp,
sizeof(WORD));
917 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
923 if ( par == 2 && newout->handle >= 0 ) {
927 LOCK(newout->pthreadslock);
929 SeekFile(newout->handle,&zeropos,SEEK_SET);
930 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+
sizeof(WORD)*2
931 ,
"$-buffer reading");
932 if ( AN.tryterm > 0 ) AN.tryterm = 0;
933 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
934 BASEPOSITION(newout->filesize) ) {
935 MLOCK(ErrorMessageLock);
936 MesPrint(
"Error reading information for $ variable");
937 MUNLOCK(ErrorMessageLock);
938 M_free(to,
"$-buffer reading");
942 *((WORD **)buffer) = to;
943 retval /=
sizeof(WORD);
946 UNLOCK(newout->pthreadslock);
949 else if ( newout->handle >= 0 ) {
951 MLOCK(ErrorMessageLock);
952 MesPrint(
"(1)Output should fit inside a single term. Increase MaxTermSize?");
954 MUNLOCK(ErrorMessageLock);
955 retval = -1;
goto RetRetval;
958 t = newout->PObuffer;
960 jj = newout->POfill - t;
961 if ( AN.tryterm > 0 && ( (jj+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
962 to = TermMalloc(
"$-sort space");
966 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
967 allocsp = ((allocsp+7)/8)*8;
968 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
969 if ( AN.tryterm > 0 ) AN.tryterm = 0;
971 *((WORD **)buffer) = to;
975 j = newout->POfill - t;
977 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
979 if ( j > AM.MaxTer )
goto TooLarge;
987 MLOCK(ErrorMessageLock);
989 MUNLOCK(ErrorMessageLock);
990 retval = -1;
goto RetRetval;
993 pp = S->SizeInFile[1];
994 MULPOS(pp,
sizeof(WORD));
999 WORD oldLogHandle = AC.LogHandle;
1001 SETBASEPOSITION(pppp,0);
1002 SeekFile(S->file.handle,&pppp,SEEK_CUR);
1003 SeekFile(S->file.handle,&pp,SEEK_END);
1004 SeekFile(S->file.handle,&pppp,SEEK_SET);
1005 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
1007 AC.LogHandle = oldLogHandle;
1015 if ( newout->handle >= 0 )
goto TooLarge;
1016 t = newout->PObuffer;
1017 j = newout->POfill - t;
1019 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
1020 goto WorkSpaceError;
1021 if ( j > AM.MaxTer )
goto TooLarge;
1028 if ( S->file.handle >= 0 ) {
1030 MLOCK(ErrorMessageLock);
1031 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p"
1032 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1033 MUNLOCK(ErrorMessageLock);
1035 if ( S->lPatch <= 0 ) {
1037 position = S->fPatches[S->fPatchN];
1041 *AR.CompressPointer = 0;
1042 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
1043 S->fpcompressed[S->fPatchN] = 1;
1045 S->fpcompressed[S->fPatchN] = 0;
1046 SetupOutputGZIP(&(S->file));
1048 while ( ( t = *ss++ ) != 0 ) {
1049 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
1050 retval = -1;
goto RetRetval;
1053 if (
FlushOut(&position,&(S->file),1) ) {
1054 retval = -1;
goto RetRetval;
1057 S->fPatches[S->fPatchN] = position;
1060 MLOCK(ErrorMessageLock);
1061 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p"
1062 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1063 MUNLOCK(ErrorMessageLock);
1069 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1070 if ( S->file.handle >= 0 ) {
1071 SynchFile(S->file.handle);
1077 MLOCK(ErrorMessageLock);
1079 MUNLOCK(ErrorMessageLock);
1080 retval = -1;
goto RetRetval;
1084 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
1086 pp = S->SizeInFile[0];
1087 MULPOS(pp,
sizeof(WORD));
1095 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
1096 Expressions[AR.CurExpr].counter = S->TermsLeft;
1097 Expressions[AR.CurExpr].size = pp;
1100 if ( AR.sLevel == 0 ) {
1101 Expressions[AR.CurExpr].counter = S->TermsLeft;
1102 Expressions[AR.CurExpr].size = pp;
1106 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
1110 ClearSortGZIP(&(S->file));
1112 CloseFile(S->file.handle);
1113 S->file.handle = -1;
1114 remove(S->file.name);
1116 MLOCK(ErrorMessageLock);
1117 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
1118 MUNLOCK(ErrorMessageLock);
1121 AR.outfile = oldoutfile;
1123 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
1128 DeAllocFileHandle(newout);
1132 else if ( newout ) {
1133 if ( newout->handle >= 0 ) {
1134 MLOCK(ErrorMessageLock);
1135 MesPrint(
"(2)Output should fit inside a single term. Increase MaxTermSize?");
1137 MUNLOCK(ErrorMessageLock);
1140 else if ( newout->POfill > newout->PObuffer ) {
1146 j = newout->POfill-newout->PObuffer;
1147 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
1148 goto WorkSpaceError;
1150 to = buffer; t = newout->PObuffer;
1151 while ( j-- > 0 ) *to++ = *t++;
1155 DeAllocFileHandle(newout);
1159 else if ( par == 2 ) {
1161 if ( retval == 0 ) {
1162 if ( newout->handle >= 0 ) {
1170 LOCK(newout->pthreadslock);
1172 SeekFile(newout->handle,&position,SEEK_END);
1174 SeekFile(newout->handle,&zeropos,SEEK_SET);
1175 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)*3
1176 ,
"$-buffer reading");
1177 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1178 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
1179 BASEPOSITION(position) ) {
1180 MLOCK(ErrorMessageLock);
1181 MesPrint(
"Error reading information for $ variable");
1182 MUNLOCK(ErrorMessageLock);
1183 M_free(to,
"$-buffer reading");
1187 *((WORD **)buffer) = to;
1188 retval /=
sizeof(WORD);
1191 UNLOCK(newout->pthreadslock);
1198 LONG wsiz = newout->POfill - newout->PObuffer;
1199 if ( AN.tryterm > 0 && ( (wsiz+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
1200 to = TermMalloc(
"$-sort space");
1203 LONG allocsp = wsiz+2;
1204 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
1205 allocsp = ((allocsp+7)/8)*8;
1206 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-buffer reading");
1207 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1209 *((WORD **)buffer) = to; t = newout->PObuffer;
1215 DeAllocFileHandle(newout);
1221 DeAllocFileHandle(newout);
1232 MLOCK(ErrorMessageLock);
1235 MUNLOCK(ErrorMessageLock);
1266 from = buffer + ( file->POsize *
sizeof(UBYTE) )/
sizeof(WORD);
1268 if ( i*((LONG)(
sizeof(WORD))) > AM.MaxTer ) {
1269 MLOCK(ErrorMessageLock);
1270 MesPrint(
"Problems in PutIn");
1271 MUNLOCK(ErrorMessageLock);
1275 while ( --i >= 0 ) *--to = *--from;
1278 if ( ( RetCode = FillInputGZIP(file,position,(UBYTE *)buffer
1279 ,file->POsize,npat) ) < 0 ) {
1280 MLOCK(ErrorMessageLock);
1281 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1282 RetCode,file->POsize);
1283 MUNLOCK(ErrorMessageLock);
1288 LOCK(file->pthreadslock);
1290 SeekFile(file->
handle,position,SEEK_SET);
1291 if ( ( RetCode = ReadFile(file->
handle,(UBYTE *)buffer,file->POsize) ) < 0 ) {
1293 UNLOCK(file->pthreadslock);
1295 MLOCK(ErrorMessageLock);
1296 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1297 RetCode,file->POsize);
1298 MUNLOCK(ErrorMessageLock);
1302 UNLOCK(file->pthreadslock);
1324 int dobracketindex = 0;
1325 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1326 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1329 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1331 MLOCK(ErrorMessageLock);
1332 MesPrint(
"%w Sflush created scratch file %s",fi->name);
1333 MUNLOCK(ErrorMessageLock);
1335 fi->
handle = (WORD)RetCode;
1336 PUTZERO(fi->filesize);
1337 PUTZERO(fi->POposition);
1340 MLOCK(ErrorMessageLock);
1341 MesPrint(
"Cannot create scratch file %s",fi->name);
1342 MUNLOCK(ErrorMessageLock);
1347 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1348 && dobracketindex == 0 ) {
1349 if ( FlushOutputGZIP(fi) )
return(-1);
1350 fi->POfill = fi->PObuffer;
1356 LOCK(fi->pthreadslock);
1358 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1359 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1360 if ( WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) != size ) {
1362 UNLOCK(fi->pthreadslock);
1364 MLOCK(ErrorMessageLock);
1365 MesPrint(
"Write error while finishing sort. Disk full?");
1366 MUNLOCK(ErrorMessageLock);
1369 ADDPOS(fi->filesize,size);
1370 ADDPOS(fi->POposition,size);
1371 fi->POfill = fi->PObuffer;
1373 UNLOCK(fi->pthreadslock);
1408 WORD i, *p, ret, *r, *rr, j, k, first;
1409 int dobracketindex = 0;
1412 if ( AT.SS != AT.S0 ) {
1416 if ( ( i = *term ) <= 0 )
return(0);
1418 ADDPOS(*position,i*
sizeof(WORD));
1421 if ( p >= fi->POstop ) {
1423 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1425 MLOCK(ErrorMessageLock);
1426 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1427 MUNLOCK(ErrorMessageLock);
1429 fi->
handle = (WORD)RetCode;
1430 PUTZERO(fi->filesize);
1431 PUTZERO(fi->POposition);
1440 MLOCK(ErrorMessageLock);
1441 MesPrint(
"Cannot create scratch file %s",fi->name);
1442 MUNLOCK(ErrorMessageLock);
1447 LOCK(fi->pthreadslock);
1449 if ( fi == AR.hidefile ) {
1450 LOCK(AS.inputslock);
1452 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1453 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1454 if ( fi == AR.hidefile ) {
1455 UNLOCK(AS.inputslock);
1458 UNLOCK(fi->pthreadslock);
1460 MLOCK(ErrorMessageLock);
1461 MesPrint(
"Write error during sort. Disk full?");
1462 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1463 fi->POsize,fi->
handle,&(fi->POposition));
1464 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1465 MUNLOCK(ErrorMessageLock);
1468 ADDPOS(fi->filesize,fi->POsize);
1470 ADDPOS(fi->POposition,fi->POsize);
1471 if ( fi == AR.hidefile ) {
1472 UNLOCK(AS.inputslock);
1475 UNLOCK(fi->pthreadslock);
1478 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1484 }
while ( --i > 0 );
1485 fi->POfull = fi->POfill = p;
1488 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1489 MLOCK(ErrorMessageLock);
1491 sprintf((
char *)(THRbuf),
"PutOut(%d)",AT.identity);
1492 PrintTerm(term,(
char *)(THRbuf));
1494 PrintTerm(term,
"PutOut");
1496 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1497 MesPrint(
"File %s, position %p",fi->name,position);
1498 MUNLOCK(ErrorMessageLock);
1501 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1502 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1503 r = rr = AR.CompressPointer;
1504 first = j = k = ret = 0;
1505 if ( ( i = *term ) != 0 ) {
1508 if ( fi == AR.outfile || fi == AR.hidefile ) {
1509 MLOCK(ErrorMessageLock);
1510 MesPrint(
"Ran into precompressed term");
1511 MUNLOCK(ErrorMessageLock);
1516 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1517 if ( dobracketindex ) {
1518 PutBracketInIndex(BHEAD term,position);
1524 WORD *polystop, *sa;
1528 while ( polystop < sa && *polystop != AR.PolyFun ) {
1529 polystop += polystop[1];
1531 if ( polystop < sa ) {
1532 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1533 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1534 i--; j--; k--; p++; r++;
1538 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1545 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1565 r[-(ABS(r[-1]))] = 0;
1566 if ( r >= AR.ComprTop ) {
1567 MLOCK(ErrorMessageLock);
1568 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1569 MUNLOCK(ErrorMessageLock);
1574 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1576 if ( dobracketindex ) {
1577 PutBracketInIndex(BHEAD term,position);
1583 WORD *polystop, *sa;
1587 while ( polystop < sa && *polystop != AR.PolyFun ) {
1588 polystop += polystop[1];
1590 if ( polystop < sa ) {
1591 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1592 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1593 i--; j--; k--; p++; r++;
1597 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1601 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1606 if ( AR.PolyFunType == 2 ) {
1608 tstop = term + *term;
1609 tstop -= ABS(tstop[-1]);
1611 while ( t < tstop ) {
1612 if ( *t == AR.PolyFun ) {
1613 t[2] &= ~MUSTCLEANPRF;
1618 if ( dobracketindex ) {
1619 PutBracketInIndex(BHEAD term,position);
1623 ADDPOS(*position,i*
sizeof(WORD));
1626 if ( p >= fi->POstop ) {
1628 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1630 sbuf->fill[sbuf->active] = fi->POstop;
1632 p = fi->PObuffer = fi->POfill = fi->POfull =
1633 sbuf->buff[sbuf->active];
1634 fi->POstop = sbuf->stop[sbuf->active];
1640 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1642 MLOCK(ErrorMessageLock);
1643 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1644 MUNLOCK(ErrorMessageLock);
1646 fi->
handle = (WORD)RetCode;
1647 PUTZERO(fi->filesize);
1648 PUTZERO(fi->POposition);
1657 MLOCK(ErrorMessageLock);
1658 MesPrint(
"Cannot create scratch file %s",fi->name);
1659 MUNLOCK(ErrorMessageLock);
1664 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1665 && dobracketindex == 0 && fi->zsp != 0 ) {
1667 if ( PutOutputGZIP(fi) )
return(-1);
1674 LOCK(fi->pthreadslock);
1676 if ( fi == AR.hidefile ) {
1677 LOCK(AS.inputslock);
1679 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1680 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1681 if ( fi == AR.hidefile ) {
1682 UNLOCK(AS.inputslock);
1685 UNLOCK(fi->pthreadslock);
1687 MLOCK(ErrorMessageLock);
1688 MesPrint(
"Write error during sort. Disk full?");
1689 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1690 fi->POsize,fi->
handle,&(fi->POposition));
1691 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1692 MUNLOCK(ErrorMessageLock);
1695 ADDPOS(fi->filesize,fi->POsize);
1697 ADDPOS(fi->POposition,fi->POsize);
1698 if ( fi == AR.hidefile ) {
1699 UNLOCK(AS.inputslock);
1702 UNLOCK(fi->pthreadslock);
1705 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1713 if ( first == 2 ) *p++ = k;
1717 else *p++ = *term++;
1723 }
while ( --i > 0 );
1724 fi->POfull = fi->POfill = p;
1752 int dobracketindex = 0;
1756 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1757 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1759 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1761 if ( fi->POfill >= fi->POstop ){
1762 sbuf->fill[sbuf->active] = fi->POstop;
1764 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1765 fi->POstop = sbuf->stop[sbuf->active];
1767 *(fi->POfill)++ = 0;
1768 sbuf->fill[sbuf->active] = fi->POfill;
1770 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1771 fi->POstop = sbuf->stop[sbuf->active];
1775 if ( fi->POfill >= fi->POstop ) {
1777 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1779 MLOCK(ErrorMessageLock);
1780 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1781 MUNLOCK(ErrorMessageLock);
1783 PUTZERO(fi->filesize);
1784 PUTZERO(fi->POposition);
1785 fi->
handle = (WORD)RetCode;
1794 MLOCK(ErrorMessageLock);
1795 MesPrint(
"Cannot create scratch file %s",fi->name);
1796 MUNLOCK(ErrorMessageLock);
1801 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1802 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1803 if ( PutOutputGZIP(fi) )
return(-1);
1804 fi->POfill = fi->PObuffer;
1810 LOCK(fi->pthreadslock);
1812 if ( fi == AR.hidefile ) {
1813 LOCK(AS.inputslock);
1815 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1816 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1818 UNLOCK(fi->pthreadslock);
1820 if ( fi == AR.hidefile ) {
1821 UNLOCK(AS.inputslock);
1823 MLOCK(ErrorMessageLock);
1824 MesPrint(
"Write error while sorting. Disk full?");
1825 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1826 fi->POsize,fi->
handle,&(fi->POposition));
1827 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1828 MUNLOCK(ErrorMessageLock);
1831 ADDPOS(fi->filesize,fi->POsize);
1832 fi->POfill = fi->PObuffer;
1833 ADDPOS(fi->POposition,fi->POsize);
1834 if ( fi == AR.hidefile ) {
1835 UNLOCK(AS.inputslock);
1838 UNLOCK(fi->pthreadslock);
1841 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1847 *(fi->POfill)++ = 0;
1848 fi->POfull = fi->POfill;
1861 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1864 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1865 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1866 if ( FlushOutputGZIP(fi) )
return(-1);
1867 fi->POfill = fi->PObuffer;
1873 LOCK(fi->pthreadslock);
1875 if ( fi == AR.hidefile ) {
1876 LOCK(AS.inputslock);
1878 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1882 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1884 UNLOCK(fi->pthreadslock);
1886 if ( fi == AR.hidefile ) {
1887 UNLOCK(AS.inputslock);
1889 MLOCK(ErrorMessageLock);
1890 MesPrint(
"Write error while finishing sorting. Disk full?");
1891 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1892 size,fi->
handle,&(fi->POposition));
1893 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1894 MUNLOCK(ErrorMessageLock);
1897 ADDPOS(fi->filesize,size);
1898 ADDPOS(fi->POposition,size);
1899 fi->POfill = fi->PObuffer;
1900 if ( fi == AR.hidefile ) {
1901 UNLOCK(AS.inputslock);
1904 UNLOCK(fi->pthreadslock);
1907 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1913 if ( dobracketindex ) {
1914 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1915 if ( b->indexfill > 0 ) {
1916 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1920 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1921 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1925 LOCK(fi->pthreadslock);
1927 SeekFile(fi->
handle,position,SEEK_END);
1929 UNLOCK(fi->pthreadslock);
1933 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1939 ADDPOS(*position,
sizeof(WORD));
1970 OutCoef = AN.SoScratC;
1971 s1 = *ps1; s2 = *ps2;
1974 if ( AddRat(BHEAD (UWORD *)s1,l1,(UWORD *)s2,l2,OutCoef,&OutLen) ) {
1975 MLOCK(ErrorMessageLock);
1977 MUNLOCK(ErrorMessageLock);
1980 if ( AN.ncmod != 0 ) {
1981 if ( ( AC.modmode & POSNEG ) != 0 ) {
1988 j = ABS(OutLen); OutCoef[j] = 1;
1989 for ( i = 1; i < j; i++ ) OutCoef[j+i] = 0;
1991 else if ( BigLong(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
1992 SubPLon(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod),OutCoef,&OutLen);
1993 OutCoef[OutLen] = 1;
1994 for ( i = 1; i < OutLen; i++ ) OutCoef[OutLen+i] = 0;
1997 if ( !OutLen ) { *ps1 = *ps2 = 0;
return(0); }
1999 if ( OutLen < 0 ) i = - ( --OutLen );
2001 if ( l1 < 0 ) l1 = -l1;
2006 s2 = (WORD *)OutCoef;
2007 while ( --i > 0 ) *s1++ = *s2++;
2009 while ( --l1 >= 0 ) *s1++ = 0;
2012 if ( l2 < 0 ) l2 = -l2;
2017 s1 = (WORD *)OutCoef;
2018 while ( --i > 0 ) *s2++ = *s1++;
2020 while ( --l2 >= 0 ) *s2++ = 0;
2030 if ( (S->sFill + j) >= S->sTop2 ) {
2037 if ( l1 < 0 ) l1 = - l1;
2042 while ( s1 < t ) *s2++ = *s1++;
2043 s1 = (WORD *)OutCoef;
2044 while ( --i > 0 ) *s2++ = *s1++;
2050 if ( **ps1 > AM.MaxTer/((LONG)(
sizeof(WORD))) ) {
2051 MLOCK(ErrorMessageLock);
2052 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2053 AM.MaxTer/
sizeof(WORD));
2054 MUNLOCK(ErrorMessageLock);
2094 WORD *s1, *s2, *m, *w, *t, oldpw = S->PolyWise;
2095 s1 = *ps1 + S->PolyWise;
2096 s2 = *ps2 + S->PolyWise;
2101 if ( S->PolyFlag == 2 && AR.PolyFunExp != 2 && AR.PolyFunExp != 3 ) {
2102 WORD **oldSplitScratch = AN.SplitScratch;
2103 LONG oldSplitScratchSize = AN.SplitScratchSize;
2104 LONG oldInScratch = AN.InScratch;
2105 WORD oldtype = AR.SortType;
2106 if ( (WORD *)((UBYTE *)w + AM.MaxTer) >= AT.WorkTop ) {
2107 MLOCK(ErrorMessageLock);
2108 MesPrint(
"Program was adding polyratfun arguments");
2110 MUNLOCK(ErrorMessageLock);
2112 AR.SortType = SORTHIGHFIRST;
2114 AN.SplitScratch = AN.SplitScratch1;
2115 AN.SplitScratchSize = AN.SplitScratchSize1;
2116 AN.InScratch = AN.InScratch1;
2118 S->PolyWise = oldpw;
2119 AN.SplitScratch1 = AN.SplitScratch;
2120 AN.SplitScratchSize1 = AN.SplitScratchSize;
2121 AN.InScratch1 = AN.InScratch;
2122 AN.SplitScratch = oldSplitScratch;
2123 AN.SplitScratchSize = oldSplitScratchSize;
2124 AN.InScratch = oldInScratch;
2126 AR.SortType = oldtype;
2127 if ( w[1] <= FUNHEAD ||
2128 ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
2129 *ps1 = *ps2 = 0;
return(0);
2133 if ( w + s1[1] + s2[1] + 12 + ARGHEAD >= AT.WorkTop ) {
2134 MLOCK(ErrorMessageLock);
2135 MesPrint(
"Program was adding polyfun arguments");
2137 MUNLOCK(ErrorMessageLock);
2144 if ( w[1] <= FUNHEAD ) { *ps1 = *ps2 = 0;
return(0); }
2145 if ( w[1] <= s1[1] || w[1] <= s2[1] ) {
2146 if ( w[1] > s1[1] ) {
2155 while ( t < m ) *s1++ = *t++;
2156 **ps1 = WORDDIF(s1,(*ps1));
2165 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2167 MesPrint(
"------Garbage collection-------");
2169 AT.WorkPointer += w[1];
2173 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2176 MLOCK(ErrorMessageLock);
2177 AO.OutFill = AO.OutputLine = OutBuf;
2181 while ( --i >= 0 ) {
2182 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2185 AO.OutFill = AO.OutputLine = OutBuf;
2190 while ( --i >= 0 ) {
2191 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2194 AO.OutFill = AO.OutputLine = OutBuf;
2199 while ( --i >= 0 ) {
2200 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2203 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2204 MUNLOCK(ErrorMessageLock);
2206 MLOCK(ErrorMessageLock);
2207 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2208 MUNLOCK(ErrorMessageLock);
2222 while ( t < w ) *s2++ = *t++;
2226 if ( *m > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
2227 MLOCK(ErrorMessageLock);
2228 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2229 AM.MaxTer/
sizeof(WORD));
2230 MUNLOCK(ErrorMessageLock);
2242#define INSLENGTH(x) w[1] = FUNHEAD+ARGHEAD+x; w[FUNHEAD] = ARGHEAD+x;
2255 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2256 WORD tempterm[8+FUNHEAD];
2258 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2259 *m++ = 0; *m++ = 0; FILLARG(m)
2260 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2261 if ( s1[FUNHEAD] < 0 ) {
2262 if ( s2[FUNHEAD] < 0 ) {
2263 if ( s1[FUNHEAD] <= -FUNCTION ) {
2264 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2265 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2267 *m++ = 2; *m++ = 1; *m++ = 3;
2268 INSLENGTH(4+FUNHEAD)
2270 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2271 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2272 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2273 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2275 s2[FUNHEAD] = s1[FUNHEAD];
2278 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2280 *m++ = 1; *m++ = 1; *m++ = 3;
2281 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2283 *m++ = 1; *m++ = 1; *m++ = 3;
2284 INSLENGTH(8+2*FUNHEAD)
2286 else if ( s2[FUNHEAD] == -SYMBOL ) {
2287 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2288 *m++ = 1; *m++ = 1; *m++ = 3;
2289 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2291 *m++ = 1; *m++ = 1; *m++ = 3;
2292 INSLENGTH(12+FUNHEAD)
2296 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2297 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2299 *m++ = 1; *m++ = 1; *m++ = 3;
2300 INSLENGTH(8+FUNHEAD)
2303 else if ( s1[FUNHEAD] == -SYMBOL ) {
2304 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2305 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2306 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2307 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2311 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2312 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2313 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2314 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2315 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2316 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2317 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2321 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2322 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2323 *m++ = 1; *m++ = 1; *m++ = 3;
2324 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2326 *m++ = 1; *m++ = 1; *m++ = 3;
2327 INSLENGTH(12+FUNHEAD)
2331 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2332 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2333 *m++ = 1; *m++ = 1; *m++ = 3;
2338 if ( s2[FUNHEAD] <= -FUNCTION ) {
2340 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2341 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2343 *m++ = 1; *m++ = 1; *m++ = 3;
2344 INSLENGTH(8+FUNHEAD)
2346 else if ( s2[FUNHEAD] == -SYMBOL ) {
2348 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2349 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2350 *m++ = 1; *m++ = 1; *m++ = 3;
2355 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2356 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2357 else { i1 = (WORD)x1; i2 = 3; }
2358 if ( x1 && AN.ncmod != 0 ) {
2363 if ( Modulus(m) ) Terminate(-1);
2364 if ( *m == 0 ) w[1] = 0;
2366 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2384 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2391 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2404 if ( s1[FUNHEAD] <= -FUNCTION ) {
2405 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2407 *t++ = 1; *t++ = 1; *t++ = 3;
2409 else if ( s1[FUNHEAD] == -SYMBOL ) {
2410 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2411 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2412 *t++ = 1; *t++ = 1; *t++ = 3;
2415 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2416 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2432 tstop1 = s1 + s1[1];
2433 s1 += FUNHEAD+ARGHEAD;
2435 tstop2 = s2 + s2[1];
2436 s2 += FUNHEAD+ARGHEAD;
2440 oldPolyFlag = AT.SS->PolyFlag;
2441 AT.SS->PolyFlag = 0;
2442 while ( s1 < tstop1 && s2 < tstop2 ) {
2443 i1 = CompareTerms(s1,s2,(WORD)(-1));
2448 else if ( i1 < 0 ) {
2471 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2472 MLOCK(ErrorMessageLock);
2473 MesPrint(
"Addition of coefficients of PolyFun");
2474 MUNLOCK(ErrorMessageLock);
2484 *mm = WORDDIF(m,mm);
2485 if ( AN.ncmod != 0 ) {
2486 if ( Modulus(mm) ) Terminate(-1);
2493 while ( s1 < tstop1 ) *m++ = *s1++;
2494 while ( s2 < tstop2 ) *m++ = *s2++;
2495 w[1] = WORDDIF(m,w);
2496 w[FUNHEAD] = w[1] - FUNHEAD;
2497 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2498 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2499 else w[1] = FUNHEAD+2;
2500 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2503 AT.SS->PolyFlag = oldPolyFlag;
2540 WORD *stopper1, *stopper2, *t2;
2542 WORD *stopex1, *stopex2;
2545 WORD count = -1, localPoly, polyhit = -1;
2547 if ( AR.sLevel == 0 ) {
2551 if ( S->PolyFlag ) {
2559 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2560 S->PolyFlag = AR.PolyFunType;
2561 if ( AR.PolyFunType == 2 &&
2562 ( AR.PolyFunExp == 2 || AR.PolyFunExp == 3 ) ) S->PolyFlag = 1;
2564 else { localPoly = 0; }
2568 GETSTOP(term2,stopper2);
2571 while ( t1 < stopper1 && t2 < stopper2 ) {
2573 if ( *t1 == HAAKJE )
return(PREV(-1));
2574 if ( *t2 == HAAKJE )
return(PREV(1));
2575 if ( *t1 >= (FUNCTION-1) ) {
2576 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2577 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2578 if ( *t1 < FUNCTION )
return(PREV(1));
2579 if ( *t2 < FUNCTION )
return(PREV(-1));
2580 c1 = functions[*t1-FUNCTION].commute;
2581 c2 = functions[*t2-FUNCTION].commute;
2583 if ( c2 )
return(PREV(1));
2584 else return(PREV(*t2-*t1));
2587 if ( !c2 )
return(PREV(-1));
2588 else return(PREV(*t2-*t1));
2591 else return(PREV(*t2-*t1));
2598 if ( localPoly && c1 < FUNCTION ) {
2601 if ( c1 <= (FUNCTION-1)
2602 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec ) ) {
2603 if ( c1 == SYMBOL ) {
2604 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2605 && s1[-1] == 4 && s2[-1] == 4
2606 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2607 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2614 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2617 else if ( AR.SortType >= SORTPOWERFIRST ) {
2620 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2622 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2625 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2632 if ( AR.SortType==SORTLOWFIRST ) {
2633 return(PREV((s1[1]>0?-1:1)));
2636 return(PREV((s1[1]<0?-1:1)));
2641 if ( AR.SortType==SORTLOWFIRST ) {
2643 return(PREV((s1[1]<0?1:-1)));
2646 return(PREV((s2[1]<0?-1:1)));
2651 return(PREV((s1[1]<0?-1:1)));
2654 return(PREV((s2[1]<0?1:-1)));
2659 if ( *s1 != *s2 )
return(
2660 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2665 if ( AR.SortType==SORTLOWFIRST ) {
2666 return(PREV((s2[1]<0?-1:1)));
2669 return(PREV((s2[1]<0?1:-1)));
2673 else if ( c1 == DOTPRODUCT ) {
2674 if ( AR.SortType >= SORTPOWERFIRST ) {
2677 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2679 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2682 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2687 if ( s2 >= t2 )
return(PREV(1));
2688 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2690 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2692 if ( *s1 != *s2 )
return(
2693 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2696 if ( s2 < t2 )
return(PREV(-1));
2700 if ( s2 >= t2 )
return(PREV(1));
2701 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2704 if ( s2 < t2 )
return(PREV(-1));
2712 if ( localPoly && c1 == AR.PolyFun ) {
2714 if ( S->PolyFlag == 1 ) {
2716 if ( *s1 > 0 ) i1 = *s1;
2717 else if ( *s1 <= -FUNCTION ) i1 = 1;
2719 if ( *s2 > 0 ) i2 = *s2;
2720 else if ( *s2 <= -FUNCTION ) i2 = 1;
2722 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2727 WORD *u1, *u2, *ustop;
2729 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2736 ustop = u2 - ABS(u2[-1]);
2738 while ( u1 < ustop ) {
2739 if ( *u1 == INDEX )
goto NoPoly;
2746 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2753 ustop = u2 - ABS(u2[-1]);
2755 while ( u1 < ustop ) {
2756 if ( *u1 == INDEX )
goto NoPoly;
2763 S->PolyWise = WORDDIF(s1,term1);
2764 S->PolyWise -= FUNHEAD;
2770 S->PolyWise = localPoly = 0;
2773 else if ( AR.PolyFunType == 2 ) {
2774 WORD i1, i2, i1a, i2a;
2775 if ( *s1 > 0 ) i1 = *s1;
2776 else if ( *s1 <= -FUNCTION ) i1 = 1;
2778 if ( *s2 > 0 ) i2 = *s2;
2779 else if ( *s2 <= -FUNCTION ) i2 = 1;
2781 if ( s1[i1] > 0 ) i1a = s1[i1];
2782 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2784 if ( s2[i2] > 0 ) i2a = s2[i2];
2785 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2787 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2792 WORD *u1, *u2, *ustop;
2794 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2799 while ( u1 < s1+i1 ) {
2801 ustop = u2 - ABS(u2[-1]);
2803 while ( u1 < ustop ) {
2804 if ( *u1 == INDEX )
goto NoPoly;
2811 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2815 u1 = s1 +i1 + ARGHEAD;
2818 ustop = u2 - ABS(u2[-1]);
2820 while ( u1 < ustop ) {
2821 if ( *u1 == INDEX )
goto NoPoly;
2828 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2833 while ( u1 < s2+i2 ) {
2835 ustop = u2 - ABS(u2[-1]);
2837 while ( u1 < ustop ) {
2838 if ( *u1 == INDEX )
goto NoPoly;
2845 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2849 u1 = s2 + i2 + ARGHEAD;
2852 ustop = u2 - ABS(u2[-1]);
2854 while ( u1 < ustop ) {
2855 if ( *u1 == INDEX )
goto NoPoly;
2862 S->PolyWise = WORDDIF(s1,term1);
2863 S->PolyWise -= FUNHEAD;
2868 S->PolyWise = localPoly = 0;
2872 S->PolyWise = localPoly = 0;
2876 t1 = term1 + S->PolyWise;
2877 t2 = term2 + S->PolyWise;
2887 if ( s2 >= t2 )
return(PREV(-1));
2894 if ( AC.properorderflag ) {
2896 oldpolyflag = S->PolyFlag;
2898 if ( ( c2 = -CompArg(s1,s2) ) != 0 ) {
2899 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2901 S->PolyFlag = oldpolyflag;
2910 if ( s2 >= t2 )
return(PREV(-1));
2912 s1 += ARGHEAD; s2 += ARGHEAD;
2913 oldpolyflag = S->PolyFlag;
2915 while ( s1 < stopex1 ) {
2916 if ( s2 >= stopex2 ) {
2917 S->PolyFlag = oldpolyflag;
return(PREV(-1));
2919 if ( ( c2 = CompareTerms(s1,s2,(WORD)1) ) != 0 ) {
2920 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2925 S->PolyFlag = oldpolyflag;
2926 if ( s2 < stopex2 )
return(PREV(1));
2928 else return(PREV(1));
2931 if ( *s2 > 0 )
return(PREV(-1));
2932 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2933 if ( *s1 > -FUNCTION ) {
2934 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2940 if ( s2 < t2 )
return(PREV(1));
2944 if ( AR.SortType != SORTLOWFIRST ) {
2945 if ( t1 < stopper1 )
return(PREV(1));
2946 if ( t2 < stopper2 )
return(PREV(-1));
2949 if ( t1 < stopper1 )
return(PREV(-1));
2950 if ( t2 < stopper2 )
return(PREV(1));
2953 if ( level == 3 )
return(
CompCoef(term1,term2));
2980 WORD *t1, *t2, *tt1, *tt2;
2983 if ( AR.SortType == SORTLOWFIRST ) { low = 1; high = -1; }
2984 else { low = -1; high = 1; }
2985 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
2986 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
2987 if ( AN.polysortflag > 0 ) {
2989 while ( t1 < tt1 ) { sum1 += t1[1]; t1 += 2; }
2990 while ( t2 < tt2 ) { sum2 += t2[1]; t2 += 2; }
2991 if ( sum1 < sum2 )
return(low);
2992 if ( sum1 > sum2 )
return(high);
2993 t1 = term1+3; t2 = term2 + 3;
2995 while ( t1 < tt1 && t2 < tt2 ) {
2996 if ( *t1 > *t2 )
return(low);
2997 if ( *t1 < *t2 )
return(high);
2998 if ( t1[1] < t2[1] )
return(low);
2999 if ( t1[1] > t2[1] )
return(high);
3002 if ( t1 < tt1 )
return(high);
3003 if ( t2 < tt2 )
return(low);
3023 WORD *t1, *t2, *tt1, *tt2, *ttt1, *ttt2;
3025 DUMMYUSE(AT.WorkPointer);
3026 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
3027 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
3028 while ( t1 < tt1 && t2 < tt2 ) {
3030 if ( t1[0] < t2[0] )
return(-1);
3033 else if ( *t1 == HAAKJE ) {
3034 t1 += 3; t2 += 3;
continue;
3036 ttt1 = t1+t1[1]; ttt2 = t2+t2[1];
3037 while ( t1 < ttt1 && t2 < ttt2 ) {
3038 if ( *t1 > *t2 )
return(-1);
3039 if ( *t1 < *t2 )
return(1);
3040 if ( t1[1] < t2[1] )
return(-1);
3041 if ( t1[1] > t2[1] )
return(1);
3044 if ( t1 < ttt1 )
return(1);
3045 if ( t2 < ttt2 )
return(-1);
3047 if ( t1 < tt1 )
return(1);
3048 if ( t2 < tt2 )
return(-1);
3113 if ( AT.SS == AT.S0 && !AR.NoCompress ) {
3114 if ( AN.compressSize == 0 ) {
3115 if ( *ss ) { AN.compressSize = **ss + 64; }
3116 else { AN.compressSize = AM.MaxTer/
sizeof(WORD) + 2; }
3117 AN.compressSpace = (WORD *)Malloc1(AN.compressSize*
sizeof(WORD),
"Compression");
3119 AN.compressSpace[0] = 0;
3124 if ( j > AN.compressSize ) {
3126 t = (WORD *)Malloc1(newsize*
sizeof(WORD),
"Compression");
3128 if ( AN.compressSpace ) {
3129 for ( i = 0; i < *AN.compressSpace; i++ ) t[i] = AN.compressSpace[i];
3130 M_free(AN.compressSpace,
"Compression");
3132 AN.compressSpace = t;
3133 AN.compressSize = newsize;
3135 t = AN.compressSpace;
3139 WORD *polystop, *sa;
3143 while ( polystop < sa && *polystop != AR.PolyFun ) {
3144 polystop += polystop[1];
3146 while ( i > 0 && j > 0 && *s == *t && s < polystop ) {
3147 i--; j--; s++; t++; k--;
3154 while ( i > 0 && j > 0 && *s == *t && s < sa ) { i--; j--; s++; t++; k--; }
3163 size += *AN.compressSpace;
3164 if ( k == -1 ) { t--; s--; j++; }
3166 while ( --j >= 0 ) *t++ = *s++;
3168 t = AN.compressSpace + *AN.compressSpace;
3169 t[-(ABS(t[-1]))] = 0;
3244 WORD **pp3, **pp1, **pp2;
3245 LONG i, newleft, newright, split;
3247 if ( number < 2 )
return(number);
3248 if ( number == 2 ) {
3249 pp1 = Pointer; pp2 = pp1 + 1;
3250 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3251 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3253 else if ( i == 0 ) {
3255 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) number = 0; }
3256 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) number = 0; }
3262 newright =
SplitMerge(BHEAD Pointer+split,number-split);
3263 if ( newright == 0 )
return(newleft);
3271 if ( newleft > 0 && newright > 0 &&
3272 ( i = CompareTerms(Pointer[newleft-1],Pointer[split],(WORD)0) ) >= 0 ) {
3273 pp2 = Pointer+split; pp1 = Pointer+newleft-1;
3275 if ( S->PolyWise ) {
3276 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3280 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3286 newleft += newright;
3288 while ( --newright >= 0 ) *pp1++ = *pp2++;
3293 if ( split >= AN.SplitScratchSize ) {
3294 AN.SplitScratchSize = (split*3)/2+100;
3295 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3296 AN.SplitScratchSize = S->Terms2InSmall/2;
3297 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3298 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3300 pp3 = AN.SplitScratch; pp1 = Pointer;
3301 for ( i = 0; i < newleft; i++ ) *pp3++ = *pp1++;
3302 AN.InScratch = newleft;
3303 pp1 = AN.SplitScratch; pp2 = Pointer + split; pp3 = Pointer;
3307 while ( newleft > 8 ) {
3308 LONG nnleft = newleft/2;
3309 if ( ( i = CompareTerms(pp1[nnleft],*pp2,(WORD)0) ) < 0 )
break;
3312 newleft -= nnleft+1;
3314 if ( S->PolyWise ) { i =
AddPoly(BHEAD pp3-1,pp2); }
3315 else { i =
AddCoef(BHEAD pp3-1,pp2); }
3316 if ( i == 0 ) pp3--;
3323 while ( newleft > 0 && newright > 0 ) {
3324 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3333 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3334 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3335 pp1++; pp2++; newleft--; newright--;
3338 for ( i = 0; i < newleft; i++ ) *pp3++ = *pp1++;
3342 for ( i = 0; i < newright; i++ ) *pp3++ = *pp2++;
3345 return(pp3 - Pointer);
3350LONG
SplitMerge(PHEAD WORD **Pointer, LONG number)
3354 WORD **pp3, **pp1, **pp2;
3355 LONG nleft, nright, i, newleft, newright;
3358 if ( number < 2 )
return(number);
3359 if ( number == 2 ) {
3360 pp1 = Pointer; pp2 = pp1 + 1;
3361 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3362 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3364 else if ( i == 0 ) {
3366 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3367 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3371 pptop = Pointer + number;
3372 nleft = number >> 1; nright = number - nleft;
3374 newright =
SplitMerge(BHEAD Pointer+nleft,nright);
3382 if ( newleft > 0 && newright > 0 &&
3383 ( i = CompareTerms(Pointer[newleft-1],Pointer[nleft],(WORD)0) ) >= 0 ) {
3384 pp2 = Pointer+nleft; pp1 = Pointer+newleft-1;
3386 if ( S->PolyWise ) {
3387 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3391 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3394 *pp2++ = 0; newright--;
3397 newleft += newright;
3399 while ( --newright >= 0 ) *pp1++ = *pp2++;
3400 while ( pp1 < pptop ) *pp1++ = 0;
3404 if ( nleft > AN.SplitScratchSize ) {
3405 AN.SplitScratchSize = (nleft*3)/2+100;
3406 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3407 AN.SplitScratchSize = S->Terms2InSmall/2;
3408 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3409 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3411 pp3 = AN.SplitScratch; pp1 = Pointer; i = nleft;
3412 do { *pp3++ = *pp1; *pp1++ = 0; }
while ( *pp1 && --i > 0 );
3413 if ( i > 0 ) { *pp3 = 0; i--; }
3414 AN.InScratch = nleft - i;
3415 pp1 = AN.SplitScratch; pp2 = Pointer + nleft; pp3 = Pointer;
3416 while ( nleft > 0 && nright > 0 && *pp1 && *pp2 ) {
3417 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3428 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3429 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3430 *pp1++ = 0; *pp2++ = 0; nleft--; nright--;
3433 while ( --nleft >= 0 && *pp1 ) { *pp3++ = *pp1; *pp1++ = 0; }
3434 while ( --nright >= 0 && *pp2 ) { *pp3++ = *pp2++; }
3435 nleft = pp3 - Pointer;
3436 while ( pp3 < pptop ) *pp3++ = 0;
3466 WORD **Point, *s2, *t, *garbuf, i;
3468 int tobereturned = 0;
3473 MLOCK(ErrorMessageLock);
3474 MesPrint(
"in: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3476 Point = S->sPointer;
3478 while ( --k >= 0 ) {
3479 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3481 Point = AN.SplitScratch;
3483 while ( --k >= 0 ) {
3484 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3487 MesPrint(
"total = %l, nterms = %l",2*total,AN.InScratch);
3488 MUNLOCK(ErrorMessageLock);
3494 if ( S->lBuffer != 0 && S->lFill + total <= S->lTop ) {
3498 garbuf = (WORD *)Malloc1(total*
sizeof(WORD),
"Garbage buffer");
3502 Point = S->sPointer;
3504 while ( --k >= 0 ) {
3512 Point = AN.SplitScratch;
3514 while ( --k >= 0 ) {
3524 Point = S->sPointer;
3526 while ( --k >= 0 ) {
3534 Point = AN.SplitScratch;
3536 while ( --k >= 0 ) {
3546 MLOCK(ErrorMessageLock);
3547 MesPrint(
"out: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3548 if ( S->sFill >= S->sTop2 ) {
3549 MesPrint(
"We are in deep trouble");
3551 MUNLOCK(ErrorMessageLock);
3553 if ( tobereturned ) M_free(garbuf,
"Garbage buffer");
3581 WORD **poin, **poin2, ul, k, i, im, *m1;
3582 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3583 WORD *m2, *m3, r31, r33, ki, *rr;
3593 int oldgzipCompress = AR.gzipCompress;
3595 AR.gzipCompress = 0;
3599 fout = &(AR.FoStage4[0]);
3602 poin = S->poina; poin2 = S->poin2a;
3603 rr = AR.CompressPointer;
3610 if ( fout->
handle < 0 ) {
3612 PUTZERO(AN.OldPosOut);
3613 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3614 MLOCK(ErrorMessageLock);
3615 MesPrint(
"Cannot create file %s",fout->name);
3616 MUNLOCK(ErrorMessageLock);
3620 MLOCK(ErrorMessageLock);
3621 MesPrint(
"%w MergePatches created output file %s",fout->name);
3622 MUNLOCK(ErrorMessageLock);
3625 PUTZERO(fout->filesize);
3626 PUTZERO(fout->POposition);
3634 LOCK(fout->pthreadslock);
3636 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3638 UNLOCK(fout->pthreadslock);
3641 PUTZERO(S->fPatches[0]);
3642 fout->POfill = fout->PObuffer;
3643 PUTZERO(fout->POposition);
3648 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3649 S->fpcompressed[S->fPatchN] = 1;
3651 S->fpcompressed[S->fPatchN] = 0;
3652 SetupOutputGZIP(fout);
3655 else if ( par == 0 && S->stage4 > 0 ) {
3667 S->iPatches = S->fPatches;
3668 S->fPatches = S->inPatches;
3669 S->inPatches = S->iPatches;
3670 (S->inNum) = S->fPatchN;
3671 AN.OldPosIn = AN.OldPosOut;
3673 m1 = S->fpincompressed;
3674 S->fpincompressed = S->fpcompressed;
3675 S->fpcompressed = m1;
3676 for ( i = 0; i < S->inNum; i++ ) {
3677 S->fPatchesStop[i] = S->iPatches[i+1];
3679 MLOCK(ErrorMessageLock);
3680 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3681 MUNLOCK(ErrorMessageLock);
3693 AR.gzipCompress = 0;
3696 S->iPatches = S->fPatches;
3697 S->inNum = S->fPatchN;
3699 m1 = S->fpincompressed;
3700 S->fpincompressed = S->fpcompressed;
3701 S->fpcompressed = m1;
3702 for ( i = 0; i < S->inNum; i++ ) {
3703 S->fPatchesStop[i] = S->fPatches[i+1];
3705 MLOCK(ErrorMessageLock);
3706 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3707 MUNLOCK(ErrorMessageLock);
3715 S->Patches[S->lPatch] = S->lFill;
3716 for ( i = 0; i < S->lPatch; i++ ) {
3717 S->pStop[i] = S->Patches[i+1]-1;
3718 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3722 S->lPatch = (S->inNum);
3724 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3726 if ( S->lPatch > 1 ) {
3729 SetupAllInputGZIP(S);
3732 for ( i = 0; i < S->lPatch; i++ ) {
3733 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3735 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3736 S->pStop[i] = m2 = p;
3738 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3740 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3745 if ( fout->
handle >= 0 ) {
3748 LOCK(fout->pthreadslock);
3750 SeekFile(fout->
handle,&position,SEEK_END);
3751 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3753 UNLOCK(fout->pthreadslock);
3757 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3766 if ( S->lPatch == 1 ) {
3769 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3775 m2 = m1 = *S->Patches;
3778 i = -(*m1++); m2 += i; im = *m1+i+1;
3779 while ( i > 0 ) { *m1-- = *m2--; i--; }
3783 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD m1); }
3786 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3787 ADDPOS(S->SizeInFile[par],im);
3792 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3795 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3796 ADDPOS(S->SizeInFile[par],1);
3799 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3800 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3802 ADDPOS(position,length);
3803 ADDPOS(fout->POposition,length);
3804 ADDPOS(fout->filesize,length);
3805 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3815 #define FRONTSIZE (2*AM.MaxTer)
3816 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3818 SetupAllInputGZIP(S);
3820 position2 = S->iPatches[0];
3821 while ( ( length = FillInputGZIP(fin,&position2,
3823 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3824 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3825 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3826 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3834 i = -(*m1++); m2 += i; im = *m1+i+1;
3835 while ( i > 0 ) { *m1-- = *m2--; i--; }
3839 if ( AS.MasterSort && ( fout == AR.outfile ) ) {
3840 im = PutToMaster(BHEAD m1);
3844 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3845 ADDPOS(S->SizeInFile[par],im);
3849 if ( m1 < copytop && *m1 == 0 )
break;
3855 while ( m1 > m2 ) *--m3 = *--m1;
3860 MLOCK(ErrorMessageLock);
3861 MesPrint(
"Readerror");
3865 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3868 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3869 ADDPOS(S->SizeInFile[par],1);
3872 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3873 while ( ( length = ReadFile(fin->
handle,
3874 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3875 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3877 ADDPOS(position,length);
3878 ADDPOS(fout->POposition,length);
3879 ADDPOS(fout->filesize,length);
3880 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3883 MLOCK(ErrorMessageLock);
3884 MesPrint(
"Readerror");
3891 else if ( S->lPatch > 0 ) {
3896 do { lpat *= 2; }
while ( lpat < S->lPatch );
3897 mpat = ( lpat >> 1 ) - 1;
3898 k = lpat - S->lPatch;
3903 for ( i = 1; i < lpat; i++ ) {
3906 for ( i = 1; i <= k; i++ ) {
3908 poin[im] = S->Patches[i-1];
3909 poin2[im] = poin[im] + *(poin[im]);
3912 S->tree[mpat+i] = 0;
3913 poin[im-1] = poin2[im-1] = 0;
3915 for ( i = (k*2)+1; i <= lpat; i++ ) {
3918 poin[i] = S->Patches[i-k-1];
3919 poin2[i] = poin[i] + *(poin[i]);
3939 if ( !*(poin[k]) ) {
3940 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3941 if ( S->tree[i] == -1 ) {
3954 if ( S->tree[i] > 0 ) {
3955 if ( ( c = CompareTerms(poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3959 S->used[level] = S->tree[i];
3969 l1 = *( m1 = poin[S->tree[i]] );
3970 l2 = *( m2 = poin[k] );
3971 if ( S->PolyWise ) {
3976 if ( S->PolyFlag == 2 ) {
3978 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3979 MLOCK(ErrorMessageLock);
3980 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3981 MUNLOCK(ErrorMessageLock);
3988 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3989 MLOCK(ErrorMessageLock);
3990 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3991 MUNLOCK(ErrorMessageLock);
3998 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
4000 if ( r1 == m1[1] ) {
4003 else if ( r1 < m1[1] ) {
4007 while ( --r1 >= 0 ) *--m1 = *--m2;
4010 while ( --r1 >= 0 ) *--m1 = *--m2;
4012 poin[S->tree[i]] = m1;
4020 poin[S->tree[i]] = m2;
4027 r1 = *( m1 += l1 - 1 );
4029 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
4030 r2 = *( m2 += l2 - 1 );
4032 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
4034 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
4035 MLOCK(ErrorMessageLock);
4036 MesCall(
"MergePatches");
4037 MUNLOCK(ErrorMessageLock);
4041 if ( AN.ncmod != 0 ) {
4042 if ( ( AC.modmode & POSNEG ) != 0 ) {
4045 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
4047 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
4049 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
4053 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
4054 if ( r3 < 0 ) r3 = -r3;
4055 if ( r1 < 0 ) r1 = -r1;
4060 ul = S->used[level] = S->tree[i];
4067 r1 = poin2[ul][1] - im + 1;
4069 m2 = poin[ul] - im + 1;
4070 while ( ++im <= 0 ) *--m1 = *--m2;
4075 poin[ul] = poin2[ul];
4077 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
4080 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
4082 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4083 S->Patches[ki],&(poin[ul]),ki));
4085 poin2[ul] = poin[ul] + im;
4090 S->used[++level] = k;
4096 else if ( r31 < 0 ) {
4105 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
4111 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
4112 MLOCK(ErrorMessageLock);
4113 MesPrint(
"Coefficient overflow during sort");
4114 MUNLOCK(ErrorMessageLock);
4117 m2 = poin[S->tree[i]];
4118 m3 = ( poin[S->tree[i]] -= r31 );
4119 do { *m3++ = *m2++; }
while ( m2 < m1 );
4123 *(poin[S->tree[i]]) += r31;
4125 m2 = (WORD *)coef; im = r3;
4136 r1 = poin2[k][1] - im + 1;
4138 m2 = poin[k] - im + 1;
4139 while ( ++im <= 0 ) *--m1 = *--m2;
4146 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
4149 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
4151 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4152 S->Patches[ki],&(poin[k]),ki));
4154 poin2[k] = poin[k] + im;
4162 else if ( S->tree[i] < 0 ) {
4173 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD poin[k]); }
4176 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
4177 MLOCK(ErrorMessageLock);
4178 MesPrint(
"Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
4179 MUNLOCK(ErrorMessageLock);
4182 ADDPOS(S->SizeInFile[par],im);
4190 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
4193 if (
FlushOut(&position,fout,1) )
goto ReturnError;
4194 ADDPOS(S->SizeInFile[par],1);
4198 SeekFile(fout->
handle,&position,SEEK_CUR);
4201 S->fPatches[S->fPatchN] = position;
4203 if ( par == 0 && fout != AR.outfile ) {
4212 S->fPatches[S->fPatchN] = position;
4213 if ( ISNOTZEROPOS(AN.OldPosIn) ) {
4215 SeekFile(fin->
handle,&(AN.OldPosIn),SEEK_SET);
4221 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
4223 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&AN.OldPosIn),(LONG)
sizeof(
POSITION)) !=
4225 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
4227 MLOCK(ErrorMessageLock);
4228 MesPrint(
"Read error fourth stage sorting");
4229 MUNLOCK(ErrorMessageLock);
4234 for ( i = 0; i < S->inNum; i++ ) {
4235 S->fPatchesStop[i] = S->iPatches[i+1];
4237 MLOCK(ErrorMessageLock);
4238 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
4239 MUNLOCK(ErrorMessageLock);
4261 MLOCK(ErrorMessageLock);
4262 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4263 MUNLOCK(ErrorMessageLock);
4273 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4274 PUTZERO(S->SizeInFile[0]);
4288 MLOCK(ErrorMessageLock);
4289 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4290 MUNLOCK(ErrorMessageLock);
4295 AR.gzipCompress = oldgzipCompress;
4300 AR.gzipCompress = oldgzipCompress;
4305 MLOCK(ErrorMessageLock);
4306 MesPrint(
"Error while writing to file.");
4310 MLOCK(ErrorMessageLock);
4312 MesCall(
"MergePatches");
4313 MUNLOCK(ErrorMessageLock);
4315 AR.gzipCompress = oldgzipCompress;
4337 WORD **ss, *lfill, j, *t;
4339 LONG lSpace, sSpace, RetCode, over, tover;
4341 if ( ( ( AP.PreDebug & DUMPTOSORT ) == DUMPTOSORT ) && AR.sLevel == 0 ) {
4343 sprintf((
char *)(THRbuf),
"StoreTerm(%d)",AT.identity);
4344 PrintTerm(term,(
char *)(THRbuf));
4346 PrintTerm(term,
"StoreTerm");
4349 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
4350 S->sFill = *(S->PoinFill);
4351 if ( S->sTerms >= S->TermsInSmall || ( S->sFill + *term ) >= S->sTop ) {
4355 tover = over = S->sTerms;
4359 PrintTime((UBYTE *)
"Before SplitMerge");
4363 PrintTime((UBYTE *)
"After SplitMerge");
4368 S->TermsLeft -= over - RetCode;
4372 lSpace = sSpace + (S->lFill - S->lBuffer)
4373 - (AM.MaxTer/
sizeof(WORD))*((LONG)S->lPatch);
4374 SETBASEPOSITION(pp,lSpace);
4375 MULPOS(pp,
sizeof(WORD));
4376 if ( S->file.handle >= 0 ) {
4377 ADD2POS(pp,S->fPatches[S->fPatchN]);
4380 WORD oldLogHandle = AC.LogHandle;
4381 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4383 AC.LogHandle = oldLogHandle;
4385 if ( ( S->lPatch >= S->MaxPatches ) ||
4386 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer ) ) >= S->lTop ) ) {
4396 SETBASEPOSITION(pp,sSpace);
4397 MULPOS(pp,
sizeof(WORD));
4398 ADD2POS(pp,S->fPatches[S->fPatchN]);
4401 WORD oldLogHandle = AC.LogHandle;
4402 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4404 AC.LogHandle = oldLogHandle;
4407 S->lFill = S->lBuffer;
4409 S->Patches[S->lPatch++] = S->lFill;
4410 lfill = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
4413 while ( ( t = *ss++ ) != 0 ) {
4415 if ( j < 0 ) j = t[1] + 2;
4424 S->PoinFill = S->sPointer;
4425 *(S->PoinFill) = S->sFill = S->sBuffer;
4428 while ( --j >= 0 ) *S->sFill++ = *term++;
4432 *++S->PoinFill = S->sFill;
4437 MLOCK(ErrorMessageLock);
4438 MesCall(
"StoreTerm");
4439 MUNLOCK(ErrorMessageLock);
4457 if ( S->fPatchN >= S->MaxFpatches ) {
4472 MLOCK(ErrorMessageLock);
4473 MesPrint(
"Currently Stage 4 sorts are not allowed for function arguments or $ variables.");
4474 MesPrint(
"Please increase correspondingsorting parameters (sub-) in the setup.");
4475 MUNLOCK(ErrorMessageLock);
4479 MLOCK(ErrorMessageLock);
4481 MesPrint(
"StageSort in thread %d",identity);
4482#elif defined(WITHMPI)
4483 MesPrint(
"StageSort in process %d",PF.me);
4485 MesPrint(
"StageSort");
4487 MUNLOCK(ErrorMessageLock);
4488 SeekFile(fout->
handle,&position,SEEK_END);
4493 if ( (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(S->fPatchN)),(LONG)
sizeof(WORD)) !=
4495 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(AN.OldPosOut)),(LONG)
sizeof(
POSITION)) !=
4497 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(S->fPatches),(LONG)(S->fPatchN+1)
4499 MLOCK(ErrorMessageLock);
4500 MesPrint(
"Write error while staging sort. Disk full?");
4501 MUNLOCK(ErrorMessageLock);
4504 AN.OldPosOut = position;
4505 fout->filesize = position;
4506 ADDPOS(fout->filesize,(S->fPatchN+2)*
sizeof(
POSITION) +
sizeof(WORD));
4507 fout->POposition = fout->filesize;
4508 S->fPatches[0] = fout->filesize;
4511 if ( AR.FoStage4[0].PObuffer == 0 ) {
4512 AR.FoStage4[0].PObuffer = (WORD *)Malloc1(AR.FoStage4[0].POsize*
sizeof(WORD)
4514 AR.FoStage4[0].POfill = AR.FoStage4[0].PObuffer;
4515 AR.FoStage4[0].POstop = AR.FoStage4[0].PObuffer
4516 + AR.FoStage4[0].POsize/
sizeof(WORD);
4518 AR.FoStage4[0].pthreadslock = dummylock;
4521 if ( AR.FoStage4[1].PObuffer == 0 ) {
4522 AR.FoStage4[1].PObuffer = (WORD *)Malloc1(AR.FoStage4[1].POsize*
sizeof(WORD)
4524 AR.FoStage4[1].POfill = AR.FoStage4[1].PObuffer;
4525 AR.FoStage4[1].POstop = AR.FoStage4[1].PObuffer
4526 + AR.FoStage4[1].POsize/
sizeof(WORD);
4528 AR.FoStage4[1].pthreadslock = dummylock;
4555 WORD *v, *s, *m, k, i;
4556 WORD *pScrat, *stop, *sv, error = 0;
4557 pScrat = AT.WorkPointer;
4558 if ( ( AT.WorkPointer + 8 * AM.MaxWildcards ) >= AT.WorkTop ) {
4559 MLOCK(ErrorMessageLock);
4561 MUNLOCK(ErrorMessageLock);
4569 while ( v < stop && (
4570 *v == FROMSET || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4571 while ( v < stop ) {
4573 if ( AM.Ordering[*v] < AM.Ordering[*m] ) {
4576 else if ( *v == *m ) {
4577 if ( v[2] < m[2] ) {
4580 else if ( v[2] == m[2] ) {
4583 if ( s < stop && ( *s == FROMSET
4584 || *s == SETTONUM || *s == LOADDOLLAR ) ) {
4585 if ( sv < stop && ( *sv == FROMSET
4586 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4587 if ( s[2] != sv[2] ) {
4589 MLOCK(ErrorMessageLock);
4590 MesPrint(
"&Wildcard set conflict");
4591 MUNLOCK(ErrorMessageLock);
4597 if ( sv < stop && ( *sv == FROMSET
4598 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4610 while ( v < stop && ( *v == FROMSET
4611 || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4617 while ( m < stop && ( *m == FROMSET
4618 || *m == SETTONUM || *m == LOADDOLLAR ) ) {
4625 while ( i < nw && ( w[i] < 0 || w[i] == FROMSET
4626 || w[i] == SETTONUM || w[i] == LOADDOLLAR ) ) i += w[i+1];
4628 AC.NwildC = k = WORDDIF(pScrat,AT.WorkPointer);
4648 int minnum = num, i;
4649 if ( AN.FunSorts ) {
4651 if ( AN.MaxFunSorts > 3 ) {
4652 minnum = (AN.MaxFunSorts+4)/2;
4656 else if ( minnum == 0 ) minnum = 1;
4657 for ( i = minnum; i < AN.NumFunSorts; i++ ) {
4660 if ( S->file.handle >= 0 ) {
4664 ClearSortGZIP(&(S->file));
4666 CloseFile(S->file.handle);
4667 S->file.handle = -1;
4668 remove(S->file.name);
4670 MLOCK(ErrorMessageLock);
4671 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4672 MUNLOCK(ErrorMessageLock);
4675 M_free(S,
"sorting struct");
4679 AN.MaxFunSorts = minnum;
4683 if ( S->file.handle >= 0 ) {
4687 ClearSortGZIP(&(S->file));
4689 CloseFile(S->file.handle);
4690 S->file.handle = -1;
4691 remove(S->file.name);
4693 MLOCK(ErrorMessageLock);
4694 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4695 MUNLOCK(ErrorMessageLock);
4701 for ( i = 0; i < 2; i++ ) {
4702 if ( AR.FoStage4[i].handle >= 0 ) {
4705 ClearSortGZIP(&(AR.FoStage4[i]));
4707 CloseFile(AR.FoStage4[i].handle);
4708 remove(AR.FoStage4[i].name);
4709 AR.FoStage4[i].handle = -1;
4711 MLOCK(ErrorMessageLock);
4712 MesPrint(
"%w CleanUpSort removed stage4 file %s",AR.FoStage4[i].name);
4713 MUNLOCK(ErrorMessageLock);
4730 if ( AR.sLevel >= 0 ) {
4732 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
4744WORD *PolyRatFunSpecial(PHEAD WORD *t1, WORD *t2)
4746 WORD *oldworkpointer = AT.WorkPointer, *t, *r;
4750 if ( *t == -SYMBOL ) {
4751 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4753 if ( t[2] != -SNUMBER )
goto Illegal;
4756 else if ( *t == -SNUMBER ) {
4759 if ( *t == -SYMBOL ) {
4760 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4763 else if ( *t == -SNUMBER ) {
4767 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4768 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4772 exp1 = -t[ARGHEAD+4];
4776 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4777 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4781 exp1 = t[ARGHEAD+4];
4783 if ( *t != -SNUMBER )
goto Illegal;
4789 if ( *t == -SYMBOL ) {
4790 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4792 if ( t[2] != -SNUMBER )
goto Illegal;
4795 else if ( *t == -SNUMBER ) {
4798 if ( *t == -SYMBOL ) {
4799 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4802 else if ( *t == -SNUMBER ) {
4806 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4807 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4811 exp2 = -t[ARGHEAD+4];
4815 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4816 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4820 exp2 = t[ARGHEAD+4];
4822 if ( *t != -SNUMBER )
goto Illegal;
4827 if ( exp1 <= exp2 ) { i = t1[1]; r = t1; }
4828 else { i = t2[1]; r = t2; }
4832 return(oldworkpointer);
4834 MesPrint(
"Illegal occurrence of PolyRatFun with divergent option");
4846VOID SimpleSplitMergeRec(WORD *array,WORD num,WORD *auxarray)
4848 WORD n1,n2,i,j,k,*t1,*t2;
4849 if ( num < 2 )
return;
4851 if ( array[0] > array[1] ) {
4852 EXCH(array[0],array[1])
4858 SimpleSplitMergeRec(array,n1,auxarray);
4859 SimpleSplitMergeRec(array+n1,n2,auxarray);
4860 if ( array[n1-1] <= array[n1] )
return;
4862 t1 = array; t2 = auxarray; i = n1; NCOPY(t2,t1,i);
4863 i = 0; j = n1; k = 0;
4864 while ( i < n1 && j < num ) {
4865 if ( auxarray[i] <= array[j] ) { array[k++] = auxarray[i++]; }
4866 else { array[k++] = array[j++]; }
4868 while ( i < n1 ) array[k++] = auxarray[i++];
4874VOID SimpleSplitMerge(WORD *array,WORD num)
4876 WORD *auxarray = Malloc1(
sizeof(WORD)*num/2,
"SimpleSplitMerge");
4877 SimpleSplitMergeRec(array,num,auxarray);
4878 M_free(auxarray,
"SimpleSplitMerge");
4892WORD BinarySearch(WORD *array,WORD num,WORD x)
4894 WORD i, bot, top, med;
4896 for ( i = 0; i < num; i++ )
if ( array[i] == x )
return(i);
4899 if ( array[0] > x || array[num-1] < x )
return(-1);
4900 bot = 0; top = num-1; med = (top+bot)/2;
4902 if ( array[med] == x )
return(med);
4903 if ( array[med] < x ) { bot = med+1; }
4904 else { top = med-1; }
4906 }
while ( med >= bot && med <= top );
WORD * poly_ratfun_add(PHEAD WORD *, WORD *)
WORD CompCoef(WORD *, WORD *)
int NormalModulus(UWORD *, WORD *)
int PF_ISendSbuf(int to, int tag)
LONG PutIn(FILEHANDLE *file, POSITION *position, WORD *buffer, WORD **take, int npat)
WORD CompareHSymbols(WORD *term1, WORD *term2, WORD par)
LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
WORD Sflush(FILEHANDLE *fi)
LONG EndSort(PHEAD WORD *buffer, int par)
WORD FlushOut(POSITION *position, FILEHANDLE *fi, int compr)
WORD SortWild(WORD *w, WORD nw)
WORD MergePatches(WORD par)
WORD StoreTerm(PHEAD WORD *term)
WORD AddPoly(PHEAD WORD **ps1, WORD **ps2)
LONG ComPress(WORD **ss, LONG *n)
WORD PutOut(PHEAD WORD *term, POSITION *position, FILEHANDLE *fi, WORD ncomp)
void CleanUpSort(int num)
WORD Compare1(WORD *term1, WORD *term2, WORD level)
VOID WriteStats(POSITION *plspace, WORD par)
WORD CompareSymbols(WORD *term1, WORD *term2, WORD par)
VOID StageSort(FILEHANDLE *fout)
WORD AddCoef(PHEAD WORD **ps1, WORD **ps2)
VOID AddArgs(PHEAD WORD *s1, WORD *s2, WORD *m)
BRACKETINDEX * indexbuffer