145 if ( AT.SS == AT.S0 && AC.StatsFlag ) {
147 if ( AC.ThreadStats == 0 && identity > 0 )
return;
148#elif defined(WITHMPI)
149 if ( AC.OldParallelStats )
return;
150 if ( ! AC.ProcessStats && PF.me != MASTER )
return;
152 if ( Expressions == 0 )
return;
154 if ( par == STATSSPLITMERGE ) {
155 if ( AC.ShortStatsMax == 0 )
return;
157 if ( AR.ShortSortCount < AC.ShortStatsMax )
return;
159 AR.ShortSortCount = 0;
163 char humanGenTermsText[HUMANSTRLEN] =
"";
164 char humanTermsLeftText[HUMANSTRLEN] =
"";
165 char humanBytesText[HUMANSTRLEN] =
"";
166 char humanUnsortedBytesText[HUMANSTRLEN] =
"";
167 char humanComparisonsText[HUMANSTRLEN] =
"";
168 char humanMaxTermSizeText[HUMANSTRLEN] =
"";
169 if ( AC.HumanStatsFlag ) {
170 HumanString(humanGenTermsText, (
float)(S->GenTerms), humanTermsSuffix);
171 HumanString(humanTermsLeftText, (
float)(S->TermsLeft), humanTermsSuffix);
172 HumanString(humanBytesText, (
float)(BASEPOSITION(*plspace)), humanBytesSuffix);
173 HumanString(humanUnsortedBytesText, (
float)(S->verbUnsortedSize), humanBytesSuffix);
174 HumanString(humanComparisonsText, (
float)(S->verbComparisons), humanTermsSuffix);
175 HumanString(humanMaxTermSizeText, (
float)(S->verbMaxTermSize), humanTermsSuffix);
178 MLOCK(ErrorMessageLock);
183 const WORD oldLogHandle = AC.LogHandle;
184 if ( checkLogType && AM.LogType ) {
188 if ( AC.ShortStats ) {}
191 if ( identity > 0 ) {
192 MesPrint(
" Thread %d reporting",identity);
197#elif defined(WITHMPI)
198 if ( PF.me != MASTER ) {
199 MesPrint(
" Process %d reporting",PF.me);
214 use_wtime = AC.WTimeStatsFlag;
215#if defined(WITHPTHREADS)
216 if ( use_wtime && identity > 0 ) use_wtime = 0;
217#elif defined(WITHMPI)
218 if ( use_wtime && PF.me != MASTER ) use_wtime = 0;
220 char *wpref = use_wtime ?
"W" :
"";
221 char *wspac = use_wtime ?
"" :
" ";
223 timepart = (UWORD)(millitime%1000);
227 if ( AC.ShortStats ) {
228#if defined(WITHPTHREADS) || defined(WITHMPI)
230 if ( identity > 0 ) {
232 if ( PF.me != MASTER ) {
233 const int identity = PF.me;
235 if ( par == STATSSPLITMERGE || par == STATSPOSTSORT ) {
236 snprintf(buf,
sizeof(buf),
237 "%d: %7ld.%02us %8ld>%10ld%3s%10ld:%10ld %s %s",identity,
238 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
239 S->TermsLeft,BASEPOSITION(*plspace),EXPRNAME(AR.CurExpr),
243 else if ( par == STATSMERGETOFILE ) {
244 snprintf(buf,
sizeof(buf),
245 "%d: %7ld.%02us %10ld:%10ld",identity,millitime,timepart,
246 S->TermsLeft,BASEPOSITION(*plspace));
253 if ( par == STATSSPLITMERGE || par == STATSPOSTSORT ) {
254 snprintf(buf,
sizeof(buf),
255 "%7ld.%02us %8ld>%10ld%3s%10ld:%10ld %s %s",
256 millitime,timepart,AN.ninterms,S->GenTerms,toterms[par],
257 S->TermsLeft,BASEPOSITION(*plspace),EXPRNAME(AR.CurExpr),
261 else if ( par == STATSMERGETOFILE ) {
262 snprintf(buf,
sizeof(buf),
263 "%7ld.%02us %10ld:%10ld",millitime,timepart,
264 S->TermsLeft,BASEPOSITION(*plspace));
270 if ( par == STATSMERGETOFILE ) {
271 snprintf(buf,
sizeof(buf),
272 "%sTime = %7ld.%02u sec",wpref,millitime,timepart);
276 snprintf(buf,
sizeof(buf),
277 "%sTime = %7ld.%02u sec %sGenerated terms =%11ld%s",
278 wpref, millitime, timepart, wspac, S->GenTerms, humanGenTermsText);
282 const int exprlen = strlen((
char*)EXPRNAME(AR.CurExpr));
283 const int overflow = MaX(0, exprlen - COL_EXP);
284 if ( par == STATSSPLITMERGE ) {
285 int width = snprintf(buf,
sizeof(buf),
287 COL_EXP, EXPRNAME(AR.CurExpr),
288 MaX(0,COL_SPA-1-overflow), AN.ninterms, FG.swmes[par]);
289 width += snprintf(buf+width,
sizeof(buf)-width,
291 MaX(0,COL_EQU-1-width),
"");
292 snprintf(buf+width,
sizeof(buf)-width,
294 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
299 if ( identity > 0 && par == STATSPOSTSORT ) {
300 int width = snprintf(buf,
sizeof(buf),
301 "%*s%*s Terms in thread",
302 COL_EXP, EXPRNAME(AR.CurExpr), MaX(0,COL_SPA-overflow),
"");
303 width += snprintf(buf+width,
sizeof(buf)-width,
305 MaX(0,COL_EQU-1-width),
"");
306 snprintf(buf+width,
sizeof(buf)-width,
308 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
312#elif defined(WITHMPI)
313 if ( PF.me != MASTER && par == STATSPOSTSORT ) {
314 int width = snprintf(buf,
sizeof(buf),
315 "%*s%*s Terms in process=",
316 COL_EXP, EXPRNAME(AR.CurExpr), MaX(0,COL_SPA-overflow),
"");
317 snprintf(buf+width,
sizeof(buf)-width,
319 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
325 int width = snprintf(buf,
sizeof(buf),
327 COL_EXP, EXPRNAME(AR.CurExpr), MaX(0,COL_SPA-overflow),
"",
329 width += snprintf(buf+width,
sizeof(buf)-width,
331 MaX(0,COL_EQU-1-width),
"");
332 snprintf(buf+width,
sizeof(buf)-width,
334 MaX(0,COL_VAL-width), S->TermsLeft, humanTermsLeftText);
339 const WORD dig = DigitsIn(BASEPOSITION(*plspace));
340 snprintf(buf,
sizeof(buf),
341 "%*s Bytes used%*s=%11ld%s",
342 COL_EXP+COL_SPA, AC.Commercial, MiN(6,17-dig),
"",
343 BASEPOSITION(*plspace), humanBytesText);
347 if ( par == STATSPOSTSORT ) {
348 if ( AC.SortVerbose ) {
349 snprintf(buf,
sizeof(buf),
"%*s Unsorted bytes =%11ld%s",
350 COL_EXP+COL_SPA,
"", S->verbUnsortedSize, humanUnsortedBytesText);
352 snprintf(buf,
sizeof(buf),
"%*s Small Buffer =%5ld,%5ld",
353 COL_EXP+COL_SPA,
"", S->verbSBsortTerms, S->verbSBsortCap);
355 snprintf(buf,
sizeof(buf),
"%*s Large Buffer =%5ld,%5ld",
356 COL_EXP+COL_SPA,
"", S->verbLBsortPatches, S->verbLBsortCap);
358 snprintf(buf,
sizeof(buf),
"%*s Comparisons =%11ld%s",
359 COL_EXP+COL_SPA,
"", S->verbComparisons, humanComparisonsText);
361 snprintf(buf,
sizeof(buf),
"%24s Largest Term =%11ld%s",
362 "",S->verbMaxTermSize,humanMaxTermSizeText);
368 MesPrint(
"Total number of writes: %l, reads: %l, seeks, %l"
369 ,numwrites,numreads,numseeks);
370 MesPrint(
"Total number of mallocs: %l, frees: %l"
371 ,nummallocs,numfrees);
375 AC.LogHandle = oldLogHandle;
377 MUNLOCK(ErrorMessageLock);
490 WORD j, **ss, *to, *t;
491 LONG sSpace, over, tover, spare, retval = 0;
494 FILEHANDLE *fout = 0, *oldoutfile = 0, *newout = 0;
496 if ( AM.exitflag && AR.sLevel == 0 )
return(0);
499 oldoutfile = AR.outfile;
509 oldoutfile = AR.outfile;
528 PrintTime((UBYTE *)
"EndSort, before SplitMerge");
530 S->sPointer[
SplitMerge(BHEAD S->sPointer,S->sTerms)] = 0;
532 PrintTime((UBYTE *)
"Endsort, after SplitMerge");
535 tover = over = S->sTerms;
538 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
541 S->TermsLeft -= over - spare;
542 if ( par == 1 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
544 else if ( S != AT.S0 ) {
548 while ( ( t = *ss++ ) != 0 ) { sSpace += *t; }
549 if ( AN.tryterm > 0 && ( (sSpace+1)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
550 to = TermMalloc(
"$-sort space");
553 LONG allocsp = sSpace+1;
554 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
555 allocsp = ((allocsp+7)/8)*8;
556 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-sort space");
557 if ( AN.tryterm > 0 ) AN.tryterm = 0;
559 *((WORD **)buffer) = to;
561 while ( ( t = *ss++ ) != 0 ) {
562 j = *t;
while ( --j >= 0 ) *to++ = *t++;
570 while ( ( t = *ss++ ) != 0 ) {
572 if ( ( sSpace += j ) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
574 while ( ( t = *ss++ ) != 0 ) {
577 MLOCK(ErrorMessageLock);
578 MesPrint(
"Sorted function argument too long (%d words). Increase MaxTermSize (%l words).", sSpace, AM.MaxTer/((LONG)
sizeof(WORD)));
579 MUNLOCK(ErrorMessageLock);
580 retval = -1;
goto RetRetval;
582 while ( --j >= 0 ) *to++ = *t++;
585 retval = to - buffer;
593 *AR.CompressPointer = 0;
594 SeekScratch(AR.outfile,&position);
606 {
int oldgzipCompress = AR.gzipCompress;
611 while ( ( t = *ss++ ) != 0 ) {
612 if ( *t ) S->TermsLeft++;
614 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD t); }
617 if (
PutOut(BHEAD t,&position,fout,1) < 0 ) {
618 retval = -1;
goto RetRetval;
623 if ( AS.MasterSort && ( fout == AR.outfile ) ) { PutToMaster(BHEAD 0); }
627 retval = -1;
goto RetRetval;
630 AR.gzipCompress = oldgzipCompress;
634 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
637 if ( PF.me != MASTER && PF.exprtodo < 0 )
goto RetRetval;
639 DIFPOS(oldpos,position,oldpos);
640 S->SpaceLeft = BASEPOSITION(oldpos);
641 WriteStats(&oldpos,STATSPOSTSORT,NOCHECKLOGTYPE);
646 else if ( par == 1 && newout == 0 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
648 lSpace = sSpace + (S->lFill - S->lBuffer) - (LONG)S->lPatch*(AM.MaxTer/
sizeof(WORD));
651 SETBASEPOSITION(pp,lSpace);
652 MULPOS(pp,
sizeof(WORD));
653 if ( S->file.handle >= 0 ) {
654 ADD2POS(pp,S->fPatches[S->fPatchN]);
657 if ( S->lPatch > 0 || S->file.handle >= 0 ) {
661 if ( par == 2 ) { AR.outfile = newout = AllocFileHandle(0,(
char *)0); }
662 if ( S->lPatch > 0 ) {
663 if ( ( S->lPatch >= S->MaxPatches ) ||
664 ( ( (WORD *)(((UBYTE *)(S->lFill + sSpace)) + 2*AM.MaxTer) ) >= S->lTop ) ) {
669 if ( S->lPatch >= S->MaxPatches ) S->verbLBsortPatches++;
670 else S->verbLBsortCap++;
673 MLOCK(ErrorMessageLock);
674 MesPrint(
"%w EndSort: lPatch = %d, MaxPatches = %d,lFill = %x, sSpace = %ld, MaxTer = %d, lTop = %x"
675 ,S->lPatch,S->MaxPatches,S->lFill,sSpace,AM.MaxTer/
sizeof(WORD),S->lTop);
676 MUNLOCK(ErrorMessageLock);
680 MLOCK(ErrorMessageLock);
682 MUNLOCK(ErrorMessageLock);
683 retval = -1;
goto RetRetval;
686 pp = S->SizeInFile[1];
687 MULPOS(pp,
sizeof(WORD));
693 SETBASEPOSITION(pppp,0);
694 SeekFile(S->file.handle,&pppp,SEEK_CUR);
695 SeekFile(S->file.handle,&pp,SEEK_END);
696 SeekFile(S->file.handle,&pppp,SEEK_SET);
697 WriteStats(&pp,STATSMERGETOFILE,CHECKLOGTYPE);
702 S->Patches[S->lPatch++] = S->lFill;
703 to = (WORD *)(((UBYTE *)(S->lFill)) + AM.MaxTer);
706 while ( ( t = *ss++ ) != 0 ) {
708 if ( j < 0 ) j = t[1] + 2;
709 while ( --j >= 0 ) *to++ = *t++;
714 if ( S->file.handle < 0 ) {
716 MLOCK(ErrorMessageLock);
718 MUNLOCK(ErrorMessageLock);
719 retval = -1;
goto RetRetval;
722 pp = S->SizeInFile[2];
723 MULPOS(pp,
sizeof(WORD));
725 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
731 if ( par == 2 && newout->handle >= 0 ) {
735 LOCK(newout->pthreadslock);
737 SeekFile(newout->handle,&zeropos,SEEK_SET);
738 to = (WORD *)Malloc1(BASEPOSITION(newout->filesize)+
sizeof(WORD)*2
739 ,
"$-buffer reading");
740 if ( AN.tryterm > 0 ) AN.tryterm = 0;
741 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(newout->filesize)) ) !=
742 BASEPOSITION(newout->filesize) ) {
743 MLOCK(ErrorMessageLock);
744 MesPrint(
"Error reading information for $ variable");
745 MUNLOCK(ErrorMessageLock);
746 M_free(to,
"$-buffer reading");
750 *((WORD **)buffer) = to;
751 retval /=
sizeof(WORD);
754 UNLOCK(newout->pthreadslock);
757 else if ( newout->handle >= 0 ) {
764 MLOCK(ErrorMessageLock);
765 MesPrint(
"(1)Output should fit inside a single term. Increase MaxTermSize?");
767 MUNLOCK(ErrorMessageLock);
768 retval = -1;
goto RetRetval;
771 t = newout->PObuffer;
774 j = newout->POfill - t;
776 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
778 if ( j > AM.MaxTer ) {
779 MLOCK(ErrorMessageLock);
780 MesPrint(
"Encountered term of size: %d words.", j/(LONG)
sizeof(WORD) );
781 MUNLOCK(ErrorMessageLock);
785 retval = to - buffer - 1;
792 MLOCK(ErrorMessageLock);
794 MUNLOCK(ErrorMessageLock);
795 retval = -1;
goto RetRetval;
798 pp = S->SizeInFile[1];
799 MULPOS(pp,
sizeof(WORD));
805 SETBASEPOSITION(pppp,0);
806 SeekFile(S->file.handle,&pppp,SEEK_CUR);
807 SeekFile(S->file.handle,&pp,SEEK_END);
808 SeekFile(S->file.handle,&pppp,SEEK_SET);
809 WriteStats(&pp,STATSMERGETOFILE,CHECKLOGTYPE);
817 if ( newout->handle >= 0 )
goto TooLarge;
818 t = newout->PObuffer;
819 j = newout->POfill - t;
821 if ( to >= AT.WorkSpace && to < AT.WorkTop && to+j > AT.WorkTop )
823 if ( j > AM.MaxTer )
goto TooLarge;
830 if ( S->file.handle >= 0 ) {
832 MLOCK(ErrorMessageLock);
833 MesPrint(
"%w EndSort: fPatchN = %d, lPatch = %d, position = %12p"
834 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
835 MUNLOCK(ErrorMessageLock);
837 if ( S->lPatch <= 0 ) {
839 position = S->fPatches[S->fPatchN];
842 *AR.CompressPointer = 0;
844 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
845 S->fpcompressed[S->fPatchN] = 1;
847 S->fpcompressed[S->fPatchN] = 0;
848 SetupOutputGZIP(&(S->file));
850 while ( ( t = *ss++ ) != 0 ) {
851 if (
PutOut(BHEAD t,&position,&(S->file),1) < 0 ) {
852 retval = -1;
goto RetRetval;
855 if (
FlushOut(&position,&(S->file),1) ) {
856 retval = -1;
goto RetRetval;
859 S->fPatches[S->fPatchN] = position;
862 MLOCK(ErrorMessageLock);
863 MesPrint(
"%w EndSort+: fPatchN = %d, lPatch = %d, position = %12p"
864 ,S->fPatchN,S->lPatch,&(S->fPatches[S->fPatchN]));
865 MUNLOCK(ErrorMessageLock);
871 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
872 if ( S->file.handle >= 0 ) {
873 SynchFile(S->file.handle);
879 MLOCK(ErrorMessageLock);
881 MUNLOCK(ErrorMessageLock);
882 retval = -1;
goto RetRetval;
886 if ( AS.MasterSort && ( fout == AR.outfile ) )
goto RetRetval;
888 pp = S->SizeInFile[0];
889 MULPOS(pp,
sizeof(WORD));
897 if ( AR.sLevel == 0 && (PF.me == MASTER || PF.exprtodo >= 0) ) {
898 Expressions[AR.CurExpr].counter = S->TermsLeft;
899 Expressions[AR.CurExpr].size = pp;
902 if ( AR.sLevel == 0 ) {
903 Expressions[AR.CurExpr].counter = S->TermsLeft;
904 Expressions[AR.CurExpr].size = pp;
908 if ( S->file.handle >= 0 && ( par != 1 ) && ( par != 2 ) ) {
912 ClearSortGZIP(&(S->file));
914 CloseFile(S->file.handle);
916 remove(S->file.name);
918 MLOCK(ErrorMessageLock);
919 MesPrint(
"%wEndSort: sortfile %s removed",S->file.name);
920 MUNLOCK(ErrorMessageLock);
923 AR.outfile = oldoutfile;
925 if ( AR.sLevel >= 0 ) AT.SS = AN.FunSorts[AR.sLevel];
930 DeAllocFileHandle(newout);
935 if ( newout->handle >= 0 ) {
936 MLOCK(ErrorMessageLock);
937 MesPrint(
"(2)Output should fit inside a single term. Increase MaxTermSize?");
939 MUNLOCK(ErrorMessageLock);
942 else if ( newout->POfill > newout->PObuffer ) {
948 j = newout->POfill-newout->PObuffer;
949 if ( buffer >= AT.WorkSpace && buffer < AT.WorkTop && buffer+j > AT.WorkTop )
952 to = buffer; t = newout->PObuffer;
953 while ( j-- > 0 ) *to++ = *t++;
957 DeAllocFileHandle(newout);
961 else if ( par == 2 ) {
964 if ( newout->handle >= 0 ) {
972 LOCK(newout->pthreadslock);
974 SeekFile(newout->handle,&position,SEEK_END);
976 SeekFile(newout->handle,&zeropos,SEEK_SET);
977 to = (WORD *)Malloc1(BASEPOSITION(position)+
sizeof(WORD)*3
978 ,
"$-buffer reading");
979 if ( AN.tryterm > 0 ) AN.tryterm = 0;
980 if ( ( retval = ReadFile(newout->handle,(UBYTE *)to,BASEPOSITION(position)) ) !=
981 BASEPOSITION(position) ) {
982 MLOCK(ErrorMessageLock);
983 MesPrint(
"Error reading information for $ variable");
984 MUNLOCK(ErrorMessageLock);
985 M_free(to,
"$-buffer reading");
989 *((WORD **)buffer) = to;
990 retval /=
sizeof(WORD);
993 UNLOCK(newout->pthreadslock);
1000 LONG wsiz = newout->POfill - newout->PObuffer;
1001 if ( AN.tryterm > 0 && ( (wsiz+2)*
sizeof(WORD) < (
size_t)(AM.MaxTer) ) ) {
1002 to = TermMalloc(
"$-sort space");
1005 LONG allocsp = wsiz+2;
1006 if ( allocsp < MINALLOC ) allocsp = MINALLOC;
1007 allocsp = ((allocsp+7)/8)*8;
1008 to = (WORD *)Malloc1(allocsp*
sizeof(WORD),
"$-buffer reading");
1009 if ( AN.tryterm > 0 ) AN.tryterm = 0;
1011 *((WORD **)buffer) = to; t = newout->PObuffer;
1017 DeAllocFileHandle(newout);
1023 DeAllocFileHandle(newout);
1030 MLOCK(ErrorMessageLock);
1033 MUNLOCK(ErrorMessageLock);
1206 WORD i, *p, ret, *r, *rr, j, k, first;
1207 int dobracketindex = 0;
1210 if ( AT.SS != AT.S0 ) {
1214 if ( ( i = *term ) <= 0 )
return(0);
1216 ADDPOS(*position,i*
sizeof(WORD));
1219 if ( p >= fi->POstop ) {
1221 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1223 MLOCK(ErrorMessageLock);
1224 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1225 MUNLOCK(ErrorMessageLock);
1227 fi->
handle = (WORD)RetCode;
1228 PUTZERO(fi->filesize);
1229 PUTZERO(fi->POposition);
1238 MLOCK(ErrorMessageLock);
1239 MesPrint(
"Cannot create scratch file %s",fi->name);
1240 MUNLOCK(ErrorMessageLock);
1245 LOCK(fi->pthreadslock);
1247 if ( fi == AR.hidefile ) {
1248 LOCK(AS.inputslock);
1250 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1251 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1252 if ( fi == AR.hidefile ) {
1253 UNLOCK(AS.inputslock);
1256 UNLOCK(fi->pthreadslock);
1258 MLOCK(ErrorMessageLock);
1259 MesPrint(
"Write error during sort. Disk full?");
1260 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1261 fi->POsize,fi->
handle,&(fi->POposition));
1262 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1263 MUNLOCK(ErrorMessageLock);
1266 ADDPOS(fi->filesize,fi->POsize);
1268 ADDPOS(fi->POposition,fi->POsize);
1269 if ( fi == AR.hidefile ) {
1270 UNLOCK(AS.inputslock);
1273 UNLOCK(fi->pthreadslock);
1276 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1282 }
while ( --i > 0 );
1283 fi->POfull = fi->POfill = p;
1286 if ( ( AP.PreDebug & DUMPOUTTERMS ) == DUMPOUTTERMS ) {
1287 MLOCK(ErrorMessageLock);
1289 snprintf((
char *)(THRbuf),100,
"PutOut(%d)",AT.identity);
1290 PrintTerm(term,(
char *)(THRbuf));
1292 PrintTerm(term,
"PutOut");
1294 MesPrint(
"ncomp = %d, AR.NoCompress = %d, AR.sLevel = %d",ncomp,AR.NoCompress,AR.sLevel);
1295 MesPrint(
"File %s, position %p",fi->name,position);
1296 MUNLOCK(ErrorMessageLock);
1299 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1300 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1301 r = rr = AR.CompressPointer;
1302 first = j = k = ret = 0;
1303 if ( ( i = *term ) != 0 ) {
1306 if ( fi == AR.outfile || fi == AR.hidefile ) {
1307 MLOCK(ErrorMessageLock);
1308 MesPrint(
"Ran into precompressed term");
1309 MUNLOCK(ErrorMessageLock);
1314 else if ( !AR.NoCompress && ( ncomp > 0 ) && AR.sLevel <= 0 ) {
1315 if ( dobracketindex ) {
1316 PutBracketInIndex(BHEAD term,position);
1322 WORD *polystop, *sa;
1326 while ( polystop < sa && *polystop != AR.PolyFun ) {
1327 polystop += polystop[1];
1329 if ( polystop < sa ) {
1330 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1331 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1332 i--; j--; k--; p++; r++;
1336 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1340 else if ( AT.aux_ != 0 ) {
1341 WORD *floatstop, *sa;
1345 while ( floatstop < sa && *floatstop != FLOATFUN ) {
1346 floatstop += floatstop[1];
1348 if ( floatstop < sa ) {
1349 while ( i > 0 && j > 0 && *p == *r && p < floatstop ) {
1350 i--; j--; k--; p++; r++;
1354 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1362 while ( i > 0 && j > 0 && *p == *r && p < sa ) { i--; j--; k--; p++; r++; }
1382 r[-(ABS(r[-1]))] = 0;
1383 if ( r >= AR.ComprTop ) {
1384 MLOCK(ErrorMessageLock);
1385 MesPrint(
"CompressSize of %10l is insufficient",AM.CompressSize);
1386 MUNLOCK(ErrorMessageLock);
1391 else if ( !AR.NoCompress && ( ncomp < 0 ) && AR.sLevel <= 0 ) {
1393 if ( dobracketindex ) {
1394 PutBracketInIndex(BHEAD term,position);
1400 WORD *polystop, *sa;
1404 while ( polystop < sa && *polystop != AR.PolyFun ) {
1405 polystop += polystop[1];
1407 if ( polystop < sa ) {
1408 if ( AR.PolyFunType == 2 ) polystop[2] &= ~MUSTCLEANPRF;
1409 while ( i > 0 && j > 0 && *p == *r && p < polystop ) {
1410 i--; j--; k--; p++; r++;
1414 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1418 while ( i > 0 && j > 0 && *p == *r ) { i--; j--; k--; p++; r++; }
1423 if ( AR.PolyFunType == 2 ) {
1425 tstop = term + *term;
1426 tstop -= ABS(tstop[-1]);
1428 while ( t < tstop ) {
1429 if ( *t == AR.PolyFun ) {
1430 t[2] &= ~MUSTCLEANPRF;
1435 if ( dobracketindex ) {
1436 PutBracketInIndex(BHEAD term,position);
1440 ADDPOS(*position,i*
sizeof(WORD));
1443 if ( p >= fi->POstop ) {
1445 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1447 sbuf->fill[sbuf->active] = fi->POstop;
1449 p = fi->PObuffer = fi->POfill = fi->POfull =
1450 sbuf->buff[sbuf->active];
1451 fi->POstop = sbuf->stop[sbuf->active];
1457 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1459 MLOCK(ErrorMessageLock);
1460 MesPrint(
"%w PutOut created sortfile %s",fi->name);
1461 MUNLOCK(ErrorMessageLock);
1463 fi->
handle = (WORD)RetCode;
1464 PUTZERO(fi->filesize);
1465 PUTZERO(fi->POposition);
1474 MLOCK(ErrorMessageLock);
1475 MesPrint(
"Cannot create scratch file %s",fi->name);
1476 MUNLOCK(ErrorMessageLock);
1481 if ( !AR.NoCompress && ncomp > 0 && AR.gzipCompress > 0
1482 && dobracketindex == 0 && fi->zsp != 0 ) {
1484 if ( PutOutputGZIP(fi) )
return(-1);
1491 LOCK(fi->pthreadslock);
1493 if ( fi == AR.hidefile ) {
1494 LOCK(AS.inputslock);
1496 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1497 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1498 if ( fi == AR.hidefile ) {
1499 UNLOCK(AS.inputslock);
1502 UNLOCK(fi->pthreadslock);
1504 MLOCK(ErrorMessageLock);
1505 MesPrint(
"Write error during sort. Disk full?");
1506 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1507 fi->POsize,fi->
handle,&(fi->POposition));
1508 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1509 MUNLOCK(ErrorMessageLock);
1512 ADDPOS(fi->filesize,fi->POsize);
1514 ADDPOS(fi->POposition,fi->POsize);
1515 if ( fi == AR.hidefile ) {
1516 UNLOCK(AS.inputslock);
1519 UNLOCK(fi->pthreadslock);
1522 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1530 if ( first == 2 ) *p++ = k;
1534 else *p++ = *term++;
1540 }
while ( --i > 0 );
1541 fi->POfull = fi->POfill = p;
1569 int dobracketindex = 0;
1573 if ( AR.sLevel <= 0 && Expressions[AR.CurExpr].newbracketinfo
1574 && ( fi == AR.outfile || fi == AR.hidefile ) ) dobracketindex = 1;
1576 if ( PF.me != MASTER && AR.sLevel <= 0 && (fi == AR.outfile || fi == AR.hidefile) && PF.parallel && PF.exprtodo < 0 ) {
1578 if ( fi->POfill >= fi->POstop ){
1579 sbuf->fill[sbuf->active] = fi->POstop;
1581 fi->POfull = fi->POfill = fi->PObuffer = sbuf->buff[sbuf->active];
1582 fi->POstop = sbuf->stop[sbuf->active];
1584 *(fi->POfill)++ = 0;
1585 sbuf->fill[sbuf->active] = fi->POfill;
1587 fi->PObuffer = fi->POfill = fi->POfull = sbuf->buff[sbuf->active];
1588 fi->POstop = sbuf->stop[sbuf->active];
1592 if ( fi->POfill >= fi->POstop ) {
1594 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
1596 MLOCK(ErrorMessageLock);
1597 MesPrint(
"%w FlushOut created scratch file %s",fi->name);
1598 MUNLOCK(ErrorMessageLock);
1600 PUTZERO(fi->filesize);
1601 PUTZERO(fi->POposition);
1602 fi->
handle = (WORD)RetCode;
1611 MLOCK(ErrorMessageLock);
1612 MesPrint(
"Cannot create scratch file %s",fi->name);
1613 MUNLOCK(ErrorMessageLock);
1618 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1619 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1620 if ( PutOutputGZIP(fi) )
return(-1);
1621 fi->POfill = fi->PObuffer;
1627 LOCK(fi->pthreadslock);
1629 if ( fi == AR.hidefile ) {
1630 LOCK(AS.inputslock);
1632 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1633 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
1635 UNLOCK(fi->pthreadslock);
1637 if ( fi == AR.hidefile ) {
1638 UNLOCK(AS.inputslock);
1640 MLOCK(ErrorMessageLock);
1641 MesPrint(
"Write error while sorting. Disk full?");
1642 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1643 fi->POsize,fi->
handle,&(fi->POposition));
1644 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1645 MUNLOCK(ErrorMessageLock);
1648 ADDPOS(fi->filesize,fi->POsize);
1649 fi->POfill = fi->PObuffer;
1650 ADDPOS(fi->POposition,fi->POsize);
1651 if ( fi == AR.hidefile ) {
1652 UNLOCK(AS.inputslock);
1655 UNLOCK(fi->pthreadslock);
1658 if ( AS.MasterSort && AC.ThreadSortFileSynch && fi != AR.hidefile ) {
1664 *(fi->POfill)++ = 0;
1665 fi->POfull = fi->POfill;
1678 size = (fi->POfill-fi->PObuffer)*
sizeof(WORD);
1681 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1682 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1683 if ( FlushOutputGZIP(fi) )
return(-1);
1684 fi->POfill = fi->PObuffer;
1690 LOCK(fi->pthreadslock);
1692 if ( fi == AR.hidefile ) {
1693 LOCK(AS.inputslock);
1695 SeekFile(fi->
handle,&(fi->POposition),SEEK_SET);
1699 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),size) ) != size ) {
1701 UNLOCK(fi->pthreadslock);
1703 if ( fi == AR.hidefile ) {
1704 UNLOCK(AS.inputslock);
1706 MLOCK(ErrorMessageLock);
1707 MesPrint(
"Write error while finishing sorting. Disk full?");
1708 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
1709 size,fi->
handle,&(fi->POposition));
1710 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
1711 MUNLOCK(ErrorMessageLock);
1714 ADDPOS(fi->filesize,size);
1715 ADDPOS(fi->POposition,size);
1716 fi->POfill = fi->PObuffer;
1717 if ( fi == AR.hidefile ) {
1718 UNLOCK(AS.inputslock);
1721 UNLOCK(fi->pthreadslock);
1724 if ( AS.MasterSort && AC.ThreadSortFileSynch ) {
1730 if ( dobracketindex ) {
1731 BRACKETINFO *b = Expressions[AR.CurExpr].newbracketinfo;
1732 if ( b->indexfill > 0 ) {
1733 DIFPOS(b->
indexbuffer[b->indexfill-1].next,*position,Expressions[AR.CurExpr].onfile);
1737 if ( AT.SS == AT.S0 && !AR.NoCompress && AR.gzipCompress > 0
1738 && dobracketindex == 0 && ( compr > 0 ) && fi->zsp != 0 ) {
1742 LOCK(fi->pthreadslock);
1744 SeekFile(fi->
handle,position,SEEK_END);
1746 UNLOCK(fi->pthreadslock);
1750 ADDPOS(*position,((UBYTE *)fi->POfill-(UBYTE *)fi->PObuffer));
1756 ADDPOS(*position,
sizeof(WORD));
1914 WORD *s1, *s2, *m, *w, *t, oldpw = S->PolyWise;
1915 s1 = *ps1 + S->PolyWise;
1916 s2 = *ps2 + S->PolyWise;
1921 if ( S->PolyFlag == 2 && AR.PolyFunExp != 2 && AR.PolyFunExp != 3 ) {
1922 WORD **oldSplitScratch = AN.SplitScratch;
1923 LONG oldSplitScratchSize = AN.SplitScratchSize;
1924 LONG oldInScratch = AN.InScratch;
1925 WORD oldtype = AR.SortType;
1926 if ( (WORD *)((UBYTE *)w + AM.MaxTer) >= AT.WorkTop ) {
1927 MLOCK(ErrorMessageLock);
1928 MesPrint(
"Program was adding polyratfun arguments");
1930 MUNLOCK(ErrorMessageLock);
1932 AR.SortType = SORTHIGHFIRST;
1934 AN.SplitScratch = AN.SplitScratch1;
1935 AN.SplitScratchSize = AN.SplitScratchSize1;
1936 AN.InScratch = AN.InScratch1;
1938 S->PolyWise = oldpw;
1939 AN.SplitScratch1 = AN.SplitScratch;
1940 AN.SplitScratchSize1 = AN.SplitScratchSize;
1941 AN.InScratch1 = AN.InScratch;
1942 AN.SplitScratch = oldSplitScratch;
1943 AN.SplitScratchSize = oldSplitScratchSize;
1944 AN.InScratch = oldInScratch;
1946 AR.SortType = oldtype;
1947 if ( w[1] <= FUNHEAD ||
1948 ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
1949 *ps1 = *ps2 = 0;
return(0);
1953 if ( w + s1[1] + s2[1] + 12 + ARGHEAD >= AT.WorkTop ) {
1954 MLOCK(ErrorMessageLock);
1955 MesPrint(
"Program was adding polyfun arguments");
1957 MUNLOCK(ErrorMessageLock);
1964 if ( w[1] <= FUNHEAD ) { *ps1 = *ps2 = 0;
return(0); }
1965 if ( w[1] <= s1[1] || w[1] <= s2[1] ) {
1966 if ( w[1] > s1[1] ) {
1975 while ( t < m ) *s1++ = *t++;
1976 **ps1 = WORDDIF(s1,(*ps1));
1985 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
1987 MesPrint(
"------Garbage collection-------");
1989 AT.WorkPointer += w[1];
1993 if ( (S->sFill + (**ps1 + w[1] - s1[1])) >= S->sTop2 ) {
1996 MLOCK(ErrorMessageLock);
1997 AO.OutFill = AO.OutputLine = OutBuf;
2001 while ( --i >= 0 ) {
2002 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2005 AO.OutFill = AO.OutputLine = OutBuf;
2010 while ( --i >= 0 ) {
2011 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2014 AO.OutFill = AO.OutputLine = OutBuf;
2019 while ( --i >= 0 ) {
2020 TalToLine((UWORD)(*s2++)); TokenToLine((UBYTE *)
" ");
2023 if ( AR.sLevel > 0 ) {
2024 MesPrint(
"Please increase SubSmallExtension setup parameter.");
2027 MesPrint(
"Please increase SmallExtension setup parameter.");
2029 MUNLOCK(ErrorMessageLock);
2031 MLOCK(ErrorMessageLock);
2032 if ( AR.sLevel > 0 ) {
2033 MesPrint(
"Please increase SubSmallExtension setup parameter.");
2036 MesPrint(
"Please increase SmallExtension setup parameter.");
2038 MUNLOCK(ErrorMessageLock);
2052 while ( t < w ) *s2++ = *t++;
2056 if ( *m > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
2057 MLOCK(ErrorMessageLock);
2058 MesPrint(
"Term too complex after polynomial addition. MaxTermSize = %10l",
2059 AM.MaxTer/
sizeof(WORD));
2060 MUNLOCK(ErrorMessageLock);
2063 if ( *m > S->verbMaxTermSize ) S->verbMaxTermSize = *m;
2086 WORD *w = m, *mm, *t, *t1, *t2, *tstop1, *tstop2;
2087 WORD tempterm[8+FUNHEAD];
2089 *m++ = AR.PolyFun; *m++ = 0; FILLFUN(m)
2090 *m++ = 0; *m++ = 0; FILLARG(m)
2091 if ( s1[FUNHEAD] < 0 || s2[FUNHEAD] < 0 ) {
2092 if ( s1[FUNHEAD] < 0 ) {
2093 if ( s2[FUNHEAD] < 0 ) {
2094 if ( s1[FUNHEAD] <= -FUNCTION ) {
2095 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2096 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2098 *m++ = 2; *m++ = 1; *m++ = 3;
2099 INSLENGTH(4+FUNHEAD)
2101 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2102 i1 = functions[-FUNCTION-s1[FUNHEAD]].commute != 0;
2103 i2 = functions[-FUNCTION-s2[FUNHEAD]].commute != 0;
2104 if ( ( !i1 && i2 ) || ( i1 == i2 && i1 > i2 ) ) {
2106 s2[FUNHEAD] = s1[FUNHEAD];
2109 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2111 *m++ = 1; *m++ = 1; *m++ = 3;
2112 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2114 *m++ = 1; *m++ = 1; *m++ = 3;
2115 INSLENGTH(8+2*FUNHEAD)
2117 else if ( s2[FUNHEAD] == -SYMBOL ) {
2118 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2119 *m++ = 1; *m++ = 1; *m++ = 3;
2120 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2122 *m++ = 1; *m++ = 1; *m++ = 3;
2123 INSLENGTH(12+FUNHEAD)
2127 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2128 *m++ = 4+FUNHEAD; *m++ = -s1[FUNHEAD]; *m++ = FUNHEAD;
2130 *m++ = 1; *m++ = 1; *m++ = 3;
2131 INSLENGTH(8+FUNHEAD)
2134 else if ( s1[FUNHEAD] == -SYMBOL ) {
2135 if ( s2[FUNHEAD] == s1[FUNHEAD] ) {
2136 if ( s1[FUNHEAD+1] == s2[FUNHEAD+1] ) {
2137 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1];
2138 *m++ = 1; *m++ = 2; *m++ = 1; *m++ = 3;
2142 if ( s1[FUNHEAD+1] > s2[FUNHEAD+1] )
2143 { i1 = s2[FUNHEAD+1]; i2 = s1[FUNHEAD+1]; }
2144 else { i1 = s1[FUNHEAD+1]; i2 = s2[FUNHEAD+1]; }
2145 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i1;
2146 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2147 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = i2;
2148 *m++ = 1; *m++ = 1; *m++ = 1; *m++ = 3;
2152 else if ( s2[FUNHEAD] <= -FUNCTION ) {
2153 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2154 *m++ = 1; *m++ = 1; *m++ = 3;
2155 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2157 *m++ = 1; *m++ = 1; *m++ = 3;
2158 INSLENGTH(12+FUNHEAD)
2162 *m++ = ABS(s2[FUNHEAD+1]); *m++ = 1; *m++ = s2[FUNHEAD+1] < 0 ? -3: 3;
2163 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s1[FUNHEAD+1]; *m++ = 1;
2164 *m++ = 1; *m++ = 1; *m++ = 3;
2169 if ( s2[FUNHEAD] <= -FUNCTION ) {
2171 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2172 *m++ = 4+FUNHEAD; *m++ = -s2[FUNHEAD]; *m++ = FUNHEAD;
2174 *m++ = 1; *m++ = 1; *m++ = 3;
2175 INSLENGTH(8+FUNHEAD)
2177 else if ( s2[FUNHEAD] == -SYMBOL ) {
2179 *m++ = ABS(s1[FUNHEAD+1]); *m++ = 1; *m++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2180 *m++ = 8; *m++ = SYMBOL; *m++ = 4; *m++ = s2[FUNHEAD+1]; *m++ = 1;
2181 *m++ = 1; *m++ = 1; *m++ = 3;
2186 x1 = (LONG)s1[FUNHEAD+1] + (LONG)s2[FUNHEAD+1];
2187 if ( x1 < 0 ) { i1 = (WORD)(-x1); i2 = -3; }
2188 else { i1 = (WORD)x1; i2 = 3; }
2189 if ( x1 && AN.ncmod != 0 ) {
2194 if ( Modulus(m) ) Terminate(-1);
2195 if ( *m == 0 ) w[1] = 0;
2197 if ( *m == 4 && ( m[1] & MAXPOSITIVE ) == m[1]
2215 else if ( ( i1 & MAXPOSITIVE ) == i1 ) {
2222 *m++ = 4; *m++ = i1; *m++ = 1; *m++ = i2;
2235 if ( s1[FUNHEAD] <= -FUNCTION ) {
2236 *t++ = 4+FUNHEAD; *t++ = -s1[FUNHEAD]; *t++ = FUNHEAD;
2238 *t++ = 1; *t++ = 1; *t++ = 3;
2240 else if ( s1[FUNHEAD] == -SYMBOL ) {
2241 *t++ = 8; *t++ = SYMBOL; *t++ = 4;
2242 *t++ = s1[FUNHEAD+1]; *t++ = 1;
2243 *t++ = 1; *t++ = 1; *t++ = 3;
2246 *t++ = 4; *t++ = ABS(s1[FUNHEAD+1]);
2247 *t++ = 1; *t++ = s1[FUNHEAD+1] < 0 ? -3: 3;
2263 tstop1 = s1 + s1[1];
2264 s1 += FUNHEAD+ARGHEAD;
2266 tstop2 = s2 + s2[1];
2267 s2 += FUNHEAD+ARGHEAD;
2271 oldPolyFlag = AT.SS->PolyFlag;
2272 AT.SS->PolyFlag = 0;
2273 while ( s1 < tstop1 && s2 < tstop2 ) {
2274 i1 = CompareTerms(BHEAD s1,s2,(WORD)(-1));
2279 else if ( i1 < 0 ) {
2302 if ( AddRat(BHEAD (UWORD *)t1,i1,(UWORD *)t2,i2,(UWORD *)m,&i) ) {
2303 MLOCK(ErrorMessageLock);
2304 MesPrint(
"Addition of coefficients of PolyFun");
2305 MUNLOCK(ErrorMessageLock);
2315 *mm = WORDDIF(m,mm);
2316 if ( AN.ncmod != 0 ) {
2317 if ( Modulus(mm) ) Terminate(-1);
2324 while ( s1 < tstop1 ) *m++ = *s1++;
2325 while ( s2 < tstop2 ) *m++ = *s2++;
2326 w[1] = WORDDIF(m,w);
2327 w[FUNHEAD] = w[1] - FUNHEAD;
2328 if ( ToFast(w+FUNHEAD,w+FUNHEAD) ) {
2329 if ( w[FUNHEAD] <= -FUNCTION ) w[1] = FUNHEAD+1;
2330 else w[1] = FUNHEAD+2;
2331 if ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) w[1] = FUNHEAD;
2334 AT.SS->PolyFlag = oldPolyFlag;
2375WORD
Compare1(PHEAD WORD *term1, WORD *term2, WORD level)
2378 WORD *stopper1, *stopper2, *t2;
2380 WORD *stopex1, *stopex2;
2383 WORD count = -1, localPoly, polyhit = -1;
2386 S->verbComparisons++;
2388 if ( S->PolyFlag ) {
2396 count = 0; localPoly = 1; S->PolyWise = polyhit = 0;
2397 S->PolyFlag = AR.PolyFunType;
2398 if ( AR.PolyFunType == 2 &&
2399 ( AR.PolyFunExp == 2 || AR.PolyFunExp == 3 ) ) S->PolyFlag = 1;
2401 else { localPoly = 0; }
2403 AT.SortFloatMode = 0;
2408 GETSTOP(term2,stopper2);
2411 while ( t1 < stopper1 && t2 < stopper2 ) {
2413 if ( *t1 == HAAKJE )
return(PREV(-1));
2414 if ( *t2 == HAAKJE )
return(PREV(1));
2415 if ( *t1 >= (FUNCTION-1) ) {
2416 if ( *t2 < (FUNCTION-1) )
return(PREV(-1));
2417 if ( *t1 < FUNCTION && *t2 < FUNCTION )
return(PREV(*t2-*t1));
2418 if ( *t1 < FUNCTION )
return(PREV(1));
2419 if ( *t2 < FUNCTION )
return(PREV(-1));
2420 c1 = functions[*t1-FUNCTION].commute;
2421 c2 = functions[*t2-FUNCTION].commute;
2423 if ( c2 )
return(PREV(1));
2424 else return(PREV(*t2-*t1));
2427 if ( !c2 )
return(PREV(-1));
2428 else return(PREV(*t2-*t1));
2431 else return(PREV(*t2-*t1));
2438 if ( localPoly && c1 < FUNCTION ) {
2441 if ( c1 <= (FUNCTION-1)
2442 || ( c1 >= FUNCTION && functions[c1-FUNCTION].spec > 0 ) ) {
2443 if ( c1 == SYMBOL ) {
2444 if ( *s1 == FACTORSYMBOL && *s2 == FACTORSYMBOL
2445 && s1[-1] == 4 && s2[-1] == 4
2446 && ( ( t1 < stopper1 && *t1 == HAAKJE )
2447 || ( t1 == stopper1 && AT.fromindex ) ) ) {
2454 if ( s1[1] != s2[1] )
return(s2[1]-s1[1]);
2457 else if ( AR.SortType >= SORTPOWERFIRST ) {
2460 while ( s1 < t1 ) { i1 += s1[1]; s1 += 2; }
2462 while ( s2 < t2 ) { i1 -= s2[1]; s2 += 2; }
2465 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2472 if ( AR.SortType==SORTLOWFIRST ) {
2473 return(PREV((s1[1]>0?-1:1)));
2476 return(PREV((s1[1]<0?-1:1)));
2481 if ( AR.SortType==SORTLOWFIRST ) {
2483 return(PREV((s1[1]<0?1:-1)));
2486 return(PREV((s2[1]<0?-1:1)));
2491 return(PREV((s1[1]<0?-1:1)));
2494 return(PREV((s2[1]<0?1:-1)));
2499 if ( *s1 != *s2 )
return(
2500 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2505 if ( AR.SortType==SORTLOWFIRST ) {
2506 return(PREV((s2[1]<0?-1:1)));
2509 return(PREV((s2[1]<0?1:-1)));
2513 else if ( c1 == DOTPRODUCT ) {
2514 if ( AR.SortType >= SORTPOWERFIRST ) {
2517 while ( s1 < t1 ) { i1 += s1[2]; s1 += 3; }
2519 while ( s2 < t2 ) { i1 -= s2[2]; s2 += 3; }
2522 if ( AR.SortType >= SORTANTIPOWER ) i1 = -i1;
2527 if ( s2 >= t2 )
return(PREV(1));
2528 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2530 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2532 if ( *s1 != *s2 )
return(
2533 PREV((AR.SortType==SORTLOWFIRST?*s2-*s1:*s1-*s2)));
2536 if ( s2 < t2 )
return(PREV(-1));
2540 if ( s2 >= t2 )
return(PREV(1));
2541 if ( *s1 != *s2 )
return(PREV(*s2-*s1));
2544 if ( s2 < t2 )
return(PREV(-1));
2552 if ( localPoly && c1 == AR.PolyFun ) {
2554 if ( S->PolyFlag == 1 ) {
2556 if ( *s1 > 0 ) i1 = *s1;
2557 else if ( *s1 <= -FUNCTION ) i1 = 1;
2559 if ( *s2 > 0 ) i2 = *s2;
2560 else if ( *s2 <= -FUNCTION ) i2 = 1;
2562 if ( s1+i1 == t1 && s2+i2 == t2 ) {
2567 WORD *u1, *u2, *ustop;
2569 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2576 ustop = u2 - ABS(u2[-1]);
2578 while ( u1 < ustop ) {
2579 if ( *u1 == INDEX )
goto NoPoly;
2586 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2593 ustop = u2 - ABS(u2[-1]);
2595 while ( u1 < ustop ) {
2596 if ( *u1 == INDEX )
goto NoPoly;
2603 S->PolyWise = WORDDIF(s1,term1);
2604 S->PolyWise -= FUNHEAD;
2610 S->PolyWise = localPoly = 0;
2613 else if ( AR.PolyFunType == 2 ) {
2614 WORD i1, i2, i1a, i2a;
2615 if ( *s1 > 0 ) i1 = *s1;
2616 else if ( *s1 <= -FUNCTION ) i1 = 1;
2618 if ( *s2 > 0 ) i2 = *s2;
2619 else if ( *s2 <= -FUNCTION ) i2 = 1;
2621 if ( s1[i1] > 0 ) i1a = s1[i1];
2622 else if ( s1[i1] <= -FUNCTION ) i1a = 1;
2624 if ( s2[i2] > 0 ) i2a = s2[i2];
2625 else if ( s2[i2] <= -FUNCTION ) i2a = 1;
2627 if ( s1+i1+i1a == t1 && s2+i2+i2a == t2 ) {
2632 WORD *u1, *u2, *ustop;
2634 if ( *s1 != -SNUMBER && *s1 != -SYMBOL && *s1 > -FUNCTION )
2639 while ( u1 < s1+i1 ) {
2641 ustop = u2 - ABS(u2[-1]);
2643 while ( u1 < ustop ) {
2644 if ( *u1 == INDEX )
goto NoPoly;
2651 if ( s1[i1] != -SNUMBER && s1[i1] != -SYMBOL && s1[i1] > -FUNCTION )
2655 u1 = s1 +i1 + ARGHEAD;
2658 ustop = u2 - ABS(u2[-1]);
2660 while ( u1 < ustop ) {
2661 if ( *u1 == INDEX )
goto NoPoly;
2668 if ( *s2 != -SNUMBER && *s2 != -SYMBOL && *s2 > -FUNCTION )
2673 while ( u1 < s2+i2 ) {
2675 ustop = u2 - ABS(u2[-1]);
2677 while ( u1 < ustop ) {
2678 if ( *u1 == INDEX )
goto NoPoly;
2685 if ( s2[i2] != -SNUMBER && s2[i2] != -SYMBOL && s2[i2] > -FUNCTION )
2689 u1 = s2 + i2 + ARGHEAD;
2692 ustop = u2 - ABS(u2[-1]);
2694 while ( u1 < ustop ) {
2695 if ( *u1 == INDEX )
goto NoPoly;
2702 S->PolyWise = WORDDIF(s1,term1);
2703 S->PolyWise -= FUNHEAD;
2708 S->PolyWise = localPoly = 0;
2712 S->PolyWise = localPoly = 0;
2716 t1 = term1 + S->PolyWise;
2717 t2 = term2 + S->PolyWise;
2724 if ( level == 0 && c1 == FLOATFUN && t1 == stopper1 && t2 == stopper2 && AT.aux_ != 0 ) {
2728 if ( TestFloat(s1-FUNHEAD) ) {
2729 if ( TestFloat(s2-FUNHEAD) ) { AT.SortFloatMode = 3;
return(0); }
2732 else if ( TestFloat(s2-FUNHEAD) ) {
return(-1); }
2739 if ( s2 >= t2 )
return(PREV(-1));
2746 if ( AC.properorderflag ) {
2748 oldpolyflag = S->PolyFlag;
2750 if ( ( c2 = -CompArg(s1,s2) ) != 0 ) {
2751 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2753 S->PolyFlag = oldpolyflag;
2762 if ( s2 >= t2 )
return(PREV(-1));
2764 s1 += ARGHEAD; s2 += ARGHEAD;
2765 oldpolyflag = S->PolyFlag;
2767 while ( s1 < stopex1 ) {
2768 if ( s2 >= stopex2 ) {
2769 S->PolyFlag = oldpolyflag;
return(PREV(-1));
2771 if ( ( c2 = CompareTerms(BHEAD s1,s2,(WORD)1) ) != 0 ) {
2772 S->PolyFlag = oldpolyflag;
return(PREV(c2));
2777 S->PolyFlag = oldpolyflag;
2778 if ( s2 < stopex2 )
return(PREV(1));
2780 else return(PREV(1));
2783 if ( *s2 > 0 )
return(PREV(-1));
2784 if ( *s1 != *s2 ) {
return(PREV(*s1-*s2)); }
2785 if ( *s1 > -FUNCTION ) {
2786 if ( *++s1 != *++s2 ) {
return(PREV(*s2-*s1)); }
2792 if ( s2 < t2 )
return(PREV(1));
2796 if ( level == 0 && t1 < stopper1 && *t1 == FLOATFUN && t1+t1[1] == stopper1
2797 && TestFloat(t1) && AT.aux_ != 0 ) {
2798 AT.SortFloatMode = 1;
return(0);
2800 else if ( level == 0 && t2 < stopper2 && *t2 == FLOATFUN && t2+t2[1] == stopper2
2801 && TestFloat(t2) && AT.aux_ != 0 ) {
2802 AT.SortFloatMode = 2;
return(0);
2806 if ( AR.SortType != SORTLOWFIRST ) {
2807 if ( t1 < stopper1 )
return(PREV(1));
2808 if ( t2 < stopper2 )
return(PREV(-1));
2811 if ( t1 < stopper1 )
return(PREV(-1));
2812 if ( t2 < stopper2 )
return(PREV(1));
2815 if ( level == 3 )
return(
CompCoef(term1,term2));
3111 WORD **pp3, **pp1, **pp2, **pptop;
3112 LONG i, newleft, newright, split;
3114#ifdef SPLITMERGEDEBUG
3116 printf(
"%4ld: ", number);
3117 for (
int ii = 0; ii < S->sTerms; ii++) {
3118 if ( (S->sPointer)[ii] ) {
3119 printf(
"%4d ", (
unsigned)(S->sPointer[ii]-S->sBuffer));
3129 if ( number < 2 )
return(number);
3130 if ( number == 2 ) {
3131 pp1 = Pointer; pp2 = pp1 + 1;
3132 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3133 pp3 = (WORD **)(*pp1); *pp1 = *pp2; *pp2 = (WORD *)pp3;
3135 else if ( i == 0 ) {
3137 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) == 0 ) number = 0; }
3138 else {
if (
AddCoef(BHEAD pp1,pp2) == 0 ) number = 0; }
3142 pptop = Pointer + number;
3145 newright =
SplitMerge(BHEAD Pointer+split,number-split);
3146 if ( newright == 0 )
return(newleft);
3154 if ( newleft > 0 && newright > 0 &&
3155 ( i = CompareTerms(BHEAD Pointer[newleft-1],Pointer[split],(WORD)0) ) >= 0 ) {
3156 pp2 = Pointer+split; pp1 = Pointer+newleft-1;
3158 if ( S->PolyWise ) {
3159 if (
AddPoly(BHEAD pp1,pp2) > 0 ) pp1++;
3163 if (
AddCoef(BHEAD pp1,pp2) > 0 ) pp1++;
3166 *pp2++ = 0; newright--;
3169 newleft += newright;
3171 while ( --newright >= 0 ) *pp1++ = *pp2++;
3172 while ( pp1 < pptop ) *pp1++ = 0;
3177 if ( split >= AN.SplitScratchSize ) {
3178 AN.SplitScratchSize = (split*3)/2+100;
3179 if ( AN.SplitScratchSize > S->Terms2InSmall/2 )
3180 AN.SplitScratchSize = S->Terms2InSmall/2;
3181 if ( AN.SplitScratch ) M_free(AN.SplitScratch,
"AN.SplitScratch");
3182 AN.SplitScratch = (WORD **)Malloc1(AN.SplitScratchSize*
sizeof(WORD *),
"AN.SplitScratch");
3185 pp3 = AN.SplitScratch; pp1 = Pointer;
3187 for ( i = 0; i < newleft; i++ ) { *pp3++ = *pp1; *pp1++ = 0; }
3188 AN.InScratch = newleft;
3189 pp1 = AN.SplitScratch; pp2 = Pointer + split; pp3 = Pointer;
3191#ifdef NEWSPLITMERGETIMSORT
3195 while ( newleft > 8 ) {
3197 LONG nnleft = newleft/2;
3198 if ( ( i = CompareTerms(BHEAD pp1[nnleft],*pp2,(WORD)0) ) < 0 ) {
3204 for (
int iii = 0; iii < nnleft; iii++) {
3210 if ( S->PolyWise ) { i =
AddPoly(BHEAD pp1,pp2); }
3211 else { i =
AddCoef(BHEAD pp1,pp2); }
3229 while ( newleft > 0 && newright > 0 ) {
3230 if ( ( i = CompareTerms(BHEAD *pp1,*pp2,(WORD)0) ) < 0 ) {
3241 if ( S->PolyWise ) {
if (
AddPoly(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3242 else {
if (
AddCoef(BHEAD pp1,pp2) > 0 ) *pp3++ = *pp1; }
3243 *pp1++ = 0; *pp2++ = 0; newleft--; newright--;
3246 for ( i = 0; i < newleft; i++ ) { *pp3++ = *pp1; *pp1++ = 0; }
3250 for ( i = 0; i < newright; i++ ) { *pp3++ = *pp2++; }
3252 newleft = pp3 - Pointer;
3253 while ( pp3 < pptop ) *pp3++ = 0;
3506 WORD **poin, **poin2, ul, k, i, im, *m1;
3507 WORD *p, lpat, mpat, level, l1, l2, r1, r2, r3, c;
3508 WORD *m2, *m3, r31, r33, ki, *rr;
3518 int oldgzipCompress = AR.gzipCompress;
3520 AR.gzipCompress = 0;
3524 fout = &(AR.FoStage4[0]);
3527 poin = S->poina; poin2 = S->poin2a;
3528 rr = AR.CompressPointer;
3535 if ( fout->
handle < 0 ) {
3537 PUTZERO(S->OldPosOut);
3538 if ( ( fhandle = CreateFile(fout->name) ) < 0 ) {
3539 MLOCK(ErrorMessageLock);
3540 MesPrint(
"Cannot create file %s",fout->name);
3541 MUNLOCK(ErrorMessageLock);
3545 MLOCK(ErrorMessageLock);
3546 MesPrint(
"%w MergePatches created output file %s",fout->name);
3547 MUNLOCK(ErrorMessageLock);
3550 PUTZERO(fout->filesize);
3551 PUTZERO(fout->POposition);
3559 LOCK(fout->pthreadslock);
3561 SeekFile(fout->
handle,&(fout->filesize),SEEK_SET);
3563 UNLOCK(fout->pthreadslock);
3566 PUTZERO(S->fPatches[0]);
3567 fout->POfill = fout->PObuffer;
3568 PUTZERO(fout->POposition);
3573 if ( S == AT.S0 && AR.NoCompress == 0 && AR.gzipCompress > 0 )
3574 S->fpcompressed[S->fPatchN] = 1;
3576 S->fpcompressed[S->fPatchN] = 0;
3577 SetupOutputGZIP(fout);
3580 else if ( par == 0 && S->stage4 > 0 ) {
3592 S->iPatches = S->fPatches;
3593 S->fPatches = S->inPatches;
3594 S->inPatches = S->iPatches;
3595 (S->inNum) = S->fPatchN;
3596 S->OldPosIn = S->OldPosOut;
3598 m1 = S->fpincompressed;
3599 S->fpincompressed = S->fpcompressed;
3600 S->fpcompressed = m1;
3601 for ( i = 0; i < S->inNum; i++ ) {
3602 S->fPatchesStop[i] = S->iPatches[i+1];
3604 MLOCK(ErrorMessageLock);
3605 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3606 MUNLOCK(ErrorMessageLock);
3618 AR.gzipCompress = 0;
3621 S->iPatches = S->fPatches;
3622 S->inNum = S->fPatchN;
3624 m1 = S->fpincompressed;
3625 S->fpincompressed = S->fpcompressed;
3626 S->fpcompressed = m1;
3627 for ( i = 0; i < S->inNum; i++ ) {
3628 S->fPatchesStop[i] = S->fPatches[i+1];
3630 MLOCK(ErrorMessageLock);
3631 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
3632 MUNLOCK(ErrorMessageLock);
3640 S->Patches[S->lPatch] = S->lFill;
3641 for ( i = 0; i < S->lPatch; i++ ) {
3642 S->pStop[i] = S->Patches[i+1]-1;
3643 S->Patches[i] = (WORD *)(((UBYTE *)(S->Patches[i])) + AM.MaxTer);
3647 S->lPatch = (S->inNum);
3649 if ( S->lPatch > 1 || ( (PF.exprtodo <0) && (fout == AR.outfile || fout == AR.hidefile ) ) ) {
3651 if ( S->lPatch > 1 ) {
3654 SetupAllInputGZIP(S);
3657 for ( i = 0; i < S->lPatch; i++ ) {
3658 p = (WORD *)(((UBYTE *)p)+2*AM.MaxTer+COMPINC*
sizeof(WORD));
3660 p = (WORD *)(((UBYTE *)p) + fin->POsize);
3661 S->pStop[i] = m2 = p;
3663 PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i);
3665 ADDPOS(S->iPatches[i],
PutIn(fin,&(S->iPatches[i]),S->Patches[i],&m2,i));
3670 if ( fout->
handle >= 0 ) {
3673 LOCK(fout->pthreadslock);
3675 SeekFile(fout->
handle,&position,SEEK_END);
3676 ADDPOS(position,((fout->POfill-fout->PObuffer)*
sizeof(WORD)));
3678 UNLOCK(fout->pthreadslock);
3682 SETBASEPOSITION(position,(fout->POfill-fout->PObuffer)*
sizeof(WORD));
3691 if ( S->lPatch == 1 ) {
3694 if ( fout->
handle < 0 )
if (
Sflush(fout) )
goto PatCall;
3700 m2 = m1 = *S->Patches;
3703 i = -(*m1++); m2 += i; im = *m1+i+1;
3704 while ( i > 0 ) { *m1-- = *m2--; i--; }
3715 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3716 im = PutToMaster(BHEAD m1);
3720 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3721 ADDPOS(S->SizeInFile[par],im);
3726 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3727 PutToMaster(BHEAD 0);
3731 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3732 ADDPOS(S->SizeInFile[par],1);
3735 length = (LONG)(*S->pStop)-(LONG)(*S->Patches)+
sizeof(WORD);
3736 if ( WriteFile(fout->
handle,(UBYTE *)(*S->Patches),length) != length )
3738 ADDPOS(position,length);
3739 ADDPOS(fout->POposition,length);
3740 ADDPOS(fout->filesize,length);
3741 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3751 #define FRONTSIZE (2*AM.MaxTer)
3752 WORD *copybuf = (WORD *)(((UBYTE *)(S->sBuffer)) + FRONTSIZE);
3754 SetupAllInputGZIP(S);
3756 position2 = S->iPatches[0];
3757 while ( ( length = FillInputGZIP(fin,&position2,
3759 (S->SmallEsize*
sizeof(WORD)-FRONTSIZE),0) ) > 0 ) {
3760 copytop = (WORD *)(((UBYTE *)copybuf)+length);
3761 while ( *m1 && ( ( *m1 > 0 && m1+*m1 < copytop ) ||
3762 ( *m1 < 0 && ( m1+1 < copytop ) && ( m1+m1[1]+1 < copytop ) ) ) )
3770 i = -(*m1++); m2 += i; im = *m1+i+1;
3771 while ( i > 0 ) { *m1-- = *m2--; i--; }
3775 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3776 im = PutToMaster(BHEAD m1);
3780 if ( ( im =
PutOut(BHEAD m1,&position,fout,1) ) < 0 )
goto ReturnError;
3781 ADDPOS(S->SizeInFile[par],im);
3785 if ( m1 < copytop && *m1 == 0 )
break;
3791 while ( m1 > m2 ) *--m3 = *--m1;
3796 MLOCK(ErrorMessageLock);
3797 MesPrint(
"Readerror");
3801 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
3802 PutToMaster(BHEAD 0);
3806 if (
FlushOut(&position,fout,1) )
goto ReturnError;
3807 ADDPOS(S->SizeInFile[par],1);
3810 SeekFile(fin->
handle,&(S->iPatches[0]),SEEK_SET);
3811 while ( ( length = ReadFile(fin->
handle,
3812 (UBYTE *)(S->sBuffer),S->SmallEsize*
sizeof(WORD)) ) > 0 ) {
3813 if ( WriteFile(fout->
handle,(UBYTE *)(S->sBuffer),length) != length )
3815 ADDPOS(position,length);
3816 ADDPOS(fout->POposition,length);
3817 ADDPOS(fout->filesize,length);
3818 ADDPOS(S->SizeInFile[par],length/
sizeof(WORD));
3821 MLOCK(ErrorMessageLock);
3822 MesPrint(
"Readerror");
3829 else if ( S->lPatch > 0 ) {
3834 do { lpat *= 2; }
while ( lpat < S->lPatch );
3835 mpat = ( lpat >> 1 ) - 1;
3836 k = lpat - S->lPatch;
3841 for ( i = 1; i < lpat; i++ ) {
3844 for ( i = 1; i <= k; i++ ) {
3846 poin[im] = S->Patches[i-1];
3847 poin2[im] = poin[im] + *(poin[im]);
3850 S->tree[mpat+i] = 0;
3851 poin[im-1] = poin2[im-1] = 0;
3853 for ( i = (k*2)+1; i <= lpat; i++ ) {
3856 poin[i] = S->Patches[i-k-1];
3857 poin2[i] = poin[i] + *(poin[i]);
3877 if ( !*(poin[k]) ) {
3878 do {
if ( !( i >>= 1 ) )
goto EndOfMerge; }
while ( !S->tree[i] );
3879 if ( S->tree[i] == -1 ) {
3892 if ( S->tree[i] > 0 ) {
3893 if ( ( c = CompareTerms(BHEAD poin[S->tree[i]],poin[k],(WORD)0) ) > 0 ) {
3897 S->used[level] = S->tree[i];
3907 l1 = *( m1 = poin[S->tree[i]] );
3908 l2 = *( m2 = poin[k] );
3909 if ( S->PolyWise ) {
3914 if ( S->PolyFlag == 2 ) {
3916 if ( *tt1 + w[1] - m1[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
3917 MLOCK(ErrorMessageLock);
3918 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
3919 MUNLOCK(ErrorMessageLock);
3926 if ( w + m1[1] + m2[1] > AT.WorkTop ) {
3927 MLOCK(ErrorMessageLock);
3928 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
3929 MUNLOCK(ErrorMessageLock);
3936 || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) )
3938 if ( r1 == m1[1] ) {
3941 else if ( r1 < m1[1] ) {
3945 while ( --r1 >= 0 ) *--m1 = *--m2;
3948 while ( --r1 >= 0 ) *--m1 = *--m2;
3950 poin[S->tree[i]] = m1;
3958 poin[S->tree[i]] = m2;
3965 else if ( AT.SortFloatMode ) {
3966 WORD *term1, *term2;
3967 term1 = poin[S->tree[i]];
3969 if ( MergeWithFloat(BHEAD &term1,&term2) == 0 )
3971 poin[S->tree[i]] = term1;
3975 r1 = *( m1 += l1 - 1 );
3977 r1 = ( ( r1 > 0 ) ? (r1-1) : (r1+1) ) >> 1;
3978 r2 = *( m2 += l2 - 1 );
3980 r2 = ( ( r2 > 0 ) ? (r2-1) : (r2+1) ) >> 1;
3982 if ( AddRat(BHEAD (UWORD *)m1,r1,(UWORD *)m2,r2,coef,&r3) ) {
3983 MLOCK(ErrorMessageLock);
3984 MesCall(
"MergePatches");
3985 MUNLOCK(ErrorMessageLock);
3989 if ( AN.ncmod != 0 ) {
3990 if ( ( AC.modmode & POSNEG ) != 0 ) {
3993 else if ( BigLong(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod)) >= 0 ) {
3995 SubPLon(coef,r3,(UWORD *)AC.cmod,ABS(AN.ncmod),coef,&r3);
3997 for ( ii = 1; ii < r3; ii++ ) coef[r3+ii] = 0;
4001 r33 = ( r3 > 0 ) ? ( r3 + 1 ) : ( r3 - 1 );
4002 if ( r3 < 0 ) r3 = -r3;
4003 if ( r1 < 0 ) r1 = -r1;
4008 ul = S->used[level] = S->tree[i];
4015 r1 = poin2[ul][1] - im + 1;
4017 m2 = poin[ul] - im + 1;
4018 while ( ++im <= 0 ) *--m1 = *--m2;
4023 poin[ul] = poin2[ul];
4025 if ( !par && (poin[ul] + im + COMPINC) >= S->pStop[ki]
4028 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[ul]),ki);
4030 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4031 S->Patches[ki],&(poin[ul]),ki));
4033 poin2[ul] = poin[ul] + im;
4038 S->used[++level] = k;
4044 else if ( r31 < 0 ) {
4053 if( (poin[S->tree[i]]+l1+r31) >= poin2[S->tree[i]] ) {
4059 if ( (l1 + r31) > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
4060 MLOCK(ErrorMessageLock);
4061 MesPrint(
"Coefficient overflow during sort");
4062 MUNLOCK(ErrorMessageLock);
4065 m2 = poin[S->tree[i]];
4066 m3 = ( poin[S->tree[i]] -= r31 );
4067 do { *m3++ = *m2++; }
while ( m2 < m1 );
4071 *(poin[S->tree[i]]) += r31;
4073 m2 = (WORD *)coef; im = r3;
4084 r1 = poin2[k][1] - im + 1;
4086 m2 = poin[k] - im + 1;
4087 while ( ++im <= 0 ) *--m1 = *--m2;
4094 if ( !par && ( (poin[k] + im + COMPINC) >= S->pStop[ki] )
4097 PutIn(fin,&(S->iPatches[ki]),S->Patches[ki],&(poin[k]),ki);
4099 ADDPOS(S->iPatches[ki],
PutIn(fin,&(S->iPatches[ki]),
4100 S->Patches[ki],&(poin[k]),ki));
4102 poin2[k] = poin[k] + im;
4110 else if ( S->tree[i] < 0 ) {
4121 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
4122 im = PutToMaster(BHEAD poin[k]);
4126 if ( ( im =
PutOut(BHEAD poin[k],&position,fout,1) ) < 0 ) {
4127 MLOCK(ErrorMessageLock);
4128 MesPrint(
"Called from MergePatches with k = %d (stream %d)",k,S->ktoi[k]);
4129 MUNLOCK(ErrorMessageLock);
4132 ADDPOS(S->SizeInFile[par],im);
4140 if ( AS.MasterSort && ( fout == AR.outfile ) && S == AT.S0 ) {
4141 PutToMaster(BHEAD 0);
4145 if (
FlushOut(&position,fout,1) )
goto ReturnError;
4146 ADDPOS(S->SizeInFile[par],1);
4150 SeekFile(fout->
handle,&position,SEEK_CUR);
4153 S->fPatches[S->fPatchN] = position;
4155 if ( par == 0 && fout != AR.outfile ) {
4164 S->fPatches[S->fPatchN] = position;
4165 if ( ISNOTZEROPOS(S->OldPosIn) ) {
4167 SeekFile(fin->
handle,&(S->OldPosIn),SEEK_SET);
4173 if ( (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->inNum)),(LONG)
sizeof(WORD)) !=
4175 || (ULONG)ReadFile(fin->
handle,(UBYTE *)(&(S->OldPosIn)),(LONG)
sizeof(
POSITION)) !=
4177 || (ULONG)ReadFile(fin->
handle,(UBYTE *)S->iPatches,(LONG)((S->inNum)+1)
4179 MLOCK(ErrorMessageLock);
4180 MesPrint(
"Read error fourth stage sorting");
4181 MUNLOCK(ErrorMessageLock);
4186 for ( i = 0; i < S->inNum; i++ ) {
4187 S->fPatchesStop[i] = S->iPatches[i+1];
4189 MLOCK(ErrorMessageLock);
4190 MesPrint(
"%w fPatchesStop[%d] = %10p",i,&(S->fPatchesStop[i]));
4191 MUNLOCK(ErrorMessageLock);
4213 MLOCK(ErrorMessageLock);
4214 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4215 MUNLOCK(ErrorMessageLock);
4225 {
FILEHANDLE *ff = fin; fin = fout; fout = ff; }
4226 PUTZERO(S->SizeInFile[0]);
4240 MLOCK(ErrorMessageLock);
4241 MesPrint(
"%w MergePatches removed in file %s",fin->name);
4242 MUNLOCK(ErrorMessageLock);
4247 AR.gzipCompress = oldgzipCompress;
4252 AR.gzipCompress = oldgzipCompress;
4257 MLOCK(ErrorMessageLock);
4258 MesPrint(
"Error while writing to file.");
4262 MLOCK(ErrorMessageLock);
4264 MesCall(
"MergePatches");
4265 MUNLOCK(ErrorMessageLock);
4267 AR.gzipCompress = oldgzipCompress;