1 // Code ported from musl libc 8f12c4e110acb3bbbdc8abfb3a552c3ced718039
2 // and then modified to use softfloat and to assume f128 for everything
3 
4 #include "parse_f128.h"
5 #include "softfloat.h"
6 #include "zigendian.h"
7 #include <stddef.h>
8 #include <sys/types.h>
9 #include <errno.h>
10 #include <limits.h>
11 #include <string.h>
12 #include <math.h>
13 
14 #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
15 #define shlim(f, lim) __shlim((f), (lim))
16 #define shgetc(f) (((f)->rpos != (f)->shend) ? *(f)->rpos++ : __shgetc(f))
17 #define shunget(f) ((f)->shlim>=0 ? (void)(f)->rpos-- : (void)0)
18 
19 #define sh_fromstring(f, s) \
20     ((f)->buf = (f)->rpos = (void *)(s), (f)->rend = (void*)-1)
21 
22 #define LD_B1B_DIG 4
23 #define LD_B1B_MAX 10384593, 717069655, 257060992, 658440191
24 #define KMAX 2048
25 
26 #define MASK (KMAX-1)
27 
28 #define CONCAT2(x,y) x ## y
29 #define CONCAT(x,y) CONCAT2(x,y)
30 
31 #define F_PERM 1
32 #define F_NORD 4
33 #define F_NOWR 8
34 #define F_EOF 16
35 #define F_ERR 32
36 #define F_SVB 64
37 #define F_APP 128
38 
39 #define EOF (-1)
40 
41 #define LDBL_MANT_DIG 113
42 #define LDBL_MIN_EXP (-16381)
43 #define LDBL_MAX_EXP 16384
44 
45 #define LDBL_DIG 33
46 #define LDBL_MIN_10_EXP (-4931)
47 #define LDBL_MAX_10_EXP 4932
48 
49 #define DECIMAL_DIG 36
50 
51 #if defined(ZIG_BYTE_ORDER) && ZIG_BYTE_ORDER == ZIG_LITTLE_ENDIAN
52 union ldshape {
53     float128_t f;
54     struct {
55         uint64_t lo;
56         uint32_t mid;
57         uint16_t top;
58         uint16_t se;
59     } i;
60     struct {
61         uint64_t lo;
62         uint64_t hi;
63     } i2;
64 };
65 #elif defined(ZIG_BYTE_ORDER) && ZIG_BYTE_ORDER == ZIG_BIG_ENDIAN
66 union ldshape {
67     float128_t f;
68     struct {
69         uint16_t se;
70         uint16_t top;
71         uint32_t mid;
72         uint64_t lo;
73     } i;
74     struct {
75         uint64_t hi;
76         uint64_t lo;
77     } i2;
78 };
79 #else
80 #error Unsupported endian
81 #endif
82 
83 struct MuslFILE {
84     unsigned flags;
85     unsigned char *rpos, *rend;
86     int (*close)(struct MuslFILE *);
87     unsigned char *wend, *wpos;
88     unsigned char *mustbezero_1;
89     unsigned char *wbase;
90     size_t (*read)(struct MuslFILE *, unsigned char *, size_t);
91     size_t (*write)(struct MuslFILE *, const unsigned char *, size_t);
92     off_t (*seek)(struct MuslFILE *, off_t, int);
93     unsigned char *buf;
94     size_t buf_size;
95     struct MuslFILE *prev, *next;
96     int fd;
97     int pipe_pid;
98     long lockcount;
99     int mode;
100     volatile int lock;
101     int lbf;
102     void *cookie;
103     off_t off;
104     char *getln_buf;
105     void *mustbezero_2;
106     unsigned char *shend;
107     off_t shlim, shcnt;
108     struct MuslFILE *prev_locked, *next_locked;
109     struct __locale_struct *locale;
110 };
111 
__shlim(struct MuslFILE * f,off_t lim)112 static void __shlim(struct MuslFILE *f, off_t lim)
113 {
114     f->shlim = lim;
115     f->shcnt = f->buf - f->rpos;
116     /* If lim is nonzero, rend must be a valid pointer. */
117     if (lim && f->rend - f->rpos > lim)
118         f->shend = f->rpos + lim;
119     else
120         f->shend = f->rend;
121 }
122 
__toread(struct MuslFILE * f)123 static int __toread(struct MuslFILE *f)
124 {
125     f->mode |= f->mode-1;
126     if (f->wpos != f->wbase) f->write(f, 0, 0);
127     f->wpos = f->wbase = f->wend = 0;
128     if (f->flags & F_NORD) {
129         f->flags |= F_ERR;
130         return EOF;
131     }
132     f->rpos = f->rend = f->buf + f->buf_size;
133     return (f->flags & F_EOF) ? EOF : 0;
134 }
135 
__uflow(struct MuslFILE * f)136 static int __uflow(struct MuslFILE *f)
137 {
138     unsigned char c;
139     if (!__toread(f) && f->read(f, &c, 1)==1) return c;
140     return EOF;
141 }
142 
__shgetc(struct MuslFILE * f)143 static int __shgetc(struct MuslFILE *f)
144 {
145     int c;
146     off_t cnt = shcnt(f);
147     if ((f->shlim && cnt >= f->shlim) || (c=__uflow(f)) < 0) {
148         f->shcnt = f->buf - f->rpos + cnt;
149         f->shend = f->rpos;
150         f->shlim = -1;
151         return EOF;
152     }
153     cnt++;
154     if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
155         f->shend = f->rpos + (f->shlim - cnt);
156     else
157         f->shend = f->rend;
158     f->shcnt = f->buf - f->rpos + cnt;
159     if (f->rpos[-1] != c) f->rpos[-1] = c;
160     return c;
161 }
162 
scanexp(struct MuslFILE * f,int pok)163 static long long scanexp(struct MuslFILE *f, int pok)
164 {
165     int c;
166     int x;
167     long long y;
168     int neg = 0;
169 
170     c = shgetc(f);
171     if (c=='+' || c=='-') {
172         neg = (c=='-');
173         c = shgetc(f);
174         if (c-'0'>=10U && pok) shunget(f);
175     }
176     if (c-'0'>=10U && c!='_') {
177         shunget(f);
178         return LLONG_MIN;
179     }
180     for (x=0; ; c = shgetc(f)) {
181         if (c=='_') {
182             continue;
183         } else if (c-'0'<10U && x<INT_MAX/10) {
184             x = 10*x + c-'0';
185         } else {
186             break;
187         }
188     }
189     for (y=x; ; c = shgetc(f)) {
190         if (c=='_') {
191             continue;
192         } else if (c-'0'<10U && y<LLONG_MAX/100) {
193             y = 10*y + c-'0';
194         } else {
195             break;
196         }
197     }
198     for (; c-'0'<10U || c=='_'; c = shgetc(f));
199     shunget(f);
200     return neg ? -y : y;
201 }
202 
copysignf128(float128_t x,float128_t y)203 static float128_t copysignf128(float128_t x, float128_t y)
204 {
205     union ldshape ux = {x}, uy = {y};
206     ux.i.se &= 0x7fff;
207     ux.i.se |= uy.i.se & 0x8000;
208     return ux.f;
209 }
210 
mul_eq_f128_float(float128_t * x,float op_float)211 static void mul_eq_f128_float(float128_t *x, float op_float) {
212     //x *= 0x1p120f;
213     float32_t op_f32;
214     memcpy(&op_f32, &op_float, sizeof(float));
215     float128_t op_f128;
216     f32_to_f128M(op_f32, &op_f128);
217     float128_t new_value;
218     f128M_mul(x, &op_f128, &new_value);
219     *x = new_value;
220 }
221 
dbl_to_f128(double x)222 static float128_t dbl_to_f128(double x) {
223     float64_t x_f64;
224     memcpy(&x_f64, &x, sizeof(double));
225     float128_t result;
226     f64_to_f128M(x_f64, &result);
227     return result;
228 }
229 
fmodf128(float128_t x,float128_t y)230 static float128_t fmodf128(float128_t x, float128_t y)
231 {
232     union ldshape ux = {x}, uy = {y};
233     int ex = ux.i.se & 0x7fff;
234     int ey = uy.i.se & 0x7fff;
235     int sx = ux.i.se & 0x8000;
236 
237     float128_t zero;
238     ui32_to_f128M(0, &zero);
239     // if (y == 0 || isnan(y) || ex == 0x7fff)
240     if (f128M_eq(&y, &zero) || f128M_isSignalingNaN(&y) || ex == 0x7fff) {
241         //return (x*y)/(x*y);
242         float128_t x_times_y;
243         f128M_mul(&x, &y, &x_times_y);
244         float128_t result;
245         f128M_div(&x_times_y, &x_times_y, &result);
246         return result;
247     }
248     ux.i.se = ex;
249     uy.i.se = ey;
250     //if (ux.f <= uy.f) {
251     if (f128M_le(&ux.f, &uy.f)) {
252         //if (ux.f == uy.f) {
253         if (f128M_eq(&ux.f, &uy.f)) {
254             //return 0*x;
255             float128_t result;
256             f128M_mul(&zero, &x, &result);
257             return result;
258         }
259         return x;
260     }
261 
262     /* normalize x and y */
263     if (!ex) {
264         //ux.f *= 0x1p120f;
265         mul_eq_f128_float(&ux.f, 0x1p120f);
266 
267         ex = ux.i.se - 120;
268     }
269     if (!ey) {
270         //uy.f *= 0x1p120f;
271         mul_eq_f128_float(&uy.f, 0x1p120f);
272 
273         ey = uy.i.se - 120;
274     }
275 
276     /* x mod y */
277     uint64_t hi, lo, xhi, xlo, yhi, ylo;
278     xhi = (ux.i2.hi & -1ULL>>16) | 1ULL<<48;
279     yhi = (uy.i2.hi & -1ULL>>16) | 1ULL<<48;
280     xlo = ux.i2.lo;
281     ylo = uy.i2.lo;
282     for (; ex > ey; ex--) {
283         hi = xhi - yhi;
284         lo = xlo - ylo;
285         if (xlo < ylo)
286             hi -= 1;
287         if (hi >> 63 == 0) {
288             if ((hi|lo) == 0) {
289                 //return 0*x;
290                 float128_t result;
291                 f128M_mul(&zero, &x, &result);
292                 return result;
293             }
294             xhi = 2*hi + (lo>>63);
295             xlo = 2*lo;
296         } else {
297             xhi = 2*xhi + (xlo>>63);
298             xlo = 2*xlo;
299         }
300     }
301     hi = xhi - yhi;
302     lo = xlo - ylo;
303     if (xlo < ylo)
304         hi -= 1;
305     if (hi >> 63 == 0) {
306         if ((hi|lo) == 0) {
307             //return 0*x;
308             float128_t result;
309             f128M_mul(&zero, &x, &result);
310             return result;
311         }
312         xhi = hi;
313         xlo = lo;
314     }
315     for (; xhi >> 48 == 0; xhi = 2*xhi + (xlo>>63), xlo = 2*xlo, ex--);
316     ux.i2.hi = xhi;
317     ux.i2.lo = xlo;
318 
319     /* scale result */
320     if (ex <= 0) {
321         ux.i.se = (ex+120)|sx;
322         //ux.f *= 0x1p-120f;
323         mul_eq_f128_float(&ux.f, 0x1p-120f);
324     } else
325         ux.i.se = ex|sx;
326     return ux.f;
327 }
328 
int_mul_f128_cast_u32(int sign,uint32_t x0)329 static float128_t int_mul_f128_cast_u32(int sign, uint32_t x0) {
330     float128_t x0_f128;
331     ui32_to_f128M(x0, &x0_f128);
332     float128_t sign_f128;
333     i32_to_f128M(sign, &sign_f128);
334     float128_t result;
335     f128M_mul(&sign_f128, &x0_f128, &result);
336     return result;
337 }
338 
triple_divide(int sign,uint32_t x0,int p10s)339 static float128_t triple_divide(int sign, uint32_t x0, int p10s) {
340     float128_t part1 = int_mul_f128_cast_u32(sign, x0);
341     float128_t p10s_f128;
342     i32_to_f128M(p10s, &p10s_f128);
343     float128_t result;
344     f128M_div(&part1, &p10s_f128, &result);
345     return result;
346 }
347 
triple_multiply(int sign,uint32_t x0,int p10s)348 static float128_t triple_multiply(int sign, uint32_t x0, int p10s) {
349     float128_t part1 = int_mul_f128_cast_u32(sign, x0);
350     float128_t p10s_f128;
351     i32_to_f128M(p10s, &p10s_f128);
352     float128_t result;
353     f128M_mul(&part1, &p10s_f128, &result);
354     return result;
355 }
356 
mul_eq_f128_int(float128_t * y,int sign)357 static void mul_eq_f128_int(float128_t *y, int sign) {
358     float128_t sign_f128;
359     i32_to_f128M(sign, &sign_f128);
360     float128_t new_value;
361     f128M_mul(y, &sign_f128, &new_value);
362     *y = new_value;
363 }
364 
make_f128(uint64_t hi,uint64_t lo)365 static float128_t make_f128(uint64_t hi, uint64_t lo) {
366     union ldshape ux;
367     ux.i2.hi = hi;
368     ux.i2.lo = lo;
369     return ux.f;
370 }
371 
mul_eq_f128_f128(float128_t * a,float128_t b)372 static void mul_eq_f128_f128(float128_t *a, float128_t b) {
373     float128_t new_value;
374     f128M_mul(a, &b, &new_value);
375     *a = new_value;
376 }
377 
add_eq_f128_dbl(float128_t * a,double b)378 static void add_eq_f128_dbl(float128_t *a, double b) {
379     float64_t b_f64;
380     memcpy(&b_f64, &b, sizeof(double));
381 
382     float128_t b_f128;
383     f64_to_f128M(b_f64, &b_f128);
384 
385     float128_t new_value;
386     f128M_add(a, &b_f128, &new_value);
387     *a = new_value;
388 }
389 
scalbnf128(float128_t x,int n)390 static float128_t scalbnf128(float128_t x, int n)
391 {
392     union ldshape u;
393 
394     if (n > 16383) {
395         //x *= 0x1p16383q;
396         mul_eq_f128_f128(&x, make_f128(0x7ffe000000000000, 0x0000000000000000));
397         n -= 16383;
398         if (n > 16383) {
399             //x *= 0x1p16383q;
400             mul_eq_f128_f128(&x, make_f128(0x7ffe000000000000, 0x0000000000000000));
401             n -= 16383;
402             if (n > 16383)
403                 n = 16383;
404         }
405     } else if (n < -16382) {
406         //x *= 0x1p-16382q * 0x1p113q;
407         {
408             float128_t mul_result;
409             float128_t a = make_f128(0x0001000000000000, 0x0000000000000000);
410             float128_t b = make_f128(0x4070000000000000, 0x0000000000000000);
411             f128M_mul(&a, &b, &mul_result);
412             mul_eq_f128_f128(&x, mul_result);
413         }
414         n += 16382 - 113;
415         if (n < -16382) {
416             //x *= 0x1p-16382q * 0x1p113q;
417             {
418                 float128_t mul_result;
419                 float128_t a = make_f128(0x0001000000000000, 0x0000000000000000);
420                 float128_t b = make_f128(0x4070000000000000, 0x0000000000000000);
421                 f128M_mul(&a, &b, &mul_result);
422                 mul_eq_f128_f128(&x, mul_result);
423             }
424             n += 16382 - 113;
425             if (n < -16382)
426                 n = -16382;
427         }
428     }
429     //u.f = 1.0;
430     ui32_to_f128M(1, &u.f);
431     u.i.se = 0x3fff + n;
432     mul_eq_f128_f128(&x, u.f);
433     return x;
434 }
435 
fabsf128(float128_t x)436 static float128_t fabsf128(float128_t x)
437 {
438     union ldshape u = {x};
439 
440     u.i.se &= 0x7fff;
441     return u.f;
442 }
443 
decfloat(struct MuslFILE * f,int c,int bits,int emin,int sign,int pok)444 static float128_t decfloat(struct MuslFILE *f, int c, int bits, int emin, int sign, int pok)
445 {
446     uint32_t x[KMAX];
447     static const uint32_t th[] = { LD_B1B_MAX };
448     int i, j, k, a, z;
449     long long lrp=0, dc=0;
450     long long e10=0;
451     int lnz = 0;
452     int gotdig = 0, gotrad = 0;
453     int rp;
454     int e2;
455     int emax = -emin-bits+3;
456     int denormal = 0;
457     float128_t y;
458     float128_t zero;
459     ui32_to_f128M(0, &zero);
460     float128_t frac=zero;
461     float128_t bias=zero;
462     static const int p10s[] = { 10, 100, 1000, 10000,
463         100000, 1000000, 10000000, 100000000 };
464 
465     j=0;
466     k=0;
467 
468     /* Don't let leading zeros/underscores consume buffer space */
469     for (; ; c = shgetc(f)) {
470         if (c=='_') {
471             continue;
472         } else if (c=='0') {
473             gotdig=1;
474         } else {
475             break;
476         }
477     }
478 
479     if (c=='.') {
480         gotrad = 1;
481         for (c = shgetc(f); ; c = shgetc(f)) {
482             if (c == '_') {
483                 continue;
484             } else if (c=='0') {
485                 gotdig=1;
486                 lrp--;
487             } else {
488                 break;
489             }
490         }
491     }
492 
493     x[0] = 0;
494     for (; c-'0'<10U || c=='.' || c=='_'; c = shgetc(f)) {
495         if (c == '_') {
496             continue;
497         } else if (c == '.') {
498             if (gotrad) break;
499             gotrad = 1;
500             lrp = dc;
501         } else if (k < KMAX-3) {
502             dc++;
503             if (c!='0') lnz = dc;
504             if (j) x[k] = x[k]*10 + c-'0';
505             else x[k] = c-'0';
506             if (++j==9) {
507                 k++;
508                 j=0;
509             }
510             gotdig=1;
511         } else {
512             dc++;
513             if (c!='0') {
514                 lnz = (KMAX-4)*9;
515                 x[KMAX-4] |= 1;
516             }
517         }
518     }
519     if (!gotrad) lrp=dc;
520 
521     if (gotdig && (c|32)=='e') {
522         e10 = scanexp(f, pok);
523         if (e10 == LLONG_MIN) {
524             if (pok) {
525                 shunget(f);
526             } else {
527                 shlim(f, 0);
528                 return zero;
529             }
530             e10 = 0;
531         }
532         lrp += e10;
533     } else if (c>=0) {
534         shunget(f);
535     }
536     if (!gotdig) {
537         errno = EINVAL;
538         shlim(f, 0);
539         return zero;
540     }
541 
542     /* Handle zero specially to avoid nasty special cases later */
543     if (!x[0]) {
544         //return sign * 0.0;
545         return dbl_to_f128(sign * 0.0);
546     }
547 
548     /* Optimize small integers (w/no exponent) and over/under-flow */
549     if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0)) {
550         //return sign * (float128_t)x[0];
551         float128_t sign_f128;
552         i32_to_f128M(sign, &sign_f128);
553         float128_t x0_f128;
554         ui32_to_f128M(x[0], &x0_f128);
555         float128_t result;
556         f128M_mul(&sign_f128, &x0_f128, &result);
557         return result;
558     }
559     if (lrp > -emin/2) {
560         errno = ERANGE;
561         //return sign * LDBL_MAX * LDBL_MAX;
562         return zero;
563     }
564     if (lrp < emin-2*LDBL_MANT_DIG) {
565         errno = ERANGE;
566         //return sign * LDBL_MIN * LDBL_MIN;
567         return zero;
568     }
569 
570     /* Align incomplete final B1B digit */
571     if (j) {
572         for (; j<9; j++) x[k]*=10;
573         k++;
574         j=0;
575     }
576 
577     a = 0;
578     z = k;
579     e2 = 0;
580     rp = lrp;
581 
582     /* Optimize small to mid-size integers (even in exp. notation) */
583     if (lnz<9 && lnz<=rp && rp < 18) {
584         if (rp == 9) {
585             //return sign * (float128_t)(x[0]);
586             return int_mul_f128_cast_u32(sign, x[0]);
587         }
588         if (rp < 9) {
589             //return sign * (float128_t)(x[0]) / p10s[8-rp];
590             return triple_divide(sign, x[0], p10s[8-rp]);
591         }
592         int bitlim = bits-3*(int)(rp-9);
593         if (bitlim>30 || x[0]>>bitlim==0)
594             //return sign * (float128_t)(x[0]) * p10s[rp-10];
595             return triple_multiply(sign, x[0], p10s[rp-10]);
596     }
597 
598     /* Drop trailing zeros */
599     for (; !x[z-1]; z--);
600 
601     /* Align radix point to B1B digit boundary */
602     if (rp % 9) {
603         int rpm9 = rp>=0 ? rp%9 : rp%9+9;
604         int p10 = p10s[8-rpm9];
605         uint32_t carry = 0;
606         for (k=a; k!=z; k++) {
607             uint32_t tmp = x[k] % p10;
608             x[k] = x[k]/p10 + carry;
609             carry = 1000000000/p10 * tmp;
610             if (k==a && !x[k]) {
611                 a = (a+1 & MASK);
612                 rp -= 9;
613             }
614         }
615         if (carry) x[z++] = carry;
616         rp += 9-rpm9;
617     }
618 
619     /* Upscale until desired number of bits are left of radix point */
620     while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
621         uint32_t carry = 0;
622         e2 -= 29;
623         for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
624             uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
625             if (tmp > 1000000000) {
626                 carry = tmp / 1000000000;
627                 x[k] = tmp % 1000000000;
628             } else {
629                 carry = 0;
630                 x[k] = tmp;
631             }
632             if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
633             if (k==a) break;
634         }
635         if (carry) {
636             rp += 9;
637             a = (a-1 & MASK);
638             if (a == z) {
639                 z = (z-1 & MASK);
640                 x[z-1 & MASK] |= x[z];
641             }
642             x[a] = carry;
643         }
644     }
645 
646     /* Downscale until exactly number of bits are left of radix point */
647     for (;;) {
648         uint32_t carry = 0;
649         int sh = 1;
650         for (i=0; i<LD_B1B_DIG; i++) {
651             k = (a+i & MASK);
652             if (k == z || x[k] < th[i]) {
653                 i=LD_B1B_DIG;
654                 break;
655             }
656             if (x[a+i & MASK] > th[i]) break;
657         }
658         if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
659         /* FIXME: find a way to compute optimal sh */
660         if (rp > 9+9*LD_B1B_DIG) sh = 9;
661         e2 += sh;
662         for (k=a; k!=z; k=(k+1 & MASK)) {
663             uint32_t tmp = x[k] & (1<<sh)-1;
664             x[k] = (x[k]>>sh) + carry;
665             carry = (1000000000>>sh) * tmp;
666             if (k==a && !x[k]) {
667                 a = (a+1 & MASK);
668                 i--;
669                 rp -= 9;
670             }
671         }
672         if (carry) {
673             if ((z+1 & MASK) != a) {
674                 x[z] = carry;
675                 z = (z+1 & MASK);
676             } else x[z-1 & MASK] |= 1;
677         }
678     }
679 
680     /* Assemble desired bits into floating point variable */
681     for (y=zero,i=0; i<LD_B1B_DIG; i++) {
682         if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
683         //y = 1000000000.0L * y + x[a+i & MASK];
684         float128_t const_f128;
685         ui64_to_f128M(1000000000, &const_f128);
686         float128_t mul_y;
687         f128M_mul(&const_f128, &y, &mul_y);
688         float128_t x_f128;
689         ui32_to_f128M(x[a+i & MASK], &x_f128);
690         f128M_add(&mul_y, &x_f128, &y);
691     }
692 
693     //y *= sign;
694     mul_eq_f128_int(&y, sign);
695 
696     /* Limit precision for denormal results */
697     if (bits > LDBL_MANT_DIG+e2-emin) {
698         bits = LDBL_MANT_DIG+e2-emin;
699         if (bits<0) bits=0;
700         denormal = 1;
701     }
702 
703     /* Calculate bias term to force rounding, move out lower bits */
704     if (bits < LDBL_MANT_DIG) {
705         bias = copysignf128(dbl_to_f128(scalbn(1, 2*LDBL_MANT_DIG-bits-1)), y);
706         frac = fmodf128(y, dbl_to_f128(scalbn(1, LDBL_MANT_DIG-bits)));
707         //y -= frac;
708         {
709             float128_t new_value;
710             f128M_sub(&y, &frac, &new_value);
711             y = new_value;
712         }
713         //y += bias;
714         {
715             float128_t new_value;
716             f128M_add(&y, &frac, &new_value);
717             y = new_value;
718         }
719     }
720 
721     /* Process tail of decimal input so it can affect rounding */
722     if ((a+i & MASK) != z) {
723         uint32_t t = x[a+i & MASK];
724         if (t < 500000000 && (t || (a+i+1 & MASK) != z)) {
725             //frac += 0.25*sign;
726             add_eq_f128_dbl(&frac, 0.25*sign);
727         } else if (t > 500000000) {
728             //frac += 0.75*sign;
729             add_eq_f128_dbl(&frac, 0.75*sign);
730         } else if (t == 500000000) {
731             if ((a+i+1 & MASK) == z) {
732                 //frac += 0.5*sign;
733                 add_eq_f128_dbl(&frac, 0.5*sign);
734             } else {
735                 //frac += 0.75*sign;
736                 add_eq_f128_dbl(&frac, 0.75*sign);
737             }
738         }
739         //if (LDBL_MANT_DIG-bits >= 2 && !fmodf128(frac, 1))
740         if (LDBL_MANT_DIG-bits >= 2) {
741             float128_t one;
742             ui32_to_f128M(1, &one);
743             float128_t mod_result = fmodf128(frac, one);
744             if (f128M_eq(&mod_result, &zero)) {
745                 //frac++;
746                 add_eq_f128_dbl(&frac, 1.0);
747             }
748         }
749     }
750 
751     //y += frac;
752     {
753         float128_t new_value;
754         f128M_add(&y, &frac, &new_value);
755         y = new_value;
756     }
757     //y -= bias;
758     {
759         float128_t new_value;
760         f128M_sub(&y, &bias, &new_value);
761         y = new_value;
762     }
763 
764     if ((e2+LDBL_MANT_DIG & INT_MAX) > emax-5) {
765         //if (fabsf128(y) >= 0x1p113)
766         float128_t abs_y = fabsf128(y);
767         float128_t mant_f128 = make_f128(0x4070000000000000, 0x0000000000000000);
768         if (!f128M_lt(&abs_y, &mant_f128)) {
769             if (denormal && bits==LDBL_MANT_DIG+e2-emin)
770                 denormal = 0;
771             //y *= 0.5;
772             {
773                 float128_t point_5 = dbl_to_f128(0.5);
774                 float128_t new_value;
775                 f128M_mul(&y, &point_5, &new_value);
776                 y = new_value;
777             }
778 
779             e2++;
780         }
781         if (e2+LDBL_MANT_DIG>emax || (denormal && !f128M_eq(&frac, &zero)))
782             errno = ERANGE;
783     }
784 
785     return scalbnf128(y, e2);
786 }
787 
hexfloat(struct MuslFILE * f,int bits,int emin,int sign,int pok)788 static float128_t hexfloat(struct MuslFILE *f, int bits, int emin, int sign, int pok)
789 {
790     float128_t zero;
791     ui32_to_f128M(0, &zero);
792     float128_t one;
793     ui32_to_f128M(1, &one);
794     float128_t sixteen;
795     ui32_to_f128M(16, &sixteen);
796     float128_t point_5 = dbl_to_f128(0.5);
797 
798     uint32_t x = 0;
799     float128_t y = zero;
800     float128_t scale = one;
801     float128_t bias = zero;
802     int gottail = 0, gotrad = 0, gotdig = 0;
803     long long rp = 0;
804     long long dc = 0;
805     long long e2 = 0;
806     int d;
807     int c;
808 
809     c = shgetc(f);
810 
811     /* Skip leading zeros/underscores */
812     for (; c=='0' || c=='_'; c = shgetc(f)) gotdig = 1;
813 
814     if (c=='.') {
815         gotrad = 1;
816         c = shgetc(f);
817         /* Count zeros after the radix point before significand */
818         for (rp=0; ; c = shgetc(f)) {
819             if (c == '_') {
820                 continue;
821             } else if (c == '0') {
822                 gotdig = 1;
823                 rp--;
824             } else {
825                 break;
826             }
827         }
828     }
829 
830     for (; c-'0'<10U || (c|32)-'a'<6U || c=='.' || c=='_'; c = shgetc(f)) {
831         if (c=='_') {
832             continue;
833         } else if (c=='.') {
834             if (gotrad) break;
835             rp = dc;
836             gotrad = 1;
837         } else {
838             gotdig = 1;
839             if (c > '9') d = (c|32)+10-'a';
840             else d = c-'0';
841             if (dc<8) {
842                 x = x*16 + d;
843             } else if (dc < LDBL_MANT_DIG/4+1) {
844                 //y += d*(scale/=16);
845                 {
846                     float128_t divided;
847                     f128M_div(&scale, &sixteen, &divided);
848                     scale = divided;
849                     float128_t d_f128;
850                     i32_to_f128M(d, &d_f128);
851                     float128_t add_op;
852                     f128M_mul(&d_f128, &scale, &add_op);
853                     float128_t new_y;
854                     f128M_add(&y, &add_op, &new_y);
855                     y = new_y;
856                 }
857             } else if (d && !gottail) {
858                 //y += 0.5*scale;
859                 {
860                     float128_t add_op;
861                     f128M_mul(&point_5, &scale, &add_op);
862                     float128_t new_y;
863                     f128M_add(&y, &add_op, &new_y);
864                     y = new_y;
865                 }
866                 gottail = 1;
867             }
868             dc++;
869         }
870     }
871     if (!gotdig) {
872         shunget(f);
873         if (pok) {
874             shunget(f);
875             if (gotrad) shunget(f);
876         } else {
877             shlim(f, 0);
878         }
879         //return sign * 0.0;
880         return dbl_to_f128(sign * 0.0);
881     }
882     if (!gotrad) rp = dc;
883     while (dc<8) x *= 16, dc++;
884     if ((c|32)=='p') {
885         e2 = scanexp(f, pok);
886         if (e2 == LLONG_MIN) {
887             if (pok) {
888                 shunget(f);
889             } else {
890                 shlim(f, 0);
891                 return zero;
892             }
893             e2 = 0;
894         }
895     } else {
896         shunget(f);
897     }
898     e2 += 4*rp - 32;
899 
900     if (!x) {
901         //return sign * 0.0;
902         return dbl_to_f128(sign * 0.0);
903     }
904     if (e2 > -emin) {
905         errno = ERANGE;
906         //return sign * LDBL_MAX * LDBL_MAX;
907         return zero;
908     }
909     if (e2 < emin-2*LDBL_MANT_DIG) {
910         errno = ERANGE;
911         //return sign * LDBL_MIN * LDBL_MIN;
912         return zero;
913     }
914 
915     while (x < 0x80000000) {
916         //if (y>=0.5)
917         if (!f128M_lt(&y, &point_5)) {
918             x += x + 1;
919             //y += y - 1;
920             {
921                 float128_t minus_one;
922                 f128M_sub(&y, &one, &minus_one);
923                 float128_t new_y;
924                 f128M_add(&y, &minus_one, &new_y);
925                 y = new_y;
926             }
927         } else {
928             x += x;
929             //y += y;
930             {
931                 float128_t new_y;
932                 f128M_add(&y, &y, &new_y);
933                 y = new_y;
934             }
935         }
936         e2--;
937     }
938 
939     if (bits > 32+e2-emin) {
940         bits = 32+e2-emin;
941         if (bits<0) bits=0;
942     }
943 
944     if (bits < LDBL_MANT_DIG) {
945         float128_t sign_f128;
946         i32_to_f128M(sign, &sign_f128);
947         bias = copysignf128(dbl_to_f128(scalbn(1, 32+LDBL_MANT_DIG-bits-1)), sign_f128);
948     }
949 
950     //if (bits<32 && y && !(x&1)) x++, y=0;
951     if (bits<32 && !f128M_eq(&y, &zero) && !(x&1)) x++, y=zero;
952 
953     //y = bias + sign*(float128_t)x + sign*y;
954     {
955         float128_t x_f128;
956         ui32_to_f128M(x, &x_f128);
957         float128_t sign_f128;
958         i32_to_f128M(sign, &sign_f128);
959         float128_t sign_mul_x;
960         f128M_mul(&sign_f128, &x_f128, &sign_mul_x);
961         float128_t sign_mul_y;
962         f128M_mul(&sign_f128, &y, &sign_mul_y);
963         float128_t bias_op;
964         f128M_add(&bias, &sign_mul_x, &bias_op);
965         float128_t new_y;
966         f128M_add(&bias_op, &sign_mul_y, &new_y);
967         y = new_y;
968     }
969     //y -= bias;
970     {
971         float128_t new_y;
972         f128M_sub(&y, &bias, &new_y);
973         y = new_y;
974     }
975 
976     if (f128M_eq(&y, &zero)) errno = ERANGE;
977 
978     return scalbnf128(y, e2);
979 }
980 
isspace(int c)981 static int isspace(int c)
982 {
983     return c == ' ' || (unsigned)c-'\t' < 5;
984 }
985 
makeInf128()986 static inline float128_t makeInf128() {
987     union ldshape ux;
988     ux.i2.hi = 0x7fff000000000000UL;
989     ux.i2.lo = 0x0UL;
990     return ux.f;
991 }
992 
makeNaN128()993 static inline float128_t makeNaN128() {
994     uint64_t rand = 0UL;
995     union ldshape ux;
996     ux.i2.hi = 0x7fff000000000000UL | (rand & 0xffffffffffffUL);
997     ux.i2.lo = 0x0UL;
998     return ux.f;
999 }
1000 
__floatscan(struct MuslFILE * f,int prec,int pok)1001 float128_t __floatscan(struct MuslFILE *f, int prec, int pok)
1002 {
1003     int sign = 1;
1004     size_t i;
1005     int bits = LDBL_MANT_DIG;
1006     int emin = LDBL_MIN_EXP-bits;
1007     int c;
1008 
1009     while (isspace((c=shgetc(f))));
1010 
1011     if (c=='+' || c=='-') {
1012         sign -= 2*(c=='-');
1013         c = shgetc(f);
1014     }
1015 
1016     for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
1017         if (i<7) c = shgetc(f);
1018     if (i==3 || i==8 || (i>3 && pok)) {
1019         if (i!=8) {
1020             shunget(f);
1021             if (pok) for (; i>3; i--) shunget(f);
1022         }
1023         //return sign * INFINITY;
1024         float128_t sign_f128;
1025         i32_to_f128M(sign, &sign_f128);
1026         float128_t infinity_f128 = makeInf128();
1027         float128_t result;
1028         f128M_mul(&sign_f128, &infinity_f128, &result);
1029         return result;
1030     }
1031     if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
1032         if (i<2) c = shgetc(f);
1033     if (i==3) {
1034         if (shgetc(f) != '(') {
1035             shunget(f);
1036             return makeNaN128();
1037         }
1038         for (i=1; ; i++) {
1039             c = shgetc(f);
1040             if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
1041                 continue;
1042             if (c==')') return makeNaN128();
1043             shunget(f);
1044             if (!pok) {
1045                 errno = EINVAL;
1046                 shlim(f, 0);
1047                 float128_t zero;
1048                 ui32_to_f128M(0, &zero);
1049                 return zero;
1050             }
1051             while (i--) shunget(f);
1052             return makeNaN128();
1053         }
1054         return makeNaN128();
1055     }
1056 
1057     if (i) {
1058         shunget(f);
1059         errno = EINVAL;
1060         shlim(f, 0);
1061         float128_t zero;
1062         ui32_to_f128M(0, &zero);
1063         return zero;
1064     }
1065 
1066     if (c=='0') {
1067         c = shgetc(f);
1068         if ((c|32) == 'x')
1069             return hexfloat(f, bits, emin, sign, pok);
1070         shunget(f);
1071         c = '0';
1072     }
1073 
1074     return decfloat(f, c, bits, emin, sign, pok);
1075 }
1076 
parse_f128(const char * s,char ** p)1077 float128_t parse_f128(const char *s, char **p) {
1078     struct MuslFILE f;
1079     sh_fromstring(&f, s);
1080     shlim(&f, 0);
1081     float128_t y = __floatscan(&f, 2, 1);
1082     off_t cnt = shcnt(&f);
1083     if (p) *p = cnt ? (char *)s + cnt : (char *)s;
1084     return y;
1085 }
1086