Coverage Report

Created: 2025-03-18 06:55

/src/gmp/mpz/cfdiv_r_2exp.c
Line
Count
Source (jump to first uncovered line)
1
/* mpz_cdiv_r_2exp, mpz_fdiv_r_2exp -- remainder from mpz divided by 2^n.
2
3
Copyright 2001, 2002, 2004, 2012 Free Software Foundation, Inc.
4
5
This file is part of the GNU MP Library.
6
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of either:
9
10
  * the GNU Lesser General Public License as published by the Free
11
    Software Foundation; either version 3 of the License, or (at your
12
    option) any later version.
13
14
or
15
16
  * the GNU General Public License as published by the Free Software
17
    Foundation; either version 2 of the License, or (at your option) any
18
    later version.
19
20
or both in parallel, as here.
21
22
The GNU MP Library is distributed in the hope that it will be useful, but
23
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
for more details.
26
27
You should have received copies of the GNU General Public License and the
28
GNU Lesser General Public License along with the GNU MP Library.  If not,
29
see https://www.gnu.org/licenses/.  */
30
31
#include "gmp-impl.h"
32
33
34
/* Bit mask of "n" least significant bits of a limb. */
35
0
#define LOW_MASK(n)   ((CNST_LIMB(1) << (n)) - 1)
36
37
38
/* dir==1 for ceil, dir==-1 for floor */
39
40
static void __gmpz_cfdiv_r_2exp (REGPARM_3_1 (mpz_ptr, mpz_srcptr, mp_bitcnt_t, int)) REGPARM_ATTR (1);
41
0
#define cfdiv_r_2exp(w,u,cnt,dir)  __gmpz_cfdiv_r_2exp (REGPARM_3_1 (w, u, cnt, dir))
42
43
REGPARM_ATTR (1) static void
44
cfdiv_r_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt, int dir)
45
0
{
46
0
  mp_size_t  usize, abs_usize, limb_cnt, i;
47
0
  mp_srcptr  up;
48
0
  mp_ptr     wp;
49
0
  mp_limb_t  high;
50
51
0
  usize = SIZ(u);
52
0
  if (usize == 0)
53
0
    {
54
0
      SIZ(w) = 0;
55
0
      return;
56
0
    }
57
58
0
  limb_cnt = cnt / GMP_NUMB_BITS;
59
0
  cnt %= GMP_NUMB_BITS;
60
0
  abs_usize = ABS (usize);
61
62
  /* MPZ_REALLOC(w) below is only when w!=u, so we can fetch PTR(u) here
63
     nice and early */
64
0
  up = PTR(u);
65
66
0
  if ((usize ^ dir) < 0)
67
0
    {
68
      /* Round towards zero, means just truncate */
69
70
0
      if (w == u)
71
0
  {
72
    /* if already smaller than limb_cnt then do nothing */
73
0
    if (abs_usize <= limb_cnt)
74
0
      return;
75
0
    wp = (mp_ptr) up;
76
0
  }
77
0
      else
78
0
  {
79
0
    i = MIN (abs_usize, limb_cnt+1);
80
0
    wp = MPZ_NEWALLOC (w, i);
81
0
    MPN_COPY (wp, up, i);
82
83
    /* if smaller than limb_cnt then only the copy is needed */
84
0
    if (abs_usize <= limb_cnt)
85
0
      {
86
0
        SIZ(w) = usize;
87
0
        return;
88
0
      }
89
0
  }
90
0
    }
91
0
  else
92
0
    {
93
      /* Round away from zero, means twos complement if non-zero */
94
95
      /* if u!=0 and smaller than divisor, then must negate */
96
0
      if (abs_usize <= limb_cnt)
97
0
  goto negate;
98
99
      /* if non-zero low limb, then must negate */
100
0
      for (i = 0; i < limb_cnt; i++)
101
0
  if (up[i] != 0)
102
0
    goto negate;
103
104
      /* if non-zero partial limb, then must negate */
105
0
      if ((up[limb_cnt] & LOW_MASK (cnt)) != 0)
106
0
  goto negate;
107
108
      /* otherwise low bits of u are zero, so that's the result */
109
0
      SIZ(w) = 0;
110
0
      return;
111
112
0
    negate:
113
      /* twos complement negation to get 2**cnt-u */
114
115
0
      wp = MPZ_REALLOC (w, limb_cnt+1);
116
0
      up = PTR(u);
117
118
      /* Ones complement */
119
0
      i = MIN (abs_usize, limb_cnt+1);
120
0
      ASSERT_CARRY (mpn_neg (wp, up, i));
121
0
      for ( ; i <= limb_cnt; i++)
122
0
  wp[i] = GMP_NUMB_MAX;
123
124
0
      usize = -usize;
125
0
    }
126
127
  /* Mask the high limb */
128
0
  high = wp[limb_cnt];
129
0
  high &= LOW_MASK (cnt);
130
0
  wp[limb_cnt] = high;
131
132
  /* Strip any consequent high zeros */
133
0
  while (high == 0)
134
0
    {
135
0
      limb_cnt--;
136
0
      if (limb_cnt < 0)
137
0
  {
138
0
    SIZ(w) = 0;
139
0
    return;
140
0
  }
141
0
      high = wp[limb_cnt];
142
0
    }
143
144
0
  limb_cnt++;
145
0
  SIZ(w) = (usize >= 0 ? limb_cnt : -limb_cnt);
146
0
}
147
148
149
void
150
mpz_cdiv_r_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
151
0
{
152
0
  cfdiv_r_2exp (w, u, cnt, 1);
153
0
}
154
155
void
156
mpz_fdiv_r_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
157
0
{
158
0
  cfdiv_r_2exp (w, u, cnt, -1);
159
0
}