From 0276e0fe3b9e2594f6bd5f30b2c9b53d8e3c3c46 Mon Sep 17 00:00:00 2001 From: xtaci Date: Wed, 20 May 2020 23:09:49 +0800 Subject: [PATCH] optimized msb with http://graphics.stanford.edu/~seander/bithacks.html --- alloc.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/alloc.go b/alloc.go index 52887f6..9e3acf3 100644 --- a/alloc.go +++ b/alloc.go @@ -7,7 +7,7 @@ import ( var ( defaultAllocator *Allocator - debruijinPos = [...]byte{0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9} + debruijinPos = [...]byte{0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31} ) func init() { @@ -68,6 +68,5 @@ func msb(size int) byte { v |= v >> 4 v |= v >> 8 v |= v >> 16 - v = (v >> 1) + 1 - return debruijinPos[(v*0x077CB531)>>27] + return debruijinPos[(v*0x07C4ACDD)>>27] }