sum.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /****************************************************************
  2. The author of this software is David M. Gay.
  3. Copyright (C) 1998 by Lucent Technologies
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and
  6. its documentation for any purpose and without fee is hereby
  7. granted, provided that the above copyright notice appear in all
  8. copies and that both that the copyright notice and this
  9. permission notice and warranty disclaimer appear in supporting
  10. documentation, and that the name of Lucent or any of its entities
  11. not be used in advertising or publicity pertaining to
  12. distribution of the software without specific, written prior
  13. permission.
  14. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  17. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  19. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  21. THIS SOFTWARE.
  22. ****************************************************************/
  23. /* Please send bug reports to David M. Gay (dmg at acm dot org,
  24. * with " at " changed at "@" and " dot " changed to "."). */
  25. #include "gdtoaimp.h"
  26. Bigint *
  27. #ifdef KR_headers
  28. sum(a, b) Bigint *a; Bigint *b;
  29. #else
  30. sum(Bigint *a, Bigint *b)
  31. #endif
  32. {
  33. Bigint *c;
  34. ULong carry, *xc, *xa, *xb, *xe, y;
  35. #ifdef Pack_32
  36. ULong z;
  37. #endif
  38. if (a->wds < b->wds) {
  39. c = b; b = a; a = c;
  40. }
  41. c = Balloc(a->k);
  42. if (c == NULL)
  43. return (NULL);
  44. c->wds = a->wds;
  45. carry = 0;
  46. xa = a->x;
  47. xb = b->x;
  48. xc = c->x;
  49. xe = xc + b->wds;
  50. #ifdef Pack_32
  51. do {
  52. y = (*xa & 0xffff) + (*xb & 0xffff) + carry;
  53. carry = (y & 0x10000) >> 16;
  54. z = (*xa++ >> 16) + (*xb++ >> 16) + carry;
  55. carry = (z & 0x10000) >> 16;
  56. Storeinc(xc, z, y);
  57. }
  58. while(xc < xe);
  59. xe += a->wds - b->wds;
  60. while(xc < xe) {
  61. y = (*xa & 0xffff) + carry;
  62. carry = (y & 0x10000) >> 16;
  63. z = (*xa++ >> 16) + carry;
  64. carry = (z & 0x10000) >> 16;
  65. Storeinc(xc, z, y);
  66. }
  67. #else
  68. do {
  69. y = *xa++ + *xb++ + carry;
  70. carry = (y & 0x10000) >> 16;
  71. *xc++ = y & 0xffff;
  72. }
  73. while(xc < xe);
  74. xe += a->wds - b->wds;
  75. while(xc < xe) {
  76. y = *xa++ + carry;
  77. carry = (y & 0x10000) >> 16;
  78. *xc++ = y & 0xffff;
  79. }
  80. #endif
  81. if (carry) {
  82. if (c->wds == c->maxwds) {
  83. b = Balloc(c->k + 1);
  84. if (b == NULL)
  85. return (NULL);
  86. Bcopy(b, c);
  87. Bfree(c);
  88. c = b;
  89. }
  90. c->x[c->wds++] = 1;
  91. }
  92. return c;
  93. }