transpose_generic.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Implementation of the TRANSPOSE intrinsic
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Tobias Schlüter
  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 <string.h>
  23. #include <assert.h>
  24. extern void transpose (gfc_array_char *, gfc_array_char *);
  25. export_proto(transpose);
  26. static void
  27. transpose_internal (gfc_array_char *ret, gfc_array_char *source)
  28. {
  29. /* r.* indicates the return array. */
  30. index_type rxstride, rystride;
  31. char *rptr;
  32. /* s.* indicates the source array. */
  33. index_type sxstride, systride;
  34. const char *sptr;
  35. index_type xcount, ycount;
  36. index_type x, y;
  37. index_type size;
  38. assert (GFC_DESCRIPTOR_RANK (source) == 2
  39. && GFC_DESCRIPTOR_RANK (ret) == 2);
  40. size = GFC_DESCRIPTOR_SIZE(ret);
  41. if (ret->base_addr == NULL)
  42. {
  43. assert (ret->dtype == source->dtype);
  44. GFC_DIMENSION_SET(ret->dim[0], 0, GFC_DESCRIPTOR_EXTENT(source,1) - 1,
  45. 1);
  46. GFC_DIMENSION_SET(ret->dim[1], 0, GFC_DESCRIPTOR_EXTENT(source,0) - 1,
  47. GFC_DESCRIPTOR_EXTENT(source, 1));
  48. ret->base_addr = xmallocarray (size0 ((array_t*)ret), size);
  49. ret->offset = 0;
  50. }
  51. else if (unlikely (compile_options.bounds_check))
  52. {
  53. index_type ret_extent, src_extent;
  54. ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
  55. src_extent = GFC_DESCRIPTOR_EXTENT(source,1);
  56. if (src_extent != ret_extent)
  57. runtime_error ("Incorrect extent in return value of TRANSPOSE"
  58. " intrinsic in dimension 1: is %ld,"
  59. " should be %ld", (long int) src_extent,
  60. (long int) ret_extent);
  61. ret_extent = GFC_DESCRIPTOR_EXTENT(ret,1);
  62. src_extent = GFC_DESCRIPTOR_EXTENT(source,0);
  63. if (src_extent != ret_extent)
  64. runtime_error ("Incorrect extent in return value of TRANSPOSE"
  65. " intrinsic in dimension 2: is %ld,"
  66. " should be %ld", (long int) src_extent,
  67. (long int) ret_extent);
  68. }
  69. sxstride = GFC_DESCRIPTOR_STRIDE_BYTES(source,0);
  70. systride = GFC_DESCRIPTOR_STRIDE_BYTES(source,1);
  71. xcount = GFC_DESCRIPTOR_EXTENT(source,0);
  72. ycount = GFC_DESCRIPTOR_EXTENT(source,1);
  73. rxstride = GFC_DESCRIPTOR_STRIDE_BYTES(ret,0);
  74. rystride = GFC_DESCRIPTOR_STRIDE_BYTES(ret,1);
  75. rptr = ret->base_addr;
  76. sptr = source->base_addr;
  77. for (y = 0; y < ycount; y++)
  78. {
  79. for (x = 0; x < xcount; x++)
  80. {
  81. memcpy (rptr, sptr, size);
  82. sptr += sxstride;
  83. rptr += rystride;
  84. }
  85. sptr += systride - (sxstride * xcount);
  86. rptr += rxstride - (rystride * xcount);
  87. }
  88. }
  89. extern void transpose (gfc_array_char *, gfc_array_char *);
  90. export_proto(transpose);
  91. void
  92. transpose (gfc_array_char *ret, gfc_array_char *source)
  93. {
  94. transpose_internal (ret, source);
  95. }
  96. extern void transpose_char (gfc_array_char *, GFC_INTEGER_4,
  97. gfc_array_char *, GFC_INTEGER_4);
  98. export_proto(transpose_char);
  99. void
  100. transpose_char (gfc_array_char *ret,
  101. GFC_INTEGER_4 ret_length __attribute__((unused)),
  102. gfc_array_char *source,
  103. GFC_INTEGER_4 source_length __attribute__((unused)))
  104. {
  105. transpose_internal (ret, source);
  106. }
  107. extern void transpose_char4 (gfc_array_char *, GFC_INTEGER_4,
  108. gfc_array_char *, GFC_INTEGER_4);
  109. export_proto(transpose_char4);
  110. void
  111. transpose_char4 (gfc_array_char *ret,
  112. GFC_INTEGER_4 ret_length __attribute__((unused)),
  113. gfc_array_char *source,
  114. GFC_INTEGER_4 source_length __attribute__((unused)))
  115. {
  116. transpose_internal (ret, source);
  117. }