@@ -782,60 +782,75 @@ protected static String getUTF32Name(int byteorder) {
782
782
@ GenerateNodeFactory
783
783
abstract static class TrufflePInt_AsPrimitive extends NativeBuiltin {
784
784
785
- @ Specialization
786
- Object doPInt (int obj , int signed , long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
787
- if (targetTypeSize == 4 ) {
785
+ @ Specialization (guards = "targetTypeSize == 4" )
786
+ int doInt4 (int obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
787
+ return obj ;
788
+ }
789
+
790
+ @ Specialization (guards = "targetTypeSize == 8" )
791
+ long doInt8 (int obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
792
+ if (signed != 0 ) {
788
793
return obj ;
789
- } else if (targetTypeSize == 8 ) {
790
- if (signed != 0 ) {
791
- return (long ) obj ;
792
- } else {
793
- return obj & 0xFFFFFFFFL ;
794
- }
795
794
} else {
796
- return raiseNative (- 1 , PythonErrorType . SystemError , "Unsupported target size: %d" , targetTypeSize ) ;
795
+ return obj & 0xFFFFFFFFL ;
797
796
}
798
797
}
799
798
800
- @ Specialization
801
- Object doPInt (long obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , String targetTypeName ) {
802
- if (targetTypeSize == 4 ) {
799
+ @ Specialization (guards = {"targetTypeSize != 4" , "targetTypeSize != 8" })
800
+ int doIntOther (@ SuppressWarnings ("unused" ) int obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
801
+ return raiseNative (-1 , PythonErrorType .SystemError , "Unsupported target size: %d" , targetTypeSize );
802
+ }
803
+
804
+ @ Specialization (guards = "targetTypeSize == 4" )
805
+ int doLong4 (@ SuppressWarnings ("unused" ) long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , String targetTypeName ) {
806
+ return raiseNative (-1 , PythonErrorType .OverflowError , "Python int too large to convert to C %s" , targetTypeName );
807
+ }
808
+
809
+ @ Specialization (guards = "targetTypeSize == 8" )
810
+ long doLong8 (long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
811
+ return obj ;
812
+ }
813
+
814
+ @ Specialization (guards = {"targetTypeSize != 4" , "targetTypeSize != 8" })
815
+ int doPInt (@ SuppressWarnings ("unused" ) long obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
816
+ return raiseNative (-1 , PythonErrorType .SystemError , "Unsupported target size: %d" , targetTypeSize );
817
+ }
818
+
819
+ @ Specialization (guards = "targetTypeSize == 4" )
820
+ int doPInt4 (PInt obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , String targetTypeName ) {
821
+ try {
822
+ if (signed != 0 ) {
823
+ return obj .intValueExact ();
824
+ } else if (obj .bitCount () <= 32 ) {
825
+ return obj .intValue ();
826
+ } else {
827
+ throw new ArithmeticException ();
828
+ }
829
+ } catch (ArithmeticException e ) {
803
830
return raiseNative (-1 , PythonErrorType .OverflowError , "Python int too large to convert to C %s" , targetTypeName );
804
- } else if (targetTypeSize == 8 ) {
805
- return obj ;
806
- } else {
807
- return raiseNative (-1 , PythonErrorType .SystemError , "Unsupported target size: %d" , targetTypeSize );
808
831
}
809
832
}
810
833
811
- @ Specialization
812
- Object doPInt (PInt obj , int signed , long targetTypeSize , String targetTypeName ) {
834
+ @ Specialization ( guards = "targetTypeSize == 8" )
835
+ long doPInt8 (PInt obj , int signed , @ SuppressWarnings ( "unused" ) long targetTypeSize , String targetTypeName ) {
813
836
try {
814
- if (targetTypeSize == 4 ) {
815
- if (signed != 0 ) {
816
- return obj .intValueExact ();
817
- } else if (obj .bitCount () <= 32 ) {
818
- return obj .intValue ();
819
- } else {
820
- throw new ArithmeticException ();
821
- }
822
- } else if (targetTypeSize == 8 ) {
823
- if (signed != 0 ) {
824
- return obj .longValueExact ();
825
- } else if (obj .bitCount () <= 64 ) {
826
- return obj .longValue ();
827
- } else {
828
- throw new ArithmeticException ();
829
- }
837
+ if (signed != 0 ) {
838
+ return obj .longValueExact ();
839
+ } else if (obj .bitCount () <= 64 ) {
840
+ return obj .longValue ();
830
841
} else {
831
- return raiseNative (- 1 , PythonErrorType . SystemError , "Unsupported target size: %d" , targetTypeSize );
842
+ throw new ArithmeticException ( );
832
843
}
833
-
834
844
} catch (ArithmeticException e ) {
835
845
return raiseNative (-1 , PythonErrorType .OverflowError , "Python int too large to convert to C %s" , targetTypeName );
836
846
}
837
847
}
838
848
849
+ @ Specialization (guards = {"targetTypeSize != 4" , "targetTypeSize != 8" })
850
+ int doPInt (@ SuppressWarnings ("unused" ) PInt obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , @ SuppressWarnings ("unused" ) String targetTypeName ) {
851
+ return raiseNative (-1 , PythonErrorType .SystemError , "Unsupported target size: %d" , targetTypeSize );
852
+ }
853
+
839
854
@ Specialization (guards = {"!isInteger(obj)" , "!isPInt(obj)" })
840
855
@ SuppressWarnings ("unused" )
841
856
int doGeneric (Object obj , boolean signed , int targetTypeSize , String targetTypeName ) {
0 commit comments