]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[lib] Replace 64-bit divisions with ZSTD_div64()
authorNick Terrell <terrelln@fb.com>
Thu, 27 Aug 2020 22:01:18 +0000 (15:01 -0700)
committerNick Terrell <terrelln@fb.com>
Wed, 9 Sep 2020 21:35:39 +0000 (14:35 -0700)
contrib/single_file_libs/zstd-in.c
lib/common/zstd_deps.h
lib/compress/fse_compress.c

index 7e1d68fa77f5800d612b02103112aafc8caad797..4f0fb56a2e7fc45d69956f926386e35916841592 100644 (file)
@@ -45,6 +45,7 @@
 
 /* Include zstd_deps.h first with all the options we need enabled. */
 #define ZSTD_DEPS_NEED_MALLOC
+#define ZSTD_DEPS_NEED_MATH64
 #include "common/zstd_deps.h"
 
 #include "common/debug.c"
index e7b8a467644c4d953bb52366db5ebca42476c9e8..3b4db18765c5843d4dc36c2c7cea08154f6adff7 100644 (file)
 #endif /* ZSTD_DEPS_MALLOC */
 #endif /* ZSTD_DEPS_NEED_MALLOC */
 
+/*
+ * Provides 64-bit math support.
+ * Need:
+ * U64 ZSTD_div64(U64 dividend, U32 divisor)
+ */
+#ifdef ZSTD_DEPS_NEED_MATH64
+#ifndef ZSTD_DEPS_MATH64
+#define ZSTD_DEPS_MATH64
+
+static U64 ZSTD_div64(U64 dividend, U32 divisor) {
+  return dividend / divisor;
+}
+
+#endif /* ZSTD_DEPS_MATH64 */
+#endif /* ZSTD_DEPS_NEED_MATH64 */
+
 /* Need:
  * assert()
  */
index a2622c5e38fbbf160bd77922065ed9f88a827dcd..8b6ddc196f9053cbce5a3f0ed77d8d99677892ad 100644 (file)
@@ -24,6 +24,7 @@
 #include "../common/fse.h"
 #include "../common/error_private.h"
 #define ZSTD_DEPS_NEED_MALLOC
+#define ZSTD_DEPS_NEED_MATH64
 #include "../common/zstd_deps.h"  /* ZSTD_malloc, ZSTD_free, ZSTD_memcpy, ZSTD_memset */
 
 
@@ -415,7 +416,7 @@ static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count,
 
     {   U64 const vStepLog = 62 - tableLog;
         U64 const mid = (1ULL << (vStepLog-1)) - 1;
-        U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total;   /* scale on remaining */
+        U64 const rStep = ZSTD_div64((((U64)1<<vStepLog) * ToDistribute) + mid, (U32)total);   /* scale on remaining */
         U64 tmpTotal = mid;
         for (s=0; s<=maxSymbolValue; s++) {
             if (norm[s]==NOT_YET_ASSIGNED) {
@@ -445,7 +446,7 @@ size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
     {   static U32 const rtbTable[] = {     0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
         short const lowProbCount = useLowProbCount ? -1 : 1;
         U64 const scale = 62 - tableLog;
-        U64 const step = ((U64)1<<62) / total;   /* <== here, one division ! */
+        U64 const step = ZSTD_div64((U64)1<<62, (U32)total);   /* <== here, one division ! */
         U64 const vStep = 1ULL<<(scale-20);
         int stillToDistribute = 1<<tableLog;
         unsigned s;