/***************************************************************************** * * NCSA HDF version 3.10 * July 1, 1990 * * NCSA HDF Version 3.10 source code and documentation are 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 * *****************************************************************************/ #ifdef RCSID static char RcsId??(??) = "@(#)$Revision: 3.10 $" #endif /* $Header: /pita/work/HDF/dev/RCS/src/dfkit.c,v 3.10 90/07/02 09:37:52 clow beta $ $Log: dfkit.c,v $ * Revision 3.10 90/07/02 09:37:52 clow * enclosed DFconvert macro and function with an #ifdef on FUNC_CONV * so that the selection between these is a compilation option * * Revision 3.9 90/06/21 10:40:53 clow * Changes UNICOS conversion functions for non-UNICOS env to be similar * to the UNICOS fortran functions, and * "#if 0" out the DFconvert function, use macro in dfconvert.h * * Revision 3.8 90/06/07 17:39:28 clow * bug fix in conversion routines * */ /* * Changes: * DFconvert() calls FORTRAN conversion routines if compiled with UNICOS * DFconvert() takes an array rather than a single number * Register parameters have been declared * Pointers are now used rather than array indexing */ /* Modified DFconvert so that it takes an array rather than a single number */ /* Now call FORTRAN conversion routines */ #include #include "df.h" union fpx { float f; long l; }; union float_uint_uchar { float32 f; int32 i; unsigned char c??(4??); }; char *DFIstrncpy(dest, source, len) register char *source, *dest; int len; { for(; (--len > 0) && (*dest++ = *source++);); if (!len) *dest = '\0'; return(dest); } #ifdef FUNC_CONV /* function convert, otherwise, this function is done in macro (dfconvert.h) */ int DFconvert(source, dest, ntype, sourcetype, desttype, size) char *source, *dest; int ntype; register int sourcetype, desttype; int size; { register char t; /* Temporary, used in byte-swapping */ register int i; int status; /********************************************************/ /* this routine only converts floating point numbers */ /********************************************************/ if( ntype == DFNT_FLOAT ) { /********************************************************/ /* treat pc -> ieee and ieee -> pc as a special case */ /********************************************************/ if( ( ( sourcetype == DFNTF_IEEE ) && ( desttype == DFNTF_PC ) ) || ( ( sourcetype == DFNTF_PC ) && ( desttype == DFNTF_IEEE ) ) ) { for( i=0; il == 0) tmp = 0; else { tmp = (C_EMASK & cray_fp->l); if (tmp < MINEXP || tmp > MAXEXP) { DFerror = DFE_BADFP; return(-1); } tmp = ((tmp + C2I_diff) << 7) | ( (cray_fp->l & C_FMASK) << 8 ) | ( (cray_fp->l & C_SMASK)); } DFmovmem((char *)&tmp, ieee_fp, 4); } return(0); } #endif /* 0 */ /* On UNICOS, this function is defined as a fortran function */ #ifndef UNICOS /* Conversion from IEEE floating point format to Cray format */ /* shut lint up */ /* ARGSUSED */ SCUP32(ieee_fp, cray_fp, size, status) char *cray_fp; char *ieee_fp; int *size; int *status; { DFerror = DFE_BADCONV; *status = -1; } #endif /*!UNICOS*/ #if 0 register union fpx *cray_fp; register char *ieee_fp; int size; { long tmp; register int i; register long I2C_diff; I2C_diff = (CRAY_BIAS - IEEE_BIAS + 1) << 23; for (i=0; il = tmp & I_EMASK) == 0) { cray_fp->l = 0; continue; } cray_fp->l += I2C_diff; cray_fp->l = (cray_fp->l<< 25) | ( (tmp & I_FMASK) << 24) | ( (tmp & I_SMASK) << 32) | C_IMPLICIT; } return (0); } #endif /* 0 */ DFIc2fstr(str, len) char* str; int len; { int i; for(i=0; (str??(i??)); i++); for(; i=0 && (!isprint(str??(i??)) || !isgraph(str??(i??))); i--) /*EMPTY*/; #else /* IBM */ /* END JES 90 */ for(i=len-1;i>=0 && (!isascii(str??(i??)) || !isgraph(str??(i??))); i--) /*EMPTY*/; #endif /* IBM */ /* JES 90 */ cstr = DFIgetspace(i+2); cstr??(i+1??) = '\0'; for (; i>=0; i--) cstr??(i??) = str??(i??); return cstr; } #ifdef IBM /* JES 90 */ int DFCVv2iF(in, out, size) /* JES 90 */ #else /* JES 90 */ int DFCVvaxF2ieeeF(in, out, size) #endif /* JES 90 */ union float_uint_uchar in??(??), out??(??); int size; { register unsigned char exp; int i; for (i=0; i> 7); /* extract exponent */ if (!exp && !in??(i??).c??(1??)) out??(i??).i = 0; /* zero value */ else if (exp>2) { /* normal value */ out??(i??).c??(0??) = in??(i??).c??(1??) - 1; /* subtracts 2 from exponent */ /* copy mantissa, LSB of exponent */ out??(i??).c??(1??) = in??(i??).c??(0??); out??(i??).c??(2??) = in??(i??).c??(3??); out??(i??).c??(3??) = in??(i??).c??(2??); } else if (exp) { /* denormalized number */ register int shft; out??(i??).c??(0??) = in??(i??).c??(1??) & 0x80; /* keep sign, zero exponent */ shft = 3 - exp; /* shift original mant by 1 or 2 to get denormalized mant */ /* prefix mantissa with '1'b or '01'b as appropriate */ out??(i??).c??(1??) = ((in??(i??).c??(0??) & 0x7f) >> shft) | (0x10 << exp); out??(i??).c??(2??) = (in??(i??).c??(0??) << (8-shft)) | (in??(i??).c??(3??) >> shft); out??(i??).c??(3??) = (in??(i??).c??(3??) << (8-shft)) | (in??(i??).c??(2??) >> shft); } else { /* sign=1 -> infinity or NaN */ out??(i??).c??(0??) = 0xff; /* set exp to 255 */ /* copy mantissa */ out??(i??).c??(1??) = in??(i??).c??(0??) | 0x80; /* LSB of exp = 1 */ out??(i??).c??(2??) = in??(i??).c??(3??); out??(i??).c??(3??) = in??(i??).c??(2??); } } return(0); } #ifdef IBM /* JES 90 */ int DFCVi2vF(in, out, size) /* JES 90 */ #else /* JES 90 */ int DFCVieeeF2vaxF(in, out, size) #endif /* JES 90 */ union float_uint_uchar in??(??), out??(??); int size; { register unsigned char exp; int i; for (i=0; i> 7); /* extract exponent */ if (exp) { /* non-zero exponent */ /* copy mantissa, last bit of exponent */ out??(i??).c??(0??) = in??(i??).c??(1??); out??(i??).c??(2??) = in??(i??).c??(3??); out??(i??).c??(3??) = in??(i??).c??(2??); if (exp<254) /* normal value */ out??(i??).c??(1??) = in??(i??).c??(0??) + 1; /* actually adds two to exp */ else { /* infinity or NaN */ if (exp==254) /* unrepresentable - OFL */ out??(i??).i = 0; /* set mant=0 for overflow */ out??(i??).c??(0??) &= 0x7f; /* set last bit of exp to 0 */ out??(i??).c??(1??) = 0x80; /* sign=1 exp=0 -> OFL or NaN */ } } else if (in??(i??).c??(1??) & 0x60) { /* denormalized value */ register int shft; shft = (in??(i??).c??(1??) & 0x40) ? 1 : 2; /* shift needed to normalize */ /* shift mantissa */ /* note last bit of exp set to 1 implicitly */ out??(i??).c??(0??) = (in??(i??).c??(1??) << shft) & (in??(i??).c??(2??) >> (8-shft)); out??(i??).c??(3??) = (in??(i??).c??(2??) << shft) & (in??(i??).c??(3??) >> (8-shft)); out??(i??).c??(2??) = in??(i??).c??(3??) << shft; out??(i??).c??(1??) = (in??(i??).c??(0??) & 0x80); /* sign */ if (shft==1) { /* set exp to 2 */ out??(i??).c??(1??) |= 0x01; out??(i??).c??(0??) &= 0x7f; /* set LSB of exp to 0 */ } } else out??(i??).i = 0; /* zero */ } return(0); } /* ------------------------------------------------------------------ * procedure: cfi32s(float ief, float ibf) * ------------------------------------------------------------------- * Convert floating point, IEEE 32-bit to IBM 32-bit, i.e float * * input: ief Array of 32-bit IEEE floating point numbers, * single precision. * nf Number of elements in ief to convert, integer. * output: ibf Array of IBM floating point numbers, float values * Can be located at the same space as ief. * * Format (bits, left to right): | Exponent bias: * sign exponent mantissa | * IBM 1 7 24 | 64 dec or 40 hex * IEEE 1 8 23 | 127 * | * Usage notes: * 1. Infinite IEEE values are converted to the largest IBM values. * 2. NaN (Not a Number) values are converted to the largest values * like infinities. * 3. Denormalized and zero values are kept intact (they are * analogous in both IEEE and IBM standards. * 4. Precision in the mantissa could be lost by rounding off the * least significant bits. 0 <= |error| <= 0.24E-6 * (From 0 to 3 least significant bits out of 24 mantissa bits * could be rounded.) * ------------------------------------------------------------------- * PROCESSOR - C, AIX * * LIBRARY - NONE * * DEPENDENCES - NONE * * ENTRY POINT - cfi32s * * ERRORS - NOT FOUND * * 06/16/89 Valery I. Garger, Technology Integration * Group, CNSF, Cornell University * 01/02/90 Adapted for HDF by jes (Joan Slottow ) * UCLA - OAC * ------------------------------------------------------------------- * Change log: No. * ------------------------------------------------------------------- */ #define last 0x000000ff #define impl 0x00800000 #define sign 0x80000000 #define tiss 0x007fffff int cfi32s( ieee, ibm, size ) /* JES 90 */ int32 ieee??( ??), ibm??( ??), size; /* JES 90 */ { int32 j, k, ibmsign, ibmexp, ibmfrac; /* JES 90 */ for( j = 0; j < size; j++ ) { ibmfrac = ieee??( j ??); ibmsign = ieee??( j ??) & sign; ibmexp = ( ieee??( j ??) >> 23 ) & last; if( ibmexp != 0 ) { if( ibmexp == 255 ) { ibmexp = 378; ibmfrac = tiss; } ibmexp = ibmexp - 127 + 256 +1; k = ibmexp%4; ibmexp = ibmexp/4; if( k != 0 ) ibmexp = ibmexp + 1; ibmexp = ibmexp << 24 ; ibmfrac = (ibmfrac & tiss) | impl ; if (k != 0) ibmfrac = ( ibmfrac + (1 << (3-k) ) ) >> (4-k); } ibm??( j ??) = ibmsign | ibmexp | ibmfrac; } return(0); /* JES 90 */ } /* ------------------------------------------------------------------ * procedure: cfsi32(float ibmfp,float ieeefp) * ------------------------------------------------------------------- * PROCESSOR - C, AIX * * LIBRARY - NONE * * DEPENDENCES - NONE * * ENTRY POINT - cfsi32 * * ERRORS - NOT FOUND * * 06/16/89 Valery I. Garger, Technology Integration * Group, CNSF, Cornell University * 01/02/90 put into HDF by Joan Slottow (jes) UCLA-OAC *-------------------------------------------------------------------- * Change log: No. * ------------------------------------------------------------------- * Convert floating point, 32-bit IBM to 32-bit IEEE standard. * * input: ibmfp Array of IBM floating point numbers, float values. * nf Number of elements in ibmfp to convert, int. * output: ieeefp Array of 32-bit IEEE floating point numbers. * * Format (bits, left to right): | Exponent bias: * sign exponent mantissa | * IBM 1 7 24 | 64 dec or 40 hex * IEEE 1 8 23 | 127 * | * Usage notes: * 1. IBM values that do not conform to IEEE standard (because IBM * standard can express larger and smaller values than IEEE ) * are converted to either infinite IEEE values (positive or * negative) or to zero. * 2. Denormalized and zero values are kept intact (they are * analogous in both IEEE and IBM standards. * 3. Conversion does not incur the lost of mantissa accuracy. * ------------------------------------------------------------------- */ #define exp 0x7F000000 #define sign 0x80000000 #define tis 0x00FFFFFF #define etis 0x007FFFFF int cfsi32( ibm, ieee, size ) /* JES 90 */ int32 ibm??( ??), ieee??( ??), size; /* JES 90 */ { int32 j, ibmsign, ibmexp, ibmfrac, it, k; /* JES 90 */ for( j = 0; j < size; j++ ) { ibmsign = ibm??( j ??) & sign; ibmexp = ibm??( j ??) & exp; ibmfrac = ibm??( j ??) & tis; it = ibmfrac << 8; for(k = 0; (k < 5) && (it >= 0); k++ ) it = it << 1; if( k < 4 ) { ibmfrac = (it >> 8) & etis; ibmexp = (ibmexp >> 22) - 256 + 127 - k - 1; if( ibmexp < 0 ) ibmexp = ibmfrac = 0; if( ibmexp >= 255 ) { ibmexp = 255; ibmfrac = 0; } ibmexp = ibmexp << 23; } ieee??( j ??) = ibmsign | ibmexp | ibmfrac; } return(0); /* JES 90 */ } /*********************************************************/ /* Name: DFCVa2eC */ /* added by JES 89 */ /* ASCII to EBCDIC conversion routine */ /* DFCVa2eC( string, len ) */ /* string = ASCII character string */ /* len = # of characters in string */ /*********************************************************/ int DFCVa2eC( string, len ) char string??( ??); int len; { int i; static char ebcdic??( 128 ??) = { 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x22, 0x28, 0x35, 0x29, 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x8b, 0x6a, 0x9b, 0xa1, 0x07, } ; for(i = 0; i < len; i++ ) string??( i ??) = ebcdic??( ( uint16 )string??( i ??) ??); return( 0 ); } /*********************************************************/ /* Name: DFCVe2aC */ /* added by jes 89 */ /* EBCDIC to ASCII conversion routine */ /* DFCVe2aC( string, len ) */ /* string = ASCII character string */ /* len = # of characters in string */ /*********************************************************/ int DFCVe2aC( string, len ) char string??( ??); int len; { int i; static char ascii??( 256 ??) = { 0x00, 0x01, 0x02, 0x03, 0x20, 0x09, 0x20, 0x7f, 0x20, 0x20, 0x20, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x20, 0x0a, 0x08, 0x20, 0x18, 0x19, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x1c, 0x13, 0x20, 0x0a, 0x17, 0x1b, 0x1d, 0x1f, 0x20, 0x20, 0x20, 0x05, 0x06, 0x07, 0x20, 0x20, 0x16, 0x20, 0x20, 0x1e, 0x20, 0x04, 0x20, 0x20, 0x20, 0x20, 0x14, 0x15, 0x20, 0x1a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x5b, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, 0x26, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, 0x2d, 0x2f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7c, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, 0x20, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x20, 0x7b, 0x20, 0x20, 0x20, 0x5c, 0x20, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x20, 0x7d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x20, 0x20, 0x20, 0x5b, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x5d, 0x20, 0x20, 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x5c, 0x20, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } ; for( i = 0; i < len; i++ ) string??( i ??) = ascii??( ( uint16 )string??( i ??) ??); return( 0 ); }