Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / lib / libast / sfio / sfputd.c
1 /* $XConsortium: sfputd.c /main/3 1995/11/01 18:33:55 rswiston $ */
2 /***************************************************************
3 *                                                              *
4 *                      AT&T - PROPRIETARY                      *
5 *                                                              *
6 *         THIS IS PROPRIETARY SOURCE CODE LICENSED BY          *
7 *                          AT&T CORP.                          *
8 *                                                              *
9 *                Copyright (c) 1995 AT&T Corp.                 *
10 *                     All Rights Reserved                      *
11 *                                                              *
12 *           This software is licensed by AT&T Corp.            *
13 *       under the terms and conditions of the license in       *
14 *       http://www.research.att.com/orgs/ssr/book/reuse        *
15 *                                                              *
16 *               This software was created by the               *
17 *           Software Engineering Research Department           *
18 *                    AT&T Bell Laboratories                    *
19 *                                                              *
20 *               For further information contact                *
21 *                     gsf@research.att.com                     *
22 *                                                              *
23 ***************************************************************/
24 #include        "sfhdr.h"
25
26 /*      Write out a double value in a portable format
27 **
28 **      Written by Kiem-Phong Vo (08/05/90)
29 */
30
31 #if __STD_C
32 _sfputd(Sfio_t* f, reg double v)
33 #else
34 _sfputd(f,v)
35 Sfio_t          *f;
36 reg double      v;
37 #endif
38 {
39 #define N_ARRAY         (16*sizeof(double))
40         reg int         n, w;
41         reg double      x;
42         reg uchar       *s, *ends;
43         int             exp;
44         uchar           c[N_ARRAY];
45
46         if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
47                 return -1;
48         SFLOCK(f,0);
49
50         /* get the sign of v */
51         if(v < 0.)
52         {       v = -v;
53                 n = 1;
54         }
55         else    n = 0;
56
57         /* make the magnitude of v < 1 */
58         if(v != 0.)
59                 v = frexp(v,&exp);
60         else    exp = 0;
61
62         /* code the sign of v and exp */
63         if((w = exp) < 0)
64         {       n |= 02;
65                 w = -w;
66         }
67
68         /* write out the signs and the exp */
69         SFOPEN(f,0);
70         if(sfputc(f,n) < 0 || (w = sfputu(f,w)) < 0)
71                 return -1;
72         SFLOCK(f,0);
73         w += 1;
74
75         s = (ends = &c[0])+sizeof(c);
76         while(s > ends)
77         {       /* get 2^SF_PRECIS precision at a time */
78                 n = (int)(x = ldexp(v,SF_PRECIS));
79                 *--s = n|SF_MORE;
80                 if((v = x-n) <= 0.)
81                         break;
82         }
83
84         /* last byte is not SF_MORE */
85         ends = &c[0] + sizeof(c) -1;
86         *ends &= ~SF_MORE;
87
88         /* write out coded bytes */
89         n = ends - s + 1;
90         w = SFWRITE(f,(Void_t*)s,n) == n ? w+n : -1;
91
92         SFOPEN(f,0);
93         return w;
94 }