Changeset a5e2682


Ignore:
Timestamp:
Oct 21, 2022, 12:13:25 PM (4 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
9e042d8
Parents:
8bd886e
Message:

Improve new-array subscripting to cover missing cases.

Missing cases include acknowledging a[0] and a[1] as required uses; therefore, give them
special overloads. Add comments to explain why.

Missing cases include test coverage of these overloads (where tests actually fail when an overload
is missing). Perhaps surprisingly, you need to use a lot of features at the same time for the need
for several overloads to obtain; make test coverage go there.

Also, switch thesis's demo of mutidimensional transposing/slicing to use (improved) libcfa ar
(pseudo-)trait, in place of former ix trait. Therefore, also port this demo to new array
syntax. (No changes to thesis discussion around this demo yet; these are still pending.) All
in, cause this thesis demo to be runnable again.

Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/programs/hello-md.cfa

    r8bd886e ra5e2682  
    11#include "array.hfa"
    2 
    3 
    4 trait ix( C &, E &, ztype(N) ) {
    5     E & ?[?]( C &, ptrdiff_t );
    6     void __taglen( tag(C), tag(N) );
    7 };
    8 
    9 forall( ztype(Zn), ztype(S), Timmed &, Tbase & )
    10 void __taglen( tag(arpk(Zn, S, Timmed, Tbase)), tag(Zn) ) {}
    112
    123
     
    3829
    3930
    40 forall( ztype( N ) )
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40forall( [N] )
    4141void print1d_cstyle( array(float, N) & c );
    4242
    43 forall( C &, ztype( N ) | ix( C, float, N ) )
     43forall( [N], C & | ar( C, float, N ) )
    4444void print1d( C & c );
    4545
     
    5858
    5959
    60 forall( ztype( N ) )
     60forall( [N] )
    6161void print1d_cstyle( array(float, N) & c ) {
    62     for( i; z(N) ) {
     62    for( i; N ) {
    6363        printf("%.1f  ", c[i]);
    6464    }
     
    7878
    7979
    80 forall( C &, ztype( N ) | ix( C, float, N ) )
     80forall( [N], C & | ar( C, float, N ) )
    8181void print1d( C & c ) {
    82     for( i; z(N) ) {
     82    for( i; N ) {
    8383        printf("%.1f  ", c[i]);
    8484    }
     
    9999
    100100
    101 void fill( array(float, Z(5), Z(7)) & a ) {
     101void fill( array(float, 5, 7) & a ) {
    102102    for ( i; (ptrdiff_t) 5 ) {
    103103        for ( j; 7 ) {
    104             a[[i,j]] = 1.0 * i + 0.1 * j;
    105             printf("%.1f  ", a[[i,j]]);
     104            a[i,j] = 1.0 * i + 0.1 * j;
     105            printf("%.1f  ", a[i,j]);
    106106        }
    107107        printf("\n");
     
    118118
    119119
    120 array( float, Z(5), Z(7) ) a;
     120array( float, 5, 7 ) a;
    121121fill(a);
    122122/*
     
    148148
    149149
    150 print1d( a[[ 2, all ]] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6
    151 print1d( a[[ all, 3 ]] );  // 0.3  1.3  2.3  3.3  4.3
     150print1d( a[ 2, all ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6
     151print1d( a[ all, 3 ] );  // 0.3  1.3  2.3  3.3  4.3
    152152
    153153
    154154
    155 print1d_cstyle( a[[ 2, all ]] );
     155print1d_cstyle( a[ 2, all ] );
    156156
    157157
     
    161161
    162162
    163 #ifdef SHOWERR1
     163#ifdef SHOW_ERROR_1
    164164
    165 print1d_cstyle( a[[ all, 2 ]] );  // bad
     165print1d_cstyle( a[ all, 2 ] );  // bad
    166166
    167167#endif
  • libcfa/src/containers/array.hfa

    r8bd886e ra5e2682  
     1#pragma once
     2
    13#include <assert.h>
    24
     
    1820    // About the choice of integral types offered as subscript overloads:
    1921    // Intent is to cover these use cases:
     22    //    a[0]                                                // i : zero_t
     23    //    a[1]                                                // i : one_t
     24    //    a[2]                                                // i : int
    2025    //    float foo( ptrdiff_t i ) { return a[i]; }           // i : ptrdiff_t
     26    //    float foo( size_t i ) { return a[i]; }              // i : size_t
    2127    //    forall( [N] ) ... for( i; N ) { total += a[i]; }    // i : typeof( sizeof(42) )
    2228    //    for( i; 5 ) { total += a[i]; }                      // i : int
     29    //
    2330    // It gets complicated by:
    2431    // -  CFA does overloading on concrete types, like int and unsigned int, not on typedefed
     
    2835    //    should give them type size_t.
    2936    //
    30     //                          gcc -m32         cfa -m32 given bug         gcc -m64
     37    //                          gcc -m32         cfa -m32 given bug         gcc -m64 (and cfa)
    3138    // ptrdiff_t                int              int                        long int
    3239    // size_t                   unsigned int     unsigned int               unsigned long int
    3340    // typeof( sizeof(42) )     unsigned int     unsigned long int          unsigned long int
    3441    // int                      int              int                        int
     42    //
     43    // So the solution must support types {zero_t, one_t, int, unsigned int, long int, unsigned long int}
     44    //
     45    // The solution cannot rely on implicit conversions (e.g. just have one overload for ptrdiff_t)
     46    // because assertion satisfaction requires types to match exacly.  Both higher-dimensional
     47    // subscripting and operations on slices use asserted subscript operators.  The test case
     48    // array-container/array-sbscr-cases covers the combinations.  Mike beleives that commenting out
     49    // any of the current overloads leads to one of those cases failing, either on 64- or 32-bit.
     50    // Mike is open to being shown a smaller set of overloads that still passes the test.
     51
     52    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, zero_t ) {
     53        assert( 0 < N );
     54        return (Timmed &) a.strides[0];
     55    }
     56
     57    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, one_t ) {
     58        assert( 1 < N );
     59        return (Timmed &) a.strides[1];
     60    }
    3561
    3662    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, int i ) {
     
    77103        return N;
    78104    }
     105
     106    static inline void __taglen( tag(arpk(N, S, Timmed, Tbase)), tag(N) ) {}
    79107
    80108    // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
     
    156184#endif
    157185
     186// Available for users to work around Trac #265
     187// If `a[...0...]` isn't working, try `a[...ix0...]` instead.
     188
     189#define ix0 ((ptrdiff_t)0)
     190
     191
     192
    158193//
    159194// Rotation
     
    185220//
    186221
    187 trait ar(A &, Tv &) {
    188     Tv& ?[?]( A&, ptrdiff_t );
    189     size_t ?`len( A& );
    190 };
     222// desired:
     223// trait ar(A &, Tv &, [N]) {
     224//     Tv& ?[?]( A&, zero_t );
     225//     Tv& ?[?]( A&, one_t  );
     226//     Tv& ?[?]( A&, int    );
     227//                   ...
     228//     size_t ?`len( A& );
     229//     void __taglen( tag(C), tag(N) );
     230// };
     231
     232// working around N's not being accepted as arguments to traits
     233
     234#define ar(A, Tv, N) {                 \
     235    Tv& ?[?]( A&, zero_t );            \
     236    Tv& ?[?]( A&, one_t );             \
     237    Tv& ?[?]( A&, int );               \
     238    Tv& ?[?]( A&, unsigned int );      \
     239    Tv& ?[?]( A&, long int );          \
     240    Tv& ?[?]( A&, unsigned long int ); \
     241    size_t ?`len( A& );                \
     242    void __taglen( tag(A), tag(N) );   \
     243}
  • tests/array-container/array-basic.cfa

    r8bd886e ra5e2682  
    7878}
    7979
    80 forall( A & | ar(A, float) )
     80forall( [N], A & | ar(A, float, N) )
    8181float total1d_hi( A & a ) {
    8282    float total = 0.0f;
    83     for (i; a`len)
     83    for (i; N)
    8484        total += a[i];
    8585    return total;
Note: See TracChangeset for help on using the changeset viewer.