1 | /* ----------------------------------------------------------------------
|
---|
2 | * Project: CMSIS DSP Library
|
---|
3 | * Title: arm_sqrt_q31.c
|
---|
4 | * Description: Q31 square root function
|
---|
5 | *
|
---|
6 | * $Date: 27. January 2017
|
---|
7 | * $Revision: V.1.5.1
|
---|
8 | *
|
---|
9 | * Target Processor: Cortex-M cores
|
---|
10 | * -------------------------------------------------------------------- */
|
---|
11 | /*
|
---|
12 | * Copyright (C) 2010-2017 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 "arm_math.h"
|
---|
30 | #include "arm_common_tables.h"
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @ingroup groupFastMath
|
---|
34 | */
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @addtogroup SQRT
|
---|
38 | * @{
|
---|
39 | */
|
---|
40 |
|
---|
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.
|
---|
48 | */
|
---|
49 |
|
---|
50 | arm_status arm_sqrt_q31(
|
---|
51 | q31_t in,
|
---|
52 | q31_t * pOut)
|
---|
53 | {
|
---|
54 | q31_t number, temp1, bits_val1, var1, signBits1, half;
|
---|
55 | float32_t temp_float1;
|
---|
56 | union
|
---|
57 | {
|
---|
58 | q31_t fracval;
|
---|
59 | float32_t floatval;
|
---|
60 | } tempconv;
|
---|
61 |
|
---|
62 | number = in;
|
---|
63 |
|
---|
64 | /* If the input is a positive number then compute the signBits. */
|
---|
65 | if (number > 0)
|
---|
66 | {
|
---|
67 | signBits1 = __CLZ(number) - 1;
|
---|
68 |
|
---|
69 | /* Shift by the number of signBits1 */
|
---|
70 | if ((signBits1 % 2) == 0)
|
---|
71 | {
|
---|
72 | number = number << signBits1;
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | number = number << (signBits1 - 1);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Calculate half value of the number */
|
---|
80 | half = number >> 1;
|
---|
81 | /* Store the number for later use */
|
---|
82 | temp1 = number;
|
---|
83 |
|
---|
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);
|
---|
96 |
|
---|
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;
|
---|
115 |
|
---|
116 | /* Multiply the inverse square root with the original value */
|
---|
117 | var1 = ((q31_t) (((q63_t) temp1 * var1) >> 31)) << 1;
|
---|
118 |
|
---|
119 | /* Shift the output down accordingly */
|
---|
120 | if ((signBits1 % 2) == 0)
|
---|
121 | {
|
---|
122 | var1 = var1 >> (signBits1 / 2);
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | var1 = var1 >> ((signBits1 - 1) / 2);
|
---|
127 | }
|
---|
128 | *pOut = var1;
|
---|
129 |
|
---|
130 | return (ARM_MATH_SUCCESS);
|
---|
131 | }
|
---|
132 | /* If the number is a negative number then store zero as its square root value */
|
---|
133 | else
|
---|
134 | {
|
---|
135 | *pOut = 0;
|
---|
136 | return (ARM_MATH_ARGUMENT_ERROR);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * @} end of SQRT group
|
---|
142 | */
|
---|