forked from Wiladams/LAPHLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaths.lua
More file actions
85 lines (71 loc) · 1.71 KB
/
maths.lua
File metadata and controls
85 lines (71 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local bit = require("bit")
local band, bor = bit.band, bit.bor
local rshift, lshift = bit.rshift, bit.lshift
local limits = require("limits")
local floor = math.floor;
local ceil = math.ceil;
-- because it's not in the standard math library
local function round(n)
if n >= 0 then
return floor(n+0.5)
end
return ceil(n-0.5)
end
local function is_power_of_two(value)
if value == 0 then
return false;
end
return band(value, (value-1)) == 0;
end
-- round up to the nearest
-- power of 2
local function roundup32(x)
x = x - 1;
x = bor(x,rshift(x,1));
x = bor(x,rshift(x,2));
x = bor(x,rshift(x,4));
x = bor(x,rshift(x,8));
x = bor(x,rshift(x,16));
x = x + 1;
return x
end
local function min_bytes_needed(value)
local bytes;
if (value <= limits.UINT32_MAX) then
if (value < 16777216) then
if (value <= limits.UINT16_MAX) then
if (value <= limits.UINT8_MAX) then
bytes = 1;
else
bytes = 2;
end
else
bytes = 3;
end
else
bytes = 4;
end
elseif (value <= limits.UINT64_MAX) then
if (value < 72057594000000000ULL) then
if (value < 281474976710656ULL) then
if (value < 1099511627776ULL) then
bytes = 5;
else
bytes = 6;
end
else
bytes = 7;
end
else
bytes = 8;
end
end
return bytes;
end
local exports = {
is_power_of_two = is_power_of_two;
min_bytes_needed = min_bytes_needed;
round = round;
roundup = roundup32;
}
return exports;