0

Is there any function in which z3 calculate directly the logarithm to Bitvectors?

Otherwise how can I implement the logarithm efficiently?

Robert
  • 211
  • 1
  • 4
  • 13

1 Answers1

1

I think your best bet is to code it up as an if-then-else chain as essentially a look-up table. Assuming base-2, you'd have something like (assuming lg 0 = 0):

(ite (< x 2) 0
     (ite (< x 4) 1
          (ite (< x 8) 2
           .... etc ...

Since logarithm grows slowly, you'd only need 32 branches for 32-bit vectors, etc. Other bases would be similar. You can easily generate this code automatically.

alias
  • 28,120
  • 2
  • 23
  • 40
  • You can also implement the same by counting the index of the left-most set bit, by doing bit-masking. (i.e, assuming 4-bit vectors, bitwise-and with 1000 and if the result is non-0 then logarithm is 3, otherwise bitwise-and with 01000 to get 2, etc.) I'm not sure if that'll give you anything better compared to the above solution though; but might be worth a try if the above doesn't work. – alias Aug 01 '13 at 21:12