Commits

Boolsheet  committed 3ecc36d

Cleaning up some old stuff.
Added a lookup table for power of two numbers.

  • Participants
  • Parent commits e250424

Comments (0)

Files changed (2)

 
 local POW31 = 2^31
 local POW32 = 2^32
+local POT_LUT = {}
+for i = 0, 32 do
+	POT_LUT[i] = 2^i
+end
 
 local band, bor, bxor, bxor8
-local AND_LT, OR_LT, XOR_LT
-
 
 local bit_lua = {}
 
-if BIT_CREATE_GLOBAL_TABLE then
-	if bit then
-		assert(type(bit) == "table", "'bit' not a table.")
-	else
-		bit = {}
-	end
-
-	if bit.lua then
-		assert(type(bit.lua) == "table", "'bit.lua' not a table.")
-	else
-		bit.lua = bit_lua
-	end
-end
-
 
 band = function(int1, int2, int3, ...)
 	int2 =
 
 -- The lookup tables use about 277 KiB. It's almost 4 times faster in my tests.
 if BIT_USE_LOOKUP_TABLES then
-	AND_LT, OR_LT, XOR_LT = {}, {}, {}
+	local AND_LT, OR_LT, XOR_LT = {}, {}, {}
 
 	for b1 = 0, 255 do
 		AND_LT[b1], OR_LT[b1], XOR_LT[b1] = {}, {}, {}
 
 
 bit_lua.lshift = function(int, by)
-	return (int * (2^by))%POW32
+	return (int * POT_LUT[by])%POW32
 end
 
 bit_lua.rshift = function(int, by)
-	local shifted = int / 2^by
+	local shifted = int / POT_LUT[by]
 	return shifted - shifted%1
 end
 
 bit_lua.arshift = function(int, by)
-	local pf = 2^by
+	local pf = POT_LUT[by]
 	local shifted = int / pf
-	return shifted - shifted%1 + ((int>=POW31 and (pf-1) * 2^(32-by)) or 0)
+	return shifted - shifted%1 + ((int>=POW31 and (pf-1) * POT_LUT[32-by]) or 0)
 end
 
 
 bit_lua.lrotate = function(int, by)
-	local shifted = int / 2^(32-by)
+	local shifted = int / POT_LUT[32-by]
 	local fraction = shifted%1
 	return (shifted-fraction) + fraction*POW32
 end
 
 bit_lua.rrotate = function(int, by)
-	local shifted = int / 2^by
+	local shifted = int / POT_LUT[by]
 	local fraction = shifted%1
 	return (shifted - fraction) + fraction*POW32
 end
 
 	Function list:
 
-	utils.hextostr(string_hex)
+	utils.hextobin(string_hex)
 	utils.logbase(number, number_base)
 	utils.sign(number)
 	utils.clamp(number, number_lower_limit, number_upper_limit)
 local POWN1022 = 2^-1022
 local INFINITY = math.huge
 
-
-local utils
-
-if _G.utils then
-	assert(type(_G.utils) == "table", "'utils' not a table.")
-	utils = _G.utils
-else
-	utils = {}
-	_G.utils = utils
-end
+local utils = {}
 
 
 utils.strchar_lookup = {}
 end
 
 
--- Hex to string.
-utils.hextostr = function(hex)
+-- Hex to binary.
+utils.hextobin = function(hex)
 	return (hex:gsub("(..)", function(byte) return utils.strchar_lookup[tonumber(byte, 16)] end))
 end