rshift.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* mpn_rshift -- Shift right a low-level natural-number integer.
  2. Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA. */
  16. #include <config.h>
  17. #include "gmp-impl.h"
  18. /* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
  19. and store the USIZE least significant limbs of the result at WP.
  20. The bits shifted out to the right are returned.
  21. Argument constraints:
  22. 1. 0 < CNT < BITS_PER_MP_LIMB
  23. 2. If the result is to be written over the input, WP must be <= UP.
  24. */
  25. mp_limb_t
  26. #if __STDC__
  27. mpn_rshift (register mp_ptr wp,
  28. register mp_srcptr up, mp_size_t usize,
  29. register unsigned int cnt)
  30. #else
  31. mpn_rshift (wp, up, usize, cnt)
  32. register mp_ptr wp;
  33. register mp_srcptr up;
  34. mp_size_t usize;
  35. register unsigned int cnt;
  36. #endif
  37. {
  38. register mp_limb_t high_limb, low_limb;
  39. register unsigned sh_1, sh_2;
  40. register mp_size_t i;
  41. mp_limb_t retval;
  42. #ifdef DEBUG
  43. if (usize == 0 || cnt == 0)
  44. abort ();
  45. #endif
  46. sh_1 = cnt;
  47. #if 0
  48. if (sh_1 == 0)
  49. {
  50. if (wp != up)
  51. {
  52. /* Copy from low end to high end, to allow specified input/output
  53. overlapping. */
  54. for (i = 0; i < usize; i++)
  55. wp[i] = up[i];
  56. }
  57. return usize;
  58. }
  59. #endif
  60. wp -= 1;
  61. sh_2 = BITS_PER_MP_LIMB - sh_1;
  62. high_limb = up[0];
  63. retval = high_limb << sh_2;
  64. low_limb = high_limb;
  65. for (i = 1; i < usize; i++)
  66. {
  67. high_limb = up[i];
  68. wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
  69. low_limb = high_limb;
  70. }
  71. wp[i] = low_limb >> sh_1;
  72. return retval;
  73. }