PR documentation/19309
[official-gcc.git] / libgfortran / intrinsics / spread_generic.c
blobe40739e4614a0a198d2b2e3262b920e0adf514db
1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Ligbfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
37 extern void spread (const gfc_array_char *, const gfc_array_char *,
38 const index_type *, const index_type *);
39 export_proto(spread);
41 void
42 spread (const gfc_array_char *ret, const gfc_array_char *source,
43 const index_type *along, const index_type *pncopies)
45 /* r.* indicates the return array. */
46 index_type rstride[GFC_MAX_DIMENSIONS - 1];
47 index_type rstride0;
48 index_type rdelta;
49 char *rptr;
50 char *dest;
51 /* s.* indicates the source array. */
52 index_type sstride[GFC_MAX_DIMENSIONS - 1];
53 index_type sstride0;
54 const char *sptr;
56 index_type count[GFC_MAX_DIMENSIONS - 1];
57 index_type extent[GFC_MAX_DIMENSIONS - 1];
58 index_type n;
59 index_type dim;
60 index_type size;
61 index_type ncopies;
63 size = GFC_DESCRIPTOR_SIZE (source);
64 dim = 0;
65 for (n = 0; n < GFC_DESCRIPTOR_RANK (ret); n++)
67 if (n == *along - 1)
69 rdelta = ret->dim[n].stride * size;
71 else
73 count[dim] = 0;
74 extent[dim] = source->dim[dim].ubound + 1 - source->dim[dim].lbound;
75 sstride[dim] = source->dim[dim].stride * size;
76 rstride[dim] = ret->dim[n].stride * size;
77 dim++;
80 dim = GFC_DESCRIPTOR_RANK (source);
81 if (sstride[0] == 0)
82 sstride[0] = size;
83 if (rstride[0] == 0)
84 rstride[0] = size;
86 sstride0 = sstride[0];
87 rstride0 = rstride[0];
88 rptr = ret->data;
89 sptr = source->data;
90 ncopies = *pncopies;
92 while (sptr)
94 /* Spread this element. */
95 dest = rptr;
96 for (n = 0; n < ncopies; n++)
98 memcpy (dest, sptr, size);
99 dest += rdelta;
101 /* Advance to the next element. */
102 sptr += sstride0;
103 rptr += rstride0;
104 count[0]++;
105 n = 0;
106 while (count[n] == extent[n])
108 /* When we get to the end of a dimension, reset it and increment
109 the next dimension. */
110 count[n] = 0;
111 /* We could precalculate these products, but this is a less
112 frequently used path so probably not worth it. */
113 sptr -= sstride[n] * extent[n];
114 rptr -= rstride[n] * extent[n];
115 n++;
116 if (n >= dim)
118 /* Break out of the loop. */
119 sptr = NULL;
120 break;
122 else
124 count[n]++;
125 sptr += sstride[n];
126 rptr += rstride[n];