annotate f103c8/Drivers/CMSIS/DSP_Lib/Source/FastMathFunctions/arm_sqrt_q15.c @ 2:0c59e7a7782a

Working on GPIO and RCC
author cin
date Mon, 16 Jan 2017 11:04:47 +0300
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
1 /* ----------------------------------------------------------------------
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
3 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
4 * $Date: 19. October 2015
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
5 * $Revision: V.1.4.5 a
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
6 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
7 * Project: CMSIS DSP Library
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
8 * Title: arm_sqrt_q15.c
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
9 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
10 * Description: Q15 square root function.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
11 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
13 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
14 * Redistribution and use in source and binary forms, with or without
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
15 * modification, are permitted provided that the following conditions
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
16 * are met:
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
17 * - Redistributions of source code must retain the above copyright
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
18 * notice, this list of conditions and the following disclaimer.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
19 * - Redistributions in binary form must reproduce the above copyright
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
20 * notice, this list of conditions and the following disclaimer in
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
21 * the documentation and/or other materials provided with the
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
22 * distribution.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
23 * - Neither the name of ARM LIMITED nor the names of its contributors
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
24 * may be used to endorse or promote products derived from this
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
25 * software without specific prior written permission.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
26 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
39 * -------------------------------------------------------------------- */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
40 #include "arm_math.h"
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
41 #include "arm_common_tables.h"
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
42
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
43
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
44 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
45 * @ingroup groupFastMath
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
46 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
47
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
48 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
49 * @addtogroup SQRT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
50 * @{
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
51 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
52
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
53 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
54 * @brief Q15 square root function.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
55 * @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
56 * @param[out] *pOut square root of input value.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
57 * @return The function returns ARM_MATH_SUCCESS if the input value is positive
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
58 * and ARM_MATH_ARGUMENT_ERROR if the input is negative. For
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
59 * negative inputs, the function returns *pOut = 0.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
60 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
61
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
62 arm_status arm_sqrt_q15(
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
63 q15_t in,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
64 q15_t * pOut)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
65 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
66 q15_t number, temp1, var1, signBits1, half;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
67 q31_t bits_val1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
68 float32_t temp_float1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
69 union
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
70 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
71 q31_t fracval;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
72 float32_t floatval;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
73 } tempconv;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
74
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
75 number = in;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
76
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
77 /* If the input is a positive number then compute the signBits. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
78 if(number > 0)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
79 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
80 signBits1 = __CLZ(number) - 17;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
81
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
82 /* Shift by the number of signBits1 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
83 if((signBits1 % 2) == 0)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
84 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
85 number = number << signBits1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
86 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
87 else
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
88 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
89 number = number << (signBits1 - 1);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
90 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
91
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
92 /* Calculate half value of the number */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
93 half = number >> 1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
94 /* Store the number for later use */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
95 temp1 = number;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
96
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
97 /* Convert to float */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
98 temp_float1 = number * 3.051757812500000e-005f;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
99 /*Store as integer */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
100 tempconv.floatval = temp_float1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
101 bits_val1 = tempconv.fracval;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
102 /* Subtract the shifted value from the magic number to give intial guess */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
103 bits_val1 = 0x5f3759df - (bits_val1 >> 1); /* gives initial guess */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
104 /* Store as float */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
105 tempconv.fracval = bits_val1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
106 temp_float1 = tempconv.floatval;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
107 /* Convert to integer format */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
108 var1 = (q31_t) (temp_float1 * 16384);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
109
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
110 /* 1st iteration */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
111 var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
112 ((q15_t)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
113 ((((q15_t)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
114 (((q31_t) var1 * var1) >> 15)) *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
115 (q31_t) half) >> 15))) >> 15)) << 2;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
116 /* 2nd iteration */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
117 var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
118 ((q15_t)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
119 ((((q15_t)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
120 (((q31_t) var1 * var1) >> 15)) *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
121 (q31_t) half) >> 15))) >> 15)) << 2;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
122 /* 3rd iteration */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
123 var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
124 ((q15_t)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
125 ((((q15_t)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
126 (((q31_t) var1 * var1) >> 15)) *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
127 (q31_t) half) >> 15))) >> 15)) << 2;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
128
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
129 /* Multiply the inverse square root with the original value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
130 var1 = ((q15_t) (((q31_t) temp1 * var1) >> 15)) << 1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
131
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
132 /* Shift the output down accordingly */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
133 if((signBits1 % 2) == 0)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
134 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
135 var1 = var1 >> (signBits1 / 2);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
136 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
137 else
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
138 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
139 var1 = var1 >> ((signBits1 - 1) / 2);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
140 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
141 *pOut = var1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
142
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
143 return (ARM_MATH_SUCCESS);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
144 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
145 /* If the number is a negative number then store zero as its square root value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
146 else
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
147 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
148 *pOut = 0;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
149 return (ARM_MATH_ARGUMENT_ERROR);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
150 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
151 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
152
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
153 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
154 * @} end of SQRT group
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
155 */