xref: /dragonfly/contrib/gmp/mpn/generic/sub_n.c (revision fcf53d9b)
1 /* mpn_sub_n -- Subtract equal length limb vectors.
2 
3 Copyright 1992, 1993, 1994, 1996, 2000, 2002 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
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 3 of the License, or (at your
10 option) any later version.
11 
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.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 
23 
24 #if GMP_NAIL_BITS == 0
25 
26 mp_limb_t
27 mpn_sub_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
28 {
29   mp_limb_t ul, vl, sl, rl, cy, cy1, cy2;
30 
31   ASSERT (n >= 1);
32   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
33   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
34 
35   cy = 0;
36   do
37     {
38       ul = *up++;
39       vl = *vp++;
40       sl = ul - vl;
41       cy1 = sl > ul;
42       rl = sl - cy;
43       cy2 = rl > sl;
44       cy = cy1 | cy2;
45       *rp++ = rl;
46     }
47   while (--n != 0);
48 
49   return cy;
50 }
51 
52 #endif
53 
54 #if GMP_NAIL_BITS >= 1
55 
56 mp_limb_t
57 mpn_sub_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
58 {
59   mp_limb_t ul, vl, rl, cy;
60 
61   ASSERT (n >= 1);
62   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
63   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
64 
65   cy = 0;
66   do
67     {
68       ul = *up++;
69       vl = *vp++;
70       rl = ul - vl - cy;
71       cy = rl >> (GMP_LIMB_BITS - 1);
72       *rp++ = rl & GMP_NUMB_MASK;
73     }
74   while (--n != 0);
75 
76   return cy;
77 }
78 
79 #endif
80