Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/containers/array.hfa

    rc7625e0 r63f42a8  
    55
    66// 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)
     7#define z(N) sizeof(N)
    118
    129forall( T & ) struct tag {};
     
    1916//
    2017
    21 forall( ztype(Zn), ztype(S), Timmed &, Tbase & ) {
     18forall( [N], S & | sized(S), Timmed &, Tbase & ) {
    2219    struct arpk {
    23         S strides[z(Zn)];
     20        S strides[z(N)];
    2421    };
    2522
    26     Timmed & ?[?]( arpk(Zn, S, Timmed, Tbase) & a, ptrdiff_t i ) {
     23    // About the choice of integral types offered as subscript overloads:
     24    // Intent is to cover these use cases:
     25    //    float foo( ptrdiff_t i ) { return a[i]; }           // i : ptrdiff_t
     26    //    forall( [N] ) ... for( i; N ) { total += a[i]; }    // i : typeof( sizeof(42) )
     27    //    for( i; 5 ) { total += a[i]; }                      // i : int
     28    // It gets complicated by:
     29    // -  CFA does overloading on concrete types, like int and unsigned int, not on typedefed
     30    //    types like size_t.  So trying to overload on ptrdiff_t vs int works in 64-bit mode
     31    //    but not in 32-bit mode.
     32    // -  Given bug of Trac #247, CFA gives sizeof expressions type unsigned long int, when it
     33    //    should give them type size_t.
     34    //   
     35    //                          gcc -m32         cfa -m32 given bug         gcc -m64
     36    // ptrdiff_t                int              int                        long int
     37    // size_t                   unsigned int     unsigned int               unsigned long int
     38    // typeof( sizeof(42) )     unsigned int     unsigned long int          unsigned long int
     39    // int                      int              int                        int
     40
     41    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, int i ) {
    2742        return (Timmed &) a.strides[i];
    2843    }
    2944
    30     size_t ?`len( arpk(Zn, S, Timmed, Tbase) & a ) {
    31         return z(Zn);
     45    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) {
     46        return (Timmed &) a.strides[i];
     47    }
     48
     49    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) {
     50        return (Timmed &) a.strides[i];
     51    }
     52
     53    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) {
     54        return (Timmed &) a.strides[i];
     55    }
     56
     57    static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) {
     58        return z(N);
    3259    }
    3360
    3461    // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
    35     void ?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
    36         void ?{}( S (&inner)[z(Zn)] ) {}
     62    static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) {
     63        void ?{}( S (&inner)[z(N)] ) {}
    3764        ?{}(this.strides);
    3865    }
    39     void ^?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
    40         void ^?{}( S (&inner)[z(Zn)] ) {}
     66    static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) {
     67        void ^?{}( S (&inner)[z(N)] ) {}
    4168        ^?{}(this.strides);
    4269    }
     
    4875
    4976forall( Te )
    50 Te mkar_( tag(Te) ) {}
     77static inline Te mkar_( tag(Te) ) {}
    5178
    52 forall( ztype(Zn), ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
    53 arpk(Zn, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(Zn), ZTags ) {}
     79forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
     80static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
    5481
    5582// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
     
    80107// Core -[[-,-,-]] operator
    81108
     109#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
     110
    82111// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
    83 // forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
    84 // TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
    85 //     return this[ab][bc];
    86 // }
     112
     113forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
     114static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
     115    return this[ab][bc];
     116}
     117
     118#else
    87119
    88120// Workaround form.  Listing all possibilities up to 4 dims.
    89 forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
    90             , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); } )
    91 TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
     121
     122forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
     123static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
    92124    return this[ab][bc];
    93125}
    94 forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
    95             , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); }
    96             , TD &, IxCD | { TD & ?[?]( TC &, IxCD ); } )
    97 TD & ?[?]( TA & this, IxAB ab, IxBC bc, IxCD cd ) {
    98     return this[ab][bc][cd];
    99 }
    100 forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
    101             , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); }
    102             , TD &, IxCD | { TD & ?[?]( TC &, IxCD ); }
    103             , TE &, IxDE | { TE & ?[?]( TD &, IxDE ); } )
    104 TE & ?[?]( TA & this, IxAB ab, IxBC bc, IxCD cd, IxDE de ) {
    105     return this[ab][bc][cd][de];
     126
     127forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
     128static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
     129    return this[[ab0,ab1]][bc];
    106130}
    107131
    108 // Adapters for "indexed by ptrdiff_t" implies "indexed by [this other integral type]"
    109 // Work around restriction that assertions underlying -[[-,-,-]] must match excatly
    110 forall( C &, E & | { E & ?[?]( C &, ptrdiff_t ); } ) {
     132forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
     133static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
     134    return this[[ab0,ab1,ab2]][bc];
     135}
    111136
    112     // Targeted to support:  for( i; z(N) ) ... a[[ ..., i, ... ]]
    113     E & ?[?]( C & this, size_t i ) {
    114         return this[ (ptrdiff_t) i ];
    115     }
    116 
    117     // Targeted to support:  for( i; 5 ) ... a[[ ..., i, ... ]]
    118     E & ?[?]( C & this, int i ) {
    119         return this[ (ptrdiff_t) i ];
    120     }
    121 }
     137#endif
    122138
    123139//
     
    126142
    127143// Base
    128 forall( ztype(Zq), ztype(Sq), Tbase & )
    129 tag(arpk(Zq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(Tbase) ) {}
     144forall( [Nq], Sq & | sized(Sq), Tbase & )
     145static inline tag(arpk(Nq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(Tbase) ) {}
    130146
    131147// Rec
    132 forall( ztype(Zq), ztype(Sq), ztype(Z), ztype(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(recq) ); } )
    133 tag(arpk(Z, S, recr, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(arpk(Z, S, recq, Tbase)) ) {}
     148forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
     149static inline tag(arpk(N, S, recr, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(arpk(N, S, recq, Tbase)) ) {}
    134150
    135151// Wrapper
    136152struct all_t {} all;
    137 forall( ztype(Z), ztype(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(Z), tag(S), tag(Te) ); } )
    138 result & ?[?]( arpk(Z, S, Te, Tbase) & this, all_t ) {
     153forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
     154static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) {
    139155    return (result&) this;
    140156}
Note: See TracChangeset for help on using the changeset viewer.