OSDN Git Service

Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / in_pack.m4
1 `/* Helper function for repacking arrays.
2    Copyright 2003, 2006, 2007, 2009 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 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
11
12 Libgfortran 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 General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <assert.h>'
29
30 include(iparm.m4)dnl
31
32 `#if defined (HAVE_'rtype_name`)
33
34 /* Allocates a block of memory with internal_malloc if the array needs
35    repacking.  */
36 '
37 dnl The kind (ie size) is used to name the function for logicals, integers
38 dnl and reals.  For complex, it's c4 or c8.
39 rtype_name` *
40 internal_pack_'rtype_ccode` ('rtype` * source)
41 {
42   index_type count[GFC_MAX_DIMENSIONS];
43   index_type extent[GFC_MAX_DIMENSIONS];
44   index_type stride[GFC_MAX_DIMENSIONS];
45   index_type stride0;
46   index_type dim;
47   index_type ssize;
48   const 'rtype_name` *src;
49   'rtype_name` * restrict dest;
50   'rtype_name` *destptr;
51   int n;
52   int packed;
53
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.  */
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;
63       stride[n] = source->dim[n].stride;
64       extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
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)
79     return source->data;
80
81   /* Allocate storage for the destination.  */
82   destptr = ('rtype_name` *)internal_malloc_size (ssize * sizeof ('rtype_name`));
83   dest = destptr;
84   src = source->data;
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
103              frequently used path so probably not worth it.  */
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
121 #endif
122 '