/* **********************************************************************
 * *                                                                    *
 * *        Functions to convert VAX data formats                       *
 * *                                                                    *
 * **********************************************************************
 * File: libs/user/VaxData.c
 *
 *                        COPYRIGHT (c) 2005  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

/* Credits:  This code is taken from the NORDRAD1 code in fil.c */


#include <stdlib.h>
#include <math.h>

#include "sigtypes.h"
#include "user_lib.h"

/* All these functions deal with the byte swapping to the VAX format
 * to/from native format, as well at non-aligned data, as well as
 * the VAX floating point format.
 */

UINT2 GetVaxUint2( const UINT1 p[])
{
  UINT2 tmp;

  tmp = p[1];
  tmp = tmp<<8;
  tmp += p[0];
  return  tmp;
} 

UINT4 GetVaxUint4( const UINT1 p[])
{
  UINT4 tmp;

  tmp = p[3];
  tmp = tmp<<8;
  tmp += p[2];
  tmp = tmp<<8;
  tmp += p[1];
  tmp = tmp<<8;
  tmp += p[0];
  return  tmp;
} 

void PutVaxUint2( UINT1 p[], UINT2 value)
{
  p[0] = value&0xFF;
  p[1] = value>>8;
} 

void PutVaxUint4( UINT1 p[], UINT4 value)
{
  p[0] = value&0xFF;
  p[1] = (value>>8)&0xFF;
  p[2] = (value>>16)&0xFF;
  p[3] = (value>>24)&0xFF;
} 

void PutVaxFlt4( UINT1 p[], FLT4 number)
{
  FLT4 mantissa;
  long fraction;
  int exp;
  unsigned char Byte;

  if (number == 0)
    {
      *p = 0;
      *(p+1) = 0;
      *(p+2) = 0;
      *(p+3) = 0;
      return ;
    } 
  mantissa = frexp(number, &exp);
  if (mantissa < 0)
    mantissa = -mantissa;
  fraction = mantissa *0x1000000L;
  Byte = (fraction>>16)&0x7F;
  *p = Byte;
  Byte = (fraction>>8)&0xFF;
  *(p+3) = Byte;
  Byte = fraction&0xFF;
  *(p+2) = Byte;
  exp = exp+128;
  Byte = exp&0xFF;
  *(p+1) = (Byte>>1)&0x7F;
  Byte = exp&0x01;
  Byte = Byte<<7;
  *(p) = (*p)|(Byte);
  if (number < 0)
    *(p+1) = *(p+1)|0x80;
} 


FLT4 GetVaxFlt4( const UINT1 p[])
{
  FLT4 mantissa,number;
  long fraction,tmplong;
  unsigned int exp;
  unsigned char Byte;

/* all Zeros, return 0                                                      */

  if ((*p == 0)&(*(p+1) == 0)&(*(p+2) == 0)&(*(p+3) == 0))
    {
      return 0.0;
    } 
  Byte = *p;
  Byte = Byte|0x80;
  fraction = Byte;
  fraction = (fraction<<16);
  Byte = *(p+3);
  tmplong = Byte;
  tmplong = (tmplong<<8);
  fraction = fraction+tmplong;
  Byte = *(p+2);
  tmplong = Byte;
  fraction = fraction+tmplong;
  mantissa = fraction;
  mantissa = mantissa/0x1000000L;
  Byte = *(p+1);
  Byte = (Byte<<1);
  exp = Byte;
  Byte = *(p);
  Byte = (Byte>>7);
  exp = exp+Byte;
  exp = exp-128;
  number = ldexp(mantissa, exp);
  if ((*(p+1)&0x80) != 0)
    number = -number;
  return  number;
} 
