Path: blob/main/sys/contrib/openzfs/module/zstd/lib/compress/hist.c
48774 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/* ******************************************************************2* hist : Histogram functions3* part of Finite State Entropy project4* Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.5*6* You can contact the author at :7* - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy8* - Public forum : https://groups.google.com/forum/#!forum/lz4c9*10* This source code is licensed under both the BSD-style license (found in the11* LICENSE file in the root directory of this source tree) and the GPLv2 (found12* in the COPYING file in the root directory of this source tree).13* You may select, at your option, one of the above-listed licenses.14****************************************************************** */1516/* --- dependencies --- */17#include "../common/mem.h" /* U32, BYTE, etc. */18#include "../common/debug.h" /* assert, DEBUGLOG */19#include "../common/error_private.h" /* ERROR */20#include "hist.h"212223/* --- Error management --- */24unsigned HIST_isError(size_t code) { return ERR_isError(code); }2526/*-**************************************************************27* Histogram functions28****************************************************************/29unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,30const void* src, size_t srcSize)31{32const BYTE* ip = (const BYTE*)src;33const BYTE* const end = ip + srcSize;34unsigned maxSymbolValue = *maxSymbolValuePtr;35unsigned largestCount=0;3637memset(count, 0, (maxSymbolValue+1) * sizeof(*count));38if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }3940while (ip<end) {41assert(*ip <= maxSymbolValue);42count[*ip++]++;43}4445while (!count[maxSymbolValue]) maxSymbolValue--;46*maxSymbolValuePtr = maxSymbolValue;4748{ U32 s;49for (s=0; s<=maxSymbolValue; s++)50if (count[s] > largestCount) largestCount = count[s];51}5253return largestCount;54}5556typedef enum { trustInput, checkMaxSymbolValue } HIST_checkInput_e;5758/* HIST_count_parallel_wksp() :59* store histogram into 4 intermediate tables, recombined at the end.60* this design makes better use of OoO cpus,61* and is noticeably faster when some values are heavily repeated.62* But it needs some additional workspace for intermediate tables.63* `workSpace` size must be a table of size >= HIST_WKSP_SIZE_U32.64* @return : largest histogram frequency,65* or an error code (notably when histogram would be larger than *maxSymbolValuePtr). */66static size_t HIST_count_parallel_wksp(67unsigned* count, unsigned* maxSymbolValuePtr,68const void* source, size_t sourceSize,69HIST_checkInput_e check,70U32* const workSpace)71{72const BYTE* ip = (const BYTE*)source;73const BYTE* const iend = ip+sourceSize;74unsigned maxSymbolValue = *maxSymbolValuePtr;75unsigned max=0;76U32* const Counting1 = workSpace;77U32* const Counting2 = Counting1 + 256;78U32* const Counting3 = Counting2 + 256;79U32* const Counting4 = Counting3 + 256;8081memset(workSpace, 0, 4*256*sizeof(unsigned));8283/* safety checks */84if (!sourceSize) {85memset(count, 0, maxSymbolValue + 1);86*maxSymbolValuePtr = 0;87return 0;88}89if (!maxSymbolValue) maxSymbolValue = 255; /* 0 == default */9091/* by stripes of 16 bytes */92{ U32 cached = MEM_read32(ip); ip += 4;93while (ip < iend-15) {94U32 c = cached; cached = MEM_read32(ip); ip += 4;95Counting1[(BYTE) c ]++;96Counting2[(BYTE)(c>>8) ]++;97Counting3[(BYTE)(c>>16)]++;98Counting4[ c>>24 ]++;99c = cached; cached = MEM_read32(ip); ip += 4;100Counting1[(BYTE) c ]++;101Counting2[(BYTE)(c>>8) ]++;102Counting3[(BYTE)(c>>16)]++;103Counting4[ c>>24 ]++;104c = cached; cached = MEM_read32(ip); ip += 4;105Counting1[(BYTE) c ]++;106Counting2[(BYTE)(c>>8) ]++;107Counting3[(BYTE)(c>>16)]++;108Counting4[ c>>24 ]++;109c = cached; cached = MEM_read32(ip); ip += 4;110Counting1[(BYTE) c ]++;111Counting2[(BYTE)(c>>8) ]++;112Counting3[(BYTE)(c>>16)]++;113Counting4[ c>>24 ]++;114}115ip-=4;116}117118/* finish last symbols */119while (ip<iend) Counting1[*ip++]++;120121if (check) { /* verify stats will fit into destination table */122U32 s; for (s=255; s>maxSymbolValue; s--) {123Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];124if (Counting1[s]) return ERROR(maxSymbolValue_tooSmall);125} }126127{ U32 s;128if (maxSymbolValue > 255) maxSymbolValue = 255;129for (s=0; s<=maxSymbolValue; s++) {130count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];131if (count[s] > max) max = count[s];132} }133134while (!count[maxSymbolValue]) maxSymbolValue--;135*maxSymbolValuePtr = maxSymbolValue;136return (size_t)max;137}138139/* HIST_countFast_wksp() :140* Same as HIST_countFast(), but using an externally provided scratch buffer.141* `workSpace` is a writable buffer which must be 4-bytes aligned,142* `workSpaceSize` must be >= HIST_WKSP_SIZE143*/144size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,145const void* source, size_t sourceSize,146void* workSpace, size_t workSpaceSize)147{148if (sourceSize < 1500) /* heuristic threshold */149return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize);150if ((size_t)workSpace & 3) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */151if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);152return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, trustInput, (U32*)workSpace);153}154155/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */156size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr,157const void* source, size_t sourceSize)158{159unsigned tmpCounters[HIST_WKSP_SIZE_U32];160return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters, sizeof(tmpCounters));161}162163/* HIST_count_wksp() :164* Same as HIST_count(), but using an externally provided scratch buffer.165* `workSpace` size must be table of >= HIST_WKSP_SIZE_U32 unsigned */166size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,167const void* source, size_t sourceSize,168void* workSpace, size_t workSpaceSize)169{170if ((size_t)workSpace & 3) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */171if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);172if (*maxSymbolValuePtr < 255)173return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, checkMaxSymbolValue, (U32*)workSpace);174*maxSymbolValuePtr = 255;175return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize);176}177178size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr,179const void* src, size_t srcSize)180{181unsigned tmpCounters[HIST_WKSP_SIZE_U32];182return HIST_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters, sizeof(tmpCounters));183}184185186