Files
math/safe/safe.go
T
Zach Kelling fd27f2ecb0 Reorganize math package into subpackages
- Create math/big for big.Int utilities (HexOrDecimal256, U256, parsing)
- Create math/safe for overflow-safe arithmetic (SafeAdd, SafeMul, etc.)
- Move averager files to root package level
- Add re-exports in root package for backwards compatibility
- Update README with new package structure documentation
- Remove old math/meter directory (consolidated elsewhere)
- Remove redundant math/ subdirectory structure
2026-01-03 18:22:33 -08:00

82 lines
1.6 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package safe provides overflow-safe arithmetic operations.
package safe
import (
"errors"
"math"
"math/bits"
"golang.org/x/exp/constraints"
)
var (
ErrOverflow = errors.New("overflow")
ErrUnderflow = errors.New("underflow")
)
// Add64 returns a + b, or error if overflow.
func Add64(a, b uint64) (uint64, error) {
if a > math.MaxUint64-b {
return 0, ErrOverflow
}
return a + b, nil
}
// Sub returns a - b, or error if underflow.
func Sub[T constraints.Unsigned](a, b T) (T, error) {
if a < b {
return 0, ErrUnderflow
}
return a - b, nil
}
// Mul64 returns a * b, or error if overflow.
func Mul64(a, b uint64) (uint64, error) {
if b != 0 && a > math.MaxUint64/b {
return 0, ErrOverflow
}
return a * b, nil
}
// SafeAdd returns x+y and whether overflow occurred.
func SafeAdd(x, y uint64) (uint64, bool) {
sum, carryOut := bits.Add64(x, y, 0)
return sum, carryOut != 0
}
// SafeSub returns x-y and whether underflow occurred.
func SafeSub(x, y uint64) (uint64, bool) {
diff, borrowOut := bits.Sub64(x, y, 0)
return diff, borrowOut != 0
}
// SafeMul returns x*y and whether overflow occurred.
func SafeMul(x, y uint64) (uint64, bool) {
hi, lo := bits.Mul64(x, y)
return lo, hi != 0
}
// AbsDiff returns |a - b|.
func AbsDiff[T constraints.Unsigned](a, b T) T {
return max(a, b) - min(a, b)
}
// Min returns the minimum of two uint64 values.
func Min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
// Max returns the maximum of two uint64 values.
func Max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}