github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/math/integer.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package math
    19  
    20  import (
    21  	"fmt"
    22  	"strconv"
    23  )
    24  
    25  // Integer limit values.
    26  const (
    27  	MaxInt8   = 1<<7 - 1
    28  	MinInt8   = -1 << 7
    29  	MaxInt16  = 1<<15 - 1
    30  	MinInt16  = -1 << 15
    31  	MaxInt32  = 1<<31 - 1
    32  	MinInt32  = -1 << 31
    33  	MaxInt64  = 1<<63 - 1
    34  	MinInt64  = -1 << 63
    35  	MaxUint8  = 1<<8 - 1
    36  	MaxUint16 = 1<<16 - 1
    37  	MaxUint32 = 1<<32 - 1
    38  	MaxUint64 = 1<<64 - 1
    39  )
    40  
    41  // HexOrDecimal64 marshals uint64 as hex or decimal.
    42  type HexOrDecimal64 uint64
    43  
    44  // UnmarshalText implements encoding.TextUnmarshaler.
    45  func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
    46  	int, ok := ParseUint64(string(input))
    47  	if !ok {
    48  		return fmt.Errorf("invalid hex or decimal integer %q", input)
    49  	}
    50  	*i = HexOrDecimal64(int)
    51  	return nil
    52  }
    53  
    54  // MarshalText implements encoding.TextMarshaler.
    55  func (i HexOrDecimal64) MarshalText() ([]byte, error) {
    56  	return []byte(fmt.Sprintf("%#x", uint64(i))), nil
    57  }
    58  
    59  // ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
    60  // Leading zeros are accepted. The empty string parses as zero.
    61  func ParseUint64(s string) (uint64, bool) {
    62  	if s == "" {
    63  		return 0, true
    64  	}
    65  	if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
    66  		v, err := strconv.ParseUint(s[2:], 16, 64)
    67  		return v, err == nil
    68  	}
    69  	v, err := strconv.ParseUint(s, 10, 64)
    70  	return v, err == nil
    71  }
    72  
    73  // MustParseUint64 parses s as an integer and panics if the string is invalid.
    74  func MustParseUint64(s string) uint64 {
    75  	v, ok := ParseUint64(s)
    76  	if !ok {
    77  		panic("invalid unsigned 64 bit integer: " + s)
    78  	}
    79  	return v
    80  }
    81  
    82  // NOTE: The following methods need to be optimised using either bit checking or asm
    83  
    84  // SafeSub returns subtraction result and whether overflow occurred.
    85  func SafeSub(x, y uint64) (uint64, bool) {
    86  	return x - y, x < y
    87  }
    88  
    89  // SafeAdd returns the result and whether overflow occurred.
    90  func SafeAdd(x, y uint64) (uint64, bool) {
    91  	return x + y, y > MaxUint64-x
    92  }
    93  
    94  // SafeMul returns multiplication result and whether overflow occurred.
    95  func SafeMul(x, y uint64) (uint64, bool) {
    96  	if x == 0 || y == 0 {
    97  		return 0, false
    98  	}
    99  	return x * y, y > MaxUint64/x
   100  }