Skip to content

Commit 7f6ab02

Browse files
committed
More endian functions.
1 parent 19e21dd commit 7f6ab02

File tree

7 files changed

+197
-64
lines changed

7 files changed

+197
-64
lines changed

include/tao/json/events/cbor/grammar.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "major.hpp"
1111

1212
#include "../../byte.hpp"
13-
#include "../../internal/temporary.hpp"
13+
#include "../../internal/endian.hpp"
1414

1515
namespace tao
1616
{

include/tao/json/events/cbor/to_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "major.hpp"
1212

1313
#include "../../byte.hpp"
14-
#include "../../internal/temporary.hpp"
14+
#include "../../internal/endian.hpp"
1515

1616
namespace tao
1717
{

include/tao/json/events/msgpack/to_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <string>
1111

1212
#include "../../byte.hpp"
13-
#include "../../internal/temporary.hpp"
13+
#include "../../internal/endian.hpp"
1414

1515
namespace tao
1616
{

include/tao/json/events/ubjson/to_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <string>
1111

1212
#include "../../byte.hpp"
13-
#include "../../internal/temporary.hpp"
13+
#include "../../internal/endian.hpp"
1414

1515
namespace tao
1616
{
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#ifndef TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_HPP
5+
#define TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_HPP
6+
7+
#include <cstdint>
8+
#include <cstring>
9+
10+
// TODO: Where to put this header?
11+
12+
#if defined( _WIN32 ) && !defined( __MINGW32__ )
13+
#include "endian_win.hpp"
14+
#else
15+
#include "endian_gcc.hpp"
16+
#endif
17+
18+
namespace tao
19+
{
20+
namespace json
21+
{
22+
namespace internal
23+
{
24+
template< typename N >
25+
N h_to_be( const N n ) noexcept
26+
{
27+
return N( to_and_from_be< sizeof( N ) >::convert( n ) );
28+
}
29+
30+
template< typename N >
31+
N be_to_h( const N n ) noexcept
32+
{
33+
return h_to_be( n );
34+
}
35+
36+
template< typename N >
37+
N be_to_h( const void* p ) noexcept
38+
{
39+
N n;
40+
std::memcpy( &n, p, sizeof( n ) );
41+
return internal::be_to_h( n );
42+
}
43+
44+
template< typename N >
45+
N h_to_le( const N n ) noexcept
46+
{
47+
return N( to_and_from_le< sizeof( N ) >::convert( n ) );
48+
}
49+
50+
template< typename N >
51+
N le_to_h( const N n ) noexcept
52+
{
53+
return h_to_le( n );
54+
}
55+
56+
template< typename N >
57+
N le_to_h( const void* p ) noexcept
58+
{
59+
N n;
60+
std::memcpy( &n, p, sizeof( n ) );
61+
return internal::le_to_h( n );
62+
}
63+
64+
} // namespace internal
65+
66+
} // namespace json
67+
68+
} // namespace tao
69+
70+
#endif
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
// Copyright (c) 2017 Dr. Colin Hirsch and Daniel Frey
22
// Please see LICENSE for license or visit https://github.com/taocpp/json/
33

4-
#ifndef TAOCPP_JSON_INCLUDE_INTERNAL_TEMPORARY_HPP
5-
#define TAOCPP_JSON_INCLUDE_INTERNAL_TEMPORARY_HPP
6-
7-
#include <cstdint>
8-
#include <cstring>
9-
10-
#if defined( _WIN32 ) && !defined( __MINGW32__ )
11-
#include <stdlib.h> // TODO: Or is intrin.h the 'more correct' header?
12-
#endif
13-
14-
// TODO: Better endian detection?
15-
// TODO: Where to put this header?
4+
#ifndef TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_GCC_HPP
5+
#define TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_GCC_HPP
166

177
namespace tao
188
{
199
namespace json
2010
{
2111
namespace internal
2212
{
13+
#if not defined( __BYTE_ORDER__ )
14+
#error TODO -- what?
15+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
2316

24-
#if defined( _WIN32 ) && !defined( __MINGW32__ )
17+
template< unsigned S >
18+
struct to_and_from_be
19+
{
20+
template< typename T >
21+
static T convert( const T n ) noexcept
22+
{
23+
return n;
24+
}
25+
};
2526

2627
template< unsigned S >
27-
struct bswap;
28+
struct to_and_from_le;
2829

2930
template<>
30-
struct bswap< 1 >
31+
struct to_and_from_le< 1 >
3132
{
3233
static std::uint8_t convert( const std::uint8_t n ) noexcept
3334
{
@@ -36,16 +37,16 @@ namespace tao
3637
};
3738

3839
template<>
39-
struct bswap< 2 >
40+
struct to_and_from_le< 2 >
4041
{
4142
static std::uint16_t convert( const std::uint16_t n ) noexcept
4243
{
43-
return _byteswap_ushort( n );
44+
return __builtin_bswap16( n );
4445
}
4546
};
4647

4748
template<>
48-
struct bswap< 4 >
49+
struct to_and_from_le< 4 >
4950
{
5051
static float convert( float n ) noexcept
5152
{
@@ -58,12 +59,12 @@ namespace tao
5859

5960
static std::uint32_t convert( const std::uint32_t n ) noexcept
6061
{
61-
return _byteswap_ulong( n );
62+
return __builtin_bswap32( n );
6263
}
6364
};
6465

6566
template<>
66-
struct bswap< 8 >
67+
struct to_and_from_le< 8 >
6768
{
6869
static double convert( double n ) noexcept
6970
{
@@ -76,18 +77,14 @@ namespace tao
7677

7778
static std::uint64_t convert( const std::uint64_t n ) noexcept
7879
{
79-
return _byteswap_uint64( n );
80+
return __builtin_bswap64( n );
8081
}
8182
};
8283

83-
#elif not defined( __BYTE_ORDER__ )
84-
85-
#error TODO!
86-
87-
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
84+
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
8885

8986
template< unsigned S >
90-
struct bswap
87+
struct to_and_from_le
9188
{
9289
template< typename T >
9390
static T convert( const T n ) noexcept
@@ -96,13 +93,11 @@ namespace tao
9693
}
9794
};
9895

99-
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
100-
10196
template< unsigned S >
102-
struct bswap;
97+
struct to_and_from_be;
10398

10499
template<>
105-
struct bswap< 1 >
100+
struct to_and_from_be< 1 >
106101
{
107102
static std::uint8_t convert( const std::uint8_t n ) noexcept
108103
{
@@ -111,7 +106,7 @@ namespace tao
111106
};
112107

113108
template<>
114-
struct bswap< 2 >
109+
struct to_and_from_be< 2 >
115110
{
116111
static std::uint16_t convert( const std::uint16_t n ) noexcept
117112
{
@@ -120,7 +115,7 @@ namespace tao
120115
};
121116

122117
template<>
123-
struct bswap< 4 >
118+
struct to_and_from_be< 4 >
124119
{
125120
static float convert( float n ) noexcept
126121
{
@@ -138,7 +133,7 @@ namespace tao
138133
};
139134

140135
template<>
141-
struct bswap< 8 >
136+
struct to_and_from_be< 8 >
142137
{
143138
static double convert( double n ) noexcept
144139
{
@@ -156,35 +151,12 @@ namespace tao
156151
};
157152

158153
#else
159-
160154
#error Unknown host byte order!
161-
162155
#endif
156+
} // internal
163157

164-
template< typename N >
165-
N h_to_be( const N n ) noexcept
166-
{
167-
return N( bswap< sizeof( N ) >::convert( n ) );
168-
}
169-
170-
template< typename N >
171-
N be_to_h( const N n ) noexcept
172-
{
173-
return h_to_be( n );
174-
}
175-
176-
template< typename N >
177-
N be_to_h( const void* p ) noexcept
178-
{
179-
N n;
180-
std::memcpy( &n, p, sizeof( n ) );
181-
return internal::be_to_h( n );
182-
}
183-
184-
} // namespace internal
185-
186-
} // namespace json
158+
} // json
187159

188-
} // namespace tao
160+
} // tao
189161

190162
#endif
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#ifndef TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_WIN_HPP
5+
#define TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_WIN_HPP
6+
7+
#include <cstdint>
8+
#include <cstring>
9+
10+
#include <stdlib.h> // TODO: Or is intrin.h the 'more correct' header for the _byteswap_foo() functions?
11+
12+
namespace tao
13+
{
14+
namespace json
15+
{
16+
namespace internal
17+
{
18+
template< unsigned S >
19+
struct to_and_from_le
20+
{
21+
template< typename T >
22+
static T convert( const T t ) noexcept
23+
{
24+
return t;
25+
}
26+
};
27+
28+
template< unsigned S >
29+
struct to_and_from_be;
30+
31+
template<>
32+
struct to_and_from_be< 1 >
33+
{
34+
static std::uint8_t convert( const std::uint8_t n ) noexcept
35+
{
36+
return n;
37+
}
38+
};
39+
40+
template<>
41+
struct to_and_from_be< 2 >
42+
{
43+
static std::uint16_t convert( const std::uint16_t n ) noexcept
44+
{
45+
return _byteswap_ushort( n );
46+
}
47+
};
48+
49+
template<>
50+
struct to_and_from_be< 4 >
51+
{
52+
static float convert( float n ) noexcept
53+
{
54+
std::uint32_t u;
55+
std::memcpy( &u, &n, 4 );
56+
u = convert( u );
57+
std::memcpy( &n, &u, 4 );
58+
return n;
59+
}
60+
61+
static std::uint32_t convert( const std::uint32_t n ) noexcept
62+
{
63+
return _byteswap_ulong( n );
64+
}
65+
};
66+
67+
template<>
68+
struct to_and_from_be< 8 >
69+
{
70+
static double convert( double n ) noexcept
71+
{
72+
std::uint64_t u;
73+
std::memcpy( &u, &n, 8 );
74+
u = convert( u );
75+
std::memcpy( &n, &u, 8 );
76+
return n;
77+
}
78+
79+
static std::uint64_t convert( const std::uint64_t n ) noexcept
80+
{
81+
return _byteswap_uint64( n );
82+
}
83+
};
84+
85+
} // namespace internal
86+
87+
} // namespace json
88+
89+
} // namespace tao
90+
91+
#endif

0 commit comments

Comments
 (0)