1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sqrt_q31.c
4 * Description: Q31 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"
33 * @ingroup groupFastMath
42 * @brief Q31 square root function.
43 * @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF.
44 * @param[out] *pOut square root of input value.
45 * @return The function returns ARM_MATH_SUCCESS if the input value is positive
46 * and ARM_MATH_ARGUMENT_ERROR if the input is negative. For
47 * negative inputs, the function returns *pOut = 0.
50 arm_status
arm_sqrt_q31(
54 q31_t number
, temp1
, bits_val1
, var1
, signBits1
, half
;
55 float32_t temp_float1
;
64 /* If the input is a positive number then compute the signBits. */
67 signBits1
= __CLZ(number
) - 1;
69 /* Shift by the number of signBits1 */
70 if ((signBits1
% 2) == 0)
72 number
= number
<< signBits1
;
76 number
= number
<< (signBits1
- 1);
79 /* Calculate half value of the number */
81 /* Store the number for later use */
85 temp_float1
= number
* 4.6566128731e-010f
;
87 tempconv
.floatval
= temp_float1
;
88 bits_val1
= tempconv
.fracval
;
89 /* Subtract the shifted value from the magic number to give intial guess */
90 bits_val1
= 0x5f3759df - (bits_val1
>> 1); /* gives initial guess */
92 tempconv
.fracval
= bits_val1
;
93 temp_float1
= tempconv
.floatval
;
94 /* Convert to integer format */
95 var1
= (q31_t
) (temp_float1
* 1073741824);
98 var1
= ((q31_t
) ((q63_t
) var1
* (0x30000000 -
101 (((q63_t
) var1
* var1
) >> 31)) *
102 (q63_t
) half
) >> 31))) >> 31)) << 2;
104 var1
= ((q31_t
) ((q63_t
) var1
* (0x30000000 -
107 (((q63_t
) var1
* var1
) >> 31)) *
108 (q63_t
) half
) >> 31))) >> 31)) << 2;
110 var1
= ((q31_t
) ((q63_t
) var1
* (0x30000000 -
113 (((q63_t
) var1
* var1
) >> 31)) *
114 (q63_t
) half
) >> 31))) >> 31)) << 2;
116 /* Multiply the inverse square root with the original value */
117 var1
= ((q31_t
) (((q63_t
) temp1
* var1
) >> 31)) << 1;
119 /* Shift the output down accordingly */
120 if ((signBits1
% 2) == 0)
122 var1
= var1
>> (signBits1
/ 2);
126 var1
= var1
>> ((signBits1
- 1) / 2);
130 return (ARM_MATH_SUCCESS
);
132 /* If the number is a negative number then store zero as its square root value */
136 return (ARM_MATH_ARGUMENT_ERROR
);
141 * @} end of SQRT group