Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trivial case in compile_time_utils.h #179

Closed
maths644311798 opened this issue Dec 28, 2023 · 3 comments
Closed

Trivial case in compile_time_utils.h #179

maths644311798 opened this issue Dec 28, 2023 · 3 comments

Comments

@maths644311798
Copy link
Contributor

compile_time_utils.h中有代码

template <>
constexpr uint32_t crc32<static_cast<size_t>(-1)>(
    [[maybe_unused]] const char* str) {
  return 0xFFFFFFFF;
}

这个特殊情况对应零多项式,应该返回0吧。

@Jamie-Cui
Copy link
Collaborator

可以参考一下这个逻辑,和 yacl 里面的 crc32 recursion 逻辑正好相反
https://web.mit.edu/freebsd/head/sys/libkern/crc32.c

/*
 * A function that calculates the CRC-32 based on the table above is
 * given below for documentation purposes. An equivalent implementation
 * of this function that's actually used in the kernel can be found
 * in sys/libkern.h, where it can be inlined.
 *
 *	uint32_t
 *	crc32(const void *buf, size_t size)
 *	{
 *		const uint8_t *p = buf;
 *		uint32_t crc;
 *
 *		crc = ~0U;  /* 初始化为 0xFFFFFFFF */
 *		while (size--)
 *			crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
 *		return crc ^ ~0U;
 *	}
 */

@maths644311798
Copy link
Contributor Author

我承认Yacl的实现和 https://web.mit.edu/freebsd/head/sys/libkern/crc32.c 有相同功能。但是

char c[2] = { 0x0,0x00 };
cout << hex << CT_CRC32(c) << '\n';

这段代码会输出 d202ef8d,如何解释?对于零多项式,按照CRC32算法预期应该输出0。

@maths644311798
Copy link
Contributor Author

明白了,我看了IEEE 802.3标准,确实首尾有取反操作。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants