public static nuint FSE_buildDTable_raw(uint *dt, uint nbBits) { void * ptr = (void *)dt; FSE_DTableHeader *DTableH = (FSE_DTableHeader *)(ptr); void * dPtr = (void *)(dt + 1); FSE_decode_t * dinfo = (FSE_decode_t *)(dPtr); uint tableSize = (uint)(1 << (int)nbBits); uint tableMask = tableSize - 1; uint maxSV1 = tableMask + 1; uint s; if (nbBits < 1) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); } DTableH->tableLog = (ushort)(nbBits); DTableH->fastMode = 1; for (s = 0; s < maxSV1; s++) { dinfo[s].newState = 0; dinfo[s].symbol = (byte)(s); dinfo[s].nbBits = (byte)(nbBits); } return(0); }
/*-******************************************************* * Decompression (Byte symbols) *********************************************************/ public static nuint FSE_buildDTable_rle(uint *dt, byte symbolValue) { void * ptr = (void *)dt; FSE_DTableHeader *DTableH = (FSE_DTableHeader *)(ptr); void * dPtr = (void *)(dt + 1); FSE_decode_t * cell = (FSE_decode_t *)(dPtr); DTableH->tableLog = 0; DTableH->fastMode = 0; cell->newState = 0; cell->symbol = symbolValue; cell->nbBits = 0; return(0); }
private static nuint FSE_buildDTable_internal(uint *dt, short *normalizedCounter, uint maxSymbolValue, uint tableLog, void *workSpace, nuint wkspSize) { void * tdPtr = (void *)(dt + 1); FSE_decode_t *tableDecode = (FSE_decode_t *)(tdPtr); ushort * symbolNext = (ushort *)(workSpace); byte * spread = (byte *)(symbolNext + maxSymbolValue + 1); uint maxSV1 = maxSymbolValue + 1; uint tableSize = (uint)(1 << (int)tableLog); uint highThreshold = tableSize - 1; if (((nuint)(sizeof(short)) * (maxSymbolValue + 1) + (1UL << (int)tableLog) + 8) > wkspSize) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); } if (maxSymbolValue > 255) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); } if (tableLog > (uint)((14 - 2))) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); } { FSE_DTableHeader DTableH; DTableH.tableLog = (ushort)(tableLog); DTableH.fastMode = 1; { short largeLimit = (short)(1 << (int)(tableLog - 1)); uint s; for (s = 0; s < maxSV1; s++) { if (normalizedCounter[s] == -1) { tableDecode[highThreshold--].symbol = (byte)(s); symbolNext[s] = 1; } else { if (normalizedCounter[s] >= largeLimit) { DTableH.fastMode = 0; } symbolNext[s] = (ushort)(normalizedCounter[s]); } } } memcpy((void *)(dt), (void *)(&DTableH), ((nuint)(sizeof(FSE_DTableHeader)))); } if (highThreshold == tableSize - 1) { nuint tableMask = tableSize - 1; nuint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); { ulong add = 0x0101010101010101UL; nuint pos = 0; ulong sv = 0; uint s; for (s = 0; s < maxSV1; ++s, sv += add) { int i; int n = normalizedCounter[s]; MEM_write64((void *)(spread + pos), sv); for (i = 8; i < n; i += 8) { MEM_write64((void *)(spread + pos + i), sv); } pos += (nuint)n; } } { nuint position = 0; nuint s; nuint unroll = 2; assert(tableSize % unroll == 0); for (s = 0; s < (nuint)(tableSize); s += unroll) { nuint u; for (u = 0; u < unroll; ++u) { nuint uPosition = (position + (u * step)) & tableMask; tableDecode[uPosition].symbol = spread[s + u]; } position = (position + (unroll * step)) & tableMask; } assert(position == 0); } } else { uint tableMask = tableSize - 1; uint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); uint s, position = 0; for (s = 0; s < maxSV1; s++) { int i; for (i = 0; i < normalizedCounter[s]; i++) { tableDecode[position].symbol = (byte)(s); position = (position + step) & tableMask; while (position > highThreshold) { position = (position + step) & tableMask; } } } if (position != 0) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); } } { uint u; for (u = 0; u < tableSize; u++) { byte symbol = (byte)(tableDecode[u].symbol); uint nextState = symbolNext[symbol]++; tableDecode[u].nbBits = (byte)(tableLog - BIT_highbit32(nextState)); tableDecode[u].newState = (ushort)((nextState << (int)(tableDecode[u].nbBits)) - tableSize); } } return(0); }