Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* mpihelp-add_1.c  -  MPI helper functions
0003  * Copyright (C) 1994, 1996, 1997, 1998,
0004  *               2000 Free Software Foundation, Inc.
0005  *
0006  * This file is part of GnuPG.
0007  *
0008  * Note: This code is heavily based on the GNU MP Library.
0009  *   Actually it's the same code with only minor changes in the
0010  *   way the data is stored; this is to support the abstraction
0011  *   of an optional secure memory allocation which may be used
0012  *   to avoid revealing of sensitive data due to paging etc.
0013  *   The GNU MP Library itself is published under the LGPL;
0014  *   however I decided to publish this code under the plain GPL.
0015  */
0016 
0017 #include "mpi-internal.h"
0018 #include "longlong.h"
0019 
0020 mpi_limb_t
0021 mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
0022           mpi_ptr_t s2_ptr, mpi_size_t size)
0023 {
0024     mpi_limb_t x, y, cy;
0025     mpi_size_t j;
0026 
0027     /* The loop counter and index J goes from -SIZE to -1.  This way
0028        the loop becomes faster.  */
0029     j = -size;
0030 
0031     /* Offset the base pointers to compensate for the negative indices. */
0032     s1_ptr -= j;
0033     s2_ptr -= j;
0034     res_ptr -= j;
0035 
0036     cy = 0;
0037     do {
0038         y = s2_ptr[j];
0039         x = s1_ptr[j];
0040         y += cy;    /* add previous carry to one addend */
0041         cy = y < cy;    /* get out carry from that addition */
0042         y += x;     /* add other addend */
0043         cy += y < x;    /* get out carry from that add, combine */
0044         res_ptr[j] = y;
0045     } while (++j);
0046 
0047     return cy;
0048 }