From: jrlatala@shell.golden.net (john R. Latala)
Newsgroups: comp.sys.hp48
Subject: conversion to/from IEEE single precision floating point numbers
Date: 7 Nov 1998 17:15:03 GMT
Organization: Golden Triangle Online
Lines: 135
Message-ID: <721v6o$lfo$1@cougar.golden.net>
NNTP-Posting-Host: 199.166.210.4
X-Newsreader: TIN [version 1.2 PL2]
Xref: republic.btigate.com comp.sys.hp48:18316

I've been helping a friend decode a file format used by an old program.
The program stored all of it's numbers in IEEE single precision format.
Needless to say this involved looking over a lot of hex dumps and other such
wonderful things! Converting back and forth gets old REAL fast to say the
least. 

To help things along I came up with the folloing two programs for converting
HP48 reals to/from IEEE single precision format (32-bit).

%%HP: T(3)A(D)F(.);
@
@ convert an HP48 real into a 32-bit hex number representing the IEEE 
@ 32-bit single precision fomrat
@
@ n -> # nh
@
\<< 
  \-> n
  \<<
    @ make sure we've got a real
    IF n TYPE 0 \=/ THEN
      "Need REAL number" DOERR
    ELSE
      @ if it's too big give then +infinity
      IF n 3.40282346637E38 > THEN
        # 7F800000h
      ELSE
        @ if it's too small give them -infinity
        IF n -3.40282346637E38 < THEN
          # FF800000h
        ELSE
          @ is it a zero?
          IF n 0 == THEN 
            # 0h
          ELSE 
            @ get the sign
            n 0 \>= 0 1 IFTE 
            0 
            \-> s e
            \<< 
              @ remove the sign
              n ABS 'n' STO 
              @ get the power of two exponent
              n LOG 2 LOG / FLOOR 'e' STO 
              @ scale the mantissa
              n 2 e ^ / 'n' STO 
              @ convert the mantissa into binary
              n 1 - 2 23 ^ * R\->B 
              @ convert the exponent into binary
              e 127 + 2 23 ^ * R\->B 
              @ convert the sign into binary
              s 2 31 ^ * R\->B 
              @ combine the mantissa, exponent and sign together
              + +
            \>>
          END
        END
      END
    END
  \>>
\>>

%%HP: T(3)A(D)F(.);
@
@ convert a 32-bit hex representation of a IEEE single precision number
@ back into a HP48 real
@
@ # nh -> n
@
\<< 
  \-> n
  \<<
    @ make sure we've got a binary number
    IF n TYPE # 0h TYPE \=/ THEN
      "Need BINARY number" DOERR
    ELSE
      @ shouldn't be any bits set above the bottom 32
      IF n # FFFFFFFFh NOT AND # 0h \=/ THEN
        ">32-bits" DOERR
      ELSE 
        @ get the mantissa
        n # 7FFFFFh AND 
        @ get the exponent
        n # 800000h / # FFh AND n 
        @ get the sign
        # 80000000h / 
        \-> f e s
        \<<
          @ is it a NaN?
          IF e # FFh == f # 0h \=/ AND THEN
            "NaN"
          ELSE
            @ is it +infinity or -infinity?
            IF e # FFh == f # 0h == AND THEN 
              @ figure out which one it is
              { "+\oo" "-\oo" } s B\->R 1 + GET
            ELSE
              @ is it a normalized number?
              IF e # 0h > e # FFh < AND THEN
                @ convert the sign
                -1 s B\->R ^ 
                @ convert the exponent
                2 e B\->R 127 - ^ 
                @ convert the mantissa. the 1 + at the end takes into 
                @ account the 'hidden' bit
                f B\->R 2 23 ^ / 1 + 
                * *
              ELSE
                @ is it an unormalized number?
                IF e # 0h == f # 0h \=/ AND THEN 
                  @ convert the sign
                  -1 s B\->R ^ 
                  @ convert the exponent
                  2 e B\->R 126 - ^ 
                  @ convert the mantissa with no hidden bit because this is
                  @ an unormalized number
                  f 2 23 ^ / 
                  * *
                ELSE 
                  @ it must be a zero
                  0
                END
              END
            END
          END
        \>>
      END
    END
  \>>
\>>


--
john R. Latala
jrlatala@golden.net