]>
git.saurik.com Git - apple/libc.git/blob - gen.subproj/i386.subproj/ecvt.c
70b7c2562c9a011555c95f9364a2443748d0ca7c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * ecvt converts to decimal
24 * the number of digits is specified by ndigit
25 * decptp is set to the position of the decimal point
26 * signp is set to 0 for positive, 1 for negative
30 static double ecvt_rint(double x
);
31 static double ecvt_copysign(double x
, double y
);
35 /* big enough to handle %.20f conversion of 1e308 */
39 ecvt(arg
, ndigits
, decptp
, signp
)
41 int ndigits
, *decptp
, *signp
;
43 return(cvt(arg
, ndigits
, decptp
, signp
, 1));
47 fcvt(arg
, ndigits
, decptp
, signp
)
49 int ndigits
, *decptp
, *signp
;
51 return(cvt(arg
, ndigits
, decptp
, signp
, 0));
55 cvt(arg
, ndigits
, decptp
, signp
, eflag
)
57 int ndigits
, *decptp
, *signp
;
62 register char *p
, *p1
;
63 static char buf
[NDIG
] = { 0 };
68 if (ndigits
>= NDIG
-1)
77 while (p
< &buf
[ndigits
])
94 fj
= modf(fi
/10, &fi
);
95 /**--p1 = (int)((fj+.03)*10) + '0';*/
96 *--p1
= (int)ecvt_rint((fj
)*10) + '0';
99 while (p1
< &buf
[NDIG
])
101 } else if (arg
> 0) {
102 while ((fj
= arg
*10) < 1) {
111 * p pts to where fraction should be concatenated
112 * p1 is how far conversion must go to
116 /* fcvt must provide ndigits after decimal pt */
118 /* if decpt was negative, we might done for fcvt */
124 while (p
<= p1
&& p
< &buf
[NDIG
]) {
126 arg
= modf(arg
, &fj
);
127 *p
++ = (int)fj
+ '0';
130 * if we converted all the way to the end of the
131 * buf, don't mess with rounding since there's nothing
132 * significant out here anyway
134 if (p1
>= &buf
[NDIG
]) {
139 * round by adding 5 to last digit and propagating
162 static double ecvt_rint(double x
)
164 asm("frndint" : "=t" (x
) : "0" (x
));