Back to index

glibc  2.9
add_n.c
Go to the documentation of this file.
00001 /* mpn_add_n -- Add two limb vectors of equal, non-zero length.
00002 
00003 Copyright (C) 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
00004 
00005 This file is part of the GNU MP Library.
00006 
00007 The GNU MP Library is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation; either version 2.1 of the License, or (at your
00010 option) any later version.
00011 
00012 The GNU MP Library is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00014 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00015 License for more details.
00016 
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
00019 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00020 MA 02111-1307, USA. */
00021 
00022 #include <gmp.h>
00023 #include "gmp-impl.h"
00024 
00025 mp_limb_t
00026 #if __STDC__
00027 mpn_add_n (mp_ptr res_ptr, mp_srcptr s1_ptr, mp_srcptr s2_ptr, mp_size_t size)
00028 #else
00029 mpn_add_n (res_ptr, s1_ptr, s2_ptr, size)
00030      register mp_ptr res_ptr;
00031      register mp_srcptr s1_ptr;
00032      register mp_srcptr s2_ptr;
00033      mp_size_t size;
00034 #endif
00035 {
00036   register mp_limb_t x, y, cy;
00037   register mp_size_t j;
00038 
00039   /* The loop counter and index J goes from -SIZE to -1.  This way
00040      the loop becomes faster.  */
00041   j = -size;
00042 
00043   /* Offset the base pointers to compensate for the negative indices.  */
00044   s1_ptr -= j;
00045   s2_ptr -= j;
00046   res_ptr -= j;
00047 
00048   cy = 0;
00049   do
00050     {
00051       y = s2_ptr[j];
00052       x = s1_ptr[j];
00053       y += cy;                     /* add previous carry to one addend */
00054       cy = (y < cy);        /* get out carry from that addition */
00055       y = x + y;            /* add other addend */
00056       cy = (y < x) + cy;    /* get out carry from that add, combine */
00057       res_ptr[j] = y;
00058     }
00059   while (++j != 0);
00060 
00061   return cy;
00062 }