Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions rust/src/instructions/math/logarithm/ln_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ use crate::types::value::Value;
use crate::utils::{expected_type::expected_type, get_type_name::get_type_name};
#[inline(always)]
pub fn ln_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Value, VMError> {
if !a.is_number() {
if !matches!(
&a,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: get_type_name(a),
found: get_type_name(a.clone()),
});
}
Ok(match num_type {
Expand All @@ -34,7 +37,7 @@ pub fn ln_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Value,
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: a.type_of(),
found: "unknown",
});
}
})
Expand All @@ -53,37 +56,50 @@ pub fn ln_func(stack: &mut Stack, num_type: PrimitiveTypes, ip: usize) -> Result
mod tests {
use super::*;
#[test]
fn invalid_operand_reports_type_mismatch_and_preserves_stack() {
let invalid = Value::String("invalid".into());
fn rejects_non_float_operands_without_mutating_stack() {
for (value, found) in [
(Value::Int32(1), "Integer"),
(Value::String("invalid".into()), "String"),
] {
assert!(matches!(
ln_values(value.clone(), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch { ip: 17, expected: "Float", found: actual })
if actual == found
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(matches!(
ln_func(&mut stack, PrimitiveTypes::Flt, 18),
Err(VMError::TypeMismatch { ip: 18, expected: "Float", found: actual })
if actual == found
));
assert_eq!(stack, original);
}
}
#[test]
fn accepts_cross_width_float_operands_and_returns_directive_type() {
assert!(matches!(
ln_values(invalid.clone(), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch {
ip: 17,
expected: "Float",
found: "String"
})
ln_values(Value::Float16(half::f16::ONE), PrimitiveTypes::Dbl, 19),
Ok(Value::Float64(0.0))
));
let mut stack = Stack::from_vec(vec![invalid]);
let original = stack.clone();
assert!(matches!(
ln_func(&mut stack, PrimitiveTypes::Flt, 18),
Err(VMError::TypeMismatch {
ip: 18,
expected: "Float",
found: "String"
})
ln_values(Value::Float64(1.0), PrimitiveTypes::Hlf, 20),
Ok(Value::Float16(value)) if value == half::f16::ZERO
));
assert_eq!(stack, original);
}
#[test]
fn unsupported_directive_reports_numeric_operand_type() {
fn unsupported_directive_reports_unknown_without_mutating_stack() {
assert!(matches!(
ln_values(Value::Float32(1.0), PrimitiveTypes::Int, 19),
Err(VMError::TypeMismatch {
ip: 19,
expected: "Float",
found: "float32"
found: "unknown"
})
));
let mut stack = Stack::from_vec(vec![Value::Float32(1.0)]);
let original = stack.clone();
assert!(ln_func(&mut stack, PrimitiveTypes::Int, 20).is_err());
assert_eq!(stack, original);
}
}
61 changes: 42 additions & 19 deletions rust/src/instructions/math/logarithm/log10_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ use crate::types::value::Value;
use crate::utils::{expected_type::expected_type, get_type_name::get_type_name};
#[inline(always)]
pub fn log10_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Value, VMError> {
if !a.is_number() {
if !matches!(
&a,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: get_type_name(a),
found: get_type_name(a.clone()),
});
}
Ok(match num_type {
Expand All @@ -34,7 +37,7 @@ pub fn log10_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Val
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: a.type_of(),
found: "unknown",
});
}
})
Expand All @@ -53,26 +56,46 @@ pub fn log10_func(stack: &mut Stack, num_type: PrimitiveTypes, ip: usize) -> Res
mod tests {
use super::*;
#[test]
fn invalid_operand_reports_type_mismatch_and_preserves_stack() {
let invalid = Value::String("invalid".into());
fn rejects_non_float_operands_without_mutating_stack() {
for (value, found) in [
(Value::Int32(1), "Integer"),
(Value::String("invalid".into()), "String"),
] {
assert!(matches!(
log10_values(value.clone(), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch { ip: 17, expected: "Float", found: actual })
if actual == found
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(matches!(
log10_func(&mut stack, PrimitiveTypes::Flt, 18),
Err(VMError::TypeMismatch { ip: 18, expected: "Float", found: actual })
if actual == found
));
assert_eq!(stack, original);
}
}
#[test]
fn accepts_cross_width_float_operands_and_returns_directive_type() {
assert!(matches!(
log10_values(invalid.clone(), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch {
ip: 17,
expected: "Float",
found: "String"
})
log10_values(Value::Float16(half::f16::ONE), PrimitiveTypes::Dbl, 19),
Ok(Value::Float64(0.0))
));
let mut stack = Stack::from_vec(vec![invalid]);
let original = stack.clone();
assert!(matches!(
log10_func(&mut stack, PrimitiveTypes::Flt, 18),
Err(VMError::TypeMismatch {
ip: 18,
expected: "Float",
found: "String"
})
log10_values(Value::Float64(1.0), PrimitiveTypes::Hlf, 20),
Ok(Value::Float16(value)) if value == half::f16::ZERO
));
}
#[test]
fn unsupported_directive_reports_unknown_without_mutating_stack() {
assert!(matches!(
log10_values(Value::Float32(1.0), PrimitiveTypes::Int, 21),
Err(VMError::TypeMismatch { ip: 21, expected: "Float", found: "unknown" })
));
let mut stack = Stack::from_vec(vec![Value::Float32(1.0)]);
let original = stack.clone();
assert!(log10_func(&mut stack, PrimitiveTypes::Int, 22).is_err());
assert_eq!(stack, original);
}
}
61 changes: 42 additions & 19 deletions rust/src/instructions/math/logarithm/log2_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ use crate::types::value::Value;
use crate::utils::{expected_type::expected_type, get_type_name::get_type_name};
#[inline(always)]
pub fn log2_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Value, VMError> {
if !a.is_number() {
if !matches!(
&a,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: get_type_name(a),
found: get_type_name(a.clone()),
});
}
Ok(match num_type {
Expand All @@ -34,7 +37,7 @@ pub fn log2_values(a: Value, num_type: PrimitiveTypes, ip: usize) -> Result<Valu
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: a.type_of(),
found: "unknown",
});
}
})
Expand All @@ -53,26 +56,46 @@ pub fn log2_func(stack: &mut Stack, num_type: PrimitiveTypes, ip: usize) -> Resu
mod tests {
use super::*;
#[test]
fn invalid_operand_reports_type_mismatch_and_preserves_stack() {
let invalid = Value::String("invalid".into());
fn rejects_non_float_operands_without_mutating_stack() {
for (value, found) in [
(Value::Int32(1), "Integer"),
(Value::String("invalid".into()), "String"),
] {
assert!(matches!(
log2_values(value.clone(), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch { ip: 17, expected: "Float", found: actual })
if actual == found
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(matches!(
log2_func(&mut stack, PrimitiveTypes::Flt, 18),
Err(VMError::TypeMismatch { ip: 18, expected: "Float", found: actual })
if actual == found
));
assert_eq!(stack, original);
}
}
#[test]
fn accepts_cross_width_float_operands_and_returns_directive_type() {
assert!(matches!(
log2_values(invalid.clone(), PrimitiveTypes::Flt, 17),
Err(VMError::TypeMismatch {
ip: 17,
expected: "Float",
found: "String"
})
log2_values(Value::Float16(half::f16::ONE), PrimitiveTypes::Dbl, 19),
Ok(Value::Float64(0.0))
));
let mut stack = Stack::from_vec(vec![invalid]);
let original = stack.clone();
assert!(matches!(
log2_func(&mut stack, PrimitiveTypes::Flt, 18),
Err(VMError::TypeMismatch {
ip: 18,
expected: "Float",
found: "String"
})
log2_values(Value::Float64(1.0), PrimitiveTypes::Hlf, 20),
Ok(Value::Float16(value)) if value == half::f16::ZERO
));
}
#[test]
fn unsupported_directive_reports_unknown_without_mutating_stack() {
assert!(matches!(
log2_values(Value::Float32(1.0), PrimitiveTypes::Int, 21),
Err(VMError::TypeMismatch { ip: 21, expected: "Float", found: "unknown" })
));
let mut stack = Stack::from_vec(vec![Value::Float32(1.0)]);
let original = stack.clone();
assert!(log2_func(&mut stack, PrimitiveTypes::Int, 22).is_err());
assert_eq!(stack, original);
}
}
43 changes: 32 additions & 11 deletions rust/src/instructions/math/vector/logarithm/expv_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub fn expv_values(value: Value, num_type: PrimitiveTypes, ip: usize) -> Result<
found: get_type_name(value.clone()),
})?;
for value in values.iter() {
if !value.is_number() {
if !matches!(
value,
Value::Float16(_) | Value::Float32(_) | Value::Float64(_)
) {
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
Expand All @@ -41,7 +44,7 @@ pub fn expv_values(value: Value, num_type: PrimitiveTypes, ip: usize) -> Result<
return Err(VMError::TypeMismatch {
ip,
expected: expected_type(num_type, ExpectedCategory::Float),
found: expected_type(num_type, ExpectedCategory::All),
found: "unknown",
});
}
})
Expand All @@ -65,23 +68,41 @@ mod tests {
}
#[test]
fn supports_float_directives() {
for num_type in [
PrimitiveTypes::Hlf,
PrimitiveTypes::Flt,
PrimitiveTypes::Dbl,
] {
assert!(expv_values(array(vec![Value::Int32(1)]), num_type, 0).is_ok());
}
assert!(matches!(
expv_values(array(vec![Value::Float64(0.0)]), PrimitiveTypes::Hlf, 0),
Ok(Value::Array(values)) if matches!(&values[0], Value::Float16(_))
));
assert!(matches!(
expv_values(array(vec![Value::Float16(half::f16::ZERO)]), PrimitiveTypes::Dbl, 1),
Ok(Value::Array(values)) if matches!(&values[0], Value::Float64(_))
));
}
#[test]
fn validates_operands_and_preserves_stack() {
for value in [Value::Bool(false), array(vec![Value::Bool(false)])] {
for (value, found) in [
(Value::Bool(false), "Boolean"),
(array(vec![Value::Int32(1)]), "Integer"),
(array(vec![Value::Bool(false)]), "Boolean"),
] {
assert!(matches!(
expv_values(value.clone(), PrimitiveTypes::Flt, 16),
Err(VMError::TypeMismatch { ip: 16, expected: "Float", found: actual })
if actual == found
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(expv_func(&mut stack, PrimitiveTypes::Flt, 17).is_err());
assert_eq!(stack, original);
}
assert!(expv_values(array(vec![Value::Float32(1.0)]), PrimitiveTypes::Int, 18).is_err());
let value = array(vec![Value::Float32(1.0)]);
assert!(matches!(
expv_values(value.clone(), PrimitiveTypes::Int, 18),
Err(VMError::TypeMismatch { ip: 18, expected: "Float", found: "unknown" })
));
let mut stack = Stack::from_vec(vec![value]);
let original = stack.clone();
assert!(expv_func(&mut stack, PrimitiveTypes::Int, 18).is_err());
assert_eq!(stack, original);
}
#[test]
fn preserves_nan_behavior() {
Expand Down
Loading
Loading