OSDN Git Service

* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / in_unpack_generic.c
1 /* Generic 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
28 void
29 internal_unpack (gfc_array_char * d, const void * s)
30 {
31   index_type count[GFC_MAX_DIMENSIONS - 1];
32   index_type extent[GFC_MAX_DIMENSIONS - 1];
33   index_type stride[GFC_MAX_DIMENSIONS - 1];
34   index_type stride0;
35   index_type dim;
36   index_type dsize;
37   char *dest;
38   const char *src;
39   int n;
40   int size;
41
42   dest = d->data;
43   /* This check may be redundant, but do it anyway.  */
44   if (s == dest || !s)
45     return;
46
47   size = GFC_DESCRIPTOR_SIZE (d);
48   switch (size)
49     {
50     case 4:
51       internal_unpack_4 ((gfc_array_i4 *)d, (const GFC_INTEGER_4 *)s);
52       return;
53
54     case 8:
55       internal_unpack_8 ((gfc_array_i8 *)d, (const GFC_INTEGER_8 *)s);
56       return;
57     }
58
59   if (d->dim[0].stride == 0)
60     d->dim[0].stride = 1;
61
62   dim = GFC_DESCRIPTOR_RANK (d);
63   dsize = 1;
64   for (n = 0; n < dim; n++)
65     {
66       count[n] = 0;
67       stride[n] = d->dim[n].stride;
68       extent[n] = d->dim[n].ubound + 1 - d->dim[n].lbound;
69       if (extent[n] <= 0)
70         abort ();
71
72       if (dsize == stride[n])
73         dsize *= extent[n];
74       else
75         dsize = 0;
76     }
77
78   src = s;
79
80   if (dsize != 0)
81     {
82       memcpy (dest, src, dsize * size);
83       return;
84     }
85
86   stride0 = stride[0] * size;
87
88   while (dest)
89     {
90       /* Copy the data.  */
91       memcpy (dest, src, size);
92       /* Advance to the next element.  */
93       src += size;
94       dest += stride0;
95       count[0]++;
96       /* Advance to the next source element.  */
97       n = 0;
98       while (count[n] == extent[n])
99         {
100           /* When we get to the end of a dimension, reset it and increment
101              the next dimension.  */
102           count[n] = 0;
103           /* We could precalculate these products, but this is a less
104              frequently used path so proabably not worth it.  */
105           dest -= stride[n] * extent[n] * size;
106           n++;
107           if (n == dim)
108             {
109               dest = NULL;
110               break;
111             }
112           else
113             {
114               count[n]++;
115               dest += stride[n] * size;
116             }
117         }
118     }
119 }