Tizen 2.0 Release
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / round.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30
31  Filename: round.cpp
32
33 ------------------------------------------------------------------------------
34  MODULE DESCRIPTION
35
36  Rounding function with saturation.
37
38 ------------------------------------------------------------------------------
39 */
40
41 /*----------------------------------------------------------------------------
42 ; INCLUDES
43 ----------------------------------------------------------------------------*/
44 #include    "basic_op.h"
45
46 /*----------------------------------------------------------------------------
47 ; MACROS
48 ; [Define module specific macros here]
49 ----------------------------------------------------------------------------*/
50
51 /*----------------------------------------------------------------------------
52 ; DEFINES
53 ; [Include all pre-processor statements here. Include conditional
54 ; compile variables also.]
55 ----------------------------------------------------------------------------*/
56
57 /*----------------------------------------------------------------------------
58 ; LOCAL FUNCTION DEFINITIONS
59 ; [List function prototypes here]
60 ----------------------------------------------------------------------------*/
61
62 /*----------------------------------------------------------------------------
63 ; LOCAL VARIABLE DEFINITIONS
64 ; [Variable declaration - defined here and used outside this module]
65 ----------------------------------------------------------------------------*/
66
67 /*
68 ------------------------------------------------------------------------------
69  FUNCTION NAME: pv_round
70 ------------------------------------------------------------------------------
71  INPUT AND OUTPUT DEFINITIONS
72
73  Inputs:
74     L_var1 = 32 bit signed integer (Word32) whose value falls
75              in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
76
77     pOverflow = pointer to overflow (Flag)
78
79  Outputs:
80     None
81
82  Returns:
83         result = MS 16 bits of rounded input L_var1.
84
85  Global Variables Used:
86     None
87
88  Local Variables Needed:
89     None
90
91 ------------------------------------------------------------------------------
92  FUNCTION DESCRIPTION
93
94  This function rounds the lower 16 bits of the 32 bit input number into the
95  MS 16 bits with saturation. Shift the resulting bits right by 16 and return
96  the 16 bit number:
97     pv_round(L_var1) = extract_h(L_add(L_var1,32768))
98
99 ------------------------------------------------------------------------------
100  REQUIREMENTS
101
102  None
103
104 ------------------------------------------------------------------------------
105  REFERENCES
106
107  [1] round() function in basic_op2.c,  UMTS GSM AMR speech codec, R99 -
108  Version 3.2.0, March 2, 2001
109
110 ------------------------------------------------------------------------------
111  PSEUDO-CODE
112
113 Word16 pv_round (Word32 L_var1)
114 {
115     Word16 var_out;
116     Word32 L_rounded;
117
118 * The reference ETSI code uses a global flag for Overflow in the L_add() function.
119 * In the actual implementation a pointer to Overflow flag is passed in as a
120 * parameter to the function.
121
122     L_rounded = L_add (L_var1, (Word32) 0x00008000L);
123 #if (WMOPS)
124     multiCounter[currCounter].L_add--;
125 #endif
126     var_out = extract_h (L_rounded);
127 #if (WMOPS)
128     multiCounter[currCounter].extract_h--;
129     multiCounter[currCounter].round++;
130 #endif
131     return (var_out);
132 }
133
134 ------------------------------------------------------------------------------
135  CAUTION [optional]
136  [State any special notes, constraints or cautions for users of this function]
137
138 ------------------------------------------------------------------------------
139 */
140
141 /*----------------------------------------------------------------------------
142 ; FUNCTION CODE
143 ----------------------------------------------------------------------------*/
144 OSCL_EXPORT_REF Word16 pv_round(register Word32 L_var1, Flag *pOverflow)
145 {
146     Word16  result;
147
148     L_var1 = L_add(L_var1, (Word32) 0x00008000L, pOverflow);
149     result = (Word16)(L_var1 >> 16);
150
151     return (result);
152 }