]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshbn.h
Rewrite the core divide function to not use DIVMOD_WORD.
[PuTTY.git] / sshbn.h
1 /*
2  * sshbn.h: the assorted conditional definitions of BignumInt and
3  * multiply macros used throughout the bignum code to treat numbers as
4  * arrays of the most conveniently sized word for the target machine.
5  * Exported so that other code (e.g. poly1305) can use it too.
6  */
7
8 #if defined __SIZEOF_INT128__
9 /* gcc and clang both provide a __uint128_t type on 64-bit targets
10  * (and, when they do, indicate its presence by the above macro),
11  * using the same 'two machine registers' kind of code generation that
12  * 32-bit targets use for 64-bit ints. If we have one of these, we can
13  * use a 64-bit BignumInt and a 128-bit BignumDblInt. */
14 typedef unsigned long long BignumInt;
15 typedef __uint128_t BignumDblInt;
16 #define BIGNUM_INT_MASK  0xFFFFFFFFFFFFFFFFULL
17 #define BIGNUM_TOP_BIT   0x8000000000000000ULL
18 #define BIGNUM_INT_BITS  64
19 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
20 #elif defined __GNUC__ && defined __i386__
21 typedef unsigned long BignumInt;
22 typedef unsigned long long BignumDblInt;
23 #define BIGNUM_INT_MASK  0xFFFFFFFFUL
24 #define BIGNUM_TOP_BIT   0x80000000UL
25 #define BIGNUM_INT_BITS  32
26 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
27 #elif defined _MSC_VER && defined _M_IX86
28 typedef unsigned __int32 BignumInt;
29 typedef unsigned __int64 BignumDblInt;
30 #define BIGNUM_INT_MASK  0xFFFFFFFFUL
31 #define BIGNUM_TOP_BIT   0x80000000UL
32 #define BIGNUM_INT_BITS  32
33 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
34 #elif defined _LP64
35 /* 64-bit architectures can do 32x32->64 chunks at a time */
36 typedef unsigned int BignumInt;
37 typedef unsigned long BignumDblInt;
38 #define BIGNUM_INT_MASK  0xFFFFFFFFU
39 #define BIGNUM_TOP_BIT   0x80000000U
40 #define BIGNUM_INT_BITS  32
41 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
42 #elif defined _LLP64
43 /* 64-bit architectures in which unsigned long is 32 bits, not 64 */
44 typedef unsigned long BignumInt;
45 typedef unsigned long long BignumDblInt;
46 #define BIGNUM_INT_MASK  0xFFFFFFFFUL
47 #define BIGNUM_TOP_BIT   0x80000000UL
48 #define BIGNUM_INT_BITS  32
49 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
50 #else
51 /* Fallback for all other cases */
52 typedef unsigned short BignumInt;
53 typedef unsigned long BignumDblInt;
54 #define BIGNUM_INT_MASK  0xFFFFU
55 #define BIGNUM_TOP_BIT   0x8000U
56 #define BIGNUM_INT_BITS  16
57 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
58 #endif
59
60 #define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)