]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libgmp/mpbsd/sdiv.c
This commit was generated by cvs2svn to compensate for changes in r54427,
[FreeBSD/FreeBSD.git] / contrib / libgmp / mpbsd / sdiv.c
1 /* sdiv -- Divide a MINT by a short integer.  Produce a MINT quotient
2    and a short remainder.
3
4 Copyright (C) 1991, 1994, 1995 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
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.
12
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.
17
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. */
22
23 #include "mp.h"
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 #include "longlong.h"
27
28 void
29 #if __STDC__
30 sdiv (const MINT *dividend, signed short int divisor_short, MINT *quot, short *rem_ptr)
31 #else
32 sdiv (dividend, divisor_short, quot, rem_ptr)
33      const MINT *dividend;
34      short int divisor_short;
35      MINT *quot;
36      short *rem_ptr;
37 #endif
38 {
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;
45
46   sign_dividend = dividend->_mp_size;
47   dividend_size = ABS (dividend->_mp_size);
48
49   if (dividend_size == 0)
50     {
51       quot->_mp_size = 0;
52       *rem_ptr = 0;
53       return;
54     }
55
56   sign_divisor = divisor_short;
57   divisor_limb = ABS (divisor_short);
58
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
61      need to be stored.  */
62
63   if (quot->_mp_alloc < dividend_size)
64     _mp_realloc (quot, dividend_size);
65
66   quot_ptr = quot->_mp_d;
67   dividend_ptr = dividend->_mp_d;
68
69   remainder_limb = mpn_divmod_1 (quot_ptr,
70                                  dividend_ptr, dividend_size, divisor_limb);
71
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;
77 }