|
| 1 | +-- dmlib.bit |
| 2 | +-- Bitwise operations implemented entirely in Lua. |
| 3 | +-- Represents bit arrays as non-negative Lua numbers.[1] |
| 4 | +-- Can represent 32-bit bit arrays, when Lua is compiled |
| 5 | +-- with lua_Number being double-precision IEEE 754 floating point. |
| 6 | +-- |
| 7 | +-- Based partly on Roberto Ierusalimschy's post |
| 8 | +-- in http://lua-users.org/lists/lua-l/2002-09/msg00134.html . |
| 9 | +-- |
| 10 | +-- References |
| 11 | +-- |
| 12 | +-- [1] http://lua-users.org/wiki/FloatingPoint |
| 13 | +-- |
| 14 | +-- (c) 2008 David Manura. Licensed under the same terms as Lua (MIT). |
| 15 | + |
| 16 | + |
| 17 | +local function memoize(f) |
| 18 | + local mt = {} |
| 19 | + local t = setmetatable({}, mt) |
| 20 | + function mt:__index(k) |
| 21 | + local v = f(k); t[k] = v |
| 22 | + return v |
| 23 | + end |
| 24 | + return t |
| 25 | +end |
| 26 | + |
| 27 | +local function make_bitop_uncached(t, m) |
| 28 | + local function bitop(a, b) |
| 29 | + local res,p = 0,1 |
| 30 | + while a ~= 0 and b ~= 0 do |
| 31 | + local am, bm = a%m, b%m |
| 32 | + res = res + t[am][bm]*p |
| 33 | + a = (a - am) / m |
| 34 | + b = (b - bm) / m |
| 35 | + p = p*m |
| 36 | + end |
| 37 | + res = res + (a+b)*p |
| 38 | + return res |
| 39 | + end |
| 40 | + return bitop |
| 41 | +end |
| 42 | + |
| 43 | +local function make_bitop(t) |
| 44 | + local op1 = make_bitop_uncached(t,2^1) |
| 45 | + local op2 = memoize(function(a) |
| 46 | + return memoize(function(b) |
| 47 | + return op1(a, b) |
| 48 | + end) |
| 49 | + end) |
| 50 | + return make_bitop_uncached(op2, 2^(t.n or 1)) |
| 51 | +end |
| 52 | + |
| 53 | +local bxor = make_bitop {[0]={[0]=0,[1]=1},[1]={[0]=1,[1]=0}, n=4} |
| 54 | +local F8 = 2^32 - 1 |
| 55 | +local function bnot(a) return F8 - a end |
| 56 | +local function band(a,b) return ((a+b) - bxor(a,b))/2 end |
| 57 | +local function bor(a,b) return F8 - band(F8 - a, F8 - b) end |
| 58 | + |
| 59 | +local M = {} |
| 60 | +M.bxor = bxor |
| 61 | +M.bnot = bnot |
| 62 | +M.band = band |
| 63 | +M.bor = bor |
| 64 | + |
| 65 | +return M |
| 66 | + |
| 67 | +--[[ |
| 68 | +LICENSE |
| 69 | +
|
| 70 | +Copyright (C) 2008, David Manura. |
| 71 | +
|
| 72 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 73 | +of this software and associated documentation files (the "Software"), to deal |
| 74 | +in the Software without restriction, including without limitation the rights |
| 75 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 76 | +copies of the Software, and to permit persons to whom the Software is |
| 77 | +furnished to do so, subject to the following conditions: |
| 78 | +
|
| 79 | +The above copyright notice and this permission notice shall be included in |
| 80 | +all copies or substantial portions of the Software. |
| 81 | +
|
| 82 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 83 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 84 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 85 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 86 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 87 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 88 | +THE SOFTWARE. |
| 89 | +
|
| 90 | +(end license) |
| 91 | +--]] |
0 commit comments