be/src/storage/segment/bitshuffle_wrapper.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "storage/segment/bitshuffle_wrapper.h" |
19 | | |
20 | | // Include the bitshuffle header once to get the default (non-AVX2) |
21 | | // symbols. |
22 | | #include <bitshuffle/bitshuffle.h> |
23 | | |
24 | | // Include the bitshuffle header again, but this time importing the |
25 | | // AVX2-compiled symbols by defining some macros. |
26 | | #undef BITSHUFFLE_H |
27 | | #define bshuf_compress_lz4_bound bshuf_compress_lz4_bound_avx2 |
28 | | #define bshuf_compress_lz4 bshuf_compress_lz4_avx2 |
29 | | #define bshuf_decompress_lz4 bshuf_decompress_lz4_avx2 |
30 | | #include <bitshuffle/bitshuffle.h> // NOLINT(*) |
31 | | #undef bshuf_compress_lz4_bound |
32 | | #undef bshuf_compress_lz4 |
33 | | #undef bshuf_decompress_lz4 |
34 | | |
35 | | #undef BITSHUFFLE_H |
36 | | #define bshuf_compress_lz4_bound bshuf_compress_lz4_bound_neon |
37 | | #define bshuf_compress_lz4 bshuf_compress_lz4_neon |
38 | | #define bshuf_decompress_lz4 bshuf_decompress_lz4_neon |
39 | | #include <bitshuffle/bitshuffle.h> // NOLINT(*) |
40 | | #undef bshuf_compress_lz4_bound |
41 | | #undef bshuf_compress_lz4 |
42 | | #undef bshuf_decompress_lz4 |
43 | | |
44 | | namespace doris { |
45 | | namespace bitshuffle { |
46 | | |
47 | | // Function pointers which will be assigned the correct implementation |
48 | | // for the runtime architecture. |
49 | | namespace { |
50 | | decltype(&bshuf_compress_lz4_bound) g_bshuf_compress_lz4_bound; |
51 | | decltype(&bshuf_compress_lz4) g_bshuf_compress_lz4; |
52 | | decltype(&bshuf_decompress_lz4) g_bshuf_decompress_lz4; |
53 | | } // anonymous namespace |
54 | | |
55 | | // When this translation unit is initialized, figure out the current CPU and |
56 | | // assign the correct function for this architecture. |
57 | | // |
58 | | // This avoids an expensive 'cpuid' call in the hot path, and also avoids |
59 | | // the cost of a 'std::once' call. |
60 | 9 | __attribute__((constructor)) void SelectBitshuffleFunctions() { |
61 | 9 | #ifdef __AVX2__ |
62 | 9 | g_bshuf_compress_lz4_bound = bshuf_compress_lz4_bound_avx2; |
63 | 9 | g_bshuf_compress_lz4 = bshuf_compress_lz4_avx2; |
64 | 9 | g_bshuf_decompress_lz4 = bshuf_decompress_lz4_avx2; |
65 | | #elif (defined(__i386) || defined(__x86_64__)) |
66 | | g_bshuf_compress_lz4_bound = bshuf_compress_lz4_bound; |
67 | | g_bshuf_compress_lz4 = bshuf_compress_lz4; |
68 | | g_bshuf_decompress_lz4 = bshuf_decompress_lz4; |
69 | | |
70 | | #elif defined(__ARM_NEON) && defined(__aarch64__) && !defined(__APPLE__) |
71 | | g_bshuf_compress_lz4_bound = bshuf_compress_lz4_bound_neon; |
72 | | g_bshuf_compress_lz4 = bshuf_compress_lz4_neon; |
73 | | g_bshuf_decompress_lz4 = bshuf_decompress_lz4_neon; |
74 | | #else |
75 | | g_bshuf_compress_lz4_bound = bshuf_compress_lz4_bound; |
76 | | g_bshuf_compress_lz4 = bshuf_compress_lz4; |
77 | | g_bshuf_decompress_lz4 = bshuf_decompress_lz4; |
78 | | #endif |
79 | 9 | } |
80 | | |
81 | 864k | int64_t compress_lz4(void* in, void* out, size_t size, size_t elem_size, size_t block_size) { |
82 | 864k | return g_bshuf_compress_lz4(in, out, size, elem_size, block_size); |
83 | 864k | } |
84 | 784k | int64_t decompress_lz4(void* in, void* out, size_t size, size_t elem_size, size_t block_size) { |
85 | 784k | return g_bshuf_decompress_lz4(in, out, size, elem_size, block_size); |
86 | 784k | } |
87 | 864k | size_t compress_lz4_bound(size_t size, size_t elem_size, size_t block_size) { |
88 | 864k | return g_bshuf_compress_lz4_bound(size, elem_size, block_size); |
89 | 864k | } |
90 | | |
91 | | } // namespace bitshuffle |
92 | | } // namespace doris |