github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/compress/libdeflate/crc32_vec_template.h (about)

     1  // NOLINT(build/header_guard)
     2  /*
     3   * crc32_vec_template.h - template for vectorized CRC-32 implementations
     4   *
     5   * Copyright 2016 Eric Biggers
     6   *
     7   * Permission is hereby granted, free of charge, to any person
     8   * obtaining a copy of this software and associated documentation
     9   * files (the "Software"), to deal in the Software without
    10   * restriction, including without limitation the rights to use,
    11   * copy, modify, merge, publish, distribute, sublicense, and/or sell
    12   * copies of the Software, and to permit persons to whom the
    13   * Software is furnished to do so, subject to the following
    14   * conditions:
    15   *
    16   * The above copyright notice and this permission notice shall be
    17   * included in all copies or substantial portions of the Software.
    18   *
    19   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    20   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    21   * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    22   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    23   * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    24   * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    25   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    26   * OTHER DEALINGS IN THE SOFTWARE.
    27   */
    28  
    29  #define CRC32_SLICE1	1
    30  static u32 crc32_slice1(u32, const u8 *, size_t);
    31  
    32  /*
    33   * Template for vectorized CRC-32 implementations.
    34   *
    35   * Note: on unaligned ends of the buffer, we fall back to crc32_slice1() instead
    36   * of crc32_slice8() because only a few bytes need to be processed, so a smaller
    37   * table is preferable.
    38   */
    39  static u32 ATTRIBUTES
    40  FUNCNAME(u32 remainder, const u8 *p, size_t size)
    41  {
    42  	if ((uintptr_t)p % IMPL_ALIGNMENT) {
    43  		size_t n = MIN(size, -(uintptr_t)p % IMPL_ALIGNMENT);
    44  
    45  		remainder = crc32_slice1(remainder, p, n);
    46  		p += n;
    47  		size -= n;
    48  	}
    49  	if (size >= IMPL_SEGMENT_SIZE) {
    50  		remainder = FUNCNAME_ALIGNED(remainder, (const void *)p,
    51  					     size / IMPL_SEGMENT_SIZE);
    52  		p += size - (size % IMPL_SEGMENT_SIZE);
    53  		size %= IMPL_SEGMENT_SIZE;
    54  	}
    55  	return crc32_slice1(remainder, p, size);
    56  }
    57  
    58  #undef FUNCNAME
    59  #undef FUNCNAME_ALIGNED
    60  #undef ATTRIBUTES
    61  #undef IMPL_ALIGNMENT
    62  #undef IMPL_SEGMENT_SIZE