STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
arm_sqrt_q15.c
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
3 *
4 * $Date: 19. October 2015
5 * $Revision: V.1.4.5 a
6 *
7 * Project: CMSIS DSP Library
8 * Title: arm_sqrt_q15.c
9 *
10 * Description: Q15 square root function.
11 *
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 * - Neither the name of ARM LIMITED nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
40 #include "arm_math.h"
41 #include "arm_common_tables.h"
42 
43 
63  q15_t in,
64  q15_t * pOut)
65 {
66  q15_t number, temp1, var1, signBits1, half;
67  q31_t bits_val1;
68  float32_t temp_float1;
69  union
70  {
71  q31_t fracval;
72  float32_t floatval;
73  } tempconv;
74 
75  number = in;
76 
77  /* If the input is a positive number then compute the signBits. */
78  if(number > 0)
79  {
80  signBits1 = __CLZ(number) - 17;
81 
82  /* Shift by the number of signBits1 */
83  if((signBits1 % 2) == 0)
84  {
85  number = number << signBits1;
86  }
87  else
88  {
89  number = number << (signBits1 - 1);
90  }
91 
92  /* Calculate half value of the number */
93  half = number >> 1;
94  /* Store the number for later use */
95  temp1 = number;
96 
97  /* Convert to float */
98  temp_float1 = number * 3.051757812500000e-005f;
99  /*Store as integer */
100  tempconv.floatval = temp_float1;
101  bits_val1 = tempconv.fracval;
102  /* Subtract the shifted value from the magic number to give intial guess */
103  bits_val1 = 0x5f3759df - (bits_val1 >> 1); /* gives initial guess */
104  /* Store as float */
105  tempconv.fracval = bits_val1;
106  temp_float1 = tempconv.floatval;
107  /* Convert to integer format */
108  var1 = (q31_t) (temp_float1 * 16384);
109 
110  /* 1st iteration */
111  var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
112  ((q15_t)
113  ((((q15_t)
114  (((q31_t) var1 * var1) >> 15)) *
115  (q31_t) half) >> 15))) >> 15)) << 2;
116  /* 2nd iteration */
117  var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
118  ((q15_t)
119  ((((q15_t)
120  (((q31_t) var1 * var1) >> 15)) *
121  (q31_t) half) >> 15))) >> 15)) << 2;
122  /* 3rd iteration */
123  var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
124  ((q15_t)
125  ((((q15_t)
126  (((q31_t) var1 * var1) >> 15)) *
127  (q31_t) half) >> 15))) >> 15)) << 2;
128 
129  /* Multiply the inverse square root with the original value */
130  var1 = ((q15_t) (((q31_t) temp1 * var1) >> 15)) << 1;
131 
132  /* Shift the output down accordingly */
133  if((signBits1 % 2) == 0)
134  {
135  var1 = var1 >> (signBits1 / 2);
136  }
137  else
138  {
139  var1 = var1 >> ((signBits1 - 1) / 2);
140  }
141  *pOut = var1;
142 
143  return (ARM_MATH_SUCCESS);
144  }
145  /* If the number is a negative number then store zero as its square root value */
146  else
147  {
148  *pOut = 0;
149  return (ARM_MATH_ARGUMENT_ERROR);
150  }
151 }
152 
float float32_t
32-bit floating-point type definition.
Definition: arm_math.h:407
int16_t q15_t
16-bit fractional data type in 1.15 format.
Definition: arm_math.h:392
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition: arm_math.h:397
arm_status arm_sqrt_q15(q15_t in, q15_t *pOut)
Q15 square root function.
Definition: arm_sqrt_q15.c:62
#define __CLZ
Count leading zeros.
Definition: cmsis_armcc.h:463
arm_status
Error status returned by some functions in the library.
Definition: arm_math.h:373