/
examplescommon.lua
1111 lines (910 loc) · 42.6 KB
/
examplescommon.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local R = require "rigel"
local rigel = R
local RM = require "modules"
local types = require "types"
local S = require "systolic"
local Ssugar = require "systolicsugar"
local modules = RM
local J = require "common"
local memoize = J.memoize
local err = J.err
local f = require "fixed_new"
if terralib~=nil then CT=require("examplescommonTerra") end
local C = {}
C.identity = memoize(function(A)
local identity = RM.lift( "identity_"..J.verilogSanitize(tostring(A)), A, A, 0, function(sinp) return sinp end, function() return CT.identity(A) end, "C.identity")
return identity
end)
C.cast = memoize(function(A,B)
err(types.isType(A),"cast: A should be type")
err(types.isType(B),"cast: B should be type")
err( R.isBasic(A), "cast: A should be basic type. casting "..tostring(A).." to "..tostring(B) )
err( R.isBasic(B), "cast: B should be basic type. casting "..tostring(A).." to "..tostring(B) )
assert(A:isTuple()==false)
local docast = RM.lift( J.sanitize("cast_"..tostring(A).."_"..tostring(B)), A, B, 0, function(sinp) return S.cast(sinp,B) end, function() return CT.cast(A,B) end, "C.cast" )
return docast
end)
C.tupleToArray = memoize(function(A,N)
err(types.isType(A),"tupleToArray: A should be type")
err(type(N)=="number","tupleToArray: N must be number")
local atup = types.tuple(J.broadcast(A,N))
local B = types.array2d(A,N)
local docast = RM.lift( J.sanitize("tupleToArray_"..tostring(A).."_"..tostring(N)), atup, B, 0,
function(sinp) return S.cast(sinp,B) end,
function() return CT.tupleToArray(A,N,atup,B) end, "C.tupleToArray")
return docast
end)
------------
-- return A*B as a darkroom FN. A,B are types
-- returns something of type outputType
C.multiply = memoize(function(A,B,outputType)
err( types.isType(A), "C.multiply: A must be type")
err( types.isType(B), "C.multiply: B must be type")
err( types.isType(outputType), "C.multiply: outputType must be type")
local partial = RM.lift( J.sanitize("mult_A"..tostring(A).."_B"..tostring(B).."_"..tostring(outputType)), types.tuple {A,B}, outputType, 1,
function(sinp) return S.cast(S.index(sinp,0),outputType)*S.cast(S.index(sinp,1),outputType) end,
function() return CT.multiply(A,B,outputType) end,
"C.multiply" )
return partial
end)
------------
-- return A+B as a darkroom FN. A,B are types
-- returns something of type outputType
C.sum = memoize(function( A, B, outputType, async )
err( types.isType(A), "C.sum: A must be type")
err( types.isType(B), "C.sum: B must be type")
err( types.isType(outputType), "C.sum: outputType must be type")
if async==nil then return C.sum(A,B,outputType,false) end
err(type(async)=="boolean","C.sum: async must be boolean")
local delay
if async then delay = 0 else delay = 1 end
local partial = RM.lift(
J.sanitize("sum_"..tostring(A)..tostring(B)..tostring(outputType).."_async_"..tostring(async)), types.tuple {A,B}, outputType, delay,
function(sinp)
local sout = S.cast(S.index(sinp,0),outputType)+S.cast(S.index(sinp,1),outputType)
if async then sout = sout:disablePipelining() end
return sout
end,
function()
return CT.sum(A,B,outputType,async)
end,
"C.sum")
return partial
end)
------------
-- return A-B as a darkroom FN. A,B are types
-- returns something of type outputType
C.sub = memoize(function( A, B, outputType, async )
err( types.isType(A), "C.sub: A must be type")
err( types.isType(B), "C.sub: B must be type")
err( types.isType(outputType), "C.sub: outputType must be type")
if async==nil then return C.sub(A,B,outputType,false) end
local delay
if async then delay = 0 else delay = 1 end
local partial = RM.lift( J.sanitize("sub_"..tostring(A)..tostring(B)..tostring(outputType).."_async_"..tostring(async)), types.tuple {A,B}, outputType, delay,
function(sinp)
local sout = S.cast(S.index(sinp,0),outputType)-S.cast(S.index(sinp,1),outputType)
if async then sout = sout:disablePipelining() end
return sout
end,nil,
"C.sub")
return partial
end)
-----------------------------
C.select = memoize(function(ty)
err(types.isType(ty), "C.select error: input must be type")
local ITYPE = types.tuple{types.bool(),ty,ty}
local selm = RM.lift( J.sanitize("C_select_"..tostring(ty)), ITYPE, ty, 1,
function(sinp) return S.select(S.index(sinp,0), S.index(sinp,1), S.index(sinp,2)) end,nil,
"C.select" )
return selm
end)
-----------------------------
C.eq = memoize(function(ty)
err(types.isType(ty), "C.eq error: input must be type")
local ITYPE = types.tuple{ty,ty}
local selm = RM.lift( J.sanitize("C_eq_"..tostring(ty)), ITYPE, types.bool(), 1,
function(sinp) return S.eq(S.index(sinp,0), S.index(sinp,1)) end, nil,
"C.eq" )
return selm
end)
-----------------------------
C.rcp = memoize(function(ty)
err(types.isType(ty), "C.rcp error: input must be type")
return f.parameter("finp",ty):rcp():toRigelModule("rcp_"..tostring(ty))
end)
-------------
-- {{idxType,vType},{idxType,vType}} -> {idxType,vType}
-- async: 0 cycle delay
C.argmin = memoize(function(idxType,vType, async, domax)
local ATYPE = types.tuple {idxType,vType}
local ITYPE = types.tuple{ATYPE,ATYPE}
local sinp = S.parameter( "inp", ITYPE )
local delay
if async==true then
delay = 0
else
delay = 2
end
local name = "argmin"
if domax then name="argmax" end
if async then name=name.."_async" end
name = name.."_"..J.verilogSanitize(tostring(idxType))
name = name.."_"..J.verilogSanitize(tostring(vType))
local partial = RM.lift( J.sanitize(name), ITYPE, ATYPE, delay,
function(sinp)
local a0 = S.index(sinp,0)
local a0v = S.index(a0,1)
local a1 = S.index(sinp,1)
local a1v = S.index(a1,1)
local out
if domax then
out = S.select(S.ge(a0v,a1v),a0,a1)
else
out = S.select(S.le(a0v,a1v),a0,a1)
end
if async==true then
out = out:disablePipelining()
end
return out
end,
function()
return CT.argmin(ITYPE,ATYPE,domax)
end,
"C.argmin" )
return partial
end)
------------
-- this returns a function from A[2]->A
-- return |A[0]-A[1]| as a darkroom FN.
-- The largest absolute difference possible is the max value of A - min value, so returning type A is always fine.
-- we fuse this with a cast to 'outputType' just for convenience.
C.absoluteDifference = memoize(function(A,outputType,X)
err(types.isType(A), "C.absoluteDifference: A must be type")
err(types.isType(outputType), "C.absoluteDifference: outputType must be type")
err(X==nil, "C.absoluteDifference: too many arguments")
local TY = types.array2d(A,2)
local internalType, internalType_uint
local internalType_terra
if A==types.uint(8) then
-- make sure this doesn't overflow when we add sign bit
internalType = types.int(9)
internalType_uint = types.uint(9)
internalType_terra = int16 -- should yield equivilant output
else
assert(false)
end
local partial = RM.lift( J.sanitize("absoluteDifference_"..tostring(A).."_"..tostring(outputType)), TY, outputType, 1,
function(sinp)
local subabs = S.abs(S.cast(S.index(sinp,0),internalType)-S.cast(S.index(sinp,1),internalType))
local out = S.cast(subabs, internalType_uint)
local out = S.cast(out, outputType)
return out
end,
function() return CT.absoluteDifference(A,outputType,internalType_terra) end,
"C.absoluteDifference")
return partial
end)
------------
-- returns a darkroom FN that casts type 'from' to type 'to'
-- performs [to](from >> shift)
C.shiftAndCast = memoize(function(from, to, shift)
err( types.isType(from), "C.shiftAndCast: from type must be type")
err( types.isType(to), "C.shiftAndCast: to type must be type")
err( type(shift)=="number", "C.shiftAndCast: shift must be number")
if shift >= 0 then
local touint8 = RM.lift( J.sanitize("shiftAndCast_uint" .. from.precision .. "to_uint" .. to.precision.."_shift"..tostring(shift)), from, to, 1,
function(touint8inp) return S.cast(S.rshift(touint8inp,S.constant(shift,from)), to) end,
function() return CT.shiftAndCast(from,to,shift) end,
"C.shiftAndCast")
return touint8
else
local touint8 = RM.lift( J.sanitize("shiftAndCast_uint" .. from.precision .. "to_uint" .. to.precision.."_shift"..tostring(shift)), from, to, 1,
function(touint8inp) return S.cast(S.lshift(touint8inp,S.constant(-shift,from)), to) end,
function() return CT.shiftAndCast(from,to,shift) end,
"C.shiftAndCast")
return touint8
end
end)
C.shiftAndCastSaturate = memoize(function(from, to, shift)
err( types.isType(from), "C.shiftAndCastSaturate: from type must be type")
err( types.isType(to), "C.shiftAndCastSaturate: to type must be type")
err( type(shift)=="number", "C.shiftAndCastSaturate: shift must be number")
local touint8 = RM.lift( J.sanitize("shiftAndCastSaturate_"..tostring(from).."_to_"..tostring(to).."_shift_"..tostring(shift)), from, to, 1,
function(touint8inp)
local OT = S.rshift(touint8inp,S.constant(shift,from))
return S.select(S.gt(OT,S.constant(255,from)),S.constant(255,types.uint(8)), S.cast(OT,to))
end,
function() return CT.shiftAndCastSaturate(from,to,shift) end,
"C.shiftAndCastSaturate")
return touint8
end)
-------------
-- returns a function of type {A[ConvWidth,ConvWidth], A_const[ConvWidth,ConvWidth]}
-- that convolves the two arrays
C.convolveTaps = memoize(function( A, ConvWidth, shift )
if shift==nil then shift=7 end
local TAP_TYPE = types.array2d( A, ConvWidth, ConvWidth )
local TAP_TYPE_CONST = TAP_TYPE:makeConst()
local INP_TYPE = types.tuple{types.array2d( A, ConvWidth, ConvWidth ),TAP_TYPE_CONST}
local inp = R.input( INP_TYPE )
local packed = R.apply( "packedtup", C.SoAtoAoS(ConvWidth,ConvWidth,{A,A:makeConst()}), inp )
local conv = R.apply( "partial", RM.map( C.multiply(A,A:makeConst(), types.uint(32)), ConvWidth, ConvWidth ), packed )
local conv = R.apply( "sum", RM.reduce( C.sum(types.uint(32),types.uint(32),types.uint(32)), ConvWidth, ConvWidth ), conv )
local conv = R.apply( "touint8", C.shiftAndCast(types.uint(32),A,shift), conv )
local convolve = RM.lambda( "convolveTaps", inp, conv )
return convolve
end)
------------
-- returns a function from A[ConvWidth,ConvHeight]->A
C.convolveConstant = memoize(function( A, ConvWidth, ConvHeight, tab, shift, X )
assert(type(ConvWidth)=="number")
assert(type(ConvHeight)=="number")
assert(type(tab)=="table")
assert(type(shift)=="number")
assert(X==nil)
local inp = R.input( types.array2d( A, ConvWidth, ConvHeight ) )
local r = R.constant( "convkernel", tab, types.array2d( A, ConvWidth, ConvHeight) )
local packed = R.apply( "packedtup", C.SoAtoAoS(ConvWidth,ConvHeight,{A,A}), R.concat("ptup", {inp,r}) )
local conv = R.apply( "partial", RM.map( C.multiply(A,A,types.uint(32)), ConvWidth, ConvHeight ), packed )
local conv = R.apply( "sum", RM.reduce( C.sum(types.uint(32),types.uint(32),types.uint(32)), ConvWidth, ConvHeight ), conv )
local conv = R.apply( "touint8", C.shiftAndCast( types.uint(32), A, shift ), conv )
local convolve = RM.lambda( "convolveConstant_W"..tostring(ConvWidth).."_H"..tostring(ConvHeight), inp, conv )
return convolve
end)
------------
-- returns a function from A[ConvWidth*T,ConvWidth]->A, with throughput T
C.convolveConstantTR = memoize(function( A, ConvWidth, ConvHeight, T, tab, shift, X )
assert(type(shift)=="number")
assert(type(T)=="number")
assert(T<=1)
assert(type(shift)=="number")
assert(X==nil)
local inp = R.input( types.array2d( A, ConvWidth*T, ConvHeight ) )
local r = R.apply( "convKernel", RM.constSeq( tab, A, ConvWidth, ConvHeight, T ) )
local packed = R.apply( "packedtup", C.SoAtoAoS(ConvWidth*T,ConvHeight,{A,A}), R.concat("ptup", {inp,r}) )
local conv = R.apply( "partial", RM.map( C.multiply(A,A,types.uint(32)), ConvWidth*T, ConvHeight ), packed )
local conv = R.apply( "sum", RM.reduce( C.sum(types.uint(32),types.uint(32),types.uint(32)), ConvWidth*T, ConvHeight ), conv )
local convseq = RM.lambda( "convseq_T"..tostring(1/T), inp, conv )
------------------
inp = R.input( R.V(types.array2d( A, ConvWidth*T, ConvHeight )) )
conv = R.apply( "convseqapply", RM.liftDecimate(RM.liftBasic(convseq)), inp)
conv = R.apply( "sumseq", RM.RPassthrough(RM.liftDecimate(RM.reduceSeq( C.sum(types.uint(32),types.uint(32),types.uint(32),true), T ))), conv )
conv = R.apply( "touint8", C.RVPassthrough(C.shiftAndCast( types.uint(32), A, shift )), conv )
conv = R.apply( "arrayop", C.RVPassthrough(C.arrayop( types.uint(8), 1, 1)), conv)
local convolve = RM.lambda( "convolve_tr_T"..tostring(1/T), inp, conv )
return convolve
end)
------------
-- returns a function from A[2][Width,Width]->reduceType
-- 'reduceType' is the precision we do the sum
C.SAD = memoize(function( A, reduceType, Width, X )
assert(X==nil)
local inp = R.input( types.array2d( types.array2d(A,2) , Width, Width ) )
local conv = R.apply( "partial", RM.map( C.absoluteDifference(A,reduceType), Width, Width ), inp )
local conv = R.apply( "sum", RM.reduce( C.sum(reduceType, reduceType, reduceType), Width, Width ), conv )
local convolve = RM.lambda( J.sanitize("SAD_"..tostring(A).."_reduceType"..tostring(reduceType).."_Width"..tostring(Width)), inp, conv, nil, "C.SAD" )
return convolve
end)
C.SADFixed = memoize(function( A, reduceType, Width, X )
local fixed = require "fixed"
assert(X==nil)
fixed.expectFixed(reduceType)
assert(fixed.extractSigned(reduceType)==false)
assert(fixed.extractExp(reduceType)==0)
local inp = R.input( types.array2d( types.array2d(A,2) , Width, Width ) )
-------
local ABS_inp = fixed.parameter("abs_inp", types.array2d(A,2))
local ABS_l, ABS_r = ABS_inp:index(0):lift(0):toSigned(), ABS_inp:index(1):lift(0):toSigned()
local ABS = (ABS_l-ABS_r):abs():pad(fixed.extractPrecision(reduceType),0)
------
local SUM_inp = fixed.parameter("sum_inp", types.tuple{reduceType,reduceType})
local SUM_l, SUM_r = SUM_inp:index(0), SUM_inp:index(1)
local SUM = (SUM_l+SUM_r)
SUM = SUM:truncate(fixed.extractPrecision(reduceType))
------
local conv = R.apply( "partial", RM.map( ABS:toRigelModule("absoluteDiff"), Width, Width ), inp )
local conv = R.apply( "sum", RM.reduce( SUM:toRigelModule("ABS_SUM"), Width, Width ), conv )
local convolve = RM.lambda( "SAD", inp, conv, nil, "C.SADFixed" )
return convolve
end)
C.SADFixed4 = memoize(function( A, reduceType, Width, X )
local fixed = require "fixed"
assert(X==nil)
fixed.expectFixed(reduceType)
assert(fixed.extractSigned(reduceType)==false)
assert(fixed.extractExp(reduceType)==0)
local inp = R.input( types.array2d( types.array2d(A,2) , Width, Width ) )
-------
local ABS_inp = fixed.parameter("abs_inp", types.array2d(A,2))
local ABSt = {}
for i=1,4 do
local ABS_l, ABS_r = ABS_inp:index(0):index(i-1):lift(0):toSigned(), ABS_inp:index(1):index(i-1):lift(0):toSigned()
ABSt[i] = (ABS_l-ABS_r):abs()
end
local ABS = (ABSt[1]+ABSt[2])+(ABSt[3]+ABSt[4])
ABS = ABS:pad(fixed.extractPrecision(reduceType),0)
------
local SUM_inp = fixed.parameter("sum_inp", types.tuple{reduceType,reduceType})
local SUM_l, SUM_r = SUM_inp:index(0), SUM_inp:index(1)
local SUM = (SUM_l+SUM_r)
SUM = SUM:truncate(fixed.extractPrecision(reduceType))
------
local conv = R.apply( "partial", RM.map( ABS:toRigelModule("absoluteDiff"), Width, Width ), inp )
local conv = R.apply( "sum", RM.reduce( SUM:toRigelModule("ABS_SUM"), Width, Width ), conv )
local convolve = RM.lambda( "SAD", inp, conv )
return convolve
end)
------------
-- takes a function f:A[StencilW,stencilH]->B
-- returns a function from A[T]->B[T]
C.stencilKernel = memoize(function( A, T, imageW, imageH, stencilW, stencilH, f)
local BASE_TYPE = types.array2d( A, T )
local inp = R.input( BASE_TYPE )
local convLB = R.apply( "convLB", C.stencilLinebuffer( A, imageW, imageH, T, -stencilW+1, 0, -stencilH+1, 0 ), inp)
local convstencils = R.apply( "convstencils", C.unpackStencil( A, stencilW, stencilH, T ), convLB )
local convpipe = R.apply( "conv", RM.map( f, T ), convstencils )
local convpipe = RM.lambda( "convpipe_"..f.name.."_W"..tostring(stencilW).."_H"..tostring(stencilH), inp, convpipe, nil,"C.stencilKernel" )
return convpipe
end)
------------
-- takes a function f:{A[StencilW,StencilH],tapType}->B
-- returns a function that goes from A[T]->B[T]. Applies f using a linebuffer
C.stencilKernelTaps = memoize(function( A, T, tapType, imageW, imageH, stencilW, stencilH, f )
assert(type(stencilW)=="number")
assert(type(stencilH)=="number")
local BASE_TYPE = types.array2d( A, T )
local ITYPE = types.tuple{BASE_TYPE, tapType}
local rawinp = R.input( ITYPE )
local inp = R.apply("idx0",C.index(ITYPE,0),rawinp)
local taps = R.apply("idx1",C.index(ITYPE,1),rawinp)
local convLB = R.apply( "convLB", C.stencilLinebuffer( A, imageW, imageH, T, -stencilW+1, 0, -stencilH+1, 0 ), inp)
local convstencils = R.apply( "convstencils", C.unpackStencil( A, stencilW, stencilH, T ) , convLB )
local st_tap_inp = R.apply( "broad", C.broadcast(tapType,T), taps )
st_tap_inp = R.concat("sttapinp",{convstencils,st_tap_inp})
local ST_TYPE = types.array2d( A, stencilW, stencilH )
st_tap_inp = R.apply("ST",C.SoAtoAoS(T,1,{ST_TYPE,tapType}),st_tap_inp)
local convpipe = R.apply( "conv", RM.map( f, T ), st_tap_inp )
local convpipe = RM.lambda( "convpipe", rawinp, convpipe )
return convpipe
end)
-------------
-- f should be a _lua_ function that takes two arguments, (internalW,internalH), and returns the
-- inner function based on this W,H. We have to do this for alignment reasons.
-- f should return a handshake function
-- timingFifo: include a fifo to improve timing. true by default
C.padcrop = memoize(function( A, W, H, T, L, Right, B, Top, borderValue, f, timingFifo, X )
err( type(W)=="number", "padcrop: W should be number")
err( type(H)=="number", "padcrop: H should be number")
err( type(T)=="number", "padcrop: T should be number")
err( type(L)=="number", "padcrop: L should be number")
err( type(Right)=="number", "padcrop: Right should be number")
err( type(B)=="number", "padcrop: B should be number")
err( type(Top)=="number", "padcrop: Top should be number")
err( type(f)=="function", "padcrop: f should be lua function")
err( X==nil, "padcrop: too many arguments" )
err( timingFifo==nil or type(timingFifo)=="boolean", "padcrop: timingFIFO must be nil or boolean" )
if timingFifo==nil then timingFifo=true end
local RW_TYPE = types.array2d( A, T ) -- simulate axi bus
local hsfninp = R.input( R.Handshake(RW_TYPE) )
local internalL = J.upToNearest(T,L)
local internalR = J.upToNearest(T,Right)
local fifos = {}
local statements = {}
local internalW, internalH = W+internalL+internalR,H+B+Top
local out = R.apply("pad", RM.liftHandshake(RM.padSeq(A, W, H, T, internalL, internalR, B, Top, borderValue)), hsfninp)
if timingFifo then
-- this FIFO is only for improving timing
table.insert( fifos, R.instantiateRegistered("f1",RM.fifo(types.array2d(A,T),128)) )
table.insert( statements, R.applyMethod("s3",fifos[#fifos],"store",out) )
out = R.applyMethod("l13",fifos[#fifos],"load")
end
-----------------
local internalFn = f(internalW, internalH)
local out = R.apply("HH",internalFn, out)
local padL = internalL-L
local padR = internalR-Right
local fnOutType = R.extractData(internalFn.outputType):arrayOver()
local out = R.apply("crop",RM.liftHandshake(RM.liftDecimate(C.cropHelperSeq(fnOutType, internalW, internalH, T, padL+Right+L, padR, B+Top, 0))), out)
if timingFifo then
-- this FIFO is only for improving timing
table.insert( fifos, R.instantiateRegistered("f2",RM.fifo(types.array2d(fnOutType,T),128)) )
table.insert( statements, R.applyMethod("s2",fifos[#fifos],"store",out) )
out = R.applyMethod("l2",fifos[#fifos],"load")
end
-----------------
table.insert(statements,1,out)
local name = J.sanitize("padcrop_"..tostring(A).."L"..tostring(L).."_R"..tostring(Right).."_B"..tostring(B).."_T"..tostring(Top).."_W"..tostring(W).."_H"..tostring(H)..internalFn.name)
local hsfn
if timingFifo then
hsfn = RM.lambda(name, hsfninp, R.statements(statements), fifos )
else
hsfn = RM.lambda(name, hsfninp, out)
end
return hsfn
end)
--------
function C.stencilKernelPadcrop(A,W,H,T,L,Right,B,Top,borderValue,f,timingFifo,X)
local function finternal(IW,IH)
return RM.makeHandshake(C.stencilKernel(A,T,IW,IH,Right+L+1,Top+B+1,f))
end
return C.padcrop(A,W,H,T,L,Right,B,Top,borderValue,finternal,timingFifo)
end
-- f should take (internalW,internalH) parameters
function C.stencilKernelPadcropUnpure(A,W,H,T,L,Right,B,Top,borderValue,f,timingFifo,X)
local function finternal(IW,IH)
return RM.makeHandshake(C.stencilKernel(A,T,IW,IH,Right+L+1,Top+B+1,f(IW,IH)))
end
return C.padcrop(A,W,H,T,L,Right,B,Top,borderValue,finternal,timingFifo)
end
-------------
local function invtable(bits)
local out = {}
--[=[ local terra inv(a:uint32)
if a==0 then
return 0
else
var o = ([math.pow(2,17)]/([uint32](255)+a))-[uint32](256)
if o>255 then return 255 end
return o
end
end]=]
local function round(x) if (x%1>=0.5) then return math.ceil(x) else return math.floor(x) end end
for i=0,math.pow(2,bits)-1 do
local v = (math.pow(2,17)/(256+i)) - 256
if v>255 then v = 255 end
v = round(v)
table.insert(out, v)
end
return out
end
function stripMSB(totalbits)
local ITYPE = types.uint(totalbits)
return RM.lift("stripMSB",ITYPE,types.uint(totalbits-1),0,
function(sinp) return S.cast(sinp,types.uint(totalbits-1)) end,
function() return CT.stripMSB(totalbits) end)
end
-- We want to calculate 1/x
--
-- we take the input x, and convert it to the floating point representation 1.ffffffff * 2^n
-- where f is the fractional component.
--
-- observe that (1/(1+fffff)) is between 0.5 and 1
--
-- in integer form, we have input 9 bit input in form (2^8+ffffffff) * 2^n
-- So we compute 2^9/((2^8+ffffffff)*2^n) = 2^(-n) * (2^9 / (2^8+ffffffff) )
--
-- put the (2^9 / (2^8+ffffffff) ) part in a lookup table. Since this is from 0.5 to 1,
-- we normalize to make good use of the bits in the LUT:
-- (2^17 / (2^8 + ffffffff)) - 256, which goes from 0 to 255
--
-- Plug this back in, and to find the real value, we have:
-- LUT(ffffffff) + 256, which has exponant n-17
function C.lutinvert(ty)
local fixed = require "fixed"
fixed.expectFixed(ty)
local signed = fixed.extractSigned(ty)
--------------------
local ainp = fixed.parameter("ainp",ty)
local a = ainp:hist("lutinvert_input")
local a_sign
if signed then
a_sign = a:sign()
a = a:abs()
end
local a_exp = a:msb(9)
local a_float, a_min, a_max = a:float(a_exp,9)
local aout
if signed then
aout = fixed.tuple({a_float,a_exp,a_sign})
else
aout = fixed.tuple({a_float,a_exp})
end
local afn = aout:toRigelModule("lutinvert_a")
--------------------
local lutbits = 8
------------
local binp
if signed then
binp = fixed.parameter("binp", types.tuple{types.uint(8),types.int(8),types.bool()} )
else
binp = fixed.parameter("binp", types.tuple{types.uint(8),types.int(8)} )
end
local b_inv = binp:index(0)
local b_exp = binp:index(1)
local b = (b_inv:cast(types.uint(9))+fixed.plainconstant(256,types.uint(9))):liftFloat(-a_max-17,-a_min-17+8, b_exp:neg()-fixed.plainconstant(17, types.int(8)) )
if signed then b = b:addSign(binp:index(2)) end
b = b:hist("lutinvert_output")
local bfn = b:toRigelModule("lutinvert_b")
---------------
local inp = R.input( ty )
local aout = R.apply( "a", afn, inp )
local aout_float = R.apply("aout_float", C.index(afn.outputType,0), aout)
local aout_exp = R.apply("aout_exp", C.index(afn.outputType,1), aout)
local aout_sign
if signed then aout_sign = R.apply("aout_sign", C.index(afn.outputType,2), aout) end
local aout_float_lsbs = R.apply("aout_float_lsbs", stripMSB(9), aout_float)
local inv = R.apply("inv", RM.lut(types.uint(lutbits), types.uint(8), invtable(lutbits)), aout_float_lsbs)
local out = R.apply( "b", bfn, R.concat("binp",{inv,aout_exp,aout_sign}) )
local fn = RM.lambda( "lutinvert", inp, out )
return fn, fn.outputType
end
-------------
C.stencilLinebufferPartialOffsetOverlap = memoize(function( A, w, h, T, xmin, xmax, ymin, ymax, offset, overlap )
J.map({T,w,h,xmin,xmax,ymin,ymax}, function(i) assert(type(i)=="number") end)
assert(T<=1); assert(w>0); assert(h>0);
assert(xmin<xmax)
assert(ymin<ymax)
assert(xmax==0)
assert(ymax==0)
local ST_W = -xmin+1
local ssr_region = ST_W - offset - overlap
local stride = ssr_region*T
assert(stride==math.floor(stride))
local LB = RM.makeHandshake(RM.linebuffer( A, w, h, 1, ymin ))
local SSR = RM.liftHandshake(RM.waitOnInput(RM.SSRPartial( A, T, xmin, ymin, stride, true )))
local inp = R.input( LB.inputType )
local out = R.apply("LB", LB, inp)
out = R.apply("SSR", SSR, out)
out = R.apply("slice", RM.makeHandshake(C.slice(types.array2d(A,ST_W,-ymin+1), 0, stride+overlap-1, 0,-ymin)), out)
return RM.lambda("stencilLinebufferPartialOverlap",inp,out)
end)
-------------
C.fifo = memoize(function(ty,size)
err( types.isType(ty), "C.fifo: type must be a type" )
err( R.isBasic(ty), "C.fifo: type must be basic type" )
err( type(size)=="number" and size>0, "C.fifo: size must be number > 0" )
local inp = R.input(R.Handshake(ty))
local regs = {R.instantiateRegistered("f1",RM.fifo(ty,size))}
return RM.lambda("C_FIFO_"..tostring(ty).."_size"..tostring(size), inp, R.statements{R.applyMethod("l1",regs[1],"load"),R.applyMethod("s1",regs[1],"store",inp)}, regs, "C.fifo", {size=size} )
end)
-------------
-- FIFO with loop support
function C.fifoLoop(fifos,statements,A,inp,size,name, csimOnly, X)
assert(type(name)=="string")
assert(X==nil)
table.insert( fifos, R.instantiateRegistered(name, RM.fifo(A,size,nil,nil,nil,nil,csimOnly)) )
table.insert( statements, R.applyMethod("s"..tostring(#fifos),fifos[#fifos],"store",inp) )
return R.applyMethod("l"..tostring(#fifos),fifos[#fifos],"load")
end
-------------
-- HACK: this should really be in this file, but it needs to be in the other file to satisfy dependencies in old code
C.compose = RM.compose
C.SoAtoAoS = RM.SoAtoAoS
-- takes {Handshake(a[W,H]), Handshake(b[W,H]),...} to Handshake( {a,b}[W,H] )
-- typelist should be a table of pure types
C.SoAtoAoSHandshake = memoize(function( W, H, typelist, X )
assert(X==nil)
local f = modules.SoAtoAoS(W,H,typelist)
f = modules.makeHandshake(f)
return C.compose( J.sanitize("SoAtoAoSHandshake_W"..tostring(W).."_H"..tostring(H).."_"..tostring(typelist)), f, modules.packTuple( J.map(typelist, function(t) return types.array2d(t,W,H) end) ) )
end)
-- Takes A[W,H] to A[W,H], but with a border around the edges determined by L,R,B,T
function C.border(A,W,H,L,R,B,T,value)
J.map({W,H,L,R,T,B,value},function(n) assert(type(n)=="number") end)
local res = {kind="border",generator="C.border",L=L,R=R,T=T,B=B,value=value}
res.inputType = types.array2d(A,W,H)
res.outputType = res.inputType
res.sdfInput, res.sdfOutput = {{1,1}},{{1,1}}
res.delay = 0
if terralib~=nil then res.terraModule = CT.border(res,A,W,H,L,R,B,T,value) end
return rigel.newFunction(res)
end
-- takes basic->basic to RV->RV
function C.RVPassthrough(f)
return modules.RPassthrough(modules.liftDecimate(modules.liftBasic(f)))
end
-- fully parallel up/down scale
-- if scaleX,Y > 1 then this is upsample
-- if scaleX,Y < 1 then this is downsample
C.scale = memoize(function( A, w, h, scaleX, scaleY )
assert(types.isType(A))
assert(type(w)=="number")
assert(type(h)=="number")
assert(type(scaleX)=="number")
assert(type(scaleY)=="number")
local res = { kind="scale", scaleX=scaleX, scaleY=scaleY}
res.inputType = types.array2d( A, w, h )
res.outputType = types.array2d( A, w*scaleX, h*scaleY )
res.delay = 0
if terralib~=nil then res.terraModule = CT.scale(res, A, w, h, scaleX, scaleY ) end
return rigel.newFunction(res)
end)
-- V -> RV
C.downsampleSeq = memoize(function( A, W, H, T, scaleX, scaleY, X )
err( types.isType(A), "C.downsampleSeq: A must be type")
err( type(W)=="number", "C.downsampleSeq: W must be number")
err( type(H)=="number", "C.downsampleSeq: H must be number")
err( type(T)=="number", "C.downsampleSeq: T must be number")
err( type(scaleX)=="number", "C.downsampleSeq: scaleX must be number")
err( type(scaleY)=="number", "C.downsampleSeq: scaleY must be number")
err( scaleX>=1, "C.downsampleSeq: scaleX must be >=1")
err( scaleY>=1, "C.downsampleSeq: scaleY must be >=1")
err( X==nil, "C.downsampleSeq: too many arguments" )
if scaleX==1 and scaleY==1 then
return C.identity(A)
end
local inp = rigel.input( rigel.V(types.array2d(A,T)) )
local out = inp
if scaleY>1 then
out = rigel.apply("downsampleSeq_Y", modules.liftDecimate(modules.downsampleYSeq( A, W, H, T, scaleY )), out)
end
if scaleX>1 then
local mod = modules.liftDecimate(modules.downsampleXSeq( A, W, H, T, scaleX ))
if scaleY>1 then mod=modules.RPassthrough(mod) end
out = rigel.apply("downsampleSeq_X", mod, out)
local downsampleT = math.max(T/scaleX,1)
if downsampleT<T then
-- technically, we shouldn't do this without lifting to a handshake - but we know this can never stall, so it's ok
out = rigel.apply("downsampleSeq_incrate", modules.RPassthrough(modules.changeRate(A,1,downsampleT,T)), out )
elseif downsampleT>T then assert(false) end
end
return modules.lambda( J.sanitize("downsampleSeq_"..tostring(A).."_W"..tostring(W).."_H"..tostring(H).."_T"..tostring(T).."_scaleX"..tostring(scaleX).."_scaleY"..tostring(scaleY)), inp, out,nil,"C.downsampleSeq")
end)
-- this is always Handshake
-- always has type A[T]->A[T]
C.upsampleSeq = memoize(function( A, W, H, T, scaleX, scaleY, X )
err( types.isType(A), "C.upsampleSeq: A must be type")
err( type(W)=="number", "C.upsampleSeq: W must be number")
err( type(H)=="number", "C.upsampleSeq: H must be number")
err( type(T)=="number", "C.upsampleSeq: T must be number")
err( type(scaleX)=="number", "C.upsampleSeq: scaleX must be number")
err( type(scaleY)=="number", "C.upsampleSeq: scaleY must be number")
err( scaleX>=1, "C.upsampleSeq: scaleX must be >=1")
err( scaleY>=1, "C.upsampleSeq: scaleY must be >=1")
err( X==nil, "C.upsampleSeq: too many arguments" )
if scaleX==1 and scaleY==1 then
return C.identity(types.array2d(A,T))
end
local inner
if scaleY>1 and scaleX==1 then
inner = modules.liftHandshake(modules.upsampleYSeq( A, W, H, T, scaleY ))
elseif scaleX>1 and scaleY==1 then
inner = modules.upsampleXSeq( A, T, scaleX )
else
local f = modules.upsampleXSeq( A, T, scaleX )
inner = C.compose( J.sanitize("upsampleSeq_"..tostring(A).."_W"..tostring(W).."_H"..tostring(H).."_T"..tostring(T).."_scaleX"..tostring(scaleX).."_scaleY"..tostring(scaleY)), f, modules.liftHandshake(modules.upsampleYSeq( A, W, H, T, scaleY )),nil,"C.upsampleSeq")
end
return inner
end)
-- takes A to A[T] by duplicating the input
C.broadcast = memoize(function(A,W,H)
err( types.isType(A), "C.broadcast: A must be type A")
rigel.expectBasic(A)
err( type(W)=="number", "broadcast: W should be number")
if H==nil then return C.broadcast(A,W,1) end
err( type(H)=="number", "broadcast: H should be number")
local OT = types.array2d(A, W, H)
return modules.lift( J.sanitize("Broadcast_"..tostring(A).."_W"..tostring(W).."_H"..tostring(H)),A,OT,0,
function(sinp) return S.cast(S.tuple(J.broadcast(sinp,W*H)),OT) end,
function() return CT.broadcast(A,W,H,OT) end,
"C.broadcast")
end)
C.arrayop=C.broadcast
-- extractStencils : A[w,h] -> A[(xmax-xmin+1)*(ymax-ymin+1)][w,h]
-- min, max ranges are inclusive
C.stencil = memoize(function( A, w, h, xmin, xmax, ymin, ymax )
assert( type(xmin)=="number" )
assert( type(xmax)=="number" )
assert( xmax>=xmin )
assert( type(ymin)=="number" )
assert( type(ymax)=="number" )
assert( ymax>=ymin )
rigel.expectBasic(A)
if A:isArray() then error("Input to extract stencils must not be array") end
local res = {kind="stencil", type=A, w=w, h=h, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, generator="C.stencil" }
res.delay=0
res.inputType = types.array2d(A,w,h)
res.outputType = types.array2d(types.array2d(A,xmax-xmin+1,ymax-ymin+1),w,h)
res.sdfInput, res.sdfOutput = {{1,1}},{{1,1}}
res.name = J.sanitize("Stencil_"..tostring(A).."_w"..tostring(w).."_h"..tostring(h).."_xmin"..tostring(xmin).."_xmax"..tostring(xmax).."_ymin"..tostring(ymin).."_ymax"..tostring(ymax))
res.stateful=false
if terralib~=nil then res.terraModule = CT.stencil(res, A, w, h, xmin, xmax, ymin, ymax ) end
return rigel.newFunction(res)
end)
-- this applies a border around the image. Takes A[W,H] to A[W,H], but with a border. Sequentialized to throughput T.
C.borderSeq = memoize(function( A, W, H, T, L, R, B, Top, Value, X )
err( types.isType(A), "borderSeq: type must be type")
J.map({W=W or "W",H = H or "H",T = T or "T",L = L or "L",R=R or "R",B = B or "B",Top = Top or "Top",Value = Value or "Value"},function(n,k) err(type(n)=="number","borderSeq: "..k.." must be number") end)
err( X==nil, "borderSeq: too many arguments")
local inpType = types.tuple{types.tuple{types.uint(16),types.uint(16)},A}
local modname = "BorderSeq_"..J.verilogSanitize(tostring(A)).."_W"..tostring(W).."_H"..tostring(H).."_T"..tostring(T).."_L"..tostring(L).."_R"..tostring(R).."_B"..tostring(B).."_Top"..tostring(Top).."_Value"..tostring(Value)
local f = modules.lift( modname, inpType, A, 0,
function(inp)
local inpx, inpy = S.index(S.index(inp,0),0), S.index(S.index(inp,0),1)
local lcheck = S.constant(false,types.bool())
if L~=0 then lcheck = S.lt(inpx,S.constant(L,types.uint(16))) end -- verilator lint workaround
local horizontal = S.__or(lcheck,S.ge(inpx,S.constant(W-R,types.uint(16))))
local bcheck = S.constant(false,types.bool())
if B~=0 then bcheck = S.lt(inpy,S.constant(B,types.uint(16))) end -- verilator lint workaround
local vert = S.__or(bcheck,S.ge(inpy,S.constant(H-Top,types.uint(16))))
local outside = S.__or(horizontal,vert)
return S.select(outside,S.constant(Value,A), S.index(inp,1) )
end,
function() return CT.borderSeq( A, W, H, T, L, R, B, Top, Value, inpType ) end)
return modules.liftXYSeqPointwise( modname, "C.borderSeq", f, W, H, T )
end)
-- This is the same as CropSeq, but lets you have L,R not be T-aligned
-- All it does is throws in a shift register to alter the horizontal phase
C.cropHelperSeq = memoize(function( A, W, H, T, L, R, B, Top, X )
err(X==nil, "cropHelperSeq, too many arguments")
err(type(T)=="number","T must be number")
if L%T==0 and R%T==0 then return modules.cropSeq( A, W, H, T, L, R, B, Top ) end
err( (W-L-R)%T==0, "cropSeqHelper, (W-L-R)%T~=0")
local RResidual = R%T
local inp = rigel.input( types.array2d( A, T ) )
local out = rigel.apply( "SSR", modules.SSR( A, T, -RResidual, 0 ), inp)
out = rigel.apply( "slice", C.slice( types.array2d(A,T+RResidual), 0, T-1, 0, 0), out)
out = rigel.apply( "crop", modules.cropSeq(A,W,H,T,L+RResidual,R-RResidual,B,Top), out )
return modules.lambda( J.sanitize("cropHelperSeq_"..tostring(A).."_W"..W.."_H"..H.."_T"..T.."_L"..L.."_R"..R.."_B"..B.."_Top"..Top), inp, out )
end)
C.stencilLinebuffer = memoize(function( A, w, h, T, xmin, xmax, ymin, ymax )
err(types.isType(A), "stencilLinebuffer: A must be type")
err(type(T)=="number","stencilLinebuffer: T must be number")
err(type(w)=="number","stencilLinebuffer: w must be number")
err(type(h)=="number","stencilLinebuffer: h must be number")
err(type(xmin)=="number","stencilLinebuffer: xmin must be number")
err(type(xmax)=="number","stencilLinebuffer: xmax must be number")
err(type(ymin)=="number","stencilLinebuffer: ymin must be number")
err(type(ymax)=="number","stencilLinebuffer: ymax must be number")
err(T>=1, "stencilLinebuffer: T must be >=1");
err(w>0,"stencilLinebuffer: w must be >0");
err(h>0,"stencilLinebuffer: h must be >0");
err(xmin<=xmax,"stencilLinebuffer: xmin>xmax")
err(ymin<=ymax,"stencilLinebuffer: ymin>ymax")
err(xmax==0,"stencilLinebuffer: xmax must be 0")
err(ymax==0,"stencilLinebuffer: ymax must be 0")
return C.compose( J.sanitize("stencilLinebuffer_A"..tostring(A).."_w"..w.."_h"..h.."_T"..T.."_xmin"..tostring(math.abs(xmin)).."_ymin"..tostring(math.abs(ymin))), modules.SSR( A, T, xmin, ymin), modules.linebuffer( A, w, h, T, ymin ), "C.stencilLinebuffer" )
end)
C.stencilLinebufferPartial = memoize(function( A, w, h, T, xmin, xmax, ymin, ymax )
J.map({T,w,h,xmin,xmax,ymin,ymax}, function(i) assert(type(i)=="number") end)
assert(T<=1); assert(w>0); assert(h>0);
assert(xmin<xmax)
assert(ymin<ymax)
assert(xmax==0)
assert(ymax==0)
-- SSRPartial need to be able to stall the linebuffer, so we must do this with handshake interfaces. Systolic pipelines can't stall each other
return C.compose( J.sanitize("stencilLinebufferPartial_A"..tostring(A).."_W"..tostring(w).."_H"..tostring(h)), modules.liftHandshake(modules.waitOnInput(modules.SSRPartial( A, T, xmin, ymin ))), modules.makeHandshake(modules.linebuffer( A, w, h, 1, ymin )), "C.stencilLinebufferPartial" )
end)
-- purely wiring. This should really be implemented as a lift.
C.unpackStencil = memoize(function( A, stencilW, stencilH, T, arrHeight, X )
assert(types.isType(A))
assert(type(stencilW)=="number")
assert(stencilW>0)
assert(type(stencilH)=="number")
assert(stencilH>0)
assert(type(T)=="number")
assert(T>=1)
err(arrHeight==nil, "Error: NYI - unpackStencil on non-height-1 arrays")
assert(X==nil)
local res = {kind="unpackStencil", stencilW=stencilW, stencilH=stencilH,T=T,generator="C.unpackStencil"}
res.inputType = types.array2d( A, stencilW+T-1, stencilH)
res.outputType = types.array2d( types.array2d( A, stencilW, stencilH), T )
res.sdfInput, res.sdfOutput = {{1,1}}, {{1,1}}
res.stateful = false
res.delay=0
res.name = J.sanitize("unpackStencil_"..tostring(A).."_W"..tostring(stencilW).."_H"..tostring(stencilH).."_T"..tostring(T))
if terralib~=nil then res.terraModule = CT.unpackStencil(res, A, stencilW, stencilH, T, arrHeight) end
res.systolicModule = Ssugar.moduleConstructor(res.name)
local sinp = S.parameter("inp", res.inputType)
local out = {}
for i=1,T do
out[i] = {}
for y=0,stencilH-1 do
for x=0,stencilW-1 do
out[i][y*stencilW+x+1] = S.index( sinp, x+i-1, y )
end
end
end
res.systolicModule:addFunction( S.lambda("process", sinp, S.cast( S.tuple(J.map(out,function(n) return S.cast( S.tuple(n), types.array2d(A,stencilW,stencilH) ) end)), res.outputType ), "process_output", nil, nil, S.CE("process_CE") ) )
--res.systolicModule:addFunction( S.lambda("reset", S.parameter("r",types.null()), nil, "ro" ) )
return rigel.newFunction(res)
end)
-- if index==true, then we return a value, not an array
C.slice = memoize(function( inputType, idxLow, idxHigh, idyLow, idyHigh, index, X )
err( types.isType(inputType),"slice first argument must be type" )
err( type(idxLow)=="number", "slice idxLow must be number")
err( type(idxHigh)=="number", "slice idxHigh must be number")
err( index==nil or type(index)=="boolean", "index must be bool")
err( X==nil, "C.slice: too many arguments")
if inputType:isTuple() then
assert( idxLow < #inputType.list )
assert( idxHigh < #inputType.list )
assert( idxLow == idxHigh ) -- NYI
assert( index )
local OT = inputType.list[idxLow+1]
return modules.lift( J.sanitize("index_"..tostring(inputType).."_"..idxLow), inputType, OT, 0,
function(systolicInput) return S.index( systolicInput, idxLow ) end,