integer.go 2.8 KB

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