OSDN Git Service

2007-01-18 Francois-Xavier Coudert <coudert@clipper.ens.fr>
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / in_unpack_generic.c
1 /* Generic helper function for repacking arrays.
2    Copyright 2003, 2004, 2005 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 "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37 extern void internal_unpack (gfc_array_char *, const void *);
38 export_proto(internal_unpack);
39
40 void
41 internal_unpack (gfc_array_char * d, const void * s)
42 {
43   index_type count[GFC_MAX_DIMENSIONS];
44   index_type extent[GFC_MAX_DIMENSIONS];
45   index_type stride[GFC_MAX_DIMENSIONS];
46   index_type stride0;
47   index_type dim;
48   index_type dsize;
49   char *dest;
50   const char *src;
51   int n;
52   int size;
53   int type;
54
55   dest = d->data;
56   /* This check may be redundant, but do it anyway.  */
57   if (s == dest || !s)
58     return;
59
60   type = GFC_DESCRIPTOR_TYPE (d);
61   size = GFC_DESCRIPTOR_SIZE (d);
62   switch (type)
63     {
64     case GFC_DTYPE_INTEGER:
65     case GFC_DTYPE_LOGICAL:
66     case GFC_DTYPE_REAL:
67       switch (size)
68         {
69         case 4:
70           internal_unpack_4 ((gfc_array_i4 *)d, (const GFC_INTEGER_4 *)s);
71           return;
72
73         case 8:
74           internal_unpack_8 ((gfc_array_i8 *)d, (const GFC_INTEGER_8 *)s);
75           return;
76         }
77       break;
78
79     case GFC_DTYPE_COMPLEX:
80       switch (size) 
81         {
82         case 8:
83           internal_unpack_c4 ((gfc_array_c4 *)d, (const GFC_COMPLEX_4 *)s);
84           return;
85
86         case 16:
87           internal_unpack_c8 ((gfc_array_c8 *)d, (const GFC_COMPLEX_8 *)s);
88           return;
89         }
90     default:
91       break;
92     }
93
94   if (d->dim[0].stride == 0)
95     d->dim[0].stride = 1;
96
97   dim = GFC_DESCRIPTOR_RANK (d);
98   dsize = 1;
99   for (n = 0; n < dim; n++)
100     {
101       count[n] = 0;
102       stride[n] = d->dim[n].stride;
103       extent[n] = d->dim[n].ubound + 1 - d->dim[n].lbound;
104       if (extent[n] <= 0)
105         abort ();
106
107       if (dsize == stride[n])
108         dsize *= extent[n];
109       else
110         dsize = 0;
111     }
112
113   src = s;
114
115   if (dsize != 0)
116     {
117       memcpy (dest, src, dsize * size);
118       return;
119     }
120
121   stride0 = stride[0] * size;
122
123   while (dest)
124     {
125       /* Copy the data.  */
126       memcpy (dest, src, size);
127       /* Advance to the next element.  */
128       src += size;
129       dest += stride0;
130       count[0]++;
131       /* Advance to the next source element.  */
132       n = 0;
133       while (count[n] == extent[n])
134         {
135           /* When we get to the end of a dimension, reset it and increment
136              the next dimension.  */
137           count[n] = 0;
138           /* We could precalculate these products, but this is a less
139              frequently used path so probably not worth it.  */
140           dest -= stride[n] * extent[n] * size;
141           n++;
142           if (n == dim)
143             {
144               dest = NULL;
145               break;
146             }
147           else
148             {
149               count[n]++;
150               dest += stride[n] * size;
151             }
152         }
153     }
154 }