|
Packit |
6c4009 |
/* mpn_rshift -- Shift right a low-level natural-number integer.
|
|
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 |
#include <assert.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
|
|
Packit |
6c4009 |
and store the USIZE least significant limbs of the result at WP.
|
|
Packit |
6c4009 |
The bits shifted out to the right are returned.
|
|
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_rshift (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 |
assert (usize != 0 && cnt != 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
sh_1 = cnt;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#if 0
|
|
Packit |
6c4009 |
if (sh_1 == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (wp != up)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Copy from low end to high end, to allow specified input/output
|
|
Packit |
6c4009 |
overlapping. */
|
|
Packit |
6c4009 |
for (i = 0; i < usize; i++)
|
|
Packit |
6c4009 |
wp[i] = up[i];
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return usize;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
wp -= 1;
|
|
Packit |
6c4009 |
sh_2 = BITS_PER_MP_LIMB - sh_1;
|
|
Packit |
6c4009 |
high_limb = up[0];
|
|
Packit |
6c4009 |
retval = high_limb << sh_2;
|
|
Packit |
6c4009 |
low_limb = high_limb;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (i = 1; i < usize; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
high_limb = up[i];
|
|
Packit |
6c4009 |
wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
|
|
Packit |
6c4009 |
low_limb = high_limb;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
wp[i] = low_limb >> sh_1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return retval;
|
|
Packit |
6c4009 |
}
|