add_n.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* mpn_add_n -- Add two limb vectors of equal, non-zero length.
  2. Copyright (C) 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA. */
  16. #include <config.h>
  17. #include "gmp-impl.h"
  18. mp_limb_t
  19. #if __STDC__
  20. mpn_add_n (mp_ptr res_ptr, mp_srcptr s1_ptr, mp_srcptr s2_ptr, mp_size_t size)
  21. #else
  22. mpn_add_n (res_ptr, s1_ptr, s2_ptr, size)
  23. register mp_ptr res_ptr;
  24. register mp_srcptr s1_ptr;
  25. register mp_srcptr s2_ptr;
  26. mp_size_t size;
  27. #endif
  28. {
  29. register mp_limb_t x, y, cy;
  30. register mp_size_t j;
  31. /* The loop counter and index J goes from -SIZE to -1. This way
  32. the loop becomes faster. */
  33. j = -size;
  34. /* Offset the base pointers to compensate for the negative indices. */
  35. s1_ptr -= j;
  36. s2_ptr -= j;
  37. res_ptr -= j;
  38. cy = 0;
  39. do
  40. {
  41. y = s2_ptr[j];
  42. x = s1_ptr[j];
  43. y += cy; /* add previous carry to one addend */
  44. cy = (y < cy); /* get out carry from that addition */
  45. y = x + y; /* add other addend */
  46. cy = (y < x) + cy; /* get out carry from that add, combine */
  47. res_ptr[j] = y;
  48. }
  49. while (++j != 0);
  50. return cy;
  51. }