From d0d5d4a5f29c8657b0d57563483205ab651a10b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 9 Nov 2021 16:40:29 +0100 Subject: [PATCH] Remove builtins.h, move clz() to where it is used --- lib/ethash/CMakeLists.txt | 1 - lib/ethash/builtins.h | 43 -------------------------------- test/experimental/difficulty.cpp | 19 ++++++++++++-- 3 files changed, 17 insertions(+), 46 deletions(-) delete mode 100644 lib/ethash/builtins.h diff --git a/lib/ethash/CMakeLists.txt b/lib/ethash/CMakeLists.txt index d1a6d65b..973870e0 100644 --- a/lib/ethash/CMakeLists.txt +++ b/lib/ethash/CMakeLists.txt @@ -11,7 +11,6 @@ set_target_properties(ethash PROPERTIES C_EXTENSIONS OFF CXX_EXTENSIONS OFF) target_link_libraries(ethash PRIVATE ethash::keccak) target_include_directories(ethash PUBLIC $$) target_sources(ethash PRIVATE - builtins.h endianness.hpp ${include_dir}/ethash/ethash.h ${include_dir}/ethash/ethash.hpp diff --git a/lib/ethash/builtins.h b/lib/ethash/builtins.h deleted file mode 100644 index e2571841..00000000 --- a/lib/ethash/builtins.h +++ /dev/null @@ -1,43 +0,0 @@ -/* ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm. - * Copyright 2018-2020 Pawel Bylica. - * Licensed under the Apache License, Version 2.0. - */ - -/** - * @file - * Implementation of GCC/clang builtins for MSVC compiler. - */ - -#pragma once - -#if defined(_MSC_VER) && !defined(__clang__) -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Returns the number of leading 0-bits in `x`, starting at the most significant bit position. - * If `x` is 0, the result is undefined. - */ -inline int __builtin_clz(unsigned int x) -{ - unsigned long most_significant_bit; - _BitScanReverse(&most_significant_bit, x); - return 31 - (int)most_significant_bit; -} - -/** - * Returns the number of 1-bits in `x`. - */ -inline int __builtin_popcount(unsigned int x) -{ - return (int)__popcnt(x); -} - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/test/experimental/difficulty.cpp b/test/experimental/difficulty.cpp index f5467184..ab100633 100644 --- a/test/experimental/difficulty.cpp +++ b/test/experimental/difficulty.cpp @@ -3,11 +3,26 @@ // Licensed under the Apache License, Version 2.0. #include "difficulty.h" -#include "../../lib/ethash/builtins.h" #include "../../lib/ethash/endianness.hpp" +#if defined(_MSC_VER) && !defined(__clang__) +#include +#endif + #pragma clang diagnostic ignored "-Wunknown-sanitizers" +inline int clz(uint32_t x) noexcept +{ +#if defined(_MSC_VER) && !defined(__clang__) + unsigned long most_significant_bit; + _BitScanReverse(&most_significant_bit, x); + return 31 - (int)most_significant_bit; +#else + return x != 0 ? __builtin_clz(x) : 32; +#endif +} + + extern "C" { NO_SANITIZE("unsigned-integer-overflow") NO_SANITIZE("unsigned-shift-base") @@ -40,7 +55,7 @@ ethash_hash256 ethash_difficulty_to_boundary(const ethash_hash256* difficulty) n // Normalize d. uint32_t dn[num_words]; - const int shift = __builtin_clz(d[n - 1]); + const int shift = clz(d[n - 1]); for (int i = n - 1; i > 0; i--) dn[i] = shift ? (d[i] << shift) | (d[i - 1] >> (32 - shift)) : d[i]; dn[0] = d[0] << shift;