In article <1994Apr20.165820.2427@front>, per@front.se (Per Lindberg) writes: |>I am looking for functions which runs on a VAX which can convert VAX |>single- and double precision binary floating point numbers (F_Float |>and D_Float) to/from the corresponding IEEE formats ("basic single- |>and double precision). The Run-Time Library routine CVT$CONVERT_FLOAT can be used to convert between any two of the following floating-point datatypes: VAX (F, D, G, H) IEEE (single (S), double (T), quad (X)) IBM System\360 (single and double) CRAY (Single) For IEEE types, either big-endian or little-endian is supported, but the routine does not support conversion from big-endian IEEE to little-endian IEEE, or the reverse. (However, this is easily accomplished in your own code.) Little-endian IEEE is used by Digital systems based on the MIPS and Alpha AXP processors as well as all systems based on the Intel x86 series processors (IBM PCs, etc.). Big-endian IEEE is used by most non-Digital RISC workstation vendors. The routine itself is supported in all versions of OpenVMS AXP (support for IEEE X-float is new in V6.1) and in OpenVMS VAX as of V6.1. In addition, if you have installed DEC Fortran for OpenVMS VAX V6.0 or later, CVT$CONVERT_FLOAT is added to SYS$LIBRARY:STARLET.OLB so that you may link against it. The calling format for CVT$CONVERT_FLOAT is as follows: CVT$CONVERT_FLOAT (input_value, input_type_code, output_value, output_type_code, options) Returns OpenVMS usage cond_value type longword (unsigned) access write only mechanism by value Argument input_value OpenVMS usage varying_arg type unspecified access read only mechanism by reference The input_value argument is the address of a data area containing a floating point number that is to be converted. The input_value argument may contain floating point data in F_Floating, D_floating, G_Floating, H_Floating, IEEE_ S_Floating, IEEE_T_Floating, IEEE_X_Floating, IBM_Long_Floating, IBM_ Short_Floating, or Cray_Floating format. The value of the input_type_code argument determines the format and size of the input_value argument. Input_type_code Format Size in Bytes CVT$K_VAX_F F_Floating 4 CVT$K_VAX_D D_Floating 8 CVT$K_VAX_G G_Floating 8 CVT$K_VAX_H H_Floating 16 CVT$K_IEEE_S IEEE_S_Floating 4 CVT$K_IEEE_T IEEE_T_Floating 8 CVT$K_IEEE_X IEEE_X_Floating 16 CVT$K_IBM_SHORT IBM_Short_Floating 4 CVT$K_IBM_LONG IBM_Long_Floating 8 CVT$K_CRAY Cray_Floating 8 input_type_code OpenVMS usage unsigned integer type longword (unsigned) access read only mechanism by value A longword integer specifying the type of floating point data being passed in the input_value argument. Valid type codes are: CVT$K_VAX_F = 0 CVT$K_VAX_D = 1 CVT$K_VAX_G = 2 CVT$K_VAX_H = 3 CVT$K_IEEE_S = 4 CVT$K_IEEE_T = 5 CVT$K_IEEE_X = 9 CVT$K_IBM_LONG = 6 CVT$K_IBM_SHORT = 7 CVT$K_CRAY = 8 output_value OpenVMS usage varying_arg type unspecified access write only mechanism by reference The output_value argument is the address a of data area that will receive the converted floating point number. The output_ value argument can contain floating point data in F_Floating, D_floating, G_Floating, H_Floating, IEEE_S_Floating, IEEE_ T_Floating, IEEE_X_Floating, IBM_Long_Floating, IBM_Short_Floating, or Cray_Floating format. The value of the output_type_code argument determines the size and format of the data placed into the output_value argument. output_type_code OpenVMS usage unsigned integer type longword (unsigned) access read only mechanism by value A longword integer specifying the type of floating point data that the input_value argument will be converted into and returned in the output_value argument. options OpenVMS usage mask_longword type longword (unsigned) access read only mechanism by value Conversion option specifier. The options argument is a longword bit mask in which each option bit set causes the corresponding option to be used during the conversion. The following options can be specified using the options argument: CVT$M_ROUND_TO_NEAREST bit 0 CVT$M_TRUNCATE bit 1 CVT$M_ROUND_TO_POS bit 2 CVT$M_ROUND_TO_NEG bit 3 CVT$M_VAX_ROUNDING bit 4 CVT$M_BIG_ENDIAN bit 5 CVT$M_ERR_UNDERFLOW bit 6 The input type codes are defined in language-specific system symbol definition libraries (STARLET.MLB, FORSYSDEF.TLB, etc.) in module $CVTDEF. (As of V6.1 on OpenVMS VAX.) Description CVT$CONVERT_FLOAT is a general purpose floating point conversion routine that will convert any one of the floating point data types that are listed below into any of the other floating point data types. The conversion will be subject to the options specified in the OPTIONS mask argument. Condition Values Returned CVT$_NORMAL Normal successful completion. CVT$_INPCONERR Input conversion error. CVT$_INVINPTYP Invalid input type code. CVT$_INVOPT Invalid option argument. CVT$_INVOUTTYP Invalid output type code. CVT$_INVVAL Input value was an invalid number or NaN. CVT$_NEGINF Input value was negative infinity. CVT$_OUTCONERR Output conversion error. CVT$_OVERFLOW Overflow detected during conversion. CVT$_POSINF Input value was positive infinity. CVT$_UNDERFLOW Underflow detected during conversion. CVT$_ statuses are defined in module $CVTMSG. Example /* ** =================================================================== ** ** Example of CVT$CONVERT_FLOAT ** ** -------------------------------------------------------- ----------- ** ** This example program reads IEEE T floating point numbers from an ** input file, converts them to VAX D floating point numbers and ** writes the result to an output file. ** ** The input and output filenames can be specified as the first and ** second arguments on the command line as follows: ** ** $ EXAMPLE IEEE_T_INPUT_FILE.DAT VAX_D_OUTPUT_FILE.DAT ** ** If the input and/or output files are not included on the command ** line then the program will prompt the user for them. ** ** =================================================================== */ #include unsigned long CVT$CONVERT_FLOAT(void *input_value, unsigned long input_type, void *output_value, unsigned long output_type, unsigned long options); globalvalue CVT$K_VAX_D; globalvalue CVT$K_IEEE_T; globalvalue CVT$M_ROUND_TO_NEAREST; globalvalue CVT$_NORMAL; main(int argc, char *argv[]) { double D_Float_number; unsigned long IEEE_Double_number[2]; unsigned long options; char in_filename[80]; char out_filename[80]; FILE *in_file, *out_file; unsigned long ret_status; /* ** Find out where we are going to get the data from. ** First look at the first argument of the command line. ** If nothing is there then attempt to use IEEE_T_IN.DAT. ** ================================================================= */ if (argc == 1) { printf("Enter input data file: [IEEE_T_IN.DAT]: "); if (gets(in_filename) == NULL) exit(1); if (strlen(in_filename) == 0) strcpy(in_filename, "IEEE_T_IN.DAT"); } else strcpy(in_filename, argv[1]); /* ** Find out where we are going to put the output data. ** First look at the second argument of the command line. ** If nothing is there then put it in VAX_D_OUT.DAT ** ================================================================= */ if (argc <= 2) { printf("Enter output data file: [VAX_D_OUT.DAT]: "); if (gets(out_filename) == NULL) exit(1); if (strlen(out_filename) == 0) strcpy(out_filename, "VAX_D_OUT.DAT"); } else strcpy(out_filename, argv[2]); /* ** Open the input and output files. ** -------------------------------------------------------- --------- */ if ((in_file = fopen(in_filename, "r")) == NULL) { fprintf(stderr, "%s couldn't open file %s\n", argv[0], in_filename); exit(1); } out_file = fopen(out_filename, "wb"); options = CVT$M_ROUND_TO_NEAREST; ret_status = CVT$_NORMAL; /* ** Read in each number from the file, convert it, and write it to ** the output file. ** ================================================================= */ while ((fread (&IEEE_Double_number[0], sizeof(IEEE_Double_number), 1, in_file) == 1) && (ret_status == CVT$_NORMAL)) { ret_status = CVT$CONVERT_FLOAT(&IEEE_Double_number[0], CVT$K_IEEE_T, &D_Float_number, CVT$K_VAX_D, options); fwrite(&D_Float_number, sizeof(D_Float_number), 1, out_file); printf("Converted data: %lf.\n", D_Float_number); } fclose(in_file); fclose(out_file); if (ret_status == CVT$_NORMAL) exit(1); else exit(ret_status); } � Here are the Fortran versions of modules $CVTDEF and $CVTMSG: !*** MODULE cvtdef *** PARAMETER CVT$K_VAX_F = '00000000'X ! VAX F Floating point data PARAMETER CVT$K_VAX_D = '00000001'X ! VAX D Floating point data PARAMETER CVT$K_VAX_G = '00000002'X ! VAX G Floating point data PARAMETER CVT$K_VAX_H = '00000003'X ! VAX H Floating point data PARAMETER CVT$K_IEEE_S = '00000004'X ! IEEE S Floating point data PARAMETER CVT$K_IEEE_T = '00000005'X ! IEEE T Floating point data PARAMETER CVT$K_IBM_LONG = '00000006'X ! IBM Long Floating point data PARAMETER CVT$K_IBM_SHORT = '00000007'X ! IBM Short Floating point data PARAMETER CVT$K_CRAY = '00000008'X ! Cray Floating point data PARAMETER CVT$K_IEEE_X = '00000009'X ! IEEE X Floating point data PARAMETER CVT$M_ROUND_TO_NEAREST = '00000001'X PARAMETER CVT$M_TRUNCATE = '00000002'X PARAMETER CVT$M_ROUND_TO_POS = '00000004'X PARAMETER CVT$M_ROUND_TO_NEG = '00000008'X PARAMETER CVT$M_VAX_ROUNDING = '00000010'X PARAMETER CVT$M_BIG_ENDIAN = '00000020'X PARAMETER CVT$M_ERR_UNDERFLOW = '00000040'X STRUCTURE /CVT$R_CONVERSION_OPTIONS/ PARAMETER CVT$S_ROUND_TO_NEAREST = 1 PARAMETER CVT$V_ROUND_TO_NEAREST = 0 PARAMETER CVT$S_TRUNCATE = 1 PARAMETER CVT$V_TRUNCATE = 1 PARAMETER CVT$S_ROUND_TO_POS = 1 PARAMETER CVT$V_ROUND_TO_POS = 2 PARAMETER CVT$S_ROUND_TO_NEG = 1 PARAMETER CVT$V_ROUND_TO_NEG = 3 PARAMETER CVT$S_VAX_ROUNDING = 1 PARAMETER CVT$V_VAX_ROUNDING = 4 PARAMETER CVT$S_BIG_ENDIAN = 1 PARAMETER CVT$V_BIG_ENDIAN = 5 PARAMETER CVT$S_ERR_UNDERFLOW = 1 PARAMETER CVT$V_ERR_UNDERFLOW = 6 BYTE %FILL (4) END STRUCTURE ! CVT$R_CONVERSION_OPTIONS !*** MODULE $CVTMSG *** PARAMETER CVT$_FACILITY = '000005FA'X PARAMETER CVT$_NORMAL = '05FA8009'X PARAMETER CVT$_INPCONERR = '05FA8012'X PARAMETER CVT$_INVINPTYP = '05FA801A'X PARAMETER CVT$_INVOPT = '05FA8022'X PARAMETER CVT$_INVOUTTYP = '05FA802A'X PARAMETER CVT$_INVVAL = '05FA8032'X PARAMETER CVT$_NEGINF = '05FA803A'X PARAMETER CVT$_OUTCONERR = '05FA8042'X PARAMETER CVT$_OVERFLOW = '05FA804A'X PARAMETER CVT$_POSINF = '05FA8052'X PARAMETER CVT$_UNDERFLOW = '05FA805A'X Just for completeness, DEC OSF/1 V2.0 has a similar routine in the math library (libm, I think) as follows: cvt_ftof (void * in, int in_type, void * out, int out_type, int flags) in_type and out_type specify the floating types as one of VAX f,d,g,h formats or ieee single or double. Currently the flags input is ignored. In subsequent releases, this routine may be enhanced to include IBM and CRAY single and double formats and options will be added to deal with source and destination endianness and the behavior on "error" cases. The prototype for cvt_ftof and the definitions of the type parameters can be found in the header file cvt.h. There is also a man page for this function. -- Steve Lionel Mail: lionel@quark.enet.dec.com SDT Languages Group WWW: http://www.digital.com/info/slionel.html Digital Equipment Corporation 110 Spit Brook Road, ZKO2-3/N30 Nashua, NH 03062-2698 "Free advice is worth every cent"