]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/m4/in_pack.m4
Merge tree-ssa-20020619-branch into mainline.
[thirdparty/gcc.git] / libgfortran / m4 / in_pack.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 (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 include(types.m4)dnl
27 define(rtype_kind, regexp(file, `_.\([0-9]+\)\.', `\1'))dnl
28 define(rtype_letter, regexp(file, `_\(.\)[0-9]+\.', `\1'))dnl
29 define(rtype_code,rtype_letter`'rtype_name)dnl
30 define(rtype,get_arraytype(rtype_letter,rtype_kind))dnl
31 define(rtype_name, get_typename(rtype_letter, rtype_kind))dnl
32
33
34 /* Allocates a block of memory with internal_malloc if the array needs
35 repacking. */
36
37 dnl Only the kind (ie size) is used to name the function.
38 rtype_name *
39 `internal_pack_'rtype_kind (rtype * source)
40 {
41 index_type count[GFC_MAX_DIMENSIONS - 1];
42 index_type extent[GFC_MAX_DIMENSIONS - 1];
43 index_type stride[GFC_MAX_DIMENSIONS - 1];
44 index_type stride0;
45 index_type dim;
46 index_type ssize;
47 const rtype_name *src;
48 rtype_name *dest;
49 rtype_name *destptr;
50 int n;
51 int packed;
52
53 if (source->dim[0].stride == 0)
54 {
55 source->dim[0].stride = 1;
56 return source->data;
57 }
58
59 dim = GFC_DESCRIPTOR_RANK (source);
60 ssize = 1;
61 packed = 1;
62 for (n = 0; n < dim; n++)
63 {
64 count[n] = 0;
65 stride[n] = source->dim[n].stride;
66 extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
67 if (extent[n] <= 0)
68 {
69 /* Do nothing. */
70 packed = 1;
71 break;
72 }
73
74 if (ssize != stride[n])
75 packed = 0;
76
77 ssize *= extent[n];
78 }
79
80 if (packed)
81 return source->data;
82
83 /* Allocate storage for the destination. */
84 destptr = (rtype_name *)internal_malloc_size (ssize * rtype_kind);
85 dest = destptr;
86 src = source->data;
87 stride0 = stride[0];
88
89
90 while (src)
91 {
92 /* Copy the data. */
93 *(dest++) = *src;
94 /* Advance to the next element. */
95 src += stride0;
96 count[0]++;
97 /* Advance to the next source element. */
98 n = 0;
99 while (count[n] == extent[n])
100 {
101 /* When we get to the end of a dimension, reset it and increment
102 the next dimension. */
103 count[n] = 0;
104 /* We could precalculate these products, but this is a less
105 frequently used path so proabably not worth it. */
106 src -= stride[n] * extent[n];
107 n++;
108 if (n == dim)
109 {
110 src = NULL;
111 break;
112 }
113 else
114 {
115 count[n]++;
116 src += stride[n];
117 }
118 }
119 }
120 return destptr;
121 }
122