Back to home page

OSCL-LXR

 
 

    


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