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