]>
git.saurik.com Git - apple/libc.git/blob - i386/gen/ecvt.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * ecvt converts to decimal
27 * the number of digits is specified by ndigit
28 * decptp is set to the position of the decimal point
29 * signp is set to 0 for positive, 1 for negative
33 static double ecvt_rint(double x
);
34 static double ecvt_copysign(double x
, double y
);
38 /* big enough to handle %.20f conversion of 1e308 */
42 ecvt(arg
, ndigits
, decptp
, signp
)
44 int ndigits
, *decptp
, *signp
;
46 return(cvt(arg
, ndigits
, decptp
, signp
, 1));
50 fcvt(arg
, ndigits
, decptp
, signp
)
52 int ndigits
, *decptp
, *signp
;
54 return(cvt(arg
, ndigits
, decptp
, signp
, 0));
58 cvt(arg
, ndigits
, decptp
, signp
, eflag
)
60 int ndigits
, *decptp
, *signp
;
65 register char *p
, *p1
;
66 static char buf
[NDIG
] = { 0 };
71 if (ndigits
>= NDIG
-1)
80 while (p
< &buf
[ndigits
])
97 fj
= modf(fi
/10, &fi
);
98 /**--p1 = (int)((fj+.03)*10) + '0';*/
99 *--p1
= (int)ecvt_rint((fj
)*10) + '0';
102 while (p1
< &buf
[NDIG
])
104 } else if (arg
> 0) {
105 while ((fj
= arg
*10) < 1) {
114 * p pts to where fraction should be concatenated
115 * p1 is how far conversion must go to
119 /* fcvt must provide ndigits after decimal pt */
121 /* if decpt was negative, we might done for fcvt */
127 while (p
<= p1
&& p
< &buf
[NDIG
]) {
129 arg
= modf(arg
, &fj
);
130 *p
++ = (int)fj
+ '0';
133 * if we converted all the way to the end of the
134 * buf, don't mess with rounding since there's nothing
135 * significant out here anyway
137 if (p1
>= &buf
[NDIG
]) {
142 * round by adding 5 to last digit and propagating
165 static double ecvt_rint(double x
)
167 asm("frndint" : "=t" (x
) : "0" (x
));