Skip to content

Commit

Permalink
precompiles: Improve SHA256 code quality
Browse files Browse the repository at this point in the history
Improve SHA256 code quality by fixing some clang-tidy warnings.
  • Loading branch information
chfast committed Jun 10, 2024
1 parent 65a5518 commit 985dbdb
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/evmone_precompiles/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static const uint32_t k[] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb,
0xbef9a3f7, 0xc67178f2};

struct buffer_state
struct BufferState
{
const uint8_t* p;
size_t len;
Expand All @@ -92,7 +92,7 @@ static inline uint32_t right_rot(uint32_t value, unsigned int count)
return value >> count | value << (32 - count);
}

static void init_buf_state(struct buffer_state* state, const uint8_t* input, size_t len)
static void init_buf_state(struct BufferState* state, const uint8_t* input, size_t len)
{
state->p = input;
state->len = len;
Expand All @@ -101,7 +101,7 @@ static void init_buf_state(struct buffer_state* state, const uint8_t* input, siz
state->total_len_delivered = false;
}

static bool calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state* state)
static bool calc_chunk(uint8_t chunk[CHUNK_SIZE], struct BufferState* state)
{
if (state->total_len_delivered)
{
Expand All @@ -117,7 +117,7 @@ static bool calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state* state)
}

size_t space_in_chunk = CHUNK_SIZE - state->len;
if (state->len)
if (state->len != 0)
{ // avoid adding 0 to nullptr
memcpy(chunk, state->p, state->len);
chunk += state->len;
Expand Down Expand Up @@ -182,15 +182,16 @@ static inline ALWAYS_INLINE void sha_256_implementation(
* the first word of the input message "abc" after padding is 0x61626380
*/

struct buffer_state state;
struct BufferState state; // NOLINT(*-pro-type-member-init)
init_buf_state(&state, input, len);

/* 512-bit chunks is what we will operate on. */
uint8_t chunk[CHUNK_SIZE];

while (calc_chunk(chunk, &state))
{
unsigned i = 0, j = 0;
unsigned i = 0;
unsigned j = 0;

uint32_t ah[8];
/* Initialize working variables to current hash value: */
Expand Down Expand Up @@ -293,12 +294,17 @@ __attribute__((target("bmi,bmi2"))) static void sha_256_x86_bmi(
__attribute__((target("sha,sse4.1"))) static void sha_256_x86_sha(

Check warning on line 294 in lib/evmone_precompiles/sha256.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone_precompiles/sha256.cpp#L294

Added line #L294 was not covered by tests
uint32_t h[8], const uint8_t* input, size_t len)
{
// NOLINTBEGIN(readability-isolate-declaration)
__m128i STATE0, STATE1;
__m128i MSG, TMP;
__m128i MSG0, MSG1, MSG2, MSG3;
__m128i ABEF_SAVE, CDGH_SAVE;
const __m128i MASK = _mm_set_epi64x(0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL); // NOLINT
// NOLINTEND(readability-isolate-declaration)

const __m128i MASK = _mm_set_epi64x(0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL);

Check warning on line 304 in lib/evmone_precompiles/sha256.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone_precompiles/sha256.cpp#L304

Added line #L304 was not covered by tests

// NOLINTBEGIN(cppcoreguidelines-pro-type-cstyle-cast)
// NOLINTBEGIN(portability-simd-intrinsics)
/* Load initial values */
TMP = _mm_loadu_si128((const __m128i*)&h[0]);
STATE1 = _mm_loadu_si128((const __m128i*)&h[4]);

Check warning on line 310 in lib/evmone_precompiles/sha256.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone_precompiles/sha256.cpp#L309-L310

Added lines #L309 - L310 were not covered by tests
Expand All @@ -308,7 +314,7 @@ __attribute__((target("sha,sse4.1"))) static void sha_256_x86_sha(
STATE0 = _mm_alignr_epi8(TMP, STATE1, 8); /* ABEF */
STATE1 = _mm_blend_epi16(STATE1, TMP, 0xF0); /* CDGH */

Check warning on line 315 in lib/evmone_precompiles/sha256.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone_precompiles/sha256.cpp#L312-L315

Added lines #L312 - L315 were not covered by tests

struct buffer_state state;
struct BufferState state; // NOLINT(*-pro-type-member-init)
init_buf_state(&state, input, len);

Check warning on line 318 in lib/evmone_precompiles/sha256.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone_precompiles/sha256.cpp#L318

Added line #L318 was not covered by tests

/* 512-bit chunks is what we will operate on. */
Expand Down Expand Up @@ -501,21 +507,23 @@ __attribute__((target("sha,sse4.1"))) static void sha_256_x86_sha(
/* Save state */
_mm_storeu_si128((__m128i*)&h[0], STATE0);
_mm_storeu_si128((__m128i*)&h[4], STATE1);

Check warning on line 509 in lib/evmone_precompiles/sha256.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone_precompiles/sha256.cpp#L509

Added line #L509 was not covered by tests
// NOLINTEND(portability-simd-intrinsics)
// NOLINTEND(cppcoreguidelines-pro-type-cstyle-cast)
}

#pragma GCC diagnostic pop

// https://stackoverflow.com/questions/6121792/how-to-check-if-a-cpu-supports-the-sse3-instruction-set
static void cpuid(int info[4], int InfoType)
{ // NOLINT(readability-non-const-parameter)
static void cpuid(int info[4], int InfoType) // NOLINT(readability-non-const-parameter)
{
__cpuid_count(InfoType, 0, info[0], info[1], info[2], info[3]);
}

__attribute__((constructor)) static void select_sha256_implementation(void)
__attribute__((constructor)) static void select_sha256_implementation()
{
int info[4];
cpuid(info, 0);
int nIds = info[0];
const int nIds = info[0];

bool hw_sse41 = false;
bool hw_bmi1 = false;
Expand Down

0 comments on commit 985dbdb

Please sign in to comment.