This commit is contained in:
Shengliang Guan 2022-02-28 10:40:14 +08:00
parent 9df1fed6ab
commit 0adfe3dc3d
3 changed files with 19 additions and 20 deletions

View File

@ -13,24 +13,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_UTIL_CHECKSUM_H #ifndef _TD_UTIL_CHECKSUM_H_
#define _TD_UTIL_CHECKSUM_H #define _TD_UTIL_CHECKSUM_H_
#include "tcrc32c.h"
#include "tutil.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "os.h"
#include "tcrc32c.h"
#include "tutil.h"
typedef uint32_t TSCKSUM; typedef uint32_t TSCKSUM;
static FORCE_INLINE TSCKSUM taosCalcChecksum(TSCKSUM csi, const uint8_t *stream, uint32_t ssize) { static FORCE_INLINE TSCKSUM taosCalcChecksum(TSCKSUM csi, const uint8_t *stream, uint32_t ssize) {
return (*crc32c)(csi, stream, (size_t)ssize); return (*crc32c)(csi, stream, (size_t)ssize);
} }
static FORCE_INLINE int taosCalcChecksumAppend(TSCKSUM csi, uint8_t *stream, uint32_t ssize) { static FORCE_INLINE int32_t taosCalcChecksumAppend(TSCKSUM csi, uint8_t *stream, uint32_t ssize) {
if (ssize < sizeof(TSCKSUM)) return -1; if (ssize < sizeof(TSCKSUM)) return -1;
*((TSCKSUM *)(stream + ssize - sizeof(TSCKSUM))) = (*crc32c)(csi, stream, (size_t)(ssize - sizeof(TSCKSUM))); *((TSCKSUM *)(stream + ssize - sizeof(TSCKSUM))) = (*crc32c)(csi, stream, (size_t)(ssize - sizeof(TSCKSUM)));
@ -38,11 +37,11 @@ static FORCE_INLINE int taosCalcChecksumAppend(TSCKSUM csi, uint8_t *stream, uin
return 0; return 0;
} }
static FORCE_INLINE int taosCheckChecksum(const uint8_t *stream, uint32_t ssize, TSCKSUM checksum) { static FORCE_INLINE int32_t taosCheckChecksum(const uint8_t *stream, uint32_t ssize, TSCKSUM checksum) {
return (checksum != (*crc32c)(0, stream, (size_t)ssize)); return (checksum != (*crc32c)(0, stream, (size_t)ssize));
} }
static FORCE_INLINE int taosCheckChecksumWhole(const uint8_t *stream, uint32_t ssize) { static FORCE_INLINE int32_t taosCheckChecksumWhole(const uint8_t *stream, uint32_t ssize) {
if (ssize < sizeof(TSCKSUM)) return 0; if (ssize < sizeof(TSCKSUM)) return 0;
#if (_WIN64) #if (_WIN64)
@ -56,4 +55,4 @@ static FORCE_INLINE int taosCheckChecksumWhole(const uint8_t *stream, uint32_t s
} }
#endif #endif
#endif /*_TD_UTIL_CHECKSUM_H*/ #endif /*_TD_UTIL_CHECKSUM_H_*/

View File

@ -18,8 +18,10 @@
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
#ifndef _TD_UTIL_CRC32_H #ifndef _TD_UTIL_CRC32_H_
#define _TD_UTIL_CRC32_H #define _TD_UTIL_CRC32_H_
#include "os.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -39,4 +41,4 @@ void taosResolveCRC();
} }
#endif #endif
#endif /*_TD_UTIL_CRC32_H*/ #endif /*_TD_UTIL_CRC32_H_*/

View File

@ -18,7 +18,7 @@
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
#include "os.h" #define _DEFAULT_SOURCE
#include "tcrc32c.h" #include "tcrc32c.h"
#include "tdef.h" #include "tdef.h"
@ -26,8 +26,6 @@
#include <nmmintrin.h> #include <nmmintrin.h>
#endif #endif
#define POLY 0x82f63b78 #define POLY 0x82f63b78
#define LONG_SHIFT 8192 #define LONG_SHIFT 8192
#define SHORT_SHIFT 256 #define SHORT_SHIFT 256
@ -1097,7 +1095,7 @@ static uint32_t short_shifts[4][256] = {
static uint32_t append_trivial(uint32_t crc, crc_stream input, size_t length) { static uint32_t append_trivial(uint32_t crc, crc_stream input, size_t length) {
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
crc = crc ^ input[i]; crc = crc ^ input[i];
for (int j = 0; j < 8; j++) for (int32_t j = 0; j < 8; j++)
crc = (crc >> 1) ^ 0x80000000 ^ ((~crc & 1) * POLY); crc = (crc >> 1) ^ 0x80000000 ^ ((~crc & 1) * POLY);
} }
return crc; return crc;
@ -1358,7 +1356,7 @@ void taosResolveCRC() {
#if defined _TD_ARM_ || defined _TD_MIPS_ || defined WINDOWS #if defined _TD_ARM_ || defined _TD_MIPS_ || defined WINDOWS
crc32c = crc32c_sf; crc32c = crc32c_sf;
#else #else
int sse42; int32_t sse42;
SSE42(sse42); SSE42(sse42);
crc32c = sse42 ? crc32c_hw : crc32c_sf; crc32c = sse42 ? crc32c_hw : crc32c_sf;
#endif #endif
@ -1371,10 +1369,10 @@ void taosResolveCRC() {
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
int main(int argc, char *argv[]) { int32_t main(int32_t argc, char *argv[]) {
char str[1024] = "\0"; char str[1024] = "\0";
char *ptr = str; char *ptr = str;
int count = 0; int32_t count = 0;
while ((count = read(0, ptr, 10)) > 0) { while ((count = read(0, ptr, 10)) > 0) {
ptr += count; ptr += count;
} }