ch1_mpi 1
ch1_mpi 2 * Modified by No Such Labs. (C) 2015. See README.
ch1_mpi 3 *
ch1_mpi 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
ch1_mpi 5 * SHA256(gnupg-1.4.10.tar.gz):
ch1_mpi 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
ch1_mpi 7 * (C) 1994-2005 Free Software Foundation, Inc.
ch1_mpi 8 *
ch1_mpi 9 * This program is free software: you can redistribute it and/or modify
ch1_mpi 10 * it under the terms of the GNU General Public License as published by
ch1_mpi 11 * the Free Software Foundation, either version 3 of the License, or
ch1_mpi 12 * (at your option) any later version.
ch1_mpi 13 *
ch1_mpi 14 * This program is distributed in the hope that it will be useful,
ch1_mpi 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ch1_mpi 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ch1_mpi 17 * GNU General Public License for more details.
ch1_mpi 18 *
ch1_mpi 19 * You should have received a copy of the GNU General Public License
ch1_mpi 20 * along with this program. If not, see <http:
ch1_mpi 21 */
ch1_mpi 22
ch1_mpi 23
ch1_mpi 24
ch1_mpi 25 UWtype -- An unsigned type, default type for operations (typically a "word")
ch1_mpi 26 UHWtype -- An unsigned type, at least half the size of UWtype.
ch1_mpi 27 UDWtype -- An unsigned type, at least twice as large a UWtype
ch1_mpi 28 W_TYPE_SIZE -- size in bits of UWtype
ch1_mpi 29
ch1_mpi 30 SItype, USItype -- Signed and unsigned 32 bit types.
ch1_mpi 31 DItype, UDItype -- Signed and unsigned 64 bit types.
ch1_mpi 32
ch1_mpi 33 On a 32 bit machine UWtype should typically be USItype;
ch1_mpi 34 on a 64 bit machine, UWtype should typically be UDItype.
ch1_mpi 35 */
ch1_mpi 36
ch1_mpi 37 #define __BITS4 (W_TYPE_SIZE / 4)
ch1_mpi 38 #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
ch1_mpi 39 #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
ch1_mpi 40 #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
ch1_mpi 41
ch1_mpi 42
ch1_mpi 43 that use this file takes place. */
ch1_mpi 44 #ifndef __MPN
ch1_mpi 45 #define __MPN(x) __##x
ch1_mpi 46 #endif
ch1_mpi 47
ch1_mpi 48
ch1_mpi 49 *********** Generic Versions ********
ch1_mpi 50 ***************************************/
ch1_mpi 51 #if !defined (umul_ppmm) && defined (__umulsidi3)
ch1_mpi 52 #define umul_ppmm(ph, pl, m0, m1) \
ch1_mpi 53 { \
ch1_mpi 54 UDWtype __ll = __umulsidi3 (m0, m1); \
ch1_mpi 55 ph = (UWtype) (__ll >> W_TYPE_SIZE); \
ch1_mpi 56 pl = (UWtype) __ll; \
ch1_mpi 57 }
ch1_mpi 58 #endif
ch1_mpi 59
ch1_mpi 60 #if !defined (__umulsidi3)
ch1_mpi 61 #define __umulsidi3(u, v) \
ch1_mpi 62 ({UWtype __hi, __lo; \
ch1_mpi 63 umul_ppmm (__hi, __lo, u, v); \
ch1_mpi 64 ((UDWtype) __hi << W_TYPE_SIZE) | __lo; })
ch1_mpi 65 #endif
ch1_mpi 66
ch1_mpi 67
ch1_mpi 68
ch1_mpi 69 #if !defined (add_ssaaaa)
ch1_mpi 70 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
ch1_mpi 71 do { \
ch1_mpi 72 UWtype __x; \
ch1_mpi 73 __x = (al) + (bl); \
ch1_mpi 74 (sh) = (ah) + (bh) + (__x < (al)); \
ch1_mpi 75 (sl) = __x; \
ch1_mpi 76 } while (0)
ch1_mpi 77 #endif
ch1_mpi 78
ch1_mpi 79 #if !defined (sub_ddmmss)
ch1_mpi 80 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
ch1_mpi 81 do { \
ch1_mpi 82 UWtype __x; \
ch1_mpi 83 __x = (al) - (bl); \
ch1_mpi 84 (sh) = (ah) - (bh) - (__x > (al)); \
ch1_mpi 85 (sl) = __x; \
ch1_mpi 86 } while (0)
ch1_mpi 87 #endif
ch1_mpi 88
ch1_mpi 89 #if !defined (umul_ppmm)
ch1_mpi 90 #define umul_ppmm(w1, w0, u, v) \
ch1_mpi 91 do { \
ch1_mpi 92 UWtype __x0, __x1, __x2, __x3; \
ch1_mpi 93 UHWtype __ul, __vl, __uh, __vh; \
ch1_mpi 94 UWtype __u = (u), __v = (v); \
ch1_mpi 95 \
ch1_mpi 96 __ul = __ll_lowpart (__u); \
ch1_mpi 97 __uh = __ll_highpart (__u); \
ch1_mpi 98 __vl = __ll_lowpart (__v); \
ch1_mpi 99 __vh = __ll_highpart (__v); \
ch1_mpi 100 \
ch1_mpi 101 __x0 = (UWtype) __ul * __vl; \
ch1_mpi 102 __x1 = (UWtype) __ul * __vh; \
ch1_mpi 103 __x2 = (UWtype) __uh * __vl; \
ch1_mpi 104 __x3 = (UWtype) __uh * __vh; \
ch1_mpi 105 \
ch1_mpi 106 __x1 += __ll_highpart (__x0); \
ch1_mpi 107 __x1 += __x2; \
ch1_mpi 108 if (__x1 < __x2) \
ch1_mpi 109 __x3 += __ll_B; \
ch1_mpi 110 \
ch1_mpi 111 (w1) = __x3 + __ll_highpart (__x1); \
ch1_mpi 112 (w0) = (__ll_lowpart (__x1) << W_TYPE_SIZE/2) + __ll_lowpart (__x0);\
ch1_mpi 113 } while (0)
ch1_mpi 114 #endif
ch1_mpi 115
ch1_mpi 116 #if !defined (umul_ppmm)
ch1_mpi 117 #define smul_ppmm(w1, w0, u, v) \
ch1_mpi 118 do { \
ch1_mpi 119 UWtype __w1; \
ch1_mpi 120 UWtype __m0 = (u), __m1 = (v); \
ch1_mpi 121 umul_ppmm (__w1, w0, __m0, __m1); \
ch1_mpi 122 (w1) = __w1 - (-(__m0 >> (W_TYPE_SIZE - 1)) & __m1) \
ch1_mpi 123 - (-(__m1 >> (W_TYPE_SIZE - 1)) & __m0); \
ch1_mpi 124 } while (0)
ch1_mpi 125 #endif
ch1_mpi 126
ch1_mpi 127
ch1_mpi 128 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
ch1_mpi 129 do { \
ch1_mpi 130 UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m; \
ch1_mpi 131 __d1 = __ll_highpart (d); \
ch1_mpi 132 __d0 = __ll_lowpart (d); \
ch1_mpi 133 \
ch1_mpi 134 __r1 = (n1) % __d1; \
ch1_mpi 135 __q1 = (n1) / __d1; \
ch1_mpi 136 __m = (UWtype) __q1 * __d0; \
ch1_mpi 137 __r1 = __r1 * __ll_B | __ll_highpart (n0); \
ch1_mpi 138 if (__r1 < __m) \
ch1_mpi 139 { \
ch1_mpi 140 __q1--, __r1 += (d); \
ch1_mpi 141 if (__r1 >= (d)) \
ch1_mpi 142 if (__r1 < __m) \
ch1_mpi 143 __q1--, __r1 += (d); \
ch1_mpi 144 } \
ch1_mpi 145 __r1 -= __m; \
ch1_mpi 146 \
ch1_mpi 147 __r0 = __r1 % __d1; \
ch1_mpi 148 __q0 = __r1 / __d1; \
ch1_mpi 149 __m = (UWtype) __q0 * __d0; \
ch1_mpi 150 __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
ch1_mpi 151 if (__r0 < __m) \
ch1_mpi 152 { \
ch1_mpi 153 __q0--, __r0 += (d); \
ch1_mpi 154 if (__r0 >= (d)) \
ch1_mpi 155 if (__r0 < __m) \
ch1_mpi 156 __q0--, __r0 += (d); \
ch1_mpi 157 } \
ch1_mpi 158 __r0 -= __m; \
ch1_mpi 159 \
ch1_mpi 160 (q) = (UWtype) __q1 * __ll_B | __q0; \
ch1_mpi 161 (r) = __r0; \
ch1_mpi 162 } while (0)
ch1_mpi 163
ch1_mpi 164
ch1_mpi 165 __udiv_w_sdiv (defined in libgcc or elsewhere). */
ch1_mpi 166 #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
ch1_mpi 167 #define udiv_qrnnd(q, r, nh, nl, d) \
ch1_mpi 168 do { \
ch1_mpi 169 UWtype __r; \
ch1_mpi 170 (q) = __MPN(udiv_w_sdiv) (&__r, nh, nl, d); \
ch1_mpi 171 (r) = __r; \
ch1_mpi 172 } while (0)
ch1_mpi 173 #endif
ch1_mpi 174
ch1_mpi 175
ch1_mpi 176 #if !defined (udiv_qrnnd)
ch1_mpi 177 #define UDIV_NEEDS_NORMALIZATION 1
ch1_mpi 178 #define udiv_qrnnd __udiv_qrnnd_c
ch1_mpi 179 #endif
ch1_mpi 180
ch1_mpi 181 #if !defined (count_leading_zeros)
ch1_mpi 182 extern
ch1_mpi 183 #ifdef __STDC__
ch1_mpi 184 const
ch1_mpi 185 #endif
ch1_mpi 186 unsigned char __clz_tab[];
ch1_mpi 187 #define MPI_INTERNAL_NEED_CLZ_TAB 1
ch1_mpi 188 #define count_leading_zeros(count, x) \
ch1_mpi 189 do { \
ch1_mpi 190 UWtype __xr = (x); \
ch1_mpi 191 UWtype __a; \
ch1_mpi 192 \
ch1_mpi 193 if (W_TYPE_SIZE <= 32) \
ch1_mpi 194 { \
ch1_mpi 195 __a = __xr < ((UWtype) 1 << 2*__BITS4) \
ch1_mpi 196 ? (__xr < ((UWtype) 1 << __BITS4) ? 0 : __BITS4) \
ch1_mpi 197 : (__xr < ((UWtype) 1 << 3*__BITS4) ? 2*__BITS4 : 3*__BITS4);\
ch1_mpi 198 } \
ch1_mpi 199 else \
ch1_mpi 200 { \
ch1_mpi 201 for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
ch1_mpi 202 if (((__xr >> __a) & 0xff) != 0) \
ch1_mpi 203 break; \
ch1_mpi 204 } \
ch1_mpi 205 \
ch1_mpi 206 (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
ch1_mpi 207 } while (0)
ch1_mpi 208
ch1_mpi 209 #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
ch1_mpi 210 #endif
ch1_mpi 211
ch1_mpi 212 #if !defined (count_trailing_zeros)
ch1_mpi 213
ch1_mpi 214 defined in asm, but if it is not, the C version above is good enough. */
ch1_mpi 215 #define count_trailing_zeros(count, x) \
ch1_mpi 216 do { \
ch1_mpi 217 UWtype __ctz_x = (x); \
ch1_mpi 218 UWtype __ctz_c; \
ch1_mpi 219 count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
ch1_mpi 220 (count) = W_TYPE_SIZE - 1 - __ctz_c; \
ch1_mpi 221 } while (0)
ch1_mpi 222 #endif
ch1_mpi 223
ch1_mpi 224 #ifndef UDIV_NEEDS_NORMALIZATION
ch1_mpi 225 #define UDIV_NEEDS_NORMALIZATION 0
ch1_mpi 226 #endif