matmul_l4.c 6.8 KB

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