[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / mpz_dm_ui.c
1 /* mpz_divmod_ui(quot,rem,dividend,short_divisor) --
2    Set QUOT to DIVIDEND / SHORT_DIVISOR
3    and REM to DIVIDEND mod SHORT_DIVISOR.
4
5 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
6
7 This file is part of the GNU MP Library.
8
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)
12 any later version.
13
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.
18
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.  */
22
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "longlong.h"
26
27 void
28 #ifdef __STDC__
29 mpz_divmod_ui (MP_INT *quot, MP_INT *rem,
30                const MP_INT *dividend, unsigned long int divisor_limb)
31 #else
32 mpz_divmod_ui (quot, rem, dividend, divisor_limb)
33      MP_INT *quot;
34      MP_INT *rem;
35      const MP_INT *dividend;
36      unsigned long int divisor_limb;
37 #endif
38 {
39   mp_size sign_dividend;
40   mp_size dividend_size, quot_size;
41   mp_ptr dividend_ptr, quot_ptr;
42   mp_limb remainder_limb;
43
44   sign_dividend = dividend->size;
45   dividend_size = ABS (dividend->size);
46
47   if (dividend_size == 0)
48     {
49       quot->size = 0;
50       rem->size = 0;
51       return;
52     }
53
54   /* No need for temporary allocation and copying if QUOT == DIVIDEND as
55      the divisor is just one limb, and thus no intermediate remainders
56      need to be stored.  */
57
58   if (quot->alloc < dividend_size)
59     _mpz_realloc (quot, dividend_size);
60
61   quot_ptr = quot->d;
62   dividend_ptr = dividend->d;
63
64   remainder_limb = mpn_divmod_1 (quot_ptr,
65                                  dividend_ptr, dividend_size, divisor_limb);
66
67   if (remainder_limb == 0)
68     rem->size = 0;
69   else
70     {
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;
75     }
76
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;
81 }