arm_quaternion_normalize_f32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_quaternion_normalize_f32.c
  4. * Description: Floating-point quaternion normalization
  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/quaternion_math_functions.h"
  29. #include <math.h>
  30. /**
  31. @ingroup groupQuaternionMath
  32. */
  33. /**
  34. @defgroup QuatNormalized Quaternion normalization
  35. Compute a normalized quaternion.
  36. */
  37. /**
  38. @addtogroup QuatNormalized
  39. @{
  40. */
  41. /**
  42. @brief Floating-point normalization of quaternions.
  43. @param[in] pInputQuaternions points to the input vector of quaternions
  44. @param[out] pNormalizedQuaternions points to the output vector of normalized quaternions
  45. @param[in] nbQuaternions number of quaternions in each vector
  46. @return none
  47. */
  48. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. #include "arm_helium_utils.h"
  50. void arm_quaternion_normalize_f32(const float32_t *pInputQuaternions,
  51. float32_t *pNormalizedQuaternions,
  52. uint32_t nbQuaternions)
  53. {
  54. f32x4_t vec1,vec2;
  55. float32_t squaredSum,norm;
  56. for(uint32_t i=0; i < nbQuaternions; i++)
  57. {
  58. vec1 = vld1q(pInputQuaternions);
  59. vec2 = vmulq(vec1,vec1);
  60. squaredSum = vecAddAcrossF32Mve(vec2);
  61. arm_sqrt_f32(squaredSum,&norm);
  62. vec1 = vmulq_n_f32(vec1, 1.0f / norm);
  63. vst1q(pNormalizedQuaternions, vec1);
  64. pInputQuaternions += 4;
  65. pNormalizedQuaternions += 4;
  66. }
  67. }
  68. #else
  69. void arm_quaternion_normalize_f32(const float32_t *pInputQuaternions,
  70. float32_t *pNormalizedQuaternions,
  71. uint32_t nbQuaternions)
  72. {
  73. float32_t temp;
  74. uint32_t i;
  75. for(i=0; i < nbQuaternions; i++)
  76. {
  77. temp = SQ(pInputQuaternions[4 * i + 0]) +
  78. SQ(pInputQuaternions[4 * i + 1]) +
  79. SQ(pInputQuaternions[4 * i + 2]) +
  80. SQ(pInputQuaternions[4 * i + 3]);
  81. temp = sqrtf(temp);
  82. pNormalizedQuaternions[4 * i + 0] = pInputQuaternions[4 * i + 0] / temp;
  83. pNormalizedQuaternions[4 * i + 1] = pInputQuaternions[4 * i + 1] / temp;
  84. pNormalizedQuaternions[4 * i + 2] = pInputQuaternions[4 * i + 2] / temp;
  85. pNormalizedQuaternions[4 * i + 3] = pInputQuaternions[4 * i + 3] / temp;
  86. }
  87. }
  88. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  89. /**
  90. @} end of QuatNormalized group
  91. */