1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#include "sfhdr.h"
1N/A
1N/A/* Write out a floating point value in a portable format
1N/A**
1N/A** Written by Kiem-Phong Vo.
1N/A*/
1N/A
1N/A#if __STD_C
1N/Aint _sfputd(Sfio_t* f, Sfdouble_t v)
1N/A#else
1N/Aint _sfputd(f,v)
1N/ASfio_t* f;
1N/ASfdouble_t v;
1N/A#endif
1N/A{
1N/A#define N_ARRAY (16*sizeof(Sfdouble_t))
1N/A reg ssize_t n, w;
1N/A reg uchar *s, *ends;
1N/A int exp;
1N/A uchar c[N_ARRAY];
1N/A Sfdouble_t x;
1N/A SFMTXDECL(f);
1N/A
1N/A SFMTXENTER(f,-1);
1N/A
1N/A if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
1N/A SFMTXRETURN(f, -1);
1N/A SFLOCK(f,0);
1N/A
1N/A /* get the sign of v */
1N/A if(v < 0.)
1N/A { v = -v;
1N/A n = 1;
1N/A }
1N/A else n = 0;
1N/A
1N/A /* make the magnitude of v < 1 */
1N/A if(v != 0.)
1N/A v = frexpl(v,&exp);
1N/A else exp = 0;
1N/A
1N/A /* code the sign of v and exp */
1N/A if((w = exp) < 0)
1N/A { n |= 02;
1N/A w = -w;
1N/A }
1N/A
1N/A /* write out the signs and the exp */
1N/A SFOPEN(f,0);
1N/A if(sfputc(f,n) < 0 || (w = sfputu(f,w)) < 0)
1N/A SFMTXRETURN(f, -1);
1N/A SFLOCK(f,0);
1N/A w += 1;
1N/A
1N/A s = (ends = &c[0])+sizeof(c);
1N/A while(s > ends)
1N/A { /* get 2^SF_PRECIS precision at a time */
1N/A n = (int)(x = ldexpl(v,SF_PRECIS));
1N/A *--s = n|SF_MORE;
1N/A v = x-n;
1N/A if(v <= 0.)
1N/A break;
1N/A }
1N/A
1N/A /* last byte is not SF_MORE */
1N/A ends = &c[0] + sizeof(c) -1;
1N/A *ends &= ~SF_MORE;
1N/A
1N/A /* write out coded bytes */
1N/A n = ends - s + 1;
1N/A w = SFWRITE(f,(Void_t*)s,n) == n ? w+n : -1;
1N/A
1N/A SFOPEN(f,0);
1N/A SFMTXRETURN(f,w);
1N/A}