1*2a598d0bSHerbert Xu // SPDX-License-Identifier: GPL-2.0-or-later
2*2a598d0bSHerbert Xu /* mpi-sub-ui.c - Subtract an unsigned integer from an MPI.
3*2a598d0bSHerbert Xu *
4*2a598d0bSHerbert Xu * Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013, 2015
5*2a598d0bSHerbert Xu * Free Software Foundation, Inc.
6*2a598d0bSHerbert Xu *
7*2a598d0bSHerbert Xu * This file was based on the GNU MP Library source file:
8*2a598d0bSHerbert Xu * https://gmplib.org/repo/gmp-6.2/file/510b83519d1c/mpz/aors_ui.h
9*2a598d0bSHerbert Xu *
10*2a598d0bSHerbert Xu * The GNU MP Library is free software; you can redistribute it and/or modify
11*2a598d0bSHerbert Xu * it under the terms of either:
12*2a598d0bSHerbert Xu *
13*2a598d0bSHerbert Xu * * the GNU Lesser General Public License as published by the Free
14*2a598d0bSHerbert Xu * Software Foundation; either version 3 of the License, or (at your
15*2a598d0bSHerbert Xu * option) any later version.
16*2a598d0bSHerbert Xu *
17*2a598d0bSHerbert Xu * or
18*2a598d0bSHerbert Xu *
19*2a598d0bSHerbert Xu * * the GNU General Public License as published by the Free Software
20*2a598d0bSHerbert Xu * Foundation; either version 2 of the License, or (at your option) any
21*2a598d0bSHerbert Xu * later version.
22*2a598d0bSHerbert Xu *
23*2a598d0bSHerbert Xu * or both in parallel, as here.
24*2a598d0bSHerbert Xu *
25*2a598d0bSHerbert Xu * The GNU MP Library is distributed in the hope that it will be useful, but
26*2a598d0bSHerbert Xu * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27*2a598d0bSHerbert Xu * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28*2a598d0bSHerbert Xu * for more details.
29*2a598d0bSHerbert Xu *
30*2a598d0bSHerbert Xu * You should have received copies of the GNU General Public License and the
31*2a598d0bSHerbert Xu * GNU Lesser General Public License along with the GNU MP Library. If not,
32*2a598d0bSHerbert Xu * see https://www.gnu.org/licenses/.
33*2a598d0bSHerbert Xu */
34*2a598d0bSHerbert Xu
35*2a598d0bSHerbert Xu #include "mpi-internal.h"
36*2a598d0bSHerbert Xu
mpi_sub_ui(MPI w,MPI u,unsigned long vval)37*2a598d0bSHerbert Xu int mpi_sub_ui(MPI w, MPI u, unsigned long vval)
38*2a598d0bSHerbert Xu {
39*2a598d0bSHerbert Xu if (u->nlimbs == 0) {
40*2a598d0bSHerbert Xu if (mpi_resize(w, 1) < 0)
41*2a598d0bSHerbert Xu return -ENOMEM;
42*2a598d0bSHerbert Xu w->d[0] = vval;
43*2a598d0bSHerbert Xu w->nlimbs = (vval != 0);
44*2a598d0bSHerbert Xu w->sign = (vval != 0);
45*2a598d0bSHerbert Xu return 0;
46*2a598d0bSHerbert Xu }
47*2a598d0bSHerbert Xu
48*2a598d0bSHerbert Xu /* If not space for W (and possible carry), increase space. */
49*2a598d0bSHerbert Xu if (mpi_resize(w, u->nlimbs + 1))
50*2a598d0bSHerbert Xu return -ENOMEM;
51*2a598d0bSHerbert Xu
52*2a598d0bSHerbert Xu if (u->sign) {
53*2a598d0bSHerbert Xu mpi_limb_t cy;
54*2a598d0bSHerbert Xu
55*2a598d0bSHerbert Xu cy = mpihelp_add_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);
56*2a598d0bSHerbert Xu w->d[u->nlimbs] = cy;
57*2a598d0bSHerbert Xu w->nlimbs = u->nlimbs + cy;
58*2a598d0bSHerbert Xu w->sign = 1;
59*2a598d0bSHerbert Xu } else {
60*2a598d0bSHerbert Xu /* The signs are different. Need exact comparison to determine
61*2a598d0bSHerbert Xu * which operand to subtract from which.
62*2a598d0bSHerbert Xu */
63*2a598d0bSHerbert Xu if (u->nlimbs == 1 && u->d[0] < vval) {
64*2a598d0bSHerbert Xu w->d[0] = vval - u->d[0];
65*2a598d0bSHerbert Xu w->nlimbs = 1;
66*2a598d0bSHerbert Xu w->sign = 1;
67*2a598d0bSHerbert Xu } else {
68*2a598d0bSHerbert Xu mpihelp_sub_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);
69*2a598d0bSHerbert Xu /* Size can decrease with at most one limb. */
70*2a598d0bSHerbert Xu w->nlimbs = (u->nlimbs - (w->d[u->nlimbs - 1] == 0));
71*2a598d0bSHerbert Xu w->sign = 0;
72*2a598d0bSHerbert Xu }
73*2a598d0bSHerbert Xu }
74*2a598d0bSHerbert Xu
75*2a598d0bSHerbert Xu mpi_normalize(w);
76*2a598d0bSHerbert Xu return 0;
77*2a598d0bSHerbert Xu }
78*2a598d0bSHerbert Xu EXPORT_SYMBOL_GPL(mpi_sub_ui);
79