forked from LuaDist/bitlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_limits.lua
54 lines (51 loc) · 1.47 KB
/
find_limits.lua
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
-- bitlib test suite
-- (c) Reuben Thomas 2007-2008
-- See README for license
-- Prepend a binary 1 to a hex constant
function prepend_one (s)
if s == "0" then
return "1"
elseif string.sub (s, 1, 1) == "1" then
return "3" .. string.sub (s, 2)
elseif string.sub (s, 1, 1) == "3" then
return "7" .. string.sub (s, 2)
elseif string.sub (s, 1, 1) == "7" then
return "f" .. string.sub (s, 2)
else
return "1" .. s
end
end
-- Invert a hex constant
local inverted = {
["0"] = "f", ["1"] = "e", ["2"] = "d", ["3"] = "c",
["4"] = "b", ["5"] = "a", ["6"] = "9", ["7"] = "8",
["8"] = "7", ["9"] = "6", ["a"] = "5", ["b"] = "4",
["c"] = "3", ["d"] = "2", ["e"] = "1", ["f"] = "0",
}
function invert_hex (s)
for i = 1, #s do
s = string.sub (s, 1, i - 1) .. inverted[string.sub (s, i, i)] .. string.sub (s, i + 1)
end
if string.sub (s, 1, 1) == "0" then
s = "1" .. s
end
return s
end
-- Calculate number of bits in a float mantissa
local float_bits = 0
local float_max = "0"
local float_umax = "0"
local f = 1
repeat
f = f * 2
float_bits = float_bits + 1
if f < f + 1 then
float_max = prepend_one (float_max)
end
float_umax = prepend_one (float_umax)
until f >= f + 1
local float_min = invert_hex (float_max)
print ("#define BITLIB_FLOAT_BITS " .. float_bits)
print ("#define BITLIB_FLOAT_MAX 0x" .. float_max .. "L")
print ("#define BITLIB_FLOAT_MIN (-0x" .. float_min .. "L)")
print ("#define BITLIB_FLOAT_UMAX 0x" .. float_umax .. "UL")