xref: /freebsd-src/sys/contrib/zstd/lib/compress/hist.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
10f743729SConrad Meyer /* ******************************************************************
237f1f268SConrad Meyer  * hist : Histogram functions
337f1f268SConrad Meyer  * part of Finite State Entropy project
4*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
537f1f268SConrad Meyer  *
637f1f268SConrad Meyer  *  You can contact the author at :
737f1f268SConrad Meyer  *  - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
837f1f268SConrad Meyer  *  - Public forum : https://groups.google.com/forum/#!forum/lz4c
937f1f268SConrad Meyer  *
1037f1f268SConrad Meyer  * This source code is licensed under both the BSD-style license (found in the
1137f1f268SConrad Meyer  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
1237f1f268SConrad Meyer  * in the COPYING file in the root directory of this source tree).
1337f1f268SConrad Meyer  * You may select, at your option, one of the above-listed licenses.
140f743729SConrad Meyer ****************************************************************** */
150f743729SConrad Meyer 
160f743729SConrad Meyer /* --- dependencies --- */
17f7cd7fe5SConrad Meyer #include "../common/zstd_deps.h"   /* size_t */
180f743729SConrad Meyer 
190f743729SConrad Meyer 
200f743729SConrad Meyer /* --- simple histogram functions --- */
210f743729SConrad Meyer 
220f743729SConrad Meyer /*! HIST_count():
230f743729SConrad Meyer  *  Provides the precise count of each byte within a table 'count'.
240f743729SConrad Meyer  * 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1).
250f743729SConrad Meyer  *  Updates *maxSymbolValuePtr with actual largest symbol value detected.
260f743729SConrad Meyer  * @return : count of the most frequent symbol (which isn't identified).
270f743729SConrad Meyer  *           or an error code, which can be tested using HIST_isError().
280f743729SConrad Meyer  *           note : if return == srcSize, there is only one symbol.
290f743729SConrad Meyer  */
300f743729SConrad Meyer size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr,
310f743729SConrad Meyer                   const void* src, size_t srcSize);
320f743729SConrad Meyer 
330f743729SConrad Meyer unsigned HIST_isError(size_t code);  /**< tells if a return value is an error code */
340f743729SConrad Meyer 
350f743729SConrad Meyer 
360f743729SConrad Meyer /* --- advanced histogram functions --- */
370f743729SConrad Meyer 
380f743729SConrad Meyer #define HIST_WKSP_SIZE_U32 1024
39a0483764SConrad Meyer #define HIST_WKSP_SIZE    (HIST_WKSP_SIZE_U32 * sizeof(unsigned))
400f743729SConrad Meyer /** HIST_count_wksp() :
410f743729SConrad Meyer  *  Same as HIST_count(), but using an externally provided scratch buffer.
420f743729SConrad Meyer  *  Benefit is this function will use very little stack space.
43a0483764SConrad Meyer  * `workSpace` is a writable buffer which must be 4-bytes aligned,
44a0483764SConrad Meyer  * `workSpaceSize` must be >= HIST_WKSP_SIZE
450f743729SConrad Meyer  */
460f743729SConrad Meyer size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
470f743729SConrad Meyer                        const void* src, size_t srcSize,
48a0483764SConrad Meyer                        void* workSpace, size_t workSpaceSize);
490f743729SConrad Meyer 
500f743729SConrad Meyer /** HIST_countFast() :
510f743729SConrad Meyer  *  same as HIST_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr.
520f743729SConrad Meyer  *  This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr`
530f743729SConrad Meyer  */
540f743729SConrad Meyer size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr,
550f743729SConrad Meyer                       const void* src, size_t srcSize);
560f743729SConrad Meyer 
570f743729SConrad Meyer /** HIST_countFast_wksp() :
580f743729SConrad Meyer  *  Same as HIST_countFast(), but using an externally provided scratch buffer.
59a0483764SConrad Meyer  * `workSpace` is a writable buffer which must be 4-bytes aligned,
60a0483764SConrad Meyer  * `workSpaceSize` must be >= HIST_WKSP_SIZE
610f743729SConrad Meyer  */
620f743729SConrad Meyer size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
630f743729SConrad Meyer                            const void* src, size_t srcSize,
64a0483764SConrad Meyer                            void* workSpace, size_t workSpaceSize);
650f743729SConrad Meyer 
660f743729SConrad Meyer /*! HIST_count_simple() :
670f743729SConrad Meyer  *  Same as HIST_countFast(), this function is unsafe,
680f743729SConrad Meyer  *  and will segfault if any value within `src` is `> *maxSymbolValuePtr`.
690f743729SConrad Meyer  *  It is also a bit slower for large inputs.
700f743729SConrad Meyer  *  However, it does not need any additional memory (not even on stack).
710f743729SConrad Meyer  * @return : count of the most frequent symbol.
720f743729SConrad Meyer  *  Note this function doesn't produce any error (i.e. it must succeed).
730f743729SConrad Meyer  */
740f743729SConrad Meyer unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
750f743729SConrad Meyer                            const void* src, size_t srcSize);
76