]> gcc.gnu.org Git - gcc.git/blame - libgfortran/m4/in_pack.m4
Update copyright years.
[gcc.git] / libgfortran / m4 / in_pack.m4
CommitLineData
6de9cd9a 1`/* Helper function for repacking arrays.
5624e564 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook <paul@nowt.org>
4
21d1335b 5This file is part of the GNU Fortran 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 27#include <stdlib.h>
36ae8a61
FXC
28#include <assert.h>'
29
c9e66eda 30include(iparm.m4)dnl
6de9cd9a 31
adea5e16 32`#if defined (HAVE_'rtype_name`)
644cb69f 33
6de9cd9a
DN
34/* Allocates a block of memory with internal_malloc if the array needs
35 repacking. */
adea5e16 36'
39328081
TK
37dnl The kind (ie size) is used to name the function for logicals, integers
38dnl and reals. For complex, it's c4 or c8.
adea5e16
TK
39rtype_name` *
40internal_pack_'rtype_ccode` ('rtype` * source)
6de9cd9a 41{
e33e218b
TK
42 index_type count[GFC_MAX_DIMENSIONS];
43 index_type extent[GFC_MAX_DIMENSIONS];
44 index_type stride[GFC_MAX_DIMENSIONS];
6de9cd9a
DN
45 index_type stride0;
46 index_type dim;
47 index_type ssize;
adea5e16 48 const 'rtype_name` *src;
5863aacf 49 'rtype_name` * restrict dest;
adea5e16 50 'rtype_name` *destptr;
6de9cd9a
DN
51 int n;
52 int packed;
53
6ff24d45
JB
54 /* TODO: Investigate how we can figure out if this is a temporary
55 since the stride=0 thing has been removed from the frontend. */
6de9cd9a
DN
56
57 dim = GFC_DESCRIPTOR_RANK (source);
58 ssize = 1;
59 packed = 1;
60 for (n = 0; n < dim; n++)
61 {
62 count[n] = 0;
dfb55fdc
TK
63 stride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
64 extent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
6de9cd9a
DN
65 if (extent[n] <= 0)
66 {
67 /* Do nothing. */
68 packed = 1;
69 break;
70 }
71
72 if (ssize != stride[n])
73 packed = 0;
74
75 ssize *= extent[n];
76 }
77
78 if (packed)
21d1335b 79 return source->base_addr;
6de9cd9a
DN
80
81 /* Allocate storage for the destination. */
92e6f3a4 82 destptr = xmallocarray (ssize, sizeof ('rtype_name`));
6de9cd9a 83 dest = destptr;
21d1335b 84 src = source->base_addr;
6de9cd9a
DN
85 stride0 = stride[0];
86
87
88 while (src)
89 {
90 /* Copy the data. */
91 *(dest++) = *src;
92 /* Advance to the next element. */
93 src += stride0;
94 count[0]++;
95 /* Advance to the next source element. */
96 n = 0;
97 while (count[n] == extent[n])
98 {
99 /* When we get to the end of a dimension, reset it and increment
100 the next dimension. */
101 count[n] = 0;
102 /* We could precalculate these products, but this is a less
8b6dba81 103 frequently used path so probably not worth it. */
6de9cd9a
DN
104 src -= stride[n] * extent[n];
105 n++;
106 if (n == dim)
107 {
108 src = NULL;
109 break;
110 }
111 else
112 {
113 count[n]++;
114 src += stride[n];
115 }
116 }
117 }
118 return destptr;
119}
120
644cb69f 121#endif
36ae8a61 122'
This page took 0.921992 seconds and 5 git commands to generate.