diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2016-11-17 16:24:37 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2016-11-17 16:24:37 (GMT) |
commit | 205419d4b7c3801fe37ae41bceba18fc3b230308 (patch) | |
tree | 4e47d8e8427fcbad172ecc5ae3e1199345b1aef7 /generic | |
parent | 0be726feecac0c0515760b48dd64f435024dd908 (diff) | |
parent | 3aefaf62e4de2181582277bb60fe2a4ee5c506a2 (diff) | |
download | tcl-205419d4b7c3801fe37ae41bceba18fc3b230308.zip tcl-205419d4b7c3801fe37ae41bceba18fc3b230308.tar.gz tcl-205419d4b7c3801fe37ae41bceba18fc3b230308.tar.bz2 |
Fix libtommath's mp_radix_size() function such that it returns 2 for single-digit numbers. Add testcases for mp_radix_size() and mp_iseven(). Undo useless change in bn_mp_add_d.c (bring back libtommath's version).
Diffstat (limited to 'generic')
-rw-r--r-- | generic/tclObj.c | 8 | ||||
-rw-r--r-- | generic/tclTestObj.c | 49 |
2 files changed, 50 insertions, 7 deletions
diff --git a/generic/tclObj.c b/generic/tclObj.c index 29c8e23..45f79e4 100644 --- a/generic/tclObj.c +++ b/generic/tclObj.c @@ -3253,13 +3253,11 @@ UpdateStringOfBignum( if (status != MP_OKAY) { Tcl_Panic("radix size failure in UpdateStringOfBignum"); } - if (size == 3) { + if (size < 2) { /* - * mp_radix_size() returns 3 when more than INT_MAX bytes would be + * mp_radix_size() returns < 2 when more than INT_MAX bytes would be * needed to hold the string rep (because mp_radix_size ignores - * integer overflow issues). When we know the string rep will be more - * than 3, we can conclude the string rep would overflow our string - * length limits. + * integer overflow issues). * * Note that so long as we enforce our bignums to the size that fits * in a packed bignum, this branch will never be taken. diff --git a/generic/tclTestObj.c b/generic/tclTestObj.c index a637498..f7d2bae 100644 --- a/generic/tclTestObj.c +++ b/generic/tclTestObj.c @@ -152,10 +152,11 @@ TestbignumobjCmd( Tcl_Obj *const objv[]) /* Argument vector */ { const char *const subcmds[] = { - "set", "get", "mult10", "div10", NULL + "set", "get", "mult10", "div10", "iseven", "radixsize", NULL }; enum options { - BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10 + BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10, BIGNUM_ISEVEN, + BIGNUM_RADIXSIZE }; int index, varIndex; const char *string; @@ -274,6 +275,50 @@ TestbignumobjCmd( } else { SetVarToObj(varPtr, varIndex, Tcl_NewBignumObj(&newValue)); } + break; + + case BIGNUM_ISEVEN: + if (objc != 3) { + Tcl_WrongNumArgs(interp, 2, objv, "varIndex"); + return TCL_ERROR; + } + if (CheckIfVarUnset(interp, varPtr,varIndex)) { + return TCL_ERROR; + } + if (Tcl_GetBignumFromObj(interp, varPtr[varIndex], + &bignumValue) != TCL_OK) { + return TCL_ERROR; + } + if (!Tcl_IsShared(varPtr[varIndex])) { + Tcl_SetIntObj(varPtr[varIndex], mp_iseven(&bignumValue)); + } else { + SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(mp_iseven(&bignumValue))); + } + mp_clear(&bignumValue); + break; + + case BIGNUM_RADIXSIZE: + if (objc != 3) { + Tcl_WrongNumArgs(interp, 2, objv, "varIndex"); + return TCL_ERROR; + } + if (CheckIfVarUnset(interp, varPtr,varIndex)) { + return TCL_ERROR; + } + if (Tcl_GetBignumFromObj(interp, varPtr[varIndex], + &bignumValue) != TCL_OK) { + return TCL_ERROR; + } + if (mp_radix_size(&bignumValue, 10, &index) != MP_OKAY) { + return TCL_ERROR; + } + if (!Tcl_IsShared(varPtr[varIndex])) { + Tcl_SetIntObj(varPtr[varIndex], index); + } else { + SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(index)); + } + mp_clear(&bignumValue); + break; } Tcl_SetObjResult(interp, varPtr[varIndex]); |