beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / cnd_sub_n.c
blobbd8e029a36fdf6f5e084551b64ebd71d3d599ed0
1 /* mpn_cnd_sub_n -- Compute R = U - V if CND != 0 or R = U if CND == 0.
2 Both cases should take the same time and perform the exact same memory
3 accesses, since this function is intended to be used where side-channel
4 attack resilience is relevant.
6 Copyright 1992-1994, 1996, 2000, 2002, 2008, 2009, 2011, 2013 Free Software
7 Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
22 later version.
24 or both in parallel, as here.
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 for more details.
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library. If not,
33 see https://www.gnu.org/licenses/. */
35 #include "gmp.h"
36 #include "gmp-impl.h"
38 mp_limb_t
39 mpn_cnd_sub_n (mp_limb_t cnd, mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
41 mp_limb_t ul, vl, sl, rl, cy, cy1, cy2, mask;
43 ASSERT (n >= 1);
44 ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
45 ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
47 mask = -(mp_limb_t) (cnd != 0);
48 cy = 0;
51 ul = *up++;
52 vl = *vp++ & mask;
53 #if GMP_NAIL_BITS == 0
54 sl = ul - vl;
55 cy1 = sl > ul;
56 rl = sl - cy;
57 cy2 = rl > sl;
58 cy = cy1 | cy2;
59 *rp++ = rl;
60 #else
61 rl = ul - vl;
62 rl -= cy;
63 cy = rl >> (GMP_LIMB_BITS - 1);
64 *rp++ = rl & GMP_NUMB_MASK;
65 #endif
67 while (--n != 0);
69 return cy;