Skip to content

Commit

Permalink
Clean up builtins/endianness helpers for MSVC/clang-cl
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 9, 2021
1 parent 3c89cc1 commit 221dc56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/ethash/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#pragma once

#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
#include <intrin.h>

#ifdef __cplusplus
Expand Down
44 changes: 18 additions & 26 deletions lib/ethash/endianness.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018-2019 Pawel Bylica.
// Copyright 2018 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.

/// @file
Expand All @@ -11,39 +11,31 @@

#pragma once

#include "../support/attributes.h"
#include <ethash/ethash.hpp>

#if _WIN32

#include <stdlib.h>

#define bswap32 _byteswap_ulong
#define bswap64 _byteswap_uint64

// On Windows assume little endian.
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __LITTLE_ENDIAN

#elif __APPLE__

#include <machine/endian.h>

#define bswap32 __builtin_bswap32
#define bswap64 __builtin_bswap64

#ifndef __BYTE_ORDER__
#if defined(_WIN32) // On Windows assume little endian.
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_BIG_ENDIAN__ 4321
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#else
#error "Unknown endianness"
#endif
#endif

#include <endian.h>

#if __has_builtin(__builtin_bswap64) || defined(__GNUC__)
#define bswap32 __builtin_bswap32
#define bswap64 __builtin_bswap64

#elif defined(_MSC_VER)
#include <stdlib.h>
#define bswap32 _byteswap_ulong
#define bswap64 _byteswap_uint64
#endif

namespace ethash
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

struct le
{
Expand All @@ -61,7 +53,7 @@ struct be
};


#elif __BYTE_ORDER == __BIG_ENDIAN
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__

struct le
{
Expand Down Expand Up @@ -96,4 +88,4 @@ struct be
};

#endif
} // namespace ethash
} // namespace ethash

0 comments on commit 221dc56

Please sign in to comment.