product_c16.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Implementation of the PRODUCT 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_COMPLEX_16) && defined (HAVE_GFC_COMPLEX_16)
  24. extern void product_c16 (gfc_array_c16 * const restrict,
  25. gfc_array_c16 * const restrict, const index_type * const restrict);
  26. export_proto(product_c16);
  27. void
  28. product_c16 (gfc_array_c16 * const restrict retarray,
  29. gfc_array_c16 * const restrict array,
  30. const index_type * const restrict pdim)
  31. {
  32. index_type count[GFC_MAX_DIMENSIONS];
  33. index_type extent[GFC_MAX_DIMENSIONS];
  34. index_type sstride[GFC_MAX_DIMENSIONS];
  35. index_type dstride[GFC_MAX_DIMENSIONS];
  36. const GFC_COMPLEX_16 * restrict base;
  37. GFC_COMPLEX_16 * restrict dest;
  38. index_type rank;
  39. index_type n;
  40. index_type len;
  41. index_type delta;
  42. index_type dim;
  43. int continue_loop;
  44. /* Make dim zero based to avoid confusion. */
  45. dim = (*pdim) - 1;
  46. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  47. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  48. if (len < 0)
  49. len = 0;
  50. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  51. for (n = 0; n < dim; n++)
  52. {
  53. sstride[n] = GFC_DESCRIPTOR_STRIDE(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(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. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_COMPLEX_16));
  80. if (alloc_size == 0)
  81. {
  82. /* Make sure we have a zero-sized array. */
  83. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  84. return;
  85. }
  86. }
  87. else
  88. {
  89. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  90. runtime_error ("rank of return array incorrect in"
  91. " PRODUCT intrinsic: is %ld, should be %ld",
  92. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  93. (long int) rank);
  94. if (unlikely (compile_options.bounds_check))
  95. bounds_ifunction_return ((array_t *) retarray, extent,
  96. "return value", "PRODUCT");
  97. }
  98. for (n = 0; n < rank; n++)
  99. {
  100. count[n] = 0;
  101. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  102. if (extent[n] <= 0)
  103. return;
  104. }
  105. base = array->base_addr;
  106. dest = retarray->base_addr;
  107. continue_loop = 1;
  108. while (continue_loop)
  109. {
  110. const GFC_COMPLEX_16 * restrict src;
  111. GFC_COMPLEX_16 result;
  112. src = base;
  113. {
  114. result = 1;
  115. if (len <= 0)
  116. *dest = 1;
  117. else
  118. {
  119. for (n = 0; n < len; n++, src += delta)
  120. {
  121. result *= *src;
  122. }
  123. *dest = result;
  124. }
  125. }
  126. /* Advance to the next element. */
  127. count[0]++;
  128. base += sstride[0];
  129. dest += dstride[0];
  130. n = 0;
  131. while (count[n] == extent[n])
  132. {
  133. /* When we get to the end of a dimension, reset it and increment
  134. the next dimension. */
  135. count[n] = 0;
  136. /* We could precalculate these products, but this is a less
  137. frequently used path so probably not worth it. */
  138. base -= sstride[n] * extent[n];
  139. dest -= dstride[n] * extent[n];
  140. n++;
  141. if (n == rank)
  142. {
  143. /* Break out of the look. */
  144. continue_loop = 0;
  145. break;
  146. }
  147. else
  148. {
  149. count[n]++;
  150. base += sstride[n];
  151. dest += dstride[n];
  152. }
  153. }
  154. }
  155. }
  156. extern void mproduct_c16 (gfc_array_c16 * const restrict,
  157. gfc_array_c16 * const restrict, const index_type * const restrict,
  158. gfc_array_l1 * const restrict);
  159. export_proto(mproduct_c16);
  160. void
  161. mproduct_c16 (gfc_array_c16 * const restrict retarray,
  162. gfc_array_c16 * const restrict array,
  163. const index_type * const restrict pdim,
  164. gfc_array_l1 * const restrict mask)
  165. {
  166. index_type count[GFC_MAX_DIMENSIONS];
  167. index_type extent[GFC_MAX_DIMENSIONS];
  168. index_type sstride[GFC_MAX_DIMENSIONS];
  169. index_type dstride[GFC_MAX_DIMENSIONS];
  170. index_type mstride[GFC_MAX_DIMENSIONS];
  171. GFC_COMPLEX_16 * restrict dest;
  172. const GFC_COMPLEX_16 * restrict base;
  173. const GFC_LOGICAL_1 * restrict mbase;
  174. int rank;
  175. int dim;
  176. index_type n;
  177. index_type len;
  178. index_type delta;
  179. index_type mdelta;
  180. int mask_kind;
  181. dim = (*pdim) - 1;
  182. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  183. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  184. if (len <= 0)
  185. return;
  186. mbase = mask->base_addr;
  187. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  188. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  189. #ifdef HAVE_GFC_LOGICAL_16
  190. || mask_kind == 16
  191. #endif
  192. )
  193. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  194. else
  195. runtime_error ("Funny sized logical array");
  196. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  197. mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
  198. for (n = 0; n < dim; n++)
  199. {
  200. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  201. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  202. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  203. if (extent[n] < 0)
  204. extent[n] = 0;
  205. }
  206. for (n = dim; n < rank; n++)
  207. {
  208. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
  209. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
  210. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  211. if (extent[n] < 0)
  212. extent[n] = 0;
  213. }
  214. if (retarray->base_addr == NULL)
  215. {
  216. size_t alloc_size, str;
  217. for (n = 0; n < rank; n++)
  218. {
  219. if (n == 0)
  220. str = 1;
  221. else
  222. str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  223. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  224. }
  225. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  226. retarray->offset = 0;
  227. retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
  228. if (alloc_size == 0)
  229. {
  230. /* Make sure we have a zero-sized array. */
  231. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  232. return;
  233. }
  234. else
  235. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_COMPLEX_16));
  236. }
  237. else
  238. {
  239. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  240. runtime_error ("rank of return array incorrect in PRODUCT intrinsic");
  241. if (unlikely (compile_options.bounds_check))
  242. {
  243. bounds_ifunction_return ((array_t *) retarray, extent,
  244. "return value", "PRODUCT");
  245. bounds_equal_extents ((array_t *) mask, (array_t *) array,
  246. "MASK argument", "PRODUCT");
  247. }
  248. }
  249. for (n = 0; n < rank; n++)
  250. {
  251. count[n] = 0;
  252. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  253. if (extent[n] <= 0)
  254. return;
  255. }
  256. dest = retarray->base_addr;
  257. base = array->base_addr;
  258. while (base)
  259. {
  260. const GFC_COMPLEX_16 * restrict src;
  261. const GFC_LOGICAL_1 * restrict msrc;
  262. GFC_COMPLEX_16 result;
  263. src = base;
  264. msrc = mbase;
  265. {
  266. result = 1;
  267. for (n = 0; n < len; n++, src += delta, msrc += mdelta)
  268. {
  269. if (*msrc)
  270. result *= *src;
  271. }
  272. *dest = result;
  273. }
  274. /* Advance to the next element. */
  275. count[0]++;
  276. base += sstride[0];
  277. mbase += mstride[0];
  278. dest += dstride[0];
  279. n = 0;
  280. while (count[n] == extent[n])
  281. {
  282. /* When we get to the end of a dimension, reset it and increment
  283. the next dimension. */
  284. count[n] = 0;
  285. /* We could precalculate these products, but this is a less
  286. frequently used path so probably not worth it. */
  287. base -= sstride[n] * extent[n];
  288. mbase -= mstride[n] * extent[n];
  289. dest -= dstride[n] * extent[n];
  290. n++;
  291. if (n == rank)
  292. {
  293. /* Break out of the look. */
  294. base = NULL;
  295. break;
  296. }
  297. else
  298. {
  299. count[n]++;
  300. base += sstride[n];
  301. mbase += mstride[n];
  302. dest += dstride[n];
  303. }
  304. }
  305. }
  306. }
  307. extern void sproduct_c16 (gfc_array_c16 * const restrict,
  308. gfc_array_c16 * const restrict, const index_type * const restrict,
  309. GFC_LOGICAL_4 *);
  310. export_proto(sproduct_c16);
  311. void
  312. sproduct_c16 (gfc_array_c16 * const restrict retarray,
  313. gfc_array_c16 * const restrict array,
  314. const index_type * const restrict pdim,
  315. GFC_LOGICAL_4 * mask)
  316. {
  317. index_type count[GFC_MAX_DIMENSIONS];
  318. index_type extent[GFC_MAX_DIMENSIONS];
  319. index_type dstride[GFC_MAX_DIMENSIONS];
  320. GFC_COMPLEX_16 * restrict dest;
  321. index_type rank;
  322. index_type n;
  323. index_type dim;
  324. if (*mask)
  325. {
  326. product_c16 (retarray, array, pdim);
  327. return;
  328. }
  329. /* Make dim zero based to avoid confusion. */
  330. dim = (*pdim) - 1;
  331. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  332. for (n = 0; n < dim; n++)
  333. {
  334. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  335. if (extent[n] <= 0)
  336. extent[n] = 0;
  337. }
  338. for (n = dim; n < rank; n++)
  339. {
  340. extent[n] =
  341. GFC_DESCRIPTOR_EXTENT(array,n + 1);
  342. if (extent[n] <= 0)
  343. extent[n] = 0;
  344. }
  345. if (retarray->base_addr == NULL)
  346. {
  347. size_t alloc_size, str;
  348. for (n = 0; n < rank; n++)
  349. {
  350. if (n == 0)
  351. str = 1;
  352. else
  353. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  354. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  355. }
  356. retarray->offset = 0;
  357. retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
  358. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  359. if (alloc_size == 0)
  360. {
  361. /* Make sure we have a zero-sized array. */
  362. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  363. return;
  364. }
  365. else
  366. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_COMPLEX_16));
  367. }
  368. else
  369. {
  370. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  371. runtime_error ("rank of return array incorrect in"
  372. " PRODUCT intrinsic: is %ld, should be %ld",
  373. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  374. (long int) rank);
  375. if (unlikely (compile_options.bounds_check))
  376. {
  377. for (n=0; n < rank; n++)
  378. {
  379. index_type ret_extent;
  380. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
  381. if (extent[n] != ret_extent)
  382. runtime_error ("Incorrect extent in return value of"
  383. " PRODUCT intrinsic in dimension %ld:"
  384. " is %ld, should be %ld", (long int) n + 1,
  385. (long int) ret_extent, (long int) extent[n]);
  386. }
  387. }
  388. }
  389. for (n = 0; n < rank; n++)
  390. {
  391. count[n] = 0;
  392. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  393. }
  394. dest = retarray->base_addr;
  395. while(1)
  396. {
  397. *dest = 1;
  398. count[0]++;
  399. dest += dstride[0];
  400. n = 0;
  401. while (count[n] == extent[n])
  402. {
  403. /* When we get to the end of a dimension, reset it and increment
  404. the next dimension. */
  405. count[n] = 0;
  406. /* We could precalculate these products, but this is a less
  407. frequently used path so probably not worth it. */
  408. dest -= dstride[n] * extent[n];
  409. n++;
  410. if (n == rank)
  411. return;
  412. else
  413. {
  414. count[n]++;
  415. dest += dstride[n];
  416. }
  417. }
  418. }
  419. }
  420. #endif