source: doc/theses/mike_brooks_MMath/content/array/mdim/array.hfa @ 18f7858

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 18f7858 was 8e819a9, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Mike MMath initial

  • Property mode set to 100644
File size: 3.9 KB
Line 
1
2
3// a type whose size is n
4#define Z(n) char[n]
5
6// the inverse of Z(-)
7#define z(Zn) sizeof(Zn)
8
9// if you're expecting a Z(n), say so, by asking for a ztype, instead of dtype or otype
10#define ztype(Zn) Zn & | sized(Zn)
11
12forall( T & ) struct tag {};
13#define ttag(T) ((tag(T)){})
14#define ztag(n) ttag(Z(n))
15
16
17//
18// Single-dim array sruct (with explicit packing and atom)
19//
20
21forall( ztype(Zn), ztype(S), Timmed &, Tbase & ) {
22    struct arpk {
23        S strides[z(Zn)];
24    };
25
26    Timmed & ?[?]( arpk(Zn, S, Timmed, Tbase) & a, ptrdiff_t i ) {
27        return (Timmed &) a.strides[i];
28    }
29
30    // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
31    void ?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
32        void ?{}( S (&inner)[z(Zn)] ) {}
33        ?{}(this.strides);
34    }
35    void ^?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
36        void ^?{}( S (&inner)[z(Zn)] ) {}
37        ^?{}(this.strides);
38    }
39}
40
41//
42// Sugar for declaring array structure instances
43//
44
45forall( Te )
46Te mkar_( tag(Te) ) {}
47
48forall( ztype(Zn), ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
49arpk(Zn, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(Zn), ZTags ) {}
50
51// stolen from https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
52
53    // Make a FOREACH macro
54    #define FE_0(WHAT)
55    #define FE_1(WHAT, X) WHAT(X)
56    #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
57    #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
58    #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
59    #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
60    //... repeat as needed
61
62    #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
63    #define FOR_EACH(action,...) \
64    GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
65
66#define COMMA_ttag(X) , ttag(X)
67#define array( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
68
69#define COMMA_ztag(X) , ztag(X)
70#define zarray( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
71
72//
73// Sugar for multidimensional indexing
74//
75
76// Core -[[-,-,-]] operator
77
78// Desired form, one definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
79// forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
80// TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
81//     return this[ab][bc];
82// }
83
84// Workaround form.  Listing all possibilities up to 4 dims.
85forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
86            , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); } )
87TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
88    return this[ab][bc];
89}
90// forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
91// TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
92//     return this[ab][bc];
93// }
94
95// Adapters for "indexed by ptrdiff_t" implies "indexed by [this other integral type]"
96// Work around restriction that assertions underlying -[[-,-,-]] must match excatly
97forall( C &, E & | { E & ?[?]( C &, ptrdiff_t ); } ) {
98
99    // Targeted to support:  for( i; z(N) ) ... a[[ ..., i, ... ]]
100    E & ?[?]( C & this, size_t i ) {
101        return this[ (ptrdiff_t) i ];
102    }
103
104    // Targeted to support:  for( i; 5 ) ... a[[ ..., i, ... ]]
105    E & ?[?]( C & this, int i ) {
106        return this[ (ptrdiff_t) i ];
107    }
108}
109
110//
111// Rotation
112//
113
114// Base
115forall( ztype(Zq), ztype(Sq), Tbase & )
116tag(arpk(Zq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(Tbase) ) {}
117
118// Rec
119forall( ztype(Zq), ztype(Sq), ztype(Z), ztype(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(recq) ); } )
120tag(arpk(Z, S, recr, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(arpk(Z, S, recq, Tbase)) ) {}
121
122// Wrapper
123struct all_t {} all;
124forall( ztype(Z), ztype(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(Z), tag(S), tag(Te) ); } )
125result & ?[?]( arpk(Z, S, Te, Tbase) & this, all_t ) {
126    return (result&) this;
127}
Note: See TracBrowser for help on using the repository browser.