niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 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 divisions |
| 14 | * WebRtcSpl_DivU32U16() |
| 15 | * WebRtcSpl_DivW32W16() |
| 16 | * WebRtcSpl_DivW32W16ResW16() |
| 17 | * WebRtcSpl_DivResultInQ31() |
| 18 | * WebRtcSpl_DivW32HiLow() |
| 19 | * |
| 20 | * The description header can be found in signal_processing_library.h |
| 21 | * |
| 22 | */ |
| 23 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 25 | #include "rtc_base/sanitizer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 26 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 27 | uint32_t WebRtcSpl_DivU32U16(uint32_t num, uint16_t den) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 28 | { |
| 29 | // Guard against division with 0 |
| 30 | if (den != 0) |
| 31 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 32 | return (uint32_t)(num / den); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 33 | } else |
| 34 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 35 | return (uint32_t)0xFFFFFFFF; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 39 | int32_t WebRtcSpl_DivW32W16(int32_t num, int16_t den) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 40 | { |
| 41 | // Guard against division with 0 |
| 42 | if (den != 0) |
| 43 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 44 | return (int32_t)(num / den); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 45 | } else |
| 46 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 47 | return (int32_t)0x7FFFFFFF; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 51 | int16_t WebRtcSpl_DivW32W16ResW16(int32_t num, int16_t den) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 52 | { |
| 53 | // Guard against division with 0 |
| 54 | if (den != 0) |
| 55 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 56 | return (int16_t)(num / den); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 57 | } else |
| 58 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 59 | return (int16_t)0x7FFF; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 63 | int32_t WebRtcSpl_DivResultInQ31(int32_t num, int32_t den) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 64 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 65 | int32_t L_num = num; |
| 66 | int32_t L_den = den; |
| 67 | int32_t div = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 68 | int k = 31; |
| 69 | int change_sign = 0; |
| 70 | |
| 71 | if (num == 0) |
| 72 | return 0; |
| 73 | |
| 74 | if (num < 0) |
| 75 | { |
| 76 | change_sign++; |
| 77 | L_num = -num; |
| 78 | } |
| 79 | if (den < 0) |
| 80 | { |
| 81 | change_sign++; |
| 82 | L_den = -den; |
| 83 | } |
| 84 | while (k--) |
| 85 | { |
| 86 | div <<= 1; |
| 87 | L_num <<= 1; |
| 88 | if (L_num >= L_den) |
| 89 | { |
| 90 | L_num -= L_den; |
| 91 | div++; |
| 92 | } |
| 93 | } |
| 94 | if (change_sign == 1) |
| 95 | { |
| 96 | div = -div; |
| 97 | } |
| 98 | return div; |
| 99 | } |
| 100 | |
Minyue Li | 5663ce9 | 2021-04-22 22:58:24 | [diff] [blame] | 101 | int32_t WebRtcSpl_DivW32HiLow(int32_t num, int16_t den_hi, int16_t den_low) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 102 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 103 | int16_t approx, tmp_hi, tmp_low, num_hi, num_low; |
| 104 | int32_t tmpW32; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 105 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 106 | approx = (int16_t)WebRtcSpl_DivW32W16((int32_t)0x1FFFFFFF, den_hi); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 107 | // result in Q14 (Note: 3FFFFFFF = 0.5 in Q30) |
| 108 | |
| 109 | // tmpW32 = 1/den = approx * (2.0 - den * approx) (in Q30) |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 110 | tmpW32 = (den_hi * approx << 1) + ((den_low * approx >> 15) << 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 111 | // tmpW32 = den * approx |
| 112 | |
Minyue Li | 5663ce9 | 2021-04-22 22:58:24 | [diff] [blame] | 113 | // result in Q30 (tmpW32 = 2.0-(den*approx)) |
| 114 | tmpW32 = (int32_t)((int64_t)0x7fffffffL - tmpW32); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 115 | |
| 116 | // Store tmpW32 in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 117 | tmp_hi = (int16_t)(tmpW32 >> 16); |
| 118 | tmp_low = (int16_t)((tmpW32 - ((int32_t)tmp_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 119 | |
| 120 | // tmpW32 = 1/den in Q29 |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 121 | tmpW32 = (tmp_hi * approx + (tmp_low * approx >> 15)) << 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 122 | |
| 123 | // 1/den in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 124 | tmp_hi = (int16_t)(tmpW32 >> 16); |
| 125 | tmp_low = (int16_t)((tmpW32 - ((int32_t)tmp_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 126 | |
| 127 | // Store num in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 128 | num_hi = (int16_t)(num >> 16); |
| 129 | num_low = (int16_t)((num - ((int32_t)num_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 130 | |
| 131 | // num * (1/den) by 32 bit multiplication (result in Q28) |
| 132 | |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 133 | tmpW32 = num_hi * tmp_hi + (num_hi * tmp_low >> 15) + |
| 134 | (num_low * tmp_hi >> 15); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 135 | |
| 136 | // Put result in Q31 (convert from Q28) |
| 137 | tmpW32 = WEBRTC_SPL_LSHIFT_W32(tmpW32, 3); |
| 138 | |
| 139 | return tmpW32; |
| 140 | } |