]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/BrotliCompress/enc/port.h
1 /* Copyright 2013 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7 /* Macros for endianness, branch prediction and unaligned loads and stores. */
9 #ifndef BROTLI_ENC_PORT_H_
10 #define BROTLI_ENC_PORT_H_
13 #include <string.h> /* memcpy */
15 #include "../common/port.h"
16 #include "../common/types.h"
18 #if defined OS_LINUX || defined OS_CYGWIN
20 #elif defined OS_FREEBSD
21 #include <machine/endian.h>
22 #elif defined OS_MACOSX
23 #include <machine/endian.h>
24 /* Let's try and follow the Linux convention */
25 #define __BYTE_ORDER BYTE_ORDER
26 #define __LITTLE_ENDIAN LITTLE_ENDIAN
29 /* define the macro IS_LITTLE_ENDIAN
30 using the above endian definitions from endian.h if
31 endian.h was included */
33 #if __BYTE_ORDER == __LITTLE_ENDIAN
34 #define IS_LITTLE_ENDIAN
39 #if defined(__LITTLE_ENDIAN__)
40 #define IS_LITTLE_ENDIAN
42 #endif /* __BYTE_ORDER */
44 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
45 #define IS_LITTLE_ENDIAN
48 /* Enable little-endian optimization for x64 architecture on Windows. */
49 #if (defined(_WIN32) || defined(_WIN64)) && defined(_M_X64)
50 #define IS_LITTLE_ENDIAN
53 /* Portable handling of unaligned loads, stores, and copies.
54 On some platforms, like ARM, the copy functions can be more efficient
55 then a load and a store. */
57 #if defined(ARCH_PIII) || \
58 defined(ARCH_ATHLON) || defined(ARCH_K8) || defined(_ARCH_PPC)
60 /* x86 and x86-64 can perform unaligned loads/stores directly;
61 modern PowerPC hardware can also do unaligned integer loads and stores;
62 but note: the FPU still sends unaligned loads and stores to a trap handler!
65 #define BROTLI_UNALIGNED_LOAD32(_p) (*(const uint32_t *)(_p))
66 #define BROTLI_UNALIGNED_LOAD64(_p) (*(const uint64_t *)(_p))
68 #define BROTLI_UNALIGNED_STORE32(_p, _val) \
69 (*(uint32_t *)(_p) = (_val))
70 #define BROTLI_UNALIGNED_STORE64(_p, _val) \
71 (*(uint64_t *)(_p) = (_val))
73 #elif defined(__arm__) && \
74 !defined(__ARM_ARCH_5__) && \
75 !defined(__ARM_ARCH_5T__) && \
76 !defined(__ARM_ARCH_5TE__) && \
77 !defined(__ARM_ARCH_5TEJ__) && \
78 !defined(__ARM_ARCH_6__) && \
79 !defined(__ARM_ARCH_6J__) && \
80 !defined(__ARM_ARCH_6K__) && \
81 !defined(__ARM_ARCH_6Z__) && \
82 !defined(__ARM_ARCH_6ZK__) && \
83 !defined(__ARM_ARCH_6T2__)
85 /* ARMv7 and newer support native unaligned accesses, but only of 16-bit
86 and 32-bit values (not 64-bit); older versions either raise a fatal signal,
87 do an unaligned read and rotate the words around a bit, or do the reads very
88 slowly (trip through kernel mode). */
90 #define BROTLI_UNALIGNED_LOAD32(_p) (*(const uint32_t *)(_p))
91 #define BROTLI_UNALIGNED_STORE32(_p, _val) \
92 (*(uint32_t *)(_p) = (_val))
94 static BROTLI_INLINE
uint64_t BROTLI_UNALIGNED_LOAD64(const void *p
) {
96 memcpy(&t
, p
, sizeof t
);
100 static BROTLI_INLINE
void BROTLI_UNALIGNED_STORE64(void *p
, uint64_t v
) {
101 memcpy(p
, &v
, sizeof v
);
106 /* These functions are provided for architectures that don't support */
107 /* unaligned loads and stores. */
109 static BROTLI_INLINE
uint32_t BROTLI_UNALIGNED_LOAD32(const void *p
) {
111 memcpy(&t
, p
, sizeof t
);
115 static BROTLI_INLINE
uint64_t BROTLI_UNALIGNED_LOAD64(const void *p
) {
117 memcpy(&t
, p
, sizeof t
);
121 static BROTLI_INLINE
void BROTLI_UNALIGNED_STORE32(void *p
, uint32_t v
) {
122 memcpy(p
, &v
, sizeof v
);
125 static BROTLI_INLINE
void BROTLI_UNALIGNED_STORE64(void *p
, uint64_t v
) {
126 memcpy(p
, &v
, sizeof v
);
131 #if !defined(__cplusplus) && !defined(c_plusplus) && __STDC_VERSION__ >= 199901L
132 #define BROTLI_RESTRICT restrict
133 #elif BROTLI_GCC_VERSION > 295 || defined(__llvm__)
134 #define BROTLI_RESTRICT __restrict
136 #define BROTLI_RESTRICT
139 #define _TEMPLATE(T) \
140 static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \
141 static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }
142 _TEMPLATE(double) _TEMPLATE(float) _TEMPLATE(int)
143 _TEMPLATE(size_t) _TEMPLATE(uint32_t) _TEMPLATE(uint8_t)
145 #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
146 #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
148 #define BROTLI_SWAP(T, A, I, J) { \
149 T __brotli_swap_tmp = (A)[(I)]; \
150 (A)[(I)] = (A)[(J)]; \
151 (A)[(J)] = __brotli_swap_tmp; \
154 #define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \
156 size_t _new_size = (C == 0) ? (R) : C; \
158 while (_new_size < (R)) _new_size *= 2; \
159 new_array = BROTLI_ALLOC((M), T, _new_size); \
160 if (!BROTLI_IS_OOM(m) && C != 0) \
161 memcpy(new_array, A, C * sizeof(T)); \
162 BROTLI_FREE((M), A); \
168 #endif /* BROTLI_ENC_PORT_H_ */