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