Rename stuff in Numeric for clarity

This commit is contained in:
Rerumu 2022-07-01 14:06:17 -04:00
parent a0596f45e3
commit 627927d366
6 changed files with 32 additions and 35 deletions

View File

@ -1,9 +1,6 @@
local Numeric = {} local Numeric = {}
local BIT_SET_31 = 0x80000000 local NUM_ZERO, NUM_ONE, NUM_BIT_26
local BIT_SET_32 = 0x100000000
local K_ZERO, K_ONE, K_BIT_SET_26
local bit_lshift = bit32.lshift local bit_lshift = bit32.lshift
local bit_rshift = bit32.rshift local bit_rshift = bit32.rshift
@ -62,13 +59,13 @@ else
end end
function Numeric.from_u64(value) function Numeric.from_u64(value)
return from_u32(bit_and(value), math_floor(value / BIT_SET_32)) return from_u32(bit_and(value), math_floor(value / 0x100000000))
end end
function Numeric.into_u64(value) function Numeric.into_u64(value)
local data_1, data_2 = into_u32(value) local data_1, data_2 = into_u32(value)
return data_1 + data_2 * BIT_SET_32 return data_1 + data_2 * 0x100000000
end end
function Numeric.add(lhs, rhs) function Numeric.add(lhs, rhs)
@ -78,13 +75,13 @@ function Numeric.add(lhs, rhs)
local data_1 = data_l_1 + data_r_1 local data_1 = data_l_1 + data_r_1
local data_2 = data_l_2 + data_r_2 local data_2 = data_l_2 + data_r_2
if data_1 >= BIT_SET_32 then if data_1 >= 0x100000000 then
data_1 = data_1 - BIT_SET_32 data_1 = data_1 - 0x100000000
data_2 = data_2 + 1 data_2 = data_2 + 1
end end
if data_2 >= BIT_SET_32 then if data_2 >= 0x100000000 then
data_2 = data_2 - BIT_SET_32 data_2 = data_2 - 0x100000000
end end
return from_u32(data_1, data_2) return from_u32(data_1, data_2)
@ -98,12 +95,12 @@ function Numeric.subtract(lhs, rhs)
local data_2 = data_l_2 - data_r_2 local data_2 = data_l_2 - data_r_2
if data_1 < 0 then if data_1 < 0 then
data_1 = data_1 + BIT_SET_32 data_1 = data_1 + 0x100000000
data_2 = data_2 - 1 data_2 = data_2 - 1
end end
if data_2 < 0 then if data_2 < 0 then
data_2 = data_2 + BIT_SET_32 data_2 = data_2 + 0x100000000
end end
return from_u32(data_1, data_2) return from_u32(data_1, data_2)
@ -127,7 +124,7 @@ end
function Numeric.multiply(lhs, rhs) function Numeric.multiply(lhs, rhs)
if num_is_zero(lhs) or num_is_zero(rhs) then if num_is_zero(lhs) or num_is_zero(rhs) then
return K_ZERO return NUM_ZERO
end end
local has_negative local has_negative
@ -135,7 +132,7 @@ function Numeric.multiply(lhs, rhs)
has_negative, lhs, rhs = set_absolute(lhs, rhs) has_negative, lhs, rhs = set_absolute(lhs, rhs)
-- If both longs are small, use float multiplication -- If both longs are small, use float multiplication
if num_is_less_unsigned(lhs, K_BIT_SET_26) and num_is_less_unsigned(rhs, K_BIT_SET_26) then if num_is_less_unsigned(lhs, NUM_BIT_26) and num_is_less_unsigned(rhs, NUM_BIT_26) then
local data_l_1, _ = into_u32(lhs) local data_l_1, _ = into_u32(lhs)
local data_r_1, _ = into_u32(rhs) local data_r_1, _ = into_u32(rhs)
local result = from_u64(data_l_1 * data_r_1) local result = from_u64(data_l_1 * data_r_1)
@ -211,12 +208,12 @@ function Numeric.divide_unsigned(lhs, rhs)
if num_is_zero(rhs) then if num_is_zero(rhs) then
error("division by zero") error("division by zero")
elseif num_is_zero(lhs) then elseif num_is_zero(lhs) then
return K_ZERO return NUM_ZERO
end end
local rhs_number = into_u64(rhs) local rhs_number = into_u64(rhs)
local rem = lhs local rem = lhs
local res = K_ZERO local res = NUM_ZERO
while num_is_greater_unsigned(rem, rhs) or num_is_equal(rem, rhs) do while num_is_greater_unsigned(rem, rhs) or num_is_equal(rem, rhs) do
local res_approx, delta = get_approx_delta(into_u64(rem), rhs_number) local res_approx, delta = get_approx_delta(into_u64(rem), rhs_number)
@ -230,7 +227,7 @@ function Numeric.divide_unsigned(lhs, rhs)
end end
if num_is_zero(res_temp) then if num_is_zero(res_temp) then
res_temp = K_ONE res_temp = NUM_ONE
end end
res = num_add(res, res_temp) res = num_add(res, res_temp)
@ -255,7 +252,7 @@ function Numeric.divide_signed(lhs, rhs)
end end
function Numeric.negate(value) function Numeric.negate(value)
return num_add(num_not(value), K_ONE) return num_add(num_not(value), NUM_ONE)
end end
function Numeric.bit_and(lhs, rhs) function Numeric.bit_and(lhs, rhs)
@ -342,7 +339,7 @@ function Numeric.shift_right_signed(lhs, rhs)
local _, data_l_2 = into_u32(lhs) local _, data_l_2 = into_u32(lhs)
local data_1 = bit_arshift(data_l_2, count - 32) local data_1 = bit_arshift(data_l_2, count - 32)
local data_2 = data_l_2 > BIT_SET_31 and BIT_SET_32 - 1 or 0 local data_2 = data_l_2 > 0x80000000 and 0xFFFFFFFF or 0
return from_u32(data_1, data_2) return from_u32(data_1, data_2)
end end
@ -351,7 +348,7 @@ end
function Numeric.is_negative(value) function Numeric.is_negative(value)
local _, data_2 = into_u32(value) local _, data_2 = into_u32(value)
return data_2 >= BIT_SET_31 return data_2 >= 0x80000000
end end
function Numeric.is_zero(value) function Numeric.is_zero(value)
@ -425,11 +422,11 @@ num_is_equal = Numeric.is_equal
num_is_less_unsigned = Numeric.is_less_unsigned num_is_less_unsigned = Numeric.is_less_unsigned
num_is_greater_unsigned = Numeric.is_greater_unsigned num_is_greater_unsigned = Numeric.is_greater_unsigned
K_ZERO = from_u64(0) NUM_ZERO = from_u64(0)
K_ONE = from_u64(1) NUM_ONE = from_u64(1)
K_BIT_SET_26 = from_u64(0x4000000) NUM_BIT_26 = from_u64(0x4000000)
Numeric.K_ZERO = K_ZERO Numeric.NUM_ZERO = NUM_ZERO
Numeric.K_ONE = K_ONE Numeric.NUM_ONE = NUM_ONE
return table_freeze(Numeric) return table_freeze(Numeric)

View File

@ -25,8 +25,8 @@ end
do do
local temp = {} local temp = {}
temp.K_ZERO = I64.K_ZERO temp.ZERO = I64.NUM_ZERO
temp.K_ONE = I64.K_ONE temp.ONE = I64.NUM_ONE
temp.from_u32 = num_from_u32 temp.from_u32 = num_from_u32

View File

@ -30,8 +30,8 @@ impl Visitor for Visit {
fn visit_value(&mut self, v: &Value) { fn visit_value(&mut self, v: &Value) {
let name = match v { let name = match v {
Value::I64(0) => "K_ZERO", Value::I64(0) => "ZERO",
Value::I64(1) => "K_ONE", Value::I64(1) => "ONE",
Value::I64(_) => "from_u32", Value::I64(_) => "from_u32",
_ => return, _ => return,
}; };
@ -81,7 +81,7 @@ pub fn visit(ast: &FuncData) -> (BTreeSet<(&'static str, &'static str)>, BTreeSe
}; };
if ast.local_data().iter().any(|v| v.1 == ValType::I64) { if ast.local_data().iter().any(|v| v.1 == ValType::I64) {
visit.local_set.insert(("i64", "K_ZERO")); visit.local_set.insert(("i64", "ZERO"));
} }
ast.accept(&mut visit); ast.accept(&mut visit);

View File

@ -85,8 +85,8 @@ pub fn write_i32(number: i32, w: &mut dyn Write) -> Result<()> {
fn write_i64(number: i64, w: &mut dyn Write) -> Result<()> { fn write_i64(number: i64, w: &mut dyn Write) -> Result<()> {
match number { match number {
0 => write!(w, "i64_K_ZERO "), 0 => write!(w, "i64_ZERO "),
1 => write!(w, "i64_K_ONE "), 1 => write!(w, "i64_ONE "),
_ => { _ => {
let list = number.to_ne_bytes(); let list = number.to_ne_bytes();
let a = u32::from_ne_bytes(list[0..4].try_into().unwrap()); let a = u32::from_ne_bytes(list[0..4].try_into().unwrap());

View File

@ -335,7 +335,7 @@ fn write_variable_list(ast: &FuncData, w: &mut dyn Write) -> Result<()> {
for data in ast.local_data().iter().filter(|v| v.0 != 0) { for data in ast.local_data().iter().filter(|v| v.0 != 0) {
let range = total..total + usize::try_from(data.0).unwrap(); let range = total..total + usize::try_from(data.0).unwrap();
let zero = if data.1 == ValType::I64 { let zero = if data.1 == ValType::I64 {
"i64_K_ZERO " "i64_ZERO "
} else { } else {
"0 " "0 "
}; };

View File

@ -243,8 +243,8 @@ fn write_localize_used(
let has_element_i64 = wasm.element_section().iter().any(|e| e.ty == ValType::I64); let has_element_i64 = wasm.element_section().iter().any(|e| e.ty == ValType::I64);
if has_global_i64 || has_element_i64 { if has_global_i64 || has_element_i64 {
loc_set.insert(("i64", "K_ZERO")); loc_set.insert(("i64", "ZERO"));
loc_set.insert(("i64", "K_ONE")); loc_set.insert(("i64", "ONE"));
loc_set.insert(("i64", "from_u32")); loc_set.insert(("i64", "from_u32"));
} }