xref: /dflybsd-src/contrib/gmp/mpn/generic/subcnd_n.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpn_subcnd_n -- Compute R = U - V if CND != 0 or R = U if CND == 0.
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino    THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
486d7f5d3SJohn Marino    SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino Copyright 1992, 1993, 1994, 1996, 2000, 2002, 2008, 2009 Free Software
786d7f5d3SJohn Marino Foundation, Inc.
886d7f5d3SJohn Marino 
986d7f5d3SJohn Marino This file is part of the GNU MP Library.
1086d7f5d3SJohn Marino 
1186d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1286d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1386d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1486d7f5d3SJohn Marino option) any later version.
1586d7f5d3SJohn Marino 
1686d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1786d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1886d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1986d7f5d3SJohn Marino License for more details.
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2286d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino #include "gmp.h"
2586d7f5d3SJohn Marino #include "gmp-impl.h"
2686d7f5d3SJohn Marino 
2786d7f5d3SJohn Marino 
2886d7f5d3SJohn Marino #if GMP_NAIL_BITS == 0
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino mp_limb_t
mpn_subcnd_n(mp_ptr rp,mp_srcptr up,mp_srcptr vp,mp_size_t n,mp_limb_t cnd)3186d7f5d3SJohn Marino mpn_subcnd_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n, mp_limb_t cnd)
3286d7f5d3SJohn Marino {
3386d7f5d3SJohn Marino   mp_limb_t ul, vl, sl, rl, cy, cy1, cy2, mask;
3486d7f5d3SJohn Marino 
3586d7f5d3SJohn Marino   ASSERT (n >= 1);
3686d7f5d3SJohn Marino   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
3786d7f5d3SJohn Marino   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
3886d7f5d3SJohn Marino 
3986d7f5d3SJohn Marino   mask = -(mp_limb_t) (cnd != 0);
4086d7f5d3SJohn Marino   cy = 0;
4186d7f5d3SJohn Marino   do
4286d7f5d3SJohn Marino     {
4386d7f5d3SJohn Marino       ul = *up++;
4486d7f5d3SJohn Marino       vl = *vp++ & mask;
4586d7f5d3SJohn Marino       sl = ul - vl;
4686d7f5d3SJohn Marino       cy1 = sl > ul;
4786d7f5d3SJohn Marino       rl = sl - cy;
4886d7f5d3SJohn Marino       cy2 = rl > sl;
4986d7f5d3SJohn Marino       cy = cy1 | cy2;
5086d7f5d3SJohn Marino       *rp++ = rl;
5186d7f5d3SJohn Marino     }
5286d7f5d3SJohn Marino   while (--n != 0);
5386d7f5d3SJohn Marino 
5486d7f5d3SJohn Marino   return cy;
5586d7f5d3SJohn Marino }
5686d7f5d3SJohn Marino 
5786d7f5d3SJohn Marino #endif
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino #if GMP_NAIL_BITS >= 1
6086d7f5d3SJohn Marino 
6186d7f5d3SJohn Marino mp_limb_t
mpn_subcnd_n(mp_ptr rp,mp_srcptr up,mp_srcptr vp,mp_size_t n,mp_limb_t cnd)6286d7f5d3SJohn Marino mpn_subcnd_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n, mp_limb_t cnd)
6386d7f5d3SJohn Marino {
6486d7f5d3SJohn Marino   mp_limb_t ul, vl, rl, cy, mask;
6586d7f5d3SJohn Marino 
6686d7f5d3SJohn Marino   ASSERT (n >= 1);
6786d7f5d3SJohn Marino   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
6886d7f5d3SJohn Marino   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino   mask = -(mp_limb_t) (cnd != 0);
7186d7f5d3SJohn Marino   cy = 0;
7286d7f5d3SJohn Marino   do
7386d7f5d3SJohn Marino     {
7486d7f5d3SJohn Marino       ul = *up++;
7586d7f5d3SJohn Marino       vl = *vp++ & mask;
7686d7f5d3SJohn Marino       rl = ul - vl - cy;
7786d7f5d3SJohn Marino       cy = rl >> (GMP_LIMB_BITS - 1);
7886d7f5d3SJohn Marino       *rp++ = rl & GMP_NUMB_MASK;
7986d7f5d3SJohn Marino     }
8086d7f5d3SJohn Marino   while (--n != 0);
8186d7f5d3SJohn Marino 
8286d7f5d3SJohn Marino   return cy;
8386d7f5d3SJohn Marino }
8486d7f5d3SJohn Marino 
8586d7f5d3SJohn Marino #endif
86