blob: fff73c03a2e95aa7e9d27ed334322d55426aab90 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.comf50cf1f2011-07-07 08:33:00 +000011
niklase@google.com470e71d2011-07-07 08:21:25 +000012/*
13 * This file contains the function WebRtcSpl_Sqrt().
14 * The description header can be found in signal_processing_library.h
15 *
16 */
17
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000018#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
bjornv@webrtc.orgd4fe8242014-10-13 13:01:13 +000020#include <assert.h>
21
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000022int32_t WebRtcSpl_SqrtLocal(int32_t in);
niklase@google.com470e71d2011-07-07 08:21:25 +000023
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000024int32_t WebRtcSpl_SqrtLocal(int32_t in)
niklase@google.comf50cf1f2011-07-07 08:33:00 +000025{
niklase@google.com470e71d2011-07-07 08:21:25 +000026
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000027 int16_t x_half, t16;
28 int32_t A, B, x2;
niklase@google.comf50cf1f2011-07-07 08:33:00 +000029
30 /* The following block performs:
31 y=in/2
32 x=y-2^30
33 x_half=x/2^31
34 t = 1 + (x_half) - 0.5*((x_half)^2) + 0.5*((x_half)^3) - 0.625*((x_half)^4)
35 + 0.875*((x_half)^5)
36 */
37
38 B = in;
39
40 B = WEBRTC_SPL_RSHIFT_W32(B, 1); // B = in/2
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000041 B = B - ((int32_t)0x40000000); // B = in/2 - 1/2
42 x_half = (int16_t)WEBRTC_SPL_RSHIFT_W32(B, 16);// x_half = x/2 = (in-1)/2
43 B = B + ((int32_t)0x40000000); // B = 1 + x/2
44 B = B + ((int32_t)0x40000000); // Add 0.5 twice (since 1.0 does not exist in Q31)
niklase@google.comf50cf1f2011-07-07 08:33:00 +000045
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000046 x2 = ((int32_t)x_half) * ((int32_t)x_half) * 2; // A = (x/2)^2
niklase@google.comf50cf1f2011-07-07 08:33:00 +000047 A = -x2; // A = -(x/2)^2
48 B = B + (A >> 1); // B = 1 + x/2 - 0.5*(x/2)^2
49
50 A = WEBRTC_SPL_RSHIFT_W32(A, 16);
51 A = A * A * 2; // A = (x/2)^4
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000052 t16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(A, 16);
niklase@google.comf50cf1f2011-07-07 08:33:00 +000053 B = B + WEBRTC_SPL_MUL_16_16(-20480, t16) * 2; // B = B - 0.625*A
54 // After this, B = 1 + x/2 - 0.5*(x/2)^2 - 0.625*(x/2)^4
55
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000056 t16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(A, 16);
niklase@google.comf50cf1f2011-07-07 08:33:00 +000057 A = WEBRTC_SPL_MUL_16_16(x_half, t16) * 2; // A = (x/2)^5
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000058 t16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(A, 16);
niklase@google.comf50cf1f2011-07-07 08:33:00 +000059 B = B + WEBRTC_SPL_MUL_16_16(28672, t16) * 2; // B = B + 0.875*A
60 // After this, B = 1 + x/2 - 0.5*(x/2)^2 - 0.625*(x/2)^4 + 0.875*(x/2)^5
61
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000062 t16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(x2, 16);
niklase@google.comf50cf1f2011-07-07 08:33:00 +000063 A = WEBRTC_SPL_MUL_16_16(x_half, t16) * 2; // A = x/2^3
64
65 B = B + (A >> 1); // B = B + 0.5*A
66 // 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
67
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000068 B = B + ((int32_t)32768); // Round off bit
niklase@google.comf50cf1f2011-07-07 08:33:00 +000069
70 return B;
71}
72
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000073int32_t WebRtcSpl_Sqrt(int32_t value)
niklase@google.comf50cf1f2011-07-07 08:33:00 +000074{
75 /*
76 Algorithm:
77
78 Six term Taylor Series is used here to compute the square root of a number
79 y^0.5 = (1+x)^0.5 where x = y-1
80 = 1+(x/2)-0.5*((x/2)^2+0.5*((x/2)^3-0.625*((x/2)^4+0.875*((x/2)^5)
81 0.5 <= x < 1
82
83 Example of how the algorithm works, with ut=sqrt(in), and
84 with in=73632 and ut=271 (even shift value case):
85
86 in=73632
87 y= in/131072
88 x=y-1
89 t = 1 + (x/2) - 0.5*((x/2)^2) + 0.5*((x/2)^3) - 0.625*((x/2)^4) + 0.875*((x/2)^5)
90 ut=t*(1/sqrt(2))*512
91
92 or:
93
94 in=73632
95 in2=73632*2^14
96 y= in2/2^31
97 x=y-1
98 t = 1 + (x/2) - 0.5*((x/2)^2) + 0.5*((x/2)^3) - 0.625*((x/2)^4) + 0.875*((x/2)^5)
99 ut=t*(1/sqrt(2))
100 ut2=ut*2^9
101
102 which gives:
103
104 in = 73632
105 in2 = 1206386688
106 y = 0.56176757812500
107 x = -0.43823242187500
108 t = 0.74973506527313
109 ut = 0.53014274874797
110 ut2 = 2.714330873589594e+002
111
112 or:
113
114 in=73632
115 in2=73632*2^14
116 y=in2/2
117 x=y-2^30
118 x_half=x/2^31
119 t = 1 + (x_half) - 0.5*((x_half)^2) + 0.5*((x_half)^3) - 0.625*((x_half)^4)
120 + 0.875*((x_half)^5)
121 ut=t*(1/sqrt(2))
122 ut2=ut*2^9
123
124 which gives:
125
126 in = 73632
127 in2 = 1206386688
128 y = 603193344
129 x = -470548480
130 x_half = -0.21911621093750
131 t = 0.74973506527313
132 ut = 0.53014274874797
133 ut2 = 2.714330873589594e+002
134
135 */
136
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000137 int16_t x_norm, nshift, t16, sh;
138 int32_t A;
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000139
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000140 int16_t k_sqrt_2 = 23170; // 1/sqrt2 (==5a82)
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000141
142 A = value;
143
144 if (A == 0)
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000145 return (int32_t)0; // sqrt(0) = 0
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000146
147 sh = WebRtcSpl_NormW32(A); // # shifts to normalize A
148 A = WEBRTC_SPL_LSHIFT_W32(A, sh); // Normalize A
149 if (A < (WEBRTC_SPL_WORD32_MAX - 32767))
150 {
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000151 A = A + ((int32_t)32768); // Round off bit
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000152 } else
153 {
154 A = WEBRTC_SPL_WORD32_MAX;
155 }
156
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000157 x_norm = (int16_t)WEBRTC_SPL_RSHIFT_W32(A, 16); // x_norm = AH
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000158
bjornv@webrtc.orgd4fe8242014-10-13 13:01:13 +0000159 nshift = (sh / 2);
160 assert(nshift >= 0);
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000161
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000162 A = (int32_t)WEBRTC_SPL_LSHIFT_W32((int32_t)x_norm, 16);
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000163 A = WEBRTC_SPL_ABS_W32(A); // A = abs(x_norm<<16)
164 A = WebRtcSpl_SqrtLocal(A); // A = sqrt(A)
165
bjornv@webrtc.orgd4fe8242014-10-13 13:01:13 +0000166 if (2 * nshift == sh) {
167 // Even shift value case
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000168
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000169 t16 = (int16_t)WEBRTC_SPL_RSHIFT_W32(A, 16); // t16 = AH
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000170
171 A = WEBRTC_SPL_MUL_16_16(k_sqrt_2, t16) * 2; // A = 1/sqrt(2)*t16
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000172 A = A + ((int32_t)32768); // Round off
173 A = A & ((int32_t)0x7fff0000); // Round off
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000174
175 A = WEBRTC_SPL_RSHIFT_W32(A, 15); // A = A>>16
176
177 } else
178 {
179 A = WEBRTC_SPL_RSHIFT_W32(A, 16); // A = A>>16
180 }
181
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000182 A = A & ((int32_t)0x0000ffff);
bjornv@webrtc.orgd4fe8242014-10-13 13:01:13 +0000183 A >>= nshift; // De-normalize the result.
niklase@google.comf50cf1f2011-07-07 08:33:00 +0000184
185 return A;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}