/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#include "sfhdr.h"2223/* Write out a floating point value in a portable format24**25** Written by Kiem-Phong Vo.26*/2728#if __STD_C29int _sfputd(Sfio_t* f, Sfdouble_t v)30#else31int _sfputd(f,v)32Sfio_t* f;33Sfdouble_t v;34#endif35{36#define N_ARRAY (16*sizeof(Sfdouble_t))37reg ssize_t n, w;38reg uchar *s, *ends;39int exp;40uchar c[N_ARRAY];41Sfdouble_t x;42SFMTXDECL(f);4344SFMTXENTER(f,-1);4546if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)47SFMTXRETURN(f, -1);48SFLOCK(f,0);4950/* get the sign of v */51if(v < 0.)52{ v = -v;53n = 1;54}55else n = 0;5657/* make the magnitude of v < 1 */58if(v != 0.)59v = frexpl(v,&exp);60else exp = 0;6162/* code the sign of v and exp */63if((w = exp) < 0)64{ n |= 02;65w = -w;66}6768/* write out the signs and the exp */69SFOPEN(f,0);70if(sfputc(f,n) < 0 || (w = sfputu(f,w)) < 0)71SFMTXRETURN(f, -1);72SFLOCK(f,0);73w += 1;7475s = (ends = &c[0])+sizeof(c);76while(s > ends)77{ /* get 2^SF_PRECIS precision at a time */78n = (int)(x = ldexpl(v,SF_PRECIS));79*--s = n|SF_MORE;80v = x-n;81if(v <= 0.)82break;83}8485/* last byte is not SF_MORE */86ends = &c[0] + sizeof(c) -1;87*ends &= ~SF_MORE;8889/* write out coded bytes */90n = ends - s + 1;91w = SFWRITE(f,(Void_t*)s,n) == n ? w+n : -1;9293SFOPEN(f,0);94SFMTXRETURN(f,w);95}969798