misc: syslog: Simplify implementation
[glibc.git] / stdlib / lshift.c
blobc7abb8f42b85dbc9e1687113f1c4904fe046b514
1 /* mpn_lshift -- Shift left low level.
3 Copyright (C) 1991-2022 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, see
19 <https://www.gnu.org/licenses/>. */
21 #include <gmp.h>
22 #include "gmp-impl.h"
24 /* Shift U (pointed to by UP and USIZE digits long) CNT bits to the left
25 and store the USIZE least significant digits of the result at WP.
26 Return the bits shifted out from the most significant digit.
28 Argument constraints:
29 1. 0 < CNT < BITS_PER_MP_LIMB
30 2. If the result is to be written over the input, WP must be >= UP.
33 mp_limb_t
34 mpn_lshift (register mp_ptr wp,
35 register mp_srcptr up, mp_size_t usize,
36 register unsigned int cnt)
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;
43 #ifdef DEBUG
44 if (usize == 0 || cnt == 0)
45 abort ();
46 #endif
48 sh_1 = cnt;
49 #if 0
50 if (sh_1 == 0)
52 if (wp != up)
54 /* Copy from high end to low end, to allow specified input/output
55 overlapping. */
56 for (i = usize - 1; i >= 0; i--)
57 wp[i] = up[i];
59 return 0;
61 #endif
63 wp += 1;
64 sh_2 = BITS_PER_MP_LIMB - sh_1;
65 i = usize - 1;
66 low_limb = up[i];
67 retval = low_limb >> sh_2;
68 high_limb = low_limb;
69 while (--i >= 0)
71 low_limb = up[i];
72 wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
73 high_limb = low_limb;
75 wp[i] = high_limb << sh_1;
77 return retval;