* global.c (global_alloc): Make it static.
[official-gcc.git] / libgfortran / intrinsics / transpose_generic.c
blobbd47073790b5140183a6bd8e99253b65bd587fa4
1 /* Implementation of the TRANSPOSE intrinsic
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Tobias Schlüter
5 This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include "libgfortran.h"
37 extern void transpose (gfc_array_char *, gfc_array_char *);
38 export_proto(transpose);
40 static void
41 transpose_internal (gfc_array_char *ret, gfc_array_char *source,
42 index_type size)
44 /* r.* indicates the return array. */
45 index_type rxstride, rystride;
46 char *rptr;
47 /* s.* indicates the source array. */
48 index_type sxstride, systride;
49 const char *sptr;
51 index_type xcount, ycount;
52 index_type x, y;
54 assert (GFC_DESCRIPTOR_RANK (source) == 2
55 && GFC_DESCRIPTOR_RANK (ret) == 2);
57 if (ret->data == NULL)
59 assert (ret->dtype == source->dtype);
61 ret->dim[0].lbound = 0;
62 ret->dim[0].ubound = source->dim[1].ubound - source->dim[1].lbound;
63 ret->dim[0].stride = 1;
65 ret->dim[1].lbound = 0;
66 ret->dim[1].ubound = source->dim[0].ubound - source->dim[0].lbound;
67 ret->dim[1].stride = ret->dim[0].ubound+1;
69 ret->data = internal_malloc_size (size * size0 ((array_t*)ret));
70 ret->offset = 0;
73 sxstride = source->dim[0].stride * size;
74 if (sxstride == 0)
75 sxstride = size;
76 systride = source->dim[1].stride * size;
77 xcount = source->dim[0].ubound + 1 - source->dim[0].lbound;
78 ycount = source->dim[1].ubound + 1 - source->dim[1].lbound;
80 rxstride = ret->dim[0].stride * size;
81 if (rxstride == 0)
82 rxstride = size;
83 rystride = ret->dim[1].stride * size;
85 rptr = ret->data;
86 sptr = source->data;
88 for (y = 0; y < ycount; y++)
90 for (x = 0; x < xcount; x++)
92 memcpy (rptr, sptr, size);
94 sptr += sxstride;
95 rptr += rystride;
97 sptr += systride - (sxstride * xcount);
98 rptr += rxstride - (rystride * xcount);
102 extern void transpose (gfc_array_char *, gfc_array_char *);
103 export_proto(transpose);
105 void
106 transpose (gfc_array_char *ret, gfc_array_char *source)
108 transpose_internal (ret, source, GFC_DESCRIPTOR_SIZE (source));
111 extern void transpose_char (gfc_array_char *, GFC_INTEGER_4,
112 gfc_array_char *, GFC_INTEGER_4);
113 export_proto(transpose_char);
115 void
116 transpose_char (gfc_array_char *ret,
117 GFC_INTEGER_4 ret_length __attribute__((unused)),
118 gfc_array_char *source, GFC_INTEGER_4 source_length)
120 transpose_internal (ret, source, source_length);