Hi everyone,<div><br></div><div>I have been working on an image processing library that I want to use on android. I know that performance is a bit of a problem on image processing, so I started to study some bit hacking to improve the performance on my integer operations.  My problem now is that I don't know why my c-lambdas take so much time to execute. All operations take constant time and there is no branching. Probably is some sort of conversion time between C types and GAMBIT types. I expected min-integer and abs-integer to be a lot faster than min and abs, respectively. Any help is appreciated. </div>
<div><br></div><div>URL REFERENCE: </div><div><a href="http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs">http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs</a></div><div><a href="http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax">http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax</a><br>
<br></div><div><br></div><div>MY ENVIRONMENT:</div><div>Linux casa 3.6.4-1-ARCH #1 SMP PREEMPT Mon Oct 29 15:13:56 CET 2012 i686 GNU/Linux</div><div><br></div><div><div>gcc -v</div><div>Using built-in specs.</div><div>COLLECT_GCC=gcc</div>
<div>COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/lto-wrapper</div><div>Target: i686-pc-linux-gnu</div><div>Configured with: /build/src/gcc-4.7.2/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=<a href="https://bugs.archlinux.org/">https://bugs.archlinux.org/</a> --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release</div>
<div>Thread model: posix</div><div>gcc version 4.7.2 (GCC) </div></div><div><br></div><div><div>Gambit v4.6.6 (SINGLE HOST and GCC extensions enabled)</div></div><div><br></div><div><br></div><div>SCHEME CODE: compiled with ---> gambitc -exe   bithacks.scm</div>
<div><br></div><div>;; bithacks.scm</div><div><div><br></div><div><br></div><div>(declare</div><div> (not safe)</div><div> (fixnum)</div><div> (inline)</div><div> (inline-primitives))</div><div><br></div><div>;; INTEGER ABSOLUTE</div>
<div>(define abs-integer </div><div>  (c-lambda (int) int</div><div>    "// we want to find the absolute value of arg1</div><div>     int const mask = ___arg1 >> sizeof(int) * CHAR_BIT - 1;</div><div>     ___result = (___arg1 + mask) ^ mask;"))</div>
<div><br></div><div><br></div><div>(define min-integer</div><div>  (c-lambda (int int) int</div><div>    "// find the smaller of arg1 arg2</div><div>     ___result = ___arg2 ^ ((___arg1 ^ ___arg2) & -(___arg1 < ___arg2));"))</div>
<div><br></div><div>(define (test-abs)</div><div>  (let ((result 0))</div><div>    (begin</div><div>      (display "abs:\n")</div><div>      (time </div><div>       (let loop-i ((i 0))</div><div>         (cond ((fx< i 100000)</div>
<div>                (set! result (abs i))</div><div>                (loop-i (fx+ 1 i))))))</div><div>      (display "abs-integer:\n")</div><div>      (time</div><div>       (let loop-j ((j 0))</div><div>         (cond ((fx< j 100000)</div>
<div>                (set! result (abs-integer j))</div><div>                (loop-j (fx+ 1 j))))))))) </div><div><br></div><div>(define (test-min)</div><div>  (let ((result 0))</div><div>    (begin</div><div>      (display "min:\n")</div>
<div>      (time </div><div>       (let loop-i ((i 0))</div><div>         (cond ((fx< i 100000)</div><div>                (set! result (min i result))</div><div>                (loop-i (fx+ 1 i))))))</div><div>      (display "min-integer:\n")</div>
<div>      (time</div><div>       (let loop-j ((j 0))</div><div>         (cond ((fx< j 100000)</div><div>                (set! result (min-integer j result))</div><div>                (loop-j (fx+ 1 j))))))))) </div><div>
<br></div><div>;; run tests</div><div><br></div><div>(test-abs)</div><div>(test-min)</div></div><div><br></div><div>;;;;;;;;;;;;;;;;;;;;;; RESULTS;;;;;;;;;;;;;;;;;;;;;</div><div><br></div><div><div>abs:</div><div>(time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i))))))</div>
<div>    2 ms real time</div><div>    3 ms cpu time (3 user, 0 system)</div><div>    no collections</div><div>    no bytes allocated</div><div>    no minor faults</div><div>    no major faults</div><div>abs-integer:</div>
<div>(time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j))))))</div><div>    22 ms real time</div><div>    23 ms cpu time (23 user, 0 system)</div><div>    no collections</div>
<div>    no bytes allocated</div><div>    2 minor faults</div><div>    no major faults</div><div>min:</div><div>(time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i))))))</div><div>
    2 ms real time</div><div>    3 ms cpu time (0 user, 3 system)</div><div>    no collections</div><div>    no bytes allocated</div><div>    no minor faults</div><div>    no major faults</div><div>min-integer:</div><div>
(time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j))))))</div><div>    28 ms real time</div><div>    30 ms cpu time (27 user, 3 system)</div><div>    no collections</div>
<div>    no bytes allocated</div><div>    no minor faults</div><div>    no major faults</div></div><div><br></div>