hist.h 3.7 KB

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