github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/arith/arith.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package arith
    12  
    13  import "math"
    14  
    15  // AddWithOverflow returns a+b. If ok is false, a+b overflowed.
    16  func AddWithOverflow(a, b int64) (r int64, ok bool) {
    17  	if b > 0 && a > math.MaxInt64-b {
    18  		return 0, false
    19  	}
    20  	if b < 0 && a < math.MinInt64-b {
    21  		return 0, false
    22  	}
    23  	return a + b, true
    24  }
    25  
    26  // Add32to64WithOverflow returns a+b. If ok is false, b was outside the
    27  // int32 range or a+b overflowed.
    28  func Add32to64WithOverflow(a int32, b int64) (r int32, ok bool) {
    29  	if b > math.MaxInt32 || b < math.MinInt32 {
    30  		return 0, false
    31  	}
    32  	return Add32WithOverflow(a, int32(b))
    33  }
    34  
    35  // Add32WithOverflow returns a+b. If ok is false, a+b overflowed.
    36  func Add32WithOverflow(a, b int32) (r int32, ok bool) {
    37  	if b > 0 && a > math.MaxInt32-b {
    38  		return 0, false
    39  	}
    40  	if b < 0 && a < math.MinInt32-b {
    41  		return 0, false
    42  	}
    43  	return a + b, true
    44  }
    45  
    46  // SubWithOverflow returns a-b. If ok is false, a-b overflowed.
    47  func SubWithOverflow(a, b int64) (r int64, ok bool) {
    48  	if b < 0 && a > math.MaxInt64+b {
    49  		return 0, false
    50  	}
    51  	if b > 0 && a < math.MinInt64+b {
    52  		return 0, false
    53  	}
    54  	return a - b, true
    55  }
    56  
    57  // Sub32to64WithOverflow returns a-b. If ok is false, b was outside the
    58  // int32 range or a-b overflowed.
    59  func Sub32to64WithOverflow(a int32, b int64) (r int32, ok bool) {
    60  	if b > math.MaxInt32 || b < math.MinInt32 {
    61  		return 0, false
    62  	}
    63  	return Sub32WithOverflow(a, int32(b))
    64  }
    65  
    66  // Sub32WithOverflow returns a-b. If ok is false, a-b overflowed.
    67  func Sub32WithOverflow(a, b int32) (r int32, ok bool) {
    68  	if b < 0 && a > math.MaxInt32+b {
    69  		return 0, false
    70  	}
    71  	if b > 0 && a < math.MinInt32+b {
    72  		return 0, false
    73  	}
    74  	return a - b, true
    75  }
    76  
    77  // MulHalfPositiveWithOverflow returns a*b. b must be positive. If ok
    78  // is false, a*b overflowed.
    79  func MulHalfPositiveWithOverflow(a, b int64) (r int64, ok bool) {
    80  	if a >= 0 {
    81  		if a > math.MaxInt64/b {
    82  			return 0, false
    83  		}
    84  	} else {
    85  		if a < math.MinInt64/b {
    86  			return 0, false
    87  		}
    88  	}
    89  	return a * b, true
    90  }