147 if ( AT.SS == AT.S0 && AC.StatsFlag ) {
149 if ( AC.ThreadStats == 0 && identity > 0 )
return;
150#elif defined(WITHMPI)
151 if ( AC.OldParallelStats )
return;
152 if ( ! AC.ProcessStats && PF.me != MASTER )
return;
154 if ( Expressions == 0 )
return;
156 if ( par == STATSSPLITMERGE ) {
157 if ( AC.ShortStatsMax == 0 )
return;
159 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
161 AR.ShortSortCount = 0;
165 char humanGenTermsText[HUMANSTRLEN] =
"";
166 char humanTermsLeftText[HUMANSTRLEN] =
"";
167 char humanBytesText[HUMANSTRLEN] =
"";
168 char humanUnsortedBytesText[HUMANSTRLEN] =
"";
169 char humanComparisonsText[HUMANSTRLEN] =
"";
170 char humanMaxTermSizeText[HUMANSTRLEN] =
"";
171 if ( AC.HumanStatsFlag ) {
172 HumanString(humanGenTermsText, (
float)(S->GenTerms), 1000.0f, humanTermsSuffix);
173 HumanString(humanTermsLeftText, (
float)(S->TermsLeft), 1000.0f, humanTermsSuffix);
174 HumanString(humanBytesText, (
float)(BASEPOSITION(*plspace)), 1024.0f, humanBytesSuffix);
175 HumanString(humanUnsortedBytesText, (
float)(S->verbUnsortedSize), 1024.0f, humanBytesSuffix);
176 HumanString(humanComparisonsText, (
float)(S->verbComparisons), 1000.0f, humanTermsSuffix);
177 HumanString(humanMaxTermSizeText, (
float)(S->verbMaxTermSize), 1000.0f, humanTermsSuffix);
180 MLOCK(ErrorMessageLock);
185 const WORD oldLogHandle = AC.LogHandle;
186 if ( checkLogType && AM.LogType ) {
190 if ( AC.ShortStats ) {}
193 if ( identity > 0 ) {
194 MesPrint(
" Thread %d reporting",identity);
199#elif defined(WITHMPI)
200 if ( PF.me != MASTER ) {
201 MesPrint(
" Process %d reporting",PF.me);
216 use_wtime = AC.WTimeStatsFlag;
217#if defined(WITHPTHREADS)
218 if ( use_wtime && identity > 0 ) use_wtime = 0;
219#elif defined(WITHMPI)
220 if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
222 char *wpref = use_wtime ?
"W" :
"";
223 char *wspac = use_wtime ?
"" :
" ";
225 timepart = (UWORD)(millitime%1000);
229 if ( AC.ShortStats ) {
230#if defined(WITHPTHREADS) || defined(WITHMPI)
232 if ( identity > 0 ) {
234 if ( PF.me != MASTER ) {
235 const int identity = PF.me;
237 if ( par == STATSSPLITMERGE || par == STATSPOSTSORT ) {
238 snprintf(buf,
sizeof(buf),
239 "%d: %7ld.%02us %8ld>%10ld%3s%10ld:%10ld %s %s",identity,
240 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
241 S->TermsLeft,BASEPOSITION(*plspace),EXPRNAME(AR.CurExpr),
245 else if ( par == STATSMERGETOFILE ) {
246 snprintf(buf,
sizeof(buf),
247 "%d: %7ld.%02us %10ld:%10ld",identity,millitime,timepart,
248 S->TermsLeft,BASEPOSITION(*plspace));
255 if ( par == STATSSPLITMERGE || par == STATSPOSTSORT ) {
256 snprintf(buf,
sizeof(buf),
257 "%7ld.%02us %8ld>%10ld%3s%10ld:%10ld %s %s",
258 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
259 S->TermsLeft,BASEPOSITION(*plspace),EXPRNAME(AR.CurExpr),
263 else if ( par == STATSMERGETOFILE ) {
264 snprintf(buf,
sizeof(buf),
265 "%7ld.%02us %10ld:%10ld",millitime,timepart,
266 S->TermsLeft,BASEPOSITION(*plspace));
272 if ( par == STATSMERGETOFILE ) {
273 snprintf(buf,
sizeof(buf),
274 "%sTime = %7ld.%02u sec",wpref,millitime,timepart);
278 snprintf(buf,
sizeof(buf),
279 "%sTime = %7ld.%02u sec %sGenerated terms =%11ld%s",
280 wpref, millitime, timepart, wspac, S->GenTerms, humanGenTermsText);
284 const int exprlen = strlen((
char*)EXPRNAME(AR.CurExpr));
285 const int overflow = MaX(0, exprlen - COL_EXP);
286 if ( par == STATSSPLITMERGE ) {
287 int width = snprintf(buf,
sizeof(buf),
289 COL_EXP, EXPRNAME(AR.CurExpr),
290 MaX(0,COL_SPA-1-overflow), AN.ninterms, FG.swmes[par]);
291 width += snprintf(buf+width,
sizeof(buf)-width,
293 MaX(0,COL_EQU-1-width),
"");
294 snprintf(buf+width,
sizeof(buf)-width,
296 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
301 if ( identity > 0 && par == STATSPOSTSORT ) {
302 int width = snprintf(buf,
sizeof(buf),
303 "%*s%*s Terms in thread",
304 COL_EXP, EXPRNAME(AR.CurExpr), MaX(0,COL_SPA-overflow),
"");
305 width += snprintf(buf+width,
sizeof(buf)-width,
307 MaX(0,COL_EQU-1-width),
"");
308 snprintf(buf+width,
sizeof(buf)-width,
310 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
314#elif defined(WITHMPI)
315 if ( PF.me != MASTER && par == STATSPOSTSORT ) {
316 int width = snprintf(buf,
sizeof(buf),
317 "%*s%*s Terms in process=",
318 COL_EXP, EXPRNAME(AR.CurExpr), MaX(0,COL_SPA-overflow),
"");
319 snprintf(buf+width,
sizeof(buf)-width,
321 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
327 int width = snprintf(buf,
sizeof(buf),
329 COL_EXP, EXPRNAME(AR.CurExpr), MaX(0,COL_SPA-overflow),
"",
331 width += snprintf(buf+width,
sizeof(buf)-width,
333 MaX(0,COL_EQU-1-width),
"");
334 snprintf(buf+width,
sizeof(buf)-width,
336 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
341 const WORD dig = DigitsIn(BASEPOSITION(*plspace));
342 snprintf(buf,
sizeof(buf),
343 "%*s Bytes used%*s=%11ld%s",
344 COL_EXP+COL_SPA, AC.Commercial, MiN(6,17-dig),
"",
345 BASEPOSITION(*plspace), humanBytesText);
349 if ( par == STATSPOSTSORT ) {
350 if ( AC.SortVerbose ) {
351 snprintf(buf,
sizeof(buf),
"%*s Unsorted bytes =%11ld%s",
352 COL_EXP+COL_SPA,
"", S->verbUnsortedSize, humanUnsortedBytesText);
354 snprintf(buf,
sizeof(buf),
"%*s Small Buffer =%5ld,%5ld",
355 COL_EXP+COL_SPA,
"", S->verbSBsortTerms, S->verbSBsortCap);
357 snprintf(buf,
sizeof(buf),
"%*s Large Buffer =%5ld,%5ld",
358 COL_EXP+COL_SPA,
"", S->verbLBsortPatches, S->verbLBsortCap);
360 snprintf(buf,
sizeof(buf),
"%*s Comparisons =%11ld%s",
361 COL_EXP+COL_SPA,
"", S->verbComparisons, humanComparisonsText);
363 snprintf(buf,
sizeof(buf),
"%24s Largest Term =%11ld%s",
364 "",S->verbMaxTermSize,humanMaxTermSizeText);
370 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l"
371 ,numwrites,numreads,numseeks);
372 MesPrint(
"Total number of mallocs: %l, frees: %l"
373 ,nummallocs,numfrees);
377 AC.LogHandle = oldLogHandle;
379 MUNLOCK(ErrorMessageLock);
492 WORD j, **ss, *to, *t;
493 LONG sSpace, over, tover, spare, retval = 0;
496 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
498 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
501 oldoutfile = AR.outfile;
511 oldoutfile = AR.outfile;
530 PrintTime((UBYTE *)
"EndSort, before SplitMerge");
532 S->sPointer[
SplitMerge(BHEAD S->sPointer,S->sTerms)] = 0;
534 PrintTime((UBYTE *)
"Endsort, after SplitMerge");
537 tover = over = S->sTerms;
540 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
543 S->TermsLeft -= over - spare;
544 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
546 else if ( S != AT.S0 ) {
550 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
551 if ( AN.tryterm > 0 && ( (sSpace+1)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
552 to = TermMalloc(
"$-sort space");
555 LONG allocsp = sSpace+1;
556 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
557 allocsp = ((allocsp+7)/8)*8;
558 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
559 if ( AN.tryterm > 0 ) AN.tryterm = 0;
561 *((WORD **)buffer) = to;
563 while ( ( t = *ss++ ) != 0 ) {
564 j = *t;
while ( --j >= 0 ) *to++ = *t++;
572 while ( ( t = *ss++ ) != 0 ) {
574 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
576 while ( ( t = *ss++ ) != 0 ) {
579 MLOCK(ErrorMessageLock);
580 MesPrint(
"Sorted function argument too long (%d words). Increase MaxTermSize (%l words).", sSpace, AM.MaxTer/((LONG)
sizeof(WORD)));
581 MUNLOCK(ErrorMessageLock);
582 retval = -1;
goto RetRetval;
584 while ( --j >= 0 ) *to++ = *t++;
587 retval = to - buffer;
595 *AR.CompressPointer = 0;
596 SeekScratch(AR.outfile,&position);
608 {
int oldgzipCompress = AR.gzipCompress;
613 while ( ( t = *ss++ ) != 0 ) {
614 if ( *t ) S->TermsLeft++;
616 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
619 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
620 retval = -1;
goto RetRetval;
625 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
629 retval = -1;
goto RetRetval;
632 AR.gzipCompress = oldgzipCompress;
636 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
639 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
641 DIFPOS(oldpos,position,oldpos);
642 S->SpaceLeft = BASEPOSITION(oldpos);
643 WriteStats(&oldpos,STATSPOSTSORT,NOCHECKLOGTYPE);
648 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
650 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
653 SETBASEPOSITION(pp,lSpace);
654 MULPOS(pp,
sizeof(WORD));
655 if ( S->file.handle >= 0 ) {
656 ADD2POS(pp,S->fPatches[S->fPatchN]);
659 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
663 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
664 if ( S->lPatch > 0 ) {
665 if ( ( S->lPatch >= S->MaxPatches ) ||
666 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
671 if ( S->lPatch >= S->MaxPatches ) S->verbLBsortPatches++;
672 else S->verbLBsortCap++;
675 MLOCK(ErrorMessageLock);
676 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x"
677 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
678 MUNLOCK(ErrorMessageLock);
682 MLOCK(ErrorMessageLock);
684 MUNLOCK(ErrorMessageLock);
685 retval = -1;
goto RetRetval;
688 pp = S->SizeInFile[1];
689 MULPOS(pp,
sizeof(WORD));
695 SETBASEPOSITION(pppp,0);
696 SeekFile(S->file.handle,&pppp,SEEK_CUR);
697 SeekFile(S->file.handle,&pp,SEEK_END);
698 SeekFile(S->file.handle,&pppp,SEEK_SET);
699 WriteStats(&pp,STATSMERGETOFILE,CHECKLOGTYPE);
704 S->Patches[S->lPatch++] = S->lFill;
705 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
708 while ( ( t = *ss++ ) != 0 ) {
710 if ( j < 0 ) j = t[1] + 2;
711 while ( --j >= 0 ) *to++ = *t++;
716 if ( S->file.handle < 0 ) {
718 MLOCK(ErrorMessageLock);
720 MUNLOCK(ErrorMessageLock);
721 retval = -1;
goto RetRetval;
724 pp = S->SizeInFile[2];
725 MULPOS(pp,
sizeof(WORD));
727 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
733 if ( par == 2 && newout->handle >= 0 ) {
737 LOCK(newout->pthreadslock);
739 SeekFile(newout->handle,&zeropos,SEEK_SET);
740 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+
sizeof(WORD)*2
741 ,
"$-buffer reading");
742 if ( AN.tryterm > 0 ) AN.tryterm = 0;
743 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
744 BASEPOSITION(newout->filesize) ) {
746 MLOCK(ErrorMessageLock);
747 MesPrint(
"!>Error reading information for $ variable");
748 MUNLOCK(ErrorMessageLock);
749 M_free(to,
"$-buffer reading");
754 *((WORD **)buffer) = to;
755 retval /=
sizeof(WORD);
758 UNLOCK(newout->pthreadslock);
761 else if ( newout->handle >= 0 ) {
768 MLOCK(ErrorMessageLock);
769 MesPrint(
"(1)Output should fit inside a single term. Increase MaxTermSize?");
771 MUNLOCK(ErrorMessageLock);
772 retval = -1;
goto RetRetval;
775 t = newout->PObuffer;
778 j = newout->POfill - t;
780 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
782 if ( j > AM.MaxTer ) {
784 MLOCK(ErrorMessageLock);
785 MesPrint(
"!>Encountered term of size: %d words.", j/(LONG)
sizeof(WORD) );
786 MUNLOCK(ErrorMessageLock);
791 retval = to - buffer - 1;
798 MLOCK(ErrorMessageLock);
800 MUNLOCK(ErrorMessageLock);
801 retval = -1;
goto RetRetval;
804 pp = S->SizeInFile[1];
805 MULPOS(pp,
sizeof(WORD));
811 SETBASEPOSITION(pppp,0);
812 SeekFile(S->file.handle,&pppp,SEEK_CUR);
813 SeekFile(S->file.handle,&pp,SEEK_END);
814 SeekFile(S->file.handle,&pppp,SEEK_SET);
815 WriteStats(&pp,STATSMERGETOFILE,CHECKLOGTYPE);
823 if ( newout->handle >= 0 )
goto TooLarge;
824 t = newout->PObuffer;
825 j = newout->POfill - t;
827 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
829 if ( j > AM.MaxTer )
goto TooLarge;
836 if ( S->file.handle >= 0 ) {
838 MLOCK(ErrorMessageLock);
839 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p"
840 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
841 MUNLOCK(ErrorMessageLock);
843 if ( S->lPatch <= 0 ) {
845 position = S->fPatches[S->fPatchN];
848 *AR.CompressPointer = 0;
850 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
851 S->fpcompressed[S->fPatchN] = 1;
853 S->fpcompressed[S->fPatchN] = 0;
854 SetupOutputGZIP(&(S->file));
856 while ( ( t = *ss++ ) != 0 ) {
857 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
858 retval = -1;
goto RetRetval;
861 if (
FlushOut(&position,&(S->file),1) ) {
862 retval = -1;
goto RetRetval;
865 S->fPatches[S->fPatchN] = position;
868 MLOCK(ErrorMessageLock);
869 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p"
870 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
871 MUNLOCK(ErrorMessageLock);
877 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
878 if ( S->file.handle >= 0 ) {
879 SynchFile(S->file.handle);
885 MLOCK(ErrorMessageLock);
887 MUNLOCK(ErrorMessageLock);
888 retval = -1;
goto RetRetval;
892 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
894 pp = S->SizeInFile[0];
895 MULPOS(pp,
sizeof(WORD));
903 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
904 Expressions[AR.CurExpr].counter = S->TermsLeft;
905 Expressions[AR.CurExpr].size = pp;
908 if ( AR.sLevel == 0 ) {
909 Expressions[AR.CurExpr].counter = S->TermsLeft;
910 Expressions[AR.CurExpr].size = pp;
914 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
918 ClearSortGZIP(&(S->file));
920 CloseFile(S->file.handle);
922 remove(S->file.name);
924 MLOCK(ErrorMessageLock);
925 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
926 MUNLOCK(ErrorMessageLock);
929 AR.outfile = oldoutfile;
931 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
936 DeAllocFileHandle(newout);
941 if ( newout->handle >= 0 ) {
942 MLOCK(ErrorMessageLock);
943 MesPrint(
"(2)Output should fit inside a single term. Increase MaxTermSize?");
945 MUNLOCK(ErrorMessageLock);
948 else if ( newout->POfill > newout->PObuffer ) {
954 j = newout->POfill-newout->PObuffer;
955 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
958 to = buffer; t = newout->PObuffer;
959 while ( j-- > 0 ) *to++ = *t++;
963 DeAllocFileHandle(newout);
967 else if ( par == 2 ) {
970 if ( newout->handle >= 0 ) {
978 LOCK(newout->pthreadslock);
980 SeekFile(newout->handle,&position,SEEK_END);
982 SeekFile(newout->handle,&zeropos,SEEK_SET);
983 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)*3
984 ,
"$-buffer reading");
985 if ( AN.tryterm > 0 ) AN.tryterm = 0;
986 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
987 BASEPOSITION(position) ) {
989 MLOCK(ErrorMessageLock);
990 MesPrint(
"!>Error reading information for $ variable");
991 MUNLOCK(ErrorMessageLock);
992 M_free(to,
"$-buffer reading");
997 *((WORD **)buffer) = to;
998 retval /=
sizeof(WORD);
1001 UNLOCK(newout->pthreadslock);
1008 LONG wsiz = newout->POfill - newout->PObuffer;
1009 if ( AN.tryterm > 0 && ( (wsiz+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
1010 to = TermMalloc(
"$-sort space");
1013 LONG allocsp = wsiz+2;
1014 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
1015 allocsp = ((allocsp+7)/8)*8;
1016 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-buffer reading");
1017 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1019 *((WORD **)buffer) = to; t = newout->PObuffer;
1025 DeAllocFileHandle(newout);
1031 DeAllocFileHandle(newout);
1038 MLOCK(ErrorMessageLock);
1041 MUNLOCK(ErrorMessageLock);
1220 WORD i, *p, ret, *r, *rr, j, k, first;
1221 int dobracketindex = 0;
1224 if ( AT.SS != AT.S0 ) {
1228 if ( ( i = *term ) <= 0 )
return(0);
1230 ADDPOS(*position,i*
sizeof(WORD));
1233 if ( p >= fi->POstop ) {
1235 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1237 MLOCK(ErrorMessageLock);
1238 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1239 MUNLOCK(ErrorMessageLock);
1241 fi->
handle = (WORD)RetCode;
1242 PUTZERO(fi->filesize);
1243 PUTZERO(fi->POposition);
1252 MLOCK(ErrorMessageLock);
1253 MesPrint(
"Cannot create scratch file %s",fi->name);
1254 MUNLOCK(ErrorMessageLock);
1259 LOCK(fi->pthreadslock);
1261 if ( fi == AR.hidefile ) {
1262 LOCK(AS.inputslock);
1264 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1265 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1266 if ( fi == AR.hidefile ) {
1267 UNLOCK(AS.inputslock);
1270 UNLOCK(fi->pthreadslock);
1272 MLOCK(ErrorMessageLock);
1273 MesPrint(
"Write error during sort. Disk full?");
1274 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1275 fi->POsize,fi->
handle,&(fi->POposition));
1276 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1277 MUNLOCK(ErrorMessageLock);
1280 ADDPOS(fi->filesize,fi->POsize);
1282 ADDPOS(fi->POposition,fi->POsize);
1283 if ( fi == AR.hidefile ) {
1284 UNLOCK(AS.inputslock);
1287 UNLOCK(fi->pthreadslock);
1290 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1296 }
while ( --i > 0 );
1297 fi->POfull = fi->POfill = p;
1300 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1301 MLOCK(ErrorMessageLock);
1303 snprintf((
char *)(THRbuf),100,
"PutOut(%d)",AT.identity);
1304 PrintTerm(term,(
char *)(THRbuf));
1306 PrintTerm(term,
"PutOut");
1308 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1309 MesPrint(
"File %s, position %p",fi->name,position);
1310 MUNLOCK(ErrorMessageLock);
1313 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1314 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1315 r = rr = AR.CompressPointer;
1316 first = j = k = ret = 0;
1317 if ( ( i = *term ) != 0 ) {
1320 if ( fi == AR.outfile || fi == AR.hidefile ) {
1322 MLOCK(ErrorMessageLock);
1323 MesPrint(
"!>Ran into precompressed term");
1324 MUNLOCK(ErrorMessageLock);
1330 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1331 if ( dobracketindex ) {
1332 PutBracketInIndex(BHEAD term,position);
1338 WORD *polystop, *sa;
1342 while ( polystop < sa && *polystop != AR.PolyFun ) {
1343 polystop += polystop[1];
1345 if ( polystop < sa ) {
1346 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1347 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1348 i--; j--; k--; p++; r++;
1352 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1356 else if ( AT.aux_ != 0 ) {
1357 WORD *floatstop, *sa;
1361 while ( floatstop < sa && *floatstop != FLOATFUN ) {
1362 floatstop += floatstop[1];
1364 if ( floatstop < sa ) {
1365 while ( i > 0 && j > 0 && *p == *r && p < floatstop ) {
1366 i--; j--; k--; p++; r++;
1370 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1378 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1398 r[-(ABS(r[-1]))] = 0;
1399 if ( r >= AR.ComprTop ) {
1400 MLOCK(ErrorMessageLock);
1401 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1402 MUNLOCK(ErrorMessageLock);
1407 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1409 if ( dobracketindex ) {
1410 PutBracketInIndex(BHEAD term,position);
1416 WORD *polystop, *sa;
1420 while ( polystop < sa && *polystop != AR.PolyFun ) {
1421 polystop += polystop[1];
1423 if ( polystop < sa ) {
1424 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1425 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1426 i--; j--; k--; p++; r++;
1430 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1434 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1439 if ( AR.PolyFunType == 2 ) {
1441 tstop = term + *term;
1442 tstop -= ABS(tstop[-1]);
1444 while ( t < tstop ) {
1445 if ( *t == AR.PolyFun ) {
1446 t[2] &= ~MUSTCLEANPRF;
1451 if ( dobracketindex ) {
1452 PutBracketInIndex(BHEAD term,position);
1456 ADDPOS(*position,i*
sizeof(WORD));
1459 if ( p >= fi->POstop ) {
1461 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1463 sbuf->fill[sbuf->active] = fi->POstop;
1465 p = fi->PObuffer = fi->POfill = fi->POfull =
1466 sbuf->buff[sbuf->active];
1467 fi->POstop = sbuf->stop[sbuf->active];
1473 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1475 MLOCK(ErrorMessageLock);
1476 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1477 MUNLOCK(ErrorMessageLock);
1479 fi->
handle = (WORD)RetCode;
1480 PUTZERO(fi->filesize);
1481 PUTZERO(fi->POposition);
1490 MLOCK(ErrorMessageLock);
1491 MesPrint(
"Cannot create scratch file %s",fi->name);
1492 MUNLOCK(ErrorMessageLock);
1497 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1498 && dobracketindex == 0 && fi->zsp != 0 ) {
1500 if ( PutOutputGZIP(fi) )
return(-1);
1507 LOCK(fi->pthreadslock);
1509 if ( fi == AR.hidefile ) {
1510 LOCK(AS.inputslock);
1512 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1513 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1514 if ( fi == AR.hidefile ) {
1515 UNLOCK(AS.inputslock);
1518 UNLOCK(fi->pthreadslock);
1520 MLOCK(ErrorMessageLock);
1521 MesPrint(
"Write error during sort. Disk full?");
1522 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1523 fi->POsize,fi->
handle,&(fi->POposition));
1524 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1525 MUNLOCK(ErrorMessageLock);
1528 ADDPOS(fi->filesize,fi->POsize);
1530 ADDPOS(fi->POposition,fi->POsize);
1531 if ( fi == AR.hidefile ) {
1532 UNLOCK(AS.inputslock);
1535 UNLOCK(fi->pthreadslock);
1538 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1546 if ( first == 2 ) *p++ = k;
1550 else *p++ = *term++;
1556 }
while ( --i > 0 );
1557 fi->POfull = fi->POfill = p;
1585 int dobracketindex = 0;
1589 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1590 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1592 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1594 if ( fi->POfill >= fi->POstop ){
1595 sbuf->fill[sbuf->active] = fi->POstop;
1597 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1598 fi->POstop = sbuf->stop[sbuf->active];
1600 *(fi->POfill)++ = 0;
1601 sbuf->fill[sbuf->active] = fi->POfill;
1603 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1604 fi->POstop = sbuf->stop[sbuf->active];
1608 if ( fi->POfill >= fi->POstop ) {
1610 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1612 MLOCK(ErrorMessageLock);
1613 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1614 MUNLOCK(ErrorMessageLock);
1616 PUTZERO(fi->filesize);
1617 PUTZERO(fi->POposition);
1618 fi->
handle = (WORD)RetCode;
1627 MLOCK(ErrorMessageLock);
1628 MesPrint(
"Cannot create scratch file %s",fi->name);
1629 MUNLOCK(ErrorMessageLock);
1634 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1635 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1636 if ( PutOutputGZIP(fi) )
return(-1);
1637 fi->POfill = fi->PObuffer;
1643 LOCK(fi->pthreadslock);
1645 if ( fi == AR.hidefile ) {
1646 LOCK(AS.inputslock);
1648 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1649 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1651 UNLOCK(fi->pthreadslock);
1653 if ( fi == AR.hidefile ) {
1654 UNLOCK(AS.inputslock);
1656 MLOCK(ErrorMessageLock);
1657 MesPrint(
"Write error while sorting. Disk full?");
1658 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1659 fi->POsize,fi->
handle,&(fi->POposition));
1660 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1661 MUNLOCK(ErrorMessageLock);
1664 ADDPOS(fi->filesize,fi->POsize);
1665 fi->POfill = fi->PObuffer;
1666 ADDPOS(fi->POposition,fi->POsize);
1667 if ( fi == AR.hidefile ) {
1668 UNLOCK(AS.inputslock);
1671 UNLOCK(fi->pthreadslock);
1674 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1680 *(fi->POfill)++ = 0;
1681 fi->POfull = fi->POfill;
1694 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1697 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1698 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1699 if ( FlushOutputGZIP(fi) )
return(-1);
1700 fi->POfill = fi->PObuffer;
1706 LOCK(fi->pthreadslock);
1708 if ( fi == AR.hidefile ) {
1709 LOCK(AS.inputslock);
1711 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1715 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1717 UNLOCK(fi->pthreadslock);
1719 if ( fi == AR.hidefile ) {
1720 UNLOCK(AS.inputslock);
1722 MLOCK(ErrorMessageLock);
1723 MesPrint(
"Write error while finishing sorting. Disk full?");
1724 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1725 size,fi->
handle,&(fi->POposition));
1726 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1727 MUNLOCK(ErrorMessageLock);
1730 ADDPOS(fi->filesize,size);
1731 ADDPOS(fi->POposition,size);
1732 fi->POfill = fi->PObuffer;
1733 if ( fi == AR.hidefile ) {
1734 UNLOCK(AS.inputslock);
1737 UNLOCK(fi->pthreadslock);
1740 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1746 if ( dobracketindex ) {
1747 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1748 if ( b->indexfill > 0 ) {
1749 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1753 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1754 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1758 LOCK(fi->pthreadslock);
1760 SeekFile(fi->
handle,position,SEEK_END);
1762 UNLOCK(fi->pthreadslock);
1766 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1772 ADDPOS(*position,
sizeof(WORD));
1930 WORD *s1, *s2, *m, *w, *t, oldpw = S->PolyWise;
1931 s1 = *ps1 + S->PolyWise;
1932 s2 = *ps2 + S->PolyWise;
1937 if ( S->PolyFlag == 2 && AR.PolyFunExp != 2 && AR.PolyFunExp != 3 ) {
1938 WORD **oldSplitScratch = AN.SplitScratch;
1939 LONG oldSplitScratchSize = AN.SplitScratchSize;
1940 LONG oldInScratch = AN.InScratch;
1941 WORD oldtype = AR.SortType;
1942 if ( (WORD *)((UBYTE *)w + AM.MaxTer) >= AT.WorkTop ) {
1943 MLOCK(ErrorMessageLock);
1944 MesPrint(
"Program was adding polyratfun arguments");
1946 MUNLOCK(ErrorMessageLock);
1948 AR.SortType = SORTHIGHFIRST;
1950 AN.SplitScratch = AN.SplitScratch1;
1951 AN.SplitScratchSize = AN.SplitScratchSize1;
1952 AN.InScratch = AN.InScratch1;
1954 S->PolyWise = oldpw;
1955 AN.SplitScratch1 = AN.SplitScratch;
1956 AN.SplitScratchSize1 = AN.SplitScratchSize;
1957 AN.InScratch1 = AN.InScratch;
1958 AN.SplitScratch = oldSplitScratch;
1959 AN.SplitScratchSize = oldSplitScratchSize;
1960 AN.InScratch = oldInScratch;
1962 AR.SortType = oldtype;
1963 if ( w[1] <= FUNHEAD ||
1964 ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
1965 *ps1 = *ps2 = 0;
return(0);
1969 if ( w + s1[1] + s2[1] + 12 + ARGHEAD >= AT.WorkTop ) {
1970 MLOCK(ErrorMessageLock);
1971 MesPrint(
"Program was adding polyfun arguments");
1973 MUNLOCK(ErrorMessageLock);
1980 if ( w[1] <= FUNHEAD ) { *ps1 = *ps2 = 0;
return(0); }
1981 if ( w[1] <= s1[1] || w[1] <= s2[1] ) {
1982 if ( w[1] > s1[1] ) {
1991 while ( t < m ) *s1++ = *t++;
1992 **ps1 = WORDDIF(s1,(*ps1));
2001 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2003 MesPrint(
"------Garbage collection-------");
2005 AT.WorkPointer += w[1];
2009 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
2012 MLOCK(ErrorMessageLock);
2013 AO.OutFill = AO.OutputLine = OutBuf;
2017 while ( --i >= 0 ) {
2018 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2021 AO.OutFill = AO.OutputLine = OutBuf;
2026 while ( --i >= 0 ) {
2027 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2030 AO.OutFill = AO.OutputLine = OutBuf;
2035 while ( --i >= 0 ) {
2036 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2039 if ( AR.sLevel > 0 ) {
2040 MesPrint(
"Please increase SubSmallExtension setup parameter.");
2043 MesPrint(
"Please increase SmallExtension setup parameter.");
2045 MUNLOCK(ErrorMessageLock);
2047 MLOCK(ErrorMessageLock);
2048 if ( AR.sLevel > 0 ) {
2049 MesPrint(
"Please increase SubSmallExtension setup parameter.");
2052 MesPrint(
"Please increase SmallExtension setup parameter.");
2054 MUNLOCK(ErrorMessageLock);
2068 while ( t < w ) *s2++ = *t++;
2072 if ( *m > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
2073 MLOCK(ErrorMessageLock);
2074 MesPrint(
"Term too complex after polynomial addition. MaxTermSize = %10l",
2075 AM.MaxTer/
sizeof(WORD));
2076 MUNLOCK(ErrorMessageLock);
2079 if ( *m > S->verbMaxTermSize ) S->verbMaxTermSize = *m;
2102 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2103 WORD tempterm[8+FUNHEAD];
2105 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2106 *m++ = 0; *m++ = 0; FILLARG(m)
2107 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2108 if ( s1[FUNHEAD] < 0 ) {
2109 if ( s2[FUNHEAD] < 0 ) {
2110 if ( s1[FUNHEAD] <= -FUNCTION ) {
2111 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2112 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2114 *m++ = 2; *m++ = 1; *m++ = 3;
2115 INSLENGTH(4+FUNHEAD)
2117 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2118 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2119 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2120 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2122 s2[FUNHEAD] = s1[FUNHEAD];
2125 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2127 *m++ = 1; *m++ = 1; *m++ = 3;
2128 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2130 *m++ = 1; *m++ = 1; *m++ = 3;
2131 INSLENGTH(8+2*FUNHEAD)
2133 else if ( s2[FUNHEAD] == -SYMBOL ) {
2134 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2135 *m++ = 1; *m++ = 1; *m++ = 3;
2136 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2138 *m++ = 1; *m++ = 1; *m++ = 3;
2139 INSLENGTH(12+FUNHEAD)
2143 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2144 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2146 *m++ = 1; *m++ = 1; *m++ = 3;
2147 INSLENGTH(8+FUNHEAD)
2150 else if ( s1[FUNHEAD] == -SYMBOL ) {
2151 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2152 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2153 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2154 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2158 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2159 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2160 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2161 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2162 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2163 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2164 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2168 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2169 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2170 *m++ = 1; *m++ = 1; *m++ = 3;
2171 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2173 *m++ = 1; *m++ = 1; *m++ = 3;
2174 INSLENGTH(12+FUNHEAD)
2178 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2179 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2180 *m++ = 1; *m++ = 1; *m++ = 3;
2185 if ( s2[FUNHEAD] <= -FUNCTION ) {
2187 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2188 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2190 *m++ = 1; *m++ = 1; *m++ = 3;
2191 INSLENGTH(8+FUNHEAD)
2193 else if ( s2[FUNHEAD] == -SYMBOL ) {
2195 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2196 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2197 *m++ = 1; *m++ = 1; *m++ = 3;
2202 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2203 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2204 else { i1 = (WORD)x1; i2 = 3; }
2205 if ( x1 && AN.ncmod != 0 ) {
2210 if ( Modulus(m) ) Terminate(-1);
2211 if ( *m == 0 ) w[1] = 0;
2213 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2231 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2238 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2251 if ( s1[FUNHEAD] <= -FUNCTION ) {
2252 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2254 *t++ = 1; *t++ = 1; *t++ = 3;
2256 else if ( s1[FUNHEAD] == -SYMBOL ) {
2257 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2258 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2259 *t++ = 1; *t++ = 1; *t++ = 3;
2262 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2263 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2279 tstop1 = s1 + s1[1];
2280 s1 += FUNHEAD+ARGHEAD;
2282 tstop2 = s2 + s2[1];
2283 s2 += FUNHEAD+ARGHEAD;
2287 oldPolyFlag = AT.SS->PolyFlag;
2288 AT.SS->PolyFlag = 0;
2289 while ( s1 < tstop1 && s2 < tstop2 ) {
2290 i1 = CompareTerms(BHEAD s1,s2,(WORD)(-1));
2295 else if ( i1 < 0 ) {
2318 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2320 MLOCK(ErrorMessageLock);
2321 MesPrint(
"!>Addition of coefficients of PolyFun");
2322 MUNLOCK(ErrorMessageLock);
2333 *mm = WORDDIF(m,mm);
2334 if ( AN.ncmod != 0 ) {
2335 if ( Modulus(mm) ) Terminate(-1);
2342 while ( s1 < tstop1 ) *m++ = *s1++;
2343 while ( s2 < tstop2 ) *m++ = *s2++;
2344 w[1] = WORDDIF(m,w);
2345 w[FUNHEAD] = w[1] - FUNHEAD;
2346 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2347 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2348 else w[1] = FUNHEAD+2;
2349 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2352 AT.SS->PolyFlag = oldPolyFlag;
2393WORD
Compare1(PHEAD WORD *term1, WORD *term2, WORD level)
2396 WORD *stopper1, *stopper2, *t2;
2398 WORD *stopex1, *stopex2;
2401 WORD count = -1, localPoly, polyhit = -1;
2404 S->verbComparisons++;
2406 if ( S->PolyFlag ) {
2414 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2415 S->PolyFlag = AR.PolyFunType;
2416 if ( AR.PolyFunType == 2 &&
2417 ( AR.PolyFunExp == 2 || AR.PolyFunExp == 3 ) ) S->PolyFlag = 1;
2419 else { localPoly = 0; }
2421 AT.SortFloatMode = 0;
2426 GETSTOP(term2,stopper2);
2429 while ( t1 < stopper1 && t2 < stopper2 ) {
2431 if ( *t1 == HAAKJE )
return(PREV(-1));
2432 if ( *t2 == HAAKJE )
return(PREV(1));
2433 if ( *t1 >= (FUNCTION-1) ) {
2434 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2435 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2436 if ( *t1 < FUNCTION )
return(PREV(1));
2437 if ( *t2 < FUNCTION )
return(PREV(-1));
2438 c1 = functions[*t1-FUNCTION].commute;
2439 c2 = functions[*t2-FUNCTION].commute;
2441 if ( c2 )
return(PREV(1));
2442 else return(PREV(*t2-*t1));
2445 if ( !c2 )
return(PREV(-1));
2446 else return(PREV(*t2-*t1));
2449 else return(PREV(*t2-*t1));
2456 if ( localPoly && c1 < FUNCTION ) {
2459 if ( c1 <= (FUNCTION-1)
2460 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec > 0 ) ) {
2461 if ( c1 == SYMBOL ) {
2462 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2463 && s1[-1] == 4 && s2[-1] == 4
2464 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2465 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2472 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2475 else if ( AR.SortType >= SORTPOWERFIRST ) {
2478 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2480 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2483 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2490 if ( AR.SortType==SORTLOWFIRST ) {
2491 return(PREV((s1[1]>0?-1:1)));
2494 return(PREV((s1[1]<0?-1:1)));
2499 if ( AR.SortType==SORTLOWFIRST ) {
2501 return(PREV((s1[1]<0?1:-1)));
2504 return(PREV((s2[1]<0?-1:1)));
2509 return(PREV((s1[1]<0?-1:1)));
2512 return(PREV((s2[1]<0?1:-1)));
2517 if ( *s1 != *s2 )
return(
2518 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2523 if ( AR.SortType==SORTLOWFIRST ) {
2524 return(PREV((s2[1]<0?-1:1)));
2527 return(PREV((s2[1]<0?1:-1)));
2531 else if ( c1 == DOTPRODUCT ) {
2532 if ( AR.SortType >= SORTPOWERFIRST ) {
2535 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2537 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2540 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2545 if ( s2 >= t2 )
return(PREV(1));
2546 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2548 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2550 if ( *s1 != *s2 )
return(
2551 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2554 if ( s2 < t2 )
return(PREV(-1));
2558 if ( s2 >= t2 )
return(PREV(1));
2559 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2562 if ( s2 < t2 )
return(PREV(-1));
2570 if ( localPoly && c1 == AR.PolyFun ) {
2572 if ( S->PolyFlag == 1 ) {
2574 if ( *s1 > 0 ) i1 = *s1;
2575 else if ( *s1 <= -FUNCTION ) i1 = 1;
2577 if ( *s2 > 0 ) i2 = *s2;
2578 else if ( *s2 <= -FUNCTION ) i2 = 1;
2580 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2585 WORD *u1, *u2, *ustop;
2587 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2594 ustop = u2 - ABS(u2[-1]);
2596 while ( u1 < ustop ) {
2597 if ( *u1 == INDEX )
goto NoPoly;
2604 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2611 ustop = u2 - ABS(u2[-1]);
2613 while ( u1 < ustop ) {
2614 if ( *u1 == INDEX )
goto NoPoly;
2621 S->PolyWise = WORDDIF(s1,term1);
2622 S->PolyWise -= FUNHEAD;
2628 S->PolyWise = localPoly = 0;
2631 else if ( AR.PolyFunType == 2 ) {
2632 WORD i1, i2, i1a, i2a;
2633 if ( *s1 > 0 ) i1 = *s1;
2634 else if ( *s1 <= -FUNCTION ) i1 = 1;
2636 if ( *s2 > 0 ) i2 = *s2;
2637 else if ( *s2 <= -FUNCTION ) i2 = 1;
2639 if ( s1[i1] > 0 ) i1a = s1[i1];
2640 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2642 if ( s2[i2] > 0 ) i2a = s2[i2];
2643 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2645 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2650 WORD *u1, *u2, *ustop;
2652 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2657 while ( u1 < s1+i1 ) {
2659 ustop = u2 - ABS(u2[-1]);
2661 while ( u1 < ustop ) {
2662 if ( *u1 == INDEX )
goto NoPoly;
2669 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2673 u1 = s1 +i1 + ARGHEAD;
2676 ustop = u2 - ABS(u2[-1]);
2678 while ( u1 < ustop ) {
2679 if ( *u1 == INDEX )
goto NoPoly;
2686 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2691 while ( u1 < s2+i2 ) {
2693 ustop = u2 - ABS(u2[-1]);
2695 while ( u1 < ustop ) {
2696 if ( *u1 == INDEX )
goto NoPoly;
2703 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2707 u1 = s2 + i2 + ARGHEAD;
2710 ustop = u2 - ABS(u2[-1]);
2712 while ( u1 < ustop ) {
2713 if ( *u1 == INDEX )
goto NoPoly;
2720 S->PolyWise = WORDDIF(s1,term1);
2721 S->PolyWise -= FUNHEAD;
2726 S->PolyWise = localPoly = 0;
2730 S->PolyWise = localPoly = 0;
2734 t1 = term1 + S->PolyWise;
2735 t2 = term2 + S->PolyWise;
2742 if ( level == 0 && c1 == FLOATFUN && t1 == stopper1 && t2 == stopper2 && AT.aux_ != 0 ) {
2746 if ( TestFloat(s1-FUNHEAD) ) {
2747 if ( TestFloat(s2-FUNHEAD) ) { AT.SortFloatMode = 3;
return(0); }
2750 else if ( TestFloat(s2-FUNHEAD) ) {
return(-1); }
2757 if ( s2 >= t2 )
return(PREV(-1));
2764 if ( AC.properorderflag ) {
2766 oldpolyflag = S->PolyFlag;
2768 if ( ( c2 = -CompArg(s1,s2) ) != 0 ) {
2769 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2771 S->PolyFlag = oldpolyflag;
2780 if ( s2 >= t2 )
return(PREV(-1));
2782 s1 += ARGHEAD; s2 += ARGHEAD;
2783 oldpolyflag = S->PolyFlag;
2785 while ( s1 < stopex1 ) {
2786 if ( s2 >= stopex2 ) {
2787 S->PolyFlag = oldpolyflag;
return(PREV(-1));
2789 if ( ( c2 = CompareTerms(BHEAD s1,s2,(WORD)1) ) != 0 ) {
2790 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2795 S->PolyFlag = oldpolyflag;
2796 if ( s2 < stopex2 )
return(PREV(1));
2798 else return(PREV(1));
2801 if ( *s2 > 0 )
return(PREV(-1));
2802 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2803 if ( *s1 > -FUNCTION ) {
2804 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2810 if ( s2 < t2 )
return(PREV(1));
2814 if ( level == 0 && t1 < stopper1 && *t1 == FLOATFUN && t1+t1[1] == stopper1
2815 && TestFloat(t1) && AT.aux_ != 0 ) {
2816 AT.SortFloatMode = 1;
return(0);
2818 else if ( level == 0 && t2 < stopper2 && *t2 == FLOATFUN && t2+t2[1] == stopper2
2819 && TestFloat(t2) && AT.aux_ != 0 ) {
2820 AT.SortFloatMode = 2;
return(0);
2824 if ( AR.SortType != SORTLOWFIRST ) {
2825 if ( t1 < stopper1 )
return(PREV(1));
2826 if ( t2 < stopper2 )
return(PREV(-1));
2829 if ( t1 < stopper1 )
return(PREV(-1));
2830 if ( t2 < stopper2 )
return(PREV(1));
2833 if ( level == 3 )
return(
CompCoef(term1,term2));
3129 WORD **pp3, **pp1, **pp2, **pptop;
3130 LONG i, newleft, newright, split;
3132#ifdef SPLITMERGEDEBUG
3134 printf(
"%4ld: ", number);
3135 for (
int ii = 0; ii < S->sTerms; ii++) {
3136 if ( (S->sPointer)[ii] ) {
3137 printf(
"%4d ", (
unsigned)(S->sPointer[ii]-S->sBuffer));
3147 if ( number < 2 )
return(number);
3148 if ( number == 2 ) {
3149 pp1 = Pointer; pp2 = pp1 + 1;
3150 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3151 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3153 else if ( i == 0 ) {
3155 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) number = 0; }
3156 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) number = 0; }
3160 pptop = Pointer + number;
3163 newright =
SplitMerge(BHEAD Pointer+split,number-split);
3164 if ( newright == 0 )
return(newleft);
3172 if ( newleft > 0 && newright > 0 &&
3173 ( i = CompareTerms(BHEAD Pointer[newleft-1],Pointer[split],(WORD)0) ) >= 0 ) {
3174 pp2 = Pointer+split; pp1 = Pointer+newleft-1;
3176 if ( S->PolyWise ) {
3177 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3181 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3184 *pp2++ = 0; newright--;
3187 newleft += newright;
3189 while ( --newright >= 0 ) *pp1++ = *pp2++;
3190 while ( pp1 < pptop ) *pp1++ = 0;
3195 if ( split >= AN.SplitScratchSize ) {
3196 AN.SplitScratchSize = (split*3)/2+100;
3197 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3198 AN.SplitScratchSize = S->Terms2InSmall/2;
3199 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3200 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3203 pp3 = AN.SplitScratch; pp1 = Pointer;
3205 for ( i = 0; i < newleft; i++ ) { *pp3++ = *pp1; *pp1++ = 0; }
3206 AN.InScratch = newleft;
3207 pp1 = AN.SplitScratch; pp2 = Pointer + split; pp3 = Pointer;
3209#ifdef NEWSPLITMERGETIMSORT
3213 while ( newleft > 8 ) {
3215 LONG nnleft = newleft/2;
3216 if ( ( i = CompareTerms(BHEAD pp1[nnleft],*pp2,(WORD)0) ) < 0 ) {
3222 for (
int iii = 0; iii < nnleft; iii++) {
3228 if ( S->PolyWise ) { i =
AddPoly(BHEAD pp1,pp2); }
3229 else { i =
AddCoef(BHEAD pp1,pp2); }
3247 while ( newleft > 0 && newright > 0 ) {
3248 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3259 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3260 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3261 *pp1++ = 0; *pp2++ = 0; newleft--; newright--;
3264 for ( i = 0; i < newleft; i++ ) { *pp3++ = *pp1; *pp1++ = 0; }
3268 for ( i = 0; i < newright; i++ ) { *pp3++ = *pp2++; }
3270 newleft = pp3 - Pointer;
3271 while ( pp3 < pptop ) *pp3++ = 0;
3524 WORD **poin, **poin2, ul, k, i, im, *m1;
3525 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3526 WORD *m2, *m3, r31, r33, ki, *rr;
3536 int oldgzipCompress = AR.gzipCompress;
3538 AR.gzipCompress = 0;
3542 fout = &(AR.FoStage4[0]);
3545 poin = S->poina; poin2 = S->poin2a;
3546 rr = AR.CompressPointer;
3553 if ( fout->
handle < 0 ) {
3555 PUTZERO(S->OldPosOut);
3556 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3557 MLOCK(ErrorMessageLock);
3558 MesPrint(
"Cannot create file %s",fout->name);
3559 MUNLOCK(ErrorMessageLock);
3563 MLOCK(ErrorMessageLock);
3564 MesPrint(
"%w MergePatches created output file %s",fout->name);
3565 MUNLOCK(ErrorMessageLock);
3568 PUTZERO(fout->filesize);
3569 PUTZERO(fout->POposition);
3577 LOCK(fout->pthreadslock);
3579 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3581 UNLOCK(fout->pthreadslock);
3584 PUTZERO(S->fPatches[0]);
3585 fout->POfill = fout->PObuffer;
3586 PUTZERO(fout->POposition);
3591 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3592 S->fpcompressed[S->fPatchN] = 1;
3594 S->fpcompressed[S->fPatchN] = 0;
3595 SetupOutputGZIP(fout);
3598 else if ( par == 0 && S->stage4 > 0 ) {
3610 S->iPatches = S->fPatches;
3611 S->fPatches = S->inPatches;
3612 S->inPatches = S->iPatches;
3613 (S->inNum) = S->fPatchN;
3614 S->OldPosIn = S->OldPosOut;
3616 m1 = S->fpincompressed;
3617 S->fpincompressed = S->fpcompressed;
3618 S->fpcompressed = m1;
3619 for ( i = 0; i < S->inNum; i++ ) {
3620 S->fPatchesStop[i] = S->iPatches[i+1];
3622 MLOCK(ErrorMessageLock);
3623 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3624 MUNLOCK(ErrorMessageLock);
3636 AR.gzipCompress = 0;
3639 S->iPatches = S->fPatches;
3640 S->inNum = S->fPatchN;
3642 m1 = S->fpincompressed;
3643 S->fpincompressed = S->fpcompressed;
3644 S->fpcompressed = m1;
3645 for ( i = 0; i < S->inNum; i++ ) {
3646 S->fPatchesStop[i] = S->fPatches[i+1];
3648 MLOCK(ErrorMessageLock);
3649 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3650 MUNLOCK(ErrorMessageLock);
3658 S->Patches[S->lPatch] = S->lFill;
3659 for ( i = 0; i < S->lPatch; i++ ) {
3660 S->pStop[i] = S->Patches[i+1]-1;
3661 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3665 S->lPatch = (S->inNum);
3667 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3669 if ( S->lPatch > 1 ) {
3672 SetupAllInputGZIP(S);
3675 for ( i = 0; i < S->lPatch; i++ ) {
3676 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3678 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3679 S->pStop[i] = m2 = p;
3681 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3683 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3688 if ( fout->
handle >= 0 ) {
3691 LOCK(fout->pthreadslock);
3693 SeekFile(fout->
handle,&position,SEEK_END);
3694 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3696 UNLOCK(fout->pthreadslock);
3700 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3709 if ( S->lPatch == 1 ) {
3712 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3718 m2 = m1 = *S->Patches;
3721 i = -(*m1++); m2 += i; im = *m1+i+1;
3722 while ( i > 0 ) { *m1-- = *m2--; i--; }
3733 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3734 im = PutToMaster(BHEAD m1);
3738 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3739 ADDPOS(S->SizeInFile[par],im);
3744 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3745 PutToMaster(BHEAD 0);
3749 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3750 ADDPOS(S->SizeInFile[par],1);
3753 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3754 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3756 ADDPOS(position,length);
3757 ADDPOS(fout->POposition,length);
3758 ADDPOS(fout->filesize,length);
3759 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3769 #define FRONTSIZE (2*AM.MaxTer)
3770 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3772 SetupAllInputGZIP(S);
3774 position2 = S->iPatches[0];
3775 while ( ( length = FillInputGZIP(fin,&position2,
3777 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3778 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3779 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3780 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3788 i = -(*m1++); m2 += i; im = *m1+i+1;
3789 while ( i > 0 ) { *m1-- = *m2--; i--; }
3793 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3794 im = PutToMaster(BHEAD m1);
3798 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3799 ADDPOS(S->SizeInFile[par],im);
3803 if ( m1 < copytop && *m1 == 0 )
break;
3809 while ( m1 > m2 ) *--m3 = *--m1;
3815 MLOCK(ErrorMessageLock);
3816 MesPrint(
"!>Readerror");
3821 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3822 PutToMaster(BHEAD 0);
3826 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3827 ADDPOS(S->SizeInFile[par],1);
3830 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3831 while ( ( length = ReadFile(fin->
handle,
3832 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3833 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3835 ADDPOS(position,length);
3836 ADDPOS(fout->POposition,length);
3837 ADDPOS(fout->filesize,length);
3838 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3842 MLOCK(ErrorMessageLock);
3843 MesPrint(
"!>Readerror");
3851 else if ( S->lPatch > 0 ) {
3856 do { lpat *= 2; }
while ( lpat < S->lPatch );
3857 mpat = ( lpat >> 1 ) - 1;
3858 k = lpat - S->lPatch;
3863 for ( i = 1; i < lpat; i++ ) {
3866 for ( i = 1; i <= k; i++ ) {
3868 poin[im] = S->Patches[i-1];
3869 poin2[im] = poin[im] + *(poin[im]);
3872 S->tree[mpat+i] = 0;
3873 poin[im-1] = poin2[im-1] = 0;
3875 for ( i = (k*2)+1; i <= lpat; i++ ) {
3878 poin[i] = S->Patches[i-k-1];
3879 poin2[i] = poin[i] + *(poin[i]);
3899 if ( !*(poin[k]) ) {
3900 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3901 if ( S->tree[i] == -1 ) {
3914 if ( S->tree[i] > 0 ) {
3915 if ( ( c = CompareTerms(BHEAD poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3919 S->used[level] = S->tree[i];
3929 l1 = *( m1 = poin[S->tree[i]] );
3930 l2 = *( m2 = poin[k] );
3931 if ( S->PolyWise ) {
3936 if ( S->PolyFlag == 2 ) {
3938 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3939 MLOCK(ErrorMessageLock);
3940 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3941 MUNLOCK(ErrorMessageLock);
3948 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3949 MLOCK(ErrorMessageLock);
3950 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3951 MUNLOCK(ErrorMessageLock);
3958 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
3960 if ( r1 == m1[1] ) {
3963 else if ( r1 < m1[1] ) {
3967 while ( --r1 >= 0 ) *--m1 = *--m2;
3970 while ( --r1 >= 0 ) *--m1 = *--m2;
3972 poin[S->tree[i]] = m1;
3980 poin[S->tree[i]] = m2;
3987 else if ( AT.SortFloatMode ) {
3988 WORD *term1, *term2;
3989 term1 = poin[S->tree[i]];
3991 if ( MergeWithFloat(BHEAD &term1,&term2) == 0 )
3993 poin[S->tree[i]] = term1;
3997 r1 = *( m1 += l1 - 1 );
3999 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
4000 r2 = *( m2 += l2 - 1 );
4002 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
4004 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
4005 MLOCK(ErrorMessageLock);
4006 MesCall(
"MergePatches");
4007 MUNLOCK(ErrorMessageLock);
4011 if ( AN.ncmod != 0 ) {
4012 if ( ( AC.modmode & POSNEG ) != 0 ) {
4015 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
4017 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
4019 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
4023 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
4024 if ( r3 < 0 ) r3 = -r3;
4025 if ( r1 < 0 ) r1 = -r1;
4030 ul = S->used[level] = S->tree[i];
4037 r1 = poin2[ul][1] - im + 1;
4039 m2 = poin[ul] - im + 1;
4040 while ( ++im <= 0 ) *--m1 = *--m2;
4045 poin[ul] = poin2[ul];
4047 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
4050 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
4052 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4053 S->Patches[ki],&(poin[ul]),ki));
4055 poin2[ul] = poin[ul] + im;
4060 S->used[++level] = k;
4066 else if ( r31 < 0 ) {
4075 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
4081 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
4082 MLOCK(ErrorMessageLock);
4083 MesPrint(
"Coefficient overflow during sort");
4084 MUNLOCK(ErrorMessageLock);
4087 m2 = poin[S->tree[i]];
4088 m3 = ( poin[S->tree[i]] -= r31 );
4089 do { *m3++ = *m2++; }
while ( m2 < m1 );
4093 *(poin[S->tree[i]]) += r31;
4095 m2 = (WORD *)coef; im = r3;
4106 r1 = poin2[k][1] - im + 1;
4108 m2 = poin[k] - im + 1;
4109 while ( ++im <= 0 ) *--m1 = *--m2;
4116 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
4119 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
4121 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4122 S->Patches[ki],&(poin[k]),ki));
4124 poin2[k] = poin[k] + im;
4132 else if ( S->tree[i] < 0 ) {
4143 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
4144 im = PutToMaster(BHEAD poin[k]);
4148 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
4150 MLOCK(ErrorMessageLock);
4151 MesPrint(
"!>Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
4152 MUNLOCK(ErrorMessageLock);
4156 ADDPOS(S->SizeInFile[par],im);
4164 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
4165 PutToMaster(BHEAD 0);
4169 if (
FlushOut(&position,fout,1) )
goto ReturnError;
4170 ADDPOS(S->SizeInFile[par],1);
4174 SeekFile(fout->
handle,&position,SEEK_CUR);
4177 S->fPatches[S->fPatchN] = position;
4179 if ( par == 0 && fout != AR.outfile ) {
4188 S->fPatches[S->fPatchN] = position;
4189 if ( ISNOTZEROPOS(S->OldPosIn) ) {
4191 SeekFile(fin->
handle,&(S->OldPosIn),SEEK_SET);
4197 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
4199 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->OldPosIn)),(LONG)
sizeof(
POSITION)) !=
4201 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
4204 MLOCK(ErrorMessageLock);
4205 MesPrint(
"!>Read error fourth stage sorting");
4206 MUNLOCK(ErrorMessageLock);
4212 for ( i = 0; i < S->inNum; i++ ) {
4213 S->fPatchesStop[i] = S->iPatches[i+1];
4215 MLOCK(ErrorMessageLock);
4216 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
4217 MUNLOCK(ErrorMessageLock);
4239 MLOCK(ErrorMessageLock);
4240 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4241 MUNLOCK(ErrorMessageLock);
4251 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4252 PUTZERO(S->SizeInFile[0]);
4266 MLOCK(ErrorMessageLock);
4267 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4268 MUNLOCK(ErrorMessageLock);
4273 AR.gzipCompress = oldgzipCompress;
4278 AR.gzipCompress = oldgzipCompress;
4283 MLOCK(ErrorMessageLock);
4284 MesPrint(
"Error while writing to file.");
4288 MLOCK(ErrorMessageLock);
4290 MesCall(
"MergePatches");
4291 MUNLOCK(ErrorMessageLock);
4293 AR.gzipCompress = oldgzipCompress;