re PR libfortran/19280 (Inconsistent licensing of libgfortran)
[gcc.git] / libgfortran / runtime / in_pack_generic.c
1 /* Generic helper function for repacking arrays.
2 Copyright 2003 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 2 of the License, or (at your option) any later version.
11
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.)
20
21 Libgfortran 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.
25
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. */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37 extern void *internal_pack (gfc_array_char *);
38 export_proto(internal_pack);
39
40 void *
41 internal_pack (gfc_array_char * source)
42 {
43 index_type count[GFC_MAX_DIMENSIONS - 1];
44 index_type extent[GFC_MAX_DIMENSIONS - 1];
45 index_type stride[GFC_MAX_DIMENSIONS - 1];
46 index_type stride0;
47 index_type dim;
48 index_type ssize;
49 const char *src;
50 char *dest;
51 void *destptr;
52 int n;
53 int packed;
54 index_type size;
55
56 if (source->dim[0].stride == 0)
57 {
58 source->dim[0].stride = 1;
59 return source->data;
60 }
61
62 size = GFC_DESCRIPTOR_SIZE (source);
63 switch (size)
64 {
65 case 4:
66 return internal_pack_4 ((gfc_array_i4 *)source);
67
68 case 8:
69 return internal_pack_8 ((gfc_array_i8 *)source);
70 }
71
72 dim = GFC_DESCRIPTOR_RANK (source);
73 ssize = 1;
74 packed = 1;
75 for (n = 0; n < dim; n++)
76 {
77 count[n] = 0;
78 stride[n] = source->dim[n].stride;
79 extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
80 if (extent[n] <= 0)
81 {
82 /* Do nothing. */
83 packed = 1;
84 break;
85 }
86
87 if (ssize != stride[n])
88 packed = 0;
89
90 ssize *= extent[n];
91 }
92
93 if (packed)
94 return source->data;
95
96 /* Allocate storage for the destination. */
97 destptr = internal_malloc_size (ssize * size);
98 dest = (char *)destptr;
99 src = source->data;
100 stride0 = stride[0] * size;
101
102 while (src)
103 {
104 /* Copy the data. */
105 memcpy(dest, src, size);
106 /* Advance to the next element. */
107 dest += size;
108 src += stride0;
109 count[0]++;
110 /* Advance to the next source element. */
111 n = 0;
112 while (count[n] == extent[n])
113 {
114 /* When we get to the end of a dimension, reset it and increment
115 the next dimension. */
116 count[n] = 0;
117 /* We could precalculate these products, but this is a less
118 frequently used path so proabably not worth it. */
119 src -= stride[n] * extent[n] * size;
120 n++;
121 if (n == dim)
122 {
123 src = NULL;
124 break;
125 }
126 else
127 {
128 count[n]++;
129 src += stride[n] * size;
130 }
131 }
132 }
133 return destptr;
134 }