|
| 1 | +/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ |
| 2 | +/* |
| 3 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This source code is licensed under both the BSD-style license (found in the |
| 7 | + * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 8 | + * in the COPYING file in the root directory of this source tree). |
| 9 | + * You may select, at your option, one of the above-listed licenses. |
| 10 | + */ |
| 11 | + |
| 12 | +#ifndef ZSTD_BITS_H |
| 13 | +#define ZSTD_BITS_H |
| 14 | + |
| 15 | +#include "mem.h" |
| 16 | + |
| 17 | +MEM_STATIC unsigned ZSTD_countTrailingZeros32_fallback(U32 val) |
| 18 | +{ |
| 19 | + assert(val != 0); |
| 20 | + { |
| 21 | + static const U32 DeBruijnBytePos[32] = {0, 1, 28, 2, 29, 14, 24, 3, |
| 22 | + 30, 22, 20, 15, 25, 17, 4, 8, |
| 23 | + 31, 27, 13, 23, 21, 19, 16, 7, |
| 24 | + 26, 12, 18, 6, 11, 5, 10, 9}; |
| 25 | + return DeBruijnBytePos[((U32) ((val & -(S32) val) * 0x077CB531U)) >> 27]; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +MEM_STATIC unsigned ZSTD_countTrailingZeros32(U32 val) |
| 30 | +{ |
| 31 | + assert(val != 0); |
| 32 | +# if (__GNUC__ >= 4) |
| 33 | + return (unsigned)__builtin_ctz(val); |
| 34 | +# else |
| 35 | + return ZSTD_countTrailingZeros32_fallback(val); |
| 36 | +# endif |
| 37 | +} |
| 38 | + |
| 39 | +MEM_STATIC unsigned ZSTD_countLeadingZeros32_fallback(U32 val) { |
| 40 | + assert(val != 0); |
| 41 | + { |
| 42 | + static const U32 DeBruijnClz[32] = {0, 9, 1, 10, 13, 21, 2, 29, |
| 43 | + 11, 14, 16, 18, 22, 25, 3, 30, |
| 44 | + 8, 12, 20, 28, 15, 17, 24, 7, |
| 45 | + 19, 27, 23, 6, 26, 5, 4, 31}; |
| 46 | + val |= val >> 1; |
| 47 | + val |= val >> 2; |
| 48 | + val |= val >> 4; |
| 49 | + val |= val >> 8; |
| 50 | + val |= val >> 16; |
| 51 | + return 31 - DeBruijnClz[(val * 0x07C4ACDDU) >> 27]; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +MEM_STATIC unsigned ZSTD_countLeadingZeros32(U32 val) |
| 56 | +{ |
| 57 | + assert(val != 0); |
| 58 | +# if (__GNUC__ >= 4) |
| 59 | + return (unsigned)__builtin_clz(val); |
| 60 | +# else |
| 61 | + return ZSTD_countLeadingZeros32_fallback(val); |
| 62 | +# endif |
| 63 | +} |
| 64 | + |
| 65 | +MEM_STATIC unsigned ZSTD_countTrailingZeros64(U64 val) |
| 66 | +{ |
| 67 | + assert(val != 0); |
| 68 | +# if (__GNUC__ >= 4) && defined(__LP64__) |
| 69 | + return (unsigned)__builtin_ctzll(val); |
| 70 | +# else |
| 71 | + { |
| 72 | + U32 mostSignificantWord = (U32)(val >> 32); |
| 73 | + U32 leastSignificantWord = (U32)val; |
| 74 | + if (leastSignificantWord == 0) { |
| 75 | + return 32 + ZSTD_countTrailingZeros32(mostSignificantWord); |
| 76 | + } else { |
| 77 | + return ZSTD_countTrailingZeros32(leastSignificantWord); |
| 78 | + } |
| 79 | + } |
| 80 | +# endif |
| 81 | +} |
| 82 | + |
| 83 | +MEM_STATIC unsigned ZSTD_countLeadingZeros64(U64 val) |
| 84 | +{ |
| 85 | + assert(val != 0); |
| 86 | +# if (__GNUC__ >= 4) |
| 87 | + return (unsigned)(__builtin_clzll(val)); |
| 88 | +# else |
| 89 | + { |
| 90 | + U32 mostSignificantWord = (U32)(val >> 32); |
| 91 | + U32 leastSignificantWord = (U32)val; |
| 92 | + if (mostSignificantWord == 0) { |
| 93 | + return 32 + ZSTD_countLeadingZeros32(leastSignificantWord); |
| 94 | + } else { |
| 95 | + return ZSTD_countLeadingZeros32(mostSignificantWord); |
| 96 | + } |
| 97 | + } |
| 98 | +# endif |
| 99 | +} |
| 100 | + |
| 101 | +MEM_STATIC unsigned ZSTD_NbCommonBytes(size_t val) |
| 102 | +{ |
| 103 | + if (MEM_isLittleEndian()) { |
| 104 | + if (MEM_64bits()) { |
| 105 | + return ZSTD_countTrailingZeros64((U64)val) >> 3; |
| 106 | + } else { |
| 107 | + return ZSTD_countTrailingZeros32((U32)val) >> 3; |
| 108 | + } |
| 109 | + } else { /* Big Endian CPU */ |
| 110 | + if (MEM_64bits()) { |
| 111 | + return ZSTD_countLeadingZeros64((U64)val) >> 3; |
| 112 | + } else { |
| 113 | + return ZSTD_countLeadingZeros32((U32)val) >> 3; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +MEM_STATIC unsigned ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */ |
| 119 | +{ |
| 120 | + assert(val != 0); |
| 121 | + return 31 - ZSTD_countLeadingZeros32(val); |
| 122 | +} |
| 123 | + |
| 124 | +/* ZSTD_rotateRight_*(): |
| 125 | + * Rotates a bitfield to the right by "count" bits. |
| 126 | + * https://en.wikipedia.org/w/index.php?title=Circular_shift&oldid=991635599#Implementing_circular_shifts |
| 127 | + */ |
| 128 | +MEM_STATIC |
| 129 | +U64 ZSTD_rotateRight_U64(U64 const value, U32 count) { |
| 130 | + assert(count < 64); |
| 131 | + count &= 0x3F; /* for fickle pattern recognition */ |
| 132 | + return (value >> count) | (U64)(value << ((0U - count) & 0x3F)); |
| 133 | +} |
| 134 | + |
| 135 | +MEM_STATIC |
| 136 | +U32 ZSTD_rotateRight_U32(U32 const value, U32 count) { |
| 137 | + assert(count < 32); |
| 138 | + count &= 0x1F; /* for fickle pattern recognition */ |
| 139 | + return (value >> count) | (U32)(value << ((0U - count) & 0x1F)); |
| 140 | +} |
| 141 | + |
| 142 | +MEM_STATIC |
| 143 | +U16 ZSTD_rotateRight_U16(U16 const value, U32 count) { |
| 144 | + assert(count < 16); |
| 145 | + count &= 0x0F; /* for fickle pattern recognition */ |
| 146 | + return (value >> count) | (U16)(value << ((0U - count) & 0x0F)); |
| 147 | +} |
| 148 | + |
| 149 | +#endif /* ZSTD_BITS_H */ |
0 commit comments