1*4887Schin /*********************************************************************** 2*4887Schin * * 3*4887Schin * This software is part of the ast package * 4*4887Schin * Copyright (c) 1985-2007 AT&T Knowledge Ventures * 5*4887Schin * and is licensed under the * 6*4887Schin * Common Public License, Version 1.0 * 7*4887Schin * by AT&T Knowledge Ventures * 8*4887Schin * * 9*4887Schin * A copy of the License is available at * 10*4887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 11*4887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12*4887Schin * * 13*4887Schin * Information and Software Systems Research * 14*4887Schin * AT&T Research * 15*4887Schin * Florham Park NJ * 16*4887Schin * * 17*4887Schin * Glenn Fowler <gsf@research.att.com> * 18*4887Schin * David Korn <dgk@research.att.com> * 19*4887Schin * Phong Vo <kpv@research.att.com> * 20*4887Schin * * 21*4887Schin ***********************************************************************/ 22*4887Schin #include "sfhdr.h" 23*4887Schin 24*4887Schin /* Write out a floating point value in a portable format 25*4887Schin ** 26*4887Schin ** Written by Kiem-Phong Vo. 27*4887Schin */ 28*4887Schin 29*4887Schin #if __STD_C 30*4887Schin int _sfputd(Sfio_t* f, Sfdouble_t v) 31*4887Schin #else 32*4887Schin int _sfputd(f,v) 33*4887Schin Sfio_t* f; 34*4887Schin Sfdouble_t v; 35*4887Schin #endif 36*4887Schin { 37*4887Schin #define N_ARRAY (16*sizeof(Sfdouble_t)) 38*4887Schin reg ssize_t n, w; 39*4887Schin reg uchar *s, *ends; 40*4887Schin int exp; 41*4887Schin uchar c[N_ARRAY]; 42*4887Schin Sfdouble_t x; 43*4887Schin 44*4887Schin SFMTXSTART(f,-1); 45*4887Schin 46*4887Schin if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) 47*4887Schin SFMTXRETURN(f, -1); 48*4887Schin SFLOCK(f,0); 49*4887Schin 50*4887Schin /* get the sign of v */ 51*4887Schin if(v < 0.) 52*4887Schin { v = -v; 53*4887Schin n = 1; 54*4887Schin } 55*4887Schin else n = 0; 56*4887Schin 57*4887Schin /* make the magnitude of v < 1 */ 58*4887Schin if(v != 0.) 59*4887Schin v = frexpl(v,&exp); 60*4887Schin else exp = 0; 61*4887Schin 62*4887Schin /* code the sign of v and exp */ 63*4887Schin if((w = exp) < 0) 64*4887Schin { n |= 02; 65*4887Schin w = -w; 66*4887Schin } 67*4887Schin 68*4887Schin /* write out the signs and the exp */ 69*4887Schin SFOPEN(f,0); 70*4887Schin if(sfputc(f,n) < 0 || (w = sfputu(f,w)) < 0) 71*4887Schin SFMTXRETURN(f, -1); 72*4887Schin SFLOCK(f,0); 73*4887Schin w += 1; 74*4887Schin 75*4887Schin s = (ends = &c[0])+sizeof(c); 76*4887Schin while(s > ends) 77*4887Schin { /* get 2^SF_PRECIS precision at a time */ 78*4887Schin n = (int)(x = ldexpl(v,SF_PRECIS)); 79*4887Schin *--s = n|SF_MORE; 80*4887Schin v = x-n; 81*4887Schin if(v <= 0.) 82*4887Schin break; 83*4887Schin } 84*4887Schin 85*4887Schin /* last byte is not SF_MORE */ 86*4887Schin ends = &c[0] + sizeof(c) -1; 87*4887Schin *ends &= ~SF_MORE; 88*4887Schin 89*4887Schin /* write out coded bytes */ 90*4887Schin n = ends - s + 1; 91*4887Schin w = SFWRITE(f,(Void_t*)s,n) == n ? w+n : -1; 92*4887Schin 93*4887Schin SFOPEN(f,0); 94*4887Schin SFMTXRETURN(f,w); 95*4887Schin } 96