github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/simd/add_generic.go (about)

     1  // Copyright 2018 GRAIL, Inc.  All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !amd64 appengine
     6  
     7  package simd
     8  
     9  // AddConst8UnsafeInplace adds the given constant to every byte of main[], with
    10  // unsigned overflow.
    11  //
    12  // WARNING: This is a function designed to be used in inner loops, which
    13  // assumes without checking that capacity is at least RoundUpPow2(len(main),
    14  // bytesPerVec).  It also assumes that the caller does not care if a few bytes
    15  // past the end of main[] are changed.  Use the safe version of this function
    16  // if any of these properties are problematic.
    17  // These assumptions are always satisfied when the last
    18  // potentially-size-increasing operation on main[] is {Re}makeUnsafe(),
    19  // ResizeUnsafe(), or XcapUnsafe().
    20  func AddConst8UnsafeInplace(main []byte, val byte) {
    21  	for i, x := range main {
    22  		main[i] = x + val
    23  	}
    24  }
    25  
    26  // AddConst8Inplace adds the given constant to every byte of main[], with
    27  // unsigned overflow.
    28  func AddConst8Inplace(main []byte, val byte) {
    29  	for i, x := range main {
    30  		main[i] = x + val
    31  	}
    32  }
    33  
    34  // AddConst8Unsafe sets dst[pos] := src[pos] + val for every byte in src (with
    35  // the usual unsigned overflow).
    36  //
    37  // WARNING: This is a function designed to be used in inner loops, which makes
    38  // assumptions about length and capacity which aren't checked at runtime.  Use
    39  // the safe version of this function when that's a problem.
    40  // Assumptions #2-3 are always satisfied when the last
    41  // potentially-size-increasing operation on src[] is {Re}makeUnsafe(),
    42  // ResizeUnsafe() or XcapUnsafe(), and the same is true for dst[].
    43  //
    44  // 1. len(src) and len(dst) are equal.
    45  //
    46  // 2. Capacities are at least RoundUpPow2(len(src) + 1, bytesPerVec).
    47  //
    48  // 3. The caller does not care if a few bytes past the end of dst[] are
    49  // changed.
    50  func AddConst8Unsafe(dst, src []byte, val byte) {
    51  	for i, x := range src {
    52  		dst[i] = x + val
    53  	}
    54  }
    55  
    56  // AddConst8 sets dst[pos] := src[pos] + val for every byte in src (with the
    57  // usual unsigned overflow).  It panics if len(src) != len(dst).
    58  func AddConst8(dst, src []byte, val byte) {
    59  	if len(dst) != len(src) {
    60  		panic("AddConst8() requires len(src) == len(dst).")
    61  	}
    62  	for i, x := range src {
    63  		dst[i] = x + val
    64  	}
    65  }
    66  
    67  // SubtractFromConst8UnsafeInplace subtracts every byte of main[] from the
    68  // given constant, with unsigned underflow.
    69  //
    70  // WARNING: This is a function designed to be used in inner loops, which
    71  // assumes without checking that capacity is at least RoundUpPow2(len(main),
    72  // bytesPerVec).  It also assumes that the caller does not care if a few bytes
    73  // past the end of main[] are changed.  Use the safe version of this function
    74  // if any of these properties are problematic.
    75  // These assumptions are always satisfied when the last
    76  // potentially-size-increasing operation on main[] is {Re}makeUnsafe(),
    77  // ResizeUnsafe(), or XcapUnsafe().
    78  func SubtractFromConst8UnsafeInplace(main []byte, val byte) {
    79  	for i, x := range main {
    80  		main[i] = val - x
    81  	}
    82  }
    83  
    84  // SubtractFromConst8Inplace subtracts every byte of main[] from the given
    85  // constant, with unsigned underflow.
    86  func SubtractFromConst8Inplace(main []byte, val byte) {
    87  	for i, x := range main {
    88  		main[i] = val - x
    89  	}
    90  }
    91  
    92  // SubtractFromConst8Unsafe sets dst[pos] := val - src[pos] for every byte in
    93  // src (with the usual unsigned overflow).
    94  //
    95  // WARNING: This is a function designed to be used in inner loops, which makes
    96  // assumptions about length and capacity which aren't checked at runtime.  Use
    97  // the safe version of this function when that's a problem.
    98  // Assumptions #2-3 are always satisfied when the last
    99  // potentially-size-increasing operation on src[] is {Re}makeUnsafe(),
   100  // ResizeUnsafe() or XcapUnsafe(), and the same is true for dst[].
   101  //
   102  // 1. len(src) and len(dst) are equal.
   103  //
   104  // 2. Capacities are at least RoundUpPow2(len(src) + 1, bytesPerVec).
   105  //
   106  // 3. The caller does not care if a few bytes past the end of dst[] are
   107  // changed.
   108  func SubtractFromConst8Unsafe(dst, src []byte, val byte) {
   109  	for i, x := range src {
   110  		dst[i] = val - x
   111  	}
   112  }
   113  
   114  // SubtractFromConst8 sets dst[pos] := val - src[pos] for every byte in src
   115  // (with the usual unsigned overflow).  It panics if len(src) != len(dst).
   116  func SubtractFromConst8(dst, src []byte, val byte) {
   117  	if len(dst) != len(src) {
   118  		panic("SubtractFromConst8() requires len(src) == len(dst).")
   119  	}
   120  	for i, x := range src {
   121  		dst[i] = val - x
   122  	}
   123  }