3
#if !PJMEDIA_LIBG7221_FUNCS_INLINED || \
4
(PJMEDIA_LIBG7221_FUNCS_INLINED && defined(__BASIC_OP_H__))
6
/*___________________________________________________________________________
8
| Basic arithmetic operators. |
9
|___________________________________________________________________________|
12
/*___________________________________________________________________________
15
|___________________________________________________________________________|
22
#include <pj/assert.h>
26
extern BASIC_OP multiCounter[MAXCOUNTERS];
27
extern int currCounter;
31
/*___________________________________________________________________________
33
| Constants and Globals |
34
|___________________________________________________________________________|
37
Flag g7221_Overflow = 0;
41
/*___________________________________________________________________________
44
|___________________________________________________________________________|
46
/*___________________________________________________________________________
48
| Function Name : shr |
52
| Arithmetically shift the 16 bit input var1 right var2 positions with |
53
| sign extension. If var2 is negative, arithmetically shift var1 left by |
54
| -var2 with sign extension. Saturate the result in case of underflows or |
57
| Complexity weight : 1 |
62
| 16 bit short signed integer (Word16) whose value falls in the |
63
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
66
| 16 bit short signed integer (Word16) whose value falls in the |
67
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
76
| 16 bit short signed integer (Word16) whose value falls in the |
77
| range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
78
|___________________________________________________________________________|
80
LIBG7221_DEF(Word16) shr (Word16 var1, Word16 var2)
86
return shl_nocheck(var1, (Word16) -var2);
90
return shr_nocheck(var1, var2);
93
/* ------------------------- End of shr() ------------------------- */
96
/*___________________________________________________________________________
98
| Function Name : shl |
102
| Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill|
103
| the var2 LSB of the result. If var2 is negative, arithmetically shift |
104
| var1 right by -var2 with sign extension. Saturate the result in case of |
105
| underflows or overflows. |
107
| Complexity weight : 1 |
112
| 16 bit short signed integer (Word16) whose value falls in the |
113
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
116
| 16 bit short signed integer (Word16) whose value falls in the |
117
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
126
| 16 bit short signed integer (Word16) whose value falls in the |
127
| range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
128
|___________________________________________________________________________|
130
LIBG7221_DEF(Word16) shl (Word16 var1, Word16 var2)
134
return shr_nocheck(var1, (Word16) -var2);
138
return shl_nocheck(var1, var2);
141
/* ------------------------- End of shl() ------------------------- */
145
/*___________________________________________________________________________
147
| Function Name : mult |
151
| Performs the multiplication of var1 by var2 and gives a 16 bit result |
152
| which is scaled i.e.: |
153
| mult(var1,var2) = extract_l(L_shr((var1 times var2),15)) and |
154
| mult(-32768,-32768) = 32767. |
156
| Complexity weight : 1 |
161
| 16 bit short signed integer (Word16) whose value falls in the |
162
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
165
| 16 bit short signed integer (Word16) whose value falls in the |
166
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
175
| 16 bit short signed integer (Word16) whose value falls in the |
176
| range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
177
|___________________________________________________________________________|
179
LIBG7221_DEF(Word16) mult (Word16 var1, Word16 var2)
184
L_product = (Word32) var1 *(Word32) var2;
186
L_product = (L_product & (Word32) 0xffff8000L) >> 15;
188
if (L_product & (Word32) 0x00010000L)
189
L_product = L_product | (Word32) 0xffff0000L;
191
var_out = saturate (L_product);
193
multiCounter[currCounter].mult++;
197
/* ------------------------- End of mult() ------------------------- */
200
/*___________________________________________________________________________
202
| Function Name : L_msu |
206
| Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
207
| bit result to L_var3 with saturation, return a 32 bit result: |
208
| L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)). |
210
| Complexity weight : 1 |
214
| L_var3 32 bit long signed integer (Word32) whose value falls in the |
215
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
218
| 16 bit short signed integer (Word16) whose value falls in the |
219
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
222
| 16 bit short signed integer (Word16) whose value falls in the |
223
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
232
| 32 bit long signed integer (Word32) whose value falls in the |
233
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
234
|___________________________________________________________________________|
236
LIBG7221_DEF(Word32) L_msu (Word32 L_var3, Word16 var1, Word16 var2)
241
L_product = L_mult (var1, var2);
243
multiCounter[currCounter].L_mult--;
245
L_var_out = L_sub (L_var3, L_product);
247
multiCounter[currCounter].L_sub--;
248
multiCounter[currCounter].L_msu++;
252
/* ------------------------- End of L_msu() ------------------------- */
255
/*___________________________________________________________________________
257
| Function Name : L_macNs |
261
| Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
262
| result to L_var3 without saturation, return a 32 bit result. Generate |
263
| carry and overflow values : |
264
| L_macNs(L_var3,var1,var2) = L_add_c(L_var3,L_mult(var1,var2)). |
266
| Complexity weight : 1 |
270
| L_var3 32 bit long signed integer (Word32) whose value falls in the |
271
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
274
| 16 bit short signed integer (Word16) whose value falls in the |
275
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
278
| 16 bit short signed integer (Word16) whose value falls in the |
279
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
288
| 32 bit long signed integer (Word32) whose value falls in the |
289
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
293
| In some cases the Carry flag has to be cleared or set before using |
294
| operators which take into account its value. |
295
|___________________________________________________________________________|
297
LIBG7221_DEF(Word32) L_macNs (Word32 L_var3, Word16 var1, Word16 var2)
301
L_var_out = L_mult (var1, var2);
303
multiCounter[currCounter].L_mult--;
305
L_var_out = L_add_c (L_var3, L_var_out);
307
multiCounter[currCounter].L_add_c--;
308
multiCounter[currCounter].L_macNs++;
313
/* ------------------------- End of L_macNs() ------------------------- */
316
/*___________________________________________________________________________
318
| Function Name : L_msuNs |
322
| Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
323
| bit result from L_var3 without saturation, return a 32 bit result. Ge- |
324
| nerate carry and overflow values : |
325
| L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,L_mult(var1,var2)). |
327
| Complexity weight : 1 |
331
| L_var3 32 bit long signed integer (Word32) whose value falls in the |
332
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
335
| 16 bit short signed integer (Word16) whose value falls in the |
336
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
339
| 16 bit short signed integer (Word16) whose value falls in the |
340
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
349
| 32 bit long signed integer (Word32) whose value falls in the |
350
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
354
| In some cases the Carry flag has to be cleared or set before using |
355
| operators which take into account its value. |
356
|___________________________________________________________________________|
358
LIBG7221_DEF(Word32) L_msuNs (Word32 L_var3, Word16 var1, Word16 var2)
362
L_var_out = L_mult (var1, var2);
364
multiCounter[currCounter].L_mult--;
366
L_var_out = L_sub_c (L_var3, L_var_out);
368
multiCounter[currCounter].L_sub_c--;
369
multiCounter[currCounter].L_msuNs++;
375
/* ------------------------- End of L_msuNs() ------------------------- */
379
/*___________________________________________________________________________
381
| Function Name : L_add_c |
385
| Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)|
386
| with carry. No saturation. Generate carry and Overflow values. The car- |
387
| ry and overflow values are binary variables which can be tested and as- |
390
| Complexity weight : 2 |
394
| L_var1 32 bit long signed integer (Word32) whose value falls in the |
395
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
397
| L_var2 32 bit long signed integer (Word32) whose value falls in the |
398
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
407
| 32 bit long signed integer (Word32) whose value falls in the |
408
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
412
| In some cases the Carry flag has to be cleared or set before using |
413
| operators which take into account its value. |
414
|___________________________________________________________________________|
416
LIBG7221_DEF(Word32) L_add_c (Word32 L_var1, Word32 L_var2)
422
L_var_out = L_var1 + L_var2 + GET_CARRY();
424
L_test = L_var1 + L_var2;
426
if ((L_var1 > 0) && (L_var2 > 0) && (L_test < 0))
433
if ((L_var1 < 0) && (L_var2 < 0))
448
if (((L_var1 ^ L_var2) < 0) && (L_test >= 0))
463
if (L_test == MAX_32)
466
SET_CARRY(carry_int);
470
if (L_test == (Word32) 0xFFFFFFFFL)
476
SET_CARRY(carry_int);
482
SET_CARRY(carry_int);
486
multiCounter[currCounter].L_add_c++;
492
/* ------------------------- End of L_add_c() ------------------------- */
495
/*___________________________________________________________________________
497
| Function Name : L_sub_c |
501
| Performs 32 bits subtraction of the two 32 bits variables with carry |
502
| (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow |
503
| values. The carry and overflow values are binary variables which can |
504
| be tested and assigned values. |
506
| Complexity weight : 2 |
510
| L_var1 32 bit long signed integer (Word32) whose value falls in the |
511
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
513
| L_var2 32 bit long signed integer (Word32) whose value falls in the |
514
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
523
| 32 bit long signed integer (Word32) whose value falls in the |
524
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
528
| In some cases the Carry flag has to be cleared or set before using |
529
| operators which take into account its value. |
530
|___________________________________________________________________________|
532
LIBG7221_DEF(Word32) L_sub_c (Word32 L_var1, Word32 L_var2)
541
if (L_var2 != MIN_32)
543
L_var_out = L_add_c (L_var1, -L_var2);
545
multiCounter[currCounter].L_add_c--;
550
L_var_out = L_var1 - L_var2;
560
L_var_out = L_var1 - L_var2 - (Word32) 0X00000001L;
561
L_test = L_var1 - L_var2;
563
if ((L_test < 0) && (L_var1 > 0) && (L_var2 < 0))
568
else if ((L_test > 0) && (L_var1 < 0) && (L_var2 > 0))
573
else if ((L_test > 0) && ((L_var1 ^ L_var2) > 0))
578
if (L_test == MIN_32)
581
SET_CARRY(carry_int);
585
SET_CARRY(carry_int);
590
multiCounter[currCounter].L_sub_c++;
595
/* ------------------------- End of L_sub_c() ------------------------- */
598
/*___________________________________________________________________________
600
| Function Name : L_negate |
604
| Negate the 32 bit variable L_var1 with saturation; saturate in the case |
605
| where input is -2147483648 (0x8000 0000). |
607
| Complexity weight : 2 |
611
| L_var1 32 bit long signed integer (Word32) whose value falls in the |
612
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
621
| 32 bit long signed integer (Word32) whose value falls in the |
622
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
623
|___________________________________________________________________________|
625
LIBG7221_DEF(Word32) L_negate (Word32 L_var1)
629
L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1;
631
multiCounter[currCounter].L_negate++;
635
/* ------------------------- End of L_negate() ------------------------- */
638
/*___________________________________________________________________________
640
| Function Name : mult_r |
644
| Same as mult with rounding, i.e.: |
645
| mult_r(var1,var2) = extract_l(L_shr(((var1 * var2) + 16384),15)) and |
646
| mult_r(-32768,-32768) = 32767. |
648
| Complexity weight : 2 |
653
| 16 bit short signed integer (Word16) whose value falls in the |
654
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
657
| 16 bit short signed integer (Word16) whose value falls in the |
658
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
667
| 16 bit short signed integer (Word16) whose value falls in the |
668
| range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
669
|___________________________________________________________________________|
671
LIBG7221_DEF(Word16) mult_r (Word16 var1, Word16 var2)
674
Word32 L_product_arr;
676
L_product_arr = (Word32) var1 *(Word32) var2; /* product */
677
L_product_arr += (Word32) 0x00004000L; /* round */
678
L_product_arr &= (Word32) 0xffff8000L;
679
L_product_arr >>= 15; /* shift */
681
if (L_product_arr & (Word32) 0x00010000L) /* sign extend when necessary */
683
L_product_arr |= (Word32) 0xffff0000L;
685
var_out = saturate (L_product_arr);
687
multiCounter[currCounter].mult_r++;
691
/* ------------------------- End of mult_r() ------------------------- */
695
/*___________________________________________________________________________
697
| Function Name : shr_r |
701
| Same as shr(var1,var2) but with rounding. Saturate the result in case of|
702
| underflows or overflows : |
703
| - If var2 is greater than zero : |
704
| if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1)))) |
707
| shr_r(var1,var2) = shr(var1,var2) |
709
| shr_r(var1,var2) = add(shr(var1,var2),1) |
710
| - If var2 is less than or equal to zero : |
711
| shr_r(var1,var2) = shr(var1,var2). |
713
| Complexity weight : 2 |
718
| 16 bit short signed integer (Word16) whose value falls in the |
719
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
722
| 16 bit short signed integer (Word16) whose value falls in the |
723
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
732
| 16 bit short signed integer (Word16) whose value falls in the |
733
| range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
734
|___________________________________________________________________________|
736
LIBG7221_DEF(Word16) shr_r (Word16 var1, Word16 var2)
746
var_out = shr (var1, var2);
748
multiCounter[currCounter].shr--;
753
if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
760
multiCounter[currCounter].shr_r++;
764
/* ------------------------- End of shr_r() ------------------------- */
767
/*___________________________________________________________________________
769
| Function Name : mac_r |
773
| Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
774
| result to L_var3 with saturation. Round the LS 16 bits of the result |
775
| into the MS 16 bits with saturation and shift the result right by 16. |
776
| Return a 16 bit result. |
777
| mac_r(L_var3,var1,var2) = round(L_mac(L_var3,var1,var2)) |
779
| Complexity weight : 2 |
783
| L_var3 32 bit long signed integer (Word32) whose value falls in the |
784
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
787
| 16 bit short signed integer (Word16) whose value falls in the |
788
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
791
| 16 bit short signed integer (Word16) whose value falls in the |
792
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
801
| 16 bit short signed integer (Word16) whose value falls in the |
802
| range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. |
803
|___________________________________________________________________________|
805
LIBG7221_DEF(Word16) mac_r (Word32 L_var3, Word16 var1, Word16 var2)
809
L_var3 = L_mac (L_var3, var1, var2);
811
multiCounter[currCounter].L_mac--;
813
L_var3 = L_add (L_var3, (Word32) 0x00008000L);
815
multiCounter[currCounter].L_add--;
817
var_out = extract_h (L_var3);
819
multiCounter[currCounter].extract_h--;
820
multiCounter[currCounter].mac_r++;
824
/* ------------------------- End of mac_r() ------------------------- */
827
/*___________________________________________________________________________
829
| Function Name : msu_r |
833
| Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
834
| bit result to L_var3 with saturation. Round the LS 16 bits of the res- |
835
| ult into the MS 16 bits with saturation and shift the result right by |
836
| 16. Return a 16 bit result. |
837
| msu_r(L_var3,var1,var2) = round(L_msu(L_var3,var1,var2)) |
839
| Complexity weight : 2 |
843
| L_var3 32 bit long signed integer (Word32) whose value falls in the |
844
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
847
| 16 bit short signed integer (Word16) whose value falls in the |
848
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
851
| 16 bit short signed integer (Word16) whose value falls in the |
852
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
861
| 16 bit short signed integer (Word16) whose value falls in the |
862
| range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. |
863
|___________________________________________________________________________|
865
LIBG7221_DEF(Word16) msu_r (Word32 L_var3, Word16 var1, Word16 var2)
869
L_var3 = L_msu (L_var3, var1, var2);
871
multiCounter[currCounter].L_msu--;
873
L_var3 = L_add (L_var3, (Word32) 0x00008000L);
875
multiCounter[currCounter].L_add--;
877
var_out = extract_h (L_var3);
879
multiCounter[currCounter].extract_h--;
880
multiCounter[currCounter].msu_r++;
884
/* ------------------------- End of msu_r() ------------------------- */
887
/*___________________________________________________________________________
889
| Function Name : L_deposit_h |
893
| Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The |
894
| 16 LS bits of the output are zeroed. |
896
| Complexity weight : 2 |
901
| 16 bit short signed integer (Word16) whose value falls in the |
902
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
911
| 32 bit long signed integer (Word32) whose value falls in the |
912
| range : 0x8000 0000 <= var_out <= 0x7fff 0000. |
913
|___________________________________________________________________________|
915
LIBG7221_DEF(Word32) L_deposit_h (Word16 var1)
919
L_var_out = (Word32) var1 << 16;
921
multiCounter[currCounter].L_deposit_h++;
925
/* ------------------------- End of L_deposit_h() ------------------------- */
928
/*___________________________________________________________________________
930
| Function Name : L_deposit_l |
934
| Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The |
935
| 16 MS bits of the output are sign extended. |
937
| Complexity weight : 2 |
942
| 16 bit short signed integer (Word16) whose value falls in the |
943
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
952
| 32 bit long signed integer (Word32) whose value falls in the |
953
| range : 0xFFFF 8000 <= var_out <= 0x0000 7fff. |
954
|___________________________________________________________________________|
956
LIBG7221_DEF(Word32) L_deposit_l (Word16 var1)
960
L_var_out = (Word32) var1;
962
multiCounter[currCounter].L_deposit_l++;
966
/* ------------------------- End of L_deposit_l() ------------------------- */
969
/*___________________________________________________________________________
971
| Function Name : L_shr_r |
975
| Same as L_shr(L_var1,var2) but with rounding. Saturate the result in |
976
| case of underflows or overflows : |
977
| - If var2 is greater than zero : |
978
| if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))|
981
| L_shr_r(L_var1,var2) = L_shr(L_var1,var2) |
983
| L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1) |
984
| - If var2 is less than or equal to zero : |
985
| L_shr_r(L_var1,var2) = L_shr(L_var1,var2). |
987
| Complexity weight : 3 |
992
| 32 bit long signed integer (Word32) whose value falls in the |
993
| range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
996
| 16 bit short signed integer (Word16) whose value falls in the |
997
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1006
| 32 bit long signed integer (Word32) whose value falls in the |
1007
| range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
1008
|___________________________________________________________________________|
1010
LIBG7221_DEF(Word32) L_shr_r (Word32 L_var1, Word16 var2)
1020
L_var_out = L_shr (L_var1, var2);
1022
multiCounter[currCounter].L_shr--;
1026
if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
1033
multiCounter[currCounter].L_shr_r++;
1037
/* ------------------------- End of L_shr_r() ------------------------- */
1040
/*___________________________________________________________________________
1042
| Function Name : L_abs |
1046
| Absolute value of L_var1; Saturate in case where the input is |
1049
| Complexity weight : 3 |
1054
| 32 bit long signed integer (Word32) whose value falls in the |
1055
| range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
1064
| 32 bit long signed integer (Word32) whose value falls in the |
1065
| range : 0x0000 0000 <= var_out <= 0x7fff ffff. |
1066
|___________________________________________________________________________|
1068
LIBG7221_DEF(Word32) L_abs (Word32 L_var1)
1072
if (L_var1 == MIN_32)
1080
L_var_out = -L_var1;
1089
multiCounter[currCounter].L_abs++;
1093
/* ------------------------- End of L_abs() ------------------------- */
1096
/*___________________________________________________________________________
1098
| Function Name : norm_s |
1102
| Produces the number of left shift needed to normalize the 16 bit varia- |
1103
| ble var1 for positive values on the interval with minimum of 16384 and |
1104
| maximum of 32767, and for negative values on the interval with minimum |
1105
| of -32768 and maximum of -16384; in order to normalize the result, the |
1106
| following operation must be done : |
1107
| norm_var1 = shl(var1,norm_s(var1)). |
1109
| Complexity weight : 15 |
1114
| 16 bit short signed integer (Word16) whose value falls in the |
1115
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1124
| 16 bit short signed integer (Word16) whose value falls in the |
1125
| range : 0x0000 0000 <= var_out <= 0x0000 000f. |
1126
|___________________________________________________________________________|
1128
LIBG7221_DEF(Word16) norm_s (Word16 var1)
1138
if ((UWord16)var1 == (UWord16)0xffff)
1146
var1 = (Word16)(~var1);
1148
for (var_out = 0; var1 < 0x4000; var_out++)
1156
multiCounter[currCounter].norm_s++;
1160
/* ------------------------- End of norm_s() ------------------------- */
1163
/*___________________________________________________________________________
1165
| Function Name : div_s |
1169
| Produces a result which is the fractional integer division of var1 by |
1170
| var2; var1 and var2 must be positive and var2 must be greater or equal |
1171
| to var1; the result is positive (leading bit equal to 0) and truncated |
1173
| If var1 = var2 then div(var1,var2) = 32767. |
1175
| Complexity weight : 18 |
1180
| 16 bit short signed integer (Word16) whose value falls in the |
1181
| range : 0x0000 0000 <= var1 <= var2 and var2 != 0. |
1184
| 16 bit short signed integer (Word16) whose value falls in the |
1185
| range : var1 <= var2 <= 0x0000 7fff and var2 != 0. |
1194
| 16 bit short signed integer (Word16) whose value falls in the |
1195
| range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
1196
| It's a Q15 value (point between b15 and b14). |
1197
|___________________________________________________________________________|
1199
LIBG7221_DEF(Word16) div_s (Word16 var1, Word16 var2)
1206
if ((var1 > var2) || (var1 < 0) || (var2 < 0))
1208
//printf ("Division Error var1=%d var2=%d\n", var1, var2);
1209
//abort(); /* exit (0); */
1210
pj_assert(!"Division Error");
1214
//printf ("Division by 0, Fatal error \n");
1215
//abort(); /* exit (0); */
1216
assert(!"Division by 0");
1230
L_num = L_deposit_l (var1);
1232
multiCounter[currCounter].L_deposit_l--;
1234
L_denom = L_deposit_l (var2);
1236
multiCounter[currCounter].L_deposit_l--;
1239
for (iteration = 0; iteration < 15; iteration++)
1244
if (L_num >= L_denom)
1246
L_num = L_sub (L_num, L_denom);
1248
multiCounter[currCounter].L_sub--;
1250
var_out = add (var_out, 1);
1252
multiCounter[currCounter].add--;
1260
multiCounter[currCounter].div_s++;
1264
/* ------------------------- End of div_s() ------------------------- */
1267
/*___________________________________________________________________________
1269
| Function Name : norm_l |
1273
| Produces the number of left shifts needed to normalize the 32 bit varia-|
1274
| ble L_var1 for positive values on the interval with minimum of |
1275
| 1073741824 and maximum of 2147483647, and for negative values on the in-|
1276
| terval with minimum of -2147483648 and maximum of -1073741824; in order |
1277
| to normalize the result, the following operation must be done : |
1278
| norm_L_var1 = L_shl(L_var1,norm_l(L_var1)). |
1280
| Complexity weight : 30 |
1285
| 32 bit long signed integer (Word32) whose value falls in the |
1286
| range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
1295
| 16 bit short signed integer (Word16) whose value falls in the |
1296
| range : 0x0000 0000 <= var_out <= 0x0000 001f. |
1297
|___________________________________________________________________________|
1299
LIBG7221_DEF(Word16) norm_l (Word32 L_var1)
1309
if (L_var1 == (Word32) 0xffffffffL)
1319
for (var_out = 0; L_var1 < (Word32) 0x40000000L; var_out++)
1327
multiCounter[currCounter].norm_l++;
1331
/* ------------------------- End of norm_l() ------------------------- */
1335
*****************************************************************
1336
Additional operators extracted from the G.723.1 Library
1337
Adapted for WMOPS calculations
1338
*****************************************************************
1341
/*___________________________________________________________________________
1343
| Function Name : L_mls |
1347
| Multiplies a 16 bit word v by a 32 bit word Lv and returns a 32 bit |
1348
| word (multiplying 16 by 32 bit words gives 48 bit word; the function |
1349
| extracts the 32 MSB and shift the result to the left by 1). |
1351
| A 32 bit word can be written as |
1352
| Lv = a + b * 2^16 |
1353
| where a= unsigned 16 LSBs and b= signed 16 MSBs. |
1354
| The function returns v * Lv / 2^15 which is equivalent to |
1355
| a*v / 2^15 + b*v*2 |
1357
| Complexity weight : 6 [to be confirmed] |
1362
| 32 bit long signed integer (Word32) whose value falls in the |
1363
| range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
1365
| 16 bit short signed integer (Word16) whose value falls in the |
1366
| range : 0x8000 <= var1 <= 0x7fff. |
1375
| 32 bit long signed integer (Word32) whose value falls in the |
1376
| range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
1378
|___________________________________________________________________________|
1380
LIBG7221_DEF(Word32) L_mls (Word32 Lv, Word16 v)
1384
Temp = Lv & (Word32) 0x0000ffff ;
1385
Temp = Temp * (Word32) v ;
1386
Temp = L_shr_nocheck( Temp, (Word16) 15 ) ;
1387
Temp = L_mac( Temp, v, extract_h(Lv) ) ;
1390
multiCounter[currCounter].L_shr--;
1391
multiCounter[currCounter].L_mac--;
1392
multiCounter[currCounter].extract_h--;
1393
multiCounter[currCounter].L_mls++;
1398
/* ------------------------- End of L_mls() ------------------------- */
1401
/*__________________________________________________________________________
1403
| Function Name : div_l |
1407
| Produces a result which is the fractional integer division of L_var1 by|
1408
| var2; L_var1 and var2 must be positive and var2 << 16 must be greater or|
1409
| equal to L_var1; the result is positive (leading bit equal to 0) and |
1410
| truncated to 16 bits. |
1411
| If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767. |
1413
| Complexity weight : 20 |
1418
| 32 bit long signed integer (Word32) whose value falls in the |
1419
| range : 0x0000 0000 <= var1 <= (var2 << 16) and var2 != 0. |
1420
| L_var1 must be considered as a Q.31 value |
1423
| 16 bit short signed integer (Word16) whose value falls in the |
1424
| range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0. |
1425
| var2 must be considered as a Q.15 value |
1434
| 16 bit short signed integer (Word16) whose value falls in the |
1435
| range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
1436
| It's a Q15 value (point between b15 and b14). |
1437
|___________________________________________________________________________|
1439
LIBG7221_DEF(Word16) div_l (Word32 L_num, Word16 den)
1441
Word16 var_out = (Word16)0;
1446
multiCounter[currCounter].div_l++;
1449
if ( den == (Word16) 0 ) {
1450
//printf("Division by 0 in div_l, Fatal error \n");
1452
assert(!"Division by 0");
1455
if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) {
1456
//printf("Division Error in div_l, Fatal error \n");
1458
assert(!"Division Error");
1461
L_den = L_deposit_h( den ) ;
1463
multiCounter[currCounter].L_deposit_h--;
1466
if ( L_num >= L_den ){
1470
L_num = L_shr_nocheck(L_num, (Word16)1) ;
1471
L_den = L_shr_nocheck(L_den, (Word16)1);
1473
multiCounter[currCounter].L_shr-=2;
1475
for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
1476
var_out = shl_nocheck( var_out, (Word16)1);
1477
L_num = L_shl_nocheck( L_num, (Word16)1);
1479
multiCounter[currCounter].shl--;
1480
multiCounter[currCounter].L_shl--;
1482
if (L_num >= L_den) {
1483
L_num = L_sub(L_num,L_den);
1484
var_out = add(var_out, (Word16)1);
1486
multiCounter[currCounter].L_sub--;
1487
multiCounter[currCounter].add--;
1495
/* ------------------------- End of div_l() ------------------------- */
1498
/*__________________________________________________________________________
1500
| Function Name : i_mult |
1504
| Integer 16-bit multiplication. No overflow protection is performed if |
1505
| ORIGINAL_G7231 is defined. |
1507
| Complexity weight : TBD |
1512
| 16 bit short signed integer (Word16). |
1515
| 16 bit short signed integer (Word16). |
1523
| 16 bit short signed integer (Word16). No overflow checks |
1524
| are performed if ORIGINAL_G7231 is defined. |
1525
|___________________________________________________________________________|
1527
LIBG7221_DEF(Word16) i_mult (Word16 a, Word16 b)
1529
#ifdef ORIGINAL_G7231
1532
Word32 register c=a*b;
1534
multiCounter[currCounter].i_mult++;
1536
return saturate(c) ;
1539
/* ------------------------- End of i_mult() ------------------------- */
1543
**********************************************************************
1544
The following three operators are not part of the original
1545
G.729/G.723.1 set of basic operators and implement shiftless
1546
accumulation operation.
1547
**********************************************************************
1550
/*___________________________________________________________________________
1552
| Function Name : L_mult0
1556
| L_mult0 is the 32 bit result of the multiplication of var1 times var2
1557
| without one left shift.
1559
| Complexity weight : 1
1563
| var1 16 bit short signed integer (Word16) whose value falls in the
1564
| range : 0xffff 8000 <= var1 <= 0x0000 7fff.
1566
| var2 16 bit short signed integer (Word16) whose value falls in the
1567
| range : 0xffff 8000 <= var1 <= 0x0000 7fff.
1572
| 32 bit long signed integer (Word32) whose value falls in the
1573
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
1574
|___________________________________________________________________________
1576
LIBG7221_DEF(Word32) L_mult0 (Word16 var1,Word16 var2)
1580
L_var_out = (Word32)var1 * (Word32)var2;
1583
multiCounter[currCounter].L_mult0++;
1587
/* ------------------------- End of L_mult0() ------------------------- */
1590
/*___________________________________________________________________________
1592
| Function Name : L_mac0
1596
| Multiply var1 by var2 (without left shift) and add the 32 bit result to
1597
| L_var3 with saturation, return a 32 bit result:
1598
| L_mac0(L_var3,var1,var2) = L_add(L_var3,(L_mult0(var1,var2)).
1600
| Complexity weight : 1
1604
| L_var3 32 bit long signed integer (Word32) whose value falls in the
1605
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
1607
| var1 16 bit short signed integer (Word16) whose value falls in the
1608
| range : 0xffff 8000 <= var1 <= 0x0000 7fff.
1610
| var2 16 bit short signed integer (Word16) whose value falls in the
1611
| range : 0xffff 8000 <= var1 <= 0x0000 7fff.
1616
| 32 bit long signed integer (Word32) whose value falls in the
1617
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
1618
|___________________________________________________________________________
1620
LIBG7221_DEF(Word32) L_mac0 (Word32 L_var3, Word16 var1, Word16 var2)
1625
L_product = L_mult0(var1,var2);
1626
L_var_out = L_add(L_var3,L_product);
1629
multiCounter[currCounter].L_mac0++;
1630
multiCounter[currCounter].L_mult0--;
1631
multiCounter[currCounter].L_add--;
1635
/* ------------------------- End of L_mac0() ------------------------- */
1638
/*___________________________________________________________________________
1640
| Function Name : L_msu0
1644
| Multiply var1 by var2 (without left shift) and subtract the 32 bit
1645
| result to L_var3 with saturation, return a 32 bit result:
1646
| L_msu0(L_var3,var1,var2) = L_sub(L_var3,(L_mult0(var1,var2)).
1648
| Complexity weight : 1
1652
| L_var3 32 bit long signed integer (Word32) whose value falls in the
1653
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
1655
| var1 16 bit short signed integer (Word16) whose value falls in the
1656
| range : 0xffff 8000 <= var1 <= 0x0000 7fff.
1658
| var2 16 bit short signed integer (Word16) whose value falls in the
1659
| range : 0xffff 8000 <= var1 <= 0x0000 7fff.
1664
| 32 bit long signed integer (Word32) whose value falls in the
1665
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
1666
|___________________________________________________________________________
1668
LIBG7221_DEF(Word32) L_msu0 (Word32 L_var3, Word16 var1, Word16 var2)
1673
L_product = L_mult0(var1,var2);
1674
L_var_out = L_sub(L_var3,L_product);
1677
multiCounter[currCounter].L_msu0++;
1678
multiCounter[currCounter].L_mult0--;
1679
multiCounter[currCounter].L_sub--;
1683
/* ------------------------- End of L_msu0() ------------------------- */
1686
/*___________________________________________________________________________
1688
| Function Name : LU_shl |
1692
| Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero |
1693
| fill the var2 LSB of the result. If var2 is negative, arithmetically |
1694
| shift L_var1 right by -var2 with sign extension. Saturate the result in |
1695
| case of underflows or overflows. |
1697
| Complexity weight : 2 |
1701
| L_var1 32 bit long signed integer (Word32) whose value falls in the |
1702
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1705
| 16 bit short signed integer (Word16) whose value falls in the |
1706
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1715
| 32 bit long signed integer (Word32) whose value falls in the |
1716
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1717
|___________________________________________________________________________|
1719
LIBG7221_DEF(UWord32) LU_shl (UWord32 L_var1, Word16 var2)
1722
UWord32 L_var_out = 0;
1728
neg_var2 = negate(var2);
1729
L_var_out = LU_shr (L_var1, neg_var2);
1731
multiCounter[currCounter].negate--;
1732
multiCounter[currCounter].LU_shr--;
1737
for (; var2 > 0; var2--)
1739
if (L_var1 > (UWord32) 0X7fffffffL)
1742
L_var_out = UMAX_32;
1747
if (L_var1 < (UWord32) 0x00000001L)
1750
L_var_out = (UWord32)MIN_32;
1759
multiCounter[currCounter].LU_shl++;
1763
/* ------------------------- End of LU_shl() ------------------------- */
1766
/*___________________________________________________________________________
1768
| Function Name : LU_shr |
1772
| Arithmetically shift the 32 bit input L_var1 right var2 positions with |
1773
| sign extension. If var2 is negative, arithmetically shift L_var1 left |
1774
| by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
1775
| in case of underflows or overflows. |
1777
| Complexity weight : 2 |
1781
| L_var1 32 bit long signed integer (Word32) whose value falls in the |
1782
| range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1785
| 16 bit short signed integer (Word16) whose value falls in the |
1786
| range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1795
| 32 bit long signed integer (Word32) whose value falls in the |
1796
| range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1797
|___________________________________________________________________________|
1799
LIBG7221_DEF(UWord32) LU_shr (UWord32 L_var1, Word16 var2)
1808
neg_var2 = negate(var2);
1809
L_var_out = LU_shl (L_var1, neg_var2);
1811
multiCounter[currCounter].negate--;
1812
multiCounter[currCounter].LU_shl--;
1823
L_var_out = L_var1 >> var2;
1827
multiCounter[currCounter].LU_shr++;
1831
/* ------------------------- End of LU_shr() ------------------------- */
1833
#endif /* PJMEDIA_LIBG7221_FUNCS_INLINED */
1835
/* ************************** END OF BASOP32.C ************************** */