[gambit-list] Slow Bit Hacking with c-lambda

tcl super dude tcleval at gmail.com
Sun Nov 4 13:06:02 EST 2012


Hi everyone,

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.

URL REFERENCE:
http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs
http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax


MY ENVIRONMENT:
Linux casa 3.6.4-1-ARCH #1 SMP PREEMPT Mon Oct 29 15:13:56 CET 2012 i686
GNU/Linux

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/lto-wrapper
Target: i686-pc-linux-gnu
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=https://bugs.archlinux.org/--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
Thread model: posix
gcc version 4.7.2 (GCC)

Gambit v4.6.6 (SINGLE HOST and GCC extensions enabled)


SCHEME CODE: compiled with ---> gambitc -exe   bithacks.scm

;; bithacks.scm


(declare
 (not safe)
 (fixnum)
 (inline)
 (inline-primitives))

;; INTEGER ABSOLUTE
(define abs-integer
  (c-lambda (int) int
    "// we want to find the absolute value of arg1
     int const mask = ___arg1 >> sizeof(int) * CHAR_BIT - 1;
     ___result = (___arg1 + mask) ^ mask;"))


(define min-integer
  (c-lambda (int int) int
    "// find the smaller of arg1 arg2
     ___result = ___arg2 ^ ((___arg1 ^ ___arg2) & -(___arg1 < ___arg2));"))

(define (test-abs)
  (let ((result 0))
    (begin
      (display "abs:\n")
      (time
       (let loop-i ((i 0))
         (cond ((fx< i 100000)
                (set! result (abs i))
                (loop-i (fx+ 1 i))))))
      (display "abs-integer:\n")
      (time
       (let loop-j ((j 0))
         (cond ((fx< j 100000)
                (set! result (abs-integer j))
                (loop-j (fx+ 1 j)))))))))

(define (test-min)
  (let ((result 0))
    (begin
      (display "min:\n")
      (time
       (let loop-i ((i 0))
         (cond ((fx< i 100000)
                (set! result (min i result))
                (loop-i (fx+ 1 i))))))
      (display "min-integer:\n")
      (time
       (let loop-j ((j 0))
         (cond ((fx< j 100000)
                (set! result (min-integer j result))
                (loop-j (fx+ 1 j)))))))))

;; run tests

(test-abs)
(test-min)

;;;;;;;;;;;;;;;;;;;;;; RESULTS;;;;;;;;;;;;;;;;;;;;;

abs:
(time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i))
(loop-i (fx+ 1 i))))))
    2 ms real time
    3 ms cpu time (3 user, 0 system)
    no collections
    no bytes allocated
    no minor faults
    no major faults
abs-integer:
(time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer
j)) (loop-j (fx+ 1 j))))))
    22 ms real time
    23 ms cpu time (23 user, 0 system)
    no collections
    no bytes allocated
    2 minor faults
    no major faults
min:
(time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i
result)) (loop-i (fx+ 1 i))))))
    2 ms real time
    3 ms cpu time (0 user, 3 system)
    no collections
    no bytes allocated
    no minor faults
    no major faults
min-integer:
(time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j
result)) (loop-j (fx+ 1 j))))))
    28 ms real time
    30 ms cpu time (27 user, 3 system)
    no collections
    no bytes allocated
    no minor faults
    no major faults
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20121104/2a532f5f/attachment.htm>


More information about the Gambit-list mailing list