Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / CMSIS / DSP / Source / FastMathFunctions / arm_sqrt_q31.c
blob0deea04d785839c747639961d7dd64b1a6581692
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sqrt_q31.c
4 * Description: Q31 square root function
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
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.
29 #include "arm_math.h"
30 #include "arm_common_tables.h"
32 /**
33 * @ingroup groupFastMath
36 /**
37 * @addtogroup SQRT
38 * @{
41 /**
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(
51 q31_t in,
52 q31_t * pOut)
54 q31_t number, temp1, bits_val1, var1, signBits1, half;
55 float32_t temp_float1;
56 union
58 q31_t fracval;
59 float32_t floatval;
60 } tempconv;
62 number = in;
64 /* If the input is a positive number then compute the signBits. */
65 if (number > 0)
67 signBits1 = __CLZ(number) - 1;
69 /* Shift by the number of signBits1 */
70 if ((signBits1 % 2) == 0)
72 number = number << signBits1;
74 else
76 number = number << (signBits1 - 1);
79 /* Calculate half value of the number */
80 half = number >> 1;
81 /* Store the number for later use */
82 temp1 = number;
84 /*Convert to float */
85 temp_float1 = number * 4.6566128731e-010f;
86 /*Store as integer */
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 */
91 /* Store as float */
92 tempconv.fracval = bits_val1;
93 temp_float1 = tempconv.floatval;
94 /* Convert to integer format */
95 var1 = (q31_t) (temp_float1 * 1073741824);
97 /* 1st iteration */
98 var1 = ((q31_t) ((q63_t) var1 * (0x30000000 -
99 ((q31_t)
100 ((((q31_t)
101 (((q63_t) var1 * var1) >> 31)) *
102 (q63_t) half) >> 31))) >> 31)) << 2;
103 /* 2nd iteration */
104 var1 = ((q31_t) ((q63_t) var1 * (0x30000000 -
105 ((q31_t)
106 ((((q31_t)
107 (((q63_t) var1 * var1) >> 31)) *
108 (q63_t) half) >> 31))) >> 31)) << 2;
109 /* 3rd iteration */
110 var1 = ((q31_t) ((q63_t) var1 * (0x30000000 -
111 ((q31_t)
112 ((((q31_t)
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);
124 else
126 var1 = var1 >> ((signBits1 - 1) / 2);
128 *pOut = var1;
130 return (ARM_MATH_SUCCESS);
132 /* If the number is a negative number then store zero as its square root value */
133 else
135 *pOut = 0;
136 return (ARM_MATH_ARGUMENT_ERROR);
141 * @} end of SQRT group