/ Drivers / CMSIS / DSP / Source / FastMathFunctions / arm_sqrt_q31.c
arm_sqrt_q31.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_sqrt_q31.c
  4   * Description:  Q31 square root function
  5   *
  6   * $Date:        23 April 2021
  7   * $Revision:    V1.9.0
  8   *
  9   * Target Processor: Cortex-M and Cortex-A cores
 10   * -------------------------------------------------------------------- */
 11  /*
 12   * Copyright (C) 2010-2021 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 "dsp/fast_math_functions.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  points to square root of input value
 45    @return        execution status
 46                     - \ref ARM_MATH_SUCCESS        : input value is positive
 47                     - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
 48   */
 49  #define Q28QUARTER 0x20000000 
 50  
 51  arm_status arm_sqrt_q31(
 52    q31_t in,
 53    q31_t * pOut)
 54  {
 55    q31_t number, var1, signBits1 ,temp;
 56   
 57    number = in;
 58  
 59    /* If the input is a positive number then compute the signBits. */
 60    if (number > 0)
 61    {
 62      signBits1 = __CLZ(number) - 1;
 63  
 64      /* Shift by the number of signBits1 */
 65      if ((signBits1 % 2) == 0)
 66      {
 67        number = number << signBits1;
 68      }
 69      else
 70      {
 71        number = number << (signBits1 - 1);
 72      }
 73  
 74      /* Start value for 1/sqrt(x) for the Newton iteration */
 75      var1 = sqrt_initial_lut_q31[(number>> 26) - (Q28QUARTER >> 26)];
 76  
 77      /* 0.5 var1 * (3 - number * var1 * var1) */
 78  
 79      /* 1st iteration */
 80  
 81      temp = ((q63_t) var1 * var1) >> 28;
 82      temp = ((q63_t) number * temp) >> 31;
 83      temp = 0x30000000 - temp; 
 84      var1 = ((q63_t) var1 * temp) >> 29;
 85  
 86      
 87      /* 2nd iteration */
 88      temp = ((q63_t) var1 * var1) >> 28;
 89      temp = ((q63_t) number * temp) >> 31;
 90      temp = 0x30000000 - temp; 
 91      var1 = ((q63_t) var1 * temp) >> 29;
 92  
 93      /* 3nd iteration */
 94      temp = ((q63_t) var1 * var1) >> 28;
 95      temp = ((q63_t) number * temp) >> 31;
 96      temp = 0x30000000 - temp; 
 97      var1 = ((q63_t) var1 * temp) >> 29;
 98  
 99      /* Multiply the inverse square root with the original value */
100      var1 = ((q31_t) (((q63_t) number * var1) >> 28));
101  
102      /* Shift the output down accordingly */
103      if ((signBits1 % 2) == 0)
104      {
105        var1 = var1 >> (signBits1 / 2);
106      }
107      else
108      {
109        var1 = var1 >> ((signBits1 - 1) / 2);
110      }
111      *pOut = var1;
112  
113      return (ARM_MATH_SUCCESS);
114    }
115    /* If the number is a negative number then store zero as its square root value */
116    else
117    {
118      *pOut = 0;
119  
120      return (ARM_MATH_ARGUMENT_ERROR);
121    }
122  }
123  
124  /**
125    @} end of SQRT group
126   */