#include "grib_int.h"
#include "gribFortran.h"

void flt2ibmDP(double *rval, int *ival)
{
  double dval = *rval;
  unsigned char *cval = (unsigned char *) ival;

  ref2ibm(&dval, 32);
  int kexp, kmant;
  confp3(dval, &kexp, &kmant, 32, 1);

  /* printf("kexp, kmant %d %d %g %d\n",kexp, kmant, *rval, *ival); */
  cval[0] = (unsigned char)kexp;
  cval[1] = (unsigned char)(kmant >> 16);
  cval[2] = (unsigned char)(kmant >> 8);
  cval[3] = (unsigned char)kmant;
}

void flt2ibmSP(float *rval, int *ival)
{
  double dval = *rval;
  flt2ibmDP(&dval, ival);
}

void ibm2fltDP(int *ival, double *rval)
{
  unsigned char *cval = (unsigned char *) ival;

  int kexp = cval[0];
  int kmant = (cval[1] << 16) + (cval[2] << 8) + cval[3];

  *rval = decfp2(kexp, kmant);
  /* printf("kexp, kmant %d %d %g %d\n",kexp, kmant, *rval, *ival); */
}

void ibm2fltSP(int *ival, float *rval)
{
  double dval;
  ibm2fltDP(ival, &dval);

  *rval = (float)dval;
}
