From b3664a2eaad315a7b01931c974f24503ab1290cd Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 28 Mar 2019 21:44:39 +0000 Subject: Experiment: On platforms which support it (gcc), compiled libtommath with DIGIT_BIT=60 --- generic/tcl.h | 4 ---- generic/tclInt.h | 1 - generic/tclTest.c | 1 + generic/tclTomMath.h | 42 +++++++++--------------------------------- generic/tclTomMathStubLib.c | 1 + libtommath/tommath.h | 5 ----- libtommath/tommath_private.h | 9 +++++++++ 7 files changed, 20 insertions(+), 43 deletions(-) diff --git a/generic/tcl.h b/generic/tcl.h index c287a84..65fdab9 100644 --- a/generic/tcl.h +++ b/generic/tcl.h @@ -2202,10 +2202,6 @@ typedef void (Tcl_LimitHandlerDeleteProc) (ClientData clientData); typedef struct mp_int mp_int; #define MP_INT_DECLARED -typedef unsigned int mp_digit; -#define MP_DIGIT_DECLARED -typedef unsigned TCL_WIDE_INT_TYPE mp_word; -#define MP_WORD_DECLARED /* *---------------------------------------------------------------------------- diff --git a/generic/tclInt.h b/generic/tclInt.h index 9fc778b..e6b211c 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -5050,7 +5050,6 @@ typedef struct NRE_callback { #include "tclIntDecls.h" #include "tclIntPlatDecls.h" -#include "tclTomMathDecls.h" #if !defined(USE_TCL_STUBS) && !defined(TCL_MEM_DEBUG) #define Tcl_AttemptAlloc(size) TclpAlloc(size) diff --git a/generic/tclTest.c b/generic/tclTest.c index 172b56e..dde4496 100644 --- a/generic/tclTest.c +++ b/generic/tclTest.c @@ -20,6 +20,7 @@ # define USE_TCL_STUBS #endif #include "tclInt.h" +#include "tclTomMath.h" #include "tclOO.h" #include diff --git a/generic/tclTomMath.h b/generic/tclTomMath.h index 3f23fd6..180de77 100644 --- a/generic/tclTomMath.h +++ b/generic/tclTomMath.h @@ -7,13 +7,11 @@ * Michael Fromberger but has been written from scratch with * additional optimizations in place. * - * The library is free for all purposes without any express - * guarantee it works. + * SPDX-License-Identifier: Unlicense */ #ifndef BN_H_ #define BN_H_ -#include "tclTomMathDecls.h" #ifndef MODULE_SCOPE #define MODULE_SCOPE extern #endif @@ -30,7 +28,12 @@ extern "C" { #endif /* detect 64-bit mode if possible */ -#if defined(NEVER) +#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \ + defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \ + defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \ + defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \ + defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \ + defined(__LP64__) || defined(_LP64) || defined(__64BIT__) # if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT)) # if defined(__GNUC__) /* we support 128bit integers only via: __attribute__((mode(TI))) */ @@ -45,57 +48,31 @@ extern "C" { /* some default configurations. * * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits - * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits * * At the very least a mp_digit must be able to hold 7 bits * [any size beyond that is ok provided it doesn't overflow the data type] */ #ifdef MP_8BIT -#ifndef MP_DIGIT_DECLARED typedef unsigned char mp_digit; -#define MP_DIGIT_DECLARED -#endif -#ifndef MP_WORD_DECLARED -typedef unsigned short mp_word; -#define MP_WORD_DECLARED -#endif # define MP_SIZEOF_MP_DIGIT 1 # ifdef DIGIT_BIT # error You must not define DIGIT_BIT when using MP_8BIT # endif #elif defined(MP_16BIT) -#ifndef MP_DIGIT_DECLARED typedef unsigned short mp_digit; -#define MP_DIGIT_DECLARED -#endif -#ifndef MP_WORD_DECLARED -typedef unsigned int mp_word; -#define MP_WORD_DECLARED -#endif # define MP_SIZEOF_MP_DIGIT 2 # ifdef DIGIT_BIT # error You must not define DIGIT_BIT when using MP_16BIT # endif #elif defined(MP_64BIT) /* for GCC only on supported platforms */ -#ifndef MP_DIGIT_DECLARED typedef unsigned long long mp_digit; -#define MP_DIGIT_DECLARED -#endif -typedef unsigned long mp_word __attribute__((mode(TI))); # define DIGIT_BIT 60 #else /* this is the default case, 28-bit digits */ /* this is to make porting into LibTomCrypt easier :-) */ -#ifndef MP_DIGIT_DECLARED typedef unsigned int mp_digit; -#define MP_DIGIT_DECLARED -#endif -#ifndef MP_WORD_DECLARED -typedef unsigned long long mp_word; -#define MP_WORD_DECLARED -#endif # ifdef MP_31BIT /* this is an extension that uses 31-bit digits */ @@ -151,9 +128,6 @@ typedef int mp_err; # 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 */ #ifndef MP_INT_DECLARED #define MP_INT_DECLARED @@ -804,6 +778,8 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream); #define mp_todecimal(M, S) mp_toradix((M), (S), 10) #define mp_tohex(M, S) mp_toradix((M), (S), 16) +#include "tclTomMathDecls.h" + #ifdef __cplusplus } #endif diff --git a/generic/tclTomMathStubLib.c b/generic/tclTomMathStubLib.c index 324f2a3..1cff1ca 100644 --- a/generic/tclTomMathStubLib.c +++ b/generic/tclTomMathStubLib.c @@ -12,6 +12,7 @@ */ #include "tclInt.h" +#include "tclTomMath.h" MODULE_SCOPE const TclTomMathStubs *tclTomMathStubsPtr; diff --git a/libtommath/tommath.h b/libtommath/tommath.h index b5faf8c..66e2d8c 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -50,21 +50,18 @@ typedef unsigned long long Tcl_WideUInt; /* some default configurations. * * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits - * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits * * At the very least a mp_digit must be able to hold 7 bits * [any size beyond that is ok provided it doesn't overflow the data type] */ #ifdef MP_8BIT typedef unsigned char mp_digit; -typedef unsigned short mp_word; # define MP_SIZEOF_MP_DIGIT 1 # ifdef DIGIT_BIT # error You must not define DIGIT_BIT when using MP_8BIT # endif #elif defined(MP_16BIT) typedef unsigned short mp_digit; -typedef unsigned int mp_word; # define MP_SIZEOF_MP_DIGIT 2 # ifdef DIGIT_BIT # error You must not define DIGIT_BIT when using MP_16BIT @@ -72,14 +69,12 @@ typedef unsigned int mp_word; #elif defined(MP_64BIT) /* for GCC only on supported platforms */ typedef unsigned long long mp_digit; -typedef unsigned long mp_word __attribute__((mode(TI))); # define DIGIT_BIT 60 #else /* this is the default case, 28-bit digits */ /* this is to make porting into LibTomCrypt easier :-) */ typedef unsigned int mp_digit; -typedef unsigned long long mp_word; # ifdef MP_31BIT /* this is an extension that uses 31-bit digits */ diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index 2096f77..e1ad45d 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -49,6 +49,15 @@ extern void *XREALLOC(void *p, size_t n); extern void XFREE(void *p); #endif +#if defined(MP_64BIT) +/* for GCC only on supported platforms */ +typedef unsigned long mp_word __attribute__((mode(TI))); +#elif _WIN32 +typedef __int64 mp_word; +#else +typedef unsigned long long mp_word; +#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. */ -- cgit v0.12 From 3664f127cd17cdcb246dcbe861414b231ddff55e Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 28 Mar 2019 22:42:56 +0000 Subject: More complete typedef for mp_word, for MP_8BIT and MP_16BIT as well. --- libtommath/tommath_private.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index e1ad45d..faecb9f 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -49,11 +49,15 @@ extern void *XREALLOC(void *p, size_t n); extern void XFREE(void *p); #endif -#if defined(MP_64BIT) +#if defined(MP_8BIT) +typedef unsigned short mp_word; +#elif defined(MP_16BIT) +typedef unsigned int mp_word; +#elif defined(MP_64BIT) /* for GCC only on supported platforms */ typedef unsigned long mp_word __attribute__((mode(TI))); #elif _WIN32 -typedef __int64 mp_word; +typedef unsigned __int64 mp_word; #else typedef unsigned long long mp_word; #endif -- cgit v0.12 From 7d8a7adc9fda349a9676b23e95dc03fb6af56f93 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Sun, 31 Mar 2019 20:45:46 +0000 Subject: (temporary workaround): Mark expr-47.12 and expr-47.13 as nonPortable: Those are the test-cases failing with DIGIT_BIT=60 on MacOSX but not on other platforms. To be investigated further! --- tests/expr.test | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/expr.test b/tests/expr.test index cb0c24d..921ab17 100644 --- a/tests/expr.test +++ b/tests/expr.test @@ -7087,7 +7087,7 @@ test expr-47.11 {isqrt of zero} { expr {isqrt(0)} } 0 -test expr-47.12 {isqrt of various sizes of integer} { +test expr-47.12 {isqrt of various sizes of integer} -constraints knownBug -body { set faults 0 set trouble {} for {set i 0} {$faults < 10 && $i <= 1024} {incr i} { @@ -7114,9 +7114,9 @@ test expr-47.12 {isqrt of various sizes of integer} { } } set trouble -} {} +} -result {} -test expr-47.13 {isqrt and floating point rounding (Bug 2143288)} { +test expr-47.13 {isqrt and floating point rounding (Bug 2143288)} -constraints knownBug -body { set trouble {} set faults 0 for {set i 0} {$i < 29 && $faults < 10} {incr i} { @@ -7134,7 +7134,7 @@ test expr-47.13 {isqrt and floating point rounding (Bug 2143288)} { } } set trouble -} {} +} -result {} test expr-48.1 {Bug 1770224} { expr {-0x8000000000000001 >> 0x8000000000000000} -- cgit v0.12 From d5c3b7ac251d7793f9aff83b47608bf578aceb5f Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 10 Apr 2019 19:53:39 +0000 Subject: Complete implementation for MSVC (even building minigzip) --- compat/zlib/contrib/minizip/ioapi.c | 6 +++++- compat/zlib/contrib/minizip/minizip.c | 6 +++++- generic/tclStubInit.c | 26 +++++++++++++------------- unix/configure | 8 +++++++- unix/configure.ac | 4 +++- win/configure | 5 ++++- win/configure.ac | 3 ++- 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/compat/zlib/contrib/minizip/ioapi.c b/compat/zlib/contrib/minizip/ioapi.c index 7f5c191..8274a3d 100644 --- a/compat/zlib/contrib/minizip/ioapi.c +++ b/compat/zlib/contrib/minizip/ioapi.c @@ -14,7 +14,11 @@ #define _CRT_SECURE_NO_WARNINGS #endif -#if defined(__APPLE__) || defined(IOAPI_NO_64) +#if defined(_WIN32) +#define FOPEN_FUNC(filename, mode) fopen(filename, mode) +#define FTELLO_FUNC(stream) _ftelli64(stream) +#define FSEEKO_FUNC(stream, offset, origin) _fseeki64(stream, offset, origin) +#elif defined(__APPLE__) || defined(IOAPI_NO_64) // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions #define FOPEN_FUNC(filename, mode) fopen(filename, mode) #define FTELLO_FUNC(stream) ftello(stream) diff --git a/compat/zlib/contrib/minizip/minizip.c b/compat/zlib/contrib/minizip/minizip.c index 2dd9f10..2c7ffb1 100644 --- a/compat/zlib/contrib/minizip/minizip.c +++ b/compat/zlib/contrib/minizip/minizip.c @@ -27,7 +27,11 @@ #endif #endif -#if defined(__APPLE__) || defined(IOAPI_NO_64) +#if defined(_WIN32) +#define FOPEN_FUNC(filename, mode) fopen(filename, mode) +#define FTELLO_FUNC(stream) _ftelli64(stream) +#define FSEEKO_FUNC(stream, offset, origin) _fseeki64(stream, offset, origin) +#elif defined(__APPLE__) || defined(IOAPI_NO_64) // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions #define FOPEN_FUNC(filename, mode) fopen(filename, mode) #define FTELLO_FUNC(stream) ftello(stream) diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index 5405ec4..9719390 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -138,19 +138,7 @@ #define TclBN_mp_tc_div_2d mp_tc_div_2d #define TclBN_mp_get_bit mp_get_bit -#if !defined(TCL_NO_DEPRECATED) && (!defined(_WIN32) || defined(STATIC_BUILD)) -#define TclBN_reverse bn_reverse -#define TclBN_fast_s_mp_mul_digs fast_s_mp_mul_digs -#define TclBN_fast_s_mp_sqr fast_s_mp_sqr -#define TclBN_mp_karatsuba_mul mp_karatsuba_mul -#define TclBN_mp_karatsuba_sqr mp_karatsuba_sqr -#define TclBN_mp_toom_mul mp_toom_mul -#define TclBN_mp_toom_sqr mp_toom_sqr -#define TclBN_s_mp_add s_mp_add -#define TclBN_s_mp_mul_digs s_mp_mul_digs -#define TclBN_s_mp_sqr s_mp_sqr -#define TclBN_s_mp_sub s_mp_sub -#else +#if defined(TCL_NO_DEPRECATED) || defined(TCL_WITH_EXTERNAL_TOMMATH) #define TclBN_reverse 0 #define TclBN_fast_s_mp_mul_digs 0 #define TclBN_fast_s_mp_sqr 0 @@ -162,6 +150,18 @@ #define TclBN_s_mp_mul_digs 0 #define TclBN_s_mp_sqr 0 #define TclBN_s_mp_sub 0 +#else +#define TclBN_reverse bn_reverse +#define TclBN_fast_s_mp_mul_digs fast_s_mp_mul_digs +#define TclBN_fast_s_mp_sqr fast_s_mp_sqr +#define TclBN_mp_karatsuba_mul mp_karatsuba_mul +#define TclBN_mp_karatsuba_sqr mp_karatsuba_sqr +#define TclBN_mp_toom_mul mp_toom_mul +#define TclBN_mp_toom_sqr mp_toom_sqr +#define TclBN_s_mp_add s_mp_add +#define TclBN_s_mp_mul_digs s_mp_mul_digs +#define TclBN_s_mp_sqr s_mp_sqr +#define TclBN_s_mp_sub s_mp_sub #endif /* See bug 510001: TclSockMinimumBuffers needs plat imp */ diff --git a/unix/configure b/unix/configure index d310777..ea625ca 100755 --- a/unix/configure +++ b/unix/configure @@ -4756,7 +4756,13 @@ else fi fi -if test $libtommath_ok = no; then : +if test $libtommath_ok = yes; then : + + +$as_echo "#define TCL_WITH_EXTERNAL_TOMMATH 1" >>confdefs.h + + +else TOMMATH_OBJS=\${TOMMATH_OBJS} diff --git a/unix/configure.ac b/unix/configure.ac index a2ebecf..5542b7f 100644 --- a/unix/configure.ac +++ b/unix/configure.ac @@ -178,7 +178,9 @@ AS_IF([test $libtommath_ok = yes], [ AC_SEARCH_LIBS([mp_get_bit],[tommath],[],[ libtommath_ok=no ])]) -AS_IF([test $libtommath_ok = no], [ +AS_IF([test $libtommath_ok = yes], [ + AC_DEFINE(TCL_WITH_EXTERNAL_TOMMATH, 1, [Tcl with external libtommath]) +], [ AC_SUBST(TOMMATH_OBJS,[\${TOMMATH_OBJS}]) AC_SUBST(TOMMATH_SRCS,[\${TOMMATH_SRCS}]) AC_SUBST(TOMMATH_INCLUDE,[-I\${TOMMATH_DIR}]) diff --git a/win/configure b/win/configure index 29dc6d6..0f81138 100755 --- a/win/configure +++ b/win/configure @@ -4597,10 +4597,13 @@ if test "$tcl_ok" = "yes"; then : TOMMATH_DLL_FILE=\${TOMMATH_DLL_FILE} + +$as_echo "#define TCL_WITH_EXTERNAL_TOMMATH 1" >>confdefs.h + if test "$do64bit" != "no"; then : -$as_echo "#define MD_64BIT 1" >>confdefs.h +$as_echo "#define MP_64BIT 1" >>confdefs.h if test "$GCC" == "yes"; then : diff --git a/win/configure.ac b/win/configure.ac index 3edfe78..37a0998 100644 --- a/win/configure.ac +++ b/win/configure.ac @@ -124,8 +124,9 @@ AS_IF([test "${enable_shared+set}" = "set"], [ AS_IF([test "$tcl_ok" = "yes"], [ AC_SUBST(ZLIB_DLL_FILE,[\${ZLIB_DLL_FILE}]) AC_SUBST(TOMMATH_DLL_FILE,[\${TOMMATH_DLL_FILE}]) + AC_DEFINE(TCL_WITH_EXTERNAL_TOMMATH, 1, [Tcl with external libtommath]) AS_IF([test "$do64bit" != "no"], [ - AC_DEFINE(MD_64BIT, 1, [Using libtommath.dll in 64-bit mode]) + AC_DEFINE(MP_64BIT, 1, [Using libtommath.dll in 64-bit mode]) AS_IF([test "$GCC" == "yes"],[ AC_SUBST(ZLIB_LIBS,[\${ZLIB_DIR_NATIVE}/win64/libz.dll.a]) AC_SUBST(TOMMATH_LIBS,[\${TOMMATH_DIR_NATIVE}/win64/libtommath.dll.a]) -- cgit v0.12