wok view memtest/stuff/unlzma.S @ rev 13340

get-playonlinux: update deps
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Sep 05 14:59:33 2012 +0200 (2012-09-05)
parents 3fd2f1836a8c
children 3524d7f7da59
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_LP 0
23 #define PROP_LC 3
24 #define PROPS (PROP_LC+(PROP_LP*9)+(PROP_PB*45))
26 // static const Byte *Buffer;
27 // static UInt32 bound, Code, Range;
29 /*
30 * Buffer register DS:SI
31 * all var based ws=ss:bp
32 */
34 rep0 = -4 // long
35 rep1 = rep0-4 // long
36 rep2 = rep0-8 // long
37 rep3 = rep0-12 // long
38 state = -17 // byte, 0..11
39 posState = state-1 // byte, 0..15
40 posState2 = posState-1 // byte, 0..15
41 scratched = rep0-16 // byte = 1
42 Code = -24 // long
43 outStream = -28 // long
44 nowPos = outStream // long
45 Range = Code-8 // long
46 #define LOCALS 32
48 // int LzmaDecode(CLzmaDecoderState *vs,
49 // const unsigned char *inStream,
50 // unsigned char *outStream)
51 // {
52 // CProb *p = vs->Probs;
53 // SizeT nowPos = 0;
54 // #define posStateMask = (1 << (vs->Properties.pb)) - 1;
55 // #define literalPosMask = (1 << (vs->Properties.lp)) - 1;
56 // int lc = vs->Properties.lc, state = 0, len = 0;
57 // UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
58 //
59 // {
60 // UInt32 i, numProbs = Literal /*1846*/
61 // + ((UInt32)LZMA_LIT_SIZE /*768*/ << (lc + vs->Properties.lp));
62 // for (i = 0; i < numProbs; i++) p[i] = kBitModelTotal /*2048*/ >> 1;
64 #define WS (1846+(768<<(PROP_LC+PROP_LP)))
65 #if (WS+WS+LOCALS) >= 65000
66 /* MAX WS = (1846+(768<<(8+4))) > 3MB! */
67 #error invalid (lc,lp,pb) : out of memory
68 #endif
70 ws1 = WS
71 ws2 = ws1*2
72 ws = ws2+LOCALS+15
74 #ifndef FLAT32
75 #define AX %ax
76 #define BX %bx
77 #define CX %cx
78 #define DX %dx
79 #define SI %si
80 #define DI %di
81 #define BP %bp
82 #define SP %sp
83 #define CWD cwd
84 #else
85 #define AX %eax
86 #define BX %ebx
87 #define CX %ecx
88 #define DX %edx
89 #define SI %esi
90 #define DI %edi
91 #define BP %ebp
92 #define SP %esp
93 #define CWD cdq
94 #endif
95 /*
96 * LzmaDecode:
97 #ifndef FLAT32
98 * input ds:si=inStream, es:di=outStream
99 * output outStream[], ds:si, es:di
100 .code 16
101 #else
102 * input esi=inStream, edi=outStream
103 * output outStream[], esi, edi
104 .code 32
105 #endif
106 */
108 mov $ws1, CX
109 lzd1:
110 pushw $2048/2
111 loop lzd1
112 mov SP, BP
113 movb $((LOCALS+3)/4)*2, %cl
114 initlocals:
115 pushl $1
116 loop initlocals
118 #ifndef FLAT32
119 movb $4, %cl
120 movw %es, %bx
121 shrw %cl, %bx
122 movw %es, %dx
123 shlw %cl, %dx
124 addw %dx, %di
125 movw %di, outStream(%bp)
126 adcb %bh, outStream+2(%bp)
127 incw %cx
128 #else
129 movb $5, %cl
130 movl %edi, outStream(%ebp)
131 #endif
133 // Byte previousByte = 0;
134 xor BX, BX
136 // #define RC_INIT(buffer)
137 // Buffer = buffer; Code = 0; Range = 0xFFFFFFFF;
138 // { int i; for(i=0; i<5; i++) { Code = (Code<<8) | RC_READ_BYTE; }}
139 // }
140 // RC_INIT(inStream);
142 add $13, SI // skip header
143 setrep:
144 call RC_LOAD_BYTE
145 decb Range(BP)
146 loop setrep
148 lzdmainlp:
149 // while(1) {
150 // CProb *prob;
151 // int posState = (int)((nowPos) & posStateMask);
152 //
153 // prob = p + IsMatch /*0*/ + (state << kNumPosBitsMax /*4*/) + posState;
154 // if (Bit0(prob)) { /* char */
156 xor DX, DX
157 call Bit1state // Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
158 mov $state, DI
159 jc lzdstring
161 // prob = p + Literal /*1846*/ + (LZMA_LIT_SIZE /*768*/ *
162 // ((((nowPos) & literalPosMask) << lc) + (previousByte >> (8 - lc))));
164 #if PROP_LC != 0
165 shrb $8-PROP_LC, %bl
166 #endif
168 #if PROP_LP != 0
169 movb posState2(BP), %dl
170 shl $PROP_LC, DX
171 movb $0, %bh
172 add BX, DX
173 #endif
175 movb $3, %ah
176 mul BX // dx = 3*bh
177 add $1846, AX
179 // int symbol = 1;
181 CWD
182 inc DX // symbol = 1
183 xchg AX, CX // save prob
185 // if (state >= kNumLitStates /*7*/) { /* previous was string */
186 // if (state < 4) state = 0;
188 lzd6z:
189 subb $3, (BP, DI)
191 // if (state < 4) state = 0;
193 jnc lzd6
194 movb %dh, (BP, DI) // %dh = 0
196 lzd6:
197 // else if (state < 10) state -= 3;
199 cmpb $10-3, (BP, DI)
201 // else state -= 6;
203 jnb lzd6z
204 cmpb $7-3-1, (BP, DI)
205 jbe lzd3
207 // int matchByte = outStream[nowPos - rep0];
209 call DicoRep02ESDI // %bl = outStream[nowPos - rep0];
211 // do {
212 // int bit;
213 // CProb *probLit;
214 // matchByte <<= 1; bit = (matchByte & 0x100);
216 movb $1, %bh
217 lzd4:
218 shlb $1, %bl // matchByte <<= 1
219 sbb DI, DI // save bit=C
221 // probLit = prob + 0x100 + bit + symbol;
223 mov CX, AX // restore prob
224 adcb %bh, %ah // + bit + 0x100
226 // RC_GET_BIT2(probLit, symbol, if (bit) break, if (!bit) break)
228 call Bit1axdx // C,%ax = Bit1(prob+%ax)
229 rclb $1, %dl // symbol <<= 1; symbol |= C
230 jc lzd5 // if symbol >= 0x100
231 cmp DI, AX
232 jz lzd4 // if bit == Bit1(prob+%ax)
234 // } while (symbol < 0x100);
235 // }
236 lzd3:
237 // while (symbol < 0x100) {
238 // CProb *probLit = prob + symbol;
239 // RC_GET_BIT(probLit, symbol)
240 // }
242 xor BX, BX
243 jmp lzd4
244 lzd5:
246 // outStream[nowPos++] = previousByte = (Byte)symbol;
248 xchg AX, DX
249 call outchar // %bl = outStream[nowPos++] = %al;
250 jmp lzdmainlp
252 // }
254 lzdstring:
255 mov $1, CX
257 // else { /* string */
258 // prob = p + IsRep /*192*/ + state;
260 movb $192, %dl
261 addb (BP, DI), %dl
262 mov $rep0, DI
264 // if (Bit0(prob)) {
266 call Bit1dx // Bit1(prob)
267 jc lzd8
269 // rep3 = rep2; rep2 = rep1; rep1 = rep0;
270 // state = (state < kNumLitStates /*7*/) ? 0 : 3;
272 stc
274 // prob = p + LenCoder /*818*/;
276 mov $818, DX
278 // }
280 jmp lzd11a
282 // else {
283 lzd8:
284 // prob += kNumStates /*12*/;
285 // if (Bit0(prob)) {
286 call Bit1dx12 // prob += 12; Bit1(prob)
287 jc lzd11
288 // prob = p + IsRep0Long /*240*/ + (state << kNumPosBitsMax /*4*/)
289 // + posState;
290 movb $240, %dl // dh=0
292 // if (Bit0(prob)) {
294 call Bit1state // Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
295 jc lzd12
297 // // if (nowPos == 0) return LZMA_RESULT_DATA_ERROR;
298 // state = (state < kNumLitStates /*7*/) ? 9 : 11;
300 movb $9, %dl
302 // len++; goto string;
303 jmp lzd13string // ax = 0
304 // }
305 // }
306 // else {
307 lzd11:
308 // UInt32 distance = rep1;
309 // prob += kNumStates /*12*/;
310 // if (!Bit0(prob)) {
312 call Bit1dx12 // prob += 12; Bit1(prob)
313 jnc lzd11z
315 // prob += kNumStates /*12*/;
316 // if (Bit0(prob)) distance = rep2;
318 call Bit1dx12 // prob += 12; Bit1(prob)
319 lzd11a:
320 adcb %cl, %cl
322 // else { distance = rep3; rep3 = rep2; }
323 // rep2 = rep1;
324 // }
325 // rep1 = rep0; rep0 = distance;
327 lzd11z:
328 shl $2, CX // 8->32 bits
329 sub CX, DI // &rep[cx]
330 movl (BP, DI), %eax
331 rotreplp:
332 movb 4(BP, DI), %bl
333 movb %bl, (BP, DI)
334 inc DI
335 loop rotreplp
336 testb %dh, %dh
337 jnz lzd10
338 movl %eax, (BP, DI)
340 // }
341 lzd12:
342 // state = (state < kNumLitStates /*7*/) ? 8 : 11;
344 movb $0x08, %cl
346 // prob = p + RepLenCoder /*1332*/;
348 mov $1332, DX
350 // }
351 lzd10:
352 push CX // CX = 0
354 // { /* get len */
355 // int numBits, offset;
356 // CProb *probLen = prob + LenChoice /*0*/;
357 // numBits = kLenNumLowBits /*3*/;
359 movb $8, %cl // numBits : 3,3,8
361 // if (Bit0(probLen)) {
363 call Bit1dx // Bit1(prob)
364 xchg AX, BX
365 inc DX
366 jnc lzd15 // bx=0
368 // probLen = prob + LenLow/*2*/ + (posState << kLenNumLowBits/*3*/);
369 // offset = 0;
370 // }
371 // else {
372 // probLen = prob + LenChoice2 /*1*/;
374 call Bit1dx // Bit1(prob)
375 add AX, BX
377 #if PROP_PB != 0
378 inc AX // ah=0
379 #endif
380 jc lzd16 // %ax=0, %bx=-2
381 lzd15:
382 #if PROP_PB != 0
383 movb $8, %al
384 mulb posState(BP)
385 #endif
387 // if (Bit0(probLen)) {
388 // probLen = prob + LenMid/*130*/ + (posState << kLenNumMidBits/*3*/);
390 movb $3, %cl // numBits : 3,3,8
391 lzd16:
392 #if PROP_PB != 0
393 add $2-128-1, AX // probLen : 2,130,258
394 #else
395 mov $2-128-1, AX // probLen : 2,130,258
396 #endif
397 add DX, AX
398 mov $-8+1, DX // offset : 0,8,16
399 lzdargslp:
400 add $8, DX
401 add $128, AX
402 inc BX
403 jle lzdargslp // leave with bx=1
405 // offset = kLenNumLowSymbols /*8*/;
406 // //numBits = kLenNumMidBits /*3*/;
407 // }
408 // else {
409 // probLen = prob + LenHigh /*258*/;
410 // offset = kLenNumLowSymbols /*8*/ + kLenNumMidSymbols /*8*/;
411 // numBits = kLenNumHighBits /*8*/;
412 // }
413 // }
414 // RangeDecoderBitTreeDecode(probLen, numBits, len); len += offset;
416 push DX
417 call RangeDecoder // %ax=probs, %cx=numLevels, %ax=res
418 pop DX
419 add DX, AX // offset
420 pop DX // 0
421 lzd13string:
422 push AX
424 // state = (state < kNumLitStates /*7*/) ? dl : dl|3;
426 movb $7, %cl
427 cmpb %cl, state(BP)
428 jb new_state
429 orb $3, %dl
430 new_state:
431 movb %dl, state(BP)
433 // } /* get len */
434 // if (state < 4) {
436 cmpb $4-1, %dl
437 ja lzd19
439 // int posSlot;
440 // state += kNumLitStates /*7*/;
442 addb %cl, state(BP)
444 // prob = p + PosSlot /*432*/ + (((len < kNumLenToPosStates /*4*/) ?
445 // len : kNumLenToPosStates - 1) << kNumPosSlotBits /*6*/);
447 cmp $4+1, AX
448 jb lzd21
449 mov $3+1, AX
451 lzd21:
453 dec CX // cx = 6
454 shl %cl, AX
455 add $432-64, AX
457 // RangeDecoderBitTreeDecode(prob, kNumPosSlotBits /*6*/, posSlot);
459 call RangeDecoder // %ax=probs, %cx=numLevels, %ax=res
461 // if (posSlot >= kStartPosModelIndex /*4*/) {
462 // int numDirectBits = ((posSlot >> 1) - 1);
464 #ifndef FLAT32
465 movw %cx, 2(%bp, %di) // %cx = 0
466 #endif
467 mov AX, (BP, DI)
468 mov AX, CX
469 shrw $1, CX
470 dec CX
471 cmpb $4, %al
472 jb lzd22
474 // rep0 = (2 | ((UInt32)posSlot & 1));
476 andb %bl, (BP, DI) // %bx=1
477 orb $2, (BP, DI)
479 // if (posSlot < kEndPosModelIndex /*14*/) {
481 cmpb $14, %al
482 jnb lzd23
484 // rep0 <<= numDirectBits;
486 neg AX
487 shll %cl, (BP, DI)
488 add (BP, DI), AX
490 // prob = p + SpecPos /*688*/ + rep0 - posSlot - 1;
492 add $687, AX
493 jmp lzd24
495 // }
496 // else {
497 lzd23:
498 // numDirectBits -= kNumAlignBits /*4*/;
499 // do {
500 // RC_NORMALIZE; Range >>= 1; rep0 <<= 1;
501 // if (Code >= Range) { Code -= Range; rep0 |= 1; }
503 lzd23z:
504 call RC_NORMALIZE
505 shrl $1, Range(BP)
506 movl Range(BP), %eax
507 cmpl Code(BP), %eax
508 ja lzd25
509 subl %eax, Code(BP)
510 stc
511 lzd25:
512 rcll $1, (BP, DI)
514 // } while (--numDirectBits != 0);
516 cmpb $4+1, %cl
517 loopne lzd23z
519 // prob = p + Align /* 802 */; numDirectBits = kNumAlignBits /*4*/;
520 // rep0 <<= numDirectBits;
522 shll %cl, (BP, DI)
523 mov $802, AX
524 // }
526 lzd24:
527 call RangeDecoder // %ax=probs, %cx=numLevels, %ax=res
529 // {
530 // int i = 1, mi = 1;
531 // do {
532 // CProb *prob3 = prob + mi;
533 // RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
535 orb %dh, (BP, DI) // update rep0 with DirectBits
537 // i <<= 1;
538 // } while(--numDirectBits != 0);
539 // }
540 // } else rep0 = posSlot;
541 lzd22:
542 // if (++rep0 == (UInt32)(0)) break; /* EOF */
544 incl (BP, DI)
546 lzd19:
547 pop CX
548 jz lzdone
550 // }
551 // len += kMatchMinLen;/*2*/
553 inc CX
555 // string: // if (rep0 > nowPos) return LZMA_RESULT_DATA_ERROR;
556 // do {
557 lzd13z:
558 // previousByte = outStream[nowPos - rep0];
559 // outStream[nowPos++] = previousByte;
561 call outcharDico // %bl = outStream[nowPos++] = outStream[nowPos - rep0]
563 // } while(--len != 0);
565 loop lzd13z
567 // } /* char/string */
568 // }
570 jmp lzdmainlp
572 lzdone:
573 // //RC_NORMALIZE;
574 // //*inSizeProcessed = (SizeT)(Buffer - inStream); *outSizeProcessed = nowPos;
575 // return LZMA_RESULT_OK;
576 call Dico2ESDI // set es & di (rep0 = 0)
577 lea ws2(BP), SP // dealloc
578 ret
579 // }
581 // al = outStream[nowPos - rep0];
583 /*
584 * output es:di, al
585 * scratch bh, cl, flags
586 */
588 DicoRep02ESDI:
589 stc
591 // bl = outStream[nowPos];
593 /*
594 * output es:di, bl
595 * scratch bh, cl, flags
596 */
598 Dico2ESDI:
599 #ifndef FLAT32
600 movl nowPos(%bp), %ebx
601 jnc Dico2ESDIz
602 subl rep0(%bp), %ebx
603 Dico2ESDIz:
604 movw %bx, %di
605 xorw %bx, %bx
606 shrl $4, %ebx
607 movw %bx, %es
608 movb %es:(%di), %bl
609 #else
610 movl nowPos(%bp), %edi
611 jnc Dico2ESDIz
612 subl rep0(%bp), %edi
613 Dico2ESDIz:
614 movb (%edi), %bl
615 #endif
616 ret
618 outcharDico:
620 // bl = outStream[nowPos++] = outStream[nowPos - rep0]
622 /*
623 * output es:di, bl
624 * update nowPos
625 * scratch ax, dx, bh, cl, flags
626 */
628 call DicoRep02ESDI // %bl = outStream[nowPos - rep0]
629 xchg AX, BX
630 outchar:
632 // bl = outStream[nowPos++] = previousByte = al;
634 /*
635 * output bl
636 * update nowPos
637 * scratch ax, dx, bh, di, cl, flags
638 */
640 clc
641 call Dico2ESDI
642 stosb
643 xchg AX, BX // previous byte
645 // int posState = (int)((nowPos) & posStateMask);
647 #if PROP_PB != 0 && PROP_LP != 0
648 addw $0x0101, posState2(BP)
649 andb $(((1 << PROP_PB) -1)<<8)+((1 << PROP_LP) -1), posState2(BP)
650 #else
651 # if PROP_PB != 0
652 incb posState(BP)
653 andb $((1 << PROP_PB) -1), posState(BP)
654 # endif
655 # if PROP_LP != 0
656 incb posState2(BP)
657 andb $((1 << PROP_LP) -1), posState2(BP)
658 # endif
659 #endif
660 incl nowPos(BP)
661 ret
663 //
664 // #define RC_NORMALIZE if (Range < kTopValue)
665 // { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
667 /*
668 * update Range, Code, ds:si
669 * scratch flags
670 */
672 RC_NORMALIZE:
673 cmpb $0, Range+3(BP)
674 jne RC_NORMALIZE_1
675 RC_LOAD_BYTE:
676 push AX
677 shll $8, Range(BP)
678 shll $8, Code(BP)
679 #ifndef FLAT32
680 testw %si, %si
681 jns RC_READ_BYTE
682 movw %ds, %ax
683 incw %ax
684 movw %ax, %ds
685 addw $-16, %si
686 RC_READ_BYTE:
687 #endif
688 lodsb
689 movb %al, Code(BP)
690 pop AX
691 RC_NORMALIZE_1:
692 ret
694 // Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
696 Bit1state:
697 movb $16, %al
698 mulb state(BP)
699 # if PROP_PB != 0
700 addb posState(BP), %al
701 # endif
702 Bit1axdx:
703 add DX, AX
704 jmp Bit1
706 // prob += 12; Bit1(prob)
708 Bit1dx12:
709 add $12, DX
710 Bit1dx:
711 mov DX, AX
713 // static int Bit1(CProb *p)
715 Bit1:
716 /*
717 * input ax=p
718 * output C, ax
719 * update bound, Range, Code, ds:si
720 * scratch flags
721 */
723 // {
724 // RC_NORMALIZE;
726 call RC_NORMALIZE // kill %ax, update %si
728 pushal
730 xchg AX, DI
731 add DI, DI // short *
734 // bound = (Range>>kNumBitModelTotalBits /*11*/) * *(p);
736 movl Range(BP), %eax
737 shrl $11, %eax
738 movzwl (BP, DI), %edx
739 mull %edx
741 // if (Code < bound) {
743 cmpl Code(BP), %eax
744 jbe Bit1_1
746 // Range = bound;
748 movl %eax, Range(BP)
750 // *(p) += (kBitModelTotal /*2048*/ - *(p)) >> kNumMoveBits /*5*/;
752 movw $2048, %ax
754 // return 0;
756 jmp Bit1_2
758 // }
759 // else {
761 Bit1_1:
763 // Range -= bound; Code -= bound;
765 subl %eax, Range(BP)
766 subl %eax, Code(BP)
768 // *(p) -= (*(p)) >> kNumMoveBits /*5*/;
770 movw $31, %ax
772 // return 1;
774 stc
775 Bit1_2:
776 pushf
777 subw (BP, DI), %ax
778 sarw $5, %ax
779 addw %ax, (BP, DI)
780 popf
781 popal
782 sbb AX, AX
784 // }
785 // }
787 ret
789 RangeDecoder:
791 /*
792 * input ax=probs cx=numLevels (< 8) bx=1
793 * output ax=res (backward), dh (forward)
794 * update bound, Range, Code, ds:si
795 * scratch flags, cx=0, dl
796 */
798 push BX
800 // { int i = numLevels; res = 1;
801 mov BX, DX // res = 1
803 // do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0);
805 RangeDecoder_1:
806 push AX
807 call Bit1axdx // C,%ax = Bit1(prob+%ax)
808 rclb $1, %dl // res <<= 1; res |= C
809 andb %bl, %al // current bit
810 orb %al, %bh // store in bh
811 shlb $1, %bl // update max
812 pop AX
813 loop RangeDecoder_1
815 // res -= (1 << numLevels); }
817 xchg AX, BX // move bh to dh
818 xchg AX, DX // and dl to al
819 sub %dl, %al // sub max
820 pop BX
821 ret