| 1 | /** |
| 2 | * \file zstd.c |
| 3 | * Single-file Zstandard library. |
| 4 | * |
| 5 | * Generate using: |
| 6 | * \code |
| 7 | * python combine.py -r ../../lib -x legacy/zstd_legacy.h -o zstd.c zstd-in.c |
| 8 | * \endcode |
| 9 | */ |
| 10 | /* |
| 11 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 12 | * All rights reserved. |
| 13 | * |
| 14 | * This source code is licensed under both the BSD-style license (found in the |
| 15 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 16 | * in the COPYING file in the root directory of this source tree). |
| 17 | * You may select, at your option, one of the above-listed licenses. |
| 18 | */ |
| 19 | /* |
| 20 | * Settings to bake for the single library file. |
| 21 | * |
| 22 | * Note: It's important that none of these affects 'zstd.h' (only the |
| 23 | * implementation files we're amalgamating). |
| 24 | * |
| 25 | * Note: MEM_MODULE stops xxhash redefining BYTE, U16, etc., which are also |
| 26 | * defined in mem.h (breaking C99 compatibility). |
| 27 | * |
| 28 | * Note: the undefs for xxHash allow Zstd's implementation to coincide with |
| 29 | * standalone xxHash usage (with global defines). |
| 30 | * |
| 31 | * Note: if you enable ZSTD_LEGACY_SUPPORT the combine.py script will need |
| 32 | * re-running without the "-x legacy/zstd_legacy.h" option (it excludes the |
| 33 | * legacy support at the source level). |
| 34 | * |
| 35 | * Note: multithreading is enabled for all platforms apart from Emscripten. |
| 36 | */ |
| 37 | #define DEBUGLEVEL 0 |
| 38 | #define MEM_MODULE |
| 39 | #undef XXH_NAMESPACE |
| 40 | #define XXH_NAMESPACE ZSTD_ |
| 41 | #undef XXH_PRIVATE_API |
| 42 | #define XXH_PRIVATE_API |
| 43 | #undef XXH_INLINE_ALL |
| 44 | #define XXH_INLINE_ALL |
| 45 | #define ZSTD_LEGACY_SUPPORT 0 |
| 46 | #ifndef __EMSCRIPTEN__ |
| 47 | #define ZSTD_MULTITHREAD |
| 48 | #endif |
| 49 | #define ZSTD_TRACE 0 |
| 50 | /* TODO: Can't amalgamate ASM function */ |
| 51 | #define ZSTD_DISABLE_ASM 1 |
| 52 | |
| 53 | /* >> v_patch start */ |
| 54 | #if defined(__TINYC__) |
| 55 | |
| 56 | #if defined(_WIN32) |
| 57 | #undef ZSTD_MULTITHREAD |
| 58 | #define ZSTD_NO_INTRINSICS |
| 59 | #endif |
| 60 | |
| 61 | /* tcc doesn't support ARM asm */ |
| 62 | #if defined(__arm__) || defined(__aarch64__) |
| 63 | #define NO_PREFETCH |
| 64 | #endif |
| 65 | |
| 66 | #if defined(__FreeBSD__) || defined(__OpenBSD__) |
| 67 | /* tcc on FreeBSD/OpenBSD define __GNUC__, but it can't work here */ |
| 68 | #undef __GNUC__ |
| 69 | #endif |
| 70 | |
| 71 | #endif /* __TINYC__ */ |
| 72 | /* << v_patch end */ |
| 73 | |
| 74 | /* Include zstd_deps.h first with all the options we need enabled. */ |
| 75 | #define ZSTD_DEPS_NEED_MALLOC |
| 76 | #define ZSTD_DEPS_NEED_MATH64 |
| 77 | /**** start inlining common/zstd_deps.h ****/ |
| 78 | /* |
| 79 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 80 | * All rights reserved. |
| 81 | * |
| 82 | * This source code is licensed under both the BSD-style license (found in the |
| 83 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 84 | * in the COPYING file in the root directory of this source tree). |
| 85 | * You may select, at your option, one of the above-listed licenses. |
| 86 | */ |
| 87 | |
| 88 | /* This file provides common libc dependencies that zstd requires. |
| 89 | * The purpose is to allow replacing this file with a custom implementation |
| 90 | * to compile zstd without libc support. |
| 91 | */ |
| 92 | |
| 93 | /* Need: |
| 94 | * NULL |
| 95 | * INT_MAX |
| 96 | * UINT_MAX |
| 97 | * ZSTD_memcpy() |
| 98 | * ZSTD_memset() |
| 99 | * ZSTD_memmove() |
| 100 | */ |
| 101 | #ifndef ZSTD_DEPS_COMMON |
| 102 | #define ZSTD_DEPS_COMMON |
| 103 | |
| 104 | /* Even though we use qsort_r only for the dictionary builder, the macro |
| 105 | * _GNU_SOURCE has to be declared *before* the inclusion of any standard |
| 106 | * header and the script 'combine.sh' combines the whole zstd source code |
| 107 | * in a single file. |
| 108 | */ |
| 109 | #if defined(__linux) || defined(__linux__) || defined(linux) || defined(__gnu_linux__) || \ |
| 110 | defined(__CYGWIN__) || defined(__MSYS__) |
| 111 | #if !defined(_GNU_SOURCE) && !defined(__ANDROID__) /* NDK doesn't ship qsort_r(). */ |
| 112 | #define _GNU_SOURCE |
| 113 | #endif |
| 114 | #endif |
| 115 | |
| 116 | #include <limits.h> |
| 117 | #include <stddef.h> |
| 118 | #include <string.h> |
| 119 | |
| 120 | #if defined(__GNUC__) && __GNUC__ >= 4 |
| 121 | # define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l)) |
| 122 | # define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l)) |
| 123 | # define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l)) |
| 124 | #else |
| 125 | # define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l)) |
| 126 | # define ZSTD_memmove(d,s,l) memmove((d),(s),(l)) |
| 127 | # define ZSTD_memset(p,v,l) memset((p),(v),(l)) |
| 128 | #endif |
| 129 | |
| 130 | #endif /* ZSTD_DEPS_COMMON */ |
| 131 | |
| 132 | /* Need: |
| 133 | * ZSTD_malloc() |
| 134 | * ZSTD_free() |
| 135 | * ZSTD_calloc() |
| 136 | */ |
| 137 | #ifdef ZSTD_DEPS_NEED_MALLOC |
| 138 | #ifndef ZSTD_DEPS_MALLOC |
| 139 | #define ZSTD_DEPS_MALLOC |
| 140 | |
| 141 | #include <stdlib.h> |
| 142 | |
| 143 | #define ZSTD_malloc(s) malloc(s) |
| 144 | #define ZSTD_calloc(n,s) calloc((n), (s)) |
| 145 | #define ZSTD_free(p) free((p)) |
| 146 | |
| 147 | #endif /* ZSTD_DEPS_MALLOC */ |
| 148 | #endif /* ZSTD_DEPS_NEED_MALLOC */ |
| 149 | |
| 150 | /* |
| 151 | * Provides 64-bit math support. |
| 152 | * Need: |
| 153 | * U64 ZSTD_div64(U64 dividend, U32 divisor) |
| 154 | */ |
| 155 | #ifdef ZSTD_DEPS_NEED_MATH64 |
| 156 | #ifndef ZSTD_DEPS_MATH64 |
| 157 | #define ZSTD_DEPS_MATH64 |
| 158 | |
| 159 | #define ZSTD_div64(dividend, divisor) ((dividend) / (divisor)) |
| 160 | |
| 161 | #endif /* ZSTD_DEPS_MATH64 */ |
| 162 | #endif /* ZSTD_DEPS_NEED_MATH64 */ |
| 163 | |
| 164 | /* Need: |
| 165 | * assert() |
| 166 | */ |
| 167 | #ifdef ZSTD_DEPS_NEED_ASSERT |
| 168 | #ifndef ZSTD_DEPS_ASSERT |
| 169 | #define ZSTD_DEPS_ASSERT |
| 170 | |
| 171 | #include <assert.h> |
| 172 | |
| 173 | #endif /* ZSTD_DEPS_ASSERT */ |
| 174 | #endif /* ZSTD_DEPS_NEED_ASSERT */ |
| 175 | |
| 176 | /* Need: |
| 177 | * ZSTD_DEBUG_PRINT() |
| 178 | */ |
| 179 | #ifdef ZSTD_DEPS_NEED_IO |
| 180 | #ifndef ZSTD_DEPS_IO |
| 181 | #define ZSTD_DEPS_IO |
| 182 | |
| 183 | #include <stdio.h> |
| 184 | #define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__) |
| 185 | |
| 186 | #endif /* ZSTD_DEPS_IO */ |
| 187 | #endif /* ZSTD_DEPS_NEED_IO */ |
| 188 | |
| 189 | /* Only requested when <stdint.h> is known to be present. |
| 190 | * Need: |
| 191 | * intptr_t |
| 192 | */ |
| 193 | #ifdef ZSTD_DEPS_NEED_STDINT |
| 194 | #ifndef ZSTD_DEPS_STDINT |
| 195 | #define ZSTD_DEPS_STDINT |
| 196 | |
| 197 | #include <stdint.h> |
| 198 | |
| 199 | #endif /* ZSTD_DEPS_STDINT */ |
| 200 | #endif /* ZSTD_DEPS_NEED_STDINT */ |
| 201 | /**** ended inlining common/zstd_deps.h ****/ |
| 202 | |
| 203 | /**** start inlining common/debug.c ****/ |
| 204 | /* ****************************************************************** |
| 205 | * debug |
| 206 | * Part of FSE library |
| 207 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 208 | * |
| 209 | * You can contact the author at : |
| 210 | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 211 | * |
| 212 | * This source code is licensed under both the BSD-style license (found in the |
| 213 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 214 | * in the COPYING file in the root directory of this source tree). |
| 215 | * You may select, at your option, one of the above-listed licenses. |
| 216 | ****************************************************************** */ |
| 217 | |
| 218 | |
| 219 | /* |
| 220 | * This module only hosts one global variable |
| 221 | * which can be used to dynamically influence the verbosity of traces, |
| 222 | * such as DEBUGLOG and RAWLOG |
| 223 | */ |
| 224 | |
| 225 | /**** start inlining debug.h ****/ |
| 226 | /* ****************************************************************** |
| 227 | * debug |
| 228 | * Part of FSE library |
| 229 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 230 | * |
| 231 | * You can contact the author at : |
| 232 | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 233 | * |
| 234 | * This source code is licensed under both the BSD-style license (found in the |
| 235 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 236 | * in the COPYING file in the root directory of this source tree). |
| 237 | * You may select, at your option, one of the above-listed licenses. |
| 238 | ****************************************************************** */ |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * The purpose of this header is to enable debug functions. |
| 243 | * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time, |
| 244 | * and DEBUG_STATIC_ASSERT() for compile-time. |
| 245 | * |
| 246 | * By default, DEBUGLEVEL==0, which means run-time debug is disabled. |
| 247 | * |
| 248 | * Level 1 enables assert() only. |
| 249 | * Starting level 2, traces can be generated and pushed to stderr. |
| 250 | * The higher the level, the more verbose the traces. |
| 251 | * |
| 252 | * It's possible to dynamically adjust level using variable g_debug_level, |
| 253 | * which is only declared if DEBUGLEVEL>=2, |
| 254 | * and is a global variable, not multi-thread protected (use with care) |
| 255 | */ |
| 256 | |
| 257 | #ifndef DEBUG_H_12987983217 |
| 258 | #define DEBUG_H_12987983217 |
| 259 | |
| 260 | |
| 261 | /* static assert is triggered at compile time, leaving no runtime artefact. |
| 262 | * static assert only works with compile-time constants. |
| 263 | * Also, this variant can only be used inside a function. */ |
| 264 | #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1]) |
| 265 | |
| 266 | |
| 267 | /* DEBUGLEVEL is expected to be defined externally, |
| 268 | * typically through compiler command line. |
| 269 | * Value must be a number. */ |
| 270 | #ifndef DEBUGLEVEL |
| 271 | # define DEBUGLEVEL 0 |
| 272 | #endif |
| 273 | |
| 274 | |
| 275 | /* recommended values for DEBUGLEVEL : |
| 276 | * 0 : release mode, no debug, all run-time checks disabled |
| 277 | * 1 : enables assert() only, no display |
| 278 | * 2 : reserved, for currently active debug path |
| 279 | * 3 : events once per object lifetime (CCtx, CDict, etc.) |
| 280 | * 4 : events once per frame |
| 281 | * 5 : events once per block |
| 282 | * 6 : events once per sequence (verbose) |
| 283 | * 7+: events at every position (*very* verbose) |
| 284 | * |
| 285 | * It's generally inconvenient to output traces > 5. |
| 286 | * In which case, it's possible to selectively trigger high verbosity levels |
| 287 | * by modifying g_debug_level. |
| 288 | */ |
| 289 | |
| 290 | #if (DEBUGLEVEL>=1) |
| 291 | # define ZSTD_DEPS_NEED_ASSERT |
| 292 | /**** skipping file: zstd_deps.h ****/ |
| 293 | #else |
| 294 | # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */ |
| 295 | # define assert(condition) ((void)0) /* disable assert (default) */ |
| 296 | # endif |
| 297 | #endif |
| 298 | |
| 299 | #if (DEBUGLEVEL>=2) |
| 300 | # define ZSTD_DEPS_NEED_IO |
| 301 | /**** skipping file: zstd_deps.h ****/ |
| 302 | extern int g_debuglevel; /* the variable is only declared, |
| 303 | it actually lives in debug.c, |
| 304 | and is shared by the whole process. |
| 305 | It's not thread-safe. |
| 306 | It's useful when enabling very verbose levels |
| 307 | on selective conditions (such as position in src) */ |
| 308 | |
| 309 | # define RAWLOG(l, ...) \ |
| 310 | do { \ |
| 311 | if (l<=g_debuglevel) { \ |
| 312 | ZSTD_DEBUG_PRINT(__VA_ARGS__); \ |
| 313 | } \ |
| 314 | } while (0) |
| 315 | |
| 316 | #define STRINGIFY(x) #x |
| 317 | #define TOSTRING(x) STRINGIFY(x) |
| 318 | #define LINE_AS_STRING TOSTRING(__LINE__) |
| 319 | |
| 320 | # define DEBUGLOG(l, ...) \ |
| 321 | do { \ |
| 322 | if (l<=g_debuglevel) { \ |
| 323 | ZSTD_DEBUG_PRINT(__FILE__ ":" LINE_AS_STRING ": " __VA_ARGS__); \ |
| 324 | ZSTD_DEBUG_PRINT(" \n"); \ |
| 325 | } \ |
| 326 | } while (0) |
| 327 | #else |
| 328 | # define RAWLOG(l, ...) do { } while (0) /* disabled */ |
| 329 | # define DEBUGLOG(l, ...) do { } while (0) /* disabled */ |
| 330 | #endif |
| 331 | |
| 332 | #endif /* DEBUG_H_12987983217 */ |
| 333 | /**** ended inlining debug.h ****/ |
| 334 | |
| 335 | #if !defined(ZSTD_LINUX_KERNEL) || (DEBUGLEVEL>=2) |
| 336 | /* We only use this when DEBUGLEVEL>=2, but we get -Werror=pedantic errors if a |
| 337 | * translation unit is empty. So remove this from Linux kernel builds, but |
| 338 | * otherwise just leave it in. |
| 339 | */ |
| 340 | int g_debuglevel = DEBUGLEVEL; |
| 341 | #endif |
| 342 | /**** ended inlining common/debug.c ****/ |
| 343 | /**** start inlining common/entropy_common.c ****/ |
| 344 | /* ****************************************************************** |
| 345 | * Common functions of New Generation Entropy library |
| 346 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 347 | * |
| 348 | * You can contact the author at : |
| 349 | * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 350 | * - Public forum : https://groups.google.com/forum/#!forum/lz4c |
| 351 | * |
| 352 | * This source code is licensed under both the BSD-style license (found in the |
| 353 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 354 | * in the COPYING file in the root directory of this source tree). |
| 355 | * You may select, at your option, one of the above-listed licenses. |
| 356 | ****************************************************************** */ |
| 357 | |
| 358 | /* ************************************* |
| 359 | * Dependencies |
| 360 | ***************************************/ |
| 361 | /**** start inlining mem.h ****/ |
| 362 | /* |
| 363 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 364 | * All rights reserved. |
| 365 | * |
| 366 | * This source code is licensed under both the BSD-style license (found in the |
| 367 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 368 | * in the COPYING file in the root directory of this source tree). |
| 369 | * You may select, at your option, one of the above-listed licenses. |
| 370 | */ |
| 371 | |
| 372 | #ifndef MEM_H_MODULE |
| 373 | #define MEM_H_MODULE |
| 374 | |
| 375 | /*-**************************************** |
| 376 | * Dependencies |
| 377 | ******************************************/ |
| 378 | #include <stddef.h> /* size_t, ptrdiff_t */ |
| 379 | /**** start inlining compiler.h ****/ |
| 380 | /* |
| 381 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 382 | * All rights reserved. |
| 383 | * |
| 384 | * This source code is licensed under both the BSD-style license (found in the |
| 385 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 386 | * in the COPYING file in the root directory of this source tree). |
| 387 | * You may select, at your option, one of the above-listed licenses. |
| 388 | */ |
| 389 | |
| 390 | #ifndef ZSTD_COMPILER_H |
| 391 | #define ZSTD_COMPILER_H |
| 392 | |
| 393 | #include <stddef.h> |
| 394 | |
| 395 | /**** start inlining portability_macros.h ****/ |
| 396 | /* |
| 397 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 398 | * All rights reserved. |
| 399 | * |
| 400 | * This source code is licensed under both the BSD-style license (found in the |
| 401 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 402 | * in the COPYING file in the root directory of this source tree). |
| 403 | * You may select, at your option, one of the above-listed licenses. |
| 404 | */ |
| 405 | |
| 406 | #ifndef ZSTD_PORTABILITY_MACROS_H |
| 407 | #define ZSTD_PORTABILITY_MACROS_H |
| 408 | |
| 409 | /** |
| 410 | * This header file contains macro definitions to support portability. |
| 411 | * This header is shared between C and ASM code, so it MUST only |
| 412 | * contain macro definitions. It MUST not contain any C code. |
| 413 | * |
| 414 | * This header ONLY defines macros to detect platforms/feature support. |
| 415 | * |
| 416 | */ |
| 417 | |
| 418 | |
| 419 | /* compat. with non-clang compilers */ |
| 420 | #ifndef __has_attribute |
| 421 | #define __has_attribute(x) 0 |
| 422 | #endif |
| 423 | |
| 424 | /* compat. with non-clang compilers */ |
| 425 | #ifndef __has_builtin |
| 426 | # define __has_builtin(x) 0 |
| 427 | #endif |
| 428 | |
| 429 | /* compat. with non-clang compilers */ |
| 430 | #ifndef __has_feature |
| 431 | # define __has_feature(x) 0 |
| 432 | #endif |
| 433 | |
| 434 | /* detects whether we are being compiled under msan */ |
| 435 | #ifndef ZSTD_MEMORY_SANITIZER |
| 436 | # if __has_feature(memory_sanitizer) |
| 437 | # define ZSTD_MEMORY_SANITIZER 1 |
| 438 | # else |
| 439 | # define ZSTD_MEMORY_SANITIZER 0 |
| 440 | # endif |
| 441 | #endif |
| 442 | |
| 443 | /* detects whether we are being compiled under asan */ |
| 444 | #ifndef ZSTD_ADDRESS_SANITIZER |
| 445 | # if __has_feature(address_sanitizer) |
| 446 | # define ZSTD_ADDRESS_SANITIZER 1 |
| 447 | # elif defined(__SANITIZE_ADDRESS__) |
| 448 | # define ZSTD_ADDRESS_SANITIZER 1 |
| 449 | # else |
| 450 | # define ZSTD_ADDRESS_SANITIZER 0 |
| 451 | # endif |
| 452 | #endif |
| 453 | |
| 454 | /* detects whether we are being compiled under dfsan */ |
| 455 | #ifndef ZSTD_DATAFLOW_SANITIZER |
| 456 | # if __has_feature(dataflow_sanitizer) |
| 457 | # define ZSTD_DATAFLOW_SANITIZER 1 |
| 458 | # else |
| 459 | # define ZSTD_DATAFLOW_SANITIZER 0 |
| 460 | # endif |
| 461 | #endif |
| 462 | |
| 463 | /* Mark the internal assembly functions as hidden */ |
| 464 | #ifdef __ELF__ |
| 465 | # define ZSTD_HIDE_ASM_FUNCTION(func) .hidden func |
| 466 | #elif defined(__APPLE__) |
| 467 | # define ZSTD_HIDE_ASM_FUNCTION(func) .private_extern func |
| 468 | #else |
| 469 | # define ZSTD_HIDE_ASM_FUNCTION(func) |
| 470 | #endif |
| 471 | |
| 472 | /* Compile time determination of BMI2 support */ |
| 473 | #ifndef STATIC_BMI2 |
| 474 | # if defined(__BMI2__) |
| 475 | # define STATIC_BMI2 1 |
| 476 | # elif defined(_MSC_VER) && defined(__AVX2__) |
| 477 | # define STATIC_BMI2 1 /* MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2 */ |
| 478 | # endif |
| 479 | #endif |
| 480 | |
| 481 | #ifndef STATIC_BMI2 |
| 482 | # define STATIC_BMI2 0 |
| 483 | #endif |
| 484 | |
| 485 | /* Enable runtime BMI2 dispatch based on the CPU. |
| 486 | * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. |
| 487 | */ |
| 488 | #ifndef DYNAMIC_BMI2 |
| 489 | # if ((defined(__clang__) && __has_attribute(__target__)) \ |
| 490 | || (defined(__GNUC__) \ |
| 491 | && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \ |
| 492 | && (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)) \ |
| 493 | && !defined(__BMI2__) |
| 494 | # define DYNAMIC_BMI2 1 |
| 495 | # else |
| 496 | # define DYNAMIC_BMI2 0 |
| 497 | # endif |
| 498 | #endif |
| 499 | |
| 500 | /** |
| 501 | * Only enable assembly for GNU C compatible compilers, |
| 502 | * because other platforms may not support GAS assembly syntax. |
| 503 | * |
| 504 | * Only enable assembly for Linux / MacOS / Win32, other platforms may |
| 505 | * work, but they haven't been tested. This could likely be |
| 506 | * extended to BSD systems. |
| 507 | * |
| 508 | * Disable assembly when MSAN is enabled, because MSAN requires |
| 509 | * 100% of code to be instrumented to work. |
| 510 | */ |
| 511 | #if defined(__GNUC__) |
| 512 | # if defined(__linux__) || defined(__linux) || defined(__APPLE__) || defined(_WIN32) |
| 513 | # if ZSTD_MEMORY_SANITIZER |
| 514 | # define ZSTD_ASM_SUPPORTED 0 |
| 515 | # elif ZSTD_DATAFLOW_SANITIZER |
| 516 | # define ZSTD_ASM_SUPPORTED 0 |
| 517 | # else |
| 518 | # define ZSTD_ASM_SUPPORTED 1 |
| 519 | # endif |
| 520 | # else |
| 521 | # define ZSTD_ASM_SUPPORTED 0 |
| 522 | # endif |
| 523 | #else |
| 524 | # define ZSTD_ASM_SUPPORTED 0 |
| 525 | #endif |
| 526 | |
| 527 | /** |
| 528 | * Determines whether we should enable assembly for x86-64 |
| 529 | * with BMI2. |
| 530 | * |
| 531 | * Enable if all of the following conditions hold: |
| 532 | * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM |
| 533 | * - Assembly is supported |
| 534 | * - We are compiling for x86-64 and either: |
| 535 | * - DYNAMIC_BMI2 is enabled |
| 536 | * - BMI2 is supported at compile time |
| 537 | */ |
| 538 | #if !defined(ZSTD_DISABLE_ASM) && \ |
| 539 | ZSTD_ASM_SUPPORTED && \ |
| 540 | defined(__x86_64__) && \ |
| 541 | (DYNAMIC_BMI2 || defined(__BMI2__)) |
| 542 | # define ZSTD_ENABLE_ASM_X86_64_BMI2 1 |
| 543 | #else |
| 544 | # define ZSTD_ENABLE_ASM_X86_64_BMI2 0 |
| 545 | #endif |
| 546 | |
| 547 | /* |
| 548 | * For x86 ELF targets, add .note.gnu.property section for Intel CET in |
| 549 | * assembly sources when CET is enabled. |
| 550 | * |
| 551 | * Additionally, any function that may be called indirectly must begin |
| 552 | * with ZSTD_CET_ENDBRANCH. |
| 553 | */ |
| 554 | #if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) \ |
| 555 | && defined(__has_include) |
| 556 | # if __has_include(<cet.h>) |
| 557 | # include <cet.h> |
| 558 | # define ZSTD_CET_ENDBRANCH _CET_ENDBR |
| 559 | # endif |
| 560 | #endif |
| 561 | |
| 562 | #ifndef ZSTD_CET_ENDBRANCH |
| 563 | # define ZSTD_CET_ENDBRANCH |
| 564 | #endif |
| 565 | |
| 566 | #endif /* ZSTD_PORTABILITY_MACROS_H */ |
| 567 | /**** ended inlining portability_macros.h ****/ |
| 568 | |
| 569 | /*-******************************************************* |
| 570 | * Compiler specifics |
| 571 | *********************************************************/ |
| 572 | /* force inlining */ |
| 573 | |
| 574 | #if !defined(ZSTD_NO_INLINE) |
| 575 | #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
| 576 | # define INLINE_KEYWORD inline |
| 577 | #else |
| 578 | # define INLINE_KEYWORD |
| 579 | #endif |
| 580 | |
| 581 | #if defined(__GNUC__) || defined(__IAR_SYSTEMS_ICC__) |
| 582 | # define FORCE_INLINE_ATTR __attribute__((always_inline)) |
| 583 | #elif defined(_MSC_VER) |
| 584 | # define FORCE_INLINE_ATTR __forceinline |
| 585 | #else |
| 586 | # define FORCE_INLINE_ATTR |
| 587 | #endif |
| 588 | |
| 589 | #else |
| 590 | |
| 591 | #define INLINE_KEYWORD |
| 592 | #define FORCE_INLINE_ATTR |
| 593 | |
| 594 | #endif |
| 595 | |
| 596 | /** |
| 597 | On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). |
| 598 | This explicitly marks such functions as __cdecl so that the code will still compile |
| 599 | if a CC other than __cdecl has been made the default. |
| 600 | */ |
| 601 | #if defined(_MSC_VER) |
| 602 | # define WIN_CDECL __cdecl |
| 603 | #else |
| 604 | # define WIN_CDECL |
| 605 | #endif |
| 606 | |
| 607 | /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */ |
| 608 | #if defined(__GNUC__) || defined(__IAR_SYSTEMS_ICC__) |
| 609 | # define UNUSED_ATTR __attribute__((unused)) |
| 610 | #else |
| 611 | # define UNUSED_ATTR |
| 612 | #endif |
| 613 | |
| 614 | /** |
| 615 | * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant |
| 616 | * parameters. They must be inlined for the compiler to eliminate the constant |
| 617 | * branches. |
| 618 | */ |
| 619 | #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR UNUSED_ATTR |
| 620 | /** |
| 621 | * HINT_INLINE is used to help the compiler generate better code. It is *not* |
| 622 | * used for "templates", so it can be tweaked based on the compilers |
| 623 | * performance. |
| 624 | * |
| 625 | * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the |
| 626 | * always_inline attribute. |
| 627 | * |
| 628 | * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline |
| 629 | * attribute. |
| 630 | */ |
| 631 | #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5 |
| 632 | # define HINT_INLINE static INLINE_KEYWORD |
| 633 | #else |
| 634 | # define HINT_INLINE FORCE_INLINE_TEMPLATE |
| 635 | #endif |
| 636 | |
| 637 | /* "soft" inline : |
| 638 | * The compiler is free to select if it's a good idea to inline or not. |
| 639 | * The main objective is to silence compiler warnings |
| 640 | * when a defined function in included but not used. |
| 641 | * |
| 642 | * Note : this macro is prefixed `MEM_` because it used to be provided by `mem.h` unit. |
| 643 | * Updating the prefix is probably preferable, but requires a fairly large codemod, |
| 644 | * since this name is used everywhere. |
| 645 | */ |
| 646 | #ifndef MEM_STATIC /* already defined in Linux Kernel mem.h */ |
| 647 | #if defined(__GNUC__) |
| 648 | # define MEM_STATIC static __inline UNUSED_ATTR |
| 649 | #elif defined(__IAR_SYSTEMS_ICC__) |
| 650 | # define MEM_STATIC static inline UNUSED_ATTR |
| 651 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
| 652 | # define MEM_STATIC static inline |
| 653 | #elif defined(_MSC_VER) |
| 654 | # define MEM_STATIC static __inline |
| 655 | #else |
| 656 | # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ |
| 657 | #endif |
| 658 | #endif |
| 659 | |
| 660 | /* force no inlining */ |
| 661 | #ifdef _MSC_VER |
| 662 | # define FORCE_NOINLINE static __declspec(noinline) |
| 663 | #else |
| 664 | # if defined(__GNUC__) || defined(__IAR_SYSTEMS_ICC__) |
| 665 | # define FORCE_NOINLINE static __attribute__((__noinline__)) |
| 666 | # else |
| 667 | # define FORCE_NOINLINE static |
| 668 | # endif |
| 669 | #endif |
| 670 | |
| 671 | |
| 672 | /* target attribute */ |
| 673 | #if defined(__GNUC__) || defined(__IAR_SYSTEMS_ICC__) |
| 674 | # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) |
| 675 | #else |
| 676 | # define TARGET_ATTRIBUTE(target) |
| 677 | #endif |
| 678 | |
| 679 | /* Target attribute for BMI2 dynamic dispatch. |
| 680 | * Enable lzcnt, bmi, and bmi2. |
| 681 | * We test for bmi1 & bmi2. lzcnt is included in bmi1. |
| 682 | */ |
| 683 | #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2") |
| 684 | |
| 685 | /* prefetch |
| 686 | * can be disabled, by declaring NO_PREFETCH build macro */ |
| 687 | #if defined(NO_PREFETCH) |
| 688 | # define PREFETCH_L1(ptr) do { (void)(ptr); } while (0) /* disabled */ |
| 689 | # define PREFETCH_L2(ptr) do { (void)(ptr); } while (0) /* disabled */ |
| 690 | #else |
| 691 | # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) && !defined(_M_ARM64EC) /* _mm_prefetch() is not defined outside of x86/x64 */ |
| 692 | # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ |
| 693 | # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0) |
| 694 | # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1) |
| 695 | # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) ) |
| 696 | # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) |
| 697 | # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) |
| 698 | # elif defined(__aarch64__) |
| 699 | # define PREFETCH_L1(ptr) do { __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))); } while (0) |
| 700 | # define PREFETCH_L2(ptr) do { __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))); } while (0) |
| 701 | # else |
| 702 | # define PREFETCH_L1(ptr) do { (void)(ptr); } while (0) /* disabled */ |
| 703 | # define PREFETCH_L2(ptr) do { (void)(ptr); } while (0) /* disabled */ |
| 704 | # endif |
| 705 | #endif /* NO_PREFETCH */ |
| 706 | |
| 707 | #define CACHELINE_SIZE 64 |
| 708 | |
| 709 | #define PREFETCH_AREA(p, s) \ |
| 710 | do { \ |
| 711 | const char* const _ptr = (const char*)(p); \ |
| 712 | size_t const _size = (size_t)(s); \ |
| 713 | size_t _pos; \ |
| 714 | for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \ |
| 715 | PREFETCH_L2(_ptr + _pos); \ |
| 716 | } \ |
| 717 | } while (0) |
| 718 | |
| 719 | /* vectorization |
| 720 | * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax, |
| 721 | * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */ |
| 722 | #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__) && !defined(__TINYC__) |
| 723 | # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) |
| 724 | # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) |
| 725 | # else |
| 726 | # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") |
| 727 | # endif |
| 728 | #else |
| 729 | # define DONT_VECTORIZE |
| 730 | #endif |
| 731 | |
| 732 | /* Tell the compiler that a branch is likely or unlikely. |
| 733 | * Only use these macros if it causes the compiler to generate better code. |
| 734 | * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc |
| 735 | * and clang, please do. |
| 736 | */ |
| 737 | #if defined(__GNUC__) |
| 738 | #define LIKELY(x) (__builtin_expect((x), 1)) |
| 739 | #define UNLIKELY(x) (__builtin_expect((x), 0)) |
| 740 | #else |
| 741 | #define LIKELY(x) (x) |
| 742 | #define UNLIKELY(x) (x) |
| 743 | #endif |
| 744 | |
| 745 | #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))) |
| 746 | # define ZSTD_UNREACHABLE do { assert(0), __builtin_unreachable(); } while (0) |
| 747 | #else |
| 748 | # define ZSTD_UNREACHABLE do { assert(0); } while (0) |
| 749 | #endif |
| 750 | |
| 751 | /* disable warnings */ |
| 752 | #ifdef _MSC_VER /* Visual Studio */ |
| 753 | # include <intrin.h> /* For Visual 2005 */ |
| 754 | # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ |
| 755 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 756 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
| 757 | # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
| 758 | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
| 759 | #endif |
| 760 | |
| 761 | /* compile time determination of SIMD support */ |
| 762 | #if !defined(ZSTD_NO_INTRINSICS) |
| 763 | # if defined(__AVX2__) |
| 764 | # define ZSTD_ARCH_X86_AVX2 |
| 765 | # endif |
| 766 | # if defined(__SSE2__) || defined(_M_X64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2)) |
| 767 | # define ZSTD_ARCH_X86_SSE2 |
| 768 | # endif |
| 769 | # if defined(__ARM_NEON) || defined(_M_ARM64) |
| 770 | # define ZSTD_ARCH_ARM_NEON |
| 771 | # endif |
| 772 | # |
| 773 | # if defined(ZSTD_ARCH_X86_AVX2) |
| 774 | # include <immintrin.h> |
| 775 | # endif |
| 776 | # if defined(ZSTD_ARCH_X86_SSE2) |
| 777 | # include <emmintrin.h> |
| 778 | # elif defined(ZSTD_ARCH_ARM_NEON) |
| 779 | # include <arm_neon.h> |
| 780 | # endif |
| 781 | #endif |
| 782 | |
| 783 | /* C-language Attributes are added in C23. */ |
| 784 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute) |
| 785 | # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x) |
| 786 | #else |
| 787 | # define ZSTD_HAS_C_ATTRIBUTE(x) 0 |
| 788 | #endif |
| 789 | |
| 790 | /* Only use C++ attributes in C++. Some compilers report support for C++ |
| 791 | * attributes when compiling with C. |
| 792 | */ |
| 793 | #if defined(__cplusplus) && defined(__has_cpp_attribute) |
| 794 | # define ZSTD_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
| 795 | #else |
| 796 | # define ZSTD_HAS_CPP_ATTRIBUTE(x) 0 |
| 797 | #endif |
| 798 | |
| 799 | /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute. |
| 800 | * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough |
| 801 | * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough |
| 802 | * - Else: __attribute__((__fallthrough__)) |
| 803 | */ |
| 804 | #ifndef ZSTD_FALLTHROUGH |
| 805 | # if ZSTD_HAS_C_ATTRIBUTE(fallthrough) |
| 806 | # define ZSTD_FALLTHROUGH [[fallthrough]] |
| 807 | # elif ZSTD_HAS_CPP_ATTRIBUTE(fallthrough) |
| 808 | # define ZSTD_FALLTHROUGH [[fallthrough]] |
| 809 | # elif __has_attribute(__fallthrough__) |
| 810 | /* Leading semicolon is to satisfy gcc-11 with -pedantic. Without the semicolon |
| 811 | * gcc complains about: a label can only be part of a statement and a declaration is not a statement. |
| 812 | */ |
| 813 | # define ZSTD_FALLTHROUGH ; __attribute__((__fallthrough__)) |
| 814 | # else |
| 815 | # define ZSTD_FALLTHROUGH |
| 816 | # endif |
| 817 | #endif |
| 818 | |
| 819 | /*-************************************************************** |
| 820 | * Alignment |
| 821 | *****************************************************************/ |
| 822 | |
| 823 | /* @return 1 if @u is a 2^n value, 0 otherwise |
| 824 | * useful to check a value is valid for alignment restrictions */ |
| 825 | MEM_STATIC int ZSTD_isPower2(size_t u) { |
| 826 | return (u & (u-1)) == 0; |
| 827 | } |
| 828 | |
| 829 | /* this test was initially positioned in mem.h, |
| 830 | * but this file is removed (or replaced) for linux kernel |
| 831 | * so it's now hosted in compiler.h, |
| 832 | * which remains valid for both user & kernel spaces. |
| 833 | */ |
| 834 | |
| 835 | #ifndef ZSTD_ALIGNOF |
| 836 | # if defined(__GNUC__) || defined(_MSC_VER) |
| 837 | /* covers gcc, clang & MSVC */ |
| 838 | /* note : this section must come first, before C11, |
| 839 | * due to a limitation in the kernel source generator */ |
| 840 | # define ZSTD_ALIGNOF(T) __alignof(T) |
| 841 | |
| 842 | # elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) |
| 843 | /* C11 support */ |
| 844 | # include <stdalign.h> |
| 845 | # define ZSTD_ALIGNOF(T) alignof(T) |
| 846 | |
| 847 | # else |
| 848 | /* No known support for alignof() - imperfect backup */ |
| 849 | # define ZSTD_ALIGNOF(T) (sizeof(void*) < sizeof(T) ? sizeof(void*) : sizeof(T)) |
| 850 | |
| 851 | # endif |
| 852 | #endif /* ZSTD_ALIGNOF */ |
| 853 | |
| 854 | #ifndef ZSTD_ALIGNED |
| 855 | /* C90-compatible alignment macro (GCC/Clang). Adjust for other compilers if needed. */ |
| 856 | # if defined(__GNUC__) || defined(__clang__) |
| 857 | # define ZSTD_ALIGNED(a) __attribute__((aligned(a))) |
| 858 | # elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */ |
| 859 | # define ZSTD_ALIGNED(a) _Alignas(a) |
| 860 | #elif defined(_MSC_VER) |
| 861 | # define ZSTD_ALIGNED(n) __declspec(align(n)) |
| 862 | # else |
| 863 | /* this compiler will require its own alignment instruction */ |
| 864 | # define ZSTD_ALIGNED(...) |
| 865 | # endif |
| 866 | #endif /* ZSTD_ALIGNED */ |
| 867 | |
| 868 | |
| 869 | /*-************************************************************** |
| 870 | * Sanitizer |
| 871 | *****************************************************************/ |
| 872 | |
| 873 | /** |
| 874 | * Zstd relies on pointer overflow in its decompressor. |
| 875 | * We add this attribute to functions that rely on pointer overflow. |
| 876 | */ |
| 877 | #ifndef ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
| 878 | # if __has_attribute(no_sanitize) |
| 879 | # if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 8 |
| 880 | /* gcc < 8 only has signed-integer-overlow which triggers on pointer overflow */ |
| 881 | # define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("signed-integer-overflow"))) |
| 882 | # else |
| 883 | /* older versions of clang [3.7, 5.0) will warn that pointer-overflow is ignored. */ |
| 884 | # define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("pointer-overflow"))) |
| 885 | # endif |
| 886 | # else |
| 887 | # define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
| 888 | # endif |
| 889 | #endif |
| 890 | |
| 891 | /** |
| 892 | * Helper function to perform a wrapped pointer difference without triggering |
| 893 | * UBSAN. |
| 894 | * |
| 895 | * @returns lhs - rhs with wrapping |
| 896 | */ |
| 897 | MEM_STATIC |
| 898 | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
| 899 | ptrdiff_t ZSTD_wrappedPtrDiff(unsigned char const* lhs, unsigned char const* rhs) |
| 900 | { |
| 901 | return lhs - rhs; |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * Helper function to perform a wrapped pointer add without triggering UBSAN. |
| 906 | * |
| 907 | * @return ptr + add with wrapping |
| 908 | */ |
| 909 | MEM_STATIC |
| 910 | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
| 911 | unsigned char const* ZSTD_wrappedPtrAdd(unsigned char const* ptr, ptrdiff_t add) |
| 912 | { |
| 913 | return ptr + add; |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * Helper function to perform a wrapped pointer subtraction without triggering |
| 918 | * UBSAN. |
| 919 | * |
| 920 | * @return ptr - sub with wrapping |
| 921 | */ |
| 922 | MEM_STATIC |
| 923 | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
| 924 | unsigned char const* ZSTD_wrappedPtrSub(unsigned char const* ptr, ptrdiff_t sub) |
| 925 | { |
| 926 | return ptr - sub; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Helper function to add to a pointer that works around C's undefined behavior |
| 931 | * of adding 0 to NULL. |
| 932 | * |
| 933 | * @returns `ptr + add` except it defines `NULL + 0 == NULL`. |
| 934 | */ |
| 935 | MEM_STATIC |
| 936 | unsigned char* ZSTD_maybeNullPtrAdd(unsigned char* ptr, ptrdiff_t add) |
| 937 | { |
| 938 | return add > 0 ? ptr + add : ptr; |
| 939 | } |
| 940 | |
| 941 | /* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an |
| 942 | * abundance of caution, disable our custom poisoning on mingw. */ |
| 943 | #ifdef __MINGW32__ |
| 944 | #ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE |
| 945 | #define ZSTD_ASAN_DONT_POISON_WORKSPACE 1 |
| 946 | #endif |
| 947 | #ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE |
| 948 | #define ZSTD_MSAN_DONT_POISON_WORKSPACE 1 |
| 949 | #endif |
| 950 | #endif |
| 951 | |
| 952 | #if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE) |
| 953 | /* Not all platforms that support msan provide sanitizers/msan_interface.h. |
| 954 | * We therefore declare the functions we need ourselves, rather than trying to |
| 955 | * include the header file... */ |
| 956 | #include <stddef.h> /* size_t */ |
| 957 | #define ZSTD_DEPS_NEED_STDINT |
| 958 | /**** skipping file: zstd_deps.h ****/ |
| 959 | |
| 960 | /* Make memory region fully initialized (without changing its contents). */ |
| 961 | void __msan_unpoison(const volatile void *a, size_t size); |
| 962 | |
| 963 | /* Make memory region fully uninitialized (without changing its contents). |
| 964 | This is a legacy interface that does not update origin information. Use |
| 965 | __msan_allocated_memory() instead. */ |
| 966 | void __msan_poison(const volatile void *a, size_t size); |
| 967 | |
| 968 | /* Returns the offset of the first (at least partially) poisoned byte in the |
| 969 | memory range, or -1 if the whole range is good. */ |
| 970 | intptr_t __msan_test_shadow(const volatile void *x, size_t size); |
| 971 | |
| 972 | /* Print shadow and origin for the memory range to stderr in a human-readable |
| 973 | format. */ |
| 974 | void __msan_print_shadow(const volatile void *x, size_t size); |
| 975 | #endif |
| 976 | |
| 977 | #if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE) |
| 978 | /* Not all platforms that support asan provide sanitizers/asan_interface.h. |
| 979 | * We therefore declare the functions we need ourselves, rather than trying to |
| 980 | * include the header file... */ |
| 981 | #include <stddef.h> /* size_t */ |
| 982 | |
| 983 | /** |
| 984 | * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable. |
| 985 | * |
| 986 | * This memory must be previously allocated by your program. Instrumented |
| 987 | * code is forbidden from accessing addresses in this region until it is |
| 988 | * unpoisoned. This function is not guaranteed to poison the entire region - |
| 989 | * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan |
| 990 | * alignment restrictions. |
| 991 | * |
| 992 | * \note This function is not thread-safe because no two threads can poison or |
| 993 | * unpoison memory in the same memory region simultaneously. |
| 994 | * |
| 995 | * \param addr Start of memory region. |
| 996 | * \param size Size of memory region. */ |
| 997 | void __asan_poison_memory_region(void const volatile *addr, size_t size); |
| 998 | |
| 999 | /** |
| 1000 | * Marks a memory region (<c>[addr, addr+size)</c>) as addressable. |
| 1001 | * |
| 1002 | * This memory must be previously allocated by your program. Accessing |
| 1003 | * addresses in this region is allowed until this region is poisoned again. |
| 1004 | * This function could unpoison a super-region of <c>[addr, addr+size)</c> due |
| 1005 | * to ASan alignment restrictions. |
| 1006 | * |
| 1007 | * \note This function is not thread-safe because no two threads can |
| 1008 | * poison or unpoison memory in the same memory region simultaneously. |
| 1009 | * |
| 1010 | * \param addr Start of memory region. |
| 1011 | * \param size Size of memory region. */ |
| 1012 | void __asan_unpoison_memory_region(void const volatile *addr, size_t size); |
| 1013 | #endif |
| 1014 | |
| 1015 | #endif /* ZSTD_COMPILER_H */ |
| 1016 | /**** ended inlining compiler.h ****/ |
| 1017 | /**** skipping file: debug.h ****/ |
| 1018 | /**** skipping file: zstd_deps.h ****/ |
| 1019 | |
| 1020 | |
| 1021 | /*-**************************************** |
| 1022 | * Compiler specifics |
| 1023 | ******************************************/ |
| 1024 | #if defined(_MSC_VER) /* Visual Studio */ |
| 1025 | # include <stdlib.h> /* _byteswap_ulong */ |
| 1026 | # include <intrin.h> /* _byteswap_* */ |
| 1027 | #elif defined(__ICCARM__) |
| 1028 | # include <intrinsics.h> |
| 1029 | #endif |
| 1030 | |
| 1031 | /*-************************************************************** |
| 1032 | * Basic Types |
| 1033 | *****************************************************************/ |
| 1034 | #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) |
| 1035 | # if defined(_AIX) |
| 1036 | # include <inttypes.h> |
| 1037 | # else |
| 1038 | # include <stdint.h> /* intptr_t */ |
| 1039 | # endif |
| 1040 | typedef uint8_t BYTE; |
| 1041 | typedef uint8_t U8; |
| 1042 | typedef int8_t S8; |
| 1043 | typedef uint16_t U16; |
| 1044 | typedef int16_t S16; |
| 1045 | typedef uint32_t U32; |
| 1046 | typedef int32_t S32; |
| 1047 | typedef uint64_t U64; |
| 1048 | typedef int64_t S64; |
| 1049 | #else |
| 1050 | # include <limits.h> |
| 1051 | #if CHAR_BIT != 8 |
| 1052 | # error "this implementation requires char to be exactly 8-bit type" |
| 1053 | #endif |
| 1054 | typedef unsigned char BYTE; |
| 1055 | typedef unsigned char U8; |
| 1056 | typedef signed char S8; |
| 1057 | #if USHRT_MAX != 65535 |
| 1058 | # error "this implementation requires short to be exactly 16-bit type" |
| 1059 | #endif |
| 1060 | typedef unsigned short U16; |
| 1061 | typedef signed short S16; |
| 1062 | #if UINT_MAX != 4294967295 |
| 1063 | # error "this implementation requires int to be exactly 32-bit type" |
| 1064 | #endif |
| 1065 | typedef unsigned int U32; |
| 1066 | typedef signed int S32; |
| 1067 | /* note : there are no limits defined for long long type in C90. |
| 1068 | * limits exist in C99, however, in such case, <stdint.h> is preferred */ |
| 1069 | typedef unsigned long long U64; |
| 1070 | typedef signed long long S64; |
| 1071 | #endif |
| 1072 | |
| 1073 | /*-************************************************************** |
| 1074 | * Memory I/O API |
| 1075 | *****************************************************************/ |
| 1076 | /*=== Static platform detection ===*/ |
| 1077 | MEM_STATIC unsigned MEM_32bits(void); |
| 1078 | MEM_STATIC unsigned MEM_64bits(void); |
| 1079 | MEM_STATIC unsigned MEM_isLittleEndian(void); |
| 1080 | |
| 1081 | /*=== Native unaligned read/write ===*/ |
| 1082 | MEM_STATIC U16 MEM_read16(const void* memPtr); |
| 1083 | MEM_STATIC U32 MEM_read32(const void* memPtr); |
| 1084 | MEM_STATIC U64 MEM_read64(const void* memPtr); |
| 1085 | MEM_STATIC size_t MEM_readST(const void* memPtr); |
| 1086 | |
| 1087 | MEM_STATIC void MEM_write16(void* memPtr, U16 value); |
| 1088 | MEM_STATIC void MEM_write32(void* memPtr, U32 value); |
| 1089 | MEM_STATIC void MEM_write64(void* memPtr, U64 value); |
| 1090 | |
| 1091 | /*=== Little endian unaligned read/write ===*/ |
| 1092 | MEM_STATIC U16 MEM_readLE16(const void* memPtr); |
| 1093 | MEM_STATIC U32 MEM_readLE24(const void* memPtr); |
| 1094 | MEM_STATIC U32 MEM_readLE32(const void* memPtr); |
| 1095 | MEM_STATIC U64 MEM_readLE64(const void* memPtr); |
| 1096 | MEM_STATIC size_t MEM_readLEST(const void* memPtr); |
| 1097 | |
| 1098 | MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val); |
| 1099 | MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val); |
| 1100 | MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32); |
| 1101 | MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64); |
| 1102 | MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val); |
| 1103 | |
| 1104 | /*=== Big endian unaligned read/write ===*/ |
| 1105 | MEM_STATIC U32 MEM_readBE32(const void* memPtr); |
| 1106 | MEM_STATIC U64 MEM_readBE64(const void* memPtr); |
| 1107 | MEM_STATIC size_t MEM_readBEST(const void* memPtr); |
| 1108 | |
| 1109 | MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32); |
| 1110 | MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64); |
| 1111 | MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val); |
| 1112 | |
| 1113 | /*=== Byteswap ===*/ |
| 1114 | MEM_STATIC U32 MEM_swap32(U32 in); |
| 1115 | MEM_STATIC U64 MEM_swap64(U64 in); |
| 1116 | MEM_STATIC size_t MEM_swapST(size_t in); |
| 1117 | |
| 1118 | |
| 1119 | /*-************************************************************** |
| 1120 | * Memory I/O Implementation |
| 1121 | *****************************************************************/ |
| 1122 | /* MEM_FORCE_MEMORY_ACCESS : For accessing unaligned memory: |
| 1123 | * Method 0 : always use `memcpy()`. Safe and portable. |
| 1124 | * Method 1 : Use compiler extension to set unaligned access. |
| 1125 | * Method 2 : direct access. This method is portable but violate C standard. |
| 1126 | * It can generate buggy code on targets depending on alignment. |
| 1127 | * Default : method 1 if supported, else method 0 |
| 1128 | */ |
| 1129 | #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ |
| 1130 | # ifdef __GNUC__ |
| 1131 | # define MEM_FORCE_MEMORY_ACCESS 1 |
| 1132 | # endif |
| 1133 | #endif |
| 1134 | |
| 1135 | MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; } |
| 1136 | MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; } |
| 1137 | |
| 1138 | MEM_STATIC unsigned MEM_isLittleEndian(void) |
| 1139 | { |
| 1140 | #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 1141 | return 1; |
| 1142 | #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 1143 | return 0; |
| 1144 | #elif defined(__clang__) && __LITTLE_ENDIAN__ |
| 1145 | return 1; |
| 1146 | #elif defined(__clang__) && __BIG_ENDIAN__ |
| 1147 | return 0; |
| 1148 | #elif defined(_MSC_VER) && (_M_X64 || _M_IX86) |
| 1149 | return 1; |
| 1150 | #elif defined(__DMC__) && defined(_M_IX86) |
| 1151 | return 1; |
| 1152 | #elif defined(__IAR_SYSTEMS_ICC__) && __LITTLE_ENDIAN__ |
| 1153 | return 1; |
| 1154 | #else |
| 1155 | const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ |
| 1156 | return one.c[0]; |
| 1157 | #endif |
| 1158 | } |
| 1159 | |
| 1160 | #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2) |
| 1161 | |
| 1162 | /* violates C standard, by lying on structure alignment. |
| 1163 | Only use if no other choice to achieve best performance on target platform */ |
| 1164 | MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; } |
| 1165 | MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; } |
| 1166 | MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; } |
| 1167 | MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; } |
| 1168 | |
| 1169 | MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } |
| 1170 | MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } |
| 1171 | MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; } |
| 1172 | |
| 1173 | #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1) |
| 1174 | |
| 1175 | typedef __attribute__((aligned(1))) U16 unalign16; |
| 1176 | typedef __attribute__((aligned(1))) U32 unalign32; |
| 1177 | typedef __attribute__((aligned(1))) U64 unalign64; |
| 1178 | typedef __attribute__((aligned(1))) size_t unalignArch; |
| 1179 | |
| 1180 | MEM_STATIC U16 MEM_read16(const void* ptr) { return *(const unalign16*)ptr; } |
| 1181 | MEM_STATIC U32 MEM_read32(const void* ptr) { return *(const unalign32*)ptr; } |
| 1182 | MEM_STATIC U64 MEM_read64(const void* ptr) { return *(const unalign64*)ptr; } |
| 1183 | MEM_STATIC size_t MEM_readST(const void* ptr) { return *(const unalignArch*)ptr; } |
| 1184 | |
| 1185 | MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(unalign16*)memPtr = value; } |
| 1186 | MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(unalign32*)memPtr = value; } |
| 1187 | MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(unalign64*)memPtr = value; } |
| 1188 | |
| 1189 | #else |
| 1190 | |
| 1191 | /* default method, safe and standard. |
| 1192 | can sometimes prove slower */ |
| 1193 | |
| 1194 | MEM_STATIC U16 MEM_read16(const void* memPtr) |
| 1195 | { |
| 1196 | U16 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val; |
| 1197 | } |
| 1198 | |
| 1199 | MEM_STATIC U32 MEM_read32(const void* memPtr) |
| 1200 | { |
| 1201 | U32 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val; |
| 1202 | } |
| 1203 | |
| 1204 | MEM_STATIC U64 MEM_read64(const void* memPtr) |
| 1205 | { |
| 1206 | U64 val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val; |
| 1207 | } |
| 1208 | |
| 1209 | MEM_STATIC size_t MEM_readST(const void* memPtr) |
| 1210 | { |
| 1211 | size_t val; ZSTD_memcpy(&val, memPtr, sizeof(val)); return val; |
| 1212 | } |
| 1213 | |
| 1214 | MEM_STATIC void MEM_write16(void* memPtr, U16 value) |
| 1215 | { |
| 1216 | ZSTD_memcpy(memPtr, &value, sizeof(value)); |
| 1217 | } |
| 1218 | |
| 1219 | MEM_STATIC void MEM_write32(void* memPtr, U32 value) |
| 1220 | { |
| 1221 | ZSTD_memcpy(memPtr, &value, sizeof(value)); |
| 1222 | } |
| 1223 | |
| 1224 | MEM_STATIC void MEM_write64(void* memPtr, U64 value) |
| 1225 | { |
| 1226 | ZSTD_memcpy(memPtr, &value, sizeof(value)); |
| 1227 | } |
| 1228 | |
| 1229 | #endif /* MEM_FORCE_MEMORY_ACCESS */ |
| 1230 | |
| 1231 | MEM_STATIC U32 MEM_swap32_fallback(U32 in) |
| 1232 | { |
| 1233 | return ((in << 24) & 0xff000000 ) | |
| 1234 | ((in << 8) & 0x00ff0000 ) | |
| 1235 | ((in >> 8) & 0x0000ff00 ) | |
| 1236 | ((in >> 24) & 0x000000ff ); |
| 1237 | } |
| 1238 | |
| 1239 | MEM_STATIC U32 MEM_swap32(U32 in) |
| 1240 | { |
| 1241 | #if defined(_MSC_VER) /* Visual Studio */ |
| 1242 | return _byteswap_ulong(in); |
| 1243 | #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \ |
| 1244 | || (defined(__clang__) && __has_builtin(__builtin_bswap32)) |
| 1245 | return __builtin_bswap32(in); |
| 1246 | #elif defined(__ICCARM__) |
| 1247 | return __REV(in); |
| 1248 | #else |
| 1249 | return MEM_swap32_fallback(in); |
| 1250 | #endif |
| 1251 | } |
| 1252 | |
| 1253 | MEM_STATIC U64 MEM_swap64_fallback(U64 in) |
| 1254 | { |
| 1255 | return ((in << 56) & 0xff00000000000000ULL) | |
| 1256 | ((in << 40) & 0x00ff000000000000ULL) | |
| 1257 | ((in << 24) & 0x0000ff0000000000ULL) | |
| 1258 | ((in << 8) & 0x000000ff00000000ULL) | |
| 1259 | ((in >> 8) & 0x00000000ff000000ULL) | |
| 1260 | ((in >> 24) & 0x0000000000ff0000ULL) | |
| 1261 | ((in >> 40) & 0x000000000000ff00ULL) | |
| 1262 | ((in >> 56) & 0x00000000000000ffULL); |
| 1263 | } |
| 1264 | |
| 1265 | MEM_STATIC U64 MEM_swap64(U64 in) |
| 1266 | { |
| 1267 | #if defined(_MSC_VER) /* Visual Studio */ |
| 1268 | return _byteswap_uint64(in); |
| 1269 | #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \ |
| 1270 | || (defined(__clang__) && __has_builtin(__builtin_bswap64)) |
| 1271 | return __builtin_bswap64(in); |
| 1272 | #else |
| 1273 | return MEM_swap64_fallback(in); |
| 1274 | #endif |
| 1275 | } |
| 1276 | |
| 1277 | MEM_STATIC size_t MEM_swapST(size_t in) |
| 1278 | { |
| 1279 | if (MEM_32bits()) |
| 1280 | return (size_t)MEM_swap32((U32)in); |
| 1281 | else |
| 1282 | return (size_t)MEM_swap64((U64)in); |
| 1283 | } |
| 1284 | |
| 1285 | /*=== Little endian r/w ===*/ |
| 1286 | |
| 1287 | MEM_STATIC U16 MEM_readLE16(const void* memPtr) |
| 1288 | { |
| 1289 | if (MEM_isLittleEndian()) |
| 1290 | return MEM_read16(memPtr); |
| 1291 | else { |
| 1292 | const BYTE* p = (const BYTE*)memPtr; |
| 1293 | return (U16)(p[0] + (p[1]<<8)); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) |
| 1298 | { |
| 1299 | if (MEM_isLittleEndian()) { |
| 1300 | MEM_write16(memPtr, val); |
| 1301 | } else { |
| 1302 | BYTE* p = (BYTE*)memPtr; |
| 1303 | p[0] = (BYTE)val; |
| 1304 | p[1] = (BYTE)(val>>8); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | MEM_STATIC U32 MEM_readLE24(const void* memPtr) |
| 1309 | { |
| 1310 | return (U32)MEM_readLE16(memPtr) + ((U32)(((const BYTE*)memPtr)[2]) << 16); |
| 1311 | } |
| 1312 | |
| 1313 | MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val) |
| 1314 | { |
| 1315 | MEM_writeLE16(memPtr, (U16)val); |
| 1316 | ((BYTE*)memPtr)[2] = (BYTE)(val>>16); |
| 1317 | } |
| 1318 | |
| 1319 | MEM_STATIC U32 MEM_readLE32(const void* memPtr) |
| 1320 | { |
| 1321 | if (MEM_isLittleEndian()) |
| 1322 | return MEM_read32(memPtr); |
| 1323 | else |
| 1324 | return MEM_swap32(MEM_read32(memPtr)); |
| 1325 | } |
| 1326 | |
| 1327 | MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32) |
| 1328 | { |
| 1329 | if (MEM_isLittleEndian()) |
| 1330 | MEM_write32(memPtr, val32); |
| 1331 | else |
| 1332 | MEM_write32(memPtr, MEM_swap32(val32)); |
| 1333 | } |
| 1334 | |
| 1335 | MEM_STATIC U64 MEM_readLE64(const void* memPtr) |
| 1336 | { |
| 1337 | if (MEM_isLittleEndian()) |
| 1338 | return MEM_read64(memPtr); |
| 1339 | else |
| 1340 | return MEM_swap64(MEM_read64(memPtr)); |
| 1341 | } |
| 1342 | |
| 1343 | MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64) |
| 1344 | { |
| 1345 | if (MEM_isLittleEndian()) |
| 1346 | MEM_write64(memPtr, val64); |
| 1347 | else |
| 1348 | MEM_write64(memPtr, MEM_swap64(val64)); |
| 1349 | } |
| 1350 | |
| 1351 | MEM_STATIC size_t MEM_readLEST(const void* memPtr) |
| 1352 | { |
| 1353 | if (MEM_32bits()) |
| 1354 | return (size_t)MEM_readLE32(memPtr); |
| 1355 | else |
| 1356 | return (size_t)MEM_readLE64(memPtr); |
| 1357 | } |
| 1358 | |
| 1359 | MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val) |
| 1360 | { |
| 1361 | if (MEM_32bits()) |
| 1362 | MEM_writeLE32(memPtr, (U32)val); |
| 1363 | else |
| 1364 | MEM_writeLE64(memPtr, (U64)val); |
| 1365 | } |
| 1366 | |
| 1367 | /*=== Big endian r/w ===*/ |
| 1368 | |
| 1369 | MEM_STATIC U32 MEM_readBE32(const void* memPtr) |
| 1370 | { |
| 1371 | if (MEM_isLittleEndian()) |
| 1372 | return MEM_swap32(MEM_read32(memPtr)); |
| 1373 | else |
| 1374 | return MEM_read32(memPtr); |
| 1375 | } |
| 1376 | |
| 1377 | MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32) |
| 1378 | { |
| 1379 | if (MEM_isLittleEndian()) |
| 1380 | MEM_write32(memPtr, MEM_swap32(val32)); |
| 1381 | else |
| 1382 | MEM_write32(memPtr, val32); |
| 1383 | } |
| 1384 | |
| 1385 | MEM_STATIC U64 MEM_readBE64(const void* memPtr) |
| 1386 | { |
| 1387 | if (MEM_isLittleEndian()) |
| 1388 | return MEM_swap64(MEM_read64(memPtr)); |
| 1389 | else |
| 1390 | return MEM_read64(memPtr); |
| 1391 | } |
| 1392 | |
| 1393 | MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64) |
| 1394 | { |
| 1395 | if (MEM_isLittleEndian()) |
| 1396 | MEM_write64(memPtr, MEM_swap64(val64)); |
| 1397 | else |
| 1398 | MEM_write64(memPtr, val64); |
| 1399 | } |
| 1400 | |
| 1401 | MEM_STATIC size_t MEM_readBEST(const void* memPtr) |
| 1402 | { |
| 1403 | if (MEM_32bits()) |
| 1404 | return (size_t)MEM_readBE32(memPtr); |
| 1405 | else |
| 1406 | return (size_t)MEM_readBE64(memPtr); |
| 1407 | } |
| 1408 | |
| 1409 | MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val) |
| 1410 | { |
| 1411 | if (MEM_32bits()) |
| 1412 | MEM_writeBE32(memPtr, (U32)val); |
| 1413 | else |
| 1414 | MEM_writeBE64(memPtr, (U64)val); |
| 1415 | } |
| 1416 | |
| 1417 | /* code only tested on 32 and 64 bits systems */ |
| 1418 | MEM_STATIC void MEM_check(void) { DEBUG_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); } |
| 1419 | |
| 1420 | #endif /* MEM_H_MODULE */ |
| 1421 | /**** ended inlining mem.h ****/ |
| 1422 | /**** start inlining error_private.h ****/ |
| 1423 | /* |
| 1424 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 1425 | * All rights reserved. |
| 1426 | * |
| 1427 | * This source code is licensed under both the BSD-style license (found in the |
| 1428 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 1429 | * in the COPYING file in the root directory of this source tree). |
| 1430 | * You may select, at your option, one of the above-listed licenses. |
| 1431 | */ |
| 1432 | |
| 1433 | /* Note : this module is expected to remain private, do not expose it */ |
| 1434 | |
| 1435 | #ifndef ERROR_H_MODULE |
| 1436 | #define ERROR_H_MODULE |
| 1437 | |
| 1438 | /* **************************************** |
| 1439 | * Dependencies |
| 1440 | ******************************************/ |
| 1441 | /**** start inlining ../zstd_errors.h ****/ |
| 1442 | /* |
| 1443 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 1444 | * All rights reserved. |
| 1445 | * |
| 1446 | * This source code is licensed under both the BSD-style license (found in the |
| 1447 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 1448 | * in the COPYING file in the root directory of this source tree). |
| 1449 | * You may select, at your option, one of the above-listed licenses. |
| 1450 | */ |
| 1451 | |
| 1452 | #ifndef ZSTD_ERRORS_H_398273423 |
| 1453 | #define ZSTD_ERRORS_H_398273423 |
| 1454 | |
| 1455 | #if defined (__cplusplus) |
| 1456 | extern "C" { |
| 1457 | #endif |
| 1458 | |
| 1459 | /* ===== ZSTDERRORLIB_API : control library symbols visibility ===== */ |
| 1460 | #ifndef ZSTDERRORLIB_VISIBLE |
| 1461 | /* Backwards compatibility with old macro name */ |
| 1462 | # ifdef ZSTDERRORLIB_VISIBILITY |
| 1463 | # define ZSTDERRORLIB_VISIBLE ZSTDERRORLIB_VISIBILITY |
| 1464 | # elif defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__MINGW32__) |
| 1465 | # define ZSTDERRORLIB_VISIBLE __attribute__ ((visibility ("default"))) |
| 1466 | # else |
| 1467 | # define ZSTDERRORLIB_VISIBLE |
| 1468 | # endif |
| 1469 | #endif |
| 1470 | |
| 1471 | #ifndef ZSTDERRORLIB_HIDDEN |
| 1472 | # if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__MINGW32__) |
| 1473 | # define ZSTDERRORLIB_HIDDEN __attribute__ ((visibility ("hidden"))) |
| 1474 | # else |
| 1475 | # define ZSTDERRORLIB_HIDDEN |
| 1476 | # endif |
| 1477 | #endif |
| 1478 | |
| 1479 | #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) |
| 1480 | # define ZSTDERRORLIB_API __declspec(dllexport) ZSTDERRORLIB_VISIBLE |
| 1481 | #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) |
| 1482 | # define ZSTDERRORLIB_API __declspec(dllimport) ZSTDERRORLIB_VISIBLE /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ |
| 1483 | #else |
| 1484 | # define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBLE |
| 1485 | #endif |
| 1486 | |
| 1487 | /*-********************************************* |
| 1488 | * Error codes list |
| 1489 | *-********************************************* |
| 1490 | * Error codes _values_ are pinned down since v1.3.1 only. |
| 1491 | * Therefore, don't rely on values if you may link to any version < v1.3.1. |
| 1492 | * |
| 1493 | * Only values < 100 are considered stable. |
| 1494 | * |
| 1495 | * note 1 : this API shall be used with static linking only. |
| 1496 | * dynamic linking is not yet officially supported. |
| 1497 | * note 2 : Prefer relying on the enum than on its value whenever possible |
| 1498 | * This is the only supported way to use the error list < v1.3.1 |
| 1499 | * note 3 : ZSTD_isError() is always correct, whatever the library version. |
| 1500 | **********************************************/ |
| 1501 | typedef enum { |
| 1502 | ZSTD_error_no_error = 0, |
| 1503 | ZSTD_error_GENERIC = 1, |
| 1504 | ZSTD_error_prefix_unknown = 10, |
| 1505 | ZSTD_error_version_unsupported = 12, |
| 1506 | ZSTD_error_frameParameter_unsupported = 14, |
| 1507 | ZSTD_error_frameParameter_windowTooLarge = 16, |
| 1508 | ZSTD_error_corruption_detected = 20, |
| 1509 | ZSTD_error_checksum_wrong = 22, |
| 1510 | ZSTD_error_literals_headerWrong = 24, |
| 1511 | ZSTD_error_dictionary_corrupted = 30, |
| 1512 | ZSTD_error_dictionary_wrong = 32, |
| 1513 | ZSTD_error_dictionaryCreation_failed = 34, |
| 1514 | ZSTD_error_parameter_unsupported = 40, |
| 1515 | ZSTD_error_parameter_combination_unsupported = 41, |
| 1516 | ZSTD_error_parameter_outOfBound = 42, |
| 1517 | ZSTD_error_tableLog_tooLarge = 44, |
| 1518 | ZSTD_error_maxSymbolValue_tooLarge = 46, |
| 1519 | ZSTD_error_maxSymbolValue_tooSmall = 48, |
| 1520 | ZSTD_error_cannotProduce_uncompressedBlock = 49, |
| 1521 | ZSTD_error_stabilityCondition_notRespected = 50, |
| 1522 | ZSTD_error_stage_wrong = 60, |
| 1523 | ZSTD_error_init_missing = 62, |
| 1524 | ZSTD_error_memory_allocation = 64, |
| 1525 | ZSTD_error_workSpace_tooSmall= 66, |
| 1526 | ZSTD_error_dstSize_tooSmall = 70, |
| 1527 | ZSTD_error_srcSize_wrong = 72, |
| 1528 | ZSTD_error_dstBuffer_null = 74, |
| 1529 | ZSTD_error_noForwardProgress_destFull = 80, |
| 1530 | ZSTD_error_noForwardProgress_inputEmpty = 82, |
| 1531 | /* following error codes are __NOT STABLE__, they can be removed or changed in future versions */ |
| 1532 | ZSTD_error_frameIndex_tooLarge = 100, |
| 1533 | ZSTD_error_seekableIO = 102, |
| 1534 | ZSTD_error_dstBuffer_wrong = 104, |
| 1535 | ZSTD_error_srcBuffer_wrong = 105, |
| 1536 | ZSTD_error_sequenceProducer_failed = 106, |
| 1537 | ZSTD_error_externalSequences_invalid = 107, |
| 1538 | ZSTD_error_maxCode = 120 /* never EVER use this value directly, it can change in future versions! Use ZSTD_isError() instead */ |
| 1539 | } ZSTD_ErrorCode; |
| 1540 | |
| 1541 | ZSTDERRORLIB_API const char* ZSTD_getErrorString(ZSTD_ErrorCode code); /**< Same as ZSTD_getErrorName, but using a `ZSTD_ErrorCode` enum argument */ |
| 1542 | |
| 1543 | |
| 1544 | #if defined (__cplusplus) |
| 1545 | } |
| 1546 | #endif |
| 1547 | |
| 1548 | #endif /* ZSTD_ERRORS_H_398273423 */ |
| 1549 | /**** ended inlining ../zstd_errors.h ****/ |
| 1550 | /**** skipping file: compiler.h ****/ |
| 1551 | /**** skipping file: debug.h ****/ |
| 1552 | /**** skipping file: zstd_deps.h ****/ |
| 1553 | |
| 1554 | /* **************************************** |
| 1555 | * Compiler-specific |
| 1556 | ******************************************/ |
| 1557 | #if defined(__GNUC__) |
| 1558 | # define ERR_STATIC static __attribute__((unused)) |
| 1559 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
| 1560 | # define ERR_STATIC static inline |
| 1561 | #elif defined(_MSC_VER) |
| 1562 | # define ERR_STATIC static __inline |
| 1563 | #else |
| 1564 | # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ |
| 1565 | #endif |
| 1566 | |
| 1567 | |
| 1568 | /*-**************************************** |
| 1569 | * Customization (error_public.h) |
| 1570 | ******************************************/ |
| 1571 | typedef ZSTD_ErrorCode ERR_enum; |
| 1572 | #define PREFIX(name) ZSTD_error_##name |
| 1573 | |
| 1574 | |
| 1575 | /*-**************************************** |
| 1576 | * Error codes handling |
| 1577 | ******************************************/ |
| 1578 | #undef ERROR /* already defined on Visual Studio */ |
| 1579 | #define ERROR(name) ZSTD_ERROR(name) |
| 1580 | #define ZSTD_ERROR(name) ((size_t)-PREFIX(name)) |
| 1581 | |
| 1582 | ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } |
| 1583 | |
| 1584 | ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); } |
| 1585 | |
| 1586 | /* check and forward error code */ |
| 1587 | #define CHECK_V_F(e, f) \ |
| 1588 | size_t const e = f; \ |
| 1589 | do { \ |
| 1590 | if (ERR_isError(e)) \ |
| 1591 | return e; \ |
| 1592 | } while (0) |
| 1593 | #define CHECK_F(f) do { CHECK_V_F(_var_err__, f); } while (0) |
| 1594 | |
| 1595 | |
| 1596 | /*-**************************************** |
| 1597 | * Error Strings |
| 1598 | ******************************************/ |
| 1599 | |
| 1600 | const char* ERR_getErrorString(ERR_enum code); /* error_private.c */ |
| 1601 | |
| 1602 | ERR_STATIC const char* ERR_getErrorName(size_t code) |
| 1603 | { |
| 1604 | return ERR_getErrorString(ERR_getErrorCode(code)); |
| 1605 | } |
| 1606 | |
| 1607 | /** |
| 1608 | * Ignore: this is an internal helper. |
| 1609 | * |
| 1610 | * This is a helper function to help force C99-correctness during compilation. |
| 1611 | * Under strict compilation modes, variadic macro arguments can't be empty. |
| 1612 | * However, variadic function arguments can be. Using a function therefore lets |
| 1613 | * us statically check that at least one (string) argument was passed, |
| 1614 | * independent of the compilation flags. |
| 1615 | */ |
| 1616 | static INLINE_KEYWORD UNUSED_ATTR |
| 1617 | void _force_has_format_string(const char *format, ...) { |
| 1618 | (void)format; |
| 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * Ignore: this is an internal helper. |
| 1623 | * |
| 1624 | * We want to force this function invocation to be syntactically correct, but |
| 1625 | * we don't want to force runtime evaluation of its arguments. |
| 1626 | */ |
| 1627 | #define _FORCE_HAS_FORMAT_STRING(...) \ |
| 1628 | do { \ |
| 1629 | if (0) { \ |
| 1630 | _force_has_format_string(__VA_ARGS__); \ |
| 1631 | } \ |
| 1632 | } while (0) |
| 1633 | |
| 1634 | #define ERR_QUOTE(str) #str |
| 1635 | |
| 1636 | /** |
| 1637 | * Return the specified error if the condition evaluates to true. |
| 1638 | * |
| 1639 | * In debug modes, prints additional information. |
| 1640 | * In order to do that (particularly, printing the conditional that failed), |
| 1641 | * this can't just wrap RETURN_ERROR(). |
| 1642 | */ |
| 1643 | #define RETURN_ERROR_IF(cond, err, ...) \ |
| 1644 | do { \ |
| 1645 | if (cond) { \ |
| 1646 | RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \ |
| 1647 | __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \ |
| 1648 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| 1649 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 1650 | RAWLOG(3, "\n"); \ |
| 1651 | return ERROR(err); \ |
| 1652 | } \ |
| 1653 | } while (0) |
| 1654 | |
| 1655 | /** |
| 1656 | * Unconditionally return the specified error. |
| 1657 | * |
| 1658 | * In debug modes, prints additional information. |
| 1659 | */ |
| 1660 | #define RETURN_ERROR(err, ...) \ |
| 1661 | do { \ |
| 1662 | RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \ |
| 1663 | __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \ |
| 1664 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| 1665 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 1666 | RAWLOG(3, "\n"); \ |
| 1667 | return ERROR(err); \ |
| 1668 | } while(0) |
| 1669 | |
| 1670 | /** |
| 1671 | * If the provided expression evaluates to an error code, returns that error code. |
| 1672 | * |
| 1673 | * In debug modes, prints additional information. |
| 1674 | */ |
| 1675 | #define FORWARD_IF_ERROR(err, ...) \ |
| 1676 | do { \ |
| 1677 | size_t const err_code = (err); \ |
| 1678 | if (ERR_isError(err_code)) { \ |
| 1679 | RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \ |
| 1680 | __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \ |
| 1681 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| 1682 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 1683 | RAWLOG(3, "\n"); \ |
| 1684 | return err_code; \ |
| 1685 | } \ |
| 1686 | } while(0) |
| 1687 | |
| 1688 | #endif /* ERROR_H_MODULE */ |
| 1689 | /**** ended inlining error_private.h ****/ |
| 1690 | #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ |
| 1691 | /**** start inlining fse.h ****/ |
| 1692 | /* ****************************************************************** |
| 1693 | * FSE : Finite State Entropy codec |
| 1694 | * Public Prototypes declaration |
| 1695 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 1696 | * |
| 1697 | * You can contact the author at : |
| 1698 | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 1699 | * |
| 1700 | * This source code is licensed under both the BSD-style license (found in the |
| 1701 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 1702 | * in the COPYING file in the root directory of this source tree). |
| 1703 | * You may select, at your option, one of the above-listed licenses. |
| 1704 | ****************************************************************** */ |
| 1705 | #ifndef FSE_H |
| 1706 | #define FSE_H |
| 1707 | |
| 1708 | |
| 1709 | /*-***************************************** |
| 1710 | * Dependencies |
| 1711 | ******************************************/ |
| 1712 | /**** skipping file: zstd_deps.h ****/ |
| 1713 | |
| 1714 | /*-***************************************** |
| 1715 | * FSE_PUBLIC_API : control library symbols visibility |
| 1716 | ******************************************/ |
| 1717 | #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4) |
| 1718 | # define FSE_PUBLIC_API __attribute__ ((visibility ("default"))) |
| 1719 | #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */ |
| 1720 | # define FSE_PUBLIC_API __declspec(dllexport) |
| 1721 | #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1) |
| 1722 | # define FSE_PUBLIC_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ |
| 1723 | #else |
| 1724 | # define FSE_PUBLIC_API |
| 1725 | #endif |
| 1726 | |
| 1727 | /*------ Version ------*/ |
| 1728 | #define FSE_VERSION_MAJOR 0 |
| 1729 | #define FSE_VERSION_MINOR 9 |
| 1730 | #define FSE_VERSION_RELEASE 0 |
| 1731 | |
| 1732 | #define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE |
| 1733 | #define FSE_QUOTE(str) #str |
| 1734 | #define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str) |
| 1735 | #define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION) |
| 1736 | |
| 1737 | #define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE) |
| 1738 | FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */ |
| 1739 | |
| 1740 | |
| 1741 | /*-***************************************** |
| 1742 | * Tool functions |
| 1743 | ******************************************/ |
| 1744 | FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */ |
| 1745 | |
| 1746 | /* Error Management */ |
| 1747 | FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ |
| 1748 | FSE_PUBLIC_API const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */ |
| 1749 | |
| 1750 | |
| 1751 | /*-***************************************** |
| 1752 | * FSE detailed API |
| 1753 | ******************************************/ |
| 1754 | /*! |
| 1755 | FSE_compress() does the following: |
| 1756 | 1. count symbol occurrence from source[] into table count[] (see hist.h) |
| 1757 | 2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) |
| 1758 | 3. save normalized counters to memory buffer using writeNCount() |
| 1759 | 4. build encoding table 'CTable' from normalized counters |
| 1760 | 5. encode the data stream using encoding table 'CTable' |
| 1761 | |
| 1762 | FSE_decompress() does the following: |
| 1763 | 1. read normalized counters with readNCount() |
| 1764 | 2. build decoding table 'DTable' from normalized counters |
| 1765 | 3. decode the data stream using decoding table 'DTable' |
| 1766 | |
| 1767 | The following API allows targeting specific sub-functions for advanced tasks. |
| 1768 | For example, it's possible to compress several blocks using the same 'CTable', |
| 1769 | or to save and provide normalized distribution using external method. |
| 1770 | */ |
| 1771 | |
| 1772 | /* *** COMPRESSION *** */ |
| 1773 | |
| 1774 | /*! FSE_optimalTableLog(): |
| 1775 | dynamically downsize 'tableLog' when conditions are met. |
| 1776 | It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. |
| 1777 | @return : recommended tableLog (necessarily <= 'maxTableLog') */ |
| 1778 | FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); |
| 1779 | |
| 1780 | /*! FSE_normalizeCount(): |
| 1781 | normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) |
| 1782 | 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). |
| 1783 | useLowProbCount is a boolean parameter which trades off compressed size for |
| 1784 | faster header decoding. When it is set to 1, the compressed data will be slightly |
| 1785 | smaller. And when it is set to 0, FSE_readNCount() and FSE_buildDTable() will be |
| 1786 | faster. If you are compressing a small amount of data (< 2 KB) then useLowProbCount=0 |
| 1787 | is a good default, since header deserialization makes a big speed difference. |
| 1788 | Otherwise, useLowProbCount=1 is a good default, since the speed difference is small. |
| 1789 | @return : tableLog, |
| 1790 | or an errorCode, which can be tested using FSE_isError() */ |
| 1791 | FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, |
| 1792 | const unsigned* count, size_t srcSize, unsigned maxSymbolValue, unsigned useLowProbCount); |
| 1793 | |
| 1794 | /*! FSE_NCountWriteBound(): |
| 1795 | Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'. |
| 1796 | Typically useful for allocation purpose. */ |
| 1797 | FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); |
| 1798 | |
| 1799 | /*! FSE_writeNCount(): |
| 1800 | Compactly save 'normalizedCounter' into 'buffer'. |
| 1801 | @return : size of the compressed table, |
| 1802 | or an errorCode, which can be tested using FSE_isError(). */ |
| 1803 | FSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize, |
| 1804 | const short* normalizedCounter, |
| 1805 | unsigned maxSymbolValue, unsigned tableLog); |
| 1806 | |
| 1807 | /*! Constructor and Destructor of FSE_CTable. |
| 1808 | Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ |
| 1809 | typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ |
| 1810 | |
| 1811 | /*! FSE_buildCTable(): |
| 1812 | Builds `ct`, which must be already allocated, using FSE_createCTable(). |
| 1813 | @return : 0, or an errorCode, which can be tested using FSE_isError() */ |
| 1814 | FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); |
| 1815 | |
| 1816 | /*! FSE_compress_usingCTable(): |
| 1817 | Compress `src` using `ct` into `dst` which must be already allocated. |
| 1818 | @return : size of compressed data (<= `dstCapacity`), |
| 1819 | or 0 if compressed data could not fit into `dst`, |
| 1820 | or an errorCode, which can be tested using FSE_isError() */ |
| 1821 | FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct); |
| 1822 | |
| 1823 | /*! |
| 1824 | Tutorial : |
| 1825 | ---------- |
| 1826 | The first step is to count all symbols. FSE_count() does this job very fast. |
| 1827 | Result will be saved into 'count', a table of unsigned int, which must be already allocated, and have 'maxSymbolValuePtr[0]+1' cells. |
| 1828 | 'src' is a table of bytes of size 'srcSize'. All values within 'src' MUST be <= maxSymbolValuePtr[0] |
| 1829 | maxSymbolValuePtr[0] will be updated, with its real value (necessarily <= original value) |
| 1830 | FSE_count() will return the number of occurrence of the most frequent symbol. |
| 1831 | This can be used to know if there is a single symbol within 'src', and to quickly evaluate its compressibility. |
| 1832 | If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). |
| 1833 | |
| 1834 | The next step is to normalize the frequencies. |
| 1835 | FSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'. |
| 1836 | It also guarantees a minimum of 1 to any Symbol with frequency >= 1. |
| 1837 | You can use 'tableLog'==0 to mean "use default tableLog value". |
| 1838 | If you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(), |
| 1839 | which will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined maximum (0 means "default"). |
| 1840 | |
| 1841 | The result of FSE_normalizeCount() will be saved into a table, |
| 1842 | called 'normalizedCounter', which is a table of signed short. |
| 1843 | 'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells. |
| 1844 | The return value is tableLog if everything proceeded as expected. |
| 1845 | It is 0 if there is a single symbol within distribution. |
| 1846 | If there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can be tested using FSE_isError()). |
| 1847 | |
| 1848 | 'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount(). |
| 1849 | 'buffer' must be already allocated. |
| 1850 | For guaranteed success, buffer size must be at least FSE_headerBound(). |
| 1851 | The result of the function is the number of bytes written into 'buffer'. |
| 1852 | If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError(); ex : buffer size too small). |
| 1853 | |
| 1854 | 'normalizedCounter' can then be used to create the compression table 'CTable'. |
| 1855 | The space required by 'CTable' must be already allocated, using FSE_createCTable(). |
| 1856 | You can then use FSE_buildCTable() to fill 'CTable'. |
| 1857 | If there is an error, both functions will return an ErrorCode (which can be tested using FSE_isError()). |
| 1858 | |
| 1859 | 'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). |
| 1860 | Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize' |
| 1861 | The function returns the size of compressed data (without header), necessarily <= `dstCapacity`. |
| 1862 | If it returns '0', compressed data could not fit into 'dst'. |
| 1863 | If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()). |
| 1864 | */ |
| 1865 | |
| 1866 | |
| 1867 | /* *** DECOMPRESSION *** */ |
| 1868 | |
| 1869 | /*! FSE_readNCount(): |
| 1870 | Read compactly saved 'normalizedCounter' from 'rBuffer'. |
| 1871 | @return : size read from 'rBuffer', |
| 1872 | or an errorCode, which can be tested using FSE_isError(). |
| 1873 | maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ |
| 1874 | FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter, |
| 1875 | unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, |
| 1876 | const void* rBuffer, size_t rBuffSize); |
| 1877 | |
| 1878 | /*! FSE_readNCount_bmi2(): |
| 1879 | * Same as FSE_readNCount() but pass bmi2=1 when your CPU supports BMI2 and 0 otherwise. |
| 1880 | */ |
| 1881 | FSE_PUBLIC_API size_t FSE_readNCount_bmi2(short* normalizedCounter, |
| 1882 | unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, |
| 1883 | const void* rBuffer, size_t rBuffSize, int bmi2); |
| 1884 | |
| 1885 | typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
| 1886 | |
| 1887 | /*! |
| 1888 | Tutorial : |
| 1889 | ---------- |
| 1890 | (Note : these functions only decompress FSE-compressed blocks. |
| 1891 | If block is uncompressed, use memcpy() instead |
| 1892 | If block is a single repeated byte, use memset() instead ) |
| 1893 | |
| 1894 | The first step is to obtain the normalized frequencies of symbols. |
| 1895 | This can be performed by FSE_readNCount() if it was saved using FSE_writeNCount(). |
| 1896 | 'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short. |
| 1897 | In practice, that means it's necessary to know 'maxSymbolValue' beforehand, |
| 1898 | or size the table to handle worst case situations (typically 256). |
| 1899 | FSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'. |
| 1900 | The result of FSE_readNCount() is the number of bytes read from 'rBuffer'. |
| 1901 | Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that. |
| 1902 | If there is an error, the function will return an error code, which can be tested using FSE_isError(). |
| 1903 | |
| 1904 | The next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'. |
| 1905 | This is performed by the function FSE_buildDTable(). |
| 1906 | The space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). |
| 1907 | If there is an error, the function will return an error code, which can be tested using FSE_isError(). |
| 1908 | |
| 1909 | `FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable(). |
| 1910 | `cSrcSize` must be strictly correct, otherwise decompression will fail. |
| 1911 | FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`). |
| 1912 | If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small) |
| 1913 | */ |
| 1914 | |
| 1915 | #endif /* FSE_H */ |
| 1916 | |
| 1917 | |
| 1918 | #if defined(FSE_STATIC_LINKING_ONLY) && !defined(FSE_H_FSE_STATIC_LINKING_ONLY) |
| 1919 | #define FSE_H_FSE_STATIC_LINKING_ONLY |
| 1920 | /**** start inlining bitstream.h ****/ |
| 1921 | /* ****************************************************************** |
| 1922 | * bitstream |
| 1923 | * Part of FSE library |
| 1924 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 1925 | * |
| 1926 | * You can contact the author at : |
| 1927 | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 1928 | * |
| 1929 | * This source code is licensed under both the BSD-style license (found in the |
| 1930 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 1931 | * in the COPYING file in the root directory of this source tree). |
| 1932 | * You may select, at your option, one of the above-listed licenses. |
| 1933 | ****************************************************************** */ |
| 1934 | #ifndef BITSTREAM_H_MODULE |
| 1935 | #define BITSTREAM_H_MODULE |
| 1936 | |
| 1937 | /* |
| 1938 | * This API consists of small unitary functions, which must be inlined for best performance. |
| 1939 | * Since link-time-optimization is not available for all compilers, |
| 1940 | * these functions are defined into a .h to be included. |
| 1941 | */ |
| 1942 | |
| 1943 | /*-**************************************** |
| 1944 | * Dependencies |
| 1945 | ******************************************/ |
| 1946 | /**** skipping file: mem.h ****/ |
| 1947 | /**** skipping file: compiler.h ****/ |
| 1948 | /**** skipping file: debug.h ****/ |
| 1949 | /**** skipping file: error_private.h ****/ |
| 1950 | /**** start inlining bits.h ****/ |
| 1951 | /* |
| 1952 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 1953 | * All rights reserved. |
| 1954 | * |
| 1955 | * This source code is licensed under both the BSD-style license (found in the |
| 1956 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 1957 | * in the COPYING file in the root directory of this source tree). |
| 1958 | * You may select, at your option, one of the above-listed licenses. |
| 1959 | */ |
| 1960 | |
| 1961 | #ifndef ZSTD_BITS_H |
| 1962 | #define ZSTD_BITS_H |
| 1963 | |
| 1964 | /**** skipping file: mem.h ****/ |
| 1965 | |
| 1966 | MEM_STATIC unsigned ZSTD_countTrailingZeros32_fallback(U32 val) |
| 1967 | { |
| 1968 | assert(val != 0); |
| 1969 | { |
| 1970 | static const U32 DeBruijnBytePos[32] = {0, 1, 28, 2, 29, 14, 24, 3, |
| 1971 | 30, 22, 20, 15, 25, 17, 4, 8, |
| 1972 | 31, 27, 13, 23, 21, 19, 16, 7, |
| 1973 | 26, 12, 18, 6, 11, 5, 10, 9}; |
| 1974 | return DeBruijnBytePos[((U32) ((val & -(S32) val) * 0x077CB531U)) >> 27]; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | MEM_STATIC unsigned ZSTD_countTrailingZeros32(U32 val) |
| 1979 | { |
| 1980 | assert(val != 0); |
| 1981 | #if defined(_MSC_VER) |
| 1982 | # if STATIC_BMI2 |
| 1983 | return (unsigned)_tzcnt_u32(val); |
| 1984 | # else |
| 1985 | if (val != 0) { |
| 1986 | unsigned long r; |
| 1987 | _BitScanForward(&r, val); |
| 1988 | return (unsigned)r; |
| 1989 | } else { |
| 1990 | __assume(0); /* Should not reach this code path */ |
| 1991 | } |
| 1992 | # endif |
| 1993 | #elif defined(__GNUC__) && (__GNUC__ >= 4) |
| 1994 | return (unsigned)__builtin_ctz(val); |
| 1995 | #elif defined(__ICCARM__) |
| 1996 | return (unsigned)__builtin_ctz(val); |
| 1997 | #else |
| 1998 | return ZSTD_countTrailingZeros32_fallback(val); |
| 1999 | #endif |
| 2000 | } |
| 2001 | |
| 2002 | MEM_STATIC unsigned ZSTD_countLeadingZeros32_fallback(U32 val) |
| 2003 | { |
| 2004 | assert(val != 0); |
| 2005 | { |
| 2006 | static const U32 DeBruijnClz[32] = {0, 9, 1, 10, 13, 21, 2, 29, |
| 2007 | 11, 14, 16, 18, 22, 25, 3, 30, |
| 2008 | 8, 12, 20, 28, 15, 17, 24, 7, |
| 2009 | 19, 27, 23, 6, 26, 5, 4, 31}; |
| 2010 | val |= val >> 1; |
| 2011 | val |= val >> 2; |
| 2012 | val |= val >> 4; |
| 2013 | val |= val >> 8; |
| 2014 | val |= val >> 16; |
| 2015 | return 31 - DeBruijnClz[(val * 0x07C4ACDDU) >> 27]; |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | MEM_STATIC unsigned ZSTD_countLeadingZeros32(U32 val) |
| 2020 | { |
| 2021 | assert(val != 0); |
| 2022 | #if defined(_MSC_VER) |
| 2023 | # if STATIC_BMI2 |
| 2024 | return (unsigned)_lzcnt_u32(val); |
| 2025 | # else |
| 2026 | if (val != 0) { |
| 2027 | unsigned long r; |
| 2028 | _BitScanReverse(&r, val); |
| 2029 | return (unsigned)(31 - r); |
| 2030 | } else { |
| 2031 | __assume(0); /* Should not reach this code path */ |
| 2032 | } |
| 2033 | # endif |
| 2034 | #elif defined(__GNUC__) && (__GNUC__ >= 4) |
| 2035 | return (unsigned)__builtin_clz(val); |
| 2036 | #elif defined(__ICCARM__) |
| 2037 | return (unsigned)__builtin_clz(val); |
| 2038 | #else |
| 2039 | return ZSTD_countLeadingZeros32_fallback(val); |
| 2040 | #endif |
| 2041 | } |
| 2042 | |
| 2043 | MEM_STATIC unsigned ZSTD_countTrailingZeros64(U64 val) |
| 2044 | { |
| 2045 | assert(val != 0); |
| 2046 | #if defined(_MSC_VER) && defined(_WIN64) |
| 2047 | # if STATIC_BMI2 |
| 2048 | return (unsigned)_tzcnt_u64(val); |
| 2049 | # else |
| 2050 | if (val != 0) { |
| 2051 | unsigned long r; |
| 2052 | _BitScanForward64(&r, val); |
| 2053 | return (unsigned)r; |
| 2054 | } else { |
| 2055 | __assume(0); /* Should not reach this code path */ |
| 2056 | } |
| 2057 | # endif |
| 2058 | #elif defined(__GNUC__) && (__GNUC__ >= 4) && defined(__LP64__) |
| 2059 | return (unsigned)__builtin_ctzll(val); |
| 2060 | #elif defined(__ICCARM__) |
| 2061 | return (unsigned)__builtin_ctzll(val); |
| 2062 | #else |
| 2063 | { |
| 2064 | U32 mostSignificantWord = (U32)(val >> 32); |
| 2065 | U32 leastSignificantWord = (U32)val; |
| 2066 | if (leastSignificantWord == 0) { |
| 2067 | return 32 + ZSTD_countTrailingZeros32(mostSignificantWord); |
| 2068 | } else { |
| 2069 | return ZSTD_countTrailingZeros32(leastSignificantWord); |
| 2070 | } |
| 2071 | } |
| 2072 | #endif |
| 2073 | } |
| 2074 | |
| 2075 | MEM_STATIC unsigned ZSTD_countLeadingZeros64(U64 val) |
| 2076 | { |
| 2077 | assert(val != 0); |
| 2078 | #if defined(_MSC_VER) && defined(_WIN64) |
| 2079 | # if STATIC_BMI2 |
| 2080 | return (unsigned)_lzcnt_u64(val); |
| 2081 | # else |
| 2082 | if (val != 0) { |
| 2083 | unsigned long r; |
| 2084 | _BitScanReverse64(&r, val); |
| 2085 | return (unsigned)(63 - r); |
| 2086 | } else { |
| 2087 | __assume(0); /* Should not reach this code path */ |
| 2088 | } |
| 2089 | # endif |
| 2090 | #elif defined(__GNUC__) && (__GNUC__ >= 4) |
| 2091 | return (unsigned)(__builtin_clzll(val)); |
| 2092 | #elif defined(__ICCARM__) |
| 2093 | return (unsigned)(__builtin_clzll(val)); |
| 2094 | #else |
| 2095 | { |
| 2096 | U32 mostSignificantWord = (U32)(val >> 32); |
| 2097 | U32 leastSignificantWord = (U32)val; |
| 2098 | if (mostSignificantWord == 0) { |
| 2099 | return 32 + ZSTD_countLeadingZeros32(leastSignificantWord); |
| 2100 | } else { |
| 2101 | return ZSTD_countLeadingZeros32(mostSignificantWord); |
| 2102 | } |
| 2103 | } |
| 2104 | #endif |
| 2105 | } |
| 2106 | |
| 2107 | MEM_STATIC unsigned ZSTD_NbCommonBytes(size_t val) |
| 2108 | { |
| 2109 | if (MEM_isLittleEndian()) { |
| 2110 | if (MEM_64bits()) { |
| 2111 | return ZSTD_countTrailingZeros64((U64)val) >> 3; |
| 2112 | } else { |
| 2113 | return ZSTD_countTrailingZeros32((U32)val) >> 3; |
| 2114 | } |
| 2115 | } else { /* Big Endian CPU */ |
| 2116 | if (MEM_64bits()) { |
| 2117 | return ZSTD_countLeadingZeros64((U64)val) >> 3; |
| 2118 | } else { |
| 2119 | return ZSTD_countLeadingZeros32((U32)val) >> 3; |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | MEM_STATIC unsigned ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */ |
| 2125 | { |
| 2126 | assert(val != 0); |
| 2127 | return 31 - ZSTD_countLeadingZeros32(val); |
| 2128 | } |
| 2129 | |
| 2130 | /* ZSTD_rotateRight_*(): |
| 2131 | * Rotates a bitfield to the right by "count" bits. |
| 2132 | * https://en.wikipedia.org/w/index.php?title=Circular_shift&oldid=991635599#Implementing_circular_shifts |
| 2133 | */ |
| 2134 | MEM_STATIC |
| 2135 | U64 ZSTD_rotateRight_U64(U64 const value, U32 count) { |
| 2136 | assert(count < 64); |
| 2137 | count &= 0x3F; /* for fickle pattern recognition */ |
| 2138 | return (value >> count) | (U64)(value << ((0U - count) & 0x3F)); |
| 2139 | } |
| 2140 | |
| 2141 | MEM_STATIC |
| 2142 | U32 ZSTD_rotateRight_U32(U32 const value, U32 count) { |
| 2143 | assert(count < 32); |
| 2144 | count &= 0x1F; /* for fickle pattern recognition */ |
| 2145 | return (value >> count) | (U32)(value << ((0U - count) & 0x1F)); |
| 2146 | } |
| 2147 | |
| 2148 | MEM_STATIC |
| 2149 | U16 ZSTD_rotateRight_U16(U16 const value, U32 count) { |
| 2150 | assert(count < 16); |
| 2151 | count &= 0x0F; /* for fickle pattern recognition */ |
| 2152 | return (value >> count) | (U16)(value << ((0U - count) & 0x0F)); |
| 2153 | } |
| 2154 | |
| 2155 | #endif /* ZSTD_BITS_H */ |
| 2156 | /**** ended inlining bits.h ****/ |
| 2157 | |
| 2158 | /*========================================= |
| 2159 | * Target specific |
| 2160 | =========================================*/ |
| 2161 | #ifndef ZSTD_NO_INTRINSICS |
| 2162 | # if (defined(__BMI__) || defined(__BMI2__)) && defined(__GNUC__) |
| 2163 | # include <immintrin.h> /* support for bextr (experimental)/bzhi */ |
| 2164 | # elif defined(__ICCARM__) |
| 2165 | # include <intrinsics.h> |
| 2166 | # endif |
| 2167 | #endif |
| 2168 | |
| 2169 | #define STREAM_ACCUMULATOR_MIN_32 25 |
| 2170 | #define STREAM_ACCUMULATOR_MIN_64 57 |
| 2171 | #define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64)) |
| 2172 | |
| 2173 | |
| 2174 | /*-****************************************** |
| 2175 | * bitStream encoding API (write forward) |
| 2176 | ********************************************/ |
| 2177 | typedef size_t BitContainerType; |
| 2178 | /* bitStream can mix input from multiple sources. |
| 2179 | * A critical property of these streams is that they encode and decode in **reverse** direction. |
| 2180 | * So the first bit sequence you add will be the last to be read, like a LIFO stack. |
| 2181 | */ |
| 2182 | typedef struct { |
| 2183 | BitContainerType bitContainer; |
| 2184 | unsigned bitPos; |
| 2185 | char* startPtr; |
| 2186 | char* ptr; |
| 2187 | char* endPtr; |
| 2188 | } BIT_CStream_t; |
| 2189 | |
| 2190 | MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity); |
| 2191 | MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, BitContainerType value, unsigned nbBits); |
| 2192 | MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC); |
| 2193 | MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); |
| 2194 | |
| 2195 | /* Start with initCStream, providing the size of buffer to write into. |
| 2196 | * bitStream will never write outside of this buffer. |
| 2197 | * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code. |
| 2198 | * |
| 2199 | * bits are first added to a local register. |
| 2200 | * Local register is BitContainerType, 64-bits on 64-bits systems, or 32-bits on 32-bits systems. |
| 2201 | * Writing data into memory is an explicit operation, performed by the flushBits function. |
| 2202 | * Hence keep track how many bits are potentially stored into local register to avoid register overflow. |
| 2203 | * After a flushBits, a maximum of 7 bits might still be stored into local register. |
| 2204 | * |
| 2205 | * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. |
| 2206 | * |
| 2207 | * Last operation is to close the bitStream. |
| 2208 | * The function returns the final size of CStream in bytes. |
| 2209 | * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) |
| 2210 | */ |
| 2211 | |
| 2212 | |
| 2213 | /*-******************************************** |
| 2214 | * bitStream decoding API (read backward) |
| 2215 | **********************************************/ |
| 2216 | typedef struct { |
| 2217 | BitContainerType bitContainer; |
| 2218 | unsigned bitsConsumed; |
| 2219 | const char* ptr; |
| 2220 | const char* start; |
| 2221 | const char* limitPtr; |
| 2222 | } BIT_DStream_t; |
| 2223 | |
| 2224 | typedef enum { BIT_DStream_unfinished = 0, /* fully refilled */ |
| 2225 | BIT_DStream_endOfBuffer = 1, /* still some bits left in bitstream */ |
| 2226 | BIT_DStream_completed = 2, /* bitstream entirely consumed, bit-exact */ |
| 2227 | BIT_DStream_overflow = 3 /* user requested more bits than present in bitstream */ |
| 2228 | } BIT_DStream_status; /* result of BIT_reloadDStream() */ |
| 2229 | |
| 2230 | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); |
| 2231 | MEM_STATIC BitContainerType BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); |
| 2232 | MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); |
| 2233 | MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); |
| 2234 | |
| 2235 | |
| 2236 | /* Start by invoking BIT_initDStream(). |
| 2237 | * A chunk of the bitStream is then stored into a local register. |
| 2238 | * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (BitContainerType). |
| 2239 | * You can then retrieve bitFields stored into the local register, **in reverse order**. |
| 2240 | * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. |
| 2241 | * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished. |
| 2242 | * Otherwise, it can be less than that, so proceed accordingly. |
| 2243 | * Checking if DStream has reached its end can be performed with BIT_endOfDStream(). |
| 2244 | */ |
| 2245 | |
| 2246 | |
| 2247 | /*-**************************************** |
| 2248 | * unsafe API |
| 2249 | ******************************************/ |
| 2250 | MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, BitContainerType value, unsigned nbBits); |
| 2251 | /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ |
| 2252 | |
| 2253 | MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC); |
| 2254 | /* unsafe version; does not check buffer overflow */ |
| 2255 | |
| 2256 | MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); |
| 2257 | /* faster, but works only if nbBits >= 1 */ |
| 2258 | |
| 2259 | /*===== Local Constants =====*/ |
| 2260 | static const unsigned BIT_mask[] = { |
| 2261 | 0, 1, 3, 7, 0xF, 0x1F, |
| 2262 | 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, |
| 2263 | 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, |
| 2264 | 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, |
| 2265 | 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF, |
| 2266 | 0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */ |
| 2267 | #define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0])) |
| 2268 | |
| 2269 | /*-************************************************************** |
| 2270 | * bitStream encoding |
| 2271 | ****************************************************************/ |
| 2272 | /*! BIT_initCStream() : |
| 2273 | * `dstCapacity` must be > sizeof(size_t) |
| 2274 | * @return : 0 if success, |
| 2275 | * otherwise an error code (can be tested using ERR_isError()) */ |
| 2276 | MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, |
| 2277 | void* startPtr, size_t dstCapacity) |
| 2278 | { |
| 2279 | bitC->bitContainer = 0; |
| 2280 | bitC->bitPos = 0; |
| 2281 | bitC->startPtr = (char*)startPtr; |
| 2282 | bitC->ptr = bitC->startPtr; |
| 2283 | bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer); |
| 2284 | if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall); |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
| 2288 | FORCE_INLINE_TEMPLATE BitContainerType BIT_getLowerBits(BitContainerType bitContainer, U32 const nbBits) |
| 2289 | { |
| 2290 | #if STATIC_BMI2 && !defined(ZSTD_NO_INTRINSICS) |
| 2291 | # if (defined(__x86_64__) || defined(_M_X64)) && !defined(__ILP32__) |
| 2292 | return _bzhi_u64(bitContainer, nbBits); |
| 2293 | # else |
| 2294 | DEBUG_STATIC_ASSERT(sizeof(bitContainer) == sizeof(U32)); |
| 2295 | return _bzhi_u32(bitContainer, nbBits); |
| 2296 | # endif |
| 2297 | #else |
| 2298 | assert(nbBits < BIT_MASK_SIZE); |
| 2299 | return bitContainer & BIT_mask[nbBits]; |
| 2300 | #endif |
| 2301 | } |
| 2302 | |
| 2303 | /*! BIT_addBits() : |
| 2304 | * can add up to 31 bits into `bitC`. |
| 2305 | * Note : does not check for register overflow ! */ |
| 2306 | MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, |
| 2307 | BitContainerType value, unsigned nbBits) |
| 2308 | { |
| 2309 | DEBUG_STATIC_ASSERT(BIT_MASK_SIZE == 32); |
| 2310 | assert(nbBits < BIT_MASK_SIZE); |
| 2311 | assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8); |
| 2312 | bitC->bitContainer |= BIT_getLowerBits(value, nbBits) << bitC->bitPos; |
| 2313 | bitC->bitPos += nbBits; |
| 2314 | } |
| 2315 | |
| 2316 | /*! BIT_addBitsFast() : |
| 2317 | * works only if `value` is _clean_, |
| 2318 | * meaning all high bits above nbBits are 0 */ |
| 2319 | MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, |
| 2320 | BitContainerType value, unsigned nbBits) |
| 2321 | { |
| 2322 | assert((value>>nbBits) == 0); |
| 2323 | assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8); |
| 2324 | bitC->bitContainer |= value << bitC->bitPos; |
| 2325 | bitC->bitPos += nbBits; |
| 2326 | } |
| 2327 | |
| 2328 | /*! BIT_flushBitsFast() : |
| 2329 | * assumption : bitContainer has not overflowed |
| 2330 | * unsafe version; does not check buffer overflow */ |
| 2331 | MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC) |
| 2332 | { |
| 2333 | size_t const nbBytes = bitC->bitPos >> 3; |
| 2334 | assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8); |
| 2335 | assert(bitC->ptr <= bitC->endPtr); |
| 2336 | MEM_writeLEST(bitC->ptr, bitC->bitContainer); |
| 2337 | bitC->ptr += nbBytes; |
| 2338 | bitC->bitPos &= 7; |
| 2339 | bitC->bitContainer >>= nbBytes*8; |
| 2340 | } |
| 2341 | |
| 2342 | /*! BIT_flushBits() : |
| 2343 | * assumption : bitContainer has not overflowed |
| 2344 | * safe version; check for buffer overflow, and prevents it. |
| 2345 | * note : does not signal buffer overflow. |
| 2346 | * overflow will be revealed later on using BIT_closeCStream() */ |
| 2347 | MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC) |
| 2348 | { |
| 2349 | size_t const nbBytes = bitC->bitPos >> 3; |
| 2350 | assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8); |
| 2351 | assert(bitC->ptr <= bitC->endPtr); |
| 2352 | MEM_writeLEST(bitC->ptr, bitC->bitContainer); |
| 2353 | bitC->ptr += nbBytes; |
| 2354 | if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr; |
| 2355 | bitC->bitPos &= 7; |
| 2356 | bitC->bitContainer >>= nbBytes*8; |
| 2357 | } |
| 2358 | |
| 2359 | /*! BIT_closeCStream() : |
| 2360 | * @return : size of CStream, in bytes, |
| 2361 | * or 0 if it could not fit into dstBuffer */ |
| 2362 | MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) |
| 2363 | { |
| 2364 | BIT_addBitsFast(bitC, 1, 1); /* endMark */ |
| 2365 | BIT_flushBits(bitC); |
| 2366 | if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */ |
| 2367 | return (size_t)(bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); |
| 2368 | } |
| 2369 | |
| 2370 | |
| 2371 | /*-******************************************************** |
| 2372 | * bitStream decoding |
| 2373 | **********************************************************/ |
| 2374 | /*! BIT_initDStream() : |
| 2375 | * Initialize a BIT_DStream_t. |
| 2376 | * `bitD` : a pointer to an already allocated BIT_DStream_t structure. |
| 2377 | * `srcSize` must be the *exact* size of the bitStream, in bytes. |
| 2378 | * @return : size of stream (== srcSize), or an errorCode if a problem is detected |
| 2379 | */ |
| 2380 | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) |
| 2381 | { |
| 2382 | if (srcSize < 1) { ZSTD_memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } |
| 2383 | |
| 2384 | bitD->start = (const char*)srcBuffer; |
| 2385 | bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer); |
| 2386 | |
| 2387 | if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ |
| 2388 | bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer); |
| 2389 | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
| 2390 | { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; |
| 2391 | bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ |
| 2392 | if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ } |
| 2393 | } else { |
| 2394 | bitD->ptr = bitD->start; |
| 2395 | bitD->bitContainer = *(const BYTE*)(bitD->start); |
| 2396 | switch(srcSize) |
| 2397 | { |
| 2398 | case 7: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16); |
| 2399 | ZSTD_FALLTHROUGH; |
| 2400 | |
| 2401 | case 6: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24); |
| 2402 | ZSTD_FALLTHROUGH; |
| 2403 | |
| 2404 | case 5: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32); |
| 2405 | ZSTD_FALLTHROUGH; |
| 2406 | |
| 2407 | case 4: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[3]) << 24; |
| 2408 | ZSTD_FALLTHROUGH; |
| 2409 | |
| 2410 | case 3: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[2]) << 16; |
| 2411 | ZSTD_FALLTHROUGH; |
| 2412 | |
| 2413 | case 2: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[1]) << 8; |
| 2414 | ZSTD_FALLTHROUGH; |
| 2415 | |
| 2416 | default: break; |
| 2417 | } |
| 2418 | { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; |
| 2419 | bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0; |
| 2420 | if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */ |
| 2421 | } |
| 2422 | bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8; |
| 2423 | } |
| 2424 | |
| 2425 | return srcSize; |
| 2426 | } |
| 2427 | |
| 2428 | FORCE_INLINE_TEMPLATE BitContainerType BIT_getUpperBits(BitContainerType bitContainer, U32 const start) |
| 2429 | { |
| 2430 | return bitContainer >> start; |
| 2431 | } |
| 2432 | |
| 2433 | FORCE_INLINE_TEMPLATE BitContainerType BIT_getMiddleBits(BitContainerType bitContainer, U32 const start, U32 const nbBits) |
| 2434 | { |
| 2435 | U32 const regMask = sizeof(bitContainer)*8 - 1; |
| 2436 | /* if start > regMask, bitstream is corrupted, and result is undefined */ |
| 2437 | assert(nbBits < BIT_MASK_SIZE); |
| 2438 | /* x86 transform & ((1 << nbBits) - 1) to bzhi instruction, it is better |
| 2439 | * than accessing memory. When bmi2 instruction is not present, we consider |
| 2440 | * such cpus old (pre-Haswell, 2013) and their performance is not of that |
| 2441 | * importance. |
| 2442 | */ |
| 2443 | #if defined(__x86_64__) || defined(_M_X64) |
| 2444 | return (bitContainer >> (start & regMask)) & ((((U64)1) << nbBits) - 1); |
| 2445 | #else |
| 2446 | return (bitContainer >> (start & regMask)) & BIT_mask[nbBits]; |
| 2447 | #endif |
| 2448 | } |
| 2449 | |
| 2450 | /*! BIT_lookBits() : |
| 2451 | * Provides next n bits from local register. |
| 2452 | * local register is not modified. |
| 2453 | * On 32-bits, maxNbBits==24. |
| 2454 | * On 64-bits, maxNbBits==56. |
| 2455 | * @return : value extracted */ |
| 2456 | FORCE_INLINE_TEMPLATE BitContainerType BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits) |
| 2457 | { |
| 2458 | /* arbitrate between double-shift and shift+mask */ |
| 2459 | #if 1 |
| 2460 | /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8, |
| 2461 | * bitstream is likely corrupted, and result is undefined */ |
| 2462 | return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits); |
| 2463 | #else |
| 2464 | /* this code path is slower on my os-x laptop */ |
| 2465 | U32 const regMask = sizeof(bitD->bitContainer)*8 - 1; |
| 2466 | return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask); |
| 2467 | #endif |
| 2468 | } |
| 2469 | |
| 2470 | /*! BIT_lookBitsFast() : |
| 2471 | * unsafe version; only works if nbBits >= 1 */ |
| 2472 | MEM_STATIC BitContainerType BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits) |
| 2473 | { |
| 2474 | U32 const regMask = sizeof(bitD->bitContainer)*8 - 1; |
| 2475 | assert(nbBits >= 1); |
| 2476 | return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask); |
| 2477 | } |
| 2478 | |
| 2479 | FORCE_INLINE_TEMPLATE void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) |
| 2480 | { |
| 2481 | bitD->bitsConsumed += nbBits; |
| 2482 | } |
| 2483 | |
| 2484 | /*! BIT_readBits() : |
| 2485 | * Read (consume) next n bits from local register and update. |
| 2486 | * Pay attention to not read more than nbBits contained into local register. |
| 2487 | * @return : extracted value. */ |
| 2488 | FORCE_INLINE_TEMPLATE BitContainerType BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits) |
| 2489 | { |
| 2490 | BitContainerType const value = BIT_lookBits(bitD, nbBits); |
| 2491 | BIT_skipBits(bitD, nbBits); |
| 2492 | return value; |
| 2493 | } |
| 2494 | |
| 2495 | /*! BIT_readBitsFast() : |
| 2496 | * unsafe version; only works if nbBits >= 1 */ |
| 2497 | MEM_STATIC BitContainerType BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits) |
| 2498 | { |
| 2499 | BitContainerType const value = BIT_lookBitsFast(bitD, nbBits); |
| 2500 | assert(nbBits >= 1); |
| 2501 | BIT_skipBits(bitD, nbBits); |
| 2502 | return value; |
| 2503 | } |
| 2504 | |
| 2505 | /*! BIT_reloadDStream_internal() : |
| 2506 | * Simple variant of BIT_reloadDStream(), with two conditions: |
| 2507 | * 1. bitstream is valid : bitsConsumed <= sizeof(bitD->bitContainer)*8 |
| 2508 | * 2. look window is valid after shifted down : bitD->ptr >= bitD->start |
| 2509 | */ |
| 2510 | MEM_STATIC BIT_DStream_status BIT_reloadDStream_internal(BIT_DStream_t* bitD) |
| 2511 | { |
| 2512 | assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8); |
| 2513 | bitD->ptr -= bitD->bitsConsumed >> 3; |
| 2514 | assert(bitD->ptr >= bitD->start); |
| 2515 | bitD->bitsConsumed &= 7; |
| 2516 | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
| 2517 | return BIT_DStream_unfinished; |
| 2518 | } |
| 2519 | |
| 2520 | /*! BIT_reloadDStreamFast() : |
| 2521 | * Similar to BIT_reloadDStream(), but with two differences: |
| 2522 | * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold! |
| 2523 | * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this |
| 2524 | * point you must use BIT_reloadDStream() to reload. |
| 2525 | */ |
| 2526 | MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD) |
| 2527 | { |
| 2528 | if (UNLIKELY(bitD->ptr < bitD->limitPtr)) |
| 2529 | return BIT_DStream_overflow; |
| 2530 | return BIT_reloadDStream_internal(bitD); |
| 2531 | } |
| 2532 | |
| 2533 | /*! BIT_reloadDStream() : |
| 2534 | * Refill `bitD` from buffer previously set in BIT_initDStream() . |
| 2535 | * This function is safe, it guarantees it will not never beyond src buffer. |
| 2536 | * @return : status of `BIT_DStream_t` internal register. |
| 2537 | * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */ |
| 2538 | FORCE_INLINE_TEMPLATE BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) |
| 2539 | { |
| 2540 | /* note : once in overflow mode, a bitstream remains in this mode until it's reset */ |
| 2541 | if (UNLIKELY(bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8))) { |
| 2542 | static const BitContainerType zeroFilled = 0; |
| 2543 | bitD->ptr = (const char*)&zeroFilled; /* aliasing is allowed for char */ |
| 2544 | /* overflow detected, erroneous scenario or end of stream: no update */ |
| 2545 | return BIT_DStream_overflow; |
| 2546 | } |
| 2547 | |
| 2548 | assert(bitD->ptr >= bitD->start); |
| 2549 | |
| 2550 | if (bitD->ptr >= bitD->limitPtr) { |
| 2551 | return BIT_reloadDStream_internal(bitD); |
| 2552 | } |
| 2553 | if (bitD->ptr == bitD->start) { |
| 2554 | /* reached end of bitStream => no update */ |
| 2555 | if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; |
| 2556 | return BIT_DStream_completed; |
| 2557 | } |
| 2558 | /* start < ptr < limitPtr => cautious update */ |
| 2559 | { U32 nbBytes = bitD->bitsConsumed >> 3; |
| 2560 | BIT_DStream_status result = BIT_DStream_unfinished; |
| 2561 | if (bitD->ptr - nbBytes < bitD->start) { |
| 2562 | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ |
| 2563 | result = BIT_DStream_endOfBuffer; |
| 2564 | } |
| 2565 | bitD->ptr -= nbBytes; |
| 2566 | bitD->bitsConsumed -= nbBytes*8; |
| 2567 | bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */ |
| 2568 | return result; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | /*! BIT_endOfDStream() : |
| 2573 | * @return : 1 if DStream has _exactly_ reached its end (all bits consumed). |
| 2574 | */ |
| 2575 | MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) |
| 2576 | { |
| 2577 | return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8)); |
| 2578 | } |
| 2579 | |
| 2580 | #endif /* BITSTREAM_H_MODULE */ |
| 2581 | /**** ended inlining bitstream.h ****/ |
| 2582 | |
| 2583 | /* ***************************************** |
| 2584 | * Static allocation |
| 2585 | *******************************************/ |
| 2586 | /* FSE buffer bounds */ |
| 2587 | #define FSE_NCOUNTBOUND 512 |
| 2588 | #define FSE_BLOCKBOUND(size) ((size) + ((size)>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */) |
| 2589 | #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ |
| 2590 | |
| 2591 | /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */ |
| 2592 | #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<((maxTableLog)-1)) + (((maxSymbolValue)+1)*2)) |
| 2593 | #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<(maxTableLog))) |
| 2594 | |
| 2595 | /* or use the size to malloc() space directly. Pay attention to alignment restrictions though */ |
| 2596 | #define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable)) |
| 2597 | #define FSE_DTABLE_SIZE(maxTableLog) (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable)) |
| 2598 | |
| 2599 | |
| 2600 | /* ***************************************** |
| 2601 | * FSE advanced API |
| 2602 | ***************************************** */ |
| 2603 | |
| 2604 | unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus); |
| 2605 | /**< same as FSE_optimalTableLog(), which used `minus==2` */ |
| 2606 | |
| 2607 | size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue); |
| 2608 | /**< build a fake FSE_CTable, designed to compress always the same symbolValue */ |
| 2609 | |
| 2610 | /* FSE_buildCTable_wksp() : |
| 2611 | * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). |
| 2612 | * `wkspSize` must be >= `FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog)` of `unsigned`. |
| 2613 | * See FSE_buildCTable_wksp() for breakdown of workspace usage. |
| 2614 | */ |
| 2615 | #define FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog) (((maxSymbolValue + 2) + (1ull << (tableLog)))/2 + sizeof(U64)/sizeof(U32) /* additional 8 bytes for potential table overwrite */) |
| 2616 | #define FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) (sizeof(unsigned) * FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog)) |
| 2617 | size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); |
| 2618 | |
| 2619 | #define FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) (sizeof(short) * (maxSymbolValue + 1) + (1ULL << maxTableLog) + 8) |
| 2620 | #define FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ((FSE_BUILD_DTABLE_WKSP_SIZE(maxTableLog, maxSymbolValue) + sizeof(unsigned) - 1) / sizeof(unsigned)) |
| 2621 | FSE_PUBLIC_API size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); |
| 2622 | /**< Same as FSE_buildDTable(), using an externally allocated `workspace` produced with `FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxSymbolValue)` */ |
| 2623 | |
| 2624 | #define FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) (FSE_DTABLE_SIZE_U32(maxTableLog) + 1 + FSE_BUILD_DTABLE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) + (FSE_MAX_SYMBOL_VALUE + 1) / 2 + 1) |
| 2625 | #define FSE_DECOMPRESS_WKSP_SIZE(maxTableLog, maxSymbolValue) (FSE_DECOMPRESS_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(unsigned)) |
| 2626 | size_t FSE_decompress_wksp_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize, int bmi2); |
| 2627 | /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DECOMPRESS_WKSP_SIZE_U32(maxLog, maxSymbolValue)`. |
| 2628 | * Set bmi2 to 1 if your CPU supports BMI2 or 0 if it doesn't */ |
| 2629 | |
| 2630 | typedef enum { |
| 2631 | FSE_repeat_none, /**< Cannot use the previous table */ |
| 2632 | FSE_repeat_check, /**< Can use the previous table but it must be checked */ |
| 2633 | FSE_repeat_valid /**< Can use the previous table and it is assumed to be valid */ |
| 2634 | } FSE_repeat; |
| 2635 | |
| 2636 | /* ***************************************** |
| 2637 | * FSE symbol compression API |
| 2638 | *******************************************/ |
| 2639 | /*! |
| 2640 | This API consists of small unitary functions, which highly benefit from being inlined. |
| 2641 | Hence their body are included in next section. |
| 2642 | */ |
| 2643 | typedef struct { |
| 2644 | ptrdiff_t value; |
| 2645 | const void* stateTable; |
| 2646 | const void* symbolTT; |
| 2647 | unsigned stateLog; |
| 2648 | } FSE_CState_t; |
| 2649 | |
| 2650 | static void FSE_initCState(FSE_CState_t* CStatePtr, const FSE_CTable* ct); |
| 2651 | |
| 2652 | static void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* CStatePtr, unsigned symbol); |
| 2653 | |
| 2654 | static void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* CStatePtr); |
| 2655 | |
| 2656 | /**< |
| 2657 | These functions are inner components of FSE_compress_usingCTable(). |
| 2658 | They allow the creation of custom streams, mixing multiple tables and bit sources. |
| 2659 | |
| 2660 | A key property to keep in mind is that encoding and decoding are done **in reverse direction**. |
| 2661 | So the first symbol you will encode is the last you will decode, like a LIFO stack. |
| 2662 | |
| 2663 | You will need a few variables to track your CStream. They are : |
| 2664 | |
| 2665 | FSE_CTable ct; // Provided by FSE_buildCTable() |
| 2666 | BIT_CStream_t bitStream; // bitStream tracking structure |
| 2667 | FSE_CState_t state; // State tracking structure (can have several) |
| 2668 | |
| 2669 | |
| 2670 | The first thing to do is to init bitStream and state. |
| 2671 | size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize); |
| 2672 | FSE_initCState(&state, ct); |
| 2673 | |
| 2674 | Note that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError(); |
| 2675 | You can then encode your input data, byte after byte. |
| 2676 | FSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time. |
| 2677 | Remember decoding will be done in reverse direction. |
| 2678 | FSE_encodeByte(&bitStream, &state, symbol); |
| 2679 | |
| 2680 | At any time, you can also add any bit sequence. |
| 2681 | Note : maximum allowed nbBits is 25, for compatibility with 32-bits decoders |
| 2682 | BIT_addBits(&bitStream, bitField, nbBits); |
| 2683 | |
| 2684 | The above methods don't commit data to memory, they just store it into local register, for speed. |
| 2685 | Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). |
| 2686 | Writing data to memory is a manual operation, performed by the flushBits function. |
| 2687 | BIT_flushBits(&bitStream); |
| 2688 | |
| 2689 | Your last FSE encoding operation shall be to flush your last state value(s). |
| 2690 | FSE_flushState(&bitStream, &state); |
| 2691 | |
| 2692 | Finally, you must close the bitStream. |
| 2693 | The function returns the size of CStream in bytes. |
| 2694 | If data couldn't fit into dstBuffer, it will return a 0 ( == not compressible) |
| 2695 | If there is an error, it returns an errorCode (which can be tested using FSE_isError()). |
| 2696 | size_t size = BIT_closeCStream(&bitStream); |
| 2697 | */ |
| 2698 | |
| 2699 | |
| 2700 | /* ***************************************** |
| 2701 | * FSE symbol decompression API |
| 2702 | *******************************************/ |
| 2703 | typedef struct { |
| 2704 | size_t state; |
| 2705 | const void* table; /* precise table may vary, depending on U16 */ |
| 2706 | } FSE_DState_t; |
| 2707 | |
| 2708 | |
| 2709 | static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt); |
| 2710 | |
| 2711 | static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); |
| 2712 | |
| 2713 | static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr); |
| 2714 | |
| 2715 | /**< |
| 2716 | Let's now decompose FSE_decompress_usingDTable() into its unitary components. |
| 2717 | You will decode FSE-encoded symbols from the bitStream, |
| 2718 | and also any other bitFields you put in, **in reverse order**. |
| 2719 | |
| 2720 | You will need a few variables to track your bitStream. They are : |
| 2721 | |
| 2722 | BIT_DStream_t DStream; // Stream context |
| 2723 | FSE_DState_t DState; // State context. Multiple ones are possible |
| 2724 | FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable() |
| 2725 | |
| 2726 | The first thing to do is to init the bitStream. |
| 2727 | errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize); |
| 2728 | |
| 2729 | You should then retrieve your initial state(s) |
| 2730 | (in reverse flushing order if you have several ones) : |
| 2731 | errorCode = FSE_initDState(&DState, &DStream, DTablePtr); |
| 2732 | |
| 2733 | You can then decode your data, symbol after symbol. |
| 2734 | For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'. |
| 2735 | Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out). |
| 2736 | unsigned char symbol = FSE_decodeSymbol(&DState, &DStream); |
| 2737 | |
| 2738 | You can retrieve any bitfield you eventually stored into the bitStream (in reverse order) |
| 2739 | Note : maximum allowed nbBits is 25, for 32-bits compatibility |
| 2740 | size_t bitField = BIT_readBits(&DStream, nbBits); |
| 2741 | |
| 2742 | All above operations only read from local register (which size depends on size_t). |
| 2743 | Refueling the register from memory is manually performed by the reload method. |
| 2744 | endSignal = FSE_reloadDStream(&DStream); |
| 2745 | |
| 2746 | BIT_reloadDStream() result tells if there is still some more data to read from DStream. |
| 2747 | BIT_DStream_unfinished : there is still some data left into the DStream. |
| 2748 | BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled. |
| 2749 | BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed. |
| 2750 | BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted. |
| 2751 | |
| 2752 | When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop, |
| 2753 | to properly detect the exact end of stream. |
| 2754 | After each decoded symbol, check if DStream is fully consumed using this simple test : |
| 2755 | BIT_reloadDStream(&DStream) >= BIT_DStream_completed |
| 2756 | |
| 2757 | When it's done, verify decompression is fully completed, by checking both DStream and the relevant states. |
| 2758 | Checking if DStream has reached its end is performed by : |
| 2759 | BIT_endOfDStream(&DStream); |
| 2760 | Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible. |
| 2761 | FSE_endOfDState(&DState); |
| 2762 | */ |
| 2763 | |
| 2764 | |
| 2765 | /* ***************************************** |
| 2766 | * FSE unsafe API |
| 2767 | *******************************************/ |
| 2768 | static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); |
| 2769 | /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */ |
| 2770 | |
| 2771 | |
| 2772 | /* ***************************************** |
| 2773 | * Implementation of inlined functions |
| 2774 | *******************************************/ |
| 2775 | typedef struct { |
| 2776 | int deltaFindState; |
| 2777 | U32 deltaNbBits; |
| 2778 | } FSE_symbolCompressionTransform; /* total 8 bytes */ |
| 2779 | |
| 2780 | MEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct) |
| 2781 | { |
| 2782 | const void* ptr = ct; |
| 2783 | const U16* u16ptr = (const U16*) ptr; |
| 2784 | const U32 tableLog = MEM_read16(ptr); |
| 2785 | statePtr->value = (ptrdiff_t)1<<tableLog; |
| 2786 | statePtr->stateTable = u16ptr+2; |
| 2787 | statePtr->symbolTT = ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1); |
| 2788 | statePtr->stateLog = tableLog; |
| 2789 | } |
| 2790 | |
| 2791 | |
| 2792 | /*! FSE_initCState2() : |
| 2793 | * Same as FSE_initCState(), but the first symbol to include (which will be the last to be read) |
| 2794 | * uses the smallest state value possible, saving the cost of this symbol */ |
| 2795 | MEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U32 symbol) |
| 2796 | { |
| 2797 | FSE_initCState(statePtr, ct); |
| 2798 | { const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; |
| 2799 | const U16* stateTable = (const U16*)(statePtr->stateTable); |
| 2800 | U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1<<15)) >> 16); |
| 2801 | statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits; |
| 2802 | statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, unsigned symbol) |
| 2807 | { |
| 2808 | FSE_symbolCompressionTransform const symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; |
| 2809 | const U16* const stateTable = (const U16*)(statePtr->stateTable); |
| 2810 | U32 const nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16); |
| 2811 | BIT_addBits(bitC, (BitContainerType)statePtr->value, nbBitsOut); |
| 2812 | statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; |
| 2813 | } |
| 2814 | |
| 2815 | MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePtr) |
| 2816 | { |
| 2817 | BIT_addBits(bitC, (BitContainerType)statePtr->value, statePtr->stateLog); |
| 2818 | BIT_flushBits(bitC); |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | /* FSE_getMaxNbBits() : |
| 2823 | * Approximate maximum cost of a symbol, in bits. |
| 2824 | * Fractional get rounded up (i.e. a symbol with a normalized frequency of 3 gives the same result as a frequency of 2) |
| 2825 | * note 1 : assume symbolValue is valid (<= maxSymbolValue) |
| 2826 | * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */ |
| 2827 | MEM_STATIC U32 FSE_getMaxNbBits(const void* symbolTTPtr, U32 symbolValue) |
| 2828 | { |
| 2829 | const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr; |
| 2830 | return (symbolTT[symbolValue].deltaNbBits + ((1<<16)-1)) >> 16; |
| 2831 | } |
| 2832 | |
| 2833 | /* FSE_bitCost() : |
| 2834 | * Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional bits) |
| 2835 | * note 1 : assume symbolValue is valid (<= maxSymbolValue) |
| 2836 | * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */ |
| 2837 | MEM_STATIC U32 FSE_bitCost(const void* symbolTTPtr, U32 tableLog, U32 symbolValue, U32 accuracyLog) |
| 2838 | { |
| 2839 | const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr; |
| 2840 | U32 const minNbBits = symbolTT[symbolValue].deltaNbBits >> 16; |
| 2841 | U32 const threshold = (minNbBits+1) << 16; |
| 2842 | assert(tableLog < 16); |
| 2843 | assert(accuracyLog < 31-tableLog); /* ensure enough room for renormalization double shift */ |
| 2844 | { U32 const tableSize = 1 << tableLog; |
| 2845 | U32 const deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize); |
| 2846 | U32 const normalizedDeltaFromThreshold = (deltaFromThreshold << accuracyLog) >> tableLog; /* linear interpolation (very approximate) */ |
| 2847 | U32 const bitMultiplier = 1 << accuracyLog; |
| 2848 | assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold); |
| 2849 | assert(normalizedDeltaFromThreshold <= bitMultiplier); |
| 2850 | return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold; |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | |
| 2855 | /* ====== Decompression ====== */ |
| 2856 | |
| 2857 | typedef struct { |
| 2858 | U16 tableLog; |
| 2859 | U16 fastMode; |
| 2860 | } FSE_DTableHeader; /* sizeof U32 */ |
| 2861 | |
| 2862 | typedef struct |
| 2863 | { |
| 2864 | unsigned short newState; |
| 2865 | unsigned char symbol; |
| 2866 | unsigned char nbBits; |
| 2867 | } FSE_decode_t; /* size == U32 */ |
| 2868 | |
| 2869 | MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt) |
| 2870 | { |
| 2871 | const void* ptr = dt; |
| 2872 | const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr; |
| 2873 | DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); |
| 2874 | BIT_reloadDStream(bitD); |
| 2875 | DStatePtr->table = dt + 1; |
| 2876 | } |
| 2877 | |
| 2878 | MEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr) |
| 2879 | { |
| 2880 | FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
| 2881 | return DInfo.symbol; |
| 2882 | } |
| 2883 | |
| 2884 | MEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) |
| 2885 | { |
| 2886 | FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
| 2887 | U32 const nbBits = DInfo.nbBits; |
| 2888 | size_t const lowBits = BIT_readBits(bitD, nbBits); |
| 2889 | DStatePtr->state = DInfo.newState + lowBits; |
| 2890 | } |
| 2891 | |
| 2892 | MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) |
| 2893 | { |
| 2894 | FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
| 2895 | U32 const nbBits = DInfo.nbBits; |
| 2896 | BYTE const symbol = DInfo.symbol; |
| 2897 | size_t const lowBits = BIT_readBits(bitD, nbBits); |
| 2898 | |
| 2899 | DStatePtr->state = DInfo.newState + lowBits; |
| 2900 | return symbol; |
| 2901 | } |
| 2902 | |
| 2903 | /*! FSE_decodeSymbolFast() : |
| 2904 | unsafe, only works if no symbol has a probability > 50% */ |
| 2905 | MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) |
| 2906 | { |
| 2907 | FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
| 2908 | U32 const nbBits = DInfo.nbBits; |
| 2909 | BYTE const symbol = DInfo.symbol; |
| 2910 | size_t const lowBits = BIT_readBitsFast(bitD, nbBits); |
| 2911 | |
| 2912 | DStatePtr->state = DInfo.newState + lowBits; |
| 2913 | return symbol; |
| 2914 | } |
| 2915 | |
| 2916 | MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) |
| 2917 | { |
| 2918 | return DStatePtr->state == 0; |
| 2919 | } |
| 2920 | |
| 2921 | |
| 2922 | |
| 2923 | #ifndef FSE_COMMONDEFS_ONLY |
| 2924 | |
| 2925 | /* ************************************************************** |
| 2926 | * Tuning parameters |
| 2927 | ****************************************************************/ |
| 2928 | /*!MEMORY_USAGE : |
| 2929 | * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) |
| 2930 | * Increasing memory usage improves compression ratio |
| 2931 | * Reduced memory usage can improve speed, due to cache effect |
| 2932 | * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ |
| 2933 | #ifndef FSE_MAX_MEMORY_USAGE |
| 2934 | # define FSE_MAX_MEMORY_USAGE 14 |
| 2935 | #endif |
| 2936 | #ifndef FSE_DEFAULT_MEMORY_USAGE |
| 2937 | # define FSE_DEFAULT_MEMORY_USAGE 13 |
| 2938 | #endif |
| 2939 | #if (FSE_DEFAULT_MEMORY_USAGE > FSE_MAX_MEMORY_USAGE) |
| 2940 | # error "FSE_DEFAULT_MEMORY_USAGE must be <= FSE_MAX_MEMORY_USAGE" |
| 2941 | #endif |
| 2942 | |
| 2943 | /*!FSE_MAX_SYMBOL_VALUE : |
| 2944 | * Maximum symbol value authorized. |
| 2945 | * Required for proper stack allocation */ |
| 2946 | #ifndef FSE_MAX_SYMBOL_VALUE |
| 2947 | # define FSE_MAX_SYMBOL_VALUE 255 |
| 2948 | #endif |
| 2949 | |
| 2950 | /* ************************************************************** |
| 2951 | * template functions type & suffix |
| 2952 | ****************************************************************/ |
| 2953 | #define FSE_FUNCTION_TYPE BYTE |
| 2954 | #define FSE_FUNCTION_EXTENSION |
| 2955 | #define FSE_DECODE_TYPE FSE_decode_t |
| 2956 | |
| 2957 | |
| 2958 | #endif /* !FSE_COMMONDEFS_ONLY */ |
| 2959 | |
| 2960 | |
| 2961 | /* *************************************************************** |
| 2962 | * Constants |
| 2963 | *****************************************************************/ |
| 2964 | #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2) |
| 2965 | #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG) |
| 2966 | #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1) |
| 2967 | #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2) |
| 2968 | #define FSE_MIN_TABLELOG 5 |
| 2969 | |
| 2970 | #define FSE_TABLELOG_ABSOLUTE_MAX 15 |
| 2971 | #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX |
| 2972 | # error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" |
| 2973 | #endif |
| 2974 | |
| 2975 | #define FSE_TABLESTEP(tableSize) (((tableSize)>>1) + ((tableSize)>>3) + 3) |
| 2976 | |
| 2977 | #endif /* FSE_STATIC_LINKING_ONLY */ |
| 2978 | /**** ended inlining fse.h ****/ |
| 2979 | /**** start inlining huf.h ****/ |
| 2980 | /* ****************************************************************** |
| 2981 | * huff0 huffman codec, |
| 2982 | * part of Finite State Entropy library |
| 2983 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2984 | * |
| 2985 | * You can contact the author at : |
| 2986 | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 2987 | * |
| 2988 | * This source code is licensed under both the BSD-style license (found in the |
| 2989 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 2990 | * in the COPYING file in the root directory of this source tree). |
| 2991 | * You may select, at your option, one of the above-listed licenses. |
| 2992 | ****************************************************************** */ |
| 2993 | |
| 2994 | #ifndef HUF_H_298734234 |
| 2995 | #define HUF_H_298734234 |
| 2996 | |
| 2997 | /* *** Dependencies *** */ |
| 2998 | /**** skipping file: zstd_deps.h ****/ |
| 2999 | /**** skipping file: mem.h ****/ |
| 3000 | #define FSE_STATIC_LINKING_ONLY |
| 3001 | /**** skipping file: fse.h ****/ |
| 3002 | |
| 3003 | /* *** Tool functions *** */ |
| 3004 | #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */ |
| 3005 | size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */ |
| 3006 | |
| 3007 | /* Error Management */ |
| 3008 | unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */ |
| 3009 | const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */ |
| 3010 | |
| 3011 | |
| 3012 | #define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */) |
| 3013 | #define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64)) |
| 3014 | |
| 3015 | /* *** Constants *** */ |
| 3016 | #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */ |
| 3017 | #define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */ |
| 3018 | #define HUF_SYMBOLVALUE_MAX 255 |
| 3019 | |
| 3020 | #define HUF_TABLELOG_ABSOLUTEMAX 12 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ |
| 3021 | #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX) |
| 3022 | # error "HUF_TABLELOG_MAX is too large !" |
| 3023 | #endif |
| 3024 | |
| 3025 | |
| 3026 | /* **************************************** |
| 3027 | * Static allocation |
| 3028 | ******************************************/ |
| 3029 | /* HUF buffer bounds */ |
| 3030 | #define HUF_CTABLEBOUND 129 |
| 3031 | #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */ |
| 3032 | #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ |
| 3033 | |
| 3034 | /* static allocation of HUF's Compression Table */ |
| 3035 | /* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */ |
| 3036 | typedef size_t HUF_CElt; /* consider it an incomplete type */ |
| 3037 | #define HUF_CTABLE_SIZE_ST(maxSymbolValue) ((maxSymbolValue)+2) /* Use tables of size_t, for proper alignment */ |
| 3038 | #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t)) |
| 3039 | #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \ |
| 3040 | HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */ |
| 3041 | |
| 3042 | /* static allocation of HUF's DTable */ |
| 3043 | typedef U32 HUF_DTable; |
| 3044 | #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog))) |
| 3045 | #define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \ |
| 3046 | HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) } |
| 3047 | #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \ |
| 3048 | HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) } |
| 3049 | |
| 3050 | |
| 3051 | /* **************************************** |
| 3052 | * Advanced decompression functions |
| 3053 | ******************************************/ |
| 3054 | |
| 3055 | /** |
| 3056 | * Huffman flags bitset. |
| 3057 | * For all flags, 0 is the default value. |
| 3058 | */ |
| 3059 | typedef enum { |
| 3060 | /** |
| 3061 | * If compiled with DYNAMIC_BMI2: Set flag only if the CPU supports BMI2 at runtime. |
| 3062 | * Otherwise: Ignored. |
| 3063 | */ |
| 3064 | HUF_flags_bmi2 = (1 << 0), |
| 3065 | /** |
| 3066 | * If set: Test possible table depths to find the one that produces the smallest header + encoded size. |
| 3067 | * If unset: Use heuristic to find the table depth. |
| 3068 | */ |
| 3069 | HUF_flags_optimalDepth = (1 << 1), |
| 3070 | /** |
| 3071 | * If set: If the previous table can encode the input, always reuse the previous table. |
| 3072 | * If unset: If the previous table can encode the input, reuse the previous table if it results in a smaller output. |
| 3073 | */ |
| 3074 | HUF_flags_preferRepeat = (1 << 2), |
| 3075 | /** |
| 3076 | * If set: Sample the input and check if the sample is uncompressible, if it is then don't attempt to compress. |
| 3077 | * If unset: Always histogram the entire input. |
| 3078 | */ |
| 3079 | HUF_flags_suspectUncompressible = (1 << 3), |
| 3080 | /** |
| 3081 | * If set: Don't use assembly implementations |
| 3082 | * If unset: Allow using assembly implementations |
| 3083 | */ |
| 3084 | HUF_flags_disableAsm = (1 << 4), |
| 3085 | /** |
| 3086 | * If set: Don't use the fast decoding loop, always use the fallback decoding loop. |
| 3087 | * If unset: Use the fast decoding loop when possible. |
| 3088 | */ |
| 3089 | HUF_flags_disableFast = (1 << 5) |
| 3090 | } HUF_flags_e; |
| 3091 | |
| 3092 | |
| 3093 | /* **************************************** |
| 3094 | * HUF detailed API |
| 3095 | * ****************************************/ |
| 3096 | #define HUF_OPTIMAL_DEPTH_THRESHOLD ZSTD_btultra |
| 3097 | |
| 3098 | /*! HUF_compress() does the following: |
| 3099 | * 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h") |
| 3100 | * 2. (optional) refine tableLog using HUF_optimalTableLog() |
| 3101 | * 3. build Huffman table from count using HUF_buildCTable() |
| 3102 | * 4. save Huffman table to memory buffer using HUF_writeCTable() |
| 3103 | * 5. encode the data stream using HUF_compress4X_usingCTable() |
| 3104 | * |
| 3105 | * The following API allows targeting specific sub-functions for advanced tasks. |
| 3106 | * For example, it's possible to compress several blocks using the same 'CTable', |
| 3107 | * or to save and regenerate 'CTable' using external methods. |
| 3108 | */ |
| 3109 | unsigned HUF_minTableLog(unsigned symbolCardinality); |
| 3110 | unsigned HUF_cardinality(const unsigned* count, unsigned maxSymbolValue); |
| 3111 | unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, void* workSpace, |
| 3112 | size_t wkspSize, HUF_CElt* table, const unsigned* count, int flags); /* table is used as scratch space for building and testing tables, not a return value */ |
| 3113 | size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize); |
| 3114 | size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags); |
| 3115 | size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue); |
| 3116 | int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue); |
| 3117 | |
| 3118 | typedef enum { |
| 3119 | HUF_repeat_none, /**< Cannot use the previous table */ |
| 3120 | HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */ |
| 3121 | HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */ |
| 3122 | } HUF_repeat; |
| 3123 | |
| 3124 | /** HUF_compress4X_repeat() : |
| 3125 | * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. |
| 3126 | * If it uses hufTable it does not modify hufTable or repeat. |
| 3127 | * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. |
| 3128 | * If preferRepeat then the old table will always be used if valid. |
| 3129 | * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */ |
| 3130 | size_t HUF_compress4X_repeat(void* dst, size_t dstSize, |
| 3131 | const void* src, size_t srcSize, |
| 3132 | unsigned maxSymbolValue, unsigned tableLog, |
| 3133 | void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */ |
| 3134 | HUF_CElt* hufTable, HUF_repeat* repeat, int flags); |
| 3135 | |
| 3136 | /** HUF_buildCTable_wksp() : |
| 3137 | * Same as HUF_buildCTable(), but using externally allocated scratch buffer. |
| 3138 | * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE. |
| 3139 | */ |
| 3140 | #define HUF_CTABLE_WORKSPACE_SIZE_U32 ((4 * (HUF_SYMBOLVALUE_MAX + 1)) + 192) |
| 3141 | #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned)) |
| 3142 | size_t HUF_buildCTable_wksp (HUF_CElt* tree, |
| 3143 | const unsigned* count, U32 maxSymbolValue, U32 maxNbBits, |
| 3144 | void* workSpace, size_t wkspSize); |
| 3145 | |
| 3146 | /*! HUF_readStats() : |
| 3147 | * Read compact Huffman tree, saved by HUF_writeCTable(). |
| 3148 | * `huffWeight` is destination buffer. |
| 3149 | * @return : size read from `src` , or an error Code . |
| 3150 | * Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */ |
| 3151 | size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, |
| 3152 | U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr, |
| 3153 | const void* src, size_t srcSize); |
| 3154 | |
| 3155 | /*! HUF_readStats_wksp() : |
| 3156 | * Same as HUF_readStats() but takes an external workspace which must be |
| 3157 | * 4-byte aligned and its size must be >= HUF_READ_STATS_WORKSPACE_SIZE. |
| 3158 | * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0. |
| 3159 | */ |
| 3160 | #define HUF_READ_STATS_WORKSPACE_SIZE_U32 FSE_DECOMPRESS_WKSP_SIZE_U32(6, HUF_TABLELOG_MAX-1) |
| 3161 | #define HUF_READ_STATS_WORKSPACE_SIZE (HUF_READ_STATS_WORKSPACE_SIZE_U32 * sizeof(unsigned)) |
| 3162 | size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, |
| 3163 | U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr, |
| 3164 | const void* src, size_t srcSize, |
| 3165 | void* workspace, size_t wkspSize, |
| 3166 | int flags); |
| 3167 | |
| 3168 | /** HUF_readCTable() : |
| 3169 | * Loading a CTable saved with HUF_writeCTable() */ |
| 3170 | size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights); |
| 3171 | |
| 3172 | /** HUF_getNbBitsFromCTable() : |
| 3173 | * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX |
| 3174 | * Note 1 : If symbolValue > HUF_readCTableHeader(symbolTable).maxSymbolValue, returns 0 |
| 3175 | * Note 2 : is not inlined, as HUF_CElt definition is private |
| 3176 | */ |
| 3177 | U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue); |
| 3178 | |
| 3179 | typedef struct { |
| 3180 | BYTE tableLog; |
| 3181 | BYTE maxSymbolValue; |
| 3182 | BYTE unused[sizeof(size_t) - 2]; |
| 3183 | } HUF_CTableHeader; |
| 3184 | |
| 3185 | /** HUF_readCTableHeader() : |
| 3186 | * @returns The header from the CTable specifying the tableLog and the maxSymbolValue. |
| 3187 | */ |
| 3188 | HUF_CTableHeader HUF_readCTableHeader(HUF_CElt const* ctable); |
| 3189 | |
| 3190 | /* |
| 3191 | * HUF_decompress() does the following: |
| 3192 | * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics |
| 3193 | * 2. build Huffman table from save, using HUF_readDTableX?() |
| 3194 | * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable() |
| 3195 | */ |
| 3196 | |
| 3197 | /** HUF_selectDecoder() : |
| 3198 | * Tells which decoder is likely to decode faster, |
| 3199 | * based on a set of pre-computed metrics. |
| 3200 | * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 . |
| 3201 | * Assumption : 0 < dstSize <= 128 KB */ |
| 3202 | U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize); |
| 3203 | |
| 3204 | /** |
| 3205 | * The minimum workspace size for the `workSpace` used in |
| 3206 | * HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp(). |
| 3207 | * |
| 3208 | * The space u |