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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30
31 Filename: /audio/gsm_amr/c/src/round.c
32
33------------------------------------------------------------------------------
34 REVISION HISTORY
35
36 Description: Created separate file for the round function. Sync'ed up
37          with the current template and fixed tabs.
38
39 Description: Made changes based on review meeting:
40              1. Removed long in Inputs Definitions.
41              2. Changed L_var1 to var_out and description in
42                 Output Definitions.
43
44 Description: Added a parameter to the function interface, pOverflow which is
45              a pointer to the overflow flag. This flag is required by the
46              L_add() function invoked by round().
47              Removed code that updates the MOPS counter.
48              Created a new return variable result.
49
50 Description: Removed embedded tabs. Included comment in the Pseudo code
51              Section about using pointer to overflow flag in the actual
52              implementation instead of using a global flag.
53
54 Description: Changed function name to pv_round to avoid conflict with
55              round function in C standard library.
56
57 Who:                       Date:
58 Description:
59
60------------------------------------------------------------------------------
61 MODULE DESCRIPTION
62
63 Rounding function with saturation.
64
65------------------------------------------------------------------------------
66*/
67
68/*----------------------------------------------------------------------------
69; INCLUDES
70----------------------------------------------------------------------------*/
71#include    "basic_op.h"
72
73/*----------------------------------------------------------------------------
74; MACROS
75; [Define module specific macros here]
76----------------------------------------------------------------------------*/
77
78/*----------------------------------------------------------------------------
79; DEFINES
80; [Include all pre-processor statements here. Include conditional
81; compile variables also.]
82----------------------------------------------------------------------------*/
83
84/*----------------------------------------------------------------------------
85; LOCAL FUNCTION DEFINITIONS
86; [List function prototypes here]
87----------------------------------------------------------------------------*/
88
89/*----------------------------------------------------------------------------
90; LOCAL VARIABLE DEFINITIONS
91; [Variable declaration - defined here and used outside this module]
92----------------------------------------------------------------------------*/
93
94/*
95------------------------------------------------------------------------------
96 FUNCTION NAME: pv_round
97------------------------------------------------------------------------------
98 INPUT AND OUTPUT DEFINITIONS
99
100 Inputs:
101    L_var1 = 32 bit signed integer (Word32) whose value falls
102             in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
103
104    pOverflow = pointer to overflow (Flag)
105
106 Outputs:
107    None
108
109 Returns:
110        result = MS 16 bits of rounded input L_var1.
111
112 Global Variables Used:
113    None
114
115 Local Variables Needed:
116    None
117
118------------------------------------------------------------------------------
119 FUNCTION DESCRIPTION
120
121 This function rounds the lower 16 bits of the 32 bit input number into the
122 MS 16 bits with saturation. Shift the resulting bits right by 16 and return
123 the 16 bit number:
124    pv_round(L_var1) = extract_h(L_add(L_var1,32768))
125
126------------------------------------------------------------------------------
127 REQUIREMENTS
128
129 None
130
131------------------------------------------------------------------------------
132 REFERENCES
133
134 [1] round() function in basic_op2.c,  UMTS GSM AMR speech codec, R99 -
135 Version 3.2.0, March 2, 2001
136
137------------------------------------------------------------------------------
138 PSEUDO-CODE
139
140Word16 pv_round (Word32 L_var1)
141{
142    Word16 var_out;
143    Word32 L_rounded;
144
145* The reference ETSI code uses a global flag for Overflow in the L_add() function.
146* In the actual implementation a pointer to Overflow flag is passed in as a
147* parameter to the function.
148
149    L_rounded = L_add (L_var1, (Word32) 0x00008000L);
150#if (WMOPS)
151    multiCounter[currCounter].L_add--;
152#endif
153    var_out = extract_h (L_rounded);
154#if (WMOPS)
155    multiCounter[currCounter].extract_h--;
156    multiCounter[currCounter].round++;
157#endif
158    return (var_out);
159}
160
161------------------------------------------------------------------------------
162 RESOURCES USED [optional]
163
164 When the code is written for a specific target processor the
165 the resources used should be documented below.
166
167 HEAP MEMORY USED: x bytes
168
169 STACK MEMORY USED: x bytes
170
171 CLOCK CYCLES: (cycle count equation for this function) + (variable
172                used to represent cycle count for each subroutine
173                called)
174     where: (cycle count variable) = cycle count for [subroutine
175                                     name]
176
177------------------------------------------------------------------------------
178 CAUTION [optional]
179 [State any special notes, constraints or cautions for users of this function]
180
181------------------------------------------------------------------------------
182*/
183
184/*----------------------------------------------------------------------------
185; FUNCTION CODE
186----------------------------------------------------------------------------*/
187Word16 pv_round(register Word32 L_var1, Flag *pOverflow)
188{
189    Word16  result;
190
191    L_var1 = L_add(L_var1, (Word32) 0x00008000L, pOverflow);
192    result = (Word16)(L_var1 >> 16);
193
194    return (result);
195}
196