github.com/grailbio/base@v0.0.11/simd/xor_generic.go (about)

     1  // Code generated by " ../gtl/generate.py --prefix=Xor -DOPCHAR=^ --package=simd --output=xor_generic.go bitwise_generic.go.tpl ". DO NOT EDIT.
     2  
     3  // Copyright 2018 GRAIL, Inc.  All rights reserved.
     4  // Use of this source code is governed by the Apache-2.0
     5  // license that can be found in the LICENSE file.
     6  
     7  // +build !amd64 appengine
     8  
     9  package simd
    10  
    11  // XorUnsafeInplace sets main[pos] := main[pos] ^ arg[pos] for every position
    12  // in main[].
    13  //
    14  // WARNING: This is a function designed to be used in inner loops, which makes
    15  // assumptions about length and capacity which aren't checked at runtime.  Use
    16  // the safe version of this function when that's a problem.
    17  // Assumptions #2-3 are always satisfied when the last
    18  // potentially-size-increasing operation on arg[] is {Re}makeUnsafe(),
    19  // ResizeUnsafe(), or XcapUnsafe(), and the same is true for main[].
    20  //
    21  // 1. len(arg) and len(main) must be equal.
    22  //
    23  // 2. Capacities are at least RoundUpPow2(len(main) + 1, bytesPerVec).
    24  //
    25  // 3. The caller does not care if a few bytes past the end of main[] are
    26  // changed.
    27  func XorUnsafeInplace(main, arg []byte) {
    28  	for i, x := range main {
    29  		main[i] = x ^ arg[i]
    30  	}
    31  }
    32  
    33  // XorInplace sets main[pos] := main[pos] ^ arg[pos] for every position in
    34  // main[].  It panics if slice lengths don't match.
    35  func XorInplace(main, arg []byte) {
    36  	if len(arg) != len(main) {
    37  		panic("XorInplace() requires len(arg) == len(main).")
    38  	}
    39  	for i, x := range main {
    40  		main[i] = x ^ arg[i]
    41  	}
    42  }
    43  
    44  // XorUnsafe sets dst[pos] := src1[pos] ^ src2[pos] for every position in dst.
    45  //
    46  // WARNING: This is a function designed to be used in inner loops, which makes
    47  // assumptions about length and capacity which aren't checked at runtime.  Use
    48  // the safe version of this function when that's a problem.
    49  // Assumptions #2-3 are always satisfied when the last
    50  // potentially-size-increasing operation on src1[] is {Re}makeUnsafe(),
    51  // ResizeUnsafe(), or XcapUnsafe(), and the same is true for src2[] and dst[].
    52  //
    53  // 1. len(src1), len(src2), and len(dst) must be equal.
    54  //
    55  // 2. Capacities are at least RoundUpPow2(len(dst) + 1, bytesPerVec).
    56  //
    57  // 3. The caller does not care if a few bytes past the end of dst[] are
    58  // changed.
    59  func XorUnsafe(dst, src1, src2 []byte) {
    60  	for i, x := range src1 {
    61  		dst[i] = x ^ src2[i]
    62  	}
    63  }
    64  
    65  // Xor sets dst[pos] := src1[pos] ^ src2[pos] for every position in dst.  It
    66  // panics if slice lengths don't match.
    67  func Xor(dst, src1, src2 []byte) {
    68  	dstLen := len(dst)
    69  	if (len(src1) != dstLen) || (len(src2) != dstLen) {
    70  		panic("Xor() requires len(src1) == len(src2) == len(dst).")
    71  	}
    72  	for i, x := range src1 {
    73  		dst[i] = x ^ src2[i]
    74  	}
    75  }
    76  
    77  // XorConst8UnsafeInplace sets main[pos] := main[pos] ^ val for every position
    78  // in main[].
    79  //
    80  // WARNING: This is a function designed to be used in inner loops, which makes
    81  // assumptions about length and capacity which aren't checked at runtime.  Use
    82  // the safe version of this function when that's a problem.
    83  // These assumptions are always satisfied when the last
    84  // potentially-size-increasing operation on main[] is {Re}makeUnsafe(),
    85  // ResizeUnsafe(), or XcapUnsafe().
    86  //
    87  // 1. cap(main) is at least RoundUpPow2(len(main) + 1, bytesPerVec).
    88  //
    89  // 2. The caller does not care if a few bytes past the end of main[] are
    90  // changed.
    91  func XorConst8UnsafeInplace(main []byte, val byte) {
    92  	for i, x := range main {
    93  		main[i] = x ^ val
    94  	}
    95  }
    96  
    97  // XorConst8Inplace sets main[pos] := main[pos] ^ val for every position in
    98  // main[].
    99  func XorConst8Inplace(main []byte, val byte) {
   100  	for i, x := range main {
   101  		main[i] = x ^ val
   102  	}
   103  }
   104  
   105  // XorConst8Unsafe sets dst[pos] := src[pos] ^ val for every position in dst.
   106  //
   107  // WARNING: This is a function designed to be used in inner loops, which makes
   108  // assumptions about length and capacity which aren't checked at runtime.  Use
   109  // the safe version of this function when that's a problem.
   110  // Assumptions #2-3 are always satisfied when the last
   111  // potentially-size-increasing operation on src[] is {Re}makeUnsafe(),
   112  // ResizeUnsafe(), or XcapUnsafe(), and the same is true for dst[].
   113  //
   114  // 1. len(src) and len(dst) must be equal.
   115  //
   116  // 2. Capacities are at least RoundUpPow2(len(dst) + 1, bytesPerVec).
   117  //
   118  // 3. The caller does not care if a few bytes past the end of dst[] are
   119  // changed.
   120  func XorConst8Unsafe(dst, src []byte, val byte) {
   121  	for i, x := range src {
   122  		dst[i] = x ^ val
   123  	}
   124  }
   125  
   126  // XorConst8 sets dst[pos] := src[pos] ^ val for every position in dst.  It
   127  // panics if slice lengths don't match.
   128  func XorConst8(dst, src []byte, val byte) {
   129  	if len(src) != len(dst) {
   130  		panic("XorConst8() requires len(src) == len(dst).")
   131  	}
   132  	for i, x := range src {
   133  		dst[i] = x ^ val
   134  	}
   135  }