xref: /dragonfly/contrib/gmp/mpn/generic/subcnd_n.c (revision 0fe46dc6)
1 /* mpn_subcnd_n -- Compute R = U - V if CND != 0 or R = U if CND == 0.
2 
3    THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
4    SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.
5 
6 Copyright 1992, 1993, 1994, 1996, 2000, 2002, 2008, 2009 Free Software
7 Foundation, Inc.
8 
9 This file is part of the GNU MP Library.
10 
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15 
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20 
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23 
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 
27 
28 #if GMP_NAIL_BITS == 0
29 
30 mp_limb_t
31 mpn_subcnd_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n, mp_limb_t cnd)
32 {
33   mp_limb_t ul, vl, sl, rl, cy, cy1, cy2, mask;
34 
35   ASSERT (n >= 1);
36   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
37   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
38 
39   mask = -(mp_limb_t) (cnd != 0);
40   cy = 0;
41   do
42     {
43       ul = *up++;
44       vl = *vp++ & mask;
45       sl = ul - vl;
46       cy1 = sl > ul;
47       rl = sl - cy;
48       cy2 = rl > sl;
49       cy = cy1 | cy2;
50       *rp++ = rl;
51     }
52   while (--n != 0);
53 
54   return cy;
55 }
56 
57 #endif
58 
59 #if GMP_NAIL_BITS >= 1
60 
61 mp_limb_t
62 mpn_subcnd_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n, mp_limb_t cnd)
63 {
64   mp_limb_t ul, vl, rl, cy, mask;
65 
66   ASSERT (n >= 1);
67   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
68   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
69 
70   mask = -(mp_limb_t) (cnd != 0);
71   cy = 0;
72   do
73     {
74       ul = *up++;
75       vl = *vp++ & mask;
76       rl = ul - vl - cy;
77       cy = rl >> (GMP_LIMB_BITS - 1);
78       *rp++ = rl & GMP_NUMB_MASK;
79     }
80   while (--n != 0);
81 
82   return cy;
83 }
84 
85 #endif
86