/* ******************************************************************1* hist : Histogram functions2* part of Finite State Entropy project3* Copyright (c) Meta Platforms, Inc. and affiliates.4*5* You can contact the author at :6* - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy7* - Public forum : https://groups.google.com/forum/#!forum/lz4c8*9* This source code is licensed under both the BSD-style license (found in the10* LICENSE file in the root directory of this source tree) and the GPLv2 (found11* in the COPYING file in the root directory of this source tree).12* You may select, at your option, one of the above-listed licenses.13****************************************************************** */1415/* --- dependencies --- */16#include "../common/zstd_deps.h" /* size_t */171819/* --- simple histogram functions --- */2021/*! HIST_count():22* Provides the precise count of each byte within a table 'count'.23* 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1).24* Updates *maxSymbolValuePtr with actual largest symbol value detected.25* @return : count of the most frequent symbol (which isn't identified).26* or an error code, which can be tested using HIST_isError().27* note : if return == srcSize, there is only one symbol.28*/29size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr,30const void* src, size_t srcSize);3132unsigned HIST_isError(size_t code); /**< tells if a return value is an error code */333435/* --- advanced histogram functions --- */3637#define HIST_WKSP_SIZE_U32 102438#define HIST_WKSP_SIZE (HIST_WKSP_SIZE_U32 * sizeof(unsigned))39/** HIST_count_wksp() :40* Same as HIST_count(), but using an externally provided scratch buffer.41* Benefit is this function will use very little stack space.42* `workSpace` is a writable buffer which must be 4-bytes aligned,43* `workSpaceSize` must be >= HIST_WKSP_SIZE44*/45size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,46const void* src, size_t srcSize,47void* workSpace, size_t workSpaceSize);4849/** HIST_countFast() :50* same as HIST_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr.51* This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr`52*/53size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr,54const void* src, size_t srcSize);5556/** HIST_countFast_wksp() :57* Same as HIST_countFast(), but using an externally provided scratch buffer.58* `workSpace` is a writable buffer which must be 4-bytes aligned,59* `workSpaceSize` must be >= HIST_WKSP_SIZE60*/61size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,62const void* src, size_t srcSize,63void* workSpace, size_t workSpaceSize);6465/*! HIST_count_simple() :66* Same as HIST_countFast(), this function is unsafe,67* and will segfault if any value within `src` is `> *maxSymbolValuePtr`.68* It is also a bit slower for large inputs.69* However, it does not need any additional memory (not even on stack).70* @return : count of the most frequent symbol.71* Note this function doesn't produce any error (i.e. it must succeed).72*/73unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,74const void* src, size_t srcSize);757677