arm_divide_q31.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cos_q31.c
  4. * Description: Fast cosine calculation for Q31 values
  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. #include <stdlib.h>
  31. /**
  32. @ingroup groupFastMath
  33. */
  34. /**
  35. @addtogroup divide
  36. @{
  37. */
  38. /**
  39. @brief Fixed point division
  40. @param[in] numerator Numerator
  41. @param[in] denominator Denominator
  42. @param[out] quotient Quotient value normalized between -1.0 and 1.0
  43. @param[out] shift Shift left value to get the unnormalized quotient
  44. @return error status
  45. When dividing by 0, an error ARM_MATH_NANINF is returned. And the quotient is forced
  46. to the saturated negative or positive value.
  47. */
  48. arm_status arm_divide_q31(q31_t numerator,
  49. q31_t denominator,
  50. q31_t *quotient,
  51. int16_t *shift)
  52. {
  53. int16_t sign=0;
  54. q63_t temp;
  55. int16_t shiftForNormalizing;
  56. *shift = 0;
  57. sign = (numerator<0) ^ (denominator<0);
  58. if (denominator == 0)
  59. {
  60. if (sign)
  61. {
  62. *quotient = 0x80000000;
  63. }
  64. else
  65. {
  66. *quotient = 0x7FFFFFFF;
  67. }
  68. return(ARM_MATH_NANINF);
  69. }
  70. arm_abs_q31(&numerator,&numerator,1);
  71. arm_abs_q31(&denominator,&denominator,1);
  72. temp = ((q63_t)numerator << 31) / ((q63_t)denominator);
  73. shiftForNormalizing= 32 - __CLZ(temp >> 31);
  74. if (shiftForNormalizing > 0)
  75. {
  76. *shift = shiftForNormalizing;
  77. temp = temp >> shiftForNormalizing;
  78. }
  79. if (sign)
  80. {
  81. temp = -temp;
  82. }
  83. *quotient=(q31_t)temp;
  84. return(ARM_MATH_SUCCESS);
  85. }
  86. /**
  87. @} end of divide group
  88. */