arm_quaternion_normalize_f32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. */
  47. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  48. #include "arm_helium_utils.h"
  49. void arm_quaternion_normalize_f32(const float32_t *pInputQuaternions,
  50. float32_t *pNormalizedQuaternions,
  51. uint32_t nbQuaternions)
  52. {
  53. f32x4_t vec1,vec2;
  54. float32_t squaredSum,norm;
  55. for(uint32_t i=0; i < nbQuaternions; i++)
  56. {
  57. vec1 = vld1q(pInputQuaternions);
  58. vec2 = vmulq(vec1,vec1);
  59. squaredSum = vecAddAcrossF32Mve(vec2);
  60. arm_sqrt_f32(squaredSum,&norm);
  61. vec1 = vmulq_n_f32(vec1, 1.0f / norm);
  62. vst1q(pNormalizedQuaternions, vec1);
  63. pInputQuaternions += 4;
  64. pNormalizedQuaternions += 4;
  65. }
  66. }
  67. #else
  68. void arm_quaternion_normalize_f32(const float32_t *pInputQuaternions,
  69. float32_t *pNormalizedQuaternions,
  70. uint32_t nbQuaternions)
  71. {
  72. float32_t temp;
  73. uint32_t i;
  74. for(i=0; i < nbQuaternions; i++)
  75. {
  76. temp = ARM_SQ(pInputQuaternions[4 * i + 0]) +
  77. ARM_SQ(pInputQuaternions[4 * i + 1]) +
  78. ARM_SQ(pInputQuaternions[4 * i + 2]) +
  79. ARM_SQ(pInputQuaternions[4 * i + 3]);
  80. temp = sqrtf(temp);
  81. pNormalizedQuaternions[4 * i + 0] = pInputQuaternions[4 * i + 0] / temp;
  82. pNormalizedQuaternions[4 * i + 1] = pInputQuaternions[4 * i + 1] / temp;
  83. pNormalizedQuaternions[4 * i + 2] = pInputQuaternions[4 * i + 2] / temp;
  84. pNormalizedQuaternions[4 * i + 3] = pInputQuaternions[4 * i + 3] / temp;
  85. }
  86. }
  87. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  88. /**
  89. @} end of QuatNormalized group
  90. */