Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glsl-in: Add tests for constants NaN handling #1817

Merged
merged 1 commit into from
Apr 11, 2022
Merged
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
26 changes: 23 additions & 3 deletions src/front/glsl/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ impl<'a> ConstantSolver<'a> {
ScalarValue::Float(a),
ScalarValue::Float(b),
ScalarValue::Float(c),
) => {
ScalarValue::Float(glsl_float_min(glsl_float_max(a, b), c))
}
) => ScalarValue::Float(glsl_float_clamp(a, b, c)),
_ => return Err(ConstantSolvingError::InvalidMathArg),
},
width,
Expand Down Expand Up @@ -599,6 +597,15 @@ fn glsl_float_min(x: f64, y: f64) -> f64 {
}
}

/// Helper function to implement the GLSL `clamp` function for floats.
///
/// While Rust does provide a `f64::clamp` method, it requires a version of rust
/// above our MSRV and it also panics if either `min` or `max` are `NaN`s which
/// is not the behavior specified by the glsl specification.
fn glsl_float_clamp(value: f64, min: f64, max: f64) -> f64 {
glsl_float_min(glsl_float_max(value, min), max)
}

#[cfg(test)]
mod tests {
use std::vec;
Expand All @@ -610,6 +617,19 @@ mod tests {

use super::ConstantSolver;

#[test]
fn nan_handling() {
assert!(super::glsl_float_max(f64::NAN, 2.0).is_nan());
assert!(!super::glsl_float_max(2.0, f64::NAN).is_nan());

assert!(super::glsl_float_min(f64::NAN, 2.0).is_nan());
assert!(!super::glsl_float_min(2.0, f64::NAN).is_nan());

assert!(super::glsl_float_clamp(f64::NAN, 1.0, 2.0).is_nan());
assert!(!super::glsl_float_clamp(1.0, f64::NAN, 2.0).is_nan());
assert!(!super::glsl_float_clamp(1.0, 2.0, f64::NAN).is_nan());
}

#[test]
fn unary_op() {
let mut types = UniqueArena::new();
Expand Down