62 extern LONG numwrites;
65 extern LONG nummallocs;
77 char *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;
112 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
114 AR.ShortSortCount = 0;
117 MLOCK(ErrorMessageLock);
118 if ( AC.ShortStats ) {}
121 if ( identity > 0 ) {
122 MesPrint(
" Thread %d reporting",identity);
127 #elif defined(WITHMPI)
128 if ( PF.me != MASTER ) {
129 MesPrint(
" Process %d reporting",PF.me);
144 use_wtime = AC.WTimeStatsFlag;
145 #if defined(WITHPTHREADS)
146 if ( use_wtime && identity > 0 ) use_wtime = 0;
147 #elif defined(WITHMPI)
148 if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
151 timepart = (WORD)(millitime%1000);
154 if ( AC.ShortStats ) {
155 #if defined(WITHPTHREADS) || defined(WITHMPI)
157 if ( identity > 0 ) {
159 if ( PF.me != MASTER ) {
160 const int identity = PF.me;
162 if ( par == 0 || par == 2 ) {
163 SETBASEPOSITION(pp,y);
164 if ( ISLESSPOS(*plspace,pp) ) {
165 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%10p %s %s",identity,
166 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
167 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
176 SETBASEPOSITION(pp,y);
178 if ( ISLESSPOS(*plspace,pp) ) {
179 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%11p %s %s",identity,
180 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
181 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
185 if ( ISLESSPOS(*plspace,pp) ) {
186 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%12p %s %s",identity,
187 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
188 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
192 if ( ISLESSPOS(*plspace,pp) ) {
193 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%13p %s %s",identity,
194 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
195 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
199 if ( ISLESSPOS(*plspace,pp) ) {
200 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%14p %s %s",identity,
201 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
202 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
206 if ( ISLESSPOS(*plspace,pp) ) {
207 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%15p %s %s",identity,
208 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
209 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
213 if ( ISLESSPOS(*plspace,pp) ) {
214 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%16p %s %s",identity,
215 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
216 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
220 if ( ISLESSPOS(*plspace,pp) ) {
221 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%17p %s %s",identity,
222 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
223 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
229 else if ( par == 1 ) {
230 SETBASEPOSITION(pp,y);
231 if ( ISLESSPOS(*plspace,pp) ) {
232 MesPrint(
"%d: %7l.%2is %10l:%10p",identity,millitime,timepart,
233 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
237 SETBASEPOSITION(pp,y);
239 if ( ISLESSPOS(*plspace,pp) ) {
240 MesPrint(
"%d: %7l.%2is %10l:%11p",identity,millitime,timepart,
241 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
245 if ( ISLESSPOS(*plspace,pp) ) {
246 MesPrint(
"%d: %7l.%2is %10l:%12p",identity,millitime,timepart,
247 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
251 if ( ISLESSPOS(*plspace,pp) ) {
252 MesPrint(
"%d: %7l.%2is %10l:%13p",identity,millitime,timepart,
253 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
257 if ( ISLESSPOS(*plspace,pp) ) {
258 MesPrint(
"%d: %7l.%2is %10l:%14p",identity,millitime,timepart,
259 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
263 if ( ISLESSPOS(*plspace,pp) ) {
264 MesPrint(
"%d: %7l.%2is %10l:%15p",identity,millitime,timepart,
265 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
269 if ( ISLESSPOS(*plspace,pp) ) {
270 MesPrint(
"%d: %7l.%2is %10l:%16p",identity,millitime,timepart,
271 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
275 if ( ISLESSPOS(*plspace,pp) ) {
276 MesPrint(
"%d: %7l.%2is %10l:%17p",identity,millitime,timepart,
277 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
285 if ( par == 0 || par == 2 ) {
286 SETBASEPOSITION(pp,y);
287 if ( ISLESSPOS(*plspace,pp) ) {
288 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%10p %s %s",
289 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
290 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
299 SETBASEPOSITION(pp,y);
301 if ( ISLESSPOS(*plspace,pp) ) {
302 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%11p %s %s",
303 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
304 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
308 if ( ISLESSPOS(*plspace,pp) ) {
309 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%12p %s %s",
310 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
311 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
315 if ( ISLESSPOS(*plspace,pp) ) {
316 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%13p %s %s",
317 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
318 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
322 if ( ISLESSPOS(*plspace,pp) ) {
323 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%14p %s %s",
324 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
325 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
329 if ( ISLESSPOS(*plspace,pp) ) {
330 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%15p %s %s",
331 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
332 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
336 if ( ISLESSPOS(*plspace,pp) ) {
337 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%16p %s %s",
338 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
339 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
343 if ( ISLESSPOS(*plspace,pp) ) {
344 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%17p %s %s",
345 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
346 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
352 else if ( par == 1 ) {
353 SETBASEPOSITION(pp,y);
354 if ( ISLESSPOS(*plspace,pp) ) {
355 MesPrint(
"%7l.%2is %10l:%10p",millitime,timepart,
356 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
360 SETBASEPOSITION(pp,y);
362 if ( ISLESSPOS(*plspace,pp) ) {
363 MesPrint(
"%7l.%2is %10l:%11p",millitime,timepart,
364 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
368 if ( ISLESSPOS(*plspace,pp) ) {
369 MesPrint(
"%7l.%2is %10l:%12p",millitime,timepart,
370 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
374 if ( ISLESSPOS(*plspace,pp) ) {
375 MesPrint(
"%7l.%2is %10l:%13p",millitime,timepart,
376 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
380 if ( ISLESSPOS(*plspace,pp) ) {
381 MesPrint(
"%7l.%2is %10l:%14p",millitime,timepart,
382 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
386 if ( ISLESSPOS(*plspace,pp) ) {
387 MesPrint(
"%7l.%2is %10l:%15p",millitime,timepart,
388 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
392 if ( ISLESSPOS(*plspace,pp) ) {
393 MesPrint(
"%7l.%2is %10l:%16p",millitime,timepart,
394 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
398 if ( ISLESSPOS(*plspace,pp) ) {
399 MesPrint(
"%7l.%2is %10l:%17p",millitime,timepart,
400 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
410 MesPrint(
"WTime = %7l.%2i sec",millitime,timepart);
413 MesPrint(
"Time = %7l.%2i sec",millitime,timepart);
417 #if ( BITSINLONG > 32 )
418 if ( S->GenTerms >= 10000000000L ) {
420 MesPrint(
"WTime = %7l.%2i sec Generated terms = %16l",
421 millitime,timepart,S->GenTerms);
424 MesPrint(
"Time = %7l.%2i sec Generated terms = %16l",
425 millitime,timepart,S->GenTerms);
430 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
431 millitime,timepart,S->GenTerms);
434 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
435 millitime,timepart,S->GenTerms);
440 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
441 millitime,timepart,S->GenTerms);
444 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
445 millitime,timepart,S->GenTerms);
449 #if ( BITSINLONG > 32 )
451 if ( S->TermsLeft >= 10000000000L ) {
452 MesPrint(
"%16s%8l Terms %s = %16l",EXPRNAME(AR.CurExpr),
453 AN.ninterms,FG.swmes[par],S->TermsLeft);
456 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
457 AN.ninterms,FG.swmes[par],S->TermsLeft);
460 if ( S->TermsLeft >= 10000000000L ) {
462 if ( identity > 0 && par == 2 ) {
463 MesPrint(
"%16s Terms in thread = %16l",
464 EXPRNAME(AR.CurExpr),S->TermsLeft);
467 #elif defined(WITHMPI)
468 if ( PF.me != MASTER && par == 2 ) {
469 MesPrint(
"%16s Terms in process= %16l",
470 EXPRNAME(AR.CurExpr),S->TermsLeft);
475 MesPrint(
"%16s Terms %s = %16l",
476 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
481 if ( identity > 0 && par == 2 ) {
482 MesPrint(
"%16s Terms in thread = %10l",
483 EXPRNAME(AR.CurExpr),S->TermsLeft);
486 #elif defined(WITHMPI)
487 if ( PF.me != MASTER && par == 2 ) {
488 MesPrint(
"%16s Terms in process= %10l",
489 EXPRNAME(AR.CurExpr),S->TermsLeft);
494 MesPrint(
"%16s Terms %s = %10l",
495 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
501 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
502 AN.ninterms,FG.swmes[par],S->TermsLeft);
505 if ( identity > 0 && par == 2 ) {
506 MesPrint(
"%16s Terms in thread = %10l",
507 EXPRNAME(AR.CurExpr),S->TermsLeft);
510 #elif defined(WITHMPI)
511 if ( PF.me != MASTER && par == 2 ) {
512 MesPrint(
"%16s Terms in process= %10l",
513 EXPRNAME(AR.CurExpr),S->TermsLeft);
518 MesPrint(
"%16s Terms %s = %10l",
519 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
523 SETBASEPOSITION(pp,y);
524 if ( ISLESSPOS(*plspace,pp) ) {
525 MesPrint(
"%24s Bytes used = %10p",AC.Commercial,plspace);
529 SETBASEPOSITION(pp,y);
531 if ( ISLESSPOS(*plspace,pp) ) {
532 MesPrint(
"%24s Bytes used =%11p",AC.Commercial,plspace);
536 if ( ISLESSPOS(*plspace,pp) ) {
537 MesPrint(
"%24s Bytes used =%12p",AC.Commercial,plspace);
541 if ( ISLESSPOS(*plspace,pp) ) {
542 MesPrint(
"%24s Bytes used =%13p",AC.Commercial,plspace);
546 if ( ISLESSPOS(*plspace,pp) ) {
547 MesPrint(
"%24s Bytes used =%14p",AC.Commercial,plspace);
551 if ( ISLESSPOS(*plspace,pp) ) {
552 MesPrint(
"%24s Bytes used =%15p",AC.Commercial,plspace);
556 if ( ISLESSPOS(*plspace,pp) ) {
557 MesPrint(
"%24s Bytes used =%16p",AC.Commercial,plspace);
561 if ( ISLESSPOS(*plspace,pp) ) {
562 MesPrint(
"%24s Bytes used=%17p",AC.Commercial,plspace);
568 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l"
569 ,numwrites,numreads,numseeks);
570 MesPrint(
"Total number of mallocs: %l, frees: %l"
571 ,nummallocs,numfrees);
573 MUNLOCK(ErrorMessageLock);
596 if ( AN.SoScratC == 0 )
597 AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*
sizeof(UWORD),
"NewSort");
599 if ( AR.sLevel >= AN.NumFunSorts ) {
600 if ( AN.NumFunSorts == 0 ) newsize = 100;
601 else newsize = 2*AN.NumFunSorts;
602 newFS = (
SORTING **)Malloc1((newsize+1)*
sizeof(
SORTING *),
"FunSort pointers");
603 for ( i = 0; i < AN.NumFunSorts; i++ ) newFS[i] = AN.FunSorts[i];
604 for ( ; i <= newsize; i++ ) newFS[i] = 0;
605 if ( AN.FunSorts ) M_free(AN.FunSorts,
"FunSort pointers");
606 AN.FunSorts = newFS; AN.NumFunSorts = newsize;
608 if ( AR.sLevel == 0 ) {
612 AN.FunSorts[0] = AT.S0;
613 if ( AR.PolyFun == 0 ) { AT.S0->PolyFlag = 0; }
614 else if ( AR.PolyFunType == 1 ) { AT.S0->PolyFlag = 1; }
615 else if ( AR.PolyFunType == 2 ) {
616 if ( AR.PolyFunExp == 2
617 || AR.PolyFunExp == 3 ) AT.S0->PolyFlag = 1;
618 else AT.S0->PolyFlag = 2;
620 AR.ShortSortCount = 0;
623 if ( AN.FunSorts[AR.sLevel] == 0 ) {
624 AN.FunSorts[AR.sLevel] = AllocSort(
625 AM.SLargeSize,AM.SSmallSize,AM.SSmallEsize,AM.STermsInSmall
626 ,AM.SMaxPatches,AM.SMaxFpatches,AM.SIOsize);
628 AN.FunSorts[AR.sLevel]->PolyFlag = 0;
630 AT.SS = S = AN.FunSorts[AR.sLevel];
631 S->sFill = S->sBuffer;
632 S->lFill = S->lBuffer;
635 S->GenTerms = S->TermsLeft = S->GenSpace = S->SpaceLeft = 0;
636 S->PoinFill = S->sPointer;
637 *S->PoinFill = S->sFill;
638 if ( AR.sLevel > 0 ) { S->PolyWise = 0; }
639 PUTZERO(S->SizeInFile[0]); PUTZERO(S->SizeInFile[1]); PUTZERO(S->SizeInFile[2]);
641 PUTZERO(S->file.POposition);
643 if ( AR.sLevel > AN.MaxFunSorts ) AN.MaxFunSorts = AR.sLevel;
685 WORD j, **ss, *to, *t;
686 LONG sSpace, over, tover, spare, retval = 0, jj;
689 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
691 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
694 oldoutfile = AR.outfile;
704 oldoutfile = AR.outfile;
723 PrintTime((UBYTE *)
"EndSort, before SplitMerge");
725 S->sPointer[
SplitMerge(BHEAD S->sPointer,S->sTerms)] = 0;
727 PrintTime((UBYTE *)
"Endsort, after SplitMerge");
730 tover = over = S->sTerms;
733 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
736 S->TermsLeft -= over - spare;
737 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
739 else if ( S != AT.S0 ) {
743 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
744 if ( AN.tryterm > 0 && ( (sSpace+1)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
745 to = TermMalloc(
"$-sort space");
748 LONG allocsp = sSpace+1;
749 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
750 allocsp = ((allocsp+7)/8)*8;
751 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
752 if ( AN.tryterm > 0 ) AN.tryterm = 0;
754 *((WORD **)buffer) = to;
756 while ( ( t = *ss++ ) != 0 ) {
757 j = *t;
while ( --j >= 0 ) *to++ = *t++;
765 while ( ( t = *ss++ ) != 0 ) {
767 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
768 MLOCK(ErrorMessageLock);
769 MesPrint(
"Sorted function argument too long.");
770 MUNLOCK(ErrorMessageLock);
771 retval = -1;
goto RetRetval;
773 while ( --j >= 0 ) *to++ = *t++;
783 *AR.CompressPointer = 0;
784 SeekScratch(AR.outfile,&position);
796 {
int oldgzipCompress = AR.gzipCompress;
801 while ( ( t = *ss++ ) != 0 ) {
802 if ( *t ) S->TermsLeft++;
804 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
807 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
808 retval = -1;
goto RetRetval;
813 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
817 retval = -1;
goto RetRetval;
820 AR.gzipCompress = oldgzipCompress;
824 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
827 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
829 DIFPOS(oldpos,position,oldpos);
830 S->SpaceLeft = BASEPOSITION(oldpos);
836 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
838 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
841 SETBASEPOSITION(pp,lSpace);
842 MULPOS(pp,
sizeof(WORD));
843 if ( S->file.handle >= 0 ) {
844 ADD2POS(pp,S->fPatches[S->fPatchN]);
847 WORD oldLogHandle = AC.LogHandle;
848 if ( AC.LogHandle >= 0 && AM.LogType && ( ( S->lPatch > 0 )
849 || S->file.handle >= 0 ) ) AC.LogHandle = -1;
850 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
WriteStats(&pp,0); }
851 AC.LogHandle = oldLogHandle;
853 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
854 if ( S->lPatch > 0 ) {
855 if ( ( S->lPatch >= S->MaxPatches ) ||
856 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
861 MLOCK(ErrorMessageLock);
862 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x"
863 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
864 MUNLOCK(ErrorMessageLock);
868 MLOCK(ErrorMessageLock);
870 MUNLOCK(ErrorMessageLock);
871 retval = -1;
goto RetRetval;
874 pp = S->SizeInFile[1];
875 MULPOS(pp,
sizeof(WORD));
880 WORD oldLogHandle = AC.LogHandle;
882 SETBASEPOSITION(pppp,0);
883 SeekFile(S->file.handle,&pppp,SEEK_CUR);
884 SeekFile(S->file.handle,&pp,SEEK_END);
885 SeekFile(S->file.handle,&pppp,SEEK_SET);
886 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
888 AC.LogHandle = oldLogHandle;
893 S->Patches[S->lPatch++] = S->lFill;
894 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
897 while ( ( t = *ss++ ) != 0 ) {
899 if ( j < 0 ) j = t[1] + 2;
900 while ( --j >= 0 ) *to++ = *t++;
905 if ( S->file.handle < 0 ) {
907 MLOCK(ErrorMessageLock);
909 MUNLOCK(ErrorMessageLock);
910 retval = -1;
goto RetRetval;
913 pp = S->SizeInFile[2];
914 MULPOS(pp,
sizeof(WORD));
916 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
922 if ( par == 2 && newout->handle >= 0 ) {
926 LOCK(newout->pthreadslock);
928 SeekFile(newout->handle,&zeropos,SEEK_SET);
929 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+
sizeof(WORD)*2
930 ,
"$-buffer reading");
931 if ( AN.tryterm > 0 ) AN.tryterm = 0;
932 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
933 BASEPOSITION(newout->filesize) ) {
934 MLOCK(ErrorMessageLock);
935 MesPrint(
"Error reading information for $ variable");
936 MUNLOCK(ErrorMessageLock);
937 M_free(to,
"$-buffer reading");
941 *((WORD **)buffer) = to;
942 retval /=
sizeof(WORD);
945 UNLOCK(newout->pthreadslock);
948 else if ( newout->handle >= 0 ) {
950 MLOCK(ErrorMessageLock);
951 MesPrint(
"(1)Output should fit inside a single term. Increase MaxTermSize?");
953 MUNLOCK(ErrorMessageLock);
954 retval = -1;
goto RetRetval;
957 t = newout->PObuffer;
959 jj = newout->POfill - t;
960 if ( AN.tryterm > 0 && ( (jj+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
961 to = TermMalloc(
"$-sort space");
965 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
966 allocsp = ((allocsp+7)/8)*8;
967 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
968 if ( AN.tryterm > 0 ) AN.tryterm = 0;
970 *((WORD **)buffer) = to;
974 j = newout->POfill - t;
976 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
978 if ( j > AM.MaxTer )
goto TooLarge;
986 MLOCK(ErrorMessageLock);
988 MUNLOCK(ErrorMessageLock);
989 retval = -1;
goto RetRetval;
992 pp = S->SizeInFile[1];
993 MULPOS(pp,
sizeof(WORD));
998 WORD oldLogHandle = AC.LogHandle;
1000 SETBASEPOSITION(pppp,0);
1001 SeekFile(S->file.handle,&pppp,SEEK_CUR);
1002 SeekFile(S->file.handle,&pp,SEEK_END);
1003 SeekFile(S->file.handle,&pppp,SEEK_SET);
1004 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
1006 AC.LogHandle = oldLogHandle;
1014 if ( newout->handle >= 0 )
goto TooLarge;
1015 t = newout->PObuffer;
1016 j = newout->POfill - t;
1018 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
1019 goto WorkSpaceError;
1020 if ( j > AM.MaxTer )
goto TooLarge;
1027 if ( S->file.handle >= 0 ) {
1029 MLOCK(ErrorMessageLock);
1030 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p"
1031 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1032 MUNLOCK(ErrorMessageLock);
1034 if ( S->lPatch <= 0 ) {
1036 position = S->fPatches[S->fPatchN];
1040 *AR.CompressPointer = 0;
1041 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
1042 S->fpcompressed[S->fPatchN] = 1;
1044 S->fpcompressed[S->fPatchN] = 0;
1045 SetupOutputGZIP(&(S->file));
1047 while ( ( t = *ss++ ) != 0 ) {
1048 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
1049 retval = -1;
goto RetRetval;
1052 if (
FlushOut(&position,&(S->file),1) ) {
1053 retval = -1;
goto RetRetval;
1056 S->fPatches[S->fPatchN] = position;
1059 MLOCK(ErrorMessageLock);
1060 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p"
1061 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1062 MUNLOCK(ErrorMessageLock);
1068 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1069 if ( S->file.handle >= 0 ) {
1070 SynchFile(S->file.handle);
1076 MLOCK(ErrorMessageLock);
1078 MUNLOCK(ErrorMessageLock);
1079 retval = -1;
goto RetRetval;
1083 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
1085 pp = S->SizeInFile[0];
1086 MULPOS(pp,
sizeof(WORD));
1094 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
1095 Expressions[AR.CurExpr].counter = S->TermsLeft;
1096 Expressions[AR.CurExpr].size = pp;
1099 if ( AR.sLevel == 0 ) {
1100 Expressions[AR.CurExpr].counter = S->TermsLeft;
1101 Expressions[AR.CurExpr].size = pp;
1105 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
1109 ClearSortGZIP(&(S->file));
1111 CloseFile(S->file.handle);
1112 S->file.handle = -1;
1113 remove(S->file.name);
1115 MLOCK(ErrorMessageLock);
1116 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
1117 MUNLOCK(ErrorMessageLock);
1120 AR.outfile = oldoutfile;
1122 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
1127 DeAllocFileHandle(newout);
1131 else if ( newout ) {
1132 if ( newout->handle >= 0 ) {
1133 MLOCK(ErrorMessageLock);
1134 MesPrint(
"(2)Output should fit inside a single term. Increase MaxTermSize?");
1136 MUNLOCK(ErrorMessageLock);
1139 else if ( newout->POfill > newout->PObuffer ) {
1145 j = newout->POfill-newout->PObuffer;
1146 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
1147 goto WorkSpaceError;
1149 to = buffer; t = newout->PObuffer;
1150 while ( j-- > 0 ) *to++ = *t++;
1154 DeAllocFileHandle(newout);
1158 else if ( par == 2 ) {
1160 if ( retval == 0 ) {
1161 if ( newout->handle >= 0 ) {
1169 LOCK(newout->pthreadslock);
1171 SeekFile(newout->handle,&position,SEEK_END);
1173 SeekFile(newout->handle,&zeropos,SEEK_SET);
1174 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)*3
1175 ,
"$-buffer reading");
1176 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1177 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
1178 BASEPOSITION(position) ) {
1179 MLOCK(ErrorMessageLock);
1180 MesPrint(
"Error reading information for $ variable");
1181 MUNLOCK(ErrorMessageLock);
1182 M_free(to,
"$-buffer reading");
1186 *((WORD **)buffer) = to;
1187 retval /=
sizeof(WORD);
1190 UNLOCK(newout->pthreadslock);
1197 LONG wsiz = newout->POfill - newout->PObuffer;
1198 if ( AN.tryterm > 0 && ( (wsiz+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
1199 to = TermMalloc(
"$-sort space");
1202 LONG allocsp = wsiz+2;
1203 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
1204 allocsp = ((allocsp+7)/8)*8;
1205 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-buffer reading");
1206 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1208 *((WORD **)buffer) = to; t = newout->PObuffer;
1214 DeAllocFileHandle(newout);
1220 DeAllocFileHandle(newout);
1231 MLOCK(ErrorMessageLock);
1234 MUNLOCK(ErrorMessageLock);
1265 from = buffer + ( file->POsize *
sizeof(UBYTE) )/
sizeof(WORD);
1267 if ( i*((LONG)(
sizeof(WORD))) > AM.MaxTer ) {
1268 MLOCK(ErrorMessageLock);
1269 MesPrint(
"Problems in PutIn");
1270 MUNLOCK(ErrorMessageLock);
1274 while ( --i >= 0 ) *--to = *--from;
1277 if ( ( RetCode = FillInputGZIP(file,position,(UBYTE *)buffer
1278 ,file->POsize,npat) ) < 0 ) {
1279 MLOCK(ErrorMessageLock);
1280 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1281 RetCode,file->POsize);
1282 MUNLOCK(ErrorMessageLock);
1287 LOCK(file->pthreadslock);
1289 SeekFile(file->
handle,position,SEEK_SET);
1290 if ( ( RetCode = ReadFile(file->
handle,(UBYTE *)buffer,file->POsize) ) < 0 ) {
1292 UNLOCK(file->pthreadslock);
1294 MLOCK(ErrorMessageLock);
1295 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1296 RetCode,file->POsize);
1297 MUNLOCK(ErrorMessageLock);
1301 UNLOCK(file->pthreadslock);
1323 int dobracketindex = 0;
1324 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1325 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1328 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1330 MLOCK(ErrorMessageLock);
1331 MesPrint(
"%w Sflush created scratch file %s",fi->name);
1332 MUNLOCK(ErrorMessageLock);
1334 fi->
handle = (WORD)RetCode;
1335 PUTZERO(fi->filesize);
1336 PUTZERO(fi->POposition);
1339 MLOCK(ErrorMessageLock);
1340 MesPrint(
"Cannot create scratch file %s",fi->name);
1341 MUNLOCK(ErrorMessageLock);
1346 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1347 && dobracketindex == 0 ) {
1348 if ( FlushOutputGZIP(fi) )
return(-1);
1349 fi->POfill = fi->PObuffer;
1355 LOCK(fi->pthreadslock);
1357 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1358 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1359 if ( WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) != size ) {
1361 UNLOCK(fi->pthreadslock);
1363 MLOCK(ErrorMessageLock);
1364 MesPrint(
"Write error while finishing sort. Disk full?");
1365 MUNLOCK(ErrorMessageLock);
1368 ADDPOS(fi->filesize,size);
1369 ADDPOS(fi->POposition,size);
1370 fi->POfill = fi->PObuffer;
1372 UNLOCK(fi->pthreadslock);
1407 WORD i, *p, ret, *r, *rr, j, k, first;
1408 int dobracketindex = 0;
1411 if ( AT.SS != AT.S0 ) {
1415 if ( ( i = *term ) <= 0 )
return(0);
1417 ADDPOS(*position,i*
sizeof(WORD));
1420 if ( p >= fi->POstop ) {
1422 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1424 MLOCK(ErrorMessageLock);
1425 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1426 MUNLOCK(ErrorMessageLock);
1428 fi->
handle = (WORD)RetCode;
1429 PUTZERO(fi->filesize);
1430 PUTZERO(fi->POposition);
1439 MLOCK(ErrorMessageLock);
1440 MesPrint(
"Cannot create scratch file %s",fi->name);
1441 MUNLOCK(ErrorMessageLock);
1446 LOCK(fi->pthreadslock);
1448 if ( fi == AR.hidefile ) {
1449 LOCK(AS.inputslock);
1451 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1452 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1453 if ( fi == AR.hidefile ) {
1454 UNLOCK(AS.inputslock);
1457 UNLOCK(fi->pthreadslock);
1459 MLOCK(ErrorMessageLock);
1460 MesPrint(
"Write error during sort. Disk full?");
1461 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1462 fi->POsize,fi->
handle,&(fi->POposition));
1463 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1464 MUNLOCK(ErrorMessageLock);
1467 ADDPOS(fi->filesize,fi->POsize);
1469 ADDPOS(fi->POposition,fi->POsize);
1470 if ( fi == AR.hidefile ) {
1471 UNLOCK(AS.inputslock);
1474 UNLOCK(fi->pthreadslock);
1477 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1483 }
while ( --i > 0 );
1484 fi->POfull = fi->POfill = p;
1487 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1488 MLOCK(ErrorMessageLock);
1490 sprintf((
char *)(THRbuf),
"PutOut(%d)",AT.identity);
1491 PrintTerm(term,(
char *)(THRbuf));
1493 PrintTerm(term,
"PutOut");
1495 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1496 MesPrint(
"File %s, position %p",fi->name,position);
1497 MUNLOCK(ErrorMessageLock);
1500 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1501 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1502 r = rr = AR.CompressPointer;
1503 first = j = k = ret = 0;
1504 if ( ( i = *term ) != 0 ) {
1507 if ( fi == AR.outfile || fi == AR.hidefile ) {
1508 MLOCK(ErrorMessageLock);
1509 MesPrint(
"Ran into precompressed term");
1510 MUNLOCK(ErrorMessageLock);
1515 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1516 if ( dobracketindex ) {
1517 PutBracketInIndex(BHEAD term,position);
1523 WORD *polystop, *sa;
1527 while ( polystop < sa && *polystop != AR.PolyFun ) {
1528 polystop += polystop[1];
1530 if ( polystop < sa ) {
1531 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1532 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1533 i--; j--; k--; p++; r++;
1537 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1544 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1564 r[-(ABS(r[-1]))] = 0;
1565 if ( r >= AR.ComprTop ) {
1566 MLOCK(ErrorMessageLock);
1567 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1568 MUNLOCK(ErrorMessageLock);
1573 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1575 if ( dobracketindex ) {
1576 PutBracketInIndex(BHEAD term,position);
1582 WORD *polystop, *sa;
1586 while ( polystop < sa && *polystop != AR.PolyFun ) {
1587 polystop += polystop[1];
1589 if ( polystop < sa ) {
1590 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1591 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1592 i--; j--; k--; p++; r++;
1596 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1600 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1605 if ( AR.PolyFunType == 2 ) {
1607 tstop = term + *term;
1608 tstop -= ABS(tstop[-1]);
1610 while ( t < tstop ) {
1611 if ( *t == AR.PolyFun ) {
1612 t[2] &= ~MUSTCLEANPRF;
1617 if ( dobracketindex ) {
1618 PutBracketInIndex(BHEAD term,position);
1622 ADDPOS(*position,i*
sizeof(WORD));
1625 if ( p >= fi->POstop ) {
1627 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1629 sbuf->fill[sbuf->active] = fi->POstop;
1631 p = fi->PObuffer = fi->POfill = fi->POfull =
1632 sbuf->buff[sbuf->active];
1633 fi->POstop = sbuf->stop[sbuf->active];
1639 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1641 MLOCK(ErrorMessageLock);
1642 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1643 MUNLOCK(ErrorMessageLock);
1645 fi->
handle = (WORD)RetCode;
1646 PUTZERO(fi->filesize);
1647 PUTZERO(fi->POposition);
1656 MLOCK(ErrorMessageLock);
1657 MesPrint(
"Cannot create scratch file %s",fi->name);
1658 MUNLOCK(ErrorMessageLock);
1663 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1664 && dobracketindex == 0 && fi->zsp != 0 ) {
1666 if ( PutOutputGZIP(fi) )
return(-1);
1673 LOCK(fi->pthreadslock);
1675 if ( fi == AR.hidefile ) {
1676 LOCK(AS.inputslock);
1678 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1679 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1680 if ( fi == AR.hidefile ) {
1681 UNLOCK(AS.inputslock);
1684 UNLOCK(fi->pthreadslock);
1686 MLOCK(ErrorMessageLock);
1687 MesPrint(
"Write error during sort. Disk full?");
1688 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1689 fi->POsize,fi->
handle,&(fi->POposition));
1690 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1691 MUNLOCK(ErrorMessageLock);
1694 ADDPOS(fi->filesize,fi->POsize);
1696 ADDPOS(fi->POposition,fi->POsize);
1697 if ( fi == AR.hidefile ) {
1698 UNLOCK(AS.inputslock);
1701 UNLOCK(fi->pthreadslock);
1704 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1712 if ( first == 2 ) *p++ = k;
1716 else *p++ = *term++;
1722 }
while ( --i > 0 );
1723 fi->POfull = fi->POfill = p;
1751 int dobracketindex = 0;
1755 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1756 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1758 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1760 if ( fi->POfill >= fi->POstop ){
1761 sbuf->fill[sbuf->active] = fi->POstop;
1763 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1764 fi->POstop = sbuf->stop[sbuf->active];
1766 *(fi->POfill)++ = 0;
1767 sbuf->fill[sbuf->active] = fi->POfill;
1769 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1770 fi->POstop = sbuf->stop[sbuf->active];
1774 if ( fi->POfill >= fi->POstop ) {
1776 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1778 MLOCK(ErrorMessageLock);
1779 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1780 MUNLOCK(ErrorMessageLock);
1782 PUTZERO(fi->filesize);
1783 PUTZERO(fi->POposition);
1784 fi->
handle = (WORD)RetCode;
1793 MLOCK(ErrorMessageLock);
1794 MesPrint(
"Cannot create scratch file %s",fi->name);
1795 MUNLOCK(ErrorMessageLock);
1800 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1801 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1802 if ( PutOutputGZIP(fi) )
return(-1);
1803 fi->POfill = fi->PObuffer;
1809 LOCK(fi->pthreadslock);
1811 if ( fi == AR.hidefile ) {
1812 LOCK(AS.inputslock);
1814 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1815 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1817 UNLOCK(fi->pthreadslock);
1819 if ( fi == AR.hidefile ) {
1820 UNLOCK(AS.inputslock);
1822 MLOCK(ErrorMessageLock);
1823 MesPrint(
"Write error while sorting. Disk full?");
1824 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1825 fi->POsize,fi->
handle,&(fi->POposition));
1826 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1827 MUNLOCK(ErrorMessageLock);
1830 ADDPOS(fi->filesize,fi->POsize);
1831 fi->POfill = fi->PObuffer;
1832 ADDPOS(fi->POposition,fi->POsize);
1833 if ( fi == AR.hidefile ) {
1834 UNLOCK(AS.inputslock);
1837 UNLOCK(fi->pthreadslock);
1840 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1846 *(fi->POfill)++ = 0;
1847 fi->POfull = fi->POfill;
1860 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1863 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1864 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1865 if ( FlushOutputGZIP(fi) )
return(-1);
1866 fi->POfill = fi->PObuffer;
1872 LOCK(fi->pthreadslock);
1874 if ( fi == AR.hidefile ) {
1875 LOCK(AS.inputslock);
1877 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1881 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1883 UNLOCK(fi->pthreadslock);
1885 if ( fi == AR.hidefile ) {
1886 UNLOCK(AS.inputslock);
1888 MLOCK(ErrorMessageLock);
1889 MesPrint(
"Write error while finishing sorting. Disk full?");
1890 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1891 size,fi->
handle,&(fi->POposition));
1892 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1893 MUNLOCK(ErrorMessageLock);
1896 ADDPOS(fi->filesize,size);
1897 ADDPOS(fi->POposition,size);
1898 fi->POfill = fi->PObuffer;
1899 if ( fi == AR.hidefile ) {
1900 UNLOCK(AS.inputslock);
1903 UNLOCK(fi->pthreadslock);
1906 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1912 if ( dobracketindex ) {
1913 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1914 if ( b->indexfill > 0 ) {
1915 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1919 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1920 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1924 LOCK(fi->pthreadslock);
1926 SeekFile(fi->
handle,position,SEEK_END);
1928 UNLOCK(fi->pthreadslock);
1932 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1938 ADDPOS(*position,
sizeof(WORD));
1969 OutCoef = AN.SoScratC;
1970 s1 = *ps1; s2 = *ps2;
1973 if ( AddRat(BHEAD (UWORD *)s1,l1,(UWORD *)s2,l2,OutCoef,&OutLen) ) {
1974 MLOCK(ErrorMessageLock);
1976 MUNLOCK(ErrorMessageLock);
1979 if ( AN.ncmod != 0 ) {
1980 if ( ( AC.modmode & POSNEG ) != 0 ) {
1987 j = ABS(OutLen); OutCoef[j] = 1;
1988 for ( i = 1; i < j; i++ ) OutCoef[j+i] = 0;
1990 else if ( BigLong(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
1991 SubPLon(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod),OutCoef,&OutLen);
1992 OutCoef[OutLen] = 1;
1993 for ( i = 1; i < OutLen; i++ ) OutCoef[OutLen+i] = 0;
1996 if ( !OutLen ) { *ps1 = *ps2 = 0;
return(0); }
1998 if ( OutLen < 0 ) i = - ( --OutLen );
2000 if ( l1 < 0 ) l1 = -l1;
2005 s2 = (WORD *)OutCoef;
2006 while ( --i > 0 ) *s1++ = *s2++;
2008 while ( --l1 >= 0 ) *s1++ = 0;
2011 if ( l2 < 0 ) l2 = -l2;
2016 s1 = (WORD *)OutCoef;
2017 while ( --i > 0 ) *s2++ = *s1++;
2019 while ( --l2 >= 0 ) *s2++ = 0;
2029 if ( (S->sFill + j) >= S->sTop2 ) {
2036 if ( l1 < 0 ) l1 = - l1;
2041 while ( s1 < t ) *s2++ = *s1++;
2042 s1 = (WORD *)OutCoef;
2043 while ( --i > 0 ) *s2++ = *s1++;
2049 if ( **ps1 > AM.MaxTer/((LONG)(
sizeof(WORD))) ) {
2050 MLOCK(ErrorMessageLock);
2051 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2052 AM.MaxTer/
sizeof(WORD));
2053 MUNLOCK(ErrorMessageLock);
2093 WORD *s1, *s2, *m, *w, *t, oldpw = S->PolyWise;
2094 s1 = *ps1 + S->PolyWise;
2095 s2 = *ps2 + S->PolyWise;
2100 if ( S->PolyFlag == 2 && AR.PolyFunExp != 2 && AR.PolyFunExp != 3 ) {
2101 WORD **oldSplitScratch = AN.SplitScratch;
2102 LONG oldSplitScratchSize = AN.SplitScratchSize;
2103 LONG oldInScratch = AN.InScratch;
2104 WORD oldtype = AR.SortType;
2105 if ( (WORD *)((UBYTE *)w + AM.MaxTer) >= AT.WorkTop ) {
2106 MLOCK(ErrorMessageLock);
2107 MesPrint(
"Program was adding polyratfun arguments");
2109 MUNLOCK(ErrorMessageLock);
2111 AR.SortType = SORTHIGHFIRST;
2113 AN.SplitScratch = AN.SplitScratch1;
2114 AN.SplitScratchSize = AN.SplitScratchSize1;
2115 AN.InScratch = AN.InScratch1;
2116 poly_ratfun_add(BHEAD s1,s2);
2117 S->PolyWise = oldpw;
2118 AN.SplitScratch1 = AN.SplitScratch;
2119 AN.SplitScratchSize1 = AN.SplitScratchSize;
2120 AN.InScratch1 = AN.InScratch;
2121 AN.SplitScratch = oldSplitScratch;
2122 AN.SplitScratchSize = oldSplitScratchSize;
2123 AN.InScratch = oldInScratch;
2125 AR.SortType = oldtype;
2126 if ( w[1] <= FUNHEAD ||
2127 ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
2128 *ps1 = *ps2 = 0;
return(0);
2132 if ( w + s1[1] + s2[1] + 12 + ARGHEAD >= AT.WorkTop ) {
2133 MLOCK(ErrorMessageLock);
2134 MesPrint(
"Program was adding polyfun arguments");
2136 MUNLOCK(ErrorMessageLock);
2143 if ( w[1] <= FUNHEAD ) { *ps1 = *ps2 = 0;
return(0); }
2144 if ( w[1] <= s1[1] || w[1] <= s2[1] ) {
2145 if ( w[1] > s1[1] ) {
2154 while ( t < m ) *s1++ = *t++;
2155 **ps1 = WORDDIF(s1,(*ps1));
2164 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2166 MesPrint(
"------Garbage collection-------");
2168 AT.WorkPointer += w[1];
2172 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2175 MLOCK(ErrorMessageLock);
2176 AO.OutFill = AO.OutputLine = OutBuf;
2180 while ( --i >= 0 ) {
2181 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2184 AO.OutFill = AO.OutputLine = OutBuf;
2189 while ( --i >= 0 ) {
2190 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2193 AO.OutFill = AO.OutputLine = OutBuf;
2198 while ( --i >= 0 ) {
2199 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2202 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2203 MUNLOCK(ErrorMessageLock);
2205 MLOCK(ErrorMessageLock);
2206 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2207 MUNLOCK(ErrorMessageLock);
2221 while ( t < w ) *s2++ = *t++;
2225 if ( *m > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
2226 MLOCK(ErrorMessageLock);
2227 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2228 AM.MaxTer/
sizeof(WORD));
2229 MUNLOCK(ErrorMessageLock);
2241 #define INSLENGTH(x) w[1] = FUNHEAD+ARGHEAD+x; w[FUNHEAD] = ARGHEAD+x;
2254 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2255 WORD tempterm[8+FUNHEAD];
2257 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2258 *m++ = 0; *m++ = 0; FILLARG(m)
2259 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2260 if ( s1[FUNHEAD] < 0 ) {
2261 if ( s2[FUNHEAD] < 0 ) {
2262 if ( s1[FUNHEAD] <= -FUNCTION ) {
2263 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2264 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2266 *m++ = 2; *m++ = 1; *m++ = 3;
2267 INSLENGTH(4+FUNHEAD)
2269 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2270 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2271 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2272 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2274 s2[FUNHEAD] = s1[FUNHEAD];
2277 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2279 *m++ = 1; *m++ = 1; *m++ = 3;
2280 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2282 *m++ = 1; *m++ = 1; *m++ = 3;
2283 INSLENGTH(8+2*FUNHEAD)
2285 else if ( s2[FUNHEAD] == -SYMBOL ) {
2286 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2287 *m++ = 1; *m++ = 1; *m++ = 3;
2288 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2290 *m++ = 1; *m++ = 1; *m++ = 3;
2291 INSLENGTH(12+FUNHEAD)
2295 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2296 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2298 *m++ = 1; *m++ = 1; *m++ = 3;
2299 INSLENGTH(8+FUNHEAD)
2302 else if ( s1[FUNHEAD] == -SYMBOL ) {
2303 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2304 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2305 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2306 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2310 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2311 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2312 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2313 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2314 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2315 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2316 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2320 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2321 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2322 *m++ = 1; *m++ = 1; *m++ = 3;
2323 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2325 *m++ = 1; *m++ = 1; *m++ = 3;
2326 INSLENGTH(12+FUNHEAD)
2330 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2331 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2332 *m++ = 1; *m++ = 1; *m++ = 3;
2337 if ( s2[FUNHEAD] <= -FUNCTION ) {
2339 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2340 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2342 *m++ = 1; *m++ = 1; *m++ = 3;
2343 INSLENGTH(8+FUNHEAD)
2345 else if ( s2[FUNHEAD] == -SYMBOL ) {
2347 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2348 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2349 *m++ = 1; *m++ = 1; *m++ = 3;
2354 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2355 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2356 else { i1 = (WORD)x1; i2 = 3; }
2357 if ( x1 && AN.ncmod != 0 ) {
2362 if ( Modulus(m) ) Terminate(-1);
2363 if ( *m == 0 ) w[1] = 0;
2365 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2383 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2390 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2403 if ( s1[FUNHEAD] <= -FUNCTION ) {
2404 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2406 *t++ = 1; *t++ = 1; *t++ = 3;
2408 else if ( s1[FUNHEAD] == -SYMBOL ) {
2409 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2410 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2411 *t++ = 1; *t++ = 1; *t++ = 3;
2414 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2415 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2431 tstop1 = s1 + s1[1];
2432 s1 += FUNHEAD+ARGHEAD;
2434 tstop2 = s2 + s2[1];
2435 s2 += FUNHEAD+ARGHEAD;
2439 oldPolyFlag = AT.SS->PolyFlag;
2440 AT.SS->PolyFlag = 0;
2441 while ( s1 < tstop1 && s2 < tstop2 ) {
2442 i1 = CompareTerms(s1,s2,(WORD)(-1));
2447 else if ( i1 < 0 ) {
2470 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2471 MLOCK(ErrorMessageLock);
2472 MesPrint(
"Addition of coefficients of PolyFun");
2473 MUNLOCK(ErrorMessageLock);
2483 *mm = WORDDIF(m,mm);
2484 if ( AN.ncmod != 0 ) {
2485 if ( Modulus(mm) ) Terminate(-1);
2492 while ( s1 < tstop1 ) *m++ = *s1++;
2493 while ( s2 < tstop2 ) *m++ = *s2++;
2494 w[1] = WORDDIF(m,w);
2495 w[FUNHEAD] = w[1] - FUNHEAD;
2496 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2497 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2498 else w[1] = FUNHEAD+2;
2499 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2502 AT.SS->PolyFlag = oldPolyFlag;
2539 WORD *stopper1, *stopper2, *t2;
2541 WORD *stopex1, *stopex2;
2544 WORD count = -1, localPoly, polyhit = -1;
2546 if ( AR.sLevel == 0 ) {
2550 if ( S->PolyFlag ) {
2558 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2559 S->PolyFlag = AR.PolyFunType;
2560 if ( AR.PolyFunType == 2 &&
2561 ( AR.PolyFunExp == 2 || AR.PolyFunExp == 3 ) ) S->PolyFlag = 1;
2563 else { localPoly = 0; }
2567 GETSTOP(term2,stopper2);
2570 while ( t1 < stopper1 && t2 < stopper2 ) {
2572 if ( *t1 == HAAKJE )
return(PREV(-1));
2573 if ( *t2 == HAAKJE )
return(PREV(1));
2574 if ( *t1 >= (FUNCTION-1) ) {
2575 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2576 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2577 if ( *t1 < FUNCTION )
return(PREV(1));
2578 if ( *t2 < FUNCTION )
return(PREV(-1));
2579 c1 = functions[*t1-FUNCTION].commute;
2580 c2 = functions[*t2-FUNCTION].commute;
2582 if ( c2 )
return(PREV(1));
2583 else return(PREV(*t2-*t1));
2586 if ( !c2 )
return(PREV(-1));
2587 else return(PREV(*t2-*t1));
2590 else return(PREV(*t2-*t1));
2597 if ( localPoly && c1 < FUNCTION ) {
2600 if ( c1 <= (FUNCTION-1)
2601 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec ) ) {
2602 if ( c1 == SYMBOL ) {
2603 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2604 && s1[-1] == 4 && s2[-1] == 4
2605 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2606 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2613 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2616 else if ( AR.SortType >= SORTPOWERFIRST ) {
2619 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2621 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2624 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2631 if ( AR.SortType==SORTLOWFIRST ) {
2632 return(PREV((s1[1]>0?-1:1)));
2635 return(PREV((s1[1]<0?-1:1)));
2640 if ( AR.SortType==SORTLOWFIRST ) {
2642 return(PREV((s1[1]<0?1:-1)));
2645 return(PREV((s2[1]<0?-1:1)));
2650 return(PREV((s1[1]<0?-1:1)));
2653 return(PREV((s2[1]<0?1:-1)));
2658 if ( *s1 != *s2 )
return(
2659 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2664 if ( AR.SortType==SORTLOWFIRST ) {
2665 return(PREV((s2[1]<0?-1:1)));
2668 return(PREV((s2[1]<0?1:-1)));
2672 else if ( c1 == DOTPRODUCT ) {
2673 if ( AR.SortType >= SORTPOWERFIRST ) {
2676 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2678 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2681 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2686 if ( s2 >= t2 )
return(PREV(1));
2687 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2689 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2691 if ( *s1 != *s2 )
return(
2692 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2695 if ( s2 < t2 )
return(PREV(-1));
2699 if ( s2 >= t2 )
return(PREV(1));
2700 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2703 if ( s2 < t2 )
return(PREV(-1));
2711 if ( localPoly && c1 == AR.PolyFun ) {
2713 if ( S->PolyFlag == 1 ) {
2715 if ( *s1 > 0 ) i1 = *s1;
2716 else if ( *s1 <= -FUNCTION ) i1 = 1;
2718 if ( *s2 > 0 ) i2 = *s2;
2719 else if ( *s2 <= -FUNCTION ) i2 = 1;
2721 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2726 WORD *u1, *u2, *ustop;
2728 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2735 ustop = u2 - ABS(u2[-1]);
2737 while ( u1 < ustop ) {
2738 if ( *u1 == INDEX )
goto NoPoly;
2745 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2752 ustop = u2 - ABS(u2[-1]);
2754 while ( u1 < ustop ) {
2755 if ( *u1 == INDEX )
goto NoPoly;
2762 S->PolyWise = WORDDIF(s1,term1);
2763 S->PolyWise -= FUNHEAD;
2769 S->PolyWise = localPoly = 0;
2772 else if ( AR.PolyFunType == 2 ) {
2773 WORD i1, i2, i1a, i2a;
2774 if ( *s1 > 0 ) i1 = *s1;
2775 else if ( *s1 <= -FUNCTION ) i1 = 1;
2777 if ( *s2 > 0 ) i2 = *s2;
2778 else if ( *s2 <= -FUNCTION ) i2 = 1;
2780 if ( s1[i1] > 0 ) i1a = s1[i1];
2781 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2783 if ( s2[i2] > 0 ) i2a = s2[i2];
2784 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2786 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2791 WORD *u1, *u2, *ustop;
2793 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2798 while ( u1 < s1+i1 ) {
2800 ustop = u2 - ABS(u2[-1]);
2802 while ( u1 < ustop ) {
2803 if ( *u1 == INDEX )
goto NoPoly;
2810 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2814 u1 = s1 +i1 + ARGHEAD;
2817 ustop = u2 - ABS(u2[-1]);
2819 while ( u1 < ustop ) {
2820 if ( *u1 == INDEX )
goto NoPoly;
2827 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2832 while ( u1 < s2+i2 ) {
2834 ustop = u2 - ABS(u2[-1]);
2836 while ( u1 < ustop ) {
2837 if ( *u1 == INDEX )
goto NoPoly;
2844 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2848 u1 = s2 + i2 + ARGHEAD;
2851 ustop = u2 - ABS(u2[-1]);
2853 while ( u1 < ustop ) {
2854 if ( *u1 == INDEX )
goto NoPoly;
2861 S->PolyWise = WORDDIF(s1,term1);
2862 S->PolyWise -= FUNHEAD;
2867 S->PolyWise = localPoly = 0;
2871 S->PolyWise = localPoly = 0;
2875 t1 = term1 + S->PolyWise;
2876 t2 = term2 + S->PolyWise;
2886 if ( s2 >= t2 )
return(PREV(-1));
2893 if ( AC.properorderflag ) {
2895 oldpolyflag = S->PolyFlag;
2897 if ( ( c2 = -CompArg(s1,s2) ) != 0 ) {
2898 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2900 S->PolyFlag = oldpolyflag;
2909 if ( s2 >= t2 )
return(PREV(-1));
2911 s1 += ARGHEAD; s2 += ARGHEAD;
2912 oldpolyflag = S->PolyFlag;
2914 while ( s1 < stopex1 ) {
2915 if ( s2 >= stopex2 ) {
2916 S->PolyFlag = oldpolyflag;
return(PREV(-1));
2918 if ( ( c2 = CompareTerms(s1,s2,(WORD)1) ) != 0 ) {
2919 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2924 S->PolyFlag = oldpolyflag;
2925 if ( s2 < stopex2 )
return(PREV(1));
2927 else return(PREV(1));
2930 if ( *s2 > 0 )
return(PREV(-1));
2931 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2932 if ( *s1 > -FUNCTION ) {
2933 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2939 if ( s2 < t2 )
return(PREV(1));
2943 if ( AR.SortType != SORTLOWFIRST ) {
2944 if ( t1 < stopper1 )
return(PREV(1));
2945 if ( t2 < stopper2 )
return(PREV(-1));
2948 if ( t1 < stopper1 )
return(PREV(-1));
2949 if ( t2 < stopper2 )
return(PREV(1));
2952 if ( level == 3 )
return(
CompCoef(term1,term2));
2979 WORD *t1, *t2, *tt1, *tt2;
2982 if ( AR.SortType == SORTLOWFIRST ) { low = 1; high = -1; }
2983 else { low = -1; high = 1; }
2984 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
2985 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
2986 if ( AN.polysortflag > 0 ) {
2988 while ( t1 < tt1 ) { sum1 += t1[1]; t1 += 2; }
2989 while ( t2 < tt2 ) { sum2 += t2[1]; t2 += 2; }
2990 if ( sum1 < sum2 )
return(low);
2991 if ( sum1 > sum2 )
return(high);
2992 t1 = term1+3; t2 = term2 + 3;
2994 while ( t1 < tt1 && t2 < tt2 ) {
2995 if ( *t1 > *t2 )
return(low);
2996 if ( *t1 < *t2 )
return(high);
2997 if ( t1[1] < t2[1] )
return(low);
2998 if ( t1[1] > t2[1] )
return(high);
3001 if ( t1 < tt1 )
return(high);
3002 if ( t2 < tt2 )
return(low);
3022 WORD *t1, *t2, *tt1, *tt2, *ttt1, *ttt2;
3024 DUMMYUSE(AT.WorkPointer);
3025 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
3026 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
3027 while ( t1 < tt1 && t2 < tt2 ) {
3029 if ( t1[0] < t2[0] )
return(-1);
3032 else if ( *t1 == HAAKJE ) {
3033 t1 += 3; t2 += 3;
continue;
3035 ttt1 = t1+t1[1]; ttt2 = t2+t2[1];
3036 while ( t1 < ttt1 && t2 < ttt2 ) {
3037 if ( *t1 > *t2 )
return(-1);
3038 if ( *t1 < *t2 )
return(1);
3039 if ( t1[1] < t2[1] )
return(-1);
3040 if ( t1[1] > t2[1] )
return(1);
3043 if ( t1 < ttt1 )
return(1);
3044 if ( t2 < ttt2 )
return(-1);
3046 if ( t1 < tt1 )
return(1);
3047 if ( t2 < tt2 )
return(-1);
3112 if ( AT.SS == AT.S0 && !AR.NoCompress ) {
3113 if ( AN.compressSize == 0 ) {
3114 if ( *ss ) { AN.compressSize = **ss + 64; }
3115 else { AN.compressSize = AM.MaxTer/
sizeof(WORD) + 2; }
3116 AN.compressSpace = (WORD *)Malloc1(AN.compressSize*
sizeof(WORD),
"Compression");
3118 AN.compressSpace[0] = 0;
3123 if ( j > AN.compressSize ) {
3125 t = (WORD *)Malloc1(newsize*
sizeof(WORD),
"Compression");
3127 if ( AN.compressSpace ) {
3128 for ( i = 0; i < *AN.compressSpace; i++ ) t[i] = AN.compressSpace[i];
3129 M_free(AN.compressSpace,
"Compression");
3131 AN.compressSpace = t;
3132 AN.compressSize = newsize;
3134 t = AN.compressSpace;
3138 WORD *polystop, *sa;
3142 while ( polystop < sa && *polystop != AR.PolyFun ) {
3143 polystop += polystop[1];
3145 while ( i > 0 && j > 0 && *s == *t && s < polystop ) {
3146 i--; j--; s++; t++; k--;
3153 while ( i > 0 && j > 0 && *s == *t && s < sa ) { i--; j--; s++; t++; k--; }
3162 size += *AN.compressSpace;
3163 if ( k == -1 ) { t--; s--; j++; }
3165 while ( --j >= 0 ) *t++ = *s++;
3167 t = AN.compressSpace + *AN.compressSpace;
3168 t[-(ABS(t[-1]))] = 0;
3237 #ifdef NEWSPLITMERGE
3243 WORD **pp3, **pp1, **pp2;
3244 LONG i, newleft, newright, split;
3246 if ( number < 2 )
return(number);
3247 if ( number == 2 ) {
3248 pp1 = Pointer; pp2 = pp1 + 1;
3249 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3250 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3252 else if ( i == 0 ) {
3254 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) number = 0; }
3255 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) number = 0; }
3261 newright =
SplitMerge(BHEAD Pointer+split,number-split);
3262 if ( newright == 0 )
return(newleft);
3270 if ( newleft > 0 && newright > 0 &&
3271 ( i = CompareTerms(Pointer[newleft-1],Pointer[split],(WORD)0) ) >= 0 ) {
3272 pp2 = Pointer+split; pp1 = Pointer+newleft-1;
3274 if ( S->PolyWise ) {
3275 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3279 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3285 newleft += newright;
3287 while ( --newright >= 0 ) *pp1++ = *pp2++;
3292 if ( split >= AN.SplitScratchSize ) {
3293 AN.SplitScratchSize = (split*3)/2+100;
3294 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3295 AN.SplitScratchSize = S->Terms2InSmall/2;
3296 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3297 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3299 pp3 = AN.SplitScratch; pp1 = Pointer;
3300 for ( i = 0; i < newleft; i++ ) *pp3++ = *pp1++;
3301 AN.InScratch = newleft;
3302 pp1 = AN.SplitScratch; pp2 = Pointer + split; pp3 = Pointer;
3306 while ( newleft > 8 ) {
3307 LONG nnleft = newleft/2;
3308 if ( ( i = CompareTerms(pp1[nnleft],*pp2,(WORD)0) ) < 0 )
break;
3311 newleft -= nnleft+1;
3313 if ( S->PolyWise ) { i =
AddPoly(BHEAD pp3-1,pp2); }
3314 else { i =
AddCoef(BHEAD pp3-1,pp2); }
3315 if ( i == 0 ) pp3--;
3322 while ( newleft > 0 && newright > 0 ) {
3323 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3332 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3333 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3334 pp1++; pp2++; newleft--; newright--;
3337 for ( i = 0; i < newleft; i++ ) *pp3++ = *pp1++;
3341 for ( i = 0; i < newright; i++ ) *pp3++ = *pp2++;
3344 return(pp3 - Pointer);
3349 LONG
SplitMerge(PHEAD WORD **Pointer, LONG number)
3353 WORD **pp3, **pp1, **pp2;
3354 LONG nleft, nright, i, newleft, newright;
3357 if ( number < 2 )
return(number);
3358 if ( number == 2 ) {
3359 pp1 = Pointer; pp2 = pp1 + 1;
3360 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3361 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3363 else if ( i == 0 ) {
3365 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3366 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3370 pptop = Pointer + number;
3371 nleft = number >> 1; nright = number - nleft;
3373 newright =
SplitMerge(BHEAD Pointer+nleft,nright);
3381 if ( newleft > 0 && newright > 0 &&
3382 ( i = CompareTerms(Pointer[newleft-1],Pointer[nleft],(WORD)0) ) >= 0 ) {
3383 pp2 = Pointer+nleft; pp1 = Pointer+newleft-1;
3385 if ( S->PolyWise ) {
3386 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3390 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3393 *pp2++ = 0; newright--;
3396 newleft += newright;
3398 while ( --newright >= 0 ) *pp1++ = *pp2++;
3399 while ( pp1 < pptop ) *pp1++ = 0;
3403 if ( nleft > AN.SplitScratchSize ) {
3404 AN.SplitScratchSize = (nleft*3)/2+100;
3405 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3406 AN.SplitScratchSize = S->Terms2InSmall/2;
3407 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3408 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3410 pp3 = AN.SplitScratch; pp1 = Pointer; i = nleft;
3411 do { *pp3++ = *pp1; *pp1++ = 0; }
while ( *pp1 && --i > 0 );
3412 if ( i > 0 ) { *pp3 = 0; i--; }
3413 AN.InScratch = nleft - i;
3414 pp1 = AN.SplitScratch; pp2 = Pointer + nleft; pp3 = Pointer;
3415 while ( nleft > 0 && nright > 0 && *pp1 && *pp2 ) {
3416 if ( ( i = CompareTerms(*pp1,*pp2,(WORD)0) ) < 0 ) {
3427 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3428 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3429 *pp1++ = 0; *pp2++ = 0; nleft--; nright--;
3432 while ( --nleft >= 0 && *pp1 ) { *pp3++ = *pp1; *pp1++ = 0; }
3433 while ( --nright >= 0 && *pp2 ) { *pp3++ = *pp2++; }
3434 nleft = pp3 - Pointer;
3435 while ( pp3 < pptop ) *pp3++ = 0;
3465 WORD **Point, *s2, *t, *garbuf, i;
3467 int tobereturned = 0;
3472 MLOCK(ErrorMessageLock);
3473 MesPrint(
"in: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3475 Point = S->sPointer;
3477 while ( --k >= 0 ) {
3478 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3480 Point = AN.SplitScratch;
3482 while ( --k >= 0 ) {
3483 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3486 MesPrint(
"total = %l, nterms = %l",2*total,AN.InScratch);
3487 MUNLOCK(ErrorMessageLock);
3493 if ( S->lBuffer != 0 && S->lFill + total <= S->lTop ) {
3497 garbuf = (WORD *)Malloc1(total*
sizeof(WORD),
"Garbage buffer");
3501 Point = S->sPointer;
3503 while ( --k >= 0 ) {
3511 Point = AN.SplitScratch;
3513 while ( --k >= 0 ) {
3523 Point = S->sPointer;
3525 while ( --k >= 0 ) {
3533 Point = AN.SplitScratch;
3535 while ( --k >= 0 ) {
3545 MLOCK(ErrorMessageLock);
3546 MesPrint(
"out: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3547 if ( S->sFill >= S->sTop2 ) {
3548 MesPrint(
"We are in deep trouble");
3550 MUNLOCK(ErrorMessageLock);
3552 if ( tobereturned ) M_free(garbuf,
"Garbage buffer");
3580 WORD **poin, **poin2, ul, k, i, im, *m1;
3581 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3582 WORD *m2, *m3, r31, r33, ki, *rr;
3592 int oldgzipCompress = AR.gzipCompress;
3594 AR.gzipCompress = 0;
3598 fout = &(AR.FoStage4[0]);
3601 poin = S->poina; poin2 = S->poin2a;
3602 rr = AR.CompressPointer;
3609 if ( fout->
handle < 0 ) {
3611 PUTZERO(AN.OldPosOut);
3612 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3613 MLOCK(ErrorMessageLock);
3614 MesPrint(
"Cannot create file %s",fout->name);
3615 MUNLOCK(ErrorMessageLock);
3619 MLOCK(ErrorMessageLock);
3620 MesPrint(
"%w MergePatches created output file %s",fout->name);
3621 MUNLOCK(ErrorMessageLock);
3624 PUTZERO(fout->filesize);
3625 PUTZERO(fout->POposition);
3633 LOCK(fout->pthreadslock);
3635 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3637 UNLOCK(fout->pthreadslock);
3640 PUTZERO(S->fPatches[0]);
3641 fout->POfill = fout->PObuffer;
3642 PUTZERO(fout->POposition);
3647 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3648 S->fpcompressed[S->fPatchN] = 1;
3650 S->fpcompressed[S->fPatchN] = 0;
3651 SetupOutputGZIP(fout);
3654 else if ( par == 0 && S->stage4 > 0 ) {
3666 S->iPatches = S->fPatches;
3667 S->fPatches = S->inPatches;
3668 S->inPatches = S->iPatches;
3669 (S->inNum) = S->fPatchN;
3670 AN.OldPosIn = AN.OldPosOut;
3672 m1 = S->fpincompressed;
3673 S->fpincompressed = S->fpcompressed;
3674 S->fpcompressed = m1;
3675 for ( i = 0; i < S->inNum; i++ ) {
3676 S->fPatchesStop[i] = S->iPatches[i+1];
3678 MLOCK(ErrorMessageLock);
3679 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3680 MUNLOCK(ErrorMessageLock);
3692 AR.gzipCompress = 0;
3695 S->iPatches = S->fPatches;
3696 S->inNum = S->fPatchN;
3698 m1 = S->fpincompressed;
3699 S->fpincompressed = S->fpcompressed;
3700 S->fpcompressed = m1;
3701 for ( i = 0; i < S->inNum; i++ ) {
3702 S->fPatchesStop[i] = S->fPatches[i+1];
3704 MLOCK(ErrorMessageLock);
3705 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3706 MUNLOCK(ErrorMessageLock);
3714 S->Patches[S->lPatch] = S->lFill;
3715 for ( i = 0; i < S->lPatch; i++ ) {
3716 S->pStop[i] = S->Patches[i+1]-1;
3717 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3721 S->lPatch = (S->inNum);
3723 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3725 if ( S->lPatch > 1 ) {
3728 SetupAllInputGZIP(S);
3731 for ( i = 0; i < S->lPatch; i++ ) {
3732 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3734 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3735 S->pStop[i] = m2 = p;
3737 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3739 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3744 if ( fout->
handle >= 0 ) {
3747 LOCK(fout->pthreadslock);
3749 SeekFile(fout->
handle,&position,SEEK_END);
3750 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3752 UNLOCK(fout->pthreadslock);
3756 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3765 if ( S->lPatch == 1 ) {
3768 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3774 m2 = m1 = *S->Patches;
3777 i = -(*m1++); m2 += i; im = *m1+i+1;
3778 while ( i > 0 ) { *m1-- = *m2--; i--; }
3782 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD m1); }
3785 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3786 ADDPOS(S->SizeInFile[par],im);
3791 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3794 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3795 ADDPOS(S->SizeInFile[par],1);
3798 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3799 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3801 ADDPOS(position,length);
3802 ADDPOS(fout->POposition,length);
3803 ADDPOS(fout->filesize,length);
3804 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3814 #define FRONTSIZE (2*AM.MaxTer)
3815 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3817 SetupAllInputGZIP(S);
3819 position2 = S->iPatches[0];
3820 while ( ( length = FillInputGZIP(fin,&position2,
3822 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3823 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3824 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3825 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3833 i = -(*m1++); m2 += i; im = *m1+i+1;
3834 while ( i > 0 ) { *m1-- = *m2--; i--; }
3838 if ( AS.MasterSort && ( fout == AR.outfile ) ) {
3839 im = PutToMaster(BHEAD m1);
3843 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3844 ADDPOS(S->SizeInFile[par],im);
3848 if ( m1 < copytop && *m1 == 0 )
break;
3854 while ( m1 > m2 ) *--m3 = *--m1;
3859 MLOCK(ErrorMessageLock);
3860 MesPrint(
"Readerror");
3864 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3867 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3868 ADDPOS(S->SizeInFile[par],1);
3871 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3872 while ( ( length = ReadFile(fin->
handle,
3873 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3874 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3876 ADDPOS(position,length);
3877 ADDPOS(fout->POposition,length);
3878 ADDPOS(fout->filesize,length);
3879 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3882 MLOCK(ErrorMessageLock);
3883 MesPrint(
"Readerror");
3890 else if ( S->lPatch > 0 ) {
3895 do { lpat *= 2; }
while ( lpat < S->lPatch );
3896 mpat = ( lpat >> 1 ) - 1;
3897 k = lpat - S->lPatch;
3902 for ( i = 1; i < lpat; i++ ) {
3905 for ( i = 1; i <= k; i++ ) {
3907 poin[im] = S->Patches[i-1];
3908 poin2[im] = poin[im] + *(poin[im]);
3911 S->tree[mpat+i] = 0;
3912 poin[im-1] = poin2[im-1] = 0;
3914 for ( i = (k*2)+1; i <= lpat; i++ ) {
3917 poin[i] = S->Patches[i-k-1];
3918 poin2[i] = poin[i] + *(poin[i]);
3938 if ( !*(poin[k]) ) {
3939 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3940 if ( S->tree[i] == -1 ) {
3953 if ( S->tree[i] > 0 ) {
3954 if ( ( c = CompareTerms(poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3958 S->used[level] = S->tree[i];
3968 l1 = *( m1 = poin[S->tree[i]] );
3969 l2 = *( m2 = poin[k] );
3970 if ( S->PolyWise ) {
3975 if ( S->PolyFlag == 2 ) {
3976 w = poly_ratfun_add(BHEAD m1,m2);
3977 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3978 MLOCK(ErrorMessageLock);
3979 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3980 MUNLOCK(ErrorMessageLock);
3987 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3988 MLOCK(ErrorMessageLock);
3989 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3990 MUNLOCK(ErrorMessageLock);
3997 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
3999 if ( r1 == m1[1] ) {
4002 else if ( r1 < m1[1] ) {
4006 while ( --r1 >= 0 ) *--m1 = *--m2;
4009 while ( --r1 >= 0 ) *--m1 = *--m2;
4011 poin[S->tree[i]] = m1;
4019 poin[S->tree[i]] = m2;
4026 r1 = *( m1 += l1 - 1 );
4028 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
4029 r2 = *( m2 += l2 - 1 );
4031 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
4033 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
4034 MLOCK(ErrorMessageLock);
4035 MesCall(
"MergePatches");
4036 MUNLOCK(ErrorMessageLock);
4040 if ( AN.ncmod != 0 ) {
4041 if ( ( AC.modmode & POSNEG ) != 0 ) {
4044 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
4046 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
4048 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
4052 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
4053 if ( r3 < 0 ) r3 = -r3;
4054 if ( r1 < 0 ) r1 = -r1;
4059 ul = S->used[level] = S->tree[i];
4066 r1 = poin2[ul][1] - im + 1;
4068 m2 = poin[ul] - im + 1;
4069 while ( ++im <= 0 ) *--m1 = *--m2;
4074 poin[ul] = poin2[ul];
4076 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
4079 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
4081 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4082 S->Patches[ki],&(poin[ul]),ki));
4084 poin2[ul] = poin[ul] + im;
4089 S->used[++level] = k;
4095 else if ( r31 < 0 ) {
4104 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
4110 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
4111 MLOCK(ErrorMessageLock);
4112 MesPrint(
"Coefficient overflow during sort");
4113 MUNLOCK(ErrorMessageLock);
4116 m2 = poin[S->tree[i]];
4117 m3 = ( poin[S->tree[i]] -= r31 );
4118 do { *m3++ = *m2++; }
while ( m2 < m1 );
4122 *(poin[S->tree[i]]) += r31;
4124 m2 = (WORD *)coef; im = r3;
4135 r1 = poin2[k][1] - im + 1;
4137 m2 = poin[k] - im + 1;
4138 while ( ++im <= 0 ) *--m1 = *--m2;
4145 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
4148 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
4150 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4151 S->Patches[ki],&(poin[k]),ki));
4153 poin2[k] = poin[k] + im;
4161 else if ( S->tree[i] < 0 ) {
4172 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD poin[k]); }
4175 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
4176 MLOCK(ErrorMessageLock);
4177 MesPrint(
"Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
4178 MUNLOCK(ErrorMessageLock);
4181 ADDPOS(S->SizeInFile[par],im);
4189 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
4192 if (
FlushOut(&position,fout,1) )
goto ReturnError;
4193 ADDPOS(S->SizeInFile[par],1);
4197 SeekFile(fout->
handle,&position,SEEK_CUR);
4200 S->fPatches[S->fPatchN] = position;
4202 if ( par == 0 && fout != AR.outfile ) {
4211 S->fPatches[S->fPatchN] = position;
4212 if ( ISNOTZEROPOS(AN.OldPosIn) ) {
4214 SeekFile(fin->
handle,&(AN.OldPosIn),SEEK_SET);
4220 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
4222 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&AN.OldPosIn),(LONG)
sizeof(
POSITION)) !=
4224 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
4226 MLOCK(ErrorMessageLock);
4227 MesPrint(
"Read error fourth stage sorting");
4228 MUNLOCK(ErrorMessageLock);
4233 for ( i = 0; i < S->inNum; i++ ) {
4234 S->fPatchesStop[i] = S->iPatches[i+1];
4236 MLOCK(ErrorMessageLock);
4237 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
4238 MUNLOCK(ErrorMessageLock);
4260 MLOCK(ErrorMessageLock);
4261 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4262 MUNLOCK(ErrorMessageLock);
4272 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4273 PUTZERO(S->SizeInFile[0]);
4287 MLOCK(ErrorMessageLock);
4288 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4289 MUNLOCK(ErrorMessageLock);
4294 AR.gzipCompress = oldgzipCompress;
4299 AR.gzipCompress = oldgzipCompress;
4304 MLOCK(ErrorMessageLock);
4305 MesPrint(
"Error while writing to file.");
4309 MLOCK(ErrorMessageLock);
4311 MesCall(
"MergePatches");
4312 MUNLOCK(ErrorMessageLock);
4314 AR.gzipCompress = oldgzipCompress;
4336 WORD **ss, *lfill, j, *t;
4338 LONG lSpace, sSpace, RetCode, over, tover;
4340 if ( ( ( AP.PreDebug & DUMPTOSORT ) == DUMPTOSORT ) && AR.sLevel == 0 ) {
4342 sprintf((
char *)(THRbuf),
"StoreTerm(%d)",AT.identity);
4343 PrintTerm(term,(
char *)(THRbuf));
4345 PrintTerm(term,
"StoreTerm");
4348 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
4349 S->sFill = *(S->PoinFill);
4350 if ( S->sTerms >= S->TermsInSmall || ( S->sFill + *term ) >= S->sTop ) {
4354 tover = over = S->sTerms;
4358 PrintTime((UBYTE *)
"Before SplitMerge");
4362 PrintTime((UBYTE *)
"After SplitMerge");
4367 S->TermsLeft -= over - RetCode;
4371 lSpace = sSpace + (S->lFill - S->lBuffer)
4372 - (AM.MaxTer/
sizeof(WORD))*((LONG)S->lPatch);
4373 SETBASEPOSITION(pp,lSpace);
4374 MULPOS(pp,
sizeof(WORD));
4375 if ( S->file.handle >= 0 ) {
4376 ADD2POS(pp,S->fPatches[S->fPatchN]);
4379 WORD oldLogHandle = AC.LogHandle;
4380 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4382 AC.LogHandle = oldLogHandle;
4384 if ( ( S->lPatch >= S->MaxPatches ) ||
4385 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer ) ) >= S->lTop ) ) {
4395 SETBASEPOSITION(pp,sSpace);
4396 MULPOS(pp,
sizeof(WORD));
4397 ADD2POS(pp,S->fPatches[S->fPatchN]);
4400 WORD oldLogHandle = AC.LogHandle;
4401 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4403 AC.LogHandle = oldLogHandle;
4406 S->lFill = S->lBuffer;
4408 S->Patches[S->lPatch++] = S->lFill;
4409 lfill = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
4412 while ( ( t = *ss++ ) != 0 ) {
4414 if ( j < 0 ) j = t[1] + 2;
4423 S->PoinFill = S->sPointer;
4424 *(S->PoinFill) = S->sFill = S->sBuffer;
4427 while ( --j >= 0 ) *S->sFill++ = *term++;
4431 *++S->PoinFill = S->sFill;
4436 MLOCK(ErrorMessageLock);
4437 MesCall(
"StoreTerm");
4438 MUNLOCK(ErrorMessageLock);
4456 if ( S->fPatchN >= S->MaxFpatches ) {
4471 MLOCK(ErrorMessageLock);
4472 MesPrint(
"Currently Stage 4 sorts are not allowed for function arguments or $ variables.");
4473 MesPrint(
"Please increase correspondingsorting parameters (sub-) in the setup.");
4474 MUNLOCK(ErrorMessageLock);
4478 MLOCK(ErrorMessageLock);
4480 MesPrint(
"StageSort in thread %d",identity);
4481 #elif defined(WITHMPI)
4482 MesPrint(
"StageSort in process %d",PF.me);
4484 MesPrint(
"StageSort");
4486 MUNLOCK(ErrorMessageLock);
4487 SeekFile(fout->
handle,&position,SEEK_END);
4492 if ( (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(S->fPatchN)),(LONG)
sizeof(WORD)) !=
4494 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(AN.OldPosOut)),(LONG)
sizeof(
POSITION)) !=
4496 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(S->fPatches),(LONG)(S->fPatchN+1)
4498 MLOCK(ErrorMessageLock);
4499 MesPrint(
"Write error while staging sort. Disk full?");
4500 MUNLOCK(ErrorMessageLock);
4503 AN.OldPosOut = position;
4504 fout->filesize = position;
4505 ADDPOS(fout->filesize,(S->fPatchN+2)*
sizeof(
POSITION) +
sizeof(WORD));
4506 fout->POposition = fout->filesize;
4507 S->fPatches[0] = fout->filesize;
4510 if ( AR.FoStage4[0].PObuffer == 0 ) {
4511 AR.FoStage4[0].PObuffer = (WORD *)Malloc1(AR.FoStage4[0].POsize*
sizeof(WORD)
4513 AR.FoStage4[0].POfill = AR.FoStage4[0].PObuffer;
4514 AR.FoStage4[0].POstop = AR.FoStage4[0].PObuffer
4515 + AR.FoStage4[0].POsize/
sizeof(WORD);
4517 AR.FoStage4[0].pthreadslock = dummylock;
4520 if ( AR.FoStage4[1].PObuffer == 0 ) {
4521 AR.FoStage4[1].PObuffer = (WORD *)Malloc1(AR.FoStage4[1].POsize*
sizeof(WORD)
4523 AR.FoStage4[1].POfill = AR.FoStage4[1].PObuffer;
4524 AR.FoStage4[1].POstop = AR.FoStage4[1].PObuffer
4525 + AR.FoStage4[1].POsize/
sizeof(WORD);
4527 AR.FoStage4[1].pthreadslock = dummylock;
4554 WORD *v, *s, *m, k, i;
4555 WORD *pScrat, *stop, *sv, error = 0;
4556 pScrat = AT.WorkPointer;
4557 if ( ( AT.WorkPointer + 8 * AM.MaxWildcards ) >= AT.WorkTop ) {
4558 MLOCK(ErrorMessageLock);
4560 MUNLOCK(ErrorMessageLock);
4568 while ( v < stop && (
4569 *v == FROMSET || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4570 while ( v < stop ) {
4572 if ( AM.Ordering[*v] < AM.Ordering[*m] ) {
4575 else if ( *v == *m ) {
4576 if ( v[2] < m[2] ) {
4579 else if ( v[2] == m[2] ) {
4582 if ( s < stop && ( *s == FROMSET
4583 || *s == SETTONUM || *s == LOADDOLLAR ) ) {
4584 if ( sv < stop && ( *sv == FROMSET
4585 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4586 if ( s[2] != sv[2] ) {
4588 MLOCK(ErrorMessageLock);
4589 MesPrint(
"&Wildcard set conflict");
4590 MUNLOCK(ErrorMessageLock);
4596 if ( sv < stop && ( *sv == FROMSET
4597 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4609 while ( v < stop && ( *v == FROMSET
4610 || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4616 while ( m < stop && ( *m == FROMSET
4617 || *m == SETTONUM || *m == LOADDOLLAR ) ) {
4624 while ( i < nw && ( w[i] < 0 || w[i] == FROMSET
4625 || w[i] == SETTONUM || w[i] == LOADDOLLAR ) ) i += w[i+1];
4627 AC.NwildC = k = WORDDIF(pScrat,AT.WorkPointer);
4647 int minnum = num, i;
4648 if ( AN.FunSorts ) {
4650 if ( AN.MaxFunSorts > 3 ) {
4651 minnum = (AN.MaxFunSorts+4)/2;
4655 else if ( minnum == 0 ) minnum = 1;
4656 for ( i = minnum; i < AN.NumFunSorts; i++ ) {
4659 if ( S->file.handle >= 0 ) {
4663 ClearSortGZIP(&(S->file));
4665 CloseFile(S->file.handle);
4666 S->file.handle = -1;
4667 remove(S->file.name);
4669 MLOCK(ErrorMessageLock);
4670 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4671 MUNLOCK(ErrorMessageLock);
4674 M_free(S,
"sorting struct");
4678 AN.MaxFunSorts = minnum;
4682 if ( S->file.handle >= 0 ) {
4686 ClearSortGZIP(&(S->file));
4688 CloseFile(S->file.handle);
4689 S->file.handle = -1;
4690 remove(S->file.name);
4692 MLOCK(ErrorMessageLock);
4693 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4694 MUNLOCK(ErrorMessageLock);
4700 for ( i = 0; i < 2; i++ ) {
4701 if ( AR.FoStage4[i].handle >= 0 ) {
4704 ClearSortGZIP(&(AR.FoStage4[i]));
4706 CloseFile(AR.FoStage4[i].handle);
4707 remove(AR.FoStage4[i].name);
4708 AR.FoStage4[i].handle = -1;
4710 MLOCK(ErrorMessageLock);
4711 MesPrint(
"%w CleanUpSort removed stage4 file %s",AR.FoStage4[i].name);
4712 MUNLOCK(ErrorMessageLock);
4729 if ( AR.sLevel >= 0 ) {
4731 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
4743 WORD *PolyRatFunSpecial(PHEAD WORD *t1, WORD *t2)
4745 WORD *oldworkpointer = AT.WorkPointer, *t, *r;
4749 if ( *t == -SYMBOL ) {
4750 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4752 if ( t[2] != -SNUMBER )
goto Illegal;
4755 else if ( *t == -SNUMBER ) {
4758 if ( *t == -SYMBOL ) {
4759 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4762 else if ( *t == -SNUMBER ) {
4766 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4767 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4771 exp1 = -t[ARGHEAD+4];
4775 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4776 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4780 exp1 = t[ARGHEAD+4];
4782 if ( *t != -SNUMBER )
goto Illegal;
4788 if ( *t == -SYMBOL ) {
4789 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4791 if ( t[2] != -SNUMBER )
goto Illegal;
4794 else if ( *t == -SNUMBER ) {
4797 if ( *t == -SYMBOL ) {
4798 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4801 else if ( *t == -SNUMBER ) {
4805 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4806 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4810 exp2 = -t[ARGHEAD+4];
4814 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4815 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4819 exp2 = t[ARGHEAD+4];
4821 if ( *t != -SNUMBER )
goto Illegal;
4826 if ( exp1 <= exp2 ) { i = t1[1]; r = t1; }
4827 else { i = t2[1]; r = t2; }
4831 return(oldworkpointer);
4833 MesPrint("Illegal occurrence of PolyRatFun with divergent option");
4845 VOID SimpleSplitMergeRec(WORD *array,WORD num,WORD *auxarray)
4847 WORD n1,n2,i,j,k,*t1,*t2;
4848 if ( num < 2 )
return;
4850 if ( array[0] > array[1] ) {
4851 EXCH(array[0],array[1])
4857 SimpleSplitMergeRec(array,n1,auxarray);
4858 SimpleSplitMergeRec(array+n1,n2,auxarray);
4859 if ( array[n1-1] <= array[n1] )
return;
4861 t1 = array; t2 = auxarray; i = n1; NCOPY(t2,t1,i);
4862 i = 0; j = n1; k = 0;
4863 while ( i < n1 && j < num ) {
4864 if ( auxarray[i] <= array[j] ) { array[k++] = auxarray[i++]; }
4865 else { array[k++] = array[j++]; }
4867 while ( i < n1 ) array[k++] = auxarray[i++];
4873 VOID SimpleSplitMerge(WORD *array,WORD num)
4875 WORD *auxarray = Malloc1(
sizeof(WORD)*num/2,
"SimpleSplitMerge");
4876 SimpleSplitMergeRec(array,num,auxarray);
4877 M_free(auxarray,
"SimpleSplitMerge");
4891 WORD BinarySearch(WORD *array,WORD num,WORD x)
4893 WORD i, bot, top, med;
4895 for ( i = 0; i < num; i++ )
if ( array[i] == x )
return(i);
4898 if ( array[0] > x || array[num-1] < x )
return(-1);
4899 bot = 0; top = num-1; med = (top+bot)/2;
4901 if ( array[med] == x )
return(med);
4902 if ( array[med] < x ) { bot = med+1; }
4903 else { top = med-1; }
4905 }
while ( med >= bot && med <= top );
int NormalModulus(UWORD *, WORD *)
VOID AddArgs(PHEAD WORD *, WORD *, WORD *)
WORD Compare1(WORD *, WORD *, WORD)
WORD CompareSymbols(WORD *, WORD *, WORD)
WORD AddCoef(PHEAD WORD **, WORD **)
VOID WriteStats(POSITION *, WORD)
WORD AddPoly(PHEAD WORD **, WORD **)
WORD CompareHSymbols(WORD *, WORD *, WORD)
WORD StoreTerm(PHEAD WORD *)
WORD Sflush(FILEHANDLE *)
int PF_ISendSbuf(int to, int tag)
WORD SortWild(WORD *, WORD)
LONG ComPress(WORD **, LONG *)
LONG SplitMerge(PHEAD WORD **, LONG)
BRACKETINDEX * indexbuffer
WORD PutOut(PHEAD WORD *, POSITION *, FILEHANDLE *, WORD)
VOID StageSort(FILEHANDLE *)
WORD FlushOut(POSITION *, FILEHANDLE *, int)
WORD CompCoef(WORD *, WORD *)
LONG PutIn(FILEHANDLE *, POSITION *, WORD *, WORD **, int)
LONG EndSort(PHEAD WORD *, int)