summaryrefslogtreecommitdiffstats
path: root/generic
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2019-05-14 19:13:01 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2019-05-14 19:13:01 (GMT)
commitde4f571baa7f9d53f48031e230c4d8f7c69cf1f5 (patch)
tree5a70c38238478aba4893b1207c3d5fe70bb83937 /generic
parent9ca29b09ae01c2710010821b09fff5f613c7c26f (diff)
parent8094d33e4331c3f61d015b91018908fa6fb60b96 (diff)
downloadtcl-de4f571baa7f9d53f48031e230c4d8f7c69cf1f5.zip
tcl-de4f571baa7f9d53f48031e230c4d8f7c69cf1f5.tar.gz
tcl-de4f571baa7f9d53f48031e230c4d8f7c69cf1f5.tar.bz2
Merge 8.5
Diffstat (limited to 'generic')
-rw-r--r--generic/tclObj.c10
-rw-r--r--generic/tclStrToD.c30
-rw-r--r--generic/tclStringObj.c10
-rw-r--r--generic/tclTomMath.h78
-rw-r--r--generic/tclTomMathDecls.h7
-rw-r--r--generic/tclTomMathInterface.c4
6 files changed, 88 insertions, 51 deletions
diff --git a/generic/tclObj.c b/generic/tclObj.c
index a75ecdd..9eaf1ee 100644
--- a/generic/tclObj.c
+++ b/generic/tclObj.c
@@ -2804,8 +2804,8 @@ Tcl_GetLongFromObj(
mp_int big;
UNPACK_BIGNUM(objPtr, big);
- if ((size_t) big.used <= (CHAR_BIT * sizeof(long) + DIGIT_BIT - 1)
- / DIGIT_BIT) {
+ if ((size_t) big.used <= (CHAR_BIT * sizeof(long) + MP_DIGIT_BIT - 1)
+ / MP_DIGIT_BIT) {
unsigned long value = 0, numBytes = sizeof(long);
long scratch;
unsigned char *bytes = (unsigned char *) &scratch;
@@ -3104,7 +3104,7 @@ Tcl_GetWideIntFromObj(
UNPACK_BIGNUM(objPtr, big);
if ((size_t) big.used <= (CHAR_BIT * sizeof(Tcl_WideInt)
- + DIGIT_BIT - 1) / DIGIT_BIT) {
+ + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT) {
Tcl_WideUInt value = 0;
unsigned long numBytes = sizeof(Tcl_WideInt);
Tcl_WideInt scratch;
@@ -3523,7 +3523,7 @@ Tcl_SetBignumObj(
Tcl_Panic("%s called with shared object", "Tcl_SetBignumObj");
}
if ((size_t) bignumValue->used
- <= (CHAR_BIT * sizeof(long) + DIGIT_BIT - 1) / DIGIT_BIT) {
+ <= (CHAR_BIT * sizeof(long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT) {
unsigned long value = 0, numBytes = sizeof(long);
long scratch;
unsigned char *bytes = (unsigned char *) &scratch;
@@ -3548,7 +3548,7 @@ Tcl_SetBignumObj(
tooLargeForLong:
#ifndef TCL_WIDE_INT_IS_LONG
if ((size_t) bignumValue->used
- <= (CHAR_BIT * sizeof(Tcl_WideInt) + DIGIT_BIT - 1) / DIGIT_BIT) {
+ <= (CHAR_BIT * sizeof(Tcl_WideInt) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT) {
Tcl_WideUInt value = 0;
unsigned long numBytes = sizeof(Tcl_WideInt);
Tcl_WideInt scratch;
diff --git a/generic/tclStrToD.c b/generic/tclStrToD.c
index 6257409..6e1b131 100644
--- a/generic/tclStrToD.c
+++ b/generic/tclStrToD.c
@@ -1855,15 +1855,15 @@ RefineApproximation(
*/
msb = binExponent + M2; /* 1008 */
- nDigits = msb / DIGIT_BIT + 1;
+ nDigits = msb / MP_DIGIT_BIT + 1;
mp_init_size(&twoMv, nDigits);
- i = (msb % DIGIT_BIT + 1);
+ i = (msb % MP_DIGIT_BIT + 1);
twoMv.used = nDigits;
significand *= SafeLdExp(1.0, i);
while (--nDigits >= 0) {
twoMv.dp[nDigits] = (mp_digit) significand;
significand -= (mp_digit) significand;
- significand = SafeLdExp(significand, DIGIT_BIT);
+ significand = SafeLdExp(significand, MP_DIGIT_BIT);
}
for (i = 0; i <= 8; ++i) {
if (M5 & (1 << i)) {
@@ -3162,7 +3162,7 @@ ShouldBankerRoundUpPowD(
int isodd) /* 1 if the digit is odd, 0 if even. */
{
int i;
- static const mp_digit topbit = ((mp_digit)1) << (DIGIT_BIT - 1);
+ static const mp_digit topbit = ((mp_digit)1) << (MP_DIGIT_BIT - 1);
if (b->used < sd || (b->dp[sd-1] & topbit) == 0) {
return 0;
@@ -4268,8 +4268,8 @@ TclDoubleDigits(
* in the denominator' case.
*/
- if (s2 % DIGIT_BIT != 0) {
- int delta = DIGIT_BIT - (s2 % DIGIT_BIT);
+ if (s2 % MP_DIGIT_BIT != 0) {
+ int delta = MP_DIGIT_BIT - (s2 % MP_DIGIT_BIT);
b2 += delta;
m2plus += delta;
@@ -4277,7 +4277,7 @@ TclDoubleDigits(
s2 += delta;
}
return ShorteningBignumConversionPowD(&d, convType, bw, b2, b5,
- m2plus, m2minus, m5, s2/DIGIT_BIT, k, len, ilim, ilim1,
+ m2plus, m2minus, m5, s2/MP_DIGIT_BIT, k, len, ilim, ilim1,
decpt, endPtr);
} else {
/*
@@ -4324,14 +4324,14 @@ TclDoubleDigits(
* in the denominator' case.
*/
- if (s2 % DIGIT_BIT != 0) {
- int delta = DIGIT_BIT - (s2 % DIGIT_BIT);
+ if (s2 % MP_DIGIT_BIT != 0) {
+ int delta = MP_DIGIT_BIT - (s2 % MP_DIGIT_BIT);
b2 += delta;
s2 += delta;
}
return StrictBignumConversionPowD(&d, convType, bw, b2, b5,
- s2/DIGIT_BIT, k, len, ilim, ilim1, decpt, endPtr);
+ s2/MP_DIGIT_BIT, k, len, ilim, ilim1, decpt, endPtr);
} else {
/*
* There are no helpful special cases, but at least we know in
@@ -4456,7 +4456,7 @@ TclInitDoubleConversion(void)
+ 0.5 * log(10.)) / log(10.));
minDigits = (int) floor((DBL_MIN_EXP - DBL_MANT_DIG)
* log((double) FLT_RADIX) / log(10.));
- log10_DIGIT_MAX = (int) floor(DIGIT_BIT * log(2.) / log(10.));
+ log10_DIGIT_MAX = (int) floor(MP_DIGIT_BIT * log(2.) / log(10.));
/*
* Nokia 770's software-emulated floating point is "middle endian": the
@@ -4660,7 +4660,7 @@ TclBignumToDouble(
r = 0.0;
for (i=b.used-1 ; i>=0 ; --i) {
- r = ldexp(r, DIGIT_BIT) + b.dp[i];
+ r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
mp_clear(&b);
@@ -4729,7 +4729,7 @@ TclCeil(
mp_add_d(&b, 1, &b);
}
for (i=b.used-1 ; i>=0 ; --i) {
- r = ldexp(r, DIGIT_BIT) + b.dp[i];
+ r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
@@ -4779,7 +4779,7 @@ TclFloor(
mp_copy(a, &b);
}
for (i=b.used-1 ; i>=0 ; --i) {
- r = ldexp(r, DIGIT_BIT) + b.dp[i];
+ r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
@@ -4841,7 +4841,7 @@ BignumToBiasedFrExp(
r = 0.0;
for (i=b.used-1; i>=0; --i) {
- r = ldexp(r, DIGIT_BIT) + b.dp[i];
+ r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
mp_clear(&b);
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c
index 46bd1c1..222e529 100644
--- a/generic/tclStringObj.c
+++ b/generic/tclStringObj.c
@@ -2218,11 +2218,11 @@ Tcl_AppendFormatToObj(
}
#endif
} else if (useBig && big.used) {
- int leftover = (big.used * DIGIT_BIT) % numBits;
- mp_digit mask = (~(mp_digit)0) << (DIGIT_BIT-leftover);
+ int leftover = (big.used * MP_DIGIT_BIT) % numBits;
+ mp_digit mask = (~(mp_digit)0) << (MP_DIGIT_BIT-leftover);
numDigits = 1 +
- (((Tcl_WideInt) big.used * DIGIT_BIT) / numBits);
+ (((Tcl_WideInt) big.used * MP_DIGIT_BIT) / numBits);
while ((mask & big.dp[big.used-1]) == 0) {
numDigits--;
mask >>= numBits;
@@ -2258,9 +2258,9 @@ Tcl_AppendFormatToObj(
if (useBig && big.used) {
if (index < big.used && (size_t) shift <
- CHAR_BIT*sizeof(Tcl_WideUInt) - DIGIT_BIT) {
+ CHAR_BIT*sizeof(Tcl_WideUInt) - MP_DIGIT_BIT) {
bits |= ((Tcl_WideUInt) big.dp[index++]) << shift;
- shift += DIGIT_BIT;
+ shift += MP_DIGIT_BIT;
}
shift -= numBits;
}
diff --git a/generic/tclTomMath.h b/generic/tclTomMath.h
index 26eef26..cc9f286 100644
--- a/generic/tclTomMath.h
+++ b/generic/tclTomMath.h
@@ -1,14 +1,6 @@
-/* LibTomMath, multiple-precision integer library -- Tom St Denis
- *
- * LibTomMath is a library that provides multiple-precision
- * integer arithmetic as well as number theoretic functionality.
- *
- * The library was designed directly after the MPI library by
- * Michael Fromberger but has been written from scratch with
- * additional optimizations in place.
- *
- * SPDX-License-Identifier: Unlicense
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
#ifndef BN_H_
#define BN_H_
@@ -115,29 +107,30 @@ typedef unsigned long long mp_word;
#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
#define MP_DIGIT_MAX MP_MASK
-/* equalities */
+typedef int mp_sign;
+#define MP_ZPOS 0 /* positive integer */
+#define MP_NEG 1 /* negative */
+typedef int mp_ord;
#define MP_LT -1 /* less than */
#define MP_EQ 0 /* equal to */
#define MP_GT 1 /* greater than */
-
-#define MP_ZPOS 0 /* positive integer */
-#define MP_NEG 1 /* negative */
-
+typedef int mp_bool;
+#define MP_YES 1 /* yes response */
+#define MP_NO 0 /* no response */
+typedef int mp_err;
#define MP_OKAY 0 /* ok result */
+#define MP_ERR -1 /* unknown error */
#define MP_MEM -2 /* out of mem */
#define MP_VAL -3 /* invalid input */
#define MP_RANGE MP_VAL
#define MP_ITER -4 /* Max. iterations reached */
-#define MP_YES 1 /* yes response */
-#define MP_NO 0 /* no response */
-
/* Primality generation flags */
#define LTM_PRIME_BBS 0x0001 /* BBS style prime */
#define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
#define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
-typedef int mp_err;
+/* tunable cutoffs */
/* define this to use lower memory usage routines (exptmods mostly) */
/* #define MP_LOW_MEM */
@@ -146,6 +139,8 @@ typedef int mp_err;
#ifndef MP_PREC
# ifndef MP_LOW_MEM
# define MP_PREC 32 /* default digits of precision */
+# elif defined(MP_8BIT)
+# define MP_PREC 16 /* default digits of precision */
# else
# define MP_PREC 8 /* default digits of precision */
# endif
@@ -154,6 +149,45 @@ typedef int mp_err;
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
#define MP_WARRAY (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
+/*
+ * MP_WUR - warn unused result
+ * ---------------------------
+ *
+ * The result of functions annotated with MP_WUR must be
+ * checked and cannot be ignored.
+ *
+ * Most functions in libtommath return an error code.
+ * This error code must be checked in order to prevent crashes or invalid
+ * results.
+ *
+ * If you still want to avoid the error checks for quick and dirty programs
+ * without robustness guarantees, you can `#define MP_WUR` before including
+ * tommath.h, disabling the warnings.
+ */
+#ifndef MP_WUR
+# if defined(__GNUC__) && __GNUC__ >= 4
+# define MP_WUR __attribute__((warn_unused_result))
+# else
+# define MP_WUR
+# endif
+#endif
+
+#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301)
+# define MP_DEPRECATED(x) __attribute__((deprecated("replaced by " #x)))
+# define PRIVATE_MP_DEPRECATED_PRAGMA(s) _Pragma(#s)
+# define MP_DEPRECATED_PRAGMA(s) PRIVATE_MP_DEPRECATED_PRAGMA(GCC warning s)
+#elif defined(_MSC_VER) && _MSC_VER >= 1500
+# define MP_DEPRECATED(x) __declspec(deprecated("replaced by " #x))
+# define MP_DEPRECATED_PRAGMA(s) __pragma(message(s))
+#else
+# define MP_DEPRECATED
+# define MP_DEPRECATED_PRAGMA(s)
+#endif
+
+#define USED(m) ((m)->used)
+#define DIGIT(m,k) ((m)->dp[(k)])
+#define SIGN(m) ((m)->sign)
+
/* the infamous mp_int structure */
#ifndef MP_INT_DECLARED
#define MP_INT_DECLARED
@@ -168,10 +202,6 @@ struct mp_int {
typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
-#define USED(m) ((m)->used)
-#define DIGIT(m, k) ((m)->dp[(k)])
-#define SIGN(m) ((m)->sign)
-
/* error code to char* string */
const char *mp_error_to_string(int code);
diff --git a/generic/tclTomMathDecls.h b/generic/tclTomMathDecls.h
index df66748..beb34ef 100644
--- a/generic/tclTomMathDecls.h
+++ b/generic/tclTomMathDecls.h
@@ -45,8 +45,11 @@
/* Rename the global symbols in libtommath to avoid linkage conflicts */
#define bn_reverse TclBN_reverse
+#define s_mp_reverse TclBN_reverse
#define fast_s_mp_mul_digs TclBN_fast_s_mp_mul_digs
+#define s_mp_mul_digs_fast TclBN_fast_s_mp_mul_digs
#define fast_s_mp_sqr TclBN_fast_s_mp_sqr
+#define s_mp_sqr_fast TclBN_fast_s_mp_sqr
#define mp_add TclBN_mp_add
#define mp_add_d TclBN_mp_add_d
#define mp_and TclBN_mp_and
@@ -76,7 +79,9 @@
#define mp_init_set_int TclBN_mp_init_set_int
#define mp_init_size TclBN_mp_init_size
#define mp_karatsuba_mul TclBN_mp_karatsuba_mul
+#define s_mp_karatsuba_mul TclBN_mp_karatsuba_mul
#define mp_karatsuba_sqr TclBN_mp_karatsuba_sqr
+#define s_mp_karatsuba_sqr TclBN_mp_karatsuba_sqr
#define mp_lshd TclBN_mp_lshd
#define mp_mod TclBN_mp_mod
#define mp_mod_2d TclBN_mp_mod_2d
@@ -104,7 +109,9 @@
#define mp_to_unsigned_bin TclBN_mp_to_unsigned_bin
#define mp_to_unsigned_bin_n TclBN_mp_to_unsigned_bin_n
#define mp_toom_mul TclBN_mp_toom_mul
+#define s_mp_toom_mul TclBN_mp_toom_mul
#define mp_toom_sqr TclBN_mp_toom_sqr
+#define s_mp_toom_sqr TclBN_mp_toom_sqr
#define mp_toradix_n TclBN_mp_toradix_n
#define mp_unsigned_bin_size TclBN_mp_unsigned_bin_size
#define mp_xor TclBN_mp_xor
diff --git a/generic/tclTomMathInterface.c b/generic/tclTomMathInterface.c
index 902fd8d..55dadfb 100644
--- a/generic/tclTomMathInterface.c
+++ b/generic/tclTomMathInterface.c
@@ -143,7 +143,7 @@ TclBNInitBignumFromLong(
p = a->dp;
while (v) {
*p++ = (mp_digit) (v & MP_MASK);
- v >>= DIGIT_BIT;
+ v >>= MP_DIGIT_BIT;
}
a->used = p - a->dp;
}
@@ -219,7 +219,7 @@ TclBNInitBignumFromWideUInt(
p = a->dp;
while (v) {
*p++ = (mp_digit) (v & MP_MASK);
- v >>= DIGIT_BIT;
+ v >>= MP_DIGIT_BIT;
}
a->used = p - a->dp;
}