arm_sqrt_q15.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  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. * @ingroup groupFastMath
  44. */
  45. /**
  46. * @addtogroup SQRT
  47. * @{
  48. */
  49. /**
  50. * @brief Q15 square root function.
  51. * @param[in] in input value. The range of the input value is [0 +1) or 0x0000 to 0x7FFF.
  52. * @param[out] *pOut square root of input value.
  53. * @return The function returns ARM_MATH_SUCCESS if the input value is positive
  54. * and ARM_MATH_ARGUMENT_ERROR if the input is negative. For
  55. * negative inputs, the function returns *pOut = 0.
  56. */
  57. arm_status arm_sqrt_q15(
  58. q15_t in,
  59. q15_t * pOut)
  60. {
  61. q15_t number, temp1, var1, signBits1, half;
  62. q31_t bits_val1;
  63. float32_t temp_float1;
  64. union
  65. {
  66. q31_t fracval;
  67. float32_t floatval;
  68. } tempconv;
  69. number = in;
  70. /* If the input is a positive number then compute the signBits. */
  71. if(number > 0)
  72. {
  73. signBits1 = __CLZ(number) - 17;
  74. /* Shift by the number of signBits1 */
  75. if((signBits1 % 2) == 0)
  76. {
  77. number = number << signBits1;
  78. }
  79. else
  80. {
  81. number = number << (signBits1 - 1);
  82. }
  83. /* Calculate half value of the number */
  84. half = number >> 1;
  85. /* Store the number for later use */
  86. temp1 = number;
  87. /*Convert to float */
  88. temp_float1 = number * 3.051757812500000e-005f;
  89. /*Store as integer */
  90. tempconv.floatval = temp_float1;
  91. bits_val1 = tempconv.fracval;
  92. /* Subtract the shifted value from the magic number to give intial guess */
  93. bits_val1 = 0x5f3759df - (bits_val1 >> 1); // gives initial guess
  94. /* Store as float */
  95. tempconv.fracval = bits_val1;
  96. temp_float1 = tempconv.floatval;
  97. /* Convert to integer format */
  98. var1 = (q31_t) (temp_float1 * 16384);
  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. /* Multiply the inverse square root with the original value */
  118. var1 = ((q15_t) (((q31_t) temp1 * var1) >> 15)) << 1;
  119. /* Shift the output down accordingly */
  120. if((signBits1 % 2) == 0)
  121. {
  122. var1 = var1 >> (signBits1 / 2);
  123. }
  124. else
  125. {
  126. var1 = var1 >> ((signBits1 - 1) / 2);
  127. }
  128. *pOut = var1;
  129. return (ARM_MATH_SUCCESS);
  130. }
  131. /* If the number is a negative number then store zero as its square root value */
  132. else
  133. {
  134. *pOut = 0;
  135. return (ARM_MATH_ARGUMENT_ERROR);
  136. }
  137. }
  138. /**
  139. * @} end of SQRT group
  140. */