David Marec 9 bulan lalu
induk
melakukan
c79b2cd4a1
2 mengubah file dengan 32 tambahan dan 9 penghapusan
  1. 1 1
      crc32/Makefile
  2. 31 8
      crc32/crc32c.c

+ 1 - 1
crc32/Makefile

@@ -4,7 +4,7 @@ CFLAGS+=-Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3
 CFLAGS+=-fstack-clash-protection -fstack-protector-strong
 CFLAGS+=-fstrict-flex-arrays=3 -msse4.2
 
-LDFLAGS+=-msse4.2 -Wl,-O3,--sort-common,--as-needed,-z,relro,-z,now,--strip-all -pie
+LDFLAGS+=-lutil -msse4.2 -Wl,-O3,--sort-common,--as-needed,-z,relro,-z,now,--strip-all -pie
 LDFLAGS+=-Wl,-z,nodlopen -Wl,-z,noexecstack,--no-copy-dt-needed-entries
 
 PROG=crc32c

+ 31 - 8
crc32/crc32c.c

@@ -8,6 +8,7 @@
 #include <getopt.h>
 #include <nmmintrin.h>
 #include <time.h>
+#include <libutil.h>
 
 #define POLY 0x82f63b78
 
@@ -43,8 +44,8 @@ uint32_t crc32c(const char *data, size_t length)
 {
 	uint32_t crc = ~(0U);
 	 
-	printf("Software computing:");
-	printf("------------------");
+	printf("Software computing:\n");
+	printf("------------------\n");
 
     while (length--) { 
         crc = crc32c_poly[(crc ^ (uint32_t)(*data++)) & 0xFFL] ^ (crc >> 8); 
@@ -62,8 +63,8 @@ uint32_t crc32c_hw(const char *data, size_t length)
 {
 	uint32_t crc = ~(0U);
 	 
-	printf("Hardware computing:");
-	printf("------------------");
+	printf("Hardware computing:\n");
+	printf("------------------\n");
     while (length--) { 
 		crc = _mm_crc32_u8(crc, (unsigned char)(*data++));
     }
@@ -94,6 +95,25 @@ void file_close(FILE **fd)
 }
 
 
+/*
+ * Print human readable numbers
+ *
+ * @width: spaces
+ * @bytes: value in bytes
+ */
+static void printsize(size_t width, off_t bytes)
+{
+	/*
+	 * Reserve one space before the size and allocate room for
+	 * the trailing '\0'.
+	 */
+	char buf[5];
+
+	humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
+			HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
+	(void)printf("%*s", (u_int)width, buf);
+}
+
 int main(int argc, char *argv[])
 {
 	static struct option longopts[] = {
@@ -133,7 +153,9 @@ int main(int argc, char *argv[])
 	struct stat statbuf = { 0 };
 	fstat(fd, & statbuf);
 
-	printf("Parsing %zu bytes.\n", statbuf.st_size);
+	printf("Parsing");
+	printsize(8, statbuf.st_size);
+	puts(".\n");
 
 	char *addr = (char *)mmap(NULL, (size_t)statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 
@@ -147,13 +169,14 @@ int main(int argc, char *argv[])
 	t0 = clock();
 	uint32_t c = crc32c(addr, (size_t)statbuf.st_size);
 	t1 = clock();
-	printf("CRC32C => %08x: %d\n", c, (t1 - t0) * CLOCKS_PER_SEC);
+	printf("CRC32C => %08x: %6.3f ms\n", c, 1000.0 * (double)(t1 - t0) / CLOCKS_PER_SEC);
+	printf("\n");
 	c = crc32c_hw(addr, (size_t)statbuf.st_size);
 	t0 = clock();
-	printf("CRC32C => %08x: %d\n", c, (t0 - t1) * CLOCKS_PER_SEC);
+	printf("CRC32C => %08x: %6.3f ms\n", c, 1000.0 * (double)(t0 - t1) / CLOCKS_PER_SEC);
 
 	munmap(addr, (size_t)statbuf.st_size);
 
-	return EXIT_SUCESS;
+	return EXIT_SUCCESS;
 }