Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #include <math.h>
40 : #include "prot_fx.h"
41 : #include "wmc_auto.h"
42 :
43 : /*------------------------------------------------------------------*
44 : * own_random()
45 : *
46 : * Random generator
47 : *------------------------------------------------------------------*/
48 :
49 : /*! r: output random value */
50 295028495 : int16_t own_random(
51 : int16_t *seed /* i/o: random seed */
52 : )
53 : {
54 295028495 : *seed = (int16_t) ( *seed * 31821L + 13849L );
55 :
56 295028495 : return ( *seed );
57 : }
58 :
59 : /*---------------------------------------------------------------------
60 : * sign()
61 : *
62 : *---------------------------------------------------------------------*/
63 :
64 : /*! r: sign of x (+1/-1) */
65 0 : float sign(
66 : const float x /* i : input value of x */
67 : )
68 : {
69 0 : if ( x < 0.0f )
70 : {
71 0 : return -1.0f;
72 : }
73 : else
74 : {
75 0 : return 1.0f;
76 : }
77 : }
78 :
79 : /*---------------------------------------------------------------------
80 : * log2_f()
81 : *
82 : *---------------------------------------------------------------------*/
83 :
84 : /*! r: logarithm2 of x */
85 0 : float log2_f(
86 : const float x /* i : input value of x */
87 : )
88 : {
89 0 : return (float) ( log( x ) / log( 2.0f ) );
90 : }
91 :
92 0 : int16_t norm_ul_float( uint32_t UL_var1 )
93 : {
94 : int16_t var_out;
95 :
96 0 : if ( UL_var1 == 0 )
97 : {
98 0 : var_out = 0;
99 : }
100 : else
101 : {
102 0 : for ( var_out = 0; UL_var1 < (uint32_t) 0x80000000U; var_out++ )
103 : {
104 0 : UL_var1 <<= 1;
105 : }
106 : }
107 : BASOP_CHECK();
108 :
109 0 : return ( var_out );
110 : }
111 :
112 :
113 : /*---------------------------------------------------------------------
114 : * sum_s()
115 : * sum_l()
116 : * sum_f()
117 : *
118 : *---------------------------------------------------------------------*/
119 :
120 : /*! r: sum of all vector elements */
121 352873 : int16_t sum_s(
122 : const int16_t *vec, /* i : input vector */
123 : const int16_t lvec /* i : length of input vector */
124 : )
125 : {
126 : int16_t i;
127 : int16_t tmp;
128 :
129 352873 : tmp = 0;
130 8287115 : for ( i = 0; i < lvec; i++ )
131 : {
132 7934242 : tmp += vec[i];
133 : }
134 :
135 352873 : return tmp;
136 : }
137 :
138 : /*! r: sum of all vector elements */
139 6702 : Word32 sum_l_fx(
140 : const Word32 *vec, /* i : input vector */
141 : const Word16 lvec /* i : length of input vector */
142 : )
143 : {
144 : Word16 i;
145 : Word32 tmpL;
146 :
147 6702 : tmpL = 0;
148 6702 : move32();
149 80424 : FOR( i = 0; i < lvec; i++ )
150 : {
151 73722 : tmpL = L_add( tmpL, vec[i] );
152 : }
153 :
154 6702 : return tmpL;
155 : }
156 :
157 : /*----------------------------------------------------------------------
158 : * sum2_f()
159 : *
160 : *---------------------------------------------------------------------*/
161 :
162 : /*! r: sum of all squared vector elements */
163 828 : Word32 sum2_f_16_fx(
164 : const Word16 *vec, /* i : input vector */
165 : const Word16 lvec /* i : length of input vector */
166 : )
167 : {
168 : Word16 i;
169 : Word32 tmp;
170 :
171 828 : tmp = 0;
172 828 : move32();
173 14076 : FOR( i = 0; i < lvec; i++ )
174 : {
175 13248 : tmp = L_add( tmp, L_mult0( vec[i], vec[i] ) );
176 : }
177 :
178 828 : return tmp;
179 : }
180 148 : Word32 sum2_f_16_gb_fx(
181 : const Word16 *vec, /* i : input vector */
182 : const Word16 lvec, /* i : length of input vector */
183 : Word16 gb )
184 : {
185 : int16_t i;
186 : Word32 tmp;
187 :
188 148 : tmp = 0;
189 96148 : FOR( i = 0; i < lvec; i++ )
190 : {
191 96000 : tmp = L_add( tmp, L_shr( L_mult0( vec[i], vec[i] ), gb ) );
192 : }
193 :
194 148 : return tmp;
195 : }
196 :
197 35545 : Word32 sum2_16_exp_fx(
198 : const Word16 *vec, /* i : input vector Q(15 - exp) */
199 : const Word16 lvec, /* i : length of input vector */
200 : Word16 *exp, /* i/o: exponent of vector */
201 : Word16 gb /* i : guard bits */
202 : )
203 : {
204 : Word16 i, s;
205 : Word32 L_tmp, var_a;
206 :
207 35545 : L_tmp = 0;
208 35545 : move32();
209 35545 : var_a = 0;
210 35545 : move32();
211 6546413 : FOR( i = 0; i < lvec; i++ )
212 : {
213 6510868 : var_a = L_mult0( vec[i], vec[i] ); /* 2 * Q(15 - exp) */
214 6510868 : L_tmp = L_add( L_tmp, L_shr( var_a, gb ) ); /* 2 * Q(15 - exp) - gb */
215 : }
216 :
217 35545 : s = norm_l( L_tmp );
218 35545 : L_tmp = L_shl( L_tmp, s ); /* 2 * Q(15 - exp) - gb + s */
219 :
220 35545 : *exp = add( sub( add( shl( *exp, 1 ), gb ), s ), 1 );
221 35545 : move16();
222 :
223 35545 : return L_tmp;
224 : }
225 :
226 8051169 : Word32 sum2_32_exp_fx(
227 : const Word32 *vec, /* i : input vector, Qx */
228 : const Word16 lvec, /* i : length of input vector */
229 : Word16 *exp, /* i/o: exponent of vector */
230 : Word16 gb /* i : guard bits */
231 : )
232 : {
233 : Word16 i, s, norm;
234 : Word64 W_tmp;
235 :
236 8051169 : W_tmp = 0;
237 8051169 : Word64 var_a = 0;
238 8051169 : move64();
239 8051169 : move64();
240 :
241 8051169 : norm = L_norm_arr( vec, lvec );
242 :
243 8051169 : gb = sub( gb, norm );
244 :
245 1674267131 : FOR( i = 0; i < lvec; i++ )
246 : {
247 1666215962 : var_a = W_mult0_32_32( vec[i], vec[i] ); // 2x
248 1666215962 : W_tmp = W_add( W_tmp, W_shr( var_a, gb ) ); // 2x-gb
249 : }
250 :
251 8051169 : s = W_norm( W_tmp );
252 8051169 : W_tmp = W_shl( W_tmp, s ); // 2x - gb + s
253 :
254 : //*exp = 31 - (2*(31 - *exp) - gb + s) + 32;
255 8051169 : *exp = add( sub( add( shl( *exp, 1 ), gb ), s ), 1 );
256 8051169 : move16();
257 :
258 8051169 : return W_extract_h( W_tmp );
259 : }
260 :
261 10274930 : Word32 sum2_f_32_fx( /* o : Q(2x - 31 - gb) */
262 : const Word32 *vec, /* i : input vector, Qx */
263 : const Word16 lvec, /* i : length of input vector */
264 : Word16 gb /* i : guard bits */
265 : )
266 : {
267 : Word16 i;
268 : Word32 tmp;
269 :
270 10274930 : tmp = 0;
271 10274930 : Word32 var_a = 0;
272 10274930 : move32();
273 10274930 : move32();
274 108120630 : FOR( i = 0; i < lvec; i++ )
275 : {
276 97845700 : var_a = Mpy_32_32( vec[i], vec[i] ); // 2x-31
277 97845700 : tmp = L_add( tmp, L_shr( var_a, gb ) ); // 2x-31-gb
278 : }
279 :
280 10274930 : return tmp;
281 : }
282 :
283 1925084 : Word32 sum2_32_fx(
284 : const Word32 *vec, /* i : input vector */
285 : const Word16 lvec, /* i : length of input vector */
286 : Word16 *e )
287 : {
288 : Word16 i;
289 : Word32 tmp;
290 :
291 1925084 : tmp = 0;
292 1925084 : Word32 var_a = 0;
293 1925084 : Word16 exp = 0, exp_tmp;
294 1925084 : move32();
295 1925084 : move32();
296 1925084 : move16();
297 144765630 : FOR( i = 0; i < lvec; i++ )
298 : {
299 142840546 : exp_tmp = norm_l( vec[i] );
300 142840546 : var_a = L_shl( vec[i], exp_tmp );
301 142840546 : var_a = Mpy_32_32( var_a, var_a );
302 142840546 : exp_tmp = shl( sub( *e, exp_tmp ), 1 );
303 142840546 : tmp = BASOP_Util_Add_Mant32Exp( tmp, exp, var_a, exp_tmp, &exp );
304 : }
305 1925084 : *e = exp;
306 1925084 : move16();
307 :
308 1925084 : return tmp;
309 : }
310 :
311 : /*-------------------------------------------------------------------*
312 : * set_c()
313 : * set_s()
314 : * set_f()
315 : * set_l()
316 : * set_d()
317 : *
318 : * Set the vector elements to a value
319 : *-------------------------------------------------------------------*/
320 :
321 116820 : void set_c(
322 : int8_t y[], /* i/o: Vector to set */
323 : const int8_t a, /* i : Value to set the vector to */
324 : const int32_t N /* i : Length of the vector */
325 : )
326 : {
327 : int16_t i;
328 :
329 692449 : for ( i = 0; i < N; i++ )
330 : {
331 575629 : y[i] = a;
332 : }
333 :
334 116820 : return;
335 : }
336 :
337 :
338 2515054 : void set_s(
339 : int16_t y[], /* i/o: Vector to set */
340 : const int16_t a, /* i : Value to set the vector to */
341 : const int16_t N /* i : Length of the vector */
342 : )
343 : {
344 : int16_t i;
345 :
346 29655734 : for ( i = 0; i < N; i++ )
347 : {
348 27140680 : y[i] = a;
349 : }
350 :
351 2515054 : return;
352 : }
353 :
354 :
355 0 : void set_l(
356 : int32_t y[], /* i/o: Vector to set */
357 : const int32_t a, /* i : Value to set the vector to */
358 : const int16_t N /* i : Length of the vector */
359 : )
360 : {
361 : int16_t i;
362 :
363 0 : for ( i = 0; i < N; i++ )
364 : {
365 0 : y[i] = a;
366 : }
367 :
368 0 : return;
369 : }
370 :
371 0 : void set_f(
372 : float y[], /* i/o: Vector to set */
373 : const float a, /* i : Value to set the vector to */
374 : const int16_t N /* i : Lenght of the vector */
375 : )
376 : {
377 : int16_t i;
378 :
379 0 : for ( i = 0; i < N; i++ )
380 : {
381 0 : y[i] = a;
382 : }
383 :
384 0 : return;
385 : }
386 :
387 : /*---------------------------------------------------------------------*
388 : * set_zero()
389 : *
390 : * Set a vector vec[] of dimension lvec to zero
391 : *---------------------------------------------------------------------*/
392 :
393 588 : void set_zero(
394 : float *vec, /* o : input vector */
395 : const int16_t lvec /* i : length of the vector */
396 : )
397 : {
398 : int16_t i;
399 :
400 9996 : for ( i = 0; i < lvec; i++ )
401 : {
402 9408 : *vec++ = 0.0f;
403 : }
404 :
405 588 : return;
406 : }
407 :
408 :
409 : /*---------------------------------------------------------------------*
410 : * mvr2r()
411 : * mvs2s()
412 : * mvr2s()
413 : * mvs2r()
414 : * mvr2d()
415 : * mvd2r()
416 : *
417 : * Transfer the contents of vector x[] to vector y[]
418 : *---------------------------------------------------------------------*/
419 :
420 306 : void mvr2r(
421 : const float x[], /* i : input vector */
422 : float y[], /* o : output vector */
423 : const int16_t n /* i : vector size */
424 : )
425 : {
426 : int16_t i;
427 :
428 306 : if ( n <= 0 )
429 : {
430 : /* cannot transfer vectors with size 0 */
431 0 : return;
432 : }
433 :
434 306 : if ( y < x )
435 : {
436 0 : for ( i = 0; i < n; i++ )
437 : {
438 0 : y[i] = x[i];
439 : }
440 : }
441 : else
442 : {
443 5046 : for ( i = n - 1; i >= 0; i-- )
444 : {
445 4740 : y[i] = x[i];
446 : }
447 : }
448 :
449 306 : return;
450 : }
451 :
452 1129277 : void mvs2s(
453 : const int16_t x[], /* i : input vector */
454 : int16_t y[], /* o : output vector */
455 : const int16_t n /* i : vector size */
456 : )
457 : {
458 : int16_t i;
459 :
460 1129277 : if ( n <= 0 )
461 : {
462 : /* cannot transfer vectors with size 0 */
463 0 : return;
464 : }
465 :
466 1129277 : if ( y < x )
467 : {
468 0 : for ( i = 0; i < n; i++ )
469 : {
470 0 : y[i] = x[i];
471 : }
472 : }
473 : else
474 : {
475 7904662 : for ( i = n - 1; i >= 0; i-- )
476 : {
477 6775385 : y[i] = x[i];
478 : }
479 : }
480 :
481 1129277 : return;
482 : }
483 :
484 0 : void mvl2l(
485 : const int32_t x[], /* i : input vector */
486 : int32_t y[], /* o : output vector */
487 : const int16_t n /* i : vector size */
488 : )
489 : {
490 : int16_t i;
491 :
492 0 : if ( n <= 0 )
493 : {
494 : /* no need to transfer vectors with size 0 */
495 0 : return;
496 : }
497 :
498 0 : if ( y < x )
499 : {
500 0 : for ( i = 0; i < n; i++ )
501 : {
502 0 : y[i] = x[i];
503 : }
504 : }
505 : else
506 : {
507 0 : for ( i = n - 1; i >= 0; i-- )
508 : {
509 0 : y[i] = x[i];
510 : }
511 : }
512 :
513 0 : return;
514 : }
515 :
516 : /*! r: index of the maximum value in the input vector */
517 614866 : Word16 maximum_s(
518 : const Word16 *vec, /* i : input vector */
519 : const Word16 lvec, /* i : length of input vector */
520 : Word16 *max /* o : maximum value in the input vector */
521 : )
522 : {
523 : Word16 i, ind;
524 : Word16 tmp;
525 :
526 614866 : ind = 0;
527 614866 : move16();
528 614866 : tmp = vec[0];
529 614866 : move16();
530 :
531 6361774 : FOR( i = 1; i < lvec; i++ )
532 : {
533 5746908 : IF( GT_16( vec[i], tmp ) )
534 : {
535 197207 : ind = i;
536 197207 : move16();
537 197207 : tmp = vec[i];
538 197207 : move16();
539 : }
540 : }
541 :
542 614866 : if ( max != NULL )
543 : {
544 608778 : *max = tmp;
545 608778 : move16();
546 : }
547 :
548 614866 : return ind;
549 : }
550 :
551 : /*! r: index of the maximum value in the input vector */
552 253895 : Word16 maximum_l(
553 : const Word32 *vec, /* i : input vector */
554 : const Word16 lvec, /* i : length of input vector */
555 : Word32 *max_val /* o : maximum value in the input vector */
556 : )
557 : {
558 : Word16 i, ind;
559 : Word32 tmp;
560 :
561 253895 : ind = 0;
562 253895 : tmp = vec[0];
563 253895 : move16();
564 253895 : move32();
565 68240329 : FOR( i = 1; i < lvec; i++ )
566 : {
567 67986434 : IF( GT_32( vec[i], tmp ) )
568 : {
569 1336629 : ind = i;
570 1336629 : tmp = vec[i];
571 1336629 : move16();
572 1336629 : move32();
573 : }
574 : }
575 :
576 253895 : if ( max_val != NULL )
577 : {
578 253895 : *max_val = tmp;
579 253895 : move32();
580 : }
581 :
582 253895 : return ind;
583 : }
584 :
585 : /*! r: index of the maximum value in the input vector */
586 1613296 : Word16 maximumAbs_l(
587 : const Word32 *vec, /* i : input vector */
588 : const Word16 lvec, /* i : length of input vector */
589 : Word32 *max_val /* o : maximum value in the input vector */
590 : )
591 : {
592 : Word16 j, ind;
593 : Word32 tmp;
594 :
595 1613296 : ind = 0;
596 1613296 : move16();
597 1613296 : tmp = L_abs( vec[0] );
598 :
599 59469001 : FOR( j = 1; j < lvec; j++ )
600 : {
601 57855705 : IF( GT_32( L_abs( vec[j] ), tmp ) )
602 : {
603 5476649 : ind = j;
604 5476649 : move16();
605 5476649 : tmp = L_abs( vec[j] );
606 : }
607 : }
608 :
609 1613296 : IF( max_val != NULL )
610 : {
611 1613296 : *max_val = tmp;
612 1613296 : move32();
613 : }
614 :
615 1613296 : return ind;
616 : }
617 :
618 : /*-------------------------------------------------------------------*
619 : * minimum_s()
620 : *
621 : * Finds minimum 16-bit signed integer value in the array and returns it.
622 : *-------------------------------------------------------------------*/
623 :
624 : /*! r: index of the minimum value in the input vector */
625 85300655 : Word16 minimum_s(
626 : const Word16 *vec, /* i : Input vector */
627 : const Word16 lvec, /* i : Vector length */
628 : Word16 *min_val /* o : minimum value in the input vector */
629 : )
630 : {
631 : Word16 i, ind;
632 :
633 85300655 : ind = 0;
634 85300655 : move16();
635 :
636 399753285 : FOR( i = 1; i < lvec; i++ )
637 : {
638 314452630 : if ( LT_16( vec[i], vec[ind] ) )
639 : {
640 29519288 : ind = i;
641 29519288 : move16();
642 : }
643 : }
644 :
645 85300655 : if ( min_val != NULL )
646 : {
647 85300655 : *min_val = vec[ind];
648 85300655 : move16();
649 : }
650 :
651 85300655 : return ind;
652 : }
653 :
654 : /*-------------------------------------------------------------------*
655 : * minimum_l()
656 : *
657 : * Finds minimum 16-bit signed integer value in the array and returns it.
658 : *-------------------------------------------------------------------*/
659 :
660 : /*! r: index of the minimum value in the input vector */
661 156972 : Word16 minimum_l(
662 : const Word32 *vec, /* i : Input vector */
663 : const Word16 lvec, /* i : Vector length */
664 : Word32 *min_val /* o : minimum value in the input vector */
665 : )
666 : {
667 : Word16 i, ind;
668 :
669 156972 : ind = 0;
670 156972 : move16();
671 :
672 1982504 : FOR( i = 1; i < lvec; i++ )
673 : {
674 1825532 : if ( LT_32( vec[i], vec[ind] ) )
675 : {
676 338138 : ind = i;
677 338138 : move16();
678 : }
679 : }
680 :
681 156972 : if ( min_val != NULL )
682 : {
683 156972 : *min_val = vec[ind];
684 156972 : move32();
685 : }
686 :
687 156972 : return ind;
688 : }
689 :
690 : /*---------------------------------------------------------------------*
691 : * dotp()
692 : *
693 : * Dot product of vector x[] and vector y[]
694 : *---------------------------------------------------------------------*/
695 :
696 : /*! r: dot product of x[] and y[] */
697 0 : float dotp(
698 : const float x[], /* i : vector x[] */
699 : const float y[], /* i : vector y[] */
700 : const int16_t n /* i : vector length */
701 : )
702 : {
703 : int16_t i;
704 : float suma;
705 :
706 0 : suma = x[0] * y[0];
707 :
708 0 : for ( i = 1; i < n; i++ )
709 : {
710 0 : suma += x[i] * y[i];
711 : }
712 :
713 0 : return suma;
714 : }
715 :
716 : /*---------------------------------------------------------------------*
717 : * dotp()
718 : *
719 : * Dot product of vector x[] and vector y[]
720 : *---------------------------------------------------------------------*/
721 :
722 : /*! r: dot product of x[] and y[] */
723 13284021 : Word32 dotp_fixed(
724 : const Word32 x[], /* i : vector x[] Qx */
725 : const Word32 y[], /* i : vector y[] Qy */
726 : const Word16 n /* i : vector length */
727 : )
728 : {
729 : Word16 i;
730 : Word32 suma;
731 :
732 13284021 : suma = Mpy_32_32( x[0], y[0] );
733 :
734 35202883 : FOR( i = 1; i < n; i++ )
735 : {
736 21918862 : suma = L_add( suma, Mpy_32_32( x[i], y[i] ) );
737 : }
738 :
739 13284021 : return suma;
740 : }
741 :
742 : /*To calculate dot product of two 32 bit arrays in case of overflow*/
743 2888890 : Word32 dotp_fixed_o(
744 : const Word32 x[], /* i : vector x[] */
745 : const Word32 y[], /* i : vector y[] */
746 : const Word16 n, /* i : vector length */
747 : const Word16 log_len, /* i : max factor added to result q after dot product (equal to log2(n)) */
748 : Word16 *res_q /*stores resultant Q*/
749 : )
750 : {
751 : Word16 i;
752 : Word64 suma; /*resultant q= q(x)+q(y)-9-x such that q<=31*/
753 :
754 2888890 : suma = W_shr( W_mult_32_32( x[0], y[0] ), log_len );
755 :
756 149598044 : FOR( i = 1; i < n; i++ )
757 : {
758 146709154 : suma = W_add( suma, W_shr( W_mult_32_32( x[i], y[i] ), log_len ) );
759 : }
760 2888890 : *res_q = add( sub( *res_q, log_len ), 1 );
761 2888890 : move16();
762 2888890 : test();
763 2888890 : test();
764 35143804 : WHILE( GT_64( suma, MAX_32 ) || LT_64( suma, MIN_32 ) || GT_16( *res_q, 31 ) )
765 : {
766 32254914 : suma = W_shr( suma, 1 );
767 32254914 : *res_q = sub( *res_q, 1 );
768 32254914 : move16();
769 : }
770 2888890 : return W_extract_l( suma );
771 : }
772 :
773 1378489 : Word32 dotp_fixed_32(
774 : const Word32 x[], /* i : vector x[] */
775 : const Word32 y[], /* i : vector y[] */
776 : const Word16 n, /* i : vector length */
777 : const Word16 log_len, /* i : max factor added to result q after dot product (equal to log2(n)) */
778 : Word16 *res_q /*stores resultant Q*/
779 : )
780 : {
781 : Word16 i;
782 : Word64 suma; /*resultant q= q(x)+q(y)-9-x such that q<=31*/
783 :
784 1378489 : suma = W_shr( W_mult_32_32( x[0], y[0] ), log_len );
785 :
786 1191221760 : FOR( i = 1; i < n; i++ )
787 : {
788 1189843271 : suma = W_add( suma, W_shr( W_mult_32_32( x[i], y[i] ), log_len ) );
789 : }
790 1378489 : *res_q = add( *res_q, add( sub( *res_q, log_len ), 1 ) );
791 1378489 : move16();
792 1378489 : test();
793 1378489 : test();
794 28906331 : WHILE( GT_64( suma, MAX_32 ) || LT_64( suma, MIN_32 ) || GT_16( *res_q, 31 ) )
795 : {
796 27527842 : suma = W_shr( suma, 1 );
797 27527842 : *res_q = sub( *res_q, 1 );
798 27527842 : move16();
799 : }
800 1378489 : return W_extract_l( suma );
801 : }
802 :
803 : /*---------------------------------------------------------------------*
804 : * inv_sqrt()
805 : *
806 : * Find the inverse square root of the input value
807 : *---------------------------------------------------------------------*/
808 :
809 : /*! r: inverse square root of input value */
810 0 : float inv_sqrt(
811 : const float x /* i : input value */
812 : )
813 : {
814 0 : return (float) ( 1.0 / sqrt( x ) );
815 : }
816 :
817 : /*-------------------------------------------------------------------*
818 : * v_add_w64()
819 : *
820 : * Subtraction of two vectors sample by sample
821 : *-------------------------------------------------------------------*/
822 :
823 0 : void v_add_w64(
824 : const Word64 x1[], /* i : Input vector 1 */
825 : const Word64 x2[], /* i : Input vector 2 */
826 : Word64 y[], /* o : Output vector that contains vector 1 - vector 2 */
827 : const Word16 N, /* i : Vector length */
828 : const Word16 hdrm /* i : headroom for when subtraction result > 1 or < -1 */
829 : )
830 : {
831 : Word16 i;
832 :
833 0 : FOR( i = 0; i < N; i++ )
834 : {
835 0 : y[i] = W_add( W_shr( x1[i], hdrm ), W_shr( x2[i], hdrm ) );
836 0 : move64();
837 : }
838 :
839 0 : return;
840 : }
841 :
842 :
843 : /*-------------------------------------------------------------------*
844 : * v_sub()
845 : *
846 : * Subtraction of two vectors sample by sample
847 : *-------------------------------------------------------------------*/
848 :
849 0 : void v_sub(
850 : const float x1[], /* i : Input vector 1 */
851 : const float x2[], /* i : Input vector 2 */
852 : float y[], /* o : Output vector that contains vector 1 - vector 2 */
853 : const int16_t N /* i : Vector length */
854 : )
855 : {
856 : int16_t i;
857 :
858 0 : for ( i = 0; i < N; i++ )
859 : {
860 0 : y[i] = x1[i] - x2[i];
861 : }
862 :
863 0 : return;
864 : }
865 :
866 : /*-------------------------------------------------------------------*
867 : * v_sub()
868 : *
869 : * Subtraction of two vectors sample by sample
870 : *-------------------------------------------------------------------*/
871 :
872 6287711 : void v_sub_fixed(
873 : const Word32 x1[], /* i : Input vector 1 */
874 : const Word32 x2[], /* i : Input vector 2 */
875 : Word32 y[], /* o : Output vector that contains vector 1 - vector 2 */
876 : const Word16 N, /* i : Vector length */
877 : const Word16 hdrm /* i : headroom for when subtraction result > 1 or < -1 */
878 : )
879 : {
880 : Word16 i;
881 :
882 25150844 : FOR( i = 0; i < N; i++ )
883 : {
884 18863133 : y[i] = L_sub( L_shr( x1[i], hdrm ), L_shr( x2[i], hdrm ) );
885 18863133 : move32();
886 : }
887 :
888 6287711 : return;
889 : }
890 :
891 : #ifdef VEC_ARITH_OPT_v1
892 200938875 : void v_sub_fixed_no_hdrm(
893 : const Word32 x1[], /* i : Input vector 1 */
894 : const Word32 x2[], /* i : Input vector 2 */
895 : Word32 y[], /* o : Output vector that contains vector 1 - vector 2 */
896 : const Word16 N /* i : Vector length */
897 : )
898 : {
899 : Word16 i;
900 :
901 917310594 : FOR( i = 0; i < N; i++ )
902 : {
903 716371719 : y[i] = L_sub( x1[i], x2[i] );
904 716371719 : move32();
905 : }
906 :
907 200938875 : return;
908 : }
909 : #endif /* VEC_ARITH_OPT_v1 */
910 :
911 : /*-------------------------------------------------------------------*
912 : * v_multc_fixed()
913 : *
914 : * Multiplication of vector by constant
915 : *-------------------------------------------------------------------*/
916 :
917 39465507 : void v_multc_fixed(
918 : const Word32 x[], /* i : Input vector */
919 : const Word32 c, /* i : Constant */
920 : Word32 y[], /* o : Output vector that contains c*x */
921 : const Word16 N /* i : Vector length */
922 : )
923 : {
924 : Word16 i;
925 :
926 6231942629 : FOR( i = 0; i < N; i++ )
927 : {
928 6192477122 : y[i] = Mpy_32_32( c, x[i] );
929 6192477122 : move32();
930 : }
931 :
932 39465507 : return;
933 : }
934 :
935 1802769 : void v_multc_fixed_16(
936 : const Word32 x[], /* i : Input vector */
937 : const Word16 c, /* i : Constant */
938 : Word32 y[], /* o : Output vector that contains c*x */
939 : const Word16 N /* i : Vector length */
940 : )
941 : {
942 : Word16 i;
943 :
944 198311911 : FOR( i = 0; i < N; i++ )
945 : {
946 196509142 : y[i] = Mpy_32_16_1( x[i], c );
947 196509142 : move32();
948 : }
949 :
950 1802769 : return;
951 : }
952 :
953 10430 : void v_multc_fixed_16_16(
954 : const Word16 x[], /* i : Input vector */
955 : const Word16 c, /* i : Constant */
956 : Word16 y[], /* o : Output vector that contains c*x */
957 : const Word16 N /* i : Vector length */
958 : )
959 : {
960 : Word16 i;
961 :
962 4997614 : FOR( i = 0; i < N; i++ )
963 : {
964 4987184 : y[i] = mult_r( x[i], c );
965 4987184 : move16();
966 : }
967 :
968 10430 : return;
969 : }
970 :
971 : /*-------------------------------------------------------------------*
972 : * usdequant()
973 : *
974 : * Uniform scalar de-quantizer routine
975 : *
976 : * Applies de-quantization based on scale and round operations.
977 : *-------------------------------------------------------------------*/
978 :
979 3 : float usdequant(
980 : const int16_t idx, /* i : quantizer index */
981 : const float qlow, /* i : lowest codebook entry (index 0) */
982 : const float delta /* i : quantization step */
983 : )
984 : {
985 : float g;
986 :
987 3 : g = idx * delta + qlow;
988 :
989 3 : return ( g );
990 : }
991 :
992 835 : void sort(
993 : UWord16 *x, /* i/o: Vector to be sorted */
994 : UWord16 len /* i/o: vector length */
995 : )
996 : {
997 : Word16 i;
998 : UWord16 j, tempr;
999 :
1000 4821 : FOR( i = len - 2; i >= 0; i-- )
1001 : {
1002 3986 : tempr = x[i];
1003 3986 : move16();
1004 8619 : FOR( j = i + 1; ( j < len ) && ( tempr > x[j] ); j++ )
1005 : {
1006 4633 : test();
1007 4633 : x[j - 1] = x[j];
1008 4633 : move16();
1009 : }
1010 3986 : x[j - 1] = tempr;
1011 3986 : move16();
1012 : }
1013 :
1014 835 : return;
1015 : }
|