Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "assert.h"
7 : #include "options.h"
8 : #include "cnst.h"
9 : #include "rom_com.h"
10 : #include "prot_fx.h"
11 :
12 : /*---------------------------------------------------------------
13 : * Ari decode 14 bits routines
14 : -------------------------------------------------------------*/
15 :
16 0 : Word32 ari_decode_overflow_fx( Tastat *s )
17 : {
18 0 : return ( L_sub( L_sub( s->high, 1 ), s->low ) <= 0 );
19 : }
20 :
21 : /**
22 : * \brief Start ArCo decoding
23 : *
24 : * \param[i/o] st
25 : * \param[o] s
26 : */
27 569976 : void ari_start_decoding_14bits_fx(
28 : Decoder_State *st,
29 : Tastat *s )
30 : {
31 : Word32 val;
32 :
33 569976 : val = L_and( L_deposit_l( (Word16) get_next_indice_fx( st, cbitsnew ) ), 0xffffL );
34 :
35 569976 : s->low = L_deposit_l( 0 );
36 569976 : move32();
37 569976 : s->high = L_add( ari_q4new, 1 );
38 569976 : move32();
39 569976 : s->value = val;
40 569976 : move32();
41 569976 : }
42 :
43 0 : Word16 ari_start_decoding_14bits_prm_fx(
44 : const Word16 *ptr, /* Q0 */
45 : Word16 bp,
46 : Tastat *s )
47 : {
48 : Word32 val;
49 : Word16 i;
50 : const Word16 *p;
51 :
52 0 : val = L_deposit_l( 0 );
53 :
54 0 : p = ptr + bp; /* Q0 */
55 :
56 0 : FOR( i = 0; i < cbitsnew; i++ )
57 : {
58 0 : val = L_shl( val, 1 );
59 0 : IF( *( p + i ) )
60 : {
61 0 : val = L_add( val, 1 );
62 : }
63 : }
64 0 : s->low = L_deposit_l( 0 );
65 0 : move32();
66 0 : s->high = L_add( ari_q4new, 1 );
67 0 : move32();
68 0 : s->value = val;
69 0 : move32();
70 :
71 0 : return add( bp, i );
72 : }
73 :
74 70071 : static Word16 ari_lookup_s17_fx(
75 : Word32 cum,
76 : Word32 range,
77 : UWord16 const *cum_freq /* Q0 */
78 : )
79 : {
80 : Word32 tmp;
81 : const UWord16 *p;
82 : Word16 range_l, range_h;
83 :
84 70071 : p = cum_freq;
85 :
86 : /* Note: For each indirect addressing p[i], we assume a tmp pointer init followed by a costfree reading the value */
87 : /* If the value multiplied by range is greater than cum, the pointer p is set to the tmp pointer */
88 : /* tmp_p = p+8; if (tmp_p[0]*range>cum) p = tmp_p; */
89 :
90 : /* max value in p[x] is 16384 => 14 bits */
91 70071 : range_l = extract_l( L_and( range, 0x7FFF ) );
92 70071 : range_h = extract_l( L_shr( range, 15 ) );
93 :
94 70071 : tmp = L_multi31x16_X2( range_h, range_l, p[8] );
95 70071 : IF( GT_32( tmp, cum ) )
96 : {
97 7727 : p = p + 8;
98 : }
99 :
100 70071 : tmp = L_multi31x16_X2( range_h, range_l, p[4] );
101 70071 : IF( GT_32( tmp, cum ) )
102 : {
103 17418 : p = p + 4;
104 : }
105 :
106 :
107 70071 : tmp = L_multi31x16_X2( range_h, range_l, p[2] );
108 70071 : IF( GT_32( tmp, cum ) )
109 : {
110 8005 : p = p + 2;
111 : }
112 :
113 70071 : tmp = L_multi31x16_X2( range_h, range_l, p[1] );
114 70071 : IF( GT_32( tmp, cum ) )
115 : {
116 17575 : p = p + 1;
117 :
118 17575 : tmp = L_multi31x16_X2( range_h, range_l, p[1] );
119 17575 : test();
120 17575 : IF( EQ_32( (Word32) ( &cum_freq[15] - p ), 0 ) && ( GT_32( tmp, cum ) ) )
121 : {
122 3849 : p = p + 1;
123 : }
124 : }
125 :
126 : /* return extract_l(L_shr(L_sub((Word32)p,(Word32)cum_freq),1)); */
127 70071 : L_sub( 0, 0 );
128 70071 : L_shr( 0, 0 ); /* pointer subtraction */
129 70071 : return extract_l( p - cum_freq );
130 : }
131 :
132 2975066 : static Word16 ari_lookup_s27_fx(
133 : Word32 cum,
134 : Word32 range,
135 : UWord16 const *cum_freq /* Q0 */
136 : )
137 : {
138 : Word32 tmp;
139 : Word16 range_l, range_h;
140 : Word16 il, ih, im;
141 :
142 : /* Note: For each indirect addressing p[i], we assume a tmp pointer init followed by a costfree reading the value */
143 : /* If the value multiplied by range is greater than cum, the pointer p is set to the tmp pointer */
144 : /* tmp_p = p+8; if (tmp_p[0]*range>cum) p = tmp_p; */
145 :
146 : /* max value in p[x] is 16384 => 14 bits */
147 2975066 : range_l = extract_l( L_and( range, 0x7FFF ) );
148 2975066 : range_h = extract_l( L_shr( range, 15 ) );
149 :
150 : /* begin change when compared with ari_decode_14bits_s17_ext_fx,
151 : starting with line: tmp = L_multi31x16_X2(range_h, range_l, p[8]); */
152 2975066 : il = 0;
153 2975066 : move16();
154 2975066 : ih = 27;
155 2975066 : move16();
156 :
157 : /* do a five step binary search, using the interval [il, ih) */
158 2975066 : im = 13;
159 2975066 : move16(); /* (il + ih) >> 1 */
160 2975066 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
161 2975066 : tmp = L_sub( tmp, cum );
162 2975066 : if ( ( tmp > 0 ) )
163 : {
164 1801905 : il = im;
165 1801905 : move16();
166 : }
167 2975066 : if ( ( tmp <= 0 ) )
168 : {
169 1173161 : ih = im;
170 1173161 : move16();
171 : }
172 :
173 2975066 : im = shr( add( il, ih ), 1 );
174 2975066 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
175 2975066 : tmp = L_sub( tmp, cum );
176 2975066 : if ( tmp > 0 )
177 : {
178 1044190 : il = im;
179 1044190 : move16();
180 : }
181 2975066 : if ( tmp <= 0 )
182 : {
183 1930876 : ih = im;
184 1930876 : move16();
185 : }
186 :
187 2975066 : im = shr( add( il, ih ), 1 );
188 2975066 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
189 2975066 : tmp = L_sub( tmp, cum );
190 2975066 : if ( tmp > 0 )
191 : {
192 1455998 : il = im;
193 1455998 : move16();
194 : }
195 2975066 : if ( tmp <= 0 )
196 : {
197 1519068 : ih = im;
198 1519068 : move16();
199 : }
200 :
201 2975066 : im = shr( add( il, ih ), 1 );
202 2975066 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
203 2975066 : tmp = L_sub( tmp, cum );
204 2975066 : if ( tmp > 0 )
205 : {
206 1748597 : il = im;
207 1748597 : move16();
208 : }
209 2975066 : if ( tmp <= 0 )
210 : {
211 1226469 : ih = im;
212 1226469 : move16();
213 : }
214 :
215 2975066 : IF( GT_16( sub( ih, il ), 1 ) ) /* if the interval has more than one symbol */
216 : {
217 : /* here, only ih == il + 2 is possible, which means two symbols in the interval */
218 2530258 : im = add( il, 1 ); /* (il + ih) >> 1 */
219 2530258 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
220 2530258 : tmp = L_sub( tmp, cum );
221 2530258 : if ( tmp > 0 )
222 : {
223 1235998 : il = im;
224 1235998 : move16();
225 : }
226 : }
227 :
228 2975066 : return il;
229 : }
230 :
231 1144802 : static Word16 ari_lookup_bit_fx(
232 : Word32 cum,
233 : Word32 range,
234 : UWord16 const *cum_freq /* Q0 */
235 : )
236 : {
237 1144802 : Word16 symbol = 0; /* initialize just to avoid compiler warning */
238 1144802 : move16();
239 :
240 : (void) cum_freq;
241 :
242 1144802 : range = L_shl( range, 13 ); /* L_multi31x16_X2(range_h, range_l, 8192) */
243 1144802 : cum = L_sub( range, cum );
244 :
245 1144802 : if ( cum > 0 )
246 : {
247 573899 : symbol = 1;
248 573899 : move16();
249 : }
250 1144802 : if ( cum <= 0 )
251 : {
252 570903 : symbol = 0;
253 570903 : move16();
254 : }
255 :
256 1144802 : return symbol;
257 : }
258 :
259 4189939 : static Word16 ari_decode_14bits_ext_fx(
260 : Decoder_State *st,
261 : Tastat *s,
262 : UWord16 const *cum_freq, /* Q0 */
263 : Word16 ( *lookup_fn )( Word32 cum, Word32 range, UWord16 const *cum_freq ) )
264 : {
265 : Word32 cum;
266 : Word16 symbol;
267 : Word32 low;
268 : Word32 high;
269 : Word32 range;
270 : Word32 value;
271 : Word16 i;
272 :
273 4189939 : low = s->low;
274 4189939 : move32();
275 4189939 : high = s->high;
276 4189939 : move32();
277 4189939 : value = s->value;
278 4189939 : move32();
279 :
280 4189939 : range = L_sub( high, low );
281 :
282 4189939 : cum = L_add( L_shl( L_sub( value, low ), stat_bitsnew ), sub( shl( 1, stat_bitsnew ), 1 ) );
283 4189939 : if ( cum < 0 )
284 : {
285 0 : cum = 0x7fffffff;
286 0 : move32();
287 : }
288 :
289 4189939 : symbol = lookup_fn( cum, range, cum_freq );
290 :
291 4189939 : high = L_add( low, mul_sbc_14bits( range, cum_freq[symbol] ) );
292 4189939 : low = L_add( low, mul_sbc_14bits( range, cum_freq[add( symbol, 1 )] ) );
293 :
294 15366230 : FOR( i = 0; i < 0x7FFF; i++ )
295 : {
296 : Word32 L_msb_diff, L_msb_low, L_msb_high;
297 :
298 15366230 : L_msb_high = L_shr( L_sub( high, 1 ), 14 );
299 15366230 : L_msb_low = L_shr( low, 14 );
300 15366230 : L_msb_diff = L_sub( L_msb_high, L_msb_low );
301 15366230 : IF( GE_32( L_msb_diff, 2 ) )
302 : {
303 4189939 : BREAK;
304 : }
305 11176291 : assert( NE_16( tab_ari_qnew[L_msb_high][L_msb_low], 0x0CCC ) );
306 11176291 : assert( NE_16( tab_ari_qnew[L_msb_high][L_msb_low], 0x0BBB ) );
307 11176291 : low = L_msu( low, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
308 11176291 : low = L_shl( low, 1 );
309 11176291 : high = L_msu( high, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
310 11176291 : high = L_shl( high, 1 );
311 11176291 : value = L_msu( value, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
312 11176291 : value = L_mac0( L_shl( value, 1 ), 1, get_next_indice_1_fx( st ) );
313 : }
314 :
315 4189939 : s->low = low;
316 4189939 : move32();
317 4189939 : s->high = high;
318 4189939 : move32();
319 4189939 : s->value = value;
320 4189939 : move32();
321 :
322 4189939 : return symbol;
323 : }
324 :
325 :
326 : /**
327 : * \brief Only for 17 symbols with new extended Tables
328 : */
329 70071 : Word16 ari_decode_14bits_s17_ext_fx( Decoder_State *st, Tastat *s, UWord16 const *cum_freq )
330 : {
331 70071 : return ari_decode_14bits_ext_fx( st, s, cum_freq, ari_lookup_s17_fx );
332 : }
333 :
334 : /**
335 : * \brief Only for 27 symbols with new extended Tables
336 : */
337 2975066 : Word16 ari_decode_14bits_s27_ext_fx( Decoder_State *st, Tastat *s, UWord16 const *cum_freq )
338 : {
339 2975066 : return ari_decode_14bits_ext_fx( st, s, cum_freq, ari_lookup_s27_fx );
340 : }
341 :
342 : /**
343 : * \brief Only for decoding one bit with uniform probability:
344 : * the equivalent cum_freq table used is {16384, 8192, 0}
345 : */
346 1144802 : Word16 ari_decode_14bits_bit_ext_fx( Decoder_State *st, Tastat *s )
347 : {
348 : static const UWord16 cum_freq[3] = { 16384, 8192, 0 };
349 1144802 : move16();
350 1144802 : move16();
351 1144802 : move16();
352 1144802 : return ari_decode_14bits_ext_fx( st, s, cum_freq, ari_lookup_bit_fx );
353 : }
354 :
355 0 : static Word16 ari_lookup_pow_fx( Tastat *s, Word16 base )
356 : {
357 : Word32 cum, range;
358 : Word16 symbol;
359 : Word16 range_h, range_l;
360 : Word16 pows[12]; /* "base" to the power of 2, 4, 8,... 2^12 */
361 : Word16 lowlim, highlim, testval;
362 : Word16 k;
363 :
364 0 : range = L_sub( s->high, s->low );
365 0 : cum = L_add( L_shl( L_sub( s->value, s->low ), stat_bitsnew ), ( ( 1 << stat_bitsnew ) - 1 ) );
366 :
367 0 : range_h = extract_l( L_shr( range, 15 ) );
368 0 : range_l = extract_l( L_and( range, 0x7FFF ) );
369 :
370 0 : symbol = 0;
371 0 : move16();
372 0 : lowlim = shr( base, 1 );
373 0 : highlim = 16384;
374 0 : move16();
375 :
376 : /* search for the interval where "cum" fits */
377 0 : IF( GT_32( L_multi31x16_X2( range_h, range_l, lowlim ), cum ) ) /* below pow-1 */
378 : {
379 0 : pows[0] = base;
380 0 : move16();
381 0 : testval = base;
382 0 : move16();
383 : /* increase exponent until it is smaller than "cum" */
384 0 : FOR( k = 1; k < 12; k++ )
385 : {
386 0 : highlim = testval;
387 0 : move16();
388 0 : pows[k] = mult_r( pows[k - 1], pows[k - 1] );
389 0 : move16();
390 0 : testval = mult_r( pows[k], base );
391 :
392 0 : IF( LE_32( L_multi31x16_X2( range_h, range_l, shr( testval, 1 ) ), cum ) ) /* found! big range is [lowlim,testval], (now narrow it down) */
393 : {
394 0 : lowlim = testval;
395 0 : move16();
396 0 : k = sub( k, 1 );
397 0 : symbol = shl( 1, k );
398 0 : BREAK;
399 : }
400 : }
401 0 : assert( LT_16( k, 12 ) ); /* maximum 2^10-1*/
402 :
403 : /* narrow the range down */
404 0 : FOR( k = sub( k, 2 ); k >= 0; k-- )
405 : {
406 0 : testval = mult_r( highlim, pows[k + 1] );
407 :
408 0 : IF( LE_32( L_multi31x16_X2( range_h, range_l, shr( testval, 1 ) ), cum ) )
409 : {
410 0 : lowlim = testval;
411 0 : move16();
412 0 : symbol = sub( symbol, shl( 1, k ) );
413 : }
414 : ELSE
415 : {
416 0 : highlim = testval;
417 0 : move16();
418 : }
419 : }
420 :
421 0 : highlim = shr( highlim, 1 );
422 0 : lowlim = shr( lowlim, 1 );
423 : }
424 :
425 0 : s->high = L_add( s->low, mul_sbc_14bits( range, highlim ) );
426 0 : move32();
427 0 : s->low = L_add( s->low, mul_sbc_14bits( range, lowlim ) );
428 0 : move32();
429 :
430 0 : return symbol;
431 : }
432 :
433 0 : static Word16 ari_lookup_sign_fx( Tastat *s, Word16 base )
434 : {
435 : Word32 cum, range;
436 : Word16 symbol;
437 :
438 : (void) base;
439 :
440 0 : range = L_sub( s->high, s->low );
441 0 : cum = L_sub( s->value, s->low );
442 0 : range = L_shr( range, 1 );
443 :
444 0 : IF( GT_32( range, cum ) )
445 : {
446 0 : symbol = 1;
447 0 : move16();
448 0 : s->high = L_add( s->low, range );
449 0 : move32();
450 : }
451 : ELSE
452 : {
453 0 : symbol = 0;
454 0 : move16();
455 0 : s->low = L_add( s->low, range );
456 0 : move32();
457 : }
458 :
459 0 : return symbol;
460 : }
461 :
462 0 : static Word16 ari_decode_14bits_notbl_fx(
463 : Word16 *ptr, /* Q0 */
464 : Word16 bp,
465 : Word16 bits,
466 : Word16 *res,
467 : Tastat *s,
468 : Word16 base,
469 : Word16 ( *lookup_fn )( Tastat *s, Word16 base ) )
470 : {
471 : Word16 symbol;
472 : Word32 low, high, value;
473 :
474 0 : symbol = lookup_fn( s, base );
475 :
476 0 : low = L_add( s->low, 0 );
477 0 : high = L_add( s->high, 0 );
478 0 : value = L_add( s->value, 0 );
479 :
480 0 : FOR( ; bp < bits; ++bp )
481 : {
482 : Word32 L_msb_diff, L_msb_low, L_msb_high;
483 :
484 0 : L_msb_high = L_shr( L_sub( high, 1 ), 14 );
485 0 : L_msb_low = L_shr( low, 14 );
486 0 : L_msb_diff = L_sub( L_msb_high, L_msb_low );
487 0 : IF( GE_32( L_msb_diff, 2 ) )
488 : {
489 0 : BREAK;
490 : }
491 0 : assert( NE_16( tab_ari_qnew[L_msb_high][L_msb_low], 0x0CCC ) );
492 0 : assert( NE_16( tab_ari_qnew[L_msb_high][L_msb_low], 0x0BBB ) );
493 0 : low = L_msu( low, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
494 0 : low = L_shl( low, 1 );
495 0 : high = L_msu( high, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
496 0 : high = L_shl( high, 1 );
497 0 : value = L_msu( value, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
498 0 : value = L_mac0( L_shl( value, 1 ), 1, ptr[bp] );
499 : }
500 :
501 0 : test();
502 0 : test();
503 0 : test();
504 0 : test();
505 0 : IF( ( lookup_fn != ari_lookup_sign_fx ) && !( NE_16( bp, bits ) || !( ( EQ_32( s->low, low ) ) && ( EQ_32( s->high, L_sub( high, 1 ) ) ) && ( EQ_32( s->value, value ) ) ) ) )
506 : {
507 : /* This should not happen except of bit errors. */
508 0 : s->high = 0;
509 0 : move32();
510 0 : s->low = 0;
511 0 : move32();
512 0 : *res = 0;
513 0 : move16();
514 0 : return -1;
515 : }
516 :
517 0 : s->low = low;
518 0 : move32();
519 0 : s->high = high;
520 0 : move32();
521 0 : s->value = value;
522 0 : move32();
523 :
524 0 : *res = symbol;
525 0 : move16();
526 :
527 0 : return bp;
528 : }
529 :
530 : /*------------------------------------------------------------------------
531 : * Function: ari_decode_14bits_pow_fx
532 : *
533 : * Decode a symbol which follows the exponential distribution. That is,
534 : * symbols are in the following intervals
535 : *
536 : * p(x = 0) = 1 - exp(- 0.5 * base * 2)
537 : * p(x = q>0) = exp(- (q-0.5)*base* 2) - exp(- (q+0.5)*base*2 )
538 : *
539 : *-------------------------------------------------------------------------*/
540 0 : Word16 ari_decode_14bits_pow_fx( Word16 *ptr, Word16 bp, Word16 bits, Word16 *res, Tastat *s, Word16 base )
541 : {
542 0 : return ari_decode_14bits_notbl_fx( ptr, bp, bits, res, s, base, ari_lookup_pow_fx );
543 : }
544 :
545 : /*------------------------------------------------------------------------
546 : * Function: ari_decode_14bits_sign_fx
547 : *
548 : * Decode a sign with equal probabilities.
549 : *-------------------------------------------------------------------------*/
550 0 : Word16 ari_decode_14bits_sign_fx( Word16 *ptr, Word16 bp, Word16 bits, Word16 *res, Tastat *s )
551 : {
552 0 : return ari_decode_14bits_notbl_fx( ptr, bp, bits, res, s, 0, ari_lookup_sign_fx );
553 : }
554 :
555 : /*---------------------------------------------------------------
556 : * ari_start_decoding_14bits_ivas()
557 : *
558 : * Start ArCo decoding
559 : *-------------------------------------------------------------*/
560 :
561 :
562 14024 : Word16 ari_start_decoding_14bits_prm_ivas_fx(
563 : const Word16 *ptr,
564 : Word16 bp,
565 : Tastat *s )
566 : {
567 : Word32 val;
568 : Word16 i;
569 : const Word16 *p;
570 :
571 14024 : val = 0;
572 14024 : move32();
573 14024 : p = ptr + bp;
574 :
575 238408 : FOR( i = 0; i < cbitsnew; i++ )
576 : {
577 224384 : val = L_or( L_shl( val, 1 ), *( p + i ) );
578 : }
579 14024 : s->low = 0;
580 14024 : move32();
581 14024 : s->high = ari_q4new;
582 14024 : move32();
583 14024 : s->value = val;
584 14024 : move32();
585 :
586 14024 : return add( bp, i );
587 : }
588 :
589 :
590 8614277 : Word16 ari_decode_14bits_pow_ivas(
591 : Word16 *ptr,
592 : Word16 bp,
593 : Word16 bits,
594 : Word16 *res,
595 : Tastat *s,
596 : UWord16 base )
597 : {
598 : UWord16 symbol;
599 : Word32 low, high;
600 : UWord32 range, value, cum;
601 : Word16 pows[12]; /* "base" to the power of 2, 4, 8,... 2^12 */
602 : Word16 lowlim, highlim, testval;
603 : Word16 k;
604 :
605 8614277 : highlim = 0;
606 8614277 : low = s->low;
607 8614277 : high = L_add( s->high, 1 );
608 8614277 : value = s->value;
609 8614277 : lowlim = 0;
610 8614277 : symbol = 0;
611 8614277 : move16();
612 8614277 : move32();
613 8614277 : move32();
614 8614277 : move16();
615 8614277 : move16();
616 :
617 8614277 : range = (UWord32) W_sub( high, low );
618 8614277 : move32();
619 :
620 : /* the value read from bitstream */
621 8614277 : assert( value >= (UWord32) low );
622 8614277 : cum = (UWord32) W_add( W_shl( ( W_sub( value, low ) ), stat_bitsnew ), ( 1 << stat_bitsnew ) - 1 );
623 8614277 : move32();
624 :
625 : /* search for the interval where "cum" fits */
626 8614277 : IF( GT_64( W_mult0_32_32( L_shr( base, 1 ), range ), cum ) ) /* below pow-1 */
627 : {
628 411859 : pows[0] = testval = base;
629 411859 : move16();
630 411859 : move16();
631 : /* increase exponent until it is smaller than "cum" */
632 658202 : FOR( k = 1; k < 12; k++ )
633 : {
634 658202 : highlim = testval;
635 658202 : move16();
636 658202 : pows[k] = mult_r( pows[k - 1], pows[k - 1] );
637 658202 : move16();
638 658202 : testval = mult_r( pows[k], base );
639 658202 : IF( LE_64( W_mult0_32_32( shr( testval, 1 ), range ), cum ) ) /* found! big range is [lowlim,testval], (now narrow it down) */
640 : {
641 411859 : lowlim = testval;
642 411859 : move16();
643 411859 : k = sub( k, 1 );
644 411859 : symbol = (UWord16) L_shl( 1, k );
645 411859 : BREAK;
646 : }
647 : }
648 411859 : assert( k < 12 ); /* maximum 2^10-1*/
649 : /* narrow the range down */
650 523136 : FOR( k--; k > 0; k-- )
651 : {
652 111277 : testval = mult_r( highlim, pows[k] );
653 111277 : IF( LE_64( W_mult0_32_32( shr( testval, 1 ), range ), cum ) )
654 : {
655 72085 : lowlim = testval;
656 72085 : move16();
657 72085 : symbol = (UWord16) L_sub( symbol, L_shl( 1, sub( k, 1 ) ) );
658 : }
659 : ELSE
660 : {
661 39192 : highlim = testval;
662 39192 : move16();
663 : }
664 : }
665 411859 : highlim = shr( highlim, 1 );
666 411859 : lowlim = shr( lowlim, 1 );
667 : }
668 : ELSE /* trivial case, above pow-1, that is, first symbol */
669 : {
670 8202418 : symbol = 0;
671 8202418 : lowlim = extract_l( L_shr( base, 1 ) );
672 8202418 : highlim = 16384;
673 8202418 : move16();
674 8202418 : move16();
675 : }
676 :
677 :
678 8614277 : high = L_add( low, mul_sbc_14bits( range, highlim ) );
679 :
680 8614277 : low = L_add( low, mul_sbc_14bits( range, lowlim ) );
681 :
682 : /*ptr init for ptr*/
683 10224248 : FOR( ; bp < bits; )
684 : {
685 4037593 : IF( GT_32( high, ari_q2new ) )
686 : {
687 3505956 : IF( GE_32( low, ari_q2new ) )
688 : {
689 579235 : value = (UWord32) W_sub( value, ari_q2new );
690 579235 : low = L_sub( low, ari_q2new );
691 579235 : high = L_sub( high, ari_q2new );
692 : }
693 : ELSE
694 : {
695 2926721 : test();
696 2926721 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
697 : {
698 499099 : value = (UWord32) W_sub( value, ari_q1new );
699 499099 : low = L_sub( low, ari_q1new );
700 499099 : high = L_sub( high, ari_q1new );
701 : }
702 : ELSE
703 : {
704 : BREAK;
705 : }
706 : }
707 : }
708 1609971 : low = L_add( low, low );
709 1609971 : high = L_add( high, high );
710 :
711 1609971 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
712 :
713 1609971 : value = (UWord32) ( W_shl( value, 1 ) | ptr[bp++] );
714 : }
715 :
716 8614277 : test();
717 8614277 : test();
718 8614277 : test();
719 8614277 : IF( !( NE_16( bp, bits ) || !( EQ_32( s->low, low ) && ( EQ_32( s->high, high ) ) && ( EQ_64( s->value, value ) ) ) ) )
720 : {
721 : /* This should not happen except of bit errors. */
722 0 : s->high = s->low = 0;
723 0 : move32();
724 0 : move32();
725 0 : *res = 0;
726 0 : move16();
727 0 : return -1;
728 : }
729 :
730 8614277 : s->low = low;
731 8614277 : s->high = L_sub( high, 1 );
732 8614277 : s->value = value;
733 8614277 : move32();
734 8614277 : move32();
735 8614277 : move32();
736 :
737 8614277 : *res = symbol;
738 8614277 : move16();
739 8614277 : return bp;
740 : }
741 :
742 411859 : Word16 ari_decode_14bits_sign_ivas(
743 : Word16 *ptr,
744 : Word16 bp,
745 : Word16 bits,
746 : Word16 *res,
747 : Tastat *s )
748 : {
749 : Word16 symbol;
750 : Word32 low, high;
751 : UWord32 range, value, cum;
752 :
753 411859 : low = s->low;
754 411859 : high = L_add( s->high, 1 );
755 411859 : value = s->value;
756 411859 : move32();
757 411859 : move32();
758 :
759 411859 : range = (UWord32) W_sub( high, low );
760 :
761 411859 : IF( LT_16( bp, bits ) )
762 : {
763 384319 : assert( value >= (UWord32) low );
764 384319 : cum = (UWord32) W_add( W_shl( ( W_sub( value, low ) ), stat_bitsnew ), ( 1 << stat_bitsnew ) - 1 );
765 384319 : IF( GT_64( W_shl( range, 13 ), cum ) )
766 : {
767 192701 : symbol = 2;
768 192701 : move16();
769 192701 : high = L_add( low, W_extract_l( W_shr( range, 1 ) ) );
770 : }
771 : ELSE
772 : {
773 191618 : symbol = 1;
774 191618 : move16();
775 191618 : low = L_add( low, W_extract_l( W_shr( range, 1 ) ) );
776 : }
777 :
778 : /*ptr init for ptr*/
779 769593 : FOR( ; bp < bits; )
780 : {
781 767599 : IF( GT_32( high, ari_q2new ) )
782 : {
783 670410 : IF( GE_32( low, ari_q2new ) )
784 : {
785 95308 : value = (UWord32) W_sub( value, ari_q2new );
786 95308 : low = L_sub( low, ari_q2new );
787 95308 : high = L_sub( high, ari_q2new );
788 : }
789 : ELSE
790 : {
791 575102 : test();
792 575102 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
793 : {
794 192777 : value = (UWord32) W_sub( value, ari_q1new );
795 192777 : low = L_sub( low, ari_q1new );
796 192777 : high = L_sub( high, ari_q1new );
797 : }
798 : ELSE
799 : {
800 : BREAK;
801 : }
802 : }
803 : }
804 385274 : low = L_add( low, low );
805 385274 : high = L_add( high, high );
806 :
807 385274 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
808 :
809 385274 : value = (UWord32) ( W_shl( value, 1 ) | ptr[bp++] );
810 : }
811 : }
812 : ELSE
813 : {
814 27540 : cum = (UWord32) W_sub( value, low );
815 27540 : range = (UWord32) W_shr( range, 1 );
816 27540 : IF( GT_64( range, cum ) )
817 : {
818 13723 : symbol = 2;
819 13723 : move16();
820 13723 : high = L_add( low, range );
821 : }
822 : ELSE
823 : {
824 13817 : symbol = 1;
825 13817 : move16();
826 13817 : low = L_add( low, range );
827 : }
828 : }
829 :
830 411859 : s->low = low;
831 411859 : s->high = L_sub( high, 1 );
832 411859 : s->value = value;
833 411859 : move32();
834 411859 : move32();
835 411859 : move32();
836 :
837 411859 : *res = symbol;
838 411859 : move16();
839 :
840 411859 : return bp;
841 : }
|