FreeCalypso > hg > freecalypso-citrine
comparison sprintf/float.c @ 0:75a11d740a02
initial import of gsm-fw from freecalypso-sw rev 1033:5ab737ac3ad7
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Thu, 09 Jun 2016 00:02:41 +0000 |
| parents | |
| children | a2d5d622e19e |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:75a11d740a02 |
|---|---|
| 1 /* | |
| 2 * Embedded [v]sprintf() implementation by Michael Spacefalcon, | |
| 3 * loosely based on the 4.3BSD-Tahoe version. | |
| 4 * | |
| 5 * This module contains the floating point conversion functions. | |
| 6 */ | |
| 7 | |
| 8 #include <sys/types.h> | |
| 9 #include <ctype.h> | |
| 10 #include "defs.h" | |
| 11 | |
| 12 extern double modf(); | |
| 13 | |
| 14 extern u_char * _sprintf_field(u_char *op, int width, int flags, int sign, | |
| 15 u_char *body, int size, int dprec, int fpprec); | |
| 16 | |
| 17 #define MAX_INT_DIGITS 10 /* 2^32-1 in decimal */ | |
| 18 #define MAXFRACT 16 /* max sensible for 64-bit double */ | |
| 19 #define DEFPREC 6 | |
| 20 | |
| 21 static char * | |
| 22 emit_integer_portion(unsigned number, char *endp) | |
| 23 { | |
| 24 char *t = endp; | |
| 25 | |
| 26 do { | |
| 27 *--t = tochar(number % 10); | |
| 28 number /= 10; | |
| 29 } while (number); | |
| 30 return (t); | |
| 31 } | |
| 32 | |
| 33 static int | |
| 34 f_round(double fract, char *start, char *end, int sign) | |
| 35 { | |
| 36 double tmp; | |
| 37 | |
| 38 (void)modf(fract * 10, &tmp); | |
| 39 if (tmp > 4) | |
| 40 for (;; --end) { | |
| 41 if (*end == '.') | |
| 42 --end; | |
| 43 if (++*end <= '9') | |
| 44 break; | |
| 45 *end = '0'; | |
| 46 } | |
| 47 /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */ | |
| 48 else if (sign == '-') | |
| 49 for (;; --end) { | |
| 50 if (*end == '.') | |
| 51 --end; | |
| 52 if (*end != '0') | |
| 53 break; | |
| 54 if (end == start) | |
| 55 return(1); /* clear the -ve */ | |
| 56 } | |
| 57 return(0); | |
| 58 } | |
| 59 | |
| 60 u_char * | |
| 61 _sprintf_percent_f(u_char *op, int width, int flags, int sign, | |
| 62 double number, int prec) | |
| 63 { | |
| 64 char buf[MAX_INT_DIGITS + 1 + MAXFRACT]; | |
| 65 int extra_prec = 0; | |
| 66 int origsign = sign; | |
| 67 double fract, tmp; | |
| 68 char *start, *t; | |
| 69 | |
| 70 /* first order of business: weed out infinities and NaNs */ | |
| 71 if (isinf(number)) { | |
| 72 if (number < 0) | |
| 73 sign = '-'; | |
| 74 return _sprintf_field(op, width, flags, sign, "Inf", 3, 0, 0); | |
| 75 } | |
| 76 if (isnan(number)) | |
| 77 return _sprintf_field(op, width, flags, sign, "NaN", 3, 0, 0); | |
| 78 /* OK, we know it's a valid real like in the good old VAX days */ | |
| 79 if (number < 0) { | |
| 80 sign = '-'; | |
| 81 number = -number; | |
| 82 } | |
| 83 fract = modf(number, &tmp); | |
| 84 if (tmp > (double) 0xFFFFFFFF) | |
| 85 return _sprintf_field(op, width, flags, sign, "Toobig", 6, | |
| 86 0, 0); | |
| 87 start = emit_integer_portion((unsigned) tmp, buf + MAX_INT_DIGITS); | |
| 88 if (prec > MAXFRACT) { | |
| 89 extra_prec = prec - MAXFRACT; | |
| 90 prec = MAXFRACT; | |
| 91 } else if (prec == -1) | |
| 92 prec = DEFPREC; | |
| 93 t = buf + MAX_INT_DIGITS; | |
| 94 /* | |
| 95 * if precision required or alternate flag set, add in a | |
| 96 * decimal point. | |
| 97 */ | |
| 98 if (prec || flags&ALT) | |
| 99 *t++ = '.'; | |
| 100 /* if requires more precision and some fraction left */ | |
| 101 if (fract) { | |
| 102 if (prec) | |
| 103 do { | |
| 104 fract = modf(fract * 10, &tmp); | |
| 105 *t++ = tochar((int)tmp); | |
| 106 } while (--prec && fract); | |
| 107 if (fract && f_round(fract, start, t - 1, sign)) | |
| 108 sign = origsign; | |
| 109 } | |
| 110 return _sprintf_field(op, width, flags, sign, start, t - start, | |
| 111 0, prec + extra_prec); | |
| 112 } |
