source: trunk/lib/bitblock256.hpp @ 3441

Last change on this file since 3441 was 3441, checked in by linmengl, 6 years ago

make avx2 can run now

File size: 12.0 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);
39
40static IDISA_ALWAYS_INLINE void adc(bitblock256_t x, bitblock256_t y, carry_t & carry, bitblock256_t & sum);
41static IDISA_ALWAYS_INLINE void sbb(bitblock256_t x, bitblock256_t y, carry_t & borrow, bitblock256_t & difference);
42static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock256_t cursor, carry_t & carry, bitblock256_t & rslt);
43
44static IDISA_ALWAYS_INLINE void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum);
45static IDISA_ALWAYS_INLINE void sbb(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference);
46static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock256_t cursor, carry_t carry_in, carry_t & carry_out, bitblock256_t & rslt);
47
48static IDISA_ALWAYS_INLINE bitblock256_t convert (uint64_t s);
49static IDISA_ALWAYS_INLINE bitblock128_t convert_128 (uint64_t s);
50static IDISA_ALWAYS_INLINE uint64_t convert (bitblock256_t v);
51
52static IDISA_ALWAYS_INLINE bitblock256_t carry2bitblock(carry_t carry) {  return carry;}
53static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock256_t carry) {  return carry;}
54
55static inline void add_ci_co(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum) {
56  bitblock256_t all_ones = simd256<1>::constant<1>();
57  bitblock256_t gen = simd_and(x, y);
58  bitblock256_t prop = simd_xor(x, y);
59  bitblock256_t partial_sum = simd256<64>::add(x, y);
60  bitblock256_t carry = simd_or(gen, simd_andc(prop, partial_sum));
61  bitblock256_t bubble = simd256<64>::eq(partial_sum, all_ones);
62  uint64_t carry_mask = hsimd256<64>::signmask(carry) * 2 + convert(carry_in);
63  uint64_t bubble_mask = hsimd256<64>::signmask(bubble);
64  uint64_t carry_scan_thru_bubbles = (carry_mask + bubble_mask) &~ bubble_mask;
65  uint64_t increments = carry_scan_thru_bubbles | (carry_scan_thru_bubbles - carry_mask);
66  carry_out = convert(increments >> 4);
67  uint64_t spread = 0x0000200040008001 * increments & 0x0001000100010001;
68  sum = simd256<64>::add(partial_sum, _mm256_cvtepu8_epi64(avx_select_lo128(convert(spread))));
69}
70
71IDISA_ALWAYS_INLINE void sub_bi_bo(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference){
72        bitblock256_t gen = simd_andc(y, x);
73        bitblock256_t prop = simd_not(simd_xor(x, y));
74        bitblock256_t partial = simd256<128>::sub(simd256<128>::sub(x, y), borrow_in);
75        bitblock256_t b1 = simd256<256>::slli<128>(simd256<128>::srli<127>(simd_or(gen, simd_and(prop, partial))));
76        difference = simd256<128>::sub(partial, b1);
77        borrow_out = simd_or(gen, simd_and(prop, difference));
78}
79
80static IDISA_ALWAYS_INLINE void adv_ci_co(bitblock256_t cursor, bitblock256_t carry_in, bitblock256_t & carry_out, bitblock256_t & rslt){
81        bitblock256_t shift_out = simd256<64>::srli<63>(cursor);
82        bitblock256_t low_bits = simd_or(mvmd256<64>::slli<1>(shift_out), carry_in);
83        carry_out = cursor;
84        rslt = simd_or(simd256<64>::add(cursor, cursor), low_bits);
85}
86
87
88#ifdef AVX
89#define avx_select_lo128(x) \
90        ((__m128i) _mm256_castps256_ps128(x))
91
92#define avx_select_hi128(x) \
93        ((__m128i)(_mm256_extractf128_ps(x, 1)))
94
95#define avx_general_combine256(x, y) \
96   (_mm256_insertf128_ps(_mm256_castps128_ps256((__m128) y), (__m128) x, 1))
97
98IDISA_ALWAYS_INLINE void adc128(bitblock128_t x, bitblock128_t y, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & sum)
99{
100        bitblock128_t gen = simd_and(x, y);
101        bitblock128_t prop = simd_or(x, y);
102        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry_in);
103        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
104        sum = simd128<64>::add(c1, partial);
105        carry_out = simd128<128>::srli<127>(simd_or(gen, simd_andc(prop, sum)));
106}
107
108#ifndef ADCMAGIC
109IDISA_ALWAYS_INLINE void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum)
110{
111//  Really Slow!
112//        bitblock256_t gen = simd_and(x, y);
113//        bitblock256_t prop = simd_or(x, y);
114//        sum = simd256<256>::add(simd256<256>::add(x, y), carry2bitblock(carry_in));
115//        carry_out = bitblock2carry(simd256<256>::srli<255>(simd_or(gen, simd_andc(prop, sum))));
116
117  bitblock128_t x0 = avx_select_lo128(x);
118  bitblock128_t x1 = avx_select_hi128(x);
119  bitblock128_t y0 = avx_select_lo128(y);
120  bitblock128_t y1 = avx_select_hi128(y);
121  bitblock128_t c0 = avx_select_lo128(carry2bitblock(carry_in));
122  bitblock128_t s0, s1, c1, c2;
123  adc128(x0, y0, c0, c1, s0);
124  adc128(x1, y1, c1, c2, s1);
125  sum = avx_general_combine256(s1, s0);
126  carry_out = _mm256_castps128_ps256((__m128) c2);
127}
128#endif
129
130#ifdef ADCMAGIC
131
132#ifdef AVX2
133static inline void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum) {
134        bitblock256_t all_ones = simd256<1>::constant<1>();
135        bitblock256_t gen = simd_and(x, y);
136        bitblock256_t prop = simd_xor(x, y);
137  bitblock256_t partial_sum = simd256<64>::add(x, y);
138        bitblock256_t carry = simd_or(gen, simd_andc(prop, partial_sum));
139  bitblock256_t bubble = simd256<64>::eq(partial_sum, all_ones);
140  uint64_t carry_mask = hsimd256<64>::signmask(carry) * 2 + convert(carry_in);
141  uint64_t bubble_mask = hsimd256<64>::signmask(bubble);
142        uint64_t carry_scan_thru_bubbles = (carry_mask + bubble_mask) &~ bubble_mask;
143  uint64_t increments = carry_scan_thru_bubbles | (carry_scan_thru_bubbles - carry_mask);
144  carry_out = convert(increments >> 4);
145  uint64_t spread = 0x0000200040008001 * increments & 0x0001000100010001;
146  sum = simd256<64>::add(partial_sum, _mm256_cvtepu8_epi64(convert_128(spread)));
147}
148#else
149static inline void adc(bitblock256_t x, bitblock256_t y, carry_t carry_in, carry_t & carry_out, bitblock256_t & sum) {
150  bitblock128_t all_ones = simd128<1>::constant<1>();
151  //bitblock256_t gen = simd_and(x, y);
152  //bitblock256_t prop = simd_xor(x, y);
153  bitblock128_t x0 = avx_select_lo128(x);
154  bitblock128_t x1 = avx_select_hi128(x);
155  bitblock128_t y0 = avx_select_lo128(y);
156  bitblock128_t y1 = avx_select_hi128(y);
157        bitblock128_t sum0 = simd128<64>::add(x0, y0);
158        bitblock128_t sum1 = simd128<64>::add(x1, y1);
159  //bitblock256_t icarry = simd_or(gen, simd_andc(prop, avx_general_combine256(sum1, sum0)));
160        bitblock128_t icarry0 = simd_or(simd_and(x0, y0), simd_andc(simd_or(x0, y0), sum0));
161        bitblock128_t icarry1 = simd_or(simd_and(x1, y1), simd_andc(simd_or(x1, y1), sum1));
162        // A carry may bubble through a field if it is all ones.
163        bitblock128_t bubble0 = simd128<64>::eq(sum0, all_ones);
164        bitblock128_t bubble1 = simd128<64>::eq(sum1, all_ones);
165        //bitblock128_t bubble = hsimd128<64>::packss(bubble1, bubble0);
166        bitblock256_t bubble = avx_general_combine256(bubble1, bubble0);
167        //uint64_t carry_mask = _mm256_movemask_pd((__m256d) icarry) * 2 + convert(carry_in);
168        uint64_t carry_mask = hsimd128<64>::signmask(icarry1) * 8 + hsimd128<64>::signmask(icarry0) * 2 + convert(carry_in);
169        uint64_t bubble_mask = _mm256_movemask_pd((__m256d) bubble);
170        //uint64_t bubble_mask = hsimd128<64>::signmask(bubble1) * 4 + hsimd128<64>::signmask(bubble0);
171  //uint64_t bubble_mask = hsimd128<32>::signmask(bubble);
172  uint64_t carry_scan_thru_bubbles = (carry_mask + bubble_mask) &~ bubble_mask;
173        uint64_t increments = carry_scan_thru_bubbles | (carry_scan_thru_bubbles - carry_mask);
174        carry_out = convert(increments >> 4);
175        uint64_t spread = 0x0000200040008001 * increments & 0x0001000100010001;
176        bitblock128_t inc_32 = _mm_cvtepu16_epi32(_mm_cvtsi64_si128(spread));
177        bitblock128_t inc_64_0 = esimd128<32>::mergel(simd128<1>::constant<0>(), inc_32);
178        bitblock128_t inc_64_1 = esimd128<32>::mergeh(simd128<1>::constant<0>(), inc_32);
179        sum = avx_general_combine256(simd128<64>::add(sum1, inc_64_1), simd128<64>::add(sum0, inc_64_0));
180}
181#endif
182
183#endif
184
185
186
187
188
189IDISA_ALWAYS_INLINE void sbb128(bitblock128_t x, bitblock128_t y, bitblock128_t borrow_in, bitblock128_t & borrow_out, bitblock128_t & difference)
190{
191        bitblock128_t gen = simd_andc(y, x);
192        bitblock128_t prop = simd_not(simd_xor(x, y));
193        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), borrow_in);
194        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
195        difference = simd128<64>::sub(partial, b1);
196        borrow_out = simd128<128>::srli<127>(simd_or(gen, simd_and(prop, difference)));
197}
198
199
200IDISA_ALWAYS_INLINE void sbb(bitblock256_t x, bitblock256_t y, carry_t borrow_in, carry_t & borrow_out, bitblock256_t & difference)
201{
202//        bitblock256_t gen = simd_andc(y, x);
203//        bitblock256_t prop = simd_not(simd_xor(x, y));
204//        difference = simd256<256>::sub(simd256<256>::sub(x, y), carry2bitblock(borrow_in));
205//        borrow_out = bitblock2carry(simd256<256>::srli<255>(simd_or(gen, simd_and(prop, difference))));
206  bitblock128_t x0 = avx_select_lo128(x);
207  bitblock128_t x1 = avx_select_hi128(x);
208  bitblock128_t y0 = avx_select_lo128(y);
209  bitblock128_t y1 = avx_select_hi128(y);
210  bitblock128_t b0 = avx_select_lo128(carry2bitblock(borrow_in));
211  bitblock128_t d0, d1, b1, b2;
212  sbb128(x0, y0, b0, b1, d0);
213  sbb128(x1, y1, b1, b2, d1);
214  difference = avx_general_combine256(d1, d0);
215  borrow_out = _mm256_castps128_ps256((__m128) b2);
216}
217
218IDISA_ALWAYS_INLINE void advance_with_carry128(bitblock128_t cursor, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & rslt)
219{
220bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
221bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry_in);
222carry_out = simd128<128>::srli<64>(shift_out);
223rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
224}
225
226IDISA_ALWAYS_INLINE void advance_with_carry(bitblock256_t cursor, carry_t carry_in, carry_t & carry_out, bitblock256_t & rslt)
227{
228  bitblock128_t cursor0 = avx_select_lo128(cursor);
229  bitblock128_t cursor1 = avx_select_hi128(cursor);
230  bitblock128_t  carry0 = avx_select_lo128(carry_in);
231  bitblock128_t  carry1, carry2, rslt0, rslt1;
232  advance_with_carry128(cursor0, carry0, carry1, rslt0);
233  advance_with_carry128(cursor1, carry1, carry2, rslt1);
234  rslt = avx_general_combine256(rslt1, rslt0);
235  carry_out = _mm256_castps128_ps256((__m128)carry2);
236}
237#endif
238
239IDISA_ALWAYS_INLINE bitblock256_t convert(uint64_t s)
240{
241  ubitblock b = {b._256 = simd256<128>::constant<0>()}; // = {0};
242  b._64[0] = s;
243  return b._256;
244}
245
246IDISA_ALWAYS_INLINE bitblock128_t convert_128(uint64_t s)
247{
248  ubitblock b = {b._256 = simd256<128>::constant<0>()}; // = {0};
249  b._64[0] = s;
250  return b._128[0];
251}
252
253IDISA_ALWAYS_INLINE uint64_t convert(bitblock256_t v)
254{
255  return (uint64_t) mvmd256<64>::extract<0>(v);
256}
257
258
259#endif // BITBLOCK256_HPP_
Note: See TracBrowser for help on using the repository browser.