1 /* mpz_divmod_ui(quot,rem,dividend,short_divisor) --
2 Set QUOT to DIVIDEND / SHORT_DIVISOR
3 and REM to DIVIDEND mod SHORT_DIVISOR.
5 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 The GNU MP Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with the GNU MP Library; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 mpz_divmod_ui (MP_INT *quot, MP_INT *rem,
30 const MP_INT *dividend, unsigned long int divisor_limb)
32 mpz_divmod_ui (quot, rem, dividend, divisor_limb)
35 const MP_INT *dividend;
36 unsigned long int divisor_limb;
39 mp_size sign_dividend;
40 mp_size dividend_size, quot_size;
41 mp_ptr dividend_ptr, quot_ptr;
42 mp_limb remainder_limb;
44 sign_dividend = dividend->size;
45 dividend_size = ABS (dividend->size);
47 if (dividend_size == 0)
54 /* No need for temporary allocation and copying if QUOT == DIVIDEND as
55 the divisor is just one limb, and thus no intermediate remainders
58 if (quot->alloc < dividend_size)
59 _mpz_realloc (quot, dividend_size);
62 dividend_ptr = dividend->d;
64 remainder_limb = mpn_divmod_1 (quot_ptr,
65 dividend_ptr, dividend_size, divisor_limb);
67 if (remainder_limb == 0)
71 /* Store the single-limb remainder. We don't check if there's space
72 for just one limb, since no function ever makes zero space. */
73 rem->size = sign_dividend >= 0 ? 1 : -1;
74 rem->d[0] = remainder_limb;
77 /* The quotient is DIVIDEND_SIZE limbs, but the most significant
78 might be zero. Set QUOT_SIZE properly. */
79 quot_size = dividend_size - (quot_ptr[dividend_size - 1] == 0);
80 quot->size = sign_dividend >= 0 ? quot_size : -quot_size;