(fast-logext n x) interprets the least significant
This is logically identical to logext. But, for better performance we adopt a method from Sean Anderson's bit twiddling hacks page, viz:
unsigned n; // number of bits representing the number in x int x; // sign extend this n-bit number to r int r; // resulting sign-extended number int const m = 1U << (n - 1); // mask can be pre-computed if n is fixed x = x & ((1U << n) - 1); // (Skip this if bits in x above position n are already zero.) r = (x ^ m) - m;
Macro:
(defmacro fast-logext (n x) (cond ((eql n 8) (cons 'fast-logext8 (cons x 'nil))) ((eql n 16) (cons 'fast-logext16 (cons x 'nil))) ((eql n 32) (cons 'fast-logext32 (cons x 'nil))) ((eql n 64) (cons 'fast-logext64 (cons x 'nil))) (t (cons 'fast-logext-fn (cons n (cons x 'nil))))))