mirror of
https://gitlab.com/80486DX2-66/gists
synced 2024-12-27 05:40:17 +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
|
* This was an experiment to learn about how libc turn floating point strings
|
||||||
* into actual values.
|
* 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
|
* Author: Intel A80486DX2-66
|
||||||
* License: Creative Commons Zero 1.0 Universal
|
* License: Creative Commons Zero 1.0 Universal
|
||||||
*/
|
*/
|
||||||
@ -68,7 +94,7 @@ static long long scanexp(FILE *f, int pok)
|
|||||||
int x;
|
int x;
|
||||||
long long y;
|
long long y;
|
||||||
int neg = 0;
|
int neg = 0;
|
||||||
|
|
||||||
c = shgetc(f);
|
c = shgetc(f);
|
||||||
if (c=='+' || c=='-') {
|
if (c=='+' || c=='-') {
|
||||||
neg = (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;
|
x[k] = x[k]/p10 + carry;
|
||||||
carry = 1000000000/p10 * tmp;
|
carry = 1000000000/p10 * tmp;
|
||||||
if (k==a && !x[k]) {
|
if (k==a && !x[k]) {
|
||||||
a = (a+1 & MASK);
|
a = ((a+1) & MASK);
|
||||||
rp -= 9;
|
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])) {
|
while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
|
||||||
uint32_t carry = 0;
|
uint32_t carry = 0;
|
||||||
e2 -= 29;
|
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;
|
uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
|
||||||
if (tmp > 1000000000) {
|
if (tmp > 1000000000) {
|
||||||
carry = 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;
|
carry = 0;
|
||||||
x[k] = tmp;
|
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 (k==a) break;
|
||||||
}
|
}
|
||||||
if (carry) {
|
if (carry) {
|
||||||
rp += 9;
|
rp += 9;
|
||||||
a = (a-1 & MASK);
|
a = ((a-1) & MASK);
|
||||||
if (a == z) {
|
if (a == z) {
|
||||||
z = (z-1 & MASK);
|
z = (z-1) & MASK;
|
||||||
x[z-1 & MASK] |= x[z];
|
x[(z-1) & MASK] |= x[z];
|
||||||
}
|
}
|
||||||
x[a] = carry;
|
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;
|
uint32_t carry = 0;
|
||||||
int sh = 1;
|
int sh = 1;
|
||||||
for (i=0; i<LD_B1B_DIG; i++) {
|
for (i=0; i<LD_B1B_DIG; i++) {
|
||||||
k = (a+i & MASK);
|
k = ((a+i) & MASK);
|
||||||
if (k == z || x[k] < th[i]) {
|
if (k == z || x[k] < th[i]) {
|
||||||
i=LD_B1B_DIG;
|
i=LD_B1B_DIG;
|
||||||
break;
|
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;
|
if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
|
||||||
/* FIXME: find a way to compute optimal sh */
|
/* FIXME: find a way to compute optimal sh */
|
||||||
if (rp > 9+9*LD_B1B_DIG) sh = 9;
|
if (rp > 9+9*LD_B1B_DIG) sh = 9;
|
||||||
e2 += sh;
|
e2 += sh;
|
||||||
for (k=a; k!=z; k=(k+1 & MASK)) {
|
for (k=a; k!=z; k=(k+1) & MASK) {
|
||||||
uint32_t tmp = x[k] & (1<<sh)-1;
|
uint32_t tmp = x[k] & ((1<<sh)-1);
|
||||||
x[k] = (x[k]>>sh) + carry;
|
x[k] = (x[k]>>sh) + carry;
|
||||||
carry = (1000000000>>sh) * tmp;
|
carry = (1000000000>>sh) * tmp;
|
||||||
if (k==a && !x[k]) {
|
if (k==a && !x[k]) {
|
||||||
a = (a+1 & MASK);
|
a = (a+1) & MASK;
|
||||||
i--;
|
i--;
|
||||||
rp -= 9;
|
rp -= 9;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (carry) {
|
if (carry) {
|
||||||
if ((z+1 & MASK) != a) {
|
if (((z+1) & MASK) != a) {
|
||||||
x[z] = carry;
|
x[z] = carry;
|
||||||
z = (z+1 & MASK);
|
z = (z+1) & MASK;
|
||||||
} else x[z-1 & MASK] |= 1;
|
} else x[(z-1) & MASK] |= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Assemble desired bits into floating point variable */
|
/* Assemble desired bits into floating point variable */
|
||||||
for (y=i=0; i<LD_B1B_DIG; i++) {
|
for (y=i=0; i<LD_B1B_DIG; i++) {
|
||||||
if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
|
if (((a+i) & MASK)==z) x[(z=((z+1) & MASK))-1] = 0;
|
||||||
y = 1000000000.0L * y + x[a+i & MASK];
|
y = 1000000000.0L * y + x[(a+i) & MASK];
|
||||||
}
|
}
|
||||||
|
|
||||||
y *= sign;
|
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 */
|
/* Process tail of decimal input so it can affect rounding */
|
||||||
if ((a+i & MASK) != z) {
|
if (((a+i) & MASK) != z) {
|
||||||
uint32_t t = x[a+i & MASK];
|
uint32_t t = x[(a+i) & MASK];
|
||||||
if (t < 500000000 && (t || (a+i+1 & MASK) != z))
|
if (t < 500000000 && (t || ((a+i+1) & MASK) != z))
|
||||||
frac += 0.25*sign;
|
frac += 0.25*sign;
|
||||||
else if (t > 500000000)
|
else if (t > 500000000)
|
||||||
frac += 0.75*sign;
|
frac += 0.75*sign;
|
||||||
else if (t == 500000000) {
|
else if (t == 500000000) {
|
||||||
if ((a+i+1 & MASK) == z)
|
if (((a+i+1) & MASK) == z)
|
||||||
frac += 0.5*sign;
|
frac += 0.5*sign;
|
||||||
else
|
else
|
||||||
frac += 0.75*sign;
|
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 += frac;
|
||||||
y -= bias;
|
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 (fabsl(y) >= 2/LDBL_EPSILON) {
|
||||||
if (denormal && bits==LDBL_MANT_DIG+e2-emin)
|
if (denormal && bits==LDBL_MANT_DIG+e2-emin)
|
||||||
denormal = 0;
|
denormal = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user