1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sqrt_q15.c
4 * Description: Q15 square root function
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
30 #include "arm_common_tables.h"
34 * @ingroup groupFastMath
43 * @brief Q15 square root function.
44 * @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF.
45 * @param[out] *pOut square root of input value.
46 * @return The function returns ARM_MATH_SUCCESS if the input value is positive
47 * and ARM_MATH_ARGUMENT_ERROR if the input is negative. For
48 * negative inputs, the function returns *pOut = 0.
51 arm_status
arm_sqrt_q15(
55 q15_t number
, temp1
, var1
, signBits1
, half
;
57 float32_t temp_float1
;
66 /* If the input is a positive number then compute the signBits. */
69 signBits1
= __CLZ(number
) - 17;
71 /* Shift by the number of signBits1 */
72 if ((signBits1
% 2) == 0)
74 number
= number
<< signBits1
;
78 number
= number
<< (signBits1
- 1);
81 /* Calculate half value of the number */
83 /* Store the number for later use */
86 /* Convert to float */
87 temp_float1
= number
* 3.051757812500000e-005f
;
89 tempconv
.floatval
= temp_float1
;
90 bits_val1
= tempconv
.fracval
;
91 /* Subtract the shifted value from the magic number to give intial guess */
92 bits_val1
= 0x5f3759df - (bits_val1
>> 1); /* gives initial guess */
94 tempconv
.fracval
= bits_val1
;
95 temp_float1
= tempconv
.floatval
;
96 /* Convert to integer format */
97 var1
= (q31_t
) (temp_float1
* 16384);
100 var1
= ((q15_t
) ((q31_t
) var1
* (0x3000 -
103 (((q31_t
) var1
* var1
) >> 15)) *
104 (q31_t
) half
) >> 15))) >> 15)) << 2;
106 var1
= ((q15_t
) ((q31_t
) var1
* (0x3000 -
109 (((q31_t
) var1
* var1
) >> 15)) *
110 (q31_t
) half
) >> 15))) >> 15)) << 2;
112 var1
= ((q15_t
) ((q31_t
) var1
* (0x3000 -
115 (((q31_t
) var1
* var1
) >> 15)) *
116 (q31_t
) half
) >> 15))) >> 15)) << 2;
118 /* Multiply the inverse square root with the original value */
119 var1
= ((q15_t
) (((q31_t
) temp1
* var1
) >> 15)) << 1;
121 /* Shift the output down accordingly */
122 if ((signBits1
% 2) == 0)
124 var1
= var1
>> (signBits1
/ 2);
128 var1
= var1
>> ((signBits1
- 1) / 2);
132 return (ARM_MATH_SUCCESS
);
134 /* If the number is a negative number then store zero as its square root value */
138 return (ARM_MATH_ARGUMENT_ERROR
);
143 * @} end of SQRT group