|
84 | 84 | import com.oracle.truffle.api.dsl.NodeFactory;
|
85 | 85 | import com.oracle.truffle.api.dsl.Specialization;
|
86 | 86 | import com.oracle.truffle.api.dsl.TypeSystemReference;
|
| 87 | +import com.oracle.truffle.api.profiles.ConditionProfile; |
87 | 88 |
|
88 | 89 | @CoreFunctions(extendClasses = PFloat.class)
|
89 | 90 | public final class FloatBuiltins extends PythonBuiltins {
|
@@ -929,25 +930,38 @@ static abstract class ConjugateNode extends RealNode {
|
929 | 930 | @GenerateNodeFactory
|
930 | 931 | abstract static class TruncNode extends PythonUnaryBuiltinNode {
|
931 | 932 |
|
932 |
| - private int truncate(double value) { |
| 933 | + @TruffleBoundary |
| 934 | + protected static int truncate(double value) { |
933 | 935 | return (int) (value < 0 ? Math.ceil(value) : Math.floor(value));
|
934 | 936 | }
|
935 | 937 |
|
936 | 938 | @Specialization
|
937 |
| - int trunc(double value) { |
| 939 | + int trunc(double value, |
| 940 | + @Cached("createBinaryProfile()") ConditionProfile nanProfile, |
| 941 | + @Cached("createBinaryProfile()") ConditionProfile infProfile) { |
| 942 | + if (nanProfile.profile(Double.isNaN(value))) { |
| 943 | + throw raise(PythonErrorType.ValueError, "cannot convert float NaN to integer"); |
| 944 | + } |
| 945 | + if (infProfile.profile(Double.isInfinite(value))) { |
| 946 | + throw raise(PythonErrorType.OverflowError, "cannot convert float infinity to integer"); |
| 947 | + } |
938 | 948 | return truncate(value);
|
939 | 949 | }
|
940 | 950 |
|
941 | 951 | @Specialization
|
942 |
| - int trunc(PFloat pValue) { |
| 952 | + int trunc(PFloat pValue, |
| 953 | + @Cached("createBinaryProfile()") ConditionProfile nanProfile, |
| 954 | + @Cached("createBinaryProfile()") ConditionProfile infProfile) { |
943 | 955 | double value = pValue.getValue();
|
944 |
| - if (value == Double.NaN) { |
945 |
| - raise(PythonErrorType.ValueError, "cannot convert float NaN to integer"); |
946 |
| - } else if (value == Double.NEGATIVE_INFINITY || value == Double.POSITIVE_INFINITY) { |
947 |
| - raise(PythonErrorType.OverflowError, "cannot convert float infinity to integer"); |
| 956 | + if (nanProfile.profile(Double.isNaN(value))) { |
| 957 | + throw raise(PythonErrorType.ValueError, "cannot convert float NaN to integer"); |
| 958 | + } |
| 959 | + if (infProfile.profile(Double.isInfinite(value))) { |
| 960 | + throw raise(PythonErrorType.OverflowError, "cannot convert float infinity to integer"); |
948 | 961 | }
|
949 | 962 | return truncate(value);
|
950 | 963 | }
|
| 964 | + |
951 | 965 | }
|
952 | 966 |
|
953 | 967 | @Builtin(name = __GETFORMAT__, fixedNumOfArguments = 2)
|
|
0 commit comments