1 /* sdiv -- Divide a MINT by a short integer. Produce a MINT quotient
4 Copyright (C) 1991, 1994, 1995 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
30 sdiv (const MINT *dividend, signed short int divisor_short, MINT *quot, short *rem_ptr)
32 sdiv (dividend, divisor_short, quot, rem_ptr)
34 short int divisor_short;
39 mp_size_t sign_dividend;
40 signed long int sign_divisor;
41 mp_size_t dividend_size, quot_size;
42 mp_ptr dividend_ptr, quot_ptr;
43 mp_limb_t divisor_limb;
44 mp_limb_t remainder_limb;
46 sign_dividend = dividend->_mp_size;
47 dividend_size = ABS (dividend->_mp_size);
49 if (dividend_size == 0)
56 sign_divisor = divisor_short;
57 divisor_limb = ABS (divisor_short);
59 /* No need for temporary allocation and copying even if QUOT == DIVIDEND
60 as the divisor is just one limb, and thus no intermediate remainders
63 if (quot->_mp_alloc < dividend_size)
64 _mp_realloc (quot, dividend_size);
66 quot_ptr = quot->_mp_d;
67 dividend_ptr = dividend->_mp_d;
69 remainder_limb = mpn_divmod_1 (quot_ptr,
70 dividend_ptr, dividend_size, divisor_limb);
72 *rem_ptr = sign_dividend >= 0 ? remainder_limb : -remainder_limb;
73 /* The quotient is DIVIDEND_SIZE limbs, but the most significant
74 might be zero. Set QUOT_SIZE properly. */
75 quot_size = dividend_size - (quot_ptr[dividend_size - 1] == 0);
76 quot->_mp_size = (sign_divisor ^ sign_dividend) >= 0 ? quot_size : -quot_size;