Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active October 9, 2015 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/aa0901ca5df5b0a1a536 to your computer and use it in GitHub Desktop.
Save caelifer/aa0901ca5df5b0a1a536 to your computer and use it in GitHub Desktop.
// Taken from https://github.com/mdempsky/maligned/blob/master/maligned.go#L227.
// Corrected for the negative x.
// align returns the smallest y >= x such that y % a == 0.
func align(x, a int64) int64 {
sign := int64(1)
if x < 0 {
x = -x
sign = -1
}
if a < 0 {
a = -a // align size is always positive
}
y := x + a - 1
return sign * (y - y%a)
}
@caelifer
Copy link
Author

caelifer commented Oct 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment