// SPDX-License-Identifier: GPL-2.0-or-later1/* mpi-sub-ui.c - Subtract an unsigned integer from an MPI.2*3* Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013, 20154* Free Software Foundation, Inc.5*6* This file was based on the GNU MP Library source file:7* https://gmplib.org/repo/gmp-6.2/file/510b83519d1c/mpz/aors_ui.h8*9* The GNU MP Library is free software; you can redistribute it and/or modify10* it under the terms of either:11*12* * the GNU Lesser General Public License as published by the Free13* Software Foundation; either version 3 of the License, or (at your14* option) any later version.15*16* or17*18* * the GNU General Public License as published by the Free Software19* Foundation; either version 2 of the License, or (at your option) any20* later version.21*22* or both in parallel, as here.23*24* The GNU MP Library is distributed in the hope that it will be useful, but25* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY26* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License27* for more details.28*29* You should have received copies of the GNU General Public License and the30* GNU Lesser General Public License along with the GNU MP Library. If not,31* see https://www.gnu.org/licenses/.32*/3334#include <linux/export.h>3536#include "mpi-internal.h"3738int mpi_sub_ui(MPI w, MPI u, unsigned long vval)39{40if (u->nlimbs == 0) {41if (mpi_resize(w, 1) < 0)42return -ENOMEM;43w->d[0] = vval;44w->nlimbs = (vval != 0);45w->sign = (vval != 0);46return 0;47}4849/* If not space for W (and possible carry), increase space. */50if (mpi_resize(w, u->nlimbs + 1))51return -ENOMEM;5253if (u->sign) {54mpi_limb_t cy;5556cy = mpihelp_add_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);57w->d[u->nlimbs] = cy;58w->nlimbs = u->nlimbs + cy;59w->sign = 1;60} else {61/* The signs are different. Need exact comparison to determine62* which operand to subtract from which.63*/64if (u->nlimbs == 1 && u->d[0] < vval) {65w->d[0] = vval - u->d[0];66w->nlimbs = 1;67w->sign = 1;68} else {69mpihelp_sub_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);70/* Size can decrease with at most one limb. */71w->nlimbs = (u->nlimbs - (w->d[u->nlimbs - 1] == 0));72w->sign = 0;73}74}7576mpi_normalize(w);77return 0;78}79EXPORT_SYMBOL_GPL(mpi_sub_ui);808182