aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mpn/sparc64/sparc64.h
blob: 8698a82a72ccab68ea028523016c8bf7f8aa6bd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* UltraSPARC 64 support macros.

   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
   FUTURE GNU MP RELEASES.

Copyright 2003 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */


#define LOW32(x)   ((x) & 0xFFFFFFFF)
#define HIGH32(x)  ((x) >> 32)


/* Halfword number i in src is accessed as src[i+HALF_ENDIAN_ADJ(i)].
   Plain src[i] would be incorrect in big endian, HALF_ENDIAN_ADJ has the
   effect of swapping the two halves in this case.  */
#if HAVE_LIMB_BIG_ENDIAN
#define HALF_ENDIAN_ADJ(i)  (1 - (((i) & 1) << 1))   /* +1 even, -1 odd */
#endif
#if HAVE_LIMB_LITTLE_ENDIAN
#define HALF_ENDIAN_ADJ(i)  0                        /* no adjust */
#endif
#ifndef HALF_ENDIAN_ADJ
Error, error, unknown limb endianness;
#endif


/* umul_ppmm_lowequal sets h to the high limb of q*d, assuming the low limb
   of that product is equal to l.  dh and dl are the 32-bit halves of d.

   |-----high----||----low-----|
   +------+------+
   |             |                 ph = qh * dh
   +------+------+
          +------+------+
          |             |          pm1 = ql * dh
          +------+------+
          +------+------+
          |             |          pm2 = qh * dl
          +------+------+
                 +------+------+
                 |             |   pl = ql * dl (not calculated)
                 +------+------+

   Knowing that the low 64 bits is equal to l means that LOW(pm1) + LOW(pm2)
   + HIGH(pl) == HIGH(l).  The only thing we need from those product parts
   is whether they produce a carry into the high.

   pm_l = LOW(pm1)+LOW(pm2) is done to contribute its carry, then the only
   time there's a further carry from LOW(pm_l)+HIGH(pl) is if LOW(pm_l) >
   HIGH(l).  pl is never actually calculated.  */

#define umul_ppmm_lowequal(h, q, d, dh, dl, l)  \
  do {                                          \
    mp_limb_t  ql, qh, ph, pm1, pm2, pm_l;      \
    ASSERT (dh == HIGH32(d));                   \
    ASSERT (dl == LOW32(d));                    \
    ASSERT (q*d == l);                          \
                                                \
    ql = LOW32 (q);                             \
    qh = HIGH32 (q);                            \
                                                \
    pm1 = ql * dh;                              \
    pm2 = qh * dl;                              \
    ph  = qh * dh;                              \
                                                \
    pm_l = LOW32 (pm1) + LOW32 (pm2);           \
                                                \
    (h) = ph + HIGH32 (pm1) + HIGH32 (pm2)      \
      + HIGH32 (pm_l) + ((pm_l << 32) > l);     \
                                                \
    ASSERT_HIGH_PRODUCT (h, q, d);              \
  } while (0)


/* Set h to the high of q*d, assuming the low limb of that product is equal
   to l, and that d fits in 32-bits.

   |-----high----||----low-----|
          +------+------+
          |             |          pm = qh * dl
          +------+------+
                 +------+------+
                 |             |   pl = ql * dl (not calculated)
                 +------+------+

   Knowing that LOW(pm) + HIGH(pl) == HIGH(l) (mod 2^32) means that the only
   time there's a carry from that sum is when LOW(pm) > HIGH(l).  There's no
   need to calculate pl to determine this.  */

#define umul_ppmm_half_lowequal(h, q, d, l)     \
  do {                                          \
    mp_limb_t pm;                               \
    ASSERT (q*d == l);                          \
    ASSERT (HIGH32(d) == 0);                    \
                                                \
    pm = HIGH32(q) * d;                         \
    (h) = HIGH32(pm) + ((pm << 32) > l);        \
    ASSERT_HIGH_PRODUCT (h, q, d);              \
  } while (0)


/* check that h is the high limb of x*y */
#if WANT_ASSERT
#define ASSERT_HIGH_PRODUCT(h, x, y)    \
  do {                                  \
    mp_limb_t  want_h, dummy;           \
    umul_ppmm (want_h, dummy, x, y);    \
    ASSERT (h == want_h);               \
  } while (0)
#else
#define ASSERT_HIGH_PRODUCT(h, q, d)    \
  do { } while (0)
#endif


/* Multiply u anv v, where v < 2^32.  */
#define umul_ppmm_s(w1, w0, u, v)					\
  do {									\
    UWtype __x0, __x2;							\
    UWtype __ul, __vl, __uh;						\
    UWtype __u = (u), __v = (v);					\
									\
    __ul = __ll_lowpart (__u);						\
    __uh = __ll_highpart (__u);						\
    __vl = __ll_lowpart (__v);						\
									\
    __x0 = (UWtype) __ul * __vl;					\
    __x2 = (UWtype) __uh * __vl;					\
									\
    (w1) = (__x2 + (__x0 >> W_TYPE_SIZE/2)) >> W_TYPE_SIZE/2;		\
    (w0) = (__x2 << W_TYPE_SIZE/2) + __x0;				\
  } while (0)

/* Count the leading zeros on a limb, but assuming it fits in 32 bits.
   The count returned will be in the range 32 to 63.
   This is the 32-bit generic C count_leading_zeros from longlong.h. */
#define count_leading_zeros_32(count, x)                                      \
  do {                                                                        \
    mp_limb_t  __xr = (x);                                                    \
    unsigned   __a;                                                           \
    ASSERT ((x) != 0);                                                        \
    ASSERT ((x) <= CNST_LIMB(0xFFFFFFFF));                                    \
    __a = __xr < ((UWtype) 1 << 16) ? (__xr < ((UWtype) 1 << 8) ? 1 : 8 + 1)  \
      : (__xr < ((UWtype) 1 << 24)  ? 16 + 1 : 24 + 1);                       \
                                                                              \
    (count) = W_TYPE_SIZE + 1 - __a - __clz_tab[__xr >> __a];                 \
  } while (0)


/* Set inv to a 32-bit inverse floor((b*(b-d)-1) / d), knowing that d fits
   32 bits and is normalized (high bit set).  */
#define invert_half_limb(inv, d)                \
  do {                                          \
    mp_limb_t  _n;                              \
    ASSERT ((d) <= 0xFFFFFFFF);                 \
    ASSERT ((d) & 0x80000000);                  \
    _n = (((mp_limb_t) -(d)) << 32) - 1;        \
    (inv) = (mp_limb_t) (unsigned) (_n / (d));  \
  } while (0)


/* Divide nh:nl by d, setting q to the quotient and r to the remainder.
   q, r, nh and nl are 32-bits each, d_limb is 32-bits but in an mp_limb_t,
   dinv_limb is similarly a 32-bit inverse but in an mp_limb_t.  */

#define udiv_qrnnd_half_preinv(q, r, nh, nl, d_limb, dinv_limb)         \
  do {                                                                  \
    unsigned   _n2, _n10, _n1, _nadj, _q11n, _xh, _r, _q;               \
    mp_limb_t  _n, _x;                                                  \
    ASSERT (d_limb <= 0xFFFFFFFF);                                      \
    ASSERT (dinv_limb <= 0xFFFFFFFF);                                   \
    ASSERT (d_limb & 0x80000000);                                       \
    ASSERT (nh < d_limb);                                               \
    _n10 = (nl);                                                        \
    _n2 = (nh);                                                         \
    _n1 = (int) _n10 >> 31;                                             \
    _nadj = _n10 + (_n1 & d_limb);                                      \
    _x = dinv_limb * (_n2 - _n1) + _nadj;                               \
    _q11n = ~(_n2 + HIGH32 (_x));             /* -q1-1 */               \
    _n = ((mp_limb_t) _n2 << 32) + _n10;                                \
    _x = _n + d_limb * _q11n;                 /* n-q1*d-d */            \
    _xh = HIGH32 (_x) - d_limb;               /* high(n-q1*d-d) */      \
    ASSERT (_xh == 0 || _xh == ~0);                                     \
    _r = _x + (d_limb & _xh);                 /* addback */             \
    _q = _xh - _q11n;                         /* q1+1-addback */        \
    ASSERT (_r < d_limb);                                               \
    ASSERT (d_limb * _q + _r == _n);                                    \
    (r) = _r;                                                           \
    (q) = _q;                                                           \
  } while (0)