OSDN Git Service

* m4/minloc1.m4: Update copyright year and ajust headers order.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / in_pack_generic.c
1 /* Generic helper function for repacking arrays.
2    Copyright 2003, 2004, 2005, 2007  Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
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 2 of the License, or (at your option) any later version.
11
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.)
20
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.
25
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.  */
30
31 #include "libgfortran.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35
36 extern void *internal_pack (gfc_array_char *);
37 export_proto(internal_pack);
38
39 void *
40 internal_pack (gfc_array_char * source)
41 {
42   index_type count[GFC_MAX_DIMENSIONS];
43   index_type extent[GFC_MAX_DIMENSIONS];
44   index_type stride[GFC_MAX_DIMENSIONS];
45   index_type stride0;
46   index_type dim;
47   index_type ssize;
48   const char *src;
49   char *dest;
50   void *destptr;
51   int n;
52   int packed;
53   index_type size;
54   int type;
55
56   if (source->dim[0].stride == 0)
57     {
58       source->dim[0].stride = 1;
59       return source->data;
60     }
61
62   type = GFC_DESCRIPTOR_TYPE (source);
63   size = GFC_DESCRIPTOR_SIZE (source);
64   switch (type)
65     {
66     case GFC_DTYPE_INTEGER:
67     case GFC_DTYPE_LOGICAL:
68     case GFC_DTYPE_REAL:
69       switch (size)
70         {
71         case 4:
72           return internal_pack_4 ((gfc_array_i4 *)source);
73           
74         case 8:
75           return internal_pack_8 ((gfc_array_i8 *)source);
76         }
77       break;
78
79     case GFC_DTYPE_COMPLEX:
80       switch (size)
81         {
82         case 8:
83           return internal_pack_c4 ((gfc_array_c4 *)source);
84           
85         case 16:
86           return internal_pack_c8 ((gfc_array_c8 *)source);
87         }
88       break;
89
90     default:
91       break;
92     }
93
94   dim = GFC_DESCRIPTOR_RANK (source);
95   ssize = 1;
96   packed = 1;
97   for (n = 0; n < dim; n++)
98     {
99       count[n] = 0;
100       stride[n] = source->dim[n].stride;
101       extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
102       if (extent[n] <= 0)
103         {
104           /* Do nothing.  */
105           packed = 1;
106           break;
107         }
108
109       if (ssize != stride[n])
110         packed = 0;
111
112       ssize *= extent[n];
113     }
114
115   if (packed)
116     return source->data;
117
118    /* Allocate storage for the destination.  */
119   destptr = internal_malloc_size (ssize * size);
120   dest = (char *)destptr;
121   src = source->data;
122   stride0 = stride[0] * size;
123
124   while (src)
125     {
126       /* Copy the data.  */
127       memcpy(dest, src, size);
128       /* Advance to the next element.  */
129       dest += size;
130       src += stride0;
131       count[0]++;
132       /* Advance to the next source element.  */
133       n = 0;
134       while (count[n] == extent[n])
135         {
136           /* When we get to the end of a dimension, reset it and increment
137              the next dimension.  */
138           count[n] = 0;
139           /* We could precalculate these products, but this is a less
140              frequently used path so probably not worth it.  */
141           src -= stride[n] * extent[n] * size;
142           n++;
143           if (n == dim)
144             {
145               src = NULL;
146               break;
147             }
148           else
149             {
150               count[n]++;
151               src += stride[n] * size;
152             }
153         }
154     }
155   return destptr;
156 }