arm_sqrt_q15.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "arm_math.h"
  29. #include "arm_common_tables.h"
  30. /**
  31. * @ingroup groupFastMath
  32. */
  33. /**
  34. * @addtogroup SQRT
  35. * @{
  36. */
  37. /**
  38. * @brief Q15 square root function.
  39. * @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF.
  40. * @param[out] *pOut square root of input value.
  41. * @return The function returns ARM_MATH_SUCCESS if the input value is positive
  42. * and ARM_MATH_ARGUMENT_ERROR if the input is negative. For
  43. * negative inputs, the function returns *pOut = 0.
  44. */
  45. arm_status arm_sqrt_q15(
  46. q15_t in,
  47. q15_t * pOut)
  48. {
  49. q15_t number, temp1, var1, signBits1, half;
  50. q31_t bits_val1;
  51. float32_t temp_float1;
  52. union
  53. {
  54. q31_t fracval;
  55. float32_t floatval;
  56. } tempconv;
  57. number = in;
  58. /* If the input is a positive number then compute the signBits. */
  59. if (number > 0)
  60. {
  61. signBits1 = __CLZ(number) - 17;
  62. /* Shift by the number of signBits1 */
  63. if ((signBits1 % 2) == 0)
  64. {
  65. number = number << signBits1;
  66. }
  67. else
  68. {
  69. number = number << (signBits1 - 1);
  70. }
  71. /* Calculate half value of the number */
  72. half = number >> 1;
  73. /* Store the number for later use */
  74. temp1 = number;
  75. /* Convert to float */
  76. temp_float1 = number * 3.051757812500000e-005f;
  77. /*Store as integer */
  78. tempconv.floatval = temp_float1;
  79. bits_val1 = tempconv.fracval;
  80. /* Subtract the shifted value from the magic number to give intial guess */
  81. bits_val1 = 0x5f3759df - (bits_val1 >> 1); /* gives initial guess */
  82. /* Store as float */
  83. tempconv.fracval = bits_val1;
  84. temp_float1 = tempconv.floatval;
  85. /* Convert to integer format */
  86. var1 = (q31_t) (temp_float1 * 16384);
  87. /* 1st iteration */
  88. var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
  89. ((q15_t)
  90. ((((q15_t)
  91. (((q31_t) var1 * var1) >> 15)) *
  92. (q31_t) half) >> 15))) >> 15)) << 2;
  93. /* 2nd iteration */
  94. var1 = ((q15_t) ((q31_t) var1 * (0x3000 -
  95. ((q15_t)
  96. ((((q15_t)
  97. (((q31_t) var1 * var1) >> 15)) *
  98. (q31_t) half) >> 15))) >> 15)) << 2;
  99. /* 3rd 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. /* Multiply the inverse square root with the original value */
  106. var1 = ((q15_t) (((q31_t) temp1 * var1) >> 15)) << 1;
  107. /* Shift the output down accordingly */
  108. if ((signBits1 % 2) == 0)
  109. {
  110. var1 = var1 >> (signBits1 / 2);
  111. }
  112. else
  113. {
  114. var1 = var1 >> ((signBits1 - 1) / 2);
  115. }
  116. *pOut = var1;
  117. return (ARM_MATH_SUCCESS);
  118. }
  119. /* If the number is a negative number then store zero as its square root value */
  120. else
  121. {
  122. *pOut = 0;
  123. return (ARM_MATH_ARGUMENT_ERROR);
  124. }
  125. }
  126. /**
  127. * @} end of SQRT group
  128. */