OSDN Git Service

Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / transpose_generic.c
1 /* Implementation of the TRANSPOSE intrinsic
2    Copyright 2003, 2006, 2007, 2009 Free Software Foundation, Inc.
3    Contributed by Tobias Schlüter
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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.
16
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.
20
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/>.  */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30
31 extern void transpose (gfc_array_char *, gfc_array_char *);
32 export_proto(transpose);
33
34 static void
35 transpose_internal (gfc_array_char *ret, gfc_array_char *source,
36                     index_type size)
37 {
38   /* r.* indicates the return array.  */
39   index_type rxstride, rystride;
40   char *rptr;
41   /* s.* indicates the source array.  */
42   index_type sxstride, systride;
43   const char *sptr;
44
45   index_type xcount, ycount;
46   index_type x, y;
47
48   assert (GFC_DESCRIPTOR_RANK (source) == 2
49           && GFC_DESCRIPTOR_RANK (ret) == 2);
50
51   if (ret->data == NULL)
52     {
53       assert (ret->dtype == source->dtype);
54
55       ret->dim[0].lbound = 0;
56       ret->dim[0].ubound = source->dim[1].ubound - source->dim[1].lbound;
57       ret->dim[0].stride = 1;
58
59       ret->dim[1].lbound = 0;
60       ret->dim[1].ubound = source->dim[0].ubound - source->dim[0].lbound;
61       ret->dim[1].stride = ret->dim[0].ubound+1;
62
63       ret->data = internal_malloc_size (size * size0 ((array_t*)ret));
64       ret->offset = 0;
65     }
66   else if (unlikely (compile_options.bounds_check))
67     {
68       index_type ret_extent, src_extent;
69
70       ret_extent = ret->dim[0].ubound + 1 - ret->dim[0].lbound;
71       src_extent = source->dim[1].ubound + 1 - source->dim[1].lbound;
72
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);
78
79       ret_extent = ret->dim[1].ubound + 1 - ret->dim[1].lbound;
80       src_extent = source->dim[0].ubound + 1 - source->dim[0].lbound;
81
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);
87
88     }
89
90   sxstride = source->dim[0].stride * size;
91   systride = source->dim[1].stride * size;
92   xcount = source->dim[0].ubound + 1 - source->dim[0].lbound;
93   ycount = source->dim[1].ubound + 1 - source->dim[1].lbound;
94
95   rxstride = ret->dim[0].stride * size;
96   rystride = ret->dim[1].stride * size;
97
98   rptr = ret->data;
99   sptr = source->data;
100
101   for (y = 0; y < ycount; y++)
102     {
103       for (x = 0; x < xcount; x++)
104         {
105           memcpy (rptr, sptr, size);
106
107           sptr += sxstride;
108           rptr += rystride;
109         }
110       sptr += systride - (sxstride * xcount);
111       rptr += rxstride - (rystride * xcount);
112     }
113 }
114
115
116 extern void transpose (gfc_array_char *, gfc_array_char *);
117 export_proto(transpose);
118
119 void
120 transpose (gfc_array_char *ret, gfc_array_char *source)
121 {
122   transpose_internal (ret, source, GFC_DESCRIPTOR_SIZE (source));
123 }
124
125
126 extern void transpose_char (gfc_array_char *, GFC_INTEGER_4,
127                             gfc_array_char *, GFC_INTEGER_4);
128 export_proto(transpose_char);
129
130 void
131 transpose_char (gfc_array_char *ret,
132                 GFC_INTEGER_4 ret_length __attribute__((unused)),
133                 gfc_array_char *source, GFC_INTEGER_4 source_length)
134 {
135   transpose_internal (ret, source, source_length);
136 }
137
138
139 extern void transpose_char4 (gfc_array_char *, GFC_INTEGER_4,
140                              gfc_array_char *, GFC_INTEGER_4);
141 export_proto(transpose_char4);
142
143 void
144 transpose_char4 (gfc_array_char *ret,
145                  GFC_INTEGER_4 ret_length __attribute__((unused)),
146                  gfc_array_char *source, GFC_INTEGER_4 source_length)
147 {
148   transpose_internal (ret, source, source_length * sizeof (gfc_char4_t));
149 }