5252import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
5353import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
5454import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
55+ import com .oracle .graal .python .nodes .util .*;
5556import com .oracle .graal .python .runtime .exception .PException ;
5657import com .oracle .graal .python .runtime .exception .PythonErrorType ;
5758import static com .oracle .graal .python .runtime .exception .PythonErrorType .NotImplementedError ;
@@ -86,97 +87,6 @@ public MathModuleBuiltins() {
8687 builtinConstants .put ("nan" , Double .NaN );
8788 }
8889
89- @ TypeSystemReference (PythonArithmeticTypes .class )
90- @ ImportStatic (MathGuards .class )
91- static abstract class ConvertToFloatNode extends PBaseNode {
92-
93- @ Child private LookupAndCallUnaryNode callFloatNode ;
94-
95- abstract double execute (Object x );
96-
97- public static ConvertToFloatNode create () {
98- return MathModuleBuiltinsFactory .ConvertToFloatNodeGen .create ();
99- }
100-
101- @ Specialization
102- public double toDouble (long x ) {
103- return x ;
104- }
105-
106- @ Specialization
107- public double toDouble (PInt x ) {
108- return x .doubleValue ();
109- }
110-
111- @ Specialization
112- public double toDouble (double x ) {
113- return x ;
114- }
115-
116- @ Specialization (guards = "!isNumber(x)" )
117- public double toDouble (Object x ) {
118- if (callFloatNode == null ) {
119- CompilerDirectives .transferToInterpreterAndInvalidate ();
120- callFloatNode = insert (LookupAndCallUnaryNode .create (SpecialMethodNames .__FLOAT__ ));
121- }
122- Object result = callFloatNode .executeObject (x );
123- if (result == PNone .NO_VALUE ) {
124- throw raise (TypeError , "must be real number, not %p" , x );
125- }
126- if (result instanceof PFloat ) {
127- return ((PFloat ) result ).getValue ();
128- }
129- if (result instanceof Float || result instanceof Double ) {
130- return (double ) result ;
131- }
132- throw raise (TypeError , "%p.__float__ returned non-float (type %p)" , x , result );
133- }
134- }
135-
136- @ TypeSystemReference (PythonArithmeticTypes .class )
137- @ ImportStatic (MathGuards .class )
138- static abstract class ConvertToIntNode extends PBaseNode {
139-
140- @ Child private LookupAndCallUnaryNode callIndexNode ;
141-
142- abstract Object execute (Object x );
143-
144- public static ConvertToIntNode create () {
145- return MathModuleBuiltinsFactory .ConvertToIntNodeGen .create ();
146- }
147-
148- @ Specialization
149- public long toInt (long x ) {
150- return x ;
151- }
152-
153- @ Specialization
154- public PInt toInt (PInt x ) {
155- return x ;
156- }
157-
158- @ Specialization
159- public long toInt (double x ) {
160- throw raise (TypeError , "'float' object cannot be interpreted as an integer" );
161- }
162-
163- @ Specialization (guards = "!isNumber(x)" )
164- public Object toInt (Object x ) {
165- if (callIndexNode == null ) {
166- CompilerDirectives .transferToInterpreterAndInvalidate ();
167- callIndexNode = insert (LookupAndCallUnaryNode .create (SpecialMethodNames .__INDEX__ ));
168- }
169- Object result = callIndexNode .executeObject (x );
170- if (result == PNone .NONE ) {
171- throw raise (TypeError , "'%p' object cannot be interpreted as an integer" , x );
172- }
173- if (!PGuards .isInteger (result ) && !PGuards .isPInt (result ) && !(result instanceof Boolean )) {
174- throw raise (TypeError , " __index__ returned non-int (type %p)" , result );
175- }
176- return result ;
177- }
178- }
179-
18090 public abstract static class MathUnaryBuiltinNode extends PythonUnaryBuiltinNode {
18191
18292 public void checkMathRangeError (boolean con ) {
@@ -219,7 +129,7 @@ public double doPI(PInt value) {
219129
220130 @ Specialization (guards = "!isNumber(value)" )
221131 public double doGeneral (Object value ,
222- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
132+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
223133 return count (convertToFloat .execute (value ));
224134 }
225135 }
@@ -353,7 +263,7 @@ public Object ceil(PInt value,
353263
354264 @ Specialization (guards = {"!isNumber(value)" })
355265 public Object ceil (Object value ,
356- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ,
266+ @ Cached ("create()" ) GetDoubleNode convertToFloat ,
357267 @ Cached ("create(__CEIL__)" ) LookupAndCallUnaryNode dispatchCeil ) {
358268 Object result = dispatchCeil .executeObject (value );
359269 if (result == PNone .NO_VALUE ) {
@@ -835,7 +745,7 @@ public PTuple frexpPI(PInt value) {
835745
836746 @ Specialization (guards = "!isNumber(value)" )
837747 public PTuple frexpO (Object value ,
838- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
748+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
839749 return frexpD (convertToFloat .execute (value ));
840750 }
841751 }
@@ -862,7 +772,7 @@ public boolean isNan(double value) {
862772
863773 @ Specialization (guards = "!isNumber(value)" )
864774 public boolean isinf (Object value ,
865- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
775+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
866776 return isNan (convertToFloat .execute (value ));
867777 }
868778 }
@@ -1052,7 +962,7 @@ public PTuple frexpPI(PInt value) {
1052962
1053963 @ Specialization (guards = "!isNumber(value)" )
1054964 public PTuple frexpO (Object value ,
1055- @ Cached ("create()" ) ConvertToFloatNode convertToFloatNode ) {
965+ @ Cached ("create()" ) GetDoubleNode convertToFloatNode ) {
1056966 return modfD (convertToFloatNode .execute (value ));
1057967 }
1058968 }
@@ -1077,7 +987,7 @@ public abstract static class FsumNode extends PythonUnaryBuiltinNode {
1077987 public double doIt (Object iterable ,
1078988 @ Cached ("create()" ) GetIteratorNode getIterator ,
1079989 @ Cached ("create(__NEXT__)" ) LookupAndCallUnaryNode next ,
1080- @ Cached ("create()" ) ConvertToFloatNode toFloat ,
990+ @ Cached ("create()" ) GetDoubleNode toFloat ,
1081991 @ Cached ("createBinaryProfile()" ) ConditionProfile stopProfile ) {
1082992 Object iterator = getIterator .executeWith (iterable );
1083993 double x , y , t , hi , lo = 0 , yr , inf_sum = 0 , special_sum = 0 , sum ;
@@ -1238,8 +1148,8 @@ int gcd(PInt x, double y) {
12381148
12391149 @ Specialization (guards = "!isNumber(x) || !isNumber(y)" )
12401150 Object gcd (Object x , Object y ,
1241- @ Cached ("create()" ) ConvertToIntNode xConvert ,
1242- @ Cached ("create()" ) ConvertToIntNode yConvert ,
1151+ @ Cached ("create()" ) GetIntNode xConvert ,
1152+ @ Cached ("create()" ) GetIntNode yConvert ,
12431153 @ Cached ("create()" ) GcdNode recursiveNode ) {
12441154 Object xValue = xConvert .execute (x );
12451155 Object yValue = yConvert .execute (y );
@@ -1434,7 +1344,7 @@ public boolean isfinite(double value) {
14341344
14351345 @ Specialization (guards = "!isNumber(value)" )
14361346 public boolean isinf (Object value ,
1437- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
1347+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
14381348 return isfinite (convertToFloat .execute (value ));
14391349 }
14401350 }
@@ -1462,7 +1372,7 @@ public boolean isinf(double value) {
14621372
14631373 @ Specialization (guards = "!isNumber(value)" )
14641374 public boolean isinf (Object value ,
1465- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
1375+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
14661376 return isinf (convertToFloat .execute (value ));
14671377 }
14681378 }
@@ -1473,22 +1383,22 @@ public boolean isinf(Object value,
14731383 @ GenerateNodeFactory
14741384 public abstract static class LogNode extends PythonBinaryBuiltinNode {
14751385
1476- @ Child private ConvertToFloatNode valueConvertNode ;
1477- @ Child private ConvertToFloatNode baseConvertNode ;
1386+ @ Child private GetDoubleNode valueConvertNode ;
1387+ @ Child private GetDoubleNode baseConvertNode ;
14781388 @ Child private LogNode recLogNode ;
14791389
1480- private ConvertToFloatNode getValueConvertNode () {
1390+ private GetDoubleNode getValueConvertNode () {
14811391 if (valueConvertNode == null ) {
14821392 CompilerDirectives .transferToInterpreterAndInvalidate ();
1483- valueConvertNode = insert (ConvertToFloatNode .create ());
1393+ valueConvertNode = insert (GetDoubleNode .create ());
14841394 }
14851395 return valueConvertNode ;
14861396 }
14871397
1488- private ConvertToFloatNode getBaseConvertNode () {
1398+ private GetDoubleNode getBaseConvertNode () {
14891399 if (baseConvertNode == null ) {
14901400 CompilerDirectives .transferToInterpreterAndInvalidate ();
1491- baseConvertNode = insert (ConvertToFloatNode .create ());
1401+ baseConvertNode = insert (GetDoubleNode .create ());
14921402 }
14931403 return baseConvertNode ;
14941404 }
@@ -1895,8 +1805,8 @@ public abstract static class PowNode extends PythonBinaryBuiltinNode {
18951805
18961806 @ Specialization (guards = {"!isNumber(left)||!isNumber(right)" })
18971807 double pow (Object left , Object right ,
1898- @ Cached ("create()" ) ConvertToFloatNode convertLeftFloat ,
1899- @ Cached ("create()" ) ConvertToFloatNode convertRightFloat ) {
1808+ @ Cached ("create()" ) GetDoubleNode convertLeftFloat ,
1809+ @ Cached ("create()" ) GetDoubleNode convertRightFloat ) {
19001810 return pow (convertLeftFloat .execute (left ), convertRightFloat .execute (right ));
19011811 }
19021812 }
@@ -1969,8 +1879,8 @@ public abstract static class Atan2Node extends PythonBinaryBuiltinNode {
19691879
19701880 @ Specialization (guards = "!isNumber(left) || !isNumber(right)" )
19711881 double atan2 (Object left , Object right ,
1972- @ Cached ("create()" ) ConvertToFloatNode convertLeftFloat ,
1973- @ Cached ("create()" ) ConvertToFloatNode convertRightFloat ) {
1882+ @ Cached ("create()" ) GetDoubleNode convertLeftFloat ,
1883+ @ Cached ("create()" ) GetDoubleNode convertRightFloat ) {
19741884 return atan2DD (convertLeftFloat .execute (left ), convertRightFloat .execute (right ));
19751885 }
19761886 }
@@ -2057,8 +1967,8 @@ public double hypotPIL(PInt x, long y) {
20571967
20581968 @ Specialization (guards = "!isNumber(objectX) || !isNumber(objectY)" )
20591969 public double hypotOO (Object objectX , Object objectY ,
2060- @ Cached ("create()" ) ConvertToFloatNode xConvertNode ,
2061- @ Cached ("create()" ) ConvertToFloatNode yConvertNode ) {
1970+ @ Cached ("create()" ) GetDoubleNode xConvertNode ,
1971+ @ Cached ("create()" ) GetDoubleNode yConvertNode ) {
20621972 return hypotDD (xConvertNode .execute (objectX ), yConvertNode .execute (objectY ));
20631973 }
20641974 }
0 commit comments