52
52
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
53
53
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
54
54
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
55
+ import com .oracle .graal .python .nodes .util .*;
55
56
import com .oracle .graal .python .runtime .exception .PException ;
56
57
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
57
58
import static com .oracle .graal .python .runtime .exception .PythonErrorType .NotImplementedError ;
@@ -86,97 +87,6 @@ public MathModuleBuiltins() {
86
87
builtinConstants .put ("nan" , Double .NaN );
87
88
}
88
89
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
-
180
90
public abstract static class MathUnaryBuiltinNode extends PythonUnaryBuiltinNode {
181
91
182
92
public void checkMathRangeError (boolean con ) {
@@ -219,7 +129,7 @@ public double doPI(PInt value) {
219
129
220
130
@ Specialization (guards = "!isNumber(value)" )
221
131
public double doGeneral (Object value ,
222
- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
132
+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
223
133
return count (convertToFloat .execute (value ));
224
134
}
225
135
}
@@ -353,7 +263,7 @@ public Object ceil(PInt value,
353
263
354
264
@ Specialization (guards = {"!isNumber(value)" })
355
265
public Object ceil (Object value ,
356
- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ,
266
+ @ Cached ("create()" ) GetDoubleNode convertToFloat ,
357
267
@ Cached ("create(__CEIL__)" ) LookupAndCallUnaryNode dispatchCeil ) {
358
268
Object result = dispatchCeil .executeObject (value );
359
269
if (result == PNone .NO_VALUE ) {
@@ -835,7 +745,7 @@ public PTuple frexpPI(PInt value) {
835
745
836
746
@ Specialization (guards = "!isNumber(value)" )
837
747
public PTuple frexpO (Object value ,
838
- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
748
+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
839
749
return frexpD (convertToFloat .execute (value ));
840
750
}
841
751
}
@@ -862,7 +772,7 @@ public boolean isNan(double value) {
862
772
863
773
@ Specialization (guards = "!isNumber(value)" )
864
774
public boolean isinf (Object value ,
865
- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
775
+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
866
776
return isNan (convertToFloat .execute (value ));
867
777
}
868
778
}
@@ -1052,7 +962,7 @@ public PTuple frexpPI(PInt value) {
1052
962
1053
963
@ Specialization (guards = "!isNumber(value)" )
1054
964
public PTuple frexpO (Object value ,
1055
- @ Cached ("create()" ) ConvertToFloatNode convertToFloatNode ) {
965
+ @ Cached ("create()" ) GetDoubleNode convertToFloatNode ) {
1056
966
return modfD (convertToFloatNode .execute (value ));
1057
967
}
1058
968
}
@@ -1077,7 +987,7 @@ public abstract static class FsumNode extends PythonUnaryBuiltinNode {
1077
987
public double doIt (Object iterable ,
1078
988
@ Cached ("create()" ) GetIteratorNode getIterator ,
1079
989
@ Cached ("create(__NEXT__)" ) LookupAndCallUnaryNode next ,
1080
- @ Cached ("create()" ) ConvertToFloatNode toFloat ,
990
+ @ Cached ("create()" ) GetDoubleNode toFloat ,
1081
991
@ Cached ("createBinaryProfile()" ) ConditionProfile stopProfile ) {
1082
992
Object iterator = getIterator .executeWith (iterable );
1083
993
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) {
1238
1148
1239
1149
@ Specialization (guards = "!isNumber(x) || !isNumber(y)" )
1240
1150
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 ,
1243
1153
@ Cached ("create()" ) GcdNode recursiveNode ) {
1244
1154
Object xValue = xConvert .execute (x );
1245
1155
Object yValue = yConvert .execute (y );
@@ -1434,7 +1344,7 @@ public boolean isfinite(double value) {
1434
1344
1435
1345
@ Specialization (guards = "!isNumber(value)" )
1436
1346
public boolean isinf (Object value ,
1437
- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
1347
+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
1438
1348
return isfinite (convertToFloat .execute (value ));
1439
1349
}
1440
1350
}
@@ -1462,7 +1372,7 @@ public boolean isinf(double value) {
1462
1372
1463
1373
@ Specialization (guards = "!isNumber(value)" )
1464
1374
public boolean isinf (Object value ,
1465
- @ Cached ("create()" ) ConvertToFloatNode convertToFloat ) {
1375
+ @ Cached ("create()" ) GetDoubleNode convertToFloat ) {
1466
1376
return isinf (convertToFloat .execute (value ));
1467
1377
}
1468
1378
}
@@ -1473,22 +1383,22 @@ public boolean isinf(Object value,
1473
1383
@ GenerateNodeFactory
1474
1384
public abstract static class LogNode extends PythonBinaryBuiltinNode {
1475
1385
1476
- @ Child private ConvertToFloatNode valueConvertNode ;
1477
- @ Child private ConvertToFloatNode baseConvertNode ;
1386
+ @ Child private GetDoubleNode valueConvertNode ;
1387
+ @ Child private GetDoubleNode baseConvertNode ;
1478
1388
@ Child private LogNode recLogNode ;
1479
1389
1480
- private ConvertToFloatNode getValueConvertNode () {
1390
+ private GetDoubleNode getValueConvertNode () {
1481
1391
if (valueConvertNode == null ) {
1482
1392
CompilerDirectives .transferToInterpreterAndInvalidate ();
1483
- valueConvertNode = insert (ConvertToFloatNode .create ());
1393
+ valueConvertNode = insert (GetDoubleNode .create ());
1484
1394
}
1485
1395
return valueConvertNode ;
1486
1396
}
1487
1397
1488
- private ConvertToFloatNode getBaseConvertNode () {
1398
+ private GetDoubleNode getBaseConvertNode () {
1489
1399
if (baseConvertNode == null ) {
1490
1400
CompilerDirectives .transferToInterpreterAndInvalidate ();
1491
- baseConvertNode = insert (ConvertToFloatNode .create ());
1401
+ baseConvertNode = insert (GetDoubleNode .create ());
1492
1402
}
1493
1403
return baseConvertNode ;
1494
1404
}
@@ -1895,8 +1805,8 @@ public abstract static class PowNode extends PythonBinaryBuiltinNode {
1895
1805
1896
1806
@ Specialization (guards = {"!isNumber(left)||!isNumber(right)" })
1897
1807
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 ) {
1900
1810
return pow (convertLeftFloat .execute (left ), convertRightFloat .execute (right ));
1901
1811
}
1902
1812
}
@@ -1969,8 +1879,8 @@ public abstract static class Atan2Node extends PythonBinaryBuiltinNode {
1969
1879
1970
1880
@ Specialization (guards = "!isNumber(left) || !isNumber(right)" )
1971
1881
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 ) {
1974
1884
return atan2DD (convertLeftFloat .execute (left ), convertRightFloat .execute (right ));
1975
1885
}
1976
1886
}
@@ -2057,8 +1967,8 @@ public double hypotPIL(PInt x, long y) {
2057
1967
2058
1968
@ Specialization (guards = "!isNumber(objectX) || !isNumber(objectY)" )
2059
1969
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 ) {
2062
1972
return hypotDD (xConvertNode .execute (objectX ), yConvertNode .execute (objectY ));
2063
1973
}
2064
1974
}
0 commit comments