62 extern LONG numwrites;
65 extern LONG nummallocs;
75 char *toterms[] = {
" ",
" >>",
"-->" };
94 LONG millitime, y = 0x7FFFFFFFL >> 1;
99 if ( AT.SS == AT.S0 && AC.StatsFlag ) {
101 if ( AC.ThreadStats == 0 && identity > 0 )
return;
102 #elif defined(WITHMPI) 103 if ( AC.OldParallelStats )
return;
104 if ( ! AC.ProcessStats && PF.me != MASTER )
return;
106 if ( Expressions == 0 )
return;
110 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
112 AR.ShortSortCount = 0;
115 MLOCK(ErrorMessageLock);
116 if ( AC.ShortStats ) {}
119 if ( identity > 0 ) {
120 MesPrint(
" Thread %d reporting",identity);
125 #elif defined(WITHMPI) 126 if ( PF.me != MASTER ) {
127 MesPrint(
" Process %d reporting",PF.me);
142 use_wtime = AC.WTimeStatsFlag;
143 #if defined(WITHPTHREADS) 144 if ( use_wtime && identity > 0 ) use_wtime = 0;
145 #elif defined(WITHMPI) 146 if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
149 timepart = (WORD)(millitime%1000);
152 if ( AC.ShortStats ) {
153 #if defined(WITHPTHREADS) || defined(WITHMPI) 155 if ( identity > 0 ) {
157 if ( PF.me != MASTER ) {
158 const int identity = PF.me;
160 if ( par == 0 || par == 2 ) {
161 SETBASEPOSITION(pp,y);
162 if ( ISLESSPOS(*plspace,pp) ) {
163 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%10p %s %s",identity,
164 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
165 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
174 SETBASEPOSITION(pp,y);
176 if ( ISLESSPOS(*plspace,pp) ) {
177 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%11p %s %s",identity,
178 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
179 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
183 if ( ISLESSPOS(*plspace,pp) ) {
184 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%12p %s %s",identity,
185 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
186 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
190 if ( ISLESSPOS(*plspace,pp) ) {
191 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%13p %s %s",identity,
192 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
193 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
197 if ( ISLESSPOS(*plspace,pp) ) {
198 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%14p %s %s",identity,
199 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
200 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
204 if ( ISLESSPOS(*plspace,pp) ) {
205 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%15p %s %s",identity,
206 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
207 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
211 if ( ISLESSPOS(*plspace,pp) ) {
212 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%16p %s %s",identity,
213 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
214 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
218 if ( ISLESSPOS(*plspace,pp) ) {
219 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%17p %s %s",identity,
220 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
221 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
227 else if ( par == 1 ) {
228 SETBASEPOSITION(pp,y);
229 if ( ISLESSPOS(*plspace,pp) ) {
230 MesPrint(
"%d: %7l.%2is %10l:%10p",identity,millitime,timepart,
231 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
235 SETBASEPOSITION(pp,y);
237 if ( ISLESSPOS(*plspace,pp) ) {
238 MesPrint(
"%d: %7l.%2is %10l:%11p",identity,millitime,timepart,
239 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
243 if ( ISLESSPOS(*plspace,pp) ) {
244 MesPrint(
"%d: %7l.%2is %10l:%12p",identity,millitime,timepart,
245 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
249 if ( ISLESSPOS(*plspace,pp) ) {
250 MesPrint(
"%d: %7l.%2is %10l:%13p",identity,millitime,timepart,
251 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
255 if ( ISLESSPOS(*plspace,pp) ) {
256 MesPrint(
"%d: %7l.%2is %10l:%14p",identity,millitime,timepart,
257 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
261 if ( ISLESSPOS(*plspace,pp) ) {
262 MesPrint(
"%d: %7l.%2is %10l:%15p",identity,millitime,timepart,
263 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
267 if ( ISLESSPOS(*plspace,pp) ) {
268 MesPrint(
"%d: %7l.%2is %10l:%16p",identity,millitime,timepart,
269 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
273 if ( ISLESSPOS(*plspace,pp) ) {
274 MesPrint(
"%d: %7l.%2is %10l:%17p",identity,millitime,timepart,
275 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
283 if ( par == 0 || par == 2 ) {
284 SETBASEPOSITION(pp,y);
285 if ( ISLESSPOS(*plspace,pp) ) {
286 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%10p %s %s",
287 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
288 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
297 SETBASEPOSITION(pp,y);
299 if ( ISLESSPOS(*plspace,pp) ) {
300 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%11p %s %s",
301 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
302 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
306 if ( ISLESSPOS(*plspace,pp) ) {
307 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%12p %s %s",
308 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
309 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
313 if ( ISLESSPOS(*plspace,pp) ) {
314 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%13p %s %s",
315 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
316 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
320 if ( ISLESSPOS(*plspace,pp) ) {
321 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%14p %s %s",
322 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
323 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
327 if ( ISLESSPOS(*plspace,pp) ) {
328 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%15p %s %s",
329 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
330 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
334 if ( ISLESSPOS(*plspace,pp) ) {
335 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%16p %s %s",
336 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
337 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
341 if ( ISLESSPOS(*plspace,pp) ) {
342 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%17p %s %s",
343 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
344 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
350 else if ( par == 1 ) {
351 SETBASEPOSITION(pp,y);
352 if ( ISLESSPOS(*plspace,pp) ) {
353 MesPrint(
"%7l.%2is %10l:%10p",millitime,timepart,
354 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
358 SETBASEPOSITION(pp,y);
360 if ( ISLESSPOS(*plspace,pp) ) {
361 MesPrint(
"%7l.%2is %10l:%11p",millitime,timepart,
362 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
366 if ( ISLESSPOS(*plspace,pp) ) {
367 MesPrint(
"%7l.%2is %10l:%12p",millitime,timepart,
368 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
372 if ( ISLESSPOS(*plspace,pp) ) {
373 MesPrint(
"%7l.%2is %10l:%13p",millitime,timepart,
374 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
378 if ( ISLESSPOS(*plspace,pp) ) {
379 MesPrint(
"%7l.%2is %10l:%14p",millitime,timepart,
380 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
384 if ( ISLESSPOS(*plspace,pp) ) {
385 MesPrint(
"%7l.%2is %10l:%15p",millitime,timepart,
386 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
390 if ( ISLESSPOS(*plspace,pp) ) {
391 MesPrint(
"%7l.%2is %10l:%16p",millitime,timepart,
392 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
396 if ( ISLESSPOS(*plspace,pp) ) {
397 MesPrint(
"%7l.%2is %10l:%17p",millitime,timepart,
398 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
408 MesPrint(
"WTime = %7l.%2i sec",millitime,timepart);
411 MesPrint(
"Time = %7l.%2i sec",millitime,timepart);
415 #if ( BITSINLONG > 32 ) 416 if ( S->GenTerms >= 10000000000L ) {
418 MesPrint(
"WTime = %7l.%2i sec Generated terms = %16l",
419 millitime,timepart,S->GenTerms);
422 MesPrint(
"Time = %7l.%2i sec Generated terms = %16l",
423 millitime,timepart,S->GenTerms);
428 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
429 millitime,timepart,S->GenTerms);
432 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
433 millitime,timepart,S->GenTerms);
438 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
439 millitime,timepart,S->GenTerms);
442 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
443 millitime,timepart,S->GenTerms);
447 #if ( BITSINLONG > 32 ) 449 if ( S->TermsLeft >= 10000000000L ) {
450 MesPrint(
"%16s%8l Terms %s = %16l",EXPRNAME(AR.CurExpr),
451 AN.ninterms,FG.swmes[par],S->TermsLeft);
454 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
455 AN.ninterms,FG.swmes[par],S->TermsLeft);
458 if ( S->TermsLeft >= 10000000000L ) {
460 if ( identity > 0 && par == 2 ) {
461 MesPrint(
"%16s Terms in thread = %16l",
462 EXPRNAME(AR.CurExpr),S->TermsLeft);
465 #elif defined(WITHMPI) 466 if ( PF.me != MASTER && par == 2 ) {
467 MesPrint(
"%16s Terms in process= %16l",
468 EXPRNAME(AR.CurExpr),S->TermsLeft);
473 MesPrint(
"%16s Terms %s = %16l",
474 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
479 if ( identity > 0 && par == 2 ) {
480 MesPrint(
"%16s Terms in thread = %10l",
481 EXPRNAME(AR.CurExpr),S->TermsLeft);
484 #elif defined(WITHMPI) 485 if ( PF.me != MASTER && par == 2 ) {
486 MesPrint(
"%16s Terms in process= %10l",
487 EXPRNAME(AR.CurExpr),S->TermsLeft);
492 MesPrint(
"%16s Terms %s = %10l",
493 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
499 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
500 AN.ninterms,FG.swmes[par],S->TermsLeft);
503 if ( identity > 0 && par == 2 ) {
504 MesPrint(
"%16s Terms in thread = %10l",
505 EXPRNAME(AR.CurExpr),S->TermsLeft);
508 #elif defined(WITHMPI) 509 if ( PF.me != MASTER && par == 2 ) {
510 MesPrint(
"%16s Terms in process= %10l",
511 EXPRNAME(AR.CurExpr),S->TermsLeft);
516 MesPrint(
"%16s Terms %s = %10l",
517 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
521 SETBASEPOSITION(pp,y);
522 if ( ISLESSPOS(*plspace,pp) ) {
523 MesPrint(
"%24s Bytes used = %10p",AC.Commercial,plspace);
527 SETBASEPOSITION(pp,y);
529 if ( ISLESSPOS(*plspace,pp) ) {
530 MesPrint(
"%24s Bytes used =%11p",AC.Commercial,plspace);
534 if ( ISLESSPOS(*plspace,pp) ) {
535 MesPrint(
"%24s Bytes used =%12p",AC.Commercial,plspace);
539 if ( ISLESSPOS(*plspace,pp) ) {
540 MesPrint(
"%24s Bytes used =%13p",AC.Commercial,plspace);
544 if ( ISLESSPOS(*plspace,pp) ) {
545 MesPrint(
"%24s Bytes used =%14p",AC.Commercial,plspace);
549 if ( ISLESSPOS(*plspace,pp) ) {
550 MesPrint(
"%24s Bytes used =%15p",AC.Commercial,plspace);
554 if ( ISLESSPOS(*plspace,pp) ) {
555 MesPrint(
"%24s Bytes used =%16p",AC.Commercial,plspace);
559 if ( ISLESSPOS(*plspace,pp) ) {
560 MesPrint(
"%24s Bytes used=%17p",AC.Commercial,plspace);
566 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l" 567 ,numwrites,numreads,numseeks);
568 MesPrint(
"Total number of mallocs: %l, frees: %l" 569 ,nummallocs,numfrees);
571 MUNLOCK(ErrorMessageLock);
594 if ( AN.SoScratC == 0 )
595 AN.SoScratC = (UWORD *)Malloc1(2*(AM.MaxTal+2)*
sizeof(UWORD),
"NewSort");
597 if ( AR.sLevel >= AN.NumFunSorts ) {
598 if ( AN.NumFunSorts == 0 ) newsize = 100;
599 else newsize = 2*AN.NumFunSorts;
600 newFS = (
SORTING **)Malloc1((newsize+1)*
sizeof(
SORTING *),
"FunSort pointers");
601 for ( i = 0; i < AN.NumFunSorts; i++ ) newFS[i] = AN.FunSorts[i];
602 for ( ; i <= newsize; i++ ) newFS[i] = 0;
603 if ( AN.FunSorts ) M_free(AN.FunSorts,
"FunSort pointers");
604 AN.FunSorts = newFS; AN.NumFunSorts = newsize;
606 if ( AR.sLevel == 0 ) {
607 AN.FunSorts[0] = AT.S0;
608 if ( AR.PolyFun == 0 ) { AT.S0->PolyFlag = 0; }
609 else if ( AR.PolyFunType == 1 ) { AT.S0->PolyFlag = 1; }
610 else if ( AR.PolyFunType == 2 ) {
611 if ( AR.PolyFunExp == 2
612 || AR.PolyFunExp == 3 ) AT.S0->PolyFlag = 1;
613 else AT.S0->PolyFlag = 2;
615 AR.ShortSortCount = 0;
618 if ( AN.FunSorts[AR.sLevel] == 0 ) {
619 AN.FunSorts[AR.sLevel] = AllocSort(
620 AM.SLargeSize,AM.SSmallSize,AM.SSmallEsize,AM.STermsInSmall
621 ,AM.SMaxPatches,AM.SMaxFpatches,AM.SIOsize);
623 AN.FunSorts[AR.sLevel]->PolyFlag = 0;
625 AT.SS = S = AN.FunSorts[AR.sLevel];
626 S->sFill = S->sBuffer;
627 S->lFill = S->lBuffer;
630 S->GenTerms = S->TermsLeft = S->GenSpace = S->SpaceLeft = 0;
631 S->PoinFill = S->sPointer;
632 *S->PoinFill = S->sFill;
633 PUTZERO(S->SizeInFile[0]); PUTZERO(S->SizeInFile[1]); PUTZERO(S->SizeInFile[2]);
635 PUTZERO(S->file.POposition);
637 if ( AR.sLevel > AN.MaxFunSorts ) AN.MaxFunSorts = AR.sLevel;
679 WORD j, **ss, *to, *t;
680 LONG sSpace, over, tover, spare, retval = 0, jj;
683 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
685 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
688 oldoutfile = AR.outfile;
698 oldoutfile = AR.outfile;
720 tover = over = S->sTerms;
723 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
726 S->TermsLeft -= over - spare;
727 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
729 else if ( S != AT.S0 ) {
733 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
734 if ( AN.tryterm > 0 && ( (sSpace+1)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
735 to = TermMalloc(
"$-sort space");
738 LONG allocsp = sSpace+1;
739 if ( allocsp < 20 ) allocsp = 20;
740 allocsp = ((allocsp+7)/8)*8;
741 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
742 if ( AN.tryterm > 0 ) AN.tryterm = 0;
744 *((WORD **)buffer) = to;
746 while ( ( t = *ss++ ) != 0 ) {
747 j = *t;
while ( --j >= 0 ) *to++ = *t++;
755 while ( ( t = *ss++ ) != 0 ) {
757 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
758 MLOCK(ErrorMessageLock);
759 MesPrint(
"Sorted function argument too long.");
760 MUNLOCK(ErrorMessageLock);
761 retval = -1;
goto RetRetval;
763 while ( --j >= 0 ) *to++ = *t++;
773 *AR.CompressPointer = 0;
774 SeekScratch(AR.outfile,&position);
786 {
int oldgzipCompress = AR.gzipCompress;
792 while ( ( t = *ss++ ) != 0 ) {
793 if ( *t ) S->TermsLeft++;
795 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
798 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
799 retval = -1;
goto RetRetval;
804 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
808 retval = -1;
goto RetRetval;
811 AR.gzipCompress = oldgzipCompress;
815 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
818 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
820 DIFPOS(oldpos,position,oldpos);
821 S->SpaceLeft = BASEPOSITION(oldpos);
827 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
829 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
832 SETBASEPOSITION(pp,lSpace);
833 MULPOS(pp,
sizeof(WORD));
834 if ( S->file.handle >= 0 ) {
835 ADD2POS(pp,S->fPatches[S->fPatchN]);
838 WORD oldLogHandle = AC.LogHandle;
839 if ( AC.LogHandle >= 0 && AM.LogType && ( ( S->lPatch > 0 )
840 || S->file.handle >= 0 ) ) AC.LogHandle = -1;
841 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
WriteStats(&pp,0); }
842 AC.LogHandle = oldLogHandle;
844 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
845 if ( S->lPatch > 0 ) {
846 if ( ( S->lPatch >= S->MaxPatches ) ||
847 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
852 MLOCK(ErrorMessageLock);
853 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x" 854 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
855 MUNLOCK(ErrorMessageLock);
859 MLOCK(ErrorMessageLock);
861 MUNLOCK(ErrorMessageLock);
862 retval = -1;
goto RetRetval;
865 pp = S->SizeInFile[1];
866 MULPOS(pp,
sizeof(WORD));
871 WORD oldLogHandle = AC.LogHandle;
873 SETBASEPOSITION(pppp,0);
874 SeekFile(S->file.handle,&pppp,SEEK_CUR);
875 SeekFile(S->file.handle,&pp,SEEK_END);
876 SeekFile(S->file.handle,&pppp,SEEK_SET);
877 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
879 AC.LogHandle = oldLogHandle;
884 S->Patches[S->lPatch++] = S->lFill;
885 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
888 while ( ( t = *ss++ ) != 0 ) {
890 if ( j < 0 ) j = t[1] + 2;
891 while ( --j >= 0 ) *to++ = *t++;
896 if ( S->file.handle < 0 ) {
898 MLOCK(ErrorMessageLock);
900 MUNLOCK(ErrorMessageLock);
901 retval = -1;
goto RetRetval;
904 pp = S->SizeInFile[2];
905 MULPOS(pp,
sizeof(WORD));
907 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
913 if ( par == 2 && newout->handle >= 0 ) {
917 LOCK(newout->pthreadslock);
919 SeekFile(newout->handle,&zeropos,SEEK_SET);
920 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+
sizeof(WORD)*2
921 ,
"$-buffer reading");
922 if ( AN.tryterm > 0 ) AN.tryterm = 0;
923 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
924 BASEPOSITION(newout->filesize) ) {
925 MLOCK(ErrorMessageLock);
926 MesPrint(
"Error reading information for $ variable");
927 MUNLOCK(ErrorMessageLock);
928 M_free(to,
"$-buffer reading");
932 *((WORD **)buffer) = to;
933 retval /=
sizeof(WORD);
936 UNLOCK(newout->pthreadslock);
939 else if ( newout->handle >= 0 ) {
941 MLOCK(ErrorMessageLock);
942 MesPrint(
"(1)Output should fit inside a single term. Increase MaxTermSize?");
944 MUNLOCK(ErrorMessageLock);
945 retval = -1;
goto RetRetval;
948 t = newout->PObuffer;
950 jj = newout->POfill - t;
951 if ( AN.tryterm > 0 && ( (jj+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
952 to = TermMalloc(
"$-sort space");
956 if ( allocsp < 20 ) allocsp = 20;
957 allocsp = ((allocsp+7)/8)*8;
958 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
959 if ( AN.tryterm > 0 ) AN.tryterm = 0;
961 *((WORD **)buffer) = to;
965 j = newout->POfill - t;
967 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
969 if ( j > AM.MaxTer )
goto TooLarge;
977 MLOCK(ErrorMessageLock);
979 MUNLOCK(ErrorMessageLock);
980 retval = -1;
goto RetRetval;
983 pp = S->SizeInFile[1];
984 MULPOS(pp,
sizeof(WORD));
989 WORD oldLogHandle = AC.LogHandle;
991 SETBASEPOSITION(pppp,0);
992 SeekFile(S->file.handle,&pppp,SEEK_CUR);
993 SeekFile(S->file.handle,&pp,SEEK_END);
994 SeekFile(S->file.handle,&pppp,SEEK_SET);
995 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
997 AC.LogHandle = oldLogHandle;
1005 if ( newout->handle >= 0 )
goto TooLarge;
1006 t = newout->PObuffer;
1007 j = newout->POfill - t;
1009 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
1010 goto WorkSpaceError;
1011 if ( j > AM.MaxTer )
goto TooLarge;
1018 if ( S->file.handle >= 0 ) {
1020 MLOCK(ErrorMessageLock);
1021 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p" 1022 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1023 MUNLOCK(ErrorMessageLock);
1025 if ( S->lPatch <= 0 ) {
1027 position = S->fPatches[S->fPatchN];
1031 *AR.CompressPointer = 0;
1032 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
1033 S->fpcompressed[S->fPatchN] = 1;
1035 S->fpcompressed[S->fPatchN] = 0;
1036 SetupOutputGZIP(&(S->file));
1038 while ( ( t = *ss++ ) != 0 ) {
1039 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
1040 retval = -1;
goto RetRetval;
1043 if (
FlushOut(&position,&(S->file),1) ) {
1044 retval = -1;
goto RetRetval;
1047 S->fPatches[S->fPatchN] = position;
1050 MLOCK(ErrorMessageLock);
1051 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p" 1052 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1053 MUNLOCK(ErrorMessageLock);
1059 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1060 if ( S->file.handle >= 0 ) {
1061 SynchFile(S->file.handle);
1067 MLOCK(ErrorMessageLock);
1069 MUNLOCK(ErrorMessageLock);
1070 retval = -1;
goto RetRetval;
1074 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
1076 pp = S->SizeInFile[0];
1077 MULPOS(pp,
sizeof(WORD));
1085 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
1086 Expressions[AR.CurExpr].counter = S->TermsLeft;
1087 Expressions[AR.CurExpr].size = pp;
1090 if ( AR.sLevel == 0 ) {
1091 Expressions[AR.CurExpr].counter = S->TermsLeft;
1092 Expressions[AR.CurExpr].size = pp;
1096 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
1099 CloseFile(S->file.handle);
1100 S->file.handle = -1;
1101 remove(S->file.name);
1103 MLOCK(ErrorMessageLock);
1104 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
1105 MUNLOCK(ErrorMessageLock);
1108 AR.outfile = oldoutfile;
1110 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
1115 DeAllocFileHandle(newout);
1119 else if ( newout ) {
1120 if ( newout->handle >= 0 ) {
1121 MLOCK(ErrorMessageLock);
1122 MesPrint(
"(2)Output should fit inside a single term. Increase MaxTermSize?");
1124 MUNLOCK(ErrorMessageLock);
1127 else if ( newout->POfill > newout->PObuffer ) {
1133 j = newout->POfill-newout->PObuffer;
1134 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
1135 goto WorkSpaceError;
1137 to = buffer; t = newout->PObuffer;
1138 while ( j-- > 0 ) *to++ = *t++;
1142 DeAllocFileHandle(newout);
1146 else if ( par == 2 ) {
1148 if ( retval == 0 ) {
1149 if ( newout->handle >= 0 ) {
1157 LOCK(newout->pthreadslock);
1159 SeekFile(newout->handle,&position,SEEK_END);
1161 SeekFile(newout->handle,&zeropos,SEEK_SET);
1162 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)*3
1163 ,
"$-buffer reading");
1164 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1165 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
1166 BASEPOSITION(position) ) {
1167 MLOCK(ErrorMessageLock);
1168 MesPrint(
"Error reading information for $ variable");
1169 MUNLOCK(ErrorMessageLock);
1170 M_free(to,
"$-buffer reading");
1174 *((WORD **)buffer) = to;
1175 retval /=
sizeof(WORD);
1178 UNLOCK(newout->pthreadslock);
1185 LONG wsiz = newout->POfill - newout->PObuffer;
1186 if ( AN.tryterm > 0 && ( (wsiz+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
1187 to = TermMalloc(
"$-sort space");
1190 LONG allocsp = wsiz+2;
1191 if ( allocsp < 20 ) allocsp = 20;
1192 allocsp = ((allocsp+7)/8)*8;
1193 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-buffer reading");
1194 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1196 *((WORD **)buffer) = to; t = newout->PObuffer;
1202 DeAllocFileHandle(newout);
1208 DeAllocFileHandle(newout);
1214 MLOCK(ErrorMessageLock);
1217 MUNLOCK(ErrorMessageLock);
1248 from = buffer + ( file->POsize *
sizeof(UBYTE) )/
sizeof(WORD);
1250 if ( i*((LONG)(
sizeof(WORD))) > AM.MaxTer ) {
1251 MLOCK(ErrorMessageLock);
1252 MesPrint(
"Problems in PutIn");
1253 MUNLOCK(ErrorMessageLock);
1257 while ( --i >= 0 ) *--to = *--from;
1260 if ( ( RetCode = FillInputGZIP(file,position,(UBYTE *)buffer
1261 ,file->POsize,npat) ) < 0 ) {
1262 MLOCK(ErrorMessageLock);
1263 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1264 RetCode,file->POsize);
1265 MUNLOCK(ErrorMessageLock);
1270 LOCK(file->pthreadslock);
1272 SeekFile(file->
handle,position,SEEK_SET);
1273 if ( ( RetCode = ReadFile(file->
handle,(UBYTE *)buffer,file->POsize) ) < 0 ) {
1275 UNLOCK(file->pthreadslock);
1277 MLOCK(ErrorMessageLock);
1278 MesPrint(
"PutIn: We have RetCode = %x while reading %x bytes",
1279 RetCode,file->POsize);
1280 MUNLOCK(ErrorMessageLock);
1284 UNLOCK(file->pthreadslock);
1306 int dobracketindex = 0;
1307 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1308 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1311 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1313 MLOCK(ErrorMessageLock);
1314 MesPrint(
"%w Sflush created scratch file %s",fi->name);
1315 MUNLOCK(ErrorMessageLock);
1317 fi->
handle = (WORD)RetCode;
1318 PUTZERO(fi->filesize);
1319 PUTZERO(fi->POposition);
1322 MLOCK(ErrorMessageLock);
1323 MesPrint(
"Cannot create scratch file %s",fi->name);
1324 MUNLOCK(ErrorMessageLock);
1329 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1330 && dobracketindex == 0 ) {
1331 if ( FlushOutputGZIP(fi) )
return(-1);
1332 fi->POfill = fi->PObuffer;
1338 LOCK(fi->pthreadslock);
1340 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1341 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1342 if ( WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) != size ) {
1344 UNLOCK(fi->pthreadslock);
1346 MLOCK(ErrorMessageLock);
1347 MesPrint(
"Write error while finishing sort. Disk full?");
1348 MUNLOCK(ErrorMessageLock);
1351 ADDPOS(fi->filesize,size);
1352 ADDPOS(fi->POposition,size);
1353 fi->POfill = fi->PObuffer;
1355 UNLOCK(fi->pthreadslock);
1390 WORD i, *p, ret, *r, *rr, j, k, first;
1391 int dobracketindex = 0;
1394 if ( AT.SS != AT.S0 ) {
1398 if ( ( i = *term ) <= 0 )
return(0);
1400 ADDPOS(*position,i*
sizeof(WORD));
1403 if ( p >= fi->POstop ) {
1405 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1407 MLOCK(ErrorMessageLock);
1408 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1409 MUNLOCK(ErrorMessageLock);
1411 fi->
handle = (WORD)RetCode;
1412 PUTZERO(fi->filesize);
1413 PUTZERO(fi->POposition);
1419 MLOCK(ErrorMessageLock);
1420 MesPrint(
"Cannot create scratch file %s",fi->name);
1421 MUNLOCK(ErrorMessageLock);
1426 LOCK(fi->pthreadslock);
1428 if ( fi == AR.hidefile ) {
1429 LOCK(AS.inputslock);
1431 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1432 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1433 if ( fi == AR.hidefile ) {
1434 UNLOCK(AS.inputslock);
1437 UNLOCK(fi->pthreadslock);
1439 MLOCK(ErrorMessageLock);
1440 MesPrint(
"Write error during sort. Disk full?");
1441 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1442 fi->POsize,fi->
handle,&(fi->POposition));
1443 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1444 MUNLOCK(ErrorMessageLock);
1447 ADDPOS(fi->filesize,fi->POsize);
1449 ADDPOS(fi->POposition,fi->POsize);
1450 if ( fi == AR.hidefile ) {
1451 UNLOCK(AS.inputslock);
1454 UNLOCK(fi->pthreadslock);
1457 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1463 }
while ( --i > 0 );
1464 fi->POfull = fi->POfill = p;
1467 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1468 MLOCK(ErrorMessageLock);
1470 sprintf((
char *)(THRbuf),
"PutOut(%d)",AT.identity);
1471 PrintTerm(term,(
char *)(THRbuf));
1473 PrintTerm(term,
"PutOut");
1475 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1476 MesPrint(
"File %s, position %p",fi->name,position);
1477 MUNLOCK(ErrorMessageLock);
1480 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1481 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1482 r = rr = AR.CompressPointer;
1483 first = j = k = ret = 0;
1484 if ( ( i = *term ) != 0 ) {
1487 if ( fi == AR.outfile || fi == AR.hidefile ) {
1488 MLOCK(ErrorMessageLock);
1489 MesPrint(
"Ran into precompressed term");
1490 MUNLOCK(ErrorMessageLock);
1495 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1496 if ( dobracketindex ) {
1497 PutBracketInIndex(BHEAD term,position);
1503 WORD *polystop, *sa;
1507 while ( polystop < sa && *polystop != AR.PolyFun ) {
1508 polystop += polystop[1];
1510 if ( polystop < sa ) {
1511 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1512 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1513 i--; j--; k--; p++; r++;
1517 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1524 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1544 r[-(ABS(r[-1]))] = 0;
1545 if ( r >= AR.ComprTop ) {
1546 MLOCK(ErrorMessageLock);
1547 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1548 MUNLOCK(ErrorMessageLock);
1553 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1555 if ( dobracketindex ) {
1556 PutBracketInIndex(BHEAD term,position);
1562 WORD *polystop, *sa;
1566 while ( polystop < sa && *polystop != AR.PolyFun ) {
1567 polystop += polystop[1];
1569 if ( polystop < sa ) {
1570 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1571 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1572 i--; j--; k--; p++; r++;
1576 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1580 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1585 if ( AR.PolyFunType == 2 ) {
1587 tstop = term + *term;
1588 tstop -= ABS(tstop[-1]);
1590 while ( t < tstop ) {
1591 if ( *t == AR.PolyFun ) {
1592 t[2] &= ~MUSTCLEANPRF;
1597 if ( dobracketindex ) {
1598 PutBracketInIndex(BHEAD term,position);
1602 ADDPOS(*position,i*
sizeof(WORD));
1605 if ( p >= fi->POstop ) {
1607 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1609 sbuf->fill[sbuf->active] = fi->POstop;
1611 p = fi->PObuffer = fi->POfill = fi->POfull =
1612 sbuf->buff[sbuf->active];
1613 fi->POstop = sbuf->stop[sbuf->active];
1619 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1621 MLOCK(ErrorMessageLock);
1622 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1623 MUNLOCK(ErrorMessageLock);
1625 fi->
handle = (WORD)RetCode;
1626 PUTZERO(fi->filesize);
1627 PUTZERO(fi->POposition);
1633 MLOCK(ErrorMessageLock);
1634 MesPrint(
"Cannot create scratch file %s",fi->name);
1635 MUNLOCK(ErrorMessageLock);
1640 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1641 && dobracketindex == 0 && fi->zsp != 0 ) {
1643 if ( PutOutputGZIP(fi) )
return(-1);
1650 LOCK(fi->pthreadslock);
1652 if ( fi == AR.hidefile ) {
1653 LOCK(AS.inputslock);
1655 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1656 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1657 if ( fi == AR.hidefile ) {
1658 UNLOCK(AS.inputslock);
1661 UNLOCK(fi->pthreadslock);
1663 MLOCK(ErrorMessageLock);
1664 MesPrint(
"Write error during sort. Disk full?");
1665 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1666 fi->POsize,fi->
handle,&(fi->POposition));
1667 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1668 MUNLOCK(ErrorMessageLock);
1671 ADDPOS(fi->filesize,fi->POsize);
1673 ADDPOS(fi->POposition,fi->POsize);
1674 if ( fi == AR.hidefile ) {
1675 UNLOCK(AS.inputslock);
1678 UNLOCK(fi->pthreadslock);
1681 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1689 if ( first == 2 ) *p++ = k;
1693 else *p++ = *term++;
1699 }
while ( --i > 0 );
1700 fi->POfull = fi->POfill = p;
1728 int dobracketindex = 0;
1732 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1733 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1735 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1737 if ( fi->POfill >= fi->POstop ){
1738 sbuf->fill[sbuf->active] = fi->POstop;
1740 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1741 fi->POstop = sbuf->stop[sbuf->active];
1743 *(fi->POfill)++ = 0;
1744 sbuf->fill[sbuf->active] = fi->POfill;
1746 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1747 fi->POstop = sbuf->stop[sbuf->active];
1751 if ( fi->POfill >= fi->POstop ) {
1753 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1755 MLOCK(ErrorMessageLock);
1756 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1757 MUNLOCK(ErrorMessageLock);
1759 PUTZERO(fi->filesize);
1760 PUTZERO(fi->POposition);
1761 fi->
handle = (WORD)RetCode;
1767 MLOCK(ErrorMessageLock);
1768 MesPrint(
"Cannot create scratch file %s",fi->name);
1769 MUNLOCK(ErrorMessageLock);
1774 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1775 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1776 if ( PutOutputGZIP(fi) )
return(-1);
1777 fi->POfill = fi->PObuffer;
1783 LOCK(fi->pthreadslock);
1785 if ( fi == AR.hidefile ) {
1786 LOCK(AS.inputslock);
1788 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1789 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1791 UNLOCK(fi->pthreadslock);
1793 if ( fi == AR.hidefile ) {
1794 UNLOCK(AS.inputslock);
1796 MLOCK(ErrorMessageLock);
1797 MesPrint(
"Write error while sorting. Disk full?");
1798 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1799 fi->POsize,fi->
handle,&(fi->POposition));
1800 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1801 MUNLOCK(ErrorMessageLock);
1804 ADDPOS(fi->filesize,fi->POsize);
1805 fi->POfill = fi->PObuffer;
1806 ADDPOS(fi->POposition,fi->POsize);
1807 if ( fi == AR.hidefile ) {
1808 UNLOCK(AS.inputslock);
1811 UNLOCK(fi->pthreadslock);
1814 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1820 *(fi->POfill)++ = 0;
1821 fi->POfull = fi->POfill;
1834 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1837 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1838 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1839 if ( FlushOutputGZIP(fi) )
return(-1);
1840 fi->POfill = fi->PObuffer;
1846 LOCK(fi->pthreadslock);
1848 if ( fi == AR.hidefile ) {
1849 LOCK(AS.inputslock);
1851 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1855 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1857 UNLOCK(fi->pthreadslock);
1859 if ( fi == AR.hidefile ) {
1860 UNLOCK(AS.inputslock);
1862 MLOCK(ErrorMessageLock);
1863 MesPrint(
"Write error while finishing sorting. Disk full?");
1864 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1865 size,fi->
handle,&(fi->POposition));
1866 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1867 MUNLOCK(ErrorMessageLock);
1870 ADDPOS(fi->filesize,size);
1871 ADDPOS(fi->POposition,size);
1872 fi->POfill = fi->PObuffer;
1873 if ( fi == AR.hidefile ) {
1874 UNLOCK(AS.inputslock);
1877 UNLOCK(fi->pthreadslock);
1880 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1886 if ( dobracketindex ) {
1887 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1888 if ( b->indexfill > 0 ) {
1889 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1893 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1894 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1898 LOCK(fi->pthreadslock);
1900 SeekFile(fi->
handle,position,SEEK_END);
1902 UNLOCK(fi->pthreadslock);
1906 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1912 ADDPOS(*position,
sizeof(WORD));
1943 OutCoef = AN.SoScratC;
1944 s1 = *ps1; s2 = *ps2;
1947 if ( AddRat(BHEAD (UWORD *)s1,l1,(UWORD *)s2,l2,OutCoef,&OutLen) ) {
1948 MLOCK(ErrorMessageLock);
1950 MUNLOCK(ErrorMessageLock);
1953 if ( AN.ncmod != 0 ) {
1954 if ( ( AC.modmode & POSNEG ) != 0 ) {
1961 j = ABS(OutLen); OutCoef[j] = 1;
1962 for ( i = 1; i < j; i++ ) OutCoef[j+i] = 0;
1964 else if ( BigLong(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
1965 SubPLon(OutCoef,OutLen,(UWORD *)AC.cmod,ABS(AN.ncmod),OutCoef,&OutLen);
1966 OutCoef[OutLen] = 1;
1967 for ( i = 1; i < OutLen; i++ ) OutCoef[OutLen+i] = 0;
1970 if ( !OutLen ) { *ps1 = *ps2 = 0;
return(0); }
1972 if ( OutLen < 0 ) i = - ( --OutLen );
1974 if ( l1 < 0 ) l1 = -l1;
1979 s2 = (WORD *)OutCoef;
1980 while ( --i > 0 ) *s1++ = *s2++;
1982 while ( --l1 >= 0 ) *s1++ = 0;
1985 if ( l2 < 0 ) l2 = -l2;
1990 s1 = (WORD *)OutCoef;
1991 while ( --i > 0 ) *s2++ = *s1++;
1993 while ( --l2 >= 0 ) *s2++ = 0;
2003 if ( (S->sFill + j) >= S->sTop2 ) {
2010 if ( l1 < 0 ) l1 = - l1;
2015 while ( s1 < t ) *s2++ = *s1++;
2016 s1 = (WORD *)OutCoef;
2017 while ( --i > 0 ) *s2++ = *s1++;
2023 if ( **ps1 > AM.MaxTer/((LONG)(
sizeof(WORD))) ) {
2024 MLOCK(ErrorMessageLock);
2025 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2026 AM.MaxTer/
sizeof(WORD));
2027 MUNLOCK(ErrorMessageLock);
2067 WORD *s1, *s2, *m, *w, *t, oldpw = S->PolyWise;
2068 s1 = *ps1 + S->PolyWise;
2069 s2 = *ps2 + S->PolyWise;
2074 if ( S->PolyFlag == 2 && AR.PolyFunExp != 2 && AR.PolyFunExp != 3 ) {
2075 WORD **oldSplitScratch = AN.SplitScratch;
2076 LONG oldSplitScratchSize = AN.SplitScratchSize;
2077 LONG oldInScratch = AN.InScratch;
2078 WORD oldtype = AR.SortType;
2079 if ( (WORD *)((UBYTE *)w + AM.MaxTer) >= AT.WorkTop ) {
2080 MLOCK(ErrorMessageLock);
2081 MesPrint(
"Program was adding polyratfun arguments");
2083 MUNLOCK(ErrorMessageLock);
2085 AR.SortType = SORTHIGHFIRST;
2087 AN.SplitScratch = AN.SplitScratch1;
2088 AN.SplitScratchSize = AN.SplitScratchSize1;
2089 AN.InScratch = AN.InScratch1;
2090 poly_ratfun_add(BHEAD s1,s2);
2091 S->PolyWise = oldpw;
2092 AN.SplitScratch1 = AN.SplitScratch;
2093 AN.SplitScratchSize1 = AN.SplitScratchSize;
2094 AN.InScratch1 = AN.InScratch;
2095 AN.SplitScratch = oldSplitScratch;
2096 AN.SplitScratchSize = oldSplitScratchSize;
2097 AN.InScratch = oldInScratch;
2099 AR.SortType = oldtype;
2100 if ( w[1] <= FUNHEAD ||
2101 ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
2102 *ps1 = *ps2 = 0;
return(0);
2106 if ( w + s1[1] + s2[1] + 12 + ARGHEAD >= AT.WorkTop ) {
2107 MLOCK(ErrorMessageLock);
2108 MesPrint(
"Program was adding polyfun arguments");
2110 MUNLOCK(ErrorMessageLock);
2117 if ( w[1] <= FUNHEAD ) { *ps1 = *ps2 = 0;
return(0); }
2118 if ( w[1] <= s1[1] || w[1] <= s2[1] ) {
2119 if ( w[1] > s1[1] ) {
2128 while ( t < m ) *s1++ = *t++;
2129 **ps1 = WORDDIF(s1,(*ps1));
2138 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2140 MesPrint(
"------Garbage collection-------");
2142 AT.WorkPointer += w[1];
2146 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2149 MLOCK(ErrorMessageLock);
2150 AO.OutFill = AO.OutputLine = OutBuf;
2154 while ( --i >= 0 ) {
2155 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2158 AO.OutFill = AO.OutputLine = OutBuf;
2163 while ( --i >= 0 ) {
2164 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2167 AO.OutFill = AO.OutputLine = OutBuf;
2172 while ( --i >= 0 ) {
2173 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2176 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2177 MUNLOCK(ErrorMessageLock);
2179 MLOCK(ErrorMessageLock);
2180 MesPrint(
"Please increase SmallExtension in %s",setupfilename);
2181 MUNLOCK(ErrorMessageLock);
2195 while ( t < w ) *s2++ = *t++;
2199 if ( *m > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
2200 MLOCK(ErrorMessageLock);
2201 MesPrint(
"Term to complex after polynomial addition. MaxTermSize = %10l",
2202 AM.MaxTer/
sizeof(WORD));
2203 MUNLOCK(ErrorMessageLock);
2215 #define INSLENGTH(x) w[1] = FUNHEAD+ARGHEAD+x; w[FUNHEAD] = ARGHEAD+x; 2228 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2229 WORD tempterm[8+FUNHEAD];
2231 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2232 *m++ = 0; *m++ = 0; FILLARG(m)
2233 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2234 if ( s1[FUNHEAD] < 0 ) {
2235 if ( s2[FUNHEAD] < 0 ) {
2236 if ( s1[FUNHEAD] <= -FUNCTION ) {
2237 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2238 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2240 *m++ = 2; *m++ = 1; *m++ = 3;
2241 INSLENGTH(4+FUNHEAD)
2243 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2244 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2245 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2246 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2248 s2[FUNHEAD] = s1[FUNHEAD];
2251 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2253 *m++ = 1; *m++ = 1; *m++ = 3;
2254 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2256 *m++ = 1; *m++ = 1; *m++ = 3;
2257 INSLENGTH(8+2*FUNHEAD)
2259 else if ( s2[FUNHEAD] == -SYMBOL ) {
2260 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2261 *m++ = 1; *m++ = 1; *m++ = 3;
2262 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2264 *m++ = 1; *m++ = 1; *m++ = 3;
2265 INSLENGTH(12+FUNHEAD)
2269 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2270 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2272 *m++ = 1; *m++ = 1; *m++ = 3;
2273 INSLENGTH(8+FUNHEAD)
2276 else if ( s1[FUNHEAD] == -SYMBOL ) {
2277 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2278 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2279 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2280 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2284 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2285 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2286 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2287 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2288 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2289 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2290 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2294 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2295 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2296 *m++ = 1; *m++ = 1; *m++ = 3;
2297 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2299 *m++ = 1; *m++ = 1; *m++ = 3;
2300 INSLENGTH(12+FUNHEAD)
2304 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2305 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2306 *m++ = 1; *m++ = 1; *m++ = 3;
2311 if ( s2[FUNHEAD] <= -FUNCTION ) {
2313 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2314 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2316 *m++ = 1; *m++ = 1; *m++ = 3;
2317 INSLENGTH(8+FUNHEAD)
2319 else if ( s2[FUNHEAD] == -SYMBOL ) {
2321 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2322 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2323 *m++ = 1; *m++ = 1; *m++ = 3;
2328 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2329 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2330 else { i1 = (WORD)x1; i2 = 3; }
2331 if ( x1 && AN.ncmod != 0 ) {
2336 if ( Modulus(m) ) Terminate(-1);
2337 if ( *m == 0 ) w[1] = 0;
2339 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2357 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2364 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2377 if ( s1[FUNHEAD] <= -FUNCTION ) {
2378 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2380 *t++ = 1; *t++ = 1; *t++ = 3;
2382 else if ( s1[FUNHEAD] == -SYMBOL ) {
2383 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2384 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2385 *t++ = 1; *t++ = 1; *t++ = 3;
2388 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2389 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2405 tstop1 = s1 + s1[1];
2406 s1 += FUNHEAD+ARGHEAD;
2408 tstop2 = s2 + s2[1];
2409 s2 += FUNHEAD+ARGHEAD;
2413 oldPolyFlag = AT.SS->PolyFlag;
2414 AT.SS->PolyFlag = 0;
2415 while ( s1 < tstop1 && s2 < tstop2 ) {
2416 i1 = CompareTerms(BHEAD s1,s2,(WORD)(-1));
2421 else if ( i1 < 0 ) {
2444 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2445 MLOCK(ErrorMessageLock);
2446 MesPrint(
"Addition of coefficients of PolyFun");
2447 MUNLOCK(ErrorMessageLock);
2457 *mm = WORDDIF(m,mm);
2458 if ( AN.ncmod != 0 ) {
2459 if ( Modulus(mm) ) Terminate(-1);
2466 while ( s1 < tstop1 ) *m++ = *s1++;
2467 while ( s2 < tstop2 ) *m++ = *s2++;
2468 w[1] = WORDDIF(m,w);
2469 w[FUNHEAD] = w[1] - FUNHEAD;
2470 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2471 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2472 else w[1] = FUNHEAD+2;
2473 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2476 AT.SS->PolyFlag = oldPolyFlag;
2513 WORD *stopper1, *stopper2, *t2;
2515 WORD *stopex1, *stopex2;
2518 WORD count = -1, localPoly, polyhit = -1;
2520 if ( S->PolyFlag ) {
2528 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2529 S->PolyFlag = AR.PolyFunType;
2530 if ( AR.PolyFunType == 2 &&
2531 ( AR.PolyFunExp == 2 || AR.PolyFunExp == 3 ) ) S->PolyFlag = 1;
2533 else { localPoly = 0; }
2537 GETSTOP(term2,stopper2);
2540 while ( t1 < stopper1 && t2 < stopper2 ) {
2542 if ( *t1 == HAAKJE )
return(PREV(-1));
2543 if ( *t2 == HAAKJE )
return(PREV(1));
2544 if ( *t1 >= (FUNCTION-1) ) {
2545 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2546 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2547 if ( *t1 < FUNCTION )
return(PREV(1));
2548 if ( *t2 < FUNCTION )
return(PREV(-1));
2549 c1 = functions[*t1-FUNCTION].commute;
2550 c2 = functions[*t2-FUNCTION].commute;
2552 if ( c2 )
return(PREV(1));
2553 else return(PREV(*t2-*t1));
2556 if ( !c2 )
return(PREV(-1));
2557 else return(PREV(*t2-*t1));
2560 else return(PREV(*t2-*t1));
2567 if ( localPoly && c1 < FUNCTION ) {
2570 if ( c1 <= (FUNCTION-1)
2571 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec ) ) {
2572 if ( c1 == SYMBOL ) {
2573 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2574 && s1[-1] == 4 && s2[-1] == 4
2575 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2576 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2583 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2586 else if ( AR.SortType >= SORTPOWERFIRST ) {
2589 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2591 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2594 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2601 if ( AR.SortType==SORTLOWFIRST ) {
2602 return(PREV((s1[1]>0?-1:1)));
2605 return(PREV((s1[1]<0?-1:1)));
2610 if ( AR.SortType==SORTLOWFIRST ) {
2612 return(PREV((s1[1]<0?1:-1)));
2615 return(PREV((s2[1]<0?-1:1)));
2620 return(PREV((s1[1]<0?-1:1)));
2623 return(PREV((s2[1]<0?1:-1)));
2628 if ( *s1 != *s2 )
return(
2629 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2634 if ( AR.SortType==SORTLOWFIRST ) {
2635 return(PREV((s2[1]<0?-1:1)));
2638 return(PREV((s2[1]<0?1:-1)));
2642 else if ( c1 == DOTPRODUCT ) {
2643 if ( AR.SortType >= SORTPOWERFIRST ) {
2646 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2648 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2651 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2656 if ( s2 >= t2 )
return(PREV(1));
2657 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2659 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2661 if ( *s1 != *s2 )
return(
2662 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2665 if ( s2 < t2 )
return(PREV(-1));
2669 if ( s2 >= t2 )
return(PREV(1));
2670 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2673 if ( s2 < t2 )
return(PREV(-1));
2681 if ( localPoly && c1 == AR.PolyFun ) {
2683 if ( S->PolyFlag == 1 ) {
2685 if ( *s1 > 0 ) i1 = *s1;
2686 else if ( *s1 <= -FUNCTION ) i1 = 1;
2688 if ( *s2 > 0 ) i2 = *s2;
2689 else if ( *s2 <= -FUNCTION ) i2 = 1;
2691 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2696 WORD *u1, *u2, *ustop;
2698 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2705 ustop = u2 - ABS(u2[-1]);
2707 while ( u1 < ustop ) {
2708 if ( *u1 == INDEX )
goto NoPoly;
2715 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2722 ustop = u2 - ABS(u2[-1]);
2724 while ( u1 < ustop ) {
2725 if ( *u1 == INDEX )
goto NoPoly;
2732 S->PolyWise = WORDDIF(s1,term1);
2733 S->PolyWise -= FUNHEAD;
2739 S->PolyWise = localPoly = 0;
2742 else if ( AR.PolyFunType == 2 ) {
2743 WORD i1, i2, i1a, i2a;
2744 if ( *s1 > 0 ) i1 = *s1;
2745 else if ( *s1 <= -FUNCTION ) i1 = 1;
2747 if ( *s2 > 0 ) i2 = *s2;
2748 else if ( *s2 <= -FUNCTION ) i2 = 1;
2750 if ( s1[i1] > 0 ) i1a = s1[i1];
2751 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2753 if ( s2[i2] > 0 ) i2a = s2[i2];
2754 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2756 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2761 WORD *u1, *u2, *ustop;
2763 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2768 while ( u1 < s1+i1 ) {
2770 ustop = u2 - ABS(u2[-1]);
2772 while ( u1 < ustop ) {
2773 if ( *u1 == INDEX )
goto NoPoly;
2780 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2784 u1 = s1 +i1 + ARGHEAD;
2787 ustop = u2 - ABS(u2[-1]);
2789 while ( u1 < ustop ) {
2790 if ( *u1 == INDEX )
goto NoPoly;
2797 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2802 while ( u1 < s2+i2 ) {
2804 ustop = u2 - ABS(u2[-1]);
2806 while ( u1 < ustop ) {
2807 if ( *u1 == INDEX )
goto NoPoly;
2814 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2818 u1 = s2 + i2 + ARGHEAD;
2821 ustop = u2 - ABS(u2[-1]);
2823 while ( u1 < ustop ) {
2824 if ( *u1 == INDEX )
goto NoPoly;
2831 S->PolyWise = WORDDIF(s1,term1);
2832 S->PolyWise -= FUNHEAD;
2837 S->PolyWise = localPoly = 0;
2841 S->PolyWise = localPoly = 0;
2845 t1 = term1 + S->PolyWise;
2846 t2 = term2 + S->PolyWise;
2856 if ( s2 >= t2 )
return(PREV(-1));
2863 if ( AC.properorderflag ) {
2865 oldpolyflag = S->PolyFlag;
2867 if ( ( c2 = -CompArg(s1,s2) ) != 0 ) {
2868 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2870 S->PolyFlag = oldpolyflag;
2879 if ( s2 >= t2 )
return(PREV(-1));
2881 s1 += ARGHEAD; s2 += ARGHEAD;
2882 oldpolyflag = S->PolyFlag;
2884 while ( s1 < stopex1 ) {
2885 if ( s2 >= stopex2 ) {
2886 S->PolyFlag = oldpolyflag;
return(PREV(-1));
2888 if ( ( c2 = CompareTerms(BHEAD s1,s2,(WORD)1) ) != 0 ) {
2889 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2894 S->PolyFlag = oldpolyflag;
2895 if ( s2 < stopex2 )
return(PREV(1));
2897 else return(PREV(1));
2900 if ( *s2 > 0 )
return(PREV(-1));
2901 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2902 if ( *s1 > -FUNCTION ) {
2903 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2909 if ( s2 < t2 )
return(PREV(1));
2913 if ( AR.SortType != SORTLOWFIRST ) {
2914 if ( t1 < stopper1 )
return(PREV(1));
2915 if ( t2 < stopper2 )
return(PREV(-1));
2918 if ( t1 < stopper1 )
return(PREV(-1));
2919 if ( t2 < stopper2 )
return(PREV(1));
2922 if ( level == 3 )
return(
CompCoef(term1,term2));
2948 WORD *t1, *t2, *tt1, *tt2;
2951 if ( AR.SortType == SORTLOWFIRST ) { low = 1; high = -1; }
2952 else { low = -1; high = 1; }
2953 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
2954 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
2955 if ( AN.polysortflag > 0 ) {
2957 while ( t1 < tt1 ) { sum1 += t1[1]; t1 += 2; }
2958 while ( t2 < tt2 ) { sum2 += t2[1]; t2 += 2; }
2959 if ( sum1 < sum2 )
return(low);
2960 if ( sum1 > sum2 )
return(high);
2961 t1 = term1+3; t2 = term2 + 3;
2963 while ( t1 < tt1 && t2 < tt2 ) {
2964 if ( *t1 > *t2 )
return(low);
2965 if ( *t1 < *t2 )
return(high);
2966 if ( t1[1] < t2[1] )
return(low);
2967 if ( t1[1] > t2[1] )
return(high);
2970 if ( t1 < tt1 )
return(high);
2971 if ( t2 < tt2 )
return(low);
2990 WORD *t1, *t2, *tt1, *tt2, *ttt1, *ttt2;
2992 DUMMYUSE(AT.WorkPointer);
2993 t1 = term1 + 1; tt1 = term1+*term1; tt1 -= ABS(tt1[-1]); t1 += 2;
2994 t2 = term2 + 1; tt2 = term2+*term2; tt2 -= ABS(tt2[-1]); t2 += 2;
2995 while ( t1 < tt1 && t2 < tt2 ) {
2997 if ( t1[0] < t2[0] )
return(-1);
3000 else if ( *t1 == HAAKJE ) {
3001 t1 += 3; t2 += 3;
continue;
3003 ttt1 = t1+t1[1]; ttt2 = t2+t2[1];
3004 while ( t1 < ttt1 && t2 < ttt2 ) {
3005 if ( *t1 > *t2 )
return(-1);
3006 if ( *t1 < *t2 )
return(1);
3007 if ( t1[1] < t2[1] )
return(-1);
3008 if ( t1[1] > t2[1] )
return(1);
3011 if ( t1 < ttt1 )
return(1);
3012 if ( t2 < ttt2 )
return(-1);
3014 if ( t1 < tt1 )
return(1);
3015 if ( t2 < tt2 )
return(-1);
3080 if ( AT.SS == AT.S0 && !AR.NoCompress ) {
3081 if ( AN.compressSize == 0 ) {
3082 if ( *ss ) { AN.compressSize = **ss + 64; }
3083 else { AN.compressSize = AM.MaxTer/
sizeof(WORD) + 2; }
3084 AN.compressSpace = (WORD *)Malloc1(AN.compressSize*
sizeof(WORD),
"Compression");
3086 AN.compressSpace[0] = 0;
3091 if ( j > AN.compressSize ) {
3093 t = (WORD *)Malloc1(newsize*
sizeof(WORD),
"Compression");
3095 if ( AN.compressSpace ) {
3096 for ( i = 0; i < *AN.compressSpace; i++ ) t[i] = AN.compressSpace[i];
3097 M_free(AN.compressSpace,
"Compression");
3099 AN.compressSpace = t;
3100 AN.compressSize = newsize;
3102 t = AN.compressSpace;
3106 WORD *polystop, *sa;
3110 while ( polystop < sa && *polystop != AR.PolyFun ) {
3111 polystop += polystop[1];
3113 while ( i > 0 && j > 0 && *s == *t && s < polystop ) {
3114 i--; j--; s++; t++; k--;
3121 while ( i > 0 && j > 0 && *s == *t && s < sa ) { i--; j--; s++; t++; k--; }
3130 size += *AN.compressSpace;
3131 if ( k == -1 ) { t--; s--; j++; }
3133 while ( --j >= 0 ) *t++ = *s++;
3135 t = AN.compressSpace + *AN.compressSpace;
3136 t[-(ABS(t[-1]))] = 0;
3205 #ifdef NEWSPLITMERGE 3211 WORD **pp3, **pp1, **pp2;
3212 LONG nleft, nright, i, newleft, newright;
3215 if ( number < 2 )
return(number);
3216 if ( number == 2 ) {
3217 pp1 = Pointer; pp2 = pp1 + 1;
3218 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3219 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3221 else if ( i == 0 ) {
3223 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3224 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) { number = 0; } }
3228 pptop = Pointer + number;
3229 nleft = number >> 1; nright = number - nleft;
3231 newright =
SplitMerge(BHEAD Pointer+nleft,nright);
3239 if ( newleft > 0 && newright > 0 &&
3240 ( i = CompareTerms(BHEAD Pointer[newleft-1],Pointer[nleft],(WORD)0) ) >= 0 ) {
3241 pp2 = Pointer+nleft; pp1 = Pointer+newleft-1;
3243 if ( S->PolyWise ) {
3244 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3248 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3251 *pp2++ = 0; newright--;
3254 newleft += newright;
3256 while ( --newright >= 0 ) *pp1++ = *pp2++;
3257 while ( pp1 < pptop ) *pp1++ = 0;
3261 if ( nleft > AN.SplitScratchSize ) {
3262 AN.SplitScratchSize = (nleft*3)/2+100;
3263 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3264 AN.SplitScratchSize = S->Terms2InSmall/2;
3265 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3266 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3268 pp3 = AN.SplitScratch; pp1 = Pointer; i = nleft;
3269 do { *pp3++ = *pp1; *pp1++ = 0; }
while ( *pp1 && --i > 0 );
3270 if ( i > 0 ) { *pp3 = 0; i--; }
3271 AN.InScratch = nleft - i;
3272 pp1 = AN.SplitScratch; pp2 = Pointer + nleft; pp3 = Pointer;
3273 while ( nleft > 0 && nright > 0 && *pp1 && *pp2 ) {
3274 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3285 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3286 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3287 *pp1++ = 0; *pp2++ = 0; nleft--; nright--;
3290 while ( --nleft >= 0 && *pp1 ) { *pp3++ = *pp1; *pp1++ = 0; }
3291 while ( --nright >= 0 && *pp2 ) { *pp3++ = *pp2++; }
3292 nleft = pp3 - Pointer;
3293 while ( pp3 < pptop ) *pp3++ = 0;
3304 WORD **pp3, **pp1, **pp2;
3305 LONG nleft, nright, i;
3308 if ( number < 2 )
return;
3309 if ( number == 2 ) {
3310 pp1 = Pointer; pp2 = pp1 + 1;
3311 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3312 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3314 else if ( i == 0 ) {
3315 if ( S->PolyWise ) {
if ( !
AddPoly(BHEAD pp1,pp2) ) { *pp1 = 0; } }
3316 else {
if ( !
AddCoef(BHEAD pp1,pp2) ) { *pp1 = 0; } }
3321 pptop = Pointer + number;
3322 nleft = number >> 1; nright = number - nleft;
3325 if ( nleft > AN.SplitScratchSize ) {
3326 AN.SplitScratchSize = (nleft*3)/2+100;
3327 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3328 AN.SplitScratchSize = S->Terms2InSmall/2;
3329 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3330 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3332 pp3 = AN.SplitScratch; pp1 = Pointer; i = nleft;
3333 do { *pp3++ = *pp1; *pp1++ = 0; }
while ( *pp1 && --i > 0 );
3334 if ( i > 0 ) { *pp3 = 0; i--; }
3335 AN.InScratch = nleft - i;
3336 pp1 = AN.SplitScratch; pp2 = Pointer + nleft; pp3 = Pointer;
3337 while ( *pp1 && *pp2 && nleft > 0 && nright > 0 ) {
3338 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3349 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3350 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3351 *pp1++ = 0; *pp2++ = 0; nleft--; nright--;
3354 while ( *pp1 && --nleft >= 0 ) { *pp3++ = *pp1; *pp1++ = 0; }
3355 while ( *pp2 && --nright >= 0 ) { *pp3++ = *pp2++; }
3356 while ( pp3 < pptop ) *pp3++ = 0;
3387 WORD **Point, *s2, *t, *garbuf, i;
3389 int tobereturned = 0;
3394 MLOCK(ErrorMessageLock);
3395 MesPrint(
"in: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3397 Point = S->sPointer;
3399 while ( --k >= 0 ) {
3400 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3402 Point = AN.SplitScratch;
3404 while ( --k >= 0 ) {
3405 if ( ( s2 = *Point++ ) != 0 ) { total += *s2; }
3408 MesPrint(
"total = %l, nterms = %l",2*total,AN.InScratch);
3409 MUNLOCK(ErrorMessageLock);
3415 if ( S->lBuffer != 0 && S->lFill + total <= S->lTop ) {
3419 garbuf = (WORD *)Malloc1(total*
sizeof(WORD),
"Garbage buffer");
3423 Point = S->sPointer;
3425 while ( --k >= 0 ) {
3433 Point = AN.SplitScratch;
3435 while ( --k >= 0 ) {
3445 Point = S->sPointer;
3447 while ( --k >= 0 ) {
3455 Point = AN.SplitScratch;
3457 while ( --k >= 0 ) {
3467 MLOCK(ErrorMessageLock);
3468 MesPrint(
"out: S->sFill = %x, S->sTop2 = %x",S->sFill,S->sTop2);
3469 if ( S->sFill >= S->sTop2 ) {
3470 MesPrint(
"We are in deep trouble");
3472 MUNLOCK(ErrorMessageLock);
3474 if ( tobereturned ) M_free(garbuf,
"Garbage buffer");
3502 WORD **poin, **poin2, ul, k, i, im, *m1;
3503 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3504 WORD *m2, *m3, r31, r33, ki, *rr;
3514 int oldgzipCompress = AR.gzipCompress;
3516 AR.gzipCompress = 0;
3520 fout = &(AR.FoStage4[0]);
3523 poin = S->poina; poin2 = S->poin2a;
3524 rr = AR.CompressPointer;
3531 if ( fout->
handle < 0 ) {
3533 PUTZERO(AN.OldPosOut);
3534 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3535 MLOCK(ErrorMessageLock);
3536 MesPrint(
"Cannot create file %s",fout->name);
3537 MUNLOCK(ErrorMessageLock);
3541 MLOCK(ErrorMessageLock);
3542 MesPrint(
"%w MergePatches created output file %s",fout->name);
3543 MUNLOCK(ErrorMessageLock);
3546 PUTZERO(fout->filesize);
3547 PUTZERO(fout->POposition);
3549 fout->ziobuffer = 0;
3552 LOCK(fout->pthreadslock);
3554 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3556 UNLOCK(fout->pthreadslock);
3559 PUTZERO(S->fPatches[0]);
3560 fout->POfill = fout->PObuffer;
3561 PUTZERO(fout->POposition);
3566 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3567 S->fpcompressed[S->fPatchN] = 1;
3569 S->fpcompressed[S->fPatchN] = 0;
3570 SetupOutputGZIP(fout);
3573 else if ( par == 0 && S->stage4 > 0 ) {
3585 S->iPatches = S->fPatches;
3586 S->fPatches = S->inPatches;
3587 S->inPatches = S->iPatches;
3588 (S->inNum) = S->fPatchN;
3589 AN.OldPosIn = AN.OldPosOut;
3591 m1 = S->fpincompressed;
3592 S->fpincompressed = S->fpcompressed;
3593 S->fpcompressed = m1;
3594 for ( i = 0; i < S->inNum; i++ ) {
3595 S->fPatchesStop[i] = S->iPatches[i+1];
3597 MLOCK(ErrorMessageLock);
3598 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3599 MUNLOCK(ErrorMessageLock);
3611 AR.gzipCompress = 0;
3614 S->iPatches = S->fPatches;
3615 S->inNum = S->fPatchN;
3617 m1 = S->fpincompressed;
3618 S->fpincompressed = S->fpcompressed;
3619 S->fpcompressed = m1;
3620 for ( i = 0; i < S->inNum; i++ ) {
3621 S->fPatchesStop[i] = S->fPatches[i+1];
3623 MLOCK(ErrorMessageLock);
3624 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3625 MUNLOCK(ErrorMessageLock);
3633 S->Patches[S->lPatch] = S->lFill;
3634 for ( i = 0; i < S->lPatch; i++ ) {
3635 S->pStop[i] = S->Patches[i+1]-1;
3636 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3640 S->lPatch = (S->inNum);
3642 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3644 if ( S->lPatch > 1 ) {
3647 SetupAllInputGZIP(S);
3650 for ( i = 0; i < S->lPatch; i++ ) {
3651 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3653 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3654 S->pStop[i] = m2 = p;
3656 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3658 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3663 if ( fout->
handle >= 0 ) {
3666 LOCK(fout->pthreadslock);
3668 SeekFile(fout->
handle,&position,SEEK_END);
3669 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3671 UNLOCK(fout->pthreadslock);
3675 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3684 if ( S->lPatch == 1 ) {
3687 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3693 m2 = m1 = *S->Patches;
3696 i = -(*m1++); m2 += i; im = *m1+i+1;
3697 while ( i > 0 ) { *m1-- = *m2--; i--; }
3701 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD m1); }
3704 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3705 ADDPOS(S->SizeInFile[par],im);
3710 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3713 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3714 ADDPOS(S->SizeInFile[par],1);
3717 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3718 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3720 ADDPOS(position,length);
3721 ADDPOS(fout->POposition,length);
3722 ADDPOS(fout->filesize,length);
3723 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3733 #define FRONTSIZE (2*AM.MaxTer) 3734 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3736 SetupOutputGZIP(fout);
3737 SetupAllInputGZIP(S);
3739 position2 = S->iPatches[0];
3740 while ( ( length = FillInputGZIP(fin,&position2,
3742 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3743 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3744 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3745 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3753 i = -(*m1++); m2 += i; im = *m1+i+1;
3754 while ( i > 0 ) { *m1-- = *m2--; i--; }
3758 if ( AS.MasterSort && ( fout == AR.outfile ) ) {
3759 im = PutToMaster(BHEAD m1);
3763 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3764 ADDPOS(S->SizeInFile[par],im);
3768 if ( m1 < copytop && *m1 == 0 )
break;
3774 while ( m1 > m2 ) *--m3 = *--m1;
3779 MLOCK(ErrorMessageLock);
3780 MesPrint(
"Readerror");
3784 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3787 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3788 ADDPOS(S->SizeInFile[par],1);
3791 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3792 while ( ( length = ReadFile(fin->
handle,
3793 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3794 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3796 ADDPOS(position,length);
3797 ADDPOS(fout->POposition,length);
3798 ADDPOS(fout->filesize,length);
3799 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3802 MLOCK(ErrorMessageLock);
3803 MesPrint(
"Readerror");
3810 else if ( S->lPatch > 0 ) {
3815 do { lpat <<= 1; }
while ( lpat < S->lPatch );
3816 mpat = ( lpat >> 1 ) - 1;
3817 k = lpat - S->lPatch;
3822 for ( i = 1; i < lpat; i++ ) {
3825 for ( i = 1; i <= k; i++ ) {
3826 im = ( i << 1 ) - 1;
3827 poin[im] = S->Patches[i-1];
3828 poin2[im] = poin[im] + *(poin[im]);
3831 S->tree[mpat+i] = 0;
3832 poin[im-1] = poin2[im-1] = 0;
3834 for ( i = (k<<1)+1; i <= lpat; i++ ) {
3837 poin[i] = S->Patches[i-k-1];
3838 poin2[i] = poin[i] + *(poin[i]);
3858 if ( !*(poin[k]) ) {
3859 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3860 if ( S->tree[i] == -1 ) {
3873 if ( S->tree[i] > 0 ) {
3874 if ( ( c = CompareTerms(BHEAD poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3878 S->used[level] = S->tree[i];
3888 l1 = *( m1 = poin[S->tree[i]] );
3889 l2 = *( m2 = poin[k] );
3890 if ( S->PolyWise ) {
3895 if ( S->PolyFlag == 2 ) {
3896 w = poly_ratfun_add(BHEAD m1,m2);
3897 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3898 MLOCK(ErrorMessageLock);
3899 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3900 MUNLOCK(ErrorMessageLock);
3907 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3908 MLOCK(ErrorMessageLock);
3909 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3910 MUNLOCK(ErrorMessageLock);
3917 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
3919 if ( r1 == m1[1] ) {
3922 else if ( r1 < m1[1] ) {
3926 while ( --r1 >= 0 ) *--m1 = *--m2;
3929 while ( --r1 >= 0 ) *--m1 = *--m2;
3931 poin[S->tree[i]] = m1;
3939 poin[S->tree[i]] = m2;
3946 r1 = *( m1 += l1 - 1 );
3948 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
3949 r2 = *( m2 += l2 - 1 );
3951 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
3953 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
3954 MLOCK(ErrorMessageLock);
3955 MesCall(
"MergePatches");
3956 MUNLOCK(ErrorMessageLock);
3960 if ( AN.ncmod != 0 ) {
3961 if ( ( AC.modmode & POSNEG ) != 0 ) {
3964 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
3966 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
3968 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
3972 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
3973 if ( r3 < 0 ) r3 = -r3;
3974 if ( r1 < 0 ) r1 = -r1;
3979 ul = S->used[level] = S->tree[i];
3986 r1 = poin2[ul][1] - im + 1;
3988 m2 = poin[ul] - im + 1;
3989 while ( ++im <= 0 ) *--m1 = *--m2;
3994 poin[ul] = poin2[ul];
3996 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
3999 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
4001 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4002 S->Patches[ki],&(poin[ul]),ki));
4004 poin2[ul] = poin[ul] + im;
4009 S->used[++level] = k;
4015 else if ( r31 < 0 ) {
4024 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
4030 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
4031 MLOCK(ErrorMessageLock);
4032 MesPrint(
"Coefficient overflow during sort");
4033 MUNLOCK(ErrorMessageLock);
4036 m2 = poin[S->tree[i]];
4037 m3 = ( poin[S->tree[i]] -= r31 );
4038 do { *m3++ = *m2++; }
while ( m2 < m1 );
4042 *(poin[S->tree[i]]) += r31;
4044 m2 = (WORD *)coef; im = r3;
4055 r1 = poin2[k][1] - im + 1;
4057 m2 = poin[k] - im + 1;
4058 while ( ++im <= 0 ) *--m1 = *--m2;
4065 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
4068 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
4070 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4071 S->Patches[ki],&(poin[k]),ki));
4073 poin2[k] = poin[k] + im;
4081 else if ( S->tree[i] < 0 ) {
4092 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD poin[k]); }
4095 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
4096 MLOCK(ErrorMessageLock);
4097 MesPrint(
"Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
4098 MUNLOCK(ErrorMessageLock);
4101 ADDPOS(S->SizeInFile[par],im);
4109 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
4112 if (
FlushOut(&position,fout,1) )
goto ReturnError;
4113 ADDPOS(S->SizeInFile[par],1);
4117 SeekFile(fout->
handle,&position,SEEK_CUR);
4120 S->fPatches[S->fPatchN] = position;
4122 if ( par == 0 && fout != AR.outfile ) {
4131 S->fPatches[S->fPatchN] = position;
4132 if ( ISNOTZEROPOS(AN.OldPosIn) ) {
4134 SeekFile(fin->
handle,&(AN.OldPosIn),SEEK_SET);
4140 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
4142 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&AN.OldPosIn),(LONG)
sizeof(
POSITION)) !=
4144 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
4146 MLOCK(ErrorMessageLock);
4147 MesPrint(
"Read error fourth stage sorting");
4148 MUNLOCK(ErrorMessageLock);
4153 for ( i = 0; i < S->inNum; i++ ) {
4154 S->fPatchesStop[i] = S->iPatches[i+1];
4156 MLOCK(ErrorMessageLock);
4157 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
4158 MUNLOCK(ErrorMessageLock);
4177 MLOCK(ErrorMessageLock);
4178 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4179 MUNLOCK(ErrorMessageLock);
4189 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4190 PUTZERO(S->SizeInFile[0]);
4201 MLOCK(ErrorMessageLock);
4202 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4203 MUNLOCK(ErrorMessageLock);
4208 AR.gzipCompress = oldgzipCompress;
4213 AR.gzipCompress = oldgzipCompress;
4218 MLOCK(ErrorMessageLock);
4219 MesPrint(
"Error while writing to file.");
4223 MLOCK(ErrorMessageLock);
4225 MesCall(
"MergePatches");
4226 MUNLOCK(ErrorMessageLock);
4228 AR.gzipCompress = oldgzipCompress;
4250 WORD **ss, *lfill, j, *t;
4252 LONG lSpace, sSpace, RetCode, over, tover;
4254 if ( ( ( AP.PreDebug & DUMPTOSORT ) == DUMPTOSORT ) && AR.sLevel == 0 ) {
4256 sprintf((
char *)(THRbuf),
"StoreTerm(%d)",AT.identity);
4257 PrintTerm(term,(
char *)(THRbuf));
4259 PrintTerm(term,
"StoreTerm");
4262 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
4263 S->sFill = *(S->PoinFill);
4264 if ( S->sTerms >= S->TermsInSmall || ( S->sFill + *term ) >= S->sTop ) {
4268 tover = over = S->sTerms;
4279 S->TermsLeft -= over - RetCode;
4283 lSpace = sSpace + (S->lFill - S->lBuffer)
4284 - (AM.MaxTer/
sizeof(WORD))*((LONG)S->lPatch);
4285 SETBASEPOSITION(pp,lSpace);
4286 MULPOS(pp,
sizeof(WORD));
4287 if ( S->file.handle >= 0 ) {
4288 ADD2POS(pp,S->fPatches[S->fPatchN]);
4291 WORD oldLogHandle = AC.LogHandle;
4292 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4294 AC.LogHandle = oldLogHandle;
4296 if ( ( S->lPatch >= S->MaxPatches ) ||
4297 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer ) ) >= S->lTop ) ) {
4307 SETBASEPOSITION(pp,sSpace);
4308 MULPOS(pp,
sizeof(WORD));
4309 ADD2POS(pp,S->fPatches[S->fPatchN]);
4312 WORD oldLogHandle = AC.LogHandle;
4313 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
4315 AC.LogHandle = oldLogHandle;
4318 S->lFill = S->lBuffer;
4320 S->Patches[S->lPatch++] = S->lFill;
4321 lfill = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
4324 while ( ( t = *ss++ ) != 0 ) {
4326 if ( j < 0 ) j = t[1] + 2;
4335 S->PoinFill = S->sPointer;
4336 *(S->PoinFill) = S->sFill = S->sBuffer;
4339 while ( --j >= 0 ) *S->sFill++ = *term++;
4343 *++S->PoinFill = S->sFill;
4348 MLOCK(ErrorMessageLock);
4349 MesCall(
"StoreTerm");
4350 MUNLOCK(ErrorMessageLock);
4368 if ( S->fPatchN >= S->MaxFpatches ) {
4371 MLOCK(ErrorMessageLock);
4373 MesPrint(
"StageSort in thread %d",identity);
4374 #elif defined(WITHMPI) 4375 MesPrint(
"StageSort in process %d",PF.me);
4377 MesPrint(
"StageSort");
4379 MUNLOCK(ErrorMessageLock);
4380 SeekFile(fout->
handle,&position,SEEK_END);
4385 if ( (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(S->fPatchN)),(LONG)
sizeof(WORD)) !=
4387 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(&(AN.OldPosOut)),(LONG)
sizeof(
POSITION)) !=
4389 || (ULONG)WriteFile(fout->
handle,(UBYTE *)(S->fPatches),(LONG)(S->fPatchN+1)
4391 MLOCK(ErrorMessageLock);
4392 MesPrint(
"Write error while staging sort. Disk full?");
4393 MUNLOCK(ErrorMessageLock);
4396 AN.OldPosOut = position;
4397 fout->filesize = position;
4398 ADDPOS(fout->filesize,(S->fPatchN+2)*
sizeof(
POSITION) +
sizeof(WORD));
4399 fout->POposition = fout->filesize;
4400 S->fPatches[0] = fout->filesize;
4403 if ( AR.FoStage4[0].PObuffer == 0 ) {
4404 AR.FoStage4[0].PObuffer = (WORD *)Malloc1(AR.FoStage4[0].POsize*
sizeof(WORD)
4406 AR.FoStage4[0].POfill = AR.FoStage4[0].PObuffer;
4407 AR.FoStage4[0].POstop = AR.FoStage4[0].PObuffer
4408 + AR.FoStage4[0].POsize/
sizeof(WORD);
4410 AR.FoStage4[0].pthreadslock = dummylock;
4413 if ( AR.FoStage4[1].PObuffer == 0 ) {
4414 AR.FoStage4[1].PObuffer = (WORD *)Malloc1(AR.FoStage4[1].POsize*
sizeof(WORD)
4416 AR.FoStage4[1].POfill = AR.FoStage4[1].PObuffer;
4417 AR.FoStage4[1].POstop = AR.FoStage4[1].PObuffer
4418 + AR.FoStage4[1].POsize/
sizeof(WORD);
4420 AR.FoStage4[1].pthreadslock = dummylock;
4447 WORD *v, *s, *m, k, i;
4448 WORD *pScrat, *stop, *sv, error = 0;
4449 pScrat = AT.WorkPointer;
4450 if ( ( AT.WorkPointer + 8 * AM.MaxWildcards ) >= AT.WorkTop ) {
4451 MLOCK(ErrorMessageLock);
4453 MUNLOCK(ErrorMessageLock);
4461 while ( v < stop && (
4462 *v == FROMSET || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4463 while ( v < stop ) {
4465 if ( AM.Ordering[*v] < AM.Ordering[*m] ) {
4468 else if ( *v == *m ) {
4469 if ( v[2] < m[2] ) {
4472 else if ( v[2] == m[2] ) {
4475 if ( s < stop && ( *s == FROMSET
4476 || *s == SETTONUM || *s == LOADDOLLAR ) ) {
4477 if ( sv < stop && ( *sv == FROMSET
4478 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4479 if ( s[2] != sv[2] ) {
4481 MLOCK(ErrorMessageLock);
4482 MesPrint(
"&Wildcard set conflict");
4483 MUNLOCK(ErrorMessageLock);
4489 if ( sv < stop && ( *sv == FROMSET
4490 || *sv == SETTONUM || *sv == LOADDOLLAR ) ) {
4502 while ( v < stop && ( *v == FROMSET
4503 || *v == SETTONUM || *v == LOADDOLLAR ) ) v += v[1];
4509 while ( m < stop && ( *m == FROMSET
4510 || *m == SETTONUM || *m == LOADDOLLAR ) ) {
4517 while ( i < nw && ( w[i] < 0 || w[i] == FROMSET
4518 || w[i] == SETTONUM || w[i] == LOADDOLLAR ) ) i += w[i+1];
4520 AC.NwildC = k = WORDDIF(pScrat,AT.WorkPointer);
4540 int minnum = num, i;
4541 if ( AN.FunSorts ) {
4543 if ( AN.MaxFunSorts > 3 ) {
4544 minnum = (AN.MaxFunSorts+4)/2;
4548 else if ( minnum == 0 ) minnum = 1;
4549 for ( i = minnum; i < AN.NumFunSorts; i++ ) {
4552 if ( S->file.handle >= 0 ) {
4555 CloseFile(S->file.handle);
4556 S->file.handle = -1;
4557 remove(S->file.name);
4559 MLOCK(ErrorMessageLock);
4560 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4561 MUNLOCK(ErrorMessageLock);
4564 M_free(S,
"sorting struct");
4568 AN.MaxFunSorts = minnum;
4572 if ( S->file.handle >= 0 ) {
4575 CloseFile(S->file.handle);
4576 S->file.handle = -1;
4577 remove(S->file.name);
4579 MLOCK(ErrorMessageLock);
4580 MesPrint(
"%w CleanUpSort removed file %s",S->file.name);
4581 MUNLOCK(ErrorMessageLock);
4587 for ( i = 0; i < 2; i++ ) {
4588 if ( AR.FoStage4[i].handle >= 0 ) {
4590 CloseFile(AR.FoStage4[i].handle);
4591 remove(AR.FoStage4[i].name);
4592 AR.FoStage4[i].handle = -1;
4594 MLOCK(ErrorMessageLock);
4595 MesPrint(
"%w CleanUpSort removed stage4 file %s",AR.FoStage4[i].name);
4596 MUNLOCK(ErrorMessageLock);
4613 if ( AR.sLevel >= 0 ) {
4615 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
4627 WORD *PolyRatFunSpecial(
PHEAD WORD *t1, WORD *t2)
4629 WORD *oldworkpointer = AT.WorkPointer, *t, *r;
4633 if ( *t == -SYMBOL ) {
4634 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4636 if ( t[2] != -SNUMBER )
goto Illegal;
4639 else if ( *t == -SNUMBER ) {
4642 if ( *t == -SYMBOL ) {
4643 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4646 else if ( *t == -SNUMBER ) {
4650 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4651 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4655 exp1 = -t[ARGHEAD+4];
4659 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4660 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4664 exp1 = t[ARGHEAD+4];
4666 if ( *t != -SNUMBER )
goto Illegal;
4672 if ( *t == -SYMBOL ) {
4673 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4675 if ( t[2] != -SNUMBER )
goto Illegal;
4678 else if ( *t == -SNUMBER ) {
4681 if ( *t == -SYMBOL ) {
4682 if ( t[1] != AR.PolyFunVar )
goto Illegal;
4685 else if ( *t == -SNUMBER ) {
4689 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4690 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4694 exp2 = -t[ARGHEAD+4];
4698 else if ( *t == ARGHEAD+8 && t[ARGHEAD] == 8 && t[ARGHEAD+1] == SYMBOL
4699 && t[ARGHEAD+3] == AR.PolyFunVar ) {
4703 exp2 = t[ARGHEAD+4];
4705 if ( *t != -SNUMBER )
goto Illegal;
4710 if ( exp1 <= exp2 ) { i = t1[1]; r = t1; }
4711 else { i = t2[1]; r = t2; }
4715 return(oldworkpointer);
4717 MesPrint(
"Illegal occurrence of PolyRatFun with divergent option");
int NormalModulus(UWORD *, WORD *)
LONG EndSort(PHEAD WORD *buffer, int par)
WORD StoreTerm(PHEAD WORD *term)
WORD Compare1(PHEAD WORD *term1, WORD *term2, WORD level)
WORD FlushOut(POSITION *position, FILEHANDLE *fi, int compr)
void CleanUpSort(int num)
int CompareSymbols(PHEAD WORD *term1, WORD *term2, WORD par)
LONG SplitMerge(PHEAD WORD **Pointer, LONG number)
LONG PutIn(FILEHANDLE *file, POSITION *position, WORD *buffer, WORD **take, int npat)
VOID AddArgs(PHEAD WORD *s1, WORD *s2, WORD *m)
int PF_ISendSbuf(int to, int tag)
WORD PutOut(PHEAD WORD *term, POSITION *position, FILEHANDLE *fi, WORD ncomp)
int CompareHSymbols(PHEAD WORD *term1, WORD *term2, WORD par)
WORD Sflush(FILEHANDLE *fi)
LONG ComPress(WORD **ss, LONG *n)
BRACKETINDEX * indexbuffer
VOID StageSort(FILEHANDLE *fout)
WORD AddPoly(PHEAD WORD **ps1, WORD **ps2)
WORD AddCoef(PHEAD WORD **ps1, WORD **ps2)
VOID WriteStats(POSITION *plspace, WORD par)
WORD CompCoef(WORD *, WORD *)
WORD MergePatches(WORD par)
WORD SortWild(WORD *w, WORD nw)