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 | |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 11 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | /* |
| 13 | * This file contains the function WebRtcSpl_Sqrt(). |
| 14 | * The description header can be found in signal_processing_library.h |
| 15 | * |
| 16 | */ |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 20 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 21 | int32_t WebRtcSpl_SqrtLocal(int32_t in); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 22 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 23 | int32_t WebRtcSpl_SqrtLocal(int32_t in) |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 24 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 25 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 26 | int16_t x_half, t16; |
| 27 | int32_t A, B, x2; |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 28 | |
| 29 | /* The following block performs: |
| 30 | y=in/2 |
| 31 | x=y-2^30 |
| 32 | x_half=x/2^31 |
| 33 | t = 1 + (x_half) - 0.5*((x_half)^2) + 0.5*((x_half)^3) - 0.625*((x_half)^4) |
| 34 | + 0.875*((x_half)^5) |
| 35 | */ |
| 36 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 37 | B = in / 2; |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 38 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 39 | B = B - ((int32_t)0x40000000); // B = in/2 - 1/2 |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 40 | x_half = (int16_t)(B >> 16); // x_half = x/2 = (in-1)/2 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 41 | B = B + ((int32_t)0x40000000); // B = 1 + x/2 |
| 42 | B = B + ((int32_t)0x40000000); // Add 0.5 twice (since 1.0 does not exist in Q31) |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 43 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 44 | x2 = ((int32_t)x_half) * ((int32_t)x_half) * 2; // A = (x/2)^2 |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 45 | A = -x2; // A = -(x/2)^2 |
| 46 | B = B + (A >> 1); // B = 1 + x/2 - 0.5*(x/2)^2 |
| 47 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 48 | A >>= 16; |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 49 | A = A * A * 2; // A = (x/2)^4 |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 50 | t16 = (int16_t)(A >> 16); |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 51 | B += -20480 * t16 * 2; // B = B - 0.625*A |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 52 | // After this, B = 1 + x/2 - 0.5*(x/2)^2 - 0.625*(x/2)^4 |
| 53 | |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 54 | A = x_half * t16 * 2; // A = (x/2)^5 |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 55 | t16 = (int16_t)(A >> 16); |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 56 | B += 28672 * t16 * 2; // B = B + 0.875*A |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 57 | // After this, B = 1 + x/2 - 0.5*(x/2)^2 - 0.625*(x/2)^4 + 0.875*(x/2)^5 |
| 58 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 59 | t16 = (int16_t)(x2 >> 16); |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 60 | A = x_half * t16 * 2; // A = x/2^3 |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 61 | |
| 62 | B = B + (A >> 1); // B = B + 0.5*A |
| 63 | // After this, B = 1 + x/2 - 0.5*(x/2)^2 + 0.5*(x/2)^3 - 0.625*(x/2)^4 + 0.875*(x/2)^5 |
| 64 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 65 | B = B + ((int32_t)32768); // Round off bit |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 66 | |
| 67 | return B; |
| 68 | } |
| 69 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 70 | int32_t WebRtcSpl_Sqrt(int32_t value) |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 71 | { |
| 72 | /* |
| 73 | Algorithm: |
| 74 | |
| 75 | Six term Taylor Series is used here to compute the square root of a number |
| 76 | y^0.5 = (1+x)^0.5 where x = y-1 |
| 77 | = 1+(x/2)-0.5*((x/2)^2+0.5*((x/2)^3-0.625*((x/2)^4+0.875*((x/2)^5) |
| 78 | 0.5 <= x < 1 |
| 79 | |
| 80 | Example of how the algorithm works, with ut=sqrt(in), and |
| 81 | with in=73632 and ut=271 (even shift value case): |
| 82 | |
| 83 | in=73632 |
| 84 | y= in/131072 |
| 85 | x=y-1 |
| 86 | t = 1 + (x/2) - 0.5*((x/2)^2) + 0.5*((x/2)^3) - 0.625*((x/2)^4) + 0.875*((x/2)^5) |
| 87 | ut=t*(1/sqrt(2))*512 |
| 88 | |
| 89 | or: |
| 90 | |
| 91 | in=73632 |
| 92 | in2=73632*2^14 |
| 93 | y= in2/2^31 |
| 94 | x=y-1 |
| 95 | t = 1 + (x/2) - 0.5*((x/2)^2) + 0.5*((x/2)^3) - 0.625*((x/2)^4) + 0.875*((x/2)^5) |
| 96 | ut=t*(1/sqrt(2)) |
| 97 | ut2=ut*2^9 |
| 98 | |
| 99 | which gives: |
| 100 | |
| 101 | in = 73632 |
| 102 | in2 = 1206386688 |
| 103 | y = 0.56176757812500 |
| 104 | x = -0.43823242187500 |
| 105 | t = 0.74973506527313 |
| 106 | ut = 0.53014274874797 |
| 107 | ut2 = 2.714330873589594e+002 |
| 108 | |
| 109 | or: |
| 110 | |
| 111 | in=73632 |
| 112 | in2=73632*2^14 |
| 113 | y=in2/2 |
| 114 | x=y-2^30 |
| 115 | x_half=x/2^31 |
| 116 | t = 1 + (x_half) - 0.5*((x_half)^2) + 0.5*((x_half)^3) - 0.625*((x_half)^4) |
| 117 | + 0.875*((x_half)^5) |
| 118 | ut=t*(1/sqrt(2)) |
| 119 | ut2=ut*2^9 |
| 120 | |
| 121 | which gives: |
| 122 | |
| 123 | in = 73632 |
| 124 | in2 = 1206386688 |
| 125 | y = 603193344 |
| 126 | x = -470548480 |
| 127 | x_half = -0.21911621093750 |
| 128 | t = 0.74973506527313 |
| 129 | ut = 0.53014274874797 |
| 130 | ut2 = 2.714330873589594e+002 |
| 131 | |
| 132 | */ |
| 133 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 134 | int16_t x_norm, nshift, t16, sh; |
| 135 | int32_t A; |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 136 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 137 | int16_t k_sqrt_2 = 23170; // 1/sqrt2 (==5a82) |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 138 | |
| 139 | A = value; |
| 140 | |
henrik.lundin | 71e92dc | 2016-02-15 20:38:08 | [diff] [blame] | 141 | // The convention in this function is to calculate sqrt(abs(A)). Negate the |
| 142 | // input if it is negative. |
| 143 | if (A < 0) { |
| 144 | if (A == WEBRTC_SPL_WORD32_MIN) { |
| 145 | // This number cannot be held in an int32_t after negating. |
| 146 | // Map it to the maximum positive value. |
| 147 | A = WEBRTC_SPL_WORD32_MAX; |
| 148 | } else { |
| 149 | A = -A; |
| 150 | } |
| 151 | } else if (A == 0) { |
| 152 | return 0; // sqrt(0) = 0 |
| 153 | } |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 154 | |
| 155 | sh = WebRtcSpl_NormW32(A); // # shifts to normalize A |
| 156 | A = WEBRTC_SPL_LSHIFT_W32(A, sh); // Normalize A |
| 157 | if (A < (WEBRTC_SPL_WORD32_MAX - 32767)) |
| 158 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 159 | A = A + ((int32_t)32768); // Round off bit |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 160 | } else |
| 161 | { |
| 162 | A = WEBRTC_SPL_WORD32_MAX; |
| 163 | } |
| 164 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 165 | x_norm = (int16_t)(A >> 16); // x_norm = AH |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 166 | |
bjornv@webrtc.org | d4fe824 | 2014-10-13 13:01:13 | [diff] [blame] | 167 | nshift = (sh / 2); |
kwiberg | 1e8ed4a | 2016-08-26 11:33:34 | [diff] [blame] | 168 | RTC_DCHECK_GE(nshift, 0); |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 169 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 170 | A = (int32_t)WEBRTC_SPL_LSHIFT_W32((int32_t)x_norm, 16); |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 171 | A = WEBRTC_SPL_ABS_W32(A); // A = abs(x_norm<<16) |
| 172 | A = WebRtcSpl_SqrtLocal(A); // A = sqrt(A) |
| 173 | |
bjornv@webrtc.org | d4fe824 | 2014-10-13 13:01:13 | [diff] [blame] | 174 | if (2 * nshift == sh) { |
| 175 | // Even shift value case |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 176 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 177 | t16 = (int16_t)(A >> 16); // t16 = AH |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 178 | |
Bjorn Volcker | affcfb2 | 2015-04-24 06:12:07 | [diff] [blame] | 179 | A = k_sqrt_2 * t16 * 2; // A = 1/sqrt(2)*t16 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 180 | A = A + ((int32_t)32768); // Round off |
| 181 | A = A & ((int32_t)0x7fff0000); // Round off |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 182 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 183 | A >>= 15; // A = A>>16 |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 184 | |
| 185 | } else |
| 186 | { |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 | [diff] [blame] | 187 | A >>= 16; // A = A>>16 |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 188 | } |
| 189 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 | [diff] [blame] | 190 | A = A & ((int32_t)0x0000ffff); |
bjornv@webrtc.org | d4fe824 | 2014-10-13 13:01:13 | [diff] [blame] | 191 | A >>= nshift; // De-normalize the result. |
niklase@google.com | f50cf1f | 2011-07-07 08:33:00 | [diff] [blame] | 192 | |
| 193 | return A; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 194 | } |