1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-12 16:22:22 +01:00

Introduce byteswap helpers (#2661)

* Backport std::byteswap() in helpers.h

* Introduce convert_big_endian() function

* Use convert_big_endian() in i2c byte swap functions
This commit is contained in:
Oxan van Leeuwen
2021-11-10 19:40:18 +01:00
committed by GitHub
parent 8aa72f4c1e
commit c422b2fb0b
2 changed files with 28 additions and 10 deletions

View File

@@ -306,6 +306,31 @@ template<typename T> T *new_buffer(size_t length) {
// ---------------------------------------------------------------------------------------------------------------------
/// @name STL backports
///@{
// std::byteswap is from C++23 and technically should be a template, but this will do for now.
constexpr uint8_t byteswap(uint8_t n) { return n; }
constexpr uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
constexpr uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); }
constexpr uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
///@}
/// @name Bit manipulation
///@{
/// Convert a value between host byte order and big endian (most significant byte first) order.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T convert_big_endian(T val) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return byteswap(val);
#else
return val;
#endif
}
///@}
/// @name Parsing & formatting
///@{