Skip to content

Commit

Permalink
GH-44249: [C++] Unify simd header includings (#44250)
Browse files Browse the repository at this point in the history
### Rationale for this change

#44249

### What changes are included in this PR?

Change all the system header inclusions to `simd.h`.

Also removed some duplicated defines.

### Are these changes tested?

Existing tests should be good.

### Are there any user-facing changes?

None.

* GitHub Issue: #44249

Authored-by: Ruoxi Sun <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
zanmato1984 authored Oct 2, 2024
1 parent 15cc84d commit 60f29e9
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 32 deletions.
6 changes: 5 additions & 1 deletion cpp/build-support/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,11 @@
# Hardware specific headers
'arm_neon.h',
'emmintrin.h',
'xmmintin.h',
'immintrin.h',
'intrin.h',
'nmmintrin.h',
'x86intrin.h',
'xmmintrin.h',
])

# Folders of C libraries so commonly used in C++,
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/arrow/acero/bloom_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
// under the License.

#include "arrow/acero/bloom_filter.h"

#include <random>
#include "arrow/acero/util.h" // PREFETCH

#include "arrow/util/bit_util.h" // Log2
#include "arrow/util/bitmap_ops.h" // CountSetBits
#include "arrow/util/config.h"
#include "arrow/util/prefetch.h" // PREFETCH

namespace arrow {
namespace acero {
Expand Down Expand Up @@ -152,7 +154,7 @@ void BlockedBloomFilter::FindImp(int64_t num_rows, const T* hashes,
if (enable_prefetch && UsePrefetch()) {
constexpr int kPrefetchIterations = 16;
for (int64_t i = 0; i < num_rows - kPrefetchIterations; ++i) {
PREFETCH(blocks_ + block_id(hashes[i + kPrefetchIterations]));
ARROW_PREFETCH(blocks_ + block_id(hashes[i + kPrefetchIterations]));
uint64_t result = Find(hashes[i]) ? 1ULL : 0ULL;
bits |= result << (i & 63);
if ((i & 63) == 63) {
Expand Down
5 changes: 1 addition & 4 deletions cpp/src/arrow/acero/bloom_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

#pragma once

#if defined(ARROW_HAVE_RUNTIME_AVX2)
# include <immintrin.h>
#endif

#include <atomic>
#include <cstdint>
#include <memory>
Expand All @@ -30,6 +26,7 @@
#include "arrow/memory_pool.h"
#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/util/simd.h"

namespace arrow {
namespace acero {
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/acero/bloom_filter_avx2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
// specific language governing permissions and limitations
// under the License.

#include <immintrin.h>

#include "arrow/acero/bloom_filter.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/simd.h"

namespace arrow {
namespace acero {
Expand Down
5 changes: 1 addition & 4 deletions cpp/src/arrow/compute/key_hash_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@

#pragma once

#if defined(ARROW_HAVE_RUNTIME_AVX2)
# include <immintrin.h>
#endif

#include <cstdint>

#include "arrow/compute/light_array_internal.h"
#include "arrow/compute/util.h"
#include "arrow/util/simd.h"

namespace arrow {
namespace compute {
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/compute/key_hash_internal_avx2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
// specific language governing permissions and limitations
// under the License.

#include <immintrin.h>

#include "arrow/compute/key_hash_internal.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/simd.h"

namespace arrow {
namespace compute {
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/compute/key_map_internal_avx2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
// specific language governing permissions and limitations
// under the License.

#include <immintrin.h>

#include "arrow/compute/key_map_internal.h"
#include "arrow/util/logging.h"
#include "arrow/util/simd.h"

namespace arrow {
namespace compute {
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/compute/row/encode_internal_avx2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
// specific language governing permissions and limitations
// under the License.

#include <immintrin.h>

#include "arrow/compute/row/encode_internal.h"
#include "arrow/util/simd.h"

namespace arrow {
namespace compute {
Expand Down
9 changes: 1 addition & 8 deletions cpp/src/arrow/compute/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,17 @@
#include "arrow/compute/type_fwd.h"
#include "arrow/result.h"
#include "arrow/util/cpu_info.h"
#include "arrow/util/simd.h"

#if defined(__clang__) || defined(__GNUC__)
# define BYTESWAP(x) __builtin_bswap64(x)
# define ROTL(x, n) (((x) << (n)) | ((x) >> ((-n) & 31)))
# define ROTL64(x, n) (((x) << (n)) | ((x) >> ((-n) & 63)))
# define PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
#elif defined(_MSC_VER)
# include <intrin.h>
# define BYTESWAP(x) _byteswap_uint64(x)
# define ROTL(x, n) _rotl((x), (n))
# define ROTL64(x, n) _rotl64((x), (n))
# if defined(_M_X64) || defined(_M_I86)
// https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx
# include <mmintrin.h>
# define PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
# else
# define PREFETCH(ptr) (void)(ptr) /* disabled */
# endif
#endif

namespace arrow {
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/compute/util_avx2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// specific language governing permissions and limitations
// under the License.

#include <immintrin.h>
#include <cstring>

#include "arrow/util/bit_util.h"
#include "arrow/util/logging.h"
#include "arrow/util/simd.h"

namespace arrow::util::bit_util::avx2 {

Expand Down
1 change: 0 additions & 1 deletion cpp/src/arrow/util/bit_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#if defined(_MSC_VER)
# if defined(_M_AMD64) || defined(_M_X64)
# include <intrin.h> // IWYU pragma: keep
# include <nmmintrin.h>
# endif

# pragma intrinsic(_BitScanReverse)
Expand Down
3 changes: 0 additions & 3 deletions cpp/src/arrow/util/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
# define ARROW_FORCE_INLINE __attribute__((always_inline))
# define ARROW_PREDICT_FALSE(x) (__builtin_expect(!!(x), 0))
# define ARROW_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
# define ARROW_PREFETCH(addr) __builtin_prefetch(addr)
# define ARROW_RESTRICT __restrict
# if defined(__clang__) // clang-specific
# define ARROW_COMPILER_ASSUME(expr) __builtin_assume(expr)
Expand All @@ -105,7 +104,6 @@
# define ARROW_FORCE_INLINE __forceinline
# define ARROW_PREDICT_FALSE(x) (x)
# define ARROW_PREDICT_TRUE(x) (x)
# define ARROW_PREFETCH(addr)
# define ARROW_RESTRICT __restrict
# define ARROW_COMPILER_ASSUME(expr) __assume(expr)
#else
Expand All @@ -114,7 +112,6 @@
# define ARROW_FORCE_INLINE
# define ARROW_PREDICT_FALSE(x) (x)
# define ARROW_PREDICT_TRUE(x) (x)
# define ARROW_PREFETCH(addr)
# define ARROW_RESTRICT
# define ARROW_COMPILER_ASSUME(expr)
#endif
Expand Down
31 changes: 31 additions & 0 deletions cpp/src/arrow/util/prefetch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#if defined(__GNUC__) // GCC and compatible compilers (clang, Intel ICC)
# define ARROW_PREFETCH(addr) __builtin_prefetch(addr)
#elif defined(_MSC_VER) // MSVC
# if defined(ARROW_HAVE_SSE4_2) || defined(ARROW_HAVE_RUNTIME_SSE4_2)
# include <nmmintrin.h>
# define ARROW_PREFETCH(addr) _mm_prefetch((const char*)(addr), _MM_HINT_T0)
# else
# define ARROW_PREFETCH(addr)
# endif
#else
# define ARROW_PREFETCH(addr)
#endif

0 comments on commit 60f29e9

Please sign in to comment.