@@ -837,7 +837,9 @@ impl<'a> ConstantEvaluator<'a> {
837
837
) ) ;
838
838
}
839
839
840
+ // NOTE: We try to match the declaration order of `MathFunction` here.
840
841
match fun {
842
+ // comparison
841
843
crate :: MathFunction :: Abs => {
842
844
component_wise_scalar ( self , span, [ arg] , |args| match args {
843
845
Scalar :: AbstractFloat ( [ e] ) => Ok ( Scalar :: AbstractFloat ( [ e. abs ( ) ] ) ) ,
@@ -847,31 +849,15 @@ impl<'a> ConstantEvaluator<'a> {
847
849
Scalar :: U32 ( [ e] ) => Ok ( Scalar :: U32 ( [ e] ) ) , // TODO: just re-use the expression, ezpz
848
850
} )
849
851
}
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) ] )
871
855
} )
872
856
}
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
+ } )
875
861
}
876
862
crate :: MathFunction :: Clamp => {
877
863
component_wise_scalar ! (
@@ -887,90 +873,60 @@ impl<'a> ConstantEvaluator<'a> {
887
873
}
888
874
)
889
875
}
876
+ crate :: MathFunction :: Saturate => {
877
+ component_wise_float ! ( self , span, [ arg] , |e| { Ok ( [ e. clamp( 0. , 1. ) ] ) } )
878
+ }
879
+
880
+ // trigonometry
890
881
crate :: MathFunction :: Cos => {
891
882
component_wise_float ! ( self , span, [ arg] , |e| { Ok ( [ e. cos( ) ] ) } )
892
883
}
893
884
crate :: MathFunction :: Cosh => {
894
885
component_wise_float ! ( self , span, [ arg] , |e| { Ok ( [ e. cosh( ) ] ) } )
895
886
}
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( ) ] ) } )
928
889
}
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 ( ) ] ) } )
931
892
}
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 ( ) ] ) } )
934
895
}
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( ) ] ) } )
941
898
}
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( ) ] ) } )
949
901
}
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 ( ) ] ) } )
952
904
}
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 ( ) ] ) } )
955
907
}
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 ( ) ] ) } )
958
910
}
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( ) ] ) } )
963
913
}
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( ) ] ) } )
968
916
}
969
917
crate :: MathFunction :: Radians => {
970
918
component_wise_float ! ( self , span, [ arg] , |e1| { Ok ( [ e1. to_radians( ) ] ) } )
971
919
}
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( ) ] ) } )
974
930
}
975
931
crate :: MathFunction :: Round => {
976
932
// TODO: Use `f{32,64}.round_ties_even()` when available on stable. This polyfill
@@ -998,35 +954,92 @@ impl<'a> ConstantEvaluator<'a> {
998
954
Float :: F32 ( [ e] ) => Ok ( Float :: F32 ( [ ( round_ties_even ( e as f64 ) as f32 ) ] ) ) ,
999
955
} )
1000
956
}
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
+ } )
1003
963
}
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 ( ) ] ) } )
1006
966
}
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( ) ] ) } )
1009
971
}
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 ( ) ] ) } )
1012
974
}
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 ( ) ] ) } )
1015
977
}
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 ( ) ] ) } )
1018
980
}
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
+ )
1021
998
}
1022
999
crate :: MathFunction :: Step => {
1023
1000
component_wise_float ! ( self , span, [ arg, arg1. unwrap( ) ] , |edge, x| {
1024
1001
Ok ( [ if edge <= x { 1.0 } else { 0.0 } ] )
1025
1002
} )
1026
1003
}
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( ) ] ) } )
1029
1009
}
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
+
1030
1043
fun => Err ( ConstantEvaluatorError :: NotImplemented ( format ! (
1031
1044
"{fun:?} built-in function"
1032
1045
) ) ) ,
0 commit comments