xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/alpha/dive_1.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Alpha mpn_divexact_1 -- mpn by limb exact division.
2 
3    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
4    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5    FUTURE GNU MP RELEASES.
6 
7 Copyright 2000-2003 Free Software Foundation, Inc.
8 
9 This file is part of the GNU MP Library.
10 
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
13 
14   * the GNU Lesser General Public License as published by the Free
15     Software Foundation; either version 3 of the License, or (at your
16     option) any later version.
17 
18 or
19 
20   * the GNU General Public License as published by the Free Software
21     Foundation; either version 2 of the License, or (at your option) any
22     later version.
23 
24 or both in parallel, as here.
25 
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29 for more details.
30 
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library.  If not,
33 see https://www.gnu.org/licenses/.  */
34 
35 #include "gmp-impl.h"
36 #include "longlong.h"
37 
38 
39 /*      cycles/limb
40    EV4:    47.0
41    EV5:    30.0
42    EV6:    15.0
43 */
44 
45 
46 /* The dependent chain is as follows (the same as modexact), and this is
47    what the code runs as.
48 
49        ev4    ev5   ev6
50         1      1     1    sub    y = x - h
51        23     13     7    mulq   q = y * inverse
52        23     15     7    umulh  h = high (q * d)
53        --     --    --
54        47     30    15
55 
56    The time to load src[i+1] and establish x hides under the umulh latency.  */
57 
58 void
mpn_divexact_1(mp_ptr dst,mp_srcptr src,mp_size_t size,mp_limb_t divisor)59 mpn_divexact_1 (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor)
60 {
61   mp_limb_t  inverse, lshift_mask, s, sr, s_next, c, h, x, y, q, dummy;
62   unsigned   rshift, lshift;
63 
64   ASSERT (size >= 1);
65   ASSERT (divisor != 0);
66   ASSERT (MPN_SAME_OR_SEPARATE_P (dst, src, size));
67   ASSERT_MPN (src, size);
68   ASSERT_LIMB (divisor);
69 
70   s_next = *src++;   /* src[0] */
71 
72   rshift = 0;
73   lshift_mask = 0;
74   if ((divisor & 1) == 0)
75     {
76       count_trailing_zeros (rshift, divisor);
77       lshift_mask = MP_LIMB_T_MAX;
78       divisor >>= rshift;
79     }
80 
81   binvert_limb (inverse, divisor);
82   lshift = 64 - rshift;
83 
84   c = 0;
85   h = 0;
86   sr = s_next >> rshift;
87 
88   size--;
89   if (LIKELY (size != 0))
90     {
91       do
92         {
93           s_next = *src++;      /* src[i+1] */
94           s = sr | ((s_next << lshift) & lshift_mask);
95           x = s - c;
96           c = s < c;
97           sr = s_next >> rshift;
98 
99           y = x - h;
100           c += (x < h);
101           q = y * inverse;
102           *dst++ = q;
103           umul_ppmm (h, dummy, q, divisor);
104 
105           size--;
106         }
107       while (size != 0);
108     }
109 
110   x = sr - c;
111   y = x - h;
112   q = y * inverse;
113   *dst = q;         /* dst[size-1] */
114 }
115