OSDN Git Service

fe344ca7d315003d5811e0c17ae859c65fa6df67
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / in_unpack.m4
1 `/* Helper function for repacking arrays.
2    Copyright 2003 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 (libgfor).
6
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Ligbfor 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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"'
27 include(types.m4)dnl
28 define(rtype_kind, regexp(file, `_.\([0-9]+\)\.', `\1'))dnl
29 define(rtype_letter, regexp(file, `_\(.\)[0-9]+\.', `\1'))dnl
30 define(rtype_code,rtype_letter`'rtype_name)dnl
31 define(rtype,get_arraytype(rtype_letter,rtype_kind))dnl
32 define(rtype_name, get_typename(rtype_letter, rtype_kind))dnl
33
34 dnl Only the kind (ie size) is used to name the function.
35 void
36 `internal_unpack_'rtype_kind (rtype * d, const rtype_name * src)
37 {
38   index_type count[GFC_MAX_DIMENSIONS - 1];
39   index_type extent[GFC_MAX_DIMENSIONS - 1];
40   index_type stride[GFC_MAX_DIMENSIONS - 1];
41   index_type stride0;
42   index_type dim;
43   index_type dsize;
44   rtype_name *dest;
45   int n;
46
47   dest = d->data;
48   if (src == dest || !src)
49     return;
50
51   if (d->dim[0].stride == 0)
52     d->dim[0].stride = 1;
53
54   dim = GFC_DESCRIPTOR_RANK (d);
55   dsize = 1;
56   for (n = 0; n < dim; n++)
57     {
58       count[n] = 0;
59       stride[n] = d->dim[n].stride;
60       extent[n] = d->dim[n].ubound + 1 - d->dim[n].lbound;
61       if (extent[n] <= 0)
62         abort ();
63
64       if (dsize == stride[n])
65         dsize *= extent[n];
66       else
67         dsize = 0;
68     }
69
70   if (dsize != 0)
71     {
72       memcpy (dest, src, dsize * rtype_kind);
73       return;
74     }
75
76   stride0 = stride[0];
77
78   while (dest)
79     {
80       /* Copy the data.  */
81       *dest = *(src++);
82       /* Advance to the next element.  */
83       dest += stride0;
84       count[0]++;
85       /* Advance to the next source element.  */
86       n = 0;
87       while (count[n] == extent[n])
88         {
89           /* When we get to the end of a dimension, reset it and increment
90              the next dimension.  */
91           count[n] = 0;
92           /* We could precalculate these products, but this is a less
93              frequently used path so proabably not worth it.  */
94           dest -= stride[n] * extent[n];
95           n++;
96           if (n == dim)
97             {
98               dest = NULL;
99               break;
100             }
101           else
102             {
103               count[n]++;
104               dest += stride[n];
105             }
106         }
107     }
108 }
109