Merge branch 'vendor/GMP' into gcc441
[dragonfly.git] / contrib / gmp / mpn / generic / bdiv_dbm1c.c
1 /* mpn_bdiv_dbm1c -- divide an mpn number by a divisor of B-1, where B is the
2    limb base.  The dbm1c moniker means "Divisor of B Minus 1 with Carry".
3
4    THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
5    SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
6    GUARANTEED THAT IT'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
7
8 Copyright 2008, 2009 Free Software Foundation, Inc.
9
10 This file is part of the GNU MP Library.
11
12 The GNU MP Library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
16
17 The GNU MP Library is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20 License for more details.
21
22 You should have received a copy of the GNU Lesser General Public License
23 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
24
25 #include "gmp.h"
26 #include "gmp-impl.h"
27 #include "longlong.h"
28
29
30 mp_limb_t
31 mpn_bdiv_dbm1c (mp_ptr qp, mp_srcptr ap, mp_size_t n, mp_limb_t bd, mp_limb_t h)
32 {
33   mp_limb_t a, p0, p1, cy;
34   mp_size_t i;
35
36   for (i = 0; i < n; i++)
37     {
38       a = ap[i];
39       umul_ppmm (p1, p0, a, bd << GMP_NAIL_BITS);
40       p0 >>= GMP_NAIL_BITS;
41       cy = h < p0;
42       h = (h - p0) & GMP_NUMB_MASK;
43       qp[i] = h;
44       h = h - p1 - cy;
45     }
46
47   return h;
48 }