Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/util/xldio.c
blob: ab4005850de8fdf93497117216b53b06a6cc330e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/* This file contain long double I/O routines for Windows (because Windows API
   don't support long double at all.

   They don't work on other architectures. So be curefull. */


/* This source comes from the DJGPP runtime library. It has been hacked
   to work with XaoS */
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <config.h>
#include <stdlib.h>
#include <ctype.h>
#include <xio.h>
/*#include <libc/unconst.h>*/
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <xldio.h>
#ifdef USE_XLDIO

static long double powten[] = {
    1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, 1e64L, 1e128L, 1e256L,
    1e512L, 1e1024L, 1e2048L, 1e4096L
};

long double x_strtold(CONST char *s, CONST char **sret)
{
    long double r;		/* result */
    int e, ne;			/* exponent */
    int sign;			/* +- 1.0 */
    int esign;
    int flags = 0;
    int l2powm1;

    r = 0.0L;
    sign = 1;
    e = ne = 0;
    esign = 1;

    while (*s && isspace(*s))
	s++;

    if (*s == '+')
	s++;
    else if (*s == '-') {
	sign = -1;
	s++;
    }

    while ((*s >= '0') && (*s <= '9')) {
	flags |= 1;
	r *= 10.0L;
	r += *s - '0';
	s++;
    }

    if (*s == '.') {
	s++;
	while ((*s >= '0') && (*s <= '9')) {
	    flags |= 2;
	    r *= 10.0L;
	    r += *s - '0';
	    s++;
	    ne++;
	}
    }
    if (flags == 0) {
	if (sret)
	    *sret = /*unconst(s, char *) */ s;
	return 0.0L;
    }

    if ((*s == 'e') || (*s == 'E')) {
	s++;
	if (*s == '+')
	    s++;
	else if (*s == '-') {
	    s++;
	    esign = -1;
	}
	while ((*s >= '0') && (*s <= '9')) {
	    e *= 10;
	    e += *s - '0';
	    s++;
	}
    }
    if (esign < 0) {
	esign = -esign;
	e = -e;
    }
    e = e - ne;
    if (e < -4096) {
	/* possibly subnormal number, 10^e would overflow */
	r *= 1.0e-2048L;
	e += 2048;
    }
    if (e < 0) {
	e = -e;
	esign = -esign;
    }
    if (e >= 8192)
	e = 8191;
    if (e) {
	long double d = 1.0L;
	l2powm1 = 0;
	while (e) {
	    if (e & 1)
		d *= powten[l2powm1];
	    e >>= 1;
	    l2powm1++;
	}
	if (esign > 0)
	    r *= d;
	else
	    r /= d;
    }
    if (sret)
	*sret = /*unconst(s, char *) */ s;
    return r * sign;
}

#if 0
main()
{
    printf("%E", (float) x_strtold("1.4E15", NULL));
}
#endif


#define MAXEXPLD        4952	/* this includes subnormal numbers */
static int is_nan = 0;
static char decimal = '.';
static long double pten[] = {
    1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, 1e64L, 1e128L, 1e256L,
    1e512L, 1e1024L, 1e2048L, 1e4096L
};

static long double ptenneg[] = {
    1e-1L, 1e-2L, 1e-4L, 1e-8L, 1e-16L, 1e-32L, 1e-64L, 1e-128L, 1e-256L,
    1e-512L, 1e-1024L, 1e-2048L, 1e-4096L
};

static inline char tochar(int n)
{
    if (n >= 9)
	return '9';
    if (n <= 0)
	return '0';
    return n + '0';
}

static inline int todigit(char c)
{
    if (c <= '0')
	return 0;
    if (c >= '9')
	return 9;
    return c - '0';
}

#define	LONGINT		0x01	/* long integer */
#define	LONGDBL		0x02	/* long double */
#define	SHORTINT	0x04	/* short integer */
#define	ALT		0x08	/* alternate form */
#define	LADJUST		0x10	/* left adjustment */
#define	ZEROPAD		0x20	/* zero (as opposed to blank) pad */
#define	HEXPREFIX	0x40	/* add 0x or 0X prefix */

#define MAXP 4096
#define NP   12
#define P    (4294967296.0L * 4294967296.0L * 2.0L)	/* 2^65 */
static long double INVPREC = P;
static long double PREC = 1.0L / P;
#undef P
/*
 * Defining FAST_LDOUBLE_CONVERSION results in a little bit faster
 * version, which might be less accurate (about 1 bit) for long
 * double. For 'normal' double it doesn't matter.
 */
/* #define FAST_LDOUBLE_CONVERSION */
#if 1
#define modfl mymodfl
inline long double m_floor(long double x)
{
    register long double __value;
    volatile unsigned short int __cw, __cwtmp;

    asm volatile ("fnstcw %0":"=m" (__cw));
    __cwtmp = (__cw & 0xf3ff) | 0x0400;	/* rounding down */
    asm volatile ("fldcw %0"::"m" (__cwtmp));
    asm volatile ("frndint":"=t" (__value):"0"(x));
    asm volatile ("fldcw %0"::"m" (__cw));

    return __value;

}

static inline long double mymodfl(long double x, long double *pint)
{
    /*int p=(int) x; */
    long double p = m_floor(x);
    long double frac = x - p;
    if (x < 0)
	p = p + 1, frac = frac - 1;
    *pint = p;
    return frac;
}
#endif
static char *exponentl(char *p, int expv, unsigned char fmtch)
{
    char *t;
    char expbuf[MAXEXPLD];

    *p++ = fmtch;
    if (expv < 0) {
	expv = -expv;
	*p++ = '-';
    } else
	*p++ = '+';
    t = expbuf + MAXEXPLD;
    if (expv > 9) {
	do {
	    *--t = tochar(expv % 10);
	}
	while ((expv /= 10) > 9);
	*--t = tochar(expv);
	for (; t < expbuf + MAXEXPLD; *p++ = *t++);
    } else {
	*p++ = '0';
	*p++ = tochar(expv);
    }
    return p;
}

static int isspeciall(long double d, char *bufp)
{
    struct IEEExp {
	unsigned manl:32;
	unsigned manh:32;
	unsigned exp:15;
	unsigned sign:1;
    } *ip = (struct IEEExp *) &d;

    is_nan = 0;			/* don't assume the static is 0 (emacs) */
    if (ip->exp != 0x7fff)
	return (0);
    if ((ip->manh & 0x7fffffff) || ip->manl) {
	strcpy(bufp, "NaN");
	is_nan = ip->sign ? -1 : 1;	/* kludge: we don't need the sign,  it's not nice
					   but it should work */
    } else
	(void) strcpy(bufp, "Inf");
    return (3);
}

static char *my_roundl(long double fract, int *expv, char *start,
		       char *end, char ch, char *signp)
{
    long double tmp;

    if (fract) {
	if (fract == 0.5L) {
	    char *e = end;
	    if (*e == '.')
		e--;
	    if (*e == '0' || *e == '2' || *e == '4' || *e == '6'
		|| *e == '8') {
		tmp = 3.0;
		goto start;
	    }
	}
	(void) modfl(fract * 10.0L, &tmp);
    } else
	tmp = todigit(ch);
  start:
    if (tmp > 4)
	for (;; --end) {
	    if (*end == decimal)
		--end;
	    if (++*end <= '9')
		break;
	    *end = '0';
	    if (end == start) {
		if (expv) {	/* e/E; increment exponent */
		    *end = '1';
		    ++*expv;
		} else {	/* f; add extra digit */
		    *--end = '1';
		    --start;
		}
		break;
	    }
	}
    /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
    else if (*signp == '-')
	for (;; --end) {
	    if (*end == decimal)
		--end;
	    if (*end != '0')
		break;
	    if (end == start)
		*signp = 0;
	}
    return start;
}


static int
cvtl(long double number, int prec, int flags, char *signp,
     unsigned char fmtch, char *startp, char *endp)
{
    char *p, *t;
    long double fract = 0;
    int dotrim, expcnt, gformat;
    int doextradps = 0;		/* Do extra decimal places if the precision needs it */
    int doingzero = 0;		/* We're displaying 0.0 */
    long double integer, tmp;

    if ((expcnt = isspeciall(number, startp)))
	return (expcnt);

    dotrim = expcnt = gformat = 0;
    /* fract = modfl(number, &integer); */
    integer = number;

    /* get an extra slot for rounding. */
    t = ++startp;

    p = endp - 1;
    if (integer) {
	int i, lp = NP, pt = MAXP;
#ifndef FAST_LDOUBLE_CONVERSION
	long double oint = integer, dd = 1.0L;
#endif
	if (integer > INVPREC) {
	    integer *= PREC;
	    while (lp >= 0) {
		if (integer >= pten[lp]) {
		    expcnt += pt;
		    integer *= ptenneg[lp];
#ifndef FAST_LDOUBLE_CONVERSION
		    dd *= pten[lp];
#endif
		}
		pt >>= 1;
		lp--;
	    }
#ifndef FAST_LDOUBLE_CONVERSION
	    integer = oint / dd;
#else
	    integer *= INVPREC;
#endif
	}
	/*
	 * Do we really need this ?
	 */
	for (i = 0; i < expcnt; i++)
	    *p-- = '0';
    }
    number = integer;
    fract = modfl(number, &integer);
    /* If integer is zero then we need to look at where the sig figs are */
    if (integer < 1) {
	/* If fract is zero the zero before the decimal point is a sig fig */
	if (fract == 0.0)
	    doingzero = 1;
	/* If fract is non-zero all sig figs are in fractional part */
	else
	    doextradps = 1;
    }
    /*
     * get integer portion of number; put into the end of the buffer; the
     * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
     */
    for (; integer; ++expcnt) {
	tmp = modfl(integer * 0.1L, &integer);
	*p-- = tochar((int) ((tmp + .01L) * 10));
    }
    switch (fmtch) {
    case 'f':
	/* reverse integer into beginning of buffer */
	if (expcnt)
	    for (; ++p < endp; *t++ = *p);
	else
	    *t++ = '0';
	/*
	 * if precision required or alternate flag set, add in a
	 * decimal point.
	 */
	if (prec || flags & ALT)
	    *t++ = decimal;
	/* if requires more precision and some fraction left */
	if (fract) {
	    if (prec)
		do {
		    fract = modfl(fract * 10.0L, &tmp);
		    *t++ = tochar((int) tmp);
		}
		while (--prec && fract);
	    if (fract)
		startp = my_roundl(fract, (int *) NULL, startp,
				   t - 1, (char) 0, signp);
	}
	for (; prec--; *t++ = '0');
	break;
    case 'e':
    case 'E':
      eformat:
	if (expcnt) {
	    *t++ = *++p;
	    if (prec || flags & ALT)
		*t++ = decimal;
	    /* if requires more precision and some integer left */
	    for (; prec && ++p < endp; --prec)
		*t++ = *p;
	    /*
	     * if done precision and more of the integer component,
	     * round using it; adjust fract so we don't re-round
	     * later.
	     */
	    if (!prec && ++p < endp) {
		fract = 0;
		startp = my_roundl((long double) 0.0L, &expcnt,
				   startp, t - 1, *p, signp);
	    }
	    /* adjust expcnt for digit in front of decimal */
	    --expcnt;
	}
	/* until first fractional digit, decrement exponent */
	else if (fract) {
	    int lp = NP, pt = MAXP;
#ifndef FAST_LDOUBLE_CONVERSION
	    long double ofract = fract, dd = 1.0L;
#endif
	    expcnt = -1;
	    if (fract < PREC) {
		fract *= INVPREC;
		while (lp >= 0) {
		    if (fract <= ptenneg[lp]) {
			expcnt -= pt;
			fract *= pten[lp];
#ifndef FAST_LDOUBLE_CONVERSION
			dd *= pten[lp];
#endif
		    }
		    pt >>= 1;
		    lp--;
		}
#ifndef FAST_LDOUBLE_CONVERSION
		fract = ofract * dd;
#else
		fract *= PREC;
#endif
	    }
	    /* adjust expcnt for digit in front of decimal */
	    for ( /* expcnt = -1 */ ;; --expcnt) {
		fract = modfl(fract * 10.0L, &tmp);
		if (tmp)
		    break;
	    }
	    *t++ = tochar((int) tmp);
	    if (prec || flags & ALT)
		*t++ = decimal;
	} else {
	    *t++ = '0';
	    if (prec || flags & ALT)
		*t++ = decimal;
	}
	/* if requires more precision and some fraction left */
	if (fract) {
	    if (prec)
		do {
		    fract = modfl(fract * 10.0L, &tmp);
		    *t++ = tochar((int) tmp);
		}
		while (--prec && fract);
	    if (fract)
		startp =
		    my_roundl(fract, &expcnt, startp, t - 1, (char) 0,
			      signp);
	}
	/* if requires more precision */
	for (; prec--; *t++ = '0');

	/* unless alternate flag, trim any g/G format trailing 0's */
	if (gformat && !(flags & ALT)) {
	    while (t > startp && *--t == '0');
	    if (*t == decimal)
		--t;
	    ++t;
	}
	t = exponentl(t, expcnt, fmtch);
	break;
    case 'g':
    case 'G':
	if (prec) {
	    /* If doing zero and precision is greater than 0 count the
	     * 0 before the decimal place */
	    if (doingzero)
		--prec;
	} else {
	    /* a precision of 0 is treated as precision of 1 unless doing zero */
	    if (!doingzero)
		++prec;
	}
	/*
	 * ``The style used depends on the value converted; style e
	 * will be used only if the exponent resulting from the
	 * conversion is less than -4 or greater than the precision.''
	 *  -- ANSI X3J11
	 */
	if (expcnt > prec || (!expcnt && fract && fract < .0001)) {
	    /*
	     * g/G format counts "significant digits, not digits of
	     * precision; for the e/E format, this just causes an
	     * off-by-one problem, i.e. g/G considers the digit
	     * before the decimal point significant and e/E doesn't
	     * count it as precision.
	     */
	    --prec;
	    fmtch -= 2;		/* G->E, g->e */
	    gformat = 1;
	    goto eformat;
	}
	/*
	 * reverse integer into beginning of buffer,
	 * note, decrement precision
	 */
	if (expcnt)
	    for (; ++p < endp; *t++ = *p, --prec);
	else
	    *t++ = '0';
	/*
	 * if precision required or alternate flag set, add in a
	 * decimal point.  If no digits yet, add in leading 0.
	 */
	if (prec || flags & ALT) {
	    dotrim = 1;
	    *t++ = decimal;
	} else
	    dotrim = 0;
	/* if requires more precision and some fraction left */
	while (prec && fract) {
	    fract = modfl(fract * 10.0L, &tmp);
	    *t++ = tochar((int) tmp);
	    /* If we're not adding 0s
	     * or we are but they're sig figs:
	     * decrement the precision */
	    if ((doextradps != 1) || ((int) tmp != 0)) {
		doextradps = 0;
		prec--;
	    }
	}
	if (fract)
	    startp =
		my_roundl(fract, (int *) NULL, startp, t - 1, (char) 0,
			  signp);
	/* alternate format, adds 0's for precision, else trim 0's */
	if (flags & ALT)
	    for (; prec--; *t++ = '0');
	else if (dotrim) {
	    while (t > startp && *--t == '0');
	    if (*t != decimal)
		++t;
	}
    }
    return t - startp;
}

#if 0
main()
{
    static char buf[4096];
    int i;
    cvtl(0.00000000000000000005, 4, 0, &softsign, 'G', buf,
	 buf + sizeof(buf) - 1);
    printf("%s\n", buf + 1);
    printf("%.30LG\n", (long double) 234236723234234231235324.47239L);
}

#endif
void x_ldout(long double param, int prec, xio_file stream)
{
    static char buf[4095];
    char softsign = 0;
    int l;
    if (param < 0)
	xio_putc('-', stream), param = -param;
    l = cvtl(param, prec, 0, &softsign, 'G', buf, buf + sizeof(buf));
    /*printf("a:%s %i\n",buf+1, prec); */
    buf[l + 2] = 0;
    l = strlen(buf + 1);
    if (buf[l] == '.')
	buf[l] = 0;
    /*printf("b:%s %i\n",buf+1, prec); */
    xio_puts(buf + 1, stream);
}
#endif