跳转至

HPC辅助宏

#pragma once

#include <cstddef>
#include <cstdint>
#include <type_traits>

#if defined(__GNUC__) || defined(__clang__)
#define HPC_FAST_BEGIN                 \
  _Pragma("GCC push_options") _Pragma( \
      "GCC optimize (\"O3,unroll-loops,associative-math,no-signed-zeros\")")
#define HPC_FAST_END _Pragma("GCC pop_options")
#elif defined(_MSC_VER)
#define HPC_FAST_BEGIN __pragma(optimize("gt", on))
#define HPC_FAST_END __pragma(optimize("", on))
#else
#define HPC_FAST_BEGIN
#define HPC_FAST_END
#endif

#if defined(__GNUC__) || defined(__clang__)
#define HPC_ALIGN(N) __attribute__((aligned(N)))
#elif defined(_MSC_VER)
#define HPC_ALIGN(N) __declspec(align(N))
#else
#define HPC_ALIGN(N) alignas(N)
#endif

namespace hpc {

/**
 * @brief 将指针从一种类型按位转换为另一种,是类型安全的 reinterpret_cast
 * 替代品。
 */
template <typename To, typename From>
inline To bit_cast_ptr(From* ptr) noexcept {
  static_assert(std::is_pointer_v<To>, "目标类型必须是指针。");
  return reinterpret_cast<To>(ptr);
}

/**
 * @brief 按位转换指针并立刻解引用,返回一个引用。
 */
template <typename To, typename From>
inline const To& bit_cast_ref(const From* ptr) noexcept {
  return *reinterpret_cast<const To*>(ptr);
}

}  // namespace hpc