From abf9722f425fcd4a8327dddaf5c5ccf08b1d30d6 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 28 Mar 2019 20:59:03 +0000 Subject: Turn KARATSUBA_MUL_CUTOFF (insize libtommath) et al into a #define in stead of exported int symbols. --- generic/tclTomMath.h | 13 +----- generic/tclTomMathDecls.h | 8 ---- generic/tclTomMathInterface.c | 81 ++---------------------------------- libtommath/bn_mp_get_bit.c | 12 +----- libtommath/tommath.h | 25 +---------- libtommath/tommath_private.h | 11 ++++- macosx/Tcl.xcode/project.pbxproj | 4 -- macosx/Tcl.xcodeproj/project.pbxproj | 4 -- unix/Makefile.in | 6 +-- win/Makefile.in | 1 - win/makefile.vc | 1 - 11 files changed, 16 insertions(+), 150 deletions(-) diff --git a/generic/tclTomMath.h b/generic/tclTomMath.h index 0541ad8..3f23fd6 100644 --- a/generic/tclTomMath.h +++ b/generic/tclTomMath.h @@ -25,7 +25,7 @@ extern "C" { #endif /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */ -#if defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__) +#if defined(_WIN32) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__) # define MP_32BIT #endif @@ -110,9 +110,6 @@ typedef unsigned long long mp_word; /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ #ifndef DIGIT_BIT # define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */ -typedef unsigned long mp_min_u32; -#else -typedef mp_digit mp_min_u32; #endif #define MP_DIGIT_BIT DIGIT_BIT @@ -142,14 +139,6 @@ typedef mp_digit mp_min_u32; typedef int mp_err; -/* you'll have to tune these... */ -#if defined(BUILD_tcl) || !defined(_WIN32) -MODULE_SCOPE int KARATSUBA_MUL_CUTOFF, - KARATSUBA_SQR_CUTOFF, - TOOM_MUL_CUTOFF, - TOOM_SQR_CUTOFF; -#endif - /* define this to use lower memory usage routines (exptmods mostly) */ /* #define MP_LOW_MEM */ diff --git a/generic/tclTomMathDecls.h b/generic/tclTomMathDecls.h index dc06fc6..f287b84 100644 --- a/generic/tclTomMathDecls.h +++ b/generic/tclTomMathDecls.h @@ -36,21 +36,13 @@ #define TclBNRealloc(x,s) ((void*)ckrealloc((char*)(x),(size_t)(s))) /* MODULE_SCOPE void TclBNFree( void* ); */ #define TclBNFree(x) (ckfree((char*)(x))) -/* MODULE_SCOPE void* TclBNCalloc( size_t, size_t ); */ -/* unused - no macro */ #define XMALLOC(x) TclBNAlloc(x) #define XFREE(x) TclBNFree(x) #define XREALLOC(x,n) TclBNRealloc(x,n) -#define XCALLOC(n,x) TclBNCalloc(n,x) /* Rename the global symbols in libtommath to avoid linkage conflicts */ -#define KARATSUBA_MUL_CUTOFF TclBNKaratsubaMulCutoff -#define KARATSUBA_SQR_CUTOFF TclBNKaratsubaSqrCutoff -#define TOOM_MUL_CUTOFF TclBNToomMulCutoff -#define TOOM_SQR_CUTOFF TclBNToomSqrCutoff - #define bn_reverse TclBN_reverse #define fast_s_mp_mul_digs TclBN_fast_s_mp_mul_digs #define fast_s_mp_sqr TclBN_fast_s_mp_sqr diff --git a/generic/tclTomMathInterface.c b/generic/tclTomMathInterface.c index 48db8c3..d7da4ee 100644 --- a/generic/tclTomMathInterface.c +++ b/generic/tclTomMathInterface.c @@ -89,81 +89,6 @@ TclBN_revision(void) { return TCLTOMMATH_REVISION; } -#if 0 - -/* - *---------------------------------------------------------------------- - * - * TclBNAlloc -- - * - * Allocate memory for libtommath. - * - * Results: - * Returns a pointer to the allocated block. - * - * This procedure is a wrapper around Tcl_Alloc, needed because of a - * mismatched type signature between Tcl_Alloc and malloc. - * - *---------------------------------------------------------------------- - */ - -extern void * -TclBNAlloc( - size_t x) -{ - return (void *) ckalloc((unsigned int) x); -} - -/* - *---------------------------------------------------------------------- - * - * TclBNRealloc -- - * - * Change the size of an allocated block of memory in libtommath - * - * Results: - * Returns a pointer to the allocated block. - * - * This procedure is a wrapper around Tcl_Realloc, needed because of a - * mismatched type signature between Tcl_Realloc and realloc. - * - *---------------------------------------------------------------------- - */ - -void * -TclBNRealloc( - void *p, - size_t s) -{ - return (void *) ckrealloc((char *) p, (unsigned int) s); -} - -/* - *---------------------------------------------------------------------- - * - * TclBNFree -- - * - * Free allocated memory in libtommath. - * - * Results: - * None. - * - * Side effects: - * Memory is freed. - * - * This function is simply a wrapper around Tcl_Free, needed in libtommath - * because of a type mismatch between free and Tcl_Free. - * - *---------------------------------------------------------------------- - */ - -extern void -TclBNFree( - void *p) -{ - ckree((char *) p); -} -#endif /* *---------------------------------------------------------------------- @@ -219,7 +144,7 @@ TclBNInitBignumFromLong( p = a->dp; while (v) { *p++ = (mp_digit) (v & MP_MASK); - v >>= MP_DIGIT_BIT; + v >>= DIGIT_BIT; } a->used = p - a->dp; } @@ -287,7 +212,7 @@ TclBNInitBignumFromWideUInt( Tcl_Panic("initialization failure in TclBNInitBignumFromWideUInt"); } - a->sign = MP_ZPOS; + a->sign = 0; /* * Store the magnitude in the bignum. @@ -296,7 +221,7 @@ TclBNInitBignumFromWideUInt( p = a->dp; while (v) { *p++ = (mp_digit) (v & MP_MASK); - v >>= MP_DIGIT_BIT; + v >>= DIGIT_BIT; } a->used = p - a->dp; } diff --git a/libtommath/bn_mp_get_bit.c b/libtommath/bn_mp_get_bit.c index ab732c4..f5d2450 100644 --- a/libtommath/bn_mp_get_bit.c +++ b/libtommath/bn_mp_get_bit.c @@ -27,18 +27,8 @@ int mp_get_bit(const mp_int *a, int b) limb = b / DIGIT_BIT; - /* - * Zero is a special value with the member "used" set to zero. - * Needs to be tested before the check for the upper boundary - * otherwise (limb >= a->used) would be true for a = 0 - */ - - if (mp_iszero(a) != MP_NO) { - return MP_NO; - } - if (limb >= a->used) { - return MP_VAL; + return MP_NO; } bit = (mp_digit)(1) << (b % DIGIT_BIT); diff --git a/libtommath/tommath.h b/libtommath/tommath.h index 00c8b35..85814e7 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -23,7 +23,7 @@ extern "C" { #endif /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */ -#if defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__) +#if defined(_WIN32) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__) # define MP_32BIT #endif @@ -89,17 +89,7 @@ typedef unsigned long long mp_word; # endif #endif -/* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ -#ifndef DIGIT_BIT -# define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */ -typedef unsigned long mp_min_u32; -#else -typedef mp_digit mp_min_u32; -#endif - -#define MP_DIGIT_BIT DIGIT_BIT #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) -#define MP_DIGIT_MAX MP_MASK /* equalities */ #define MP_LT -1 /* less than */ @@ -125,12 +115,6 @@ typedef mp_digit mp_min_u32; typedef int mp_err; -/* you'll have to tune these... */ -extern int KARATSUBA_MUL_CUTOFF, - KARATSUBA_SQR_CUTOFF, - TOOM_MUL_CUTOFF, - TOOM_SQR_CUTOFF; - /* define this to use lower memory usage routines (exptmods mostly) */ /* #define MP_LOW_MEM */ @@ -143,9 +127,6 @@ extern int KARATSUBA_MUL_CUTOFF, # endif #endif -/* 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)) - /* the infamous mp_int structure */ typedef struct { int used, alloc, sign; @@ -156,10 +137,6 @@ typedef struct { 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/libtommath/tommath_private.h b/libtommath/tommath_private.h index 8fc3442..2096f77 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -42,15 +42,22 @@ extern "C" { # define XMALLOC malloc # define XFREE free # define XREALLOC realloc -# define XCALLOC calloc #elif 0 /* prototypes for our heap functions */ extern void *XMALLOC(size_t n); extern void *XREALLOC(void *p, size_t n); -extern void *XCALLOC(size_t n, size_t s); extern void XFREE(void *p); #endif +/* you'll have to tune these... */ +#define KARATSUBA_MUL_CUTOFF 80 /* Min. number of digits before Karatsuba multiplication is used. */ +#define KARATSUBA_SQR_CUTOFF 120 /* Min. number of digits before Karatsuba squaring is used. */ +#define TOOM_MUL_CUTOFF 350 /* no optimal values of these are known yet so set em high */ +#define TOOM_SQR_CUTOFF 400 + +/* 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)) + /* lowlevel functions, do not call! */ int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c); int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c); diff --git a/macosx/Tcl.xcode/project.pbxproj b/macosx/Tcl.xcode/project.pbxproj index c5b3868..578fb55 100644 --- a/macosx/Tcl.xcode/project.pbxproj +++ b/macosx/Tcl.xcode/project.pbxproj @@ -145,7 +145,6 @@ F96D495108F272C3004A47F5 /* bn_s_mp_mul_digs.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D308F272B3004A47F5 /* bn_s_mp_mul_digs.c */; }; F96D495308F272C3004A47F5 /* bn_s_mp_sqr.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D508F272B3004A47F5 /* bn_s_mp_sqr.c */; }; F96D495408F272C3004A47F5 /* bn_s_mp_sub.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D608F272B3004A47F5 /* bn_s_mp_sub.c */; }; - F96D495508F272C3004A47F5 /* bncore.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D708F272B3004A47F5 /* bncore.c */; }; F96D49A908F272C4004A47F5 /* tclMacOSXBundle.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D433908F272B5004A47F5 /* tclMacOSXBundle.c */; }; F96D49AD08F272C4004A47F5 /* tclMacOSXFCmd.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D433D08F272B5004A47F5 /* tclMacOSXFCmd.c */; }; F96D49AE08F272C4004A47F5 /* tclMacOSXNotify.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D433E08F272B5004A47F5 /* tclMacOSXNotify.c */; }; @@ -619,7 +618,6 @@ F96D42D308F272B3004A47F5 /* bn_s_mp_mul_digs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bn_s_mp_mul_digs.c; sourceTree = ""; }; F96D42D508F272B3004A47F5 /* bn_s_mp_sqr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bn_s_mp_sqr.c; sourceTree = ""; }; F96D42D608F272B3004A47F5 /* bn_s_mp_sub.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bn_s_mp_sub.c; sourceTree = ""; }; - F96D42D708F272B3004A47F5 /* bncore.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bncore.c; sourceTree = ""; }; F96D432908F272B4004A47F5 /* tommath_class.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tommath_class.h; sourceTree = ""; }; F96D432A08F272B4004A47F5 /* tommath_superclass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tommath_superclass.h; sourceTree = ""; }; F96D432B08F272B4004A47F5 /* license.terms */ = {isa = PBXFileReference; explicitFileType = text; fileEncoding = 4; path = license.terms; sourceTree = ""; }; @@ -1480,7 +1478,6 @@ F96D42D308F272B3004A47F5 /* bn_s_mp_mul_digs.c */, F96D42D508F272B3004A47F5 /* bn_s_mp_sqr.c */, F96D42D608F272B3004A47F5 /* bn_s_mp_sub.c */, - F96D42D708F272B3004A47F5 /* bncore.c */, F96D432908F272B4004A47F5 /* tommath_class.h */, F96D432A08F272B4004A47F5 /* tommath_superclass.h */, ); @@ -2111,7 +2108,6 @@ F96D495108F272C3004A47F5 /* bn_s_mp_mul_digs.c in Sources */, F96D495308F272C3004A47F5 /* bn_s_mp_sqr.c in Sources */, F96D495408F272C3004A47F5 /* bn_s_mp_sub.c in Sources */, - F96D495508F272C3004A47F5 /* bncore.c in Sources */, F96D49A908F272C4004A47F5 /* tclMacOSXBundle.c in Sources */, F96D49AD08F272C4004A47F5 /* tclMacOSXFCmd.c in Sources */, F96D49AE08F272C4004A47F5 /* tclMacOSXNotify.c in Sources */, diff --git a/macosx/Tcl.xcodeproj/project.pbxproj b/macosx/Tcl.xcodeproj/project.pbxproj index 6068112..a533e11 100644 --- a/macosx/Tcl.xcodeproj/project.pbxproj +++ b/macosx/Tcl.xcodeproj/project.pbxproj @@ -145,7 +145,6 @@ F96D495108F272C3004A47F5 /* bn_s_mp_mul_digs.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D308F272B3004A47F5 /* bn_s_mp_mul_digs.c */; }; F96D495308F272C3004A47F5 /* bn_s_mp_sqr.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D508F272B3004A47F5 /* bn_s_mp_sqr.c */; }; F96D495408F272C3004A47F5 /* bn_s_mp_sub.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D608F272B3004A47F5 /* bn_s_mp_sub.c */; }; - F96D495508F272C3004A47F5 /* bncore.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D42D708F272B3004A47F5 /* bncore.c */; }; F96D49A908F272C4004A47F5 /* tclMacOSXBundle.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D433908F272B5004A47F5 /* tclMacOSXBundle.c */; }; F96D49AD08F272C4004A47F5 /* tclMacOSXFCmd.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D433D08F272B5004A47F5 /* tclMacOSXFCmd.c */; }; F96D49AE08F272C4004A47F5 /* tclMacOSXNotify.c in Sources */ = {isa = PBXBuildFile; fileRef = F96D433E08F272B5004A47F5 /* tclMacOSXNotify.c */; }; @@ -619,7 +618,6 @@ F96D42D308F272B3004A47F5 /* bn_s_mp_mul_digs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bn_s_mp_mul_digs.c; sourceTree = ""; }; F96D42D508F272B3004A47F5 /* bn_s_mp_sqr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bn_s_mp_sqr.c; sourceTree = ""; }; F96D42D608F272B3004A47F5 /* bn_s_mp_sub.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bn_s_mp_sub.c; sourceTree = ""; }; - F96D42D708F272B3004A47F5 /* bncore.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bncore.c; sourceTree = ""; }; F96D432908F272B4004A47F5 /* tommath_class.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tommath_class.h; sourceTree = ""; }; F96D432A08F272B4004A47F5 /* tommath_superclass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tommath_superclass.h; sourceTree = ""; }; F96D432B08F272B4004A47F5 /* license.terms */ = {isa = PBXFileReference; explicitFileType = text; fileEncoding = 4; path = license.terms; sourceTree = ""; }; @@ -1480,7 +1478,6 @@ F96D42D308F272B3004A47F5 /* bn_s_mp_mul_digs.c */, F96D42D508F272B3004A47F5 /* bn_s_mp_sqr.c */, F96D42D608F272B3004A47F5 /* bn_s_mp_sub.c */, - F96D42D708F272B3004A47F5 /* bncore.c */, F96D432908F272B4004A47F5 /* tommath_class.h */, F96D432A08F272B4004A47F5 /* tommath_superclass.h */, ); @@ -2111,7 +2108,6 @@ F96D495108F272C3004A47F5 /* bn_s_mp_mul_digs.c in Sources */, F96D495308F272C3004A47F5 /* bn_s_mp_sqr.c in Sources */, F96D495408F272C3004A47F5 /* bn_s_mp_sub.c in Sources */, - F96D495508F272C3004A47F5 /* bncore.c in Sources */, F96D49A908F272C4004A47F5 /* tclMacOSXBundle.c in Sources */, F96D49AD08F272C4004A47F5 /* tclMacOSXFCmd.c in Sources */, F96D49AE08F272C4004A47F5 /* tclMacOSXNotify.c in Sources */, diff --git a/unix/Makefile.in b/unix/Makefile.in index 1610962..bdab85a 100644 --- a/unix/Makefile.in +++ b/unix/Makefile.in @@ -315,7 +315,7 @@ GENERIC_OBJS = regcomp.o regexec.o regfree.o regerror.o tclAlloc.o \ OO_OBJS = tclOO.o tclOOBasic.o tclOOCall.o tclOODefineCmds.o tclOOInfo.o \ tclOOMethod.o tclOOStubInit.o -TOMMATH_OBJS = bncore.o bn_reverse.o bn_fast_s_mp_mul_digs.o \ +TOMMATH_OBJS = bn_reverse.o bn_fast_s_mp_mul_digs.o \ bn_fast_s_mp_sqr.o bn_mp_add.o bn_mp_and.o \ bn_mp_add_d.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o \ bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \ @@ -482,7 +482,6 @@ STUB_SRCS = \ $(GENERIC_DIR)/tclOOStubLib.c TOMMATH_SRCS = \ - $(TOMMATH_DIR)/bncore.c \ $(TOMMATH_DIR)/bn_reverse.c \ $(TOMMATH_DIR)/bn_fast_s_mp_mul_digs.c \ $(TOMMATH_DIR)/bn_fast_s_mp_sqr.c \ @@ -1356,9 +1355,6 @@ tclThreadTest.o: $(GENERIC_DIR)/tclThreadTest.c tclTomMathInterface.o: $(GENERIC_DIR)/tclTomMathInterface.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclTomMathInterface.c -bncore.o: $(TOMMATH_DIR)/bncore.c $(MATHHDRS) - $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bncore.c - bn_reverse.o: $(TOMMATH_DIR)/bn_reverse.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_reverse.c diff --git a/win/Makefile.in b/win/Makefile.in index b983f0a..a427eee 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -311,7 +311,6 @@ GENERIC_OBJS = \ tclZlib.$(OBJEXT) TOMMATH_OBJS = \ - bncore.${OBJEXT} \ bn_reverse.${OBJEXT} \ bn_fast_s_mp_mul_digs.${OBJEXT} \ bn_fast_s_mp_sqr.${OBJEXT} \ diff --git a/win/makefile.vc b/win/makefile.vc index adfd7c4..2e0198e 100644 --- a/win/makefile.vc +++ b/win/makefile.vc @@ -251,7 +251,6 @@ ZLIBOBJS = \ $(TMP_DIR)\zutil.obj TOMMATHOBJS = \ - $(TMP_DIR)\bncore.obj \ $(TMP_DIR)\bn_reverse.obj \ $(TMP_DIR)\bn_fast_s_mp_mul_digs.obj \ $(TMP_DIR)\bn_fast_s_mp_sqr.obj \ -- cgit v0.12