in_pack_generic.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Generic 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. extern void *internal_pack (gfc_array_char *);
  25. export_proto(internal_pack);
  26. void *
  27. internal_pack (gfc_array_char * source)
  28. {
  29. index_type count[GFC_MAX_DIMENSIONS];
  30. index_type extent[GFC_MAX_DIMENSIONS];
  31. index_type stride[GFC_MAX_DIMENSIONS];
  32. index_type stride0;
  33. index_type dim;
  34. index_type ssize;
  35. const char *src;
  36. char *dest;
  37. void *destptr;
  38. int n;
  39. int packed;
  40. index_type size;
  41. index_type type_size;
  42. if (source->base_addr == NULL)
  43. return NULL;
  44. type_size = GFC_DTYPE_TYPE_SIZE(source);
  45. size = GFC_DESCRIPTOR_SIZE (source);
  46. switch (type_size)
  47. {
  48. case GFC_DTYPE_INTEGER_1:
  49. case GFC_DTYPE_LOGICAL_1:
  50. case GFC_DTYPE_DERIVED_1:
  51. return internal_pack_1 ((gfc_array_i1 *) source);
  52. case GFC_DTYPE_INTEGER_2:
  53. case GFC_DTYPE_LOGICAL_2:
  54. return internal_pack_2 ((gfc_array_i2 *) source);
  55. case GFC_DTYPE_INTEGER_4:
  56. case GFC_DTYPE_LOGICAL_4:
  57. return internal_pack_4 ((gfc_array_i4 *) source);
  58. case GFC_DTYPE_INTEGER_8:
  59. case GFC_DTYPE_LOGICAL_8:
  60. return internal_pack_8 ((gfc_array_i8 *) source);
  61. #if defined(HAVE_GFC_INTEGER_16)
  62. case GFC_DTYPE_INTEGER_16:
  63. case GFC_DTYPE_LOGICAL_16:
  64. return internal_pack_16 ((gfc_array_i16 *) source);
  65. #endif
  66. case GFC_DTYPE_REAL_4:
  67. return internal_pack_r4 ((gfc_array_r4 *) source);
  68. case GFC_DTYPE_REAL_8:
  69. return internal_pack_r8 ((gfc_array_r8 *) source);
  70. /* FIXME: This here is a hack, which will have to be removed when
  71. the array descriptor is reworked. Currently, we don't store the
  72. kind value for the type, but only the size. Because on targets with
  73. __float128, we have sizeof(logn double) == sizeof(__float128),
  74. we cannot discriminate here and have to fall back to the generic
  75. handling (which is suboptimal). */
  76. #if !defined(GFC_REAL_16_IS_FLOAT128)
  77. # if defined (HAVE_GFC_REAL_10)
  78. case GFC_DTYPE_REAL_10:
  79. return internal_pack_r10 ((gfc_array_r10 *) source);
  80. # endif
  81. # if defined (HAVE_GFC_REAL_16)
  82. case GFC_DTYPE_REAL_16:
  83. return internal_pack_r16 ((gfc_array_r16 *) source);
  84. # endif
  85. #endif
  86. case GFC_DTYPE_COMPLEX_4:
  87. return internal_pack_c4 ((gfc_array_c4 *) source);
  88. case GFC_DTYPE_COMPLEX_8:
  89. return internal_pack_c8 ((gfc_array_c8 *) source);
  90. /* FIXME: This here is a hack, which will have to be removed when
  91. the array descriptor is reworked. Currently, we don't store the
  92. kind value for the type, but only the size. Because on targets with
  93. __float128, we have sizeof(logn double) == sizeof(__float128),
  94. we cannot discriminate here and have to fall back to the generic
  95. handling (which is suboptimal). */
  96. #if !defined(GFC_REAL_16_IS_FLOAT128)
  97. # if defined (HAVE_GFC_COMPLEX_10)
  98. case GFC_DTYPE_COMPLEX_10:
  99. return internal_pack_c10 ((gfc_array_c10 *) source);
  100. # endif
  101. # if defined (HAVE_GFC_COMPLEX_16)
  102. case GFC_DTYPE_COMPLEX_16:
  103. return internal_pack_c16 ((gfc_array_c16 *) source);
  104. # endif
  105. #endif
  106. case GFC_DTYPE_DERIVED_2:
  107. if (GFC_UNALIGNED_2(source->base_addr))
  108. break;
  109. else
  110. return internal_pack_2 ((gfc_array_i2 *) source);
  111. case GFC_DTYPE_DERIVED_4:
  112. if (GFC_UNALIGNED_4(source->base_addr))
  113. break;
  114. else
  115. return internal_pack_4 ((gfc_array_i4 *) source);
  116. case GFC_DTYPE_DERIVED_8:
  117. if (GFC_UNALIGNED_8(source->base_addr))
  118. break;
  119. else
  120. return internal_pack_8 ((gfc_array_i8 *) source);
  121. #ifdef HAVE_GFC_INTEGER_16
  122. case GFC_DTYPE_DERIVED_16:
  123. if (GFC_UNALIGNED_16(source->base_addr))
  124. break;
  125. else
  126. return internal_pack_16 ((gfc_array_i16 *) source);
  127. #endif
  128. default:
  129. break;
  130. }
  131. dim = GFC_DESCRIPTOR_RANK (source);
  132. ssize = 1;
  133. packed = 1;
  134. for (n = 0; n < dim; n++)
  135. {
  136. count[n] = 0;
  137. stride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
  138. extent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
  139. if (extent[n] <= 0)
  140. {
  141. /* Do nothing. */
  142. packed = 1;
  143. break;
  144. }
  145. if (ssize != stride[n])
  146. packed = 0;
  147. ssize *= extent[n];
  148. }
  149. if (packed)
  150. return source->base_addr;
  151. /* Allocate storage for the destination. */
  152. destptr = xmallocarray (ssize, size);
  153. dest = (char *)destptr;
  154. src = source->base_addr;
  155. stride0 = stride[0] * size;
  156. while (src)
  157. {
  158. /* Copy the data. */
  159. memcpy(dest, src, size);
  160. /* Advance to the next element. */
  161. dest += size;
  162. src += stride0;
  163. count[0]++;
  164. /* Advance to the next source element. */
  165. n = 0;
  166. while (count[n] == extent[n])
  167. {
  168. /* When we get to the end of a dimension, reset it and increment
  169. the next dimension. */
  170. count[n] = 0;
  171. /* We could precalculate these products, but this is a less
  172. frequently used path so probably not worth it. */
  173. src -= stride[n] * extent[n] * size;
  174. n++;
  175. if (n == dim)
  176. {
  177. src = NULL;
  178. break;
  179. }
  180. else
  181. {
  182. count[n]++;
  183. src += stride[n] * size;
  184. }
  185. }
  186. }
  187. return destptr;
  188. }