Botan: src/math/mp/mp_asm64/mp_asm.h Source File

Botan 1.10.17

mp_asm.h
Go to the documentation of this file.
1 /*
2 * MPI Multiply-Add Core
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_MP_MADD_H__
9 #define BOTAN_MP_MADD_H__
10
11 #include <botan/mp_types.h>
12
13 namespace Botan {
14
15 #if (BOTAN_MP_WORD_BITS != 64)
16 #error The mp_asm64 module requires that BOTAN_MP_WORD_BITS == 64
17 #endif
18
19 #if defined(BOTAN_TARGET_ARCH_IS_ALPHA)
20
21 #define BOTAN_WORD_MUL(a,b,z1,z0) do { \
22 asm("umulh %1,%2,%0" : "=r" (z0) : "r" (a), "r" (b)); \
23 z1 = a * b; \
24 } while(0);
25
26 #elif defined(BOTAN_TARGET_ARCH_IS_IA64)
27
28 #define BOTAN_WORD_MUL(a,b,z1,z0) do { \
29 asm("xmpy.hu %0=%1,%2" : "=f" (z0) : "f" (a), "f" (b)); \
30 z1 = a * b; \
31 } while(0);
32
33 #elif defined(BOTAN_TARGET_ARCH_IS_PPC64)
34
35 #define BOTAN_WORD_MUL(a,b,z1,z0) do { \
36 asm("mulhdu %0,%1,%2" : "=r" (z0) : "r" (a), "r" (b) : "cc"); \
37 z1 = a * b; \
38 } while(0);
39
40 #elif defined(BOTAN_TARGET_ARCH_IS_MIPS64)
41
42 #define BOTAN_WORD_MUL(a,b,z1,z0) do { \
43 typedef unsigned int uint128_t __attribute__((mode(TI))); \
44 uint128_t r = (uint128_t)a * b; \
45 z0 = (r >> 64) & 0xFFFFFFFFFFFFFFFF; \
46 z1 = (r ) & 0xFFFFFFFFFFFFFFFF; \
47 } while(0);
48
49 #else
50
51 // Do a 64x64->128 multiply using four 64x64->64 multiplies
52 // plus some adds and shifts. Last resort for CPUs like UltraSPARC,
53 // with 64-bit registers/ALU, but no 64x64->128 multiply.
54 inline void bigint_2word_mul(word a, word b, word* z1, word* z0)
55 {
56 const size_t MP_HWORD_BITS = BOTAN_MP_WORD_BITS / 2;
57 const word MP_HWORD_MASK = ((word)1 << MP_HWORD_BITS) - 1;
58
59 const word a_hi = (a >> MP_HWORD_BITS);
60 const word a_lo = (a & MP_HWORD_MASK);
61 const word b_hi = (b >> MP_HWORD_BITS);
62 const word b_lo = (b & MP_HWORD_MASK);
63
64 word x0 = a_hi * b_hi;
65 word x1 = a_lo * b_hi;
66 word x2 = a_hi * b_lo;
67 word x3 = a_lo * b_lo;
68
69 x2 += x3 >> (MP_HWORD_BITS);
70 x2 += x1;
71
72 if(x2 < x1) // timing channel
73 x0 += ((word)1 << MP_HWORD_BITS);
74
75 *z0 = x0 + (x2 >> MP_HWORD_BITS);
76 *z1 = ((x2 & MP_HWORD_MASK) << MP_HWORD_BITS) + (x3 & MP_HWORD_MASK);
77 }
78
79 #define BOTAN_WORD_MUL(a,b,z1,z0) bigint_2word_mul(a, b, &z1, &z0)
80
81 #endif
82
83 /*
84 * Word Multiply/Add
85 */
86 inline word word_madd2(word a, word b, word* c)
87 {
88 word z0 = 0, z1 = 0;
89
90 BOTAN_WORD_MUL(a, b, z1, z0);
91
92 z1 += *c;
93 z0 += (z1 < *c);
94
95 *c = z0;
96 return z1;
97 }
98
99 /*
100 * Word Multiply/Add
101 */
102 inline word word_madd3(word a, word b, word c, word* d)
103 {
104 word z0 = 0, z1 = 0;
105
106 BOTAN_WORD_MUL(a, b, z1, z0);
107
108 z1 += c;
109 z0 += (z1 < c);
110
111 z1 += *d;
112 z0 += (z1 < *d);
113
114 *d = z0;
115 return z1;
116 }
117
118 }
119
120 #endif
Botan::bigint_2word_mul
void bigint_2word_mul(word a, word b, word *z1, word *z0)
Definition: mp_asm.h:54
Botan::word_madd3
word word_madd3(word a, word b, word c, word *d)
Definition: mp_asm.h:102
BOTAN_WORD_MUL
#define BOTAN_WORD_MUL(a, b, z1, z0)
Definition: mp_asm.h:79
Botan::word_madd2
word word_madd2(word a, word b, word *c)
Definition: mp_asm.h:86
Botan
Definition: algo_base.h:14