Debian Codegraph @

src/multiply_64.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2019, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <stddef.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <stdio.h>
36 #include "multiply.h"
37
38 /**
39 * Add a 64-bit value x to y/sum_mid/sum_hi
40 */
41 #if defined(_MSC_VER) && (_MSC_VER>=1900) && (defined(_M_X64) || defined(__x86_64__))
42
43 #include <intrin.h>
44 #define ADD192(y, x) do { \
45 unsigned char c = 0; \
46 c = _addcarry_u64(c, x, y, &y); \
47 c = _addcarry_u64(c, 0, sum_mid, &sum_mid); \
48 _addcarry_u64(c, 0, sum_hi, &sum_hi); \
49 } while (0)
50
51 #else
52
53 #define ADD192(y, x) do { \
54 uint64_t c; \
55 y += x; \
56 sum_mid += (c = (y < x)); \
57 sum_hi += sum_mid < c; \
58 } while (0)
59
60 #endif
61
62 void addmul128(uint64_t *t, uint64_t *scratchpad, const uint64_t *a, uint64_t b0, uint64_t b1, size_t t_words, size_t a_nw)
63 {
64 uint64_t sum_low, sum_mid, sum_hi;
65 uint64_t pr_low, pr_high, aim1;
66 size_t i;
67
68 assert(t_words >= a_nw + 2);
69
70 if (a_nw == 0) {
71 return;
72 }
73
74 /** LSW **/
75 DP_MULT(a[0], b0, sum_low, sum_mid);
76 sum_hi = 0;
77
78 ADD192(t[0], sum_low);
79
80 sum_low = sum_mid;
81 sum_mid = sum_hi;
82 sum_hi = 0;
83
84 aim1 = a[0];
85 for (i=1; i<(a_nw-1)/4*4+1;) {
86 /** I **/
87 DP_MULT(aim1, b1, pr_low, pr_high);
88 ADD192(sum_low, pr_low);
89 sum_mid += pr_high;
90 sum_hi += sum_mid < pr_high;
91 DP_MULT(a[i], b0, pr_low, pr_high);
92 ADD192(sum_low, pr_low);
93 sum_mid += pr_high;
94 sum_hi += sum_mid < pr_high;
95 ADD192(t[i], sum_low);
96 sum_low = sum_mid;
97 sum_mid = sum_hi;
98 sum_hi = 0;
99 aim1 = a[i];
100 i++;
101 /** II **/
102 DP_MULT(aim1, b1, pr_low, pr_high);
103 ADD192(sum_low, pr_low);
104 sum_mid += pr_high;
105 sum_hi += sum_mid < pr_high;
106 DP_MULT(a[i], b0, pr_low, pr_high);
107 ADD192(sum_low, pr_low);
108 sum_mid += pr_high;
109 sum_hi += sum_mid < pr_high;
110 ADD192(t[i], sum_low);
111 sum_low = sum_mid;
112 sum_mid = sum_hi;
113 sum_hi = 0;
114 aim1 = a[i];
115 i++;
116 /** III **/
117 DP_MULT(aim1, b1, pr_low, pr_high);
118 ADD192(sum_low, pr_low);
119 sum_mid += pr_high;
120 sum_hi += sum_mid < pr_high;
121 DP_MULT(a[i], b0, pr_low, pr_high);
122 ADD192(sum_low, pr_low);
123 sum_mid += pr_high;
124 sum_hi += sum_mid < pr_high;
125 ADD192(t[i], sum_low);
126 sum_low = sum_mid;
127 sum_mid = sum_hi;
128 sum_hi = 0;
129 aim1 = a[i];
130 i++;
131 /** IV **/
132 DP_MULT(aim1, b1, pr_low, pr_high);
133 ADD192(sum_low, pr_low);
134 sum_mid += pr_high;
135 sum_hi += sum_mid < pr_high;
136 DP_MULT(a[i], b0, pr_low, pr_high);
137 ADD192(sum_low, pr_low);
138 sum_mid += pr_high;
139 sum_hi += sum_mid < pr_high;
140 ADD192(t[i], sum_low);
141 sum_low = sum_mid;
142 sum_mid = sum_hi;
143 sum_hi = 0;
144 aim1 = a[i];
145 i++;
146 }
147
148 /** Execute 0 to 3 times **/
149 for (; i<a_nw; i++) {
150
151 DP_MULT(aim1, b1, pr_low, pr_high);
152 ADD192(sum_low, pr_low);
153 sum_mid += pr_high;
154 sum_hi += sum_mid < pr_high;
155 DP_MULT(a[i], b0, pr_low, pr_high);
156 ADD192(sum_low, pr_low);
157 sum_mid += pr_high;
158 sum_hi += sum_mid < pr_high;
159 ADD192(t[i], sum_low);
160 sum_low = sum_mid;
161 sum_mid = sum_hi;
162 sum_hi = 0;
163 aim1 = a[i];
164 }
165
166 /** MSW - 1 **/
167 DP_MULT(a[i-1], b1, pr_low, pr_high);
168 ADD192(sum_low, pr_low);
169 sum_mid += pr_high;
170 sum_hi += sum_mid < pr_high;
171 ADD192(t[i], sum_low);
172
173 sum_low = sum_mid;
174 sum_mid = sum_hi;
175 sum_hi = 0;
176 i++;
177
178 /** MSW **/
179 ADD192(t[i], sum_low);
180 sum_low = sum_mid;
181 sum_mid = sum_hi;
182 sum_hi = 0;
183 i++;
184
185 /* i == a_nw + 2 */
186
187 /** Extend carry indefinetly **/
188 for (; i<t_words; i++) {
189 ADD192(t[i], sum_low);
190
191 sum_low = sum_mid;
192 sum_mid = sum_hi;
193 sum_hi = 0;
194 }
195 }
196
197
198 void square(uint64_t *t, uint64_t *scratchpad, const uint64_t *a, size_t nw)
199 {
200 size_t i, j;
201 uint64_t carry;
202
203 if (nw == 0) {
204 return;
205 }
206
207 memset(t, 0, 2*sizeof(uint64_t)*nw);
208
209 /** Compute all mix-products without doubling **/
210 for (i=0; i<nw; i++) {
211 carry = 0;
212
213 for (j=i+1; j<nw; j++) {
214 uint64_t sum_lo, sum_hi;
215
216 DP_MULT(a[j], a[i], sum_lo, sum_hi);
217
218 sum_lo += carry;
219 sum_hi += sum_lo < carry;
220
221 t[i+j] += sum_lo;
222 carry = sum_hi + (t[i+j] < sum_lo);
223 }
224
225 /** Propagate carry **/
226 for (j=i+nw; carry>0; j++) {
227 t[j] += (uint64_t)carry;
228 carry = t[j] < carry;
229 }
230 }
231
232 /** Double mix-products and add squares **/
233 carry = 0;
234 for (i=0, j=0; i<nw; i++, j+=2) {
235 uint64_t sum_lo, sum_hi, tmp, tmp2;
236
237 DP_MULT(a[i], a[i], sum_lo, sum_hi);
238
239 sum_lo += carry;
240 sum_hi += sum_lo < carry;
241
242 sum_hi += (tmp = ((t[j+1] << 1) + (t[j] >> 63)));
243 carry = (t[j+1] >> 63) + (sum_hi < tmp);
244
245 sum_lo += (tmp = (t[j] << 1));
246 sum_hi += (tmp2 = (sum_lo < tmp));
247 carry += sum_hi < tmp2;
248
249 t[j] = sum_lo;
250 t[j+1] = sum_hi;
251 }
252 assert(carry == 0);
253 }
254