Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created December 29, 2018 02:06
Show Gist options
  • Save charsyam/652fc538a29f4961710801a06dfa10c5 to your computer and use it in GitHub Desktop.
Save charsyam/652fc538a29f4961710801a06dfa10c5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <immintrin.h>
#include <omp.h>
#define READ_BINARY "rb"
#define CHUNK_SIZE (1024 * 1024 * 10)
unsigned int crc32c (unsigned char *buf, int len, unsigned int crc)
{
unsigned int i;
uint64_t * bp64 = (uint64_t *) buf;
for (i = 0; i < (len - (len % 8)) / 8; i++)
{
crc = _mm_crc32_u64 ((uint64_t) crc, bp64[i]);
}
for (i *= 8; i < len; i++)
{
crc = _mm_crc32_u8 (crc, buf[i]);
}
return crc;
}
int main(int argc, char *argv[]) {
struct stat st;
char *buf = malloc(CHUNK_SIZE);
char *filename = argv[1];
stat(filename, &st);
off_t fsize = st.st_size;
FILE *fp = fopen64(filename, READ_BINARY);
if (fp == NULL) {
printf("%s is not exist\n", filename);
}
size_t readed = 0;
size_t wread = 0;
unsigned int crc = 0;
while (fsize > 0) {
wread = fsize > CHUNK_SIZE ? CHUNK_SIZE : fsize;
readed = fread(buf, wread, 1, fp);
crc = crc32c((unsigned char*)buf, wread, 0);
fsize -= wread;
printf("%ld\n", crc);
}
fclose(fp);
free(buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment