/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_form_real[] = "@(#)form_real.c	1.16 10/20/92 05:45:50  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINE:	__ROr_format_Real


PURPOSE:
	This routine converts a Real value into printable format

IN:
	value - value to print
	valuelength - length in bytes of value variable
	type_info - parameters vector for lengths of this type

OUT:
RETURNS malloc'ed character string

CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/exceptions.h>
#include <elros/enc_dcd.h>
#include <elros/typeinfo.h>
#include <elros/extend_arith.h>

#include <malloc.h>
#include <memory.h>
#include <string.h>

char *__ROr_format_Real(value,valuelength,maxlengthbits,maxlength2bits,type_info)

	void *value;
	unsigned long valuelength;
	unsigned long maxlengthbits;
	unsigned long maxlength2bits;
	__RO_Type_Info *type_info;

{
   unsigned long maxlength = maxlengthbits/8 + (maxlengthbits%8 > 0);
   unsigned long maxlength2 = maxlength2bits/8 + (maxlength2bits%8 > 0);
   unsigned char *byteptr = (unsigned char *)value;
   char *buffer,*bufptr;

   if (__RO_ISFLOAT(type_info,maxlength,maxlength2,valuelength))
   {
      char space[64];

      sprintf(space,"%e",*((float *)value));
      buffer = malloc(strlen(space)+1);
      strcpy(buffer,space);
      return(buffer);
   }
   else if (__RO_ISDOUBLE(type_info,maxlength,maxlength2,valuelength))
   {
      char space[64];

      sprintf(space,"%le",*((double *)value));
      buffer = malloc(strlen(space)+1);
      strcpy(buffer,space);
      return(buffer);
   }
   else if (__RO_ISLONGDOUBLE(type_info,maxlength,maxlength2,valuelength))
   {
      char space[64];

      sprintf(space,"%le",*((LONGEST_REAL *)value));
      buffer = malloc(strlen(space)+1);
      strcpy(buffer,space);
      return(buffer);
   }

/*  This doesn't work, since 'valuelength' is not real.
   if (valuelength != maxlength+maxlength2+1)
      return(__ROx_PRINT_REALLENGTHS);
*/
   valuelength = 1+maxlength2+maxlength;


/*
	// PUT OUT THE REAL IN BYTE FORMAT
   {
      int i;
      unsigned char *byteptr = (unsigned char *)value;

      bufptr = buffer = malloc((maxlength+maxlength2+1)*3+3);
      {
         sprintf(bufptr,"%2.2x ",(unsigned int)byteptr[0]);
         bufptr += 3;
      }
      *bufptr++ = '/';
      byteptr++;
      for (i=maxlength2-1; i>=0; i--)
      {
         sprintf(bufptr,"%2.2x ",(unsigned int)byteptr[i]);
         bufptr += 3;
      }
      *bufptr++ = '/';
      byteptr += maxlength2;
      for (i=maxlength-1; i>=0; i--)
      {
         sprintf(bufptr,"%2.2x ",(unsigned int)byteptr[i]);
         bufptr += 3;
      }
      *bufptr = 0;
   }
*/


   if (zeroeq(byteptr+1,maxlength2+maxlength))
      return(strcpy(malloc(4),"0.0"));

   {
      unsigned char *byteptr = 
	(unsigned char *)memcpy(malloc(valuelength),value,valuelength);
      unsigned char *expptr = byteptr+1;
      int explen = maxlength2;
      unsigned char *mantptr = byteptr+maxlength2+1;
      int mantlen = maxlength;
      unsigned char *exp10ptr = (unsigned char *)malloc(explen);
      int count = 0;
      unsigned long extra;

      swapends(expptr,explen);
      swapends(mantptr,mantlen);

      bufptr = buffer = malloc((int)(
   	(1+DECIMAL_DIGIT_COUNT(maxlength2)) +
   	(1+DECIMAL_DIGIT_COUNT(maxlength)) +
	(5)));

      *bufptr++ = (*byteptr & __REAL_SIGN_BIT) == __REAL_SIGN_BIT
		  ? '-' : '+'; /* sign */
      *bufptr++ = '.';

      memset(exp10ptr,0,explen);
      add_long(expptr,explen,mantlen*8);
      {
		/* While the binary exponent is negative, multiply by 10
		   until it's just positive or 0. */
         while (signof(expptr,explen))
         {
            decrement(exp10ptr,explen);
            extra = times10(mantptr,mantlen);
            while (extra)
            {
               rollright(mantptr,mantlen,extra&0x01);
               extra = extra >> 1;
               increment(expptr,explen);
            }
         }
		/* Now, while the binary exponent is positive, divide by 10
		   until it's 0 or turns negative. */
         while (!zeroeq(expptr,explen) && !signof(expptr,explen))
         {
            increment(exp10ptr,explen);
            extra = (divide10(mantptr,mantlen)*256)/10;
            while (!(signof(mantptr,mantlen)))
            {
               rollleft(mantptr,mantlen,extra>>7);
               extra = (extra << 1) & 0xFF;
               decrement(expptr,explen);
            }
		/* Round */
            if (extra >> 7)
               if (add_long(mantptr,mantlen,1))
                  rollright(mantptr,mantlen,1);
         }
		/* Now you have a "almost" zero binary exponent, so we shift
		   it right 0-3 times to make it 0 */
         while (!zeroeq(expptr,explen))
         {
            extra = shiftright(mantptr,mantlen);
            increment(expptr,explen);
         }
		/* Round */
         if (extra >> 7)
            if (add_long(mantptr,mantlen,1))
               rollright(mantptr,mantlen,1);

		/* Convert the digits to base 10 */

         while (!zeroeq(mantptr,mantlen) &&
		 count++ < DECIMAL_DIGIT_COUNT(maxlength)-1)
         {
            *bufptr++ = (times10(mantptr,mantlen)+'0');
         }

      }

      *bufptr++ = 'E';
      Itoa(exp10ptr,explen,bufptr);

      free(exp10ptr);
      free(byteptr);
   }

   return(buffer);
}


