in_unpack_generic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_unpack (gfc_array_char *, const void *);
  25. export_proto(internal_unpack);
  26. void
  27. internal_unpack (gfc_array_char * d, const void * s)
  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 dsize;
  35. char *dest;
  36. const char *src;
  37. int n;
  38. int size;
  39. int type_size;
  40. dest = d->base_addr;
  41. /* This check may be redundant, but do it anyway. */
  42. if (s == dest || !s)
  43. return;
  44. type_size = GFC_DTYPE_TYPE_SIZE (d);
  45. switch (type_size)
  46. {
  47. case GFC_DTYPE_INTEGER_1:
  48. case GFC_DTYPE_LOGICAL_1:
  49. case GFC_DTYPE_DERIVED_1:
  50. internal_unpack_1 ((gfc_array_i1 *) d, (const GFC_INTEGER_1 *) s);
  51. return;
  52. case GFC_DTYPE_INTEGER_2:
  53. case GFC_DTYPE_LOGICAL_2:
  54. internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
  55. return;
  56. case GFC_DTYPE_INTEGER_4:
  57. case GFC_DTYPE_LOGICAL_4:
  58. internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
  59. return;
  60. case GFC_DTYPE_INTEGER_8:
  61. case GFC_DTYPE_LOGICAL_8:
  62. internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
  63. return;
  64. #if defined (HAVE_GFC_INTEGER_16)
  65. case GFC_DTYPE_INTEGER_16:
  66. case GFC_DTYPE_LOGICAL_16:
  67. internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
  68. return;
  69. #endif
  70. case GFC_DTYPE_REAL_4:
  71. internal_unpack_r4 ((gfc_array_r4 *) d, (const GFC_REAL_4 *) s);
  72. return;
  73. case GFC_DTYPE_REAL_8:
  74. internal_unpack_r8 ((gfc_array_r8 *) d, (const GFC_REAL_8 *) s);
  75. return;
  76. /* FIXME: This here is a hack, which will have to be removed when
  77. the array descriptor is reworked. Currently, we don't store the
  78. kind value for the type, but only the size. Because on targets with
  79. __float128, we have sizeof(logn double) == sizeof(__float128),
  80. we cannot discriminate here and have to fall back to the generic
  81. handling (which is suboptimal). */
  82. #if !defined(GFC_REAL_16_IS_FLOAT128)
  83. # if defined(HAVE_GFC_REAL_10)
  84. case GFC_DTYPE_REAL_10:
  85. internal_unpack_r10 ((gfc_array_r10 *) d, (const GFC_REAL_10 *) s);
  86. return;
  87. # endif
  88. # if defined(HAVE_GFC_REAL_16)
  89. case GFC_DTYPE_REAL_16:
  90. internal_unpack_r16 ((gfc_array_r16 *) d, (const GFC_REAL_16 *) s);
  91. return;
  92. # endif
  93. #endif
  94. case GFC_DTYPE_COMPLEX_4:
  95. internal_unpack_c4 ((gfc_array_c4 *)d, (const GFC_COMPLEX_4 *)s);
  96. return;
  97. case GFC_DTYPE_COMPLEX_8:
  98. internal_unpack_c8 ((gfc_array_c8 *)d, (const GFC_COMPLEX_8 *)s);
  99. return;
  100. /* FIXME: This here is a hack, which will have to be removed when
  101. the array descriptor is reworked. Currently, we don't store the
  102. kind value for the type, but only the size. Because on targets with
  103. __float128, we have sizeof(logn double) == sizeof(__float128),
  104. we cannot discriminate here and have to fall back to the generic
  105. handling (which is suboptimal). */
  106. #if !defined(GFC_REAL_16_IS_FLOAT128)
  107. # if defined(HAVE_GFC_COMPLEX_10)
  108. case GFC_DTYPE_COMPLEX_10:
  109. internal_unpack_c10 ((gfc_array_c10 *) d, (const GFC_COMPLEX_10 *) s);
  110. return;
  111. # endif
  112. # if defined(HAVE_GFC_COMPLEX_16)
  113. case GFC_DTYPE_COMPLEX_16:
  114. internal_unpack_c16 ((gfc_array_c16 *) d, (const GFC_COMPLEX_16 *) s);
  115. return;
  116. # endif
  117. #endif
  118. case GFC_DTYPE_DERIVED_2:
  119. if (GFC_UNALIGNED_2(d->base_addr) || GFC_UNALIGNED_2(s))
  120. break;
  121. else
  122. {
  123. internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
  124. return;
  125. }
  126. case GFC_DTYPE_DERIVED_4:
  127. if (GFC_UNALIGNED_4(d->base_addr) || GFC_UNALIGNED_4(s))
  128. break;
  129. else
  130. {
  131. internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
  132. return;
  133. }
  134. case GFC_DTYPE_DERIVED_8:
  135. if (GFC_UNALIGNED_8(d->base_addr) || GFC_UNALIGNED_8(s))
  136. break;
  137. else
  138. {
  139. internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
  140. return;
  141. }
  142. #ifdef HAVE_GFC_INTEGER_16
  143. case GFC_DTYPE_DERIVED_16:
  144. if (GFC_UNALIGNED_16(d->base_addr) || GFC_UNALIGNED_16(s))
  145. break;
  146. else
  147. {
  148. internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
  149. return;
  150. }
  151. #endif
  152. default:
  153. break;
  154. }
  155. size = GFC_DESCRIPTOR_SIZE (d);
  156. dim = GFC_DESCRIPTOR_RANK (d);
  157. dsize = 1;
  158. for (n = 0; n < dim; n++)
  159. {
  160. count[n] = 0;
  161. stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
  162. extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
  163. if (extent[n] <= 0)
  164. return;
  165. if (dsize == stride[n])
  166. dsize *= extent[n];
  167. else
  168. dsize = 0;
  169. }
  170. src = s;
  171. if (dsize != 0)
  172. {
  173. memcpy (dest, src, dsize * size);
  174. return;
  175. }
  176. stride0 = stride[0] * size;
  177. while (dest)
  178. {
  179. /* Copy the data. */
  180. memcpy (dest, src, size);
  181. /* Advance to the next element. */
  182. src += size;
  183. dest += stride0;
  184. count[0]++;
  185. /* Advance to the next source element. */
  186. n = 0;
  187. while (count[n] == extent[n])
  188. {
  189. /* When we get to the end of a dimension, reset it and increment
  190. the next dimension. */
  191. count[n] = 0;
  192. /* We could precalculate these products, but this is a less
  193. frequently used path so probably not worth it. */
  194. dest -= stride[n] * extent[n] * size;
  195. n++;
  196. if (n == dim)
  197. {
  198. dest = NULL;
  199. break;
  200. }
  201. else
  202. {
  203. count[n]++;
  204. dest += stride[n] * size;
  205. }
  206. }
  207. }
  208. }