Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
2d427e05
Commit
2d427e05
authored
Jul 25, 2016
by
Wouter van Oortmerssen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pulled out EndianSwap into its own function.
Change-Id: I4a587102db8c435b739c92b6c464c94c4ea1fe42
parent
1bba4fd9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
29 deletions
+31
-29
flatbuffers.h
include/flatbuffers/flatbuffers.h
+31
-29
No files found.
include/flatbuffers/flatbuffers.h
View file @
2d427e05
...
@@ -151,39 +151,41 @@ inline void EndianCheck() {
...
@@ -151,39 +151,41 @@ inline void EndianCheck() {
(
void
)
endiantest
;
(
void
)
endiantest
;
}
}
template
<
typename
T
>
T
EndianSwap
(
T
t
)
{
#if defined(_MSC_VER)
#pragma push_macro("__builtin_bswap16")
#pragma push_macro("__builtin_bswap32")
#pragma push_macro("__builtin_bswap64")
#define __builtin_bswap16 _byteswap_ushort
#define __builtin_bswap32 _byteswap_ulong
#define __builtin_bswap64 _byteswap_uint64
#endif
if
(
sizeof
(
T
)
==
1
)
{
// Compile-time if-then's.
return
t
;
}
else
if
(
sizeof
(
T
)
==
2
)
{
auto
r
=
__builtin_bswap16
(
*
reinterpret_cast
<
uint16_t
*>
(
&
t
));
return
*
reinterpret_cast
<
T
*>
(
&
r
);
}
else
if
(
sizeof
(
T
)
==
4
)
{
auto
r
=
__builtin_bswap32
(
*
reinterpret_cast
<
uint32_t
*>
(
&
t
));
return
*
reinterpret_cast
<
T
*>
(
&
r
);
}
else
if
(
sizeof
(
T
)
==
8
)
{
auto
r
=
__builtin_bswap64
(
*
reinterpret_cast
<
uint64_t
*>
(
&
t
));
return
*
reinterpret_cast
<
T
*>
(
&
r
);
}
else
{
assert
(
0
);
}
#if defined(_MSC_VER)
#pragma pop_macro("__builtin_bswap16")
#pragma pop_macro("__builtin_bswap32")
#pragma pop_macro("__builtin_bswap64")
#endif
}
template
<
typename
T
>
T
EndianScalar
(
T
t
)
{
template
<
typename
T
>
T
EndianScalar
(
T
t
)
{
#if FLATBUFFERS_LITTLEENDIAN
#if FLATBUFFERS_LITTLEENDIAN
return
t
;
return
t
;
#else
#else
#if defined(_MSC_VER)
return
EndianSwap
(
t
);
#pragma push_macro("__builtin_bswap16")
#pragma push_macro("__builtin_bswap32")
#pragma push_macro("__builtin_bswap64")
#define __builtin_bswap16 _byteswap_ushort
#define __builtin_bswap32 _byteswap_ulong
#define __builtin_bswap64 _byteswap_uint64
#endif
// If you're on the few remaining big endian platforms, we make the bold
// assumption you're also on gcc/clang, and thus have bswap intrinsics:
if
(
sizeof
(
T
)
==
1
)
{
// Compile-time if-then's.
return
t
;
}
else
if
(
sizeof
(
T
)
==
2
)
{
auto
r
=
__builtin_bswap16
(
*
reinterpret_cast
<
uint16_t
*>
(
&
t
));
return
*
reinterpret_cast
<
T
*>
(
&
r
);
}
else
if
(
sizeof
(
T
)
==
4
)
{
auto
r
=
__builtin_bswap32
(
*
reinterpret_cast
<
uint32_t
*>
(
&
t
));
return
*
reinterpret_cast
<
T
*>
(
&
r
);
}
else
if
(
sizeof
(
T
)
==
8
)
{
auto
r
=
__builtin_bswap64
(
*
reinterpret_cast
<
uint64_t
*>
(
&
t
));
return
*
reinterpret_cast
<
T
*>
(
&
r
);
}
else
{
assert
(
0
);
}
#if defined(_MSC_VER)
#pragma pop_macro("__builtin_bswap16")
#pragma pop_macro("__builtin_bswap32")
#pragma pop_macro("__builtin_bswap64")
#endif
#endif
#endif
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment