in_unpack_i1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Helper function for repacking arrays.
  2. Copyright (C) 2003-2015 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 <stdlib.h>
  22. #include <assert.h>
  23. #include <string.h>
  24. #if defined (HAVE_GFC_INTEGER_1)
  25. void
  26. internal_unpack_1 (gfc_array_i1 * d, const GFC_INTEGER_1 * src)
  27. {
  28. index_type count[GFC_MAX_DIMENSIONS];
  29. index_type extent[GFC_MAX_DIMENSIONS];
  30. index_type stride[GFC_MAX_DIMENSIONS];
  31. index_type stride0;
  32. index_type dim;
  33. index_type dsize;
  34. GFC_INTEGER_1 * restrict dest;
  35. int n;
  36. dest = d->base_addr;
  37. if (src == dest || !src)
  38. return;
  39. dim = GFC_DESCRIPTOR_RANK (d);
  40. dsize = 1;
  41. for (n = 0; n < dim; n++)
  42. {
  43. count[n] = 0;
  44. stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
  45. extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
  46. if (extent[n] <= 0)
  47. return;
  48. if (dsize == stride[n])
  49. dsize *= extent[n];
  50. else
  51. dsize = 0;
  52. }
  53. if (dsize != 0)
  54. {
  55. memcpy (dest, src, dsize * sizeof (GFC_INTEGER_1));
  56. return;
  57. }
  58. stride0 = stride[0];
  59. while (dest)
  60. {
  61. /* Copy the data. */
  62. *dest = *(src++);
  63. /* Advance to the next element. */
  64. dest += stride0;
  65. count[0]++;
  66. /* Advance to the next source element. */
  67. n = 0;
  68. while (count[n] == extent[n])
  69. {
  70. /* When we get to the end of a dimension, reset it and increment
  71. the next dimension. */
  72. count[n] = 0;
  73. /* We could precalculate these products, but this is a less
  74. frequently used path so probably not worth it. */
  75. dest -= stride[n] * extent[n];
  76. n++;
  77. if (n == dim)
  78. {
  79. dest = NULL;
  80. break;
  81. }
  82. else
  83. {
  84. count[n]++;
  85. dest += stride[n];
  86. }
  87. }
  88. }
  89. }
  90. #endif