Skip to content

Commit

Permalink
Merge pull request #1 from google/master
Browse files Browse the repository at this point in the history
sync latest
  • Loading branch information
ym38cho authored Oct 18, 2024
2 parents 1d56afc + a97b5d3 commit b76001d
Show file tree
Hide file tree
Showing 98 changed files with 1,782 additions and 649 deletions.
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,14 @@ HWY_TESTS = [
("hwy/", "targets_test"),
("hwy/tests/", "arithmetic_test"),
("hwy/tests/", "bit_permute_test"),
("hwy/tests/", "blockwise_combine_test"),
("hwy/tests/", "blockwise_shift_test"),
("hwy/tests/", "blockwise_test"),
("hwy/tests/", "cast_test"),
("hwy/tests/", "combine_test"),
("hwy/tests/", "compare_test"),
("hwy/tests/", "compress_test"),
("hwy/tests/", "concat_test"),
("hwy/tests/", "convert_test"),
("hwy/tests/", "count_test"),
("hwy/tests/", "crypto_test"),
Expand Down
79 changes: 69 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ if(CHECK_PIE_SUPPORTED)
endif()
endif()

if (CMAKE_CXX_COMPILER_ARCHITECTURE_ID MATCHES "RISCV32|RISCV64|RISCV128" OR CMAKE_SYSTEM_PROCESSOR MATCHES "riscv32|riscv64|riscv128")
include(CheckCSourceCompiles)
check_c_source_compiles("
#if __riscv_xlen == 64
int main() { return 0; }
#else
#error Not RISCV-64
#endif
" IS_RISCV_XLEN_64)

check_c_source_compiles("
#if __riscv_xlen == 32
int main() { return 0; }
#else
#error Not RISCV-32
#endif
" IS_RISCV_XLEN_32)

if(IS_RISCV_XLEN_32)
set(RISCV_XLEN 32)
elseif(IS_RISCV_XLEN_64)
set(RISCV_XLEN 64)
else()
message(WARNING "Unable to determine RISC-V XLEN")
endif()
endif()

include(GNUInstallDirs)

if (NOT CMAKE_BUILD_TYPE)
Expand All @@ -72,7 +99,7 @@ set(HWY_CMAKE_ARM7 OFF CACHE BOOL "Set copts for Armv7 with NEON (requires vfpv4
# skipped. For GCC 13.1+, you can also build with -fexcess-precision=standard.
set(HWY_CMAKE_SSE2 OFF CACHE BOOL "Set SSE2 as baseline for 32-bit x86?")

# Currently this will compile the entire codebase with `-march=rv64gcv1p0`:
# Currently this will compile the entire codebase with `-march=rv<XLEN>gcv1p0`:
set(HWY_CMAKE_RVV ON CACHE BOOL "Set copts for RISCV with RVV?")

# Unconditionally adding -Werror risks breaking the build when new warnings
Expand All @@ -87,6 +114,12 @@ set(HWY_ENABLE_EXAMPLES ON CACHE BOOL "Build examples")
set(HWY_ENABLE_INSTALL ON CACHE BOOL "Install library")
set(HWY_ENABLE_TESTS ON CACHE BOOL "Enable HWY tests")

if (MSVC)
set(HWY_TEST_STANDALONE ON CACHE BOOL "Disable use of googletest")
else()
set(HWY_TEST_STANDALONE OFF CACHE BOOL "Disable use of googletest")
endif()

if (NOT DEFINED CMAKE_CXX_STANDARD)
if ("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
set(HWY_CXX_STD_TGT_COMPILE_FEATURE cxx_std_17)
Expand Down Expand Up @@ -378,8 +411,13 @@ else()
# we add the gcv compiler flag, which then requires the CPU (now when using
# either compiler) to support V.
if(HWY_CMAKE_RVV)
list(APPEND HWY_FLAGS -march=rv64gcv1p0)
add_link_options(-march=rv64gcv1p0)
if(RISCV_XLEN EQUAL 64)
list(APPEND HWY_FLAGS -march=rv64gcv1p0)
add_link_options(-march=rv64gcv1p0)
elseif(RISCV_XLEN EQUAL 32)
list(APPEND HWY_FLAGS -march=rv32gcv1p0)
add_link_options(-march=rv32gcv1p0)
endif()
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
list(APPEND HWY_FLAGS -menable-experimental-extensions)
endif()
Expand Down Expand Up @@ -579,6 +617,15 @@ if (HWY_ENABLE_CONTRIB)
list(APPEND HWY_PC_FILES libhwy-contrib.pc)
endif() # HWY_ENABLE_CONTRIB
if (HWY_ENABLE_TESTS)

if (HWY_TEST_STANDALONE)
set(HWY_PC_HWY_TEST_REQUIRES "")
set(HWY_PC_HWY_TEST_CFLAGS "-DHWY_TEST_STANDALONE=1")
else()
set(HWY_PC_HWY_TEST_REQUIRES "gtest")
set(HWY_PC_HWY_TEST_CFLAGS "")
endif()

list(APPEND HWY_PC_FILES libhwy-test.pc)
endif() # HWY_ENABLE_TESTS
foreach (pc ${HWY_PC_FILES})
Expand Down Expand Up @@ -628,6 +675,8 @@ enable_testing()
include(GoogleTest)

set(HWY_SYSTEM_GTEST OFF CACHE BOOL "Use pre-installed googletest?")

if(NOT HWY_TEST_STANDALONE)
if(HWY_SYSTEM_GTEST)
find_package(GTest REQUIRED)
else()
Expand Down Expand Up @@ -656,6 +705,7 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
endif() # HWY_SYSTEM_GTEST
endif() # HWY_TEST_STANDALONE

set(HWY_TEST_FILES
hwy/contrib/algo/copy_test.cc
Expand All @@ -671,12 +721,14 @@ set(HWY_TEST_FILES
hwy/examples/skeleton_test.cc
hwy/tests/arithmetic_test.cc
hwy/tests/bit_permute_test.cc
hwy/tests/blockwise_combine_test.cc
hwy/tests/blockwise_shift_test.cc
hwy/tests/blockwise_test.cc
hwy/tests/cast_test.cc
hwy/tests/combine_test.cc
hwy/tests/compare_test.cc
hwy/tests/compress_test.cc
hwy/tests/concat_test.cc
hwy/tests/convert_test.cc
hwy/tests/count_test.cc
hwy/tests/crypto_test.cc
Expand Down Expand Up @@ -745,15 +797,19 @@ list(APPEND HWY_TEST_FILES
)
endif() # HWY_ENABLE_CONTRIB

if(HWY_SYSTEM_GTEST)
if (CMAKE_VERSION VERSION_LESS 3.20)
set(HWY_GTEST_LIBS GTest::GTest GTest::Main)
if(HWY_TEST_STANDALONE)
set(HWY_GTEST_LIBS "")
else()
if(HWY_SYSTEM_GTEST)
if (CMAKE_VERSION VERSION_LESS 3.20)
set(HWY_GTEST_LIBS GTest::GTest GTest::Main)
else()
set(HWY_GTEST_LIBS GTest::gtest GTest::gtest_main)
endif()
else()
set(HWY_GTEST_LIBS GTest::gtest GTest::gtest_main)
set(HWY_GTEST_LIBS gtest gtest_main)
endif()
else()
set(HWY_GTEST_LIBS gtest gtest_main)
endif()
endif() # HWY_TEST_STANDALONE

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests)
foreach (TESTFILE IN LISTS HWY_TEST_FILES)
Expand All @@ -766,6 +822,9 @@ foreach (TESTFILE IN LISTS HWY_TEST_FILES)
# cause compile errors because only one may be set, and other CMakeLists.txt
# that include us may set them.
target_compile_options(${TESTNAME} PRIVATE -DHWY_IS_TEST=1)
if(HWY_TEST_STANDALONE)
target_compile_options(${TESTNAME} PRIVATE -DHWY_TEST_STANDALONE=1)
endif()
target_compile_features(${TESTNAME} PRIVATE ${HWY_CXX_STD_TGT_COMPILE_FEATURE})

target_link_libraries(${TESTNAME} PRIVATE ${HWY_TEST_LIBS} ${HWY_GTEST_LIBS})
Expand Down
14 changes: 9 additions & 5 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module(name = "highway", version = "1.2.0")
module(
name = "highway",
version = "1.2.0",
)

bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "googletest", version = "1.12.1")
bazel_dep(name = "rules_cc", version = "0.0.4")
bazel_dep(name = "rules_license", version = "0.0.4")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "googletest", version = "1.15.2")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_license", version = "0.0.7")
bazel_dep(name = "platforms", version = "0.0.10")
Binary file modified g3doc/highway_intro.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion hwy/abort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ HWY_DLLEXPORT HWY_NORETURN void HWY_FORMAT(3, 4)
// Now terminate the program:
#if HWY_ARCH_RISCV
exit(1); // trap/abort just freeze Spike.
#elif HWY_IS_DEBUG_BUILD && !HWY_COMPILER_MSVC
#elif HWY_IS_DEBUG_BUILD && !HWY_COMPILER_MSVC && !HWY_ARCH_ARM
// Facilitates breaking into a debugger, but don't use this in non-debug
// builds because it looks like "illegal instruction", which is misleading.
// Also does not work on Arm.
__builtin_trap();
#else
abort(); // Compile error without this due to HWY_NORETURN.
Expand Down
4 changes: 2 additions & 2 deletions hwy/abort_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
#include "hwy/tests/test_util-inl.h" // HWY_ASSERT_EQ

namespace hwy {
namespace {

#ifdef GTEST_HAS_DEATH_TEST
namespace {
std::string GetBaseName(std::string const& file_name) {
auto last_slash = file_name.find_last_of("/\\");
return file_name.substr(last_slash + 1);
}
} // namespace

TEST(AbortDeathTest, AbortDefault) {
std::string expected = std::string("Abort at ") + GetBaseName(__FILE__) +
Expand Down Expand Up @@ -68,6 +67,7 @@ TEST(AbortTest, AbortOverrideChain) {
HWY_ASSERT(GetAbortFunc() == nullptr);
}

} // namespace
} // namespace hwy

HWY_TEST_MAIN();
3 changes: 2 additions & 1 deletion hwy/aligned_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
namespace hwy {
namespace {

#if HWY_ARCH_RISCV && defined(__riscv_v_intrinsic) && __riscv_v_intrinsic >= 11000
#if HWY_ARCH_RISCV && defined(__riscv_v_intrinsic) && \
__riscv_v_intrinsic >= 11000
// Not actually an upper bound on the size, but this value prevents crossing a
// 4K boundary (relevant on Andes).
constexpr size_t kAlignment = HWY_MAX(HWY_ALIGNMENT, 4096);
Expand Down
12 changes: 6 additions & 6 deletions hwy/aligned_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ static inline constexpr size_t ShiftCount(size_t n) {

template <typename T>
T* AllocateAlignedItems(size_t items, AllocPtr alloc_ptr, void* opaque_ptr) {
constexpr size_t size = sizeof(T);
constexpr size_t kSize = sizeof(T);

constexpr bool is_pow2 = (size & (size - 1)) == 0;
constexpr size_t bits = ShiftCount(size);
static_assert(!is_pow2 || (1ull << bits) == size, "ShiftCount is incorrect");
constexpr bool kIsPow2 = (kSize & (kSize - 1)) == 0;
constexpr size_t kBits = ShiftCount(kSize);
static_assert(!kIsPow2 || (1ull << kBits) == kSize, "ShiftCount has a bug");

const size_t bytes = is_pow2 ? items << bits : items * size;
const size_t check = is_pow2 ? bytes >> bits : bytes / size;
const size_t bytes = kIsPow2 ? items << kBits : items * kSize;
const size_t check = kIsPow2 ? bytes >> kBits : bytes / kSize;
if (check != items) {
return nullptr; // overflowed
}
Expand Down
3 changes: 1 addition & 2 deletions hwy/aligned_allocator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class FakeAllocator {
} // namespace

namespace hwy {
namespace {

#if !HWY_TEST_STANDALONE
class AlignedAllocatorTest : public testing::Test {};
Expand Down Expand Up @@ -283,8 +284,6 @@ TEST(AlignedAllocatorTest, TestDefaultInit) {
(addr2 >> (kBits - 1)) >> (kBits - 1));
}

namespace {

using std::array;
using std::vector;

Expand Down
3 changes: 2 additions & 1 deletion hwy/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ namespace hwy {
// Returns a pointer whose type is `type` (T*), while allowing the compiler to
// assume that the untyped pointer `ptr` is aligned to a multiple of sizeof(T).
#define HWY_RCAST_ALIGNED(type, ptr) \
reinterpret_cast<type>(HWY_ASSUME_ALIGNED((ptr), alignof(RemovePtr<type>)))
reinterpret_cast<type>( \
HWY_ASSUME_ALIGNED((ptr), alignof(hwy::RemovePtr<type>)))

// Clang and GCC require attributes on each function into which SIMD intrinsics
// are inlined. Support both per-function annotation (HWY_ATTR) for lambdas and
Expand Down
9 changes: 6 additions & 3 deletions hwy/base_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
namespace {

HWY_NOINLINE void TestAllLimits() {
HWY_ASSERT_EQ(uint8_t{0}, LimitsMin<uint8_t>());
Expand Down Expand Up @@ -837,14 +838,15 @@ HWY_NOINLINE void TestAllSpecialFloat() {
test(bfloat16_t());
}

} // namespace
// NOLINTNEXTLINE(google-readability-namespace-comments)
} // namespace HWY_NAMESPACE
} // namespace hwy
HWY_AFTER_NAMESPACE();

#if HWY_ONCE

namespace hwy {
namespace {
HWY_BEFORE_TEST(BaseTest);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllLimits);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllLowestHighest);
Expand All @@ -858,6 +860,7 @@ HWY_EXPORT_AND_TEST_P(BaseTest, TestAllMul128);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllEndian);
HWY_EXPORT_AND_TEST_P(BaseTest, TestAllSpecialFloat);
HWY_AFTER_TEST();
} // namespace
} // namespace hwy

#endif
HWY_TEST_MAIN();
#endif // HWY_ONCE
9 changes: 6 additions & 3 deletions hwy/contrib/algo/copy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
namespace {

// Returns random integer in [0, 128), which fits in any lane type.
template <typename T>
Expand Down Expand Up @@ -189,19 +190,21 @@ void TestAllCopyIf() {
ForUI163264(ForPartialVectors<ForeachCountAndMisalign<TestCopyIf>>());
}

} // namespace
// NOLINTNEXTLINE(google-readability-namespace-comments)
} // namespace HWY_NAMESPACE
} // namespace hwy
HWY_AFTER_NAMESPACE();

#if HWY_ONCE

namespace hwy {
namespace {
HWY_BEFORE_TEST(CopyTest);
HWY_EXPORT_AND_TEST_P(CopyTest, TestAllFill);
HWY_EXPORT_AND_TEST_P(CopyTest, TestAllCopy);
HWY_EXPORT_AND_TEST_P(CopyTest, TestAllCopyIf);
HWY_AFTER_TEST();
} // namespace
} // namespace hwy

#endif
HWY_TEST_MAIN();
#endif // HWY_ONCE
9 changes: 6 additions & 3 deletions hwy/contrib/algo/find_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
namespace {

// Returns random number in [-8, 8] - we use knowledge of the range to Find()
// values we know are not present.
Expand Down Expand Up @@ -210,18 +211,20 @@ void TestAllFindIf() {
ForAllTypes(ForPartialVectors<ForeachCountAndMisalign<TestFindIf>>());
}

} // namespace
// NOLINTNEXTLINE(google-readability-namespace-comments)
} // namespace HWY_NAMESPACE
} // namespace hwy
HWY_AFTER_NAMESPACE();

#if HWY_ONCE

namespace hwy {
namespace {
HWY_BEFORE_TEST(FindTest);
HWY_EXPORT_AND_TEST_P(FindTest, TestAllFind);
HWY_EXPORT_AND_TEST_P(FindTest, TestAllFindIf);
HWY_AFTER_TEST();
} // namespace
} // namespace hwy

#endif
HWY_TEST_MAIN();
#endif // HWY_ONCE
Loading

0 comments on commit b76001d

Please sign in to comment.