]>
Commit | Line | Data |
---|---|---|
6de9cd9a DN |
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 (libgfortran). | |
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 libgfortran; 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 "libgfortran.h" | |
26 | ||
6de9cd9a DN |
27 | /* Allocates a block of memory with internal_malloc if the array needs |
28 | repacking. */ | |
29 | ||
30 | GFC_INTEGER_4 * | |
31 | internal_pack_4 (gfc_array_i4 * source) | |
32 | { | |
33 | index_type count[GFC_MAX_DIMENSIONS - 1]; | |
34 | index_type extent[GFC_MAX_DIMENSIONS - 1]; | |
35 | index_type stride[GFC_MAX_DIMENSIONS - 1]; | |
36 | index_type stride0; | |
37 | index_type dim; | |
38 | index_type ssize; | |
39 | const GFC_INTEGER_4 *src; | |
40 | GFC_INTEGER_4 *dest; | |
41 | GFC_INTEGER_4 *destptr; | |
42 | int n; | |
43 | int packed; | |
44 | ||
45 | if (source->dim[0].stride == 0) | |
46 | { | |
47 | source->dim[0].stride = 1; | |
48 | return source->data; | |
49 | } | |
50 | ||
51 | dim = GFC_DESCRIPTOR_RANK (source); | |
52 | ssize = 1; | |
53 | packed = 1; | |
54 | for (n = 0; n < dim; n++) | |
55 | { | |
56 | count[n] = 0; | |
57 | stride[n] = source->dim[n].stride; | |
58 | extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound; | |
59 | if (extent[n] <= 0) | |
60 | { | |
61 | /* Do nothing. */ | |
62 | packed = 1; | |
63 | break; | |
64 | } | |
65 | ||
66 | if (ssize != stride[n]) | |
67 | packed = 0; | |
68 | ||
69 | ssize *= extent[n]; | |
70 | } | |
71 | ||
72 | if (packed) | |
73 | return source->data; | |
74 | ||
75 | /* Allocate storage for the destination. */ | |
76 | destptr = (GFC_INTEGER_4 *)internal_malloc_size (ssize * 4); | |
77 | dest = destptr; | |
78 | src = source->data; | |
79 | stride0 = stride[0]; | |
80 | ||
81 | ||
82 | while (src) | |
83 | { | |
84 | /* Copy the data. */ | |
85 | *(dest++) = *src; | |
86 | /* Advance to the next element. */ | |
87 | src += stride0; | |
88 | count[0]++; | |
89 | /* Advance to the next source element. */ | |
90 | n = 0; | |
91 | while (count[n] == extent[n]) | |
92 | { | |
93 | /* When we get to the end of a dimension, reset it and increment | |
94 | the next dimension. */ | |
95 | count[n] = 0; | |
96 | /* We could precalculate these products, but this is a less | |
97 | frequently used path so proabably not worth it. */ | |
98 | src -= stride[n] * extent[n]; | |
99 | n++; | |
100 | if (n == dim) | |
101 | { | |
102 | src = NULL; | |
103 | break; | |
104 | } | |
105 | else | |
106 | { | |
107 | count[n]++; | |
108 | src += stride[n]; | |
109 | } | |
110 | } | |
111 | } | |
112 | return destptr; | |
113 | } | |
114 |