round2_32.go 503 B

123456789101112131415161718192021222324252627282930
  1. //go:build !amd64 && !arm64 && !ppc64 && !ppc64le && !s390x
  2. package fasthttp
  3. import "math"
  4. func roundUpForSliceCap(n int) int {
  5. if n <= 0 {
  6. return 0
  7. }
  8. // Above 100MB, we don't round up as the overhead is too large.
  9. if n > 100*1024*1024 {
  10. return n
  11. }
  12. x := uint32(n - 1)
  13. x |= x >> 1
  14. x |= x >> 2
  15. x |= x >> 4
  16. x |= x >> 8
  17. x |= x >> 16
  18. // Make sure we don't return 0 due to overflow, even on 32 bit systems
  19. if x >= uint32(math.MaxInt32) {
  20. return math.MaxInt32
  21. }
  22. return int(x + 1)
  23. }