Skip to content

Commit d28e14c

Browse files
authored
Merge pull request #2218 from serge-sans-paille/feature/use-xtl-platform
Use xtl::endianness instead of bundling it
2 parents e3219e9 + 00fa064 commit d28e14c

4 files changed

Lines changed: 37 additions & 25 deletions

File tree

.azure-pipelines/azure-pipelines-win.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
conda install cmake==3.14.0 ^
3838
ninja ^
3939
nlohmann_json ^
40-
xtl==0.6.20 ^
40+
xtl==0.6.21 ^
4141
xsimd==7.4.8 ^
4242
python=3.6
4343
conda list

environment-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ channels:
33
- conda-forge
44
dependencies:
55
- cmake
6-
- xtl=0.6.20
6+
- xtl=0.6.21
77
- xsimd=7.4.8
88
- nlohmann_json

include/xtensor/xnpy.hpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// relicensed from MIT License with permission
1616

1717
#include <xtl/xsequence.hpp>
18+
#include <xtl/xplatform.hpp>
1819

1920
#include <algorithm>
2021
#include <complex>
@@ -43,24 +44,9 @@ namespace xt
4344
namespace detail
4445
{
4546

46-
/* Test for endianess. Compiler can optimize that to a single constant. */
47-
static inline bool is_big_endian()
48-
{
49-
uint32_t utmp = 0x01020304;
50-
char btmp[sizeof(utmp)];
51-
std::memcpy(&btmp[0], &utmp, sizeof(utmp));
52-
const bool big_endian = btmp[0] == 0x01;
53-
return big_endian;
54-
}
55-
5647
const char magic_string[] = "\x93NUMPY";
57-
const std::size_t magic_string_length = 6;
48+
const std::size_t magic_string_length = sizeof(magic_string) - 1;
5849

59-
const char little_endian_char = '<';
60-
const char big_endian_char = '>';
61-
const char no_endian_char = '|';
62-
63-
const char host_endian_char = (is_big_endian() ? big_endian_char : little_endian_char);
6450

6551
template <class O>
6652
inline void write_magic(O& ostream,
@@ -126,9 +112,26 @@ namespace xt
126112
}
127113

128114
template <class T>
129-
constexpr char get_endianess()
115+
inline char get_endianess()
130116
{
131-
return sizeof(T) <= sizeof(char) ? no_endian_char : host_endian_char;
117+
constexpr char little_endian_char = '<';
118+
constexpr char big_endian_char = '>';
119+
constexpr char no_endian_char = '|';
120+
121+
if(sizeof(T) <= sizeof(char))
122+
{
123+
return no_endian_char;
124+
}
125+
126+
switch(xtl::endianness())
127+
{
128+
case xtl::endian::little_endian:
129+
return little_endian_char;
130+
case xtl::endian::big_endian:
131+
return big_endian_char;
132+
default:
133+
return no_endian_char;
134+
}
132135
}
133136

134137
template <class T>

test/test_xnpy.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717

1818
namespace xt
1919
{
20-
std::string get_load_filename(std::string const& npy_prefix, layout_type lt = layout_type::row_major) {
21-
using detail::is_big_endian;
22-
std::string lts = lt == layout_type::row_major ? "" : "_fortran";
23-
std::string endianess = is_big_endian() ? ".be" : ".le";
24-
return npy_prefix + lts + endianess + ".npy";
20+
std::string get_load_filename(std::string const& npy_prefix, layout_type lt = layout_type::row_major)
21+
{
22+
std::string lts = lt == layout_type::row_major ? "" : "_fortran";
23+
std::string endianness;
24+
switch(xtl::endianness())
25+
{
26+
case xtl::endian::big_endian:
27+
endianness = ".be";
28+
break;
29+
case xtl::endian::little_endian:
30+
endianness = ".le";
31+
break;
32+
}
33+
return npy_prefix + lts + endianness + ".npy";
2534
}
2635

2736
TEST(xnpy, load)

0 commit comments

Comments
 (0)