home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / vms / 21944 < prev    next >
Encoding:
Internet Message Format  |  1993-01-25  |  12.9 KB

  1. Path: sparky!uunet!pmafire!news.dell.com!natinst.com!cs.utexas.edu!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!NSCVAX.PRINCETON.EDU!dragon
  2. From: dragon@NSCVAX.PRINCETON.EDU (Mighty Firebreather)
  3. Newsgroups: comp.os.vms
  4. Subject: RE: VAX (F,D,G) Floating Point <=> IEEE Floating Point?
  5. Message-ID: <0096721C.A75DEE00.2750@nscvax.princeton.edu>
  6. Date: 25 Jan 93 14:36:29 GMT
  7. Sender: daemon@ucbvax.BERKELEY.EDU
  8. Distribution: world
  9. Organization: The Internet
  10. Lines: 369
  11.  
  12.  
  13.     James Harvey <harvey@iupui.edu> writes:
  14. >
  15. >Anyone have any routines to convert to/from the VAX (F,D,G) floating point
  16. >formats and IEEE floating point formats?  Or just documentation on the IEEE
  17. >floating point formats - then I could write it myself.  Either FORTRAN or C
  18. >is OK for me.
  19. >
  20.  
  21.     This subject was discussed several years ago and three people 
  22. posted example which I saved.  I've enclosed them following my signature.
  23.  
  24. *************************************************************************
  25. *                                                                       *
  26. *                        Here, there be dragons!                        *
  27. *                      dragon@nscvax.princeton.edu                      *
  28. *                                                                       *
  29. *                                                Richard B. Gilbert     *
  30. *************************************************************************
  31.  
  32. Date: Thu, 28 Jul 88 10:57 EDT
  33. From: Frank Manion <MANION@curie.rm.fccc.edu>
  34. Subject: re: ieee to vax format
  35. To: ghosh@prism.clemson.EDU, INFO-VAX@KL.SRI.COM
  36. X-VMS-To: IN%"ghosh@prism.clemson.edu",IN-INFO-VAX,MANION
  37.  
  38. Please excuse the lateness of this reply, Info-vax seems a week delayed
  39. in reaching me. Is this happening to anyone else???
  40.  
  41. Amitava Ghosh writes:
  42. >does anyone know what the difference is between the IEEE floating
  43. >point representation and the way the VAX stores numbers. If so
  44. >how does one convert from one format to the other.  
  45.  
  46. I had to convert from ieee to vax format recently, and finding the information
  47. in a readily available form was indeed a problem. The source I found
  48. in the end was the MC68881 Floating-Point Coprocessor User's Manual. I
  49. suggest anyone intrested obtain a copy of this manual from the Motorola
  50. corporation.
  51.  
  52. The motorola chip, at least, supports single, double, extended precision,
  53. and packed decimal real string data formats. I have included a routine,
  54. written in fortran, to convert an IEEE single precision floating point
  55. number to the Vax single precision F_floating form. The routine has
  56. extensive comments which should provide some insight into the functioning of
  57. both architectures.
  58.  
  59. For more information on the vax floating point number see the "Data
  60. representation" chapter of the vax architecture handbook.
  61.  
  62. c fjm -- 11/6/87 -- Function IEEE_VAX_S(Value)
  63. c
  64. c This function takes an IEEE format MC68881 Single precision floating
  65. c point number and returns the VAX F_floating form of the number if it
  66. c is in range.
  67. c
  68. c Note: The smallest vax floating point number is 4 times smaller than
  69. c       the smallest ieee floating point number in normallized form.
  70. c    The largest vax floating point number is .5 times as large as
  71. c    the largest ieee floating point number.
  72. c
  73. c    Actual ranges:
  74. c
  75. c        Vax            IEEE
  76. c        ---            ----
  77. c
  78. c    max    1.7014119x10**38    3.4028243x10**38
  79. c    min    0.29387365x10**-38    1.175495x10**-38 (normalized)
  80. c                    1.4013x10**-45   (de-normalized)
  81. c
  82. c Let E = the exponent part of the real (in signed magnitude form), and
  83. c let M = the mantisa, and S = the sign. The vax represents floating point
  84. c numbers as:
  85. c
  86. c    S bit -- bit 15
  87. c    E     -- bits 7-14, 8 bit, excess 128 exponent.
  88. c    M     -- bits 6(MSB)-0 and bits 31-16(LSB) unsigned fraction.
  89. c       Numbers are stored in a normalized form with a hidden 1 bit
  90. c    representing a 0.1 is immediately to the left of the MSB of M. Note
  91. c    that the radix point of the fraction is to the left of the hidden
  92. c    bit in this form.
  93. c
  94. c    IF E .ge. 1 then
  95. c        value = (-1)**S x 2**(E-128) x 0.1M
  96. c    IF S .eq. 0 .and. E .eq. 0 then
  97. c        value = 0 (unsigned)
  98. c    IF S .eq. 1 .and. E .eq. 0 then
  99. c        illegal value, signalled on attempted operation.
  100. c
  101. c The IEEE representation is as follows:
  102. c
  103. c    S bit -- bit 31
  104. c    E     -- bits 23-30, 8 bit, excess 127 binary exponent.
  105. c    M     -- bits 22(MSB) - 0(LSB) unsigned fraction.
  106. c    Numbers are stored in either normalized or de-normalized form. In
  107. c       normalized form (the usual representation) a hidden 1 bit representing
  108. c       a 1.0 is immediatly to the left of the MSB of M. In denormalized form,
  109. c       the hidden bit is 0. See below. Note that in IEEE form, the radix point
  110. c    of the fraction is to the right of the hidden bit.
  111. c
  112. c    IF E .ge. 1 .and. E .le. 254 then
  113. c        actual value = (-1)**S x 2**(E-127) x 1.M
  114. c    IF E .eq. 0 .and. M .eq. 0 then
  115. c        value = signed zero (S bit is preserved)
  116. c    IF E .eq. 255 .and. M .ne. 0 then
  117. c        not a number (NAN). M can have any bit pattern, and the
  118. c        result of any operation on a NAN with the MSB of F set
  119. c        on an IEEE implementation results in a signal. The
  120. c        interpretation on any value associated with a NAN is left
  121. c        to the application.
  122. c    IF E .eq. 255 .and. M .eq. 0 then
  123. c        value = Plus or minus infinity, depending on S.
  124. c    IF E .eq. 0 .and. M .ne. 0 then
  125. c        denormalized number, used to represent numbers less 
  126. c        than can be represented in normalized form. The hidden bit
  127. c        is assumed to a zero.
  128. c        value = (-1)**S x 2**(E-126) x 0.M
  129. c
  130. c Note that the vax and ieee forms differ by a bias of 1 in their stored
  131. c exponent form, and that they also differ implicitly by a power of 2 due to
  132. c the difference in normalization scalling (ieee = 1.M, vax = 0.1M).
  133. c Thus the conversion for the exponent is Evax = Eieee + 2. Further, the vax
  134. c can represent smaller numbers in normallized form than can the 68881, so
  135. c this routine converts the de-normalized numbers that are representable
  136. c on the vax. Thus, all ieee normalized numbers with exponents less than 254
  137. c are convertable to vax representations. Further, Some of the de-normalized
  138. c numbers are convertable. Loss of range (overflow) occurs on the high end.
  139. c The actual conversion outline follows:
  140. c
  141. c Normallized numbers:
  142. c
  143. c    numbers in the magnitude of:
  144. c        1.175495 x 10**-38 to 1.7014119 x 10**38
  145. c    are converted exactly, with no loss of precision.
  146. c
  147. c    numbers with a magnitude greater than 1.7014119 x 10**38
  148. c       are converted to a vax reserved operand with sign of 1 and
  149. c    exponent and mantissa of zero. This is the equivalent of
  150. c       the result of a floating overflow on a 780 class machine.
  151. c
  152. c De-normalized numbers:
  153. c
  154. c    numbers in the magnitude of:
  155. c        0.29387365 x 10**-38 to 1.175494 x 10**-38
  156. c    are converted. Note that while the conversion is exact,
  157. c       the vax has 1 to 2 bits more precision in this range than is
  158. c    represenatble in the ieee form. The extra 1 or 2 bits are assigned
  159. c       zeros in the conversion.
  160. c
  161. c    numbers less than 0.29387365 x10**-38 are mapped on to vax zero
  162. c    with S=0, E=0, and M=0.
  163. c
  164. c Zeros:
  165. c
  166. c    both positive and minus zero in the ieee implementation are
  167. c    mapped on to vax zero with S=0, M=0, and E=0.
  168. c
  169. c Infinities:
  170. c
  171. c    Infinities have no dual in the vax architecture, as such, the
  172. c    value mapped is a vax reserved operand, with S=1, E=0, and M=0.
  173. c    This is the equivalent of a floating overflow result on the
  174. c    780 class machines.
  175. c
  176. c Not Numbers (NAN's):
  177. c
  178. c    These are mapped onto vax operands by setting the sign bit,
  179. c    clearing the exponent, and preserving the mantissa. Note that
  180. c    this conversion losses the original sign bit. The ieee representation
  181. c       has twice as many NAN's as the vax, so half are lost.
  182. c
  183.  
  184.     integer*4 function IEEE_VAX_S(value) ! really of type bit vector.
  185.  
  186.     implicit    none
  187.  
  188.     integer*4     value
  189.  
  190.     integer*4     S
  191.     integer*4     E
  192.     integer*4     M
  193.  
  194.     integer*4     lib$extzv
  195.  
  196. c Fetch the fields from IEEE format:
  197. c
  198. c    31 30          23 22                           0
  199. c      +--+--------------+------------------------------+
  200. c      | S| Exponent     |MSB   Mantissa             LSB|
  201. c      +--+--------------+------------------------------+
  202. c
  203.     S = lib$extzv(31,1,value)
  204.     E = lib$extzv(23,8,value)
  205.     M = lib$extzv(0,23,value)
  206.  
  207. c Note: The following code is optimized to deal with the two most likely
  208. c       casses first: namely, normal values and zeros.
  209. c
  210.  
  211. c Zeros and de-normalized numbers:
  212.  
  213.     if ( E .eq. 0 ) then
  214.  
  215. c Zeros:
  216.       if( M .eq. 0 ) then
  217.         S = 0
  218.  
  219. c de-normalized number:
  220.  
  221.       else ! M .ne. 0
  222.  
  223. c          number is representable if  bit 21 or 22 is set.
  224. c          shift the mantisa the right number of bits and strip the
  225. c          high order set bit which becomes the hidden bit on the vax.
  226. c          Numbers outside the representable range are set to zero.
  227.  
  228.         if ( ( M .and. '400000'x) .ne. 0 ) then
  229.           M = (M .or. '3FFFFF'x) * 2
  230.           E = 2
  231.         else if ( ( M .and. '200000'x) .ne. 0 ) then
  232.           M = (M .or. '1FFFFF'x) * 4
  233.           E = 1
  234.         else
  235.           S = 0
  236.           E = 0
  237.           M = 0
  238.           end if
  239.         end if
  240.  
  241. c Normal value:
  242.  
  243.     else if ( E .lt. 254 ) then
  244.       E = E + 2
  245.  
  246.  
  247. c un-representable (overflow)
  248.  
  249.     else if ( E .eq. 254 ) then
  250.       S = 1
  251.       E = 0
  252.       M = 0
  253.  
  254. c Infinities and NAN's
  255.  
  256.     else ! E .eq. 255
  257.       S = 1
  258.       E = 0
  259.  
  260.       end if
  261.  
  262. c Store the numbers in vax format.
  263. c
  264. c    31                   16 15 14       7 6           0
  265. c      +-----------------------+--+----------+-------------+
  266. c      |    Mantissa        LSB| S| Exponent |MSB Mantissa |
  267. c      +-----------------------+--+----------+-------------+
  268. c
  269.     call lib$insv(S,15,1,ieee_vax_s)
  270.     call lib$insv(E, 7,8,ieee_vax_s)
  271.     call lib$insv(lib$extzv(16,7,M),0,7,ieee_vax_s)
  272.     call lib$insv(lib$extzv(0,16,M),16,16,ieee_vax_s)
  273.  
  274.     return
  275.     end
  276. :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
  277. Reply to:
  278.   MANION@CURIE.RM.FCCC.EDU              Frank J. Manion
  279.                                         The Fox Chase Cancer Center
  280.   Phone: (215) 728-3660                 7701 Burholme Avenue
  281.                                         Philadelphia, PA  19111
  282.                                         USA
  283. :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
  284.  
  285. Date: 22 Jul 88 22:37:55 GMT
  286. From: acdpyr!pack@handies.ucar.edu  (Dan Packman)
  287. Organization: Atmospheric Chemistry Division/NCAR, Boulder CO
  288. Subject: Re: difference between vax floating point representation and IEEE
  289. Message-Id: <471@ncar.ucar.edu>
  290. References: <2256@hubcap.UUCP>
  291. Sender: info-vax-request@kl.sri.com
  292. To: info-vax@kl.sri.com
  293.  
  294. In article <2256@hubcap.UUCP> ghosh@hubcap.UUCP (Amitava Ghosh) writes:
  295. >does anyone know what the difference is between the IEEE floating
  296. >point representation and the way the VAX stores numbers. If so
  297. >how does one convert from one format to the other...
  298. The following program converts from vax f format to IEEE (UNIX point of view)
  299. --------cut here-------------
  300.       subroutine vax2lcl(n,iarray)
  301.       dimension iarray(n)
  302.       character*1 conv(4),ccc
  303.       integer iconv
  304.       equivalence (iconv,conv)
  305. c Convert from vax floating point f format to pyramid IEEE single
  306.       do 10 i=1,n
  307.         iconv=iarray(i)
  308.         if (iconv.ne.0) then
  309.          ccc=conv(1)
  310.          iii=ichar(conv(2))-1
  311.          if (iii.lt.0)then
  312.            write(0,100)iconv
  313.  100       format(' vax2lcl: overflow int in =',i10)
  314.            call exit(1)
  315.          endif
  316.          conv(1)=char(iii)
  317.          conv(2)=ccc
  318.          ccc=conv(3)
  319.          conv(3)=conv(4)
  320.          conv(4)=ccc
  321.          iarray(i)=iconv
  322.         endif
  323.   10  continue
  324.       return
  325.       end
  326. --------cut here-------------
  327. The following converts an array of IEEE single floats to vax floats
  328. It is written to work on a VAX/VMS machine
  329. --------cut here-------------
  330.       subroutine pyr2lcl(n,iarray)
  331.       dimension iarray(n)
  332.       byte conv(4),ccc
  333.       integer iconv
  334.       equivalence (iconv,conv)
  335. c Convert from pyramid IEEE single to vax floating point f format
  336.       do 10 i=1,n
  337.         iconv=iarray(i)
  338.         if (iconv.ne.0) then
  339.          ccc=conv(2)
  340.          iii=conv(1)+1
  341.          if (iii.gt.255)then
  342.            write(0,100)iconv
  343.  100       format(' pyr2lcl: overflow int in =',i10)
  344.            call exit(1)
  345.          endif
  346.          conv(2)=iii
  347.          conv(1)=ccc
  348.          ccc=conv(3)
  349.          conv(3)=conv(4)
  350.          conv(4)=ccc
  351.          iarray(i)=iconv
  352.         endif
  353.   10  continue
  354.       return
  355.       end
  356.  
  357. Date: 2 Aug 88 15:07:00 CST
  358. From: "MARTIN J. MOORE" <mooremj@eglin-vax.arpa>
  359. Subject: Conversion between VAX and IEEE floating point
  360. To: "info-vax" <info-vax@kl.sri.com>
  361. cc: mooremj     
  362. Reply-To: "MARTIN J. MOORE" <mooremj@eglin-vax.arpa>
  363.  
  364. Someone recently asked about converting VAX floating-point numbers to IEEE 
  365. standard floating-point numbers.  I have run across a mildly bizarre hack for 
  366. performing this conversion when (1) the numbers are single precision (32-bit) 
  367. in both cases; and (2) the destination machine requires 32-bit quantities
  368. to be specified high-word, low-word (opposite to the VAX, which is low-word,
  369. high-word.)  For example, 68000-based machines.
  370.  
  371. The formulas are simply:
  372.  
  373.     IEEE number = VAX number * 0.25 
  374.     VAX number = IEEE number * 4.0
  375.  
  376. with the caveat that the multiplication is performed *on the VAX*.
  377.  
  378.                 Marty Moore
  379. ------
  380.  
  381.