Skip to content

Commit

Permalink
abi, wit: align without division
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Oct 19, 2023
1 parent e5d4ed8 commit 696040f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
4 changes: 1 addition & 3 deletions abi/realloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func realloc(ptr unsafe.Pointer, size, align, newsize uintptr) unsafe.Pointer {
// offset returns the delta between the aligned value of ptr and ptr
// so it can be passed to unsafe.Add. The return value is guaranteed to be >= 0.
func offset(ptr, align uintptr) uintptr {
// (dividend + divisor - 1) / divisor
// http://www.cs.nott.ac.uk/~rcb/G51MPC/slides/NumberLogic.pdf
newptr := ((ptr + align - 1) / align) * align
newptr := (ptr + align - 1) &^ (align - 1)
return newptr - ptr
}

Expand Down
4 changes: 1 addition & 3 deletions wit/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package wit

// Align aligns ptr with alignment align.
func Align(ptr, align uintptr) uintptr {
// (dividend + divisor - 1) / divisor
// http://www.cs.nott.ac.uk/~rcb/G51MPC/slides/NumberLogic.pdf
return ((ptr + align - 1) / align) * align
return (ptr + align - 1) &^ (align - 1)
}

// Discriminant returns the smallest WIT integer type that can represent 0...n.
Expand Down

0 comments on commit 696040f

Please sign in to comment.