From e8eb1d82690c5c70df770df41ca237e5756f21d5 Mon Sep 17 00:00:00 2001 From: Colin Arnott Date: Thu, 6 Aug 2020 02:57:15 +0000 Subject: [PATCH] math: add MaxUint, MinInt, MaxInt Since we have int8 to int64 min max and uint8 to uint64 max constants, we should probably have some for the word size types too. This change also adds tests to validate the correctness of all integer limit values. Fixes #28538 Change-Id: Idd25782e98d16c2abedf39959b7b66e9c4c0c98b Reviewed-on: https://go-review.googlesource.com/c/go/+/247058 Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Trust: Robert Griesemer --- src/math/const.go | 5 +++++ src/math/const_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/math/const_test.go diff --git a/src/math/const.go b/src/math/const.go index 31954b0caea..5ea935fb425 100644 --- a/src/math/const.go +++ b/src/math/const.go @@ -37,6 +37,10 @@ const ( // Integer limit values. const ( + intSize = 32 << (^uint(0) >> 63) // 32 or 64 + + MaxInt = 1<<(intSize-1) - 1 + MinInt = -1 << (intSize - 1) MaxInt8 = 1<<7 - 1 MinInt8 = -1 << 7 MaxInt16 = 1<<15 - 1 @@ -45,6 +49,7 @@ const ( MinInt32 = -1 << 31 MaxInt64 = 1<<63 - 1 MinInt64 = -1 << 63 + MaxUint = 1<