Skip to content

Instantly share code, notes, and snippets.

@uchan-nos
Created March 20, 2019 04:18
Show Gist options
  • Save uchan-nos/b6c18a25fd0d1593989fa41ffad7e7d7 to your computer and use it in GitHub Desktop.
Save uchan-nos/b6c18a25fd0d1593989fa41ffad7e7d7 to your computer and use it in GitHub Desktop.
#include <cstdint>
template <class T>
T ConvEndian(T orig) {
T result{};
for (int i = 0; i < sizeof(T); ++i) {
result <<= 8;
result |= orig & 0xff;
orig >>= 8;
}
return result;
}
uint16_t f(uint16_t value) {
// 最適化されて rol 命令になる
return ConvEndian(value);
}
uint32_t f(uint32_t value) {
// 最適化されて bswap 命令になる
return ConvEndian(value);
}
uint64_t f(uint64_t value) {
// 最適化されて bswap 命令になる
return ConvEndian(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment