| 2820 | } |
| 2821 | |
| 2822 | size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) |
| 2823 | { |
| 2824 | void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ |
| 2825 | FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); |
| 2826 | U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; |
| 2827 | |
| 2828 | U32 const maxSV1 = maxSymbolValue + 1; |
| 2829 | U32 const tableSize = 1 << tableLog; |
| 2830 | U32 highThreshold = tableSize-1; |
| 2831 | |
| 2832 | /* Sanity Checks */ |
| 2833 | if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); |
| 2834 | if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); |
| 2835 | |
| 2836 | /* Init, lay down lowprob symbols */ |
| 2837 | { FSE_DTableHeader DTableH; |
| 2838 | DTableH.tableLog = (U16)tableLog; |
| 2839 | DTableH.fastMode = 1; |
| 2840 | { S16 const largeLimit= (S16)(1 << (tableLog-1)); |
| 2841 | U32 s; |
| 2842 | for (s=0; s<maxSV1; s++) { |
| 2843 | if (normalizedCounter[s]==-1) { |
| 2844 | tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; |
| 2845 | symbolNext[s] = 1; |
| 2846 | } else { |
| 2847 | if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0; |
| 2848 | symbolNext[s] = normalizedCounter[s]; |
| 2849 | } } } |
| 2850 | memcpy(dt, &DTableH, sizeof(DTableH)); |
| 2851 | } |
| 2852 | |
| 2853 | /* Spread symbols */ |
| 2854 | { U32 const tableMask = tableSize-1; |
| 2855 | U32 const step = FSE_TABLESTEP(tableSize); |
| 2856 | U32 s, position = 0; |
| 2857 | for (s=0; s<maxSV1; s++) { |
| 2858 | int i; |
| 2859 | for (i=0; i<normalizedCounter[s]; i++) { |
| 2860 | tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; |
| 2861 | position = (position + step) & tableMask; |
| 2862 | while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ |
| 2863 | } } |
| 2864 | if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
| 2865 | } |
| 2866 | |
| 2867 | /* Build Decoding table */ |
| 2868 | { U32 u; |
| 2869 | for (u=0; u<tableSize; u++) { |
| 2870 | FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol); |
| 2871 | U32 const nextState = symbolNext[symbol]++; |
| 2872 | tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) ); |
| 2873 | tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize); |
| 2874 | } } |
| 2875 | |
| 2876 | return 0; |
| 2877 | } |
| 2878 | |
| 2879 |
no test coverage detected