bitutil.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2013 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. // Adapted from: https://golang.org/src/crypto/cipher/xor.go
  5. // Package bitutil implements fast bitwise operations.
  6. package bitutil
  7. import (
  8. "runtime"
  9. "unsafe"
  10. )
  11. const wordSize = int(unsafe.Sizeof(uintptr(0)))
  12. const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "s390x"
  13. // XORBytes xors the bytes in a and b. The destination is assumed to have enough
  14. // space. Returns the number of bytes xor'd.
  15. func XORBytes(dst, a, b []byte) int {
  16. if supportsUnaligned {
  17. return fastXORBytes(dst, a, b)
  18. }
  19. return safeXORBytes(dst, a, b)
  20. }
  21. // fastXORBytes xors in bulk. It only works on architectures that support
  22. // unaligned read/writes.
  23. func fastXORBytes(dst, a, b []byte) int {
  24. n := len(a)
  25. if len(b) < n {
  26. n = len(b)
  27. }
  28. w := n / wordSize
  29. if w > 0 {
  30. dw := *(*[]uintptr)(unsafe.Pointer(&dst))
  31. aw := *(*[]uintptr)(unsafe.Pointer(&a))
  32. bw := *(*[]uintptr)(unsafe.Pointer(&b))
  33. for i := 0; i < w; i++ {
  34. dw[i] = aw[i] ^ bw[i]
  35. }
  36. }
  37. for i := n - n%wordSize; i < n; i++ {
  38. dst[i] = a[i] ^ b[i]
  39. }
  40. return n
  41. }
  42. // safeXORBytes xors one by one. It works on all architectures, independent if
  43. // it supports unaligned read/writes or not.
  44. func safeXORBytes(dst, a, b []byte) int {
  45. n := len(a)
  46. if len(b) < n {
  47. n = len(b)
  48. }
  49. for i := 0; i < n; i++ {
  50. dst[i] = a[i] ^ b[i]
  51. }
  52. return n
  53. }
  54. // ANDBytes ands the bytes in a and b. The destination is assumed to have enough
  55. // space. Returns the number of bytes and'd.
  56. func ANDBytes(dst, a, b []byte) int {
  57. if supportsUnaligned {
  58. return fastANDBytes(dst, a, b)
  59. }
  60. return safeANDBytes(dst, a, b)
  61. }
  62. // fastANDBytes ands in bulk. It only works on architectures that support
  63. // unaligned read/writes.
  64. func fastANDBytes(dst, a, b []byte) int {
  65. n := len(a)
  66. if len(b) < n {
  67. n = len(b)
  68. }
  69. w := n / wordSize
  70. if w > 0 {
  71. dw := *(*[]uintptr)(unsafe.Pointer(&dst))
  72. aw := *(*[]uintptr)(unsafe.Pointer(&a))
  73. bw := *(*[]uintptr)(unsafe.Pointer(&b))
  74. for i := 0; i < w; i++ {
  75. dw[i] = aw[i] & bw[i]
  76. }
  77. }
  78. for i := n - n%wordSize; i < n; i++ {
  79. dst[i] = a[i] & b[i]
  80. }
  81. return n
  82. }
  83. // safeANDBytes ands one by one. It works on all architectures, independent if
  84. // it supports unaligned read/writes or not.
  85. func safeANDBytes(dst, a, b []byte) int {
  86. n := len(a)
  87. if len(b) < n {
  88. n = len(b)
  89. }
  90. for i := 0; i < n; i++ {
  91. dst[i] = a[i] & b[i]
  92. }
  93. return n
  94. }
  95. // ORBytes ors the bytes in a and b. The destination is assumed to have enough
  96. // space. Returns the number of bytes or'd.
  97. func ORBytes(dst, a, b []byte) int {
  98. if supportsUnaligned {
  99. return fastORBytes(dst, a, b)
  100. }
  101. return safeORBytes(dst, a, b)
  102. }
  103. // fastORBytes ors in bulk. It only works on architectures that support
  104. // unaligned read/writes.
  105. func fastORBytes(dst, a, b []byte) int {
  106. n := len(a)
  107. if len(b) < n {
  108. n = len(b)
  109. }
  110. w := n / wordSize
  111. if w > 0 {
  112. dw := *(*[]uintptr)(unsafe.Pointer(&dst))
  113. aw := *(*[]uintptr)(unsafe.Pointer(&a))
  114. bw := *(*[]uintptr)(unsafe.Pointer(&b))
  115. for i := 0; i < w; i++ {
  116. dw[i] = aw[i] | bw[i]
  117. }
  118. }
  119. for i := n - n%wordSize; i < n; i++ {
  120. dst[i] = a[i] | b[i]
  121. }
  122. return n
  123. }
  124. // safeORBytes ors one by one. It works on all architectures, independent if
  125. // it supports unaligned read/writes or not.
  126. func safeORBytes(dst, a, b []byte) int {
  127. n := len(a)
  128. if len(b) < n {
  129. n = len(b)
  130. }
  131. for i := 0; i < n; i++ {
  132. dst[i] = a[i] | b[i]
  133. }
  134. return n
  135. }
  136. // TestBytes tests whether any bit is set in the input byte slice.
  137. func TestBytes(p []byte) bool {
  138. if supportsUnaligned {
  139. return fastTestBytes(p)
  140. }
  141. return safeTestBytes(p)
  142. }
  143. // fastTestBytes tests for set bits in bulk. It only works on architectures that
  144. // support unaligned read/writes.
  145. func fastTestBytes(p []byte) bool {
  146. n := len(p)
  147. w := n / wordSize
  148. if w > 0 {
  149. pw := *(*[]uintptr)(unsafe.Pointer(&p))
  150. for i := 0; i < w; i++ {
  151. if pw[i] != 0 {
  152. return true
  153. }
  154. }
  155. }
  156. for i := n - n%wordSize; i < n; i++ {
  157. if p[i] != 0 {
  158. return true
  159. }
  160. }
  161. return false
  162. }
  163. // safeTestBytes tests for set bits one byte at a time. It works on all
  164. // architectures, independent if it supports unaligned read/writes or not.
  165. func safeTestBytes(p []byte) bool {
  166. for i := 0; i < len(p); i++ {
  167. if p[i] != 0 {
  168. return true
  169. }
  170. }
  171. return false
  172. }