wok-tiny view plop/stuff/unlzma.S @ rev 170

memtest,plop: tune lzma compression (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri May 07 15:00:24 2021 +0000 (2021-05-07)
parents aacd5b54d010
children eb617e43dc08
line source
1 // #define RC_NORMALIZE if (Range < kTopValue) { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
2 //
3 // #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
4 // #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
5 // #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
6 //
7 //#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
8 // { UpdateBit0(p); mi <<= 1; A0; } else \
9 // { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
10 //
11 // #define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
12 //
13 // #define RangeDecoderBitTreeDecode(probs, numLevels, res) \
14 // { int i = numLevels; res = 1; \
15 // do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
16 // res -= (1 << numLevels); }
17 /*
18 * Compression with : lzma e src dst -eos -pb2 -lp0 -lc3
19 */
21 //#define PROP_PB 2
22 #define PROP_PB 0
23 #define PROP_LP 0
24 //#define PROP_LC 3
25 #define PROP_LC 0
26 #define PROPS (PROP_LC+(PROP_LP*9)+(PROP_PB*45))
28 // static const Byte *Buffer;
29 // static UInt32 bound, Code, Range;
31 /*
32 * Buffer register DS:SI
33 * all var based ws=ss:bp
34 */
36 rep0 = -4 // long
37 rep1 = rep0-4 // long
38 rep2 = rep0-8 // long
39 rep3 = rep0-12 // long
40 state = -17 // byte, 0..11
41 posState = state-1 // byte, 0..15
42 posState2 = posState-1 // byte, 0..15
43 scratched = rep0-16 // byte = 1
44 Code = -24 // long
45 outStream = -28 // long
46 nowPos = outStream // long
47 Range = Code-8 // long
48 #define LOCALS 32
50 // int LzmaDecode(CLzmaDecoderState *vs,
51 // const unsigned char *inStream,
52 // unsigned char *outStream)
53 // {
54 // CProb *p = vs->Probs;
55 // SizeT nowPos = 0;
56 // #define posStateMask = (1 << (vs->Properties.pb)) - 1;
57 // #define literalPosMask = (1 << (vs->Properties.lp)) - 1;
58 // int lc = vs->Properties.lc, state = 0, len = 0;
59 // UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
60 //
61 // {
62 // UInt32 i, numProbs = Literal /*1846*/
63 // + ((UInt32)LZMA_LIT_SIZE /*768*/ << (lc + vs->Properties.lp));
64 // for (i = 0; i < numProbs; i++) p[i] = kBitModelTotal /*2048*/ >> 1;
66 #define WS (1846+(768<<(PROP_LC+PROP_LP)))
67 #if (WS+WS+LOCALS) >= 65000
68 /* MAX WS = (1846+(768<<(8+4))) > 3MB! */
69 #error invalid (lc,lp,pb) : out of memory
70 #endif
72 ws1 = WS
73 ws2 = ws1*2
74 ws = ws2+LOCALS+15
76 #ifndef FLAT32
77 #define AX %ax
78 #define BX %bx
79 #define CX %cx
80 #define DX %dx
81 #define SI %si
82 #define DI %di
83 #define BP %bp
84 #define SP %sp
85 #define CWD cwd
86 #else
87 #define AX %eax
88 #define BX %ebx
89 #define CX %ecx
90 #define DX %edx
91 #define SI %esi
92 #define DI %edi
93 #define BP %ebp
94 #define SP %esp
95 #define CWD cdq
96 #endif
97 /*
98 * LzmaDecode:
99 #ifndef FLAT32
100 * input ds:si=inStream, es:di=outStream
101 * output outStream[], ds:si, es:di
102 .code 16
103 #else
104 * input esi=inStream, edi=outStream
105 * output outStream[], esi, edi
106 .code 32
107 #endif
108 */
110 mov $ws1, CX
111 #ifdef ONLY8086
112 movw $2048/2, %ax
113 lzd1:
114 pushw %ax
115 #else
116 lzd1:
117 pushw $2048/2
118 #endif
119 loop lzd1
120 mov SP, BP
121 movb $((LOCALS+3)/4)*2, %cl
122 #ifdef ONLY8086
123 movw $1, %ax
124 cwd
125 initlocals:
126 pushw %dx
127 pushw %ax
128 #else
129 initlocals:
130 pushl $1
131 #endif
132 loop initlocals
134 #if !defined(FLAT32) && !defined(FLAT16OUT)
135 movb $4, %cl
136 movw %es, %bx
137 shrw %cl, %bx
138 movw %es, %dx
139 shlw %cl, %dx
140 addw %dx, %di
141 movw %di, outStream(%bp)
142 adcb %bh, outStream+2(%bp)
143 incw %cx
144 #else
145 movb $5, %cl
146 mov DI, outStream(BP)
147 #endif
149 // Byte previousByte = 0;
150 xor BX, BX
152 // #define RC_INIT(buffer)
153 // Buffer = buffer; Code = 0; Range = 0xFFFFFFFF;
154 // { int i; for(i=0; i<5; i++) { Code = (Code<<8) | RC_READ_BYTE; }}
155 // }
156 // RC_INIT(inStream);
158 #ifndef NO_LZMA_HEADER
159 #ifdef CHECK_LZMA_HEADER
160 cmp.w $0x5A4C, (SI) // lzip header ('LZIP' version:1 dicobits:1)
161 je lzip_header
162 cmp.w $0x5D, (SI) // lzma header (0x5D dicosz:4 orgsz:8)
163 jne no_header
164 add $13-6, SI // skip lzma header
165 lzip_header:
166 add $6, SI // skip lzip header
167 no_header:
168 #else
169 add $13, SI // skip lzma header (0x5D dicosz:4 orgsz:8)
170 #endif
171 #endif
172 setrep:
173 call RC_LOAD_BYTE
174 decb Range(BP)
175 loop setrep
177 lzdmainlp:
178 // while(1) {
179 // CProb *prob;
180 // int posState = (int)((nowPos) & posStateMask);
181 //
182 // prob = p + IsMatch /*0*/ + (state << kNumPosBitsMax /*4*/) + posState;
183 // if (Bit0(prob)) { /* char */
185 xor DX, DX
186 call Bit1state // Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
187 mov $state, DI
188 jc lzdstring
190 // prob = p + Literal /*1846*/ + (LZMA_LIT_SIZE /*768*/ *
191 // ((((nowPos) & literalPosMask) << lc) + (previousByte >> (8 - lc))));
193 #if PROP_LC != 0
194 # ifdef ONLY8086
195 movb $8-PROP_LC, %cl
196 shrb %cl, %bl
197 # else
198 shrb $8-PROP_LC, %bl
199 # endif
200 #else
201 xor %bx,%bx
202 #endif
204 #if PROP_LP != 0
205 movb posState2(BP), %dl
206 # if PROP_LC != 0
207 # ifdef ONLY8086
208 movb $PROP_LC, %cl
209 shl %cl, DX
210 # else
211 shl $PROP_LC, DX
212 # endif
213 movb $0, %bh
214 # endif
215 add BX, DX
216 #endif
218 movb $3, %ah
219 mul BX // dx = 3*bh
220 add $1846, AX
222 // int symbol = 1;
224 CWD
225 inc DX // symbol = 1
226 xchg AX, CX // save prob
228 // if (state >= kNumLitStates /*7*/) { /* previous was string */
229 // if (state < 4) state = 0;
231 lzd6z:
232 subb $3, (BP, DI)
234 // if (state < 4) state = 0;
236 jnc lzd6
237 movb %dh, (BP, DI) // %dh = 0
239 lzd6:
240 // else if (state < 10) state -= 3;
242 cmpb $10-3, (BP, DI)
244 // else state -= 6;
246 jnb lzd6z
247 cmpb $7-3-1, (BP, DI)
248 jbe lzd3
250 // int matchByte = outStream[nowPos - rep0];
252 call DicoRep02ESDI // %bl = outStream[nowPos - rep0];
254 // do {
255 // int bit;
256 // CProb *probLit;
257 // matchByte <<= 1; bit = (matchByte & 0x100);
259 movb $1, %bh
260 lzd4:
261 shlb $1, %bl // matchByte <<= 1
262 sbb DI, DI // save bit=C
264 // probLit = prob + 0x100 + bit + symbol;
266 mov CX, AX // restore prob
267 adcb %bh, %ah // + bit + 0x100
269 // RC_GET_BIT2(probLit, symbol, if (bit) break, if (!bit) break)
271 call Bit1axdx // C,%ax = Bit1(prob+%ax)
272 rclb $1, %dl // symbol <<= 1; symbol |= C
273 jc lzd5 // if symbol >= 0x100
274 cmp DI, AX
275 jz lzd4 // if bit == Bit1(prob+%ax)
277 // } while (symbol < 0x100);
278 // }
279 lzd3:
280 // while (symbol < 0x100) {
281 // CProb *probLit = prob + symbol;
282 // RC_GET_BIT(probLit, symbol)
283 // }
285 xor BX, BX
286 jmp lzd4
287 lzd5:
289 // outStream[nowPos++] = previousByte = (Byte)symbol;
291 xchg AX, DX
292 call outchar // %bl = outStream[nowPos++] = %al;
293 jmp lzdmainlp
295 // }
297 lzdstring:
298 mov $1, CX
300 // else { /* string */
301 // prob = p + IsRep /*192*/ + state;
303 movb $192, %dl
304 addb (BP, DI), %dl
305 mov $rep0, DI
307 // if (Bit0(prob)) {
309 call Bit1dx // Bit1(prob)
310 jc lzd8
312 // rep3 = rep2; rep2 = rep1; rep1 = rep0;
313 // state = (state < kNumLitStates /*7*/) ? 0 : 3;
315 stc
317 // prob = p + LenCoder /*818*/;
319 mov $818, DX
321 // }
323 jmp lzd11a
325 // else {
326 lzd8:
327 // prob += kNumStates /*12*/;
328 // if (Bit0(prob)) {
329 call Bit1dx12 // prob += 12; Bit1(prob)
330 jc lzd11
331 // prob = p + IsRep0Long /*240*/ + (state << kNumPosBitsMax /*4*/)
332 // + posState;
333 movb $240, %dl // dh=0
335 // if (Bit0(prob)) {
337 call Bit1state // Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
338 jc lzd12
340 // // if (nowPos == 0) return LZMA_RESULT_DATA_ERROR;
341 // state = (state < kNumLitStates /*7*/) ? 9 : 11;
343 movb $9, %dl
345 // len++; goto string;
346 jmp lzd13string // ax = 0
347 // }
348 // }
349 // else {
350 lzd11:
351 // UInt32 distance = rep1;
352 // prob += kNumStates /*12*/;
353 // if (!Bit0(prob)) {
355 call Bit1dx12 // prob += 12; Bit1(prob)
356 jnc lzd11z
358 // prob += kNumStates /*12*/;
359 // if (Bit0(prob)) distance = rep2;
361 call Bit1dx12 // prob += 12; Bit1(prob)
362 lzd11a:
363 adcb %cl, %cl
365 // else { distance = rep3; rep3 = rep2; }
366 // rep2 = rep1;
367 // }
368 // rep1 = rep0; rep0 = distance;
370 lzd11z:
371 # ifdef ONLY8086
372 shl $1, CX
373 shl $1, CX // 8->32 bits
374 sub CX, DI // &rep[cx]
375 movw (BP, DI), %ax
376 pushw 2(BP, DI)
377 rotreplp:
378 movb 4(BP, DI), %bl
379 movb %bl, (BP, DI)
380 inc DI
381 loop rotreplp
382 popw %bx
383 testb %dh, %dh
384 jnz lzd10
385 movw %ax, (BP, DI)
386 movw %bx, 2(BP, DI)
387 # else
388 shl $2, CX // 8->32 bits
389 sub CX, DI // &rep[cx]
390 movl (BP, DI), %eax
391 rotreplp:
392 movb 4(BP, DI), %bl
393 movb %bl, (BP, DI)
394 inc DI
395 loop rotreplp
396 testb %dh, %dh
397 jnz lzd10
398 movl %eax, (BP, DI)
399 # endif
401 // }
402 lzd12:
403 // state = (state < kNumLitStates /*7*/) ? 8 : 11;
405 movb $0x08, %cl
407 // prob = p + RepLenCoder /*1332*/;
409 mov $1332, DX
411 // }
412 lzd10:
413 push CX // CX = 0
415 // { /* get len */
416 // int numBits, offset;
417 // CProb *probLen = prob + LenChoice /*0*/;
418 // numBits = kLenNumLowBits /*3*/;
420 movb $8, %cl // numBits : 3,3,8
422 // if (Bit0(probLen)) {
424 call Bit1dx // Bit1(prob)
425 xchg AX, BX
426 inc DX
427 jnc lzd15 // bx=0
429 // probLen = prob + LenLow/*2*/ + (posState << kLenNumLowBits/*3*/);
430 // offset = 0;
431 // }
432 // else {
433 // probLen = prob + LenChoice2 /*1*/;
435 call Bit1dx // Bit1(prob)
436 add AX, BX
438 #if PROP_PB != 0
439 inc AX // ah=0
440 #endif
441 jc lzd16 // %ax=0, %bx=-2
442 lzd15:
443 #if PROP_PB != 0
444 movb $8, %al
445 mulb posState(BP)
446 #endif
448 // if (Bit0(probLen)) {
449 // probLen = prob + LenMid/*130*/ + (posState << kLenNumMidBits/*3*/);
451 movb $3, %cl // numBits : 3,3,8
452 lzd16:
453 #if PROP_PB != 0
454 add $2-128-1, AX // probLen : 2,130,258
455 #else
456 mov $2-128-1, AX // probLen : 2,130,258
457 #endif
458 add DX, AX
459 mov $-8+1, DX // offset : 0,8,16
460 lzdargslp:
461 add $8, DX
462 add $128, AX
463 inc BX
464 jle lzdargslp // leave with bx=1
466 // offset = kLenNumLowSymbols /*8*/;
467 // //numBits = kLenNumMidBits /*3*/;
468 // }
469 // else {
470 // probLen = prob + LenHigh /*258*/;
471 // offset = kLenNumLowSymbols /*8*/ + kLenNumMidSymbols /*8*/;
472 // numBits = kLenNumHighBits /*8*/;
473 // }
474 // }
475 // RangeDecoderBitTreeDecode(probLen, numBits, len); len += offset;
477 push DX
478 call RangeDecoder // %ax=probs, %cx=numLevels, %ax=res
479 pop DX
480 add DX, AX // offset
481 pop DX // 0
482 lzd13string:
483 push AX
485 // state = (state < kNumLitStates /*7*/) ? dl : dl|3;
487 movb $7, %cl
488 cmpb %cl, state(BP)
489 jb new_state
490 orb $3, %dl
491 new_state:
492 movb %dl, state(BP)
494 // } /* get len */
495 // if (state < 4) {
497 cmpb $4-1, %dl
498 ja lzd19
500 // int posSlot;
501 // state += kNumLitStates /*7*/;
503 addb %cl, state(BP)
505 // prob = p + PosSlot /*432*/ + (((len < kNumLenToPosStates /*4*/) ?
506 // len : kNumLenToPosStates - 1) << kNumPosSlotBits /*6*/);
508 cmp $4+1, AX
509 jb lzd21
510 mov $3+1, AX
512 lzd21:
514 dec CX // cx = 6
515 shl %cl, AX
516 add $432-64, AX
518 // RangeDecoderBitTreeDecode(prob, kNumPosSlotBits /*6*/, posSlot);
520 call RangeDecoder // %ax=probs, %cx=numLevels, %ax=res
522 // if (posSlot >= kStartPosModelIndex /*4*/) {
523 // int numDirectBits = ((posSlot >> 1) - 1);
525 #ifndef FLAT32
526 movw %cx, 2(%bp, %di) // %cx = 0
527 #endif
528 mov AX, (BP, DI)
529 mov AX, CX
530 shrw $1, CX
531 dec CX
532 cmpb $4, %al
533 jb lzd22
535 // rep0 = (2 | ((UInt32)posSlot & 1));
537 andb %bl, (BP, DI) // %bx=1
538 orb $2, (BP, DI)
540 // if (posSlot < kEndPosModelIndex /*14*/) {
542 cmpb $14, %al
543 jnb lzd23
545 // rep0 <<= numDirectBits;
547 neg AX
548 # ifdef ONLY8086
549 pushw %cx
550 movb $0, %ch
551 shllrep0:
552 shlw $1, (BP, DI)
553 rclw $1, 2(BP, DI)
554 loop shllrep0
555 popw %cx
556 # else
557 shll %cl, (BP, DI)
558 # endif
559 add (BP, DI), AX
561 // prob = p + SpecPos /*688*/ + rep0 - posSlot - 1;
563 add $687, AX
564 jmp lzd24
566 // }
567 // else {
568 lzd23:
569 // numDirectBits -= kNumAlignBits /*4*/;
570 // do {
571 // RC_NORMALIZE; Range >>= 1; rep0 <<= 1;
572 // if (Code >= Range) { Code -= Range; rep0 |= 1; }
574 lzd23z:
575 call RC_NORMALIZE
576 # ifdef ONLY8086
577 pushw %dx
578 shrw $1, Range+2(BP)
579 rcrw $1, Range(BP)
580 movw Range(BP), %ax
581 movw Range+2(BP), %dx
582 cmpw Code+2(BP), %dx
583 ja lzd25
584 jb lzd25x
585 cmpw Code(BP), %ax
586 ja lzd25
587 lzd25x:
588 subw %ax, Code(BP)
589 sbbw %dx, Code+2(BP)
590 stc
591 lzd25:
592 popw %dx
593 rclw $1, (BP, DI)
594 rclw $1, 2(BP, DI)
595 # else
596 shrl $1, Range(BP)
597 movl Range(BP), %eax
598 cmpl Code(BP), %eax
599 ja lzd25
600 subl %eax, Code(BP)
601 stc
602 lzd25:
603 rcll $1, (BP, DI)
604 # endif
606 // } while (--numDirectBits != 0);
608 cmpb $4+1, %cl
609 loopne lzd23z
611 // prob = p + Align /* 802 */; numDirectBits = kNumAlignBits /*4*/;
612 // rep0 <<= numDirectBits;
614 # ifdef ONLY8086
615 pushw %cx
616 movb $0, %ch
617 shlrep0:
618 shlw $1, (BP, DI)
619 rclw $1, 2(BP, DI)
620 loop shlrep0
621 popw %cx
622 # else
623 shll %cl, (BP, DI)
624 # endif
625 mov $802, AX
626 // }
628 lzd24:
629 call RangeDecoder // %ax=probs, %cx=numLevels, %ax=res
631 // {
632 // int i = 1, mi = 1;
633 // do {
634 // CProb *prob3 = prob + mi;
635 // RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
637 orb %dh, (BP, DI) // update rep0 with DirectBits
639 // i <<= 1;
640 // } while(--numDirectBits != 0);
641 // }
642 // } else rep0 = posSlot;
643 lzd22:
644 // if (++rep0 == (UInt32)(0)) break; /* EOF */
646 # ifdef ONLY8086
647 incw (BP, DI)
648 jnz lzd19
649 incw 2(BP, DI)
650 # else
651 incl (BP, DI)
652 # endif
654 lzd19:
655 pop CX
656 jz lzdone
658 // }
659 // len += kMatchMinLen;/*2*/
661 inc CX
663 // string: // if (rep0 > nowPos) return LZMA_RESULT_DATA_ERROR;
664 // do {
665 lzd13z:
666 // previousByte = outStream[nowPos - rep0];
667 // outStream[nowPos++] = previousByte;
669 call outcharDico // %bl = outStream[nowPos++] = outStream[nowPos - rep0]
671 // } while(--len != 0);
673 loop lzd13z
675 // } /* char/string */
676 // }
678 jmp lzdmainlp
680 lzdone:
681 // //RC_NORMALIZE;
682 // //*inSizeProcessed = (SizeT)(Buffer - inStream); *outSizeProcessed = nowPos;
683 // return LZMA_RESULT_OK;
684 call Dico2ESDI // set es & di (rep0 = 0)
685 lea ws2(BP), SP // dealloc
686 ret
687 // }
689 // al = outStream[nowPos - rep0];
691 /*
692 * output es:di, al
693 * scratch bh, cl, flags
694 */
696 DicoRep02ESDI:
697 stc
699 // bl = outStream[nowPos];
701 /*
702 * output es:di, bl
703 * scratch bh, cl, flags
704 */
706 Dico2ESDI:
707 #if !defined(FLAT32) && !defined(FLAT16OUT)
708 # ifdef ONLY8086
709 pushw %ax
710 movw nowPos(%bp), %bx
711 movw nowPos+2(%bp), %ax
712 jnc Dico2ESDIz
713 subw rep0(%bp), %bx
714 sbbw rep0+2(%bp), %ax
715 Dico2ESDIz:
716 movw $0xF, %di
717 andw %bx, %di
718 pushw %cx
719 movb $4, %cl
720 shrw %cl, %bx
721 shlw %cl, %ax
722 popw %cx
723 addb %al, %bh
724 popw %ax
725 # else
726 movl nowPos(%bp), %ebx
727 jnc Dico2ESDIz
728 subl rep0(%bp), %ebx
729 Dico2ESDIz:
730 movw %bx, %di
731 xorw %bx, %bx
732 shrl $4, %ebx
733 # endif
734 movw %bx, %es
735 #else
736 mov nowPos(BP), DI
737 jnc Dico2ESDIz
738 sub rep0(BP), DI
739 Dico2ESDIz:
740 #endif
741 #ifdef FLAT32
742 movb (DI), %bl
743 #else
744 movb %es:(%di), %bl
745 #endif
746 ret
748 outcharDico:
750 // bl = outStream[nowPos++] = outStream[nowPos - rep0]
752 /*
753 * output es:di, bl
754 * update nowPos
755 * scratch ax, dx, bh, cl, flags
756 */
758 call DicoRep02ESDI // %bl = outStream[nowPos - rep0]
759 xchg AX, BX
760 outchar:
762 // bl = outStream[nowPos++] = previousByte = al;
764 /*
765 * output bl
766 * update nowPos
767 * scratch ax, dx, bh, di, cl, flags
768 */
770 clc
771 call Dico2ESDI
772 stosb
773 xchg AX, BX // previous byte
775 // int posState = (int)((nowPos) & posStateMask);
777 #if PROP_PB != 0 && PROP_LP != 0
778 addw $0x0101, posState2(BP)
779 andb $(((1 << PROP_PB) -1)<<8)+((1 << PROP_LP) -1), posState2(BP)
780 #else
781 # if PROP_PB != 0
782 incb posState(BP)
783 andb $((1 << PROP_PB) -1), posState(BP)
784 # endif
785 # if PROP_LP != 0
786 incb posState2(BP)
787 andb $((1 << PROP_LP) -1), posState2(BP)
788 # endif
789 #endif
790 #ifdef ONLY8086
791 incw nowPos(BP)
792 jnz incnowPosDone
793 incw nowPos+2(BP)
794 incnowPosDone:
795 #else
796 incl nowPos(BP)
797 #endif
798 ret
800 //
801 // #define RC_NORMALIZE if (Range < kTopValue)
802 // { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
804 /*
805 * update Range, Code, ds:si
806 * scratch flags
807 */
809 RC_NORMALIZE:
810 cmpb $0, Range+3(BP)
811 jne RC_NORMALIZE_1
812 RC_LOAD_BYTE:
813 push AX
814 #ifdef ONLY8086
815 movw Range+1(BP), %ax
816 movw %ax, Range+2(BP)
817 movw Code+1(BP), %ax
818 movw %ax, Code+2(BP)
819 xorw %ax, %ax
820 movb Range(BP), %ah
821 movw %ax, Range(BP)
822 movb Code(BP), %ah
823 movw %ax, Code(BP)
824 #else
825 shll $8, Range(BP)
826 shll $8, Code(BP)
827 #endif
828 #if !defined(FLAT16) && !defined(FLAT32)
829 testw %si, %si
830 jns RC_READ_BYTE
831 movw %ds, %ax
832 incw %ax
833 movw %ax, %ds
834 addw $-16, %si
835 RC_READ_BYTE:
836 #endif
837 lodsb
838 movb %al, Code(BP)
839 pop AX
840 RC_NORMALIZE_1:
841 ret
843 // Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
845 Bit1state:
846 movb $16, %al
847 mulb state(BP)
848 # if PROP_PB != 0
849 addb posState(BP), %al
850 # endif
851 Bit1axdx:
852 add DX, AX
853 jmp Bit1
855 // prob += 12; Bit1(prob)
857 Bit1dx12:
858 add $12, DX
859 Bit1dx:
860 mov DX, AX
862 // static int Bit1(CProb *p)
864 Bit1:
865 /*
866 * input ax=p
867 * output C, ax
868 * update bound, Range, Code, ds:si
869 * scratch flags
870 */
872 // {
873 // RC_NORMALIZE;
875 call RC_NORMALIZE // kill %ax, update %si
877 #ifdef ONLY8086
878 pushw %ax
879 pushw %cx
880 pushw %dx
881 pushw %di
882 #else
883 pushal
884 #endif
886 xchg AX, DI
887 add DI, DI // short *
890 // bound = (Range>>kNumBitModelTotalBits /*11*/) * *(p);
892 #ifdef ONLY8086
893 movw Range(BP), %dx
894 movw Range+2(BP), %ax
895 movw $11, %cx
896 shr11lp:
897 shrw $1, %ax
898 rcrw $1, %dx
899 loop shr11lp
900 movw %dx, %cx
901 mulw (BP, DI)
902 xchgw %ax, %cx
903 mulw (BP, DI)
904 addw %cx, %dx
905 #else
906 movl Range(BP), %eax
907 shrl $11, %eax
908 movzwl (BP, DI), %edx
909 mull %edx
910 #endif
912 // if (Code < bound) {
914 #ifdef ONLY8086
915 cmpw Code+2(BP), %dx
916 jb Bit1_1
917 ja Bit1_1x
918 cmpw Code(BP), %ax
919 jbe Bit1_1
920 Bit1_1x:
922 // Range = bound;
924 movw %ax, Range(BP)
925 movw %dx, Range+2(BP)
926 #else
927 cmpl Code(BP), %eax
928 jbe Bit1_1
930 // Range = bound;
932 movl %eax, Range(BP)
933 #endif
935 // *(p) += (kBitModelTotal /*2048*/ - *(p)) >> kNumMoveBits /*5*/;
937 movw $2048, %ax
939 // return 0;
941 jmp Bit1_2
943 // }
944 // else {
946 Bit1_1:
948 // Range -= bound; Code -= bound;
950 #ifdef ONLY8086
951 subw %ax, Range(BP)
952 sbbw %dx, Range+2(BP)
953 subw %ax, Code(BP)
954 sbbw %dx, Code+2(BP)
955 #else
956 subl %eax, Range(BP)
957 subl %eax, Code(BP)
958 #endif
960 // *(p) -= (*(p)) >> kNumMoveBits /*5*/;
962 movw $31, %ax
964 // return 1;
966 stc
967 Bit1_2:
968 pushf
969 subw (BP, DI), %ax
970 #ifdef ONLY8086
971 movb $5, %cl
972 sarw %cl, %ax
973 #else
974 sarw $5, %ax
975 #endif
976 addw %ax, (BP, DI)
977 popf
978 #ifdef ONLY8086
979 popw %di
980 popw %dx
981 popw %cx
982 popw %ax
983 #else
984 popal
985 #endif
986 sbb AX, AX
988 // }
989 // }
991 ret
993 RangeDecoder:
995 /*
996 * input ax=probs cx=numLevels (< 8) bx=1
997 * output ax=res (backward), dh (forward)
998 * update bound, Range, Code, ds:si
999 * scratch flags, cx=0, dl
1000 */
1002 push BX
1004 // { int i = numLevels; res = 1;
1005 mov BX, DX // res = 1
1007 // do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0);
1009 RangeDecoder_1:
1010 push AX
1011 call Bit1axdx // C,%ax = Bit1(prob+%ax)
1012 rclb $1, %dl // res <<= 1; res |= C
1013 andb %bl, %al // current bit
1014 orb %al, %bh // store in bh
1015 shlb $1, %bl // update max
1016 pop AX
1017 loop RangeDecoder_1
1019 // res -= (1 << numLevels); }
1021 xchg AX, BX // move bh to dh
1022 xchg AX, DX // and dl to al
1023 sub %dl, %al // sub max
1024 pop BX
1025 ret