Float byte format

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
richardb
Posts: 315
Joined: Tue Oct 03, 2006 8:54 pm

Float byte format

Post by richardb » Mon Jun 03, 2024 9:41 am

hi, I'm trying to send a float to a pc using the following

Code: Select all


Public Sub WriteFloat(pValue As Float)
   WriteByte(pValue.Bytes(0))
   WriteByte(pValue.Bytes(1))
   WriteByte(pValue.Bytes(2))
   WriteByte(pValue.Bytes(3))
End Sub
 End Sub
the data i get back on the pc doesn't seem in a standard Microsoft format but I'm not quite sure what is standard. for swordfish

I see the serial routine built into sf is the same byte order but i get the wrong value in windows using BitConverter.ToSingle
if i send 0.1 i get the following in my windows program

123 76 204 205
but i think it should be as follows


Byte array representation:
205 204 204 61
Converted float value: 0.1

any ideas?
Hmmm..

Jerry Messina
Swordfish Developer
Posts: 1486
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Float byte format

Post by Jerry Messina » Mon Jun 03, 2024 1:22 pm

Swordfish uses the same 32-bit floating point format as the old Microchip C compiler, so it's not the same as IEEE-754.

Here's some old code I had laying around to convert between the two... I *THINK* this works (but I haven't had another look at it)

Code: Select all

// SF floats are in 32-bit Microchip format, low:high (little-endian)
structure sf_float_t
    b(4) as byte union
    ul   as longword union
    f    as float union
end structure

structure ieee_t
    b(4) as byte union
    ul   as longword union
    f    as float union
end structure

{
                      +-----------+-----------+-----------+-----------+
                      |    eb     |    f0     |    f1     |    f2     |
   +------------------+-----------+-----------+-----------+-----------+
   | IEEE754 32-bit   | sxxx xxxx | yxxx xxxx | xxxx xxxx | xxxx xxxx |
   +------------------+-----------+-----------+-----------+-----------+
   | Microchip 32-bit | xxxx xxxx | sxxx xxxx | xxxx xxxx | xxxx xxxx |
   +------------------+-----------+-----------+-----------+-----------+

   The conversion from MCHP <--> IEEE754 requires the upper two bytes
   be rotated to move the sign bit around.
   note: there are two Microchip 32-bit formats: 
    high-low (big-endian) and low-high (little-endian)
   The above layout assumes big-endian format, NOT the default 32-bit little-endian format
}

function mchptoieee (mc_float as sf_float_t) as ieee_t
    dim ieee_fmt as ieee_t
    dim b as byte

    // swap mc low:high format to high:low
    b = mc_float.b(0)
    mc_float.b(0) = mc_float.b(3)
    mc_float.b(3) = b
    b = mc_float.b(1)
    mc_float.b(1) = mc_float.b(2)
    mc_float.b(2) = b
  
    // do the sign bit manipulation
    FSR0 = addressof(mc_float) + 2
    asm-
        bcf STATUS, 0, 0
        rlcf POSTINC0, 1, 0
        rrcf POSTDEC0, 1, 0
        rrcf INDF0, 1, 0
    end asm

    ieee_fmt.ul = mc_float.ul
    result = ieee_fmt
end function

function ieeetomchp (ieee_float as ieee_t) as float
    dim b as byte

    // do the sign bit manipulation
    FSR0 = addressof(ieee_float) + 2
    asm-
        bcf STATUS, 0, 0
        rlcf POSTINC0, 1, 0
        rlcf POSTDEC0, 1, 0
        rrcf INDF0, 1, 0
    end asm

    // swap mc high:low format to low:high
    b = ieee_float.b(0)
    ieee_float.b(0) = ieee_float.b(3)
    ieee_float.b(3) = b
    b = ieee_float.b(1)
    ieee_float.b(1) = ieee_float.b(2)
    ieee_float.b(2) = b

    result = ieee_float.f
end function

richardb
Posts: 315
Joined: Tue Oct 03, 2006 8:54 pm

Re: Float byte format

Post by richardb » Tue Jun 04, 2024 10:52 am

Thanks Jerry, I am trying to decode this in the windows application as this is for diagnostics/debug so I wanted to minimize the amount of time the pic spends sending data.

I have now found an application note from microchip but I am getting incorrect values in windows so I must have messed up my bit shifting.


Thanks for looking


Rich
Hmmm..

Post Reply