96 LONG millitime, y = 0x7FFFFFFFL >> 1;
101 if ( AT.SS == AT.S0 && AC.StatsFlag ) {
103 if ( AC.ThreadStats == 0 && identity > 0 )
return;
104#elif defined(WITHMPI)
105 if ( AC.OldParallelStats )
return;
106 if ( ! AC.ProcessStats && PF.me != MASTER )
return;
108 if ( Expressions == 0 )
return;
111 if ( AC.ShortStatsMax == 0 )
return;
113 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
115 AR.ShortSortCount = 0;
118 MLOCK(ErrorMessageLock);
119 if ( AC.ShortStats ) {}
122 if ( identity > 0 ) {
123 MesPrint(
" Thread %d reporting",identity);
128#elif defined(WITHMPI)
129 if ( PF.me != MASTER ) {
130 MesPrint(
" Process %d reporting",PF.me);
145 use_wtime = AC.WTimeStatsFlag;
146#if defined(WITHPTHREADS)
147 if ( use_wtime && identity > 0 ) use_wtime = 0;
148#elif defined(WITHMPI)
149 if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
152 timepart = (WORD)(millitime%1000);
155 if ( AC.ShortStats ) {
156#if defined(WITHPTHREADS) || defined(WITHMPI)
158 if ( identity > 0 ) {
160 if ( PF.me != MASTER ) {
161 const int identity = PF.me;
163 if ( par == 0 || par == 2 ) {
164 SETBASEPOSITION(pp,y);
165 if ( ISLESSPOS(*plspace,pp) ) {
166 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%10p %s %s",identity,
167 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
168 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
177 SETBASEPOSITION(pp,y);
179 if ( ISLESSPOS(*plspace,pp) ) {
180 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%11p %s %s",identity,
181 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
182 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
186 if ( ISLESSPOS(*plspace,pp) ) {
187 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%12p %s %s",identity,
188 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
189 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
193 if ( ISLESSPOS(*plspace,pp) ) {
194 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%13p %s %s",identity,
195 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
196 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
200 if ( ISLESSPOS(*plspace,pp) ) {
201 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%14p %s %s",identity,
202 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
203 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
207 if ( ISLESSPOS(*plspace,pp) ) {
208 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%15p %s %s",identity,
209 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
210 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
214 if ( ISLESSPOS(*plspace,pp) ) {
215 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%16p %s %s",identity,
216 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
217 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
221 if ( ISLESSPOS(*plspace,pp) ) {
222 MesPrint(
"%d: %7l.%2is %8l>%10l%3s%10l:%17p %s %s",identity,
223 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
224 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
230 else if ( par == 1 ) {
231 SETBASEPOSITION(pp,y);
232 if ( ISLESSPOS(*plspace,pp) ) {
233 MesPrint(
"%d: %7l.%2is %10l:%10p",identity,millitime,timepart,
234 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
238 SETBASEPOSITION(pp,y);
240 if ( ISLESSPOS(*plspace,pp) ) {
241 MesPrint(
"%d: %7l.%2is %10l:%11p",identity,millitime,timepart,
242 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
246 if ( ISLESSPOS(*plspace,pp) ) {
247 MesPrint(
"%d: %7l.%2is %10l:%12p",identity,millitime,timepart,
248 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
252 if ( ISLESSPOS(*plspace,pp) ) {
253 MesPrint(
"%d: %7l.%2is %10l:%13p",identity,millitime,timepart,
254 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
258 if ( ISLESSPOS(*plspace,pp) ) {
259 MesPrint(
"%d: %7l.%2is %10l:%14p",identity,millitime,timepart,
260 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
264 if ( ISLESSPOS(*plspace,pp) ) {
265 MesPrint(
"%d: %7l.%2is %10l:%15p",identity,millitime,timepart,
266 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
270 if ( ISLESSPOS(*plspace,pp) ) {
271 MesPrint(
"%d: %7l.%2is %10l:%16p",identity,millitime,timepart,
272 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
276 if ( ISLESSPOS(*plspace,pp) ) {
277 MesPrint(
"%d: %7l.%2is %10l:%17p",identity,millitime,timepart,
278 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
286 if ( par == 0 || par == 2 ) {
287 SETBASEPOSITION(pp,y);
288 if ( ISLESSPOS(*plspace,pp) ) {
289 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%10p %s %s",
290 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
291 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
300 SETBASEPOSITION(pp,y);
302 if ( ISLESSPOS(*plspace,pp) ) {
303 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%11p %s %s",
304 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
305 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
309 if ( ISLESSPOS(*plspace,pp) ) {
310 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%12p %s %s",
311 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
312 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
316 if ( ISLESSPOS(*plspace,pp) ) {
317 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%13p %s %s",
318 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
319 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
323 if ( ISLESSPOS(*plspace,pp) ) {
324 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%14p %s %s",
325 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
326 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
330 if ( ISLESSPOS(*plspace,pp) ) {
331 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%15p %s %s",
332 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
333 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
337 if ( ISLESSPOS(*plspace,pp) ) {
338 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%16p %s %s",
339 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
340 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
344 if ( ISLESSPOS(*plspace,pp) ) {
345 MesPrint(
"%7l.%2is %8l>%10l%3s%10l:%17p %s %s",
346 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
347 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
353 else if ( par == 1 ) {
354 SETBASEPOSITION(pp,y);
355 if ( ISLESSPOS(*plspace,pp) ) {
356 MesPrint(
"%7l.%2is %10l:%10p",millitime,timepart,
357 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
361 SETBASEPOSITION(pp,y);
363 if ( ISLESSPOS(*plspace,pp) ) {
364 MesPrint(
"%7l.%2is %10l:%11p",millitime,timepart,
365 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
369 if ( ISLESSPOS(*plspace,pp) ) {
370 MesPrint(
"%7l.%2is %10l:%12p",millitime,timepart,
371 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
375 if ( ISLESSPOS(*plspace,pp) ) {
376 MesPrint(
"%7l.%2is %10l:%13p",millitime,timepart,
377 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
381 if ( ISLESSPOS(*plspace,pp) ) {
382 MesPrint(
"%7l.%2is %10l:%14p",millitime,timepart,
383 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
387 if ( ISLESSPOS(*plspace,pp) ) {
388 MesPrint(
"%7l.%2is %10l:%15p",millitime,timepart,
389 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
393 if ( ISLESSPOS(*plspace,pp) ) {
394 MesPrint(
"%7l.%2is %10l:%16p",millitime,timepart,
395 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
399 if ( ISLESSPOS(*plspace,pp) ) {
400 MesPrint(
"%7l.%2is %10l:%17p",millitime,timepart,
401 S->TermsLeft,plspace,EXPRNAME(AR.CurExpr),AC.Commercial);
411 MesPrint(
"WTime = %7l.%2i sec",millitime,timepart);
414 MesPrint(
"Time = %7l.%2i sec",millitime,timepart);
418#if ( BITSINLONG > 32 )
419 if ( S->GenTerms >= 10000000000L ) {
421 MesPrint(
"WTime = %7l.%2i sec Generated terms = %16l",
422 millitime,timepart,S->GenTerms);
425 MesPrint(
"Time = %7l.%2i sec Generated terms = %16l",
426 millitime,timepart,S->GenTerms);
431 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
432 millitime,timepart,S->GenTerms);
435 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
436 millitime,timepart,S->GenTerms);
441 MesPrint(
"WTime = %7l.%2i sec Generated terms = %10l",
442 millitime,timepart,S->GenTerms);
445 MesPrint(
"Time = %7l.%2i sec Generated terms = %10l",
446 millitime,timepart,S->GenTerms);
450#if ( BITSINLONG > 32 )
452 if ( S->TermsLeft >= 10000000000L ) {
453 MesPrint(
"%16s%8l Terms %s = %16l",EXPRNAME(AR.CurExpr),
454 AN.ninterms,FG.swmes[par],S->TermsLeft);
457 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
458 AN.ninterms,FG.swmes[par],S->TermsLeft);
461 if ( S->TermsLeft >= 10000000000L ) {
463 if ( identity > 0 && par == 2 ) {
464 MesPrint(
"%16s Terms in thread = %16l",
465 EXPRNAME(AR.CurExpr),S->TermsLeft);
468#elif defined(WITHMPI)
469 if ( PF.me != MASTER && par == 2 ) {
470 MesPrint(
"%16s Terms in process= %16l",
471 EXPRNAME(AR.CurExpr),S->TermsLeft);
476 MesPrint(
"%16s Terms %s = %16l",
477 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
482 if ( identity > 0 && par == 2 ) {
483 MesPrint(
"%16s Terms in thread = %10l",
484 EXPRNAME(AR.CurExpr),S->TermsLeft);
487#elif defined(WITHMPI)
488 if ( PF.me != MASTER && par == 2 ) {
489 MesPrint(
"%16s Terms in process= %10l",
490 EXPRNAME(AR.CurExpr),S->TermsLeft);
495 MesPrint(
"%16s Terms %s = %10l",
496 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
502 MesPrint(
"%16s%8l Terms %s = %10l",EXPRNAME(AR.CurExpr),
503 AN.ninterms,FG.swmes[par],S->TermsLeft);
506 if ( identity > 0 && par == 2 ) {
507 MesPrint(
"%16s Terms in thread = %10l",
508 EXPRNAME(AR.CurExpr),S->TermsLeft);
511#elif defined(WITHMPI)
512 if ( PF.me != MASTER && par == 2 ) {
513 MesPrint(
"%16s Terms in process= %10l",
514 EXPRNAME(AR.CurExpr),S->TermsLeft);
519 MesPrint(
"%16s Terms %s = %10l",
520 EXPRNAME(AR.CurExpr),FG.swmes[par],S->TermsLeft);
524 SETBASEPOSITION(pp,y);
525 if ( ISLESSPOS(*plspace,pp) ) {
526 MesPrint(
"%24s Bytes used = %10p",AC.Commercial,plspace);
530 SETBASEPOSITION(pp,y);
532 if ( ISLESSPOS(*plspace,pp) ) {
533 MesPrint(
"%24s Bytes used =%11p",AC.Commercial,plspace);
537 if ( ISLESSPOS(*plspace,pp) ) {
538 MesPrint(
"%24s Bytes used =%12p",AC.Commercial,plspace);
542 if ( ISLESSPOS(*plspace,pp) ) {
543 MesPrint(
"%24s Bytes used =%13p",AC.Commercial,plspace);
547 if ( ISLESSPOS(*plspace,pp) ) {
548 MesPrint(
"%24s Bytes used =%14p",AC.Commercial,plspace);
552 if ( ISLESSPOS(*plspace,pp) ) {
553 MesPrint(
"%24s Bytes used =%15p",AC.Commercial,plspace);
557 if ( ISLESSPOS(*plspace,pp) ) {
558 MesPrint(
"%24s Bytes used =%16p",AC.Commercial,plspace);
562 if ( ISLESSPOS(*plspace,pp) ) {
563 MesPrint(
"%24s Bytes used=%17p",AC.Commercial,plspace);
569 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l"
570 ,numwrites,numreads,numseeks);
571 MesPrint(
"Total number of mallocs: %l, frees: %l"
572 ,nummallocs,numfrees);
574 MUNLOCK(ErrorMessageLock);
686 WORD j, **ss, *to, *t;
687 LONG sSpace, over, tover, spare, retval = 0, jj;
688 POSITION position, pp;
690 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
692 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
695 oldoutfile = AR.outfile;
705 oldoutfile = AR.outfile;
724 PrintTime((UBYTE *)
"EndSort, before SplitMerge");
726 S->sPointer[
SplitMerge(BHEAD S->sPointer,S->sTerms)] = 0;
728 PrintTime((UBYTE *)
"Endsort, after SplitMerge");
731 tover = over = S->sTerms;
734 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
737 S->TermsLeft -= over - spare;
738 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
740 else if ( S != AT.S0 ) {
744 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
745 if ( AN.tryterm > 0 && ( (sSpace+1)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
746 to = TermMalloc(
"$-sort space");
749 LONG allocsp = sSpace+1;
750 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
751 allocsp = ((allocsp+7)/8)*8;
752 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
753 if ( AN.tryterm > 0 ) AN.tryterm = 0;
755 *((WORD **)buffer) = to;
757 while ( ( t = *ss++ ) != 0 ) {
758 j = *t;
while ( --j >= 0 ) *to++ = *t++;
766 while ( ( t = *ss++ ) != 0 ) {
768 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
769 MLOCK(ErrorMessageLock);
770 MesPrint(
"Sorted function argument too long.");
771 MUNLOCK(ErrorMessageLock);
772 retval = -1;
goto RetRetval;
774 while ( --j >= 0 ) *to++ = *t++;
784 *AR.CompressPointer = 0;
785 SeekScratch(AR.outfile,&position);
797 {
int oldgzipCompress = AR.gzipCompress;
802 while ( ( t = *ss++ ) != 0 ) {
803 if ( *t ) S->TermsLeft++;
805 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
808 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
809 retval = -1;
goto RetRetval;
814 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
818 retval = -1;
goto RetRetval;
821 AR.gzipCompress = oldgzipCompress;
825 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
828 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
830 DIFPOS(oldpos,position,oldpos);
831 S->SpaceLeft = BASEPOSITION(oldpos);
837 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
839 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
842 SETBASEPOSITION(pp,lSpace);
843 MULPOS(pp,
sizeof(WORD));
844 if ( S->file.handle >= 0 ) {
845 ADD2POS(pp,S->fPatches[S->fPatchN]);
848 WORD oldLogHandle = AC.LogHandle;
849 if ( AC.LogHandle >= 0 && AM.LogType && ( ( S->lPatch > 0 )
850 || S->file.handle >= 0 ) ) AC.LogHandle = -1;
851 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
WriteStats(&pp,0); }
852 AC.LogHandle = oldLogHandle;
854 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
855 if ( S->lPatch > 0 ) {
856 if ( ( S->lPatch >= S->MaxPatches ) ||
857 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
862 MLOCK(ErrorMessageLock);
863 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x"
864 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
865 MUNLOCK(ErrorMessageLock);
869 MLOCK(ErrorMessageLock);
871 MUNLOCK(ErrorMessageLock);
872 retval = -1;
goto RetRetval;
875 pp = S->SizeInFile[1];
876 MULPOS(pp,
sizeof(WORD));
881 WORD oldLogHandle = AC.LogHandle;
883 SETBASEPOSITION(pppp,0);
884 SeekFile(S->file.handle,&pppp,SEEK_CUR);
885 SeekFile(S->file.handle,&pp,SEEK_END);
886 SeekFile(S->file.handle,&pppp,SEEK_SET);
887 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
889 AC.LogHandle = oldLogHandle;
894 S->Patches[S->lPatch++] = S->lFill;
895 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
898 while ( ( t = *ss++ ) != 0 ) {
900 if ( j < 0 ) j = t[1] + 2;
901 while ( --j >= 0 ) *to++ = *t++;
906 if ( S->file.handle < 0 ) {
908 MLOCK(ErrorMessageLock);
910 MUNLOCK(ErrorMessageLock);
911 retval = -1;
goto RetRetval;
914 pp = S->SizeInFile[2];
915 MULPOS(pp,
sizeof(WORD));
917 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
923 if ( par == 2 && newout->handle >= 0 ) {
927 LOCK(newout->pthreadslock);
929 SeekFile(newout->handle,&zeropos,SEEK_SET);
930 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+
sizeof(WORD)*2
931 ,
"$-buffer reading");
932 if ( AN.tryterm > 0 ) AN.tryterm = 0;
933 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
934 BASEPOSITION(newout->filesize) ) {
935 MLOCK(ErrorMessageLock);
936 MesPrint(
"Error reading information for $ variable");
937 MUNLOCK(ErrorMessageLock);
938 M_free(to,
"$-buffer reading");
942 *((WORD **)buffer) = to;
943 retval /=
sizeof(WORD);
946 UNLOCK(newout->pthreadslock);
949 else if ( newout->handle >= 0 ) {
951 MLOCK(ErrorMessageLock);
952 MesPrint(
"(1)Output should fit inside a single term. Increase MaxTermSize?");
954 MUNLOCK(ErrorMessageLock);
955 retval = -1;
goto RetRetval;
958 t = newout->PObuffer;
960 jj = newout->POfill - t;
961 if ( AN.tryterm > 0 && ( (jj+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
962 to = TermMalloc(
"$-sort space");
966 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
967 allocsp = ((allocsp+7)/8)*8;
968 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
969 if ( AN.tryterm > 0 ) AN.tryterm = 0;
971 *((WORD **)buffer) = to;
975 j = newout->POfill - t;
977 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
979 if ( j > AM.MaxTer )
goto TooLarge;
987 MLOCK(ErrorMessageLock);
989 MUNLOCK(ErrorMessageLock);
990 retval = -1;
goto RetRetval;
993 pp = S->SizeInFile[1];
994 MULPOS(pp,
sizeof(WORD));
999 WORD oldLogHandle = AC.LogHandle;
1001 SETBASEPOSITION(pppp,0);
1002 SeekFile(S->file.handle,&pppp,SEEK_CUR);
1003 SeekFile(S->file.handle,&pp,SEEK_END);
1004 SeekFile(S->file.handle,&pppp,SEEK_SET);
1005 if ( AC.LogHandle >= 0 && AM.LogType ) AC.LogHandle = -1;
1007 AC.LogHandle = oldLogHandle;
1015 if ( newout->handle >= 0 )
goto TooLarge;
1016 t = newout->PObuffer;
1017 j = newout->POfill - t;
1019 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
1020 goto WorkSpaceError;
1021 if ( j > AM.MaxTer )
goto TooLarge;
1028 if ( S->file.handle >= 0 ) {
1030 MLOCK(ErrorMessageLock);
1031 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p"
1032 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1033 MUNLOCK(ErrorMessageLock);
1035 if ( S->lPatch <= 0 ) {
1037 position = S->fPatches[S->fPatchN];
1041 *AR.CompressPointer = 0;
1042 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
1043 S->fpcompressed[S->fPatchN] = 1;
1045 S->fpcompressed[S->fPatchN] = 0;
1046 SetupOutputGZIP(&(S->file));
1048 while ( ( t = *ss++ ) != 0 ) {
1049 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
1050 retval = -1;
goto RetRetval;
1053 if (
FlushOut(&position,&(S->file),1) ) {
1054 retval = -1;
goto RetRetval;
1057 S->fPatches[S->fPatchN] = position;
1060 MLOCK(ErrorMessageLock);
1061 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p"
1062 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
1063 MUNLOCK(ErrorMessageLock);
1069 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1070 if ( S->file.handle >= 0 ) {
1071 SynchFile(S->file.handle);
1077 MLOCK(ErrorMessageLock);
1079 MUNLOCK(ErrorMessageLock);
1080 retval = -1;
goto RetRetval;
1084 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
1086 pp = S->SizeInFile[0];
1087 MULPOS(pp,
sizeof(WORD));
1095 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
1096 Expressions[AR.CurExpr].counter = S->TermsLeft;
1097 Expressions[AR.CurExpr].size = pp;
1100 if ( AR.sLevel == 0 ) {
1101 Expressions[AR.CurExpr].counter = S->TermsLeft;
1102 Expressions[AR.CurExpr].size = pp;
1106 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
1110 ClearSortGZIP(&(S->file));
1112 CloseFile(S->file.handle);
1113 S->file.handle = -1;
1114 remove(S->file.name);
1116 MLOCK(ErrorMessageLock);
1117 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
1118 MUNLOCK(ErrorMessageLock);
1121 AR.outfile = oldoutfile;
1123 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
1128 DeAllocFileHandle(newout);
1132 else if ( newout ) {
1133 if ( newout->handle >= 0 ) {
1134 MLOCK(ErrorMessageLock);
1135 MesPrint(
"(2)Output should fit inside a single term. Increase MaxTermSize?");
1137 MUNLOCK(ErrorMessageLock);
1140 else if ( newout->POfill > newout->PObuffer ) {
1146 j = newout->POfill-newout->PObuffer;
1147 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
1148 goto WorkSpaceError;
1150 to = buffer; t = newout->PObuffer;
1151 while ( j-- > 0 ) *to++ = *t++;
1155 DeAllocFileHandle(newout);
1159 else if ( par == 2 ) {
1161 if ( retval == 0 ) {
1162 if ( newout->handle >= 0 ) {
1170 LOCK(newout->pthreadslock);
1172 SeekFile(newout->handle,&position,SEEK_END);
1174 SeekFile(newout->handle,&zeropos,SEEK_SET);
1175 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)*3
1176 ,
"$-buffer reading");
1177 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1178 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
1179 BASEPOSITION(position) ) {
1180 MLOCK(ErrorMessageLock);
1181 MesPrint(
"Error reading information for $ variable");
1182 MUNLOCK(ErrorMessageLock);
1183 M_free(to,
"$-buffer reading");
1187 *((WORD **)buffer) = to;
1188 retval /=
sizeof(WORD);
1191 UNLOCK(newout->pthreadslock);
1198 LONG wsiz = newout->POfill - newout->PObuffer;
1199 if ( AN.tryterm > 0 && ( (wsiz+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
1200 to = TermMalloc(
"$-sort space");
1203 LONG allocsp = wsiz+2;
1204 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
1205 allocsp = ((allocsp+7)/8)*8;
1206 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-buffer reading");
1207 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1209 *((WORD **)buffer) = to; t = newout->PObuffer;
1215 DeAllocFileHandle(newout);
1221 DeAllocFileHandle(newout);
1232 MLOCK(ErrorMessageLock);
1235 MUNLOCK(ErrorMessageLock);
1408 WORD i, *p, ret, *r, *rr, j, k, first;
1409 int dobracketindex = 0;
1412 if ( AT.SS != AT.S0 ) {
1416 if ( ( i = *term ) <= 0 )
return(0);
1418 ADDPOS(*position,i*
sizeof(WORD));
1421 if ( p >= fi->POstop ) {
1423 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1425 MLOCK(ErrorMessageLock);
1426 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1427 MUNLOCK(ErrorMessageLock);
1429 fi->
handle = (WORD)RetCode;
1430 PUTZERO(fi->filesize);
1431 PUTZERO(fi->POposition);
1440 MLOCK(ErrorMessageLock);
1441 MesPrint(
"Cannot create scratch file %s",fi->name);
1442 MUNLOCK(ErrorMessageLock);
1447 LOCK(fi->pthreadslock);
1449 if ( fi == AR.hidefile ) {
1450 LOCK(AS.inputslock);
1452 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1453 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1454 if ( fi == AR.hidefile ) {
1455 UNLOCK(AS.inputslock);
1458 UNLOCK(fi->pthreadslock);
1460 MLOCK(ErrorMessageLock);
1461 MesPrint(
"Write error during sort. Disk full?");
1462 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1463 fi->POsize,fi->
handle,&(fi->POposition));
1464 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1465 MUNLOCK(ErrorMessageLock);
1468 ADDPOS(fi->filesize,fi->POsize);
1470 ADDPOS(fi->POposition,fi->POsize);
1471 if ( fi == AR.hidefile ) {
1472 UNLOCK(AS.inputslock);
1475 UNLOCK(fi->pthreadslock);
1478 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1484 }
while ( --i > 0 );
1485 fi->POfull = fi->POfill = p;
1488 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1489 MLOCK(ErrorMessageLock);
1491 sprintf((
char *)(THRbuf),
"PutOut(%d)",AT.identity);
1492 PrintTerm(term,(
char *)(THRbuf));
1494 PrintTerm(term,
"PutOut");
1496 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1497 MesPrint(
"File %s, position %p",fi->name,position);
1498 MUNLOCK(ErrorMessageLock);
1501 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1502 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1503 r = rr = AR.CompressPointer;
1504 first = j = k = ret = 0;
1505 if ( ( i = *term ) != 0 ) {
1508 if ( fi == AR.outfile || fi == AR.hidefile ) {
1509 MLOCK(ErrorMessageLock);
1510 MesPrint(
"Ran into precompressed term");
1511 MUNLOCK(ErrorMessageLock);
1516 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1517 if ( dobracketindex ) {
1518 PutBracketInIndex(BHEAD term,position);
1524 WORD *polystop, *sa;
1528 while ( polystop < sa && *polystop != AR.PolyFun ) {
1529 polystop += polystop[1];
1531 if ( polystop < sa ) {
1532 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1533 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1534 i--; j--; k--; p++; r++;
1538 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1545 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1565 r[-(ABS(r[-1]))] = 0;
1566 if ( r >= AR.ComprTop ) {
1567 MLOCK(ErrorMessageLock);
1568 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1569 MUNLOCK(ErrorMessageLock);
1574 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1576 if ( dobracketindex ) {
1577 PutBracketInIndex(BHEAD term,position);
1583 WORD *polystop, *sa;
1587 while ( polystop < sa && *polystop != AR.PolyFun ) {
1588 polystop += polystop[1];
1590 if ( polystop < sa ) {
1591 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1592 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1593 i--; j--; k--; p++; r++;
1597 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1601 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1606 if ( AR.PolyFunType == 2 ) {
1608 tstop = term + *term;
1609 tstop -= ABS(tstop[-1]);
1611 while ( t < tstop ) {
1612 if ( *t == AR.PolyFun ) {
1613 t[2] &= ~MUSTCLEANPRF;
1618 if ( dobracketindex ) {
1619 PutBracketInIndex(BHEAD term,position);
1623 ADDPOS(*position,i*
sizeof(WORD));
1626 if ( p >= fi->POstop ) {
1628 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1630 sbuf->fill[sbuf->active] = fi->POstop;
1632 p = fi->PObuffer = fi->POfill = fi->POfull =
1633 sbuf->buff[sbuf->active];
1634 fi->POstop = sbuf->stop[sbuf->active];
1640 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1642 MLOCK(ErrorMessageLock);
1643 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1644 MUNLOCK(ErrorMessageLock);
1646 fi->
handle = (WORD)RetCode;
1647 PUTZERO(fi->filesize);
1648 PUTZERO(fi->POposition);
1657 MLOCK(ErrorMessageLock);
1658 MesPrint(
"Cannot create scratch file %s",fi->name);
1659 MUNLOCK(ErrorMessageLock);
1664 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1665 && dobracketindex == 0 && fi->zsp != 0 ) {
1667 if ( PutOutputGZIP(fi) )
return(-1);
1674 LOCK(fi->pthreadslock);
1676 if ( fi == AR.hidefile ) {
1677 LOCK(AS.inputslock);
1679 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1680 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1681 if ( fi == AR.hidefile ) {
1682 UNLOCK(AS.inputslock);
1685 UNLOCK(fi->pthreadslock);
1687 MLOCK(ErrorMessageLock);
1688 MesPrint(
"Write error during sort. Disk full?");
1689 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1690 fi->POsize,fi->
handle,&(fi->POposition));
1691 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1692 MUNLOCK(ErrorMessageLock);
1695 ADDPOS(fi->filesize,fi->POsize);
1697 ADDPOS(fi->POposition,fi->POsize);
1698 if ( fi == AR.hidefile ) {
1699 UNLOCK(AS.inputslock);
1702 UNLOCK(fi->pthreadslock);
1705 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1713 if ( first == 2 ) *p++ = k;
1717 else *p++ = *term++;
1723 }
while ( --i > 0 );
1724 fi->POfull = fi->POfill = p;
1752 int dobracketindex = 0;
1756 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1757 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1759 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1761 if ( fi->POfill >= fi->POstop ){
1762 sbuf->fill[sbuf->active] = fi->POstop;
1764 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1765 fi->POstop = sbuf->stop[sbuf->active];
1767 *(fi->POfill)++ = 0;
1768 sbuf->fill[sbuf->active] = fi->POfill;
1770 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1771 fi->POstop = sbuf->stop[sbuf->active];
1775 if ( fi->POfill >= fi->POstop ) {
1777 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1779 MLOCK(ErrorMessageLock);
1780 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1781 MUNLOCK(ErrorMessageLock);
1783 PUTZERO(fi->filesize);
1784 PUTZERO(fi->POposition);
1785 fi->
handle = (WORD)RetCode;
1794 MLOCK(ErrorMessageLock);
1795 MesPrint(
"Cannot create scratch file %s",fi->name);
1796 MUNLOCK(ErrorMessageLock);
1801 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1802 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1803 if ( PutOutputGZIP(fi) )
return(-1);
1804 fi->POfill = fi->PObuffer;
1810 LOCK(fi->pthreadslock);
1812 if ( fi == AR.hidefile ) {
1813 LOCK(AS.inputslock);
1815 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1816 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1818 UNLOCK(fi->pthreadslock);
1820 if ( fi == AR.hidefile ) {
1821 UNLOCK(AS.inputslock);
1823 MLOCK(ErrorMessageLock);
1824 MesPrint(
"Write error while sorting. Disk full?");
1825 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1826 fi->POsize,fi->
handle,&(fi->POposition));
1827 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1828 MUNLOCK(ErrorMessageLock);
1831 ADDPOS(fi->filesize,fi->POsize);
1832 fi->POfill = fi->PObuffer;
1833 ADDPOS(fi->POposition,fi->POsize);
1834 if ( fi == AR.hidefile ) {
1835 UNLOCK(AS.inputslock);
1838 UNLOCK(fi->pthreadslock);
1841 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1847 *(fi->POfill)++ = 0;
1848 fi->POfull = fi->POfill;
1861 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1864 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1865 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1866 if ( FlushOutputGZIP(fi) )
return(-1);
1867 fi->POfill = fi->PObuffer;
1873 LOCK(fi->pthreadslock);
1875 if ( fi == AR.hidefile ) {
1876 LOCK(AS.inputslock);
1878 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1882 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1884 UNLOCK(fi->pthreadslock);
1886 if ( fi == AR.hidefile ) {
1887 UNLOCK(AS.inputslock);
1889 MLOCK(ErrorMessageLock);
1890 MesPrint(
"Write error while finishing sorting. Disk full?");
1891 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1892 size,fi->
handle,&(fi->POposition));
1893 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1894 MUNLOCK(ErrorMessageLock);
1897 ADDPOS(fi->filesize,size);
1898 ADDPOS(fi->POposition,size);
1899 fi->POfill = fi->PObuffer;
1900 if ( fi == AR.hidefile ) {
1901 UNLOCK(AS.inputslock);
1904 UNLOCK(fi->pthreadslock);
1907 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1913 if ( dobracketindex ) {
1914 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1915 if ( b->indexfill > 0 ) {
1916 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1920 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1921 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1925 LOCK(fi->pthreadslock);
1927 SeekFile(fi->
handle,position,SEEK_END);
1929 UNLOCK(fi->pthreadslock);
1933 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1939 ADDPOS(*position,
sizeof(WORD));
2255 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2256 WORD tempterm[8+FUNHEAD];
2258 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2259 *m++ = 0; *m++ = 0; FILLARG(m)
2260 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2261 if ( s1[FUNHEAD] < 0 ) {
2262 if ( s2[FUNHEAD] < 0 ) {
2263 if ( s1[FUNHEAD] <= -FUNCTION ) {
2264 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2265 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2267 *m++ = 2; *m++ = 1; *m++ = 3;
2268 INSLENGTH(4+FUNHEAD)
2270 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2271 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2272 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2273 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2275 s2[FUNHEAD] = s1[FUNHEAD];
2278 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2280 *m++ = 1; *m++ = 1; *m++ = 3;
2281 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2283 *m++ = 1; *m++ = 1; *m++ = 3;
2284 INSLENGTH(8+2*FUNHEAD)
2286 else if ( s2[FUNHEAD] == -SYMBOL ) {
2287 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2288 *m++ = 1; *m++ = 1; *m++ = 3;
2289 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2291 *m++ = 1; *m++ = 1; *m++ = 3;
2292 INSLENGTH(12+FUNHEAD)
2296 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2297 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2299 *m++ = 1; *m++ = 1; *m++ = 3;
2300 INSLENGTH(8+FUNHEAD)
2303 else if ( s1[FUNHEAD] == -SYMBOL ) {
2304 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2305 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2306 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2307 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2311 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2312 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2313 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2314 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2315 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2316 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2317 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2321 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2322 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2323 *m++ = 1; *m++ = 1; *m++ = 3;
2324 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2326 *m++ = 1; *m++ = 1; *m++ = 3;
2327 INSLENGTH(12+FUNHEAD)
2331 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2332 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2333 *m++ = 1; *m++ = 1; *m++ = 3;
2338 if ( s2[FUNHEAD] <= -FUNCTION ) {
2340 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2341 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2343 *m++ = 1; *m++ = 1; *m++ = 3;
2344 INSLENGTH(8+FUNHEAD)
2346 else if ( s2[FUNHEAD] == -SYMBOL ) {
2348 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2349 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2350 *m++ = 1; *m++ = 1; *m++ = 3;
2355 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2356 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2357 else { i1 = (WORD)x1; i2 = 3; }
2358 if ( x1 && AN.ncmod != 0 ) {
2363 if ( Modulus(m) ) Terminate(-1);
2364 if ( *m == 0 ) w[1] = 0;
2366 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2384 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2391 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2404 if ( s1[FUNHEAD] <= -FUNCTION ) {
2405 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2407 *t++ = 1; *t++ = 1; *t++ = 3;
2409 else if ( s1[FUNHEAD] == -SYMBOL ) {
2410 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2411 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2412 *t++ = 1; *t++ = 1; *t++ = 3;
2415 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2416 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2432 tstop1 = s1 + s1[1];
2433 s1 += FUNHEAD+ARGHEAD;
2435 tstop2 = s2 + s2[1];
2436 s2 += FUNHEAD+ARGHEAD;
2440 oldPolyFlag = AT.SS->PolyFlag;
2441 AT.SS->PolyFlag = 0;
2442 while ( s1 < tstop1 && s2 < tstop2 ) {
2443 i1 = CompareTerms(s1,s2,(WORD)(-1));
2448 else if ( i1 < 0 ) {
2471 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2472 MLOCK(ErrorMessageLock);
2473 MesPrint(
"Addition of coefficients of PolyFun");
2474 MUNLOCK(ErrorMessageLock);
2484 *mm = WORDDIF(m,mm);
2485 if ( AN.ncmod != 0 ) {
2486 if ( Modulus(mm) ) Terminate(-1);
2493 while ( s1 < tstop1 ) *m++ = *s1++;
2494 while ( s2 < tstop2 ) *m++ = *s2++;
2495 w[1] = WORDDIF(m,w);
2496 w[FUNHEAD] = w[1] - FUNHEAD;
2497 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2498 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2499 else w[1] = FUNHEAD+2;
2500 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2503 AT.SS->PolyFlag = oldPolyFlag;
2540 WORD *stopper1, *stopper2, *t2;
2542 WORD *stopex1, *stopex2;
2545 WORD count = -1, localPoly, polyhit = -1;
2547 if ( AR.sLevel == 0 ) {
2551 if ( S->PolyFlag ) {
2559 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2560 S->PolyFlag = AR.PolyFunType;
2561 if ( AR.PolyFunType == 2 &&
2562 ( AR.PolyFunExp == 2 || AR.PolyFunExp == 3 ) ) S->PolyFlag = 1;
2564 else { localPoly = 0; }
2568 GETSTOP(term2,stopper2);
2571 while ( t1 < stopper1 && t2 < stopper2 ) {
2573 if ( *t1 == HAAKJE )
return(PREV(-1));
2574 if ( *t2 == HAAKJE )
return(PREV(1));
2575 if ( *t1 >= (FUNCTION-1) ) {
2576 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2577 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2578 if ( *t1 < FUNCTION )
return(PREV(1));
2579 if ( *t2 < FUNCTION )
return(PREV(-1));
2580 c1 = functions[*t1-FUNCTION].commute;
2581 c2 = functions[*t2-FUNCTION].commute;
2583 if ( c2 )
return(PREV(1));
2584 else return(PREV(*t2-*t1));
2587 if ( !c2 )
return(PREV(-1));
2588 else return(PREV(*t2-*t1));
2591 else return(PREV(*t2-*t1));
2598 if ( localPoly && c1 < FUNCTION ) {
2601 if ( c1 <= (FUNCTION-1)
2602 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec ) ) {
2603 if ( c1 == SYMBOL ) {
2604 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2605 && s1[-1] == 4 && s2[-1] == 4
2606 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2607 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2614 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2617 else if ( AR.SortType >= SORTPOWERFIRST ) {
2620 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2622 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2625 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2632 if ( AR.SortType==SORTLOWFIRST ) {
2633 return(PREV((s1[1]>0?-1:1)));
2636 return(PREV((s1[1]<0?-1:1)));
2641 if ( AR.SortType==SORTLOWFIRST ) {
2643 return(PREV((s1[1]<0?1:-1)));
2646 return(PREV((s2[1]<0?-1:1)));
2651 return(PREV((s1[1]<0?-1:1)));
2654 return(PREV((s2[1]<0?1:-1)));
2659 if ( *s1 != *s2 )
return(
2660 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2665 if ( AR.SortType==SORTLOWFIRST ) {
2666 return(PREV((s2[1]<0?-1:1)));
2669 return(PREV((s2[1]<0?1:-1)));
2673 else if ( c1 == DOTPRODUCT ) {
2674 if ( AR.SortType >= SORTPOWERFIRST ) {
2677 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2679 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2682 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2687 if ( s2 >= t2 )
return(PREV(1));
2688 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2690 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2692 if ( *s1 != *s2 )
return(
2693 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2696 if ( s2 < t2 )
return(PREV(-1));
2700 if ( s2 >= t2 )
return(PREV(1));
2701 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2704 if ( s2 < t2 )
return(PREV(-1));
2712 if ( localPoly && c1 == AR.PolyFun ) {
2714 if ( S->PolyFlag == 1 ) {
2716 if ( *s1 > 0 ) i1 = *s1;
2717 else if ( *s1 <= -FUNCTION ) i1 = 1;
2719 if ( *s2 > 0 ) i2 = *s2;
2720 else if ( *s2 <= -FUNCTION ) i2 = 1;
2722 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2727 WORD *u1, *u2, *ustop;
2729 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2736 ustop = u2 - ABS(u2[-1]);
2738 while ( u1 < ustop ) {
2739 if ( *u1 == INDEX )
goto NoPoly;
2746 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2753 ustop = u2 - ABS(u2[-1]);
2755 while ( u1 < ustop ) {
2756 if ( *u1 == INDEX )
goto NoPoly;
2763 S->PolyWise = WORDDIF(s1,term1);
2764 S->PolyWise -= FUNHEAD;
2770 S->PolyWise = localPoly = 0;
2773 else if ( AR.PolyFunType == 2 ) {
2774 WORD i1, i2, i1a, i2a;
2775 if ( *s1 > 0 ) i1 = *s1;
2776 else if ( *s1 <= -FUNCTION ) i1 = 1;
2778 if ( *s2 > 0 ) i2 = *s2;
2779 else if ( *s2 <= -FUNCTION ) i2 = 1;
2781 if ( s1[i1] > 0 ) i1a = s1[i1];
2782 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2784 if ( s2[i2] > 0 ) i2a = s2[i2];
2785 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2787 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2792 WORD *u1, *u2, *ustop;
2794 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2799 while ( u1 < s1+i1 ) {
2801 ustop = u2 - ABS(u2[-1]);
2803 while ( u1 < ustop ) {
2804 if ( *u1 == INDEX )
goto NoPoly;
2811 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2815 u1 = s1 +i1 + ARGHEAD;
2818 ustop = u2 - ABS(u2[-1]);
2820 while ( u1 < ustop ) {
2821 if ( *u1 == INDEX )
goto NoPoly;
2828 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2833 while ( u1 < s2+i2 ) {
2835 ustop = u2 - ABS(u2[-1]);
2837 while ( u1 < ustop ) {
2838 if ( *u1 == INDEX )
goto NoPoly;
2845 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2849 u1 = s2 + i2 + ARGHEAD;
2852 ustop = u2 - ABS(u2[-1]);
2854 while ( u1 < ustop ) {
2855 if ( *u1 == INDEX )
goto NoPoly;
2862 S->PolyWise = WORDDIF(s1,term1);
2863 S->PolyWise -= FUNHEAD;
2868 S->PolyWise = localPoly = 0;
2872 S->PolyWise = localPoly = 0;
2876 t1 = term1 + S->PolyWise;
2877 t2 = term2 + S->PolyWise;
2887 if ( s2 >= t2 )
return(PREV(-1));
2894 if ( AC.properorderflag ) {
2896 oldpolyflag = S->PolyFlag;
2898 if ( ( c2 = -CompArg(s1,s2) ) != 0 ) {
2899 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2901 S->PolyFlag = oldpolyflag;
2910 if ( s2 >= t2 )
return(PREV(-1));
2912 s1 += ARGHEAD; s2 += ARGHEAD;
2913 oldpolyflag = S->PolyFlag;
2915 while ( s1 < stopex1 ) {
2916 if ( s2 >= stopex2 ) {
2917 S->PolyFlag = oldpolyflag;
return(PREV(-1));
2919 if ( ( c2 = CompareTerms(s1,s2,(WORD)1) ) != 0 ) {
2920 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2925 S->PolyFlag = oldpolyflag;
2926 if ( s2 < stopex2 )
return(PREV(1));
2928 else return(PREV(1));
2931 if ( *s2 > 0 )
return(PREV(-1));
2932 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2933 if ( *s1 > -FUNCTION ) {
2934 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2940 if ( s2 < t2 )
return(PREV(1));
2944 if ( AR.SortType != SORTLOWFIRST ) {
2945 if ( t1 < stopper1 )
return(PREV(1));
2946 if ( t2 < stopper2 )
return(PREV(-1));
2949 if ( t1 < stopper1 )
return(PREV(-1));
2950 if ( t2 < stopper2 )
return(PREV(1));
2953 if ( level == 3 )
return(
CompCoef(term1,term2));
3581 WORD **poin, **poin2, ul, k, i, im, *m1;
3582 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3583 WORD *m2, *m3, r31, r33, ki, *rr;
3593 int oldgzipCompress = AR.gzipCompress;
3595 AR.gzipCompress = 0;
3599 fout = &(AR.FoStage4[0]);
3602 poin = S->poina; poin2 = S->poin2a;
3603 rr = AR.CompressPointer;
3610 if ( fout->
handle < 0 ) {
3612 PUTZERO(AN.OldPosOut);
3613 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3614 MLOCK(ErrorMessageLock);
3615 MesPrint(
"Cannot create file %s",fout->name);
3616 MUNLOCK(ErrorMessageLock);
3620 MLOCK(ErrorMessageLock);
3621 MesPrint(
"%w MergePatches created output file %s",fout->name);
3622 MUNLOCK(ErrorMessageLock);
3625 PUTZERO(fout->filesize);
3626 PUTZERO(fout->POposition);
3634 LOCK(fout->pthreadslock);
3636 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3638 UNLOCK(fout->pthreadslock);
3641 PUTZERO(S->fPatches[0]);
3642 fout->POfill = fout->PObuffer;
3643 PUTZERO(fout->POposition);
3648 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3649 S->fpcompressed[S->fPatchN] = 1;
3651 S->fpcompressed[S->fPatchN] = 0;
3652 SetupOutputGZIP(fout);
3655 else if ( par == 0 && S->stage4 > 0 ) {
3667 S->iPatches = S->fPatches;
3668 S->fPatches = S->inPatches;
3669 S->inPatches = S->iPatches;
3670 (S->inNum) = S->fPatchN;
3671 AN.OldPosIn = AN.OldPosOut;
3673 m1 = S->fpincompressed;
3674 S->fpincompressed = S->fpcompressed;
3675 S->fpcompressed = m1;
3676 for ( i = 0; i < S->inNum; i++ ) {
3677 S->fPatchesStop[i] = S->iPatches[i+1];
3679 MLOCK(ErrorMessageLock);
3680 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3681 MUNLOCK(ErrorMessageLock);
3693 AR.gzipCompress = 0;
3696 S->iPatches = S->fPatches;
3697 S->inNum = S->fPatchN;
3699 m1 = S->fpincompressed;
3700 S->fpincompressed = S->fpcompressed;
3701 S->fpcompressed = m1;
3702 for ( i = 0; i < S->inNum; i++ ) {
3703 S->fPatchesStop[i] = S->fPatches[i+1];
3705 MLOCK(ErrorMessageLock);
3706 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3707 MUNLOCK(ErrorMessageLock);
3715 S->Patches[S->lPatch] = S->lFill;
3716 for ( i = 0; i < S->lPatch; i++ ) {
3717 S->pStop[i] = S->Patches[i+1]-1;
3718 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3722 S->lPatch = (S->inNum);
3724 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3726 if ( S->lPatch > 1 ) {
3729 SetupAllInputGZIP(S);
3732 for ( i = 0; i < S->lPatch; i++ ) {
3733 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3735 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3736 S->pStop[i] = m2 = p;
3738 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3740 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3745 if ( fout->
handle >= 0 ) {
3748 LOCK(fout->pthreadslock);
3750 SeekFile(fout->
handle,&position,SEEK_END);
3751 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3753 UNLOCK(fout->pthreadslock);
3757 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3766 if ( S->lPatch == 1 ) {
3769 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3775 m2 = m1 = *S->Patches;
3778 i = -(*m1++); m2 += i; im = *m1+i+1;
3779 while ( i > 0 ) { *m1-- = *m2--; i--; }
3783 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD m1); }
3786 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3787 ADDPOS(S->SizeInFile[par],im);
3792 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3795 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3796 ADDPOS(S->SizeInFile[par],1);
3799 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3800 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3802 ADDPOS(position,length);
3803 ADDPOS(fout->POposition,length);
3804 ADDPOS(fout->filesize,length);
3805 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3815 #define FRONTSIZE (2*AM.MaxTer)
3816 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3818 SetupAllInputGZIP(S);
3820 position2 = S->iPatches[0];
3821 while ( ( length = FillInputGZIP(fin,&position2,
3823 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3824 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3825 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3826 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3834 i = -(*m1++); m2 += i; im = *m1+i+1;
3835 while ( i > 0 ) { *m1-- = *m2--; i--; }
3839 if ( AS.MasterSort && ( fout == AR.outfile ) ) {
3840 im = PutToMaster(BHEAD m1);
3844 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3845 ADDPOS(S->SizeInFile[par],im);
3849 if ( m1 < copytop && *m1 == 0 )
break;
3855 while ( m1 > m2 ) *--m3 = *--m1;
3860 MLOCK(ErrorMessageLock);
3861 MesPrint(
"Readerror");
3865 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
3868 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3869 ADDPOS(S->SizeInFile[par],1);
3872 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3873 while ( ( length = ReadFile(fin->
handle,
3874 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3875 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3877 ADDPOS(position,length);
3878 ADDPOS(fout->POposition,length);
3879 ADDPOS(fout->filesize,length);
3880 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3883 MLOCK(ErrorMessageLock);
3884 MesPrint(
"Readerror");
3891 else if ( S->lPatch > 0 ) {
3896 do { lpat *= 2; }
while ( lpat < S->lPatch );
3897 mpat = ( lpat >> 1 ) - 1;
3898 k = lpat - S->lPatch;
3903 for ( i = 1; i < lpat; i++ ) {
3906 for ( i = 1; i <= k; i++ ) {
3908 poin[im] = S->Patches[i-1];
3909 poin2[im] = poin[im] + *(poin[im]);
3912 S->tree[mpat+i] = 0;
3913 poin[im-1] = poin2[im-1] = 0;
3915 for ( i = (k*2)+1; i <= lpat; i++ ) {
3918 poin[i] = S->Patches[i-k-1];
3919 poin2[i] = poin[i] + *(poin[i]);
3939 if ( !*(poin[k]) ) {
3940 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3941 if ( S->tree[i] == -1 ) {
3954 if ( S->tree[i] > 0 ) {
3955 if ( ( c = CompareTerms(poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3959 S->used[level] = S->tree[i];
3969 l1 = *( m1 = poin[S->tree[i]] );
3970 l2 = *( m2 = poin[k] );
3971 if ( S->PolyWise ) {
3976 if ( S->PolyFlag == 2 ) {
3978 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3979 MLOCK(ErrorMessageLock);
3980 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3981 MUNLOCK(ErrorMessageLock);
3988 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3989 MLOCK(ErrorMessageLock);
3990 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3991 MUNLOCK(ErrorMessageLock);
3998 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
4000 if ( r1 == m1[1] ) {
4003 else if ( r1 < m1[1] ) {
4007 while ( --r1 >= 0 ) *--m1 = *--m2;
4010 while ( --r1 >= 0 ) *--m1 = *--m2;
4012 poin[S->tree[i]] = m1;
4020 poin[S->tree[i]] = m2;
4027 r1 = *( m1 += l1 - 1 );
4029 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
4030 r2 = *( m2 += l2 - 1 );
4032 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
4034 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
4035 MLOCK(ErrorMessageLock);
4036 MesCall(
"MergePatches");
4037 MUNLOCK(ErrorMessageLock);
4041 if ( AN.ncmod != 0 ) {
4042 if ( ( AC.modmode & POSNEG ) != 0 ) {
4045 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
4047 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
4049 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
4053 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
4054 if ( r3 < 0 ) r3 = -r3;
4055 if ( r1 < 0 ) r1 = -r1;
4060 ul = S->used[level] = S->tree[i];
4067 r1 = poin2[ul][1] - im + 1;
4069 m2 = poin[ul] - im + 1;
4070 while ( ++im <= 0 ) *--m1 = *--m2;
4075 poin[ul] = poin2[ul];
4077 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
4080 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
4082 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4083 S->Patches[ki],&(poin[ul]),ki));
4085 poin2[ul] = poin[ul] + im;
4090 S->used[++level] = k;
4096 else if ( r31 < 0 ) {
4105 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
4111 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
4112 MLOCK(ErrorMessageLock);
4113 MesPrint(
"Coefficient overflow during sort");
4114 MUNLOCK(ErrorMessageLock);
4117 m2 = poin[S->tree[i]];
4118 m3 = ( poin[S->tree[i]] -= r31 );
4119 do { *m3++ = *m2++; }
while ( m2 < m1 );
4123 *(poin[S->tree[i]]) += r31;
4125 m2 = (WORD *)coef; im = r3;
4136 r1 = poin2[k][1] - im + 1;
4138 m2 = poin[k] - im + 1;
4139 while ( ++im <= 0 ) *--m1 = *--m2;
4146 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
4149 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
4151 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4152 S->Patches[ki],&(poin[k]),ki));
4154 poin2[k] = poin[k] + im;
4162 else if ( S->tree[i] < 0 ) {
4173 if ( AS.MasterSort && ( fout == AR.outfile ) ) { im = PutToMaster(BHEAD poin[k]); }
4176 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
4177 MLOCK(ErrorMessageLock);
4178 MesPrint(
"Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
4179 MUNLOCK(ErrorMessageLock);
4182 ADDPOS(S->SizeInFile[par],im);
4190 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
4193 if (
FlushOut(&position,fout,1) )
goto ReturnError;
4194 ADDPOS(S->SizeInFile[par],1);
4198 SeekFile(fout->
handle,&position,SEEK_CUR);
4201 S->fPatches[S->fPatchN] = position;
4203 if ( par == 0 && fout != AR.outfile ) {
4212 S->fPatches[S->fPatchN] = position;
4213 if ( ISNOTZEROPOS(AN.OldPosIn) ) {
4215 SeekFile(fin->
handle,&(AN.OldPosIn),SEEK_SET);
4221 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
4223 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&AN.OldPosIn),(LONG)
sizeof(POSITION)) !=
4225 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
4226 *
sizeof(POSITION)) != ((S->inNum)+1)*
sizeof(POSITION) ) {
4227 MLOCK(ErrorMessageLock);
4228 MesPrint(
"Read error fourth stage sorting");
4229 MUNLOCK(ErrorMessageLock);
4234 for ( i = 0; i < S->inNum; i++ ) {
4235 S->fPatchesStop[i] = S->iPatches[i+1];
4237 MLOCK(ErrorMessageLock);
4238 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
4239 MUNLOCK(ErrorMessageLock);
4261 MLOCK(ErrorMessageLock);
4262 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4263 MUNLOCK(ErrorMessageLock);
4273 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4274 PUTZERO(S->SizeInFile[0]);
4288 MLOCK(ErrorMessageLock);
4289 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4290 MUNLOCK(ErrorMessageLock);
4295 AR.gzipCompress = oldgzipCompress;
4300 AR.gzipCompress = oldgzipCompress;
4305 MLOCK(ErrorMessageLock);
4306 MesPrint(
"Error while writing to file.");
4310 MLOCK(ErrorMessageLock);
4312 MesCall(
"MergePatches");
4313 MUNLOCK(ErrorMessageLock);
4315 AR.gzipCompress = oldgzipCompress;