This is an automated email from the ASF dual-hosted git repository.
pandalee pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new fb74a777f chore(c++): move common macro into macros.h (#2970)
fb74a777f is described below
commit fb74a777f723606d764ce6296961e7140071beb5
Author: Shawn Yang <[email protected]>
AuthorDate: Wed Dec 3 18:35:01 2025 +0800
chore(c++): move common macro into macros.h (#2970)
## Why?
<!-- Describe the purpose of this PR. -->
## What does this PR do?
<!-- Describe the details of this PR. -->
## Related issues
Closes #2969
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fory/issues/new/choose) describing the
need to do so and update the document if necessary.
Delete section if not applicable.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
Delete section if not applicable.
-->
---
cpp/fory/util/bit_util.h | 61 +++++----------------
cpp/fory/util/logging.h | 12 +----
cpp/fory/util/macros.h | 109 ++++++++++++++++++++++++++++++++++++++
cpp/fory/util/platform.h | 32 -----------
cpp/fory/util/result.h | 21 +-------
cpp/fory/util/string_util.cc | 10 +---
cpp/fory/util/string_util.h | 2 +-
cpp/fory/util/string_util_test.cc | 2 +-
8 files changed, 128 insertions(+), 121 deletions(-)
diff --git a/cpp/fory/util/bit_util.h b/cpp/fory/util/bit_util.h
index 53fe4f789..0a25f4eb7 100644
--- a/cpp/fory/util/bit_util.h
+++ b/cpp/fory/util/bit_util.h
@@ -18,6 +18,8 @@
*/
#pragma once
+
+#include "fory/util/macros.h"
#include <chrono>
#include <cstdint>
#include <ctime>
@@ -27,45 +29,6 @@
#include <string>
#include <type_traits>
-#ifdef _WIN32
-#define ROW_LITTLE_ENDIAN 1
-#else
-#ifdef __APPLE__
-
-#include <machine/endian.h>
-
-#else
-#include <endian.h>
-#endif
-#
-
-#ifndef __BYTE_ORDER__
-#error "__BYTE_ORDER__ not defined"
-#endif
-#
-
-#ifndef __ORDER_LITTLE_ENDIAN__
-#error "__ORDER_LITTLE_ENDIAN__ not defined"
-#endif
-#
-
-#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-#define ROW_LITTLE_ENDIAN 1
-#else
-#define ROW_LITTLE_ENDIAN 0
-#endif
-#endif
-#if defined(_MSC_VER)
-#include <intrin.h>
-#pragma intrinsic(_BitScanReverse)
-#pragma intrinsic(_BitScanForward)
-#define ROW_BYTE_SWAP64 _byteswap_uint64
-#define ROW_BYTE_SWAP32 _byteswap_ulong
-#else
-#define ROW_BYTE_SWAP64 __builtin_bswap64
-#define ROW_BYTE_SWAP32 __builtin_bswap32
-#endif
-
namespace fory {
namespace util {
@@ -75,16 +38,20 @@ namespace util {
//
// Swap the byte order (i.e. endianess)
-static inline int64_t ByteSwap(int64_t value) { return ROW_BYTE_SWAP64(value);
}
+static inline int64_t ByteSwap(int64_t value) {
+ return FORY_BYTE_SWAP64(value);
+}
static inline uint64_t ByteSwap(uint64_t value) {
- return static_cast<uint64_t>(ROW_BYTE_SWAP64(value));
+ return static_cast<uint64_t>(FORY_BYTE_SWAP64(value));
}
-static inline int32_t ByteSwap(int32_t value) { return ROW_BYTE_SWAP32(value);
}
+static inline int32_t ByteSwap(int32_t value) {
+ return FORY_BYTE_SWAP32(value);
+}
static inline uint32_t ByteSwap(uint32_t value) {
- return static_cast<uint32_t>(ROW_BYTE_SWAP32(value));
+ return static_cast<uint32_t>(FORY_BYTE_SWAP32(value));
}
static inline int16_t ByteSwap(int16_t value) {
@@ -98,14 +65,14 @@ static inline uint16_t ByteSwap(uint16_t value) {
static inline float ByteSwap(float value) {
auto *ptr = reinterpret_cast<uint32_t *>(&value);
- uint32_t i = ROW_BYTE_SWAP32(*ptr);
+ uint32_t i = FORY_BYTE_SWAP32(*ptr);
auto *f = reinterpret_cast<float *>(&i);
return *(f);
}
static inline double ByteSwap(double value) {
auto *ptr = reinterpret_cast<uint64_t *>(&value);
- uint64_t i = ROW_BYTE_SWAP64(*ptr);
+ uint64_t i = FORY_BYTE_SWAP64(*ptr);
auto *d = reinterpret_cast<double *>(&i);
return *d;
}
@@ -153,7 +120,7 @@ using EnableIfIsEndianConvertibleType =
template <typename T, typename = EnableIfIsEndianConvertibleType<T>>
static inline T ToBigEndian(T value) {
- if constexpr (ROW_LITTLE_ENDIAN) {
+ if constexpr (FORY_LITTLE_ENDIAN) {
return ByteSwap(value);
} else {
return value;
@@ -162,7 +129,7 @@ static inline T ToBigEndian(T value) {
template <typename T, typename = EnableIfIsEndianConvertibleType<T>>
static inline T ToLittleEndian(T value) {
- if constexpr (ROW_LITTLE_ENDIAN) {
+ if constexpr (FORY_LITTLE_ENDIAN) {
return value;
} else {
return ByteSwap(value);
diff --git a/cpp/fory/util/logging.h b/cpp/fory/util/logging.h
index a5be8cd68..dcc64c0c0 100644
--- a/cpp/fory/util/logging.h
+++ b/cpp/fory/util/logging.h
@@ -24,17 +24,7 @@
#include <iostream>
#include <string>
-// Force-inline attribute for hot path functions
-#if defined(__GNUC__) || defined(__clang__)
-#define FORY_ALWAYS_INLINE __attribute__((always_inline)) inline
-#define FORY_NOINLINE __attribute__((noinline))
-#elif defined(_MSC_VER)
-#define FORY_ALWAYS_INLINE __forceinline
-#define FORY_NOINLINE __declspec(noinline)
-#else
-#define FORY_ALWAYS_INLINE inline
-#define FORY_NOINLINE
-#endif
+#include "fory/util/macros.h"
namespace fory {
diff --git a/cpp/fory/util/macros.h b/cpp/fory/util/macros.h
new file mode 100644
index 000000000..11f4bf67e
--- /dev/null
+++ b/cpp/fory/util/macros.h
@@ -0,0 +1,109 @@
+/*
+ * 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
+
+// Platform detection for SIMD
+#if defined(__x86_64__) || defined(_M_X64)
+#include <immintrin.h>
+#define FORY_HAS_IMMINTRIN
+#elif defined(__ARM_NEON) || defined(__ARM_NEON__)
+#include <arm_neon.h>
+#define FORY_HAS_NEON
+#elif defined(__SSE2__)
+#include <emmintrin.h>
+#define FORY_HAS_SSE2
+#elif defined(__riscv) && __riscv_vector
+#include <riscv_vector.h>
+#define FORY_HAS_RISCV_VECTOR
+#endif
+
+// Force-inline attribute for hot path functions
+#if defined(__GNUC__) || defined(__clang__)
+#define FORY_ALWAYS_INLINE __attribute__((always_inline)) inline
+#define FORY_NOINLINE __attribute__((noinline))
+#elif defined(_MSC_VER)
+#define FORY_ALWAYS_INLINE __forceinline
+#define FORY_NOINLINE __declspec(noinline)
+#else
+#define FORY_ALWAYS_INLINE inline
+#define FORY_NOINLINE
+#endif
+
+// GCC branch prediction hints
+#if defined(__GNUC__)
+#define FORY_PREDICT_FALSE(x) (__builtin_expect(x, 0))
+#define FORY_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
+#define FORY_NORETURN __attribute__((noreturn))
+#define FORY_PREFETCH(addr) __builtin_prefetch(addr)
+#elif defined(_MSC_VER)
+#define FORY_NORETURN __declspec(noreturn)
+#define FORY_PREDICT_FALSE(x) x
+#define FORY_PREDICT_TRUE(x) x
+#define FORY_PREFETCH(addr)
+#else
+#define FORY_NORETURN
+#define FORY_PREDICT_FALSE(x) x
+#define FORY_PREDICT_TRUE(x) x
+#define FORY_PREFETCH(addr)
+#endif
+
+// Endianness detection
+#ifdef _WIN32
+#define FORY_LITTLE_ENDIAN 1
+#else
+#ifdef __APPLE__
+#include <machine/endian.h>
+#else
+#include <endian.h>
+#endif
+
+#ifndef __BYTE_ORDER__
+#error "__BYTE_ORDER__ not defined"
+#endif
+
+#ifndef __ORDER_LITTLE_ENDIAN__
+#error "__ORDER_LITTLE_ENDIAN__ not defined"
+#endif
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define FORY_LITTLE_ENDIAN 1
+#else
+#define FORY_LITTLE_ENDIAN 0
+#endif
+#endif
+
+// Byte swap macros
+#if defined(_MSC_VER)
+#include <intrin.h>
+#pragma intrinsic(_BitScanReverse)
+#pragma intrinsic(_BitScanForward)
+#define FORY_BYTE_SWAP64 _byteswap_uint64
+#define FORY_BYTE_SWAP32 _byteswap_ulong
+#else
+#define FORY_BYTE_SWAP64 __builtin_bswap64
+#define FORY_BYTE_SWAP32 __builtin_bswap32
+#endif
+
+// Target-specific function attributes
+#if defined(__GNUC__) && !defined(_MSC_VER)
+#define FORY_TARGET_AVX2_ATTR __attribute__((target("avx2")))
+#else
+#define FORY_TARGET_AVX2_ATTR
+#endif
diff --git a/cpp/fory/util/platform.h b/cpp/fory/util/platform.h
deleted file mode 100644
index 8e03f19a8..000000000
--- a/cpp/fory/util/platform.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.
- */
-
-#if defined(__x86_64__) || defined(_M_X64)
-#include <immintrin.h>
-#define FORY_HAS_IMMINTRIN
-#elif defined(__ARM_NEON) || defined(__ARM_NEON__)
-#include <arm_neon.h>
-#define FORY_HAS_NEON
-#elif defined(__SSE2__)
-#include <emmintrin.h>
-#define FORY_HAS_SSE2
-#elif defined(__riscv) && __riscv_vector
-#include <riscv_vector.h>
-#define FORY_HAS_RISCV_VECTOR
-#endif
diff --git a/cpp/fory/util/result.h b/cpp/fory/util/result.h
index 7fe24fe5b..052050be7 100644
--- a/cpp/fory/util/result.h
+++ b/cpp/fory/util/result.h
@@ -21,29 +21,10 @@
#include "fory/util/error.h"
#include "fory/util/logging.h"
+#include "fory/util/macros.h"
#include <type_traits>
#include <utility>
-//
-// GCC branch prediction hints
-//
-#if defined(__GNUC__)
-#define FORY_PREDICT_FALSE(x) (__builtin_expect(x, 0))
-#define FORY_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
-#define FORY_NORETURN __attribute__((noreturn))
-#define FORY_PREFETCH(addr) __builtin_prefetch(addr)
-#elif defined(_MSC_VER)
-#define FORY_NORETURN __declspec(noreturn))
-#define FORY_PREDICT_FALSE(x) x
-#define FORY_PREDICT_TRUE(x) x
-#define FORY_PREFETCH(addr)
-#else
-#define FORY_NORETURN
-#define FORY_PREDICT_FALSE(x) x
-#define FORY_PREDICT_TRUE(x) x
-#define FORY_PREFETCH(addr)
-#endif
-
namespace fory {
/// Helper type to disambiguate error construction in Result<T, E>
diff --git a/cpp/fory/util/string_util.cc b/cpp/fory/util/string_util.cc
index 9ce817c67..f1f6a6c55 100644
--- a/cpp/fory/util/string_util.cc
+++ b/cpp/fory/util/string_util.cc
@@ -20,15 +20,9 @@
#include <chrono>
#include <string>
-#include "platform.h"
+#include "macros.h"
#include "string_util.h"
-#if defined(__GNUC__) && !defined(_MSC_VER)
-#define FORY_TARGET_AVX2_ATTR __attribute__((target("avx2")))
-#else
-#define FORY_TARGET_AVX2_ATTR
-#endif
-
namespace fory {
std::u16string utf8ToUtf16SIMD(const std::string &utf8, bool is_little_endian)
{
@@ -519,5 +513,3 @@ std::u16string utf8ToUtf16(const std::string &utf8, bool
is_little_endian) {
#endif
} // namespace fory
-
-#undef FORY_TARGET_AVX2_ATTR
diff --git a/cpp/fory/util/string_util.h b/cpp/fory/util/string_util.h
index 9bfe270d3..878f72752 100644
--- a/cpp/fory/util/string_util.h
+++ b/cpp/fory/util/string_util.h
@@ -19,7 +19,7 @@
#pragma once
-#include "platform.h"
+#include "macros.h"
#include <array>
#include <cstdint>
#include <string>
diff --git a/cpp/fory/util/string_util_test.cc
b/cpp/fory/util/string_util_test.cc
index f2d6b5ef0..ec65cf40c 100644
--- a/cpp/fory/util/string_util_test.cc
+++ b/cpp/fory/util/string_util_test.cc
@@ -23,7 +23,7 @@
#include <random>
#include "fory/util/logging.h"
-#include "platform.h"
+#include "macros.h"
#include "string_util.h"
#include "gtest/gtest.h"
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]