Skip to content

Commit 9753478

Browse files
style(const_eval): match variant decl. order of current MathFunction impls.
1 parent 27f2d93 commit 9753478

File tree

1 file changed

+117
-104
lines changed

1 file changed

+117
-104
lines changed

naga/src/proc/constant_evaluator.rs

Lines changed: 117 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,9 @@ impl<'a> ConstantEvaluator<'a> {
837837
));
838838
}
839839

840+
// NOTE: We try to match the declaration order of `MathFunction` here.
840841
match fun {
842+
// comparison
841843
crate::MathFunction::Abs => {
842844
component_wise_scalar(self, span, [arg], |args| match args {
843845
Scalar::AbstractFloat([e]) => Ok(Scalar::AbstractFloat([e.abs()])),
@@ -847,31 +849,15 @@ impl<'a> ConstantEvaluator<'a> {
847849
Scalar::U32([e]) => Ok(Scalar::U32([e])), // TODO: just re-use the expression, ezpz
848850
})
849851
}
850-
crate::MathFunction::Acos => {
851-
component_wise_float!(self, span, [arg], |e| { Ok([e.acos()]) })
852-
}
853-
crate::MathFunction::Acosh => {
854-
component_wise_float!(self, span, [arg], |e| { Ok([e.acosh()]) })
855-
}
856-
crate::MathFunction::Asin => {
857-
component_wise_float!(self, span, [arg], |e| { Ok([e.asin()]) })
858-
}
859-
crate::MathFunction::Asinh => {
860-
component_wise_float!(self, span, [arg], |e| { Ok([e.asinh()]) })
861-
}
862-
crate::MathFunction::Atan => {
863-
component_wise_float!(self, span, [arg], |e| { Ok([e.atan()]) })
864-
}
865-
crate::MathFunction::Atanh => {
866-
component_wise_float!(self, span, [arg], |e| { Ok([e.atanh()]) })
867-
}
868-
crate::MathFunction::Pow => {
869-
component_wise_float!(self, span, [arg, arg1.unwrap()], |e1, e2| {
870-
Ok([e1.powf(e2)])
852+
crate::MathFunction::Min => {
853+
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| {
854+
Ok([e1.min(e2)])
871855
})
872856
}
873-
crate::MathFunction::Ceil => {
874-
component_wise_float!(self, span, [arg], |e| { Ok([e.ceil()]) })
857+
crate::MathFunction::Max => {
858+
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| {
859+
Ok([e1.max(e2)])
860+
})
875861
}
876862
crate::MathFunction::Clamp => {
877863
component_wise_scalar!(
@@ -887,90 +873,60 @@ impl<'a> ConstantEvaluator<'a> {
887873
}
888874
)
889875
}
876+
crate::MathFunction::Saturate => {
877+
component_wise_float!(self, span, [arg], |e| { Ok([e.clamp(0., 1.)]) })
878+
}
879+
880+
// trigonometry
890881
crate::MathFunction::Cos => {
891882
component_wise_float!(self, span, [arg], |e| { Ok([e.cos()]) })
892883
}
893884
crate::MathFunction::Cosh => {
894885
component_wise_float!(self, span, [arg], |e| { Ok([e.cosh()]) })
895886
}
896-
crate::MathFunction::CountLeadingZeros => {
897-
component_wise_concrete_int!(self, span, [arg], |e| {
898-
#[allow(clippy::useless_conversion)]
899-
Ok([e
900-
.leading_zeros()
901-
.try_into()
902-
.expect("bit count overflowed 32 bits, somehow!?")])
903-
})
904-
}
905-
crate::MathFunction::CountOneBits => {
906-
component_wise_concrete_int!(self, span, [arg], |e| {
907-
#[allow(clippy::useless_conversion)]
908-
Ok([e
909-
.count_ones()
910-
.try_into()
911-
.expect("bit count overflowed 32 bits, somehow!?")])
912-
})
913-
}
914-
crate::MathFunction::CountTrailingZeros => {
915-
component_wise_concrete_int!(self, span, [arg], |e| {
916-
#[allow(clippy::useless_conversion)]
917-
Ok([e
918-
.trailing_zeros()
919-
.try_into()
920-
.expect("bit count overflowed 32 bits, somehow!?")])
921-
})
922-
}
923-
crate::MathFunction::Degrees => {
924-
component_wise_float!(self, span, [arg], |e| { Ok([e.to_degrees()]) })
925-
}
926-
crate::MathFunction::Exp => {
927-
component_wise_float!(self, span, [arg], |e| { Ok([e.exp()]) })
887+
crate::MathFunction::Sin => {
888+
component_wise_float!(self, span, [arg], |e| { Ok([e.sin()]) })
928889
}
929-
crate::MathFunction::Exp2 => {
930-
component_wise_float!(self, span, [arg], |e| { Ok([e.exp2()]) })
890+
crate::MathFunction::Sinh => {
891+
component_wise_float!(self, span, [arg], |e| { Ok([e.sinh()]) })
931892
}
932-
crate::MathFunction::Floor => {
933-
component_wise_float!(self, span, [arg], |e| { Ok([e.floor()]) })
893+
crate::MathFunction::Tan => {
894+
component_wise_float!(self, span, [arg], |e| { Ok([e.tan()]) })
934895
}
935-
crate::MathFunction::Fract => {
936-
component_wise_float!(self, span, [arg], |e| {
937-
// N.B., Rust's definition of `fract` is `e - e.trunc()`, so we can't use that
938-
// here.
939-
Ok([e - e.floor()])
940-
})
896+
crate::MathFunction::Tanh => {
897+
component_wise_float!(self, span, [arg], |e| { Ok([e.tanh()]) })
941898
}
942-
crate::MathFunction::Fma => {
943-
component_wise_float!(
944-
self,
945-
span,
946-
[arg, arg1.unwrap(), arg2.unwrap()],
947-
|e1, e2, e3| { Ok([e1.mul_add(e2, e3)]) }
948-
)
899+
crate::MathFunction::Acos => {
900+
component_wise_float!(self, span, [arg], |e| { Ok([e.acos()]) })
949901
}
950-
crate::MathFunction::InverseSqrt => {
951-
component_wise_float!(self, span, [arg], |e| { Ok([1. / e.sqrt()]) })
902+
crate::MathFunction::Asin => {
903+
component_wise_float!(self, span, [arg], |e| { Ok([e.asin()]) })
952904
}
953-
crate::MathFunction::Log => {
954-
component_wise_float!(self, span, [arg], |e| { Ok([e.ln()]) })
905+
crate::MathFunction::Atan => {
906+
component_wise_float!(self, span, [arg], |e| { Ok([e.atan()]) })
955907
}
956-
crate::MathFunction::Log2 => {
957-
component_wise_float!(self, span, [arg], |e| { Ok([e.log2()]) })
908+
crate::MathFunction::Asinh => {
909+
component_wise_float!(self, span, [arg], |e| { Ok([e.asinh()]) })
958910
}
959-
crate::MathFunction::Max => {
960-
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| {
961-
Ok([e1.max(e2)])
962-
})
911+
crate::MathFunction::Acosh => {
912+
component_wise_float!(self, span, [arg], |e| { Ok([e.acosh()]) })
963913
}
964-
crate::MathFunction::Min => {
965-
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| {
966-
Ok([e1.min(e2)])
967-
})
914+
crate::MathFunction::Atanh => {
915+
component_wise_float!(self, span, [arg], |e| { Ok([e.atanh()]) })
968916
}
969917
crate::MathFunction::Radians => {
970918
component_wise_float!(self, span, [arg], |e1| { Ok([e1.to_radians()]) })
971919
}
972-
crate::MathFunction::ReverseBits => {
973-
component_wise_concrete_int!(self, span, [arg], |e| { Ok([e.reverse_bits()]) })
920+
crate::MathFunction::Degrees => {
921+
component_wise_float!(self, span, [arg], |e| { Ok([e.to_degrees()]) })
922+
}
923+
924+
// decomposition
925+
crate::MathFunction::Ceil => {
926+
component_wise_float!(self, span, [arg], |e| { Ok([e.ceil()]) })
927+
}
928+
crate::MathFunction::Floor => {
929+
component_wise_float!(self, span, [arg], |e| { Ok([e.floor()]) })
974930
}
975931
crate::MathFunction::Round => {
976932
// TODO: Use `f{32,64}.round_ties_even()` when available on stable. This polyfill
@@ -998,35 +954,92 @@ impl<'a> ConstantEvaluator<'a> {
998954
Float::F32([e]) => Ok(Float::F32([(round_ties_even(e as f64) as f32)])),
999955
})
1000956
}
1001-
crate::MathFunction::Saturate => {
1002-
component_wise_float!(self, span, [arg], |e| { Ok([e.clamp(0., 1.)]) })
957+
crate::MathFunction::Fract => {
958+
component_wise_float!(self, span, [arg], |e| {
959+
// N.B., Rust's definition of `fract` is `e - e.trunc()`, so we can't use that
960+
// here.
961+
Ok([e - e.floor()])
962+
})
1003963
}
1004-
crate::MathFunction::Sign => {
1005-
component_wise_signed!(self, span, [arg], |e| { Ok([e.signum()]) })
964+
crate::MathFunction::Trunc => {
965+
component_wise_float!(self, span, [arg], |e| { Ok([e.trunc()]) })
1006966
}
1007-
crate::MathFunction::Sin => {
1008-
component_wise_float!(self, span, [arg], |e| { Ok([e.sin()]) })
967+
968+
// exponent
969+
crate::MathFunction::Exp => {
970+
component_wise_float!(self, span, [arg], |e| { Ok([e.exp()]) })
1009971
}
1010-
crate::MathFunction::Sinh => {
1011-
component_wise_float!(self, span, [arg], |e| { Ok([e.sinh()]) })
972+
crate::MathFunction::Exp2 => {
973+
component_wise_float!(self, span, [arg], |e| { Ok([e.exp2()]) })
1012974
}
1013-
crate::MathFunction::Tan => {
1014-
component_wise_float!(self, span, [arg], |e| { Ok([e.tan()]) })
975+
crate::MathFunction::Log => {
976+
component_wise_float!(self, span, [arg], |e| { Ok([e.ln()]) })
1015977
}
1016-
crate::MathFunction::Tanh => {
1017-
component_wise_float!(self, span, [arg], |e| { Ok([e.tanh()]) })
978+
crate::MathFunction::Log2 => {
979+
component_wise_float!(self, span, [arg], |e| { Ok([e.log2()]) })
1018980
}
1019-
crate::MathFunction::Sqrt => {
1020-
component_wise_float!(self, span, [arg], |e| { Ok([e.sqrt()]) })
981+
crate::MathFunction::Pow => {
982+
component_wise_float!(self, span, [arg, arg1.unwrap()], |e1, e2| {
983+
Ok([e1.powf(e2)])
984+
})
985+
}
986+
987+
// computational
988+
crate::MathFunction::Sign => {
989+
component_wise_signed!(self, span, [arg], |e| { Ok([e.signum()]) })
990+
}
991+
crate::MathFunction::Fma => {
992+
component_wise_float!(
993+
self,
994+
span,
995+
[arg, arg1.unwrap(), arg2.unwrap()],
996+
|e1, e2, e3| { Ok([e1.mul_add(e2, e3)]) }
997+
)
1021998
}
1022999
crate::MathFunction::Step => {
10231000
component_wise_float!(self, span, [arg, arg1.unwrap()], |edge, x| {
10241001
Ok([if edge <= x { 1.0 } else { 0.0 }])
10251002
})
10261003
}
1027-
crate::MathFunction::Trunc => {
1028-
component_wise_float!(self, span, [arg], |e| { Ok([e.trunc()]) })
1004+
crate::MathFunction::Sqrt => {
1005+
component_wise_float!(self, span, [arg], |e| { Ok([e.sqrt()]) })
1006+
}
1007+
crate::MathFunction::InverseSqrt => {
1008+
component_wise_float!(self, span, [arg], |e| { Ok([1. / e.sqrt()]) })
10291009
}
1010+
1011+
// bits
1012+
crate::MathFunction::CountTrailingZeros => {
1013+
component_wise_concrete_int!(self, span, [arg], |e| {
1014+
#[allow(clippy::useless_conversion)]
1015+
Ok([e
1016+
.trailing_zeros()
1017+
.try_into()
1018+
.expect("bit count overflowed 32 bits, somehow!?")])
1019+
})
1020+
}
1021+
crate::MathFunction::CountLeadingZeros => {
1022+
component_wise_concrete_int!(self, span, [arg], |e| {
1023+
#[allow(clippy::useless_conversion)]
1024+
Ok([e
1025+
.leading_zeros()
1026+
.try_into()
1027+
.expect("bit count overflowed 32 bits, somehow!?")])
1028+
})
1029+
}
1030+
crate::MathFunction::CountOneBits => {
1031+
component_wise_concrete_int!(self, span, [arg], |e| {
1032+
#[allow(clippy::useless_conversion)]
1033+
Ok([e
1034+
.count_ones()
1035+
.try_into()
1036+
.expect("bit count overflowed 32 bits, somehow!?")])
1037+
})
1038+
}
1039+
crate::MathFunction::ReverseBits => {
1040+
component_wise_concrete_int!(self, span, [arg], |e| { Ok([e.reverse_bits()]) })
1041+
}
1042+
10301043
fun => Err(ConstantEvaluatorError::NotImplemented(format!(
10311044
"{fun:?} built-in function"
10321045
))),

0 commit comments

Comments
 (0)