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