6 #include "../inc/streams.h"
10 # define INT_PACK_DATA int_pack_data
11 # define INT_GET_TI_HEADER_C int_get_ti_header_c
12 # define INT_GEN_TI_HEADER_C int_gen_ti_header_c
13 # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c
14 # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c
15 # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c
16 # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field
17 # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field
18 # define PERTURB_REAL perturb_real
19 # define INSPECT_HEADER inspect_header
20 # define RESET_MASK reset_mask
21 # define SET_MASK set_mask
22 # define GET_MASK get_mask
25 # define INT_PACK_DATA int_pack_data__
26 # define INT_GET_TI_HEADER_C int_get_ti_header_c__
27 # define INT_GEN_TI_HEADER_C int_gen_ti_header_c__
28 # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c__
29 # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c__
30 # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c__
31 # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field__
32 # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field__
33 # define PERTURB_REAL perturb_real__
34 # define INSPECT_HEADER inspect_header__
35 # define RESET_MASK reset_mask__
36 # define SET_MASK set_mask__
37 # define GET_MASK get_mask__
39 # define INT_PACK_DATA int_pack_data_
40 # define INT_GET_TI_HEADER_C int_get_ti_header_c_
41 # define INT_GEN_TI_HEADER_C int_gen_ti_header_c_
42 # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c_
43 # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c_
44 # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c_
45 # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field_
46 # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field_
47 # define PERTURB_REAL perturb_real_
48 # define INSPECT_HEADER inspect_header_
49 # define RESET_MASK reset_mask_
50 # define SET_MASK set_mask_
51 # define GET_MASK get_mask_
56 #ifdef MEMCPY_FOR_BCOPY
57 # define bcopy(A,B,C) memcpy((B),(A),(C))
60 /* CALL int_pack_data ( hdrbuf , hdrbufsiz * inttypesize , int_local_output_buffer, int_local_output_cursor ) */
63 INT_PACK_DATA ( unsigned char *buf
, int *ninbytes
, unsigned char *obuf
, int *cursor
)
67 for ( i
= 0 ; i
< *ninbytes
; i
++ )
69 obuf
[lcurs
++] = buf
[i
] ;
75 INT_GEN_TI_HEADER_C ( char * hdrbuf
, int * hdrbufsize
, /* hdrbufsize is in bytes */
76 int * itypesize
, int * typesize
,
77 int * DataHandle
, char * Data
,
78 int * Count
, int * code
)
84 bcopy( code
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 2 */
85 bcopy( DataHandle
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 3 */
86 bcopy( typesize
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 4 */
87 bcopy( Count
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 5 */
88 bcopy( Data
, p
, *Count
* *typesize
) ; p
+= *Count
* *typesize
; /* 6++ */
89 *hdrbufsize
= (int) (p
- hdrbuf
) ;
90 bcopy( hdrbufsize
, hdrbuf
, sizeof(int) ) ;
95 INT_GET_TI_HEADER_C ( char * hdrbuf
, int * hdrbufsize
, int * n
, /* hdrbufsize and n are in bytes */
96 int * itypesize
, int * typesize
,
97 int * DataHandle
, char * Data
,
98 int * Count
, int * code
)
103 bcopy( p
, hdrbufsize
, sizeof(int) ) ; p
+= sizeof(int) ; /* 1 */
104 bcopy( p
, code
, sizeof(int) ) ; p
+= sizeof(int) ; /* 2 */
105 bcopy( p
, DataHandle
, sizeof(int) ) ; p
+= sizeof(int) ; /* 3 */
106 bcopy( p
, typesize
, sizeof(int) ) ; p
+= sizeof(int) ; /* 4 */
107 bcopy( p
, Count
, sizeof(int) ) ; p
+= sizeof(int) ; /* 5 */
108 if ( *Count
* *typesize
> 0 ) {
109 bcopy( p
, Data
, *Count
* *typesize
) ; p
+= *Count
* *typesize
; /* 6++ */
111 *n
= (int)( p
- hdrbuf
) ;
115 #define MAX_FLDS 2000
116 static char fld_name
[MAX_FLDS
][256] ;
117 static char *fld_cache
[MAX_FLDS
] ;
118 static int fld_curs
[MAX_FLDS
] ;
119 static int fld_bufsize
[MAX_FLDS
] ;
121 static int numflds
= 0 ;
122 static int frst
= 1 ;
124 int INIT_STORE_PIECE_OF_FIELD ()
128 for ( i
= 0 ; i
< MAX_FLDS
; i
++ ) {
129 fld_cache
[i
] = NULL
;
134 for ( i
= 0 ; i
< MAX_FLDS
; i
++ ) {
135 strcpy( fld_name
[i
], "" ) ;
136 if ( fld_cache
[i
] != NULL
) free( fld_cache
[i
] ) ;
137 fld_cache
[i
] = NULL
;
144 int INIT_RETRIEVE_PIECES_OF_FIELD ()
151 ADD_TO_BUFSIZE_FOR_FIELD_C ( int varname
[], int * chunksize
)
158 for ( i
= 1; i
<= n
; i
++ ) { vname
[i
-1] = varname
[i
] ; }
162 for ( i
= 0 ; i
< numflds
; i
++ ) { if ( !strcmp( fld_name
[i
], vname
) ) { found
= i
; break ; } }
165 strcpy( fld_name
[found
], vname
) ;
166 fld_bufsize
[found
] = *chunksize
;
170 fld_bufsize
[found
] += *chunksize
;
172 if ( fld_cache
[found
] != NULL
) { free( fld_cache
[found
] ) ; }
173 fld_cache
[found
] = NULL
;
178 STORE_PIECE_OF_FIELD_C ( char * buf
, int varname
[], int * chunksize
, int *retval
)
185 for ( i
= 1; i
<= n
; i
++ ) { vname
[i
-1] = varname
[i
] ; }
189 for ( i
= 0 ; i
< numflds
; i
++ ) { if ( !strcmp( fld_name
[i
], vname
) ) { found
= i
; break ; } }
192 fprintf(stderr
,"frame/pack_utils.c: field (%s) not found; was not set up with add_to_bufsize_for_field\n",vname
) ;
198 if ( fld_cache
[found
] == NULL
) {
199 fld_cache
[found
] = (char *) malloc( fld_bufsize
[found
] ) ;
200 fld_curs
[found
] = 0 ;
203 if ( fld_curs
[found
] + *chunksize
> fld_bufsize
[found
] ) {
206 "frame/pack_utils.c: %s would overwrite %d + %d > %d [%d]\n",vname
, fld_curs
[found
], *chunksize
, fld_bufsize
[found
], found
) ;
212 bcopy( buf
, fld_cache
[found
]+fld_curs
[found
], *chunksize
) ;
213 fld_curs
[found
] += *chunksize
;
219 RETRIEVE_PIECES_OF_FIELD_C ( char * buf
, int varname
[], int * insize
, int * outsize
, int *retval
)
225 if ( fld
< numflds
) {
227 if ( fld_curs
[fld
] > *insize
) {
228 fprintf(stderr
,"retrieve: fld_curs[%d] (%d) > *insize (%d)\n",fld
,fld_curs
[fld
], *insize
) ;
231 *outsize
= ( fld_curs
[fld
] <= *insize
) ? fld_curs
[fld
] : *insize
;
232 bcopy( fld_cache
[fld
], buf
, *outsize
) ;
233 varname
[0] = (int) strlen( fld_name
[fld
] ) ;
234 for ( i
= 1 ; i
<= varname
[0] ; i
++ ) varname
[i
] = fld_name
[fld
][i
-1] ;
235 if ( fld_cache
[fld
] != NULL
) free ( fld_cache
[fld
] ) ;
236 fld_cache
[fld
] = NULL
;
237 fld_bufsize
[fld
] = 0 ;
248 #define INDEX_2(A,B,NB) ( (B) + (A)*(NB) )
249 #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) )
250 /* flip low order bit of fp number */
252 PERTURB_REAL ( float * field
, int ds
[], int de
[], int ms
[], int me
[], int ps
[], int pe
[] )
255 int le
; /* index of little end */
258 unsigned char a
[4], *p
;
259 if ( sizeof(float) != 4 ) return(-1) ;
260 /* check endianness of machine */
263 if ( a
[0] == 0x40 ) le
= 3 ;
264 for ( k
= ps
[2]-ms
[2] ; k
<= pe
[2]-ms
[2] ; k
++ )
265 for ( j
= ps
[1]-ms
[1] ; j
<= pe
[1]-ms
[1] ; j
++ )
266 for ( i
= ps
[0]-ms
[0] ; i
<= pe
[0]-ms
[0] ; i
++ )
268 /* do not change zeros */
269 if ( field
[ INDEX_3(k
,j
,i
) ] != 0.0 ) {
270 p
= (unsigned char *)&(field
[ INDEX_3(k
,j
,i
) ] ) ;
271 if ( *(p
+le
) & 1 ) { *(p
+le
) &= 0x7e ; }
272 else { *(p
+le
) |= 1 ; }
278 int INSPECT_HEADER ( char * buf
, int * sz
, int * line
)
282 fprintf(stderr
,"INSPECT_HEADER: line = %d ", *line
) ;
283 if ( buf
!= NULL
&& sz
!= NULL
) {
284 for ( i
= 0 ; i
< *sz
&& i
< 256 ; i
++ ) { if ( (buf
[i
] >= 'a' && buf
[i
] <= 'z') || buf
[i
] == '_' ||
285 (buf
[i
] >= 'A' && buf
[i
] <= 'Z') ||
286 (buf
[i
] >= '0' && buf
[i
] <= '9') ) fprintf(stderr
,"%c",buf
[i
]) ;
288 fprintf(stderr
,"\n") ;
294 /* note that these work the same as the routines in tools/misc.c, but are Fortran callable.
295 They must be kept in sync, functionally. */
298 RESET_MASK ( unsigned int * mask
, int *e
)
303 w
= *e
/ (8*sizeof(int)-1) ;
305 m
= ~( n
<< *e
% (8*sizeof(int)-1) ) ;
306 if ( w
>= 0 && w
< IO_MASK_SIZE
) {
312 SET_MASK ( unsigned int * mask
, int *e
)
317 w
= *e
/ (8*sizeof(int)-1) ;
319 m
= ( n
<< *e
% (8*sizeof(int)-1) ) ;
320 if ( w
>= 0 && w
< IO_MASK_SIZE
) {
325 /* this is slightly different from in tools dir since it returns result as argument, not function */
326 /* definition of IO_MASK_SIZE comes from build and must be uniform with frame/module_domain_type.F and
327 version of this function in tools dir */
329 GET_MASK ( unsigned int * mask
, int *e
, int * retval
)
334 w
= *e
/ (8*sizeof(int)-1) ; /* 8 is number of bits per byte */
335 if ( w
>= 0 && w
< IO_MASK_SIZE
) {
337 n
= ( 1 << *e
% (8*sizeof(int)-1) ) ;;
338 *retval
= ( (m
& n
) != 0 ) ;
345 # ifndef WRAP_MALLOC_ALIGNMENT
346 # define WRAP_MALLOC_ALIGNMENT 128
348 # define _XOPEN_SOURCE 600
350 void *malloc(size_t size
)
353 if (posix_memalign(&tmp
, WRAP_MALLOC_ALIGNMENT
, size
) == 0)
365 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock
368 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock__
370 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock_
374 # if !defined(MS_SUA) && !defined(_WIN32)
375 # include <sys/time.h>
376 int RSL_INTERNAL_MICROCLOCK ()
379 struct timezone tzp
;
380 int isec
; /* seconds */
381 int usec
; /* microseconds */
383 gettimeofday( &tb
, &tzp
) ;
386 msecs
= 1000000 * isec
+ usec
;