/src/lzma-fuzz/sdk/C/Ppmd7.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Ppmd7.c -- PPMdH codec |
2 | | 2018-07-04 : Igor Pavlov : Public domain |
3 | | This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */ |
4 | | |
5 | | #include "Precomp.h" |
6 | | |
7 | | #include <string.h> |
8 | | |
9 | | #include "Ppmd7.h" |
10 | | |
11 | | const Byte PPMD7_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 }; |
12 | | static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051}; |
13 | | |
14 | 20.8M | #define MAX_FREQ 124 |
15 | 3.28M | #define UNIT_SIZE 12 |
16 | | |
17 | 2.18M | #define U2B(nu) ((UInt32)(nu) * UNIT_SIZE) |
18 | 768k | #define U2I(nu) (p->Units2Indx[(size_t)(nu) - 1]) |
19 | 3.61M | #define I2U(indx) (p->Indx2Units[indx]) |
20 | | |
21 | | #ifdef PPMD_32BIT |
22 | | #define REF(ptr) (ptr) |
23 | | #else |
24 | 24.9M | #define REF(ptr) ((UInt32)((Byte *)(ptr) - (p)->Base)) |
25 | | #endif |
26 | | |
27 | 328k | #define STATS_REF(ptr) ((CPpmd_State_Ref)REF(ptr)) |
28 | | |
29 | 29.1M | #define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) |
30 | 3.87M | #define STATS(ctx) Ppmd7_GetStats(p, ctx) |
31 | 3.17M | #define ONE_STATE(ctx) Ppmd7Context_OneState(ctx) |
32 | 3.36M | #define SUFFIX(ctx) CTX((ctx)->Suffix) |
33 | | |
34 | | typedef CPpmd7_Context * CTX_PTR; |
35 | | |
36 | | struct CPpmd7_Node_; |
37 | | |
38 | | typedef |
39 | | #ifdef PPMD_32BIT |
40 | | struct CPpmd7_Node_ * |
41 | | #else |
42 | | UInt32 |
43 | | #endif |
44 | | CPpmd7_Node_Ref; |
45 | | |
46 | | typedef struct CPpmd7_Node_ |
47 | | { |
48 | | UInt16 Stamp; /* must be at offset 0 as CPpmd7_Context::NumStats. Stamp=0 means free */ |
49 | | UInt16 NU; |
50 | | CPpmd7_Node_Ref Next; /* must be at offset >= 4 */ |
51 | | CPpmd7_Node_Ref Prev; |
52 | | } CPpmd7_Node; |
53 | | |
54 | | #ifdef PPMD_32BIT |
55 | | #define NODE(ptr) (ptr) |
56 | | #else |
57 | 658k | #define NODE(offs) ((CPpmd7_Node *)(p->Base + (offs))) |
58 | | #endif |
59 | | |
60 | | void Ppmd7_Construct(CPpmd7 *p) |
61 | 3.24k | { |
62 | 3.24k | unsigned i, k, m; |
63 | | |
64 | 3.24k | p->Base = 0; |
65 | | |
66 | 126k | for (i = 0, k = 0; i < PPMD_NUM_INDEXES; i++) |
67 | 123k | { |
68 | 123k | unsigned step = (i >= 12 ? 4 : (i >> 2) + 1); |
69 | 415k | do { p->Units2Indx[k++] = (Byte)i; } while (--step); |
70 | 123k | p->Indx2Units[i] = (Byte)k; |
71 | 123k | } |
72 | | |
73 | 3.24k | p->NS2BSIndx[0] = (0 << 1); |
74 | 3.24k | p->NS2BSIndx[1] = (1 << 1); |
75 | 3.24k | memset(p->NS2BSIndx + 2, (2 << 1), 9); |
76 | 3.24k | memset(p->NS2BSIndx + 11, (3 << 1), 256 - 11); |
77 | | |
78 | 12.9k | for (i = 0; i < 3; i++) |
79 | 9.73k | p->NS2Indx[i] = (Byte)i; |
80 | 824k | for (m = i, k = 1; i < 256; i++) |
81 | 820k | { |
82 | 820k | p->NS2Indx[i] = (Byte)m; |
83 | 820k | if (--k == 0) |
84 | 71.3k | k = (++m) - 2; |
85 | 820k | } |
86 | | |
87 | 3.24k | memset(p->HB2Flag, 0, 0x40); |
88 | 3.24k | memset(p->HB2Flag + 0x40, 8, 0x100 - 0x40); |
89 | 3.24k | } |
90 | | |
91 | | void Ppmd7_Free(CPpmd7 *p, ISzAllocPtr alloc) |
92 | 6.49k | { |
93 | 6.49k | ISzAlloc_Free(alloc, p->Base); |
94 | 6.49k | p->Size = 0; |
95 | 6.49k | p->Base = 0; |
96 | 6.49k | } |
97 | | |
98 | | BoolInt Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAllocPtr alloc) |
99 | 3.24k | { |
100 | 3.24k | if (!p->Base || p->Size != size) |
101 | 3.24k | { |
102 | 3.24k | size_t size2; |
103 | 3.24k | Ppmd7_Free(p, alloc); |
104 | 3.24k | size2 = 0 |
105 | 3.24k | #ifndef PPMD_32BIT |
106 | 3.24k | + UNIT_SIZE |
107 | 3.24k | #endif |
108 | 3.24k | ; |
109 | 3.24k | p->AlignOffset = |
110 | | #ifdef PPMD_32BIT |
111 | | (4 - size) & 3; |
112 | | #else |
113 | 3.24k | 4 - (size & 3); |
114 | 3.24k | #endif |
115 | 3.24k | if ((p->Base = (Byte *)ISzAlloc_Alloc(alloc, p->AlignOffset + size + size2)) == 0) |
116 | 0 | return False; |
117 | 3.24k | p->Size = size; |
118 | 3.24k | } |
119 | 3.24k | return True; |
120 | 3.24k | } |
121 | | |
122 | | static void InsertNode(CPpmd7 *p, void *node, unsigned indx) |
123 | 474k | { |
124 | 474k | *((CPpmd_Void_Ref *)node) = p->FreeList[indx]; |
125 | 474k | p->FreeList[indx] = REF(node); |
126 | 474k | } |
127 | | |
128 | | static void *RemoveNode(CPpmd7 *p, unsigned indx) |
129 | 324k | { |
130 | 324k | CPpmd_Void_Ref *node = (CPpmd_Void_Ref *)Ppmd7_GetPtr(p, p->FreeList[indx]); |
131 | 324k | p->FreeList[indx] = *node; |
132 | 324k | return node; |
133 | 324k | } |
134 | | |
135 | | static void SplitBlock(CPpmd7 *p, void *ptr, unsigned oldIndx, unsigned newIndx) |
136 | 83.8k | { |
137 | 83.8k | unsigned i, nu = I2U(oldIndx) - I2U(newIndx); |
138 | 83.8k | ptr = (Byte *)ptr + U2B(I2U(newIndx)); |
139 | 83.8k | if (I2U(i = U2I(nu)) != nu) |
140 | 2.62k | { |
141 | 2.62k | unsigned k = I2U(--i); |
142 | 2.62k | InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1); |
143 | 2.62k | } |
144 | 83.8k | InsertNode(p, ptr, i); |
145 | 83.8k | } |
146 | | |
147 | | static void GlueFreeBlocks(CPpmd7 *p) |
148 | 87.4k | { |
149 | | #ifdef PPMD_32BIT |
150 | | CPpmd7_Node headItem; |
151 | | CPpmd7_Node_Ref head = &headItem; |
152 | | #else |
153 | 87.4k | CPpmd7_Node_Ref head = p->AlignOffset + p->Size; |
154 | 87.4k | #endif |
155 | | |
156 | 87.4k | CPpmd7_Node_Ref n = head; |
157 | 87.4k | unsigned i; |
158 | | |
159 | 87.4k | p->GlueCount = 255; |
160 | | |
161 | | /* create doubly-linked list of free blocks */ |
162 | 3.41M | for (i = 0; i < PPMD_NUM_INDEXES; i++) |
163 | 3.32M | { |
164 | 3.32M | UInt16 nu = I2U(i); |
165 | 3.32M | CPpmd7_Node_Ref next = (CPpmd7_Node_Ref)p->FreeList[i]; |
166 | 3.32M | p->FreeList[i] = 0; |
167 | 3.37M | while (next != 0) |
168 | 55.7k | { |
169 | 55.7k | CPpmd7_Node *node = NODE(next); |
170 | 55.7k | node->Next = n; |
171 | 55.7k | n = NODE(n)->Prev = next; |
172 | 55.7k | next = *(const CPpmd7_Node_Ref *)node; |
173 | 55.7k | node->Stamp = 0; |
174 | 55.7k | node->NU = (UInt16)nu; |
175 | 55.7k | } |
176 | 3.32M | } |
177 | 87.4k | NODE(head)->Stamp = 1; |
178 | 87.4k | NODE(head)->Next = n; |
179 | 87.4k | NODE(n)->Prev = head; |
180 | 87.4k | if (p->LoUnit != p->HiUnit) |
181 | 6.35k | ((CPpmd7_Node *)p->LoUnit)->Stamp = 1; |
182 | | |
183 | | /* Glue free blocks */ |
184 | 131k | while (n != head) |
185 | 43.6k | { |
186 | 43.6k | CPpmd7_Node *node = NODE(n); |
187 | 43.6k | UInt32 nu = (UInt32)node->NU; |
188 | 43.6k | for (;;) |
189 | 70.9k | { |
190 | 70.9k | CPpmd7_Node *node2 = NODE(n) + nu; |
191 | 70.9k | nu += node2->NU; |
192 | 70.9k | if (node2->Stamp != 0 || nu >= 0x10000) |
193 | 43.6k | break; |
194 | 27.2k | NODE(node2->Prev)->Next = node2->Next; |
195 | 27.2k | NODE(node2->Next)->Prev = node2->Prev; |
196 | 27.2k | node->NU = (UInt16)nu; |
197 | 27.2k | } |
198 | 43.6k | n = node->Next; |
199 | 43.6k | } |
200 | | |
201 | | /* Fill lists of free blocks */ |
202 | 115k | for (n = NODE(head)->Next; n != head;) |
203 | 28.4k | { |
204 | 28.4k | CPpmd7_Node *node = NODE(n); |
205 | 28.4k | unsigned nu; |
206 | 28.4k | CPpmd7_Node_Ref next = node->Next; |
207 | 28.4k | for (nu = node->NU; nu > 128; nu -= 128, node += 128) |
208 | 0 | InsertNode(p, node, PPMD_NUM_INDEXES - 1); |
209 | 28.4k | if (I2U(i = U2I(nu)) != nu) |
210 | 4.37k | { |
211 | 4.37k | unsigned k = I2U(--i); |
212 | 4.37k | InsertNode(p, node + k, nu - k - 1); |
213 | 4.37k | } |
214 | 28.4k | InsertNode(p, node, i); |
215 | 28.4k | n = next; |
216 | 28.4k | } |
217 | 87.4k | } |
218 | | |
219 | | static void *AllocUnitsRare(CPpmd7 *p, unsigned indx) |
220 | 989k | { |
221 | 989k | unsigned i; |
222 | 989k | void *retVal; |
223 | 989k | if (p->GlueCount == 0) |
224 | 87.4k | { |
225 | 87.4k | GlueFreeBlocks(p); |
226 | 87.4k | if (p->FreeList[indx] != 0) |
227 | 4.52k | return RemoveNode(p, indx); |
228 | 87.4k | } |
229 | 984k | i = indx; |
230 | 984k | do |
231 | 34.1M | { |
232 | 34.1M | if (++i == PPMD_NUM_INDEXES) |
233 | 905k | { |
234 | 905k | UInt32 numBytes = U2B(I2U(indx)); |
235 | 905k | p->GlueCount--; |
236 | 905k | return ((UInt32)(p->UnitsStart - p->Text) > numBytes) ? (p->UnitsStart -= numBytes) : (NULL); |
237 | 905k | } |
238 | 34.1M | } |
239 | 33.2M | while (p->FreeList[i] == 0); |
240 | 79.4k | retVal = RemoveNode(p, i); |
241 | 79.4k | SplitBlock(p, retVal, i, indx); |
242 | 79.4k | return retVal; |
243 | 984k | } |
244 | | |
245 | | static void *AllocUnits(CPpmd7 *p, unsigned indx) |
246 | 1.24M | { |
247 | 1.24M | UInt32 numBytes; |
248 | 1.24M | if (p->FreeList[indx] != 0) |
249 | 137k | return RemoveNode(p, indx); |
250 | 1.10M | numBytes = U2B(I2U(indx)); |
251 | 1.10M | if (numBytes <= (UInt32)(p->HiUnit - p->LoUnit)) |
252 | 615k | { |
253 | 615k | void *retVal = p->LoUnit; |
254 | 615k | p->LoUnit += numBytes; |
255 | 615k | return retVal; |
256 | 615k | } |
257 | 491k | return AllocUnitsRare(p, indx); |
258 | 1.10M | } |
259 | | |
260 | | #define MyMem12Cpy(dest, src, num) \ |
261 | 323k | { UInt32 *d = (UInt32 *)dest; const UInt32 *s = (const UInt32 *)src; UInt32 n = num; \ |
262 | 526k | do { d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; s += 3; d += 3; } while (--n); } |
263 | | |
264 | | static void *ShrinkUnits(CPpmd7 *p, void *oldPtr, unsigned oldNU, unsigned newNU) |
265 | 14.9k | { |
266 | 14.9k | unsigned i0 = U2I(oldNU); |
267 | 14.9k | unsigned i1 = U2I(newNU); |
268 | 14.9k | if (i0 == i1) |
269 | 398 | return oldPtr; |
270 | 14.5k | if (p->FreeList[i1] != 0) |
271 | 10.1k | { |
272 | 10.1k | void *ptr = RemoveNode(p, i1); |
273 | 10.1k | MyMem12Cpy(ptr, oldPtr, newNU); |
274 | 10.1k | InsertNode(p, oldPtr, i0); |
275 | 10.1k | return ptr; |
276 | 10.1k | } |
277 | 4.40k | SplitBlock(p, oldPtr, i0, i1); |
278 | 4.40k | return oldPtr; |
279 | 14.5k | } |
280 | | |
281 | 12.6M | #define SUCCESSOR(p) ((CPpmd_Void_Ref)((p)->SuccessorLow | ((UInt32)(p)->SuccessorHigh << 16))) |
282 | | |
283 | | static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v) |
284 | 35.7M | { |
285 | 35.7M | (p)->SuccessorLow = (UInt16)((UInt32)(v) & 0xFFFF); |
286 | 35.7M | (p)->SuccessorHigh = (UInt16)(((UInt32)(v) >> 16) & 0xFFFF); |
287 | 35.7M | } |
288 | | |
289 | | static void RestartModel(CPpmd7 *p) |
290 | 89.5k | { |
291 | 89.5k | unsigned i, k, m; |
292 | | |
293 | 89.5k | memset(p->FreeList, 0, sizeof(p->FreeList)); |
294 | 89.5k | p->Text = p->Base + p->AlignOffset; |
295 | 89.5k | p->HiUnit = p->Text + p->Size; |
296 | 89.5k | p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE; |
297 | 89.5k | p->GlueCount = 0; |
298 | | |
299 | 89.5k | p->OrderFall = p->MaxOrder; |
300 | 89.5k | p->RunLength = p->InitRL = -(Int32)((p->MaxOrder < 12) ? p->MaxOrder : 12) - 1; |
301 | 89.5k | p->PrevSuccess = 0; |
302 | | |
303 | 89.5k | p->MinContext = p->MaxContext = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */ |
304 | 89.5k | p->MinContext->Suffix = 0; |
305 | 89.5k | p->MinContext->NumStats = 256; |
306 | 89.5k | p->MinContext->SummFreq = 256 + 1; |
307 | 89.5k | p->FoundState = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */ |
308 | 89.5k | p->LoUnit += U2B(256 / 2); |
309 | 89.5k | p->MinContext->Stats = REF(p->FoundState); |
310 | 23.0M | for (i = 0; i < 256; i++) |
311 | 22.9M | { |
312 | 22.9M | CPpmd_State *s = &p->FoundState[i]; |
313 | 22.9M | s->Symbol = (Byte)i; |
314 | 22.9M | s->Freq = 1; |
315 | 22.9M | SetSuccessor(s, 0); |
316 | 22.9M | } |
317 | | |
318 | 11.5M | for (i = 0; i < 128; i++) |
319 | 103M | for (k = 0; k < 8; k++) |
320 | 91.7M | { |
321 | 91.7M | UInt16 *dest = p->BinSumm[i] + k; |
322 | 91.7M | UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 2)); |
323 | 825M | for (m = 0; m < 64; m += 8) |
324 | 733M | dest[m] = val; |
325 | 91.7M | } |
326 | | |
327 | 2.32M | for (i = 0; i < 25; i++) |
328 | 38.0M | for (k = 0; k < 16; k++) |
329 | 35.8M | { |
330 | 35.8M | CPpmd_See *s = &p->See[i][k]; |
331 | 35.8M | s->Summ = (UInt16)((5 * i + 10) << (s->Shift = PPMD_PERIOD_BITS - 4)); |
332 | 35.8M | s->Count = 4; |
333 | 35.8M | } |
334 | 89.5k | } |
335 | | |
336 | | void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder) |
337 | 3.23k | { |
338 | 3.23k | p->MaxOrder = maxOrder; |
339 | 3.23k | RestartModel(p); |
340 | 3.23k | p->DummySee.Shift = PPMD_PERIOD_BITS; |
341 | 3.23k | p->DummySee.Summ = 0; /* unused */ |
342 | 3.23k | p->DummySee.Count = 64; /* unused */ |
343 | 3.23k | } |
344 | | |
345 | | static CTX_PTR CreateSuccessors(CPpmd7 *p, BoolInt skip) |
346 | 1.13M | { |
347 | 1.13M | CPpmd_State upState; |
348 | 1.13M | CTX_PTR c = p->MinContext; |
349 | 1.13M | CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState); |
350 | 1.13M | CPpmd_State *ps[PPMD7_MAX_ORDER]; |
351 | 1.13M | unsigned numPs = 0; |
352 | | |
353 | 1.13M | if (!skip) |
354 | 979k | ps[numPs++] = p->FoundState; |
355 | | |
356 | 1.58M | while (c->Suffix) |
357 | 895k | { |
358 | 895k | CPpmd_Void_Ref successor; |
359 | 895k | CPpmd_State *s; |
360 | 895k | c = SUFFIX(c); |
361 | 895k | if (c->NumStats != 1) |
362 | 483k | { |
363 | 17.8M | for (s = STATS(c); s->Symbol != p->FoundState->Symbol; s++); |
364 | 483k | } |
365 | 411k | else |
366 | 411k | s = ONE_STATE(c); |
367 | 895k | successor = SUCCESSOR(s); |
368 | 895k | if (successor != upBranch) |
369 | 437k | { |
370 | 437k | c = CTX(successor); |
371 | 437k | if (numPs == 0) |
372 | 61.4k | return c; |
373 | 376k | break; |
374 | 437k | } |
375 | 457k | ps[numPs++] = s; |
376 | 457k | } |
377 | | |
378 | 1.07M | upState.Symbol = *(const Byte *)Ppmd7_GetPtr(p, upBranch); |
379 | 1.07M | SetSuccessor(&upState, upBranch + 1); |
380 | | |
381 | 1.07M | if (c->NumStats == 1) |
382 | 205k | upState.Freq = ONE_STATE(c)->Freq; |
383 | 865k | else |
384 | 865k | { |
385 | 865k | UInt32 cf, s0; |
386 | 865k | CPpmd_State *s; |
387 | 75.6M | for (s = STATS(c); s->Symbol != upState.Symbol; s++); |
388 | 865k | cf = s->Freq - 1; |
389 | 865k | s0 = c->SummFreq - c->NumStats - cf; |
390 | 865k | upState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : ((2 * cf + 3 * s0 - 1) / (2 * s0)))); |
391 | 865k | } |
392 | | |
393 | 1.07M | do |
394 | 1.41M | { |
395 | | /* Create Child */ |
396 | 1.41M | CTX_PTR c1; /* = AllocContext(p); */ |
397 | 1.41M | if (p->HiUnit != p->LoUnit) |
398 | 822k | c1 = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); |
399 | 589k | else if (p->FreeList[0] != 0) |
400 | 92.4k | c1 = (CTX_PTR)RemoveNode(p, 0); |
401 | 497k | else |
402 | 497k | { |
403 | 497k | c1 = (CTX_PTR)AllocUnitsRare(p, 0); |
404 | 497k | if (!c1) |
405 | 37.4k | return NULL; |
406 | 497k | } |
407 | 1.37M | c1->NumStats = 1; |
408 | 1.37M | *ONE_STATE(c1) = upState; |
409 | 1.37M | c1->Suffix = REF(c); |
410 | 1.37M | SetSuccessor(ps[--numPs], REF(c1)); |
411 | 1.37M | c = c1; |
412 | 1.37M | } |
413 | 1.37M | while (numPs != 0); |
414 | | |
415 | 1.03M | return c; |
416 | 1.07M | } |
417 | | |
418 | | static void SwapStates(CPpmd_State *t1, CPpmd_State *t2) |
419 | 5.10M | { |
420 | 5.10M | CPpmd_State tmp = *t1; |
421 | 5.10M | *t1 = *t2; |
422 | 5.10M | *t2 = tmp; |
423 | 5.10M | } |
424 | | |
425 | | static void UpdateModel(CPpmd7 *p) |
426 | 10.6M | { |
427 | 10.6M | CPpmd_Void_Ref successor, fSuccessor = SUCCESSOR(p->FoundState); |
428 | 10.6M | CTX_PTR c; |
429 | 10.6M | unsigned s0, ns; |
430 | | |
431 | 10.6M | if (p->FoundState->Freq < MAX_FREQ / 4 && p->MinContext->Suffix != 0) |
432 | 741k | { |
433 | 741k | c = SUFFIX(p->MinContext); |
434 | | |
435 | 741k | if (c->NumStats == 1) |
436 | 261k | { |
437 | 261k | CPpmd_State *s = ONE_STATE(c); |
438 | 261k | if (s->Freq < 32) |
439 | 261k | s->Freq++; |
440 | 261k | } |
441 | 479k | else |
442 | 479k | { |
443 | 479k | CPpmd_State *s = STATS(c); |
444 | 479k | if (s->Symbol != p->FoundState->Symbol) |
445 | 218k | { |
446 | 13.5M | do { s++; } while (s->Symbol != p->FoundState->Symbol); |
447 | 218k | if (s[0].Freq >= s[-1].Freq) |
448 | 167k | { |
449 | 167k | SwapStates(&s[0], &s[-1]); |
450 | 167k | s--; |
451 | 167k | } |
452 | 218k | } |
453 | 479k | if (s->Freq < MAX_FREQ - 9) |
454 | 479k | { |
455 | 479k | s->Freq += 2; |
456 | 479k | c->SummFreq += 2; |
457 | 479k | } |
458 | 479k | } |
459 | 741k | } |
460 | | |
461 | 10.6M | if (p->OrderFall == 0) |
462 | 152k | { |
463 | 152k | p->MinContext = p->MaxContext = CreateSuccessors(p, True); |
464 | 152k | if (p->MinContext == 0) |
465 | 4.66k | { |
466 | 4.66k | RestartModel(p); |
467 | 4.66k | return; |
468 | 4.66k | } |
469 | 148k | SetSuccessor(p->FoundState, REF(p->MinContext)); |
470 | 148k | return; |
471 | 152k | } |
472 | | |
473 | 10.4M | *p->Text++ = p->FoundState->Symbol; |
474 | 10.4M | successor = REF(p->Text); |
475 | 10.4M | if (p->Text >= p->UnitsStart) |
476 | 4.42k | { |
477 | 4.42k | RestartModel(p); |
478 | 4.42k | return; |
479 | 4.42k | } |
480 | | |
481 | 10.4M | if (fSuccessor) |
482 | 1.67M | { |
483 | 1.67M | if (fSuccessor <= successor) |
484 | 979k | { |
485 | 979k | CTX_PTR cs = CreateSuccessors(p, False); |
486 | 979k | if (cs == NULL) |
487 | 32.7k | { |
488 | 32.7k | RestartModel(p); |
489 | 32.7k | return; |
490 | 32.7k | } |
491 | 946k | fSuccessor = REF(cs); |
492 | 946k | } |
493 | 1.63M | if (--p->OrderFall == 0) |
494 | 283k | { |
495 | 283k | successor = fSuccessor; |
496 | 283k | p->Text -= (p->MaxContext != p->MinContext); |
497 | 283k | } |
498 | 1.63M | } |
499 | 8.80M | else |
500 | 8.80M | { |
501 | 8.80M | SetSuccessor(p->FoundState, successor); |
502 | 8.80M | fSuccessor = REF(p->MinContext); |
503 | 8.80M | } |
504 | | |
505 | 10.4M | s0 = p->MinContext->SummFreq - (ns = p->MinContext->NumStats) - (p->FoundState->Freq - 1); |
506 | | |
507 | 11.8M | for (c = p->MaxContext; c != p->MinContext; c = SUFFIX(c)) |
508 | 1.49M | { |
509 | 1.49M | unsigned ns1; |
510 | 1.49M | UInt32 cf, sf; |
511 | 1.49M | if ((ns1 = c->NumStats) != 1) |
512 | 583k | { |
513 | 583k | if ((ns1 & 1) == 0) |
514 | 353k | { |
515 | | /* Expand for one UNIT */ |
516 | 353k | unsigned oldNU = ns1 >> 1; |
517 | 353k | unsigned i = U2I(oldNU); |
518 | 353k | if (i != U2I((size_t)oldNU + 1)) |
519 | 337k | { |
520 | 337k | void *ptr = AllocUnits(p, i + 1); |
521 | 337k | void *oldPtr; |
522 | 337k | if (!ptr) |
523 | 24.1k | { |
524 | 24.1k | RestartModel(p); |
525 | 24.1k | return; |
526 | 24.1k | } |
527 | 313k | oldPtr = STATS(c); |
528 | 313k | MyMem12Cpy(ptr, oldPtr, oldNU); |
529 | 313k | InsertNode(p, oldPtr, i); |
530 | 313k | c->Stats = STATS_REF(ptr); |
531 | 313k | } |
532 | 353k | } |
533 | 559k | c->SummFreq = (UInt16)(c->SummFreq + (2 * ns1 < ns) + 2 * ((4 * ns1 <= ns) & (c->SummFreq <= 8 * ns1))); |
534 | 559k | } |
535 | 907k | else |
536 | 907k | { |
537 | 907k | CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0); |
538 | 907k | if (!s) |
539 | 20.3k | { |
540 | 20.3k | RestartModel(p); |
541 | 20.3k | return; |
542 | 20.3k | } |
543 | 887k | *s = *ONE_STATE(c); |
544 | 887k | c->Stats = REF(s); |
545 | 887k | if (s->Freq < MAX_FREQ / 4 - 1) |
546 | 851k | s->Freq <<= 1; |
547 | 35.4k | else |
548 | 35.4k | s->Freq = MAX_FREQ - 4; |
549 | 887k | c->SummFreq = (UInt16)(s->Freq + p->InitEsc + (ns > 3)); |
550 | 887k | } |
551 | 1.44M | cf = 2 * (UInt32)p->FoundState->Freq * (c->SummFreq + 6); |
552 | 1.44M | sf = (UInt32)s0 + c->SummFreq; |
553 | 1.44M | if (cf < 6 * sf) |
554 | 917k | { |
555 | 917k | cf = 1 + (cf > sf) + (cf >= 4 * sf); |
556 | 917k | c->SummFreq += 3; |
557 | 917k | } |
558 | 529k | else |
559 | 529k | { |
560 | 529k | cf = 4 + (cf >= 9 * sf) + (cf >= 12 * sf) + (cf >= 15 * sf); |
561 | 529k | c->SummFreq = (UInt16)(c->SummFreq + cf); |
562 | 529k | } |
563 | 1.44M | { |
564 | 1.44M | CPpmd_State *s = STATS(c) + ns1; |
565 | 1.44M | SetSuccessor(s, successor); |
566 | 1.44M | s->Symbol = p->FoundState->Symbol; |
567 | 1.44M | s->Freq = (Byte)cf; |
568 | 1.44M | c->NumStats = (UInt16)(ns1 + 1); |
569 | 1.44M | } |
570 | 1.44M | } |
571 | 10.3M | p->MaxContext = p->MinContext = CTX(fSuccessor); |
572 | 10.3M | } |
573 | | |
574 | | static void Rescale(CPpmd7 *p) |
575 | 160k | { |
576 | 160k | unsigned i, adder, sumFreq, escFreq; |
577 | 160k | CPpmd_State *stats = STATS(p->MinContext); |
578 | 160k | CPpmd_State *s = p->FoundState; |
579 | 160k | { |
580 | 160k | CPpmd_State tmp = *s; |
581 | 204k | for (; s != stats; s--) |
582 | 44.0k | s[0] = s[-1]; |
583 | 160k | *s = tmp; |
584 | 160k | } |
585 | 160k | escFreq = p->MinContext->SummFreq - s->Freq; |
586 | 160k | s->Freq += 4; |
587 | 160k | adder = (p->OrderFall != 0); |
588 | 160k | s->Freq = (Byte)((s->Freq + adder) >> 1); |
589 | 160k | sumFreq = s->Freq; |
590 | | |
591 | 160k | i = p->MinContext->NumStats - 1; |
592 | 160k | do |
593 | 537k | { |
594 | 537k | escFreq -= (++s)->Freq; |
595 | 537k | s->Freq = (Byte)((s->Freq + adder) >> 1); |
596 | 537k | sumFreq += s->Freq; |
597 | 537k | if (s[0].Freq > s[-1].Freq) |
598 | 96.2k | { |
599 | 96.2k | CPpmd_State *s1 = s; |
600 | 96.2k | CPpmd_State tmp = *s1; |
601 | 96.2k | do |
602 | 1.21M | s1[0] = s1[-1]; |
603 | 1.21M | while (--s1 != stats && tmp.Freq > s1[-1].Freq); |
604 | 96.2k | *s1 = tmp; |
605 | 96.2k | } |
606 | 537k | } |
607 | 537k | while (--i); |
608 | | |
609 | 160k | if (s->Freq == 0) |
610 | 71.7k | { |
611 | 71.7k | unsigned numStats = p->MinContext->NumStats; |
612 | 71.7k | unsigned n0, n1; |
613 | 118k | do { i++; } while ((--s)->Freq == 0); |
614 | 71.7k | escFreq += i; |
615 | 71.7k | p->MinContext->NumStats = (UInt16)(p->MinContext->NumStats - i); |
616 | 71.7k | if (p->MinContext->NumStats == 1) |
617 | 31.3k | { |
618 | 31.3k | CPpmd_State tmp = *stats; |
619 | 31.3k | do |
620 | 54.0k | { |
621 | 54.0k | tmp.Freq = (Byte)(tmp.Freq - (tmp.Freq >> 1)); |
622 | 54.0k | escFreq >>= 1; |
623 | 54.0k | } |
624 | 54.0k | while (escFreq > 1); |
625 | 31.3k | InsertNode(p, stats, U2I(((numStats + 1) >> 1))); |
626 | 31.3k | *(p->FoundState = ONE_STATE(p->MinContext)) = tmp; |
627 | 31.3k | return; |
628 | 31.3k | } |
629 | 40.3k | n0 = (numStats + 1) >> 1; |
630 | 40.3k | n1 = (p->MinContext->NumStats + 1) >> 1; |
631 | 40.3k | if (n0 != n1) |
632 | 14.9k | p->MinContext->Stats = STATS_REF(ShrinkUnits(p, stats, n0, n1)); |
633 | 40.3k | } |
634 | 128k | p->MinContext->SummFreq = (UInt16)(sumFreq + escFreq - (escFreq >> 1)); |
635 | 128k | p->FoundState = STATS(p->MinContext); |
636 | 128k | } |
637 | | |
638 | | CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *escFreq) |
639 | 1.09M | { |
640 | 1.09M | CPpmd_See *see; |
641 | 1.09M | unsigned nonMasked = p->MinContext->NumStats - numMasked; |
642 | 1.09M | if (p->MinContext->NumStats != 256) |
643 | 278k | { |
644 | 278k | see = p->See[(unsigned)p->NS2Indx[(size_t)nonMasked - 1]] + |
645 | 278k | (nonMasked < (unsigned)SUFFIX(p->MinContext)->NumStats - p->MinContext->NumStats) + |
646 | 278k | 2 * (unsigned)(p->MinContext->SummFreq < 11 * p->MinContext->NumStats) + |
647 | 278k | 4 * (unsigned)(numMasked > nonMasked) + |
648 | 278k | p->HiBitsFlag; |
649 | 278k | { |
650 | 278k | unsigned r = (see->Summ >> see->Shift); |
651 | 278k | see->Summ = (UInt16)(see->Summ - r); |
652 | 278k | *escFreq = r + (r == 0); |
653 | 278k | } |
654 | 278k | } |
655 | 821k | else |
656 | 821k | { |
657 | 821k | see = &p->DummySee; |
658 | 821k | *escFreq = 1; |
659 | 821k | } |
660 | 1.09M | return see; |
661 | 1.09M | } |
662 | | |
663 | | static void NextContext(CPpmd7 *p) |
664 | 14.9M | { |
665 | 14.9M | CTX_PTR c = CTX(SUCCESSOR(p->FoundState)); |
666 | 14.9M | if (p->OrderFall == 0 && (Byte *)c > p->Text) |
667 | 5.28M | p->MinContext = p->MaxContext = c; |
668 | 9.66M | else |
669 | 9.66M | UpdateModel(p); |
670 | 14.9M | } |
671 | | |
672 | | void Ppmd7_Update1(CPpmd7 *p) |
673 | 9.06M | { |
674 | 9.06M | CPpmd_State *s = p->FoundState; |
675 | 9.06M | s->Freq += 4; |
676 | 9.06M | p->MinContext->SummFreq += 4; |
677 | 9.06M | if (s[0].Freq > s[-1].Freq) |
678 | 4.93M | { |
679 | 4.93M | SwapStates(&s[0], &s[-1]); |
680 | 4.93M | p->FoundState = --s; |
681 | 4.93M | if (s->Freq > MAX_FREQ) |
682 | 770 | Rescale(p); |
683 | 4.93M | } |
684 | 9.06M | NextContext(p); |
685 | 9.06M | } |
686 | | |
687 | | void Ppmd7_Update1_0(CPpmd7 *p) |
688 | 2.88M | { |
689 | 2.88M | p->PrevSuccess = (2 * p->FoundState->Freq > p->MinContext->SummFreq); |
690 | 2.88M | p->RunLength += p->PrevSuccess; |
691 | 2.88M | p->MinContext->SummFreq += 4; |
692 | 2.88M | if ((p->FoundState->Freq += 4) > MAX_FREQ) |
693 | 158k | Rescale(p); |
694 | 2.88M | NextContext(p); |
695 | 2.88M | } |
696 | | |
697 | | void Ppmd7_UpdateBin(CPpmd7 *p) |
698 | 3.00M | { |
699 | 3.00M | p->FoundState->Freq = (Byte)(p->FoundState->Freq + (p->FoundState->Freq < 128 ? 1: 0)); |
700 | 3.00M | p->PrevSuccess = 1; |
701 | 3.00M | p->RunLength++; |
702 | 3.00M | NextContext(p); |
703 | 3.00M | } |
704 | | |
705 | | void Ppmd7_Update2(CPpmd7 *p) |
706 | 963k | { |
707 | 963k | p->MinContext->SummFreq += 4; |
708 | 963k | if ((p->FoundState->Freq += 4) > MAX_FREQ) |
709 | 1.15k | Rescale(p); |
710 | 963k | p->RunLength = p->InitRL; |
711 | 963k | UpdateModel(p); |
712 | 963k | } |