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

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

Continued work on eliminating BlockNo?

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