round2_32.go 550 B

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