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