]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/m4/in_unpack.m4
re PR fortran/37577 ([meta-bug] change internal array descriptor format for better...
[thirdparty/gcc.git] / libgfortran / m4 / in_unpack.m4
CommitLineData
6de9cd9a 1`/* Helper function for repacking arrays.
748086b7 2 Copyright 2003, 2006, 2007, 2009 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook <paul@nowt.org>
4
57dea9f6 5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6de9cd9a 6
57dea9f6
TM
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
6de9cd9a 9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
57dea9f6
TM
11
12Libgfortran is distributed in the hope that it will be useful,
6de9cd9a
DN
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57dea9f6 15GNU General Public License for more details.
6de9cd9a 16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
6de9cd9a 25
36ae8a61 26#include "libgfortran.h"
6de9cd9a
DN
27#include <stdlib.h>
28#include <assert.h>
36ae8a61
FXC
29#include <string.h>'
30
c9e66eda 31include(iparm.m4)dnl
6de9cd9a 32
644cb69f
FXC
33`#if defined (HAVE_'rtype_name`)'
34
39328081
TK
35dnl Only the kind (ie size) is used to name the function for integers,
36dnl reals and logicals. For complex, it's c4 and c8.
adea5e16
TK
37`void
38internal_unpack_'rtype_ccode` ('rtype` * d, const 'rtype_name` * src)
6de9cd9a 39{
e33e218b
TK
40 index_type count[GFC_MAX_DIMENSIONS];
41 index_type extent[GFC_MAX_DIMENSIONS];
42 index_type stride[GFC_MAX_DIMENSIONS];
6de9cd9a
DN
43 index_type stride0;
44 index_type dim;
45 index_type dsize;
5863aacf 46 'rtype_name` * restrict dest;
6de9cd9a
DN
47 int n;
48
49 dest = d->data;
50 if (src == dest || !src)
51 return;
52
6de9cd9a
DN
53 dim = GFC_DESCRIPTOR_RANK (d);
54 dsize = 1;
55 for (n = 0; n < dim; n++)
56 {
57 count[n] = 0;
dfb55fdc
TK
58 stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
59 extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
6de9cd9a 60 if (extent[n] <= 0)
210879b8 61 return;
6de9cd9a
DN
62
63 if (dsize == stride[n])
210879b8 64 dsize *= extent[n];
6de9cd9a 65 else
210879b8 66 dsize = 0;
6de9cd9a
DN
67 }
68
69 if (dsize != 0)
70 {
adea5e16 71 memcpy (dest, src, dsize * sizeof ('rtype_name`));
6de9cd9a
DN
72 return;
73 }
74
75 stride0 = stride[0];
76
77 while (dest)
78 {
79 /* Copy the data. */
80 *dest = *(src++);
81 /* Advance to the next element. */
82 dest += stride0;
83 count[0]++;
84 /* Advance to the next source element. */
85 n = 0;
86 while (count[n] == extent[n])
87 {
88 /* When we get to the end of a dimension, reset it and increment
89 the next dimension. */
90 count[n] = 0;
91 /* We could precalculate these products, but this is a less
8b6dba81 92 frequently used path so probably not worth it. */
6de9cd9a
DN
93 dest -= stride[n] * extent[n];
94 n++;
95 if (n == dim)
96 {
97 dest = NULL;
98 break;
99 }
100 else
101 {
102 count[n]++;
103 dest += stride[n];
104 }
105 }
106 }
107}
108
644cb69f 109#endif
36ae8a61 110'