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 ) */
62 INT_PACK_DATA ( unsigned char *buf
, int *ninbytes
, unsigned char *obuf
, int *cursor
)
66 for ( i
= 0 ; i
< *ninbytes
; i
++ )
68 obuf
[lcurs
++] = buf
[i
] ;
74 INT_GEN_TI_HEADER_C ( char * hdrbuf
, int * hdrbufsize
, /* hdrbufsize is in bytes */
75 int * itypesize
, int * typesize
,
76 int * DataHandle
, char * Data
,
77 int * Count
, int * code
)
83 bcopy( code
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 2 */
84 bcopy( DataHandle
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 3 */
85 bcopy( typesize
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 4 */
86 bcopy( Count
, p
, sizeof(int) ) ; p
+= sizeof(int) ; /* 5 */
87 bcopy( Data
, p
, *Count
* *typesize
) ; p
+= *Count
* *typesize
; /* 6++ */
88 *hdrbufsize
= (int) (p
- hdrbuf
) ;
89 bcopy( hdrbufsize
, hdrbuf
, sizeof(int) ) ;
94 INT_GET_TI_HEADER_C ( char * hdrbuf
, int * hdrbufsize
, int * n
, /* hdrbufsize and n are in bytes */
95 int * itypesize
, int * typesize
,
96 int * DataHandle
, char * Data
,
97 int * Count
, int * code
)
102 bcopy( p
, hdrbufsize
, sizeof(int) ) ; p
+= sizeof(int) ; /* 1 */
103 bcopy( p
, code
, sizeof(int) ) ; p
+= sizeof(int) ; /* 2 */
104 bcopy( p
, DataHandle
, sizeof(int) ) ; p
+= sizeof(int) ; /* 3 */
105 bcopy( p
, typesize
, sizeof(int) ) ; p
+= sizeof(int) ; /* 4 */
106 bcopy( p
, Count
, sizeof(int) ) ; p
+= sizeof(int) ; /* 5 */
107 if ( *Count
* *typesize
> 0 ) {
108 bcopy( p
, Data
, *Count
* *typesize
) ; p
+= *Count
* *typesize
; /* 6++ */
110 *n
= (int)( p
- hdrbuf
) ;
114 #define MAX_FLDS 2000
115 static char fld_name
[MAX_FLDS
][256] ;
116 static char *fld_cache
[MAX_FLDS
] ;
117 static int fld_curs
[MAX_FLDS
] ;
118 static int fld_bufsize
[MAX_FLDS
] ;
120 static int numflds
= 0 ;
121 static int frst
= 1 ;
123 int INIT_STORE_PIECE_OF_FIELD ()
127 for ( i
= 0 ; i
< MAX_FLDS
; i
++ ) {
128 fld_cache
[i
] = NULL
;
133 for ( i
= 0 ; i
< MAX_FLDS
; i
++ ) {
134 strcpy( fld_name
[i
], "" ) ;
135 if ( fld_cache
[i
] != NULL
) free( fld_cache
[i
] ) ;
136 fld_cache
[i
] = NULL
;
143 int INIT_RETRIEVE_PIECES_OF_FIELD ()
150 ADD_TO_BUFSIZE_FOR_FIELD_C ( int varname
[], int * chunksize
)
157 for ( i
= 1; i
<= n
; i
++ ) { vname
[i
-1] = varname
[i
] ; }
161 for ( i
= 0 ; i
< numflds
; i
++ ) { if ( !strcmp( fld_name
[i
], vname
) ) { found
= i
; break ; } }
164 strcpy( fld_name
[found
], vname
) ;
165 fld_bufsize
[found
] = *chunksize
;
169 fld_bufsize
[found
] += *chunksize
;
171 if ( fld_cache
[found
] != NULL
) { free( fld_cache
[found
] ) ; }
172 fld_cache
[found
] = NULL
;
177 STORE_PIECE_OF_FIELD_C ( char * buf
, int varname
[], int * chunksize
, int *retval
)
184 for ( i
= 1; i
<= n
; i
++ ) { vname
[i
-1] = varname
[i
] ; }
188 for ( i
= 0 ; i
< numflds
; i
++ ) { if ( !strcmp( fld_name
[i
], vname
) ) { found
= i
; break ; } }
191 fprintf(stderr
,"frame/pack_utils.c: field (%s) not found; was not set up with add_to_bufsize_for_field\n",vname
) ;
197 if ( fld_cache
[found
] == NULL
) {
198 fld_cache
[found
] = (char *) malloc( fld_bufsize
[found
] ) ;
199 fld_curs
[found
] = 0 ;
202 if ( fld_curs
[found
] + *chunksize
> fld_bufsize
[found
] ) {
205 "frame/pack_utils.c: %s would overwrite %d + %d > %d [%d]\n",vname
, fld_curs
[found
], *chunksize
, fld_bufsize
[found
], found
) ;
211 bcopy( buf
, fld_cache
[found
]+fld_curs
[found
], *chunksize
) ;
212 fld_curs
[found
] += *chunksize
;
218 RETRIEVE_PIECES_OF_FIELD_C ( char * buf
, int varname
[], int * insize
, int * outsize
, int *retval
)
224 if ( fld
< numflds
) {
226 if ( fld_curs
[fld
] > *insize
) {
227 fprintf(stderr
,"retrieve: fld_curs[%d] (%d) > *insize (%d)\n",fld
,fld_curs
[fld
], *insize
) ;
230 *outsize
= ( fld_curs
[fld
] <= *insize
) ? fld_curs
[fld
] : *insize
;
231 bcopy( fld_cache
[fld
], buf
, *outsize
) ;
232 varname
[0] = (int) strlen( fld_name
[fld
] ) ;
233 for ( i
= 1 ; i
<= varname
[0] ; i
++ ) varname
[i
] = fld_name
[fld
][i
-1] ;
234 if ( fld_cache
[fld
] != NULL
) free ( fld_cache
[fld
] ) ;
235 fld_cache
[fld
] = NULL
;
236 fld_bufsize
[fld
] = 0 ;
247 #define INDEX_2(A,B,NB) ( (B) + (A)*(NB) )
248 #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) )
249 /* flip low order bit of fp number */
251 PERTURB_REAL ( float * field
, int ds
[], int de
[], int ms
[], int me
[], int ps
[], int pe
[] )
254 int le
; /* index of little end */
257 unsigned char a
[4], *p
;
258 if ( sizeof(float) != 4 ) return(-1) ;
259 /* check endianness of machine */
262 if ( a
[0] == 0x40 ) le
= 3 ;
263 for ( k
= ps
[2]-ms
[2] ; k
<= pe
[2]-ms
[2] ; k
++ )
264 for ( j
= ps
[1]-ms
[1] ; j
<= pe
[1]-ms
[1] ; j
++ )
265 for ( i
= ps
[0]-ms
[0] ; i
<= pe
[0]-ms
[0] ; i
++ )
267 /* do not change zeros */
268 if ( field
[ INDEX_3(k
,j
,i
) ] != 0.0 ) {
269 p
= (unsigned char *)&(field
[ INDEX_3(k
,j
,i
) ] ) ;
270 if ( *(p
+le
) & 1 ) { *(p
+le
) &= 0x7e ; }
271 else { *(p
+le
) |= 1 ; }
277 int INSPECT_HEADER ( char * buf
, int * sz
, int * line
)
281 fprintf(stderr
,"INSPECT_HEADER: line = %d ", *line
) ;
282 if ( buf
!= NULL
&& sz
!= NULL
) {
283 for ( i
= 0 ; i
< *sz
&& i
< 256 ; i
++ ) { if ( (buf
[i
] >= 'a' && buf
[i
] <= 'z') || buf
[i
] == '_' ||
284 (buf
[i
] >= 'A' && buf
[i
] <= 'Z') ||
285 (buf
[i
] >= '0' && buf
[i
] <= '9') ) fprintf(stderr
,"%c",buf
[i
]) ;
287 fprintf(stderr
,"\n") ;
293 /* note that these work the same as the routines in tools/misc.c, but are Fortran callable.
294 They must be kept in sync, functionally. */
297 RESET_MASK ( unsigned int * mask
, int *e
)
302 w
= *e
/ (8*sizeof(int)-1) ;
304 m
= ~( n
<< *e
% (8*sizeof(int)-1) ) ;
305 if ( w
>= 0 && w
< IO_MASK_SIZE
) {
311 SET_MASK ( unsigned int * mask
, int *e
)
316 w
= *e
/ (8*sizeof(int)-1) ;
318 m
= ( n
<< *e
% (8*sizeof(int)-1) ) ;
319 if ( w
>= 0 && w
< IO_MASK_SIZE
) {
324 /* this is slightly different from in tools dir since it returns result as argument, not function */
325 /* definition of IO_MASK_SIZE comes from build and must be uniform with frame/module_domain_type.F and
326 version of this function in tools dir */
328 GET_MASK ( unsigned int * mask
, int *e
, int * retval
)
333 w
= *e
/ (8*sizeof(int)-1) ; /* 8 is number of bits per byte */
334 if ( w
>= 0 && w
< IO_MASK_SIZE
) {
336 n
= ( 1 << *e
% (8*sizeof(int)-1) ) ;;
337 *retval
= ( (m
& n
) != 0 ) ;
344 # ifndef WRAP_MALLOC_ALIGNMENT
345 # define WRAP_MALLOC_ALIGNMENT 128
347 # define _XOPEN_SOURCE 600
349 void *malloc(size_t size
)
352 if (posix_memalign(&tmp
, WRAP_MALLOC_ALIGNMENT
, size
) == 0)
364 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock
367 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock__
369 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock_
373 # if !defined(MS_SUA) && !defined(_WIN32)
374 # include <sys/time.h>
375 RSL_INTERNAL_MICROCLOCK ()
378 struct timezone tzp
;
379 int isec
; /* seconds */
380 int usec
; /* microseconds */
382 gettimeofday( &tb
, &tzp
) ;
385 msecs
= 1000000 * isec
+ usec
;