arm_sqrt_q31.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "dsp/fast_math_functions.h"
  29. #include "arm_common_tables.h"
  30. /**
  31. @ingroup groupFastMath
  32. */
  33. /**
  34. @addtogroup SQRT
  35. @{
  36. */
  37. /**
  38. @brief Q31 square root function.
  39. @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF
  40. @param[out] pOut points to square root of input value
  41. @return execution status
  42. - \ref ARM_MATH_SUCCESS : input value is positive
  43. - \ref ARM_MATH_ARGUMENT_ERROR : input value is negative; *pOut is set to 0
  44. */
  45. #define Q28QUARTER 0x20000000
  46. arm_status arm_sqrt_q31(
  47. q31_t in,
  48. q31_t * pOut)
  49. {
  50. q31_t number, var1, signBits1 ,temp;
  51. number = in;
  52. /* If the input is a positive number then compute the signBits. */
  53. if (number > 0)
  54. {
  55. signBits1 = __CLZ(number) - 1;
  56. /* Shift by the number of signBits1 */
  57. if ((signBits1 % 2) == 0)
  58. {
  59. number = number << signBits1;
  60. }
  61. else
  62. {
  63. number = number << (signBits1 - 1);
  64. }
  65. /* Start value for 1/sqrt(x) for the Newton iteration */
  66. var1 = sqrt_initial_lut_q31[(number>> 26) - (Q28QUARTER >> 26)];
  67. /* 0.5 var1 * (3 - number * var1 * var1) */
  68. /* 1st iteration */
  69. temp = ((q63_t) var1 * var1) >> 28;
  70. temp = ((q63_t) number * temp) >> 31;
  71. temp = 0x30000000 - temp;
  72. var1 = ((q63_t) var1 * temp) >> 29;
  73. /* 2nd iteration */
  74. temp = ((q63_t) var1 * var1) >> 28;
  75. temp = ((q63_t) number * temp) >> 31;
  76. temp = 0x30000000 - temp;
  77. var1 = ((q63_t) var1 * temp) >> 29;
  78. /* 3nd iteration */
  79. temp = ((q63_t) var1 * var1) >> 28;
  80. temp = ((q63_t) number * temp) >> 31;
  81. temp = 0x30000000 - temp;
  82. var1 = ((q63_t) var1 * temp) >> 29;
  83. /* Multiply the inverse square root with the original value */
  84. var1 = ((q31_t) (((q63_t) number * var1) >> 28));
  85. /* Shift the output down accordingly */
  86. if ((signBits1 % 2) == 0)
  87. {
  88. var1 = var1 >> (signBits1 / 2);
  89. }
  90. else
  91. {
  92. var1 = var1 >> ((signBits1 - 1) / 2);
  93. }
  94. *pOut = var1;
  95. return (ARM_MATH_SUCCESS);
  96. }
  97. /* If the number is a negative number then store zero as its square root value */
  98. else
  99. {
  100. *pOut = 0;
  101. return (ARM_MATH_ARGUMENT_ERROR);
  102. }
  103. }
  104. /**
  105. @} end of SQRT group
  106. */