in_unpack_c4.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Helper function for repacking arrays.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <string.h>
  22. #if defined (HAVE_GFC_COMPLEX_4)
  23. void
  24. internal_unpack_c4 (gfc_array_c4 * d, const GFC_COMPLEX_4 * src)
  25. {
  26. index_type count[GFC_MAX_DIMENSIONS];
  27. index_type extent[GFC_MAX_DIMENSIONS];
  28. index_type stride[GFC_MAX_DIMENSIONS];
  29. index_type stride0;
  30. index_type dim;
  31. index_type dsize;
  32. GFC_COMPLEX_4 * restrict dest;
  33. dest = d->base_addr;
  34. if (src == dest || !src)
  35. return;
  36. dim = GFC_DESCRIPTOR_RANK (d);
  37. dsize = 1;
  38. for (index_type n = 0; n < dim; n++)
  39. {
  40. count[n] = 0;
  41. stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
  42. extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
  43. if (extent[n] <= 0)
  44. return;
  45. if (dsize == stride[n])
  46. dsize *= extent[n];
  47. else
  48. dsize = 0;
  49. }
  50. if (dsize != 0)
  51. {
  52. memcpy (dest, src, dsize * sizeof (GFC_COMPLEX_4));
  53. return;
  54. }
  55. stride0 = stride[0];
  56. while (dest)
  57. {
  58. /* Copy the data. */
  59. *dest = *(src++);
  60. /* Advance to the next element. */
  61. dest += stride0;
  62. count[0]++;
  63. /* Advance to the next source element. */
  64. index_type n = 0;
  65. while (count[n] == extent[n])
  66. {
  67. /* When we get to the end of a dimension, reset it and increment
  68. the next dimension. */
  69. count[n] = 0;
  70. /* We could precalculate these products, but this is a less
  71. frequently used path so probably not worth it. */
  72. dest -= stride[n] * extent[n];
  73. n++;
  74. if (n == dim)
  75. {
  76. dest = NULL;
  77. break;
  78. }
  79. else
  80. {
  81. count[n]++;
  82. dest += stride[n];
  83. }
  84. }
  85. }
  86. }
  87. #endif