beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / alpha / dive_1.c
blob88b82db2f73e94532eb16732d2cd87ce1474e4e8
1 /* Alpha mpn_divexact_1 -- mpn by limb exact division.
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.
7 Copyright 2000-2003 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
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.
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.
24 or both in parallel, as here.
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.
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/. */
35 #include "gmp.h"
36 #include "gmp-impl.h"
37 #include "longlong.h"
40 /* cycles/limb
41 EV4: 47.0
42 EV5: 30.0
43 EV6: 15.0
47 /* The dependent chain is as follows (the same as modexact), and this is
48 what the code runs as.
50 ev4 ev5 ev6
51 1 1 1 sub y = x - h
52 23 13 7 mulq q = y * inverse
53 23 15 7 umulh h = high (q * d)
54 -- -- --
55 47 30 15
57 The time to load src[i+1] and establish x hides under the umulh latency. */
59 void
60 mpn_divexact_1 (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor)
62 mp_limb_t inverse, lshift_mask, s, sr, s_next, c, h, x, y, q, dummy;
63 unsigned rshift, lshift;
65 ASSERT (size >= 1);
66 ASSERT (divisor != 0);
67 ASSERT (MPN_SAME_OR_SEPARATE_P (dst, src, size));
68 ASSERT_MPN (src, size);
69 ASSERT_LIMB (divisor);
71 s_next = *src++; /* src[0] */
73 rshift = 0;
74 lshift_mask = 0;
75 if ((divisor & 1) == 0)
77 count_trailing_zeros (rshift, divisor);
78 lshift_mask = MP_LIMB_T_MAX;
79 divisor >>= rshift;
82 binvert_limb (inverse, divisor);
83 lshift = 64 - rshift;
85 c = 0;
86 h = 0;
87 sr = s_next >> rshift;
89 size--;
90 if (LIKELY (size != 0))
94 s_next = *src++; /* src[i+1] */
95 s = sr | ((s_next << lshift) & lshift_mask);
96 x = s - c;
97 c = s < c;
98 sr = s_next >> rshift;
100 y = x - h;
101 c += (x < h);
102 q = y * inverse;
103 *dst++ = q;
104 umul_ppmm (h, dummy, q, divisor);
106 size--;
108 while (size != 0);
111 x = sr - c;
112 y = x - h;
113 q = y * inverse;
114 *dst = q; /* dst[size-1] */