summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2025-04-02 21:12:44 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2025-04-02 21:12:44 (GMT)
commiteb47492d3f5f7b030861e9184d51dcb8a26cb00e (patch)
tree85bf4f688caf6976c67f9a11ed465220fd94f3f1
parentcacb5f06829a2145c84b9cc82793261386c032d0 (diff)
downloadtcl-eb47492d3f5f7b030861e9184d51dcb8a26cb00e.zip
tcl-eb47492d3f5f7b030861e9184d51dcb8a26cb00e.tar.gz
tcl-eb47492d3f5f7b030861e9184d51dcb8a26cb00e.tar.bz2
Fix -Wconversion and unused parameter warnings. Backported from (but unrelated to) TIP #626
-rw-r--r--generic/tclStrToD.c168
1 files changed, 78 insertions, 90 deletions
diff --git a/generic/tclStrToD.c b/generic/tclStrToD.c
index 432d11b..8afe40c 100644
--- a/generic/tclStrToD.c
+++ b/generic/tclStrToD.c
@@ -7,7 +7,7 @@
* into strings of digits, and procedures for interconversion among
* 'double' and 'mp_int' types.
*
- * Copyright (c) 2005 by Kevin B. Kenny. All rights reserved.
+ * Copyright (c) 2005 Kevin B. Kenny. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
@@ -127,7 +127,7 @@ typedef unsigned int fpu_control_t __attribute__ ((__mode__ (__HI__)));
* Definitions of the parts of an IEEE754-format floating point number.
*/
-#define SIGN_BIT 0x80000000
+#define SIGN_BIT 0x80000000
/* Mask for the sign bit in the first word of
* a double. */
#define EXP_MASK 0x7FF00000
@@ -301,17 +301,17 @@ static const Tcl_WideUInt wuipow5[] = {
static int AccumulateDecimalDigit(unsigned, int,
Tcl_WideUInt *, mp_int *, int);
static double MakeHighPrecisionDouble(int signum,
- mp_int *significand, int nSigDigs, long exponent);
+ mp_int *significand, int nSigDigs, int exponent);
static double MakeLowPrecisionDouble(int signum,
Tcl_WideUInt significand, int nSigDigs,
- long exponent);
+ int exponent);
#ifdef IEEE_FLOATING_POINT
static double MakeNaN(int signum, Tcl_WideUInt tag);
#endif
static double RefineApproximation(double approx,
mp_int *exactSignificand, int exponent);
static void MulPow5(mp_int *, unsigned, mp_int *);
-static int NormalizeRightward(Tcl_WideUInt *);
+static int NormalizeRightward(Tcl_WideUInt *);
static int RequiredPrecision(Tcl_WideUInt);
static void DoubleToExpAndSig(double, Tcl_WideUInt *, int *,
int *);
@@ -335,7 +335,7 @@ static void CastOutPowersOf2(int *, int *, int *);
static char * ShorteningInt64Conversion(Double *, int, Tcl_WideUInt,
int, int, int, int, int, int, int, int, int,
int, int, int *, char **);
-static char * StrictInt64Conversion(Double *, int, Tcl_WideUInt,
+static char * StrictInt64Conversion(Tcl_WideUInt,
int, int, int, int, int, int,
int, int, int *, char **);
static int ShouldBankerRoundUpPowD(mp_int *, int, int);
@@ -347,7 +347,7 @@ static char * ShorteningBignumConversionPowD(Double *dPtr,
int sd, int k, int len,
int ilim, int ilim1, int *decpt,
char **endPtr);
-static char * StrictBignumConversionPowD(Double *dPtr, int convType,
+static char * StrictBignumConversionPowD(
Tcl_WideUInt bw, int b2, int b5,
int sd, int k, int len,
int ilim, int ilim1, int *decpt,
@@ -361,7 +361,7 @@ static char * ShorteningBignumConversion(Double *dPtr, int convType,
int s2, int s5, int k, int len,
int ilim, int ilim1, int *decpt,
char **endPtr);
-static char * StrictBignumConversion(Double *dPtr, int convType,
+static char * StrictBignumConversion(
Tcl_WideUInt bw, int b2,
int s2, int s5, int k, int len,
int ilim, int ilim1, int *decpt,
@@ -529,7 +529,7 @@ TclParseNumber(
* point. */
int exponentSignum = 0; /* Signum of the exponent of a floating point
* number. */
- long exponent = 0; /* Exponent of a floating point number. */
+ int exponent = 0; /* Exponent of a floating point number. */
const char *p; /* Pointer to next character to scan. */
size_t len; /* Number of characters remaining after p. */
const char *acceptPoint; /* Pointer to position after last character in
@@ -1019,17 +1019,17 @@ TclParseNumber(
case EXPONENT:
/*
* Found an exponent with at least one digit. Accumulate it,
- * making sure to hard-pin it to LONG_MAX on overflow.
+ * making sure to hard-pin it to INT_MAX on overflow.
*/
acceptState = state;
acceptPoint = p;
acceptLen = len;
if (isdigit(UCHAR(c))) {
- if (exponent < (LONG_MAX - 9) / 10) {
+ if (exponent < (INT_MAX - 9) / 10) {
exponent = 10 * exponent + (c - '0');
} else {
- exponent = LONG_MAX;
+ exponent = INT_MAX;
}
state = EXPONENT;
break;
@@ -1172,6 +1172,7 @@ TclParseNumber(
p = acceptPoint;
len = acceptLen;
+
if (!(flags & TCL_PARSE_NO_WHITESPACE)) {
/*
* Accept trailing whitespace.
@@ -1411,19 +1412,19 @@ TclParseNumber(
/*
* Adjust the exponent for the number of trailing zeros that
* have not been accumulated, and the number of digits after
- * the decimal point. Pin any overflow to LONG_MAX/LONG_MIN
+ * the decimal point. Pin any overflow to INT_MAX/INT_MIN
* respectively.
*/
if (exponent >= 0) {
- if (exponent - numDigitsAfterDp > LONG_MAX - numTrailZeros) {
- exponent = LONG_MAX;
+ if (exponent - numDigitsAfterDp > INT_MAX - numTrailZeros) {
+ exponent = INT_MAX;
} else {
exponent = exponent - numDigitsAfterDp + numTrailZeros;
}
} else {
- if (exponent + numTrailZeros < LONG_MIN + numDigitsAfterDp) {
- exponent = LONG_MIN;
+ if (exponent + numTrailZeros < INT_MIN + numDigitsAfterDp) {
+ exponent = INT_MIN;
} else {
exponent = exponent + numTrailZeros - numDigitsAfterDp;
}
@@ -1625,7 +1626,7 @@ MakeLowPrecisionDouble(
int signum, /* 1 if the number is negative, 0 otherwise */
Tcl_WideUInt significand, /* Significand of the number */
int numSigDigs, /* Number of digits in the significand */
- long exponent) /* Power of ten */
+ int exponent) /* Power of ten */
{
TCL_IEEE_DOUBLE_ROUNDING_DECL
@@ -1639,7 +1640,7 @@ MakeLowPrecisionDouble(
* ulp, so we need to change rounding mode to 53-bits. We also make
* 'retval' volatile, so that it doesn't get promoted to a register.
*/
- volatile double retval; /* Value of the number. */
+ volatile double retval; /* Value of the number. */
/*
* Test for zero significand, which requires explicit construction
@@ -1664,8 +1665,8 @@ MakeLowPrecisionDouble(
* without special handling.
*/
- retval = (double)
- ((Tcl_WideInt)significand * pow10vals[exponent]);
+ retval =
+ ((double)significand * pow10vals[exponent]);
goto returnValue;
} else {
int diff = QUICK_MAX - numSigDigs;
@@ -1678,8 +1679,8 @@ MakeLowPrecisionDouble(
* with only one roundoff.
*/
- volatile double factor = (double)
- ((Tcl_WideInt)significand * pow10vals[diff]);
+ volatile double factor =
+ ((double)significand * pow10vals[diff]);
retval = factor * pow10vals[exponent-diff];
goto returnValue;
}
@@ -1692,8 +1693,8 @@ MakeLowPrecisionDouble(
* only one rounding.
*/
- retval = (double)
- ((Tcl_WideInt)significand / pow10vals[-exponent]);
+ retval =
+ ((double)significand / pow10vals[-exponent]);
goto returnValue;
}
}
@@ -1750,7 +1751,7 @@ MakeHighPrecisionDouble(
int signum, /* 1=negative, 0=nonnegative */
mp_int *significand, /* Exact significand of the number */
int numSigDigs, /* Number of significant digits */
- long exponent) /* Power of 10 by which to multiply */
+ int exponent) /* Power of 10 by which to multiply */
{
TCL_IEEE_DOUBLE_ROUNDING_DECL
@@ -1809,9 +1810,9 @@ MakeHighPrecisionDouble(
goto returnValue;
}
retval = SafeLdExp(retval, machexp);
- if (tiny == 0.0) {
- tiny = SafeLdExp(1.0, DBL_MIN_EXP * log2FLT_RADIX - mantBits);
- }
+ if (tiny == 0.0) {
+ tiny = SafeLdExp(1.0, DBL_MIN_EXP * log2FLT_RADIX - mantBits);
+ }
if (retval < tiny) {
retval = tiny;
}
@@ -2067,7 +2068,7 @@ RefineApproximation(
*/
if (roundToEven) {
rteSignificand = frexp(approxResult, &rteExponent);
- rteSigWide = ldexp(rteSignificand, FP_PRECISION);
+ rteSigWide = (Tcl_WideInt)ldexp(rteSignificand, FP_PRECISION);
if ((rteSigWide & 1) == 0) {
mp_clear(&twoMd);
mp_clear(&twoMv);
@@ -2214,12 +2215,12 @@ RequiredPrecision(
Tcl_WideUInt w) /* Number to interrogate. */
{
int rv;
- unsigned long wi;
+ unsigned int wi;
- if (w & ((Tcl_WideUInt) 0xFFFFFFFF << 32)) {
- wi = (unsigned long) (w >> 32); rv = 32;
+ if (w & ((Tcl_WideUInt)0xFFFFFFFF << 32)) {
+ wi = (unsigned int)(w >> 32); rv = 32;
} else {
- wi = (unsigned long) w; rv = 0;
+ wi = (unsigned int)w; rv = 0;
}
if (wi & 0xFFFF0000) {
wi >>= 16; rv += 16;
@@ -2346,13 +2347,13 @@ FormatInfAndNaN(
*decpt = 9999;
if (!(d->w.word1) && !(d->w.word0 & HI_ORDER_SIG_MASK)) {
- retval = ckalloc(9);
+ retval = (char *)ckalloc(9);
strcpy(retval, "Infinity");
if (endPtr) {
*endPtr = retval + 8;
}
} else {
- retval = ckalloc(4);
+ retval = (char *)ckalloc(4);
strcpy(retval, "NaN");
if (endPtr) {
*endPtr = retval + 3;
@@ -2383,7 +2384,7 @@ FormatZero(
int *decpt, /* Location of the decimal point. */
char **endPtr) /* Pointer to the end of the formatted data */
{
- char *retval = ckalloc(2);
+ char *retval = (char *)ckalloc(2);
strcpy(retval, "0");
if (endPtr) {
@@ -2625,7 +2626,7 @@ SetPrecisionLimits(
static inline char *
BumpUp(
- char *s, /* Cursor pointing one past the end of the
+ char *s, /* Cursor pointing one past the end of the
* string. */
char *retval, /* Start of the string of digits. */
int *kPtr) /* Position of the decimal point. */
@@ -2753,7 +2754,7 @@ ShorteningQuickFormat(
digit = (int) d;
d -= digit;
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
/*
* Truncate the conversion if the string of digits is within 1/2 ulp
@@ -2826,12 +2827,12 @@ StrictQuickFormat(
* Extract a digit.
*/
- digit = (int) d;
+ digit = (int)d;
d -= digit;
if (d == 0.0) {
ilim = i;
}
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
/*
* When the given digit count is reached, handle trailing strings of 0
@@ -2936,7 +2937,7 @@ QuickConversion(
* Handle the peculiar case where the result has no significant digits.
*/
- retval = ckalloc(len + 1);
+ retval = (char *)ckalloc(len + 1);
if (ilim == 0) {
d = d - 5.;
if (d > eps.d) {
@@ -3049,7 +3050,7 @@ ShorteningInt64Conversion(
char **endPtr) /* OUTPUT: Position of the terminal '\0' at
* the end of the returned string. */
{
- char *retval = ckalloc(len + 1);
+ char *retval = (char *)ckalloc(len + 1);
/* Output buffer. */
Tcl_WideUInt b = (bw * wuipow5[b5]) << b2;
/* Numerator of the fraction being
@@ -3095,7 +3096,7 @@ ShorteningInt64Conversion(
/*
* Does the current digit put us on the low side of the exact value
- * but within within roundoff of being exact?
+ * but within roundoff of being exact?
*/
if (b < mplus || (b == mplus
@@ -3118,7 +3119,7 @@ ShorteningInt64Conversion(
* Stash the current digit.
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
break;
}
@@ -3135,7 +3136,7 @@ ShorteningInt64Conversion(
break;
}
++digit;
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
break;
}
@@ -3143,7 +3144,7 @@ ShorteningInt64Conversion(
* Have we converted all the requested digits?
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
@@ -3199,9 +3200,6 @@ ShorteningInt64Conversion(
static inline char *
StrictInt64Conversion(
- Double *dPtr, /* Original number to convert. */
- int convType, /* Type of conversion (shortest, Steele,
- * E format, F format). */
Tcl_WideUInt bw, /* Integer significand. */
int b2, int b5, /* Scale factor for the significand in the
* numerator. */
@@ -3215,7 +3213,7 @@ StrictInt64Conversion(
char **endPtr) /* OUTPUT: Position of the terminal '\0' at
* the end of the returned string. */
{
- char *retval = ckalloc(len + 1);
+ char *retval = (char *)ckalloc(len + 1);
/* Output buffer. */
Tcl_WideUInt b = (bw * wuipow5[b5]) << b2;
/* Numerator of the fraction being
@@ -3253,7 +3251,7 @@ StrictInt64Conversion(
* Have we converted all the requested digits?
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
@@ -3425,7 +3423,7 @@ ShorteningBignumConversionPowD(
char **endPtr) /* OUTPUT: Position of the terminal '\0' at
* the end of the returned string. */
{
- char *retval = ckalloc(len + 1);
+ char *retval = (char *)ckalloc(len + 1);
/* Output buffer. */
mp_int b; /* Numerator of the fraction being
* converted. */
@@ -3489,7 +3487,7 @@ ShorteningBignumConversionPowD(
/*
* Does the current digit put us on the low side of the exact value
- * but within within roundoff of being exact?
+ * but within roundoff of being exact?
*/
r1 = mp_cmp_mag(&b, (m2plus > m2minus)? &mplus : &mminus);
@@ -3513,7 +3511,7 @@ ShorteningBignumConversionPowD(
* Stash the last digit.
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
break;
}
@@ -3530,7 +3528,7 @@ ShorteningBignumConversionPowD(
break;
}
++digit;
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
break;
}
@@ -3538,7 +3536,7 @@ ShorteningBignumConversionPowD(
* Have we converted all the requested digits?
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
if (i == ilim) {
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
@@ -3599,9 +3597,6 @@ ShorteningBignumConversionPowD(
static inline char *
StrictBignumConversionPowD(
- Double *dPtr, /* Original number to convert. */
- int convType, /* Type of conversion (shortest, Steele,
- * E format, F format). */
Tcl_WideUInt bw, /* Integer significand. */
int b2, int b5, /* Scale factor for the significand in the
* numerator. */
@@ -3615,7 +3610,7 @@ StrictBignumConversionPowD(
char **endPtr) /* OUTPUT: Position of the terminal '\0' at
* the end of the returned string. */
{
- char *retval = ckalloc(len + 1);
+ char *retval = (char *)ckalloc(len + 1);
/* Output buffer. */
mp_int b; /* Numerator of the fraction being
* converted. */
@@ -3665,7 +3660,7 @@ StrictBignumConversionPowD(
* Have we converted all the requested digits?
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
if (i == ilim) {
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
@@ -3723,15 +3718,13 @@ ShouldBankerRoundUp(
int r = mp_cmp_mag(twor, S);
switch (r) {
- case MP_LT:
- return 0;
case MP_EQ:
return isodd;
case MP_GT:
return 1;
+ default:
+ return 0;
}
- Tcl_Panic("in ShouldBankerRoundUp, trichotomy fails!");
- return 0;
}
/*
@@ -3769,8 +3762,6 @@ ShouldBankerRoundUpToNext(
mp_add(b, m, temp);
r = mp_cmp_mag(temp, S);
switch(r) {
- case MP_LT:
- return 0;
case MP_EQ:
if (convType == TCL_DD_STEELE0) {
return 0;
@@ -3779,9 +3770,9 @@ ShouldBankerRoundUpToNext(
}
case MP_GT:
return 1;
+ default:
+ return 0;
}
- Tcl_Panic("in ShouldBankerRoundUpToNext, trichotomy fails!");
- return 0;
}
/*
@@ -3817,7 +3808,7 @@ ShorteningBignumConversion(
int *decpt, /* OUTPUT: Position of the decimal point. */
char **endPtr) /* OUTPUT: Pointer to the end of the number */
{
- char *retval = ckalloc(len+1);
+ char *retval = (char *)ckalloc(len+1);
/* Buffer of digits to return. */
char *s = retval; /* Cursor in the return value. */
mp_int b; /* Numerator of the result. */
@@ -3875,7 +3866,7 @@ ShorteningBignumConversion(
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
- digit = dig.dp[0];
+ digit = (int)dig.dp[0];
/*
* Does the current digit leave us with a remainder small enough to
@@ -3894,7 +3885,7 @@ ShorteningBignumConversion(
break;
}
}
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
break;
}
@@ -3911,7 +3902,7 @@ ShorteningBignumConversion(
s = BumpUp(s, retval, &k);
break;
}
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
break;
}
@@ -3919,7 +3910,7 @@ ShorteningBignumConversion(
* Have we converted all the requested digits?
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
if (i == ilim) {
mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
@@ -4020,8 +4011,6 @@ ShorteningBignumConversion(
static inline char *
StrictBignumConversion(
- Double *dPtr, /* Original number being converted. */
- int convType, /* Conversion type. */
Tcl_WideUInt bw, /* Integer significand and exponent. */
int b2, /* Scale factor for the significand. */
int s2, int s5, /* Scale factors for denominator. */
@@ -4032,7 +4021,7 @@ StrictBignumConversion(
int *decpt, /* OUTPUT: Position of the decimal point. */
char **endPtr) /* OUTPUT: Pointer to the end of the number */
{
- char *retval = ckalloc(len+1);
+ char *retval = (char *)ckalloc(len+1);
/* Buffer of digits to return. */
char *s = retval; /* Cursor in the return value. */
mp_int b; /* Numerator of the result. */
@@ -4073,13 +4062,13 @@ StrictBignumConversion(
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
- digit = dig.dp[0];
+ digit = (int)dig.dp[0];
/*
* Is a single digit all that was requested?
*/
- *s++ = '0' + digit;
+ *s++ = '0' + (char)digit;
if (++i >= ilim) {
mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
@@ -4123,11 +4112,11 @@ StrictBignumConversion(
if (dig.used > 1) {
Tcl_Panic("wrong digit!");
}
- digit = dig.dp[0];
+ digit = (int)dig.dp[0];
for (j = g-1; j >= 0; --j) {
int t = itens[j];
- *s++ = digit / t + '0';
+ *s++ = (char)(digit / t + '0');
digit %= t;
}
i += g;
@@ -4461,7 +4450,7 @@ TclDoubleDigits(
* operations.
*/
- return StrictInt64Conversion(&d, convType, bw, b2, b5, s2, s5, k,
+ return StrictInt64Conversion(bw, b2, b5, s2, s5, k,
len, ilim, ilim1, decpt, endPtr);
} else if (s5 == 0) {
/*
@@ -4478,7 +4467,7 @@ TclDoubleDigits(
b2 += delta;
s2 += delta;
}
- return StrictBignumConversionPowD(&d, convType, bw, b2, b5,
+ return StrictBignumConversionPowD(bw, b2, b5,
s2/MP_DIGIT_BIT, k, len, ilim, ilim1, decpt, endPtr);
} else {
/*
@@ -4488,7 +4477,7 @@ TclDoubleDigits(
* fewer mp_int divisions.
*/
- return StrictBignumConversion(&d, convType, bw, b2, s2, s5, k,
+ return StrictBignumConversion(bw, b2, s2, s5, k,
len, ilim, ilim1, decpt, endPtr);
}
}
@@ -4703,7 +4692,7 @@ Tcl_InitBignumFromDouble(
mp_init(b);
mp_zero(b);
} else {
- Tcl_WideInt w = ldexp(fract, mantBits);
+ Tcl_WideInt w = (Tcl_WideInt)ldexp(fract, mantBits);
int shift = expt - mantBits;
TclBNInitBignumFromWideInt(b, w);
@@ -4739,7 +4728,6 @@ TclBignumToDouble(
int bits, shift, i, lsb;
double r;
-
/*
* We need a 'mantBits'-bit significand. Determine what shift will
* give us that.
@@ -5038,7 +5026,7 @@ Pow10TimesFrExp(
* Multiply by 10**exponent.
*/
- retval = frexp(retval * pow10vals[exponent&0xF], &j);
+ retval = frexp(retval * pow10vals[exponent & 0xF], &j);
expt += j;
for (i=4; i<9; ++i) {
if (exponent & (1<<i)) {
@@ -5136,8 +5124,8 @@ TclFormatNaN(
if (n770_fp) {
bitwhack.iv = Nokia770Twiddle(bitwhack.iv);
}
- if (bitwhack.iv & ((Tcl_WideUInt) 1 << 63)) {
- bitwhack.iv &= ~ ((Tcl_WideUInt) 1 << 63);
+ if (bitwhack.iv & ((Tcl_WideUInt)1 << 63)) {
+ bitwhack.iv &= ~ ((Tcl_WideUInt)1 << 63);
*buffer++ = '-';
}
*buffer++ = 'N';