1/*
2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12/*
13 * This file contains implementations of the functions
14 * WebRtcSpl_VectorBitShiftW16()
15 * WebRtcSpl_VectorBitShiftW32()
16 * WebRtcSpl_VectorBitShiftW32ToW16()
17 * WebRtcSpl_ScaleVector()
18 * WebRtcSpl_ScaleVectorWithSat()
19 * WebRtcSpl_ScaleAndAddVectors()
20 *
21 * The description header can be found in signal_processing_library.h
22 *
23 */
24
25#include "signal_processing_library.h"
26
27void WebRtcSpl_VectorBitShiftW16(WebRtc_Word16 *res,
28                             WebRtc_Word16 length,
29                             G_CONST WebRtc_Word16 *in,
30                             WebRtc_Word16 right_shifts)
31{
32    int i;
33
34    if (right_shifts > 0)
35    {
36        for (i = length; i > 0; i--)
37        {
38            (*res++) = ((*in++) >> right_shifts);
39        }
40    } else
41    {
42        for (i = length; i > 0; i--)
43        {
44            (*res++) = ((*in++) << (-right_shifts));
45        }
46    }
47}
48
49void WebRtcSpl_VectorBitShiftW32(WebRtc_Word32 *out_vector,
50                             WebRtc_Word16 vector_length,
51                             G_CONST WebRtc_Word32 *in_vector,
52                             WebRtc_Word16 right_shifts)
53{
54    int i;
55
56    if (right_shifts > 0)
57    {
58        for (i = vector_length; i > 0; i--)
59        {
60            (*out_vector++) = ((*in_vector++) >> right_shifts);
61        }
62    } else
63    {
64        for (i = vector_length; i > 0; i--)
65        {
66            (*out_vector++) = ((*in_vector++) << (-right_shifts));
67        }
68    }
69}
70
71void WebRtcSpl_VectorBitShiftW32ToW16(WebRtc_Word16 *res,
72                                  WebRtc_Word16 length,
73                                  G_CONST WebRtc_Word32 *in,
74                                  WebRtc_Word16 right_shifts)
75{
76    int i;
77
78    if (right_shifts >= 0)
79    {
80        for (i = length; i > 0; i--)
81        {
82            (*res++) = (WebRtc_Word16)((*in++) >> right_shifts);
83        }
84    } else
85    {
86        WebRtc_Word16 left_shifts = -right_shifts;
87        for (i = length; i > 0; i--)
88        {
89            (*res++) = (WebRtc_Word16)((*in++) << left_shifts);
90        }
91    }
92}
93
94void WebRtcSpl_ScaleVector(G_CONST WebRtc_Word16 *in_vector, WebRtc_Word16 *out_vector,
95                           WebRtc_Word16 gain, WebRtc_Word16 in_vector_length,
96                           WebRtc_Word16 right_shifts)
97{
98    // Performs vector operation: out_vector = (gain*in_vector)>>right_shifts
99    int i;
100    G_CONST WebRtc_Word16 *inptr;
101    WebRtc_Word16 *outptr;
102
103    inptr = in_vector;
104    outptr = out_vector;
105
106    for (i = 0; i < in_vector_length; i++)
107    {
108        (*outptr++) = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(*inptr++, gain, right_shifts);
109    }
110}
111
112void WebRtcSpl_ScaleVectorWithSat(G_CONST WebRtc_Word16 *in_vector, WebRtc_Word16 *out_vector,
113                                 WebRtc_Word16 gain, WebRtc_Word16 in_vector_length,
114                                 WebRtc_Word16 right_shifts)
115{
116    // Performs vector operation: out_vector = (gain*in_vector)>>right_shifts
117    int i;
118    WebRtc_Word32 tmpW32;
119    G_CONST WebRtc_Word16 *inptr;
120    WebRtc_Word16 *outptr;
121
122    inptr = in_vector;
123    outptr = out_vector;
124
125    for (i = 0; i < in_vector_length; i++)
126    {
127        tmpW32 = WEBRTC_SPL_MUL_16_16_RSFT(*inptr++, gain, right_shifts);
128        (*outptr++) = WebRtcSpl_SatW32ToW16(tmpW32);
129    }
130}
131
132void WebRtcSpl_ScaleAndAddVectors(G_CONST WebRtc_Word16 *in1, WebRtc_Word16 gain1, int shift1,
133                                  G_CONST WebRtc_Word16 *in2, WebRtc_Word16 gain2, int shift2,
134                                  WebRtc_Word16 *out, int vector_length)
135{
136    // Performs vector operation: out = (gain1*in1)>>shift1 + (gain2*in2)>>shift2
137    int i;
138    G_CONST WebRtc_Word16 *in1ptr;
139    G_CONST WebRtc_Word16 *in2ptr;
140    WebRtc_Word16 *outptr;
141
142    in1ptr = in1;
143    in2ptr = in2;
144    outptr = out;
145
146    for (i = 0; i < vector_length; i++)
147    {
148        (*outptr++) = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(gain1, *in1ptr++, shift1)
149                + (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(gain2, *in2ptr++, shift2);
150    }
151}
152