Skip to content

Commit 119a549

Browse files
style(const_eval): match variant decl. order of current MathFunction impls.
1 parent 05bb72b commit 119a549

File tree

1 file changed

+113
-100
lines changed

1 file changed

+113
-100
lines changed

naga/src/proc/constant_evaluator.rs

Lines changed: 113 additions & 100 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,86 +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| { Ok([e.fract()]) })
896+
crate::MathFunction::Tanh => {
897+
component_wise_float!(self, span, [arg], |e| { Ok([e.tanh()]) })
937898
}
938-
crate::MathFunction::Fma => {
939-
component_wise_float!(
940-
self,
941-
span,
942-
[arg, arg1.unwrap(), arg2.unwrap()],
943-
|e1, e2, e3| { Ok([e1.mul_add(e2, e3)]) }
944-
)
899+
crate::MathFunction::Acos => {
900+
component_wise_float!(self, span, [arg], |e| { Ok([e.acos()]) })
945901
}
946-
crate::MathFunction::InverseSqrt => {
947-
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()]) })
948904
}
949-
crate::MathFunction::Log => {
950-
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()]) })
951907
}
952-
crate::MathFunction::Log2 => {
953-
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()]) })
954910
}
955-
crate::MathFunction::Max => {
956-
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| {
957-
Ok([e1.max(e2)])
958-
})
911+
crate::MathFunction::Acosh => {
912+
component_wise_float!(self, span, [arg], |e| { Ok([e.acosh()]) })
959913
}
960-
crate::MathFunction::Min => {
961-
component_wise_scalar!(self, span, [arg, arg1.unwrap()], |e1, e2| {
962-
Ok([e1.min(e2)])
963-
})
914+
crate::MathFunction::Atanh => {
915+
component_wise_float!(self, span, [arg], |e| { Ok([e.atanh()]) })
964916
}
965917
crate::MathFunction::Radians => {
966918
component_wise_float!(self, span, [arg], |e1| { Ok([e1.to_radians()]) })
967919
}
968-
crate::MathFunction::ReverseBits => {
969-
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()]) })
970930
}
971931
crate::MathFunction::Round => {
972932
// TODO: Use `f{32,64}.round_ties_even()` when available on stable. This polyfill
@@ -994,35 +954,88 @@ impl<'a> ConstantEvaluator<'a> {
994954
Float::F32([e]) => Ok(Float::F32([(round_ties_even(e as f64) as f32)])),
995955
})
996956
}
997-
crate::MathFunction::Saturate => {
998-
component_wise_float!(self, span, [arg], |e| { Ok([e.clamp(0., 1.)]) })
957+
crate::MathFunction::Fract => {
958+
component_wise_float!(self, span, [arg], |e| { Ok([e.fract()]) })
999959
}
1000-
crate::MathFunction::Sign => {
1001-
component_wise_signed!(self, span, [arg], |e| { Ok([e.signum()]) })
960+
crate::MathFunction::Trunc => {
961+
component_wise_float!(self, span, [arg], |e| { Ok([e.trunc()]) })
1002962
}
1003-
crate::MathFunction::Sin => {
1004-
component_wise_float!(self, span, [arg], |e| { Ok([e.sin()]) })
963+
964+
// exponent
965+
crate::MathFunction::Exp => {
966+
component_wise_float!(self, span, [arg], |e| { Ok([e.exp()]) })
1005967
}
1006-
crate::MathFunction::Sinh => {
1007-
component_wise_float!(self, span, [arg], |e| { Ok([e.sinh()]) })
968+
crate::MathFunction::Exp2 => {
969+
component_wise_float!(self, span, [arg], |e| { Ok([e.exp2()]) })
1008970
}
1009-
crate::MathFunction::Tan => {
1010-
component_wise_float!(self, span, [arg], |e| { Ok([e.tan()]) })
971+
crate::MathFunction::Log => {
972+
component_wise_float!(self, span, [arg], |e| { Ok([e.ln()]) })
1011973
}
1012-
crate::MathFunction::Tanh => {
1013-
component_wise_float!(self, span, [arg], |e| { Ok([e.tanh()]) })
974+
crate::MathFunction::Log2 => {
975+
component_wise_float!(self, span, [arg], |e| { Ok([e.log2()]) })
1014976
}
1015-
crate::MathFunction::Sqrt => {
1016-
component_wise_float!(self, span, [arg], |e| { Ok([e.sqrt()]) })
977+
crate::MathFunction::Pow => {
978+
component_wise_float!(self, span, [arg, arg1.unwrap()], |e1, e2| {
979+
Ok([e1.powf(e2)])
980+
})
981+
}
982+
983+
// computational
984+
crate::MathFunction::Sign => {
985+
component_wise_signed!(self, span, [arg], |e| { Ok([e.signum()]) })
986+
}
987+
crate::MathFunction::Fma => {
988+
component_wise_float!(
989+
self,
990+
span,
991+
[arg, arg1.unwrap(), arg2.unwrap()],
992+
|e1, e2, e3| { Ok([e1.mul_add(e2, e3)]) }
993+
)
1017994
}
1018995
crate::MathFunction::Step => {
1019996
component_wise_float!(self, span, [arg, arg1.unwrap()], |edge, x| {
1020997
Ok([if edge <= x { 1.0 } else { 0.0 }])
1021998
})
1022999
}
1023-
crate::MathFunction::Trunc => {
1024-
component_wise_float!(self, span, [arg], |e| { Ok([e.trunc()]) })
1000+
crate::MathFunction::Sqrt => {
1001+
component_wise_float!(self, span, [arg], |e| { Ok([e.sqrt()]) })
10251002
}
1003+
crate::MathFunction::InverseSqrt => {
1004+
component_wise_float!(self, span, [arg], |e| { Ok([1. / e.sqrt()]) })
1005+
}
1006+
1007+
// bits
1008+
crate::MathFunction::CountTrailingZeros => {
1009+
component_wise_concrete_int!(self, span, [arg], |e| {
1010+
#[allow(clippy::useless_conversion)]
1011+
Ok([e
1012+
.trailing_zeros()
1013+
.try_into()
1014+
.expect("bit count overflowed 32 bits, somehow!?")])
1015+
})
1016+
}
1017+
crate::MathFunction::CountLeadingZeros => {
1018+
component_wise_concrete_int!(self, span, [arg], |e| {
1019+
#[allow(clippy::useless_conversion)]
1020+
Ok([e
1021+
.leading_zeros()
1022+
.try_into()
1023+
.expect("bit count overflowed 32 bits, somehow!?")])
1024+
})
1025+
}
1026+
crate::MathFunction::CountOneBits => {
1027+
component_wise_concrete_int!(self, span, [arg], |e| {
1028+
#[allow(clippy::useless_conversion)]
1029+
Ok([e
1030+
.count_ones()
1031+
.try_into()
1032+
.expect("bit count overflowed 32 bits, somehow!?")])
1033+
})
1034+
}
1035+
crate::MathFunction::ReverseBits => {
1036+
component_wise_concrete_int!(self, span, [arg], |e| { Ok([e.reverse_bits()]) })
1037+
}
1038+
10261039
fun => Err(ConstantEvaluatorError::NotImplemented(format!(
10271040
"{fun:?} built-in function"
10281041
))),

0 commit comments

Comments
 (0)