Optimizing simple integer-arithmetic
Posted: Mon Dec 29, 2008 2:52 pm
While trying to make a little algorithm more efficient, I was wondering whether it was possible to use low-level-integer-arithmetic of the architecture I am using, directly. I like the many numeric types of common lisp, which are flexible and well-optimized for their general purposes, but for my special purpose, it would be enogh to have integers ranging from -10000 to 10000 to add, subtract, multiply (and divide rounded). For any value to be out of this range, I wouldnt care what happens.
As far as I know, most processors are calculating sums and products directly modulo 2^n for some n, i.e. x86 modulo 2^32. This would be sufficient for my purposes (and it would certainly be faster).
Specifying (integer -10000 10000) or fixnum seems not to be enough. I read http://www.sbcl.org/manual/Modular-arithmetic.html - this doesnt seem to work for multiplying, and it only holds for sbcl - but on the other hand, it is about a very old version of sbcl, maybe it meanwhile has gotten better.
On the other hand, sbcl has some functions in sb-vm, namely sb-vm::*-smod30 and sb-vm::+-smod30. These look like they were optimized to some degree. I dont like to write special code for different lisp implementations, but if necessary (and if it is useful - meanwhile it is fast enough, but i am still interested in this issue) I would do that, but then at least I would like to write optimized code for many other implementations, too. Maybe there is already some kind of Library allowing to find types which are some kind of more low-level in different implementations, maybe there is even a standard for such things.
As far as I know, most processors are calculating sums and products directly modulo 2^n for some n, i.e. x86 modulo 2^32. This would be sufficient for my purposes (and it would certainly be faster).
Specifying (integer -10000 10000) or fixnum seems not to be enough. I read http://www.sbcl.org/manual/Modular-arithmetic.html - this doesnt seem to work for multiplying, and it only holds for sbcl - but on the other hand, it is about a very old version of sbcl, maybe it meanwhile has gotten better.
On the other hand, sbcl has some functions in sb-vm, namely sb-vm::*-smod30 and sb-vm::+-smod30. These look like they were optimized to some degree. I dont like to write special code for different lisp implementations, but if necessary (and if it is useful - meanwhile it is fast enough, but i am still interested in this issue) I would do that, but then at least I would like to write optimized code for many other implementations, too. Maybe there is already some kind of Library allowing to find types which are some kind of more low-level in different implementations, maybe there is even a standard for such things.