arm_merge_sort_f32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_merge_sort_f32.c
  4. * Description: Floating point merge sort
  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/support_functions.h"
  29. #include "arm_sorting.h"
  30. static void topDownMerge(float32_t * pA, uint32_t begin, uint32_t middle, uint32_t end, float32_t * pB, uint8_t dir)
  31. {
  32. /* Left array is pA[begin:middle-1]
  33. * Right Array is pA[middle:end-1]
  34. * They are merged in pB
  35. */
  36. uint32_t i = begin;
  37. uint32_t j = middle;
  38. uint32_t k;
  39. // Read all the elements in the sublist
  40. for (k = begin; k < end; k++)
  41. {
  42. // Merge
  43. if (i < middle && (j >= end || dir==(pA[i] <= pA[j])) )
  44. {
  45. pB[k] = pA[i];
  46. i++;
  47. }
  48. else
  49. {
  50. pB[k] = pA[j];
  51. j++;
  52. }
  53. }
  54. }
  55. static void arm_merge_sort_core_f32(float32_t * pB, uint32_t begin, uint32_t end, float32_t * pA, uint8_t dir)
  56. {
  57. if((int32_t)end - (int32_t)begin >= 2 ) // If run size != 1 divide
  58. {
  59. int32_t middle = (end + begin) / 2; // Take the middle point
  60. arm_merge_sort_core_f32(pA, begin, middle, pB, dir); // Sort the left part
  61. arm_merge_sort_core_f32(pA, middle, end, pB, dir); // Sort the right part
  62. topDownMerge(pB, begin, middle, end, pA, dir);
  63. }
  64. }
  65. /**
  66. @ingroup groupSupport
  67. */
  68. /**
  69. @addtogroup Sorting
  70. @{
  71. */
  72. /**
  73. * @param[in] S points to an instance of the sorting structure.
  74. * @param[in] pSrc points to the block of input data.
  75. * @param[out] pDst points to the block of output data
  76. * @param[in] blockSize number of samples to process.
  77. *
  78. * @par Algorithm
  79. * The merge sort algorithm is a comparison algorithm that
  80. * divide the input array in sublists and merge them to produce
  81. * longer sorted sublists until there is only one list remaining.
  82. *
  83. * @par A work array is always needed. It must be allocated by the user
  84. * linked to the instance at initialization time.
  85. *
  86. * @par It's an in-place algorithm. In order to obtain an out-of-place
  87. * function, a memcpy of the source vector is performed
  88. */
  89. void arm_merge_sort_f32(
  90. const arm_merge_sort_instance_f32 * S,
  91. float32_t *pSrc,
  92. float32_t *pDst,
  93. uint32_t blockSize)
  94. {
  95. float32_t * pA;
  96. /* Out-of-place */
  97. if(pSrc != pDst)
  98. {
  99. memcpy(pDst, pSrc, blockSize*sizeof(float32_t));
  100. pA = pDst;
  101. }
  102. else
  103. pA = pSrc;
  104. /* A working buffer is needed */
  105. memcpy(S->buffer, pSrc, blockSize*sizeof(float32_t));
  106. arm_merge_sort_core_f32(S->buffer, 0, blockSize, pA, S->dir);
  107. }
  108. /**
  109. @} end of Sorting group
  110. */