github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/bitutil/bitutil.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2013 Go作者。版权所有。
    10  //此源代码的使用受BSD样式的控制
    11  //可以在许可文件中找到的许可证。
    12  
    13  //改编自:https://golang.org/src/crypto/cipher/xor.go
    14  
    15  //包bitutil实现快速按位操作。
    16  package bitutil
    17  
    18  import (
    19  	"runtime"
    20  	"unsafe"
    21  )
    22  
    23  const wordSize = int(unsafe.Sizeof(uintptr(0)))
    24  const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "s390x"
    25  
    26  //xorbytes xor是a和b中的字节。假定目标具有足够的
    27  //空间。返回字节数xor'd。
    28  func XORBytes(dst, a, b []byte) int {
    29  	if supportsUnaligned {
    30  		return fastXORBytes(dst, a, b)
    31  	}
    32  	return safeXORBytes(dst, a, b)
    33  }
    34  
    35  //FastXorBytes大容量XORS。它只适用于支持
    36  //未对齐的读/写。
    37  func fastXORBytes(dst, a, b []byte) int {
    38  	n := len(a)
    39  	if len(b) < n {
    40  		n = len(b)
    41  	}
    42  	w := n / wordSize
    43  	if w > 0 {
    44  		dw := *(*[]uintptr)(unsafe.Pointer(&dst))
    45  		aw := *(*[]uintptr)(unsafe.Pointer(&a))
    46  		bw := *(*[]uintptr)(unsafe.Pointer(&b))
    47  		for i := 0; i < w; i++ {
    48  			dw[i] = aw[i] ^ bw[i]
    49  		}
    50  	}
    51  	for i := n - n%wordSize; i < n; i++ {
    52  		dst[i] = a[i] ^ b[i]
    53  	}
    54  	return n
    55  }
    56  
    57  //安全字节一个接一个。它适用于所有体系结构,独立于
    58  //它是否支持未对齐的读/写。
    59  func safeXORBytes(dst, a, b []byte) int {
    60  	n := len(a)
    61  	if len(b) < n {
    62  		n = len(b)
    63  	}
    64  	for i := 0; i < n; i++ {
    65  		dst[i] = a[i] ^ b[i]
    66  	}
    67  	return n
    68  }
    69  
    70  //AndBytes和A和B中的字节。假定目标具有足够的
    71  //空间。返回字节数和'd'。
    72  func ANDBytes(dst, a, b []byte) int {
    73  	if supportsUnaligned {
    74  		return fastANDBytes(dst, a, b)
    75  	}
    76  	return safeANDBytes(dst, a, b)
    77  }
    78  
    79  //FastAndBytes和批量。它只适用于支持
    80  //未对齐的读/写。
    81  func fastANDBytes(dst, a, b []byte) int {
    82  	n := len(a)
    83  	if len(b) < n {
    84  		n = len(b)
    85  	}
    86  	w := n / wordSize
    87  	if w > 0 {
    88  		dw := *(*[]uintptr)(unsafe.Pointer(&dst))
    89  		aw := *(*[]uintptr)(unsafe.Pointer(&a))
    90  		bw := *(*[]uintptr)(unsafe.Pointer(&b))
    91  		for i := 0; i < w; i++ {
    92  			dw[i] = aw[i] & bw[i]
    93  		}
    94  	}
    95  	for i := n - n%wordSize; i < n; i++ {
    96  		dst[i] = a[i] & b[i]
    97  	}
    98  	return n
    99  }
   100  
   101  //安全字节和一个接一个。它适用于所有体系结构,独立于
   102  //它是否支持未对齐的读/写。
   103  func safeANDBytes(dst, a, b []byte) int {
   104  	n := len(a)
   105  	if len(b) < n {
   106  		n = len(b)
   107  	}
   108  	for i := 0; i < n; i++ {
   109  		dst[i] = a[i] & b[i]
   110  	}
   111  	return n
   112  }
   113  
   114  //ORBYTES或A和B中的字节。假定目标具有足够的
   115  //空间。返回字节数或'd'。
   116  func ORBytes(dst, a, b []byte) int {
   117  	if supportsUnaligned {
   118  		return fastORBytes(dst, a, b)
   119  	}
   120  	return safeORBytes(dst, a, b)
   121  }
   122  
   123  //FastOrbytes或大量。它只适用于支持
   124  //未对齐的读/写。
   125  func fastORBytes(dst, a, b []byte) int {
   126  	n := len(a)
   127  	if len(b) < n {
   128  		n = len(b)
   129  	}
   130  	w := n / wordSize
   131  	if w > 0 {
   132  		dw := *(*[]uintptr)(unsafe.Pointer(&dst))
   133  		aw := *(*[]uintptr)(unsafe.Pointer(&a))
   134  		bw := *(*[]uintptr)(unsafe.Pointer(&b))
   135  		for i := 0; i < w; i++ {
   136  			dw[i] = aw[i] | bw[i]
   137  		}
   138  	}
   139  	for i := n - n%wordSize; i < n; i++ {
   140  		dst[i] = a[i] | b[i]
   141  	}
   142  	return n
   143  }
   144  
   145  //安全字节或一个接一个。它适用于所有体系结构,独立于
   146  //它是否支持未对齐的读/写。
   147  func safeORBytes(dst, a, b []byte) int {
   148  	n := len(a)
   149  	if len(b) < n {
   150  		n = len(b)
   151  	}
   152  	for i := 0; i < n; i++ {
   153  		dst[i] = a[i] | b[i]
   154  	}
   155  	return n
   156  }
   157  
   158  //测试字节测试输入字节片中是否设置了任何位。
   159  func TestBytes(p []byte) bool {
   160  	if supportsUnaligned {
   161  		return fastTestBytes(p)
   162  	}
   163  	return safeTestBytes(p)
   164  }
   165  
   166  //FastTestBytes批量测试设置位。它只适用于那些
   167  //支持未对齐的读/写。
   168  func fastTestBytes(p []byte) bool {
   169  	n := len(p)
   170  	w := n / wordSize
   171  	if w > 0 {
   172  		pw := *(*[]uintptr)(unsafe.Pointer(&p))
   173  		for i := 0; i < w; i++ {
   174  			if pw[i] != 0 {
   175  				return true
   176  			}
   177  		}
   178  	}
   179  	for i := n - n%wordSize; i < n; i++ {
   180  		if p[i] != 0 {
   181  			return true
   182  		}
   183  	}
   184  	return false
   185  }
   186  
   187  //safetestBytes一次测试一个字节的设置位。它适用于所有
   188  //体系结构,独立于是否支持未对齐的读/写。
   189  func safeTestBytes(p []byte) bool {
   190  	for i := 0; i < len(p); i++ {
   191  		if p[i] != 0 {
   192  			return true
   193  		}
   194  	}
   195  	return false
   196  }