go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/int_generic.go (about)

     1  //go:build (!linux && !darwin && !dragonfly && !freebsd && !netbsd && !solaris) || (!amd64 && !arm64 && !mips64x && !ppc64 && !ppc64le && !loong64 && !s390x)
     2  
     3  package starlark
     4  
     5  // generic Int implementation as a union
     6  
     7  import "math/big"
     8  
     9  type intImpl struct {
    10  	// We use only the signed 32-bit range of small to ensure
    11  	// that small+small and small*small do not overflow.
    12  	small_ int64    // minint32 <= small <= maxint32
    13  	big_   *big.Int // big != nil <=> value is not representable as int32
    14  }
    15  
    16  // --- low-level accessors ---
    17  
    18  // get returns the small and big components of the Int.
    19  // small is defined only if big is nil.
    20  // small is sign-extended to 64 bits for ease of subsequent arithmetic.
    21  func (i Int) get() (small int64, big *big.Int) {
    22  	return i.impl.small_, i.impl.big_
    23  }
    24  
    25  // Precondition: math.MinInt32 <= x && x <= math.MaxInt32
    26  func makeSmallInt(x int64) Int {
    27  	return Int{intImpl{small_: x}}
    28  }
    29  
    30  // Precondition: x cannot be represented as int32.
    31  func makeBigInt(x *big.Int) Int {
    32  	return Int{intImpl{big_: x}}
    33  }