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