github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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  // Ctz64 counts trailing (low-order) zeroes,
     8  // and if all are zero, then 64.
     9  func Ctz64(x uint64) uint64 {
    10  	if x&0xffffffff == 0 {
    11  		return 32 + uint64(Ctz32(uint32(x>>32)))
    12  	}
    13  	return uint64(Ctz32(uint32(x)))
    14  
    15  }
    16  
    17  // Ctz32 counts trailing (low-order) zeroes,
    18  // and if all are zero, then 32.
    19  func Ctz32(x uint32) uint32 {
    20  	if x&0xffff == 0 {
    21  		return 16 + uint32(Ctz16(uint16(x>>16)))
    22  	}
    23  	return uint32(Ctz16(uint16(x)))
    24  }
    25  
    26  // Ctz16 counts trailing (low-order) zeroes,
    27  // and if all are zero, then 16.
    28  func Ctz16(x uint16) uint16 {
    29  	if x&0xff == 0 {
    30  		return 8 + uint16(Ctz8(uint8(x>>8)))
    31  	}
    32  	return uint16(Ctz8(uint8(x)))
    33  }
    34  
    35  // Ctz8 counts trailing (low-order) zeroes,
    36  // and if all are zero, then 8.
    37  func Ctz8(x uint8) uint8 {
    38  	return ctzVals[x]
    39  }
    40  
    41  var ctzVals = [256]uint8{
    42  	8, 0, 1, 0, 2, 0, 1, 0,
    43  	3, 0, 1, 0, 2, 0, 1, 0,
    44  	4, 0, 1, 0, 2, 0, 1, 0,
    45  	3, 0, 1, 0, 2, 0, 1, 0,
    46  	5, 0, 1, 0, 2, 0, 1, 0,
    47  	3, 0, 1, 0, 2, 0, 1, 0,
    48  	4, 0, 1, 0, 2, 0, 1, 0,
    49  	3, 0, 1, 0, 2, 0, 1, 0,
    50  	6, 0, 1, 0, 2, 0, 1, 0,
    51  	3, 0, 1, 0, 2, 0, 1, 0,
    52  	4, 0, 1, 0, 2, 0, 1, 0,
    53  	3, 0, 1, 0, 2, 0, 1, 0,
    54  	5, 0, 1, 0, 2, 0, 1, 0,
    55  	3, 0, 1, 0, 2, 0, 1, 0,
    56  	4, 0, 1, 0, 2, 0, 1, 0,
    57  	3, 0, 1, 0, 2, 0, 1, 0,
    58  	7, 0, 1, 0, 2, 0, 1, 0,
    59  	3, 0, 1, 0, 2, 0, 1, 0,
    60  	4, 0, 1, 0, 2, 0, 1, 0,
    61  	3, 0, 1, 0, 2, 0, 1, 0,
    62  	5, 0, 1, 0, 2, 0, 1, 0,
    63  	3, 0, 1, 0, 2, 0, 1, 0,
    64  	4, 0, 1, 0, 2, 0, 1, 0,
    65  	3, 0, 1, 0, 2, 0, 1, 0,
    66  	6, 0, 1, 0, 2, 0, 1, 0,
    67  	3, 0, 1, 0, 2, 0, 1, 0,
    68  	4, 0, 1, 0, 2, 0, 1, 0,
    69  	3, 0, 1, 0, 2, 0, 1, 0,
    70  	5, 0, 1, 0, 2, 0, 1, 0,
    71  	3, 0, 1, 0, 2, 0, 1, 0,
    72  	4, 0, 1, 0, 2, 0, 1, 0,
    73  	3, 0, 1, 0, 2, 0, 1, 0}
    74  
    75  // Bswap64 returns its input with byte order reversed
    76  // 0x0102030405060708 -> 0x0807060504030201
    77  func Bswap64(x uint64) uint64 {
    78  	c8 := uint64(0xff00ff00ff00ff00)
    79  	a := (x & c8) >> 8
    80  	b := (x &^ c8) << 8
    81  	x = a | b
    82  	c16 := uint64(0xffff0000ffff0000)
    83  	a = (x & c16) >> 16
    84  	b = (x &^ c16) << 16
    85  	x = a | b
    86  	c32 := uint64(0xffffffff00000000)
    87  	a = (x & c32) >> 32
    88  	b = (x &^ c32) << 32
    89  	x = a | b
    90  	return x
    91  }
    92  
    93  // Bswap32 returns its input with byte order reversed
    94  // 0x01020304 -> 0x04030201
    95  func Bswap32(x uint32) uint32 {
    96  	c8 := uint32(0xff00ff00)
    97  	a := (x & c8) >> 8
    98  	b := (x &^ c8) << 8
    99  	x = a | b
   100  	c16 := uint32(0xffff0000)
   101  	a = (x & c16) >> 16
   102  	b = (x &^ c16) << 16
   103  	x = a | b
   104  	return x
   105  }