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