Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/* Vis5D version 5.1 */
/*
Vis5D system for visualizing five dimensional gridded data sets
Copyright (C) 1990 - 1997 Bill Hibbard, Johan Kellum, Brian Paul,
Dave Santek, and Andre Battaiola.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Functions to do binary I/O of floats, ints.
*
* >>>> These functions are built on top of Unix I/O functions, not stdio! <<<<
*
* The file format is assumed to be BIG-ENDIAN.
* If this code is compiled with -DLITTLE and executes on a little endian
* CPU then byte-swapping will be done.
*
* If an ANSI compiler is used prototypes and ANSI function declarations
* are used. Otherwise use K&R conventions.
*
* If we're running on a CRAY (8-byte ints and floats), conversions will
* be done as needed.
*/
/*
* Updates:
*
* April 13, 1995, brianp
* added cray_to_ieee and iee_to_cray array conversion functions.
* fixed potential cray bug in write_float4_array function.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef _CRAY
# include <string.h>
#endif
#include "binio.h"
/**********************************************************************/
/****** Byte Flipping *****/
/**********************************************************************/
#define FLIP4( n ) ( (n & 0xff000000) >> 24 \
| (n & 0x00ff0000) >> 8 \
| (n & 0x0000ff00) << 8 \
| (n & 0x000000ff) << 24 )
#define FLIP2( n ) (((unsigned short) (n & 0xff00)) >> 8 | (n & 0x00ff) << 8)
/*
* Flip the order of the 4 bytes in an array of 4-byte words.
*/
void flip4( const unsigned int *src, unsigned int *dest, int n )
{
int i;
for (i=0;i<n;i++) {
unsigned int tmp = src[i];
dest[i] = FLIP4( tmp );
}
}
/*
* Flip the order of the 2 bytes in an array of 2-byte words.
*/
void flip2( const unsigned short *src, unsigned short *dest, int n )
{
int i;
for (i=0;i<n;i++) {
unsigned short tmp = src[i];
dest[i] = FLIP2( tmp );
}
}
#ifdef _CRAY
/*****************************************************************************
*
* The following source code is in the public domain.
* Specifically, we give to the public domain all rights for future licensing
* of the source code, all resale rights, and all publishing rights.
*
* We ask, but do not require, that the following message be included in all
* derived works:
*
* Portions developed at the National Center for Supercomputing Applications at
* the University of Illinois at Urbana-Champaign.
*
* THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
* SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
* WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
*
****************************************************************************/
/** THESE ROUTINES MUST BE COMPILED ON THE CRAY ONLY SINCE THEY **/
/** REQUIRE 8-BYTES PER C-TYPE LONG **/
/* Cray to IEEE single precision */
static void c_to_if( long *t, const long *f)
{
if (*f != 0){
*t = (((*f & 0x8000000000000000) | /* sign bit */
((((*f & 0x7fff000000000000) >> 48)-16258) << 55)) + /* exp */
(((*f & 0x00007fffff000000) +
((*f & 0x0000000000800000) << 1)) << 8)); /* mantissa */
}
else *t = *f;
}
#define C_TO_IF( T, F ) \
if (F != 0) { \
T = (((F & 0x8000000000000000) | \
((((F & 0x7fff000000000000) >> 48)-16258) << 55)) + \
(((F & 0x00007fffff000000) + \
((F & 0x0000000000800000) << 1)) << 8)); \
} \
else { \
T = F; \
}
/* IEEE single precison to Cray */
static void if_to_c( long *t, const long *f)
{
if (*f != 0) {
*t = (((*f & 0x8000000000000000) |
((*f & 0x7f80000000000000) >> 7) +
(16258 << 48)) |
(((*f & 0x007fffff00000000) >> 8) | (0x0000800000000000)));
if ((*f << 1) == 0) *t = 0;
}
else *t = *f;
}
/* T and F must be longs! */
#define IF_TO_C( T, F ) \
if (F != 0) { \
T = (((F & 0x8000000000000000) | \
((F & 0x7f80000000000000) >> 7) + \
(16258 << 48)) | \
(((F & 0x007fffff00000000) >> 8) | (0x0000800000000000))); \
if ((F << 1) == 0) T = 0; \
} \
else { \
T = F; \
}
/*
* Convert an array of Cray 8-byte floats to an array of IEEE 4-byte floats.
*/
void cray_to_ieee_array( long *dest, const float *source, int n )
{
long *dst;
const long *src;
long tmp1, tmp2;
int i;
dst = dest;
src = (const long *) source;
for (i=0;i<n;i+=2) { /* add 1 in case n is odd */
c_to_if( &tmp1, &src[i] );
c_to_if( &tmp2, &src[i+1] );
*dst = (tmp1 & 0xffffffff00000000) | (tmp2 >> 32);
dst++;
}
}
/*
* Convert an array of IEEE 4-byte floats to an array of 8-byte Cray floats.
*/
void ieee_to_cray_array( float *dest, const long *source, int n )
{
long *dst;
const long *src;
int i;
long ieee;
src = source;
dst = (long *) dest;
for (i=0;i<n;i++) {
/* most significant 4-bytes of ieee contain bit pattern to convert */
if ((i&1)==0) {
/* get upper half */
ieee = src[i/2] & 0xffffffff00000000;
}
else {
/* get lower half */
ieee = src[i/2] << 32;
}
if_to_c( dst, &ieee );
dst++;
}
}
#endif /*_CRAY*/
/**********************************************************************/
/***** Read Functions *****/
/**********************************************************************/
/*
* Read a block of bytes.
* Input: f - the file descriptor to read from.
* b - address of buffer to read into.
* n - number of bytes to read.
* Return: number of bytes read, 0 if error.
*/
int read_bytes( int f, void *b, int n )
{
return read( f, b, n );
}
/*
* Read an array of 2-byte integers.
* Input: f - file descriptor
* iarray - address to put integers
* n - number of integers to read.
* Return: number of integers read.
*/
int read_int2_array( int f, short *iarray, int n )
{
#ifdef _CRAY
int i;
signed char *buffer;
int nread;
buffer = (signed char *) malloc( n * 2 );
if (!buffer) return 0;
nread = read( f, buffer, n*2 );
if (nread<=0) return 0;
nread /= 2;
for (i=0;i<nread;i++) {
/* don't forget about sign extension! */
iarray[i] = (buffer[i*2] * 256) | buffer[i*2+1];
}
free( buffer );
return nread;
#else
int nread = read( f, iarray, n*2 );
if (nread<=0)
return 0;
#ifdef LITTLE
flip2( (const unsigned short *) iarray, (unsigned short *) iarray, nread/2);
#endif
return nread/2;
#endif
}
/*
* Read an array of unsigned 2-byte integers.
* Input: f - file descriptor
* iarray - address to put integers
* n - number of integers to read.
* Return: number of integers read.
*/
int read_uint2_array( int f, unsigned short *iarray, int n )
{
#ifdef _CRAY
int i;
unsigned char *buffer;
int nread;
buffer = (unsigned char *) malloc( n * 2 );
if (!buffer) return 0;
nread = read( f, buffer, n*2 );
if (nread<=0) return 0;
nread /= 2;
for (i=0;i<nread;i++) {
iarray[i] = (buffer[i*2] << 8) | buffer[i*2+1];
}
free( buffer );
return nread;
#else
int nread = read( f, iarray, n*2 );
if (nread<=0)
return 0;
#ifdef LITTLE
flip2( iarray, iarray, nread/2 );
#endif
return nread/2;
#endif
}
/*
* Read a 4-byte integer.
* Input: f - the file descriptor to read from
* i - pointer to integer to put result into.
* Return: 1 = ok, 0 = error
*/
int read_int4( int f, int *i )
{
#ifdef LITTLE
/* read big endian and convert to little endian */
unsigned int n;
if (read( f, &n, 4 )==4) {
*i = FLIP4( n );
return 1;
}
else {
return 0;
}
#else
if (read( f, i, 4 )==4) {
# ifdef _CRAY
*i = *i >> 32;
# endif
return 1;
}
else {
return 0;
}
#endif
}
/*
* Read an array of 4-byte integers.
* Input: f - file descriptor
* iarray - address to put integers
* n - number of integers to read.
* Return: number of integers read.
*/
int read_int4_array( int f, int *iarray, int n )
{
#ifdef _CRAY
int j, nread;
int *buffer;
buffer = (int *) malloc( (n+1)*4 );
if (!buffer)
return 0;
nread = read( f, buffer, 4*n );
if (nread<=0) {
return 0;
}
nread /= 4;
for (j=0;j<nread;j++) {
if ((j&1)==0) {
iarray[j] = buffer[j/2] >> 32;
}
else {
iarray[j] = buffer[j/2] & 0xffffffff;
}
}
free( buffer );
return nread;
#else
int nread = read( f, iarray, 4*n );
if (nread<=0)
return 0;
# ifdef LITTLE
flip4( (const unsigned int *) iarray, (unsigned int *) iarray, nread/4 );
# endif
return nread/4;
#endif
}
/*
* Read a 4-byte IEEE float.
* Input: f - the file descriptor to read from.
* x - pointer to float to put result into.
* Return: 1 = ok, 0 = error
*/
int read_float4( int f, float *x )
{
#ifdef _CRAY
long buffer = 0;
if ( read( f, &buffer, 4 )==4 ) {
/* convert IEEE float (buffer) to Cray float (x) */
if_to_c( (long *) x, &buffer );
return 1;
}
return 0;
#else
# ifdef LITTLE
unsigned int n, *iptr;
if (read( f, &n, 4 )==4) {
iptr = (unsigned int *) x;
*iptr = FLIP4( n );
return 1;
}
else {
return 0;
}
# else
if (read( f, x, 4 )==4) {
return 1;
}
else {
return 0;
}
# endif
#endif
}
/*
* Read an array of 4-byte IEEE floats.
* Input: f - file descriptor
* x - address to put floats
* n - number of floats to read.
* Return: number of floats read.
*/
int read_float4_array( int f, float *x, int n )
{
#ifdef _CRAY
/* read IEEE floats into buffer, then convert to Cray format */
long *buffer;
int i, nread;
buffer = (long *) malloc( (n+1) * 4 );
if (!buffer) return 0;
nread = read( f, buffer, n*4 );
if (nread<=0) return 0;
nread /= 4;
ieee_to_cray_array( x, buffer, nread );
free( buffer );
return nread;
#else
int nread = read( f, x, 4*n );
if (nread<=0)
return 0;
#ifdef LITTLE
flip4( (const unsigned int *) x, (unsigned int*) x, nread/4 );
#endif
return nread/4;
#endif
}
/*
* Read a block of memory.
* Input: f - file descriptor
* data - address of first byte
* elements - number of elements to read
* elsize - size of each element to read (1, 2 or 4)
* Return: number of elements written
*/
int read_block( int f, void *data, int elements, int elsize )
{
if (elsize==1) {
return read( f, data, elements );
}
else if (elsize==2) {
#ifdef LITTLE
int n;
n = read( f, data, elements*2 ) / 2;
if (n==elements) {
flip2( (const unsigned short *) data, (unsigned short *) data,
elements );
}
return n;
#else
return read( f, data, elements*2 ) / 2;
#endif
}
else if (elsize==4) {
#ifdef LITTLE
int n;
n = read( f, data, elements*4 ) / 4;
if (n==elements) {
flip4( (const unsigned int *) data, (unsigned int *) data, elements );
}
return n;
#else
return read( f, data, elements*4 ) / 4;
#endif
}
else {
printf("Fatal error in read_block(): bad elsize (%d)\n", elsize );
abort();
}
return 0;
}
/**********************************************************************/
/***** Write Functions *****/
/**********************************************************************/
/*
* Write a block of bytes.
* Input: f - the file descriptor to write to.
* b - address of buffer to write.
* n - number of bytes to write.
* Return: number of bytes written, 0 if error.
*/
int write_bytes( int f, const void *b, int n )
{
return write( f, b, n );
}
/*
* Write an array of 2-byte integers.
* Input: f - file descriptor
* iarray - address to put integers
* n - number of integers to write.
* Return: number of integers written
*/
int write_int2_array( int f, const short *iarray, int n )
{
#ifdef _CRAY
printf("write_int2_array not implemented!\n");
exit(1);
#else
int nwritten;
#ifdef LITTLE
flip2( (const unsigned short *) iarray, (unsigned short *) iarray, n );
#endif
nwritten = write( f, iarray, 2*n );
#ifdef LITTLE
flip2( (const unsigned short *) iarray, (unsigned short *) iarray, n );
#endif
if (nwritten<=0)
return 0;
return nwritten/2;
#endif
}
/*
* Write an array of 2-byte unsigned integers.
* Input: f - file descriptor
* iarray - address to put integers
* n - number of integers to write.
* Return: number of integers written
*/
int write_uint2_array( int f, const unsigned short *iarray, int n )
{
#ifdef _CRAY
int i, nwritten;
unsigned char *buffer;
buffer = (unsigned char *) malloc( 2*n );
if (!buffer) return 0;
for (i=0;i<n;i++) {
buffer[i*2] = (iarray[i] >> 8) & 0xff;
buffer[i*2+1] = iarray[i] & 0xff;
}
nwritten = write( f, buffer, 2*n );
free( buffer );
if (nwritten<=0)
return 0;
else
return nwritten/2;
#else
int nwritten;
#ifdef LITTLE
flip2( iarray, (unsigned short *) iarray, n );
#endif
nwritten = write( f, iarray, 2*n );
#ifdef LITTLE
flip2( iarray, (unsigned short *) iarray, n );
#endif
if (nwritten<=0)
return 0;
else
return nwritten/2;
#endif
}
/*
* Write a 4-byte integer.
*Input: f - the file descriptor
* i - the integer
* Return: 1 = ok, 0 = error
*/
int write_int4( int f, int i )
{
#ifdef _CRAY
i = i << 32;
return write( f, &i, 4 ) > 0;
#else
# ifdef LITTLE
i = FLIP4( i );
# endif
return write( f, &i, 4 ) > 0;
#endif
}
/*
* Write an array of 4-byte integers.
* Input: f - the file descriptor
* i - the array of ints
* n - the number of ints in array
* Return: number of integers written.
*/
int write_int4_array( int f, const int *i, int n )
{
#ifdef _CRAY
int j, nwritten;
char *buf, *b, *ptr;
b = buf = (char *) malloc( n*4 + 8 );
if (!b)
return 0;
ptr = (char *) i;
for (j=0;j<n;j++) {
ptr += 4; /* skip upper 4 bytes */
*b++ = *ptr++;
*b++ = *ptr++;
*b++ = *ptr++;
*b++ = *ptr++;
}
nwritten = write( f, buf, 4*n );
free( buf );
if (nwritten<=0)
return 0;
else
return nwritten / 4;
#else
# ifdef LITTLE
int nwritten;
flip4( (const unsigned int *) i, (unsigned int *) i, n );
nwritten = write( f, i, 4*n );
flip4( (const unsigned int *) i, (unsigned int *) i, n );
if (nwritten<=0)
return 0;
else
return nwritten / 4;
# else
return write( f, i, 4*n ) / 4;
# endif
#endif
}
/*
* Write a 4-byte IEEE float.
* Input: f - the file descriptor
* x - the float
* Return: 1 = ok, 0 = error
*/
int write_float4( int f, float x )
{
#ifdef _CRAY
char buffer[8];
c_to_if( (long *) buffer, (const long *) &x );
return write( f, buffer, 4 ) > 0;
#else
# ifdef LITTLE
float y;
unsigned int *iptr = (unsigned int *) &y, temp;
y = (float) x;
temp = FLIP4( *iptr );
return write( f, &temp, 4 ) > 0;
# else
float y;
y = (float) x;
return write( f, &y, 4 ) > 0;
# endif
#endif
}
/*
* Write an array of 4-byte IEEE floating point numbers.
* Input: f - the file descriptor
* x - the array of floats
* n - number of floats in array
* Return: number of float written.
*/
int write_float4_array( int f, const float *x, int n )
{
#ifdef _CRAY
/* convert cray floats to IEEE and put into buffer */
int nwritten;
long *buffer;
buffer = (long *) malloc( n*4 + 8 );
if (!buffer)
return 0;
cray_to_ieee_array( buffer, x, n );
nwritten = write( f, buffer, 4*n );
free( buffer );
if (nwritten<=0)
return 0;
else
return nwritten / 4;
#else
# ifdef LITTLE
int nwritten;
flip4( (const unsigned int *) x, (unsigned int *) x, n );
nwritten = write( f, x, 4*n );
flip4( (const unsigned int *) x, (unsigned int *) x, n );
if (nwritten<=0)
return 0;
else
return nwritten / 4;
# else
return write( f, x, 4*n ) / 4;
# endif
#endif
}
/*
* Write a block of memory.
* Input: f - file descriptor
* data - address of first byte
* elements - number of elements to write
* elsize - size of each element to write (1, 2 or 4)
* Return: number of elements written
*/
int write_block( int f, const void *data, int elements, int elsize )
{
if (elsize==1) {
return write( f, data, elements );
}
else if (elsize==2) {
#ifdef LITTLE
int n;
flip2( (const unsigned short *) data, (unsigned short *) data, elements);
n = write( f, data, elements*2 ) / 2;
flip2( (const unsigned short *) data, (unsigned short *) data, elements);
return n;
#else
return write( f, data, elements*2 ) / 2;
#endif
}
else if (elsize==4) {
#ifdef LITTLE
int n;
flip4( (const unsigned int *) data, (unsigned int *) data, elements );
n = write( f, data, elements*4 ) / 4;
flip4( (const unsigned int *) data, (unsigned int *) data, elements );
return n;
#else
return write( f, data, elements*4 ) / 4;
#endif
}
else {
printf("Fatal error in write_block(): bad elsize (%d)\n", elsize );
abort();
}
return 0;
}