[lib] Replace 64-bit divisions with ZSTD_div64()

This commit is contained in:
Nick Terrell 2020-08-27 15:01:18 -07:00
parent a90779397a
commit ac3a136b0a
3 changed files with 20 additions and 2 deletions

View File

@ -45,6 +45,7 @@
/* Include zstd_deps.h first with all the options we need enabled. */ /* Include zstd_deps.h first with all the options we need enabled. */
#define ZSTD_DEPS_NEED_MALLOC #define ZSTD_DEPS_NEED_MALLOC
#define ZSTD_DEPS_NEED_MATH64
#include "common/zstd_deps.h" #include "common/zstd_deps.h"
#include "common/debug.c" #include "common/debug.c"

View File

@ -93,6 +93,22 @@
#endif /* ZSTD_DEPS_MALLOC */ #endif /* ZSTD_DEPS_MALLOC */
#endif /* ZSTD_DEPS_NEED_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: /* Need:
* assert() * assert()
*/ */

View File

@ -24,6 +24,7 @@
#include "../common/fse.h" #include "../common/fse.h"
#include "../common/error_private.h" #include "../common/error_private.h"
#define ZSTD_DEPS_NEED_MALLOC #define ZSTD_DEPS_NEED_MALLOC
#define ZSTD_DEPS_NEED_MATH64
#include "../common/zstd_deps.h" /* ZSTD_malloc, ZSTD_free, ZSTD_memcpy, ZSTD_memset */ #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 vStepLog = 62 - tableLog;
U64 const mid = (1ULL << (vStepLog-1)) - 1; 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; U64 tmpTotal = mid;
for (s=0; s<=maxSymbolValue; s++) { for (s=0; s<=maxSymbolValue; s++) {
if (norm[s]==NOT_YET_ASSIGNED) { 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 }; { static U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
short const lowProbCount = useLowProbCount ? -1 : 1; short const lowProbCount = useLowProbCount ? -1 : 1;
U64 const scale = 62 - tableLog; 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); U64 const vStep = 1ULL<<(scale-20);
int stillToDistribute = 1<<tableLog; int stillToDistribute = 1<<tableLog;
unsigned s; unsigned s;