Fix r216010 fallout
[official-gcc.git] / libgfortran / intrinsics / transpose_generic.c
bloba5e2947a6cf96d86f1bb9f3e286309e826552334
1 /* Implementation of the TRANSPOSE intrinsic
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Tobias Schlüter
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
31 extern void transpose (gfc_array_char *, gfc_array_char *);
32 export_proto(transpose);
34 static void
35 transpose_internal (gfc_array_char *ret, gfc_array_char *source)
37 /* r.* indicates the return array. */
38 index_type rxstride, rystride;
39 char *rptr;
40 /* s.* indicates the source array. */
41 index_type sxstride, systride;
42 const char *sptr;
44 index_type xcount, ycount;
45 index_type x, y;
46 index_type size;
48 assert (GFC_DESCRIPTOR_RANK (source) == 2
49 && GFC_DESCRIPTOR_RANK (ret) == 2);
51 size = GFC_DESCRIPTOR_SIZE(ret);
53 if (ret->base_addr == NULL)
55 assert (ret->dtype == source->dtype);
57 GFC_DIMENSION_SET(ret->dim[0], 0, GFC_DESCRIPTOR_EXTENT(source,1) - 1,
58 1);
60 GFC_DIMENSION_SET(ret->dim[1], 0, GFC_DESCRIPTOR_EXTENT(source,0) - 1,
61 GFC_DESCRIPTOR_EXTENT(source, 1));
63 ret->base_addr = xmallocarray (size0 ((array_t*)ret), size);
64 ret->offset = 0;
66 else if (unlikely (compile_options.bounds_check))
68 index_type ret_extent, src_extent;
70 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
71 src_extent = GFC_DESCRIPTOR_EXTENT(source,1);
73 if (src_extent != ret_extent)
74 runtime_error ("Incorrect extent in return value of TRANSPOSE"
75 " intrinsic in dimension 1: is %ld,"
76 " should be %ld", (long int) src_extent,
77 (long int) ret_extent);
79 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,1);
80 src_extent = GFC_DESCRIPTOR_EXTENT(source,0);
82 if (src_extent != ret_extent)
83 runtime_error ("Incorrect extent in return value of TRANSPOSE"
84 " intrinsic in dimension 2: is %ld,"
85 " should be %ld", (long int) src_extent,
86 (long int) ret_extent);
90 sxstride = GFC_DESCRIPTOR_STRIDE_BYTES(source,0);
91 systride = GFC_DESCRIPTOR_STRIDE_BYTES(source,1);
92 xcount = GFC_DESCRIPTOR_EXTENT(source,0);
93 ycount = GFC_DESCRIPTOR_EXTENT(source,1);
95 rxstride = GFC_DESCRIPTOR_STRIDE_BYTES(ret,0);
96 rystride = GFC_DESCRIPTOR_STRIDE_BYTES(ret,1);
98 rptr = ret->base_addr;
99 sptr = source->base_addr;
101 for (y = 0; y < ycount; y++)
103 for (x = 0; x < xcount; x++)
105 memcpy (rptr, sptr, size);
107 sptr += sxstride;
108 rptr += rystride;
110 sptr += systride - (sxstride * xcount);
111 rptr += rxstride - (rystride * xcount);
116 extern void transpose (gfc_array_char *, gfc_array_char *);
117 export_proto(transpose);
119 void
120 transpose (gfc_array_char *ret, gfc_array_char *source)
122 transpose_internal (ret, source);
126 extern void transpose_char (gfc_array_char *, GFC_INTEGER_4,
127 gfc_array_char *, GFC_INTEGER_4);
128 export_proto(transpose_char);
130 void
131 transpose_char (gfc_array_char *ret,
132 GFC_INTEGER_4 ret_length __attribute__((unused)),
133 gfc_array_char *source,
134 GFC_INTEGER_4 source_length __attribute__((unused)))
136 transpose_internal (ret, source);
140 extern void transpose_char4 (gfc_array_char *, GFC_INTEGER_4,
141 gfc_array_char *, GFC_INTEGER_4);
142 export_proto(transpose_char4);
144 void
145 transpose_char4 (gfc_array_char *ret,
146 GFC_INTEGER_4 ret_length __attribute__((unused)),
147 gfc_array_char *source,
148 GFC_INTEGER_4 source_length __attribute__((unused)))
150 transpose_internal (ret, source);