raw
mpi-genesis             1 /* mpihelp-add_1.c  -  MPI helper functions
mpi-genesis 2 * Copyright (C) 1994, 1996, 1997, 1998,
mpi-genesis 3 * 2000 Free Software Foundation, Inc.
mpi-genesis 4 *
mpi-genesis 5 * This file is part of GnuPG.
mpi-genesis 6 *
mpi-genesis 7 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 8 * it under the terms of the GNU General Public License as published by
mpi-genesis 9 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 10 * (at your option) any later version.
mpi-genesis 11 *
mpi-genesis 12 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 15 * GNU General Public License for more details.
mpi-genesis 16 *
mpi-genesis 17 * You should have received a copy of the GNU General Public License
mpi-genesis 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis 19 *
mpi-genesis 20 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis 21 * Actually it's the same code with only minor changes in the
mpi-genesis 22 * way the data is stored; this is to support the abstraction
mpi-genesis 23 * of an optional secure memory allocation which may be used
mpi-genesis 24 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis 25 * The GNU MP Library itself is published under the LGPL;
mpi-genesis 26 * however I decided to publish this code under the plain GPL.
mpi-genesis 27 */
mpi-genesis 28
mpi-genesis 29 #include <config.h>
mpi-genesis 30 #include <stdio.h>
mpi-genesis 31 #include <stdlib.h>
mpi-genesis 32 #include "mpi-internal.h"
mpi-genesis 33 #include "longlong.h"
mpi-genesis 34
mpi-genesis 35 mpi_limb_t
mpi-genesis 36 mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 37 mpi_ptr_t s2_ptr, mpi_size_t size)
mpi-genesis 38 {
mpi-genesis 39 mpi_limb_t x, y, cy;
mpi-genesis 40 mpi_size_t j;
mpi-genesis 41
mpi-genesis 42 /* The loop counter and index J goes from -SIZE to -1. This way
mpi-genesis 43 the loop becomes faster. */
mpi-genesis 44 j = -size;
mpi-genesis 45
mpi-genesis 46 /* Offset the base pointers to compensate for the negative indices. */
mpi-genesis 47 s1_ptr -= j;
mpi-genesis 48 s2_ptr -= j;
mpi-genesis 49 res_ptr -= j;
mpi-genesis 50
mpi-genesis 51 cy = 0;
mpi-genesis 52 do {
mpi-genesis 53 y = s2_ptr[j];
mpi-genesis 54 x = s1_ptr[j];
mpi-genesis 55 y += cy; /* add previous carry to one addend */
mpi-genesis 56 cy = y < cy; /* get out carry from that addition */
mpi-genesis 57 y += x; /* add other addend */
mpi-genesis 58 cy += y < x; /* get out carry from that add, combine */
mpi-genesis 59 res_ptr[j] = y;
mpi-genesis 60 } while( ++j );
mpi-genesis 61
mpi-genesis 62 return cy;
mpi-genesis 63 }
mpi-genesis 64