IEEE to MSBIN converter

IEEE to MSBIN converter

Postby Enrico Maria Giordano » Sat Jan 17, 2015 7:22 pm

Dear friends, I need to convert from IEEE to MSBIN numeric format. Currently I'm using the function below (found on Internet) but I just discovered that it fails with -0.328 and other similar values. Any suggestions?

Code: Select all  Expand view
int _fieeetomsbin(float *src4, float *dest4)
{
    unsigned char *ieee = (unsigned char *)src4;
    unsigned char *msbin = (unsigned char *)dest4;
    unsigned char sign;
    unsigned char msbin_exp = 0x00;
    int i;

    /* See _fmsbintoieee() for details of formats   */
    sign = ( unsigned char ) ( ieee[3] & 0x80 );
    msbin_exp |= ( unsigned char ) ( ieee[3] << 1 );
    msbin_exp |= ( unsigned char ) ( ieee[2] >> 7 );

    /* An ieee exponent of 0xfe overflows in MBF    */
    if (msbin_exp == 0xfe) return 1;

    msbin_exp += ( unsigned char ) 2;     /* actually, -127 + 128 + 1 */

    for (i=0; i<4; i++) msbin[i] = 0;

    msbin[3] = msbin_exp;

    msbin[2] |= sign;
    msbin[2] |= ( unsigned char ) ( ieee[2] & 0x7f );
    msbin[1] = ieee[1];
    msbin[0] = ieee[0];

    return 0;
}


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: IEEE to MSBIN converter

Postby Antonio Linares » Sun Jan 18, 2015 6:29 pm

Enrico,

The inner format of float numbers is different from one C compiler to another.

What C compiler was that example for ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41323
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: IEEE to MSBIN converter

Postby Enrico Maria Giordano » Sun Jan 18, 2015 7:45 pm

Antonio,

it should be for Borland, what I'm using.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: IEEE to MSBIN converter

Postby Antonio Linares » Sun Jan 18, 2015 8:26 pm

Enrico,

See this:

Code: Select all  Expand view
int _fieeetomsbin(float *src4, float *dest4)
     {
     unsigned char *ieee = (unsigned char *)src4;
     unsigned char *msbin = (unsigned char *)dest4;
     unsigned char sign = 0x00;
     unsigned char msbin_exp = 0x00;
     int i;

     /* See _fmsbintoieee() for details of formats   */
     sign = ieee[3] & 0x80;
     msbin_exp |= ieee[3] << 1;
     msbin_exp |= ieee[2] >> 7;

     /* An ieee exponent of 0xfe overflows in MBF    */
     if (msbin_exp == 0xfe) return 1;

     msbin_exp += 2;     /* actually, -127 + 128 + 1 */

     for (i=0; i<4; i++) msbin[i] = 0;

     msbin[3] = msbin_exp;

     msbin[2] |= sign;
     msbin[2] |= ieee[2] & 0x7f;
     msbin[1] = ieee[1];
     msbin[0] = ieee[0];

     return 0;
     }
 


http://files.mpoli.fi/unpacked/software/programm/c/c_all.zip/ti1400.asc

It seems as it is for Borland:
// The following are implementations of Microsoft RTL functions
// not include in the Borland RTL.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41323
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: IEEE to MSBIN converter

Postby Antonio Linares » Sun Jan 18, 2015 8:28 pm

ops, thats the one you are using :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41323
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Re: IEEE to MSBIN converter

Postby Antonio Linares » Sun Jan 18, 2015 10:57 pm

Enrico,

They are just 4 bytes, so maybe you could do the conversion from PRG code and inspect each byte.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41323
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: IEEE to MSBIN converter

Postby Enrico Maria Giordano » Mon Jan 19, 2015 9:17 am

Antonio,

I already done all the tests I can imagine. The problem is that I don't know what the result should be. What I know for sure is that it's failing (GPF) for some values. :-(

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: IEEE to MSBIN converter

Postby Antonio Linares » Mon Jan 19, 2015 9:53 am

Enrico,

So the problem is a GPF ?

Don't you get a hb_out.log with xHarbour to report the GPF origin ?

Is the GPF coming from that function ?

We could place some traces into it...
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41323
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: IEEE to MSBIN converter

Postby Enrico Maria Giordano » Mon Jan 19, 2015 10:25 am

Antonio,

yes, I already traced the error in how the function _fieeetomsbin() fills the float number producing a NAN for some values.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: IEEE to MSBIN converter

Postby Enrico Maria Giordano » Mon Jan 19, 2015 10:48 pm

This is a complete sample using a different function that shows the problem too:

Code: Select all  Expand view
#include <stdio.h>


int _fieeetomsbin( float *src, float *dst )
{
    union
    {
        float a;
        unsigned long b;
    } c;

    unsigned short man;
    unsigned short exp;

    c.a = *src;

    if ( c.b )
    {
        man = c.b >> 16;
        exp = ( ( man << 1 ) & 0xff00 ) + 0x0200;

        if ( ( exp & 0x8000 ) != ( ( man << 1 ) & 0x8000 ) )
            return 1;

        man = ( man & 0x7f ) | ( ( man >> 8 ) & 0x80 );
        man |= exp;
        c.b = ( c.b & 0xffff ) | ( ( long ) man << 16 );
    }

    *dst = c.a;

    return 0;
}


float IeeToMsb( double nTmp )
{
    float nIee = ( float ) nTmp;
    float nMsb;

    _fieeetomsbin( &nIee, &nMsb );

    return nMsb;
}


int main()
{
    printf ( "%f\n", IeeToMsb( 0.328 ) );
    printf ( "%f\n", IeeToMsb( -0.328 ) );

    return 0;
}


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: IEEE to MSBIN converter

Postby Antonio Linares » Tue Jan 20, 2015 7:01 am

Enrico,

Does it GPF ?

If so, where ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41323
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: IEEE to MSBIN converter

Postby Enrico Maria Giordano » Tue Jan 20, 2015 8:33 am

Antonio,

with Borland it GPFs on return of the function _fieeetomsbin(). I tried three different online C compilers and got NAN (Not A Number) in the second printf().

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8331
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 15 guests