github.com/searKing/golang/go@v1.2.117/exp/math/limits.go (about) 1 // Copyright 2023 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package math 6 7 import ( 8 "unsafe" 9 10 "golang.org/x/exp/constraints" 11 ) 12 13 // MinInt returns the smallest value representable by the type. 14 func MinInt[T constraints.Integer]() T { 15 var zero T 16 minusOne := ^zero 17 if minusOne > 0 { 18 return zero // Unsigned 19 } 20 bits := unsafe.Sizeof(zero) << 3 21 return minusOne << (bits - 1) // Signed 22 } 23 24 // MaxInt returns the largest value representable by the type. 25 func MaxInt[T constraints.Integer]() T { 26 return ^MinInt[T]() 27 }