arm_divide_q31.c 2.5 KB

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