Skip to content

Commit 7e3054f

Browse files
Use std::endian to check for endianess. (#5668)
This allows making the endianness check `constexpr`. --- TYPE: NO_HISTORY
1 parent 383faf0 commit 7e3054f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

tiledb/sm/misc/endian.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#ifndef TILEDB_MISC_ENDIAN_H
3535
#define TILEDB_MISC_ENDIAN_H
3636

37+
#include <bit>
3738
#include <climits>
3839
#include <cstdint>
3940
#include <type_traits>
@@ -48,16 +49,15 @@ namespace tiledb::sm::utils::endianness {
4849
* Returns true if the current CPU architecture has little endian byte
4950
* ordering, false for big endian.
5051
*/
51-
inline bool is_little_endian() {
52-
const int n = 1;
53-
return *(char*)&n == 1;
52+
inline constexpr bool is_little_endian() {
53+
return std::endian::native == std::endian::little;
5454
}
5555

5656
/**
5757
* Returns true if the current CPU architecture has big endian byte
5858
* ordering, false for little endian.
5959
*/
60-
inline bool is_big_endian() {
60+
inline constexpr bool is_big_endian() {
6161
return !is_little_endian();
6262
}
6363

@@ -85,7 +85,7 @@ constexpr U bswap(T i) {
8585
template <class T>
8686
inline T decode_le(const void* const data) {
8787
const T* const n = reinterpret_cast<const T* const>(data);
88-
if (is_little_endian()) {
88+
if constexpr (is_little_endian()) {
8989
return *n;
9090
}
9191

@@ -99,7 +99,7 @@ inline T decode_le(const void* const data) {
9999
template <class T>
100100
inline T decode_be(const void* const data) {
101101
const T* const n = reinterpret_cast<const T* const>(data);
102-
if (is_big_endian()) {
102+
if constexpr (is_big_endian()) {
103103
return *n;
104104
}
105105

@@ -112,7 +112,7 @@ inline T decode_be(const void* const data) {
112112
template <class T>
113113
inline void encode_be(const T value, void* const data) {
114114
T* const n = reinterpret_cast<T* const>(data);
115-
if (is_big_endian()) {
115+
if constexpr (is_big_endian()) {
116116
*n = value;
117117
return;
118118
}

0 commit comments

Comments
 (0)