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 571609 : void ari_start_decoding_14bits_fx(
28 : Decoder_State *st,
29 : Tastat *s )
30 : {
31 : Word32 val;
32 :
33 571609 : val = L_and( L_deposit_l( (Word16) get_next_indice_fx( st, cbitsnew ) ), 0xffffL );
34 :
35 571609 : s->low = L_deposit_l( 0 );
36 571609 : move32();
37 571609 : s->high = L_add( ari_q4new, 1 );
38 571609 : move32();
39 571609 : s->value = val;
40 571609 : move32();
41 571609 : }
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 68798 : 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 68798 : 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 68798 : range_l = extract_l( L_and( range, 0x7FFF ) );
92 68798 : range_h = extract_l( L_shr( range, 15 ) );
93 :
94 68798 : tmp = L_multi31x16_X2( range_h, range_l, p[8] );
95 68798 : IF( GT_32( tmp, cum ) )
96 : {
97 7797 : p = p + 8;
98 : }
99 :
100 68798 : tmp = L_multi31x16_X2( range_h, range_l, p[4] );
101 68798 : IF( GT_32( tmp, cum ) )
102 : {
103 17315 : p = p + 4;
104 : }
105 :
106 :
107 68798 : tmp = L_multi31x16_X2( range_h, range_l, p[2] );
108 68798 : IF( GT_32( tmp, cum ) )
109 : {
110 8043 : p = p + 2;
111 : }
112 :
113 68798 : tmp = L_multi31x16_X2( range_h, range_l, p[1] );
114 68798 : IF( GT_32( tmp, cum ) )
115 : {
116 17505 : p = p + 1;
117 :
118 17505 : tmp = L_multi31x16_X2( range_h, range_l, p[1] );
119 17505 : test();
120 17505 : IF( EQ_32( (Word32) ( &cum_freq[15] - p ), 0 ) && ( GT_32( tmp, cum ) ) )
121 : {
122 3846 : p = p + 1;
123 : }
124 : }
125 :
126 : /* return extract_l(L_shr(L_sub((Word32)p,(Word32)cum_freq),1)); */
127 68798 : L_sub( 0, 0 );
128 68798 : L_shr( 0, 0 ); /* pointer subtraction */
129 68798 : return extract_l( p - cum_freq );
130 : }
131 :
132 2986954 : 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 2986954 : range_l = extract_l( L_and( range, 0x7FFF ) );
148 2986954 : 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 2986954 : il = 0;
153 2986954 : move16();
154 2986954 : ih = 27;
155 2986954 : move16();
156 :
157 : /* do a five step binary search, using the interval [il, ih) */
158 2986954 : im = 13;
159 2986954 : move16(); /* (il + ih) >> 1 */
160 2986954 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
161 2986954 : tmp = L_sub( tmp, cum );
162 2986954 : if ( ( tmp > 0 ) )
163 : {
164 1811209 : il = im;
165 1811209 : move16();
166 : }
167 2986954 : if ( ( tmp <= 0 ) )
168 : {
169 1175745 : ih = im;
170 1175745 : move16();
171 : }
172 :
173 2986954 : im = shr( add( il, ih ), 1 );
174 2986954 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
175 2986954 : tmp = L_sub( tmp, cum );
176 2986954 : if ( tmp > 0 )
177 : {
178 1044710 : il = im;
179 1044710 : move16();
180 : }
181 2986954 : if ( tmp <= 0 )
182 : {
183 1942244 : ih = im;
184 1942244 : move16();
185 : }
186 :
187 2986954 : im = shr( add( il, ih ), 1 );
188 2986954 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
189 2986954 : tmp = L_sub( tmp, cum );
190 2986954 : if ( tmp > 0 )
191 : {
192 1460397 : il = im;
193 1460397 : move16();
194 : }
195 2986954 : if ( tmp <= 0 )
196 : {
197 1526557 : ih = im;
198 1526557 : move16();
199 : }
200 :
201 2986954 : im = shr( add( il, ih ), 1 );
202 2986954 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
203 2986954 : tmp = L_sub( tmp, cum );
204 2986954 : if ( tmp > 0 )
205 : {
206 1754254 : il = im;
207 1754254 : move16();
208 : }
209 2986954 : if ( tmp <= 0 )
210 : {
211 1232700 : ih = im;
212 1232700 : move16();
213 : }
214 :
215 2986954 : 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 2539652 : im = add( il, 1 ); /* (il + ih) >> 1 */
219 2539652 : tmp = L_multi31x16_X2( range_h, range_l, cum_freq[im] );
220 2539652 : tmp = L_sub( tmp, cum );
221 2539652 : if ( tmp > 0 )
222 : {
223 1240158 : il = im;
224 1240158 : move16();
225 : }
226 : }
227 :
228 2986954 : return il;
229 : }
230 :
231 1149422 : static Word16 ari_lookup_bit_fx(
232 : Word32 cum,
233 : Word32 range,
234 : UWord16 const *cum_freq /* Q0 */
235 : )
236 : {
237 1149422 : Word16 symbol = 0; /* initialize just to avoid compiler warning */
238 1149422 : move16();
239 :
240 : (void) cum_freq;
241 :
242 1149422 : range = L_shl( range, 13 ); /* L_multi31x16_X2(range_h, range_l, 8192) */
243 1149422 : cum = L_sub( range, cum );
244 :
245 1149422 : if ( cum > 0 )
246 : {
247 576119 : symbol = 1;
248 576119 : move16();
249 : }
250 1149422 : if ( cum <= 0 )
251 : {
252 573303 : symbol = 0;
253 573303 : move16();
254 : }
255 :
256 1149422 : return symbol;
257 : }
258 :
259 4205174 : 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 4205174 : low = s->low;
274 4205174 : move32();
275 4205174 : high = s->high;
276 4205174 : move32();
277 4205174 : value = s->value;
278 4205174 : move32();
279 :
280 4205174 : range = L_sub( high, low );
281 :
282 4205174 : cum = L_add( L_shl( L_sub( value, low ), stat_bitsnew ), sub( shl( 1, stat_bitsnew ), 1 ) );
283 4205174 : if ( cum < 0 )
284 : {
285 0 : cum = 0x7fffffff;
286 0 : move32();
287 : }
288 :
289 4205174 : symbol = lookup_fn( cum, range, cum_freq );
290 :
291 4205174 : high = L_add( low, mul_sbc_14bits( range, cum_freq[symbol] ) );
292 4205174 : low = L_add( low, mul_sbc_14bits( range, cum_freq[add( symbol, 1 )] ) );
293 :
294 15436359 : FOR( i = 0; i < 0x7FFF; i++ )
295 : {
296 : Word32 L_msb_diff, L_msb_low, L_msb_high;
297 :
298 15436359 : L_msb_high = L_shr( L_sub( high, 1 ), 14 );
299 15436359 : L_msb_low = L_shr( low, 14 );
300 15436359 : L_msb_diff = L_sub( L_msb_high, L_msb_low );
301 15436359 : IF( GE_32( L_msb_diff, 2 ) )
302 : {
303 4205174 : BREAK;
304 : }
305 11231185 : assert( NE_16( tab_ari_qnew[L_msb_high][L_msb_low], 0x0CCC ) );
306 11231185 : assert( NE_16( tab_ari_qnew[L_msb_high][L_msb_low], 0x0BBB ) );
307 11231185 : low = L_msu( low, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
308 11231185 : low = L_shl( low, 1 );
309 11231185 : high = L_msu( high, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
310 11231185 : high = L_shl( high, 1 );
311 11231185 : value = L_msu( value, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
312 11231185 : value = L_mac0( L_shl( value, 1 ), 1, get_next_indice_1_fx( st ) );
313 : }
314 :
315 4205174 : s->low = low;
316 4205174 : move32();
317 4205174 : s->high = high;
318 4205174 : move32();
319 4205174 : s->value = value;
320 4205174 : move32();
321 :
322 4205174 : return symbol;
323 : }
324 :
325 :
326 : /**
327 : * \brief Only for 17 symbols with new extended Tables
328 : */
329 68798 : Word16 ari_decode_14bits_s17_ext_fx( Decoder_State *st, Tastat *s, UWord16 const *cum_freq )
330 : {
331 68798 : 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 2986954 : Word16 ari_decode_14bits_s27_ext_fx( Decoder_State *st, Tastat *s, UWord16 const *cum_freq )
338 : {
339 2986954 : 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 1149422 : Word16 ari_decode_14bits_bit_ext_fx( Decoder_State *st, Tastat *s )
347 : {
348 : static const UWord16 cum_freq[3] = { 16384, 8192, 0 };
349 1149422 : move16();
350 1149422 : move16();
351 1149422 : move16();
352 1149422 : 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 14023 : 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 14023 : val = 0;
572 14023 : move32();
573 14023 : p = ptr + bp;
574 :
575 238391 : FOR( i = 0; i < cbitsnew; i++ )
576 : {
577 224368 : val = L_or( L_shl( val, 1 ), *( p + i ) );
578 : }
579 14023 : s->low = 0;
580 14023 : move32();
581 14023 : s->high = ari_q4new;
582 14023 : move32();
583 14023 : s->value = val;
584 14023 : move32();
585 :
586 14023 : return add( bp, i );
587 : }
588 :
589 :
590 8631895 : 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 8631895 : highlim = 0;
606 8631895 : low = s->low;
607 8631895 : high = L_add( s->high, 1 );
608 8631895 : value = s->value;
609 8631895 : lowlim = 0;
610 8631895 : symbol = 0;
611 8631895 : move16();
612 8631895 : move32();
613 8631895 : move32();
614 8631895 : move16();
615 8631895 : move16();
616 :
617 8631895 : range = (UWord32) W_sub( high, low );
618 8631895 : move32();
619 :
620 : /* the value read from bitstream */
621 8631895 : assert( value >= (UWord32) low );
622 8631895 : cum = (UWord32) W_add( W_shl( ( W_sub( value, low ) ), stat_bitsnew ), ( 1 << stat_bitsnew ) - 1 );
623 8631895 : move32();
624 :
625 : /* search for the interval where "cum" fits */
626 8631895 : IF( GT_64( W_mult0_32_32( L_shr( base, 1 ), range ), cum ) ) /* below pow-1 */
627 : {
628 412806 : pows[0] = testval = base;
629 412806 : move16();
630 412806 : move16();
631 : /* increase exponent until it is smaller than "cum" */
632 657942 : FOR( k = 1; k < 12; k++ )
633 : {
634 657942 : highlim = testval;
635 657942 : move16();
636 657942 : pows[k] = mult_r( pows[k - 1], pows[k - 1] );
637 657942 : move16();
638 657942 : testval = mult_r( pows[k], base );
639 657942 : IF( LE_64( W_mult0_32_32( shr( testval, 1 ), range ), cum ) ) /* found! big range is [lowlim,testval], (now narrow it down) */
640 : {
641 412806 : lowlim = testval;
642 412806 : move16();
643 412806 : k = sub( k, 1 );
644 412806 : symbol = (UWord16) L_shl( 1, k );
645 412806 : BREAK;
646 : }
647 : }
648 412806 : assert( k < 12 ); /* maximum 2^10-1*/
649 : /* narrow the range down */
650 523128 : FOR( k--; k > 0; k-- )
651 : {
652 110322 : testval = mult_r( highlim, pows[k] );
653 110322 : IF( LE_64( W_mult0_32_32( shr( testval, 1 ), range ), cum ) )
654 : {
655 71308 : lowlim = testval;
656 71308 : move16();
657 71308 : symbol = (UWord16) L_sub( symbol, L_shl( 1, sub( k, 1 ) ) );
658 : }
659 : ELSE
660 : {
661 39014 : highlim = testval;
662 39014 : move16();
663 : }
664 : }
665 412806 : highlim = shr( highlim, 1 );
666 412806 : lowlim = shr( lowlim, 1 );
667 : }
668 : ELSE /* trivial case, above pow-1, that is, first symbol */
669 : {
670 8219089 : symbol = 0;
671 8219089 : lowlim = extract_l( L_shr( base, 1 ) );
672 8219089 : highlim = 16384;
673 8219089 : move16();
674 8219089 : move16();
675 : }
676 :
677 :
678 8631895 : high = L_add( low, mul_sbc_14bits( range, highlim ) );
679 :
680 8631895 : low = L_add( low, mul_sbc_14bits( range, lowlim ) );
681 :
682 : /*ptr init for ptr*/
683 10243468 : FOR( ; bp < bits; )
684 : {
685 4030165 : IF( GT_32( high, ari_q2new ) )
686 : {
687 3499100 : IF( GE_32( low, ari_q2new ) )
688 : {
689 579352 : value = (UWord32) W_sub( value, ari_q2new );
690 579352 : low = L_sub( low, ari_q2new );
691 579352 : high = L_sub( high, ari_q2new );
692 : }
693 : ELSE
694 : {
695 2919748 : test();
696 2919748 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
697 : {
698 501156 : value = (UWord32) W_sub( value, ari_q1new );
699 501156 : low = L_sub( low, ari_q1new );
700 501156 : high = L_sub( high, ari_q1new );
701 : }
702 : ELSE
703 : {
704 : BREAK;
705 : }
706 : }
707 : }
708 1611573 : low = L_add( low, low );
709 1611573 : high = L_add( high, high );
710 :
711 1611573 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
712 :
713 1611573 : value = (UWord32) ( W_shl( value, 1 ) | ptr[bp++] );
714 : }
715 :
716 8631895 : test();
717 8631895 : test();
718 8631895 : test();
719 8631895 : 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 8631895 : s->low = low;
731 8631895 : s->high = L_sub( high, 1 );
732 8631895 : s->value = value;
733 8631895 : move32();
734 8631895 : move32();
735 8631895 : move32();
736 :
737 8631895 : *res = symbol;
738 8631895 : move16();
739 8631895 : return bp;
740 : }
741 :
742 412806 : 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 412806 : low = s->low;
754 412806 : high = L_add( s->high, 1 );
755 412806 : value = s->value;
756 412806 : move32();
757 412806 : move32();
758 :
759 412806 : range = (UWord32) W_sub( high, low );
760 :
761 412806 : IF( LT_16( bp, bits ) )
762 : {
763 385216 : assert( value >= (UWord32) low );
764 385216 : cum = (UWord32) W_add( W_shl( ( W_sub( value, low ) ), stat_bitsnew ), ( 1 << stat_bitsnew ) - 1 );
765 385216 : IF( GT_64( W_shl( range, 13 ), cum ) )
766 : {
767 192963 : symbol = 2;
768 192963 : move16();
769 192963 : high = L_add( low, W_extract_l( W_shr( range, 1 ) ) );
770 : }
771 : ELSE
772 : {
773 192253 : symbol = 1;
774 192253 : move16();
775 192253 : low = L_add( low, W_extract_l( W_shr( range, 1 ) ) );
776 : }
777 :
778 : /*ptr init for ptr*/
779 771394 : FOR( ; bp < bits; )
780 : {
781 769359 : IF( GT_32( high, ari_q2new ) )
782 : {
783 672196 : IF( GE_32( low, ari_q2new ) )
784 : {
785 95393 : value = (UWord32) W_sub( value, ari_q2new );
786 95393 : low = L_sub( low, ari_q2new );
787 95393 : high = L_sub( high, ari_q2new );
788 : }
789 : ELSE
790 : {
791 576803 : test();
792 576803 : IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
793 : {
794 193622 : value = (UWord32) W_sub( value, ari_q1new );
795 193622 : low = L_sub( low, ari_q1new );
796 193622 : high = L_sub( high, ari_q1new );
797 : }
798 : ELSE
799 : {
800 : BREAK;
801 : }
802 : }
803 : }
804 386178 : low = L_add( low, low );
805 386178 : high = L_add( high, high );
806 :
807 386178 : assert( abs( ptr[bp] ) <= 1 && "AC expects reading binary values!!!" );
808 :
809 386178 : value = (UWord32) ( W_shl( value, 1 ) | ptr[bp++] );
810 : }
811 : }
812 : ELSE
813 : {
814 27590 : cum = (UWord32) W_sub( value, low );
815 27590 : range = (UWord32) W_shr( range, 1 );
816 27590 : IF( GT_64( range, cum ) )
817 : {
818 13682 : symbol = 2;
819 13682 : move16();
820 13682 : high = L_add( low, range );
821 : }
822 : ELSE
823 : {
824 13908 : symbol = 1;
825 13908 : move16();
826 13908 : low = L_add( low, range );
827 : }
828 : }
829 :
830 412806 : s->low = low;
831 412806 : s->high = L_sub( high, 1 );
832 412806 : s->value = value;
833 412806 : move32();
834 412806 : move32();
835 412806 : move32();
836 :
837 412806 : *res = symbol;
838 412806 : move16();
839 :
840 412806 : return bp;
841 : }
|