1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #ifndef CRAY |
---|
6 | # ifdef NOUNDERSCORE |
---|
7 | # define INT_PACK_DATA int_pack_data |
---|
8 | # define INT_GET_TI_HEADER_C int_get_ti_header_c |
---|
9 | # define INT_GEN_TI_HEADER_C int_gen_ti_header_c |
---|
10 | # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c |
---|
11 | # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c |
---|
12 | # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c |
---|
13 | # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field |
---|
14 | # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field |
---|
15 | # define PERTURB_REAL perturb_real |
---|
16 | # else |
---|
17 | # ifdef F2CSTYLE |
---|
18 | # define INT_PACK_DATA int_pack_data__ |
---|
19 | # define INT_GET_TI_HEADER_C int_get_ti_header_c__ |
---|
20 | # define INT_GEN_TI_HEADER_C int_gen_ti_header_c__ |
---|
21 | # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c__ |
---|
22 | # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c__ |
---|
23 | # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c__ |
---|
24 | # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field__ |
---|
25 | # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field__ |
---|
26 | # define PERTURB_REAL perturb_real__ |
---|
27 | # else |
---|
28 | # define INT_PACK_DATA int_pack_data_ |
---|
29 | # define INT_GET_TI_HEADER_C int_get_ti_header_c_ |
---|
30 | # define INT_GEN_TI_HEADER_C int_gen_ti_header_c_ |
---|
31 | # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c_ |
---|
32 | # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c_ |
---|
33 | # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c_ |
---|
34 | # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field_ |
---|
35 | # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field_ |
---|
36 | # define PERTURB_REAL perturb_real_ |
---|
37 | # endif |
---|
38 | # endif |
---|
39 | #endif |
---|
40 | |
---|
41 | /* CALL int_pack_data ( hdrbuf , hdrbufsiz * inttypesize , int_local_output_buffer, int_local_output_cursor ) */ |
---|
42 | |
---|
43 | INT_PACK_DATA ( unsigned char *buf , int *ninbytes , unsigned char *obuf, int *cursor ) |
---|
44 | { |
---|
45 | int i, lcurs ; |
---|
46 | lcurs = *cursor - 1 ; |
---|
47 | for ( i = 0 ; i < *ninbytes ; i++ ) |
---|
48 | { |
---|
49 | obuf[lcurs++] = buf[i] ; |
---|
50 | } |
---|
51 | *cursor = lcurs+1 ; |
---|
52 | } |
---|
53 | |
---|
54 | int |
---|
55 | INT_GEN_TI_HEADER_C ( char * hdrbuf, int * hdrbufsize, /* hdrbufsize is in bytes */ |
---|
56 | int * itypesize, int * typesize, |
---|
57 | int * DataHandle, char * Data, |
---|
58 | int * Count, int * code ) |
---|
59 | { |
---|
60 | int i ; |
---|
61 | char * p ; |
---|
62 | p = hdrbuf ; |
---|
63 | p += sizeof(int) ; |
---|
64 | bcopy( code, p, sizeof(int) ) ; p += sizeof(int) ; /* 2 */ |
---|
65 | bcopy( DataHandle, p, sizeof(int) ) ; p += sizeof(int) ; /* 3 */ |
---|
66 | bcopy( typesize, p, sizeof(int) ) ; p += sizeof(int) ; /* 4 */ |
---|
67 | bcopy( Count, p, sizeof(int) ) ; p += sizeof(int) ; /* 5 */ |
---|
68 | bcopy( Data, p, *Count * *typesize ) ; p += *Count * *typesize ; /* 6++ */ |
---|
69 | *hdrbufsize = (int) (p - hdrbuf) ; |
---|
70 | bcopy( hdrbufsize, hdrbuf, sizeof(int) ) ; |
---|
71 | return(0) ; |
---|
72 | } |
---|
73 | |
---|
74 | int |
---|
75 | INT_GET_TI_HEADER_C ( char * hdrbuf, int * hdrbufsize, int * n, /* hdrbufsize and n are in bytes */ |
---|
76 | int * itypesize, int * typesize, |
---|
77 | int * DataHandle, char * Data, |
---|
78 | int * Count, int * code ) |
---|
79 | { |
---|
80 | int i ; |
---|
81 | char * p ; |
---|
82 | p = hdrbuf ; |
---|
83 | bcopy( p, hdrbufsize, sizeof(int) ) ; p += sizeof(int) ; /* 1 */ |
---|
84 | bcopy( p, code, sizeof(int) ) ; p += sizeof(int) ; /* 2 */ |
---|
85 | bcopy( p, DataHandle, sizeof(int) ) ; p += sizeof(int) ; /* 3 */ |
---|
86 | bcopy( p, typesize, sizeof(int) ) ; p += sizeof(int) ; /* 4 */ |
---|
87 | bcopy( p, Count, sizeof(int) ) ; p += sizeof(int) ; /* 5 */ |
---|
88 | if ( *Count * *typesize > 0 ) { |
---|
89 | bcopy( p, Data, *Count * *typesize ) ; p += *Count * *typesize ; /* 6++ */ |
---|
90 | } |
---|
91 | *n = (int)( p - hdrbuf ) ; |
---|
92 | return(0) ; |
---|
93 | } |
---|
94 | |
---|
95 | #define MAX_FLDS 2000 |
---|
96 | static char fld_name[MAX_FLDS][256] ; |
---|
97 | static char *fld_cache[MAX_FLDS] ; |
---|
98 | static int fld_curs[MAX_FLDS] ; |
---|
99 | static int fld_bufsize[MAX_FLDS] ; |
---|
100 | static int fld = 0 ; |
---|
101 | static int numflds = 0 ; |
---|
102 | static int frst = 1 ; |
---|
103 | |
---|
104 | int INIT_STORE_PIECE_OF_FIELD () |
---|
105 | { |
---|
106 | int i ; |
---|
107 | if ( frst ) { |
---|
108 | for ( i = 0 ; i < MAX_FLDS ; i++ ) { |
---|
109 | fld_cache[i] = NULL ; |
---|
110 | } |
---|
111 | frst = 0 ; |
---|
112 | } |
---|
113 | numflds = 0 ; |
---|
114 | for ( i = 0 ; i < MAX_FLDS ; i++ ) { |
---|
115 | strcpy( fld_name[i], "" ) ; |
---|
116 | if ( fld_cache[i] != NULL ) free( fld_cache[i] ) ; |
---|
117 | fld_cache[i] = NULL ; |
---|
118 | fld_curs[i] = 0 ; |
---|
119 | fld_bufsize[i] = 0 ; |
---|
120 | } |
---|
121 | return(0) ; |
---|
122 | } |
---|
123 | |
---|
124 | int INIT_RETRIEVE_PIECES_OF_FIELD () |
---|
125 | { |
---|
126 | fld = 0 ; |
---|
127 | return(0) ; |
---|
128 | } |
---|
129 | |
---|
130 | int |
---|
131 | ADD_TO_BUFSIZE_FOR_FIELD_C ( int varname[], int * chunksize ) |
---|
132 | { |
---|
133 | int i, n ; |
---|
134 | int found ; |
---|
135 | char vname[256] ; |
---|
136 | |
---|
137 | n = varname[0] ; |
---|
138 | for ( i = 1; i <= n ; i++ ) { vname[i-1] = varname[i] ; } |
---|
139 | vname[n] = '\0' ; |
---|
140 | |
---|
141 | found = -1 ; |
---|
142 | for ( i = 0 ; i < numflds ; i++ ) { if ( !strcmp( fld_name[i], vname ) ) { found = i ; break ; } } |
---|
143 | if ( found == -1 ) { |
---|
144 | found = numflds++ ; |
---|
145 | strcpy( fld_name[found], vname ) ; |
---|
146 | fld_bufsize[found] = *chunksize ; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | fld_bufsize[found] += *chunksize ; |
---|
151 | } |
---|
152 | if ( fld_cache[found] != NULL ) { free( fld_cache[found] ) ; } |
---|
153 | fld_cache[found] = NULL ; |
---|
154 | return(0) ; |
---|
155 | } |
---|
156 | |
---|
157 | int |
---|
158 | STORE_PIECE_OF_FIELD_C ( char * buf , int varname[], int * chunksize, int *retval ) |
---|
159 | { |
---|
160 | int i, n ; |
---|
161 | int found ; |
---|
162 | char vname[256] ; |
---|
163 | |
---|
164 | n = varname[0] ; |
---|
165 | for ( i = 1; i <= n ; i++ ) { vname[i-1] = varname[i] ; } |
---|
166 | vname[n] = '\0' ; |
---|
167 | |
---|
168 | found = -1 ; |
---|
169 | for ( i = 0 ; i < numflds ; i++ ) { if ( !strcmp( fld_name[i], vname ) ) { found = i ; break ; } } |
---|
170 | if ( found == -1 ) { |
---|
171 | fprintf(stderr,"frame/pack_utils.c: field (%s) not found; was not set up with add_to_bufsize_for_field\n",vname ) ; |
---|
172 | *retval = 1 ; |
---|
173 | return(0) ; |
---|
174 | } |
---|
175 | |
---|
176 | if ( fld_cache[found] == NULL ) { |
---|
177 | fld_cache[found] = (char *) malloc( fld_bufsize[found] ) ; |
---|
178 | fld_curs[found] = 0 ; |
---|
179 | } |
---|
180 | |
---|
181 | if ( fld_curs[found] + *chunksize > fld_bufsize[found] ) { |
---|
182 | fprintf(stderr, |
---|
183 | "frame/pack_utils.c: %s would overwrite %d + %d > %d [%d]\n",vname, fld_curs[found], *chunksize, fld_bufsize[found], found ) ; |
---|
184 | *retval = 1 ; |
---|
185 | return(0) ; |
---|
186 | } |
---|
187 | |
---|
188 | bcopy( buf, fld_cache[found]+fld_curs[found], *chunksize ) ; |
---|
189 | fld_curs[found] += *chunksize ; |
---|
190 | *retval = 0 ; |
---|
191 | return(0) ; |
---|
192 | } |
---|
193 | |
---|
194 | int |
---|
195 | RETRIEVE_PIECES_OF_FIELD_C ( char * buf , int varname[], int * insize, int * outsize, int *retval ) |
---|
196 | { |
---|
197 | int i, n ; |
---|
198 | int found ; |
---|
199 | char vname[256] ; |
---|
200 | |
---|
201 | if ( fld < numflds ) { |
---|
202 | if ( fld_curs[fld] > *insize ) { |
---|
203 | fprintf(stderr,"retrieve: fld_curs[%d] (%d) > *insize (%d)\n",fld,fld_curs[fld], *insize ) ; |
---|
204 | } |
---|
205 | *outsize = ( fld_curs[fld] <= *insize ) ? fld_curs[fld] : *insize ; |
---|
206 | varname[0] = (int) strlen( fld_name[fld] ) ; |
---|
207 | for ( i = 1 ; i <= varname[0] ; i++ ) varname[i] = fld_name[fld][i-1] ; |
---|
208 | for ( i = 0 ; i < *outsize ; i++ ) buf[i] = fld_cache[fld][i] ; |
---|
209 | if ( fld_cache[fld] != NULL ) free ( fld_cache[fld] ) ; |
---|
210 | fld_cache[fld] = NULL ; |
---|
211 | fld_bufsize[fld] = 0 ; |
---|
212 | fld++ ; |
---|
213 | *retval = 0 ; |
---|
214 | } |
---|
215 | else { |
---|
216 | numflds = 0 ; |
---|
217 | *retval = -1 ; |
---|
218 | } |
---|
219 | return(0) ; |
---|
220 | } |
---|
221 | |
---|
222 | #define INDEX_2(A,B,NB) ( (B) + (A)*(NB) ) |
---|
223 | #define INDEX_3(A,B,C) INDEX_2( (A), INDEX_2( (B), (C), (me[1]-ms[1]+1) ), (me[1]-ms[1]+1)*(me[0]-ms[0]+1) ) |
---|
224 | /* flip low order bit of fp number */ |
---|
225 | int |
---|
226 | PERTURB_REAL ( float * field, int ds[], int de[], int ms[], int me[], int ps[], int pe[] ) |
---|
227 | { |
---|
228 | int i,j,k ; |
---|
229 | int le ; /* index of little end */ |
---|
230 | float x = 2.0 ; |
---|
231 | unsigned int y ; |
---|
232 | unsigned char a[4], *p ; |
---|
233 | if ( sizeof(float) != 4 ) return(-1) ; |
---|
234 | /* check endianness of machine */ |
---|
235 | bcopy ( &x, a, 4 ) ; |
---|
236 | le = 0 ; |
---|
237 | if ( a[0] == 0x40 ) le = 3 ; |
---|
238 | for ( k = ps[2]-1 ; k <= pe[2]-1 ; k++ ) |
---|
239 | for ( j = ps[1]-1 ; j <= pe[1]-1 ; j++ ) |
---|
240 | for ( i = ps[0]-1 ; i <= pe[0]-1 ; i++ ) |
---|
241 | { |
---|
242 | /* do not change zeros */ |
---|
243 | if ( field[ INDEX_3(k,j,i) ] != 0.0 ) { |
---|
244 | p = (unsigned char *)&(field[ INDEX_3(k,j,i) ] ) ; |
---|
245 | if ( *(p+le) & 1 ) { *(p+le) &= 0x7e ; } |
---|
246 | else { *(p+le) |= 1 ; } |
---|
247 | } |
---|
248 | } |
---|
249 | return(0) ; |
---|
250 | } |
---|