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