matmull.m4 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. `/* Implementation of the MATMUL intrinsic
  2. Copyright (C) 2002-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(iparm.m4)dnl
  24. `#if defined (HAVE_'rtype_name`)
  25. /* Dimensions: retarray(x,y) a(x, count) b(count,y).
  26. Either a or b can be rank 1. In this case x or y is 1. */
  27. extern void matmul_'rtype_code` ('rtype` * const restrict,
  28. gfc_array_l1 * const restrict, gfc_array_l1 * const restrict);
  29. export_proto(matmul_'rtype_code`);
  30. void
  31. matmul_'rtype_code` ('rtype` * const restrict retarray,
  32. gfc_array_l1 * const restrict a, gfc_array_l1 * const restrict b)
  33. {
  34. const GFC_LOGICAL_1 * restrict abase;
  35. const GFC_LOGICAL_1 * restrict bbase;
  36. 'rtype_name` * restrict dest;
  37. index_type rxstride;
  38. index_type rystride;
  39. index_type xcount;
  40. index_type ycount;
  41. index_type xstride;
  42. index_type ystride;
  43. index_type x;
  44. index_type y;
  45. int a_kind;
  46. int b_kind;
  47. const GFC_LOGICAL_1 * restrict pa;
  48. const GFC_LOGICAL_1 * restrict pb;
  49. index_type astride;
  50. index_type bstride;
  51. index_type count;
  52. index_type n;
  53. assert (GFC_DESCRIPTOR_RANK (a) == 2
  54. || GFC_DESCRIPTOR_RANK (b) == 2);
  55. if (retarray->base_addr == NULL)
  56. {
  57. if (GFC_DESCRIPTOR_RANK (a) == 1)
  58. {
  59. GFC_DIMENSION_SET(retarray->dim[0], 0,
  60. GFC_DESCRIPTOR_EXTENT(b,1) - 1, 1);
  61. }
  62. else if (GFC_DESCRIPTOR_RANK (b) == 1)
  63. {
  64. GFC_DIMENSION_SET(retarray->dim[0], 0,
  65. GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
  66. }
  67. else
  68. {
  69. GFC_DIMENSION_SET(retarray->dim[0], 0,
  70. GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
  71. GFC_DIMENSION_SET(retarray->dim[1], 0,
  72. GFC_DESCRIPTOR_EXTENT(b,1) - 1,
  73. GFC_DESCRIPTOR_EXTENT(retarray,0));
  74. }
  75. retarray->base_addr
  76. = xmallocarray (size0 ((array_t *) retarray), sizeof ('rtype_name`));
  77. retarray->offset = 0;
  78. }
  79. else if (unlikely (compile_options.bounds_check))
  80. {
  81. index_type ret_extent, arg_extent;
  82. if (GFC_DESCRIPTOR_RANK (a) == 1)
  83. {
  84. arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
  85. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
  86. if (arg_extent != ret_extent)
  87. runtime_error ("Incorrect extent in return array in"
  88. " MATMUL intrinsic: is %ld, should be %ld",
  89. (long int) ret_extent, (long int) arg_extent);
  90. }
  91. else if (GFC_DESCRIPTOR_RANK (b) == 1)
  92. {
  93. arg_extent = GFC_DESCRIPTOR_EXTENT(a,0);
  94. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
  95. if (arg_extent != ret_extent)
  96. runtime_error ("Incorrect extent in return array in"
  97. " MATMUL intrinsic: is %ld, should be %ld",
  98. (long int) ret_extent, (long int) arg_extent);
  99. }
  100. else
  101. {
  102. arg_extent = GFC_DESCRIPTOR_EXTENT(a,0);
  103. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
  104. if (arg_extent != ret_extent)
  105. runtime_error ("Incorrect extent in return array in"
  106. " MATMUL intrinsic for dimension 1:"
  107. " is %ld, should be %ld",
  108. (long int) ret_extent, (long int) arg_extent);
  109. arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
  110. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,1);
  111. if (arg_extent != ret_extent)
  112. runtime_error ("Incorrect extent in return array in"
  113. " MATMUL intrinsic for dimension 2:"
  114. " is %ld, should be %ld",
  115. (long int) ret_extent, (long int) arg_extent);
  116. }
  117. }
  118. abase = a->base_addr;
  119. a_kind = GFC_DESCRIPTOR_SIZE (a);
  120. if (a_kind == 1 || a_kind == 2 || a_kind == 4 || a_kind == 8
  121. #ifdef HAVE_GFC_LOGICAL_16
  122. || a_kind == 16
  123. #endif
  124. )
  125. abase = GFOR_POINTER_TO_L1 (abase, a_kind);
  126. else
  127. internal_error (NULL, "Funny sized logical array");
  128. bbase = b->base_addr;
  129. b_kind = GFC_DESCRIPTOR_SIZE (b);
  130. if (b_kind == 1 || b_kind == 2 || b_kind == 4 || b_kind == 8
  131. #ifdef HAVE_GFC_LOGICAL_16
  132. || b_kind == 16
  133. #endif
  134. )
  135. bbase = GFOR_POINTER_TO_L1 (bbase, b_kind);
  136. else
  137. internal_error (NULL, "Funny sized logical array");
  138. dest = retarray->base_addr;
  139. '
  140. sinclude(`matmul_asm_'rtype_code`.m4')dnl
  141. `
  142. if (GFC_DESCRIPTOR_RANK (retarray) == 1)
  143. {
  144. rxstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  145. rystride = rxstride;
  146. }
  147. else
  148. {
  149. rxstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  150. rystride = GFC_DESCRIPTOR_STRIDE(retarray,1);
  151. }
  152. /* If we have rank 1 parameters, zero the absent stride, and set the size to
  153. one. */
  154. if (GFC_DESCRIPTOR_RANK (a) == 1)
  155. {
  156. astride = GFC_DESCRIPTOR_STRIDE_BYTES(a,0);
  157. count = GFC_DESCRIPTOR_EXTENT(a,0);
  158. xstride = 0;
  159. rxstride = 0;
  160. xcount = 1;
  161. }
  162. else
  163. {
  164. astride = GFC_DESCRIPTOR_STRIDE_BYTES(a,1);
  165. count = GFC_DESCRIPTOR_EXTENT(a,1);
  166. xstride = GFC_DESCRIPTOR_STRIDE_BYTES(a,0);
  167. xcount = GFC_DESCRIPTOR_EXTENT(a,0);
  168. }
  169. if (GFC_DESCRIPTOR_RANK (b) == 1)
  170. {
  171. bstride = GFC_DESCRIPTOR_STRIDE_BYTES(b,0);
  172. assert(count == GFC_DESCRIPTOR_EXTENT(b,0));
  173. ystride = 0;
  174. rystride = 0;
  175. ycount = 1;
  176. }
  177. else
  178. {
  179. bstride = GFC_DESCRIPTOR_STRIDE_BYTES(b,0);
  180. assert(count == GFC_DESCRIPTOR_EXTENT(b,0));
  181. ystride = GFC_DESCRIPTOR_STRIDE_BYTES(b,1);
  182. ycount = GFC_DESCRIPTOR_EXTENT(b,1);
  183. }
  184. for (y = 0; y < ycount; y++)
  185. {
  186. for (x = 0; x < xcount; x++)
  187. {
  188. /* Do the summation for this element. For real and integer types
  189. this is the same as DOT_PRODUCT. For complex types we use do
  190. a*b, not conjg(a)*b. */
  191. pa = abase;
  192. pb = bbase;
  193. *dest = 0;
  194. for (n = 0; n < count; n++)
  195. {
  196. if (*pa && *pb)
  197. {
  198. *dest = 1;
  199. break;
  200. }
  201. pa += astride;
  202. pb += bstride;
  203. }
  204. dest += rxstride;
  205. abase += xstride;
  206. }
  207. abase -= xstride * xcount;
  208. bbase += ystride;
  209. dest += rystride - (rxstride * xcount);
  210. }
  211. }
  212. #endif
  213. '