2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
13 * This file contains implementations of the divisions
14 * WebRtcSpl_DivU32U16()
15 * WebRtcSpl_DivW32W16()
16 * WebRtcSpl_DivW32W16ResW16()
17 * WebRtcSpl_DivResultInQ31()
18 * WebRtcSpl_DivW32HiLow()
20 * The description header can be found in signal_processing_library.h
24 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
26 uint32_t WebRtcSpl_DivU32U16(uint32_t num, uint16_t den)
28 // Guard against division with 0
31 return (uint32_t)(num / den);
34 return (uint32_t)0xFFFFFFFF;
38 int32_t WebRtcSpl_DivW32W16(int32_t num, int16_t den)
40 // Guard against division with 0
43 return (int32_t)(num / den);
46 return (int32_t)0x7FFFFFFF;
50 int16_t WebRtcSpl_DivW32W16ResW16(int32_t num, int16_t den)
52 // Guard against division with 0
55 return (int16_t)(num / den);
58 return (int16_t)0x7FFF;
62 int32_t WebRtcSpl_DivResultInQ31(int32_t num, int32_t den)
100 int32_t WebRtcSpl_DivW32HiLow(int32_t num, int16_t den_hi, int16_t den_low)
102 int16_t approx, tmp_hi, tmp_low, num_hi, num_low;
105 approx = (int16_t)WebRtcSpl_DivW32W16((int32_t)0x1FFFFFFF, den_hi);
106 // result in Q14 (Note: 3FFFFFFF = 0.5 in Q30)
108 // tmpW32 = 1/den = approx * (2.0 - den * approx) (in Q30)
109 tmpW32 = (WEBRTC_SPL_MUL_16_16(den_hi, approx) << 1)
110 + ((WEBRTC_SPL_MUL_16_16(den_low, approx) >> 15) << 1);
111 // tmpW32 = den * approx
113 tmpW32 = (int32_t)0x7fffffffL - tmpW32; // result in Q30 (tmpW32 = 2.0-(den*approx))
115 // Store tmpW32 in hi and low format
116 tmp_hi = (int16_t)(tmpW32 >> 16);
117 tmp_low = (int16_t)((tmpW32 - ((int32_t)tmp_hi << 16)) >> 1);
119 // tmpW32 = 1/den in Q29
120 tmpW32 = ((WEBRTC_SPL_MUL_16_16(tmp_hi, approx) + (WEBRTC_SPL_MUL_16_16(tmp_low, approx)
123 // 1/den in hi and low format
124 tmp_hi = (int16_t)(tmpW32 >> 16);
125 tmp_low = (int16_t)((tmpW32 - ((int32_t)tmp_hi << 16)) >> 1);
127 // Store num in hi and low format
128 num_hi = (int16_t)(num >> 16);
129 num_low = (int16_t)((num - ((int32_t)num_hi << 16)) >> 1);
131 // num * (1/den) by 32 bit multiplication (result in Q28)
133 tmpW32 = (WEBRTC_SPL_MUL_16_16(num_hi, tmp_hi) + (WEBRTC_SPL_MUL_16_16(num_hi, tmp_low)
134 >> 15) + (WEBRTC_SPL_MUL_16_16(num_low, tmp_hi) >> 15));
136 // Put result in Q31 (convert from Q28)
137 tmpW32 = WEBRTC_SPL_LSHIFT_W32(tmpW32, 3);