MCPcopy Create free account
hub / github.com/F-Stack/f-stack / FSE_normalizeCount

Function FSE_normalizeCount

freebsd/contrib/openzfs/module/zstd/lib/zstd.c:7914–7973  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7912
7913
7914size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
7915 const unsigned* count, size_t total,
7916 unsigned maxSymbolValue)
7917{
7918 /* Sanity checks */
7919 if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
7920 if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC); /* Unsupported size */
7921 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported size */
7922 if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */
7923
7924 { static U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
7925 U64 const scale = 62 - tableLog;
7926 U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */
7927 U64 const vStep = 1ULL<<(scale-20);
7928 int stillToDistribute = 1<<tableLog;
7929 unsigned s;
7930 unsigned largest=0;
7931 short largestP=0;
7932 U32 lowThreshold = (U32)(total >> tableLog);
7933
7934 for (s=0; s<=maxSymbolValue; s++) {
7935 if (count[s] == total) return 0; /* rle special case */
7936 if (count[s] == 0) { normalizedCounter[s]=0; continue; }
7937 if (count[s] <= lowThreshold) {
7938 normalizedCounter[s] = -1;
7939 stillToDistribute--;
7940 } else {
7941 short proba = (short)((count[s]*step) >> scale);
7942 if (proba<8) {
7943 U64 restToBeat = vStep * rtbTable[proba];
7944 proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
7945 }
7946 if (proba > largestP) { largestP=proba; largest=s; }
7947 normalizedCounter[s] = proba;
7948 stillToDistribute -= proba;
7949 } }
7950 if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
7951 /* corner case, need another normalization method */
7952 size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
7953 if (FSE_isError(errorCode)) return errorCode;
7954 }
7955 else normalizedCounter[largest] += (short)stillToDistribute;
7956 }
7957
7958#if 0
7959 { /* Print Table (debug) */
7960 U32 s;
7961 U32 nTotal = 0;
7962 for (s=0; s<=maxSymbolValue; s++)
7963 RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]);
7964 for (s=0; s<=maxSymbolValue; s++)
7965 nTotal += abs(normalizedCounter[s]);
7966 if (nTotal != (1U<<tableLog))
7967 RAWLOG(2, "Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
7968 getchar();
7969 }
7970#endif
7971

Callers 4

FSE_compress_wkspFunction · 0.70
HUF_compressWeightsFunction · 0.70
ZSTD_NCountCostFunction · 0.70
ZSTD_buildCTableFunction · 0.70

Calls 4

absFunction · 0.85
FSE_minTableLogFunction · 0.70
FSE_normalizeM2Function · 0.70
FSE_isErrorFunction · 0.70

Tested by

no test coverage detected