HHDB Example Source Code


/*
  **********************************************************************
  * INT2BIGE.c, BIGE2INT.c, FLT2IEEE.c, IEEE2FLT,c                     *
  * Copyright (C) 1997 Dept. of Mathematics, University of Houston     *
  * This is free software; you can use, copy, distribute and/or modify *
  * it freely under the disclaimer that no warranties are offered nor  *
  * liabilities assumed through such actions.                          *
  **********************************************************************
  **********************************************************************
  * Four utilities for machines that either don't use standard ints,   *
  * such as Dec, Intel etc., or floats, such as Cray etc.              *
  *                                                                    *
  *  (void)INT2BIGE(local_int,bige_int)                                *
  *  int *local_int;                                                   *
  *  char bige_int[4];                                                 *
  *    Converts your local integer to a 4-byte, big-endian,            *
  *    2's complement integer.                                         *
  *                                                                    *
  *  (void)BIGE2INT(local_int,bige_int)                                *
  *    Converts a 4-byte, big-endian, 2's complement integer,          *
  *    to your local integer.                                          *
  *                                                                    *
  *  (void)FLT2IEEE(local_flt,ieee_flt)                                *
  *  float *local_flt;                                                 *
  *  char  ieee_flt[4];                                                *
  *    Converts your local float to a 4-byte ieee float.               *
  *                                                                    *
  *  (void)IEEE2FLT(local_flt,ieee_flt)                                *
  *    Converts a 4-byte ieee float to your local float.               *
  *                                                                    *
  * To cross-link with Fortran, define these for your machine:         *
  *   #define INT2BIGE int2bige_ (This works for yeach suns.)          *
  *   #define BIGE2INT bige2int_                                       *
  *   #define FLT2IEEE flt2ieee_                                       *
  *   #define IEEE2FLT ieee2flt_                                       *
  *                                                                    *
  * Make sure that your Unix has (double)frexp(double,int*) and        *
  * (double)ldexp(double,int). You may need to compile this with       *
  * the -lm library.                                                   *
  **********************************************************************
*/

#define INT2BIGE int2bige
#define BIGE2INT bige2int
#define FLT2IEEE flt2ieee
#define IEEE2FLT ieee2flt

#define FREXP(val,exp) frexp(val,exp)
#define LDEXP(val,ex)  ldexp(val,ex)

#include <math.h> /* Need this for frexp & ldexp above. See man pages. */


void INT2BIGE(local_int,bige_int)
int           *local_int;
unsigned char *bige_int;
{
  if( *local_int < 0 ) {
    unsigned long ul = -(long)(*local_int+1);
    unsigned char shft;

    shft = ul/16777216; ul = ul-shft*16777216;
    bige_int[0] = ~shft;
    shft = ul/65536;    ul = ul-shft*65536;
    bige_int[1] = ~shft;
    shft = ul/256;      ul = ul-shft*256;
    bige_int[2] = ~shft;
    bige_int[3] = ~ul;
    return;
  }
  else {
    unsigned long ul =  (long)(*local_int);
    unsigned char shft;

    shft = ul/16777216; ul = ul-shft*16777216;
    bige_int[0] = shft;
    shft = ul/65536;    ul = ul-shft*65536;
    bige_int[1] = shft;
    shft = ul/256;      ul = ul-shft*256;
    bige_int[2] = shft;
    bige_int[3] = ul;
    return;
  }
}


void BIGE2INT(local_int,bige_int)
int           *local_int;
unsigned char *bige_int;
{
  if( bige_int[0] > 127 ) {
    unsigned char be0 = ~bige_int[0], be1 = ~bige_int[1],
                  be2 = ~bige_int[2], be3 = ~bige_int[3];
    *local_int = -(1+be3+256*(be2+256*(be1+256*be0)));
    return;
  }
  else {
    *local_int = bige_int[3]+256*(bige_int[2]
                +256*(bige_int[1]+256*bige_int[0]));
    return;
  }
}


void FLT2IEEE(local_flt,ieee_flt)
float         *local_flt;
unsigned char *ieee_flt;
{
  double frac;
  int S,E;
  long  F,shft;

  frac = 2.0*FREXP((double)*local_flt,&E); /* Normalize to [1.0,2.0). */
  E = E+126; /* Normalized to [0,255]. */
  S = 0;
  if( frac < 0.0 ) {
    S = 128;
    frac = -frac;
  }
  F = (frac-1.0)*(8388608.0)+0.5; /* (8388608) = 2**23. */

  /* Clip out-of-range exp's. (We're cheating a little here.) */
  if( E <= 0 ) {
    E = 0;
    F = 0; /* Yields +-0. */
  }
  if( E >= 255 ) {
    E = 255;
    F = 0; /* Yields +-infty. */
  }

  /* Build: SEEEEEEE EFFFFFF FFFFFFFF FFFFFFFFF */
  ieee_flt[0]  = S;                      shft = E/2;
  ieee_flt[0] |= shft; E = (E-shft*2)*128;
  ieee_flt[1]  = E;                      shft = F/65536;
  ieee_flt[1] |= shft; F = F-shft*65536; shft = F/256;
  ieee_flt[2]  = shft; F = F-shft*256;
  ieee_flt[3]  = F;

  return;
}


void IEEE2FLT(local_flt,ieee_flt)
float         *local_flt;
unsigned char *ieee_flt;
{
  double frac;
  int S,E;
  long  F;

  /* Decode: SEEEEEEE EFFFFFF FFFFFFFF FFFFFFFFF */
  S = ( 128&ieee_flt[0] );
  E = ( (ieee_flt[0]&127)*2 ) | ( ieee_flt[1]/128 );
  F = ieee_flt[3] + 256*( ieee_flt[2] + 256*( ieee_flt[1]&127 ) );

  /* (We're cheating a little here.) */
  if( E == 0 ) {
    E = 0;
    frac = 0.0; /* Yields +-0. */
  }
  else
  if( E == 255 ) {
    E = 255;
    frac = 1.0; /* Yields +-infty. */
  }
  else {
    frac = (((double)F-0.5)*(0.11920928955078125E-06) + 1.0);
  }

  if( S ) 
    *local_flt = -LDEXP(frac,E-127);
  else
    *local_flt =  LDEXP(frac,E-127);

  return;
}

Last modified: Mon Sep 14 11:21:01 MET DST 1998