1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_sqrt_q15.c
4  * Description:  Q15 square root function
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13  *
14  * SPDX-License-Identifier: Apache-2.0
15  *
16  * Licensed under the Apache License, Version 2.0 (the License); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #include "dsp/fast_math_functions.h"
30 #include "arm_common_tables.h"
31 
32 #define Q12QUARTER 0x2000
33 
34 /**
35   @ingroup groupFastMath
36  */
37 
38 /**
39   @addtogroup SQRT
40   @{
41  */
42 
43 /**
44   @brief         Q15 square root function.
45   @param[in]     in    input value.  The range of the input value is [0 +1) or 0x0000 to 0x7FFF
46   @param[out]    pOut  points to square root of input value
47   @return        execution status
48                    - \ref ARM_MATH_SUCCESS        : input value is positive
49                    - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
50  */
51 
arm_sqrt_q15(q15_t in,q15_t * pOut)52 arm_status arm_sqrt_q15(
53   q15_t in,
54   q15_t * pOut)
55 {
56   q15_t number, var1, signBits1,temp;
57 
58   number = in;
59 
60   /* If the input is a positive number then compute the signBits. */
61   if (number > 0)
62   {
63     signBits1 = __CLZ(number) - 17;
64 
65     /* Shift by the number of signBits1 */
66     if ((signBits1 % 2) == 0)
67     {
68       number = number << signBits1;
69     }
70     else
71     {
72       number = number << (signBits1 - 1);
73     }
74     /* Start value for 1/sqrt(x) for the Newton iteration */
75     var1 = sqrt_initial_lut_q15[(number>> 11) - (Q12QUARTER >> 11)];
76 
77     /* 0.5 var1 * (3 - number * var1 * var1) */
78     /* 1st iteration */
79 
80    temp = ((q31_t) var1 * var1) >> 12;
81    temp = ((q31_t) number * temp) >> 15;
82    temp = 0x3000 - temp;
83    var1 = ((q31_t) var1 * temp) >> 13;
84 
85    temp = ((q31_t) var1 * var1) >> 12;
86    temp = ((q31_t) number * temp) >> 15;
87    temp = 0x3000 - temp;
88    var1 = ((q31_t) var1 * temp) >> 13;
89 
90    temp = ((q31_t) var1 * var1) >> 12;
91    temp = ((q31_t) number * temp) >> 15;
92    temp = 0x3000 - temp;
93    var1 = ((q31_t) var1 * temp) >> 13;
94 
95     /* Multiply the inverse square root with the original value */
96 
97     var1 = ((q15_t) (((q31_t) number * var1) >> 12));
98 
99     /* Shift the output down accordingly */
100     if ((signBits1 % 2) == 0)
101     {
102       var1 = var1 >> (signBits1 / 2);
103     }
104     else
105     {
106       var1 = var1 >> ((signBits1 - 1) / 2);
107     }
108     *pOut = var1;
109 
110 
111     return (ARM_MATH_SUCCESS);
112   }
113   /* If the number is a negative number then store zero as its square root value */
114   else
115   {
116     *pOut = 0;
117 
118     return (ARM_MATH_ARGUMENT_ERROR);
119   }
120 }
121 
122 /**
123   @} end of SQRT group
124  */
125