1*c03c5b1cSMartin Matuska /* ****************************************************************** 2*c03c5b1cSMartin Matuska * hist : Histogram functions 3*c03c5b1cSMartin Matuska * part of Finite State Entropy project 4*c03c5b1cSMartin Matuska * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. 5*c03c5b1cSMartin Matuska * 6*c03c5b1cSMartin Matuska * You can contact the author at : 7*c03c5b1cSMartin Matuska * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy 8*c03c5b1cSMartin Matuska * - Public forum : https://groups.google.com/forum/#!forum/lz4c 9*c03c5b1cSMartin Matuska * 10*c03c5b1cSMartin Matuska * This source code is licensed under both the BSD-style license (found in the 11*c03c5b1cSMartin Matuska * LICENSE file in the root directory of this source tree) and the GPLv2 (found 12*c03c5b1cSMartin Matuska * in the COPYING file in the root directory of this source tree). 13*c03c5b1cSMartin Matuska * You may select, at your option, one of the above-listed licenses. 14*c03c5b1cSMartin Matuska ****************************************************************** */ 15*c03c5b1cSMartin Matuska 16*c03c5b1cSMartin Matuska /* --- dependencies --- */ 17*c03c5b1cSMartin Matuska #include <stddef.h> /* size_t */ 18*c03c5b1cSMartin Matuska 19*c03c5b1cSMartin Matuska 20*c03c5b1cSMartin Matuska /* --- simple histogram functions --- */ 21*c03c5b1cSMartin Matuska 22*c03c5b1cSMartin Matuska /*! HIST_count(): 23*c03c5b1cSMartin Matuska * Provides the precise count of each byte within a table 'count'. 24*c03c5b1cSMartin Matuska * 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). 25*c03c5b1cSMartin Matuska * Updates *maxSymbolValuePtr with actual largest symbol value detected. 26*c03c5b1cSMartin Matuska * @return : count of the most frequent symbol (which isn't identified). 27*c03c5b1cSMartin Matuska * or an error code, which can be tested using HIST_isError(). 28*c03c5b1cSMartin Matuska * note : if return == srcSize, there is only one symbol. 29*c03c5b1cSMartin Matuska */ 30*c03c5b1cSMartin Matuska size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr, 31*c03c5b1cSMartin Matuska const void* src, size_t srcSize); 32*c03c5b1cSMartin Matuska 33*c03c5b1cSMartin Matuska unsigned HIST_isError(size_t code); /**< tells if a return value is an error code */ 34*c03c5b1cSMartin Matuska 35*c03c5b1cSMartin Matuska 36*c03c5b1cSMartin Matuska /* --- advanced histogram functions --- */ 37*c03c5b1cSMartin Matuska 38*c03c5b1cSMartin Matuska #define HIST_WKSP_SIZE_U32 1024 39*c03c5b1cSMartin Matuska #define HIST_WKSP_SIZE (HIST_WKSP_SIZE_U32 * sizeof(unsigned)) 40*c03c5b1cSMartin Matuska /** HIST_count_wksp() : 41*c03c5b1cSMartin Matuska * Same as HIST_count(), but using an externally provided scratch buffer. 42*c03c5b1cSMartin Matuska * Benefit is this function will use very little stack space. 43*c03c5b1cSMartin Matuska * `workSpace` is a writable buffer which must be 4-bytes aligned, 44*c03c5b1cSMartin Matuska * `workSpaceSize` must be >= HIST_WKSP_SIZE 45*c03c5b1cSMartin Matuska */ 46*c03c5b1cSMartin Matuska size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr, 47*c03c5b1cSMartin Matuska const void* src, size_t srcSize, 48*c03c5b1cSMartin Matuska void* workSpace, size_t workSpaceSize); 49*c03c5b1cSMartin Matuska 50*c03c5b1cSMartin Matuska /** HIST_countFast() : 51*c03c5b1cSMartin Matuska * same as HIST_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr. 52*c03c5b1cSMartin Matuska * This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr` 53*c03c5b1cSMartin Matuska */ 54*c03c5b1cSMartin Matuska size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr, 55*c03c5b1cSMartin Matuska const void* src, size_t srcSize); 56*c03c5b1cSMartin Matuska 57*c03c5b1cSMartin Matuska /** HIST_countFast_wksp() : 58*c03c5b1cSMartin Matuska * Same as HIST_countFast(), but using an externally provided scratch buffer. 59*c03c5b1cSMartin Matuska * `workSpace` is a writable buffer which must be 4-bytes aligned, 60*c03c5b1cSMartin Matuska * `workSpaceSize` must be >= HIST_WKSP_SIZE 61*c03c5b1cSMartin Matuska */ 62*c03c5b1cSMartin Matuska size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr, 63*c03c5b1cSMartin Matuska const void* src, size_t srcSize, 64*c03c5b1cSMartin Matuska void* workSpace, size_t workSpaceSize); 65*c03c5b1cSMartin Matuska 66*c03c5b1cSMartin Matuska /*! HIST_count_simple() : 67*c03c5b1cSMartin Matuska * Same as HIST_countFast(), this function is unsafe, 68*c03c5b1cSMartin Matuska * and will segfault if any value within `src` is `> *maxSymbolValuePtr`. 69*c03c5b1cSMartin Matuska * It is also a bit slower for large inputs. 70*c03c5b1cSMartin Matuska * However, it does not need any additional memory (not even on stack). 71*c03c5b1cSMartin Matuska * @return : count of the most frequent symbol. 72*c03c5b1cSMartin Matuska * Note this function doesn't produce any error (i.e. it must succeed). 73*c03c5b1cSMartin Matuska */ 74*c03c5b1cSMartin Matuska unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, 75*c03c5b1cSMartin Matuska const void* src, size_t srcSize); 76