18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * FSE : Finite State Entropy codec 38c2ecf20Sopenharmony_ci * Public Prototypes declaration 48c2ecf20Sopenharmony_ci * Copyright (C) 2013-2016, Yann Collet. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are 108c2ecf20Sopenharmony_ci * met: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * * Redistributions of source code must retain the above copyright 138c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 148c2ecf20Sopenharmony_ci * * Redistributions in binary form must reproduce the above 158c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following disclaimer 168c2ecf20Sopenharmony_ci * in the documentation and/or other materials provided with the 178c2ecf20Sopenharmony_ci * distribution. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 208c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 218c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 228c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 238c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 248c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 258c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 268c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 278c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 288c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 298c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 308c2ecf20Sopenharmony_ci * 318c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it under 328c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2 as published by the 338c2ecf20Sopenharmony_ci * Free Software Foundation. This program is dual-licensed; you may select 348c2ecf20Sopenharmony_ci * either version 2 of the GNU General Public License ("GPL") or BSD license 358c2ecf20Sopenharmony_ci * ("BSD"). 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * You can contact the author at : 388c2ecf20Sopenharmony_ci * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 398c2ecf20Sopenharmony_ci */ 408c2ecf20Sopenharmony_ci#ifndef FSE_H 418c2ecf20Sopenharmony_ci#define FSE_H 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/*-***************************************** 448c2ecf20Sopenharmony_ci* Dependencies 458c2ecf20Sopenharmony_ci******************************************/ 468c2ecf20Sopenharmony_ci#include <linux/types.h> /* size_t, ptrdiff_t */ 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/*-***************************************** 498c2ecf20Sopenharmony_ci* FSE_PUBLIC_API : control library symbols visibility 508c2ecf20Sopenharmony_ci******************************************/ 518c2ecf20Sopenharmony_ci#define FSE_PUBLIC_API 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/*------ Version ------*/ 548c2ecf20Sopenharmony_ci#define FSE_VERSION_MAJOR 0 558c2ecf20Sopenharmony_ci#define FSE_VERSION_MINOR 9 568c2ecf20Sopenharmony_ci#define FSE_VERSION_RELEASE 0 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE 598c2ecf20Sopenharmony_ci#define FSE_QUOTE(str) #str 608c2ecf20Sopenharmony_ci#define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str) 618c2ecf20Sopenharmony_ci#define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR * 100 * 100 + FSE_VERSION_MINOR * 100 + FSE_VERSION_RELEASE) 648c2ecf20Sopenharmony_ciFSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/*-***************************************** 678c2ecf20Sopenharmony_ci* Tool functions 688c2ecf20Sopenharmony_ci******************************************/ 698c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* Error Management */ 728c2ecf20Sopenharmony_ciFSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci/*-***************************************** 758c2ecf20Sopenharmony_ci* FSE detailed API 768c2ecf20Sopenharmony_ci******************************************/ 778c2ecf20Sopenharmony_ci/*! 788c2ecf20Sopenharmony_ciFSE_compress() does the following: 798c2ecf20Sopenharmony_ci1. count symbol occurrence from source[] into table count[] 808c2ecf20Sopenharmony_ci2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) 818c2ecf20Sopenharmony_ci3. save normalized counters to memory buffer using writeNCount() 828c2ecf20Sopenharmony_ci4. build encoding table 'CTable' from normalized counters 838c2ecf20Sopenharmony_ci5. encode the data stream using encoding table 'CTable' 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ciFSE_decompress() does the following: 868c2ecf20Sopenharmony_ci1. read normalized counters with readNCount() 878c2ecf20Sopenharmony_ci2. build decoding table 'DTable' from normalized counters 888c2ecf20Sopenharmony_ci3. decode the data stream using decoding table 'DTable' 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciThe following API allows targeting specific sub-functions for advanced tasks. 918c2ecf20Sopenharmony_ciFor example, it's possible to compress several blocks using the same 'CTable', 928c2ecf20Sopenharmony_cior to save and provide normalized distribution using external method. 938c2ecf20Sopenharmony_ci*/ 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/* *** COMPRESSION *** */ 968c2ecf20Sopenharmony_ci/*! FSE_optimalTableLog(): 978c2ecf20Sopenharmony_ci dynamically downsize 'tableLog' when conditions are met. 988c2ecf20Sopenharmony_ci It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. 998c2ecf20Sopenharmony_ci @return : recommended tableLog (necessarily <= 'maxTableLog') */ 1008c2ecf20Sopenharmony_ciFSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci/*! FSE_normalizeCount(): 1038c2ecf20Sopenharmony_ci normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) 1048c2ecf20Sopenharmony_ci 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). 1058c2ecf20Sopenharmony_ci @return : tableLog, 1068c2ecf20Sopenharmony_ci or an errorCode, which can be tested using FSE_isError() */ 1078c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_normalizeCount(short *normalizedCounter, unsigned tableLog, const unsigned *count, size_t srcSize, unsigned maxSymbolValue); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/*! FSE_NCountWriteBound(): 1108c2ecf20Sopenharmony_ci Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'. 1118c2ecf20Sopenharmony_ci Typically useful for allocation purpose. */ 1128c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/*! FSE_writeNCount(): 1158c2ecf20Sopenharmony_ci Compactly save 'normalizedCounter' into 'buffer'. 1168c2ecf20Sopenharmony_ci @return : size of the compressed table, 1178c2ecf20Sopenharmony_ci or an errorCode, which can be tested using FSE_isError(). */ 1188c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_writeNCount(void *buffer, size_t bufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/*! Constructor and Destructor of FSE_CTable. 1218c2ecf20Sopenharmony_ci Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ 1228c2ecf20Sopenharmony_citypedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/*! FSE_compress_usingCTable(): 1258c2ecf20Sopenharmony_ci Compress `src` using `ct` into `dst` which must be already allocated. 1268c2ecf20Sopenharmony_ci @return : size of compressed data (<= `dstCapacity`), 1278c2ecf20Sopenharmony_ci or 0 if compressed data could not fit into `dst`, 1288c2ecf20Sopenharmony_ci or an errorCode, which can be tested using FSE_isError() */ 1298c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_compress_usingCTable(void *dst, size_t dstCapacity, const void *src, size_t srcSize, const FSE_CTable *ct); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/*! 1328c2ecf20Sopenharmony_ciTutorial : 1338c2ecf20Sopenharmony_ci---------- 1348c2ecf20Sopenharmony_ciThe first step is to count all symbols. FSE_count() does this job very fast. 1358c2ecf20Sopenharmony_ciResult will be saved into 'count', a table of unsigned int, which must be already allocated, and have 'maxSymbolValuePtr[0]+1' cells. 1368c2ecf20Sopenharmony_ci'src' is a table of bytes of size 'srcSize'. All values within 'src' MUST be <= maxSymbolValuePtr[0] 1378c2ecf20Sopenharmony_cimaxSymbolValuePtr[0] will be updated, with its real value (necessarily <= original value) 1388c2ecf20Sopenharmony_ciFSE_count() will return the number of occurrence of the most frequent symbol. 1398c2ecf20Sopenharmony_ciThis can be used to know if there is a single symbol within 'src', and to quickly evaluate its compressibility. 1408c2ecf20Sopenharmony_ciIf there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciThe next step is to normalize the frequencies. 1438c2ecf20Sopenharmony_ciFSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'. 1448c2ecf20Sopenharmony_ciIt also guarantees a minimum of 1 to any Symbol with frequency >= 1. 1458c2ecf20Sopenharmony_ciYou can use 'tableLog'==0 to mean "use default tableLog value". 1468c2ecf20Sopenharmony_ciIf you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(), 1478c2ecf20Sopenharmony_ciwhich will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined maximum (0 means "default"). 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciThe result of FSE_normalizeCount() will be saved into a table, 1508c2ecf20Sopenharmony_cicalled 'normalizedCounter', which is a table of signed short. 1518c2ecf20Sopenharmony_ci'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells. 1528c2ecf20Sopenharmony_ciThe return value is tableLog if everything proceeded as expected. 1538c2ecf20Sopenharmony_ciIt is 0 if there is a single symbol within distribution. 1548c2ecf20Sopenharmony_ciIf there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can be tested using FSE_isError()). 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount(). 1578c2ecf20Sopenharmony_ci'buffer' must be already allocated. 1588c2ecf20Sopenharmony_ciFor guaranteed success, buffer size must be at least FSE_headerBound(). 1598c2ecf20Sopenharmony_ciThe result of the function is the number of bytes written into 'buffer'. 1608c2ecf20Sopenharmony_ciIf there is an error, the function will return an ErrorCode (which can be tested using FSE_isError(); ex : buffer size too small). 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci'normalizedCounter' can then be used to create the compression table 'CTable'. 1638c2ecf20Sopenharmony_ciThe space required by 'CTable' must be already allocated, using FSE_createCTable(). 1648c2ecf20Sopenharmony_ciYou can then use FSE_buildCTable() to fill 'CTable'. 1658c2ecf20Sopenharmony_ciIf there is an error, both functions will return an ErrorCode (which can be tested using FSE_isError()). 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). 1688c2ecf20Sopenharmony_ciSimilar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize' 1698c2ecf20Sopenharmony_ciThe function returns the size of compressed data (without header), necessarily <= `dstCapacity`. 1708c2ecf20Sopenharmony_ciIf it returns '0', compressed data could not fit into 'dst'. 1718c2ecf20Sopenharmony_ciIf there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). 1728c2ecf20Sopenharmony_ci*/ 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci/* *** DECOMPRESSION *** */ 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci/*! FSE_readNCount(): 1778c2ecf20Sopenharmony_ci Read compactly saved 'normalizedCounter' from 'rBuffer'. 1788c2ecf20Sopenharmony_ci @return : size read from 'rBuffer', 1798c2ecf20Sopenharmony_ci or an errorCode, which can be tested using FSE_isError(). 1808c2ecf20Sopenharmony_ci maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ 1818c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSymbolValuePtr, unsigned *tableLogPtr, const void *rBuffer, size_t rBuffSize); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/*! Constructor and Destructor of FSE_DTable. 1848c2ecf20Sopenharmony_ci Note that its size depends on 'tableLog' */ 1858c2ecf20Sopenharmony_citypedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci/*! FSE_buildDTable(): 1888c2ecf20Sopenharmony_ci Builds 'dt', which must be already allocated, using FSE_createDTable(). 1898c2ecf20Sopenharmony_ci return : 0, or an errorCode, which can be tested using FSE_isError() */ 1908c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci/*! FSE_decompress_usingDTable(): 1938c2ecf20Sopenharmony_ci Decompress compressed source `cSrc` of size `cSrcSize` using `dt` 1948c2ecf20Sopenharmony_ci into `dst` which must be already allocated. 1958c2ecf20Sopenharmony_ci @return : size of regenerated data (necessarily <= `dstCapacity`), 1968c2ecf20Sopenharmony_ci or an errorCode, which can be tested using FSE_isError() */ 1978c2ecf20Sopenharmony_ciFSE_PUBLIC_API size_t FSE_decompress_usingDTable(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci/*! 2008c2ecf20Sopenharmony_ciTutorial : 2018c2ecf20Sopenharmony_ci---------- 2028c2ecf20Sopenharmony_ci(Note : these functions only decompress FSE-compressed blocks. 2038c2ecf20Sopenharmony_ci If block is uncompressed, use memcpy() instead 2048c2ecf20Sopenharmony_ci If block is a single repeated byte, use memset() instead ) 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ciThe first step is to obtain the normalized frequencies of symbols. 2078c2ecf20Sopenharmony_ciThis can be performed by FSE_readNCount() if it was saved using FSE_writeNCount(). 2088c2ecf20Sopenharmony_ci'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short. 2098c2ecf20Sopenharmony_ciIn practice, that means it's necessary to know 'maxSymbolValue' beforehand, 2108c2ecf20Sopenharmony_cior size the table to handle worst case situations (typically 256). 2118c2ecf20Sopenharmony_ciFSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'. 2128c2ecf20Sopenharmony_ciThe result of FSE_readNCount() is the number of bytes read from 'rBuffer'. 2138c2ecf20Sopenharmony_ciNote that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that. 2148c2ecf20Sopenharmony_ciIf there is an error, the function will return an error code, which can be tested using FSE_isError(). 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ciThe next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'. 2178c2ecf20Sopenharmony_ciThis is performed by the function FSE_buildDTable(). 2188c2ecf20Sopenharmony_ciThe space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). 2198c2ecf20Sopenharmony_ciIf there is an error, the function will return an error code, which can be tested using FSE_isError(). 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci`FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable(). 2228c2ecf20Sopenharmony_ci`cSrcSize` must be strictly correct, otherwise decompression will fail. 2238c2ecf20Sopenharmony_ciFSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`). 2248c2ecf20Sopenharmony_ciIf there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small) 2258c2ecf20Sopenharmony_ci*/ 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci/* *** Dependency *** */ 2288c2ecf20Sopenharmony_ci#include "bitstream.h" 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci/* ***************************************** 2318c2ecf20Sopenharmony_ci* Static allocation 2328c2ecf20Sopenharmony_ci*******************************************/ 2338c2ecf20Sopenharmony_ci/* FSE buffer bounds */ 2348c2ecf20Sopenharmony_ci#define FSE_NCOUNTBOUND 512 2358c2ecf20Sopenharmony_ci#define FSE_BLOCKBOUND(size) (size + (size >> 7)) 2368c2ecf20Sopenharmony_ci#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci/* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */ 2398c2ecf20Sopenharmony_ci#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1 << (maxTableLog - 1)) + ((maxSymbolValue + 1) * 2)) 2408c2ecf20Sopenharmony_ci#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1 << maxTableLog)) 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci/* ***************************************** 2438c2ecf20Sopenharmony_ci* FSE advanced API 2448c2ecf20Sopenharmony_ci*******************************************/ 2458c2ecf20Sopenharmony_ci/* FSE_count_wksp() : 2468c2ecf20Sopenharmony_ci * Same as FSE_count(), but using an externally provided scratch buffer. 2478c2ecf20Sopenharmony_ci * `workSpace` size must be table of >= `1024` unsigned 2488c2ecf20Sopenharmony_ci */ 2498c2ecf20Sopenharmony_cisize_t FSE_count_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, unsigned *workSpace); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci/* FSE_countFast_wksp() : 2528c2ecf20Sopenharmony_ci * Same as FSE_countFast(), but using an externally provided scratch buffer. 2538c2ecf20Sopenharmony_ci * `workSpace` must be a table of minimum `1024` unsigned 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_cisize_t FSE_countFast_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize, unsigned *workSpace); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci/*! FSE_count_simple 2588c2ecf20Sopenharmony_ci * Same as FSE_countFast(), but does not use any additional memory (not even on stack). 2598c2ecf20Sopenharmony_ci * This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr` (presuming it's also the size of `count`). 2608c2ecf20Sopenharmony_ci*/ 2618c2ecf20Sopenharmony_cisize_t FSE_count_simple(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ciunsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus); 2648c2ecf20Sopenharmony_ci/**< same as FSE_optimalTableLog(), which used `minus==2` */ 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cisize_t FSE_buildCTable_raw(FSE_CTable *ct, unsigned nbBits); 2678c2ecf20Sopenharmony_ci/**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */ 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cisize_t FSE_buildCTable_rle(FSE_CTable *ct, unsigned char symbolValue); 2708c2ecf20Sopenharmony_ci/**< build a fake FSE_CTable, designed to compress always the same symbolValue */ 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci/* FSE_buildCTable_wksp() : 2738c2ecf20Sopenharmony_ci * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). 2748c2ecf20Sopenharmony_ci * `wkspSize` must be >= `(1<<tableLog)`. 2758c2ecf20Sopenharmony_ci */ 2768c2ecf20Sopenharmony_cisize_t FSE_buildCTable_wksp(FSE_CTable *ct, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize); 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cisize_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits); 2798c2ecf20Sopenharmony_ci/**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */ 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_cisize_t FSE_buildDTable_rle(FSE_DTable *dt, unsigned char symbolValue); 2828c2ecf20Sopenharmony_ci/**< build a fake FSE_DTable, designed to always generate the same symbolValue */ 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_cisize_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize); 2858c2ecf20Sopenharmony_ci/**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */ 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci/* ***************************************** 2888c2ecf20Sopenharmony_ci* FSE symbol compression API 2898c2ecf20Sopenharmony_ci*******************************************/ 2908c2ecf20Sopenharmony_ci/*! 2918c2ecf20Sopenharmony_ci This API consists of small unitary functions, which highly benefit from being inlined. 2928c2ecf20Sopenharmony_ci Hence their body are included in next section. 2938c2ecf20Sopenharmony_ci*/ 2948c2ecf20Sopenharmony_citypedef struct { 2958c2ecf20Sopenharmony_ci ptrdiff_t value; 2968c2ecf20Sopenharmony_ci const void *stateTable; 2978c2ecf20Sopenharmony_ci const void *symbolTT; 2988c2ecf20Sopenharmony_ci unsigned stateLog; 2998c2ecf20Sopenharmony_ci} FSE_CState_t; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_cistatic void FSE_initCState(FSE_CState_t *CStatePtr, const FSE_CTable *ct); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_cistatic void FSE_encodeSymbol(BIT_CStream_t *bitC, FSE_CState_t *CStatePtr, unsigned symbol); 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_cistatic void FSE_flushCState(BIT_CStream_t *bitC, const FSE_CState_t *CStatePtr); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci/**< 3088c2ecf20Sopenharmony_ciThese functions are inner components of FSE_compress_usingCTable(). 3098c2ecf20Sopenharmony_ciThey allow the creation of custom streams, mixing multiple tables and bit sources. 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ciA key property to keep in mind is that encoding and decoding are done **in reverse direction**. 3128c2ecf20Sopenharmony_ciSo the first symbol you will encode is the last you will decode, like a LIFO stack. 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ciYou will need a few variables to track your CStream. They are : 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ciFSE_CTable ct; // Provided by FSE_buildCTable() 3178c2ecf20Sopenharmony_ciBIT_CStream_t bitStream; // bitStream tracking structure 3188c2ecf20Sopenharmony_ciFSE_CState_t state; // State tracking structure (can have several) 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ciThe first thing to do is to init bitStream and state. 3228c2ecf20Sopenharmony_ci size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize); 3238c2ecf20Sopenharmony_ci FSE_initCState(&state, ct); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ciNote that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError(); 3268c2ecf20Sopenharmony_ciYou can then encode your input data, byte after byte. 3278c2ecf20Sopenharmony_ciFSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time. 3288c2ecf20Sopenharmony_ciRemember decoding will be done in reverse direction. 3298c2ecf20Sopenharmony_ci FSE_encodeByte(&bitStream, &state, symbol); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ciAt any time, you can also add any bit sequence. 3328c2ecf20Sopenharmony_ciNote : maximum allowed nbBits is 25, for compatibility with 32-bits decoders 3338c2ecf20Sopenharmony_ci BIT_addBits(&bitStream, bitField, nbBits); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ciThe above methods don't commit data to memory, they just store it into local register, for speed. 3368c2ecf20Sopenharmony_ciLocal register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). 3378c2ecf20Sopenharmony_ciWriting data to memory is a manual operation, performed by the flushBits function. 3388c2ecf20Sopenharmony_ci BIT_flushBits(&bitStream); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ciYour last FSE encoding operation shall be to flush your last state value(s). 3418c2ecf20Sopenharmony_ci FSE_flushState(&bitStream, &state); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ciFinally, you must close the bitStream. 3448c2ecf20Sopenharmony_ciThe function returns the size of CStream in bytes. 3458c2ecf20Sopenharmony_ciIf data couldn't fit into dstBuffer, it will return a 0 ( == not compressible) 3468c2ecf20Sopenharmony_ciIf there is an error, it returns an errorCode (which can be tested using FSE_isError()). 3478c2ecf20Sopenharmony_ci size_t size = BIT_closeCStream(&bitStream); 3488c2ecf20Sopenharmony_ci*/ 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci/* ***************************************** 3518c2ecf20Sopenharmony_ci* FSE symbol decompression API 3528c2ecf20Sopenharmony_ci*******************************************/ 3538c2ecf20Sopenharmony_citypedef struct { 3548c2ecf20Sopenharmony_ci size_t state; 3558c2ecf20Sopenharmony_ci const void *table; /* precise table may vary, depending on U16 */ 3568c2ecf20Sopenharmony_ci} FSE_DState_t; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_cistatic void FSE_initDState(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD, const FSE_DTable *dt); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_cistatic unsigned char FSE_decodeSymbol(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_cistatic unsigned FSE_endOfDState(const FSE_DState_t *DStatePtr); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci/**< 3658c2ecf20Sopenharmony_ciLet's now decompose FSE_decompress_usingDTable() into its unitary components. 3668c2ecf20Sopenharmony_ciYou will decode FSE-encoded symbols from the bitStream, 3678c2ecf20Sopenharmony_ciand also any other bitFields you put in, **in reverse order**. 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ciYou will need a few variables to track your bitStream. They are : 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ciBIT_DStream_t DStream; // Stream context 3728c2ecf20Sopenharmony_ciFSE_DState_t DState; // State context. Multiple ones are possible 3738c2ecf20Sopenharmony_ciFSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable() 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ciThe first thing to do is to init the bitStream. 3768c2ecf20Sopenharmony_ci errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ciYou should then retrieve your initial state(s) 3798c2ecf20Sopenharmony_ci(in reverse flushing order if you have several ones) : 3808c2ecf20Sopenharmony_ci errorCode = FSE_initDState(&DState, &DStream, DTablePtr); 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ciYou can then decode your data, symbol after symbol. 3838c2ecf20Sopenharmony_ciFor information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'. 3848c2ecf20Sopenharmony_ciKeep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out). 3858c2ecf20Sopenharmony_ci unsigned char symbol = FSE_decodeSymbol(&DState, &DStream); 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ciYou can retrieve any bitfield you eventually stored into the bitStream (in reverse order) 3888c2ecf20Sopenharmony_ciNote : maximum allowed nbBits is 25, for 32-bits compatibility 3898c2ecf20Sopenharmony_ci size_t bitField = BIT_readBits(&DStream, nbBits); 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ciAll above operations only read from local register (which size depends on size_t). 3928c2ecf20Sopenharmony_ciRefueling the register from memory is manually performed by the reload method. 3938c2ecf20Sopenharmony_ci endSignal = FSE_reloadDStream(&DStream); 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ciBIT_reloadDStream() result tells if there is still some more data to read from DStream. 3968c2ecf20Sopenharmony_ciBIT_DStream_unfinished : there is still some data left into the DStream. 3978c2ecf20Sopenharmony_ciBIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled. 3988c2ecf20Sopenharmony_ciBIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed. 3998c2ecf20Sopenharmony_ciBIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted. 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ciWhen reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop, 4028c2ecf20Sopenharmony_cito properly detect the exact end of stream. 4038c2ecf20Sopenharmony_ciAfter each decoded symbol, check if DStream is fully consumed using this simple test : 4048c2ecf20Sopenharmony_ci BIT_reloadDStream(&DStream) >= BIT_DStream_completed 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ciWhen it's done, verify decompression is fully completed, by checking both DStream and the relevant states. 4078c2ecf20Sopenharmony_ciChecking if DStream has reached its end is performed by : 4088c2ecf20Sopenharmony_ci BIT_endOfDStream(&DStream); 4098c2ecf20Sopenharmony_ciCheck also the states. There might be some symbols left there, if some high probability ones (>50%) are possible. 4108c2ecf20Sopenharmony_ci FSE_endOfDState(&DState); 4118c2ecf20Sopenharmony_ci*/ 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci/* ***************************************** 4148c2ecf20Sopenharmony_ci* FSE unsafe API 4158c2ecf20Sopenharmony_ci*******************************************/ 4168c2ecf20Sopenharmony_cistatic unsigned char FSE_decodeSymbolFast(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD); 4178c2ecf20Sopenharmony_ci/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */ 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci/* ***************************************** 4208c2ecf20Sopenharmony_ci* Implementation of inlined functions 4218c2ecf20Sopenharmony_ci*******************************************/ 4228c2ecf20Sopenharmony_citypedef struct { 4238c2ecf20Sopenharmony_ci int deltaFindState; 4248c2ecf20Sopenharmony_ci U32 deltaNbBits; 4258c2ecf20Sopenharmony_ci} FSE_symbolCompressionTransform; /* total 8 bytes */ 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ciZSTD_STATIC void FSE_initCState(FSE_CState_t *statePtr, const FSE_CTable *ct) 4288c2ecf20Sopenharmony_ci{ 4298c2ecf20Sopenharmony_ci const void *ptr = ct; 4308c2ecf20Sopenharmony_ci const U16 *u16ptr = (const U16 *)ptr; 4318c2ecf20Sopenharmony_ci const U32 tableLog = ZSTD_read16(ptr); 4328c2ecf20Sopenharmony_ci statePtr->value = (ptrdiff_t)1 << tableLog; 4338c2ecf20Sopenharmony_ci statePtr->stateTable = u16ptr + 2; 4348c2ecf20Sopenharmony_ci statePtr->symbolTT = ((const U32 *)ct + 1 + (tableLog ? (1 << (tableLog - 1)) : 1)); 4358c2ecf20Sopenharmony_ci statePtr->stateLog = tableLog; 4368c2ecf20Sopenharmony_ci} 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci/*! FSE_initCState2() : 4398c2ecf20Sopenharmony_ci* Same as FSE_initCState(), but the first symbol to include (which will be the last to be read) 4408c2ecf20Sopenharmony_ci* uses the smallest state value possible, saving the cost of this symbol */ 4418c2ecf20Sopenharmony_ciZSTD_STATIC void FSE_initCState2(FSE_CState_t *statePtr, const FSE_CTable *ct, U32 symbol) 4428c2ecf20Sopenharmony_ci{ 4438c2ecf20Sopenharmony_ci FSE_initCState(statePtr, ct); 4448c2ecf20Sopenharmony_ci { 4458c2ecf20Sopenharmony_ci const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform *)(statePtr->symbolTT))[symbol]; 4468c2ecf20Sopenharmony_ci const U16 *stateTable = (const U16 *)(statePtr->stateTable); 4478c2ecf20Sopenharmony_ci U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1 << 15)) >> 16); 4488c2ecf20Sopenharmony_ci statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits; 4498c2ecf20Sopenharmony_ci statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; 4508c2ecf20Sopenharmony_ci } 4518c2ecf20Sopenharmony_ci} 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ciZSTD_STATIC void FSE_encodeSymbol(BIT_CStream_t *bitC, FSE_CState_t *statePtr, U32 symbol) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform *)(statePtr->symbolTT))[symbol]; 4568c2ecf20Sopenharmony_ci const U16 *const stateTable = (const U16 *)(statePtr->stateTable); 4578c2ecf20Sopenharmony_ci U32 nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16); 4588c2ecf20Sopenharmony_ci BIT_addBits(bitC, statePtr->value, nbBitsOut); 4598c2ecf20Sopenharmony_ci statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; 4608c2ecf20Sopenharmony_ci} 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ciZSTD_STATIC void FSE_flushCState(BIT_CStream_t *bitC, const FSE_CState_t *statePtr) 4638c2ecf20Sopenharmony_ci{ 4648c2ecf20Sopenharmony_ci BIT_addBits(bitC, statePtr->value, statePtr->stateLog); 4658c2ecf20Sopenharmony_ci BIT_flushBits(bitC); 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci/* ====== Decompression ====== */ 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_citypedef struct { 4718c2ecf20Sopenharmony_ci U16 tableLog; 4728c2ecf20Sopenharmony_ci U16 fastMode; 4738c2ecf20Sopenharmony_ci} FSE_DTableHeader; /* sizeof U32 */ 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_citypedef struct { 4768c2ecf20Sopenharmony_ci unsigned short newState; 4778c2ecf20Sopenharmony_ci unsigned char symbol; 4788c2ecf20Sopenharmony_ci unsigned char nbBits; 4798c2ecf20Sopenharmony_ci} FSE_decode_t; /* size == U32 */ 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ciZSTD_STATIC void FSE_initDState(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD, const FSE_DTable *dt) 4828c2ecf20Sopenharmony_ci{ 4838c2ecf20Sopenharmony_ci const void *ptr = dt; 4848c2ecf20Sopenharmony_ci const FSE_DTableHeader *const DTableH = (const FSE_DTableHeader *)ptr; 4858c2ecf20Sopenharmony_ci DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); 4868c2ecf20Sopenharmony_ci BIT_reloadDStream(bitD); 4878c2ecf20Sopenharmony_ci DStatePtr->table = dt + 1; 4888c2ecf20Sopenharmony_ci} 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ciZSTD_STATIC BYTE FSE_peekSymbol(const FSE_DState_t *DStatePtr) 4918c2ecf20Sopenharmony_ci{ 4928c2ecf20Sopenharmony_ci FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state]; 4938c2ecf20Sopenharmony_ci return DInfo.symbol; 4948c2ecf20Sopenharmony_ci} 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ciZSTD_STATIC void FSE_updateState(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD) 4978c2ecf20Sopenharmony_ci{ 4988c2ecf20Sopenharmony_ci FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state]; 4998c2ecf20Sopenharmony_ci U32 const nbBits = DInfo.nbBits; 5008c2ecf20Sopenharmony_ci size_t const lowBits = BIT_readBits(bitD, nbBits); 5018c2ecf20Sopenharmony_ci DStatePtr->state = DInfo.newState + lowBits; 5028c2ecf20Sopenharmony_ci} 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ciZSTD_STATIC BYTE FSE_decodeSymbol(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD) 5058c2ecf20Sopenharmony_ci{ 5068c2ecf20Sopenharmony_ci FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state]; 5078c2ecf20Sopenharmony_ci U32 const nbBits = DInfo.nbBits; 5088c2ecf20Sopenharmony_ci BYTE const symbol = DInfo.symbol; 5098c2ecf20Sopenharmony_ci size_t const lowBits = BIT_readBits(bitD, nbBits); 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci DStatePtr->state = DInfo.newState + lowBits; 5128c2ecf20Sopenharmony_ci return symbol; 5138c2ecf20Sopenharmony_ci} 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci/*! FSE_decodeSymbolFast() : 5168c2ecf20Sopenharmony_ci unsafe, only works if no symbol has a probability > 50% */ 5178c2ecf20Sopenharmony_ciZSTD_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD) 5188c2ecf20Sopenharmony_ci{ 5198c2ecf20Sopenharmony_ci FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state]; 5208c2ecf20Sopenharmony_ci U32 const nbBits = DInfo.nbBits; 5218c2ecf20Sopenharmony_ci BYTE const symbol = DInfo.symbol; 5228c2ecf20Sopenharmony_ci size_t const lowBits = BIT_readBitsFast(bitD, nbBits); 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci DStatePtr->state = DInfo.newState + lowBits; 5258c2ecf20Sopenharmony_ci return symbol; 5268c2ecf20Sopenharmony_ci} 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ciZSTD_STATIC unsigned FSE_endOfDState(const FSE_DState_t *DStatePtr) { return DStatePtr->state == 0; } 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci/* ************************************************************** 5318c2ecf20Sopenharmony_ci* Tuning parameters 5328c2ecf20Sopenharmony_ci****************************************************************/ 5338c2ecf20Sopenharmony_ci/*!MEMORY_USAGE : 5348c2ecf20Sopenharmony_ci* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) 5358c2ecf20Sopenharmony_ci* Increasing memory usage improves compression ratio 5368c2ecf20Sopenharmony_ci* Reduced memory usage can improve speed, due to cache effect 5378c2ecf20Sopenharmony_ci* Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ 5388c2ecf20Sopenharmony_ci#ifndef FSE_MAX_MEMORY_USAGE 5398c2ecf20Sopenharmony_ci#define FSE_MAX_MEMORY_USAGE 14 5408c2ecf20Sopenharmony_ci#endif 5418c2ecf20Sopenharmony_ci#ifndef FSE_DEFAULT_MEMORY_USAGE 5428c2ecf20Sopenharmony_ci#define FSE_DEFAULT_MEMORY_USAGE 13 5438c2ecf20Sopenharmony_ci#endif 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci/*!FSE_MAX_SYMBOL_VALUE : 5468c2ecf20Sopenharmony_ci* Maximum symbol value authorized. 5478c2ecf20Sopenharmony_ci* Required for proper stack allocation */ 5488c2ecf20Sopenharmony_ci#ifndef FSE_MAX_SYMBOL_VALUE 5498c2ecf20Sopenharmony_ci#define FSE_MAX_SYMBOL_VALUE 255 5508c2ecf20Sopenharmony_ci#endif 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci/* ************************************************************** 5538c2ecf20Sopenharmony_ci* template functions type & suffix 5548c2ecf20Sopenharmony_ci****************************************************************/ 5558c2ecf20Sopenharmony_ci#define FSE_FUNCTION_TYPE BYTE 5568c2ecf20Sopenharmony_ci#define FSE_FUNCTION_EXTENSION 5578c2ecf20Sopenharmony_ci#define FSE_DECODE_TYPE FSE_decode_t 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci/* *************************************************************** 5608c2ecf20Sopenharmony_ci* Constants 5618c2ecf20Sopenharmony_ci*****************************************************************/ 5628c2ecf20Sopenharmony_ci#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE - 2) 5638c2ecf20Sopenharmony_ci#define FSE_MAX_TABLESIZE (1U << FSE_MAX_TABLELOG) 5648c2ecf20Sopenharmony_ci#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE - 1) 5658c2ecf20Sopenharmony_ci#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE - 2) 5668c2ecf20Sopenharmony_ci#define FSE_MIN_TABLELOG 5 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci#define FSE_TABLELOG_ABSOLUTE_MAX 15 5698c2ecf20Sopenharmony_ci#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX 5708c2ecf20Sopenharmony_ci#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" 5718c2ecf20Sopenharmony_ci#endif 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci#define FSE_TABLESTEP(tableSize) ((tableSize >> 1) + (tableSize >> 3) + 3) 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci#endif /* FSE_H */ 576