lshift.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* mpn_lshift -- Shift left low level.
  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 digits long) CNT bits to the left
  19. and store the USIZE least significant digits of the result at WP.
  20. Return the bits shifted out from the most significant digit.
  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_lshift (register mp_ptr wp,
  28. register mp_srcptr up, mp_size_t usize,
  29. register unsigned int cnt)
  30. #else
  31. mpn_lshift (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 high end to low end, to allow specified input/output
  53. overlapping. */
  54. for (i = usize - 1; i >= 0; i--)
  55. wp[i] = up[i];
  56. }
  57. return 0;
  58. }
  59. #endif
  60. wp += 1;
  61. sh_2 = BITS_PER_MP_LIMB - sh_1;
  62. i = usize - 1;
  63. low_limb = up[i];
  64. retval = low_limb >> sh_2;
  65. high_limb = low_limb;
  66. while (--i >= 0)
  67. {
  68. low_limb = up[i];
  69. wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
  70. high_limb = low_limb;
  71. }
  72. wp[i] = high_limb << sh_1;
  73. return retval;
  74. }