github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datacodec/math.go (about) 1 // Copyright 2021 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package datacodec 16 17 import ( 18 "math" 19 ) 20 21 // Adapted from java.lang.Math#addExact(long, long). 22 // Returns the sum of its arguments, and a boolean indicating whether the sum overflowed. 23 func addExact(x, y int64) (int64, bool) { 24 r := x + y 25 if ((x ^ r) & (y ^ r)) < 0 { 26 return 0, true 27 } 28 return r, false 29 } 30 31 // Adapted from: 32 // https://stackoverflow.com/questions/50744681/testing-overflow-in-integer-multiplication. 33 // Returns the product of its arguments, and a boolean indicating whether the product overflowed. 34 // Another interesting implementation can be found in java.lang.Math#multiplyExact(long, long). 35 func multiplyExact(x, y int64) (int64, bool) { 36 if x == 0 || y == 0 || x == 1 || y == 1 { 37 return x * y, false 38 } else if x == math.MinInt64 || y == math.MinInt64 { 39 return 0, true 40 } else { 41 r := x * y 42 if r/y != x { 43 return 0, true 44 } 45 return r, false 46 } 47 } 48 49 // Adapted from java.lang.Math#floorDiv(int, int). 50 // Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient. 51 // There is one special case, if the dividend is the Long.MIN_VALUE and the divisor is -1, then integer overflow occurs 52 // and the result is equal to the Long.MIN_VALUE. 53 // Normal integer division operates under the round to zero rounding mode (truncation). This operation instead acts 54 // under the round toward negative infinity (floor) rounding mode. The floor rounding mode gives different results than 55 // truncation when the exact result is negative. 56 func floorDiv(x, y int64) int64 { 57 r := x / y 58 // if the signs are different and modulo not zero, round down 59 if (x^y) < 0 && (r*y != x) { 60 r-- 61 } 62 return r 63 } 64 65 // Adapted from java.lang.Math#floorMod(int, int). 66 // Returns the floor modulus of the long arguments. 67 // The floor modulus is x - (floorDiv(x, y) * y), has the same sign as the divisor y, and is in the range of 68 // -abs(y) < r < +abs(y). 69 func floorMod(x, y int64) int64 { 70 return x - floorDiv(x, y)*y 71 }