Skip to content
0.2.0-dev Download ZIP
43 lines ·1.7 KB Raw
1/*
2 * Copyright (C) 2014, 2021, 2024-2025, D. R. Commander.
3 * Copyright (C) 2014, Olle Liljenzin.
4 * Copyright (C) 2020, Arm Limited.
5 *
6 * For conditions of distribution and use, see the accompanying README.ijg
7 * file.
8 */
9
10/*
11 * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
12 * used for bit counting rather than the lookup table. This will reduce the
13 * memory footprint by 64k, which is important for some mobile applications
14 * that create many isolated instances of libjpeg-turbo (web browsers, for
15 * instance.) This may improve performance on some mobile platforms as well.
16 * This feature is enabled by default only on Arm processors, because some x86
17 * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
18 * shown to have a significant performance impact even on the x86 chips that
19 * have a fast implementation of it. When building for Armv6, you can
20 * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
21 * flags (this defines __thumb__).
22 */
23
24/* NOTE: Both GCC and Clang define __GNUC__ */
25#if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \
26 defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC)
27#if !defined(__thumb__) || defined(__thumb2__)
28#define USE_CLZ_INTRINSIC
29#endif
30#endif
31
32#ifdef USE_CLZ_INTRINSIC
33#if defined(_MSC_VER) && !defined(__clang__)
34#define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x))
35#else
36#define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
37#endif
38#define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
39#else
40extern const unsigned char jpeg_nbits_table[65536];
41#define JPEG_NBITS(x) (jpeg_nbits_table[x])
42#define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
43#endif