source: icGREP/icgrep-devel/icgrep/kernels/radix64.cpp @ 5311

Last change on this file since 5311 was 5311, checked in by nmedfort, 2 years ago

Removed BlockNo? + updated LookAhead? to use getInputStream.

File size: 28.8 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#include "radix64.h"
6#include <kernels/streamset.h>
7#include <IR_Gen/idisa_builder.h>
8#include <llvm/IR/Module.h>
9#include <llvm/Support/raw_ostream.h>
10
11using namespace llvm;
12
13namespace kernel {
14
15// This kernel produces an expanded input stream by duplicating every third byte.
16// It is implemented using SIMD shufflevector operations.  With 16-byte registers,
17// a single shufflevector operation produces 16 bytes of output data from the
18// 12 bytes of input data.   With 32-byte registers, 32 bytes of output data are
19// produced from 24 bytes of input data.
20//
21// Using aligned SIMD loads, an inner loop processes three registers full of input
22// data (i.e., three BytePacks) to produce four registers full of output.   This is
23// a 3 step process.
24// Step 1:  Load input_pack0, apply the shuffle operation to produce output_pack0.
25//          At this point 3/4 of the data in input_pack0 has been processed.
26// Step 2:  Load input_pack1, apply a shuffle operation to use the remaining
27//          1/4 of input_pack0 and 1/2 of input_pack1 to produce output_pack1.
28//          At this point 1/2 of the data in input_pack1 has been processed.
29// Step 3:  Load input_pack2, apply a shuffle operation to use the remaining 1/2
30//          of input_pack1 and 1/4 of input_pack2 to produce output_pack2.
31//          Then apply a further shuffle opertaion to use the remaining 3/4 of
32//          input_pack2 to produce output_pack3.
33
34// The doSegment method processes input in terms of tripleBlocks, 3 blocks of input,
35// producing 4 blocks of output.   Unless less than one tripleBlock remains, the
36// doSegment method always processes an integral number of tripleBlocks as a logical
37// segment.  Both input and output buffers are hence maintained at block boundaries,
38// with the input data completely processed for each tripleBlock.
39//
40// The pipeline must guarantee that the doSegment method is called with the
41// a continous buffer for the full segment (number of blocks).
42
43void expand3_4Kernel::generateDoSegmentMethod(Value *doFinal, const std::vector<Value *> &producerPos) {
44
45    BasicBlock * expand2_3entry = iBuilder->GetInsertBlock();
46    BasicBlock * expand_3_4_loop = CreateBasicBlock("expand_3_4_loop");
47    BasicBlock * expand3_4_loop_exit = CreateBasicBlock("expand3_4_loop_exit");
48    BasicBlock * finalStep1 = CreateBasicBlock("finalStep1");
49    BasicBlock * finalStep2 = CreateBasicBlock("finalStep2");
50    BasicBlock * step2load = CreateBasicBlock("step2load");
51    BasicBlock * step2store = CreateBasicBlock("step2store");
52    BasicBlock * finalStep3 = CreateBasicBlock("finalStep3");
53    BasicBlock * step3load = CreateBasicBlock("step3load");
54    BasicBlock * step3store = CreateBasicBlock("step3store");
55    BasicBlock * step3store2 = CreateBasicBlock("step3store2");
56    BasicBlock * itemsDone = CreateBasicBlock("itemsDone");
57    BasicBlock * expand3_4_final = CreateBasicBlock("expand3_4_final");
58    BasicBlock * expand3_4_exit = CreateBasicBlock("expand3_4_exit");
59   
60    // Determine the require shufflevector constants.
61    const unsigned PACK_SIZE = iBuilder->getStride()/8;
62   
63    // Construct a list of indexes in  the form
64    // 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, ...
65    unsigned sourceByteIndex = 0;
66    unsigned expand3_4_index[PACK_SIZE];
67    for (unsigned i = 0; i < PACK_SIZE; i++) {
68        expand3_4_index[i] = sourceByteIndex;
69        if (i % 4 != 2) sourceByteIndex++;
70    }
71    unsigned const expand3_4_offset[4] = {PACK_SIZE, 3*PACK_SIZE/4, PACK_SIZE/2, PACK_SIZE/4};
72    Value * expand_3_4_shuffle[4];
73    for (unsigned j = 0; j < 4; j++) {
74        std::vector<Constant *> Idxs;
75        for (unsigned i = 0; i < PACK_SIZE; i++) {
76            Idxs.push_back(ConstantInt::get(iBuilder->getInt32Ty(), expand3_4_offset[j] + expand3_4_index[i]));
77        }
78        expand_3_4_shuffle[j] = ConstantVector::get(Idxs);
79    }
80    Constant * Const3 = iBuilder->getSize(3);
81    Constant * Const4 = iBuilder->getSize(4);
82    Constant * tripleBlockSize = iBuilder->getSize(3 * iBuilder->getStride());
83    Constant * packSize = iBuilder->getSize(PACK_SIZE);
84    Constant * triplePackSize = iBuilder->getSize(3 * PACK_SIZE); // 3 packs per loop.
85    UndefValue * undefPack = UndefValue::get(iBuilder->fwVectorType(8));
86   
87    const unsigned packAlign = iBuilder->getBitBlockWidth()/8;
88
89    Value * processed = getProcessedItemCount("sourceStream");
90    Value * itemsAvail = iBuilder->CreateSub(producerPos[0], processed);
91   
92    //
93    // The main loop processes 3 packs of data at a time.  For doFinal
94    // processing, process all the remaining sets of 3 packs, otherwise
95    // process in multiples of 3 full blocks of data.
96    //
97    Value * loopDivisor = iBuilder->CreateSelect(doFinal, triplePackSize, tripleBlockSize);
98    Value * excessItems = iBuilder->CreateURem(itemsAvail, loopDivisor);
99    Value * loopItemsToDo = iBuilder->CreateSub(itemsAvail, excessItems);
100
101    // A block is made up of 8 packs.  Get the pointer to the first pack (changes the type of the pointer only).
102    Value * sourcePackPtr = getInputStream("sourceStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
103    Value * outputPackPtr = getOutputStream("expandedStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
104
105    Value * hasFullLoop = iBuilder->CreateICmpUGE(loopItemsToDo, triplePackSize);
106
107    iBuilder->CreateCondBr(hasFullLoop, expand_3_4_loop, expand3_4_loop_exit);
108    iBuilder->SetInsertPoint(expand_3_4_loop);
109    PHINode * loopInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
110    PHINode * loopOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
111    PHINode * loopItemsRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
112
113    loopInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
114    loopOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
115    loopItemsRemain->addIncoming(loopItemsToDo, expand2_3entry);
116
117    // Step 1 of the main loop.
118    Value * pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopInput_ptr, packAlign));
119    Value * expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
120    iBuilder->CreateBlockAlignedStore(expand0, loopOutput_ptr);
121    // Step 2 of the main loop.
122    Value * inPack1_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(1));
123    Value * outPack1_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(1));
124    Value * pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
125    Value * expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1, expand_3_4_shuffle[1]));
126    iBuilder->CreateBlockAlignedStore(expand1, outPack1_ptr);
127    // Step 3 of the main loop.
128    Value * inPack2_ptr = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(2));
129    Value * outPack2_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(2));
130    Value * pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
131    Value * expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1, pack2, expand_3_4_shuffle[2]));
132    iBuilder->CreateBlockAlignedStore(expand2, outPack2_ptr);
133    Value * outPack3_ptr = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(3));
134    Value * expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2, undefPack, expand_3_4_shuffle[3]));
135    iBuilder->CreateBlockAlignedStore(expand3, outPack3_ptr);
136
137    Value * loopNextInputPack = iBuilder->CreateGEP(loopInput_ptr, iBuilder->getInt32(3));
138    Value * remainingItems = iBuilder->CreateSub(loopItemsRemain, triplePackSize);
139
140    Value * loopNextOutputPack;
141    loopNextOutputPack = iBuilder->CreateGEP(loopOutput_ptr, iBuilder->getInt32(4));
142
143    loopInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
144    loopOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
145    loopItemsRemain->addIncoming(remainingItems, expand_3_4_loop);
146
147    Value * continueLoop = iBuilder->CreateICmpUGE(remainingItems, triplePackSize);
148    iBuilder->CreateCondBr(continueLoop, expand_3_4_loop, expand3_4_loop_exit);
149   
150    iBuilder->SetInsertPoint(expand3_4_loop_exit);
151    PHINode * loopExitInput_ptr = iBuilder->CreatePHI(sourcePackPtr->getType(), 2);
152    PHINode * loopExitOutput_ptr = iBuilder->CreatePHI(outputPackPtr->getType(), 2);
153    loopExitInput_ptr->addIncoming(sourcePackPtr, expand2_3entry);
154    loopExitOutput_ptr->addIncoming(outputPackPtr, expand2_3entry);
155    loopExitInput_ptr->addIncoming(loopNextInputPack, expand_3_4_loop);
156    loopExitOutput_ptr->addIncoming(loopNextOutputPack, expand_3_4_loop);
157
158    // Update the produced and processed items count based on the loopItemsToDo value.
159    processed = iBuilder->CreateAdd(processed, loopItemsToDo);
160    setProcessedItemCount("sourceStream", processed);
161   
162    // We have produced 4 output bytes for every 3 input bytes.
163    Value * totalProduced = iBuilder->CreateMul(iBuilder->CreateUDiv(processed, Const3), Const4);
164    setProducedItemCount("expandedStream", totalProduced);
165   
166    // Except for final segment processing, we are done.
167    iBuilder->CreateCondBr(doFinal, expand3_4_final, expand3_4_exit);
168
169    // Final segment processing.   Less than a triplePack remains.
170    iBuilder->SetInsertPoint(expand3_4_final);
171   
172    // There may be one or two remaining full packs and/or a partial pack.
173    //
174    // We have several cases depending on the number of reumaing items.  Let N = packSize
175    // (a) 0 remaining items: all done
176    // (b) 1..3N/4 remaining items:  do Step1 only, no items or pending data will remain
177    // (c) 3N/4+1 .. N remaining items:  do Step 1, do Step 2 for pending data from Step 1 only, there is no more input.
178    // (d) N+1 .. 6N/4 remaining items:  do Step 1 and Step 2, no items or pending data will remain.
179    // (e) 6N/4+1 .. 2N remaining items: do Steps 1 and 2, do Step 3 for pending data only, there is no more input.
180    // (f) 2N+1 .. 9N/4 remaining items: do Steps 1 and 2, do Step 3 up to the first write only.
181    // (g) 9N/4+1 .. 3N - 1 remaining items: do Steps 1, 2 and 3.
182    Value * condition_a = iBuilder->CreateICmpEQ(excessItems, ConstantInt::getNullValue(iBuilder->getSizeTy()));
183    iBuilder->CreateCondBr(condition_a, itemsDone, finalStep1);
184    // Final Step1 processing
185    iBuilder->SetInsertPoint(finalStep1);
186    pack0 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(loopExitInput_ptr, packAlign));
187    expand0 = iBuilder->bitCast(iBuilder->CreateShuffleVector(undefPack, pack0, expand_3_4_shuffle[0]));
188    iBuilder->CreateAlignedStore(expand0, loopExitOutput_ptr, packAlign);
189    Value * condition_b = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(3 * PACK_SIZE/4));
190    iBuilder->CreateCondBr(condition_b, itemsDone, finalStep2);
191    // Final Step 2 processing
192    iBuilder->SetInsertPoint(finalStep2);
193    Value * condition_c = iBuilder->CreateICmpULE(excessItems, packSize);
194    iBuilder->CreateCondBr(condition_c, step2store, step2load);
195    iBuilder->SetInsertPoint(step2load);
196    inPack1_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(1));
197    pack1 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack1_ptr, packAlign));
198    iBuilder->CreateBr(step2store);
199    iBuilder->SetInsertPoint(step2store);
200    PHINode * pack1phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
201    pack1phi->addIncoming(undefPack, finalStep2);
202    pack1phi->addIncoming(pack1, step2load);
203    outPack1_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(1));
204    expand1 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack0, pack1phi, expand_3_4_shuffle[1]));
205    iBuilder->CreateAlignedStore(expand1, outPack1_ptr, packAlign);
206    Value * condition_d = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(6 * PACK_SIZE/4));
207    iBuilder->CreateCondBr(condition_d, itemsDone, finalStep3);
208    // Final Step 3
209    iBuilder->SetInsertPoint(finalStep3);
210    Value * condition_e = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(2 * PACK_SIZE));
211    iBuilder->CreateCondBr(condition_e, step3store, step3load);
212    iBuilder->SetInsertPoint(step3load);
213    inPack2_ptr = iBuilder->CreateGEP(loopExitInput_ptr, iBuilder->getInt32(2));
214    pack2 = iBuilder->fwCast(8, iBuilder->CreateAlignedLoad(inPack2_ptr, packAlign));
215    iBuilder->CreateBr(step3store);
216    iBuilder->SetInsertPoint(step3store);
217    PHINode * pack2phi = iBuilder->CreatePHI(iBuilder->fwVectorType(8), 2);
218    pack2phi->addIncoming(undefPack, finalStep3);
219    pack2phi->addIncoming(pack2, step3load);
220    outPack2_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(2));
221    expand2 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack1phi, pack2phi, expand_3_4_shuffle[2]));
222    iBuilder->CreateAlignedStore(expand2, outPack2_ptr, packAlign);
223    Value * condition_f = iBuilder->CreateICmpULE(excessItems, iBuilder->getSize(9 * PACK_SIZE/4));
224    iBuilder->CreateCondBr(condition_f, itemsDone, step3store2);
225    iBuilder->SetInsertPoint(step3store2);
226    outPack3_ptr = iBuilder->CreateGEP(loopExitOutput_ptr, iBuilder->getInt32(3));
227    expand3 = iBuilder->bitCast(iBuilder->CreateShuffleVector(pack2phi, undefPack, expand_3_4_shuffle[3]));
228    iBuilder->CreateAlignedStore(expand3, outPack3_ptr, packAlign);
229    iBuilder->CreateBr(itemsDone);
230    //
231    iBuilder->SetInsertPoint(itemsDone);
232    processed = iBuilder->CreateAdd(processed, excessItems);
233    setProcessedItemCount("sourceStream", processed);
234
235    // We have produced 4 output bytes for every 3 input bytes.  If the number of input
236    // bytes is not a multiple of 3, then we have one more output byte for each excess
237    // input byte.
238    totalProduced = iBuilder->CreateAdd(iBuilder->CreateMul(iBuilder->CreateUDiv(processed, Const3), Const4), iBuilder->CreateURem(processed, Const3));
239    setProducedItemCount("expandedStream", totalProduced);
240   
241    iBuilder->CreateBr(expand3_4_exit);
242    iBuilder->SetInsertPoint(expand3_4_exit);
243}
244
245
246// Radix 64 determination, converting 3 bytes to 4 6-bit values.
247//
248//  00000000|zyxwvuts|rqpmnlkj|hgfedcba    Original
249//           zy                            bits to move 6 positions right
250//             xwvuts                      bits to move 8 positions left
251//                    rqpm                 bits to move 4 positions right
252//                        nlkj             bits to move 10 positions left
253//                             hqfedc      bits to move 2 positions right
254//                                   ba    bits to move 12 positions left
255//    xwvuts|  nlkjzy|  barqpm|  hgfedc    Target
256inline Value * radix64Kernel::processPackData(llvm::Value * bytepack) const {
257
258    Value * step_right_6 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00C00000));
259    Value * right_6_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_6), 6);
260
261    Value * step_left_8 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x003F0000));
262    Value * left_8_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_8), 8);
263    Value * mid = iBuilder->simd_or(right_6_result, left_8_result);
264
265    Value * step_right_4 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x0000F000));
266    Value * right_4_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_4), 4);
267    mid = iBuilder->simd_or(mid, right_4_result);
268
269    Value * step_left_10 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000F00));
270    Value * left_10_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_10), 10);
271    mid = iBuilder->simd_or(mid, left_10_result);
272
273    Value * step_right_2 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x000000FC));
274    Value * right_2_result = iBuilder->simd_srli(32, iBuilder->simd_and(bytepack, step_right_2), 2);
275    mid = iBuilder->simd_or(mid, right_2_result);
276
277    Value * step_left_12 = iBuilder->simd_fill(32, ConstantInt::get(iBuilder->getInt32Ty(), 0x00000003));
278    Value * left_12_result = iBuilder->simd_slli(32, iBuilder->simd_and(bytepack, step_left_12), 12);
279    mid = iBuilder->simd_or(mid, left_12_result);
280
281    return iBuilder->bitCast(mid);
282}
283
284void radix64Kernel::generateDoBlockMethod() {
285    for (unsigned i = 0; i < 8; i++) {
286        Value * expandedStream = getInputStream("expandedStream", iBuilder->getInt32(0), iBuilder->getInt32(i));
287        Value * bytepack = iBuilder->CreateBlockAlignedLoad(expandedStream);
288        Value * radix64pack = processPackData(bytepack);
289        Value * radix64stream = getOutputStream("radix64stream",iBuilder->getInt32(0), iBuilder->getInt32(i));
290        iBuilder->CreateBlockAlignedStore(radix64pack, radix64stream);
291    }
292    Value * produced = getProducedItemCount("radix64stream");
293    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
294    setProducedItemCount("radix64stream", produced);
295}
296
297void radix64Kernel::generateFinalBlockMethod(Value * remainingBytes) {
298
299    BasicBlock * entry = iBuilder->GetInsertBlock();
300    BasicBlock * radix64_loop = CreateBasicBlock("radix64_loop");
301    BasicBlock * loopExit = CreateBasicBlock("loopExit");
302    BasicBlock * fbExit = CreateBasicBlock("fbExit");
303    // Final Block arguments: self, remaining.
304    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
305
306    const unsigned PACK_SIZE = iBuilder->getStride()/8;
307    Constant * packSize = iBuilder->getSize(PACK_SIZE);
308
309    // Enter the loop only if there is at least one byte remaining to process.
310    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, radix64_loop);
311
312    iBuilder->SetInsertPoint(radix64_loop);
313    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
314    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
315    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
316    loopRemain->addIncoming(remainingBytes, entry);
317
318    Value * expandedStreamLoopPtr = getInputStream("expandedStream", iBuilder->getInt32(0), idx);
319    Value * bytepack = iBuilder->CreateBlockAlignedLoad(expandedStreamLoopPtr);
320    Value * radix64pack = processPackData(bytepack);
321
322    Value * radix64streamPtr = getOutputStream("radix64stream", iBuilder->getInt32(0), idx);
323    iBuilder->CreateBlockAlignedStore(radix64pack, radix64streamPtr);
324
325    Value* nextIdx = iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1));
326    idx->addIncoming(nextIdx, radix64_loop);
327    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
328    loopRemain->addIncoming(remainAfterLoop, radix64_loop);
329
330    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
331
332    iBuilder->CreateCondBr(continueLoop, radix64_loop, loopExit);
333
334    iBuilder->SetInsertPoint(loopExit);
335
336    iBuilder->CreateBr(fbExit);
337
338    iBuilder->SetInsertPoint(fbExit);
339    Value * outputNumberAdd = iBuilder->CreateSelect(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(0)), iBuilder->getSize(0), iBuilder->getSize(1));
340    Value * produced = iBuilder->CreateAdd(getProducedItemCount("radix64stream"), iBuilder->CreateAdd(remainingBytes, outputNumberAdd));
341    setProducedItemCount("radix64stream", produced);
342}
343
344inline llvm::Value* base64Kernel::processPackData(llvm::Value* bytepack) const {
345    Value * mask_gt_25 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(25)));
346    Value * mask_gt_51 = iBuilder->simd_ugt(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(51)));
347    Value * mask_eq_62 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(62)));
348    Value * mask_eq_63 = iBuilder->simd_eq(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8(63)));
349    // Strategy:
350    // 1. add ord('A') = 65 to all radix64 values, this sets the correct values for entries 0 to 25.
351    // 2. add ord('a') - ord('A') - (26 - 0) = 6 to all values >25, this sets the correct values for entries 0 to 51
352    // 3. subtract ord('a') - ord('0') + (52 - 26) = 75 to all values > 51, this sets the correct values for entries 0 to 61
353    // 4. subtract ord('0') - ord('+') + (62 - 52) = 15 for all values = 62
354    // 4. add ord('/') - ord('0') - (63 - 52) = 3 for all values = 63
355    Value * t0_25 = iBuilder->simd_add(8, bytepack, iBuilder->simd_fill(8, iBuilder->getInt8('A')));
356    Value * t0_51 = iBuilder->simd_add(8, t0_25, iBuilder->simd_and(mask_gt_25, iBuilder->simd_fill(8, iBuilder->getInt8(6))));
357    Value * t0_61 = iBuilder->simd_sub(8, t0_51, iBuilder->simd_and(mask_gt_51, iBuilder->simd_fill(8, iBuilder->getInt8(75))));
358    Value * t0_62 = iBuilder->simd_sub(8, t0_61, iBuilder->simd_and(mask_eq_62, iBuilder->simd_fill(8, iBuilder->getInt8(15))));
359    return iBuilder->simd_sub(8, t0_62, iBuilder->simd_and(mask_eq_63, iBuilder->simd_fill(8, iBuilder->getInt8(12))));
360}
361
362void base64Kernel::generateDoBlockMethod() {
363    for (unsigned i = 0; i < 8; i++) {
364        Value * radix64stream_ptr = getInputStream("radix64stream", iBuilder->getInt32(0), iBuilder->getInt32(i));
365        Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64stream_ptr);
366        Value* base64pack = processPackData(bytepack);
367        Value * base64stream_ptr = getOutputStream("base64stream", iBuilder->getInt32(0), iBuilder->getInt32(i));
368        iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64stream_ptr);
369    }
370    Value * produced = getProducedItemCount("base64stream");
371    produced = iBuilder->CreateAdd(produced, iBuilder->getSize(iBuilder->getStride()));
372    setProducedItemCount("base64stream", produced);
373}
374
375//// Special processing for the base 64 format.   The output must always contain a multiple
376//// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
377//// number of radix 64 values
378//void base64Kernel::generateFinalBlockMethod(Value * remainingBytes) {
379
380//    BasicBlock * entry = iBuilder->GetInsertBlock();
381//    BasicBlock * base64_loop = CreateBasicBlock("base64_loop");
382//    BasicBlock * loopExit = CreateBasicBlock("loopExit");
383//    BasicBlock * doPadding = CreateBasicBlock("doPadding");
384//    BasicBlock * doPadding2 = CreateBasicBlock("doPadding2");
385//    BasicBlock * fbExit = CreateBasicBlock("fbExit");
386
387//    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
388//    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
389//    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
390
391//    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
392
393//    // Enter the loop only if there is at least one byte remaining to process.
394//    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
395
396//    iBuilder->SetInsertPoint(base64_loop);
397//    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
398//    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
399//    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
400//    loopRemain->addIncoming(remainingBytes, entry);
401//    Value * radix64streamPtr = getInputStream("radix64stream", iBuilder->getInt32(0), idx);
402//    Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64streamPtr);
403//    Value * base64pack = processPackData(bytepack);
404//    Value * base64streamPtr = getOutputStream("base64stream", iBuilder->getInt32(0), idx);
405
406//    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64streamPtr);
407//    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
408//    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
409//    loopRemain->addIncoming(remainAfterLoop, base64_loop);
410
411//    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
412//    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
413
414//    iBuilder->SetInsertPoint(loopExit);
415//    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
416
417//    iBuilder->SetInsertPoint(doPadding);
418
419//    base64streamPtr = getOutputStream("base64stream", iBuilder->getInt32(0), idx);
420//    Value * i8streamPtr = iBuilder->CreatePointerCast(base64streamPtr, iBuilder->getInt8PtrTy());
421//    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8streamPtr, remainingBytes));
422//    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
423//    iBuilder->SetInsertPoint(doPadding2);
424//    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
425//    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8streamPtr, finalPadPos));
426//    iBuilder->CreateBr(fbExit);
427//    iBuilder->SetInsertPoint(fbExit);
428//    Value * produced = iBuilder->CreateAdd(getProducedItemCount("base64stream"), iBuilder->CreateAdd(remainingBytes, padBytes));
429//    setProducedItemCount("base64stream", produced);
430//}
431
432// Special processing for the base 64 format.   The output must always contain a multiple
433// of 4 bytes.   When the number of radix 64 values is not a multiple of 4
434// number of radix 64 values
435void base64Kernel::generateFinalBlockMethod(Value * remainingBytes) {
436
437    BasicBlock * entry = iBuilder->GetInsertBlock();
438    BasicBlock * base64_loop = CreateBasicBlock("base64_loop");
439    BasicBlock * loopExit = CreateBasicBlock("loopExit");
440    BasicBlock * doPadding = CreateBasicBlock("doPadding");
441    BasicBlock * doPadding2 = CreateBasicBlock("doPadding2");
442    BasicBlock * fbExit = CreateBasicBlock("fbExit");
443
444    Value * remainMod4 = iBuilder->CreateAnd(remainingBytes, iBuilder->getSize(3));
445    Value * padBytes = iBuilder->CreateSub(iBuilder->getSize(4), remainMod4);
446    padBytes = iBuilder->CreateAnd(padBytes, iBuilder->getSize(3));
447
448    Constant * packSize = iBuilder->getSize(iBuilder->getStride() / 8);
449
450    // Enter the loop only if there is at least one byte remaining to process.
451    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainingBytes, iBuilder->getSize(0)), fbExit, base64_loop);
452
453    iBuilder->SetInsertPoint(base64_loop);
454    PHINode * idx = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2);
455    PHINode * loopRemain = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
456    idx->addIncoming(ConstantInt::getNullValue(iBuilder->getInt32Ty()), entry);
457    loopRemain->addIncoming(remainingBytes, entry);
458    Value * radix64streamPtr = getInputStream("radix64stream", iBuilder->getInt32(0), idx);
459    Value * bytepack = iBuilder->CreateBlockAlignedLoad(radix64streamPtr);
460    Value * base64pack = processPackData(bytepack);
461    Value * base64streamPtr = getOutputStream("base64stream", iBuilder->getInt32(0), idx);
462    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(base64pack), base64streamPtr);
463    idx->addIncoming(iBuilder->CreateAdd(idx, ConstantInt::get(iBuilder->getInt32Ty(), 1)), base64_loop);
464    Value* remainAfterLoop = iBuilder->CreateSub(loopRemain, packSize);
465    loopRemain->addIncoming(remainAfterLoop, base64_loop);
466
467    Value* continueLoop = iBuilder->CreateICmpSGT(remainAfterLoop, iBuilder->getSize(0));
468    iBuilder->CreateCondBr(continueLoop, base64_loop, loopExit);
469
470    iBuilder->SetInsertPoint(loopExit);
471    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(padBytes, iBuilder->getSize(0)), fbExit, doPadding);
472
473    iBuilder->SetInsertPoint(doPadding);
474    Value * i8output_ptr = getOutputStream("base64stream", iBuilder->getInt32(0));
475    i8output_ptr = iBuilder->CreatePointerCast(i8output_ptr, iBuilder->getInt8PtrTy());
476    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, remainingBytes));
477    iBuilder->CreateCondBr(iBuilder->CreateICmpEQ(remainMod4, iBuilder->getSize(3)), fbExit, doPadding2);
478    iBuilder->SetInsertPoint(doPadding2);
479    Value * finalPadPos = iBuilder->CreateAdd(remainingBytes, iBuilder->getSize(1));
480    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), '='), iBuilder->CreateGEP(i8output_ptr, finalPadPos));
481    iBuilder->CreateBr(fbExit);
482    iBuilder->SetInsertPoint(fbExit);
483    Value * produced = iBuilder->CreateAdd(getProducedItemCount("base64stream"), iBuilder->CreateAdd(remainingBytes, padBytes));
484    setProducedItemCount("base64stream", produced);
485}
486
487expand3_4Kernel::expand3_4Kernel(IDISA::IDISA_Builder * iBuilder)
488: SegmentOrientedKernel(iBuilder, "expand3_4",
489            {Binding{iBuilder->getStreamSetTy(1, 8), "sourceStream"}},
490            {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}},
491            {}, {}, {}) {
492    setDoBlockUpdatesProducedItemCountsAttribute(true);
493}
494
495radix64Kernel::radix64Kernel(IDISA::IDISA_Builder * iBuilder)
496: BlockOrientedKernel(iBuilder, "radix64",
497            {Binding{iBuilder->getStreamSetTy(1, 8), "expandedStream"}},
498            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
499            {}, {}, {}) {
500    setDoBlockUpdatesProducedItemCountsAttribute(true);
501}
502
503base64Kernel::base64Kernel(IDISA::IDISA_Builder * iBuilder)
504: BlockOrientedKernel(iBuilder, "base64",
505            {Binding{iBuilder->getStreamSetTy(1, 8), "radix64stream"}},
506            {Binding{iBuilder->getStreamSetTy(1, 8), "base64stream"}},
507            {}, {}, {}) {
508    setDoBlockUpdatesProducedItemCountsAttribute(true);
509}
510
511}
Note: See TracBrowser for help on using the repository browser.