mirror of
https://github.com/koreader/koreader
synced 2024-10-31 21:20:20 +00:00
7210fb478d
* Pickup the eponymous blitting performance tweaks from koreader/koreader-base#878 * Cleanup BitOpts usage (require & cache) * Unify oddness checks (MOD -> AND) * Enforce the native Portrait orientation on Kobo (except @ 16bpp, i.e., KSM w/ 8bpp swap disabled), to allow for faster blitting when unrotted. * Switch CRe BB to 32BPP on color screens * Minor cleanups
77 lines
1.5 KiB
Lua
77 lines
1.5 KiB
Lua
--[[--
|
|
Simple math helper functions
|
|
]]
|
|
|
|
local bit = require("bit")
|
|
|
|
local Math = {}
|
|
|
|
local band = bit.band
|
|
|
|
function Math.roundAwayFromZero(num)
|
|
if num > 0 then
|
|
return math.ceil(num)
|
|
else
|
|
return math.floor(num)
|
|
end
|
|
end
|
|
|
|
function Math.round(num)
|
|
return math.floor(num + 0.5)
|
|
end
|
|
|
|
function Math.oddEven(number)
|
|
if band(number, 1) == 1 then
|
|
return "odd"
|
|
else
|
|
return "even"
|
|
end
|
|
end
|
|
|
|
local function tmin_max(tab, func, op)
|
|
if #tab == 0 then return nil, nil end
|
|
local index, value = 1, tab[1]
|
|
for i = 2, #tab do
|
|
if func then
|
|
if func(value, tab[i]) then
|
|
index, value = i, tab[i]
|
|
end
|
|
elseif op == "min" then
|
|
if value > tab[i] then
|
|
index, value = i, tab[i]
|
|
end
|
|
elseif op == "max" then
|
|
if value < tab[i] then
|
|
index, value = i, tab[i]
|
|
end
|
|
end
|
|
end
|
|
return index, value
|
|
end
|
|
|
|
--[[--
|
|
Returns the minimum element of a table.
|
|
The optional argument func specifies a one-argument ordering function.
|
|
|
|
@tparam table tab
|
|
@tparam func func
|
|
@treturn dynamic minimum element of a table
|
|
]]
|
|
function Math.tmin(tab, func)
|
|
return tmin_max(tab, func, "min")
|
|
end
|
|
|
|
--[[--
|
|
Returns the maximum element of a table.
|
|
The optional argument func specifies a one-argument ordering function.
|
|
|
|
@tparam table tab
|
|
@tparam func func
|
|
@treturn dynamic maximum element of a table
|
|
]]
|
|
function Math.tmax(tab, func)
|
|
return tmin_max(tab, func, "max")
|
|
end
|
|
|
|
return Math
|