summaryrefslogtreecommitdiffstats
path: root/generic/tclTomMathInterface.c
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2017-09-13 10:14:31 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2017-09-13 10:14:31 (GMT)
commit9d6ae5462247cf6c5ce043e4689c3e5210628e93 (patch)
tree6461d7a562bf2e681c2cf49147418c2349f58c07 /generic/tclTomMathInterface.c
parentb9e8eecd80020e9dc1539f6f99f121472aff3126 (diff)
downloadtcl-9d6ae5462247cf6c5ce043e4689c3e5210628e93.zip
tcl-9d6ae5462247cf6c5ce043e4689c3e5210628e93.tar.gz
tcl-9d6ae5462247cf6c5ce043e4689c3e5210628e93.tar.bz2
Make mp_get_long and mp_set_long available to tommath-enabled Tcl extensions. Deprecate the internal TclBNInitBignumFrom* functions, in favor of the official tommath functions with the same purpose.
Diffstat (limited to 'generic/tclTomMathInterface.c')
-rw-r--r--generic/tclTomMathInterface.c89
1 files changed, 22 insertions, 67 deletions
diff --git a/generic/tclTomMathInterface.c b/generic/tclTomMathInterface.c
index 48db8c3..78888a7 100644
--- a/generic/tclTomMathInterface.c
+++ b/generic/tclTomMathInterface.c
@@ -168,7 +168,7 @@ TclBNFree(
/*
*----------------------------------------------------------------------
*
- * TclBNInitBignumFromLong --
+ * TclInitBignumFromLong --
*
* Allocate and initialize a 'bignum' from a native 'long'.
*
@@ -181,47 +181,20 @@ TclBNFree(
*----------------------------------------------------------------------
*/
-extern void
-TclBNInitBignumFromLong(
+void
+TclInitBignumFromLong(
mp_int *a,
- long initVal)
+ long v)
{
- int status;
- unsigned long v;
- mp_digit *p;
-
- /*
- * Allocate enough memory to hold the largest possible long
- */
-
- status = mp_init_size(a,
- (CHAR_BIT * sizeof(long) + DIGIT_BIT - 1) / DIGIT_BIT);
- if (status != MP_OKAY) {
+ if (mp_init_size(a, (CHAR_BIT * sizeof(long) + DIGIT_BIT - 1) / DIGIT_BIT) != MP_OKAY) {
Tcl_Panic("initialization failure in TclBNInitBignumFromLong");
}
-
- /*
- * Convert arg to sign and magnitude.
- */
-
- if (initVal < 0) {
- a->sign = MP_NEG;
- v = -initVal;
+ if (v < (long)0) {
+ mp_set_long_long(a, (Tcl_WideUInt)(-(Tcl_WideInt)v));
+ mp_neg(a, a);
} else {
- a->sign = MP_ZPOS;
- v = initVal;
- }
-
- /*
- * Store the magnitude in the bignum.
- */
-
- p = a->dp;
- while (v) {
- *p++ = (mp_digit) (v & MP_MASK);
- v >>= MP_DIGIT_BIT;
+ mp_set_long_long(a, (Tcl_WideUInt)v);
}
- a->used = p - a->dp;
}
/*
@@ -240,16 +213,19 @@ TclBNInitBignumFromLong(
*----------------------------------------------------------------------
*/
-extern void
-TclBNInitBignumFromWideInt(
+void
+TclInitBignumFromWideInt(
mp_int *a, /* Bignum to initialize */
Tcl_WideInt v) /* Initial value */
{
+ if (mp_init_size(a, (CHAR_BIT * sizeof(Tcl_WideUInt) + DIGIT_BIT - 1) / DIGIT_BIT) != MP_OKAY) {
+ Tcl_Panic("initialization failure in TclBNInitBignumFromWideInt");
+ }
if (v < (Tcl_WideInt)0) {
- TclBNInitBignumFromWideUInt(a, (Tcl_WideUInt)(-v));
+ mp_set_long_long(a, (Tcl_WideUInt)(-v));
mp_neg(a, a);
} else {
- TclBNInitBignumFromWideUInt(a, (Tcl_WideUInt)v);
+ mp_set_long_long(a, (Tcl_WideUInt)v);
}
}
@@ -269,36 +245,15 @@ TclBNInitBignumFromWideInt(
*----------------------------------------------------------------------
*/
-extern void
-TclBNInitBignumFromWideUInt(
+void
+TclInitBignumFromWideUInt(
mp_int *a, /* Bignum to initialize */
Tcl_WideUInt v) /* Initial value */
{
- int status;
- mp_digit *p;
-
- /*
- * Allocate enough memory to hold the largest possible Tcl_WideUInt.
- */
-
- status = mp_init_size(a,
- (CHAR_BIT * sizeof(Tcl_WideUInt) + DIGIT_BIT - 1) / DIGIT_BIT);
- if (status != MP_OKAY) {
- Tcl_Panic("initialization failure in TclBNInitBignumFromWideUInt");
- }
-
- a->sign = MP_ZPOS;
-
- /*
- * Store the magnitude in the bignum.
- */
-
- p = a->dp;
- while (v) {
- *p++ = (mp_digit) (v & MP_MASK);
- v >>= MP_DIGIT_BIT;
- }
- a->used = p - a->dp;
+ if (mp_init_size(a, (CHAR_BIT * sizeof(Tcl_WideUInt) + DIGIT_BIT - 1) / DIGIT_BIT) != MP_OKAY) {
+ Tcl_Panic("initialization failure in TclBNInitBignumFromWideUInt");
+ }
+ mp_set_long_long(a, v);
}
/*