This commit is contained in:
Sahil-4555 2025-10-12 13:33:02 +05:30
parent 55fdee27cd
commit 49cae59c6a

View file

@ -27,32 +27,32 @@ func XORBytes(dst, a, b []byte) int {
// fastXORBytes xors in bulk. It only works on architectures that support // fastXORBytes xors in bulk. It only works on architectures that support
// unaligned read/writes. // unaligned read/writes.
func fastXORBytes(dst, a, b []byte) int { func fastXORBytes(dst, a, b []byte) int {
n := min(len(a), len(b), len(dst)) n := min(len(a), len(b), len(dst))
if n == 0 { if n == 0 {
return 0 return 0
} }
wordSize := int(unsafe.Sizeof(uintptr(0))) wordSize := int(unsafe.Sizeof(uintptr(0)))
i := 0 i := 0
for ; i < n && uintptr(unsafe.Pointer(&a[i]))%uintptr(wordSize) != 0; i++ { for ; i < n && uintptr(unsafe.Pointer(&a[i]))%uintptr(wordSize) != 0; i++ {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
w := (n - i) / wordSize w := (n - i) / wordSize
if w > 0 { if w > 0 {
ad := unsafe.Slice((*uintptr)(unsafe.Pointer(&a[i])), w) ad := unsafe.Slice((*uintptr)(unsafe.Pointer(&a[i])), w)
bd := unsafe.Slice((*uintptr)(unsafe.Pointer(&b[i])), w) bd := unsafe.Slice((*uintptr)(unsafe.Pointer(&b[i])), w)
dd := unsafe.Slice((*uintptr)(unsafe.Pointer(&dst[i])), w) dd := unsafe.Slice((*uintptr)(unsafe.Pointer(&dst[i])), w)
for j := 0; j < w; j++ { for j := 0; j < w; j++ {
dd[j] = ad[j] ^ bd[j] dd[j] = ad[j] ^ bd[j]
} }
i += w * wordSize i += w * wordSize
} }
for ; i < n; i++ { for ; i < n; i++ {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
return n return n
} }
// safeXORBytes xors one by one. It works on all architectures, independent if // safeXORBytes xors one by one. It works on all architectures, independent if
@ -77,32 +77,32 @@ func ANDBytes(dst, a, b []byte) int {
// fastANDBytes ands in bulk. It only works on architectures that support // fastANDBytes ands in bulk. It only works on architectures that support
// unaligned read/writes. // unaligned read/writes.
func fastANDBytes(dst, a, b []byte) int { func fastANDBytes(dst, a, b []byte) int {
n := min(len(a), len(b), len(dst)) n := min(len(a), len(b), len(dst))
if n == 0 { if n == 0 {
return 0 return 0
} }
wordSize := int(unsafe.Sizeof(uintptr(0))) wordSize := int(unsafe.Sizeof(uintptr(0)))
i := 0 i := 0
for ; i < n && uintptr(unsafe.Pointer(&a[i]))%uintptr(wordSize) != 0; i++ { for ; i < n && uintptr(unsafe.Pointer(&a[i]))%uintptr(wordSize) != 0; i++ {
dst[i] = a[i] & b[i] dst[i] = a[i] & b[i]
} }
w := (n - i) / wordSize w := (n - i) / wordSize
if w > 0 { if w > 0 {
ad := unsafe.Slice((*uintptr)(unsafe.Pointer(&a[i])), w) ad := unsafe.Slice((*uintptr)(unsafe.Pointer(&a[i])), w)
bd := unsafe.Slice((*uintptr)(unsafe.Pointer(&b[i])), w) bd := unsafe.Slice((*uintptr)(unsafe.Pointer(&b[i])), w)
dd := unsafe.Slice((*uintptr)(unsafe.Pointer(&dst[i])), w) dd := unsafe.Slice((*uintptr)(unsafe.Pointer(&dst[i])), w)
for j := 0; j < w; j++ { for j := 0; j < w; j++ {
dd[j] = ad[j] & bd[j] dd[j] = ad[j] & bd[j]
} }
i += w * wordSize i += w * wordSize
} }
for ; i < n; i++ { for ; i < n; i++ {
dst[i] = a[i] & b[i] dst[i] = a[i] & b[i]
} }
return n return n
} }
// safeANDBytes ands one by one. It works on all architectures, independent if // safeANDBytes ands one by one. It works on all architectures, independent if
@ -127,32 +127,32 @@ func ORBytes(dst, a, b []byte) int {
// fastORBytes ors in bulk. It only works on architectures that support // fastORBytes ors in bulk. It only works on architectures that support
// unaligned read/writes. // unaligned read/writes.
func fastORBytes(dst, a, b []byte) int { func fastORBytes(dst, a, b []byte) int {
n := min(len(a), len(b), len(dst)) n := min(len(a), len(b), len(dst))
if n == 0 { if n == 0 {
return 0 return 0
} }
wordSize := int(unsafe.Sizeof(uintptr(0))) wordSize := int(unsafe.Sizeof(uintptr(0)))
i := 0 i := 0
for ; i < n && uintptr(unsafe.Pointer(&a[i]))%uintptr(wordSize) != 0; i++ { for ; i < n && uintptr(unsafe.Pointer(&a[i]))%uintptr(wordSize) != 0; i++ {
dst[i] = a[i] | b[i] dst[i] = a[i] | b[i]
} }
w := (n - i) / wordSize w := (n - i) / wordSize
if w > 0 { if w > 0 {
ad := unsafe.Slice((*uintptr)(unsafe.Pointer(&a[i])), w) ad := unsafe.Slice((*uintptr)(unsafe.Pointer(&a[i])), w)
bd := unsafe.Slice((*uintptr)(unsafe.Pointer(&b[i])), w) bd := unsafe.Slice((*uintptr)(unsafe.Pointer(&b[i])), w)
dd := unsafe.Slice((*uintptr)(unsafe.Pointer(&dst[i])), w) dd := unsafe.Slice((*uintptr)(unsafe.Pointer(&dst[i])), w)
for j := 0; j < w; j++ { for j := 0; j < w; j++ {
dd[j] = ad[j] | bd[j] dd[j] = ad[j] | bd[j]
} }
i += w * wordSize i += w * wordSize
} }
for ; i < n; i++ { for ; i < n; i++ {
dst[i] = a[i] | b[i] dst[i] = a[i] | b[i]
} }
return n return n
} }
// safeORBytes ors one by one. It works on all architectures, independent if // safeORBytes ors one by one. It works on all architectures, independent if