add_n.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* mpn_add_n -- Add two limb vectors of equal, non-zero length.
  2. Copyright (C) 1992-2019 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <gmp.h>
  16. #include "gmp-impl.h"
  17. mp_limb_t
  18. mpn_add_n (mp_ptr res_ptr, mp_srcptr s1_ptr, mp_srcptr s2_ptr, mp_size_t size)
  19. {
  20. register mp_limb_t x, y, cy;
  21. register mp_size_t j;
  22. /* The loop counter and index J goes from -SIZE to -1. This way
  23. the loop becomes faster. */
  24. j = -size;
  25. /* Offset the base pointers to compensate for the negative indices. */
  26. s1_ptr -= j;
  27. s2_ptr -= j;
  28. res_ptr -= j;
  29. cy = 0;
  30. do
  31. {
  32. y = s2_ptr[j];
  33. x = s1_ptr[j];
  34. y += cy; /* add previous carry to one addend */
  35. cy = (y < cy); /* get out carry from that addition */
  36. y = x + y; /* add other addend */
  37. cy = (y < x) + cy; /* get out carry from that add, combine */
  38. res_ptr[j] = y;
  39. }
  40. while (++j != 0);
  41. return cy;
  42. }