[SciPy-user] IBM float point format...

Robert Kern robert.kern@gmail....
Wed Jan 14 15:30:41 CST 2009


On Wed, Jan 14, 2009 at 08:32, fred <fredmfp@gmail.com> wrote:
> Hi all,
>
> Is there something ready-to-use in numpy/scipy to read data stored in
> IBM floating point format from file?
>
> I usually use scipy.io.numpyio.{fread, fwrite} to read my files, but for
> this peculiar case, I don't know how I can handle it...

Not in numpy or scipy. I do have a ufunc in my own code for this. It's
also fairly straightforward to implement in pure numpy if you can bear
the memory cost of the temporaries (and of course, you can chunk the
input to reduce that cost).

Here is a Python implementation:

    def ibm2ieee(ibm):
        """ Converts an IBM floating point number into IEEE format. """

        sign = ibm >> 31 & 0x01

        exponent = ibm >> 24 & 0x7f

        mantissa = ibm & 0x00ffffff
        mantissa = (mantissa * 1.0) / pow(2, 24)

        ieee = (1 - 2 * sign) * mantissa * pow(16.0, exponent - 64)

        return ieee


Here is the ufunc:

#define IBM_MANTISSA_UNIT (16777216.0)

static void ibm2ieee_loop(char **args, npy_intp *dimensions, npy_intp *steps,
    void *data)
{
    char *ibmbuf = args[0];
    char *output = args[1];

    npy_intp i, n=dimensions[0];
    npy_int32 ibm_val, exponent, mantissa;
    npy_float32 ieee_val;
    short sign;

    for (i=0; i < n; i++, ibmbuf+=steps[0], output+=steps[1]) {
        ibm_val = *(npy_int32*)ibmbuf;
        sign = (ibm_val >> 31) & 0x01;
        exponent = ((ibm_val >> 24) & 0x7F) - 64;
        mantissa = ibm_val & 0x00FFFFFF;
        ieee_val = (1-2*sign) * (mantissa / IBM_MANTISSA_UNIT) *
powf(16.0, exponent);
        *(npy_float32*)output = ieee_val;
    }
}

static char ibm2ieee_sigs[] = {
    NPY_INT32, NPY_FLOAT32
};
static char ibm2ieee_doc[] = "Convert an IBM floating point number
(viewed as a 32-bit integer) to an IEEE-754 float32.";
static PyUFuncGenericFunction ibm2ieee_functions[] = {ibm2ieee_loop};
static void* ibm2ieee_data[] = {NULL};


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list