Tcl Source Code

Check-in [dc9f2eaf23]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:
* libtommath/bn_mp_add_d.c: Bug fix. For mp_add_d(&a, d, &c), when &a has the value -d, then the value &c computed should be zero, but mp_add_d was producing an inconsistent zero value with a sign field of MP_NEG, something like a value of -0, which other routines in libtommath can't handle.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | kennykb-numerics-branch
Files: files | file ages | folders
SHA1:dc9f2eaf23c8e0903a557e562261332d129d4e57
User & Date: dgp 2005-09-16 16:13:16
Context
2005-09-16
19:29
[kennykb-numerics-branch]
* generic/tclInt.h: Added TclBNInitBigNumFrom...
check-in: 6e1addf33e user: dgp tags: kennykb-numerics-branch
16:13
* libtommath/bn_mp_add_d.c: Bug fix. For mp_add_d(&a, d, &c), when &a has the v...
check-in: dc9f2eaf23 user: dgp tags: kennykb-numerics-branch
15:35
[kennykb-numerics-branch]
* generic/tclTomMath.h: Added mp_cmp_d to routines from ...
check-in: f5324453ec user: dgp tags: kennykb-numerics-branch
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ChangeLog.

2
3
4
5
6
7
8






9
10
11
12
13
14
15

	[kennykb-numerics-branch]

	* generic/tclTomMath.h:	Added mp_cmp_d to routines from
	* unix/Makefile.in:	libtommath used by Tcl.
	* win/Makefile.in:
	* win/makefile.vc:







	* generic/tclExecute.c:	Dropped all creation of "bigOne" values
	and just use tommath routines that accept the value "1" directly.

2005-09-15  Don Porter  <dgp@users.sourceforge.net>

	[kennykb-numerics-branch]	Merge updates from HEAD.







>
>
>
>
>
>







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

	[kennykb-numerics-branch]

	* generic/tclTomMath.h:	Added mp_cmp_d to routines from
	* unix/Makefile.in:	libtommath used by Tcl.
	* win/Makefile.in:
	* win/makefile.vc:

	* libtommath/bn_mp_add_d.c:	Bug fix.  For mp_add_d(&a, d, &c),
	when &a has the value -d, then the value &c computed should be zero,
	but mp_add_d was producing an inconsistent zero value with a sign
	field of MP_NEG, something like a value of -0, which other routines
	in libtommath can't handle.

	* generic/tclExecute.c:	Dropped all creation of "bigOne" values
	and just use tommath routines that accept the value "1" directly.

2005-09-15  Don Porter  <dgp@users.sourceforge.net>

	[kennykb-numerics-branch]	Merge updates from HEAD.

Added libtommath/bn_mp_add_d.c.





















































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <tommath.h>
#ifdef BN_MP_ADD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
 * LibTomMath is a library that provides multiple-precision
 * integer arithmetic as well as number theoretic functionality.
 *
 * The library was designed directly after the MPI library by
 * 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.
 *
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
 */

/* single digit addition */
int
mp_add_d (mp_int * a, mp_digit b, mp_int * c)
{
  int     res, ix, oldused;
  mp_digit *tmpa, *tmpc, mu;

  /* grow c as required */
  if (c->alloc < a->used + 1) {
     if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
        return res;
     }
  }

  /* if a is negative and |a| >= b, call c = |a| - b */
  if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) {
     /* temporarily fix sign of a */
     a->sign = MP_ZPOS;

     /* c = |a| - b */
     res = mp_sub_d(a, b, c);

     /* fix signs  */
     a->sign = MP_NEG;
     c->sign = (c->used) ? MP_NEG : MP_ZPOS;

     return res;
  }

  /* old number of used digits in c */
  oldused = c->used;

  /* sign always positive */
  c->sign = MP_ZPOS;

  /* source alias */
  tmpa    = a->dp;

  /* destination alias */
  tmpc    = c->dp;

  /* if a is positive */
  if (a->sign == MP_ZPOS) {
     /* add digit, after this we're propagating
      * the carry.
      */
     *tmpc   = *tmpa++ + b;
     mu      = *tmpc >> DIGIT_BIT;
     *tmpc++ &= MP_MASK;

     /* now handle rest of the digits */
     for (ix = 1; ix < a->used; ix++) {
        *tmpc   = *tmpa++ + mu;
        mu      = *tmpc >> DIGIT_BIT;
        *tmpc++ &= MP_MASK;
     }
     /* set final carry */
     ix++;
     *tmpc++  = mu;

     /* setup size */
     c->used = a->used + 1;
  } else {
     /* a was negative and |a| < b */
     c->used  = 1;

     /* the result is a single digit */
     if (a->used == 1) {
        *tmpc++  =  b - a->dp[0];
     } else {
        *tmpc++  =  b;
     }

     /* setup count so the clearing of oldused
      * can fall through correctly
      */
     ix       = 1;
  }

  /* now zero to oldused */
  while (ix++ < oldused) {
     *tmpc++ = 0;
  }
  mp_clamp(c);

  return MP_OKAY;
}

#endif