bit_ops.lua 577 B

123456789101112131415161718192021222324
  1. local bit = require "bit"
  2. local ops = {}
  3. local band = bit.band
  4. local bor = bit.bor
  5. local bxor = bit.bxor
  6. ops.uint32_lrot = bit.rol
  7. ops.byte_xor = bxor
  8. ops.uint32_xor_3 = bxor
  9. ops.uint32_xor_4 = bxor
  10. function ops.uint32_ternary(a, b, c)
  11. -- c ~ (a & (b ~ c)) has less bitwise operations than (a & b) | (~a & c).
  12. return bxor(c, band(a, bxor(b, c)))
  13. end
  14. function ops.uint32_majority(a, b, c)
  15. -- (a & (b | c)) | (b & c) has less bitwise operations than (a & b) | (a & c) | (b & c).
  16. return bor(band(a, bor(b, c)), band(b, c))
  17. end
  18. return ops