acinclude.m4 (LIBGFOR_CHECK_ATTRIBUTE_VISIBILITY): New.
[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 (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 extern void *internal_pack (gfc_array_char *);
29 export_proto(internal_pack);
30
31 void *
32 internal_pack (gfc_array_char * source)
33 {
34 index_type count[GFC_MAX_DIMENSIONS - 1];
35 index_type extent[GFC_MAX_DIMENSIONS - 1];
36 index_type stride[GFC_MAX_DIMENSIONS - 1];
37 index_type stride0;
38 index_type dim;
39 index_type ssize;
40 const char *src;
41 char *dest;
42 void *destptr;
43 int n;
44 int packed;
45 index_type size;
46
47 if (source->dim[0].stride == 0)
48 {
49 source->dim[0].stride = 1;
50 return source->data;
51 }
52
53 size = GFC_DESCRIPTOR_SIZE (source);
54 switch (size)
55 {
56 case 4:
57 return internal_pack_4 ((gfc_array_i4 *)source);
58
59 case 8:
60 return internal_pack_8 ((gfc_array_i8 *)source);
61 }
62
63 dim = GFC_DESCRIPTOR_RANK (source);
64 ssize = 1;
65 packed = 1;
66 for (n = 0; n < dim; n++)
67 {
68 count[n] = 0;
69 stride[n] = source->dim[n].stride;
70 extent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
71 if (extent[n] <= 0)
72 {
73 /* Do nothing. */
74 packed = 1;
75 break;
76 }
77
78 if (ssize != stride[n])
79 packed = 0;
80
81 ssize *= extent[n];
82 }
83
84 if (packed)
85 return source->data;
86
87 /* Allocate storage for the destination. */
88 destptr = internal_malloc_size (ssize * size);
89 dest = (char *)destptr;
90 src = source->data;
91 stride0 = stride[0] * size;
92
93 while (src)
94 {
95 /* Copy the data. */
96 memcpy(dest, src, size);
97 /* Advance to the next element. */
98 dest += size;
99 src += stride0;
100 count[0]++;
101 /* Advance to the next source element. */
102 n = 0;
103 while (count[n] == extent[n])
104 {
105 /* When we get to the end of a dimension, reset it and increment
106 the next dimension. */
107 count[n] = 0;
108 /* We could precalculate these products, but this is a less
109 frequently used path so proabably not worth it. */
110 src -= stride[n] * extent[n] * size;
111 n++;
112 if (n == dim)
113 {
114 src = NULL;
115 break;
116 }
117 else
118 {
119 count[n]++;
120 src += stride[n] * size;
121 }
122 }
123 }
124 return destptr;
125 }