Subject: VAX D formats to Sun IEEE

From: [email-masked] (Kenneth H. Simpson)
Date: Sun, 1 Aug 1993 22:05:42 GMT

Here is a code fragment - haven't looked at for a while - should work. 

/*----------------------------------------------------------------------- 
   vax2sun.c - converts VAX D_floating floats stored on a Sun to 
   IEEE floats. 
-------------------------------------------------------------------------*/ 
typedef unsigned int bits;                   /* int bit fields */
	
typedef union { 
	float real; 
	unsigned char byte[4];
	struct {          
		bits fraction      : 7;
		bits exponent      : 8;	   
		bits sign          : 1;	 
		bits word          :16;
	} vaxbits; 
} VAXFLOAT; 

typedef union { 
	float real; 
	unsigned char byte[4];
	struct {                    
		bits word          :16;
		bits fraction      : 7;
		bits exponent      : 8;	   
		bits sign          : 1;	 
	} sunbits;
} SUNFLOAT; 

typedef struct {                                /* vax to sun double */
	bits n0 : 2;   /* high byte */
	bits    :27;
	bits n1 : 3;
	bits n2 : 3;   /* low byte */
	bits    :29;
} BITS;

typedef union {                 /* bit swap */
	double real;
	int integer[2];
	BITS bits;
} DOUBLE;

static float SunFloat(vaxDfloat) 
	float vaxDfloat; 
{ 
  	SUNFLOAT sunfloat, *sunptr = &sunfloat; 
  	VAXFLOAT vaxfloat, *vaxptr = &vaxfloat; 
  
  	vaxptr->real = vaxDfloat; 
 
  	/* map bits */ 
  
  	sunptr->sunbits.sign     = vaxptr->vaxbits.sign; 
  	sunptr->sunbits.exponent = vaxptr->vaxbits.exponent; 
  	sunptr->sunbits.fraction = vaxptr->vaxbits.fraction; 
  	sunptr->sunbits.word     = vaxptr->vaxbits.word; 

  	return(sunptr->real/4.0);		
}

/* Ken Stewart's code? */

static double SunDouble(vaxDfloat) /* convert VAX D_float to double */
	DOUBLE vaxDfloat;
{
	int save;	/* save sign and high exp bit */
	
	vaxDfloat.integer[1] 	= vaxDfloat.integer[1]>>3; /* low integer */
	vaxDfloat.bits.n2 	= vaxDfloat.bits.n1;
	save 			= vaxDfloat.bits.n0;
	vaxDfloat.bits.n0 	= 0;
	vaxDfloat.integer[0] 	= vaxDfloat.integer[0]>>3; /* high integer */
	vaxDfloat.bits.n0 	= save;

	return(vaxDfloat.real/(double)4);	/* normalize */	
}
-- 
==============================================================================
Kenneth Simpson                        		NASA
Internet: [email-masked]         	Ames Research Center, MS/269-1
UUCP: ames!ptolemy!ken    			Moffett Field, CA 94035-1000