#include <gpk_std.h>
#include "readwrite.h"
#include "floatfmt.h"
#include <math.h>
#include <stdio.h>
#include <assert.h>

double b2float_double(struct b2float x)
{
 return x.mantissa * pow(2.0, x.exponent-7);
}

double b4float_double(struct b4float x)
{
 return pow(2.0, x.exponent-7) * ((x.mantissa[2]/256.0
 									+ x.mantissa[1])/256.0
								   + x.mantissa[0]);
}

BOOL read_double_2(FILE *fp, double *q)
{
 struct b2float p;
 BOOL ok = read_schar_1(fp, &p.mantissa)
 			&& read_schar_1(fp, &p.exponent);
 assert(q);

 if(ok)	{
	*q = b2float_double(p);
	return TRUE;
 	}
 return FALSE;
}

BOOL read_float_2(FILE *fp, float *q)
{
 struct b2float p;
 BOOL ok = read_schar_1(fp, &p.mantissa)
 			&& read_schar_1(fp, &p.exponent);
 assert(q);

 if(ok)	{
	*q = b2float_double(p);
	return TRUE;
 	}
 return FALSE;
}

BOOL read_float_4(FILE *fp, float *q)
{
 struct b4float p;
 BOOL ok =  read_schars(fp, 3, p.mantissa)
 			&& read_schar_1(fp, &p.exponent);
 assert(q);

 if(ok)	{
	*q = b4float_double(p);
	return TRUE;
 	}
 return FALSE;
}

BOOL read_double_4(FILE *fp, double *q)
{
 struct b4float p;
 BOOL ok =  read_schars(fp, 3, p.mantissa)
 			&& read_schar_1(fp, &p.exponent);
 assert(q);

 if(ok)	{
	*q = b4float_double(p);
	return TRUE;
 	}
 return FALSE;
}
