source: S-port/trunk/Drivers/CMSIS/DSP/Source/FastMathFunctions/arm_sqrt_q15.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 4.3 KB
Line 
1/* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sqrt_q15.c
4 * Description: Q15 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/**
34 * @ingroup groupFastMath
35 */
36
37/**
38 * @addtogroup SQRT
39 * @{
40 */
41
42 /**
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.
49 */
50
51arm_status arm_sqrt_q15(
52 q15_t in,
53 q15_t * pOut)
54{
55 q15_t number, temp1, var1, signBits1, half;
56 q31_t bits_val1;
57 float32_t temp_float1;
58 union
59 {
60 q31_t fracval;
61 float32_t floatval;
62 } tempconv;
63
64 number = in;
65
66 /* If the input is a positive number then compute the signBits. */
67 if (number > 0)
68 {
69 signBits1 = __CLZ(number) - 17;
70
71 /* Shift by the number of signBits1 */
72 if ((signBits1 % 2) == 0)
73 {
74 number = number << signBits1;
75 }
76 else
77 {
78 number = number << (signBits1 - 1);
79 }
80
81 /* Calculate half value of the number */
82 half = number >> 1;
83 /* Store the number for later use */
84 temp1 = number;
85
86 /* Convert to float */
87 temp_float1 = number * 3.051757812500000e-005f;
88 /*Store as integer */
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 */
93 /* Store as float */
94 tempconv.fracval = bits_val1;
95 temp_float1 = tempconv.floatval;
96 /* Convert to integer format */
97 var1 = (q31_t) (temp_float1 * 16384);
98
99 /* 1st iteration */
100 var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
101 ((q15_t)
102 ((((q15_t)
103 (((q31_t) var1 * var1) >> 15)) *
104 (q31_t) half) >> 15))) >> 15)) << 2;
105 /* 2nd iteration */
106 var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
107 ((q15_t)
108 ((((q15_t)
109 (((q31_t) var1 * var1) >> 15)) *
110 (q31_t) half) >> 15))) >> 15)) << 2;
111 /* 3rd iteration */
112 var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
113 ((q15_t)
114 ((((q15_t)
115 (((q31_t) var1 * var1) >> 15)) *
116 (q31_t) half) >> 15))) >> 15)) << 2;
117
118 /* Multiply the inverse square root with the original value */
119 var1 = ((q15_t) (((q31_t) temp1 * var1) >> 15)) << 1;
120
121 /* Shift the output down accordingly */
122 if ((signBits1 % 2) == 0)
123 {
124 var1 = var1 >> (signBits1 / 2);
125 }
126 else
127 {
128 var1 = var1 >> ((signBits1 - 1) / 2);
129 }
130 *pOut = var1;
131
132 return (ARM_MATH_SUCCESS);
133 }
134 /* If the number is a negative number then store zero as its square root value */
135 else
136 {
137 *pOut = 0;
138 return (ARM_MATH_ARGUMENT_ERROR);
139 }
140}
141
142/**
143 * @} end of SQRT group
144 */
Note: See TracBrowser for help on using the repository browser.