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 : ===========================================================================
35 : File: ENH1632.C v.2.3 - 30.Nov.2009
36 : ===========================================================================
37 :
38 : ITU-T STL BASIC OPERATORS
39 :
40 : ENHANCED 16-BIT & 32-BIT ARITHMETIC OPERATORS
41 :
42 : History:
43 : 07 Nov 04 v2.0 Incorporation of new 32-bit / 40-bit / control
44 : operators for the ITU-T Standard Tool Library as
45 : described in Geneva, 20-30 January 2004 WP 3/16 Q10/16
46 : TD 11 document and subsequent discussions on the
47 : wp3audio@yahoogroups.com email reflector.
48 :
49 : ============================================================================
50 : */
51 :
52 :
53 : /*****************************************************************************
54 : *
55 : * Enhanced 16/32 bit operators :
56 : * s_max()
57 : * s_min()
58 : * L_max()
59 : * L_min()
60 : * shl_r()
61 : * L_shl_r()
62 : * L_mac0()
63 : * L_mult0()
64 : * L_msu0()
65 : * s_and()
66 : * s_or()
67 : * s_xor()
68 : * L_and()
69 : * L_or()
70 : * lshl()
71 : * lshr()
72 : * L_lshl()
73 : * L_lshr()
74 : * rotr()
75 : * rotl()
76 : * L_rotr()
77 : * L_rotl()
78 : *
79 : *****************************************************************************/
80 :
81 :
82 : /*****************************************************************************
83 : *
84 : * Include-Files
85 : *
86 : *****************************************************************************/
87 :
88 : #include <stdio.h>
89 : #include <stdlib.h>
90 : #include "stl.h"
91 :
92 : #define WMC_TOOL_SKIP
93 :
94 : /*****************************************************************************
95 : *
96 : * Constants and Globals
97 : *
98 : *****************************************************************************/
99 :
100 :
101 : /*****************************************************************************
102 : *
103 : * Functions
104 : *
105 : *****************************************************************************/
106 :
107 :
108 : /*****************************************************************************
109 : *
110 : * Function Name : lshl
111 : *
112 : * Purpose :
113 : *
114 : * Logically shifts left var1 by var2 positions.
115 : * - If var2 is negative, var1 is shifted to the LSBits by (-var2)
116 : * positions with insertion of 0 at the MSBit.
117 : * - If var2 is positive, var1 is shifted to the MSBits by (var2)
118 : * positions.
119 : *
120 : * Complexity weight : 1
121 : *
122 : * Inputs :
123 : *
124 : * var1 16 bit short signed integer (Word16) whose value falls in
125 : * the range 0xffff 8000 <= var1 <= 0x0000 7fff.
126 : *
127 : * var2 16 bit short signed integer (Word16) whose value falls in
128 : * the range 0xffff 8000 <= var2 <= 0x0000 7fff.
129 : *
130 : * Outputs :
131 : *
132 : * none
133 : *
134 : * Return Value:
135 : *
136 : * var_out 16 bit short signed integer (Word16) whose value falls in
137 : * the range 0xffff 8000 <= var_out <= 0x0000 7fff.
138 : *
139 : *****************************************************************************/
140 0 : Word16 lshl( Word16 var1, Word16 var2 )
141 : {
142 0 : Word16 var_out = 0;
143 :
144 0 : if ( var2 < 0 )
145 : {
146 0 : var2 = -var2;
147 0 : var_out = lshr( var1, var2 );
148 : #ifdef WMOPS
149 : multiCounter[currCounter].lshr--;
150 : #endif
151 : }
152 : else
153 : {
154 0 : if ( var2 == 0 || var1 == 0 )
155 : {
156 0 : var_out = var1;
157 : }
158 0 : else if ( var2 >= 16 )
159 : {
160 0 : var_out = 0;
161 : }
162 : else
163 : {
164 0 : var_out = var1 << var2;
165 : }
166 : }
167 :
168 : #ifdef WMOPS
169 : multiCounter[currCounter].lshl++;
170 : #endif
171 :
172 : BASOP_CHECK();
173 :
174 :
175 0 : return ( var_out );
176 : }
177 :
178 : /*****************************************************************************
179 : *
180 : * Function Name : lshr
181 : *
182 : * Purpose :
183 : *
184 : * Logically shifts right var1 by var2 positions.
185 : * - If var2 is positive, var1 is shifted to the LSBits by (var2)
186 : * positions with insertion of 0 at the MSBit.
187 : * - If var2 is negative, var1 is shifted to the MSBits by (-var2)
188 : * positions.
189 : *
190 : * Complexity weight : 1
191 : *
192 : * Inputs :
193 : *
194 : * var1 16 bit short signed integer (Word16) whose value falls in
195 : * the range 0xffff 8000 <= var1 <= 0x0000 7fff.
196 : *
197 : * var2 16 bit short signed integer (Word16) whose value falls in
198 : * the range 0xffff 8000 <= var2 <= 0x0000 7fff.
199 : *
200 : * Outputs :
201 : *
202 : * none
203 : *
204 : * Return Value:
205 : *
206 : * var_out 16 bit short signed integer (Word16) whose value falls in
207 : * the range 0xffff 8000 <= var_out <= 0x0000 7fff.
208 : *
209 : *****************************************************************************/
210 3736059 : Word16 lshr( Word16 var1, Word16 var2 )
211 : {
212 : Word16 var_out;
213 :
214 3736059 : if ( var2 < 0 )
215 : {
216 0 : var2 = -var2;
217 0 : var_out = lshl( var1, var2 );
218 : #ifdef WMOPS
219 : multiCounter[currCounter].lshl--;
220 : #endif
221 : }
222 : else
223 : {
224 3736059 : if ( var2 == 0 || var1 == 0 )
225 : {
226 48650 : var_out = var1;
227 : }
228 3687409 : else if ( var2 >= 16 )
229 : {
230 0 : var_out = 0;
231 : }
232 : else
233 : {
234 3687409 : var_out = var1 >> 1;
235 3687409 : var_out = var_out & 0x7fff;
236 3687409 : var_out = var_out >> ( var2 - 1 );
237 : }
238 : }
239 :
240 : #ifdef WMOPS
241 : multiCounter[currCounter].lshr++;
242 : #endif
243 :
244 : BASOP_CHECK();
245 :
246 :
247 3736059 : return ( var_out );
248 : }
249 :
250 :
251 : /*****************************************************************************
252 : *
253 : * Function Name : L_lshl
254 : *
255 : * Purpose :
256 : *
257 : * Logically shifts left L_var1 by var2 positions.
258 : * - If var2 is negative, L_var1 is shifted to the LSBits by (-var2)
259 : * positions with insertion of 0 at the MSBit.
260 : * - If var2 is positive, L_var1 is shifted to the MSBits by (var2)
261 : * positions.
262 : *
263 : * Complexity weight : 1
264 : *
265 : * Inputs :
266 : *
267 : * L_var1 32 bit long signed integer (Word32) whose value falls in
268 : * the range 0x8000 0000 <= L_var1 <= 0x7fff ffff.
269 : *
270 : * var2 16 bit short signed integer (Word16) whose value falls in
271 : * the range 0xffff 8000 <= var2 <= 0x0000 7fff.
272 : *
273 : * Outputs :
274 : *
275 : * none
276 : *
277 : * Return Value:
278 : *
279 : * L_var_out 32 bit long signed integer (Word32) whose value falls in
280 : * the range 0x8000 0000 <= L_var_out <= 0x7fff ffff.
281 : *
282 : *****************************************************************************/
283 131374 : Word32 L_lshl( Word32 L_var1, Word16 var2 )
284 : {
285 131374 : Word32 L_var_out = 0;
286 :
287 131374 : if ( var2 < 0 )
288 : {
289 0 : var2 = -var2;
290 0 : L_var_out = L_lshr( L_var1, var2 );
291 : #ifdef WMOPS
292 : multiCounter[currCounter].L_lshr--;
293 : #endif
294 : }
295 : else
296 : {
297 131374 : if ( var2 == 0 || L_var1 == 0 )
298 : {
299 0 : L_var_out = L_var1;
300 : }
301 131374 : else if ( var2 >= 32 )
302 : {
303 0 : L_var_out = 0;
304 : }
305 : else
306 : {
307 131374 : L_var_out = L_var1 << var2;
308 : }
309 : }
310 :
311 : #ifdef WMOPS
312 : multiCounter[currCounter].L_lshl++;
313 : #endif
314 :
315 : BASOP_CHECK();
316 :
317 131374 : return ( L_var_out );
318 : }
319 :
320 :
321 : /*****************************************************************************
322 : *
323 : * Function Name : L_lshr
324 : *
325 : * Purpose :
326 : *
327 : * Logically shifts right L_var1 by var2 positions.
328 : * - If var2 is positive, L_var1 is shifted to the LSBits by (var2)
329 : * positions with insertion of 0 at the MSBit.
330 : * - If var2 is negative, L_var1 is shifted to the MSBits by (-var2)
331 : * positions.
332 : *
333 : * Complexity weight : 1
334 : *
335 : * Inputs :
336 : *
337 : * L_var1 32 bit long signed integer (Word32) whose value falls in
338 : * the range 0x8000 0000 <= L_var1 <= 0x7fff ffff.
339 : *
340 : * var2 16 bit short signed integer (Word16) whose value falls in
341 : * the range 0xffff 8000 <= var2 <= 0x0000 7fff.
342 : *
343 : * Outputs :
344 : *
345 : * none
346 : *
347 : * Return Value:
348 : *
349 : * L_var_out 32 bit long signed integer (Word32) whose value falls in
350 : * the range 0x8000 0000 <= L_var_out <= 0x7fff ffff.
351 : *
352 : *****************************************************************************/
353 0 : Word32 L_lshr( Word32 L_var1, Word16 var2 )
354 : {
355 : Word32 L_var_out;
356 :
357 0 : if ( var2 < 0 )
358 : {
359 0 : var2 = -var2;
360 0 : L_var_out = L_lshl( L_var1, var2 );
361 : #ifdef WMOPS
362 : multiCounter[currCounter].L_lshl--;
363 : #endif
364 : }
365 : else
366 : {
367 0 : if ( var2 == 0 || L_var1 == 0 )
368 : {
369 0 : L_var_out = L_var1;
370 : }
371 0 : else if ( var2 >= 32 )
372 : {
373 0 : L_var_out = 0;
374 : }
375 : else
376 : {
377 0 : L_var_out = L_var1 >> 1;
378 0 : L_var_out = L_var_out & 0x7fffffff;
379 0 : L_var_out = L_var_out >> ( var2 - 1 );
380 : }
381 : }
382 :
383 : #ifdef WMOPS
384 : multiCounter[currCounter].L_lshr++;
385 : #endif
386 :
387 : BASOP_CHECK();
388 :
389 :
390 0 : return ( L_var_out );
391 : }
392 :
393 :
394 : /*****************************************************************************
395 : *
396 : * Function Name : shl_r
397 : *
398 : * Purpose :
399 : *
400 : * Identical to shl( var1, var2) but with rounding. Saturates the result
401 : * in case of underflows or overflows.
402 : *
403 : * Complexity weight : 3
404 : *
405 : * Inputs :
406 : *
407 : * var1 16 bit short signed integer (Word16) whose value falls in
408 : * the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
409 : *
410 : * var2 16 bit short signed integer (Word16) whose value falls in
411 : * the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
412 : *
413 : * Outputs :
414 : *
415 : * none
416 : *
417 : * Return Value :
418 : *
419 : * var_out 16 bit short signed integer (Word16) whose value falls in
420 : * the range : 0xffff 8000 <= var_out <= 0x0000 7fff.
421 : *
422 : *****************************************************************************/
423 0 : Word16 shl_r( Word16 var1, Word16 var2 )
424 : {
425 : Word16 var_out;
426 :
427 0 : if ( var2 >= 0 )
428 : {
429 0 : var_out = shl( var1, var2 );
430 : #ifdef WMOPS
431 : multiCounter[currCounter].shl--;
432 : #endif
433 : }
434 : else
435 : {
436 0 : var2 = -var2;
437 0 : var_out = shr_r( var1, var2 );
438 : #ifdef WMOPS
439 : multiCounter[currCounter].shr_r--;
440 : #endif
441 : }
442 :
443 : #ifdef WMOPS
444 : multiCounter[currCounter].shl_r++;
445 : #endif
446 :
447 0 : return ( var_out );
448 : }
449 :
450 :
451 : /*****************************************************************************
452 : *
453 : * Function Name : L_shl_r
454 : *
455 : * Purpose :
456 : *
457 : * Same as L_shl( var1, var2) but with rounding. Saturates the result in
458 : * case of underflows or overflows.
459 : *
460 : * Complexity weight : 3
461 : *
462 : * Inputs :
463 : *
464 : * L_var1 32 bit long signed integer (Word32) whose value falls in
465 : * the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
466 : *
467 : * var2 16 bit short signed integer (Word16) whose value falls in
468 : * the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
469 : *
470 : * Outputs :
471 : *
472 : * none
473 : *
474 : * Return Value :
475 : *
476 : * L_var_out 32 bit long signed integer (Word32) whose value falls in
477 : * the range : 0x8000 0000 <= var_out <= 0x7fff ffff.
478 : *
479 : *****************************************************************************/
480 6924 : Word32 L_shl_r( Word32 L_var1, Word16 var2 )
481 : {
482 : Word32 var_out;
483 :
484 6924 : if ( var2 >= 0 )
485 : {
486 5976 : var_out = L_shl( L_var1, var2 );
487 : #ifdef WMOPS
488 : multiCounter[currCounter].L_shl--;
489 : #endif
490 : }
491 : else
492 : {
493 948 : var2 = -var2;
494 948 : var_out = L_shr_r( L_var1, var2 );
495 : #ifdef WMOPS
496 : multiCounter[currCounter].L_shr_r--;
497 : #endif
498 : }
499 :
500 : #ifdef WMOPS
501 : multiCounter[currCounter].L_shl_r++;
502 : #endif
503 :
504 6924 : return ( var_out );
505 : }
506 :
507 :
508 : /*****************************************************************************
509 : *
510 : * Function Name : rotr
511 : *
512 : * Purpose :
513 : *
514 : * Performs a 16-bit logical rotation of var1 by 1 bit to the LSBits. The
515 : * MSBit is set to var2 bit 0. The LSBit of var1 is kept in *var3 bit 0.
516 : *
517 : * Complexity weight : 3
518 : *
519 : * Inputs :
520 : *
521 : * var1 16 bit short signed integer (Word16) whose value falls in
522 : * the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
523 : *
524 : * var2 16 bit short signed integer (Word16) whose value must be 0
525 : * or 1.
526 : *
527 : * Outputs :
528 : *
529 : * *var3 Points on a 16 bit short signed integer (Word16) whose
530 : * value will be 0 or 1.
531 : *
532 : * Return Value :
533 : *
534 : * var_out 16 bit short signed integer (Word16) whose value falls in
535 : * the range : 0xffff 8000 <= var_out <= 0x0000 7fff.
536 : *
537 : *****************************************************************************/
538 0 : Word16 rotr( Word16 var1, Word16 var2, Word16 *var3 )
539 : {
540 : Word16 var_out;
541 :
542 0 : *var3 = s_and( var1, 0x1 );
543 0 : var_out = s_or( lshr( var1, 1 ), lshl( var2, 15 ) );
544 :
545 : #ifdef WMOPS
546 : multiCounter[currCounter].s_and--;
547 : multiCounter[currCounter].lshl--;
548 : multiCounter[currCounter].lshr--;
549 : multiCounter[currCounter].s_or--;
550 : multiCounter[currCounter].rotr++;
551 : #endif
552 :
553 0 : return ( var_out );
554 : }
555 :
556 :
557 : /*****************************************************************************
558 : *
559 : * Function Name : rotl
560 : *
561 : * Purpose :
562 : *
563 : * Performs a 16-bit logical rotation of var1 by 1 bit to the MSBits. The
564 : * LSBit is set to var2 bit 0. The MSBit of var1 is kept in *var3 bit 0.
565 : *
566 : * Complexity weight : 3
567 : *
568 : * Inputs :
569 : *
570 : * var1 16 bit short signed integer (Word16) whose value falls in
571 : * the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
572 : *
573 : * var2 16 bit short signed integer (Word16) whose value must be 0
574 : * or 1.
575 : *
576 : * Outputs :
577 : *
578 : * *var3 Points on a 16 bit short signed integer (Word16) whose
579 : * value will be 0 or 1.
580 : *
581 : * Return Value :
582 : *
583 : * var_out 16 bit short signed integer (Word16) whose value falls in
584 : * the range : 0xffff 8000 <= var_out <= 0x0000 7fff.
585 : *
586 : *****************************************************************************/
587 0 : Word16 rotl( Word16 var1, Word16 var2, Word16 *var3 )
588 : {
589 : Word16 var_out;
590 :
591 0 : *var3 = lshr( var1, 15 );
592 :
593 0 : var_out = s_or( lshl( var1, 1 ), s_and( var2, 0x1 ) );
594 :
595 : #ifdef WMOPS
596 : multiCounter[currCounter].lshr--;
597 : multiCounter[currCounter].s_and--;
598 : multiCounter[currCounter].lshl--;
599 : multiCounter[currCounter].s_or--;
600 : multiCounter[currCounter].rotl++;
601 : #endif
602 :
603 0 : return ( var_out );
604 : }
605 :
606 :
607 : /*****************************************************************************
608 : *
609 : * Function Name : L_rotr
610 : *
611 : * Purpose :
612 : *
613 : * Performs a 32-bit logical rotation of L_var1 by 1 bit to the LSBits. The
614 : * MSBit is set to var2 bit 0. The LSBit of L_var1 is kept in *var3 bit 0.
615 : *
616 : * Complexity weight : 3
617 : *
618 : * Inputs :
619 : *
620 : * L_var1 32 bit long signed integer (Word32) whose value falls in
621 : * the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
622 : *
623 : * var2 16 bit short signed integer (Word16) whose value must be 0
624 : * or 1.
625 : *
626 : * Outputs :
627 : *
628 : * *var3 Points on a 16 bit short signed integer (Word16) whose
629 : * value will be 0 or 1.
630 : *
631 : * Return Value :
632 : *
633 : * L_var_out 32 bit long signed integer (Word32) whose value falls in
634 : * the range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
635 : *
636 : *****************************************************************************/
637 0 : Word32 L_rotr( Word32 L_var1, Word16 var2, Word16 *var3 )
638 : {
639 : Word32 L_var_out;
640 :
641 0 : *var3 = s_and( extract_l( L_var1 ), 0x1 );
642 :
643 0 : L_var_out = L_or( L_lshr( L_var1, 1 ), L_lshl( L_deposit_l( var2 ), 31 ) );
644 :
645 : #ifdef WMOPS
646 : multiCounter[currCounter].extract_l--;
647 : multiCounter[currCounter].s_and--;
648 : multiCounter[currCounter].L_deposit_l--;
649 : multiCounter[currCounter].L_lshl--;
650 : multiCounter[currCounter].L_lshr--;
651 : multiCounter[currCounter].L_or--;
652 : multiCounter[currCounter].L_rotr++;
653 : #endif
654 :
655 0 : return ( L_var_out );
656 : }
657 :
658 :
659 : /*****************************************************************************
660 : *
661 : * Function Name : L_rotl
662 : *
663 : * Purpose :
664 : *
665 : * Performs a 32-bit logical rotation of L_var1 by 1 bit to the MSBits. The
666 : * LSBit is set to var2 bit 0. The MSBit of L_var1 is kept in *var3 bit 0.
667 : *
668 : * Complexity weight : 3
669 : *
670 : * Inputs :
671 : *
672 : * L_var1 32 bit long signed integer (Word32) whose value falls in
673 : * the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
674 : *
675 : * var2 16 bit short signed integer (Word16) whose value must be 0
676 : * or 1.
677 : *
678 : * Outputs :
679 : *
680 : * *var3 Points on a 16 bit short signed integer (Word16) whose
681 : * value will be 0 or 1.
682 : *
683 : * Return Value :
684 : *
685 : * L_var_out 32 bit long signed integer (Word32) whose value falls in
686 : * the range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
687 : *
688 : *****************************************************************************/
689 0 : Word32 L_rotl( Word32 L_var1, Word16 var2, Word16 *var3 )
690 : {
691 : Word32 L_var_out;
692 :
693 0 : *var3 = extract_l( L_lshr( L_var1, 31 ) );
694 :
695 0 : L_var_out = L_or( L_lshl( L_var1, 1 ), L_deposit_l( s_and( var2, 0x1 ) ) );
696 :
697 : #ifdef WMOPS
698 : multiCounter[currCounter].L_lshr--;
699 : multiCounter[currCounter].extract_l--;
700 : multiCounter[currCounter].s_and--;
701 : multiCounter[currCounter].L_deposit_l--;
702 : multiCounter[currCounter].L_lshl--;
703 : multiCounter[currCounter].L_or--;
704 : multiCounter[currCounter].L_rotl++;
705 : #endif
706 :
707 0 : return ( L_var_out );
708 : }
709 :
710 : #undef WMC_TOOL_SKIP
|