@@ -611,10 +611,12 @@ public static StarlarkInt shiftLeft(StarlarkInt x, StarlarkInt y) throws EvalExc
611611
612612 /** Returns x ^ y. */
613613 public static StarlarkInt xor (StarlarkInt x , StarlarkInt y ) {
614- if ( x instanceof Int32 && y instanceof Int32 ) {
615- long xl = (( Int32 ) x ). v ;
616- long yl = (( Int32 ) y ). v ;
614+ try {
615+ long xl = x . toLongFast () ;
616+ long yl = y . toLongFast () ;
617617 return StarlarkInt .of (xl ^ yl );
618+ } catch (Overflow unused ) {
619+ /* fall through */
618620 }
619621
620622 BigInteger xbig = x .toBigInteger ();
@@ -625,10 +627,12 @@ public static StarlarkInt xor(StarlarkInt x, StarlarkInt y) {
625627
626628 /** Returns x | y. */
627629 public static StarlarkInt or (StarlarkInt x , StarlarkInt y ) {
628- if ( x instanceof Int32 && y instanceof Int32 ) {
629- long xl = (( Int32 ) x ). v ;
630- long yl = (( Int32 ) y ). v ;
630+ try {
631+ long xl = x . toLongFast () ;
632+ long yl = y . toLongFast () ;
631633 return StarlarkInt .of (xl | yl );
634+ } catch (Overflow unused ) {
635+ /* fall through */
632636 }
633637
634638 BigInteger xbig = x .toBigInteger ();
@@ -639,10 +643,12 @@ public static StarlarkInt or(StarlarkInt x, StarlarkInt y) {
639643
640644 /** Returns x & y. */
641645 public static StarlarkInt and (StarlarkInt x , StarlarkInt y ) {
642- if ( x instanceof Int32 && y instanceof Int32 ) {
643- long xl = (( Int32 ) x ). v ;
644- long yl = (( Int32 ) y ). v ;
646+ try {
647+ long xl = x . toLongFast () ;
648+ long yl = y . toLongFast () ;
645649 return StarlarkInt .of (xl & yl );
650+ } catch (Overflow unused ) {
651+ /* fall through */
646652 }
647653
648654 BigInteger xbig = x .toBigInteger ();
@@ -653,9 +659,11 @@ public static StarlarkInt and(StarlarkInt x, StarlarkInt y) {
653659
654660 /** Returns ~x. */
655661 public static StarlarkInt bitnot (StarlarkInt x ) {
656- if ( x instanceof Int32 ) {
657- long xl = (( Int32 ) x ). v ;
662+ try {
663+ long xl = x . toLongFast () ;
658664 return StarlarkInt .of (~xl );
665+ } catch (Overflow unused ) {
666+ /* fall through */
659667 }
660668
661669 BigInteger xbig = x .toBigInteger ();
0 commit comments