Blame stdlib/lshift.c

Packit 6c4009
/* mpn_lshift -- Shift left low level.
Packit 6c4009
Packit 6c4009
Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
This file is part of the GNU MP Library.
Packit 6c4009
Packit 6c4009
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 6c4009
it under the terms of the GNU Lesser General Public License as published by
Packit 6c4009
the Free Software Foundation; either version 2.1 of the License, or (at your
Packit 6c4009
option) any later version.
Packit 6c4009
Packit 6c4009
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 6c4009
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 6c4009
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
Packit 6c4009
License for more details.
Packit 6c4009
Packit 6c4009
You should have received a copy of the GNU Lesser General Public License
Packit 6c4009
along with the GNU MP Library; see the file COPYING.LIB.  If not, see
Packit 6c4009
<http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <gmp.h>
Packit 6c4009
#include "gmp-impl.h"
Packit 6c4009
Packit 6c4009
/* Shift U (pointed to by UP and USIZE digits long) CNT bits to the left
Packit 6c4009
   and store the USIZE least significant digits of the result at WP.
Packit 6c4009
   Return the bits shifted out from the most significant digit.
Packit 6c4009
Packit 6c4009
   Argument constraints:
Packit 6c4009
   1. 0 < CNT < BITS_PER_MP_LIMB
Packit 6c4009
   2. If the result is to be written over the input, WP must be >= UP.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
mp_limb_t
Packit 6c4009
mpn_lshift (register mp_ptr wp,
Packit 6c4009
	    register mp_srcptr up, mp_size_t usize,
Packit 6c4009
	    register unsigned int cnt)
Packit 6c4009
{
Packit 6c4009
  register mp_limb_t high_limb, low_limb;
Packit 6c4009
  register unsigned sh_1, sh_2;
Packit 6c4009
  register mp_size_t i;
Packit 6c4009
  mp_limb_t retval;
Packit 6c4009
Packit 6c4009
#ifdef DEBUG
Packit 6c4009
  if (usize == 0 || cnt == 0)
Packit 6c4009
    abort ();
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  sh_1 = cnt;
Packit 6c4009
#if 0
Packit 6c4009
  if (sh_1 == 0)
Packit 6c4009
    {
Packit 6c4009
      if (wp != up)
Packit 6c4009
	{
Packit 6c4009
	  /* Copy from high end to low end, to allow specified input/output
Packit 6c4009
	     overlapping.  */
Packit 6c4009
	  for (i = usize - 1; i >= 0; i--)
Packit 6c4009
	    wp[i] = up[i];
Packit 6c4009
	}
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  wp += 1;
Packit 6c4009
  sh_2 = BITS_PER_MP_LIMB - sh_1;
Packit 6c4009
  i = usize - 1;
Packit 6c4009
  low_limb = up[i];
Packit 6c4009
  retval = low_limb >> sh_2;
Packit 6c4009
  high_limb = low_limb;
Packit 6c4009
  while (--i >= 0)
Packit 6c4009
    {
Packit 6c4009
      low_limb = up[i];
Packit 6c4009
      wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
Packit 6c4009
      high_limb = low_limb;
Packit 6c4009
    }
Packit 6c4009
  wp[i] = high_limb << sh_1;
Packit 6c4009
Packit 6c4009
  return retval;
Packit 6c4009
}