From 25a1f3d894e556ad09ff624fd69db9b9d8b98369 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Tue, 22 Jan 2019 21:44:27 +0000 Subject: One more libtommath function, mp_tc_div_2d, which simplifies code. Some more code readability improvements. --- generic/tclExecute.c | 21 ++++++--------------- generic/tclStubInit.c | 1 + generic/tclTomMath.decls | 4 ++++ generic/tclTomMathDecls.h | 6 ++++++ generic/tclUtil.c | 19 ++++++++----------- unix/Makefile.in | 6 +++++- win/Makefile.in | 1 + win/makefile.vc | 1 + 8 files changed, 32 insertions(+), 27 deletions(-) diff --git a/generic/tclExecute.c b/generic/tclExecute.c index c7cd9fc..a433a92 100644 --- a/generic/tclExecute.c +++ b/generic/tclExecute.c @@ -1830,7 +1830,7 @@ TclIncrObj( return TCL_ERROR; } - if ((type1 != TCL_NUMBER_BIG) && (type2 != TCL_NUMBER_BIG)) { + if ((type1 == TCL_NUMBER_INT) && (type2 == TCL_NUMBER_INT)) { Tcl_WideInt w1, w2, sum; w1 = *((const Tcl_WideInt *)ptr1); @@ -7888,7 +7888,7 @@ ExecuteExtendedBinaryMathOp( return constants[0]; } - if (type2 != TCL_NUMBER_BIG) { + if (type2 == TCL_NUMBER_INT) { Tcl_WideInt wQuotient, wRemainder; w2 = *((const Tcl_WideInt *)ptr2); wQuotient = w1 / w2; @@ -8010,7 +8010,7 @@ ExecuteExtendedBinaryMathOp( * Handle shifts within the native wide range. */ - if ((type1 != TCL_NUMBER_BIG) + if ((type1 == TCL_NUMBER_INT) && ((size_t)shift < CHAR_BIT*sizeof(Tcl_WideInt))) { w1 = *((const Tcl_WideInt *)ptr1); if (!((w1>0 ? w1 : ~w1) @@ -8076,16 +8076,7 @@ ExecuteExtendedBinaryMathOp( if (opcode == INST_LSHIFT) { mp_mul_2d(&big1, shift, &bigResult); } else { - mp_init(&bigRemainder); - mp_div_2d(&big1, shift, &bigResult, &bigRemainder); - if (mp_isneg(&bigRemainder)) { - /* - * Convert to Tcl's integer division rules. - */ - - mp_sub_d(&bigResult, 1, &bigResult); - } - mp_clear(&bigRemainder); + mp_tc_div_2d(&big1, shift, &bigResult); } mp_clear(&big1); BIG_RESULT(&bigResult); @@ -8094,7 +8085,7 @@ ExecuteExtendedBinaryMathOp( case INST_BITOR: case INST_BITXOR: case INST_BITAND: - if ((type1 == TCL_NUMBER_BIG) || (type2 == TCL_NUMBER_BIG)) { + if ((type1 != TCL_NUMBER_INT) || (type2 != TCL_NUMBER_INT)) { Tcl_TakeBignumFromObj(NULL, valuePtr, &big1); Tcl_TakeBignumFromObj(NULL, value2Ptr, &big2); @@ -8473,7 +8464,7 @@ ExecuteExtendedBinaryMathOp( #endif DOUBLE_RESULT(dResult); } - if ((type1 != TCL_NUMBER_BIG) && (type2 != TCL_NUMBER_BIG)) { + if ((type1 == TCL_NUMBER_INT) && (type2 == TCL_NUMBER_INT)) { w1 = *((const Tcl_WideInt *)ptr1); w2 = *((const Tcl_WideInt *)ptr2); diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index a545cd0..fa3f734 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -1016,6 +1016,7 @@ const TclTomMathStubs tclTomMathStubs = { TclBN_mp_tc_and, /* 73 */ TclBN_mp_tc_or, /* 74 */ TclBN_mp_tc_xor, /* 75 */ + TclBN_mp_tc_div_2d, /* 76 */ }; static const TclStubHooks tclStubHooks = { diff --git a/generic/tclTomMath.decls b/generic/tclTomMath.decls index 075dcfc..a6c3d5b 100644 --- a/generic/tclTomMath.decls +++ b/generic/tclTomMath.decls @@ -264,6 +264,10 @@ declare 74 { declare 75 { int TclBN_mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) } +declare 76 { + int TclBN_mp_tc_div_2d(const mp_int *a, int b, mp_int *c) +} + # Local Variables: # mode: tcl diff --git a/generic/tclTomMathDecls.h b/generic/tclTomMathDecls.h index da273de..1e402fd 100644 --- a/generic/tclTomMathDecls.h +++ b/generic/tclTomMathDecls.h @@ -108,6 +108,7 @@ #define mp_sub TclBN_mp_sub #define mp_sub_d TclBN_mp_sub_d #define mp_tc_and TclBN_mp_tc_and +#define mp_tc_div_2d TclBN_mp_tc_div_2d #define mp_tc_or TclBN_mp_tc_or #define mp_tc_xor TclBN_mp_tc_xor #define mp_to_unsigned_bin TclBN_mp_to_unsigned_bin @@ -337,6 +338,8 @@ EXTERN int TclBN_mp_tc_or(const mp_int *a, const mp_int *b, /* 75 */ EXTERN int TclBN_mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c); +/* 76 */ +EXTERN int TclBN_mp_tc_div_2d(const mp_int *a, int b, mp_int *c); typedef struct TclTomMathStubs { int magic; @@ -418,6 +421,7 @@ typedef struct TclTomMathStubs { int (*tclBN_mp_tc_and) (const mp_int *a, const mp_int *b, mp_int *c); /* 73 */ int (*tclBN_mp_tc_or) (const mp_int *a, const mp_int *b, mp_int *c); /* 74 */ int (*tclBN_mp_tc_xor) (const mp_int *a, const mp_int *b, mp_int *c); /* 75 */ + int (*tclBN_mp_tc_div_2d) (const mp_int *a, int b, mp_int *c); /* 76 */ } TclTomMathStubs; extern const TclTomMathStubs *tclTomMathStubsPtr; @@ -584,6 +588,8 @@ extern const TclTomMathStubs *tclTomMathStubsPtr; (tclTomMathStubsPtr->tclBN_mp_tc_or) /* 74 */ #define TclBN_mp_tc_xor \ (tclTomMathStubsPtr->tclBN_mp_tc_xor) /* 75 */ +#define TclBN_mp_tc_div_2d \ + (tclTomMathStubsPtr->tclBN_mp_tc_div_2d) /* 76 */ #endif /* defined(USE_TCL_STUBS) */ diff --git a/generic/tclUtil.c b/generic/tclUtil.c index 04fc2af..cf73a18 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -3719,18 +3719,15 @@ GetWideForIndex( *widePtr = *(Tcl_WideInt *)cd; return TCL_OK; } - if (numType == TCL_NUMBER_BIG) { - /* objPtr holds an integer outside the signed wide range */ - /* Truncate to the signed wide range. */ - if (mp_isneg((mp_int *)cd)) { - *widePtr = WIDE_MIN; - } else { - *widePtr = WIDE_MAX; - } - return TCL_OK; + if (numType != TCL_NUMBER_BIG) { + /* Must be a double -> not a valid index */ + goto parseError; } - /* Must be a double -> not a valid index */ - goto parseError; + + /* objPtr holds an integer outside the signed wide range */ + /* Truncate to the signed wide range. */ + *widePtr = mp_isneg((mp_int *)cd) ? WIDE_MIN : WIDE_MAX; + return TCL_OK; } /* objPtr does not hold a number, check the end+/- format... */ diff --git a/unix/Makefile.in b/unix/Makefile.in index b3f9a6b..d769f03 100644 --- a/unix/Makefile.in +++ b/unix/Makefile.in @@ -337,7 +337,7 @@ TOMMATH_OBJS = bncore.o bn_reverse.o bn_fast_s_mp_mul_digs.o \ bn_mp_read_radix.o bn_mp_rshd.o bn_mp_set.o bn_mp_set_int.o \ bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \ bn_mp_sqr.o bn_mp_sqrt.o bn_mp_sub.o bn_mp_sub_d.o \ - bn_mp_tc_and.o bn_mp_tc_or.o bn_mp_tc_xor.o \ + bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \ bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \ bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_toradix_n.o \ bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_s_mp_add.o \ @@ -549,6 +549,7 @@ TOMMATH_SRCS = \ $(TOMMATH_DIR)/bn_mp_sub.c \ $(TOMMATH_DIR)/bn_mp_sub_d.c \ $(TOMMATH_DIR)/bn_mp_tc_and.c \ + $(TOMMATH_DIR)/bn_mp_tc_div_2d.c \ $(TOMMATH_DIR)/bn_mp_tc_or.c \ $(TOMMATH_DIR)/bn_mp_tc_xor.c \ $(TOMMATH_DIR)/bn_mp_to_unsigned_bin.c \ @@ -1636,6 +1637,9 @@ bn_mp_sub_d.o: $(TOMMATH_DIR)/bn_mp_sub_d.c $(MATHHDRS) bn_mp_tc_and.o: $(TOMMATH_DIR)/bn_mp_tc_and.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_and.c +bn_mp_tc_div_2d.o: $(TOMMATH_DIR)/bn_mp_tc_div_2d.c $(MATHHDRS) + $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_div_2d.c + bn_mp_tc_or.o: $(TOMMATH_DIR)/bn_mp_tc_or.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_or.c diff --git a/win/Makefile.in b/win/Makefile.in index 024b9ac..f97582a 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -419,6 +419,7 @@ TOMMATH_OBJS = \ bn_mp_sub.${OBJEXT} \ bn_mp_sub_d.${OBJEXT} \ bn_mp_tc_and.${OBJEXT} \ + bn_mp_tc_div_2d.${OBJEXT} \ bn_mp_tc_or.${OBJEXT} \ bn_mp_tc_xor.${OBJEXT} \ bn_mp_to_unsigned_bin.${OBJEXT} \ diff --git a/win/makefile.vc b/win/makefile.vc index 2db1108..66a9fdf 100644 --- a/win/makefile.vc +++ b/win/makefile.vc @@ -311,6 +311,7 @@ TOMMATHOBJS = \ $(TMP_DIR)\bn_mp_sub.obj \ $(TMP_DIR)\bn_mp_sub_d.obj \ $(TMP_DIR)\bn_mp_tc_and.obj \ + $(TMP_DIR)\bn_mp_tc_div_2d.obj \ $(TMP_DIR)\bn_mp_tc_or.obj \ $(TMP_DIR)\bn_mp_tc_xor.obj \ $(TMP_DIR)\bn_mp_to_unsigned_bin.obj \ -- cgit v0.12