mpi-sub-ui.c 2.3 KB

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