]>
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 | ||
57dea9f6 TM |
7 | Libgfortran is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU General Public | |
6de9cd9a | 9 | License as published by the Free Software Foundation; either |
57dea9f6 | 10 | version 2 of the License, or (at your option) any later version. |
6de9cd9a | 11 | |
57dea9f6 TM |
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, | |
6de9cd9a DN |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
57dea9f6 | 24 | GNU General Public License for more details. |
6de9cd9a | 25 | |
57dea9f6 TM |
26 | You should have received a copy of the GNU General Public |
27 | License along with libgfortran; see the file COPYING. If not, | |
6de9cd9a DN |
28 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
29 | Boston, MA 02111-1307, USA. */ | |
30 | ||
31 | #include "config.h" | |
32 | #include <stdlib.h> | |
33 | #include <assert.h> | |
34 | #include "libgfortran.h" | |
35 | ||
6de9cd9a DN |
36 | /* Allocates a block of memory with internal_malloc if the array needs |
37 | repacking. */ | |
38 | ||
39 | GFC_INTEGER_4 * | |
40 | internal_pack_4 (gfc_array_i4 * source) | |
41 | { | |
42 | index_type count[GFC_MAX_DIMENSIONS - 1]; | |
43 | index_type extent[GFC_MAX_DIMENSIONS - 1]; | |
44 | index_type stride[GFC_MAX_DIMENSIONS - 1]; | |
45 | index_type stride0; | |
46 | index_type dim; | |
47 | index_type ssize; | |
48 | const GFC_INTEGER_4 *src; | |
49 | GFC_INTEGER_4 *dest; | |
50 | GFC_INTEGER_4 *destptr; | |
51 | int n; | |
52 | int packed; | |
53 | ||
54 | if (source->dim[0].stride == 0) | |
55 | { | |
56 | source->dim[0].stride = 1; | |
57 | return source->data; | |
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 = (GFC_INTEGER_4 *)internal_malloc_size (ssize * 4); | |
86 | dest = destptr; | |
87 | src = source->data; | |
88 | stride0 = stride[0]; | |
89 | ||
90 | ||
91 | while (src) | |
92 | { | |
93 | /* Copy the data. */ | |
94 | *(dest++) = *src; | |
95 | /* Advance to the next element. */ | |
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]; | |
108 | n++; | |
109 | if (n == dim) | |
110 | { | |
111 | src = NULL; | |
112 | break; | |
113 | } | |
114 | else | |
115 | { | |
116 | count[n]++; | |
117 | src += stride[n]; | |
118 | } | |
119 | } | |
120 | } | |
121 | return destptr; | |
122 | } | |
123 |