OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / in_pack_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_pack (gfc_array_char * source)
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 ssize;
37   const char *src;
38   char *dest;
39   void *destptr;
40   int n;
41   int packed;
42   index_type size;
43
44   if (source->dim[0].stride == 0)
45     {
46       source->dim[0].stride = 1;
47       return source->data;
48     }
49
50   size = GFC_DESCRIPTOR_SIZE (source);
51   switch (size)
52     {
53     case 4:
54       return internal_pack_4 ((gfc_array_i4 *)source);
55
56     case 8:
57       return internal_pack_8 ((gfc_array_i8 *)source);
58     }
59
60   dim = GFC_DESCRIPTOR_RANK (source);
61   ssize = 1;
62   packed = 1;
63   for (n = 0; n < dim; n++)
64     {
65       count[n] = 0;
66       stride[n] = source->dim[n].stride;
67       extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
68       if (extent[n] <= 0)
69         {
70           /* Do nothing.  */
71           packed = 1;
72           break;
73         }
74
75       if (ssize != stride[n])
76         packed = 0;
77
78       ssize *= extent[n];
79     }
80
81   if (packed)
82     return source->data;
83
84    /* Allocate storage for the destination.  */
85   destptr = internal_malloc_size (ssize * size);
86   dest = (char *)destptr;
87   src = source->data;
88   stride0 = stride[0] * size;
89
90   while (src)
91     {
92       /* Copy the data.  */
93       memcpy(dest, src, size);
94       /* Advance to the next element.  */
95       dest += size;
96       src += stride0;
97       count[0]++;
98       /* Advance to the next source element.  */
99       n = 0;
100       while (count[n] == extent[n])
101         {
102           /* When we get to the end of a dimension, reset it and increment
103              the next dimension.  */
104           count[n] = 0;
105           /* We could precalculate these products, but this is a less
106              frequently used path so proabably not worth it.  */
107           src -= stride[n] * extent[n] * size;
108           n++;
109           if (n == dim)
110             {
111               src = NULL;
112               break;
113             }
114           else
115             {
116               count[n]++;
117               src += stride[n] * size;
118             }
119         }
120     }
121   return destptr;
122 }
123