2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file crc64.c
|
|
|
|
/// \brief CRC64 calculation
|
|
|
|
//
|
2022-11-14 19:34:57 +00:00
|
|
|
// Authors: Lasse Collin
|
|
|
|
// Ilya Kurdyukov
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "check.h"
|
2023-10-12 17:23:40 +00:00
|
|
|
#include "crc_common.h"
|
2022-11-14 19:34:57 +00:00
|
|
|
|
2023-10-14 04:17:57 +00:00
|
|
|
#ifdef CRC_GENERIC
|
|
|
|
|
2022-11-14 19:34:57 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
// Generic slice-by-four CRC64 //
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
# define A1(x) ((x) >> 56)
|
|
|
|
#else
|
|
|
|
# define A1 A
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-04-13 08:27:40 +00:00
|
|
|
// See the comments in crc32_fast.c. They aren't duplicated here.
|
2022-11-14 19:34:57 +00:00
|
|
|
static uint64_t
|
|
|
|
crc64_generic(const uint8_t *buf, size_t size, uint64_t crc)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
|
|
|
crc = ~crc;
|
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
2009-10-04 19:57:12 +00:00
|
|
|
crc = bswap64(crc);
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (size > 4) {
|
|
|
|
while ((uintptr_t)(buf) & 3) {
|
|
|
|
crc = lzma_crc64_table[0][*buf++ ^ A1(crc)] ^ S8(crc);
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *const limit = buf + (size & ~(size_t)(3));
|
|
|
|
size &= (size_t)(3);
|
|
|
|
|
|
|
|
while (buf < limit) {
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
2022-10-31 09:54:44 +00:00
|
|
|
const uint32_t tmp = (uint32_t)(crc >> 32)
|
2019-12-30 22:29:48 +00:00
|
|
|
^ aligned_read32ne(buf);
|
2007-12-08 22:42:33 +00:00
|
|
|
#else
|
2022-10-31 09:54:44 +00:00
|
|
|
const uint32_t tmp = (uint32_t)crc
|
|
|
|
^ aligned_read32ne(buf);
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
buf += 4;
|
|
|
|
|
|
|
|
crc = lzma_crc64_table[3][A(tmp)]
|
|
|
|
^ lzma_crc64_table[2][B(tmp)]
|
|
|
|
^ S32(crc)
|
|
|
|
^ lzma_crc64_table[1][C(tmp)]
|
|
|
|
^ lzma_crc64_table[0][D(tmp)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (size-- != 0)
|
|
|
|
crc = lzma_crc64_table[0][*buf++ ^ A1(crc)] ^ S8(crc);
|
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
2009-10-04 19:57:12 +00:00
|
|
|
crc = bswap64(crc);
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ~crc;
|
|
|
|
}
|
2022-11-14 19:34:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CRC_GENERIC) && defined(CRC_CLMUL)
|
2023-10-14 05:23:23 +00:00
|
|
|
//////////////////////////
|
|
|
|
// Function dispatching //
|
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
// If both the generic and CLMUL implementations are usable, then the
|
|
|
|
// function that is used is selected at runtime. See crc32_fast.c.
|
|
|
|
|
2023-06-27 14:05:23 +00:00
|
|
|
typedef uint64_t (*crc64_func_type)(
|
|
|
|
const uint8_t *buf, size_t size, uint64_t crc);
|
|
|
|
|
2023-07-19 15:36:00 +00:00
|
|
|
#if defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(__clang__)
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wunused-function"
|
|
|
|
#endif
|
2023-06-27 14:05:23 +00:00
|
|
|
|
|
|
|
static crc64_func_type
|
|
|
|
crc64_resolve(void)
|
|
|
|
{
|
2023-10-14 04:17:57 +00:00
|
|
|
return lzma_is_clmul_supported() ? &lzma_crc64_clmul : &crc64_generic;
|
2023-06-27 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 15:36:00 +00:00
|
|
|
#if defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(__clang__)
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
2023-06-27 14:05:23 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_FUNC_ATTRIBUTE_IFUNC
|
|
|
|
|
2022-11-14 19:34:57 +00:00
|
|
|
#ifdef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
|
|
|
|
# define CRC64_SET_FUNC_ATTR __attribute__((__constructor__))
|
2023-06-27 14:05:23 +00:00
|
|
|
static crc64_func_type crc64_func;
|
2022-11-14 19:34:57 +00:00
|
|
|
#else
|
|
|
|
# define CRC64_SET_FUNC_ATTR
|
|
|
|
static uint64_t crc64_dispatch(const uint8_t *buf, size_t size, uint64_t crc);
|
2023-06-27 14:05:23 +00:00
|
|
|
static crc64_func_type crc64_func = &crc64_dispatch;
|
2022-11-14 19:34:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
CRC64_SET_FUNC_ATTR
|
|
|
|
static void
|
|
|
|
crc64_set_func(void)
|
|
|
|
{
|
2023-06-27 14:05:23 +00:00
|
|
|
crc64_func = crc64_resolve();
|
2022-11-14 19:34:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
|
|
|
|
static uint64_t
|
|
|
|
crc64_dispatch(const uint8_t *buf, size_t size, uint64_t crc)
|
|
|
|
{
|
|
|
|
crc64_set_func();
|
|
|
|
return crc64_func(buf, size, crc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2023-06-27 14:05:23 +00:00
|
|
|
#endif
|
2022-11-14 19:34:57 +00:00
|
|
|
|
|
|
|
|
2023-10-14 02:23:03 +00:00
|
|
|
#ifdef CRC_USE_IFUNC
|
2023-06-27 14:05:23 +00:00
|
|
|
extern LZMA_API(uint64_t)
|
|
|
|
lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
|
|
|
|
__attribute__((__ifunc__("crc64_resolve")));
|
|
|
|
#else
|
2022-11-14 19:34:57 +00:00
|
|
|
extern LZMA_API(uint64_t)
|
|
|
|
lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
|
|
|
|
{
|
|
|
|
#if defined(CRC_GENERIC) && defined(CRC_CLMUL)
|
2023-10-14 05:23:23 +00:00
|
|
|
|
2022-11-14 19:34:57 +00:00
|
|
|
#ifdef CRC_USE_GENERIC_FOR_SMALL_INPUTS
|
|
|
|
if (size <= 16)
|
|
|
|
return crc64_generic(buf, size, crc);
|
|
|
|
#endif
|
|
|
|
return crc64_func(buf, size, crc);
|
|
|
|
|
|
|
|
#elif defined(CRC_CLMUL)
|
|
|
|
// If CLMUL is used unconditionally without runtime CPU detection
|
|
|
|
// then omitting the generic version and its 8 KiB lookup table
|
|
|
|
// makes the library smaller.
|
|
|
|
//
|
|
|
|
// FIXME: Lookup table isn't currently omitted on 32-bit x86,
|
|
|
|
// see crc64_table.c.
|
2023-10-14 04:17:57 +00:00
|
|
|
return lzma_crc64_clmul(buf, size, crc);
|
2022-11-14 19:34:57 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
return crc64_generic(buf, size, crc);
|
|
|
|
#endif
|
|
|
|
}
|
2023-06-27 14:05:23 +00:00
|
|
|
#endif
|