///////////////////////////////////////////////////////////////////////////
////                        ieeefloat.c                                ////
////                                                                   ////
////    This library converts IEEE float format to and fro from        ////
////    the CCS PIC format.                                            ////
////        (C) Copyright 1996,2006 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                               ////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

/*
int32 f_PICtoIEEE(float f)
PURPOSE: This function converts float from CCS -PIC format to IEEE format in little endian mode
PARAMS: Floating point number
RETURNS: 32 bit number
*/
int32 f_PICtoIEEE(float f)
{

   int32 * ret;
   #locate f=0x20

   ret = &f;   

   #if defined(__PCB__) || defined(__PCM__)
      #asm
      
      // switch the bits
      rlf   0x21
      rrf   0x20
      rrf   0x21
      
      #endasm
   #elif defined(__PCH__)
      #asm
      
      // switch the bits
      rlcf   0x21
      rrcf   0x20
      rrcf   0x21   
      
      #endasm
   #else
      #error Invalid complier defined
   #endif
   
   #asm

   // swap the outer bytes
      movf   0x20,W
      xorwf  0x23,W
      xorwf  0x23,F
      xorwf  0x23,W
      movwf  0x20

   // swap the inner bytes
      movf   0x21,W
      xorwf  0x22,W
      xorwf  0x22,F
      xorwf  0x22,W
      movwf  0x21
   
   #endasm
   
   return *ret;
}

/*
float f_IEEEtoPIC(int32 f)
PURPOSE: This function converts IEEE format to a float
PARAMS: 32 bit number
RETURNS: Floating point number
*/
float f_IEEEtoPIC(int32 f)
{

   float * ret;
   #locate f=0x20
   
   ret = &f;
   
   #asm

   // swap the outer bytes
      movf   0x20,W
      xorwf  0x23,W
      xorwf  0x23,F
      xorwf  0x23,W
      movwf  0x20

   // swap the inner bytes
      movf   0x21,W
      xorwf  0x22,W
      xorwf  0x22,F
      xorwf  0x22,W
      movwf  0x21

   #endasm

   #if defined(__PCB__) || defined(__PCM__)
      #asm

      // switch the bits
      rlf   0x21
      rlf   0x20
      rrf   0x21
      
      #endasm
   #elif defined(__PCH__)
      #asm
      
      // switch the bits
      rlcf   0x21
      rlcf   0x20
      rrcf   0x21   
      
      #endasm
   #else
      #error Invalid complier defined
   #endif
   
   return *ret;
}
