bn_mp_signed_rsh.c 553 B

12345678910111213141516171819202122
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_SIGNED_RSH_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* shift right by a certain bit count with sign extension */
  6. mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c)
  7. {
  8. mp_err res;
  9. if (a->sign == MP_ZPOS) {
  10. return mp_div_2d(a, b, c, NULL);
  11. }
  12. res = mp_add_d(a, 1uL, c);
  13. if (res != MP_OKAY) {
  14. return res;
  15. }
  16. res = mp_div_2d(c, b, c, NULL);
  17. return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res;
  18. }
  19. #endif