Floating point VAX <---> IEEE 
Author Message
 Floating point VAX <---> IEEE

      Hello World,

      We are looking for a well documented public domain F77 (or "C")
      code for converting VAX 32-bit floating-point format to IEEE
      32-bit floating-point format and vice-versa.   (We want to
      be able to process our FP data in a mixed VAX/SUN net).
      Since in F77 bit manipulations are not standard (but will be
      - I believe - in F90), this stuff is tricky.  Any flavor F77
      (or C) is welcome.  I will post the answers I receive if appropriate.

      Thanks,

      Marin van Heel

      Fritz Haber Institute of the Max Planck Society  
      Faradayweg 4-6, W-1000 Berlin 33, Fed.Rep.Germany
      Tel: 49-30-8305380     fax: 49-30-8305520        




Mon, 18 Jul 1994 15:43:50 GMT  
 Floating point VAX <---> IEEE

   Path: milton!uw-beaver!micro-heart-of-gold.mit.edu!xn.ll.mit.edu!ames!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!math.fu-berlin.de!fub!geminix.in-berlin.de!tertius.in-berlin.de!van_heel

   Newsgroups: comp.lang.fortran
   Date: 30 Jan 92 07:43:50 GMT
   Organization: Rechenzentrum MPG Berlin
   Lines: 25

         Hello World,

         We are looking for a well documented public domain F77 (or "C")
         code for converting VAX 32-bit floating-point format to IEEE
         32-bit floating-point format and vice-versa.   (We want to
         be able to process our FP data in a mixed VAX/SUN net).
         Since in F77 bit manipulations are not standard (but will be
         - I believe - in F90), this stuff is tricky.  Any flavor F77
         (or C) is welcome.  I will post the answers I receive if appropriate.

         Thanks,

         Marin van Heel

         Fritz Haber Institute of the Max Planck Society  
         Faradayweg 4-6, W-1000 Berlin 33, Fed.Rep.Germany
         Tel: 49-30-8305380     fax: 49-30-8305520        


===============================================================================

It's actually very simple.  The following is based on an article by Loren Buhle
in the VAX Professional, Aug. '91 and modified by a collegue here at PMEL.

But can anyone supply similar routines to handle
VAX-G <--> IEEE long (double precision) ?

steve
--------------------

*   T.FOR:         Test program.
*   FP_TEST.FOR:           Subroutine to test machine type.    
*   FP_CONV.FOR:           Subroutine to convert an array of floating point numbers.

C:      T.FOR

        LOGICAL*1 BB_IN(4),BB_OUT(4)
        EQUIVALENCE (BB_IN(1),X_IN)
        EQUIVALENCE (BB_OUT(1),X_OUT)

           CALL FP_TEST(ID)

           WRITE (6,11)  ID
   11      FORMAT (' ID returned: ',I5)

           IF (ID .EQ. 0)  THEN
              WRITE (6,21)
   21         FORMAT(/' This system determined to be a VAX'/)
           ELSE IF (ID .EQ. 1)  THEN
              WRITE (6,22)
   22         FORMAT(/' This system determined to be RISC/Ultrix'/)
           ELSE IF (ID .EQ. 2)  THEN
              WRITE (6,23)
   23         FORMAT(/' This system determined to be a SUN'/)
           END IF

  100      WRITE (6,101)
  101      FORMAT (' Convert to:'/,
        1          '    (0) = VAX',
        1          '    (1) = Risc/Ultrix,',
        1          '    (2) = SUN: '$)
           READ (5,102)  ID_OUT
  102      FORMAT (I10)

           IF (ID_OUT .LT. 0 .OR. ID_OUT .GT. 2)  GO TO 100

           WRITE (6,201)
  201      FORMAT (/' Enter (0) for byte input',
        1           '  (1) for FP input: '$)
           READ (5,202)  IN_FMT
  202      FORMAT (I10)

           IF (IN_FMT .EQ. 0)  THEN

  300         WRITE (6,301)
  301         FORMAT (' 4 Bytes to convert: '$)
              READ (5,302)  BB_IN
  302         FORMAT (4I6)
              WRITE (6,311)  X_IN,BB_IN
  311         FORMAT (' Number to convert: ',G15.5,I10,3I4)

              X_OUT = X_IN
              CALL FP_CONV (ID,ID_OUT,X_OUT,1)
              WRITE (6,321)  X_OUT,BB_OUT
  321         FORMAT (' Converted to: ',G15.5,I10,3I4)

              GO TO 300

           END IF

  500      WRITE (6,501)
  501      FORMAT (' Floating point to convert: '$)
           READ (5,502)  X_IN
  502      FORMAT (G15.0)
           WRITE (6,511)  X_IN,BB_IN
  511      FORMAT (' Number to convert: ',G15.5,I10,3I4)
           X_OUT = X_IN
           CALL FP_CONV (ID,ID_OUT,X_OUT,1)
           WRITE (6,521)  X_OUT,BB_OUT
  521      FORMAT (' Converted to:      ',G15.5,I10,3I4)

           GO TO 500

        END
C:      FP_TEST.FOR
C:
C:      This subroutine tests the byte order of a floating point number
C:      on a computer.
C:
C:      If ID = -1 this is an unknown machine.  This is an error condition.
C:         ID =  0 this is a VAX machine.
C:         ID =  1 this is IEEE fp machine (Ultrix/RISC, Intel 80x86)
C:         ID =  2 this is a Stardent or Sun
C:

        SUBROUTINE FP_TEST (ID)

        REAL AA
        LOGICAL*1 BB(4)
        EQUIVALENCE (AA,BB(1))

        DATA BB/-6,71,-102,-10/

           ID = -1                              ! DEFAULT is unknown.

           IF (AA .EQ. 32123.3) THEN
              ID = 0                            ! machine is a VAX
           ELSEIF (AA .EQ. -1.5645968E33) THEN
              ID = 1                            ! Ultrix/RISC or Intel 80x86
           ELSEIF (AA .EQ. -2.5910251E35) THEN
              ID = 2                            ! SUN
           END IF

        RETURN
        END
C:      FP_CONV.FOR
C:
C:      This subroutine converts the FLOATING POINT format from
C:      TYP_IN to TYP_OUT.
C:
C:      The machine numbering scheme is contained in FP_TEST.
C:

        SUBROUTINE FP_CONV (TYP_IN,TYP_OUT,BUFFER,NCNT)

        INTEGER*4 TYP_IN,TYP_OUT
        REAL*4 BUFFER(NCNT),DUM
        LOGICAL*1 BB(4),BDUM
        EQUIVALENCE (BB(1),DUM)

           IF (TYP_IN .EQ. TYP_OUT)  RETURN

           IF (TYP_IN .EQ. 0 .AND. TYP_OUT .EQ. 1)  THEN        ! VAX -> RISC

              DO K = 1, NCNT
                 DUM = BUFFER(K)
                 BDUM = BB(1)
                 BB(1) = BB(3)
                 BB(3) = BDUM
                 BDUM = BB(2)
                 BB(2) = BB(4)
                 BB(4) = BDUM-1
                 BUFFER(K) = DUM
             END DO

             RETURN

           END IF

           IF (TYP_IN .EQ. 1 .AND. TYP_OUT .EQ. 0)  THEN        ! RISC -> VAX

              DO K = 1, NCNT
                 DUM = BUFFER(K)
                 BDUM = BB(1)
                 BB(1) = BB(3)
                 BB(3) = BDUM
                 BDUM = BB(4)
                 BB(4) = BB(2)
                 BB(2) = BDUM+1
                 BUFFER(K) = DUM
              END DO

              RETURN

           END IF

           IF (TYP_IN .EQ. 0 .AND. TYP_OUT .EQ. 2)  THEN        ! VAX -> SUN

              DO K = 1, NCNT
                 DUM = BUFFER(K)
                 BDUM = BB(1)
                 BB(1) = BB(2)-1
                 BB(2) = BDUM
                 BDUM = BB(4)
                 BB(4) = BB(3)
                 BB(3) = BDUM
                 BUFFER(K) = DUM
              END DO

              RETURN

           END IF

           IF (TYP_IN .EQ. 2 .AND. TYP_OUT .EQ. 0)  THEN        ! SUN -> VAX

              DO K = 1, NCNT
                 DUM = BUFFER(K)
                 BDUM = BB(2)
                 BB(2) = BB(1)+1
                 BB(1) = BDUM
                 BDUM = BB(4)
                 BB(4) = BB(3)
                 BB(3) = BDUM
                 BUFFER(K) = DUM
              END DO

              RETURN

           END IF

           DO K = 1, NCNT                               ! SUN <-> RISC/Ultrix
              DUM = BUFFER(K)
              BDUM = BB(1)
              BB(1) = BB(4)
              BB(4) = BDUM
              BDUM = BB(2)
              BB(2) = BB(3)
              BB(3) = BDUM
              BUFFER(K) = DUM
           END DO

        RETURN
        END
--
                |  NOAA/PMEL               |   ph. (206) 526-6080  
Steve Hankin    |  7600 Sand Point Way NE  |   FAX (206) 526-6744



Tue, 02 Aug 1994 01:37:08 GMT  
 Floating point VAX <---> IEEE

Quote:
>     be able to process our FP data in a mixed VAX/SUN net).

For this special case, (viz. Sun + other) there is a library call
available in libm, that may be of some interest.

NAME
     convert_external - convert external binary data formats

SYNOPSIS
     #include <floatingpoint.h>
     enum convert_external_arch_t = {
          convert_external_sparc, convert_external_pc,
          convert_external_vax, convert_external_vaxg,
          convert_external_s370, convert_external_cray };

     enum convert_external_type_t = {
          convert_external_signed,
          convert_external_unsigned,
          convert_external_float };

     enum convert_external_rounding_t = {
          convert_external_common = -1,
          convert_external_biased = -2,
          convert_external_away = -3 };

     typedef struct {
          enum convert_external_arch_t arch ;     /* format architecture */
          enum convert_external_type_t type ;     /* format type */
          int size ;                    /* format size in 8-bit bytes */
          } convert_external_t ;

     fp_exception_field_type
     convert_external(src_p, src_format, dst_p, dst_format, rounding, n)
     char *src_p, *dst_p;
     convert_external_t src_format, dst_format;
     int rounding;
     int n;

     fp_exception_field_type
     convert_external_(src_p, src_format_p, dst_p, dst_format_p, rounding_p, n_p)
     char *src_p, *dst_p;
     convert_external_t *src_format_p, *dst_format_p;
     int *rounding_p;
     int *n_p;

DESCRIPTION
     convert_external() is used in C programs to convert  between
     binary data on non-SPARC systems and SPARC binary data.  The
     data  may  be  signed  integers,   unsigned   integers,   or
     floating-point data.  convert_external_() is used in Fortran
     programs (CALL CONVERT_EXTERNAL(...)) for the same  purpose,
     with all parameters call-by-reference.

Sun Microsystems    Last change: 26 July 1990                   1

     The convert_external_t type describes the supported formats;
     for each architecture, signed and unsigned integers of sizes
     1, 2, 4, 8, or 16 bytes, and floating-point types  of  sizes
     4, 8, 12, or 16 bytes in size are supported.  If an improper
     size is specified for the source or destination, no  conver-
     sion  occurs  and (1<<fp_division) is returned, an exception
     that can't arise otherwise.

     Supported architectures and formats include

     center  ;  l  c  c   c   c   c   .    external_t     endian-
        float*4   float*8   float*12  float*16
     architecture   ness format    format    format    format

     sparc     big  IEEE single    IEEE  double    MC68881   IEEE
     quad                           extended  pc   little    IEEE
     single    IEEE         double    i80x87    IEEE         quad
                              extended         vax  little    VAX
     F     VAX  D     none VAX  H  vaxg little    VAX   F     VAX
     G     none VAX      H     s370 big  S/370     single   S/370
     double   none S/370                                 extended
     cray big  none Cray-1 single  none Cray-1 double

     Note that MC68000 and SPARC formats are identical big-endian
     formats;  the  Intel  PC formats for 8086/8087, 80x86/7, and
     80486 are identical little-endian.  Table  entries  ``none''
     represent  improper  sizes;  no  conversion  occurs.  ``IEEE
     quad'' refers to the natural big- or little-endian extension
     of  IEEE  single  and  double  precision to a format with 15
     exponent bits, one implicit significand bit, and 112  expli-
     cit significand bits.

USAGE
     excep = convert_external(src_p, src_format,
               dst_p, dst_format, rounding, n);

          excep
               The function value is an accumulation of  all  the
               exceptions  encountered during the conversion.  If
               detecting individual exceptions  is  important,  n
               should  be  1  so  that only one data item is con-
               verted at a time.

          src_p
               A pointer to the data to be converted stored as  a
               contiguous  array.  The pointer is declared char *
               to emphasize that there are no alignment  require-
               ments, although data being converted to SPARC for-
               mat for subsequent processing by the same  program
               should be appropriately aligned.

          dst_p

Sun Microsystems    Last change: 26 July 1990                   2

               A pointer to the converted data stored as  a  con-
               tiguous array.

          src_format
               Description of the format of the source data.

          dst_format
               Description of the format of the destination data.

          rounding
               The rounding mode  to  apply  to  the  destination
               data.

          n    Number of data items to be converted.

     Rounding choices; the first is intended to  be  satisfactory
     for almost all applications:

     (int) convert_external_common
          integer and unsigned destination formats  round  toward
          zero (2.9 rounds to 2, -2.9 rounds to -2),
          IEEE  floating-point  destinations  round  to   nearest
          unbiased,
          VAX  floating-point  destinations  round   to   nearest
          biased,
          IBM 370  and  Cray  floating-point  destinations  round
          toward zero.

     (int) convert_external_biased
          to round to nearest biased (for  integer  destinations,
          1.5 rounds to 2, 2.5 rounds to 3).

     (int) convert_external_away
          to round away from zero (for integer destinations,  2.1
          rounds to 3, -2.1 rounds to -3).

     (int) fp_nearest, fp_tozero, fp_positive, fp_negative
          to obtain rounding in one of  the  modes  specified  in
          IEEE 754.

     After the conversion, if (excep & (1 << fp_invalid))  !=  0,
     then  one or more invalid exceptions occurred; similarly for
     the other exceptions, which are defined this way:

     fp_invalid
          An input argument was an IEEE signaling NaN  or  a  VAX
          reserved  operand, or an input argument was an infinity
          and the destination was an integer or unsigned type, or
          a floating-point type with no infinity representation.

     fp_overflow
          An input argument was finite and large but could not be

Sun Microsystems    Last change: 26 July 1990                   3

          represented  in the destination format with only normal
          rounding error; if the destination format is integer or
          unsigned then fp_overflow represents integer overflow.

     fp_underflow
          An input argument was finite and small and could not be
          represented  in the destination format with only normal
          rounding error.  This can  only  arise  with  floating-
          point destinations.

     fp_inexact
          An  input  argument  was  finite  but  could   not   be
          represented exactly in the destination format.

     fp_division
          The source or destination formats aren't supported.  No
          conversions are done.

     Note that converting external data to SPARC format, process-
     ing on a SPARC, then converting back to external format will
     almost never yield the same results as processing  the  data
     entirely on the external system, even if the conversions and
     processing yield no exceptions on the SPARC.

EXAMPLES
  Generating data on an external system
     The following code demonstrates typical  Fortran  generation
     of binary floating-point data on an external system:
                  REAL A(100)
                  WRITE(9) A

  Converting external data to SPARC internal form in C
     Suppose the previous binary data file was created on a  tape
     on  a  Cray  and read on a SPARC system.  To convert to IEEE
     double-precision floating-point  format,  but  using  biased
     rounding,
          #include <floatingpoint.h>

          char datasrc[800];
          double datadest[100];
          fp_exception_field_type excep;
          int rounding;
          int i;
          convert_external_t src, dest;

          /*    read the Cray data into the array datasrc somehow, then ...  */

          src.arch = convert_external_cray;
          src.type = convert_external_float;
          src.size = 8;
          dest.arch = convert_external_sparc;
          dest.type = convert_external_float;

Sun Microsystems    Last change: 26 July 1990                   4

          dest.size = 8;
          rounding = convert_external_biased;

          excep = convert_external((char *) datasrc, src,
                    (char *) datadest, dest, rounding, 100);

          /*
           * usually you aren't interested in inexact exceptions
           * and you'd do this
           */
          excep &= ~(1 << fp_inexact);

          /*
           * other exceptions are possible in this example - after the fact,
           * you can find out where they happened this way
           */
          if (excep != 0) for (i = 0 ; i < 100 ; i++) {
              excep = convert_external((char *)&datasrc[8 * i], src,
                    (char *)&datadest[i], dest, rounding, 1);
              if (excep != 0) {
                  /* do something specific about datadest[i] */
              }
          }

  Converting SPARC internal data to external form in Fortran
     Suppose data created in a SPARC program is to be  read  into
     an IBM PC.  To convert,
          #include <f77_floatingpoint.h>

                  REAL*4 datasrc(100)
                  REAL*4 datadest(100)
                  INTEGER excep, convert_external, round
                  INTEGER src(4), dest(4)

          c        create datasrc array somehow, then ...

                  src(1) = convert_external_sparc
                  src(2) = convert_external_float
...

read more »



Tue, 02 Aug 1994 03:46:39 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Floating point VAX <---> IEEE (me, too!)

2. MSBIN float format <-> IEEE float format

3. IBM 370 Floating point to IEEE floating point

4. ><><><><>Heeeeeeeeeeeeeeelp on INT 14!><><><><><>

5. VAX float to IEEE float converter

6. vax floating pont to unix floating point

7. <<<<<YOU MUST CHECK THIS OUT >>>>>>>>>> 2103

8. Convert VAX floating pt to IEEE?

9. <><><> FLOODFILL <><><>

10. >>>HELP, DECOMPILER<<<

11. <<<XXX Password>>>

12. >>>>>>>>>>>>>>>>>>>HEY!<<<<<<<<<<<<<<<<<<<<<<<

 

 
Powered by phpBB® Forum Software