xref: /dflybsd-src/contrib/gmp/mpz/dive_ui.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpz_divexact_ui -- exact division mpz by ulong.
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino Copyright 2001, 2002 Free Software Foundation, Inc.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino This file is part of the GNU MP Library.
686d7f5d3SJohn Marino 
786d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
886d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
986d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1086d7f5d3SJohn Marino option) any later version.
1186d7f5d3SJohn Marino 
1286d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1486d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1586d7f5d3SJohn Marino License for more details.
1686d7f5d3SJohn Marino 
1786d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1886d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino #include "gmp.h"
2186d7f5d3SJohn Marino #include "gmp-impl.h"
2286d7f5d3SJohn Marino 
2386d7f5d3SJohn Marino void
mpz_divexact_ui(mpz_ptr dst,mpz_srcptr src,unsigned long divisor)2486d7f5d3SJohn Marino mpz_divexact_ui (mpz_ptr dst, mpz_srcptr src, unsigned long divisor)
2586d7f5d3SJohn Marino {
2686d7f5d3SJohn Marino   mp_size_t  size, abs_size;
2786d7f5d3SJohn Marino   mp_ptr     dst_ptr;
2886d7f5d3SJohn Marino 
2986d7f5d3SJohn Marino   if (divisor == 0)
3086d7f5d3SJohn Marino     DIVIDE_BY_ZERO;
3186d7f5d3SJohn Marino 
3286d7f5d3SJohn Marino   /* For nails don't try to be clever if d is bigger than a limb, just fake
3386d7f5d3SJohn Marino      up an mpz_t and go to the main mpz_divexact.  */
3486d7f5d3SJohn Marino   if (divisor > GMP_NUMB_MAX)
3586d7f5d3SJohn Marino     {
3686d7f5d3SJohn Marino       mp_limb_t  dlimbs[2];
3786d7f5d3SJohn Marino       mpz_t      dz;
3886d7f5d3SJohn Marino       ALLOC(dz) = 2;
3986d7f5d3SJohn Marino       PTR(dz) = dlimbs;
4086d7f5d3SJohn Marino       mpz_set_ui (dz, divisor);
4186d7f5d3SJohn Marino       mpz_divexact (dst, src, dz);
4286d7f5d3SJohn Marino       return;
4386d7f5d3SJohn Marino     }
4486d7f5d3SJohn Marino 
4586d7f5d3SJohn Marino   size = SIZ(src);
4686d7f5d3SJohn Marino   if (size == 0)
4786d7f5d3SJohn Marino     {
4886d7f5d3SJohn Marino       SIZ(dst) = 0;
4986d7f5d3SJohn Marino       return;
5086d7f5d3SJohn Marino     }
5186d7f5d3SJohn Marino   abs_size = ABS (size);
5286d7f5d3SJohn Marino 
5386d7f5d3SJohn Marino   MPZ_REALLOC (dst, abs_size);
5486d7f5d3SJohn Marino   dst_ptr = PTR(dst);
5586d7f5d3SJohn Marino 
5686d7f5d3SJohn Marino   MPN_DIVREM_OR_DIVEXACT_1 (dst_ptr, PTR(src), abs_size, (mp_limb_t) divisor);
5786d7f5d3SJohn Marino   abs_size -= (dst_ptr[abs_size-1] == 0);
5886d7f5d3SJohn Marino   SIZ(dst) = (size >= 0 ? abs_size : -abs_size);
5986d7f5d3SJohn Marino }
60