Lines Matching +full:- +full:p

1 /* Ppmd7.c -- PPMdH codec
2 2018-07-04 : Igor Pavlov : Public domain
18 #define U2I(nu) (p->Units2Indx[(size_t)(nu) - 1])
19 #define I2U(indx) (p->Indx2Units[indx])
24 #define REF(ptr) ((UInt32)((Byte *)(ptr) - (p)->Base))
29 #define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref))
30 #define STATS(ctx) Ppmd7_GetStats(p, ctx)
32 #define SUFFIX(ctx) CTX((ctx)->Suffix)
57 #define NODE(offs) ((CPpmd7_Node *)(p->Base + (offs)))
60 void Ppmd7_Construct(CPpmd7 *p) in Ppmd7_Construct() argument
64 p->Base = 0; in Ppmd7_Construct()
69 do { p->Units2Indx[k++] = (Byte)i; } while (--step); in Ppmd7_Construct()
70 p->Indx2Units[i] = (Byte)k; in Ppmd7_Construct()
73 p->NS2BSIndx[0] = (0 << 1); in Ppmd7_Construct()
74 p->NS2BSIndx[1] = (1 << 1); in Ppmd7_Construct()
75 memset(p->NS2BSIndx + 2, (2 << 1), 9); in Ppmd7_Construct()
76 memset(p->NS2BSIndx + 11, (3 << 1), 256 - 11); in Ppmd7_Construct()
79 p->NS2Indx[i] = (Byte)i; in Ppmd7_Construct()
82 p->NS2Indx[i] = (Byte)m; in Ppmd7_Construct()
83 if (--k == 0) in Ppmd7_Construct()
84 k = (++m) - 2; in Ppmd7_Construct()
87 memset(p->HB2Flag, 0, 0x40); in Ppmd7_Construct()
88 memset(p->HB2Flag + 0x40, 8, 0x100 - 0x40); in Ppmd7_Construct()
91 void Ppmd7_Free(CPpmd7 *p, ISzAllocPtr alloc) in Ppmd7_Free() argument
93 ISzAlloc_Free(alloc, p->Base); in Ppmd7_Free()
94 p->Size = 0; in Ppmd7_Free()
95 p->Base = 0; in Ppmd7_Free()
98 BoolInt Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAllocPtr alloc) in Ppmd7_Alloc() argument
100 if (!p->Base || p->Size != size) in Ppmd7_Alloc()
103 Ppmd7_Free(p, alloc); in Ppmd7_Alloc()
109 p->AlignOffset = in Ppmd7_Alloc()
111 (4 - size) & 3; in Ppmd7_Alloc()
113 4 - (size & 3); in Ppmd7_Alloc()
115 if ((p->Base = (Byte *)ISzAlloc_Alloc(alloc, p->AlignOffset + size + size2)) == 0) in Ppmd7_Alloc()
117 p->Size = size; in Ppmd7_Alloc()
122 static void InsertNode(CPpmd7 *p, void *node, unsigned indx) in InsertNode() argument
124 *((CPpmd_Void_Ref *)node) = p->FreeList[indx]; in InsertNode()
125 p->FreeList[indx] = REF(node); in InsertNode()
128 static void *RemoveNode(CPpmd7 *p, unsigned indx) in RemoveNode() argument
130 CPpmd_Void_Ref *node = (CPpmd_Void_Ref *)Ppmd7_GetPtr(p, p->FreeList[indx]); in RemoveNode()
131 p->FreeList[indx] = *node; in RemoveNode()
135 static void SplitBlock(CPpmd7 *p, void *ptr, unsigned oldIndx, unsigned newIndx) in SplitBlock() argument
137 unsigned i, nu = I2U(oldIndx) - I2U(newIndx); in SplitBlock()
141 unsigned k = I2U(--i); in SplitBlock()
142 InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1); in SplitBlock()
144 InsertNode(p, ptr, i); in SplitBlock()
147 static void GlueFreeBlocks(CPpmd7 *p) in GlueFreeBlocks() argument
153 CPpmd7_Node_Ref head = p->AlignOffset + p->Size; in GlueFreeBlocks()
159 p->GlueCount = 255; in GlueFreeBlocks()
161 /* create doubly-linked list of free blocks */ in GlueFreeBlocks()
165 CPpmd7_Node_Ref next = (CPpmd7_Node_Ref)p->FreeList[i]; in GlueFreeBlocks()
166 p->FreeList[i] = 0; in GlueFreeBlocks()
170 node->Next = n; in GlueFreeBlocks()
171 n = NODE(n)->Prev = next; in GlueFreeBlocks()
173 node->Stamp = 0; in GlueFreeBlocks()
174 node->NU = (UInt16)nu; in GlueFreeBlocks()
177 NODE(head)->Stamp = 1; in GlueFreeBlocks()
178 NODE(head)->Next = n; in GlueFreeBlocks()
179 NODE(n)->Prev = head; in GlueFreeBlocks()
180 if (p->LoUnit != p->HiUnit) in GlueFreeBlocks()
181 ((CPpmd7_Node *)p->LoUnit)->Stamp = 1; in GlueFreeBlocks()
187 UInt32 nu = (UInt32)node->NU; in GlueFreeBlocks()
191 nu += node2->NU; in GlueFreeBlocks()
192 if (node2->Stamp != 0 || nu >= 0x10000) in GlueFreeBlocks()
194 NODE(node2->Prev)->Next = node2->Next; in GlueFreeBlocks()
195 NODE(node2->Next)->Prev = node2->Prev; in GlueFreeBlocks()
196 node->NU = (UInt16)nu; in GlueFreeBlocks()
198 n = node->Next; in GlueFreeBlocks()
202 for (n = NODE(head)->Next; n != head;) in GlueFreeBlocks()
206 CPpmd7_Node_Ref next = node->Next; in GlueFreeBlocks()
207 for (nu = node->NU; nu > 128; nu -= 128, node += 128) in GlueFreeBlocks()
208 InsertNode(p, node, PPMD_NUM_INDEXES - 1); in GlueFreeBlocks()
211 unsigned k = I2U(--i); in GlueFreeBlocks()
212 InsertNode(p, node + k, nu - k - 1); in GlueFreeBlocks()
214 InsertNode(p, node, i); in GlueFreeBlocks()
219 static void *AllocUnitsRare(CPpmd7 *p, unsigned indx) in AllocUnitsRare() argument
223 if (p->GlueCount == 0) in AllocUnitsRare()
225 GlueFreeBlocks(p); in AllocUnitsRare()
226 if (p->FreeList[indx] != 0) in AllocUnitsRare()
227 return RemoveNode(p, indx); in AllocUnitsRare()
235 p->GlueCount--; in AllocUnitsRare()
236 return ((UInt32)(p->UnitsStart - p->Text) > numBytes) ? (p->UnitsStart -= numBytes) : (NULL); in AllocUnitsRare()
239 while (p->FreeList[i] == 0); in AllocUnitsRare()
240 retVal = RemoveNode(p, i); in AllocUnitsRare()
241 SplitBlock(p, retVal, i, indx); in AllocUnitsRare()
245 static void *AllocUnits(CPpmd7 *p, unsigned indx) in AllocUnits() argument
248 if (p->FreeList[indx] != 0) in AllocUnits()
249 return RemoveNode(p, indx); in AllocUnits()
251 if (numBytes <= (UInt32)(p->HiUnit - p->LoUnit)) in AllocUnits()
253 void *retVal = p->LoUnit; in AllocUnits()
254 p->LoUnit += numBytes; in AllocUnits()
257 return AllocUnitsRare(p, indx); in AllocUnits()
262 do { d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; s += 3; d += 3; } while (--n); }
264 static void *ShrinkUnits(CPpmd7 *p, void *oldPtr, unsigned oldNU, unsigned newNU) in ShrinkUnits() argument
270 if (p->FreeList[i1] != 0) in ShrinkUnits()
272 void *ptr = RemoveNode(p, i1); in ShrinkUnits()
274 InsertNode(p, oldPtr, i0); in ShrinkUnits()
277 SplitBlock(p, oldPtr, i0, i1); in ShrinkUnits()
281 #define SUCCESSOR(p) ((CPpmd_Void_Ref)((p)->SuccessorLow | ((UInt32)(p)->SuccessorHigh << 16))) argument
283 static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v) in SetSuccessor() argument
285 (p)->SuccessorLow = (UInt16)((UInt32)(v) & 0xFFFF); in SetSuccessor()
286 (p)->SuccessorHigh = (UInt16)(((UInt32)(v) >> 16) & 0xFFFF); in SetSuccessor()
289 static void RestartModel(CPpmd7 *p) in RestartModel() argument
293 memset(p->FreeList, 0, sizeof(p->FreeList)); in RestartModel()
294 p->Text = p->Base + p->AlignOffset; in RestartModel()
295 p->HiUnit = p->Text + p->Size; in RestartModel()
296 p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE; in RestartModel()
297 p->GlueCount = 0; in RestartModel()
299 p->OrderFall = p->MaxOrder; in RestartModel()
300 p->RunLength = p->InitRL = -(Int32)((p->MaxOrder < 12) ? p->MaxOrder : 12) - 1; in RestartModel()
301 p->PrevSuccess = 0; in RestartModel()
303 p->MinContext = p->MaxContext = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */ in RestartModel()
304 p->MinContext->Suffix = 0; in RestartModel()
305 p->MinContext->NumStats = 256; in RestartModel()
306 p->MinContext->SummFreq = 256 + 1; in RestartModel()
307 p->FoundState = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */ in RestartModel()
308 p->LoUnit += U2B(256 / 2); in RestartModel()
309 p->MinContext->Stats = REF(p->FoundState); in RestartModel()
312 CPpmd_State *s = &p->FoundState[i]; in RestartModel()
313 s->Symbol = (Byte)i; in RestartModel()
314 s->Freq = 1; in RestartModel()
321 UInt16 *dest = p->BinSumm[i] + k; in RestartModel()
322 UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 2)); in RestartModel()
330 CPpmd_See *s = &p->See[i][k]; in RestartModel()
331 s->Summ = (UInt16)((5 * i + 10) << (s->Shift = PPMD_PERIOD_BITS - 4)); in RestartModel()
332 s->Count = 4; in RestartModel()
336 void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder) in Ppmd7_Init() argument
338 p->MaxOrder = maxOrder; in Ppmd7_Init()
339 RestartModel(p); in Ppmd7_Init()
340 p->DummySee.Shift = PPMD_PERIOD_BITS; in Ppmd7_Init()
341 p->DummySee.Summ = 0; /* unused */ in Ppmd7_Init()
342 p->DummySee.Count = 64; /* unused */ in Ppmd7_Init()
345 static CTX_PTR CreateSuccessors(CPpmd7 *p, BoolInt skip) in CreateSuccessors() argument
348 CTX_PTR c = p->MinContext; in CreateSuccessors()
349 CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState); in CreateSuccessors()
354 ps[numPs++] = p->FoundState; in CreateSuccessors()
356 while (c->Suffix) in CreateSuccessors()
361 if (c->NumStats != 1) in CreateSuccessors()
363 for (s = STATS(c); s->Symbol != p->FoundState->Symbol; s++); in CreateSuccessors()
378 upState.Symbol = *(const Byte *)Ppmd7_GetPtr(p, upBranch); in CreateSuccessors()
381 if (c->NumStats == 1) in CreateSuccessors()
382 upState.Freq = ONE_STATE(c)->Freq; in CreateSuccessors()
387 for (s = STATS(c); s->Symbol != upState.Symbol; s++); in CreateSuccessors()
388 cf = s->Freq - 1; in CreateSuccessors()
389 s0 = c->SummFreq - c->NumStats - cf; in CreateSuccessors()
390 … upState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : ((2 * cf + 3 * s0 - 1) / (2 * s0)))); in CreateSuccessors()
396 CTX_PTR c1; /* = AllocContext(p); */ in CreateSuccessors()
397 if (p->HiUnit != p->LoUnit) in CreateSuccessors()
398 c1 = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); in CreateSuccessors()
399 else if (p->FreeList[0] != 0) in CreateSuccessors()
400 c1 = (CTX_PTR)RemoveNode(p, 0); in CreateSuccessors()
403 c1 = (CTX_PTR)AllocUnitsRare(p, 0); in CreateSuccessors()
407 c1->NumStats = 1; in CreateSuccessors()
409 c1->Suffix = REF(c); in CreateSuccessors()
410 SetSuccessor(ps[--numPs], REF(c1)); in CreateSuccessors()
425 static void UpdateModel(CPpmd7 *p) in UpdateModel() argument
427 CPpmd_Void_Ref successor, fSuccessor = SUCCESSOR(p->FoundState); in UpdateModel()
431 if (p->FoundState->Freq < MAX_FREQ / 4 && p->MinContext->Suffix != 0) in UpdateModel()
433 c = SUFFIX(p->MinContext); in UpdateModel()
435 if (c->NumStats == 1) in UpdateModel()
438 if (s->Freq < 32) in UpdateModel()
439 s->Freq++; in UpdateModel()
444 if (s->Symbol != p->FoundState->Symbol) in UpdateModel()
446 do { s++; } while (s->Symbol != p->FoundState->Symbol); in UpdateModel()
447 if (s[0].Freq >= s[-1].Freq) in UpdateModel()
449 SwapStates(&s[0], &s[-1]); in UpdateModel()
450 s--; in UpdateModel()
453 if (s->Freq < MAX_FREQ - 9) in UpdateModel()
455 s->Freq += 2; in UpdateModel()
456 c->SummFreq += 2; in UpdateModel()
461 if (p->OrderFall == 0) in UpdateModel()
463 p->MinContext = p->MaxContext = CreateSuccessors(p, True); in UpdateModel()
464 if (p->MinContext == 0) in UpdateModel()
466 RestartModel(p); in UpdateModel()
469 SetSuccessor(p->FoundState, REF(p->MinContext)); in UpdateModel()
473 *p->Text++ = p->FoundState->Symbol; in UpdateModel()
474 successor = REF(p->Text); in UpdateModel()
475 if (p->Text >= p->UnitsStart) in UpdateModel()
477 RestartModel(p); in UpdateModel()
485 CTX_PTR cs = CreateSuccessors(p, False); in UpdateModel()
488 RestartModel(p); in UpdateModel()
493 if (--p->OrderFall == 0) in UpdateModel()
496 p->Text -= (p->MaxContext != p->MinContext); in UpdateModel()
501 SetSuccessor(p->FoundState, successor); in UpdateModel()
502 fSuccessor = REF(p->MinContext); in UpdateModel()
505 s0 = p->MinContext->SummFreq - (ns = p->MinContext->NumStats) - (p->FoundState->Freq - 1); in UpdateModel()
507 for (c = p->MaxContext; c != p->MinContext; c = SUFFIX(c)) in UpdateModel()
511 if ((ns1 = c->NumStats) != 1) in UpdateModel()
520 void *ptr = AllocUnits(p, i + 1); in UpdateModel()
524 RestartModel(p); in UpdateModel()
529 InsertNode(p, oldPtr, i); in UpdateModel()
530 c->Stats = STATS_REF(ptr); in UpdateModel()
533 …c->SummFreq = (UInt16)(c->SummFreq + (2 * ns1 < ns) + 2 * ((4 * ns1 <= ns) & (c->SummFreq <= 8 * n… in UpdateModel()
537 CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0); in UpdateModel()
540 RestartModel(p); in UpdateModel()
544 c->Stats = REF(s); in UpdateModel()
545 if (s->Freq < MAX_FREQ / 4 - 1) in UpdateModel()
546 s->Freq <<= 1; in UpdateModel()
548 s->Freq = MAX_FREQ - 4; in UpdateModel()
549 c->SummFreq = (UInt16)(s->Freq + p->InitEsc + (ns > 3)); in UpdateModel()
551 cf = 2 * (UInt32)p->FoundState->Freq * (c->SummFreq + 6); in UpdateModel()
552 sf = (UInt32)s0 + c->SummFreq; in UpdateModel()
556 c->SummFreq += 3; in UpdateModel()
561 c->SummFreq = (UInt16)(c->SummFreq + cf); in UpdateModel()
566 s->Symbol = p->FoundState->Symbol; in UpdateModel()
567 s->Freq = (Byte)cf; in UpdateModel()
568 c->NumStats = (UInt16)(ns1 + 1); in UpdateModel()
571 p->MaxContext = p->MinContext = CTX(fSuccessor); in UpdateModel()
574 static void Rescale(CPpmd7 *p) in Rescale() argument
577 CPpmd_State *stats = STATS(p->MinContext); in Rescale()
578 CPpmd_State *s = p->FoundState; in Rescale()
581 for (; s != stats; s--) in Rescale()
582 s[0] = s[-1]; in Rescale()
585 escFreq = p->MinContext->SummFreq - s->Freq; in Rescale()
586 s->Freq += 4; in Rescale()
587 adder = (p->OrderFall != 0); in Rescale()
588 s->Freq = (Byte)((s->Freq + adder) >> 1); in Rescale()
589 sumFreq = s->Freq; in Rescale()
591 i = p->MinContext->NumStats - 1; in Rescale()
594 escFreq -= (++s)->Freq; in Rescale()
595 s->Freq = (Byte)((s->Freq + adder) >> 1); in Rescale()
596 sumFreq += s->Freq; in Rescale()
597 if (s[0].Freq > s[-1].Freq) in Rescale()
602 s1[0] = s1[-1]; in Rescale()
603 while (--s1 != stats && tmp.Freq > s1[-1].Freq); in Rescale()
607 while (--i); in Rescale()
609 if (s->Freq == 0) in Rescale()
611 unsigned numStats = p->MinContext->NumStats; in Rescale()
613 do { i++; } while ((--s)->Freq == 0); in Rescale()
615 p->MinContext->NumStats = (UInt16)(p->MinContext->NumStats - i); in Rescale()
616 if (p->MinContext->NumStats == 1) in Rescale()
621 tmp.Freq = (Byte)(tmp.Freq - (tmp.Freq >> 1)); in Rescale()
625 InsertNode(p, stats, U2I(((numStats + 1) >> 1))); in Rescale()
626 *(p->FoundState = ONE_STATE(p->MinContext)) = tmp; in Rescale()
630 n1 = (p->MinContext->NumStats + 1) >> 1; in Rescale()
632 p->MinContext->Stats = STATS_REF(ShrinkUnits(p, stats, n0, n1)); in Rescale()
634 p->MinContext->SummFreq = (UInt16)(sumFreq + escFreq - (escFreq >> 1)); in Rescale()
635 p->FoundState = STATS(p->MinContext); in Rescale()
638 CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *escFreq) in Ppmd7_MakeEscFreq() argument
641 unsigned nonMasked = p->MinContext->NumStats - numMasked; in Ppmd7_MakeEscFreq()
642 if (p->MinContext->NumStats != 256) in Ppmd7_MakeEscFreq()
644 see = p->See[(unsigned)p->NS2Indx[(size_t)nonMasked - 1]] + in Ppmd7_MakeEscFreq()
645 (nonMasked < (unsigned)SUFFIX(p->MinContext)->NumStats - p->MinContext->NumStats) + in Ppmd7_MakeEscFreq()
646 2 * (unsigned)(p->MinContext->SummFreq < 11 * p->MinContext->NumStats) + in Ppmd7_MakeEscFreq()
648 p->HiBitsFlag; in Ppmd7_MakeEscFreq()
650 unsigned r = (see->Summ >> see->Shift); in Ppmd7_MakeEscFreq()
651 see->Summ = (UInt16)(see->Summ - r); in Ppmd7_MakeEscFreq()
657 see = &p->DummySee; in Ppmd7_MakeEscFreq()
663 static void NextContext(CPpmd7 *p) in NextContext() argument
665 CTX_PTR c = CTX(SUCCESSOR(p->FoundState)); in NextContext()
666 if (p->OrderFall == 0 && (Byte *)c > p->Text) in NextContext()
667 p->MinContext = p->MaxContext = c; in NextContext()
669 UpdateModel(p); in NextContext()
672 void Ppmd7_Update1(CPpmd7 *p) in Ppmd7_Update1() argument
674 CPpmd_State *s = p->FoundState; in Ppmd7_Update1()
675 s->Freq += 4; in Ppmd7_Update1()
676 p->MinContext->SummFreq += 4; in Ppmd7_Update1()
677 if (s[0].Freq > s[-1].Freq) in Ppmd7_Update1()
679 SwapStates(&s[0], &s[-1]); in Ppmd7_Update1()
680 p->FoundState = --s; in Ppmd7_Update1()
681 if (s->Freq > MAX_FREQ) in Ppmd7_Update1()
682 Rescale(p); in Ppmd7_Update1()
684 NextContext(p); in Ppmd7_Update1()
687 void Ppmd7_Update1_0(CPpmd7 *p) in Ppmd7_Update1_0() argument
689 p->PrevSuccess = (2 * p->FoundState->Freq > p->MinContext->SummFreq); in Ppmd7_Update1_0()
690 p->RunLength += p->PrevSuccess; in Ppmd7_Update1_0()
691 p->MinContext->SummFreq += 4; in Ppmd7_Update1_0()
692 if ((p->FoundState->Freq += 4) > MAX_FREQ) in Ppmd7_Update1_0()
693 Rescale(p); in Ppmd7_Update1_0()
694 NextContext(p); in Ppmd7_Update1_0()
697 void Ppmd7_UpdateBin(CPpmd7 *p) in Ppmd7_UpdateBin() argument
699 p->FoundState->Freq = (Byte)(p->FoundState->Freq + (p->FoundState->Freq < 128 ? 1: 0)); in Ppmd7_UpdateBin()
700 p->PrevSuccess = 1; in Ppmd7_UpdateBin()
701 p->RunLength++; in Ppmd7_UpdateBin()
702 NextContext(p); in Ppmd7_UpdateBin()
705 void Ppmd7_Update2(CPpmd7 *p) in Ppmd7_Update2() argument
707 p->MinContext->SummFreq += 4; in Ppmd7_Update2()
708 if ((p->FoundState->Freq += 4) > MAX_FREQ) in Ppmd7_Update2()
709 Rescale(p); in Ppmd7_Update2()
710 p->RunLength = p->InitRL; in Ppmd7_Update2()
711 UpdateModel(p); in Ppmd7_Update2()