monotone

monotone Mtn Source Tree

Root/botan/mp_asmi.h

1/*************************************************
2* Lowest Level MPI Algorithms Header File *
3* (C) 1999-2008 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_MP_ASM_INTERNAL_H__
7#define BOTAN_MP_ASM_INTERNAL_H__
8
9#include <botan/mp_asm.h>
10
11namespace Botan {
12
13extern "C" {
14
15/*************************************************
16* Word Addition *
17*************************************************/
18inline word word_add(word x, word y, word* carry)
19 {
20 word z = x + y;
21 word c1 = (z < x);
22 z += *carry;
23 *carry = c1 | (z < *carry);
24 return z;
25 }
26
27/*************************************************
28* Eight Word Block Addition, Two Argument *
29*************************************************/
30inline word word8_add2(word x[8], const word y[8], word carry)
31 {
32 x[0] = word_add(x[0], y[0], &carry);
33 x[1] = word_add(x[1], y[1], &carry);
34 x[2] = word_add(x[2], y[2], &carry);
35 x[3] = word_add(x[3], y[3], &carry);
36 x[4] = word_add(x[4], y[4], &carry);
37 x[5] = word_add(x[5], y[5], &carry);
38 x[6] = word_add(x[6], y[6], &carry);
39 x[7] = word_add(x[7], y[7], &carry);
40 return carry;
41 }
42
43/*************************************************
44* Eight Word Block Addition, Three Argument *
45*************************************************/
46inline word word8_add3(word z[8], const word x[8],
47 const word y[8], word carry)
48 {
49 z[0] = word_add(x[0], y[0], &carry);
50 z[1] = word_add(x[1], y[1], &carry);
51 z[2] = word_add(x[2], y[2], &carry);
52 z[3] = word_add(x[3], y[3], &carry);
53 z[4] = word_add(x[4], y[4], &carry);
54 z[5] = word_add(x[5], y[5], &carry);
55 z[6] = word_add(x[6], y[6], &carry);
56 z[7] = word_add(x[7], y[7], &carry);
57 return carry;
58 }
59
60/*************************************************
61* Word Subtraction *
62*************************************************/
63inline word word_sub(word x, word y, word* carry)
64 {
65 word t0 = x - y;
66 word c1 = (t0 > x);
67 word z = t0 - *carry;
68 *carry = c1 | (z > t0);
69 return z;
70 }
71
72/*************************************************
73* Eight Word Block Subtraction, Two Argument *
74*************************************************/
75inline word word8_sub2(word x[4], const word y[4], word carry)
76 {
77 x[0] = word_sub(x[0], y[0], &carry);
78 x[1] = word_sub(x[1], y[1], &carry);
79 x[2] = word_sub(x[2], y[2], &carry);
80 x[3] = word_sub(x[3], y[3], &carry);
81 x[4] = word_sub(x[4], y[4], &carry);
82 x[5] = word_sub(x[5], y[5], &carry);
83 x[6] = word_sub(x[6], y[6], &carry);
84 x[7] = word_sub(x[7], y[7], &carry);
85 return carry;
86 }
87
88/*************************************************
89* Eight Word Block Subtraction, Three Argument *
90*************************************************/
91inline word word8_sub3(word z[8], const word x[8],
92 const word y[8], word carry)
93 {
94 z[0] = word_sub(x[0], y[0], &carry);
95 z[1] = word_sub(x[1], y[1], &carry);
96 z[2] = word_sub(x[2], y[2], &carry);
97 z[3] = word_sub(x[3], y[3], &carry);
98 z[4] = word_sub(x[4], y[4], &carry);
99 z[5] = word_sub(x[5], y[5], &carry);
100 z[6] = word_sub(x[6], y[6], &carry);
101 z[7] = word_sub(x[7], y[7], &carry);
102 return carry;
103 }
104
105/*************************************************
106* Eight Word Block Linear Multiplication *
107*************************************************/
108inline word word8_linmul2(word x[4], word y, word carry)
109 {
110 x[0] = word_madd2(x[0], y, &carry);
111 x[1] = word_madd2(x[1], y, &carry);
112 x[2] = word_madd2(x[2], y, &carry);
113 x[3] = word_madd2(x[3], y, &carry);
114 x[4] = word_madd2(x[4], y, &carry);
115 x[5] = word_madd2(x[5], y, &carry);
116 x[6] = word_madd2(x[6], y, &carry);
117 x[7] = word_madd2(x[7], y, &carry);
118 return carry;
119 }
120
121/*************************************************
122* Eight Word Block Linear Multiplication *
123*************************************************/
124inline word word8_linmul3(word z[8], const word x[8], word y, word carry)
125 {
126 z[0] = word_madd2(x[0], y, &carry);
127 z[1] = word_madd2(x[1], y, &carry);
128 z[2] = word_madd2(x[2], y, &carry);
129 z[3] = word_madd2(x[3], y, &carry);
130 z[4] = word_madd2(x[4], y, &carry);
131 z[5] = word_madd2(x[5], y, &carry);
132 z[6] = word_madd2(x[6], y, &carry);
133 z[7] = word_madd2(x[7], y, &carry);
134 return carry;
135 }
136
137/*************************************************
138* Eight Word Block Multiply/Add *
139*************************************************/
140inline word word8_madd3(word z[8], const word x[8], word y, word carry)
141 {
142 z[0] = word_madd3(x[0], y, z[0], &carry);
143 z[1] = word_madd3(x[1], y, z[1], &carry);
144 z[2] = word_madd3(x[2], y, z[2], &carry);
145 z[3] = word_madd3(x[3], y, z[3], &carry);
146 z[4] = word_madd3(x[4], y, z[4], &carry);
147 z[5] = word_madd3(x[5], y, z[5], &carry);
148 z[6] = word_madd3(x[6], y, z[6], &carry);
149 z[7] = word_madd3(x[7], y, z[7], &carry);
150 return carry;
151 }
152
153/*************************************************
154* Multiply-Add Accumulator *
155*************************************************/
156inline void word3_muladd(word* w2, word* w1, word* w0, word a, word b)
157 {
158 word carry = *w0;
159 *w0 = word_madd2(a, b, &carry);
160 *w1 += carry;
161 *w2 += (*w1 < carry) ? 1 : 0;
162 }
163
164/*************************************************
165* Multiply-Add Accumulator *
166*************************************************/
167inline void word3_muladd_2(word* w2, word* w1, word* w0, word a, word b)
168 {
169 word carry = 0;
170 a = word_madd2(a, b, &carry);
171 b = carry;
172
173 word top = (b >> (BOTAN_MP_WORD_BITS-1));
174 b <<= 1;
175 b |= (a >> (BOTAN_MP_WORD_BITS-1));
176 a <<= 1;
177
178 carry = 0;
179 *w0 = word_add(*w0, a, &carry);
180 *w1 = word_add(*w1, b, &carry);
181 *w2 = word_add(*w2, top, &carry);
182 }
183
184}
185
186}
187
188#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status