diff --git a/c-programming/experiments/floatscan-experiment.c b/c-programming/experiments/floatscan-experiment.c index 3e65af7..0e433cc 100644 --- a/c-programming/experiments/floatscan-experiment.c +++ b/c-programming/experiments/floatscan-experiment.c @@ -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] 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 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) + 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 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;