| 1 | |
| 2 | /* |
| 3 | * BZip3 - A spiritual successor to BZip2. |
| 4 | * Copyright (C) 2022-2024 Kamila Szewczyk |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU Lesser General Public License as published by the Free |
| 8 | * Software Foundation, either version 3 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public License along with |
| 17 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _COMMON_H |
| 21 | #define _COMMON_H |
| 22 | |
| 23 | #define KiB(x) ((x)*1024) |
| 24 | #define MiB(x) ((x)*1024 * 1024) |
| 25 | #define BWT_BOUND(x) (bz3_bound(x) + 128) |
| 26 | |
| 27 | #include <inttypes.h> |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | typedef uint8_t u8; |
| 31 | typedef uint16_t u16; |
| 32 | typedef uint32_t u32; |
| 33 | typedef uint64_t u64; |
| 34 | |
| 35 | typedef int8_t s8; |
| 36 | typedef int16_t s16; |
| 37 | typedef int32_t s32; |
| 38 | |
| 39 | static s32 read_neutral_s32(const u8 * data) { |
| 40 | return ((u32)data[0]) | (((u32)data[1]) << 8) | (((u32)data[2]) << 16) | (((u32)data[3]) << 24); |
| 41 | } |
| 42 | |
| 43 | static void write_neutral_s32(u8 * data, s32 value) { |
| 44 | data[0] = value & 0xFF; |
| 45 | data[1] = (value >> 8) & 0xFF; |
| 46 | data[2] = (value >> 16) & 0xFF; |
| 47 | data[3] = (value >> 24) & 0xFF; |
| 48 | } |
| 49 | |
| 50 | #if defined(__GNUC__) || defined(__clang__) |
| 51 | #define RESTRICT __restrict__ |
| 52 | #elif defined(_MSC_VER) || defined(__INTEL_COMPILER) |
| 53 | #define RESTRICT __restrict |
| 54 | #else |
| 55 | #define RESTRICT restrict |
| 56 | #warning Your compiler, configuration or platform might not be supported. |
| 57 | #endif |
| 58 | |
| 59 | #if defined(__has_builtin) |
| 60 | #if __has_builtin(__builtin_prefetch) |
| 61 | #define HAS_BUILTIN_PREFETCH |
| 62 | #endif |
| 63 | #elif defined(__GNUC__) && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 2)) || (__GNUC__ >= 4)) |
| 64 | #define HAS_BUILTIN_PREFETCH |
| 65 | #endif |
| 66 | |
| 67 | #if defined(__has_builtin) |
| 68 | #if __has_builtin(__builtin_bswap16) |
| 69 | #define HAS_BUILTIN_BSWAP16 |
| 70 | #endif |
| 71 | #elif defined(__GNUC__) && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) |
| 72 | #define HAS_BUILTIN_BSWAP16 |
| 73 | #endif |
| 74 | |
| 75 | #if defined(HAS_BUILTIN_PREFETCH) |
| 76 | #define prefetch(address) __builtin_prefetch((const void *)(address), 0, 0) |
| 77 | #define prefetchw(address) __builtin_prefetch((const void *)(address), 1, 0) |
| 78 | #elif defined(_M_IX86) || defined(_M_AMD64) || defined(__x86_64__) || defined(i386) || defined(__i386__) || \ |
| 79 | defined(__i386) |
| 80 | #include <intrin.h> |
| 81 | #define prefetch(address) _mm_prefetch((const void *)(address), _MM_HINT_NTA) |
| 82 | #define prefetchw(address) _m_prefetchw((const void *)(address)) |
| 83 | #elif defined(_M_ARM) || defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \ |
| 84 | defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) |
| 85 | #include <intrin.h> |
| 86 | #define prefetch(address) __prefetch((const void *)(address)) |
| 87 | #define prefetchw(address) __prefetchw((const void *)(address)) |
| 88 | #elif defined(_M_ARM64) || defined(__aarch64__) |
| 89 | #include <intrin.h> |
| 90 | #define prefetch(address) __prefetch2((const void *)(address), 1) |
| 91 | #define prefetchw(address) __prefetch2((const void *)(address), 17) |
| 92 | #else |
| 93 | #error Your compiler, configuration or platform is not supported. |
| 94 | #endif |
| 95 | |
| 96 | #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) |
| 97 | #if defined(_LITTLE_ENDIAN) || (defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN) || \ |
| 98 | (defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN) || \ |
| 99 | (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ |
| 100 | (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 101 | #define __LITTLE_ENDIAN__ |
| 102 | #elif defined(_BIG_ENDIAN) || (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \ |
| 103 | (defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN) || \ |
| 104 | (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \ |
| 105 | (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 106 | #define __BIG_ENDIAN__ |
| 107 | #elif defined(_WIN32) |
| 108 | #define __LITTLE_ENDIAN__ |
| 109 | #endif |
| 110 | #endif |
| 111 | |
| 112 | #if defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) |
| 113 | #if defined(HAS_BUILTIN_BSWAP16) |
| 114 | #define bswap16(x) (__builtin_bswap16(x)) |
| 115 | #elif defined(_MSC_VER) && !defined(__INTEL_COMPILER) |
| 116 | #define bswap16(x) (_byteswap_ushort(x)) |
| 117 | #else |
| 118 | #define bswap16(x) ((u16)(x >> 8) | (u16)(x << 8)) |
| 119 | #endif |
| 120 | #elif !defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__) |
| 121 | #define bswap16(x) (x) |
| 122 | #else |
| 123 | #error Your compiler, configuration or platform is not supported. |
| 124 | #endif |
| 125 | |
| 126 | #endif |