source: trunk/lib/bitblock256.hpp @ 3451

Last change on this file since 3451 was 3451, checked in by cameron, 4 years ago

carryout2carry for AVX2

File size: 12.7 KB
Line 
1#ifndef BITBLOCK256_HPP_
2#define BITBLOCK256_HPP_
3
4/*=============================================================================
5    bitblock256 - Specific 256 bit implementations.
6
7    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy, Hua Huang and Nigel Medforth.
8    Licensed to the public under the Open Software License 3.0.
9    Licensed to International Characters Inc.
10       under the Academic Free License version 3.0.
11
12=============================================================================*/
13
14#include "idisa128.hpp"
15#include "idisa256.hpp"
16#include "builtins.hpp"
17
18union ubitblock {
19        bitblock256_t _256;
20        bitblock128_t _128[sizeof(bitblock256_t)/sizeof(bitblock256_t)];
21        uint64_t _64[sizeof(bitblock256_t)/sizeof(uint64_t)];
22        uint32_t _32[sizeof(bitblock256_t)/sizeof(uint32_t)];
23        uint16_t _16[sizeof(bitblock256_t)/sizeof(uint16_t)];
24        uint8_t _8[sizeof(bitblock256_t)/sizeof(uint8_t)];
25};
26
27/* The type used to store a carry bit. */
28typedef bitblock256_t carry_t;
29
30static IDISA_ALWAYS_INLINE void add_ci_co(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum);
31static IDISA_ALWAYS_INLINE void sub_bi_bo(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference);
32static IDISA_ALWAYS_INLINE void adv_ci_co(bitblock256_t cursor, bitblock256_t carry_in, bitblock256_t & carry_out, bitblock256_t & rslt);
33
34
35
36
37static IDISA_ALWAYS_INLINE bitblock256_t carry2bitblock(carry_t carry);
38static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock256_t carry);
39static IDISA_ALWAYS_INLINE carry_t carryout2carry(bitblock256_t carryout);
40
41static IDISA_ALWAYS_INLINE void adc(bitblock256_t x, bitblock256_t y, carry_t & carry, bitblock256_t & sum);
42static IDISA_ALWAYS_INLINE void sbb(bitblock256_t x, bitblock256_t y, carry_t & borrow, bitblock256_t & difference);
43static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock256_t cursor, carry_t & carry, bitblock256_t & rslt);
44
45static IDISA_ALWAYS_INLINE void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum);
46static IDISA_ALWAYS_INLINE void sbb(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference);
47static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock256_t cursor, carry_t carry_in, carry_t & carry_out, bitblock256_t & rslt);
48
49static IDISA_ALWAYS_INLINE bitblock256_t convert (uint64_t s);
50static IDISA_ALWAYS_INLINE bitblock128_t convert_128 (uint64_t s);
51static IDISA_ALWAYS_INLINE uint64_t convert (bitblock256_t v);
52
53static IDISA_ALWAYS_INLINE bitblock256_t carry2bitblock(carry_t carry) {  return carry;}
54static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock256_t carry) {  return carry;}
55
56static IDISA_ALWAYS_INLINE carry_t carryout2carry(bitblock256_t carryout) {
57  return carryout;
58}
59
60static IDISA_ALWAYS_INLINE void add_ci_co(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum) {
61  bitblock256_t all_ones = simd256<1>::constant<1>();
62  bitblock256_t gen = simd_and(x, y);
63  bitblock256_t prop = simd_xor(x, y);
64  bitblock256_t partial_sum = simd256<64>::add(x, y);
65  bitblock256_t carry = simd_or(gen, simd_andc(prop, partial_sum));
66  bitblock256_t bubble = simd256<64>::eq(partial_sum, all_ones);
67  uint64_t carry_mask = hsimd256<64>::signmask(carry) * 2 + convert(carry_in);
68  uint64_t bubble_mask = hsimd256<64>::signmask(bubble);
69  uint64_t carry_scan_thru_bubbles = (carry_mask + bubble_mask) &~ bubble_mask;
70  uint64_t increments = carry_scan_thru_bubbles | (carry_scan_thru_bubbles - carry_mask);
71  carry_out = convert(increments >> 4);
72  uint64_t spread = 0x0000200040008001 * increments & 0x0001000100010001;
73  sum = simd256<64>::add(partial_sum, _mm256_cvtepu16_epi64(avx_select_lo128(convert(spread))));
74}
75
76static IDISA_ALWAYS_INLINE void sub_bi_bo(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference){
77  bitblock256_t gen = simd_andc(y, x);
78  bitblock256_t prop = simd_not(simd_xor(x, y));
79  bitblock256_t partial_diff = simd256<64>::sub(x, y);
80  bitblock256_t borrow = simd_or(gen, simd_and(prop, partial_diff));
81  bitblock256_t bubble = simd256<64>::eq(partial_diff, simd<1>::constant<0>());
82  uint64_t borrow_mask = hsimd256<64>::signmask(borrow) * 2 + convert(borrow_in);
83  uint64_t bubble_mask = hsimd256<64>::signmask(bubble);
84  uint64_t borrow_scan_thru_bubbles = (borrow_mask + bubble_mask) &~ bubble_mask;
85  uint64_t decrements = borrow_scan_thru_bubbles | (borrow_scan_thru_bubbles - borrow_mask);
86  borrow_out = convert(decrements >> 4);
87  uint64_t spread = 0x0000200040008001 * decrements & 0x0001000100010001;
88  difference = simd256<64>::sub(partial_diff, _mm256_cvtepu16_epi64(avx_select_lo128(convert(spread))));
89}
90
91static IDISA_ALWAYS_INLINE void adv_ci_co(bitblock256_t cursor, bitblock256_t carry_in, bitblock256_t & carry_out, bitblock256_t & rslt){
92        bitblock256_t shift_out = simd256<64>::srli<63>(cursor);
93        bitblock256_t low_bits = simd_or(mvmd256<64>::slli<1>(shift_out), carry_in);
94        carry_out = mvmd256<64>::srli<3>(shift_out);
95        rslt = simd_or(simd256<64>::add(cursor, cursor), low_bits);
96}
97
98IDISA_ALWAYS_INLINE bitblock256_t convert(uint64_t s)
99{
100  ubitblock b = {b._256 = simd256<128>::constant<0>()}; // = {0};
101  b._64[0] = s;
102  return b._256;
103}
104
105IDISA_ALWAYS_INLINE bitblock128_t convert_128(uint64_t s)
106{
107  ubitblock b = {b._256 = simd256<128>::constant<0>()}; // = {0};
108  b._64[0] = s;
109  return b._128[0];
110}
111
112IDISA_ALWAYS_INLINE uint64_t convert(bitblock256_t v)
113{
114  return (uint64_t) mvmd256<64>::extract<0>(v);
115}
116
117// The code below is not used.
118
119#ifdef AVX
120#define avx_select_lo128(x) \
121        ((__m128i) _mm256_castps256_ps128(x))
122
123#define avx_select_hi128(x) \
124        ((__m128i)(_mm256_extractf128_ps(x, 1)))
125
126#define avx_general_combine256(x, y) \
127   (_mm256_insertf128_ps(_mm256_castps128_ps256((__m128) y), (__m128) x, 1))
128
129IDISA_ALWAYS_INLINE void adc128(bitblock128_t x, bitblock128_t y, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & sum)
130{
131        bitblock128_t gen = simd_and(x, y);
132        bitblock128_t prop = simd_or(x, y);
133        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry_in);
134        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
135        sum = simd128<64>::add(c1, partial);
136        carry_out = simd128<128>::srli<127>(simd_or(gen, simd_andc(prop, sum)));
137}
138
139#ifndef ADCMAGIC
140IDISA_ALWAYS_INLINE void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum)
141{
142//  Really Slow!
143//        bitblock256_t gen = simd_and(x, y);
144//        bitblock256_t prop = simd_or(x, y);
145//        sum = simd256<256>::add(simd256<256>::add(x, y), carry2bitblock(carry_in));
146//        carry_out = bitblock2carry(simd256<256>::srli<255>(simd_or(gen, simd_andc(prop, sum))));
147
148  bitblock128_t x0 = avx_select_lo128(x);
149  bitblock128_t x1 = avx_select_hi128(x);
150  bitblock128_t y0 = avx_select_lo128(y);
151  bitblock128_t y1 = avx_select_hi128(y);
152  bitblock128_t c0 = avx_select_lo128(carry2bitblock(carry_in));
153  bitblock128_t s0, s1, c1, c2;
154  adc128(x0, y0, c0, c1, s0);
155  adc128(x1, y1, c1, c2, s1);
156  sum = avx_general_combine256(s1, s0);
157  carry_out = _mm256_castps128_ps256((__m128) c2);
158}
159#endif
160
161#ifdef ADCMAGIC
162
163#ifdef AVX2
164static inline void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum) {
165        bitblock256_t all_ones = simd256<1>::constant<1>();
166        bitblock256_t gen = simd_and(x, y);
167        bitblock256_t prop = simd_xor(x, y);
168  bitblock256_t partial_sum = simd256<64>::add(x, y);
169        bitblock256_t carry = simd_or(gen, simd_andc(prop, partial_sum));
170  bitblock256_t bubble = simd256<64>::eq(partial_sum, all_ones);
171  uint64_t carry_mask = hsimd256<64>::signmask(carry) * 2 + convert(carry_in);
172  uint64_t bubble_mask = hsimd256<64>::signmask(bubble);
173        uint64_t carry_scan_thru_bubbles = (carry_mask + bubble_mask) &~ bubble_mask;
174  uint64_t increments = carry_scan_thru_bubbles | (carry_scan_thru_bubbles - carry_mask);
175  carry_out = convert(increments >> 4);
176  uint64_t spread = 0x0000200040008001 * increments & 0x0001000100010001;
177  sum = simd256<64>::add(partial_sum, _mm256_cvtepu8_epi64(convert_128(spread)));
178}
179#else
180static inline void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum) {
181  bitblock128_t all_ones = simd128<1>::constant<1>();
182  //bitblock256_t gen = simd_and(x, y);
183  //bitblock256_t prop = simd_xor(x, y);
184  bitblock128_t x0 = avx_select_lo128(x);
185  bitblock128_t x1 = avx_select_hi128(x);
186  bitblock128_t y0 = avx_select_lo128(y);
187  bitblock128_t y1 = avx_select_hi128(y);
188        bitblock128_t sum0 = simd128<64>::add(x0, y0);
189        bitblock128_t sum1 = simd128<64>::add(x1, y1);
190  //bitblock256_t icarry = simd_or(gen, simd_andc(prop, avx_general_combine256(sum1, sum0)));
191        bitblock128_t icarry0 = simd_or(simd_and(x0, y0), simd_andc(simd_or(x0, y0), sum0));
192        bitblock128_t icarry1 = simd_or(simd_and(x1, y1), simd_andc(simd_or(x1, y1), sum1));
193        // A carry may bubble through a field if it is all ones.
194        bitblock128_t bubble0 = simd128<64>::eq(sum0, all_ones);
195        bitblock128_t bubble1 = simd128<64>::eq(sum1, all_ones);
196        //bitblock128_t bubble = hsimd128<64>::packss(bubble1, bubble0);
197        bitblock256_t bubble = avx_general_combine256(bubble1, bubble0);
198        //uint64_t carry_mask = _mm256_movemask_pd((__m256d) icarry) * 2 + convert(carry_in);
199        uint64_t carry_mask = hsimd128<64>::signmask(icarry1) * 8 + hsimd128<64>::signmask(icarry0) * 2 + convert(carry_in);
200        uint64_t bubble_mask = _mm256_movemask_pd((__m256d) bubble);
201        //uint64_t bubble_mask = hsimd128<64>::signmask(bubble1) * 4 + hsimd128<64>::signmask(bubble0);
202  //uint64_t bubble_mask = hsimd128<32>::signmask(bubble);
203  uint64_t carry_scan_thru_bubbles = (carry_mask + bubble_mask) &~ bubble_mask;
204        uint64_t increments = carry_scan_thru_bubbles | (carry_scan_thru_bubbles - carry_mask);
205        carry_out = convert(increments >> 4);
206        uint64_t spread = 0x0000200040008001 * increments & 0x0001000100010001;
207        bitblock128_t inc_32 = _mm_cvtepu16_epi32(_mm_cvtsi64_si128(spread));
208        bitblock128_t inc_64_0 = esimd128<32>::mergel(simd128<1>::constant<0>(), inc_32);
209        bitblock128_t inc_64_1 = esimd128<32>::mergeh(simd128<1>::constant<0>(), inc_32);
210        sum = avx_general_combine256(simd128<64>::add(sum1, inc_64_1), simd128<64>::add(sum0, inc_64_0));
211}
212#endif
213
214#endif
215
216IDISA_ALWAYS_INLINE void sbb128(bitblock128_t x, bitblock128_t y, bitblock128_t borrow_in, bitblock128_t & borrow_out, bitblock128_t & difference)
217{
218        bitblock128_t gen = simd_andc(y, x);
219        bitblock128_t prop = simd_not(simd_xor(x, y));
220        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), borrow_in);
221        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
222        difference = simd128<64>::sub(partial, b1);
223        borrow_out = simd128<128>::srli<127>(simd_or(gen, simd_and(prop, difference)));
224}
225
226
227IDISA_ALWAYS_INLINE void sbb(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference)
228{
229//        bitblock256_t gen = simd_andc(y, x);
230//        bitblock256_t prop = simd_not(simd_xor(x, y));
231//        difference = simd256<256>::sub(simd256<256>::sub(x, y), carry2bitblock(borrow_in));
232//        borrow_out = bitblock2carry(simd256<256>::srli<255>(simd_or(gen, simd_and(prop, difference))));
233  bitblock128_t x0 = avx_select_lo128(x);
234  bitblock128_t x1 = avx_select_hi128(x);
235  bitblock128_t y0 = avx_select_lo128(y);
236  bitblock128_t y1 = avx_select_hi128(y);
237  bitblock128_t b0 = avx_select_lo128(carry2bitblock(borrow_in));
238  bitblock128_t d0, d1, b1, b2;
239  sbb128(x0, y0, b0, b1, d0);
240  sbb128(x1, y1, b1, b2, d1);
241  difference = avx_general_combine256(d1, d0);
242  borrow_out = _mm256_castps128_ps256((__m128) b2);
243}
244
245IDISA_ALWAYS_INLINE void advance_with_carry128(bitblock128_t cursor, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & rslt)
246{
247bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
248bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry_in);
249carry_out = simd128<128>::srli<64>(shift_out);
250rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
251}
252
253IDISA_ALWAYS_INLINE void advance_with_carry(bitblock256_t cursor, carry_t carry_in, carry_t & carry_out, bitblock256_t & rslt)
254{
255  bitblock128_t cursor0 = avx_select_lo128(cursor);
256  bitblock128_t cursor1 = avx_select_hi128(cursor);
257  bitblock128_t  carry0 = avx_select_lo128(carry_in);
258  bitblock128_t  carry1, carry2, rslt0, rslt1;
259  advance_with_carry128(cursor0, carry0, carry1, rslt0);
260  advance_with_carry128(cursor1, carry1, carry2, rslt1);
261  rslt = avx_general_combine256(rslt1, rslt0);
262  carry_out = _mm256_castps128_ps256((__m128)carry2);
263}
264#endif
265
266#endif // BITBLOCK256_HPP_
Note: See TracBrowser for help on using the repository browser.