views:

249

answers:

3

Hi,

I've found the following question Convert Delphi Real48 to C# double but I want to go the other way, C# to Delphi.

Does anyone know how this can be done? I've tried reverse engineering the code but without much luck.

Update:

I'm after C# code that will take a double and convert it into a Real48 (byte[] of size 6).

Thanks

+1  A: 

If you're familiar with C (as you're writing in C# you should be fine), check out this function. Moving it across into C# should not be too difficult.

It's rather ugly, but neccesary I'm thinking.

Reference: http://forums.ni.com/ni/board/message?board.id=60&message.id=3553

    enum prconverr double_to_real (double d, real *r)
    /* converts C double to Pascal real, returns error code */

{
    union doublearray da;
    unsigned x;

    da.d = d;

    /* check for 0.0 */
    if ((da.a[0] == 0x0000) &&
        (da.a[1] == 0x0000) &&
        (da.a[2] == 0x0000) &&
        /* ignore sign bit */
        ((da.a[3] & 0x7FFF) == 0x0000)) {
        /* exponent and significand are both 0, so value is 0.0 */
        (*r)[2] = (*r)[1] = (*r)[0] = 0x0000;
        /* sign bit is ignored ( -0.0 -> 0.0 ) */
        return prOK;
    }

    /* test for maximum exponent value */
    if ((da.a[3] & 0x7FF0) == 0x7FF0) {
        /* value is either Inf or NaN */
        if ((da.a[0] == 0x0000) &&
            (da.a[1] == 0x0000) &&
            (da.a[2] == 0x0000) &&
            ((da.a[3] & 0x000F) == 0x0000)) {
            /* significand is 0, so value is Inf */
            /* value becomes signed maximum real, */
            /* and error code prInf is returned */
            (*r)[1] = (*r)[0] = 0xFFFF;
            (*r)[2] = 0x7FFF |
                      (da.a[3] & 0x8000); /* retain sign bit */
            return prInf;
        } else {
            /* significand is not 0, so value is NaN */
            /* value becomes 0.0, and prNaN code is returned */
            /* sign bit is ignored (no negative NaN) */
            (*r)[2] = (*r)[1] = (*r)[0] = 0x0000;
            /* sign bit is ignored ( -NaN -> +NaN ) */
            return prNaN;
        }
    }

    /* round significand if necessary */
    if ((da.a[0] & 0x1000) == 0x1000) {
        /* significand's 40th bit set, so round significand up */
        if ((da.a[0] & 0xE000) != 0xE000)
            /* room to increment 3 most significant bits */
            da.a[0] += 0x2000;
        else {
            /* carry bit to next element */
            da.a[0] = 0x0000;
            /* carry from 0th to 1st element */
            if (da.a[1] != 0xFFFF)
                da.a[1]++;
            else {
                da.a[1] = 0x0000;
                /* carry from 1st to 2nd element */
                if (da.a[2] != 0xFFFF)
                    da.a[2]++;
                else {
                    da.a[2] = 0x0000;
                    /* carry from 2nd to 3rd element */
                    /* significand may overflow into exponent */
                    /* exponent not full, so won't overflow */
                    da.a[3]++;
                }
            }
        }
    }

    /* get exponent for underflow/overflow tests */
    x = (da.a[3] & 0x7FF0) >> 4;

    /* test for underflow */
    if (x < 895) {
        /* value is below real range */
        (*r)[2] = (*r)[1] = (*r)[0] = 0x0000;
        if ((da.a[3] & 0x8000) == 0x8000)
            /* sign bit was set, so value was negative */
            return prNegUnderflow;
        else
            /* sign bit was not set */
            return prPosUnderflow;
    }

    /* test for overflow */
    if (x > 1149) {
        /* value is above real range */
        (*r)[1] = (*r)[0] = 0xFFFF;
        (*r)[2] = 0x7FFF | (da.a[3] & 0x8000); /* retain sign bit */
        return prOverflow;
    }

    /* value is within real range */
    (*r)[0] = (x - 894) |  /* re-bias exponent */
              ((da.a[0] & 0xE000) >> 5) |  /* begin significand */
              (da.a[1] << 11);
    (*r)[1] = (da.a[1] >> 5) |
              (da.a[2] << 11);
    (*r)[2] = (da.a[2] >> 5) |
              ((da.a[3] & 0x000F) << 11) |
              (da.a[3] & 0x8000);  /* copy sign bit */
    return prOK;

}
Seidr
@Seidr I dont understand the code you've posted, where is it getting the return values from and how are you populating d.a with values at the start? Thanks
Simon G
From what I can see - the 'da' variable is an array of values (bits of memory I guess) derived from the input double. For the second argument of this function you provide a POINTER to a 'real' variable that you've defined before calling this function. The output is then piped out into that variable directly.As I said, this is a C function, and so will require modification in order to have it working in C#. I provided it purely as a point in the right direction. I'm unsure as to C#s support of pointers, you see.Good luck
Seidr
A: 

The simplest way, if possible, would be to convert it to a string, pass, then convert it back to a Real48

Zartog
A: 

I came across this thread looking for the same code. Here is what I ended up writing:

public static byte [] Double2Real48(double d)
{
    byte [] r48 = new byte[6];
    byte [] da = BitConverter.GetBytes(d);

    for (int i = 0; i < r48.Length; i++)
        r48[i] = 0;

    //Copy the negative flag
    r48[5] |= (byte)(da[7] & 0x80);

    //Get the expoent
    byte b1 = (byte)(da[7] & 0x7f);
    ushort n = (ushort)(b1 << 4);
    byte b2 = (byte)(da[6] & 0xf0);
    b2 >>= 4;
    n |= b2;

    if (n == 0)
        return r48;

    byte ex = (byte)(n - 1023);
    r48[0] = (byte)(ex + 129);

    //Copy the Mantissa
    r48[5] |= (byte)((da[6] & 0x0f) << 3);//Get the last four bits
    r48[5] |= (byte)((da[5] & 0xe0) >> 5);//Get the first three bits

    r48[4]  = (byte)((da[5] & 0x1f) << 3);//Get the last 5 bits
    r48[4] |= (byte)((da[4] & 0xe0) >> 5);//Get the first three bits

    r48[3]  = (byte)((da[4] & 0x1f) << 3);//Get the last 5 bits
    r48[3] |= (byte)((da[3] & 0xe0) >> 5);//Get the first three bits

    r48[2]  = (byte)((da[3] & 0x1f) << 3);//Get the last 5 bits
    r48[2] |= (byte)((da[2] & 0xe0) >> 5);//Get the first three bits

    r48[1]  = (byte)((da[2] & 0x1f) << 3);//Get the last 5 bits
    r48[1] |= (byte)((da[1] & 0xe0) >> 5);//Get the first three bits

    return r48;

}

Real48 is similar to IEEE 754 in that the Mantissa will be the same. The bit shift are necessary to get the Mantissa in the right location.

Real48 exponent has a bias of 129 and the double has a bias of 1023.

The negative flag is stored in the first bit of the last byte.

Notes: I don't think this code will work on a big endian machine. It does not check for NAN or INF.

Here is the code that converts a real48 to a double. It was ported from the Free Pascal compiler:

static double real2double(byte [] r)
{
    byte [] res = new byte[8];
    int exponent;

    //Return zero if the exponent is zero        
    if (r[0] == 0)
        return (double)0;

    //Copy Mantissa
    res[0] = 0;
    res[1] = (byte)(r[1] << 5);
    res[2] = (byte)((r[1] >> 3) | (r[2] << 5));
    res[3] = (byte)((r[2] >> 3) | (r[3] << 5));
    res[4] = (byte)((r[3] >> 3) | (r[4] << 5));
    res[5] = (byte)((r[4] >> 3) | ((r[5] & 0x7f) << 5));
    res[6] = (byte)((r[5] & 0x7f) >> 3);

    //Copy exponent
    //correct exponent
    exponent = (r[0] + (1023-129));
    res[6] = (byte)(res[6] | ((exponent & 0xf) << 4));
    res[7] = (byte)(exponent >> 4);

    //Set Sign
    res[7] = (byte)(res[7] | (r[5] & 0x80));
    return BitConverter.ToDouble(res, 0);  
}
jeng