mirror of
https://gitlab.com/80486DX2-66/gists
synced 2024-12-26 20:30:24 +05:30
floatscan-experiment.c: reformat the original code
This commit is contained in:
parent
4f9ceae08f
commit
f411dfc6dc
@ -2,9 +2,35 @@
|
||||
* This was an experiment to learn about how libc turn floating point strings
|
||||
* into actual values.
|
||||
*
|
||||
* The original code is from musl libc: version 1.2.4, src/internal/floatscan.c
|
||||
* The original code is licensed to and was taken from musl libc, originally
|
||||
* stored at src/internal/floatscan.c
|
||||
*
|
||||
* The added code:
|
||||
* musl libc is licensed under MIT license:
|
||||
* musl as a whole is licensed under the following standard MIT license:
|
||||
*
|
||||
* ----------------------------------------------------------------------
|
||||
* Copyright © 2005-2020 Rich Felker, et al.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* The license information of the added code and this modification:
|
||||
* Author: Intel A80486DX2-66
|
||||
* License: Creative Commons Zero 1.0 Universal
|
||||
*/
|
||||
@ -68,7 +94,7 @@ static long long scanexp(FILE *f, int pok)
|
||||
int x;
|
||||
long long y;
|
||||
int neg = 0;
|
||||
|
||||
|
||||
c = shgetc(f);
|
||||
if (c=='+' || c=='-') {
|
||||
neg = (c=='-');
|
||||
@ -214,7 +240,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
|
||||
x[k] = x[k]/p10 + carry;
|
||||
carry = 1000000000/p10 * tmp;
|
||||
if (k==a && !x[k]) {
|
||||
a = (a+1 & MASK);
|
||||
a = ((a+1) & MASK);
|
||||
rp -= 9;
|
||||
}
|
||||
}
|
||||
@ -226,7 +252,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
|
||||
while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
|
||||
uint32_t carry = 0;
|
||||
e2 -= 29;
|
||||
for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
|
||||
for (k=(z-1) & MASK; ; k=(k-1) & MASK) {
|
||||
uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
|
||||
if (tmp > 1000000000) {
|
||||
carry = tmp / 1000000000;
|
||||
@ -235,15 +261,15 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
|
||||
carry = 0;
|
||||
x[k] = tmp;
|
||||
}
|
||||
if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
|
||||
if (k==((z-1) & MASK) && k!=a && !x[k]) z = k;
|
||||
if (k==a) break;
|
||||
}
|
||||
if (carry) {
|
||||
rp += 9;
|
||||
a = (a-1 & MASK);
|
||||
a = ((a-1) & MASK);
|
||||
if (a == z) {
|
||||
z = (z-1 & MASK);
|
||||
x[z-1 & MASK] |= x[z];
|
||||
z = (z-1) & MASK;
|
||||
x[(z-1) & MASK] |= x[z];
|
||||
}
|
||||
x[a] = carry;
|
||||
}
|
||||
@ -254,39 +280,39 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
|
||||
uint32_t carry = 0;
|
||||
int sh = 1;
|
||||
for (i=0; i<LD_B1B_DIG; i++) {
|
||||
k = (a+i & MASK);
|
||||
k = ((a+i) & MASK);
|
||||
if (k == z || x[k] < th[i]) {
|
||||
i=LD_B1B_DIG;
|
||||
break;
|
||||
}
|
||||
if (x[a+i & MASK] > th[i]) break;
|
||||
if (x[(a+i) & MASK] > th[i]) break;
|
||||
}
|
||||
if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
|
||||
/* FIXME: find a way to compute optimal sh */
|
||||
if (rp > 9+9*LD_B1B_DIG) sh = 9;
|
||||
e2 += sh;
|
||||
for (k=a; k!=z; k=(k+1 & MASK)) {
|
||||
uint32_t tmp = x[k] & (1<<sh)-1;
|
||||
for (k=a; k!=z; k=(k+1) & MASK) {
|
||||
uint32_t tmp = x[k] & ((1<<sh)-1);
|
||||
x[k] = (x[k]>>sh) + carry;
|
||||
carry = (1000000000>>sh) * tmp;
|
||||
if (k==a && !x[k]) {
|
||||
a = (a+1 & MASK);
|
||||
a = (a+1) & MASK;
|
||||
i--;
|
||||
rp -= 9;
|
||||
}
|
||||
}
|
||||
if (carry) {
|
||||
if ((z+1 & MASK) != a) {
|
||||
if (((z+1) & MASK) != a) {
|
||||
x[z] = carry;
|
||||
z = (z+1 & MASK);
|
||||
} else x[z-1 & MASK] |= 1;
|
||||
z = (z+1) & MASK;
|
||||
} else x[(z-1) & MASK] |= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Assemble desired bits into floating point variable */
|
||||
for (y=i=0; i<LD_B1B_DIG; i++) {
|
||||
if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
|
||||
y = 1000000000.0L * y + x[a+i & MASK];
|
||||
if (((a+i) & MASK)==z) x[(z=((z+1) & MASK))-1] = 0;
|
||||
y = 1000000000.0L * y + x[(a+i) & MASK];
|
||||
}
|
||||
|
||||
y *= sign;
|
||||
@ -307,14 +333,14 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
|
||||
}
|
||||
|
||||
/* Process tail of decimal input so it can affect rounding */
|
||||
if ((a+i & MASK) != z) {
|
||||
uint32_t t = x[a+i & MASK];
|
||||
if (t < 500000000 && (t || (a+i+1 & MASK) != z))
|
||||
if (((a+i) & MASK) != z) {
|
||||
uint32_t t = x[(a+i) & MASK];
|
||||
if (t < 500000000 && (t || ((a+i+1) & MASK) != z))
|
||||
frac += 0.25*sign;
|
||||
else if (t > 500000000)
|
||||
frac += 0.75*sign;
|
||||
else if (t == 500000000) {
|
||||
if ((a+i+1 & MASK) == z)
|
||||
if (((a+i+1) & MASK) == z)
|
||||
frac += 0.5*sign;
|
||||
else
|
||||
frac += 0.75*sign;
|
||||
@ -326,7 +352,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
|
||||
y += frac;
|
||||
y -= bias;
|
||||
|
||||
if ((e2+LDBL_MANT_DIG & INT_MAX) > emax-5) {
|
||||
if (((e2+LDBL_MANT_DIG) & INT_MAX) > emax-5) {
|
||||
if (fabsl(y) >= 2/LDBL_EPSILON) {
|
||||
if (denormal && bits==LDBL_MANT_DIG+e2-emin)
|
||||
denormal = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user