Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Unified Diff: base/sys_byteorder.h

Issue 2077563002: base: Use compiler builtins for byte swapping (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add <stdlib.h> to sys_byteorder.h Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/base.gyp ('k') | base/sys_byteorder_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/sys_byteorder.h
diff --git a/base/sys_byteorder.h b/base/sys_byteorder.h
index ddb3f5bcda405fc07be49866304419bb0b7a55ff..8d9066c70228bfd7c36ec0cb3a2afe30dc006a2d 100644
--- a/base/sys_byteorder.h
+++ b/base/sys_byteorder.h
@@ -15,27 +15,35 @@
#include "build/build_config.h"
+#if defined(COMPILER_MSVC)
+#include <stdlib.h>
+#endif
+
namespace base {
// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
inline uint16_t ByteSwap(uint16_t x) {
- return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
+#if defined(COMPILER_MSVC)
+ return _byteswap_ushort(x);
+#else
+ return __builtin_bswap16(x);
+#endif
}
inline uint32_t ByteSwap(uint32_t x) {
- return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) |
- ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24);
+#if defined(COMPILER_MSVC)
+ return _byteswap_ulong(x);
+#else
+ return __builtin_bswap32(x);
+#endif
}
inline uint64_t ByteSwap(uint64_t x) {
- return ((x & 0x00000000000000ffull) << 56) |
- ((x & 0x000000000000ff00ull) << 40) |
- ((x & 0x0000000000ff0000ull) << 24) |
- ((x & 0x00000000ff000000ull) << 8) |
- ((x & 0x000000ff00000000ull) >> 8) |
- ((x & 0x0000ff0000000000ull) >> 24) |
- ((x & 0x00ff000000000000ull) >> 40) |
- ((x & 0xff00000000000000ull) >> 56);
+#if defined(COMPILER_MSVC)
+ return _byteswap_uint64(x);
+#else
+ return __builtin_bswap64(x);
+#endif
}
// Converts the bytes in |x| from host order (endianness) to little endian, and
« no previous file with comments | « base/base.gyp ('k') | base/sys_byteorder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698