xref: /netbsd-src/external/gpl3/gcc.old/dist/libgfortran/m4/in_unpack.m4 (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg`/* Helper function for repacking arrays.
2*4c3eb207Smrg   Copyright (C) 2003-2020 Free Software Foundation, Inc.
3627f7eb2Smrg   Contributed by Paul Brook <paul@nowt.org>
4627f7eb2Smrg
5627f7eb2SmrgThis file is part of the GNU Fortran runtime library (libgfortran).
6627f7eb2Smrg
7627f7eb2SmrgLibgfortran is free software; you can redistribute it and/or
8627f7eb2Smrgmodify it under the terms of the GNU General Public
9627f7eb2SmrgLicense as published by the Free Software Foundation; either
10627f7eb2Smrgversion 3 of the License, or (at your option) any later version.
11627f7eb2Smrg
12627f7eb2SmrgLibgfortran is distributed in the hope that it will be useful,
13627f7eb2Smrgbut WITHOUT ANY WARRANTY; without even the implied warranty of
14627f7eb2SmrgMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15627f7eb2SmrgGNU General Public License for more details.
16627f7eb2Smrg
17627f7eb2SmrgUnder Section 7 of GPL version 3, you are granted additional
18627f7eb2Smrgpermissions described in the GCC Runtime Library Exception, version
19627f7eb2Smrg3.1, as published by the Free Software Foundation.
20627f7eb2Smrg
21627f7eb2SmrgYou should have received a copy of the GNU General Public License and
22627f7eb2Smrga copy of the GCC Runtime Library Exception along with this program;
23627f7eb2Smrgsee the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24627f7eb2Smrg<http://www.gnu.org/licenses/>.  */
25627f7eb2Smrg
26627f7eb2Smrg#include "libgfortran.h"
27627f7eb2Smrg#include <string.h>'
28627f7eb2Smrg
29627f7eb2Smrginclude(iparm.m4)dnl
30627f7eb2Smrg
31627f7eb2Smrg`#if defined (HAVE_'rtype_name`)'
32627f7eb2Smrg
33627f7eb2Smrgdnl Only the kind (ie size) is used to name the function for integers,
34627f7eb2Smrgdnl reals and logicals.  For complex, it's c4 and c8.
35627f7eb2Smrg`void
36627f7eb2Smrginternal_unpack_'rtype_ccode` ('rtype` * d, const 'rtype_name` * src)
37627f7eb2Smrg{
38627f7eb2Smrg  index_type count[GFC_MAX_DIMENSIONS];
39627f7eb2Smrg  index_type extent[GFC_MAX_DIMENSIONS];
40627f7eb2Smrg  index_type stride[GFC_MAX_DIMENSIONS];
41627f7eb2Smrg  index_type stride0;
42627f7eb2Smrg  index_type dim;
43627f7eb2Smrg  index_type dsize;
44627f7eb2Smrg  'rtype_name` * restrict dest;
45627f7eb2Smrg
46627f7eb2Smrg  dest = d->base_addr;
47627f7eb2Smrg  if (src == dest || !src)
48627f7eb2Smrg    return;
49627f7eb2Smrg
50627f7eb2Smrg  dim = GFC_DESCRIPTOR_RANK (d);
51627f7eb2Smrg  dsize = 1;
52627f7eb2Smrg  for (index_type n = 0; n < dim; n++)
53627f7eb2Smrg    {
54627f7eb2Smrg      count[n] = 0;
55627f7eb2Smrg      stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
56627f7eb2Smrg      extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
57627f7eb2Smrg      if (extent[n] <= 0)
58627f7eb2Smrg	return;
59627f7eb2Smrg
60627f7eb2Smrg      if (dsize == stride[n])
61627f7eb2Smrg	dsize *= extent[n];
62627f7eb2Smrg      else
63627f7eb2Smrg	dsize = 0;
64627f7eb2Smrg    }
65627f7eb2Smrg
66627f7eb2Smrg  if (dsize != 0)
67627f7eb2Smrg    {
68627f7eb2Smrg      memcpy (dest, src, dsize * sizeof ('rtype_name`));
69627f7eb2Smrg      return;
70627f7eb2Smrg    }
71627f7eb2Smrg
72627f7eb2Smrg  stride0 = stride[0];
73627f7eb2Smrg
74627f7eb2Smrg  while (dest)
75627f7eb2Smrg    {
76627f7eb2Smrg      /* Copy the data.  */
77627f7eb2Smrg      *dest = *(src++);
78627f7eb2Smrg      /* Advance to the next element.  */
79627f7eb2Smrg      dest += stride0;
80627f7eb2Smrg      count[0]++;
81627f7eb2Smrg      /* Advance to the next source element.  */
82627f7eb2Smrg      index_type n = 0;
83627f7eb2Smrg      while (count[n] == extent[n])
84627f7eb2Smrg        {
85627f7eb2Smrg          /* When we get to the end of a dimension, reset it and increment
86627f7eb2Smrg             the next dimension.  */
87627f7eb2Smrg          count[n] = 0;
88627f7eb2Smrg          /* We could precalculate these products, but this is a less
89627f7eb2Smrg             frequently used path so probably not worth it.  */
90627f7eb2Smrg          dest -= stride[n] * extent[n];
91627f7eb2Smrg          n++;
92627f7eb2Smrg          if (n == dim)
93627f7eb2Smrg            {
94627f7eb2Smrg              dest = NULL;
95627f7eb2Smrg              break;
96627f7eb2Smrg            }
97627f7eb2Smrg          else
98627f7eb2Smrg            {
99627f7eb2Smrg              count[n]++;
100627f7eb2Smrg              dest += stride[n];
101627f7eb2Smrg            }
102627f7eb2Smrg        }
103627f7eb2Smrg    }
104627f7eb2Smrg}
105627f7eb2Smrg
106627f7eb2Smrg#endif
107627f7eb2Smrg'
108