OSDN Git Service

* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / spread_generic.c
1 /* Generic implementation of the RESHAPE intrinsic
2    Copyright 2002 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 (libgfor).
6
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Ligbfor 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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
27
28 void
29 __spread (const gfc_array_char * ret, const gfc_array_char * source,
30     const index_type * along, const index_type * pncopies)
31 {
32   /* r.* indicates the return array.  */
33   index_type rstride[GFC_MAX_DIMENSIONS - 1];
34   index_type rstride0;
35   index_type rdelta;
36   char *rptr;
37   char *dest;
38   /* s.* indicates the source array.  */
39   index_type sstride[GFC_MAX_DIMENSIONS - 1];
40   index_type sstride0;
41   const char *sptr;
42
43   index_type count[GFC_MAX_DIMENSIONS - 1];
44   index_type extent[GFC_MAX_DIMENSIONS - 1];
45   index_type n;
46   index_type dim;
47   index_type size;
48   index_type ncopies;
49
50   size = GFC_DESCRIPTOR_SIZE (source);
51   dim = 0;
52   for (n = 0; n < GFC_DESCRIPTOR_RANK (ret); n++)
53     {
54       if (n == *along - 1)
55         {
56           rdelta = ret->dim[n].stride * size;
57         }
58       else
59         {
60           count[dim] = 0;
61           extent[dim] = source->dim[dim].ubound + 1 - source->dim[dim].lbound;
62           sstride[dim] = source->dim[dim].stride * size;
63           rstride[dim] = ret->dim[n].stride * size;
64           dim++;
65         }
66     }
67   dim = GFC_DESCRIPTOR_RANK (source);
68   if (sstride[0] == 0)
69     sstride[0] = size;
70   if (rstride[0] == 0)
71     rstride[0] = size;
72
73   sstride0 = sstride[0];
74   rstride0 = rstride[0];
75   rptr = ret->data;
76   sptr = source->data;
77   ncopies = *pncopies;
78
79   while (sptr)
80     {
81       /* Spread this element.  */
82       dest = rptr;
83       for (n = 0; n < ncopies; n++)
84         {
85           memcpy (dest, sptr, size);
86           dest += rdelta;
87         }
88       /* Advance to the next element.  */
89       sptr += sstride0;
90       rptr += rstride0;
91       count[0]++;
92       n = 0;
93       while (count[n] == extent[n])
94         {
95           /* When we get to the end of a dimension, reset it and increment
96              the next dimension.  */
97           count[n] = 0;
98           /* We could precalculate these products, but this is a less
99              frequently used path so probably not worth it.  */
100           sptr -= sstride[n] * extent[n];
101           rptr -= rstride[n] * extent[n];
102           n++;
103           if (n >= dim)
104             {
105               /* Break out of the loop.  */
106               sptr = NULL;
107               break;
108             }
109           else
110             {
111               count[n]++;
112               sptr += sstride[n];
113               rptr += rstride[n];
114             }
115         }
116     }
117 }