• Here the code:

    // cTrial9.js
    //
    var c=`
    // c trials... to get some speed in odd memory manipulation,
    // expecially for graphics buffers to move things around...
    // such as running text... (odd zig-zag... and pull out)
    // On PICO w/ dev=false profiled on 20 cols x 7 rows w/
    // col counts: ccc = 1 | 9 | 18 --> 0.326 | 0.357 | 0.388 [ms]
    
    // Development Example - set dev = true (in line 29):
    //       copy left - destination same as or different from source
    // 
    //
    // 7 x 4 24bpp graphics buffer in zigo-zag driving a 28 neopixels string
    //
    //        00       01       02       03       04       05       06
    //       .-----------------.        .-----------------.
    // 00    |00 01 02|03 04 05|06 07 08|09 10 11|12 13 14|15 16 17|18 19 20 --.
    //       |                 |        |                 |                    |
    // 01 .--|41 40 39|38 37 36|35 34 33|32 31 30|29 28 27|26 25 24|23 22 21 <-'
    //    |  |                 | <----- |                 |
    // 02 '->|42 43 44|45 46 47|48 49 50|51 52 53|54 55 56|57 58 59|60 61 62 --.
    //       |                 |        |                 |                    |
    // 03    |83 82 81|80 79 78|77 76 75|74 73 72|71 70 69|68 67 66|65 64 63 <-'
    //       '-----------------'        '-----------------'
    //
    `;
    // define destination
    var dev = false
      , dcc = (dev) ? 7 : 20
      , drc = (dev) ? 4 :  7
      , db, dg = Graphics.createArrayBuffer(dcc,drc,24
                ,{zigzag:true,color_order:"rgb"} );
    
    var lon = false
      , log = function() { console.log.apply(console,arguments); };
    
    // copy zig-zag-ed neopixles to left - args array w/ [0] = result
    var czxLArgs; // 'globalized' argument array
    
    function czxL( // copy zig-zag pixels left
          // helper / wrapper to call compiled C: cdC.czxL(args)
          ccc // =0: arg setup, >0: copy col count in pixels
        , crc // copy row count in pixels
        , dst // destination Uint8ArrayBuffer
        , dco // dst col offset in pixels (0..cols-1)
        , dcc // dst col count in pixels
        , src // source Uint8ArrayBuffer
        , sco // src col offset in pixels (0..cols-1)
        , scc // src col count in pixels
        , bpp // bytes per pixel, if absent|falsy: default=3
        , chk // check for validity (not yet implemented)
        ) {
      var args = new Uint32Array(9)
        , addr = E.getAddressOf(args,1)
        , tSiz = (typeof bpp == "undefined") ? 3 : bpp;
      args[0] = 0; // will be return value
      args[1] = ccc*tSiz;
      args[2] = crc;
      args[3] = E.getAddressOf(dst,1);
      args[4] = dco*tSiz;
      args[5] = dcc*tSiz;
      args[6] = E.getAddressOf(src,1);
      args[7] = sco*tSiz;
      args[8] = scc*tSiz;
      if (args[2] && args[6]) { // check for flat strs | avoid disaster
        if (ccc>0) { // doit, otherwise just args setup for reuse
          var t0 = getTime();
          args[0] = cdC.bciLeft(addr);
          var t = getTime() - t0;
          log("t =",("      "+Math.round(t*1000000)/1000).substr(-6)­,"[ms]");
        }
      } else {
        args[0] = -1; // src or dst not flat string (args[2] or args[3] is 0)
      }
      return args;
    }
    
    var cdC = E.compiledC(`
    // int bciLeft(int)
    int bciLeft(unsigned int *args) {
      unsigned int   ccc = *(args+1);
      unsigned int   crc = *(args+2);
      char          *dst = (char*)*(args+3);
      unsigned int   dbo = *(args+4);
      unsigned int   dcc = *(args+5);
      char          *src = (char*)*(args+6);
      unsigned int   sbo = *(args+7);
      unsigned int   scc = *(args+8);
      char            *d;
      char            *s;
      unsigned int     c;
      unsigned int     r = 0; // row
      int            cnt = 0;
      while (r < crc) {   // row < row count
        d = dst + dbo; s = src + sbo; c = ccc;
        while (c-- > 0) *(d++) = *(s++); // copy in even/zig rows - inc'g
        dst += dcc; src += scc; r++; cnt += ccc;
        if (r < crc) {
          dst += dcc - 1; src += scc - 1; d = dst - dbo; s = src - sbo; c = ccc;
          while (c-- > 0) *(d--) = *(s--); // copy in odd/zag rows - dec'g
          dst++; src++; r++; cnt += ccc;
      } }
      return cnt;
    }
    `);
    
    function init() {
      db = dg.buffer; // 'globalize' dst buf
      for (var idx=0; idx<db.length; idx++) {  // prime buf w/......values
        db[idx] = idx % 256; }                 // ...values 0..256,0..256,...
      if (dev) dmpd();
      czxLArgs = czxL( // 'copy zig-zag-ed pixel Left'
          (dev) ? 2 : 19  // ccc - copy col count in pixels | 0 = args build only
        , (dev) ? 4 :  7   // crc - copy row count
        ,         db       // dst - destination buffer
        ,         0        // dco - dst col offset
        , (dev) ? 7 : 20   // dcc - dst col count
        ,         db       // src - source buffer
        , (dev) ? 3 :  1   // sco - src col offset
        , (dev) ? 7 : 20   // scc - src col count
        );
      if (dev) dmpd();
      log("czxLArgs =",czxLArgs);
    }
    
    function onInit() {
      init();
    }
    
    function dmpd() { log("dst buf:"); dmp(db,dcc,3); } // dump destination
    function dmp(buf,bcc,xSiz) { // dump buf to console for debug
      var x = 0, r = 0, s = "", i, j;
      for (i=0;i<bcc;i++) { s+=" "+("  "+i).substr(-3);
        for (j=1;j<xSiz;j++) s+="   "; } log("rows \\ cols",s); s = "";       
      while (x<buf.length) {
        for (i=0;i<bcc;i++) {
          s+=("  "+buf[x++]).substr(-3)+("  "+buf[x++]).substr(-3)+("  "+buf[x++]).substr(-3)+" ";
        } log((" "+(r++)).substr(-2),"-  '->",s,"--."); s = "";
        if (x<buf.length) {
          x+=(bcc*xSiz)-1;
          for (i=0;i<bcc;i++) {
            s+=("  "+buf[x--]).substr(-3)+("  "+buf[x--]).substr(-3)+("  "+buf[x--]).substr(-3)+" ";
          } log((" "+(r++)).substr(-2),"-  .--",s,"<-'"); s = "";
          x+=(bcc*xSiz)+1;
        }
      } log("-- .");
    }
    
    c = "";
    setTimeout(onInit,500); // while dev'g; comment before upload for save()
    

    The console output showing before and after of the development example copying colums 3 and 4 in 7 x 4 to columns 0 and 1 (output sligtly modified/condensed to fit forum's width and not to wrap):

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v01 (c) 2018 G.Williams
    >
    dst buf:
    rows\cols 0         1         2         3         4         5         6
     0 '-> 0  1  2   3  4  5   6  7  8   9 10 11  12 13 14  15 16 17  18 19 20--.
     1 .--41 40 39  38 37 36  35 34 33  32 31 30  29 28 27  26 25 24  23 22 21<-'
     2 '->42 43 44  45 46 47  48 49 50  51 52 53  54 55 56  57 58 59  60 61 62--.
     3 .--83 82 81  80 79 78  77 76 75  74 73 72  71 70 69  68 67 66  65 64 63<-'
    -- .
    t =  0.327 [ms]
    dst buf:
    rows\cols 0         1         2         3         4         5         6
     0 '-> 9 10 11  12 13 14   6  7  8   9 10 11  12 13 14  15 16 17  18 19 20--.
     1 .--32 31 30  29 28 27  35 34 33  32 31 30  29 28 27  26 25 24  23 22 21<-'
     2 '->51 52 53  54 55 56  48 49 50  51 52 53  54 55 56  57 58 59  60 61 62--.
     3 .--74 73 72  71 70 69  77 76 75  74 73 72  71 70 69  68 67 66  65 64 63<-'
    -- .
    czxLArgs = new Uint32Array([24, 6, 4, 536875436, 0, 21, 536875436, 9, 21])
    >
    
About

Avatar for allObjects @allObjects started