1 | #ifndef CARRYQ_HPP_ |
---|
2 | #define CARRYQ_HPP_ |
---|
3 | |
---|
4 | /*============================================================================= |
---|
5 | carryQ.hpp - Pablo compiler support for carry introduction. |
---|
6 | Ken Herdy, Robert D. Cameron |
---|
7 | Copyright (C) 2012, Robert D. Cameron, Kenneth S. Herdy. |
---|
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 | April 2012 |
---|
12 | =============================================================================*/ |
---|
13 | |
---|
14 | #include <stdint.h> |
---|
15 | #include <iostream> |
---|
16 | |
---|
17 | #include "bitblock.hpp" |
---|
18 | #include "stdio.h" |
---|
19 | |
---|
20 | /////////////////////////////////////////////////////////////////////////////// |
---|
21 | // |
---|
22 | // Carry method variants. |
---|
23 | // |
---|
24 | // BitBlock_op_ci_co() - standard block non while loop statement and in final block if ignore the carry out |
---|
25 | // BitBlock_op_co() - standard block while loop and in final block while loop if ignore carry out |
---|
26 | // BitBlock_op_ci() - final block non while loop statement |
---|
27 | // BitBlock_op() - final while loop statement |
---|
28 | // |
---|
29 | // BitBlock_op_ci(), BitBlock_op() methods not implemented to reduce the total number of |
---|
30 | // methods and Pablo compiler complexity. |
---|
31 | // |
---|
32 | /////////////////////////////////////////////////////////////////////////////// |
---|
33 | |
---|
34 | #define interpose32(x,y,pos) interpose32_<pos>(x,y) |
---|
35 | template<uint32_t n> |
---|
36 | IDISA_ALWAYS_INLINE BitBlock interpose32_(BitBlock s, BitBlock s32) { |
---|
37 | return simd_or(simd<32>::slli<n>(s), simd<32>::srli<32-n>(s32)); |
---|
38 | } |
---|
39 | |
---|
40 | template<uint32_t n> |
---|
41 | IDISA_ALWAYS_INLINE BitBlock interpose64_(BitBlock s, BitBlock s64) { |
---|
42 | return simd_or(simd<64>::slli<n>(s), simd<64>::srli<64-n>(s64)); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | #include <string.h> |
---|
47 | |
---|
48 | |
---|
49 | // Array of BitBlock implementation. |
---|
50 | template <uint16_t CarryCount, uint16_t AdvanceNCount> |
---|
51 | class CarryArray { |
---|
52 | |
---|
53 | public: |
---|
54 | |
---|
55 | #define Carry0 simd<BLOCK_SIZE>::constant<0>() |
---|
56 | #define Carry1 simd<BLOCK_SIZE>::constant<1>() |
---|
57 | |
---|
58 | BitBlock cq[CarryCount + AdvanceNCount]; |
---|
59 | //BitBlock pending64[AdvanceNCount]; |
---|
60 | CarryArray() |
---|
61 | { |
---|
62 | memset (cq, 0, sizeof(BitBlock) * (CarryCount + AdvanceNCount)); |
---|
63 | //memset(pending64, 0, sizeof(BitBlock) * AdvanceNCount); |
---|
64 | } |
---|
65 | ~CarryArray() {} |
---|
66 | |
---|
67 | IDISA_ALWAYS_INLINE BitBlock BitBlock_advance_ci_co(BitBlock strm, BitBlock carryin, uint16_t carryno) |
---|
68 | { |
---|
69 | BitBlock rslt; |
---|
70 | advance_with_carry(strm, carryin, cq[carryno], rslt); |
---|
71 | return rslt; |
---|
72 | } |
---|
73 | |
---|
74 | IDISA_ALWAYS_INLINE BitBlock BitBlock_add_ci_co(BitBlock strm1, BitBlock strm2, BitBlock carryin, const uint16_t carryno) |
---|
75 | { |
---|
76 | BitBlock sum; |
---|
77 | adc(strm1, strm2, carryin, cq[carryno], sum); |
---|
78 | return sum; |
---|
79 | } |
---|
80 | |
---|
81 | IDISA_ALWAYS_INLINE BitBlock BitBlock_sub_ci_co(BitBlock strm1, BitBlock strm2, BitBlock carryin, uint16_t carryno) |
---|
82 | { |
---|
83 | BitBlock diff; |
---|
84 | sbb(strm1, strm2, carryin, cq[carryno], diff); |
---|
85 | return diff; |
---|
86 | } |
---|
87 | |
---|
88 | IDISA_ALWAYS_INLINE BitBlock BitBlock_scantofirst(BitBlock charclass, BitBlock carryin, uint16_t carryno) |
---|
89 | { |
---|
90 | BitBlock marker; |
---|
91 | // BitBlock c = carry_flip(carryin); |
---|
92 | adc(simd<BLOCK_SIZE>::constant<0>(), simd_not(charclass), carryin, cq[carryno], marker); |
---|
93 | // cq[carryno] = carry_flip(cq[carryno]); |
---|
94 | return simd_and(marker, charclass); |
---|
95 | } |
---|
96 | |
---|
97 | IDISA_ALWAYS_INLINE BitBlock BitBlock_scanthru_ci_co(BitBlock markers0, BitBlock charclass, BitBlock carryin, uint16_t carryno) |
---|
98 | { |
---|
99 | BitBlock markers1; |
---|
100 | adc(markers0, charclass, carryin, cq[carryno], markers1); |
---|
101 | return simd_andc(markers1, charclass); |
---|
102 | } |
---|
103 | |
---|
104 | IDISA_ALWAYS_INLINE BitBlock BitBlock_advance_then_scanthru(BitBlock markers0, BitBlock charclass, BitBlock carryin, uint16_t carryno) |
---|
105 | { |
---|
106 | BitBlock markers1; |
---|
107 | //assert(!bitblock::any(simd_and(markers0, charclass))); |
---|
108 | adc(markers0, simd_or(charclass, markers0), carryin, cq[carryno], markers1); |
---|
109 | return simd_andc(markers1, charclass); |
---|
110 | } |
---|
111 | |
---|
112 | IDISA_ALWAYS_INLINE BitBlock BitBlock_span_upto(BitBlock starts, BitBlock follows, BitBlock carryin, uint16_t carryno) |
---|
113 | { |
---|
114 | BitBlock span; |
---|
115 | sbb(follows, starts, carryin, cq[carryno], span); |
---|
116 | return span; |
---|
117 | } |
---|
118 | |
---|
119 | IDISA_ALWAYS_INLINE BitBlock BitBlock_inclusive_span(BitBlock starts, BitBlock ends, BitBlock carryin, uint16_t carryno) |
---|
120 | { |
---|
121 | BitBlock span; |
---|
122 | sbb(ends, starts, carryin, cq[carryno], span); |
---|
123 | return simd_or(span, ends); |
---|
124 | } |
---|
125 | |
---|
126 | IDISA_ALWAYS_INLINE BitBlock BitBlock_exclusive_span(BitBlock starts, BitBlock ends, BitBlock carryin, uint16_t carryno) |
---|
127 | { |
---|
128 | BitBlock span; |
---|
129 | sbb(ends, starts, carryin, cq[carryno], span); |
---|
130 | return simd_andc(span, starts); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | IDISA_ALWAYS_INLINE BitBlock BitBlock_advance32_ci_co(BitBlock strm, uint32_t pending_in, uint32_t & pending_out) |
---|
135 | { |
---|
136 | pending_out = (uint32_t) mvmd<32>::extract< (sizeof(BitBlock)/sizeof(pending_out))-1 >(strm); |
---|
137 | return simd_or(simd<BLOCK_SIZE>::slli<32>(strm), mvmd<BLOCK_SIZE>::fill((uint64_t)pending_in)); |
---|
138 | } |
---|
139 | |
---|
140 | template <int n> IDISA_ALWAYS_INLINE BitBlock BitBlock_advance_n_(BitBlock strm, BitBlock pending_in, uint16_t pendingno) |
---|
141 | { |
---|
142 | BitBlock half_block_shifted = esimd<BLOCK_SIZE/2>::mergel(strm, pending_in); |
---|
143 | cq[CarryCount + pendingno] = bitblock::srli<BLOCK_SIZE/2>(strm); |
---|
144 | //pending64[pendingno] = bitblock::srli<BLOCK_SIZE/2>(strm); |
---|
145 | BitBlock result = simd_or(simd<BLOCK_SIZE/2>::srli<(BLOCK_SIZE/2)-n>(half_block_shifted), |
---|
146 | simd<BLOCK_SIZE/2>::slli<n>(strm)); |
---|
147 | return result; |
---|
148 | } |
---|
149 | |
---|
150 | IDISA_ALWAYS_INLINE bool CarryTest(uint16_t carryno, uint16_t carry_count) |
---|
151 | |
---|
152 | |
---|
153 | { |
---|
154 | BitBlock c1 = cq[carryno]; |
---|
155 | int ubound = carryno + carry_count; |
---|
156 | for (int i = carryno + 1; i < ubound ; i++) { |
---|
157 | c1 = carry_or(c1, cq[i]); |
---|
158 | } |
---|
159 | return test_carry(c1); |
---|
160 | } |
---|
161 | |
---|
162 | IDISA_ALWAYS_INLINE void CarryDequeueEnqueue(uint16_t carryno, uint16_t carry_count) |
---|
163 | { |
---|
164 | return; |
---|
165 | } |
---|
166 | |
---|
167 | IDISA_ALWAYS_INLINE void CarryQ_Adjust(uint16_t carry_count) |
---|
168 | { |
---|
169 | return; |
---|
170 | } |
---|
171 | |
---|
172 | IDISA_ALWAYS_INLINE void CarryCombine(BitBlock local_cq[], uint16_t carryno, uint16_t carry_count) |
---|
173 | { |
---|
174 | for (int i = 0; i < carry_count; i++) { |
---|
175 | cq[carryno+i] = carry_or(cq[carryno+i], local_cq[i]); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | IDISA_ALWAYS_INLINE void CarryCombine1(uint16_t carryno, uint16_t carry2) |
---|
180 | { |
---|
181 | cq[carryno] = carry_or(cq[carryno], cq[carry2]); |
---|
182 | cq[carry2] = Carry0; |
---|
183 | } |
---|
184 | |
---|
185 | IDISA_ALWAYS_INLINE BitBlock get_carry_in(uint16_t carryno) const |
---|
186 | { |
---|
187 | return carry2bitblock(cq[carryno]); |
---|
188 | } |
---|
189 | |
---|
190 | // IDISA_ALWAYS_INLINE BitBlock get_pending64(uint16_t advance_n_blkno) const |
---|
191 | // { |
---|
192 | // return pending64[advance_n_blkno]; |
---|
193 | // } |
---|
194 | |
---|
195 | IDISA_ALWAYS_INLINE BitBlock get_pending64(uint16_t advance_n_blkno) const |
---|
196 | { |
---|
197 | return cq[CarryCount + advance_n_blkno]; |
---|
198 | } |
---|
199 | |
---|
200 | //private: |
---|
201 | // helpers |
---|
202 | IDISA_ALWAYS_INLINE BitBlock carry_flip(BitBlock carry) const |
---|
203 | { |
---|
204 | return simd_xor(carry, Carry1); |
---|
205 | } |
---|
206 | |
---|
207 | IDISA_ALWAYS_INLINE bool test_carry(BitBlock carry) const |
---|
208 | { |
---|
209 | return bitblock::any(carry); |
---|
210 | } |
---|
211 | |
---|
212 | IDISA_ALWAYS_INLINE BitBlock carry_or(BitBlock carry1, BitBlock carry2) const |
---|
213 | { |
---|
214 | return simd_or(carry1, carry2); |
---|
215 | } |
---|
216 | |
---|
217 | #undef Carry0 |
---|
218 | #undef Carry1 |
---|
219 | |
---|
220 | }; |
---|
221 | |
---|
222 | #endif // CARRYQ_HPP_ |
---|