Coverage Report

Created: 2024-11-21 06:47

/src/libgmp/mpz/tdiv_qr.c
Line
Count
Source (jump to first uncovered line)
1
/* mpz_tdiv_qr(quot,rem,dividend,divisor) -- Set QUOT to DIVIDEND/DIVISOR,
2
   and REM to DIVIDEND mod DIVISOR.
3
4
Copyright 1991, 1993, 1994, 2000, 2001, 2005, 2011, 2012, 2021 Free
5
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 either:
11
12
  * the GNU Lesser General Public License as published by the Free
13
    Software Foundation; either version 3 of the License, or (at your
14
    option) any later version.
15
16
or
17
18
  * the GNU General Public License as published by the Free Software
19
    Foundation; either version 2 of the License, or (at your option) any
20
    later version.
21
22
or both in parallel, as here.
23
24
The GNU MP Library is distributed in the hope that it will be useful, but
25
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
for more details.
28
29
You should have received copies of the GNU General Public License and the
30
GNU Lesser General Public License along with the GNU MP Library.  If not,
31
see https://www.gnu.org/licenses/.  */
32
33
#include "gmp-impl.h"
34
#include "longlong.h"
35
36
void
37
mpz_tdiv_qr (mpz_ptr quot, mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
38
3.75k
{
39
3.75k
  mp_size_t ql, n0;
40
3.75k
  mp_size_t ns, ds, nl, dl;
41
3.75k
  mp_ptr np, dp, qp, rp;
42
3.75k
  TMP_DECL;
43
44
3.75k
  ns = SIZ (num);
45
3.75k
  ds = SIZ (den);
46
3.75k
  nl = ABS (ns);
47
3.75k
  dl = ABS (ds);
48
3.75k
  ql = nl - dl + 1;
49
50
3.75k
  if (UNLIKELY (dl == 0))
51
0
    DIVIDE_BY_ZERO;
52
53
3.75k
  rp = MPZ_REALLOC (rem, dl);
54
55
3.75k
  if (ql <= 0)
56
1.76k
    {
57
1.76k
      if (num != rem)
58
1.76k
  {
59
1.76k
    np = PTR (num);
60
1.76k
    MPN_COPY (rp, np, nl);
61
1.76k
    SIZ (rem) = SIZ (num);
62
1.76k
  }
63
      /* This needs to follow the assignment to rem, in case the
64
   numerator and quotient are the same.  */
65
1.76k
      SIZ (quot) = 0;
66
1.76k
      return;
67
1.76k
    }
68
69
1.98k
  qp = MPZ_REALLOC (quot, ql);
70
71
1.98k
  TMP_MARK;
72
1.98k
  np = PTR (num);
73
1.98k
  dp = PTR (den);
74
75
  /* FIXME: We should think about how to handle the temporary allocation.
76
     Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
77
     allocate temp space.  */
78
79
  /* Copy denominator to temporary space if it overlaps with the quotient
80
     or remainder.  */
81
1.98k
  if (dp == rp || dp == qp)
82
0
    {
83
0
      mp_ptr tp;
84
0
      tp = TMP_ALLOC_LIMBS (dl);
85
0
      MPN_COPY (tp, dp, dl);
86
0
      dp = tp;
87
0
    }
88
  /* Copy numerator to temporary space if it overlaps with the quotient or
89
     remainder.  */
90
1.98k
  if (np == rp || np == qp)
91
0
    {
92
0
      mp_ptr tp;
93
0
      tp = TMP_ALLOC_LIMBS (nl);
94
0
      MPN_COPY (tp, np, nl);
95
0
      np = tp;
96
0
    }
97
98
3.42k
  for (n0 = 0; *dp == 0; ++dp)
99
1.44k
    {
100
1.44k
      rp [n0++] = *np++;
101
1.44k
      --nl;
102
1.44k
    }
103
1.98k
  mpn_tdiv_qr (qp, rp + n0, 0L, np, nl, dp, dl - n0);
104
105
1.98k
  ql -=  qp[ql - 1] == 0;
106
1.98k
  MPN_NORMALIZE (rp, dl);
107
108
1.98k
  SIZ (quot) = (ns ^ ds) >= 0 ? ql : -ql;
109
1.98k
  SIZ (rem) = ns >= 0 ? dl : -dl;
110
1.98k
  TMP_FREE;
111
1.98k
}