github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/runtime/internal/sys/intrinsics.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package sys 6 7 // Using techniques from http://supertech.csail.mit.edu/papers/debruijn.pdf 8 9 const deBruijn64 = 0x0218a392cd3d5dbf 10 11 var deBruijnIdx64 = [64]byte{ 12 0, 1, 2, 7, 3, 13, 8, 19, 13 4, 25, 14, 28, 9, 34, 20, 40, 14 5, 17, 26, 38, 15, 46, 29, 48, 15 10, 31, 35, 54, 21, 50, 41, 57, 16 63, 6, 12, 18, 24, 27, 33, 39, 17 16, 37, 45, 47, 30, 53, 49, 56, 18 62, 11, 23, 32, 36, 44, 52, 55, 19 61, 22, 43, 51, 60, 42, 59, 58, 20 } 21 22 const deBruijn32 = 0x04653adf 23 24 var deBruijnIdx32 = [32]byte{ 25 0, 1, 2, 6, 3, 11, 7, 16, 26 4, 14, 12, 21, 8, 23, 17, 26, 27 31, 5, 10, 15, 13, 20, 22, 25, 28 30, 9, 19, 24, 29, 18, 28, 27, 29 } 30 31 const deBruijn16 = 0x09af 32 33 var deBruijnIdx16 = [16]byte{ 34 0, 1, 2, 5, 3, 9, 6, 11, 35 15, 4, 8, 10, 14, 7, 13, 12, 36 } 37 38 const deBruijn8 = 0x17 39 40 var deBruijnIdx8 = [8]byte{ 41 0, 1, 2, 4, 7, 3, 6, 5, 42 } 43 44 // Ctz64 counts trailing (low-order) zeroes, 45 // and if all are zero, then 64. 46 func Ctz64(x uint64) uint64 { 47 x &= -x // isolate low-order bit 48 y := x * deBruijn64 >> 58 // extract part of deBruijn sequence 49 y = uint64(deBruijnIdx64[y]) // convert to bit index 50 z := (x - 1) >> 57 & 64 // adjustment if zero 51 return y + z 52 } 53 54 // Ctz32 counts trailing (low-order) zeroes, 55 // and if all are zero, then 32. 56 func Ctz32(x uint32) uint32 { 57 x &= -x // isolate low-order bit 58 y := x * deBruijn32 >> 27 // extract part of deBruijn sequence 59 y = uint32(deBruijnIdx32[y]) // convert to bit index 60 z := (x - 1) >> 26 & 32 // adjustment if zero 61 return y + z 62 } 63 64 // Ctz16 counts trailing (low-order) zeroes, 65 // and if all are zero, then 16. 66 func Ctz16(x uint16) uint16 { 67 x &= -x // isolate low-order bit 68 y := x * deBruijn16 >> 12 // extract part of deBruijn sequence 69 y = uint16(deBruijnIdx16[y]) // convert to bit index 70 z := (x - 1) >> 11 & 16 // adjustment if zero 71 return y + z 72 } 73 74 // Ctz8 counts trailing (low-order) zeroes, 75 // and if all are zero, then 8. 76 func Ctz8(x uint8) uint8 { 77 x &= -x // isolate low-order bit 78 y := x * deBruijn8 >> 5 // extract part of deBruijn sequence 79 y = uint8(deBruijnIdx8[y]) // convert to bit index 80 z := (x - 1) >> 4 & 8 // adjustment if zero 81 return y + z 82 } 83 84 // Bswap64 returns its input with byte order reversed 85 // 0x0102030405060708 -> 0x0807060504030201 86 func Bswap64(x uint64) uint64 { 87 c8 := uint64(0x00ff00ff00ff00ff) 88 a := x >> 8 & c8 89 b := (x & c8) << 8 90 x = a | b 91 c16 := uint64(0x0000ffff0000ffff) 92 a = x >> 16 & c16 93 b = (x & c16) << 16 94 x = a | b 95 c32 := uint64(0x00000000ffffffff) 96 a = x >> 32 & c32 97 b = (x & c32) << 32 98 x = a | b 99 return x 100 } 101 102 // Bswap32 returns its input with byte order reversed 103 // 0x01020304 -> 0x04030201 104 func Bswap32(x uint32) uint32 { 105 c8 := uint32(0x00ff00ff) 106 a := x >> 8 & c8 107 b := (x & c8) << 8 108 x = a | b 109 c16 := uint32(0x0000ffff) 110 a = x >> 16 & c16 111 b = (x & c16) << 16 112 x = a | b 113 return x 114 }