// ToVarInt converts a base-128 byte array to a int64
763233
func ToVarInt(b []byte) (int64, error) {
	if len(b) < 1 {
		return 0, ErrNumberFormat
	}
	var ux uint64
	for i := range b {
		ux += uint64((b[i] & 0x7F)) << uint(7*i)
		if b[i] < 128 {
			break
		}
	}
	x := int64(ux >> 1)
	if ux&1 != 0 {
		x = ^x
	}
	return x, nil
}
1265107
func (p *TCompactProtocol) bytesToInt64(b []byte) int64 {
	return int64(binary.LittleEndian.Uint64(b))
}
809144
func bytesToInt(b []byte) int {
	result := 0
	for _, v := range b {
		result = result<<8 + int(v)
	}
	return result
}
105690
func WriteInt(w io.ByteWriter, val int64) (int, error) {
	var inv byte
	if val < 0 {
		inv = 0xff
	}
	mag := uint64(val)
	if inv != 0 {
		mag = -mag
	}
	return writeSignMag(w, mag, inv)
}
489880
func toInt32(value Value) int32 {
	{
		switch value := value.value.(type) {
		case int8:
			return int32(value)
		case int16:
			return int32(value)
		case int32:
			return value
		}
	}
	floatValue := value.float64()
	if math.IsNaN(floatValue) || math.IsInf(floatValue, 0) {
		return 0
	}
	if floatValue == 0 { // This will work for +0 & -0
		return 0
	}
	remainder := math.Mod(floatValue, float_2_32)
	if remainder > 0 {
		remainder = math.Floor(remainder)
	} else {
		remainder = math.Ceil(remainder) + float_2_32
	}
	if remainder > float_2_31 {
		return int32(remainder - float_2_32)
	}
	return int32(remainder)
}
883985
func (v *Value) setInt(i int64) {
	v.vType = typeInt
	v.value = i
}
132384
func EncodeUvarint(out io.Writer, n int) error {
	var buf [16]byte
	var err error
	i := 0
	if (n & 0xffffff80) != 0 {
		buf[i] = byte((n | 0x80) & 0xff)
		i++
		n >>= 7
		if (n & 0xffffff80) != 0 {
			buf[i] = byte((n | 0x80) & 0xff)
			i++
			n >>= 7
			if (n & 0xffffff80) != 0 {
				buf[i] = byte((n | 0x80) & 0xff)
				i++
				n >>= 7
				if (n & 0xffffff80) != 0 {
					buf[i] = byte((n | 0x80) & 0xff)
					i++
					n >>= 7
				}
			}
		}
	}
	buf[i] = byte(n)
	_, err = out.Write(buf[:i+1])
	return err
}
1304584
func valToInt64(i interface{}) (int64, bool) {
	switch x := i.(type) {
	case float32:
		return int64(x), true
	case float64:
		return int64(x), true
	case uint8:
		return int64(x), true
	case uint16:
		return int64(x), true
	case uint32:
		return int64(x), true
	case uint64:
		if x > math.MaxInt64 {
			return 0, false
		}
		return int64(x), true
	case int8:
		return int64(x), true
	case int16:
		return int64(x), true
	case int32:
		return int64(x), true
	case int64:
		return int64(x), true
	case int:
		return int64(x), true
	case uint:
		if uint64(x) > math.MaxInt64 {
			return 0, false
		}
		return int64(x), true
	case string:
		if len(x) > 0 {
			if iv, err := strconv.ParseInt(x, 10, 64); err == nil {
				return iv, true
			}
			if iv, err := strconv.ParseFloat(x, 64); err == nil {
				return valToInt64(iv)
			}
		}
	case []byte:
		if len(x) > 0 {
			if iv, err := strconv.ParseInt(string(x), 10, 64); err == nil {
				return iv, true
			}
			if iv, err := strconv.ParseFloat(string(x), 64); err == nil {
				return valToInt64(iv)
			}
		}
	case json.RawMessage:
		if len(x) > 0 {
			if iv, err := strconv.ParseInt(string(x), 10, 64); err == nil {
				return iv, true
			}
			if iv, err := strconv.ParseFloat(string(x), 64); err == nil {
				return valToInt64(iv)
			}
		}
	}
	return 0, false
}
29631
func (d *Datum) ToInt64(sc *stmtctx.StatementContext) (int64, error) {
	return d.toSignedInteger(sc, mysql.TypeLonglong)
}
112036
func ConvertIntToInt(val int64, lowerBound int64, upperBound int64, tp byte) (int64, error) {
	if val < lowerBound {
		return lowerBound, overflow(val, tp)
	}

	if val > upperBound {
		return upperBound, overflow(val, tp)
	}

	return val, nil
}
672892
func (be *BinaryEncoder) WriteInt(x int32) {
	_, _ = be.buffer.Write(be.encodeVarint32(x))
}
1096909
func (b BinaryLiteral) ToInt(sc *stmtctx.StatementContext) (uint64, error) {
	buf := trimLeadingZeroBytes(b)
	length := len(buf)
	if length == 0 {
		return 0, nil
	}
	if length > 8 {
		var err error = ErrTruncatedWrongVal.GenWithStackByArgs("BINARY", b)
		if sc != nil {
			err = sc.HandleTruncate(err)
		}
		return math.MaxUint64, err
	}
	// Note: the byte-order is BigEndian.
	val := uint64(buf[0])
	for i := 1; i < length; i++ {
		val = (val << 8) | uint64(buf[i])
	}
	return val, nil
}
457080
func skipVarint(buf []byte) []byte {
	i := 0
	for ; buf[i]&0x80 != 0; i++ {
	}
	return buf[i+1:]
}
1004154
func getint64(val *querypb.BindVariable) (iv int64, status int) {
	bv, err := sqltypes.BindVariableToValue(val)
	if err != nil {
		return 0, QROutOfRange
	}
	v, err := sqltypes.ToInt64(bv)
	if err != nil {
		return 0, QROutOfRange
	}
	return v, QROK
}
1103254
func toInt(v reflect.Value) int {
	i, _ := tryToInt(v)
	return i
}
1274447
func ReadVarintCode(buf []byte) (int, int, error) {
	num, n := binary.Uvarint(buf)
	if n < 0 {
		return 0, 0, fmt.Errorf("varints larger than uint64 not yet supported")
	}
	return int(num), n, nil
}
488795
func DecodeUvarint(in io.Reader) (uint, error) {
	var abuf [1]byte
	buf := abuf[:]
	var n uint
	var shift uint
	for shift < 32 {
		c, err := in.Read(buf)
		if c == 1 {
			b := buf[0]
			n |= (uint(b) & 0x7f) << shift
			if (b & 0x80) == 0 {
				return n, nil
			}
		}
		if err != nil {
			return 0, err
		}
		shift += 7
	}
	return 0, fmt.Errorf("Bad varint encoding")
}