ifunction_logical.m4 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. dnl Support macro file for intrinsic functions.
  2. dnl Contains the generic sections of the array functions.
  3. dnl This file is part of the GNU Fortran Runtime Library (libgfortran)
  4. dnl Distributed under the GNU GPL with exception. See COPYING for details.
  5. dnl
  6. dnl Pass the implementation for a single section as the parameter to
  7. dnl {MASK_}ARRAY_FUNCTION.
  8. dnl The variables base, delta, and len describe the input section.
  9. dnl For masked section the mask is described by mbase and mdelta.
  10. dnl These should not be modified. The result should be stored in *dest.
  11. dnl The names count, extent, sstride, dstride, base, dest, rank, dim
  12. dnl retarray, array, pdim and mstride should not be used.
  13. dnl The variable n is declared as index_type and may be used.
  14. dnl Other variable declarations may be placed at the start of the code,
  15. dnl The types of the array parameter and the return value are
  16. dnl atype_name and rtype_name respectively.
  17. dnl Execution should be allowed to continue to the end of the block.
  18. dnl You should not return or break from the inner loop of the implementation.
  19. dnl Care should also be taken to avoid using the names defined in iparm.m4
  20. define(START_ARRAY_FUNCTION,
  21. `
  22. extern void name`'rtype_qual`_'atype_code (rtype * const restrict,
  23. gfc_array_l1 * const restrict, const index_type * const restrict);
  24. export_proto(name`'rtype_qual`_'atype_code);
  25. void
  26. name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  27. gfc_array_l1 * const restrict array,
  28. const index_type * const restrict pdim)
  29. {
  30. index_type count[GFC_MAX_DIMENSIONS];
  31. index_type extent[GFC_MAX_DIMENSIONS];
  32. index_type sstride[GFC_MAX_DIMENSIONS];
  33. index_type dstride[GFC_MAX_DIMENSIONS];
  34. const GFC_LOGICAL_1 * restrict base;
  35. rtype_name * restrict dest;
  36. index_type rank;
  37. index_type n;
  38. index_type len;
  39. index_type delta;
  40. index_type dim;
  41. int src_kind;
  42. int continue_loop;
  43. /* Make dim zero based to avoid confusion. */
  44. dim = (*pdim) - 1;
  45. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  46. src_kind = GFC_DESCRIPTOR_SIZE (array);
  47. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  48. if (len < 0)
  49. len = 0;
  50. delta = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  51. for (n = 0; n < dim; n++)
  52. {
  53. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
  54. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  55. if (extent[n] < 0)
  56. extent[n] = 0;
  57. }
  58. for (n = dim; n < rank; n++)
  59. {
  60. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n + 1);
  61. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n + 1);
  62. if (extent[n] < 0)
  63. extent[n] = 0;
  64. }
  65. if (retarray->base_addr == NULL)
  66. {
  67. size_t alloc_size, str;
  68. for (n = 0; n < rank; n++)
  69. {
  70. if (n == 0)
  71. str = 1;
  72. else
  73. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  74. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  75. }
  76. retarray->offset = 0;
  77. retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
  78. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  79. if (alloc_size == 0)
  80. {
  81. /* Make sure we have a zero-sized array. */
  82. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  83. return;
  84. }
  85. else
  86. retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name));
  87. }
  88. else
  89. {
  90. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  91. runtime_error ("rank of return array incorrect in"
  92. " u_name intrinsic: is %ld, should be %ld",
  93. (long int) GFC_DESCRIPTOR_RANK (retarray),
  94. (long int) rank);
  95. if (unlikely (compile_options.bounds_check))
  96. {
  97. for (n=0; n < rank; n++)
  98. {
  99. index_type ret_extent;
  100. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
  101. if (extent[n] != ret_extent)
  102. runtime_error ("Incorrect extent in return value of"
  103. " u_name intrinsic in dimension %d:"
  104. " is %ld, should be %ld", (int) n + 1,
  105. (long int) ret_extent, (long int) extent[n]);
  106. }
  107. }
  108. }
  109. for (n = 0; n < rank; n++)
  110. {
  111. count[n] = 0;
  112. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  113. if (extent[n] <= 0)
  114. return;
  115. }
  116. base = array->base_addr;
  117. if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
  118. #ifdef HAVE_GFC_LOGICAL_16
  119. || src_kind == 16
  120. #endif
  121. )
  122. {
  123. if (base)
  124. base = GFOR_POINTER_TO_L1 (base, src_kind);
  125. }
  126. else
  127. internal_error (NULL, "Funny sized logical array in u_name intrinsic");
  128. dest = retarray->base_addr;
  129. continue_loop = 1;
  130. while (continue_loop)
  131. {
  132. const GFC_LOGICAL_1 * restrict src;
  133. rtype_name result;
  134. src = base;
  135. {
  136. ')dnl
  137. define(START_ARRAY_BLOCK,
  138. ` if (len <= 0)
  139. *dest = '$1`;
  140. else
  141. {
  142. for (n = 0; n < len; n++, src += delta)
  143. {
  144. ')dnl
  145. define(FINISH_ARRAY_FUNCTION,
  146. ` }
  147. *dest = result;
  148. }
  149. }
  150. /* Advance to the next element. */
  151. count[0]++;
  152. base += sstride[0];
  153. dest += dstride[0];
  154. n = 0;
  155. while (count[n] == extent[n])
  156. {
  157. /* When we get to the end of a dimension, reset it and increment
  158. the next dimension. */
  159. count[n] = 0;
  160. /* We could precalculate these products, but this is a less
  161. frequently used path so probably not worth it. */
  162. base -= sstride[n] * extent[n];
  163. dest -= dstride[n] * extent[n];
  164. n++;
  165. if (n == rank)
  166. {
  167. /* Break out of the look. */
  168. continue_loop = 0;
  169. break;
  170. }
  171. else
  172. {
  173. count[n]++;
  174. base += sstride[n];
  175. dest += dstride[n];
  176. }
  177. }
  178. }
  179. }')dnl
  180. define(ARRAY_FUNCTION,
  181. `START_ARRAY_FUNCTION
  182. $2
  183. START_ARRAY_BLOCK($1)
  184. $3
  185. FINISH_ARRAY_FUNCTION')dnl